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
41 changes: 32 additions & 9 deletions src/agent/pi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function noProxyWarning(agent: string): string {

const RETRY_INTERVAL_MS = 10000;

type RegisterState = { sid?: string; toolsFor?: string; toolsReady?: boolean; pending?: Promise<void>; retryAt?: number; identityAt?: string; retryIntervalMs: number };
type RegisterState = { sid?: string; toolsFor?: string; toolsReady?: boolean; pending?: Promise<void>; retryAt?: number; identityAt?: string; retryIntervalMs: number; manifestPrime?: { base: string; tools: Promise<ManifestTool[] | undefined> } };

// omp never emits before_provider_headers, so the x-bili-plugin marker cannot
// be stamped per request. Register the conversation id once (after tools are
Expand Down Expand Up @@ -214,13 +214,22 @@ async function registerTools(pi: ExtensionAPI, ctx: Ctx, state: RegisterState, a
if (state.retryAt !== undefined && Date.now() < state.retryAt) return;
const wait = state.retryIntervalMs;
state.pending = (async () => {
let tools: ManifestTool[];
try {
tools = await fetchManifest(proxyBase);
} catch (err) {
state.retryAt = Date.now() + wait;
console.error(`bili-plugin(${agent}): manifest fetch failed: ${err instanceof Error ? err.message : String(err)} — retrying in ${wait / 1000}s`);
return;
// #1217: consume the load-time manifest prime (see factory). It is
// taken exactly once and only for the same proxy origin; a failed
// prime resolves to undefined and falls through to the normal fetch
// below (same failure path, same log, then retry-throttled).
let tools: ManifestTool[] | undefined;
const prime = state.manifestPrime;
state.manifestPrime = undefined;
if (prime !== undefined && prime.base === proxyBase) tools = await prime.tools;
if (tools === undefined) {
try {
tools = await fetchManifest(proxyBase);
} catch (err) {
state.retryAt = Date.now() + wait;
console.error(`bili-plugin(${agent}): manifest fetch failed: ${err instanceof Error ? err.message : String(err)} — retrying in ${wait / 1000}s`);
return;
}
}
try {
// toolsFor (not sid) guards the register loop: a retry after a
Expand Down Expand Up @@ -266,6 +275,18 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM
return function biliPlugin(pi: ExtensionAPI): void {
const agent = agentName(agentOverride);
const state: RegisterState = { retryIntervalMs: opts?.retryIntervalMs ?? RETRY_INTERVAL_MS };
// #1217: -p single-shot fires round 1 before session_start's manifest
// fetch can resolve, leaving the request unmarked → anonymous
// proxy-mode session. Prime the fetch at load time: the launcher
// health-checks the proxy before spawning pi, so by session_start the
// prime has almost always settled and toolsReady flips in microtasks
// — ahead of round 1. Native mode (#519) sets BILLION_CONTEXT_PROXY
// only after async bootstrap, so no prime exists there and round 1
// stays on wire mode (residual; the host would have to await us).
const primeBase = detectProxyBase(undefined);
if (primeBase !== undefined) {
state.manifestPrime = { base: primeBase, tools: fetchManifest(primeBase).then((t) => t, () => undefined) };
}
// #535: file-free routing — override provider baseUrls at load from
// the launcher-passed manifest (see buildPiEnv). registerProvider is
// queued during initial extension load and applied before any model
Expand Down Expand Up @@ -486,7 +507,9 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM
}
// The x-bili-plugin marker tells the proxy "the client owns the
// ACP tools natively — skip wire-level injection". Ownership is
// claimed only once tools are registered (#162).
// claimed only once tools are registered (#162). In launcher mode
// the manifest fetch is primed at extension load time (#1217), so
// by round 1 registration has almost always completed.
if (state.toolsReady === true) {
const sid = sessionIdOf(ctx);
if (sid !== undefined) headers["x-bili-plugin-conversation"] = sid;
Expand Down
148 changes: 147 additions & 1 deletion tests/plugin-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ async function flush(): Promise<void> {
await new Promise((r) => setTimeout(r, 20));
}

const sleep = (ms: number): Promise<void> => new Promise((r) => setTimeout(r, ms));

// Windows CI runners can take seconds on a cold loopback connect, so a fixed
// 20ms flush races the manifest fetch. Poll for registration instead.
async function waitForTools(pi: FakePi, count: number, timeoutMs = 15000): Promise<void> {
Expand Down Expand Up @@ -338,7 +340,8 @@ test("pi extension registers manifest tools and stamps headers when proxied", as
biliPlugin(pi as never);
// The header is only stamped once tools are registered, so register
// first (a real session does this via session_start before the first
// provider request; a one-shot -p run races it and rides wire mode).
// provider request; launcher-mode -p wins that race via the load-time
// manifest prime — #1217, covered by the dedicated test below).
await pi.events.get("session_start")!({}, fakeCtx(proxy));
await waitForTools(pi, 2);
const headers: Record<string, string> = {};
Expand Down Expand Up @@ -401,6 +404,149 @@ test("before_provider_headers stamps after a session_start prewarm too", async (
}
});

test("#1217: launcher-mode manifest fetch is primed at extension load time", async () => {
// Correctness of the -p first-request stamp under ANY latency is #1228's
// awaited ownership claim; this prime is the latency half: the manifest
// fetch must be IN FLIGHT before any event fires (load time, not
// session_start), so that awaited claim resolves without waiting on the
// network RTT. omp has no header hook at all — for it the prime is the
// only way round 1 can carry plugin headers. Assert on observed request
// arrival, not wall-clock margins.
const manifest = JSON.stringify({ tools: { anthropic: [
{ name: "compress", description: "c", input_schema: { type: "object", properties: {} } },
{ name: "acp_status", description: "s", input_schema: { type: "object", properties: {} } },
] } });
let manifestHits = 0;
const server = http.createServer((req, res) => {
if (req.url === "/__bili/plugin/manifest") {
manifestHits++;
res.writeHead(200, { "content-type": "application/json" });
res.end(manifest);
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 AddressInfo).port}`;
try {
const ctx = { sessionManager: { getSessionId: () => "sess-42" }, model: { contextWindow: 1000000, baseUrl: "https://api.example.com/v1" } };
await withEnv({ BILLION_CONTEXT_PROXY: origin }, async () => {
const pi = makeFakePi();
createBiliPlugin("pi")(pi as never);
const deadline = Date.now() + 5000;
while (manifestHits === 0 && Date.now() < deadline) await sleep(5);
assert.ok(manifestHits >= 1, "manifest fetch must already be in flight before any event (load-time prime)");
await pi.events.get("session_start")!({}, ctx);
await waitForTools(pi, 2);
const headers: Record<string, string> = {};
await pi.events.get("before_provider_headers")!({ headers }, ctx);
assert.equal(headers["x-bili-plugin"], "pi");
assert.equal(headers["x-bili-plugin-conversation"], "sess-42");
assert.equal(pi.tools.length, 2);
});
} finally {
server.close();
}
});

test("#1217: a failed manifest prime falls back to the event-time fetch and retry throttle", async () => {
// A failed prime must degrade to the EXISTING event-time path: one
// fallback fetch, then retry-throttled. With #1228's awaited claim, round
// 1 rides wire mode while throttled, and the next awaited event performs
// the recovery fetch and stamps in the same call. The first two requests
// always fail and later ones succeed, so settlement is detected by
// observed request arrival — never by wall-clock margins.
let manifestRequests = 0;
const server = http.createServer((req, res) => {
if (req.url === "/__bili/plugin/manifest") {
manifestRequests++;
if (manifestRequests > 2) {
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify({ tools: { anthropic: [
{ name: "compress", description: "c", input_schema: { type: "object", properties: {} } },
{ name: "acp_status", description: "s", input_schema: { type: "object", properties: {} } },
] } }));
return;
}
res.writeHead(404);
res.end("{}");
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 AddressInfo).port}`;
try {
const ctx = { sessionManager: { getSessionId: () => "sess-42" }, model: { contextWindow: 1000000, baseUrl: "https://api.example.com/v1" } };
await withEnv({ BILLION_CONTEXT_PROXY: origin }, async () => {
const pi = makeFakePi();
createBiliPlugin("pi", { retryIntervalMs: 500 })(pi as never);
// Request #1 = the failed load-time prime; request #2 = the
// event-time fallback fetch kicked off by session_start. Wait
// until BOTH are observed at the still-failing server, then drain.
await pi.events.get("session_start")!({}, ctx);
const settleDeadline = Date.now() + 5000;
while (manifestRequests < 2 && Date.now() < settleDeadline) await sleep(5);
assert.ok(manifestRequests >= 2, "failed prime must fall through to the event-time fetch");
await flush();
assert.equal(pi.tools.length, 0, "two failed fetches → no tools yet");
// Round 1 while throttled: the awaited claim finds retryAt armed →
// NO third request, NO stamp — graceful wire-mode degradation.
const round1: Record<string, string> = {};
await pi.events.get("before_provider_headers")!({ headers: round1 }, ctx);
assert.deepEqual(round1, {}, "throttled round 1 rides wire mode");
assert.equal(manifestRequests, 2, "throttle window makes no extra fetches");
// Recovery: once the throttle expires, the next awaited event does
// exactly one recovery fetch and stamps in the same call.
let stamped: Record<string, string> | undefined;
const recoverDeadline = Date.now() + 5000;
while (!stamped && Date.now() < recoverDeadline) {
const h: Record<string, string> = {};
await pi.events.get("before_provider_headers")!({ headers: h }, ctx);
if (h["x-bili-plugin"] === "pi") stamped = h;
else await sleep(10);
}
assert.ok(stamped !== undefined, "recovery fetch succeeds once the throttle expires");
assert.equal(stamped["x-bili-plugin-conversation"], "sess-42");
assert.equal(pi.tools.length, 2);
assert.equal(manifestRequests, 3, "recovery is a single fetch, not a storm");
});
} finally {
server.close();
}
});

test("#1217: kill switch suppresses the load-time manifest prime", async () => {
let manifestHits = 0;
const server = http.createServer((req, res) => {
if (req.url === "/__bili/plugin/manifest") {
manifestHits++;
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify({ tools: { anthropic: [] } }));
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 AddressInfo).port}`;
try {
await withEnv({ BILLION_CONTEXT_PROXY: origin, BILLION_CONTEXT_PLUGIN: "0" }, async () => {
const pi = makeFakePi();
createBiliPlugin("pi")(pi as never);
await sleep(50);
assert.equal(manifestHits, 0, "BILLION_CONTEXT_PLUGIN=0 must keep the plugin fully inert at load time");
});
} finally {
server.close();
}
});

test("before_provider_headers stays silent when the manifest fetch keeps failing", async () => {
// Graceful degradation: a dead manifest endpoint means the plugin never
// claims ownership, so the session rides the proxy's wire mode forever.
Expand Down
Loading