Skip to content

[Bug] opencode system-context reconcile:每次 AGENTS.md 编辑使 Responses 会话 key 里的 instructions 指纹漂移,压缩状态被重置(master 现存) #1102

Closed

Description

@ranxianglei

来源:PR #1092 深审的 salvage 分析(评论:https://github.com/ranxianglei/billion-context/pull/1092#issuecomment-5762997680)。该 PR 的 commit 04c39e4(src/server.ts:1350-1366)是对这个 bug 的一个修复尝试,但它 load-bearing 于锚(见下),不能原样拆走,需要独立设计。

Bug

master 上 Responses 会话 key = subagentNamespace(<identity>, instructions),其中 instructions 参与指纹。opencode 的 system-context reconcile 会话中 instructions 随 AGENTS.md 编辑漂移 → 同一逻辑会话被 fork 成孤儿 |sub:<fp> 会话 → 锚/压缩状态重置,折叠视图丢弃。同一 bug 面向所有 instructions 会中途变化但人格不变的客户端。

为什么不能直接拆 04c39e4

该 hunk 只在 opts.stableSystemAnchor 开启时跳过 instructions 指纹,正确性依赖锚把 instruction drift 吸收为 notes。单独落地会把真不同人格(同 conversation id、不同 instructions)并进同一 session/fold 空间——正是 subagentNamespace 的 instructions 指纹要防的。

建议方向

  • 把 trusted-codexTurn 待遇扩展到提供原生 turn/conversation 标记的客户端(原生稳定 id 优先,AGENTS §7.3 "Prefer native stable identifiers");
  • 或 config gate:显式声明 "persona 固定、instructions 可漂移" 的客户端列表;
  • 区分 "same persona, updated instructions"(本 bug)与 "different persona"(指纹必须继续防御)。

