91 lines
5.0 KiB
Python
91 lines
5.0 KiB
Python
"""How a cycle starts: the route it runs on, from the archive or the plan, the facts chunk-preflight finds for a fresh chunk, and the items loop-inbox lists."""
|
|
import pathlib
|
|
import typing
|
|
|
|
from release_plan.release_standing import default_plan, DefaultPlan
|
|
from git_cli.git_repo import default_ref, file_at
|
|
from loop_effects.effects import Effects, say
|
|
from failure import Failure
|
|
from loop_claude.routing import first_route, kept, routed_item
|
|
from release_plan.todo_table import Item
|
|
from transcript_archive.read import latest_worker
|
|
from loop_claude.loop.this_loop import Loop
|
|
from loop_claude.cycle import prompts
|
|
from model_routing.route import Route, route_from
|
|
from model_routing.ladder import Rung
|
|
from claude_code.session import session_open, transcript_file
|
|
from loop_state.running_loops import taken_items
|
|
|
|
# Seconds routing waits for origin to name its default branch, asked only where the checkout records none; past them, the chunk runs on the project's rung.
|
|
ORIGIN_TIMEOUT = 60
|
|
|
|
|
|
def shown_items(loop: Loop, todo: str | Failure, routed: int) -> typing.Callable[[tuple[int, ...]], None]:
|
|
"""A function that lists in loop-inbox the items a chunk takes, given the IDs the worker names, with their taglines in todo, as fetched_todo read it as the cycle started. routed is the item the chunk was routed on, 0 for none."""
|
|
return lambda taken_in: loop.listed.show(taken_items(todo, routed, taken_in))
|
|
|
|
|
|
def fetched_todo(loop: Loop) -> str | Failure:
|
|
"""todo.md on the default branch, as last fetched, which lists and scores the items a chunk takes. fetched_plan reads it too, but also the release tag, CHANGELOG.md and each todo.md since the tag, which neither needs."""
|
|
ref = default_ref(loop.root, ORIGIN_TIMEOUT, run=loop.effects.git)
|
|
return ref if isinstance(ref, Failure) else file_at(ref, "todo.md", loop.root, loop.effects.git)
|
|
|
|
|
|
def routed(route: Route | None, project: Rung | None, model: str, archived: typing.Callable[[], Route | None], item: typing.Callable[[], Item | None]) -> Route:
|
|
"""The route the cycle runs on. project is the project's rung, and model the project's model.
|
|
|
|
Where project is None, the model is off the ladder, and the worker runs on it. Otherwise the chunk keeps route, or on a rerun the route its archive last names, as routing.kept allows. Else the plan item routes the chunk afresh.
|
|
"""
|
|
if project is None:
|
|
return Route(Rung(model))
|
|
route = route or archived()
|
|
return route if route and kept(route, project) else first_route(project, item())
|
|
|
|
|
|
def archived_route(archive_folder: pathlib.Path, loop: Loop) -> Route | None:
|
|
"""The route named by the latest verdict archived for the loop's open chunk; None for a chunk not yet judged."""
|
|
transcript = transcript_file(loop.session)
|
|
worker = latest_worker(archive_folder, transcript) if transcript else None
|
|
if isinstance(worker, Failure):
|
|
say(f"routing the chunk afresh: {worker.reason}", loop.effects)
|
|
return None
|
|
route = route_from(worker) if worker else None
|
|
if worker and route is None:
|
|
say(f"routing the chunk afresh: the archive's latest Worker: line names no route loop-claude can read: {worker}", loop.effects)
|
|
return route
|
|
|
|
|
|
def chunk_start(loop: Loop) -> tuple[str, typing.Callable[[], Item | None]]:
|
|
"""The facts for a fresh worker's prompt, empty where the session is open, and the reader of the item that routes the chunk. The reader reads the default branch as last fetched: for a fresh chunk, by the preflight that runs first; for an open session, which runs none, by whatever fetched last."""
|
|
facts = "" if session_open(loop.session) else chunk_facts(loop)
|
|
return facts, lambda: plan_item(fetched_plan(loop), loop.effects)
|
|
|
|
|
|
def chunk_facts(loop: Loop) -> str:
|
|
"""What `chunk-preflight --worktree` printed at the checkout root, for a fresh worker's prompt, or why it could not run."""
|
|
done = loop.effects.python([str(loop.launcher.with_name("chunk-preflight")), "--worktree"], loop.root)
|
|
if done.returncode:
|
|
return prompts.preflight_failed(done.stderr.strip() or f"it exited {done.returncode}")
|
|
return prompts.preflight_facts(done.stdout.strip())
|
|
|
|
|
|
def fetched_plan(loop: Loop) -> DefaultPlan | Failure:
|
|
"""What the default branch, as last fetched, holds for routing."""
|
|
ref = default_ref(loop.root, ORIGIN_TIMEOUT, run=loop.effects.git)
|
|
return ref if isinstance(ref, Failure) else default_plan(loop.root, ref, loop.effects.git)
|
|
|
|
|
|
def plan_item(plan: DefaultPlan | Failure, effects: Effects) -> Item | None:
|
|
"""The item that routes a chunk, release_standing.chunk_item's pick from the default branch; None without one, so the chunk runs on the project's rung."""
|
|
# It prints the reason itself, through effects, like the other loop-claude functions that print. todo.md item 51 moves them all to one injected effect, so changing this one alone would be undone.
|
|
if isinstance(plan, Failure):
|
|
item: Item | Failure | None = plan
|
|
elif isinstance(plan.todo, Failure):
|
|
item = plan.todo
|
|
else:
|
|
item = routed_item(plan.todo, plan.cut)
|
|
if isinstance(item, Failure):
|
|
say(f"routing on the project's rung: {item.reason}", effects)
|
|
return None
|
|
return item
|