From eec4ff99722b8ed288d05e894bbb6b3712b83553 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 17:20:29 +0100 Subject: [PATCH] refactor: one autorun worktree path and checkout --- scripts/cleanup_disk.py | 3 ++- scripts/core/gitclone.py | 12 ++++++++++++ scripts/core/paths.py | 4 ++++ scripts/devtools/diskcleanup.py | 11 +++-------- scripts/unattended/runner.py | 6 ++---- scripts/unattended/worktree.py | 10 ++++++---- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/scripts/cleanup_disk.py b/scripts/cleanup_disk.py index c5eea9e94..d39c35b17 100644 --- a/scripts/cleanup_disk.py +++ b/scripts/cleanup_disk.py @@ -35,7 +35,8 @@ import time from pathlib import Path sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) -from devtools.diskcleanup import ALERT_GB, apply, busy_paths, disk_line, inside, main_checkout, plan +from core.gitclone import main_checkout +from devtools.diskcleanup import ALERT_GB, apply, busy_paths, disk_line, inside, plan def main() -> int: diff --git a/scripts/core/gitclone.py b/scripts/core/gitclone.py index 3f640541e..a3f5f996c 100644 --- a/scripts/core/gitclone.py +++ b/scripts/core/gitclone.py @@ -4,6 +4,7 @@ from __future__ import annotations import re import subprocess +from pathlib import Path class GitRefused(RuntimeError): @@ -37,6 +38,17 @@ def required_git_lines(*args: str) -> list[str]: return _lines(result.stdout) +def main_checkout(cwd: Path) -> Path: + """The main checkout of the clone holding cwd, also from a linked worktree, + GitRefused when cwd is in no clone.""" + args = ("rev-parse", "--path-format=absolute", "--git-common-dir") + result = _run(("-C", str(cwd), *args)) + if result.returncode: + detail = result.stderr.strip() or f"exit {result.returncode}" + raise GitRefused(f"git {' '.join(args)}: {detail}") + return Path(result.stdout.strip()).parent + + # Where a clone with no remote naming the project keeps its main line, each # remote's names in the order tried. FALLBACK_REMOTES = ("upstream", "origin") diff --git a/scripts/core/paths.py b/scripts/core/paths.py index 75bb38dca..427304def 100644 --- a/scripts/core/paths.py +++ b/scripts/core/paths.py @@ -19,6 +19,10 @@ BIOS_DIR = "bios" DATA_DIRS_FILE = f"{PLATFORMS_DIR}/_data_dirs.yml" # Where generate_truth writes its models and diff_truth and export_native read them. TRUTH_DIR = "dist/truth" +# The unattended runner's home, and the worktree it commits from, which disk +# cleanup must never take for an idle session worktree. +AUTORUN_DIR = ".cache/autorun" +AUTORUN_WORKTREE = f"{AUTORUN_DIR}/worktree" def sanitize_pack_path(raw: str) -> str: diff --git a/scripts/devtools/diskcleanup.py b/scripts/devtools/diskcleanup.py index 929cae6b7..3ed633d78 100644 --- a/scripts/devtools/diskcleanup.py +++ b/scripts/devtools/diskcleanup.py @@ -11,12 +11,13 @@ from collections.abc import Iterable from dataclasses import dataclass from pathlib import Path +from core.paths import AUTORUN_WORKTREE + GENERATED = re.compile( r"^(database\.json|install/.*\.json|exports/.*\.json|README\.md|CONTRIBUTING\.md" r"|website/src/data/[^/]+\.json)$" ) KEPT_TMP = ("acquire",) -AUTOMATION_WORKTREE = Path(".cache") / "autorun" / "worktree" ALERT_GB = 100 @@ -37,12 +38,6 @@ def git(cwd: Path, *args: str, check: bool = True) -> str: return proc.stdout -def main_checkout(cwd: Path) -> Path: - """The main checkout, also when run from inside a linked worktree.""" - common = git(cwd, "rev-parse", "--path-format=absolute", "--git-common-dir").strip() - return Path(common).parent - - def busy_paths() -> set[str]: """Every working directory and open file of every readable process.""" found: set[str] = set() @@ -100,7 +95,7 @@ def linked_worktrees(repo: Path) -> list[Path]: def session_worktrees(repo: Path) -> list[Path]: """Linked worktrees inside the checkout, the automation worktree aside.""" - keep = repo / AUTOMATION_WORKTREE + keep = repo / AUTORUN_WORKTREE return [p for p in linked_worktrees(repo) if inside(p, repo) and p != keep] diff --git a/scripts/unattended/runner.py b/scripts/unattended/runner.py index dcb9124f6..cf053dda5 100644 --- a/scripts/unattended/runner.py +++ b/scripts/unattended/runner.py @@ -19,6 +19,7 @@ from pathlib import Path from core.artifacts import TIMESTAMP_KEYS from core.hostguard import LockBusy, free_gb, global_lock, lock_busy +from core.paths import AUTORUN_DIR, AUTORUN_WORKTREE from core.statefile import now_iso, save_state from unattended.breaker import may_push, refresh_breaker from unattended.jobs import AutorunError, BLOCKED, DEFERRED, FAILED, Job, OK, REFUSED, Step, job_env @@ -34,9 +35,6 @@ from unattended.worktree import ( ) -AUTORUN_DIR = Path(".cache") / "autorun" - - ALERT_FREE_GB = 100 LOG_DAYS = 30 SEGFAULT_CODES = (139, -11) @@ -81,7 +79,7 @@ class Context: @property def worktree(self) -> Path: - return self.home / "worktree" + return self.repo / AUTORUN_WORKTREE @property def state_path(self) -> Path: diff --git a/scripts/unattended/worktree.py b/scripts/unattended/worktree.py index e28bf5888..2179c8981 100644 --- a/scripts/unattended/worktree.py +++ b/scripts/unattended/worktree.py @@ -11,6 +11,8 @@ import subprocess from collections.abc import Callable, Sequence from pathlib import Path +from core.gitclone import GitRefused +from core.gitclone import main_checkout as gitclone_main_checkout from core.paths import REPO_ROOT from unattended.jobs import AutorunError, BLOCKED, CommitRule, FAILED, token_env @@ -158,7 +160,7 @@ def push_batch(worktree: Path, gate_base: str, regate: Callable[[], bool]) -> st def main_checkout() -> Path: """The main checkout, also when this script runs from a linked worktree.""" - proc = git(REPO_ROOT, "rev-parse", "--path-format=absolute", "--git-common-dir", - check=False) - common = Path(proc.stdout.strip()) if proc.returncode == 0 else REPO_ROOT / ".git" - return common.parent + try: + return gitclone_main_checkout(REPO_ROOT) + except GitRefused: + return REPO_ROOT