Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s
CI / tests (pull_request) Successful in 3m43s
This commit is contained in:
@@ -5,11 +5,11 @@ 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
|
||||
from text_files.file_writes import replace_json
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
@@ -21,6 +21,8 @@ class Models:
|
||||
# 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:
|
||||
@@ -48,18 +50,29 @@ def get_or_ask_model(project_key: str, paths: Paths, driver: str, effects: Effec
|
||||
chosen = ask_for_model(project_key, saved, paths.model_catalog, driver, effects)
|
||||
if isinstance(chosen, Stop):
|
||||
return chosen
|
||||
# Reread: another loop may have saved its project while this one waited on the human.
|
||||
models = read_models_file(saved)
|
||||
if isinstance(models, Failure):
|
||||
return Stop(1, models.reason)
|
||||
if failure := replace_json(saved, {**models, project_key: chosen}, indent="\t", sort_keys=True):
|
||||
return Stop(1, f"could not save the model in {saved}: {failure.reason}")
|
||||
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 drivers 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 driver 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. loop-claude and the replay tools read it."""
|
||||
"""Each project's model, as the models file at saved maps them; none where there is no such file. The chunk drivers and the replay tools read it, and only save_model writes it."""
|
||||
try:
|
||||
with open(saved, encoding="utf-8") as handle:
|
||||
models = json.load(handle)
|
||||
|
||||
Reference in New Issue
Block a user