Repository navigation
feat(opencode): native plugin mode for OpenCode 2.x — self-spawning proxy (pi pattern) + #819 node-runtime fix - #824
Conversation
…wning proxy in the plugin Follows the pi-native pattern (#706) so both hosts share one bootstrap core. - src/agent/native-bootstrap.ts: side-effect-free helpers shared by pi-native and opencode-native (nativeProxyScriptPath / nativeBootstrapGate / singleFlight); importing it never spawns a proxy. - src/agent/opencode-v2.ts: V2 factory createOpencodeV2Setup() extracted from opencode.ts (tool registration, context-window refresh, compaction-boundary reporting, header stamping), parameterized by an optional per-request route(e, state) hook. - src/agent/opencode-native.ts: native entry billion-context-opencode-native. Bootstraps a proxy via ensureProxyRunning on load (no bili daemon needed); its http.request hook rewrites model-API URLs to <proxy>/bili/<url> by replacing the request reference (Request.url is readonly, #810). Liveness is probed against GET /__bili/health before every send; a dead proxy respawns with a 15s cooldown, and failed respawn degrades to direct sends with one warning, auto-recovering on later requests. - src/launcher.ts: resolveNodeRuntime() fixes #819 — ensureProxyRunning no longer spawns process.execPath blindly, which breaks when the host process is not Node itself. Resolution: live node -> BILLION_CONTEXT_NODE override -> PATH search -> actionable error. - src/plugin-install.ts: bili plugin install opencode also installs the native plugin as wrapper dir <configDir>/plugins/billion-context/index.js re-exporting dist/agent/opencode-native.js (OpenCode 2.x requires directory-form plugin entries, #754), disables compaction.auto with merge preservation (.bili-bak restore on remove), and makes the MCP shell optional (missing live proxy origin skips it with a note instead of failing). - tests: new tests/opencode-native.test.ts (gate, route rewrite, respawn/degrade, setup integration, kill switch); launcher.test.ts gains resolveNodeRuntime units + spawn-runtime wiring; plugin-agent.test.ts roundtrip extended for plugin dir / compaction merge+restore plus a no-proxy case. Also repairs the dsh phase-4 launch test finally block referencing an undeclared prevNoProxy (pre-existing ReferenceError; DSH_HOME leak fixed too). - docs: README OpenCode 2.0 Native bullet; CHANGELOG entry.
[bot] 🏷 Reviewing PR #824 now. It's stacked on |
[bot] 🏷 Review complete — LGTM, ready to merge once #706 lands (stacked base). Per the [PR] protocol I did not redo the work — pulled Pre-flight (re-run, all green): CI note: because this PR targets Code review highlights (all sound):
Small issues — fixed directly on the branch (commit
One discrepancy to sort out (details on #825): the PR body says this change "also repairs a pre-existing broken test" (dsh phase-4) — but that repair is not in this diff, and I could not reproduce the failure on master. See my evidence there before the "Note:" paragraph reads as accurate. Verdict: no blocking issues. Merge after #706. 审查结论:opencode 原生插件模式(自启动代理 + #819 node-runtime 修复)实现与测试全部本地复核通过,直接修了 README 两处小问题(缩进漂移、表格缺 native 模式指引)已补在分支上( |
…xtensions can back off under native mode (#820) Verified against billion-context-pi and opencode-acp sources that their existing auto-disable does not cover native mode: both check BILLION_CONTEXT_PROXY only at extension-load time (native bootstrap writes it asynchronously after the proxy is up) and detect /bili/ in the configured baseUrl (native rewrites at the fetch layer, invisible to config). Launcher mode and manual wiring keep working as before. - native-bootstrap.ts: markNativeHost() — synchronous, first writer wins - pi-native.ts / opencode-native.ts: set BILLION_CONTEXT_NATIVE=<host> at module evaluation, gated on the same shouldBootstrap*() condition as the bootstrap itself (opt-outs and launcher-managed launches leave it unset) - README 'Which do I need?': mutual-exclusion note; CHANGELOG entries - tests: marker assertions in pi-native/opencode-native files + units
|
看有回归问题吗? |
…ds (#809 D/N4) Two safety defects found while hands-on testing #824 (reported in #809). D - ensureProxyRunning registered only child.on("exit"), not "error". An async spawn failure (EACCES/ENOENT on the resolved runtime) emits "error", so with no listener it became an uncaughtException that killed the host process, while the poll loop misreported "did not become healthy" after burning SPAWN_WAIT_MS. Register an "error" handler, break the poll on it, and reject with the cause. N4 - opencode.json may carry a non-object "mcp" (e.g. a bare string); `"bili" in <non-object>` throws TypeError. Install tolerated it via its catch ("skipped"), but remove had no guard and crashed, leaving an irreconcilable half-installed state; status returned "error:". Guard all three sites with isPlainMcpObject: install defaults absent->{} and skips present-non-object, while remove/status degrade gracefully so remove always cleans up (reversibility). Regression tests: launcher.test.ts (child "error" handler registered + rejects "spawn failed"); plugin-agent.test.ts (install/remove/status survive a string mcp). Full suite green: 1652 pass / 0 fail / 2 pre-existing skip.
Add an attach sub-mode to the opencode native entry (#824 vehicle): when BILLION_CONTEXT_ATTACH=<url> is set, model traffic routes through that pre-existing proxy instead of spawning one — no ownership, no respawn. Per owner decision (#809): an explicitly-specified external proxy fails CLOSED on death (always rewrite toward the target, never fall back to direct, plus one loud diagnostic); unset defaults to the self-spawn watchdog (attach off by default). Precedence off > attach > spawn — a launcher-owned proxy or opt-out stands down even over an explicit attach. - native-bootstrap.ts: nativeAttachOrigin() env resolver (http(s)-only, trailing-slash normalized). - native-intercept.ts: NativeInterceptState.attach flag. - opencode-native.ts: planNativeOpencode() three-way posture decision; the route gains an attach branch sharing the extracted rewriteToProxy helper (self-spawn path unchanged). - tests/opencode-native.test.ts: resolution, precedence, and fail-closed route behavior (7 cases). typecheck clean; 1652 pass / 0 fail / 2 skip (win32-only); build OK. Refs #809.
|
看有回归问题吗? |
[bot] 🏷 没发现回归。 做了严格的 A/B 对比(base
base 上通过的每个用例在 PR 上仍然通过,失败数恒为 0。新增的 存量代码面核对(diff vs base,只有这些既有文件被动过):
集成层:ci-e2e-fake(真 codex 走构建产物打 fake upstream,zero tokens)在 PR head 上绿——它端到端覆盖了本 PR 唯一动过的共享运行时路径(launcher spawn,#819 修复)。 残余风险如实说:单测覆盖不到的行为变化理论上存在,但共享代码 diff 很小且已逐行 review,加上 e2e-fake 兜底,我认为回归风险可忽略。(注:stacked base #706 自身的回归归 #706 管,不在本 PR diff 范围内。) 回归核查结论:base 与 PR head 全量测试 A/B 均为 0 失败,存量代码改动面已逐一核对(主管线零改动),ci-e2e-fake 端到端绿——无回归发现。 |
…status (ranxianglei#836) opencode.json may carry a non-object `mcp` (e.g. a bare string); `"bili" in <non-object>` throws TypeError. remove had no guard and crashed, stranding a half-install (broken reversibility); status surfaced "error:". Guard all three sites with isPlainMcpObject: install skips a present non-object mcp, while remove/status degrade to "not installed" so remove always completes cleanly. Found hands-on testing ranxianglei#824 (reported in ranxianglei#809 as finding N4). An equivalent guard also ships inside ranxianglei#824; this PR lands it independently on master, which was still exposed today. Regression test added in tests/plugin-agent.test.ts. Full suite green: 1650 tests / 1648 pass / 0 fail / 2 pre-existing skip. typecheck + build clean.
…ianglei#835) ensureProxyRunning registered only child.on('exit'), never 'error'. An async spawn failure (EACCES/ENOENT on a mis-resolved runtime) emits 'error', which with no listener becomes an uncaughtException that kills the host process, while the poll loop burns the full SPAWN_WAIT_MS and misreports 'proxy did not become healthy'. runClient already handled 'error' correctly; the proxy-spawn path did not. Register an 'error' handler, break the poll on it, and reject with the spawn cause ('bili: proxy spawn failed (<detail>)'). Regression test added to tests/launcher.test.ts (asserts an 'error' handler is registered + rejects 'spawn failed'). Found as finding D of ranxianglei#809 while hands-on testing ranxianglei#824; the identical change already lives in open PR ranxianglei#824 (which targets feat/pi-native-plugin, not master). This brings it forward to master independently so the defect is closed on the mainline. Fixes ranxianglei#835.
Implements the opencode line of #820 (native-mode umbrella), per the #809 design. Modeled on pi-native (#706) so both hosts share one bootstrap core.
Stacked: base is
feat/pi-native-plugin, not master. This branch reuses #706'snative-intercept.tsfoundation (not yet on master) — merge after #706.What
OpenCode 2.x now runs billion-context as an ordinary plugin — no
bililauncher, no daemon:bili plugin install opencodewrites a wrapper dir<configDir>/plugins/billion-context/index.jsre-exportingdist/agent/opencode-native.js(directory form is mandatory for OpenCode 2.x plugin entries, feat: OpenCode 2.0 support — V2 launcher mode + pure-proxy validation (#735) #754), keeps writing the MCP shell when a live proxy origin exists (now optional — missing origin skips it with a note instead of failing), and setscompaction.auto=falsemerge-preserving, restoring from.bili-bakon remove (single compression owner).ensureProxyRunning(spawn-in-plugin, same as pi-native).http.requesthook rewrites model-API URLs to<proxy>/bili/<url>by replacing the request reference (Request.urlis readonly, feat: opencode 2.x no-launcher npm-plugin deployment #810). Liveness is probed againstGET /__bili/healthbefore every send; dead proxy → respawn (15s cooldown); failed respawn → one warning + direct sends, auto-recovering on later requests.src/agent/opencode-v2.ts(createOpencodeV2Setup, parameterized by an optional per-requestroute(e, state)hook);opencode.tskeeps its V1+V2 export shape unchanged.src/agent/native-bootstrap.ts(used by pi-native and opencode-native); importing it never spawns anything.#819 fix included
ensureProxyRunningspawnedprocess.execPathunconditionally — under a non-Node host process (e.g. the Go-based opencode binary loading the plugin) that spawns the host binary instead of Node. NewresolveNodeRuntime(): live node →BILLION_CONTEXT_NODEoverride → PATH search → actionable error; injectable viaLauncherDeps.nodeRuntime.Coexistence with standalone extensions (#820 follow-up)
Verified against the
billion-context-piandopencode-acpsources that their existing auto-disable does NOT cover native mode: both checkBILLION_CONTEXT_PROXYonly at extension-load time (native bootstrap writes it asynchronously after the proxy is up) and detect/bili/in the configured baseUrl (native rewrites happen at the fetch layer, invisible to config). Launcher mode and manual wiring keep working as before.This PR adds a synchronous
BILLION_CONTEXT_NATIVE=<host>marker set at module evaluation (gated on the same shouldBootstrap*() condition as the bootstrap itself; first writer wins;markNativeHost()insrc/agent/native-bootstrap.ts) so the standalone extensions can stand down at action time. Standalone-side adoption is tracked in ranxianglei/billion-context-pi#461 and ranxianglei/opencode-acp#405. README "Which do I need?" documents the mutual exclusion.Pre-flight
npm run typecheck✅npm test✅ 1649 pass / 0 fail / 2 skipped (e2e gated)npm run build✅; builtdist/agent/opencode-native.jssmoke-tested (export shape, no accidental bootstrap underNODE_TEST_CONTEXT)E2E_CHECK=1). Full 4-phase run needs a live local upstream(
http://127.0.0.1:8199/v1) which is unavailable in this agent sandbox (ECONNREFUSED) —not a regression: the run confirmed the proxy spawn path this PR touches works (proxy came up,
served codex, forwarded cleanly). CI (
ci-e2e.yml, manual dispatch with secrets) covers the full run.tests/opencode-native.test.ts(gate, route rewrite, pending/failing bootstrap, respawns + cooldown, give-up-once, setup integration, kill switch),tests/launcher.test.tsresolveNodeRuntime units + spawn-runtime wiring,tests/plugin-agent.test.tsroundtrip extension (plugin dir, compaction merge+restore, idempotency) + no-proxy install caseNote: also repairs a pre-existing broken test found while working — the
runLaunch dsh … (#535 phase 4)finally block referenced an undeclaredprevNoProxy(ReferenceError on every run; DSH_HOME env leak fixed in the same block). Filed separately.