Repository navigation
Closed
Description
背景
- conversation-id line in the static system prompt breaks the upstream prefix cache on fork/resume/session-switch (cached≈0 despite byte-identical history) #1611:
withConversationIdNote(src/compress-tool.ts:410)把 per-conversation id 行拼进静态 system → fork/--continue/resume 时历史字节相同但 id 行不同 → 供应商逐字节前缀 LCP 在 system 内断裂 → cached=0(实测 116 条消息逐字节继承 input=71727 cached=0)。 - fix: conversation id rides the ephemeral tail note, not the static system (#1611) #1614 / fix: carry per-conversation id in an ephemeral trailing note, not the static system (#1611) #1620:两条在途修复(指针注+门控尾注 / 无条件尾注)仍让模型看见 id —— bug(chain-checkpoint): 裸戳记进入模型上下文后被复述,并可能造成可见污染与空完成轮 #1565 已证明注入元数据会被模型复述成伪上下文,任何形式的模型可见 id 都是同类风险源。
- 新设计立场:模型上下文零 id 字节——不给它看,它也不需要带。模型从「身份信使」降级为「只表达意图(压缩哪个范围)」,身份由机器通道携带。
设计
1. 移除一切模型可见的 id
- 删
withConversationIdNote及 server.ts 三处调用(:3575 / :3804 / :4093)。 - 删 MCP 工具 schema 上的
conversation_id参数(plugin.tsCONVERSATION_ID_PARAM/withConversationIdParam,compress-tool.tsSEARCH_CONTEXT_CONVERSATION_ID_PARAM跨会话参数)。 - mcp.ts:263 的「pass the conversation_id argument — see the 'your bili conversation id' line」错误文案改为 host 侧出路(
BILI_CONVERSATION_ID/ 独立 shim)。 - HTTP body 的
conversationId字段保留(扩展 forwardTool / shim env 绑定填的是它,机器通道不受影响;旧会话里模型已抄进 args 的 id 也继续被读,无破坏性迁移)。
2. 出站 tool_use 环见证路由(ring witness)
代理是模型输出的唯一必经之路:任何 MCP 工具调用必然先出现在某会话的响应流里,代理流出时记下它——
- 记录点:三个 loop adapter 的 buffered tool-call 完成处(
toolCallsEmitted汇聚点,adapter-openai / adapter-anthropic / adapter-responses)。 - 键:
sha256(name + stableStringify(args))(复用 prefix-affinity 的stableStringify,键序规范化,host 重新序列化不破匹配)。args 里的 conversation_id 原样入哈希(环记录的是被流出的内容,两侧同源)。 - 值:会话 id。环形:每会话最近 N 条(~32)+ TTL(~10min),内存上限恒定。
无 body id 的 POST /__bili/plugin/tool 按同一哈希反查 → 命中流出它的那个会话。摘要文本(数百字符自由文本)是天然唯一键;跨会话逐字节碰撞天文级不可能,若碰撞(多候选)拒猜,响亮失败。
3. 路由优先级
ring 命中(见证证据) > body.conversationId(扩展/env 通道) > 单活跃仲裁 > 响亮 400(host 侧出路)
ring 优先于 body id 的理由:claude code 子 agent(#970)共享 shim,env 绑定指向主会话,而子 agent 的 tool_use 流在子会话(claudeSubagentSplit 派生 <id>|sub:<agentId>)——环见证比 env 更准。扩展通道(pi/opencode/dsh 的 sessionIdOf(ctx))与环一致时无冲突,分歧时见证胜出。
4. 三层各司其职(互不干扰)
| 层 | 匹配什么 | 环是否触碰 |
|---|---|---|
| bili 会话身份(prefix-affinity,#1148 剥前导 system) | 消息链内容哈希 | 否 |
| 供应商 prompt cache | 出站请求逐字节 LCP | 否(环不上 wire) |
| 环见证 | 代理内存 tool_use 哈希→会话 |
— |
请求/wire 零字节改动:没有注入就没有剥离,fork 后身份照旧、供应商缓存照继承。
边缘与残留风险(如实)
- 代理重启(流出→POST 之间,窗口秒级):环丢 → body id / 单活跃仲裁兜底。
- host 结构性篡改 args:哈希失配 → 仲裁路径。
- 误路由防护是弱保证:m-ref 跨会话可重叠(都从 m00001 起),
parseCompressArgs对目标会话可能解析成功——硬保证靠环命中本身,仲裁仅在环未命中时介入。 - 安全性净收紧:现状是模型在 args 里写任意
conversation_id代理照单全收;环设计把无 body id 路由限制为「本代理亲自见证过该内容」或「排除法单候选」。
验收
- 环单元测试:记录/反查/TTL/N 上限/多候选拒猜。
- 路由集成测试:无 id POST 命中正确会话;ring 与 body id 分歧时 ring 胜;无环记录时单活跃仲裁/400。
- 注入缺席测试:system/工具 schema/错误文案中不再出现 pfa- id 或 conversation_id 指引。
- typecheck / 全量测试 / build 绿。
取代关系
- Fixes conversation-id line in the static system prompt breaks the upstream prefix cache on fork/resume/session-switch (cached≈0 despite byte-identical history) #1611(以零注入方式,而非 system/尾注注入方式)。
- 取代 fix: conversation id rides the ephemeral tail note, not the static system (#1611) #1614、fix: carry per-conversation id in an ephemeral trailing note, not the static system (#1611) #1620(两者均可关闭;本 issue 独立实现,不基于它们)。
Metadata
Metadata
Assignees
Labels
No labels
Activity
ranxianglei commentedon Sep 29, 2026
[bot] 🏷 开工。方案按 issue 设计实现:零 id 注入 + 出站 tool_use 环见证路由。
计划:
withConversationIdNote及 server.ts 全部调用点、MCP manifest 上的conversation_id参数(含 search_context 跨会话参数的 schema 侧)、mcp.ts 错误文案改为 host 侧出路。sha256(name + stableStringify(args))→会话;handlePluginTool路由优先级:ring 命中 > body.conversationId > 单活跃仲裁 > 响亮 400。ranxianglei commentedon Sep 29, 2026
Implementation PR: #1692 (branch
2026-09-29_zero-id-ring-routing).Removes every model-visible conversation id (wire system note, manifest
conversation_idparams, search_context description) and adds the outbound tool_use witness ring (src/tool-ring.ts) with recording on all five plugin response lanes + the routing ladder inhandlePluginTool. Local gates: typecheck/build green, 18+6 new tests green, flipped suites green, goldens regenerated, full-suite sweep shows zero regressions vs local master.ranxianglei commentedon Sep 29, 2026
[bot] 🏷 Review of #1692 (
71f3aa73, branch2026-09-29_zero-id-ring-routing)先说清楚一件事:#1692 不是我开的。我此前按本 issue 做了部分平行实现(plugin.ts 路由阶梯 + tool-ring.ts),你的评论指向 #1692 后我核实了该 PR 真实存在且完整,遂停止重复劳动、清理了本地冗余改动,转为独立 review(loop guard:一个 issue 最多一个 PR)。以下是审查结论。
① 方向关:正确。#1611 的根因是 per-conversation id 行落在静态 system 里,fork/resume 时 system 字节变化 → 供应商逐字节前缀 LCP 断裂 → cached=0。零注入让出站请求跨 fork 字节稳定,环不上 wire,正是 issue 的设计立场;修的是根因不是症状。
② 回归审查(逐项既有行为):
[Your bili conversation id: …]note —— 有意变更(本 issue 目的),5 个注入点全删,无残留引用(grep 确认withConversationIdNote/CONVERSATION_ID_PARAM/withConversationIdParam已消失)。conversation_id参数 —— 有意变更;goldens(bili.json / manifest-default / manifest-optin)纯删属性,我本地跑了scripts/update-wire-contract-goldens.ts重新生成后与提交版本逐字节一致(符合 wire-contract ledger 规则)。conversation_id:delete args.conversation_idlegacy strip 保留,canonical 反查路径(resolveConversation fallback)未动,issue760 测试仍覆盖 canonical-id 路由 —— 保持。executed via plugin (routed by X, #1685)—— 无害。ensureCanonicalId仍存活(session.ts),未被误删成死代码。③ 代码关:路由阶梯与 issue 优先级逐条吻合(唯一 witness > body id,分歧时 witness 胜并留 info log;多候选无 body id → 响亮 400 refuse-to-guess;多候选有 body id → body id 胜不猜;无 witness → body id → 10min 单活跃仲裁 → 400)。记录点选在 5 条 plugin 响应 lane 比 issue 设想的 loop adapter 更精准(MCP bili 工具只走 plugin lane),settle 在 res.end 前、成功/catch 双路径都覆盖,observe-only 不碰转发字节(#1039 verbatim 不变量完好)。两处有意偏离 issue 文本均已在 PR body 声明且合理(witnessHash 两侧剥 legacy id 防 echo fork key;shim 删硬失败而非改文案)。
发现一个非阻塞小缺陷(src/tool-ring.ts
recordToolWitness):环形驱逐只处理最旧槽位,当同一 hash 在窗口内出现 ≥2 次(如 ring=[X,Y,X] cap=3 时再记 Y)会误删中段存活条目的索引 → 该 witness 提前 miss。失败方向安全(回落 body id/仲裁,绝不误路由),触发条件苛刻(同一 name+args 在近 32 条见证内重复)。建议后续小修:驱逐前检查 hash 是否仍在环中。④ 测试有效性:真测试,断言有牙。tool-ring.test.ts 18 例钉住哈希归一化/host 前缀两侧/legacy strip parity/键序无关/unparseable 不记录/N 上限索引同步驱逐/多候选集 + handlePluginTool 全阶梯(真 session store,断言执行落点)。tool-witness-pipes.test.ts 6 例把记录侧钉在全部 5 条 lane(含 openai 分片累积、anthropic partial_json 累积、responses item+args.done 合并)。两个缺口:(a) TTL 过期没有单测(issue 验收明列 TTL,需要一个假时钟接缝);(b) 无名 tool call 不记录的负向用例缺失(次要)。都不阻塞。
⑤ diff 整洁度:15 文件全部 on-topic,golden churn 有再生成依据,README en/zh 同步,无跑题改动。
验证证据:我在 detached worktree 复验 head sha——typecheck ✅、npm test 3264 tests / 0 fail / 12 skip ✅、build ✅;CI 13/13 check-runs 全绿(含 e2e、windows/ubuntu 双平台、hermetic registry、codex/dsh/pi/opencode 四车道)。mergeable_state=blocked 不是 CI 原因,应是待人工 approve。触碰 wire/message shape,按 auto-merge gate 属 human-merge 范畴。
可以合并。 中文摘要:修复了 #1611 的前缀缓存断裂根因——把模型可见的 conversation_id 全部移除、改用代理对出站 tool_use 的环见证做零注入路由,代码方向正确、回归清点零意外、测试有牙且有 CI 全绿背书,仅余一个安全方向的驱逐小缺陷和一个 TTL 测试缺口(建议后续补),可以合并。
fix(#1685 review): duplicate-hash eviction/sweep must keep the witnes…
fix: zero-injection session identity — route id-less MCP tool calls b…
fix(#1685 review): duplicate-hash eviction/sweep must keep the witnes…
chore: regenerate wire-contract goldens after rebase (#1685)
test: flip #1712 range-variant pin to the zero-injection contract (#1685
fix: zero-injection session identity — route id-less MCP tool calls b…
fix(#1685 review): duplicate-hash eviction/sweep must keep the witnes…
chore: regenerate wire-contract goldens after rebase (#1685)
test: flip #1712 range-variant pin to the zero-injection contract (#1685
Merge pull request #1692 from ranxianglei/2026-09-29_zero-id-ring-rou…
Merge pull request #1773 from ranxianglei/2026-09-30_e2e-plugin-tool-…
test: e2e the ranxianglei#1685 plugin tool routing ladder + MCP shim …
test(e2e): plugin-lane grow-and-compress — MCP host loop folds via /_…
fix: consume Codex per-call _meta.threadId in the MCP shim (ranxiangl…
fix: stamp nativeCaller on host-native agent tool forwarding (ranxian…