Answer stability round 1: 0 🔴, 2 🟡 (1 misread); compact on the worker's rung, announce the compaction and skip it once the cache expired

This commit is contained in:
2026-11-19 04:42:14 +01:00
parent 30595cfdf4
commit 78dd557057
5 changed files with 113 additions and 40 deletions
+15 -8
View File
@@ -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: