Title each decision by what holds now, and make the small fixes filed against bin/, the README, .gitignore and stability-reviewer
This commit is contained in:
@@ -67,7 +67,9 @@ def run_judge(root, args, prompt, transcript) -> Verdict | Failure:
|
||||
if proc.returncode != 0:
|
||||
return Failure(proc.stderr.strip() or f"the judge exited {proc.returncode}")
|
||||
verdict = verdict_from(structured_output(proc.stdout))
|
||||
if isinstance(verdict, Verdict) and not grounded(verdict, transcript):
|
||||
if isinstance(verdict, Failure):
|
||||
return Failure(f"{verdict.reason}; it replied: {proc.stdout.strip()}")
|
||||
if not grounded(verdict, transcript):
|
||||
return Failure(f"the judge quoted {verdict.evidence!r}, nothing in the transcript" + (f", for the question: {verdict.question}" if verdict.interrupt else ""))
|
||||
return verdict
|
||||
|
||||
|
||||
@@ -10,12 +10,13 @@ on the terminal, every worktree first marked trusted in ~/.claude.json. An unfin
|
||||
its session next cycle; the next chunk starts a fresh session.
|
||||
Cycles continue until an exit below or Ctrl-C. Words: ~/.claude/CLAUDE.md → "Vocabulary".
|
||||
|
||||
Exit 1: no task, not inside a checkout, no premises, a models file it cannot read or write, claude
|
||||
leaves no transcript or one it cannot read or archive, claude cannot start for an interrupt, a
|
||||
reload with unreadable state, or the worker or the judge fails three times running, retried a minute
|
||||
apart within the cycle. A worker fails when claude cannot start, exits non-zero, is killed, or ends
|
||||
without a result event; a judge when claude cannot start or exits non-zero, or its verdict misses or
|
||||
mistypes a field, interrupts without a question, or quotes nothing in the transcript.
|
||||
Exit 1: no task, git cannot start or finds no checkout, no premises, a models file it cannot read or
|
||||
write, claude leaves no transcript or one it cannot read or archive, claude cannot start for an
|
||||
interrupt, a reload that cannot start or reads unreadable state, or the worker or the judge fails
|
||||
three times running, retried a minute apart within the cycle. A worker fails when claude cannot
|
||||
start, exits non-zero, is killed, or ends without a result event; a judge when claude cannot start
|
||||
or exits non-zero, or its verdict misses or mistypes a field, interrupts without a question, or
|
||||
quotes nothing in the transcript.
|
||||
Exit 2: the human is needed, to pick the model or answer an interrupt, and stdin or stdout is not a
|
||||
terminal, the input ends, or the interrupt ended with nothing typed. Rerunning resumes any
|
||||
open session.
|
||||
@@ -63,7 +64,8 @@ def run_loop(argv, env, paths, launcher, cwd) -> Stop:
|
||||
project = project_name(state.root, paths.code)
|
||||
session = session_for(project, paths)
|
||||
while True:
|
||||
exec_if_edited(launcher, baseline, state)
|
||||
if stop := exec_if_edited(launcher, baseline, state):
|
||||
return stop
|
||||
state = dataclasses.replace(state, cycle_number=state.cycle_number + 1)
|
||||
premises = read_premises(state.root)
|
||||
if isinstance(premises, Stop):
|
||||
|
||||
@@ -6,6 +6,8 @@ import subprocess
|
||||
import sys
|
||||
import typing
|
||||
|
||||
from loop_claude.outcome import Stop
|
||||
|
||||
RELOAD_FLAG = "--reloaded"
|
||||
T = typing.TypeVar("T")
|
||||
|
||||
@@ -42,11 +44,15 @@ def fingerprint(launcher) -> str:
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def exec_if_edited(launcher, baseline, state) -> None:
|
||||
def exec_if_edited(launcher, baseline, state) -> Stop | None:
|
||||
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 None
|
||||
try:
|
||||
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 None
|
||||
os.execv(sys.executable, [sys.executable, str(launcher), RELOAD_FLAG, json.dumps(dataclasses.asdict(state))])
|
||||
except OSError as failure:
|
||||
return Stop(1, f"could not reload loop-claude: {failure.strerror}; rerun loop-claude <task>")
|
||||
return None
|
||||
|
||||
@@ -3,7 +3,7 @@ import dataclasses
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Failure:
|
||||
"""A step failed and its caller decides: `retried` reruns a worker or judge run until it fails three times running; anything else becomes a Stop or a warning."""
|
||||
"""A step failed, for reason; its caller decides what follows."""
|
||||
reason: str
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,10 @@ from loop_claude.outcome import Stop
|
||||
|
||||
|
||||
def checkout_root(cwd) -> str | Stop:
|
||||
found = subprocess.run(["git", "rev-parse", "--show-toplevel"], capture_output=True, cwd=cwd, text=True)
|
||||
try:
|
||||
found = subprocess.run(["git", "rev-parse", "--show-toplevel"], capture_output=True, cwd=cwd, text=True)
|
||||
except OSError as failure:
|
||||
return Stop(1, f"could not start git: {failure.strerror}")
|
||||
if found.returncode != 0:
|
||||
return Stop(1, found.stderr.strip())
|
||||
return found.stdout.strip()
|
||||
@@ -12,7 +15,10 @@ def checkout_root(cwd) -> str | Stop:
|
||||
|
||||
def checkouts(root) -> list[str]:
|
||||
found = [root]
|
||||
listing = subprocess.run(["git", "worktree", "list", "--porcelain"], capture_output=True, cwd=root, text=True)
|
||||
try:
|
||||
listing = subprocess.run(["git", "worktree", "list", "--porcelain"], capture_output=True, cwd=root, text=True)
|
||||
except OSError:
|
||||
return found
|
||||
if listing.returncode == 0:
|
||||
found += [line.split(" ", 1)[1] for line in listing.stdout.splitlines() if line.startswith("worktree ")]
|
||||
return sorted(set(found))
|
||||
|
||||
+14
-1
@@ -5,6 +5,7 @@ import pathlib
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
|
||||
|
||||
@@ -13,7 +14,7 @@ from loop_claude.claude_code import formats, transcript
|
||||
from loop_claude.cycle import judge
|
||||
from loop_claude.loop import main, reload
|
||||
from loop_claude.outcome import Failure, Stop
|
||||
from loop_claude.project import premises
|
||||
from loop_claude.project import checkout, premises
|
||||
|
||||
CWD = pathlib.Path("/c")
|
||||
|
||||
@@ -133,6 +134,18 @@ class LoopState(unittest.TestCase):
|
||||
self.assertEqual(reload.decode_state('{"count": 1, "name": "n"}', Stringly), Stringly(1, "n"))
|
||||
self.assertIsNone(reload.decode_state('{"count": "1", "name": "n"}', Stringly))
|
||||
|
||||
def test_stops_when_the_reload_cannot_start_python(self):
|
||||
with tempfile.TemporaryDirectory() as root, mock.patch("os.execv", side_effect=OSError(2, "No such file or directory")):
|
||||
launcher = pathlib.Path(root, "loop-claude")
|
||||
launcher.write_text("")
|
||||
self.assertEqual(reload.exec_if_edited(launcher, "stale", main.LoopState(1, "/r", "do it")), Stop(1, "could not reload loop-claude: No such file or directory; rerun loop-claude <task>"))
|
||||
|
||||
|
||||
class Checkouts(unittest.TestCase):
|
||||
def test_lists_the_root_alone_when_git_cannot_start(self):
|
||||
with tempfile.TemporaryDirectory() as empty, mock.patch.dict(os.environ, {"PATH": empty}):
|
||||
self.assertEqual(checkout.checkouts(empty), [empty])
|
||||
|
||||
|
||||
|
||||
class ReplaceFile(unittest.TestCase):
|
||||
|
||||
@@ -300,6 +300,10 @@ class Premises(unittest.TestCase):
|
||||
run = checkout.run([worker()])
|
||||
self.assertEqual((run.code, run.calls), (1, []))
|
||||
self.assertEqual(run.stderr.strip(), f"README.md in {checkout.root}: Is a directory - no premises.")
|
||||
(checkout.temp / "fake-bin" / "git").unlink()
|
||||
run = checkout.run([worker()])
|
||||
self.assertEqual((run.code, run.calls), (1, []))
|
||||
self.assertEqual(run.stderr.strip(), "could not start git: No such file or directory")
|
||||
|
||||
def test_names_the_session_by_the_checkout_relative_to_code_or_else_to_the_root(self):
|
||||
checkout = Checkout(self)
|
||||
@@ -378,8 +382,8 @@ class Cycles(unittest.TestCase):
|
||||
self.assertEqual(run.roles(), ["worker", "judge", "judge", "judge"])
|
||||
self.assertEqual(run.sleeps, [60, 60])
|
||||
self.assertIn("retrying the judge in a minute: judge down", run.stderr)
|
||||
self.assertIn("retrying the judge in a minute: the judge returned no verdict", run.stderr)
|
||||
self.assertEqual(run.stderr.strip().splitlines()[-1], "the judge failed three times running: the judge's verdict has no evidence of type string")
|
||||
self.assertIn("retrying the judge in a minute: the judge returned no verdict; it replied: {}\n", run.stderr)
|
||||
self.assertEqual(run.stderr.strip().splitlines()[-1], 'the judge failed three times running: the judge\'s verdict has no evidence of type string; it replied: {"structured_output": {"interrupt": false}}')
|
||||
|
||||
def test_retries_the_judge_alone_when_its_verdict_quotes_nothing_in_the_transcript(self):
|
||||
checkout = Checkout(self)
|
||||
|
||||
Reference in New Issue
Block a user