Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/agent/opencode-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,15 @@ if (process.env.NODE_TEST_CONTEXT === undefined && nativeActive) {
state.ready = start();
}

export default { id: "billion-context-opencode-native", setup: createOpencodeV2Setup({ route: createNativeRoute(state) }) };
// #840: gate the traffic-touching route on nativeActive — mirroring pi's gated
// fetch intercept (src/agent/pi-native.ts). Under a `bili opencode` launch the
// launcher owns the proxy; an idle route would probe nothing, never rewrite,
// and still fire the spurious "proxy unavailable" warning on HTTPS (cert-MITM)
// launches where the launcher's proxy compresses fine. Without the route the
// shared setup degrades to launcher-mode detection (/bili/ URL or
// BILLION_CONTEXT_PROXY env) — the same shape as src/agent/opencode.ts.
const v2Setup = nativeActive
? createOpencodeV2Setup({ route: createNativeRoute(state) })
: createOpencodeV2Setup();

export default { id: "billion-context-opencode-native", setup: v2Setup };
110 changes: 110 additions & 0 deletions tests/opencode-native-overlap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import test from "node:test";
import assert from "node:assert/strict";

// #840 seam audit (opencode line): evaluate the REAL native entry under a
// `bili opencode` launch environment — the launcher owns the proxy, so the
// exported setup must carry NO route: request references stay untouched, the
// spurious "proxy unavailable" warning cannot fire, and the shared V2 setup
// degrades to launcher-mode detection (/bili/ URL or BILLION_CONTEXT_PROXY
// env). NODE_TEST_CONTEXT stays set so the bootstrap block cannot run
// regardless; the gate under test is the export shape itself.
process.env.NODE_TEST_CONTEXT = "1";
process.env.BILLION_CONTEXT_PROXY = "http://127.0.0.1:9999";
process.env.BILI_PROVIDER_REWRITES = '{"vllm":"http://127.0.0.1:9999/bili/http://upstream.local/v1"}';

import type { V2HttpRequestEvent, V2PluginContext } from "../src/agent/opencode-v2.ts";
import { ACP_TOOLS_OPENAI, ABSORB_TOOL_OPENAI } from "../src/compress-tool.ts";

const nativeDefault = (await import("../src/agent/opencode-native.ts")).default;
const MODEL_URL = "https://api.anthropic.com/v1/messages";
const EXPECTED_TOOLS = [...ACP_TOOLS_OPENAI.map((t) => t.function.name), ABSORB_TOOL_OPENAI.function.name];

interface MockCtx {
ctx: V2PluginContext;
hooks: Array<{ name: string; cb: (e: V2HttpRequestEvent) => void | Promise<void> }>;
tools: string[];
}

function makeCtx(): MockCtx {
const hooks: MockCtx["hooks"] = [];
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() {} };
},
},
};
return { ctx, hooks, tools };
}

test("overlap: native entry still exports a plugin object", () => {
assert.equal(nativeDefault.id, "billion-context-opencode-native");
assert.equal(typeof nativeDefault.setup, "function");
});

test("overlap: evaluation marks no native host (bootstrap skipped)", () => {
assert.notEqual(process.env.BILLION_CONTEXT_NATIVE, "opencode");
});

test("overlap: model-API request reference stays untouched and no spurious warning fires", async () => {
const { ctx, hooks, tools } = makeCtx();
const cleanup = await nativeDefault.setup(ctx);
const origError = console.error;
const warnings: string[] = [];
console.error = (...args: unknown[]) => {
warnings.push(args.join(" "));
};
try {
assert.deepEqual(hooks.map((h) => h.name), ["http.request"]);
assert.deepEqual(tools.sort(), [...EXPECTED_TOOLS].sort());
for (let i = 0; i < 2; i++) {
const req = new Request(MODEL_URL, { method: "POST", headers: { "content-type": "application/json" }, body: "{}" });
const e: V2HttpRequestEvent = { sessionID: "ses_ov", request: req };
await hooks[0].cb(e);
assert.equal(e.request, req, `fire ${i}: request reference must be the original`);
}
assert.equal(warnings.filter((w) => w.includes("proxy unavailable")).length, 0);
assert.equal(warnings.filter((w) => w.includes("bili-native")).length, 0);
} finally {
console.error = origError;
cleanup();
}
});

