95 lines
5.4 KiB
Python
95 lines
5.4 KiB
Python
"""How a cycle starts: the route it runs on, from the archive or the plan, and the facts chunk-preflight finds for a fresh chunk."""
|
|
import pathlib
|
|
import typing
|
|
|
|
from git_cli.git_repo import default_ref
|
|
from release_plan.release_standing import DefaultPlan, project_plan, UNSEEN_RELEASES
|
|
from loop_effects.effects import Effects, say
|
|
from failure import Failure
|
|
from loop_claude.routing import first_route, kept, routed_item
|
|
from loop_claude.loop.chunk_windows import held_beside, item_of
|
|
from chunk_preflight.main import ITEM
|
|
from plan.todo_table import Item
|
|
from model_routing.ladder import Ladder, Rung
|
|
from transcript_archive.read import latest_worker
|
|
from project_config.main import located
|
|
from loop_claude.loop.this_loop import Loop
|
|
from plan_store.plans import Plan
|
|
from loop_claude.cycle import prompts
|
|
from model_routing.route import Route, route_from
|
|
from claude_code.session import session_open, transcript_file
|
|
|
|
# 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 scored_plan(default: DefaultPlan | Failure, effects: Effects) -> Plan | Failure:
|
|
"""The plan, which scores the items a chunk takes, from what the default branch holds; where it can't be read or scored, it says why."""
|
|
plan = default.plan if isinstance(default, DefaultPlan) else default
|
|
if isinstance(plan, Failure):
|
|
say(f"the items the worker names lift no rung: {plan.reason}", effects)
|
|
return plan
|
|
|
|
|
|
def routed(route: Route | None, ladder: Ladder, 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 on ladder, 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, ladder, project) else first_route(ladder, 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, DefaultPlan | Failure]:
|
|
"""The facts for a fresh worker's prompt, empty where the session is open, and what the default branch holds, read once for the cycle: it routes the chunk, through plan_item, and scores the items the worker names, through scored_plan. It 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, fetched_plan(loop)
|
|
|
|
|
|
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. The chunk of a window named for an item starts from that item, and either ends with the items the project's other chunks hold."""
|
|
item = item_of(loop.window.name)
|
|
done = loop.effects.python([str(loop.launcher.with_name("chunk-preflight")), "--worktree", *([] if item is None else [ITEM, str(item)])], loop.root)
|
|
facts = prompts.preflight_failed(done.stderr.strip() or f"it exited {done.returncode}") if done.returncode else prompts.preflight_facts(done.stdout.strip())
|
|
held = held_beside(loop.window.name, loop.root, loop.paths, loop.effects)
|
|
return facts + (prompts.held_by_others(held) if held else "")
|
|
|
|
|
|
def fetched_plan(loop: Loop) -> DefaultPlan | Failure:
|
|
"""What the default branch, as last fetched, holds for routing; the plan and the changelog as they stand where the project's config puts them outside the checkout. It says UNSEEN_RELEASES for such a plan."""
|
|
config = located(loop.root, loop.paths.home)
|
|
if isinstance(config, Failure):
|
|
return config
|
|
if config.tracked("plan") is None:
|
|
say(UNSEEN_RELEASES, loop.effects)
|
|
ref = default_ref(loop.root, ORIGIN_TIMEOUT, run=loop.effects.git)
|
|
return ref if isinstance(ref, Failure) else project_plan(config, ref, loop.root, loop.effects.git)
|
|
|
|
|
|
def plan_item(plan: DefaultPlan | Failure, window_item: int | None, effects: Effects) -> Item | None:
|
|
"""The item that routes a chunk: the one its window is named for, or else 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
|
|
else:
|
|
item = routed_item(plan.plan, plan.cut, window_item)
|
|
if isinstance(item, Failure):
|
|
say(f"routing on the project's rung: {item.reason}", effects)
|
|
return None
|
|
return item
|