Retry the worker and the judge through one counter, in retry.py
This commit is contained in:
@@ -1,12 +1,11 @@
|
|||||||
import dataclasses
|
import dataclasses
|
||||||
import itertools
|
|
||||||
import json
|
import json
|
||||||
import subprocess
|
import subprocess
|
||||||
import typing
|
import typing
|
||||||
|
|
||||||
from loop_claude.claude_code.formats import structured_output
|
from loop_claude.claude_code.formats import structured_output
|
||||||
from loop_claude.outcome import Failure
|
from loop_claude.outcome import Failure
|
||||||
from loop_claude.retry import stop_or_sleep_a_minute
|
from loop_claude.retry import retried
|
||||||
|
|
||||||
JUDGE_RULES = (
|
JUDGE_RULES = (
|
||||||
"Decide whether the human must now be interrupted. interrupt=true when any of these appear:\n"
|
"Decide whether the human must now be interrupted. interrupt=true when any of these appear:\n"
|
||||||
@@ -57,26 +56,20 @@ def judge(root, task, premises, transcript, model):
|
|||||||
"--tools", "",
|
"--tools", "",
|
||||||
]
|
]
|
||||||
prompt = judge_prompt(task, premises, transcript)
|
prompt = judge_prompt(task, premises, transcript)
|
||||||
for failures in itertools.count(1):
|
return retried("judge", lambda: run_judge(root, args, prompt, transcript))
|
||||||
verdict = run_judge(root, args, prompt)
|
|
||||||
if isinstance(verdict, Failure):
|
|
||||||
reason = verdict.reason
|
|
||||||
elif not grounded(verdict, transcript):
|
|
||||||
reason = f"the judge quoted {verdict.evidence!r}, nothing in the transcript" + (f", for the question: {verdict.question}" if verdict.interrupt else "")
|
|
||||||
else:
|
|
||||||
return verdict
|
|
||||||
if stop := stop_or_sleep_a_minute("judge", failures, reason):
|
|
||||||
return stop
|
|
||||||
|
|
||||||
|
|
||||||
def run_judge(root, args, prompt):
|
def run_judge(root, args, prompt, transcript):
|
||||||
try:
|
try:
|
||||||
proc = subprocess.run(["claude", *args], capture_output=True, cwd=root, input=prompt, text=True)
|
proc = subprocess.run(["claude", *args], capture_output=True, cwd=root, input=prompt, text=True)
|
||||||
except OSError as failure:
|
except OSError as failure:
|
||||||
return Failure(f"could not start claude: {failure.strerror}")
|
return Failure(f"could not start claude: {failure.strerror}")
|
||||||
if proc.returncode != 0:
|
if proc.returncode != 0:
|
||||||
return Failure(proc.stderr.strip() or f"the judge exited {proc.returncode}")
|
return Failure(proc.stderr.strip() or f"the judge exited {proc.returncode}")
|
||||||
return verdict_from(structured_output(proc.stdout))
|
verdict = verdict_from(structured_output(proc.stdout))
|
||||||
|
if isinstance(verdict, Verdict) and 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
|
||||||
|
|
||||||
|
|
||||||
def judge_prompt(task, premises, transcript):
|
def judge_prompt(task, premises, transcript):
|
||||||
|
|||||||
@@ -4,14 +4,14 @@ from loop_claude.claude_code.transcript import read, render
|
|||||||
from loop_claude.claude_runs.interrupt import interrupt
|
from loop_claude.claude_runs.interrupt import interrupt
|
||||||
from loop_claude.claude_runs.judge import judge
|
from loop_claude.claude_runs.judge import judge
|
||||||
from loop_claude.claude_runs.worker import run_worker, worker_args
|
from loop_claude.claude_runs.worker import run_worker, worker_args
|
||||||
from loop_claude.outcome import Continue, Failure, Stop
|
from loop_claude.outcome import Failure, Stop
|
||||||
|
from loop_claude.retry import retried
|
||||||
|
|
||||||
|
|
||||||
def run_cycle(root, project, task, premises, models, paths):
|
def run_cycle(root, project, task, premises, models, paths):
|
||||||
session = session_for(project, paths)
|
session = session_for(project, paths)
|
||||||
resuming = bool(transcript_file(session))
|
if stop := retried("worker", lambda: run_worker(root, worker_args(task, session, models.worker, bool(transcript_file(session))))):
|
||||||
if failure := run_worker(root, worker_args(task, session, models.worker, resuming)):
|
return stop
|
||||||
return failure
|
|
||||||
path = transcript_file(session)
|
path = transcript_file(session)
|
||||||
if not path:
|
if not path:
|
||||||
return Stop(1, f"claude left no transcript for session {session.id}")
|
return Stop(1, f"claude left no transcript for session {session.id}")
|
||||||
@@ -22,7 +22,6 @@ def run_cycle(root, project, task, premises, models, paths):
|
|||||||
if failure := archive(project, path, f"Task: {task}\n\nVerdict: finished", paths):
|
if failure := archive(project, path, f"Task: {task}\n\nVerdict: finished", paths):
|
||||||
return Stop(1, failure.reason)
|
return Stop(1, failure.reason)
|
||||||
delete_session(session)
|
delete_session(session)
|
||||||
return Continue()
|
|
||||||
|
|
||||||
|
|
||||||
def judge_through_interrupts(root, project, task, premises, models, session, path, paths):
|
def judge_through_interrupts(root, project, task, premises, models, session, path, paths):
|
||||||
|
|||||||
+4
-13
@@ -37,18 +37,16 @@ import dataclasses
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
from loop_claude.cycle import run_cycle
|
from loop_claude.cycle import run_cycle
|
||||||
from loop_claude.outcome import Continue, Failure, Stop
|
from loop_claude.outcome import Stop
|
||||||
from loop_claude.project.checkout import checkout_root, project_name
|
from loop_claude.project.checkout import checkout_root, project_name
|
||||||
from loop_claude.project.models import project_models
|
from loop_claude.project.models import project_models
|
||||||
from loop_claude.project.premises import read_premises
|
from loop_claude.project.premises import read_premises
|
||||||
from loop_claude.reload import RELOAD_FLAG, decode_state, exec_if_edited, fingerprint
|
from loop_claude.reload import RELOAD_FLAG, decode_state, exec_if_edited, fingerprint
|
||||||
from loop_claude.retry import stop_or_sleep_a_minute
|
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass(frozen=True)
|
@dataclasses.dataclass(frozen=True)
|
||||||
class LoopState:
|
class LoopState:
|
||||||
cycle_number: int
|
cycle_number: int
|
||||||
failures: int
|
|
||||||
root: str
|
root: str
|
||||||
task: str
|
task: str
|
||||||
|
|
||||||
@@ -79,15 +77,8 @@ def run_loop(argv, env, paths, launcher, cwd):
|
|||||||
if isinstance(models, Stop):
|
if isinstance(models, Stop):
|
||||||
return models
|
return models
|
||||||
print(f"\n=== cycle {state.cycle_number} ===", flush=True)
|
print(f"\n=== cycle {state.cycle_number} ===", flush=True)
|
||||||
match run_cycle(state.root, project, state.task, premises, models, paths):
|
if stop := run_cycle(state.root, project, state.task, premises, models, paths):
|
||||||
case Stop() as stop:
|
return stop
|
||||||
return stop
|
|
||||||
case Failure(reason):
|
|
||||||
state = dataclasses.replace(state, failures=state.failures + 1)
|
|
||||||
if stop := stop_or_sleep_a_minute("worker", state.failures, reason):
|
|
||||||
return stop
|
|
||||||
case Continue():
|
|
||||||
state = dataclasses.replace(state, failures=0)
|
|
||||||
|
|
||||||
|
|
||||||
def loop_state(argv, cwd):
|
def loop_state(argv, cwd):
|
||||||
@@ -96,7 +87,7 @@ def loop_state(argv, cwd):
|
|||||||
if task in ("", "-h", "--help"):
|
if task in ("", "-h", "--help"):
|
||||||
return Stop(0 if task else 1, __doc__.strip())
|
return Stop(0 if task else 1, __doc__.strip())
|
||||||
root = checkout_root(cwd)
|
root = checkout_root(cwd)
|
||||||
return root if isinstance(root, Stop) else LoopState(0, 0, root, task)
|
return root if isinstance(root, Stop) else LoopState(0, root, task)
|
||||||
state = decode_state(argv[1], LoopState) if len(argv) == 2 else None
|
state = decode_state(argv[1], LoopState) if len(argv) == 2 else None
|
||||||
if not (state and state.root and state.task):
|
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 Stop(1, f"the reload cannot read the loop's state from {argv[1:]}; rerun loop-claude <task>")
|
||||||
|
|||||||
@@ -1,14 +1,9 @@
|
|||||||
import dataclasses
|
import dataclasses
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass(frozen=True)
|
|
||||||
class Continue:
|
|
||||||
"""The cycle ended; the loop starts the next."""
|
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass(frozen=True)
|
@dataclasses.dataclass(frozen=True)
|
||||||
class Failure:
|
class Failure:
|
||||||
"""A step failed and its caller decides: a worker or judge run is retried until it fails three times running; anything else becomes a Stop or a warning."""
|
"""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."""
|
||||||
reason: str
|
reason: str
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,16 @@
|
|||||||
|
import itertools
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from loop_claude.outcome import Stop
|
from loop_claude.outcome import Failure, Stop
|
||||||
|
|
||||||
|
|
||||||
def stop_or_sleep_a_minute(who, failures, reason):
|
def retried(who, attempt):
|
||||||
if failures == 3:
|
for failures in itertools.count(1):
|
||||||
return Stop(1, f"the {who} failed three times running: {reason}")
|
result = attempt()
|
||||||
print(f"retrying the {who} in a minute: {reason}", file=sys.stderr)
|
if not isinstance(result, Failure):
|
||||||
time.sleep(60)
|
return result
|
||||||
|
if failures == 3:
|
||||||
|
return Stop(1, f"the {who} failed three times running: {result.reason}")
|
||||||
|
print(f"retrying the {who} in a minute: {result.reason}", file=sys.stderr)
|
||||||
|
time.sleep(60)
|
||||||
|
|||||||
Reference in New Issue
Block a user