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);
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..30d0402663 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,54 @@ 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);
+ }
+});
+
+// #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);
+});
+