Give the reload and the strike their own modules

This commit is contained in:
2026-09-28 11:11:23 +02:00
parent 5de7e0fc9c
commit 4171a1ecbe
9 changed files with 88 additions and 69 deletions
+12 -42
View File
@@ -43,19 +43,14 @@ Claude Code internals this rests on, observed on 2.1.282, the model catalog on 2
file has the highest fetchedAt.
"""
import dataclasses
import hashlib
import json
import os
import pathlib
import subprocess
import sys
from loop_claude.checkout import checkout_root
from loop_claude.cycle import parse, project_model, run_cycle, strike
from loop_claude.cycle import project_model, run_cycle
from loop_claude.outcome import Continue, Retry, Stop
from loop_claude.premises import read_premises
RELOAD_FLAG = "--reloaded"
from loop_claude.reload import RELOAD_FLAG, decode_state, fingerprint, reload_if_edited
from loop_claude.strike import strike
@dataclasses.dataclass(frozen=True)
@@ -66,9 +61,9 @@ class LoopState:
task: str
def main(argv, env, paths, launcher):
def main(argv, env, paths, launcher, cwd):
try:
stop = run_loop(argv, env, paths, launcher)
stop = run_loop(argv, env, paths, launcher, cwd)
except KeyboardInterrupt:
stop = Stop(130, "")
if stop.reason:
@@ -76,8 +71,8 @@ def main(argv, env, paths, launcher):
return stop.code
def run_loop(argv, env, paths, launcher):
state = loop_state(argv)
def run_loop(argv, env, paths, launcher, cwd):
state = loop_state(argv, cwd)
if isinstance(state, Stop):
return state
baseline = fingerprint(launcher)
@@ -102,40 +97,15 @@ def run_loop(argv, env, paths, launcher):
state = dataclasses.replace(state, failures=0)
# The reload
def loop_state(argv):
def loop_state(argv, cwd):
if argv[:1] != [RELOAD_FLAG]:
task = " ".join(argv)
if task in ("", "-h", "--help"):
return Stop(0 if task else 1, __doc__.strip())
root = checkout_root()
root = checkout_root(cwd)
return root if isinstance(root, Stop) else LoopState(0, 0, root, task)
handed = parse(argv[1]) if len(argv) == 2 else None
shape = {field.name: field.type for field in dataclasses.fields(LoopState)}
if not handed or handed.keys() != shape.keys() or any(type(handed[name]) is not kind for name, kind in shape.items()) or not (handed["root"] and handed["task"]):
state = decode_state(argv[1], LoopState) if len(argv) == 2 else None
if not (state and state.root and state.task):
return Stop(1, f"the reload cannot read the loop's state from {argv[1:]}; rerun loop-claude <task>")
return LoopState(**handed)
def fingerprint(launcher):
# An unreadable file hashes its error, and the --help trial judges it.
digest = hashlib.sha256()
for path in [launcher, *sorted(pathlib.Path(__file__).parent.glob("*.py"))]:
try:
content = path.read_bytes()
except OSError as failure:
content = str(failure).encode()
digest.update(f"{path.name}\0{len(content)}\0".encode() + content)
return digest.hexdigest()
def reload_if_edited(launcher, baseline, state):
if fingerprint(launcher) == baseline:
return
trial = subprocess.run([sys.executable, launcher, "--help"], capture_output=True, text=True)
if trial.returncode != 0:
print(f"keeping the running script: {trial.stderr.strip() or f'exit {trial.returncode}'}", file=sys.stderr)
return
os.execv(sys.executable, [sys.executable, str(launcher), RELOAD_FLAG, json.dumps(dataclasses.asdict(state))])
return state