Fix #12: the final hand-back after an interim stop always gets its line

EV-20 (PR #271) sat 8 h in router_check: after an interim stop in round 3 the
Opus dev's final hand-back got no "no verification" line.

- An agent that stays idle (waiting for a message, not on its own work) for
  2 minutes has handed back. A moment idle between turns does not count;
  waiting on background work stays an interim stop.
- Watches are kept in the store: a plugin reload cancels the timers, and the
  next event picks the stored watches up again.
- /router lists the agents still waiting for a hand-back, and for how long.

5 tests (one old test now goes idle briefly, then runs); 6 mutations, all caught.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MdqPWvuDpbpRvDEdURoAwo
This commit is contained in:
2026-11-24 02:08:24 +00:00
co-authored by Claude Opus 5.5
parent 2184024fe7
commit fade3b8bba
3 changed files with 177 additions and 11 deletions
+108 -3
View File
@@ -310,6 +310,8 @@ type World = {
/** Untracked files in the clone; `addDuringTests` joins them while the full run goes. */
untracked?: string[]
addDuringTests?: string
/** What the plugin's store holds at the start. */
seed?: Record<string, unknown>
/** What `git rev-parse HEAD` answers. */
headSha?: string
}
@@ -320,7 +322,7 @@ const ok = (stdout: string, exitCode = 0) => ({
})
function world(on: On, w: World) {
mock.store(on)
mock.store(on, w.seed)
const clock = mock.clock(on)
on('ui.status', async () => ({ value: undefined }))
on('ui.toast', async () => ({ value: undefined }))
@@ -771,7 +773,7 @@ test('TOOL-12: review and test agents get no hand-back line', async ($, on) => {
// ---------- interim stops (TOOL-12 follow-up) ----------
/** A turn end of the agent while it is still alive: its status says so. */
async function interimStop($: Engine, clock: { advance: (ms: number) => Promise<void> }, w: World, agentId: string, status = 'waiting') {
async function interimStop($: Engine, clock: { advance: (ms: number) => Promise<void> }, w: World, agentId: string, status = 'waiting', model = 'claude-sonnet-5-5') {
w.status[agentId] = status
await ($.turn as unknown as { complete: (e: unknown) => Promise<unknown> }).complete({
answer: 'Waiting for the test suite.',
@@ -780,7 +782,7 @@ async function interimStop($: Engine, clock: { advance: (ms: number) => Promise<
turnId: 't',
agentId,
reason: 'answer',
usage: { model: 'claude-sonnet-5-5', input_tokens: 1, output_tokens: 1, cache_creation_input_tokens: 0, cache_read_input_tokens: 0 },
usage: { model, input_tokens: 1, output_tokens: 1, cache_creation_input_tokens: 0, cache_read_input_tokens: 0 },
})
await clock.advance(1)
}
@@ -814,6 +816,8 @@ test('follow-up: an interim stop of an Opus dev posts nothing; its hand-back pos
const id = r.agentId as string
await interimStop($, clock, w, id)
await interimStop($, clock, w, id, 'idle')
await clock.advance(60_000) // a moment idle between turns (under IDLE_HANDBACK_MS) ...
w.status[id] = 'running' // ... then its next turn
await clock.advance(10 * 60 * 1000)
expect(w.all).toEqual([])
w.status[id] = 'completed'
@@ -1428,3 +1432,104 @@ test('#11: header lines are not doubled, text after the marker is guidance, mid-
expect('Call Agent with prompt [router-rewrite T9] now'.match(REWRITE_MARK)).toBe(null)
expect('> [router-rewrite T9]'.match(REWRITE_MARK)?.[1]).toBe('T9')
})
// ---------- #12: the last hand-back after an interim stop always gets its line ----------
const OPUS_DEV = `EV-20: decide how the export ramp should recover. Clone: ${DEV}/`
test('#12: an Opus dev that ends idle after an interim stop gets its line (EV-20 round 3)', async ($, on) => {
const w = fresh()
const clock = world(on, w)
const r = await $.agent.spawn({ prompt: OPUS_DEV, description: 'EV-20 dev' } as never)
const id = r.agentId as string
w.status[id] = 'completed'
await finish($, clock, id, 'claude-opus-5-5') // round 1
await resume($, id, 'round 2')
await finish($, clock, id, 'claude-opus-5-5') // round 2
expect(w.all.length).toBe(2)
await resume($, id, 'round 3')
await interimStop($, clock, w, id, 'waiting', 'claude-opus-5-5') // on its own background work
await clock.advance(30 * 60 * 1000)
expect(w.all.length).toBe(2)
// its work is done; it ends its turn and stays idle, waiting for the manager
await interimStop($, clock, w, id, 'idle', 'claude-opus-5-5')
await clock.advance(60_000)
expect(w.all.length).toBe(2) // not yet: a moment idle is no hand-back
await clock.advance(2 * 60_000)
expect(w.all.length).toBe(3)
expect(w.all[2]).toBe('[model-router] T1 no verification: dev ran on Opus. Go straight to review.')
await clock.advance(60 * 60_000)
expect(w.all.length).toBe(3) // once
})
test('#12: a plugin reload during an interim stop does not lose the hand-back', async ($, on) => {
const w = fresh()
w.diffs = ['diff']
// after a hot reload: the watch is in the store, but nothing in the new module watches it
w.seed = { watching: { 'agent-1': { wid: 'before-reload', ended: 'answer', reported: 'claude-sonnet-5-5', since: 0 } } }
const clock = world(on, w)
const r = await $.agent.spawn({ prompt: devBrief(), description: 'dev' } as never)
const id = r.agentId as string
expect(id).toBe('agent-1')
w.status[id] = 'completed'
await clock.advance(60_000)
expect(w.all).toEqual([])
// the next event after the reload (here a main-loop turn) picks the stored watch up
await ($.turn as unknown as { complete: (e: unknown) => Promise<unknown> }).complete({
answer: 'ok', durationMs: 1, isAborted: false, turnId: 'main', reason: 'answer',
})
await clock.advance(1)
expect(w.handBacks).toEqual(['[model-router] T1 verifying on Opus...'])
expect(w.verifier).toEqual(['opus'])
await clock.advance(60 * 60_000)
expect(w.handBacks.length).toBe(1)
})
test('#12: a watch that ran its course is not resumed after a reload', async ($, on) => {
const w = fresh()
w.diffs = ['diff']
const clock = world(on, w)
const r = await $.agent.spawn({ prompt: devBrief(), description: 'dev' } as never)
const id = r.agentId as string
w.status[id] = 'completed'
await finish($, clock, id, 'claude-sonnet-5-5')
expect(w.handBacks.length).toBe(1)
await ($.turn as unknown as { complete: (e: unknown) => Promise<unknown> }).complete({
answer: 'ok', durationMs: 1, isAborted: false, turnId: 'main', reason: 'answer',
})
await clock.advance(60_000)
expect(w.handBacks.length).toBe(1)
expect(w.verifier.length).toBe(1)
})
test('#12: idle only counts when it is held: idle, running, idle again starts the clock over', async ($, on) => {
const w = fresh()
const clock = world(on, w)
const r = await $.agent.spawn({ prompt: OPUS_DEV, description: 'EV-20 dev' } as never)
const id = r.agentId as string
await interimStop($, clock, w, id, 'idle', 'claude-opus-5-5')
await clock.advance(90_000)
w.status[id] = 'running'
await clock.advance(30_000)
w.status[id] = 'idle'
await clock.advance(90_000) // 3 min idle in all, never 2 in a row
expect(w.all).toEqual([])
await clock.advance(60_000)
expect(w.all).toEqual(['[model-router] T1 no verification: dev ran on Opus. Go straight to review.'])
})
test('#12: /router lists the agents still waiting for a hand-back', async ($, on) => {
const w = fresh()
const clock = world(on, w)
const r = await $.agent.spawn({ prompt: OPUS_DEV, description: 'EV-20 dev' } as never)
const id = r.agentId as string
await interimStop($, clock, w, id, 'waiting', 'claude-opus-5-5')
await clock.advance(30 * 60_000)
const during = (await $.command.run({ command: 'router', args: '' } as never)).text ?? ''
expect(during).toContain(`waiting for a hand-back: T1 (agent ${id}, 30 min)`)
w.status[id] = 'completed'
await clock.advance(16_000)
const after = (await $.command.run({ command: 'router', args: '' } as never)).text ?? ''
expect(after).not.toContain('waiting for a hand-back')
})