From 54bed5c235c92099ec3da0f54f54240819bb29d3 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 24 Nov 2026 02:58:50 +0100 Subject: [PATCH] fix: keep test envs and patches in disk cleanup --- scripts/cleanup_disk.py | 6 ++++-- scripts/devtools/diskcleanup.py | 27 ++++++++++++++++++++++++--- tests/test_autorun.py | 20 ++++++++++++++++++++ wiki/tools.md | 5 +++-- 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/scripts/cleanup_disk.py b/scripts/cleanup_disk.py index d39c35b17..ce390f73f 100644 --- a/scripts/cleanup_disk.py +++ b/scripts/cleanup_disk.py @@ -14,8 +14,10 @@ kept, so committed work stays reachable. A worktree with real uncommitted code is listed, never removed. tmp/ entries older than --hours that no process uses are removed, except -tmp/acquire, git bundles (*.bundle, which are backups) and an entry that -holds a linked worktree, which the worktree rule judges. +what KEPT_TMP in devtools/diskcleanup.py lists (tmp/acquire, git bundles and +patches, the TUI test environments, the Python 3.8 wrapper, the CI +guardian's state) and an entry that holds a linked worktree, which the +worktree rule judges. The last line reports free space, and an ALERT line when it is under --alert-gb. The exit code is 1 under that threshold, so a timer sees it. diff --git a/scripts/devtools/diskcleanup.py b/scripts/devtools/diskcleanup.py index e2c369d2a..7ed493067 100644 --- a/scripts/devtools/diskcleanup.py +++ b/scripts/devtools/diskcleanup.py @@ -3,6 +3,7 @@ from __future__ import annotations import contextlib +import fnmatch import os import shutil import subprocess @@ -13,7 +14,20 @@ from pathlib import Path from core.generated import is_generated from core.paths import AUTORUN_WORKTREE -KEPT_TMP = ("acquire",) +# What tmp/ keeps whatever its age, by name pattern, with the reason printed. +KEPT_TMP: dict[str, str] = { + # auto_fetch writes the files it found and verified here. + "acquire": "backup", + # A git bundle or a patch saved before a risky operation. + "*.bundle": "backup", + "*.patch": "backup", + # The TUI pty tests run under tmp/tuienv312 and tmp/tuienv38. + "tuienv*": "test environment", + # The Python 3.8 wrapper the installer floor is checked with. + "py38bin": "test environment", + # The CI guardian's log, scripts and the runs it has already seen. + "ci_guardian*": "CI guardian state", +} ALERT_GB = 100 @@ -107,6 +121,12 @@ def judge_worktree(path: Path, busy: Iterable[str], hours: float, now: float) -> return Decision(path, "worktree", "remove", f"{age:.0f}h old, clean") +def kept_reason(name: str) -> str: + """Why tmp/ keeps an entry of this name whatever its age, or ''.""" + return next((reason for pattern, reason in KEPT_TMP.items() + if fnmatch.fnmatchcase(name, pattern)), "") + + def judge_tmp( repo: Path, busy: Iterable[str], hours: float, now: float, worktrees: Iterable[Path] = (), @@ -117,8 +137,9 @@ def judge_tmp( worktrees = list(worktrees) decisions = [] for entry in sorted(tmp.iterdir()): - if entry.name in KEPT_TMP or entry.name.endswith(".bundle"): - action, reason = "keep", "backup" + kept = kept_reason(entry.name) + if kept: + action, reason = "keep", kept elif any(inside(w, entry) for w in worktrees): action, reason = "keep", "holds a worktree" elif age_hours(entry, now) <= hours: diff --git a/tests/test_autorun.py b/tests/test_autorun.py index f7117e300..4a4dccb7a 100644 --- a/tests/test_autorun.py +++ b/tests/test_autorun.py @@ -843,6 +843,26 @@ class Cleanup(Origin): cd.apply(self.repo, decisions.values()) self.assertEqual((work / ".gitignore").read_text(), "edited") + def test_old_kept_tmp_entries_survive(self): + """What every gate runs under and the backups outlive any age: + the scheduled cleaner removed the TUI environments and a wip patch.""" + tmp = self.repo / "tmp" + names = ("acquire", "tuienv312", "tuienv38", "py38bin", "ci_guardian.log", + "ci_guardian_seen.txt", "main.bundle", "wip-2026-11-12.patch") + stamp = time.time() - 1000 * 3600 + for name in names: + path = tmp / name + path.parent.mkdir(parents=True, exist_ok=True) + if "." in name: + path.write_bytes(b"") + else: + path.mkdir() + os.utime(path, (stamp, stamp)) + decisions = cd.judge_tmp(self.repo, [], 72, time.time()) + self.assertEqual({d.path.name: d.action for d in decisions}, + dict.fromkeys(names, "keep")) + self.assertTrue(all(d.reason for d in decisions)) + def test_low_space_raises_the_alert_line(self): line, low = cd.disk_line(self.tmp, alert_gb=10 ** 9) self.assertTrue(low) diff --git a/wiki/tools.md b/wiki/tools.md index d19e4e85e..867949bc2 100644 --- a/wiki/tools.md +++ b/wiki/tools.md @@ -895,8 +895,9 @@ theirs. One is removed only when no process uses it, it is older than `CONTRIBUTING.md`, `website/src/data/*.json`). One holding real changes is listed, never removed. Branches are always kept. A worktree elsewhere on disk and the automation worktree are never candidates. `tmp/` entries older than `--hours` that no process uses -are removed, except `tmp/acquire`, `*.bundle` and an entry that holds a -linked worktree. +are removed, except `tmp/acquire`, `*.bundle`, `*.patch`, the TUI test +environments (`tuienv*`), the Python 3.8 wrapper (`py38bin`), the CI +guardian's files and an entry that holds a linked worktree. The last line gives free space, with an `ALERT` line under `--alert-gb` (default 100), and the exit code is then 1. `--check` reports space only.