diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 17b1c295c4..cc05c7874a 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -1028,9 +1028,16 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM // never runs once an earlier handler returned cancel. Accepted // evidence, in order: (1) local — we stamped // x-bili-plugin-conversation for this session id (tools registered - // AND a request routed through the proxy), or omp's identity register - // succeeded; (2) remote — the proxy confirms it carries the - // conversation id (/__bili/plugin/status ok). A non-http(s) baseUrl + // AND a request routed through the proxy) [pi only]; (2) remote — the + // proxy confirms it carries the conversation id (/__bili/plugin/status + // reports a LIVE session, sessionId non-null) [pi and omp]. omp's + // identity register is NOT carriage evidence (#7060: registration + // populates registeredIds without creating a tracked session, so a + // stamp-only claim cancels native compaction for a conversation the + // proxy never sees → overflow). A 200 WITHOUT a session (pre-first- + // request / chain-advisory, sessionId: null) is likewise NOT carriage: + // the proxy knows the id from registration/runtime-info but has never + // folded it. A non-http(s) baseUrl // vetoes by default (opt-in via BILI_NON_HTTP_PROVIDERS below only // widens the candidate set; carriage evidence still decides). Hosts // exposing no stable session id keep the @@ -1056,9 +1063,18 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM } const sid = ctx === undefined ? undefined : sessionIdOf(ctx); if (sid === undefined || sid.length === 0) return true; - if (agent === "pi" ? state.carriedSids?.has(sid) === true : state.identityAt === sid) return true; + if (agent === "pi" && state.carriedSids?.has(sid) === true) return true; try { - return (await fetchStatus(proxyBase, sid)) !== undefined; + const status = await fetchStatus(proxyBase, sid); + // 200 is NOT carriage evidence by itself: the status endpoint + // answers 200 for conversations it has NOT tracked — + // pre-first-request (runtime-info registered, no session yet) + // and chain-advisory (content verdict only) both carry + // sessionId: null. Carriage is a LIVE tracked session, i.e. a + // non-null session id. Claiming on the no-session 200s re-opens + // the #7060 bypass (cancel native compaction for a + // conversation the proxy never folds → overflow). + return status !== undefined && typeof status.sessionId === "string" && status.sessionId.length > 0; } catch (err) { logHostEvent("error", "[native-client]", `bili-plugin(${agent}): compaction ownership probe failed (${err instanceof Error ? err.message : String(err)}) — leaving native compaction enabled`); return false; diff --git a/tests/plugin-agent.test.ts b/tests/plugin-agent.test.ts index a9d757cde7..797bd8d7ac 100644 --- a/tests/plugin-agent.test.ts +++ b/tests/plugin-agent.test.ts @@ -90,7 +90,7 @@ type FakeProxy = { close(): Promise; }; -async function startFakeProxy(opts: { failRegister?: number; statusOk?: boolean; decompressFailure?: boolean } = {}): Promise { +async function startFakeProxy(opts: { failRegister?: number; statusOk?: boolean; decompressFailure?: boolean; statusPhase?: "pre-first-request" } = {}): Promise { const toolCalls: FakeProxy["toolCalls"] = []; const registers: FakeProxy["registers"] = []; const runtimeInfos: FakeProxy["runtimeInfos"] = []; @@ -153,9 +153,17 @@ async function startFakeProxy(opts: { failRegister?: number; statusOk?: boolean; if (opts.statusOk === false) { res.writeHead(404, { "content-type": "application/json" }); res.end(JSON.stringify({ ok: false, error: "unknown plugin conversation" })); + } else if (opts.statusPhase === "pre-first-request") { + // Realistic no-session 200: the proxy knows the id from + // runtime-info but has never tracked a session for it (the + // #7060 bypass shape). sessionId: null is the carriage + // discriminator — a 200 alone is NOT carriage evidence. + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true, phase: "pre-first-request", sessionId: null, contextTokens: 0 })); } else { + // Realistic full-session 200: a live tracked session (sessionId set). res.writeHead(200, { "content-type": "application/json" }); - res.end(JSON.stringify({ ok: true, contextTokens: 1234 })); + res.end(JSON.stringify({ ok: true, sessionId: "sess-carried", contextTokens: 1234 })); } return; } @@ -538,20 +546,29 @@ test("#1382: compaction cancel requires evidence the proxy carries this conversa await runOmpCase(true, true, "omp: proxy confirms carriage → announced auto compaction cancelled"); await runOmpCase(false, false, "omp: proxy never saw the conversation → announced auto compaction proceeds"); - // (H) omp identity fast path: a successful identity register is local - // evidence — the cancel holds even though the proxy reports unknown. - const identityProxy = await startFakeProxy({ statusOk: false }); + // (H) omp identity register is NOT carriage evidence (#7060): the proxy + // is UP and knows the id (identity registered + runtime-info reported) + // but has NEVER tracked a session for it — the status endpoint answers + // 200 pre-first-request with sessionId: null. That 200 is NOT carriage + // evidence, so the plugin must NOT claim ownership: native compaction + // proceeds. (The 404 "proxy never saw the conversation" shape is covered + // by runOmpCase(false, false) above.) + const identityProxy = await startFakeProxy({ statusPhase: "pre-first-request" }); try { await withEnv({ BILLION_CONTEXT_PROXY: identityProxy.origin }, async () => { const omp = makeFakePi(); createBiliPlugin("omp")(omp as never); // #1403: pck stamping (the identity signal) only fires for // destinations bili actually processes — route through /bili/. - const ctx = { ...httpCtx("sess-omp-id"), model: { contextWindow: 1000000, baseUrl: `${identityProxy.origin}/bili/https://api.example.com/v1` } }; + // model.id present so reportOmpRuntimeInfo fires (it is gated on a + // non-empty model id) — that is the precondition for the proxy to + // answer 200 pre-first-request, the real #7060 bypass shape. + const ctx = { ...httpCtx("sess-omp-id"), model: { id: "test-model", contextWindow: 1000000, baseUrl: `${identityProxy.origin}/bili/https://api.example.com/v1` } }; const payload = await omp.events.get("before_provider_request")!({ payload: { messages: [{ role: "user", content: "hi" }] } }, ctx); assert.equal((payload as { prompt_cache_key?: string })?.prompt_cache_key, "sess-omp-id", "identity registration completed with the request"); + assert.ok(identityProxy.runtimeInfos.some((r) => r.conversationId === "sess-omp-id"), "runtime-info was reported under the session id"); omp.events.get("auto_compaction_start")!({}, undefined); - assert.deepEqual(await omp.events.get("session_before_compact")!({}, ctx), { cancel: true }, "identity-registered session is carried by construction"); + assert.deepEqual(await omp.events.get("session_before_compact")!({}, ctx), undefined, "identity-registered + runtime-info session is NOT carried — 200 pre-first-request (sessionId null) → native compaction proceeds"); }); } finally { await identityProxy.close();