Codex Context Repair
Safety Rules
- Treat Codex local state as user data. Never delete or truncate session history.
- Always run diagnosis first unless the user explicitly asked for repair.
- Before repair, back up the target rollout JSONL to
C:\Users\Administrator\.codex\migration_backups. - Prefer repairing one explicit thread id. If several threads match a title/path, show candidates and ask the user to choose.
- Do not paste large JSONL/session contents into chat. Report ids, paths, counts, and concise findings.
Workflow
- Identify the affected thread:
- Use a user-provided thread id when available.
- Otherwise list likely threads by title, cwd, updated time, token count, and rollout path.
- Diagnose:
- Check
state_5.sqliteforthreads,jobs, andstage1_outputs. - Inspect the target
sessions/**/*.jsonltail fortask_started,task_complete,turn_aborted,compacted, andcontext_compacted. - Measure large JSONL lines and recent
token_countevents.
- Check
- Decide whether repair is appropriate:
- Good candidate: a compact/task turn started but no completion event follows, or token count is near the model context window and no recent
context_compactedwas written. - Bad candidate: active current thread still running, missing rollout file, or multiple ambiguous matches.
- Good candidate: a compact/task turn started but no completion event follows, or token count is near the model context window and no recent
- Repair only with explicit intent:
- Run
scripts/repair_codex_context.py --thread-id <id> --repair. - The script appends a manual
turn_abortedfor an unfinished last task when needed. - The script appends a small
compactedcheckpoint containing a recovery summary and next-step prompt. - The script appends
context_compactedafter the checkpoint and refreshesthreads.updated_at.
- Run
- Verify:
- Re-read the JSONL tail and confirm the final events are the manual compact checkpoint and
context_compacted. - Tell the user to restart Codex if the UI still shows cached old state.
- Re-read the JSONL tail and confirm the final events are the manual compact checkpoint and
Script
Use:
python C:\Users\Administrator\.codex\skills\codex-context-repair\scripts\repair_codex_context.py --list
python C:\Users\Administrator\.codex\skills\codex-context-repair\scripts\repair_codex_context.py --thread-id <thread-id> --diagnose
python C:\Users\Administrator\.codex\skills\codex-context-repair\scripts\repair_codex_context.py --thread-id <thread-id> --repair
Useful filters:
python C:\Users\Administrator\.codex\skills\codex-context-repair\scripts\repair_codex_context.py --list --cwd-like "D:\Desktop"
python C:\Users\Administrator\.codex\skills\codex-context-repair\scripts\repair_codex_context.py --list --title-like "重构"
On Windows PowerShell, if Chinese filter text gets mangled, use thread id, cwd fragments without Chinese, or Unicode escapes through Python.