test("overlap: header stamping degrades to launcher-mode env detection", async () => {
const { ctx, hooks } = makeCtx();
const cleanup = await nativeDefault.setup(ctx);
try {
const req = new Request(MODEL_URL, { method: "POST" });
const e: V2HttpRequestEvent = { sessionID: "ses_env", request: req };
await hooks[0].cb(e);
assert.equal(req.headers.get("x-bili-plugin-conversation"), "ses_env");
assert.equal(req.headers.get("x-bili-plugin"), "opencode");
} finally {
cleanup();
}
});

test("overlap: /bili/-wrapped URLs detect the proxy from the URL (HTTP provider case)", async () => {
const { ctx, hooks } = makeCtx();
const cleanup = await nativeDefault.setup(ctx);
try {
const url = "http://127.0.0.1:4321/bili/http://upstream.local/v1/chat/completions";
const req = new Request(url, { method: "POST" });
const e: V2HttpRequestEvent = { sessionID: "ses_bili", request: req };
await hooks[0].cb(e);
assert.equal(e.request, req);
assert.equal(req.headers.get("x-bili-plugin-conversation"), "ses_bili");
assert.equal(req.headers.get("x-bili-plugin"), "opencode");
} finally {
cleanup();
}
});
42 changes: 42 additions & 0 deletions tests/pi-native-overlap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import test from "node:test";
import assert from "node:assert/strict";

// #840 seam audit (pi line): evaluate the REAL native entry under each
// `bili pi` launch environment variant — the launcher owns the proxy, so the
// global fetch patch (the only traffic-touching seam) must NOT be installed,
// no native host marker may be written, and the plugin export stays
// unconditional (tools remain available through launcher-mode detection).
// NODE_TEST_CONTEXT stays set so the bootstrap block cannot run regardless;
// query-suffixed imports force fresh module evaluations per variant.
process.env.NODE_TEST_CONTEXT = "1";

const INTERCEPT_FLAG = "__biliNativeFetchIntercept"; // must match native-intercept.ts
const origFetch = globalThis.fetch;

async function assertOverlapSeam(importSuffix: string): Promise<void> {
const mod = await import(`../src/agent/pi-native.ts?seam=${importSuffix}`);
assert.equal(typeof mod.default, "function", "plugin export stays unconditional");
assert.equal((globalThis as Record<string, unknown>)[INTERCEPT_FLAG], undefined, "fetch patch not installed");
assert.equal(globalThis.fetch, origFetch, "globalThis.fetch untouched");
assert.notEqual(process.env.BILLION_CONTEXT_NATIVE, "pi", "no native host marker");
}

test("overlap (BILLION_CONTEXT_PROXY): fetch unpatched, no marker", async () => {
delete process.env.BILI_PROVIDER_REWRITES;
process.env.BILLION_CONTEXT_PROXY = "http://127.0.0.1:9999";
try {
await assertOverlapSeam("proxy-env");
} finally {
delete process.env.BILLION_CONTEXT_PROXY;
}
});

test("overlap (BILI_PROVIDER_REWRITES): fetch unpatched, no marker", async () => {
delete process.env.BILLION_CONTEXT_PROXY;
process.env.BILI_PROVIDER_REWRITES = '{"qwen":"http://127.0.0.1:9999/bili/http://upstream.local/v1"}';
try {
await assertOverlapSeam("rewrites-env");
} finally {
delete process.env.BILI_PROVIDER_REWRITES;
}
});
Loading