Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566"""Ownership and process checks shared by the manual UI review tools."""
from __future__ import annotations
import fcntlimport osfrom pathlib import Pathimport signalimport subprocessimport sysimport timefrom typing import BinaryIO
def acquire_base(base: Path, *, exclusive: bool) -> BinaryIO: base.mkdir(parents=True, exist_ok=True) lease = (base / ".review.lock").open("a+b") try: fcntl.flock( lease, (fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH) | fcntl.LOCK_NB ) except BaseException: lease.close() raise return lease
def reserve_run(base: Path, name: str) -> Path: if not name or name in (".", "..") or Path(name).name != name: raise ValueError("run name must be one directory name") runs = base / "runs" runs.mkdir(exist_ok=True) run = runs / name run.mkdir() # Existing runs and their evidence must never be replaced. (run / ".active").touch() return run
def processes(home: Path) -> list[str]: args = ["ps", "-axeww", "-o", "pid=,command="] if sys.platform == "darwin": args.insert(2, "-E") listing = subprocess.run( args, capture_output=True, text=True, check=True, timeout=30 ) return [ line[:200] for line in listing.stdout.splitlines() if f"ALBEDO_HOME={home} " in line + " " and int(line.split()[0]) != os.getpid() ]
def reap(home: Path) -> list[str]: """Signal home-matching processes, wait, and report any still observed.""" for line in processes(home): try: os.kill(int(line.split()[0]), signal.SIGTERM) except ProcessLookupError: pass deadline = time.monotonic() + 5 while True: remaining = processes(home) if not remaining or time.monotonic() >= deadline: return remaining time.sleep(0.05)