Public: anyone can read this; the crew can change it.
Talk: a turn's words written before it heard a confirm's answer are not kept, so the thread never says a pressed Do it still waits #17507
Merged 26h 23m ago, live in production
talk-stale → main 0 commits 2 threads · 2 unresolved
4 files
changed +74
−0
src/crew/agent_runner.ts +5
| @@ -60,6 +60,11 @@ export interface AgentRun { | ||
| 60 | 60 | * send after the session ended returns false. |
| 61 | 61 | */ |
| 62 | 62 | readonly onSteer?: ((send: (text: string) => boolean) => void) | undefined; |
| 63 | /** | |
| 64 | * The answer to one user turn ended: words steered in while it ran are what the agent hears | |
| 65 | * next, so what it said before this, it said without them. | |
| 66 | */ | |
| 67 | readonly onAnswered?: (() => void) | undefined; | |
| 63 | 68 | /** |
| 64 | 69 | * The session's spend so far, each time it reports some: tokens as each |
| 65 | 70 | * assistant message's `usage` arrives, the cost once the result names it |
src/crew/claude.ts +1
| @@ -509,6 +509,7 @@ async function runClaudeRaw(input: ClaudeInput): Promise<ClaudeOutcome> { | ||
| 509 | 509 | } |
| 510 | 510 | // A turn is over. Something said meanwhile is the next turn; nothing |
| 511 | 511 | // said means the session is done, and closing stdin lets it end. |
| 512 | input.onAnswered?.(); | |
| 512 | 513 | if (pending > 0 && ok) { |
| 513 | 514 | pending--; |
| 514 | 515 | armIdle(); |
src/crew/conversation.ts +29
| @@ -149,6 +149,7 @@ export function reportAnswered(app: App, userId: string, conversationId: string, | ||
| 149 | 149 | const running = live.get(conversationId); |
| 150 | 150 | // A turn that can hear words now is handed the answer between its steps. |
| 151 | 151 | if (running !== undefined && turnHears(conversationId) && steerRun(app, running, said.text)) { |
| 152 | aheadOf(app).set(running, (aheadOf(app).get(running) ?? 0) + 1); | |
| 152 | 153 | if (said.fenced) readFenced(app.db, via); |
| 153 | 154 | return true; |
| 154 | 155 | } |
| @@ -260,6 +261,23 @@ export function reportedBefore( | ||
| 260 | 261 | /** Turns with a session listening, by conversation id: the run id a message is steered to. */ |
| 261 | 262 | const live = new Map<string, string>(); |
| 262 | 263 | |
| 264 | /** | |
| 265 | * Answers to held calls handed to a running turn (`reportAnswered`) that its agent has not reached | |
| 266 | * yet, by run: words steered in are heard after the answer it is writing ends. What it says before | |
| 267 | * then narrates a state the answer already changed (a confirm "waiting on your yes" that was | |
| 268 | * pressed), so it is not kept; it speaks again once it has heard. | |
| 269 | */ | |
| 270 | const answersAhead = new WeakMap<App, Map<string, number>>(); | |
| 271 | ||
| 272 | /** | |
| 273 | * This forge's answers ahead of its runs (`answersAhead`): a forge's own state, not the process's. | |
| 274 | */ | |
| 275 | const aheadOf = (app: App): Map<string, number> => { | |
| 276 | let ahead = answersAhead.get(app); | |
| 277 | if (ahead === undefined) answersAhead.set(app, (ahead = new Map())); | |
| 278 | return ahead; | |
| 279 | }; | |
| 280 | ||
| 263 | 281 | /** |
| 264 | 282 | * Where a turn runs: an empty directory of its own under the data dir, made at |
| 265 | 283 | * first use, so nothing a turn could name sits beside forge.db or secret.key. |
| @@ -980,7 +998,17 @@ function begin( | ||
| 980 | 998 | else calls.ended(delta.id, delta.name, delta.ok !== false); |
| 981 | 999 | } |
| 982 | 1000 | }, |
| 1001 | onAnswered: () => { | |
| 1002 | const ahead = (aheadOf(app).get(runId) ?? 0) - 1; | |
| 1003 | if (ahead > 0) aheadOf(app).set(runId, ahead); | |
| 1004 | else aheadOf(app).delete(runId); | |
| 1005 | }, | |
| 983 | 1006 | onSaid: (said) => { |
| 1007 | // Said before it heard an answer to a held call: superseded, so not kept. | |
| 1008 | if (said.kind === "text" && aheadOf(app).has(runId)) { | |
| 1009 | appendRunLog(app.config.dataDir, runId, `not kept, said before it heard an answer: ${said.text.slice(0, 200)}`); | |
| 1010 | return; | |
| 1011 | } | |
| 984 | 1012 | liveSaid(app.db, conversation.id, said.kind, said.text); |
| 985 | 1013 | if (said.kind === "text") lastText = said.text; |
| 986 | 1014 | if (said.text.trim() === "") return; |
| @@ -1089,6 +1117,7 @@ function begin( | ||
| 1089 | 1117 | ended = { how: outcome === undefined ? "error" : outcome.ok ? "ok" : outcome.reason, usd: cost }; |
| 1090 | 1118 | } finally { |
| 1091 | 1119 | if (live.get(conversation.id) === runId) live.delete(conversation.id); |
| 1120 | aheadOf(app).delete(runId); | |
| 1092 | 1121 | if (stoppers.get(conversation.id)?.runId === runId) stoppers.delete(conversation.id); |
| 1093 | 1122 | liveEnd(app.db, conversation.id); |
| 1094 | 1123 | // A restart leaves the next message unsent (its turn would be cut again); anything else sends |
test/talk-confirm-result.test.ts +39
| @@ -420,6 +420,45 @@ describe("a Not now", () => { | ||
| 420 | 420 | }); |
| 421 | 421 | }); |
| 422 | 422 | |
| 423 | describe("a turn that hears an answer late", () => { | |
| 424 | test("what it said before hearing it is not kept: the thread never says a pressed confirm still waits", async () => { | |
| 425 | const made = await createRepo(app, alice, { name: "late", visibility: "public" }); | |
| 426 | if (!made.ok) throw new Error(String(made.error)); | |
| 427 | // Writes its answer only once the answer to the hold was steered in, as the owner's turn did | |
| 428 | // (03:31:18 answered, 03:31:20 "waiting on your yes"), then answers what it heard. | |
| 429 | const path = join(dir, "late"); | |
| 430 | writeFileSync( | |
| 431 | path, | |
| 432 | `#!/bin/sh | |
| 433 | if [ "$1" = "--version" ]; then echo "2.1.0 (fake)"; exit 0; fi | |
| 434 | IFS= read -r first | |
| 435 | echo '{"type":"system","subtype":"init","session_id":"s-late","model":"fake"}' | |
| 436 | IFS= read -r second | |
| 437 | echo '{"type":"assistant","session_id":"s-late","message":{"id":"m1","content":[{"type":"text","text":"Archiving waits on your yes."}],"usage":{"input_tokens":1,"output_tokens":1}}}' | |
| 438 | echo '{"type":"result","subtype":"success","is_error":false,"result":"Archiving waits on your yes.","total_cost_usd":0.001,"num_turns":1,"session_id":"s-late","terminal_reason":"completed","usage":{"input_tokens":1,"output_tokens":1}}' | |
| 439 | echo '{"type":"assistant","session_id":"s-late","message":{"id":"m2","content":[{"type":"text","text":"You said not now, so nothing was archived."}],"usage":{"input_tokens":1,"output_tokens":1}}}' | |
| 440 | echo '{"type":"result","subtype":"success","is_error":false,"result":"You said not now, so nothing was archived.","total_cost_usd":0.001,"num_turns":1,"session_id":"s-late","terminal_reason":"completed","usage":{"input_tokens":1,"output_tokens":1}}' | |
| 441 | `, | |
| 442 | ); | |
| 443 | chmodSync(path, 0o755); | |
| 444 | configureConversation(app, options(path)); | |
| 445 | const c = createConversation(app.db, alice.id); | |
| 446 | if (!c.ok) throw new Error(c.error); | |
| 447 | const said = say(app, options(path), alice.id, "archive late", [], "inject", c.value.id); | |
| 448 | if (!said.ok) throw new Error(said.error); | |
| 449 | expect(await waitFor(() => turnHears(c.value.id))).toBe(true); | |
| 450 | const minted = mintViaToken(app.db, alice.id, `conversation:${c.value.id}`, "go", new Date(Date.now() + 600_000).toISOString()); | |
| 451 | if (!minted.ok) throw new Error(minted.error); | |
| 452 | const held = await mcp(minted.value.token, "archive", { repo: "alice/late", archived: true, dryRun: false }); | |
| 453 | const id = /needs_confirm\\*":\\*"([0-9a-z]+)/.exec(held)?.[1]; | |
| 454 | if (id === undefined) throw new Error(held); | |
| 455 | expect((await answer(alice, id, false)).status).toBe(200); | |
| 456 | await said.value.done; | |
| 457 | const agent = turns(alice, c.value.id).filter((t) => t.role === "agent").map((t) => t.text); | |
| 458 | expect(agent).toEqual(["You said not now, so nothing was archived."]); | |
| 459 | }); | |
| 460 | }); | |
| 461 | ||
| 423 | 462 | describe("the next turn", () => { |
| 424 | 463 | test("opens with answers its session never saw, none it was told, and none twice", () => { |
| 425 | 464 | const c = createConversation(app.db, alice.id); |
new above
offline
One decrement per turn end, however many answers were steered in. Two answers into one turn leave the counter at 1 and the next real reply is dropped. Next: decrement per message heard.