import contextlib import dataclasses import io import json import pathlib import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from claude_code import entries, session from failure import Failure from loop_effects.doubles import FakeEffects, NOW, ran, Streamed from agent_cycle import judge, lingering_workers, worker from claude_code.reply import Limited from machine_load import memory from model_routing.ladder import Rung from loop_effects.child_shells import Shell from text_files.utc_stamp import utc class Limits(unittest.TestCase): def test_the_worker_reports_the_latest_reset_its_stream_names(self): rejected = [{"type": "rate_limit_event", "rate_limit_info": {"status": "rejected", "resetsAt": resets_at}} for resets_at in (NOW + 60, NOW + 600)] allowed = {"type": "rate_limit_event", "rate_limit_info": {"status": "allowed", "resetsAt": NOW + 9000}} limited = {"type": "result", "api_error_status": 429, "result": "You've hit your session limit"} fake = FakeEffects(streams=[Streamed(1, rejected[1], allowed, rejected[0], limited)]) self.assertEqual(worker.run_worker("/r", [], {}, pathlib.Path("/archive"), Rung("m"), fake.effects()), Limited(NOW + 600, "You've hit your session limit")) def test_a_limit_outranks_the_exit_code_and_its_message_fits_one_line(self): def limited(**said): return {"type": "result", "api_error_status": 429, **said} self.assertEqual(worker.worker_outcome(-9, limited(result="limit\n resets 7pm"), []), Limited(None, "limit resets 7pm")) self.assertEqual(worker.worker_outcome(1, limited(result=" \n"), [NOW]), Limited(NOW, "claude gave no message")) self.assertEqual(worker.worker_outcome(0, limited(), []), Limited(None, "claude gave no message")) self.assertEqual(worker.worker_outcome(0, None, [NOW]), Failure("claude exited 0 without a result event")) self.assertIsNone(worker.worker_outcome(0, {"type": "result"}, [NOW])) def test_the_judge_reports_a_limit_whatever_its_exit_code(self): reply = json.dumps({"type": "result", "api_error_status": 429, "is_error": True, "result": "You've hit your session limit · resets 7:40pm (Europe/Stockholm)"}) fake = FakeEffects(claude=[ran(reply, code=1, stderr="limited")]) self.assertEqual(judge.run_judge("/r", [], "", judge.Grounds("", "", ""), fake.effects()), Limited(None, "You've hit your session limit · resets 7:40pm (Europe/Stockholm)")) def test_reads_only_a_rejecting_events_whole_seconds(self): for info, want in [({"status": "rejected", "resetsAt": NOW}, NOW), ({"status": "allowed", "resetsAt": NOW}, None), ({"status": "rejected", "resetsAt": True}, None), ({"status": "rejected"}, None), ("rejected", None)]: with self.subTest(info=info): self.assertEqual(entries.rejected_until({"type": "rate_limit_event", "rate_limit_info": info}), want) class LingeringWorkers(unittest.TestCase): RESULT = {"type": "result"} def worker_with(self, *events, held=()): """Runs a worker whose claude prints events and holds shells `held`; returns the claude, what loop-claude said and the record's text.""" clock = [NOW] streamed = Streamed(0, *events, clock=clock, held=held) fake = FakeEffects(streams=[streamed]) effects = dataclasses.replace(fake.effects(), now=lambda: clock[0]) with tempfile.TemporaryDirectory() as folder, contextlib.redirect_stdout(io.StringIO()) as said: # The archive folder of a project's first stop doesn't exist yet. archive = pathlib.Path(folder, "transcripts", "project") worker.run_worker("/r", [], {}, archive, Rung("m"), effects) record = archive / lingering_workers.RECORD return streamed, said.getvalue(), record.read_text() if record.exists() else "" def shell(self, written=NOW - 1000, runs_busy_tool=False, holds_slot=False): return Shell(pid=7, started=NOW - 1000, command="until false; do sleep 15; done", written=written, runs_busy_tool=runs_busy_tool, holds_slot=holds_slot) 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 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"- {utc(NOW + 300)} {line}\n") 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_a_worker_keeps_every_shell_while_one_of_them_writes_output_runs_a_busy_tool_or_holds_a_heavy_slot(self): for busy in (self.shell(written=NOW + 200), self.shell(runs_busy_tool=True), self.shell(holds_slot=True)): with self.subTest(busy=busy): streamed, _, _ = self.worker_with(self.RESULT, None, held=[self.shell(), busy]) self.assertEqual(streamed.stopped, []) def test_a_worker_keeps_a_shell_that_wrote_within_the_quiet_time_and_stops_one_that_wrote_before_it(self): # The check comes 300 s after the result, so a shell written at NOW + 300 - QUIET is the latest that is still kept. kept = self.shell(written=NOW + 300 - lingering_workers.QUIET) stopped = self.shell(written=NOW + 300 - lingering_workers.QUIET - 1) self.assertEqual(self.worker_with(self.RESULT, None, held=[kept])[0].stopped, []) self.assertEqual(self.worker_with(self.RESULT, None, held=[stopped])[0].stopped, [stopped]) def test_a_worker_reads_on_past_a_check_that_found_it_still_working(self): clock = [NOW] effects = dataclasses.replace(FakeEffects(streams=[Streamed(0, None, self.RESULT, clock=clock)]).effects(), now=lambda: clock[0]) self.assertIsNone(worker.run_worker("/r", [], {}, pathlib.Path("/archive"), Rung("m"), effects)) def test_a_stop_that_cannot_be_recorded_is_said_on_stderr_and_every_shell_is_still_stopped(self): clock = [NOW] held = [self.shell(), self.shell()] streamed = Streamed(0, self.RESULT, None, clock=clock, held=held) effects = dataclasses.replace(FakeEffects(streams=[streamed]).effects(), now=lambda: clock[0]) with tempfile.NamedTemporaryFile() as blocker, contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()) as failed: worker.run_worker("/r", [], {}, pathlib.Path(blocker.name), Rung("m"), effects) self.assertEqual(streamed.stopped, held) self.assertIn("could not record the stop", failed.getvalue()) def test_every_stop_of_a_check_is_recorded_though_the_archive_folder_exists_after_the_first(self): held = [self.shell(), self.shell()] _, said, record = self.worker_with(self.RESULT, None, held=held) self.assertEqual(record.count("stopped shell 7"), 2) class MemoryWait(unittest.TestCase): def test_a_worker_starts_only_once_a_quarter_of_memory_is_available(self): fake = FakeEffects(free_memory=[memory.Memory(available=3, total=16), memory.Memory(available=3, total=16), memory.Memory(available=4, total=16)], streams=[Streamed(0)]) with contextlib.redirect_stderr(io.StringIO()) as said: self.assertIsNone(worker.run_worker("/r", [], {}, pathlib.Path("/archive"), Rung("m"), fake.effects())) self.assertEqual([seam for seam, _ in fake.calls], ["memory", "sleep", "memory", "sleep", "memory", "stream_claude"]) self.assertEqual(fake.made("sleep"), [worker.MEMORY_POLL] * 2) self.assertEqual(said.getvalue().count("waiting"), 1, said.getvalue()) def test_a_worker_starts_at_once_where_memory_cannot_be_read(self): fake = FakeEffects(free_memory=[None], streams=[Streamed(0)]) self.assertIsNone(worker.run_worker("/r", [], {}, pathlib.Path("/archive"), Rung("m"), fake.effects())) self.assertEqual([seam for seam, _ in fake.calls], ["memory", "stream_claude"]) class WorkerArgs(unittest.TestCase): OPENED = session.session_named("A proj a-driver", pathlib.Path("/h/.claude/projects")) def test_opens_a_fresh_session_by_name_and_resumes_an_open_one(self): fresh = worker.worker_args(self.OPENED, Rung("m"), "Do it.", resume=False) self.assertEqual(fresh[fresh.index("--session-id") + 1], str(self.OPENED.id)) self.assertEqual(fresh[fresh.index("--name") + 1], "A proj a-driver") resumed = worker.worker_args(self.OPENED, Rung("m"), "Continue the task.", resume=True) self.assertEqual(resumed[resumed.index("--resume") + 1], str(self.OPENED.id)) self.assertNotIn("--session-id", resumed) self.assertEqual(resumed[resumed.index("-p") + 1], "Continue the task.") def test_disallows_a_wakeup_so_a_worker_that_ends_its_turn_exits(self): for resume in (False, True): with self.subTest(resume=resume): args = worker.worker_args(self.OPENED, Rung("m"), "Do it.", resume=resume) self.assertEqual(args[args.index("--disallowedTools") + 1], "ScheduleWakeup") if __name__ == "__main__": unittest.main()