diff --git a/AGENTS.md b/AGENTS.md index 857a4b4a60..685bdeba4f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -13,7 +13,7 @@ | Language | TypeScript (strict, ESM) | | Build | tsup (bundling, inlines acp-kernel) | | Test | Node.js built-in: `node --import tsx --test tests/*.test.ts` | -| Runtime Dep | `acp-kernel` (bundled at build time, zero runtime deps in dist) | +| Runtime Dep | `acp-kernel` (bundled at build time) + `zod` (external, only used by `dist/agent/opencode-native.js` V1 tools; `dist/index.js` stays dependency-free) | ### Repository Info @@ -65,7 +65,7 @@ billion-context/ │ ├── compress-tool.ts # compress tool parsing (kernel parseCompressArgs) │ ├── decompress-shared.ts # Shared decompress logic │ ├── orphan-gc.ts # Orphaned block cleanup -│ ├── agent/ # Thin agent-side plugins (pi/omp/opencode) +│ ├── agent/ # Thin agent-side plugins (pi/omp/opencode; opencode-acp-command.ts = shared /acp hooks V1+V2, opencode-native.ts = self-spawn native, V1 `.server()` + V2 `setup`) │ ├── web/ # Web UI (config + context windows) │ ├── fetch-util.ts # HTTP fetch with timeout │ └── util.ts # Misc utilities @@ -76,7 +76,7 @@ billion-context/ ### Key Design Decisions -1. **acp-kernel is bundled inline** — tsup does NOT list it in `external`, so `dist/index.js` is self-contained (zero runtime deps) +1. **acp-kernel is bundled inline** — tsup does NOT list it in `external`, so `dist/index.js` is self-contained. Exception: `zod` (exact `4.1.8`, matching the opencode host's own zod so V1 plugin-tool shapes interoperate) is a real dependency and stays external — only `dist/agent/opencode-native.js` imports it (lazily, at plugin-tool registration); `dist/index.js` and every other entry remain zod-free. When zod cannot be resolved at runtime the V1 plugin degrades to plain proxy mode instead of failing. 2. **Tags use XML format** `m00001` — written with hex escapes (`\x3c`, `\x3e`) to avoid Write/Edit tool stripping 3. **Auto-update**: checks npm registry every 3 min (`CHECK_INTERVAL_MS = 3*60*1000`), first check per process ignores throttle 4. **Tee logger**: all proxy logs go through `src/logger.ts` (file + stderr). Do NOT use `console.error` in server-side modules — use `loggerLog()`. diff --git a/CHANGELOG.md b/CHANGELOG.md index 726c4c4517..08a722ced2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,14 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ## [Unreleased] +### Features + +- **Coexistence marker for standalone extensions (#820)**: the pi-native and opencode-native entries now set `BILLION_CONTEXT_NATIVE=` synchronously at module evaluation (gated on their own bootstrap gate, first writer wins — `markNativeHost()` in `src/agent/native-bootstrap.ts`), so the standalone `billion-context-pi` / `opencode-acp` extensions can stand down at action time when running alongside a native entry. Verified against both sibling repos that the pre-existing auto-disable does NOT cover native mode: they 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. Standalone-side adoption of the marker is tracked in each repo's issue tracker. +- **opencode 2.x native mode: package-installed opencode gets plugin-mode compression with no launcher (#820, npm-package design #809)**: `bili plugin install opencode` now installs, alongside the MCP shell (kept, but optional — a missing live proxy origin skips it instead of failing the install), a self-spawning native plugin: a stable wrapper directory `/plugins/billion-context/index.js` re-exporting `dist/agent/opencode-native.js` (OpenCode 2.x rejects bare FILE paths in `plugin` — the directory's index.js is the entrypoint, #754 probe) plus `compaction.auto=false` (single compression owner; merge-preserving, restored from the `.bili-bak` backup on remove). At plugin load the same gate as pi-native applies (`BILI_NATIVE_OPENCODE=0` opt-out; skipped entirely under a `bili` launch) and the package's own proxy is bootstrapped through `ensureProxyRunning` (attach-or-spawn, ephemeral port, parent-pid watchdog, `deps.scriptPath`). OpenCode plugins have no fetch seam to patch, so egress interception uses the `http.request` hook, replacing the outgoing fetch Request REFERENCE with `new Request("/bili/", old)` carrying method/headers/body (WHATWG Request.url is read-only at runtime — verified live, #810). Header stamping, native tools and compaction-boundary reporting reuse the shared V2 setup factory (`src/agent/opencode-v2.ts`, extracted from opencode.ts so launcher mode and native mode share ONE protocol implementation). Because an http.request hook cannot observe send failures (the host swallows them), every request liveness-probes `/__bili/health` of the current origin before routing — one respawn if the proxy died mid-session, direct send + one-time warning when no origin can be made healthy (auto-recovering: each request re-verifies, unlike pi's lifetime latch). Also fixes #819: `ensureProxyRunning` resolves the Node executable for spawning via `resolveNodeRuntime()` (live Node wins → `BILLION_CONTEXT_NODE` override → PATH search) instead of blind `process.execPath`, which inside a host binary (opencode/pi native) is the HOST executable — the spawned proxy died at spawn. +- **pi native mode: bare `pi` gets plugin-mode compression with no launcher (#519)**: with this package installed (pi's package manager or `bili plugin install pi`), the pi manifest now loads `dist/agent/pi-native.js`, which at extension load (1) spawns the package's own proxy (`ensureProxyRunning` — ephemeral port, parent-pid watchdog = the pi process, attaches to a healthy compatible instance instead of doubling), (2) patches `globalThis.fetch` to rewrite model-API requests to `/bili/` (verified end-to-end: pi's SDK stack resolves fetch from the global at first-request client construction, so a load-time patch always wins; SSE streams pass through untouched; a proxy death mid-session triggers one respawn + retry), and (3) exports the existing `createBiliPlugin` factory with `BILLION_CONTEXT_PROXY` set — tools, `x-bili-plugin` headers, `/acp` and the compaction cancel all reuse the launcher-mode code paths. The patch matches only model-API URL shapes (`/v1/messages`, `(v\d+/)?chat/completions|completions|responses|conversations` suffixes — version unpinned for zhipuai `/v4/…`, path-prefixed for bailian `/apps/anthropic/v1/messages`) and never touches the proxy's own `/bili/`|`/__bili/` paths or non-HTTP(S) URLs; a failed/timeout bootstrap degrades to direct sends with a one-time warning. Opt-out: `BILI_NATIVE_PI=0`. Skipped entirely under a `bili` launch (`BILLION_CONTEXT_PROXY`/`BILI_PROVIDER_REWRITES` set) — `bili pi` keeps injecting `dist/agent/pi.js` as before. `ensureProxyRunning` gained `deps.scriptPath` (inside pi, `process.argv[1]` is pi's own script, not the package bin). Coexistence: the legacy `billion-context-pi` adapter detects the proxy via a `/bili/` baseUrl prefix or `BILLION_CONTEXT_PROXY`, both invisible under native mode (fetch-layer rewrite; env written asynchronously) — so BOTH installed used to mean double compression. `bili plugin install pi` removes the legacy entry, and the synchronous `BILLION_CONTEXT_NATIVE=pi` marker (see above) lets the standalone extension stand down at action time once it adopts the check. ### Fixes +- **pi native mode: a failed mid-session respawn degrades to direct sends instead of rethrowing the proxy's network error (#513 audit)**: when the spawned proxy died and the one-shot respawn also failed, the interceptor logged an `onDispatch(url, "direct")` but then re-threw the original `TypeError` — the model call died instead of degrading, and `BILLION_CONTEXT_PROXY` stayed set, so the event-time `session_before_compact` cancel kept cancelling native compaction while all traffic went direct (context grows uncompressed, forever). The give-up path now sends the request direct (same contract as bootstrap failure, one-time warning) and fires a new `onGiveUp` hook; pi-native uses it to clear the env so event-time ownership checks disarm together with the traffic. Also: rebuilding a Request-object input against the rewritten target no longer routes caller-side defects (already-consumed or locked body) into the proxy-death branch — they surface as-is instead of respawning an orphaned proxy. - **Proactive nudge fires on upstreams that NEVER report usage — no more unbounded climb to the hard limit (#728)**: ChatGPT-login-style backends omit `usage.input_tokens` entirely (`response.completed` carries none), so `lastInputTokens` stayed 0 for the whole session — and the kernel's `decideNudge` is structurally unfireable at tokenCount == 0 (growth reference falls back to tokenCount itself → growth ≡ 0; mass-ready and all pressure bands require usage ≥ their pct lines). Proactive compression therefore never fired and contexts climbed unbounded until preflight's hard-limit emergency path took over (#726 reached ~1.32M tokens against a 1.05M window before the ~630K-token emergency summarization — which then 502-looped). `effectiveTokenCount` fed 0 for explicit-identity zero-baseline sessions on the "self-heals via the next measured usage report" assumption, which breaks for never-reporting upstreams. The fix mirrors how OpenAI handles exactly this in codex itself (server-observed usage first-class, local estimator fallback underneath — verified against `openai/codex` sources during triage): each `prepare*` now records the char-count upper bound of THAT turn's outbound payload (post-fold processed messages + system/tools overhead + image tokens) into `session.stats.localInputEstimate`, and `effectiveTokenCount` feeds it ONLY while `lastInputTokens == 0`, capped by the current request's inbound upper bound so a stale-high reading can't outlive a client-side shrink. Real usage always takes precedence the moment any report lands (same invariant as #604's `armFailureShrink` exception — the upper-bound estimator only errs early, compresses earlier, never later); turn 1 of a fresh explicit session is byte-identical to before (nothing measured yet → still 0); anonymous-prefix-affinity forks keep their raw-inbound-bound regime untouched; the estimate self-corrects after every fold (post-fold outbound payload shrinks → next reading drops, no inflation even though proxy clients re-send full raw history every turn); it is cleared on the native-compaction-boundary reset. Net effect: silent-backend sessions get incremental compression from the moment the local estimate crosses the nudge thresholds instead of one giant emergency fold at the wall. Rebase onto current master exposed an interaction with #817's per-body preflight dead-end cooldown key: the nudge text injected into the system prompt changes every turn, so keying the cooldown on the sha256 of the proxy-REBUILT wire body misses on identical client retries once the fallback arms the nudge — every retry re-burned the doomed summarization walk, defeating #726's quota protection on exactly the silent backends this fix targets. The cooldown identity now hashes the client's raw inbound request body instead, so proxy-side injections (nudge/system/tools/tags) can no longer defeat it. Tests pin: silent-backend multi-turn (turn 1 idle → turn 2 nudges, preflight silent), real-usage precedence (reported tokens beat a ~90% payload), stale-high cap (shrunk history does not re-trigger). - **Bump acp-kernel 0.0.74 → 0.0.77; `acp_cache` held off every wire surface until adoption (#800)**: the pin jumps three kernel releases — 0.0.75 (cache-reconciliation core: `acp_cache` tool + `buildCacheReport`; recommend tail-below-gate fix), 0.0.76 (Google/Gemini wire codec; recommend range integrity), 0.0.77 (nudge ranges ordered/merged by position instead of ref number — fixes #887). Kernel ≥0.0.75 ships a fifth proxy tool, `acp_cache`, but this proxy has no execution path for it: an intercepted call fell through `executeProxyTool` into the unknown-tool branch, which reports SUCCESS with `[Unknown proxy tool: acp_cache]` — a model calling it would get a plausible-looking result that did nothing. Until adoption lands (feature #800, owner decision pending), `src/compress-tool.ts` derives `PROXY_TOOL_NAMES` and the four `BILI_ACP_TOOLS_*` arrays from the kernel constants minus `UNADOPTED_KERNEL_TOOLS` (`{acp_cache}`) — the single choke point every serving surface consumes (Responses/OpenAI/Anthropic tool arrays, plugin manifest + allowed list, absorb gate, Responses-adapter classification), so the tool stays off the wire everywhere without touching call sites. `MUTATING_PROXY_TOOLS`/`READONLY_PROXY_TOOLS` stay raw re-exports (classification sets only; `acp_cache` is correctly classified readonly and no serving surface reads them). The #841 parity test (`tests/search-context-cross-session.test.ts`) extends the "no other tool may change" contract against the FILTERED kernel side only, so a future BILI-side leak of an unadopted tool still fails. - **A render tag wrapped around the model's whole turn no longer reaches the client as orphan markup (PR #872)**: a model imitating the render tag can write its entire turn — tool call included — where the opening's attributes are still open (recorded shape: `` (launcher, preferred) or `/bili/` prefix | +**Native mode vs standalone extensions.** The host-native plugins (`bili plugin install pi` / `opencode` — they spawn the proxy inside the host process) and the standalone in-process extensions (`billion-context-pi`, `opencode-acp`) are **mutually exclusive**: both active means double compression. The installer makes the switch: `bili plugin install pi` replaces the legacy `npm:billion-context-pi` entry, and `bili plugin install opencode` strips legacy `opencode-acp` entries from the global opencode.json — bare name, `npm:` alias, versioned (`opencode-acp@stable`), or path form, array or object shape; the original config is snapshotted to `opencode.json.bili-bak`. A **project-local** install (`opencode plugin opencode-acp` writes `/.opencode/opencode.json`, not the global config) is not touched — remove it by hand; the installer note reminds you. As a runtime safety net for manual installs, the native entries set `BILLION_CONTEXT_NATIVE=` synchronously at load so a standalone extension can stand down at action time — its own load-time `BILLION_CONTEXT_PROXY` check cannot see a proxy that native mode spawns asynchronously, and its `/bili/` baseUrl check never sees the fetch-layer rewrite. + +**Legacy sessions from opencode-acp (v1 lane routing).** On OpenCode 1.x, `bili plugin install opencode` keeps pre-migration sessions WORKING: the native entry absorbs the installed `opencode-acp` package (imported directly from `node_modules` — `.opencode/node_modules`, project `node_modules`, global npm root, or opencode's config-scope modules, first hit wins) and routes per session. A session is legacy iff opencode-acp's persisted state file exists (`/opencode/storage/plugin/acp/.json`): + +- **Legacy session** — compression runs through the absorbed opencode-acp (its own `` refs and block store keep working: `compress` / `decompress` / `search_context` / `acp_status` / `acp_context_recap` all execute in it). Its model requests carry `x-bili-plugin-bypass: 1`, and the proxy forwards them VERBATIM — no wire injection, no nudge, no session binding. +- **New session** — bili owns it: tool calls forward to the proxy's plugin endpoints (plugin mode). The tool slots the model sees carry the DCP schemas (one def per name process-wide on v1), but the executor routes by session lane, so a new session's `compress` reaches the proxy while a legacy session's reaches opencode-acp. `acp_context_recap` has no proxy counterpart — on new sessions the proxy answers it with its unknown-tool message. + +`/acp` and `/dcp` route the same way. Adoption of new sessions into opencode-acp's registry is prevented by gating its transforms (system / messages / text.complete) on the legacy predicate. Degradation: when the opencode-acp package is absent or fails to import, bili runs alone and legacy sessions behave as read-only archives (old `` tags render, `decompress` returns `[Block … not found]`, new refs restart from m00001). + ## Install ```bash @@ -286,9 +295,37 @@ contract: never fires, the plugin stays inert and the session transparently runs in plain proxy mode (wire-level tool injection) instead of breaking — observed on two adjacent `dev` builds (2026-09-13 / 2026-09-14) whose API surfaces - differ from each other (#754 review probes); conversely verified end-to-end - on `@opencode/cli` 2.0.3 (native `acp_status` executed through the plugin - endpoint, zero wire-level injection). + differ from each other (#754 review probes); conversely verified end-to-end + on `@opencode/cli` 2.0.3 (native `acp_status` executed through the plugin + endpoint, zero wire-level injection). +- **Native (no launcher):** with the package installed from npm, run + `bili plugin install opencode` — it writes a self-spawning plugin into your + real opencode config (`/plugins/billion-context/index.js` → + `dist/agent/opencode-native.js`) and sets `compaction.auto: false`, after + which plain `opencode` works as-is. At load the plugin bootstraps its own + proxy (attaches to a healthy instance instead of doubling; parent-pid + watchdog kills it when opencode exits), routes model-API traffic to + `/bili/`, and exposes the same native bili tools as + launcher mode — no fixed port, no env var, no launcher. + Opt-out: `BILI_NATIVE_OPENCODE=0`. If no proxy can be made healthy, requests + go direct (uncompressed) with a one-time warning and recover automatically. + Under a `bili opencode` launch this entry is skipped entirely (the launcher + owns the proxy). + + The same wrapper serves **OpenCode 1.x** through the V1 `.server()` hooks + (verified on 1.14.46 and 1.18.31): the `config` hook mutates the shared + config object in-process to rewrite every provider `options.baseURL` to + `/bili/…` and sets `compaction.auto: false`; `chat.headers` stamps + the plugin headers per request; `tool` registers the bili tools with real + zod shapes (zod is a runtime dependency — when it cannot be resolved the + plugin degrades to plain proxy mode: rewrite only, wire-injected tools); + the `/acp` command renders the same status panel. Providers **without** + an explicit `baseURL` (SDK defaults, e.g. bare `@ai-sdk/openai` → + api.openai.com) are caught by a global `fetch` patch (the pi-native + mechanism) that reroutes model-API calls to the proxy — verified end-to-end + on 1.14.46 and 1.18.31 (log: `v1: fetch patch installed`), including the + OpenAI Responses endpoint. The patch is idempotent and passes + `/bili/`-wrapped URLs through untouched. - **Pure proxy:** point the provider baseURL at the proxy like any other client: diff --git a/README.zh-CN.md b/README.zh-CN.md index dcc4a7a879..91a60d1bea 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -78,10 +78,21 @@ AI 编程助手的通用上下文压缩代理 | 客户端 | 用这个 | |---|---| | **pi** | [`billion-context-pi`](https://github.com/ranxianglei/billion-context-pi)(进程内扩展) | +| **opencode 1.x** | [`billion-context`](https://github.com/ranxianglei/billion-context) —— `bili plugin install opencode`(V1 `.server()` 钩子,与 2.x 同一个包装器)或 `bili opencode`(启动器);[`opencode-acp`](https://github.com/ranxianglei/opencode-acp)(进程内扩展)仍可用 | +| **opencode 2.0+** | `bili opencode`(内置 V2 插件 —— 原生工具,无需另装包)或 `bili plugin install opencode`(自拉起原生插件,免启动器) | | **opencode** | [`billion-context`](https://github.com/ranxianglei/billion-context),`bili opencode`(新会话走 bili 代理;现有 [`opencode-acp`](https://github.com/ranxianglei/opencode-acp) 会话继续可用,见下文「OpenCode 1.x」) | | **omp** | [`billion-context`](https://github.com/ranxianglei/billion-context),`bili omp`(内置插件) | | **其余所有**(没有上下文 hook) | [`billion-context`](https://github.com/ranxianglei/billion-context) —— `bili `(启动器,优先)或 `/bili/` 前缀 | +**原生模式 vs 独立扩展。** 宿主原生插件(`bili plugin install pi` / `opencode` —— 代理在宿主进程内拉起)与独立进程内扩展(`billion-context-pi`、`opencode-acp`)**互斥**:两者同时生效意味着双重压缩。安装器负责切换:`bili plugin install pi` 会替换旧的 `npm:billion-context-pi` 条目;`bili plugin install opencode` 会从全局 opencode.json 里剔除旧的 `opencode-acp` 条目 —— 裸名、`npm:` 别名、带版本号(`opencode-acp@stable`)、路径形式都认,数组/对象两种形态都处理;原配置会快照到 `opencode.json.bili-bak`。**项目级**安装(`opencode plugin opencode-acp` 写的是 `/.opencode/opencode.json`,不是全局配置)不会被碰 —— 需手动移除,安装器输出里会提醒。作为手动安装的运行期安全网,原生入口在加载时同步设置 `BILLION_CONTEXT_NATIVE=`,让独立扩展在动作时自动退出 —— 它自己的加载期 `BILLION_CONTEXT_PROXY` 检查看不见原生模式异步拉起的代理,`/bili/` baseURL 检查也看不见 fetch 层改写。 + +**从 opencode-acp 迁移旧会话(v1 泳道路由)。** 在 OpenCode 1.x 上,`bili plugin install opencode` 让迁移前的旧会话继续可用:原生入口会吸收已安装的 `opencode-acp` 包(直接从 `node_modules` 导入 —— `.opencode/node_modules`、项目 `node_modules`、全局 npm root、opencode 配置级 modules,先到先得),并按会话路由。会话属于 legacy ⟺ opencode-acp 的持久化状态文件存在(`/opencode/storage/plugin/acp/.json`): + +- **旧会话** —— 压缩由被吸收的 opencode-acp 执行(它自己的 `` 引用号与块存储照常工作:`compress` / `decompress` / `search_context` / `acp_status` / `acp_context_recap` 全部在它里面执行)。其模型请求带 `x-bili-plugin-bypass: 1`,代理原样转发 —— 不注入 wire 工具、不注 nudge、不绑定会话。 +- **新会话** —— bili 接管:工具调用转发到代理的 plugin 端点(plugin 模式)。模型看到的工具槽位带 DCP schema(v1 进程级每个名字仅一份定义),但执行器按会话分泳道:新会话的 `compress` 发到代理,旧会话的发给 opencode-acp。`acp_context_recap` 没有代理端对应 —— 新会话调用会收到代理的 unknown-tool 消息。 + +`/acp` 与 `/dcp` 同样路由。新会话被 opencode-acp 注册表收编的可能被其 transform 门控(system / messages / text.complete 都过 legacy 谓词)阻断。退化路径:opencode-acp 包缺失或导入失败时 bili 单独运行,旧会话退化为只读存档(旧 `` 标签照常渲染、`decompress` 返回 `[Block … not found]`、新引用号从 m00001 重新开始)。 + ## 安装 ```bash @@ -112,7 +123,7 @@ bili pi # 拉起 pi,走代理 —— file-free(#53 bili codex # 拉起 codex bili claude # 拉起 claude bili omp # pi 同款,file-free(#535):环境变量 + 扩展 registerProvider + 压缩取消,真实 ~/.omp 不动 -bili opencode # HTTPS 走 MITM + 临时 opencode.json(HTTP 走 /bili/)+ 轻量 /acp 插件;OpenCode 1.x:旧 opencode-acp 会话继续可用(条目从副本中移除、包以库形式导入,#920) +bili opencode # HTTPS 走 MITM + 临时 opencode.json(HTTP 走 /bili/)+ 轻量 /acp 插件;OpenCode 2.0+:内置 V2 插件带原生 bili 工具,自动关掉原生 auto-compaction;OpenCode 1.x:旧 opencode-acp 会话继续可用(条目从副本中移除、包以库形式导入,#920)。详见下方「OpenCode 专属说明」 bili hermes # file-free(#535):hermes 代理环境变量(HTTPS_PROXY + HERMES_CA_BUNDLE)—— https 走 CONNECT MITM,http 走绝对形式转发;真实 ~/.hermes 不动 bili dsh # deepseek-harness:非回环上游走代理 env(https MITM、http absolute-form),回环上游保留 overlay DSH_HOME(~/.dsh-bili)改写(#535),内置 deepseek 路由走 DEEPSEEK_BASE_URL,经 --patch 注入原生 /acp 命令 bili codebuddy # Tencent CodeBuddy Code CLI:CODEBUDDY_BASE_URL /bili/ 重写(OpenAI chat completions wire),预算对齐走 CODEBUDDY_AUTO_COMPACT_WINDOW;真实 ~/.codebuddy 不动 @@ -169,6 +180,33 @@ curl -s http://localhost:8787/__bili/stats 同时也打到 stderr)。每个请求应该看到一行 `processTurn`,等对话变长后 会出现 `[acp-usage] round N input=X cached=Y (cache hit Z%)` + `compress` 事件。 +### OpenCode 专属说明 + +OpenCode 2.0 换了新插件 API(`@opencode/plugin`);独立扩展 `opencode-acp` 仅支持 V1,在 2.0 下不加载。bili 的两种模式在 2.0 都能用。2.x 插件 API 面在不同 build 间仍在变动(相邻的 npm `dev` 构建 `ctx` 形状不同),下面列的钩子/工具细节是针对具体版本的观察,不是稳定契约: + +- **启动器:** `bili opencode` 用法不变。在 2.x 宿主上,它把内置 V2 插件(`dist/agent/opencode.js`)以临时包装目录的形式注入临时配置(目录入口 `index.js` 再 re-export 插件文件 —— 2.x 拒绝配置 `plugin` 数组里的裸文件路径;1.x 宿主用裸文件路径)。宿主代次用 `--version` 探测(探测失败默认 1.x 形状)。V2 插件在宿主内原生注册 bili 工具 —— compress / decompress / search_context / acp_status(另加 absorb),JSON-Schema 入参 —— 并在每个 provider 请求上盖章代理头,压缩走插件模式,不做 wire 级工具注入;原生 auto-compaction 自动关闭(`compaction.auto: false`)。所有注册都是防御式的(可选链):任一 2.x build 上接缝缺失或未触发时,插件保持情性,会话透明回退纯代理模式(wire 级注入)而不是报错 —— 在两个相邻 `dev` build(2026-09-13 / 2026-09-14)上观察到过 API 面不同(#754 评审探针);反之在 `@opencode/cli` 2.0.3 上端到端验证过(原生 `acp_status` 经插件端点执行,零 wire 注入)。 +- **原生(免启动器):** npm 装好包后跑 `bili plugin install opencode` —— 它往真实 opencode 配置写一个自拉起插件(`/plugins/billion-context/index.js` → `dist/agent/opencode-native.js`)并设 `compaction.auto: false`,之后直接跑 `opencode` 即可。加载时插件自拉起自己的代理(健康的已有实例直接复用不重复起;父进程 pid 看门狗在 opencode 退出时收掉它),把模型流量路由到 `/bili/`,并暴露与启动器模式相同的原生 bili 工具 —— 无固定端口、无环境变量、免启动器。退出:`BILI_NATIVE_OPENCODE=0`。若没有任何代理能拉到健康状态,请求直连(不压缩)并给一次性告警,之后自动恢复。在 `bili opencode` 启动下该入口整体跳过(代理归启动器管)。 + + 同一个包装器也服务 **OpenCode 1.x**(V1 `.server()` 钩子;在 1.14.46 和 1.18.31 上验证):`config` 钩子在进程内直接改共享配置对象,把每个 provider 的 `options.baseURL` 改写为 `/bili/…` 并设 `compaction.auto: false`;`chat.headers` 每次请求盖章插件头;`tool` 用真实 zod 形状注册 bili 工具(zod 是运行时依赖 —— 解析不到时插件降级纯代理模式:只改写、wire 注入工具);`/acp` 命令渲染同一张状态面板。**没有显式 `baseURL`** 的 provider(SDK 默认值,如裸 `@ai-sdk/openai` → api.openai.com)由全局 `fetch` 补丁(pi-native 同机制)兑住 —— 模型 API 调用重路由到代理(日志:`v1: fetch patch installed`),在 1.14.46 / 1.18.31 上含 OpenAI Responses 端点端到端验证;补丁幂等,`/bili/` 包装过的 URL 原样直通。 +- **纯代理:** 与其它客户端一样,把 provider baseURL 指向代理: + + ```json + { + "provider": { + "myprovider": { + "npm": "@ai-sdk/openai-compatible", + "options": { + "baseURL": "http://localhost:8787/bili/http://upstream.example/v1", + "apiKey": "sk-any" + } + } + } + } + ``` + + 注意:2.0 AI-SDK provider 即使本地端点从不校验也要求 `apiKey` 字段 —— 随便填个非空值。 + +注意事项:2.x 系列以 npm 包 `@opencode/cli` 发布。命令支持随 build 而定:某个预发布版只暴露 list/get/update/remove,而 2.0.x 稳定版允许插件经 `ctx.command.transform((editor) => editor.add(...))` 新增命令 —— TUI 里接受斜杠菜单补全(Tab + Enter)即可调用;注意 `opencode run` 模式完全不派发斜杠命令(它们会透传给模型)。内置插件在两种形状上都刻意不注册命令。 ### 客户端用 `http.proxy`(CONNECT)接入但从不压缩 部分客户端(VS Code 系 IDE:CodeBuddy、Cursor、Windsurf……)只提供一个 HTTP **代理**设置(`http.proxy`、`codingcopilot.httpProxyURL` 等),没有可改写的模型 base-URL。这类客户端不走普通的 `/bili/…` 请求,而是把 `CONNECT <模型域名>:443` 发给代理。只有当模型域名在 bili 的 **MITM 白名单**里时这条路径才会被解密;否则 bili 只做盲隧道(不透明转发),永远看不到——也就无法压缩——模型请求(#897)。 diff --git a/devlog/2026-09-18_opencode-acp-bridge/REQ.md b/devlog/2026-09-18_opencode-acp-bridge/REQ.md deleted file mode 100644 index 3cda708da5..0000000000 --- a/devlog/2026-09-18_opencode-acp-bridge/REQ.md +++ /dev/null @@ -1,91 +0,0 @@ -# REQ — opencode v1: keep opencode-acp legacy sessions working after switching to bili - -## Date - -2026-09-18 - -## Background - -Issue #920. Users migrating from `opencode-acp` (V1 in-process extension, DCP -protocol) to `bili plugin install opencode` lose their old sessions' compression -machinery: acp's ref space / blocks live in its own store -(`~/.local/share/opencode/storage/plugin/acp/.json`), bili starts a -fresh ACP-kernel ref space per conversation, and acp itself globally -self-disables when it sees `/bili/` provider baseURLs. Today `decompress` on an -old ref returns `[Block … not found]` and numbering restarts. - -Requirement (user-directed design in the issue): on OpenCode 1.x, **old -sessions (acp state on disk) keep working through opencode-acp; new sessions go -through bili.** - -### Constraints verified against real code before implementing - -- v1 tool registry is process-global and static (`resolveTools` + permission - only; `chat.params` cannot touch tools) — no per-request tool filtering hook. - ⇒ acp's DCP tool definitions stay registered for every session; legacy - gating must happen inside the executors/hooks, not via tool visibility. -- acp and bili speak different protocols (DCP ranges vs ACP tags) — tool - definitions are not interchangeable. ⇒ the proxy keeps owning ACP wire tools - for new sessions; same-named client tools are dropped before injection so - upstream sees one definition per name. -- acp adopts every new session eagerly (`registry.getOrCreate` in its - messages.transform) and self-disables globally on `/bili/` detection. ⇒ bili - must never invoke acp's `config` hook, and every other hook is gated on - "an acp store file exists for this session" so non-legacy sessions are never - adopted. -- acp's package exports only `default server` — no factory hooks. ⇒ bili - imports the installed package as a library (`await import()` around a - temporarily unset `BILLION_CONTEXT_PROXY`) and wraps the returned hooks. -- The BILLION_CONTEXT_PROXY guard runs when `server(ctx)` is called, not at - module evaluation — import is side-effect-free; env save/delete/restore - around call + init suffices. -- OpenCode 1.x exposes a per-request `chat.headers` hook (verified in the live - v1.14.46 binary: trigger sits in the LLM request path, built-ins use it for - real wire headers) — the bridge stamps `x-bili-plugin-bypass: 1` per request - keyed on sessionID. - -## Design decisions - -- **bili-side absorption, zero opencode-acp changes.** New module - `src/agent/acp-bridge.ts` (bundled into dist/agent/opencode.js): - - `resolveAcpPackage(directory)`: env spec slot first (authoritative — the - copy the host would have loaded), then `/node_modules`, then - `/opencode/node_modules`, then newest valid - `/opencode/packages/opencode-acp*`. Valid = package.json - major === 1 + dist/index.js present. - - `acpStoreDir(directory)`: mirrors acp's own resolution - (project `.opencode/acp.jsonc|json` > `$OPENCODE_CONFIG_DIR` > global XDG - config; jsonc-parser; `~` expansion; relative → host directory). - - `createAcpBridge(ctx)`: import with BILLION_CONTEXT_PROXY removed, validate - hooks shape, wrap system.transform / messages.transform / text.complete / - command.execute.before / every tool executor behind the legacy gate - (store-file existence); event passed through ungated (it only reacts to - local compress tool parts, which new sessions cannot produce because the - proxy intercepts wire compress calls); `config` hook excluded entirely. -- **Proxy bypass mode**: `x-bili-plugin-bypass: 1` → raw passthrough ahead of - any pipeline processing (no JSON.parse, no window resolution, no session - binding, no injection, no compression, no state written). -- **Proxy-mode tool ownership flip**: the three inject helpers now drop - same-named client tools before injecting bili's definitions (previously - union: client definition won). Plugin mode never calls these helpers, so it - is unaffected. -- **Launcher**: `prepareOpencodeHttpRewrite` strips opencode-acp entries from - the temp config clone (string / tuple / object forms; fork-safe matcher) so - the host never loads acp armed; the first stripped spec rides along via - `BILI_OPENCODE_ACP_SPEC` so the bridge imports exactly that copy. -- **Graceful degradation**: package not found / import fails / version ≠ 1 / - hooks shape mismatch → exact pre-change behavior (documented in README). - -## Acceptance criteria - -- [x] Legacy session (store file present): acp hooks + DCP tools active, - requests raw-passthrough, `/dcp` command available. -- [x] New session: acp never invoked (no adoption), plain proxy mode with ACP - wire tools; upstream body contains exactly one definition per tool name. -- [x] Bypass header honored verbatim (byte-identical body forward), including - unparseable bodies. -- [x] Launcher strips all entry forms; fork-named entries untouched; user's - real config never modified. -- [x] Every degradation path returns null → current behavior. -- [x] Regression tests added and passing; full suite green; typecheck clean; - build green. diff --git a/devlog/2026-09-18_opencode-acp-bridge/WORKLOG.md b/devlog/2026-09-18_opencode-acp-bridge/WORKLOG.md deleted file mode 100644 index d490f055d0..0000000000 --- a/devlog/2026-09-18_opencode-acp-bridge/WORKLOG.md +++ /dev/null @@ -1,65 +0,0 @@ -# WORKLOG — opencode v1: keep opencode-acp legacy sessions working after switching to bili - -## Date - -2026-09-18 - -## What was done (this PR) - -- `src/agent/acp-bridge.ts` (NEW, ~350 lines): library-import bridge over the - installed opencode-acp package. `resolveAcpPackage` (env spec slot - authoritative → project node_modules → config-dir node_modules → newest - cache slot; major-1 + dist validation), `acpStoreDir` (mirrors acp's own - storagePath resolution incl. jsonc parsing and walk-up project config), - `createAcpBridge` (BILLION_CONTEXT_PROXY save/delete/restore around import + - init; hooks shape validation; legacy gate = store-file existence on every - wrapped hook and tool executor; event ungated by design; `config` hook - excluded). Any failure → null with a once-per-process warning. -- `src/agent/opencode.ts`: V1 `server()` now initializes the bridge when the - launcher env is active; merges wrapped hooks + acp's DCP tool map into the - returned Hooks, registers `/dcp` alongside `/acp`, chains command handlers - (acp first — its abort throw wins for legacy sessions), and adds a - `chat.headers` hook stamping `x-bili-plugin-bypass: 1` for legacy - sessionIDs. V2 `setup()` untouched. -- `src/server.ts`: new early-return honoring `x-bili-plugin-bypass: 1` - (constant in src/util.ts) — raw passthrough via the existing `forward()` - before JSON.parse / window / session binding. The three wire-tool inject - helpers (anthropic / openai / responses) now drop same-named client tools - before injecting bili's definitions, so upstream sees one definition per - name in proxy mode. -- `src/launcher.ts`: `prepareOpencodeHttpRewrite` strips opencode-acp entries - from the cloned config (all spec forms, fork-safe matcher) before appending - the bili plugin, and exports the first stripped spec to the spawn env as - `BILI_OPENCODE_ACP_SPEC`. -- Tests: `tests/acp-bridge.test.ts` (NEW, 5 tests — probe order/authority, - validation rejections, store-dir resolution matrix, env save/restore around - acp init, full gating matrix, all degradation paths), - `tests/fix-920-bypass-drop.test.ts` (NEW, 3 tests — bypass byte-fidelity - incl. unparseable body, proxy-mode same-name drop), `tests/launcher.test.ts` - (updated expectations + new all-spec-forms strip test). -- Docs: README.md / README.zh-CN.md (new "OpenCode 1.x" sections + table + - launcher lines), CONFIGURATION.md / CONFIGURATION.zh-CN.md (launcher bullet - + bypass header note), this devlog entry. - -## Verification - -- `npm run typecheck`: clean. -- `npm test`: 1677 pass, 0 fail, 2 skipped (pre-existing gated skips). -- `npm run build`: success; dist sanity checked (BILI_OPENCODE_ACP_SPEC in - dist/agent/opencode.js, x-bili-plugin-bypass in dist/index.js). -- E2E preflight (`E2E_CHECK=1 tests/e2e/e2e-codex.test.ts`): pass. Full A/B - run NOT possible in the agent environment: the suite's zero-cost upstream - (127.0.0.1:8199 sglang) is unreachable here — noted in the PR body. - -## Ceiling notes / deferred - -- The legacy gate is store-file **existence**, slightly looser than acp's own - shape validation (`loadSessionState` returns null on malformed files). A - corrupt legacy file therefore behaves exactly as it does under standalone - acp — no new failure mode introduced. -- acp's `startAutoUpdate` still runs inside the imported instance (same npm - self-update it does standalone); acceptable, unchanged surface. -- OpenCode 2.x hosts are out of scope by design (V2 plugin path already owns - compression natively; acp there was inert via self-disable anyway). -- If opencode-acp ever ships a factory export or per-session disable API, the - wrapper layer in acp-bridge.ts is the single place to simplify. diff --git a/package-lock.json b/package-lock.json index 9cacef94c9..5dcde1555b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,9 @@ "name": "billion-context", "version": "0.1.118", "license": "MIT", + "dependencies": { + "zod": "4.1.8" + }, "bin": { "bili": "dist/index.js", "bili-proxy": "dist/index.js" @@ -2165,6 +2168,15 @@ "engines": { "node": ">=18" } + }, + "node_modules/zod": { + "version": "4.1.8", + "resolved": "https://registry.npmjs.org/zod/-/zod-4.1.8.tgz", + "integrity": "sha512-5R1P+WwQqmmMIEACyzSvo4JXHY5WiAFHRMg+zBZKgKS+Q1viRa0C1hmUKtHltoIFKtIdki3pRxkmpP74jnNYHQ==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } } } } diff --git a/package.json b/package.json index 64f7a604ec..a869bfc1c6 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,9 @@ "bugs": { "url": "https://github.com/ranxianglei/billion-context/issues" }, + "dependencies": { + "zod": "4.1.8" + }, "devDependencies": { "@types/node": "^22.0.0", "@types/node-forge": "^1.3.14", @@ -67,7 +70,7 @@ "pi": { "image": "https://raw.githubusercontent.com/ranxianglei/billion-context/master/docs/logo.png", "extensions": [ - "./dist/agent/pi.js" + "./dist/agent/pi-native.js" ] }, "publishConfig": { diff --git a/src/agent/acp-bridge.ts b/src/agent/acp-bridge.ts deleted file mode 100644 index 8fd7e03bc4..0000000000 --- a/src/agent/acp-bridge.ts +++ /dev/null @@ -1,348 +0,0 @@ -// #920: keep opencode-acp legacy sessions working after switching to bili. -// -// The V1 thin plugin (opencode.ts) imports the INSTALLED opencode-acp package -// as a library and wraps its hooks so that ONLY sessions with acp state on -// disk ("legacy") are handled by acp. New sessions are never adopted: every -// stateful hook gates on "an acp store file exists for this session". The -// host never loads acp from config (the launcher strips the entry from its -// temp-config clone), so acp's global self-disable on /bili/ baseURLs (its -// config hook) is never armed — we simply never invoke that hook. -// -// Graceful degradation: package not found / import failure / version drift / -// unexpected hook shape → createAcpBridge returns null and the plugin behaves -// exactly as before this change (legacy sessions degrade as they do today). - -import * as fs from "node:fs"; -import { homedir } from "node:os"; -import path from "node:path"; -import { pathToFileURL } from "node:url"; -import { parse as parseJsonc } from "jsonc-parser"; - -// Set by the launcher (src/launcher.ts) to the opencode-acp spec it stripped -// from the temp-config clone, so the bridge imports the SAME package copy the -// host would have loaded (legacy state formats must match the writer). -const ACP_SPEC_ENV = "BILI_OPENCODE_ACP_SPEC"; - -export interface AcpToolDef { - description?: string; - args?: unknown; - execute?: (args: unknown, toolCtx: { sessionID?: string; [key: string]: unknown }) => Promise; - [key: string]: unknown; -} - -// Loose host-wire shapes — the imported handlers are opaque; they receive -// whatever the host sends, and the wrappers forward both args untouched. -type AcpSystemTransform = (input: { sessionID?: string; [key: string]: unknown }, output: { system: unknown[] }) => Promise | void; -type AcpMessagesTransform = (input: Record, output: { messages: unknown[] }) => Promise | void; -type AcpTextComplete = (input: { sessionID?: string; [key: string]: unknown }, output: { text: string }) => Promise | void; -type AcpCommandBefore = (input: { command?: string; sessionID?: string; arguments?: string; [key: string]: unknown }, output: Record) => Promise | void; -type AcpEventHandler = (input: { event?: unknown; [key: string]: unknown }) => Promise | void; - -interface AcpHooks { - "experimental.chat.system.transform"?: AcpSystemTransform; - "experimental.chat.messages.transform"?: AcpMessagesTransform; - "experimental.text.complete"?: AcpTextComplete; - "command.execute.before"?: AcpCommandBefore; - event?: AcpEventHandler; - config?: (input: unknown) => Promise | void; - tool?: Record; -} - -export interface AcpBridge { - version: string; - storeDir: string; - isLegacySession(sessionID: string): boolean; - wrapped: { - "experimental.chat.system.transform": (input: { sessionID?: string; [key: string]: unknown }, output: { system: unknown[] }) => Promise; - "experimental.chat.messages.transform": (input: Record, output: { messages: unknown[] }) => Promise; - "experimental.text.complete": (input: { sessionID?: string; [key: string]: unknown }, output: { text: string }) => Promise; - "command.execute.before": (input: { command?: string; sessionID?: string; arguments?: string; [key: string]: unknown }, output: Record) => Promise; - event: (input: { event?: unknown; [key: string]: unknown }) => Promise; - }; - tools: Record; -} - -function xdgDataHome(): string { - return process.env.XDG_DATA_HOME || path.join(homedir(), ".local", "share"); -} - -function xdgConfigHome(): string { - return process.env.XDG_CONFIG_HOME || path.join(homedir(), ".config"); -} - -function xdgCacheHome(): string { - return process.env.XDG_CACHE_HOME || path.join(homedir(), ".cache"); -} - -/** Mirror of acp's findOpencodeDir: nearest ancestor containing .opencode. */ -function findProjectAcpDir(directory: string): string | undefined { - let cur = directory; - for (;;) { - const cand = path.join(cur, ".opencode"); - try { - if (fs.statSync(cand).isDirectory()) return cand; - } catch { /* keep walking up */ } - const parent = path.dirname(cur); - if (parent === cur) return undefined; - cur = parent; - } -} - -/** Mirror of acp's storage-path resolution (lib/state/persistence.ts): - * empty → default dir; "~" → home; "~/x" expanded; absolute as-is; - * relative → against the session directory. */ -function resolveStorageDir(configured: string | undefined, directory: string): string { - if (!configured) return path.join(xdgDataHome(), "opencode", "storage", "plugin", "acp"); - if (configured === "~") return homedir(); - if (configured.startsWith("~/")) return path.join(homedir(), configured.slice(2)); - if (path.isAbsolute(configured)) return configured; - return path.join(directory, configured); -} - -/** Read `storagePath` from acp's config files, same precedence as acp - * (project .opencode > $OPENCODE_CONFIG_DIR > global XDG config). */ -function readConfiguredStoragePath(directory: string): string | undefined { - const dirs: string[] = []; - const projectDir = findProjectAcpDir(directory); - if (projectDir) dirs.push(projectDir); - if (process.env.OPENCODE_CONFIG_DIR) dirs.push(process.env.OPENCODE_CONFIG_DIR); - dirs.push(path.join(xdgConfigHome(), "opencode")); - for (const dir of dirs) { - for (const name of ["acp.jsonc", "acp.json"]) { - let text: string; - try { - text = fs.readFileSync(path.join(dir, name), "utf8"); - } catch { - continue; - } - const parsed = parseJsonc(text) as { storagePath?: unknown } | null; - if (parsed && typeof parsed === "object" && typeof parsed.storagePath === "string" && parsed.storagePath.length > 0) { - return parsed.storagePath; - } - } - } - return undefined; -} - -export function acpStoreDir(directory: string): string { - return resolveStorageDir(readConfiguredStoragePath(directory), directory); -} - -function readPkgVersion(pkgPath: string): number[] | undefined { - try { - const raw = JSON.parse(fs.readFileSync(path.join(pkgPath, "package.json"), "utf8")) as { version?: string }; - const m = /^(\d+)\.(\d+)(?:\.(\d+))?/.exec(typeof raw.version === "string" ? raw.version : ""); - if (!m) return undefined; - return [Number(m[1]), Number(m[2]), Number(m[3] ?? 0)]; - } catch { - return undefined; - } -} - -function compareVersions(a: number[], b: number[]): number { - for (let i = 0; i < 3; i++) { - if ((a[i] ?? 0) !== (b[i] ?? 0)) return (a[i] ?? 0) - (b[i] ?? 0); - } - return 0; -} - -function isValidAcpPkg(pkgPath: string): { version: number[]; mtime: number } | undefined { - const version = readPkgVersion(pkgPath); - // v1 bridge only — the V1 plugin shape this bridge wraps does not exist - // for other majors (#920 scope). - if (!version || version[0] !== 1) return undefined; - try { - return { version, mtime: fs.statSync(path.join(pkgPath, "dist", "index.js")).mtimeMs }; - } catch { - return undefined; - } -} - -/** Locate the installed opencode-acp package. The launcher-stripped spec - * (env hint) is authoritative when its cache slot is valid — that IS the - * copy the host would have loaded (legacy state must match the writer). - * Otherwise: /node_modules, /opencode/node_modules, - * then the newest valid /opencode/packages/opencode-acp* slot. */ -export function resolveAcpPackage(directory: string): string | undefined { - const spec = process.env[ACP_SPEC_ENV]; - if (typeof spec === "string" && spec.length > 0) { - const slot = path.join(xdgCacheHome(), "opencode", "packages", spec, "node_modules", "opencode-acp"); - if (isValidAcpPkg(slot)) return slot; - // Spec'd copy missing or broken — fall through to generic probes. - } - const candidates: string[] = [ - path.join(directory, "node_modules", "opencode-acp"), - path.join(xdgConfigHome(), "opencode", "node_modules", "opencode-acp"), - ]; - const pkgRoot = path.join(xdgCacheHome(), "opencode", "packages"); - let entries: string[] = []; - try { - entries = fs.readdirSync(pkgRoot); - } catch { /* no packages cache */ } - for (const entry of [...entries].sort()) { - if (/^opencode-acp(@|\/|$)/.test(entry)) { - candidates.push(path.join(pkgRoot, entry, "node_modules", "opencode-acp")); - } - } - let best: { pkgPath: string; version: number[]; mtime: number } | undefined; - for (const candidate of candidates) { - const valid = isValidAcpPkg(candidate); - if (!valid) continue; - if (!best) { - best = { pkgPath: candidate, ...valid }; - continue; - } - // Higher version wins; ties keep the earlier candidate (probe-order priority). - if (compareVersions(valid.version, best.version) > 0) { - best = { pkgPath: candidate, ...valid }; - } - } - return best?.pkgPath; -} - -let degradeLogged = false; - -function logDegrade(why: string): void { - if (degradeLogged) return; - degradeLogged = true; - console.warn(`[bili-opencode] acp bridge unavailable (${why}) — legacy sessions fall back to ordinary bili mode (#920)`); -} - -interface AcpMessageLike { - info?: { role?: string; id?: string; sessionID?: string }; - parts?: Array<{ ignored?: boolean } | null> | null; -} - -/** Mirror of acp's getLastUserMessage (lib/messages/query.ts) — enough of it - * to extract the session id the eager adoption would key on: last real - * (non-synthetic, non-fully-ignored) user message, walking backwards. */ -function lastUserSessionId(messages: unknown): string | undefined { - if (!Array.isArray(messages)) return undefined; - for (let i = messages.length - 1; i >= 0; i--) { - const msg = messages[i] as AcpMessageLike | null | undefined; - if (!msg || typeof msg !== "object") continue; - const info = msg.info; - if (!info || typeof info !== "object") continue; - if (info.role !== "user") continue; - const id = typeof info.id === "string" ? info.id : ""; - if (id.startsWith("msg_dcp_summary_") || id.startsWith("msg_dcp_text_") || id.startsWith("msg_acp_recap_")) continue; - const parts = Array.isArray(msg.parts) ? msg.parts : []; - if (parts.length === 0) continue; - if (parts.every((p) => p !== null && typeof p === "object" && p.ignored)) continue; - return typeof info.sessionID === "string" ? info.sessionID : undefined; - } - return undefined; -} - -/** Import the installed opencode-acp as a library and wrap its hooks behind - * the legacy-session gate. Returns null (→ normal bili mode) on ANY failure. */ -export async function createAcpBridge(ctx: { directory: string }): Promise { - const directory = ctx.directory; - const pkgPath = resolveAcpPackage(directory); - if (!pkgPath) { - logDegrade("opencode-acp package not found"); - return null; - } - // acp's BILLION_CONTEXT_PROXY guard runs when server(ctx) is CALLED, not - // at module evaluation — save/delete around the call, restore in finally. - const savedProxy = process.env.BILLION_CONTEXT_PROXY; - delete process.env.BILLION_CONTEXT_PROXY; - let raw: Record | null; - let version = ""; - try { - const mod = await import(pathToFileURL(path.join(pkgPath, "dist", "index.js")).href); - const serverFn: unknown = (mod as { default?: unknown }).default; - if (typeof serverFn !== "function") { - logDegrade("unexpected export shape (no default function)"); - return null; - } - const hooksOut = await (serverFn as (hostCtx: unknown) => Promise>)(ctx); - if (!hooksOut || typeof hooksOut !== "object") { - logDegrade("plugin returned no hooks"); - return null; - } - raw = hooksOut; - try { - version = String((JSON.parse(fs.readFileSync(path.join(pkgPath, "package.json"), "utf8")) as { version?: string }).version ?? ""); - } catch { /* version is cosmetic */ } - } catch (err) { - logDegrade(`import/init failed: ${err instanceof Error ? err.message : String(err)}`); - return null; - } finally { - if (savedProxy === undefined) delete process.env.BILLION_CONTEXT_PROXY; - else process.env.BILLION_CONTEXT_PROXY = savedProxy; - } - - const hooks = raw as Partial; - if (typeof hooks["experimental.chat.messages.transform"] !== "function") { - logDegrade("version drift: messages.transform hook missing"); - return null; - } - const toolsRaw = hooks.tool; - if (!toolsRaw || typeof toolsRaw !== "object" || !toolsRaw.compress || typeof toolsRaw.compress !== "object") { - logDegrade("version drift: compress tool missing"); - return null; - } - - const storeDir = acpStoreDir(directory); - const isLegacySession = (sessionID: string): boolean => { - try { - return fs.existsSync(path.join(storeDir, `${sessionID}.json`)); - } catch { - return false; - } - }; - - const wrapped: AcpBridge["wrapped"] = { - "experimental.chat.system.transform": async (input, output) => { - if (typeof input?.sessionID !== "string" || !isLegacySession(input.sessionID)) return; - await hooks["experimental.chat.system.transform"]!(input, output); - }, - "experimental.chat.messages.transform": async (input, output) => { - const sid = lastUserSessionId(output?.messages); - if (!sid || !isLegacySession(sid)) return; - await hooks["experimental.chat.messages.transform"]!(input, output); - }, - "experimental.text.complete": async (input, output) => { - if (typeof input?.sessionID !== "string" || !isLegacySession(input.sessionID)) return; - await hooks["experimental.text.complete"]!(input, output); - }, - "command.execute.before": async (input, output) => { - if (typeof input?.sessionID !== "string" || !isLegacySession(input.sessionID)) return; - await hooks["command.execute.before"]!(input, output); - }, - event: async (input) => { - // Ungated by design: acp's event handler only reacts to LOCAL - // `compress` tool parts (timing bookkeeping keyed by - // messageID+callID). Under bili, new sessions never produce local - // compress parts — the proxy executes wire compress calls - // server-side and strips them from the stream — so this cannot - // adopt or touch a non-legacy session. - await hooks.event?.(input); - }, - }; - - const tools: Record = {}; - for (const [name, def] of Object.entries(toolsRaw)) { - if (!def || typeof def !== "object") continue; - if (def.execute) { - const origExecute = def.execute; - tools[name] = { - ...def, - execute: async (args, toolCtx) => { - // Static process-global registry: definitions serve legacy - // sessions; a non-legacy session calling one gets a - // degraded answer instead of accidental adoption. - if (typeof toolCtx?.sessionID === "string" && !isLegacySession(toolCtx.sessionID)) { - return `[bili] "${name}" is not available in this session — it belongs to an older opencode-acp session. This session compresses through the billion-context proxy.`; - } - return origExecute(args, toolCtx); - }, - }; - } else { - tools[name] = def; - } - } - - return { version, storeDir, isLegacySession, wrapped, tools }; -} diff --git a/src/agent/native-bootstrap.ts b/src/agent/native-bootstrap.ts new file mode 100644 index 0000000000..6d0a456e5b --- /dev/null +++ b/src/agent/native-bootstrap.ts @@ -0,0 +1,90 @@ +// Shared native-mode bootstrap plumbing (#519 pi pattern, reused by every +// host-native entry — opencode 2.x per #820). Kept free of host-specific side +// effects: importing this module must NEVER spawn a proxy or patch anything — +// the host entries (pi-native.ts / opencode-native.ts) own their module-level +// bootstrap blocks. + +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +/** dist/agent/.js → dist/index.js (the package bin). Resolved at + * runtime so the artifact works from any install root. */ +export function nativeProxyScriptPath(fromUrl: string = import.meta.url): string { + return path.resolve(path.dirname(fileURLToPath(fromUrl)), "..", "index.js"); +} + +/** Common gate for host-native bootstraps: off when the global plugin kill + * switch or the per-host opt-out key is set, or when a `bili` launch already + * owns a proxy (MITM transparent sets BILLION_CONTEXT_PROXY; /bili/ rewrite + * mode sets BILI_PROVIDER_REWRITES). */ +export function nativeBootstrapGate(env: NodeJS.ProcessEnv, optOutKey: string): boolean { + if (env.BILLION_CONTEXT_PLUGIN === "0") return false; + if (env[optOutKey] === "0") return false; + if (env.BILLION_CONTEXT_PROXY !== undefined && env.BILLION_CONTEXT_PROXY.trim().length > 0) return false; + if (env.BILI_PROVIDER_REWRITES !== undefined) return false; + return true; +} + +/** Normalize a preset BILLION_CONTEXT_PROXY value (http/https, trailing + * slashes stripped) for attach-style routing. Returns undefined when unset, + * blank, or not a valid http(s) origin. */ +export function proxyEnvOrigin(env: NodeJS.ProcessEnv): string | undefined { + const raw = env.BILLION_CONTEXT_PROXY; + if (raw === undefined) return undefined; + const url = raw.trim(); + if (url.length === 0 || !/^https?:\/\//i.test(url)) return undefined; + try { + new URL(url); + } catch { + return undefined; + } + return url.replace(/\/+$/, ""); +} + +/** External-proxy attach signal (#809): when BILLION_CONTEXT_ATTACH holds a + * valid http(s) origin, a host-native entry routes model traffic THROUGH that + * pre-existing proxy instead of spawning its own — no ownership, no respawn, + * fail-closed on its death. Returns the normalized origin (trailing slash + * stripped) or undefined when unset/blank/malformed/non-http(s). */ +export function nativeAttachOrigin(env: NodeJS.ProcessEnv): string | undefined { + const raw = env.BILLION_CONTEXT_ATTACH; + if (raw === undefined) return undefined; + const url = raw.trim(); + if (url.length === 0 || !/^https?:\/\//i.test(url)) return undefined; + try { + new URL(url); + } catch { + return undefined; + } + return url.replace(/\/+$/, ""); +} + +/** Coexistence marker (#820): tells standalone in-process bili extensions + * (billion-context-pi / opencode-acp) that a host-native entry owns THIS + * process so they back off instead of double-compressing. Set synchronously + * at module evaluation — before any await — because those extensions check + * BILLION_CONTEXT_PROXY at load time (our bootstrap writes it only after the + * proxy is up) and their /bili/ baseUrl check never sees our fetch-layer + * rewrite. First writer wins: one process hosts one native entry. Callers + * gate on their own shouldBootstrap*() so opt-outs and launches where a bili + * launcher already manages the proxy leave the marker unset. */ +export function markNativeHost(env: NodeJS.ProcessEnv, host: string): void { + if (env.BILLION_CONTEXT_NATIVE === undefined || env.BILLION_CONTEXT_NATIVE.length === 0) { + env.BILLION_CONTEXT_NATIVE = host; + } +} + +/** Concurrent callers share one in-flight bootstrap — a burst of failures + * (the proxy died mid-session) must not spawn one proxy per failing request: + * ensureProxyRunning has no in-flight dedup of its own. */ +export function singleFlight(fn: () => Promise): () => Promise { + let inFlight: Promise | undefined; + return (): Promise => { + if (inFlight === undefined) { + inFlight = fn().finally(() => { + inFlight = undefined; + }); + } + return inFlight; + }; +} diff --git a/src/agent/native-intercept.ts b/src/agent/native-intercept.ts new file mode 100644 index 0000000000..c39efb3edf --- /dev/null +++ b/src/agent/native-intercept.ts @@ -0,0 +1,168 @@ +// Native-mode fetch interception (#519): a globalThis.fetch patch that +// silently routes model-API requests through a bili proxy that the extension +// itself spawned (see pi-native.ts). Verified end-to-end on pi 0.83.6: pi's +// provider stack (pi-stable-ai → Anthropic/OpenAI SDKs) resolves its fetch +// from the global at FIRST-request client construction, so a patch installed +// at extension load always wins. The patch is surgical — it rewrites ONLY +// model-API shaped URLs and leaves every other request untouched. + +export interface NativeInterceptState { + /** Proxy origin ("http://127.0.0.1:PORT") once the bootstrap resolved. + * Written by the owner (pi-native.ts); read synchronously on each call. */ + origin: string | undefined; + /** Resolves to the proxy origin once healthy, or undefined on failure. */ + ready: Promise; + /** Owner hook: re-run the bootstrap (proxy died → respawn). */ + respawn?: () => Promise; + /** Owner hook: fired once when a respawn attempt fails and the session + * degrades to direct sends for good — clear proxy-owned state (e.g. the + * BILLION_CONTEXT_PROXY env) so event-time ownership checks disarm with + * the traffic. */ + onGiveUp?: () => void; + /** Attach mode (#809): route through a user-supplied external proxy at + * state.origin instead of a spawned one — no respawn, fail-closed on + * death. Set by the host entry when BILLION_CONTEXT_ATTACH is present. */ + attach?: boolean; + /** How long a pre-ready model request waits for the bootstrap before + * falling back to a direct (uncompressed) send. */ + readyTimeoutMs?: number; + /** Test/observability hook: every dispatched decision. */ + onDispatch?: (url: string, action: "rewrite" | "direct" | "self" | "retry") => void; +} + +const INTERCEPT_FLAG = "__biliNativeFetchIntercept"; + +// Model-API endpoint suffixes across the wires bili proxies: Anthropic +// `/v1/messages`, OpenAI chat `/v1/chat/completions` (and legacy +// `/v1/completions`), Responses `/v1/responses`, Mistral +// `/v1/chat/completions`|`/v1/conversations`. Version segment is optional +// and unpinned (zhipuai uses `/v4/chat/completions`, bailian mounts +// `/apps/anthropic/v1/messages`), so match on the trailing shape only. +const MODEL_API_SUFFIX = /(?:^|\/)(?:v\d+\/)?(?:messages|chat\/completions|completions|responses|conversations)\/?$/; + +/** True when the URL points at a model-API endpoint worth proxying. Never + * true for bili's own proxy paths (`/bili/…`, `/__bili/…`) or non-HTTP(S). */ +export function isModelApiUrl(url: string): boolean { + if (!/^https?:\/\//i.test(url)) return false; + if (url.includes("/__bili/") || url.includes("/__acp/")) return false; + try { + const u = new URL(url); + const segments = u.pathname.split("/").filter((s) => s.length > 0); + if (segments[0] === "bili") return false; + const pathname = u.pathname.replace(/\/+$/, ""); + return MODEL_API_SUFFIX.test(pathname); + } catch { + return false; + } +} + +function fetchUrlOf(input: string | URL | Request): string | undefined { + try { + if (typeof input === "string") return input; + if (input instanceof URL) return input.href; + if (input !== null && typeof input === "object" && typeof (input as Request).url === "string") { + return (input as Request).url; + } + } catch { + // fallthrough + } + return undefined; +} + +async function withTimeout(p: Promise, ms: number): Promise { + let timer: ReturnType | undefined; + const timeout = new Promise((resolve) => { + timer = setTimeout(() => resolve(undefined), ms); + }); + try { + return await Promise.race([p, timeout]); + } catch { + return undefined; + } finally { + if (timer !== undefined) clearTimeout(timer); + } +} + +export async function readyOrigin(state: NativeInterceptState): Promise { + if (state.origin !== undefined) return state.origin; + return withTimeout(state.ready, state.readyTimeoutMs ?? 15000); +} + +/** Install the global fetch patch. Idempotent: a second call is a no-op + * (returns false) so double-loading the entry cannot double-wrap. */ +export function installNativeFetchIntercept(state: NativeInterceptState): boolean { + const g = globalThis as Record; + if (g[INTERCEPT_FLAG] === true) return false; + const orig = globalThis.fetch; + let warned = false; + + const patched = async (input: string | URL | Request, init?: RequestInit): Promise => { + const url = fetchUrlOf(input); + if (url === undefined || !isModelApiUrl(url)) return orig(input, init); + + // Rebuild a Request-object input against the rewritten target. A + // caller-side defect here (already-consumed or locked body) must not + // reach the proxy-death branch below — respawning would orphan a + // fresh proxy for a request that can never be sent. + const makeTarget = (target: string): string | URL | Request => + typeof input === "string" || input instanceof URL ? target : new Request(target, input); + + const origin = await readyOrigin(state); + if (origin === undefined) { + // Bootstrap failed or timed out — NEVER break the agent: send + // direct (uncompressed) and say so once. + if (!warned) { + warned = true; + console.error(`bili-native: proxy not ready — model request goes direct (uncompressed): ${url}`); + } + state.onDispatch?.(url, "direct"); + return orig(input, init); + } + if (url.startsWith(`${origin}/`)) { + state.onDispatch?.(url, "self"); + return orig(input, init); + } + const first = makeTarget(`${origin}/bili/${url}`); + state.onDispatch?.(`${origin}/bili/${url}`, "rewrite"); + try { + return await orig(first, init); + } catch (err) { + // The spawned proxy can die mid-session (its parent watchdog + // fires when the FIRST owning pi exits while later sessions + // still ride it). A network-level failure (undici throws + // TypeError) triggers one respawn + one retry. + if (err instanceof TypeError && state.respawn !== undefined) { + state.origin = undefined; + state.ready = state.respawn(); + const again = await readyOrigin(state); + if (again !== undefined) { + const retried = makeTarget(`${again}/bili/${url}`); + state.onDispatch?.(`${again}/bili/${url}`, "retry"); + return await orig(retried, init); + } + // Respawn failed — this session runs direct for its lifetime. + // Degrade exactly like a bootstrap failure: actually send the + // request direct, then let the owner clear proxy-owned state. + state.onGiveUp?.(); + if (!warned) { + warned = true; + console.error(`bili-native: proxy respawn failed — model requests go direct (uncompressed): ${url}`); + } + state.onDispatch?.(url, "direct"); + return orig(input, init); + } + throw err; + } + }; + + globalThis.fetch = patched as typeof globalThis.fetch; + g[INTERCEPT_FLAG] = true; + return true; +} + +/** Test-only: drop the patch guard so a suite can install again. The + * caller owns restoring globalThis.fetch. */ +export function _resetForTest(): void { + const g = globalThis as Record; + delete g[INTERCEPT_FLAG]; +} diff --git a/src/agent/opencode-acp-command.ts b/src/agent/opencode-acp-command.ts new file mode 100644 index 0000000000..8e3613a411 --- /dev/null +++ b/src/agent/opencode-acp-command.ts @@ -0,0 +1,114 @@ +// Shared /acp command hooks for the OpenCode 1.x plugin surface (V1 `.server()` +// hooks). Extracted from src/agent/opencode.ts so both deployments register +// the identical command: the launcher-gated plugin (BILLION_CONTEXT_PROXY set +// by `bili opencode`) and the native V1 entry (src/agent/opencode-native.ts, +// self-spawned/attached proxy whose origin resolves asynchronously). +// +// The proxy base is read through a getter because the native entry learns its +// origin only after bootstrap; the launcher variant captures the env value at +// module load and serves it verbatim. + +import { fetchProxyVersion } from "./shared.js"; + +export interface OpencodeCommandConfig { + template: string; + description?: string; +} + +export interface OpencodeConfig { + command?: Record; + [key: string]: unknown; +} + +export interface OpencodePromptPart { + type: string; + text: string; + ignored?: boolean; +} + +export interface OpencodeClient { + session?: { + prompt?: (args: { + path: { id: string }; + body: { noReply: boolean; parts: OpencodePromptPart[] }; + }) => Promise; + }; +} + +export interface OpencodeCommandInput { + command: string; + sessionID: string; + arguments?: string; +} + +export interface OpencodeAcpHooks { + config?: (input: OpencodeConfig) => Promise; + "command.execute.before"?: (input: OpencodeCommandInput) => Promise; +} + +export async function showAcpText(ctx: { client?: OpencodeClient }, sid: string, text: string): Promise { + // Direct method call — `const p = ctx.client.session.prompt; p(...)` loses `this` (this._client) and throws. + const session = ctx.client?.session; + if (!session || typeof session.prompt !== "function") { + console.error("[bili-opencode] /acp render failed: session.prompt unavailable"); + return; + } + try { + await session.prompt({ + path: { id: sid }, + body: { noReply: true, parts: [{ type: "text", text, ignored: true }] }, + }); + } catch (err) { + console.error(`[bili-opencode] /acp render failed: ${err instanceof Error ? err.message : String(err)}`); + } +} + +/** The /acp command hooks (registration + render). `getProxyBase` returns the + * proxy origin or undefined when unavailable — an undefined base renders a + * diagnostic instead of failing silently. The config hook ONLY registers the + * command; callers layer provider rewriting on top. */ +export function createAcpCommandHooks(getProxyBase: () => string | undefined, ctx: { client?: OpencodeClient }): OpencodeAcpHooks { + return { + config: async (opencodeConfig) => { + opencodeConfig.command ??= {}; + opencodeConfig.command["acp"] = { + template: "", + description: "Show ACP status (billion-context proxy)", + }; + }, + "command.execute.before": async (input) => { + if (input.command !== "acp") return; + const sid = input.sessionID; + const proxyBase = getProxyBase(); + let text: string; + if (proxyBase === undefined || proxyBase.length === 0) { + text = "bili: proxy not running (native bootstrap failed) — model traffic goes direct"; + } else { + try { + const res = await fetch(`${proxyBase}/__bili/plugin/status?conversationId=${encodeURIComponent(sid)}&fallback=latest`); + const status = (await res.json()) as { ok?: boolean; panel?: string; error?: string }; + if (typeof status.panel === "string" && status.panel.length > 0) { + text = status.panel; + } else if (status.ok === false) { + // zero sessions on the proxy (fresh launch) — friendly idle notice + let version: string | undefined; + try { + version = await fetchProxyVersion(proxyBase); + } catch { + version = undefined; + } + text = version !== undefined + ? `billion-context@${version} — proxy connected, no ACP session yet. Send a model request, then run /acp again.` + : "bili: no ACP session yet (send a model request first, then run /acp)"; + } else { + text = "bili: proxy returned no status panel"; + } + } catch (err) { + text = `bili: /acp failed (${err instanceof Error ? err.message : String(err)})`; + } + } + await showAcpText(ctx, sid, text); + throw new Error("__BILI_ACP_HANDLED__"); + }, + }; +} diff --git a/src/agent/opencode-legacy.ts b/src/agent/opencode-legacy.ts new file mode 100644 index 0000000000..08f5ce26a9 --- /dev/null +++ b/src/agent/opencode-legacy.ts @@ -0,0 +1,191 @@ +// opencode-acp absorption for LEGACY V1 sessions (#920). +// +// Users migrating from the standalone `opencode-acp` extension (DCP protocol, +// its own ref space + block store) to `bili plugin install opencode` still +// need their OLD sessions to work: those sessions' history is annotated with +// `` tags and their refs live in acp's store — bili's kernel +// cannot serve them. Routing requirement: legacy sessions keep running +// through opencode-acp, new sessions go through bili. +// +// Probed facts (opencode-acp 1.18.x, ~/projects/opencode-acp): +// - package exports ONLY `default server` (index.ts); `server(ctx)` returns +// `{}` immediately when BILLION_CONTEXT_PROXY is set — the env is checked +// ONLY there (index.ts:38); executors never re-check it, so calling +// `server(ctx)` with the env temporarily unset yields the armed hooks. +// - its `config` hook self-disables the whole plugin when any provider +// baseURL contains `/bili/` (findBiliProxyProviders) AND denies the tools +// via permission.deny — so we call it with `provider` hidden and merge +// back only the keys it legitimately owns (permission / command / +// experimental.primary_tools / agent snapshot side effects). +// - adoption of NEW sessions happens in messages.transform via +// registry.getOrCreate(lastUserMessage.info.sessionID) — gating that hook +// (and system.transform / text.complete) on legacy state keeps new +// sessions out of acp's registry entirely. +// - legacy session marker: acp's persisted state file at +// `/opencode/storage/plugin/acp/.json` +// (getDefaultStorageDir, lib/state/persistence.ts:67). config storagePath +// overrides are NOT probed (documented limitation). +// - tool args are kernel-compatible: compress `content: +// [{topic,startId,endId,summary}]` is the kernel's accepted object form +// (verified: parseCompressArgs maps startId/endId → ranges[].startRef/ +// endRef), decompress/search_context/acp_status args match the kernel +// shapes — so NEW-session calls can be forwarded to the proxy verbatim +// with zero translation. `acp_context_recap` has no proxy counterpart +// (rejected by /__bili/plugin/tool) — legacy-only, documented. +// - the v1 host loads tool defs from the hooks table statically; ONE def +// per name process-wide. The slots therefore carry acp's DCP schemas for +// everyone, and the WRAPPED executors route per session: legacy → acp +// executor, new → forwardTool to the proxy (plugin-mode carrier). +// +// Degradation: package not found / import fails / unexpected shape → the +// caller falls back to bili's own V1 tools (current behavior); legacy +// sessions then degrade as documented (read-only archives). + +import { accessSync, existsSync, readdirSync } from "node:fs"; +import { homedir } from "node:os"; +import path from "node:path"; +import { pathToFileURL } from "node:url"; + +export interface LegacyAcpToolDef { + description?: unknown; + args?: unknown; + execute?: (args: Record, ctx: { sessionID: string; messageID?: string }) => Promise; +} + +export interface LegacyAcpModule { + /** acp's full hooks table (config/tool/hooks keys as returned by server()). */ + hooks: Record; + /** hooks.tool — the DCP tool slots (compress/decompress/search_context/acp_status/acp_context_recap). */ + tools: Record; + /** hooks.config — must be called with `provider` hidden (self-disable neutered). */ + configHook: ((cfg: Record) => Promise) | undefined; + /** hooks["command.execute.before"] — /acp + /dcp handler for legacy sessions. */ + commandHook: ((input: { command: string; sessionID: string; arguments?: string }, output?: { parts?: unknown[] }) => Promise) | undefined; + /** resolved dist/index.js path (for logs). */ + source: string; +} + +/** Candidate `opencode-acp/dist/index.js` paths, most specific first: + * the launcher's `BILI_OPENCODE_ACP_SPEC` hint when its cache slot exists + * (#920 launcher path — the launcher knows exactly which entry it stripped + * from the temp config), then project plugin scope, plain project + * node_modules, opencode's v1 package cache (`opencode plugin opencode-acp` + * — global AND project installs land in + * `~/.cache/opencode/packages/opencode-acp@/node_modules`; every + * version present is offered, newest first), global user npm root, and + * opencode's config-scope node_modules. */ +export function legacyAcpCandidates(cwd: string | undefined): string[] { + const dirs: string[] = []; + if (cwd !== undefined && cwd.length > 0) { + dirs.push(path.join(cwd, ".opencode", "node_modules")); + dirs.push(path.join(cwd, "node_modules")); + } + const out: string[] = []; + const pushDir = (d: string): void => { + out.push(path.join(d, "opencode-acp", "dist", "index.js")); + }; + for (const d of dirs) pushDir(d); + const cacheHome = process.env.XDG_CACHE_HOME ?? path.join(homedir(), ".cache"); + const packages = path.join(cacheHome, "opencode", "packages"); + const spec = process.env.BILI_OPENCODE_ACP_SPEC; + if (spec !== undefined && spec.length > 0) { + // the spec is the package-cache slot name ("opencode-acp@1.18.1"); + // authoritative when present — avoid guessing among cache versions. + const hinted = path.join(packages, spec, "node_modules", "opencode-acp", "dist", "index.js"); + try { + accessSync(hinted); + out.unshift(hinted); + } catch { + // hint does not resolve — fall through to discovery + } + } + try { + const versions = readdirSync(packages) + .filter((e) => e.startsWith("opencode-acp@")) + .sort() + .reverse(); + for (const v of versions) { + pushDir(path.join(packages, v, "node_modules")); + } + } catch { + // no package cache + } + pushDir(path.join(homedir(), ".local", "lib", "node_modules")); + pushDir(path.join(process.env.XDG_CONFIG_HOME ?? path.join(homedir(), ".config"), "opencode", "node_modules")); + return out; +} + +/** acp's default persisted-state location for a session id. */ +export function legacyAcpStatePath(sessionId: string): string { + const dataHome = process.env.XDG_DATA_HOME ?? path.join(homedir(), ".local", "share"); + return path.join(dataHome, "opencode", "storage", "plugin", "acp", `${sessionId}.json`); +} + +/** A session is legacy iff acp has a state file for it. Ids are sanitized + * (no separators, bounded length) before touching the filesystem. */ +export function isLegacyAcpSession(sessionId: string | undefined): boolean { + if (sessionId === undefined || sessionId.length === 0 || sessionId.length > 200) return false; + if (/[/\\\s]/.test(sessionId)) return false; + try { + return existsSync(legacyAcpStatePath(sessionId)); + } catch { + return false; + } +} + +export async function loadLegacyAcp(ctx: { directory?: string; client?: unknown }, log: (msg: string) => void = () => {}): Promise { + let found: string | undefined; + for (const p of legacyAcpCandidates(ctx.directory)) { + if (existsSync(p)) { + found = p; + break; + } + } + if (found === undefined) return undefined; + // acp's server() refuses to arm when BILLION_CONTEXT_PROXY is set; the + // check runs only at entry, so it is unset strictly around the call and + // restored in finally. Concurrent readers on this event loop see either + // value only across our own awaits here. + const prev = process.env.BILLION_CONTEXT_PROXY; + try { + if (prev !== undefined) delete process.env.BILLION_CONTEXT_PROXY; + const mod = (await import(pathToFileURL(found).href)) as { default?: unknown }; + const server = mod.default; + if (typeof server !== "function") { + log(`[bili-opencode-native] legacy: ${found} exports no server function — ignored`); + return undefined; + } + const hooks = (await (server as (c: unknown) => Promise>)(ctx)) ?? {}; + if (typeof hooks !== "object") return undefined; + const toolsRaw = hooks.tool; + const tools = toolsRaw !== null && typeof toolsRaw === "object" && !Array.isArray(toolsRaw) ? (toolsRaw as Record) : {}; + if (Object.keys(tools).length === 0) { + log(`[bili-opencode-native] legacy: ${found} armed but registered no tools (acp disabled by its own config?) — ignored`); + return undefined; + } + const configHook = typeof hooks.config === "function" ? (hooks.config as LegacyAcpModule["configHook"]) : undefined; + const commandHook = typeof hooks["command.execute.before"] === "function" ? (hooks["command.execute.before"] as LegacyAcpModule["commandHook"]) : undefined; + log(`[bili-opencode-native] legacy opencode-acp absorbed from ${found} (tools: ${Object.keys(tools).join(", ")})`); + return { hooks, tools, configHook, commandHook, source: found }; + } catch (err) { + log(`[bili-opencode-native] legacy: failed to load ${found}: ${String(err)} — legacy sessions degrade to read-only`); + return undefined; + } finally { + if (prev === undefined) delete process.env.BILLION_CONTEXT_PROXY; + else process.env.BILLION_CONTEXT_PROXY = prev; + } +} + +/** Call acp's config hook without letting it see (and self-disable on) the + * /bili/-wrapped provider URLs, then merge back the top-level keys it may + * have replaced on the shadow copy (it writes via `??=`/spread-replace, so a + * reference change marks an owned write; in-place mutations already hit the + * real object). */ +export async function callLegacyAcpConfig(configHook: NonNullable, cfg: Record): Promise { + const shadow: Record = { ...cfg }; + delete shadow.provider; + await configHook(shadow); + for (const key of ["permission", "command", "experimental"] as const) { + if (shadow[key] !== cfg[key]) cfg[key] = shadow[key]; + } +} diff --git a/src/agent/opencode-native.ts b/src/agent/opencode-native.ts new file mode 100644 index 0000000000..eaf9b8de6e --- /dev/null +++ b/src/agent/opencode-native.ts @@ -0,0 +1,590 @@ +// Native opencode mode (#820 opencode line; npm-package design tracked in +// #809): the package-installed OpenCode 2.x plugin bootstraps its own proxy — +// the pi pattern (#519/#706), adapted to opencode's egress seam. Flow at +// plugin load: +// 1. spawn the package's own proxy (`dist/index.js start`, ephemeral port, +// parent-pid watchdog = this opencode process) via ensureProxyRunning — +// a healthy compatible instance is ATTACHED, not doubled; +// 2. register an http.request hook that rewrites model-API URLs to +// `/bili/` and stamps nothing else — header +// stamping / tools / compaction reporting all reuse the shared V2 setup +// (opencode-v2.ts), which detects the proxy through the env var set in +// step 1; +// 3. set BILLION_CONTEXT_PROXY after bootstrap so tool execute() and the +// compaction reporter find the proxy through their existing env path. +// +// Why URL rewrite instead of pi's global fetch patch: opencode's plugins have +// no fetch seam they can patch — the only observed egress hook is http.request +// with the outgoing fetch Request at e.request. WHATWG Request.url is +// read-only at runtime (verified live on 2.0.x, #810), so the request REFERENCE +// is replaced with a new Request to `/bili/` carrying the same +// method/headers/body. +// +// Liveness gate (differs from pi by design): an http.request hook CANNOT +// observe send failures (the host swallows them; #809 verification list), so +// unlike pi's post-failure respawn, every outgoing request first probes +// `/__bili/health` (bounded) of the current origin and respawns if dead. If +// no origin can be made healthy the request goes DIRECT (uncompressed) and a +// one-time stderr warning fires — recovery is automatic once a proxy is +// healthy again (no permanent latch; each request re-verifies). Bootstrap +// retries are rate-limited to one attempt per RESPAWN_COOLDOWN_MS so a +// persistently failing spawn does not become a per-request spawn storm. +// +// Skipped when opted out (BILI_NATIVE_OPENCODE=0 / BILLION_CONTEXT_PLUGIN=0) +// or when a `bili` /bili/ launch owns routing (BILI_PROVIDER_REWRITES). A +// preset BILLION_CONTEXT_PROXY is treated as an EXTERNAL attach target +// (probe + rewrite + stamp) instead of a stand-down — see planNativeOpencode. +// +// Deployment: OpenCode 2.x `plugin` entries must be DIRECTORIES whose index.js +// is the entrypoint (bare file paths are rejected, #754 probe) — `bili plugin +// install opencode` writes /plugins/billion-context/index.js +// re-exporting this entry (same wrapper shape the launcher builds, +// src/launcher.ts prepareOpencodeHttpRewrite). +// +// OpenCode 1.x loads the SAME entry through `.server(ctx)` (hooks object). +// V1 runtime facts (probed on 1.14.46, ~/projects/opencode-stable): +// - config hook: receives the SHARED cached config object (Config.get → +// InstanceState.use) — mutating provider..options.baseURL persists +// for the process lifetime, no file writes needed. Providers WITHOUT an +// explicit baseURL (SDK defaults) are caught by the global fetch patch +// installed in server() (see installNativeFetchIntercept below). +// - "chat.headers": per-LLM-request header mutation — the V1 equivalent of +// V2's stampHeaders (x-bili-plugin + conversation id). +// - tool: { [name]: { description, args, execute } } registers native tools; +// args must be REAL zod fields (registry wraps them with its own +// z.object(...).safeParse) — hence zod is a runtime dependency, lazily +// imported below. Same tool set/schemas as V2 (ACP_TOOLS_OPENAI), +// converted JSON-schema → zod. When zod cannot be resolved (dist copied +// without node_modules) the plugin degrades to proxy mode: no headers, no +// tools — the proxy injects wire tools, compression still works. +// - command.execute.before + ctx.client.session.prompt: /acp command +// (shared factory opencode-acp-command.ts). +// - "experimental.session.compacting" exists but config compaction.auto=false +// (written by the config hook, same as the launcher/installer) is the +// owner-switch; no compaction-prompt surgery needed. + +import { ACP_TOOLS_OPENAI } from "../compress-tool.js"; +import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST, unwrapUpstream, wrapUpstream } from "../launcher.js"; +import { createAcpCommandHooks } from "./opencode-acp-command.js"; +import { markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, singleFlight } from "./native-bootstrap.js"; +import { installNativeFetchIntercept, isModelApiUrl, readyOrigin, type NativeInterceptState } from "./native-intercept.js"; +import { createOpencodeV2Setup, type V2HttpRequestEvent, type V2State } from "./opencode-v2.js"; +import { callLegacyAcpConfig, isLegacyAcpSession, loadLegacyAcp, type LegacyAcpModule } from "./opencode-legacy.js"; + +/** Decides whether the native bootstrap should run in this process. */ +export function shouldBootstrapNativeOpencode(env: NodeJS.ProcessEnv): boolean { + return nativeBootstrapGate(env, "BILI_NATIVE_OPENCODE"); +} + +/** Decide this process's native posture (#809). Precedence kill-switch > + * /bili/ rewrite launch > attach > spawn. A preset BILLION_CONTEXT_PROXY is + * an ATTACH target, not a stand-down: the pseudo-attach hole (preset env + + * bare opencode) used to disarm routing entirely while tools still found the + * proxy through the env — model traffic went direct, the proxy never saw the + * session, and every tool forward 404'd. Attach keeps the user's intent + * ("route through THIS proxy") and matches BILLION_CONTEXT_ATTACH semantics + * (probe + rewrite + stamp); explicit BILLION_CONTEXT_ATTACH wins when both + * are set. A /bili/ launch (BILI_PROVIDER_REWRITES) still stands us down — + * its URLs are already proxy-shaped and isModelApiUrl skips them. */ +export function planNativeOpencode(env: NodeJS.ProcessEnv): { mode: "off" | "attach" | "spawn"; attachOrigin?: string } { + if (env.BILLION_CONTEXT_PLUGIN === "0" || env.BILI_NATIVE_OPENCODE === "0") return { mode: "off" }; + if (env.BILI_PROVIDER_REWRITES !== undefined) return { mode: "off" }; + const attachOrigin = nativeAttachOrigin(env) ?? proxyEnvOrigin(env); + if (attachOrigin !== undefined) return { mode: "attach", attachOrigin }; + return { mode: "spawn" }; +} + +const HEALTH_TIMEOUT_MS = 1500; +const RESPAWN_COOLDOWN_MS = 15_000; + +async function probeHealth(origin: string): Promise { + try { + const res = await fetch(`${origin}/__bili/health`, { signal: AbortSignal.timeout(HEALTH_TIMEOUT_MS) }); + return res.ok; + } catch { + return false; + } +} + +export interface OpencodeNativeRouteDeps { + probe?: (origin: string) => Promise; + /** Bootstrap retry interval when no live origin is held (tests shrink it). */ + respawnCooldownMs?: number; +} + +/** Build the native-mode route callback consumed by createOpencodeV2Setup. + * The probe is injectable for tests; the default does a bounded GET of + * /__bili/health. */ +export function createNativeRoute(state: NativeInterceptState, deps: OpencodeNativeRouteDeps = {}): (e: V2HttpRequestEvent, s: V2State) => Promise { + const probe = deps.probe ?? probeHealth; + const respawnCooldownMs = deps.respawnCooldownMs ?? RESPAWN_COOLDOWN_MS; + let warned = false; + let attachWarned = false; + let lastRespawn = 0; + + const healthyOrigin = async (): Promise => { + let ownedThenLost = false; + if (state.origin !== undefined) { + if (await probe(state.origin)) return state.origin; + // Proxy died mid-session. Clearing origin first makes concurrent + // callers share the same state.ready (dedup). + ownedThenLost = true; + state.origin = undefined; + } + // Retry bootstrap whenever no live origin is held — either just lost + // it or the load-time bootstrap failed (the hook cannot observe send + // failures, so nothing else would retry). Cooldown bounds attempts to + // one per interval instead of one per request. + if (state.respawn !== undefined && (ownedThenLost || Date.now() - lastRespawn >= respawnCooldownMs)) { + lastRespawn = Date.now(); + state.ready = state.respawn(); + } + const o = await readyOrigin(state); + if (o !== undefined && (await probe(o))) return o; + if (ownedThenLost) state.onGiveUp?.(); + return undefined; + }; + + const rewriteToProxy = (ev: V2HttpRequestEvent, target: string, url: string): void => { + try { + ev.request = new Request(`${target}/bili/${url}`, ev.request as unknown as Request); + } catch { + // undici refuses to copy a body-bearing Request without explicit + // duplex — reconstruct with the body stream passed explicitly. + const old = ev.request as unknown as { method?: unknown; headers?: Iterable | null; body?: ReadableStream | null }; + try { + const init: RequestInit & { duplex?: "half" } = { method: typeof old.method === "string" ? old.method : "GET" }; + const pairs: [string, string][] = []; + try { + for (const pair of old.headers ?? []) pairs.push([pair[0], pair[1]]); + } catch {} + if (pairs.length > 0) init.headers = pairs; + if (old.body != null) { + init.body = old.body as RequestInit["body"]; + init.duplex = "half"; + } + ev.request = new Request(`${target}/bili/${url}`, init); + } catch { + // replacement impossible (exotic body) — request goes direct + } + } + }; + + return async (e, s) => { + const url = typeof e.request?.url === "string" ? e.request.url : undefined; + if (url === undefined) return; + if (!isModelApiUrl(url)) return; + + // Attach mode (#809): route through the user's external proxy. We do + // NOT own it — no spawn, no respawn. Fail-closed by construction: we + // always rewrite toward the target and never fall back to direct, so a + // dead target surfaces as a client-visible connection error (plus one + // loud diagnostic), never as silent uncompressed traffic. + if (state.attach) { + const target = state.origin; + if (target === undefined) return; + const up = await probe(target); + if (!up && !attachWarned) { + attachWarned = true; + console.error(`bili-native-opencode: external proxy ${target} unreachable — model requests will fail (compression unavailable)`); + } else if (up) { + attachWarned = false; + } + s.proxyBase = target; + rewriteToProxy(e, target, url); + return; + } + + const target = await healthyOrigin(); + if (target === undefined) { + if (!warned) { + warned = true; + console.error("bili-native-opencode: proxy unavailable — model requests go direct (uncompressed)"); + } + return; + } + warned = false; + s.proxyBase = target; + rewriteToProxy(e, target, url); + }; +} + +const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined) }; + +async function bootstrap(): Promise { + try { + const handle = await ensureProxyRunning( + { host: LAUNCHER_DEFAULT_HOST, port: 0, passthrough: false, debug: false }, + { scriptPath: nativeProxyScriptPath() }, + ); + state.origin = handle.origin; + process.env.BILLION_CONTEXT_PROXY = handle.origin; + return handle.origin; + } catch (err) { + console.error(`bili-native-opencode: proxy bootstrap failed — model traffic goes direct (uncompressed): ${err instanceof Error ? err.message : String(err)}`); + return undefined; + } +} + +const plan = planNativeOpencode(process.env); +if (plan.mode !== "off") { + markNativeHost(process.env, "opencode"); + if (process.env.NODE_TEST_CONTEXT === undefined) { + if (plan.mode === "attach") { + state.attach = true; + state.origin = plan.attachOrigin; + // Tools / compaction discover the proxy through this env path, as + // in self-spawn; no respawn/onGiveUp — the external proxy isn't + // ours to restart or clean up. + process.env.BILLION_CONTEXT_PROXY = plan.attachOrigin; + } else { + const start = singleFlight(bootstrap); + state.respawn = start; + state.onGiveUp = () => { + delete process.env.BILLION_CONTEXT_PROXY; + }; + state.ready = start(); + } + } +} + +// ———— OpenCode 1.x native surface (V1 `.server()`) ———————————————————— + +type ZodLike = typeof import("zod"); + +interface V1ToolContext { + sessionID: string; + messageID?: string; + agent?: string; + abort?: AbortSignal; +} + +interface V1Tool { + description: string; + args: Record; + execute: (args: Record, ctx: V1ToolContext) => Promise; +} + +export interface V1ProviderOptions { + baseURL?: unknown; + [key: string]: unknown; +} + +export interface V1ModelLimit { + context?: unknown; + [key: string]: unknown; +} + +export interface V1ModelDef { + limit?: V1ModelLimit | undefined; + [key: string]: unknown; +} + +export interface V1ProviderDef { + options?: V1ProviderOptions | undefined; + models?: Record | undefined; + [key: string]: unknown; +} + +export interface V1Config { + provider?: Record | undefined; + command?: Record; + compaction?: unknown; + [key: string]: unknown; +} + +export interface V1ChatHeadersInput { + sessionID: string; + agent?: string; + model?: { providerID?: unknown; id?: unknown }; +} + +export interface V1Hooks { + config?: (input: V1Config) => Promise; + "chat.headers"?: (input: V1ChatHeadersInput, output: { headers: Record }) => Promise; + "command.execute.before"?: (input: { command: string; sessionID: string; arguments?: string }, output?: { parts: unknown[] }) => Promise; + tool?: Record; + event?: (input: { event?: unknown }) => Promise; + "experimental.chat.system.transform"?: (input: { sessionID?: string }, output: { system?: unknown[] }) => Promise; + "experimental.chat.messages.transform"?: (input: unknown, output: { messages?: unknown }) => Promise; + "experimental.text.complete"?: (input: { sessionID: string }, output: { text?: unknown }) => Promise; +} + +export interface V1PluginContext { + client?: import("./opencode-acp-command.js").OpencodeClient; + directory?: string; +} + +/** JSON-schema (ACP_TOOLS_OPENAI parameters) → zod raw shape. Only the shapes + * our own tools use; anything exotic degrades to z.any() — the proxy + * re-validates server-side anyway (parseCompressArgs), client zod is just + * the host's parameter gate. */ +export function jsonSchemaToZodShape(schema: unknown, z: ZodLike): Record { + if (schema === null || typeof schema !== "object" || Array.isArray(schema)) return {}; + const props = (schema as { properties?: unknown }).properties; + if (props === null || typeof props !== "object" || Array.isArray(props)) return {}; + const out: Record = {}; + for (const [key, raw] of Object.entries(props as Record)) { + out[key] = jsonSchemaFieldToZod(raw, z); + } + return out; +} + +function jsonSchemaFieldToZod(raw: unknown, z: ZodLike): unknown { + if (raw === null || typeof raw !== "object") return z.any(); + const spec = raw as { type?: unknown; enum?: unknown; items?: unknown; anyOf?: unknown; description?: unknown }; + if (Array.isArray(spec.enum) && spec.enum.length > 0 && spec.enum.every((v) => typeof v === "string")) { + return z.enum(spec.enum as [string, ...string[]]); + } + switch (spec.type) { + case "string": + return typeof spec.description === "string" ? z.string().describe(spec.description) : z.string(); + case "number": + return z.number(); + case "boolean": + return z.boolean(); + case "array": { + const item = jsonSchemaFieldToZod(spec.items, z); + return item === undefined ? z.array(z.any()) : z.array(item as never); + } + default: + // object / anyOf / unknown — accept anything; the proxy validates + return z.any(); + } +} + +/** Rewrite provider baseURLs to `/bili/` (idempotent) and flip + * compaction.auto off. Providers without an explicit baseURL keep their SDK + * default (traffic goes direct) — V1 has no request seam to catch those. */ +export function rewriteV1Providers(cfg: V1Config, origin: string): number { + const providers = cfg.provider; + if (providers === null || typeof providers !== "object") return 0; + let rewritten = 0; + for (const entry of Object.values(providers)) { + const options = entry?.options; + if (options === null || typeof options !== "object") continue; + const base = options.baseURL; + if (typeof base !== "string" || base.trim().length === 0) continue; + if (!/^https?:\/\//i.test(base)) continue; + // unwrapUpstream strips ANY existing `<…>/bili/` prefix (stale wrap + // from another proxy origin included), wrapUpstream re-adds ours. + const next = wrapUpstream(origin, unwrapUpstream(base)); + if (next === base) continue; + options.baseURL = next; + rewritten++; + } + const compaction = cfg.compaction; + cfg.compaction = { + ...(compaction !== null && typeof compaction === "object" && !Array.isArray(compaction) ? (compaction as Record) : {}), + auto: false, + }; + return rewritten; +} + +/** Provider-declared model windows (`provider..models..limit.context`) as a `${id}/${m}` map. + * V1 has no catalog seam (cf. V2 `ctx.catalog.model.list()`), so config is the only window source; + * non-finite/non-positive values are dropped so a partial table stays unstamped rather than fabricated. */ +export function extractV1Windows(cfg: V1Config): Map { + const map = new Map(); + const providers = cfg.provider; + if (providers === null || typeof providers !== "object") return map; + for (const [pid, entry] of Object.entries(providers)) { + const models = entry?.models; + if (models === null || typeof models !== "object") continue; + for (const [mid, model] of Object.entries(models)) { + const c = model?.limit?.context; + if (typeof c === "number" && Number.isFinite(c) && c > 0) map.set(`${pid}/${mid}`, Math.floor(c)); + } + } + return map; +} + +export interface V1NativeDeps { + /** zod module (tests inject; runtime lazy-imports "zod"). */ + z?: ZodLike; + /** Tool forwarder (tests inject; runtime POSTs /__bili/plugin/tool). */ + forward?: (origin: string, conversationId: string, tool: string, args: unknown) => Promise; + /** Absorbed opencode-acp for legacy sessions (#920); when present its DCP + * tool slots serve BOTH lanes (legacy → acp executor, new → forward). */ + legacy?: LegacyAcpModule; + /** Legacy-session predicate (tests inject; runtime = state file exists). */ + isLegacy?: (sessionId: string | undefined) => boolean; + log?: (msg: string) => void; +} + +/** Build the V1 hooks for a RESOLVED proxy origin. `deps.z` present → plugin + * mode (tools + header stamping); absent → proxy mode (rewrite only, the + * proxy injects wire tools). Exported for tests; the `server` export below + * wires the real bootstrap + zod. */ +function lastSessionFromMessages(messages: unknown): string | undefined { + if (!Array.isArray(messages)) return undefined; + for (let i = messages.length - 1; i >= 0; i--) { + const info = (messages[i] as { info?: { sessionID?: unknown } } | undefined)?.info; + if (typeof info?.sessionID === "string") return info.sessionID; + } + return undefined; +} + +export function createV1ServerHooks(origin: string, ctx: V1PluginContext, deps: V1NativeDeps = {}): V1Hooks { + const acp = createAcpCommandHooks(() => origin, ctx); + const legacy = deps.legacy; + const isLegacy = deps.isLegacy ?? isLegacyAcpSession; + const log = deps.log ?? ((msg: string) => console.log(msg)); + let windows = new Map(); + const hooks: V1Hooks = { + config: async (cfg) => { + if (legacy?.configHook !== undefined) { + await callLegacyAcpConfig(legacy.configHook, cfg); + } + await acp.config?.(cfg); + const n = rewriteV1Providers(cfg, origin); + if (n > 0) log(`[bili-opencode-native] v1: rewrote ${n} provider baseURL(s) -> ${origin}/bili/`); + windows = extractV1Windows(cfg); + }, + "command.execute.before": async (input, output) => { + if ((input.command === "acp" || input.command === "dcp") && legacy?.commandHook !== undefined && isLegacy(input.sessionID)) { + await legacy.commandHook(input, output); + return; + } + await acp["command.execute.before"]?.(input); + }, + }; + if (legacy !== undefined) { + // Legacy lane (#920): route acp's own transforms to legacy sessions + // only — new sessions never enter acp's registry (no adoption). + const sys = legacy.hooks["experimental.chat.system.transform"]; + if (typeof sys === "function") { + hooks["experimental.chat.system.transform"] = async (input, output) => { + if (!isLegacy(input.sessionID)) return; + await (sys as (i: unknown, o: unknown) => Promise)(input, output); + }; + } + const msgT = legacy.hooks["experimental.chat.messages.transform"]; + if (typeof msgT === "function") { + hooks["experimental.chat.messages.transform"] = async (input, output) => { + if (!isLegacy(lastSessionFromMessages(output?.messages))) return; + await (msgT as (i: unknown, o: unknown) => Promise)(input, output); + }; + } + const textC = legacy.hooks["experimental.text.complete"]; + if (typeof textC === "function") { + hooks["experimental.text.complete"] = async (input, output) => { + if (!isLegacy(input.sessionID)) return; + await (textC as (i: unknown, o: unknown) => Promise)(input, output); + }; + } + // Event hook carries no session (acp uses it only for compress timing + // on message.part.updated) — pass through ungated, state-independent. + const evt = legacy.hooks.event; + if (typeof evt === "function") { + hooks.event = evt as V1Hooks["event"]; + } + } + if (deps.z !== undefined || legacy !== undefined) { + hooks["chat.headers"] = async (input, output) => { + if (isLegacy(input.sessionID)) { + // Legacy sessions run through absorbed acp; the proxy must + // forward their traffic verbatim (no injection, no binding). + output.headers["x-bili-plugin-bypass"] = "1"; + return; + } + output.headers["x-bili-plugin"] = "opencode"; + output.headers["x-bili-plugin-conversation"] = input.sessionID; + const model = input.model; + if (model && typeof model.providerID === "string" && typeof model.id === "string") { + const w = windows.get(`${model.providerID}/${model.id}`); + if (w !== undefined) output.headers["x-bili-plugin-context-window"] = String(w); + } + }; + const forward = deps.forward ?? ((o, conversationId, tool, args) => import("./shared.js").then((m) => m.forwardTool(o, conversationId, tool, args))); + if (legacy !== undefined) { + // Tool slots carry acp's DCP schemas (kernel-parseable object form) + // for BOTH lanes; executors route per session. acp_context_recap + // has no proxy counterpart — legacy-only, forwarded calls fail + // with the endpoint's unknown-tool message (documented). + const tools: Record = {}; + for (const [name, def] of Object.entries(legacy.tools)) { + const exec = typeof def.execute === "function" ? def.execute : undefined; + tools[name] = { + description: typeof def.description === "string" ? def.description : name, + args: (def.args ?? {}) as Record, + execute: async (args, v1ctx) => { + if (exec !== undefined && isLegacy(v1ctx.sessionID)) { + return String((await exec(args, v1ctx)) ?? ""); + } + return forward(origin, v1ctx.sessionID, name, args); + }, + }; + } + hooks.tool = tools; + } else { + const tools: Record = {}; + if (deps.z !== undefined) { + for (const t of ACP_TOOLS_OPENAI) { + const fn = t.function; + tools[fn.name] = { + description: fn.description ?? fn.name, + args: jsonSchemaToZodShape(fn.parameters, deps.z), + execute: async (args, v1ctx) => forward(origin, v1ctx.sessionID, fn.name, args), + }; + } + } else { + console.error("[bili-opencode-native] v1: zod unavailable — plugin tools skipped; sessions run in proxy mode (wire-injected compress)"); + } + if (Object.keys(tools).length > 0) hooks.tool = tools; + } + } + return hooks; +} + +async function resolveV1Origin(): Promise { + try { + return await readyOrigin(state); + } catch { + return undefined; + } +} + +const server = async (ctx: V1PluginContext): Promise => { + if (plan.mode === "off") return {}; + const origin = await resolveV1Origin(); + if (origin === undefined) { + console.error("[bili-opencode-native] v1: proxy bootstrap failed — model traffic goes direct (uncompressed)"); + return {}; + } + console.log(`[bili-opencode-native] v1 active (proxy ${origin})`); + // Global fetch patch (pi-native's mechanism): the config-hook rewrite can + // only catch providers with an EXPLICIT baseURL — V1 has no request-level + // hook, and providers relying on the SDK default (e.g. bare + // `@ai-sdk/openai` → api.openai.com) would silently bypass the proxy. + // v1's provider stack resolves `fetch` late (`customFetch ?? fetch` inside + // the per-request wrapper, provider.ts), so a global patch catches those + // requests too. Idempotent (flag-guarded) and disjoint from the config + // rewrite: isModelApiUrl passes `/bili/`-prefixed URLs straight through, + // so already-rewritten providers never double-wrap. Proxy-death respawn / + // degrade-to-direct semantics come with the shared state. + if (installNativeFetchIntercept(state)) { + console.log("[bili-opencode-native] v1: fetch patch installed (catches providers without an explicit baseURL)"); + } + let z: ZodLike | undefined; + try { + z = (await import("zod")) as ZodLike; + } catch { + z = undefined; + } + // Legacy lane (#920): absorb the installed opencode-acp so pre-migration + // sessions keep their DCP machinery. Absent/failing package → undefined → + // bili-only mode (legacy sessions degrade to read-only, documented). + let legacy: LegacyAcpModule | undefined; + try { + // Pass the host's full ctx object through: V1PluginContext types only + // what bili uses (client/directory), but the runtime object may carry + // extra host fields acp's hooks need at action time. + legacy = await loadLegacyAcp(ctx, (msg) => console.log(msg)); + } catch { + legacy = undefined; + } + return createV1ServerHooks(origin, ctx, { z, legacy }); +}; + +export default { id: "billion-context-opencode-native", setup: createOpencodeV2Setup({ route: createNativeRoute(state) }), server }; diff --git a/src/agent/opencode-v2.ts b/src/agent/opencode-v2.ts new file mode 100644 index 0000000000..0ff03994a6 --- /dev/null +++ b/src/agent/opencode-v2.ts @@ -0,0 +1,337 @@ +// Shared OpenCode 2.x (V2 plugin API) setup factory — one implementation of +// tool registration / header stamping / context-window refresh / compaction +// boundary reporting used by BOTH deployment modes: +// - launcher mode: src/agent/opencode.ts (proxy pointed at by config/env) +// - native mode: src/agent/opencode-native.ts (self-spawned proxy) +// Keeping the protocol logic here means the two modes cannot drift apart. +// +// No acp-kernel, no compression logic — the proxy is the single compression +// authority. Structural types only (no @opencode/plugin import) so this file +// stays loadable under both host generations via the object exports above it +// (V2 validates `{ id, setup }`; V1 >= 1.18.29 calls `.server()`). +// +// Runtime API facts — VERSION-SPECIFIC (the 2.x plugin surface changes between +// builds; do not generalize beyond the build named): +// - next-17444 pre-release (probed live by the author): model.request registers +// but NEVER FIRES; http.request fires per outgoing provider request with a +// fetch Request at e.request (mutating e.request.headers reaches the wire); +// NO ctx.tool.reload(); command editor list/get/update/remove only (no ADD — +// hence no /acp under V2; acp_status tool is the in-host equivalent). +// - @opencode/cli 2.0.x stable (provenance confirmed during #754 review; +// probed live on 2.0.1 + 2.0.3): setup() is chosen over server(); BOTH +// model.request and http.request hooks fire (e.request mutation reaches the +// wire); ctx.tool = {reload, transform, hook} (reload EXISTS here); +// ctx.command.transform(editor.add) CAN add commands (TUI invocation needs +// Tab+Enter completion accept; `run` mode dispatches no slash commands at +// all); configured `plugin` entries must be DIRECTORIES (file paths are +// rejected with WARN "configured plugin path must be a directory"; the +// directory's index.js is the entrypoint) — the launcher wraps this single +// file accordingly (src/launcher.ts opencodeMajorVersion). End-to-end +// verified on 2.0.3: true plugin mode, native tools via the plugin tool +// endpoint, zero wire-level injection. +// - npm dev builds 2026-09-13 / 2026-09-14 (probed live during #754 review): +// first loads plugins via V1 server() only; second exposes setup() but has +// no ctx.session / ctx.tool at all. Adjacent dev builds disagree with each +// other and with both of the above. +// Consequence: every registration below uses optional chaining so the plugin +// is inert-safe on any surface; when no seam fires, sessions transparently run +// in proxy mode (wire-level tool injection) instead of failing. Tools stay +// registered synchronously from bundled schemas (exact parity with the proxy's +// openai tool list, src/compress-tool.ts) because reload-based refresh is not +// available on all observed surfaces. + +import { ACP_TOOLS_OPENAI, ABSORB_TOOL_OPENAI } from "../compress-tool.js"; +import { fetchProxyVersion, fetchStatus, forwardTool, proxyBaseFromEnv, proxyBaseFromUrl, reportCompactionBoundary } from "./shared.js"; + +// OpenCode V2 TUI renders a synthetic message as a visible Notice row only when its display text fits the +// timeline cap (~1KB): longer text renders nothing (#880). Panels go to description verbatim under the cap. +import { V2_SYNTHETIC_TEXT } from "./shared.js"; +const V2_SYNTHETIC_VISIBLE_MAX = 1024; + +type V2Registration = { dispose?: () => void | Promise }; + +interface V2Headers { + set(name: string, value: string): void; +} + +export interface V2HttpRequestEvent { + sessionID?: unknown; + model?: { providerID?: unknown; id?: unknown }; + request?: { url?: unknown; headers?: V2Headers | null }; +} + +interface V2ToolEditor { + add(tool: { + name: string; + description?: string; + input: unknown; + options?: { namespace?: string; permission?: string; codemode?: boolean; pinned?: boolean }; + execute: (input: Record, ctx: { sessionID: string }) => Promise<{ content: string }>; + }): void; +} + +interface V2CommandInvocation { + sessionID?: unknown; + [key: string]: unknown; +} + +interface V2CommandEditor { + add(def: { + name: string; + description?: string; + execute: (input: V2CommandInvocation) => Promise; + }): void; +} + +interface V2CatalogModelEntry { + providerID?: unknown; + id?: unknown; + limit?: { context?: unknown }; +} + +export interface V2PluginContext { + session?: { + hook?: (name: string, cb: (e: V2HttpRequestEvent) => void | Promise) => void | Promise; + synthetic?: (input: { sessionID: string; text: string; description?: string; resume?: boolean }) => Promise; + }; + tool?: { + transform?: (cb: (editor: V2ToolEditor) => void) => void | Promise; + }; + command?: { + transform?: (cb: (editor: V2CommandEditor) => void) => void | Promise; + }; + event?: { subscribe?: (opts?: { signal?: AbortSignal }) => AsyncIterable<{ type?: unknown; data?: Record }> | undefined }; + catalog?: { model?: { list?: () => Promise<{ data?: V2CatalogModelEntry[] }> | undefined } | undefined }; +} + +const WINDOW_REFRESH_MS = 60000; + +// Kill switch = fully inert (same semantics as detectProxyBase): gates header stamping, tool forwarding, compaction reporting. +const pluginDisabled = (): boolean => process.env.BILLION_CONTEXT_PLUGIN === "0"; + +const V2_BILI_TOOLS = [...ACP_TOOLS_OPENAI, ABSORB_TOOL_OPENAI].map((t) => ({ + name: t.function.name, + description: t.function.description, + input: t.function.parameters, +})); + +export interface V2State { + proxyBase?: string; + windows?: Map; + windowsAt?: number; +} + +function refreshWindows(ctx: V2PluginContext, state: V2State): void { + const now = Date.now(); + if (state.windows && state.windowsAt !== undefined && now - state.windowsAt < WINDOW_REFRESH_MS) return; + state.windowsAt = now; + void (async () => { + try { + const res = await ctx.catalog?.model?.list?.(); + const map = new Map(); + for (const m of res?.data ?? []) { + const pid = typeof m.providerID === "string" ? m.providerID : ""; + const id = typeof m.id === "string" ? m.id : ""; + const c = m.limit?.context; + if (pid && id && typeof c === "number" && Number.isFinite(c) && c > 0) map.set(`${pid}/${id}`, Math.floor(c)); + } + if (map.size > 0) state.windows = map; + } catch { + // catalog unavailable — window header simply goes unstamped + } + })(); +} + +export interface OpencodeV2SetupOptions { + /** Native mode only: called for EVERY outgoing provider request before + * stamping. Owns proxy discovery and URL rewriting to the self-spawned + * proxy (idempotent on already-routed URLs) and sets state.proxyBase + * when traffic is routed. Absent in launcher mode (no routing). */ + route?: (e: V2HttpRequestEvent, state: V2State) => void | Promise; +} + +export function createOpencodeV2Setup(options: OpencodeV2SetupOptions = {}): (ctx: V2PluginContext) => Promise<() => void> { + return async (ctx: V2PluginContext): Promise<() => void> => { + const ac = new AbortController(); + const state: V2State = {}; + const registrations: V2Registration[] = []; + + const stampHeaders = (e: V2HttpRequestEvent): void => { + const headers = e.request?.headers; + const sid = typeof e.sessionID === "string" ? e.sessionID : ""; + if (!headers || typeof headers.set !== "function" || !sid || !state.proxyBase) return; + headers.set("x-bili-plugin-conversation", sid); + headers.set("x-bili-plugin", "opencode"); + const model = e.model; + if (model && typeof model.providerID === "string" && typeof model.id === "string") { + const window = state.windows?.get(`${model.providerID}/${model.id}`); + if (window !== undefined) headers.set("x-bili-plugin-context-window", String(window)); + } + }; + + const httpRequestHook = async (e: V2HttpRequestEvent): Promise => { + if (pluginDisabled()) return; + const url = e.request?.url; + if (typeof url !== "string") return; + if (options.route !== undefined) { + // Native mode: route owns discovery + rewriting (it is called + // per request even once state.proxyBase is known, because the + // URL rewrite must happen per request and env detection alone + // would skip it after bootstrap). + await options.route(e, state); + } else if (!state.proxyBase) { + state.proxyBase = proxyBaseFromUrl(url) ?? proxyBaseFromEnv(); + } + if (!state.proxyBase) return; + refreshWindows(ctx, state); + stampHeaders(e); + }; + + const hookReg = await ctx.session?.hook?.("http.request", httpRequestHook); + if (hookReg) registrations.push(hookReg); + + const toolReg = await ctx.tool?.transform?.((editor) => { + for (const t of V2_BILI_TOOLS) { + editor.add({ + name: t.name, + description: t.description, + input: t.input, + options: { codemode: false, permission: "allow" }, + execute: async (args, tctx) => { + if (pluginDisabled()) return { content: "bili: disabled (BILLION_CONTEXT_PLUGIN=0)" }; + const base = state.proxyBase ?? proxyBaseFromEnv(); + if (!base) return { content: "bili: no proxy detected (launch opencode through `bili opencode`, or point the provider baseURL at the bili proxy)" }; + try { + // Panel-first for acp_status: the proxy's status + // endpoint renders the same rich panel the /acp + // command shows; the forwarded kernel tool returns + // the legacy flat report. acp_status is read-only, + // so reading the panel changes no state. Fall back + // to the tool call when no panel comes back (older + // proxy, unknown conversation). + if (t.name === "acp_status") { + const status = await fetchStatus(base, tctx.sessionID); + const panel = status?.["panel"]; + if (typeof panel === "string" && panel.length > 0) return { content: panel }; + } + const result = await forwardTool(base, tctx.sessionID, t.name, args); + return { content: result }; + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + if (msg.includes("no model request has arrived with this conversation id yet")) { + return { content: "bili: no ACP state for this session yet — no model request has been routed through the proxy. Tell the user to send one normal message first; ACP activates automatically once model traffic flows through the proxy (verify the provider baseURL goes through bili, or launch via `bili opencode` / the installed plugin)." }; + } + return { content: msg }; + } + }, + }); + } + }); + if (toolReg) registrations.push(toolReg); + + // /acp status command (#809): registered via the V2 command editor where + // supported (2.0.x stable; inert-safe otherwise) and rendered as a + // synthetic non-model-turn message; TUI-only (`run` dispatches no slash cmds). + try { + const commandReg = await ctx.command?.transform?.((editor) => { + editor.add({ + name: "acp", + description: "Show ACP status (billion-context proxy)", + execute: async (input) => { + const sid = typeof input.sessionID === "string" ? input.sessionID : ""; + if (!sid) { + console.warn("[bili-opencode] /acp invoked without a sessionID; cannot render ACP status"); + return; + } + let text: string; + if (pluginDisabled()) { + text = "bili: disabled (BILLION_CONTEXT_PLUGIN=0)"; + } else { + const base = state.proxyBase ?? proxyBaseFromEnv(); + if (!base) { + text = "bili: no proxy detected (set BILLION_CONTEXT_PROXY or point the provider at the proxy's /bili/ URL, then run /acp again)"; + } else { + try { + const status = await fetchStatus(base, sid); + if (status && typeof status.panel === "string" && status.panel.length > 0) { + text = status.panel; + } else if (status && status.ok === false) { + let version: string | undefined; + try { + version = await fetchProxyVersion(base); + } catch { + version = undefined; + } + text = version !== undefined + ? `billion-context@${version} — proxy connected, no ACP session yet. Send a model request, then run /acp again.` + : "bili: no ACP session yet (send a model request first, then run /acp)"; + } else if (!status) { + // fetchStatus soft-fails to undefined both when the proxy 404s an absent or + // never-seen conversation and when the proxy is unreachable. Probe the + // manifest to claim "connected" only when it answers. + const version = await fetchProxyVersion(base).catch(() => undefined); + text = version !== undefined + ? `billion-context@${version} — proxy connected, no ACP session yet. Send a model request, then run /acp again.` + : "bili: cannot reach the bili proxy (run `bili start`, then run /acp again)"; + } else { + const errText = status?.error; + text = typeof errText === "string" && errText.length > 0 + ? `bili: proxy returned no status panel (${errText})` + : "bili: proxy returned no status panel"; + } + } catch (err) { + text = `bili: /acp failed (${err instanceof Error ? err.message : String(err)})`; + } + } + } + try { + // resume:false — OpenCode V2 defaults to delivery "steer" + execution.wake(), which would + // start a model turn on every /acp invocation with no user input (spurious empty turns). + // Short panels become the visible description verbatim; long ones keep their leading + // lines plus a marker so the model-facing body never repeats the whole panel. + const description = text.length > V2_SYNTHETIC_VISIBLE_MAX + ? text.slice(0, V2_SYNTHETIC_VISIBLE_MAX - 20) + "\n\n[panel truncated]" + : text; + await ctx.session?.synthetic?.({ sessionID: sid, text: V2_SYNTHETIC_TEXT, description, resume: false }); + } catch (err) { + console.error(`[bili-opencode] /acp render failed: ${err instanceof Error ? err.message : String(err)}`); + } + }, + }); + }); + if (commandReg) registrations.push(commandReg); + } catch (err) { + console.warn(`[bili-opencode] /acp command registration unavailable; continuing without it: ${err instanceof Error ? err.message : String(err)}`); + } + + const subscription = ctx.event?.subscribe?.({ signal: ac.signal }); + if (subscription && typeof subscription[Symbol.asyncIterator] === "function") { + void (async () => { + try { + for await (const evt of subscription) { + if (evt?.type !== "session.compaction.ended" || pluginDisabled()) continue; + const data = evt.data; + const cid = data && typeof data.sessionID === "string" ? data.sessionID : ""; + const base = state.proxyBase ?? proxyBaseFromEnv(); + if (!cid || !base) continue; + reportCompactionBoundary(base, cid).catch(() => {}); + } + } catch { + // subscription closed (abort on cleanup) + } + })(); + } + + return () => { + ac.abort(); + for (const r of registrations) { + try { + void r.dispose?.(); + } catch {} + } + state.proxyBase = undefined; + state.windows = undefined; + state.windowsAt = undefined; + }; + }; +} diff --git a/src/agent/opencode.ts b/src/agent/opencode.ts index 4e25efdf76..225fffb9d6 100644 --- a/src/agent/opencode.ts +++ b/src/agent/opencode.ts @@ -7,21 +7,28 @@ // - binds the opencode session id to the proxy session via the // pending-register queue (POST /__bili/plugin/register on session.created) // - renders the proxy's buildStatusPanel via an ignored chat message - -import { ACP_TOOLS_OPENAI, ABSORB_TOOL_OPENAI } from "../compress-tool.js"; -import { createAcpBridge, type AcpBridge, type AcpToolDef } from "./acp-bridge.js"; -import { BILI_PLUGIN_BYPASS_HEADER } from "../util.js"; -import { fetchProxyVersion, forwardTool, proxyBaseFromEnv, proxyBaseFromUrl, reportCompactionBoundary } from "./shared.js"; - -interface OpencodeCommandConfig { - template: string; - description?: string; -} - -interface OpencodeConfig { - command?: Record; - [key: string]: unknown; -} +// +// OpenCode 2.x (V2 plugin API) loads `{ id, setup }` instead of `.server()` — +// setup comes from the shared factory src/agent/opencode-v2.ts, which the +// native npm entry (opencode-native.ts) reuses so both deployment modes share +// one protocol implementation. The V2 runtime-API facts live in that file. +// +// The /acp command hooks themselves live in src/agent/opencode-acp-command.ts +// and are shared with the native V1 entry (opencode-native.ts). + +import { createAcpCommandHooks } from "./opencode-acp-command.js"; +import { createOpencodeV2Setup } from "./opencode-v2.js"; +import { installNativeFetchIntercept, type NativeInterceptState } from "./native-intercept.js"; + +export { showAcpText } from "./opencode-acp-command.js"; +export type { + OpencodeCommandConfig, + OpencodeConfig, + OpencodePromptPart, + OpencodeClient, + OpencodeCommandInput, + OpencodeAcpHooks, +} from "./opencode-acp-command.js"; interface OpencodeSessionInfo { id?: unknown; @@ -36,346 +43,34 @@ interface OpencodeEventInput { event?: OpencodeEvent; } -interface OpencodeCommandInput { - command: string; - sessionID: string; - arguments?: string; - [key: string]: unknown; -} - -interface OpencodePromptPart { - type: string; - text: string; - ignored?: boolean; -} - -interface OpencodeClient { - session?: { - prompt?: (args: { - path: { id: string }; - body: { noReply: boolean; parts: OpencodePromptPart[] }; - }) => Promise; - }; -} - interface OpencodePluginContext { - client?: OpencodeClient; - directory?: string; + client?: import("./opencode-acp-command.js").OpencodeClient; } interface OpencodeHooks { - config?: (input: OpencodeConfig) => Promise; + config?: (input: import("./opencode-acp-command.js").OpencodeConfig) => Promise; event?: (input: OpencodeEventInput) => Promise; - "command.execute.before"?: (input: OpencodeCommandInput, output: { parts: unknown[] }) => Promise; - tool?: Record; - "chat.headers"?: (input: { sessionID?: string }, output: { headers?: Record }) => Promise; - "experimental.chat.system.transform"?: (input: { sessionID?: string; [key: string]: unknown }, output: { system: unknown[] }) => void | Promise; - "experimental.chat.messages.transform"?: (input: Record, output: { messages: unknown[] }) => void | Promise; - "experimental.text.complete"?: (input: { sessionID?: string; [key: string]: unknown }, output: { text: string }) => void | Promise; + "command.execute.before"?: (input: import("./opencode-acp-command.js").OpencodeCommandInput, output: { parts: unknown[] }) => Promise; } const proxyBase = process.env.BILLION_CONTEXT_PROXY ?? ""; -async function showText(ctx: OpencodePluginContext, sid: string, text: string): Promise { - // Direct method call — `const p = ctx.client.session.prompt; p(...)` loses `this` (this._client) and throws. - const session = ctx.client?.session; - if (!session || typeof session.prompt !== "function") { - console.error("[bili-opencode] /acp render failed: session.prompt unavailable"); - return; - } - try { - await session.prompt({ - path: { id: sid }, - body: { noReply: true, parts: [{ type: "text", text, ignored: true }] }, - }); - } catch (err) { - console.error(`[bili-opencode] /acp render failed: ${err instanceof Error ? err.message : String(err)}`); - } -} - const server = async (ctx: OpencodePluginContext): Promise => { if (!proxyBase) return {}; console.log("[bili-opencode] plugin active (proxy " + proxyBase + ")"); - - // #920: legacy opencode-acp sessions keep their in-process machinery by - // importing the installed acp package as a library; null → current behavior. - let bridge: AcpBridge | null = null; - try { - bridge = await createAcpBridge({ directory: typeof ctx.directory === "string" ? ctx.directory : process.cwd() }); - } catch (err) { - console.error(`[bili-opencode] acp bridge init failed: ${err instanceof Error ? err.message : String(err)}`); - bridge = null; + // V1 host: same SDK-default-baseURL gap as the native entry — the + // launcher's config-file rewrite only catches explicit baseURLs. Global + // fetch patch catches the rest; `origin` is already resolved (the + // launcher owns the proxy lifecycle, no respawn hooks needed). + const intercept: NativeInterceptState = { origin: proxyBase, ready: Promise.resolve(proxyBase) }; + if (installNativeFetchIntercept(intercept)) { + console.log("[bili-opencode] v1: fetch patch installed (catches providers without an explicit baseURL)"); } - - const biliCommandBefore = async (input: OpencodeCommandInput): Promise => { - if (input.command !== "acp") return; - const sid = input.sessionID; - let text: string; - try { - const res = await fetch(`${proxyBase}/__bili/plugin/status?conversationId=${encodeURIComponent(sid)}&fallback=latest`); - const status = (await res.json()) as { ok?: boolean; panel?: string; error?: string }; - if (typeof status.panel === "string" && status.panel.length > 0) { - text = status.panel; - } else if (status.ok === false) { - // zero sessions on the proxy (fresh launch) — friendly idle notice - let version: string | undefined; - try { - version = await fetchProxyVersion(proxyBase); - } catch { - version = undefined; - } - text = version !== undefined - ? `billion-context@${version} — proxy connected, no ACP session yet. Send a model request, then run /acp again.` - : "bili: no ACP session yet (send a model request first, then run /acp)"; - } else { - text = "bili: proxy returned no status panel"; - } - } catch (err) { - text = `bili: /acp failed (${err instanceof Error ? err.message : String(err)})`; - } - await showText(ctx, sid, text); - throw new Error("__BILI_ACP_HANDLED__"); + return { + ...createAcpCommandHooks(() => proxyBase, ctx), }; - - const hooksOut: OpencodeHooks = { - config: async (opencodeConfig) => { - opencodeConfig.command ??= {}; - opencodeConfig.command["acp"] = { - template: "", - description: "Show ACP status (billion-context proxy)", - }; - // acp normally registers /dcp from its own config hook, which the - // bridge must not invoke — mirror it so legacy sessions keep the command. - if (bridge) { - opencodeConfig.command["dcp"] = { - template: "", - description: "Show DCP compression context (opencode-acp)", - }; - } - }, - "command.execute.before": biliCommandBefore, - }; - - if (bridge) { - Object.assign(hooksOut, bridge.wrapped); - hooksOut.tool = bridge.tools; - hooksOut["command.execute.before"] = async (input) => { - // acp first: on legacy sessions its handler throws its abort - // sentinel for /acp|/dcp; unhandled commands fall through below. - await bridge.wrapped["command.execute.before"](input, {}); - await biliCommandBefore(input); - }; - hooksOut["chat.headers"] = async (input, output) => { - if (typeof input?.sessionID === "string" && bridge.isLegacySession(input.sessionID)) { - if (!output.headers) output.headers = {}; - output.headers[BILI_PLUGIN_BYPASS_HEADER] = "1"; - } - }; - console.log(`[bili-opencode] acp bridge active (opencode-acp ${bridge.version || "?"}, legacy store ${bridge.storeDir})`); - } - return hooksOut; }; -// --------------------------------------------------------------------------- -// OpenCode 2.0 (V2 plugin API). Same protocol-client contract as V1 above: no -// acp-kernel, no compression logic — the proxy is the single compression -// authority. Structural types only (no @opencode/plugin import) so this file -// stays loadable under both host generations via the object export below -// (V2 validates `{ id, setup }`; V1 >= 1.18.29 calls `.server()`). -// -// Runtime API facts — VERSION-SPECIFIC (the 2.x plugin surface changes between -// builds; do not generalize beyond the build named): -// - next-17444 pre-release (probed live by the author): model.request registers -// but NEVER FIRES; http.request fires per outgoing provider request with a -// fetch Request at e.request (mutating e.request.headers reaches the wire); -// NO ctx.tool.reload(); command editor list/get/update/remove only (no ADD — -// hence no /acp under V2; acp_status tool is the in-host equivalent). -// - @opencode/cli 2.0.x stable (provenance confirmed during #754 review; -// probed live on 2.0.1 + 2.0.3): setup() is chosen over server(); BOTH -// model.request and http.request hooks fire (e.request mutation reaches the -// wire); ctx.tool = {reload, transform, hook} (reload EXISTS here); -// ctx.command.transform(editor.add) CAN add commands (TUI invocation needs -// Tab+Enter completion accept; `run` mode dispatches no slash commands at -// all); configured `plugin` entries must be DIRECTORIES (file paths are -// rejected with WARN "configured plugin path must be a directory"; the -// directory's index.js is the entrypoint) — the launcher wraps this single -// file accordingly (src/launcher.ts opencodeMajorVersion). End-to-end -// verified on 2.0.3: true plugin mode, native tools via the plugin tool -// endpoint, zero wire-level injection. -// - npm dev builds 2026-09-13 / 2026-09-14 (probed live during #754 review): -// first loads plugins via V1 server() only; second exposes setup() but has -// no ctx.session / ctx.tool at all. Adjacent dev builds disagree with each -// other and with both of the above. -// Consequence: every registration below uses optional chaining so the plugin -// is inert-safe on any surface; when no seam fires, sessions transparently run -// in proxy mode (wire-level tool injection) instead of failing. Tools stay -// registered synchronously from bundled schemas (exact parity with the proxy's -// openai tool list, src/compress-tool.ts) because reload-based refresh is not -// available on all observed surfaces. -// --------------------------------------------------------------------------- - -type V2Registration = { dispose?: () => void | Promise }; - -interface V2Headers { - set(name: string, value: string): void; -} - -interface V2HttpRequestEvent { - sessionID?: unknown; - model?: { providerID?: unknown; id?: unknown }; - request?: { url?: unknown; headers?: V2Headers | null }; -} - -interface V2ToolEditor { - add(tool: { - name: string; - description?: string; - input: unknown; - options?: { namespace?: string; permission?: string; codemode?: boolean; pinned?: boolean }; - execute: (input: Record, ctx: { sessionID: string }) => Promise<{ content: string }>; - }): void; -} - -interface V2CatalogModelEntry { - providerID?: unknown; - id?: unknown; - limit?: { context?: unknown }; -} - -interface V2PluginContext { - session?: { - hook?: (name: string, cb: (e: V2HttpRequestEvent) => void | Promise) => void | Promise; - }; - tool?: { - transform?: (cb: (editor: V2ToolEditor) => void) => void | Promise; - }; - event?: { subscribe?: (opts?: { signal?: AbortSignal }) => AsyncIterable<{ type?: unknown; data?: Record }> | undefined }; - catalog?: { model?: { list?: () => Promise<{ data?: V2CatalogModelEntry[] }> | undefined } | undefined }; -} - -const WINDOW_REFRESH_MS = 60000; - -// Kill switch = fully inert (same semantics as detectProxyBase): gates header stamping, tool forwarding, compaction reporting. -const pluginDisabled = (): boolean => process.env.BILLION_CONTEXT_PLUGIN === "0"; - -const V2_BILI_TOOLS = [...ACP_TOOLS_OPENAI, ABSORB_TOOL_OPENAI].map((t) => ({ - name: t.function.name, - description: t.function.description, - input: t.function.parameters, -})); - -interface V2State { - proxyBase?: string; - windows?: Map; - windowsAt?: number; -} - -function refreshWindows(ctx: V2PluginContext, state: V2State): void { - const now = Date.now(); - if (state.windows && state.windowsAt !== undefined && now - state.windowsAt < WINDOW_REFRESH_MS) return; - state.windowsAt = now; - void (async () => { - try { - const res = await ctx.catalog?.model?.list?.(); - const map = new Map(); - for (const m of res?.data ?? []) { - const pid = typeof m.providerID === "string" ? m.providerID : ""; - const id = typeof m.id === "string" ? m.id : ""; - const c = m.limit?.context; - if (pid && id && typeof c === "number" && Number.isFinite(c) && c > 0) map.set(`${pid}/${id}`, Math.floor(c)); - } - if (map.size > 0) state.windows = map; - } catch { - // catalog unavailable — window header simply goes unstamped - } - })(); -} - -const setup = async (ctx: V2PluginContext): Promise<() => void> => { - const ac = new AbortController(); - const state: V2State = {}; - const registrations: V2Registration[] = []; - - const stampHeaders = (e: V2HttpRequestEvent): void => { - const headers = e.request?.headers; - const sid = typeof e.sessionID === "string" ? e.sessionID : ""; - if (!headers || typeof headers.set !== "function" || !sid || !state.proxyBase) return; - headers.set("x-bili-plugin-conversation", sid); - headers.set("x-bili-plugin", "opencode"); - const model = e.model; - if (model && typeof model.providerID === "string" && typeof model.id === "string") { - const window = state.windows?.get(`${model.providerID}/${model.id}`); - if (window !== undefined) headers.set("x-bili-plugin-context-window", String(window)); - } - }; - - const httpRequestHook = async (e: V2HttpRequestEvent): Promise => { - if (pluginDisabled()) return; - const url = e.request?.url; - if (typeof url !== "string") return; - if (!state.proxyBase) { - state.proxyBase = proxyBaseFromUrl(url) ?? proxyBaseFromEnv(); - } - if (!state.proxyBase) return; - refreshWindows(ctx, state); - stampHeaders(e); - }; - - const hookReg = await ctx.session?.hook?.("http.request", httpRequestHook); - if (hookReg) registrations.push(hookReg); - - const toolReg = await ctx.tool?.transform?.((editor) => { - for (const t of V2_BILI_TOOLS) { - editor.add({ - name: t.name, - description: t.description, - input: t.input, - options: { codemode: false, permission: "allow" }, - execute: async (args, tctx) => { - if (pluginDisabled()) return { content: "bili: disabled (BILLION_CONTEXT_PLUGIN=0)" }; - const base = state.proxyBase ?? proxyBaseFromEnv(); - if (!base) return { content: "bili: no proxy detected (launch opencode through `bili opencode`, or point the provider baseURL at the bili proxy)" }; - try { - const result = await forwardTool(base, tctx.sessionID, t.name, args); - return { content: result }; - } catch (err) { - return { content: err instanceof Error ? err.message : String(err) }; - } - }, - }); - } - }); - if (toolReg) registrations.push(toolReg); - - const subscription = ctx.event?.subscribe?.({ signal: ac.signal }); - if (subscription && typeof subscription[Symbol.asyncIterator] === "function") { - void (async () => { - try { - for await (const evt of subscription) { - if (evt?.type !== "session.compaction.ended" || pluginDisabled()) continue; - const data = evt.data; - const cid = data && typeof data.sessionID === "string" ? data.sessionID : ""; - const base = state.proxyBase ?? proxyBaseFromEnv(); - if (!cid || !base) continue; - reportCompactionBoundary(base, cid).catch(() => {}); - } - } catch { - // subscription closed (abort on cleanup) - } - })(); - } - - return () => { - ac.abort(); - for (const r of registrations) { - try { - void r.dispose?.(); - } catch {} - } - state.proxyBase = undefined; - state.windows = undefined; - state.windowsAt = undefined; - }; -}; +const setup = createOpencodeV2Setup(); export default { id: "billion-context-opencode", setup, server }; diff --git a/src/agent/pi-native.ts b/src/agent/pi-native.ts new file mode 100644 index 0000000000..0359c86488 --- /dev/null +++ b/src/agent/pi-native.ts @@ -0,0 +1,76 @@ +// Native pi mode (#519): the package-installed pi extension bootstraps its +// own proxy — bare `pi` (with this package installed via pi's package +// manager or `bili plugin install pi`) gets full plugin-mode compression +// with NO launcher. Flow at extension load: +// 1. spawn the package's own proxy (`dist/index.js start`, ephemeral +// port, parent-pid watchdog = this pi process) via ensureProxyRunning — +// a healthy compatible instance is ATTACHED, not doubled; +// 2. patch globalThis.fetch (native-intercept.ts) so model-API requests +// are rewritten to `/bili/`; +// 3. set BILLION_CONTEXT_PROXY so the shared plugin (pi.ts) detects the +// proxy through its existing env fallback — tools, headers, /acp and +// the compaction cancel all reuse the launcher-mode code paths. +// Skipped when a proxy is already managed (BILLION_CONTEXT_PROXY set by a +// `bili` MITM launch, or BILI_PROVIDER_REWRITES set by a `bili` /bili/ +// launch) or opted out (BILI_NATIVE_PI=0). + +import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST } from "../launcher.js"; +import { createBiliPlugin } from "./pi.js"; +import { markNativeHost, nativeBootstrapGate, nativeProxyScriptPath, singleFlight } from "./native-bootstrap.js"; +import { installNativeFetchIntercept, type NativeInterceptState } from "./native-intercept.js"; +import { fetchStatus } from "./shared.js"; + +// Shared plumbing lives in native-bootstrap.ts (side-effect-free — importing +// pi-native.ts from another host entry must not run pi's bootstrap). +export { nativeProxyScriptPath, singleFlight } from "./native-bootstrap.js"; + +/** Decides whether the native bootstrap should run in this process. */ +export function shouldBootstrapNative(env: NodeJS.ProcessEnv): boolean { + return nativeBootstrapGate(env, "BILI_NATIVE_PI"); +} + +function errMessage(err: unknown): string { + return err instanceof Error ? err.message : String(err); +} + +const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined) }; + +async function bootstrap(): Promise { + try { + const handle = await ensureProxyRunning( + { host: LAUNCHER_DEFAULT_HOST, port: 0, passthrough: false, debug: false }, + { scriptPath: nativeProxyScriptPath() }, + ); + const origin = handle.origin; + state.origin = origin; + process.env.BILLION_CONTEXT_PROXY = origin; + return origin; + } catch (err) { + console.error(`bili-native: proxy bootstrap failed — model traffic goes direct (uncompressed): ${errMessage(err)}`); + return undefined; + } +} + +const nativeActive = shouldBootstrapNative(process.env); +if (nativeActive) markNativeHost(process.env, "pi"); + +// node:test imports this module for shouldBootstrapNative/nativeProxyScriptPath — +// never bootstrap a real proxy from inside a test run. +if (process.env.NODE_TEST_CONTEXT === undefined && nativeActive) { + const start = singleFlight(bootstrap); + state.respawn = start; + state.onGiveUp = () => { + // We wrote BILLION_CONTEXT_PROXY at successful bootstrap. If the proxy + // dies mid-session and the respawn fails, traffic goes direct — clear + // the env so event-time ownership checks (session_before_compact + // cancel, header stamping) stop claiming compression ownership and + // native compaction comes back with the direct traffic. + delete process.env.BILLION_CONTEXT_PROXY; + }; + state.ready = start(); + installNativeFetchIntercept(state); +} + +export default createBiliPlugin(); + +export { fetchStatus } from "./pi.js"; diff --git a/src/agent/pi.ts b/src/agent/pi.ts index a148386b3a..cb2807809d 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -59,8 +59,8 @@ function agentName(override: string | undefined): string { return process.env.BILLION_CONTEXT_PLUGIN_AGENT === "omp" ? "omp" : "pi"; } -function proxyBaseForCtx(ctx: Ctx): string | undefined { - return detectProxyBase(ctx.model?.baseUrl); +function proxyBaseForCtx(ctx: Ctx | undefined): string | undefined { + return detectProxyBase(ctx?.model?.baseUrl); } function sessionIdOf(ctx: Ctx): string | undefined { @@ -275,23 +275,28 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM } // #535: cancel the host's NATIVE compaction so its summarizer never // fires alongside bili's ACP compression — the in-extension - // replacement for the old compaction-off config injection. Only - // armed under `bili` launch: plain pi/omp with the plugin installed - // stays fully native. - // pi: the event carries `reason`; cancel only threshold + overflow so - // manual /compact stays user-owned. - // omp (#851): session_before_compact carries NO reason field, so at - // hook level manual compaction (/compact, plan-mode "Approve and - // compact context") is indistinguishable from auto — but every auto - // pass announces itself first via auto_compaction_start (reason - // threshold|overflow|idle|incomplete), which omp emits (awaited) - // before the hook fires; manual paths never do. Track the - // announcement: announced passes stay cancelled, unannounced ones - // are left user-owned. A surviving native compaction is safe: the - // proxy archives the unreachable blocks on session_compact (#395). - if ((agent === "pi" || agent === "omp") && process.env.BILLION_CONTEXT_PROXY !== undefined) { + // replacement for the old compaction-off config injection. pi's event + // carries `reason`: cancel only threshold + overflow so manual + // /compact stays user-owned. omp (#851): session_before_compact + // carries NO reason field, so at hook level manual compaction + // (/compact, plan-mode "Approve and compact context") is + // indistinguishable from auto — but every auto pass announces itself + // first via auto_compaction_start (reason threshold|overflow|idle| + // incomplete), which omp emits (awaited) before the hook fires; + // manual paths never do. Track the announcement: announced passes + // stay cancelled, unannounced ones are left user-owned. A surviving + // native compaction is safe: the proxy archives the unreachable + // blocks on session_compact (#395). + // Whether we own compression is decided at EVENT time, not load time: + // in native mode (#519) the proxy origin lands in + // BILLION_CONTEXT_PROXY only after the async bootstrap finishes, so a + // load-time check would leave the cancel disarmed for the whole + // session. Plain pi/omp with the plugin installed but NO reachable + // proxy (incl. a failed bootstrap) stays fully native. + if (agent === "pi" || agent === "omp") { if (agent === "pi") { - pi.on("session_before_compact", (event) => { + pi.on("session_before_compact", (event, ctx) => { + if (proxyBaseForCtx(ctx) === undefined) return undefined; const reason = (event as unknown as { reason?: unknown }).reason; if (reason === "threshold" || reason === "overflow") return { cancel: true }; return undefined; @@ -304,7 +309,8 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM pi.on("auto_compaction_end", () => { autoPending = false; }); - pi.on("session_before_compact", () => { + pi.on("session_before_compact", (event, ctx) => { + if (proxyBaseForCtx(ctx) === undefined) return undefined; if (!autoPending) return undefined; autoPending = false; return { cancel: true }; diff --git a/src/agent/shared.ts b/src/agent/shared.ts index c6bcfc99fb..6b8a2de12f 100644 --- a/src/agent/shared.ts +++ b/src/agent/shared.ts @@ -156,3 +156,7 @@ export async function fetchProxyVersion(proxyBase: string): Promise 0 ? version : undefined; } + +// Model-facing body for /acp status renders: the visible panel goes to the synthetic message's +// description (TUI Notice cap ~1KB); this inert one-liner keeps the model context clean. +export const V2_SYNTHETIC_TEXT = "[bili-acp-status] ACP status panel was displayed in your terminal; this line is not an instruction."; diff --git a/src/launcher.ts b/src/launcher.ts index e1acc2b797..3463d21faf 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -173,6 +173,14 @@ export interface LauncherDeps { spawnImpl?: SpawnFn; now?: () => number; sleep?: (ms: number) => Promise; + /** #519 native mode: the proxy entry script to spawn. The launcher + * default (process.argv[1]) is WRONG inside a host process like pi — + * pi-native.ts passes its own package's dist/index.js instead. */ + scriptPath?: string; + /** #819: explicit Node executable for spawning the proxy. Inside a host + * process (opencode/pi native binary) process.execPath is NOT Node; + * defaults to resolveNodeRuntime(). */ + nodeRuntime?: string; } export function isLaunchClient(value: string): value is ClientName { @@ -1987,6 +1995,40 @@ function proxyStartArgs(opts: LaunchOptions): string[] { return args; } +/** #819: resolve the executable that runs the proxy entry script. In a plain + * Node CLI, process.execPath is correct; inside a host process (the opencode + * or pi native binary) it is the HOST executable — spawning it with a .js + * argv passes the script to the wrong program. A live Node always wins, then + * an explicit BILLION_CONTEXT_NODE override, then a PATH search. */ +export function resolveNodeRuntime( + execPath: string = process.execPath, + env: NodeJS.ProcessEnv = process.env, + platform: NodeJS.Platform = process.platform, + existsImpl: (p: string) => boolean = fs.existsSync, +): string { + const base = path.basename(execPath).toLowerCase(); + if (base === "node" || base === "node.exe") return execPath; + const override = typeof env.BILLION_CONTEXT_NODE === "string" ? env.BILLION_CONTEXT_NODE.trim() : ""; + if (override.length > 0 && existsImpl(override)) return override; + // join with the SIMULATED platform's separators: a posix-style PATH on + // win32 (and vice versa) must not be normalized through the host's + // path.join, or the candidates no longer match what existsImpl expects. + const sep = platform === "win32" ? ";" : ":"; + const names = platform === "win32" ? ["node.exe"] : ["node"]; + for (const dir of (env.PATH ?? "").split(sep)) { + if (!dir) continue; + for (const name of names) { + // separator-preserving concatenation: never normalize — win32 + // accepts forward slashes, and normalizing through path.join + // would rewrite a posix-style entry on a win32 host (or the + // reverse), missing the file existsImpl would find. + const candidate = dir.endsWith("/") || dir.endsWith("\\") ? dir + name : dir + "/" + name; + if (existsImpl(candidate)) return candidate; + } + } + throw new Error("bili: cannot find a Node runtime to spawn the proxy (this process is not Node) — set BILLION_CONTEXT_NODE"); +} + export async function ensureProxyRunning( opts: LaunchOptions, deps: LauncherDeps = {}, @@ -2042,7 +2084,7 @@ export async function ensureProxyRunning( // pointed there only ever reach an explicitly-started `bili start`. // The child's EADDRINUSE retry covers the pick/spawn race. const port = opts.port > 0 ? opts.port : await pickEphemeralPort(opts.host); - const script = process.argv[1]; + const script = deps.scriptPath ?? process.argv[1]; if (!script) throw new Error("bili: cannot resolve launcher script path"); const logPath = path.join(os.tmpdir(), `bili-proxy-${port}.log`); const logFd = fs.openSync(logPath, "a"); @@ -2076,7 +2118,7 @@ export async function ensureProxyRunning( let child: SpawnChild; try { child = spawnImpl( - process.execPath, + deps.nodeRuntime ?? resolveNodeRuntime(), [script, ...proxyStartArgs({ ...opts, port })], { detached: true, @@ -2107,16 +2149,23 @@ export async function ensureProxyRunning( // healthy — otherwise a startup crash (bad config, missing upstream, …) // burns the whole SPAWN_WAIT_MS poll window before erroring. let childExit: { code: number | null; signal: string | null } | undefined; + // #809/D: an async spawn failure (EACCES/ENOENT on the resolved runtime) + // emits 'error', not 'exit'. Unhandled, it becomes an uncaughtException + // that kills the host process; capture it so we fail fast with the cause. + let childError: unknown; child.on?.("exit", (...rest: unknown[]) => { childExit = { code: typeof rest[0] === "number" ? rest[0] : null, signal: typeof rest[1] === "string" ? rest[1] : null, }; }); + child.on?.("error", (...rest: unknown[]) => { + childError = rest[0]; + }); const deadline = now() + SPAWN_WAIT_MS; while (now() < deadline) { - if (childExit) break; + if (childExit || childError !== undefined) break; await sleepImpl(HEALTH_POLL_INTERVAL_MS); const inst = readInstance(); if (isProxyInstanceFile(inst) && inst.launchToken === launchToken) { @@ -2135,6 +2184,10 @@ export async function ensureProxyRunning( return { origin: proxyOrigin(opts.host, port), port, child, logPath }; } } + if (childError !== undefined) { + const detail = childError instanceof Error ? childError.message : String(childError); + throw new Error(`bili: proxy spawn failed (${detail}) (log: ${logPath})`); + } if (childExit) { const detail = childExit.code !== null ? `code ${childExit.code}` diff --git a/src/plugin-install.ts b/src/plugin-install.ts index 8855fea617..a53753f6b7 100644 --- a/src/plugin-install.ts +++ b/src/plugin-install.ts @@ -7,7 +7,8 @@ // omp ~/.omp/agent/config.yml extensions: [/dist/agent/omp.js] // claude `claude mcp add` (user scope; writes ~/.claude.json) // codex ~/.codex/config.toml [mcp_servers.bili] -// opencode ~/.config/opencode/opencode.json mcp.bili +// opencode /opencode.json{c}|config.json (highest-precedence existing; #927) +// mcp.bili + native plugin dir + compaction.auto=false // Installers throw on failure (bad/locked config, missing host CLI); the CLI // layer catches, prints `bili plugin: ` and exits 1. @@ -16,6 +17,7 @@ import path from "node:path"; import os from "node:os"; import { execFileSync } from "node:child_process"; import { fileURLToPath } from "node:url"; +import { applyEdits, modify as jsoncModify, parse as jsoncParse, type ParseError } from "jsonc-parser"; import { resolvePiHome } from "./client-config.js"; import { isPidAlive, isProxyInstanceFile, readProxyInstanceFile } from "./instance.js"; @@ -427,46 +429,337 @@ function codexStatus(): string { // — opencode ———————————————————————————————————————————————————————————— -function opencodeJson(): string { +// #927: OpenCode reads BOTH `plugin` (1.x canonical) and `plugins` (2.x +// canonical): 2.x merges the two arrays, while 1.x hard-errors on `plugins` +// (ConfigInvalidError: unrecognized_keys). Dual-writing is off the table — +// pick ONE effective key by host major version. A failed version probe falls +// back to major 1, whose key (`plugin`) 2.x still honors as a legacy alias, +// so degradation can never write a dead config line. +export function pickPluginKey(major: number): "plugin" | "plugins" { + return major >= 2 ? "plugins" : "plugin"; +} + +const PLUGIN_KEYS = ["plugin", "plugins"] as const; + +const ocMajorCache = new Map(); + +/** Host major version via ` --version` (probe failure → 1). Same + * contract as the launcher's probe; honors the BILI_CLIENT_BIN override. */ +export function detectOpencodeMajor(): number { + const command = process.env.BILI_CLIENT_BIN?.trim() || "opencode"; + const hit = ocMajorCache.get(command); + if (hit !== undefined) return hit; + let major = 1; + try { + // Windows: npm global shims are .cmd/.bat — execFileSync cannot spawn + // those directly (EINVAL), so route through the shell with explicit + // quoting. Plain exes and POSIX shebang scripts go straight through. + const viaShell = process.platform === "win32" && /\.(cmd|bat)$/i.test(command); + const out = viaShell + ? execFileSync(`"${command}" --version`, { shell: true, timeout: 5000, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }) + : execFileSync(command, ["--version"], { timeout: 5000, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }); + const m = /(\d+)\s*\./.exec(out); + if (m) major = parseInt(m[1], 10); + } catch {} + ocMajorCache.set(command, major); + return major; +} + +function pluginEntries(data: Record, key: string): string[] { + const v = data[key]; + if (Array.isArray(v)) return v.filter((x): x is string => typeof x === "string"); + if (v !== null && typeof v === "object") return Object.keys(v as Record); + return []; +} + +// #927: write target = explicit OPENCODE_CONFIG override, else the +// highest-precedence file ALREADY PRESENT in the config dir (opencode merges +// config.json -> opencode.json -> opencode.jsonc, later wins), else create +// opencode.json. Never spawn a second file next to an existing config — that +// splits one logical config across files (#927). +export function opencodeTargetFile(): string { const raw = process.env.OPENCODE_CONFIG?.trim(); if (raw && raw.length > 0) return raw; const xdg = process.env.XDG_CONFIG_HOME?.trim(); - if (xdg && xdg.length > 0) return path.join(xdg, "opencode/opencode.json"); - return path.join(os.homedir(), ".config", "opencode", "opencode.json"); + const dir = xdg && xdg.length > 0 ? path.join(xdg, "opencode") : path.join(os.homedir(), ".config", "opencode"); + for (const name of ["opencode.jsonc", "opencode.json", "config.json"]) { + const p = path.join(dir, name); + if (fs.existsSync(p)) return p; + } + return path.join(dir, "opencode.json"); +} + +// #820 native mode: OpenCode 2.x rejects bare FILE paths in `plugin` — the +// entry must be a DIRECTORY whose index.js is the entrypoint (#754 probe). The +// wrapper directory lives next to the config so it survives config moves. +function opencodePluginDir(configFile: string): string { + return path.join(path.dirname(configFile), "plugins", "billion-context"); } +// #809/N4: opencode.json may carry a non-object `mcp` (e.g. a bare string); +// `"bili" in ` throws TypeError. Guard so install/remove/status +// degrade gracefully instead of crashing (a crashing remove leaves no cleanup). function isPlainMcpObject(v: unknown): v is Record { return v !== null && typeof v === "object" && !Array.isArray(v); } +function readOriginalText(file: string): string { + try { + return fs.readFileSync(file, "utf8"); + } catch (err) { + if ((err as { code?: string }).code === "ENOENT") return ""; + throw err; + } +} + +// JSONC-tolerant parse: the target may be an opencode.jsonc carrying comments +// (and trailing commas) — strict JSON.parse would refuse it outright, and a +// stringify round-trip would wash the comments away on write-back. Tolerance +// is bounded: ANY diagnostic beyond comments/trailing commas means the file +// is broken, and a broken config is never overwritten (§7.3). +function parseOpencodeConfig(text: string, file: string): Record { + const errors: ParseError[] = []; + const parsed = jsoncParse(text, errors, { allowTrailingComma: true }); + if (errors.length > 0 || parsed === undefined) { + const first = errors[0]; + const detail = first ? ` (offset ${first.offset})` : ""; + throw new Error(`${file}: not valid JSON${detail} — fix it or restore ${path.basename(file)}.bili-bak first; refusing to overwrite`); + } + if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) { + throw new Error(`${file}: expected a JSON object at top level, refusing to overwrite`); + } + return parsed as Record; +} + +function loadOpencodeConfig(file: string): { original: string; data: Record } { + const original = readOriginalText(file); + return { original, data: original === "" ? {} : parseOpencodeConfig(original, file) }; +} + +// Write back preserving everything bili did not touch: a missing or plain-JSON +// target is serialized whole (the historical behavior); a JSONC target gets +// surgical top-level-key edits so comments and formatting elsewhere survive +// byte-for-byte (#927). A no-op run (nothing touched) leaves the file alone. +function writeOpencodeConfig(file: string, original: string, data: Record, touched: ReadonlySet): void { + if (touched.size === 0) return; + fs.mkdirSync(path.dirname(file), { recursive: true }); + backupOnce(file); + if (original === "") { + fs.writeFileSync(file, JSON.stringify(data, null, 2) + "\n"); + return; + } + let strict = false; + try { + JSON.parse(original); + strict = true; + } catch {} + if (strict) { + fs.writeFileSync(file, JSON.stringify(data, null, 2) + "\n"); + return; + } + let text = original; + for (const key of touched) { + text = applyEdits(text, jsoncModify(text, [key], data[key], { formattingOptions: { tabSize: 2, insertSpaces: true } })); + } + fs.writeFileSync(file, text); +} + +// Legacy opencode-acp plugin entries — both config shapes v1 accepts: +// array: "opencode-acp" | "npm:opencode-acp" | "/opencode-acp[/index.js]" | "opencode-acp@" +// object: { "opencode-acp": "stable", ... } +// The in-process extension and the native plugin are two compression owners — +// keeping both armed means double compression / ref-space fights (#918). +// #927: strip from BOTH key spellings — a 2.x host may carry acp entries under +// `plugins`, an older v1 install under `plugin`. +function isLegacyOpencodeAcpEntry(entry: string): boolean { + if (entry === "opencode-acp" || entry.startsWith("opencode-acp@")) return true; + if (entry.startsWith("npm:")) { + const rest = entry.slice(4); + return rest === "opencode-acp" || rest.startsWith("opencode-acp@"); + } + const normalized = entry.replace(/\\/g, "/"); + const segments = normalized.split("/").filter((s) => s.length > 0); + // Any path segment naming the package identifies an acp install — covers + // dir specs, /index.js, and deeper entry paths like dist/index.js. + return segments.some((s) => s === "opencode-acp" || s.startsWith("opencode-acp@")); +} + +function stripLegacyOpencodeAcp(data: Record, notes: string[], touched: Set): void { + const removed: string[] = []; + for (const key of PLUGIN_KEYS) { + const v = data[key]; + let dropped = 0; + if (Array.isArray(v)) { + const survivors: unknown[] = []; + for (const entry of v) { + if (typeof entry === "string" && isLegacyOpencodeAcpEntry(entry)) { + removed.push(entry); + dropped++; + continue; + } + survivors.push(entry); + } + if (dropped > 0) { + if (survivors.length === 0) delete data[key]; + else data[key] = survivors; + touched.add(key); + } + } else if (v !== null && typeof v === "object") { + const map = v as Record; + for (const k of Object.keys(map)) { + if (isLegacyOpencodeAcpEntry(k)) { + delete map[k]; + removed.push(k); + dropped++; + } + } + if (dropped > 0) { + if (Object.keys(map).length === 0) delete data[key]; + touched.add(key); + } + } + } + if (removed.length > 0) { + notes.push(`replaced opencode-acp plugin entries (${removed.join(", ")}) — single compression owner; restore from the .bili-bak backup if that was intended`); + notes.push("also check /.opencode/opencode.json — a LOCAL-scope opencode-acp install (opencode plugin opencode-acp) lives there, not in this global config"); + } +} + function opencodeInstall(): string { - const file = opencodeJson(); - const mcpJs = path.join(selfPackageRoot(), "dist", "mcp.js"); - requireDistFile(mcpJs); - const data = readJson(file); - if (data.mcp != null && !isPlainMcpObject(data.mcp)) return `opencode: mcp.bili skipped ("mcp" is not an object) -> ${file}`; - const mcp = (data.mcp as Record | undefined) ?? {}; - if ("bili" in mcp) return `opencode: already installed (${file})`; - mcp.bili = { type: "local", command: [process.execPath, mcpJs], environment: { BILI_MCP_PROXY: proxyOriginForInstall() }, enabled: true }; - data.mcp = mcp; - writeJson(file, data); - return `opencode: installed -> ${file} mcp.bili`; + const file = opencodeTargetFile(); + const { original, data } = loadOpencodeConfig(file); + const notes: string[] = []; + const touched = new Set(); + + // MCP shell is optional: the native plugin below self-spawns a proxy, so a + // missing live origin skips the shell instead of failing the install. + try { + const mcpJs = path.join(selfPackageRoot(), "dist", "mcp.js"); + requireDistFile(mcpJs); + const rawMcp = data.mcp; + if (rawMcp != null && !isPlainMcpObject(rawMcp)) { + notes.push('mcp.bili skipped ("mcp" is not an object)'); + } else { + const mcp = (rawMcp as Record | undefined) ?? {}; + if ("bili" in mcp) notes.push("mcp.bili present"); + else { + mcp.bili = { type: "local", command: [process.execPath, mcpJs], environment: { BILI_MCP_PROXY: proxyOriginForInstall() }, enabled: true }; + data.mcp = mcp; + touched.add("mcp"); + notes.push("mcp.bili written"); + } + } + } catch (err) { + notes.push(`mcp.bili skipped (${err instanceof Error ? err.message : String(err)})`); + } + + // Native plugin (#820): self-spawned proxy + http.request URL rewrite. + const agentJs = path.join(selfPackageRoot(), "dist", "agent", "opencode-native.js"); + requireDistFile(agentJs); + // Single compression owner FIRST: drop any opencode-acp entry before + // adding ours, so both never load armed in one host (#918). The write + // below snapshots the original config to .bili-bak (first write only). + stripLegacyOpencodeAcp(data, notes, touched); + const dir = opencodePluginDir(file); + fs.mkdirSync(dir, { recursive: true }); + fs.writeFileSync(path.join(dir, "index.js"), `export { default } from ${JSON.stringify(agentJs)};\n`); + // #927: the single effective key for this host — see pickPluginKey. + const key = pickPluginKey(detectOpencodeMajor()); + const existing = pluginEntries(data, key); + if (!existing.includes(dir)) { + data[key] = [...existing, dir]; + touched.add(key); + notes.push(`${key} -> ${dir}`); + } else { + notes.push(`${key} present`); + } + + // Single compression owner: with the native plugin installed, ACP owns + // compression — disable host auto-compaction (merge-preserving; the key is + // ignored on OpenCode 1.x). Restored from the pre-install backup on + // remove. #927: skip when the target already says auto:false — the user + // wrote it themselves, so one file carries the fact exactly once. + const curCompaction = data.compaction; + const curObj = curCompaction !== null && typeof curCompaction === "object" && !Array.isArray(curCompaction) ? curCompaction as Record : undefined; + if (curObj?.auto === false) { + notes.push("compaction.auto already disabled"); + } else { + data.compaction = { ...(curObj ?? {}), auto: false }; + touched.add("compaction"); + notes.push("compaction.auto set to false"); + } + + writeOpencodeConfig(file, original, data, touched); + return `opencode: installed -> ${file} (${notes.join("; ")})`; } function opencodeRemove(): string { - const file = opencodeJson(); - const data = readJson(file); + const file = opencodeTargetFile(); + const { original, data } = loadOpencodeConfig(file); + const notes: string[] = []; + const touched = new Set(); + const mcp = data.mcp; - if (!isPlainMcpObject(mcp) || !("bili" in mcp)) return `opencode: not installed (${file})`; - delete mcp.bili; - if (Object.keys(mcp).length === 0) delete data.mcp; - writeJson(file, data); - return `opencode: removed from ${file}`; + if (isPlainMcpObject(mcp) && "bili" in mcp) { + delete mcp.bili; + if (Object.keys(mcp).length === 0) delete data.mcp; + touched.add("mcp"); + notes.push("mcp.bili removed"); + } + + // #927: clean our entry out of whichever key spelling carries it. + const dir = opencodePluginDir(file); + let dirRemoved = false; + for (const key of PLUGIN_KEYS) { + const entries = pluginEntries(data, key); + if (!entries.includes(dir)) continue; + const remaining = entries.filter((p) => p !== dir); + if (remaining.length === 0) delete data[key]; + else data[key] = remaining; + touched.add(key); + dirRemoved = true; + } + if (dirRemoved) { + fs.rmSync(dir, { recursive: true, force: true }); + notes.push(`plugin dir removed (${dir})`); + } + + if (notes.length > 0) { + const cur = data.compaction as Record | undefined; + if (cur && cur.auto === false) { + const bak = `${file}.bili-bak`; + if (fs.existsSync(bak)) { + try { + const bakData = loadOpencodeConfig(bak).data; + if (bakData.compaction === undefined) delete data.compaction; + else data.compaction = bakData.compaction; + touched.add("compaction"); + notes.push("compaction restored from backup"); + } catch { + // unreadable backup — leave current settings untouched + } + } else if (Object.keys(cur).length === 1) { + // no pre-install config existed, so our {auto:false} is the entire + // key — dropping it restores the pre-install state exactly; with + // extra keys present we can't tell user edits apart, so leave them + delete data.compaction; + touched.add("compaction"); + notes.push("compaction removed (no prior config)"); + } + } + } + + writeOpencodeConfig(file, original, data, touched); + return notes.length > 0 ? `opencode: removed from ${file} (${notes.join("; ")})` : `opencode: not installed (${file})`; } function opencodeStatus(): string { - const mcp = readJson(opencodeJson()).mcp; - return isPlainMcpObject(mcp) && "bili" in mcp ? "installed" : "not installed"; + const file = opencodeTargetFile(); + const { data } = loadOpencodeConfig(file); + const mcp = data.mcp; + const dir = opencodePluginDir(file); + const listed = PLUGIN_KEYS.some((k) => pluginEntries(data, k).includes(dir)); + return (isPlainMcpObject(mcp) && "bili" in mcp) || listed ? "installed" : "not installed"; } // — dispatch ———————————————————————————————————————————————————————————— diff --git a/src/plugin.ts b/src/plugin.ts index 2d90cab881..d3d2d51d26 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -50,6 +50,11 @@ const PROXY_VERSION = (() => { export const PLUGIN_AGENT_HEADER = "x-bili-plugin"; export const PLUGIN_CONVERSATION_HEADER = "x-bili-plugin-conversation"; +/** #920: legacy-lane marker. Set by the absorbed opencode-acp wrapper for + * sessions that still run through the legacy DCP machinery — the proxy + * forwards such requests VERBATIM (no wire injection, no session binding, + * no compress loop): the legacy extension owns compression for them. */ +export const PLUGIN_BYPASS_HEADER = "x-bili-plugin-bypass"; export const PLUGIN_CONTEXT_WINDOW_HEADER = "x-bili-plugin-context-window"; export const PLUGIN_PROTOCOL_VERSION = 1; @@ -79,6 +84,10 @@ export function pluginConversationHeader(headers: Record): string | undefined { + return headerValue(headers, PLUGIN_BYPASS_HEADER); +} + /** The plugin reports its agent's own model context window (what the agent * configured, e.g. a pinned/overridden contextWindow). It replaces the * native-window source (built-in table / models.dev registry) in the config diff --git a/tests/acp-bridge.test.ts b/tests/acp-bridge.test.ts deleted file mode 100644 index 9777d34b64..0000000000 --- a/tests/acp-bridge.test.ts +++ /dev/null @@ -1,289 +0,0 @@ -// #920: acp bridge — package probing, store-dir mirroring, hook gating, -// graceful degradation. Hermetic: fake package trees under os.tmpdir(), -// XDG_* env vars saved/restored per test. - -import * as fs from "node:fs"; -import * as os from "node:os"; -import path from "node:path"; -import { test } from "node:test"; -import assert from "node:assert/strict"; -import { acpStoreDir, createAcpBridge, resolveAcpPackage } from "../src/agent/acp-bridge.js"; - -const ENV_KEYS = ["XDG_DATA_HOME", "XDG_CONFIG_HOME", "XDG_CACHE_HOME", "BILI_OPENCODE_ACP_SPEC", "OPENCODE_CONFIG_DIR", "BILLION_CONTEXT_PROXY"] as const; - -function withEnv(overrides: Record, fn: () => void | Promise): void | Promise { - const saved: Record = {}; - for (const key of ENV_KEYS) saved[key] = process.env[key]; - for (const [key, value] of Object.entries(overrides)) { - if (value === undefined) delete process.env[key]; - else process.env[key] = value; - } - const restore = (): void => { - for (const key of ENV_KEYS) { - if (saved[key] === undefined) delete process.env[key]; - else process.env[key] = saved[key]; - } - }; - return Promise.resolve(fn()).finally(restore); -} - -function makePkg(base: string, version: string, opts?: { dist?: boolean; body?: string }): string { - const pkgPath = path.join(base, "opencode-acp"); - fs.mkdirSync(path.join(pkgPath, "dist"), { recursive: true }); - fs.writeFileSync(path.join(pkgPath, "package.json"), JSON.stringify({ name: "opencode-acp", version })); - if (opts?.dist !== false) { - fs.writeFileSync(path.join(pkgPath, "dist", "index.js"), opts?.body ?? "export default async () => ({});"); - } - return pkgPath; -} - -const FAKE_ACP_BODY = ` -globalThis.__acp = globalThis.__acp || {}; -const r = globalThis.__acp; -r.initProxyEnv = process.env.BILLION_CONTEXT_PROXY ?? null; -export default async (ctx) => { - r.initDirectory = ctx.directory; - const rec = (key) => (...args) => { r[key] = args; }; - return { - "experimental.chat.system.transform": rec("system"), - "experimental.chat.messages.transform": rec("messages"), - "experimental.text.complete": rec("textComplete"), - "command.execute.before": rec("commandBefore"), - event: rec("event"), - config: rec("config"), - tool: { - compress: { description: "dcp compress", execute: rec("toolCompress") }, - decompress: { description: "dcp decompress", execute: rec("toolDecompress") }, - }, - }; -}; -`; - -function userMsg(sessionID: string, opts?: { id?: string; parts?: unknown[] }): Record { - return { - info: { role: "user", id: opts?.id ?? "msg_real_1", sessionID }, - parts: opts?.parts ?? [{ type: "text", text: "hi" }], - }; -} - -test("resolveAcpPackage: probe order and validity rules", () => withEnv({ XDG_CACHE_HOME: undefined, XDG_CONFIG_HOME: undefined, BILI_OPENCODE_ACP_SPEC: undefined }, () => { - const base = fs.mkdtempSync(path.join(os.tmpdir(), "acp-resolve-")); - try { - const cache = path.join(base, "cache"); - const config = path.join(base, "config"); - const proj = path.join(base, "proj"); - process.env.XDG_CACHE_HOME = cache; - process.env.XDG_CONFIG_HOME = config; - makePkg(path.join(cache, "opencode", "packages", "opencode-acp@latest", "node_modules"), "1.2.0"); - makePkg(path.join(proj, "node_modules"), "1.5.0"); - makePkg(path.join(config, "opencode", "node_modules"), "1.1.0"); - // highest version wins across probes - assert.equal(resolveAcpPackage(proj), path.join(proj, "node_modules", "opencode-acp")); - // launcher-stripped spec is authoritative even over a newer copy - process.env.BILI_OPENCODE_ACP_SPEC = "opencode-acp@latest"; - assert.equal(resolveAcpPackage(proj), path.join(cache, "opencode", "packages", "opencode-acp@latest", "node_modules", "opencode-acp")); - // broken spec'd slot (major 2) falls through to generic probes - const brokenSlot = makePkg(path.join(cache, "opencode", "packages", "opencode-acp-broken", "node_modules"), "1.0.0"); - fs.writeFileSync(path.join(brokenSlot, "package.json"), JSON.stringify({ name: "opencode-acp", version: "2.0.0" })); - process.env.BILI_OPENCODE_ACP_SPEC = "opencode-acp-broken"; - assert.equal(resolveAcpPackage(proj), path.join(proj, "node_modules", "opencode-acp")); - } finally { - fs.rmSync(base, { recursive: true, force: true }); - } -})); - -test("resolveAcpPackage: newest valid cache slot; invalid copies skipped", () => withEnv({ XDG_CACHE_HOME: undefined, XDG_CONFIG_HOME: undefined, BILI_OPENCODE_ACP_SPEC: undefined }, () => { - const base = fs.mkdtempSync(path.join(os.tmpdir(), "acp-resolve2-")); - try { - const cache = path.join(base, "cache"); - const config = path.join(base, "config"); - const proj = path.join(base, "proj"); - process.env.XDG_CACHE_HOME = cache; - process.env.XDG_CONFIG_HOME = config; - const slots = path.join(cache, "opencode", "packages"); - makePkg(path.join(slots, "opencode-acp@latest", "node_modules"), "1.2.0"); - makePkg(path.join(slots, "opencode-acp@1.0.0", "node_modules"), "1.0.0"); - makePkg(path.join(slots, "opencode-acp-old", "node_modules"), "2.3.0"); // wrong major - makePkg(path.join(slots, "opencode-acp-nodist", "node_modules"), "1.9.0", { dist: false }); // missing dist - makePkg(path.join(slots, "other-pkg", "node_modules"), "9.9.9"); // unrelated entry - makePkg(path.join(slots, "my-opencode-acp-fork", "node_modules"), "9.8.0"); // fork must not match - assert.equal(resolveAcpPackage(proj), path.join(slots, "opencode-acp@latest", "node_modules", "opencode-acp")); - // nothing valid anywhere → undefined - fs.rmSync(slots, { recursive: true, force: true }); - assert.equal(resolveAcpPackage(proj), undefined); - } finally { - fs.rmSync(base, { recursive: true, force: true }); - } -})); - -test("acpStoreDir: default, XDG override, storagePath precedence and forms", () => withEnv({ XDG_DATA_HOME: undefined, XDG_CONFIG_HOME: undefined, OPENCODE_CONFIG_DIR: undefined }, () => { - const base = fs.mkdtempSync(path.join(os.tmpdir(), "acp-store-")); - try { - const data = path.join(base, "data"); - const config = path.join(base, "config"); - const proj = path.join(base, "proj"); - process.env.XDG_DATA_HOME = data; - process.env.XDG_CONFIG_HOME = config; - fs.mkdirSync(proj, { recursive: true }); - // no acp config anywhere → default dir - assert.equal(acpStoreDir(proj), path.join(data, "opencode", "storage", "plugin", "acp")); - // global acp.jsonc WITH COMMENTS (jsonc-parser path), absolute storagePath - fs.mkdirSync(path.join(config, "opencode"), { recursive: true }); - fs.writeFileSync(path.join(config, "opencode", "acp.jsonc"), "// comment\n{ /* inline */\n \"storagePath\": \"/abs/store\"\n}\n"); - assert.equal(acpStoreDir(proj), "/abs/store"); - // project .opencode beats global - fs.mkdirSync(path.join(proj, ".opencode")); - fs.writeFileSync(path.join(proj, ".opencode", "acp.json"), JSON.stringify({ storagePath: "./rel-store" })); - assert.equal(acpStoreDir(proj), path.join(proj, "rel-store")); - // walk-up: nested cwd finds the ancestor .opencode. Like acp itself, - // a relative storagePath resolves against the HOST directory (ctx.directory), - // not the .opencode dir holding the config file. - const nested = path.join(proj, "deep", "nested"); - fs.mkdirSync(nested, { recursive: true }); - assert.equal(acpStoreDir(nested), path.join(nested, "rel-store")); - // ~ expansion - fs.rmSync(path.join(proj, ".opencode"), { recursive: true, force: true }); - fs.writeFileSync(path.join(config, "opencode", "acp.jsonc"), JSON.stringify({ storagePath: "~/tilde-store" })); - const home = process.env.HOME || ""; - assert.ok(acpStoreDir(proj).startsWith(home)); - assert.ok(acpStoreDir(proj).endsWith(path.join("tilde-store"))); - // malformed config → falls back to default dir (no throw) - fs.writeFileSync(path.join(config, "opencode", "acp.jsonc"), "{ not valid json !!"); - assert.equal(acpStoreDir(proj), path.join(data, "opencode", "storage", "plugin", "acp")); - } finally { - fs.rmSync(base, { recursive: true, force: true }); - } -})); - -test("createAcpBridge: import hygiene, gating matrix, tool wrapping", async () => withEnv({ XDG_DATA_HOME: undefined, XDG_CONFIG_HOME: undefined, XDG_CACHE_HOME: undefined, BILI_OPENCODE_ACP_SPEC: undefined, OPENCODE_CONFIG_DIR: undefined, BILLION_CONTEXT_PROXY: "http://127.0.0.1:9999/bili" }, async () => { - const base = fs.mkdtempSync(path.join(os.tmpdir(), "acp-bridge-")); - try { - const data = path.join(base, "data"); - const proj = path.join(base, "proj"); - process.env.XDG_DATA_HOME = data; - process.env.XDG_CONFIG_HOME = path.join(base, "config-empty"); - process.env.XDG_CACHE_HOME = path.join(base, "cache-empty"); - fs.mkdirSync(proj, { recursive: true }); - makePkg(path.join(proj, "node_modules"), "1.0.0", { body: FAKE_ACP_BODY }); - const storeDir = path.join(data, "opencode", "storage", "plugin", "acp"); - fs.mkdirSync(storeDir, { recursive: true }); - fs.writeFileSync(path.join(storeDir, "ses_legacy.json"), "{}"); - - delete (globalThis as Record).__acp; - const bridge = await createAcpBridge({ directory: proj }); - assert.ok(bridge, "bridge must be created"); - const b = bridge; - assert.equal(b.version, "1.0.0"); - assert.equal(b.storeDir, storeDir); - assert.equal(b.isLegacySession("ses_legacy"), true); - assert.equal(b.isLegacySession("ses_new"), false); - // BILLION_CONTEXT_PROXY removed during acp init, restored afterwards - const rec = (globalThis as Record).__acp; - assert.equal(rec.initProxyEnv, null); - assert.equal(rec.initDirectory, proj); - assert.equal(process.env.BILLION_CONTEXT_PROXY, "http://127.0.0.1:9999/bili"); - // config hook is never exposed - assert.ok(!("config" in b.wrapped)); - - await b.wrapped["experimental.chat.system.transform"]({ sessionID: "ses_legacy", model: {} }, { system: [] }); - assert.ok(Array.isArray(rec.system), "legacy system.transform delegated"); - rec.system = undefined; - await b.wrapped["experimental.chat.system.transform"]({ sessionID: "ses_new", model: {} }, { system: [] }); - assert.equal(rec.system, undefined, "new-session system.transform skipped"); - await b.wrapped["experimental.chat.system.transform"]({ model: {} }, { system: [] }); - assert.equal(rec.system, undefined, "missing sessionID skipped"); - - await b.wrapped["experimental.chat.messages.transform"]({}, { messages: [userMsg("ses_legacy")] }); - assert.ok(rec.messages, "legacy messages.transform delegated"); - rec.messages = undefined; - await b.wrapped["experimental.chat.messages.transform"]({}, { messages: [userMsg("ses_new")] }); - assert.equal(rec.messages, undefined, "new-session messages.transform skipped (no adoption)"); - await b.wrapped["experimental.chat.messages.transform"]({}, { messages: [userMsg("ses_legacy", { id: "msg_dcp_summary_9" })] }); - assert.equal(rec.messages, undefined, "synthetic summary message skipped"); - await b.wrapped["experimental.chat.messages.transform"]({}, { messages: [userMsg("ses_legacy", { parts: [] })] }); - assert.equal(rec.messages, undefined, "empty-parts user message skipped"); - await b.wrapped["experimental.chat.messages.transform"]({}, { messages: [userMsg("ses_legacy", { parts: [{ ignored: true }] })] }); - assert.equal(rec.messages, undefined, "fully-ignored user message skipped"); - - await b.wrapped["experimental.text.complete"]({ sessionID: "ses_legacy" }, { text: "x" }); - assert.ok(rec.textComplete, "legacy text.complete delegated"); - rec.textComplete = undefined; - await b.wrapped["experimental.text.complete"]({ sessionID: "ses_new" }, { text: "x" }); - assert.equal(rec.textComplete, undefined, "new-session text.complete skipped"); - - await b.wrapped["command.execute.before"]({ command: "acp", sessionID: "ses_legacy" }, {}); - assert.ok(rec.commandBefore, "legacy command.execute.before delegated"); - rec.commandBefore = undefined; - await b.wrapped["command.execute.before"]({ command: "acp", sessionID: "ses_new" }, {}); - assert.equal(rec.commandBefore, undefined, "new-session command skipped"); - - await b.wrapped.event({ event: { type: "message.part.updated" } }); - assert.ok(rec.event, "event handler ungated (timing bookkeeping only)"); - - assert.deepEqual(Object.keys(b.tools).sort(), ["compress", "decompress"]); - const out = await b.tools.compress.execute({}, { sessionID: "ses_new" }); - assert.equal(typeof out, "string"); - assert.match(out as string, /not available in this session/); - assert.equal(rec.toolCompress, undefined, "non-legacy tool call never reaches acp"); - const realOut = await b.tools.compress.execute({ ranges: [] }, { sessionID: "ses_legacy" }); - assert.ok(rec.toolCompress, "legacy tool call delegated"); - assert.notEqual(realOut, out); - } finally { - fs.rmSync(base, { recursive: true, force: true }); - delete (globalThis as Record).__acp; - } -})); - -test("createAcpBridge: graceful degradation paths return null", async () => { - const cases: Array<{ name: string; setup: (pkgBase: string) => void }> = [ - { name: "import throws", setup: (pkgBase) => makePkg(pkgBase, "1.0.0", { body: "throw new Error('boom');" }) }, - { name: "no default export", setup: (pkgBase) => makePkg(pkgBase, "1.0.0", { body: "export const x = 1;" }) }, - { name: "messages.transform missing", setup: (pkgBase) => makePkg(pkgBase, "1.0.0", { body: "export default async () => ({ tool: { compress: { execute: async () => 'ok' } } });" }) }, - { name: "compress tool missing", setup: (pkgBase) => makePkg(pkgBase, "1.0.0", { body: "export default async () => ({ 'experimental.chat.messages.transform': async () => {} });" }) }, - ]; - for (const c of cases) { - await withEnv({ XDG_DATA_HOME: undefined, XDG_CONFIG_HOME: undefined, XDG_CACHE_HOME: undefined, BILI_OPENCODE_ACP_SPEC: undefined, OPENCODE_CONFIG_DIR: undefined, BILLION_CONTEXT_PROXY: "http://x/bili" }, async () => { - const base = fs.mkdtempSync(path.join(os.tmpdir(), "acp-degrade-")); - try { - const proj = path.join(base, "proj"); - process.env.XDG_DATA_HOME = path.join(base, "data"); - process.env.XDG_CONFIG_HOME = path.join(base, "config-empty"); - process.env.XDG_CACHE_HOME = path.join(base, "cache-empty"); - fs.mkdirSync(proj, { recursive: true }); - c.setup(path.join(proj, "node_modules")); - assert.equal(await createAcpBridge({ directory: proj }), null, c.name); - } finally { - fs.rmSync(base, { recursive: true, force: true }); - } - }); - } - // no package anywhere - await withEnv({ XDG_DATA_HOME: undefined, XDG_CONFIG_HOME: undefined, XDG_CACHE_HOME: undefined, BILI_OPENCODE_ACP_SPEC: undefined, OPENCODE_CONFIG_DIR: undefined }, async () => { - const base = fs.mkdtempSync(path.join(os.tmpdir(), "acp-none-")); - try { - const proj = path.join(base, "proj"); - process.env.XDG_DATA_HOME = path.join(base, "data"); - process.env.XDG_CONFIG_HOME = path.join(base, "config-empty"); - process.env.XDG_CACHE_HOME = path.join(base, "cache-empty"); - fs.mkdirSync(proj, { recursive: true }); - assert.equal(await createAcpBridge({ directory: proj }), null, "package not found"); - } finally { - fs.rmSync(base, { recursive: true, force: true }); - } - }); - // wrong major only - await withEnv({ XDG_DATA_HOME: undefined, XDG_CONFIG_HOME: undefined, XDG_CACHE_HOME: undefined, BILI_OPENCODE_ACP_SPEC: undefined, OPENCODE_CONFIG_DIR: undefined }, async () => { - const base = fs.mkdtempSync(path.join(os.tmpdir(), "acp-major-")); - try { - const proj = path.join(base, "proj"); - process.env.XDG_DATA_HOME = path.join(base, "data"); - process.env.XDG_CONFIG_HOME = path.join(base, "config-empty"); - process.env.XDG_CACHE_HOME = path.join(base, "cache-empty"); - fs.mkdirSync(proj, { recursive: true }); - makePkg(path.join(proj, "node_modules"), "2.0.0", { body: FAKE_ACP_BODY }); - assert.equal(await createAcpBridge({ directory: proj }), null, "major 2 rejected"); - } finally { - fs.rmSync(base, { recursive: true, force: true }); - } - }); -}); diff --git a/tests/issue927-opencode-install.test.ts b/tests/issue927-opencode-install.test.ts new file mode 100644 index 0000000000..45b8861acb --- /dev/null +++ b/tests/issue927-opencode-install.test.ts @@ -0,0 +1,265 @@ +// #927: the opencode install target must be ONE effective config file written +// under the host-generation's canonical plugin key, without duplicating facts +// the user already wrote. Covers: version-aware key, jsonc preference, comment +// preservation, compaction.auto single-source, broken-target refusal, and +// remove/status across key spellings. +import { test } from "node:test"; +import * as assert from "node:assert/strict"; +import * as fs from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import { pluginInstall, pluginRemove, pluginStatusAll, pickPluginKey, detectOpencodeMajor } from "../src/plugin-install.ts"; +import { parse as jsoncParse, type ParseError } from "jsonc-parser"; + +function readJsonc(file: string): Record { + const errors: ParseError[] = []; + const data = jsoncParse(fs.readFileSync(file, "utf8"), errors, { allowTrailingComma: true }); + assert.equal(errors.length, 0, "written config must stay valid JSONC"); + return data as Record; +} + +async function withEnv(vars: Record, fn: () => Promise): Promise { + const saved = new Map(); + for (const [k, v] of Object.entries(vars)) { + saved.set(k, process.env[k]); + if (v === undefined) delete process.env[k]; + else process.env[k] = v; + } + try { + await fn(); + } finally { + for (const [k, v] of saved) { + if (v === undefined) delete process.env[k]; + else process.env[k] = v; + } + } +} + +function isolatedHome(): { home: string; cfgDir: string; env: Record } { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-oc927-")); + const cfgDir = path.join(home, ".config-x/opencode"); + return { + home, + cfgDir, + env: { + HOME: home, + XDG_CONFIG_HOME: path.join(home, ".config-x"), + XDG_DATA_HOME: path.join(home, ".data-x"), + XDG_STATE_HOME: path.join(home, ".state-x"), + XDG_CACHE_HOME: path.join(home, ".cache-x"), + OPENCODE_CONFIG: undefined, + BILI_CLIENT_BIN: undefined, + BILI_MCP_PROXY: "http://127.0.0.1:8787", + }, + }; +} + +function ocKeys(): { ocKey: "plugin" | "plugins"; otherKey: "plugin" | "plugins" } { + const ocKey = pickPluginKey(detectOpencodeMajor()); + return { ocKey, otherKey: ocKey === "plugin" ? "plugins" : "plugin" }; +} + +test("pickPluginKey: 2.x writes plugins, 1.x and unknowns write plugin (#927)", () => { + assert.equal(pickPluginKey(1), "plugin"); + assert.equal(pickPluginKey(2), "plugins"); + assert.equal(pickPluginKey(3), "plugins"); +}); + +test("detectOpencodeMajor: honors BILI_CLIENT_BIN, fails soft to 1 (#927)", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-oc927-ver-")); + const isWin = process.platform === "win32"; + try { + // Windows: execFileSync cannot spawn shebang scripts — fake the npm + // global shim shape (.cmd) there, matching what BILI_CLIENT_BIN points + // at in the wild. + const ext = isWin ? ".cmd" : ""; + const v2bin = path.join(home, `fake-opencode-v2${ext}`); + fs.writeFileSync(v2bin, isWin ? "@echo opencode 2.0.3\r\n" : "#!/bin/sh\necho \"opencode 2.0.3\"\n"); + if (!isWin) fs.chmodSync(v2bin, 0o755); + await withEnv({ BILI_CLIENT_BIN: v2bin }, async () => { + assert.equal(detectOpencodeMajor(), 2); + }); + const deadBin = path.join(home, `fake-opencode-dead${ext}`); + fs.writeFileSync(deadBin, isWin ? "@exit /b 3\r\n" : "#!/bin/sh\nexit 3\n"); + if (!isWin) fs.chmodSync(deadBin, 0o755); + await withEnv({ BILI_CLIENT_BIN: deadBin }, async () => { + assert.equal(detectOpencodeMajor(), 1); + }); + await withEnv({ BILI_CLIENT_BIN: path.join(home, "does-not-exist") }, async () => { + assert.equal(detectOpencodeMajor(), 1); + }); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("install targets existing opencode.jsonc, preserves comments, keeps compaction.auto single-sourced (#927)", async () => { + const { home, cfgDir, env } = isolatedHome(); + const { ocKey } = ocKeys(); + const file = path.join(cfgDir, "opencode.jsonc"); + fs.mkdirSync(cfgDir, { recursive: true }); + const original = [ + "{", + ' // user comment that must survive', + ' "$schema": "https://opencode.ai/config.json",', + ' "compaction": {', + " /* hand-tuned */", + ' "auto": false,', + ' "reserve": 5000', + " },", + ' "provider": { "myprov": {} }', + "}", + "", + ].join("\n"); + fs.writeFileSync(file, original); + try { + await withEnv(env, async () => { + const msg = pluginInstall("opencode"); + assert.match(msg, /installed -> .*opencode\.jsonc/); + assert.match(msg, /mcp\.bili written/); + assert.match(msg, /compaction\.auto already disabled/); + assert.doesNotMatch(msg, /set to false/); + const dir = path.join(cfgDir, "plugins/billion-context"); + let text = fs.readFileSync(file, "utf8"); + assert.ok(text.includes("// user comment that must survive"), "line comment survived"); + assert.ok(text.includes("/* hand-tuned */"), "block comment survived"); + const data = readJsonc(file); + assert.deepEqual(data.compaction, { auto: false, reserve: 5000 }); + assert.ok(Array.isArray(data[ocKey]) && (data[ocKey] as string[]).includes(dir)); + assert.equal(fs.existsSync(path.join(cfgDir, "opencode.json")), false, "no second file spawned"); + assert.equal(fs.existsSync(path.join(cfgDir, "config.json")), false); + + // idempotent re-run: byte-identical file, presence notes only + const again = pluginInstall("opencode"); + assert.match(again, /mcp\.bili present/); + assert.match(again, new RegExp(`${ocKey} present`)); + assert.match(again, /compaction\.auto already disabled/); + assert.equal(fs.readFileSync(file, "utf8"), text, "re-run leaves the file byte-identical"); + + // remove: entries gone, compaction restored from backup, comments intact + assert.match(pluginRemove("opencode"), /removed/); + text = fs.readFileSync(file, "utf8"); + assert.ok(text.includes("// user comment that must survive")); + const after = readJsonc(file); + assert.equal(after.mcp, undefined); + assert.equal(after[ocKey], undefined); + assert.deepEqual(after.compaction, { auto: false, reserve: 5000 }); + assert.equal(fs.existsSync(dir), false); + assert.equal(pluginStatusAll().find((r) => r.agent === "opencode")?.status, "not installed"); + }); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("fresh config dir: creates plain opencode.json, remove restores pre-install state (#927)", async () => { + const { home, cfgDir, env } = isolatedHome(); + const { ocKey } = ocKeys(); + const file = path.join(cfgDir, "opencode.json"); + try { + await withEnv(env, async () => { + const msg = pluginInstall("opencode"); + // plain includes(): `file` on win32 is a backslash path that must + // not be fed to a regex + assert.ok(msg.includes(`installed -> ${file} (`), msg); + assert.match(msg, /compaction\.auto set to false/); + const data = JSON.parse(fs.readFileSync(file, "utf8")) as Record; + assert.deepEqual(data.compaction, { auto: false }); + assert.deepEqual(data[ocKey], [path.join(cfgDir, "plugins/billion-context")]); + assert.equal(fs.existsSync(`${file}.bili-bak`), false, "nothing existed to back up"); + + assert.match(pluginRemove("opencode"), /removed/); + assert.match(pluginRemove("opencode"), /not installed/); + const after = JSON.parse(fs.readFileSync(file, "utf8")) as Record; + assert.equal(after.mcp, undefined); + assert.equal(after[ocKey], undefined); + assert.equal(after.compaction, undefined); + }); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("only config.json present: installer edits it, does not spawn opencode.json (#927)", async () => { + const { home, cfgDir, env } = isolatedHome(); + const { ocKey } = ocKeys(); + const file = path.join(cfgDir, "config.json"); + fs.mkdirSync(cfgDir, { recursive: true }); + fs.writeFileSync(file, JSON.stringify({ marker: "keep" }, null, 2)); + try { + await withEnv(env, async () => { + const msg = pluginInstall("opencode"); + // plain includes(): `file` on win32 is a backslash path that must + // not be fed to a regex + assert.ok(msg.includes(`installed -> ${file} (`), msg); + const data = JSON.parse(fs.readFileSync(file, "utf8")) as Record; + assert.equal(data.marker, "keep"); + assert.ok(Array.isArray(data[ocKey])); + assert.equal(fs.existsSync(path.join(cfgDir, "opencode.json")), false); + assert.equal(fs.existsSync(path.join(cfgDir, "opencode.jsonc")), false); + }); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("jsonc + json both present: jsonc wins, sibling json stays byte-identical (#927)", async () => { + const { home, cfgDir, env } = isolatedHome(); + const { ocKey } = ocKeys(); + fs.mkdirSync(cfgDir, { recursive: true }); + const jsoncFile = path.join(cfgDir, "opencode.jsonc"); + const jsonFile = path.join(cfgDir, "opencode.json"); + fs.writeFileSync(jsoncFile, "{\n // top comment\n \"compaction\": { \"auto\": false }\n}\n"); + const siblingBytes = '{\n "marker": "json"\n}\n'; + fs.writeFileSync(jsonFile, siblingBytes); + try { + await withEnv(env, async () => { + const msg = pluginInstall("opencode"); + assert.ok(msg.includes(`installed -> ${jsoncFile} (`), msg); + const data = readJsonc(jsoncFile); + assert.ok(Array.isArray(data[ocKey])); + assert.equal(fs.readFileSync(jsonFile, "utf8"), siblingBytes, "sibling opencode.json untouched"); + }); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("broken target config: refuses to overwrite, writes nothing (#927)", async () => { + const { home, cfgDir, env } = isolatedHome(); + const file = path.join(cfgDir, "opencode.jsonc"); + fs.mkdirSync(cfgDir, { recursive: true }); + const broken = "nope{"; + fs.writeFileSync(file, broken); + try { + await withEnv(env, async () => { + assert.throws(() => pluginInstall("opencode"), /refusing to overwrite/); + assert.equal(fs.readFileSync(file, "utf8"), broken, "broken config left byte-identical"); + assert.equal(fs.existsSync(`${file}.bili-bak`), false); + assert.equal(fs.existsSync(path.join(cfgDir, "plugins/billion-context/index.js")), false); + }); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("status/remove recognize our entry under either key spelling (#927)", async () => { + const { home, cfgDir, env } = isolatedHome(); + const { otherKey } = ocKeys(); + const file = path.join(cfgDir, "opencode.json"); + const dir = path.join(cfgDir, "plugins/billion-context"); + fs.mkdirSync(cfgDir, { recursive: true }); + fs.writeFileSync(file, JSON.stringify({ [otherKey]: [dir] })); + try { + await withEnv(env, async () => { + assert.equal(pluginStatusAll().find((r) => r.agent === "opencode")?.status, "installed"); + assert.match(pluginRemove("opencode"), /removed/); + const after = JSON.parse(fs.readFileSync(file, "utf8")) as Record; + assert.equal(after[otherKey], undefined); + assert.equal(fs.existsSync(dir), false); + assert.equal(pluginStatusAll().find((r) => r.agent === "opencode")?.status, "not installed"); + }); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index 957c575d45..8665282491 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -66,6 +66,7 @@ import { resolveOpencodeConfigFile, findFreePort, ensureProxyRunning, + resolveNodeRuntime, stopProxy, resolveLauncherWindow, resolveCodexBudgetArgs, @@ -935,6 +936,35 @@ test("ensureProxyRunning: throws when never healthy within deadline", async () = ); }); +test("ensureProxyRunning: registers a child 'error' handler so an async spawn failure rejects cleanly (#809/D)", async () => { + const subscribed: string[] = []; + const child: SpawnChild = { + pid: 42420, + unref() {}, + kill() { + return true; + }, + on(event, listener) { + subscribed.push(event); + if (event === "error") setImmediate(() => listener(new Error("spawn /bad/node EACCES"))); + }, + }; + await assert.rejects( + ensureProxyRunning( + { host: "127.0.0.1", port: 8791, passthrough: false, debug: false }, + { + spawnImpl: () => child, + fetchImpl: async () => ({ ok: false }), + readInstanceFile: () => undefined, + sleep: () => new Promise((r) => setTimeout(r, 0)), + }, + ), + /spawn failed/, + ); + assert.ok(subscribed.includes("exit")); + assert.ok(subscribed.includes("error"), "an 'error' handler was registered on the spawned proxy child"); +}); + function recordedInstance(over: Partial = {}): InstanceFile { return { origin: "http://127.0.0.1:8787", @@ -2662,6 +2692,58 @@ test("runLaunch dsh: no loopback custom providers — no DSH_HOME overlay (#535 } }); +test("resolveNodeRuntime: a live Node executable wins without consulting PATH (#819)", () => { + assert.equal(resolveNodeRuntime("/usr/local/bin/node", { PATH: "" }, "linux", () => false), "/usr/local/bin/node"); + assert.equal(resolveNodeRuntime("C:/nodejs/node.exe", {}, "win32", () => false), "C:/nodejs/node.exe"); +}); + +test("resolveNodeRuntime: non-Node host uses the BILLION_CONTEXT_NODE override when it exists", () => { + const exists = (p: string): boolean => p === "/opt/runtimes/node"; + assert.equal( + resolveNodeRuntime("/usr/bin/opencode", { BILLION_CONTEXT_NODE: " /opt/runtimes/node ", PATH: "" }, "linux", exists), + "/opt/runtimes/node", + ); + // an override pointing at a missing file is ignored — the PATH search still runs + assert.equal( + resolveNodeRuntime("/usr/bin/opencode", { BILLION_CONTEXT_NODE: "/missing/node", PATH: "/usr/local/bin" }, "linux", (p) => p === "/usr/local/bin/node"), + "/usr/local/bin/node", + ); +}); + +test("resolveNodeRuntime: PATH search finds node for non-Node hosts (posix + win32 shapes)", () => { + const posixExists = (p: string): boolean => p === "/opt/host/bin/node"; + assert.equal( + resolveNodeRuntime("/snap/opencode/current/usr/bin/opencode", { PATH: "/nonexistent:/opt/host/bin" }, "linux", posixExists), + "/opt/host/bin/node", + ); + // forward-slash fake paths keep the join platform-independent on any CI host + const winExists = (p: string): boolean => p === "C:/Program Files/nodejs/node.exe"; + assert.equal( + resolveNodeRuntime("C:/opencode/opencode.exe", { PATH: "C:/nope;C:/Program Files/nodejs" }, "win32", winExists), + "C:/Program Files/nodejs/node.exe", + ); +}); + +test("resolveNodeRuntime: throws with the actionable message when nothing resolves", () => { + assert.throws( + () => resolveNodeRuntime("/usr/bin/opencode", { PATH: "/nonexistent" }, "linux", () => false), + /BILLION_CONTEXT_NODE/, + ); +}); + +test("ensureProxyRunning: spawns the resolved Node runtime, not blind process.execPath (#819)", async () => { + let spawnedCmd: string | null = null; + const spawnImpl: SpawnFn = (cmd) => { + spawnedCmd = cmd as string; + return makeFakeChild(42425); + }; + await ensureProxyRunning( + { host: "127.0.0.1", port: 8787, passthrough: false, debug: false }, + { fetchImpl: async () => ({ ok: true }), spawnImpl, readInstanceFile: () => undefined, nodeRuntime: "/custom/node" }, + ); + assert.equal(spawnedCmd, "/custom/node"); +}); + test("runLaunch omp: launcher hands per-model windows to the spawned proxy", async () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-mw-")); const prevHome = process.env.HOME; diff --git a/tests/native-intercept.test.ts b/tests/native-intercept.test.ts new file mode 100644 index 0000000000..8d0c67b8c6 --- /dev/null +++ b/tests/native-intercept.test.ts @@ -0,0 +1,241 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { installNativeFetchIntercept, isModelApiUrl, _resetForTest, type NativeInterceptState } from "../src/agent/native-intercept.ts"; + +test("isModelApiUrl: matches model-API endpoint shapes", () => { + assert.equal(isModelApiUrl("http://127.0.0.1:8199/v1/messages"), true); + assert.equal(isModelApiUrl("http://127.0.0.1:8199/v1/chat/completions"), true); + assert.equal(isModelApiUrl("http://127.0.0.1:8199/v1/completions"), true); + assert.equal(isModelApiUrl("http://127.0.0.1:8199/v1/responses"), true); + assert.equal(isModelApiUrl("https://coding.dashscope.aliyuncs.com/apps/anthropic/v1/messages"), true); + assert.equal(isModelApiUrl("https://open.bigmodel.cn/api/coding/paas/v4/chat/completions"), true); + assert.equal(isModelApiUrl("https://api.anthropic.com/v1/messages?beta=true"), true); + assert.equal(isModelApiUrl("http://localhost:9123/v1/messages/"), true); +}); + +test("isModelApiUrl: rejects non-model URLs, proxy paths, non-HTTP", () => { + assert.equal(isModelApiUrl("http://127.0.0.1:8199/v1/models"), false); + assert.equal(isModelApiUrl("http://127.0.0.1:36485/__bili/plugin/manifest"), false); + assert.equal(isModelApiUrl("http://127.0.0.1:36485/bili/http://127.0.0.1:8199/v1/messages"), false); + assert.equal(isModelApiUrl("https://registry.npmjs.org/billion-context"), false); + assert.equal(isModelApiUrl("https://example.com/v1/messages/count_tokens"), false); + assert.equal(isModelApiUrl("file:///tmp/v1/messages"), false); + assert.equal(isModelApiUrl("not a url"), false); + assert.equal(isModelApiUrl("https://api.anthropic.com/v1/messages/count_tokens"), false); +}); + +function fakeFetch(sink: string[]) { + return (async (input: RequestInfo | URL, _init?: RequestInit) => { + sink.push(typeof input === "string" ? input : input instanceof URL ? input.href : (input as Request).url); + return new Response("{}", { status: 200 }); + }) as typeof fetch; +} + +async function withPatch(state: NativeInterceptState, fn: (fetch: typeof globalThis.fetch) => Promise): Promise<{ sink: string[]; result: T }> { + const saved = globalThis.fetch; + _resetForTest(); + const sink: string[] = []; + globalThis.fetch = fakeFetch(sink); + try { + assert.equal(installNativeFetchIntercept(state), true); + const result = await fn(globalThis.fetch); + return { sink, result }; + } finally { + globalThis.fetch = saved; + _resetForTest(); + } +} + +test("install: rewrites model URLs once ready", async () => { + const state: NativeInterceptState = { origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }; + const { sink } = await withPatch(state, async (fetch) => { + const res = await fetch("http://127.0.0.1:8199/v1/messages", { method: "POST" }); + assert.equal(res.status, 200); + }); + assert.deepEqual(sink, ["http://127.0.0.1:40001/bili/http://127.0.0.1:8199/v1/messages"]); +}); + +test("install: waits for a not-yet-ready proxy before rewriting", async () => { + let release: (v: string | undefined) => void = () => {}; + const ready = new Promise((r) => { + release = r; + }); + const state: NativeInterceptState = { origin: undefined, ready, readyTimeoutMs: 5000 }; + const { sink } = await withPatch(state, async (fetch) => { + const pending = fetch("http://127.0.0.1:8199/v1/messages"); + await new Promise((r) => setTimeout(r, 20)); + release("http://127.0.0.1:40002"); + const res = await pending; + assert.equal(res.status, 200); + }); + assert.deepEqual(sink, ["http://127.0.0.1:40002/bili/http://127.0.0.1:8199/v1/messages"]); +}); + +test("install: falls back to direct when the bootstrap fails/times out", async () => { + const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined), readyTimeoutMs: 50 }; + const { sink } = await withPatch(state, async (fetch) => { + const res = await fetch("http://127.0.0.1:8199/v1/messages"); + assert.equal(res.status, 200); + }); + assert.deepEqual(sink, ["http://127.0.0.1:8199/v1/messages"]); +}); + +test("install: leaves non-model URLs untouched", async () => { + const state: NativeInterceptState = { origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }; + const { sink } = await withPatch(state, async (fetch) => { + await fetch("https://registry.npmjs.org/billion-context"); + await fetch("http://127.0.0.1:40001/__bili/plugin/manifest"); + }); + assert.deepEqual(sink, ["https://registry.npmjs.org/billion-context", "http://127.0.0.1:40001/__bili/plugin/manifest"]); +}); + +test("install: proxy-origin URLs are never re-proxied (self guard)", async () => { + const state: NativeInterceptState = { origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }; + const { sink } = await withPatch(state, async (fetch) => { + await fetch("http://127.0.0.1:40001/v1/messages"); + }); + assert.deepEqual(sink, ["http://127.0.0.1:40001/v1/messages"]); +}); + +test("install: TypeError triggers one respawn + retry", async () => { + const calls: string[] = []; + const saved = globalThis.fetch; + _resetForTest(); + let failNext = true; + globalThis.fetch = (async (input: RequestInfo | URL) => { + const url = typeof input === "string" ? input : input instanceof URL ? input.href : (input as Request).url; + calls.push(url); + if (failNext) { + failNext = false; + throw new TypeError("fetch failed"); + } + return new Response("{}", { status: 200 }); + }) as typeof fetch; + const state: NativeInterceptState = { origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001"), respawn: undefined }; + try { + assert.equal(installNativeFetchIntercept(state), true); + state.respawn = () => { + state.origin = "http://127.0.0.1:40009"; + state.ready = Promise.resolve("http://127.0.0.1:40009"); + return Promise.resolve("http://127.0.0.1:40009"); + }; + const res = await globalThis.fetch("http://127.0.0.1:8199/v1/messages"); + assert.equal(res.status, 200); + assert.deepEqual(calls, [ + "http://127.0.0.1:40001/bili/http://127.0.0.1:8199/v1/messages", + "http://127.0.0.1:40009/bili/http://127.0.0.1:8199/v1/messages", + ]); + assert.equal(state.origin, "http://127.0.0.1:40009"); + } finally { + globalThis.fetch = saved; + _resetForTest(); + } +}); + +test("install: second install is a no-op while active", async () => { + const state: NativeInterceptState = { origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }; + const saved = globalThis.fetch; + _resetForTest(); + globalThis.fetch = fakeFetch([]); + try { + assert.equal(installNativeFetchIntercept(state), true); + assert.equal(installNativeFetchIntercept(state), false); + } finally { + globalThis.fetch = saved; + _resetForTest(); + } +}); + +test("install: Request-object input is re-dispatched with the rewritten URL", async () => { + const state: NativeInterceptState = { origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }; + const { sink } = await withPatch(state, async (fetch) => { + const req = new Request("http://127.0.0.1:8199/v1/messages", { method: "POST", body: "{}" }); + const res = await fetch(req); + assert.equal(res.status, 200); + }); + assert.deepEqual(sink, ["http://127.0.0.1:40001/bili/http://127.0.0.1:8199/v1/messages"]); +}); + +test("install: failed respawn degrades to a direct send and fires onGiveUp", async () => { + const calls: string[] = []; + const dispatches: string[] = []; + const saved = globalThis.fetch; + _resetForTest(); + let failNext = true; + let respawns = 0; + let giveUps = 0; + globalThis.fetch = (async (input: RequestInfo | URL) => { + const url = typeof input === "string" ? input : input instanceof URL ? input.href : (input as Request).url; + calls.push(url); + if (failNext) { + failNext = false; + throw new TypeError("fetch failed"); + } + return new Response("{}", { status: 200 }); + }) as typeof fetch; + const state: NativeInterceptState = { + origin: "http://127.0.0.1:40001", + ready: Promise.resolve("http://127.0.0.1:40001"), + respawn: () => { + respawns += 1; + return Promise.resolve(undefined); + }, + onGiveUp: () => { + giveUps += 1; + }, + onDispatch: (_url, action) => dispatches.push(action), + }; + try { + assert.equal(installNativeFetchIntercept(state), true); + const res = await globalThis.fetch("http://127.0.0.1:8199/v1/messages"); + assert.equal(res.status, 200); + assert.deepEqual(calls, [ + "http://127.0.0.1:40001/bili/http://127.0.0.1:8199/v1/messages", + "http://127.0.0.1:8199/v1/messages", + ]); + assert.deepEqual(dispatches, ["rewrite", "direct"]); + assert.equal(state.origin, undefined); + assert.equal(respawns, 1); + assert.equal(giveUps, 1); + // The degrade is permanent for the session: later model requests go + // direct without re-entering the respawn path. + const res2 = await globalThis.fetch("http://127.0.0.1:8199/v1/messages"); + assert.equal(res2.status, 200); + assert.equal(calls[2], "http://127.0.0.1:8199/v1/messages"); + assert.deepEqual(dispatches, ["rewrite", "direct", "direct"]); + assert.equal(respawns, 1); + assert.equal(giveUps, 1); + } finally { + globalThis.fetch = saved; + _resetForTest(); + } +}); + +test("install: a consumed-body Request throws without triggering a respawn", async () => { + const saved = globalThis.fetch; + _resetForTest(); + let respawnCalls = 0; + globalThis.fetch = (async () => new Response("{}", { status: 200 })) as typeof fetch; + const state: NativeInterceptState = { + origin: "http://127.0.0.1:40001", + ready: Promise.resolve("http://127.0.0.1:40001"), + respawn: () => { + respawnCalls += 1; + return Promise.resolve("http://127.0.0.1:40001"); + }, + }; + try { + assert.equal(installNativeFetchIntercept(state), true); + const req = new Request("http://127.0.0.1:8199/v1/messages", { + method: "POST", + body: ReadableStream.from([new TextEncoder().encode("{}")]), + duplex: "half", + } as RequestInit & { duplex?: string }); + await req.arrayBuffer(); + await assert.rejects(() => globalThis.fetch(req), TypeError); + assert.equal(respawnCalls, 0); + } finally { + globalThis.fetch = saved; + _resetForTest(); + } +}); diff --git a/tests/opencode-native-v1.test.ts b/tests/opencode-native-v1.test.ts new file mode 100644 index 0000000000..705088877c --- /dev/null +++ b/tests/opencode-native-v1.test.ts @@ -0,0 +1,425 @@ +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; +import type { z } from "zod"; + +import { + createV1ServerHooks, + extractV1Windows, + jsonSchemaToZodShape, + rewriteV1Providers, + type V1Config, + type V1ProviderOptions, +} from "../src/agent/opencode-native.js"; + +// Minimal structural stand-in for the host's zod module: only the builders +// jsonSchemaToZodShape touches. Real zod (4.1.8) is exercised implicitly by +// the shape assertions below via the actual import. +const fakeZ = { + any: () => ({ __fake: "any" }), + string: () => ({ __fake: "string", describe: (d: string) => ({ __fake: "string", desc: d }) }), + number: () => ({ __fake: "number" }), + boolean: () => ({ __fake: "boolean" }), + array: (item: unknown) => ({ __fake: "array", item }), + enum: (values: [string, ...string[]]) => ({ __fake: "enum", values }), +} as unknown as typeof z; + +describe("jsonSchemaToZodShape", () => { + it("maps primitive, enum, array and unknown fields", () => { + const shape = jsonSchemaToZodShape( + { + type: "object", + properties: { + range: { type: "string", description: "refs" }, + limit: { type: "number" }, + force: { type: "boolean" }, + mode: { type: "string", enum: ["auto", "manual"] }, + keep: { type: "array", items: { type: "string" } }, + nested: { type: "object", properties: { x: { type: "string" } } }, + weird: { anyOf: [{ type: "string" }, { type: "number" }] }, + }, + }, + fakeZ, + ); + assert.equal((shape.range as { __fake: string }).__fake, "string"); + assert.equal((shape.limit as { __fake: string }).__fake, "number"); + assert.equal((shape.force as { __fake: string }).__fake, "boolean"); + assert.deepEqual((shape.mode as { values: string[] }).values, ["auto", "manual"]); + assert.equal((shape.keep as { item: { __fake: string } }).item.__fake, "string"); + assert.equal((shape.nested as { __fake: string }).__fake, "any"); + assert.equal((shape.weird as { __fake: string }).__fake, "any"); + }); + + it("tolerates non-object schemas", () => { + assert.deepEqual(jsonSchemaToZodShape(undefined, fakeZ), {}); + assert.deepEqual(jsonSchemaToZodShape({ type: "string" }, fakeZ), {}); + assert.deepEqual(jsonSchemaToZodShape({ properties: "nope" }, fakeZ), {}); + }); + + it("produces fields the real host zod accepts in z.object()", async () => { + // The v1 registry wraps our shape with the HOST's zod: simulate with + // the real zod dependency (4.1.8) to prove cross-instance interop. + const real = (await import("zod")) as typeof z; + const shape = jsonSchemaToZodShape( + { type: "object", properties: { range: { type: "string" }, limit: { type: "number" } } }, + real, + ); + const parsed = real.object(shape).safeParse({ range: "m1-m5", limit: 3 }); + assert.equal(parsed.success, true); + const bad = real.object(shape).safeParse({ range: 7, limit: 3 }); + assert.equal(bad.success, false); + }); +}); + +describe("rewriteV1Providers", () => { + const origin = "http://127.0.0.1:19199"; + + it("rewrites http(s) baseURLs and preserves provider entries", () => { + const cfg: V1Config = { + provider: { + openai: { options: { baseURL: "https://api.openai.com/v1", apiKey: "sk-x" } }, + local: { options: { baseURL: "http://127.0.0.1:8199/v1" } }, + }, + }; + const n = rewriteV1Providers(cfg, origin); + assert.equal(n, 2); + const openai = (cfg.provider?.openai?.options ?? {}) as V1ProviderOptions; + const local = (cfg.provider?.local?.options ?? {}) as V1ProviderOptions; + assert.equal(openai.baseURL, `${origin}/bili/https://api.openai.com/v1`); + assert.equal(local.baseURL, `${origin}/bili/http://127.0.0.1:8199/v1`); + // untouched sibling keys + assert.equal(openai.apiKey, "sk-x"); + }); + + it("is idempotent for already-wrapped URLs", () => { + const cfg: V1Config = { + provider: { wrapped: { options: { baseURL: `${origin}/bili/https://api.openai.com/v1` } } }, + }; + assert.equal(rewriteV1Providers(cfg, origin), 0); + assert.equal(cfg.provider?.wrapped?.options?.baseURL, `${origin}/bili/https://api.openai.com/v1`); + }); + + it("skips non-http and missing baseURLs, handles absent provider table", () => { + const cfg: V1Config = { + provider: { + weird: { options: { baseURL: "file:///nope" } }, + none: { options: {} }, + empty: { options: { baseURL: " " } }, + }, + }; + assert.equal(rewriteV1Providers(cfg, origin), 0); + assert.deepEqual(rewriteV1Providers({}, origin), 0); + const broken: V1Config = { provider: "not-a-table" }; + assert.equal(rewriteV1Providers(broken, origin), 0); + }); + + it("unwraps double-wrapped upstreams instead of nesting", () => { + const cfg: V1Config = { + provider: { x: { options: { baseURL: `http://other:1/bili/https://api.anthropic.com` } } }, + }; + const n = rewriteV1Providers(cfg, origin); + assert.equal(n, 1); + assert.equal(cfg.provider?.x?.options?.baseURL, `${origin}/bili/https://api.anthropic.com`); + }); + + it("disables compaction.auto while merging existing settings", () => { + const cfg: V1Config = { + provider: { x: { options: { baseURL: "https://api.openai.com/v1" } } }, + compaction: { threshold: 0.8 }, + }; + rewriteV1Providers(cfg, origin); + assert.deepEqual(cfg.compaction, { threshold: 0.8, auto: false }); + }); +}); + +describe("extractV1Windows", () => { + it("collects finite positive limits keyed by provider/model and floors them", () => { + const cfg: V1Config = { + provider: { + openai: { models: { gpt: { limit: { context: 128_000.9 } } } }, + anthropic: { models: { claude: { limit: { context: 200_000 } } } }, + }, + }; + const m = extractV1Windows(cfg); + assert.equal(m.get("openai/gpt"), 128000); + assert.equal(m.get("anthropic/claude"), 200000); + }); + + it("drops non-finite, non-positive and non-numeric limits", () => { + const cfg: V1Config = { + provider: { + p: { + models: { + zero: { limit: { context: 0 } }, + neg: { limit: { context: -5 } }, + nan: { limit: { context: Number.NaN } }, + str: { limit: { context: "big" } }, + empty: { limit: {} }, + nolimit: {}, + }, + }, + }, + }; + assert.deepEqual(extractV1Windows(cfg), new Map()); + }); + + it("returns an empty map for missing or broken provider tables", () => { + assert.deepEqual(extractV1Windows({}), new Map()); + assert.deepEqual(extractV1Windows({ provider: "nope" }), new Map()); + assert.deepEqual(extractV1Windows({ provider: { p: {} } }), new Map()); + assert.deepEqual(extractV1Windows({ provider: { p: { models: "nope" } } }), new Map()); + }); +}); + +describe("createV1ServerHooks", () => { + const origin = "http://127.0.0.1:19199"; + + function makeDeps() { + const forwarded: Array<{ conversationId: string; tool: string; args: unknown }> = []; + return { + z: fakeZ, + forward: async (o: string, conversationId: string, tool: string, args: unknown) => { + forwarded.push({ conversationId, tool, args }); + assert.equal(o, origin); + return `panel:${tool}`; + }, + forwarded, + }; + } + + it("registers /acp command, rewrites providers, stamps headers and tools when zod present", async () => { + const deps = makeDeps(); + const hooks = createV1ServerHooks(origin, {}, deps); + assert.ok(hooks.config); + assert.ok(hooks["chat.headers"]); + assert.ok(hooks["command.execute.before"]); + const names = Object.keys(hooks.tool ?? {}); + assert.ok(names.includes("compress"), `tools include compress, got ${names.join(",")}`); + + const cfg: V1Config = { provider: { o: { options: { baseURL: "https://api.openai.com/v1" } } } }; + await hooks.config?.(cfg); + assert.equal(cfg.provider?.o?.options?.baseURL, `${origin}/bili/https://api.openai.com/v1`); + assert.ok(cfg.command?.acp); + + const headers: Record = {}; + await hooks["chat.headers"]?.({ sessionID: "ses_1" }, { headers }); + assert.equal(headers["x-bili-plugin"], "opencode"); + assert.equal(headers["x-bili-plugin-conversation"], "ses_1"); + + const compress = hooks.tool?.compress; + assert.ok(compress); + const out = await compress.execute({ range: "m1-m2" }, { sessionID: "ses_1" }); + assert.equal(out, "panel:compress"); + assert.deepEqual(deps.forwarded, [{ conversationId: "ses_1", tool: "compress", args: { range: "m1-m2" } }]); + }); + + it("degrades to proxy mode (no headers, no tools) when zod is unavailable", async () => { + const hooks = createV1ServerHooks(origin, {}, {}); + assert.equal(hooks["chat.headers"], undefined); + assert.equal(hooks.tool, undefined); + assert.ok(hooks.config); + const cfg: V1Config = { provider: { o: { options: { baseURL: "https://api.openai.com/v1" } } } }; + await hooks.config?.(cfg); + assert.equal(cfg.provider?.o?.options?.baseURL, `${origin}/bili/https://api.openai.com/v1`); + }); + + it("stamps x-bili-plugin-context-window from config-declared model limits (omits when absent)", async () => { + const deps = makeDeps(); + const hooks = createV1ServerHooks(origin, {}, deps); + assert.ok(hooks["chat.headers"]); + const cfg: V1Config = { + provider: { + openai: { options: { baseURL: "https://api.openai.com/v1" }, models: { gpt: { limit: { context: 128_000.9 } } } }, + }, + }; + await hooks.config?.(cfg); + const h1: Record = {}; + await hooks["chat.headers"]?.({ sessionID: "ses_w", model: { providerID: "openai", id: "gpt" } }, { headers: h1 }); + assert.equal(h1["x-bili-plugin-context-window"], "128000"); + const h2: Record = {}; + await hooks["chat.headers"]?.({ sessionID: "ses_w", model: { providerID: "openai", id: "other" } }, { headers: h2 }); + assert.equal(h2["x-bili-plugin-context-window"], undefined); + const h3: Record = {}; + await hooks["chat.headers"]?.({ sessionID: "ses_w" }, { headers: h3 }); + assert.equal(h3["x-bili-plugin-context-window"], undefined); + }); + + it("command.execute.before only reacts to /acp", async () => { + const deps = makeDeps(); + const hooks = createV1ServerHooks(origin, {}, deps); + await hooks["command.execute.before"]?.({ command: "other", sessionID: "s" }); + // /acp path throws the sentinel after rendering — assert the sentinel shape + await assert.rejects( + hooks["command.execute.before"]?.({ command: "acp", sessionID: "ses_x" }), + /__BILI_ACP_HANDLED__/, + ); + }); +}); + +// --------------------------------------------------------------------------- +// #920 legacy lane routing: absorbed opencode-acp serves pre-migration +// sessions; new sessions go to the proxy. +// --------------------------------------------------------------------------- + +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { + callLegacyAcpConfig, + isLegacyAcpSession, + legacyAcpStatePath, + type LegacyAcpModule, +} from "../src/agent/opencode-legacy.js"; + +function fakeLegacyModule(events: string[]): LegacyAcpModule { + const hooks: Record = { + config: async (cfg: Record) => { + events.push(`legacy-config:${JSON.stringify(Object.keys(cfg))}`); + cfg.permission = { deny: ["dcp_*"] }; + }, + "command.execute.before": async (input: { command: string; sessionID: string }, output?: { parts?: unknown[] }) => { + events.push(`legacy-command:${input.command}:${input.sessionID}:${output === undefined ? "no-output" : "output"}`); + }, + "experimental.chat.system.transform": async (input: { sessionID: string }, output?: { system?: unknown[] }) => { + events.push(`legacy-system:${input.sessionID}:${output === undefined ? "no-output" : "output"}`); + }, + "experimental.chat.messages.transform": async (_i: unknown, output: { messages: unknown[] }) => { + const last = output.messages[output.messages.length - 1] as { info?: { sessionID?: string } }; + events.push(`legacy-messages:${last?.info?.sessionID}`); + }, + "experimental.text.complete": async (input: { sessionID: string }, output?: { text?: unknown }) => { + events.push(`legacy-text:${input.sessionID}:${output === undefined ? "no-output" : "output"}`); + }, + tool: { + compress: { + description: "legacy compress", + args: { content: { __fake: "string" } }, + execute: async (args: Record, ctx: { sessionID: string }) => { + events.push(`legacy-compress:${ctx.sessionID}`); + return "legacy-compressed"; + }, + }, + acp_status: { + description: "legacy status", + args: {}, + execute: async (_a: Record, ctx: { sessionID: string }) => { + events.push(`legacy-status:${ctx.sessionID}`); + return "legacy-status"; + }, + }, + }, + }; + return { hooks, tools: hooks.tool as LegacyAcpModule["tools"], configHook: hooks.config as LegacyAcpModule["configHook"], commandHook: hooks["command.execute.before"] as LegacyAcpModule["commandHook"], source: "/fake/opencode-acp/dist/index.js" }; +} + +describe("createV1ServerHooks legacy routing (#920)", () => { + it("routes tools, headers, command and transforms per session lane", async () => { + const events: string[] = []; + const legacy = fakeLegacyModule(events); + const forwarded: { sid: string; tool: string; args: unknown }[] = []; + const hooks = createV1ServerHooks("http://127.0.0.1:19999", {}, { + z: fakeZ, + legacy, + isLegacy: (sid) => sid === "ses_legacy", + forward: async (_o, sid, tool, args) => { + forwarded.push({ sid, tool, args }); + return "proxied"; + }, + log: () => {}, + }); + + // legacy session: tool executes via absorbed acp + const r1 = await hooks.tool?.compress.execute({ content: [] }, { sessionID: "ses_legacy" }); + assert.equal(r1, "legacy-compressed"); + assert.deepEqual(forwarded, []); + // new session: forwarded to the proxy + const r2 = await hooks.tool?.compress.execute({ content: [] }, { sessionID: "ses_new" }); + assert.equal(r2, "proxied"); + assert.deepEqual(forwarded, [{ sid: "ses_new", tool: "compress", args: { content: [] } }]); + + // headers: legacy bypasses, new stamps plugin mode + const h1: Record = {}; + await hooks["chat.headers"]?.({ sessionID: "ses_legacy" } as never, { headers: h1 }); + assert.equal(h1["x-bili-plugin"], undefined); + assert.equal(h1["x-bili-plugin-bypass"], "1"); + const h2: Record = {}; + await hooks["chat.headers"]?.({ sessionID: "ses_new" } as never, { headers: h2 }); + assert.equal(h2["x-bili-plugin"], "opencode"); + assert.equal(h2["x-bili-plugin-conversation"], "ses_new"); + assert.equal(h2["x-bili-plugin-bypass"], undefined); + + // /acp command: legacy session -> acp handler (no throw); new -> bili handler (throws __BILI_ACP_HANDLED__ after render) + // output must reach the absorbed acp handler (its handlers are two-arg (input, output)) + await hooks["command.execute.before"]?.({ command: "acp", sessionID: "ses_legacy", arguments: "" }, { parts: [] }); + assert.ok(events.includes("legacy-command:acp:ses_legacy:output")); + events.length = 0; + await assert.rejects(hooks["command.execute.before"]?.({ command: "acp", sessionID: "ses_new", arguments: "" }), /__BILI_ACP_HANDLED__/); + assert.equal(events.filter((e) => e.startsWith("legacy-command")).length, 0); + + // transforms gated to legacy sessions only; output reaches acp on the legacy lane + await hooks["experimental.chat.system.transform"]?.({ sessionID: "ses_new" }, { system: [] }); + assert.equal(events.filter((e) => e.startsWith("legacy-system:ses_new")).length, 0); + await hooks["experimental.chat.system.transform"]?.({ sessionID: "ses_legacy" }, { system: [] }); + assert.ok(events.includes("legacy-system:ses_legacy:output")); + await hooks["experimental.chat.messages.transform"]?.({}, { messages: [{ role: "user" }, { role: "assistant", info: { sessionID: "ses_new" } }] }); + assert.equal(events.filter((e) => e === "legacy-messages:ses_new").length, 0); + await hooks["experimental.chat.messages.transform"]?.({}, { messages: [{ info: { sessionID: "ses_legacy" } }] }); + assert.ok(events.includes("legacy-messages:ses_legacy")); + await hooks["experimental.text.complete"]?.({ sessionID: "ses_new" }, { text: "" }); + await hooks["experimental.text.complete"]?.({ sessionID: "ses_legacy" }, { text: "" }); + assert.equal(events.filter((e) => e.startsWith("legacy-text:ses_new")).length, 0); + assert.ok(events.includes("legacy-text:ses_legacy:output")); + }); + + it("config hook hides providers from absorbed acp and still rewrites", async () => { + const events: string[] = []; + const legacy = fakeLegacyModule(events); + const cfg: V1Config = { + provider: { testprov: { options: { baseURL: "http://127.0.0.1:19998/v1" } } }, + }; + await createV1ServerHooks("http://127.0.0.1:19999", {}, { + z: fakeZ, + legacy, + isLegacy: () => false, + forward: async () => "proxied", + log: () => {}, + }).config?.(cfg); + // legacy saw no provider key (shadow had provider: undefined) + assert.ok(events.some((e) => e.startsWith("legacy-config:") && !e.includes("provider"))); + // its permission write survived the merge + assert.deepEqual(cfg.permission, { deny: ["dcp_*"] }); + // and the rewrite still happened + assert.equal((cfg.provider.testprov?.options as V1ProviderOptions).baseURL, "http://127.0.0.1:19999/bili/http://127.0.0.1:19998/v1"); + }); +}); + +describe("legacy session state file (#920)", () => { + it("detects legacy sessions by acp state file and sanitizes ids", () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-legacy-state-")); + process.env.XDG_DATA_HOME = path.join(home, "data"); + try { + fs.mkdirSync(path.dirname(legacyAcpStatePath("ses_old")), { recursive: true }); + fs.writeFileSync(legacyAcpStatePath("ses_old"), "{}"); + assert.equal(isLegacyAcpSession("ses_old"), true); + assert.equal(isLegacyAcpSession("ses_fresh"), false); + assert.equal(isLegacyAcpSession(undefined), false); + assert.equal(isLegacyAcpSession(""), false); + assert.equal(isLegacyAcpSession("../../etc"), false); + } finally { + delete process.env.XDG_DATA_HOME; + fs.rmSync(home, { recursive: true, force: true }); + } + }); + + it("callLegacyAcpConfig merges only owned top-level keys", async () => { + const cfg: Record = { provider: { p: 1 }, agent: { keep: true } }; + await callLegacyAcpConfig(async (shadow) => { + assert.equal((shadow as { provider?: unknown }).provider, undefined); + shadow.experimental = { replaced: true }; // reference change → owned + (shadow as { permission?: unknown }).permission = { deny: ["x"] }; + }, cfg); + assert.equal((cfg.provider as { p?: number }).p, 1); // untouched + assert.deepEqual(cfg.agent, { keep: true }); // not an owned key + assert.deepEqual(cfg.experimental, { replaced: true }); + assert.deepEqual(cfg.permission, { deny: ["x"] }); + }); +}); diff --git a/tests/opencode-native.test.ts b/tests/opencode-native.test.ts new file mode 100644 index 0000000000..df1641dd14 --- /dev/null +++ b/tests/opencode-native.test.ts @@ -0,0 +1,369 @@ +import test from "node:test"; +import assert from "node:assert/strict"; + +// The module-level guard reads NODE_TEST_CONTEXT while the module EVALUATES, +// and static imports hoist above any assignment — so the value must be set +// first and the module loaded dynamically. +process.env.NODE_TEST_CONTEXT = "1"; + +import type { NativeInterceptState } from "../src/agent/native-intercept.ts"; +import type { V2HttpRequestEvent, V2PluginContext, V2State } from "../src/agent/opencode-v2.ts"; +import { ACP_TOOLS_OPENAI, ABSORB_TOOL_OPENAI } from "../src/compress-tool.ts"; + +const { shouldBootstrapNativeOpencode, createNativeRoute, planNativeOpencode } = await import("../src/agent/opencode-native.ts"); +const { createOpencodeV2Setup } = await import("../src/agent/opencode-v2.ts"); +const { markNativeHost, nativeAttachOrigin } = await import("../src/agent/native-bootstrap.ts"); +const nativeDefault = (await import("../src/agent/opencode-native.ts")).default; + +const EXPECTED_TOOLS = [...ACP_TOOLS_OPENAI.map((t) => t.function.name), ABSORB_TOOL_OPENAI.function.name]; +const MODEL_URL = "https://api.anthropic.com/v1/messages"; +const sleep = (ms: number): Promise => new Promise((r) => setTimeout(r, ms)); + +test("shouldBootstrapNativeOpencode: true in a bare host with no bili env", () => { + assert.equal(shouldBootstrapNativeOpencode({}), true); +}); + +test("shouldBootstrapNativeOpencode: false when the plugin or native mode is opted out", () => { + assert.equal(shouldBootstrapNativeOpencode({ BILLION_CONTEXT_PLUGIN: "0" }), false); + assert.equal(shouldBootstrapNativeOpencode({ BILI_NATIVE_OPENCODE: "0" }), false); +}); + +test("shouldBootstrapNativeOpencode: false when a bili launch already owns a proxy", () => { + assert.equal(shouldBootstrapNativeOpencode({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485" }), false); + assert.equal(shouldBootstrapNativeOpencode({ BILLION_CONTEXT_PROXY: " " }), true); + assert.equal(shouldBootstrapNativeOpencode({ BILI_PROVIDER_REWRITES: '{"vllm":"http://127.0.0.1:1/bili/http://x"}' }), false); +}); + +test("native entry exports an OpenCode 2.x plugin object", () => { + assert.equal(nativeDefault.id, "billion-context-opencode-native"); + assert.equal(typeof nativeDefault.setup, "function"); +}); + +// #820 coexistence: the standalone opencode-acp extension must see this marker +// at its action-time check even though our bootstrap writes BILLION_CONTEXT_PROXY +// only later (async) and its /bili/ baseUrl check never sees our rewrite. +test("module evaluation marks the process as a native opencode host", () => { + assert.equal(process.env.BILLION_CONTEXT_NATIVE, "opencode"); +}); + +test("markNativeHost: sets when unset, first writer wins", () => { + const env: NodeJS.ProcessEnv = {}; + markNativeHost(env, "pi"); + assert.equal(env.BILLION_CONTEXT_NATIVE, "pi"); + markNativeHost(env, "opencode"); + assert.equal(env.BILLION_CONTEXT_NATIVE, "pi"); + const blank: NodeJS.ProcessEnv = { BILLION_CONTEXT_NATIVE: "" }; + markNativeHost(blank, "omp"); + assert.equal(blank.BILLION_CONTEXT_NATIVE, "omp"); +}); + +test("route: healthy origin rewrites the request reference and records proxyBase", async () => { + const origin = "http://127.0.0.1:9999"; + const state: NativeInterceptState = { origin, ready: Promise.resolve(origin) }; + const route = createNativeRoute(state, { probe: async () => true }); + const s: V2State = {}; + const body = JSON.stringify({ messages: [] }); + const e: V2HttpRequestEvent = { + sessionID: "ses_1", + request: new Request(MODEL_URL, { method: "POST", headers: { "content-type": "application/json", authorization: "Bearer x" }, body }), + }; + await route(e, s); + const out = e.request as Request; + assert.equal(out.url, `${origin}/bili/${MODEL_URL}`); + assert.equal(out.method, "POST"); + assert.equal(out.headers.get("authorization"), "Bearer x"); + assert.equal(await out.text(), body); + assert.equal(s.proxyBase, origin); +}); + +test("route: non-model-API and already-routed URLs are left untouched", async () => { + const origin = "http://127.0.0.1:9999"; + const state: NativeInterceptState = { origin, ready: Promise.resolve(origin) }; + const route = createNativeRoute(state, { probe: async () => true }); + const s: V2State = {}; + const plain = new Request("https://github.com/repos"); + const e1: V2HttpRequestEvent = { request: plain }; + await route(e1, s); + assert.equal(e1.request, plain); + const routed = new Request(`${origin}/bili/${MODEL_URL}`); + const e2: V2HttpRequestEvent = { request: routed }; + await route(e2, s); + assert.equal(e2.request, routed); + assert.equal(s.proxyBase, undefined); +}); + +test("route: waits for a pending bootstrap before routing", async () => { + let release!: (o: string) => void; + const state: NativeInterceptState = { origin: undefined, ready: new Promise((r) => (release = r)) }; + const route = createNativeRoute(state, { probe: async () => true }); + const s: V2State = {}; + const e: V2HttpRequestEvent = { request: new Request(MODEL_URL) }; + const pending = route(e, s); + release("http://127.0.0.1:4321"); + await pending; + assert.equal((e.request as Request).url, `http://127.0.0.1:4321/bili/${MODEL_URL}`); +}); + +test("route: failed bootstrap sends direct with a single warning (no respawn wired)", async () => { + const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined) }; + const route = createNativeRoute(state, { probe: async () => true }); + const s: V2State = {}; + const origError = console.error; + const warnings: string[] = []; + console.error = (...args: unknown[]) => { + warnings.push(args.join(" ")); + }; + try { + const e1: V2HttpRequestEvent = { request: new Request(MODEL_URL) }; + await route(e1, s); + assert.equal((e1.request as Request).url, MODEL_URL); + const e2: V2HttpRequestEvent = { request: new Request(MODEL_URL) }; + await route(e2, s); + assert.equal((e2.request as Request).url, MODEL_URL); + assert.equal(s.proxyBase, undefined); + assert.equal(warnings.filter((w) => w.includes("bili-native-opencode")).length, 1); + } finally { + console.error = origError; + } +}); + +test("route: dead origin respawns once and routes to the replacement", async () => { + const dead = "http://127.0.0.1:1111"; + const live = "http://127.0.0.1:2222"; + const state: NativeInterceptState = { + origin: dead, + ready: Promise.resolve(dead), + respawn: async () => { + state.origin = live; + return live; + }, + }; + const route = createNativeRoute(state, { probe: async (o) => o === live }); + const s: V2State = {}; + const e: V2HttpRequestEvent = { request: new Request(MODEL_URL) }; + await route(e, s); + assert.equal((e.request as Request).url, `${live}/bili/${MODEL_URL}`); + assert.equal(s.proxyBase, live); +}); + +test("route: failed respawn fires onGiveUp once and holds direct (cooldown suppresses re-spawn)", async () => { + const dead = "http://127.0.0.1:1111"; + let gaveUp = 0; + let respawns = 0; + const state: NativeInterceptState = { + origin: dead, + ready: Promise.resolve(dead), + respawn: async () => { + respawns++; + return undefined; + }, + onGiveUp: () => { + gaveUp++; + }, + }; + const route = createNativeRoute(state, { probe: async () => false }); + const s: V2State = {}; + const e1: V2HttpRequestEvent = { request: new Request(MODEL_URL) }; + await route(e1, s); + assert.equal((e1.request as Request).url, MODEL_URL); + assert.equal(gaveUp, 1); + assert.equal(respawns, 1); + const e2: V2HttpRequestEvent = { request: new Request(MODEL_URL) }; + await route(e2, s); + assert.equal(gaveUp, 1); + assert.equal(respawns, 1); +}); + +test("route: load-time bootstrap failure retries after the cooldown only", async () => { + let respawns = 0; + const state: NativeInterceptState = { + origin: undefined, + ready: Promise.resolve(undefined), + respawn: async () => { + respawns++; + return undefined; + }, + }; + const route = createNativeRoute(state, { probe: async () => true, respawnCooldownMs: 10 }); + const s: V2State = {}; + const e1: V2HttpRequestEvent = { request: new Request(MODEL_URL) }; + await route(e1, s); + assert.equal(respawns, 1); + const e2: V2HttpRequestEvent = { request: new Request(MODEL_URL) }; + await route(e2, s); + assert.equal(respawns, 1); + await sleep(15); + const e3: V2HttpRequestEvent = { request: new Request(MODEL_URL) }; + await route(e3, s); + assert.equal(respawns, 2); +}); + +test("setup(route): header stamping applies to the REPLACED request, tools register natively", async () => { + const origin = "http://127.0.0.1:9999"; + const state: NativeInterceptState = { origin, ready: Promise.resolve(origin) }; + const hooks: Array<{ name: string; cb: (e: V2HttpRequestEvent) => void | Promise }> = []; + const tools: string[] = []; + const ctx: V2PluginContext = { + session: { + hook: (name, cb) => { + hooks.push({ name, cb }); + return { dispose() {} }; + }, + }, + tool: { + transform: (add) => { + add({ add: (t) => tools.push(t.name) }); + return { dispose() {} }; + }, + }, + }; + const setup = createOpencodeV2Setup({ route: createNativeRoute(state, { probe: async () => true }) }); + const cleanup = await setup(ctx); + assert.deepEqual(hooks.map((h) => h.name), ["http.request"]); + assert.deepEqual(tools.sort(), [...EXPECTED_TOOLS].sort()); + const e: V2HttpRequestEvent = { + sessionID: "ses_abc", + model: { providerID: "anthropic", id: "claude-x" }, + request: new Request(MODEL_URL, { method: "POST" }), + }; + await hooks[0].cb(e); + const out = e.request as Request; + assert.equal(out.url, `${origin}/bili/${MODEL_URL}`); + assert.equal(out.headers.get("x-bili-plugin-conversation"), "ses_abc"); + assert.equal(out.headers.get("x-bili-plugin"), "opencode"); + cleanup(); +}); + +test("setup(route): kill switch keeps the hook fully inert", async () => { + const origin = "http://127.0.0.1:9999"; + const state: NativeInterceptState = { origin, ready: Promise.resolve(origin) }; + const hooks: Array<(e: V2HttpRequestEvent) => void | Promise> = []; + const ctx: V2PluginContext = { + session: { + hook: (_name, cb) => { + hooks.push(cb); + return { dispose() {} }; + }, + }, + }; + const setup = createOpencodeV2Setup({ route: createNativeRoute(state, { probe: async () => true }) }); + const cleanup = await setup(ctx); + process.env.BILLION_CONTEXT_PLUGIN = "0"; + try { + const req = new Request(MODEL_URL); + const e: V2HttpRequestEvent = { sessionID: "ses_x", request: req }; + await hooks[0](e); + assert.equal(e.request, req); + } finally { + delete process.env.BILLION_CONTEXT_PLUGIN; + cleanup(); + } +}); + +test("nativeAttachOrigin: unset/blank/malformed/non-http(s) all resolve to undefined", () => { + assert.equal(nativeAttachOrigin({}), undefined); + assert.equal(nativeAttachOrigin({ BILLION_CONTEXT_ATTACH: "" }), undefined); + assert.equal(nativeAttachOrigin({ BILLION_CONTEXT_ATTACH: " " }), undefined); + assert.equal(nativeAttachOrigin({ BILLION_CONTEXT_ATTACH: "not a url" }), undefined); + assert.equal(nativeAttachOrigin({ BILLION_CONTEXT_ATTACH: "ftp://127.0.0.1:21" }), undefined); +}); + +test("nativeAttachOrigin: normalizes a valid http(s) origin (trailing slash stripped)", () => { + assert.equal(nativeAttachOrigin({ BILLION_CONTEXT_ATTACH: "http://127.0.0.1:8787" }), "http://127.0.0.1:8787"); + assert.equal(nativeAttachOrigin({ BILLION_CONTEXT_ATTACH: "http://127.0.0.1:8787///" }), "http://127.0.0.1:8787"); + assert.equal(nativeAttachOrigin({ BILLION_CONTEXT_ATTACH: " https://proxy.example.com/ " }), "https://proxy.example.com"); +}); + +test("planNativeOpencode: default is spawn; opt-out and /bili/ launches are off", () => { + assert.deepEqual(planNativeOpencode({}), { mode: "spawn" }); + assert.deepEqual(planNativeOpencode({ BILLION_CONTEXT_PLUGIN: "0" }), { mode: "off" }); + assert.deepEqual(planNativeOpencode({ BILI_NATIVE_OPENCODE: "0" }), { mode: "off" }); + assert.deepEqual(planNativeOpencode({ BILI_PROVIDER_REWRITES: '{"vllm":"http://127.0.0.1:1/bili/http://x"}' }), { mode: "off" }); +}); + +test("planNativeOpencode: a preset BILLION_CONTEXT_PROXY is an attach target, not a stand-down", () => { + assert.deepEqual( + planNativeOpencode({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485" }), + { mode: "attach", attachOrigin: "http://127.0.0.1:36485" }, + ); + assert.deepEqual( + planNativeOpencode({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485/" }), + { mode: "attach", attachOrigin: "http://127.0.0.1:36485" }, + ); + // kill switches and a /bili/ launch still win over the preset + assert.deepEqual(planNativeOpencode({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485", BILLION_CONTEXT_PLUGIN: "0" }), { mode: "off" }); + assert.deepEqual(planNativeOpencode({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485", BILI_PROVIDER_REWRITES: "{}" }), { mode: "off" }); + // a garbage preset falls back to spawn (self-managed) rather than dead-off + assert.deepEqual(planNativeOpencode({ BILLION_CONTEXT_PROXY: "garbage" }), { mode: "spawn" }); + assert.deepEqual(planNativeOpencode({ BILLION_CONTEXT_PROXY: " " }), { mode: "spawn" }); +}); + +test("planNativeOpencode: attach wins over spawn when no launcher owns the proxy", () => { + assert.deepEqual( + planNativeOpencode({ BILLION_CONTEXT_ATTACH: "http://10.0.0.5:9000/" }), + { mode: "attach", attachOrigin: "http://10.0.0.5:9000" }, + ); + assert.deepEqual(planNativeOpencode({ BILLION_CONTEXT_ATTACH: "garbage" }), { mode: "spawn" }); +}); + +test("planNativeOpencode: explicit BILLION_CONTEXT_ATTACH wins over the env preset", () => { + assert.deepEqual( + planNativeOpencode({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485", BILLION_CONTEXT_ATTACH: "http://10.0.0.5:9000" }), + { mode: "attach", attachOrigin: "http://10.0.0.5:9000" }, + ); +}); + +test("native route: attach mode routes model traffic through the external proxy", async () => { + const origin = "http://10.0.0.5:9000"; + let respawnCalls = 0; + const state: NativeInterceptState = { + attach: true, + origin, + ready: Promise.resolve(origin), + respawn: async () => { + respawnCalls++; + return undefined; + }, + }; + const s: V2State = {}; + const route = createNativeRoute(state, { probe: async () => true }); + const e: V2HttpRequestEvent = { sessionID: "ses_a", request: new Request(MODEL_URL, { method: "POST" }) }; + await route(e, s); + assert.equal((e.request as Request).url, `${origin}/bili/${MODEL_URL}`); + assert.equal(s.proxyBase, origin); + assert.equal(respawnCalls, 0); +}); + +test("native route: attach mode FAILS CLOSED when the external proxy is down", async () => { + const origin = "http://10.0.0.5:9000"; + const state: NativeInterceptState = { attach: true, origin, ready: Promise.resolve(origin) }; + const s: V2State = {}; + const warns: string[] = []; + const origErr = console.error; + console.error = (...a: unknown[]) => { + warns.push(a.join(" ")); + }; + try { + const route = createNativeRoute(state, { probe: async () => false }); + await route({ sessionID: "ses_b", request: new Request(MODEL_URL, { method: "POST" }) }, s); + const second: V2HttpRequestEvent = { sessionID: "ses_b", request: new Request(MODEL_URL, { method: "POST" }) }; + await route(second, s); + assert.equal((second.request as Request).url, `${origin}/bili/${MODEL_URL}`); + assert.ok(warns.some((w) => w.includes("unreachable")), "expected an unreachable diagnostic"); + assert.ok(warns.filter((w) => w.includes("unreachable")).length === 1, "expected exactly one warning"); + } finally { + console.error = origErr; + } +}); + +test("native route: attach mode leaves non-model requests untouched", async () => { + const origin = "http://10.0.0.5:9000"; + const state: NativeInterceptState = { attach: true, origin, ready: Promise.resolve(origin) }; + const s: V2State = {}; + const route = createNativeRoute(state, { probe: async () => true }); + const req = new Request("https://api.anthropic.com/v1/models", { method: "GET" }); + const e: V2HttpRequestEvent = { sessionID: "ses_c", request: req }; + await route(e, s); + assert.equal(e.request, req); + assert.equal(s.proxyBase, undefined); +}); diff --git a/tests/opencode-v2.test.ts b/tests/opencode-v2.test.ts index 3023b6327a..493ebd3f51 100644 --- a/tests/opencode-v2.test.ts +++ b/tests/opencode-v2.test.ts @@ -42,6 +42,22 @@ function startFakeProxyV2(): Promise<{ origin: string; toolCalls: Array<{ conver }); return; } + if ((req.url ?? "").startsWith("/__bili/plugin/status")) { + if ((req.url ?? "").includes("ses_acp_idle")) { + res.writeHead(404, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: false, error: "unknown plugin conversation" })); + return; + } + res.writeHead(200, { "content-type": "application/json" }); + const panel = (req.url ?? "").includes("ses_acp_long") ? "X".repeat(1500) : "ACP-PANEL-OK"; + res.end(JSON.stringify({ ok: true, panel })); + return; + } + if ((req.url ?? "") === "/__bili/plugin/manifest") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ version: "9.9.9-test" })); + return; + } res.writeHead(404); res.end("{}"); }); @@ -62,12 +78,20 @@ interface FakeAddedTool { execute: (input: Record, ctx: { sessionID: string }) => Promise<{ content: string }>; } +interface FakeAddedCommand { + name: string; + description?: string; + execute: (input: Record) => Promise; +} + function makeFakeCtx() { const eventQueue: Array<{ type?: unknown; data?: Record }> = []; let wake: (() => void) | undefined; let closed = false; let signal: AbortSignal | undefined; const addedTools: FakeAddedTool[] = []; + const addedCommands: FakeAddedCommand[] = []; + const syntheticCalls: Array<{ sessionID: string; text: string; description?: string; resume?: boolean }> = []; let modelRequestCb: ((e: Record) => void | Promise) | undefined; const disposed: number[] = []; @@ -78,6 +102,10 @@ function makeFakeCtx() { modelRequestCb = cb; return { dispose: () => { disposed.push(1); } }; }, + synthetic: async (input: { sessionID: string; text: string; description?: string; resume?: boolean }) => { + syntheticCalls.push(input); + return {}; + }, }, tool: { transform: async (cb: (editor: { add: (t: FakeAddedTool) => void }) => void) => { @@ -85,6 +113,12 @@ function makeFakeCtx() { return { dispose: () => { disposed.push(2); } }; }, }, + command: { + transform: async (cb: (editor: { add: (c: FakeAddedCommand) => void }) => void) => { + cb({ add: (c) => addedCommands.push(c) }); + return { dispose: () => { disposed.push(3); } }; + }, + }, event: { subscribe: (opts?: { signal?: AbortSignal }) => { signal = opts?.signal; @@ -126,6 +160,8 @@ function makeFakeCtx() { }, pushEvent: (evt: { type?: unknown; data?: Record }) => { eventQueue.push(evt); wake?.(); }, get addedTools() { return addedTools; }, + get addedCommands() { return addedCommands; }, + get syntheticCalls() { return syntheticCalls; }, get disposed() { return disposed; }, }; } @@ -249,8 +285,15 @@ test("v2 setup: activates from /bili/ baseURL on round 1, stamps headers, forwar assert.equal(proxy.toolCalls.at(-1)?.conversationId, "ses_v2_1"); assert.equal(proxy.toolCalls.at(-1)?.tool, "compress"); - const statusOut = await fake.addedTools.find((t) => t.name === "acp_status")!.execute({}, { sessionID: "ses_other" }); + // Panel-first: acp_status returns the status panel when the + // proxy serves one for this conversation (ses_other gets the + // generic "ACP-PANEL-OK"), and falls back to the forwarded + // kernel tool when the status endpoint 404s (ses_acp_idle). + const panelOut = await fake.addedTools.find((t) => t.name === "acp_status")!.execute({}, { sessionID: "ses_other" }); + assert.equal(panelOut.content, "ACP-PANEL-OK"); + const statusOut = await fake.addedTools.find((t) => t.name === "acp_status")!.execute({}, { sessionID: "ses_acp_idle" }); assert.equal(statusOut.content, "STATUS-RESULT"); + assert.equal(proxy.toolCalls.at(-1)?.tool, "acp_status"); } finally { cleanup(); } @@ -316,6 +359,195 @@ test("v2 setup: cleanup aborts event subscription and disposes registrations", a } }); +test("v2 setup: /acp command registered and renders proxy status panel via synthetic", async () => { + const proxy = await startFakeProxyV2(); + const fake = makeFakeCtx(); + try { + await withEnv({ BILLION_CONTEXT_PROXY: proxy.origin, BILLION_CONTEXT_PLUGIN: undefined }, async () => { + const cleanup = await biliOpencodePlugin.setup(fake.ctx as never); + try { + const acp = fake.addedCommands.find((c) => c.name === "acp"); + assert.ok(acp, "/acp command registered"); + assert.equal(typeof acp!.execute, "function"); + await acp!.execute({ sessionID: "ses_acp_1" }); + await until(() => fake.syntheticCalls.length === 1); + assert.equal(fake.syntheticCalls[0].sessionID, "ses_acp_1"); + assert.match(fake.syntheticCalls[0].description!, /ACP-PANEL-OK/); + assert.match(fake.syntheticCalls[0].text, /displayed in your terminal/); + assert.match(fake.syntheticCalls[0].text, /not an instruction/); + assert.equal(fake.syntheticCalls[0].resume, false); + } finally { + cleanup(); + } + }); + } finally { + await proxy.close(); + } +}); + +test("v2 setup: /acp reports no proxy detected via synthetic when no proxy", async () => { + const fake = makeFakeCtx(); + await withEnv({ BILLION_CONTEXT_PROXY: undefined, BILLION_CONTEXT_PLUGIN: undefined }, async () => { + const cleanup = await biliOpencodePlugin.setup(fake.ctx as never); + try { + const acp = fake.addedCommands.find((c) => c.name === "acp")!; + await acp.execute({ sessionID: "s_nopx" }); + await until(() => fake.syntheticCalls.length === 1); + assert.match(fake.syntheticCalls[0].description!, /no proxy detected/); + assert.match(fake.syntheticCalls[0].text, /not an instruction/); + assert.equal(fake.syntheticCalls[0].resume, false); + } finally { + cleanup(); + } + }); +}); + +test("v2 setup: first /acp before any model request shows the idle notice (proxy 404s the conversation)", async () => { + const proxy = await startFakeProxyV2(); + const fake = makeFakeCtx(); + try { + await withEnv({ BILLION_CONTEXT_PROXY: proxy.origin, BILLION_CONTEXT_PLUGIN: undefined }, async () => { + const cleanup = await biliOpencodePlugin.setup(fake.ctx as never); + try { + const acp = fake.addedCommands.find((c) => c.name === "acp")!; + await acp.execute({ sessionID: "ses_acp_idle" }); + await until(() => fake.syntheticCalls.length === 1); + assert.match(fake.syntheticCalls[0].description!, /billion-context@9\.9\.9-test \u2014 proxy connected, no ACP session yet/); + assert.match(fake.syntheticCalls[0].text, /not an instruction/); + assert.equal(fake.syntheticCalls[0].resume, false); + } finally { + cleanup(); + } + }); + } finally { + await proxy.close(); + } +}); + + +test("v2 setup: /acp truncates long panels to the TUI notice cap", async () => { + const proxy = await startFakeProxyV2(); + const fake = makeFakeCtx(); + try { + await withEnv({ BILLION_CONTEXT_PROXY: proxy.origin, BILLION_CONTEXT_PLUGIN: undefined }, async () => { + const cleanup = await biliOpencodePlugin.setup(fake.ctx as never); + try { + const acp = fake.addedCommands.find((c) => c.name === "acp")!; + await acp.execute({ sessionID: "ses_acp_long" }); + await until(() => fake.syntheticCalls.length === 1); + const desc = fake.syntheticCalls[0].description ?? ""; + assert.ok(desc.length > 0 && desc.length <= 1024); + assert.match(desc, /\[panel truncated\]$/); + assert.match(fake.syntheticCalls[0].text, /not an instruction/); + } finally { + cleanup(); + } + }); + } finally { + await proxy.close(); + } +}); + +test("v2 /acp: host omits sessionID → warn once, render nothing (never an empty-id synthetic)", async () => { + const warns: string[] = []; + const origWarn = console.warn; + console.warn = (msg?: unknown) => { warns.push(String(msg)); }; + try { + for (const plugin of ["0", undefined]) { + const f = makeFakeCtx(); + await withEnv({ BILLION_CONTEXT_PROXY: undefined, BILLION_CONTEXT_PLUGIN: plugin }, async () => { + const cleanup = await biliOpencodePlugin.setup(f.ctx as never); + try { + const acp = f.addedCommands.find((c) => c.name === "acp")!; + await acp.execute({}); + await new Promise((r) => setTimeout(r, 20)); + assert.equal(f.syntheticCalls.length, 0, `no synthetic for missing sessionID (BILLION_CONTEXT_PLUGIN=${String(plugin)})`); + } finally { + cleanup(); + } + }); + } + assert.equal(warns.filter((w) => w.includes("sessionID")).length, 2, "one warning per no-sessionID invocation"); + } finally { + console.warn = origWarn; + } +}); + +test("v2 /acp: disabled + valid session still renders the 'disabled' notice", async () => { + const fake = makeFakeCtx(); + await withEnv({ BILLION_CONTEXT_PROXY: undefined, BILLION_CONTEXT_PLUGIN: "0" }, async () => { + const cleanup = await biliOpencodePlugin.setup(fake.ctx as never); + try { + const acp = fake.addedCommands.find((c) => c.name === "acp")!; + await acp.execute({ sessionID: "ses_dis" }); + await until(() => fake.syntheticCalls.length === 1); + assert.match(fake.syntheticCalls[0].description!, /disabled/); + assert.equal(fake.syntheticCalls[0].sessionID, "ses_dis"); + } finally { + cleanup(); + } + }); +}); + +test("v2 /acp: surfaces status.error when the proxy returns no panel", async () => { + const server = http.createServer((req, res) => { + if ((req.url ?? "").startsWith("/__bili/plugin/status")) { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true, error: "backend-busy" })); + return; + } + res.writeHead(404); + res.end("{}"); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const origin = `http://127.0.0.1:${(server.address() as { port: number }).port}`; + const fake = makeFakeCtx(); + try { + await withEnv({ BILLION_CONTEXT_PROXY: origin, BILLION_CONTEXT_PLUGIN: undefined }, async () => { + const cleanup = await biliOpencodePlugin.setup(fake.ctx as never); + try { + const acp = fake.addedCommands.find((c) => c.name === "acp")!; + await acp.execute({ sessionID: "ses_err" }); + await until(() => fake.syntheticCalls.length === 1); + assert.match(fake.syntheticCalls[0].description!, /no status panel/); + assert.match(fake.syntheticCalls[0].description!, /backend-busy/); + } finally { + cleanup(); + } + }); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } +}); + +test("v2 /acp: a throwing command editor degrades to no-/acp without killing setup", async () => { + const fake = makeFakeCtx(); + const ctxObj = fake.ctx as unknown as { command?: unknown }; + ctxObj.command = { + transform: async (cb: (editor: { add: (c: unknown) => void }) => void) => { + cb({ add: () => { throw new Error("command editor has no add"); } }); + return { dispose: () => {} }; + }, + }; + const warns: string[] = []; + const origWarn = console.warn; + console.warn = (msg?: unknown) => { warns.push(String(msg)); }; + try { + await withEnv({ BILLION_CONTEXT_PROXY: undefined, BILLION_CONTEXT_PLUGIN: undefined }, async () => { + const cleanup = await biliOpencodePlugin.setup(fake.ctx as never); + try { + assert.ok(typeof cleanup === "function", "setup resolved despite the command editor throwing"); + } finally { + cleanup(); + } + }); + assert.ok(warns.some((w) => w.includes("/acp")), "degradation logged"); + } finally { + console.warn = origWarn; + } +}); + test("fetchManifest openai format maps parameters to inputSchema", async () => { const MANIFEST_OPENAI = [ { name: "compress", description: "Compress a range", parameters: { type: "object", properties: { content: { type: "array" } }, required: ["content"] } }, diff --git a/tests/pi-native.test.ts b/tests/pi-native.test.ts new file mode 100644 index 0000000000..bab59ca16e --- /dev/null +++ b/tests/pi-native.test.ts @@ -0,0 +1,77 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import path from "node:path"; +import { pathToFileURL } from "node:url"; +import { shouldBootstrapNative, nativeProxyScriptPath, singleFlight } from "../src/agent/pi-native.ts"; +import { ensureProxyRunning, type SpawnChild, type SpawnFn } from "../src/launcher.ts"; + +// #820 coexistence: standalone billion-context-pi checks env at load time, so +// the marker must be set synchronously during module evaluation (before our +// async bootstrap writes BILLION_CONTEXT_PROXY) for its action-time back-off. +test("module evaluation marks the process as a native pi host", () => { + assert.equal(process.env.BILLION_CONTEXT_NATIVE, "pi"); +}); + +test("shouldBootstrapNative: true in a bare host with no bili env", () => { + assert.equal(shouldBootstrapNative({}), true); +}); + +test("shouldBootstrapNative: false when the plugin or native mode is opted out", () => { + assert.equal(shouldBootstrapNative({ BILLION_CONTEXT_PLUGIN: "0" }), false); + assert.equal(shouldBootstrapNative({ BILI_NATIVE_PI: "0" }), false); +}); + +test("shouldBootstrapNative: false when a bili launch already owns a proxy", () => { + assert.equal(shouldBootstrapNative({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485" }), false); + assert.equal(shouldBootstrapNative({ BILLION_CONTEXT_PROXY: " " }), true); + assert.equal(shouldBootstrapNative({ BILI_PROVIDER_REWRITES: '{"vllm":"http://127.0.0.1:1/bili/http://x"}' }), false); +}); + +test("nativeProxyScriptPath: dist/agent/pi-native.js resolves to the package bin", () => { + // Build the from-URL from a platform-native absolute path: a hardcoded + // file:///opt/... URL is invalid on Windows (no drive letter — + // fileURLToPath throws ERR_INVALID_FILE_URL_PATH). + const agentFile = path.resolve(path.sep, "opt", "pkg", "dist", "agent", "pi-native.js"); + const resolved = nativeProxyScriptPath(pathToFileURL(agentFile).href); + assert.equal(resolved, path.resolve(path.sep, "opt", "pkg", "dist", "index.js")); +}); + +function makeFakeChild(pid: number): SpawnChild { + return { + pid, + unref() {}, + kill() { + return true; + }, + on() {}, + }; +} + +test("singleFlight: concurrent callers share one in-flight run, then re-arm", async () => { + let runs = 0; + const start = singleFlight(async () => { + runs++; + await new Promise((r) => setTimeout(r, 20)); + return "http://127.0.0.1:40001"; + }); + const [a, b, c] = await Promise.all([start(), start(), start()]); + assert.equal(runs, 1, "three concurrent callers → one bootstrap"); + assert.deepEqual([a, b, c], ["http://127.0.0.1:40001", "http://127.0.0.1:40001", "http://127.0.0.1:40001"]); + // after settling, the next call starts fresh (a later respawn is allowed) + const d = await start(); + assert.equal(runs, 2); + assert.equal(d, "http://127.0.0.1:40001"); +}); + +test("ensureProxyRunning: deps.scriptPath overrides process.argv[1] for the spawned proxy (#519)", async () => { + let spawnScriptArg = ""; + const spawnImpl: SpawnFn = (_command, args) => { + spawnScriptArg = args[0]; + return makeFakeChild(42431); + }; + await ensureProxyRunning( + { host: "127.0.0.1", port: 8787, passthrough: false, debug: false }, + { fetchImpl: async () => ({ ok: true }), spawnImpl, readInstanceFile: () => undefined, scriptPath: "/opt/pkg/dist/index.js" }, + ); + assert.equal(spawnScriptArg, "/opt/pkg/dist/index.js"); +}); diff --git a/tests/plugin-agent.test.ts b/tests/plugin-agent.test.ts index 64136b77bd..b3adfa3726 100644 --- a/tests/plugin-agent.test.ts +++ b/tests/plugin-agent.test.ts @@ -11,7 +11,7 @@ process.env.NODE_ENV = "test"; import { proxyBaseFromUrl, proxyBaseFromEnv, detectProxyBase, fetchManifest, forwardTool, fetchStatus } from "../src/agent/shared.ts"; import biliPlugin, { createBiliPlugin } from "../src/agent/pi.ts"; import ompPlugin from "../src/agent/omp.ts"; -import { pluginInstall, pluginRemove, pluginStatusAll, PLUGIN_AGENTS, selfPackageRoot } from "../src/plugin-install.ts"; +import { pluginInstall, pluginRemove, pluginStatusAll, PLUGIN_AGENTS, selfPackageRoot, pickPluginKey, detectOpencodeMajor } from "../src/plugin-install.ts"; import { resolveProxyOrigin, forwardTool as mcpForwardTool } from "../src/mcp.ts"; function withEnv(vars: Record, fn: () => void | Promise): Promise { @@ -294,13 +294,24 @@ test("#535/#851: session_before_compact cancels only auto compaction under bili } }); -test("#535: plain pi (no BILLION_CONTEXT_PROXY) → no compaction cancel handler", () => { +test("#535/#519: no proxy at factory time → cancel inert until a proxy appears", () => { const prevProxy = process.env.BILLION_CONTEXT_PROXY; delete process.env.BILLION_CONTEXT_PROXY; try { const pi = makeFakePi(); createBiliPlugin("pi")(pi as never); - assert.equal(pi.events.get("session_before_compact"), undefined, "native run stays fully native"); + const handler = pi.events.get("session_before_compact"); + assert.ok(handler, "handler registered; arming decided at event time"); + assert.equal(handler({ reason: "threshold" }, undefined), undefined, "no proxy → native compaction untouched"); + // native mode (#519): BILLION_CONTEXT_PROXY lands only AFTER the factory ran + process.env.BILLION_CONTEXT_PROXY = "http://127.0.0.1:8787"; + assert.deepEqual(handler({ reason: "threshold" }, undefined), { cancel: true }); + assert.deepEqual(handler({ reason: "overflow" }, undefined), { cancel: true }); + assert.equal(handler({ reason: "manual" }, undefined), undefined, "manual /compact stays user-owned"); + // /bili/ baseUrl routing (no env at all) arms the cancel too + delete process.env.BILLION_CONTEXT_PROXY; + const biliCtx = { model: { baseUrl: "http://127.0.0.1:8787/bili/https://api.example.com/v1" } }; + assert.deepEqual(handler({ reason: "threshold" }, biliCtx), { cancel: true }); } finally { if (prevProxy === undefined) delete process.env.BILLION_CONTEXT_PROXY; else process.env.BILLION_CONTEXT_PROXY = prevProxy; @@ -877,12 +888,34 @@ test("plugin install/remove roundtrips for pi/omp/codex/opencode under a fake HO assert.match(pluginInstall("codex"), /already installed/); assert.match(pluginRemove("codex"), /removed/); - assert.match(pluginInstall("opencode"), /installed/); - const oc = JSON.parse(fs.readFileSync(path.join(home, ".config/opencode/opencode.json"), "utf8")) as { mcp: Record }> }; - assert.equal(oc.mcp.bili.command[1]!.endsWith(path.join("dist", "mcp.js")), true); - assert.equal(oc.mcp.bili.environment?.BILI_MCP_PROXY, "http://127.0.0.1:8787"); + // #820: pre-seed user settings — install must merge around them and remove must restore them. + // #927: seed under whichever key this host's opencode generation uses, so the + // round-trip assertions hold on both 1.x ("plugin") and 2.x ("plugins") machines. + const ocKey = pickPluginKey(detectOpencodeMajor()); + const ocFile = path.join(home, ".config/opencode/opencode.json"); + fs.mkdirSync(path.dirname(ocFile), { recursive: true }); + fs.writeFileSync(ocFile, JSON.stringify({ $schema: "https://opencode.ai/config.json", compaction: { auto: true, buffer: 100 }, [ocKey]: ["some-other-plugin"] })); + const ocPluginDir = path.join(home, ".config/opencode/plugins/billion-context"); + const ocInstallMsg = pluginInstall("opencode"); + assert.match(ocInstallMsg, /installed/); + assert.match(ocInstallMsg, /mcp\.bili written/); + let oc = JSON.parse(fs.readFileSync(ocFile, "utf8")) as { mcp?: Record }>; compaction?: Record; $schema?: string } & Record; + assert.equal(oc.mcp?.bili.command[1]!.endsWith(path.join("dist", "mcp.js")), true); + assert.equal(oc.mcp?.bili.environment?.BILI_MCP_PROXY, "http://127.0.0.1:8787"); + assert.deepEqual(oc[ocKey], ["some-other-plugin", ocPluginDir]); + assert.match(fs.readFileSync(path.join(ocPluginDir, "index.js"), "utf8").replace(/\\+/g, "/"), /agent\/opencode-native\.js/); + assert.deepEqual(oc.compaction, { auto: false, buffer: 100 }); + const ocAgain = pluginInstall("opencode"); + assert.match(ocAgain, /mcp\.bili present/); + assert.match(ocAgain, new RegExp(`${ocKey} present`)); assert.match(pluginRemove("opencode"), /removed/); - assert.equal((JSON.parse(fs.readFileSync(path.join(home, ".config/opencode/opencode.json"), "utf8")) as { mcp?: unknown }).mcp, undefined); + oc = JSON.parse(fs.readFileSync(ocFile, "utf8")) as Record; + assert.equal(oc.mcp, undefined); + // only the bili entry is dropped — the user's other plugin survives + assert.deepEqual(oc[ocKey], ["some-other-plugin"]); + assert.deepEqual(oc.compaction, { auto: true, buffer: 100 }); + assert.equal(oc.$schema, "https://opencode.ai/config.json"); + assert.equal(fs.existsSync(ocPluginDir), false); assert.throws(() => pluginInstall("claude"), /claude: install failed/); // The CLAUDE stub above keeps that assertion deterministic even on @@ -897,6 +930,107 @@ test("plugin install/remove roundtrips for pi/omp/codex/opencode under a fake HO fs.rmSync(home, { recursive: true, force: true }); }); +test("plugin install opencode without a live proxy: MCP shell skipped, native plugin still installed (#820)", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-oc-noproxy-")); + const ocFile = path.join(home, ".config/opencode/opencode.json"); + try { + await withEnv({ OPENCODE_CONFIG: ocFile, BILI_MCP_PROXY: undefined, XDG_STATE_HOME: path.join(home, "state") }, async () => { + const ocKey = pickPluginKey(detectOpencodeMajor()); + const msg = pluginInstall("opencode"); + assert.match(msg, /installed/); + assert.match(msg, /mcp\.bili skipped/); + assert.match(msg, /no bili proxy origin found/); + const data = JSON.parse(fs.readFileSync(ocFile, "utf8")) as { mcp?: unknown; compaction?: Record } & Record; + assert.equal(data.mcp, undefined); + assert.deepEqual(data[ocKey], [path.join(home, ".config/opencode/plugins/billion-context")]); + assert.deepEqual(data.compaction, { auto: false }); + assert.ok(fs.existsSync(path.join(home, ".config/opencode/plugins/billion-context/index.js"))); + assert.match(pluginRemove("opencode"), /removed/); + const after = JSON.parse(fs.readFileSync(ocFile, "utf8")) as Record; + assert.equal(after.mcp, undefined); + assert.equal(after[ocKey], undefined); + assert.equal(after.compaction, undefined); + }); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("plugin install opencode replaces legacy opencode-acp entries — array and object shapes (#918)", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-oc-acp-")); + const ocFile = path.join(home, ".config/opencode/opencode.json"); + fs.mkdirSync(path.dirname(ocFile), { recursive: true }); + try { + await withEnv({ OPENCODE_CONFIG: ocFile, BILI_MCP_PROXY: undefined, XDG_STATE_HOME: path.join(home, "state") }, async () => { + const ocKey = pickPluginKey(detectOpencodeMajor()); + const otherKey = ocKey === "plugin" ? "plugins" : "plugin"; + const dir = path.join(home, ".config/opencode/plugins/billion-context"); + // array shape: bare name, npm: alias, path (incl. deep entry path), versioned + fs.writeFileSync(ocFile, JSON.stringify({ + [ocKey]: ["opencode-acp", "npm:opencode-acp", "/opt/other-plugin", path.join(home, "ext/opencode-acp/index.js"), path.join(home, "ext2/opencode-acp/dist/index.js"), "opencode-acp@stable"], + })); + let msg = pluginInstall("opencode"); + assert.match(msg, /replaced opencode-acp plugin entries/); + let data = JSON.parse(fs.readFileSync(ocFile, "utf8")) as Record; + assert.deepEqual(data[ocKey], ["/opt/other-plugin", dir]); + assert.ok(fs.existsSync(`${ocFile}.bili-bak`)); + pluginRemove("opencode"); + + // object shape: version-map form (defensive; normalized to keys) + fs.writeFileSync(ocFile, JSON.stringify({ [ocKey]: { "opencode-acp": "stable", "other": "1.0" } })); + msg = pluginInstall("opencode"); + assert.match(msg, /replaced opencode-acp plugin entries \(opencode-acp\)/); + data = JSON.parse(fs.readFileSync(ocFile, "utf8")); + // sibling object entries survive as bare array specs + our dir + assert.deepEqual(data[ocKey], ["other", dir]); + pluginRemove("opencode"); + + // #927: legacy entries under the OTHER spelling are stripped too + fs.writeFileSync(ocFile, JSON.stringify({ [otherKey]: ["npm:opencode-acp", "/opt/other-plugin"], [ocKey]: ["/opt/mine"] })); + msg = pluginInstall("opencode"); + assert.match(msg, /replaced opencode-acp plugin entries \(npm:opencode-acp\)/); + data = JSON.parse(fs.readFileSync(ocFile, "utf8")); + assert.deepEqual(data[otherKey], ["/opt/other-plugin"]); + assert.deepEqual(data[ocKey], ["/opt/mine", dir]); + pluginRemove("opencode"); + + // no legacy entries -> no note, plugin untouched + fs.writeFileSync(ocFile, JSON.stringify({ [ocKey]: ["/opt/other-plugin"] })); + msg = pluginInstall("opencode"); + assert.doesNotMatch(msg, /replaced opencode-acp/); + assert.equal(msg.includes("other-plugin"), false); + }); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("plugin install/remove/status survive a non-object mcp in opencode.json (#809/N4)", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-oc-badmcp-")); + const ocFile = path.join(home, ".config/opencode/opencode.json"); + fs.mkdirSync(path.dirname(ocFile), { recursive: true }); + try { + await withEnv({ OPENCODE_CONFIG: ocFile, BILI_MCP_PROXY: undefined, XDG_STATE_HOME: path.join(home, "state") }, async () => { + const ocKey = pickPluginKey(detectOpencodeMajor()); + fs.writeFileSync(ocFile, JSON.stringify({ mcp: "totally-broken-string" })); + const msg = pluginInstall("opencode"); + assert.match(msg, /installed/); + let data = JSON.parse(fs.readFileSync(ocFile, "utf8")) as Record; + assert.deepEqual(data[ocKey], [path.join(home, ".config/opencode/plugins/billion-context")]); + assert.deepEqual(data.compaction, { auto: false }); + assert.equal(pluginStatusAll().find((r) => r.agent === "opencode")?.status, "installed"); + assert.doesNotThrow(() => pluginRemove("opencode")); + data = JSON.parse(fs.readFileSync(ocFile, "utf8")) as Record; + assert.equal(data[ocKey], undefined); + assert.equal(data.compaction, undefined); + assert.ok(!fs.existsSync(path.join(home, ".config/opencode/plugins/billion-context/index.js"))); + assert.equal(pluginStatusAll().find((r) => r.agent === "opencode")?.status, "not installed"); + }); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + test("omp plugin: scoped matching, existence check, overlay redirect (issue #392)", async () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-plugin-omp-")); const root = selfPackageRoot(); @@ -1026,9 +1160,17 @@ test("plugin opencode survives a non-object mcp (issue #836 / #809 N4)", async ( assert.equal(pluginStatusAll().find((r) => r.agent === "opencode")!.status, "not installed"); assert.equal(fs.readFileSync(ocFile, "utf8"), malformed); + // New installer (#919/#927): a bogus mcp key skips ONLY the mcp shell — + // the native plugin entry + compaction.auto still land, and sibling + // keys survive untouched. assert.doesNotThrow(() => pluginInstall("opencode")); assert.match(pluginInstall("opencode"), /skipped/i); - assert.equal(fs.readFileSync(ocFile, "utf8"), malformed); + const data = JSON.parse(fs.readFileSync(ocFile, "utf8")) as Record; + assert.equal(data.mcp, "bogus-string"); + assert.equal(data.other, 1); + const ocKey = pickPluginKey(detectOpencodeMajor()); + assert.deepEqual(data[ocKey], [path.join(ocDir, "plugins", "billion-context")]); + assert.deepEqual(data.compaction, { auto: false }); }); fs.rmSync(home, { recursive: true, force: true }); }); diff --git a/tests/plugin-bypass.test.ts b/tests/plugin-bypass.test.ts new file mode 100644 index 0000000000..1511e4c8ec --- /dev/null +++ b/tests/plugin-bypass.test.ts @@ -0,0 +1,108 @@ +// #920 legacy lane: requests marked x-bili-plugin-bypass are forwarded +// VERBATIM — no wire tool injection, no nudge, no session binding — because +// the absorbed opencode-acp owns compression for those sessions. +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { afterEach, beforeEach, describe, it } from "node:test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer, type ProxyOptions } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { _resetPluginStateForTest } from "../src/plugin.ts"; + +interface Harness { + proxyPort: number; + upstreamPort: number; + captured: { body: string; headers: Record }[]; + close(): Promise; +} + +async function startHarness(): Promise { + const captured: Harness["captured"] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + captured.push({ body: Buffer.concat(chunks).toString(), headers: { ...req.headers } }); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "msg_bypass", role: "assistant", content: [{ type: "text", text: "ok" }], usage: { input_tokens: 5, output_tokens: 1 } })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = upstream.address().port; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + _resetPluginStateForTest(); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "test-model": { context: 400_000 } } } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const proxyPort = proxy.address().port; + + return { + proxyPort, + upstreamPort, + captured, + close: async () => { + proxy.close(); + upstream.close(); + await Promise.allSettled([once(proxy, "close"), once(upstream, "close")]); + }, + }; +} + +let h: Harness | undefined; + +beforeEach(async () => { + h = await startHarness(); +}); +afterEach(async () => { + await h?.close(); + h = undefined; +}); + +describe("plugin bypass header (#920)", () => { + it("bypassed requests are forwarded verbatim — no tool injection, no nudge", async () => { + const body = { model: "test-model", stream: false, messages: [{ role: "user", content: "hello legacy session" }] }; + const resp = await fetch(`http://127.0.0.1:${h!.proxyPort}/bili/http://127.0.0.1:${h!.upstreamPort}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-bili-plugin-bypass": "1" }, + body: JSON.stringify(body), + }); + assert.equal(resp.status, 200); + assert.equal(h!.captured.length, 1); + const seen = JSON.parse(h!.captured[0]!.body) as { tools?: unknown[]; messages: unknown[] }; + assert.equal(seen.tools, undefined); + assert.deepEqual(seen.messages, body.messages); + assert.equal(h!.captured[0]!.headers["x-bili-plugin-bypass"], "1"); + }); + + it("without the header the same request gets the plugin pipeline (tools injected)", async () => { + const body = { model: "test-model", stream: false, messages: [{ role: "user", content: "hello new session" }] }; + const resp = await fetch(`http://127.0.0.1:${h!.proxyPort}/bili/http://127.0.0.1:${h!.upstreamPort}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify(body), + }); + assert.equal(resp.status, 200); + const seen = JSON.parse(h!.captured[0]!.body) as { tools?: { function?: { name?: string } }[] }; + const names = (seen.tools ?? []).map((t) => t.function?.name); + assert.ok(names.includes("compress"), `expected compress in ${JSON.stringify(names)}`); + }); +}); diff --git a/tsup.config.ts b/tsup.config.ts index 8b5b4a35c1..91e76068d9 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -1,7 +1,7 @@ import { defineConfig } from "tsup"; export default defineConfig({ - entry: ["src/index.ts", "src/mcp.ts", "src/agent/pi.ts", "src/agent/omp.ts", "src/agent/opencode.ts", "src/agent/dsh-acp.ts"], + entry: ["src/index.ts", "src/mcp.ts", "src/agent/pi.ts", "src/agent/pi-native.ts", "src/agent/omp.ts", "src/agent/opencode.ts", "src/agent/opencode-native.ts", "src/agent/dsh-acp.ts"], format: ["esm"], target: "node20", platform: "node",