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

85 lines
5.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: build-release measures review rounds…`."""
from collections.abc import Mapping
import pathlib
from routing_figures import archive
from routing_figures.calls import Call, calls, totals
from agent_cycle.hand_over import claude_args, ClaudeCommand, hand_over
from loop_effects.effects import Effects, say
from failure import Failure
from chunk_driver.this_loop import Loop
from loop_state.paths import Paths
from model_routing.ladder_file import read_ladder, SHIPPED
from routing_figures.render import render
from model_routing.ladder import Rung
from routing_figures import state
from agent_cycle.stop import Stop
from agent_cycle.direct_message import tell
from text_files.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.env, 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, env: Mapping[str, str], 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 build-release's archive: measuring the routing figures ===", effects, stdout=True)
measured = archive.measure_archive(root, str(paths.home), env, raised.since, effects.git)
if isinstance(measured, Failure):
return measured
# The shipped ladder, not the project's: the figures span every project.
ladder = read_ladder(SHIPPED)
if isinstance(ladder, Failure):
return ladder
figures = totals(measured.chunks, measured.skipped, measured.unread)
found = calls(ladder, figures)
if unwritten := archive.write_figures(root, render(ladder, 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:
"""Says each call on stderr and DMs it, then puts it to the human through a claude in this setup's development clone, where the routing decision is changed. Off a terminal it stays unraised and comes again at the next measurement on one. A machine without that clone has no way to change the decision, so a DM that went out is the last word there and marks the calls raised; one that didn't go out leaves them to come again."""
bullets = "".join(f"- {call.sentence()}\n" for call in new)
question = f"build-release's routing figures call for a routing change:\n{bullets}The figures: {root / archive.FIGURES}"
say(f"\n{question}", loop.effects)
sent = tell(f"build-release in {loop.root} asks:\n\n{question}", loop.effects)
if not loop.paths.config_clone.is_dir():
if sent:
say(f"No development clone at {loop.paths.config_clone}, where the routing decision is changed: the calls above are all this machine puts to you, and they are not raised again.", loop.effects)
keep_raised(root, new, loop)
else:
say(f"No development clone at {loop.paths.config_clone}, where the routing decision is changed, and the DM didn't go out: the calls are raised again at the next measurement.", loop.effects)
return None
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 build-release's figures to the human, and don't change anything 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 the setup's CLAUDE.md says; the next chunk of this repo files the plan item for these calls."
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}")
keep_raised(root, new, loop)
return None
def keep_raised(root: pathlib.Path, new: list[Call], loop: Loop) -> None:
"""Marks the calls raised once they have been put to the human, answered or not: an unanswered call reaches the plan through docs/decisions.md → "Working rules", so it is not asked twice."""
if unmarked := state.mark_raised(root, new, utc(loop.effects.now()), loop.effects.replace_file):
say(f"routing figures: {unmarked.reason}", loop.effects)