Skip to content

About

Deterministic, archive-free ring-buffer and disk/inode overflow guard for OpenAI Codex CLI (~/.codex) on Linux & WSL2

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

codex-hdd-guard

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)


Why codex-hdd-guard exists

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:

  1. Leaked Git temp directories (~/.codex/.tmp/git-XXXXXX):
    • Codex CLI can leak hundreds of thousands of empty temporary git-XXXXXX directories (350,000+ directories consuming 1.4M+ inodes and bloating the .tmp directory inode itself to 12 MiB+, making readdir() / ls painfully slow).
  2. Unbounded session rollouts (~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl):
    • Every session appends .jsonl rollouts indefinitely (60–70+ GiB across 10,000+ files after a few weeks of multi-agent work).
  3. Unsynchronized & uncompacted SQLite databases (state_5.sqlite & thread_history_1.sqlite):
    • Simply running find ~/.codex/sessions -mtime +14 -delete breaks codex resume (leaving dead thread pointers in state_5.sqlite), risks deleting pinned threads (is_pinned = 1) or currently open sessions (/proc/*/fd), and never reclaims the multi-GiB SQLite freelist in thread_history_1.sqlite.
  4. 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.


Key Safety Guarantees (Fail-Closed Design)

  • Fail-Closed Pin Index (Exit 3): Reads SELECT id FROM threads WHERE is_pinned != 0 from state_5.sqlite (and sqlite/state_5.sqlite). If any pin database is locked (BEGIN EXCLUSIVE), corrupt, or unreadable, --apply aborts immediately with 0 deletions and exits with code 3.
  • Live /proc Open-FD & CWD Protection: Scans /proc/*/fd and /proc/*/cwd both at startup and immediately before every unlink(), rmtree(), and SQLite rw transaction. 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 if sessions/ exceeds the --sessions-max-gb cap (emits a sessions_cap_warning in alerts instead).
  • Race-Free .tmp Directory Inode Compaction: When ~/.codex/.tmp exceeds 1 MiB directory inode size after cleaning git-XXXXXX leaks, rebuilds .tmp atomically under an exclusive flock only when zero processes hold open FDs inside .tmp.
  • Synchronized SQLite Pruning & Safe VACUUM: Cleans deleted thread IDs across state_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) and thread_history_1.sqlite (thread_items, thread_turns, thread_realtime_items, thread_history_projection_state). Runs VACUUM only when the DB is not open, .sqlite-maintenance.lock is free, freelist pages exceed --vacuum-threshold-mb (256 MiB), and os.statvfs confirms >= 1.25x free disk space.
  • Atomic Status Receipt (Exit 4) & Built-in Healthcheck (Exit 5): Every --apply run atomically writes <codex-home>/state/codex-hdd-guard-last-run.json via temporary file + os.fsync() + os.replace(). --check-last-run validates receipt freshness, pin-index health, and active alerts.

Quickstart

1. Install & Symlink

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.py

2. Preview with --dry-run (default, read-only)

codex-hdd-guard.py --codex-home ~/.codex --dry-run --json

3. Execute Cleanup (--apply)

codex-hdd-guard.py --codex-home ~/.codex --apply --json

4. Enable Automatic 6-Hour systemd --user Timer

mkdir -p ~/.config/systemd/user
codex-hdd-guard.py --print-systemd-units # inspect generated units

Or 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.timer

5. Run Healthcheck (--check-last-run)

codex-hdd-guard.py --codex-home ~/.codex --check-last-run --json

Optional Local Configuration (~/.codex/hdd-guard-config.json)

By 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 Codes

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

Running the End-to-End Test Suite

python3 tests/test_codex_hdd_guard.py

The 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).


License

MIT License — see LICENSE.

About

Deterministic, archive-free ring-buffer and disk/inode overflow guard for OpenAI Codex CLI (~/.codex) on Linux & WSL2

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages