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
44 changes: 36 additions & 8 deletions src/agent/opencode-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
//
// 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.
// unlike pi's post-failure respawn, each outgoing request checks
// `/__bili/health` of the current origin and respawns if dead. The verdict is
// TTL-cached per origin (#928): steady-state requests reuse the last result
// and re-probe only after HEALTH_PROBE_TTL_MS, so a healthy proxy costs no
// per-request RTT and a dead one is found within at most one TTL. 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; re-checked at least every TTL). 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
Expand Down Expand Up @@ -96,6 +99,10 @@ export function planNativeOpencode(env: NodeJS.ProcessEnv): { mode: "off" | "att

const HEALTH_TIMEOUT_MS = 1500;
const RESPAWN_COOLDOWN_MS = 15_000;
/** Probe-verdict trust window (#928). Far below RESPAWN_COOLDOWN_MS: that
* cooldown already assumes multi-second proxy stability, so a few-second
* detection horizon is consistent with it while removing the per-request RTT. */
const HEALTH_PROBE_TTL_MS = 2_000;

async function probeHealth(origin: string): Promise<boolean> {
try {
Expand All @@ -106,17 +113,38 @@ async function probeHealth(origin: string): Promise<boolean> {
}
}

/** Wrap a probe in a per-origin TTL cache (#928). Both verdicts are cached: a
* healthy hit skips the steady-state loopback RTT; a dead hit avoids re-paying
* the full HEALTH_TIMEOUT_MS on every request across the death+cooldown window.
* Stale entries for other origins are evicted on insert, keeping the map bounded. */
function withProbeTtl(probe: (origin: string) => Promise<boolean>, ttlMs: number): (origin: string) => Promise<boolean> {
const cache = new Map<string, { ok: boolean; at: number }>();
return (origin) => {
const now = Date.now();
const hit = cache.get(origin);
if (hit !== undefined && now - hit.at < ttlMs) return Promise.resolve(hit.ok);
return probe(origin).then((ok) => {
const t = Date.now();
for (const [key, entry] of cache) if (t - entry.at >= ttlMs) cache.delete(key);
cache.set(origin, { ok, at: t });
return ok;
});
};
}

export interface OpencodeNativeRouteDeps {
probe?: (origin: string) => Promise<boolean>;
/** Bootstrap retry interval when no live origin is held (tests shrink it). */
respawnCooldownMs?: number;
/** Probe-verdict cache TTL; defaults to HEALTH_PROBE_TTL_MS (tests shrink it). */
probeTtlMs?: 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<void> {
const probe = deps.probe ?? probeHealth;
const probe = withProbeTtl(deps.probe ?? probeHealth, deps.probeTtlMs ?? HEALTH_PROBE_TTL_MS);
const respawnCooldownMs = deps.respawnCooldownMs ?? RESPAWN_COOLDOWN_MS;
let warned = false;
let attachWarned = false;
Expand Down
48 changes: 48 additions & 0 deletions tests/opencode-native.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,51 @@ test("native route: attach mode leaves non-model requests untouched", async () =
assert.equal(e.request, req);
assert.equal(s.proxyBase, undefined);
});

// #928: the health verdict is TTL-cached per origin. Steady-state requests must
// reuse the last result instead of paying a loopback RTT on every request.
test("route: healthy origin triggers a single health probe across N steady-state requests (#928)", async () => {
const origin = "http://127.0.0.1:9999";
let calls = 0;
const state: NativeInterceptState = { origin, ready: Promise.resolve(origin) };
const route = createNativeRoute(state, { probe: async () => { calls++; return true; }, probeTtlMs: 10_000 });
const s: V2State = {};
for (let i = 0; i < 5; i++) {
const e: V2HttpRequestEvent = { request: new Request(MODEL_URL) };
await route(e, s);
assert.equal((e.request as Request).url, `${origin}/bili/${MODEL_URL}`);
}
assert.equal(calls, 1, "expected exactly one probe for N requests within the TTL");
});

test("route: re-probes once the probe TTL elapses rather than latching forever (#928)", async () => {
const origin = "http://127.0.0.1:9999";
let calls = 0;
const state: NativeInterceptState = { origin, ready: Promise.resolve(origin) };
const route = createNativeRoute(state, { probe: async () => { calls++; return true; }, probeTtlMs: 20 });
const s: V2State = {};
await route({ request: new Request(MODEL_URL) }, s);
assert.equal(calls, 1);
await sleep(45);
await route({ request: new Request(MODEL_URL) }, s);
assert.equal(calls, 2, "expected a fresh probe after the TTL elapsed");
});

test("route: a proxy that dies is detected within one TTL and degrades to direct (#928)", async () => {
const origin = "http://127.0.0.1:9999";
let alive = true;
let calls = 0;
const state: NativeInterceptState = { origin, ready: Promise.resolve(origin) };
const route = createNativeRoute(state, { probe: async () => { calls++; return alive; }, probeTtlMs: 30 });
const s: V2State = {};
const up: V2HttpRequestEvent = { request: new Request(MODEL_URL) };
await route(up, s);
assert.equal((up.request as Request).url, `${origin}/bili/${MODEL_URL}`);
assert.equal(calls, 1);
alive = false;
await sleep(45);
const down: V2HttpRequestEvent = { request: new Request(MODEL_URL) };
await route(down, s);
assert.equal((down.request as Request).url, MODEL_URL, "expected an unrewritten (direct) request after the proxy died");
assert.ok(calls >= 2, "expected a re-probe within one TTL that observed the dead proxy");
});
Loading