fix: preserve signed thinking turns during replay normalization

This commit is contained in:
Shakker
2026-04-12 03:36:54 +01:00
committed by Shakker
parent 0b95510ec5
commit 92f9e09a8e
2 changed files with 43 additions and 0 deletions

View File

@@ -946,6 +946,37 @@ describe("wrapStreamFnSanitizeMalformedToolCalls", () => {
expect(seenContext.messages).toBe(messages);
});
it("preserves signed thinking turns when replayed tool calls would otherwise be sanitized", async () => {
const messages = [
{
role: "assistant",
content: [
{ type: "thinking", thinking: "internal", thinkingSignature: "sig_1" },
{ type: "toolCall", id: "toolu_legacy", name: "gateway", arguments: {} },
],
},
{
role: "user",
content: [{ type: "text", text: "retry" }],
},
];
const baseFn = vi.fn((_model, _context) =>
createFakeStream({ events: [], resultMessage: { role: "assistant", content: [] } }),
);
const wrapped = wrapStreamFnSanitizeMalformedToolCalls(baseFn as never, new Set(["read"]));
const stream = wrapped(
{ api: "anthropic-messages" } as never,
{ messages } as never,
{} as never,
) as FakeWrappedStream | Promise<FakeWrappedStream>;
await Promise.resolve(stream);
expect(baseFn).toHaveBeenCalledTimes(1);
const seenContext = baseFn.mock.calls[0]?.[1] as { messages: unknown[] };
expect(seenContext.messages).toBe(messages);
});
it("preserves sessions_spawn attachment payloads on replay", async () => {
const attachmentContent = "INLINE_ATTACHMENT_PAYLOAD";
const messages = [

View File

@@ -229,6 +229,14 @@ type AnthropicToolResultContentBlock = {
toolUseId?: unknown;
};
function isThinkingLikeReplayBlock(block: unknown): boolean {
if (!block || typeof block !== "object") {
return false;
}
const type = (block as { type?: unknown }).type;
return type === "thinking" || type === "redacted_thinking";
}
function isReplayToolCallBlock(block: unknown): block is ReplayToolCallBlock {
if (!block || typeof block !== "object") {
return false;
@@ -283,6 +291,10 @@ function sanitizeReplayToolCallInputs(
out.push(message);
continue;
}
if (message.content.some((block) => isThinkingLikeReplayBlock(block))) {
out.push(message);
continue;
}
const nextContent: typeof message.content = [];
let messageChanged = false;