Skip to content

feat: v1 legacy-lane routing — opencode-acp old sessions keep working, new sessions go to bili (#920) - #921

Merged
ranxianglei merged 6 commits into
feat/pi-native-pluginfrom
2026-09-19_opencode-v1-legacy-sessions
Sep 18, 2026
Merged

ranxianglei merged 6 commits into
feat/pi-native-pluginfrom
2026-09-19_opencode-v1-legacy-sessions

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #920

What

On OpenCode 1.x, after bili plugin install opencode, old opencode-acp sessions keep working and new sessions run through bili — no read-only-archive compromise.

Mechanism

  • Absorption: loadLegacyAcp() imports the installed opencode-acp package directly (.opencode/node_modules → project node_modules → global npm root → opencode config-scope modules, first hit) by calling its server(ctx) with BILLION_CONTEXT_PROXY unset strictly around the call — its ONLY env check is at entry (probed: index.ts:38), executors never re-check.
  • Legacy predicate: a session is legacy iff acp's state file exists at <XDG_DATA_HOME>/opencode/storage/plugin/acp/<sid>.json (probed: getDefaultStorageDir, lib/state/persistence.ts:67). Ids sanitized before touching the fs.
  • Per-lane routing (createV1ServerHooks):
    • tool slots carry acp's DCP schemas for BOTH lanes (v1 host registers one def per name process-wide); the wrapped executor routes: legacy → acp executor, new → forwardTool to the proxy (plugin mode). acp tool args are kernel-compatible — verified parseCompressArgs accepts the content: [{topic,startId,endId,summary}] object form — so new-session calls forward verbatim.
    • chat.headers: legacy → x-bili-plugin-bypass: 1 only; new → x-bili-plugin + conversation + context-window stamps.
    • x-bili-plugin-bypass makes the proxy forward VERBATIM (no wire injection / nudge / session binding — same skip-path as fix: stitched-stream usage double-count after compress re-request (adapter-anthropic >0 guard) #300 hopMarker / billion-context ACP kernel 改写 body 导致 ZCode (zcode.z.ai) 405/3012 #661 route passthrough).
    • /acp + /dcp: legacy → acp handler, new → bili panel.
    • acp's transforms (system / messages / text.complete) are gated on the legacy predicate → new sessions never enter acp's registry (no adoption, no double compression).
    • acp's config hook runs on a provider-hidden shadow (its /bili/-URL self-disable is neutered); owned top-level keys (permission/command/experimental) merge back by reference change.
  • Degradation: package absent / import fails → bili runs alone; legacy sessions become read-only archives (documented in README EN+zh).

Also in this PR

Note on base

Base is feat/pi-native-plugin (the integration branch #850/#919 merged into), NOT master.

Tests

  • tests/opencode-native-v1.test.ts (24 pass): lane routing (tools/headers/command/transforms per session), shadow-config merge, provider-hidden assertion, legacy state-file detection + id sanitization.
  • tests/plugin-bypass.test.ts (new, 2 pass): bypassed requests reach the upstream verbatim (no injected tools); without the header the same request gets the plugin pipeline.
  • Full suite: 1705 tests, 1703 pass, 2 skipped (E2E gates), typecheck clean, build clean.

Known limits (documented)

  • acp_context_recap has no proxy counterpart — on new sessions the proxy answers with its unknown-tool message.
  • config storagePath overrides in acp are not probed — legacy detection uses the default path.
  • Project-local opencode plugin opencode-acp config entries are still manual removal (installer warns).

Sisyphus added 2 commits September 18, 2026 10:04
…l opencode (#918)

Two compression owners in one host = double compression / ref-space
fights. The installer now removes opencode-acp entries from the global
opencode.json (bare / npm: / versioned / path specs, array or object
shape; original snapshotted to .bili-bak) and warns about project-local
installs it cannot touch. Object-shaped plugin maps are normalized to
their keys so sibling entries survive. Docs (EN+zh) document the strip
and the old-session migration semantics: old <acp> refs live in
opencode-acp's own store, decompress returns not-found, numbering
restarts — treat pre-migration sessions as read-only archives.
…ons, bili owns new ones (#920)

OpenCode 1.x hosts migrating from opencode-acp keep their pre-migration
sessions WORKING. The V1 native entry absorbs the installed opencode-acp
package (loadLegacyAcp: .opencode/node_modules > project node_modules >
global npm root > config-scope modules) by importing its server() with
BILLION_CONTEXT_PROXY unset strictly around the call (its only env check
is at entry), then routes per session:

- legacy session (acp state file exists in
  <XDG_DATA_HOME>/opencode/storage/plugin/acp/<sid>.json) — compression
  runs INSIDE the absorbed acp (its dcp refs, block store, all five
  tools). Model requests carry x-bili-plugin-bypass: 1; the proxy
  forwards them verbatim (no injection/nudge/binding).
- new session — tool executors forward to the proxy plugin endpoints
  (plugin mode). Tool slots carry the DCP schemas for both lanes (v1
  registers one def per name process-wide); the executor picks the lane.

acp's transforms (system/messages/text.complete) are gated on the
legacy predicate so new sessions never enter its registry; its config
hook runs on a provider-hidden shadow (its /bili/ self-disable is
neutered) and owned keys are merged back. /acp and /dcp route per lane.
Degradation: package absent/import-failed -> bili alone, legacy sessions
read-only (documented).

Also: cherry-picked 74701fb (installer strips legacy opencode-acp
entries, #918) — it was pushed to the already-merged #919 branch and
orphaned.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到,开始 review 本 PR。步骤:① 拉取分支核对代码;② 在 PR head 上跑 typecheck / 全量测试 / build;③ 检查 CI 状态。稍后给出结论。

… any segment (#920 review)

Two review hardening fixes:

- loadLegacyAcp now receives the host's full ctx object instead of a
  {directory, client} projection. V1PluginContext types only the fields
  bili uses, but the runtime object may carry extra host fields that
  acp's hooks need at action time (the projection would drop them).
- isLegacyOpencodeAcpEntry matches path entries by ANY segment naming
  opencode-acp instead of only last/parent — deep entry paths like
  <dir>/opencode-acp/dist/index.js were missed, leaving both compression
  owners armed (the double-compression case #918 exists to prevent).
  Test fixture extended with the dist/index.js form.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review 完成。结论:核对无误、本地全绿,可以合并(合并操作按规则由人来)。详情如下。

代码核对(拉分支逐文件过)

验证结果(PR head 上实际执行)

检查 结果
npm run typecheck clean
npm test 1705 tests / 1703 pass / 2 skipped(E2E gates)/ 0 fail —— 与 PR 正文声称完全一致
npm run build success
GitHub CI 仅 ci-e2e-fake("real codex vs deterministic fake upstream")跑了:success。主 ci.yml 只对 target=master 的 PR 触发,本 PR base 是 feat/pi-native-plugin 所以没跑 —— 上面本地全量运行已覆盖该缺口

Review 中发现的两个小问题(已直接修掉,commit 49b290f 推到 PR 分支)

  1. loadLegacyAcp 收到的是投影 ctx({directory, client}):V1PluginContext 只声明了 bili 用到的两个字段,但 host 运行时 ctx 对象可能携带其他字段;若 acp 的 hooks 在 action time 用到它们会被静默丢掉。改为透传 host 的原始 ctx 对象(src/agent/opencode-native.ts:580)。
  2. installer 的路径形态检测偏窄:isLegacyOpencodeAcpEntry 原来只看 last/parent 段,<dir>/opencode-acp/dist/index.js 这类深入口路径会漏检 → 两个压缩 owner 同时 armed,正是 OpenCode 1.x: native v1 support via the standard install path (near-v2 effectiveness) #918 要防的 double-compression 场景。改为任意路径段命中即匹配(src/plugin-install.ts),测试 fixture 补了 dist/index.js 形态。

修复后重跑:typecheck clean,全量 1705/1703 pass/2 skipped/0 fail,build clean;新 head commit 的 e2e-fake CI 同样绿。

已知限制(接受)

  • acp_context_recap 无 proxy 对应物(new session 收到 unknown-tool 回复)——已文档化。
  • legacy 检测只用默认 storagePath(config storagePath override 未探测)——已文档化。
  • 项目级 acp config 条目需手动移除(installer 有 warning)——已文档化。

中文摘要:核对了 v1 legacy-lane routing 的实现与测试(含 cherry-pick 溯源、CI 缺口由本地全量补位),修复了 review 中发现的两个小问题——absorbed acp 拿到的 ctx 被投影丢字段、installer 漏检深层路径形态导致潜在双压缩——已直接推入 PR 分支,可以合并。

- pass output param through all absorbed-acp wrappers (system.transform /
  text.complete / command.execute.before crashed with 'output is undefined'
  — acp hooks are (input, output) two-arg)
- legacyAcpCandidates: include v1 package cache
  ~/.cache/opencode/packages/opencode-acp@<ver>/node_modules (every version,
  newest first) — that is where 'opencode plugin opencode-acp' actually
  installs; project/global node_modules rarely see it
- wire loadLegacyAcp log through from .server() (absorption was silent)

Verified in container: legacy session ses_f4d9 — b1 decompress restores
phase-1 facts, model-created b2 block via absorbed acp, model requests
bypass-forwarded verbatim ([plugin-bypass] in proxy log); new session
ses_f4d821 — tools forward to proxy, session bound (plugin mode).
@ranxianglei

Copy link
Copy Markdown
Owner Author

再review下 有新代码

…920 review)

7223468 fixed real 'output is undefined' crashes in acp's
system.transform / text.complete / command.execute.before handlers, but
the lane-routing test still called those hooks one-arg with fakes that
ignore the second parameter — so a regression of exactly this class
would not have been caught (tests are outside tsconfig.build.json, so
typecheck does not cover them either).

Fakes now record whether output reached them (:output/:no-output marker)
and the assertions require it on the legacy lane. Mutation-verified:
reverting the wrapper to a one-arg call fails the suite.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 第二轮 review 完成(新 commit 7223468 "legacy lane hardening from docker E2E",+36/−15,2 files)。结论:修复方向正确、验证全绿;发现一个测试缺口已直接补上(commit a63fa66),可以合并(合并由人来)。

对新 commit 的逐项核对

1. output 参数透传(核心修复)—— 正确
acp 的 hooks 是 (input, output) 双参,原 wrapper 只传 input,导致 system.transform / text.complete / command.execute.before 在真实 legacy session 上以 'output is undefined' 崩溃 —— 这个 bug 是真实存在的(你们 docker E2E 抓到的),修复方式(wrapper 透传 + V1Hooks 接口把这两个 hook 的 output 改为必选)正确。我额外核对了 fallback 路径:acp["command.execute.before"]?.(input) 保持单参是对的 —— bili 自己的 handler(src/agent/opencode-acp-command.ts:79)签名就是 (input) => Promise<void>,不接收 output。

2. 候选路径加入 v1 package cache —— 正确
~/.cache/opencode/packages/opencode-acp@<ver>/node_modules(尊重 XDG_CACHE_HOME、目录不存在时 try/catch 兜底、first-hit 语义不变)。理由成立:那才是 opencode plugin opencode-acp 的实际安装位置。一个小观察(不阻塞,仅报告):版本排序用的是 .sort().reverse() 字典序而非 semver —— 若同时缓存了 opencode-acp@1.9.0 和 @1.10.0,会先试到 1.9.0。实际影响很小:任意缓存版本都能服务 legacy session(同一 state file 格式/DCP 协议),最坏是吸收到旧版而非最新版。要不要改成 semver 比较由你定。

3. log 接线 —— 符合惯例
.server() 里传入 (msg) => console.log(msg)。该模块是 agent-side plugin(opencode host 进程内运行),文件内已有 8 处 console.log/error,与既有惯例一致(loggerLog 规则约束的是 server-side proxy 模块),不算违规。

验证结果(在 7223468 上实际执行)

检查 结果
npm run typecheck clean
npm test 1705 / 1703 pass / 2 skipped(E2E gates)/ 0 fail
npm run build success
GitHub CI(e2e-fake) success

另确认:无测试对 legacyAcpCandidates 做全等断言,新增 cache 目录不会引入环境相关的 flaky。

Review 中发现并已修的缺口(commit a63fa66,已推 PR 分支)

测试没有钉住这次修的双参契约。 tests/opencode-native-v1.test.ts 里 fake acp hooks 仍只收一个参数且忽略 output,调用处也只传 input —— 也就是说,如果 output 透传再回归(正是 'output is undefined' 这一类),单测不会失败;而且 tests/ 不在 tsconfig.build.json 的 include 里(typecheck 只覆盖 src/**),所以把 V1Hooks 的 output 改成必选后,旧的单参调用也过不了任何自动检查。这次只有你们的 docker E2E 能抓到它。

修法(最小改动):fake hooks 记录 output 是否到达(:output/:no-output 标记),调用处补传 { parts: [] } / { system: [] } / { text: "" },legacy lane 断言要求 :output 标记。做了变异验证:把 wrapper 临时改回单参调用,恰好 1 个测试 AssertionError 失败 —— 证明新断言真的钉住了这个回归。修复后重跑:typecheck clean、全量 1705/1703 pass/2 skipped/0 fail、build clean、新 head CI 绿。

中文摘要:核对了 docker E2E 硬化的三项修复(output 双参透传、package cache 候选路径、log 接线)均正确且本地/CI 全绿;发现并直接修复了"双参契约无单测钉住"的测试缺口(变异验证过),另报告了一个不阻塞的小观察(cache 版本排序为字典序非 semver),可以合并。

…ode install (#927)

- pick ONE effective plugin key by host major version (>=2 -> plugins,
  1.x/probe-failure -> plugin): 1.x hard-errors on 'plugins', 2.x merges
  both keys, so dual-writing is impossible and the probe fallback can
  never write a dead line
- write target = OPENCODE_CONFIG override, else highest-precedence file
  already present (opencode.jsonc > opencode.json > config.json), else
  create opencode.json — no more second file next to an existing config
- JSONC targets get surgical top-level-key edits (jsonc-parser), so user
  comments/formatting survive byte-for-byte; plain JSON keeps the
  historical full re-serialize; broken targets are refused, never
  overwritten
- compaction.auto is set to false only when the target does not already
  say auto:false (single source of truth); remove/status now recognize
  our entry under either key spelling and strip legacy acp entries from
  both
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.

1 participant