Skip to content

fix: size codex unknown-model windows from live CODEX_HOME for local peers (#2593) - #2600

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-10-10_codex-live-window-align
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-10-10_codex-live-window-align

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Summary

Fixes #2593 — a Codex NEW model missing from bili's release-time bundled model table was clamped to the 272K unknown-model fallback even though the running Codex perceives its real (often larger) window, under-clamping long sessions into unnecessary preflight compression and 502s. Existing plugin/runtime/launcher window reports were re-limited by the same stale table.

Root cause

codexWindowForModel() (src/codex-models.ts) resolved a model's perceived window from the bundled snapshot only (src/codex-models-snapshot.json, fetched at release time) plus the 272K floor. It never consulted Codex's own live cache, so any slug not yet in the snapshot fell to 272K regardless of what this Codex actually runs.

Fix

For a LOCAL Codex peer (loopback) with no fresher report, align to this proxy's own CODEX_HOME (read-only): models_cache.json + base config.toml, mirroring Codex's budget math:

total     = base override ? min(override, max_context_window) : resolved_window
perceived = floor(total * effective_percent / 100)      // percent defaults to 100

Remote peers keep the bundled snapshot + 272K fallback — a remote host's cache is not this client's real config.

Verification (the issue's exact scenario)

With models_cache.json = context_window=373000, max_context_window=872000, effective_context_window_percent=95 and base config.toml = model_context_window=1048576:

  • local peer, routed context 1,050,000 → aligned to 828,400 (= min(1048576,872000)*95%); input budget 700,400 after the 128K output reservation — the ~507,906 preflight estimate now passes without a summary call.
  • old behavior: 272,000 → 204,000 budget → preflight 502.

Verified end-to-end against a real temp CODEX_HOME (disk-load path), not just injected fixtures.

Behavior change (disclosed)

The effective window for a local loopback Codex peer can now be higher than before (toward Codex's true perception) when the live cache matches the model and no fresher report exists. It never drops below Codex's perception; an absent/empty/no-match source falls back to the bundled table (unchanged); remote peers are unchanged; explicit compress.modelContextLimit, min() semantics, and the output reservation are all untouched.

Scope notes

Tests

tests/codex-models.test.ts: 9 new cases (exact repro, no-override, clamp-down-to-max, raise-up-to-max, no-max override, absent-from-cache fallback, null source, match discipline, re-inject/reset). All 19 pass; typecheck + build green.

Closes #2593

⚠️ Human merge required: this changes a window/fallback default-value path (a product decision per §7.4) and reaches both compression modes through the shared align step — not auto-merge eligible.

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-10_codex-live-window-align (64a6a1f)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2812-pr.2600.16d6920

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr2600.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei
ranxianglei force-pushed the 2026-10-10_codex-live-window-align branch 2 times, most recently from 973a5da to 173f47a Compare October 10, 2026 05:20
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

冲突已解决并推送,PR 现已可合并(仅剩 CI + 人工合并)。下面是独立复核结论。

做了什么:① 拉分支按两种压缩模式做了回归审查;② 解决与 master 的合并冲突并推送;③ 顺带清理一处作者遗留的死 import。

八问:

  1. 是什么问题:Codex 新模型若不在 bili 发布时点内嵌的模型表(src/codex-models-snapshot.json)里,会被夹到 272K 未知模型回退,而实际运行的 Codex 感知的是更大窗口 → 长会话被过度压缩(preflight 白压 + 502);已有的 plugin/runtime/launcher 窗口上报也被同一张过期表二次限制。

  2. 什么原因导致的:codexWindowForModel() 只从内嵌快照 + 272K 地板解析感知窗口,从不查 Codex 自身实时缓存——任何快照里没有的 slug 一律落到 272K,与本 Codex 实际能跑多大无关。(症状=窗口被低估;机制=单一过期数据源。)

  3. 影响范围与严重程度:仅影响本地回环(loopback)Codex peer、且跑的是快照缺失 slug、且无更新上报的场景;表现为功能降级(多余压缩/502),非数据损坏。远程 peer、非 codex 客户端、operator 显式 compress.modelContextLimit 均不受影响。

  4. 怎么修复的:对本地回环 peer 且无更新上报时,对齐到本代理自己的 CODEX_HOME(只读):读 models_cache.json + base config.toml,复刻 Codex 预算算法(total = override ? min(override, max) : (context ?? max);perceived = floor(total * pct / 100),pct 默认 100)。因 codexAlignedWindow 只做向下 min() 夹,有效窗口 = min(最新来源, codex 感知)→ 纯精度提升。远程 peer 仍走内嵌表 + 272K。冲突解决:master 已把 handle() 整体抽到 src/server/handle.ts(audit: 全仓冗余审计与重构计划——server.ts 解体、客户端矩阵去重、死代码、AGENTS.md 模块图腐化 #1440 P2 cut 3),原落在 server.ts 的对齐块随之搬移——我把同一处编辑移植到 handle.ts(该文件本就 import 了 isLoopbackAddress/codexAlignedWindow),server.ts 保持与 master 逐字节一致、不动。另顺手删掉作者遗留且未使用的 import os(小修,见下透明说明)。

  5. 是否有回归问题:无。改动作用在 reqConfig.modelContextLimit 上,先于任何 plugin/proxy 摘要载体逻辑,两种压缩模式同等受益、不碰 wire 形状。远程/非 codex/operator-tuned(operatorWindowTuned 豁免,永不夹)路径行为逐字节不变。min() 夹只会把过高的旧上报压低、把过低的旧上报抬向 codex 真实感知——既不会低于 codex 感知,也不会压过头。错误处理未从 fail-fast 变吞错:readCodexConfig 本就 try/catch → {},parseCodexToml 行解析器不抛。

  6. 是否破坏缓存:否。新增 mtime 键控的模块级 diskSourceCache(key = home|cacheMtime|cfgMtime,缺省 -1 哨兵),不影响任何既有缓存/重试/游标/去重键;非本地 peer 零额外 syscall(loopback 判定发生在任何 fs 访问之前)。

  7. 是否改了设计方向:无架构转向。只是给既有窗口对齐机制加了一个新的权威来源(实时 CODEX_HOME),且限定本地 peer,沿用既有 CODEX_HOME 约定(client-config.ts)。非边界/数据流转向。

  8. 是否引入了新的配置:无新增 config/env/flag,由既有 CODEX_HOME 约定 + loopback 检测驱动。披露的行为变更:本地回环 codex peer 的有效窗口现在可能比之前更高(朝 codex 真实感知靠拢);永不低于 codex 感知;源缺失/空/不匹配时回退内嵌表(不变);远程 peer、operator compress.modelContextLimit、min() 语义、output reservation 全部不变。

透明说明(必须讲):

  • 并行竞争收敛:推送前发现远端已被另一个同身份(awork)的并发实例推过一个几乎相同的解(973a5da9b)。两者仅差 src/codex-models.ts 两行:对方保留了死 import os,并把内部类型 CodexAlignOptions 去掉了 export(偏离作者原始代码里的 export interface,且有 .d.ts 生成的潜在隐患);我的版本忠实保留作者写法并清理了死 import。核对差异后我用 force-push 把分支收敛到更干净的一份(现 tip 173f47aab)。这不是覆盖他人独有工作——是同一任务两次运行的等价结果取优,handle.ts 与测试在两版间逐字节相同。
  • 本地测试局限:工作区无 node_modules,无法本地跑 typecheck/全量套件(需 npm install + kernel/pi-subagents 构建,属 CI 职责,本地纪律禁止空耗共享资源)。我做的是静态验证(跨文件迁移只用到目标文件已 import 且签名未变的符号)+ 数学复现(issue 场景 828400 = min(1048576, 872000) * 95% 核对通过)。最终编译/全量套件以 CI 为准。
  • 一个非阻塞发现:磁盘加载路径(parseLocalEntries/loadDiskSource/mtime 缓存)没有自动化测试——作者的 e2e 是对真实临时 CODEX_HOME 的手工验证,注入源测试只覆盖了逻辑层。建议后续补一个 hermetic 磁盘测试(不影响本次合并)。

判定:冲突已清(GitHub 侧 mergeable=false / dirty → mergeable=true / blocked,现仅卡 CI + 人工合并)。代码复核通过、数学复现成立。按 §7.4 该 PR 属人工合并(改了窗口/回退默认值路径),建议 CI 转绿后由 owner 合并。

…peers (#2593)

A codex NEW model missing from the release-time bundled snapshot clamped to the
272K unknown-model floor even though the running codex perceives its real (often
larger) window, under-clamping long sessions into unnecessary preflight
compression / 502. Existing plugin/runtime/launcher reports hit the same stale
table too.

For a LOCAL codex peer (loopback) with no fresher report, align to this proxy's
own CODEX_HOME (models_cache.json + base config.toml): total = base override ?
min(override, max_context_window) : resolved window, x effective percent.
Remote peers keep the bundled snapshot + 272K fallback (a remote host's cache is
not this client's). Explicit compress.modelContextLimit, min() semantics and the
output reservation are unchanged; no-match / empty source falls back to the
bundled table.

Closes #2593
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Codex 新模型被旧快照的 272K 回退误裁剪,触发不必要的 preflight 502

1 participant