diff --git a/docs/msgid-infrastructure.md b/docs/msgid-infrastructure.md index deb4f2fbf7..d216fc7fdd 100644 --- a/docs/msgid-infrastructure.md +++ b/docs/msgid-infrastructure.md @@ -1,6 +1,6 @@ # Message-ID lane infrastructure: the sessionid+msgid production/consumption map -> Status: companion doc for the gap fix mandated by the #5826 release review (owner ruling 2026-11-07). Identity-ladder doctrine lives in message-identity.md (kernel) and session-identity.md (session side); this file does ONE job — explain where sessionid+msgid, the cross-lane public substrate, is produced, how it travels, who validates it (six copies), where it is pinned, its five consumers, and its declared trust boundaries. Read this before touching any of it. +> Status: companion doc for the gap fix mandated by the #5826 release review (owner ruling 2026-11-07). Identity-ladder doctrine lives in message-identity.md (kernel) and session-identity.md (session side); this file does ONE job — explain where sessionid+msgid, the cross-lane public substrate, is produced, how it travels, who validates it (six copies), where it is pinned, its six consumers, and its declared trust boundaries. Read this before touching any of it. ## 1. What it is @@ -48,15 +48,16 @@ One-off closure from the review: `parseTailIds` alone lacked the reserved-prefix The unified-store `messages.msg_id` column is the ONLY id truth: ingestion pins validated ids into rows (T5 ruling: no parallel ledger; skeleton rows carry no ids). Ledger primary key `(session_id, msg_id, content_hash)`; canonical rows key on **MSGID, not content hash** (#4491); fork clones preserve numbers (#4316 L3 PR3/3 `cloneSessionRows`). -## 6. Consumers (five; what question each one asks of the ids) +## 6. Consumers (six; what question each one asks of the ids) | Consumer | Location | Question answered | Id-usage boundary | |---|---|---|---| -| ① storage-column identity | unified store `messages.msg_id` | join key for every settle/reconcile path | the single identity-space entry point; the other four's id evidence ultimately folds in here | +| ① storage-column identity | unified store `messages.msg_id` | join key for every settle/reconcile path | the single identity-space entry point; the other five's id evidence ultimately folds in here | | ② session-resolve last-resort rung #5719 | tryMsgIdRescue (src/session-resolve.ts ~:324; fork-exit call sites ~:432/:481/:501) | after bytes/tag evidence was destroyed (compaction deleted cells, system rewrite deleted fragments), is this request the SAME session? | ANY id overlap under the same raw key ⇒ same session; NOTE: no LANE attribution (§7 boundary B) | | ③ settlement membership probe #4805 | probeRequestMembership (src/request-membership.ts; call sites src/server/prepare-openai.ts:597, prepare-anthropic.ts:640; consumed cache-ledger.ts:1866) | was this id admitted by this session's ledger? needle = latest admitted id | **pure evidence — never enters the identity space**, never re-keys/forks; worst case one unsettled cycle, self-heals | | ④ incremental-lane canonical rows #4491/#4834 | src/incremental-lane.ts (validateMsgIds :623, claim-reuse 400 :628, cache-control rekey :631-635; dsh-side burned-claim pre-screen ~:2584) | which canonical rows did this delta turn append? | claim semantics: re-sent claimed id = retry-storm signal, turn 400s | | ⑤ watermark tail continuity v2-v4 #5403/#5418 | PF_TAIL_IDS_HEADER (src/server/fastpath.ts:48) | does the client's delta window still originate where the server seed says? | under v4 width self-consistency (len=T−K+1, re-verified server-side) the count echo degrades to diagnostic-only; ids prove WINDOW ORIGIN ONLY, never prefix integrity (owner ruling 2026-11-02; §7.4 blind spot stands, §7 boundary A) | +| ⑥ fork-adopt msgid track #6262 | tryForkAdoption(src/agent/fork-adopt.ts — client slot-matches collected host ids against the parent snapshot's rawIds) + handlePluginFork(src/plugin.ts — request.prefixMsgIds[k] === prefix[k].rawId over the child's resent prefix) | after a byte drift (host rewrite/re-render of an earlier message), is this prefix still the parent session's? | compared ONLY against rawId — never enters the identity space, never re-keys; ANY mismatch ⇒ silent fallback to the strict byte fork (fields ignored); malformed field shape ⇒ 400 INVALID_REQUEST; reuses identifier(), no new validation copy (§4) | ## 7. Trust boundaries (declared as part of the contract, not bugs) diff --git a/docs/msgid-infrastructure.zh-CN.md b/docs/msgid-infrastructure.zh-CN.md index d76357b723..56839d10d0 100644 --- a/docs/msgid-infrastructure.zh-CN.md +++ b/docs/msgid-infrastructure.zh-CN.md @@ -1,7 +1,7 @@ # 消息身份公用设施:sessionid+msgid 的生产与消费地图 > 状态:#5826 release review(2026-11-07)owner 裁定落地的缺口修复配套文档。 -> 身份层级教义见根目录 message-identity.md 与会话侧 session-identity.md;本文只负责一件事——把 **sessionid+msgid 这套跨车道的公用设施**的生产端、传输形态、校验副本、持久化单一事实源、五个消费方、信任边界,一次说明白。改动任何一处之前先读这里。 +> 身份层级教义见根目录 message-identity.md 与会话侧 session-identity.md;本文只负责一件事——把 **sessionid+msgid 这套跨车道的公用设施**的生产端、传输形态、校验副本、持久化单一事实源、六个消费方、信任边界,一次说明白。改动任何一处之前先读这里。 ## 1. 这是什么 @@ -49,15 +49,16 @@ 统一存储 `messages.msg_id` 列是唯一 id 事实源:ingest 时把校验过的 id 钉进行(T5 裁定:无双平行 ledger、skeleton 行不携 id)。账本主键 `(session_id, msg_id, content_hash)`;canonical 行按 **MSGID 而非内容 hash** 键控(#4491);fork 克隆保号(#4316 L3 PR3/3 `cloneSessionRows`)。 -## 6. 消费方(五个,id 各自回答什么问题) +## 6. 消费方(六个,id 各自回答什么问题) | 消费方 | 位置 | 回答的问题 | id 使用边界 | |---|---|---|---| -| ① 存储列身份 | unified store `messages.msg_id` | settle/reconcile 一切 join 的主键 | 唯一身份空间入口,其余四方的 id 证据最终都折到这里 | +| ① 存储列身份 | unified store `messages.msg_id` | settle/reconcile 一切 join 的主键 | 唯一身份空间入口,其余五方的 id 证据最终都折到这里 | | ② session-resolve 最后一档 #5719 | tryMsgIdRescue(src/session-resolve.ts ~:324;fork-exit 调用点 ~:432/:481/:501) | 字节/标签证据被压缩或 system 重写摧毁后,这条请求还是不是既有会话? | 同一 raw key 下**任意 id 重叠 ⇒ 认定同会话**;注意它不做 lane 归属判定(§7 边界 B) | | ③ 结算 membership 探针 #4805 | probeRequestMembership(src/request-membership.ts;调用点 src/server/prepare-openai.ts:597、prepare-anthropic.ts:640;消费 cache-ledger.ts:1866) | 这个 id 是否已被本会话账本准入?needle = 最近准入 id | **纯证据,永不进身份空间**、永不 re-key/fork;误伤仅致一轮结算未决,自癒 | | ④ 增量车道 canonical 行 #4491/#4834 | src/incremental-lane.ts(validateMsgIds :623、认领复用 400 :628、cache-control rekey :631-635;dsh 侧 burned-claim 预筛 ~:2584) | 本轮增量追加了哪些正史行? | 认领语义:已认领 id 再上行=重试风暴信号,整回合 400 | | ⑤ 水位尾连续性 v2-v4 #5403/#5418 | PF_TAIL_IDS_HEADER(src/server/fastpath.ts:48) | 客户端 delta 窗口的起点还和服务端 seed 对齐吗? | v4 宽度自洽(len=T−K+1,服务端重验)时计数回声降为纯诊断;id 只证**窗口来源**,永不证前缀完整性(owner 裁定 2026-11-02;§7.4 盲点照旧,§7 边界 A) | +| ⑥ fork 收养 msgid 轨 #6262 | tryForkAdoption(src/agent/fork-adopt.ts——客户端逐格比对采集到的宿主 id 与父快照 rawId)+ handlePluginFork(src/plugin.ts——request.prefixMsgIds[k] === prefix[k].rawId,对子会话重发前缀逐格验) | 字节漂移(宿主改写/重渲染了较早消息)之后,这段前缀还是不是父会话的? | 只与 rawId 比对——永不进身份空间、永不 re-key;任一失配 ⇒ 静默回退严格字节 fork(附加字段忽略);字段形状非法 ⇒ 400 INVALID_REQUEST;复用 identifier(),无新增校验副本(§4) | ## 7. 信任边界(声明为契约的一部分,不是 bug) diff --git a/docs/plugin-protocol.md b/docs/plugin-protocol.md index 622f184b2b..7cdf1ea9c0 100644 --- a/docs/plugin-protocol.md +++ b/docs/plugin-protocol.md @@ -191,15 +191,19 @@ Discover `capabilities.fork` from the manifest; do not infer support from the pa "parentRevision": "", "branchPoint": { "messageCount": 1, "orderHash": "" }, "orderedMessages": [{ "rawId": "h_example", "ref": "m00001", "identityHash": "" }], - "idempotencyKey": "stable-operation-id" + "idempotencyKey": "stable-operation-id", + "prefixMsgIds": ["host-uuid-1", "host-uuid-2"], + "driftFrom": 1 } ``` +`prefixMsgIds` and `driftFrom` are optional (#6262). On a parent whose kernel ids are the host's own stable message ids (a pinned session, where snapshot `rawId` IS the host id), a client may additionally present the host id of every matched prefix message, plus the hash-run length as `driftFrom` (`0 ≤ driftFrom < prefixMsgIds.length`). The server accepts a byte-drifted prefix only when every presented id equals the parent's `rawId` — ids prove prefix identity where bytes no longer match — and marks the receipt `status: "drifted"` (or `"expanded+drifted"` when a block also crosses the branch) with the verified `driftFrom` echoed. A gate failure never fails the fork: the request falls back to the strict byte-verified fork (`"exact"`/`"expanded"`) and the extra fields are ignored. Malformed shapes (wrong length, bad identifier, `driftFrom` out of range or without `prefixMsgIds`) return `400 INVALID_REQUEST`. Parents without host-pinned ids make the track a no-op, byte path unchanged. + `protocolVersion` is `2` (current) or `1` (legacy) in both directions; the proxy accepts either (`acceptsProtocolVersions` in the manifest advertises this) and rejects anything newer with `400 INVALID_REQUEST`. Under protocol 2 the `parentRevision` token covers exactly the session id, the ordered-prefix length (`messageCount`), and the active block set — so routine stats churn (context estimates, usage bookkeeping) no longer changes the revision, while any structural change (messages appended or rewritten, a block folded or unfolded) still does. Protocol-1 clients that present the legacy whole-store revision token are still matched during the transition window; treat `parentRevision` as opaque in both cases: on `409 PARENT_REVISION_CONFLICT` take a fresh snapshot and re-match, regardless of which protocol version you speak. The branch is an ordered prefix, including an empty prefix. Compute its existing order hash as lowercase SHA-256 of UTF-8 `JSON.stringify(orderedMessages)` with each identity object's keys in order `rawId`, `ref`, `identityHash`; for a full prefix reuse snapshot `orderHash`. Do not hash descriptors or folded text. Extra child messages, including a final model reply that has not left the proxy yet, may be appended on the child's first model request after copying the matched prefix. -New success is `201`; exact payload replay is `200` with `replayed: true`, even after restart or later parent changes. Both return `status`, `parentRevision`, `childRevision`, `sessionId`, `branchPoint`, `inheritedBlocks`, and `expandedBlocks`. Reusing the child id with another parent, key or payload returns `409 CHILD_CONFLICT`. Invalid shapes/version/identifiers return `400 INVALID_REQUEST`; absent parent returns `404 PARENT_NOT_FOUND`; stale parent returns `409 PARENT_REVISION_CONFLICT`; altered order/raw/ref/identity returns `409 BRANCH_POINT_CONFLICT`. Missing nested originals return `409 PARENT_STATE_INCOMPLETE` with `status: unavailable`. Publication/persistence failure returns `503 FORK_FAILED` with `status: unavailable` and exposes no child; the same request may be retried. +New success is `201`; exact payload replay is `200` with `replayed: true`, even after restart or later parent changes. Both return `status`, `parentRevision`, `childRevision`, `sessionId`, `branchPoint`, `inheritedBlocks`, and `expandedBlocks` (plus `driftFrom` when the msgid track accepted a drift, above). Reusing the child id with another parent, key or payload returns `409 CHILD_CONFLICT`. Invalid shapes/version/identifiers return `400 INVALID_REQUEST`; absent parent returns `404 PARENT_NOT_FOUND`; stale parent returns `409 PARENT_REVISION_CONFLICT`; altered order/raw/ref/identity returns `409 BRANCH_POINT_CONFLICT`. Missing nested originals return `409 PARENT_STATE_INCOMPLETE` with `status: unavailable`. Publication/persistence failure returns `503 FORK_FAILED` with `status: unavailable` and exposes no child; the same request may be retried. Boundary handling is `exact` when complete blocks fit inside the prefix, `expanded` when a block crosses it (crossing summaries are omitted, affected nested descendants are expanded, matched originals are retained), and `unavailable` when safe reconstruction is impossible. Blocks, nested caches, raw messages, issued ref namespace, token snapshots, rules and prefix CCR payloads are independent copies. Note that the child inherits the parent's ENTIRE issued-ref namespace (dead refs included): refs are never reused, so forks keep allocating fresh refs without colliding with ids a sibling may still hold. The child never reads its parent's later state as a fallback. Registration and first-request resume heuristics do not replace its copied state. With existing persistence enabled, raw snapshots, receipts, nested originals and the child's CCR payloads survive restart; with persistence disabled, durability is not promised. Atomic child publication/idempotency covers concurrent requests to one proxy process; concurrent writers sharing one state directory across processes are not supported by this protocol. @@ -211,7 +215,7 @@ Marker divert (#3248 phase 2 — native extensions): instead of switching the pr 中文增量要点(#3248):`POST /__bili/plugin/turn`,首次/恢复用 `sinceSeq:0` 全量(种子隐含 `resync:true`,永不 409);此后每轮只发新增(含上一轮 assistant 回复,客户端自持)。响应即上游原文,`x-bili-applied-seq` 为应用后的长度,下一轮作 `sinceSeq` 回传。409 必须按 serverSeq 对齐或全量重同步;503 表示未开 storage.unified。会话身份沿用客户端自己的 conversation id——增量与全量重发同一会话可任意切换,折叠/工具/状态栏不变。阶段 2(标记分流):普通模型请求上盖 `x-bili-plugin-turn: 1` 头 + 增量形状 body(顶层 conversationId/sinceSeq/messages、无顶层 model)即可,增量形状 turn 属 lane 流量,与直连 turn 端点同信任级(#3343):非回环/隧道/不可信源一律 403(其探针本就 403,正常宿主不会远程发增量);管理前缀(`/__bili/`、`/__acp/`)永不走分流。lane 按原始 URL 转发;探针 `GET /__bili/plugin/turn`;开关 rollout `pluginIncrementalTurns`(默认 0%=零变化,`BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS=1` 或配置强制开)。客户端状态机在 src/agent/incremental-client.ts,新宿主复用勿重写。已接宿主:pi/pi-native(同一扩展工厂,后者零代码继承)、dsh-native(fetch 补丁 beforeSend/headersFor/onResponse)、opencode(http.request 钩子 incremental 适配回调换 body+盖章)、omp(无头事件通道,读 body 里 prompt_cache_key 当会话 id,直接在 init 上盖章);OpenAI 工具形状(tool_calls/role:tool)自动回退全量重发;每 URL 单飞行+30s 过期。 -中文集成要点:fork 协议现为 2(服务端同时接受 1);先读取 manifest 能力及 snapshot,按原文、role、工具身份确认匹配前缀,仅提交该前缀的 orderedMessages,不猜测多模态分支点。被 `compress.protectedTools` 硬排除的消息在两个数组中均为 `"ref": null`(参与顺序与身份、无地址 ref),与其他前缀条目一样逐字提交;ref 合法值仅 `mNNNNN` 字符串或 `null`。父 revision 变化必须重新取快照并重新匹配。子会话使用独立稳定 id,首次请求可追加尚未出站的模型回复,不能重置继承的 refs/blocks。手动摘要使用 snapshot 的 refs 和 `expectedRevision: snapshot.parentRevision` 调用公开 tool;摘要期间历史发生变化时返回 409,不执行旧摘要。状态栏读取 `contextTokens`、`contextTokensSource`、`contextTokensAt`、`model`、`contextLimit`、`contextGeneration`、`sessionId` 与 `sessionRevision`,不得用累计账单 input/cache 冒充有效上下文。手动 compress/decompress 后为独立时间及 generation 的 estimate,不改累计账单或 last-usage 基线;下一次正数 usage 才恢复 usage,零值或缺失 usage 不得复活旧观测。未知来源返回 unavailable,tokens/time/generation 为 null。 +中文集成要点:fork 协议现为 2(服务端同时接受 1);先读取 manifest 能力及 snapshot,按原文、role、工具身份确认匹配前缀,仅提交该前缀的 orderedMessages,不猜测多模态分支点。父会话若已 pin 宿主 msgid(snapshot 的 rawId 即宿主 id),可另附 `prefixMsgIds`(每个前缀消息的宿主 id)与 `driftFrom`(哈希前缀长度):宿主 id 全等时服务端接受字节已漂移的前缀,回执 `status: "drifted"`(跨块时 `"expanded+drifted"`)并回显已验证的 `driftFrom`;gate 失败不影响 fork 成功(回退严格字节 fork,附加字段被忽略);字段形状非法返回 400,未 pin 的父会话该轨自动无效。被 `compress.protectedTools` 硬排除的消息在两个数组中均为 `"ref": null`(参与顺序与身份、无地址 ref),与其他前缀条目一样逐字提交;ref 合法值仅 `mNNNNN` 字符串或 `null`。父 revision 变化必须重新取快照并重新匹配。子会话使用独立稳定 id,首次请求可追加尚未出站的模型回复,不能重置继承的 refs/blocks。手动摘要使用 snapshot 的 refs 和 `expectedRevision: snapshot.parentRevision` 调用公开 tool;摘要期间历史发生变化时返回 409,不执行旧摘要。状态栏读取 `contextTokens`、`contextTokensSource`、`contextTokensAt`、`model`、`contextLimit`、`contextGeneration`、`sessionId` 与 `sessionRevision`,不得用累计账单 input/cache 冒充有效上下文。手动 compress/decompress 后为独立时间及 generation 的 estimate,不改累计账单或 last-usage 基线;下一次正数 usage 才恢复 usage,零值或缺失 usage 不得复活旧观测。未知来源返回 unavailable,tokens/time/generation 为 null。 ### Incremental cache-mark updates diff --git a/reference/architecture.md b/reference/architecture.md index 5611e6ef02..0a0e8b763a 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -226,7 +226,7 @@ billion-context/ │ │ ├── conversation-recall.ts # Fallback identity when host attribution context is missing (#4082) │ │ ├── dsh-compaction-disable.ts # Load-time overlay standing dsh basic compaction down (#3354) │ │ ├── dsh-compaction-gate.ts # Always-on compaction gate entry, independent of bili-native (#3662) -│ │ ├── fork-adopt.ts # Client half of fork adoption: prefix match + receipt (#2399) +│ │ ├── fork-adopt.ts # Client half of fork adoption: prefix match + receipt (#2399, msgid track #6262) │ │ ├── host-log.ts # Host-safe log channel: durable record, zero terminal traffic (#2827) │ │ ├── incremental-client.ts # Host-process incremental-turn client: diff, ack, resync (#3248) │ │ ├── opencode-msgids.ts # Opencode msgid harvest + wire alignment (#4491) @@ -346,7 +346,7 @@ billion-context/ │ ├── src/ # 14 modules: delegate tool/lifecycle, fleet inspector/widget, config, events │ ├── tests/ # Component unit suite (`npm --prefix pi-subagents test`, runs on TS source) │ └── package.json # Own name/version (npm `billion-context-pi-subagents`), own build chain (tsup → dist + d.ts) - ├── tests/ # 614 test files (+ hermetic e2e lanes under tests/e2e/) + ├── tests/ # 615 test files (+ hermetic e2e lanes under tests/e2e/) ├── tsup.config.ts # Build config (inlines acp-kernel + billion-context-pi-subagents; zod external) └── package.json # npm manifest (consumes kernel via `"acp-kernel": "file:./kernel"`, pi-subagents via `"billion-context-pi-subagents": "file:./pi-subagents"`; version bumped ONLY on release branches) diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index daecb744c2..6131b8121f 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -1600,7 +1600,10 @@ async function maybeForkAdoptBeforeSend(ctx: PluginContext, init: RequestInit | `bili-native-dsh: fork adoption for ${child.sid} skipped at the proxy-base gate — the proxy origin is not known yet, so the fork protocol cannot be reached (#4215)`); return; } - await dshForkAdopter.maybeAdopt({ base, parent: child.parent, child: child.sid, body }); + // #6262 principle 1: host msgids for the msgid identity track — the SAME + // parity-pinned collector the incremental lane uses (hostMsgIdsOf); a + // mismatched count falls back to the byte track inside the adopter. + await dshForkAdopter.maybeAdopt({ base, parent: child.parent, child: child.sid, body, msgIds: hostMsgIdsOf((body as { messages?: unknown }).messages) }); } // #1677: session id of the command's invoking agent (host-passed invocation); malformed diff --git a/src/agent/fork-adopt.ts b/src/agent/fork-adopt.ts index 1396f8f99d..6aa10d386c 100644 --- a/src/agent/fork-adopt.ts +++ b/src/agent/fork-adopt.ts @@ -408,6 +408,10 @@ export async function tryForkAdoption(opts: { parentConversationId: string; childConversationId: string; body: unknown; + // #6262 principle 1: host-stable message ids aligned to the wire, as the lane's + // parity-pinned collector yields them. Length-mismatched or absent → byte track + // only (exactly today's behavior). + msgIds?: string[]; fetchImpl?: typeof fetch; log?: (line: string) => void; }): Promise { @@ -422,6 +426,11 @@ export async function tryForkAdoption(opts: { } const core = chatBodyToCore(opts.body); if (core === null || core.length === 0) return { outcome: "degraded", reason: "body unmappable" }; + // #6262 principle 1: the only check this module performs on msgIds is the + // core-alignment length lock — host-pinned parents expose the host ids as + // snapshot rawIds, so a misaligned array can never match and the byte track + // carries the fork (same outcome as absent). + const msgIds = opts.msgIds !== undefined && opts.msgIds.length === core.length ? opts.msgIds : undefined; const snapshotUrl = `${opts.base}/__bili/plugin/snapshot?conversationId=${encodeURIComponent(opts.parentConversationId)}`; for (let attempt = 0; attempt < 2; attempt += 1) { let snapshot: Awaited>; @@ -439,11 +448,32 @@ export async function tryForkAdoption(opts: { return { outcome: "degraded", reason: `snapshot error ${err instanceof Error ? err.message : String(err)}` }; } if (snapshot === null) return { outcome: "degraded", reason: "snapshot malformed" }; - const branchPoint = matchForkPrefix(core, snapshot.orderedMessages); - if (branchPoint === 0) return { outcome: "degraded", reason: "no prefix match" }; + const nHash = matchForkPrefix(core, snapshot.orderedMessages); + if (nHash === 0) return { outcome: "degraded", reason: "no prefix match" }; + // #6262 principle 1 (msgid identity track, discussion #6262): when the lane + // hands us host-stable message ids, identity outranks bytes. A parent whose + // messages were host-pinned carries the host ids AS its snapshot rawIds, so + // an id match at a slot whose bytes drifted (the host re-rendered that + // message after the parent saw it) does not truncate the prefix — the fork + // claim extends to the id-stable length, driftFrom marking where the byte + // stop was. The claimed identities stay the PARENT's (copied verbatim); the + // server re-gates every claimed id against its own snapshot before trusting + // the drift region. No new id validation here: the lane's collector is the + // parity-pinned one, and a length mismatch (wire/core misalignment — part + // splits, hoisted system) simply falls back to the byte track. + let branchPoint = nHash; + let driftFrom: number | undefined; + let nId = 0; + if (msgIds !== undefined) { + while (nId < msgIds.length && nId < snapshot.orderedMessages.length && msgIds[nId] === snapshot.orderedMessages[nId]!.rawId) nId += 1; + if (nId > nHash) { + branchPoint = nId; + driftFrom = nHash; + } + } const prefix = snapshot.orderedMessages.slice(0, branchPoint); const orderHash = branchPoint === snapshot.orderedMessages.length ? snapshot.orderHash : forkOrderHashOf(prefix); - const payload = { + const payload: Record = { protocolVersion: 1, parentConversationId: opts.parentConversationId, childConversationId: opts.childConversationId, @@ -452,6 +482,14 @@ export async function tryForkAdoption(opts: { orderedMessages: prefix, idempotencyKey: `fork:${opts.childConversationId}`, }; + // #6262: send the host ids for the claimed prefix only when EVERY claimed + // cell is id-verified client-side (nId >= branchPoint) — a shorter id run + // would put unverified ids in the gate's path for no gain. driftFrom rides + // only with an extended prefix. + if (msgIds !== undefined && nId >= branchPoint) { + payload.prefixMsgIds = msgIds.slice(0, branchPoint); + if (driftFrom !== undefined) payload.driftFrom = driftFrom; + } let res: Response; try { res = await doFetch(`${opts.base}/__bili/plugin/fork`, { @@ -513,7 +551,15 @@ export async function manifestForkCapable(base: string, fetchImpl?: typeof fetch } } -export type ForkAdoptInput = { base: string; parent: string; child: string; body: unknown }; +export type ForkAdoptInput = { + base: string; + parent: string; + child: string; + body: unknown; + // #6262 principle 1: lane-collected host msgids for the msgid identity track; + // absent → byte track (today's behavior). See tryForkAdoption. + msgIds?: string[]; +}; /** Per-host fork-adoption coordinator (#2399 stage 2): absorbs the bookkeeping * every host needs (done/attempts/single-flight keyed by child id, the @@ -569,6 +615,7 @@ export function createForkAdopter(log: (line: string) => void, opts?: { maxAttem parentConversationId: input.parent, childConversationId: input.child, body: input.body, + msgIds: input.msgIds, fetchImpl: opts?.fetchImpl, log, }); diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 83792937a7..86871eb72e 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -1508,7 +1508,28 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM let proxyBase = proxyBaseForCtx(ctx); if (proxyBase === undefined) proxyBase = await awaitNativeProxyOrigin(); if (proxyBase === undefined) return; - await forkAdopter.maybeAdopt({ base: proxyBase, parent, child: sid, body: (event as { payload?: unknown } | undefined)?.payload }); + // #6262 principle 1: host msgids for the msgid identity track — the + // SAME gates the incremental pin uses (anthropic wire only, every + // message content-pinnable, ids aligned to the full wire). A lane + // that can't resolve stable ids yields undefined → the byte track + // carries the fork (today's behavior). No new validation: laneMsgIdsForWire + // is the parity-pinned collector. + const payload = (event as { payload?: unknown } | undefined)?.payload; + let forkMsgIds: string[] | undefined; + if (payload !== null && typeof payload === "object") { + const messages = (payload as { messages?: unknown }).messages; + if (wireMessagesLookAnthropic(messages)) { + let branch: unknown; + try { + branch = ctx.sessionManager?.getBranch?.(); + } catch { + branch = undefined; + } + const ids = laneMsgIdsForWire(messages, branch); + if (ids !== undefined && ids.length === messages.length && messages.every((m) => lanePinnableContent(m.content))) forkMsgIds = ids; + } + } + await forkAdopter.maybeAdopt({ base: proxyBase, parent, child: sid, body: payload, msgIds: forkMsgIds }); } pi.on("before_provider_request", async (event, ctx) => { // #5403/#5418 unified watermark channel: capture THIS request's diff --git a/src/plugin.ts b/src/plugin.ts index a4824a39d0..8188137d0b 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1195,6 +1195,15 @@ type ForkRequest = { branchPoint: { messageCount: number; orderHash: string }; orderedMessages: ForkIdentity[]; idempotencyKey: string; + // #6262 principle 1 (msgid identity track, discussion #6262): the child's + // host-stable ids for the claimed prefix. A host-pinned parent exposes the + // host ids AS its snapshot rawIds, so the gate in handlePluginFork is rawId + // equality — no new validation copy (identifier() below is the SAME check + // every rawId already passes). + prefixMsgIds?: string[]; + // Where the byte-verified prefix stopped and the identity track extended it. + // Only rides WITH prefixMsgIds; >= messageCount normalizes to "no drift". + driftFrom?: number; }; function stableJson(value: unknown): string { @@ -1658,7 +1667,19 @@ function parseForkRequest(payload: string): ForkRequest { if (!identifier(item.rawId) || !(item.ref === null || (typeof item.ref === "string" && /^m\d{5,}$/.test(item.ref))) || !hash(item.identityHash)) throw new Error("invalid raw/ref identity"); return { rawId: item.rawId, ref: item.ref, identityHash: item.identityHash }; }); - return { protocolVersion: b.protocolVersion, parentConversationId: b.parentConversationId, childConversationId: b.childConversationId, parentRevision: b.parentRevision, branchPoint: { messageCount: point.messageCount as number, orderHash: point.orderHash }, orderedMessages, idempotencyKey: b.idempotencyKey }; + // #6262 principle 1: optional msgid track. identifier() is REUSED, not copied — + // the same shape check every claimed rawId already passes above. + let prefixMsgIds: string[] | undefined; + if (b.prefixMsgIds !== undefined) { + if (!Array.isArray(b.prefixMsgIds) || b.prefixMsgIds.length !== (point.messageCount as number) || b.prefixMsgIds.some((v: unknown) => !identifier(v))) throw new Error("invalid prefixMsgIds"); + prefixMsgIds = b.prefixMsgIds as string[]; + } + let driftFrom: number | undefined; + if (b.driftFrom !== undefined) { + if (prefixMsgIds === undefined || !Number.isSafeInteger(b.driftFrom) || (b.driftFrom as number) < 0 || (b.driftFrom as number) > (point.messageCount as number)) throw new Error("invalid driftFrom"); + driftFrom = (b.driftFrom as number) < (point.messageCount as number) ? (b.driftFrom as number) : undefined; + } + return { protocolVersion: b.protocolVersion, parentConversationId: b.parentConversationId, childConversationId: b.childConversationId, parentRevision: b.parentRevision, branchPoint: { messageCount: point.messageCount as number, orderHash: point.orderHash }, orderedMessages, idempotencyKey: b.idempotencyKey, ...(prefixMsgIds !== undefined ? { prefixMsgIds } : {}), ...(driftFrom !== undefined ? { driftFrom } : {}) }; } /** [#2399 stage 2] omp/pi identity-register their session ids at session_start @@ -1718,6 +1739,23 @@ export async function handlePluginFork(payload: string, res: ServerResponse): Pr } const prefix = snapshot.orderedMessages.slice(0, request.branchPoint.messageCount); if (prefix.length !== request.branchPoint.messageCount || forkOrderHash(request.orderedMessages) !== request.branchPoint.orderHash || stableJson(prefix) !== stableJson(request.orderedMessages)) return forkReply(res, 409, { ok: false, code: "BRANCH_POINT_CONFLICT", error: "ordered prefix/hash does not match parent" }); + // #6262 principle 1 (msgid identity track): the byte check above is + // final for the prefix AS CLAIMED (the client copies the parent's + // identities verbatim, including the drift region). When the client + // additionally claims host-stable ids for the prefix and every one + // equals the parent's rawId at its slot (host-pinned parents: the + // rawId IS the host id), the region [driftFrom, messageCount) is + // identity-stable byte drift — the host re-rendered those messages + // after the parent saw them but kept their ids. Blocks and refs are + // keyed by rawId, and the child's pinned ids match the parent's, so + // everything copied below stays valid in the child: accept and mark + // the receipt. Gate failure (parent not host-pinned — its rawIds are + // content digests, never host ids — or any id/length mismatch) → the + // byte-verified fork proceeds EXACTLY as before: strict, no 409. + let forkDriftFrom: number | undefined; + if (request.prefixMsgIds !== undefined && request.prefixMsgIds.length === prefix.length && request.prefixMsgIds.every((id, k) => id === prefix[k]!.rawId)) { + forkDriftFrom = request.driftFrom; + } const rawIds = new Set(prefix.map((m) => m.rawId)); const child = createSession(request.childConversationId, { ...parent.meta, label: request.childConversationId }); const crossing = parent.state.blocks.filter((b) => b.effectiveMessageIds.some((id) => rawIds.has(id)) && !b.effectiveMessageIds.every((id) => rawIds.has(id))); @@ -1839,7 +1877,7 @@ export async function handlePluginFork(payload: string, res: ServerResponse): Pr (child.transient ??= {}).latestKnownMsgId = parent.transient.latestKnownMsgId; child.transient.needlePayloadUpper = parent.transient.needlePayloadUpper; } - const response = { ok: true, status: crossing.length > 0 ? "expanded" : "exact", protocolVersion: 2, parentConversationId: request.parentConversationId, childConversationId: request.childConversationId, sessionId: child.id, parentRevision: request.parentRevision, childRevision: forkSnapshot(child).parentRevision, branchPoint: request.branchPoint, expandedBlocks: crossing.map((b) => b.blockId), inheritedBlocks: child.state.blocks.map((b) => ({ id: b.blockId, tier: b.tier, active: b.active, expanded: b.expanded === true })), replayed: false }; + const response = { ok: true, status: forkDriftFrom !== undefined ? (crossing.length > 0 ? "expanded+drifted" : "drifted") : crossing.length > 0 ? "expanded" : "exact", protocolVersion: 2, parentConversationId: request.parentConversationId, childConversationId: request.childConversationId, sessionId: child.id, parentRevision: request.parentRevision, childRevision: forkSnapshot(child).parentRevision, branchPoint: request.branchPoint, ...(forkDriftFrom !== undefined ? { driftFrom: forkDriftFrom } : {}), expandedBlocks: crossing.map((b) => b.blockId), inheritedBlocks: child.state.blocks.map((b) => ({ id: b.blockId, tier: b.tier, active: b.active, expanded: b.expanded === true })), replayed: false }; child.metadata.publicForkReceipt = { requestHash, response }; await publishForkSession(child, parent.id); recordPluginSession(request.childConversationId, child.id); diff --git a/tests/issue6262-fork-msgid-track.test.ts b/tests/issue6262-fork-msgid-track.test.ts new file mode 100644 index 0000000000..4d3711e44b --- /dev/null +++ b/tests/issue6262-fork-msgid-track.test.ts @@ -0,0 +1,338 @@ +// #6262 principle 1 (msgid identity track, discussion #6262): the fork protocol +// gains an OPTIONAL identity track. When both sides expose host-stable message +// ids (a host-pinned parent carries the host ids AS its snapshot rawIds), a +// cell whose id matches but whose bytes drifted no longer truncates the fork +// prefix — the claim extends to the id-stable length, `driftFrom` marking the +// byte stop. Everything degrades to today's byte-only track: absent ids on +// either side, a misaligned array, or an unpinned parent (content-digest +// rawIds never equal host ids) → the byte path, byte for byte. +// +// Server observability: the fork response gains `driftFrom` and status +// "drifted" / "expanded+drifted" when the gate passes; a gate failure is +// SILENT (strict byte fork, no 409 — old clients and unpinned parents see +// exactly today's behavior). +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { mkdtempSync, rmSync } 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 } from "../src/plugin.ts"; +import { _resetSessionsForTest } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { resetToolRingForTest } from "../src/tool-ring.ts"; +import { forkIdentityHashOf, forkOrderHashOf, openaiBodyToCore, tryForkAdoption, type ForkIdentity } from "../src/agent/fork-adopt.ts"; + +process.env.NODE_ENV = "test"; +const testRoot = mkdtempSync(join(tmpdir(), "bili-6262-fork-")); +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; + +// --------------------------------------------------------------------------- +// Client track (tryForkAdoption) — stubbed snapshot/fork transport. +// --------------------------------------------------------------------------- + +const PARENT_MESSAGES = [ + { role: "user", content: "u1" }, + { role: "assistant", content: "a1" }, + { role: "user", content: "u2" }, + { role: "assistant", content: "a2" }, + { role: "user", content: "u3" }, +] as const; +const PARENT_BODY = { model: "m", messages: [...PARENT_MESSAGES] }; +const RAW_IDS = ["r-0", "r-1", "r-2", "r-3", "r-4"]; +const PARENT_CORE = openaiBodyToCore(PARENT_BODY); +assert(PARENT_CORE !== null && PARENT_CORE.length === 5, "harness: parent body must project to 5 core messages"); + +// Snapshot identities derived from the parent core: identityHash = the same +// hash the client will recompute, rawId = the (pinned) host id. +const IDENTITIES: ForkIdentity[] = PARENT_CORE.map((m, k) => ({ rawId: RAW_IDS[k]!, ref: null, identityHash: forkIdentityHashOf(m) })); +const SNAPSHOT = { + ok: true, + protocolVersion: 2, + status: "exact", + sessionId: "parent-sess", + parentRevision: "a".repeat(64), + orderHash: forkOrderHashOf(IDENTITIES), + orderedMessages: IDENTITIES, +}; + +type Call = { url: string; body?: string }; +function stubTransport(forkResponse?: { status: number; body?: Record }): { calls: Call[]; fetchImpl: typeof fetch } { + const calls: Call[] = []; + const fetchImpl = (async (url: string | URL | Request, init?: RequestInit) => { + const u = String(url); + calls.push({ url: u, body: typeof init?.body === "string" ? init.body : undefined }); + if (u.includes("/__bili/plugin/snapshot")) { + return new Response(JSON.stringify(SNAPSHOT), { status: 200, headers: { "content-type": "application/json" } }); + } + return new Response(JSON.stringify(forkResponse?.body ?? { ok: true, replayed: false }), { status: forkResponse?.status ?? 201, headers: { "content-type": "application/json" } }); + }) as unknown as typeof fetch; + return { calls, fetchImpl }; +} + +// Child = parent with the 4th cell (assistant "a2") re-rendered by the host — +// same stable id, different bytes. Byte stop = 3, id run = 5. +const DRIFT_BODY = { + model: "m", + messages: [ + { role: "user", content: "u1" }, + { role: "assistant", content: "a1" }, + { role: "user", content: "u2" }, + { role: "assistant", content: "a2-DRIFT" }, + { role: "user", content: "u3" }, + ], +}; + +test("#6262: msgid track extends the prefix across an identity-stable drift cell", async () => { + const { calls, fetchImpl } = stubTransport(); + const result = await tryForkAdoption({ base: "http://proxy", parentConversationId: "p", childConversationId: "c", body: DRIFT_BODY, msgIds: [...RAW_IDS], fetchImpl }); + assert.deepEqual(result, { outcome: "adopted", branchPoint: 5, replayed: false }); + const fork = calls.find((c) => c.url.includes("/__bili/plugin/fork")); + assert(fork !== undefined && fork.body !== undefined); + const payload = JSON.parse(fork!.body!) as { + branchPoint: { messageCount: number; orderHash: string }; + orderedMessages: ForkIdentity[]; + prefixMsgIds?: string[]; + driftFrom?: number; + idempotencyKey: string; + }; + assert.equal(payload.branchPoint.messageCount, 5); + assert.equal(payload.driftFrom, 3, "driftFrom = the byte stop"); + assert.deepEqual(payload.prefixMsgIds, RAW_IDS, "host ids for the whole claimed prefix"); + assert.deepEqual(payload.orderedMessages, IDENTITIES, "claimed identities stay the PARENT's, verbatim"); + assert.equal((payload.branchPoint as { orderHash: string }).orderHash, SNAPSHOT.orderHash, "full-length prefix reuses the snapshot orderHash"); + assert.equal(payload.idempotencyKey, "fork:c"); +}); + +test("#6262: absent msgIds → byte track, byte for byte (legacy client)", async () => { + const { calls, fetchImpl } = stubTransport(); + const result = await tryForkAdoption({ base: "http://proxy", parentConversationId: "p", childConversationId: "c", body: DRIFT_BODY, fetchImpl }); + assert.deepEqual(result, { outcome: "adopted", branchPoint: 3, replayed: false }); + const payload = JSON.parse(calls.find((c) => c.url.includes("/__bili/plugin/fork"))!.body!) as Record; + assert.equal(payload.branchPoint && (payload.branchPoint as { messageCount: number }).messageCount, 3); + assert.equal("prefixMsgIds" in payload, false, "no id track fields on the byte path"); + assert.equal("driftFrom" in payload, false); + assert.equal((payload.orderedMessages as unknown[]).length, 3); +}); + +test("#6262: unpinned parent (ids never align) → byte track, fields omitted", async () => { + const { calls, fetchImpl } = stubTransport(); + const hostUuids = ["uuid-0", "uuid-1", "uuid-2", "uuid-3", "uuid-4"]; + const result = await tryForkAdoption({ base: "http://proxy", parentConversationId: "p", childConversationId: "c", body: DRIFT_BODY, msgIds: hostUuids, fetchImpl }); + assert.deepEqual(result, { outcome: "adopted", branchPoint: 3, replayed: false }); + const payload = JSON.parse(calls.find((c) => c.url.includes("/__bili/plugin/fork"))!.body!) as Record; + assert.equal("prefixMsgIds" in payload, false, "nId=0 < branchPoint → unverified ids are never sent"); + assert.equal("driftFrom" in payload, false); +}); + +test("#6262: id run shorter than the byte run → byte track wins, fields omitted", async () => { + const { calls, fetchImpl } = stubTransport(); + const result = await tryForkAdoption({ base: "http://proxy", parentConversationId: "p", childConversationId: "c", body: DRIFT_BODY, msgIds: ["r-0", "r-1", "different"], fetchImpl }); + assert.deepEqual(result, { outcome: "adopted", branchPoint: 3, replayed: false }); + const payload = JSON.parse(calls.find((c) => c.url.includes("/__bili/plugin/fork"))!.body!) as Record; + assert.equal("prefixMsgIds" in payload, false, "nId=2 < branchPoint=3 → omitted"); + assert.equal("driftFrom" in payload, false); +}); + +test("#6262: pinned parent, no drift → ids ride the full byte prefix, no driftFrom", async () => { + const { calls, fetchImpl } = stubTransport(); + const result = await tryForkAdoption({ base: "http://proxy", parentConversationId: "p", childConversationId: "c", body: { ...PARENT_BODY, messages: [...PARENT_MESSAGES] }, msgIds: [...RAW_IDS], fetchImpl }); + assert.deepEqual(result, { outcome: "adopted", branchPoint: 5, replayed: false }); + const payload = JSON.parse(calls.find((c) => c.url.includes("/__bili/plugin/fork"))!.body!) as Record; + assert.deepEqual(payload.prefixMsgIds, RAW_IDS); + assert.equal("driftFrom" in payload, false, "no extension → no driftFrom"); +}); + +test("#6262: misaligned msgIds (length ≠ core) → byte track, fields omitted", async () => { + const { calls, fetchImpl } = stubTransport(); + const result = await tryForkAdoption({ base: "http://proxy", parentConversationId: "p", childConversationId: "c", body: DRIFT_BODY, msgIds: RAW_IDS.slice(0, 4), fetchImpl }); + assert.deepEqual(result, { outcome: "adopted", branchPoint: 3, replayed: false }); + const payload = JSON.parse(calls.find((c) => c.url.includes("/__bili/plugin/fork"))!.body!) as Record; + assert.equal("prefixMsgIds" in payload, false); + assert.equal("driftFrom" in payload, false); +}); + +// --------------------------------------------------------------------------- +// Server gate (full chain) — real proxy, real snapshot/fork endpoints. +// --------------------------------------------------------------------------- + +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; + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + const store = new SessionStore({ dir: dir + "/sessions", enabled: false, debounceMs: 60000 }); + _setStoreForTest(store); + _setForTest({}); + const upstream = http.createServer((req, res) => { + req.on("data", () => {}); + req.on("end", () => { + 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 } })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const addr = upstream.address(); + assert(addr && typeof addr === "object"); + const upstreamUrl = `http://127.0.0.1:${addr.port}`; + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstreamUrl, routes: { [upstreamUrl]: { models: { "claude-test": { 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 paddr = proxy.address(); + assert(paddr && typeof paddr === "object"); + return { upstreamUrl, origin: `http://127.0.0.1:${paddr.port}`, close: async () => { store.cancelAll(); proxy.close(); proxy.closeIdleConnections?.(); upstream.close(); upstream.closeIdleConnections?.(); await Promise.all([once(proxy, "close"), once(upstream, "close")]); } }; +} + +// Parent core = 4 cells (the system message hoists out of the fold space): +// [u, a, u, a]. The drift cell is the LAST parent cell (assistant "a4"). +const PARENT4 = [ + { role: "system", content: "system prompt" }, + { role: "user", content: "first original ".repeat(250) }, + { role: "assistant", content: "second original ".repeat(250) }, + { role: "user", content: "third original ".repeat(250) }, + { role: "assistant", content: "fourth original" }, +]; + +test("#6262: full chain — drifted fork adopts past the byte stop and the child replay rides it", async () => { + const h = await harness(); + try { + const seed = await fetch(`${h.origin}/bili/${h.upstreamUrl}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": "parent-sid" }, + body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: false, messages: PARENT4 }), + }); + assert.equal(seed.status, 200, await seed.text()); + + const parentSnap = (await (await fetch(`${h.origin}/__bili/plugin/snapshot?conversationId=parent-sid`)).json()) as { + parentRevision: string; + orderHash: string; + orderedMessages: Array<{ rawId: string; ref: string | null; identityHash: string }>; + }; + assert.equal(parentSnap.orderedMessages.length, 4, "system hoists out of the fold space"); + const rawIds = parentSnap.orderedMessages.map((m) => m.rawId); + + // Child: parent with the last cell re-rendered (same host id, new bytes) + a new tail. + const childMessages = [ + PARENT4[0]!, + PARENT4[1]!, + PARENT4[2]!, + PARENT4[3]!, + { role: "assistant", content: "fourth original RE-RENDERED" }, + { role: "user", content: "new fork tail" }, + ]; + const adoption = await tryForkAdoption({ + base: h.origin, + parentConversationId: "parent-sid", + childConversationId: "child-sid", + body: { model: "claude-test", max_tokens: 1024, stream: false, messages: childMessages }, + msgIds: [...rawIds, "tail-host-id"], + }); + assert.deepEqual(adoption, { outcome: "adopted", branchPoint: 4, replayed: false }, "the id track extends the byte stop (3) to 4"); + + const childSnap = (await (await fetch(`${h.origin}/__bili/plugin/snapshot?conversationId=child-sid`)).json()) as { orderedMessages: unknown[] }; + assert.equal(childSnap.orderedMessages.length, 4, "the child inherits the full parent prefix, drift cell included"); + + // The child's first replay (drifted text) must be accepted — the ledger + // reserved the drift cell's id/ref, so the kernel rides it instead of + // 409ing the child conversation. + const replay = await fetch(`${h.origin}/bili/${h.upstreamUrl}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": "child-sid" }, + body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: false, messages: childMessages }), + }); + assert.equal(replay.status, 200, await replay.text()); + } finally { + await h.close(); + } +}); + +test("#6262: server marks the receipt — status drifted + driftFrom echo; gate failure stays strict (no 409)", async () => { + const h = await harness(); + try { + const seed = await fetch(`${h.origin}/bili/${h.upstreamUrl}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": "p2" }, + body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: false, messages: PARENT4 }), + }); + assert.equal(seed.status, 200, await seed.text()); + const parentSnap = (await (await fetch(`${h.origin}/__bili/plugin/snapshot?conversationId=p2`)).json()) as { + parentRevision: string; + orderedMessages: Array<{ rawId: string; ref: string | null; identityHash: string }>; + }; + const rawIds = parentSnap.orderedMessages.map((m) => m.rawId); + const prefix = parentSnap.orderedMessages; + + // (1) gate passes → status "drifted" + driftFrom echo. + const driftedRes = await fetch(`${h.origin}/__bili/plugin/fork`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ protocolVersion: 1, parentConversationId: "p2", childConversationId: "c-drift", parentRevision: parentSnap.parentRevision, branchPoint: { messageCount: 4, orderHash: forkOrderHashOf(prefix) }, orderedMessages: prefix, idempotencyKey: "fork:c-drift", prefixMsgIds: rawIds, driftFrom: 3 }), + }); + const driftedText = await driftedRes.text(); + assert.equal(driftedRes.status, 201, driftedText); + const driftedBody = JSON.parse(driftedText) as { status: string; driftFrom?: number; inheritedBlocks: unknown[] }; + assert.equal(driftedBody.status, "drifted"); + assert.equal(driftedBody.driftFrom, 3); + + // (2) gate failure (ids ≠ parent rawIds) → the byte-verified fork still + // succeeds, STRICT: status "exact", no driftFrom, no 409. + const strictRes = await fetch(`${h.origin}/__bili/plugin/fork`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ protocolVersion: 1, parentConversationId: "p2", childConversationId: "c-strict", parentRevision: parentSnap.parentRevision, branchPoint: { messageCount: 4, orderHash: forkOrderHashOf(prefix) }, orderedMessages: prefix, idempotencyKey: "fork:c-strict", prefixMsgIds: ["wrong-0", "wrong-1", "wrong-2", "wrong-3"] }), + }); + const strictText = await strictRes.text(); + assert.equal(strictRes.status, 201, strictText); + const strictBody = JSON.parse(strictText) as { status: string; driftFrom?: number }; + assert.equal(strictBody.status, "exact"); + assert.equal("driftFrom" in strictBody, false); + + // (3) prefixMsgIds present but driftFrom === messageCount → normalized + // to no drift: exact. + const normRes = await fetch(`${h.origin}/__bili/plugin/fork`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ protocolVersion: 1, parentConversationId: "p2", childConversationId: "c-norm", parentRevision: parentSnap.parentRevision, branchPoint: { messageCount: 4, orderHash: forkOrderHashOf(prefix) }, orderedMessages: prefix, idempotencyKey: "fork:c-norm", prefixMsgIds: rawIds, driftFrom: 4 }), + }); + const normText = await normRes.text(); + assert.equal(normRes.status, 201, normText); + const normBody = JSON.parse(normText) as { status: string; driftFrom?: number }; + assert.equal(normBody.status, "exact"); + assert.equal("driftFrom" in normBody, false); + + // (4) parse rejections — 400, never a 409, never a silent ignore. + const badLength = await fetch(`${h.origin}/__bili/plugin/fork`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ protocolVersion: 1, parentConversationId: "p2", childConversationId: "c-bad1", parentRevision: parentSnap.parentRevision, branchPoint: { messageCount: 4, orderHash: forkOrderHashOf(prefix) }, orderedMessages: prefix, idempotencyKey: "fork:c-bad1", prefixMsgIds: rawIds.slice(0, 3) }), + }); + assert.equal(badLength.status, 400); + const badShape = await fetch(`${h.origin}/__bili/plugin/fork`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ protocolVersion: 1, parentConversationId: "p2", childConversationId: "c-bad2", parentRevision: parentSnap.parentRevision, branchPoint: { messageCount: 4, orderHash: forkOrderHashOf(prefix) }, orderedMessages: prefix, idempotencyKey: "fork:c-bad2", prefixMsgIds: ["ok", "", "x", "y"] }), + }); + assert.equal(badShape.status, 400); + const driftOnly = await fetch(`${h.origin}/__bili/plugin/fork`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ protocolVersion: 1, parentConversationId: "p2", childConversationId: "c-bad3", parentRevision: parentSnap.parentRevision, branchPoint: { messageCount: 4, orderHash: forkOrderHashOf(prefix) }, orderedMessages: prefix, idempotencyKey: "fork:c-bad3", driftFrom: 3 }), + }); + assert.equal(driftOnly.status, 400); + const driftOob = await fetch(`${h.origin}/__bili/plugin/fork`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ protocolVersion: 1, parentConversationId: "p2", childConversationId: "c-bad4", parentRevision: parentSnap.parentRevision, branchPoint: { messageCount: 4, orderHash: forkOrderHashOf(prefix) }, orderedMessages: prefix, idempotencyKey: "fork:c-bad4", prefixMsgIds: rawIds, driftFrom: 9 }), + }); + assert.equal(driftOob.status, 400); + } finally { + await h.close(); + } +});