Compact the worker's context above 200k tokens, from item 231's evaluation of context cost, and file items 235 and 236

This commit is contained in:
2026-10-27 00:13:08 +01:00
parent aa020f15f7
commit f2e239d111
9 changed files with 52 additions and 8 deletions
+3 -3
View File
@@ -5,6 +5,7 @@ import typing
from loop_claude.cycle.states import Active, Archived, Closing, Ended, Event, HumanTalked, HumanTyped, Interrupting, Judging, QUOTED_ANSWERS_PER_CYCLE, Ruled, State, TalkChosen, Talking, transition, TypedAnswer, WorkerRan, Working
from loop_claude.routing import after_ruling, judge_rung
from transcript_archive.write import archive
from loop_claude.cycle.worker import AUTO_COMPACT_WINDOW, COMPACT_ABOVE, run_worker, worker_args
from claude_code.session import delete_session, Session, session_open, transcript_file
from loop_claude.effects import Effects
from failure import Failure
@@ -17,7 +18,6 @@ from loop_claude.cycle.interrupt import put_question, talk, Talk
from claude_code.transcript import read_transcript
from loop_claude.cycle.retry import retried
from model_routing.route import Route
from loop_claude.cycle.worker import run_worker, worker_args
from model_routing.ladder import Rung
from loop_claude.escalation_mark import StepUp
from loop_claude.stop import Stop
@@ -116,8 +116,8 @@ def work(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None, step_up: Ste
def worker_environment(cycle: Cycle) -> dict[str, str]:
"""The rungs the worker's claude is handed, which rule-hook holds its review launches to."""
return worker_rungs.environment(cycle.project, cycle.route)
"""The rungs the worker's claude is handed, which rule-hook holds its review launches to, and the context size above which it compacts."""
return {**worker_rungs.environment(cycle.project, cycle.route), AUTO_COMPACT_WINDOW: str(COMPACT_ABOVE)}
def worker_start(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None, step_up: StepUp | None) -> list[str]:
+3
View File
@@ -12,6 +12,9 @@ from claude_code.session import Session
PRINTED_TOOL_INPUT = 160
# Seconds between reads of the machine's memory while too little of it is available to start a worker.
MEMORY_POLL = 60
# The context size, in tokens, above which the worker's claude compacts, handed to it in this variable: docs/decisions.md → `loop-claude: the worker's claude compacts its context above 200k tokens`.
AUTO_COMPACT_WINDOW = "CLAUDE_CODE_AUTO_COMPACT_WINDOW"
COMPACT_ABOVE = 200_000
def run_worker(root: str, args: list[str], env: dict[str, str], effects: Effects) -> Failure | Limited | None:
+4
View File
@@ -47,6 +47,10 @@ time. The layout, and when a copy is replaced, are in bin/transcript_archive/__i
Before each worker starts, loop-claude waits until at least a quarter of the machine's memory is available
(bin/machine_load/memory.py).
Each worker's claude, and the session /talk opens, compacts its context once it passes 200k tokens,
handed as CLAUDE_CODE_AUTO_COMPACT_WINDOW (bin/loop_claude/cycle/worker.py), so a long chunk
re-reads less each turn; ~/.claude/CLAUDE.md → "Compact instructions" says what the summary keeps.
Every claude loop-claude starts runs the project's model, saved per <key> in
~/.claude/loop-claude-models.json and read each cycle, unless routing, below, puts the worker,
the session /talk opens and the judge on another rung, a model at an effort. A project without an
+1 -1
View File
@@ -256,7 +256,7 @@ class Cycles(unittest.TestCase):
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("finished")])
cycle = dataclasses.replace(self.cycle(fake), project=Rung("claude-opus-5-5", "medium"), route=Route(Rung("claude-haiku-5-5", "max"), 163, 1, 5))
self.assertEqual(run.run_cycle(cycle).verdict, Finished("working"))
self.assertEqual(fake.worker_environments, [{"LOOP_CLAUDE_PROJECT_RUNG": "claude-opus-5-5 at medium effort", "LOOP_CLAUDE_WORKER_RUNG": "claude-haiku-5-5 at max effort", "LOOP_CLAUDE_ESCAPE_AND_IMPACT": "5"}])
self.assertEqual(fake.worker_environments, [{"LOOP_CLAUDE_PROJECT_RUNG": "claude-opus-5-5 at medium effort", "LOOP_CLAUDE_WORKER_RUNG": "claude-haiku-5-5 at max effort", "LOOP_CLAUDE_ESCAPE_AND_IMPACT": "5", "CLAUDE_CODE_AUTO_COMPACT_WINDOW": "200000"}])
def test_the_claude_an_interrupt_resumes_is_handed_the_same_rungs_as_the_worker(self):
def answer(args):