Give the reload and the strike their own modules
This commit is contained in:
@@ -3,8 +3,8 @@ import subprocess
|
||||
from loop_claude.outcome import Stop
|
||||
|
||||
|
||||
def checkout_root():
|
||||
found = subprocess.run(["git", "rev-parse", "--show-toplevel"], capture_output=True, text=True)
|
||||
def checkout_root(cwd):
|
||||
found = subprocess.run(["git", "rev-parse", "--show-toplevel"], capture_output=True, cwd=cwd, text=True)
|
||||
if found.returncode != 0:
|
||||
return Stop(1, found.stderr.strip())
|
||||
return found.stdout.strip()
|
||||
|
||||
@@ -10,6 +10,7 @@ import uuid
|
||||
|
||||
from loop_claude.checkout import checkouts, project_name
|
||||
from loop_claude.outcome import Continue, Retry, Stop
|
||||
from loop_claude.strike import strike
|
||||
|
||||
SESSION_NAMESPACE = uuid.UUID("64fb586b-d926-4bb3-a44c-c903e86d3eec")
|
||||
|
||||
@@ -45,13 +46,6 @@ JUDGE_SCHEMA = {
|
||||
}
|
||||
|
||||
|
||||
def strike(who, failures, failure):
|
||||
if failures == 3:
|
||||
return Stop(1, f"the {who} failed three times running: {failure}")
|
||||
print(f"retrying the {who} in a minute: {failure}", file=sys.stderr)
|
||||
time.sleep(60)
|
||||
|
||||
|
||||
def run_cycle(root, task, premises, chosen, paths, env):
|
||||
name = session_name(root, paths.code)
|
||||
session_id = uuid.uuid5(SESSION_NAMESPACE, name)
|
||||
|
||||
+12
-42
@@ -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
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import dataclasses
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
RELOAD_FLAG = "--reloaded"
|
||||
|
||||
|
||||
def decode_state(handed, kind):
|
||||
try:
|
||||
fields = json.loads(handed)
|
||||
except ValueError:
|
||||
return None
|
||||
shape = {field.name: field.type for field in dataclasses.fields(kind)}
|
||||
if not isinstance(fields, dict) or fields.keys() != shape.keys():
|
||||
return None
|
||||
if any(type(fields[name]) is not type_ for name, type_ in shape.items()):
|
||||
return None
|
||||
return kind(**fields)
|
||||
|
||||
|
||||
def fingerprint(launcher):
|
||||
# An unreadable file hashes its error, and the --help trial judges it.
|
||||
digest = hashlib.sha256()
|
||||
for path in [launcher, *sorted((launcher.parent / "loop_claude").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))])
|
||||
@@ -0,0 +1,11 @@
|
||||
import sys
|
||||
import time
|
||||
|
||||
from loop_claude.outcome import Stop
|
||||
|
||||
|
||||
def strike(who, failures, failure):
|
||||
if failures == 3:
|
||||
return Stop(1, f"the {who} failed three times running: {failure}")
|
||||
print(f"retrying the {who} in a minute: {failure}", file=sys.stderr)
|
||||
time.sleep(60)
|
||||
Reference in New Issue
Block a user