Deterministic, archive-free ring-buffer and disk/inode overflow guard for OpenAI Codex CLI (~/.codex) on Linux & WSL2.
(Deutsche Prozess- und Architektur-Dokumentation: docs/PROZESS-UND-ARCHITEKTUR-DE.md)
Heavy daily use of the OpenAI Codex CLI on Linux or WSL2 causes ~/.codex to grow without bound across four distinct storage and inode drivers:
- Leaked Git temp directories (
~/.codex/.tmp/git-XXXXXX):- Codex CLI can leak hundreds of thousands of empty temporary
git-XXXXXXdirectories (350,000+directories consuming1.4M+inodes and bloating the.tmpdirectory inode itself to12 MiB+, makingreaddir()/lspainfully slow).
- Codex CLI can leak hundreds of thousands of empty temporary
- Unbounded session rollouts (
~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl):- Every session appends
.jsonlrollouts indefinitely (60–70+ GiBacross10,000+files after a few weeks of multi-agent work).
- Every session appends
- Unsynchronized & uncompacted SQLite databases (
state_5.sqlite&thread_history_1.sqlite):- Simply running
find ~/.codex/sessions -mtime +14 -deletebreakscodex resume(leaving dead thread pointers instate_5.sqlite), risks deleting pinned threads (is_pinned = 1) or currently open sessions (/proc/*/fd), and never reclaims the multi-GiB SQLite freelist inthread_history_1.sqlite.
- Simply running
- Expired dated run logs and backup snapshots (
~/.codex/state/&~/.codex/backups/):- Dated batch/evaluation runs and timestamped backup snapshots accumulate over time.
Instead of shuffling old sessions into dead .tar.zst archives on the same disk, codex-hdd-guard enforces a fail-closed, zero-dependency Python ring buffer that reclaims real disk blocks and inodes safely.
- Fail-Closed Pin Index (
Exit 3): ReadsSELECT id FROM threads WHERE is_pinned != 0fromstate_5.sqlite(andsqlite/state_5.sqlite). If any pin database is locked (BEGIN EXCLUSIVE), corrupt, or unreadable,--applyaborts immediately with 0 deletions and exits with code3. - Live
/procOpen-FD & CWD Protection: Scans/proc/*/fdand/proc/*/cwdboth at startup and immediately before everyunlink(),rmtree(), and SQLiterwtransaction. Files or directories held open by any running process are never touched. - 48-Hour Safety Floor: Never evicts session rollouts modified within the last 48 hours (
--sessions-safety-floor-hours 48), even ifsessions/exceeds the--sessions-max-gbcap (emits asessions_cap_warninginalertsinstead). - Race-Free
.tmpDirectory Inode Compaction: When~/.codex/.tmpexceeds1 MiBdirectory inode size after cleaninggit-XXXXXXleaks, rebuilds.tmpatomically under an exclusiveflockonly when zero processes hold open FDs inside.tmp. - Synchronized SQLite Pruning & Safe
VACUUM: Cleans deleted thread IDs acrossstate_5.sqlite(threads,thread_read_receipts,thread_dynamic_tools,thread_attachments,guardian_review_feedback,thread_spawn_edges,attached_thread_parent_id,rollout_migration_skipped_rollouts) andthread_history_1.sqlite(thread_items,thread_turns,thread_realtime_items,thread_history_projection_state). RunsVACUUMonly when the DB is not open,.sqlite-maintenance.lockis free, freelist pages exceed--vacuum-threshold-mb(256 MiB), andos.statvfsconfirms>= 1.25xfree disk space. - Atomic Status Receipt (
Exit 4) & Built-in Healthcheck (Exit 5): Every--applyrun atomically writes<codex-home>/state/codex-hdd-guard-last-run.jsonvia temporary file +os.fsync()+os.replace().--check-last-runvalidates receipt freshness, pin-index health, and active alerts.
git clone https://github.com/simfor99/codex-hdd-guard.git ~/projects/codex-hdd-guard
chmod +x ~/projects/codex-hdd-guard/bin/codex-hdd-guard.py
ln -sf ~/projects/codex-hdd-guard/bin/codex-hdd-guard.py ~/.local/bin/codex-hdd-guard.pycodex-hdd-guard.py --codex-home ~/.codex --dry-run --jsoncodex-hdd-guard.py --codex-home ~/.codex --apply --jsonmkdir -p ~/.config/systemd/user
codex-hdd-guard.py --print-systemd-units # inspect generated unitsOr write ~/.config/systemd/user/codex-hdd-guard.service and ~/.config/systemd/user/codex-hdd-guard.timer and enable:
systemctl --user daemon-reload
systemctl --user enable --now codex-hdd-guard.timer
systemctl --user list-timers codex-hdd-guard.timercodex-hdd-guard.py --codex-home ~/.codex --check-last-run --jsonBy default, codex-hdd-guard keeps all undated skill/service state directories in ~/.codex/state/ (kept_durable_unmatched) and only expires dated run directories (*YYYY-MM-DD*, *YYYYMMDD*), .log files, and timestamped backup snapshots (*-backup-YYYYMMDD*).
To protect additional filenames or add custom ephemeral regex patterns without modifying the script, create ~/.codex/hdd-guard-config.json:
{
"extra_protected_state_entries": [
"my-service-registry.json"
],
"extra_ephemeral_state_patterns": [
"^.*-scratch-goal$"
],
"extra_protected_backup_entries": [
"skills"
],
"extra_ephemeral_backup_patterns": []
}| Exit Code | Meaning |
|---|---|
0 |
Success (--dry-run, --apply, or healthy --check-last-run) |
2 |
Invalid CLI arguments or invalid --codex-home |
3 |
Fail-Closed Abort: Pin database (state_5.sqlite) locked or unreadable (0 files deleted) |
4 |
Receipt Write Failure: --apply could not atomically write state/codex-hdd-guard-last-run.json |
5 |
Healthcheck Alert (--check-last-run): Receipt missing, older than --max-receipt-age-hours (12h), or has active alerts |
python3 tests/test_codex_hdd_guard.pyThe test suite requires only the Python 3 standard library (sqlite3, tempfile, subprocess, fcntl, pathlib) and verifies all four cleanup rails, BEGIN EXCLUSIVE pin-database lock protection (Exit 3), unwritable receipt failure (Exit 4), and --check-last-run healthcheck verification (Exit 0 / Exit 5).
MIT License — see LICENSE.