117 lines
5.1 KiB
Python
117 lines
5.1 KiB
Python
import dataclasses
|
|
import json
|
|
import pathlib
|
|
import sys
|
|
import typing
|
|
|
|
from claude_code.model_catalog import catalog_models
|
|
from chunk_driver.claims import CLAIMS, locked
|
|
from loop_effects.effects import Effects, say
|
|
from failure import Failure
|
|
from agent_cycle.stop import needs_human, needs_terminal, Stop
|
|
from loop_state.paths import Paths
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Models:
|
|
judge: str
|
|
worker: str
|
|
|
|
|
|
# The environment variables that name the judge's and the worker's model for one run, over the model saved or configured.
|
|
JUDGE_MODEL = "LOOP_CLAUDE_JUDGE_MODEL"
|
|
WORKER_MODEL = "LOOP_CLAUDE_WORKER_MODEL"
|
|
# The file, in CLAIMS under the home folder, that save_model keeps locked with flock while it rereads and replaces the models file. The models file's own folder is a checkout of this setup, which takes no lock file.
|
|
MODELS_LOCK = "models.lock"
|
|
|
|
|
|
def overridden(env: typing.Mapping[str, str], default: str) -> Models:
|
|
"""The models of a run: default, unless env names another for the judge or the worker."""
|
|
return Models(env.get(JUDGE_MODEL) or default, env.get(WORKER_MODEL) or default)
|
|
|
|
|
|
def get_or_ask_models(project_key: str, paths: Paths, env: typing.Mapping[str, str], driver: str, effects: Effects) -> Models | Stop:
|
|
named = overridden(env, "")
|
|
if named.judge and named.worker:
|
|
return named
|
|
chosen = get_or_ask_model(project_key, paths, driver, effects)
|
|
if isinstance(chosen, Stop):
|
|
return chosen
|
|
return overridden(env, chosen)
|
|
|
|
|
|
def get_or_ask_model(project_key: str, paths: Paths, driver: str, effects: Effects) -> str | Stop:
|
|
saved = paths.models_file
|
|
models = read_models_file(saved)
|
|
if isinstance(models, Failure):
|
|
return Stop(1, models.reason)
|
|
if kept := models.get(project_key):
|
|
return kept
|
|
chosen = ask_for_model(project_key, saved, paths.model_catalog, driver, effects)
|
|
if isinstance(chosen, Stop):
|
|
return chosen
|
|
if failure := save_model(project_key, chosen, paths, effects.replace_file):
|
|
return Stop(1, failure.reason)
|
|
say(f"{project_key} runs on {chosen}", effects)
|
|
return chosen
|
|
|
|
|
|
def save_model(project_key: str, chosen: str, paths: Paths, replace: typing.Callable[[pathlib.Path, bytes], Failure | None]) -> Failure | None:
|
|
"""Adds the project's model to the models file, whose only writer this is, through replace, which writes a file as replace_file does. It holds MODELS_LOCK from its read to its write, so two entry points that save at once both keep their pick."""
|
|
saved = paths.models_file
|
|
with locked(paths.home / CLAIMS / MODELS_LOCK) as unlocked:
|
|
if unlocked:
|
|
return Failure(f"could not save the model in {saved}: {unlocked.reason}")
|
|
# Read under the lock: another entry point may have saved its project while this one waited on the human.
|
|
models = read_models_file(saved)
|
|
if isinstance(models, Failure):
|
|
return models
|
|
if failure := replace(saved, json.dumps({**models, project_key: chosen}, indent="\t", sort_keys=True).encode()):
|
|
return Failure(f"could not save the model in {saved}: {failure.reason}")
|
|
return None
|
|
|
|
|
|
def read_models_file(saved: pathlib.Path) -> dict[str, str] | Failure:
|
|
"""Each project's model, as the models file at saved maps them; none where there is no such file. The entry points, loop-claude and do-epic, and the replay tools read it."""
|
|
try:
|
|
with open(saved, encoding="utf-8") as handle:
|
|
models = json.load(handle)
|
|
except FileNotFoundError:
|
|
return {}
|
|
except (OSError, ValueError) as failure:
|
|
return Failure(f"could not read {saved}: {getattr(failure, 'strerror', None) or failure}")
|
|
if not isinstance(models, dict) or not all(isinstance(chosen, str) and chosen for chosen in models.values()):
|
|
return Failure(f"could not read {saved}: not an object mapping projects to model names")
|
|
return models
|
|
|
|
|
|
def ask_for_model(project_key: str, saved: pathlib.Path, folder: pathlib.Path, driver: str, effects: Effects) -> str | Stop:
|
|
if stop := needs_terminal(f"pick {project_key}'s model, or merge {{\"{project_key}\": \"<model>\"}} into {saved}", effects.on_terminal(), driver):
|
|
return stop
|
|
offered = catalog_models(folder)
|
|
choices = [f" {number}. {label} ({model_id})" for number, (model_id, label) in enumerate(offered, 1)] or [f" no model catalog cached in {folder}"]
|
|
say("\n".join([f"\nThe model to run {project_key} on, saved in {saved}:", *choices]), effects)
|
|
while True:
|
|
try:
|
|
answer = effects.input(f"{'A number, or any' if offered else 'A'} name claude --model takes: ")
|
|
except EOFError:
|
|
print(file=sys.stderr) # Ends the prompt's line. It isn't a message, so it gets no time.
|
|
return needs_human(f"pick {project_key}'s model", "the input ended")
|
|
picked = model_picked(answer, offered)
|
|
if isinstance(picked, Failure):
|
|
say(picked.reason, effects)
|
|
elif picked:
|
|
return picked
|
|
|
|
|
|
def model_picked(answer: str, offered: list[tuple[str, object]]) -> str | Failure | None:
|
|
"""The model the answer picks, a Failure naming the numbers to pick from, or None to ask again."""
|
|
answer = answer.strip()
|
|
if not answer:
|
|
return None
|
|
if not (offered and answer.isdecimal()):
|
|
return answer
|
|
if 1 <= int(answer) <= len(offered):
|
|
return offered[int(answer) - 1][0]
|
|
return Failure(f"pick 1 to {len(offered)}")
|