115 lines
6.2 KiB
Python
115 lines
6.2 KiB
Python
"""How a cycle starts, for every entry point that drives chunks: the models and the ladder it routes on, the route its worker runs on, and the Cycle built from them.
|
|
|
|
An entry point starts a cycle in this order:
|
|
|
|
1. It reads what it alone knows, such as the premises, the records and the task.
|
|
2. `project_routing` or `routing_on` reads the models and the ladder.
|
|
3. `announced_route` picks the route and prints the cycle's opening line.
|
|
4. `cycle_of` builds the Cycle.
|
|
|
|
Each read is made afresh every cycle, so an edit between two cycles takes effect in the next.
|
|
"""
|
|
import dataclasses
|
|
import pathlib
|
|
import typing
|
|
|
|
from agent_cycle.run import Cycle
|
|
from claude_code.entries import Entry
|
|
from failure import Failure
|
|
from agent_cycle.routing import first_route, judge_rung, kept, Scored
|
|
from chunk_driver.project.model_choice import get_or_ask_models, Models
|
|
from git_cli.git_repo import Git
|
|
from model_routing.ladder import Ladder, ProjectRung, Rung
|
|
from transcript_archive.read import latest_worker
|
|
from chunk_driver.this_loop import Loop
|
|
from transcript_archive.project_key import project_key
|
|
from model_routing.ladder_file import project_ladder_file, read_ladder
|
|
from model_routing.route import Route, route_from
|
|
from loop_effects.effects import say
|
|
from agent_cycle.stop import Stop
|
|
from agent_cycle.verdicts import TakenIn
|
|
from claude_code.session import transcript_file
|
|
from agent_cycle.worker_prompts import WorkerPrompts
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Routing:
|
|
"""What a cycle routes on, as read when it started."""
|
|
# The project's worker and judge models.
|
|
models: Models
|
|
ladder: Ladder
|
|
# The file the ladder was read from: the shipped one, or the project's own.
|
|
ladder_file: pathlib.Path
|
|
|
|
@property
|
|
def project(self) -> ProjectRung | None:
|
|
"""The project's rung on the ladder; None for a worker model off the ladder."""
|
|
return self.ladder.project_rung(self.models.worker)
|
|
|
|
|
|
def project_routing(loop: Loop, driver: str) -> Routing | Stop:
|
|
"""The routing of the project checked out at the loop's root: its models, asked of the human where none is saved, and its ladder. The stop where one cannot be read."""
|
|
models = get_or_ask_models(project_key(loop.root, loop.paths.home), loop.paths, loop.env, driver, loop.effects)
|
|
if isinstance(models, Stop):
|
|
return models
|
|
ladder_file = project_ladder_file(loop.root, loop.paths.home)
|
|
return Stop(1, ladder_file.reason) if isinstance(ladder_file, Failure) else routing_on(models, ladder_file)
|
|
|
|
|
|
def routing_on(models: Models, ladder_file: pathlib.Path) -> Routing | Stop:
|
|
"""The routing of models on the ladder ladder_file holds; the stop where it cannot be read."""
|
|
ladder = read_ladder(ladder_file)
|
|
return Stop(1, ladder.reason) if isinstance(ladder, Failure) else Routing(models, ladder, ladder_file)
|
|
|
|
|
|
def announced_route(cycle_number: int, kept_route: Route | None, routing: Routing, archive_folder: pathlib.Path, item: typing.Callable[[], Scored | None], loop: Loop) -> Route:
|
|
"""The route cycle cycle_number runs on, printed with the cycle's opening line. kept_route is the one the chunk's run holds, and item finds what routes a fresh chunk."""
|
|
route = routed(kept_route, routing.project, routing.models.worker, lambda: archived_route(archive_folder, loop), item)
|
|
say(f"\n=== cycle {cycle_number}, worker on {route.rung} ===", loop.effects, stdout=True)
|
|
return route
|
|
|
|
|
|
def routed(route: Route | None, project: ProjectRung | None, model: str, archived: typing.Callable[[], Route | None], item: typing.Callable[[], Scored | 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 what item finds 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 that can be read: {worker}", loop.effects)
|
|
return route
|
|
|
|
|
|
def cycle_of(loop: Loop, routing: Routing, route: Route, archive_folder: pathlib.Path, *, task: str, facts: str, premises: str, records: str, unfinished_evidence: str, prompts: WorkerPrompts, taken_in: typing.Callable[[list[Entry]], TakenIn]) -> Cycle:
|
|
"""The cycle that runs in the loop's window on route. The keyword arguments are what the entry point alone knows; Cycle says what each is."""
|
|
return Cycle(config=setup_version(loop.paths.claude_dir, loop.effects.git), effects=loop.effects, facts=facts, judge=judge_rung(routing.ladder, routing.models.judge), premises=premises, project=routing.project, project_archive=archive_folder, records=records, root=loop.root, route=route, session=loop.session, task=task, prompts=prompts, taken_in=taken_in, unfinished_evidence=unfinished_evidence, window=loop.window)
|
|
|
|
|
|
def setup_version(claude_dir: pathlib.Path, git: Git) -> str:
|
|
"""The release and commit of this setup that the worker's claude reads, as `v0.8.0 (<sha>)`; a commit past the latest release reads as git describe gives it, such as `v0.8.0-3-g1a2b3c4 (<sha>)`. Where git can't tell, `unknown` and why."""
|
|
described = git("describe", "--tags", "--match", "v*", "--always", cwd=str(claude_dir))
|
|
commit = git("rev-parse", "HEAD", cwd=str(claude_dir))
|
|
if isinstance(described, Failure):
|
|
return unknown_version(described)
|
|
if isinstance(commit, Failure):
|
|
return unknown_version(commit)
|
|
return f"{described.strip()} ({commit.strip()})"
|
|
|
|
|
|
def unknown_version(failure: Failure) -> str:
|
|
# One line, since the archive reads the line after it as the judge's.
|
|
return f"unknown: {' '.join(failure.reason.split())}"
|