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
19 changes: 17 additions & 2 deletions src/agent/pi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,11 +523,26 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM
const maxOut = (ctx.model as { maxTokens?: unknown } | undefined)?.maxTokens;
reportRuntimeInfoOnChange(proxyBaseForCtx(ctx), { agent, model: modelId, contextWindow: typeof window === "number" && window > 0 ? Math.floor(window) : undefined, maxOutput: typeof maxOut === "number" && maxOut > 0 ? Math.floor(maxOut) : undefined, baseURL: ctx.model?.baseUrl, source: "client-config" });
}
pi.on("before_provider_request", (event, ctx) => {
pi.on("before_provider_request", async (event, ctx) => {
// omp emits this per model request (but never before_provider_headers);
// it doubles as the retry driver when the session_start manifest
// fetch raced the proxy startup. Cached by sid, throttled by retryAt.
void registerTools(pi, ctx, state, agent).catch((err: unknown) => console.error(`bili-plugin(${agent}): ${err instanceof Error ? err.message : String(err)}`));
// #1230: omp's runner also AWAITS async handlers (emitBeforeProviderRequest)
// and uses the resolved value as the outgoing payload — so the plugin-mode
// claim serializes behind tool registration instead of racing it. For omp
// the claim is the identity registration that runs INSIDE registerTools
// (after the manifest fetch): a one-shot (`omp -p`) dispatches exactly ONE
// request, and with fire-and-forget registration it left before the proxy
// saw the conversation id — bound as an anonymous pfa session, rode proxy
// mode forever, no round 2 to self-heal. registerTools is idempotent
// (sid-cached, pending-deduped, retryAt-throttled), so the await is bounded
// (worst case = manifest fetch + identity POST timeouts); a permanently
// failing manifest fetch still degrades to wire mode.
try {
await registerTools(pi, ctx, state, agent);
} catch (err) {
console.error(`bili-plugin(${agent}): tool registration failed (${err instanceof Error ? err.message : String(err)}) — riding wire mode for this request`);
}
if (agent === "omp") reportOmpRuntimeInfo(ctx);
return stampPromptCacheKey(event, ctx, agent);
});
Expand Down
61 changes: 42 additions & 19 deletions tests/plugin-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,29 @@ test("omp plugin identity-registers the conversation once tools are ready", asyn
}
});

test("#1230: before_provider_request awaits identity registration — omp one-shot (-p) first dispatch claims plugin mode", async () => {
const proxy = await startFakeProxy();
try {
const pi = makeFakePi();
ompPlugin(pi as never);
// The -p shape: NO session_start prewarm — the one and only request
// dispatches at boot. omp's runner awaits async handlers and sends the
// resolved payload, so by the time the handler RESOLVES the identity
// register must already have landed on the wire — the proxy binds the
// dispatching session into plugin mode instead of an anonymous pfa.
const out = await pi.events.get("before_provider_request")!({ type: "before_provider_request", payload: { model: "m", messages: [{ role: "user", content: "hi" }] } }, fakeCtx(proxy, "omp-sess-oneshot"));
assert.equal(pi.tools.length, 2, "tools registered before the request could leave");
assert.deepEqual(proxy.registers, [{ conversationId: "omp-sess-oneshot", agent: "omp", identity: true }], "identity register landed BEFORE the handler resolved");
assert.equal((out as Record<string, unknown>).prompt_cache_key, "omp-sess-oneshot", "payload still stamped with the omp session id");
// Subsequent fires stay cached (no re-register, no re-fetch).
const again = await pi.events.get("before_provider_request")!({ type: "before_provider_request", payload: { model: "m", messages: [{ role: "user", content: "hi" }] } }, fakeCtx(proxy, "omp-sess-oneshot"));
assert.deepEqual(proxy.registers, [{ conversationId: "omp-sess-oneshot", agent: "omp", identity: true }], "no duplicate register on the second fire");
assert.equal((again as Record<string, unknown>).prompt_cache_key, "omp-sess-oneshot");
} finally {
await proxy.close();
}
});

