90 lines
4.9 KiB
Python
90 lines
4.9 KiB
Python
import contextlib
|
|
import dataclasses
|
|
import io
|
|
import json
|
|
import pathlib
|
|
import tempfile
|
|
import unittest
|
|
|
|
from agent_cycle import stuck_workers, worker
|
|
from loop_claude.test_loop_claude import FakeEffects, NOW, Streamed, ran
|
|
from loop_effects.child_shells import Shell
|
|
from model_routing.ladder import Rung
|
|
from text_files.utc_stamp import utc
|
|
|
|
RUNG = Rung("claude-opus-5-5", "medium")
|
|
ASSISTANT = {"type": "assistant", "message": {"content": []}}
|
|
|
|
|
|
def verdict(said="stuck", process=8, why="pytest waits on a shell that ignores SIGINT"):
|
|
return ran(json.dumps({"type": "result", "structured_output": {"verdict": said, "process": process, "why": why}}))
|
|
|
|
|
|
def shell(cpu=2.0, started=NOW - 1000, written=NOW - 1000, runs_busy_tool=False):
|
|
return Shell(pid=7, started=started, command="bin/run-tests", written=written, runs_busy_tool=runs_busy_tool, cpu=cpu, processes=("8 S 2.0 s python3 -m pytest",), output="collected 40 items")
|
|
|
|
|
|
class StuckWorkers(unittest.TestCase):
|
|
def worker_with(self, *events, held=(), replies=()):
|
|
"""Runs a worker whose claude prints events and holds the shells held, while each investigation replies in turn; returns the claude, the fake, what was said on stdout and stderr and the record's text."""
|
|
clock = [NOW]
|
|
streamed = Streamed(0, *events, clock=clock, held=held)
|
|
fake = FakeEffects(streams=[streamed], claude=replies)
|
|
effects = dataclasses.replace(fake.effects(), now=lambda: clock[0])
|
|
with tempfile.TemporaryDirectory() as folder, contextlib.redirect_stdout(io.StringIO()) as said, contextlib.redirect_stderr(io.StringIO()) as failed:
|
|
archive = pathlib.Path(folder, "transcripts", "project")
|
|
worker.run_worker("/r", [], {}, archive, RUNG, effects)
|
|
record = archive / stuck_workers.RECORD
|
|
return streamed, fake, said.getvalue(), failed.getvalue(), record.read_text() if record.exists() else ""
|
|
|
|
def test_a_silent_worker_whose_shell_used_no_cpu_since_the_last_check_is_investigated_and_the_verdict_said_and_recorded_without_a_kill(self):
|
|
streamed, fake, said, _, record = self.worker_with(ASSISTANT, None, None, held=[shell()], replies=[verdict()])
|
|
[(args, prompt)] = fake.made("claude")
|
|
self.assertEqual(args[args.index("--model") + 1 :][:3], ["claude-opus-5-5", "--effort", "medium"])
|
|
self.assertEqual(args[args.index("--tools") + 1], "")
|
|
for evidence in ("bin/run-tests", "8 S 2.0 s python3 -m pytest", "collected 40 items", "working", "waiting on a person", "stuck"):
|
|
self.assertIn(evidence, prompt)
|
|
line = "stuck check: shell 7 has run 1600 s, written nothing for 1600 s and used no CPU for 300 s: bin/run-tests; a headless claude judged the worker stuck, held by process 8: pytest waits on a shell that ignores SIGINT"
|
|
self.assertEqual(said, f"<{NOW + 600}> {line}\n")
|
|
self.assertEqual(record, f"- {utc(NOW + 600)} {line}\n")
|
|
self.assertEqual(streamed.stopped, [])
|
|
|
|
def test_a_shell_older_than_a_chunk_usually_runs_is_investigated_at_the_first_check(self):
|
|
_, fake, said, _, _ = self.worker_with(ASSISTANT, None, held=[shell(started=NOW - stuck_workers.OLD, written=NOW + 290)], replies=[verdict("working", 7, "the tests still print")])
|
|
self.assertEqual(len(fake.made("claude")), 1)
|
|
self.assertIn(f"shell 7 has run {stuck_workers.OLD + 300} s, written nothing for 10 s: bin/run-tests; a headless claude judged the worker working, held by process 7: the tests still print", said)
|
|
|
|
def test_a_worker_whose_shells_show_no_sign_is_not_investigated(self):
|
|
for events, held in (
|
|
((ASSISTANT, None, None), [shell(runs_busy_tool=True)]),
|
|
((ASSISTANT, None, None), [shell(written=NOW + 600 - 179)]),
|
|
((ASSISTANT, None), [shell()]),
|
|
((ASSISTANT, None, None), []),
|
|
(({"type": "result"}, None, None), [shell()]),
|
|
((ASSISTANT, None), [shell(started=NOW + 300 - stuck_workers.OLD)]),
|
|
):
|
|
with self.subTest(events=events, held=held):
|
|
_, fake, said, _, record = self.worker_with(*events, held=held)
|
|
self.assertEqual((fake.made("claude"), record), ([], ""))
|
|
|
|
def test_a_shell_that_used_cpu_since_the_last_check_is_not_investigated(self):
|
|
clock = [NOW]
|
|
used = [shell(cpu=2.0), shell(cpu=2.5)]
|
|
streamed = Streamed(0, ASSISTANT, None, None, clock=clock)
|
|
streamed.shells = lambda: [used.pop(0)]
|
|
fake = FakeEffects(streams=[streamed])
|
|
with contextlib.redirect_stdout(io.StringIO()):
|
|
worker.run_worker("/r", [], {}, pathlib.Path("/archive"), RUNG, dataclasses.replace(fake.effects(), now=lambda: clock[0]))
|
|
self.assertEqual(fake.made("claude"), [])
|
|
|
|
def test_an_investigation_that_fails_or_answers_outside_the_schema_is_said_on_stderr(self):
|
|
for reply, reason in ((ran(code=1, stderr="no model"), "no model"), (verdict("hung"), "answered outside its schema")):
|
|
with self.subTest(reason=reason):
|
|
_, _, said, failed, record = self.worker_with(ASSISTANT, None, None, held=[shell()], replies=[reply])
|
|
self.assertEqual((said, record), ("", ""))
|
|
self.assertIn(f"could not judge whether the worker is stuck: {reason}", failed)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|