From ab88993542a7323749ce2386eaedcc82aa83c8cf Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 17 Nov 2026 00:41:23 +0800 Subject: [PATCH 1/2] fix: read Claude Code's deferred-tools list in lane decisions (#6958) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Claude Code keeps MCP tools out of tools[] until the model loads them with ToolSearch; until then it lists them by name in a message. bili decided both lane decisions from tools[] alone: (1) mcpDialect was true only when tools[] declared mcp__bili__compress, so while the tool was deferred the prompt named bare compress — a tool that does not exist on the host (14 workers hit 'No such tool available: compress', 13 grew to 133K-234K tokens). (2) Claude Code has no host profile, so the anonymous profile's toolsHostScopedByDefault=false kept the compress surface on whatever tools[] held — an agent with no bili tool was nudged 127x up to 440K tokens with no way to fold. The fix reads the deferred list: mcp__bili__compress counts as listed when declared OR deferred, and a Claude Code request (system block carrying cc_version=) with no compress tool declared or listed reads as unreachable. Other hosts keep today's behaviour (the deferred-list header phrase is Claude-Code-specific). Host-side only; no kernel change. Fixes #6958 --- src/server/lane-decisions.ts | 148 +++++++++++++++++++++++++++- tests/claude-deferred-tools.test.ts | 144 +++++++++++++++++++++++++++ 2 files changed, 290 insertions(+), 2 deletions(-) create mode 100644 tests/claude-deferred-tools.test.ts diff --git a/src/server/lane-decisions.ts b/src/server/lane-decisions.ts index 62d52dec23..e4aaeb7089 100644 --- a/src/server/lane-decisions.ts +++ b/src/server/lane-decisions.ts @@ -251,6 +251,128 @@ export function nudgeOn(i: NudgeGateInputs): boolean { && i.modelWantsIt; } +// #6958: Claude Code keeps MCP tools OUT of tools[] until the model loads +// them with ToolSearch; until then it lists them by name in a message +// ("The following deferred tools are now available via ToolSearch. ..."). +// The lane decisions must read that list so a deferred mcp__bili__compress +// counts as LISTED (declared OR deferred — the name alone is enough for the +// model to call it, per the issue repro), and so a Claude Code request whose +// tools[] AND deferred list both lack a compress tool reads as UNREACHABLE +// (the anonymous profile's toolsHostScopedByDefault=false would otherwise +// keep the surface on and nudge agents that have no way to fold). +// +// The header phrase is matched leniently (deferred tools … ToolSearch on one +// line) so a Claude Code wording tweak doesn't silently re-break the gate; +// the tool names are the following non-blank lines (a blank line ends the +// list). A name is a single token — prose lines are ignored. + +/** Collect the system-prompt text from the parsed body, per wire shape. */ +function collectSystemText(parsed: unknown, protocol: LaneProtocol): string { + if (parsed === null || typeof parsed !== "object") return ""; + const body = parsed as Record; + const parts: string[] = []; + if (protocol === "anthropic") { + pushTextParts(parts, body["system"]); + } else if (protocol === "openai") { + const msgs = body["messages"]; + if (Array.isArray(msgs)) { + for (const m of msgs) { + if (m !== null && typeof m === "object" && (m as Record)["role"] === "system") { + pushTextParts(parts, (m as Record)["content"]); + } + } + } + } else if (protocol === "responses") { + pushTextParts(parts, body["instructions"]); + } else if (protocol === "google") { + const si = body["systemInstruction"]; + if (si !== null && typeof si === "object" && !Array.isArray(si)) { + pushTextParts(parts, (si as Record)["parts"]); + } else { + pushTextParts(parts, si); + } + } + return parts.join("\n"); +} + +/** Collect the message text from the parsed body, per wire shape. */ +function collectMessageText(parsed: unknown, protocol: LaneProtocol): string { + if (parsed === null || typeof parsed !== "object") return ""; + const body = parsed as Record; + const parts: string[] = []; + if (protocol === "anthropic" || protocol === "openai") { + const msgs = body["messages"]; + if (Array.isArray(msgs)) { + for (const m of msgs) { + if (m !== null && typeof m === "object") pushTextParts(parts, (m as Record)["content"]); + } + } + } else if (protocol === "responses") { + const input = body["input"]; + if (Array.isArray(input)) { + for (const item of input) { + if (item !== null && typeof item === "object") { + const rec = item as Record; + // Skip non-message items (additional_tools, tool calls). + if (rec["type"] === undefined || rec["type"] === "message") pushTextParts(parts, rec["content"]); + } + } + } + } else if (protocol === "google") { + const contents = body["contents"]; + if (Array.isArray(contents)) { + for (const c of contents) { + if (c !== null && typeof c === "object") { + const ps = (c as Record)["parts"]; + if (Array.isArray(ps)) { + for (const p of ps) { + if (p !== null && typeof p === "object" && typeof (p as Record)["text"] === "string") { + parts.push((p as Record)["text"] as string); + } + } + } + } + } + } + } + return parts.join("\n"); +} + +/** Recursively collect `text` fields from a wire content value (string, + * block array, or single block). */ +function pushTextParts(parts: string[], v: unknown): void { + if (typeof v === "string") { if (v.length > 0) parts.push(v); return; } + if (Array.isArray(v)) { for (const item of v) pushTextParts(parts, item); return; } + if (v !== null && typeof v === "object") { + const rec = v as Record; + if (typeof rec["text"] === "string") parts.push(rec["text"]); + } +} + +/** Parse the deferred-tools list(s) out of body text. Returns the set of + * tool names listed (one per line, a blank line ends the list). */ +function parseDeferredTools(text: string): Set { + const out = new Set(); + if (text.length === 0) return out; + const lines = text.split("\n"); + let i = 0; + while (i < lines.length) { + const line = lines[i]; + if (line.includes("deferred tools") && line.toLowerCase().includes("toolsearch")) { + i++; + while (i < lines.length) { + const name = lines[i].trim(); + if (name === "") break; + // A tool name is a single token; prose lines are ignored. + if (!name.includes(" ")) out.add(name); + i++; + } + } + i++; + } + return out; +} + /** Lane-decision resolver — truth-table pinned across all four wires × lanes * × tool shapes by tests/lane-decisions-matrix.test.ts; a new tool shape or * profile default lands there as a positive+negative row pair (#3617). */ @@ -276,11 +398,30 @@ export function resolveLaneDecisions(input: LaneDecisionInput): LaneDecisions { // was that same single-literal gate going stale when DSH 0.2.x renamed // the transport (12× ARMED-SUPPRESSED at 42% usage, pendingT1 10× over // threshold, fully silent). + // #6958: Claude Code defers MCP tools out of tools[] until the model + // loads them with ToolSearch; until then it lists them by name in a + // message. Read that list so a deferred mcp__bili__compress counts as + // listed (declared OR deferred), and so a Claude Code request (system + // block carrying cc_version=) with no compress tool declared OR listed + // reads as unreachable. Other hosts keep today's behaviour: the deferred + // list is Claude-Code-specific (the header phrase never appears on other + // wires), so their observed sets are unchanged. + const systemText = collectSystemText(input.parsed, input.protocol); + const claudeCode = systemText.includes("cc_version="); + const deferredTools = parseDeferredTools(collectMessageText(input.parsed, input.protocol) + "\n" + systemText); + const mcpCompressListed = declaredTools.has(mcpBiliToolName("compress")) || deferredTools.has(mcpBiliToolName("compress")); + const bareCompressListed = declaredTools.has("compress") || deferredTools.has("compress"); const compressReachable = !input.pluginMode || declaredTools.has("compress") || declaredTools.has(mcpBiliToolName("compress")) + || deferredTools.has("compress") + || deferredTools.has(mcpBiliToolName("compress")) || [...CODE_MODE_OMNIBUS_TOOL_NAMES].some((n) => declaredTools.has(n)) - || !profile.toolsHostScopedByDefault + // #6958: for Claude Code the anonymous profile's + // toolsHostScopedByDefault=false must NOT keep the surface on — reach + // is observed from the declared/deferred tool table above. Other hosts + // keep the profile default (claudeCode false → term unchanged). + || (!claudeCode && !profile.toolsHostScopedByDefault) // #2155: a sticky plugin-bound round with NO lane header is the zombie // shape (plugin gone from the client). The nudge channel doubles as // the self-heal probe — suppressing it here starves zombieStreak and @@ -290,7 +431,10 @@ export function resolveLaneDecisions(input: LaneDecisionInput): LaneDecisions { // (#4344's silent-bridge case always sends the header — unaffected.) || (input.pluginMode && input.pluginHeaderPresent === false); const compressSurfaceOn = input.shouldInject && compressReachable; - const mcpDialect = input.pluginMode && declaredTools.has(mcpBiliToolName("compress")) && !declaredTools.has("compress"); + // #4344/#6958: the standing prompt teaches the model the name it can + // actually call. The MCP dialect applies when mcp__bili__compress is + // listed (declared OR deferred) and the bare name is not. + const mcpDialect = input.pluginMode && mcpCompressListed && !bareCompressListed; const billingBlockObserved = extractBillingAttributionBlock(systemOf(input.parsed)) !== undefined; const billingBlockWanted = profile.wantsBillingBlock || billingBlockObserved; return Object.freeze({ diff --git a/tests/claude-deferred-tools.test.ts b/tests/claude-deferred-tools.test.ts new file mode 100644 index 0000000000..7038b63cd7 --- /dev/null +++ b/tests/claude-deferred-tools.test.ts @@ -0,0 +1,144 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { resolveLaneDecisions } from "../src/server/lane-decisions.ts"; + +// #6958: Claude Code keeps MCP tools OUT of tools[] until the model loads +// them with ToolSearch; until then it lists them by name in a message: +// +// The following deferred tools are now available via ToolSearch. Their +// schemas are NOT loaded — calling them directly will fail with +// InputValidationError. Use ToolSearch with query "select:[,...]" +// to load tool schemas before calling them: +// mcp__bili__compress +// WebFetch +// ... +// +// bili decided both lane decisions from tools[] alone: +// (1) mcpDialect was true only when tools[] declared mcp__bili__compress, +// so while the tool was deferred the prompt named bare `compress` — a +// tool that does not exist on the host (14 workers hit "No such tool +// available: compress", 13 grew to 133K–234K tokens). +// (2) Claude Code has no host profile, so the anonymous profile's +// toolsHostScopedByDefault=false kept the compress surface on whatever +// tools[] held — an agent with no bili tool was nudged 127× up to 440K +// tokens with no way to fold. +// +// The fix reads the deferred list: mcp__bili__compress counts as listed when +// declared OR deferred, and a Claude Code request (system block carrying +// cc_version=) with no compress tool declared or listed reads as unreachable. +// Other hosts keep today's behaviour (the deferred-list header phrase is +// Claude-Code-specific). +// +// These 5 cases build anthropic-wire requests in the shape Claude Code +// 2.1.325 sends. On master, 3 of the 5 fail (cases 1, 2, 3); with the fix +// all 5 pass. + +const BILLING_HEADER = "x-anthropic-billing-header: cc_version=2.1.325.ca9; cc_entrypoint=sdk-cli; cch=00000;"; + +/** The deferred-tools list Claude Code injects into a message: a header line + * ending with ":", then tool names one-per-line, a blank line ends the list. */ +function deferredToolsList(names: string[]): string { + return "The following deferred tools are now available via ToolSearch. Their schemas are NOT loaded — calling them directly will fail with InputValidationError. Use ToolSearch with query \"select:[,...]\" to load tool schemas before calling them:\n" + names.join("\n"); +} + +/** Build an anthropic-wire body in the shape Claude Code 2.1.325 sends: + * billing header in the system block, optional deferred-tools list in the + * first user message, declared tools in tools[]. */ +function ccBody(deferred: string[], declared: string[]): unknown { + const messages: Array<{ role: string; content: string }> = []; + if (deferred.length > 0) messages.push({ role: "user", content: deferredToolsList(deferred) }); + messages.push({ role: "user", content: "hello" }, { role: "assistant", content: "hi" }); + return { + model: "model-test", + system: [ + { type: "text", text: BILLING_HEADER }, + { type: "text", text: "You are Claude Code, Anthropic's official CLI for Claude." }, + ], + messages, + ...(declared.length > 0 ? { tools: declared.map((name) => ({ name, description: "d", input_schema: { type: "object", properties: {} } })) } : {}), + max_tokens: 64_000, + stream: false, + }; +} + +const input = (body: unknown, pluginAgent: string) => ({ + protocol: "anthropic" as const, + pluginMode: true, + pluginAgent, + parsed: body, + shouldInject: true, + pluginHeaderPresent: true, +}); + +interface CaseExpect { + mcpDialect: boolean; + compressSurfaceOn: boolean; +} + +const CASES: Array<{ name: string; body: unknown; agent: string; expect: CaseExpect }> = [ + { + // #6958 bug 1: the tool is deferred (not in tools[]), so the prompt + // must teach the MCP-prefixed name the host actually exposes. + name: "deferred mcp__bili__compress → MCP dialect name in the prompt", + body: ccBody(["mcp__bili__compress", "WebFetch"], []), + agent: "claude", + expect: { mcpDialect: true, compressSurfaceOn: true }, + }, + { + // #6958 bug 2: no compress tool declared or listed → the surface is + // off and the agent is not nudged (it has no way to fold). + name: "no compress tool (declared or deferred) → surface off, no nudge", + body: ccBody([], []), + agent: "claude", + expect: { mcpDialect: false, compressSurfaceOn: false }, + }, + { + // #6958 bug 2: the deferred list holds other tools but not compress + // → same as the no-tool case. + name: "deferred list has other tools but not compress → surface off, no nudge", + body: ccBody(["WebFetch", "WebSearch"], []), + agent: "claude", + expect: { mcpDialect: false, compressSurfaceOn: false }, + }, + { + // Control: the tool is declared in tools[] (not deferred) → the MCP + // dialect applies (passes on master). + name: "declared mcp__bili__compress → MCP dialect name in the prompt", + body: ccBody([], ["mcp__bili__compress"]), + agent: "claude", + expect: { mcpDialect: true, compressSurfaceOn: true }, + }, + { + // Control: the bare name is declared → no MCP dialect (passes on + // master). + name: "declared bare compress → bare name in the prompt", + body: ccBody([], ["compress"]), + agent: "claude", + expect: { mcpDialect: false, compressSurfaceOn: true }, + }, +]; + +for (const c of CASES) { + test(`cc-deferred ${c.name}`, () => { + const d = resolveLaneDecisions(input(c.body, c.agent)); + assert.equal(d.mcpDialect, c.expect.mcpDialect, `mcpDialect (case=${c.name})`); + assert.equal(d.compressSurfaceOn, c.expect.compressSurfaceOn, `compressSurfaceOn (case=${c.name})`); + }); +} + +// Regression guard: a NON-Claude-Code host with no compress tool keeps today's +// behaviour (the anonymous profile's toolsHostScopedByDefault=false keeps the +// surface on). This is the "other hosts keep today's behaviour" guarantee — +// the cc_version= gate must not leak to other wires. +test("cc-deferred non-CC host with no compress tool → surface stays on (unchanged)", () => { + const body = { + model: "model-test", + system: [{ type: "text", text: "You are a coding agent." }], + messages: [{ role: "user", content: "hello" }, { role: "assistant", content: "hi" }], + max_tokens: 64_000, + stream: false, + }; + const d = resolveLaneDecisions(input(body, "pi")); + assert.equal(d.compressSurfaceOn, true, "pi with no compress tool keeps the surface on"); + assert.equal(d.mcpDialect, false, "pi with no compress tool has no MCP dialect"); +}); From 4b3ca0b254ca8505734ca572b213a2b188a1f351 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 17 Nov 2026 01:32:57 +0800 Subject: [PATCH 2/2] ci: retrigger flaky #2929 test shards (token-estimation calibration, #4599)