Files
claude-config/bin/task_bench/bench_folder.py
T

44 lines
1.8 KiB
Python

"""The folder a bench keeps its runs' streams and its digest in, under the chunk's review folder, and the lock that says a bench
or a gather is using it. The streams are large and nothing reads them once gather has, so each bench removes the folders left
by benches that crashed or were never gathered."""
import contextlib
import fcntl
import os
import pathlib
import shutil
import typing
from failure import Failure
from chunk_driver.claims import locked
FOLDER = "task-bench"
# Beside FOLDER, not in it, since a bench empties its folder. It marks FOLDER as a bench's, and stays once the folder is gone.
LOCK = "task-bench.lock"
@contextlib.contextmanager
def held(review: pathlib.Path) -> typing.Iterator[pathlib.Path | Failure]:
"""Holds the bench folder of the review folder review, waiting for a bench or a gather that holds it, and yields its path;
a Failure, holding nothing, where its lock cannot be taken."""
with locked(review / LOCK) as unlocked:
yield unlocked or review / FOLDER
def clear_left(reports: pathlib.Path) -> None:
"""Removes each bench folder under reports that no process holds. One that cannot be locked or removed stays.
A bench that ended without a gather loses its folder to the next bench in any checkout: nothing tells a digest that waits
for its gather from one nobody will gather."""
for at, folders, files in os.walk(reports):
if LOCK not in files or FOLDER not in folders:
continue
# A bench's folder holds copies of whole trees, none of them another bench's review folder.
folders.remove(FOLDER)
try:
with open(os.path.join(at, LOCK), "a") as lock:
# A lock this process holds through `held` refuses too, so a bench leaves its own folder to its caller.
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
shutil.rmtree(os.path.join(at, FOLDER), ignore_errors=True)
except OSError:
continue