"""One `claude -p --output-format json` run, as loop-claude's judge and the replays start it.""" import dataclasses import subprocess import typing from failure import Failure from claude_code.entries import hit_limit, limit_said, parse_entry # Effects.claude: claude's arguments, its working directory and the prompt it reads on stdin. Claude = typing.Callable[[list[str], str, str], subprocess.CompletedProcess[str]] @dataclasses.dataclass(frozen=True) class Limited: """claude hit a usage limit; said is claude's message about it, and resets_at is when the limit resets, in seconds since the epoch, or None when unknown.""" resets_at: int | None said: str def claude_reply(claude: Claude, args: list[str], cwd: str, prompt: str, who: str) -> dict | Failure | Limited: """claude's json reply, or why there is none; who names the run in the message for a non-zero exit, such as the judge.""" try: proc = claude(args, cwd, prompt) except OSError as failure: return Failure(f"could not start claude: {failure.strerror}") reply = parse_entry(proc.stdout) or {} # The json output names the reset only in prose, such as "resets 7:40pm (Europe/Stockholm)", so its limit carries no reset time. if hit_limit(reply): return Limited(None, limit_said(reply)) if proc.returncode != 0: return Failure(proc.stderr.strip() or f"{who} exited {proc.returncode}") return reply