Compact a waiting worker's session shortly before its prompt cache expires
CI / tests (pull_request) Failing after 3m16s
CI / tests (pull_request) Failing after 3m16s
This commit is contained in:
@@ -112,6 +112,8 @@ class FakeEffects:
|
||||
self.worker_environments = []
|
||||
self.terminal_environments = []
|
||||
self.terminal_folders = []
|
||||
# Each timer started, as its delay and action, which a test runs where the timer fires.
|
||||
self.timers = []
|
||||
|
||||
def effects(self):
|
||||
return Effects(
|
||||
@@ -129,10 +131,15 @@ class FakeEffects:
|
||||
python=lambda args, cwd: self.answer("python", (args, cwd)),
|
||||
sleep=lambda seconds: self.calls.append(("sleep", seconds)),
|
||||
stream_claude=self.stream,
|
||||
timer=self.timer,
|
||||
tmux=self.tmux,
|
||||
tmux_on_terminal=lambda args: self.tmux(args).returncode,
|
||||
)
|
||||
|
||||
def timer(self, delay, action):
|
||||
self.timers.append((delay, action))
|
||||
return lambda: self.calls.append(("stop_timer", delay))
|
||||
|
||||
def tmux(self, args):
|
||||
self.tmux_calls.append(args)
|
||||
if args[0] == "list-windows":
|
||||
@@ -798,6 +805,62 @@ class Interrupts(unittest.TestCase):
|
||||
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 the human types answer at, after every timer started has fired."""
|
||||
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.assertIn(f"{STAMP} compacted the worker's claude session before its prompt cache expires; still waiting for an answer\n", said)
|
||||
|
||||
def test_an_answer_before_the_timer_fires_stops_it_and_compacts_nothing(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, 50000, True), ("late", 51, 5000, 0, 70000, True), ("five minutes", 1, 0, 5000, 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_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"{STAMP} could not compact the worker's claude session: claude exited 1; still waiting for an answer\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"))
|
||||
@@ -1965,6 +2028,9 @@ class Spend(unittest.TestCase):
|
||||
self.assertEqual(tokens_of({"input_tokens": 1, "cache_creation_input_tokens": 9}), Tokens(1, 9, 0, 0, 0))
|
||||
self.assertEqual(tokens_of(None), Tokens())
|
||||
|
||||
def test_counts_a_messages_context_as_every_prompt_token_and_no_output(self):
|
||||
self.assertEqual(Tokens(1, 2, 3, 4, 5).context(), 10)
|
||||
|
||||
def test_counts_a_message_written_in_the_stamped_second_or_without_a_readable_time_as_before_since(self):
|
||||
thread = [turn("m1", "a", "2026-11-01T10:00:01Z", output_tokens=7), turn("m2", "a", "not a time", output_tokens=3), turn("m3", "a", "2026-11-01T10:00:02Z", output_tokens=1)]
|
||||
self.assertEqual(spend.spent("2026-11-01T10:00:01Z", thread, [], "j", Tokens()), {("worker", "a"): Tokens(output=1)})
|
||||
|
||||
Reference in New Issue
Block a user