test("pi plugin does not identity-register (it stamps headers instead)", async () => {
const proxy = await startFakeProxy();
try {
Expand Down Expand Up @@ -1515,14 +1538,16 @@ test("omp before_provider_request stamps prompt_cache_key only for chat-completi
createBiliPlugin("omp")(pi as never);
return pi;
};
const handler = (pi: FakePi, payload: unknown) =>
// #1230: the handler is async (it awaits tool registration); fakeCtx
// (undefined) keeps registerTools a no-op, so awaiting stays pure.
const handler = async (pi: FakePi, payload: unknown) =>
pi.events.get("before_provider_request")!({ type: "before_provider_request", payload }, fakeCtx(undefined, sid));

// chat payload (messages, no input, no max_tokens, no native pck) → stamped
{
const pi = mk();
const payload = { model: "glm-4", messages: [{ role: "user", content: "hi" }] };
const out = handler(pi, payload) as Record<string, unknown>;
const out = await handler(pi, payload) as Record<string, unknown>;
assert.equal(out.prompt_cache_key, sid, "chat payload stamped with the omp session id");
assert.deepEqual(out.messages, payload.messages, "rest of the payload preserved");
assert.equal(payload.prompt_cache_key, undefined, "original payload not mutated");
Expand All @@ -1531,20 +1556,20 @@ test("omp before_provider_request stamps prompt_cache_key only for chat-completi
// providers use maxTokensField:"max_tokens") → stamped (#268)
{
const pi = mk();
const out = handler(pi, { model: "glm-4", messages: [{ role: "user", content: "hi" }], max_tokens: 4096, temperature: 0.7 }) as Record<string, unknown>;
const out = await handler(pi, { model: "glm-4", messages: [{ role: "user", content: "hi" }], max_tokens: 4096, temperature: 0.7 }) as Record<string, unknown>;
assert.equal(out.prompt_cache_key, sid, "chat-completions payload WITH max_tokens stamped");
assert.equal(out.max_tokens, 4096, "max_tokens preserved");
}
// native prompt_cache_key already present → not overridden
{
const pi = mk();
const out = handler(pi, { messages: [{ role: "user", content: "hi" }], prompt_cache_key: "native-pck" });
const out = await handler(pi, { messages: [{ role: "user", content: "hi" }], prompt_cache_key: "native-pck" });
assert.equal(out, undefined, "native pck is not overridden");
}
// responses payload (input array) → untouched
{
const pi = mk();
const out = handler(pi, { input: [{ role: "user", content: "hi" }] });
const out = await handler(pi, { input: [{ role: "user", content: "hi" }] });
assert.equal(out, undefined, "responses payload (input) untouched");
}
// anthropic wire shape (messages + max_tokens) → stamped too: the plugin
Expand All @@ -1553,34 +1578,34 @@ test("omp before_provider_request stamps prompt_cache_key only for chat-completi
// forwarding to the real Anthropic (#268)
{
const pi = mk();
const out = handler(pi, { messages: [{ role: "user", content: "hi" }], max_tokens: 1024, system: "s" }) as Record<string, unknown>;
const out = await handler(pi, { messages: [{ role: "user", content: "hi" }], max_tokens: 1024, system: "s" }) as Record<string, unknown>;
assert.equal(out.prompt_cache_key, sid, "anthropic wire shape stamped (proxy strips before forward)");
}
// no session id → untouched
{
const pi = mk();
const out = pi.events.get("before_provider_request")!({ type: "before_provider_request", payload: { messages: [{ role: "user", content: "hi" }] } }, fakeCtx(undefined, ""));
const out = await pi.events.get("before_provider_request")!({ type: "before_provider_request", payload: { messages: [{ role: "user", content: "hi" }] } }, fakeCtx(undefined, ""));
assert.equal(out, undefined, "no session id → untouched");
}
// non-object payload → untouched
{
const pi = mk();
assert.equal(handler(pi, "not an object"), undefined, "non-object payload untouched");
assert.equal(handler(pi, null), undefined, "null payload untouched");
assert.equal(handler(pi, [1, 2, 3]), undefined, "array payload untouched");
assert.equal(await handler(pi, "not an object"), undefined, "non-object payload untouched");
assert.equal(await handler(pi, null), undefined, "null payload untouched");
assert.equal(await handler(pi, [1, 2, 3]), undefined, "array payload untouched");
}
// no messages array → untouched
{
const pi = mk();
assert.equal(handler(pi, { model: "x" }), undefined, "no messages → untouched");
assert.equal(handler(pi, { messages: "nope" }), undefined, "non-array messages → untouched");
assert.equal(await handler(pi, { model: "x" }), undefined, "no messages → untouched");
assert.equal(await handler(pi, { messages: "nope" }), undefined, "non-array messages → untouched");
}
});

test("pi agent never stamps prompt_cache_key (it stamps headers instead)", async () => {
const pi = makeFakePi();
createBiliPlugin("pi")(pi as never);
const out = pi.events.get("before_provider_request")!({ type: "before_provider_request", payload: { messages: [{ role: "user", content: "hi" }] } }, fakeCtx(undefined, "pi-uuid"));
const out = await pi.events.get("before_provider_request")!({ type: "before_provider_request", payload: { messages: [{ role: "user", content: "hi" }] } }, fakeCtx(undefined, "pi-uuid"));
assert.equal(out, undefined, "pi agent never stamps the body");
});

Expand All @@ -1601,12 +1626,10 @@ test("#957: omp reports runtime-info via before_provider_request (omp has no hea
const ctxA = { ...fakeCtx(proxy, "omp-rt-1"), model: { id: "omp-rt-model-a", contextWindow: 200000, maxTokens: 32768, ...baseModel } };
const ctxB = { ...fakeCtx(proxy, "omp-rt-1"), model: { id: "omp-rt-model-b", contextWindow: 128000, maxTokens: 16384, ...baseModel } };
await pi.events.get("session_start")!({}, ctxA);
// tools not ready yet → round 1 rides wire mode, no report. The
// manifest fetch cannot complete inside this synchronous block, so
// toolsReady is provably false here.
await pi.events.get("before_provider_request")!({}, ctxA);
assert.equal(proxy.runtimeInfos.length, 0, "no report before toolsReady");
await waitForTools(pi, 2);
// #1230: the request handler awaits tool registration, so by the time
// round 1's handler resolves the tools are ready and the report rides
// the FIRST request (previously round 1 left before toolsReady flipped
// and reported nothing — a race artifact, not a policy).
await pi.events.get("before_provider_request")!({}, ctxA);
await waitForRuntimeInfoCount(proxy, 1);
assert.deepEqual(proxy.runtimeInfos[0], {
Expand Down
Loading