diff --git a/bin/claude_code/tokens.py b/bin/claude_code/tokens.py index 527aba6d..db68eae5 100644 --- a/bin/claude_code/tokens.py +++ b/bin/claude_code/tokens.py @@ -13,6 +13,10 @@ class Tokens: read: int = 0 output: int = 0 + def context(self) -> int: + """The tokens of the prompt the message answered: its context.""" + return self.input + self.write_5m + self.write_1h + self.read + def plus(self, other: "Tokens") -> "Tokens": return Tokens(*(mine + theirs for mine, theirs in zip(dataclasses.astuple(self), dataclasses.astuple(other)))) diff --git a/bin/judge_replay/test_judge_replay.py b/bin/judge_replay/test_judge_replay.py index 4fbdf718..8971723f 100644 --- a/bin/judge_replay/test_judge_replay.py +++ b/bin/judge_replay/test_judge_replay.py @@ -140,7 +140,7 @@ class Main(unittest.TestCase): result = reply(Rung(args[args.index("--model") + 1], args[args.index("--effort") + 1]), "said Verdict: interrupt" in prompt.rsplit("latest last:", 1)[-1]) return subprocess.CompletedProcess(args, 0, json.dumps(result), "") - effects = Effects(claude=claude, claude_on_terminal=unused, discard_typed=unused, dm=unused, execv=unused, git=unused, input=unused, local_time=unused, memory=unused, now=lambda: 1790000000.0, on_terminal=unused, python=unused, sleep=unused, stream_claude=unused, tmux=unused, tmux_on_terminal=unused) + effects = Effects(claude=claude, claude_on_terminal=unused, discard_typed=unused, dm=unused, execv=unused, git=unused, input=unused, local_time=unused, memory=unused, now=lambda: 1790000000.0, on_terminal=unused, python=unused, sleep=unused, stream_claude=unused, timer=unused, tmux=unused, tmux_on_terminal=unused) out, err = io.StringIO(), io.StringIO() with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): code = main.main(argv, {}, self.home, effects) @@ -302,7 +302,7 @@ class JudgeArchivedRuns(unittest.TestCase): cwds.append(cwd) return subprocess.CompletedProcess(args, 0, json.dumps({"type": "result", "structured_output": output, "total_cost_usd": 0.25}), "") - effects = Effects(claude=claude, claude_on_terminal=unused, discard_typed=unused, dm=unused, execv=unused, git=unused, input=unused, local_time=unused, memory=unused, now=lambda: 1790000000.0, on_terminal=unused, python=unused, sleep=unused, stream_claude=unused, tmux=unused, tmux_on_terminal=unused) + effects = Effects(claude=claude, claude_on_terminal=unused, discard_typed=unused, dm=unused, execv=unused, git=unused, input=unused, local_time=unused, memory=unused, now=lambda: 1790000000.0, on_terminal=unused, python=unused, sleep=unused, stream_claude=unused, timer=unused, tmux=unused, tmux_on_terminal=unused) grounds = grounds_of(prompt, self.ENTRIES) return (grounds if isinstance(grounds, Failure) else judge_archived("/code/p", prompt, grounds, len(self.ENTRIES), REFERENCE, effects)), cwds diff --git a/bin/loop_claude/cycle/interrupt.py b/bin/loop_claude/cycle/interrupt.py index 37c42ca5..ad0c34e8 100644 --- a/bin/loop_claude/cycle/interrupt.py +++ b/bin/loop_claude/cycle/interrupt.py @@ -1,4 +1,5 @@ import dataclasses +import datetime import sys import typing import uuid @@ -6,7 +7,11 @@ import uuid from loop_claude.hand_over import claude_args, ClaudeCommand, hand_over from loop_effects.effects import Effects, say from failure import Failure +from claude_code.entries import AssistantTurn, Entry +from claude_code.reply import Limited +from claude_code.tokens import tokens_of from claude_code.transcript import human_turns, read_transcript +from loop_claude.cycle.worker import run_worker from loop_claude.stop import needs_human, needs_terminal, Stop from model_routing.ladder import Rung from loop_claude.direct_message import tell @@ -16,6 +21,10 @@ ANSWER_QUESTION = "answer the question above" # TALK, what the human types at loop-claude's terminal to open claude for the question, and ASK_ANSWER were picked on 2026-10-17 by a bench of three readers seated as the README's audience, as ~/.claude/CLAUDE.md → "Working loop" → "Bench a syntax with the people who write it." has it. TALK = "/talk" ASK_ANSWER = f"Answer ({TALK} opens claude, Enter stops the loop): " +# While the human hasn't answered, the worker's session is compacted CACHE_MARGIN seconds before its hour-long prompt cache expires: compacting reads the context from the cache, while resuming after it expired writes the whole context again. Below COMPACT_FLOOR tokens of context, that write costs less than compacting. A five-minute cache expires within the margin, so it is left as it is. lilleman chose this on 2026-11-11. +HOUR_CACHE = 3600 +CACHE_MARGIN = 600 +COMPACT_FLOOR = 60_000 @dataclasses.dataclass(frozen=True) @@ -57,12 +66,52 @@ def interrupt(question: str, answer_here: bool, session: WorkerSession, window: def answered(answer_here: bool, session: WorkerSession, effects: Effects) -> str | Talked | Stop: """The human's answer, as interrupt returns it.""" - answer = typed_answer(effects) if answer_here else TALK + answer = waited_answer(session, effects) if answer_here else TALK if answer != TALK: return answer return human_typed(session.transcript_path, lambda: claude_open(session, effects)) or Talked() +def waited_answer(session: WorkerSession, effects: Effects) -> str | Stop: + """typed_answer, compacting the worker's session where no answer has come shortly before its prompt cache expires. The claude TALK opens compacts an idle session itself.""" + entries = read_transcript(session.transcript_path) + delay = None if isinstance(entries, Failure) else compaction_delay(entries, effects.now()) + if delay is None: + return typed_answer(effects) + stop_timer = effects.timer(delay, lambda: compact(session, effects)) + try: + return typed_answer(effects) + finally: + # Waits for a compaction under way, so the worker doesn't resume the session while it is compacted. + stop_timer() + + +def compaction_delay(entries: list[Entry], now: float) -> float | None: + """Seconds from now until CACHE_MARGIN before the session's prompt cache expires. None where its context is under COMPACT_FLOOR tokens, its cache lasts five minutes, or the margin has begun.""" + turns = [entry for entry in entries if isinstance(entry, AssistantTurn) and entry.usage] + if not turns or tokens_of(turns[-1].usage).context() < COMPACT_FLOOR: + return None + # A read refreshes the cache for the lifetime it was written with. + written = next((tokens for turn in reversed(turns) if (tokens := tokens_of(turn.usage)).write_5m or tokens.write_1h), None) + if written is None or not written.write_1h: + return None + try: + last_read = datetime.datetime.fromisoformat(turns[-1].timestamp).timestamp() + except ValueError: + return None + delay = last_read + HOUR_CACHE - CACHE_MARGIN - now + return delay if delay > 0 else None + + +def compact(session: WorkerSession, effects: Effects) -> None: + """Compacts the worker's session with `claude -p /compact`, saying how that went.""" + outcome = run_worker(session.root, claude_args(session.rung, ["-p", "/compact", "--resume", str(session.id), "--output-format", "stream-json", "--verbose"]), session.env, effects) + if outcome is None: + say("compacted the worker's claude session before its prompt cache expires; still waiting for an answer", effects) + else: + say(f"could not compact the worker's claude session: {outcome.said if isinstance(outcome, Limited) else outcome.reason}; still waiting for an answer", effects) + + def typed_answer(effects: Effects) -> str | Stop: """The line typed at loop-claude's terminal, stripped. A Stop where they type no answer, or the input ends.""" try: diff --git a/bin/loop_claude/loop/usage.py b/bin/loop_claude/loop/usage.py index 9c0b5f99..bec688ce 100644 --- a/bin/loop_claude/loop/usage.py +++ b/bin/loop_claude/loop/usage.py @@ -58,6 +58,9 @@ there: - /talk: opens the worker's claude session with `claude --resume` on the terminal, to read the diff or discuss first. Once that claude exits, the judge rules on what was typed there. - Enter alone: stops the loop. +Where no answer has come 10 minutes before the worker's hour-long prompt cache expires, and its +context holds at least 60k tokens, loop-claude compacts the worker's session with +`claude -p /compact` and keeps waiting, so the resume doesn't write the whole context again. A release ready to cut opens that claude session on the terminal at once, since only the human may cut it. While claude is open, Ctrl-C reaches it alone, and the loop goes on. Where the project's config sets notifier, loop-claude also sends the human each question, naming its tmux window as diff --git a/bin/loop_claude/test_loop_claude.py b/bin/loop_claude/test_loop_claude.py index 533f8190..7463c79b 100644 --- a/bin/loop_claude/test_loop_claude.py +++ b/bin/loop_claude/test_loop_claude.py @@ -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)}) diff --git a/bin/loop_effects/effects.py b/bin/loop_effects/effects.py index 457ef24e..79ee2656 100644 --- a/bin/loop_effects/effects.py +++ b/bin/loop_effects/effects.py @@ -9,6 +9,7 @@ import signal import subprocess import sys import termios +import threading import time import typing @@ -51,6 +52,8 @@ class Effects: python: typing.Callable[[list[str], str], subprocess.CompletedProcess[str]] sleep: typing.Callable[[float], None] stream_claude: typing.Callable[[list[str], str, dict[str, str]], Streaming] + # Starts a timer, as start_timer does. + timer: typing.Callable[[float, typing.Callable[[], None]], typing.Callable[[], None]] # Runs tmux with these arguments, as run_tmux does. tmux: typing.Callable[[list[str]], subprocess.CompletedProcess[str]] # Runs tmux on the terminal, as to attach a session, and returns its exit code once the human detaches. @@ -86,6 +89,7 @@ def system_effects(backends: typing.Mapping[Notifier, Backend[typing.Any]] = BAC python=lambda args, cwd: run_python(args, cwd, PYTHON_TIMEOUT), sleep=time.sleep, stream_claude=StreamingClaude, + timer=start_timer, tmux=run_tmux, tmux_on_terminal=lambda args: subprocess.run(["tmux", *args]).returncode, ) @@ -112,6 +116,19 @@ def run_python(args: list[str], cwd: str, timeout: float) -> subprocess.Complete return subprocess.CompletedProcess(command, 1, "", f"stopped after {timeout} s") +def start_timer(delay: float, action: typing.Callable[[], None]) -> typing.Callable[[], None]: + """Runs action on a thread of its own once delay seconds have passed, and returns what stops the timer: it cancels an action not yet started, and waits for one that has started to end.""" + timer = threading.Timer(delay, action) + # A loop that exits while it waits doesn't wait for the action. + timer.daemon = True + timer.start() + + def stop() -> None: + timer.cancel() + timer.join() + return stop + + def run_tmux(args: list[str]) -> subprocess.CompletedProcess[str]: """Runs tmux and returns what it printed. A tmux that cannot start, as where none is installed, reads as exit 127, saying why on stderr.""" command = ["tmux", *args] diff --git a/bin/loop_effects/test_effects.py b/bin/loop_effects/test_effects.py index 45a0c479..6ecf9635 100644 --- a/bin/loop_effects/test_effects.py +++ b/bin/loop_effects/test_effects.py @@ -6,13 +6,14 @@ import pathlib import signal import sys import tempfile +import threading import time import unittest import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) -from loop_effects.effects import run_python, say, system_effects +from loop_effects.effects import run_python, say, start_timer, system_effects NOW = 1790000000 # How a line said at NOW opens, with clocked's local_time. @@ -134,3 +135,27 @@ class PythonEffect(unittest.TestCase): done = run_python(["-c", "pass"], gone, 60) self.assertEqual(done.returncode, 1) self.assertIn(gone, done.stderr) + + +class Timer(unittest.TestCase): + def test_runs_the_action_beside_the_caller_once_the_delay_has_passed(self): + ran = threading.Event() + start_timer(0.05, ran.set) + self.assertTrue(ran.wait(10)) + + def test_a_timer_stopped_before_its_delay_has_passed_never_runs_the_action(self): + ran = threading.Event() + start_timer(0.2, ran.set)() + self.assertFalse(ran.wait(0.5)) + + def test_stopping_a_timer_whose_action_runs_waits_for_the_action_to_end(self): + started, ended = threading.Event(), threading.Event() + + def action(): + started.set() + time.sleep(0.2) + ended.set() + stop = start_timer(0, action) + self.assertTrue(started.wait(10)) + stop() + self.assertTrue(ended.is_set()) diff --git a/bin/review_replay/test_review_replay.py b/bin/review_replay/test_review_replay.py index b54b0bd8..d86d76f9 100644 --- a/bin/review_replay/test_review_replay.py +++ b/bin/review_replay/test_review_replay.py @@ -268,7 +268,7 @@ class Main(unittest.TestCase): result = result if "api_error_status" in result else {"type": "result", "structured_output": {"found": [n + 1 for n in result["found"]]}} return subprocess.CompletedProcess(args, 0, json.dumps(result), "") - effects = Effects(claude=claude, claude_on_terminal=unused, discard_typed=unused, dm=unused, execv=unused, git=system_effects().git, input=unused, local_time=unused, memory=unused, now=lambda: 1790000000.0, on_terminal=unused, python=unused, sleep=unused, stream_claude=unused, tmux=unused, tmux_on_terminal=unused) + effects = Effects(claude=claude, claude_on_terminal=unused, discard_typed=unused, dm=unused, execv=unused, git=system_effects().git, input=unused, local_time=unused, memory=unused, now=lambda: 1790000000.0, on_terminal=unused, python=unused, sleep=unused, stream_claude=unused, timer=unused, tmux=unused, tmux_on_terminal=unused) out, err = io.StringIO(), io.StringIO() with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): code = main.main(argv, {}, self.home, effects) diff --git a/docs/reading-cost.md b/docs/reading-cost.md index 62d3cdb1..731c705c 100644 --- a/docs/reading-cost.md +++ b/docs/reading-cost.md @@ -2570,3 +2570,20 @@ Read most (opens and searches): - `bin/failure.py` 1 - `bin/markdown_section/test_markdown_section.py` 1 - `bin/run-tests` 1 + +## Reading cost, chunk 4655673a30a9, finished 2026-11-17T11:46:22Z + +111449 new input tokens before the first commit. + +Read most (opens and searches): + +- `bin/review_replay/test_review_replay.py` 6 +- `README.md` 2 +- `bin/review_replay/main.py` 2 +- `bin/review_replay/replay.py` 2 +- `docs/decisions.md` 2 +- `CHANGELOG.md` 1 +- `bin/review_replay/omit_comparison.py` 1 +- `bin/rung_replay/archive.py` 1 +- `bin/rung_replay/search.py` 1 +- `bin/rung_replay/test_rung_replay.py` 1