Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,14 @@ const ACP_TAG_MARK = "\x3cacp ";
// nonexistent (preflight), so acp_summary survives as the carrier and
// systemToUser later re-voices the survivors as USER messages (leaving them at
// their anchors) for strict backends (#377).
/** True for a signed Anthropic thinking-family message: a reasoning message
* with a signature, or a verbatim redacted_thinking block sidecar. */
function hasSignedThinkingBlock(m: BiliMessage): boolean {
if (m.contentType === "reasoning") return typeof m.thinkingSignature === "string" && m.thinkingSignature.length > 0;
const raw = m.rawAnthropicBlock as { type?: unknown } | undefined;
return raw?.type === "redacted_thinking";
}

/** [#3222] Compute #651's drop set BEFORE core.processTurn runs: the kernel's
* hide-compress-calls node removes inactive-block call+result pairs INSIDE
* processTurn, so a pass computed on the post-turn view has lost its anchors
Expand All @@ -1614,7 +1622,12 @@ export function planReasoningDrop(
// any signed thinking in view disables the drop for this request.
// Presence-based and per-request: Claude sessions without extended
// thinking keep #651's savings.
if (msgs.some((m) => m.contentType === "reasoning" && typeof m.thinkingSignature === "string" && m.thinkingSignature.length > 0)) return new Set();
// [#1960] redacted_thinking blocks are signed too, but the kernel codec
// carries them as a `text` message whose rawAnthropicBlock is the verbatim
// block (never contentType "reasoning"), so they must arm the gate as well:
// dropping a sibling reasoning run rewrites the assistant message that
// holds them -> "thinking or redacted_thinking blocks ... cannot be modified".
if (msgs.some(hasSignedThinkingBlock)) return new Set();
return dropCompressReasoningIds(msgs, reasoning);
}

Expand Down
29 changes: 29 additions & 0 deletions tests/reasoning-echo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,35 @@ describe("#1658 anthropic signed-thinking gate (#651 drop vs #684 pair invariant
assert.equal(c.lines.length, 1);
});

it("redacted_thinking in view disables the #651 drop (#1960)", () => {
// The redacted carrier must sit BEFORE the reasoning run: coreToAnthropic merges same-role messages into one assistant message, so dropping the run rewrites the message carrying the redacted block. Placed after the run it would break contiguity and the drop never fires at all (test would pass without the fix).
const redacted: BiliMessage = { id: "rd", role: "assistant", contentType: "text", text: "[redacted_thinking]", rawAnthropicBlock: { type: "redacted_thinking", data: "ZGF0YQ==" } };
const msgs: BiliMessage[] = [redacted, { id: "r", role: "assistant", contentType: "reasoning", text: "x".repeat(3000) }, CALL(), RESULT(), USER()];
const c = collector();
assert.equal(withReasoningDrop(msgs, undefined, c.log, "s1", false), msgs);
assert.equal(c.lines.length, 0);
});

const reproInboundRedacted: AnthropicMessage[] = [
{ role: "user", content: "q" },
{ role: "assistant", content: [{ type: "redacted_thinking", data: "ZGF0YQ==" }, { type: "thinking", thinking: "x".repeat(3000) }, { type: "tool_use", id: "tu1", name: "compress", input: {} }] },
{ role: "user", content: [{ type: "tool_result", tool_use_id: "tu1", content: "ok" }] },
{ role: "user", content: "next" },
];

it("regression: unsigned thinking beside a redacted block no longer drops (#1960 wire shape)", () => {
const { msgs } = anthropicToCore({ model: "claude-opus-4-5", messages: reproInboundRedacted });
assert.ok(msgs.some((m) => m.rawAnthropicBlock !== undefined && (m.rawAnthropicBlock as { type?: string }).type === "redacted_thinking"));
const c = collector();
const out = withReasoningDrop(msgs, undefined, c.log, "s1", false);
assert.equal(out, msgs);
assert.equal(c.lines.length, 0);
const outbound = coreToAnthropic(out);
const assistantOut = outbound[1]!;
assert.ok(Array.isArray(assistantOut.content));
assert.equal(assistantOut.content.length, 3);
});

it("empty-string signature does not arm the gate", () => {
const msgs: BiliMessage[] = [{ id: "r", role: "assistant", contentType: "reasoning", text: "x".repeat(3000), thinkingSignature: "" }, CALL(), RESULT(), USER()];
const c = collector();
Expand Down
Loading