Repository navigation
fix(claude): preserve final-answer phases across continuations #38562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3833,6 +3833,204 @@ describe("ClaudeAdapterV2 background wake turns", () => { | |
| }); | ||
| const makeWakeHarness = makeWakeHarnessWithOptions(); | ||
|
|
||
| describe("Claude final answer phases", () => { | ||
| it.effect.each([ | ||
| "streamed", | ||
| "snapshot", | ||
| "late snapshot", | ||
| "repeated snapshot", | ||
| "result only", | ||
| ] as const)("preserves confirmed final-answer intent from %s delivery", (delivery) => | ||
| Effect.gen(function* () { | ||
| const harness = yield* makeWakeHarness; | ||
| yield* harness.runtime.startTurn( | ||
| makeClaudeTestTurnInput({ | ||
| threadId: harness.threadId, | ||
| providerThread: harness.providerThread, | ||
| now: yield* DateTime.now, | ||
| attemptId: RunAttemptId.make(`answer-phase-${delivery}`), | ||
| text: "Explain the result, then check your work.", | ||
| attachments: [], | ||
| }), | ||
| ); | ||
| const snapshot = (id: string, value: string, stopReason: string | null, suffix = "") => | ||
| claudeSdkFrame({ | ||
| type: "assistant", | ||
| uuid: `${id}-snapshot${suffix}`, | ||
| session_id: WAKE_NATIVE_SESSION, | ||
| parent_tool_use_id: null, | ||
| message: { | ||
| id, | ||
| model: "claude-sonnet-4-6", | ||
| stop_reason: stopReason, | ||
| content: value ? [{ type: "text", text: value }] : [], | ||
| }, | ||
| }); | ||
| const stream = (event: unknown) => | ||
| claudeSdkFrame({ | ||
| type: "stream_event", | ||
| event, | ||
| parent_tool_use_id: null, | ||
| session_id: WAKE_NATIVE_SESSION, | ||
| uuid: "answer-phase-stream", | ||
| }); | ||
| if (delivery !== "result only") { | ||
| yield* harness.offerAndWait(snapshot("progress", "Checking.", "tool_use")); | ||
| if (delivery === "streamed") { | ||
| yield* harness.offerAndWait( | ||
| stream({ type: "message_start", message: { id: "answer" } }), | ||
| ); | ||
| yield* harness.offerAndWait( | ||
| stream({ | ||
| type: "content_block_start", | ||
| index: 0, | ||
| content_block: { type: "text", text: "" }, | ||
| }), | ||
| ); | ||
| yield* harness.offerAndWait( | ||
| stream({ | ||
| type: "content_block_delta", | ||
| index: 0, | ||
| delta: { type: "text_delta", text: "The complete answer." }, | ||
| }), | ||
| ); | ||
| yield* harness.offerAndWait(stream({ type: "content_block_stop", index: 0 })); | ||
| } | ||
| yield* harness.offerAndWait( | ||
| snapshot("answer", "The complete answer.", delivery === "snapshot" ? "end_turn" : null), | ||
| ); | ||
| if (delivery === "streamed") { | ||
| yield* harness.offerAndWait( | ||
| claudeSdkFrame({ | ||
| type: "stream_event", | ||
| event: { type: "message_delta", delta: { stop_reason: "end_turn" } }, | ||
| parent_tool_use_id: "unrelated-subagent", | ||
| session_id: WAKE_NATIVE_SESSION, | ||
| uuid: "nested-completion", | ||
| api_message_id: "answer", | ||
| }), | ||
| ); | ||
| yield* harness.until(() => | ||
| harness.events.some( | ||
| (event) => | ||
| event.type === "turn_item.updated" && | ||
| event.turnItem.type === "assistant_message" && | ||
| event.turnItem.text === "The complete answer.", | ||
| ), | ||
| ); | ||
| const beforeRootCompletion = harness.events | ||
| .flatMap((event) => | ||
| event.type === "turn_item.updated" && event.turnItem.type === "assistant_message" | ||
| ? [event.turnItem] | ||
| : [], | ||
| ) | ||
| .findLast((item) => item.text === "The complete answer."); | ||
| assert.isDefined(beforeRootCompletion); | ||
| assert.equal(beforeRootCompletion?.phase, undefined); | ||
| yield* harness.offerAndWait( | ||
| stream({ type: "message_start", message: { id: "next-message" } }), | ||
| ); | ||
| yield* harness.offerAndWait( | ||
| claudeSdkFrame({ | ||
| type: "stream_event", | ||
| event: { type: "message_delta", delta: { stop_reason: "end_turn" } }, | ||
| parent_tool_use_id: null, | ||
| session_id: WAKE_NATIVE_SESSION, | ||
| uuid: "late-completion", | ||
| api_message_id: "answer", | ||
| }), | ||
| ); | ||
| } else if (delivery === "late snapshot" || delivery === "repeated snapshot") { | ||
| yield* harness.offerAndWait( | ||
| snapshot("answer", "", "end_turn", delivery === "late snapshot" ? "-metadata" : ""), | ||
| ); | ||
| } | ||
| yield* harness.offerAndWait( | ||
| snapshot("later-answer", "Verification finished.", "end_turn"), | ||
| ); | ||
| } | ||
| yield* harness.offerAndWait( | ||
| makeResultFrame({ | ||
| uuid: "answer-phase-result", | ||
| result: delivery === "result only" ? "The complete answer." : "Verification finished.", | ||
| }), | ||
| ); | ||
| yield* Queue.take(harness.terminalReceipts); | ||
| const latestByText = new Map( | ||
| harness.events.flatMap((event) => | ||
| event.type === "turn_item.updated" && event.turnItem.type === "assistant_message" | ||
| ? [[event.turnItem.text, event.turnItem] as const] | ||
| : [], | ||
| ), | ||
| ); | ||
| assert.equal(latestByText.get("The complete answer.")?.phase, "final_answer"); | ||
| if (delivery !== "result only") { | ||
| assert.equal(latestByText.get("Checking.")?.phase, undefined); | ||
| assert.equal(latestByText.get("Verification finished.")?.phase, "final_answer"); | ||
| } | ||
| }).pipe( | ||
| Effect.scoped, | ||
| Effect.provide( | ||
| Layer.mergeAll( | ||
| IdAllocator.layer, | ||
| McpProviderSessions.layer, | ||
| NodeServices.layer, | ||
| NodeCrypto.layer, | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| it.effect.each(["tool_use", "max_tokens", "stop_sequence", null] as const)( | ||
| "does not confirm a reply that ended with %s", | ||
| (stopReason) => | ||
| Effect.gen(function* () { | ||
| const harness = yield* makeWakeHarness; | ||
| yield* harness.runtime.startTurn( | ||
| makeClaudeTestTurnInput({ | ||
| threadId: harness.threadId, | ||
| providerThread: harness.providerThread, | ||
| now: yield* DateTime.now, | ||
| attemptId: RunAttemptId.make("unconfirmed-answer-phase"), | ||
| text: "Explain the result.", | ||
| attachments: [], | ||
| }), | ||
| ); | ||
| yield* harness.offerAndWait( | ||
| claudeSdkFrame({ | ||
| type: "assistant", | ||
| uuid: "unconfirmed-answer", | ||
| session_id: WAKE_NATIVE_SESSION, | ||
| parent_tool_use_id: null, | ||
| message: { | ||
| id: "unconfirmed-message", | ||
| model: "claude-sonnet-4-6", | ||
| stop_reason: stopReason, | ||
| content: [{ type: "text", text: "Unconfirmed text." }], | ||
| }, | ||
| }), | ||
| ); | ||
| yield* harness.offerAndWait(makeResultFrame({ uuid: "unconfirmed-result", result: "" })); | ||
| yield* Queue.take(harness.terminalReceipts); | ||
| for (const event of harness.events) { | ||
| if (event.type === "turn_item.updated" && event.turnItem.type === "assistant_message") { | ||
| assert.equal(event.turnItem.phase, undefined); | ||
| } | ||
| } | ||
|
Comment on lines
+4015
to
+4019
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Require the unconfirmed text to be emitted. If the adapter drops the snapshot, the empty result still allows the turn to finish. The loop then runs no assertions. In each existing row, assert that an assistant item contains “Unconfirmed text.” before checking that its phase is absent.
|
||
| }).pipe( | ||
| Effect.scoped, | ||
| Effect.provide( | ||
| Layer.mergeAll( | ||
| IdAllocator.layer, | ||
| McpProviderSessions.layer, | ||
| NodeServices.layer, | ||
| NodeCrypto.layer, | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| describe("ClaudeAdapterV2 session title sync", () => { | ||
| type TitleSyncHarness = Effect.Success<ReturnType<typeof makeWakeHarnessWithOptions>>; | ||
| const startTurn = Effect.fnUntraced(function* ( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assert that a repeated snapshot preserves the answer's item ID.
latestByTexthides duplicates. If two item IDs carry “The complete answer.” and the last one is final, this test passes. In the existing repeated-snapshot row, resend the same UUID and text withend_turn. Assert that the answer has one distinct item ID and that its latest update isfinal_answer.As per path instructions, “Extend an existing test or table when it fits, and test at the lowest layer that fails on main without repeating coverage at wrappers or call sites.”
Source: Path instructions