From 005ec647a0aaf49148a82b6b3645b06eb71211c0 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Mon, 16 Nov 2026 12:11:50 +0800 Subject: [PATCH 1/3] =?UTF-8?q?dsh=20purpose:=20stop=20guessing=20which=20?= =?UTF-8?q?requests=20are=20auxiliary=20=E2=80=94=20the=20plugin=20declare?= =?UTF-8?q?s=20it=20(#6880)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The dsh in-process plugin reads GenerateOptions.purpose off the llm/stream dispatch options (dsh contract: set ONLY on auxiliary calls — session-title / compaction / auto-review / timeline-report; ordinary conversation leaves it unset) and stamps it per fetch as x-bili-plugin-purpose, next to the existing x-bili-plugin(-conversation) marker. Server side: - plugin.ts: pluginPurposeHeader() — honored only behind the x-bili-plugin marker (same trust discipline as the conversation header); an unannounced client's purpose header is inert. - side-request.ts: isSideRequest gains a purpose parameter — a declared purpose classifies the request as side by declaration, outranking every body heuristic (max_tokens<=200 / system-text templates #2503/#4438/ #5656/#5584). Those recognizers stay as the fallback for pre-#6880 plugin installs. KNOWN_AUXILIARY_PURPOSES documents the set (not a gate). - handle.ts: both isSideRequest call sites pass the declared purpose (a declared side request takes the #388 verbatim passthrough and rides the raw persona key — no more |sub: ghost lanes); the compaction guard short-circuits on purpose=compaction so the marker-DECISIVE INSTRUCTION text check (#2193) is no longer load-bearing for post-#6880 installs while remaining the fallback. Tests: new tests/issue6880-purpose-declaration.test.ts (trust gate, declared sidecar at 4096 budget with template-drifted text, no-header control, unannounced purpose inert, declared compaction survives instruction drift); dsh-llm-stream-attribution.test.ts covers plugin-side capture/stamp (including junk-value degradation). --- src/agent/dsh-native.ts | 26 ++- src/plugin.ts | 19 ++ src/server/handle.ts | 17 +- src/server/side-request.ts | 25 ++- tests/dsh-llm-stream-attribution.test.ts | 46 +++++ tests/issue6880-purpose-declaration.test.ts | 206 ++++++++++++++++++++ 6 files changed, 330 insertions(+), 9 deletions(-) create mode 100644 tests/issue6880-purpose-declaration.test.ts diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 01e0252ca4..1a21f92c17 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -1221,7 +1221,11 @@ function registerArmed(): Promise { // log line each. Three states + per-endpoint cumulative counts make the // "dozens refused, one passed" distribution diagnosable at a glance. type GateAttribution = - | { state: "ok"; sid: string } + // #6880: `purpose` rides the dispatch's own options (GenerateOptions.purpose, + // dsh contract: set ONLY on auxiliary calls — ordinary conversation leaves + // it unset). Stamped as x-bili-plugin-purpose so the server classifies the + // request BY DECLARATION instead of content-guessing (#6880). + | { state: "ok"; sid: string; purpose?: string } | { state: "none"; initiatorPresent: boolean } | { state: "threw"; message: string }; @@ -1247,6 +1251,7 @@ type GateAttribution = interface LlmDispatchScope { sid: string; msgIds: string[] | undefined; + purpose?: string; } const dispatchScope = new AsyncLocalStorage(); @@ -1341,7 +1346,7 @@ function attributionOf(ctx: PluginContext): GateAttribution { noticedScopedAttribution = true; persistClientEvent("bili-native-dsh: llm/stream attribution active — session id rides the request object (#4082)"); } - return { state: "ok", sid: scoped.sid }; + return { state: "ok", sid: scoped.sid, purpose: scoped.purpose }; } try { const init = ctx.agents?.currentInitiator?.(); @@ -2079,13 +2084,17 @@ export async function apply(ctx: PluginContext): Promise { if (typeof ctx.inject === "function" && typeof ctx.on === "function") { const onEvent = ctx.on.bind(ctx); ctx.inject(["llm"], () => { - onEvent("llm/stream", (options: { sessionId?: unknown; messages?: unknown }, next: () => AsyncIterable) => { + onEvent("llm/stream", (options: { sessionId?: unknown; messages?: unknown; purpose?: unknown }, next: () => AsyncIterable) => { const sid = typeof options?.sessionId === "string" && options.sessionId.length > 0 ? options.sessionId : undefined; if (sid === undefined) return next(); // #4491: capture the host message ids riding the same options // — undefined (all-or-nothing) when any message lacks a usable // id, so sidecar shapes degrade to id-less turns. - const scope: LlmDispatchScope = { sid, msgIds: hostMsgIdsOf(options?.messages) }; + // #6880: capture the dispatch's declared purpose the same way + // (dsh sets it on every auxiliary call; conversation leaves it + // unset). Absent/odd values simply stay off the turn. + const purpose = typeof options?.purpose === "string" && options.purpose.length > 0 && options.purpose.length <= 64 && /^[\x21-\x7e]+$/.test(options.purpose) ? options.purpose : undefined; + const scope: LlmDispatchScope = { sid, msgIds: hostMsgIdsOf(options?.messages), purpose }; // #5403/#5418 v3: feed the per-slot id sequence into the // watermark channel (the fetch chokepoint emits count + // tail-ids; core = pf-channel.ts) — "?" slots ride id-less @@ -2600,6 +2609,15 @@ export async function apply(ctx: PluginContext): Promise { const target = sessionModelTargetOf(ctx) ?? globalModelTarget(modelInfo.services); refreshModelInfo(register.base, target); const headers: Record = { "x-bili-plugin": "dsh", "x-bili-plugin-conversation": sid }; + // #6880: the declared auxiliary purpose rides its own header (ASCII- + // gated like the model header below — an odd value degrades to no + // header, and the server falls back to its content heuristics). Only + // set for auxiliary dispatches by dsh's contract, so PRESENCE means + // "side request" without any per-feature content matching. + if (attr.purpose !== undefined) { + const purposeHeader = asciiHeaderValue(attr.purpose); + if (purposeHeader !== undefined) headers["x-bili-plugin-purpose"] = purposeHeader; + } // #3370: the incremental-turn marker (and, since #3329, the turn id) // is no longer stamped here — a closure-level armed flag shared by // every session raced concurrent requests across the awaited diff --git a/src/plugin.ts b/src/plugin.ts index 45cf4e2e38..12903619f6 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -144,6 +144,17 @@ const PLUGIN_INSTRUCTIONS_MUTABLE_HEADER = "x-bili-plugin-instructions-mutable"; * agents route by intent (side-request.ts SIDE_REQUEST_AGENTS); explicit "main" * vetoes side heuristics, while other persona ids remain inert telemetry. */ const PLUGIN_REQUEST_AGENT_HEADER = "x-bili-plugin-agent"; +/** #6880: the host's declared PURPOSE for the dispatch (dsh stamps + * GenerateOptions.purpose verbatim — "session-title", "compaction", + * "auto-review", "timeline-report", ...; its contract: ordinary + * conversation requests leave it unset). Carries the side/main intent + * that content-guessing used to approximate: a PLUGIN-ANNOUNCED request + * with a non-empty purpose is a side request by declaration, no per-feature + * template matching needed (the content recognizers stay as the fallback + * for pre-#6880 plugin installs). Honored only behind the x-bili-plugin + * marker, same trust discipline as the conversation header: an unannounced + * client's purpose header is inert telemetry. */ +const PLUGIN_PURPOSE_HEADER = "x-bili-plugin-purpose"; const PLUGIN_PROTOCOL_VERSION = 1; @@ -212,6 +223,14 @@ function headerValue(headers: Record, nam return t && t.length > 0 ? t : undefined; } +export function pluginPurposeHeader(headers: Record): string | undefined { + // #6880: purpose is a protocol-internal steering signal — honoring it + // without the plugin announcement would let any client that can reach + // the endpoint opt its traffic out of the session pipeline. A real + // plugin sends the marker and the purpose together. + return pluginAgentHeader(headers) !== undefined ? headerValue(headers, PLUGIN_PURPOSE_HEADER) : undefined; +} + export function pluginAgentHeader(headers: Record): string | undefined { return headerValue(headers, PLUGIN_AGENT_HEADER); } diff --git a/src/server/handle.ts b/src/server/handle.ts index 00122ada6a..8a30a8a281 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -56,7 +56,7 @@ import { maybeAdoptForkBlocks, maybeAdoptResume, maybeAdoptDerivedFork } from ". import { executeThreadsReforge } from "./threads-lane.js"; import { resetThreadTailTracking } from "./thread-tracking.js"; import { resolveClaudeTranscriptLineage } from "../claude-transcript-lineage.js"; -import { consumePluginRegisterFor, pluginAgentHeader, pluginCompressionProxyHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginReportedModel, pluginRequestAgentHeader, pluginRuntimeInfoFor, pluginRuntimeInfoForConversation, preparePluginWireMetadata, publicForkInputCheck, recordChainVerdict, recordPluginSession, rememberPluginMessages, resolveConversation, runtimeConversationId, takePendingPluginRegister } from "../plugin.js"; +import { consumePluginRegisterFor, pluginAgentHeader, pluginCompressionProxyHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginPurposeHeader, pluginReportedContextWindow, pluginReportedMaxOutput, pluginReportedModel, pluginRequestAgentHeader, pluginRuntimeInfoFor, pluginRuntimeInfoForConversation, preparePluginWireMetadata, publicForkInputCheck, recordChainVerdict, recordPluginSession, rememberPluginMessages, resolveConversation, runtimeConversationId, takePendingPluginRegister } from "../plugin.js"; import { scheduleAffinityPersist } from "../affinity-persist.js"; import { evaluateChain, extractChainCarriers } from "../chain-checkpoint.js"; import { BILI_PASSTHROUGH_HEADER, BILI_PLUGIN_BYPASS_HEADER, isLoopbackAddress, normalizeUpstreamOrigin, provenCompletionReserve, reserveOutputHeadroom, resolveOutputHeadroomCap, shouldReserveOutputHeadroom, type WireProtocol } from "../util.js"; @@ -1241,7 +1241,7 @@ async function handleRequest( const ccTitleSidecar = sideAgent !== "main" && claudeAnthropicPersonaOrderApplies(req.headers) && isClaudeCodeTitleRequest(protocol, parsed); const sideRequestLike = !countTokens && !responsesCompact && protocol !== null - && (isSideRequest(parsed, sideAgent) + && (isSideRequest(parsed, sideAgent, pluginPurposeHeader(req.headers)) // #2503: dsh title sidecar — its output budget grew past the // <=200 heuristic on dsh 0.2.1-alpha.2 (preset 64 -> 4096); // recognize it by system text so it keeps the raw key instead @@ -2485,7 +2485,16 @@ async function handleRequest( // is dsh-side, caught, and recoverable. Runs before the #388 // side-request lane: the compaction call is a full-budget request, so // only this guard can catch it. - if (protocol !== null && isDshCompactionCall(protocol, parsed)) { + // #6880: declaration-first compaction recognition. The post-#6880 dsh + // plugin stamps x-bili-plugin-purpose: compaction from the dispatch + // options itself, so the guard no longer depends on the INSTRUCTION + // text still matching the tracked template (the #2193 lesson — a + // stale shape goes silent and permanently loses the substrate). The + // marker-DECISIVE text check remains as the fallback for pre-#6880 + // installs; dsh-only, like the text path's trust domain. + const declaredDshCompaction = pluginAgentHeader(req.headers) === "dsh" + && pluginPurposeHeader(req.headers) === "compaction"; + if (protocol !== null && (isDshCompactionCall(protocol, parsed) || declaredDshCompaction)) { // #3733: AUTHORED compaction — when the knob is on and the session // holds active fold blocks, answer the call LOCALLY with our folded // snapshot instead of refusing: zero egress (autoModeForge #2740 @@ -2632,7 +2641,7 @@ async function handleRequest( // #5135: ccTitleSidecar joins the intent set so the title call rides // the #388 passthrough (kernel state untouched) like every other // recognized side request. - const sideIntent = isSideRequest(parsed, requestAgent) || dshTitleSidecar || dshReviewSidecar || dshSingleShotSidecar || ccTitleSidecar; + const sideIntent = isSideRequest(parsed, requestAgent, pluginPurposeHeader(req.headers)) || dshTitleSidecar || dshReviewSidecar || dshSingleShotSidecar || ccTitleSidecar; // #388/#2157 follow-up: side requests must not touch kernel state under // a public-fork receipt either. The receipt's first-request check above // has already accepted this request (inherited prefix matched, or live diff --git a/src/server/side-request.ts b/src/server/side-request.ts index b6c1cb04b0..7971a35cd4 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -20,6 +20,23 @@ import { dshCarrierRawMarkers } from "../durable-message-guards.js"; // (x-bili-plugin-agent) and a known side-request agent is a side req by // definition regardless of budget. export const SIDE_REQUEST_MAX_TOKENS = 200; + +/** #6880: the auxiliary purposes dsh's GenerateOptions.purpose carries on the + * wire today: session-title (session-title-llm), compaction (compaction-basic + * summarizer), auto-review (experimental/auto-review classifyRisk), + * timeline-report (experimental/timeline summary). This set is DOCUMENTATION, + * not a gate: dsh's contract is "ordinary conversation requests leave purpose + * unset", so ANY non-empty declared purpose on a plugin-announced request + * classifies it as a side request by declaration. A brand-new sidecar purpose + * needs no bili recognizer anymore — the per-feature template chain + * (#2503/#4438/#5656/#5584) stays only as the fallback for pre-#6880 plugin + * installs. */ +export const KNOWN_AUXILIARY_PURPOSES = new Set([ + "session-title", + "compaction", + "auto-review", + "timeline-report", +]); // Persona ids whose requests are side requests by intent (#1699). Main personas // (build/plan/general/...) are deliberately absent — they are real turns. export const SIDE_REQUEST_AGENTS: ReadonlySet = new Set(["title"]); @@ -184,10 +201,16 @@ export function isAudioOutputCall(parsed: unknown): boolean { return Object.keys(audio).length > 0; } -export function isSideRequest(parsed: unknown, requestAgent?: string): boolean { +export function isSideRequest(parsed: unknown, requestAgent?: string, purpose?: string): boolean { if (!parsed || typeof parsed !== "object") return false; if (requestAgent !== undefined && SIDE_REQUEST_AGENTS.has(requestAgent)) return true; if (requestAgent === "main") return false; + // #6880: the DECLARED purpose outranks every body heuristic. It arrives + // pre-gated (plugin.ts only surfaces it behind the x-bili-plugin marker), + // so a non-empty value is the plugin telling us — from the dispatch + // options it itself controls — that this call is auxiliary: route it + // through the #388 verbatim passthrough without content matching. + if (purpose !== undefined && purpose.length > 0) return true; const p = parsed as Record; // #2500: BEFORE the #546 rule — a server-tool-only utility call carries // non-empty tools but is not an agent turn. Routed through the #388 side diff --git a/tests/dsh-llm-stream-attribution.test.ts b/tests/dsh-llm-stream-attribution.test.ts index 70d6790a24..df574a83ef 100644 --- a/tests/dsh-llm-stream-attribution.test.ts +++ b/tests/dsh-llm-stream-attribution.test.ts @@ -400,5 +400,51 @@ test("agentless compaction dispatch (sessionId, no initiator) is attributed", as await it.next(); assert.ok(verdict !== false && verdict !== undefined, "compaction lane is attributed via the request sid"); assert.equal(stamped["x-bili-plugin-conversation"], "sess-compaction"); + // #6880: the declared purpose rides its own header on the same dispatch. + assert.equal(stamped["x-bili-plugin-purpose"], "compaction"); + }); +}); + +test("#6880: the dispatch purpose is captured into the scope and stamped per fetch", async (t) => { + await withSetup(async (handle) => { + const gate = _stateTakeoverGateForTest(); + const beforeSend = _stateBeforeSendForTest(); + const headersFor = _stateHeadersForTest(); + assert.ok(gate); + assert.ok(beforeSend); + assert.ok(headersFor); + const stamps: Record[] = []; + const base = async function* (): AsyncIterable { + const init: { headers?: Record } = { headers: {} }; + await gate(MODEL_URL, { bytes: 200 }, init); + await beforeSend(MODEL_URL, init); + stamps.push({ ...(headersFor?.(MODEL_URL) ?? {}), ...headersOf(init) }); + yield 1; + }; + handle.setInitiator(undefined); + + // 1. declared purpose: stamped verbatim next to the conversation header. + { + const it = runWaterfall(handle.events, { sessionId: "sess-review", purpose: "auto-review" }, base)[Symbol.asyncIterator](); + await it.next(); + assert.equal(stamps.at(-1)?.["x-bili-plugin-conversation"], "sess-review"); + assert.equal(stamps.at(-1)?.["x-bili-plugin-purpose"], "auto-review"); + } + // 2. ordinary conversation leaves purpose unset → NO purpose header + // (presence is the side-request signal; main turns stay quiet). + { + const it = runWaterfall(handle.events, { sessionId: "sess-main" }, base)[Symbol.asyncIterator](); + await it.next(); + assert.equal(stamps.at(-1)?.["x-bili-plugin-conversation"], "sess-main"); + assert.equal(stamps.at(-1)?.["x-bili-plugin-purpose"], undefined); + } + // 3. hostile/odd values degrade to no header, never a bad header. + for (const junk of ["", 42, { a: 1 }, "bad\x00control", "a".repeat(65), "has space", "non-ascii-\u00e9"] as unknown[]) { + stamps.length = 0; + const it = runWaterfall(handle.events, { sessionId: "sess-junk", purpose: junk }, base)[Symbol.asyncIterator](); + await it.next(); + assert.equal(stamps.at(-1)?.["x-bili-plugin-purpose"], undefined, `junk purpose ${JSON.stringify(String(junk).slice(0, 20))} stays off the wire`); + assert.equal(stamps.at(-1)?.["x-bili-plugin-conversation"], "sess-junk"); + } }); }); diff --git a/tests/issue6880-purpose-declaration.test.ts b/tests/issue6880-purpose-declaration.test.ts new file mode 100644 index 0000000000..d48ce6ba22 --- /dev/null +++ b/tests/issue6880-purpose-declaration.test.ts @@ -0,0 +1,206 @@ +/** + * #6880: the plugin-declared request purpose replaces content-guessing for + * dsh's auxiliary calls. + * + * The post-#6880 dsh plugin stamps `x-bili-plugin-purpose` from + * GenerateOptions.purpose on every dispatch (dsh contract: ordinary + * conversation requests leave it unset). The server classifies a + * plugin-ANNOUNCED request with a non-empty purpose as a side request by + * declaration — no per-feature template matching — and lets the guard + * recognize a declared compaction even when dsh's INSTRUCTION text drifted. + * The content heuristics stay as the fallback for pre-#6880 plugin installs, + * so every pin below pairs the declared path with its undeclared twin. + */ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { mkdtempSync, rmSync, mkdirSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetPluginStateForTest, pluginPurposeHeader, resolveConversation } from "../src/plugin.ts"; +import { _resetSessionsForTest } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { resetToolRingForTest } from "../src/tool-ring.ts"; +import { KNOWN_AUXILIARY_PURPOSES, isSideRequest } from "../src/server/side-request.ts"; +import { DSH_COMPACTION_INSTRUCTION_PREFIX } from "../src/server/dsh-compaction-guard.ts"; + +// ---- unit pins ------------------------------------------------------------- + +test("pluginPurposeHeader honors the purpose only behind the x-bili-plugin marker", () => { + const base = { "x-bili-plugin-purpose": "compaction" }; + assert.equal(pluginPurposeHeader({ ...base, "x-bili-plugin": "dsh" }), "compaction"); + // unannounced client: inert telemetry, never steering + assert.equal(pluginPurposeHeader(base), undefined); + // empty / whitespace values are absent values + assert.equal(pluginPurposeHeader({ "x-bili-plugin": "dsh", "x-bili-plugin-purpose": " " }), undefined); + assert.equal(pluginPurposeHeader({ "x-bili-plugin": "dsh" }), undefined); + // multi-value: the first wins, like every sibling plugin header + assert.equal(pluginPurposeHeader({ "x-bili-plugin": "dsh", "x-bili-plugin-purpose": ["session-title", "junk"] }), "session-title"); +}); + +test("isSideRequest: a declared purpose outranks every body heuristic", () => { + // the dsh title shape that broke the legacy budget rule (#2503): 4096 + // budget, no tools — with a purpose it is a side request by declaration + const titleShape = { model: "fake-a", max_completion_tokens: 4096, messages: [{ role: "user", content: "x" }] }; + assert.equal(isSideRequest(titleShape, undefined, "session-title"), true); + // a NEW, previously-unknown sidecar purpose needs no recognizer at all + assert.equal(isSideRequest(titleShape, undefined, "some-future-sidecar"), true); + // declared conversation (no purpose) keeps the legacy behavior + assert.equal(isSideRequest(titleShape), false); + assert.equal(isSideRequest(titleShape, undefined, undefined), false); + // purpose still cannot override an explicit "main" agent veto from the + // same host (opencode v2 territory — the two headers never co-occur in + // practice, but the intent rung order must stay sane) + assert.equal(isSideRequest({ model: "fake-a", messages: [{ role: "user", content: "x" }] }, "main", "whatever"), false); +}); + +test("KNOWN_AUXILIARY_PURPOSES documents the four dsh purposes on the wire today", () => { + assert.ok(KNOWN_AUXILIARY_PURPOSES.has("session-title")); + assert.ok(KNOWN_AUXILIARY_PURPOSES.has("compaction")); + assert.ok(KNOWN_AUXILIARY_PURPOSES.has("auto-review")); + assert.ok(KNOWN_AUXILIARY_PURPOSES.has("timeline-report")); +}); + +// ---- handle-level pins ----------------------------------------------------- + +process.env.NODE_ENV = "test"; +const testRoot = mkdtempSync(join(tmpdir(), "bili-issue6880-")); +test.after(() => rmSync(testRoot, { recursive: true, force: true })); +for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = testRoot; + +const MAIN_SYSTEM = "MAIN OPERATING SYSTEM"; + +async function harness() { + const dir = mkdtempSync(join(testRoot, "run-")); + for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = dir; + mkdirSync(join(dir, "billion-context", "sessions"), { recursive: true }); + mkdirSync(join(dir, "sessions"), { recursive: true }); + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + const store = new SessionStore({ dir: dir + "/sessions", enabled: false, debounceMs: 60000 }); + _setStoreForTest(store); + _setForTest({}); + const forwarded: Record[] = []; + const upstream = await new Promise<{ url: string; close: () => Promise }>((resolve) => { + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + 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 } })); + }); + }); + server.listen(0, "127.0.0.1", () => resolve({ url: `http://127.0.0.1:${(server.address() as { port: number }).port}`, close: async () => { server.close(); } })); + }); + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstream.url, routes: { [upstream.url]: { models: { "fake-a": { context: 400000 } } } }, modelContextLimit: 400000, kernelConfig: defaultConfig(400000), compress: { injectTool: true, injectNudge: true, preserveRecentMessages: 1, preserveRecentTokens: 0, minCompressRangeChars: 100 }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } } as ProxyOptions); + await once(proxy, "listening"); + const origin = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; + const send = async (conversationId: string, body: Record, extra: Record = {}) => { + const headers: Record = { "content-type": "application/json", "x-acp-session": conversationId, ...extra }; + return fetch(`${origin}/bili/${upstream.url}/v1/chat/completions`, { method: "POST", headers, body: JSON.stringify(body) }); + }; + const dshSend = (conversationId: string, body: Record, agent?: string, purpose?: string) => { + const extra: Record = { "x-bili-plugin": "dsh", "x-bili-plugin-conversation": conversationId }; + if (agent !== undefined) extra["x-bili-plugin-agent"] = agent; + if (purpose !== undefined) extra["x-bili-plugin-purpose"] = purpose; + return send(conversationId, body, extra).then(async (res) => ({ status: res.status, body: (await res.json()) as Record })); + }; + const subSessions = async (prefix: string) => { + const list = (await (await fetch(origin + "/__bili/sessions")).json()) as { sessions?: { id: string }[] }; + assert.ok(Array.isArray(list.sessions)); + return (list.sessions ?? []).map((s) => s.id).filter((id) => id.startsWith(prefix + "|sub:")); + }; + return { forwarded, dshSend, send, subSessions, origin, close: async () => { store.cancelAll(); proxy.close(); upstream.close(); await new Promise((r2) => setTimeout(r2, 50)); } }; +} + +test("declared title sidecar rides the passthrough under the raw key even with unknown system text", async () => { + const h = await harness(); + try { + const history = [ + { role: "user", content: "set up the workspace" }, + { role: "assistant", content: "done" }, + ]; + // The #2503 killer shape: alpha.2 budget (4096, no tools) AND a system + // prompt that is NOT the tracked DSH_TITLE_SYSTEM_PREFIX template — + // i.e. a dsh version whose title text moved. Only the declaration + // can still classify it. + const driftedTitleSystem = "Summarize this conversation into a short heading. New template text."; + const titleBody = { model: "fake-a", max_completion_tokens: 4096, stream: false, messages: [{ role: "system", content: driftedTitleSystem }, ...history, { role: "user", content: `[${JSON.stringify(history)}]` }] }; + + // 1. declared: side request by purpose — verbatim, no ghost, no kernel. + await h.dshSend("conv1", titleBody, undefined, "session-title"); + assert.deepEqual(h.forwarded.at(-1), titleBody, "declared sidecar forwarded verbatim (nothing injected)"); + assert.equal(resolveConversation("conv1").session?.stats.requests ?? 0, 0, "declared sidecar runs no kernel request"); + assert.deepEqual(await h.subSessions("conv1"), [], "declared sidecar mints no |sub: ghost"); + + // 2. first real main turn still claims the raw key. + const mainBody = { model: "fake-a", max_completion_tokens: 8192, stream: false, messages: [{ role: "system", content: MAIN_SYSTEM }, ...history, { role: "assistant", content: "ok" }, { role: "user", content: "now configure ci" }], tools: [{ type: "function", function: { name: "bash", description: "run a command", parameters: { type: "object", properties: {} } } }] }; + await h.dshSend("conv1", mainBody, "main"); + const session = resolveConversation("conv1").session!; + assert.equal(session.stats.requests, 1, "the main turn runs the full pipeline exactly once"); + assert.deepEqual(await h.subSessions("conv1"), [], "main turn stays on the raw key (no persona fork)"); + } finally { + await h.close(); + } +}); + +test("the same body WITHOUT the purpose header is a main turn (legacy path unchanged)", async () => { + const h = await harness(); + try { + const history = [{ role: "user", content: "hello" }]; + const body = { model: "fake-a", max_completion_tokens: 4096, stream: false, messages: [{ role: "system", content: "Summarize this conversation into a short heading." }, ...history, { role: "user", content: "anything" }] }; + // pre-#6880 plugin: no purpose header, unknown system text, big + // budget — the legacy heuristics cannot classify it as a sidecar, so + // it runs the full pipeline exactly as before (behavior parity for + // un-upgraded installs). + const r1 = await h.dshSend("conv2", body); + assert.equal(r1.status, 200); + assert.equal(resolveConversation("conv2").session?.stats.requests ?? 0, 1, "undeclared looks like a main turn to the legacy rules"); + // but with the declaration, an IDENTICAL body on a fresh id is a + // sidecar — the header, not the bytes, decides. + const r2 = await h.dshSend("conv3", body, undefined, "session-title"); + assert.equal(r2.status, 200); + assert.equal(resolveConversation("conv3").session?.stats.requests ?? 0, 0, "identical body + declaration is a sidecar"); + } finally { + await h.close(); + } +}); + +test("a purpose header without the x-bili-plugin marker is inert", async () => { + const h = await harness(); + try { + const body = { model: "fake-a", max_completion_tokens: 8192, stream: false, messages: [{ role: "system", content: MAIN_SYSTEM }, { role: "user", content: "hello" }] }; + // unannounced client: the purpose must not steer it into the side + // passthrough — the trust gate keeps the pipeline running. + const r = await h.send("conv4", body, { "x-bili-plugin-purpose": "compaction" }); + assert.equal(r.status, 200); + assert.equal(resolveConversation("conv4").session?.stats.requests ?? 0, 1, "no marker: purpose ignored, full pipeline runs"); + } finally { + await h.close(); + } +}); + +test("a declared dsh compaction is guarded even when the INSTRUCTION text drifted", async () => { + const h = await harness(); + try { + // dsh bumped its compaction INSTRUCTION template; the tracked prefix + // no longer matches (the #2193 failure mode) — but the plugin + // declaration still carries the purpose. + const driftedInstruction = "Please compress the prior conversation. " + DSH_COMPACTION_INSTRUCTION_PREFIX.slice(0, 3); + const body = { model: "fake-a", max_completion_tokens: 8192, stream: false, messages: [{ role: "system", content: MAIN_SYSTEM }, { role: "user", content: "old work" }, { role: "assistant", content: "ok" }, { role: "user", content: driftedInstruction }] }; + const r = await h.dshSend("conv5", body, undefined, "compaction"); + assert.equal(r.status, 422, "declared compaction is refused like its text-matched twin"); + const err = r.body?.error as { code?: string }; + assert.equal(err?.code, "dsh_compaction_refused"); + assert.equal(h.forwarded.length, 0, "the refused call never reaches the upstream"); + } finally { + await h.close(); + } +}); From 81937b114cfa5b567e5eef5f499298a838e5eba5 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Mon, 16 Nov 2026 12:15:45 +0800 Subject: [PATCH 2/3] side-lane truth table: pin the declared-purpose input (#6880, lane-guard) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The lane-guard job (#2170) requires a side-lane suite edit alongside any side-request.ts/handle.ts lane change: exhaustively pin isSideRequest's new purpose argument over agent × purpose × the strongest main-shape body (main veto outranks declaration; known side agent wins as before; empty/ absent purpose keeps the pre-#6880 table byte-identical), plus the two blind shapes the template recognizers exist for flipping by declaration without content matching. --- tests/side-lane-truth-table.test.ts | 42 ++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/tests/side-lane-truth-table.test.ts b/tests/side-lane-truth-table.test.ts index 30d0402663..2630bcd4ea 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, DSH_TITLE_SYSTEM_PREFIX, isDshTitleRequest, isSideRequest, resolveSideLane, SIDE_REQUEST_MAX_TOKENS } from "../src/server/side-request.ts"; +import { demoteGate, DSH_TITLE_SYSTEM_PREFIX, isDshTitleRequest, isSideRequest, resolveSideLane, SIDE_REQUEST_AGENTS, 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 @@ -173,3 +173,43 @@ test("isDshTitleRequest: non-object bodies stay closed", () => { assert.equal(isDshTitleRequest("nope"), false); }); + +// #6880: the DECLARED purpose is a third isSideRequest input. It only reaches +// the gate pre-gated (plugin.ts surfaces it behind the x-bili-plugin marker +// only — an unannounced client's header never arrives here), so a non-empty +// value IS the plugin's statement that this call is auxiliary: it outranks +// every body heuristic, while the "main" agent veto still wins over it (an +// explicitly main-classified turn is a main turn, declaration or not). An +// absent/empty purpose leaves the pre-#6880 table untouched. +const PURPOSE_STATES: (string | undefined)[] = [undefined, "", "session-title", "compaction", "auto-review", "timeline-report", "brand-new-purpose"]; +// the strongest body-side MAIN evidence: tools veto + a budget far past 200 +const MAIN_SHAPE_BODY: Record = { max_output_tokens: 384_000, tools: [{ name: "bash" }] }; + +test("isSideRequest: declared purpose is exhaustive over agent × purpose × main-shape body (#6880)", () => { + let combos = 0; + for (const requestAgent of AGENT_STATES) { + for (const purpose of PURPOSE_STATES) { + combos++; + const got = isSideRequest(MAIN_SHAPE_BODY, requestAgent, purpose); + const want = requestAgent === "main" + ? false // the main veto outranks purpose (declaration is auxiliary intent, not a main-override) + : requestAgent !== undefined && SIDE_REQUEST_AGENTS.has(requestAgent) + ? true // known side agent wins as before + : purpose !== undefined && purpose.length > 0 + ? true // non-empty declared purpose → side by declaration + : false; // body heuristics decide exactly as pre-#6880 + assert.equal(got, want, JSON.stringify({ requestAgent, purpose })); + } + } + assert.ok(combos >= 21, `exercised ${combos} combinations`); +}); + +test("isSideRequest: declared purpose flips the two pre-#6880 blind shapes (4096 title budget, #2203 review blob)", () => { + // the exact shapes the per-feature template recognizers exist for — + // declaration makes them ride the lane without ANY content matching + assert.equal(isSideRequest(dshTitleBody(4096), undefined, "session-title"), true); + assert.equal(isSideRequest({ max_output_tokens: 384_000 }, undefined, "auto-review"), true); + // and an empty string is inert — the table is byte-identical to pre-#6880 + assert.equal(isSideRequest(dshTitleBody(4096), undefined, ""), false); + assert.equal(isSideRequest({ max_output_tokens: 384_000 }, undefined, ""), false); +}); From 4662b074ea8a89ed17ce76374aefe4c8c9046094 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Mon, 16 Nov 2026 13:30:31 +0800 Subject: [PATCH 3/3] =?UTF-8?q?plugin:=20v2=20wire=20=E2=80=94=20declare?= =?UTF-8?q?=20EVERY=20dispatch's=20purpose,=20main=20traffic=20keys=20the?= =?UTF-8?q?=20raw=20session=20id=20(#6880)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The #6883 purpose header was only stamped for auxiliary dispatches, which left the persona-order ladder (claim races, |sub: fork rungs) live for dsh main turns even on upgraded plugins. dsh mints one UUID per session and never recycles an id (SESSION_ALREADY_EXISTS in its store), so for a conversation whose plugin declares purposes the raw session id IS the complete identity — the ladder is dead weight there. - dsh-native: stamp x-bili-plugin-purpose on every dispatch — the real dsh purpose for aux calls, or the new "conversation" sentinel for main turns (dsh leaves GenerateOptions.purpose unset for ordinary conversation, so no real purpose can collide; junk values degrade to the sentinel, never the raw value). - plugin.ts: export PLUGIN_PURPOSE_CONVERSATION sentinel. - handle.ts: normalize the sentinel back to "absent" before the side classification (it is a main turn, never a sidecar); a request CARRYING the header in any value skips all four persona-order call sites and keys the verbatim raw id. Pre-#6883 plugins stamp no header and keep the ladder byte-for-byte; persisted |sub: lanes of old traffic become orphan history (new declared traffic never resolves onto them). The declared compaction guard now reads the normalized purpose; the compaction authored/refusal logs and the sideLane label state the ACTUAL identification basis (declared purpose=… instead of claiming the content heuristics) — review follow-up on the original PR, folded in. - tests: truth-table pin for the sentinel split (#2170 lane-guard), three new handle-level v2 pins (first-sight raw key, system-drift stays raw, sentinel is a main turn), attribution stamps updated to the v2 wire. --- src/agent/dsh-native.ts | 21 ++--- src/plugin.ts | 10 +++ src/server/handle.ts | 52 ++++++++++--- tests/dsh-llm-stream-attribution.test.ts | 13 ++-- tests/issue6880-purpose-declaration.test.ts | 85 +++++++++++++++++++-- tests/side-lane-truth-table.test.ts | 24 ++++++ 6 files changed, 173 insertions(+), 32 deletions(-) diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 1a21f92c17..5d83069fb5 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -2609,15 +2609,18 @@ export async function apply(ctx: PluginContext): Promise { const target = sessionModelTargetOf(ctx) ?? globalModelTarget(modelInfo.services); refreshModelInfo(register.base, target); const headers: Record = { "x-bili-plugin": "dsh", "x-bili-plugin-conversation": sid }; - // #6880: the declared auxiliary purpose rides its own header (ASCII- - // gated like the model header below — an odd value degrades to no - // header, and the server falls back to its content heuristics). Only - // set for auxiliary dispatches by dsh's contract, so PRESENCE means - // "side request" without any per-feature content matching. - if (attr.purpose !== undefined) { - const purposeHeader = asciiHeaderValue(attr.purpose); - if (purposeHeader !== undefined) headers["x-bili-plugin-purpose"] = purposeHeader; - } + // #6880: the declared purpose rides its own header (ASCII-gated like + // the model header below — an odd value degrades to no header, and + // the server falls back to its content heuristics). Post-#6883 this is + // stamped on EVERY dispatch: the real dsh purpose for auxiliary + // calls, or the "conversation" sentinel for main turns (dsh leaves + // GenerateOptions.purpose unset for ordinary conversation). PRESENCE + // is therefore the v2 declaration — the server routes declared aux by + // the header alone and keys declared main traffic on the raw session + // id (no persona-order ladder); the sentinel itself is not a side. + const declaredPurpose = attr.purpose ?? "conversation"; + const purposeHeader = asciiHeaderValue(declaredPurpose); + if (purposeHeader !== undefined) headers["x-bili-plugin-purpose"] = purposeHeader; // #3370: the incremental-turn marker (and, since #3329, the turn id) // is no longer stamped here — a closure-level armed flag shared by // every session raced concurrent requests across the awaited diff --git a/src/plugin.ts b/src/plugin.ts index 12903619f6..d58d0ffa8b 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -155,6 +155,16 @@ const PLUGIN_REQUEST_AGENT_HEADER = "x-bili-plugin-agent"; * marker, same trust discipline as the conversation header: an unannounced * client's purpose header is inert telemetry. */ const PLUGIN_PURPOSE_HEADER = "x-bili-plugin-purpose"; +/** #6880 v2 wire: the post-#6883 dsh plugin stamps this sentinel for MAIN + * turns (dsh leaves GenerateOptions.purpose unset for ordinary conversation, + * so no real dsh purpose can collide with it). PRESENCE of the header in any + * value is the declaration that this conversation's plugin announces every + * dispatch's purpose — main traffic then keys the raw session id directly + * (dsh mints one UUID per session, ids never recycle) instead of routing + * through the persona-order ladder, and its auxiliary traffic is a declared + * side request by the header alone. The sentinel itself is NOT a side: + * handle.ts normalizes it back to "absent" before the side classification. */ +export const PLUGIN_PURPOSE_CONVERSATION = "conversation"; const PLUGIN_PROTOCOL_VERSION = 1; diff --git a/src/server/handle.ts b/src/server/handle.ts index 8a30a8a281..11dbc5d5b5 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -56,7 +56,7 @@ import { maybeAdoptForkBlocks, maybeAdoptResume, maybeAdoptDerivedFork } from ". import { executeThreadsReforge } from "./threads-lane.js"; import { resetThreadTailTracking } from "./thread-tracking.js"; import { resolveClaudeTranscriptLineage } from "../claude-transcript-lineage.js"; -import { consumePluginRegisterFor, pluginAgentHeader, pluginCompressionProxyHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginPurposeHeader, pluginReportedContextWindow, pluginReportedMaxOutput, pluginReportedModel, pluginRequestAgentHeader, pluginRuntimeInfoFor, pluginRuntimeInfoForConversation, preparePluginWireMetadata, publicForkInputCheck, recordChainVerdict, recordPluginSession, rememberPluginMessages, resolveConversation, runtimeConversationId, takePendingPluginRegister } from "../plugin.js"; +import { consumePluginRegisterFor, PLUGIN_PURPOSE_CONVERSATION, pluginAgentHeader, pluginCompressionProxyHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginPurposeHeader, pluginReportedContextWindow, pluginReportedMaxOutput, pluginReportedModel, pluginRequestAgentHeader, pluginRuntimeInfoFor, pluginRuntimeInfoForConversation, preparePluginWireMetadata, publicForkInputCheck, recordChainVerdict, recordPluginSession, rememberPluginMessages, resolveConversation, runtimeConversationId, takePendingPluginRegister } from "../plugin.js"; import { scheduleAffinityPersist } from "../affinity-persist.js"; import { evaluateChain, extractChainCarriers } from "../chain-checkpoint.js"; import { BILI_PASSTHROUGH_HEADER, BILI_PLUGIN_BYPASS_HEADER, isLoopbackAddress, normalizeUpstreamOrigin, provenCompletionReserve, reserveOutputHeadroom, resolveOutputHeadroomCap, shouldReserveOutputHeadroom, type WireProtocol } from "../util.js"; @@ -1240,8 +1240,17 @@ async function handleRequest( // resolver — never opened globally. const ccTitleSidecar = sideAgent !== "main" && claudeAnthropicPersonaOrderApplies(req.headers) && isClaudeCodeTitleRequest(protocol, parsed); + // #6880: normalize the declared purpose for the SIDE classification + // (and the lane log below) — one parse, marker-gated. The + // "conversation" sentinel the post-#6883 plugin stamps on MAIN turns + // means "no declared auxiliary purpose", so it is indistinguishable + // from the absent header here (an aux dispatch never carries the + // sentinel: dsh leaves purpose unset for ordinary conversation and + // the plugin only stamps the real value). + const rawDeclaredPurpose = pluginPurposeHeader(req.headers); + const declaredPurpose = rawDeclaredPurpose === PLUGIN_PURPOSE_CONVERSATION ? undefined : rawDeclaredPurpose; const sideRequestLike = !countTokens && !responsesCompact && protocol !== null - && (isSideRequest(parsed, sideAgent, pluginPurposeHeader(req.headers)) + && (isSideRequest(parsed, sideAgent, declaredPurpose) // #2503: dsh title sidecar — its output budget grew past the // <=200 heuristic on dsh 0.2.1-alpha.2 (preset 64 -> 4096); // recognize it by system text so it keeps the raw key instead @@ -1291,6 +1300,19 @@ async function handleRequest( // reviews fork; an empty system is non-anchoring (verbatim key), so // system-less auxiliary calls keep riding the main session. const dshPersona = dshPersonaFingerprintApplies(req.headers); + // #6880 terminal state: a request that CARRIES the purpose header in + // any value is from a conversation whose plugin declares every + // dispatch's purpose (post-#6883 dsh). For those, the persona-order + // ladder (claim races, fork rungs) is dead weight — dsh mints one + // UUID per session and never recycles an id (SESSION_ALREADY_EXISTS + // in its store), so the raw session id IS the complete identity and + // the main turn must key it verbatim. All auxiliary traffic from + // such a plugin is declared side above and never reaches the + // persona branches below. Pre-#6883 plugins stamp no header at all, + // so they keep the ladder byte-for-byte (rollout safety). The + // already-persisted |sub: lanes of old traffic become orphan + // history: new declared traffic simply never resolves onto them. + const dshDeclaredPersona = dshPersona && rawDeclaredPurpose !== undefined; const personaSystemText = protocol === "openai" ? openaiSystemTextForPersona(parsed as OpenAIRequestBody) : protocol === "anthropic" @@ -1372,7 +1394,7 @@ async function handleRequest( // empty systemInstruction is non-anchoring (kernel anchor // semantics): ride the verbatim id — never route empty-tag // traffic through the order resolver. - ? (dshPersona && !sideRequestLike + ? (!dshDeclaredPersona && dshPersona && !sideRequestLike ? (personaSystemText === "" ? (googleIdentity?.value ?? googleSignal) : personaConversationOrder( @@ -1397,7 +1419,7 @@ async function handleRequest( // across main and subagent sessions. (claudeSub !== undefined && opts.subagentSplit !== false ? claudeSubagentSplit(anthropicIdentity?.value ?? anthropicSignal, req.headers, systemTextsForSplit) - : dshPersona && !sideRequestLike + : !dshDeclaredPersona && dshPersona && !sideRequestLike // #2241: continuity-aware anchor — a system change whose // history continues the raw key's prefix-affinity chain // (model switch) MIGRATES the anchor instead of forking @@ -1455,7 +1477,7 @@ async function handleRequest( ) : anthropicIdentity?.value ?? anthropicSignal) : protocol === "openai" - ? (dshPersona && !sideRequestLike && personaSystemText !== "" + ? (!dshDeclaredPersona && dshPersona && !sideRequestLike && personaSystemText !== "" ? personaConversationOrder( openaiIdentity?.value ?? openaiSignal, (parsed as OpenAIRequestBody).messages, @@ -1474,7 +1496,7 @@ async function handleRequest( // kernel's empty-instructions non-anchoring path is left // untouched for metadata-less clients). ? codexTurn.value - : dshPersona && !sideRequestLike && ((parsed as ResponsesRequestBody).instructions ?? "") !== "" + : !dshDeclaredPersona && dshPersona && !sideRequestLike && ((parsed as ResponsesRequestBody).instructions ?? "") !== "" // #2203/#2241: parity with the anthropic/openai lanes — // dsh-over-Responses rides the same continuity-aware persona // anchor: a model switch (history continues the raw key's @@ -2493,8 +2515,16 @@ async function handleRequest( // marker-DECISIVE text check remains as the fallback for pre-#6880 // installs; dsh-only, like the text path's trust domain. const declaredDshCompaction = pluginAgentHeader(req.headers) === "dsh" - && pluginPurposeHeader(req.headers) === "compaction"; + && declaredPurpose === "compaction"; if (protocol !== null && (isDshCompactionCall(protocol, parsed) || declaredDshCompaction)) { + // #6880: the log must state the ACTUAL identification basis — a + // declared compaction is recognized by declaration even when the + // INSTRUCTION text has drifted (the exact case this declaration + // exists for), so claiming the text match there misleads the + // operator reading the lane. + const identifiedAs = declaredDshCompaction + ? "declared purpose=compaction (#6880)" + : "final user message = COMPACTION_INSTRUCTION"; // #3733: AUTHORED compaction — when the knob is on and the session // holds active fold blocks, answer the call LOCALLY with our folded // snapshot instead of refusing: zero egress (autoModeForge #2740 @@ -2518,11 +2548,11 @@ async function handleRequest( ? buildDshCompactionForgeBody(protocol as "openai" | "anthropic" | "responses", forgeText, { inputTokens: Math.max(1, estimateCjkTokens(forgeText)), outputTokens: 0 }, (parsed as { stream?: unknown }).stream === true) : null; if (authored !== null) { - log("info", `[${session.id}] dsh compaction call identified (final user message = COMPACTION_INSTRUCTION, ${inboundMsgs} msgs) — ANSWERED LOCALLY with the bili snapshot (#3733 authored checkpoint): upstream not contacted, dsh records our tagged checkpoint; originals stay recoverable via CCR/export`); + log("info", `[${session.id}] dsh compaction call identified (${identifiedAs}, ${inboundMsgs} msgs) — ANSWERED LOCALLY with the bili snapshot (#3733 authored checkpoint): upstream not contacted, dsh records our tagged checkpoint; originals stay recoverable via CCR/export`); } else if (session.metadata.dshCompactionRefused !== true) { session.metadata.dshCompactionRefused = true; const shapeDrift = inboundMsgs !== null && inboundMsgs > DSH_COMPACTION_SHAPE_MSGS ? `, shape drifted from the ≤${DSH_COMPACTION_SHAPE_MSGS}-msg rc.1 envelope — rc.2 replays the full shadowed region (#2193)` : ""; - log("warn", `[${session.id}] dsh native compaction call identified (final user message = COMPACTION_INSTRUCTION, ${inboundMsgs} msgs${shapeDrift}) — REFUSED, not forwarded: bili owns compression on this lane; a landed dsh checkpoint would durably shadow the raw history (#1729, cf. #1206/#1772)`); + log("warn", `[${session.id}] dsh native compaction call identified (${identifiedAs}, ${inboundMsgs} msgs${shapeDrift}) — REFUSED, not forwarded: bili owns compression on this lane; a landed dsh checkpoint would durably shadow the raw history (#1729, cf. #1206/#1772)`); } // #2490: remember the replayed envelope SIZE. The host's raw ledger is // invisible to bili's folded view (folds never shrink what dsh itself @@ -2641,7 +2671,7 @@ async function handleRequest( // #5135: ccTitleSidecar joins the intent set so the title call rides // the #388 passthrough (kernel state untouched) like every other // recognized side request. - const sideIntent = isSideRequest(parsed, requestAgent, pluginPurposeHeader(req.headers)) || dshTitleSidecar || dshReviewSidecar || dshSingleShotSidecar || ccTitleSidecar; + const sideIntent = isSideRequest(parsed, requestAgent, declaredPurpose) || dshTitleSidecar || dshReviewSidecar || dshSingleShotSidecar || ccTitleSidecar; // #388/#2157 follow-up: side requests must not touch kernel state under // a public-fork receipt either. The receipt's first-request check above // has already accepted this request (inherited prefix matched, or live @@ -2665,7 +2695,7 @@ async function handleRequest( // #2500 server-tool utility shape) diverts under a receipt. // #2170 measure 1: the decision itself is resolveSideLane() (pure, // truth-table-tested); demotedSide ⊆ lane==="side" by construction. - const sideLane = resolveSideLane({ countTokens, responsesCompact, protocol, stripApplied: demotedSide, sideIntent, requestAgent, sideLabel: serverToolUtility ? "server-tool utility call (#2500)" : structuredOutputUtility ? "structured-output utility call (#5073)" : audioOutputUtility ? "audio-output utility call (#6636)" : dshTitleSidecar ? "dsh title-gen sidecar (#2503)" : dshReviewSidecar ? "dsh auto-review sidecar (#4438)" : dshSingleShotSidecar ? "dsh single-shot sidecar (#5656)" : ccTitleSidecar ? "claude-code title-gen sidecar (#5135)" : undefined }); + const sideLane = resolveSideLane({ countTokens, responsesCompact, protocol, stripApplied: demotedSide, sideIntent, requestAgent, sideLabel: declaredPurpose !== undefined ? `declared purpose=${declaredPurpose} (#6880)` : serverToolUtility ? "server-tool utility call (#2500)" : structuredOutputUtility ? "structured-output utility call (#5073)" : audioOutputUtility ? "audio-output utility call (#6636)" : dshTitleSidecar ? "dsh title-gen sidecar (#2503)" : dshReviewSidecar ? "dsh auto-review sidecar (#4438)" : dshSingleShotSidecar ? "dsh single-shot sidecar (#5656)" : ccTitleSidecar ? "claude-code title-gen sidecar (#5135)" : undefined }); if (sideLane.lane === "side") { // #554: the passthrough below skips EVERY input-side guard by design // (#388) — a full-history side request over the window is a diff --git a/tests/dsh-llm-stream-attribution.test.ts b/tests/dsh-llm-stream-attribution.test.ts index df574a83ef..5bb5b7d0bd 100644 --- a/tests/dsh-llm-stream-attribution.test.ts +++ b/tests/dsh-llm-stream-attribution.test.ts @@ -430,20 +430,23 @@ test("#6880: the dispatch purpose is captured into the scope and stamped per fet assert.equal(stamps.at(-1)?.["x-bili-plugin-conversation"], "sess-review"); assert.equal(stamps.at(-1)?.["x-bili-plugin-purpose"], "auto-review"); } - // 2. ordinary conversation leaves purpose unset → NO purpose header - // (presence is the side-request signal; main turns stay quiet). + // 2. ordinary conversation leaves purpose unset → the "conversation" + // sentinel is stamped (presence is the v2 declaration; the server + // normalizes the sentinel back to "no declared purpose", so it is + // never a side request). { const it = runWaterfall(handle.events, { sessionId: "sess-main" }, base)[Symbol.asyncIterator](); await it.next(); assert.equal(stamps.at(-1)?.["x-bili-plugin-conversation"], "sess-main"); - assert.equal(stamps.at(-1)?.["x-bili-plugin-purpose"], undefined); + assert.equal(stamps.at(-1)?.["x-bili-plugin-purpose"], "conversation"); } - // 3. hostile/odd values degrade to no header, never a bad header. + // 3. hostile/odd values degrade to the main-turn sentinel — never a + // bad header, never a declared aux purpose. for (const junk of ["", 42, { a: 1 }, "bad\x00control", "a".repeat(65), "has space", "non-ascii-\u00e9"] as unknown[]) { stamps.length = 0; const it = runWaterfall(handle.events, { sessionId: "sess-junk", purpose: junk }, base)[Symbol.asyncIterator](); await it.next(); - assert.equal(stamps.at(-1)?.["x-bili-plugin-purpose"], undefined, `junk purpose ${JSON.stringify(String(junk).slice(0, 20))} stays off the wire`); + assert.equal(stamps.at(-1)?.["x-bili-plugin-purpose"], "conversation", `junk purpose ${JSON.stringify(String(junk).slice(0, 20))} degrades to the sentinel, never the raw value`); assert.equal(stamps.at(-1)?.["x-bili-plugin-conversation"], "sess-junk"); } }); diff --git a/tests/issue6880-purpose-declaration.test.ts b/tests/issue6880-purpose-declaration.test.ts index d48ce6ba22..2f8c0fad95 100644 --- a/tests/issue6880-purpose-declaration.test.ts +++ b/tests/issue6880-purpose-declaration.test.ts @@ -3,13 +3,17 @@ * dsh's auxiliary calls. * * The post-#6880 dsh plugin stamps `x-bili-plugin-purpose` from - * GenerateOptions.purpose on every dispatch (dsh contract: ordinary - * conversation requests leave it unset). The server classifies a - * plugin-ANNOUNCED request with a non-empty purpose as a side request by - * declaration — no per-feature template matching — and lets the guard - * recognize a declared compaction even when dsh's INSTRUCTION text drifted. - * The content heuristics stay as the fallback for pre-#6880 plugin installs, - * so every pin below pairs the declared path with its undeclared twin. + * GenerateOptions.purpose on EVERY dispatch (dsh contract: ordinary + * conversation requests leave it unset, so the plugin stamps the + * "conversation" sentinel for main turns). The server normalizes the + * sentinel back to "absent" before the side classification, treats any + * OTHER declared purpose as a side request by declaration — no per-feature + * template matching — lets the guard recognize a declared compaction even + * when dsh's INSTRUCTION text drifted, and keys declared main traffic on + * the raw session id (the v2 wire terminal state of #6880: one conversation + * one key, no persona-order ladder). The content heuristics stay as the + * fallback for pre-#6880 plugin installs, so every pin below pairs the + * declared path with its undeclared twin. */ import assert from "node:assert/strict"; import http from "node:http"; @@ -204,3 +208,70 @@ test("a declared dsh compaction is guarded even when the INSTRUCTION text drifte await h.close(); } }); + +// ---- #6880 v2 wire: declared main traffic keys the raw session id -------- +// +// A request that carries the purpose header in ANY value comes from a +// conversation whose plugin declares every dispatch's purpose (post-#6883 +// dsh). dsh mints one UUID per session and never recycles an id, so for +// those conversations the raw session id IS the complete identity: the +// persona-order ladder (claim races, fork rungs) is skipped and the main +// turn lands on the verbatim key. Pre-#6883 plugins stamp no header and +// keep the ladder byte-for-byte. + +test("v2: a declared main turn keys the raw id on FIRST sight (no ladder, no claim race)", async () => { + const h = await harness(); + try { + // A review-shape blob (foreign system, no shared history with what + // comes later) would FIRST-SIGHT the raw key under the legacy + // ladder and steal it from the real main turn (#1916). Declared + // traffic never enters the ladder — its aux is a declared side, so + // the first request that ever claims the raw key IS the main turn. + const mainBody = { model: "fake-a", max_completion_tokens: 8192, stream: false, messages: [{ role: "system", content: MAIN_SYSTEM }, { role: "user", content: "set up the workspace" }, { role: "assistant", content: "done" }, { role: "user", content: "now configure ci" }] }; + const r = await h.dshSend("conv6", mainBody, undefined, "conversation"); + assert.equal(r.status, 200); + const session = resolveConversation("conv6").session!; + assert.ok(session, "the raw conversation resolved to a session"); + assert.equal(session.stats.requests, 1, "the declared main turn runs the full pipeline"); + assert.deepEqual(await h.subSessions("conv6"), [], "no persona fork off a declared main turn"); + } finally { + await h.close(); + } +}); + +test("v2: a system change on a declared conversation stays on the raw key (no drift fork)", async () => { + const h = await harness(); + try { + const base = [{ role: "user", content: "hello" }, { role: "assistant", content: "hi" }]; + // turn 1, undeclared (pre-#6883 plugin shape): the legacy ladder + // anchors the persona on the raw key. + await h.dshSend("conv7", { model: "fake-a", max_completion_tokens: 8192, stream: false, messages: [{ role: "system", content: MAIN_SYSTEM }, ...base] }); + // turn 2: the SAME conversation, new plugin, but its system text has + // drifted (dsh version bump) and the history is history-disjoint. + // Legacy ladder: all continuation evidence fails → |sub: fork + // (the #5517/#5656 drift cluster). Declared: the raw id IS the + // identity — one session, no fork, blocks never orphan. + const r = await h.dshSend("conv7", { model: "fake-a", max_completion_tokens: 8192, stream: false, messages: [{ role: "system", content: MAIN_SYSTEM + " (v2 instructions)" }, { role: "user", content: "completely different fresh context" }, { role: "user", content: "keep going" }] }, undefined, "conversation"); + assert.equal(r.status, 200); + assert.equal(resolveConversation("conv7").session?.stats.requests ?? 0, 2, "both turns live on the raw session"); + assert.deepEqual(await h.subSessions("conv7"), [], "no |sub: drift lane off declared traffic"); + } finally { + await h.close(); + } +}); + +test("v2: the conversation sentinel is a MAIN turn, not a side request", async () => { + const h = await harness(); + try { + // Without the sentinel normalization the raw header value would + // declare this a sidecar and the kernel would see zero requests — + // the full pipeline running proves the split (side declaration vs + // identity declaration) holds on one header. + const body = { model: "fake-a", max_completion_tokens: 8192, stream: false, messages: [{ role: "system", content: MAIN_SYSTEM }, { role: "user", content: "hello" }], tools: [{ type: "function", function: { name: "bash", description: "run a command", parameters: { type: "object", properties: {} } } }] }; + const r = await h.dshSend("conv8", body, undefined, "conversation"); + assert.equal(r.status, 200); + assert.equal(resolveConversation("conv8").session?.stats.requests ?? 0, 1, "sentinel rides the main pipeline (not the side passthrough)"); + } finally { + await h.close(); + } +}); diff --git a/tests/side-lane-truth-table.test.ts b/tests/side-lane-truth-table.test.ts index 2630bcd4ea..200d638735 100644 --- a/tests/side-lane-truth-table.test.ts +++ b/tests/side-lane-truth-table.test.ts @@ -1,6 +1,7 @@ import assert from "node:assert/strict"; import test from "node:test"; import { demoteGate, DSH_TITLE_SYSTEM_PREFIX, isDshTitleRequest, isSideRequest, resolveSideLane, SIDE_REQUEST_AGENTS, SIDE_REQUEST_MAX_TOKENS } from "../src/server/side-request.ts"; +import { PLUGIN_PURPOSE_CONVERSATION, pluginPurposeHeader } from "../src/plugin.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 @@ -213,3 +214,26 @@ test("isSideRequest: declared purpose flips the two pre-#6880 blind shapes (4096 assert.equal(isSideRequest(dshTitleBody(4096), undefined, ""), false); assert.equal(isSideRequest({ max_output_tokens: 384_000 }, undefined, ""), false); }); + +// #6880 v2 wire: the post-#6883 dsh plugin stamps the purpose header on EVERY +// dispatch — real purposes for aux calls, the "conversation" sentinel for +// main turns (dsh leaves GenerateOptions.purpose unset for ordinary +// conversation, so no real dsh purpose can equal it). handle.ts normalizes +// the sentinel back to "absent" before the side classification, so the +// table above stays byte-identical for main traffic while PRESENCE of any +// value is the declaration that flips the persona-order lanes to the raw +// session id. These pins lock that split: the sentinel behaves EXACTLY like +// the absent header here, and only a non-sentinel value declares a side. +test("isSideRequest: the conversation sentinel normalizes to absent; only real purposes declare side", () => { + assert.equal(PLUGIN_PURPOSE_CONVERSATION, "conversation"); + const shapes = [dshTitleBody(4096), { max_output_tokens: 384_000 }, { model: "fake-a", messages: [{ role: "user", content: "x" }] }]; + for (const body of shapes) { + // reference: normalization exactly as handle.ts applies it + const sentinelSeen = pluginPurposeHeader({ "x-bili-plugin": "dsh", "x-bili-plugin-purpose": PLUGIN_PURPOSE_CONVERSATION }); + const normalized = sentinelSeen === PLUGIN_PURPOSE_CONVERSATION ? undefined : sentinelSeen; + assert.equal(normalized, undefined, "the sentinel normalizes to the absent header"); + assert.equal(isSideRequest(body, undefined, normalized), isSideRequest(body, undefined, undefined), "sentinel-normalized == absent"); + // and a real purpose on the same body still declares side + assert.equal(isSideRequest(body, undefined, "session-title"), true); + } +});