附带 salvage 清单(来自 #1092,与 #1097 相关)

Activity

  1. ranxianglei commented on Sep 21, 2026

    @ranxianglei
    OwnerAuthor

    🤖 Powered by ework · qwen3.8-27b

    [bot] 🏷 Triage: bug confirmed on master; independent fix designed (NOT lifting #1092's hunk). Implementation starting now.

    1. Reproduce / verify

    • Master keying path: src/server.ts:1348 → subagentNamespace(identity, instructions). The kernel anchors the first-seen instructions fingerprint per conversation id and namespaces any later different fingerprint as <id>|sub:<fp> (acp-kernel subagentNamespace). So any mid-conversation instructions drift forks the session and resets anchor + compression state. Confirmed from the code path, not just the report.
    • Verified opencode's side in upstream source (anomalyco/opencode, dev branch, packages/opencode/src/session/llm/request.ts): every model request stamps x-session-affinity / X-Session-Id = the session id, and task-tool subagents get their OWN child session ids (sessions.create({ parentID })). One conversation id = one persona in opencode, so same-id + drifting instructions can only mean system-context reconcile (AGENTS.md edit) — never a persona switch. For this client the fingerprint is pure harm.

    2. Layer judgment
    Root cause is in session-key derivation (identity → session mapping), not wire shape: the instructions fingerprint exists to separate personas that SHARE one conversation id (Codex threads, #150). It is misapplied to clients whose native ids are already persona-scoped. The fix belongs where the key is derived — the same site as #1092's hunk, but trust must come from verified client semantics instead of the anchor feature.

    3. Design (independent of #1092)
    Skip the instructions fingerprint ONLY when the winning conversation signal is a verified persona-scoped native identifier:

    • opencode native headers: x-session-affinity (value ses_…) / x-opencode-session;
    • plugin declaration: new protocol header x-bili-plugin-instructions-mutable: 1, honored only together with x-bili-plugin + x-bili-plugin-conversation. Only the opencode plugin stamps it — other hosts (dsh/hermes/…) stay unverified and keep today's behavior.

    Everything else keeps the fingerprint unchanged: Codex body/header/metadata session ids (#150 guard), x-claude-code-session-id (Claude subagents share the main session id, #970), grok/mcode/generic x-session-id, prompt-cache-key. Zero regression surface; the existing #150 e2e guard stays green.

    Rejected alternatives: blanket trust of all client-provided ids (would re-merge #150's case); comparing old-vs-new instructions content (fragile across compaction/truncation boundaries); user config gate (bug persists by default until configured — the declaration belongs with the integration that knows the host's semantics).

    Next: unit matrix + e2e regression (same ses_ id, edited instructions → one session, compression state carried over) + plugin-mode variant, then a PR linked here.

    中文摘要:确认了 opencode 编辑 AGENTS.md 后 Responses 会话被 instructions 指纹分裂成孤儿、压缩状态重置的 bug(master 现存);按"仅已验证的 persona-scoped 原生会话标识才跳过 instructions 指纹"独立设计修复(opencode 原生头 + 插件声明头),其余客户端防御保持不变,正在实现并准备提 PR。

  2. ranxianglei commented on Sep 21, 2026

    @ranxianglei
    OwnerAuthor

    🤖 Powered by ework · qwen3.8-27b

    [bot] 🏷 ## 修复完成 → PR #1104

    一句话摘要:修了 Responses 会话 instructions 指纹随 AGENTS.md 编辑漂移、fork 孤儿 |sub:<fp> 会话并重置压缩状态的问题——只对"已验证 persona-scoped"的会话 id(opencode 原生 ses_ 头 / 插件显式声明)跳过 instructions 指纹,其余客户端全部保留指纹,#150 的人格隔离保证不受影响;可以合并(建议合并前用 manual-dispatch CI e2e 补跑一次 codex 全流程)。

    审核结论(动手前验证了什么)

    • 复现确认:master 上 src/server.ts Responses 分支的会话 key = subagentNamespace(identity, instructions);kernel 侧 subagentNamespace 对同一 identity 首次见到的非空 instructions 锚定指纹,之后漂移即 fork |sub:<fp>。opencode 每次请求都把当前 AGENTS.md 渲染进 instructions(system-context reconcile)→ 每次编辑都触发 fork,折叠视图丢弃、压缩状态归零。bug 属实,层次判断与 issue 一致:根因是指纹把 "same persona, updated instructions" 误判为 "different persona",不是锚/压缩机制本身的问题。
    • 为什么不拆 04c39e4:同意 issue 的判断——该 hunk 的正确性依赖 stableSystemAnchor 把 drift 吸收为 notes,单独落地会把真不同人格(同 conversation id、不同 instructions)并进同一 session/fold 空间。本 PR 走独立设计。
    • 方案选择(issue 给了三个方向):采用"trusted 原生 id 待遇扩展" + "客户端显式声明"的组合,没有做全局 config gate——gate 需要用户主动配置,默认不配时 bug 依旧存在;而声明头随客户端集成自带,默认就对。内容相似度启发式否决(在 compaction/truncation 边界不可靠)。

    信任集(每项都对客户端源码核实过)

    信号 处置 依据
    x-opencode-session / x-session-affinity: ses_… 跳过指纹 opencode 每个模型请求都盖自己的 session id;task tool 子代理经 sessions.create({ parentID }) 拿全新子 session id(opencode 仓库 packages/opencode/src/session/llm/request.ts、tool/task.ts)→ 一个 id 只对应一个人格
    x-bili-plugin-conversation + x-bili-plugin-instructions-mutable: 1 跳过指纹 插件显式声明宿主 id 是 persona-scoped;目前只有 opencode 两个插件(V1 hooks + V2 setup)盖章,dsh/hermes 等未核实语义的宿主不盖
    codex body/header/metadata session id(#150)、x-claude-code-session-id(Claude 子代理共享主 session id,#970)、grok/mcode/generic、pck 保留指纹 现状零回归面;真不同人格共享同 id 的场景仍被防住

    改动面

    src/session-id.ts(抽出 conversationHeaderSource() + 新增 instructionsFingerprintExempt(),信任集及理由全写在注释里)、src/server.ts(Responses 分支对豁免信号按原文 keying,与 trusted codexTurn 分支同等待遇)、src/plugin.ts(新常量 + manifest 条目)、两个 opencode agent 插件(盖章)、PLUGIN.md(文档)、新增 tests/responses-instructions-drift.test.ts。

    验证证据

    • 新测试 7/7 通过(5 单元:信任集各 case 含 claude 头优先于 affinity、marker 门控等;2 e2e:native 头漂移 / 插件声明漂移,断言无 fork + 压缩状态跨漂移延续)。
    • 负向对照:只回退 src/server.ts 重跑 → 两个 e2e 均失败(fork 复现),证明测试真的咬住了修复。
    • 全量 pre-flight:npm run typecheck 干净;npm test 2169/2171 通过(2 skip = ACP_TEST_E2E gated);npm run build 干净。
    • codex e2e:E2E_CHECK=1 零 token 预检通过;ACP_TEST_E2E=1 全流程在本沙盒跑不了——本地 sglang upstream(127.0.0.1:8199)拒绝连接,环境问题,与本改动无关(codex identity 路径未动,body session_id 仍走指纹)。建议合并前 manual-dispatch 跑一次 CI e2e 兜底。

    附带发现(仅报告,不开新 issue)

    1. kernel 折叠时会话的第一条 user message 即使被 active block 覆盖也会原样转发(acp-kernel rebuildMessages 先 pin firstUserIndex 再 prune)——有意设计,master 同样如此,与本 bug 正交;测试已用 seed 消息对规避并在注释里写明。
    2. salvage 清单里其余两项(c0b041c anthropic cache_control 断点稳定测试、Prepared.systemNotes 通道形状)仍归属 feat: stable head-system anchor for prefix-cache preservation (#1085) #1092/Feature: content-addressed message store — ID-referenced tool results + acp_retrieve (built-in CCR) #1097,不在本 PR 范围。

    PR: #1104 (Fixes #1102)

  3. ranxianglei commented on Sep 22, 2026

    @ranxianglei
    OwnerAuthor

    附带发现提交 issue
    本 issue 根本原因是按照 hash 判断消息导致的吗

  4. ranxianglei commented on Sep 22, 2026

    @ranxianglei
    OwnerAuthor

    🤖 Powered by ework · qwen3.8-27b

    [bot] 🏷 ## 回答:根因不是"按 hash 判断消息",是会话 key 的输入选择

    先分清两个不同层次的 "hash"

    ① 消息级身份(健康,与本 bug 无关)
    kernel 给每条消息分配 content-hash id + 单调递增的 mNNNNN 引用号,会话内永不复用。压缩状态(block、折叠视图)是挂在会话上的,不是按消息 hash 找的。这一层工作正常。

    ② 会话级命名空间(真正的根因所在)
    master 上 Responses 分支的会话 key = subagentNamespace(identity, instructions)(acp-kernel wire 模块),逻辑是:

    const fp = hashId(instructions);          // 当前请求 instructions 的指纹
    const anchor = anchors.get(identityValue); // 该 conversation id 首次见到的指纹
    if (anchor === void 0) { anchors.set(identityValue, fp); return identityValue; }
    return anchor === fp ? identityValue : `${identityValue}|sub:${fp}`;  // 漂移 → fork

    同一个 conversation id,第一次见到的 instructions 被锚定为基准;之后任何一轮 requests 里 instructions 文本变了 → fp 不等 → 会话 key 变成 <id>|sub:<fp> → 孤儿会话,压缩状态归零。opencode 每次 AGENTS.md 编辑都把新内容重渲染进 instructions,于是每编辑一次 fork 一次。

    所以根因是什么

    不是 hash,是把"会中途变化的字段"放进了会话 key。 hash 在这里只是比较原语——把 hashId(instructions) 换成字符串全等比较,这个 bug 一个字节都不会变。缺陷是设计决策层面的隐含假设:"instructions 变了 ⇒ persona 变了"。对 opencode 这类原生 id 就是 persona-scoped(一个人格一个 session id,子代理拿全新子 id)的客户端,这个假设不成立:instructions 变了但人格没变,那是 "same persona, updated system context"。

    PR #1104 的修法也印证这一点:不是删掉指纹机制(codex 线程跨人格复用同一 session id #150、Claude 子代理共享主 session id #970——对它们 instructions 变化确实意味着不同 persona,指纹必须保留),而是把已验证 persona-scoped 的信号(opencode 原生 ses_ 头 / 插件显式声明)从这一个维度里摘出去,按原文 keying。

    一句话:病因在"key 由什么构成"(输入选择),不在"用什么方式比较"(hash 与否)。


    关于"附带发现提交 issue"

    系统规则规定工作会话不能开新 issue(自动开的 issue 会再触发 agent 会话,形成无限 issue/PR 循环;issue 必须由人发起)。所以我把可直接粘贴的 issue 稿准备好了,请你在对应仓库点一下新建:

    第一条发现 → 归属 ranxianglei/acp-kernel(行为在 kernel 侧)

    标题:

    [行为确认] rebuildMessages pin 住第一条 user message:即使被 active block 覆盖仍原样转发——请确认意图并写入契约文档

    正文(4 反引号内整段复制):

    来源:billion-context#1102(https://github.com/ranxianglei/billion-context/issues/1102,opencode instructions 指纹漂移)分析时发现。该行为与本 bug 正交(master 与修复后表现一致),但下游 e2e fixture 被迫适配它,故提交确认 + 文档化。
    
    ## 行为
    
    `src/prune.ts` 的 `rebuildMessages`(acp-kernel 0.0.82 dist 核实)中,会话的**第一条 user message** 即使被 active block 覆盖也总是原样转发:
    
    ```js
    if (index === firstUserIndex && firstUserIndex >= 0) {
      result.push(messages[index]);
      continue;   // ← 跳过了下面的 covered 检查
    }
    if (covered.has(messages[index].id)) continue;
    ```
    
    pin 检查先于 covered-by-active-block 的剪枝检查执行。因此压缩包含第一条 user message 的范围后,后续重建的 wire 里会同时出现渲染后的 summary(`[Compressed conversation section] …`)和第一条 user message 的完整原文;其余被覆盖消息正常从 wire 消失。
    
    ## 证据
    
    - billion-context PR #1104(https://github.com/ranxianglei/billion-context/pull/1104)设计 e2e fixture 时观察到不对称折叠:compress 执行、block active(effIds 覆盖两条目标消息)之后,下一轮出站 body 中——user 侧目标当时是会话第一条 user message(m00001),按此规则仍原样转发;assistant 侧目标(m00002)正常消失。
    - master 与修复后表现一致,kernel 行为,与 bili 侧任何改动无关。
    - billion-context 测试现用"压缩目标前放一对 seed 消息"规避,并已在测试注释中写明该 pin。
    
    ## 影响
    
    - 下游消费者无法对第一条 user message 做压缩后 wire 字节级断言(fixture 必须适配)。
    - 估算压缩后 wire 体积 / prefix-cache 稳定性时需计入 pinned 消息保持原文(第一条 user message 常是任务描述,可能很长)。
    
    ## 问题 / 建议处理
    
    看起来是有意设计(pin 住任务开场白以在压缩后保留 framing)。若有意:请在 kernel 契约/spec 文档中写明——billion-context AGENTS.md §7.3 已注明 "Codifying the kernel-side spec is a separate acp-kernel change — deferred"。若非有意:pin 检查应移到 covered 检查之后(或当消息属于 active block 时跳过)。

    第二条发现(salvage 清单两项归属 #1092/#1097):这条不是缺陷,只是范围归属说明——c0b041c 的 cache_control 断点稳定测试和 Prepared.systemNotes 通道形状本来就挂在已打开的 #1092/#1097 上,无需另开 issue;需要的话你在那两个 issue 里加一句交叉引用即可。

  5. ranxianglei commented on Sep 22, 2026

    @ranxianglei
    OwnerAuthor

    问题你帮我报告

  6. ranxianglei commented on Sep 22, 2026

    @ranxianglei
    OwnerAuthor

    🤖 Powered by ework · qwen3.8-27b

    [bot] 🏷 已按你的指示代为提交。

    acp-kernel#341(行为在 kernel 侧,故开在归属仓库):
    ranxianglei/acp-kernel#341

    • 标题:[行为确认] rebuildMessages pin 住第一条 user message:即使被 active block 覆盖仍原样转发——请确认意图并写入契约文档
    • 正文即上一条我贴出的稿子,开头带来源标记(来源: billion-context#1102 …分析时发现),含行为、代码证据(0.0.82 dist 核实的 pin 先于 covered 检查)、PR fix(responses): keep persona-scoped sessions stable across instruction drift #1104 的观测、影响、以及"若有意→写文档 / 若非有意→调整检查顺序"两条处理建议。

    说明:这条是行为确认 + 文档化类 issue,不是 bug 修复单;它不阻塞本 issue 的 PR #1104。第二条附带发现(salvage 两项归属 #1092/#1097)如前述只是范围归属,未单独开单。

    @Dog 需要我改标题、加标签或补充内容的话直接说。

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