Files

92 lines
4.5 KiB
Python

import pathlib
from claude_code.entries import AssistantTurn, ends_stream, entry_from, hit_limit, json_object, limit_said, rejected_until
from agent_cycle.lingering_workers import CHECK_EVERY, RECORD, stopped_shells
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
from agent_cycle.stuck_workers import Watch
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], project_archive: pathlib.Path, investigator: Rung, effects: Effects) -> Failure | Limited | None:
"""Runs the worker's claude once the machine has room for it, as run_claude does with a Watch whose stuck checks run on investigator, the project's rung."""
wait_for_memory(effects)
return run_claude(root, args, env, effects, Watch(investigator, project_archive))
def run_claude(root: str, args: list[str], env: dict[str, str], effects: Effects, watch: Watch | None = None) -> Failure | Limited | None:
"""Runs a headless claude at once, printing what it says, and returns how it failed, or None. Where watch is given, a claude that lingers after its final result loses its quiet shells, as `lingering_workers.stopped_shells` says, each stop appended to RECORD in watch's archive; and a silent claude that has printed no result is checked for stuck shells, as `stuck_workers` says."""
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
# Whether the worker's latest line is its result; a worker that prints on is working.
printed_result = False
resets: list[int] = []
for raw in proc.lines(CHECK_EVERY):
if raw is None:
if watch is not None:
stopped_shells(proc, printed_result, watch.archive / RECORD, effects)
if not printed_result:
watch.check(proc, root, effects)
continue
event = json_object(raw) or {}
printed_result = ends_stream(event)
if printed_result:
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(),
]