Skip to content

零注入会话身份:移除模型可见 conversation_id,以出站 tool_use 环见证路由 MCP 工具调用(fixes #1611 的非注入路线) #1685

Closed

Description

@ranxianglei

背景

设计

1. 移除一切模型可见的 id

  • 删 withConversationIdNote 及 server.ts 三处调用(:3575 / :3804 / :4093)。
  • 删 MCP 工具 schema 上的 conversation_id 参数(plugin.ts CONVERSATION_ID_PARAM/withConversationIdParam,compress-tool.ts SEARCH_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 绿。

取代关系

Activity

  1. ranxianglei commented on Sep 29, 2026

    @ranxianglei
    OwnerAuthor

    🤖 Powered by ework · qwen3.8-27b

    [bot] 🏷 开工。方案按 issue 设计实现:零 id 注入 + 出站 tool_use 环见证路由。

    计划:

    1. 基于最新 master(v0.1.172 之后)新建分支;独立实现,不基于 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。
    2. 删除 withConversationIdNote 及 server.ts 全部调用点、MCP manifest 上的 conversation_id 参数(含 search_context 跨会话参数的 schema 侧)、mcp.ts 错误文案改为 host 侧出路。
    3. 新增环见证模块(per-session 环形缓冲 N≈32 + TTL≈10min),在三个 loop adapter 的 buffered tool-call 完成点记录 sha256(name + stableStringify(args))→会话;handlePluginTool 路由优先级:ring 命中 > body.conversationId > 单活跃仲裁 > 响亮 400。
    4. 测试:环单元(记录/反查/TTL/N 上限/多候选拒猜)、路由集成(无 id 命中、ring 胜 body id、无环记录仲裁/400)、注入缺席断言。
    5. typecheck / 全量测试 / build 绿后开 PR(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,取代 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)。
  2. ranxianglei commented on Sep 29, 2026

    @ranxianglei
    OwnerAuthor

    Implementation PR: #1692 (branch 2026-09-29_zero-id-ring-routing).

    Removes every model-visible conversation id (wire system note, manifest conversation_id params, 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 in handlePluginTool. Local gates: typecheck/build green, 18+6 new tests green, flipped suites green, goldens regenerated, full-suite sweep shows zero regressions vs local master.

  3. ranxianglei commented on Sep 29, 2026

    @ranxianglei
    OwnerAuthor

    🤖 Powered by ework · qwen3.8-27b

    [bot] 🏷 Review of #1692 (71f3aa73, branch 2026-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 的设计立场;修的是根因不是症状。

    ② 回归审查(逐项既有行为):

    • 出站 system prompt:少一行 [Your bili conversation id: …] note —— 有意变更(本 issue 目的),5 个注入点全删,无残留引用(grep 确认 withConversationIdNote/CONVERSATION_ID_PARAM/withConversationIdParam 已消失)。
    • MCP manifest schema:三种格式全部去掉 conversation_id 参数 —— 有意变更;goldens(bili.json / manifest-default / manifest-optin)纯删属性,我本地跑了 scripts/update-wire-contract-goldens.ts 重新生成后与提交版本逐字节一致(符合 wire-contract ledger 规则)。
    • 旧模型回显 echo 的 conversation_id:delete args.conversation_id legacy strip 保留,canonical 反查路径(resolveConversation fallback)未动,issue760 测试仍覆盖 canonical-id 路由 —— 保持。
    • mcp.ts shim:无 id 调用从「硬失败报错」改为「转发给代理」—— 有意变更且是设计自洽所必需(若 shim 硬失败,无 id POST 到不了代理,环永远用不上;issue §1 那条文案要求与 §2 内部矛盾,PR 的解法是对的)。host 出路改由代理侧 400 文案承载(CLAUDE_CODE_SESSION_ID / BILI_CONVERSATION_ID 绑定)。
    • search_context MCP 侧模型可见跨会话目标移除,机器通道保留(keepForSearch 仍透传 conversation_id 给 executeSearchContextTarget)—— 有意收窄,issue 明确要求。
    • log 行格式变为 executed via plugin (routed by X, #1685) —— 无害。
    • ensureCanonicalId 仍存活(session.ts),未被误删成死代码。
    • 结论:本版本引入回归:0 处;所有行为变更均为 issue 声明的有意变更。

    ③ 代码关:路由阶梯与 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 测试缺口(建议后续补),可以合并。

  4. added 9 commits that reference this issue on Sep 29, 2026
  5. added 2 commits that reference this issue on Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions