codex-1m-context-window-setup
Configures and verifies an expanded, model-aware context window for OpenAI Codex CLI and Codex Desktop by safely updating the shared base config. Use whenever Codex shows about 258K context, the user asks for 500K or 1M context, auto-compaction happens too often, model_context_window or model_auto_compact_token_limit needs repair, or a workstation/classroom needs the same long-context setup across macOS and Windows. Detects the selected model's live maximum, requests up to 1M tokens, sets compaction to 60% of the attainable window, preserves unrelated TOML, backs up changes, and fails rather than guessing when the model contract cannot be verified.
1实现原理 · 为什么它能做到
单一 Python 脚本三模式(doctor/apply/verify),全部由 subprocess 调 Codex 官方 CLI 取『活数据』:codex doctor --json 解析出所选模型与 config.load 状态,codex debug models 拿当前模型目录,绝不背记/编造模型上限。
If Codex cannot return a live model catalog, stop. Do not substitute remembered model limits, API marketing numbers, or a bundled default.
model-aware 计算:target_raw = min(1,000,000, 模型 max_context_window);usable = target_raw × effective_context_window_percent;auto-compact = target_raw × 3/5(60% 用户既定策略);并输出 capped_by_model / expands_catalog_default 供报告。
target_raw = min(TARGET_CONTEXT_CEILING, maximum_raw) target_effective = target_raw * effective_percent // 100 compact_limit = target_raw * COMPACT_NUMERATOR // COMPACT_DENOMINATOR
apply 是保守编辑器:正则只替换文件顶层两个键(model_context_window / model_auto_compact_token_limit)的简单整数赋值行;语法无法保守保留时拒绝改;新键插在第一个 table 头之前;改完 tomllib 回读校验。
raise SetupError( f"top-level {key} uses syntax the conservative editor cannot preserve; " "no file was changed")
写盘安全链:内容寻址备份(config.toml.<UTC时间>.<sha12>.bak 放 CODEX_HOME/backups/codex-1m-context-window-setup/)→ 同目录临时文件+fsync+os.replace 原子替换 → 写前检测并发改动 → 写后 strict-config doctor 验证,失败自动还原原字节。
backup = backup_dir / f"config.toml.{timestamp}.{digest}.bak"
verify 是漂移检测器:重跑 live catalog 计算推荐值,与当前 base config 不符即以非零退出报告 mismatch——Codex 升级/默认模型变更后一跑即知是否仍合规。
It exits non-zero when the current base config no longer equals the model-aware recommendation. Report the mismatch; do not call it configured.
技能边界显式收缩:不改模型/推理档位/沙箱/审批/插件/MCP/浏览器设置、不碰 profile 级配置、不覆盖 per-command -c flag、不声称当前线程已扩容、不强制 1M。
Do not ask the user for raw token numbers. The model catalog is the authority and the script derives the attainable values.
2核心能力
3外部依赖
| Kind | Dependency / Endpoint |
|---|---|
| cli | codex(官方 CLI;doctor/debug models/--strict-config doctor) |
| cli | uv(uv run --no-project python 执行脚本) |
4管控建议与判定依据
- scripts/: 执行本地脚本或命令: python3 运行 codex_context_window.py(纯 stdlib)
- 本地工作目录: 文件读写与工件输出: apply 时写 $CODEX_HOME/config.toml(内容寻址备份 + 同目录原子替换 + 失败回滚原文);备份写 $CODEX_HOME
5第二遍独立确认
- [ok] 只改两键的保守编辑承诺 — TARGET_KEYS=('model_context_window','model_auto_compact_token_limit');SIMPLE_INTEGER_ASSIGNMENT 仅匹配顶层;非保守语法 raise 且不写。
- [ok] 备份/原子/回滚链 — backup_config → atomic_write(mkstemp+fsync+os.replace) → strict_config_check → 异常分支 atomic_write 原文/删除新文件,全链在代码中。
- [ok] 无网络/凭证 — 脚本 imports 无 http 库;env 仅 CODEX_HOME;不读 auth 文件。
- [ok] verify 漂移语义 — main() exit_code = 0 if status=='configured' else 1(verify 分支),SKILL 描述一致。
- [unlocatable] codex CLI 输出 schema(doctor --json / debug models 字段) — 字段名(checks.config.load/details.model、models[].context_window/max_context_window/effective_context_window_percent)依赖安装的 codex 版本,本环境无 Codex 未运行验证;SKILL 要求 doctor 失败即停机,schema 漂移会被显式失败捕获。
6结论
9d4e6694887d55eb…