diff --git a/bin/loop_claude/cycle/run.py b/bin/loop_claude/cycle/run.py index cf65353b..fec99f47 100644 --- a/bin/loop_claude/cycle/run.py +++ b/bin/loop_claude/cycle/run.py @@ -76,9 +76,13 @@ def ruling_step_up(cycle: Cycle, verdict: Verdict) -> StepUp | None: def stopped(cycle: Cycle, stop: Stop, why: str) -> Stop: - path = transcript_file(cycle.session) - failure = archive(cycle.project_archive, path, cycle.task, outcomes.stopped(cycle.route.worker_line(), why), cycle.effects.now()) if path else None - # A failed archive exits 1 whatever the stop; when the stop was this archive failing, its reason is said once. + return archived_stop(cycle.project_archive, cycle.session, cycle.task, cycle.route, stop, why, cycle.effects) + + +def archived_stop(project_archive: pathlib.Path, session: Session, task: str, route: Route, stop: Stop, why: str, effects: Effects) -> Stop: + """stop, once archived with the route the worker holds. A failed archive exits 1 whatever the stop; when the stop was this archive failing, its reason is said once.""" + path = transcript_file(session) + failure = archive(project_archive, path, task, outcomes.stopped(route.worker_line(), why), effects.now()) if path else None return Stop(1, f"{failure.reason}\n{stop.reason}".rstrip()) if failure and failure.reason != stop.reason else stop diff --git a/bin/loop_claude/loop/main.py b/bin/loop_claude/loop/main.py index fd073bbe..0ca62357 100644 --- a/bin/loop_claude/loop/main.py +++ b/bin/loop_claude/loop/main.py @@ -102,13 +102,13 @@ Exit 1, saying why, when: more than 2 minutes past. The judge's reply gives its reset time only as text, so the judge is always retried every 10 minutes. stderr says when the next try is due. -Exit 2, when the human is needed and cannot answer: +Exit 2, when the human is needed to go on and cannot be asked, or the chunk has gone on too long: - stdin or stdout is not a terminal, when a model must be picked or an interrupt answered - the input ends while a model is picked - an interrupt's answer is empty, or the input ends before one is typed - the claude opened for /talk, or for a release ready to cut, ends with nothing typed -- the judge rules a chunk unfinished 6 cycles running; a finished chunk restarts the run, and a rerun - gives the chunk 6 more +- the judge rules a chunk unfinished 6 cycles running; finish the chunk yourself, or rerun to give it + 6 more. A finished chunk restarts the run. Rerunning resumes any open session. """ @@ -117,12 +117,12 @@ import pathlib import sys import typing -from loop_claude.routing import after_unfinished, ESCALATION_CAP, first_route, kept, routed_item, UNFINISHED_PER_STEP +from loop_claude.routing import after_unfinished, first_route, kept, routed_item from transcript_archive.write import archive from transcript_archive.location import archive_root, project_archive from loop_claude.loop.routing_check import check_routing from loop_claude.project.checkout import checkout_root -from loop_claude.cycle.run import Concluded, Cycle, run_cycle +from loop_claude.cycle.run import archived_stop, Concluded, Cycle, run_cycle from loop_claude.loop.reload import decode_handed, exec_if_edited, fingerprint, LoopRun, RELOAD_FLAG from chunk_pick import default_plan, DefaultPlan from git_repo import default_ref @@ -145,8 +145,8 @@ from loop_claude.stop import needs_human, Stop # Seconds routing waits for origin to name its default branch, asked only where the checkout records none; past them, the chunk runs on the project's rung. ORIGIN_TIMEOUT = 60 -# How many unfinished verdicts running end the loop to ask the human, so a worker and a judge that disagree about a chunk cannot cycle unattended without end: the worker's two escalation steps, which every second verdict takes, and one step's worth more at the top. No archived chunk has had more than one. -UNFINISHED_LIMIT = UNFINISHED_PER_STEP * (ESCALATION_CAP + 1) +# How many unfinished verdicts running end the loop, so a worker and a judge that disagree about a chunk cannot cycle unattended without end. A policy number, not derived from the routing steps: well above the one unfinished verdict any archived chunk has had, and low enough that a stuck pair spends six worker and judge runs, not more. "loop-claude --help" states it. +UNFINISHED_LIMIT = 6 def main(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, cwd: pathlib.Path, effects: Effects) -> int: @@ -189,11 +189,10 @@ def run_loop(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launc concluded = run_cycle(Cycle(effects=effects, facts=facts, judge_model=models.judge, paths=paths, premises=premises, project=project, project_archive=archive_folder, records=records, root=loop_run.root, route=route, session=session, task=loop_run.task, unfinished_evidence=loop_run.unfinished_evidence)) if isinstance(concluded, Stop): return concluded - loop_run = after_verdict(loop_run, project, concluded) - if stop := archive_step(archive_folder, session, loop_run.task, concluded.route, loop_run.route, effects): - return stop - if stop := past_limit(archive_folder, session, loop_run, effects): - return stop + closed = closed_cycle(loop_run, project, concluded, archive_folder, session, effects) + if isinstance(closed, Stop): + return closed + loop_run = closed if stop := check_routing(archive_root(paths.home, env), project or Rung(models.worker), paths, effects): return stop @@ -209,12 +208,31 @@ def routed(route: Route | None, project: Rung | None, model: str, archived: typi return route if route and kept(route, project) else first_route(project, item()) +def closed_cycle(loop_run: LoopRun, project: Rung | None, concluded: Concluded, archive_folder: pathlib.Path, session: Session, effects: Effects) -> LoopRun | Stop: + """The loop run after a cycle concluded, with the step-up archived; the stop that asks the human, archived, once the chunk has had UNFINISHED_LIMIT unfinished verdicts running.""" + after = after_verdict(loop_run, project, concluded) + if stop := archive_step(archive_folder, session, after.task, concluded.route, after.route, effects): + return stop + if limit := past_limit(after): + # An unfinished verdict always leaves a route. It is archived as the next cycle would run on it, so a rerun resumes there. + return archived_stop(archive_folder, session, after.task, after.route or concluded.route, limit, limit.reason, effects) + return after + + +def past_limit(loop_run: LoopRun) -> Stop | None: + """The stop that asks the human once the loop run holds UNFINISHED_LIMIT unfinished verdicts running.""" + if loop_run.unfinished_run < UNFINISHED_LIMIT: + return None + return needs_human("decide whether the chunk is finished", f"the judge ruled it unfinished {loop_run.unfinished_run} cycles running and loop-claude does not go on unasked; finish the chunk yourself, or rerun loop-claude to give it {UNFINISHED_LIMIT} more cycles") + + def after_verdict(loop_run: LoopRun, project: Rung | None, concluded: Concluded) -> LoopRun: """The loop run the next cycle starts from. A finished chunk drops its route and its run of unfinished verdicts. An unfinished one takes the route stepped_route makes, the evidence the judge gave for the verdict, and counts the verdict.""" verdict = concluded.verdict if isinstance(verdict, Finished): return dataclasses.replace(loop_run, route=None, unfinished_evidence="", unfinished_run=0) - return dataclasses.replace(loop_run, route=stepped_route(project, concluded.route, verdict), unfinished_evidence=verdict.evidence, unfinished_run=loop_run.unfinished_run + 1) + route = stepped_route(project, concluded.route, verdict) + return dataclasses.replace(loop_run, route=route, unfinished_evidence=verdict.evidence, unfinished_run=loop_run.unfinished_run + 1) def stepped_route(project: Rung | None, ended_on: Route, verdict: Unfinished) -> Route: @@ -222,16 +240,6 @@ def stepped_route(project: Rung | None, ended_on: Route, verdict: Unfinished) -> return after_unfinished(project, ended_on, verdict) if project else ended_on -def past_limit(archive_folder: pathlib.Path, session: Session, loop_run: LoopRun, effects: Effects) -> Stop | None: - """The stop that asks the human once the chunk has had UNFINISHED_LIMIT unfinished verdicts running, archived with the route the next cycle would run on, so a rerun resumes on it.""" - if loop_run.unfinished_run < UNFINISHED_LIMIT or loop_run.route is None: - return None - stop = needs_human("decide whether the chunk is finished", f"the judge ruled it unfinished {loop_run.unfinished_run} cycles running; rerun loop-claude to give it {UNFINISHED_LIMIT} more") - path = transcript_file(session) - failure = archive(archive_folder, path, loop_run.task, outcomes.stopped(loop_run.route.worker_line(), stop.reason), effects.now()) if path else None - return Stop(1, f"{failure.reason}\n{stop.reason}") if failure else stop - - def archive_step(archive_folder: pathlib.Path, session: Session, task: str, before: Route, after: Route | None, effects: Effects) -> Stop | None: """Archives the route after an unfinished verdict as a step-up, where its worker line differs from the one before it: a higher rung, or a higher E + I taken in. The verdict itself names the route before, which its cycle ran on.""" if after is None or after.worker_line() == before.worker_line(): diff --git a/bin/loop_claude/test_black_box.py b/bin/loop_claude/test_black_box.py index d15ad153..ec57b7c4 100644 --- a/bin/loop_claude/test_black_box.py +++ b/bin/loop_claude/test_black_box.py @@ -653,10 +653,10 @@ class Cycles(unittest.TestCase): six = [step for _ in range(6) for step in (worker(), judge())] run = checkout.run(six + [worker()]) self.assertEqual((run.code, run.roles()), (2, ["worker", "judge"] * 6)) - self.assertEqual(run.stderr.strip().splitlines()[-1], "the human is needed to decide whether the chunk is finished, but the judge ruled it unfinished 6 cycles running; rerun loop-claude to give it 6 more") + self.assertEqual(run.stderr.strip().splitlines()[-1], "the human is needed to decide whether the chunk is finished, but the judge ruled it unfinished 6 cycles running and loop-claude does not go on unasked; finish the chunk yourself, or rerun loop-claude to give it 6 more cycles") stops = [outcome for outcome in checkout.archived().values() if "Stopped: " in outcome] self.assertEqual(len(stops), 1) - self.assertIn(f" Stopped: the human is needed to decide whether the chunk is finished, but the judge ruled it unfinished 6 cycles running; rerun loop-claude to give it 6 more\n\nWorker: {OFF_LADDER}\n", stops[0]) + self.assertIn(f" Stopped: the human is needed to decide whether the chunk is finished, but the judge ruled it unfinished 6 cycles running and loop-claude does not go on unasked; finish the chunk yourself, or rerun loop-claude to give it 6 more cycles\n\nWorker: {OFF_LADDER}\n", stops[0]) run = checkout.run(six[:-2] + [worker()]) self.assertEqual((run.code, run.roles()), (130, ["worker", "judge"] * 6)) diff --git a/bin/loop_claude/test_loop_claude.py b/bin/loop_claude/test_loop_claude.py index 5c916220..d9aad476 100644 --- a/bin/loop_claude/test_loop_claude.py +++ b/bin/loop_claude/test_loop_claude.py @@ -1013,6 +1013,9 @@ class Steps(unittest.TestCase): self.assertEqual(main.after_verdict(dataclasses.replace(run_with, unfinished_run=3), self.OPUS, run.Concluded(Finished("x"), run_with.route)).unfinished_run, 0) self.assertEqual(main.after_verdict(run_with, self.OPUS, run.Concluded(Unfinished("new", 4), run_with.route)), dataclasses.replace(run_with, route=Route(Rung("claude-opus-5-5", "high"), unfinished=1), unfinished_evidence="new", unfinished_run=1)) + def test_help_states_the_limit_of_unfinished_verdicts_running(self): + self.assertIn(f"unfinished {main.UNFINISHED_LIMIT} cycles running", " ".join(main.__doc__.split())) + def test_steps_from_the_route_its_cycle_ended_on_which_a_ruling_may_have_raised(self): run_with = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="", route=Route(self.OPUS, 3, 3, 4)) raised = Route(Rung("claude-opus-5-5", "high"), 3, 3, 4, escalated_at=5) @@ -1531,7 +1534,7 @@ class ExitContract(unittest.TestCase): ("cycle/interrupt.py", "talk", "1", "f'could not start claude for the interrupt:", "claude cannot start for an interrupt"), ("cycle/interrupt.py", "talk", "1", "f'could not tell whether you typed anything:", read), ("cycle/retry.py", "retried", "1", "f'the {who} failed three times running:", "the worker or the judge fails three times running"), - ("cycle/run.py", "stopped", "1", "f'{failure.reason}", read), + ("cycle/run.py", "archived_stop", "1", "f'{failure.reason}", read), ("cycle/run.py", "work", "1", "f'claude left no transcript for session", "claude leaves no transcript"), ("cycle/run.py", "keep_answer", "1", "failure.reason", read), ("cycle/run.py", "rule", "1", "entries.reason", read), @@ -1539,7 +1542,6 @@ class ExitContract(unittest.TestCase): ("cycle/run.py", "ask", "1", "failure.reason", read), ("cycle/run.py", "close", "1", "failure.reason", read), ("cycle/run.py", "close", "1", "failure.reason", "a finished chunk's session cannot be deleted"), - ("loop/main.py", "past_limit", "1", "f'{failure.reason}\\n{stop.reason}'", read), ("loop/main.py", "archive_step", "1", "failure.reason", read), ("loop/main.py", "loop_run_from", "0 if task else 1", "__doc__.strip()", "no task is given"), ("loop/main.py", "loop_run_from", "1", 'f"the edited loop-claude cannot read the state', "a reload cannot read the state the running script handed it"),