Answer stability round 1: 1 🔴, 4 🟡 (1 misread); keep a shell whose command holds a heavy slot, and say a failed record write on stderr

This commit is contained in:
2026-11-18 20:12:40 +01:00
parent abae3a0559
commit a7439c2e00
6 changed files with 108 additions and 66 deletions
+13 -10
View File
@@ -12,26 +12,29 @@ from text_files.utc_stamp import utc
# Seconds without a line from the worker before a check, so a check is 5 minutes after its last line.
CHECK_EVERY = 300
# Seconds a final result must have stood, and a shell's output must have been quiet, before a shell is stopped.
SETTLED = 180
# Seconds a shell's output must have been quiet before the shell is stopped.
QUIET = 180
RECORD = "lingering-workers.md"
def stopped_shells(proc: Streaming, settled_at: float | None, record: pathlib.Path, effects: Effects) -> None:
"""Stops the shells of a claude whose final result came at settled_at and that is still alive, saying each stop on the terminal and in record. A claude that is still working (settled_at is None) or whose result came SETTLED seconds ago or less keeps its shells, and so does one with a shell that wrote output within SETTLED seconds or runs a busy tool, as `Shell.runs_busy_tool` says."""
now = effects.now()
if settled_at is None or now - settled_at <= SETTLED:
def stopped_shells(proc: Streaming, printed_result: bool, record: pathlib.Path, effects: Effects) -> None:
"""Stops the shells of a claude that is still alive CHECK_EVERY seconds after it printed its final result (printed_result), saying each stop on the terminal and in record. A claude whose latest line is not a result is working and keeps its shells, and so does one with a shell that wrote output within QUIET seconds or runs a busy tool, as `Shell.runs_busy_tool` says. A shell a background subagent started is not one of the claude's children, so it is never stopped."""
if not printed_result:
return
now = effects.now()
shells = proc.shells()
if any(now - shell.written <= SETTLED or shell.runs_busy_tool for shell in shells):
if any(now - shell.written <= QUIET or shell.runs_busy_tool for shell in shells):
return
for shell in shells:
proc.stop(shell)
line = stop_line(shell, now)
say(line, effects, stdout=True)
record.parent.mkdir(parents=True, exist_ok=True)
with record.open("a") as handle:
handle.write(f"- {utc(now)} {line}\n")
try:
record.parent.mkdir(parents=True, exist_ok=True)
with record.open("a") as handle:
handle.write(f"- {utc(now)} {line}\n")
except OSError as failure:
say(f"could not record the stop in {record}: {failure.strerror}", effects)
def stop_line(shell: Shell, now: float) -> str:
+5 -5
View File
@@ -25,16 +25,16 @@ def run_worker(root: str, args: list[str], env: dict[str, str], project_archive:
except OSError as failure:
return Failure(f"could not start claude: {failure.strerror}")
result: dict | None = None
# When the worker printed its latest result; None while its latest line is something else, since a worker that prints on is working.
settled_at: float | None = None
# Whether the worker's latest line is its result; a worker that prints on is working.
printed_result = False
resets: list[int] = []
for raw in proc.lines(CHECK_EVERY):
if raw is None:
stopped_shells(proc, settled_at, project_archive / RECORD, effects)
stopped_shells(proc, printed_result, project_archive / RECORD, effects)
continue
event = json_object(raw) or {}
settled_at = effects.now() if ends_stream(event) else None
if ends_stream(event):
printed_result = ends_stream(event)
if printed_result:
result = event
if (resets_at := rejected_until(event)) is not None:
resets.append(resets_at)