diff --git a/bin/agent_cycle/lingering_workers.py b/bin/agent_cycle/lingering_workers.py index 6c7b14af..f83bc0a1 100644 --- a/bin/agent_cycle/lingering_workers.py +++ b/bin/agent_cycle/lingering_workers.py @@ -18,7 +18,7 @@ RECORD = "lingering-workers.md" 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.""" + """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 subagent's shells are children of the claude too, so they count like its own: a subagent that prints keeps them, and one inside a single quiet call loses it.""" if not printed_result: return now = effects.now() diff --git a/bin/loop_effects/test_effects.py b/bin/loop_effects/test_effects.py index 5292ae40..0d373048 100644 --- a/bin/loop_effects/test_effects.py +++ b/bin/loop_effects/test_effects.py @@ -27,6 +27,15 @@ def clocked(): return dataclasses.replace(system_effects(), local_time=lambda seconds: f"<{seconds}>", now=lambda: NOW) +def wait_until(condition): + """Polls up to 5 s for condition, so a test doesn't depend on how fast a process starts.""" + for _ in range(100): + if condition(): + return + time.sleep(0.05) + raise AssertionError("a process did not reach its state in time") + + class Said(unittest.TestCase): def test_opens_a_line_with_the_local_time_of_the_effects_clock(self): with contextlib.redirect_stderr(io.StringIO()) as stderr, contextlib.redirect_stdout(io.StringIO()) as stdout: @@ -140,19 +149,11 @@ class PythonEffect(unittest.TestCase): class ChildShells(unittest.TestCase): - def wait_until(self, condition): - """Polls up to 5 s for condition, so a test doesn't depend on how fast a process starts.""" - for _ in range(100): - if condition(): - return - time.sleep(0.05) - self.fail("a process did not reach its state in time") - def started(self, command, **options): """A shell running command, once a process runs below it.""" proc = subprocess.Popen(["bash", "-c", command], **options) self.addCleanup(lambda: (proc.kill(), proc.wait())) - self.wait_until(lambda: child_shells.descendants(proc.pid)) + wait_until(lambda: child_shells.descendants(proc.pid)) return proc def shell_of(self, proc): @@ -195,7 +196,7 @@ class ChildShells(unittest.TestCase): [below] = child_shells.descendants(shell.pid) child_shells.stop(self.shell_of(shell)) self.assertEqual(shell.wait(timeout=5), -signal.SIGTERM) - self.wait_until(lambda: not os.path.exists(f"/proc/{below}")) + wait_until(lambda: not os.path.exists(f"/proc/{below}")) self.assertIsNone(other.poll()) def test_reads_the_parent_and_name_of_a_process_whose_name_holds_parentheses(self): @@ -209,13 +210,13 @@ class ChildShells(unittest.TestCase): def test_reads_every_argument_of_a_process_including_bytes_that_are_not_utf8(self): proc = subprocess.Popen(["sh", "-c", "sleep 60; true", "\udcffX"]) self.addCleanup(lambda: (proc.kill(), proc.wait())) - self.wait_until(lambda: child_shells.argv_of(proc.pid)[:2] == ["sh", "-c"]) + wait_until(lambda: child_shells.argv_of(proc.pid)[:2] == ["sh", "-c"]) self.assertEqual(child_shells.argv_of(proc.pid), ["sh", "-c", "sleep 60; true", "\ufffdX"]) self.assertEqual(child_shells.argv_of(2**30), []) def test_finds_a_process_below_a_process_below_a_shell(self): shell = self.started("bash -c 'sleep 60; true'; true") - self.wait_until(lambda: len(child_shells.descendants(shell.pid)) == 2) + wait_until(lambda: len(child_shells.descendants(shell.pid)) == 2) def test_a_process_that_is_gone_holds_no_slot_and_a_vanished_child_does_not_hide_the_shells_after_it(self): self.assertFalse(child_shells.holds_slot(2**30, pathlib.Path("/"))) @@ -272,18 +273,12 @@ class StreamingClaudeLines(unittest.TestCase): streaming, _ = self.claude("bash -c 'sleep 60; true' &\necho ready\nwait") lines = streaming.lines(5) self.assertEqual(next(lines), "ready\n") - self.wait_for_shells(streaming) + wait_until(streaming.shells) [shell] = streaming.shells() self.assertEqual(shell.command, "bash -c sleep 60; true") streaming.stop(shell) self.assertEqual(streaming.wait(), 0) - def wait_for_shells(self, streaming): - for _ in range(100): - if streaming.shells(): - return - time.sleep(0.05) - self.fail("the shell did not start") class Timer(unittest.TestCase):