diff --git a/src/main/ipc/pty/provider/bind-listeners.ts b/src/main/ipc/pty/provider/bind-listeners.ts index fadc255b33aa..b8813dba24e2 100644 --- a/src/main/ipc/pty/provider/bind-listeners.ts +++ b/src/main/ipc/pty/provider/bind-listeners.ts @@ -30,6 +30,7 @@ export function bindProviderListeners(session: PtyIpcSession): void { localProvider.onWriteUnavailable?.((payload) => { // Before the window check: a headless host has no window, only remote streams. publishPtyEndpointLoss(payload.id) + session.runtime?.notePtyEndpointLoss(payload.id) if ( !session.mainWindow || session.mainWindow.isDestroyed() || diff --git a/src/main/ipc/pty/pty-write-ipc-validation.test.ts b/src/main/ipc/pty/pty-write-ipc-validation.test.ts index 31ad95dd98ab..32a27562a6b7 100644 --- a/src/main/ipc/pty/pty-write-ipc-validation.test.ts +++ b/src/main/ipc/pty/pty-write-ipc-validation.test.ts @@ -113,6 +113,23 @@ describe('registerPtyHandlers', () => { id: result.id }) }) + it('tells the runtime about a daemon endpoint loss even with no window', () => { + let fanout: ((payload: { id: string }) => void) | undefined + installDaemonTestProvider({ + onWriteUnavailable: vi.fn((callback: (payload: { id: string }) => void) => { + fanout = callback + return () => {} + }) + }) + const runtime = new OrcaRuntimeService() + const notePtyEndpointLoss = vi.spyOn(runtime, 'notePtyEndpointLoss') + registerPtyHandlers(mainWindow as never, runtime) + mainWindow.webContents.isDestroyed.mockReturnValue(true) + + fanout?.({ id: 'pane-1' }) + + expect(notePtyEndpointLoss).toHaveBeenCalledWith('pane-1') + }) it('rejects malformed and cross-window pty write IPC before provider writes', async () => { const mockProc = createMockProc() spawnMock.mockReturnValue(mockProc.proc) diff --git a/src/main/runtime/terminal-subscriber-driven-daemon-attach.test.ts b/src/main/runtime/terminal-subscriber-driven-daemon-attach.test.ts index b3e1a50123fe..77e13a45b904 100644 --- a/src/main/runtime/terminal-subscriber-driven-daemon-attach.test.ts +++ b/src/main/runtime/terminal-subscriber-driven-daemon-attach.test.ts @@ -29,6 +29,8 @@ type RuntimeInternals = { worktrees: [], targetWorktreeId: string | null ) => Promise + // Optional so this suite still runs, and fails on behavior, against a runtime without it. + notePtyEndpointLoss?: (ptyId: string) => void } function internals(runtime: OrcaRuntimeService): RuntimeInternals { @@ -48,10 +50,18 @@ function createDaemonProviderModel(opts: { snapshotCapable: boolean }) { const sessions = new Map() const attachCalls: string[] = [] const resizeCalls: [string, number, number][] = [] + const writes: string[] = [] let nextAttachBarrier: { promise: Promise; result: boolean } | null = null let runtime: OrcaRuntimeService | null = null const controller = { - write: () => true, + // The adapter refuses input to a session until this app attaches it again. + write: (ptyId: string, data: string) => { + if (!sessions.get(ptyId)?.attached) { + return false + } + writes.push(data) + return true + }, kill: () => true, getForegroundProcess: async () => null, listProcesses: async () => @@ -110,6 +120,13 @@ function createDaemonProviderModel(opts: { snapshotCapable: boolean }) { sessions, attachCalls, resizeCalls, + writes, + /** Main's daemon link dropped and came back: the daemon released this app's attachments. */ + dropAttachments(): void { + for (const session of sessions.values()) { + session.attached = false + } + }, deferNextAttach(result: boolean): () => void { let release!: () => void const promise = new Promise((resolve) => { @@ -483,3 +500,145 @@ describe('subscriber-driven daemon attach (never-activated tab)', () => { expect(read.tail.join('\n')).not.toContain('stale provider frame') }) }) + +/** A session this app spawned and attached; then main's daemon link drops and returns. The daemon + * releases every attachment of the reconnecting client, and only a pane mounted in this window + * re-attaches on its own. Main must take back every other session's stream so background agents' + * records stay fresh and a paired client, phone or CLI send is not refused. */ +describe('re-attach after a daemon reconnect detached a spawned session', () => { + function setupSpawnedAttachedSession(): ReturnType { + const setup = setupNeverAttachedDaemonSession({ + snapshotCapable: true, + screen: 'agent screen\r\n' + }) + setup.runtime.onPtySpawned(PTY_ID, undefined, { awaitsRegistration: false }) + setup.model.sessions.get(PTY_ID)!.attached = true + return setup + } + + function loseEndpoint( + runtime: OrcaRuntimeService, + model: ReturnType + ): void { + model.dropAttachments() + // What the local provider's write-unavailable fan-out reports for every session it held. + internals(runtime).notePtyEndpointLoss?.(PTY_ID) + } + + it('takes back the stream of a session no view shows', async () => { + const { runtime, model, handle } = setupSpawnedAttachedSession() + + loseEndpoint(runtime, model) + + await vi.waitFor(() => expect(model.attachCalls).toEqual([PTY_ID])) + expect(model.emitData(PTY_ID, 'background agent output\r\n')).toBe(true) + await expect( + runtime.sendTerminal(handle, { text: 'typed' }, { inputKind: 'driving' }) + ).resolves.toMatchObject({ accepted: true }) + expect(model.writes).toEqual(['typed']) + }) + + it('holds input sent while the re-attach is in flight instead of refusing it', async () => { + const { runtime, model, handle } = setupSpawnedAttachedSession() + const releaseAttach = model.deferNextAttach(true) + + loseEndpoint(runtime, model) + await vi.waitFor(() => expect(model.attachCalls).toEqual([PTY_ID])) + const send = runtime.sendTerminal(handle, { text: 'early key' }, { inputKind: 'driving' }) + await Promise.resolve() + expect(model.writes).toEqual([]) + releaseAttach() + + await expect(send).resolves.toMatchObject({ accepted: true }) + expect(model.writes).toEqual(['early key']) + }) + + it('drops input cancelled while the re-attach is in flight', async () => { + const { runtime, model, handle } = setupSpawnedAttachedSession() + const releaseAttach = model.deferNextAttach(true) + loseEndpoint(runtime, model) + await vi.waitFor(() => expect(model.attachCalls).toEqual([PTY_ID])) + const cancel = new AbortController() + const send = runtime.sendTerminal( + handle, + { text: 'cancelled key' }, + { inputKind: 'driving', signal: cancel.signal } + ) + await Promise.resolve() + + cancel.abort() + releaseAttach() + + await expect(send).rejects.toThrow('request_aborted') + expect(model.writes).toEqual([]) + }) + + it('does not hand held input to a terminal that replaced the one it was sent to', async () => { + const { runtime, model, handle } = setupSpawnedAttachedSession() + const releaseAttach = model.deferNextAttach(true) + loseEndpoint(runtime, model) + await vi.waitFor(() => expect(model.attachCalls).toEqual([PTY_ID])) + const send = runtime.sendTerminal( + handle, + { text: 'for the old shell' }, + { inputKind: 'driving' } + ) + await Promise.resolve() + + await runtime.onPtyExit(PTY_ID, 0) + runtime.onPtySpawned(PTY_ID, undefined, { awaitsRegistration: false }) + model.sessions.get(PTY_ID)!.attached = true + releaseAttach() + + await expect(send).rejects.toThrow() + expect(model.writes).toEqual([]) + }) + + it('leaves a failed re-attach to the next view or inventory pass, not to each keystroke', async () => { + const { runtime, model, handle } = setupSpawnedAttachedSession() + const releaseAttach = model.deferNextAttach(false) + loseEndpoint(runtime, model) + releaseAttach() + await vi.waitFor(() => expect(model.attachCalls).toEqual([PTY_ID])) + + await runtime.sendTerminal(handle, { text: 'a' }, { inputKind: 'driving' }).catch(() => {}) + await runtime.sendTerminal(handle, { text: 'b' }, { inputKind: 'driving' }).catch(() => {}) + expect(model.attachCalls).toEqual([PTY_ID]) + + const harness = startMultiplex(runtime) + await vi.waitFor(() => expect(harness.handlers.has(0)).toBe(true)) + sendSubscribe(harness, 1, handle) + await waitForSubscribed(harness, 1) + await vi.waitFor(() => expect(model.attachCalls).toEqual([PTY_ID, PTY_ID])) + expect(model.emitData(PTY_ID, 'live again\r\n')).toBe(true) + }) + + it('redoes an earlier subscriber-driven attach that the loss undid', async () => { + // Never-attached session: the first remote subscribe attaches it (the restored-tab path). + const { runtime, model, handle } = setupNeverAttachedDaemonSession({ snapshotCapable: false }) + const harness = startMultiplex(runtime) + await vi.waitFor(() => expect(harness.handlers.has(0)).toBe(true)) + sendSubscribe(harness, 1, handle) + await waitForSubscribed(harness, 1) + await vi.waitFor(() => expect(model.attachCalls).toEqual([PTY_ID])) + + loseEndpoint(runtime, model) + + await vi.waitFor(() => expect(model.attachCalls).toEqual([PTY_ID, PTY_ID])) + expect(model.emitData(PTY_ID, 'second life\r\n')).toBe(true) + await vi.waitFor(() => expect(outputText(harness)).toContain('second life')) + }) + + it('attaches again when another loss lands while the re-attach is in flight', async () => { + const { runtime, model } = setupSpawnedAttachedSession() + const releaseFirst = model.deferNextAttach(true) + + loseEndpoint(runtime, model) + await vi.waitFor(() => expect(model.attachCalls).toEqual([PTY_ID])) + loseEndpoint(runtime, model) + releaseFirst() + + await vi.waitFor(() => expect(model.attachCalls).toEqual([PTY_ID, PTY_ID])) + expect(model.emitData(PTY_ID, 'after both\r\n')).toBe(true) + }) +}) diff --git a/src/main/runtime/terminal/input/terminal-input-dispatch.ts b/src/main/runtime/terminal/input/terminal-input-dispatch.ts index 8aede49dc600..bd0ceb01f99d 100644 --- a/src/main/runtime/terminal/input/terminal-input-dispatch.ts +++ b/src/main/runtime/terminal/input/terminal-input-dispatch.ts @@ -125,6 +125,13 @@ export abstract class OrcaRuntimeWithControllerKnowsPtyIsLive extends OrcaRuntim return probe } + /** The PTY a handle names right now, read without resolving or refusing the handle. */ + private handlePtyId(handle: string): string | undefined { + return ( + this.getLivePtyForHandle(handle)?.pty.ptyId ?? this.handles.get(handle)?.ptyId ?? undefined + ) + } + /** sendTerminal once plugin hooks decided (terminal-plugin-hooked-input.ts). */ protected async sendTerminalAfterPluginHooks( handle: string, @@ -139,6 +146,8 @@ export abstract class OrcaRuntimeWithControllerKnowsPtyIsLive extends OrcaRuntim const target = await starting return { ...(await this.sendTerminalAfterPluginHooks(target, action, options)), handle } } + // Before resolving the target: a terminal closed or replaced during the wait is then refused, not written. + await this.waitForPtyEndpointReattach(this.handlePtyId(handle), options.signal) const pty = this.getLivePtyForHandle(handle) if (pty) { if (!pty.pty.connected) { @@ -244,6 +253,10 @@ export abstract class OrcaRuntimeWithControllerKnowsPtyIsLive extends OrcaRuntim const target = await starting return { ...(await this.sendAgentPromptAfterPluginHooks(target, prompt, options)), handle } } + await this.waitForPtyEndpointReattach( + this.handlePtyId(handle), + agentPromptObservationSignal(options) + ) const pty = this.getLivePtyForHandle(handle) if (pty) { if (!pty.pty.connected) { diff --git a/src/main/runtime/terminal/layout/terminal-view-presence.ts b/src/main/runtime/terminal/layout/terminal-view-presence.ts index 290d7a169b8c..0c818d0c2985 100644 --- a/src/main/runtime/terminal/layout/terminal-view-presence.ts +++ b/src/main/runtime/terminal/layout/terminal-view-presence.ts @@ -1,3 +1,4 @@ +import { setTimeout as delay } from 'node:timers/promises' import { OrcaRuntimeWithRecordAgentPromptLifecycleState } from '../output/terminal-output-generation' import type { TerminalOscLinkRange } from '../../../../shared/terminal/links/terminal-osc-link-ranges' import type { PtyProviderBufferSnapshot } from '../../../providers/pty-provider-contract' @@ -11,6 +12,9 @@ import type { RuntimeTerminalBufferSnapshot } from '../records/runtime-terminal- import { AUTHORITATIVE_TERMINAL_SNAPSHOT_TIMEOUT_MS } from '../output/provider-snapshot-budget' import { assertTerminalInputWithinLimitWithYield } from '../../terminal-send-payload' +// Covers a daemon reconnect plus the attach; past it the write proceeds and is refused as before. +const ENDPOINT_REATTACH_INPUT_WAIT_MS = 10_000 + export abstract class OrcaRuntimeWithNotifyRemoteTerminalViewPresenceChanged extends OrcaRuntimeWithRecordAgentPromptLifecycleState { protected abstract serializeTerminalBufferFromAvailableState( ptyId: string, @@ -69,6 +73,60 @@ export abstract class OrcaRuntimeWithNotifyRemoteTerminalViewPresenceChanged ext return this.mainDaemonAttachments.isKnownUnattachedLocal(ptyId) } + /** The local provider lost this PTY's endpoint; main re-attaches it so its output and input resume. */ + notePtyEndpointLoss(ptyId: string): void { + this.mainDaemonAttachments.noteEndpointLoss(ptyId) + } + + /** Input waits, bounded, for main's in-flight re-attach after an endpoint loss instead of being refused. */ + protected async waitForPtyEndpointReattach( + ptyId: string | undefined, + signal?: AbortSignal + ): Promise { + const reattach = + ptyId === undefined ? undefined : this.mainDaemonAttachments.pendingReattach(ptyId) + if (ptyId === undefined || !reattach) { + return + } + const generation = this.getPtyLifecycleGeneration(ptyId) + await this.settleEndpointReattach(ptyId, reattach, signal) + if (signal?.aborted) { + // A cancelled send must not be written once the terminal is back. + throw new Error('request_aborted') + } + // Input held for one process must not reach a process that replaced it under the same id. + if (this.getPtyLifecycleGeneration(ptyId) !== generation) { + throw new Error('terminal_handle_stale') + } + } + + private async settleEndpointReattach( + ptyId: string, + first: Promise, + signal?: AbortSignal + ): Promise { + const deadline = performance.now() + ENDPOINT_REATTACH_INPUT_WAIT_MS + // A further loss mid-attach starts another attempt; keep waiting within the same bound. + let reattach: Promise | undefined = first + while (reattach) { + const remainingMs = deadline - performance.now() + if (remainingMs <= 0) { + return + } + const stopWaiting = new AbortController() + const waitSignal = signal ? AbortSignal.any([signal, stopWaiting.signal]) : stopWaiting.signal + const settled = await Promise.race([ + reattach.then(() => true), + delay(remainingMs, false, { signal: waitSignal }).catch(() => false) + ]) + stopWaiting.abort() + if (!settled) { + return + } + reattach = this.mainDaemonAttachments.pendingReattach(ptyId) + } + } + /** Mark a raw-output viewer without transferring terminal query authority. */ registerRawTerminalViewSubscriber(ptyId: string): () => void { return this.terminalViewSubscribers.registerRaw(ptyId) diff --git a/src/main/runtime/terminal/lifecycle/main-daemon-attachments.ts b/src/main/runtime/terminal/lifecycle/main-daemon-attachments.ts index a6cedb8379cb..2fe432a4aa94 100644 --- a/src/main/runtime/terminal/lifecycle/main-daemon-attachments.ts +++ b/src/main/runtime/terminal/lifecycle/main-daemon-attachments.ts @@ -1,5 +1,8 @@ type MainDaemonAttachmentDependencies = { - isUnattachedLocalCandidate: (ptyId: string) => boolean + /** A live local daemon session main may attach now (no spawn admission in flight). */ + isAttachableLocalSession: (ptyId: string) => boolean + /** Main already holds this session's screen, so an attach began before this run's tracking. */ + hasMainTerminalState: (ptyId: string) => boolean attachProvider: (ptyId: string) => Promise | null } @@ -13,21 +16,41 @@ export class MainDaemonAttachments { private readonly attaching = new Map>() private readonly queued: (() => void)[] = [] private running = 0 + // Endpoint losses per session while main owes it a re-attach; a count so a loss mid-attach is noticed. + private readonly endpointLosses = new Map() constructor(private readonly deps: MainDaemonAttachmentDependencies) {} markSpawnPublished(ptyId: string): void { this.attached.add(ptyId) + this.endpointLosses.delete(ptyId) } /** Exit or respawn ends the generation whose stream this described. */ forget(ptyId: string): void { this.attached.delete(ptyId) this.attaching.delete(ptyId) + this.endpointLosses.delete(ptyId) } isKnownUnattachedLocal(ptyId: string): boolean { - return !this.attached.has(ptyId) && this.deps.isUnattachedLocalCandidate(ptyId) + return ( + !this.attached.has(ptyId) && + this.deps.isAttachableLocalSession(ptyId) && + !this.deps.hasMainTerminalState(ptyId) + ) + } + + /** The daemon dropped this run's attachment, so main re-attaches like at adoption; a mounted pane alone would leave background sessions silent. */ + noteEndpointLoss(ptyId: string): void { + this.endpointLosses.set(ptyId, (this.endpointLosses.get(ptyId) ?? 0) + 1) + this.attached.delete(ptyId) + this.ensure(ptyId) + } + + /** Main's re-attach after an endpoint loss, while in flight; input waits on it instead of being refused. */ + pendingReattach(ptyId: string): Promise | undefined { + return this.endpointLosses.has(ptyId) ? this.attaching.get(ptyId) : undefined } /** Inventory proved the session live; attach it, or retry once an in-flight refusal settles. */ @@ -45,9 +68,10 @@ export class MainDaemonAttachments { } ensure(ptyId: string): void { - if (this.attaching.has(ptyId) || !this.isKnownUnattachedLocal(ptyId)) { + if (this.attaching.has(ptyId) || !this.isAttachOwed(ptyId)) { return } + const lossCount = this.endpointLosses.get(ptyId) const attempt: Promise = this.acquireSlot().then(() => this.attach(ptyId, attempt)) this.attaching.set(ptyId, attempt) void attempt.then((attached) => { @@ -55,12 +79,25 @@ export class MainDaemonAttachments { return } this.attaching.delete(ptyId) + if (this.endpointLosses.get(ptyId) !== lossCount) { + // A loss during the attempt can undo the attach it made. + this.ensure(ptyId) + return + } if (attached) { this.attached.add(ptyId) + this.endpointLosses.delete(ptyId) } }) } + // A lost session needs its stream back even though main still holds its screen. + private isAttachOwed(ptyId: string): boolean { + return this.endpointLosses.has(ptyId) + ? !this.attached.has(ptyId) && this.deps.isAttachableLocalSession(ptyId) + : this.isKnownUnattachedLocal(ptyId) + } + private acquireSlot(): Promise { if (this.running < MAX_CONCURRENT_ATTACHES) { this.running += 1 @@ -73,7 +110,7 @@ export class MainDaemonAttachments { private async attach(ptyId: string, attempt: Promise): Promise { try { // While this queued, an exit or respawn may have superseded it, or a pane may have attached. - if (this.attaching.get(ptyId) !== attempt || !this.isKnownUnattachedLocal(ptyId)) { + if (this.attaching.get(ptyId) !== attempt || !this.isAttachOwed(ptyId)) { return false } return (await this.deps.attachProvider(ptyId)) ?? false diff --git a/src/main/runtime/terminal/lifecycle/terminal-view-resources.ts b/src/main/runtime/terminal/lifecycle/terminal-view-resources.ts index e08111d044fa..f39353518e74 100644 --- a/src/main/runtime/terminal/lifecycle/terminal-view-resources.ts +++ b/src/main/runtime/terminal/lifecycle/terminal-view-resources.ts @@ -195,18 +195,15 @@ export abstract class OrcaRuntimeWithFitOverrideListeners extends OrcaRuntimeWit >() protected readonly mainDaemonAttachments = new MainDaemonAttachments({ - isUnattachedLocalCandidate: (ptyId) => { - if ( - this.headlessTerminals.has(ptyId) || - this.providerSnapshotPreferredPtys.has(ptyId) || - this.pendingPtyRegistrationIncarnations.has(ptyId) || - parseAppSshPtyId(ptyId) - ) { + isAttachableLocalSession: (ptyId) => { + if (this.pendingPtyRegistrationIncarnations.has(ptyId) || parseAppSshPtyId(ptyId)) { return false } const pty = this.ptysById.get(ptyId) return pty !== undefined && pty.connectionId === null && pty.connected }, + hasMainTerminalState: (ptyId) => + this.headlessTerminals.has(ptyId) || this.providerSnapshotPreferredPtys.has(ptyId), attachProvider: (ptyId) => this.ptyController?.attach?.(ptyId) ?? null })