"""Runs a child agent's verifier command in its own process group and exits 0 when the command passes, or 1 with bounded failure detail on stdout.""" from __future__ import annotations import json import os import subprocess import tempfile from albedo_helper_lifetime import Commands, read_request OUTPUT_LIMIT = 8192 TIMEOUT = 300.0 def tail_excerpt(text: str) -> str: stripped = text.strip() if len(stripped) <= 4000: return stripped return "...\n" + stripped[-4000:] def main() -> int: request = json.loads(read_request()) commands = Commands() command = str(request["command"]) cwd = request.get("cwd") cwd = cwd if isinstance(cwd, str) and os.path.isdir(cwd) else None with tempfile.TemporaryFile() as stdin, tempfile.TemporaryFile() as out: process = commands.start( command, cwd=cwd, stdin=stdin, stdout=out, stderr=subprocess.STDOUT, ) timed_out = False try: commands.wait(process, TIMEOUT) except subprocess.TimeoutExpired: timed_out = True finally: commands.finish(process) out.seek(0) raw = out.read(OUTPUT_LIMIT * 2).decode(errors="replace") excerpt = tail_excerpt(raw) if timed_out: detail = f"verifier `{command}` timed out after {TIMEOUT:g}s" if excerpt: detail += f":\n{excerpt}" print(detail) return 1 if process.returncode == 0: return 0 detail = f"verifier `{command}` failed (exit {process.returncode})" if excerpt: detail += f":\n{excerpt}" print(detail) return 1 if __name__ == "__main__": raise SystemExit(main())