Skip to content

Commit 7527b39

Browse files
committed
fix(server): keep progress updates as Claude text, lone thinking as a Thought
Restores the progress-update promotion, but only for a thinking block that directly follows another one before a tool call. Under summarized display a lone block before a tool call is reasoning in practice.
1 parent 540a3e8 commit 7527b39

2 files changed

Lines changed: 67 additions & 19 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2809,10 +2809,17 @@ describe("ClaudeAdapterV2 background wake turns", () => {
28092809
),
28102810
);
28112811

2812-
// Summarized thinking can't tell a progress update from reasoning, so both stay Thoughts.
2813-
it.effect.each(["claude-opus-5-5", "claude-fable-5-1", "claude-opus-5", "claude-sonnet-4-6"])(
2814-
"keeps %s thinking right before a tool call as reasoning",
2815-
(model) =>
2812+
// Summarized display can't mark a progress update, so only the second of two
2813+
// thinking blocks before a tool call becomes text; a lone block is reasoning.
2814+
it.effect.each([
2815+
{ model: "claude-opus-5-5", thoughts: 2, expected: ["reasoning", "assistant_message"] },
2816+
{ model: "claude-fable-5-1", thoughts: 2, expected: ["reasoning", "assistant_message"] },
2817+
{ model: "claude-opus-5", thoughts: 2, expected: ["reasoning", "reasoning"] },
2818+
{ model: "claude-sonnet-4-6", thoughts: 2, expected: ["reasoning", "reasoning"] },
2819+
{ model: "claude-opus-5-5", thoughts: 1, expected: ["reasoning"] },
2820+
] as const)(
2821+
"shows $model thinking before a tool call as $expected",
2822+
({ model, thoughts, expected }) =>
28162823
Effect.gen(function* () {
28172824
const harness = yield* makeWakeHarness;
28182825
yield* harness.runtime.startTurn(
@@ -2833,17 +2840,16 @@ describe("ClaudeAdapterV2 background wake turns", () => {
28332840
parent_tool_use_id: null,
28342841
message: { id: "reply-message", model, content },
28352842
});
2843+
const texts = ["Planning the check", "Found it: the memory file is the cause."].slice(
2844+
0,
2845+
thoughts,
2846+
);
28362847
const frames = [
2837-
snapshot("reasoning-snapshot", [
2838-
{ type: "thinking", thinking: "Planning the check", signature: "sig" },
2839-
]),
2840-
snapshot("reply-snapshot", [
2841-
{
2842-
type: "thinking",
2843-
thinking: "Found it: the memory file is the cause.",
2844-
signature: "sig",
2845-
},
2846-
]),
2848+
...texts.map((thinking, index) =>
2849+
snapshot(`thinking-snapshot-${index}`, [
2850+
{ type: "thinking", thinking, signature: "sig" },
2851+
]),
2852+
),
28472853
snapshot("tool-snapshot", [
28482854
{ type: "tool_use", id: "tool-1", name: "Bash", input: { command: "ls" } },
28492855
]),
@@ -2861,10 +2867,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
28612867
);
28622868
assert.deepEqual(
28632869
[...latest.values()].map((item) => [item.type, item.text]),
2864-
[
2865-
["reasoning", "Planning the check"],
2866-
["reasoning", "Found it: the memory file is the cause."],
2867-
],
2870+
texts.map((text, index) => [expected[index], text]),
28682871
);
28692872
}).pipe(
28702873
Effect.scoped,

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,17 @@ function textFromClaudeContent(content: SDKAssistantMessage["message"]["content"
11651165
return content.flatMap((part) => (part.type === "text" ? [part.text] : [])).join("");
11661166
}
11671167

1168+
// Claude 5.1 and later return the text written ahead of a tool call as a
1169+
// thinking block, at most one per call, instead of a text block. Summarized
1170+
// display makes it look like reasoning, so only the second of two thinking
1171+
// blocks right before a tool call is treated as that text.
1172+
function claudeWritesUpdatesAsThinking(model: string): boolean {
1173+
const match = /claude-[a-z]+-(\d+)(?:-(\d{1,2}))?(?!\d)/u.exec(model);
1174+
if (match === null) return false;
1175+
const major = Number(match[1]);
1176+
return major > 5 || (major === 5 && Number(match[2] ?? 0) >= 1);
1177+
}
1178+
11681179
// In SDK mode Claude reports `/goal` only through the transcript (its
11691180
// `active_goal` event is remote-only): synthetic command output names the
11701181
// goal, and each unmet evaluator check returns as Stop hook feedback.
@@ -2906,6 +2917,13 @@ interface ActiveClaudeTurnContext {
29062917
readonly nextBlockIndex: Map<string, number>;
29072918
readonly snapshotBlockIndex: Map<string, number>;
29082919
readonly snapshots: Set<string>;
2920+
// The latest snapshot block, when it was non-empty thinking.
2921+
lastThinking: {
2922+
readonly messageId: string;
2923+
readonly itemId: string;
2924+
readonly text: string;
2925+
readonly followsThinking: boolean;
2926+
} | null;
29092927
readonly blocks: Map<
29102928
string,
29112929
{ readonly startedAt: DateTime.Utc; readonly ordinal: number; text: string }
@@ -6847,7 +6865,24 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
68476865
// The SDK emits one assistant snapshot per completed content block.
68486866
// Count thinking blocks separately so snapshots share the streamed ID.
68496867
for (const block of message.message.content) {
6850-
if (block.type !== "thinking") continue;
6868+
if (block.type !== "thinking") {
6869+
const update = context.reasoning.lastThinking;
6870+
context.reasoning.lastThinking = null;
6871+
if (
6872+
block.type === "tool_use" &&
6873+
update?.messageId === message.message.id &&
6874+
update.followsThinking &&
6875+
claudeWritesUpdatesAsThinking(message.message.model)
6876+
) {
6877+
// Same item ID, so the update replaces the Thought in place.
6878+
yield* emitAssistantTextArtifacts({
6879+
context,
6880+
nativeItemId: update.itemId,
6881+
text: update.text,
6882+
});
6883+
}
6884+
continue;
6885+
}
68516886
const index = context.reasoning.snapshotBlockIndex.get(message.message.id) ?? 0;
68526887
context.reasoning.snapshotBlockIndex.set(message.message.id, index + 1);
68536888
const itemId = `${message.message.id}:thinking:${index}`;
@@ -6859,6 +6894,15 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
68596894
...(finalText ? { finalText } : {}),
68606895
emitEmpty: false,
68616896
});
6897+
const previous = context.reasoning.lastThinking;
6898+
context.reasoning.lastThinking = finalText
6899+
? {
6900+
messageId: message.message.id,
6901+
itemId,
6902+
text: finalText,
6903+
followsThinking: previous?.messageId === message.message.id,
6904+
}
6905+
: null;
68626906
}
68636907
}
68646908

@@ -8553,6 +8597,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
85538597
streamBlocks: new Map(),
85548598
nextBlockIndex: new Map(),
85558599
snapshotBlockIndex: new Map(),
8600+
lastThinking: null,
85568601
snapshots: new Set(),
85578602
blocks: new Map(),
85588603
},

0 commit comments

Comments
 (0)