From bb97a6512fc76ed6a2eb59ea1e5532b1082c7c00 Mon Sep 17 00:00:00 2001 From: SSU-WEI HUANG Date: Fri, 13 Nov 2026 18:48:00 +0800 Subject: [PATCH] fix(runner): verified exit must confirm stop despite unrelated protected wrapper (#2346) * fix(runner): let verified exit confirm stop when only unrelated wrappers are protected * test(runner): verified exit confirms stop despite unrelated protected wrapper --- cli/src/runner/run.stopSession.test.ts | 12 +++++++++++- cli/src/runner/run.ts | 5 ++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/cli/src/runner/run.stopSession.test.ts b/cli/src/runner/run.stopSession.test.ts index 58121682..aa8094bb 100644 --- a/cli/src/runner/run.stopSession.test.ts +++ b/cli/src/runner/run.stopSession.test.ts @@ -1,4 +1,4 @@ -import { mkdtempSync, rmSync } from 'node:fs' +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' @@ -50,6 +50,7 @@ import { startRunner } from './run' describe('runner stop confirmation after shared Codex tracking loss', () => { let stopSession: Parameters[0]['stopSession'] + let reportSession: Parameters[0]['onHappySessionWebhook'] let record: CodexRuntimeRecord let home: string @@ -59,6 +60,7 @@ describe('runner stop confirmation after shared Codex tracking loss', () => { const captured = new Error('control handlers captured before runner networking') mocks.controlServer.mockImplementation(async handlers => { stopSession = handlers.stopSession + reportSession = handlers.onHappySessionWebhook throw captured }) // Stop startup at control-server registration. Exercise its real closure @@ -67,6 +69,7 @@ describe('runner stop confirmation after shared Codex tracking loss', () => { vi.spyOn(process, 'exit').mockImplementation(() => { throw captured }) vi.stubEnv('HAPI_RUNNER_HANDOFF_FROM_PID', '') Object.defineProperty(configuration, 'runnerStateFile', { value: join(home, 'runner.json'), configurable: true }) + writeFileSync(join(home, 'runner.json.verified-exits.json'), JSON.stringify(['exited'])) try { await expect(startRunner()).rejects.toBe(captured) expect(stopSession!).toBeTypeOf('function') @@ -115,6 +118,13 @@ describe('runner stop confirmation after shared Codex tracking loss', () => { await expect(stopSession('target')).resolves.toBe('unknown') }) + it('lets verified exit confirm stop despite an unrelated protected wrapper', async () => { + record.sessions = {} + reportSession('other', { hostPid: 4242, startedBy: 'terminal', capabilities: { concurrentClients: true } } as Parameters[1]) + await expect(stopSession('exited')).resolves.toBe('already_gone') + await expect(stopSession('target')).resolves.toBe('unknown') + }) + it('still refuses when the orphan sweep cannot establish exit', async () => { mocks.reapOrphans.mockResolvedValue('still_alive') await expect(stopSession('target')).resolves.toBe('still_alive') diff --git a/cli/src/runner/run.ts b/cli/src/runner/run.ts index 167abd4d..697ddd30 100644 --- a/cli/src/runner/run.ts +++ b/cli/src/runner/run.ts @@ -1709,7 +1709,10 @@ export async function startRunner(options: { workspaceRoots?: string[] } = {}): // whose wrapper may still be finishing cleanup after tracking loss. // Sibling protection alone remains insufficient proof of archival. const binding = sessionRegistryBindingState(liveRuntimes, sessionId); - if (binding !== 'absent' || sessionRuntimeHasActiveSiblings(liveRuntimes, sessionId) || protectedTrackedPids.size > 0) { + // Protected wrappers hosting only other sessions must not mask this + // session's verified exit, or reconcileCodexResume stays pending forever. + if (binding !== 'absent' || sessionRuntimeHasActiveSiblings(liveRuntimes, sessionId) + || (protectedTrackedPids.size > 0 && !hasVerifiedExit(sessionId))) { const decision = decideKeepWrapperArchive(binding); logger.debug( `[RUNNER RUN] Session ${sessionId}; shared binding=${binding}; archive=${decision}`