"""Read a routing ladder from its JSON file: the one this setup ships, or a project's own, kept in its config's folder. docs/decisions.md → `routing: one ladder of (model, effort) rungs`.""" import json import pathlib import re from model_routing.ladder import EFFORTS, Ladder, Rung from failure import Failure from project_config.main import located from project_config.settings import Unnamed # The file's name in both places. Three readers, seated as the human who writes an override in a hurry, picked a bare list of objects over a list of strings, a list of pairs and a list under a key. Two of them picked one name for both files: an override is then a copy of the shipped file with rungs cut. NAME = "routing-ladder.json" # The ladder this setup ships, beside this module, so a copy of bin/ carries it: mutate-changes and the tests of a reload each run the tools from such a copy. SHIPPED = pathlib.Path(__file__).resolve().with_name(NAME) # A model as Rung.family reads it: the Agent tool's family name follows the first hyphen. MODEL = re.compile(r"[^\s-]+(-[^\s-]+)+") SHAPE = f'a JSON list of rungs, least competent first, each as {{"model": "claude-opus-5-5", "effort": "medium"}}: a model\'s full name, one model for each family such as opus, and an effort of {", ".join(EFFORTS[:-1])} or {EFFORTS[-1]}' def project_ladder_file(directory: str | None, home: pathlib.Path, shipped: pathlib.Path = SHIPPED) -> pathlib.Path | Failure: """The file that holds the ladder of the project whose checkout holds directory, else the current directory: the file NAME in its config's folder under home, or shipped where that folder holds none. Also shipped where no config names a checkout there, or git fails there. A Failure where the project's config is faulty or cannot be read, since its folder may hold the ladder.""" config = located(directory, home) if isinstance(config, Unnamed): return shipped if isinstance(config, Failure): return config own = config.folder / NAME return own if own.exists() else shipped def project_ladder(directory: str | None, home: pathlib.Path, shipped: pathlib.Path = SHIPPED) -> Ladder | Failure: """The ladder project_ladder_file's file holds. A Failure where that file holds a fault too: the shipped ladder never stands in for a project's own, since work routed on it could run on a model the project cannot reach.""" file = project_ladder_file(directory, home, shipped) return file if isinstance(file, Failure) else read_ladder(file) def read_ladder(file: pathlib.Path) -> Ladder | Failure: """The ladder a file holds; a Failure naming the file and its fault where it holds none.""" try: held = json.loads(file.read_text(encoding="utf-8")) except (OSError, UnicodeDecodeError, ValueError) as failure: # An OSError's own text names the file again. return Failure(f"cannot read the routing ladder {file}: {failure.strerror if isinstance(failure, OSError) else failure}. The routing ladder is {SHAPE}") faults = faults_of(held) if faults: return Failure(f"{file}: {'; '.join(faults)}. The routing ladder is {SHAPE}") return Ladder(tuple(Rung(rung["model"], rung["effort"]) for rung in held)) def faults_of(held: object) -> list[str]: """What keeps held from being a ladder: it is no list or an empty one, or else each rung, by its number from 1, that is misspelt, repeats one above it, or names a second model of a family.""" if not isinstance(held, list) or not held: return ["it holds no list of rungs"] faults = [] # The first rung of each family, by number: the Agent tool launches by family name, which Ladder.from_agent reads back as that family's one model. families: dict[str, tuple[int, str]] = {} for number, rung in enumerate(held, 1): if not well_formed(rung): faults.append(f"rung {number} is {json.dumps(rung)}") continue family = Rung(rung["model"]).family() first, model = families.setdefault(family, (number, rung["model"])) if rung in held[:number - 1]: faults.append(f"rung {number} repeats rung {held.index(rung) + 1}") elif model != rung["model"]: faults.append(f"rung {number} names a second model of the family {family}, after rung {first}") return faults def well_formed(rung: object) -> bool: return isinstance(rung, dict) and rung.keys() == {"model", "effort"} and isinstance(rung["model"], str) and bool(MODEL.fullmatch(rung["model"])) and rung["effort"] in EFFORTS