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.
1How It Works & Mechanism
A single Python script with three modes (doctor/apply/verify), all invoking the official Codex CLI via subprocess to retrieve 'live data': codex doctor --json parses the selected model and config.load status, while codex debug models fetches the current model catalog, never memorizing or hallucinating model limits.
If Codex cannot return a live model catalog, stop. Do not substitute remembered model limits, API marketing numbers, or a bundled default.
Model-aware computation: target_raw = min(1,000,000, model max_context_window); usable = target_raw × effective_context_window_percent; auto-compact = target_raw × 3/5 (user-established 60% policy); and outputs capped_by_model / expands_catalog_default for reporting.
target_raw = min(TARGET_CONTEXT_CEILING, maximum_raw) target_effective = target_raw * effective_percent // 100 compact_limit = target_raw * COMPACT_NUMERATOR // COMPACT_DENOMINATOR
apply is a conservative editor: regex replaces only simple integer assignment lines for two top-level keys (model_context_window / model_auto_compact_token_limit); it refuses to modify if syntax cannot be conservatively preserved; new keys are inserted before the first table header; followed by tomllib re-read verification after modification.
raise SetupError( f"top-level {key} uses syntax the conservative editor cannot preserve; " "no file was changed")
Safe disk-writing chain: content-addressed backup (config.toml.<UTC_timestamp>.<sha12>.bak placed in CODEX_HOME/backups/codex-1m-context-window-setup/) → same-directory temporary file + fsync + os.replace atomic substitution → pre-write concurrency change detection → post-write strict-config doctor verification, automatically restoring original bytes upon failure.
backup = backup_dir / f"config.toml.{timestamp}.{digest}.bak"
verify acts as a drift detector: re-runs live catalog to compute recommended values and reports a mismatch with a non-zero exit code if it deviates from the current base config—instantly revealing compliance status after Codex upgrades or default model changes.
It exits non-zero when the current base config no longer equals the model-aware recommendation. Report the mismatch; do not call it configured.
Explicitly constrained skill scope: does not change model/reasoning tiers/sandbox/approval/plugins/MCP/browser settings, does not touch profile-level configurations, does not override per-command -c flags, does not claim the current thread is expanded, and does not enforce 1M unconditionally.
Do not ask the user for raw token numbers. The model catalog is the authority and the script derives the attainable values.
2Core Capabilities
3External Dependencies
| Kind | Dependency / Endpoint |
|---|---|
| cli | codex(官方 CLI;doctor/debug models/--strict-config doctor) |
| cli | uv(uv run --no-project python 执行脚本) |
4Recommended Governance & Evidence
- scripts/: Executes local script or command: python3 runs codex_context_window.py (standard library only)
- 本地工作目录: File read/write and artifact output: Writes $CODEX_HOME/config.toml during apply (content-addressed backup + atomic replacement in same directory + failure rollback to original content); backups written to $CODEX_HOME
5Second-Pass Verification
- [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 漂移会被显式失败捕获。
6Verdict & Scenarios
9d4e6694887d55eb…