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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: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
86 changes: 86 additions & 0 deletions scripts/update-codex-models-snapshot.mjs
Original file line number Diff line number Diff line change
@@ -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)`);
4 changes: 3 additions & 1 deletion src/client-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, CodexProvider>;
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/codex-models-snapshot.json
Original file line number Diff line number Diff line change
@@ -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}}}
31 changes: 31 additions & 0 deletions src/codex-models.ts
Original file line number Diff line number Diff line change
@@ -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<string, CodexModelEntry> };

/** 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<string, CodexModelEntry> = (() => {
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;
}
30 changes: 30 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<string, number> | 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
Expand Down
26 changes: 25 additions & 1 deletion src/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
Loading
Loading