Skip to content
Closed
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
10 changes: 6 additions & 4 deletions src/agent/omp-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<string, string>): void {
const turnId = headers?.[INCREMENTAL_TURN_ID_HEADER];
Expand Down
57 changes: 56 additions & 1 deletion src/agent/pi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<string>();

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<string, unknown>;
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`;
Expand Down Expand Up @@ -942,6 +974,29 @@ export function nudgeNotifyText(pending: Pick<NudgeDebugEvent, "emergency" | "te
return `[ACP nudge → context]${pending.emergency ? " [EMERGENCY]" : ""}\n${pending.text}`;
}

/** #3304 prepareDirectSend for the omp lane: the shared incremental guard
* (restore a swapped body or refuse), then drop the prompt_cache_key the
* plugin stamped — on a direct send no proxy strips it, and strict upstreams
* reject the unknown field with a 400. */
export function createOmpDirectSendGuard(): (url: string, input: string | URL | Request, init: RequestInit | undefined) => Promise<DirectSendPrep> {
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);
Expand Down
66 changes: 66 additions & 0 deletions tests/omp-direct-send-stamped-pck.test.ts
Original file line number Diff line number Diff line change
@@ -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<T>(fn: () => T): T {
const g = globalThis as Record<PropertyKey, unknown>;
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<string, unknown> {
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<string, unknown>;
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");
});
Loading