e55b8826fb
# Conflicts: # bin/agent_cycle/interrupt.py # bin/agent_cycle/run.py # bin/agent_cycle/states.py # docs/reading-cost.md # todo.md
78 lines
3.5 KiB
Python
78 lines
3.5 KiB
Python
from claude_code.entries import AssistantTurn, ends_stream, entry_from, hit_limit, json_object, limit_said, rejected_until
|
|
from loop_effects.effects import Effects, say
|
|
from failure import Failure
|
|
from claude_code.reply import Limited
|
|
from claude_code.transcript import part_line
|
|
from machine_load.memory import ROOM_SHARE
|
|
from model_routing.ladder import Rung
|
|
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 auto-compact window, in tokens, handed to the worker's claude in the environment variable AUTO_COMPACT_WINDOW names. Claude Code compacts before the window fills, at about 170k: docs/decisions.md → `loop-claude: the worker's claude compacts its context before it fills a 200k window`.
|
|
AUTO_COMPACT_WINDOW = "CLAUDE_CODE_AUTO_COMPACT_WINDOW"
|
|
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:
|
|
return Failure(f"could not start claude: {failure.strerror}")
|
|
result: dict | None = None
|
|
resets: list[int] = []
|
|
for raw in proc.lines():
|
|
event = json_object(raw) or {}
|
|
if ends_stream(event):
|
|
result = event
|
|
if (resets_at := rejected_until(event)) is not None:
|
|
resets.append(resets_at)
|
|
if isinstance(turn := entry_from(event), AssistantTurn):
|
|
for part in turn.parts:
|
|
if line := part_line(part, PRINTED_TOOL_INPUT):
|
|
say(line, effects, stdout=True)
|
|
return worker_outcome(proc.wait(), result, resets)
|
|
|
|
|
|
def wait_for_memory(effects: Effects) -> None:
|
|
"""Returns once the machine has room for a worker, or its memory cannot be read, so loops running at once do not push it into swap. While it waits, it says so once on stderr and reads the memory again each MEMORY_POLL seconds."""
|
|
said = False
|
|
while (memory := effects.memory()) is not None and not memory.has_room():
|
|
if not said:
|
|
say(f"waiting to start the worker until at least {ROOM_SHARE:.0%} of the machine's memory is available; {memory.available // 1024} MiB of {memory.total // 1024} MiB is available now", effects)
|
|
said = True
|
|
effects.sleep(MEMORY_POLL)
|
|
|
|
|
|
def worker_outcome(code: int, result: dict | None, resets: list[int]) -> Failure | Limited | None:
|
|
if result is not None and hit_limit(result):
|
|
return Limited(max(resets, default=None), limit_said(result))
|
|
if code < 0:
|
|
return Failure(f"claude killed by signal {-code}")
|
|
if code:
|
|
return Failure(f"claude exited {code}")
|
|
return None if result is not None else Failure("claude exited 0 without a result event")
|
|
|
|
|
|
def worker_args(session: Session, worker: Rung, prompt: str, resume: bool) -> list[str]:
|
|
# --session-id creates the session under the id loop-claude keeps; --resume continues that session.
|
|
start = ["--resume", str(session.id)] if resume else ["--name", session.name, "--session-id", str(session.id)]
|
|
# -p fails without --verbose under stream-json.
|
|
return [
|
|
"-p", prompt,
|
|
*start,
|
|
"--output-format", "stream-json",
|
|
"--permission-mode", "bypassPermissions",
|
|
# A pending wakeup keeps claude -p from exiting, which holds back the judge.
|
|
"--disallowedTools", "ScheduleWakeup",
|
|
"--verbose",
|
|
*worker.flags(),
|
|
]
|