Name loop_claude's modules and words by what each holds, explain the reload, and tie each exit-1 clause of --help to its Stop

This commit is contained in:
2026-10-02 08:42:29 +02:00
parent dc844030fa
commit 211524196b
27 changed files with 212 additions and 165 deletions
+19 -19
View File
@@ -4,12 +4,12 @@ import pathlib
import sys
import typing
from loop_claude import human_needed
from loop_claude.claude_code.catalog import offered_models
from loop_claude.claude_code.catalog import catalog_models
from loop_claude.effects import Effects
from loop_claude.failure import Failure
from loop_claude.file_writes import replace_json
from loop_claude.outcome import Failure, Stop
from loop_claude.paths import Paths
from loop_claude.stop import Stop, needs_human, needs_terminal
@dataclasses.dataclass(frozen=True)
@@ -18,37 +18,37 @@ class Models:
worker: str
def project_models(project: str, paths: Paths, env: typing.Mapping[str, str], effects: Effects) -> Models | Stop:
def models_for(project_key: str, paths: Paths, env: typing.Mapping[str, str], effects: Effects) -> Models | Stop:
judge, worker = env.get("LOOP_CLAUDE_JUDGE_MODEL"), env.get("LOOP_CLAUDE_WORKER_MODEL")
if judge and worker:
return Models(judge, worker)
chosen = project_model(project, paths, effects)
chosen = saved_model(project_key, paths, effects)
if isinstance(chosen, Stop):
return chosen
return Models(judge or chosen, worker or chosen)
def project_model(name: str, paths: Paths, effects: Effects) -> str | Stop:
def saved_model(project_key: str, paths: Paths, effects: Effects) -> str | Stop:
saved = paths.models_file
models = read_models(saved)
models = read_models_file(saved)
if isinstance(models, Stop):
return models
if saved_model := models.get(name):
return saved_model
chosen = ask_model(name, saved, paths.model_catalog, effects)
if kept := models.get(project_key):
return kept
chosen = ask_for_model(project_key, saved, paths.model_catalog, 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(saved)
models = read_models_file(saved)
if isinstance(models, Stop):
return models
if failure := replace_json(saved, {**models, name: chosen}, indent="\t", sort_keys=True):
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}")
print(f"{name} runs on {chosen}", file=sys.stderr)
print(f"{project_key} runs on {chosen}", file=sys.stderr)
return chosen
def read_models(saved: pathlib.Path) -> dict[str, str] | Stop:
def read_models_file(saved: pathlib.Path) -> dict[str, str] | Stop:
try:
with open(saved, encoding="utf-8") as handle:
models = json.load(handle)
@@ -61,11 +61,11 @@ def read_models(saved: pathlib.Path) -> dict[str, str] | Stop:
return models
def ask_model(name: str, saved: pathlib.Path, folder: pathlib.Path, effects: Effects) -> str | Stop:
if stop := human_needed.off_terminal(f"pick {name}'s model, or merge {{\"{name}\": \"<model>\"}} into {saved}", effects.on_terminal()):
def ask_for_model(project_key: str, saved: pathlib.Path, folder: pathlib.Path, effects: Effects) -> str | Stop:
if stop := needs_terminal(f"pick {project_key}'s model, or merge {{\"{project_key}\": \"<model>\"}} into {saved}", effects.on_terminal()):
return stop
offered = offered_models(folder)
print(f"\nThe model to run {name} on, saved in {saved}:", file=sys.stderr)
offered = catalog_models(folder)
print(f"\nThe model to run {project_key} on, saved in {saved}:", file=sys.stderr)
for number, (model_id, label) in enumerate(offered, 1):
print(f" {number}. {label} ({model_id})", file=sys.stderr)
if not offered:
@@ -75,7 +75,7 @@ def ask_model(name: str, saved: pathlib.Path, folder: pathlib.Path, effects: Eff
answer = effects.input(f"{'A number, or any' if offered else 'A'} name claude --model takes: ")
except EOFError:
print(file=sys.stderr)
return human_needed.stop(f"pick {name}'s model", "the input ended")
return needs_human(f"pick {project_key}'s model", "the input ended")
picked = model_picked(answer, offered)
if isinstance(picked, Failure):
print(picked.reason, file=sys.stderr)