Keep a worker's shells that run a busy tool, name the shell's real command and fix the review's prose findings
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
"""Ends a worker whose claude -p printed its final result and then waits on a background shell that never ends.
|
||||
"""Ends a worker whose claude -p printed its final result and then waits on background shells that have gone quiet.
|
||||
|
||||
Claude Code's own time limit for a background command cannot do this: `BASH_DEFAULT_TIMEOUT_MS` and `BASH_MAX_TIMEOUT_MS` only raise the limit, and a call's `timeout` of up to 2 hours replaces the 10 minutes of a `claude -p` run (docs: code.claude.com/docs/en/tools-reference.md → "Time limit for background commands"). On 2026-11-18 a worker waited 95 minutes in an `until` loop whose `pgrep -f` matched its own command line.
|
||||
|
||||
The stops go to RECORD in the project's transcript archive rather than to the judge's records: the judge reads a worker's transcript, so the archive is where the pattern can be counted when it becomes a rule-hook ban (item 421).
|
||||
"""
|
||||
import pathlib
|
||||
|
||||
from loop_effects.effects import Effects, say, Streaming
|
||||
from loop_effects.child_shells import Shell
|
||||
from loop_effects.effects import Effects, Streaming, say
|
||||
from text_files.utc_stamp import utc
|
||||
|
||||
# Seconds between the checks, counted from the last line the worker printed.
|
||||
# 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
|
||||
@@ -15,12 +18,12 @@ 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 that printed its final result at settled_at and is still alive, and says so on the terminal and in record. Stops nothing where the claude is still working, or a shell writes output or holds a run-heavy slot."""
|
||||
"""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:
|
||||
return
|
||||
shells = proc.shells()
|
||||
if any(now - shell.written <= SETTLED or shell.holds_slot for shell in shells):
|
||||
if any(now - shell.written <= SETTLED or shell.runs_busy_tool for shell in shells):
|
||||
return
|
||||
for shell in shells:
|
||||
proc.stop(shell)
|
||||
@@ -28,8 +31,8 @@ def stopped_shells(proc: Streaming, settled_at: float | None, record: pathlib.Pa
|
||||
say(line, effects, stdout=True)
|
||||
record.parent.mkdir(parents=True, exist_ok=True)
|
||||
with record.open("a") as handle:
|
||||
handle.write(f"- {effects.local_time(now)} {line}\n")
|
||||
handle.write(f"- {utc(now)} {line}\n")
|
||||
|
||||
|
||||
def stop_line(shell: Shell, now: float) -> str:
|
||||
return f"stopped pid {shell.pid} ({int(now - shell.started)} s old, quiet {int(now - shell.written)} s) that held a worker after its final result: {shell.command}"
|
||||
return f"stopped shell {shell.pid}, which held a worker after its final result; it had run {int(now - shell.started)} s and written nothing for {int(now - shell.written)} s: {shell.command}"
|
||||
|
||||
@@ -25,7 +25,7 @@ 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 last line the worker printed, if it is a result: a worker that prints on is working.
|
||||
# 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
|
||||
resets: list[int] = []
|
||||
for raw in proc.lines(CHECK_EVERY):
|
||||
|
||||
@@ -48,11 +48,6 @@ failure, keeps its window, with its last lines, and its chunk keeps its items; t
|
||||
claude session is named "A <key> loop-claude <window>", so a rerun resumes a chunk left open, and is
|
||||
deleted when the chunk finishes. A fresh worker is told the items the project's other chunks hold,
|
||||
running or stopped: each one's own and those on its latest `Chunk items:` line, and leaves them out.
|
||||
A worker whose claude printed its final result and still waits on a background shell that has
|
||||
written nothing for 3 minutes loses that shell, found at the next 5-minute check. A shell that
|
||||
writes, or holds a run-heavy slot, keeps the worker alive. Each stop is printed and appended to
|
||||
lingering-workers.md in the project's transcript archive
|
||||
(bin/loop_claude/cycle/lingering_workers.py).
|
||||
|
||||
A question only the human can answer is an interrupt (bin/loop_claude/cycle/interrupt.py).
|
||||
loop-claude prints the question in the chunk's window and rings its bell. Until it is answered the
|
||||
@@ -83,6 +78,13 @@ transcript is copied into a folder per chunk under $LOOP_CLAUDE_TRANSCRIPTS/<key
|
||||
then each verdict, quoted answer, rejected judge reply, step-up and stop, each opened by its UTC
|
||||
time. The layout, and when a copy is replaced, are in bin/transcript_archive/__init__.py.
|
||||
|
||||
A worker's claude can print its final result and then keep waiting on a background shell that
|
||||
never ends. Every 5 minutes without output from that claude, loop-claude stops each of its shells
|
||||
and what runs below them, once the result is over 3 minutes old and no shell has written output in
|
||||
the last 3 minutes or runs run-heavy or chunk-pr. It prints each stop and appends it, with the UTC
|
||||
time, to lingering-workers.md at the project's folder in that archive
|
||||
(bin/loop_claude/cycle/lingering_workers.py).
|
||||
|
||||
Before each worker starts, loop-claude waits until at least a quarter of the machine's memory is
|
||||
available (bin/machine_load/memory.py).
|
||||
|
||||
|
||||
@@ -791,30 +791,30 @@ class LingeringWorkers(unittest.TestCase):
|
||||
record = pathlib.Path(folder, lingering_workers.RECORD)
|
||||
return streamed, said.getvalue(), record.read_text() if record.exists() else ""
|
||||
|
||||
def shell(self, written=NOW - 1000, holds_slot=False):
|
||||
return Shell(pid=7, started=NOW - 1000, command="until false; do sleep 15; done", written=written, holds_slot=holds_slot)
|
||||
def shell(self, written=NOW - 1000, runs_busy_tool=False):
|
||||
return Shell(pid=7, started=NOW - 1000, command="until false; do sleep 15; done", written=written, runs_busy_tool=runs_busy_tool)
|
||||
|
||||
def test_a_worker_that_lingers_after_its_result_loses_its_quiet_shell_and_the_stop_is_said_and_recorded(self):
|
||||
def test_a_worker_that_lingers_after_its_result_loses_its_quiet_shell_and_the_stop_is_said_and_recorded_in_utc(self):
|
||||
shell = self.shell()
|
||||
streamed, said, record = self.worker_with(self.RESULT, None, held=[shell])
|
||||
self.assertEqual(streamed.stopped, [shell])
|
||||
line = f"stopped pid 7 (1300 s old, quiet 1300 s) that held a worker after its final result: {shell.command}"
|
||||
line = f"stopped shell 7, which held a worker after its final result; it had run 1300 s and written nothing for 1300 s: {shell.command}"
|
||||
self.assertEqual(said, f"<{NOW + 300}> {line}\n")
|
||||
self.assertEqual(record, f"- <{NOW + 300}> {line}\n")
|
||||
self.assertEqual(record, f"- {utc(NOW + 300)} {line}\n")
|
||||
|
||||
def test_nothing_is_stopped_where_the_worker_printed_after_its_result_or_no_check_came_due(self):
|
||||
def test_a_worker_keeps_its_shells_where_it_printed_after_its_result_or_printed_none_or_no_check_came_due(self):
|
||||
for events in ((self.RESULT, {"type": "assistant", "message": {"content": []}}, None), (self.RESULT,), (None,), ({"type": "assistant", "message": {"content": []}}, None)):
|
||||
with self.subTest(events=events):
|
||||
streamed, said, record = self.worker_with(*events, held=[self.shell()])
|
||||
self.assertEqual((streamed.stopped, said, record), ([], "", ""))
|
||||
|
||||
def test_nothing_is_stopped_while_a_shell_writes_output_or_holds_a_heavy_slot(self):
|
||||
for busy in (self.shell(written=NOW + 200), self.shell(holds_slot=True)):
|
||||
def test_a_worker_keeps_every_shell_while_one_of_them_writes_output_or_runs_a_busy_tool(self):
|
||||
for busy in (self.shell(written=NOW + 200), self.shell(runs_busy_tool=True)):
|
||||
with self.subTest(busy=busy):
|
||||
streamed, _, _ = self.worker_with(self.RESULT, None, held=[self.shell(), busy])
|
||||
self.assertEqual(streamed.stopped, [])
|
||||
|
||||
def test_a_check_before_the_result_has_stood_long_enough_stops_nothing(self):
|
||||
def test_a_worker_keeps_its_shells_until_its_result_has_stood_longer_than_the_settled_time(self):
|
||||
fake = FakeEffects()
|
||||
effects = dataclasses.replace(fake.effects(), now=lambda: NOW + lingering_workers.SETTLED)
|
||||
proc = Streamed(0, held=[self.shell()])
|
||||
|
||||
Reference in New Issue
Block a user