Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/ipc/pty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5912,6 +5912,17 @@ export function registerPtyHandlers(
return null
}
},
supportsProviderBufferSnapshot: async (ptyId, signal) => {
try {
const provider = getProviderForPty(ptyId)
if (provider.probeAuthoritativeBufferSnapshotSupport) {
return await provider.probeAuthoritativeBufferSnapshotSupport({ signal })
}
return provider.canProvideAuthoritativeBufferSnapshot?.(ptyId) === true
} catch {
return false
}
},
hasRendererSerializer: (ptyId) => {
// Why: a synchronous probe lets the runtime decide whether to skip the daemon-snapshot seed (renderer will hydrate) or run it (no renderer authoritative).
return rendererSerializerReadiness.has(ptyId)
Expand Down
6 changes: 6 additions & 0 deletions src/main/providers/pty-provider-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export type PtyProviderBufferSnapshot = {
cwd?: string | null
lastTitle?: string
seq: number
/** Process identity that owned the provider screen at this sequence. */
incarnationId?: PtyIncarnationId
source: 'headless'
oscLinks?: TerminalOscLinkRange[]
alternateScreen?: boolean
Expand Down Expand Up @@ -178,6 +180,10 @@ export type IPtyProvider = {
) => Promise<PtyProviderBufferSnapshot | null>
/** Whether this exact PTY can return a sequence-safe provider snapshot. */
canProvideAuthoritativeBufferSnapshot?: (id: string) => boolean
/** Negotiated proof that provider snapshots are available on this execution host. */
probeAuthoritativeBufferSnapshotSupport?: (
options?: PtyProbeOptions
) => boolean | Promise<boolean>
/**
* The size the PTY has ACTUALLY applied, not the last size requested.
* resize() is fire-and-forget for remote providers (daemon/SSH `notify`),
Expand Down
4 changes: 4 additions & 0 deletions src/main/providers/ssh-pty-provider-output-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export class SshPtyProviderOutputState {
}
}

ptyIncarnation(relayPtyId: string): string | undefined {
return this.incarnationByRelayPtyId.get(relayPtyId)
}

private resolvePtyIncarnation(relayPtyId: string, incarnationId: unknown): string {
this.rememberPtyIncarnation(relayPtyId, incarnationId)
let resolved = this.incarnationByRelayPtyId.get(relayPtyId)
Expand Down
67 changes: 67 additions & 0 deletions src/main/providers/ssh-pty-provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it, vi, beforeEach } from 'vitest'
import { SshPtyProvider } from './ssh-pty-provider'
import { AGENT_SESSION_EXECUTION_OWNER_PROTOCOL_VERSION } from '../../shared/agent-session-host-authority'
import { PTY_VISIBLE_SCREEN_SNAPSHOT_PROTOCOL_VERSION } from '../../shared/pty-visible-screen-snapshot'
import {
createMockMux,
expectRequest,
Expand All @@ -25,6 +26,72 @@ describe('SshPtyProvider', () => {
expect(provider.canProvideAuthoritativeBufferSnapshot(scopedPty1)).toBe(false)
})

it('negotiates authoritative visible-screen snapshots', async () => {
mux.request.mockResolvedValue({
visibleScreenSnapshotVersion: PTY_VISIBLE_SCREEN_SNAPSHOT_PROTOCOL_VERSION
})

await expect(provider.probeAuthoritativeBufferSnapshotSupport()).resolves.toBe(true)
expect(provider.canProvideAuthoritativeBufferSnapshot(scopedPty1)).toBe(true)
expectRequest(mux.request, 'pty.getCapabilities', undefined, { timeoutMs: 5_000 })
})

it('keeps old SSH relays snapshot-ineligible', async () => {
mux.request.mockResolvedValue({})

await expect(provider.probeAuthoritativeBufferSnapshotSupport()).resolves.toBe(false)
expect(provider.canProvideAuthoritativeBufferSnapshot(scopedPty1)).toBe(false)
})

it('returns an authoritative snapshot for the current incarnation', async () => {
mux.request
.mockResolvedValueOnce({
visibleScreenSnapshotVersion: PTY_VISIBLE_SCREEN_SNAPSHOT_PROTOCOL_VERSION
})
.mockResolvedValueOnce({ replay: '', incarnationId: 'incarnation-1' })
.mockResolvedValueOnce({
data: '',
cols: 80,
rows: 24,
seq: 12,
incarnationId: 'incarnation-1',
source: 'headless'
})

await provider.probeAuthoritativeBufferSnapshotSupport()
await provider.attachForReconnect(scopedPty1)

await expect(provider.getBufferSnapshot(scopedPty1)).resolves.toEqual({
data: '',
cols: 80,
rows: 24,
seq: 12,
incarnationId: 'incarnation-1',
source: 'headless'
})
})

it('rejects a visible-screen snapshot from another incarnation', async () => {
mux.request
.mockResolvedValueOnce({
visibleScreenSnapshotVersion: PTY_VISIBLE_SCREEN_SNAPSHOT_PROTOCOL_VERSION
})
.mockResolvedValueOnce({ replay: '', incarnationId: 'incarnation-1' })
.mockResolvedValueOnce({
data: 'wrong owner',
cols: 80,
rows: 24,
seq: 12,
incarnationId: 'incarnation-2',
source: 'headless'
})

await provider.probeAuthoritativeBufferSnapshotSupport()
await provider.attachForReconnect(scopedPty1)

await expect(provider.getBufferSnapshot(scopedPty1)).resolves.toBeNull()
})

it('keeps a shared claim probe alive when one waiter disconnects', async () => {
let finishProbe!: (result: { agentSessionClaimVersion: number }) => void
mux.request.mockReturnValueOnce(
Expand Down
20 changes: 19 additions & 1 deletion src/main/providers/ssh-pty-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { SshPtySpawnExitRaceTracker } from './ssh-pty-spawn-exit-race'
import { SshAgentSessionCapabilities } from './ssh-agent-session-capabilities'
import type { PtyProcessInspection } from './pty-process-inspection'
import { writeToSshPty, writeToSshPtyWithSettlement } from './ssh-pty-write'
import { SshPtyVisibleScreenSnapshots } from './ssh-pty-visible-screen-snapshot'

// Why: sequential relay teardown calls share one absolute budget; convert to the mux-relative timeout only at dispatch.
function relayTimeoutOptions(deadlineMs: number | undefined): { timeoutMs: number } | undefined {
Expand All @@ -38,6 +39,7 @@ export class SshPtyProvider implements IPtyProvider {
private readonly agentSessionCapabilities: SshAgentSessionCapabilities
private spawnExitRaces = new SshPtySpawnExitRaceTracker()
private readonly outputState: SshPtyProviderOutputState
private readonly visibleScreenSnapshots: SshPtyVisibleScreenSnapshots

requestHostRpc: NonNullable<IPtyProvider['requestHostRpc']> = (method, params, options) =>
this.mux.request(method, params as Record<string, unknown>, options)
Expand All @@ -61,6 +63,11 @@ export class SshPtyProvider implements IPtyProvider {
this.spawnExitRaces.recordExit(relayPtyId, incarnationId)
}
})
this.visibleScreenSnapshots = new SshPtyVisibleScreenSnapshots(
mux,
(id) => this.toRelayPtyId(id),
(id) => this.outputState.ptyIncarnation(id)
)
}

dispose(): void {
Expand All @@ -70,7 +77,18 @@ export class SshPtyProvider implements IPtyProvider {

getConnectionId = (): string => this.connectionId

canProvideAuthoritativeBufferSnapshot = (_id: string): boolean => false
canProvideAuthoritativeBufferSnapshot = (_id: string): boolean =>
this.visibleScreenSnapshots.canProvide()

async probeAuthoritativeBufferSnapshotSupport(
options: { signal?: AbortSignal } = {}
): Promise<boolean> {
return await this.visibleScreenSnapshots.probe(options)
}

async getBufferSnapshot(id: string, opts: { scrollbackRows?: number } = {}) {
return await this.visibleScreenSnapshots.get(id, opts)
}

private toRelayPtyId = (id: string): string => toRelaySshPtyId(this.connectionId, id)

Expand Down
85 changes: 85 additions & 0 deletions src/main/providers/ssh-pty-visible-screen-snapshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { PTY_VISIBLE_SCREEN_SNAPSHOT_PROTOCOL_VERSION } from '../../shared/pty-visible-screen-snapshot'
import type { SshChannelMultiplexer } from '../ssh/ssh-channel-multiplexer'
import { waitForSshCapabilityProbe } from './ssh-capability-probe-waiter'
import type { PtyProviderBufferSnapshot } from './types'

export class SshPtyVisibleScreenSnapshots {
private capabilityProbe: Promise<boolean> | null = null
private supported = false

constructor(
private readonly mux: SshChannelMultiplexer,
private readonly toRelayPtyId: (id: string) => string,
private readonly readIncarnation: (relayPtyId: string) => string | undefined
) {}

canProvide(): boolean {
return this.supported
}

async probe(options: { signal?: AbortSignal } = {}): Promise<boolean> {
const probe = this.capabilityProbe ?? this.probeCapability()
this.capabilityProbe = probe
try {
const supported = await waitForSshCapabilityProbe(probe, options.signal)
this.supported = supported
if (!supported && this.capabilityProbe === probe) {
this.capabilityProbe = null
}
return supported
} catch {
return false
}
}

async get(
id: string,
opts: { scrollbackRows?: number } = {}
): Promise<PtyProviderBufferSnapshot | null> {
if (!this.supported) {
return null
}
const relayPtyId = this.toRelayPtyId(id)
const expectedIncarnation = this.readIncarnation(relayPtyId)
if (!expectedIncarnation) {
return null
}
const result = await this.mux.request('pty.getBufferSnapshot', {
id: relayPtyId,
scrollbackRows: opts.scrollbackRows ?? 0
})
return isSshPtyBufferSnapshot(result, expectedIncarnation) ? result : null
}

private async probeCapability(): Promise<boolean> {
try {
const result = (await this.mux.request('pty.getCapabilities', undefined, {
timeoutMs: 5_000
})) as { visibleScreenSnapshotVersion?: unknown }
return result.visibleScreenSnapshotVersion === PTY_VISIBLE_SCREEN_SNAPSHOT_PROTOCOL_VERSION
} catch {
return false
}
}
}

function isSshPtyBufferSnapshot(
value: unknown,
expectedIncarnation: string
): value is PtyProviderBufferSnapshot {
if (!value || typeof value !== 'object') {
return false
}
const snapshot = value as Partial<PtyProviderBufferSnapshot>
return (
snapshot.incarnationId === expectedIncarnation &&
typeof snapshot.data === 'string' &&
Number.isInteger(snapshot.cols) &&
Number(snapshot.cols) > 0 &&
Number.isInteger(snapshot.rows) &&
Number(snapshot.rows) > 0 &&
Number.isSafeInteger(snapshot.seq) &&
Number(snapshot.seq) >= 0 &&
snapshot.source === 'headless'
)
}
Loading
Loading