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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag

- **msgid 钉进身份层**:插件增量轮体带 `msgIds` 数组(与 messages 平行)——客户端消息 id 直接成为内核/存储身份(m.id、store msg_id、折叠覆盖锚),不再走内容派生 h_ 哈希。校验严格(非法条目/重复/`acp_summary_` 保留前缀/空白控制字符→整轮降级回内容派生;非 resync 轮重复认领已存在 id→400 拒绝,身份投毒绝不入库);装配体经管线转发前剥离 `biliMsgIds` 字段(上游零感知)。对账=纯函数 `alignIdSet`(msg消失=compressed(活跃块覆盖或压缩签名佐证)/deleted;重叠率<0.5 且双侧≥8 条=世代翻转;edits=同 id 异签名)。
- **渲染端点**:POST `__bili/plugin/render` {conversationId, untilSeq?}(rollout `pluginLaneServerHistory` 门控,缺省 0%)——服务端正史切片出站前缀,折叠覆盖渲染为 acp_summary 锚,degrade-never-guess(任何失步非 2xx,宿主回退发本地原文);探针 `GET /__bili/plugin/turn` 新增 `serverHistory` 字段。
- **四宿主 msgIds 接线**(dsh/pi/opencode/omp,`#4491` 宿主侧):增量轮体从宿主拿到自己的稳定消息 id 并随 `msgIds` 上车——dsh=`llm/stream` options.messages[].id(挂进既有 #4082 ALS 作用域,发送时 1:1 对齐);pi=扩展层 CoreMessage.id(`pi-msgids.ts` 模块,`validateMsgIds` 直用服务端校验);opencode=V1 `messages.transform` info.id(V2 http.request 只见 wire,降级为内容派生);omp=`sessionManager.getBranch()` 树条目 id(before_provider_request 快照 hint+count/role 对齐)。全或无纪律贯穿:任一消息拿不到合格 id(缺失/重复/保留前缀/控制字符/数量不齐)整轮降级不带字段,宁可降级绝不错配;omp 另持 msgidClaims 本地镜像防重试撞 400。桩:`tests/{pi,dsh,opencode,omp}-msgids.test.ts`(4+5+5+6 例)。
- **车道流 e2e 桩**(`tests/msgid-lane-flow.test.ts` 5 例):钉 id 落行+编辑=追加新行(绝不换回旧 seq,409-loop 契约)/undo-redo 同内容=身份 join 原行复活零新行/delta 重复认领=400/divergent resync 后无 409 循环/无 msgIds 轮=字节等价对照组(含 rollout 门开时管线重钉、门关时仅内存 canonical 钉定的两态验证)。

- **seq ledger foundations — block interval bounds + ledger-numbered refs (#4316 layer 3, phase 1).** Block coverage now carries its ledger interval: `planIngest` fills `blocks.start_seq/end_seq` with the min/max unified-store seq of the covered messages (`-1/-1` when no rows resolve) — coverage bounds are id-independent, so client-side byte churn can never invalidate them. `CoreMessage.ledgerSeq` sidecar: store-served views (snapshot-render prefix rows) stamp the row's `(session, seq)`; `assignRefs` issues the ref as the direct display of that number (seq+1 in the 1-based display space), advancing the allocation cursor past the stamp so counter-issued refs never collide with future stamps. A store-served message's number is assigned once at ingest and is structurally immune to the #4316 churn family (model-switch re-stamps, image re-encodes re-minting every content-hash id). Stakes first in `tests/seq-ledger.test.ts`: interval bounds (S1), ref-set stability across a full id re-mint under snapshot render (S2), and the kernel seam (S3). Follow-ups: incremental-lane stamping, fork `cloneRows` number-preserving copy, and the one-time legacy re-mint window handling — tracked in #4316.
Expand Down
69 changes: 60 additions & 9 deletions src/agent/dsh-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1146,19 +1146,57 @@ type GateAttribution =
// 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>();
// #4491: the same options also carry the host session message ids
// (`options.messages[].id` — dsh stamps a stable UUID on every message),
// so the scope payload grew from a bare sid into { sid, msgIds } and the
// incremental conversion can pin the turn's messages to the host's own
// identity (server-side canonical rows keyed by msgIds instead of
// content-derived hashes). All-or-nothing per dispatch: a sidecar whose
// messages carry no ids (compaction/title shapes) captures none, and the
// field simply stays off that turn — degradation, never a guess.
interface LlmDispatchScope {
sid: string;
msgIds: string[] | undefined;
}
const dispatchScope = new AsyncLocalStorage<LlmDispatchScope>();

let noticedScopedAttribution = false;

/** Re-enter the sid scope on every iterator advance: generator bodies
/** #4491: extract the host message ids from llm/stream `options.messages`,
* all-or-nothing. Mirrors validateMsgIds in src/msgid-pin.ts (the 400-class
* conditions — bad entry, reserved prefix, control/space, duplicate) so a
* hostile/odd host id can never hard-fail the turn; the host-side helper
* must not import server modules (same discipline as the marker-header
* twin in incremental-client.ts), so the constants are duplicated — keep
* them in sync. The COUNT check is deliberately absent: alignment against
* the actually-sent wire messages happens at send time (maybeIncrementalNative).
*/
function hostMsgIdsOf(messages: unknown): string[] | undefined {
if (!Array.isArray(messages) || messages.length === 0) return undefined;
const ids: string[] = [];
const seen = new Set<string>();
for (const m of messages) {
if (m === null || typeof m !== "object") return undefined;
const id = (m as { id?: unknown }).id;
if (typeof id !== "string" || id.length === 0 || id.length > 512) return undefined;
if (id.startsWith("acp_summary_")) return undefined; // kernel anchor namespace
if (/[\s\p{C}]/u.test(id)) return undefined;
if (seen.has(id)) return undefined;
seen.add(id);
ids.push(id);
}
return ids;
}

/** Re-enter the dispatch 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> {
function wrapScopedIterable<T>(iter: AsyncIterable<T>, scope: LlmDispatchScope): AsyncIterable<T> {
return {
[Symbol.asyncIterator]: () => {
const it = iter[Symbol.asyncIterator]();
const run = <R>(op: () => Promise<R>): Promise<R> => sidScope.run(sid, op);
const run = <R>(op: () => Promise<R>): Promise<R> => dispatchScope.run(scope, 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 })),
Expand All @@ -1174,13 +1212,13 @@ function attributionOf(ctx: PluginContext): GateAttribution {
// 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) {
const scoped = dispatchScope.getStore();
if (scoped !== undefined && scoped.sid.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 };
return { state: "ok", sid: scoped.sid };
}
try {
const init = ctx.agents?.currentInitiator?.();
Expand Down Expand Up @@ -1693,13 +1731,17 @@ export function apply(ctx: PluginContext): void {
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>) => {
onEvent("llm/stream", (options: { sessionId?: unknown; messages?: unknown }, next: () => AsyncIterable<unknown>) => {
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) };
// 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));
return dispatchScope.run(scope, () => wrapScopedIterable(next(), scope));
});
});
}
Expand Down Expand Up @@ -2237,12 +2279,21 @@ export function apply(ctx: PluginContext): void {
if (!(await probeIncrementalTurns(base))) return;
sweepStale(incPendingAcks, INC_PENDING_TTL_MS);
const sid = attr.sid;
// #4491: pin the turn to the host's own message ids — the ids captured
// on the llm/stream options are only trustworthy when they line up
// 1:1 with the wire messages ACTUALLY being sent (the adapter may
// drop/reshape entries). Equal count ⇒ same order (the host renders
// its session messages in order); anything else omits the field —
// degrade over mis-pair, never pin a wrong id.
const scopedIds = dispatchScope.getStore()?.msgIds;
const msgIds = scopedIds !== undefined && scopedIds.length === (parsed.messages as unknown[]).length ? scopedIds : undefined;
const plan = planIncrementalTurn({
conversationId: sid,
path: "/v1/messages", // placeholder — the marker divert uses the ORIGINAL routed URL
messages: parsed.messages as IncrementalWireMessage[],
system: "system" in parsed ? parsed.system : undefined,
request: parsed as Record<string, unknown>,
msgIds,
});
if (plan.mode === "skip") return;
// #3370 single-flight (omp/opencode pattern; kept over #3329's
Expand Down
106 changes: 102 additions & 4 deletions src/agent/incremental-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,37 @@ export interface IncrementalWireMessage {
content: unknown;
}

/** #4491 host msgids: the host's stable message ids for the NEXT outgoing
* request of a conversation, as (id, role) pairs in session order. Stashed
* by the host-side extension event (it owns the sessionManager) and read
* back by the fetch-layer planner (it owns the actually-sent messages);
* the consumer aligns by count+role against the real wire set and refuses
* the whole field on any mismatch — a misaligned id would pin the wrong
* identity server-side, while a missing field merely falls back to
* content-derived ids. */
export interface HostMsgIdHintEntry {
id: string;
role: string;
}

const HOST_MSGID_HINT_TTL_MS = 30_000;
const hostMsgIdHints = new Map<string, { entries: HostMsgIdHintEntry[]; at: number }>();

/** Record the branch-entry (id, role) snapshot for a conversation's next
* outgoing request. Overwrites per request — one hint per conversation,
* always the generation of the payload being dispatched. */
export function noteHostMsgIdHint(conversationId: string, entries: HostMsgIdHintEntry[]): void {
sweepStale(hostMsgIdHints, HOST_MSGID_HINT_TTL_MS);
hostMsgIdHints.set(conversationId, { entries, at: Date.now() });
}

/** The stashed hint for a conversation, or undefined when none is fresh. */
export function hostMsgIdHintFor(conversationId: string): HostMsgIdHintEntry[] | undefined {
const entry = hostMsgIdHints.get(conversationId);
if (entry === undefined || Date.now() - entry.at >= HOST_MSGID_HINT_TTL_MS) return undefined;
return entry.entries;
}

interface ConvState {
/** Last server ack (x-bili-applied-seq) — flat message count. */
sinceSeq: number;
Expand All @@ -185,6 +216,53 @@ interface ConvState {

const convs = new Map<string, ConvState>();

// #4491 host msgids: ids this client has already put on an envelope for the
// conversation — the client-side mirror of the lane's canonical id set. The
// lane HARD-FAILS a non-resync turn (400) whose msgIds reuse a live canonical
// id ("msgIds reuse an existing canonical id"), so a delta candidate already
// in this set degrades to "no field" instead of killing the request: the
// realistic trigger is a host retry re-planning the same delta after the
// first attempt may have landed server-side. A resync/seed replaces the set
// wholesale, exactly like the server replaces the canonical.
const msgidClaims = new Map<string, Set<string>>();

/** Local mirror of the lane's validateMsgIds reject classes
* (src/msgid-pin.ts — keep in sync; the host bundle must not import server
* modules). The lane hard-fails a turn on a poisoned id array, so the host
* screens candidates BEFORE they ride the envelope: anything the server
* would reject degrades to "no field" here (content-derived ids) instead of
* a 400 that kills the whole request. */
const MSGID_MAX_LEN = 512;
const MSGID_RESERVED_PREFIX = "acp_summary_";
const MSGID_BAD_CHARS = /[\s\p{C}]/u;

function screenMsgIds(ids: unknown): ids is string[] {
if (!Array.isArray(ids) || ids.length === 0) return false;
const seen = new Set<string>();
for (const v of ids) {
if (typeof v !== "string" || v.length === 0 || v.length > MSGID_MAX_LEN) return false;
if (v.startsWith(MSGID_RESERVED_PREFIX)) return false;
if (MSGID_BAD_CHARS.test(v)) return false;
if (seen.has(v)) return false;
seen.add(v);
}
return true;
}

/** Delta-turn claim gate: undefined when any candidate id was already sent
* on an earlier envelope of this conversation (the lane would 400 — see
* msgidClaims); otherwise the ids are claimed and returned. */
function claimableDeltaMsgIds(conversationId: string, ids: string[]): string[] | undefined {
const claims = msgidClaims.get(conversationId);
if (claims === undefined) {
msgidClaims.set(conversationId, new Set(ids));
return ids;
}
for (const id of ids) if (claims.has(id)) return undefined;
for (const id of ids) claims.add(id);
return ids;
}

const PROBE_TTL_MS = 30_000;
const probeCache = new Map<string, { ok: boolean; at: number }>();

Expand Down Expand Up @@ -287,9 +365,18 @@ interface PlanArgs {
/** Anthropic request params from the original body (model, max_tokens…);
* optional — missing params ride the lane's defaults. */
request?: Record<string, unknown>;
/** #4491: host-stable message ids aligned 1:1 with `messages` (same
* length), or undefined when the host cannot vouch for every entry.
* Carried onto the envelope as `msgIds` parallel to the messages the
* mode actually sends (seed/resync: the full set; delta: the suffix),
* so the server pins the client's identity instead of content-derived
* hashes. All-or-nothing: a length mismatch, a screen failure, or an
* already-claimed id drops the WHOLE field — never a partial or
* misaligned array. */
msgIds?: string[];
}

function buildBody(args: PlanArgs, sinceSeq: number, messages: IncrementalWireMessage[], resync: boolean, includeSystem: boolean): string {
function buildBody(args: PlanArgs, sinceSeq: number, messages: IncrementalWireMessage[], resync: boolean, includeSystem: boolean, msgIds?: string[]): string {
// `request` is optional at the call sites (a host may plan with only
// messages+system); missing params simply ride their lane defaults.
const src = args.request ?? {};
Expand All @@ -305,6 +392,9 @@ function buildBody(args: PlanArgs, sinceSeq: number, messages: IncrementalWireMe
};
if (resync) body.resync = true;
if (includeSystem) body.system = args.system === undefined ? null : args.system;
// #4491: only a provably-parallel, non-empty array rides the envelope —
// an empty delta (pure sticky-system update) pins nothing and stays off.
if (msgIds !== undefined && msgIds.length === messages.length && msgIds.length > 0) body.msgIds = msgIds;
return JSON.stringify(body);
}

Expand All @@ -313,6 +403,9 @@ function buildBody(args: PlanArgs, sinceSeq: number, messages: IncrementalWireMe
export function planIncrementalTurn(args: PlanArgs, forceResync = false): IncrementalPlan {
const state = convs.get(args.conversationId);
if ((state !== undefined && state.dead) || args.messages.length === 0) return { mode: "skip" };
// #4491: vouched ids must be exactly parallel to the outgoing set and
// pass the local screen; anything else means "no field" this turn.
const fullIds = args.msgIds !== undefined && args.msgIds.length === args.messages.length && screenMsgIds(args.msgIds) ? args.msgIds : undefined;
const systemJson = args.system === undefined ? undefined : JSON.stringify(args.system);
if (state === undefined || forceResync) {
const includeSystem = state === undefined || systemJson !== state.systemJson;
Expand All @@ -322,7 +415,8 @@ export function planIncrementalTurn(args: PlanArgs, forceResync = false): Increm
// empty (fresh conversation) or already live (interop with
// full-resend traffic: the client's view is authoritative and
// identical-or-newer). Only DELTA turns can 409.
return { mode: forceResync ? "resync" : "seed", body: buildBody(args, 0, args.messages, true, includeSystem) };
if (fullIds !== undefined) msgidClaims.set(args.conversationId, new Set(fullIds));
return { mode: forceResync ? "resync" : "seed", body: buildBody(args, 0, args.messages, true, includeSystem, fullIds) };
}
const hashes = args.messages.map(wireHash);
// Prefix alignment: everything previously acked must still lead the list.
Expand All @@ -332,7 +426,8 @@ export function planIncrementalTurn(args: PlanArgs, forceResync = false): Increm
if (state.sent[i] !== hashes[i]) { aligned = false; break; }
}
if (!aligned || hashes.length < state.sent.length) {
return { mode: "resync", body: buildBody(args, 0, args.messages, true, systemJson !== state.systemJson) };
if (fullIds !== undefined) msgidClaims.set(args.conversationId, new Set(fullIds));
return { mode: "resync", body: buildBody(args, 0, args.messages, true, systemJson !== state.systemJson, fullIds) };
}
if (hashes.length === state.sent.length) {
// Equal message set: normally a retry of the same turn → normal path.
Expand All @@ -344,9 +439,10 @@ export function planIncrementalTurn(args: PlanArgs, forceResync = false): Increm
}
return { mode: "skip" }; // retry of the same turn → normal path
}
const deltaIds = fullIds !== undefined ? claimableDeltaMsgIds(args.conversationId, fullIds.slice(state.sent.length)) : undefined;
return {
mode: "delta",
body: buildBody(args, state.sinceSeq, args.messages.slice(state.sent.length), false, systemJson !== state.systemJson),
body: buildBody(args, state.sinceSeq, args.messages.slice(state.sent.length), false, systemJson !== state.systemJson, deltaIds),
};
}

Expand Down Expand Up @@ -411,4 +507,6 @@ export function __resetIncrementalClientForTests(): void {
droppedParamWarned.clear();
droppedParamWarnPairs = 0;
originalBodies.clear();
hostMsgIdHints.clear();
msgidClaims.clear();
}
Loading
Loading