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
5 changes: 5 additions & 0 deletions src/client-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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). */
Expand Down Expand Up @@ -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/)) {
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down
109 changes: 95 additions & 14 deletions src/codex-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" // <CODEX_HOME>/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;
Expand All @@ -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 `<CODEX_HOME>/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<string, string | string[] | undefined>,
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<string, string | string[] | undefined>,
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). */
Expand Down
1 change: 0 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
19 changes: 14 additions & 5 deletions src/server/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -99,6 +99,11 @@ function persistLineageLinkNow(session: Session): void {
const vertexModelNormalised = new Set<string>();
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<string>();

export async function handle(
req: http.IncomingMessage,
res: http.ServerResponse,
Expand Down Expand Up @@ -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";
}
Expand Down
54 changes: 53 additions & 1 deletion tests/codex-live-window.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 });
});
Loading