Files
claude-config/bin/loop_claude/loop/routing_check.py
T

70 lines
4.4 KiB
Python

"""Every state.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: loop-claude measures review rounds…`."""
import pathlib
from routing_figures import archive
from loop_state.waiting.ask import Ask, OpenOnTerminal
from routing_figures.calls import Call, calls, totals
from loop_state.hand_over import claude_args, ClaudeCommand, hand_over
from loop_effects.effects import Effects, say
from failure import Failure
from loop_state.waiting.listing import listed
from loop_claude.loop.this_loop import Loop
from loop_state.paths import Paths
from routing_figures.render import render
from model_routing.ladder import Rung
from routing_figures import state
from loop_claude.stop import Stop
from records.utc_stamp import utc
def check_routing(root: pathlib.Path, rung: Rung, loop: Loop) -> 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. root is the archive's, 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, loop.paths, loop.effects) if held else []
if isinstance(new, Failure):
say(f"routing figures: {new.reason}", loop.effects)
return None
return raise_calls(root, new, rung, loop) if new else None
def new_calls(root: pathlib.Path, paths: Paths, effects: Effects) -> list[Call] | 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)
if isinstance(cycle, Failure):
return cycle
kept = state.read_state(root)
if isinstance(kept, Failure) or not state.due(cycle, kept.measured_at):
return kept if isinstance(kept, Failure) else []
raised = state.raised(root)
if isinstance(raised, Failure):
return raised
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), raised.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, raised.since)) or state.mark_measured(root, cycle):
return unwritten
return [call for call in found if call not in raised.calls]
def raise_calls(root: pathlib.Path, new: list[Call], rung: Rung, loop: Loop) -> Stop | None:
"""Puts the calls to the human through a claude in this setup's development clone, where the routing decision is changed; 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.sentence()}\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}", loop.effects)
if not loop.effects.on_terminal():
say("Not on a terminal: the calls are raised again at the next measurement.", loop.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 in this checkout, the development clone, as ~/.claude/CLAUDE.md says; the next chunk of this repo files the plan item for these calls."
ask = Ask(loop.effects.new_id(), str(loop.session.id), loop.root, question, loop.effects.now(), OpenOnTerminal())
with listed(loop.listed.folder, ask, loop.effects):
unstarted = hand_over(ClaudeCommand(claude_args(rung, [prompt]), str(loop.paths.config_clone), {}), loop.effects)
if unstarted:
return Stop(1, f"could not start claude for the routing calls in {loop.paths.config_clone}, the setup's development clone: {unstarted.reason}")
# Raised once the human has seen them, answered or not: an unanswered call reaches the plan through .claude/rules/routing-calls.md, so it is not asked twice.
if unmarked := state.mark_raised(root, new, utc(loop.effects.now())):
say(f"routing figures: {unmarked.reason}", loop.effects)
return None