Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 日志可见。
### 压缩

Expand Down
76 changes: 75 additions & 1 deletion src/agent/dsh-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<unknown>) => AsyncIterable<unknown>), 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.
Expand Down Expand Up @@ -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<string>();

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<T>(iter: AsyncIterable<T>, sid: string): AsyncIterable<T> {
return {
[Symbol.asyncIterator]: () => {
const it = iter[Symbol.asyncIterator]();
const run = <R>(op: () => Promise<R>): Promise<R> => 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;
Expand Down Expand Up @@ -1498,6 +1545,33 @@ export function apply(ctx: PluginContext): void {
path: "/__bili",
handler: (req: unknown, res: RelayRes): void | Promise<void> => 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<unknown>) => {
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
Expand Down
Loading
Loading