Annotate the return type of every function in loop_claude
This commit is contained in:
@@ -5,7 +5,7 @@ from loop_claude.file_writes import make_private, replace_file
|
||||
from loop_claude.outcome import Failure
|
||||
|
||||
|
||||
def archive(project, path, note, paths):
|
||||
def archive(project, path, note, paths) -> Failure | None:
|
||||
folder = paths.archive / project
|
||||
try:
|
||||
with open(path, "rb") as handle:
|
||||
|
||||
@@ -10,7 +10,7 @@ from loop_claude.project.checkout import checkouts
|
||||
TO = "answer the question above"
|
||||
|
||||
|
||||
def interrupt(root, session_id, model, path, question, paths):
|
||||
def interrupt(root, session_id, model, path, question, paths) -> Stop | None:
|
||||
print(f"\n{question}", file=sys.stderr)
|
||||
if stop := human_needed.off_terminal(TO):
|
||||
return stop
|
||||
|
||||
@@ -5,7 +5,7 @@ import typing
|
||||
|
||||
from loop_claude.claude_code.formats import structured_output
|
||||
from loop_claude.cycle.retry import retried
|
||||
from loop_claude.outcome import Failure
|
||||
from loop_claude.outcome import Failure, Stop
|
||||
|
||||
JUDGE_RULES = (
|
||||
"Decide whether the human must now be interrupted. interrupt=true when any of these appear:\n"
|
||||
@@ -44,7 +44,7 @@ JUDGE_SCHEMA = {
|
||||
}
|
||||
|
||||
|
||||
def judge(root, task, premises, transcript, model):
|
||||
def judge(root, task, premises, transcript, model) -> Verdict | Stop:
|
||||
# No tools, so the verdict rests on the transcript grounded() checks it against; no persistence, so its runs stay out of the session picker.
|
||||
args = [
|
||||
"-p",
|
||||
@@ -59,7 +59,7 @@ def judge(root, task, premises, transcript, model):
|
||||
return retried("judge", lambda: run_judge(root, args, prompt, transcript))
|
||||
|
||||
|
||||
def run_judge(root, args, prompt, transcript):
|
||||
def run_judge(root, args, prompt, transcript) -> Verdict | Failure:
|
||||
try:
|
||||
proc = subprocess.run(["claude", *args], capture_output=True, cwd=root, input=prompt, text=True)
|
||||
except OSError as failure:
|
||||
@@ -72,7 +72,7 @@ def run_judge(root, args, prompt, transcript):
|
||||
return verdict
|
||||
|
||||
|
||||
def judge_prompt(task, premises, transcript):
|
||||
def judge_prompt(task, premises, transcript) -> str:
|
||||
return (
|
||||
f"From README.md:\n{premises}\n\n"
|
||||
f"The task an agent was given:\n{task}\n\n"
|
||||
@@ -82,7 +82,7 @@ def judge_prompt(task, premises, transcript):
|
||||
)
|
||||
|
||||
|
||||
def verdict_from(output):
|
||||
def verdict_from(output) -> Verdict | Failure:
|
||||
if not isinstance(output, dict):
|
||||
return Failure("the judge returned no verdict")
|
||||
for name, kind in VERDICT_TYPES.items():
|
||||
@@ -93,5 +93,5 @@ def verdict_from(output):
|
||||
return Verdict(**{name: output[name] for name in VERDICT_TYPES})
|
||||
|
||||
|
||||
def grounded(verdict, transcript):
|
||||
def grounded(verdict, transcript) -> bool:
|
||||
return verdict.evidence.strip() != "" and verdict.evidence in transcript
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import itertools
|
||||
import sys
|
||||
import time
|
||||
import typing
|
||||
|
||||
from loop_claude.outcome import Failure, Stop
|
||||
|
||||
T = typing.TypeVar("T")
|
||||
|
||||
def retried(who, attempt):
|
||||
|
||||
def retried(who, attempt: typing.Callable[[], T | Failure]) -> T | Stop:
|
||||
for failures in itertools.count(1):
|
||||
result = attempt()
|
||||
if not isinstance(result, Failure):
|
||||
|
||||
@@ -4,7 +4,7 @@ from loop_claude.claude_code.session import Session, delete_session, transcript_
|
||||
from loop_claude.claude_code.transcript import read, render
|
||||
from loop_claude.cycle.archive import archive
|
||||
from loop_claude.cycle.interrupt import interrupt
|
||||
from loop_claude.cycle.judge import judge
|
||||
from loop_claude.cycle.judge import Verdict, judge
|
||||
from loop_claude.cycle.retry import retried
|
||||
from loop_claude.cycle.worker import run_worker, worker_args
|
||||
from loop_claude.outcome import Failure, Stop
|
||||
@@ -23,7 +23,7 @@ class Cycle:
|
||||
task: str
|
||||
|
||||
|
||||
def run_cycle(cycle):
|
||||
def run_cycle(cycle) -> Stop | None:
|
||||
if stop := retried("worker", lambda: run_worker(cycle.root, worker_args(cycle.task, cycle.session, cycle.models.worker, bool(transcript_file(cycle.session))))):
|
||||
return stop
|
||||
path = transcript_file(cycle.session)
|
||||
@@ -36,9 +36,10 @@ def run_cycle(cycle):
|
||||
if failure := archive(cycle.project, path, f"Task: {cycle.task}\n\nVerdict: finished", cycle.paths):
|
||||
return Stop(1, failure.reason)
|
||||
delete_session(cycle.session)
|
||||
return None
|
||||
|
||||
|
||||
def judge_through_interrupts(cycle, path):
|
||||
def judge_through_interrupts(cycle, path) -> Verdict | Stop:
|
||||
# The interrupt appends to this transcript, so each pass judges and archives it anew.
|
||||
while True:
|
||||
entries = read(path)
|
||||
|
||||
@@ -5,7 +5,7 @@ from loop_claude.claude_code.transcript import part_line
|
||||
from loop_claude.outcome import Failure
|
||||
|
||||
|
||||
def run_worker(root, args):
|
||||
def run_worker(root, args) -> Failure | None:
|
||||
try:
|
||||
proc = subprocess.Popen(["claude", *args], cwd=root, encoding="utf-8", errors="replace", stdin=subprocess.DEVNULL, stdout=subprocess.PIPE)
|
||||
except OSError as failure:
|
||||
@@ -26,7 +26,7 @@ def run_worker(root, args):
|
||||
return None if done else Failure("claude exited 0 without a result event")
|
||||
|
||||
|
||||
def worker_args(task, session, model, resuming):
|
||||
def worker_args(task, session, model, resuming) -> list[str]:
|
||||
if resuming:
|
||||
start = ["-p", "Continue the task; the previous run stopped before it was finished.", "--resume", str(session.id)]
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user