Answer the architect's first round
This commit is contained in:
@@ -20,14 +20,10 @@ def interrupt(root, session_id, model, path, question, paths, effects: Effects)
|
||||
return Stop(1, entries.reason)
|
||||
before = human_turns(entries)
|
||||
try:
|
||||
effects.run(["claude", "--model", model, "--permission-mode", "bypassPermissions", "--resume", str(session_id)], cwd=root)
|
||||
effects.claude_on_terminal(["--model", model, "--permission-mode", "bypassPermissions", "--resume", str(session_id)], root)
|
||||
except OSError as failure:
|
||||
return Stop(1, f"could not start claude for the interrupt: {failure.strerror}")
|
||||
entries = read(path)
|
||||
if isinstance(entries, Failure):
|
||||
return Stop(1, f"could not tell whether you typed anything: {entries.reason}")
|
||||
return None if typed_since(before, entries) else human_needed.stop(TO, "the interrupt ended with nothing typed")
|
||||
|
||||
|
||||
def typed_since(turns_before, entries) -> bool:
|
||||
return human_turns(entries) > turns_before
|
||||
return None if human_turns(entries) > before else human_needed.stop(TO, "the interrupt ended with nothing typed")
|
||||
|
||||
@@ -63,7 +63,7 @@ def judge(root, task, premises, transcript, model, effects: Effects) -> Verdict
|
||||
|
||||
def run_judge(root, args, prompt, transcript, effects: Effects) -> Verdict | Failure:
|
||||
try:
|
||||
proc = effects.run(["claude", *args], capture_output=True, cwd=root, input=prompt, text=True)
|
||||
proc = effects.claude(args, root, prompt)
|
||||
except OSError as failure:
|
||||
return Failure(f"could not start claude: {failure.strerror}")
|
||||
if proc.returncode != 0:
|
||||
|
||||
@@ -4,37 +4,38 @@ from loop_claude.cycle.verdicts import Finished, Interrupt, Unfinished, Verdict
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Fresh:
|
||||
"""No transcript: the worker opens the session."""
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Resuming:
|
||||
"""The session's transcript exists: the worker continues it."""
|
||||
class Working:
|
||||
"""The worker runs: opening the session, or resuming it while its transcript exists."""
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Judging:
|
||||
transcript: str
|
||||
transcript_path: str
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Interrupting:
|
||||
transcript: str
|
||||
transcript_path: str
|
||||
question: str
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Finishing:
|
||||
transcript: str
|
||||
transcript_path: str
|
||||
|
||||
|
||||
State = Fresh | Resuming | Judging | Interrupting | Finishing
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Ended:
|
||||
"""The cycle is over; the next one starts Working."""
|
||||
|
||||
|
||||
Active = Working | Judging | Interrupting | Finishing
|
||||
State = Active | Ended
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class WorkerRan:
|
||||
transcript: str
|
||||
transcript_path: str
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
@@ -55,23 +56,18 @@ class Archived:
|
||||
Event = WorkerRan | Ruled | Answered | Archived
|
||||
|
||||
|
||||
def opening(transcript: str) -> Fresh | Resuming:
|
||||
return Resuming() if transcript else Fresh()
|
||||
|
||||
|
||||
def transition(state: State, event: Event) -> State:
|
||||
"""The state after event; a cycle ends when it reaches Fresh or Resuming again."""
|
||||
match state, event:
|
||||
case Fresh() | Resuming(), WorkerRan(transcript):
|
||||
return Judging(transcript)
|
||||
case Judging(transcript), Ruled(Interrupt(question=question)):
|
||||
return Interrupting(transcript, question)
|
||||
case Judging(transcript), Ruled(Finished()):
|
||||
return Finishing(transcript)
|
||||
case Working(), WorkerRan(transcript_path):
|
||||
return Judging(transcript_path)
|
||||
case Judging(transcript_path), Ruled(Interrupt(question=question)):
|
||||
return Interrupting(transcript_path, question)
|
||||
case Judging(transcript_path), Ruled(Finished()):
|
||||
return Finishing(transcript_path)
|
||||
case Judging(), Ruled(Unfinished()):
|
||||
return Resuming()
|
||||
case Interrupting(transcript), Answered():
|
||||
return Judging(transcript)
|
||||
return Ended()
|
||||
case Interrupting(transcript_path), Answered():
|
||||
return Judging(transcript_path)
|
||||
case Finishing(), Archived():
|
||||
return Fresh()
|
||||
return Ended()
|
||||
raise ValueError(f"no transition from {state} on {event}")
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import dataclasses
|
||||
import typing
|
||||
|
||||
from loop_claude.claude_code.session import Session, delete_session, transcript_file
|
||||
from loop_claude.claude_code.transcript import read, render
|
||||
@@ -6,7 +7,7 @@ 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.retry import retried
|
||||
from loop_claude.cycle.states import Answered, Archived, Event, Finishing, Fresh, Interrupting, Judging, Resuming, Ruled, State, WorkerRan, opening, transition
|
||||
from loop_claude.cycle.states import Active, Answered, Archived, Ended, Event, Finishing, Interrupting, Judging, Ruled, State, WorkerRan, Working, transition
|
||||
from loop_claude.cycle.worker import run_worker, worker_args
|
||||
from loop_claude.effects import Effects
|
||||
from loop_claude.outcome import Failure, Stop
|
||||
@@ -27,43 +28,59 @@ class Cycle:
|
||||
|
||||
|
||||
def run_cycle(cycle) -> Stop | None:
|
||||
state: State = opening(transcript_file(cycle.session))
|
||||
while True:
|
||||
state: State = Working()
|
||||
while not isinstance(state, Ended):
|
||||
event = step(cycle, state)
|
||||
if isinstance(event, Stop):
|
||||
return event
|
||||
state = transition(state, event)
|
||||
if isinstance(state, Fresh | Resuming):
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
def step(cycle, state) -> Event | Stop:
|
||||
"""Carries out state's effects and reports what happened."""
|
||||
def step(cycle, state: Active) -> Event | Stop:
|
||||
match state:
|
||||
case Fresh() | Resuming():
|
||||
if stop := retried("worker", lambda: run_worker(cycle.root, worker_args(cycle.task, cycle.session, cycle.models.worker, resuming(cycle.session)), cycle.effects), cycle.effects):
|
||||
return stop
|
||||
path = transcript_file(cycle.session)
|
||||
return WorkerRan(path) if path else Stop(1, f"claude left no transcript for session {cycle.session.id}")
|
||||
case Judging(path):
|
||||
entries = read(path)
|
||||
if isinstance(entries, Failure):
|
||||
return Stop(1, entries.reason)
|
||||
verdict = judge(cycle.root, cycle.task, cycle.premises, render(entries), cycle.models.judge, cycle.effects)
|
||||
return verdict if isinstance(verdict, Stop) else Ruled(verdict)
|
||||
case Interrupting(path, question):
|
||||
# The interrupt appends to this transcript, so it is archived at each question and judged anew after.
|
||||
if failure := archive(cycle.project, 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, path, question, cycle.paths, cycle.effects) or Answered()
|
||||
case Finishing(path):
|
||||
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 Archived()
|
||||
raise ValueError(f"no step for {state}")
|
||||
case Working():
|
||||
return work(cycle)
|
||||
case Judging(transcript_path):
|
||||
return rule(cycle, transcript_path)
|
||||
case Interrupting(transcript_path, question):
|
||||
return ask(cycle, transcript_path, question)
|
||||
case Finishing(transcript_path):
|
||||
return close(cycle, transcript_path)
|
||||
case _:
|
||||
typing.assert_never(state)
|
||||
|
||||
|
||||
def resuming(session) -> bool:
|
||||
def work(cycle) -> WorkerRan | Stop:
|
||||
# Asked at each attempt: a failed one may have opened the session.
|
||||
return isinstance(opening(transcript_file(session)), Resuming)
|
||||
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
|
||||
path = transcript_file(cycle.session)
|
||||
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:
|
||||
entries = read(transcript_path)
|
||||
if isinstance(entries, Failure):
|
||||
return Stop(1, entries.reason)
|
||||
verdict = judge(cycle.root, cycle.task, cycle.premises, render(entries), cycle.models.judge, cycle.effects)
|
||||
return verdict if isinstance(verdict, Stop) else Ruled(verdict)
|
||||
|
||||
|
||||
def ask(cycle, transcript_path, question) -> Answered | Stop:
|
||||
# The interrupt appends to this transcript, so it is archived at each question and judged anew after.
|
||||
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:
|
||||
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:
|
||||
"""A run resumes the session only while its transcript exists."""
|
||||
return bool(transcript_file(session))
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import subprocess
|
||||
|
||||
from loop_claude.claude_code.formats import assistant_parts, ends_stream, parse
|
||||
from loop_claude.claude_code.transcript import part_line
|
||||
from loop_claude.effects import Effects
|
||||
@@ -8,18 +6,17 @@ from loop_claude.outcome import Failure
|
||||
|
||||
def run_worker(root, args, effects: Effects) -> Failure | None:
|
||||
try:
|
||||
proc = effects.popen(["claude", *args], cwd=root, encoding="utf-8", errors="replace", stdin=subprocess.DEVNULL, stdout=subprocess.PIPE)
|
||||
proc = effects.stream_claude(args, root)
|
||||
except OSError as failure:
|
||||
return Failure(f"could not start claude: {failure.strerror}")
|
||||
with proc:
|
||||
done = False
|
||||
for raw in proc.stdout or ():
|
||||
event = parse(raw) or {}
|
||||
done = done or ends_stream(event)
|
||||
for part in assistant_parts(event):
|
||||
if line := part_line(part, 160):
|
||||
print(line, flush=True)
|
||||
code = proc.wait()
|
||||
done = False
|
||||
for raw in proc.lines():
|
||||
event = parse(raw) or {}
|
||||
done = done or ends_stream(event)
|
||||
for part in assistant_parts(event):
|
||||
if line := part_line(part, 160):
|
||||
print(line, flush=True)
|
||||
code = proc.wait()
|
||||
if code < 0:
|
||||
return Failure(f"claude killed by signal {-code}")
|
||||
if code:
|
||||
|
||||
Reference in New Issue
Block a user