From e59495a63768fe73e205ef4f4af36dd96dd29e34 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 29 Aug 2026 03:00:28 +0800 Subject: [PATCH] feat: codex compression-budget coordination (PR-D/E1/C/E2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-D: bili codex launcher injects -c model_context_window / model_auto_compact_token_limit from bili's effective window (single source of truth shared with the server pipeline), so codex's auto-compact ledger aligns with bili and ACP fires first. PR-E1: bundled codex model-table snapshot (scripts/update-codex-models-snapshot.mjs, same refresh pattern as registry-snapshot); when the client is codex (x-codex-turn-metadata header) and the model is in the table, the effective window clamps to min(bili window, codex table). PR-C: semantic rebasing gate — the native-compaction boundary is now marked only on a confirmed response.completed (SSE observer / JSON parse); failed/incomplete/truncated/empty-body outcomes leave ACP state untouched. Trigger-final requests take a read-only projection (no kernel state mutation, no injections, no preflight). PR-E2: conditional interception + forge (kill-switch BILI_CODEX_COMPACT=intercept, default pass). When ACP state is healthy, codex compaction requests (trailing compaction_trigger or /responses/compact) are answered locally: V2 SSE with a single compaction item + response.completed, or V1 JSON {output:[compaction]}. The ACP summary is recorded by forged item id and replayed as a developer message on the next request, giving a deterministic handoff. Unhealthy ACP passes through to native compaction (PR-C gates the rebase correctly). Tests: tests/codex-compact-coordination.test.ts (12 unit + 11 e2e). Full suite 763/763, typecheck + build clean. --- package.json | 1 + scripts/update-codex-models-snapshot.mjs | 86 ++++ src/client-config.ts | 4 +- src/codex-models-snapshot.json | 1 + src/codex-models.ts | 31 ++ src/config.ts | 30 ++ src/launcher.ts | 26 +- src/server.ts | 323 +++++++++++-- tests/codex-compact-coordination.test.ts | 551 +++++++++++++++++++++++ 9 files changed, 1018 insertions(+), 35 deletions(-) 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-compact-coordination.test.ts diff --git a/package.json b/package.json index 19f4101e73..6df1ecc69f 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "scripts": { "build": "tsup", "registry:snapshot": "node scripts/update-registry-snapshot.mjs", + "codex: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..60c94c6bc7 --- /dev/null +++ b/scripts/update-codex-models-snapshot.mjs @@ -0,0 +1,86 @@ +#!/usr/bin/env node +/** Refresh src/codex-models-snapshot.json from openai/codex's model table + * (codex-rs/models-manager/models.json on the main branch). + * + * The snapshot is committed to the repo and bundled into dist at build time + * (same offline-floor pattern as registry-snapshot.json). It feeds the + * #320 PR-E1 budget clamp: when a codex client (x-codex-turn-metadata header) + * requests a model that is in this table, bili's effective window is clamped + * to min(bili window, codex window) so ACP always compresses before codex's + * own auto-compact ledger trips. + * + * Only the window fields are kept per model: auto_compact_token_limit is + * null for every current entry (codex clamps to 90% of the window in code), + * so it carries no information. + * + * Run manually or before a release: + * npm run codex: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, or the fetched table has no usable windows, 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 MODELS_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(MODELS_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 fetchTable() { + if (proxyUrl) { + try { + const { ProxyAgent } = await import("undici"); + const table = await attempt(new ProxyAgent({ uri: proxyUrl })); + console.log(`fetched codex model table via proxy ${proxyUrl}`); + return table; + } catch (e) { + console.log(`proxy attempt failed (${e.message}); trying direct`); + } + } + const table = await attempt(undefined); + console.log("fetched codex model table direct"); + return table; +} + +let table; +try { + table = await fetchTable(); +} catch (e) { + console.error(`could not fetch ${MODELS_URL}: ${e.message}`); + console.error(`keeping the existing ${path.basename(OUT_FILE)} untouched`); + process.exit(1); +} + +const entries = Array.isArray(table?.models) ? table.models : []; +const models = {}; +for (const entry of entries) { + if (!entry || typeof entry.slug !== "string") continue; + const slim = {}; + if (typeof entry.context_window === "number" && entry.context_window > 0) slim.context_window = entry.context_window; + if (typeof entry.max_context_window === "number" && entry.max_context_window > 0) slim.max_context_window = entry.max_context_window; + if (Object.keys(slim).length > 0) models[entry.slug] = slim; +} +const count = Object.keys(models).length; +if (count === 0) { + console.error(`fetched table has ${entries.length} entries but no usable context windows — refusing to publish an empty snapshot`); + console.error(`keeping the existing ${path.basename(OUT_FILE)} untouched`); + process.exit(1); +} + +const slim = { fetchedAt: new Date().toISOString(), source: "openai/codex codex-rs/models-manager/models.json", models }; +const body = JSON.stringify(slim); +await writeFile(OUT_FILE, body, "utf8"); +console.log(`wrote ${OUT_FILE} (${count} models with windows, ${(body.length / 1024).toFixed(1)} KB)`); diff --git a/src/client-config.ts b/src/client-config.ts index 510829baf6..f47fecf7fe 100644 --- a/src/client-config.ts +++ b/src/client-config.ts @@ -28,6 +28,8 @@ export interface CodexProvider { export interface CodexConfig { modelProvider?: string; openaiBaseUrl?: string; + /** Top-level `model` — the model codex will run (default model). */ + model?: string; /** Top-level `model` + `model_context_window` override pair (if set). */ modelWindows?: ModelWindow[]; providers: Record; @@ -220,7 +222,7 @@ export function parseCodexToml(text: string): CodexConfig { if (table === "") { if (key === "model_provider") result.modelProvider = val; else if (key === "openai_base_url") result.openaiBaseUrl = val; - else if (key === "model") codexModel = val; + else if (key === "model") { codexModel = val; result.model = val; } } else if (curProvider && key === "base_url") { result.providers[curProvider].baseUrl = val; } diff --git a/src/codex-models-snapshot.json b/src/codex-models-snapshot.json new file mode 100644 index 0000000000..fe4b60c024 --- /dev/null +++ b/src/codex-models-snapshot.json @@ -0,0 +1 @@ +{"fetchedAt":"2026-08-28T16:23:27.229Z","source":"openai/codex codex-rs/models-manager/models.json","models":{"gpt-5.6-sol":{"context_window":272000,"max_context_window":872000},"gpt-5.6-terra":{"context_window":272000,"max_context_window":872000},"gpt-5.6-luna":{"context_window":272000,"max_context_window":872000},"gpt-daybreak-blue-latest":{"context_window":272000,"max_context_window":872000},"gpt-daybreak-red-latest":{"context_window":372000,"max_context_window":372000},"gpt-5.5":{"context_window":272000,"max_context_window":272000},"gpt-5.4":{"context_window":272000,"max_context_window":1000000},"gpt-5.4-mini":{"context_window":272000,"max_context_window":272000},"gpt-5.2":{"context_window":272000,"max_context_window":272000},"codex-auto-review":{"context_window":272000,"max_context_window":872000}}} diff --git a/src/codex-models.ts b/src/codex-models.ts new file mode 100644 index 0000000000..930bdd4585 --- /dev/null +++ b/src/codex-models.ts @@ -0,0 +1,31 @@ +import type { IncomingHttpHeaders } from "node:http"; +import codexSnapshot from "./codex-models-snapshot.json" with { type: "json" }; + +type CodexModelEntry = { context_window?: number; max_context_window?: number }; +type CodexSnapshotShape = { fetchedAt?: unknown; source?: unknown; models?: Record }; + +/** Slim snapshot of openai/codex's model table (codex-rs/models-manager/ + * models.json), committed at src/codex-models-snapshot.json (refresh with + * `npm run codex:snapshot`) and inlined into dist at build time — the same + * offline-floor pattern as registry-snapshot.json. Only the window fields + * are kept: auto_compact_token_limit is null for every current entry (the + * 90% clamp lives in codex code), so context_window is codex's knowledge. */ +const MODELS: Record = (() => { + const snap = codexSnapshot as CodexSnapshotShape; + if (!snap || typeof snap !== "object" || !snap.models || typeof snap.models !== "object") return {}; + return snap.models; +})(); + +export function codexModelWindow(model: string | undefined): number | undefined { + if (!model) return undefined; + const entry = MODELS[model]; + if (!entry) return undefined; + const primary = entry.context_window; + if (typeof primary === "number" && primary > 0) return primary; + const max = entry.max_context_window; + return typeof max === "number" && max > 0 ? max : undefined; +} + +export function isCodexClient(headers: IncomingHttpHeaders): boolean { + return headers["x-codex-turn-metadata"] !== undefined; +} diff --git a/src/config.ts b/src/config.ts index dba3785380..20c41ccb36 100644 --- a/src/config.ts +++ b/src/config.ts @@ -4,6 +4,7 @@ import { dirname } from "node:path"; import { configFile } from "./paths.js"; import { log as loggerLog } from "./logger.js"; import { validateHttpProxy, type ProxyFallbackOptions } from "./upstream-proxy.js"; +import { peekRegistryContext } from "./registry.js"; export function safeReadJson(path: string): unknown { try { @@ -144,6 +145,35 @@ export function lookupContextLimit(model: string | undefined): number | undefine return undefined; } +export type StaticWindowSource = "launcher" | "registry" | "config" | "table"; + +/** Static (sync, never-fetch) resolution of bili's effective context window + * for a model — the exact source chain and order the request pipeline walks + * before its async registry upgrade: launcher-declared window → warm + * models.dev cache → per-route config → built-in table. Shared by the server + * (per request) and the `bili codex` launcher (spawn-time budget + * coordination, #320) so both read one source of truth. */ +export function resolveStaticWindow( + model: string | undefined, + host: string | undefined, + routes: ProviderRoutes, + upstreamUrl: string | undefined, + launcherWindows: Record | undefined, +): { window: number; source: StaticWindowSource } | undefined { + if (!model) return undefined; + const launcher = launcherWindows?.[model]; + if (typeof launcher === "number" && launcher > 0) { + return { window: launcher, source: "launcher" }; + } + const peeked = peekRegistryContext(model, host); + if (peeked !== undefined) return { window: peeked, source: "registry" }; + const configured = resolveConfiguredContextLimit(routes, upstreamUrl, model); + if (configured !== undefined) return { window: configured, source: "config" }; + const table = lookupContextLimit(model); + if (table !== undefined) return { window: table, source: "table" }; + return undefined; +} + /** Floor for the EFFECTIVE context window (after output-headroom reservation) * when the window came from a low-confidence fallback — the built-in table * above or the env default — rather than an authoritative source (plugin diff --git a/src/launcher.ts b/src/launcher.ts index 20b2235750..9c6fae08ec 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -43,6 +43,8 @@ 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 { loadRoutes, resolveStaticWindow } from "./config.js"; +import { codexModelWindow } from "./codex-models.js"; export { type ClaudeSettings, @@ -349,6 +351,28 @@ export function buildCodexArgs( return args; } +/** #320 PR-D: point codex's auto-compact ledger at bili's effective window so + * ACP (55-75%) always fires before codex native auto-compact (90%). Both keys + * get the SAME value on purpose: codex internally clamps the compact limit to + * min(configured, 90% x window). The min() against codexModelWindow is + * load-bearing — never tell codex a window bigger than its own table assumes. */ +export function codexBudgetArgs(config: ClientConfig): string[] { + const codex = config.codex; + const model = codex?.model; + if (!codex || !model) return []; + const upstream = codex.providers?.[codex.modelProvider ?? ""]?.baseUrl ?? codex.openaiBaseUrl; + let host: string | undefined; + if (upstream) { + try { host = new URL(upstream).host; } catch { host = undefined; } + } + const win = resolveStaticWindow(model, host, loadRoutes(process.env), upstream, collectModelWindows(config)); + if (!win) return []; + const codexWindow = codexModelWindow(model); + const effective = codexWindow !== undefined ? Math.min(win.window, codexWindow) : win.window; + console.error(`[bili] codex budget: model=${model} window=${effective} (bili ${win.source}=${win.window}${codexWindow !== undefined ? `, codex table=${codexWindow}` : ""})`); + return ["-c", `model_context_window=${effective}`, "-c", `model_auto_compact_token_limit=${effective}`]; +} + export function buildClaudeEnv( origin: string, caPath: string, @@ -1435,7 +1459,7 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} if (codexConversationId) clientArgs = [...buildCodexMcpArgs(origin, codexConversationId), ...clientArgs]; } else { env = buildCodexEnv(origin, resolveCombinedCaPath(process.env), process.env); - clientArgs = buildCodexArgs(origin, routes.httpRewrites, routes.httpsRewrites, clientArgs); + clientArgs = buildCodexArgs(origin, routes.httpRewrites, routes.httpsRewrites, [...codexBudgetArgs(config), ...clientArgs]); if (injectMcp && codexConversationId) clientArgs = [...buildCodexMcpArgs(origin, codexConversationId), ...clientArgs]; } } else { diff --git a/src/server.ts b/src/server.ts index 60d2eac1a2..61b4b1e150 100644 --- a/src/server.ts +++ b/src/server.ts @@ -6,8 +6,9 @@ import { resolveCompress, resolveCompressPrompts, resolveRequestConfig } from ". import type { ProxyOptions } from "./config.js"; 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 { FALLBACK_EFFECTIVE_WINDOW_FLOOR, resolveCompressProtocol, resolveStaticWindow } from "./config.js"; +import { contextFromRegistry, loadRegistry } from "./registry.js"; +import { codexModelWindow, isCodexClient } 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"; @@ -40,7 +41,7 @@ import { conversationSignalResponses, subagentNamespace, } from "acp-kernel/wire"; -import { getSession, listSessions, type Session, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, withSessionLock, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, snapshotMessages } from "./session.js"; +import { getSession, listSessions, type Session, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, withSessionLock, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, resetSessionCompression, snapshotMessages } from "./session.js"; import { COMPRESS_TOOL, ACP_TOOLS_ANTHROPIC, ACP_TOOLS_OPENAI, ACP_TOOLS_RESPONSES, ACP_READONLY_TOOLS_RESPONSES, COMPRESS_TOOL_NAME, buildCompressSystemPrompt, buildCompressHybridSystemPrompt, withStagedCompressGuidance } from "./compress-tool.js"; import { rewriteSseStream, rewriteJsonResponse, type RewriteCtx } from "./stream.js"; import { applyRanges } from "./stream.js"; @@ -107,10 +108,6 @@ export function parseLauncherModelWindows(raw: string | undefined): Record> = parseLauncherModelWindows(process.env.BILI_LAUNCHER_MODEL_WINDOWS); -function launcherContextWindow(model: string): number | undefined { - return LAUNCHER_MODEL_WINDOWS[model]; -} - /** Parse an `anthropic-beta` header for a larger-context beta (e.g. * `context-1m-2025-08-07` → 1,000,000). The beta lets the CLIENT negotiate a * window beyond the model's standard size, so it is the most direct per-request @@ -437,6 +434,9 @@ type Prepared = { pluginMode?: boolean; responsesTextProtocol?: boolean; resetAfterSuccess?: boolean; + /** #320 PR-C: trigger-final requests are read-only projections — preflight + * must not inject a compress call AFTER the terminal compaction_trigger. */ + skipPreflight?: boolean; responsesProjection?: ResponsesProjection; anthropicSystem?: AnthropicRequestBody["system"]; /** Original leading system/developer prefix text captured by the kernel's @@ -778,26 +778,28 @@ async function handle( const host = (() => { try { return embeddedUrl ? new URL(embeddedUrl).host : undefined; } catch { return undefined; } })(); const betaWindow = anthropicBetaContextWindow(req.headers); const pluginWindow = pluginReportedContextWindow(req.headers); - const launcherWindow = launcherContextWindow(model); - const peekWindow = host ? peekRegistryContext(model, host) : undefined; - const configuredWindow = resolveConfiguredContextLimit(opts.routes, embeddedUrl, model); - let native = betaWindow - ?? pluginWindow - ?? launcherWindow - ?? peekWindow - ?? configuredWindow - ?? lookupContextLimit(model); + const staticWindow = resolveStaticWindow(model, host, opts.routes, embeddedUrl, LAUNCHER_MODEL_WINDOWS); + let native = betaWindow ?? pluginWindow ?? staticWindow?.window; // Fallback = no authoritative source AND the operator did not // explicitly tune the window via compress.modelContextLimit (an // explicit tuning is owned by the operator — never floored). The // beta window is authoritative (the client's own runtime // negotiation), so it also clears the fallback flag. const operatorWindowTuned = resolveCompress(opts.routes, embeddedUrl, model, opts.compress).modelContextLimit !== undefined; - nativeFromFallback = !betaWindow && !pluginWindow && !launcherWindow && !peekWindow && !configuredWindow && !operatorWindowTuned; + nativeFromFallback = !betaWindow && !pluginWindow && (staticWindow === undefined || staticWindow.source === "table") && !operatorWindowTuned; if (!native && host) { native = await contextFromRegistry(model, host); if (native) nativeFromFallback = false; } + // #320 PR-E1: codex's auto-compact ledger follows its own model + // table, not bili's window — clamp down to codex's knowledge so + // ACP always fires first. Models absent from the codex table are + // never auto-compacted by codex, so no clamp applies. + const codexClamp = applyCodexWindowClamp(native, model, req.headers); + if (codexClamp.clamped) { + native = codexClamp.window; + nativeFromFallback = false; + } reqConfig = resolveRequestConfig(config, opts.routes, embeddedUrl, model, native, opts.compress); reqPrompts = resolveCompressPrompts(resolveCompress(opts.routes, embeddedUrl, model, opts.compress)); } @@ -1030,6 +1032,13 @@ async function handle( } if (reserved !== reqConfig.modelContextLimit) reqConfig = { ...reqConfig, modelContextLimit: reserved }; } + // #320 PR-E2: with BILI_CODEX_COMPACT=intercept and healthy ACP state, + // forge codex's compaction response locally (deterministic handoff); + // otherwise pass through — the PR-C semantic gate rebases only on a + // genuinely completed native compaction. + if (await forgeCodexCompactIfEligible(req, res, session, reqConfig, responsesCompact, parsed, log)) { + return; + } // acquireInFlight must precede the lock so evictOldest() cannot flush // this session between getSession and lock acquisition (inFlight===0 // window). Released in the outer finally after forward completes. @@ -1050,7 +1059,7 @@ async function handle( ? prepareResponsesCompact(bodyBuffer, parsed as ResponsesRequestBody, session) : prepareResponses(parsed as ResponsesRequestBody, req, opts, core, reqConfig, reqPrompts, log, session, responsesIdentity!, pluginMode, upstreamOrigin); prepared = runPrepare(); - if (!countTokens && !responsesCompact) { + if (!countTokens && !responsesCompact && !prepared.skipPreflight) { const outcome = await preflightCompressIfNeeded( prepared, runPrepare, @@ -1399,6 +1408,52 @@ function prepareResponses( log("info", `[${sessionId}] dropped ${droppedEmpty} whitespace-only message item(s) before projection (flattened-turn artifact)`); } + // #320 PR-E2: replay forged compaction summaries — items whose id bili + // forged are swapped in place for a developer message carrying the summary + // (real/native compaction items with unknown ids pass through untouched). + const forgedReplayed = rewriteForgedCompactions(parsed.input, session); + if (forgedReplayed > 0) { + log("info", `[${sessionId}] replayed ${forgedReplayed} forged compaction summary(s) as developer message(s)`); + } + + // #320 PR-C: a request whose FINAL input item is `compaction_trigger` is a + // native remote-compact call (codex requires the trigger to stay final or + // the upstream 400s). Forward it as a READ-ONLY projection: sanitized + // original input, no kernel state mutation, no compress prompt/tools, no + // nudge, no preflight. Unknown item types round-trip byte-identical + // through the kernel, so the trigger survives this path untouched. + const triggerFinal = Array.isArray(parsed.input) + && parsed.input.length > 0 + && (parsed.input[parsed.input.length - 1] as { type?: string })?.type === "compaction_trigger"; + if (triggerFinal) { + const rebuilt: ResponsesRequestBody = { ...parsed }; + const promptCacheKey = resolvePromptCacheKey( + rebuilt.prompt_cache_key, + identity, + opts.promptCache.routing, + upstreamOrigin, + ); + if (promptCacheKey && !rebuilt.prompt_cache_key) rebuilt.prompt_cache_key = promptCacheKey; + if (process.env.ACP_KEEP_RESPONSE_ID !== "1") delete rebuilt.previous_response_id; + delete rebuilt.instructions; + delete (rebuilt as Record).prompt_cache_retention; + log("info", `[${sessionId}] compaction_trigger final — read-only projection (no injection, no state mutation, no preflight)`); + markDirty(session); + return { + body: JSON.stringify(rebuilt), + session, + processedMessages: [], + originalMessages: [], + protocol: "responses", + stream, + compressInjected: false, + pluginMode, + nudge: undefined, + prompts, + skipPreflight: true, + }; + } + const shouldInject = opts.compress.injectTool; const injectTools = shouldInject && !pluginMode; // Route config is keyed by the upstream THIS request goes to (#286: a @@ -1448,15 +1503,7 @@ 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) { + if (opts.compress.injectNudge && turn.nudge?.shouldInject && shouldInject) { try { const rendered = renderNudgeText(turn.nudge, prompts); if (rendered.text) { @@ -2126,9 +2173,10 @@ async function forward( if (!upstream.body) { res.end(); clearUpstreamTimer(); + // #320 PR-C: a body-less 2xx (e.g. 204) carries no terminal event — + // outcome unknown; the safe default is NOT to rebase ACP state. 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 Responses compact returned an empty body (status ${upstream.status}); outcome unknown, NOT rebasing ACP state`); } return; } @@ -2160,11 +2208,24 @@ 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`); + // #320 PR-C: semantic rebase gate — observe the response terminal + // event instead of trusting HTTP 2xx. Responses failures arrive as + // response.failed/incomplete INSIDE a 200 SSE stream; rebasing ACP + // state on those discards recoverable compression state. + const outcome = prepared.stream + ? await pipeThroughObservingSse(upstream.body, res) + : await pipeThroughObservingJson(upstream.body, res); + clearUpstreamTimer(); + if (outcome === "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 Responses compact outcome=${outcome}; NOT rebasing ACP state`); + } + } else { + await pipeThrough(upstream.body, res); + clearUpstreamTimer(); } return; } @@ -2342,6 +2403,98 @@ async function pipeThrough(stream: ReadableStream, res: http.ServerR } } +type CompactOutcome = "completed" | "failed" | "unknown"; + +function sseTerminalOutcome(block: string): CompactOutcome { + // Cheap prefilter: only blocks mentioning a terminal type are parsed. A + // model's text can contain these literals inside item content — the JSON + // `type` field of the parsed event is authoritative, so false positives + // from the prefilter resolve to "unknown". + if (!block.includes("response.completed") && !block.includes("response.failed") && !block.includes("response.incomplete")) { + return "unknown"; + } + let eventType: string | undefined; + for (const line of block.split("\n")) { + if (line.startsWith("event:")) { + eventType = line.slice(6).trim(); + continue; + } + if (line.startsWith("data:")) { + const raw = line.slice(5).trim(); + if (!raw) continue; + try { + const ev = JSON.parse(raw) as { type?: unknown }; + if (typeof ev.type === "string") eventType = ev.type; + } catch { + // non-JSON data line — not a terminal event + } + } + } + if (eventType === "response.completed") return "completed"; + if (eventType === "response.failed" || eventType === "response.incomplete") return "failed"; + return "unknown"; +} + +async function pipeThroughObservingSse(stream: ReadableStream, res: http.ServerResponse): Promise { + const reader = stream.getReader(); + const decoder = new TextDecoder(); + let buffer = ""; + let outcome: CompactOutcome = "unknown"; + try { + for (;;) { + const { done, value } = await reader.read(); + if (done) break; + const chunk = Buffer.from(value); + if (!res.write(chunk)) { + await new Promise((r) => res.once("drain", () => r())); + } + if (outcome === "unknown") { + buffer += decoder.decode(value, { stream: true }).replace(/\r\n/g, "\n").replace(/\r/g, "\n"); + let sep: number; + while ((sep = buffer.indexOf("\n\n")) !== -1) { + const block = buffer.slice(0, sep); + buffer = buffer.slice(sep + 2); + const terminal = sseTerminalOutcome(block); + if (terminal !== "unknown") { + outcome = terminal; + buffer = ""; + break; + } + } + } + } + } finally { + reader.releaseLock(); + res.end(); + } + return outcome; +} + +async function pipeThroughObservingJson(stream: ReadableStream, res: http.ServerResponse): Promise { + const chunks: Buffer[] = []; + const reader = stream.getReader(); + try { + for (;;) { + const { done, value } = await reader.read(); + if (done) break; + const chunk = Buffer.from(value); + chunks.push(chunk); + if (!res.write(chunk)) { + await new Promise((r) => res.once("drain", () => r())); + } + } + } finally { + reader.releaseLock(); + res.end(); + } + try { + JSON.parse(Buffer.concat(chunks).toString("utf8")); + return "completed"; + } catch { + return "unknown"; + } +} + async function dumpStreamToFile(stream: ReadableStream, dir: string, name: string): Promise { const { mkdirSync, createWriteStream } = await import("node:fs"); const { join } = await import("node:path"); @@ -2462,3 +2615,107 @@ function logMsg(opts: ProxyOptions, level: string, msg: string): void { if (!opts.log) return; loggerLog(level, msg); } + +// #320 PR-E1: clamp bili's effective window to codex's own model-table +// knowledge for codex-originated requests (see the call site above). +export function applyCodexWindowClamp( + native: number | undefined, + model: string | undefined, + headers: http.IncomingHttpHeaders, +): { window: number | undefined; clamped: boolean } { + if (!isCodexClient(headers)) return { window: native, clamped: false }; + const codexWindow = codexModelWindow(model); + if (codexWindow !== undefined && (native === undefined || codexWindow < native)) { + return { window: codexWindow, clamped: true }; + } + return { window: native, clamped: false }; +} + +// #320 PR-E2: replay bili-forged compaction items (ids recorded in +// session.metadata.forgedCompactions) as developer messages carrying the ACP +// summary, so the next upstream turn actually sees the summary. Items with +// unknown ids (real server-side compactions) pass through untouched. +export function rewriteForgedCompactions(input: unknown, session: Session): number { + if (!Array.isArray(input)) return 0; + const forged = session.metadata.forgedCompactions as Record | undefined; + if (!forged) return 0; + let count = 0; + for (let i = 0; i < input.length; i++) { + const item = input[i] as { type?: string; id?: string }; + if (item && item.type === "compaction" && item.id && typeof forged[item.id] === "string") { + input[i] = { type: "message", role: "developer", content: forged[item.id] }; + count++; + } + } + return count; +} + +// #320 PR-E2: conditional interception. Forges only when the kill-switch is +// on, the client is codex, the request IS a compaction request (trailing +// compaction_trigger or /responses/compact), and ACP state is healthy +// (steady-state usage below the window AND compression state exists). A +// forged compaction resets the ACP state; the summary travels with the forged +// item id and is replayed on the next request (rewriteForgedCompactions). +async function forgeCodexCompactIfEligible( + req: http.IncomingMessage, + res: http.ServerResponse, + session: Session, + reqConfig: Config, + responsesCompact: boolean, + parsed: unknown, + log: (level: string, msg: string) => void, +): Promise { + if (process.env.BILI_CODEX_COMPACT !== "intercept") return false; + if (!isCodexClient(req.headers)) return false; + const body = parsed as { model?: string; stream?: boolean; input?: unknown }; + const triggerFinal = Array.isArray(body?.input) + && body.input.length > 0 + && (body.input[body.input.length - 1] as { type?: string })?.type === "compaction_trigger"; + if (!responsesCompact && !triggerFinal) return false; + const limit = reqConfig.modelContextLimit; + const steady = session.stats.lastInputTokens < limit; + const hasState = session.state.blocks.length > 0 || session.lastCompress !== undefined; + if (!steady || !hasState) return false; + const summary = session.state.blocks + .map((b) => (b.topic ? `## ${b.topic}\n${b.summary}` : b.summary)) + .join("\n\n"); + if (!summary) return false; + const cmpId = `cmp_${randomUUID()}`; + const respId = `resp_${randomUUID()}`; + await withSessionLock(session, async () => { + resetSessionCompression(session); + const prev = (session.metadata.forgedCompactions as Record | undefined) ?? {}; + const next: Record = { ...prev, [cmpId]: summary }; + const keys = Object.keys(next); + while (keys.length > 32) { + const oldest = keys.shift(); + if (oldest === undefined) break; + delete next[oldest]; + } + session.metadata.forgedCompactions = next; + markDirty(session); + }); + if (body?.stream === true) { + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + res.write(`event: response.output_item.done\ndata: ${JSON.stringify({ + type: "response.output_item.done", + item: { type: "compaction", id: cmpId, encrypted_content: summary }, + })}\n\n`); + res.write(`event: response.completed\ndata: ${JSON.stringify({ + type: "response.completed", + response: { + id: respId, + status: "completed", + model: body.model ?? "", + output: [], + usage: { input_tokens: 1, output_tokens: 0, total_tokens: 1 }, + }, + })}\n\n`); + res.end(); + } else { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ output: [{ type: "compaction", id: cmpId, encrypted_content: summary }] })); + } + log("info", `[${session.id}] codex compaction forged (BILI_CODEX_COMPACT=intercept): ${responsesCompact ? "/responses/compact" : "compaction_trigger"} → ${summary.length} chars summary, ACP state reset`); + return true; +} diff --git a/tests/codex-compact-coordination.test.ts b/tests/codex-compact-coordination.test.ts new file mode 100644 index 0000000000..32a18dc86a --- /dev/null +++ b/tests/codex-compact-coordination.test.ts @@ -0,0 +1,551 @@ +import assert from "node:assert/strict"; +import fs from "node:fs"; +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 { resolveStaticWindow, type ProviderRoutes } from "../src/config.ts"; +import { codexModelWindow, isCodexClient } from "../src/codex-models.ts"; +import { codexBudgetArgs } from "../src/launcher.ts"; +import type { ClientConfig } from "../src/client-config.ts"; +import { applyCodexWindowClamp, rewriteForgedCompactions, startServer, type ProxyOptions } from "../src/server.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { getSession, peekSession, _resetSessionsForTest } from "../src/session.ts"; + +// #320: codex compression-budget coordination (PR-D launcher injection, +// PR-E1 window clamp, PR-C semantic rebasing gate, PR-E2 conditional +// interception + forge). + +const CODEX_HEADERS: Record = { "x-codex-turn-metadata": "sess=1;thread=1;turn=1" }; + +function makeBlock(blockId: string, summary: string, topic?: string) { + return { + blockId, + runId: "r1", + tier: 1 as const, + ...(topic ? { topic } : {}), + summary, + directMessageIds: [], + effectiveMessageIds: [], + directBlockIds: [], + compressedTokens: 1000, + createdAt: Date.now(), + survivedCount: 0, + generation: "young" as const, + active: true, + }; +} + +test("snapshot: src/codex-models-snapshot.json ships a usable model table", () => { + const snap = JSON.parse(fs.readFileSync(new URL("../src/codex-models-snapshot.json", import.meta.url), "utf8")) as { + models: Record; + }; + assert.ok(snap.models && Object.keys(snap.models).length > 0, "snapshot must ship at least one model"); + for (const [slug, entry] of Object.entries(snap.models)) { + const w = entry.context_window ?? entry.max_context_window; + assert.ok(typeof w === "number" && w > 0, `model ${slug} must carry a positive window`); + } + assert.ok(fs.existsSync(new URL("../scripts/update-codex-models-snapshot.mjs", import.meta.url)), "refresh script must exist"); + const pkg = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf8")) as { scripts: Record }; + assert.ok(pkg.scripts["codex:snapshot"], "package.json must wire the codex:snapshot script"); +}); + +test("codexModelWindow: in-table / out-of-table / undefined", () => { + assert.equal(codexModelWindow("gpt-5.5"), 272_000); + assert.equal(codexModelWindow("gpt-5.4"), 272_000); + assert.equal(codexModelWindow("qwen3.8-27b"), undefined); + assert.equal(codexModelWindow(""), undefined); + assert.equal(codexModelWindow(undefined), undefined); +}); + +test("isCodexClient: keyed on x-codex-turn-metadata presence", () => { + assert.equal(isCodexClient(CODEX_HEADERS), true); + assert.equal(isCodexClient({}), false); + assert.equal(isCodexClient({ "x-session-id": "x" }), false); +}); + +test("applyCodexWindowClamp: in-table clamp / user-override / out-of-table / non-codex / native missing", () => { + assert.deepEqual(applyCodexWindowClamp(1_000_000, "gpt-5.5", CODEX_HEADERS), { window: 272_000, clamped: true }); + assert.deepEqual(applyCodexWindowClamp(200_000, "gpt-5.5", CODEX_HEADERS), { window: 200_000, clamped: false }); + assert.deepEqual(applyCodexWindowClamp(1_000_000, "qwen3.8-27b", CODEX_HEADERS), { window: 1_000_000, clamped: false }); + assert.deepEqual(applyCodexWindowClamp(1_000_000, "gpt-5.5", {}), { window: 1_000_000, clamped: false }); + assert.deepEqual(applyCodexWindowClamp(undefined, "gpt-5.5", CODEX_HEADERS), { window: 272_000, clamped: true }); + assert.deepEqual(applyCodexWindowClamp(undefined, "qwen3.8-27b", CODEX_HEADERS), { window: undefined, clamped: false }); +}); + +test("resolveStaticWindow: source precedence launcher > registry > config > table", () => { + setRegistryForTest({ "reg-model": { limit: { context: 200_000 } } }); + const routes = { "http://r.example": { models: { "cfg-model": { context: 150_000 } } } } as unknown as ProviderRoutes; + assert.deepEqual(resolveStaticWindow("any-model", undefined, routes, undefined, { "any-model": 999 }), { window: 999, source: "launcher" }); + assert.deepEqual(resolveStaticWindow("reg-model", "h.example", routes, undefined, undefined), { window: 200_000, source: "registry" }); + assert.deepEqual(resolveStaticWindow("cfg-model", undefined, routes, "http://r.example/v1", undefined), { window: 150_000, source: "config" }); + assert.deepEqual(resolveStaticWindow("claude-opus-4", undefined, {}, undefined, undefined), { window: 200_000, source: "table" }); + assert.equal(resolveStaticWindow("zzz-unknown-model", undefined, {}, undefined, undefined), undefined); + assert.equal(resolveStaticWindow(undefined, undefined, {}, undefined, undefined), undefined); +}); + +test("codexBudgetArgs: no codex model → no args", () => { + assert.deepEqual(codexBudgetArgs({} as ClientConfig), []); + assert.deepEqual(codexBudgetArgs({ codex: {} } as ClientConfig), []); +}); + +test("codexBudgetArgs: min(bili window, codex table) — in-table model", () => { + const cfg: ClientConfig = { + codex: { + model: "gpt-5.5", + modelProvider: "prov", + providers: { prov: { baseUrl: "http://127.0.0.1:9999/v1" } }, + modelWindows: [{ id: "gpt-5.5", contextWindow: 500_000 }], + }, + }; + assert.deepEqual(codexBudgetArgs(cfg), ["-c", "model_context_window=272000", "-c", "model_auto_compact_token_limit=272000"]); +}); + +test("codexBudgetArgs: user override below codex table wins", () => { + const cfg: ClientConfig = { + codex: { + model: "gpt-5.5", + providers: { prov: { baseUrl: "http://127.0.0.1:9999/v1" } }, + modelWindows: [{ id: "gpt-5.5", contextWindow: 200_000 }], + }, + }; + assert.deepEqual(codexBudgetArgs(cfg), ["-c", "model_context_window=200000", "-c", "model_auto_compact_token_limit=200000"]); +}); + +test("codexBudgetArgs: model outside codex table → bili window as-is", () => { + const cfg: ClientConfig = { + codex: { + model: "qwen3.8-27b", + openaiBaseUrl: "http://127.0.0.1:9999/v1", + modelWindows: [{ id: "qwen3.8-27b", contextWindow: 262_144 }], + }, + }; + assert.deepEqual(codexBudgetArgs(cfg), ["-c", "model_context_window=262144", "-c", "model_auto_compact_token_limit=262144"]); +}); + +test("rewriteForgedCompactions: forged id → developer message; unknown id untouched", () => { + _setStoreForTest(new SessionStore({ enabled: false })); + _resetSessionsForTest(); + const s = getSession("t-forge-rewrite", { protocol: "responses" }); + s.metadata.forgedCompactions = { cmp_1: "SUMMARY-TEXT" }; + const input: unknown[] = [ + { type: "message", id: "m1", role: "user", content: "hi" }, + { type: "compaction", id: "cmp_1", encrypted_content: "whatever" }, + { type: "compaction", id: "cmp_unknown", encrypted_content: "real" }, + ]; + const n = rewriteForgedCompactions(input, s); + assert.equal(n, 1); + assert.deepEqual(input[1], { type: "message", role: "developer", content: "SUMMARY-TEXT" }); + assert.equal((input[2] as { id: string }).id, "cmp_unknown"); +}); + +test("rewriteForgedCompactions: non-array / no map → 0", () => { + _setStoreForTest(new SessionStore({ enabled: false })); + _resetSessionsForTest(); + const s = getSession("t-forge-rewrite2", { protocol: "responses" }); + assert.equal(rewriteForgedCompactions("nope", s), 0); + assert.equal(rewriteForgedCompactions([{ type: "compaction", id: "x" }], s), 0); +}); + +type Seen = { url: string; body: any; headers: http.IncomingHttpHeaders }[]; + +function startUpstream(handler: (body: any, url: string, res: http.ServerResponse) => void): Promise<{ port: number; seen: Seen; close: () => Promise }> { + const seen: Seen = []; + 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"); + const body = raw ? JSON.parse(raw) : undefined; + seen.push({ url: req.url ?? "", body, headers: req.headers }); + handler(body, req.url ?? "", res); + }); + }); + return new Promise((resolve) => { + upstream.listen(0, "127.0.0.1", () => { + resolve({ + port: (upstream.address() as { port: number }).port, + seen, + close: () => new Promise((res, rej) => { upstream.close((e) => (e ? rej(e) : res())); }), + }); + }); + }); +} + +function sseEvent(type: string, data: unknown): string { + return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; +} + +function responsesCompletedSse(usage: { input_tokens: number; output_tokens: number; total_tokens: number }): string { + return sseEvent("response.completed", { + type: "response.completed", + response: { id: "resp_up_1", status: "completed", output: [], usage }, + }); +} + +async function startProxy(t: test.TestContext, modelContextLimit: number): Promise<{ url: (p: string) => string; close: () => Promise }> { + _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: {}, + modelContextLimit, + kernelConfig: defaultConfig(modelContextLimit), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-session-id", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const port = (proxy.address() as { port: number }).port; + return { url: (p: string) => `http://127.0.0.1:${port}/bili/${p}`, close: () => new Promise((res, rej) => { proxy.close((e) => (e ? rej(e) : res())); }) }; +} + +async function post(url: string, body: unknown, sessionId: string, extra?: Record): Promise<{ status: number; text: string }> { + const r = await fetch(url, { + method: "POST", + headers: { "content-type": "application/json", "x-session-id": sessionId, ...extra }, + body: JSON.stringify(body), + }); + return { status: r.status, text: await r.text() }; +} + +function parseSse(text: string): { type: string; data: any }[] { + const out: { type: string; data: any }[] = []; + for (const block of text.split("\n\n")) { + const ev = block.match(/^event: (.+)$/m); + const data = block.match(/^data: (.+)$/m); + if (ev && data) { + try { out.push({ type: ev[1]!.trim(), data: JSON.parse(data[1]!) }); } catch { /* keep only well-formed events */ } + } + } + return out; +} + +test("e2e PR-E2 kill-switch pass (default): compaction request reaches upstream unmodified", async (t) => { + const upstream = await startUpstream((_b, _u, res) => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(responsesCompletedSse({ input_tokens: 100, output_tokens: 5, total_tokens: 105 })); + }); + const proxy = await startProxy(t, 200_000); + const url = proxy.url(`http://127.0.0.1:${upstream.port}/v1/responses`); + + const r1 = await post(url, { model: "gpt-5.5", stream: true, input: [{ type: "message", role: "user", content: "hello" }] }, "cs-pass", CODEX_HEADERS); + assert.equal(r1.status, 200); + + const r2 = await post(url, { + model: "gpt-5.5", + stream: true, + input: [{ type: "message", role: "user", content: "hello" }, { type: "message", role: "assistant", content: "ok" }, { type: "compaction_trigger" }], + }, "cs-pass", CODEX_HEADERS); + assert.equal(r2.status, 200); + + assert.equal(upstream.seen.length, 2, "both turns must reach the upstream in pass mode"); + const lastInput = upstream.seen[1]!.body.input as { type?: string }[]; + assert.equal(lastInput[lastInput.length - 1]!.type, "compaction_trigger", "read-only projection keeps the trigger final"); + assert.equal(peekSession("cs-pass")?.metadata.forgedCompactions, undefined, "no forge records in pass mode"); + await proxy.close(); + await upstream.close(); +}); + +test("e2e PR-E2 intercept + healthy ACP: V2 trigger request forged, upstream untouched, summary replayed next turn", async (t) => { + process.env.BILI_CODEX_COMPACT = "intercept"; + t.after(() => { delete process.env.BILI_CODEX_COMPACT; }); + const upstream = await startUpstream((_b, _u, res) => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(responsesCompletedSse({ input_tokens: 100, output_tokens: 5, total_tokens: 105 })); + }); + const proxy = await startProxy(t, 200_000); + const url = proxy.url(`http://127.0.0.1:${upstream.port}/v1/responses`); + + const session = getSession("forge-v2", { protocol: "responses" }); + session.stats.lastInputTokens = 1000; + session.state.blocks = [makeBlock("b1", "SUMMARY-ONE"), makeBlock("b2", "SUMMARY-TWO", "topic-two")]; + + const r = await post(url, { + model: "gpt-5.5", + stream: true, + input: [{ type: "message", role: "user", content: "go" }, { type: "compaction_trigger" }], + }, "forge-v2", CODEX_HEADERS); + assert.equal(r.status, 200); + assert.equal(upstream.seen.length, 0, "intercepted request must NOT reach the upstream"); + + const events = parseSse(r.text); + const done = events.find((e) => e.type === "response.output_item.done"); + assert.ok(done, "forged stream must carry response.output_item.done"); + assert.equal(done!.data.item.type, "compaction"); + assert.equal(done!.data.item.encrypted_content, "SUMMARY-ONE\n\n## topic-two\nSUMMARY-TWO"); + assert.ok(done!.data.item.id.startsWith("cmp_")); + const completed = events.find((e) => e.type === "response.completed"); + assert.ok(completed, "forged stream must end with response.completed"); + assert.ok(completed!.data.response.id.startsWith("resp_")); + assert.ok(completed!.data.response.usage.total_tokens > 0); + + const cmpId: string = done!.data.item.id; + const after = peekSession("forge-v2")!; + assert.equal(after.state.blocks.length, 0, "ACP state must be reset by the forge"); + const forged = after.metadata.forgedCompactions as Record; + assert.equal(forged[cmpId], "SUMMARY-ONE\n\n## topic-two\nSUMMARY-TWO"); + + const r3 = await post(url, { + model: "gpt-5.5", + stream: true, + input: [ + { type: "compaction", id: cmpId, encrypted_content: "SUMMARY-ONE\n\n## topic-two\nSUMMARY-TWO" }, + { type: "message", role: "user", content: "next" }, + ], + }, "forge-v2", CODEX_HEADERS); + assert.equal(r3.status, 200); + assert.equal(upstream.seen.length, 1, "the follow-up turn reaches the upstream"); + const fwd = upstream.seen[0]!.body.input as { type?: string; role?: string; content?: string }[]; + assert.ok(!fwd.some((i) => i.type === "compaction"), "forged compaction item must not be forwarded"); + const dev = fwd.find((i) => i.role === "developer"); + assert.ok(dev, "summary must be replayed as a developer message"); + assert.ok(dev!.content.includes("SUMMARY-ONE\n\n## topic-two\nSUMMARY-TWO"), "summary must be carried by the replayed developer message"); + await proxy.close(); + await upstream.close(); +}); + +test("e2e PR-E2 intercept + healthy ACP: V1 /responses/compact forged as JSON", async (t) => { + process.env.BILI_CODEX_COMPACT = "intercept"; + t.after(() => { delete process.env.BILI_CODEX_COMPACT; }); + const upstream = await startUpstream((_b, _u, res) => { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ output: [] })); + }); + const proxy = await startProxy(t, 200_000); + const url = compactUrl(proxy, upstream.port); + + const session = getSession("forge-v1", { protocol: "responses" }); + session.stats.lastInputTokens = 1000; + session.state.blocks = [makeBlock("b1", "V1-SUMMARY")]; + + const r = await post(url, { model: "gpt-5.5", input: [{ type: "message", role: "user", content: "go" }] }, "forge-v1", CODEX_HEADERS); + assert.equal(r.status, 200); + assert.equal(upstream.seen.length, 0, "intercepted compact request must NOT reach the upstream"); + const body = JSON.parse(r.text) as { output: { type: string; id: string; encrypted_content: string }[] }; + assert.equal(body.output.length, 1); + assert.equal(body.output[0]!.type, "compaction"); + assert.equal(body.output[0]!.encrypted_content, "V1-SUMMARY"); + assert.ok(body.output[0]!.id.startsWith("cmp_")); + await proxy.close(); + await upstream.close(); +}); + +test("e2e PR-E2 intercept + unhealthy ACP (no state): passes through", async (t) => { + process.env.BILI_CODEX_COMPACT = "intercept"; + t.after(() => { delete process.env.BILI_CODEX_COMPACT; }); + const upstream = await startUpstream((_b, _u, res) => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(responsesCompletedSse({ input_tokens: 100, output_tokens: 5, total_tokens: 105 })); + }); + const proxy = await startProxy(t, 200_000); + const url = proxy.url(`http://127.0.0.1:${upstream.port}/v1/responses`); + + const r = await post(url, { + model: "gpt-5.5", + stream: true, + input: [{ type: "message", role: "user", content: "go" }, { type: "compaction_trigger" }], + }, "forge-unhealthy", CODEX_HEADERS); + assert.equal(r.status, 200); + assert.equal(upstream.seen.length, 1, "unhealthy ACP state must fall through to native compaction"); + await proxy.close(); + await upstream.close(); +}); + +test("e2e PR-E2 intercept + over-limit steady state: passes through", async (t) => { + process.env.BILI_CODEX_COMPACT = "intercept"; + t.after(() => { delete process.env.BILI_CODEX_COMPACT; }); + const upstream = await startUpstream((_b, _u, res) => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(responsesCompletedSse({ input_tokens: 100, output_tokens: 5, total_tokens: 105 })); + }); + const proxy = await startProxy(t, 200_000); + const url = proxy.url(`http://127.0.0.1:${upstream.port}/v1/responses`); + + const session = getSession("forge-over", { protocol: "responses" }); + session.stats.lastInputTokens = 200_001; + session.state.blocks = [makeBlock("b1", "S")]; + + const r = await post(url, { + model: "zzz-not-in-codex-table", + stream: true, + input: [{ type: "message", role: "user", content: "go" }, { type: "compaction_trigger" }], + }, "forge-over", CODEX_HEADERS); + assert.equal(r.status, 200); + assert.equal(upstream.seen.length, 1, "over-limit steady state must fall through to native compaction"); + await proxy.close(); + await upstream.close(); +}); + +test("e2e PR-E2 intercept + non-codex client: passes through", async (t) => { + process.env.BILI_CODEX_COMPACT = "intercept"; + t.after(() => { delete process.env.BILI_CODEX_COMPACT; }); + const upstream = await startUpstream((_b, _u, res) => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(responsesCompletedSse({ input_tokens: 100, output_tokens: 5, total_tokens: 105 })); + }); + const proxy = await startProxy(t, 200_000); + const url = proxy.url(`http://127.0.0.1:${upstream.port}/v1/responses`); + + const session = getSession("forge-noncodex", { protocol: "responses" }); + session.stats.lastInputTokens = 1000; + session.state.blocks = [makeBlock("b1", "S")]; + + const r = await post(url, { + model: "gpt-5.5", + stream: true, + input: [{ type: "message", role: "user", content: "go" }, { type: "compaction_trigger" }], + }, "forge-noncodex"); + assert.equal(r.status, 200); + assert.equal(upstream.seen.length, 1, "non-codex clients are never intercepted"); + await proxy.close(); + await upstream.close(); +}); + +function compactUrl(proxy: { url: (p: string) => string }, port: number): string { + return proxy.url(`http://127.0.0.1:${port}/v1/responses/compact`); +} + +async function runCompactOutcome(t: test.TestContext, upstreamPort: number, sessionId: string, sse: string, emptyBody = false): Promise { + const upstream = await startUpstream((_b, _u, res) => { + if (emptyBody) { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(); + } else { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(sse); + } + }); + const proxy = await startProxy(t, 200_000); + const r = await post(compactUrl(proxy, upstreamPort), { model: "gpt-5.5", stream: true, input: [{ type: "message", role: "user", content: "go" }] }, sessionId); + assert.equal(r.status, 200); + await proxy.close(); + await upstream.close(); + return Boolean(peekSession(sessionId)?.metadata.nativeCompactionBoundary); +} + +test("e2e PR-C: SSE response.completed → rebase scheduled", async (t) => { + const upstream = await startUpstream((_b, _u, res) => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(responsesCompletedSse({ input_tokens: 10, output_tokens: 5, total_tokens: 15 })); + }); + const proxy = await startProxy(t, 200_000); + const r = await post(compactUrl(proxy, upstream.port), { model: "gpt-5.5", stream: true, input: [{ type: "message", role: "user", content: "go" }] }, "pc-completed"); + assert.equal(r.status, 200); + assert.ok(peekSession("pc-completed")?.metadata.nativeCompactionBoundary, "completed → rebase scheduled"); + await proxy.close(); + await upstream.close(); +}); + +test("e2e PR-C: SSE response.failed → NOT rebased", async (t) => { + const upstream = await startUpstream((_b, _u, res) => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(sseEvent("response.failed", { type: "response.failed", response: { id: "resp_x", error: { code: "boom", message: "nope" } } })); + }); + const proxy = await startProxy(t, 200_000); + const r = await post(compactUrl(proxy, upstream.port), { model: "gpt-5.5", stream: true, input: [{ type: "message", role: "user", content: "go" }] }, "pc-failed"); + assert.equal(r.status, 200); + assert.equal(peekSession("pc-failed")?.metadata.nativeCompactionBoundary, undefined, "failed compaction must not rebase ACP state"); + await proxy.close(); + await upstream.close(); +}); + +test("e2e PR-C: SSE response.incomplete → NOT rebased", async (t) => { + const upstream = await startUpstream((_b, _u, res) => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(sseEvent("response.incomplete", { type: "response.incomplete", response: { id: "resp_x", incomplete_details: { reason: "max_output_tokens" } } })); + }); + const proxy = await startProxy(t, 200_000); + const r = await post(compactUrl(proxy, upstream.port), { model: "gpt-5.5", stream: true, input: [{ type: "message", role: "user", content: "go" }] }, "pc-incomplete"); + assert.equal(r.status, 200); + assert.equal(peekSession("pc-incomplete")?.metadata.nativeCompactionBoundary, undefined, "incomplete compaction must not rebase ACP state"); + await proxy.close(); + await upstream.close(); +}); + +test("e2e PR-C: truncated SSE (no terminal event) → NOT rebased", async (t) => { + const upstream = await startUpstream((_b, _u, res) => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(sseEvent("response.output_item.done", { type: "response.output_item.done", item: { type: "message", role: "assistant", content: "partial" } })); + }); + const proxy = await startProxy(t, 200_000); + const r = await post(compactUrl(proxy, upstream.port), { model: "gpt-5.5", stream: true, input: [{ type: "message", role: "user", content: "go" }] }, "pc-truncated"); + assert.equal(r.status, 200); + assert.equal(peekSession("pc-truncated")?.metadata.nativeCompactionBoundary, undefined, "truncated stream → unknown outcome → no rebase"); + await proxy.close(); + await upstream.close(); +}); + +test("e2e PR-C: empty-body 200 → NOT rebased", async (t) => { + const upstream = await startUpstream((_b, _u, res) => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(); + }); + const proxy = await startProxy(t, 200_000); + const r = await post(compactUrl(proxy, upstream.port), { model: "gpt-5.5", stream: true, input: [{ type: "message", role: "user", content: "go" }] }, "pc-empty"); + assert.equal(r.status, 200); + assert.equal(peekSession("pc-empty")?.metadata.nativeCompactionBoundary, undefined, "empty body → unknown outcome → no rebase"); + await proxy.close(); + await upstream.close(); +}); + +test("e2e PR-E1: codex client window clamped to codex table → nudge fires earlier than control", async (t) => { + const upstream = await startUpstream((_b, _u, res) => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(responsesCompletedSse({ input_tokens: 219_000, output_tokens: 1_000, total_tokens: 220_000 })); + }); + const proxy = await startProxy(t, 1_000_000); + const url = proxy.url(`http://127.0.0.1:${upstream.port}/v1/responses`); + + const longText = "x".repeat(20_000); + const filler: { type: string; role: "user" | "assistant"; content: string }[] = []; + for (let i = 1; i <= 12; i++) { + filler.push({ type: "message", role: "user", content: `q${i} ` + "f".repeat(997) }); + filler.push({ type: "message", role: "assistant", content: `a${i} ` + "e".repeat(997) }); + } + const turn2 = { + model: "gpt-5.5", + stream: true, + input: [ + { type: "message", role: "user", content: "hello" }, + { type: "message", role: "assistant", content: "ok" }, + { type: "message", role: "user", content: "continue" }, + { type: "message", role: "assistant", content: longText }, + ...filler, + { type: "message", role: "user", content: "now summarize" }, + ], + }; + + // Codex client: bili window clamped 1M → 272k (codex table), 220k/272k = 81% > 75% → nudge. + await post(url, { model: "gpt-5.5", stream: true, input: [{ type: "message", role: "user", content: "hello" }] }, "e1-codex", CODEX_HEADERS); + const rc = await post(url, turn2, "e1-codex", CODEX_HEADERS); + assert.equal(rc.status, 200); + + // Control: no codex header → window stays 1M (table gpt-5 → 400k, still far above 220k/75%) → no nudge. + await post(url, { model: "gpt-5.5", stream: true, input: [{ type: "message", role: "user", content: "hello" }] }, "e1-control"); + const rr = await post(url, turn2, "e1-control"); + assert.equal(rr.status, 200); + + assert.equal(upstream.seen.length, 4); + const codexFwd = upstream.seen[2]!.body.input as { role?: string; content?: string }[]; + const controlFwd = upstream.seen[3]!.body.input as { role?: string; content?: string }[]; + const codexLast = codexFwd[codexFwd.length - 1]!; + const controlLast = controlFwd[controlFwd.length - 1]!; + assert.ok(typeof controlLast.content === "string" && controlLast.content.endsWith("now summarize"), "control (no clamp) must not nudge at 55% of 400k"); + assert.notEqual(codexLast.content, "now summarize", "clamped codex window (272k) must nudge at 81%"); + assert.equal(codexLast.role, "user"); + await proxy.close(); + await upstream.close(); +});