From 3104bdb8942c500e45bbde1c035ece3d3816a0bb Mon Sep 17 00:00:00 2001 From: deshark Date: Wed, 14 Oct 2026 16:12:38 +0800 Subject: [PATCH 1/3] fix: recognize the dsh title sidecar on the anthropic wire (#3149) --- src/server/side-request.ts | 26 ++++--------- src/util.ts | 48 ++++++++++++++++++++++++ tests/dsh-title-sidecar-handle.test.ts | 52 +++++++++++++++++++++++++- tests/side-lane-truth-table.test.ts | 29 +++++++++++++- 4 files changed, 133 insertions(+), 22 deletions(-) diff --git a/src/server/side-request.ts b/src/server/side-request.ts index 68288b8a34..d3ca6e10a5 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -1,7 +1,7 @@ import type { WireProtocol } from "../util.js"; import { estimateRawBodyTokens } from "../preflight.js"; import { imageTokensInParsedBody } from "../image-tokens.js"; -import { reserveOutputHeadroom, shouldReserveOutputHeadroom } from "../util.js"; +import { reserveOutputHeadroom, shouldReserveOutputHeadroom, systemCarrierTexts } from "../util.js"; import { type ResolvedImageBilling } from "../image-tokens.js"; import { ACP_TOOL_NAMES, ABSORB_TOOL_NAME, IMAGE_FULL_TOOL_NAME, RETRIEVE_TOOL_NAME, RULE_TOOL_NAME } from "acp-kernel"; @@ -102,28 +102,16 @@ export function isSideRequest(parsed: unknown, requestAgent?: string): boolean { // The caller must gate on x-bili-plugin=dsh (evidence-per-client discipline, // cf. dshPersonaFingerprintApplies in src/session-id.ts). export const DSH_TITLE_SYSTEM_PREFIX = "Create a concise title for an AI coding-assistant session"; + +// #3149: on the anthropic wire the title prompt rides the TOP-LEVEL `system` +// field while messages[] holds only the user blob; reading messages[] alone +// (#2503) let the sidecar claim the raw anchor and fork the real conversation +// onto a |sub: ghost. systemCarrierTexts (src/util.ts) reads every wire. export function isDshTitleRequest(parsed: unknown): boolean { if (!parsed || typeof parsed !== "object") return false; const p = parsed as Record; if (Array.isArray(p.tools) && p.tools.length > 0) return false; - const msgs = p.messages; - if (!Array.isArray(msgs)) return false; - for (const m of msgs) { - if (!m || typeof m !== "object" || (m as Record).role !== "system") continue; - const c = (m as Record).content; - let text = ""; - if (typeof c === "string") { - text = c; - } else if (Array.isArray(c)) { - for (const part of c) { - if (part && typeof part === "object" && typeof (part as Record).text === "string") { - text += (part as Record).text as string; - } - } - } - if (text.startsWith(DSH_TITLE_SYSTEM_PREFIX)) return true; - } - return false; + return systemCarrierTexts(p).some((t) => t.startsWith(DSH_TITLE_SYSTEM_PREFIX)); } // #2170 measure 1: the two lane gates extracted from src/server.ts so they have diff --git a/src/util.ts b/src/util.ts index 9585123718..cd307566fd 100644 --- a/src/util.ts +++ b/src/util.ts @@ -491,3 +491,51 @@ export function appendSystemText( export function shouldReserveOutputHeadroom(protocol: string | undefined): boolean { return protocol !== "anthropic"; } + +/** System-carrier texts of a parsed request body, covering all four wire shapes: + * anthropic/openai-hoisted top-level `system` (string or text-block array), + * responses `instructions`, google `systemInstruction.parts`, and system/developer + * entries kept inside `messages[]`/`input[]` by raw clients (identical carrier + * list to estimateWireOverhead, src/server/budget.ts). */ +export function systemCarrierTexts(parsed: Record): string[] { + const out: string[] = []; + const sys = parsed.system; + if (typeof sys === "string") out.push(sys); + else if (Array.isArray(sys)) pushBlockTexts(out, sys); + const inst = parsed.instructions; + if (typeof inst === "string") out.push(inst); + const si = parsed.systemInstruction; + if (typeof si === "object" && si !== null) { + const parts = (si as Record).parts; + if (Array.isArray(parts)) pushBlockTexts(out, parts); + } + for (const key of ["messages", "input"]) { + const arr = parsed[key]; + if (!Array.isArray(arr)) continue; + for (const entry of arr) { + if (typeof entry !== "object" || entry === null) continue; + const rec = entry as Record; + if (rec.role !== "system" && rec.role !== "developer") continue; + const c = rec.content; + if (typeof c === "string") out.push(c); + else if (Array.isArray(c)) pushBlockTexts(out, c); + } + } + return out; +} + +function pushBlockTexts(out: string[], blocks: unknown[]): void { + for (const b of blocks) { + const t = blockText(b); + if (t !== undefined) out.push(t); + } +} + +function blockText(v: unknown): string | undefined { + if (typeof v === "string") return v; + if (typeof v === "object" && v !== null) { + const t = (v as Record).text; + if (typeof t === "string") return t; + } + return undefined; +} diff --git a/tests/dsh-title-sidecar-handle.test.ts b/tests/dsh-title-sidecar-handle.test.ts index 60e5546b1c..ae8c995b8d 100644 --- a/tests/dsh-title-sidecar-handle.test.ts +++ b/tests/dsh-title-sidecar-handle.test.ts @@ -54,7 +54,10 @@ async function harness() { req.on("end", () => { forwarded.push(JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record); res.writeHead(200, { "content-type": "application/json" }); - res.end(JSON.stringify({ id: "chat_test", object: "chat.completion", choices: [{ index: 0, message: { role: "assistant", content: "answer" }, finish_reason: "stop" }], usage: { prompt_tokens: 10000, completion_tokens: 10, total_tokens: 10010 } })); + const anthropicWire = (req.url ?? "").includes("/messages"); + res.end(JSON.stringify(anthropicWire + ? { id: "msg_test", type: "message", role: "assistant", model: "fake-a", content: [{ type: "text", text: "answer" }], stop_reason: "end_turn", usage: { input_tokens: 10000, output_tokens: 10 } } + : { id: "chat_test", object: "chat.completion", choices: [{ index: 0, message: { role: "assistant", content: "answer" }, finish_reason: "stop" }], usage: { prompt_tokens: 10000, completion_tokens: 10, total_tokens: 10010 } })); }); }); server.listen(0, "127.0.0.1", () => resolve({ url: `http://127.0.0.1:${(server.address() as { port: number }).port}`, close: async () => { server.close(); } })); @@ -71,10 +74,16 @@ async function harness() { const response = await fetch(`${origin}/bili/${upstream.url}/v1/chat/completions`, { method: "POST", headers, body: JSON.stringify(body) }); assert.equal(response.status, 200, await response.text()); }; + const dshSendAnthropic = async (conversationId: string, body: Record, agent?: string) => { + const headers: Record = { "content-type": "application/json", "x-acp-session": conversationId, "x-bili-plugin": "dsh", "x-bili-plugin-conversation": conversationId }; + if (agent !== undefined) headers["x-bili-plugin-agent"] = agent; + const response = await fetch(`${origin}/bili/${upstream.url}/v1/messages`, { method: "POST", headers, body: JSON.stringify(body) }); + assert.equal(response.status, 200, await response.text()); + }; const sessionIds = async () => ((await (await fetch(origin + "/__bili/sessions")).json()) as { sessions: { id: string }[] }).sessions.map((s) => s.id); const subSessions = async (prefix: string) => (await sessionIds()).filter((id) => id.startsWith(prefix + "|sub:")); const status = async (conversationId: string) => (await (await fetch(`${origin}/__bili/plugin/status?conversationId=${conversationId}`)).json()) as { contextTokens: number | null; inputTokens: number }; - return { forwarded, dshSend, subSessions, status, origin, close: async () => { store.cancelAll(); proxy.close(); upstream.close(); await new Promise((r2) => setTimeout(r2, 50)); } }; + return { forwarded, dshSend, dshSendAnthropic, subSessions, status, origin, close: async () => { store.cancelAll(); proxy.close(); upstream.close(); await new Promise((r2) => setTimeout(r2, 50)); } }; } test("dsh alpha.2 title sidecar rides the #388 passthrough under the raw key (no |sub: ghost)", async () => { @@ -118,3 +127,42 @@ test("dsh alpha.2 title sidecar rides the #388 passthrough under the raw key (no await h.close(); } }); + +// #3149: on the anthropic wire the title prompt rides the TOP-LEVEL `system` +// field (messages[] holds only the user blob). #2503 read messages[] only, so +// here the sidecar claimed the raw anchor and forked the conversation onto a +// persisted |sub: ghost — one DSH session listed twice in the Web UI. +test("dsh alpha.2 title sidecar on the anthropic wire keeps the raw key — no |sub: ghost (#3149)", async () => { + const h = await harness(); + try { + const history = [ + { role: "user", content: "set up the workspace" }, + { role: "assistant", content: "done" }, + ]; + // 1. title sidecar first, anthropic shape: budget 4096, no tools, no + // agent header, system at the top level (nothing in messages[]). + const t1Body = { model: "fake-a", max_tokens: 4096, system: TITLE_SYSTEM, messages: [{ role: "user", content: `[${JSON.stringify(history)}]` }] }; + await h.dshSendAnthropic("conv-anthropic", t1Body); + assert.deepEqual(h.forwarded.at(-1), t1Body, "title sidecar forwarded verbatim on the anthropic wire"); + assert.deepEqual(await h.subSessions("conv-anthropic"), [], "the anthropic sidecar mints no |sub: ghost"); + const sideSession = resolveConversation("conv-anthropic").session; + assert.equal(sideSession?.stats.requests ?? 0, 0, "the sidecar claims no anchor and runs no kernel request"); + + // 2. first real main turn — real top-level system + tools; it must claim + // the raw key rather than fork behind a title-prompt anchor. + const mainBody = { + model: "fake-a", + max_tokens: 8192, + system: MAIN_SYSTEM, + messages: [...history, { role: "assistant", content: "ok" }, { role: "user", content: "now configure ci" }], + tools: [{ name: "bash", description: "run a command", input_schema: { type: "object", properties: {} } }], + }; + await h.dshSendAnthropic("conv-anthropic", mainBody, "main"); + const session = resolveConversation("conv-anthropic").session; + assert.ok(session, "raw-key session exists"); + assert.equal(session.stats.requests, 1, "the main turn runs the full pipeline exactly once on the raw key"); + assert.deepEqual(await h.subSessions("conv-anthropic"), [], "the main conversation stays on the raw key — no |sub: ghost"); + } finally { + await h.close(); + } +}); diff --git a/tests/side-lane-truth-table.test.ts b/tests/side-lane-truth-table.test.ts index 857f004921..a55347ad47 100644 --- a/tests/side-lane-truth-table.test.ts +++ b/tests/side-lane-truth-table.test.ts @@ -1,6 +1,6 @@ import assert from "node:assert/strict"; import test from "node:test"; -import { demoteGate, isSideRequest, resolveSideLane, SIDE_REQUEST_MAX_TOKENS } from "../src/server/side-request.ts"; +import { demoteGate, DSH_TITLE_SYSTEM_PREFIX, isDshTitleRequest, isSideRequest, resolveSideLane, SIDE_REQUEST_MAX_TOKENS } from "../src/server/side-request.ts"; // #2170 measure 1: exhaustive truth-table pin for the two extracted lane // decision functions. These tables ARE the regression net: any future edit to @@ -122,3 +122,30 @@ test("isSideRequest: dsh auto-review shape over the Responses wire stays off the assert.equal(isSideRequest({ max_output_tokens: 384_000 }, undefined), false); assert.equal(isSideRequest({}, undefined), false); }); + +// #3149: the dsh title sidecar is a LANE input, and every wire carries its +// system text differently (anthropic: TOP-LEVEL system, messages[] holds only +// the user blob — the carrier #2503 never read). The table pins every carrier +// the predicate must read plus the closures; the lane-guard job (#2170) ties it +// to the gate edit. +const TITLE_TEXT = `${DSH_TITLE_SYSTEM_PREFIX} from the supplied human messages.`; +const TITLE_CARRIER_ROWS: [string, Record, boolean][] = [ + ["openai: messages system string", { messages: [{ role: "system", content: TITLE_TEXT }, { role: "user", content: "hi" }] }, true], + ["openai: messages system parts array", { messages: [{ role: "system", content: [{ type: "text", text: TITLE_TEXT }] }] }, true], + ["openai: developer-role carrier", { messages: [{ role: "developer", content: TITLE_TEXT }] }, true], + ["anthropic: top-level system string", { system: TITLE_TEXT, messages: [{ role: "user", content: "[]" }] }, true], + ["anthropic: top-level system text-block array", { system: [{ type: "text", text: TITLE_TEXT }], messages: [{ role: "user", content: "[]" }] }, true], + ["responses: top-level instructions", { instructions: TITLE_TEXT, input: [{ role: "user", content: "hi" }] }, true], + ["responses: input system entry", { input: [{ role: "system", content: TITLE_TEXT }] }, true], + ["google: systemInstruction parts", { systemInstruction: { parts: [{ text: TITLE_TEXT }] }, contents: [{ role: "user", parts: [{ text: "hi" }] }] }, true], + ["anthropic: no title text", { system: "MAIN SYSTEM", messages: [{ role: "user", content: "[]" }] }, false], + ["anthropic: a tool-carrying main turn is vetoed", { tools: [{ name: "bash" }], system: TITLE_TEXT, messages: [{ role: "user", content: "[]" }] }, false], + ["no system carrier at all", { messages: [{ role: "user", content: "hi" }] }, false], +]; + +test("isDshTitleRequest: every wire's system carrier is read (the #3149 anthropic blind spot)", () => { + for (const [name, body, want] of TITLE_CARRIER_ROWS) { + assert.equal(isDshTitleRequest(body), want, name); + } +}); + From 6db54b5f82413cd63e3486c5f52644058f284235 Mon Sep 17 00:00:00 2001 From: awork Date: Wed, 14 Oct 2026 16:49:54 +0800 Subject: [PATCH 2/3] refactor(server): pi-subagent channel consumes the shared systemCarrierTexts (#3153) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review follow-up on the #3153 fix: the PR hoisted the all-wire system-carrier scan into src/util.ts so side-request.ts would not carry a third copy, but the byte-identical private copy in src/server/pi-subagent-channel.ts (#2694) was left behind. Two identical copies of a carrier list whose doc comments both claim parity with estimateWireOverhead is a drift trap — point the channel detector at the shared export instead (logic unchanged, zero behavior delta). --- src/server/pi-subagent-channel.ts | 50 ++----------------------------- 1 file changed, 2 insertions(+), 48 deletions(-) diff --git a/src/server/pi-subagent-channel.ts b/src/server/pi-subagent-channel.ts index 06e7809a8f..2f6d352676 100644 --- a/src/server/pi-subagent-channel.ts +++ b/src/server/pi-subagent-channel.ts @@ -38,6 +38,8 @@ // ({type:"namespace", tools:[...]}) exposing ACP grants are seen, not treated as // absent. +import { systemCarrierTexts } from "../util.js"; + /** Complete child-stamp contract: the producer emits * `` (XML-attr-escaped name) in the tagged * prompt. We require the prefix plus a quoted non-empty name but do NOT require @@ -71,54 +73,6 @@ function signalFromText(text: string): PiSubagentChildSignal { return { present: true, agent: m[1] }; } -function blockText(v: unknown): string | undefined { - if (typeof v === "string") return v; - if (typeof v === "object" && v !== null) { - const t = (v as Record).text; - if (typeof t === "string") return t; - } - return undefined; -} - -function pushBlockTexts(out: string[], blocks: unknown[]): void { - for (const b of blocks) { - const t = blockText(b); - if (t !== undefined) out.push(t); - } -} - -/** System-carrier texts of a parsed request body, covering all four wire shapes: - * anthropic/openai-hoisted top-level `system` (string or text-block array), - * responses `instructions`, google `systemInstruction.parts`, and system/developer - * entries kept inside `messages[]`/`input[]` by raw clients (identical carrier - * list to estimateWireOverhead, src/server/budget.ts). */ -function systemCarrierTexts(parsed: Record): string[] { - const out: string[] = []; - const sys = parsed.system; - if (typeof sys === "string") out.push(sys); - else if (Array.isArray(sys)) pushBlockTexts(out, sys); - const inst = parsed.instructions; - if (typeof inst === "string") out.push(inst); - const si = parsed.systemInstruction; - if (typeof si === "object" && si !== null) { - const parts = (si as Record).parts; - if (Array.isArray(parts)) pushBlockTexts(out, parts); - } - for (const key of ["messages", "input"]) { - const arr = parsed[key]; - if (!Array.isArray(arr)) continue; - for (const entry of arr) { - if (typeof entry !== "object" || entry === null) continue; - const rec = entry as Record; - if (rec.role !== "system" && rec.role !== "developer") continue; - const c = rec.content; - if (typeof c === "string") out.push(c); - else if (Array.isArray(c)) pushBlockTexts(out, c); - } - } - return out; -} - function detectInParsed(parsed: Record): PiSubagentChildSignal { for (const text of systemCarrierTexts(parsed)) { const sig = signalFromText(text); From 2333a48779a31fd271d104d8ca8a5d81585e15e2 Mon Sep 17 00:00:00 2001 From: awork Date: Wed, 14 Oct 2026 16:53:59 +0800 Subject: [PATCH 3/3] test: migrate the retired dsh-title-side-lane pins into the truth table (#3153 review) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR retires tests/dsh-title-side-lane.test.ts (#2503); two of its pins had no replacement — the isSideRequest budget-heuristic boundary around the title body (rc.2 64 caught / alpha.2 4096 missed, the gap the text predicate exists for) and the non-object guard on isDshTitleRequest. Move them next to the new carrier table so the ledger does not shrink with the file. --- tests/side-lane-truth-table.test.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/side-lane-truth-table.test.ts b/tests/side-lane-truth-table.test.ts index a55347ad47..30d0402663 100644 --- a/tests/side-lane-truth-table.test.ts +++ b/tests/side-lane-truth-table.test.ts @@ -149,3 +149,27 @@ test("isDshTitleRequest: every wire's system carrier is read (the #3149 anthropi } }); +// #3153 review: this PR retires tests/dsh-title-side-lane.test.ts (#2503). Its +// remaining pins — the budget-heuristic boundary around the title body and the +// non-object guard — move here so the ledger does not shrink with the file. +function dshTitleBody(maxCompletionTokens: number): Record { + return { + model: "fake-a", + max_completion_tokens: maxCompletionTokens, + messages: [ + { role: "system", content: TITLE_TEXT }, + { role: "user", content: "[{\"role\":\"user\",\"content\":\"hello\"}]" }, + ], + }; +} + +test("isSideRequest: the <=200 budget heuristic still catches rc.2 titles and misses alpha.2 (the gap the text predicate exists for)", () => { + assert.equal(isSideRequest(dshTitleBody(64)), true, "rc.2 64 budget rides the original heuristic"); + assert.equal(isSideRequest(dshTitleBody(4096)), false, "alpha.2 4096 budget defeats the heuristic"); +}); + +test("isDshTitleRequest: non-object bodies stay closed", () => { + assert.equal(isDshTitleRequest(null), false); + assert.equal(isDshTitleRequest("nope"), false); +}); +