diff --git a/src/main/daemon/daemon-protocol-version.test.ts b/src/main/daemon/daemon-protocol-version.test.ts index 1b7498e99..025d7838e 100644 --- a/src/main/daemon/daemon-protocol-version.test.ts +++ b/src/main/daemon/daemon-protocol-version.test.ts @@ -3,6 +3,7 @@ import { AGENT_SESSION_CLAIM_DAEMON_PROTOCOL_VERSION, AGENT_SESSION_CREATE_OPERATION_DAEMON_PROTOCOL_VERSION, COMPLETION_PROCESS_INSPECTION_PROTOCOL_VERSION, + GET_FOREGROUND_PROCESS_PROTOCOL_VERSION, PREVIOUS_DAEMON_PROTOCOL_VERSIONS, PROTOCOL_VERSION } from './daemon-protocol-version' @@ -11,6 +12,7 @@ describe('daemon protocol version', () => { it('ships preflight-cache replacement after completion inspection', () => { expect(PROTOCOL_VERSION).toBe(28) expect(COMPLETION_PROCESS_INSPECTION_PROTOCOL_VERSION).toBe(27) + expect(GET_FOREGROUND_PROCESS_PROTOCOL_VERSION).toBe(11) expect(AGENT_SESSION_CLAIM_DAEMON_PROTOCOL_VERSION).toBe(26) expect(AGENT_SESSION_CREATE_OPERATION_DAEMON_PROTOCOL_VERSION).toBe(26) expect(PREVIOUS_DAEMON_PROTOCOL_VERSIONS).toEqual( diff --git a/src/main/daemon/daemon-protocol-version.ts b/src/main/daemon/daemon-protocol-version.ts index c5b4f2c1b..f2a890aac 100644 --- a/src/main/daemon/daemon-protocol-version.ts +++ b/src/main/daemon/daemon-protocol-version.ts @@ -2,6 +2,7 @@ // v28 replaces v26/v27 daemons that can retain permanent macOS preflight rejections (#9756). export const PROTOCOL_VERSION = 28 export const COMPLETION_PROCESS_INSPECTION_PROTOCOL_VERSION = 27 +export const GET_FOREGROUND_PROCESS_PROTOCOL_VERSION = 11 export const PTY_STARTUP_INGRESS_PROTOCOL_VERSION = 25 export const AGENT_SESSION_CLAIM_DAEMON_PROTOCOL_VERSION = 26 export const AGENT_SESSION_CREATE_OPERATION_DAEMON_PROTOCOL_VERSION = 26 diff --git a/src/main/daemon/daemon-pty-adapter.test.ts b/src/main/daemon/daemon-pty-adapter.test.ts index 2ad8ef28f..980f29f17 100644 --- a/src/main/daemon/daemon-pty-adapter.test.ts +++ b/src/main/daemon/daemon-pty-adapter.test.ts @@ -6,7 +6,11 @@ import { mkdtempSync, mkdirSync, rmSync, existsSync, readFileSync, writeFileSync import { DaemonClient } from './client' import { DaemonProtocolError } from './daemon-errors' import { DaemonPtyAdapter } from './daemon-pty-adapter' -import { COMPLETION_PROCESS_INSPECTION_PROTOCOL_VERSION } from './daemon-protocol-version' +import { + COMPLETION_PROCESS_INSPECTION_PROTOCOL_VERSION, + GET_FOREGROUND_PROCESS_PROTOCOL_VERSION, + PROTOCOL_VERSION +} from './daemon-protocol-version' import { DaemonServer } from './daemon-server' import { HeadlessEmulator } from './headless-emulator' import { getHistorySessionDirName } from './history-paths' @@ -929,31 +933,53 @@ describe('DaemonPtyAdapter (IPtyProvider)', () => { }) describe('inspectProcess on pre-inspection daemon protocols', () => { - // Why: daemons outlive an in-place app update, so a v26 daemon must still answer inspections - // client-side; throwing here leaves agent-completion detection permanently retrying. type ClientInternals = { client: { request: ReturnType; disconnect: ReturnType } } - function createLegacyAdapter(request: ReturnType): DaemonPtyAdapter { - const legacy = new DaemonPtyAdapter({ + function createInspectionAdapter( + protocolVersion: number, + request: ReturnType + ): DaemonPtyAdapter { + const inspectionAdapter = new DaemonPtyAdapter({ socketPath, tokenPath, - protocolVersion: COMPLETION_PROCESS_INSPECTION_PROTOCOL_VERSION - 1 + protocolVersion }) - ;(legacy as unknown as ClientInternals).client = { request, disconnect: vi.fn() } - return legacy + ;(inspectionAdapter as unknown as ClientInternals).client = { + request, + disconnect: vi.fn() + } + return inspectionAdapter } - it('composes a real inspection from the foreground call the daemon does support', async () => { + it('reports protocol 10 inspection as unavailable without unsupported RPCs', async () => { + const request = vi.fn() + const legacy = createInspectionAdapter(GET_FOREGROUND_PROCESS_PROTOCOL_VERSION - 1, request) + + await expect(legacy.inspectProcess('sess-a')).resolves.toEqual({ + foregroundProcess: null, + hasChildProcesses: true, + unavailable: true + }) + await expect(legacy.getForegroundProcess('sess-a')).resolves.toBeNull() + await expect(legacy.hasChildProcesses('sess-a')).resolves.toBe(true) + expect(request).not.toHaveBeenCalled() + + legacy.dispose() + }) + + it.each([ + GET_FOREGROUND_PROCESS_PROTOCOL_VERSION, + COMPLETION_PROCESS_INSPECTION_PROTOCOL_VERSION - 1 + ])('composes protocol %s inspection from getForegroundProcess', async (protocolVersion) => { const request = vi.fn(async () => ({ foregroundProcess: 'codex' })) - const legacy = createLegacyAdapter(request) + const legacy = createInspectionAdapter(protocolVersion, request) expect(await legacy.inspectProcess('sess-a')).toEqual({ foregroundProcess: 'codex', hasChildProcesses: true }) - // Why: the fallback must not depend on a capability the legacy daemon lacks. expect(request).toHaveBeenCalledWith('getForegroundProcess', { sessionId: 'sess-a' }) expect(request).not.toHaveBeenCalledWith('inspectProcess', expect.anything()) @@ -961,7 +987,10 @@ describe('DaemonPtyAdapter (IPtyProvider)', () => { }) it('reports an idle shell as having no child processes', async () => { - const legacy = createLegacyAdapter(vi.fn(async () => ({ foregroundProcess: 'bash' }))) + const legacy = createInspectionAdapter( + COMPLETION_PROCESS_INSPECTION_PROTOCOL_VERSION - 1, + vi.fn(async () => ({ foregroundProcess: 'bash' })) + ) expect(await legacy.inspectProcess('sess-a')).toEqual({ foregroundProcess: 'bash', @@ -972,11 +1001,10 @@ describe('DaemonPtyAdapter (IPtyProvider)', () => { }) it('reports a null foreground as idle, matching what the legacy daemon can report', async () => { - // Why: pins the one response shape whose semantics differ from v27, where inspectProcess goes - // through getAliveSession() and throws for a vanished session while getForegroundProcess stays - // null-not-throw. Reading it as idle is deliberate — this is also the only shape that reaches a - // user-visible completion — so the divergence must not change silently. - const legacy = createLegacyAdapter(vi.fn(async () => ({ foregroundProcess: null }))) + const legacy = createInspectionAdapter( + COMPLETION_PROCESS_INSPECTION_PROTOCOL_VERSION - 1, + vi.fn(async () => ({ foregroundProcess: null })) + ) expect(await legacy.inspectProcess('sess-a')).toEqual({ foregroundProcess: null, @@ -989,7 +1017,8 @@ describe('DaemonPtyAdapter (IPtyProvider)', () => { it('rejects rather than reading as idle when the daemon call fails', async () => { // Why: getForegroundProcess swallows errors into null; composing through it would turn a dead // socket into a false "agent exited" completion, the mirror of the bug this path fixes. - const legacy = createLegacyAdapter( + const legacy = createInspectionAdapter( + COMPLETION_PROCESS_INSPECTION_PROTOCOL_VERSION - 1, vi.fn(async () => { throw new Error('socket_closed') }) @@ -1000,21 +1029,19 @@ describe('DaemonPtyAdapter (IPtyProvider)', () => { legacy.dispose() }) - it('still delegates to the daemon once the protocol supports inspectProcess', async () => { - const request = vi.fn(async () => ({ foregroundProcess: 'codex', hasChildProcesses: true })) - const current = new DaemonPtyAdapter({ - socketPath, - tokenPath, - protocolVersion: COMPLETION_PROCESS_INSPECTION_PROTOCOL_VERSION - }) - ;(current as unknown as ClientInternals).client = { request, disconnect: vi.fn() } + it.each([COMPLETION_PROCESS_INSPECTION_PROTOCOL_VERSION, PROTOCOL_VERSION])( + 'delegates protocol %s inspection to inspectProcess', + async (protocolVersion) => { + const request = vi.fn(async () => ({ foregroundProcess: 'codex', hasChildProcesses: true })) + const current = createInspectionAdapter(protocolVersion, request) - await current.inspectProcess('sess-a') + await current.inspectProcess('sess-a') - expect(request).toHaveBeenCalledWith('inspectProcess', { sessionId: 'sess-a' }) + expect(request).toHaveBeenCalledWith('inspectProcess', { sessionId: 'sess-a' }) - current.dispose() - }) + current.dispose() + } + ) }) describe('serialize / revive', () => { diff --git a/src/main/daemon/daemon-pty-adapter.ts b/src/main/daemon/daemon-pty-adapter.ts index 8d0cdf796..e977ed6fb 100644 --- a/src/main/daemon/daemon-pty-adapter.ts +++ b/src/main/daemon/daemon-pty-adapter.ts @@ -11,6 +11,7 @@ import { CODEX_SHELL_READY_TIMEOUT_MS } from './session' import { CLEAN_DISCONNECT_PROTOCOL_VERSION, COMPLETION_PROCESS_INSPECTION_PROTOCOL_VERSION, + GET_FOREGROUND_PROCESS_PROTOCOL_VERSION, AGENT_SESSION_CLAIM_DAEMON_PROTOCOL_VERSION, AGENT_SESSION_CREATE_OPERATION_DAEMON_PROTOCOL_VERSION, GIT_CREDENTIAL_GUARD_HOST_PROTOCOL_VERSION, @@ -38,6 +39,7 @@ import type { PtySpawnOptions, PtySpawnResult } from '../providers/types' +import type { PtyProcessInspection } from '../providers/pty-process-inspection' import { isShellProcess } from '../../shared/agent-detection' import { resolveWslSessionContext } from './wsl-session-context' import { normalizeWslColdRestoreCwd } from './wsl-cold-restore-cwd' @@ -824,12 +826,16 @@ export class DaemonPtyAdapter implements IPtyProvider { } async hasChildProcesses(id: string): Promise { + if (this.protocolVersion < GET_FOREGROUND_PROCESS_PROTOCOL_VERSION) { + return true + } return this.hasChildProcessesFromForeground(await this.getForegroundProcess(id)) } - async inspectProcess( - id: string - ): Promise<{ foregroundProcess: string | null; hasChildProcesses: boolean }> { + async inspectProcess(id: string): Promise { + if (this.protocolVersion < GET_FOREGROUND_PROCESS_PROTOCOL_VERSION) { + return { foregroundProcess: null, hasChildProcesses: true, unavailable: true } + } if (this.protocolVersion < COMPLETION_PROCESS_INSPECTION_PROTOCOL_VERSION) { // Why: pre-v27 daemons survive an in-place app update; compose the inspection client-side from the // one call they do support instead of throwing, or completion detection stays dead until recreate. @@ -850,6 +856,9 @@ export class DaemonPtyAdapter implements IPtyProvider { } async getForegroundProcess(id: string): Promise { + if (this.protocolVersion < GET_FOREGROUND_PROCESS_PROTOCOL_VERSION) { + return null + } try { const result = await this.client.request<{ foregroundProcess: string | null }>( 'getForegroundProcess', diff --git a/src/main/daemon/daemon-pty-router.test.ts b/src/main/daemon/daemon-pty-router.test.ts index 0c66cf38d..0861df788 100644 --- a/src/main/daemon/daemon-pty-router.test.ts +++ b/src/main/daemon/daemon-pty-router.test.ts @@ -150,6 +150,26 @@ it('rejects completion inspection when no daemon owns the session', async () => await expect(router.inspectProcess('unmapped-session')).rejects.toThrow('terminal_gone') }) +it('preserves unavailable inspection from the owning legacy daemon', async () => { + const legacy = createAdapter('legacy', ['legacy-session']) + vi.mocked(legacy.inspectProcess).mockResolvedValue({ + foregroundProcess: null, + hasChildProcesses: true, + unavailable: true + }) + const router = new DaemonPtyRouter({ + current: createAdapter('current'), + legacy: [legacy] + }) + await router.discoverLegacySessions() + + await expect(router.inspectProcess('legacy-session')).resolves.toEqual({ + foregroundProcess: null, + hasChildProcesses: true, + unavailable: true + }) +}) + describe('DaemonPtyRouter', () => { it('reports separate conservative resume and fresh-create boundaries', () => { const current = createAdapter( diff --git a/src/main/daemon/daemon-pty-router.ts b/src/main/daemon/daemon-pty-router.ts index e30021a6f..8f2a8b410 100644 --- a/src/main/daemon/daemon-pty-router.ts +++ b/src/main/daemon/daemon-pty-router.ts @@ -8,6 +8,7 @@ import type { PtySpawnResult } from '../providers/types' import type { PtyIncarnationId } from '../../shared/pty-incarnation' +import type { PtyProcessInspection } from '../providers/pty-process-inspection' export class DaemonPtyRouter implements IPtyProvider { private current: DaemonPtyAdapter @@ -189,9 +190,7 @@ export class DaemonPtyRouter implements IPtyProvider { return this.adapterFor(id).getForegroundProcess(id) } - async inspectProcess( - id: string - ): Promise<{ foregroundProcess: string | null; hasChildProcesses: boolean }> { + async inspectProcess(id: string): Promise { return this.adapterForInspection(id).inspectProcess(id) } diff --git a/src/main/daemon/degraded-daemon-pty-provider.test.ts b/src/main/daemon/degraded-daemon-pty-provider.test.ts index a8211b361..cccd6e0cb 100644 --- a/src/main/daemon/degraded-daemon-pty-provider.test.ts +++ b/src/main/daemon/degraded-daemon-pty-provider.test.ts @@ -2,8 +2,10 @@ import { describe, expect, it, vi } from 'vitest' import { DegradedDaemonPtyProvider } from './degraded-daemon-pty-provider' import type { DaemonPtyAdapter } from './daemon-pty-adapter' import type { IPtyProvider, PtySpawnOptions, PtySpawnResult } from '../providers/types' +import type { PtyProcessInspection } from '../providers/pty-process-inspection' type ProviderMock = IPtyProvider & { + inspectProcess: (id: string) => Promise emitData: (id: string, data: string, sequenceChars?: number) => void emitReplay: (id: string, data: string) => void emitExit: (id: string, code: number) => void @@ -42,6 +44,7 @@ function createProvider( acknowledgeDataEvent: vi.fn(), hasChildProcesses: vi.fn(async () => false), getForegroundProcess: vi.fn(async () => null), + inspectProcess: vi.fn(async () => ({ foregroundProcess: null, hasChildProcesses: false })), confirmForegroundProcess: vi.fn(async () => `${label}-confirmed`), serialize: vi.fn(async () => '{}'), revive: vi.fn(async () => {}), @@ -123,6 +126,27 @@ it('rejects completion inspection instead of borrowing the fallback provider', a await expect(provider.inspectProcess('unmapped-session')).rejects.toThrow('terminal_gone') }) +it('preserves unavailable inspection from an owning daemon', async () => { + const daemon = createDaemonAdapter('daemon', ['daemon-session']) + vi.mocked(daemon.inspectProcess).mockResolvedValue({ + foregroundProcess: null, + hasChildProcesses: true, + unavailable: true + }) + const provider = new DegradedDaemonPtyProvider({ + current: daemon, + legacy: [], + fallback: createProvider('fallback') + }) + await provider.discoverDaemonSessions() + + await expect(provider.inspectProcess('daemon-session')).resolves.toEqual({ + foregroundProcess: null, + hasChildProcesses: true, + unavailable: true + }) +}) + describe('DegradedDaemonPtyProvider', () => { it('only delegates owner-listing authority to the provider that owns the id', async () => { const current = createDaemonAdapter('daemon', ['daemon-session']) diff --git a/src/main/daemon/types.ts b/src/main/daemon/types.ts index 2ee1ebd95..aaa65028c 100644 --- a/src/main/daemon/types.ts +++ b/src/main/daemon/types.ts @@ -27,6 +27,7 @@ export { AGENT_SESSION_CREATE_OPERATION_DAEMON_PROTOCOL_VERSION, CLEAN_DISCONNECT_PROTOCOL_VERSION, COMPLETION_PROCESS_INSPECTION_PROTOCOL_VERSION, + GET_FOREGROUND_PROCESS_PROTOCOL_VERSION, GIT_CREDENTIAL_GUARD_HOST_PROTOCOL_VERSION, PREVIOUS_DAEMON_PROTOCOL_VERSIONS, PROTOCOL_VERSION, diff --git a/src/main/ipc/pty.test.ts b/src/main/ipc/pty.test.ts index 534d48d1c..79f2cf765 100644 --- a/src/main/ipc/pty.test.ts +++ b/src/main/ipc/pty.test.ts @@ -5905,6 +5905,24 @@ describe('registerPtyHandlers', () => { expect(provider.confirmForegroundProcess).not.toHaveBeenCalled() }) + it('preserves unavailable process inspection results from the provider', async () => { + const inspectProcess = vi.fn(async () => ({ + foregroundProcess: null, + hasChildProcesses: true, + unavailable: true as const + })) + registerPtyHandlers(mainWindow as never) + setLocalPtyProvider({ inspectProcess } as never) + + await expect( + handlers.get('pty:inspectProcess')!(null, { id: 'legacy-daemon-pty' }) + ).resolves.toEqual({ + foregroundProcess: null, + hasChildProcesses: true, + unavailable: true + }) + }) + // Why: daemon resize is fire-and-forget, so pty:getSize must report the APPLIED size, not the requested one (Claude-Code split-pane desync). describe('pty:getSize reports applied size, not requested size', () => { function setupProviderWithAppliedSize(args: { diff --git a/src/main/providers/pty-process-inspection.test.ts b/src/main/providers/pty-process-inspection.test.ts index 60ce53bc5..b18214b33 100644 --- a/src/main/providers/pty-process-inspection.test.ts +++ b/src/main/providers/pty-process-inspection.test.ts @@ -25,6 +25,18 @@ describe('PTY provider process inspection', () => { expect(inspectProcess).toHaveBeenCalledExactlyOnceWith('pty-1') }) + it('preserves an unavailable inspection result', async () => { + const inspection = { + foregroundProcess: null, + hasChildProcesses: true, + unavailable: true as const + } + const inspectProcess = vi.fn().mockResolvedValue(inspection) + const provider = { inspectProcess } as unknown as IPtyProvider + + await expect(inspectPtyProviderProcess(provider, 'pty-1')).resolves.toEqual(inspection) + }) + it('falls back to the existing provider process APIs', async () => { const getForegroundProcess = vi.fn().mockResolvedValue('codex') const hasChildProcesses = vi.fn().mockResolvedValue(true) diff --git a/src/main/providers/pty-process-inspection.ts b/src/main/providers/pty-process-inspection.ts index 68e174ff8..760095c2e 100644 --- a/src/main/providers/pty-process-inspection.ts +++ b/src/main/providers/pty-process-inspection.ts @@ -3,6 +3,7 @@ import type { IPtyProvider } from './types' export type PtyProcessInspection = { foregroundProcess: string | null hasChildProcesses: boolean + unavailable?: true } type CompletionSensitivePtyProvider = IPtyProvider & { diff --git a/src/main/providers/ssh-pty-provider.test.ts b/src/main/providers/ssh-pty-provider.test.ts index f204369c9..99bb651ed 100644 --- a/src/main/providers/ssh-pty-provider.test.ts +++ b/src/main/providers/ssh-pty-provider.test.ts @@ -835,6 +835,18 @@ describe('SshPtyProvider', () => { expect(mux.request).toHaveBeenCalledWith('pty.getForegroundProcess', { id: 'pty-1' }) }) + it('preserves unavailable process inspection', async () => { + const inspection = { + foregroundProcess: null, + hasChildProcesses: true, + unavailable: true as const + } + mux.request.mockResolvedValue(inspection) + + await expect(provider.inspectProcess(scopedPty1)).resolves.toEqual(inspection) + expect(mux.request).toHaveBeenCalledWith('pty.inspectProcess', { id: 'pty-1' }) + }) + it('serializes scoped app ids using raw relay ids', async () => { mux.request.mockResolvedValue('serialized') diff --git a/src/main/providers/ssh-pty-provider.ts b/src/main/providers/ssh-pty-provider.ts index 6dc0cb39a..59f20bfdf 100644 --- a/src/main/providers/ssh-pty-provider.ts +++ b/src/main/providers/ssh-pty-provider.ts @@ -23,6 +23,7 @@ import { import { buildSshPtySpawnRequest } from './ssh-pty-spawn-request' import { SshPtySpawnExitRaceTracker } from './ssh-pty-spawn-exit-race' import { SshAgentSessionCapabilities } from './ssh-agent-session-capabilities' +import type { PtyProcessInspection } from './pty-process-inspection' // 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 { @@ -281,12 +282,10 @@ export class SshPtyProvider implements IPtyProvider { return result as string | null } - async inspectProcess( - id: string - ): Promise<{ foregroundProcess: string | null; hasChildProcesses: boolean }> { + async inspectProcess(id: string): Promise { return (await this.mux.request('pty.inspectProcess', { id: this.toRelayPtyId(id) - })) as { foregroundProcess: string | null; hasChildProcesses: boolean } + })) as PtyProcessInspection } async serialize(ids: string[]): Promise { diff --git a/src/main/runtime/orca-runtime.test.ts b/src/main/runtime/orca-runtime.test.ts index 0029ed696..d16233a14 100644 --- a/src/main/runtime/orca-runtime.test.ts +++ b/src/main/runtime/orca-runtime.test.ts @@ -1297,7 +1297,7 @@ async function createExplicitAgentStatusHarness(options: { getForegroundProcess: (ptyId: string) => Promise inspectProcess?: ( ptyId: string - ) => Promise<{ foregroundProcess: string | null; hasChildProcesses: boolean }> + ) => Promise<{ foregroundProcess: string | null; hasChildProcesses: boolean; unavailable?: true }> confirmForegroundProcess?: (ptyId: string) => Promise title?: string }): Promise<{ @@ -10545,6 +10545,24 @@ describe('OrcaRuntimeService', () => { expect(getForegroundProcess).not.toHaveBeenCalled() }) + it('preserves provider unavailable results during process inspection', async () => { + const inspection = { + foregroundProcess: null, + hasChildProcesses: true, + unavailable: true as const + } + const inspectProcess = vi.fn(async () => inspection) + const getForegroundProcess = vi.fn(async () => null) + const { runtime, handle } = await createExplicitAgentStatusHarness({ + getForegroundProcess, + inspectProcess + }) + + await expect(runtime.inspectTerminalProcess(handle)).resolves.toEqual(inspection) + expect(inspectProcess).toHaveBeenCalledExactlyOnceWith('pty-1') + expect(getForegroundProcess).not.toHaveBeenCalled() + }) + it('calls foreground confirmation with its controller receiver', async () => { const getForegroundProcess = vi.fn(async () => 'powershell.exe') const confirmForegroundProcess = vi.fn( diff --git a/src/main/runtime/orca-runtime.ts b/src/main/runtime/orca-runtime.ts index d3a9cb257..49be1d9c1 100644 --- a/src/main/runtime/orca-runtime.ts +++ b/src/main/runtime/orca-runtime.ts @@ -1464,7 +1464,7 @@ type RuntimePtyController = { getForegroundProcess(ptyId: string): Promise inspectProcess?( ptyId: string - ): Promise<{ foregroundProcess: string | null; hasChildProcesses: boolean }> + ): Promise<{ foregroundProcess: string | null; hasChildProcesses: boolean; unavailable?: true }> confirmForegroundProcess?(ptyId: string): Promise hasChildProcesses?(ptyId: string): Promise clearBuffer?(ptyId: string): Promise @@ -15832,7 +15832,7 @@ export class OrcaRuntimeService { async inspectTerminalProcess( terminalSelector: string - ): Promise<{ foregroundProcess: string | null; hasChildProcesses: boolean }> { + ): Promise<{ foregroundProcess: string | null; hasChildProcesses: boolean; unavailable?: true }> { const leaf = this.resolveLiveLeafForHandle(terminalSelector) if (!leaf?.ptyId || !this.ptyController) { throw new Error('terminal_gone') diff --git a/src/preload/api-types.ts b/src/preload/api-types.ts index b72739e98..dc3cbba64 100644 --- a/src/preload/api-types.ts +++ b/src/preload/api-types.ts @@ -1374,9 +1374,11 @@ export type PreloadApi = { publishTerminalViewAttributes: (attributes: TerminalViewAttributes) => void hasChildProcesses: (id: string) => Promise getForegroundProcess: (id: string) => Promise - inspectProcess: ( - id: string - ) => Promise<{ foregroundProcess: string | null; hasChildProcesses: boolean }> + inspectProcess: (id: string) => Promise<{ + foregroundProcess: string | null + hasChildProcesses: boolean + unavailable?: true + }> confirmForegroundProcess: (id: string) => Promise getCwd: (id: string) => Promise getSize: (id: string) => Promise<{ cols: number; rows: number } | null> diff --git a/src/preload/index.ts b/src/preload/index.ts index 7de16fc46..8aa289018 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -984,8 +984,11 @@ const api = { ipcRenderer.invoke('pty:getForegroundProcess', { id }), inspectProcess: ( id: string - ): Promise<{ foregroundProcess: string | null; hasChildProcesses: boolean }> => - ipcRenderer.invoke('pty:inspectProcess', { id }), + ): Promise<{ + foregroundProcess: string | null + hasChildProcesses: boolean + unavailable?: true + }> => ipcRenderer.invoke('pty:inspectProcess', { id }), confirmForegroundProcess: (id: string): Promise => ipcRenderer.invoke('pty:confirmForegroundProcess', { id }), diff --git a/src/renderer/src/components/terminal-pane/agent-completion-coordinator.test.ts b/src/renderer/src/components/terminal-pane/agent-completion-coordinator.test.ts index 957e0a9bb..b2dfffb92 100644 --- a/src/renderer/src/components/terminal-pane/agent-completion-coordinator.test.ts +++ b/src/renderer/src/components/terminal-pane/agent-completion-coordinator.test.ts @@ -284,6 +284,80 @@ describe('agent completion coordinator', () => { }) }) + it('keeps hook working evidence across unavailable inspections', async () => { + const dispatchCompletion = vi.fn() + const coordinator = createAgentCompletionCoordinator({ + paneKey: 'tab-1:leaf-1', + getPtyId: () => 'pty-1', + getSettings: () => null, + inspectProcess: vi.fn(async () => ({ + foregroundProcess: null, + hasChildProcesses: true, + unavailable: true as const + })), + dispatchCompletion, + isLive: () => true + }) + + coordinator.startProcessTracking() + coordinator.observeHookStatus({ state: 'working', agentType: 'codex', prompt: 'test' }) + await vi.advanceTimersByTimeAsync(2_000) + coordinator.observeHookStatus({ state: 'done', agentType: 'codex', prompt: 'test' }) + + expect(dispatchCompletion).not.toHaveBeenCalled() + await vi.advanceTimersByTimeAsync(HOOK_DONE_QUIET_MS) + expect(dispatchCompletion).toHaveBeenCalledTimes(1) + }) + + it('keeps explicit-title evidence across unavailable inspections', async () => { + const dispatchCompletion = vi.fn() + const coordinator = createAgentCompletionCoordinator({ + paneKey: 'tab-1:leaf-1', + getPtyId: () => 'pty-1', + getSettings: () => null, + inspectProcess: vi.fn(async () => ({ + foregroundProcess: null, + hasChildProcesses: true, + unavailable: true as const + })), + dispatchCompletion, + isLive: () => true + }) + + coordinator.startProcessTracking() + coordinator.observeTitle('Codex working') + await vi.advanceTimersByTimeAsync(2_000) + coordinator.observeClassifiedTitleCompletion('done') + + expect(dispatchCompletion).toHaveBeenCalledExactlyOnceWith('done') + }) + + it('resets exit confirmation across an unavailable inspection', async () => { + let result: RuntimeTerminalProcessInspection = processResult('codex') + const dispatchCompletion = vi.fn() + const coordinator = createAgentCompletionCoordinator({ + paneKey: 'tab-1:leaf-1', + getPtyId: () => 'pty-1', + getSettings: () => null, + inspectProcess: vi.fn(async () => result), + dispatchCompletion, + isLive: () => true + }) + + coordinator.startProcessTracking() + await vi.advanceTimersByTimeAsync(2_000) + result = processResult(null, false) + await vi.advanceTimersByTimeAsync(750) + result = { foregroundProcess: null, hasChildProcesses: true, unavailable: true } + await vi.advanceTimersByTimeAsync(750) + result = processResult(null, false) + await vi.advanceTimersByTimeAsync(1_500) + expect(dispatchCompletion).not.toHaveBeenCalled() + + await vi.advanceTimersByTimeAsync(750) + expect(dispatchCompletion).toHaveBeenCalledTimes(1) + }) + it('does not mark an agent-to-agent process replacement as terminal idle', async () => { let foregroundProcess = 'codex' const dispatchCompletion = vi.fn() diff --git a/src/renderer/src/runtime/runtime-terminal-inspection.test.ts b/src/renderer/src/runtime/runtime-terminal-inspection.test.ts index 8202f5aa9..4223f6d2b 100644 --- a/src/renderer/src/runtime/runtime-terminal-inspection.test.ts +++ b/src/renderer/src/runtime/runtime-terminal-inspection.test.ts @@ -116,6 +116,27 @@ describe('runtime terminal owner routing', () => { expect(localHasChildren).not.toHaveBeenCalled() }) + it('preserves unavailable inspection from the PTY owning environment', async () => { + runtimeCall.mockResolvedValue({ + ok: true, + result: { + process: { foregroundProcess: null, hasChildProcesses: true, unavailable: true } + }, + _meta: { runtimeId: 'runtime-1' } + }) + + await expect( + inspectRuntimeTerminalProcess( + { activeRuntimeEnvironmentId: 'env-2' }, + 'remote:env-1@@terminal-1' + ) + ).resolves.toEqual({ + foregroundProcess: null, + hasChildProcesses: true, + unavailable: true + }) + }) + it('uses strict main-process inspection for a direct SSH PTY', async () => { localInspect.mockResolvedValue({ foregroundProcess: 'codex', hasChildProcesses: true }) @@ -128,6 +149,20 @@ describe('runtime terminal owner routing', () => { expect(localHasChildren).not.toHaveBeenCalled() }) + it('preserves unavailable inspection for a direct SSH PTY', async () => { + localInspect.mockResolvedValue({ + foregroundProcess: null, + hasChildProcesses: true, + unavailable: true + }) + + await expect(inspectRuntimeTerminalProcess(null, 'ssh:host@@pty-1')).resolves.toEqual({ + foregroundProcess: null, + hasChildProcesses: true, + unavailable: true + }) + }) + it.each(['no_connected_pty', 'terminal_handle_stale', 'terminal_gone'])( 'reports %s remote process inspection as unavailable', async (code) => {