64 lines
4.2 KiB
Python
64 lines
4.2 KiB
Python
"""Every CYCLES_PER_CHECK cycles across all projects, measure the archive's routing figures, keep them at its root, and interrupt the human with each call for a routing change not raised before: docs/decisions.md → `routing:`."""
|
|
import pathlib
|
|
|
|
from routing_figures import archive
|
|
from loop_claude.waiting import Ask, listed, OpenOnTerminal
|
|
from routing_figures.measure import calls, due, render, totals
|
|
from loop_claude.hand_over import claude_args, ClaudeCommand, hand_over
|
|
from loop_claude.effects import Effects, say
|
|
from failure import Failure
|
|
from loop_claude.paths import Paths
|
|
from model_routing.ladder import Rung
|
|
from loop_claude.stop import Stop
|
|
from utc_stamp import utc
|
|
|
|
|
|
def check_routing(root: pathlib.Path, checkout: str, session: str, rung: Rung, paths: Paths, effects: Effects) -> Stop | None:
|
|
"""Stops only when claude cannot start for the calls; a figure it cannot measure or keep is said on stderr, and the loop goes on. checkout is the loop's, session is its worker's session id, which loop-inbox lists the calls under, and rung is the rung of the claude that puts the calls to the human."""
|
|
with archive.locked(root) as held:
|
|
new = new_calls(root, paths, effects) if held else []
|
|
if isinstance(new, Failure):
|
|
say(f"routing figures: {new.reason}", effects)
|
|
return None
|
|
return raise_calls(root, checkout, session, new, rung, paths, effects) if new else None
|
|
|
|
|
|
def new_calls(root: pathlib.Path, paths: Paths, effects: Effects) -> list[str] | Failure:
|
|
"""Once due, measures and keeps the figures, and returns their calls not raised since the latest reset; none while not due."""
|
|
cycle = archive.cycles(root)
|
|
last = archive.last_measured(root)
|
|
if isinstance(cycle, Failure) or isinstance(last, Failure) or not due(cycle, last):
|
|
return cycle if isinstance(cycle, Failure) else last if isinstance(last, Failure) else []
|
|
kept = archive.raised(root)
|
|
if isinstance(kept, Failure):
|
|
return kept
|
|
say(f"\n=== cycle {cycle} across loop-claude's archive: measuring the routing figures ===", effects, stdout=True)
|
|
measured = archive.measure_archive(root, str(paths.home), kept.since, effects.git)
|
|
if isinstance(measured, Failure):
|
|
return measured
|
|
figures = totals(measured.chunks, measured.skipped, measured.unread)
|
|
found = calls(figures)
|
|
if unwritten := archive.write_figures(root, render(figures, found, utc(effects.now()), cycle, kept.since)):
|
|
return unwritten
|
|
return [call for call in found if call not in kept.calls]
|
|
|
|
|
|
def raise_calls(root: pathlib.Path, checkout: str, session: str, new: list[str], rung: Rung, paths: Paths, effects: Effects) -> Stop | None:
|
|
"""Puts the calls to the human through a claude in this setup's checkout, where the routing decision lives; loop-inbox lists them under the loop's checkout while that claude is open. Off a terminal they are only said, and stay in the figures, raised at the next measurement on a terminal."""
|
|
bullets = "".join(f"- {call}\n" for call in new)
|
|
question = f"loop-claude's routing figures call for a routing change:\n{bullets}The figures: {root / archive.FIGURES}"
|
|
say(f"\n{question}", effects)
|
|
if not effects.on_terminal():
|
|
say("Not on a terminal: the calls are raised again at the next measurement.", effects)
|
|
return None
|
|
prompt = f"Put these calls for a routing change from loop-claude's figures to the human, and change nothing until they answer:\n{bullets}The figures are in {root / archive.FIGURES}. The ladder, its steps and the delegation rule are docs/decisions.md → `routing:`, and a change to them is the human's. Record what they decide as ~/.claude/CLAUDE.md says; the next chunk of this repo files the plan item for these calls."
|
|
ask = Ask(effects.new_id(), session, checkout, question, effects.now(), OpenOnTerminal())
|
|
with listed(paths.waiting, ask, effects):
|
|
unstarted = hand_over(ClaudeCommand(claude_args(rung, [prompt]), str(paths.claude_dir), {}), paths, effects)
|
|
if unstarted:
|
|
return Stop(1, f"could not start claude for the routing calls: {unstarted.reason}")
|
|
# Raised once the human has seen them, answered or not: an unanswered call reaches the plan through AGENTS.md, so it is not asked twice.
|
|
if unmarked := archive.mark_raised(root, new, utc(effects.now())):
|
|
say(f"routing figures: {unmarked.reason}", effects)
|
|
return None
|