From 2a429212e908ff35cc3479ff0671b7d008501ba1 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 24 Sep 2026 09:12:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20omp=20one-shot=20(-p)=20first=20request?= =?UTF-8?q?=20claims=20plugin=20mode=20=E2=80=94=20await=20identity=20regi?= =?UTF-8?q?stration=20before=20dispatch=20(#1230)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mirror #1228 on the before_provider_request path: make the handler async and await registerTools before stamping/reporting. omp's runner awaits async handlers and sends the resolved payload (verified in the omp 18.0.3 dist), so the plugin-mode claim (the identity:true register that runs INSIDE registerTools, after the manifest fetch) now serializes ahead of the single -p dispatch instead of racing it. A losing race bound the session as an anonymous pfa-*/proxy-mode session with no round 2 to self-heal. --- src/agent/pi.ts | 19 ++++++++++-- tests/plugin-agent.test.ts | 61 ++++++++++++++++++++++++++------------ 2 files changed, 59 insertions(+), 21 deletions(-) diff --git a/src/agent/pi.ts b/src/agent/pi.ts index dd7e635587..5bb4c6946c 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -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); }); diff --git a/tests/plugin-agent.test.ts b/tests/plugin-agent.test.ts index 97e7b3f0b8..09a3538a6f 100644 --- a/tests/plugin-agent.test.ts +++ b/tests/plugin-agent.test.ts @@ -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).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).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 { @@ -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; + const out = await handler(pi, payload) as Record; 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"); @@ -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; + const out = await handler(pi, { model: "glm-4", messages: [{ role: "user", content: "hi" }], max_tokens: 4096, temperature: 0.7 }) as Record; 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 @@ -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; + const out = await handler(pi, { messages: [{ role: "user", content: "hi" }], max_tokens: 1024, system: "s" }) as Record; 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"); }); @@ -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], {