This commit is contained in:
+14
-4
@@ -7,9 +7,10 @@ from transcript_archive.write import archive
|
||||
from agent_cycle.worker import AUTO_COMPACT_WINDOW, COMPACT_WINDOW, run_worker, worker_args
|
||||
from claude_code.output_limits import BASH_CEILING, BASH_CEILING_CHARACTERS
|
||||
from agent_cycle.judge import Brief, judge, Rejected
|
||||
from claude_code.session import delete_session, Session, session_open, transcript_file
|
||||
from claude_code.session import delete_session, discard_empty_transcript, Session, session_open, transcript_file
|
||||
from loop_effects.effects import Effects, say
|
||||
from claude_code.entries import Entry
|
||||
from claude_code.reply import Limited
|
||||
from failure import Failure
|
||||
from agent_cycle.verdicts import Finished, Interrupt, Judged, QuotedAnswer, TakenIn, Unfinished, Verdict
|
||||
from agent_cycle.interrupt import interrupt, Talked, WorkerSession
|
||||
@@ -121,8 +122,7 @@ def step(cycle: Cycle, state: Active) -> Event | Stop:
|
||||
def work(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None, stepped: bool) -> WorkerRan | Stop:
|
||||
if isinstance(answered, QuotedAnswer):
|
||||
say(f"\n=== {cycle.prompts.quote_sources} answered: {answered.question} ===\n{answered.answer}", cycle.effects, stdout=True)
|
||||
# worker_start runs inside the lambda: a failed attempt may have opened the session.
|
||||
if stop := retried("worker", lambda: run_worker(cycle.root, worker_start(cycle, answered, stepped), worker_environment(cycle), cycle.project_archive, cycle.project.rung if cycle.project else cycle.route.rung, cycle.effects), cycle.effects):
|
||||
if stop := retried("worker", lambda: started_worker(cycle, answered, stepped), cycle.effects):
|
||||
return stop
|
||||
path = transcript_file(cycle.session)
|
||||
return WorkerRan(path) if path is not None else Stop(1, f"claude left no transcript for session {cycle.session.id}")
|
||||
@@ -137,13 +137,23 @@ def worker_session(cycle: Cycle, transcript_path: str, stepped_from: Rung | None
|
||||
return WorkerSession(cycle.root, cycle.session.id, cycle.route.rung, stepped_from or cycle.route.rung, transcript_path, worker_environment(cycle))
|
||||
|
||||
|
||||
def worker_start(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None, stepped: bool) -> list[str]:
|
||||
def started_worker(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None, stepped: bool) -> Failure | Limited | None:
|
||||
"""One run of the worker. worker_start runs on each attempt: a failed one may have opened the session, or left it empty."""
|
||||
args = worker_start(cycle, answered, stepped)
|
||||
if isinstance(args, Failure):
|
||||
return args
|
||||
return run_worker(cycle.root, args, worker_environment(cycle), cycle.project_archive, cycle.project.rung if cycle.project else cycle.route.rung, cycle.effects)
|
||||
|
||||
|
||||
def worker_start(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None, stepped: bool) -> list[str] | Failure:
|
||||
if isinstance(answered, QuotedAnswer):
|
||||
return worker_args(cycle.session, cycle.route.rung, cycle.prompts.answered(answered.question, answered.answer, cycle.route, cycle.project, stepped), resume=True)
|
||||
if isinstance(answered, TypedAnswer):
|
||||
return worker_args(cycle.session, cycle.route.rung, cycle.prompts.typed(answered.question, answered.answer, cycle.route, cycle.project, stepped), resume=True)
|
||||
if session_open(cycle.session):
|
||||
return worker_args(cycle.session, cycle.route.rung, cycle.prompts.resumed(cycle.unfinished_evidence, cycle.route, cycle.project), resume=True)
|
||||
if failure := discard_empty_transcript(cycle.session):
|
||||
return failure
|
||||
return worker_args(cycle.session, cycle.route.rung, cycle.prompts.fresh(cycle.task, cycle.premises, cycle.route, cycle.project, cycle.facts), resume=False)
|
||||
|
||||
|
||||
|
||||
@@ -172,6 +172,16 @@ Worker: {OFF_LADDER}
|
||||
self.assertEqual(workers[1][workers[1].index("-p") + 1], "Resumed: working")
|
||||
self.assertEqual(self.archived_outcomes(), [f"Task: Do it.\n\n2026-09-21T14:13:20Z Verdict: unfinished\n\nEvidence:\n> working\n\nConfig: {CONFIG}\nJudge: judge-model\nWorker: {OFF_LADDER}\n"] * 2)
|
||||
|
||||
def test_a_session_whose_transcript_is_empty_starts_fresh_with_its_session_id_where_claude_cannot_resume_it(self):
|
||||
path = self.paths.claude_code_transcripts / "-fake" / f"{self.session.id}.jsonl"
|
||||
path.parent.mkdir(parents=True)
|
||||
path.touch()
|
||||
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("finished")])
|
||||
self.assertEqual(run.run_cycle(self.cycle(fake)).verdict, Finished("working"))
|
||||
worker, = fake.made("stream_claude")
|
||||
self.assertIn("--session-id", worker)
|
||||
self.assertNotIn("--resume", worker)
|
||||
|
||||
def test_a_cycle_stopped_by_a_failure_or_ctrl_c_archives_what_its_session_holds(self):
|
||||
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[ran(code=1)] * 3)
|
||||
self.assertEqual(run.run_cycle(self.cycle(fake)).code, 1)
|
||||
|
||||
Reference in New Issue
Block a user