From 50637d2c102de2c20f2c15b900972d41b66b698a Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 31 Oct 2026 06:04:37 +0800 Subject: [PATCH 1/2] fix: read Codex model_catalog_json so custom-catalog installs stop falling back to 272K (#5246) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Codex resolves its live model table from exactly one path, /models_cache.json. On installs driven by a custom catalog (config.toml `model_catalog_json = ...`) that file does not exist, so bili fell through to the bundled snapshot (no entry) and the flat 272K floor, clamping the effective window far below what codex actually perceives and triggering emergency nudges at ~35% of the real window. - Parse `model_catalog_json` from codex's own config.toml and resolve the live table from that path when set, else models_cache.json (relative paths resolve against CODEX_HOME). - When neither file is readable but config.toml sets `model_context_window`, size from that value capped by the known max_context_window (the bundled entry's max, or the 272K unknown-model ceiling) instead of the flat 272K floor — never budgeting above what codex perceives — and log a warn naming the degraded path. - Name the winning window source (client-report/catalog-json/models-cache/config-context-window/bundled-snapshot/flat-fallback) in the clamp log line instead of the misleading "current client metadata preferred" text. Local codex peers on such installs now get the real perceived window (toward larger, never below codex perception); remote peers, non-codex clients, operator modelContextLimit, min() semantics and output reservation are unchanged. Closes #5246 --- src/client-config.ts | 5 ++ src/codex-models.ts | 109 ++++++++++++++++++++++++++++---- src/server/handle.ts | 19 ++++-- tests/codex-live-window.test.ts | 54 +++++++++++++++- 4 files changed, 167 insertions(+), 20 deletions(-) diff --git a/src/client-config.ts b/src/client-config.ts index 0fe470dda9..fd63f9ffa5 100644 --- a/src/client-config.ts +++ b/src/client-config.ts @@ -60,6 +60,8 @@ export interface CodexConfig { model?: string; /** Top-level `model_context_window` override (if set). */ contextWindow?: number; + /** Top-level `model_catalog_json` — path to the live model catalog (#5246). */ + modelCatalogJson?: string; /** Top-level `model_auto_compact_token_limit` override (if set). */ autoCompactLimit?: number; /** Top-level `model_max_output_tokens` override (if set) (#971). */ @@ -698,6 +700,7 @@ export function parseCodexToml(text: string): CodexConfig { let curProvider: string | null = null; let codexModel: string | undefined; let codexContextWindow: number | undefined; + let codexCatalogJson: string | undefined; let codexAutoCompactLimit: number | undefined; let codexMaxOutput: number | undefined; for (const rawLine of text.split(/\r?\n/)) { @@ -723,6 +726,7 @@ export function parseCodexToml(text: string): CodexConfig { 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_catalog_json") codexCatalogJson = val; } else if (curProvider && key === "base_url") { result.providers[curProvider].baseUrl = val; } @@ -734,6 +738,7 @@ export function parseCodexToml(text: string): CodexConfig { } if (codexModel) result.model = codexModel; if (codexContextWindow) result.contextWindow = codexContextWindow; + if (codexCatalogJson) result.modelCatalogJson = codexCatalogJson; if (codexAutoCompactLimit) result.autoCompactLimit = codexAutoCompactLimit; if (codexMaxOutput) result.maxOutput = codexMaxOutput; const win = toModelWindow(codexModel, codexContextWindow, codexMaxOutput); diff --git a/src/codex-models.ts b/src/codex-models.ts index 1cf33e8664..743303675a 100644 --- a/src/codex-models.ts +++ b/src/codex-models.ts @@ -80,21 +80,41 @@ export function codexWindowForModel(model: string): number { return entry ? resolvedWindow(entry) : CODEX_FALLBACK_CONTEXT_WINDOW; } +/** Which source determined a codex client's aligned window — surfaced in the + * clamp/warn log lines so triage sees what actually won (#5246 piece 3). */ +export type CodexWindowSource = + | "client-report" // a fresher client report (plugin/runtime/launcher) + | "catalog-json" // config.toml model_catalog_json live table + | "models-cache" // /models_cache.json live table + | "config-context-window" // no live table — sized from config.toml model_context_window + | "bundled-snapshot" // release-time bundled model table entry + | "flat-fallback"; // CODEX_FALLBACK_CONTEXT_WINDOW (unknown model) + +interface LiveResolution { + window: number; + source: "catalog-json" | "models-cache" | "config-context-window"; +} + let liveTable: { key: string; models: CodexModelEntry[] } | undefined; function positiveWindow(value: unknown): number | undefined { return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : undefined; } -/** 本机 Codex 的缓存比发布时的模型表更新;只读模型元数据,不读取认证或会话。 */ -export function readCodexModelWindow(model: string, codexHome: string): number | undefined { - const file = path.join(codexHome, "models_cache.json"); +function nonEmptyString(value: unknown): value is string { + return typeof value === "string" && value.length > 0; +} + +/** Parse ONE live codex model-table file into slim entries. Returns null when + * the file is absent/unreadable/malformed — leaving any prior cached table + * intact so a transient bad write never reuses stale entries (#2593). */ +function parseLiveTable(file: string): CodexModelEntry[] | null { try { const stat = fs.statSync(file); const key = `${file}|${stat.ino}|${stat.mtimeMs}|${stat.ctimeMs}|${stat.size}`; if (liveTable?.key !== key) { const parsed: unknown = JSON.parse(fs.readFileSync(file, "utf8")); - if (!parsed || typeof parsed !== "object" || !("models" in parsed) || !Array.isArray(parsed.models)) return undefined; + if (!parsed || typeof parsed !== "object" || !("models" in parsed) || !Array.isArray(parsed.models)) return null; const models: CodexModelEntry[] = []; for (const value of parsed.models) { if (!value || typeof value !== "object" || typeof value.slug !== "string" || !value.slug) continue; @@ -107,26 +127,87 @@ export function readCodexModelWindow(model: string, codexHome: string): number | } liveTable = { key, models }; } - const entry = lookupModel(model, liveTable.models); - if (!entry) return undefined; - const configured = positiveWindow(readCodexConfig(codexHome).contextWindow); - const window = configured === undefined ? resolvedWindow(entry) : Math.min(configured, entry.maxContextWindow ?? configured); - return Math.floor(window * (entry.effectiveContextWindowPercent ?? 95) / 100); + return liveTable.models; } catch { - return undefined; + return null; + } +} + +/** The running codex's perceived window for a model, mirroring codex's own + * budget math. The live table beats the release-time snapshot — resolved from + * config.toml `model_catalog_json` when set, else `/models_cache.json` + * (#5246: custom-catalog installs have NO models_cache.json, which is why they + * fell through to the flat 272K floor). A base `model_context_window` override + * caps the entry's max_context_window. When NO live table is readable but + * config.toml declares model_context_window, size from that capped by the + * codex-known ceiling (bundled max_context_window, else the 272K unknown-model + * fallback) — never above what codex itself would perceive (#292 discipline). + * Read-only model metadata; never touches auth/sessions. */ +function resolveLiveCodexWindow(model: string, codexHome: string): LiveResolution | null { + const cfg = readCodexConfig(codexHome); + const catalogRaw = cfg.modelCatalogJson; + const catalogPath = nonEmptyString(catalogRaw) + ? path.resolve(codexHome, catalogRaw) + : path.join(codexHome, "models_cache.json"); + const catalogSource: "catalog-json" | "models-cache" = nonEmptyString(catalogRaw) ? "catalog-json" : "models-cache"; + const models = parseLiveTable(catalogPath); + if (models) { + const entry = lookupModel(model, models); + if (entry) { + const configured = positiveWindow(cfg.contextWindow); + const window = configured === undefined ? resolvedWindow(entry) : Math.min(configured, entry.maxContextWindow ?? configured); + return { window: Math.floor(window * (entry.effectiveContextWindowPercent ?? 95) / 100), source: catalogSource }; + } + } + const configured = positiveWindow(cfg.contextWindow); + if (configured !== undefined) { + const bundled = lookupModel(model, TABLE); + const ceiling = bundled?.maxContextWindow ?? CODEX_FALLBACK_CONTEXT_WINDOW; + const total = Math.min(configured, ceiling); + return { window: Math.floor(total * (bundled?.effectiveContextWindowPercent ?? 95) / 100), source: "config-context-window" }; + } + return null; +} + +/** 本机 Codex 的活表窗口(含 config.toml 兜底);只读模型元数据,不读取认证或会话。 */ +export function readCodexModelWindow(model: string, codexHome: string): number | undefined { + return resolveLiveCodexWindow(model, codexHome)?.window; +} + +/** Detailed alignment: the aligned limit PLUS the source that won, so the + * caller can log what actually determined the value (#5246 piece 3). */ +export function codexAlignedWindowDetailed( + limit: number, + model: string, + headers: Record, + clientWindow?: number, +): { limit: number; clamped: boolean; source: CodexWindowSource | null } { + if (!isCodexClient(headers)) return { limit, clamped: false, source: null }; + const cw = positiveWindow(clientWindow); + if (cw !== undefined) { + const clamped = limit > cw; + return { limit: clamped ? cw : limit, clamped, source: "client-report" }; + } + const live = resolveLiveCodexWindow(model, resolveCodexHome(process.env)); + if (live) { + const clamped = limit > live.window; + return { limit: clamped ? live.window : limit, clamped, source: live.source }; } + const bundled = lookupModel(model, TABLE); + const w = bundled ? resolvedWindow(bundled) : CODEX_FALLBACK_CONTEXT_WINDOW; + const clamped = limit > w; + return { limit: clamped ? w : limit, clamped, source: bundled ? "bundled-snapshot" : "flat-fallback" }; } +/** Backward-compatible shape (pre-#5246): the aligned limit + clamp flag only. */ export function codexAlignedWindow( limit: number, model: string, headers: Record, clientWindow?: number, ): { limit: number; clamped: boolean } { - if (!isCodexClient(headers)) return { limit, clamped: false }; - const w = positiveWindow(clientWindow) ?? readCodexModelWindow(model, resolveCodexHome(process.env)) ?? codexWindowForModel(model); - if (limit > w) return { limit: w, clamped: true }; - return { limit, clamped: false }; + const d = codexAlignedWindowDetailed(limit, model, headers, clientWindow); + return { limit: d.limit, clamped: d.clamped }; } /** Test hook: replace the bundled table (mirrors registry._setForTest). */ diff --git a/src/server/handle.ts b/src/server/handle.ts index 3c0355e702..e7f2a40f4b 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -12,7 +12,7 @@ import { FALLBACK_EFFECTIVE_WINDOW_FLOOR, findRoute, findRouteKey, lookupContext import { resolveProxyDecision } from "../upstream-proxy.js"; import { contextFromRegistry, peekRegistryContext, peekRegistryOutputLimit, peekRegistryPriceProfile } from "../registry.js"; import { stampBillingPrice } from "../billing.js"; -import { codexAlignedWindow } from "../codex-models.js"; +import { codexAlignedWindowDetailed, type CodexWindowSource } from "../codex-models.js"; import { DecodedRequestAdmission, DecodedRequestBusyError } from "../request-body-budget.js"; import { hostIdForLog, maskHeadersForLog, maskUrlForLog, maskUrlsInText } from "../log-mask.js"; import { bodyMightCarryImages, buildIncomingImageIndex, pruneRetrieveImgExports, refreshIncomingImageIndex, type IndexedImage } from "../image-restore.js"; @@ -99,6 +99,11 @@ function persistLineageLinkNow(session: Session): void { const vertexModelNormalised = new Set(); const VERTEX_MODEL_LOG_CAP = 64; +// #5246: a custom-catalog Codex install with no readable live model table gets +// its window from config.toml model_context_window — flag it once per model so +// triage knows the value is a declared guess, not a verified catalog entry. +const codexConfigFallbackWarned = new Set(); + export async function handle( req: http.IncomingMessage, res: http.ServerResponse, @@ -818,15 +823,19 @@ async function handleRequest( // 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, pluginWindow ?? runtimeWindow ?? launcherWindow); + const aligned: { limit: number; clamped: boolean; source: CodexWindowSource | null } = operatorWindowTuned + ? { limit: reqConfig.modelContextLimit, clamped: false, source: null } + : codexAlignedWindowDetailed(reqConfig.modelContextLimit, model, req.headers, pluginWindow ?? runtimeWindow ?? launcherWindow); + if (aligned.source === "config-context-window" && !codexConfigFallbackWarned.has(model)) { + codexConfigFallbackWarned.add(model); + log("warn", `[codex] model=${model} live model table unavailable (no readable models_cache.json/model_catalog_json) — effective window sized from config.toml model_context_window, not a verified model catalog`); + } if (aligned.clamped) { const before = reqConfig.modelContextLimit; reqConfig = { ...reqConfig, modelContextLimit: aligned.limit }; nativeFromFallback = false; windowShrinkReason = "codex"; - log("info", `[codex] effective window clamped ${before} → ${aligned.limit} (client-window alignment for model=${model}; current client metadata preferred over bundled fallback) upstream=${embeddedUrl ? hostIdForLog(embeddedUrl) : "none"} route=${routeLabel}`); + log("info", `[codex] effective window clamped ${before} → ${aligned.limit} (model=${model}; window source=${aligned.source}) upstream=${embeddedUrl ? hostIdForLog(embeddedUrl) : "none"} route=${routeLabel}`); } else if (operatorWindowTuned && native !== undefined && reqConfig.modelContextLimit < native) { windowShrinkReason = "operator"; } diff --git a/tests/codex-live-window.test.ts b/tests/codex-live-window.test.ts index 3e87a0aa40..421f5b688d 100644 --- a/tests/codex-live-window.test.ts +++ b/tests/codex-live-window.test.ts @@ -6,7 +6,7 @@ import test from "node:test"; import http from "node:http"; import { once } from "node:events"; import { createCore, defaultConfig, defaultPrompts } from "acp-kernel"; -import { codexAlignedWindow, readCodexModelWindow } from "../src/codex-models.ts"; +import { codexAlignedWindow, codexAlignedWindowDetailed, readCodexModelWindow } from "../src/codex-models.ts"; import { reserveOutputHeadroom } from "../src/util.ts"; import { preflightCompress } from "../src/preflight.ts"; import { getSession } from "../src/session.ts"; @@ -156,3 +156,55 @@ test("real preflight: live window admits fixed overhead that the stale fallback await closed; } }); + +test("#5246: model_catalog_json path is consulted when models_cache.json is absent", (t) => { + const home = fixture(t); + const catalog = path.join(home, "model-catalog.json"); + fs.writeFileSync(catalog, JSON.stringify({ models: [{ slug: model, context_window: 272_000, max_context_window: 872_000 }] })); + fs.writeFileSync(path.join(home, "config.toml"), `model_catalog_json = "${catalog}"\nmodel_context_window = 1050000\n`); + assert.equal(fs.existsSync(path.join(home, "models_cache.json")), false); + assert.equal(readCodexModelWindow(model, home), 828_400); +}); + +test("#5246: model_catalog_json outranks models_cache.json when both exist", (t) => { + const home = fixture(t); + const catalog = path.join(home, "custom.json"); + fs.writeFileSync(catalog, JSON.stringify({ models: [{ slug: model, context_window: 300_000 }] })); + fs.writeFileSync(path.join(home, "models_cache.json"), JSON.stringify({ models: [{ slug: model, context_window: 999_000 }] })); + fs.writeFileSync(path.join(home, "config.toml"), `model_catalog_json = "${catalog}"\n`); + assert.equal(readCodexModelWindow(model, home), 285_000); +}); + +test("#5246: config-only fallback caps at the codex-known ceiling, never the raw override", (t) => { + const home = fixture(t); + fs.writeFileSync(path.join(home, "config.toml"), "model_context_window = 1050000\n"); + assert.equal(readCodexModelWindow(model, home), 258_400); + assert.equal(readCodexModelWindow("gpt-5.4", home), 950_000); + assert.equal(readCodexModelWindow("gpt-5.5", home), 258_400); + fs.rmSync(path.join(home, "config.toml")); + assert.equal(readCodexModelWindow(model, home), undefined); +}); + +test("#5246: codexAlignedWindowDetailed names the winning source per rung", (t) => { + const home = fixture(t); + const previous = process.env.CODEX_HOME; + t.after(() => { if (previous === undefined) delete process.env.CODEX_HOME; else process.env.CODEX_HOME = previous; }); + process.env.CODEX_HOME = home; + const nonCodex = { "user-agent": "node-fetch" }; + assert.deepEqual(codexAlignedWindowDetailed(1_050_000, model, codex, 900_000), { limit: 900_000, clamped: true, source: "client-report" }); + const catalog = path.join(home, "cat.json"); + fs.writeFileSync(catalog, JSON.stringify({ models: [{ slug: model, context_window: 300_000 }] })); + fs.writeFileSync(path.join(home, "config.toml"), `model_catalog_json = "${catalog}"\n`); + assert.deepEqual(codexAlignedWindowDetailed(1_050_000, model, codex), { limit: 285_000, clamped: true, source: "catalog-json" }); + fs.rmSync(path.join(home, "config.toml")); + fs.rmSync(catalog); + fs.writeFileSync(path.join(home, "models_cache.json"), JSON.stringify({ models: [{ slug: model, context_window: 300_000 }] })); + assert.deepEqual(codexAlignedWindowDetailed(1_050_000, model, codex), { limit: 285_000, clamped: true, source: "models-cache" }); + fs.rmSync(path.join(home, "models_cache.json")); + fs.writeFileSync(path.join(home, "config.toml"), "model_context_window = 1050000\n"); + assert.deepEqual(codexAlignedWindowDetailed(1_050_000, model, codex), { limit: 258_400, clamped: true, source: "config-context-window" }); + fs.rmSync(path.join(home, "config.toml")); + assert.deepEqual(codexAlignedWindowDetailed(1_050_000, "gpt-5.5", codex), { limit: 272_000, clamped: true, source: "bundled-snapshot" }); + assert.deepEqual(codexAlignedWindowDetailed(1_050_000, "glm-5", codex), { limit: 272_000, clamped: true, source: "flat-fallback" }); + assert.deepEqual(codexAlignedWindowDetailed(1_050_000, "gpt-5.5", nonCodex), { limit: 1_050_000, clamped: false, source: null }); +}); From 8201c681c6de095d7c4dabccea25a5e8520a3c34 Mon Sep 17 00:00:00 2001 From: ework-daemon Date: Sat, 31 Oct 2026 09:36:23 +0800 Subject: [PATCH 2/2] chore: drop dead codexAlignedWindow import from server.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The sole call site moved to src/server/handle.ts when handle() was extracted (e7af74691); the import at server.ts:17 has been unused since. Found while reviewing #5246 — trivial cleanup, no behavior change. --- src/server.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/server.ts b/src/server.ts index a8cbd89bf6..e5bd37617b 100644 --- a/src/server.ts +++ b/src/server.ts @@ -14,7 +14,6 @@ import { loadNamedProviders, loadOptions, loadRoutes } from "./config.js"; import { resetProxyCache } from "./upstream-proxy.js"; import { effectivePromptCacheRouting, FALLBACK_EFFECTIVE_WINDOW_FLOOR, findRoute, findRouteKey, lookupContextLimit, resolveConfiguredContextLimit, resolveConfiguredOutputLimit, resolveCompressProtocol, resolveResignSettings } from "./config.js"; import { contextFromRegistry, loadRegistry, peekRegistryContext, peekRegistryOutputLimit, peekRegistryPriceProfile } from "./registry.js"; -import { codexAlignedWindow } from "./codex-models.js"; import { fetchWithTimeout, upstreamTimeoutMs } from "./fetch-util.js"; import { getUpstreamConnectionStatus, resolveProxy, resolveProxyDecision, proxyDispatcher, type UpstreamProxyDecision } from "./upstream-proxy.js"; import { buildProxyFailureResponse, isExpectedFailFast, type SkippedProxyInfo } from "./upstream-fail.js";