Fix Anthropic empty thinking signature replay

closes #4464
This commit is contained in:
Mario Zechner
2026-05-28 12:02:54 +02:00
parent 3e9f717445
commit 458a7bc27c
10 changed files with 252 additions and 18 deletions

View File

@@ -0,0 +1,88 @@
import { describe, expect, it } from "vitest";
import { streamSimple } from "../src/stream.ts";
import type { AssistantMessage, Context, Model } from "../src/types.ts";
interface AnthropicPayload {
messages?: Array<{
role: string;
content: Array<{ type: string; text?: string; thinking?: string; signature?: string }>;
}>;
}
class PayloadCaptured extends Error {
constructor() {
super("payload captured");
this.name = "PayloadCaptured";
}
}
function makeModel(allowEmptySignature?: boolean): Model<"anthropic-messages"> {
return {
id: "mimo-v2.5-pro",
name: "MiMo-V2.5-Pro",
api: "anthropic-messages",
provider: "xiaomi-token-plan-ams",
baseUrl: "http://127.0.0.1:9/anthropic",
reasoning: true,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 1048576,
maxTokens: 1024,
...(allowEmptySignature === undefined ? {} : { compat: { allowEmptySignature } }),
};
}
function makeContext(thinkingSignature: string): Context {
const assistant: AssistantMessage = {
role: "assistant",
content: [{ type: "thinking", thinking: "internal reasoning", thinkingSignature }],
provider: "xiaomi-token-plan-ams",
api: "anthropic-messages",
model: "mimo-v2.5-pro",
timestamp: Date.now(),
usage: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 0,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
},
stopReason: "stop",
};
return {
messages: [
{ role: "user", content: "first", timestamp: Date.now() },
assistant,
{ role: "user", content: "second", timestamp: Date.now() },
],
};
}
async function capturePayload(model: Model<"anthropic-messages">, context: Context): Promise<AnthropicPayload> {
let capturedPayload: AnthropicPayload | undefined;
const stream = streamSimple(model, context, {
apiKey: "fake-key",
onPayload: (payload) => {
capturedPayload = payload as AnthropicPayload;
throw new PayloadCaptured();
},
});
await stream.result();
if (!capturedPayload) throw new Error("Expected payload capture before request");
return capturedPayload;
}
describe("Anthropic empty thinking signature compat", () => {
it("converts empty-signature thinking to text by default", async () => {
const payload = await capturePayload(makeModel(), makeContext(""));
const assistant = payload.messages?.find((message) => message.role === "assistant");
expect(assistant?.content).toEqual([{ type: "text", text: "internal reasoning" }]);
});
it("preserves empty-signature thinking when allowEmptySignature is enabled", async () => {
const payload = await capturePayload(makeModel(true), makeContext(" "));
const assistant = payload.messages?.find((message) => message.role === "assistant");
expect(assistant?.content).toEqual([{ type: "thinking", thinking: "internal reasoning", signature: "" }]);
});
});

View File

@@ -0,0 +1,114 @@
import { describe, expect, it } from "vitest";
import { completeSimple, getEnvApiKey, streamSimple } from "../src/stream.ts";
import type { AssistantMessage, Context, Model } from "../src/types.ts";
const provider = "xiaomi-token-plan-ams";
const apiKey = getEnvApiKey(provider);
const model: Model<"anthropic-messages"> = {
id: "mimo-v2.5-pro",
name: "MiMo-V2.5-Pro Anthropic smoke",
api: "anthropic-messages",
provider,
baseUrl: "https://token-plan-ams.xiaomimimo.com/anthropic",
reasoning: true,
input: ["text"],
cost: { input: 1, output: 3, cacheRead: 0.2, cacheWrite: 0 },
contextWindow: 1048576,
maxTokens: 1024,
compat: { allowEmptySignature: true },
};
interface AnthropicPayload {
messages?: Array<{
role: string;
content: string | Array<{ type: string; text?: string; thinking?: string; signature?: string }>;
}>;
}
class PayloadCaptured extends Error {
constructor() {
super("payload captured");
this.name = "PayloadCaptured";
}
}
function makeInitialContext(): Context {
return {
systemPrompt: "You are concise. Follow the requested output format exactly.",
messages: [
{
role: "user",
content: "Think internally if you need to, then reply with exactly this text and nothing else: first-ok",
timestamp: Date.now(),
},
],
};
}
function getThinkingBlocks(message: AssistantMessage) {
return message.content.filter((block) => block.type === "thinking");
}
async function captureReplayPayload(context: Context): Promise<AnthropicPayload> {
let capturedPayload: AnthropicPayload | undefined;
const stream = streamSimple(model, context, {
apiKey,
maxTokens: 512,
reasoning: "high",
onPayload: (payload) => {
capturedPayload = payload as AnthropicPayload;
throw new PayloadCaptured();
},
});
await stream.result();
if (!capturedPayload) {
throw new Error("Expected payload capture before request");
}
return capturedPayload;
}
describe.skipIf(!apiKey)("Xiaomi Token Plan AMS Anthropic empty thinking signature smoke", () => {
it("reproduces empty thinking signatures and preserves them for replay", { timeout: 60000, retry: 1 }, async () => {
const firstContext = makeInitialContext();
const first = await completeSimple(model, firstContext, {
apiKey,
maxTokens: 512,
reasoning: "high",
});
expect(first.stopReason, first.errorMessage).toBe("stop");
const thinkingBlocks = getThinkingBlocks(first);
expect(thinkingBlocks.length).toBeGreaterThan(0);
expect(thinkingBlocks.some((block) => block.thinkingSignature === "")).toBe(true);
const replayContext: Context = {
...firstContext,
messages: [
...firstContext.messages,
first,
{
role: "user",
content: "Reply with exactly this text and nothing else: second-ok",
timestamp: Date.now(),
},
],
};
const replayPayload = await captureReplayPayload(replayContext);
const assistantPayload = replayPayload.messages?.find((message) => message.role === "assistant");
expect(assistantPayload).toBeDefined();
expect(Array.isArray(assistantPayload!.content)).toBe(true);
const replayedThinking = (assistantPayload!.content as Array<{ type: string; text?: string }>).filter(
(block) => block.type === "thinking",
);
const replayedText = (assistantPayload!.content as Array<{ type: string; text?: string }>).filter(
(block) => block.type === "text",
);
expect(replayedThinking).toEqual([{ type: "thinking", thinking: thinkingBlocks[0].thinking, signature: "" }]);
expect(replayedText.some((block) => block.text === thinkingBlocks[0].thinking)).toBe(false);
});
});