Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

6.8 KB
Raw Blame
/**
 * TDD regression for #6953 — thinking blocks with empty signatures poison the
 * Anthropic leg of combo/blend routes.
 *
 * Non-Anthropic providers (codex/gpt-5.x) synthesize Anthropic-format `thinking`
 * blocks with `signature: ""`. When the client replays these in the next
 * request's history, the Anthropic leg rejects them with HTTP 400 "Invalid
 * signature in thinking block", and the router silently falls back to codex
 * permanently.
 *
 * The old code fabricated a DEFAULT_THINKING_CLAUDE_SIGNATURE to fill the empty
 * signature — but that fabricated signature is equally foreign to Anthropic, so
 * it also 400'd.
 *
 * Fix (#6953): strip thinking blocks with empty/missing signatures entirely.
 * They carry no replayable cryptographic value. For `redacted_thinking`, strip
 * if `data` is empty/missing for the same reason.
 */
import test from "node:test";
import assert from "node:assert/strict";

const { openaiToClaudeRequest } =
  await import("../../open-sse/translator/request/openai-to-claude.ts");

test('#6953: thinking block with signature:"" is stripped, not fabricated', () => {
  const result = openaiToClaudeRequest(
    "claude-opus-4-8",
    {
      messages: [
        { role: "user", content: "hello" },
        {
          role: "assistant",
          content: [
            { type: "text", text: "I will help you." },
            { type: "thinking", thinking: "reasoning here", signature: "" },
            {
              type: "text",
              text: "Let me use a tool.",
            },
          ],
        },
        { role: "user", content: "ok go ahead" },
      ],
    },
    false
  );

  const assistant = result.messages.find((m) => m.role === "assistant");
  assert.ok(assistant, "expected assistant message");

  // The thinking block with empty signature must be DROPPED, not preserved
  // with a fabricated signature.
  const thinkingBlocks = assistant.content.filter((b) => b && b.type === "thinking");
  assert.equal(
    thinkingBlocks.length,
    0,
    "thinking block with empty signature must be stripped, not fabricated"
  );

  // Text blocks must survive
  const textBlocks = assistant.content.filter((b) => b && b.type === "text");
  assert.ok(textBlocks.length >= 1, "text blocks must be preserved");
});

test("#6953: thinking block with valid signature is preserved verbatim", () => {
  const realSig = "EuY2xhdWRlLXNpZ25hdHVyZS0xNzA5...";
  const result = openaiToClaudeRequest(
    "claude-opus-4-8",
    {
      messages: [
        { role: "user", content: "hello" },
        {
          role: "assistant",
          content: [
            { type: "thinking", thinking: "real reasoning", signature: realSig },
            { type: "text", text: "response" },
          ],
        },
        { role: "user", content: "ok" },
      ],
    },
    false
  );

  const assistant = result.messages.find((m) => m.role === "assistant");
  assert.ok(assistant);

  const thinkingBlocks = assistant.content.filter((b) => b && b.type === "thinking");
  assert.equal(thinkingBlocks.length, 1, "valid thinking block must be preserved");
  assert.equal(thinkingBlocks[0].signature, realSig, "valid signature must be preserved verbatim");
});

test("#6953/#12105: thinking block with undefined signature is stripped like the empty-string case", () => {
  // A thinking block without a signature field is what the response translator emits for
  // cross-provider reasoning_content (#12105). It carries no replayable signature either, so
  // it must be dropped rather than stamped with the fabricated default — Anthropic rejects
  // that fabricated signature with HTTP 400 exactly like the empty-string case.
  const result = openaiToClaudeRequest(
    "claude-opus-4-8",
    {
      messages: [
        { role: "user", content: "hello" },
        {
          role: "assistant",
          content: [
            { type: "thinking", thinking: "I already have this" },
            { type: "text", text: "response" },
          ],
        },
        { role: "user", content: "ok" },
      ],
    },
    false
  );

  const assistant = result.messages.find((m) => m.role === "assistant");
  assert.ok(assistant);

  const thinkingBlocks = assistant.content.filter((b) => b && b.type === "thinking");
  assert.equal(
    thinkingBlocks.length,
    0,
    "thinking block with undefined signature must be stripped, not fabricated"
  );
  const textBlocks = assistant.content.filter((b) => b && b.type === "text");
  assert.equal(textBlocks.length, 1, "text block must be preserved");
});

test("#6953: redacted_thinking with empty data is stripped", () => {
  const result = openaiToClaudeRequest(
    "claude-opus-4-8",
    {
      messages: [
        { role: "user", content: "hello" },
        {
          role: "assistant",
          content: [
            { type: "redacted_thinking", data: "" },
            { type: "text", text: "response" },
          ],
        },
        { role: "user", content: "ok" },
      ],
    },
    false
  );

  const assistant = result.messages.find((m) => m.role === "assistant");
  assert.ok(assistant);

  const redactedBlocks = assistant.content.filter((b) => b && b.type === "redacted_thinking");
  assert.equal(redactedBlocks.length, 0, "redacted_thinking with empty data must be stripped");
});

test("#6953: combo scenario — codex-sourced thinking block does not block Anthropic leg", () => {
  // Simulates a combo route: turn 1 served by codex produced a thinking block
  // with signature:"". Turn 2 should be able to route to Anthropic without
  // the poisoned block causing a 400.
  const result = openaiToClaudeRequest(
    "claude-opus-4-8",
    {
      messages: [
        { role: "user", content: "write a function" },
        {
          role: "assistant",
          content: [
            {
              type: "thinking",
              thinking: "**Reviewing the request**\n\nI need to write a function...",
              signature: "", // codex-sourced, no real signature
            },
            { type: "text", text: "Here's the function:" },
            {
              type: "tool_use",
              id: "toolu_01abc",
              name: "write_file",
              input: { path: "main.rs", content: "fn main() {}" },
            },
          ],
        },
        { role: "user", content: "looks good, now add tests" },
      ],
    },
    false
  );

  const assistant = result.messages.find((m) => m.role === "assistant");
  assert.ok(assistant);

  // No thinking block with empty-string signature should survive
  const badThinking = assistant.content.find(
    (b) => b && b.type === "thinking" && b.signature === ""
  );
  assert.equal(
    badThinking,
    undefined,
    "no thinking block with empty-string signature should survive"
  );

  // Tool use must survive
  const toolUse = assistant.content.find((b) => b && b.type === "tool_use");
  assert.ok(toolUse, "tool_use block must be preserved");
});