From 5890a1e4b5d4d80ec5793450359cce8e4354179e Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 5 Oct 2026 15:11:04 +0800 Subject: [PATCH 1/2] fix: side requests must not claim the persona namespace anchor (#2156) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit subagentNamespace anchors the raw key to the FIRST system seen under an identity, but identity resolution ran BEFORE the #388 side-passthrough short-circuit — so a dsh side request arriving first stole the anchor and forked every real main turn onto |sub:, leaving the host-stamped bare id with zero refs and compress failing permanently. Side requests now resolve verbatim: they neither read nor write the anchor (they never touch kernel state). Covers both facets of one mechanism: side-first anchor theft, and post-anchor side requests riding junk |sub: sessions instead of the main key. Same fix lands on the proxy-mode Responses side shape (instructions-fingerprint branch) which had the identical race. --- src/server.ts | 45 ++++++++-- src/server/side-request.ts | 13 ++- tests/dsh-persona-fingerprint.test.ts | 120 ++++++++++++++++++++++++++ 3 files changed, 168 insertions(+), 10 deletions(-) diff --git a/src/server.ts b/src/server.ts index 7c66dd5637..715fde22af 100644 --- a/src/server.ts +++ b/src/server.ts @@ -152,7 +152,7 @@ import { buildForwardHeaders, connectionNamedHeaders, NO_IDENTITY_MESSAGE, RESPO import { installWebSocketBridge } from "./ws-bridge.js"; import { codexResponsesCodec, responsesCodec } from "./responses-ws.js"; import { currentFetchTransport } from "./fetch-transport.js"; -import { isSideRequest, outputBudgetField, restoreOutputBudget, SIDE_REQUEST_MAX_TOKENS, sideRequestGuard, stripLeakedBiliTools } from "./server/side-request.js"; +import { hasLeakedBiliToolsOnly, isSideRequest, outputBudgetField, restoreOutputBudget, SIDE_REQUEST_MAX_TOKENS, sideRequestGuard, stripLeakedBiliTools } from "./server/side-request.js"; import { dshCompactionRefusal, isDshCompactionCall } from "./server/dsh-compaction-guard.js"; import { clampOutgoingOutput, countSystemAndToolsTokens, emergencyNudge, estimateInputTokens, estimateWireOverhead, projectThinkingMass } from "./server/budget.js"; import { awaitDrain, bufferToStream, dumpStreamToFile, pipeThrough, readStreamToBuffer } from "./server/stream-io.js"; @@ -2172,6 +2172,33 @@ async function handle( clientProvided: !!convHeader, } : undefined; + // #2156: a request that will ride the #388 side passthrough further + // down must NEVER participate in persona namespace resolution. + // subagentNamespace's first-seen-system anchor is mutable process + // state: whichever request arrives first under an identity claims the + // raw key. A side request carries kernel-no-touch status, yet anchoring + // it lets its utility system claim the raw key and fork every real + // main turn onto `|sub:` — the host-stamped bare id then finds + // zero refs and compress fails permanently (this issue). Side requests + // therefore resolve VERBATIM: they neither read nor write the anchor + // and share the main session key per the #388 contract (before the + // fix, once any main turn had anchored, they instead rode junk + // `|sub:` sessions). Mirrors the #388 lane condition below + // minus publicForkPrefix, which needs the session resolved AFTER this + // point: a public-fork child arrives under a FRESH childConversationId + // where first-seen anchoring returns the raw key anyway, so omitting + // the veto cannot move any key. detectAcpArtifacts stays last — it + // re-encodes the whole history and must only run for the all-bili + // subset (same short-circuit discipline as the demotedSide gate below; + // proxy-mode traffic never reaches it because the plugin header gate + // fails first). + const sideAgent = pluginRequestAgentHeader(req.headers); + const sideRequestLike = !countTokens && !responsesCompact && protocol !== null + && (isSideRequest(parsed, sideAgent) + || (pluginAgentHeader(req.headers) !== undefined && sideAgent !== "main" + && req.headers["x-bili-ws-lane"] === undefined + && hasLeakedBiliToolsOnly(parsed) + && detectAcpArtifacts(bodyBuffer, parsed) === null)); // #1916/#1307/#1314: the dsh persona fingerprint — dsh stamps ONE // conversation id on every model request of a session, INCLUDING the // auto-review classifyRisk() calls (fixed REVIEW_POLICY system + a @@ -2206,11 +2233,11 @@ async function handle( // across main and subagent sessions. (claudeSub !== undefined && opts.subagentSplit !== false ? claudeSubagentSplit(anthropicIdentity?.value ?? anthropicSignal, req.headers, systemTextsForSplit) - : dshPersona + : dshPersona && !sideRequestLike ? subagentNamespace(anthropicIdentity?.value ?? anthropicSignal, personaSystemText) : anthropicIdentity?.value ?? anthropicSignal) : protocol === "openai" - ? (dshPersona + ? (dshPersona && !sideRequestLike ? subagentNamespace(openaiIdentity?.value ?? openaiSignal, personaSystemText) : openaiIdentity?.value ?? openaiSignal) : codexTurn @@ -2241,11 +2268,13 @@ async function handle( // conversation evolved (upgrade / plugin / AGENTS.md), // not a new persona. See instructionsFingerprintApplies // in src/session-id.ts. - ? subagentNamespace( - responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader), - (parsed as ResponsesRequestBody).instructions, - ) - : (responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader)); + ? (!sideRequestLike + ? subagentNamespace( + responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader), + (parsed as ResponsesRequestBody).instructions, + ) + : (responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader))) + : (responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader)); // #1916/#1307: true when the dsh persona fingerprint actually split // this request onto a suffixed session key (kernel anchor mismatch). // Used by the recordPluginSession branch below so the fork records diff --git a/src/server/side-request.ts b/src/server/side-request.ts index c7381419b6..9f6333b605 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -92,7 +92,11 @@ function biliToolNamesIn(parsed: Record): string[] | null { * starved tool-carrying request is the death-spiral rescue path — * restoreOutputBudget must run so the model regains the output room to emit * compress — so such requests stay main turns even when every tool is bili's. */ -export function stripLeakedBiliTools(parsed: unknown): boolean { +/** #2156: READ-ONLY structural twin of stripLeakedBiliTools — same verdict, + * no mutation. Lets callers (the persona-anchor bypass in server.ts) ask + * "would this request be demoted via #1897?" before any decision point that + * must not pay the mutation or re-run the check. */ +export function hasLeakedBiliToolsOnly(parsed: unknown): boolean { if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return false; const p = parsed as Record; const names = biliToolNamesIn(p); @@ -102,7 +106,12 @@ export function stripLeakedBiliTools(parsed: unknown): boolean { const raw = readOutputBudget(p, field); if (typeof raw === "number" && raw > 0 && raw <= SIDE_REQUEST_MAX_TOKENS) return false; } - delete p.tools; + return true; +} + +export function stripLeakedBiliTools(parsed: unknown): boolean { + if (!hasLeakedBiliToolsOnly(parsed)) return false; + delete (parsed as Record).tools; return true; } diff --git a/tests/dsh-persona-fingerprint.test.ts b/tests/dsh-persona-fingerprint.test.ts index 4471e50269..3dbaa37490 100644 --- a/tests/dsh-persona-fingerprint.test.ts +++ b/tests/dsh-persona-fingerprint.test.ts @@ -31,6 +31,7 @@ process.env.BILI_PERSIST = "0"; const MODEL = "claude-sonnet-4-5"; const MAIN_SYSTEM = "You are DeepSeek Harness, the main coding agent.\nWorkspace: /tmp."; const REVIEW_SYSTEM = "You are the final authorization reviewer for exactly one pending tool call.\nAnswer with one word."; +const SIDE_SYSTEM = "You are a title generator. Write one short title for the conversation."; const forkKey = (conv: string, system: string): string => `${conv}|sub:${createHash("sha256").update(system, "utf8").digest("hex").slice(0, 16)}`; @@ -248,6 +249,125 @@ test("e2e anthropic lane: the same persona fork applies to dsh-over-anthropic", } }); +// #2156: a side request (title-gen shape: tiny budget, no tools, its own +// utility system) arriving FIRST under the conversation id used to claim the +// persona anchor — the kernel's first-seen-system rule anchored its system on +// the raw key, so every real main turn forked onto `|sub:` and the +// host-stamped bare id found zero refs: compress failed permanently. Side +// requests must resolve verbatim: they never touch kernel state (#388), so +// they must neither read nor write the anchor. A later facet of the same +// mechanism: once any main turn had anchored, side requests rode junk +// `|sub:` sessions instead of sharing the main key. +test("e2e openai lane #2156: side request FIRST must not steal the persona anchor", async () => { + const rig = await startRig(); + const CONV = "dshp-sidefirst"; + try { + const url = `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/chat/completions`; + const headers: Record = { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": CONV }; + const tools = [{ type: "function", function: { name: "bash", description: "run", parameters: { type: "object", properties: {} } } }]; + const side = () => ({ model: MODEL, max_tokens: 100, messages: [{ role: "system", content: SIDE_SYSTEM }, { role: "user", content: "Summarize this conversation in one short title." }] }); + + // Phase 1 — the side request arrives BEFORE any main turn (the race). + const r1 = await fetch(url, { method: "POST", headers, body: JSON.stringify(side()) }); + assert.equal(r1.status, 200); + await r1.text(); + const s1 = peekSession(CONV); + assert.ok(s1, "side request resolved onto the raw conversation key (#388: side requests share the main key)"); + assert.equal(Object.keys(s1.state.messageRefs.byRaw).length, 0, "side passthrough touches no kernel state"); + assert.equal(s1.stats.requests, 0, "side passthrough counts no requests"); + assert.ok(!peekSession(forkKey(CONV, SIDE_SYSTEM)), "no junk |sub: session"); + + // Phase 2 — the FIRST main turn keeps the raw key even though a side + // request arrived before it (the anchor-steal repro). + const r2 = await fetch(url, { + method: "POST", + headers, + body: JSON.stringify({ model: MODEL, max_tokens: 64_000, tools, messages: [{ role: "system", content: MAIN_SYSTEM }, ...mainMsgs(12)] }), + }); + assert.equal(r2.status, 200); + await r2.text(); + const main = peekSession(CONV); + assert.ok(main, "main session exists under the RAW key"); + assert.ok(Object.keys(main.state.messageRefs.byRaw).length >= 12, "refs assigned under the bare id — compress with the host-stamped id works"); + assert.ok(!peekSession(forkKey(CONV, MAIN_SYSTEM)), "main turn did NOT fork onto |sub:"); + assert.equal(main.stats.requests, 1, "exactly the main turn counted under the raw key"); + + // Phase 3 — a LATER side request still rides the raw key (second facet: + // pre-fix it minted a junk |sub: session once anchored). + const r3 = await fetch(url, { method: "POST", headers, body: JSON.stringify(side()) }); + assert.equal(r3.status, 200); + await r3.text(); + assert.ok(!peekSession(forkKey(CONV, SIDE_SYSTEM)), "post-anchor side request did not mint a junk forked session"); + assert.equal(peekSession(CONV)?.stats.requests, 1, "side request counted nowhere"); + + // Phase 4 — review persona still forks (the #1916 mechanism this fix + // must not disturb): full-pipeline request, own system, own fork. + const r4 = await fetch(url, { + method: "POST", + headers, + body: JSON.stringify({ + model: MODEL, + max_tokens: 384_000, + messages: [ + { role: "system", content: REVIEW_SYSTEM }, + { role: "user", content: "Conversation transcript (flattened):\nuser: build it\nassistant: ok\nDecide: risky?" }, + ], + }), + }); + assert.equal(r4.status, 200); + await r4.text(); + const fork = peekSession(forkKey(CONV, REVIEW_SYSTEM)); + assert.ok(fork, "review persona still forks onto |sub: after the fix"); + assert.equal(peekSession(CONV)?.stats.requests, 1, "main session untouched by the review"); + } finally { + await closeRig(rig); + } +}); + +test("e2e anthropic lane #2156: the same anchor-steal race on dsh-over-anthropic", async () => { + const rig = await startRig(); + const CONV = "dshp-sidefirst-anthropic"; + try { + const url = `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/messages`; + const headers: Record = { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": CONV }; + const side = () => ({ + model: MODEL, + max_tokens: 100, + system: [{ type: "text", text: SIDE_SYSTEM }], + messages: [{ role: "user", content: [{ type: "text", text: "Summarize this conversation in one short title." }] }], + }); + + const r1 = await fetch(url, { method: "POST", headers, body: JSON.stringify(side()) }); + assert.equal(r1.status, 200); + await r1.text(); + const s1 = peekSession(CONV); + assert.ok(s1, "side request resolved onto the raw conversation key"); + assert.equal(Object.keys(s1.state.messageRefs.byRaw).length, 0, "side passthrough touches no kernel state"); + + const r2 = await fetch(url, { + method: "POST", + headers, + body: JSON.stringify({ + model: MODEL, + max_tokens: 64_000, + system: [{ type: "text", text: MAIN_SYSTEM }], + messages: Array.from({ length: 12 }, (_, i): { role: string; content: { type: string; text: string }[] } => ({ + role: i % 2 === 0 ? "user" : "assistant", + content: [{ type: "text", text: `main-${i + 1}-` + "z".repeat(6000) }], + })), + }), + }); + assert.equal(r2.status, 200); + await r2.text(); + const main = peekSession(CONV); + assert.ok(main, "main session exists under the RAW key"); + assert.ok(Object.keys(main.state.messageRefs.byRaw).length >= 12, "refs assigned under the bare id"); + assert.ok(!peekSession(forkKey(CONV, MAIN_SYSTEM)), "main turn did NOT fork onto |sub:"); + } finally { + await closeRig(rig); + } +}); + test("e2e allowlist pin: a non-dsh plugin agent with two different systems stays on ONE session", async () => { const rig = await startRig(); const CONV = "dshp-plain"; From ab65aa0ad17716e91feff1b8c6f6d0b3f4e4b9c5 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 5 Oct 2026 16:48:28 +0800 Subject: [PATCH 2/2] =?UTF-8?q?docs:=20KDD#9=20=E2=80=94=20side=20requests?= =?UTF-8?q?=20never=20claim=20the=20persona=20anchor=20(#2156/#2160)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AGENTS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index 758e159263..aa12e24042 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -45,7 +45,7 @@ The full file-by-file map lives in [reference/architecture.md](reference/archite - **proxy mode** (plain client, no header): the proxy executes `compress` server-side; the tool call is ephemeral (never enters the client's history) and preflight blocks have none, so the carrier is the **`acp_summary` message** — the kernel renders it role `system`, but `systemToUser` (`src/util.ts`) re-voices it as a **`user` message** (leaving it at its anchor) so strict backends (SGLang: exactly one system at index 0, #377) accept it and the head system message stays byte-stable for the prefix cache. - The mode is decided per request and bound per session (`session.metadata.pluginAgent`, sticky, upgrade-only). See TECHNICAL-NOTES.md "Two compression modes". 8. **Nudge cadence is flat 50K by design (kernel contract)** — acp-kernel pins the growth interval at 50000 for every window size (`nudge.growthFloor == nudge.growthCap == 50000`; window-percentage scaling was deliberately removed — #379/#380 settled "growth-driven, no usage/count proxy gates"). Do NOT re-scale the interval with the context window or reintroduce percentage gates; a 1M-window session folding every 50K of growth is intended lean-context behavior. User escape hatch: `compress.nudgeGrowthTokens` (flattens `growthFloor`+`growthCap` to a fixed step) — big-window users who want a lazier cadence set it explicitly. Prompt wording is kernel-owned (`src/nudge-text.ts`). -9. **Hosts that reuse ONE session id across personas get id + system-hash session keys ("hash 不一样自动分裂")** — when a plugin host stamps the SAME conversation id on every model request of a session INCLUDING auxiliary personas (dsh auto-review `classifyRisk()`: fixed REVIEW_POLICY system + a fresh flattened blob before every tool call — #1916/#1307/#1314), those auxiliary requests must be keyed by `subagentNamespace(id, systemText)` so the persona forks onto its own `|sub:` session instead of overwriting the main session's baseline/snapshots. The FIRST system seen under the id keeps the raw key (kernel anchor), so main turns never fork; an empty system is non-anchoring (verbatim). This is an EVIDENCE-PERMITLIST (`dshPersonaFingerprintApplies` in `src/session-id.ts`), the same discipline as the instructions fingerprint (#1104/#1107): do NOT enable it wholesale — for everyone else system drift mid-id means "same conversation, evolved", and forking resets compression for no defending bug (#1106). A future host found reusing session ids across personas gets added to that predicate WITH traffic evidence (shape + stability proof: the host's main system must be session-stable, e.g. dsh composes it once per session and puts volatile time-context in a USER message). The fork must record itself under its suffixed id (`personaForked` → `recordPluginSession`) so the single-valued conversations map keeps the raw key owned by the main session (#970 discipline). Tests: `tests/dsh-persona-fingerprint.test.ts`. +9. **Hosts that reuse ONE session id across personas get id + system-hash session keys ("hash 不一样自动分裂")** — when a plugin host stamps the SAME conversation id on every model request of a session INCLUDING auxiliary personas (dsh auto-review `classifyRisk()`: fixed REVIEW_POLICY system + a fresh flattened blob before every tool call — #1916/#1307/#1314), those auxiliary requests must be keyed by `subagentNamespace(id, systemText)` so the persona forks onto its own `|sub:` session instead of overwriting the main session's baseline/snapshots. The FIRST system seen under the id keeps the raw key (kernel anchor), so main turns never fork; an empty system is non-anchoring (verbatim); SIDE REQUESTS never claim the anchor either — dsh's title-gen sidecar fires at session start before/concurrent with the first main turn, and claiming it would fork the whole main conversation onto `|sub:` while every host-stamped ACP management call (compress/decompress/acp_status) routes into the ghost's empty session and fails forever (#2156/#2160; the `sideRequestLike` gate in `src/server.ts` mirrors the #388 passthrough predicate). This is an EVIDENCE-PERMITLIST (`dshPersonaFingerprintApplies` in `src/session-id.ts`), the same discipline as the instructions fingerprint (#1104/#1107): do NOT enable it wholesale — for everyone else system drift mid-id means "same conversation, evolved", and forking resets compression for no defending bug (#1106). A future host found reusing session ids across personas gets added to that predicate WITH traffic evidence (shape + stability proof: the host's main system must be session-stable, e.g. dsh composes it once per session and puts volatile time-context in a USER message). The fork must record itself under its suffixed id (`personaForked` → `recordPluginSession`) so the single-valued conversations map keeps the raw key owned by the main session (#970 discipline). Tests: `tests/dsh-persona-fingerprint.test.ts`. 10. **Thinking-family blocks are signature-verified — NEVER rewrite them (#1960)** — Anthropic `thinking`/`redacted_thinking` blocks and Gemini `thoughtSignature` parts are verified byte-for-byte when the latest assistant message is replayed ("thinking blocks ... cannot be modified"). The bytes the proxy forwards become the client's persisted history, and the bytes the loop accumulates become the re-request rebuild — BOTH must stay verbatim. Any transformation (tag/marker stripping, dedup, trimming, re-laying-out block order, dropping empty segments) desynchronizes text from signature and bricks the session: first on the in-turn re-request, then on every subsequent request via the client's echo. #1882 put a prose filter on the thinking channel and it was exactly this bug. Treat ANY future change touching thinking-family payloads as high-risk: it needs loop-level byte-equality tests (re-request body AND client-visible stream) on both signed wires, not just adapter-level ones. The prose filters (#206/#1881) are for the TEXT channel only.