"""Read a routing ladder from its JSON file: the one this setup ships, or the one a project keeps in its config's folder in its place. 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: the tests of a reload and mutate-changes 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"}}, with an effort of {", ".join(EFFORTS[:-1])} or {EFFORTS[-1]}' def project_ladder(directory: str | None, home: pathlib.Path, shipped: pathlib.Path = SHIPPED) -> Ladder | Failure: """The ladder of the project whose checkout holds directory, else the current directory: the file NAME in its config's folder under home, or the shipped one where that folder holds none. Where no config names a checkout there, the shipped one. A Failure where the project's config or the ladder's file is faulty or cannot be read, since a tool on another ladder than the project's would route its work wrong.""" config = located(directory, home) if isinstance(config, Unnamed): return read_ladder(shipped) if isinstance(config, Failure): return config own = config.folder / NAME return read_ladder(own if own.exists() else shipped) 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: return Failure(f"cannot read the routing ladder {file}: {failure}. It holds {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 that is misspelt or repeats one above it, by its number from 1.""" if not isinstance(held, list) or not held: return ["it holds no list of rungs"] faults = [] for number, rung in enumerate(held, 1): if not well_formed(rung): faults.append(f"rung {number} is {json.dumps(rung)}") elif rung in held[:number - 1]: faults.append(f"rung {number} repeats rung {held.index(rung) + 1}") 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