Answer architecture round 1: 1 🔴, 5 🟡; track bin/model_routing, hand over the route by its rung, share the R + C mapping, record the routing item, and have the judge report R + C plainly
This commit is contained in:
@@ -44,11 +44,12 @@ Where that model is on the routing ladder (bin/model_routing/ladder.py), the wor
|
||||
interrupt run on a rung of it instead: a model at an effort. As a chunk starts, the R + C of
|
||||
todo.md's top item in the next release, skipping a question, picks the rung from the model's
|
||||
cheapest one, and never below it; without such an item, or with a C missing, the chunk runs on the
|
||||
model's cheapest rung. The worker's first prompt names the rung and asks it to say when the chunk
|
||||
takes in an item of higher R + C. The judge reports the highest such R + C it sees the worker say,
|
||||
and the next cycle steps up to the rung that picks, never down. Every second unfinished verdict on
|
||||
a chunk steps it up one rung. A rerun routes an open chunk afresh. The archive records each
|
||||
verdict's rung.
|
||||
model's cheapest rung. The worker's first prompt names the rung and the item, and asks it to name
|
||||
each other item the chunk takes in, with its R + C. The judge reports the highest R + C it sees the
|
||||
worker state, and the next cycle steps up to the rung that picks, never down. Every second
|
||||
unfinished verdict on a chunk steps it up one rung. A rung an edit took off the ladder counts as
|
||||
the model's cheapest. A rerun routes an open chunk afresh. The archive records each verdict's rung,
|
||||
with the item and R + C that routed the chunk.
|
||||
|
||||
A change to bin/loop-claude or any bin/**/*.py takes effect at the next cycle boundary, once
|
||||
loop-claude --help succeeds: the loop reloads into the edited script.
|
||||
@@ -95,13 +96,12 @@ from loop_claude.cycle.run import Cycle, run_cycle
|
||||
from loop_claude.loop.reload import decode_handed, exec_if_edited, fingerprint, LoopRun, RELOAD_FLAG
|
||||
from loop_claude.effects import Effects
|
||||
from loop_claude.cycle.judge import Finished, Unfinished
|
||||
from loop_claude.cycle.routing import first_rung, routed_item, stepped
|
||||
from loop_claude.loop.routing import first_route, Route, routed_item, stepped
|
||||
from loop_claude.project.model_choice import get_or_ask_models
|
||||
from model_routing.ladder import LADDER, project_rung, Rung
|
||||
from loop_claude.paths import Paths
|
||||
from transcript_archive.location import project_archive
|
||||
from project_key import project_key
|
||||
from loop_claude.cycle import prompts
|
||||
from model_routing.ladder import project_rung, Rung
|
||||
from loop_claude.project.premises import read_premises
|
||||
from loop_claude.project.records import read_records
|
||||
from claude_code.session import session_named
|
||||
@@ -141,30 +141,29 @@ def run_loop(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launc
|
||||
if isinstance(models, Stop):
|
||||
return models
|
||||
anchor = project_rung(models.worker)
|
||||
loop_run, worker, routed = route_worker(loop_run, anchor, models.worker)
|
||||
print(f"\n=== cycle {loop_run.cycle_number}, worker on {worker} ===", flush=True)
|
||||
verdict = run_cycle(Cycle(effects=effects, judge_model=models.judge, paths=paths, premises=premises, project_archive=archive, records=records, root=loop_run.root, routed=routed, session=session, task=loop_run.task, unfinished_evidence=loop_run.unfinished_evidence, worker=worker))
|
||||
loop_run = routed(loop_run, anchor)
|
||||
route = loop_run.route or Route(Rung(models.worker))
|
||||
print(f"\n=== cycle {loop_run.cycle_number}, worker on {route.rung} ===", flush=True)
|
||||
verdict = run_cycle(Cycle(effects=effects, judge_model=models.judge, paths=paths, premises=premises, project_archive=archive, records=records, root=loop_run.root, route=route, session=session, task=loop_run.task, unfinished_evidence=loop_run.unfinished_evidence))
|
||||
if isinstance(verdict, Stop):
|
||||
return verdict
|
||||
loop_run = after_verdict(loop_run, anchor, verdict)
|
||||
|
||||
|
||||
def route_worker(loop_run: LoopRun, anchor: int | None, project_model: str) -> tuple[LoopRun, Rung, str]:
|
||||
"""The loop run, the worker's rung, and what the fresh prompt says of its route; a chunk is routed once, as it starts."""
|
||||
def routed(loop_run: LoopRun, anchor: int | None) -> LoopRun:
|
||||
"""The loop run with its chunk's route; a chunk is routed once, as it starts, and not at all off the ladder."""
|
||||
if anchor is None:
|
||||
return loop_run, Rung(project_model), ""
|
||||
if loop_run.rung >= 0:
|
||||
return loop_run, LADDER[loop_run.rung], ""
|
||||
item = routed_item(read_todo(loop_run.root))
|
||||
loop_run = dataclasses.replace(loop_run, rung=first_rung(anchor, item), unfinished=0)
|
||||
return loop_run, LADDER[loop_run.rung], prompts.routed(LADDER[loop_run.rung], item) if item else ""
|
||||
return dataclasses.replace(loop_run, route=None)
|
||||
if loop_run.route:
|
||||
return loop_run
|
||||
return dataclasses.replace(loop_run, route=first_route(anchor, routed_item(read_todo(loop_run.root))))
|
||||
|
||||
|
||||
def after_verdict(loop_run: LoopRun, anchor: int | None, verdict: Finished | Unfinished) -> LoopRun:
|
||||
if isinstance(verdict, Finished):
|
||||
return dataclasses.replace(loop_run, rung=-1, unfinished=0, unfinished_evidence="")
|
||||
rung, unfinished = stepped(anchor, loop_run.rung, loop_run.unfinished, verdict.bundled) if anchor is not None else (loop_run.rung, 0)
|
||||
return dataclasses.replace(loop_run, rung=rung, unfinished=unfinished, unfinished_evidence=verdict.evidence)
|
||||
return dataclasses.replace(loop_run, route=None, unfinished_evidence="")
|
||||
route = stepped(anchor, loop_run.route, verdict.bundled) if anchor is not None and loop_run.route else loop_run.route
|
||||
return dataclasses.replace(loop_run, route=route, unfinished_evidence=verdict.evidence)
|
||||
|
||||
|
||||
def read_todo(root: str) -> str:
|
||||
|
||||
@@ -3,9 +3,11 @@ import hashlib
|
||||
import json
|
||||
import pathlib
|
||||
import sys
|
||||
import types
|
||||
import typing
|
||||
|
||||
from loop_claude.effects import Effects
|
||||
from loop_claude.loop.routing import Route
|
||||
from loop_claude.stop import Stop
|
||||
|
||||
RELOAD_FLAG = "--reloaded"
|
||||
@@ -20,9 +22,8 @@ class LoopRun:
|
||||
root: str
|
||||
task: str
|
||||
unfinished_evidence: str
|
||||
# The chunk's worker rung on the ladder, -1 until the chunk is routed; and the unfinished verdicts since it last stepped up.
|
||||
rung: int = -1
|
||||
unfinished: int = 0
|
||||
# None until the chunk is routed, and where the project's model is off the ladder.
|
||||
route: Route | None = None
|
||||
|
||||
|
||||
def decode_handed(handed: str, kind: type[T]) -> T | None:
|
||||
@@ -30,12 +31,24 @@ def decode_handed(handed: str, kind: type[T]) -> T | None:
|
||||
fields = json.loads(handed)
|
||||
except ValueError:
|
||||
return None
|
||||
shape = typing.get_type_hints(kind)
|
||||
if not isinstance(fields, dict) or fields.keys() != shape.keys():
|
||||
return None
|
||||
if any(type(fields[name]) is not type_ for name, type_ in shape.items()):
|
||||
return None
|
||||
return kind(**fields)
|
||||
decoded = decoded_as(fields, kind)
|
||||
return None if decoded is REJECTED else decoded
|
||||
|
||||
|
||||
REJECTED = object()
|
||||
|
||||
|
||||
def decoded_as(value: object, kind: typing.Any) -> typing.Any:
|
||||
"""value as kind, or REJECTED: a dataclass from an object holding exactly its fields, a union's first member that fits, else a value of exactly kind."""
|
||||
if isinstance(kind, types.UnionType):
|
||||
return next((found for option in typing.get_args(kind) if (found := decoded_as(value, option)) is not REJECTED), REJECTED)
|
||||
if isinstance(kind, type) and dataclasses.is_dataclass(kind):
|
||||
shape = typing.get_type_hints(kind)
|
||||
if not isinstance(value, dict) or value.keys() != shape.keys():
|
||||
return REJECTED
|
||||
fields = {name: decoded_as(value[name], type_) for name, type_ in shape.items()}
|
||||
return REJECTED if any(field is REJECTED for field in fields.values()) else kind(**fields)
|
||||
return value if type(value) is kind else REJECTED
|
||||
|
||||
|
||||
def fingerprint(launcher: pathlib.Path) -> str:
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
"""Which ladder rung a chunk's worker runs on: docs/decisions.md → `routing:`."""
|
||||
import dataclasses
|
||||
|
||||
from model_routing.ladder import climbed, index_of, LADDER, picked, Rung
|
||||
from failure import Failure
|
||||
from todo_table import Item, items
|
||||
|
||||
UNFINISHED_PER_STEP = 2
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Route:
|
||||
"""A chunk's worker rung, the plan item whose R + C picked it (0 for none), and the unfinished verdicts since it last stepped up."""
|
||||
rung: Rung
|
||||
item: int = 0
|
||||
risk_and_complexity: int = 0
|
||||
unfinished: int = 0
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.rung}, routed by item {self.item}'s R + C of {self.risk_and_complexity}" if self.item else str(self.rung)
|
||||
|
||||
|
||||
def routed_item(todo: str) -> Item | None:
|
||||
"""The next release's first item that waits on no question, as rank_todo.py ranks the table; None when there is none, or it has no C."""
|
||||
found = items(todo)
|
||||
if not found or isinstance(found, Failure):
|
||||
return None
|
||||
next_release = min(item.release for item in found)
|
||||
takes = next((item for item in found if item.release == next_release and item.exempt != "question"), None)
|
||||
return takes if takes and takes.risk_and_complexity else None
|
||||
|
||||
|
||||
def first_route(anchor: int, item: Item | None) -> Route:
|
||||
"""The route a chunk starts on: the rung its item's R + C picks from anchor, the project's rung, never below it."""
|
||||
if item is None:
|
||||
return Route(LADDER[anchor])
|
||||
return Route(LADDER[max(anchor, picked(anchor, item.risk_and_complexity))], item.id, item.risk_and_complexity)
|
||||
|
||||
|
||||
def stepped(anchor: int, route: Route, bundled: int) -> Route:
|
||||
"""The route after one more unfinished verdict; bundled is the highest R + C the judge saw the worker take into the chunk, 0 for none.
|
||||
|
||||
A rung an edit took off the ladder counts as the project's rung.
|
||||
"""
|
||||
rung = index_of(route.rung)
|
||||
rung = max(anchor if rung is None else rung, picked(anchor, bundled) if bundled else anchor)
|
||||
unfinished = route.unfinished + 1
|
||||
if unfinished >= UNFINISHED_PER_STEP:
|
||||
return dataclasses.replace(route, rung=LADDER[climbed(rung, 1)], unfinished=0)
|
||||
return dataclasses.replace(route, rung=LADDER[rung], unfinished=unfinished)
|
||||
Reference in New Issue
Block a user