1仕組みと動作原理
単一の Python スクリプトで 3 つのモード(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.
モデル認識型の計算: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 は保守的なエディタ:正規表現により最上位の 2 つのキー(model_context_window / model_auto_compact_token_limit)の単純な整数代入行のみを置換。構文を安全に維持できない場合は変更を拒絶。新規キーは最初のテーブルヘッダーの直前に挿入し、変更後は tomllib で再読込検証を実施。
raise SetupError( f"top-level {key} uses syntax the conservative editor cannot preserve; " "no file was changed")
安全なディスク書き込みチェーン:コンテンツ連動型バックアップ(CODEX_HOME/backups/codex-1m-context-window-setup/ 配下に config.toml.<UTC時刻>.<sha12>.bak を配置)→ 同一ディレクトリの一時ファイル + fsync + os.replace によるアトミック置換 → 書き込み前の並行変更検出 → 書き込み後の strict-config doctor 検証(失敗時は元バイト列に自動ロールバック)。
backup = backup_dir / f"config.toml.{timestamp}.{digest}.bak"
verify はドリフト検出器として機能:live catalog を再実行して推奨値を計算し、現在のベース設定と一致しない場合は非ゼロ終了コードで 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/ブラウザ設定の変更不可、プロファイル単位の設定には非関与、コマンドごとの -c フラグは上書きせず、現在のスレッドが拡張済みと主張せず、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(標準ライブラリのみ)を実行
- 本地工作目录: ファイルの読み書きと成果物出力: 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…