diff --git a/src/main/pi/agent-status-extension-source.test.ts b/src/main/pi/agent-status-extension-source.test.ts index 3bae53f6cf6..e5b610a34c5 100644 --- a/src/main/pi/agent-status-extension-source.test.ts +++ b/src/main/pi/agent-status-extension-source.test.ts @@ -6,7 +6,13 @@ describe('getPiAgentStatusExtensionSource', () => { it('registers Prime hooks only in the event-emitting daemon worker', () => { const frontend = createHarness({ kind: 'prime-agent', - env: { PRIME_AGENT_INTERNAL_DAEMON_WORKER: undefined } + env: { + PRIME_AGENT_INTERNAL_DAEMON_WORKER: undefined, + ORCA_PRIME_AGENT_STATUS_OWNED: String(SELF_PID - 1) + }, + killImpl: () => { + throw Object.assign(new Error('ESRCH'), { code: 'ESRCH' }) + } }) const worker = createHarness({ kind: 'prime-agent', @@ -14,7 +20,8 @@ describe('getPiAgentStatusExtensionSource', () => { }) expect(frontend.handlers).toEqual({}) - expect(frontend.processEnv.ORCA_PI_STATUS_OWNED).toBeUndefined() + expect(frontend.killMock).not.toHaveBeenCalled() + expect(frontend.processEnv.ORCA_PRIME_AGENT_STATUS_OWNED).toBe(String(SELF_PID - 1)) expect(worker.handlers.agent_start).toBeTypeOf('function') expect(worker.processEnv.ORCA_PRIME_AGENT_STATUS_OWNED).toBe(String(SELF_PID)) }) @@ -322,6 +329,30 @@ describe('getPiAgentStatusExtensionSource', () => { expect(harness.processEnv.ORCA_PI_STATUS_OWNED).toBe(String(SELF_PID)) }) + it('claims the pane when the inherited owner PID exceeds safe integer precision', () => { + const harness = createHarness({ + kind: 'pi', + pid: SELF_PID, + env: { ORCA_PI_STATUS_OWNED: '99999999999999999999999' } + }) + + expect(harness.killMock).not.toHaveBeenCalled() + expect(harness.handlers.agent_end).toBeTypeOf('function') + expect(harness.processEnv.ORCA_PI_STATUS_OWNED).toBe(String(SELF_PID)) + }) + + it('claims the pane when the inherited owner PID exceeds the process API range', () => { + const harness = createHarness({ + kind: 'pi', + pid: SELF_PID, + env: { ORCA_PI_STATUS_OWNED: String(2 ** 31) } + }) + + expect(harness.killMock).not.toHaveBeenCalled() + expect(harness.handlers.agent_end).toBeTypeOf('function') + expect(harness.processEnv.ORCA_PI_STATUS_OWNED).toBe(String(SELF_PID)) + }) + it.each(['pi', 'omp', 'prime-agent'] as const)( 'registers no status handlers for a nested %s subagent process', (kind) => { diff --git a/src/main/pi/agent-status-handler-source.ts b/src/main/pi/agent-status-handler-source.ts index b9c4b7583ae..f4b417d4d01 100644 --- a/src/main/pi/agent-status-handler-source.ts +++ b/src/main/pi/agent-status-handler-source.ts @@ -66,7 +66,7 @@ export function getPiAgentStatusHandlerSourceLines(kind: PiAgentKind): string[] '// import (the extension loads inside pi/omp with no Orca deps).', 'function isStatusOwnerAlive(pid: string): boolean {', ' const parsed = Number(pid)', - ' if (!Number.isInteger(parsed) || parsed < 1) return false', + ' if (!Number.isSafeInteger(parsed) || parsed < 1 || parsed > 0x7fffffff) return false', " if (typeof process.kill !== 'function') return true", ' try {', ' process.kill(parsed, 0)',