diff --git a/package.json b/package.json index 62348570a7..6a76ba1b52 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,13 @@ "name": "billion-context", "version": "0.1.114", "description": "Context-compression proxy that lets AI coding agents run for days — billions of tokens through one context window. Sits between any agent (Claude Code, Codex, Cursor, Aider, …) and its model API, folding consumed conversation into reversible, prefix-cache-friendly summaries. Zero per-agent code: just point your base URL at the proxy.", - "type": "module", - "main": "dist/index.js", - "bin": { + "type": "module", + "main": "dist/index.js", + "exports": { + ".": "./dist/index.js", + "./server": "./dist/agent/opencode-local.js" + }, + "bin": { "bili": "./dist/index.js", "bili-proxy": "./dist/index.js" }, diff --git a/src/agent/opencode-local.ts b/src/agent/opencode-local.ts new file mode 100644 index 0000000000..08cd03e950 --- /dev/null +++ b/src/agent/opencode-local.ts @@ -0,0 +1,139 @@ +// No-launcher OpenCode deployment (#809): `plugins: ["billion-context"]` in +// opencode.jsonc, resolved via package.json exports["./server"]. Unlike the +// launcher entry this one OWNS its proxy: on first need it spawns a loopback +// bili and rewrites provider requests to `/bili/`. Inert when the launcher +// already owns the proxy (BILLION_CONTEXT_PROXY set) or the kill switch is on. +// Loopback-only by construction — the host is hardcoded to 127.0.0.1 so the +// spawned proxy can never be reached off-machine. + +import { spawn } from "node:child_process"; +import { existsSync } from "node:fs"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import { findNodeRuntime, probeProxy } from "./shared.js"; +import { createOpencodeV2Setup, isProtocolSupported, pluginDisabled, type EnsureProxyResult, type V2PluginContext } from "./opencode-v2.js"; + +export const DEFAULT_LOCAL_PORT = 18787; +const SPAWN_POLL_ATTEMPTS = 25; +const SPAWN_POLL_INTERVAL_MS = 200; + +interface LocalOptionsResult { + proxyBase: string; + warnings: string[]; +} + +export function parseLocalOptions(raw: unknown): LocalOptionsResult { + const warnings: string[] = []; + let port = DEFAULT_LOCAL_PORT; + if (raw && typeof raw === "object") { + const value = (raw as Record).port; + if (value !== undefined) { + const num = typeof value === "number" ? value : (/^\d+$/.test(String(value)) ? Number.parseInt(String(value), 10) : NaN); + if (Number.isInteger(num) && num >= 1 && num <= 65535) { + port = num; + } else { + warnings.push(`billion-context: invalid plugin option "port" (${JSON.stringify(value)}) — using default ${DEFAULT_LOCAL_PORT}`); + } + } + } + return { proxyBase: `http://127.0.0.1:${port}`, warnings }; +} + +export function rewriteToBili(url: string, proxyBase: string): string | undefined { + const prefix = `${proxyBase}/bili/`; + if (url.startsWith(prefix)) return url; + if (/^https?:\/\//i.test(url)) return `${prefix}${url}`; + return undefined; +} + +export function resolvePackageRoot(): string { + // Built layout: /dist/agent/opencode-local.js → two levels up is . + return path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", ".."); +} + +export function buildSpawnArgs(packageRoot: string, port: number): string[] { + // --no-auto-update is MANDATORY here: the npm cache strips .git, so the + // git-checkout guard in update.ts (#580) never fires and a self-updating + // proxy would race the host mid-request. --host pins loopback explicitly. + return [path.join(packageRoot, "dist", "index.js"), "start", "--port", String(port), "--host", "127.0.0.1", "--no-auto-update"]; +} + +function portOf(proxyBase: string): number { + try { + const p = new URL(proxyBase).port; + return p !== "" ? Number(p) : DEFAULT_LOCAL_PORT; + } catch { + return DEFAULT_LOCAL_PORT; + } +} + +function sleep(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +interface EnsureProxyDeps { + spawn?: typeof spawn; + packageRoot?: () => string; +} + +export async function ensureProxyLoopback(base: string, deps: EnsureProxyDeps = {}): Promise { + const spawnFn = deps.spawn ?? spawn; + const first = await probeProxy(base); + if (first.connected && first.identityOk) { + return { connected: true, identityOk: true, protocolSupported: isProtocolSupported(first.protocolVersion), version: first.version }; + } + if (first.connected) { + console.error(`[bili-opencode-local] ${base} answered but is not a billion-context proxy — refusing to adopt a foreign service, running inert`); + return { connected: false, identityOk: false }; + } + const packageRoot = deps.packageRoot ? deps.packageRoot() : resolvePackageRoot(); + const indexJs = path.join(packageRoot, "dist", "index.js"); + if (!existsSync(indexJs)) { + console.error(`[bili-opencode-local] cannot start proxy: missing ${indexJs} — running inert`); + return { connected: false, identityOk: false }; + } + const node = findNodeRuntime(); + if (!node) { + console.error("[bili-opencode-local] no Node runtime found (process.execPath is not node and 'node' is not on PATH; set BILLION_CONTEXT_NODE to override) — cannot start proxy, running inert"); + return { connected: false, identityOk: false }; + } + try { + const child = spawnFn(node, buildSpawnArgs(packageRoot, portOf(base)), { detached: true, stdio: "ignore", cwd: packageRoot }); + // spawn() reports ENOENT/EACCES ASYNCHRONOUSLY via 'error'; an unhandled + // 'error' event throws and takes down the host process. Observe it so a + // bad runtime degrades to inert (the poll below already times out). + child.on?.("error", (err) => { + console.error(`[bili-opencode-local] proxy child error: ${err instanceof Error ? err.message : String(err)} — running inert`); + }); + child.unref?.(); + } catch (err) { + console.error(`[bili-opencode-local] spawn failed: ${err instanceof Error ? err.message : String(err)} — running inert`); + return { connected: false, identityOk: false }; + } + for (let i = 0; i < SPAWN_POLL_ATTEMPTS; i++) { + await sleep(SPAWN_POLL_INTERVAL_MS); + const probe = await probeProxy(base); + if (probe.connected && probe.identityOk) { + return { connected: true, identityOk: true, protocolSupported: isProtocolSupported(probe.protocolVersion), version: probe.version }; + } + } + console.error(`[bili-opencode-local] proxy did not come up at ${base} within timeout — running inert`); + return { connected: false, identityOk: false }; +} + +const plugin = { + id: "billion-context-opencode-local", + setup: async (ctx: V2PluginContext, maybeOptions?: unknown): Promise<() => void> => { + const rawOptions = (ctx as { options?: unknown }).options ?? maybeOptions; + const parsed = parseLocalOptions(rawOptions); + for (const warning of parsed.warnings) console.warn(warning); + return createOpencodeV2Setup({ + active: () => !process.env.BILLION_CONTEXT_PROXY && !pluginDisabled(), + resolveInitialProxyBase: () => parsed.proxyBase, + ensureProxy: ensureProxyLoopback, + rewriteRequestUrl: rewriteToBili, + })(ctx); + }, +}; + +export default plugin; diff --git a/src/agent/opencode-v2.ts b/src/agent/opencode-v2.ts new file mode 100644 index 0000000000..4f3da27ab4 --- /dev/null +++ b/src/agent/opencode-v2.ts @@ -0,0 +1,294 @@ +// Shared OpenCode 2.0 (V2 plugin API) factory for the billion-context proxy. +// Two host deployments share one implementation so their wire behavior cannot +// drift (#809): the launcher entry (src/agent/opencode.ts, activates only when +// BILLION_CONTEXT_PROXY is set) and the no-launcher npm-plugin entry +// (src/agent/opencode-local.ts, spawns its own loopback proxy). Both call +// createOpencodeV2Setup with a mode-specific config; everything else — tool +// registration, header stamping, window refresh, compaction reporting, cleanup +// — lives here once. +// +// Same protocol-client contract as the V1 server in opencode.ts: no acp-kernel, +// no compression logic — the proxy is the single compression authority. +// Structural types only (no @opencode/plugin import) so this stays loadable +// under both host generations. +// +// 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). 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 { forwardTool, reportCompactionBoundary } from "./shared.js"; + +export type V2Registration = { dispose?: () => void | Promise }; + +// e.request is a WHATWG Request at runtime (OpenCode 2.0.x http.request seam): +// its .url is a readonly getter, so routing rewrites the request REFERENCE +// (which the seam allows swapping wholesale) rather than writing .url (which +// throws TypeError in strict/ESM and fails every session through the hook). +export interface V2HttpRequestEvent { + sessionID?: unknown; + model?: { providerID?: unknown; id?: unknown }; + request?: Request; +} + +export 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; +} + +export interface V2CatalogModelEntry { + providerID?: unknown; + id?: unknown; + limit?: { context?: unknown }; +} + +export 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 }; +} + +export interface V2State { + proxyBase?: string; + windows?: Map; + windowsAt?: number; +} + +const WINDOW_REFRESH_MS = 60000; + +// Kill switch = fully inert (same semantics as detectProxyBase): gates header stamping, tool forwarding, compaction reporting. +export 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, +})); + +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 + } + })(); +} + +function stampHeaders(e: V2HttpRequestEvent, state: V2State): 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)); + } +} + +// The proxy and this plugin ship as one package, so protocol skew is rare; the +// check is a safety net for the skew window where a stale cached copy pairs a +// new proxy with an old plugin (or vice versa). An UNKNOWN or MISSING +// protocolVersion disables native tool registration (the embedded schemas may +// not match) while still letting header-stamped requests route through the +// proxy — fail closed on compatibility we cannot confirm. +export const SUPPORTED_PROTOCOL_VERSIONS = new Set([1]); + +export function isProtocolSupported(version: number | undefined): boolean { + return typeof version === "number" && SUPPORTED_PROTOCOL_VERSIONS.has(version); +} + +export interface EnsureProxyResult { + connected: boolean; + identityOk?: boolean; + protocolSupported?: boolean; + version?: string; +} + +export interface OpencodeV2Config { + /** false → fully inert (no hooks, no tools, no spawn). Local uses this to + * stand down when the launcher owns the proxy or the kill switch is set. */ + active?: () => boolean; + /** Eagerly resolve the proxy base at setup time (local: options → default). */ + resolveInitialProxyBase?: () => string | undefined | Promise; + /** Confirm the resolved base is a live bili proxy, spawning one on miss + * (local only). Launcher omits this — it assumes the launcher started it. */ + ensureProxy?: (base: string) => Promise; + /** Rewrite the outgoing request URL to route through the proxy (local: + * idempotent `/bili/` prefix). Must be idempotent — the hook re-runs per + * request. */ + rewriteRequestUrl?: (url: string, proxyBase: string) => string | undefined; + /** Lazily resolve the proxy base on first request (launcher: `/bili/` URL + * or env detection). Mutually exclusive in practice with eager resolution. */ + lazyResolveProxyBase?: (url: string) => string | undefined; +} + +export function createOpencodeV2Setup(config: OpencodeV2Config): (ctx: V2PluginContext) => Promise<() => void> { + return async (ctx: V2PluginContext): Promise<() => void> => { + if (config.active && !config.active()) return () => {}; + const ac = new AbortController(); + const state: V2State = {}; + const registrations: V2Registration[] = []; + let registerTools = true; + + if (config.resolveInitialProxyBase) { + const base = await config.resolveInitialProxyBase(); + if (base) { + if (config.ensureProxy) { + const ensured = await config.ensureProxy(base); + if (!ensured.connected) { + // Loud by design (v1): a local deployment that cannot reach + // or start its proxy degrades to inert passthrough rather + // than silently claiming compression it never performs. + console.error("[bili-opencode-local] proxy unreachable at " + base + " — running inert (requests pass through uncompressed)"); + } else { + state.proxyBase = base; + registerTools = ensured.protocolSupported !== false; + if (!registerTools) console.warn("[bili-opencode-local] proxy protocol unsupported — native tools disabled"); + } + } else { + state.proxyBase = base; + } + } + } + + const httpRequestHook = async (e: V2HttpRequestEvent): Promise => { + if (pluginDisabled()) return; + try { + const url = e.request?.url; + if (typeof url !== "string") return; + if (!state.proxyBase && config.lazyResolveProxyBase) { + state.proxyBase = config.lazyResolveProxyBase(url); + } + if (!state.proxyBase) return; + if (config.rewriteRequestUrl) { + const next = config.rewriteRequestUrl(url, state.proxyBase); + // Swap the request REFERENCE to one targeting the proxy; never + // write e.request.url (readonly getter -> TypeError -> dead session). + // method/headers/body carry over from the original Request. + if (next && next !== url && e.request) { + try { + e.request = new Request(next, e.request); + } catch { + // body not reconstructible (e.g. consumed stream) — leave + // the request untouched so it proceeds direct (uncompressed) + // rather than crashing the host session. + } + } + } + refreshWindows(ctx, state); + stampHeaders(e, state); + } catch { + // A throwing hook rejects the host's outgoing request and fails the + // whole session. billion-context must never take the host down — + // swallow and let the request proceed uncompressed. + } + }; + + const hookReg = await ctx.session?.hook?.("http.request", httpRequestHook); + if (hookReg) registrations.push(hookReg); + + if (registerTools) { + 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 ?? process.env.BILLION_CONTEXT_PROXY; + 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 ?? process.env.BILLION_CONTEXT_PROXY; + 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 58926ef8f4..4e43e13c87 100644 --- a/src/agent/opencode.ts +++ b/src/agent/opencode.ts @@ -7,9 +7,13 @@ // - 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 +// +// The OpenCode 2.0 (V2) half is shared with the no-launcher npm-plugin entry +// (#809) in src/agent/opencode-v2.ts; only the launcher-specific lazy proxy +// detection is wired here. -import { ACP_TOOLS_OPENAI, ABSORB_TOOL_OPENAI } from "../compress-tool.js"; -import { fetchProxyVersion, forwardTool, proxyBaseFromEnv, proxyBaseFromUrl, reportCompactionBoundary } from "./shared.js"; +import { fetchProxyVersion, proxyBaseFromEnv, proxyBaseFromUrl } from "./shared.js"; +import { createOpencodeV2Setup } from "./opencode-v2.js"; interface OpencodeCommandConfig { template: string; @@ -127,206 +131,8 @@ const server = async (ctx: OpencodePluginContext): Promise => { }; }; -// --------------------------------------------------------------------------- -// 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({ + lazyResolveProxyBase: (url) => proxyBaseFromUrl(url) ?? proxyBaseFromEnv(), +}); export default { id: "billion-context-opencode", setup, server }; diff --git a/src/agent/shared.ts b/src/agent/shared.ts index c6bcfc99fb..0c2c7e7394 100644 --- a/src/agent/shared.ts +++ b/src/agent/shared.ts @@ -5,6 +5,9 @@ // source of truth), (3) forwards tool executes, (4) reads status. Same // package as the proxy ⇒ same version ⇒ no kernel-skew bug class. +import path from "node:path"; +import { existsSync } from "node:fs"; + export type ManifestTool = { name: string; description?: string; @@ -156,3 +159,63 @@ export async function fetchProxyVersion(proxyBase: string): Promise 0 ? version : undefined; } + +/** Identity + protocol probe for lazy local spawn (#809): one manifest GET that + * both confirms the origin is OUR proxy (not a foreign service squatting the + * port) and reads its protocol/version. Soft-fail by design — `connected:false` + * covers both "nothing listening" and "something else answered", so callers can + * distinguish adopt-vs-spawn-vs-stand-down without try/catch. */ +export interface ProxyProbe { + connected: boolean; + identityOk: boolean; + protocolVersion?: number; + version?: string; +} + +export async function probeProxy(proxyBase: string, timeoutMs = STATUS_TIMEOUT_MS): Promise { + try { + const { ok, json } = await fetchJson(`${proxyBase}/__bili/plugin/manifest`, undefined, timeoutMs); + if (!ok || !json || typeof json !== "object") return { connected: false, identityOk: false }; + const data = json as { proxy?: unknown; protocolVersion?: unknown; version?: unknown }; + return { + connected: true, + identityOk: data.proxy === "billion-context", + protocolVersion: typeof data.protocolVersion === "number" ? data.protocolVersion : undefined, + version: typeof data.version === "string" ? data.version : undefined, + }; + } catch { + return { connected: false, identityOk: false }; + } +} + +/** Resolve a real Node runtime for spawning the proxy from INSIDE a host process + * (#809). process.execPath is NOT reliable there: under a native host binary + * (e.g. the Mach-O arm64 OpenCode CLI) it points at the host executable, not Node, + * so spawning it with dist/index.js just prints CLI help and exits. Order: + * execPath if it is node -> BILLION_CONTEXT_NODE override -> PATH walk -> undefined + * (caller fails loud-and-inert). Preferring an already-running Node keeps the + * launcher path byte-identical regardless of env. Dependency-injectable so every + * branch is testable. REUSE for ALL in-host proxy spawns (pi-native #706 / #519) — + * never assume process.execPath is Node. */ +export interface NodeLookupOpts { + execPath?: string; + pathEnv?: string; + exists?: (p: string) => boolean; +} + +export function findNodeRuntime(opts: NodeLookupOpts = {}): string | undefined { + const execPath = opts.execPath ?? process.execPath; + const pathEnv = opts.pathEnv ?? process.env.PATH ?? ""; + const exists = opts.exists ?? existsSync; + if (/^node(\.exe)?$/i.test(path.basename(execPath))) return execPath; + const override = process.env.BILLION_CONTEXT_NODE; + if (override && override.length > 0 && exists(override)) return override; + for (const dir of pathEnv.split(path.delimiter)) { + if (!dir) continue; + for (const name of ["node.exe", "node"]) { + const candidate = path.join(dir, name); + if (exists(candidate)) return candidate; + } + } + return undefined; +} diff --git a/src/launcher.ts b/src/launcher.ts index 68f02530cd..36c05ed47a 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -34,6 +34,7 @@ import path from "node:path"; import { pathToFileURL } from "node:url"; import { execFileSync, spawn, type StdioOptions } from "node:child_process"; import { DEFAULT_MITM_DOMAINS } from "./mitm.js"; +import { findNodeRuntime } from "./agent/shared.js"; import { claimStartingMarker, clearStartingMarker, @@ -1938,6 +1939,11 @@ export async function ensureProxyRunning( const port = opts.port > 0 ? opts.port : await pickEphemeralPort(opts.host); const script = process.argv[1]; if (!script) throw new Error("bili: cannot resolve launcher script path"); + // #706/#809: never assume process.execPath is Node — pi-native spawn-in-plugin + // reuses this path under the host binary. In the CLI/daemon context execPath + // IS node, so findNodeRuntime() resolves to the same value as before. + const node = findNodeRuntime(); + if (!node) throw new Error("bili: cannot resolve a Node runtime to spawn the proxy (set BILLION_CONTEXT_NODE)"); const logPath = path.join(os.tmpdir(), `bili-proxy-${port}.log`); const logFd = fs.openSync(logPath, "a"); // #707: publish the starting marker BEFORE spawning so concurrent launches @@ -1970,7 +1976,7 @@ export async function ensureProxyRunning( let child: SpawnChild; try { child = spawnImpl( - process.execPath, + node, [script, ...proxyStartArgs({ ...opts, port })], { detached: true, diff --git a/tests/opencode-local.test.ts b/tests/opencode-local.test.ts new file mode 100644 index 0000000000..d35c58c035 --- /dev/null +++ b/tests/opencode-local.test.ts @@ -0,0 +1,263 @@ +import assert from "node:assert"; +import fs from "node:fs"; +import http from "node:http"; +import { EventEmitter, once } from "node:events"; +import os from "node:os"; +import path from "node:path"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import biliLocalPlugin, { + DEFAULT_LOCAL_PORT, + buildSpawnArgs, + ensureProxyLoopback, + parseLocalOptions, + resolvePackageRoot, + rewriteToBili, +} from "../src/agent/opencode-local.ts"; +import { findNodeRuntime } from "../src/agent/shared.ts"; +import { isProtocolSupported } from "../src/agent/opencode-v2.ts"; + +// Real WHATWG Request (not a plain object): the OpenCode seam hands us a Request +// whose .url is readonly, so the mock must mirror that or it cannot catch a +// regression that writes .url directly. +type HookEvent = { sessionID?: unknown; model?: unknown; request?: Request }; +type HookCb = (e: HookEvent) => void | Promise; +type AddedTool = { name: string; input: unknown; options?: Record }; + +function makeLocalFakeCtx() { + let cb: HookCb | undefined; + const addedTools: AddedTool[] = []; + const hookNames: string[] = []; + const ctx = { + session: { + hook: async (name: string, c: HookCb) => { + hookNames.push(name); + cb = c; + return { dispose: () => {} }; + }, + }, + tool: { + transform: async (editor: (ed: { add: (t: AddedTool) => void }) => void) => { + editor({ add: (t) => addedTools.push(t) }); + return { dispose: () => {} }; + }, + }, + catalog: { + model: { list: async () => ({ data: [{ providerID: "qwen", id: "m1", limit: { context: 262144 } }] }) }, + }, + }; + const fire = async (input: string | Request, sessionID: string, model?: { providerID?: unknown; id?: unknown }): Promise<{ headers: Record; url: string; before: Request; after: Request | null; swapped: boolean }> => { + const before = typeof input === "string" + ? new Request(input, { method: "POST", headers: { "content-type": "application/json" }, body: "{}" }) + : input; + const evt: HookEvent = { sessionID, model, request: before }; + await cb!(evt); + const out: Record = {}; + const h = evt.request?.headers; + if (h) for (const [k, v] of h.entries()) out[k] = v; + return { headers: out, url: evt.request?.url ?? "", before, after: evt.request ?? null, swapped: evt.request !== before }; + }; + return { ctx, fire, addedTools, hookNames }; +} + +async function withEnv(vars: Record, fn: () => void | 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; + } + return Promise.resolve(fn()).finally(() => { + for (const [k, v] of saved) { + if (v === undefined) delete process.env[k]; + else process.env[k] = v; + } + }); +} + +test("object export: .id/.setup for the no-launcher entry", () => { + assert.equal(biliLocalPlugin.id, "billion-context-opencode-local"); + assert.equal(typeof biliLocalPlugin.setup, "function"); +}); + +test("rewriteToBili prefixes absolute http(s) upstreams and is idempotent", () => { + const base = "http://127.0.0.1:18787"; + assert.equal(rewriteToBili("http://api.openai.com/v1/chat/completions", base), `${base}/bili/http://api.openai.com/v1/chat/completions`); + assert.equal(rewriteToBili("https://api.anthropic.com/v1/messages", base), `${base}/bili/https://api.anthropic.com/v1/messages`); + const q = "https://up.example/v1/x?a=1&b=2 c#f"; + assert.equal(rewriteToBili(q, base), `${base}/bili/${q}`); + const oncePrefixed = rewriteToBili("http://api.openai.com/v1", base)!; + assert.equal(rewriteToBili(oncePrefixed, base), oncePrefixed); +}); + +test("rewriteToBili leaves non-http(s) URLs untouched", () => { + const base = "http://127.0.0.1:18787"; + assert.equal(rewriteToBili("/relative/path", base), undefined); + assert.equal(rewriteToBili("ftp://x/y", base), undefined); +}); + +test("parseLocalOptions defaults and validates port", () => { + assert.deepEqual(parseLocalOptions(undefined), { proxyBase: `http://127.0.0.1:${DEFAULT_LOCAL_PORT}`, warnings: [] }); + assert.deepEqual(parseLocalOptions({}), { proxyBase: `http://127.0.0.1:${DEFAULT_LOCAL_PORT}`, warnings: [] }); + assert.equal(parseLocalOptions({ port: 9000 }).proxyBase, "http://127.0.0.1:9000"); + assert.equal(parseLocalOptions({ port: "9000" }).proxyBase, "http://127.0.0.1:9000"); + for (const bad of ["18787x", -1, 0, 99999, 1878.5]) { + const r = parseLocalOptions({ port: bad }); + assert.equal(r.proxyBase, `http://127.0.0.1:${DEFAULT_LOCAL_PORT}`); + assert.equal(r.warnings.length, 1); + assert.match(r.warnings[0]!, /invalid plugin option "port"/); + } + assert.equal(parseLocalOptions("junk").proxyBase, `http://127.0.0.1:${DEFAULT_LOCAL_PORT}`); +}); + +test("buildSpawnArgs pins loopback, disables auto-update, uses the start subcommand", () => { + const args = buildSpawnArgs("/some/root", 18787); + assert.deepEqual(args, [path.join("/some/root", "dist", "index.js"), "start", "--port", "18787", "--host", "127.0.0.1", "--no-auto-update"]); + assert.ok(args.includes("--no-auto-update")); + assert.ok(args.includes("127.0.0.1")); + assert.ok(args.includes("start")); +}); + +test("resolvePackageRoot is an absolute path", () => { + assert.ok(path.isAbsolute(resolvePackageRoot())); +}); + +test("local mode adopts a live proxy, stamps headers, rewrites url idempotently", async () => { + const server = http.createServer((req, res) => { + if ((req.url ?? "") === "/__bili/plugin/manifest" && req.method === "GET") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true, protocolVersion: 1, proxy: "billion-context", version: "99.0.0-test" })); + return; + } + res.writeHead(404); + res.end("{}"); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const port = (server.address() as { port: number }).port; + const fake = makeLocalFakeCtx(); + const cleanup = await biliLocalPlugin.setup(fake.ctx as never, { port }); + try { + assert.deepEqual(fake.hookNames, ["http.request"]); + assert.ok(fake.addedTools.length > 0, "native tools registered against the adopted proxy"); + const r1 = await fake.fire("https://api.openai.com/v1/chat/completions", "s1", { providerID: "qwen", id: "m1" }); + assert.equal(r1.headers["x-bili-plugin"], "opencode"); + assert.equal(r1.headers["x-bili-plugin-conversation"], "s1"); + assert.equal(r1.url, `http://127.0.0.1:${port}/bili/https://api.openai.com/v1/chat/completions`); + const r2 = await fake.fire(r1.url, "s1"); + assert.equal(r2.url, r1.url); + } finally { + cleanup(); + await new Promise((resolve) => server.close(() => resolve())); + } +}); + +test("local mode stands down inert when the launcher owns the proxy", async () => { + const fake = makeLocalFakeCtx(); + await withEnv({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:9999" }, async () => { + const cleanup = await biliLocalPlugin.setup(fake.ctx as never, { port: DEFAULT_LOCAL_PORT }); + assert.ok(typeof cleanup === "function"); + assert.deepEqual(fake.hookNames, [], "no hooks registered while standing down"); + assert.equal(fake.addedTools.length, 0, "no tools registered while standing down"); + cleanup(); + }); +}); + +test("local mode stands down inert when the kill switch is set", async () => { + const fake = makeLocalFakeCtx(); + await withEnv({ BILLION_CONTEXT_PROXY: undefined, BILLION_CONTEXT_PLUGIN: "0" }, async () => { + const cleanup = await biliLocalPlugin.setup(fake.ctx as never, { port: DEFAULT_LOCAL_PORT }); + assert.deepEqual(fake.hookNames, []); + assert.equal(fake.addedTools.length, 0); + cleanup(); + }); +}); + +test("local mode routes via request-reference swap on a real WHATWG Request (never writes readonly .url)", async () => { + const server = http.createServer((req, res) => { + if ((req.url ?? "") === "/__bili/plugin/manifest" && req.method === "GET") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true, protocolVersion: 1, proxy: "billion-context", version: "99.0.0-test" })); + return; + } + res.writeHead(404); + res.end("{}"); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const port = (server.address() as { port: number }).port; + const base = `http://127.0.0.1:${port}`; + const fake = makeLocalFakeCtx(); + const cleanup = await biliLocalPlugin.setup(fake.ctx as never, { port }); + try { + const r = await fake.fire("https://api.openai.com/v1/chat/completions", "s1"); + assert.ok(r.swapped, "hook replaced the request reference instead of writing readonly .url"); + assert.notStrictEqual(r.after, r.before); + assert.equal(r.after!.url, `${base}/bili/https://api.openai.com/v1/chat/completions`); + assert.equal(r.before.method, "POST"); + } finally { + cleanup(); + await new Promise((resolve) => server.close(() => resolve())); + } +}); + +test("findNodeRuntime: BILLION_CONTEXT_NODE override wins when it exists", () => + withEnv({ BILLION_CONTEXT_NODE: "/opt/custom/node" }, () => { + assert.equal(findNodeRuntime({ execPath: "/usr/bin/opencode", pathEnv: "", exists: (p) => p === "/opt/custom/node" }), "/opt/custom/node"); + })); + +test("findNodeRuntime: uses process.execPath when it is node", () => + withEnv({ BILLION_CONTEXT_NODE: undefined }, () => { + assert.equal(findNodeRuntime({ execPath: "/usr/local/bin/node", pathEnv: "" }), "/usr/local/bin/node"); + })); + +test("findNodeRuntime: walks PATH for node when execPath is a native binary", () => + withEnv({ BILLION_CONTEXT_NODE: undefined }, () => { + const dirs = ["/opt/a", "/opt/b"]; + const hit = path.join(dirs[1], "node"); + assert.equal(findNodeRuntime({ execPath: "/usr/local/bin/opencode", pathEnv: dirs.join(path.delimiter), exists: (p) => p === hit }), hit); + })); + +test("findNodeRuntime: returns undefined when nothing resolves", () => + withEnv({ BILLION_CONTEXT_NODE: undefined }, () => { + assert.equal(findNodeRuntime({ execPath: "/usr/local/bin/opencode", pathEnv: "/a:/b", exists: () => false }), undefined); + })); + +test("findNodeRuntime: running-under-node wins over BILLION_CONTEXT_NODE override", () => + withEnv({ BILLION_CONTEXT_NODE: "/opt/custom/node" }, () => { + assert.equal(findNodeRuntime({ execPath: "/usr/local/bin/node", pathEnv: "" }), "/usr/local/bin/node"); + })); + +test("isProtocolSupported: known version supported", () => { + assert.equal(isProtocolSupported(1), true); +}); + +test("isProtocolSupported: unknown numeric version fails closed", () => { + assert.equal(isProtocolSupported(2), false); +}); + +test("isProtocolSupported: missing protocolVersion fails closed", () => { + assert.equal(isProtocolSupported(undefined), false); +}); + +test("ensureProxyLoopback observes async spawn 'error' and degrades inert instead of crashing the host", async () => { + const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), "bili-f3-")); + try { + fs.mkdirSync(path.join(tmpRoot, "dist"), { recursive: true }); + fs.writeFileSync(path.join(tmpRoot, "dist", "index.js"), "// stub\n"); + const child = Object.assign(new EventEmitter(), { unref: () => {} }); + const fakeSpawn = (() => { + setTimeout(() => child.emit("error", new Error("spawn ENOENT")), 0); + return child; + }); + const result = await ensureProxyLoopback("http://127.0.0.1:18799", { + spawn: fakeSpawn as unknown as typeof import("node:child_process").spawn, + packageRoot: () => tmpRoot, + }); + assert.deepEqual(result, { connected: false, identityOk: false }); + } finally { + fs.rmSync(tmpRoot, { recursive: true, force: true }); + } +}); diff --git a/tsup.config.ts b/tsup.config.ts index 8b5b4a35c1..9dd6abf807 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/omp.ts", "src/agent/opencode.ts", "src/agent/opencode-local.ts", "src/agent/dsh-acp.ts"], format: ["esm"], target: "node20", platform: "node",