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).
This commit is contained in:
Neil
2026-11-11 06:05:27 -08:00
parent 07288af2a6
commit 0656bce51f
2 changed files with 42 additions and 7 deletions
@@ -68,12 +68,38 @@ function refuse(reason: Parameters<typeof agentSessionCheckpointRefusalMessage>[
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<string, Promise<CheckpointRepo> | { notRepoUntil: number }>()
function openCached(directory: string): Promise<CheckpointRepo> {
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<boolean>
export function createAgentSessionCheckpoints(deps: AgentSessionCheckpointsDeps) {
const open = deps.openRepo ?? ((directory: string) => openCheckpointRepo(directory))
const repoFor = async (sessionId: string): Promise<CheckpointRepo | null> => {
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,
@@ -59,14 +59,17 @@ let codex: ReturnType<typeof fakeCodex>
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',