fix: harden retention cleanup safety

This commit is contained in:
2026-09-12 14:12:17 +08:00
parent da5c2b4503
commit b3ab1bfc52
6 changed files with 259 additions and 55 deletions
+13 -8
View File
@@ -1,8 +1,8 @@
#!/usr/bin/env python3
"""Inspect and optionally clean stale job directories.
"""Inspect retention candidates and optionally clean verified managed jobs.
Default mode is a safe dry-run that reports reclaimable bytes. Pass --apply to
actually remove unreferenced job directories.
Default mode is a safe dry-run. Metadata-less workspaces are reported for
manual investigation but are never removed by ``--apply``.
"""
from __future__ import annotations
@@ -46,7 +46,11 @@ def referenced_job_ids() -> set[str]:
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--max-age-days", type=float, default=30)
parser.add_argument("--apply", action="store_true", help="Actually delete stale job directories")
parser.add_argument(
"--apply",
action="store_true",
help="Delete only CleanupService-verified candidates; orphan workspaces remain report-only",
)
parser.add_argument("--json", type=Path, default=None, help="Write JSON report")
args = parser.parse_args()
@@ -89,10 +93,11 @@ def main() -> None:
if os.environ.get("CLEANUP_APPLY_ENABLED", "false").strip().lower() != "true":
parser.error("--apply requires CLEANUP_APPLY_ENABLED=true")
applied = cleanup.apply(datetime.now(timezone.utc))
freed = 0
for item in orphaned:
freed += storage.remove_job_dir(item["job_id"])
store.delete_job(item["job_id"])
freed = sum(
item.size_bytes
for item in applied.temporary_jobs
if item.job_id in applied.deleted_job_ids
)
print(
"freed_bytes="
f"{freed} deleted_job_ids={applied.deleted_job_ids} "