diff --git a/CHANGELOG.md b/CHANGELOG.md index 33bc8696..3250c6c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,4 +16,5 @@ - `loop-claude` counts an interrupt reply starting with `<` as typed. - `loop-claude`'s judge sees a reply the human types while claude works, and an interrupt counts one as typed. - `loop-claude`'s archive holds the raw `.jsonl` transcript, one copy per distinct transcript, named by its SHA-256, with the verdict beside it in `.verdict.md`; a transcript it cannot archive exits 1. +- `loop-claude` exits 1 when it cannot delete a finished chunk's session, naming what to delete before rerunning; the next chunk would otherwise resume it. - `loop-claude` keeps the mode of `~/.claude.json` and of its models file when it rewrites them, and creates the models file 0600. diff --git a/bin/loop_claude/claude_code/session.py b/bin/loop_claude/claude_code/session.py index 2df3e1bf..09da7e25 100644 --- a/bin/loop_claude/claude_code/session.py +++ b/bin/loop_claude/claude_code/session.py @@ -3,9 +3,9 @@ import glob import os import pathlib import shutil -import sys import uuid +from loop_claude.outcome import Failure from loop_claude.paths import Paths NAMESPACE = uuid.UUID("64fb586b-d926-4bb3-a44c-c903e86d3eec") @@ -26,18 +26,25 @@ def session_for(project: str, paths: Paths) -> Session: # /-home-me-code-log/.jsonl -def transcript_file(session: Session) -> str: +def transcript_file(session: Session) -> str | None: hit = glob.glob(os.path.join(glob.escape(str(session.claude_code_transcripts)), "*", f"{session.id}.jsonl")) - return hit[0] if hit else "" + return hit[0] if hit else None -def delete_session(session: Session) -> None: +def session_open(session: Session) -> bool: + return transcript_file(session) is not None + + +def delete_session(session: Session) -> Failure | None: path = transcript_file(session) - if not path: - return + if path is None: + return None + stem = path.removesuffix(".jsonl") # .jsonl, /subagents/, and from older versions .superseded-1789290815002/ and .orphaned-*.jsonl - for sibling in glob.glob(glob.escape(path.removesuffix(".jsonl")) + "*"): + # The transcript goes last: a failed delete leaves a session that session_open still sees, never a new session with this id beside the old one's files. + for sibling in sorted(glob.glob(glob.escape(stem) + "*"), key=lambda sibling: sibling == path): try: shutil.rmtree(sibling) if os.path.isdir(sibling) else os.remove(sibling) except OSError as failure: - print(f"could not remove {sibling}: {failure}", file=sys.stderr) + return Failure(f"could not delete {sibling}: {failure.strerror or failure}; delete every file and folder matching {stem}*, then rerun loop-claude; a rerun before that resumes the finished chunk's session") + return None diff --git a/bin/loop_claude/cycle/steps.py b/bin/loop_claude/cycle/steps.py index c6d315d3..c91c0e2b 100644 --- a/bin/loop_claude/cycle/steps.py +++ b/bin/loop_claude/cycle/steps.py @@ -1,7 +1,7 @@ import dataclasses import typing -from loop_claude.claude_code.session import Session, delete_session, transcript_file +from loop_claude.claude_code.session import Session, delete_session, session_open, transcript_file from loop_claude.claude_code.transcript import read, render from loop_claude.cycle.archive import archive from loop_claude.cycle.interrupt import interrupt @@ -56,7 +56,7 @@ def work(cycle: Cycle) -> WorkerRan | Stop: 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}") + return WorkerRan(path) if path is not None else Stop(1, f"claude left no transcript for session {cycle.session.id}") def rule(cycle: Cycle, transcript_path: str) -> Ruled | Stop: @@ -77,9 +77,6 @@ def ask(cycle: Cycle, transcript_path: str, question: str) -> Answered | 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) + if failure := delete_session(cycle.session): + return Stop(1, failure.reason) return Archived() - - -def session_open(session: Session) -> bool: - return bool(transcript_file(session)) diff --git a/bin/loop_claude/loop/main.py b/bin/loop_claude/loop/main.py index 71f43961..8100bce6 100644 --- a/bin/loop_claude/loop/main.py +++ b/bin/loop_claude/loop/main.py @@ -11,12 +11,12 @@ its session next cycle; the next chunk starts a fresh session. Cycles continue until an exit below or Ctrl-C. Words: ~/.claude/CLAUDE.md → "Vocabulary". Exit 1: no task, git cannot start or finds no checkout, no premises, a models file it cannot read or -write, claude leaves no transcript or one it cannot read or archive, claude cannot start for an -interrupt, python cannot start for a reload, a reload whose loop run it cannot read, or the worker or the -judge fails three times running, retried a minute apart within the cycle. A worker fails when claude -cannot start, exits non-zero, is killed, or ends without a result event; a judge when claude cannot -start or exits non-zero, or its verdict misses or mistypes a field, interrupts without a question, -or quotes nothing in the transcript. +write, claude leaves no transcript or one it cannot read or archive, a finished chunk's session it +cannot delete, claude cannot start for an interrupt, python cannot start for a reload, a reload whose +loop run it cannot read, or the worker or the judge fails three times running, retried a minute apart +within the cycle. A worker fails when claude cannot start, exits non-zero, is killed, or ends without +a result event; a judge when claude cannot start or exits non-zero, or its verdict misses or mistypes +a field, interrupts without a question, or quotes nothing in the transcript. Exit 2: the human is needed, to pick the model or answer an interrupt, and stdin or stdout is not a terminal, the input ends, or the interrupt ended with nothing typed. Rerunning resumes any open session. diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index d546b3ba..d8ef6e6c 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -152,7 +152,7 @@ class Cycles(unittest.TestCase): fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("finished")]) self.assertIsNone(steps.run_cycle(self.cycle(fake))) self.assertEqual(self.archived_verdicts(), ["Task: Do it.\n\nVerdict: finished\n"]) - self.assertEqual(session.transcript_file(self.session), "") + self.assertIsNone(session.transcript_file(self.session)) def test_an_unfinished_chunk_keeps_its_session_and_the_next_cycle_resumes_it(self): fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)] * 2, claude=[judged("unfinished")] * 2) @@ -178,6 +178,18 @@ class Cycles(unittest.TestCase): self.assertNotIn("the second", prompts[0]) self.assertIn("[human] the second", prompts[1]) + @unittest.skipIf(os.geteuid() == 0, "root writes into a read-only directory") + def test_stops_keeping_the_transcript_when_the_finished_chunks_session_cannot_be_deleted(self): + folder = self.paths.claude_code_transcripts / "-fake" / str(self.session.id) + (folder / "subagents").mkdir(parents=True) + folder.chmod(0o500) + self.addCleanup(folder.chmod, 0o700) + fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("finished")]) + stop = steps.run_cycle(self.cycle(fake)) + self.assertEqual(stop, Stop(1, f"could not delete {folder}: Permission denied; delete every file and folder matching {folder}*, then rerun loop-claude; a rerun before that resumes the finished chunk's session")) + self.assertTrue(session.session_open(self.session)) + self.assertEqual(self.archived_verdicts(), ["Task: Do it.\n\nVerdict: finished\n"]) + def test_stops_when_the_worker_leaves_no_transcript(self): fake = FakeEffects(streams=[Streamed(0)]) self.assertEqual(steps.run_cycle(self.cycle(fake)), Stop(1, f"claude left no transcript for session {self.session.id}")) diff --git a/todo.md b/todo.md index 57d397f0..e489b34e 100644 --- a/todo.md +++ b/todo.md @@ -8,7 +8,6 @@ The rebuild's design sketches, the blind judges' verdicts on them and what to ta The panels on `rebuild` at 8585257 and on PR #182 named these under Self-sufficiency and Locality, the two dimensions under the floor. Scores: `docs/decisions.md` → "comprehension floor". -- **Make a failed session delete stop the loop, and give the session one owner.** `delete_session` only prints a failed removal, so `close` reports `Archived` and the next chunk resumes the finished chunk's session without its own task prompt. Return a `Failure` and stop on it; move `session_open` into `session.py`; have `transcript_file` return `str | None`. Goal 4.1. - **Give larv-review §3's rules one home per rule.** Every seat of the 2026-10-01 panel needed `SKILL.md`, `review-findings.md` and `CLAUDE.md` open to apply one rule of "Round cap" or "Re-entry". Goal 4.1. - **Name `loop_claude`'s modules and words by what each holds.** The 2026-10-01 panel misread `formats.py` (it holds every Claude Code shape, `mark_trusted` and `models_in` included), `steps.py`, `outcome.py` and `human_needed.py`. "Project", "transcripts", "verdict" and "model" each name two or three things. Goal 4.1. - **State in `reload.py` why a loop reloads its own script, and tie each exit-1 clause of `--help` to the `Stop` that produces it.** Two seats found the reload's purpose nowhere in the code, and one could keep the exit contract true only by grepping every `Stop(1`. Name what a reload could not read, and its likely cause, in both `--help` and the reload's error: "loop run" is defined nowhere a reader looks. Goal 4.1.