Name each entry shape written_by_human checks, and annotate every parameter in loop_claude

This commit is contained in:
2026-10-01 23:47:03 +02:00
parent 70ad9a9caa
commit 39acf6dff0
24 changed files with 113 additions and 84 deletions
+2 -1
View File
@@ -3,9 +3,10 @@ import sys
from loop_claude.file_writes import make_private, replace_file
from loop_claude.outcome import Failure
from loop_claude.paths import Paths
def archive(project, path, note, paths) -> Failure | None:
def archive(project: str, path: str, note: str, paths: Paths) -> Failure | None:
folder = paths.archive / project
try:
with open(path, "rb") as handle:
+3 -1
View File
@@ -1,16 +1,18 @@
import sys
import uuid
from loop_claude import human_needed
from loop_claude.claude_code.transcript import human_turns, read
from loop_claude.claude_code.trust import trust
from loop_claude.effects import Effects
from loop_claude.outcome import Failure, Stop
from loop_claude.paths import Paths
from loop_claude.project.checkout import checkouts
TO = "answer the question above"
def interrupt(root, session_id, model, path, question, paths, effects: Effects) -> Stop | None:
def interrupt(root: str, session_id: uuid.UUID, model: str, path: str, question: str, paths: Paths, effects: Effects) -> Stop | None:
print(f"\n{question}", file=sys.stderr)
if stop := human_needed.off_terminal(TO, effects.on_terminal()):
return stop
+6 -6
View File
@@ -45,7 +45,7 @@ JUDGE_SCHEMA = {
}
def judge(root, task, premises, transcript, model, effects: Effects) -> Verdict | Stop:
def judge(root: str, task: str, premises: str, transcript: str, model: str, effects: Effects) -> 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",
@@ -60,7 +60,7 @@ def judge(root, task, premises, transcript, model, effects: Effects) -> Verdict
return retried("judge", lambda: run_judge(root, args, prompt, transcript, effects), effects)
def run_judge(root, args, prompt, transcript, effects: Effects) -> Verdict | Failure:
def run_judge(root: str, args: list[str], prompt: str, transcript: str, effects: Effects) -> Verdict | Failure:
try:
proc = effects.claude(args, root, prompt)
except OSError as failure:
@@ -75,7 +75,7 @@ def run_judge(root, args, prompt, transcript, effects: Effects) -> Verdict | Fai
return verdict
def judge_prompt(task, premises, transcript) -> str:
def judge_prompt(task: str, premises: str, transcript: str) -> str:
return (
f"From README.md:\n{premises}\n\n"
f"The task an agent was given:\n{task}\n\n"
@@ -85,7 +85,7 @@ def judge_prompt(task, premises, transcript) -> str:
)
def verdict_from(output) -> Verdict | Failure:
def verdict_from(output: object) -> Verdict | Failure:
if not isinstance(output, dict):
return Failure("the judge returned no verdict")
for name, kind in REPLY_TYPES.items():
@@ -99,11 +99,11 @@ def verdict_from(output) -> Verdict | Failure:
return Finished(output["evidence"]) if output["finished"] else Unfinished(output["evidence"])
def grounded(evidence, transcript) -> bool:
def grounded(evidence: str, transcript: str) -> bool:
quoted = plain(evidence)
return quoted != "" and quoted in plain(transcript)
def plain(text) -> str:
def plain(text: str) -> str:
# The judge drops markdown emphasis and reflows whitespace when it quotes.
return " ".join(text.replace("*", "").replace("_", "").split())
+1 -1
View File
@@ -7,7 +7,7 @@ from loop_claude.outcome import Failure, Stop
T = typing.TypeVar("T")
def retried(who, attempt: typing.Callable[[], T | Failure], effects: Effects) -> T | Stop:
def retried(who: str, attempt: typing.Callable[[], T | Failure], effects: Effects) -> T | Stop:
failures = 0
while True:
failures += 1
+7 -7
View File
@@ -27,7 +27,7 @@ class Cycle:
task: str
def run_cycle(cycle) -> Stop | None:
def run_cycle(cycle: Cycle) -> Stop | None:
state: State = Working()
while not isinstance(state, Ended):
event = step(cycle, state)
@@ -37,7 +37,7 @@ def run_cycle(cycle) -> Stop | None:
return None
def step(cycle, state: Active) -> Event | Stop:
def step(cycle: Cycle, state: Active) -> Event | Stop:
match state:
case Working():
return work(cycle)
@@ -51,7 +51,7 @@ def step(cycle, state: Active) -> Event | Stop:
typing.assert_never(state)
def work(cycle) -> WorkerRan | Stop:
def work(cycle: Cycle) -> WorkerRan | Stop:
# session_open runs inside the lambda: a failed attempt may have opened the session.
if stop := retried("worker", lambda: run_worker(cycle.root, worker_args(cycle.task, cycle.session, cycle.models.worker, session_open(cycle.session)), cycle.effects), cycle.effects):
return stop
@@ -59,7 +59,7 @@ def work(cycle) -> WorkerRan | Stop:
return WorkerRan(path) if path else Stop(1, f"claude left no transcript for session {cycle.session.id}")
def rule(cycle, transcript_path) -> Ruled | Stop:
def rule(cycle: Cycle, transcript_path: str) -> Ruled | Stop:
entries = read(transcript_path)
if isinstance(entries, Failure):
return Stop(1, entries.reason)
@@ -67,19 +67,19 @@ def rule(cycle, transcript_path) -> Ruled | Stop:
return verdict if isinstance(verdict, Stop) else Ruled(verdict)
def ask(cycle, transcript_path, question) -> Answered | Stop:
def ask(cycle: Cycle, transcript_path: str, question: str) -> Answered | Stop:
# The interrupt appends to this transcript, so it is archived at each question.
if failure := archive(cycle.project, transcript_path, f"Task: {cycle.task}\n\nVerdict: interrupt\n\n{question}", cycle.paths):
return Stop(1, failure.reason)
return interrupt(cycle.root, cycle.session.id, cycle.models.worker, transcript_path, question, cycle.paths, cycle.effects) or Answered()
def close(cycle, transcript_path) -> Archived | Stop:
def close(cycle: Cycle, transcript_path: str) -> Archived | Stop:
if failure := archive(cycle.project, transcript_path, f"Task: {cycle.task}\n\nVerdict: finished", cycle.paths):
return Stop(1, failure.reason)
delete_session(cycle.session)
return Archived()
def session_open(session) -> bool:
def session_open(session: Session) -> bool:
return bool(transcript_file(session))
+3 -2
View File
@@ -1,10 +1,11 @@
from loop_claude.claude_code.formats import assistant_parts, ends_stream, parse
from loop_claude.claude_code.session import Session
from loop_claude.claude_code.transcript import part_line
from loop_claude.effects import Effects
from loop_claude.outcome import Failure
def run_worker(root, args, effects: Effects) -> Failure | None:
def run_worker(root: str, args: list[str], effects: Effects) -> Failure | None:
try:
proc = effects.stream_claude(args, root)
except OSError as failure:
@@ -24,7 +25,7 @@ def run_worker(root, args, effects: Effects) -> Failure | None:
return None if done else Failure("claude exited 0 without a result event")
def worker_args(task, session, model, resuming) -> list[str]:
def worker_args(task: str, session: Session, model: str, resuming: bool) -> list[str]:
if resuming:
start = ["-p", "Continue the task; the previous run stopped before it was finished.", "--resume", str(session.id)]
else: