Files
lucioandClaude Opus 5.5 275d42aeb4 Add docs, tests and agent rules; fix demo paths and a lock-release race
- README.md: usage, keys, how a sanity run works, config, demo command
- CLAUDE.md: rules for agents, ported from verilogrustformatter with a
  single serial dev loop (lead plans, dev, QA, lead reviews)
- tests/test_runner.py: stdlib unittest suite running the runner end to
  end against the demo sandbox (pass, fail, cancel, stale, stamp hash)
- tools/guard.py: time limit plus leaked-process check for test runs
- TODO.md and docs/features/dsflow-tui/README.md: backlog and feature doc

Fixes found by the tests:
- the fake dssc resolved paths wrong under symlinked dirs, breaking the
  default --demo location on macOS (/var -> /private/var)
- the runner saved its terminal status before releasing locks, so the
  TUI could report a run finished while locks were still held

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-09-23 11:09:35 -07:00

56 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""Run a command with a wall-clock limit, then fail if it leaked runner processes.
tools/guard.py [-t SECONDS] -- <command> [args...]
Kills the command's whole process group when it exceeds the limit (default
600 s). Afterwards, any process still matching the dsflow_tui runner or its
bash helpers is reported and the guard exits non-zero: a leak is a finding.
"""
import argparse
import os
import signal
import subprocess
import sys
import time
LEAK_PATTERN = "_runjob|dsflow-tui"
def leftovers():
out = subprocess.run(["pgrep", "-fl", LEAK_PATTERN], stdout=subprocess.PIPE,
universal_newlines=True).stdout
return [l for l in out.splitlines() if "guard.py" not in l]
def main():
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument("-t", type=int, default=600, metavar="SECONDS")
ap.add_argument("command", nargs=argparse.REMAINDER)
args = ap.parse_args()
cmd = args.command[1:] if args.command[:1] == ["--"] else args.command
if not cmd:
ap.error("no command given")
p = subprocess.Popen(cmd, start_new_session=True)
try:
rc = p.wait(timeout=args.t)
except subprocess.TimeoutExpired:
os.killpg(p.pid, signal.SIGKILL)
p.wait()
print("guard: killed after %ds: %s" % (args.t, " ".join(cmd)), file=sys.stderr)
rc = 124
time.sleep(1) # let detached runners finish their own cleanup
leaked = leftovers()
if leaked:
print("guard: leftover processes (a finding - report the command):", file=sys.stderr)
for line in leaked:
print(" " + line, file=sys.stderr)
return rc or 3
return rc
if __name__ == "__main__":
sys.exit(main())