Answer product-owner round 1: 0 🔴, 3 🟡 (0 misread); check a shell that holds a heavy slot, name the process's command, and judge each shell once
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
"""Asks a headless claude whether a worker that prints nothing before its final result is stuck, where one of its shells shows a sign of it, and reports the verdict.
|
||||
"""Reports whether a worker that has gone quiet before its final result is stuck, as a headless claude judges it.
|
||||
|
||||
A worker can stall on a command that hangs, as a full test run did on a shell that ignored SIGINT (item 402). The check runs on the tick `lingering_workers` uses, CHECK_EVERY seconds after the worker's last line, so a worker that prints is never checked. A shell shows a sign where it used no CPU since the last check and wrote nothing for QUIET seconds, or where it is older than OLD; one that runs a busy tool, as `Shell.runs_busy_tool` says, shows none, since a wait for a gate is work. Each chunk's loop checks its own worker: no process watches a whole tmux session, and the shells' commands, processes and output hold what the worker's pane shows.
|
||||
A worker can stall on a command that hangs, as a full test run did on a shell that ignored SIGINT (item 402). The check runs on the tick `lingering_workers` uses: CHECK_EVERY seconds after the worker's last line. It looks for two signs in each of the worker's shells. One is a shell that used no CPU since the last check and wrote no output for QUIET seconds. The other is a shell older than OLD. A shell that runs a busy tool, as `Shell.runs_busy_tool` says, shows neither sign, since waiting for a slot or a merge is work. A shell that holds a heavy slot shows them, since a hung test run holds one.
|
||||
|
||||
The verdict is only reported, on the terminal and in RECORD in the project's transcript archive, and kills nothing until its verdicts have matched what the human would have done.
|
||||
Where a shell shows a sign, a headless claude judges the worker from each shell's command, processes and output tail. These hold what the worker's tmux pane shows, so each chunk's loop checks its own worker, and no process watches a whole tmux session. Each shell is judged once, so a stall that lasts all night costs one call.
|
||||
|
||||
The check only reports, on the terminal and in RECORD in the project's transcript archive. Killing the process it names waits until the recorded verdicts match what the human would have done.
|
||||
"""
|
||||
import dataclasses
|
||||
import json
|
||||
@@ -32,32 +34,37 @@ SCHEMA = {
|
||||
"required": ["verdict", "process", "why"],
|
||||
"additionalProperties": False,
|
||||
}
|
||||
PROMPT = f"""A headless Claude Code worker has printed nothing for {CHECK_EVERY} seconds. Judge from its shells below whether it is `working`, `waiting on a person` or `stuck`, and name the pid of the process that holds it and why.
|
||||
A worker that waits on a gate, such as run-heavy's wait for a slot or a test run that still writes output or uses CPU, is working. Stuck means the process will not finish on its own, as a command waiting on a child that never exits.
|
||||
PROMPT = f"""A headless Claude Code worker hasn't printed anything for {CHECK_EVERY} seconds. Judge from its shells below whether it is `working`, `waiting on a person` or `stuck`. Name the pid of the process it waits on, and say why.
|
||||
- Working: it waits on a process that will finish, such as a test run that still writes output or uses CPU, or run-heavy waiting for a slot.
|
||||
- Waiting on a person: it waits on input or an approval only a person can give, such as a prompt read from a terminal.
|
||||
- Stuck: it waits on a process that will never finish on its own, such as a command waiting on a child that never exits.
|
||||
"""
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class Watch:
|
||||
"""What checks a worker for stuck shells: the rung its investigations run on, the project's transcript archive, and each shell's CPU seconds at the last check."""
|
||||
"""What checks a worker for stuck shells: the rung its investigations run on, the project's transcript archive, each shell's CPU seconds at the last check, and the shells already judged."""
|
||||
rung: Rung
|
||||
archive: pathlib.Path
|
||||
cpu: dict[int, float] = dataclasses.field(default_factory=dict)
|
||||
judged: set[int] = dataclasses.field(default_factory=set)
|
||||
|
||||
def check(self, proc: Streaming, root: str, effects: Effects) -> None:
|
||||
"""Investigates the worker whose claude is proc where a shell shows a sign, and says the verdict on the terminal and in RECORD; a failed investigation is said on stderr."""
|
||||
"""Has a headless claude judge the worker whose claude is proc, where a shell not yet judged shows a sign. The verdict, or why there is none, goes to the terminal and to RECORD."""
|
||||
now = effects.now()
|
||||
shells = proc.shells()
|
||||
signs = [sign for shell in shells if (sign := sign_of(shell, self.cpu.get(shell.pid), now))]
|
||||
signs = {shell.pid: sign for shell in shells if (sign := sign_of(shell, self.cpu.get(shell.pid), now))}
|
||||
self.cpu = {shell.pid: shell.cpu for shell in shells}
|
||||
if not signs:
|
||||
if not signs.keys() - self.judged:
|
||||
return
|
||||
self.judged |= signs.keys()
|
||||
judged = investigated(shells, self.rung, root, effects)
|
||||
if isinstance(judged, Failure):
|
||||
say(f"could not judge whether the worker is stuck: {judged.reason}", effects)
|
||||
return
|
||||
line = f"stuck check: {'; '.join(signs)}; a headless claude judged the worker {judged['verdict']}, held by process {judged['process']}: {judged['why']}"
|
||||
say(line, effects, stdout=True)
|
||||
line = f"stuck check: could not judge whether the worker is stuck: {judged.reason}"
|
||||
else:
|
||||
line = f"stuck check: a headless claude judged the worker {judged['verdict']}, naming process {judged['process']} ({command_of(judged['process'], shells)}): {judged['why']}"
|
||||
line = " ".join(line.split()) + "".join(f"\n - {' '.join(sign.split())}" for sign in signs.values())
|
||||
say(line, effects, stdout=not isinstance(judged, Failure))
|
||||
record = self.archive / RECORD
|
||||
try:
|
||||
record.parent.mkdir(parents=True, exist_ok=True)
|
||||
@@ -68,13 +75,24 @@ class Watch:
|
||||
|
||||
|
||||
def sign_of(shell: Shell, cpu_before: float | None, now: float) -> str:
|
||||
"""What says shell may hold its worker stuck, given the CPU seconds it had used at the last check; empty where nothing does."""
|
||||
"""The sign that shell may hold its worker stuck, given the CPU seconds it had used at the last check; empty where it shows none."""
|
||||
if shell.runs_busy_tool:
|
||||
return ""
|
||||
idle = cpu_before == shell.cpu and now - shell.written > QUIET
|
||||
if not (idle or now - shell.started > OLD):
|
||||
return ""
|
||||
return f"shell {shell.pid} has run {int(now - shell.started)} s, written nothing for {int(now - shell.written)} s{f' and used no CPU for {CHECK_EVERY} s' if idle else ''}: {shell.command}"
|
||||
return f"shell {shell.pid} has run {int(now - shell.started)} s and written no output for {int(now - shell.written)} s{f', and used no CPU for {CHECK_EVERY} s' if idle else ''}: {shell.command}"
|
||||
|
||||
|
||||
def command_of(pid: int, shells: list[Shell]) -> str:
|
||||
"""The command line of the process pid among shells and the processes below them, as the evidence shows it."""
|
||||
for shell in shells:
|
||||
if shell.pid == pid:
|
||||
return shell.command
|
||||
for line in shell.processes:
|
||||
if line.split(" ", 1)[0] == str(pid):
|
||||
return line.split(" ", 4)[4]
|
||||
return "not in the evidence"
|
||||
|
||||
|
||||
def investigated(shells: list[Shell], rung: Rung, root: str, effects: Effects) -> dict[str, typing.Any] | Failure:
|
||||
|
||||
Reference in New Issue
Block a user