Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
88 changes: 88 additions & 0 deletions scripts/update-codex-models-snapshot.mjs
Original file line number Diff line number Diff line change
@@ -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)`);
57 changes: 57 additions & 0 deletions src/codex-models-snapshot.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
107 changes: 107 additions & 0 deletions src/codex-models.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>): 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<string, unknown>,
): { 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]);
}
16 changes: 16 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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));
}
}
Expand Down
82 changes: 82 additions & 0 deletions tests/codex-models.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
Loading
Loading