#!/usr/bin/env bash set -euo pipefail CODEX_HOME="${CODEX_HOME:-$HOME/.codex}" SESSIONS_DIR="$CODEX_HOME/sessions" ARCHIVE_DIR="${CODEX_ARCHIVE_DIR:-$HOME/.codex_archives/sessions}" OLDER_THAN_DAYS="${OLDER_THAN_DAYS:-30}" MIN_SIZE_MB="${MIN_SIZE_MB:-50}" APPLY=0 if [[ "${1:-}" == "--apply" ]]; then APPLY=1 fi if [[ ! -d "$SESSIONS_DIR" ]]; then echo "Sessions directory not found: $SESSIONS_DIR" >&2 exit 1 fi python3 - "$SESSIONS_DIR" "$ARCHIVE_DIR" "$OLDER_THAN_DAYS" "$MIN_SIZE_MB" "$APPLY" <<'PY' from datetime import datetime, timedelta from pathlib import Path import shutil import sys sessions_dir = Path(sys.argv[1]) archive_dir = Path(sys.argv[2]) older_than_days = int(sys.argv[3]) min_size_mb = int(sys.argv[4]) apply = int(sys.argv[5]) cutoff = datetime.now() - timedelta(days=older_than_days) candidates = [] for path in sessions_dir.rglob("*.jsonl"): stat = path.stat() mtime = datetime.fromtimestamp(stat.st_mtime) size_mb = stat.st_size / (1024 * 1024) if mtime < cutoff and size_mb >= min_size_mb: candidates.append((size_mb, mtime, path)) candidates.sort(reverse=True) if not candidates: print("No session files matched the archive rules.") raise SystemExit(0) print("Candidates:") for size_mb, mtime, path in candidates: print(f"{size_mb:9.1f} MB {mtime:%Y-%m-%d} {path}") if not apply: print() print("Dry run only. Re-run with --apply to move these files to:") print(archive_dir) raise SystemExit(0) for _, _, path in candidates: rel = path.relative_to(sessions_dir) dest = archive_dir / rel dest.parent.mkdir(parents=True, exist_ok=True) shutil.move(str(path), str(dest)) print() print(f"Archived {len(candidates)} session files to {archive_dir}") PY