From 0656bce51f2e568dc6ed3e76f11bee8fcec7ebd4 Mon Sep 17 00:00:00 2001 From: Neil Date: Wed, 11 Nov 2026 06:05:27 -0800 Subject: [PATCH] fix(test,checkpoints): aim the refused-task fixture at its task; discover repos once structured-worker-cli 'keeps a refused first task inspectable' armed a global 'reject the next turn/start' right after 'agent launch' returned. launch returns at input_accepted, before the coordinator's own first turn reaches the agent, and #191's pre-turn snapshot widened that gap, so the coordinator's turn sometimes consumed the rejection and the worker's task was accepted. Product refusal was never lost: the logged turn/start order shows the coordinator refused instead. The fixture now refuses only the turn carrying the task text. Checkpoints also discover the repo once per folder (not-a-repo rechecked after 60 s, cache dropped on any capture failure), so later sends add no git spawn before the turn starts. 20/20 runs green (8/8 tests each). --- .../checkpoints/agent-session-checkpoints.ts | 32 +++++++++++++++++-- .../structured-worker-cli.integration.test.ts | 17 +++++++--- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/main/native-chat/agent-session-wire/checkpoints/agent-session-checkpoints.ts b/src/main/native-chat/agent-session-wire/checkpoints/agent-session-checkpoints.ts index c372aa82751..11960fd8613 100644 --- a/src/main/native-chat/agent-session-wire/checkpoints/agent-session-checkpoints.ts +++ b/src/main/native-chat/agent-session-wire/checkpoints/agent-session-checkpoints.ts @@ -68,12 +68,38 @@ function refuse(reason: Parameters[ throw new Error(agentSessionCheckpointRefusalMessage(reason)) } +// Repo discovery is a git spawn; a send pays it before its turn starts, so it runs once per folder. +// A folder that isn't a repo is re-checked after a minute, so a later `git init` is picked up. +const NOT_A_REPO_RECHECK_MS = 60_000 +const discovered = new Map | { notRepoUntil: number }>() +function openCached(directory: string): Promise { + const known = discovered.get(directory) + if (known && 'then' in known) { + return known + } + if (known && Date.now() < known.notRepoUntil) { + return Promise.reject(new WorktreeCheckpointError('not-a-repo', `${directory} is not a repo.`)) + } + const opening = openCheckpointRepo(directory) + discovered.set(directory, opening) + opening.catch((error: unknown) => { + if (discovered.get(directory) !== opening) { + return + } + if (error instanceof WorktreeCheckpointError && error.reason === 'not-a-repo') { + discovered.set(directory, { notRepoUntil: Date.now() + NOT_A_REPO_RECHECK_MS }) + } else { + discovered.delete(directory) + } + }) + return opening +} + /** Something else in the workspace that may write files: a terminal agent, say. */ /** Gets the worktree root the restore would write. */ export type AgentSessionWorkspaceBusy = (root: string) => Promise export function createAgentSessionCheckpoints(deps: AgentSessionCheckpointsDeps) { - const open = deps.openRepo ?? ((directory: string) => openCheckpointRepo(directory)) const repoFor = async (sessionId: string): Promise => { const record = deps.record(sessionId) // WSL worktrees route git through another filesystem; their temp index paths don't map. @@ -82,7 +108,7 @@ export function createAgentSessionCheckpoints(deps: AgentSessionCheckpointsDeps) } const directory = record.launchDirectory ?? (await deps.resolveWorkspacePath?.(record.location.workspaceId)) - return directory ? open(directory) : null + return directory ? (deps.openRepo ? deps.openRepo(directory) : openCached(directory)) : null } const captureTurn = async ( @@ -119,6 +145,8 @@ export function createAgentSessionCheckpoints(deps: AgentSessionCheckpointsDeps) }) } catch (error) { if (!(error instanceof WorktreeCheckpointError && error.reason === 'not-a-repo')) { + // The folder may have moved or been recreated; rediscover on the next send. + discovered.clear() deps.logger.warn('[agent-session] turn checkpoint skipped', { scope: 'checkpoint-capture', sessionId, diff --git a/src/main/runtime/rpc/structured-worker-cli.integration.test.ts b/src/main/runtime/rpc/structured-worker-cli.integration.test.ts index 872daba1539..c5bd17534ca 100644 --- a/src/main/runtime/rpc/structured-worker-cli.integration.test.ts +++ b/src/main/runtime/rpc/structured-worker-cli.integration.test.ts @@ -59,14 +59,17 @@ let codex: ReturnType let socket: UnixSocketTransport | undefined let runId: string let finishTurns = true -let rejectNextTurn = false +/** Refuses the turn/start whose input carries this text; aimed at one task, so a turn another + * chat starts later (the coordinator's own first turn can land after `agent launch` returns) + * can't consume it. */ +let rejectTurnContaining: string | null = null const dispatches: string[] = [] const requests: RpcRequest[] = [] beforeEach(async () => { resetProviderFaults() finishTurns = true - rejectNextTurn = false + rejectTurnContaining = null rig = await authorityProfile([]) await mkdir(join(root, 'codex-home')) rig.profile.updateSettings({ @@ -89,8 +92,12 @@ beforeEach(async () => { const connection = await codex.openConnection(...args) const request = connection.request.bind(connection) connection.request = async (method, params) => { - if (method === 'turn/start' && rejectNextTurn) { - rejectNextTurn = false + if ( + method === 'turn/start' && + rejectTurnContaining !== null && + JSON.stringify(params).includes(rejectTurnContaining) + ) { + rejectTurnContaining = null throw new CodexAppServerRequestError('turn/start', -32000, 'Task refused', 'Task refused') } const response = await request(method, params) @@ -349,7 +356,7 @@ it.each(['worker-release', 'retry-of'] as const)( 'keeps a refused first task inspectable across identity reload until %s releases it', async (cleanup) => { const coordinator = await launch() - rejectNextTurn = true + rejectTurnContaining = 'Refused fixture task' const receipt = await cli([ 'orchestration', 'worker-start',