|
| 1 | +/** |
| 2 | + * Steering rotation: runSubAgent mints the submit_result turn token at leaf |
| 3 | + * dispatch and rotates it on every followup steer, so a worker holding the |
| 4 | + * dispatched token cannot submit after its turn was superseded. The pure |
| 5 | + * evaluator half (old token rejected, budget reset) is covered in |
| 6 | + * submit-result.test.ts; this test drives the real runSubAgent wiring end to |
| 7 | + * end — the one seam the pure tests cannot see is whether followup actually |
| 8 | + * mints, swaps, and re-states the token. Same stub-agent pattern as |
| 9 | + * followup-live-agent.test.ts. |
| 10 | + */ |
| 11 | +import { describe, expect, test } from "bun:test"; |
| 12 | +import { mkdtemp } from "node:fs/promises"; |
| 13 | +import { tmpdir } from "node:os"; |
| 14 | +import { join } from "node:path"; |
| 15 | + |
| 16 | +import { withMockedModuleDuring } from "../../tests/helpers/mock-module.js"; |
| 17 | +import { defined } from "../../tests/helpers/defined.js"; |
| 18 | +import { createPermissionGate } from "../permission/gate.js"; |
| 19 | +import type { RunSubAgentParams } from "./types.js"; |
| 20 | + |
| 21 | +const testPermissionGate = createPermissionGate({ |
| 22 | + approvals: [], |
| 23 | + interactive: false, |
| 24 | + skipPermissions: true, |
| 25 | + reactorGated: false, |
| 26 | +}); |
| 27 | + |
| 28 | +/** First send hangs (the run stays alive for steering); later sends resolve. */ |
| 29 | +function createRotatingStubAgent(sendLog: string[]) { |
| 30 | + return { |
| 31 | + async send(content: string, optsSend?: { signal?: AbortSignal }) { |
| 32 | + sendLog.push(content); |
| 33 | + if (sendLog.length === 1) { |
| 34 | + return await new Promise((_, reject) => { |
| 35 | + if (optsSend?.signal?.aborted === true) { |
| 36 | + reject( |
| 37 | + optsSend.signal.reason instanceof Error |
| 38 | + ? optsSend.signal.reason |
| 39 | + : new Error("aborted"), |
| 40 | + ); |
| 41 | + return; |
| 42 | + } |
| 43 | + optsSend?.signal?.addEventListener( |
| 44 | + "abort", |
| 45 | + () => { |
| 46 | + const reason = defined(optsSend.signal).reason; |
| 47 | + reject(reason instanceof Error ? reason : new Error("aborted")); |
| 48 | + }, |
| 49 | + { once: true }, |
| 50 | + ); |
| 51 | + }); |
| 52 | + } |
| 53 | + return { |
| 54 | + type: "reply" as const, |
| 55 | + reply: `reply #${sendLog.length}`, |
| 56 | + turn: { role: "assistant", content: [] }, |
| 57 | + }; |
| 58 | + }, |
| 59 | + stream: () => |
| 60 | + (async function* () { |
| 61 | + yield* []; |
| 62 | + })(), |
| 63 | + deliver: () => undefined, |
| 64 | + close: async () => undefined, |
| 65 | + setSource: () => undefined, |
| 66 | + setSources: () => undefined, |
| 67 | + history: async () => [], |
| 68 | + checkpoints: async () => [], |
| 69 | + readAt: async () => [], |
| 70 | + blobReader: {}, |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +function tokenOfSend(send: string): string | undefined { |
| 75 | + return send.match(/^## Turn token\n(.+)$/m)?.[1]; |
| 76 | +} |
| 77 | + |
| 78 | +describe("submit_result token rotation on steering", () => { |
| 79 | + test("followup rotates the leaf turn token and states the replacement in the steer", async () => { |
| 80 | + const cwd = await mkdtemp(join(tmpdir(), "cl6946-token-rotation-")); |
| 81 | + const sendLog: string[] = []; |
| 82 | + |
| 83 | + const outcome = await withMockedModuleDuring( |
| 84 | + import.meta.resolve("../agent/live-tool-dispatch.js"), |
| 85 | + (real: typeof import("../agent/live-tool-dispatch.js")) => ({ |
| 86 | + ...real, |
| 87 | + createAgentWithLiveToolDispatch: async () => |
| 88 | + createRotatingStubAgent(sendLog) as unknown as Awaited< |
| 89 | + ReturnType<typeof real.createAgentWithLiveToolDispatch> |
| 90 | + >, |
| 91 | + }), |
| 92 | + async () => { |
| 93 | + const { runSubAgent } = await import("./run.js"); |
| 94 | + |
| 95 | + let handles: |
| 96 | + | { |
| 97 | + close: (ms?: number) => Promise<void>; |
| 98 | + interrupt: () => void; |
| 99 | + followup: (message: string) => Promise<string>; |
| 100 | + } |
| 101 | + | undefined; |
| 102 | + |
| 103 | + const params: RunSubAgentParams = { |
| 104 | + cwd, |
| 105 | + workdirBase: join(cwd, ".ctx"), |
| 106 | + permissionGate: testPermissionGate, |
| 107 | + provider: { |
| 108 | + providerName: "test", |
| 109 | + baseURL: "http://localhost", |
| 110 | + model: "test-model", |
| 111 | + }, |
| 112 | + description: "token rotation probe", |
| 113 | + prompt: "hold for steering", |
| 114 | + persist: true, |
| 115 | + tier: "leaf", |
| 116 | + onAgentReady: (h) => { |
| 117 | + handles = h; |
| 118 | + }, |
| 119 | + }; |
| 120 | + |
| 121 | + const runPromise = runSubAgent(params); |
| 122 | + for (let i = 0; i < 500 && sendLog.length < 1; i++) { |
| 123 | + await new Promise((resolve) => setTimeout(resolve, 1)); |
| 124 | + } |
| 125 | + if (handles === undefined) throw new Error("onAgentReady never fired"); |
| 126 | + |
| 127 | + const reply = await handles.followup("new orders: pivot to X"); |
| 128 | + // followup replaced the per-turn interrupt controller, so interrupt() |
| 129 | + // can no longer reach the still-hung first send — close() aborts the |
| 130 | + // run controller instead and settles the run for cleanup. Both can |
| 131 | + // reject with the abort reason; settlement is all we need. |
| 132 | + await handles.close().catch(() => undefined); |
| 133 | + await runPromise.catch(() => undefined); |
| 134 | + return { reply }; |
| 135 | + }, |
| 136 | + ); |
| 137 | + |
| 138 | + expect(outcome.reply).toBe("reply #2"); |
| 139 | + expect(sendLog.length).toBe(2); |
| 140 | + |
| 141 | + // The dispatched brief states the first token; the steer carries the |
| 142 | + // followup message plus a DIFFERENT token — the old one dies here. |
| 143 | + const dispatched = tokenOfSend(defined(sendLog[0])); |
| 144 | + const steered = tokenOfSend(defined(sendLog[1])); |
| 145 | + expect(dispatched).toBeDefined(); |
| 146 | + expect(steered).toBeDefined(); |
| 147 | + expect(defined(steered)).not.toBe(defined(dispatched)); |
| 148 | + expect(defined(sendLog[1]).startsWith("new orders: pivot to X")).toBe(true); |
| 149 | + expect(defined(sendLog[1])).toContain(`turn_token="${defined(steered)}"`); |
| 150 | + }); |
| 151 | +}); |
0 commit comments