Skip to content

fix: key OpenCode 2.x child sessions by their own x-opencode-session-id (#2779) - #2792

Merged
ranxianglei merged 3 commits into
masterfrom
2026-10-11_opencode-native-session-id
Oct 13, 2026
Merged

ranxianglei merged 3 commits into
masterfrom
2026-10-11_opencode-native-session-id

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #2779

Root cause

OpenCode 2.x stamps each model request's OWN session id in x-opencode-session-id, while x-session-affinity / X-Session-Id / x-opencode-session all carry the inherited cache-routing affinity (parentID ?? fork source ?? own id, packages/core/src/session/affinity.ts). The identity chain in conversationHeaderSource read only the affinity-side headers, so a parent and all of its subagent/fork siblings resolved to ONE conversation key and shared mutable ACP state.

Verified against source:

  • opencode 2.x: packages/core/src/session/model-request.ts:278-295 (per-request header stamping) + packages/core/src/session/affinity.ts:7-8.
  • opencode 1.x (v1.15.13, packages/opencode/src/session/llm/request.ts:168-184) stamped its OWN id into the affinity headers and never sent x-opencode-session-id — the change is inert for 1.x and restores 1.x isolation parity for 2.x.

Fix

Insert x-opencode-session-id into the priority list between x-mavis-session-id and x-session-affinity (client-specific trust class; blank values fall through as before). Upstream headers and cache keys are forwarded byte-unchanged; explicit full-plugin identity precedence (x-bili-plugin-conversation under the x-bili-plugin marker) is retained.

Behavior change (old -> new)

Proxy mode, OpenCode 2.x only: subagent/fork sessions are now keyed by their OWN ses_ id instead of the inherited parent affinity (old: shared mutable ACP state with the parent; new: isolated). Root sessions unchanged (affinity === own id there). 1.x traffic unchanged (header absent). Plugin mode unchanged (plugin-header precedence preserved). No config/env surface change.

Tests

  • tests/proxy-session-id.test.ts: chain-order unit cases (beats the affinity trio; lone header; whitespace-only falls back; claude/mavis/plugin precedence retained).
  • tests/responses-instructions-drift.test.ts: winning-header assertions incl. blank fallback and plugin precedence.
  • tests/e2e-session-identity.test.ts: five new hermetic e2e tests over chat-completions / responses / anthropic — interleaved parent+sibling requests yield THREE independent compression states; upstream-passthrough asserts confirm x-session-affinity/x-session-id/x-opencode-session reach upstream unchanged alongside the child's own x-opencode-session-id; plus blank-header fallback and plugin-precedence tests.

Local: targeted suites 50/50 green, typecheck clean, build green. Full suite left to CI.

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-11_opencode-native-session-id (d83ac65)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3304-pr.2792.71d9d95

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr2792.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei
ranxianglei force-pushed the 2026-10-11_opencode-native-session-id branch from 3cffa59 to 62a0f88 Compare October 11, 2026 13:05
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR #2792 复审完成(本 PR 是 #2779 的交付物,此轮按 PR Review 规范对代码与行为做了完整审查,并把分支 rebase 到当前 master)。

① 原 issue 方向关:#2779 已在 #2779 线程里对照 opencode 2.x 源码(model-request.ts + affinity.ts)与 1.x(v1.15.13 request.ts)核实——bug 成立且根因定位正确(身份选择链缺原生头,不是 affinity 语义问题);修法打在身份选择层、不动上游头部与 cache key,方向无误。

② 回归审查(逐消费点清点,rebase 后 HEAD 上复核):identity 链的全部消费者逐一过过——

③ 代码审查:核心改动一行(names 数组在 x-mavis-session-id 与 x-session-affinity 之间插入 x-opencode-session-id)+ 信任级注释;空值沿用既有 trim fall-through(session-id.ts:74);400-hint 文案与 README en/zh 同步更新。无 as any、无无关改动。

④ 测试有效性:新测试是真测试——5 个 hermetic e2e(chat/responses/anthropic)交错父+兄弟请求断言三个独立会话(bug 回归时会塌成 1 个,抓得住)+ 上游透传头逐字节断言;单测钉住链序、空头回落、claude/mavis/plugin 优先级。未加 gate,CI 每次必跑。

⑤ diff 整洁度:7 文件 +152/-6,每一行都与本 PR 目的相关(核心 1 行+注释、hint 字符串、文档、测试),干净。

结构化八问

  1. 是什么问题:OpenCode 2.x 的 subagent/fork 子请求自身会话 id 只在 x-opencode-session-id 里,三个亲和头全带父级缓存路由亲和键,导致父与兄弟共享一份可变压缩状态。
  2. 什么原因导致的:身份优先级表早于 2.x 定型;2.x 把原生 id 移入新头、旧三头改为承载缓存路由亲和键,列表未收录新头,回落读到亲和头。
  3. 影响范围与严重程度:仅 proxy 模式 + OpenCode 2.x + 有 subagent/fork 的会话——特定条件下的数据级污染(状态互串、baseline/snapshot 归属错乱),非完全不可用;root 会话与 1.x 流量不受影响。
  4. 怎么修复的:给身份选择链的 client-specific 信任级补上该原生头(插在 mavis 与 affinity 之间),空值沿用既有 fall-through;不改写任何上游头部/cache key。
  5. 是否有回归问题:无。上面②的逐点清点全部保持;唯一可见变化是 web UI 里子会话标签显示自身 id 而非父 id(内部展示,更准确)。
  6. 是否破坏缓存:不影响——上游 x-session-affinity/X-Session-Id/x-opencode-session/promptCacheKey 原样转发(e2e 有逐字节断言);变化的只是 bili 内部会话键推导。
  7. 是否改了设计方向:没有,仍是单一 per-request 身份选择模型。
  8. 是否引入新配置:没有。

行为变更披露(old→new):proxy 模式下 OpenCode 2.x 的 subagent/fork 会话:旧 = 按继承父亲和键归并(与父共享 state)→ 新 = 按自身 ses_ id 独立成会话。root 会话不变(affinity===own id);1.x 不变(从不发该头,反而恢复 1.x 隔离对等性);plugin 模式不变(plugin 头优先保留)。

本轮操作:master 在 PR 开出后前进了 4 个提交(#2781/#2788,均 storage 域,与本 PR 零文件重叠)→ 已 rebase 到当前 master(e7b94a2e0),新 head 62a0f8899;rebase 后本地定向验证:typecheck 干净、三个触及套件 50/50 绿、build 绿(build-info commit=62a0f8899)。全量套件交 CI。

判定:代码可合;等新 head 的 CI 全绿后即可合并,合并由人执行。

@ranxianglei
ranxianglei force-pushed the 2026-10-11_opencode-native-session-id branch 3 times, most recently from c5daee3 to 51e7e59 Compare October 11, 2026 14:28
…id (#2779)

OpenCode 2.x stamps each model request's OWN session id in x-opencode-session-id while x-session-affinity / X-Session-Id / x-opencode-session all carry the inherited cache-routing affinity (parentID ?? fork source ?? own id). The identity chain read only the affinity-side headers, so a parent and all of its subagent/fork siblings resolved to one conversation key and shared mutable ACP state. Insert x-opencode-session-id into the priority list between x-mavis-session-id and x-session-affinity (client-specific trust class); blank values fall through as before. Upstream headers and cache keys are forwarded byte-unchanged. 1.x traffic is inert (it never sends the header — it stamped its own id into the affinity headers, so this restores 1.x isolation for 2.x); root sessions keep their current key (affinity === own id).
@ranxianglei
ranxianglei force-pushed the 2026-10-11_opencode-native-session-id branch from 51e7e59 to 5f70fc7 Compare October 11, 2026 15:02
ework-agent and others added 2 commits October 11, 2026 23:27
…ntion)

The suite sends anonymous requests whose prefix-affinity chains were
persisted into the machine-global stateDir()/prefix-affinity.json,
sharing the file with every other un-isolated suite running in parallel
on the CI runner. Add the standard XDG_* mkdtemp isolation used by the
other e2e suites so the suite stops being a writer to the shared pool.
@ranxianglei
ranxianglei merged commit 87060e5 into master Oct 13, 2026
32 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.346-master.87060e5 (rolling master tag).

npm install -g billion-context@0.0.346-master.87060e5  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

@ranxianglei
ranxianglei deleted the 2026-10-11_opencode-native-session-id branch October 15, 2026 05:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Native OpenCode children share mutable ACP state when parent affinity wins over x-opencode-session-id

1 participant