Files
claude-config/bin/agent_cycle/test_interrupt.py
T

230 lines
12 KiB
Python

import contextlib
import dataclasses
import io
import json
import pathlib
import shutil
import sys
import tempfile
import unittest
import uuid
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from loop_effects.doubles import assistant, FakeEffects, human, NOW, STAMP, Streamed
from failure import Failure
from agent_cycle import interrupt
from machine_load import memory
from loop_state import paths
from model_routing.ladder import Rung
from agent_cycle.stop import Stop
from agent_cycle import tmux
from text_files.utc_stamp import utc
from agent_cycle.doubles import WINDOW
class Interrupts(unittest.TestCase):
def setUp(self):
self.home = pathlib.Path(tempfile.mkdtemp())
self.addCleanup(shutil.rmtree, self.home)
self.paths = paths.paths_from(self.home)
self.transcript = self.home / "t.jsonl"
self.transcript.write_text(json.dumps(human("a")) + "\n")
self.session = interrupt.WorkerSession(str(self.home), uuid.uuid7(), Rung("m", "high"), Rung("m", "high"), str(self.transcript), {"LOOP_CLAUDE_WORKER_RUNG": "m at high effort"})
def put(self, fake, answer_here=True, effects=None, question="Which one?"):
"""interrupt's answer and what it said, without the bell it rang, which self.bells counts."""
with contextlib.redirect_stderr(io.StringIO()) as said:
answer = interrupt.interrupt(question, answer_here, self.session, WINDOW, "loop-claude", effects or fake.effects())
self.bells = said.getvalue().count(tmux.BELL)
return answer, said.getvalue().replace(tmux.BELL, "")
def typed_there(self, args):
"""The claude opened on the terminal: the human types a line in the worker's session, and it ends."""
with open(self.transcript, "a") as handle:
handle.write(json.dumps(human("cut it")) + "\n")
return 0
def worked(self, minutes_ago, written_1h=5000, read=70000, written_5m=0):
"""Appends the worker's last message, sent minutes_ago before NOW, whose usage wrote written_1h tokens to the hour-long cache, written_5m to the five-minute one, and read read tokens from the cache."""
said = assistant({"type": "text", "text": "Which one?"})
said["timestamp"] = utc(NOW - minutes_ago * 60)
said["message"] |= {"id": "msg-1", "model": "m", "usage": {"input_tokens": 2, "cache_creation": {"ephemeral_5m_input_tokens": written_5m, "ephemeral_1h_input_tokens": written_1h}, "cache_read_input_tokens": read, "output_tokens": 100}}
with open(self.transcript, "a") as handle:
handle.write(json.dumps(said) + "\n")
def fired(self, fake, answer):
"""An input that first fires every timer started, then returns answer as the line the human typed."""
def typed(prompt):
for _, action in fake.timers:
action()
return answer
return typed
def test_compacts_the_workers_session_ten_minutes_before_its_hour_long_prompt_cache_expires_and_keeps_waiting(self):
self.worked(minutes_ago=10)
fake = FakeEffects(streams=[Streamed(0)])
fake.scripts["input"] = [self.fired(fake, "Use B.")]
answer, said = self.put(fake)
self.assertEqual(answer, "Use B.")
self.assertEqual([delay for delay, _ in fake.timers], [40 * 60])
self.assertEqual([what for seam, what in fake.calls if seam == "stream_claude"], [["--model", "m", "--effort", "high", "--permission-mode", "bypassPermissions", "-p", "/compact", "--resume", str(self.session.id), "--output-format", "stream-json", "--verbose"]])
self.assertEqual(fake.worker_environments, [self.session.env])
self.assertEqual(fake.worker_folders, [self.session.root])
self.assertIn(f"\n{STAMP} compacting the worker's claude session before its prompt cache expires; an answer typed meanwhile is read once that ends\n\n{STAMP} compacted the worker's claude session\n", said)
self.assertNotIn("still waiting", said)
def test_compacts_on_the_rung_the_worker_ran_on_where_a_ruling_stepped_it_up_and_talk_opens_claude_on_the_new_rung(self):
self.worked(minutes_ago=10)
self.session = dataclasses.replace(self.session, rung=Rung("n", "medium"))
fake = FakeEffects(streams=[Streamed(0)], on_terminal_claude=[self.typed_there])
fake.scripts["input"] = [self.fired(fake, "/talk")]
self.assertEqual(self.put(fake)[0], interrupt.Talked())
(compaction,), (opened,) = fake.made("stream_claude"), fake.made("claude_on_terminal")
self.assertEqual(compaction[:4], ["--model", "m", "--effort", "high"])
self.assertEqual(opened[:4], ["--model", "n", "--effort", "medium"])
def test_compacts_until_the_prompt_cache_expires_and_not_from_then_on(self):
for name, late, compactions in (("a second before", 50 * 60 - 1, 1), ("as it expires", 50 * 60, 0)):
with self.subTest(name):
self.transcript.write_text(json.dumps(human("a")) + "\n")
self.worked(minutes_ago=10)
fake = FakeEffects(streams=[Streamed(0)])
clock = [NOW]
def typed(prompt):
clock[0] = NOW + late
for _, action in fake.timers:
action()
return "Use B."
fake.scripts["input"] = [typed]
answer, said = self.put(fake, effects=dataclasses.replace(fake.effects(), now=lambda: clock[0]))
self.assertEqual(answer, "Use B.")
self.assertEqual(len(fake.made("stream_claude")), compactions)
self.assertEqual(said.count("compact"), 2 * compactions, said)
def test_a_compaction_doesnt_wait_for_memory(self):
self.worked(minutes_ago=10)
fake = FakeEffects(streams=[Streamed(0)], free_memory=[memory.Memory(available=3, total=16)])
fake.scripts["input"] = [self.fired(fake, "Use B.")]
said = self.put(fake)[1]
self.assertEqual([seam for seam, _ in fake.calls if seam in ("memory", "sleep", "stream_claude")], ["stream_claude"])
self.assertNotIn("waiting to start the worker", said)
def test_an_answer_before_the_timer_fires_stops_it_and_doesnt_compact_anything(self):
self.worked(minutes_ago=10)
fake = FakeEffects(inputs=["Use B."])
self.assertEqual(self.put(fake)[0], "Use B.")
self.assertEqual([call for call in fake.calls if call[0] in ("stop_timer", "stream_claude")], [("stop_timer", 40 * 60)])
def test_stops_the_timer_before_talk_opens_the_workers_session(self):
self.worked(minutes_ago=10)
fake = FakeEffects(inputs=["/talk"], on_terminal_claude=[self.typed_there])
self.assertEqual(self.put(fake)[0], interrupt.Talked())
self.assertEqual([call[0] for call in fake.calls if call[0] in ("stop_timer", "claude_on_terminal")], ["stop_timer", "claude_on_terminal"])
def test_starts_no_timer_under_60k_tokens_of_context_past_the_margin_on_a_five_minute_cache_or_where_claude_opens_at_once(self):
for name, minutes_ago, written_1h, written_5m, read, answer_here in (("small", 10, 5000, 0, 54997, True), ("margin begun", 50, 5000, 0, 70000, True), ("five minutes", 1, 0, 5000, 70000, True), ("nothing written", 1, 0, 0, 70000, True), ("opens at once", 10, 5000, 0, 70000, False)):
with self.subTest(name):
self.transcript.write_text(json.dumps(human("a")) + "\n")
self.worked(minutes_ago, written_1h, read, written_5m)
fake = FakeEffects(inputs=["Use B."], on_terminal_claude=[self.typed_there])
self.put(fake, answer_here=answer_here)
self.assertEqual(fake.timers, [])
def test_starts_the_timer_at_60k_tokens_of_context(self):
self.worked(minutes_ago=10, read=54998)
fake = FakeEffects(inputs=["Use B."])
self.put(fake)
self.assertEqual([delay for delay, _ in fake.timers], [40 * 60])
def test_starts_no_timer_where_the_transcript_cannot_be_read_and_still_asks(self):
self.transcript.unlink()
fake = FakeEffects(inputs=["Use B."])
self.assertEqual(self.put(fake)[0], "Use B.")
self.assertEqual(fake.timers, [])
def test_a_compaction_that_hits_a_usage_limit_says_the_limits_message_and_keeps_waiting(self):
self.worked(minutes_ago=10)
fake = FakeEffects(streams=[Streamed(0, {"type": "result", "api_error_status": 429, "result": "You've hit your session limit"})])
fake.scripts["input"] = [self.fired(fake, "Use B.")]
answer, said = self.put(fake)
self.assertEqual(answer, "Use B.")
self.assertIn(f"\n{STAMP} could not compact the worker's claude session: You've hit your session limit\n", said)
def test_a_compaction_that_fails_says_why_and_keeps_waiting(self):
self.worked(minutes_ago=10)
fake = FakeEffects(streams=[Streamed(1)])
fake.scripts["input"] = [self.fired(fake, "Use B.")]
answer, said = self.put(fake)
self.assertEqual(answer, "Use B.")
self.assertIn(f"\n{STAMP} could not compact the worker's claude session: claude exited 1\n", said)
def test_stops_off_a_terminal_before_asking(self):
fake = FakeEffects(on_terminal=False)
self.assertEqual(self.put(fake), (Stop(2, "the human is needed to answer the question above, but stdin or stdout is not a terminal; rerun loop-claude on a terminal"), f"\n{STAMP} Which one?\n"))
self.assertEqual(fake.calls, [])
def test_dms_the_question_naming_the_project_on_a_terminal_or_off_one(self):
for on_terminal in (True, False):
with self.subTest(on_terminal=on_terminal):
fake = FakeEffects(inputs=["Use B."], on_terminal=on_terminal)
self.put(fake)
self.assertEqual(fake.dms, [f"loop-claude in {self.home} asks, in the tmux window loop-proj:item-7:\n\nWhich one?"])
def test_marks_its_window_and_rings_the_bell_while_it_waits_and_clears_the_mark_once_answered(self):
fake = FakeEffects(inputs=["Use B."])
answer, said = self.put(fake)
self.assertEqual(answer, "Use B.")
self.assertEqual(self.bells, 1)
self.assertEqual(fake.tmux_calls, [["rename-window", "-t", "%4", "item-7 ?"], ["rename-window", "-t", "%4", "item-7"]])
def test_a_loop_that_stops_unanswered_leaves_its_window_marked(self):
fake = FakeEffects(on_terminal=False)
self.put(fake)
self.assertEqual(fake.tmux_calls, [["rename-window", "-t", "%4", "item-7 ?"]])
def test_drops_the_keys_typed_before_the_question_ahead_of_the_dm_so_an_answer_typed_while_it_sends_counts(self):
fake = FakeEffects(inputs=["Use B."])
seen = []
effects = dataclasses.replace(fake.effects(), dm=lambda text, directory: seen.append(list(fake.calls)))
self.assertEqual(self.put(fake, effects=effects)[0], "Use B.")
self.assertEqual(seen, [[("discard_typed", None)]])
def test_says_why_a_dm_failed_and_still_takes_the_answer(self):
fake = FakeEffects(inputs=["Use B."], dm_failures=[Failure("cannot reach https://chat.example: timed out")])
self.assertEqual(self.put(fake), ("Use B.", f"\n{STAMP} Which one?\n{STAMP} could not DM you: cannot reach https://chat.example: timed out\n"))
def test_returns_the_line_typed_after_the_question_stripped_dropping_what_was_typed_before(self):
fake = FakeEffects(inputs=[" Use B. "])
self.assertEqual(self.put(fake), ("Use B.", f"\n{STAMP} Which one?\n"))
self.assertEqual(fake.calls, [("discard_typed", None), ("input", "Answer (/talk opens claude, Enter stops the loop): ")])
def test_talks_on_talk_or_at_once_where_the_answer_is_not_asked_for_here(self):
for typed, answer_here in (([" /talk "], True), ([], False)):
with self.subTest(typed=typed):
fake = FakeEffects(inputs=typed, on_terminal_claude=[self.typed_there])
self.assertEqual(self.put(fake, answer_here)[0], interrupt.Talked())
self.assertEqual(len(fake.made("input")), len(typed))
self.assertEqual(fake.calls[0], ("discard_typed", None))
def test_stops_on_an_empty_answer_or_the_inputs_end(self):
for typed, why, said in ((" ", "nothing was typed", ""), (EOFError(), "the input ended", "\n")):
with self.subTest(why=why):
self.assertEqual(self.put(FakeEffects(inputs=[typed])), (Stop(2, f"the human is needed to answer the question above, but {why}"), f"\n{STAMP} Which one?\n{said}"))
def test_stops_when_nothing_was_typed_in_the_session(self):
fake = FakeEffects(inputs=["/talk"], on_terminal_claude=[0])
stop, _ = self.put(fake)
self.assertEqual(fake.made("claude_on_terminal")[0][:4], ["--model", "m", "--effort", "high"])
self.assertEqual(stop, Stop(2, "the human is needed to answer the question above, but the interrupt ended without anything typed"))
def test_hands_the_claude_that_takes_the_terminal_the_workers_environment(self):
fake = FakeEffects(inputs=["/talk"], on_terminal_claude=[0])
self.put(fake)
self.assertEqual(fake.terminal_environments, [{"LOOP_CLAUDE_WORKER_RUNG": "m at high effort", "CLAUDE_CODE_SANDBOXED": "1"}])
if __name__ == "__main__":
unittest.main()