From ad8cc2db65be6ba24dd7a3ac06dd9c48dbc0e988 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Wed, 21 Oct 2026 12:49:37 +0800 Subject: [PATCH] dsh: carry session identity on the request object via llm/stream (#4082) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The LlmRuntime stamps options.sessionId on every dispatch and documents the llm/stream waterfall as the interception point. Wrap each stream's iteration in an AsyncLocalStorage keyed by that sid so adapter fetches read attribution that follows the request, not the async chain — retry chains, agentless side-calls (compaction, session-title), disposed initiator scopes, and host runtime gaps all stay attributed (#2975, #3918, #4082). Attribution order: llm/stream sid first, ambient currentInitiator fallback, the negotiated recall bridge (#4094) stays as the last resort. Agentless dispatches without a sid pass through untouched. --- CHANGELOG.md | 8 + src/agent/dsh-native.ts | 76 +++++- tests/dsh-llm-stream-attribution.test.ts | 297 +++++++++++++++++++++++ 3 files changed, 380 insertions(+), 1 deletion(-) create mode 100644 tests/dsh-llm-stream-attribution.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 5650fcaff5..b0618a7bff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ## [Unreleased] +### dsh 归因正面修:llm/stream 会话身份随请求对象(#4082) + +- dsh 的 LlmRuntime 在每次派发上盖章 `options.sessionId`(主循环/压缩/标题生成,重试重发也重新盖章),且把 llm/stream waterfall 文档化为官方拦截点。插件现在把每条流的迭代包进以该 sid 为键的 AsyncLocalStorage:适配器的 fetch 读到的会话身份**跟随请求对象,不再依赖异步链形状**——429 重试链、无 initiator 的 agentless 侧调用(compaction/标题)、已销毁的 initiator 作用域、宿主运行时缺口(#2975/#3918/#4082)全部免疫。 +- 归因读取顺序:llm/stream sid(先,派发自己的真相)→ `ctx.agents.currentInitiator()`(回退)→ 协商式指纹桥(PR #4094,兜底保留)。压缩侧调用天然带归因,喂 dsh-authored compaction。 +- 无 `sessionId` 的 agentless 车道(auto-review)原样透传,现有阶梯决定。 +- 一次性可见性:`bili-native-dsh: llm/stream attribution active — session id rides the request object (#4082)`。 + + - dsh 归因丢失协商回退(#4082/#3918/#2975):宿主重试/切模型/睡眠恢复的请求脱离 agent 执行链,归因为空时此前直接裸发上游(无压缩无护栏,且会话永久失守)。现在 shim 在归因正常时学习「车道 → 会话 + 边界指纹」(该次请求最后一条消息),归因丢失时若请求历史包含某已知边界且唯一命中,则由 takeover 门认领并原子补盖 x-bili-plugin-conversation(#3370 纪律),请求照常走插件车道;fork 从中间分叉不含边界 → 不认,行为与今天逐字节一致;无证据/多义 → 维持直发。每个 2xx 响应新增 x-bili-session 头(协议级,配合客户端可自持身份,见 PLUGIN.md);认领事件进 client 日志可见。 ### 压缩 diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 61505ed004..a77c0bfa05 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -42,6 +42,7 @@ // boundary is left to the kernel's natural ingest diff (#395 gap, acceptable: // manual /compact is rare and auto mode is off). +import { AsyncLocalStorage } from "node:async_hooks"; import { readFileSync } from "node:fs"; import { homedir } from "node:os"; import path from "node:path"; @@ -153,7 +154,7 @@ export type PluginContext = { // cordis ctx.on covers both shapes — webserver/index-inject gathers // per-startup rows for the web index (__BILI__ global for the settings // entry), while session/created and session/event feed the title channel. - on?: (event: string, listener: ((table: Array<{ kind: string; name?: string; value?: unknown }>) => void) | ((session: { id?: unknown }, event: { type?: unknown; data?: { title?: unknown } }) => void) | ((raw: unknown, next: () => unknown) => unknown), options?: { global?: boolean }) => void; + on?: (event: string, listener: ((table: Array<{ kind: string; name?: string; value?: unknown }>) => void) | ((session: { id?: unknown }, event: { type?: unknown; data?: { title?: unknown } }) => void) | ((raw: unknown, next: () => unknown) => unknown) | ((options: { sessionId?: unknown }, next: () => AsyncIterable) => AsyncIterable), options?: { global?: boolean }) => void; // #3354: cordis service registry — the load-time compaction-disable overlay // publishes its gate service here so the preset inject entries latch on it. // Optional: hosts without a registry skip the gate without breaking boot. @@ -1028,7 +1029,53 @@ type GateAttribution = | { state: "none"; initiatorPresent: boolean } | { state: "threw"; message: string }; +// #4082: session identity carried by the REQUEST OBJECT, not the async chain. +// dsh's `llm/stream` waterfall stamps `options.sessionId` on every model +// dispatch — the main loop's buildRequest, the compaction side-calls, the +// session-title lane — and re-stamps it on every retry re-send (retry is a +// `step()` `continue`, not an external queue). The LlmRuntime class documents +// the waterfall as THE interception point, and first-party plugins +// (dsh-session-checkpoint-policy) already listen there. Wrapping each +// stream's iteration in OUR scope gives the adapter's internal fetches an +// id that cannot be lost to chain shape: agentless side-calls (no initiator +// but a sessionId), disposed initiator scopes (currentInitiator throws), or +// host runtime gaps that reroute the ambient context. +const sidScope = new AsyncLocalStorage(); + +let noticedScopedAttribution = false; + +/** Re-enter the sid scope on every iterator advance: generator bodies + * resume in the consumer's dynamic context, so each `next()` must run the + * adapter's continuation (its awaits, and any fetches it starts) inside the + * same scope the dispatch carried. */ +function wrapScopedIterable(iter: AsyncIterable, sid: string): AsyncIterable { + return { + [Symbol.asyncIterator]: () => { + const it = iter[Symbol.asyncIterator](); + const run = (op: () => Promise): Promise => sidScope.run(sid, op); + return { + next: () => run(() => it.next()), + return: (value?: unknown) => run(() => it.return !== undefined ? it.return(value as never) : Promise.resolve({ done: true, value: value as T })), + throw: (reason?: unknown) => run(() => it.throw !== undefined ? it.throw(reason as never) : Promise.reject(reason)), + }; + }, + }; +} + function attributionOf(ctx: PluginContext): GateAttribution { + // #4082: the llm/stream-carried sid outranks the ambient initiator — it + // is the dispatch's own truth (the loop stamps the session it is + // driving), and it survives exactly the shapes the ambient reading does + // not (agentless compaction/title lanes, disposed scopes, retried + // re-sends whose initiator scope went away between attempts). + const scoped = sidScope.getStore(); + if (typeof scoped === "string" && scoped.length > 0) { + if (!noticedScopedAttribution) { + noticedScopedAttribution = true; + persistClientEvent("bili-native-dsh: llm/stream attribution active — session id rides the request object (#4082)"); + } + return { state: "ok", sid: scoped }; + } try { const init = ctx.agents?.currentInitiator?.(); const sid = init?.session?.id; @@ -1498,6 +1545,33 @@ export function apply(ctx: PluginContext): void { path: "/__bili", handler: (req: unknown, res: RelayRes): void | Promise => relayAdminRequest(req, res), }; + // #4082: the positive attribution fix. dsh routes EVERY streaming model + // dispatch — first attempts, llm-retry re-sends, model switches, the + // compaction and session-title side-calls — through the `llm/stream` + // waterfall, and the loop stamps `options.sessionId` on each request + // object (types.d.ts: "Session identity stamped by the loop for request + // routing"). Listening there and wrapping the stream's iteration in our + // sidScope means the adapter's fetches read a session id that survives + // every chain shape the ambient initiator does not. Waterfall contract: + // always call next() and faithfully pass the iterable through — a + // listener that drops it would cut the stream (same discipline as the + // first-party checkpoint-policy plugin). Dispatches WITHOUT a sessionId + // (auto-review's agentless lane) pass through untouched and keep riding + // the existing attribution ladder (ambient → negotiated recall). + if (typeof ctx.inject === "function" && typeof ctx.on === "function") { + const onEvent = ctx.on.bind(ctx); + ctx.inject(["llm"], () => { + onEvent("llm/stream", (options: { sessionId?: unknown }, next: () => AsyncIterable) => { + const sid = typeof options?.sessionId === "string" && options.sessionId.length > 0 ? options.sessionId : undefined; + if (sid === undefined) return next(); + // next() may start the adapter's fetch eagerly (inside this + // call), and lazy continuations resume on each advance — the + // run() below covers the former, wrapScopedIterable the latter. + return sidScope.run(sid, () => wrapScopedIterable(next(), sid)); + }); + }); + } + if (typeof ctx.inject === "function") { // Cordis inject: fires once the service is available, re-runs on // change; the effect disposes the prior registration on each re-run diff --git a/tests/dsh-llm-stream-attribution.test.ts b/tests/dsh-llm-stream-attribution.test.ts new file mode 100644 index 0000000000..0db9448dfa --- /dev/null +++ b/tests/dsh-llm-stream-attribution.test.ts @@ -0,0 +1,297 @@ +/** + * #4082: dsh llm/stream attribution — session identity rides the request + * object, not the async chain. + * + * dsh's LlmRuntime stamps options.sessionId on every dispatch (main loop + * buildRequest, compaction, session-title) and documents the llm/stream + * waterfall as the interception point. The plugin wraps each stream's + * iteration in an AsyncLocalStorage keyed by that sid, so adapter fetches + * stay attributed through exactly the shapes the ambient initiator reading + * loses: retry chains, agentless side-calls, disposed initiator scopes. + */ +import { strict as assert } from "node:assert"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import test, { after, before } from "node:test"; + +// Same hermetic posture as the sibling dsh tests: nothing in this file +// should depend on this machine's real proxy/env wiring. +delete process.env.BILI_PROVIDER_REWRITES; +delete process.env.BILI_NATIVE_DSH; +delete process.env.BILLION_CONTEXT_PLUGIN; + +import { + apply, + _resetRegisterForTest, + _settleNativeForTest, + _stateBeforeSendForTest, + _stateHeadersForTest, + _stateTakeoverGateForTest, + type PluginContext, +} from "../src/agent/dsh-native.ts"; +import { __resetConversationRecallForTests } from "../src/agent/conversation-recall.ts"; + +const MODEL_URL = "https://api.anthropic.com/v1/messages"; + +interface RegisteredToolLike { + name: string; +} + +interface CtxHandle { + ctx: PluginContext; + events: Map unknown) => unknown>>; + setInitiator: (sid: string | undefined) => void; + registered: RegisteredToolLike[]; +} + +function makeCtx(): CtxHandle { + const events = new Map unknown) => unknown>>(); + const registered: RegisteredToolLike[] = []; + let currentSid: string | undefined; + const ctx = { + inject: (deps: readonly string[], cb: (sub: unknown) => void) => { + // Only the llm service matters here; other inject targets + // (webServer, agentDefaultModel, …) stay cold like the sibling + // test harness. + if (deps.includes("llm")) cb({}); + }, + on: (name: string, listener: (payload: unknown, next: () => unknown) => unknown) => { + const list = events.get(name) ?? []; + list.push(listener); + events.set(name, list); + }, + emit: () => undefined, + logger: { info: () => undefined, warn: () => undefined, error: () => undefined }, + tools: { register: (tool: RegisteredToolLike) => void registered.push(tool) }, + commands: { register: () => undefined }, + agents: { + currentInitiator: () => + currentSid === undefined ? undefined : { session: { id: currentSid } }, + }, + setInitiator: (sid: string | undefined) => { + currentSid = sid; + }, + } as unknown as PluginContext & { setInitiator: (sid: string | undefined) => void }; + return { ctx, events, setInitiator: (sid) => ctx.setInitiator(sid), registered }; +} + +async function waitFor(predicate: () => boolean, what: string): Promise { + const deadline = Date.now() + 15_000; + while (!predicate()) { + if (Date.now() > deadline) throw new Error(`timeout waiting for ${what}`); + await new Promise((resolve) => setTimeout(resolve, 50)); + } +} + +/** + * Runs the registered llm/stream waterfall over a base adapter (the role + * dsh's real LlmRuntime plays): composes the listeners like cordis does — + * outermost first, each receives (options, next) — and returns the final + * iterable so tests can advance it and assert scope retention across + * yields (the shape of an adapter retry loop). + */ +function runWaterfall( + events: Map unknown) => unknown>>, + options: Record, + base: () => AsyncIterable, +): AsyncIterable { + const listeners = events.get("llm/stream") ?? []; + const composed = listeners.reduceRight<(payload: unknown, next: () => unknown) => unknown>( + (acc, listener) => (payload, next) => listener(payload, () => acc(payload, next)), + () => base(), + ); + return composed(options, () => base()) as AsyncIterable; +} + +async function withSetup(fn: (handle: CtxHandle) => Promise): Promise { + __resetConversationRecallForTests(); + const home = mkdtempSync(join(tmpdir(), "dsh-llm-stream-")); + const saved: Record = {}; + for (const key of ["BILLION_CONTEXT_PROXY", "DSH_HOME", "BILLION_CONTEXT_PLUGIN", "BILI_NATIVE_DSH", "BILI_PROVIDER_REWRITES"]) { + saved[key] = process.env[key]; + } + const proxy = await startMockProxy(); + process.env.BILLION_CONTEXT_PROXY = proxy.origin; + process.env.DSH_HOME = home; + _resetRegisterForTest(proxy.origin); + const handle = makeCtx(); + try { + await apply(handle.ctx); + await waitFor(() => handle.registered.length === 1, "attach tool registration"); + await fn(handle); + } finally { + await _settleNativeForTest(); + proxy.server.close(); + proxy.server.closeIdleConnections?.(); + for (const [key, value] of Object.entries(saved)) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + rmSync(home, { recursive: true, force: true }); + } +} + +interface MockProxy { + origin: string; + server: import("node:http").Server; +} + +async function startMockProxy(): Promise { + const { createServer } = await import("node:http"); + const server = createServer((req, res) => { + const url = req.url ?? ""; + if (url === "/__bili/plugin/manifest") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + version: "0.1.119", + tools: { anthropic: [{ name: "compress", description: "Compress a range of messages", input_schema: { type: "object", properties: { summary: { type: "string" } }, required: ["summary"] } }] }, + })); + return; + } + if (url === "/__bili/plugin/session-name") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({})); + return; + } + res.writeHead(404, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "not found" })); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address(); + if (address === null || typeof address === "object" && !("port" in address)) throw new Error("no port"); + const port = typeof address === "object" && "port" in address ? address.port : 0; + return { origin: `http://127.0.0.1:${port}`, server }; +} + +function headersOf(init: { headers?: unknown }): Record { + const raw = init.headers; + if (raw instanceof Headers) { + const out: Record = {}; + for (const [k, v] of raw.entries()) out[k] = v; + return out; + } + if (Array.isArray(raw)) return Object.fromEntries(raw as [string, string][]); + if (raw && typeof raw === "object") return { ...(raw as Record) }; + return {}; +} + +test("llm/stream sid survives a lost ambient initiator (retry shape)", async (t) => { + await withSetup(async (handle) => { + const gate = _stateTakeoverGateForTest(); + const beforeSend = _stateBeforeSendForTest(); + const headersFor = _stateHeadersForTest(); + assert.ok(gate, "takeover gate is installed after apply()"); + assert.ok(beforeSend, "beforeSend hook is installed after apply()"); + assert.ok(headersFor, "headersFor stamp is installed after apply()"); + const verdicts: Array = []; + const stamps: Array> = []; + // The "adapter": performs its work (fetch-shaped gate+beforeSend + // calls) before the first yield AND after resuming from it — the + // shape of an adapter-internal retry loop. + const base = async function* (): AsyncIterable { + const init: { headers?: Record } = { + headers: { "content-type": "application/json" }, + }; + verdicts.push(await gate(MODEL_URL, { bytes: 120 }, init)); + await beforeSend(MODEL_URL, init); + stamps.push({ ...(headersFor?.(MODEL_URL) ?? {}), ...headersOf(init) }); + yield 1; + // Post-yield resumption: the scope must re-enter on advance. + const init2: { headers?: Record } = { + headers: { "content-type": "application/json" }, + }; + verdicts.push(await gate(MODEL_URL, { bytes: 120 }, init2)); + await beforeSend(MODEL_URL, init2); + stamps.push({ ...(headersFor?.(MODEL_URL) ?? {}), ...headersOf(init2) }); + return; + }; + // No ambient initiator — the exact retry shape where the ambient + // reading lost attribution (#4082/#3918). + handle.setInitiator(undefined); + const stream = runWaterfall(handle.events, { sessionId: "sess-gamma", model: "deepseek-chat" }, base); + const it = stream[Symbol.asyncIterator](); + const first = await it.next(); + assert.deepEqual(first, { done: false, value: 1 }); + const second = await it.next(); + assert.ok(second.done, "generator completes"); + assert.equal(verdicts.length, 2); + assert.ok(verdicts[0] !== false && verdicts[0] !== undefined, "pre-yield gate accepts the scoped dispatch"); + assert.ok(verdicts[1] !== false && verdicts[1] !== undefined, "post-yield advance stays in scope"); + assert.equal(stamps[0]["x-bili-plugin-conversation"], "sess-gamma"); + assert.equal(stamps[1]["x-bili-plugin-conversation"], "sess-gamma"); + }); +}); + +test("agentless dispatch without sessionId passes through unattributed", async (t) => { + await withSetup(async (handle) => { + const gate = _stateTakeoverGateForTest(); + assert.ok(gate); + let gateCalls = 0; + const base = async function* (): AsyncIterable { + gateCalls++; + yield 7; + }; + handle.setInitiator(undefined); + const stream = runWaterfall(handle.events, { model: "deepseek-chat" }, base); + const results: number[] = []; + for await (const chunk of stream) results.push(chunk); + assert.deepEqual(results, [7], "values pass through untouched"); + // The generator never called the gate; nothing is stamped. + assert.equal(gateCalls, 1); + }); +}); + +test("request-object sid outranks the ambient initiator", async (t) => { + await withSetup(async (handle) => { + const gate = _stateTakeoverGateForTest(); + const beforeSend = _stateBeforeSendForTest(); + const headersFor = _stateHeadersForTest(); + assert.ok(gate); + assert.ok(beforeSend); + assert.ok(headersFor); + let stamped: Record = {}; + const base = async function* (): AsyncIterable { + const init: { headers?: Record } = { headers: {} }; + await gate(MODEL_URL, { bytes: 80 }, init); + await beforeSend(MODEL_URL, init); + stamped = { ...(headersFor?.(MODEL_URL) ?? {}), ...headersOf(init) }; + yield 1; + }; + handle.setInitiator("sess-ambient"); + const stream = runWaterfall(handle.events, { sessionId: "sess-dispatch" }, base); + const it = stream[Symbol.asyncIterator](); + await it.next(); + // The dispatch's own sessionId is the truth (the loop stamped the + // session it is driving); the ambient initiator is the fallback. + assert.equal(stamped["x-bili-plugin-conversation"], "sess-dispatch"); + }); +}); + +test("agentless compaction dispatch (sessionId, no initiator) is attributed", async (t) => { + await withSetup(async (handle) => { + const gate = _stateTakeoverGateForTest(); + const beforeSend = _stateBeforeSendForTest(); + const headersFor = _stateHeadersForTest(); + assert.ok(gate); + assert.ok(beforeSend); + assert.ok(headersFor); + let verdict: boolean | string | { action: string } | undefined; + let stamped: Record = {}; + const base = async function* (): AsyncIterable { + const init: { headers?: Record } = { headers: {} }; + verdict = await gate(MODEL_URL, { bytes: 200 }, init); + await beforeSend(MODEL_URL, init); + stamped = { ...(headersFor?.(MODEL_URL) ?? {}), ...headersOf(init) }; + yield 1; + }; + // dsh-compaction-basic runMaintenance: no initiator, sessionId + + // purpose stamped on the dispatch. + handle.setInitiator(undefined); + const stream = runWaterfall(handle.events, { sessionId: "sess-compaction", purpose: "compaction" }, base); + const it = stream[Symbol.asyncIterator](); + 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"); + }); +});