Skip to content

Commit 8852e50

Browse files
Wibiaslidge-jun
authored andcommitted
Preserve agent_message boundaries to prevent Anthropic signed-thinking replay failures
1 parent df29afd commit 8852e50

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

‎src/responses/parser.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,34 @@ export function parseRequest(body: unknown): OcxParsedRequest {
278278
continue;
279279
}
280280

281+
if (effectiveType === "agent_message") {
282+
const agentMessage = item as {
283+
author?: string;
284+
recipient?: string;
285+
content?: unknown;
286+
};
287+
288+
const content = inputContentParts(
289+
agentMessage.content as unknown[] | string | undefined,
290+
);
291+
292+
const hasContent =
293+
typeof content === "string"
294+
? content.trim().length > 0
295+
: content.length > 0;
296+
297+
// An agent_message is external input delivered to the parent agent.
298+
// Preserve it as a user-role turn so signed Anthropic thinking blocks
299+
// on either side are never merged into one modified assistant response.
300+
messages.push({
301+
role: "user",
302+
content: hasContent ? content : "(sub-agent message received)",
303+
timestamp: now,
304+
});
305+
306+
continue;
307+
}
308+
281309
if (effectiveType === "message") {
282310
const msg = item as { role?: string; content?: unknown };
283311
switch (msg.role) {
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { parseRequest } from "../src/responses/parser";
3+
4+
describe("Responses parser agent_message boundaries", () => {
5+
test("keeps agent_message between separate assistant reasoning turns", () => {
6+
const parsed = parseRequest({
7+
model: "claude-fable-5",
8+
stream: true,
9+
input: [
10+
{
11+
type: "reasoning",
12+
id: "rs_before_agent",
13+
summary: [
14+
{
15+
type: "summary_text",
16+
text: "reasoning before the sub-agent response",
17+
},
18+
],
19+
encrypted_content: "opaque-signature-before",
20+
},
21+
{
22+
type: "agent_message",
23+
author: "probe_all",
24+
recipient: "root",
25+
content: [
26+
{
27+
type: "input_text",
28+
text: "sub-agent result",
29+
},
30+
],
31+
},
32+
{
33+
type: "reasoning",
34+
id: "rs_after_agent",
35+
summary: [
36+
{
37+
type: "summary_text",
38+
text: "reasoning after the sub-agent response",
39+
},
40+
],
41+
encrypted_content: "opaque-signature-after",
42+
},
43+
{
44+
type: "function_call",
45+
id: "fc_after_agent",
46+
call_id: "call_after_agent",
47+
name: "shell_command",
48+
arguments: JSON.stringify({ command: "echo ok" }),
49+
},
50+
{
51+
type: "function_call_output",
52+
call_id: "call_after_agent",
53+
output: "ok",
54+
},
55+
],
56+
});
57+
58+
const messages = parsed.context.messages;
59+
60+
expect(messages.map((message) => message.role)).toEqual([
61+
"assistant",
62+
"user",
63+
"assistant",
64+
"toolResult",
65+
]);
66+
67+
const firstAssistant = messages[0];
68+
if (!firstAssistant || firstAssistant.role !== "assistant") {
69+
throw new Error("expected the first parsed message to be assistant");
70+
}
71+
72+
expect(firstAssistant.content).toHaveLength(1);
73+
expect(firstAssistant.content[0]).toMatchObject({
74+
type: "thinking",
75+
thinking: "reasoning before the sub-agent response",
76+
itemId: "rs_before_agent",
77+
});
78+
79+
expect(messages[1]).toMatchObject({
80+
role: "user",
81+
content: "sub-agent result",
82+
});
83+
84+
const secondAssistant = messages[2];
85+
if (!secondAssistant || secondAssistant.role !== "assistant") {
86+
throw new Error("expected the third parsed message to be assistant");
87+
}
88+
89+
expect(secondAssistant.content).toHaveLength(2);
90+
expect(secondAssistant.content[0]).toMatchObject({
91+
type: "thinking",
92+
thinking: "reasoning after the sub-agent response",
93+
itemId: "rs_after_agent",
94+
});
95+
expect(secondAssistant.content[1]).toMatchObject({
96+
type: "toolCall",
97+
id: "call_after_agent",
98+
name: "shell_command",
99+
arguments: { command: "echo ok" },
100+
thoughtSignature: "fc_after_agent",
101+
});
102+
103+
expect(messages[3]).toMatchObject({
104+
role: "toolResult",
105+
toolCallId: "call_after_agent",
106+
toolName: "shell_command",
107+
content: "ok",
108+
isError: false,
109+
});
110+
});
111+
});

0 commit comments

Comments
 (0)