diff --git a/.gitignore b/.gitignore index d1c867d8..1f3ba5cb 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,8 @@ !agents/ !agents/*.md !bin/ +!bin/file_io/ +!bin/file_io/*.py !bin/loop-claude !bin/loop_claude/ !bin/loop_claude/**/ diff --git a/bin/file_io/__init__.py b/bin/file_io/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/bin/loop_claude/failure.py b/bin/file_io/failure.py similarity index 100% rename from bin/loop_claude/failure.py rename to bin/file_io/failure.py diff --git a/bin/loop_claude/markdown_sections.py b/bin/file_io/markdown.py similarity index 100% rename from bin/loop_claude/markdown_sections.py rename to bin/file_io/markdown.py diff --git a/bin/loop_claude/file_writes.py b/bin/file_io/writes.py similarity index 95% rename from bin/loop_claude/file_writes.py rename to bin/file_io/writes.py index cca4bdaa..3ab80661 100644 --- a/bin/loop_claude/file_writes.py +++ b/bin/file_io/writes.py @@ -3,7 +3,7 @@ import os import pathlib import typing -from loop_claude.failure import Failure +from file_io.failure import Failure def make_private(folder: pathlib.Path) -> None: diff --git a/bin/loop_claude/claude_code/session.py b/bin/loop_claude/claude_code/session.py index a04ff067..c1f965d5 100644 --- a/bin/loop_claude/claude_code/session.py +++ b/bin/loop_claude/claude_code/session.py @@ -5,7 +5,7 @@ import pathlib import shutil import uuid -from loop_claude.failure import Failure +from file_io.failure import Failure from loop_claude.paths import Paths NAMESPACE = uuid.UUID("64fb586b-d926-4bb3-a44c-c903e86d3eec") diff --git a/bin/loop_claude/claude_code/transcript.py b/bin/loop_claude/claude_code/transcript.py index 29eaa246..f30efb17 100644 --- a/bin/loop_claude/claude_code/transcript.py +++ b/bin/loop_claude/claude_code/transcript.py @@ -1,7 +1,7 @@ import json from loop_claude.claude_code.entries import assistant_parts, human_text, loop_text, parse_entry, part_text, tool_call -from loop_claude.failure import Failure +from file_io.failure import Failure JUDGED_TOOL_INPUT = 400 diff --git a/bin/loop_claude/claude_code/trust.py b/bin/loop_claude/claude_code/trust.py index d6cb662e..ee8ec4a9 100644 --- a/bin/loop_claude/claude_code/trust.py +++ b/bin/loop_claude/claude_code/trust.py @@ -2,8 +2,8 @@ import json import pathlib import sys -from loop_claude.failure import Failure -from loop_claude.file_writes import replace_json +from file_io.failure import Failure +from file_io.writes import replace_json # A failure only warns: the interrupt still runs, and the human answers Claude Code's trust dialog. diff --git a/bin/loop_claude/cycle/archive.py b/bin/loop_claude/cycle/archive.py index 03f1b4a7..acf852f8 100644 --- a/bin/loop_claude/cycle/archive.py +++ b/bin/loop_claude/cycle/archive.py @@ -3,9 +3,9 @@ import os import pathlib import sys -from loop_claude.failure import Failure +from file_io.failure import Failure from loop_claude.claude_code.transcript import first_timestamped_line -from loop_claude.file_writes import make_private, replace_file +from file_io.writes import make_private, replace_file from loop_claude.paths import Paths from loop_claude.claude_code.session import session_files diff --git a/bin/loop_claude/cycle/interrupt.py b/bin/loop_claude/cycle/interrupt.py index d36ee638..b92f25ea 100644 --- a/bin/loop_claude/cycle/interrupt.py +++ b/bin/loop_claude/cycle/interrupt.py @@ -2,7 +2,7 @@ import sys import uuid from loop_claude.effects import Effects -from loop_claude.failure import Failure +from file_io.failure import Failure from loop_claude.claude_code.transcript import human_turns, read_transcript from loop_claude.stop import needs_human, needs_terminal, Stop from loop_claude.paths import Paths diff --git a/bin/loop_claude/cycle/judge.py b/bin/loop_claude/cycle/judge.py index 7d7d5062..fe2392f7 100644 --- a/bin/loop_claude/cycle/judge.py +++ b/bin/loop_claude/cycle/judge.py @@ -4,7 +4,7 @@ import re import typing from loop_claude.effects import Effects -from loop_claude.failure import Failure +from file_io.failure import Failure from loop_claude.cycle.verdicts import Finished, Interrupt, QuotedAnswer, Unfinished, Verdict from loop_claude.claude_code.entries import hit_limit, limit_said, parse_entry, structured_output from loop_claude.cycle.limited import Limited diff --git a/bin/loop_claude/cycle/retry.py b/bin/loop_claude/cycle/retry.py index c6a52464..8e35edad 100644 --- a/bin/loop_claude/cycle/retry.py +++ b/bin/loop_claude/cycle/retry.py @@ -2,7 +2,7 @@ import sys import typing from loop_claude.effects import Effects -from loop_claude.failure import Failure +from file_io.failure import Failure from loop_claude.cycle.limited import Limited from loop_claude.stop import Stop diff --git a/bin/loop_claude/cycle/run.py b/bin/loop_claude/cycle/run.py index 898f168a..58e9a168 100644 --- a/bin/loop_claude/cycle/run.py +++ b/bin/loop_claude/cycle/run.py @@ -5,7 +5,7 @@ from loop_claude.cycle.states import Active, Archived, Closing, Ended, Event, Hu from loop_claude.cycle.archive import archive from loop_claude.claude_code.session import delete_session, Session, session_open, transcript_file from loop_claude.effects import Effects -from loop_claude.failure import Failure +from file_io.failure import Failure from loop_claude.cycle.verdicts import Finished, QuotedAnswer, Unfinished from loop_claude.cycle.interrupt import interrupt from loop_claude.cycle.judge import judge diff --git a/bin/loop_claude/cycle/worker.py b/bin/loop_claude/cycle/worker.py index fd6013c4..243138e3 100644 --- a/bin/loop_claude/cycle/worker.py +++ b/bin/loop_claude/cycle/worker.py @@ -1,6 +1,6 @@ from loop_claude.claude_code.entries import assistant_parts, ends_stream, hit_limit, limit_said, parse_entry, rejected_until from loop_claude.effects import Effects -from loop_claude.failure import Failure +from file_io.failure import Failure from loop_claude.cycle.limited import Limited from loop_claude.claude_code.transcript import part_line from loop_claude.project.records import QUOTE_SOURCES diff --git a/bin/loop_claude/loop/main.py b/bin/loop_claude/loop/main.py index 489b76f8..e0cf6f35 100644 --- a/bin/loop_claude/loop/main.py +++ b/bin/loop_claude/loop/main.py @@ -33,8 +33,8 @@ Every claude loop-claude starts runs the project's model, saved per in run, is asked for one. LOOP_CLAUDE_JUDGE_MODEL overrides it for the judge, LOOP_CLAUDE_WORKER_MODEL for the worker and the interrupt; with both set, nothing is asked. -A change to bin/loop-claude or bin/loop_claude/**/*.py takes effect at the next cycle boundary, once -loop-claude --help succeeds: the loop reloads into the edited script. +A change to bin/loop-claude, bin/file_io/**/*.py or bin/loop_claude/**/*.py takes effect at the next +cycle boundary, once loop-claude --help succeeds: the loop reloads into the edited script. Exit 1, saying why, when: - no task is given diff --git a/bin/loop_claude/loop/reload.py b/bin/loop_claude/loop/reload.py index e7fc5bc0..5bdb987e 100644 --- a/bin/loop_claude/loop/reload.py +++ b/bin/loop_claude/loop/reload.py @@ -8,6 +8,7 @@ import typing from loop_claude.effects import Effects from loop_claude.stop import Stop +PACKAGES = ("file_io", "loop_claude") RELOAD_FLAG = "--reloaded" T = typing.TypeVar("T") @@ -36,7 +37,7 @@ def decode_handed(handed: str, kind: type[T]) -> T | None: def fingerprint(launcher: pathlib.Path) -> str: digest = hashlib.sha256() - for path in [launcher, *sorted((launcher.parent / "loop_claude").rglob("*.py"))]: + for path in [launcher, *sorted(path for package in PACKAGES for path in (launcher.parent / package).rglob("*.py"))]: try: content = path.read_bytes() except OSError as failure: diff --git a/bin/loop_claude/project/model_choice.py b/bin/loop_claude/project/model_choice.py index 53868918..03fdf0a5 100644 --- a/bin/loop_claude/project/model_choice.py +++ b/bin/loop_claude/project/model_choice.py @@ -6,10 +6,10 @@ import typing from loop_claude.claude_code.model_catalog import catalog_models from loop_claude.effects import Effects -from loop_claude.failure import Failure +from file_io.failure import Failure from loop_claude.stop import needs_human, needs_terminal, Stop from loop_claude.paths import Paths -from loop_claude.file_writes import replace_json +from file_io.writes import replace_json @dataclasses.dataclass(frozen=True) diff --git a/bin/loop_claude/project/premises.py b/bin/loop_claude/project/premises.py index 5f3f02c7..c3b02ca6 100644 --- a/bin/loop_claude/project/premises.py +++ b/bin/loop_claude/project/premises.py @@ -1,5 +1,5 @@ from loop_claude.project.checkout import read_checkout_file -from loop_claude.markdown_sections import sections +from file_io.markdown import sections from loop_claude.stop import Stop diff --git a/bin/rank_todo.py b/bin/rank_todo.py index 8b9d0848..42178fd5 100755 --- a/bin/rank_todo.py +++ b/bin/rank_todo.py @@ -57,10 +57,9 @@ import pathlib import re import sys -# Kept inside loop_claude/ so reload's fingerprint covers them. -from loop_claude.failure import Failure -from loop_claude.markdown_sections import FENCE, sections -from loop_claude.file_writes import replace_file +from file_io.failure import Failure +from file_io.markdown import FENCE, sections +from file_io.writes import replace_file EXEMPTIONS = ("", "decision", "defect", "principle", "question", "security") ITEMS_HEADER = "| ID | Release | Exempt | Item | R | S | A | G | Goals | Score |" diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index 32e38ace..2197500c 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -20,11 +20,12 @@ from loop_claude.cycle import archive, interrupt, judge, retry, run, states, wor from loop_claude.project import checkout, model_choice, records from loop_claude.effects import Effects from loop_claude.claude_code import entries, session, transcript, trust -from loop_claude.failure import Failure -from loop_claude import file_writes, markdown_sections, paths +from file_io.failure import Failure from loop_claude.cycle.verdicts import Finished, Interrupt, QuotedAnswer, Unfinished from loop_claude.cycle.limited import Limited from loop_claude.loop import main, reload +from file_io import markdown, writes +from loop_claude import paths from loop_claude.stop import Stop CWD = pathlib.Path("/c") @@ -397,12 +398,12 @@ class Records(unittest.TestCase): class Sections(unittest.TestCase): def test_keeps_every_headed_section_with_its_bodies(self): text = "# Title\n\nintro\n\n## Goals\n\n1. one\n```sh\n# not a heading\n```\n### Later\n2. two\n#1 priority\n\n## Empty\n\n## Audience\nme\n## Audience\nagain\n# Appendix\nnot audience\n" - self.assertEqual(markdown_sections.sections(text), {"Goals": ["1. one\n```sh\n# not a heading\n```\n### Later\n2. two\n#1 priority"], "Empty": [""], "Audience": ["me", "again"]}) + self.assertEqual(markdown.sections(text), {"Goals": ["1. one\n```sh\n# not a heading\n```\n### Later\n2. two\n#1 priority"], "Empty": [""], "Audience": ["me", "again"]}) def test_closes_a_fence_only_on_its_own_marker(self): text = "## Goals\n````md\n~~~\n## Quoted\n```\n````\n ~~~\n## Also quoted\n ~~~\n1. one\n## Audience\nme\n" - self.assertEqual(list(markdown_sections.sections(text)), ["Goals", "Audience"]) - self.assertEqual(list(markdown_sections.sections("```\nquoted:\n```python\n## Inside\n```\n## Goals\n1. one\n")), ["Goals"]) + self.assertEqual(list(markdown.sections(text)), ["Goals", "Audience"]) + self.assertEqual(list(markdown.sections("```\nquoted:\n```python\n## Inside\n```\n## Goals\n1. one\n")), ["Goals"]) class Render(unittest.TestCase): @@ -606,7 +607,7 @@ class ReplaceFile(unittest.TestCase): def test_removes_what_it_staged_when_the_replace_fails(self): with tempfile.TemporaryDirectory() as root: os.mkdir(os.path.join(root, "in-the-way")) - self.assertEqual(file_writes.replace_file(pathlib.Path(root, "in-the-way"), b"data"), Failure("Is a directory")) + self.assertEqual(writes.replace_file(pathlib.Path(root, "in-the-way"), b"data"), Failure("Is a directory")) self.assertEqual(os.listdir(root), ["in-the-way"]) diff --git a/bin/test_loop_claude_black_box.py b/bin/test_loop_claude_black_box.py index 8c9f0209..2a50efb0 100644 --- a/bin/test_loop_claude_black_box.py +++ b/bin/test_loop_claude_black_box.py @@ -670,14 +670,15 @@ class Reload(unittest.TestCase): worker(append=["loop-claude", "\ndef\n"]), judge(), worker(replace=["loop-claude", "\ndef\n", "\n# edited\n"]), judge(), worker(append=["loop_claude/nested/edited.py", "# edited\n"]), judge(), + worker(append=["file_io/writes.py", "# edited\n"]), judge(), worker(), ]) self.assertEqual(run.code, 130) - self.assertEqual(run.roles(), ["worker", "judge"] * 4) + self.assertEqual(run.roles(), ["worker", "judge"] * 5) self.assertIn("keeping the running script", run.stderr) reloads = [json.loads(argv[1]) for argv in run.starts if argv[:1] == ["--reloaded"]] - self.assertEqual(reloads, [{"cycle_number": cycle, "root": str(checkout.root), "task": TASK} for cycle in (2, 3)]) - self.assertIn("=== cycle 4 ===", run.stdout) + self.assertEqual(reloads, [{"cycle_number": cycle, "root": str(checkout.root), "task": TASK} for cycle in (2, 3, 4)]) + self.assertIn("=== cycle 5 ===", run.stdout) outside = checkout.temp / "outside" outside.mkdir() diff --git a/bin/test_rank_todo.py b/bin/test_rank_todo.py index c7430f75..202c717b 100644 --- a/bin/test_rank_todo.py +++ b/bin/test_rank_todo.py @@ -7,7 +7,7 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) -from loop_claude.failure import Failure +from file_io.failure import Failure import rank_todo from test_python_floor import ROOT diff --git a/bin/test_types.py b/bin/test_types.py index e071bb85..a505de47 100644 --- a/bin/test_types.py +++ b/bin/test_types.py @@ -11,5 +11,5 @@ class Types(unittest.TestCase): self.assertIsNotNone(floor, "README.md's Install names `Python 3.N or newer`") if shutil.which("uv") is None: self.fail("uv on PATH fetches mypy; see README.md's Test line") - ran = subprocess.run(["uv", "tool", "run", "--python", "%d.%d" % floor, "mypy==2.3.1", "--cache-dir=/dev/null", "--check-untyped-defs", "--disallow-untyped-defs", "-m", "rank_todo", "-m", "statusline", "-p", "loop_claude"], capture_output=True, cwd=ROOT / "bin", text=True) + ran = subprocess.run(["uv", "tool", "run", "--python", "%d.%d" % floor, "mypy==2.3.1", "--cache-dir=/dev/null", "--check-untyped-defs", "--disallow-untyped-defs", "-m", "rank_todo", "-m", "statusline", "-p", "file_io", "-p", "loop_claude"], capture_output=True, cwd=ROOT / "bin", text=True) self.assertEqual(ran.returncode, 0, ran.stdout + ran.stderr)