From 921649e5f92fc39e5fcf75df844af1073ff90111 Mon Sep 17 00:00:00 2001 From: mz <6292459+Maziar123@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:12:11 +0300 Subject: [PATCH] fix(omp): drop the stamped prompt_cache_key on a direct send While a proxy is claimed (BILLION_CONTEXT_PROXY), the omp plugin stamps the session id as a top-level prompt_cache_key. The proxy strips it before it forwards. But when one request falls back to a direct send (the proxy is not ready, or its respawn failed), the claim is still set, so the body was already stamped and nothing strips the field. A strict upstream then rejects the request: 400 prompt_cache_key: Extra inputs are not permitted Record the values the plugin stamps, and give the omp lane its own direct-send guard: it runs the shared incremental guard first, then removes a top-level prompt_cache_key only when its value is one this process stamped. A key the host set itself is left alone. --- src/agent/omp-native.ts | 10 ++-- src/agent/pi.ts | 57 +++++++++++++++++++- tests/omp-direct-send-stamped-pck.test.ts | 66 +++++++++++++++++++++++ 3 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 tests/omp-direct-send-stamped-pck.test.ts diff --git a/src/agent/omp-native.ts b/src/agent/omp-native.ts index 66022f7288..eba718e16b 100644 --- a/src/agent/omp-native.ts +++ b/src/agent/omp-native.ts @@ -22,11 +22,11 @@ // (BILLION_CONTEXT_PLUGIN=0 / BILI_NATIVE_OMP=0). import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST } from "../launcher.js"; -import { createBiliPlugin } from "./pi.js"; +import { createBiliPlugin, createOmpDirectSendGuard } from "./pi.js"; import { applyOmpFirstEventTimeout, applyOmpMessageThreadsOff, markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, setNativeOriginWaiter, singleFlight } from "./native-bootstrap.js"; import { installNativeFetchIntercept, noteRoutedOrigin, observeRoutedOrigin, readyOrigin, setDeclaredModelEndpoints, type NativeInterceptState } from "./native-intercept.js"; import { loadDeclaredModelEndpoints } from "../model-endpoints.js"; -import { clearIncrementalProbe, createPrepareDirectSend, hostMsgIdHintFor, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalAck, noteIncrementalRejection, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js"; +import { clearIncrementalProbe, hostMsgIdHintFor, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalAck, noteIncrementalRejection, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js"; import * as pfChannel from "./pf-channel.js"; import { fetchProxyVersion, waitForProxyVersion } from "./shared.js"; import { logHostEvent } from "./host-log.js"; @@ -214,8 +214,10 @@ export async function ompIncrementalBeforeSend(url: string, init: RequestInit | state.beforeSend = ompIncrementalBeforeSend; // #3304: the omp swap stamps the turn id atomically with the body, so the -// strict shared guard applies — mutated + unrestorable ⇒ refuse locally. -state.prepareDirectSend = createPrepareDirectSend(); +// strict shared guard applies — mutated + unrestorable ⇒ refuse locally. A +// direct send also drops the prompt_cache_key the plugin stamped (no proxy +// strips it on that path). +state.prepareDirectSend = createOmpDirectSendGuard(); export function ompIncrementalOnResponse(_url: string, response: Response, headers?: Record): void { const turnId = headers?.[INCREMENTAL_TURN_ID_HEADER]; diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 17b1c295c4..29cda5c2c7 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -17,7 +17,7 @@ import { disposeSubagentSelfReg, selfRegisterForSession, type SubagentSelfRegSta import { isModelApiUrl, nativeInterceptInstalled } from "./native-intercept.js"; import { wirePiSubagents, piCommandsEnabled } from "./pi-subagents.js"; import { wireToolGuardrails } from "./tool-guardrails.js"; -import { clearIncrementalProbe, noteHostMsgIdHint, INCREMENTAL_TURN_MARKER_HEADER, incrementalEnvelopeConversationId, incrementalWireHash, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, pullIncrementalReceipt, sweepStale, wireMessagesLookAnthropic, type HostMsgIdHintEntry, type IncrementalWireMessage } from "./incremental-client.js"; +import { clearIncrementalProbe, createPrepareDirectSend, noteHostMsgIdHint, INCREMENTAL_TURN_MARKER_HEADER, incrementalEnvelopeConversationId, incrementalWireHash, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, pullIncrementalReceipt, sweepStale, wireMessagesLookAnthropic, type HostMsgIdHintEntry, type IncrementalWireMessage } from "./incremental-client.js"; import { laneMsgIdsForOpenAiWire, laneMsgIdsForWire, lanePinnableContent } from "./pi-msgids.js"; import * as pfChannel from "./pf-channel.js"; import { validateMsgIds } from "../msgid-pin.js"; @@ -551,9 +551,41 @@ export function stampPromptCacheKey(event: unknown, ctx: Ctx, agent: string): Re if (typeof p.prompt_cache_key === "string" && p.prompt_cache_key.trim().length > 0) return undefined; const sid = sessionIdOf(ctx); if (sid === undefined || sid.length === 0) return undefined; + rememberStampedPromptCacheKey(sid); return { ...p, prompt_cache_key: sid }; } +// Values this process stamped as prompt_cache_key. The proxy strips the field +// before it forwards; a request that degrades to a direct send never reaches +// the proxy, so the omp direct-send guard drops the stamp itself — only a +// value recorded here, never a key the host set. Bounded, oldest first out. +const STAMPED_PROMPT_CACHE_KEYS_MAX = 1024; +const stampedPromptCacheKeys = new Set(); + +function rememberStampedPromptCacheKey(sid: string): void { + stampedPromptCacheKeys.delete(sid); + stampedPromptCacheKeys.add(sid); + if (stampedPromptCacheKeys.size > STAMPED_PROMPT_CACHE_KEYS_MAX) { + const oldest = stampedPromptCacheKeys.values().next().value; + if (oldest !== undefined) stampedPromptCacheKeys.delete(oldest); + } +} + +/** The JSON body without the prompt_cache_key this process stamped, or + * undefined when the body carries no such stamp (unchanged, send as-is). */ +function withoutStampedPromptCacheKey(text: string): string | undefined { + let parsed: unknown; + try { + parsed = JSON.parse(text); + } catch { + return undefined; + } + if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return undefined; + const { prompt_cache_key: key, ...rest } = parsed as Record; + if (typeof key !== "string" || !stampedPromptCacheKeys.has(key)) return undefined; + return JSON.stringify(rest); +} + function fmtTok(n: number): string { if (n < 1000) return String(n); if (n < 1_000_000) return `${(n / 1000).toFixed(1)}K`; @@ -942,6 +974,29 @@ export function nudgeNotifyText(pending: Pick Promise { + const shared = createPrepareDirectSend(); + return async (url, input, init) => { + const prep = await shared(url, input, init); + if (prep.outcome === "unrestorable") return prep; + let text: string | undefined = prep.outcome === "restored" ? prep.body : undefined; + if (text === undefined) { + try { + if (typeof init?.body === "string") text = init.body; + else if (input instanceof Request) text = await input.clone().text(); + } catch { + text = undefined; // unreadable body — send as the shared guard decided + } + } + const cleaned = text === undefined ? undefined : withoutStampedPromptCacheKey(text); + return cleaned === undefined ? prep : { outcome: "restored", body: cleaned }; + }; +} + export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalMs?: number }): (pi: ExtensionAPI) => void { return function biliPlugin(pi: ExtensionAPI): void { const agent = agentName(agentOverride); diff --git a/tests/omp-direct-send-stamped-pck.test.ts b/tests/omp-direct-send-stamped-pck.test.ts new file mode 100644 index 0000000000..95880200a5 --- /dev/null +++ b/tests/omp-direct-send-stamped-pck.test.ts @@ -0,0 +1,66 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { createOmpDirectSendGuard, stampPromptCacheKey } from "../src/agent/pi.ts"; + +// The omp plugin stamps the session id as a top-level prompt_cache_key while a +// proxy is claimed (BILLION_CONTEXT_PROXY). The proxy strips it before it +// forwards. When one request then degrades to a direct send (proxy not ready, +// or its respawn failed) the claim is still set, so the body was stamped, but +// no proxy strips it: a strict upstream (Anthropic Messages) answers 400 +// "prompt_cache_key: Extra inputs are not permitted". The omp direct-send +// guard must drop the stamp it added — and only that. + +const INTERCEPT_FLAG = Symbol.for("billion-context.native-fetch-intercept"); +const PROXY_ORIGIN = "http://127.0.0.1:36011"; +const URL_MESSAGES = "http://127.0.0.1:8199/v1/messages"; + +function withNativeClaim(fn: () => T): T { + const g = globalThis as Record; + const prevFlag = g[INTERCEPT_FLAG]; + const prevProxy = process.env.BILLION_CONTEXT_PROXY; + g[INTERCEPT_FLAG] = true; + process.env.BILLION_CONTEXT_PROXY = PROXY_ORIGIN; + try { + return fn(); + } finally { + if (prevFlag === undefined) delete g[INTERCEPT_FLAG]; + else g[INTERCEPT_FLAG] = prevFlag; + if (prevProxy === undefined) delete process.env.BILLION_CONTEXT_PROXY; + else process.env.BILLION_CONTEXT_PROXY = prevProxy; + } +} + +function stampedBody(sessionId: string): Record { + const ctx = { model: { baseUrl: "http://127.0.0.1:8199/v1" }, sessionManager: { getSessionId: () => sessionId } }; + const event = { type: "before_provider_request", payload: { model: "m", max_tokens: 16, messages: [{ role: "user", content: "hi" }] } }; + const out = withNativeClaim(() => stampPromptCacheKey(event, ctx, "omp")); + assert.equal(out?.prompt_cache_key, sessionId, "fixture: the plugin stamps the session id"); + return out!; +} + +test("direct send drops the prompt_cache_key the plugin stamped", async () => { + const body = stampedBody("sess-direct-1"); + const prep = await createOmpDirectSendGuard()(URL_MESSAGES, URL_MESSAGES, { method: "POST", body: JSON.stringify(body) }); + assert.equal(prep.outcome, "restored"); + const sent = JSON.parse((prep as { body: string }).body) as Record; + assert.equal("prompt_cache_key" in sent, false); + assert.deepEqual(sent, { model: "m", max_tokens: 16, messages: [{ role: "user", content: "hi" }] }); +}); + +test("direct send keeps a prompt_cache_key the plugin did not stamp", async () => { + stampedBody("sess-direct-2"); + const own = { model: "m", messages: [{ role: "user", content: "hi" }], prompt_cache_key: "host-own-key" }; + const prep = await createOmpDirectSendGuard()(URL_MESSAGES, URL_MESSAGES, { method: "POST", body: JSON.stringify(own) }); + assert.equal(prep.outcome, "as-is"); +}); + +test("direct send leaves a body without prompt_cache_key untouched", async () => { + const plain = JSON.stringify({ model: "m", messages: [{ role: "user", content: "hi" }] }); + const prep = await createOmpDirectSendGuard()(URL_MESSAGES, URL_MESSAGES, { method: "POST", body: plain }); + assert.equal(prep.outcome, "as-is"); +}); + +test("a non-JSON body stays as-is", async () => { + const prep = await createOmpDirectSendGuard()(URL_MESSAGES, URL_MESSAGES, { method: "POST", body: "not json" }); + assert.equal(prep.outcome, "as-is"); +});