From 7e40273e3ac76781a1dcd1be8f031d03f10d5dfb Mon Sep 17 00:00:00 2001 From: ework-agent Date: Fri, 28 Aug 2026 22:32:29 +0800 Subject: [PATCH 01/18] feat: partition codex subagent threads by turn-metadata (PR-A of #316) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Codex (>=0.147) reuses the root session-id header on subagent threads, so the legacy identity chain mapped every thread of one task onto a single session — subagents inherited the root's compression state, breaking #150's isolation (a guardian subagent must read the user's original authorization verbatim, never a compressed summary). Add codexTurnIdentity(): when x-codex-turn-metadata is present and cross-checked (metadata.thread_id === thread-id header), partition by thread_source — "user" keeps the session-id header (current root semantics, stable across turns), "subagent" gets its own thread-id (fresh independent state per thread). Untrusted metadata (absent / unparseable / mismatched / unknown thread_source) falls through to the legacy chain unchanged, so no-metadata clients (omp pck, etc.) see zero regression. The trusted id enters the verbatim session chain directly (no hashing, no new dimensions) and bypasses subagentNamespace, leaving the kernel's empty-instructions non-anchoring path untouched for metadata-less clients. Tests: unit coverage for both thread_source values, cross-check mismatch, JSON parse failure, unknown thread_source, missing headers, omp pck chain unaffected; plus a server-level test proving subagent and root threads land in distinct isolated sessions. --- src/server.ts | 35 ++++++-- src/session-id.ts | 50 +++++++++++ tests/codex-turn-isolation.test.ts | 140 +++++++++++++++++++++++++++++ tests/proxy-session-id.test.ts | 83 ++++++++++++++++- 4 files changed, 298 insertions(+), 10 deletions(-) create mode 100644 tests/codex-turn-isolation.test.ts diff --git a/src/server.ts b/src/server.ts index 60d2eac1a2..3bf4c081f1 100644 --- a/src/server.ts +++ b/src/server.ts @@ -56,7 +56,7 @@ import { sanitizeResponsesInputIds, dropWhitespaceResponsesMessages, normalizeRe import { rewriteOpenaiJsonResponse } from "./stream-openai.js"; import { rewriteResponsesJsonResponse } from "./stream-responses.js"; import { emitStreamError } from "./stream-error.js"; -import { affinityToken, clientConversationHeader, preferPromptCacheKeyIdentity, type ConversationIdentity } from "./session-id.js"; +import { affinityToken, clientConversationHeader, codexTurnIdentity, preferPromptCacheKeyIdentity, type ConversationIdentity } from "./session-id.js"; import { prefixAffinity, type AnonymousAffinity } from "./prefix-affinity.js"; import { consumePluginRegisterFor, flushConversations, handlePluginManifest, handlePluginRegister, handlePluginStatus, handlePluginTool, loadConversations, pipePluginJson, pipePluginResponsesWithStrip, pipeThroughWithUsage, pluginAgentHeader, pluginConversationHeader, pluginReportedContextWindow, recordPluginSession, rememberPluginMessages, takePendingPluginRegister } from "./plugin.js"; import { setupMitm, readMitmUpstream } from "./mitm.js"; @@ -834,11 +834,22 @@ async function handle( // shared 200-char prefix and leak compression state across sessions. const clientConv = clientConversationHeader(req.headers); const convHeader = clientConv ?? sessionHeader; + // Codex turn-metadata partitioning (#316 / PR-A): when the explicit + // Codex turn metadata is present and cross-checked against the + // thread-id header, partition compression state by thread_source. + // Root ("user") turns keep the session-id header (current semantics, + // stable across turns); subagents get their own thread-id (fresh + // independent state per thread, #150). Untrusted metadata (absent / + // unparseable / mismatched / unknown thread_source) → undefined, and + // the legacy chain below is unchanged. + const codexTurn = protocol === "responses" ? codexTurnIdentity(req.headers) : undefined; const responsesIdentity = protocol === "responses" - ? preferPromptCacheKeyIdentity( - conversationIdentityResponses(parsed as ResponsesRequestBody, convHeader), - parsed as ResponsesRequestBody, - ) + ? (codexTurn + ? { value: codexTurn.value, source: "header" as const, clientProvided: true } + : preferPromptCacheKeyIdentity( + conversationIdentityResponses(parsed as ResponsesRequestBody, convHeader), + parsed as ResponsesRequestBody, + )) : undefined; // OpenAI chat mirrors the responses pck promotion: clients that replay // full history statelessly (omp chat-completions via a relay) send NO @@ -863,10 +874,16 @@ async function handle( ? conversationSignalAnthropic(parsed as AnthropicRequestBody, convHeader) : protocol === "openai" ? openaiIdentity?.value ?? openaiSignal - : subagentNamespace( - responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader), - (parsed as ResponsesRequestBody).instructions, - ); + : codexTurn + // Trusted Codex turn id enters the verbatim session chain + // directly — do NOT route it through subagentNamespace (the + // kernel's empty-instructions non-anchoring path is left + // untouched for metadata-less clients). + ? codexTurn.value + : subagentNamespace( + responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader), + (parsed as ResponsesRequestBody).instructions, + ); // The session ID is the client-provided conversation value VERBATIM — // no hash, no protocol/credential/upstream dimensions (#286): those // are all mutable mid-conversation (bearer rotation, relay switching, diff --git a/src/session-id.ts b/src/session-id.ts index c9efad0aa1..5e016c76fa 100644 --- a/src/session-id.ts +++ b/src/session-id.ts @@ -89,3 +89,53 @@ export function preferPromptCacheKeyIdentity( if (pck.length === 0) return identity; return { ...identity, value: pck, source: "prompt-cache-key", clientProvided: true }; } + +/** + * Codex turn-metadata partitioning (#316 / PR-A). + * + * Codex (>=0.147) sends `x-codex-turn-metadata` (JSON) on every Responses + * request carrying `thread_source` ("user" for the root thread, "subagent" + * for spawned agent threads) and `thread_id`. Subagent threads REUSE the + * root's `session-id` header, so the legacy identity chain maps every thread + * of one task onto a single session — subagents inherit the root's + * compression state, which breaks #150's isolation (a guardian subagent must + * read the user's original authorization verbatim, never a compressed + * summary). + * + * When the metadata is present AND cross-checked (metadata.thread_id === + * `thread-id` header), partition by thread_source: + * - "user" → the `session-id` header (current root semantics, stable + * across turns) + * - "subagent" → the `thread-id` header (fresh independent state per + * thread; self-contained replay is lossless) + * Any other thread_source, unparseable JSON, missing/mismatched thread_id, or + * a missing `session-id` header on a "user" turn → undefined: the caller + * falls through to the legacy chain unchanged. + */ +export type CodexTurnIdentity = { + value: string; + threadSource: "user" | "subagent"; +}; + +export function codexTurnIdentity(headers: Record): CodexTurnIdentity | undefined { + const raw = headers["x-codex-turn-metadata"]; + if (typeof raw !== "string" || raw.trim().length === 0) return undefined; + let parsed: unknown; + try { + parsed = JSON.parse(raw); + } catch { + return undefined; + } + if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return undefined; + const record = parsed as Record; + const threadSource = record["thread_source"]; + if (threadSource !== "user" && threadSource !== "subagent") return undefined; + const metaThreadId = record["thread_id"]; + if (typeof metaThreadId !== "string" || metaThreadId.trim().length === 0) return undefined; + const threadHeader = headers["thread-id"]; + if (typeof threadHeader !== "string" || threadHeader.trim() !== metaThreadId.trim()) return undefined; + if (threadSource === "subagent") return { value: threadHeader.trim(), threadSource }; + const sessionHeader = headers["session-id"]; + if (typeof sessionHeader !== "string" || sessionHeader.trim().length === 0) return undefined; + return { value: sessionHeader.trim(), threadSource }; +} diff --git a/tests/codex-turn-isolation.test.ts b/tests/codex-turn-isolation.test.ts new file mode 100644 index 0000000000..de1f81fff1 --- /dev/null +++ b/tests/codex-turn-isolation.test.ts @@ -0,0 +1,140 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetSessionsForTest, peekSession } from "../src/session.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; + +const ROOT_SESSION = "01a048b8-c704-7c00-8000-000000000000"; +const SUB_THREAD = "01a048b8-c728-7c00-8000-000000000000"; + +function listen(server: http.Server): Promise { + if (server.listening) return Promise.resolve(); + return once(server, "listening").then(() => undefined); +} + +function close(server: http.Server): Promise { + return new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve()))); +} + +function sseBlock(type: string, data: Record): string { + return `event: ${type}\ndata: ${JSON.stringify({ type, ...data })}\n\n`; +} + +/** Minimal valid Responses-API SSE upstream: one assistant "ok" message. */ +function startMockResponsesUpstream(): http.Server { + const server = http.createServer((_req, res) => { + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + res.write(sseBlock("response.created", { response: { id: "resp_1", status: "in_progress" } })); + res.write( + sseBlock("response.output_item.added", { + output_index: 0, + item: { type: "message", id: "msg_1", role: "assistant", status: "in_progress", content: [] }, + }), + ); + res.write(sseBlock("response.output_text.delta", { item_id: "msg_1", output_index: 0, content_index: 0, delta: "ok" })); + res.write(sseBlock("response.output_text.done", { item_id: "msg_1", output_index: 0, text: "ok" })); + res.write( + sseBlock("response.output_item.done", { + output_index: 0, + item: { type: "message", id: "msg_1", role: "assistant", status: "completed", content: [{ type: "output_text", text: "ok" }] }, + }), + ); + res.write( + sseBlock("response.completed", { + response: { + id: "resp_1", + status: "completed", + output: [{ type: "message", id: "msg_1", role: "assistant", status: "completed", content: [{ type: "output_text", text: "ok" }] }], + usage: { input_tokens: 10, output_tokens: 2 }, + }, + }), + ); + res.end(); + }); + server.listen(0, "127.0.0.1"); + return server; +} + +const rootMeta = (over: Record = {}) => + JSON.stringify({ request_kind: "turn", thread_source: "user", thread_id: ROOT_SESSION, turn_id: "turn-1", window_id: "win-1", ...over }); +const subMeta = (over: Record = {}) => + JSON.stringify({ request_kind: "turn", thread_source: "subagent", thread_id: SUB_THREAD, turn_id: "turn-2", window_id: "win-1", ...over }); + +test("codex turn-metadata partition (#316 / PR-A): subagent and root threads get isolated sessions", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + _resetSessionsForTest(); + const upstream = startMockResponsesUpstream(); + await listen(upstream); + const upstreamPort = (upstream.address() as { port: number }).port; + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: 1_000_000 } } } } as ProxyOptions["routes"], + modelContextLimit: 1_000_000, + kernelConfig: defaultConfig(1_000_000), + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + await listen(proxy); + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/responses`; + const post = async (headers: Record, body: Record) => { + const res = await fetch(url, { + method: "POST", + headers: { "content-type": "application/json", ...headers }, + body: JSON.stringify(body), + duplex: "half", + } as RequestInit); + const text = await res.text(); + return { status: res.status, text }; + }; + try { + // 1. Root thread: session-id == thread-id == ROOT, thread_source user. + const rootRes = await post( + { "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta() }, + { model: "gpt-test", stream: true, instructions: "", input: [{ type: "message", role: "user", content: [{ type: "input_text", text: "root turn 1" }] }] }, + ); + assert.equal(rootRes.status, 200, `root request: ${rootRes.text}`); + assert.ok(peekSession(ROOT_SESSION), "root request must create the session keyed by the session-id header"); + + // 2. Subagent: REUSES the root session-id but carries its own thread-id. + // It must land in a SEPARATE session keyed by the thread-id, not the + // root session (this is the #150 isolation the bug broke). + const subRes = await post( + { "session-id": ROOT_SESSION, "thread-id": SUB_THREAD, "x-codex-turn-metadata": subMeta() }, + { model: "gpt-test", stream: true, instructions: "", input: [{ type: "message", role: "user", content: [{ type: "input_text", text: "subagent turn 1" }] }] }, + ); + assert.equal(subRes.status, 200, `subagent request: ${subRes.text}`); + const subSession = peekSession(SUB_THREAD); + assert.ok(subSession, "subagent request must create a session keyed by the thread-id header"); + assert.notEqual(subSession!.id, ROOT_SESSION, "subagent session must be distinct from the root session"); + assert.notEqual(subSession, peekSession(ROOT_SESSION), "subagent and root must be separate session objects (isolated compression state)"); + + // 3. Root identity is stable across turns: a later root turn (churned + // turn_id/window_id) must reuse the SAME root session. + const rootRes2 = await post( + { "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ turn_id: "turn-2", window_id: "win-9" }) }, + { model: "gpt-test", stream: true, instructions: "", input: [{ type: "message", role: "user", content: [{ type: "input_text", text: "root turn 2" }] }] }, + ); + assert.equal(rootRes2.status, 200, `root turn 2: ${rootRes2.text}`); + assert.equal(peekSession(ROOT_SESSION)!.stats.requests, 2, "two root turns must accumulate on the same root session"); + assert.equal(peekSession(SUB_THREAD)!.stats.requests, 1, "subagent session must not absorb root turns"); + } finally { + await close(proxy); + await close(upstream); + } +}); diff --git a/tests/proxy-session-id.test.ts b/tests/proxy-session-id.test.ts index a314121379..9e1cd651cf 100644 --- a/tests/proxy-session-id.test.ts +++ b/tests/proxy-session-id.test.ts @@ -1,6 +1,6 @@ import { test } from "node:test"; import assert from "node:assert/strict"; -import { affinityToken, clientConversationHeader, preferPromptCacheKeyIdentity } from "../src/session-id.ts"; +import { affinityToken, clientConversationHeader, codexTurnIdentity, preferPromptCacheKeyIdentity } from "../src/session-id.ts"; import { conversationIdentityResponses, conversationSignalResponses } from "acp-kernel/wire"; test("preferPromptCacheKeyIdentity: fingerprint + prompt_cache_key → stable client-provided identity (omp stateless replay)", () => { @@ -136,3 +136,84 @@ test("conversationIdentityResponses: identical anonymous openers share a content assert.equal(a, b); assert.match(a, /^[0-9a-f]{16}$/); }); + +// ---- codexTurnIdentity (#316 / PR-A): codex turn-metadata partitioning ---- + +const ROOT_SESSION = "01a048b8-c704-7c00-8000-000000000000"; +const SUB_THREAD = "01a048b8-c728-7c00-8000-000000000000"; +const rootMeta = (over: Record = {}) => + JSON.stringify({ request_kind: "turn", thread_source: "user", thread_id: ROOT_SESSION, turn_id: "turn-1", window_id: "win-1", ...over }); +const subMeta = (over: Record = {}) => + JSON.stringify({ request_kind: "turn", thread_source: "subagent", thread_id: SUB_THREAD, turn_id: "turn-2", window_id: "win-1", ...over }); + +test("codexTurnIdentity: thread_source user → session-id header (current root semantics)", () => { + const id = codexTurnIdentity({ + "session-id": ROOT_SESSION, + "thread-id": ROOT_SESSION, + "x-codex-turn-metadata": rootMeta(), + }); + assert.deepEqual(id, { value: ROOT_SESSION, threadSource: "user" }); +}); + +test("codexTurnIdentity: thread_source subagent → thread-id header (fresh per-thread state, #150)", () => { + // A subagent REUSES the root's session-id header but carries its own + // thread-id — it must resolve to the thread-id, NOT the root session. + const id = codexTurnIdentity({ + "session-id": ROOT_SESSION, + "thread-id": SUB_THREAD, + "x-codex-turn-metadata": subMeta(), + }); + assert.deepEqual(id, { value: SUB_THREAD, threadSource: "subagent" }); +}); + +test("codexTurnIdentity: root identity is stable across turns (turn_id/window_id churn is irrelevant)", () => { + const a = codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ turn_id: "turn-1", window_id: "w1" }) }); + const b = codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ turn_id: "turn-99", window_id: "w42" }) }); + assert.deepEqual(a, b); + assert.equal(a!.value, ROOT_SESSION); +}); + +test("codexTurnIdentity: metadata.thread_id must equal the thread-id header (cross-check, PR #249)", () => { + // metadata says one thread, header says another → do not trust. + const mismatch = codexTurnIdentity({ + "session-id": ROOT_SESSION, + "thread-id": SUB_THREAD, + "x-codex-turn-metadata": rootMeta({ thread_id: "01a048b8-ffff-7c00-8000-000000000000" }), + }); + assert.equal(mismatch, undefined); + // header missing entirely → do not trust. + const noHeader = codexTurnIdentity({ + "session-id": ROOT_SESSION, + "x-codex-turn-metadata": subMeta(), + }); + assert.equal(noHeader, undefined); +}); + +test("codexTurnIdentity: JSON parse failure / non-object → do not trust (legacy chain)", () => { + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": "{not json" }), undefined); + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": "42" }), undefined); + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": "[1,2,3]" }), undefined); + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": "null" }), undefined); + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": " " }), undefined); +}); + +test("codexTurnIdentity: unknown/missing thread_source → do not trust (legacy chain)", () => { + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ thread_source: "system" }) }), undefined); + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ thread_source: undefined }) }), undefined); + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ thread_id: 123 }) }), undefined); +}); + +test("codexTurnIdentity: user turn with no session-id header → do not trust (legacy chain)", () => { + assert.equal(codexTurnIdentity({ "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta() }), undefined); +}); + +test("codexTurnIdentity: no metadata header (omp / other Responses clients) → undefined, legacy chain untouched", () => { + // omp-style stateless replay: prompt_cache_key identity, no codex headers. + // codexTurnIdentity must return undefined so the pck promotion chain runs. + const id = codexTurnIdentity({}); + assert.equal(id, undefined); + const body = { input: [{ type: "message", role: "user", content: "hi" }], prompt_cache_key: "omp-pck-1" }; + const promoted = preferPromptCacheKeyIdentity(conversationIdentityResponses(body, undefined), body); + assert.equal(promoted!.source, "prompt-cache-key"); + assert.equal(promoted!.value, "omp-pck-1"); +}); From 8e9c99a5123e5ddb6f8629520286f431e98639d2 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Fri, 28 Aug 2026 22:47:40 +0800 Subject: [PATCH 02/18] feat: anonymous tail-window reattach + fork lineage for prefix affinity (#316 PR-B) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A client that drops its oldest messages (truncation) previously failed the full-depth prefix match and was orphaned into a fresh pfa- session, stranding the old session's compression blocks. resolve() now falls back to a tail-window comparison: the incoming's leading items are matched against each stored chain's trailing items (position-independent per-item hashes), so a truncated replay reattaches its original session and the kernel reconciles by message id (verified: blocks deactivate gracefully on truncation and revive on full replay, no throw). New pfa- sessions also record a best-effort lineage (discarded candidates + reason truncated/forked) in session metadata for UI/debug only — never used for matching. Live-edge overwrite semantics unchanged. - src/prefix-affinity.ts: TAIL_WINDOW/MIN_TAIL_MATCH/MAX_STORED_ITEMS/ MIN_FORK_PREFIX; perItemHashes + lcpLength; resolve() gains tail-window reattach + lineage; note() stores per-item hashes; AnonymousAffinity gains via + itemHashes + lineage. - src/server.ts: tail-window reattach log line; note() 4th arg; metadata records via + lineage. - tests/prefix-affinity.test.ts: note() call sites updated to 4 args. - tests/tail-window-reconcile.test.ts (new): tail-window reattach, prefix/new regressions, fork + truncated lineage, and a kernel reconcile anchor test. Independent of PR-A (codex turn-metadata, #317). Pre-flight: typecheck OK, 746/746 tests, build OK. --- src/prefix-affinity.ts | 135 +++++++++++++++++++++++++--- src/server.ts | 14 ++- tests/prefix-affinity.test.ts | 20 ++--- tests/tail-window-reconcile.test.ts | 134 +++++++++++++++++++++++++++ 4 files changed, 278 insertions(+), 25 deletions(-) create mode 100644 tests/tail-window-reconcile.test.ts diff --git a/src/prefix-affinity.ts b/src/prefix-affinity.ts index 0a456f9bb9..72d43fe44f 100644 --- a/src/prefix-affinity.ts +++ b/src/prefix-affinity.ts @@ -45,6 +45,27 @@ const MAX_TRACKED_SESSIONS = 256; /** Chains unused for this long stop matching (sessions may outlive tracking). */ const TTL_MS = 7 * 24 * 60 * 60 * 1000; +/** Tail-window reattach window (#316 / PR-B): how many of the incoming's + * LEADING items are compared against each stored chain's TRAILING items to + * reattach a truncated replay (a client that drops its oldest messages keeps + * a fixed-size recent window, so the incoming head == the stored tail). */ +const TAIL_WINDOW = 8; + +/** Minimum window size (items) for a tail-window reattach to be trusted. A + * 1-2 item window is too weak a signal (a single shared message is common); + * below this the request falls through to a new session as before. */ +const MIN_TAIL_MATCH = 3; + +/** Per tracked chain, store at most this many per-item hashes (the trailing + * ones). Bounds memory (256 chains × 64 × 64B ≈ 1MB) and keeps the tail + * window (8) comfortably available. Chains deeper than this lose their head, + * so fork-lineage prefix detection is best-effort for very long chains. */ +const MAX_STORED_ITEMS = 64; + +/** Minimum shared prefix (items) to record a "forked" lineage on a new + * session. UI/debug only — never used for matching. */ +const MIN_FORK_PREFIX = 3; + export interface AnonymousAffinity { /** Stable session id: "pfa-" + short hash of (tail, depth). */ sessionId: string; @@ -56,6 +77,16 @@ export interface AnonymousAffinity { incomingDepth: number; /** Chain hash of the incoming tail (log correlation). */ tailHash: string; + /** How the session was resolved: a full-prefix match, a tail-window + * reattach (truncated replay), or a brand-new session. */ + via: "prefix" | "tail-window" | "new"; + /** Per-item hashes of the incoming (trailing, capped at MAX_STORED_ITEMS) — + * passed to note() so the tracked chain can serve future tail-window + * reattach + fork-lineage lookups. */ + itemHashes: string[]; + /** Lineage for a NEW session: the discarded match candidates and why they + * were abandoned. Recorded for UI/debug only — NEVER used for matching. */ + lineage?: { parents: string[]; reason: "truncated" | "forked"; sharedPrefix?: number }; } interface ChainEntry { @@ -63,6 +94,9 @@ interface ChainEntry { depth: number; tailHash: string; lastSeen: number; + /** Per-item hashes (position-independent sha256 of each canonical + * message), trailing, capped at MAX_STORED_ITEMS. */ + itemHashes: string[]; } /** Deterministic JSON with recursively sorted object keys, so two replays @@ -104,6 +138,22 @@ function chainHashes(messages: unknown[]): string[] { return bytes >= MIN_CANONICAL_BYTES ? hashes : []; } +/** Position-INDEPENDENT per-item hashes: itemHashes[i] = sha256(canonical(msg_i)). + * Unlike the progressive chainHashes (which depend on the full prefix and so + * cannot match across a truncation), these let a window of the incoming head + * be compared against a window of a stored tail item-for-item. */ +function perItemHashes(messages: unknown[]): string[] { + return messages.map((m) => sha256(stableStringify(m))); +} + +/** Length of the longest common prefix of two per-item hash arrays. */ +function lcpLength(a: string[], b: string[]): number { + const n = Math.min(a.length, b.length); + let i = 0; + while (i < n && a[i] === b[i]) i++; + return i; +} + export class PrefixAffinityResolver { private trackedChains = new Map(); @@ -115,40 +165,103 @@ export class PrefixAffinityResolver { resolve(messages: unknown[]): AnonymousAffinity | null { const hashes = chainHashes(messages); if (hashes.length === 0 || !hasUserMessage(messages)) return null; - const tailHash = hashes[hashes.length - 1]!; + const incomingDepth = hashes.length; + const tailHash = hashes[incomingDepth - 1]!; + const incItemHashes = perItemHashes(messages); + const storedItemHashes = incItemHashes.slice(-MAX_STORED_ITEMS); const tracked = this.trackedChains; this.expire(tracked); + // 1. Full-depth prefix match (the original radix-style resolution). let best: ChainEntry | undefined; for (const entry of tracked.values()) { - if (entry.depth > hashes.length) continue; + if (entry.depth > incomingDepth) continue; if (hashes[entry.depth - 1] !== entry.tailHash) continue; if (!best || entry.depth > best.depth || (entry.depth === best.depth && entry.lastSeen > best.lastSeen)) best = entry; } - if (best) { return { sessionId: best.sessionId, matchedDepth: best.depth, storedDepth: best.depth, - incomingDepth: hashes.length, + incomingDepth, tailHash, + via: "prefix", + itemHashes: storedItemHashes, }; } - // No stored chain is a prefix of the incoming history: this request - // starts a session, anchored deterministically on its current tail so - // an identical replay after a proxy restart reattaches the same id. - const sessionId = `pfa-${sha256(`${tailHash}\u0000${hashes.length}`).slice(0, 16)}`; - return { sessionId, matchedDepth: 0, storedDepth: 0, incomingDepth: hashes.length, tailHash }; + // 2. Tail-window reattach (#316 / PR-B): a client that dropped its + // oldest messages replays a fixed recent window, so the incoming + // HEAD aligns with a stored chain's TAIL. Compare the incoming's + // leading items against each stored chain's trailing items. + const candidates: ChainEntry[] = []; + for (const entry of tracked.values()) { + const w = Math.min(TAIL_WINDOW, incomingDepth, entry.depth); + if (w < MIN_TAIL_MATCH) continue; + const tailBase = entry.itemHashes.length - w; + if (tailBase < 0) continue; + let match = true; + for (let i = 0; i < w; i++) { + if (incItemHashes[i] !== entry.itemHashes[tailBase + i]) { + match = false; + break; + } + } + if (match) candidates.push(entry); + } + if (candidates.length === 1) { + const entry = candidates[0]!; + const w = Math.min(TAIL_WINDOW, incomingDepth, entry.depth); + return { + sessionId: entry.sessionId, + matchedDepth: w, + storedDepth: entry.depth, + incomingDepth, + tailHash, + via: "tail-window", + itemHashes: storedItemHashes, + }; + } + + // 3. New session, anchored deterministically on its current tail so an + // identical replay after a proxy restart reattaches the same id. + // Record lineage (UI/debug only) for the discarded candidates. + let lineage: AnonymousAffinity["lineage"]; + if (candidates.length > 1) { + lineage = { parents: candidates.map((c) => c.sessionId), reason: "truncated" }; + } else { + let forkParent: ChainEntry | undefined; + let forkLcp = 0; + for (const entry of tracked.values()) { + if (entry.depth > MAX_STORED_ITEMS) continue; + const lcp = lcpLength(incItemHashes, entry.itemHashes); + if (lcp >= MIN_FORK_PREFIX && lcp > forkLcp) { + forkLcp = lcp; + forkParent = entry; + } + } + if (forkParent) lineage = { parents: [forkParent.sessionId], reason: "forked", sharedPrefix: forkLcp }; + } + const sessionId = `pfa-${sha256(`${tailHash}\u0000${incomingDepth}`).slice(0, 16)}`; + return { + sessionId, + matchedDepth: 0, + storedDepth: 0, + incomingDepth, + tailHash, + via: "new", + itemHashes: storedItemHashes, + ...(lineage ? { lineage } : {}), + }; } /** Record the chain of a session (on creation and after every anonymous * request — the incoming history is the truth, appends extend it). */ - note(sessionId: string, depth: number, tailHash: string): void { + note(sessionId: string, depth: number, tailHash: string, itemHashes: string[]): void { const tracked = this.trackedChains; tracked.delete(sessionId); - tracked.set(sessionId, { sessionId, depth, tailHash, lastSeen: Date.now() }); + tracked.set(sessionId, { sessionId, depth, tailHash, itemHashes, lastSeen: Date.now() }); while (tracked.size > MAX_TRACKED_SESSIONS) { const oldest = [...tracked.values()].sort((a, b) => a.lastSeen - b.lastSeen)[0]; if (!oldest) break; diff --git a/src/server.ts b/src/server.ts index 60d2eac1a2..2a46a760f3 100644 --- a/src/server.ts +++ b/src/server.ts @@ -900,12 +900,16 @@ async function handle( : { error: { type: "invalid_request_error", message: NO_IDENTITY_MESSAGE } })); return; } - if (anonAffinity.matchedDepth > 0) { + if (anonAffinity.via === "tail-window") { + log("info", `[prefix-affinity] anonymous ${protocol} request → session ${anonAffinity.sessionId} (tail-window reattach window=${anonAffinity.matchedDepth}/${anonAffinity.incomingDepth}, tail=${anonAffinity.tailHash.slice(0, 8)}; truncated replay reattached, kernel reconciles by message id)`); + loggerLog("info", `[prefix-affinity] session ${anonAffinity.sessionId} reattached via tail-window (window ${anonAffinity.matchedDepth}/${anonAffinity.incomingDepth}, tail=${anonAffinity.tailHash.slice(0, 8)})`); + } else if (anonAffinity.matchedDepth > 0) { log("info", `[prefix-affinity] anonymous ${protocol} request → session ${anonAffinity.sessionId} (prefix match depth=${anonAffinity.matchedDepth}/${anonAffinity.incomingDepth}, tail=${anonAffinity.tailHash.slice(0, 8)}; fork semantics: diverged histories split on their next request)`); loggerLog("info", `[prefix-affinity] session ${anonAffinity.sessionId} matched at depth ${anonAffinity.matchedDepth}/${anonAffinity.incomingDepth} (tail=${anonAffinity.tailHash.slice(0, 8)})`); } else { - log("info", `[prefix-affinity] new anonymous session ${anonAffinity.sessionId} (depth=${anonAffinity.incomingDepth}, tail=${anonAffinity.tailHash.slice(0, 8)})`); - loggerLog("info", `[prefix-affinity] new session ${anonAffinity.sessionId} at depth ${anonAffinity.incomingDepth} (tail=${anonAffinity.tailHash.slice(0, 8)})`); + const lineage = anonAffinity.lineage ? `; lineage=${anonAffinity.lineage.reason} of ${anonAffinity.lineage.parents.join(",")}` : ""; + log("info", `[prefix-affinity] new anonymous session ${anonAffinity.sessionId} (depth=${anonAffinity.incomingDepth}, tail=${anonAffinity.tailHash.slice(0, 8)}${lineage})`); + loggerLog("info", `[prefix-affinity] new session ${anonAffinity.sessionId} at depth ${anonAffinity.incomingDepth} (tail=${anonAffinity.tailHash.slice(0, 8)}${lineage})`); } } const sessionId = anonAffinity ? anonAffinity.sessionId : conversation; @@ -929,10 +933,12 @@ async function handle( : clientConversationHeader(req.headers); const session = getSession(sessionId, { protocol, upstreamOrigin, label: clientLabel ?? (anonAffinity ? "prefix-affinity" : undefined) }); if (anonAffinity) { - prefixAffinity.note(sessionId, anonAffinity.incomingDepth, anonAffinity.tailHash); + prefixAffinity.note(sessionId, anonAffinity.incomingDepth, anonAffinity.tailHash, anonAffinity.itemHashes); session.metadata.anonymousPrefixAffinity = { depth: anonAffinity.incomingDepth, tailHash: anonAffinity.tailHash, + via: anonAffinity.via, + ...(anonAffinity.lineage ? { lineage: anonAffinity.lineage } : {}), }; } // Launcher-mode binding (#162): prefer identity — claude code sends diff --git a/tests/prefix-affinity.test.ts b/tests/prefix-affinity.test.ts index 7680b55648..f51691b5b3 100644 --- a/tests/prefix-affinity.test.ts +++ b/tests/prefix-affinity.test.ts @@ -15,7 +15,7 @@ test("prefix-affinity: append-only continuation resolves to the same session", ( assert.ok(a); assert.equal(a.matchedDepth, 0); assert.match(a.sessionId, /^pfa-[0-9a-f]{16}$/); - r.note(a.sessionId, a.incomingDepth, a.tailHash); + r.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes); const b = r.resolve([user("hello there friend"), { role: "assistant", content: "hi" }, user("second question")]); assert.ok(b); @@ -27,7 +27,7 @@ test("prefix-affinity: append-only continuation resolves to the same session", ( test("prefix-affinity: different conversations with distinct roots stay separate", () => { const r = new PrefixAffinityResolver(); const a = r.resolve([user("project one setup question")]); - r.note(a!.sessionId, a!.incomingDepth, a!.tailHash); + r.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); const b = r.resolve([user("totally different topic opener")]); assert.notEqual(b!.sessionId, a!.sessionId, "different content must not collide"); assert.equal(b!.matchedDepth, 0); @@ -37,12 +37,12 @@ test("prefix-affinity: shared opening, divergent continuation forks on the next const r = new PrefixAffinityResolver(); const shared = [user("same opening message with substance"), { role: "assistant", content: "ok" }]; const a = r.resolve(shared); - r.note(a!.sessionId, a!.incomingDepth, a!.tailHash); + r.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); // Branch A extends first — it keeps the session. const aNext = r.resolve([...shared, user("branch A continuation")]); assert.equal(aNext!.sessionId, a!.sessionId); - r.note(aNext!.sessionId, aNext!.incomingDepth, aNext!.tailHash); + r.note(aNext!.sessionId, aNext!.incomingDepth, aNext!.tailHash, aNext!.itemHashes); // Branch B diverges: its history does not extend the stored chain (hash // at stored depth differs), so it starts its own session. @@ -54,7 +54,7 @@ test("prefix-affinity: shared opening, divergent continuation forks on the next test("prefix-affinity: identical replay after restart reattaches the same deterministic id", () => { const first = new PrefixAffinityResolver(); const a = first.resolve([user("persistent conversation anchor")]); - first.note(a!.sessionId, a!.incomingDepth, a!.tailHash); + first.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); // Fresh process: no in-memory index, same first-turn content. const second = new PrefixAffinityResolver(); @@ -66,7 +66,7 @@ test("prefix-affinity: trimmed history no longer matches — safe new session", const r = new PrefixAffinityResolver(); const full = [user("first message with content"), { role: "assistant", content: "r1" }, user("second message")]; const a = r.resolve(full); - r.note(a!.sessionId, a!.incomingDepth, a!.tailHash); + r.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); // Client-side microcompact drops the middle turn: the stored 3-deep chain // is NOT a prefix of the trimmed history (it is longer). @@ -80,7 +80,7 @@ test("prefix-affinity: no environmental partitioning — same content survives c const r = new PrefixAffinityResolver(); const history = [user("same words across rotating credentials")]; const a = r.resolve(history); - r.note(a!.sessionId, a!.incomingDepth, a!.tailHash); + r.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); // #286 lesson: bearer rotation / relay switching / protocol translation // happen MID-CONVERSATION. There is no partition surface at all, so the // identical replay keeps resolving to the same session. @@ -106,7 +106,7 @@ test("prefix-affinity: system+user openai shape (shared system must not collide) const r = new PrefixAffinityResolver(); const sys = { role: "system", content: "You are ZCode, a shared IDE system prompt injected into every conversation." }; const a = r.resolve([sys, user("question about project A")]); - r.note(a!.sessionId, a!.incomingDepth, a!.tailHash); + r.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); const b = r.resolve([sys, user("question about project B")]); assert.notEqual(b!.sessionId, a!.sessionId, "identical system prefix must not merge distinct conversations"); }); @@ -115,7 +115,7 @@ test("prefix-affinity: LRU cap bounds tracked sessions", () => { const r = new PrefixAffinityResolver(); for (let i = 0; i < 262; i++) { const a = r.resolve([user(`unique conversation number ${i} with filler content`)]); - r.note(a!.sessionId, a!.incomingDepth, a!.tailHash); + r.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); } assert.equal(r.trackedSessionIds().length, 256); }); @@ -130,7 +130,7 @@ test("prefix-affinity: stableStringify sorts keys recursively", () => { test("prefix-affinity: key-order differences across replays still match", () => { const r = new PrefixAffinityResolver(); const a = r.resolve([{ role: "user", content: "key order robustness check", meta: { x: 1, y: 2 } }]); - r.note(a!.sessionId, a!.incomingDepth, a!.tailHash); + r.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); const b = r.resolve([{ meta: { y: 2, x: 1 }, content: "key order robustness check", role: "user" }]); assert.equal(b!.sessionId, a!.sessionId, "same logical message in different key order must match"); }); diff --git a/tests/tail-window-reconcile.test.ts b/tests/tail-window-reconcile.test.ts new file mode 100644 index 0000000000..39449ea2df --- /dev/null +++ b/tests/tail-window-reconcile.test.ts @@ -0,0 +1,134 @@ +import assert from "node:assert"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { PrefixAffinityResolver } from "../src/prefix-affinity.ts"; +import { createCore, createInitialState, defaultConfig } from "acp-kernel"; +import { anthropicToCore, type AnthropicRequestBody } from "acp-kernel/wire"; + +function user(text: string): Record { + return { role: "user", content: text }; +} + +// N distinct user messages (content long enough to clear MIN_CANONICAL_BYTES). +function chain(n: number, prefix = "msg"): Record[] { + const out: Record[] = []; + for (let i = 0; i < n; i++) out.push(user(`${prefix} ${i} with enough substance to hash`)); + return out; +} + +test("tail-window: truncated replay (drop oldest) reattaches the original session", () => { + const r = new PrefixAffinityResolver(); + const full = chain(10); + const a = r.resolve(full)!; + assert.equal(a.via, "new"); + r.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes); + + // Client keeps a fixed recent window: drops the 2 oldest, appends 1 new. + // Incoming head (first 8) == stored tail (last 8) → tail-window reattach. + const truncated = [...full.slice(2), user("a brand new tenth-plus turn here")]; + const b = r.resolve(truncated)!; + assert.equal(b.sessionId, a.sessionId, "truncated replay must reattach the original session"); + assert.equal(b.via, "tail-window"); + assert.equal(b.matchedDepth, 8, "window is capped at TAIL_WINDOW=8"); + assert.equal(b.storedDepth, 10); + assert.equal(b.incomingDepth, 9); +}); + +test("tail-window: append-only continuation still resolves via prefix (regression)", () => { + const r = new PrefixAffinityResolver(); + const base = chain(4); + const a = r.resolve(base)!; + r.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes); + const b = r.resolve([...base, user("an appended continuation turn")])!; + assert.equal(b.sessionId, a.sessionId); + assert.equal(b.via, "prefix", "a true prefix extension must not be misread as a tail reattach"); + assert.equal(b.matchedDepth, 4); +}); + +test("tail-window: brand-new conversation resolves via new (regression)", () => { + const r = new PrefixAffinityResolver(); + const a = r.resolve(chain(3, "alpha"))!; + r.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes); + const b = r.resolve(chain(3, "beta"))!; + assert.equal(b.via, "new"); + assert.equal(b.matchedDepth, 0); + assert.notEqual(b.sessionId, a.sessionId); +}); + +test("fork lineage: divergent continuation records forked lineage (UI/debug only)", () => { + const r = new PrefixAffinityResolver(); + const shared = chain(4, "shared"); + const a = r.resolve(shared)!; + r.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes); + + // Replace the 4th item: shares a 3-item prefix, diverges at item 4. + const divergent = [...shared.slice(0, 3), user("a divergent fourth turn here")]; + const b = r.resolve(divergent)!; + assert.equal(b.via, "new", "a divergence must not steal the stored chain"); + assert.notEqual(b.sessionId, a.sessionId); + assert.equal(b.lineage?.reason, "forked"); + assert.deepEqual(b.lineage?.parents, [a.sessionId]); + assert.equal(b.lineage?.sharedPrefix, 3); +}); + +test("truncated lineage: ambiguous multi-candidate records truncated lineage", () => { + const r = new PrefixAffinityResolver(); + // Two tracked chains that share the same trailing 8 items but differ in + // their oldest items. A truncated replay whose head == that shared tail is + // ambiguous → new session with a "truncated" lineage naming both parents. + const tail = chain(8, "common-tail"); + const aFull = [...chain(2, "branch-a-head"), ...tail]; + const bFull = [...chain(2, "branch-b-head"), ...tail]; + const a = r.resolve(aFull)!; + r.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes); + const b = r.resolve(bFull)!; + r.note(b.sessionId, b.incomingDepth, b.tailHash, b.itemHashes); + + const incoming = [...tail, user("a fresh turn after truncation")]; + const c = r.resolve(incoming)!; + assert.equal(c.via, "new", "an ambiguous tail match must not guess a parent"); + assert.equal(c.lineage?.reason, "truncated"); + assert.ok(c.lineage?.parents.includes(a.sessionId)); + assert.ok(c.lineage?.parents.includes(b.sessionId)); +}); + +// Kernel reconcile anchor (#316 / PR-B): a truncated replay against +// block-bearing state must NOT throw, must deactivate the affected block +// gracefully (active → false), and must revive it (survivedCount++) when the +// full history returns. This is what makes tail-window reattach safe with zero +// kernel changes. +test("kernel reconcile: truncated replay deactivates blocks, full replay revives them (no throw)", () => { + const core = createCore(); + const config = defaultConfig(200000); + const state = createInitialState(); + const body: AnthropicRequestBody = { model: "claude-test", messages: [] }; + for (let i = 0; i < 40; i++) body.messages.push({ role: i % 2 === 0 ? "user" : "assistant", content: `message ${i} ${"y".repeat(500)}` }); + const { msgs } = anthropicToCore(body); + const turn = core.processTurn({ messages: msgs, state, config, tokenCount: 9999, renderTags: "text-only" }); + const res = core.applyCompression({ + ranges: [{ startRef: "m00001", endRef: "m00015", summary: "early history summary long enough to pass min length check".repeat(3) }], + state: turn.state, config, messages: turn.messages, + }); + let st = res.state; + const block = st.blocks[0]; + assert.ok(block, "a compression block should exist"); + assert.equal(block.active, true, "block starts active"); + const survivedBefore = block.survivedCount; + + // Truncated replay: drop the 10 oldest messages (which include the covered ones). + const truncated = core.processTurn({ messages: msgs.slice(10), state: st, config, tokenCount: 9999, renderTags: "text-only" }); + st = truncated.state; + const deactivated = st.blocks.find((b) => b.blockId === block.blockId); + assert.ok(deactivated, "block still present after truncation"); + assert.equal(deactivated.active, false, "block must deactivate gracefully when its messages are truncated away"); + + // Full replay: the messages return → the block revives. + const full = core.processTurn({ messages: msgs, state: st, config, tokenCount: 9999, renderTags: "text-only" }); + st = full.state; + const revived = st.blocks.find((b) => b.blockId === block.blockId); + assert.ok(revived, "block still present after full replay"); + assert.equal(revived.active, true, "block must revive when its messages return"); + assert.equal(revived.survivedCount, survivedBefore + 1, "survivedCount must increment on revival"); +}); From 40eb72e1d085a78ae2790ca6cdf241def3aa034e Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Fri, 28 Aug 2026 23:58:32 +0800 Subject: [PATCH 03/18] fix: partition any non-user codex thread_source by thread-id (PR review) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit guardian_review / guardian_classifier (Feature string) / memory_consolidation threads previously fell back to the legacy chain and landed on the root session — the exact #150 isolation violation. Invert the discrimination: only "user" joins the root session; every other thread_source with a valid thread_id cross-check partitions by thread-id. --- src/session-id.ts | 31 ++++++++++++++++++++----------- tests/proxy-session-id.test.ts | 22 ++++++++++++++++++++-- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/session-id.ts b/src/session-id.ts index 5e016c76fa..2b1a8ec470 100644 --- a/src/session-id.ts +++ b/src/session-id.ts @@ -106,15 +106,22 @@ export function preferPromptCacheKeyIdentity( * `thread-id` header), partition by thread_source: * - "user" → the `session-id` header (current root semantics, stable * across turns) - * - "subagent" → the `thread-id` header (fresh independent state per - * thread; self-contained replay is lossless) - * Any other thread_source, unparseable JSON, missing/mismatched thread_id, or - * a missing `session-id` header on a "user" turn → undefined: the caller - * falls through to the legacy chain unchanged. + * - anything else → the `thread-id` header (fresh independent state per + * thread; self-contained replay is lossless). codex's + * ThreadSource serializes more than user/subagent — + * "guardian_review" (review sessions), "memory_consolidation", + * and arbitrary Feature strings such as "guardian_classifier" + * (guardian-v2 async scorer) — all of which are internal + * threads that need #150 isolation just as much, so the + * discrimination is inverted: only "user" joins the root + * session, every other source gets its own thread state. + * A non-string/empty thread_source, unparseable JSON, missing/mismatched + * thread_id, or a missing `session-id` header on a "user" turn → undefined: + * the caller falls through to the legacy chain unchanged. */ export type CodexTurnIdentity = { value: string; - threadSource: "user" | "subagent"; + threadSource: string; }; export function codexTurnIdentity(headers: Record): CodexTurnIdentity | undefined { @@ -129,13 +136,15 @@ export function codexTurnIdentity(headers: Record; const threadSource = record["thread_source"]; - if (threadSource !== "user" && threadSource !== "subagent") return undefined; + if (typeof threadSource !== "string" || threadSource.trim().length === 0) return undefined; const metaThreadId = record["thread_id"]; if (typeof metaThreadId !== "string" || metaThreadId.trim().length === 0) return undefined; const threadHeader = headers["thread-id"]; if (typeof threadHeader !== "string" || threadHeader.trim() !== metaThreadId.trim()) return undefined; - if (threadSource === "subagent") return { value: threadHeader.trim(), threadSource }; - const sessionHeader = headers["session-id"]; - if (typeof sessionHeader !== "string" || sessionHeader.trim().length === 0) return undefined; - return { value: sessionHeader.trim(), threadSource }; + if (threadSource.trim() === "user") { + const sessionHeader = headers["session-id"]; + if (typeof sessionHeader !== "string" || sessionHeader.trim().length === 0) return undefined; + return { value: sessionHeader.trim(), threadSource: "user" }; + } + return { value: threadHeader.trim(), threadSource }; } diff --git a/tests/proxy-session-id.test.ts b/tests/proxy-session-id.test.ts index 9e1cd651cf..560c003624 100644 --- a/tests/proxy-session-id.test.ts +++ b/tests/proxy-session-id.test.ts @@ -197,12 +197,30 @@ test("codexTurnIdentity: JSON parse failure / non-object → do not trust (legac assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": " " }), undefined); }); -test("codexTurnIdentity: unknown/missing thread_source → do not trust (legacy chain)", () => { - assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ thread_source: "system" }) }), undefined); +test("codexTurnIdentity: non-string/missing thread_source → do not trust (legacy chain)", () => { assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ thread_source: undefined }) }), undefined); + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ thread_source: 42 }) }), undefined); + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ thread_source: " " }) }), undefined); assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ thread_id: 123 }) }), undefined); }); +test("codexTurnIdentity: any non-user thread_source partitions by thread-id (guardian family, #150)", () => { + // codex ThreadSource serializes more than user/subagent: review sessions + // send "guardian_review", the guardian-v2 async scorer sends + // "guardian_classifier" (Feature string), and memory consolidation sends + // "memory_consolidation". All are internal threads that must NOT inherit + // the root session's compression state — the discrimination is inverted: + // only "user" joins the root, everything else gets its own thread state. + for (const source of ["subagent", "guardian_review", "guardian_classifier", "memory_consolidation", "collab_spawn", "system"]) { + const id = codexTurnIdentity({ + "session-id": ROOT_SESSION, + "thread-id": SUB_THREAD, + "x-codex-turn-metadata": subMeta({ thread_source: source, thread_id: SUB_THREAD }), + }); + assert.deepEqual(id, { value: SUB_THREAD, threadSource: source }, `thread_source=${source}`); + } +}); + test("codexTurnIdentity: user turn with no session-id header → do not trust (legacy chain)", () => { assert.equal(codexTurnIdentity({ "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta() }), undefined); }); From 3058729f2d1d5832438df9868dc882603e7683b1 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Fri, 28 Aug 2026 23:58:38 +0800 Subject: [PATCH 04/18] fix: tail-window reattach matches at any offset, not just the stored tail (PR review) Tail-pinned matching only reattached when the client retained <= TAIL_WINDOW (8) items; a rolling-window client keeping 20-30 items orphaned on every slide. Search the incoming's leading window across the stored chain's per-item hashes at any offset (<= 57 offsets x 8 compares, trivial). MAX_STORED_ITEMS 64 -> 128 so deeper windows stay searchable. --- src/prefix-affinity.ts | 48 +++++++++++++++++------------ tests/tail-window-reconcile.test.ts | 26 ++++++++++++++++ 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/src/prefix-affinity.ts b/src/prefix-affinity.ts index 72d43fe44f..e4aad652c7 100644 --- a/src/prefix-affinity.ts +++ b/src/prefix-affinity.ts @@ -45,10 +45,11 @@ const MAX_TRACKED_SESSIONS = 256; /** Chains unused for this long stop matching (sessions may outlive tracking). */ const TTL_MS = 7 * 24 * 60 * 60 * 1000; -/** Tail-window reattach window (#316 / PR-B): how many of the incoming's - * LEADING items are compared against each stored chain's TRAILING items to - * reattach a truncated replay (a client that drops its oldest messages keeps - * a fixed-size recent window, so the incoming head == the stored tail). */ +/** Tail-window reattach window (#316 / PR-B): the incoming's LEADING items + * are searched for as a contiguous run at ANY offset inside each stored + * chain's per-item hashes (the incoming head is the retained suffix of the + * stored history, but a rolling-window client may retain any number of + * items, so the match offset must be free — not pinned to the stored tail). */ const TAIL_WINDOW = 8; /** Minimum window size (items) for a tail-window reattach to be trusted. A @@ -57,10 +58,10 @@ const TAIL_WINDOW = 8; const MIN_TAIL_MATCH = 3; /** Per tracked chain, store at most this many per-item hashes (the trailing - * ones). Bounds memory (256 chains × 64 × 64B ≈ 1MB) and keeps the tail + * ones). Bounds memory (256 chains × 128 × 64B ≈ 2MB) and keeps the tail * window (8) comfortably available. Chains deeper than this lose their head, * so fork-lineage prefix detection is best-effort for very long chains. */ -const MAX_STORED_ITEMS = 64; +const MAX_STORED_ITEMS = 128; /** Minimum shared prefix (items) to record a "forked" lineage on a new * session. UI/debug only — never used for matching. */ @@ -192,23 +193,30 @@ export class PrefixAffinityResolver { } // 2. Tail-window reattach (#316 / PR-B): a client that dropped its - // oldest messages replays a fixed recent window, so the incoming - // HEAD aligns with a stored chain's TAIL. Compare the incoming's - // leading items against each stored chain's trailing items. + // oldest messages replays a retained suffix of the stored history + // (plus new appends), so the incoming's LEADING items must appear + // as a contiguous run somewhere inside the stored chain's item + // hashes — at any offset, not just the stored tail (a rolling-window + // client may retain more items than TAIL_WINDOW). + const w = Math.min(TAIL_WINDOW, incomingDepth); const candidates: ChainEntry[] = []; - for (const entry of tracked.values()) { - const w = Math.min(TAIL_WINDOW, incomingDepth, entry.depth); - if (w < MIN_TAIL_MATCH) continue; - const tailBase = entry.itemHashes.length - w; - if (tailBase < 0) continue; - let match = true; - for (let i = 0; i < w; i++) { - if (incItemHashes[i] !== entry.itemHashes[tailBase + i]) { - match = false; - break; + if (w >= MIN_TAIL_MATCH) { + for (const entry of tracked.values()) { + const stored = entry.itemHashes; + for (let j = 0; j + w <= stored.length; j++) { + let match = true; + for (let i = 0; i < w; i++) { + if (incItemHashes[i] !== stored[j + i]) { + match = false; + break; + } + } + if (match) { + candidates.push(entry); + break; + } } } - if (match) candidates.push(entry); } if (candidates.length === 1) { const entry = candidates[0]!; diff --git a/tests/tail-window-reconcile.test.ts b/tests/tail-window-reconcile.test.ts index 39449ea2df..ae3099fa5c 100644 --- a/tests/tail-window-reconcile.test.ts +++ b/tests/tail-window-reconcile.test.ts @@ -36,6 +36,32 @@ test("tail-window: truncated replay (drop oldest) reattaches the original sessio assert.equal(b.incomingDepth, 9); }); +test("tail-window: rolling-window client retaining > TAIL_WINDOW items reattaches at any offset", () => { + // A dsh-style web client keeps a 20-30 item rolling window: the retained + // suffix is LONGER than TAIL_WINDOW, so the incoming head matches the + // stored chain in the middle, not at its tail. The any-offset substring + // search must still find it (tail-pinned matching would orphan it). + const r = new PrefixAffinityResolver(); + const full = chain(30); + const a = r.resolve(full)!; + assert.equal(a.via, "new"); + r.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes); + + // Sliding window: retains the last 20 (> TAIL_WINDOW=8), appends 1 new. + const slid = [...full.slice(10), user("a brand new turn after the window slid")]; + const b = r.resolve(slid)!; + assert.equal(b.sessionId, a.sessionId, "a >8-item rolling window must reattach the original session"); + assert.equal(b.via, "tail-window"); + assert.equal(b.matchedDepth, 8); + + // Deeper slides keep reattaching as the window keeps sliding. + r.note(b.sessionId, b.incomingDepth, b.tailHash, b.itemHashes); + const slid2 = [...full.slice(20), user("yet another turn after sliding again")]; + const c = r.resolve(slid2)!; + assert.equal(c.sessionId, a.sessionId, "a second slide must reattach the same session"); + assert.equal(c.via, "tail-window"); +}); + test("tail-window: append-only continuation still resolves via prefix (regression)", () => { const r = new PrefixAffinityResolver(); const base = chain(4); From 0bca1b7a31658058b863ec9550550592e14ac87b Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 29 Aug 2026 00:33:39 +0800 Subject: [PATCH 05/18] feat: align launcher auto-compact budgets with bili window (codex -c, claude env) (#321 PR-D) --- src/client-config.ts | 42 +++++- src/launcher.ts | 131 +++++++++++++++- tests/launcher.test.ts | 330 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 498 insertions(+), 5 deletions(-) diff --git a/src/client-config.ts b/src/client-config.ts index 510829baf6..7e4496dc2c 100644 --- a/src/client-config.ts +++ b/src/client-config.ts @@ -8,6 +8,12 @@ import path from "node:path"; export interface ClaudeSettings { anthropicBaseUrl?: string; + /** Model claude runs: settings `env.ANTHROPIC_MODEL` ?? top-level `model`. */ + model?: string; + /** The user's explicit auto-compact window (settings `autoCompactWindow` + * or `env.CLAUDE_CODE_AUTO_COMPACT_WINDOW`) — when set, the launcher + * must NOT override it with its own budget injection (#321). */ + autoCompactWindow?: number; } export interface ModelWindow { @@ -28,6 +34,12 @@ export interface CodexProvider { export interface CodexConfig { modelProvider?: string; openaiBaseUrl?: string; + /** Top-level `model` — the model codex runs (budget alignment, #321). */ + model?: string; + /** Top-level `model_context_window` override (if set). */ + contextWindow?: number; + /** Top-level `model_auto_compact_token_limit` override (if set). */ + autoCompactLimit?: number; /** Top-level `model` + `model_context_window` override pair (if set). */ modelWindows?: ModelWindow[]; providers: Record; @@ -173,18 +185,35 @@ export function readClaudeSettings(homeDir: string, cwd: string, env: NodeJS.Pro path.join(cwd, ".claude", "settings.json"), ]; let anthropicBaseUrl: string | undefined; + let model: string | undefined; + let autoCompactWindow: number | undefined; for (const f of files) { const obj = readJsonObject(f); const settingsEnv = obj?.env; if (settingsEnv && typeof settingsEnv === "object" && !Array.isArray(settingsEnv)) { - const v = (settingsEnv as Record).ANTHROPIC_BASE_URL; + const e = settingsEnv as Record; + const v = e.ANTHROPIC_BASE_URL; if (nonEmpty(v)) anthropicBaseUrl = v; + // env-block values beat same-file top-level settings (claude applies + // the env block as real environment, which outranks settings). + const m = e.ANTHROPIC_MODEL; + if (nonEmpty(m)) model = String(m); + const acw = Number(e.CLAUDE_CODE_AUTO_COMPACT_WINDOW); + if (Number.isFinite(acw) && acw > 0) autoCompactWindow = acw; } + const tm = obj?.model; + if (nonEmpty(tm) && model === undefined) model = String(tm); + const tacw = Number(obj?.autoCompactWindow); + if (Number.isFinite(tacw) && tacw > 0 && autoCompactWindow === undefined) autoCompactWindow = tacw; } // Honor a shell-exported ANTHROPIC_BASE_URL (claude's native override) so // the launcher wraps the relay the user actually uses, not the default. if (!anthropicBaseUrl && nonEmpty(env.ANTHROPIC_BASE_URL)) anthropicBaseUrl = env.ANTHROPIC_BASE_URL; - return anthropicBaseUrl ? { anthropicBaseUrl } : {}; + return { + ...(anthropicBaseUrl ? { anthropicBaseUrl } : {}), + ...(model ? { model } : {}), + ...(autoCompactWindow ? { autoCompactWindow } : {}), + }; } /** @@ -198,6 +227,7 @@ export function parseCodexToml(text: string): CodexConfig { let curProvider: string | null = null; let codexModel: string | undefined; let codexContextWindow: number | undefined; + let codexAutoCompactLimit: number | undefined; for (const rawLine of text.split(/\r?\n/)) { const line = rawLine.trim(); if (!line || line.startsWith("#")) continue; @@ -224,10 +254,14 @@ export function parseCodexToml(text: string): CodexConfig { } else if (curProvider && key === "base_url") { result.providers[curProvider].baseUrl = val; } - } else if (numMatch && table === "" && numMatch[1] === "model_context_window") { - codexContextWindow = Number(numMatch[2]); + } else if (numMatch && table === "") { + if (numMatch[1] === "model_context_window") codexContextWindow = Number(numMatch[2]); + else if (numMatch[1] === "model_auto_compact_token_limit") codexAutoCompactLimit = Number(numMatch[2]); } } + if (codexModel) result.model = codexModel; + if (codexContextWindow) result.contextWindow = codexContextWindow; + if (codexAutoCompactLimit) result.autoCompactLimit = codexAutoCompactLimit; const win = toModelWindow(codexModel, codexContextWindow); if (win) result.modelWindows = [win]; return result; diff --git a/src/launcher.ts b/src/launcher.ts index 20b2235750..dd7048b7c9 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -42,7 +42,9 @@ import { selfPackageRoot, isBiliPiEntry, ompPluginLoadedFrom } from "./plugin-in function selfDistFile(name: string): string { return path.join(selfPackageRoot(), "dist", name); } -import { nonEmpty, resolvePiHome, resolveOmpHome, resolveHermesHome, resolveDshHome, loadClientConfig, collectModelWindows, type ClientConfig, resolveOpencodeConfigFile, type OpencodeConfig, type OpencodeProvider, type HermesConfig, type HermesProvider } from "./client-config.js"; +import { nonEmpty, resolvePiHome, resolveOmpHome, resolveHermesHome, resolveDshHome, loadClientConfig, collectModelWindows, type ClientConfig, type CodexConfig, resolveOpencodeConfigFile, type OpencodeConfig, type OpencodeProvider, type HermesConfig, type HermesProvider } from "./client-config.js"; +import { loadRoutes, resolveConfiguredContextLimit, lookupContextLimit, type ProviderRoutes } from "./config.js"; +import { contextFromRegistry } from "./registry.js"; export { type ClaudeSettings, @@ -349,6 +351,108 @@ export function buildCodexArgs( return args; } +/** + * PR-D (#321): budget alignment for launcher-spawned agents. Resolves the + * context window bili will use as its compression denominator for `model` — + * the same chain the proxy applies, minus the per-request-only sources + * (anthropic-beta header, plugin report, launcher window): + * per-route per-model config declaration → built-in CONTEXT_LIMIT_TABLE + * → models.dev registry (snapshot-first, offline-safe). + */ +export async function resolveLauncherWindow( + model: string | undefined, + routes: ProviderRoutes, + upstreamUrl: string | undefined, +): Promise { + if (!model) return undefined; + let host: string | undefined; + if (upstreamUrl) { + try { + host = new URL(upstreamUrl).hostname; + } catch { + host = undefined; + } + } + return ( + resolveConfiguredContextLimit(routes, upstreamUrl, model) ?? + lookupContextLimit(model) ?? + (await contextFromRegistry(model, host)) + ); +} + +/** + * PR-D (#321): codex's auto-compact budget is keyed off the window CODEX + * believes the model has (its bundled model table — bili has no say in it), + * while bili's ACP compression is keyed off bili's own window resolution. + * Two uncoordinated budgets (#292): when bili's window exceeds codex's + * perception, codex's ledger (server-reported usage, which bili sees) crosses + * codex's ~90% threshold first and fires its native compaction ahead of ACP. + * + * Injecting `-c model_context_window= -c model_auto_compact_token_limit=` + * (W = bili's effective window) makes codex's auto-compact threshold 90%×W — + * ACP (≈55%×W) always fires first, and codex's LOCAL compaction (benign for + * bili: same-session truncation the kernel deactivates by message id) only + * backstops when ACP fails, before codex's 95% hard cap. codex clamps both + * values to its own max_context_window, so an over-generous W degrades to + * codex's own perception instead of overshooting. + * + * Returns [] (no injection) when: + * - no model is configured (nothing to resolve a window for), + * - the user already set `model_context_window` in codex's config.toml + * (bili's proxy uses exactly that value as its launcher window — the + * budget is already aligned by the user's own declaration), + * - bili resolves no window for the model (no authoritative value to inject). + * A user-set `model_auto_compact_token_limit` is honored (not overridden). + */ +/** The base URL codex will actually call: the selected provider's + * `base_url`, else top-level `openai_base_url`, else codex's built-in + * OpenAI default (model-provider-info: `https://api.openai.com/v1`). */ +export function codexUpstreamUrl(codex: CodexConfig | undefined): string { + const provider = codex?.modelProvider ? codex?.providers?.[codex.modelProvider]?.baseUrl : undefined; + return provider ?? codex?.openaiBaseUrl ?? "https://api.openai.com/v1"; +} + +export async function resolveCodexBudgetArgs(opts: { + model: string | undefined; + clientWindow: number | undefined; + clientAutoCompactLimit: number | undefined; + routes: ProviderRoutes; + upstreamUrl: string | undefined; +}): Promise { + const { model, clientWindow, clientAutoCompactLimit, routes, upstreamUrl } = opts; + if (!model || clientWindow) return []; + const window = await resolveLauncherWindow(model, routes, upstreamUrl); + if (!window) return []; + const limit = clientAutoCompactLimit ?? window; + return ["-c", `model_context_window=${window}`, "-c", `model_auto_compact_token_limit=${limit}`]; +} + +/** + * PR-D (#321): claude-code's auto-compact window is a single env knob — + * `CLAUDE_CODE_AUTO_COMPACT_WINDOW` outranks settings and is clamped DOWN to + * the model window claude itself perceives (never up), so injecting bili's + * window is always safe: it tightens claude's threshold to bili's budget when + * bili's window is smaller, and is a no-op when it is larger. + * + * Returns {} (no injection) when: no model resolvable, the user already set + * an explicit auto-compact window (settings `autoCompactWindow` or + * `env.CLAUDE_CODE_AUTO_COMPACT_WINDOW`, or a shell-exported env var), or + * bili resolves no window for the model. + */ +export async function resolveClaudeBudgetEnv(opts: { + model: string | undefined; + userAutoCompactWindow: number | undefined; + shellAutoCompactWindow: string | undefined; + routes: ProviderRoutes; + upstreamUrl: string | undefined; +}): Promise { + const { model, userAutoCompactWindow, shellAutoCompactWindow, routes, upstreamUrl } = opts; + if (!model || userAutoCompactWindow !== undefined || nonEmpty(shellAutoCompactWindow)) return {}; + const window = await resolveLauncherWindow(model, routes, upstreamUrl); + if (!window) return {}; + return { CLAUDE_CODE_AUTO_COMPACT_WINDOW: String(window) }; +} + export function buildClaudeEnv( origin: string, caPath: string, @@ -1278,6 +1382,9 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} const config = loadClientConfig(process.env, process.cwd()); const base = baseClientName(params.client); const routes = discoverRoutes(base, config); + // bili's own route graph (same sources the spawned proxy child reads — + // used to resolve the budget-alignment window, #321). + const biliRoutes = loadRoutes(process.env); const domains = dedupeInOrder([...routes.httpsDomains, ...(params.mitmDomains ?? [])]); const handle = await ensureProxyRunning({ host, port, passthrough, debug, mitmDomains: domains, modelWindows: collectModelWindows(config) }, deps); console.error( @@ -1436,6 +1543,17 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} } else { env = buildCodexEnv(origin, resolveCombinedCaPath(process.env), process.env); clientArgs = buildCodexArgs(origin, routes.httpRewrites, routes.httpsRewrites, clientArgs); + const budgetArgs = await resolveCodexBudgetArgs({ + model: config.codex?.model, + clientWindow: config.codex?.contextWindow, + clientAutoCompactLimit: config.codex?.autoCompactLimit, + routes: biliRoutes, + upstreamUrl: codexUpstreamUrl(config.codex), + }); + if (budgetArgs.length > 0) { + clientArgs = [...budgetArgs, ...clientArgs]; + console.error(`bili: codex budget aligned — ${budgetArgs.slice(2).join(", ")} (model: ${config.codex?.model})`); + } if (injectMcp && codexConversationId) clientArgs = [...buildCodexMcpArgs(origin, codexConversationId), ...clientArgs]; } } else { @@ -1443,6 +1561,17 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} ? buildClaudePluginEnv(origin, true, process.env) : buildClaudeEnv(origin, ca, routes.httpRewrites, routes.httpsRewrites, process.env); if (directUrl) env.BILLION_CONTEXT_PROXY = origin; + const claudeBudget = await resolveClaudeBudgetEnv({ + model: nonEmpty(process.env.ANTHROPIC_MODEL) ? process.env.ANTHROPIC_MODEL : config.claude?.model, + userAutoCompactWindow: config.claude?.autoCompactWindow, + shellAutoCompactWindow: process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW, + routes: biliRoutes, + upstreamUrl: config.claude?.anthropicBaseUrl ?? "https://api.anthropic.com", + }); + Object.assign(env, claudeBudget); + if (claudeBudget.CLAUDE_CODE_AUTO_COMPACT_WINDOW !== undefined) { + console.error(`bili: claude budget aligned — CLAUDE_CODE_AUTO_COMPACT_WINDOW=${claudeBudget.CLAUDE_CODE_AUTO_COMPACT_WINDOW}`); + } if (injectMcp) { const mcpFile = path.join(os.tmpdir(), `bili-mcp-${Date.now()}.json`); fs.writeFileSync(mcpFile, JSON.stringify(buildMcpConfig(origin))); diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index 23fa7d5f32..170c1f2e02 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -48,12 +48,18 @@ import { findFreePort, ensureProxyRunning, stopProxy, + resolveLauncherWindow, + resolveCodexBudgetArgs, + resolveClaudeBudgetEnv, + codexUpstreamUrl, + readClaudeSettings, type SpawnChild, type SpawnFn, runLaunch, type ClientConfig, type HttpRewrite, } from "../src/launcher.ts"; +import { _setForTest as registrySetForTest, _resetForTest as registryResetForTest } from "../src/registry.ts"; test("isLaunchClient: pi/claude/codex/omp/opencode/pi-test true, others false", () => { assert.equal(isLaunchClient("pi"), true); @@ -1721,3 +1727,327 @@ test("runLaunch omp: launcher hands per-model windows to the spawned proxy", asy fs.rmSync(home, { recursive: true, force: true }); } }); + +// --- PR-D (#321): launcher budget alignment (codex -c / claude env) --- + +test("codexUpstreamUrl: provider base_url > openai_base_url > built-in default", () => { + assert.equal(codexUpstreamUrl(undefined), "https://api.openai.com/v1"); + assert.equal(codexUpstreamUrl({ providers: {} }), "https://api.openai.com/v1"); + assert.equal( + codexUpstreamUrl({ providers: {}, openaiBaseUrl: "https://relay.example.com/v1" }), + "https://relay.example.com/v1", + ); + assert.equal( + codexUpstreamUrl({ + modelProvider: "relay", + openaiBaseUrl: "https://openai.example.com/v1", + providers: { relay: { baseUrl: "https://relay.example.com/v1" } }, + }), + "https://relay.example.com/v1", + ); + assert.equal( + codexUpstreamUrl({ modelProvider: "missing", openaiBaseUrl: "https://openai.example.com/v1", providers: {} }), + "https://openai.example.com/v1", + ); +}); + +test("resolveLauncherWindow: config route > built-in table > registry > nothing", async () => { + const routes = { "https://api.openai.com/v1": { models: { "gpt-x": { context: 123456 } } } }; + registrySetForTest({}); + try { + assert.equal(await resolveLauncherWindow("gpt-x", routes, "https://api.openai.com/v1"), 123456); + assert.equal(await resolveLauncherWindow("gpt-5.5", {}, "https://api.openai.com/v1"), 400000); + registrySetForTest({ "openai/bili-fallback-model": { limit: { context: 333333 } } }); + assert.equal(await resolveLauncherWindow("bili-fallback-model", {}, "https://api.openai.com/v1"), 333333); + assert.equal(await resolveLauncherWindow("bili-nonexistent-model-xyz", {}, "https://api.openai.com/v1"), undefined); + assert.equal(await resolveLauncherWindow(undefined, routes, "https://api.openai.com/v1"), undefined); + } finally { + registryResetForTest(); + } +}); + +test("resolveCodexBudgetArgs: injects window + same-value limit from bili's chain", async () => { + registrySetForTest({}); + try { + assert.deepEqual( + await resolveCodexBudgetArgs({ model: "gpt-5.5", clientWindow: undefined, clientAutoCompactLimit: undefined, routes: {}, upstreamUrl: "https://api.openai.com/v1" }), + ["-c", "model_context_window=400000", "-c", "model_auto_compact_token_limit=400000"], + ); + const routes = { "https://relay.example.com/v1": { models: { "gpt-x": { context: 123456 } } } }; + assert.deepEqual( + await resolveCodexBudgetArgs({ model: "gpt-x", clientWindow: undefined, clientAutoCompactLimit: undefined, routes, upstreamUrl: "https://relay.example.com/v1" }), + ["-c", "model_context_window=123456", "-c", "model_auto_compact_token_limit=123456"], + ); + registrySetForTest({ "openai/bili-fallback-model": { limit: { context: 333333 } } }); + assert.deepEqual( + await resolveCodexBudgetArgs({ model: "bili-fallback-model", clientWindow: undefined, clientAutoCompactLimit: undefined, routes: {}, upstreamUrl: "https://api.openai.com/v1" }), + ["-c", "model_context_window=333333", "-c", "model_auto_compact_token_limit=333333"], + ); + } finally { + registryResetForTest(); + } +}); + +test("resolveCodexBudgetArgs: no injection when user self-aligned or unresolvable", async () => { + registrySetForTest({}); + try { + assert.deepEqual(await resolveCodexBudgetArgs({ model: "gpt-5.5", clientWindow: 1000000, clientAutoCompactLimit: undefined, routes: {}, upstreamUrl: "https://api.openai.com/v1" }), []); + assert.deepEqual(await resolveCodexBudgetArgs({ model: undefined, clientWindow: undefined, clientAutoCompactLimit: undefined, routes: {}, upstreamUrl: "https://api.openai.com/v1" }), []); + assert.deepEqual(await resolveCodexBudgetArgs({ model: "bili-nonexistent-model-xyz", clientWindow: undefined, clientAutoCompactLimit: undefined, routes: {}, upstreamUrl: "https://api.openai.com/v1" }), []); + } finally { + registryResetForTest(); + } +}); + +test("resolveCodexBudgetArgs: honors user-set model_auto_compact_token_limit", async () => { + assert.deepEqual( + await resolveCodexBudgetArgs({ model: "gpt-5.5", clientWindow: undefined, clientAutoCompactLimit: 111111, routes: {}, upstreamUrl: "https://api.openai.com/v1" }), + ["-c", "model_context_window=400000", "-c", "model_auto_compact_token_limit=111111"], + ); +}); + +test("resolveClaudeBudgetEnv: injects CLAUDE_CODE_AUTO_COMPACT_WINDOW from bili's chain", async () => { + registrySetForTest({}); + try { + assert.deepEqual( + await resolveClaudeBudgetEnv({ model: "claude-sonnet-4-5", userAutoCompactWindow: undefined, shellAutoCompactWindow: undefined, routes: {}, upstreamUrl: "https://api.anthropic.com" }), + { CLAUDE_CODE_AUTO_COMPACT_WINDOW: "200000" }, + ); + const routes = { "https://relay.example.com": { models: { "claude-x": { context: 123456 } } } }; + assert.deepEqual( + await resolveClaudeBudgetEnv({ model: "claude-x", userAutoCompactWindow: undefined, shellAutoCompactWindow: undefined, routes, upstreamUrl: "https://relay.example.com" }), + { CLAUDE_CODE_AUTO_COMPACT_WINDOW: "123456" }, + ); + registrySetForTest({ "anthropic/bili-fallback-model": { limit: { context: 333333 } } }); + assert.deepEqual( + await resolveClaudeBudgetEnv({ model: "bili-fallback-model", userAutoCompactWindow: undefined, shellAutoCompactWindow: undefined, routes: {}, upstreamUrl: "https://api.anthropic.com" }), + { CLAUDE_CODE_AUTO_COMPACT_WINDOW: "333333" }, + ); + } finally { + registryResetForTest(); + } +}); + +test("resolveClaudeBudgetEnv: no injection when user self-aligned or unresolvable", async () => { + registrySetForTest({}); + try { + assert.deepEqual(await resolveClaudeBudgetEnv({ model: "claude-sonnet-4-5", userAutoCompactWindow: 300000, shellAutoCompactWindow: undefined, routes: {}, upstreamUrl: "https://api.anthropic.com" }), {}); + assert.deepEqual(await resolveClaudeBudgetEnv({ model: "claude-sonnet-4-5", userAutoCompactWindow: undefined, shellAutoCompactWindow: "250000", routes: {}, upstreamUrl: "https://api.anthropic.com" }), {}); + assert.deepEqual(await resolveClaudeBudgetEnv({ model: undefined, userAutoCompactWindow: undefined, shellAutoCompactWindow: undefined, routes: {}, upstreamUrl: "https://api.anthropic.com" }), {}); + assert.deepEqual(await resolveClaudeBudgetEnv({ model: "bili-nonexistent-model-xyz", userAutoCompactWindow: undefined, shellAutoCompactWindow: undefined, routes: {}, upstreamUrl: "https://api.anthropic.com" }), {}); + } finally { + registryResetForTest(); + } +}); + +test("parseCodexToml: stores model / model_context_window / model_auto_compact_token_limit", () => { + const cfg = parseCodexToml(` +model = "gpt-5.5" +model_context_window = 1000000 +model_auto_compact_token_limit = 900000 + +[model_providers.relay] +base_url = "https://relay.example.com/v1" +`); + assert.equal(cfg.model, "gpt-5.5"); + assert.equal(cfg.contextWindow, 1000000); + assert.equal(cfg.autoCompactLimit, 900000); + assert.deepEqual(cfg.modelWindows, [{ id: "gpt-5.5", contextWindow: 1000000 }]); + const bare = parseCodexToml(`model = "gpt-5.5"\n`); + assert.equal(bare.model, "gpt-5.5"); + assert.equal(bare.contextWindow, undefined); + assert.equal(bare.autoCompactLimit, undefined); + assert.equal(bare.modelWindows, undefined); +}); + +test("readClaudeSettings: model from env block / top-level, autoCompactWindow from both forms", () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-claude-settings-")); + try { + const settingsDir = path.join(home, ".claude"); + fs.mkdirSync(settingsDir, { recursive: true }); + // top-level model + autoCompactWindow + fs.writeFileSync(path.join(settingsDir, "settings.json"), JSON.stringify({ model: "claude-sonnet-4-5", autoCompactWindow: 300000 })); + let cfg = readClaudeSettings(home, os.tmpdir(), {}); + assert.equal(cfg.model, "claude-sonnet-4-5"); + assert.equal(cfg.autoCompactWindow, 300000); + // env block beats same-file top-level + fs.writeFileSync( + path.join(settingsDir, "settings.json"), + JSON.stringify({ model: "claude-sonnet-4-5", autoCompactWindow: 300000, env: { ANTHROPIC_MODEL: "claude-opus-4-5", CLAUDE_CODE_AUTO_COMPACT_WINDOW: "250000" } }), + ); + cfg = readClaudeSettings(home, os.tmpdir(), {}); + assert.equal(cfg.model, "claude-opus-4-5"); + assert.equal(cfg.autoCompactWindow, 250000); + // nothing set → empty object + fs.writeFileSync(path.join(settingsDir, "settings.json"), JSON.stringify({})); + cfg = readClaudeSettings(home, os.tmpdir(), {}); + assert.deepEqual(cfg, {}); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("runLaunch codex: budget args injected for MITM mode (built-in table window)", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-codex-budget-")); + const prevHome = process.env.HOME; + const prevUserProfile = process.env.USERPROFILE; + const prevClientBin = process.env.BILI_CLIENT_BIN; + const prevAnthropicModel = process.env.ANTHROPIC_MODEL; + const prevAutoCompact = process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + process.env.HOME = home; + if (prevUserProfile !== undefined) process.env.USERPROFILE = home; + delete process.env.ANTHROPIC_MODEL; + delete process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + const fakeCodex = path.join(home, "fake-codex"); + fs.writeFileSync(fakeCodex, ""); + process.env.BILI_CLIENT_BIN = fakeCodex; + const codexHome = path.join(home, ".codex"); + fs.mkdirSync(codexHome, { recursive: true }); + fs.writeFileSync(path.join(codexHome, "config.toml"), 'model = "gpt-5.5"\n'); + + const clientArgsSeen: string[][] = []; + const spawnImpl: SpawnFn = (cmd, args) => { + if (cmd === fakeCodex) { + clientArgsSeen.push([...args]); + const child = makeFakeChild(0); + const orig = child.on.bind(child); + (child as { on: SpawnChild["on"] }).on = (event, listener) => { + orig(event, listener); + if (event === "exit") setTimeout(() => listener(0, null), 0); + return child; + }; + return child; + } + return makeFakeChild(42422); + }; + const fetchImpl = async () => ({ ok: true }); + const prevExit = process.exit; + process.exit = (() => undefined) as typeof process.exit; + + try { + await runLaunch( + { client: "codex", clientArgs: [], overrides: {} }, + { fetchImpl, spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(clientArgsSeen.length, 1); + const args = clientArgsSeen[0]; + const i = args.indexOf("model_context_window=400000"); + assert.ok(i > -1, `expected model_context_window=400000 in ${JSON.stringify(args)}`); + assert.equal(args[i - 1], "-c"); + const j = args.indexOf("model_auto_compact_token_limit=400000"); + assert.ok(j > -1, `expected model_auto_compact_token_limit=400000 in ${JSON.stringify(args)}`); + assert.equal(args[j - 1], "-c"); + + // user self-aligned (model_context_window in config.toml) → no injection + fs.writeFileSync(path.join(codexHome, "config.toml"), 'model = "gpt-5.5"\nmodel_context_window = 1000000\n'); + clientArgsSeen.length = 0; + await runLaunch( + { client: "codex", clientArgs: [], overrides: {} }, + { fetchImpl, spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(clientArgsSeen.length, 1); + assert.ok(!clientArgsSeen[0].some((a) => a.startsWith("model_context_window=")), JSON.stringify(clientArgsSeen[0])); + + // no model in config.toml → no injection + fs.writeFileSync(path.join(codexHome, "config.toml"), ""); + clientArgsSeen.length = 0; + await runLaunch( + { client: "codex", clientArgs: [], overrides: {} }, + { fetchImpl, spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(clientArgsSeen.length, 1); + assert.ok(!clientArgsSeen[0].some((a) => a.startsWith("model_context_window=")), JSON.stringify(clientArgsSeen[0])); + } finally { + process.exit = prevExit; + process.env.HOME = prevHome; + if (prevUserProfile === undefined) delete process.env.USERPROFILE; + else process.env.USERPROFILE = prevUserProfile; + if (prevClientBin === undefined) delete process.env.BILI_CLIENT_BIN; + else process.env.BILI_CLIENT_BIN = prevClientBin; + if (prevAnthropicModel === undefined) delete process.env.ANTHROPIC_MODEL; + else process.env.ANTHROPIC_MODEL = prevAnthropicModel; + if (prevAutoCompact === undefined) delete process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + else process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW = prevAutoCompact; + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("runLaunch claude: CLAUDE_CODE_AUTO_COMPACT_WINDOW injected (built-in table window)", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-claude-budget-")); + const prevHome = process.env.HOME; + const prevUserProfile = process.env.USERPROFILE; + const prevClientBin = process.env.BILI_CLIENT_BIN; + const prevAnthropicModel = process.env.ANTHROPIC_MODEL; + const prevAutoCompact = process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + process.env.HOME = home; + if (prevUserProfile !== undefined) process.env.USERPROFILE = home; + delete process.env.ANTHROPIC_MODEL; + delete process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + const fakeClaude = path.join(home, "fake-claude"); + fs.writeFileSync(fakeClaude, ""); + process.env.BILI_CLIENT_BIN = fakeClaude; + const claudeDir = path.join(home, ".claude"); + fs.mkdirSync(claudeDir, { recursive: true }); + fs.writeFileSync(path.join(claudeDir, "settings.json"), JSON.stringify({ model: "claude-sonnet-4-5" })); + + const clientEnvs: (NodeJS.ProcessEnv | undefined)[] = []; + const spawnImpl: SpawnFn = (cmd, args, opts) => { + if (cmd === fakeClaude) { + clientEnvs.push((opts as { env?: NodeJS.ProcessEnv } | undefined)?.env); + const child = makeFakeChild(0); + const orig = child.on.bind(child); + (child as { on: SpawnChild["on"] }).on = (event, listener) => { + orig(event, listener); + if (event === "exit") setTimeout(() => listener(0, null), 0); + return child; + }; + return child; + } + return makeFakeChild(42422); + }; + const fetchImpl = async () => ({ ok: true }); + const prevExit = process.exit; + process.exit = (() => undefined) as typeof process.exit; + + try { + await runLaunch( + { client: "claude", clientArgs: [], overrides: {} }, + { fetchImpl, spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(clientEnvs.length, 1); + assert.equal(clientEnvs[0]?.CLAUDE_CODE_AUTO_COMPACT_WINDOW, "200000"); + + // user self-aligned (settings autoCompactWindow) → no injection + fs.writeFileSync(path.join(claudeDir, "settings.json"), JSON.stringify({ model: "claude-sonnet-4-5", autoCompactWindow: 300000 })); + clientEnvs.length = 0; + await runLaunch( + { client: "claude", clientArgs: [], overrides: {} }, + { fetchImpl, spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(clientEnvs.length, 1); + assert.equal(clientEnvs[0]?.CLAUDE_CODE_AUTO_COMPACT_WINDOW, undefined); + + // shell-exported ANTHROPIC_MODEL wins over settings model + fs.writeFileSync(path.join(claudeDir, "settings.json"), JSON.stringify({ model: "claude-sonnet-4-5" })); + process.env.ANTHROPIC_MODEL = "claude-opus-4-5"; + clientEnvs.length = 0; + await runLaunch( + { client: "claude", clientArgs: [], overrides: {} }, + { fetchImpl, spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(clientEnvs.length, 1); + assert.equal(clientEnvs[0]?.CLAUDE_CODE_AUTO_COMPACT_WINDOW, "200000"); + } finally { + process.exit = prevExit; + process.env.HOME = prevHome; + if (prevUserProfile === undefined) delete process.env.USERPROFILE; + else process.env.USERPROFILE = prevUserProfile; + if (prevClientBin === undefined) delete process.env.BILI_CLIENT_BIN; + else process.env.BILI_CLIENT_BIN = prevClientBin; + if (prevAnthropicModel === undefined) delete process.env.ANTHROPIC_MODEL; + else process.env.ANTHROPIC_MODEL = prevAnthropicModel; + if (prevAutoCompact === undefined) delete process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + else process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW = prevAutoCompact; + fs.rmSync(home, { recursive: true, force: true }); + } +}); From 8d27bcaf7b0057b0e559d2ea4819dd69283f4310 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 29 Aug 2026 01:01:51 +0800 Subject: [PATCH 06/18] fix: gate native-compaction rebase on semantic terminal state (#321 PR-C) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The rebase marker fired on ANY 2xx upstream response, so a failed, incomplete, or truncated compaction stream scheduled a rebase on top of a compaction that never happened (the #249 false-rebase bug): bili discarded valid blocks and mis-tracked the ledger. - new src/stream-terminal.ts: terminal-state observer for Responses responses. SSE: response.completed -> completed, response.failed / response.incomplete -> failed, no terminal event before EOF -> unknown. JSON: output array -> completed, error field -> failed, unparseable / empty / oversized -> unknown. unknown is the safe default (no rebase; next request self-heals via kernel message-id deactivation). - server.ts: both markNativeCompactionBoundary call sites now gate on observed terminal === completed. 2xx empty body no longer rebases. - trigger-path hardening in prepareResponses: a trailing compaction_trigger request is detected BEFORE injection — no compress system prompt, no compress tools, renderTags none, compressInjected false (response flows through plain passthrough instead of the rewriter), and resetAfterSuccess is set so its terminal state gates the rebase. Nudge skip now keys off the same pre-injection detection. - tests: 16 unit tests for the observer + e2e through the proxy covering completed / failed / truncated / 2xx-error-JSON / empty-body / JSON-output and the clean-forward assertions; finality-test control moved to a separate session (a completed trigger now rebases the compacting session, legitimately disarming its nudge). --- src/server.ts | 50 +++--- src/stream-terminal.ts | 109 ++++++++++++ tests/compaction-trigger-finality.test.ts | 16 +- tests/native-compact-terminal.test.ts | 193 ++++++++++++++++++++++ tests/stream-terminal.test.ts | 117 +++++++++++++ 5 files changed, 461 insertions(+), 24 deletions(-) create mode 100644 src/stream-terminal.ts create mode 100644 tests/native-compact-terminal.test.ts create mode 100644 tests/stream-terminal.test.ts diff --git a/src/server.ts b/src/server.ts index 60d2eac1a2..9234c2bc98 100644 --- a/src/server.ts +++ b/src/server.ts @@ -55,6 +55,7 @@ import { runCompressLoop, pickAdapter } from "./loop/index.js"; import { sanitizeResponsesInputIds, dropWhitespaceResponsesMessages, normalizeResponsesMessageItems } from "./loop/adapter-responses.js"; import { rewriteOpenaiJsonResponse } from "./stream-openai.js"; import { rewriteResponsesJsonResponse } from "./stream-responses.js"; +import { observeResponsesTerminalState } from "./stream-terminal.js"; import { emitStreamError } from "./stream-error.js"; import { affinityToken, clientConversationHeader, preferPromptCacheKeyIdentity, type ConversationIdentity } from "./session-id.js"; import { prefixAffinity, type AnonymousAffinity } from "./prefix-affinity.js"; @@ -1401,6 +1402,12 @@ function prepareResponses( const shouldInject = opts.compress.injectTool; const injectTools = shouldInject && !pluginMode; + // Codex native remote-compact request: no compress prompt/tools (the model + // produces the compaction itself), no acp tags, plain passthrough so the + // response terminal state can gate the rebase marker. + const isCompactionTrigger = Array.isArray(parsed.input) + && parsed.input.length > 0 + && parsed.input[parsed.input.length - 1]!.type === "compaction_trigger"; // Route config is keyed by the upstream THIS request goes to (#286: a // session can outlive its first relay — session.meta.upstreamOrigin is // first-wins and would silently ignore the new relay's route settings). @@ -1416,7 +1423,7 @@ function prepareResponses( log("info", `[${sessionId}] input items: ${Array.isArray(parsed.input) ? parsed.input.map((i: ResponseInputItem) => i.type).join(",") : "(string)"}`); } const tokenCount = session.stats.lastInputTokens; - const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" }); + const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: process.env.ACP_RENDER_NONE || isCompactionTrigger ? "none" : "text-only" }); session.state = turn.state; // The fold from last turn's compress has now materialized in state — // future usage reports are post-fold reality, drop the credit. @@ -1436,7 +1443,7 @@ function prepareResponses( processedMessages = stripKernelSummaries(turn.messages, turn.state); reapOrphanBlocks(session, msgs, deactivateBlock); rebuiltInput = patchResponsesInput(projection, processedMessages); - if (shouldInject && !process.env.ACP_NO_COMPRESS_PROMPT) { + if (shouldInject && !isCompactionTrigger && !process.env.ACP_NO_COMPRESS_PROMPT) { const prompt = responsesTextProtocol ? buildCompressHybridSystemPrompt(prompts) : buildCompressSystemPrompt(prompts); const devContent = [...projection.systemParts, prompt].join("\n\n---\n\n"); rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent); @@ -1448,15 +1455,10 @@ function prepareResponses( } else if (projection.systemParts.length > 0) { rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, projection.systemParts.join("\n\n---\n\n")); } - // Codex's native remote-compact request ends with a `compaction_trigger` - // item that the upstream requires to be the FINAL input item (else 400 - // "must be the final input item"). A nudge appended after it would break - // Codex's own compaction, and is redundant — the native compact IS the - // compression. Skip the nudge for such requests. - const triggerFinal = Array.isArray(rebuiltInput) - && rebuiltInput.length > 0 - && rebuiltInput[rebuiltInput.length - 1]!.type === "compaction_trigger"; - if (opts.compress.injectNudge && turn.nudge?.shouldInject && shouldInject && !triggerFinal) { + // A nudge appended after a trailing `compaction_trigger` would break + // the upstream's "must be the final input item" requirement and is + // redundant — the native compact IS the compression. + if (opts.compress.injectNudge && turn.nudge?.shouldInject && shouldInject && !isCompactionTrigger) { try { const rendered = renderNudgeText(turn.nudge, prompts); if (rendered.text) { @@ -1522,11 +1524,12 @@ function prepareResponses( responsesProjection, protocol: "responses", stream, - compressInjected: injectTools, + compressInjected: injectTools && !isCompactionTrigger, pluginMode, responsesTextProtocol, nudge, prompts, + resetAfterSuccess: isCompactionTrigger, }; } @@ -2127,8 +2130,7 @@ async function forward( res.end(); clearUpstreamTimer(); if (prepared?.resetAfterSuccess) { - markNativeCompactionBoundary(prepared.session); - log("info", `[${prepared.session.id}] native Responses compact completed; rebase scheduled for next Responses turn`); + log("warn", `[${prepared.session.id}] native compact response had no body; rebase NOT scheduled`); } return; } @@ -2160,11 +2162,21 @@ async function forward( prepared.compressInjected && prepared.processedMessages.length > 0; if (!useRewriter || prepared === null) { - await pipeThrough(upstream.body, res); - clearUpstreamTimer(); - if (prepared?.resetAfterSuccess) { - markNativeCompactionBoundary(prepared.session); - log("info", `[${prepared.session.id}] native Responses compact completed; rebase scheduled for next Responses turn`); + if (prepared && prepared.resetAfterSuccess) { + const [toClient, toObserve] = upstream.body.tee(); + const observed = observeResponsesTerminalState(toObserve, prepared.stream); + await pipeThrough(toClient, res); + clearUpstreamTimer(); + const terminal = await observed; + if (terminal === "completed") { + markNativeCompactionBoundary(prepared.session); + log("info", `[${prepared.session.id}] native Responses compact completed; rebase scheduled for next Responses turn`); + } else { + log("warn", `[${prepared.session.id}] native compact response terminal=${terminal}; rebase NOT scheduled`); + } + } else { + await pipeThrough(upstream.body, res); + clearUpstreamTimer(); } return; } diff --git a/src/stream-terminal.ts b/src/stream-terminal.ts new file mode 100644 index 0000000000..bbf187af6f --- /dev/null +++ b/src/stream-terminal.ts @@ -0,0 +1,109 @@ +// #321 PR-C: terminal-state observer for native-compaction responses. The +// rebase marker may only fire on "completed": a failed/incomplete/truncated +// stream means the client kept its history, and rebasing on a compaction that +// never happened discards valid blocks (#249). "unknown" is the safe default +// (no rebase; the next request self-heals via kernel message-id deactivation). + +export type TerminalState = "completed" | "failed" | "unknown"; + +const SUCCEEDED = "response.completed"; +const FAILED_EVENTS = new Set(["response.failed", "response.incomplete"]); +const MAX_JSON_BYTES = 16 << 20; + +export async function observeResponsesTerminalState(stream: ReadableStream, isStream: boolean): Promise { + return isStream ? observeSseTerminalState(stream) : observeJsonTerminalState(stream); +} + +function terminalFromSseLine(line: string): TerminalState | null { + if (line.startsWith("event:")) { + const name = line.slice(6).trim(); + if (name === SUCCEEDED) return "completed"; + if (FAILED_EVENTS.has(name)) return "failed"; + return null; + } + if (line.startsWith("data:")) { + const data = line.slice(5).trim(); + if (!data.startsWith("{")) return null; + try { + const type = (JSON.parse(data) as { type?: unknown }).type; + if (type === SUCCEEDED) return "completed"; + if (type === "response.failed" || type === "response.incomplete") return "failed"; + } catch { + return null; + } + } + return null; +} + +async function observeSseTerminalState(stream: ReadableStream): Promise { + const reader = stream.getReader(); + const decoder = new TextDecoder(); + let buf = ""; + let state: TerminalState = "unknown"; + try { + for (;;) { + const { done, value } = await reader.read(); + if (done) break; + buf += decoder.decode(value, { stream: true }); + let nl: number; + while ((nl = buf.indexOf("\n")) >= 0) { + const line = buf.slice(0, nl).replace(/\r$/, ""); + buf = buf.slice(nl + 1); + const next = terminalFromSseLine(line); + if (next !== null) { + state = next; + break; + } + } + if (state !== "unknown") { + // Release this tee branch; the client branch keeps draining the source. + await reader.cancel().catch(() => {}); + break; + } + } + } catch { + return "unknown"; + } finally { + reader.releaseLock(); + } + return state; +} + +async function observeJsonTerminalState(stream: ReadableStream): Promise { + const reader = stream.getReader(); + const chunks: Buffer[] = []; + let size = 0; + let overflow = false; + try { + for (;;) { + const { done, value } = await reader.read(); + if (done) break; + if (!overflow) { + size += value.length; + if (size <= MAX_JSON_BYTES) { + chunks.push(Buffer.from(value)); + } else { + overflow = true; + } + } + } + } catch { + return "unknown"; + } finally { + reader.releaseLock(); + } + if (overflow) return "unknown"; + const text = Buffer.concat(chunks).toString("utf8").trim(); + if (!text) return "unknown"; + let body: unknown; + try { + body = JSON.parse(text); + } catch { + return "unknown"; + } + if (typeof body !== "object" || body === null) return "unknown"; + const rec = body as Record; + if (rec.error !== undefined) return "failed"; + if (Array.isArray(rec.output)) return "completed"; + return "unknown"; +} diff --git a/tests/compaction-trigger-finality.test.ts b/tests/compaction-trigger-finality.test.ts index 7b9c9f0a9f..b81441eafa 100644 --- a/tests/compaction-trigger-finality.test.ts +++ b/tests/compaction-trigger-finality.test.ts @@ -82,10 +82,10 @@ test("e2e #280r2 (Responses): trailing compaction_trigger stays final — nudge const proxyPort = proxy.address().port; const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/responses`; - const post = (input: unknown) => fetch(url, { + const post = (input: unknown, session = "trig-sess") => fetch(url, { method: "POST", headers: { "content-type": "application/json" }, - body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: "trig-sess", instructions: "You are the test coding agent.", input }), + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: session, instructions: "You are the test coding agent.", input }), }); try { @@ -108,9 +108,15 @@ test("e2e #280r2 (Responses): trailing compaction_trigger stays final — nudge assert.equal(lastItem.type, "compaction_trigger", "compaction_trigger is the final input item"); assert.ok(!bodies[bodies.length - 1].includes("Context limit reached"), "no nudge appended after the trigger"); - // Control: same armed session, normal trailing user message — the - // nudge must still be injected. - const r3 = await post([...conversation(), { type: "message", role: "user", content: "What should we do next?" }]); + // Control: normal trailing user message — the nudge must still be + // injected. Runs on a SEPARATE session: since #321 PR-C a completed + // trigger response marks a native-compaction boundary, so the next + // turn on trig-sess rebases (usage reset) and the nudge would be + // legitimately unarmed there. The warmup turn arms the usage report. + const r3a = await post(conversation(), "trig-ctl"); + assert.equal(r3a.status, 200); + await r3a.text(); + const r3 = await post([...conversation(), { type: "message", role: "user", content: "What should we do next?" }], "trig-ctl"); assert.equal(r3.status, 200); await r3.text(); assert.ok(bodies[bodies.length - 1].includes("Context limit reached"), "nudge still injected on a normal request"); diff --git a/tests/native-compact-terminal.test.ts b/tests/native-compact-terminal.test.ts new file mode 100644 index 0000000000..7aa25b5e71 --- /dev/null +++ b/tests/native-compact-terminal.test.ts @@ -0,0 +1,193 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer, type ProxyOptions } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { listSessions } from "../src/session.ts"; + +// #321 PR-C: the native-compaction rebase marker must fire ONLY when the +// upstream response semantically completed. failed/incomplete/truncated +// streams, 2xx error JSON, and empty bodies must NOT schedule a rebase. +// Also: a trailing compaction_trigger request must be forwarded WITHOUT the +// compress prompt/tools and with the trigger still final (trigger hardening). + +function sse(type: string, data: unknown): string { + return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; +} + +function conversation() { + const input: { type: string; role: string; content: string }[] = []; + for (let i = 0; i < 4; i++) { + input.push({ type: "message", role: i % 2 === 0 ? "user" : "assistant", content: `Message ${i}: ` + `WORK_${i}_content_`.repeat(120) }); + } + return input; +} + +function pendingRebase(sessionId: string): boolean | undefined { + const s = listSessions().find((x) => x.meta.label === sessionId); + if (!s) return undefined; + const b = s.metadata.nativeCompactionBoundary as Record | undefined; + return b ? (b.pendingRebase as boolean) : undefined; +} + +test("e2e #321 PR-C: rebase only on completed; trigger request forwarded clean", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const bodies: string[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + bodies.push(raw); + const mode = (req.url?.split("?")[1] ?? "").includes("mode=") ? new URLSearchParams(req.url?.split("?")[1]).get("mode") : undefined; + if (req.url?.includes("/responses/compact")) { + if (mode === "json-error") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: { message: "compact failed" } })); + return; + } + if (mode === "empty") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(); + return; + } + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "resp_c", status: "completed", output: [{ type: "message", role: "user", content: "summary" }], usage: { input_tokens: 10, output_tokens: 1 } })); + return; + } + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + if (mode === "failed") { + res.write(sse("response.created", { type: "response.created", response: { id: "r1" } })); + res.write(sse("response.failed", { type: "response.failed", response: { id: "r1", status: "failed", error: { code: "boom" } } })); + res.end(); + return; + } + if (mode === "truncated") { + res.write(sse("response.created", { type: "response.created", response: { id: "r1" } })); + res.write(sse("response.output_item.added", { type: "response.output_item.added", item: { type: "message" } })); + res.end(); + return; + } + res.write(sse("response.created", { type: "response.created", response: { id: "r1" } })); + res.write(sse("response.completed", { type: "response.completed", response: { id: "r1", status: "completed", output: [], usage: { input_tokens: 100, output_tokens: 5, total_tokens: 105 } } })); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = upstream.address().port; + + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-resp": { context: 10_000 } } } }, + modelContextLimit: 10_000, + kernelConfig: defaultConfig(10_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const proxyPort = proxy.address().port; + const base = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1`; + + const postResponses = (path: string, body: unknown) => fetch(`${base}${path}`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify(body), + }); + + try { + // 1. Trigger form, SSE completed → rebase scheduled. The forwarded + // body must NOT carry the compress prompt/tools, must keep the + // client instructions, and the trigger must stay final. + const r1 = await postResponses("/responses?mode=completed", { + model: "gpt-resp", + stream: true, + session_id: "term-sess", + instructions: "You are the test coding agent.", + tools: [{ type: "custom", name: "shell" }], + input: [...conversation(), { type: "compaction_trigger" }], + }); + assert.equal(r1.status, 200); + await r1.text(); + const fwd = JSON.parse(bodies[bodies.length - 1]) as { input: { type: string; content?: unknown }[]; tools?: { name?: string }[] }; + assert.equal(fwd.input[fwd.input.length - 1].type, "compaction_trigger", "trigger stays final"); + const devContent = String(fwd.input.find((i) => i.type === "message" && typeof i.content === "string" && String(i.content).includes("You are the test coding agent."))?.content ?? ""); + assert.ok(devContent.includes("You are the test coding agent."), "client instructions still forwarded"); + assert.ok(!JSON.stringify(fwd).includes("Compression Philosophy"), "compress prompt NOT injected"); + assert.ok(!(fwd.tools ?? []).some((t) => t.name === "compress"), "compress tool NOT injected"); + assert.equal(pendingRebase("term-sess"), true, "completed trigger response schedules the rebase"); + + // Reconcile on the next turn: state reset, marker consumed. + const r1b = await postResponses("/responses", { + model: "gpt-resp", + stream: true, + session_id: "term-sess", + input: [{ type: "message", role: "user", content: "fresh start" }], + }); + assert.equal(r1b.status, 200); + await r1b.text(); + assert.equal(pendingRebase("term-sess"), false, "rebase reconciled on next turn"); + const s1 = listSessions().find((x) => x.meta.label === "term-sess"); + assert.equal(s1?.blockContents.size, 0, "blocks cleared by the rebase"); + + // 2. Trigger form, SSE failed → NO rebase. + const r2 = await postResponses("/responses?mode=failed", { + model: "gpt-resp", + stream: true, + session_id: "term-failed", + input: [...conversation(), { type: "compaction_trigger" }], + }); + assert.equal(r2.status, 200); + await r2.text(); + assert.equal(pendingRebase("term-failed"), undefined, "failed stream must not schedule a rebase"); + + // 3. Trigger form, SSE truncated (no terminal event) → NO rebase. + const r3 = await postResponses("/responses?mode=truncated", { + model: "gpt-resp", + stream: true, + session_id: "term-trunc", + input: [...conversation(), { type: "compaction_trigger" }], + }); + assert.equal(r3.status, 200); + await r3.text(); + assert.equal(pendingRebase("term-trunc"), undefined, "truncated stream must not schedule a rebase"); + + // 4. Endpoint form, 2xx JSON error body → NO rebase. + const r4 = await postResponses("/responses/compact?mode=json-error", { model: "gpt-resp", session_id: "term-jsonerr" }); + assert.equal(r4.status, 200); + await r4.text(); + assert.equal(pendingRebase("term-jsonerr"), undefined, "2xx error JSON must not schedule a rebase"); + + // 5. Endpoint form, empty body → NO rebase. + const r5 = await postResponses("/responses/compact?mode=empty", { model: "gpt-resp", session_id: "term-empty" }); + assert.equal(r5.status, 200); + await r5.text(); + assert.equal(pendingRebase("term-empty"), undefined, "empty body must not schedule a rebase"); + + // 6. Endpoint form, JSON with output → rebase (the #249 happy path). + const r6 = await postResponses("/responses/compact", { model: "gpt-resp", session_id: "term-jsonok" }); + assert.equal(r6.status, 200); + await r6.text(); + assert.equal(pendingRebase("term-jsonok"), true, "JSON output response schedules the rebase"); + } finally { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + } +}); diff --git a/tests/stream-terminal.test.ts b/tests/stream-terminal.test.ts new file mode 100644 index 0000000000..9b592f298e --- /dev/null +++ b/tests/stream-terminal.test.ts @@ -0,0 +1,117 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { observeResponsesTerminalState } from "../src/stream-terminal.ts"; + +function sseStream(chunks: string[]): ReadableStream { + const enc = new TextEncoder(); + return new ReadableStream({ + start(controller) { + for (const c of chunks) controller.enqueue(enc.encode(c)); + controller.close(); + }, + }); +} + +function sse(type: string, data: unknown): string { + return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; +} + +test("SSE: response.completed terminal event → completed", async () => { + const stream = sseStream([ + sse("response.created", { type: "response.created", response: { id: "r1" } }), + sse("response.output_item.added", { type: "response.output_item.added", item: { type: "compaction" } }), + sse("response.completed", { type: "response.completed", response: { id: "r1", status: "completed" } }), + ]); + assert.equal(await observeResponsesTerminalState(stream, true), "completed"); +}); + +test("SSE: response.failed → failed", async () => { + const stream = sseStream([ + sse("response.created", { type: "response.created", response: { id: "r1" } }), + sse("response.failed", { type: "response.failed", response: { status: "failed", error: { code: "boom" } } }), + ]); + assert.equal(await observeResponsesTerminalState(stream, true), "failed"); +}); + +test("SSE: response.incomplete → failed", async () => { + const stream = sseStream([ + sse("response.incomplete", { type: "response.incomplete", response: { status: "incomplete" } }), + ]); + assert.equal(await observeResponsesTerminalState(stream, true), "failed"); +}); + +test("SSE: stream ends without terminal event (truncation) → unknown", async () => { + const stream = sseStream([ + sse("response.created", { type: "response.created", response: { id: "r1" } }), + sse("response.output_item.added", { type: "response.output_item.added", item: { type: "message" } }), + ]); + assert.equal(await observeResponsesTerminalState(stream, true), "unknown"); +}); + +test("SSE: empty stream → unknown", async () => { + assert.equal(await observeResponsesTerminalState(sseStream([]), true), "unknown"); +}); + +test("SSE: data-line type fallback when event: lines are stripped", async () => { + const stream = sseStream([ + `data: ${JSON.stringify({ type: "response.created", response: { id: "r1" } })}\n\n`, + `data: ${JSON.stringify({ type: "response.completed", response: { id: "r1" } })}\n\n`, + ]); + assert.equal(await observeResponsesTerminalState(stream, true), "completed"); + const failed = sseStream([`data: ${JSON.stringify({ type: "response.failed", response: {} })}\n\n`]); + assert.equal(await observeResponsesTerminalState(failed, true), "failed"); +}); + +test("SSE: CRLF endings and events split across chunks", async () => { + const full = sse("response.created", { type: "response.created" }) + sse("response.completed", { type: "response.completed" }); + const crlf = full.replace(/\n/g, "\r\n"); + const mid = Math.floor(crlf.length / 2); + assert.equal(await observeResponsesTerminalState(sseStream([crlf.slice(0, mid), crlf.slice(mid)]), true), "completed"); +}); + +test("SSE: terminal event wins even if more events follow", async () => { + const stream = sseStream([ + sse("response.completed", { type: "response.completed", response: { id: "r1" } }), + sse("response.output_item.added", { type: "response.output_item.added", item: {} }), + ]); + assert.equal(await observeResponsesTerminalState(stream, true), "completed"); +}); + +test("SSE: non-JSON data lines are ignored, not fatal", async () => { + const stream = sseStream([ + "data: [DONE]\n\n", + sse("response.completed", { type: "response.completed", response: { id: "r1" } }), + ]); + assert.equal(await observeResponsesTerminalState(stream, true), "completed"); +}); + +test("JSON: output array present → completed", async () => { + const stream = sseStream([JSON.stringify({ id: "r1", status: "completed", output: [{ type: "compaction", encrypted_content: "x" }], usage: {} })]); + assert.equal(await observeResponsesTerminalState(stream, false), "completed"); +}); + +test("JSON: error field present → failed", async () => { + const stream = sseStream([JSON.stringify({ error: { message: "compact failed" } })]); + assert.equal(await observeResponsesTerminalState(stream, false), "failed"); +}); + +test("JSON: unparseable body → unknown", async () => { + assert.equal(await observeResponsesTerminalState(sseStream(["502 bad gateway"]), false), "unknown"); +}); + +test("JSON: non-object body → unknown", async () => { + assert.equal(await observeResponsesTerminalState(sseStream(["42"]), false), "unknown"); +}); + +test("JSON: empty body → unknown", async () => { + assert.equal(await observeResponsesTerminalState(sseStream([]), false), "unknown"); +}); + +test("JSON: body without output field → unknown", async () => { + assert.equal(await observeResponsesTerminalState(sseStream([JSON.stringify({ id: "r1", status: "completed" })]), false), "unknown"); +}); + +test("JSON: oversized body → unknown (safe default)", async () => { + const big = JSON.stringify({ output: ["x".repeat((16 << 20) + 1)] }); + assert.equal(await observeResponsesTerminalState(sseStream([big]), false), "unknown"); +}); From c6d1f2c88f7b8da40551d87581d979d6cf4f9417 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 29 Aug 2026 01:21:59 +0800 Subject: [PATCH 07/18] feat: clamp effective window to codex's model perception for codex clients (#321 PR-E1) --- package.json | 1 + scripts/update-codex-models-snapshot.mjs | 88 +++++++++++++ src/codex-models-snapshot.json | 57 ++++++++ src/codex-models.ts | 107 +++++++++++++++ src/server.ts | 16 +++ tests/codex-models.test.ts | 82 ++++++++++++ tests/codex-window-align.test.ts | 160 +++++++++++++++++++++++ 7 files changed, 511 insertions(+) create mode 100644 scripts/update-codex-models-snapshot.mjs create mode 100644 src/codex-models-snapshot.json create mode 100644 src/codex-models.ts create mode 100644 tests/codex-models.test.ts create mode 100644 tests/codex-window-align.test.ts diff --git a/package.json b/package.json index 19f4101e73..bf6fc1e62b 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "scripts": { "build": "tsup", "registry:snapshot": "node scripts/update-registry-snapshot.mjs", + "codex-models:snapshot": "node scripts/update-codex-models-snapshot.mjs", "typecheck": "tsc --noEmit --project tsconfig.build.json", "test": "node --import tsx --test tests/*.test.ts", "start": "node dist/index.js" diff --git a/scripts/update-codex-models-snapshot.mjs b/scripts/update-codex-models-snapshot.mjs new file mode 100644 index 0000000000..32e6ef91cb --- /dev/null +++ b/scripts/update-codex-models-snapshot.mjs @@ -0,0 +1,88 @@ +#!/usr/bin/env node +/** Refresh src/codex-models-snapshot.json from openai/codex (main branch). + +The snapshot is the SLIM form of codex's bundled model table +(codex-rs/models-manager/models.json) — only the fields bili consumes for +budget alignment (#321 PR-E1): slug, contextWindow, maxContextWindow, and the +optional autoCompactTokenLimit / effectiveContextWindowPercent. The full +upstream file is ~424KB of tool/modality metadata bili never reads; the slim +form keeps the bundle small and the contract explicit. + +Run manually or before a release: + npm run codex-models:snapshot + +Node's global fetch ignores http(s)_proxy env vars, so the script tries the +configured shell proxy first (undici ProxyAgent) and falls back to a direct +connection. If BOTH fail the existing snapshot is left untouched (exit 1). +*/ +import { writeFile } from "node:fs/promises"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +const SOURCE_URL = "https://raw.githubusercontent.com/openai/codex/main/codex-rs/models-manager/models.json"; +const OUT_FILE = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "src", "codex-models-snapshot.json"); +const proxyUrl = process.env.https_proxy || process.env.HTTPS_PROXY || process.env.http_proxy || process.env.HTTP_PROXY; + +async function attempt(dispatcher) { + const res = await fetch(SOURCE_URL, { + ...(dispatcher ? { dispatcher } : {}), + signal: AbortSignal.timeout(20_000), + headers: { Accept: "application/json" }, + }); + if (!res.ok) throw new Error(`HTTP ${res.status}`); + return res.json(); +} + +async function fetchFull() { + if (proxyUrl) { + try { + const { ProxyAgent } = await import("undici"); + const full = await attempt(new ProxyAgent({ uri: proxyUrl })); + console.log(`fetched openai/codex via proxy ${proxyUrl}`); + return full; + } catch (e) { + console.log(`proxy attempt failed (${e.message}); trying direct`); + } + } + const full = await attempt(undefined); + console.log("fetched openai/codex direct"); + return full; +} + +let full; +try { + full = await fetchFull(); +} catch (e) { + console.error(`could not fetch ${SOURCE_URL}: ${e.message}`); + console.error(`keeping the existing ${path.basename(OUT_FILE)} untouched`); + process.exit(1); +} + +const models = Array.isArray(full?.models) ? full.models : null; +if (!models || models.length === 0) { + console.error("unexpected upstream shape: expected { models: [...] }"); + console.error(`keeping the existing ${path.basename(OUT_FILE)} untouched`); + process.exit(1); +} + +const slim = models.map((m) => { + const e = { slug: m.slug }; + for (const [k, key] of [ + ["context_window", "contextWindow"], + ["max_context_window", "maxContextWindow"], + ["auto_compact_token_limit", "autoCompactTokenLimit"], + ["effective_context_window_percent", "effectiveContextWindowPercent"], + ]) { + if (m[k] !== null && m[k] !== undefined) e[key] = m[k]; + } + return e; +}); + +const body = JSON.stringify({ + source: `openai/codex main — codex-rs/models-manager/models.json`, + fetchedAt: new Date().toISOString().replace(/\.\d+Z$/, "Z"), + count: slim.length, + models: slim, +}) + "\n"; +await writeFile(OUT_FILE, body, "utf8"); +console.log(`wrote ${OUT_FILE} (${slim.length} models, ${(body.length / 1024).toFixed(1)} KB)`); diff --git a/src/codex-models-snapshot.json b/src/codex-models-snapshot.json new file mode 100644 index 0000000000..1b07d59d27 --- /dev/null +++ b/src/codex-models-snapshot.json @@ -0,0 +1,57 @@ +{ + "source": "openai/codex main (commit 868c9ed) \u2014 codex-rs/models-manager/models.json", + "fetchedAt": "2026-08-28T17:09:46Z", + "count": 10, + "models": [ + { + "slug": "gpt-5.6-sol", + "contextWindow": 272000, + "maxContextWindow": 872000 + }, + { + "slug": "gpt-5.6-terra", + "contextWindow": 272000, + "maxContextWindow": 872000 + }, + { + "slug": "gpt-5.6-luna", + "contextWindow": 272000, + "maxContextWindow": 872000 + }, + { + "slug": "gpt-daybreak-blue-latest", + "contextWindow": 272000, + "maxContextWindow": 872000 + }, + { + "slug": "gpt-daybreak-red-latest", + "contextWindow": 372000, + "maxContextWindow": 372000 + }, + { + "slug": "gpt-5.5", + "contextWindow": 272000, + "maxContextWindow": 272000 + }, + { + "slug": "gpt-5.4", + "contextWindow": 272000, + "maxContextWindow": 1000000 + }, + { + "slug": "gpt-5.4-mini", + "contextWindow": 272000, + "maxContextWindow": 272000 + }, + { + "slug": "gpt-5.2", + "contextWindow": 272000, + "maxContextWindow": 272000 + }, + { + "slug": "codex-auto-review", + "contextWindow": 272000, + "maxContextWindow": 872000 + } + ] +} diff --git a/src/codex-models.ts b/src/codex-models.ts new file mode 100644 index 0000000000..06539a7956 --- /dev/null +++ b/src/codex-models.ts @@ -0,0 +1,107 @@ +import snapshot from "./codex-models-snapshot.json" with { type: "json" }; + +/** One entry of codex's bundled model table (slim form — see + * scripts/update-codex-models-snapshot.mjs). Mirrors the window fields of + * codex-rs `protocol/src/openai_models.rs` ModelInfo that drive its budget. */ +export interface CodexModelEntry { + slug: string; + contextWindow?: number; + maxContextWindow?: number; + autoCompactTokenLimit?: number; + effectiveContextWindowPercent?: number; +} + +interface CodexModelsSnapshot { + source: string; + fetchedAt: string; + count: number; + models: CodexModelEntry[]; +} + +const SNAPSHOT = snapshot as CodexModelsSnapshot; +const TABLE: CodexModelEntry[] = SNAPSHOT.models; + +/** codex's unknown-model fallback window (codex-rs + * models-manager/src/model_info.rs `model_info_from_slug`: + * context_window = max_context_window = 272_000). A model that matches NO + * table slug is NOT "unperceived" by codex — it auto-compacts at 90% of + * this, so the min() alignment must treat it as perceived at 272K. */ +export const CODEX_FALLBACK_CONTEXT_WINDOW = 272_000; + +/** codex's perceived window for a model = resolved_context_window() = + * context_window.or(max_context_window) (openai_models.rs). */ +function resolvedWindow(m: CodexModelEntry): number { + return m.contextWindow ?? m.maxContextWindow ?? CODEX_FALLBACK_CONTEXT_WINDOW; +} + +/** Emulates codex's table lookup (models-manager/src/manager.rs + * `construct_model_info_from_candidates`): longest-prefix match where the + * REQUESTED model starts with the table slug, then a single namespaced-suffix + * retry (`custom/gpt-5.3-codex` → match on `gpt-5.3-codex`) for provider-like + * namespaces, then the 272K fallback. Config overrides are intentionally NOT + * emulated — a user's `model_context_window` override can only clamp the + * perception DOWN (to max_context_window), never raise it, so the table + * value is the safe upper bound for alignment. */ +function lookupWindow(model: string): number { + const direct = longestPrefixMatch(model); + if (direct) return resolvedWindow(direct); + const slash = model.indexOf("/"); + if (slash > 0) { + const namespace = model.slice(0, slash); + const suffix = model.slice(slash + 1); + if (!suffix.includes("/") && /^[A-Za-z0-9_-]+$/.test(namespace)) { + const m = longestPrefixMatch(suffix); + if (m) return resolvedWindow(m); + } + } + return CODEX_FALLBACK_CONTEXT_WINDOW; +} + +function longestPrefixMatch(model: string): CodexModelEntry | undefined { + let best: CodexModelEntry | undefined; + for (const m of TABLE) { + if (!model.startsWith(m.slug)) continue; + if (!best || m.slug.length > best.slug.length) best = m; + } + return best; +} + +/** The context window codex BELIEVES a model has (its own bundled table + + * 272K fallback). codex auto-compacts at 90% of this and hard-stops at 95%, + * so bili must never budget a codex client above it (#321 PR-E1). */ +export function codexWindowForModel(model: string): number { + return lookupWindow(model); +} + +/** True when the request carries codex CLI's User-Agent. codex sets + * `{originator}/{version} (...)` with DEFAULT_ORIGINATOR = `codex_cli_rs` + * (login/src/auth/default_client.rs) on every request. */ +export function isCodexClient(headers: Record): boolean { + const ua = headers["user-agent"]; + const list = Array.isArray(ua) ? ua : ua === undefined ? [] : [ua]; + return list.some((h) => typeof h === "string" && h.startsWith("codex_cli_rs/")); +} + +/** E1 min() alignment: cap bili's effective window at codex's own perception + * when the client is codex. Non-codex clients and limits already at or below + * the perception pass through untouched. */ +export function codexAlignedWindow( + limit: number, + model: string, + headers: Record, +): { limit: number; clamped: boolean } { + if (!isCodexClient(headers)) return { limit, clamped: false }; + const w = codexWindowForModel(model); + if (limit > w) return { limit: w, clamped: true }; + return { limit, clamped: false }; +} + +/** Test hook: replace the bundled table (mirrors registry._setForTest). */ +export function _setCodexTableForTest(models: CodexModelEntry[]): void { + TABLE.length = 0; + TABLE.push(...models); +} + +export function _resetCodexTableForTest(): void { + _setCodexTableForTest([...SNAPSHOT.models]); +} diff --git a/src/server.ts b/src/server.ts index 60d2eac1a2..e20c226aeb 100644 --- a/src/server.ts +++ b/src/server.ts @@ -8,6 +8,7 @@ import { loadOptions, loadRoutes } from "./config.js"; import { resetProxyCache } from "./upstream-proxy.js"; import { FALLBACK_EFFECTIVE_WINDOW_FLOOR, lookupContextLimit, resolveConfiguredContextLimit, resolveCompressProtocol } from "./config.js"; import { contextFromRegistry, loadRegistry, peekRegistryContext } from "./registry.js"; +import { codexAlignedWindow } from "./codex-models.js"; import { fetchWithTimeout, MAX_REQUEST_BYTES } from "./fetch-util.js"; import { formatUpstreamError, getUpstreamConnectionStatus, recordUpstreamConnection, resolveProxy, resolveProxyDecision, proxyDispatcher } from "./upstream-proxy.js"; import { maskHeaderForLog, maskHeadersForLog, maskHostPortForLog, maskUrlForLog, maskUrlsInText } from "./log-mask.js"; @@ -799,6 +800,21 @@ async function handle( if (native) nativeFromFallback = false; } reqConfig = resolveRequestConfig(config, opts.routes, embeddedUrl, model, native, opts.compress); + // #321 PR-E1: a codex client carries its OWN window perception + // (bundled model table + 272K unknown-model fallback) and + // auto-compacts at 90% of it. If bili's budget exceeds what codex + // believes, codex's native compaction fires first — the #292 + // misalignment. Cap the effective window at codex's perception; + // the clamped value is authoritative for this client (codex's own + // config), so it also clears the low-confidence fallback flag + // (no effective-floor after output-headroom reservation). + const aligned = codexAlignedWindow(reqConfig.modelContextLimit, model, req.headers); + if (aligned.clamped) { + const before = reqConfig.modelContextLimit; + reqConfig = { ...reqConfig, modelContextLimit: aligned.limit }; + nativeFromFallback = false; + log("info", `[codex] effective window clamped ${before} → ${aligned.limit} (codex's own perception for model=${model}; ACP now compresses before codex's native auto-compact)`); + } reqPrompts = resolveCompressPrompts(resolveCompress(opts.routes, embeddedUrl, model, opts.compress)); } } diff --git a/tests/codex-models.test.ts b/tests/codex-models.test.ts new file mode 100644 index 0000000000..00a7225651 --- /dev/null +++ b/tests/codex-models.test.ts @@ -0,0 +1,82 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { + CODEX_FALLBACK_CONTEXT_WINDOW, + _resetCodexTableForTest, + _setCodexTableForTest, + codexAlignedWindow, + codexWindowForModel, + isCodexClient, +} from "../src/codex-models.ts"; + +// #321 PR-E1: codex carries its own window perception (bundled model table + +// 272K unknown-model fallback) and auto-compacts at 90% of it. bili must cap +// a codex client's effective window at that perception, or codex's native +// compaction fires first (the #292 misalignment). + +const CODEX_UA = "codex_cli_rs/0.53.0 (linux 6.8.0; x86_64) cli"; + +test("snapshot integrity: every entry has a slug and a resolvable window", () => { + assert.ok(codexWindowForModel("gpt-5.5") > 0); + assert.equal(CODEX_FALLBACK_CONTEXT_WINDOW, 272_000); +}); + +test("in-table exact slug: context_window wins over max_context_window", () => { + assert.equal(codexWindowForModel("gpt-5.5"), 272_000); + assert.equal(codexWindowForModel("gpt-5.4"), 272_000, "gpt-5.4 max is 1M but codex resolves context_window=272K"); + assert.equal(codexWindowForModel("gpt-daybreak-red-latest"), 372_000); +}); + +test("longest-prefix match: model starting with a table slug inherits it", () => { + assert.equal(codexWindowForModel("gpt-5.5-mini"), 272_000, "gpt-5.5-mini → gpt-5.5"); + assert.equal(codexWindowForModel("gpt-5.4-turbo"), 272_000, "gpt-5.4-turbo → gpt-5.4"); + assert.equal(codexWindowForModel("gpt-5.4-mini-x"), 272_000, "longest slug wins (gpt-5.4-mini over gpt-5.4)"); +}); + +test("namespaced suffix retry: provider-like namespace stripped once", () => { + assert.equal(codexWindowForModel("custom/gpt-5.5"), 272_000); + assert.equal(codexWindowForModel("openai/gpt-5.4"), 272_000); +}); + +test("not in table: 272K fallback (codex's model_info_from_slug), NOT unlimited", () => { + assert.equal(codexWindowForModel("qwen3.8-27b"), 272_000); + assert.equal(codexWindowForModel("my-custom-model"), 272_000); + assert.equal(codexWindowForModel("custom/gpt-5.3-codex"), 272_000, "suffix miss → fallback"); + assert.equal(codexWindowForModel("a/b/gpt-5.5"), 272_000, "double slash → no retry → fallback"); +}); + +test("isCodexClient: UA prefix codex_cli_rs/ only", () => { + assert.equal(isCodexClient({ "user-agent": CODEX_UA }), true); + assert.equal(isCodexClient({ "user-agent": "node-fetch/3.1" }), false); + assert.equal(isCodexClient({}), false); + assert.equal(isCodexClient({ "user-agent": ["node-fetch/3.1", CODEX_UA] }), true, "array headers"); + assert.equal(isCodexClient({ "user-agent": "Codex_CLI_RS/0.53.0" }), false, "case-sensitive"); +}); + +test("codexAlignedWindow: min() semantics per acceptance (in-table / not-in-table / user override)", () => { + const codex = { "user-agent": CODEX_UA }; + const other = { "user-agent": "node-fetch/3.1" }; + // in-table: bili 400K (built-in table) → clamped to codex's 272K + assert.deepEqual(codexAlignedWindow(400_000, "gpt-5.5", codex), { limit: 272_000, clamped: true }); + // not-in-table: bili 1M → clamped to the 272K fallback + assert.deepEqual(codexAlignedWindow(1_000_000, "qwen3.8-27b", codex), { limit: 272_000, clamped: true }); + // bili below perception: untouched (min keeps bili's) + assert.deepEqual(codexAlignedWindow(200_000, "gpt-5.5", codex), { limit: 200_000, clamped: false }); + // equal: untouched + assert.deepEqual(codexAlignedWindow(272_000, "gpt-5.5", codex), { limit: 272_000, clamped: false }); + // non-codex client: never touched + assert.deepEqual(codexAlignedWindow(400_000, "gpt-5.5", other), { limit: 400_000, clamped: false }); + // user override below perception (PR-D style -c model_context_window=100000): untouched + assert.deepEqual(codexAlignedWindow(100_000, "gpt-5.5", codex), { limit: 100_000, clamped: false }); +}); + +test("test hooks: table replacement + reset", () => { + _setCodexTableForTest([{ slug: "test-model", contextWindow: 12_345 }]); + assert.equal(codexWindowForModel("test-model"), 12_345); + assert.equal(codexWindowForModel("test-model-x"), 12_345); + _resetCodexTableForTest(); + assert.equal(codexWindowForModel("gpt-5.5"), 272_000); +}); diff --git a/tests/codex-window-align.test.ts b/tests/codex-window-align.test.ts new file mode 100644 index 0000000000..6b2db02845 --- /dev/null +++ b/tests/codex-window-align.test.ts @@ -0,0 +1,160 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer, type ProxyOptions } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { listSessions } from "../src/session.ts"; + +// #321 PR-E1: a codex client (UA `codex_cli_rs/…`) carries its own window +// perception and auto-compacts at 90% of it. The proxy caps the effective +// window at codex's perception (min(bili, codex)), so ACP compresses before +// codex's native compaction can fire (the #292 misalignment). The cap is +// per-request (the UA may appear/disappear between requests of one session) +// and observable via the plugin-reported effectiveContextLimit. + +const CODEX_UA = "codex_cli_rs/0.53.0 (linux 6.8.0; x86_64) cli"; + +function okJson(model: string): string { + return JSON.stringify({ id: "msg_s", type: "message", role: "assistant", model, content: [{ type: "text", text: "s" }], stop_reason: "end_turn", usage: { input_tokens: 500, output_tokens: 5 } }); +} + +interface Rig { + proxyPort: number; + upstreamPort: number; + proxy: http.Server; + upstream: http.Server; +} + +async function startRig(): Promise { + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + let model = "gpt-5.5"; + try { model = (JSON.parse(raw) as { model?: string }).model ?? model; } catch { /* keep default */ } + res.writeHead(200, { "content-type": "application/json" }); + res.end(okJson(model)); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = upstream.address().port; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: {} }, + modelContextLimit: 200_000, + kernelConfig: defaultConfig(200_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const proxyPort = proxy.address().port; + return { proxyPort, upstreamPort, proxy, upstream }; +} + +function url(rig: Rig): string { + return `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/messages`; +} + +async function post(rig: Rig, session: string, model: string, codex: boolean): Promise { + const headers: Record = { + "content-type": "application/json", + "x-acp-session": session, + "x-bili-plugin": "test-agent", + }; + if (codex) headers["user-agent"] = CODEX_UA; + const r = await fetch(url(rig), { method: "POST", headers, body: JSON.stringify({ model, max_tokens: 1024, stream: false, messages: [{ role: "user", content: "hi" }] }) }); + await r.text(); + return r.status; +} + +function effectiveLimit(session: string): number | undefined { + return listSessions().find((s) => s.meta.label === session)?.metadata.effectiveContextLimit; +} + +async function closeRig(rig: Rig): Promise { + rig.proxy.close(); + await once(rig.proxy, "close"); + rig.upstream.close(); + await once(rig.upstream, "close"); +} + +test("e2e: codex UA + in-table model → window clamped to codex's perception", async () => { + const rig = await startRig(); + try { + assert.equal(await post(rig, "cw-in", "gpt-5.5", true), 200); + assert.equal(effectiveLimit("cw-in"), 272_000, "gpt-5.5: bili table 400K clamped to codex 272K"); + } finally { + await closeRig(rig); + } +}); + +test("e2e: non-codex client → no clamp", async () => { + const rig = await startRig(); + try { + assert.equal(await post(rig, "cw-plain", "gpt-5.5", false), 200); + assert.equal(effectiveLimit("cw-plain"), 400_000, "control: built-in table 400K untouched"); + } finally { + await closeRig(rig); + } +}); + +test("e2e: codex UA + prefix-matched model → clamped via longest-prefix", async () => { + const rig = await startRig(); + try { + assert.equal(await post(rig, "cw-prefix", "gpt-5.5-mini", true), 200); + assert.equal(effectiveLimit("cw-prefix"), 272_000, "gpt-5.5-mini → gpt-5.5 (272K)"); + } finally { + await closeRig(rig); + } +}); + +test("e2e: codex UA + not-in-table model → clamped to the 272K fallback", async () => { + const rig = await startRig(); + try { + assert.equal(await post(rig, "cw-fallback", "glm-5", true), 200); + assert.equal(effectiveLimit("cw-fallback"), 272_000, "glm-5: bili table 1M clamped to codex's unknown-model 272K"); + } finally { + await closeRig(rig); + } +}); + +test("e2e: codex UA + bili window below perception → untouched (min keeps bili's)", async () => { + const rig = await startRig(); + try { + assert.equal(await post(rig, "cw-below", "claude-sonnet-4-5", true), 200); + assert.equal(effectiveLimit("cw-below"), 200_000, "200K < 272K perception → no clamp"); + } finally { + await closeRig(rig); + } +}); + +test("e2e: per-request resolution — UA disappears, window reverts", async () => { + const rig = await startRig(); + try { + assert.equal(await post(rig, "cw-perreq", "gpt-5.5", true), 200); + assert.equal(effectiveLimit("cw-perreq"), 272_000, "with codex UA → clamped"); + assert.equal(await post(rig, "cw-perreq", "gpt-5.5", false), 200); + assert.equal(effectiveLimit("cw-perreq"), 400_000, "same session, UA gone → 400K again"); + } finally { + await closeRig(rig); + } +}); From 008bb95b28ab1bdc42f9e5ae64d01149feaaa8ff Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 29 Aug 2026 02:15:34 +0800 Subject: [PATCH 08/18] feat: intercept + forge codex native compaction when ACP is healthy (#321 PR-E2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When BILI_CODEX_COMPACT=intercept, the client is codex (UA codex_cli_rs/), and the ACP state is healthy (transform ok + steady-state < 90% of window + an active block to hand off), bili forges a success response for codex's native compaction requests and never contacts upstream — a deterministic handoff to the ACP state: - trigger form (input ends with compaction_trigger): forged 2-frame SSE (response.output_item.done with one {type:compaction, id:fc_bili_*, encrypted_content:bili:acp:} + response.completed with honest usage). Matches codex's collect_compaction_output contract (exactly one compaction item + response.completed; all other events ignored). - endpoint form (POST /responses/compact): forged JSON {output:[...]} where output is the rebuilt history (recent items + ACP summary), matching codex's CompactHistoryResponse parser. Otherwise (kill-switch off by default, ACP not keeping up >= 90%, or nothing compressed yet) the request passes through to upstream and native compaction backstops (rebased by the PR-C semantic gate). Bili-generated compaction items (fc_bili_ prefix / bili:acp: sentinel) are stripped from subsequent requests so codex's echoed item does not double-count. Pre-flight: typecheck clean; 752/752 tests (7 unit + 5 e2e new); build ok; forge code verified inlined in dist. --- src/codex-compact.ts | 84 ++++++++++++ src/server.ts | 97 +++++++++++++- tests/codex-compact-e2e.test.ts | 230 ++++++++++++++++++++++++++++++++ tests/codex-compact.test.ts | 131 ++++++++++++++++++ 4 files changed, 535 insertions(+), 7 deletions(-) create mode 100644 src/codex-compact.ts create mode 100644 tests/codex-compact-e2e.test.ts create mode 100644 tests/codex-compact.test.ts diff --git a/src/codex-compact.ts b/src/codex-compact.ts new file mode 100644 index 0000000000..c0ad1086c6 --- /dev/null +++ b/src/codex-compact.ts @@ -0,0 +1,84 @@ +import { randomUUID } from "node:crypto"; +import type { Session } from "./session.js"; + +export const CODEX_COMPACT_ID_PREFIX = "fc_bili_"; +export const CODEX_COMPACT_SENTINEL = "bili:acp:"; + +const CODEX_UA_PREFIX = "codex_cli_rs/"; + +export type CodexCompactMode = "intercept" | "pass"; + +// Read per-request (not cached at startup) so a running proxy can flip the +// kill-switch without a restart. +export function codexCompactMode(): CodexCompactMode { + const v = process.env.BILI_CODEX_COMPACT?.trim().toLowerCase(); + return v === "intercept" ? "intercept" : "pass"; +} + +// The `originator` header is only sent for non-default thread originators, so +// the UA prefix (DEFAULT_ORIGINATOR in codex's default_client.rs) is the +// reliable client signal. +export function isCodexClient(headers: Record): boolean { + const ua = headers["user-agent"]; + if (!ua) return false; + const s = Array.isArray(ua) ? ua[0] : ua; + return typeof s === "string" && s.startsWith(CODEX_UA_PREFIX); +} + +export function hasCompactionTrigger(input: unknown): boolean { + if (!Array.isArray(input)) return false; + const last = input[input.length - 1] as { type?: unknown } | undefined; + return last?.type === "compaction_trigger"; +} + +// Recognize a compaction item WE generated (id prefix or sentinel in the blob). +// Codex echoes it back in the next request; stripping it keeps the summary +// sourced from state (no double-count). Real OpenAI blobs carry neither marker, +// so they are left untouched. +export function isBiliCompactionItem(item: unknown): boolean { + const it = item as { type?: unknown; id?: unknown; encrypted_content?: unknown } | null; + if (!it || it.type !== "compaction") return false; + if (typeof it.id === "string" && it.id.startsWith(CODEX_COMPACT_ID_PREFIX)) return true; + if (typeof it.encrypted_content === "string" && it.encrypted_content.startsWith(CODEX_COMPACT_SENTINEL)) return true; + return false; +} + +export function stripBiliCompactionItems(input: T[]): T[] { + return input.filter((item) => !isBiliCompactionItem(item)); +} + +// Safety valve: forge ONLY when the transform succeeded, the steady-state +// context is below the 90% threshold (codex's own auto-compact point — above +// it, bili's ACP has failed to keep up and native compaction must backstop), +// and there is at least one active block (a real summary to hand off). +export function codexCompactGate(session: Session, effectiveLimit: number, transformOk: boolean): boolean { + if (!transformOk || effectiveLimit <= 0) return false; + if (session.stats.lastInputTokens >= effectiveLimit * 0.9) return false; + return session.state.blocks.some((b) => b.active); +} + +// Minimal legal forged SSE stream for the trigger form: exactly one compaction +// output item + response.completed. Codex's parser (codex-api/src/sse/responses.rs) +// ignores every other event kind and requires response.completed to parse with +// id + usage{input_tokens,output_tokens,total_tokens}. +export function buildTriggerForgeSse( + summaryText: string, + usage: { inputTokens: number; outputTokens: number; totalTokens: number }, +): string { + const compactionItem = { + type: "compaction", + id: `${CODEX_COMPACT_ID_PREFIX}${randomUUID()}`, + encrypted_content: `${CODEX_COMPACT_SENTINEL}${summaryText}`, + }; + const completed = { + id: `resp_bili_${randomUUID()}`, + usage: { + input_tokens: usage.inputTokens, + output_tokens: usage.outputTokens, + total_tokens: usage.totalTokens, + }, + }; + const frame1 = `data: ${JSON.stringify({ type: "response.output_item.done", item: compactionItem })}\n\n`; + const frame2 = `data: ${JSON.stringify({ type: "response.completed", response: completed })}\n\n`; + return frame1 + frame2; +} diff --git a/src/server.ts b/src/server.ts index 60d2eac1a2..add923c907 100644 --- a/src/server.ts +++ b/src/server.ts @@ -53,6 +53,7 @@ import { defaultLogFile, stateDir, proxyOriginFile } from "./paths.js"; import { compressLoopResponsesJson } from "./compress-loop-responses.js"; import { runCompressLoop, pickAdapter } from "./loop/index.js"; import { sanitizeResponsesInputIds, dropWhitespaceResponsesMessages, normalizeResponsesMessageItems } from "./loop/adapter-responses.js"; +import { codexCompactMode, isCodexClient, hasCompactionTrigger, stripBiliCompactionItems, codexCompactGate, buildTriggerForgeSse } from "./codex-compact.js"; import { rewriteOpenaiJsonResponse } from "./stream-openai.js"; import { rewriteResponsesJsonResponse } from "./stream-responses.js"; import { emitStreamError } from "./stream-error.js"; @@ -444,11 +445,15 @@ type Prepared = { * rebuilt payload and compress-loop round must re-inject it. */ openaiSystemText?: string; nudge?: NudgeDecision; - /** Effective compression prompts for this request (three-level cascade, - * defaults to the kernel's defaultPrompts). Carried so the compress loop - * in forward() rebuilds the SAME system prompt the request was prepared - * with. */ + /** Effective compression prompts for this request (three-level cascade, + * defaults to the kernel's defaultPrompts). Carried so the compress loop + * in forward() rebuilds the SAME system prompt the request was prepared + * with. */ prompts?: Prompts; + /** Set when a codex native-compaction request was intercepted and a + * success response was forged locally (BILI_CODEX_COMPACT=intercept + + * gate passed). forward() serves `body` without contacting upstream. */ + codexForge?: { kind: "endpoint" | "trigger"; body: string; contentType: string }; }; @@ -1047,7 +1052,7 @@ async function handle( : protocol === "openai" ? prepareOpenai(parsed as OpenAIRequestBody, req, opts, core, reqConfig, reqPrompts, log, session, pluginMode) : responsesCompact - ? prepareResponsesCompact(bodyBuffer, parsed as ResponsesRequestBody, session) + ? prepareResponsesCompact(bodyBuffer, parsed as ResponsesRequestBody, session, req, core, reqConfig, log) : prepareResponses(parsed as ResponsesRequestBody, req, opts, core, reqConfig, reqPrompts, log, session, responsesIdentity!, pluginMode, upstreamOrigin); prepared = runPrepare(); if (!countTokens && !responsesCompact) { @@ -1375,12 +1380,24 @@ function prepareResponses( log("info", `[${sessionId}] reconciled ACP state after native Responses compact boundary`); } + // A codex client echoes our forged compaction item back in the next request; + // drop it so the summary is sourced from state (no double-count). Real + // OpenAI blobs carry no bili marker and pass through untouched. + if (Array.isArray(parsed.input)) { + const cleaned = stripBiliCompactionItems(parsed.input); + if (cleaned.length !== parsed.input.length) { + log("info", `[${sessionId}] stripped ${parsed.input.length - cleaned.length} bili compaction item(s) echoed by codex`); + parsed.input = cleaned; + } + } + let processedMessages: CoreMessage[] = []; let originalMessages: CoreMessage[] = []; let nudge: NudgeDecision | undefined; let responsesProjection: ResponsesProjection | undefined; let rebuiltInput: ResponseInputItem[] | string = parsed.input; let toolsOut = parsed.tools; + let transformOk = false; // #242: over-long input item ids (poisoned rollouts) 400 upstream on every // request; rewrite them to short deterministic ids before anything reads @@ -1469,11 +1486,33 @@ function prepareResponses( } catch { } } + transformOk = true; } catch (err) { log("warn", `[${sessionId}] kernel transform failed, forwarding unchanged: ${String(err)}`); processedMessages = []; } + // E2 trigger form: codex's native remote-compaction request (final input item + // is compaction_trigger). When the kill-switch is on, the client is codex, and + // the safety gate passes, forge a success SSE (one compaction item + + // response.completed) and skip upstream — a deterministic handoff to the ACP + // state instead of a foreign compaction blob. + let codexForge: Prepared["codexForge"] | undefined; + if (transformOk + && codexCompactMode() === "intercept" + && isCodexClient(req.headers) + && hasCompactionTrigger(parsed.input) + && codexCompactGate(session, config.modelContextLimit, transformOk)) { + const summaries = session.state.blocks.filter((b) => b.active).map((b) => b.summary); + const total = session.stats.lastInputTokens; + codexForge = { + kind: "trigger", + body: buildTriggerForgeSse(summaries.join("\n\n"), { inputTokens: total, outputTokens: 0, totalTokens: total }), + contentType: "text/event-stream", + }; + log("info", `[${sessionId}] codex compact intercepted (trigger); forged SSE with ${summaries.length} block summary(s), upstream not contacted`); + } + const rebuilt: ResponsesRequestBody = { ...parsed, input: rebuiltInput, tools: toolsOut }; // Route with the upstream THIS request goes to — session.meta.upstreamOrigin // is first-wins and would keep injecting pck toward a relay we switched @@ -1527,6 +1566,7 @@ function prepareResponses( responsesTextProtocol, nudge, prompts, + codexForge, }; } @@ -1576,9 +1616,17 @@ export function prepareCountTokens( } } -function prepareResponsesCompact(body: Buffer, parsed: ResponsesRequestBody, session: Session): Prepared { +function prepareResponsesCompact( + body: Buffer, + parsed: ResponsesRequestBody, + session: Session, + req: http.IncomingMessage, + core: CompressionCore, + config: Config, + log: (level: string, msg: string) => void, +): Prepared { ++session.stats.requests; - return { + const base: Prepared = { body, session, processedMessages: [], @@ -1588,6 +1636,33 @@ function prepareResponsesCompact(body: Buffer, parsed: ResponsesRequestBody, ses compressInjected: false, resetAfterSuccess: true, }; + if (codexCompactMode() !== "intercept" || !isCodexClient(req.headers) || !Array.isArray(parsed.input)) { + return base; + } + // E2 endpoint form: /responses/compact. When the gate passes, run the same + // fold pipeline as a normal turn and forge the compacted history as + // {"output": [...]} — a deterministic handoff to the ACP state instead of a + // foreign compaction blob. + const cleaned = stripBiliCompactionItems(parsed.input); + const forgeBody: ResponsesRequestBody = { ...parsed, input: cleaned }; + let transformOk = false; + try { + const projection = responsesToCore(forgeBody); + const turn = core.processTurn({ messages: projection.msgs, state: session.state, config, tokenCount: session.stats.lastInputTokens, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" }); + session.state = turn.state; + transformOk = true; + if (!codexCompactGate(session, config.modelContextLimit, transformOk)) return base; + const processed = stripKernelSummaries(turn.messages, turn.state); + const output = patchResponsesInput(projection, processed); + if (typeof output === "string") return base; + snapshotMessages(session, projection.msgs); + markDirty(session); + log("info", `[${session.id}] codex compact intercepted (endpoint); forged history with ${output.length} item(s), upstream not contacted`); + return { ...base, codexForge: { kind: "endpoint", body: JSON.stringify({ output }), contentType: "application/json" } }; + } catch (err) { + log("warn", `[${session.id}] codex compact forge failed (${String(err)}); passing through to upstream`); + return base; + } } export function isChatGptCodexUpstream(upstream: string | undefined): boolean { @@ -1867,6 +1942,14 @@ async function forward( instanceId: string, affinity?: string, ): Promise { + // E2: a codex native-compaction request intercepted in prepare() carries a + // forged success response — serve it without contacting upstream. + if (prepared?.codexForge) { + log("info", `[${prepared.session.id}] codex compact served locally (${prepared.codexForge.kind}); upstream not contacted`); + res.writeHead(200, { "content-type": prepared.codexForge.contentType }); + res.end(prepared.codexForge.body); + return; + } // #300: stamp the chain marker ONLY when this instance actually processed // the request (prepared !== null). A passthrough forward (prepared === null) // must NOT claim processing — otherwise a downstream processing bili would diff --git a/tests/codex-compact-e2e.test.ts b/tests/codex-compact-e2e.test.ts new file mode 100644 index 0000000000..c66ce1d938 --- /dev/null +++ b/tests/codex-compact-e2e.test.ts @@ -0,0 +1,230 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer, type ProxyOptions } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { listSessions, _resetSessionsForTest } from "../src/session.ts"; + +// Issue #321 PR-E2: conditional interception + forgery of codex's native +// compaction requests. When BILI_CODEX_COMPACT=intercept, the client is codex, +// and the ACP state is healthy (transform ok + steady-state < 90% + an active +// block to hand off), bili forges a success response and never contacts +// upstream — a deterministic handoff to the ACP state. Otherwise (kill-switch +// off, ACP not keeping up, or nothing compressed yet) the request passes +// through to upstream and native compaction backstops. + +const CODEX_UA = "codex_cli_rs/0.1.0 (linux x86_64)"; +const SESSION = "trig-sess"; + +function sse(type: string, data: unknown): string { + return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; +} +function completed(inputTokens: number): string { + return sse("response.completed", { + response: { id: "resp_done", status: "completed", output: [], usage: { input_tokens: inputTokens, output_tokens: 5, total_tokens: inputTokens + 5 } }, + }); +} +function fcEvents(callId: string, args: string): string { + return [ + sse("response.output_item.added", { item: { type: "function_call", id: `fc_${callId}`, call_id: callId, name: "compress" }, output_index: 0 }), + sse("response.function_call_arguments.delta", { item_id: `fc_${callId}`, delta: args }), + sse("response.output_item.done", { item: { type: "function_call", id: `fc_${callId}`, call_id: callId, name: "compress", arguments: args }, output_index: 0 }), + ].join(""); +} +// 7 messages × ~4400 chars (~1100 tokens each, ~7700 total). Below the 10k +// window so preflight never fires; above the recent-tail protection so +// m00001/m00002 are compressible by the model. +function conversation() { + const input: { type: string; role: string; content: string }[] = []; + for (let i = 0; i < 7; i++) { + input.push({ type: "message", role: i % 2 === 0 ? "user" : "assistant", content: `Message ${i} of the working session. ` + `WORK_${i}_content_`.repeat(290) }); + } + return input; +} + +type Harness = { + proxy: http.Server; + upstream: http.Server; + bodies: string[]; + url: string; + compactUrl: string; +}; + +async function withHarness(opts: { mode?: string; firstTurnTokens: number }, fn: (h: Harness) => Promise): Promise { + const bodies: string[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + bodies.push(raw); + if ((req.url ?? "").includes("/responses/compact")) { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ output: [] })); + return; + } + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + if (bodies.length === 1) { + // First turn: the model compresses the oldest pair → active block. + const compressArgs = JSON.stringify({ + content: [{ startId: "m00001", endId: "m00002", topic: "setup", summary: "MAIN-SUMMARY-SETUP-CONTEXT-FOLDED-BY-COMPRESSION-LONG-ENOUGH-FOR-KERNEL-MIN-LENGTH-CHECK" }], + }); + res.write(fcEvents("call_p", compressArgs)); + } + res.write(completed(opts.firstTurnTokens)); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = upstream.address().port; + + if (opts.mode === undefined) delete process.env.BILI_CODEX_COMPACT; + else process.env.BILI_CODEX_COMPACT = opts.mode; + + _setStoreForTest(new SessionStore({ enabled: false })); + _resetSessionsForTest(); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-resp": { context: 10_000 } } } }, + modelContextLimit: 10_000, + kernelConfig: defaultConfig(10_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const proxyPort = proxy.address().port; + const base = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1`; + const h: Harness = { proxy, upstream, bodies, url: `${base}/responses`, compactUrl: `${base}/responses/compact` }; + try { + await fn(h); + } finally { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + delete process.env.BILI_CODEX_COMPACT; + } +} + +// Turn 1: a normal turn where the model compresses the oldest pair, leaving an +// active block. Returns the upstream-request count after setup. +async function setupCompressedSession(h: Harness): Promise { + const r1 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: conversation() }), + }); + assert.equal(r1.status, 200); + await r1.text(); + const s = listSessions().find((x) => x.meta.label === SESSION); + assert.ok(s, "session exists"); + assert.ok((s!.state.blocks ?? []).some((b) => b.active), "setup created an active block"); + return h.bodies.length; +} + +test("e2e E2 (trigger form): intercept + healthy ACP → forged 2-frame SSE, upstream untouched", async () => { + await withHarness({ mode: "intercept", firstTurnTokens: 1000 }, async (h) => { + const afterSetup = await setupCompressedSession(h); + const s = listSessions().find((x) => x.meta.label === SESSION)!; + assert.ok(s.stats.lastInputTokens < 9000, "low steady-state usage → gate passes"); + + const r2 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...conversation(), { type: "compaction_trigger" }] }), + }); + assert.equal(r2.status, 200, "intercepted compact returns 200"); + assert.equal(r2.headers.get("content-type"), "text/event-stream", "forged SSE"); + const frames = (await r2.text()).split("\n\n").filter((f) => f.startsWith("data: ")); + assert.equal(frames.length, 2, "exactly two data frames"); + const e1 = JSON.parse(frames[0]!.slice("data: ".length)) as { type: string; item: { type: string; id: string; encrypted_content: string } }; + assert.equal(e1.type, "response.output_item.done"); + assert.equal(e1.item.type, "compaction"); + assert.ok(e1.item.id.startsWith("fc_bili_"), "bili compaction id prefix"); + assert.ok(e1.item.encrypted_content.startsWith("bili:acp:"), "sentinel in blob"); + const e2 = JSON.parse(frames[1]!.slice("data: ".length)) as { type: string; response: { id: string; usage: { total_tokens: number } } }; + assert.equal(e2.type, "response.completed"); + assert.ok(e2.response.id.startsWith("resp_bili_"), "forged response id"); + + assert.equal(h.bodies.length, afterSetup, "upstream NOT contacted for the intercepted compact"); + }); +}); + +test("e2e E2 (trigger form): kill-switch off (default) → forwarded to upstream", async () => { + await withHarness({ mode: undefined, firstTurnTokens: 1000 }, async (h) => { + const afterSetup = await setupCompressedSession(h); + const r2 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...conversation(), { type: "compaction_trigger" }] }), + }); + assert.equal(r2.status, 200); + await r2.text(); + assert.equal(h.bodies.length, afterSetup + 1, "compact request forwarded to upstream"); + }); +}); + +test("e2e E2 (trigger form): intercept + ACP not keeping up (≥90%) → forwarded (native backstop)", async () => { + await withHarness({ mode: "intercept", firstTurnTokens: 15000 }, async (h) => { + const afterSetup = await setupCompressedSession(h); + const s = listSessions().find((x) => x.meta.label === SESSION)!; + assert.ok(s.stats.lastInputTokens >= 9000, "high steady-state usage → gate fails"); + const r2 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...conversation(), { type: "compaction_trigger" }] }), + }); + assert.equal(r2.status, 200); + await r2.text(); + assert.equal(h.bodies.length, afterSetup + 1, "unhealthy ACP → compact forwarded to upstream"); + }); +}); + +test("e2e E2 (trigger form): intercept + nothing compressed yet → forwarded (no summary to hand off)", async () => { + await withHarness({ mode: "intercept", firstTurnTokens: 1000 }, async (h) => { + // Fresh session, no setup compression — the compact request is the first. + const r = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: "fresh-sess", instructions: "You are the test coding agent.", input: [...conversation(), { type: "compaction_trigger" }] }), + }); + assert.equal(r.status, 200); + const text = await r.text(); + assert.ok(!text.includes("fc_bili_"), "no active block → compact NOT forged (forwarded to upstream)"); + assert.ok(!text.includes("resp_bili_"), "no forged response id"); + assert.ok(h.bodies.length >= 1, "compact request reached upstream"); + }); +}); + +test("e2e E2 (endpoint form): intercept + healthy ACP → forged JSON {output}, upstream untouched", async () => { + await withHarness({ mode: "intercept", firstTurnTokens: 1000 }, async (h) => { + const afterSetup = await setupCompressedSession(h); + const r2 = await fetch(h.compactUrl, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: false, session_id: SESSION, instructions: "You are the test coding agent.", input: conversation() }), + }); + assert.equal(r2.status, 200, "intercepted endpoint compact returns 200"); + assert.ok((r2.headers.get("content-type") ?? "").includes("application/json"), "forged JSON"); + const parsed = JSON.parse(await r2.text()) as { output: unknown[] }; + assert.ok(Array.isArray(parsed.output), "output is an array"); + assert.ok(parsed.output.length > 0, "output carries the compacted history"); + assert.equal(h.bodies.length, afterSetup, "upstream NOT contacted for the intercepted endpoint compact"); + }); +}); diff --git a/tests/codex-compact.test.ts b/tests/codex-compact.test.ts new file mode 100644 index 0000000000..64c810c6b1 --- /dev/null +++ b/tests/codex-compact.test.ts @@ -0,0 +1,131 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { createInitialState, type CompressionBlock } from "acp-kernel"; +import type { Session } from "../src/session.ts"; +import { + codexCompactMode, + isCodexClient, + hasCompactionTrigger, + isBiliCompactionItem, + stripBiliCompactionItems, + codexCompactGate, + buildTriggerForgeSse, + CODEX_COMPACT_ID_PREFIX, + CODEX_COMPACT_SENTINEL, +} from "../src/codex-compact.ts"; + +test("codexCompactMode: kill-switch two states + default + case/trim", () => { + const prev = process.env.BILI_CODEX_COMPACT; + try { + delete process.env.BILI_CODEX_COMPACT; + assert.equal(codexCompactMode(), "pass", "default is pass"); + process.env.BILI_CODEX_COMPACT = "pass"; + assert.equal(codexCompactMode(), "pass"); + process.env.BILI_CODEX_COMPACT = "intercept"; + assert.equal(codexCompactMode(), "intercept"); + process.env.BILI_CODEX_COMPACT = "INTERCEPT"; + assert.equal(codexCompactMode(), "intercept", "case-insensitive"); + process.env.BILI_CODEX_COMPACT = " intercept "; + assert.equal(codexCompactMode(), "intercept", "trimmed"); + process.env.BILI_CODEX_COMPACT = "bogus"; + assert.equal(codexCompactMode(), "pass", "unknown value falls back to pass"); + } finally { + if (prev === undefined) delete process.env.BILI_CODEX_COMPACT; + else process.env.BILI_CODEX_COMPACT = prev; + } +}); + +test("isCodexClient: UA prefix detection (Node lowercases header keys)", () => { + assert.equal(isCodexClient({ "user-agent": "codex_cli_rs/0.1.0 (linux x86_64)" }), true); + assert.equal(isCodexClient({ "user-agent": "codex_cli_rs/0.2.1" }), true); + assert.equal(isCodexClient({ "user-agent": "openai-node/3.0" }), false); + assert.equal(isCodexClient({}), false, "no UA"); + assert.equal(isCodexClient({ "user-agent": ["codex_cli_rs/0.1.0", "other"] }), true, "array UA takes first"); + assert.equal(isCodexClient({ "user-agent": ["other", "codex_cli_rs/0.1.0"] }), false, "array UA first is not codex"); +}); + +test("hasCompactionTrigger: only a FINAL compaction_trigger counts", () => { + assert.equal(hasCompactionTrigger([{ type: "message" }, { type: "compaction_trigger" }]), true); + assert.equal(hasCompactionTrigger([{ type: "compaction_trigger" }]), true); + assert.equal(hasCompactionTrigger([{ type: "message" }]), false); + assert.equal(hasCompactionTrigger([{ type: "compaction_trigger" }, { type: "message" }]), false, "trigger must be final"); + assert.equal(hasCompactionTrigger([]), false); + assert.equal(hasCompactionTrigger("a string"), false); +}); + +test("isBiliCompactionItem: our-vs-real-blob distinction", () => { + assert.equal(isBiliCompactionItem({ type: "compaction", id: `${CODEX_COMPACT_ID_PREFIX}abc` }), true, "id prefix"); + assert.equal(isBiliCompactionItem({ type: "compaction", encrypted_content: `${CODEX_COMPACT_SENTINEL}summary` }), true, "sentinel"); + assert.equal(isBiliCompactionItem({ type: "compaction", id: "fc_real", encrypted_content: "opaque-blob" }), false, "real OpenAI blob untouched"); + assert.equal(isBiliCompactionItem({ type: "message", role: "user", content: "hi" }), false, "not a compaction item"); + assert.equal(isBiliCompactionItem(null), false); +}); + +test("stripBiliCompactionItems: removes ours, keeps real blobs + messages", () => { + const ours = { type: "compaction", id: `${CODEX_COMPACT_ID_PREFIX}x`, encrypted_content: `${CODEX_COMPACT_SENTINEL}s` }; + const real = { type: "compaction", id: "fc_real", encrypted_content: "opaque" }; + const msg = { type: "message", role: "user", content: "hi" }; + const out = stripBiliCompactionItems([ours, real, msg]); + assert.equal(out.length, 2); + assert.ok(out.includes(real), "real blob kept"); + assert.ok(out.includes(msg), "message kept"); + assert.ok(!out.includes(ours), "ours removed"); +}); + +function block(active: boolean): CompressionBlock { + return { + blockId: "b1", runId: "r1", tier: 1, summary: "S", + directMessageIds: [], effectiveMessageIds: [], directBlockIds: [], + compressedTokens: 100, createdAt: 0, survivedCount: 0, generation: "young", active, + }; +} + +function mockSession(lastInputTokens: number, blocks: CompressionBlock[]): Session { + const state = createInitialState(); + state.blocks = blocks; + return { + id: "test", + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens, compressCreditTokens: 0, contextTokens: 0 }, + metadata: {}, + state, + createdAt: 0, + lastSeen: 0, + blockContents: new Map(), + inFlight: 0, + persisted: false, + }; +} + +test("codexCompactGate: safety-valve matrix", () => { + const limit = 10_000; + assert.equal(codexCompactGate(mockSession(1_000, [block(true)]), limit, true), true, "healthy + active block + transform ok"); + assert.equal(codexCompactGate(mockSession(1_000, [block(true)]), limit, false), false, "transform failed → pass through"); + assert.equal(codexCompactGate(mockSession(1_000, [block(false)]), limit, true), false, "no active block → pass through"); + assert.equal(codexCompactGate(mockSession(1_000, []), limit, true), false, "no blocks → pass through"); + assert.equal(codexCompactGate(mockSession(9_000, [block(true)]), limit, true), false, "at 90% → ACP not keeping up → pass through"); + assert.equal(codexCompactGate(mockSession(9_500, [block(true)]), limit, true), false, "above 90% → pass through"); + assert.equal(codexCompactGate(mockSession(8_999, [block(true)]), limit, true), true, "just below 90% → forge"); + assert.equal(codexCompactGate(mockSession(1_000, [block(true)]), 0, true), false, "zero limit → pass through"); +}); + +test("buildTriggerForgeSse: minimal legal 2-frame stream", () => { + const sse = buildTriggerForgeSse("SUMMARY-TEXT", { inputTokens: 1000, outputTokens: 0, totalTokens: 1000 }); + const frames = sse.split("\n\n").filter((f) => f.length > 0); + assert.equal(frames.length, 2, "exactly two data frames"); + const e1 = JSON.parse(frames[0]!.slice("data: ".length)) as { type: string; item: { type: string; id: string; encrypted_content: string } }; + assert.equal(e1.type, "response.output_item.done"); + assert.equal(e1.item.type, "compaction"); + assert.ok(e1.item.id.startsWith(CODEX_COMPACT_ID_PREFIX), "compaction id has bili prefix"); + assert.ok(e1.item.encrypted_content.startsWith(CODEX_COMPACT_SENTINEL), "blob carries the sentinel"); + assert.ok(e1.item.encrypted_content.includes("SUMMARY-TEXT"), "summary text in blob"); + const e2 = JSON.parse(frames[1]!.slice("data: ".length)) as { type: string; response: { id: string; usage: { input_tokens: number; output_tokens: number; total_tokens: number } } }; + assert.equal(e2.type, "response.completed"); + assert.ok(e2.response.id.startsWith("resp_bili_"), "completed response id"); + assert.equal(e2.response.usage.input_tokens, 1000); + assert.equal(e2.response.usage.output_tokens, 0); + assert.equal(e2.response.usage.total_tokens, 1000); +}); From 8adeef168a4f9867e16b2f9c1eee273c83eaceae Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 29 Aug 2026 02:22:01 +0800 Subject: [PATCH 09/18] docs: manual claude /bili/ routing + auto-compact alignment (#321 PR-D) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The /bili/ prefix section had no Claude Code entry. Add it (claude's undici fetch ignores HTTPS_PROXY, so the /bili/ URL form via ANTHROPIC_BASE_URL is the only manual option) plus a note that manual mode must set CLAUDE_CODE_AUTO_COMPACT_WINDOW itself — the bili claude launcher does this automatically (PR-D) but manual /bili/ users do not get the injection. claude clamps the value down to its perceived model window, so over-setting is safe. EN + zh-CN. --- CONFIGURATION.md | 14 ++++++++++++++ CONFIGURATION.zh-CN.md | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index b7ec2cb075..5d55020be9 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -449,6 +449,20 @@ Run `codex login` as usual; the OAuth token travels in the `Authorization` heade "baseUrl": "http://localhost:8787/bili/https://api.anthropic.com" ``` +**Claude Code** — set the `ANTHROPIC_BASE_URL` env var to the `/bili/` URL. (claude's undici fetch ignores `HTTPS_PROXY`, so the `/bili/` URL form is the only manual option — cert MITM cannot intercept it.) + +```bash +export ANTHROPIC_BASE_URL="http://localhost:8787/bili/https://api.anthropic.com" +``` + +> **Auto-compact alignment (manual mode only).** The `bili claude` launcher automatically sets `CLAUDE_CODE_AUTO_COMPACT_WINDOW` to bili's effective window for your model, so claude's own auto-compact threshold lines up with bili's compression budget. In manual `/bili/` mode you must do this yourself — otherwise claude may run its own local auto-compact (a "summarize the conversation" turn) on a threshold that doesn't match bili's window. That is usually harmless (same session-id, so bili re-derives state from the truncation) but noisier than needed. Set it to bili's effective window for your model: +> +> ```bash +> export CLAUDE_CODE_AUTO_COMPACT_WINDOW= +> ``` +> +> claude clamps this value **down** to the window it perceives for the model (never up), so over-setting is safe. You can also set it persistently via claude's settings (`autoCompactWindow`). + **Other API-key clients** (Cursor / Aider / Continue …) — wherever the upstream URL is configured, prepend `http://localhost:8787/bili/`. Nothing else changes. The `/bili/` prefix doubles as a **self-detection signal**: billion-context client extensions (billion-context-pi / opencode-acp) recognize it in their own baseUrl and self-disable, so you never get double compression. diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index ff51a49030..d1df7f1fb9 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -449,6 +449,20 @@ openai_base_url = "http://localhost:8787/bili/https://chatgpt.com/backend-api/co "baseUrl": "http://localhost:8787/bili/https://api.anthropic.com" ``` +**Claude Code** —— 把 `ANTHROPIC_BASE_URL` 环境变量设成 `/bili/` URL。(claude 的 undici fetch 忽略 `HTTPS_PROXY`,所以 `/bili/` URL 形式是唯一的手动方式 —— 证书 MITM 拦不到它。) + +```bash +export ANTHROPIC_BASE_URL="http://localhost:8787/bili/https://api.anthropic.com" +``` + +> **自动压缩对齐(仅手动模式)。** `bili claude` launcher 会自动把 `CLAUDE_CODE_AUTO_COMPACT_WINDOW` 设成 bili 对你模型的有效窗口,让 claude 自己的自动压缩阈值与 bili 的压缩预算对齐。手动 `/bili/` 模式下需要你自己设 —— 否则 claude 可能在与 bili 窗口不一致的阈值上跑它自己的本地自动压缩(一次“总结对话”轮次)。这通常无害(同一 session-id,bili 会从截断中重新推导状态),但比必要的更吵。把它设成 bili 对你模型的有效窗口: +> +> ```bash +> export CLAUDE_CODE_AUTO_COMPACT_WINDOW= +> ``` +> +> claude 会把这个值**向下**钳制到它自己感知的模型窗口(不会向上),所以设大了是安全的。也可以持久化到 claude 的 settings(`autoCompactWindow`)里。 + **其他 API-key 客户端(Cursor / Aider / Continue ……)** —— 只要配置了上游 URL,前面加 `http://localhost:8787/bili/` 就行,其他都不用改。 `/bili/` 前缀还是个**自检测信号**:billion-context 的客户端扩展(billion-context-pi / opencode-acp)能在自己的 baseUrl 里认出它并自禁用,避免双层压缩。 From c507e0cb9ef815391601b757662ecc2dcc56858f Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 29 Aug 2026 13:10:27 +0800 Subject: [PATCH 10/18] =?UTF-8?q?fix:=20accept=20codex=5Fexec/=20UA=20pref?= =?UTF-8?q?ix=20=E2=80=94=20codex=200.147=20exec=20sends=20codex=5Fexec/0.?= =?UTF-8?q?147.0,=20so=20the=20intercept=20never=20fired=20for=20real=20ex?= =?UTF-8?q?ec-mode=20clients=20(verified=20by=20real-device=20e2e:=20isCod?= =?UTF-8?q?ex=3Dtrue,=20gate=3Dtrue,=20forged=20SSE=20accepted)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/codex-compact.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/codex-compact.ts b/src/codex-compact.ts index c0ad1086c6..6d56a997e9 100644 --- a/src/codex-compact.ts +++ b/src/codex-compact.ts @@ -4,7 +4,7 @@ import type { Session } from "./session.js"; export const CODEX_COMPACT_ID_PREFIX = "fc_bili_"; export const CODEX_COMPACT_SENTINEL = "bili:acp:"; -const CODEX_UA_PREFIX = "codex_cli_rs/"; +const CODEX_UA_PREFIXES = ["codex_cli_rs/", "codex_exec/"]; export type CodexCompactMode = "intercept" | "pass"; @@ -22,7 +22,7 @@ export function isCodexClient(headers: Record s.startsWith(p)); } export function hasCompactionTrigger(input: unknown): boolean { From c959942a80de503985f636fe4d401bce46d037d0 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 29 Aug 2026 13:18:15 +0800 Subject: [PATCH 11/18] =?UTF-8?q?test:=20pin=20codex=5Fexec/=20UA=20prefix?= =?UTF-8?q?=20=E2=80=94=20regression=20guard=20for=20the=20real-device=20e?= =?UTF-8?q?xec-mode=20UA=20(c507e0c)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/codex-compact.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/codex-compact.test.ts b/tests/codex-compact.test.ts index 64c810c6b1..fb33254512 100644 --- a/tests/codex-compact.test.ts +++ b/tests/codex-compact.test.ts @@ -41,6 +41,7 @@ test("codexCompactMode: kill-switch two states + default + case/trim", () => { test("isCodexClient: UA prefix detection (Node lowercases header keys)", () => { assert.equal(isCodexClient({ "user-agent": "codex_cli_rs/0.1.0 (linux x86_64)" }), true); assert.equal(isCodexClient({ "user-agent": "codex_cli_rs/0.2.1" }), true); + assert.equal(isCodexClient({ "user-agent": "codex_exec/0.147.0 (linux x86_64)" }), true, "exec-mode originator (codex 0.147 real-device UA)"); assert.equal(isCodexClient({ "user-agent": "openai-node/3.0" }), false); assert.equal(isCodexClient({}), false, "no UA"); assert.equal(isCodexClient({ "user-agent": ["codex_cli_rs/0.1.0", "other"] }), true, "array UA takes first"); From 00749640a630979c6094f4cfd12cd4a8f1fb2a0a Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 29 Aug 2026 13:51:43 +0800 Subject: [PATCH 12/18] fix: unify isCodexClient on CODEX_UA_PREFIXES (codex_exec/ was missed by codex-models.ts) --- src/codex-models.ts | 17 ++++------------- tests/codex-models.test.ts | 6 ++++-- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/codex-models.ts b/src/codex-models.ts index 06539a7956..732318a0f6 100644 --- a/src/codex-models.ts +++ b/src/codex-models.ts @@ -1,4 +1,7 @@ import snapshot from "./codex-models-snapshot.json" with { type: "json" }; +import { isCodexClient } from "./codex-compact.js"; + +export { isCodexClient }; /** One entry of codex's bundled model table (slim form — see * scripts/update-codex-models-snapshot.mjs). Mirrors the window fields of @@ -73,22 +76,10 @@ export function codexWindowForModel(model: string): number { return lookupWindow(model); } -/** True when the request carries codex CLI's User-Agent. codex sets - * `{originator}/{version} (...)` with DEFAULT_ORIGINATOR = `codex_cli_rs` - * (login/src/auth/default_client.rs) on every request. */ -export function isCodexClient(headers: Record): boolean { - const ua = headers["user-agent"]; - const list = Array.isArray(ua) ? ua : ua === undefined ? [] : [ua]; - return list.some((h) => typeof h === "string" && h.startsWith("codex_cli_rs/")); -} - -/** E1 min() alignment: cap bili's effective window at codex's own perception - * when the client is codex. Non-codex clients and limits already at or below - * the perception pass through untouched. */ export function codexAlignedWindow( limit: number, model: string, - headers: Record, + headers: Record, ): { limit: number; clamped: boolean } { if (!isCodexClient(headers)) return { limit, clamped: false }; const w = codexWindowForModel(model); diff --git a/tests/codex-models.test.ts b/tests/codex-models.test.ts index 00a7225651..06c36dcdfe 100644 --- a/tests/codex-models.test.ts +++ b/tests/codex-models.test.ts @@ -48,11 +48,13 @@ test("not in table: 272K fallback (codex's model_info_from_slug), NOT unlimited" assert.equal(codexWindowForModel("a/b/gpt-5.5"), 272_000, "double slash → no retry → fallback"); }); -test("isCodexClient: UA prefix codex_cli_rs/ only", () => { +test("isCodexClient: UA prefixes codex_cli_rs/ and codex_exec/", () => { assert.equal(isCodexClient({ "user-agent": CODEX_UA }), true); + assert.equal(isCodexClient({ "user-agent": "codex_exec/0.147.0" }), true, "exec-mode UA"); assert.equal(isCodexClient({ "user-agent": "node-fetch/3.1" }), false); assert.equal(isCodexClient({}), false); - assert.equal(isCodexClient({ "user-agent": ["node-fetch/3.1", CODEX_UA] }), true, "array headers"); + assert.equal(isCodexClient({ "user-agent": [CODEX_UA, "node-fetch/3.1"] }), true, "array headers (first entry)"); + assert.equal(isCodexClient({ "user-agent": ["node-fetch/3.1", CODEX_UA] }), false, "array headers use first entry"); assert.equal(isCodexClient({ "user-agent": "Codex_CLI_RS/0.53.0" }), false, "case-sensitive"); }); From 656f90d61231bc320b260de5d324d75b7351db5d Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 29 Aug 2026 14:27:42 +0800 Subject: [PATCH 13/18] =?UTF-8?q?fix:=20codex=20compact=20=E2=80=94=20hand?= =?UTF-8?q?=20forged=20summaries=20off=20to=20post-forge=20turns?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The trigger-form forge put block summaries into the opaque compaction item, but the kernel deactivates blocks whose messages vanish from the truncated replay (syncBlocks keeps a block only while one of its effectiveMessageIds or its acp_summary_ message is present) — so everything that lived only in compressed blocks disappeared from the model's view on the first post-forge turn. Fix: capture the active blocks' summaries in the kernel's render format at forge time and at echo-strip time (upgrade path for sessions forged by an older build) into session.metadata.codexForgedSummaries (persisted, exact-text dedup), and re-inject them into the developer message on every /responses turn. Tests: unit coverage for renderForgedSummary/mergeForgedSummaries; mock-upstream integration test asserting the post-forge turn strips the echoed fc_bili_ item and re-injects the summary as a developer message; e2e forge phase now also recalls a sentinel that was compressed pre-forge (archive #6, not in codex's retained tail). --- src/codex-compact.ts | 29 ++++++++++++++++++++++ src/server.ts | 26 +++++++++++++++++--- tests/codex-compact-e2e.test.ts | 43 +++++++++++++++++++++++++++++++++ tests/codex-compact.test.ts | 32 ++++++++++++++++++++++++ tests/e2e/e2e-codex.test.ts | 10 ++++++++ 5 files changed, 136 insertions(+), 4 deletions(-) diff --git a/src/codex-compact.ts b/src/codex-compact.ts index 6d56a997e9..e27336bcf7 100644 --- a/src/codex-compact.ts +++ b/src/codex-compact.ts @@ -1,4 +1,5 @@ import { randomUUID } from "node:crypto"; +import type { CompressionBlock } from "acp-kernel"; import type { Session } from "./session.js"; export const CODEX_COMPACT_ID_PREFIX = "fc_bili_"; @@ -82,3 +83,31 @@ export function buildTriggerForgeSse( const frame2 = `data: ${JSON.stringify({ type: "response.completed", response: completed })}\n\n`; return frame1 + frame2; } + +// The kernel renders block summaries as system messages with this header +// (acp-kernel SUMMARY_HEADER). Reuse the exact format so the model reads a +// captured handoff summary the same way it reads a live kernel-rendered one. +const FORGED_SUMMARY_HEADER = "[Compressed conversation section]"; + +export function renderForgedSummary(block: Pick): string { + const body = block.summary.trim(); + const topicLine = block.topic ? `${FORGED_SUMMARY_HEADER} — ${block.topic}` : FORGED_SUMMARY_HEADER; + return body.length === 0 ? topicLine : `${topicLine}\n${body}`; +} + +// The forged compaction item is opaque to the model and codex truncates its +// history to [compaction_item, tail…], so the kernel deactivates the blocks +// covering the truncated prefix on the next replay and their summaries would +// vanish from the model's view. Capture them (kernel render format) at forge +// time; the server re-injects them into the developer message every turn +// (endpoint-form semantics). Append-only + exact-text dedup: a second forge +// accumulates, re-capture of deactivated blocks adds nothing. +export function mergeForgedSummaries(existing: string[] | undefined, blocks: readonly CompressionBlock[]): string[] { + const merged = existing ? [...existing] : []; + for (const block of blocks) { + if (!block.active) continue; + const rendered = renderForgedSummary(block); + if (!merged.includes(rendered)) merged.push(rendered); + } + return merged; +} diff --git a/src/server.ts b/src/server.ts index 8565f49740..77ef43962f 100644 --- a/src/server.ts +++ b/src/server.ts @@ -54,7 +54,7 @@ import { defaultLogFile, stateDir, proxyOriginFile } from "./paths.js"; import { compressLoopResponsesJson } from "./compress-loop-responses.js"; import { runCompressLoop, pickAdapter } from "./loop/index.js"; import { sanitizeResponsesInputIds, dropWhitespaceResponsesMessages, normalizeResponsesMessageItems } from "./loop/adapter-responses.js"; -import { codexCompactMode, isCodexClient, hasCompactionTrigger, stripBiliCompactionItems, codexCompactGate, buildTriggerForgeSse } from "./codex-compact.js"; +import { codexCompactMode, isCodexClient, hasCompactionTrigger, stripBiliCompactionItems, codexCompactGate, buildTriggerForgeSse, mergeForgedSummaries } from "./codex-compact.js"; import { rewriteOpenaiJsonResponse } from "./stream-openai.js"; import { rewriteResponsesJsonResponse } from "./stream-responses.js"; import { observeResponsesTerminalState } from "./stream-terminal.js"; @@ -1428,6 +1428,16 @@ function prepareResponses( if (cleaned.length !== parsed.input.length) { log("info", `[${sessionId}] stripped ${parsed.input.length - cleaned.length} bili compaction item(s) echoed by codex`); parsed.input = cleaned; + // The truncated replay deactivates the blocks covering the dropped + // prefix during the processTurn below — capture their summaries + // while still active (also covers sessions forged by an older + // build that never captured them). + const prevForged = session.metadata.codexForgedSummaries as string[] | undefined; + const captured = mergeForgedSummaries(prevForged, session.state.blocks); + if (captured.length !== (prevForged?.length ?? 0)) { + session.metadata.codexForgedSummaries = captured; + markDirty(session); + } } } @@ -1499,17 +1509,19 @@ function prepareResponses( processedMessages = stripKernelSummaries(turn.messages, turn.state); reapOrphanBlocks(session, msgs, deactivateBlock); rebuiltInput = patchResponsesInput(projection, processedMessages); + const forgedSummaries = (session.metadata.codexForgedSummaries as string[] | undefined) ?? []; if (shouldInject && !isCompactionTrigger && !process.env.ACP_NO_COMPRESS_PROMPT) { const prompt = responsesTextProtocol ? buildCompressHybridSystemPrompt(prompts) : buildCompressSystemPrompt(prompts); - const devContent = [...projection.systemParts, prompt].join("\n\n---\n\n"); + const devContent = [...projection.systemParts, ...forgedSummaries, prompt].join("\n\n---\n\n"); rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent); if (!process.env.ACP_NO_INJECT_TOOL && injectTools) { toolsOut = responsesTextProtocol ? injectResponsesTool(parsed.tools, ACP_READONLY_TOOLS_RESPONSES) : injectResponsesTool(parsed.tools); } - } else if (projection.systemParts.length > 0) { - rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, projection.systemParts.join("\n\n---\n\n")); + } else if (projection.systemParts.length > 0 || forgedSummaries.length > 0) { + const devContent = [...projection.systemParts, ...forgedSummaries].join("\n\n---\n\n"); + rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent); } // A nudge appended after a trailing `compaction_trigger` would break // the upstream's "must be the final input item" requirement and is @@ -1545,6 +1557,12 @@ function prepareResponses( && hasCompactionTrigger(parsed.input) && codexCompactGate(session, config.modelContextLimit, transformOk)) { const summaries = session.state.blocks.filter((b) => b.active).map((b) => b.summary); + const prevForged = session.metadata.codexForgedSummaries as string[] | undefined; + const captured = mergeForgedSummaries(prevForged, session.state.blocks); + if (captured.length !== (prevForged?.length ?? 0)) { + session.metadata.codexForgedSummaries = captured; + markDirty(session); + } const total = session.stats.lastInputTokens; codexForge = { kind: "trigger", diff --git a/tests/codex-compact-e2e.test.ts b/tests/codex-compact-e2e.test.ts index c66ce1d938..e6d297394c 100644 --- a/tests/codex-compact-e2e.test.ts +++ b/tests/codex-compact-e2e.test.ts @@ -212,6 +212,49 @@ test("e2e E2 (trigger form): intercept + nothing compressed yet → forwarded (n }); }); +test("e2e E2 (trigger form): post-forge turn — echo stripped, forged summary re-injected as developer message", async () => { + await withHarness({ mode: "intercept", firstTurnTokens: 1000 }, async (h) => { + const afterSetup = await setupCompressedSession(h); + + // Turn 2: codex native auto-compact trigger → intercepted + forged. + const r2 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...conversation(), { type: "compaction_trigger" }] }), + }); + assert.equal(r2.status, 200); + const frames = (await r2.text()).split("\n\n").filter((f) => f.startsWith("data: ")); + const e1 = JSON.parse(frames[0]!.slice("data: ".length)) as { item: { type: string; id: string; encrypted_content: string } }; + assert.equal(e1.item.type, "compaction", "forge returned the compaction item"); + + // Turn 3: codex replays [forged compaction item, retained tail, new + // turn]. The kernel deactivates the block (its messages are gone from + // the replay), so the summary must survive via the captured + // developer-message re-injection — and the echoed bili item must not + // reach upstream. + const r3 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [e1.item, ...conversation().slice(-2), { type: "message", role: "user", content: "continue the work" }] }), + }); + assert.equal(r3.status, 200); + await r3.text(); + assert.equal(h.bodies.length, afterSetup + 1, "post-forge turn forwarded to upstream exactly once"); + + const s = listSessions().find((x) => x.meta.label === SESSION)!; + const captured = s.metadata.codexForgedSummaries as string[] | undefined; + assert.ok(Array.isArray(captured) && captured.length > 0, "forge captured the active block summaries"); + assert.ok(captured.some((t) => t.includes("MAIN-SUMMARY-SETUP")), "captured summary is the setup block's"); + + const fwd = h.bodies[h.bodies.length - 1]; + assert.ok(!fwd.includes("fc_bili_"), "echoed bili compaction item stripped before forwarding"); + const fwdBody = JSON.parse(fwd) as { input: Array<{ type: string; role?: string; content?: unknown }> }; + const dev = fwdBody.input.find((i) => i.type === "message" && i.role === "developer"); + assert.ok(dev, "developer message present in forwarded input"); + assert.ok(String(dev!.content).includes("MAIN-SUMMARY-SETUP"), "pre-compaction summary re-injected for the model"); + }); +}); + test("e2e E2 (endpoint form): intercept + healthy ACP → forged JSON {output}, upstream untouched", async () => { await withHarness({ mode: "intercept", firstTurnTokens: 1000 }, async (h) => { const afterSetup = await setupCompressedSession(h); diff --git a/tests/codex-compact.test.ts b/tests/codex-compact.test.ts index fb33254512..39a6501c54 100644 --- a/tests/codex-compact.test.ts +++ b/tests/codex-compact.test.ts @@ -13,6 +13,8 @@ import { stripBiliCompactionItems, codexCompactGate, buildTriggerForgeSse, + renderForgedSummary, + mergeForgedSummaries, CODEX_COMPACT_ID_PREFIX, CODEX_COMPACT_SENTINEL, } from "../src/codex-compact.ts"; @@ -130,3 +132,33 @@ test("buildTriggerForgeSse: minimal legal 2-frame stream", () => { assert.equal(e2.response.usage.output_tokens, 0); assert.equal(e2.response.usage.total_tokens, 1000); }); + +function blockWith(summary: string, active: boolean, topic?: string): CompressionBlock { + const b = block(active); + b.summary = summary; + if (topic !== undefined) b.topic = topic; + return b; +} + +test("renderForgedSummary: kernel render format (topic, no topic, empty body)", () => { + assert.equal(renderForgedSummary({ summary: "S", topic: "T" }), "[Compressed conversation section] — T\nS"); + assert.equal(renderForgedSummary({ summary: "S" }), "[Compressed conversation section]\nS"); + assert.equal(renderForgedSummary({ summary: " " }), "[Compressed conversation section]"); +}); + +test("mergeForgedSummaries: append active, skip inactive, dedup, accumulate across forges", () => { + const a = blockWith("SUMMARY-A", true, "Topic A"); + const b = blockWith("SUMMARY-B", true); + const c = blockWith("SUMMARY-C", false); + let merged = mergeForgedSummaries(undefined, [a, b, c]); + assert.equal(merged.length, 2, "inactive block skipped"); + assert.equal(merged[0], "[Compressed conversation section] — Topic A\nSUMMARY-A"); + assert.equal(merged[1], "[Compressed conversation section]\nSUMMARY-B"); + merged = mergeForgedSummaries(merged, [a, b]); + assert.equal(merged.length, 2, "exact-text dedup on re-capture"); + const d = blockWith("SUMMARY-D", true); + merged = mergeForgedSummaries(merged, [d]); + assert.equal(merged.length, 3, "second forge accumulates"); + assert.equal(merged[2], "[Compressed conversation section]\nSUMMARY-D"); + assert.deepEqual(mergeForgedSummaries(undefined, []), []); +}); diff --git a/tests/e2e/e2e-codex.test.ts b/tests/e2e/e2e-codex.test.ts index 19a1a10509..d0f253a892 100644 --- a/tests/e2e/e2e-codex.test.ts +++ b/tests/e2e/e2e-codex.test.ts @@ -280,6 +280,16 @@ test("e2e codex: warmup / load / ACP compress / purity / (forge)", { skip: skipR ["-c", "model_auto_compact_token_limit=3000"]); assert.strictEqual(f2.code, 0, `post-forge exit=${f2.code}`); assert.ok(f2.last.includes("9600"), `post-forge recall wrong: ${f2.last}`); + // Pre-forge compressed recall: 档案#6's sentinel was folded into an ACP + // block in phase 3 (phase 4 proved it recallable from the summary) and is + // NOT in codex's retained tail after the forge — it must survive via the + // forged-summary handoff (developer-message re-injection), not via the + // opaque compaction item. + const f3 = await turnExpect(ctx, "只回答一个数字: 档案#6 的哨兵值是多少?", + (last) => digits(last).includes(SENTINELS[5]), + ["-c", "model_auto_compact_token_limit=999999"]); + assert.strictEqual(f3.code, 0, `pre-forge-compressed recall exit=${f3.code}`); + assert.ok(digits(f3.last).includes(SENTINELS[5]), `pre-forge compressed value lost after forge: ${f3.last}`); const sessionsDir = path.join(ctx.codexHome, "sessions"); let rolloutHasForgeItem = false; if (fs.existsSync(sessionsDir)) { From 7ed8dab1d6834cd37cf05a942934236f9aafad78 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 29 Aug 2026 14:28:19 +0800 Subject: [PATCH 14/18] fix: strip bili compaction item on every /responses/compact forwarding path; tail-window offset-0 guard MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit prepareResponsesCompact mutated state and then returned the original (unstripped) body on gate-fail/catch — the echoed fc_bili_ item reached upstream. Strip before the early return so pass-mode, gate-fail, and catch paths all forward a clean body. Tail-window reattach now excludes offset 0: a head-to-head match is either a full-prefix case (step 1) or a distinct conversation sharing a templated head — never a truncation reattach (the retained suffix of a truncation starts strictly inside the stored chain). Closes the cross-merge of two anonymous conversations with identical templated heads. --- src/prefix-affinity.ts | 8 ++++++-- src/server.ts | 10 +++++++--- tests/tail-window-reconcile.test.ts | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/prefix-affinity.ts b/src/prefix-affinity.ts index e4aad652c7..22a8dbed97 100644 --- a/src/prefix-affinity.ts +++ b/src/prefix-affinity.ts @@ -197,13 +197,17 @@ export class PrefixAffinityResolver { // (plus new appends), so the incoming's LEADING items must appear // as a contiguous run somewhere inside the stored chain's item // hashes — at any offset, not just the stored tail (a rolling-window - // client may retain more items than TAIL_WINDOW). + // client may retain more items than TAIL_WINDOW). Offset 0 is + // excluded: a head-to-head match is either a full-prefix case + // (step 1) or a distinct conversation sharing a templated head — + // never a truncation reattach (the retained suffix of a truncation + // starts strictly inside the stored chain). const w = Math.min(TAIL_WINDOW, incomingDepth); const candidates: ChainEntry[] = []; if (w >= MIN_TAIL_MATCH) { for (const entry of tracked.values()) { const stored = entry.itemHashes; - for (let j = 0; j + w <= stored.length; j++) { + for (let j = 1; j + w <= stored.length; j++) { let match = true; for (let i = 0; i < w; i++) { if (incItemHashes[i] !== stored[j + i]) { diff --git a/src/server.ts b/src/server.ts index 77ef43962f..ff0ab365e0 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1686,8 +1686,14 @@ function prepareResponsesCompact( log: (level: string, msg: string) => void, ): Prepared { ++session.stats.requests; + // A bili-forged compaction item is never for the upstream (it carries our + // sentinel blob) — strip it on every forwarding path, same as the normal + // /responses pipeline does. + const cleaned = Array.isArray(parsed.input) ? stripBiliCompactionItems(parsed.input) : parsed.input; + const stripped = Array.isArray(parsed.input) && cleaned.length !== parsed.input.length; + const forgeBody: ResponsesRequestBody = { ...parsed, input: cleaned }; const base: Prepared = { - body, + body: stripped ? Buffer.from(JSON.stringify(forgeBody)) : body, session, processedMessages: [], originalMessages: [], @@ -1703,8 +1709,6 @@ function prepareResponsesCompact( // fold pipeline as a normal turn and forge the compacted history as // {"output": [...]} — a deterministic handoff to the ACP state instead of a // foreign compaction blob. - const cleaned = stripBiliCompactionItems(parsed.input); - const forgeBody: ResponsesRequestBody = { ...parsed, input: cleaned }; let transformOk = false; try { const projection = responsesToCore(forgeBody); diff --git a/tests/tail-window-reconcile.test.ts b/tests/tail-window-reconcile.test.ts index ae3099fa5c..7d8d32a1e0 100644 --- a/tests/tail-window-reconcile.test.ts +++ b/tests/tail-window-reconcile.test.ts @@ -83,6 +83,22 @@ test("tail-window: brand-new conversation resolves via new (regression)", () => assert.notEqual(b.sessionId, a.sessionId); }); +test("tail-window: distinct conversation sharing a long templated head must NOT merge (offset-0 guard)", () => { + const r = new PrefixAffinityResolver(); + const sharedHead = chain(8, "templated-onboarding"); + const aFull = [...sharedHead, ...chain(4, "conv-a-body")]; + const a = r.resolve(aFull)!; + r.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes); + + // B shares the entire 8-item head and diverges after it. A truncation + // reattach can never start at the stored head (such a client would be + // caught by the full-prefix step), so B must NOT be merged into A. + const bIncoming = [...sharedHead, ...chain(3, "conv-b-body")]; + const b = r.resolve(bIncoming)!; + assert.equal(b.via, "new", "a shared templated head must not reattach the other conversation"); + assert.notEqual(b.sessionId, a.sessionId); +}); + test("fork lineage: divergent continuation records forked lineage (UI/debug only)", () => { const r = new PrefixAffinityResolver(); const shared = chain(4, "shared"); From ccd70368dd878eb9a4a8b682c8859a1223ec0c24 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 29 Aug 2026 14:28:23 +0800 Subject: [PATCH 15/18] docs: document BILI_CODEX_COMPACT in CONFIGURATION (EN + zh-CN) --- CONFIGURATION.md | 1 + CONFIGURATION.zh-CN.md | 1 + 2 files changed, 2 insertions(+) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 5d55020be9..4525a38813 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -359,6 +359,7 @@ Environment variables take precedence over the config file. They are useful for | `BILI_LAUNCHER_PLUGIN` | Set `0` to disable the launcher's bili MCP server injection for claude/codex (pure wire mode). Native MCP tools are injected by default. See [Launcher Reference](#launcher-reference). | | `BILI_LAUNCHER_DIRECT` | Set `1` for direct-URL routing in the launcher (drop MITM/CA trust). See [Launcher Reference](#launcher-reference). | | `BILI_CLAUDE_UPSTREAM` | claude direct mode: your relay endpoint, when `ANTHROPIC_BASE_URL` already points at a relay the launcher would otherwise bypass. | +| `BILI_CODEX_COMPACT` | Codex native-compaction handling: `pass` (default) forwards codex's compaction requests upstream (native compaction backstops); `intercept` forges a local handoff to the ACP state when the safety gate passes (transform ok + steady-state usage < 90% of the window + at least one active compressed block) — trigger form forges a 2-frame SSE, endpoint form forges `{output}` — and never contacts upstream. Forged ACP summaries are re-injected as a developer message on every subsequent turn so compressed content stays visible after codex truncates its history. On any gate failure the request passes through untouched. | --- diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index d1df7f1fb9..35c3e59dac 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -359,6 +359,7 @@ | `BILI_LAUNCHER_PLUGIN` | 设 `0` 关闭 launcher 为 claude/codex 注入 bili MCP 服务器(退回纯 wire 模式)。原生 MCP 工具默认注入。见[启动器参考](#启动器参考)。 | | `BILI_LAUNCHER_DIRECT` | 设 `1` 启用 launcher 直连 URL 路由(放弃 MITM/CA 信任)。见[启动器参考](#启动器参考)。 | | `BILI_CLAUDE_UPSTREAM` | claude 直连模式:当 `ANTHROPIC_BASE_URL` 已指向某个 relay 时,用它指定你的 relay 端点(否则会被旁路)。 | +| `BILI_CODEX_COMPACT` | codex 原生压缩处理:`pass`(默认)把 codex 的压缩请求转发给上游(原生压缩兜底);`intercept` 在安全门通过时(transform 成功 + 稳态用量 < 窗口 90% + 至少一个活跃压缩块)在本地伪造向 ACP 状态的交接 —— trigger 形态伪造 2 帧 SSE,endpoint 形态伪造 `{output}` —— 且不接触上游。伪造的 ACP 摘要会在之后的每个回合以 developer 消息重新注入,保证 codex 截断历史后压缩内容仍可见。任一安全门失败则原样透传。 | --- From 1c6a72c8996aefab6ca34da3633d7c6a78bb28f7 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 29 Aug 2026 15:18:12 +0800 Subject: [PATCH 16/18] fix: reconcile the two parallel review-fix passes (hybrid handoff) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ework's pass (656f90d..ccd7036) and my pass (d4fe1ba) fixed the same findings concurrently. This reconciles them, best mechanism per finding: - MAJOR (summary loss after trigger-form forge): hybrid. The echoed fc_bili_ item is REPLACED by a summary-carrying user message (history-borne: kernel-compressible, kept by codex's user-message retention) — and ework's forge-time capture into session.metadata.codexForgedSummaries stays as the fallback: when the echo does NOT come back, the captured summaries are re-injected into the developer message. Exactly one path is active per turn (no duplication), and neither an echoed-item loss nor unbounded permanent injection can drop or bloat the handoff. - tail-window cross-merge: keep ework's offset-0 guard (a head-to-head match is never a truncation reattach) over the byte-size heuristic. - endpoint compact: strip-on-all-paths (ework) + all-or-nothing state commit with restore on gate-fail/rebuild-failure/error (mine). - codexTurnIdentity: validated 'user' turns fall through to the legacy precedence chain (stronger headers may own the id). - pending plugin register can no longer be claimed by a subagent thread. - codex window clamp exempts an operator's explicit compress.modelContextLimit (operator tuning is never floored/clamped). - trigger-form forge honors parsed.stream (JSON when not streaming) and mints usage from max(lastInputTokens, payload estimate, 1). - isCompactionTrigger reuses hasCompactionTrigger. 823/823 unit tests; full e2e incl. forge + pre-forge deep recall pass against local sglang. --- src/codex-compact.ts | 76 ++++++++++++++++++++++------ src/server.ts | 90 +++++++++++++++++++++------------ src/session-id.ts | 26 +++++++--- tests/codex-compact-e2e.test.ts | 33 +++++++++--- tests/codex-compact.test.ts | 46 +++++++++++++++-- tests/proxy-session-id.test.ts | 6 +-- 6 files changed, 207 insertions(+), 70 deletions(-) diff --git a/src/codex-compact.ts b/src/codex-compact.ts index e27336bcf7..08a66cea92 100644 --- a/src/codex-compact.ts +++ b/src/codex-compact.ts @@ -48,6 +48,46 @@ export function stripBiliCompactionItems(input: T[]): T[] { return input.filter((item) => !isBiliCompactionItem(item)); } +// The summary text a forged blob carries (sentinel-prefixed plaintext). +export function extractBiliSummary(item: unknown): string | undefined { + const it = item as { encrypted_content?: unknown } | null; + if (!it || typeof it.encrypted_content !== "string") return undefined; + if (!it.encrypted_content.startsWith(CODEX_COMPACT_SENTINEL)) return undefined; + const text = it.encrypted_content.slice(CODEX_COMPACT_SENTINEL.length); + return text.length > 0 ? text : undefined; +} + +// An echoed fc_bili_ compaction item is REPLACED (in place) by a plain user +// message carrying the extracted summary — a history-borne handoff the kernel +// can fold again and codex's retention keeps. Rare bounded duplication (a +// block whose anchors survived the truncation also renders) is accepted over +// data loss. Marker items without an extractable blob (id-prefix-only, e.g. +// minted by an older build) are still dropped; real OpenAI blobs pass through +// untouched. +export function replaceBiliCompactionItems(input: T[]): { items: T[]; replaced: number; dropped: number } { + const items: T[] = []; + let replaced = 0; + let dropped = 0; + for (const item of input) { + if (!isBiliCompactionItem(item)) { + items.push(item); + continue; + } + const summary = extractBiliSummary(item); + if (summary === undefined) { + dropped++; + continue; + } + items.push({ + type: "message", + role: "user", + content: [{ type: "input_text", text: `[bili] context summary after compaction:\n${summary}` }], + } as T); + replaced++; + } + return { items, replaced, dropped }; +} + // Safety valve: forge ONLY when the transform succeeded, the steady-state // context is below the 90% threshold (codex's own auto-compact point — above // it, bili's ACP has failed to keep up and native compaction must backstop), @@ -58,30 +98,36 @@ export function codexCompactGate(session: Session, effectiveLimit: number, trans return session.state.blocks.some((b) => b.active); } -// Minimal legal forged SSE stream for the trigger form: exactly one compaction -// output item + response.completed. Codex's parser (codex-api/src/sse/responses.rs) -// ignores every other event kind and requires response.completed to parse with -// id + usage{input_tokens,output_tokens,total_tokens}. -export function buildTriggerForgeSse( +// Minimal legal forged reply for the trigger form. Streaming: exactly one +// compaction output item + response.completed (codex's parser, +// codex-api/src/sse/responses.rs, ignores every other event kind and requires +// response.completed to parse with id + usage). Non-streaming: the JSON +// response shape {id, output, usage}. +export function buildTriggerForgeBody( summaryText: string, usage: { inputTokens: number; outputTokens: number; totalTokens: number }, -): string { + stream: boolean, +): { body: string; contentType: string } { const compactionItem = { type: "compaction", id: `${CODEX_COMPACT_ID_PREFIX}${randomUUID()}`, encrypted_content: `${CODEX_COMPACT_SENTINEL}${summaryText}`, }; - const completed = { - id: `resp_bili_${randomUUID()}`, - usage: { - input_tokens: usage.inputTokens, - output_tokens: usage.outputTokens, - total_tokens: usage.totalTokens, - }, + const usageJson = { + input_tokens: usage.inputTokens, + output_tokens: usage.outputTokens, + total_tokens: usage.totalTokens, }; + const id = `resp_bili_${randomUUID()}`; + if (!stream) { + return { + body: JSON.stringify({ id, output: [compactionItem], usage: usageJson }), + contentType: "application/json", + }; + } const frame1 = `data: ${JSON.stringify({ type: "response.output_item.done", item: compactionItem })}\n\n`; - const frame2 = `data: ${JSON.stringify({ type: "response.completed", response: completed })}\n\n`; - return frame1 + frame2; + const frame2 = `data: ${JSON.stringify({ type: "response.completed", response: { id, usage: usageJson } })}\n\n`; + return { body: frame1 + frame2, contentType: "text/event-stream" }; } // The kernel renders block summaries as system messages with this header diff --git a/src/server.ts b/src/server.ts index ff0ab365e0..c2af8e51b3 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,7 +1,7 @@ import http from "node:http"; import fs from "node:fs"; import { randomUUID } from "node:crypto"; -import { createCore, type CompressionCore, type CompressionState, type Config, type CoreMessage, type NudgeDecision, type Prompts, defaultPrompts, estimateTokensFast, renderNudgeText, deactivateBlock, viableRanges } from "acp-kernel"; +import { createCore, type CompressionCore, type CompressionState, type Config, type CoreMessage, type NudgeDecision, type Prompts, defaultPrompts, defaultCountTokens, estimateTokensFast, renderNudgeText, deactivateBlock, viableRanges } from "acp-kernel"; import { resolveCompress, resolveCompressPrompts, resolveRequestConfig } from "./compress-settings.js"; import type { ProxyOptions } from "./config.js"; import { loadOptions, loadRoutes } from "./config.js"; @@ -54,7 +54,7 @@ import { defaultLogFile, stateDir, proxyOriginFile } from "./paths.js"; import { compressLoopResponsesJson } from "./compress-loop-responses.js"; import { runCompressLoop, pickAdapter } from "./loop/index.js"; import { sanitizeResponsesInputIds, dropWhitespaceResponsesMessages, normalizeResponsesMessageItems } from "./loop/adapter-responses.js"; -import { codexCompactMode, isCodexClient, hasCompactionTrigger, stripBiliCompactionItems, codexCompactGate, buildTriggerForgeSse, mergeForgedSummaries } from "./codex-compact.js"; +import { codexCompactMode, isCodexClient, hasCompactionTrigger, stripBiliCompactionItems, replaceBiliCompactionItems, codexCompactGate, buildTriggerForgeBody, mergeForgedSummaries } from "./codex-compact.js"; import { rewriteOpenaiJsonResponse } from "./stream-openai.js"; import { rewriteResponsesJsonResponse } from "./stream-responses.js"; import { observeResponsesTerminalState } from "./stream-terminal.js"; @@ -810,11 +810,15 @@ async function handle( // (bundled model table + 272K unknown-model fallback) and // auto-compacts at 90% of it. If bili's budget exceeds what codex // believes, codex's native compaction fires first — the #292 - // misalignment. Cap the effective window at codex's perception; - // the clamped value is authoritative for this client (codex's own - // config), so it also clears the low-confidence fallback flag - // (no effective-floor after output-headroom reservation). - const aligned = codexAlignedWindow(reqConfig.modelContextLimit, model, req.headers); + // misalignment. Cap the effective window at codex's perception. + // An operator's explicit compress.modelContextLimit is exempt + // (operator tuning is owned by the operator — never floored and + // never clamped); the clamped value is authoritative for this + // client (codex's own config), so it also clears the + // low-confidence fallback flag. + const aligned = operatorWindowTuned + ? { limit: reqConfig.modelContextLimit, clamped: false } + : codexAlignedWindow(reqConfig.modelContextLimit, model, req.headers); if (aligned.clamped) { const before = reqConfig.modelContextLimit; reqConfig = { ...reqConfig, modelContextLimit: aligned.limit }; @@ -992,7 +996,10 @@ async function handle( pluginConversation = clientConv ?? conversation; } } - if (!pluginAgent && session.stats.requests === 0) { + if (!pluginAgent && session.stats.requests === 0 && codexTurnIdentity(req.headers) === undefined) { + // A codex subagent thread mints a fresh session too, but it must + // not claim the ROOT conversation's pending register (the plugin + // binding belongs to the root session, #317). const pending = takePendingPluginRegister(); if (pending) { pluginAgent = pending.agent; @@ -1420,24 +1427,18 @@ function prepareResponses( log("info", `[${sessionId}] reconciled ACP state after native Responses compact boundary`); } - // A codex client echoes our forged compaction item back in the next request; - // drop it so the summary is sourced from state (no double-count). Real + // A codex client echoes our forged compaction item back in the next + // request; replace it with a plain summary-carrying user message so the + // handoff rides the replayable history (kernel-compressible, retained by + // codex's own user-message rule) instead of a foreign opaque blob. Real // OpenAI blobs carry no bili marker and pass through untouched. + let echoReplaced = false; if (Array.isArray(parsed.input)) { - const cleaned = stripBiliCompactionItems(parsed.input); - if (cleaned.length !== parsed.input.length) { - log("info", `[${sessionId}] stripped ${parsed.input.length - cleaned.length} bili compaction item(s) echoed by codex`); - parsed.input = cleaned; - // The truncated replay deactivates the blocks covering the dropped - // prefix during the processTurn below — capture their summaries - // while still active (also covers sessions forged by an older - // build that never captured them). - const prevForged = session.metadata.codexForgedSummaries as string[] | undefined; - const captured = mergeForgedSummaries(prevForged, session.state.blocks); - if (captured.length !== (prevForged?.length ?? 0)) { - session.metadata.codexForgedSummaries = captured; - markDirty(session); - } + const { items, replaced, dropped } = replaceBiliCompactionItems(parsed.input); + if (replaced > 0 || dropped > 0) { + echoReplaced = true; + log("info", `[${sessionId}] replaced ${replaced} echoed bili compaction item(s) with summary handoff message(s)${dropped > 0 ? `, dropped ${dropped} legacy marker item(s)` : ""}`); + parsed.input = items as typeof parsed.input; } } @@ -1471,9 +1472,7 @@ function prepareResponses( // Codex native remote-compact request: no compress prompt/tools (the model // produces the compaction itself), no acp tags, plain passthrough so the // response terminal state can gate the rebase marker. - const isCompactionTrigger = Array.isArray(parsed.input) - && parsed.input.length > 0 - && parsed.input[parsed.input.length - 1]!.type === "compaction_trigger"; + const isCompactionTrigger = hasCompactionTrigger(parsed.input); // Route config is keyed by the upstream THIS request goes to (#286: a // session can outlive its first relay — session.meta.upstreamOrigin is // first-wins and would silently ignore the new relay's route settings). @@ -1509,7 +1508,15 @@ function prepareResponses( processedMessages = stripKernelSummaries(turn.messages, turn.state); reapOrphanBlocks(session, msgs, deactivateBlock); rebuiltInput = patchResponsesInput(projection, processedMessages); - const forgedSummaries = (session.metadata.codexForgedSummaries as string[] | undefined) ?? []; + // Fallback path: when the echo did NOT come back this turn (client + // dropped it / restarted), the history-borne handoff is absent and the + // forge-time captured summaries are re-injected into the developer + // message so the pre-compaction content is never lost. When the echo + // DID come back, the replacement message carries the summaries and + // the injection is suppressed to avoid duplicating them. + const forgedSummaries = echoReplaced + ? [] + : (session.metadata.codexForgedSummaries as string[] | undefined) ?? []; if (shouldInject && !isCompactionTrigger && !process.env.ACP_NO_COMPRESS_PROMPT) { const prompt = responsesTextProtocol ? buildCompressHybridSystemPrompt(prompts) : buildCompressSystemPrompt(prompts); const devContent = [...projection.systemParts, ...forgedSummaries, prompt].join("\n\n---\n\n"); @@ -1563,11 +1570,16 @@ function prepareResponses( session.metadata.codexForgedSummaries = captured; markDirty(session); } - const total = session.stats.lastInputTokens; + // Codex recomputes its ledger from the next real request, but the + // usage we mint here must not read as an empty context: fall back to + // estimating the trigger payload itself when lastInputTokens is + // stale/zero. The reply honors parsed.stream (JSON body when not + // streaming). + const est = defaultCountTokens(typeof parsed.input === "string" ? parsed.input : JSON.stringify(parsed.input ?? "")); + const total = Math.max(session.stats.lastInputTokens, est, 1); codexForge = { kind: "trigger", - body: buildTriggerForgeSse(summaries.join("\n\n"), { inputTokens: total, outputTokens: 0, totalTokens: total }), - contentType: "text/event-stream", + ...buildTriggerForgeBody(summaries.join("\n\n"), { inputTokens: total, outputTokens: 0, totalTokens: total }, stream), }; log("info", `[${sessionId}] codex compact intercepted (trigger); forged SSE with ${summaries.length} block summary(s), upstream not contacted`); } @@ -1705,6 +1717,11 @@ function prepareResponsesCompact( if (codexCompactMode() !== "intercept" || !isCodexClient(req.headers) || !Array.isArray(parsed.input)) { return base; } + // The state commit below is all-or-nothing: every non-forge path restores + // the pre-turn state so a passthrough compact is not raced against a + // half-applied fold (the upstream's own compaction boundary is handled by + // markNativeCompactionBoundary + rebase instead). + const prevState = session.state; // E2 endpoint form: /responses/compact. When the gate passes, run the same // fold pipeline as a normal turn and forge the compacted history as // {"output": [...]} — a deterministic handoff to the ACP state instead of a @@ -1715,15 +1732,22 @@ function prepareResponsesCompact( const turn = core.processTurn({ messages: projection.msgs, state: session.state, config, tokenCount: session.stats.lastInputTokens, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" }); session.state = turn.state; transformOk = true; - if (!codexCompactGate(session, config.modelContextLimit, transformOk)) return base; + if (!codexCompactGate(session, config.modelContextLimit, transformOk)) { + session.state = prevState; + return base; + } const processed = stripKernelSummaries(turn.messages, turn.state); const output = patchResponsesInput(projection, processed); - if (typeof output === "string") return base; + if (typeof output === "string") { + session.state = prevState; + return base; + } snapshotMessages(session, projection.msgs); markDirty(session); log("info", `[${session.id}] codex compact intercepted (endpoint); forged history with ${output.length} item(s), upstream not contacted`); return { ...base, codexForge: { kind: "endpoint", body: JSON.stringify({ output }), contentType: "application/json" } }; } catch (err) { + session.state = prevState; log("warn", `[${session.id}] codex compact forge failed (${String(err)}); passing through to upstream`); return base; } diff --git a/src/session-id.ts b/src/session-id.ts index 2b1a8ec470..be0bd78f74 100644 --- a/src/session-id.ts +++ b/src/session-id.ts @@ -104,8 +104,13 @@ export function preferPromptCacheKeyIdentity( * * When the metadata is present AND cross-checked (metadata.thread_id === * `thread-id` header), partition by thread_source: - * - "user" → the `session-id` header (current root semantics, stable - * across turns) + * - "user" → undefined (validated, but the id resolves through the + * legacy precedence chain — binding the session-id header + * directly would leapfrog stronger headers such as + * x-bili-plugin-conversation and flip the identity when a + * wrapper adds/drops the metadata mid-session; the legacy + * chain reads the same session-id header for plain codex + * traffic, so the root value is unchanged) * - anything else → the `thread-id` header (fresh independent state per * thread; self-contained replay is lossless). codex's * ThreadSource serializes more than user/subagent — @@ -115,9 +120,9 @@ export function preferPromptCacheKeyIdentity( * threads that need #150 isolation just as much, so the * discrimination is inverted: only "user" joins the root * session, every other source gets its own thread state. - * A non-string/empty thread_source, unparseable JSON, missing/mismatched - * thread_id, or a missing `session-id` header on a "user" turn → undefined: - * the caller falls through to the legacy chain unchanged. + * A non-string/empty thread_source, unparseable JSON, or missing/mismatched + * thread_id → undefined: the caller falls through to the legacy chain + * unchanged. */ export type CodexTurnIdentity = { value: string; @@ -142,9 +147,14 @@ export function codexTurnIdentity(headers: Record { +test("e2e E2 (trigger form): post-forge turn — echo replaced by a history-borne handoff; dev re-injection only when the echo is absent", async () => { await withHarness({ mode: "intercept", firstTurnTokens: 1000 }, async (h) => { const afterSetup = await setupCompressedSession(h); @@ -228,10 +228,9 @@ test("e2e E2 (trigger form): post-forge turn — echo stripped, forged summary r assert.equal(e1.item.type, "compaction", "forge returned the compaction item"); // Turn 3: codex replays [forged compaction item, retained tail, new - // turn]. The kernel deactivates the block (its messages are gone from - // the replay), so the summary must survive via the captured - // developer-message re-injection — and the echoed bili item must not - // reach upstream. + // turn]. The echo is REPLACED by a summary-carrying user message — a + // history-borne handoff the kernel can fold again — and the developer + // re-injection is suppressed for this turn to avoid duplication. const r3 = await fetch(h.url, { method: "POST", headers: { "content-type": "application/json", "user-agent": CODEX_UA }, @@ -247,11 +246,29 @@ test("e2e E2 (trigger form): post-forge turn — echo stripped, forged summary r assert.ok(captured.some((t) => t.includes("MAIN-SUMMARY-SETUP")), "captured summary is the setup block's"); const fwd = h.bodies[h.bodies.length - 1]; - assert.ok(!fwd.includes("fc_bili_"), "echoed bili compaction item stripped before forwarding"); + assert.ok(!fwd.includes("fc_bili_"), "echoed bili compaction item replaced before forwarding"); const fwdBody = JSON.parse(fwd) as { input: Array<{ type: string; role?: string; content?: unknown }> }; + const handoff = fwdBody.input.find((i) => JSON.stringify(i).includes("[bili] context summary after compaction")); + assert.ok(handoff, "summary handoff user message present in forwarded input"); + assert.ok(JSON.stringify(handoff).includes("MAIN-SUMMARY-SETUP"), "pre-compaction summary carried by the handoff"); const dev = fwdBody.input.find((i) => i.type === "message" && i.role === "developer"); - assert.ok(dev, "developer message present in forwarded input"); - assert.ok(String(dev!.content).includes("MAIN-SUMMARY-SETUP"), "pre-compaction summary re-injected for the model"); + assert.ok(!dev || !JSON.stringify(dev).includes("MAIN-SUMMARY-SETUP"), "developer re-injection suppressed while the echo handoff carries the summary"); + + // Turn 4: the echo did NOT come back (codex dropped / restarted) — + // now the captured summaries must surface via the developer-message + // re-injection fallback instead. + const r4 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...conversation().slice(-2), { type: "message", role: "user", content: "still here" }] }), + }); + assert.equal(r4.status, 200); + await r4.text(); + const fwd4 = h.bodies[h.bodies.length - 1]; + const fwd4Body = JSON.parse(fwd4) as { input: Array<{ type: string; role?: string; content?: unknown }> }; + const dev4 = fwd4Body.input.find((i) => i.type === "message" && i.role === "developer"); + assert.ok(dev4, "developer message present when the echo is absent"); + assert.ok(JSON.stringify(dev4).includes("MAIN-SUMMARY-SETUP"), "captured summary re-injected via the fallback path"); }); }); diff --git a/tests/codex-compact.test.ts b/tests/codex-compact.test.ts index 39a6501c54..ed1dfaacc1 100644 --- a/tests/codex-compact.test.ts +++ b/tests/codex-compact.test.ts @@ -11,8 +11,9 @@ import { hasCompactionTrigger, isBiliCompactionItem, stripBiliCompactionItems, + replaceBiliCompactionItems, codexCompactGate, - buildTriggerForgeSse, + buildTriggerForgeBody, renderForgedSummary, mergeForgedSummaries, CODEX_COMPACT_ID_PREFIX, @@ -115,8 +116,34 @@ test("codexCompactGate: safety-valve matrix", () => { assert.equal(codexCompactGate(mockSession(1_000, [block(true)]), 0, true), false, "zero limit → pass through"); }); -test("buildTriggerForgeSse: minimal legal 2-frame stream", () => { - const sse = buildTriggerForgeSse("SUMMARY-TEXT", { inputTokens: 1000, outputTokens: 0, totalTokens: 1000 }); +test("replaceBiliCompactionItems: echo becomes a summary handoff message in place", () => { + const ours = { type: "compaction", id: `${CODEX_COMPACT_ID_PREFIX}x`, encrypted_content: `${CODEX_COMPACT_SENTINEL}summary text` }; + const real = { type: "compaction", id: "fc_real", encrypted_content: "opaque" }; + const msg = { type: "message", role: "user", content: [{ type: "input_text", text: "hi" }] }; + const { items, replaced, dropped } = replaceBiliCompactionItems([msg, ours, real]); + assert.equal(replaced, 1); + assert.equal(dropped, 0); + assert.equal(items.length, 3, "position-preserving replacement, real blob kept"); + const handoff = items[1] as { type: string; role: string; content: { type: string; text: string }[] }; + assert.equal(handoff.type, "message"); + assert.equal(handoff.role, "user"); + assert.equal(handoff.content[0]!.type, "input_text"); + assert.ok(handoff.content[0]!.text.includes("summary text"), "extracted summary rides in the message"); + assert.ok(items.includes(real) && items.includes(msg), "neighbors untouched"); +}); + +test("replaceBiliCompactionItems: legacy id-only marker items still drop; empty blob drops", () => { + const legacy = { type: "compaction", id: `${CODEX_COMPACT_ID_PREFIX}old`, encrypted_content: "not-ours" }; + const empty = { type: "compaction", id: `${CODEX_COMPACT_ID_PREFIX}e`, encrypted_content: CODEX_COMPACT_SENTINEL }; + const { items, replaced, dropped } = replaceBiliCompactionItems([legacy, empty]); + assert.equal(replaced, 0); + assert.equal(dropped, 2); + assert.equal(items.length, 0); +}); + +test("buildTriggerForgeBody: minimal legal 2-frame stream", () => { + const { body: sse, contentType } = buildTriggerForgeBody("SUMMARY-TEXT", { inputTokens: 1000, outputTokens: 0, totalTokens: 1000 }, true); + assert.equal(contentType, "text/event-stream"); const frames = sse.split("\n\n").filter((f) => f.length > 0); assert.equal(frames.length, 2, "exactly two data frames"); const e1 = JSON.parse(frames[0]!.slice("data: ".length)) as { type: string; item: { type: string; id: string; encrypted_content: string } }; @@ -133,6 +160,19 @@ test("buildTriggerForgeSse: minimal legal 2-frame stream", () => { assert.equal(e2.response.usage.total_tokens, 1000); }); +test("buildTriggerForgeBody: non-streaming JSON shape", () => { + const { body, contentType } = buildTriggerForgeBody("S", { inputTokens: 7, outputTokens: 0, totalTokens: 7 }, false); + assert.equal(contentType, "application/json"); + const parsed = JSON.parse(body) as { id: string; output: { type: string; id: string; encrypted_content: string }[]; usage: { input_tokens: number; total_tokens: number } }; + assert.ok(parsed.id.startsWith("resp_bili_")); + assert.equal(parsed.output.length, 1); + assert.equal(parsed.output[0]!.type, "compaction"); + assert.ok(parsed.output[0]!.id.startsWith(CODEX_COMPACT_ID_PREFIX)); + assert.ok(parsed.output[0]!.encrypted_content.startsWith(CODEX_COMPACT_SENTINEL)); + assert.equal(parsed.usage.input_tokens, 7); + assert.equal(parsed.usage.total_tokens, 7); +}); + function blockWith(summary: string, active: boolean, topic?: string): CompressionBlock { const b = block(active); b.summary = summary; diff --git a/tests/proxy-session-id.test.ts b/tests/proxy-session-id.test.ts index 560c003624..928a53cab3 100644 --- a/tests/proxy-session-id.test.ts +++ b/tests/proxy-session-id.test.ts @@ -146,13 +146,13 @@ const rootMeta = (over: Record = {}) => const subMeta = (over: Record = {}) => JSON.stringify({ request_kind: "turn", thread_source: "subagent", thread_id: SUB_THREAD, turn_id: "turn-2", window_id: "win-1", ...over }); -test("codexTurnIdentity: thread_source user → session-id header (current root semantics)", () => { +test("codexTurnIdentity: thread_source user → undefined (legacy precedence chain owns the root id)", () => { const id = codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta(), }); - assert.deepEqual(id, { value: ROOT_SESSION, threadSource: "user" }); + assert.equal(id, undefined, "validated root turn must fall through to the legacy chain (stronger headers may win)"); }); test("codexTurnIdentity: thread_source subagent → thread-id header (fresh per-thread state, #150)", () => { @@ -170,7 +170,7 @@ test("codexTurnIdentity: root identity is stable across turns (turn_id/window_id const a = codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ turn_id: "turn-1", window_id: "w1" }) }); const b = codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ turn_id: "turn-99", window_id: "w42" }) }); assert.deepEqual(a, b); - assert.equal(a!.value, ROOT_SESSION); + assert.equal(a, undefined); }); test("codexTurnIdentity: metadata.thread_id must equal the thread-id header (cross-check, PR #249)", () => { From 413d3a762219775caa967be6124229658dd14fd8 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 29 Aug 2026 15:42:21 +0800 Subject: [PATCH 17/18] feat: default BILI_CODEX_COMPACT=intercept (opt out with pass) --- CONFIGURATION.md | 2 +- CONFIGURATION.zh-CN.md | 2 +- src/codex-compact.ts | 2 +- tests/codex-compact-e2e.test.ts | 4 ++-- tests/codex-compact.test.ts | 10 +++++----- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 4525a38813..b8ff76bc6e 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -359,7 +359,7 @@ Environment variables take precedence over the config file. They are useful for | `BILI_LAUNCHER_PLUGIN` | Set `0` to disable the launcher's bili MCP server injection for claude/codex (pure wire mode). Native MCP tools are injected by default. See [Launcher Reference](#launcher-reference). | | `BILI_LAUNCHER_DIRECT` | Set `1` for direct-URL routing in the launcher (drop MITM/CA trust). See [Launcher Reference](#launcher-reference). | | `BILI_CLAUDE_UPSTREAM` | claude direct mode: your relay endpoint, when `ANTHROPIC_BASE_URL` already points at a relay the launcher would otherwise bypass. | -| `BILI_CODEX_COMPACT` | Codex native-compaction handling: `pass` (default) forwards codex's compaction requests upstream (native compaction backstops); `intercept` forges a local handoff to the ACP state when the safety gate passes (transform ok + steady-state usage < 90% of the window + at least one active compressed block) — trigger form forges a 2-frame SSE, endpoint form forges `{output}` — and never contacts upstream. Forged ACP summaries are re-injected as a developer message on every subsequent turn so compressed content stays visible after codex truncates its history. On any gate failure the request passes through untouched. | +| `BILI_CODEX_COMPACT` | Codex native-compaction handling. Default `intercept`: bili intercepts codex's compaction requests and forges a local handoff to the ACP state when the safety gate passes (transform ok + steady-state usage < 90% of the window + at least one active compressed block) — trigger form forges a 2-frame SSE, endpoint form forges `{output}` — and never contacts upstream. Forged ACP summaries are re-injected as a history-borne handoff message (developer-message fallback) so compressed content stays visible after codex truncates its history. Set `pass` to opt out and forward codex's compaction requests upstream (native compaction backstops). On any gate failure the request passes through untouched. | --- diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 35c3e59dac..7b994f0a7c 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -359,7 +359,7 @@ | `BILI_LAUNCHER_PLUGIN` | 设 `0` 关闭 launcher 为 claude/codex 注入 bili MCP 服务器(退回纯 wire 模式)。原生 MCP 工具默认注入。见[启动器参考](#启动器参考)。 | | `BILI_LAUNCHER_DIRECT` | 设 `1` 启用 launcher 直连 URL 路由(放弃 MITM/CA 信任)。见[启动器参考](#启动器参考)。 | | `BILI_CLAUDE_UPSTREAM` | claude 直连模式:当 `ANTHROPIC_BASE_URL` 已指向某个 relay 时,用它指定你的 relay 端点(否则会被旁路)。 | -| `BILI_CODEX_COMPACT` | codex 原生压缩处理:`pass`(默认)把 codex 的压缩请求转发给上游(原生压缩兜底);`intercept` 在安全门通过时(transform 成功 + 稳态用量 < 窗口 90% + 至少一个活跃压缩块)在本地伪造向 ACP 状态的交接 —— trigger 形态伪造 2 帧 SSE,endpoint 形态伪造 `{output}` —— 且不接触上游。伪造的 ACP 摘要会在之后的每个回合以 developer 消息重新注入,保证 codex 截断历史后压缩内容仍可见。任一安全门失败则原样透传。 | +| `BILI_CODEX_COMPACT` | codex 原生压缩处理。默认 `intercept`:安全门通过时(transform 成功 + 稳态用量 < 窗口 90% + 至少一个活跃压缩块)拦截 codex 的压缩请求,在本地伪造向 ACP 状态的交接——trigger 形态伪造 2 帧 SSE,endpoint 形态伪造 `{output}`——且不接触上游。伪造的 ACP 摘要经历史承载交接消息注入(缺席时 developer 消息兜底),保证 codex 截断历史后压缩内容仍可见。设为 `pass` 可退出,把 codex 的压缩请求转发给上游(原生压缩兜底)。任一安全门失败则原样透传。 | --- diff --git a/src/codex-compact.ts b/src/codex-compact.ts index 08a66cea92..c009d7281d 100644 --- a/src/codex-compact.ts +++ b/src/codex-compact.ts @@ -13,7 +13,7 @@ export type CodexCompactMode = "intercept" | "pass"; // kill-switch without a restart. export function codexCompactMode(): CodexCompactMode { const v = process.env.BILI_CODEX_COMPACT?.trim().toLowerCase(); - return v === "intercept" ? "intercept" : "pass"; + return v === "pass" ? "pass" : "intercept"; } // The `originator` header is only sent for non-default thread originators, so diff --git a/tests/codex-compact-e2e.test.ts b/tests/codex-compact-e2e.test.ts index 45282dc703..d97aec0b9f 100644 --- a/tests/codex-compact-e2e.test.ts +++ b/tests/codex-compact-e2e.test.ts @@ -166,8 +166,8 @@ test("e2e E2 (trigger form): intercept + healthy ACP → forged 2-frame SSE, ups }); }); -test("e2e E2 (trigger form): kill-switch off (default) → forwarded to upstream", async () => { - await withHarness({ mode: undefined, firstTurnTokens: 1000 }, async (h) => { +test("e2e E2 (trigger form): kill-switch off (BILI_CODEX_COMPACT=pass) → forwarded to upstream", async () => { + await withHarness({ mode: "pass", firstTurnTokens: 1000 }, async (h) => { const afterSetup = await setupCompressedSession(h); const r2 = await fetch(h.url, { method: "POST", diff --git a/tests/codex-compact.test.ts b/tests/codex-compact.test.ts index ed1dfaacc1..c2724e81b0 100644 --- a/tests/codex-compact.test.ts +++ b/tests/codex-compact.test.ts @@ -24,17 +24,17 @@ test("codexCompactMode: kill-switch two states + default + case/trim", () => { const prev = process.env.BILI_CODEX_COMPACT; try { delete process.env.BILI_CODEX_COMPACT; - assert.equal(codexCompactMode(), "pass", "default is pass"); + assert.equal(codexCompactMode(), "intercept", "default is intercept"); process.env.BILI_CODEX_COMPACT = "pass"; assert.equal(codexCompactMode(), "pass"); process.env.BILI_CODEX_COMPACT = "intercept"; assert.equal(codexCompactMode(), "intercept"); process.env.BILI_CODEX_COMPACT = "INTERCEPT"; assert.equal(codexCompactMode(), "intercept", "case-insensitive"); - process.env.BILI_CODEX_COMPACT = " intercept "; - assert.equal(codexCompactMode(), "intercept", "trimmed"); - process.env.BILI_CODEX_COMPACT = "bogus"; - assert.equal(codexCompactMode(), "pass", "unknown value falls back to pass"); + process.env.BILI_CODEX_COMPACT = " pass "; + assert.equal(codexCompactMode(), "pass", "trimmed"); + process.env.BILI_CODEX_COMPACT = "banana"; + assert.equal(codexCompactMode(), "intercept", "unknown value stays on intercept"); } finally { if (prev === undefined) delete process.env.BILI_CODEX_COMPACT; else process.env.BILI_CODEX_COMPACT = prev; From e6b822da3781f21731b44044626c40c6493c130e Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 29 Aug 2026 16:16:42 +0800 Subject: [PATCH 18/18] fix: auto-fallback to wire tools when codex upstream is local/private codex 0.147 ships MCP tools to the model as a namespace tool type; self-hosted servers (sglang/vllm/ollama/llama.cpp) do not parse it, so the injected bili MCP tools become silently invisible and the model fumbles for them. Users had no way to discover BILI_LAUNCHER_PLUGIN=0. launcherInjectMcp now falls back to wire-injected flat tools when the codex upstream host is loopback/RFC1918/ULA/link-local/.local and BILI_LAUNCHER_PLUGIN is unset; BILI_LAUNCHER_PLUGIN=1 forces plugin mode. claude is unaffected (it flattens MCP tools itself). --- CONFIGURATION.md | 3 +- CONFIGURATION.zh-CN.md | 3 +- src/launcher.ts | 70 +++++++++++++++++++++++++++--- tests/launcher-plugin-mode.test.ts | 24 +++++++++- 4 files changed, 91 insertions(+), 9 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index b8ff76bc6e..85eaef3bb6 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -356,7 +356,7 @@ Environment variables take precedence over the config file. They are useful for | `BILLION_CONTEXT_PROXY` | Exported by the launcher; client-side bili plugins/extensions detect it and self-disable their own compression (no double compression). | | `BILLION_CONTEXT_PLUGIN` | Set `0` to disable plugin mode entirely (wire-level tool injection resumes). | | `BILI_LAUNCHER_MODEL_WINDOWS` | Internal: the launcher hands the client's own per-model context windows (pi `models.json`, omp `models.yml`, opencode `models..limit`, codex `model_context_window`) to the spawned proxy as JSON, so the nudge denominator matches the real window for self-hosted models. Only the launcher sets it — no user configuration. | -| `BILI_LAUNCHER_PLUGIN` | Set `0` to disable the launcher's bili MCP server injection for claude/codex (pure wire mode). Native MCP tools are injected by default. See [Launcher Reference](#launcher-reference). | +| `BILI_LAUNCHER_PLUGIN` | Set `0` to disable the launcher's bili MCP server injection for claude/codex (pure wire mode); `1` forces plugin mode. Default: injected — except codex with a local/private upstream (sglang/vllm/ollama cannot parse codex's namespace tool type, so bili auto-falls back to wire tools there). See [Launcher Reference](#launcher-reference). | | `BILI_LAUNCHER_DIRECT` | Set `1` for direct-URL routing in the launcher (drop MITM/CA trust). See [Launcher Reference](#launcher-reference). | | `BILI_CLAUDE_UPSTREAM` | claude direct mode: your relay endpoint, when `ANTHROPIC_BASE_URL` already points at a relay the launcher would otherwise bypass. | | `BILI_CODEX_COMPACT` | Codex native-compaction handling. Default `intercept`: bili intercepts codex's compaction requests and forges a local handoff to the ACP state when the safety gate passes (transform ok + steady-state usage < 90% of the window + at least one active compressed block) — trigger form forges a 2-frame SSE, endpoint form forges `{output}` — and never contacts upstream. Forged ACP summaries are re-injected as a history-borne handoff message (developer-message fallback) so compressed content stays visible after codex truncates its history. Set `pass` to opt out and forward codex's compaction requests upstream (native compaction backstops). On any gate failure the request passes through untouched. | @@ -560,6 +560,7 @@ The `/bili/` rewrite modes write a **temp copy** — the real config is never ed - **omp** — does NOT ship the plugin; the launcher auto-injects `-e dist/agent/omp.js` when the config carries no loadable bili entry (same zero-config ride as pi). Two omp-specific mechanics make the plugin fully native there: omp 17.x mounts extension tools that omit `loadMode` under its `xd://` device URLs (invisible to the model's main turn), so the plugin registers its tools with `loadMode: "essential"` — the model gets the four ACP tools natively; and since omp's fork emits no `before_provider_headers`, the plugin binds the conversation via the launcher identity register (`POST /__bili/plugin/register`, keyed by omp's session id = `prompt_cache_key`/`x-session-id`) — bound sessions run in plugin mode (wire injection suppressed) with the native `/acp` command. - **opencode** — the temp config appends the thin plugin automatically. - **claude / codex** — on by default: the launcher injects a single `bili` MCP server (`--mcp-config` for claude, `-c mcp_servers.bili.*` for codex — both ephemeral, nothing written to host config), so the host gets native tools out of the box (verified with claude 2.1.227 / codex 0.147.0). `BILI_LAUNCHER_PLUGIN=0` falls back to plain wire mode — for hosts older than the verified builds that have not been tested against the injection flags. +- **codex + self-hosted upstream auto-fallback** — codex 0.147 ships MCP tools to the model as a `namespace` tool type; self-hosted servers (sglang/vllm/ollama/llama.cpp) do not parse it, leaving the tools silently invisible. When the codex upstream host is loopback/private (`127.0.0.1`, RFC1918, ULA, `.local`, …) and `BILI_LAUNCHER_PLUGIN` is unset, bili automatically uses wire mode instead (flat tools every server understands) and says so on stderr. `BILI_LAUNCHER_PLUGIN=1` forces plugin mode regardless. - **hermes** — no plugin API; always wire mode. - **dsh** — the launcher always splices a `--patch ` flag into dsh's argv (written to `~/.dsh-bili/.bili-acp.patch.yml`), inserting `dist/agent/dsh-acp.js` into the profile's loader tree: the native `/acp` command, same shape as dsh's own `/compact`. Works on every profile that composes the commands service (web/tui interactive surfaces; the `headless` one-shot driver sends its task straight to the model and parses no commands — `/compact` behaves the same there). Subcommand forms are handled: `dsh web` gets the flag after `web`, `dsh plugin`/`--dump-default-config` take none. diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 7b994f0a7c..e196d512ff 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -356,7 +356,7 @@ | `BILLION_CONTEXT_PROXY` | launcher 会导出它;客户端侧 bili 插件/扩展检测到后自禁用自身压缩(避免双重压缩)。 | | `BILLION_CONTEXT_PLUGIN` | 设 `0` 彻底关闭插件模式(恢复 wire 层工具注入)。 | | `BILI_LAUNCHER_MODEL_WINDOWS` | 内部使用:launcher 把客户端自身配置里的逐模型上下文窗口(pi `models.json`、omp `models.yml`、opencode `models..limit`、codex `model_context_window`)以 JSON 传给自己拉起的代理,让 nudge 分母对自托管模型也用真实窗口。只有 launcher 会设置,无需用户配置。 | -| `BILI_LAUNCHER_PLUGIN` | 设 `0` 关闭 launcher 为 claude/codex 注入 bili MCP 服务器(退回纯 wire 模式)。原生 MCP 工具默认注入。见[启动器参考](#启动器参考)。 | +| `BILI_LAUNCHER_PLUGIN` | 设 `0` 关闭 launcher 为 claude/codex 注入 bili MCP 服务器(退回纯 wire 模式);设 `1` 强制插件模式。默认注入——但 codex 上游为本地/私网地址时自动退回 wire 模式(sglang/vllm/ollama 不解析 codex 的 namespace 工具类型)。见[启动器参考](#启动器参考)。 | | `BILI_LAUNCHER_DIRECT` | 设 `1` 启用 launcher 直连 URL 路由(放弃 MITM/CA 信任)。见[启动器参考](#启动器参考)。 | | `BILI_CLAUDE_UPSTREAM` | claude 直连模式:当 `ANTHROPIC_BASE_URL` 已指向某个 relay 时,用它指定你的 relay 端点(否则会被旁路)。 | | `BILI_CODEX_COMPACT` | codex 原生压缩处理。默认 `intercept`:安全门通过时(transform 成功 + 稳态用量 < 窗口 90% + 至少一个活跃压缩块)拦截 codex 的压缩请求,在本地伪造向 ACP 状态的交接——trigger 形态伪造 2 帧 SSE,endpoint 形态伪造 `{output}`——且不接触上游。伪造的 ACP 摘要经历史承载交接消息注入(缺席时 developer 消息兜底),保证 codex 截断历史后压缩内容仍可见。设为 `pass` 可退出,把 codex 的压缩请求转发给上游(原生压缩兜底)。任一安全门失败则原样透传。 | @@ -560,6 +560,7 @@ Claude Code 的 undici fetch 忽略 `HTTPS_PROXY`,所以证书 MITM 拦不到 - **omp** —— 发行版不自带插件;启动器在配置里没有可加载的 bili 条目时自动注入 `-e dist/agent/omp.js`(与 pi 相同的零配置搭车)。两个 omp 专属机制让插件在那里完全原生:omp 17.x 会把未声明 `loadMode` 的扩展工具挂到 `xd://` 设备 URL 下(模型主回合看不到),插件因此用 `loadMode: "essential"` 注册 —— 模型直接拿到四个 ACP 原生工具;omp 分叉不发 `before_provider_headers`,插件改走启动器身份注册(`POST /__bili/plugin/register`,以 omp 会话 id = `prompt_cache_key`/`x-session-id` 为键)绑定会话 —— 绑定后的会话进入插件模式(抑制 wire 注入)并带有原生 `/acp` 命令。 - **opencode** —— 临时配置自动追加薄插件。 - **claude / codex** —— 默认开启:启动器注入单个 `bili` MCP 服务器(claude 用 `--mcp-config`,codex 用 `-c mcp_servers.bili.*` —— 都是临时生效,不写宿主配置),开箱即原生工具(已在 claude 2.1.227 / codex 0.147.0 验证)。`BILI_LAUNCHER_PLUGIN=0` 退回纯 wire 模式 —— 适用于早于已验证版本、未针对注入参数测试的宿主。 +- **codex + 自建上游自动回退** —— codex 0.147 把 MCP 工具以 `namespace` 工具类型发给模型;自建推理服务(sglang/vllm/ollama/llama.cpp)不解析该类型,工具会静默失明。当 codex 上游主机是环回/私网地址(`127.0.0.1`、RFC1918、ULA、`.local` 等)且未设置 `BILI_LAUNCHER_PLUGIN` 时,bili 自动改用 wire 模式(扁平工具,所有服务都认识)并在 stderr 说明。`BILI_LAUNCHER_PLUGIN=1` 可强制插件模式。 - **hermes** —— 无插件 API;永远 wire 模式。 - **dsh** —— 启动器始终在 dsh 的 argv 里拼接 `--patch `(写入 `~/.dsh-bili/.bili-acp.patch.yml`),把 `dist/agent/dsh-acp.js` 插进 profile 的加载树:原生 `/acp` 命令,与 dsh 自带 `/compact` 同一形态。在任何组合了 commands 服务的 profile(web/tui 交互表面)都可用;`headless` 一次性驱动器把任务直接发给模型、不解析命令(原生 `/compact` 在那里同样不可用)。子命令形态已处理:`dsh web` 的 flag 插在 `web` 之后,`dsh plugin`/`--dump-default-config` 不注入。 diff --git a/src/launcher.ts b/src/launcher.ts index dd7048b7c9..14d328755c 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -482,6 +482,48 @@ export function launcherDirectUrl(env: NodeJS.ProcessEnv): boolean { return env.BILI_LAUNCHER_DIRECT === "1"; } +/** True when the host points at self-hosted inference: loopback, RFC1918, + * link-local, IPv6 ULA, or an mDNS/LAN name. Those servers (sglang/vllm/ + * ollama/llama.cpp) do not understand codex's `namespace` tool type, so + * MCP-injected tools would be silently invisible to the model. */ +export function isPrivateUpstreamHost(raw: string): boolean { + let host: string; + try { + host = new URL(raw).hostname.toLowerCase(); + } catch { + return false; + } + if (host === "localhost" || host.endsWith(".local") || host.endsWith(".lan") || host.endsWith(".internal")) return true; + if (host.startsWith("[")) host = host.slice(1, -1); + if (host.includes(":")) { + if (host === "::1" || host === "::") return true; + const first = host.split(":")[0] ?? ""; + if (/^f[cd]/.test(first)) return true; + if (/^fe[89ab]/.test(first)) return true; + const dotted = /::ffff:(\d+\.\d+\.\d+\.\d+)$/.exec(host); + if (dotted && isPrivateIPv4(dotted[1]!)) return true; + const hex = /::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(host); + if (hex) { + const w1 = Number.parseInt(hex[1]!, 16); + const w2 = Number.parseInt(hex[2]!, 16); + if (isPrivateIPv4(`${w1 >> 8}.${w1 & 0xff}.${w2 >> 8}.${w2 & 0xff}`)) return true; + } + return false; + } + if (/^\d+\.\d+\.\d+\.\d+$/.test(host)) return isPrivateIPv4(host); + return false; +} + +function isPrivateIPv4(host: string): boolean { + const a = Number.parseInt(host.split(".")[0] ?? "", 10); + const b = Number.parseInt(host.split(".")[1] ?? "", 10); + if (a === 127 || a === 10 || a === 0) return true; + if (a === 172 && b >= 16 && b <= 31) return true; + if (a === 192 && b === 168) return true; + if (a === 169 && b === 254) return true; + return false; +} + /** Plugin-in-launcher MCP injection is ON by default for claude/codex * (zero-config, mirroring the pi/omp/opencode auto-injection): the launcher * injects a single `bili` MCP server so the host gets native tools instead @@ -489,9 +531,22 @@ export function launcherDirectUrl(env: NodeJS.ProcessEnv): boolean { * pure wire mode — for hosts older than the verified builds (claude 2.1.227, * codex 0.147.0) that have not been tested against `--mcp-config` / * `-c mcp_servers.*`. pi/omp/opencode/hermes/dsh are always excluded — they - * have their own native plugin surface (or none). */ -export function launcherInjectMcp(env: NodeJS.ProcessEnv, base: string): boolean { - return base !== "pi" && base !== "omp" && base !== "opencode" && base !== "hermes" && base !== "dsh" && env.BILI_LAUNCHER_PLUGIN !== "0"; + * have their own native plugin surface (or none). + * + * codex auto-fallback: codex 0.147 ships MCP tools to the model as a + * `namespace` tool type. Self-hosted upstreams (sglang/vllm/ollama) do not + * parse it — the injected tools become silently invisible and the model + * fumbles for them. When the codex upstream is a local/private endpoint and + * the user has not chosen explicitly, wire mode (flat tools every server + * understands) is the sane default. `BILI_LAUNCHER_PLUGIN=1` forces plugin + * mode regardless of the upstream. */ +export function launcherInjectMcp(env: NodeJS.ProcessEnv, base: string, codexUpstream?: string): boolean { + if (base === "pi" || base === "omp" || base === "opencode" || base === "hermes" || base === "dsh") return false; + if (env.BILI_LAUNCHER_PLUGIN === "0") return false; + if (base === "codex" && env.BILI_LAUNCHER_PLUGIN === undefined && codexUpstream !== undefined && isPrivateUpstreamHost(codexUpstream)) { + return false; + } + return true; } /** Ephemeral MCP config for --mcp-config / -c mcp_servers.bili.*: a single @@ -1418,11 +1473,16 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} ); } } - const injectMcp = launcherInjectMcp(process.env, base); + const codexUpstream = base === "codex" ? codexUpstreamUrl(config.codex) : undefined; + const injectMcp = launcherInjectMcp(process.env, base, codexUpstream); if (injectMcp) { console.error(`bili: injecting native bili MCP tools for ${base} (disable with BILI_LAUNCHER_PLUGIN=0).`); } else if (base === "claude" || base === "codex") { - console.error("bili: native MCP tools disabled (BILI_LAUNCHER_PLUGIN=0) — running in pure wire mode."); + if (process.env.BILI_LAUNCHER_PLUGIN === "0") { + console.error("bili: native MCP tools disabled (BILI_LAUNCHER_PLUGIN=0) — running in pure wire mode."); + } else { + console.error(`bili: codex upstream ${codexUpstream} is local/private — self-hosted models cannot see codex namespace MCP tools; using wire-injected flat tools instead (force MCP with BILI_LAUNCHER_PLUGIN=1).`); + } } const origin = handle.origin; if (base === "pi") { diff --git a/tests/launcher-plugin-mode.test.ts b/tests/launcher-plugin-mode.test.ts index 2524e19d66..335109f594 100644 --- a/tests/launcher-plugin-mode.test.ts +++ b/tests/launcher-plugin-mode.test.ts @@ -11,7 +11,7 @@ import { startServer, type ProxyOptions } from "../src/server.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; import { _resetPluginStateForTest } from "../src/plugin.ts"; -import { buildClaudePluginEnv, buildCodexMcpArgs, buildMcpConfig, launcherDirectUrl, launcherInjectMcp } from "../src/launcher.ts"; +import { buildClaudePluginEnv, buildCodexMcpArgs, buildMcpConfig, isPrivateUpstreamHost, launcherDirectUrl, launcherInjectMcp } from "../src/launcher.ts"; // Launcher mode (#162): hosts that cannot attach per-request headers // (claude/codex spawned by `bili claude` / `bili codex`) bind into plugin mode @@ -236,7 +236,27 @@ test("launcher injection builders: direct-URL env, MCP config JSON, codex -c arg // hosts older than the verified builds (claude 2.1.227, codex 0.147.0). assert.equal(launcherInjectMcp({}, "claude"), true, "plugin injection on by default (claude)"); assert.equal(launcherInjectMcp({}, "codex"), true, "plugin injection on by default (codex)"); - assert.equal(launcherInjectMcp({ BILI_LAUNCHER_PLUGIN: "0" }, "claude"), false, "explicit opt-out honored (claude)"); + assert.equal(launcherInjectMcp({}, "codex", "http://127.0.0.1:8199/v1"), false, "loopback codex upstream falls back to wire tools"); + assert.equal(launcherInjectMcp({}, "codex", "http://localhost:8000/v1"), false, "localhost falls back"); + assert.equal(launcherInjectMcp({}, "codex", "http://192.168.1.5:8000/v1"), false, "RFC1918 codex upstream falls back"); + assert.equal(launcherInjectMcp({}, "codex", "http://10.0.0.3:11434/v1"), false, "10/8 falls back (ollama-style)"); + assert.equal(launcherInjectMcp({}, "codex", "http://[::1]:8080/v1"), false, "IPv6 loopback falls back"); + assert.equal(launcherInjectMcp({}, "codex", "http://[fd00::1]:8080/v1"), false, "IPv6 ULA falls back"); + assert.equal(launcherInjectMcp({}, "codex", "http://mybox.local:8000/v1"), false, "mDNS name falls back"); + assert.equal(launcherInjectMcp({ BILI_LAUNCHER_PLUGIN: "1" }, "codex", "http://127.0.0.1:8199/v1"), true, "explicit opt-in overrides the local-upstream fallback"); + assert.equal(launcherInjectMcp({ BILI_LAUNCHER_PLUGIN: "0" }, "codex", "http://127.0.0.1:8199/v1"), false, "explicit opt-out still honored"); + assert.equal(launcherInjectMcp({}, "codex", "https://api.openai.com/v1"), true, "public codex upstream keeps MCP tools"); + assert.equal(launcherInjectMcp({}, "codex", "https://gwv1701.comfly.org/v1"), true, "public relay keeps MCP tools"); + assert.equal(launcherInjectMcp({}, "codex", "not a url"), true, "unparseable upstream: keep MCP tools (conservative)"); + assert.equal(launcherInjectMcp({}, "claude", "http://127.0.0.1:8199"), true, "auto-fallback is codex-only (claude flattens MCP tools itself)"); + assert.equal(isPrivateUpstreamHost("http://127.0.0.1:8199/v1"), true); + assert.equal(isPrivateUpstreamHost("http://172.16.0.1/v1"), true); + assert.equal(isPrivateUpstreamHost("http://172.32.0.1/v1"), false, "172 outside 16-31 is public"); + assert.equal(isPrivateUpstreamHost("http://169.254.1.1/v1"), true, "link-local"); + assert.equal(isPrivateUpstreamHost("http://8.8.8.8/v1"), false); + assert.equal(isPrivateUpstreamHost("http://[::ffff:10.1.2.3]:9/v1"), true, "IPv4-mapped IPv6"); + assert.equal(isPrivateUpstreamHost("http://[2001:db8::1]:9/v1"), false, "global IPv6"); + assert.equal(isPrivateUpstreamHost(""), false, "empty string unparseable"); assert.equal(launcherInjectMcp({ BILI_LAUNCHER_PLUGIN: "0" }, "claude"), false, "explicit opt-out honored (claude)"); assert.equal(launcherInjectMcp({ BILI_LAUNCHER_PLUGIN: "0" }, "codex"), false, "explicit opt-out honored (codex)"); assert.equal(launcherInjectMcp({ BILI_LAUNCHER_PLUGIN: "1" }, "claude"), true, "explicit opt-in still works"); assert.equal(launcherInjectMcp({ BILI_LAUNCHER_PLUGIN: "1" }, "pi"), false, "pi always excluded (native extension #154)");