From e60e891251222b58f8dcded90266be1595cd89ba Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Tue, 17 Nov 2026 13:00:25 +0100 Subject: [PATCH 01/11] Compact a waiting worker's session shortly before its prompt cache expires --- bin/claude_code/tokens.py | 4 ++ bin/judge_replay/test_judge_replay.py | 4 +- bin/loop_claude/cycle/interrupt.py | 51 ++++++++++++++++++- bin/loop_claude/loop/usage.py | 3 ++ bin/loop_claude/test_loop_claude.py | 66 +++++++++++++++++++++++++ bin/loop_effects/effects.py | 17 +++++++ bin/loop_effects/test_effects.py | 27 +++++++++- bin/review_replay/test_review_replay.py | 2 +- docs/reading-cost.md | 17 +++++++ 9 files changed, 186 insertions(+), 5 deletions(-) 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 From e5258525e74712b89b711db384f142b9b85a9e5a Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Tue, 17 Nov 2026 14:05:14 +0100 Subject: [PATCH 02/11] Sort interrupt's imports by their first binding --- bin/loop_claude/cycle/interrupt.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/loop_claude/cycle/interrupt.py b/bin/loop_claude/cycle/interrupt.py index ad0c34e8..33281be5 100644 --- a/bin/loop_claude/cycle/interrupt.py +++ b/bin/loop_claude/cycle/interrupt.py @@ -4,18 +4,18 @@ import sys import typing import uuid +from claude_code.entries import AssistantTurn, Entry 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 claude_code.reply import Limited from loop_claude.stop import needs_human, needs_terminal, Stop +from loop_claude.cycle.worker import run_worker from model_routing.ladder import Rung from loop_claude.direct_message import tell from loop_claude import tmux +from claude_code.tokens import tokens_of 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. From 444f5824d983aea9d257ae086c97bfa4691a98dc Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 19 Nov 2026 04:00:32 +0100 Subject: [PATCH 03/11] =?UTF-8?q?Answer=20security=20round=201:=200=20?= =?UTF-8?q?=F0=9F=94=B4,=200=20=F0=9F=9F=A1=20(0=20misread);=20no=20findin?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From 30595cfdf48fed51de8fe1d7052cf6c54dd38d76 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 19 Nov 2026 04:11:13 +0100 Subject: [PATCH 04/11] Test the compaction's boundaries, an unreadable transcript, a usage limit and the timer's daemon thread --- bin/loop_claude/test_loop_claude.py | 25 ++++++++++++++++++++++++- bin/loop_effects/test_effects.py | 7 +++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/bin/loop_claude/test_loop_claude.py b/bin/loop_claude/test_loop_claude.py index 7463c79b..934da642 100644 --- a/bin/loop_claude/test_loop_claude.py +++ b/bin/loop_claude/test_loop_claude.py @@ -110,6 +110,7 @@ class FakeEffects: self.free_memory = list(free_memory) self.terminal = on_terminal self.worker_environments = [] + self.worker_folders = [] self.terminal_environments = [] self.terminal_folders = [] # Each timer started, as its delay and action, which a test runs where the timer fires. @@ -161,6 +162,7 @@ class FakeEffects: def stream(self, args, cwd, env): self.worker_environments.append(env) + self.worker_folders.append(cwd) return self.answer("stream_claude", args) def answer(self, seam, what): @@ -830,6 +832,7 @@ class Interrupts(unittest.TestCase): 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"{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): @@ -845,7 +848,7 @@ class Interrupts(unittest.TestCase): 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)): + 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) @@ -853,6 +856,26 @@ class Interrupts(unittest.TestCase): 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"{STAMP} could not compact the worker's claude session: You've hit your session limit; still waiting for an answer\n", said) + def test_a_compaction_that_fails_says_why_and_keeps_waiting(self): self.worked(minutes_ago=10) fake = FakeEffects(streams=[Streamed(1)]) diff --git a/bin/loop_effects/test_effects.py b/bin/loop_effects/test_effects.py index 6ecf9635..f1e98449 100644 --- a/bin/loop_effects/test_effects.py +++ b/bin/loop_effects/test_effects.py @@ -143,6 +143,13 @@ class Timer(unittest.TestCase): start_timer(0.05, ran.set) self.assertTrue(ran.wait(10)) + def test_the_systems_timer_runs_on_a_daemon_thread_so_a_loop_that_exits_while_it_waits_does_not_wait_for_it(self): + self.assertIs(system_effects().timer, start_timer) + before = set(threading.enumerate()) + stop = start_timer(60, lambda: None) + self.addCleanup(stop) + self.assertEqual([thread.daemon for thread in set(threading.enumerate()) - before], [True]) + def test_a_timer_stopped_before_its_delay_has_passed_never_runs_the_action(self): ran = threading.Event() start_timer(0.2, ran.set)() From 78dd557057709832e1f1ecf99e727f20b967387a Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 19 Nov 2026 04:42:14 +0100 Subject: [PATCH 05/11] =?UTF-8?q?Answer=20stability=20round=201:=200=20?= =?UTF-8?q?=F0=9F=94=B4,=202=20=F0=9F=9F=A1=20(1=20misread);=20compact=20o?= =?UTF-8?q?n=20the=20worker's=20rung,=20announce=20the=20compaction=20and?= =?UTF-8?q?=20skip=20it=20once=20the=20cache=20expired?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/loop_claude/cycle/interrupt.py | 23 +++++--- bin/loop_claude/cycle/run.py | 20 +++---- bin/loop_claude/cycle/states.py | 23 ++++---- bin/loop_claude/cycle/worker.py | 5 ++ bin/loop_claude/test_loop_claude.py | 82 +++++++++++++++++++++++++---- 5 files changed, 113 insertions(+), 40 deletions(-) diff --git a/bin/loop_claude/cycle/interrupt.py b/bin/loop_claude/cycle/interrupt.py index 33281be5..f2db7e23 100644 --- a/bin/loop_claude/cycle/interrupt.py +++ b/bin/loop_claude/cycle/interrupt.py @@ -11,7 +11,7 @@ from failure import Failure from claude_code.transcript import human_turns, read_transcript from claude_code.reply import Limited from loop_claude.stop import needs_human, needs_terminal, Stop -from loop_claude.cycle.worker import run_worker +from loop_claude.cycle.worker import run_claude from model_routing.ladder import Rung from loop_claude.direct_message import tell from loop_claude import tmux @@ -33,6 +33,8 @@ class WorkerSession: root: str id: uuid.UUID rung: Rung + # The rung the worker last ran on: below rung where the ruling that interrupts stepped the worker up. + ran_on: Rung transcript_path: str # Added to the environment of the worker's claude, and of the claude /talk opens. env: dict[str, str] @@ -75,10 +77,11 @@ def answered(answer_here: bool, session: WorkerSession, effects: Effects) -> str 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()) + now = effects.now() + delay = None if isinstance(entries, Failure) else compaction_delay(entries, now) if delay is None: return typed_answer(effects) - stop_timer = effects.timer(delay, lambda: compact(session, effects)) + stop_timer = effects.timer(delay, lambda: compact(session, now + delay + CACHE_MARGIN, effects)) try: return typed_answer(effects) finally: @@ -103,13 +106,17 @@ def compaction_delay(entries: list[Entry], now: float) -> float | None: 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) +def compact(session: WorkerSession, expires: float, effects: Effects) -> None: + """Compacts the worker's session with `claude -p /compact`, saying that it starts and how it went. Each line opens on a line of its own, below the open ASK_ANSWER. From expires on, when the prompt cache has expired, it doesn't do anything: a timer fires late where the machine was suspended.""" + if effects.now() >= expires: + return + say("\ncompacting the worker's claude session before its prompt cache expires; an answer typed meanwhile is read once that ends", effects) + # On the rung the worker ran on, not the one a ruling stepped it up to: the prompt cache is kept per model, and another effort may miss it too, so another rung would read the whole context at full price. Without run_worker's wait for memory, which could outlast the cache. + outcome = run_claude(session.root, claude_args(session.ran_on, ["-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) + say("\ncompacted the worker's claude session", 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) + say(f"\ncould not compact the worker's claude session: {outcome.said if isinstance(outcome, Limited) else outcome.reason}", effects) def typed_answer(effects: Effects) -> str | Stop: diff --git a/bin/loop_claude/cycle/run.py b/bin/loop_claude/cycle/run.py index e5cfed01..6e8f2ea3 100644 --- a/bin/loop_claude/cycle/run.py +++ b/bin/loop_claude/cycle/run.py @@ -74,8 +74,8 @@ def run_cycle(cycle: Cycle) -> Stop | Concluded: if isinstance(event, Stop): return stopped(cycle, event, event.reason) # Only a ruling's step-up changes the route within a cycle, here, once rule has archived the ruling on the route before it. No transition reads the current route, so Cycle holds it, not the states. - if isinstance(event, Ruled) and event.stepped_to: - cycle = dataclasses.replace(cycle, route=event.stepped_to) + if isinstance(event, Ruled) and event.stepped: + cycle = dataclasses.replace(cycle, route=event.stepped.after) state = transition(state, event) except KeyboardInterrupt: return stopped(cycle, Stop(130, ""), "Ctrl-C") @@ -109,8 +109,8 @@ def step(cycle: Cycle, state: Active) -> Event | Stop: return work(cycle, answered, stepped) case Judging(transcript_path, quoted_answers): return rule(cycle, transcript_path, quoted_answers) - case Interrupting(transcript_path, ruling, _, _): - return ask(cycle, transcript_path, ruling) + case Interrupting(transcript_path, ruling, _, stepped_from): + return ask(cycle, transcript_path, ruling, stepped_from) case Closing(transcript_path, verdict): return close(cycle, transcript_path, verdict) case _: @@ -132,8 +132,8 @@ def worker_environment(cycle: Cycle) -> dict[str, str]: return {**worker_rungs.environment(cycle.project, cycle.route), AUTO_COMPACT_WINDOW: str(COMPACT_WINDOW)} -def worker_session(cycle: Cycle, transcript_path: str) -> WorkerSession: - return WorkerSession(cycle.root, cycle.session.id, cycle.route.rung, transcript_path, worker_environment(cycle)) +def worker_session(cycle: Cycle, transcript_path: str, stepped_from: Rung | None) -> WorkerSession: + return WorkerSession(cycle.root, cycle.session.id, cycle.route.rung, stepped_from or cycle.route.rung, transcript_path, worker_environment(cycle)) def worker_start(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None, stepped: bool) -> list[str]: @@ -164,7 +164,7 @@ def rule(cycle: Cycle, transcript_path: str, quoted_answers: int) -> Ruled | Sto stepped = ruling_step_up(cycle, verdict) if failure := archive_stepped(cycle, transcript_path, ruling_outcome(cycle, verdict, quoted_answers, annotation), stepped, verdict.judged.run): return Stop(1, failure.reason) - return Ruled(verdict, stepped.after if stepped else None) + return Ruled(verdict, stepped) def annotation_of(cycle: Cycle, transcript_path: str, entries: list[Entry], judged: Judged) -> outcomes.Annotation | Failure: @@ -193,10 +193,10 @@ def keep_rejection(cycle: Cycle, transcript_path: str, rejected: Rejected) -> St return None -def ask(cycle: Cycle, transcript_path: str, ruling: Interrupt | QuotedAnswer) -> HumanTyped | HumanTalked | Stop: - """The human's answer to a ruling that rule archived as it ruled; the human's answer then grows the transcript.""" +def ask(cycle: Cycle, transcript_path: str, ruling: Interrupt | QuotedAnswer, stepped_from: Rung | None) -> HumanTyped | HumanTalked | Stop: + """The human's answer to a ruling that rule archived as it ruled; the human's answer then grows the transcript. stepped_from is the rung the worker ran on, where the ruling stepped it up.""" # Only the human may cut a release, so a release ready to cut opens claude's session at once. - answer = interrupt(question_for(ruling), not (isinstance(ruling, Interrupt) and ruling.release_ready), worker_session(cycle, transcript_path), cycle.window, cycle.effects) + answer = interrupt(question_for(ruling), not (isinstance(ruling, Interrupt) and ruling.release_ready), worker_session(cycle, transcript_path, stepped_from), cycle.window, cycle.effects) match answer: case Stop(): return answer diff --git a/bin/loop_claude/cycle/states.py b/bin/loop_claude/cycle/states.py index 78a7e328..cf24478f 100644 --- a/bin/loop_claude/cycle/states.py +++ b/bin/loop_claude/cycle/states.py @@ -1,7 +1,8 @@ import dataclasses from loop_claude.verdicts import Finished, Interrupt, QuotedAnswer, Unfinished, Verdict -from model_routing.route import Route +from model_routing.ladder import Rung +from loop_claude.routing import StepUp # A backstop for what the judge misses, such as a reworded repeat, so the worker cannot loop unattended on answers quoted from the premises or the records. QUOTED_ANSWERS_PER_CYCLE = 3 @@ -36,8 +37,8 @@ class Interrupting: transcript_path: str ruling: Interrupt | QuotedAnswer quoted_answers: int = 0 - # Whether the ruling stepped the worker up a rung. It holds through the answer the human types, and drops on /talk. - stepped: bool = False + # The rung the worker ran on, where the ruling stepped it up from there; None where the rung holds. It holds through the answer the human types, and drops on /talk. + stepped_from: Rung | None = None @dataclasses.dataclass(frozen=True) @@ -63,8 +64,8 @@ class WorkerRan: @dataclasses.dataclass(frozen=True) class Ruled: verdict: Verdict - # The route an interrupt's or a quoted answer's escalation stepped the worker up to, which the cycle runs on from here; None where the route holds, and always for a finished or unfinished verdict. - stepped_to: Route | None = None + # The step-up an interrupt's or a quoted answer's escalation made, whose `after` the cycle runs on from here; None where the route holds, and always for a finished or unfinished verdict. + stepped: StepUp | None = None @dataclasses.dataclass(frozen=True) @@ -94,14 +95,14 @@ def transition(state: State, event: Event) -> State: match state, event: case Working(_, quoted_answers), WorkerRan(transcript_path): return Judging(transcript_path, quoted_answers) - case Judging(transcript_path, quoted_answers), Ruled(Interrupt() | QuotedAnswer() as ruling, stepped_to) if to_the_human(ruling, quoted_answers): - return Interrupting(transcript_path, ruling, quoted_answers, stepped_to is not None) - case Judging(_, quoted_answers), Ruled(QuotedAnswer() as answered, stepped_to): - return Working(answered, quoted_answers + 1, stepped_to is not None) + case Judging(transcript_path, quoted_answers), Ruled(Interrupt() | QuotedAnswer() as ruling, stepped) if to_the_human(ruling, quoted_answers): + return Interrupting(transcript_path, ruling, quoted_answers, stepped.before.rung if stepped else None) + case Judging(_, quoted_answers), Ruled(QuotedAnswer() as answered, stepped): + return Working(answered, quoted_answers + 1, stepped is not None) case Judging(transcript_path, _), Ruled(Finished() | Unfinished() as verdict): return Closing(transcript_path, verdict) - case Interrupting(_, _, quoted_answers, stepped), HumanTyped(typed): - return Working(typed, quoted_answers, stepped) + case Interrupting(_, _, quoted_answers, stepped_from), HumanTyped(typed): + return Working(typed, quoted_answers, stepped_from is not None) # The human worked in the worker's claude session, so the judge rules again on the transcript it leaves. case Interrupting(transcript_path, _, quoted_answers), HumanTalked(): return Judging(transcript_path, quoted_answers) diff --git a/bin/loop_claude/cycle/worker.py b/bin/loop_claude/cycle/worker.py index 2ca284fd..6613a4e0 100644 --- a/bin/loop_claude/cycle/worker.py +++ b/bin/loop_claude/cycle/worker.py @@ -17,6 +17,11 @@ COMPACT_WINDOW = 200_000 def run_worker(root: str, args: list[str], env: dict[str, str], effects: Effects) -> Failure | Limited | None: wait_for_memory(effects) + return run_claude(root, args, env, effects) + + +def run_claude(root: str, args: list[str], env: dict[str, str], effects: Effects) -> Failure | Limited | None: + """Runs a headless claude at once, printing what it says, and returns how it failed, or None.""" try: proc = effects.stream_claude(args, root, env) except OSError as failure: diff --git a/bin/loop_claude/test_loop_claude.py b/bin/loop_claude/test_loop_claude.py index 934da642..8124a097 100644 --- a/bin/loop_claude/test_loop_claude.py +++ b/bin/loop_claude/test_loop_claude.py @@ -195,13 +195,15 @@ def judged(verdict, evidence="working", answer="", escalation="", release_ready= class Worker: """A worker claude: writes a line to the session's transcript, then streams a result.""" - def __init__(self, home, prompted=False, text="working", usage=None): + def __init__(self, home, prompted=False, text="working", usage=None, sent=None): self.home = home # Whether it records its -p prompt in the transcript first, as claude does. self.prompted = prompted self.text = text # The usage its message records, twice as Claude Code writes a message of two blocks; none where None. self.usage = usage + # The time its message records, as utc() writes it; none where None. + self.sent = sent def __call__(self, args): flag = "--session-id" if "--session-id" in args else "--resume" @@ -215,6 +217,8 @@ class Worker: said = assistant({"type": "text", "text": self.text}) if self.usage is not None: said["message"] |= {"id": "msg-1", "model": "claude-opus-5-5", "usage": self.usage} + if self.sent is not None: + said["timestamp"] = self.sent handle.write((json.dumps(said) + "\n") * (1 if self.usage is None else 2)) return Streamed(0) @@ -269,14 +273,15 @@ class Transitions(unittest.TestCase): states.transition(state, event) def test_a_rulings_step_up_reaches_the_worker_it_resumes_through_the_answer_the_human_types_and_no_further(self): - stepped_to = Route(Rung("claude-fable-5-2", "low"), escalated_at=3) + ran_on = Rung("claude-opus-5-5", "xhigh") + stepped = routing.StepUp(Route(ran_on), Route(Rung("claude-fable-5-2", "low"), escalated_at=3)) interrupt, quoted = Interrupt("e", "q", "round 2: 🔴"), QuotedAnswer("e", "q", "A.", "round 2: 🔴") for state, event, after in ( - (states.Judging("/t"), states.Ruled(quoted, stepped_to), states.Working(quoted, 1, True)), - (states.Judging("/t", 3), states.Ruled(quoted, stepped_to), states.Interrupting("/t", quoted, 3, True)), - (states.Judging("/t"), states.Ruled(interrupt, stepped_to), states.Interrupting("/t", interrupt, 0, True)), - (states.Interrupting("/t", interrupt, 0, True), states.HumanTyped(TYPED), states.Working(TYPED, 0, True)), - (states.Interrupting("/t", interrupt, 0, True), states.HumanTalked(), states.Judging("/t")), + (states.Judging("/t"), states.Ruled(quoted, stepped), states.Working(quoted, 1, True)), + (states.Judging("/t", 3), states.Ruled(quoted, stepped), states.Interrupting("/t", quoted, 3, ran_on)), + (states.Judging("/t"), states.Ruled(interrupt, stepped), states.Interrupting("/t", interrupt, 0, ran_on)), + (states.Interrupting("/t", interrupt, 0, ran_on), states.HumanTyped(TYPED), states.Working(TYPED, 0, True)), + (states.Interrupting("/t", interrupt, 0, ran_on), states.HumanTalked(), states.Judging("/t")), (states.Working(TYPED, 0, True), states.WorkerRan("/t"), states.Judging("/t")), ): with self.subTest(state=state, event=event): @@ -647,6 +652,23 @@ Spent since 2026-09-21T14:13:20Z, in tokens of input, cache write 5m, cache writ self.assertIn(" Launch each reviewer with the Agent tool set to model fable, effort low, the rung item 179's E + I of 5 picks.", typed) self.assertNotIn("went through", first[first.index("-p") + 1]) + def test_an_interrupt_that_steps_the_worker_up_compacts_its_session_on_the_rung_it_ran_on(self): + usage = {"input_tokens": 2, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 5000}, "cache_read_input_tokens": 70000, "output_tokens": 100} + worker = Worker(self.paths.claude_code_transcripts.parent, usage=usage, sent=utc(NOW - 600)) + fake = FakeEffects(streams=[worker, Streamed(0), worker], claude=[judged("interrupt", escalation="working"), judged("finished")]) + + def typed(prompt): + for _, action in fake.timers: + action() + return "Use B." + fake.scripts["input"] = [typed] + cycle = dataclasses.replace(self.cycle(fake), project=OPUS_PROJECT, route=Route(Routing.SONNET, 179, 2, 5)) + with contextlib.redirect_stderr(io.StringIO()): + run.run_cycle(cycle) + _, compaction, resumed = fake.made("stream_claude") + self.assertEqual(compaction[compaction.index("-p") + 1], "/compact") + self.assertEqual([(args[args.index("--model") + 1], args[args.index("--effort") + 1]) for args in (compaction, resumed)], [("claude-sonnet-5-5", "high"), ("claude-opus-5-5", "medium")]) + def test_a_typed_answer_past_the_quoted_answers_cap_reaches_the_worker_with_its_own_question(self): worker = Worker(self.paths.claude_code_transcripts.parent) fake = FakeEffects(streams=[worker] * 5, claude=[judged("quoted", answer="Use A.")] * 4 + [judged("finished")], inputs=["Use B."]) @@ -792,7 +814,7 @@ class Interrupts(unittest.TestCase): 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"), str(self.transcript), {"LOOP_CLAUDE_WORKER_RUNG": "m at high effort"}) + 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.""" @@ -833,7 +855,45 @@ class Interrupts(unittest.TestCase): 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"{STAMP} compacted the worker's claude session before its prompt cache expires; still waiting for an answer\n", said) + 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_compacts_nothing(self): self.worked(minutes_ago=10) @@ -874,7 +934,7 @@ class Interrupts(unittest.TestCase): 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: You've hit your session limit; still waiting for an answer\n", said) + 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) @@ -882,7 +942,7 @@ class Interrupts(unittest.TestCase): 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) + 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) From 708347382b7734fc6726abd63112de5a97f30d62 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 19 Nov 2026 04:44:37 +0100 Subject: [PATCH 06/11] Check the stopped timer once stop returns, not after a wait --- bin/loop_effects/test_effects.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/loop_effects/test_effects.py b/bin/loop_effects/test_effects.py index f1e98449..6ee8f18b 100644 --- a/bin/loop_effects/test_effects.py +++ b/bin/loop_effects/test_effects.py @@ -152,8 +152,9 @@ class Timer(unittest.TestCase): def test_a_timer_stopped_before_its_delay_has_passed_never_runs_the_action(self): ran = threading.Event() + # stop returns once the timer's thread has ended, so the action cannot run later. start_timer(0.2, ran.set)() - self.assertFalse(ran.wait(0.5)) + self.assertFalse(ran.is_set()) def test_stopping_a_timer_whose_action_runs_waits_for_the_action_to_end(self): started, ended = threading.Event(), threading.Event() From a2ea6738222099fb0d9d09e3952e2fd96d67e79c Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 19 Nov 2026 04:44:37 +0100 Subject: [PATCH 07/11] =?UTF-8?q?Answer=20prose=20round=201:=200=20?= =?UTF-8?q?=F0=9F=94=B4,=202=20=F0=9F=9F=A1=20(0=20misread);=20reword=20th?= =?UTF-8?q?e=20timer's=20docstrings,=20comments=20and=20help,=20rename=20t?= =?UTF-8?q?he=20no-compaction=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/loop_claude/cycle/interrupt.py | 4 ++-- bin/loop_claude/loop/usage.py | 8 ++++---- bin/loop_claude/test_loop_claude.py | 4 ++-- bin/loop_effects/effects.py | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bin/loop_claude/cycle/interrupt.py b/bin/loop_claude/cycle/interrupt.py index f2db7e23..4a3de5fc 100644 --- a/bin/loop_claude/cycle/interrupt.py +++ b/bin/loop_claude/cycle/interrupt.py @@ -21,7 +21,7 @@ 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. +# 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 a session that last wrote one isn't compacted. lilleman chose this on 2026-11-11. HOUR_CACHE = 3600 CACHE_MARGIN = 600 COMPACT_FLOOR = 60_000 @@ -75,7 +75,7 @@ def answered(answer_here: bool, session: WorkerSession, effects: Effects) -> str 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.""" + """typed_answer, compacting the worker's session where no answer has come by CACHE_MARGIN seconds before its prompt cache expires. /talk needs no compaction: the claude it opens compacts an idle session itself.""" entries = read_transcript(session.transcript_path) now = effects.now() delay = None if isinstance(entries, Failure) else compaction_delay(entries, now) diff --git a/bin/loop_claude/loop/usage.py b/bin/loop_claude/loop/usage.py index bec688ce..8fc46308 100644 --- a/bin/loop_claude/loop/usage.py +++ b/bin/loop_claude/loop/usage.py @@ -58,16 +58,16 @@ 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 :, each release ready to cut and each stop as a DM, as bin/notify sends it. Four stops are left out: Ctrl-C, printing this help, the loop-claude that started the windows once it attaches or prints how to, and a loop whose chunk finished once it started the next chunks. A DM -that fails is said on stderr, and the loop goes on. +that fails is said on stderr, and the loop goes on. Where no answer has come 10 minutes before the +worker's hour-long prompt cache expires, and the worker's 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. As a chunk starts, before it is routed, loop-claude runs bin/chunk-preflight --worktree, adding --item in a window named for an item, and quotes what it prints in the first prompt diff --git a/bin/loop_claude/test_loop_claude.py b/bin/loop_claude/test_loop_claude.py index 8124a097..8d9fb3a1 100644 --- a/bin/loop_claude/test_loop_claude.py +++ b/bin/loop_claude/test_loop_claude.py @@ -838,7 +838,7 @@ class Interrupts(unittest.TestCase): handle.write(json.dumps(said) + "\n") def fired(self, fake, answer): - """An input the human types answer at, after every timer started has fired.""" + """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() @@ -895,7 +895,7 @@ class Interrupts(unittest.TestCase): 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_compacts_nothing(self): + 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.") diff --git a/bin/loop_effects/effects.py b/bin/loop_effects/effects.py index 79ee2656..0b347eac 100644 --- a/bin/loop_effects/effects.py +++ b/bin/loop_effects/effects.py @@ -119,7 +119,7 @@ def run_python(args: list[str], cwd: str, timeout: float) -> subprocess.Complete 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. + # A timer still running doesn't keep the process alive once the loop exits. timer.daemon = True timer.start() From f0f9d9139665f6197d389fc663bb6108b3f44a7d Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 19 Nov 2026 04:44:37 +0100 Subject: [PATCH 08/11] =?UTF-8?q?Answer=20product-owner=20round=201:=200?= =?UTF-8?q?=20=F0=9F=94=B4,=201=20=F0=9F=9F=A1=20(0=20misread);=20announce?= =?UTF-8?q?=20the=20compaction=20on=20a=20line=20of=20its=20own,=20fixed?= =?UTF-8?q?=20in=20the=20stability=20answer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From 1fe59960fb1f6b6a4c8f854b413dc8122f7db5c1 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 19 Nov 2026 04:45:19 +0100 Subject: [PATCH 09/11] File item 398: measure the compaction's saving --- todo.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/todo.md b/todo.md index 8e927899..8f14b73a 100644 --- a/todo.md +++ b/todo.md @@ -6,7 +6,7 @@ `Bar = 0.1` -`Next ID = 398` +`Next ID = 399` ## Items @@ -42,6 +42,7 @@ | 68 | 0.14.0 | | principle | **Test `workflow-bench/compare.py`'s blinding and `bench.py`'s run plan.** | 2 | 1 | 1 | 2 | 4 | 1.1 by 5 | 1.5 | | 102 | 0.14.0 | | | **Give loop-claude's retry delay, and the file it reads the machine's memory from, environment overrides, so the black-box test stops replacing `time.sleep` and `machine_load.memory.MEMINFO` through `sitecustomize`.** | 1 | 1 | 2 | 2 | 3 | 1.1 by 4 | 1.5 | | 135 | 0.14.0 | | | **Fingerprint only the modules loop-claude imports, so editing another tool's code no longer reloads a running loop.** | 2 | 2 | 3 | 5 | 3 | 1.1 by 4 | 1.5 | +| 398 | 0.14.0 | | | **Measure whether compacting a waiting worker's session cut the cost of 20 chunks with an interrupt, with quality holding.** | 2 | 2 | 1 | 3 | 3 | 1.4 by 4 | 1.4 | | 5 | 0.14.0 | | decision | **Key a project on its checkout's full path, and refuse to start while another session uses that key.** | 4 | 2 | 3 | 7 | 5 | 1.1 by 6 | 1.4 | | 291 | 0.14.0 | | owner | **Let `principles/technical-principles.md` → "Sane defaults" allow a library whose README goals ask that it work with no config to default the clock and randomness in its public constructor, where a documented option overrides each.** | 1 | 2 | 3 | 4 | 1 | 1.2 by 3 | 1.3 | | 156 | 0.14.0 | | decision | **Drop `rank_todo.py`'s ranking of an old-shape table once no project under `~/code` holds one.** | 2 | 1 | 3 | 4 | 4 | 1.1 by 4 | 1.2 | @@ -356,6 +357,10 @@ Found by the principles audit of PR #242. The `SITECUSTOMIZE` block in `bin/loop The maintainability senior and the inherited architect of PR #257's scoring run named `fingerprint` and `exec_if_edited` in `bin/loop_claude/loop/reload.py`: the fingerprint covers every `bin/**/*.py`, so editing `statusline.py` or `rank_todo.py` re-execs a running loop. Make `fingerprint` easier to read. +### 398. Measure whether compacting a waiting worker's session cut the cost of 20 chunks with an interrupt, with quality holding. + +The check lilleman set for item 310 on 2026-11-11, which project principle 1 asks for before a cost cut stands: compare the cost entries of 20 archived chunks with an interrupt from before the compaction shipped against 20 from after it. Measure the tokens written to the cache by each resume after an interrupt, and the tokens per finished chunk, and check that review rounds and unfinished verdicts per chunk hold. Where the saving doesn't show, or quality drops, file an item to remove the compaction or retune `COMPACT_FLOOR` and `CACHE_MARGIN` in `bin/loop_claude/cycle/interrupt.py`. Record the outcome as a decision. Start it once 20 chunks with an interrupt have run on 0.12.0 or later. + ### 5. Key a project on its checkout's full path, and refuse to start while another session uses that key. The key names the project's session, archive folder and models entry. `project_key`, in `bin/transcript_archive/project_key.py`, strips `~/code/` and then any leading `/`, so `~/code/a` and `/a` share all three, and one loop can resume or delete the other's session. From d9875ee460be8cd9b500214e6d812b6555193c3d Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 19 Nov 2026 04:47:57 +0100 Subject: [PATCH 10/11] File item 443: measure the compaction's saving --- todo.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/todo.md b/todo.md index 58098668..efc29bb6 100644 --- a/todo.md +++ b/todo.md @@ -6,7 +6,7 @@ `Bar = 0.1` -`Next ID = 443` +`Next ID = 444` ## Items @@ -46,6 +46,7 @@ | 68 | 0.14.0 | | principle | **Test `workflow-bench/compare.py`'s blinding and `bench.py`'s run plan.** | 2 | 1 | 1 | 2 | 4 | 1.1 by 5 | 1.5 | | 102 | 0.14.0 | | | **Give loop-claude's retry delay, and the file it reads the machine's memory from, environment overrides, so the black-box test stops replacing `time.sleep` and `machine_load.memory.MEMINFO` through `sitecustomize`.** | 1 | 1 | 2 | 2 | 3 | 1.1 by 4 | 1.5 | | 135 | 0.14.0 | | | **Fingerprint only the modules loop-claude imports, so editing another tool's code no longer reloads a running loop.** | 2 | 2 | 3 | 5 | 3 | 1.1 by 4 | 1.5 | +| 443 | 0.14.0 | | | **Measure whether compacting a waiting worker's session cut the cost of 20 chunks with an interrupt, with quality holding.** | 2 | 2 | 1 | 3 | 3 | 1.4 by 4 | 1.4 | | 5 | 0.14.0 | | decision | **Key a project on its checkout's full path, and refuse to start while another session uses that key.** | 4 | 2 | 3 | 7 | 5 | 1.1 by 6 | 1.4 | | 291 | 0.14.0 | | owner | **Let `principles/technical-principles.md` → "Sane defaults" allow a library whose README goals ask that it work with no config to default the clock and randomness in its public constructor, where a documented option overrides each.** | 1 | 2 | 3 | 4 | 1 | 1.2 by 3 | 1.3 | | 156 | 0.14.0 | | decision | **Drop `rank_todo.py`'s ranking of an old-shape table once no project under `~/code` holds one.** | 2 | 1 | 3 | 4 | 4 | 1.1 by 4 | 1.2 | @@ -401,6 +402,10 @@ Found by the principles audit of PR #242. The `SITECUSTOMIZE` block in `bin/loop The maintainability senior and the inherited architect of PR #257's scoring run named `fingerprint` and `exec_if_edited` in `bin/loop_claude/loop/reload.py`: the fingerprint covers every `bin/**/*.py`, so editing `statusline.py` or `rank_todo.py` re-execs a running loop. Make `fingerprint` easier to read. +### 443. Measure whether compacting a waiting worker's session cut the cost of 20 chunks with an interrupt, with quality holding. + +The check lilleman set for item 310 on 2026-11-11, which project principle 1 asks for before a cost cut stands: compare the cost entries of 20 archived chunks with an interrupt from before the compaction shipped against 20 from after it. Measure the tokens written to the cache by each resume after an interrupt, and the tokens per finished chunk, and check that review rounds and unfinished verdicts per chunk hold. Where the saving doesn't show, or quality drops, file an item to remove the compaction or retune `COMPACT_FLOOR` and `CACHE_MARGIN` in `bin/loop_claude/cycle/interrupt.py`. Record the outcome as a decision. Start it once 20 chunks with an interrupt have run on 0.12.0 or later. + ### 5. Key a project on its checkout's full path, and refuse to start while another session uses that key. The key names the project's session, archive folder and models entry. `project_key`, in `bin/transcript_archive/project_key.py`, strips `~/code/` and then any leading `/`, so `~/code/a` and `/a` share all three, and one loop can resume or delete the other's session. From e019a6b78c7bad3aa1cce0d5be8b501b4abbb80a Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 19 Nov 2026 04:53:47 +0100 Subject: [PATCH 11/11] Finish item 310 in the plan --- CHANGELOG.md | 1 + todo.md | 18 ------------------ 2 files changed, 1 insertion(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9f82176..8090c6be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- loop-claude compacts a waiting worker's session 10 minutes before its hour-long prompt cache expires, where no answer to an interrupt has come and the context holds at least 60k tokens, so the resume doesn't write the whole context again. - do-epic gives a `do-epic-human` issue that blocks another its own window: it DMs you once through the notifier of one of the epic's repos, takes no running slot, and polls until you close the issue in Jira, when the issues it held up may start. - do-epic tells an epic chunk's worker how its repo ships the change: a bullet under the repo's changelog where the repo publishes something, none where its plan is in Jira, and no `--finish` for `chunk-pr merge`. - `do-epic --status` says which chunk asks you a question, and since when each parked chunk has awaited its gate. diff --git a/todo.md b/todo.md index efc29bb6..7286d4af 100644 --- a/todo.md +++ b/todo.md @@ -15,7 +15,6 @@ | 420 | 0.12.0 | | | **End a worker that printed its final result and lingers, by killing the background shells it left, after a few minutes, and print each kill with its evidence on loop-claude's terminal.** | 2 | 2 | 2 | 4 | 3 | 1.5 by 6 | 2.0 | | 417 | 0.12.0 | | | **Run do-epic end to end against a sandbox epic on a real Jira site with a command of its own, outside `run-tests`.** | 2 | 1 | 1 | 2 | 3 | 1 by 5 | 1.9 | | 442 | 0.12.0 | | | **Have `do-epic ` split each open issue by repo, pick the repo of each issue that no label names one for, asking the human where unclear, and re-rank the epic, before it starts chunks.** | 3 | 2 | 3 | 6 | 5 | 1 by 5 | 1.2 | -| 310 | 0.12.0 | | | **Compact a waiting worker's session shortly before its prompt cache expires, only when the human has not answered an interrupt by then.** | 3 | 2 | 3 | 6 | 4 | 1.4 by 3 | 0.9 | | 422 | 0.12.0 | 420 | | **Check every 5 minutes whether a tmux window of the loop looks stuck by other signs than a lingering worker, investigate it with a headless claude, and kill only the process the investigation names.** | 3 | 2 | 3 | 6 | 5 | 1.5 by 4 | 0.8 | | 365 | 0.13.0 | | | **Have `score-outcomes` read the finished and dropped items of a plan outside the checkout, from the project repo's history or from Jira's issue changelogs.** | 3 | 2 | 2 | 5 | 4 | 1.6 by 2, 1.1 by 2 | 1.2 | | 368 | 0.13.0 | | | **Call a release review due, call a release ready to cut, and tell whether a chunk owes a scoring run, for a plan outside the checkout, from the project repo's history or from Jira.** | 3 | 2 | 2 | 5 | 4 | 1.6 by 2, 1.1 by 2 | 1.2 | @@ -111,23 +110,6 @@ Touches: - `bin/do_epic/test_main.py` - `README.md`: `## Tools`, `### Set up a project` (small) -### 310. Compact a waiting worker's session shortly before its prompt cache expires, only when the human has not answered an interrupt by then. - -281 worker resumes after the 1-hour cache expired rewrote a whole context, $379 of the $9,154 measured from 2026-09-30 to 2026-11-11 (4.1%). A compaction while the cache is warm reads the context at a twentieth of the input price and leaves about 20k tokens to rewrite later. - -lilleman, 2026-11-11: do it on a timer, so an answer that comes in time doesn't cost anything. When loop-claude interrupts, start a timer a margin short of the cache lifetime (about 50 minutes of a 1-hour cache; read the lifetime from the session's last `cache_creation` usage). If no answer has come when it fires, compact the session, then keep waiting. Skip it below about 60k tokens of context, where the rewrite costs less than the compaction. A wait for a usage limit can't be helped this way. - -Probe first that `claude -p "/compact" --resume ` compacts a session: the Agent SDK documents `/compact` as a prompt, the CLI pages don't show it. Claude Code compacts idle interactive sessions itself ("Compacted while idle"), but not `-p` runs. Check: the cost entries of 20 chunks with an interrupt, before and after. - -Touches: -- `bin/agent_cycle/interrupt.py`: `interrupt`, `answered`, `typed_answer`, `WorkerSession` -- `bin/loop_effects/effects.py`: `Effects`, `system_effects` -- `bin/claude_code/tokens.py`: `tokens_of` (small) -- `bin/claude_code/reply.py`: `claude_reply` (small) -- `bin/loop_claude/loop/usage.py`: `HELP` (small) -- `bin/loop_claude/test_loop_claude.py`: `FakeEffects`, `Interrupts` -- `bin/loop_effects/test_effects.py` - ### 422. Check every 5 minutes whether a tmux window of the loop looks stuck by other signs than a lingering worker, investigate it with a headless claude, and kill only the process the investigation names. Item 420 ends a worker that printed its final result and lingers. A window can also stall earlier: a test run that hangs on a shell ignoring SIGINT (item 402's first full run), a pane with no output while its process tree does no work, or a background shell older than the chunk's usual length.