diff --git a/README.md b/README.md index 4e1e67c6..3550176a 100644 --- a/README.md +++ b/README.md @@ -219,7 +219,7 @@ Install puts only `loop-claude` on `PATH`; call the others by their path, such a ### Packages under `bin/` -`bin/` is the import root: each launcher puts it on `sys.path`, so a module imports another by its path under `bin/`, such as `from claude_code.entries import parse_entry`. Each tool's package holds its tests. Beside the packages, `bin/`'s root holds only the launchers, the tools of one file with their tests, `test_env.py`, `failure.py` and `setup.cfg`. `failure.py`'s `Failure`, which modules across `bin/` return for a failed step, belongs to no single package. `agent_cycle/`, `atlassian/`, `branch_chunks/`, `chunk_driver/`, `claude_code/`, `command_lines/`, `forge/`, `git_cli/`, `loop_effects/`, `loop_state/`, `machine_load/`, `model_routing/`, `notifier/`, `plan/`, `plan_store/`, `record_store/`, `release_plan/`, `review_rules/`, `rung_replay/`, `text_files/` and `transcript_archive/` serve more than one tool. No package imports one that imports it back, and a package's `doubles.py` holds the test doubles that other packages' tests use, so no module imports a `test_*` module of another package; `repo_checks/test_package_cycles.py` checks both. +`bin/` is the import root: each launcher puts it on `sys.path`, so a module imports another by its path under `bin/`, such as `from claude_code.entries import parse_entry`. Each tool's package holds its tests. Beside the packages, `bin/`'s root holds only the launchers, the tools of one file with their tests, `test_env.py`, `failure.py` and `setup.cfg`. `failure.py`'s `Failure`, which modules across `bin/` return for a failed step, belongs to no single package. `agent_cycle/`, `atlassian/`, `branch_chunks/`, `chunk_driver/`, `claude_code/`, `command_lines/`, `forge/`, `git_cli/`, `loop_effects/`, `loop_state/`, `machine_load/`, `model_routing/`, `notifier/`, `plan/`, `plan_store/`, `record_store/`, `release_plan/`, `review_rules/`, `rung_replay/`, `text_files/` and `transcript_archive/` serve more than one tool. No package imports one that imports it back, and a package's `doubles.py` holds the test doubles that other packages' tests use, so no module imports a `test_*` module of another package; `repo_checks/test_package_cycles.py` checks both. No test swaps a global with `mock.patch`: the code under test takes each effect as an argument, and the test hands it a double, which `repo_checks/test_no_patched_globals.py` checks. - `adf_codec/` converts markdown to ADF, the rich text of Jira Cloud and Confluence, and back, in a throwaway container running a pinned `@larvit/adf-codec`. It builds the container's image inside a heavy slot. - `agent_cycle/` runs one cycle (`run.py`): a headless claude worker, then the judge of its transcript. It also holds the interrupt, the tmux window and the routing of the worker's rung. The tool that drives the cycles supplies the prompts and file names of its own workflow (`worker_prompts.py`), and reads which items a worker took in. `loop_claude` and `do_epic` drive it; `agent_cycle` doesn't import either. `doubles.py` holds the doubles its tests share with `chunk_driver`'s. diff --git a/bin/adf_codec/convert.py b/bin/adf_codec/convert.py index 4e9ed444..fe3f4720 100644 --- a/bin/adf_codec/convert.py +++ b/bin/adf_codec/convert.py @@ -8,16 +8,18 @@ adf-codec has two pairs of calls, and each function here names the one it runs: """ import contextlib import dataclasses +import fcntl import hashlib import json import os import pathlib import subprocess import sys +import time import typing from failure import Failure -from machine_load.slots import HELD, slot_count, slot_folder, usable_cpus, waited_slot +from machine_load.slots import HELD, Lock, slot_count, slot_folder, usable_cpus, waited_slot SHIM = pathlib.Path(__file__).resolve().parent / "node_shim" SHIM_FILES = ("Dockerfile", "package.json", "package-lock.json", "shim.mjs") @@ -62,7 +64,13 @@ def docker_args(image: str, mode: str) -> list[str]: return ["docker", "run", "--rm", "-i", "--memory", MEMORY, "--memory-swap", MEMORY, "--network", "none", "--read-only", image, mode] -def docker(args: list[str], text: str, seconds: int, call: typing.Callable[..., typing.Any] = subprocess.run) -> Ran | Failure: +def docker(args: list[str], text: str, seconds: int) -> Ran | Failure: + """Runs a docker command line as a process of this one: the shim's shell, as git_repo.git is git's.""" + return docker_ran(args, text, seconds, subprocess.run) + + +def docker_ran(args: list[str], text: str, seconds: int, call: typing.Callable[..., typing.Any]) -> Ran | Failure: + """What call, which runs a command line as subprocess.run does, made of a docker command line on text.""" try: done = call(args, input=text, capture_output=True, text=True, encoding="utf-8", timeout=seconds) except FileNotFoundError: @@ -73,16 +81,22 @@ def docker(args: list[str], text: str, seconds: int, call: typing.Callable[..., @contextlib.contextmanager -def heavy_slot(env: typing.Mapping[str, str] = os.environ, slots: int | None = None) -> typing.Iterator[Failure | None]: - """Holds one of the machine's heavy slots, which run-heavy shares, for the with block. Under run-heavy the process already holds one, so it doesn't wait.""" +def machine_slot() -> typing.Iterator[Failure | None]: + """heavy_slot on this process's environment, home, processors, stderr and clock: the shim's shell, as docker is.""" + with heavy_slot(os.environ, pathlib.Path.home(), slot_count(usable_cpus()), lambda text: print(text, file=sys.stderr, flush=True), time.sleep, fcntl.flock) as refused: + yield refused + + +@contextlib.contextmanager +def heavy_slot(env: typing.Mapping[str, str], home: pathlib.Path, slots: int, say: typing.Callable[[str], None], sleep: typing.Callable[[float], None], lock: Lock) -> typing.Iterator[Failure | None]: + """Holds one of the machine's heavy slots, which run-heavy shares, for the with block, telling say where it waits for one. Under run-heavy the process already holds one, so it doesn't wait.""" if HELD in env: yield None return - folder = slot_folder(env, pathlib.Path.home()) - count = slot_count(usable_cpus()) if slots is None else slots + folder = slot_folder(env, home) try: folder.mkdir(parents=True, exist_ok=True) - _, fd = waited_slot(folder, count, lambda: print(f"adf-codec: waiting for a heavy slot, of {count} on this machine, in {folder}, to build the shim's image; `fuser {folder}/slot-*` names the processes that hold them", file=sys.stderr, flush=True)) + _, fd = waited_slot(folder, slots, lambda: say(f"adf-codec: waiting for a heavy slot, of {slots} on this machine, in {folder}, to build the shim's image; `fuser {folder}/slot-*` names the processes that hold them"), sleep, lock) except OSError as failure: yield Failure(f"cannot take a heavy slot in {folder}: {failure.strerror}") return @@ -92,7 +106,7 @@ def heavy_slot(env: typing.Mapping[str, str] = os.environ, slots: int | None = N os.close(fd) -def ensure_image(shim: pathlib.Path, run_docker: Docker = docker, slot: Slot = heavy_slot) -> str | Failure: +def ensure_image(shim: pathlib.Path, run_docker: Docker = docker, slot: Slot = machine_slot) -> str | Failure: """The tag of the image for the shim folder, built from that folder first where this machine has no image under that tag. The build waits for a heavy slot. Once this process holds the slot, it inspects again, as another process may have built the image meanwhile.""" diff --git a/bin/adf_codec/test_adf_codec.py b/bin/adf_codec/test_adf_codec.py index c1f79f42..464ec4e7 100644 --- a/bin/adf_codec/test_adf_codec.py +++ b/bin/adf_codec/test_adf_codec.py @@ -1,4 +1,5 @@ import contextlib +import fcntl import json import os import subprocess @@ -161,7 +162,7 @@ def test_docker_passes_the_text_on_stdin_and_returns_the_output(): seen.update(kwargs, args=args) return subprocess.CompletedProcess(args, 3, "out", "err") - assert convert.docker(["docker", "x"], "text", 7, call) == convert.Ran(3, "out", "err") + assert convert.docker_ran(["docker", "x"], "text", 7, call) == convert.Ran(3, "out", "err") assert seen == {"args": ["docker", "x"], "input": "text", "capture_output": True, "text": True, "encoding": "utf-8", "timeout": 7} @@ -169,7 +170,7 @@ def test_docker_missing_becomes_a_failure(): def call(args, **kwargs): raise FileNotFoundError - failure = convert.docker(["docker", "x"], "", 7, call) + failure = convert.docker_ran(["docker", "x"], "", 7, call) assert failure == Failure("docker is not installed, and the adf-codec shim runs in a container") @@ -177,7 +178,7 @@ def test_docker_that_takes_too_long_becomes_a_failure(): def call(args, **kwargs): raise subprocess.TimeoutExpired(args, 7) - assert convert.docker(["docker", "build", "x"], "", 7, call) == Failure("`docker build` took more than 7 seconds") + assert convert.docker_ran(["docker", "build", "x"], "", 7, call) == Failure("`docker build` took more than 7 seconds") def shim_dir(tmp_path): @@ -260,19 +261,27 @@ def test_a_failure_to_start_docker_passes_through_inspect_and_build(tmp_path, an assert convert.ensure_image(tmp_path, run_docker, Slot(run_docker)) == DOWN +def not_said(text): + raise AssertionError(f"said {text}") + + +def not_slept(seconds): + raise AssertionError(f"slept {seconds} seconds") + + def test_a_heavy_slot_is_held_until_the_block_ends(tmp_path): - with convert.heavy_slot({"XDG_STATE_HOME": str(tmp_path)}, 1) as held: + with convert.heavy_slot({"XDG_STATE_HOME": str(tmp_path)}, tmp_path, 1, not_said, not_slept, fcntl.flock) as held: assert held is None - assert slots.taken_slot(tmp_path / "run-heavy", 1) is None - slot, fd = slots.taken_slot(tmp_path / "run-heavy", 1) + assert slots.taken_slot(tmp_path / "run-heavy", 1, fcntl.flock) is None + slot, fd = slots.taken_slot(tmp_path / "run-heavy", 1, fcntl.flock) os.close(fd) def test_a_process_under_run_heavy_takes_no_second_slot(tmp_path): (tmp_path / "run-heavy").mkdir() - slot, fd = slots.taken_slot(tmp_path / "run-heavy", 1) + slot, fd = slots.taken_slot(tmp_path / "run-heavy", 1, fcntl.flock) try: - with convert.heavy_slot({"XDG_STATE_HOME": str(tmp_path), slots.HELD: "0"}, 1) as held: + with convert.heavy_slot({"XDG_STATE_HOME": str(tmp_path), slots.HELD: "0"}, tmp_path, 1, not_said, not_slept, fcntl.flock) as held: assert held is None finally: os.close(fd) @@ -280,7 +289,7 @@ def test_a_process_under_run_heavy_takes_no_second_slot(tmp_path): def test_a_heavy_slot_folder_that_cannot_be_made_yields_a_failure(tmp_path): (tmp_path / "run-heavy").write_text("a file where the folder goes") - with convert.heavy_slot({"XDG_STATE_HOME": str(tmp_path)}, 1) as held: + with convert.heavy_slot({"XDG_STATE_HOME": str(tmp_path)}, tmp_path, 1, not_said, not_slept, fcntl.flock) as held: assert isinstance(held, Failure) and held.reason.startswith(f"cannot take a heavy slot in {tmp_path / 'run-heavy'}: ") diff --git a/bin/agent_cycle/run.py b/bin/agent_cycle/run.py index 70702d4d..065cdef2 100644 --- a/bin/agent_cycle/run.py +++ b/bin/agent_cycle/run.py @@ -101,7 +101,7 @@ def archived_stop(project_archive: pathlib.Path, session: Session, task: str, ro def archived(project_archive: pathlib.Path, transcript_path: str, task: str, outcome: str, effects: Effects, judge_run: JudgeRun | None = None) -> Failure | None: """transcript_archive.write.archive, stamped now and saying its notes through effects. A cycle and close.py, which has no Cycle, both archive through it.""" - return archive(project_archive, transcript_path, task, outcome, effects.now(), lambda text: say(text, effects), judge_run) + return archive(project_archive, transcript_path, task, outcome, effects.now(), lambda text: say(text, effects), judge_run, replace=effects.replace_file) def step(cycle: Cycle, state: Active) -> Event | Stop: diff --git a/bin/agent_cycle/test_interrupt.py b/bin/agent_cycle/test_interrupt.py index 17d917de..ebc42344 100644 --- a/bin/agent_cycle/test_interrupt.py +++ b/bin/agent_cycle/test_interrupt.py @@ -7,7 +7,6 @@ import shutil import sys import tempfile import unittest -import unittest.mock import uuid sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) diff --git a/bin/agent_cycle/test_judge.py b/bin/agent_cycle/test_judge.py index 71ec6090..c4b5ed67 100644 --- a/bin/agent_cycle/test_judge.py +++ b/bin/agent_cycle/test_judge.py @@ -3,7 +3,6 @@ import json import pathlib import sys import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) diff --git a/bin/agent_cycle/test_retry.py b/bin/agent_cycle/test_retry.py index ed97ed74..eafc7630 100644 --- a/bin/agent_cycle/test_retry.py +++ b/bin/agent_cycle/test_retry.py @@ -3,7 +3,6 @@ import io import pathlib import sys import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) diff --git a/bin/agent_cycle/test_run.py b/bin/agent_cycle/test_run.py index 8a31f5ee..64440dc0 100644 --- a/bin/agent_cycle/test_run.py +++ b/bin/agent_cycle/test_run.py @@ -11,7 +11,6 @@ import sys import tempfile import types import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -25,6 +24,7 @@ from transcript_archive.location import project_archive from model_routing.ladder import ProjectRung, Rung from agent_cycle.doubles import PROMPTS, WINDOW from model_routing.ladder_file import read_ladder, SHIPPED +from text_files.file_writes import replace_file from model_routing.route import Route from agent_cycle import routing from agent_cycle import run, stuck_workers, worker_prompts @@ -202,11 +202,8 @@ Worker: {OFF_LADDER} def test_stops_when_a_judge_run_cannot_be_archived(self): self.archive.mkdir(parents=True) fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("finished")]) - def replace_file(path, data): - return Failure("disk full") if "/judge/" in str(path) else real_replace_file(path, data) - real_replace_file = write.replace_file - with unittest.mock.patch("transcript_archive.write.replace_file", replace_file): - stop = run.run_cycle(self.cycle(fake)) + fake.replace_file = lambda path, data: Failure("disk full") if "/judge/" in str(path) else replace_file(path, data) + stop = run.run_cycle(self.cycle(fake)) self.assertEqual(stop.code, 1) self.assertRegex(stop.reason, r"^could not archive the transcript: .*/judge/[0-9a-f]{64}\.json: disk full$") @@ -244,7 +241,7 @@ Worker: {OFF_LADDER} new = {"type": "assistant", "timestamp": "2026-09-21T14:13:21Z", "message": {"id": "new", "model": "claude-opus-5-5", "content": [], "usage": {"output_tokens": 5}}} transcript.write_text("\n".join(json.dumps(entry) for entry in [{"type": "queue-operation", "timestamp": "2026-09-21T14:00:00Z"}, old, human("No, keep the old name."), new]) + "\n") with contextlib.redirect_stderr(io.StringIO()): - self.assertIsNone(write.archive(self.archive, str(transcript), "Do it.", outcomes.unfinished("w", "e", "j", "", annotation=outcomes.Annotation("c", "Spent since the chunk began, in tokens of x:\n- worker, m: 1")), NOW)) + self.assertIsNone(write.archive(self.archive, str(transcript), "Do it.", outcomes.unfinished("w", "e", "j", "", annotation=outcomes.Annotation("c", "Spent since the chunk began, in tokens of x:\n- worker, m: 1")), NOW, replace=replace_file)) fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("unfinished", reversed="No, keep the old name.")]) run.run_cycle(self.cycle(fake)) latest = max(self.archived_outcomes(), key=len) diff --git a/bin/agent_cycle/test_spend.py b/bin/agent_cycle/test_spend.py index 3e6baf28..a2b5f267 100644 --- a/bin/agent_cycle/test_spend.py +++ b/bin/agent_cycle/test_spend.py @@ -3,7 +3,6 @@ import pathlib import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) diff --git a/bin/agent_cycle/test_states.py b/bin/agent_cycle/test_states.py index bb2efccb..328c01b3 100644 --- a/bin/agent_cycle/test_states.py +++ b/bin/agent_cycle/test_states.py @@ -1,7 +1,6 @@ import pathlib import sys import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) diff --git a/bin/agent_cycle/test_tmux.py b/bin/agent_cycle/test_tmux.py index 8577cecd..23fea0ef 100644 --- a/bin/agent_cycle/test_tmux.py +++ b/bin/agent_cycle/test_tmux.py @@ -2,7 +2,6 @@ import pathlib import subprocess import sys import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) diff --git a/bin/agent_cycle/test_worker.py b/bin/agent_cycle/test_worker.py index fae69230..c5d3cbc9 100644 --- a/bin/agent_cycle/test_worker.py +++ b/bin/agent_cycle/test_worker.py @@ -6,7 +6,6 @@ import pathlib import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) diff --git a/bin/atlassian/jira.py b/bin/atlassian/jira.py index fca56fc5..db5ed9e6 100644 --- a/bin/atlassian/jira.py +++ b/bin/atlassian/jira.py @@ -16,7 +16,8 @@ DONE = "done" # What a failed create ends with: a request Jira never answered, or answered brokenly, may still have filed the issue. PERHAPS_FILED = "unless Jira refused the request, the issue may exist: look under {under} before filing it again" # What a failed move ends with, for the same reason. -PERHAPS_MOVED = "unless Jira refused the request, the issue may have moved: look at {key} before finishing the item again" +SHAPE = "is not in the shape Jira Cloud's REST API version 3 documents" +PERHAPS_MOVED ="unless Jira refused the request, the issue may have moved: look at {key} before finishing the item again" @dataclasses.dataclass(frozen=True) @@ -44,17 +45,21 @@ def entry_of(raw: object, property_: str) -> Entry | Failure: key = raw.get("key") if isinstance(raw, dict) else None if not isinstance(raw, dict) or not isinstance(key, str) or not ISSUE_KEY.fullmatch(key): return Failure("the search answered an issue without an issue key") - # The answer's nested fields are read as Jira documents them; one missing or of another type ends the read here, as a Failure, so no later step meets it. - try: - fields = raw["fields"] - return Entry(key, fields, blocked_by_links(fields), raw.get("properties", {}).get(property_, {})) - except (AttributeError, KeyError, TypeError) as failure: - return Failure(unshaped(key, failure)) + # Each nested field is checked as Jira documents it; one missing or of another type ends the read here, as a Failure, so no later step meets it. + fields, properties = raw.get("fields"), raw.get("properties", {}) + if not isinstance(fields, dict): + return Failure(f"{key} {SHAPE}: its fields are no object") + if not isinstance(properties, dict): + return Failure(f"{key} {SHAPE}: its properties are no object") + links = blocked_by_links(fields) + if isinstance(links, Failure): + return Failure(f"{key} {SHAPE}: {links.reason}") + return Entry(key, fields, links, properties.get(property_, {})) def unshaped(key: str, failure: Exception) -> str: """Why the issue named key cannot be read, for a field that is missing or of another type than Jira documents.""" - return f"{key} is not in the shape Jira Cloud's REST API version 3 documents: {failure!r}" + return f"{key} {SHAPE}: {failure!r}" def offer_of(raw: object) -> tuple[str, str, str, str] | None: @@ -115,11 +120,11 @@ def rank_refusal(site: str, key: str, before: str, authorization: str, http: Htt return "; ".join(str(error) for entry in refused for error in entry.get("errors") or [f"Jira answered {entry.get('status')}"]) or None -def searched(site: str, what: str, jql: str, asked: dict[str, object], authorization: str, http: Http) -> list[object] | Failure: - """Every issue a JQL search on site finds, page by page. The dict asked holds the request's `fields` and `properties` lists. The text what names the issues sought, in the Failure of a search that doesn't end.""" +def searched(site: str, what: str, jql: str, asked: dict[str, object], authorization: str, http: Http, max_pages: int = MAX_PAGES) -> list[object] | Failure: + """Every issue a JQL search on site finds, page by page, up to max_pages pages. The dict asked holds the request's `fields` and `properties` lists. The text what names the issues sought, in the Failure of a search that doesn't end.""" body: dict[str, object] = {"jql": jql, "maxResults": PAGE} | asked found: list[object] = [] - for _ in range(MAX_PAGES): + for _ in range(max_pages): page = http("POST", f"{site}/rest/api/3/search/jql", authorization, body) if isinstance(page, Failure): return page @@ -129,9 +134,27 @@ def searched(site: str, what: str, jql: str, asked: dict[str, object], authoriza if not page.get("nextPageToken"): return found body = body | {"nextPageToken": page["nextPageToken"]} - return Failure(f"the search for {what} did not end within {MAX_PAGES} pages") + return Failure(f"the search for {what} did not end within {max_pages} pages") -def blocked_by_links(fields: dict[str, typing.Any]) -> tuple[Link, ...]: - """The "is blocked by" links among an issue's `issuelinks` field. A field not in the shape Jira documents raises a KeyError or TypeError, which the caller turns into a Failure.""" - return tuple(Link(link["inwardIssue"]["key"], link["id"]) for link in fields["issuelinks"] if "inwardIssue" in link and link["type"]["inward"] == "is blocked by") +def blocked_by_links(fields: dict[str, typing.Any]) -> tuple[Link, ...] | Failure: + """The "is blocked by" links among an issue's `issuelinks` field, or how that field is not in the shape Jira documents.""" + listed = fields.get("issuelinks") + if not isinstance(listed, list): + return Failure("its issuelinks are no list") + found = [] + for link in listed: + if not isinstance(link, dict): + return Failure("its issuelinks hold a link that is no object") + if "inwardIssue" not in link: + continue + kind = link.get("type") + if not isinstance(kind, dict) or not isinstance(kind.get("inward"), str): + return Failure("its issuelinks hold a link whose type names no inward text") + if kind["inward"] != "is blocked by": + continue + blocker, id_ = link["inwardIssue"], link.get("id") + if not isinstance(blocker, dict) or not isinstance(blocker.get("key"), str) or not isinstance(id_, str): + return Failure("its issuelinks hold a link whose id or whose linked issue's key is no text") + found.append(Link(blocker["key"], id_)) + return tuple(found) diff --git a/bin/atlassian/test_jira.py b/bin/atlassian/test_jira.py new file mode 100644 index 00000000..f2fcea04 --- /dev/null +++ b/bin/atlassian/test_jira.py @@ -0,0 +1,57 @@ +import pathlib +import sys +import unittest + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) + +from atlassian.jira import Entry, entry_of, Link +from failure import Failure +from atlassian import jira + +BLOCKED = {"id": "9", "type": {"inward": "is blocked by"}, "inwardIssue": {"key": "ABC-2"}} +SHAPE = "ABC-1 is not in the shape Jira Cloud's REST API version 3 documents: " + + +def raw(*links: object, **more: object) -> dict[str, object]: + return {"key": "ABC-1", "fields": {"issuelinks": list(links)}} | more + + +class TheHeadOfAnEntry(unittest.TestCase): + def test_holds_the_key_the_fields_the_blocked_by_links_and_the_property_asked_for(self): + outward = {"id": "3", "type": {"inward": "is blocked by"}, "outwardIssue": {"key": "ABC-3"}} + relates = {"id": "4", "type": {"inward": "relates to"}, "inwardIssue": {"key": "ABC-4"}} + found = entry_of(raw(BLOCKED, outward, relates, properties={"plan": {"L": 2}}), "plan") + self.assertEqual(found, Entry("ABC-1", {"issuelinks": [BLOCKED, outward, relates]}, (Link("ABC-2", "9"),), {"L": 2})) + self.assertEqual(entry_of(raw(), "plan"), Entry("ABC-1", {"issuelinks": []}, (), {})) + + def test_an_entry_without_an_issue_key_is_a_failure(self): + for wrong in ([], {}, {"key": 7}, {"key": "abc"}): + with self.subTest(wrong=wrong): + self.assertEqual(entry_of(wrong, "plan"), Failure("the search answered an issue without an issue key")) + + def test_an_entry_in_another_shape_is_a_failure_naming_the_issue_and_the_field(self): + cases = { + "its fields are no object": {"key": "ABC-1"}, + "its properties are no object": raw(properties=[]), + "its issuelinks are no list": {"key": "ABC-1", "fields": {}}, + "its issuelinks hold a link that is no object": raw("link"), + "its issuelinks hold a link whose type names no inward text": raw(BLOCKED | {"type": None}), + "its issuelinks hold a link whose id or whose linked issue's key is no text": raw(BLOCKED | {"inwardIssue": {"key": 7}}), + } + for how, wrong in cases.items(): + with self.subTest(how=how): + self.assertEqual(entry_of(wrong, "plan"), Failure(SHAPE + how)) + self.assertEqual(entry_of(raw(BLOCKED | {"id": 9}), "plan"), Failure(SHAPE + "its issuelinks hold a link whose id or whose linked issue's key is no text")) + self.assertEqual(entry_of(raw(BLOCKED | {"inwardIssue": None}), "plan"), Failure(SHAPE + "its issuelinks hold a link whose id or whose linked issue's key is no text")) + + def test_a_fault_in_reading_the_links_is_not_reported_as_jiras(self): + class Faulty(dict): + def get(self, key, default=None): + raise TypeError("ours") + + with self.assertRaisesRegex(TypeError, "ours"): + entry_of({"key": "ABC-1", "fields": Faulty()}, "plan") + + +if __name__ == "__main__": + unittest.main() diff --git a/bin/branch_chunks/test_branch_chunks.py b/bin/branch_chunks/test_branch_chunks.py index e790d5ef..16aa0877 100644 --- a/bin/branch_chunks/test_branch_chunks.py +++ b/bin/branch_chunks/test_branch_chunks.py @@ -5,7 +5,6 @@ import subprocess import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -258,8 +257,7 @@ class BranchChunks(unittest.TestCase): def read(self, run=None, titles=BARE): """branch_chunks over main, run by git_repo's git, or else by run.""" - with unittest.mock.patch.dict(os.environ, self.env, clear=True): - return branch_chunks("main", functools.partial(run or git, cwd=str(self.checkout)), titles) + return branch_chunks("main", functools.partial(run or git, cwd=str(self.checkout)), titles) def fixing_history(self): """Merges seven chunks onto main, then checks out a branch off it. Returns each of main's chunks by label, with the chunks that fixed it. diff --git a/bin/chunk_driver/claims.py b/bin/chunk_driver/claims.py index 2ba31dd8..e8837c80 100644 --- a/bin/chunk_driver/claims.py +++ b/bin/chunk_driver/claims.py @@ -1,12 +1,13 @@ """How entry points claim a checkout before they start a chunk in it, so that two never start the same chunk and do-epic never starts one where loop-claude runs. -An entry point holds `starting` on `project_lock` while it lists the windows of the checkout's chunks and starts new ones. It starts each chunk in a tmux window with CHUNK_FLAG, and hands that window the variables named with HANDED. +An entry point holds `starting` on `project_lock` while it lists the windows of the checkout's chunks and starts new ones. Where the lock cannot be taken, `starting` yields a Failure, and the entry point doesn't start any chunk. It starts each chunk in a tmux window with CHUNK_FLAG, and hands that window the variables named with HANDED. """ import contextlib import fcntl import pathlib import typing +from failure import Failure from transcript_archive.project_key import project_key from agent_cycle import tmux @@ -21,12 +22,28 @@ HANDED = "LOOP_CLAUDE_" @contextlib.contextmanager -def starting(folder: pathlib.Path) -> typing.Iterator[None]: - """Holds the LOCK in folder: project_lock's for a project, or do-epic's for an epic. Raises OSError where the file cannot be opened or locked.""" - folder.mkdir(parents=True, exist_ok=True) - with open(folder / LOCK, "a") as lock: - fcntl.flock(lock, fcntl.LOCK_EX) - yield +def starting(folder: pathlib.Path) -> typing.Iterator[Failure | None]: + """Holds the LOCK in folder: project_lock's for a project, or do-epic's for an epic. It yields a Failure, and doesn't hold any lock, where the file cannot be opened or locked.""" + with locked(folder / LOCK) as failure: + yield failure + + +@contextlib.contextmanager +def locked(file: pathlib.Path) -> typing.Iterator[Failure | None]: + """Holds a flock on file, which it makes with its folder. It yields a Failure, and doesn't hold any lock, where the file cannot be opened or locked.""" + try: + file.parent.mkdir(parents=True, exist_ok=True) + lock = open(file, "a") + except OSError as failure: + yield Failure(f"cannot lock {failure.filename or file}: {failure.strerror}") + return + with lock: + try: + fcntl.flock(lock, fcntl.LOCK_EX) + except OSError as failure: + yield Failure(f"cannot lock {file}: {failure.strerror}") + return + yield None def project_lock(root: str, home: pathlib.Path) -> pathlib.Path: diff --git a/bin/chunk_driver/project/model_choice.py b/bin/chunk_driver/project/model_choice.py index 5153c7f9..eedfb6b0 100644 --- a/bin/chunk_driver/project/model_choice.py +++ b/bin/chunk_driver/project/model_choice.py @@ -5,11 +5,11 @@ import sys import typing from claude_code.model_catalog import catalog_models +from chunk_driver.claims import CLAIMS, locked from loop_effects.effects import Effects, say from failure import Failure from agent_cycle.stop import needs_human, needs_terminal, Stop from loop_state.paths import Paths -from text_files.file_writes import replace_json @dataclasses.dataclass(frozen=True) @@ -21,6 +21,8 @@ class Models: # The environment variables that name the judge's and the worker's model for one run, over the model saved or configured. JUDGE_MODEL = "LOOP_CLAUDE_JUDGE_MODEL" WORKER_MODEL = "LOOP_CLAUDE_WORKER_MODEL" +# The file, in CLAIMS under the home folder, that save_model keeps locked with flock while it rereads and replaces the models file. The models file's own folder is a checkout of this setup, which takes no lock file. +MODELS_LOCK = "models.lock" def overridden(env: typing.Mapping[str, str], default: str) -> Models: @@ -48,18 +50,29 @@ def get_or_ask_model(project_key: str, paths: Paths, driver: str, effects: Effec chosen = ask_for_model(project_key, saved, paths.model_catalog, driver, effects) if isinstance(chosen, Stop): return chosen - # Reread: another loop may have saved its project while this one waited on the human. - models = read_models_file(saved) - if isinstance(models, Failure): - return Stop(1, models.reason) - if failure := replace_json(saved, {**models, project_key: chosen}, indent="\t", sort_keys=True): - return Stop(1, f"could not save the model in {saved}: {failure.reason}") + if failure := save_model(project_key, chosen, paths, effects.replace_file): + return Stop(1, failure.reason) say(f"{project_key} runs on {chosen}", effects) return chosen +def save_model(project_key: str, chosen: str, paths: Paths, replace: typing.Callable[[pathlib.Path, bytes], Failure | None]) -> Failure | None: + """Adds the project's model to the models file, whose only writer this is, through replace, which writes a file as replace_file does. It holds MODELS_LOCK from its read to its write, so two drivers that save at once both keep their pick.""" + saved = paths.models_file + with locked(paths.home / CLAIMS / MODELS_LOCK) as unlocked: + if unlocked: + return Failure(f"could not save the model in {saved}: {unlocked.reason}") + # Read under the lock: another driver may have saved its project while this one waited on the human. + models = read_models_file(saved) + if isinstance(models, Failure): + return models + if failure := replace(saved, json.dumps({**models, project_key: chosen}, indent="\t", sort_keys=True).encode()): + return Failure(f"could not save the model in {saved}: {failure.reason}") + return None + + def read_models_file(saved: pathlib.Path) -> dict[str, str] | Failure: - """Each project's model, as the models file at saved maps them; none where there is no such file. loop-claude and the replay tools read it.""" + """Each project's model, as the models file at saved maps them; none where there is no such file. The chunk drivers and the replay tools read it, and only save_model writes it.""" try: with open(saved, encoding="utf-8") as handle: models = json.load(handle) diff --git a/bin/chunk_driver/project/test_model_choice.py b/bin/chunk_driver/project/test_model_choice.py index 356cb873..c7ea56c9 100644 --- a/bin/chunk_driver/project/test_model_choice.py +++ b/bin/chunk_driver/project/test_model_choice.py @@ -1,8 +1,10 @@ +import dataclasses +import fcntl +import json import pathlib import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) @@ -10,6 +12,8 @@ from failure import Failure from loop_effects.doubles import FakeEffects from chunk_driver.drivers import LOOP_CLAUDE from chunk_driver.project import model_choice +from loop_state.paths import paths_from +from text_files.file_writes import replace_file from agent_cycle.stop import Stop @@ -20,6 +24,68 @@ class Overridden(unittest.TestCase): self.assertEqual(model_choice.overridden({"LOOP_CLAUDE_JUDGE_MODEL": "j", "LOOP_CLAUDE_WORKER_MODEL": "w"}, "saved"), model_choice.Models("j", "w")) +class SavingAModel(unittest.TestCase): + def setUp(self): + temp = tempfile.TemporaryDirectory() + self.addCleanup(temp.cleanup) + self.paths = paths_from(pathlib.Path(temp.name)) + self.lock = self.paths.home / ".local/state/loop-claude/models.lock" + + def test_adds_the_projects_model_to_those_the_file_holds(self): + self.paths.models_file.parent.mkdir() + self.paths.models_file.write_text(json.dumps({"other": "model-x"})) + self.assertIsNone(model_choice.save_model("proj", "model-b", self.paths, replace_file)) + self.assertEqual(json.loads(self.paths.models_file.read_text()), {"other": "model-x", "proj": "model-b"}) + + def lock_is_held(self) -> bool: + with open(self.lock, "a") as other: + try: + fcntl.flock(other, fcntl.LOCK_EX | fcntl.LOCK_NB) + except BlockingIOError: + return True + return False + + def test_holds_the_models_lock_from_its_read_to_its_write(self): + self.paths.models_file.parent.mkdir() + held = [] + + class Watched(pathlib.PosixPath): + """The models file, which notes whether the lock is held each time it is opened.""" + + def __fspath__(watched) -> str: + held.append(self.lock_is_held()) + return super().__fspath__() + + def replace(path: pathlib.Path, data: bytes) -> None: + held.append(self.lock_is_held()) + + paths = dataclasses.replace(self.paths, models_file=Watched(self.paths.models_file)) + self.assertIsNone(model_choice.save_model("proj", "model-b", paths, replace)) + self.assertEqual(held, [True, True]) + self.assertFalse(self.lock_is_held()) + + def test_doesnt_save_anything_where_it_cannot_take_the_lock(self): + self.paths.models_file.parent.mkdir() + self.lock.parent.parent.mkdir(parents=True) + self.lock.parent.write_text("") + saved = model_choice.save_model("proj", "model-b", self.paths, replace_file) + assert isinstance(saved, Failure) + self.assertRegex(saved.reason, f"^could not save the model in {self.paths.models_file}: cannot lock {self.lock.parent}: \\w") + self.assertFalse(self.paths.models_file.exists()) + + def test_a_pick_is_saved_through_save_model_alone(self): + self.paths.models_file.parent.mkdir() + fake, held = FakeEffects(inputs=["my-model"]), [] + + def replace(path: pathlib.Path, data: bytes) -> Failure: + held.append(self.lock_is_held()) + return Failure("could not save") + + fake.replace_file = replace + self.assertEqual(model_choice.get_or_ask_model("proj", self.paths, LOOP_CLAUDE, fake.effects()), Stop(1, f"could not save the model in {self.paths.models_file}: could not save")) + self.assertEqual(held, [True]) + + class ModelPrompt(unittest.TestCase): OFFERED = [("claude-opus-5-5", "Opus 5.5"), ("claude-haiku-4-5", "Haiku 4.5")] diff --git a/bin/chunk_driver/project/test_records.py b/bin/chunk_driver/project/test_records.py index bafe0a2d..be0f4590 100644 --- a/bin/chunk_driver/project/test_records.py +++ b/bin/chunk_driver/project/test_records.py @@ -2,7 +2,6 @@ import pathlib import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) diff --git a/bin/chunk_driver/test_chunk_run.py b/bin/chunk_driver/test_chunk_run.py index 1122a68e..1479dd10 100644 --- a/bin/chunk_driver/test_chunk_run.py +++ b/bin/chunk_driver/test_chunk_run.py @@ -16,6 +16,7 @@ from agent_cycle.verdicts import Finished, TakenIn, Unfinished from transcript_archive import outcomes, write from model_routing.ladder import ProjectRung, Rung from model_routing.ladder_file import read_ladder, SHIPPED +from text_files.file_writes import replace_file from model_routing.route import Route from agent_cycle import run from claude_code import session @@ -59,7 +60,7 @@ class Steps(unittest.TestCase): transcript.write_text(json.dumps({"type": "queue-operation", "timestamp": "1"}) + "\n") before = Route(self.FABLE_LOW, 3, 3, 4) with contextlib.redirect_stderr(io.StringIO()): - self.assertIsNone(write.archive(archive, str(transcript), "Do it.", outcomes.unfinished(before.worker_line(), "working", "j", ""), NOW)) + self.assertIsNone(write.archive(archive, str(transcript), "Do it.", outcomes.unfinished(before.worker_line(), "working", "j", ""), NOW, replace=replace_file)) after = chunk_run.closed_cycle(chunk_run.ChunkRun(cycle_number=1, task="Do it.", unfinished_evidence=""), OPUS_PROJECT, run.Concluded(Unfinished("working", taken_in=TakenIn(highest_likelihood=4)), before), archive, "a-driver", a_loop(FakeEffects().effects(), session=opened)) self.assertEqual(cycle_start.archived_route(archive, a_loop(FakeEffects().effects(), session=opened)), Route(self.FABLE_MEDIUM, 3, 3, 4)) with contextlib.redirect_stderr(io.StringIO()): @@ -79,6 +80,6 @@ class Steps(unittest.TestCase): transcript.write_text(json.dumps({"type": "queue-operation", "timestamp": "1"}) + "\n") before = Route(self.FABLE_LOW, 3, 3, 4) with contextlib.redirect_stderr(io.StringIO()): - self.assertIsNone(write.archive(archive, str(transcript), "Do it.", outcomes.unfinished(before.worker_line(), "working", "j", ""), NOW)) + self.assertIsNone(write.archive(archive, str(transcript), "Do it.", outcomes.unfinished(before.worker_line(), "working", "j", ""), NOW, replace=replace_file)) chunk_run.closed_cycle(chunk_run.ChunkRun(cycle_number=1, task="Do it.", unfinished_evidence=""), OPUS_PROJECT, run.Concluded(Unfinished("working", taken_in=TakenIn(highest_escape_and_impact=6)), before), archive, "a-driver", a_loop(FakeEffects().effects(), session=opened)) self.assertEqual(cycle_start.archived_route(archive, a_loop(FakeEffects().effects(), session=opened)), Route(self.FABLE_LOW, 3, 3, 4, escape_and_impact_taken_in=6)) diff --git a/bin/chunk_driver/test_claims.py b/bin/chunk_driver/test_claims.py new file mode 100644 index 00000000..9ccb694d --- /dev/null +++ b/bin/chunk_driver/test_claims.py @@ -0,0 +1,41 @@ +import pathlib +import sys +import tempfile +import unittest + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) + +from failure import Failure +from chunk_driver.claims import LOCK, starting + + +class Starting(unittest.TestCase): + def setUp(self) -> None: + temp = tempfile.TemporaryDirectory() + self.addCleanup(temp.cleanup) + self.folder = pathlib.Path(temp.name) + + def test_yields_none_while_it_holds_the_lock_in_a_folder_it_makes(self) -> None: + with starting(self.folder / "state" / "proj") as failure: + self.assertIsNone(failure) + self.assertTrue((self.folder / "state" / "proj" / LOCK).is_file()) + + def test_yields_a_failure_naming_the_path_it_could_not_make(self) -> None: + (self.folder / "state").write_text("", encoding="utf-8") + with starting(self.folder / "state") as failure: + assert isinstance(failure, Failure) + self.assertRegex(failure.reason, f"^cannot lock {self.folder}/state: \\w") + + def test_yields_a_failure_naming_the_lock_it_could_not_open(self) -> None: + (self.folder / LOCK).mkdir() + with starting(self.folder) as failure: + assert isinstance(failure, Failure) + self.assertRegex(failure.reason, f"^cannot lock {self.folder}/{LOCK}: \\w") + + def test_an_error_raised_while_the_lock_is_held_is_the_callers(self) -> None: + with self.assertRaises(OSError), starting(self.folder): + raise OSError("not the lock's") + + +if __name__ == "__main__": + unittest.main() diff --git a/bin/chunk_driver/test_cycle_start.py b/bin/chunk_driver/test_cycle_start.py index 3a4ad967..0358d9f0 100644 --- a/bin/chunk_driver/test_cycle_start.py +++ b/bin/chunk_driver/test_cycle_start.py @@ -1,4 +1,5 @@ import contextlib +import functools import io import json import os @@ -7,7 +8,6 @@ import sys import tempfile import types import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -15,10 +15,11 @@ from chunk_driver.doubles import a_loop from chunk_driver import cycle_start from failure import Failure from loop_effects.doubles import FakeEffects, NOW, STAMP -from git_cli.git_repo import git +from git_cli import git_repo from transcript_archive import outcomes, write from model_routing.ladder import ProjectRung, Rung from model_routing.ladder_file import read_ladder, SHIPPED +from text_files.file_writes import replace_file from model_routing.route import Route, WorkerLine from claude_code import session @@ -28,19 +29,18 @@ OPUS_PROJECT = ProjectRung(read_ladder(SHIPPED), Rung("claude-opus-5-5", "medium class SetupVersion(unittest.TestCase): def test_names_the_release_and_the_commit_or_why_git_cannot_tell(self): with tempfile.TemporaryDirectory() as root: - isolated = {"GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_NOSYSTEM": "1", "GIT_CEILING_DIRECTORIES": root} - with unittest.mock.patch.dict(os.environ, isolated): - gone = cycle_start.setup_version(pathlib.Path(root), git) - self.assertRegex(gone, r"^unknown: [^\n]*not a git repository") - for step in (["init", "-q"], ["-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "one"]): - self.assertIsInstance(git(*step, cwd=root), str) - head = git("rev-parse", "HEAD", cwd=root).strip() - self.assertEqual(cycle_start.setup_version(pathlib.Path(root), git), f"{head[:7]} ({head})") - git("tag", "v0.1.0", cwd=root) - self.assertEqual(cycle_start.setup_version(pathlib.Path(root), git), f"v0.1.0 ({head})") - git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "two", cwd=root) - head = git("rev-parse", "HEAD", cwd=root).strip() - self.assertEqual(cycle_start.setup_version(pathlib.Path(root), git), f"v0.1.0-1-g{head[:7]} ({head})") + git = functools.partial(git_repo.git, env={**os.environ, "GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_NOSYSTEM": "1", "GIT_CEILING_DIRECTORIES": root}) + gone = cycle_start.setup_version(pathlib.Path(root), git) + self.assertRegex(gone, r"^unknown: [^\n]*not a git repository") + for step in (["init", "-q"], ["-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "one"]): + self.assertIsInstance(git(*step, cwd=root), str) + head = git("rev-parse", "HEAD", cwd=root).strip() + self.assertEqual(cycle_start.setup_version(pathlib.Path(root), git), f"{head[:7]} ({head})") + git("tag", "v0.1.0", cwd=root) + self.assertEqual(cycle_start.setup_version(pathlib.Path(root), git), f"v0.1.0 ({head})") + git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "two", cwd=root) + head = git("rev-parse", "HEAD", cwd=root).strip() + self.assertEqual(cycle_start.setup_version(pathlib.Path(root), git), f"v0.1.0-1-g{head[:7]} ({head})") def test_names_why_git_cannot_name_the_commit_on_one_line(self): def fake_git(*args, cwd=None): @@ -79,12 +79,12 @@ class Routes(unittest.TestCase): transcript.write_text(json.dumps({"type": "queue-operation", "timestamp": "1"}) + "\n") self.assertIsNone(cycle_start.archived_route(archive, a_loop(FakeEffects().effects(), session=opened))) with contextlib.redirect_stderr(io.StringIO()): - self.assertIsNone(write.archive(archive, str(transcript), "Do it.", outcomes.unfinished(route.worker_line(), "working", "j", ""), NOW)) + self.assertIsNone(write.archive(archive, str(transcript), "Do it.", outcomes.unfinished(route.worker_line(), "working", "j", ""), NOW, replace=replace_file)) with contextlib.redirect_stderr(stderr): self.assertEqual(cycle_start.archived_route(archive, a_loop(FakeEffects().effects(), session=opened)), route) self.assertEqual(stderr.getvalue(), "") with contextlib.redirect_stderr(io.StringIO()): - self.assertIsNone(write.archive(archive, str(transcript), "Do it.", outcomes.stepped_up(WorkerLine('{"model": "claude-opus-5-5", "rung": 3}')), NOW + 1)) + self.assertIsNone(write.archive(archive, str(transcript), "Do it.", outcomes.stepped_up(WorkerLine('{"model": "claude-opus-5-5", "rung": 3}')), NOW + 1, replace=replace_file)) with contextlib.redirect_stderr(io.StringIO()) as stderr: self.assertIsNone(cycle_start.archived_route(archive, a_loop(FakeEffects().effects(), session=opened))) self.assertEqual(stderr.getvalue(), f'{STAMP} routing the chunk afresh: the archive\'s latest Worker: line names no route that can be read: {{"model": "claude-opus-5-5", "rung": 3}}\n') diff --git a/bin/chunk_pr/approval.py b/bin/chunk_pr/approval.py index 2e6fa2d8..d143f1f9 100644 --- a/bin/chunk_pr/approval.py +++ b/bin/chunk_pr/approval.py @@ -1,5 +1,4 @@ """The human approval that `chunk-pr merge` waits for where the project's config sets `wait-for-approval`.""" -import time import typing from forge.reviews import APPROVED, CHANGES_REQUESTED, github_decision, REVIEW_REQUIRED, waiting_threads @@ -34,7 +33,7 @@ def approval(number: str, run: Run) -> bool | Failure: return decision == APPROVED -def approved(number: str, run: Run, sleep: typing.Callable[[float], None] = time.sleep) -> Failure | None: +def approved(number: str, run: Run, sleep: typing.Callable[[float], None]) -> Failure | None: """None once GitHub reports pull request number approved; a Failure where `approval` fails, where a reviewer's comment waits for an answer, or where no approval came within WAIT_SECONDS.""" for waited in range(0, WAIT_SECONDS + 1, POLL_SECONDS): read = approval(number, run) diff --git a/bin/chunk_pr/main.py b/bin/chunk_pr/main.py index 158011d7..3601d624 100644 --- a/bin/chunk_pr/main.py +++ b/bin/chunk_pr/main.py @@ -139,7 +139,7 @@ def step(argv: list[str], run: Run, config: Config, parks: bool = False) -> str return Failure("give a title of one line") if title and not one_line(title[0]) else (misfit(title[0], run, config) if title else None) or marked_ready(run, title[0] if title else None) case ["merge", *given]: finishes = finishes_of(given) - return Failure(USAGE) if finishes is None else merged(run, config, finishes=finishes, parks=parks) + return Failure(USAGE) if finishes is None else merged(run, config, time.sleep, finishes=finishes, parks=parks) case ["clean"]: return cleaned(run) return Failure(USAGE) @@ -305,7 +305,7 @@ def finishes_of(given: list[str]) -> list[list[str]] | None: return finishes -def merged(run: Run, config: Config, sleep: typing.Callable[[float], None] = time.sleep, finishes: typing.Sequence[list[str]] = (), parks: bool = False) -> str | Failure: +def merged(run: Run, config: Config, sleep: typing.Callable[[float], None], finishes: typing.Sequence[list[str]] = (), parks: bool = False) -> str | Failure: """Merge the branch's pull request in the project's merge queue. Each pass of the loop is one turn in the queue; a pull request that still wants its approval leaves the queue while it waits, so the next chunk merges meanwhile, and takes a new turn once approved. Where parks, it doesn't wait for the approval: it fails at once, as `left_to_park` says.""" found = pull(run) if isinstance(found, Failure): diff --git a/bin/chunk_pr/test_chunk_pr.py b/bin/chunk_pr/test_chunk_pr.py index 6fdfb43d..b4d678e3 100644 --- a/bin/chunk_pr/test_chunk_pr.py +++ b/bin/chunk_pr/test_chunk_pr.py @@ -603,7 +603,7 @@ class MergeQueue(unittest.TestCase): def test_a_branch_behind_the_default_branch_merges_it_in_and_pushes_before_the_checks(self): checkout = self.moved(Checkout(GITHUB, open=True)) - self.assertEqual(merged(checkout.__call__, checkout.config), "Merged pull request #12 (merge).") + self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None), "Merged pull request #12 (merge).") self.assertEqual(changes(checkout), [MERGE, self.PUSHED, ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", "decaf"]]) self.assertLess(checkout.ran.index(self.PUSHED), checkout.ran.index(GH_CHECKS)) @@ -611,7 +611,7 @@ class MergeQueue(unittest.TestCase): checkout = self.moved(Checkout(GITHUB, open=True)) checkout.answers[tuple(MERGE)] = Failure("git merge --quiet: CONFLICT") checkout.answers[("git", "diff", "--name-only", "--diff-filter=U")] = "bin/tool.py\n" - failed = merged(checkout.__call__, checkout.config) + failed = merged(checkout.__call__, checkout.config, lambda seconds: None) self.assertIsInstance(failed, Failure) self.assertIn("conflicts in bin/tool.py; the merge stays in progress: resolve them, commit, push, and run chunk-pr merge again", failed.reason) self.assertEqual((changes(checkout), MERGE in checkout.ran), ([], True)) @@ -620,7 +620,7 @@ class MergeQueue(unittest.TestCase): def test_a_finished_item_is_committed_under_the_configs_title_and_pushed_before_the_merge(self): checkout, folder = self.with_plan(Checkout(GITHUB, open=True)) files = [f"{folder}/todo.md", f"{folder}/CHANGELOG.md"] - self.assertEqual(merged(checkout.__call__, checkout.config, finishes=[["7", "--changelog", "Exports keep their order."]]), "Merged pull request #12 (merge).") + self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7", "--changelog", "Exports keep their order."]]), "Merged pull request #12 (merge).") self.assertEqual(changes(checkout)[:1], [[self.TODO_ITEM, "finish", "7", "--changelog", "Exports keep their order."]]) self.assertEqual(changes(checkout)[-4:], [ ["git", "-C", folder, "add", "--", *files], @@ -637,7 +637,7 @@ class MergeQueue(unittest.TestCase): def test_an_item_the_plan_no_longer_holds_is_named_on_the_last_line_and_what_an_earlier_run_left_uncommitted_is_committed(self): checkout, folder = self.with_plan(Checkout(GITHUB, open=True), held=False) - self.assertEqual(merged(checkout.__call__, checkout.config, finishes=[["7"]]), "Merged pull request #12 (merge). The plan held no item 7 when merge began, so merge left the plan as it was there.") + self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7"]]), "Merged pull request #12 (merge). The plan held no item 7 when merge began, so merge left the plan as it was there.") self.assertNotIn([self.TODO_ITEM, "finish", "7"], checkout.ran) self.assertEqual([command[3] for command in changes(checkout)[:-1]], ["add", "commit", "origin"]) self.assertEqual(changes(checkout)[-1], ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", "f00d"]) @@ -645,7 +645,7 @@ class MergeQueue(unittest.TestCase): def test_each_item_the_plan_no_longer_holds_is_named(self): checkout, _ = self.with_plan(Checkout(GITHUB, open=True), held=False) checkout.answers[(self.TODO_ITEM, "show", "8")] = Failure("todo-item show 8: todo.md holds no item 8") - self.assertIn("The plan held no item 7, 8 when merge began", str(merged(checkout.__call__, checkout.config, finishes=[["7"], ["8"]]))) + self.assertIn("The plan held no item 7, 8 when merge began", str(merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7"], ["8"]]))) def test_a_plan_that_turns_unreadable_inside_the_turn_leaves_it_unmerged(self): checkout, _ = self.with_plan(Checkout(GITHUB, open=True)) @@ -653,7 +653,7 @@ class MergeQueue(unittest.TestCase): def run_whose_second_show_fails(command: list[str]) -> str | Failure: shows.extend([command] if command[1:2] == ["show"] else []) return Failure("todo-item show 7: unreadable") if command[1:2] == ["show"] and len(shows) == 2 else run(command) - self.assertEqual(merged(run_whose_second_show_fails, checkout.config, finishes=[["7"]]), Failure("todo-item show 7: unreadable")) + self.assertEqual(merged(run_whose_second_show_fails, checkout.config, lambda seconds: None, finishes=[["7"]]), Failure("todo-item show 7: unreadable")) self.assertNotIn([self.TODO_ITEM, "finish", "7"], checkout.ran) def test_a_finish_that_changes_no_file_commits_and_pushes_nothing(self): @@ -664,13 +664,13 @@ class MergeQueue(unittest.TestCase): checkout.answers[("git", "-C", folder, "status", "--porcelain", "--", f"{folder}/todo.md", f"{folder}/CHANGELOG.md")] = "\n" else: checkout.answers.update({(self.CONFIG, "plan"): "PAY-1\n", (self.CONFIG, "changelog"): f"{folder}/none.md\n"}) - self.assertEqual(merged(checkout.__call__, checkout.config, finishes=[["7", "--no-changelog"]]), "Merged pull request #12 (merge).") + self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7", "--no-changelog"]]), "Merged pull request #12 (merge).") self.assertEqual(changes(checkout), [[self.TODO_ITEM, "finish", "7", "--no-changelog"], ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]]) def test_a_branch_outside_the_branch_template_titles_the_finish_with_the_summary_alone(self): checkout, folder = self.with_plan(Checkout(GITHUB, open=True)) checkout.answers[tuple(BRANCH)] = "feature/PAY-{id}\n" - merged(checkout.__call__, checkout.config, finishes=[["7"]]) + merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7"]]) self.assertIn("Finish item 7 in the plan", [command[6] for command in checkout.ran if command[3:4] == ["commit"]]) def test_a_plan_it_cannot_read_or_an_item_it_cannot_finish_leaves_it_unmerged(self): @@ -678,7 +678,7 @@ class MergeQueue(unittest.TestCase): with self.subTest(failing): checkout, _ = self.with_plan(Checkout(GITHUB, open=True)) checkout.answers[(self.TODO_ITEM, failing, "7")] = Failure(f"todo-item {failing} 7: no config names the checkout") - self.assertEqual(merged(checkout.__call__, checkout.config, finishes=[["7"]]), Failure(f"todo-item {failing} 7: no config names the checkout")) + self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7"]]), Failure(f"todo-item {failing} 7: no config names the checkout")) self.assertNotIn(self.PUSHED, checkout.ran) def test_a_step_of_the_turn_that_fails_leaves_it_unmerged_with_that_failure(self): @@ -690,23 +690,23 @@ class MergeQueue(unittest.TestCase): def failing_run(command: list[str]) -> str | Failure: asked = ["" if part == folder else part for part in command] return Failure("that step failed") if tuple(asked[:len(failing)]) == tuple(failing) else run(command) - self.assertEqual(merged(failing_run, checkout.config, finishes=[["7"]]), Failure("that step failed")) + self.assertEqual(merged(failing_run, checkout.config, lambda seconds: None, finishes=[["7"]]), Failure("that step failed")) self.assertNotIn("merge", [command[2] for command in checkout.ran if command[0] == "gh"]) def test_a_finish_that_fails_after_the_default_branch_was_merged_in_still_pushes_the_branch(self): checkout, _ = self.with_plan(self.moved(Checkout(GITHUB, open=True))) checkout.answers[(self.TODO_ITEM, "finish", "7")] = Failure("todo-item finish 7: a decision cites item 7") - self.assertEqual(merged(checkout.__call__, checkout.config, finishes=[["7"]]), Failure("todo-item finish 7: a decision cites item 7")) + self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7"]]), Failure("todo-item finish 7: a decision cites item 7")) self.assertEqual(changes(checkout), [MERGE, self.PUSHED]) def test_the_scoring_run_is_asked_for_once_the_finish_is_committed_and_one_owed_leaves_it_unmerged(self): checkout, _ = self.with_plan(Checkout(GITHUB, open=True)) commit = lambda: next(index for index, command in enumerate(checkout.ran) if command[3:4] == ["commit"]) - self.assertEqual(merged(checkout.__call__, checkout.config, finishes=[["7"]]), "Merged pull request #12 (merge).") + self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7"]]), "Merged pull request #12 (merge).") self.assertLess(commit(), checkout.ran.index(OWED)) checkout, _ = self.with_plan(Checkout(GITHUB, open=True)) checkout.answers[tuple(OWED)] = Failure("the chunk owes 1 scoring run") - self.assertEqual(merged(checkout.__call__, checkout.config, finishes=[["7"]]), Failure("the chunk owes 1 scoring run")) + self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None, finishes=[["7"]]), Failure("the chunk owes 1 scoring run")) self.assertEqual(changes(checkout)[-1], self.PUSHED) def test_a_merge_holds_the_queue_through_the_checks_and_frees_it_while_it_waits_for_an_approval(self): @@ -740,7 +740,7 @@ class MergeQueue(unittest.TestCase): checkout = Checkout(GITHUB, open=True) checkout.answers[tuple(WAITS)] = "true\n" checkout.threads = [self.thread("bin/asked.py", "reviewer")] - failed = merged(checkout, checkout.config) + failed = merged(checkout, checkout.config, lambda seconds: None) self.assertIsInstance(failed, Failure) self.assertIn("waits for an answer, in bin/asked.py;", failed.reason) self.assertEqual(changes(checkout), []) @@ -766,7 +766,7 @@ class MergeQueue(unittest.TestCase): with self.subTest(name): checkout = Checkout(GITHUB, open=True) checkout.answers.update({(self.CONFIG, "comprehension-history"): f"{history}\n", **answers}) - done = merged(checkout.__call__, checkout.config) + done = merged(checkout.__call__, checkout.config, lambda seconds: None) self.assertEqual(isinstance(done, Failure) and "run the paired ruling again" in done.reason, stops, done) self.assertEqual(changes(checkout), [] if stops else [["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]]) @@ -774,14 +774,14 @@ class MergeQueue(unittest.TestCase): ruled = ("git", "log", "-1", "--format=%H", "-G", "^Ruling: ", "origin/main..HEAD", "--", "/checkout/history.md") checkout = Checkout(GITHUB, open=True) checkout.answers[(self.CONFIG, "comprehension-history")] = "/elsewhere/history.md\n" - self.assertEqual(merged(checkout.__call__, checkout.config), "Merged pull request #12 (merge).") + self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None), "Merged pull request #12 (merge).") self.assertNotIn("log", [command[1] for command in checkout.ran]) for failing in (ruled, ("git", "rev-list", "--count", "bead..origin/main"), ("git", "rev-parse", "--show-toplevel")): with self.subTest(failing): checkout = Checkout(GITHUB, open=True) checkout.answers.update({(self.CONFIG, "comprehension-history"): "/checkout/history.md\n", ruled: "bead\n"}) checkout.answers[failing] = Failure("git failed") - self.assertEqual(merged(checkout.__call__, checkout.config), Failure("git failed")) + self.assertEqual(merged(checkout.__call__, checkout.config, lambda seconds: None), Failure("git failed")) def test_a_turn_keeps_every_other_merge_out_until_it_ends_even_by_an_error(self): with self.assertRaises(ValueError): @@ -809,7 +809,7 @@ class MergeQueue(unittest.TestCase): def test_a_queue_it_cannot_open_leaves_it_unmerged(self): checkout = Checkout(GITHUB, open=True) checkout.answers[tuple(COMMON)] = "/nonexistent/folder\n" - self.assertEqual(merged(checkout, checkout.config), Failure("cannot take a turn in the merge queue, /nonexistent/folder/chunk-pr-merge-queue: No such file or directory")) + self.assertEqual(merged(checkout, checkout.config, lambda seconds: None), Failure("cannot take a turn in the merge queue, /nonexistent/folder/chunk-pr-merge-queue: No such file or directory")) self.assertEqual(checkout.acted(), []) diff --git a/bin/compress-archives b/bin/compress-archives index 77a2684f..c7b02cc6 100755 --- a/bin/compress-archives +++ b/bin/compress-archives @@ -1,9 +1,15 @@ #!/usr/bin/env python3 +import functools +import gzip +import os import pathlib import sys +import time sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) from compress_archives import main +from text_files.compressed import compress +from text_files.file_writes import replace_file -sys.exit(main.main(sys.argv[1:])) +sys.exit(main.main(sys.argv[1:], os.environ, pathlib.Path.home(), time.time, functools.partial(compress, pack=gzip.compress, replace=replace_file, sync=os.fsync))) diff --git a/bin/compress_archives/main.py b/bin/compress_archives/main.py index 4c61829f..38ba0205 100644 --- a/bin/compress_archives/main.py +++ b/bin/compress_archives/main.py @@ -14,31 +14,28 @@ its plain name. README.md → Install has the crontab line that runs it daily. It prints how many files it compressed. It exits 1, saying why, on any argument but --help or -h, and at the first file it cannot compress, leaving that file and every one after it plain. """ -import os import pathlib import stat import sys -import time import typing from transcript_archive.location import archive_root -from text_files.compressed import compress, SUFFIX from failure import Failure from review_rules.review_folder import REPORTS +from text_files.compressed import SUFFIX # Why 30 days: docs/decisions.md → `archives: gzip each transcript in loop-claude's archive and each file in the review folders 30 days after it last changed, keeping all of them`. AGE_DAYS = 30 -def main(argv: list[str], env: typing.Mapping[str, str] = os.environ, home: pathlib.Path | None = None, now: float | None = None) -> int: +def main(argv: list[str], env: typing.Mapping[str, str], home: pathlib.Path, now: typing.Callable[[], float], compress: typing.Callable[[pathlib.Path], Failure | None]) -> int: if argv in (["-h"], ["--help"]): print(__doc__.strip()) return 0 if argv: print(f"unknown arguments {' '.join(argv)}; usage: compress-archives", file=sys.stderr) return 1 - home = home or pathlib.Path.home() - cutoff = (time.time() if now is None else now) - AGE_DAYS * 24 * 60 * 60 + cutoff = now() - AGE_DAYS * 24 * 60 * 60 count = 0 for path in [*old_files(archive_root(home, env), "*.jsonl", cutoff), *old_files(home / REPORTS, "*", cutoff)]: if failed := compress(path): diff --git a/bin/compress_archives/test_compress_archives.py b/bin/compress_archives/test_compress_archives.py index 83a0fdc1..0ee88655 100644 --- a/bin/compress_archives/test_compress_archives.py +++ b/bin/compress_archives/test_compress_archives.py @@ -11,6 +11,7 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) +from text_files.doubles import compress from failure import Failure from compress_archives import main @@ -43,10 +44,10 @@ class Main(unittest.TestCase): path.write_bytes(f"{path.name}\n".encode()) os.utime(path, (NOW - seconds_ago, NOW - seconds_ago)) - def run_main(self, argv=(), env=None, now=NOW): + def run_main(self, argv=(), env=None, now=NOW, compress=compress): out, err = io.StringIO(), io.StringIO() with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): - code = main.main(list(argv), env or {}, self.home, now) + code = main.main(list(argv), env or {}, self.home, lambda: now, compress) return code, out.getvalue(), err.getvalue() def kept(self): @@ -80,9 +81,8 @@ class Main(unittest.TestCase): self.assertEqual(self.run_main(now=now), (0, "Compressed 0 files.\n", "")) self.assertTrue((self.archive / "a.jsonl").is_file()) - def test_measures_the_30_days_from_the_clock_by_default(self): - with mock.patch.object(main.time, "time", return_value=NOW): - self.assertEqual(self.run_main(now=None), (0, "Compressed 4 files.\n", "")) + def test_measures_the_30_days_from_the_clock_it_is_given(self): + self.assertEqual(self.run_main(now=NOW), (0, "Compressed 4 files.\n", "")) def test_walks_on_past_a_gzip_a_link_and_a_file_removed_while_it_walks(self): gone = mock.Mock(**{"is_symlink.return_value": False, "stat.side_effect": FileNotFoundError}) @@ -109,8 +109,7 @@ class Main(unittest.TestCase): self.assertEqual(self.run_main(), (0, "Compressed 0 files.\n", "")) def test_stops_at_the_first_failure_keeping_that_file_and_every_later_one(self): - with mock.patch.object(main, "compress", return_value=Failure("could not compress x: full")): - code, out, err = self.run_main() + code, out, err = self.run_main(compress=lambda path: Failure("could not compress x: full")) self.assertEqual((code, out, err), (1, "", "could not compress x: full\n")) self.assertTrue((self.archive / "a.jsonl").is_file()) diff --git a/bin/deploy_release/cut.py b/bin/deploy_release/cut.py index b764bfbd..9245079b 100644 --- a/bin/deploy_release/cut.py +++ b/bin/deploy_release/cut.py @@ -6,6 +6,7 @@ import pathlib import shutil import sys import tempfile +import typing from release_plan.release_standing import chunk_item, Cut, default_plan, Next, ReadyToCut, ReviewDue, Unplanned from project_config.settings import DEFAULT_FLOOR @@ -21,7 +22,7 @@ from deploy_release import release_pull from project_config import templates -def cut(clone: str, checkout: str) -> str | Failure: +def cut(clone: str, checkout: str, sleep: typing.Callable[[float], None]) -> str | Failure: """What the cut did, as the tool prints it; a Failure with git's, the forge's or the tool's reason where a step stopped.""" if isinstance(same := same_origin(clone, checkout), Failure): return same @@ -39,7 +40,7 @@ def cut(clone: str, checkout: str) -> str | Failure: title, tag = f"Cut release {named}", f"v{named}" merged = commit_titled(title, ref, clone) if merged is None: - landed = land(clone, ref, named, title) + landed = land(clone, ref, named, title, sleep) if isinstance(landed, Failure): return landed if isinstance(fetched := git("fetch", "--quiet", "origin", cwd=clone), Failure): @@ -149,7 +150,7 @@ def commit_titled(title: str, ref: str, clone: str) -> str | None | Failure: return next((line.split(" ", 1)[0] for line in log.splitlines() if line.split(" ", 1)[1:] == [title]), None) -def land(clone: str, ref: str, named: str, title: str) -> Failure | None: +def land(clone: str, ref: str, named: str, title: str, sleep: typing.Callable[[float], None]) -> Failure | None: """Push the branch release- with the cut commit and open its pull request, where an earlier run hasn't; then wait for its checks and merge it.""" url = git("config", "--get", "remote.origin.url", cwd=clone) if isinstance(url, Failure): @@ -182,7 +183,7 @@ def land(clone: str, ref: str, named: str, title: str) -> Failure | None: if isinstance(required, Failure): return required said(f"waiting for the checks of pull request {number}" + (f"; {base} requires {', '.join(sorted(required))}" if required else "")) - if isinstance(passed := pulls.checks_passed(on, number, head, required, run), Failure): + if isinstance(passed := pulls.checks_passed(on, number, head, required, run, sleep), Failure): return passed said(f"the checks of pull request {number} passed") if isinstance(merged := release_pull.merge(on, number, head, run), Failure): diff --git a/bin/deploy_release/main.py b/bin/deploy_release/main.py index 2742fc9f..c9a827e5 100644 --- a/bin/deploy_release/main.py +++ b/bin/deploy_release/main.py @@ -39,6 +39,7 @@ move stopped, fix what it names and run a bare deploy-release. import os import pathlib import sys +import time from deploy_release.cut import cut from deploy_release.deploy import deploy @@ -55,7 +56,7 @@ def main(argv: list[str]) -> int: print("deploy-release: usage: deploy-release [--cut] []", file=sys.stderr) return 1 checkout = rest[0] if rest else str(pathlib.Path.home() / ".claude") - moved = cut(os.getcwd(), checkout) if cutting else deploy(checkout) + moved = cut(os.getcwd(), checkout, time.sleep) if cutting else deploy(checkout) if isinstance(moved, Failure): print(f"deploy-release: {moved.reason}", file=sys.stderr) return 1 diff --git a/bin/do-epic b/bin/do-epic index 9f0a3e71..0e812de1 100755 --- a/bin/do-epic +++ b/bin/do-epic @@ -10,4 +10,4 @@ from loop_effects import effects from do_epic import main from loop_state import paths -sys.exit(main.main(sys.argv[1:], os.environ, paths.paths_from(pathlib.Path.home()), LAUNCHER, effects.system_effects())) +sys.exit(main.main(sys.argv[1:], os.environ, paths.paths_from(pathlib.Path.home()), LAUNCHER, effects.system_effects(pathlib.Path.home(), os.environ))) diff --git a/bin/do-epic-e2e b/bin/do-epic-e2e index 01856292..16db3ad8 100755 --- a/bin/do-epic-e2e +++ b/bin/do-epic-e2e @@ -6,6 +6,6 @@ import sys LAUNCHER = pathlib.Path(__file__).resolve() sys.path.insert(0, str(LAUNCHER.parent)) -from do_epic.e2e import main +from do_epic.e2e import main, sandbox, scenario -sys.exit(main.main(sys.argv[1:], os.environ, LAUNCHER.parent / "do-epic")) +sys.exit(main.main(sys.argv[1:], os.environ, LAUNCHER.parent / "do-epic", sandbox.epic_made, scenario.run)) diff --git a/bin/do_epic/backend/jira.py b/bin/do_epic/backend/jira.py index 257d07d4..1d49e1ed 100644 --- a/bin/do_epic/backend/jira.py +++ b/bin/do_epic/backend/jira.py @@ -5,7 +5,7 @@ import typing from adf_codec.convert import adfs_to_markdown, markdown_to_adf, Refused from atlassian.client import basic_auth, Http, jira_json, ToAdf, ToMarkdown -from atlassian.jira import DONE, entry_of, finishing_transition, ISSUE_KEY, move_refusal, PERHAPS_FILED, PERHAPS_MOVED, project_of, rank_refusal, searched, unshaped +from atlassian.jira import DONE, entry_of, finishing_transition, ISSUE_KEY, MAX_PAGES, move_refusal, PERHAPS_FILED, PERHAPS_MOVED, project_of, rank_refusal, searched, unshaped from project_config.settings import Epic, Finish, JIRA_FINISH_STATUS, JIRA_FINISH_TRANSITION from do_epic.backend.protocol import EpicIssue, no_child, no_parent, no_words, unfit, unfit_label from failure import Failure @@ -33,6 +33,8 @@ class JiraEpic: to_markdown: ToMarkdown = adfs_to_markdown to_adf: ToAdf = markdown_to_adf finish: Finish | None = None + # The pages a search may take before the read fails. + max_pages: int = MAX_PAGES @property def epic_key(self) -> str: @@ -223,7 +225,7 @@ class JiraEpic: def searched(self, what: str, parents: tuple[str, ...], authorization: str) -> list[Found] | Failure: """The issues whose parent is one of parents, in rank order.""" named = ", ".join(f'"{key}"' for key in parents) - answered = searched(self.epic.site, what, f"parent in ({named}) ORDER BY Rank ASC", {"fields": FIELDS, "properties": [PROPERTY]}, authorization, self.http) + answered = searched(self.epic.site, what, f"parent in ({named}) ORDER BY Rank ASC", {"fields": FIELDS, "properties": [PROPERTY]}, authorization, self.http, self.max_pages) if isinstance(answered, Failure): return answered found = [found_of(raw) for raw in answered] diff --git a/bin/do_epic/backend/test_jira.py b/bin/do_epic/backend/test_jira.py index d8e7b8b5..e516ab8a 100644 --- a/bin/do_epic/backend/test_jira.py +++ b/bin/do_epic/backend/test_jira.py @@ -5,7 +5,6 @@ import re import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) @@ -329,12 +328,13 @@ class Jira(Case): ({"key": "abc-34", "fields": valid}, "the search answered an issue without an issue key"), ({"key": "ABC-34", "fields": {"summary": "Count the runs.", "parent": {"key": "ABC-12"}, "issuelinks": []}}, shape + ": KeyError('labels')"), *((({"key": "ABC-34", "fields": valid | changed}, shape + ": its summary, labels, assignee, status, parent, description or a linked issue's key holds another kind of value") for changed in ( - {"summary": 3}, {"labels": "api"}, {"labels": [3]}, {"assignee": {"displayName": 0}}, {"status": {"name": 0, "statusCategory": {"key": "new"}}}, {"parent": {"key": "ABC"}}, {"parent": {"key": 12}}, {"description": "text"}, {"issuelinks": [linked | {"inwardIssue": {"key": 7}}]}, {"issuelinks": [linked | {"inwardIssue": {"key": "ABC-x"}}]}, + {"summary": 3}, {"labels": "api"}, {"labels": [3]}, {"assignee": {"displayName": 0}}, {"status": {"name": 0, "statusCategory": {"key": "new"}}}, {"parent": {"key": "ABC"}}, {"parent": {"key": 12}}, {"description": "text"}, {"issuelinks": [linked | {"inwardIssue": {"key": "ABC-x"}}]}, ))), - ({"key": "ABC-34", "fields": {key: value for key, value in valid.items() if key != "issuelinks"}}, shape + ": KeyError('issuelinks')"), + ({"key": "ABC-34", "fields": {key: value for key, value in valid.items() if key != "issuelinks"}}, shape + ": its issuelinks are no list"), ({"key": "ABC-34", "fields": valid, "properties": {"plan-item": "L 2"}}, "ABC-34's issue property plan-item is not a JSON object"), ({"key": "ABC-34", "fields": valid, "properties": {"plan-item": {"L": "many"}}}, "item ABC-34's L 'many' is not a whole number from 1 to 4; ABC-34 holds it in its issue property plan-item"), - ({"key": "ABC-34", "fields": valid, "properties": None}, shape + ": AttributeError(\"'NoneType' object has no attribute 'get'\")"), + ({"key": "ABC-34", "fields": valid, "properties": None}, shape + ": its properties are no object"), + ({"key": "ABC-34", "fields": valid | {"issuelinks": [linked | {"inwardIssue": {"key": 7}}]}}, shape + ": its issuelinks hold a link whose id or whose linked issue's key is no text"), ): with self.subTest(raw=raw): self.jira.answers[("POST", SEARCH)] = {"issues": [raw]} @@ -347,14 +347,13 @@ class Jira(Case): self.assertEqual(JiraEpic(EPIC, self.env_file, self.jira, lambda documents: converted, to_adf).read(), Failure(reason)) def test_a_search_that_does_not_end_names_the_issues_it_sought(self) -> None: - store = self.new_store() + store = dataclasses.replace(self.new_store(), max_pages=1) children = [store.file("ABC-12", "Count the runs.", "", ()) for _ in range(2)] for _ in range(3): store.file(str(children[0]), "A task.", "", ()) - with unittest.mock.patch("atlassian.jira.MAX_PAGES", 1): - self.assertEqual(store.read(), Failure("the search for the sub-tasks of ABC-12's child issues did not end within 1 pages")) - store.file("ABC-12", "Count the runs.", "", ()) - self.assertEqual(store.read(), Failure("the search for ABC-12's child issues did not end within 1 pages")) + self.assertEqual(store.read(), Failure("the search for the sub-tasks of ABC-12's child issues did not end within 1 pages")) + store.file("ABC-12", "Count the runs.", "", ()) + self.assertEqual(store.read(), Failure("the search for ABC-12's child issues did not end within 1 pages")) def test_refuses_to_file_under_another_epics_child_or_an_issue_it_cannot_read(self) -> None: self.jira.issues.append({"key": "ABC-5", "fields": {"parent": {"key": "ABC-1"}, "status": TO_DO}}) diff --git a/bin/do_epic/e2e/main.py b/bin/do_epic/e2e/main.py index 1f568252..2d7592a1 100644 --- a/bin/do_epic/e2e/main.py +++ b/bin/do_epic/e2e/main.py @@ -13,8 +13,12 @@ It reads three environment variables and fails, naming each that is missing: {chr(10).join(f' {name}: {meaning}' for name, meaning in settings.MEANING.items())} It prints a line per check, and exits 0 only where every check held.""" +EpicMade = typing.Callable[[settings.Sandbox, str], typing.ContextManager[str | Failure]] +Run = typing.Callable[[settings.Sandbox, str, pathlib.Path, typing.Mapping[str, str]], scenario.Report] -def main(argv: list[str], env: typing.Mapping[str, str], launcher: pathlib.Path) -> int: + +def main(argv: list[str], env: typing.Mapping[str, str], launcher: pathlib.Path, epic_made: EpicMade, run: Run) -> int: + """Runs do-epic-e2e: epic_made makes the sandbox epic, as sandbox.epic_made does, and run drives do-epic against it, as scenario.run does.""" if argv: print(HELP, file=sys.stdout if argv in (["-h"], ["--help"]) else sys.stderr) return 0 if argv in (["-h"], ["--help"]) else 1 @@ -25,12 +29,12 @@ def main(argv: list[str], env: typing.Mapping[str, str], launcher: pathlib.Path) report = scenario.Report() left: sandbox.Leftover | None = None try: - with sandbox.epic_made(found, "do-epic e2e sandbox epic") as key: + with epic_made(found, "do-epic e2e sandbox epic") as key: if isinstance(key, Failure): print(f"do-epic-e2e: {key.reason}", file=sys.stderr) return 1 print(f"Sandbox epic {key} in {found.jira_url}") - report = scenario.run(found, key, launcher, env) + report = run(found, key, launcher, env) except sandbox.Leftover as failure: left = failure # The checks are printed even where the epic could not be deleted. diff --git a/bin/do_epic/e2e/test_main.py b/bin/do_epic/e2e/test_main.py index 4cd12dcf..e047b32b 100644 --- a/bin/do_epic/e2e/test_main.py +++ b/bin/do_epic/e2e/test_main.py @@ -1,6 +1,5 @@ import contextlib import io -from unittest import mock import pathlib import sys import tempfile @@ -18,7 +17,7 @@ class MainTest(unittest.TestCase): def run_main(self, argv: list[str]) -> tuple[int, str, str]: out, err = io.StringIO(), io.StringIO() with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): - code = main.main(argv, {}, pathlib.Path("do-epic")) + code = main.main(argv, {}, pathlib.Path("do-epic"), sandbox.epic_made, scenario.run) return code, out.getvalue(), err.getvalue() def test_fails_naming_each_missing_setting_before_it_touches_anything(self) -> None: @@ -56,10 +55,14 @@ class ReportedTest(unittest.TestCase): out, err = io.StringIO(), io.StringIO() report = report or scenario.Report(["a check held"], ["a check broke"]) - with mock.patch.object(sandbox, "epic_made", epic_made), mock.patch.object(scenario, "run", return_value=report) as ran: - with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): - code = main.main([], self.env, pathlib.Path("do-epic")) - self.ran_with = ran.call_args.args if ran.called else None + self.ran_with = None + + def ran(*args: object) -> scenario.Report: + self.ran_with = args + return report + + with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): + code = main.main([], self.env, pathlib.Path("do-epic"), epic_made, ran) return code, out.getvalue(), err.getvalue() def test_prints_each_check_and_fails_where_one_broke(self) -> None: diff --git a/bin/do_epic/main.py b/bin/do_epic/main.py index adda3270..f7f21ef9 100644 --- a/bin/do_epic/main.py +++ b/bin/do_epic/main.py @@ -251,6 +251,8 @@ def chunk_run(key: str, epic: EpicRun) -> Stop: if isinstance(place, Stop): return place root, repo = place + if unmade := folder_made(root): + return Stop(1, unmade.reason) session = session_named(f"A {epic.key} do-epic {key}", epic.paths.claude_code_transcripts) loop = Loop(root=root, session=session, window=tmux.Window(epic.session, key, pane), paths=epic.paths, env=epic.env, launcher=epic.launcher, effects=epic.effects) run = ChunkRun() @@ -263,6 +265,9 @@ def chunk_run(key: str, epic: EpicRun) -> Stop: cycle = next_cycle(run, loop, repo is not None, epic, news) if isinstance(cycle, Stop): return cycle + if isinstance(cycle, EpicReopened): + say(f"the epic {epic.key} holds an open issue again, so its finishing chunk ends until none is open", epic.effects, stdout=True) + return handed_on(key, epic) run = dataclasses.replace(run, task=cycle.task) concluded = run_cycle(cycle) if isinstance(concluded, Stop): @@ -355,15 +360,22 @@ def chunk_root(key: str, issue: EpicIssue | None, epic: EpicRun) -> tuple[str, s if isinstance(pick, Failure): return Stop(1, pick.reason) if isinstance(pick, ClaudeWork): - folder = epic.paths.home / scheduler.STATE / epic.key - folder.mkdir(parents=True, exist_ok=True) - return str(folder), None + return str(epic.paths.home / scheduler.STATE / epic.key), None if not isinstance(pick, Repo): return Stop(1, f"{key} is no chunk: no label names its repo") found = locate(pick.name, [], epic.config, epic.paths.home, epic.effects.git) return Stop(1, found.reason) if isinstance(found, Failure) else (str(real(found.checkout)), pick.name) +def folder_made(root: str) -> Failure | None: + """Makes the folder a chunk works in where it is missing, as the epic's folder is before its first chunk without a repo.""" + try: + pathlib.Path(root).mkdir(parents=True, exist_ok=True) + except OSError as failure: + return Failure(f"could not make the chunk's folder {root}: {failure.strerror}") + return None + + def handed_on(key: str, epic: EpicRun) -> Stop: """The stop of a do-epic whose chunk finished. It first moves the chunk's issue to done (the epic's finishing has none), and its split item too where that was the last of the item's sub-tasks, so that no finished chunk starts again and what they block may start, then starts each chunk that may start now. Its window closes as it exits.""" # The epic's finishing has no issue to move: the epic is closed by `--close-epic`. @@ -396,8 +408,8 @@ def split_items_closed(epic: EpicRun, issues: list[EpicIssue]) -> list[EpicIssue return issues -def next_cycle(run: ChunkRun, loop: Loop, in_repo: bool, epic: EpicRun, news: str = "") -> Cycle | Stop: - """The cycle run runs next, from the issue, the premises, the records, the models and the routing ladder read afresh; the stop where one of them cannot be read. news is what `parked_through` tells the worker of a chunk that was parked, and opens its prompt. A chunk in a repo routes on that repo's model and ladder, and one without a repo on the machine setting `repoless-model` and the shipped ladder.""" +def next_cycle(run: ChunkRun, loop: Loop, in_repo: bool, epic: EpicRun, news: str = "") -> Cycle | EpicReopened | Stop: + """The cycle run runs next, from the issue, the premises, the records, the models and the routing ladder read afresh; the stop where one of them cannot be read, and EpicReopened where the epic's finishing has no cycle to run. news is what `parked_through` tells the worker of a chunk that was parked, and opens its prompt. A chunk in a repo routes on that repo's model and ladder, and one without a repo on the machine setting `repoless-model` and the shipped ladder.""" premises = premises_read(loop.root if in_repo else None, epic) if isinstance(premises, Failure): return Stop(1, premises.reason) @@ -408,7 +420,7 @@ def next_cycle(run: ChunkRun, loop: Loop, in_repo: bool, epic: EpicRun, news: st if isinstance(records, Failure): return Stop(1, records.reason) work = chunk_work(loop.window.name, loop.root if in_repo else None, language or epic.config.language, epic) - if isinstance(work, Stop): + if isinstance(work, (Stop, EpicReopened)): return work task, issue = work routing = project_routing(loop, chunk_prompt.DRIVER) if in_repo else routing_on(repoless_models(epic), SHIPPED) @@ -420,8 +432,13 @@ def next_cycle(run: ChunkRun, loop: Loop, in_repo: bool, epic: EpicRun, news: st return cycle_of(loop, routing, route, archive_folder, task=task, facts="", premises=premises, records=records, unfinished_evidence=run.unfinished_evidence, prompts=prompts, taken_in=lambda entries: TakenIn()) -def chunk_work(key: str, checkout: str | None, language: str, epic: EpicRun) -> tuple[str, RoutingIssue | None] | Stop: - """The task of the chunk called key, and the issue that routes it, or None where that issue has no scores: both from the epic's issues, read afresh. The epic's finishing has no issue, and it ends here once the epic holds an open issue again, since working that issue comes first.""" +@dataclasses.dataclass(frozen=True) +class EpicReopened: + """The epic holds an open issue again, so its finishing chunk has no work until none is open: working that issue comes first.""" + + +def chunk_work(key: str, checkout: str | None, language: str, epic: EpicRun) -> tuple[str, RoutingIssue | None] | EpicReopened | Stop: + """The task of the chunk called key, and the issue that routes it, or None where that issue has no scores: both from the epic's issues, read afresh. The epic's finishing has no issue, and its work is EpicReopened once the epic holds an open issue again.""" if key != epic.key: issue = issue_read(key, epic) if isinstance(issue, Stop): @@ -434,8 +451,7 @@ def chunk_work(key: str, checkout: str | None, language: str, epic: EpicRun) -> if isinstance(issues, Failure): return Stop(1, issues.reason) if not finish.due(issues): - say(f"the epic {epic.key} holds an open issue again, so its finishing chunk ends until none is open", epic.effects, stdout=True) - return handed_on(key, epic) + return EpicReopened() return finish.task(epic.key, issues, epic.launcher, language), None diff --git a/bin/do_epic/scheduler.py b/bin/do_epic/scheduler.py index 8f7b7d90..e2a8337c 100644 --- a/bin/do_epic/scheduler.py +++ b/bin/do_epic/scheduler.py @@ -140,45 +140,44 @@ def start_chunks(epic: Epic, issues: list[EpicIssue], finishing: bool, own: str parked = parked_chunks(epic.folder) if isinstance(parked, Failure): return parked - try: - with starting(epic.folder): - windows = tmux.listed(session, run) - live = {window.name: window for window in windows if not window.dead and window.name != own} - running = {window.name for window in windows if not window.dead and not window.parked} - {own} - stopped = {window.name for window in windows if window.dead} - set(live) - # The windows this call started that take a running slot. - taken = 0 - for chunk in found: - if chunk.issue in running or chunk.issue == own: - continue - slot = chunk.issue not in parked and not chunk.human - if chunk.human and chunk.issue in live: - waits.append(Waits(chunk.issue, HUMAN_WAIT)) - elif chunk.issue in parked and chunk.issue in live: - waits.append(Waits(chunk.issue, parked[chunk.issue].waits(live[chunk.issue].awaits))) - elif own and (chunk.issue in stopped or chunk.issue in parked): - waits.append(Waits(chunk.issue, f"its window stopped; run `do-epic {epic.key}` to start it again")) - elif slot and len(running) + taken >= epic.cap: - waits.append(Waits(chunk.issue, f"the epic runs as many chunks as its `{CAP}` line allows: {epic.cap}")) + with starting(epic.folder) as unlocked: + if unlocked: + return unlocked + windows = tmux.listed(session, run) + live = {window.name: window for window in windows if not window.dead and window.name != own} + running = {window.name for window in windows if not window.dead and not window.parked} - {own} + stopped = {window.name for window in windows if window.dead} - set(live) + # The windows this call started that take a running slot. + taken = 0 + for chunk in found: + if chunk.issue in running or chunk.issue == own: + continue + slot = chunk.issue not in parked and not chunk.human + if chunk.human and chunk.issue in live: + waits.append(Waits(chunk.issue, HUMAN_WAIT)) + elif chunk.issue in parked and chunk.issue in live: + waits.append(Waits(chunk.issue, parked[chunk.issue].waits(live[chunk.issue].awaits))) + elif own and (chunk.issue in stopped or chunk.issue in parked): + waits.append(Waits(chunk.issue, f"its window stopped; run `do-epic {epic.key}` to start it again")) + elif slot and len(running) + taken >= epic.cap: + waits.append(Waits(chunk.issue, f"the epic runs as many chunks as its `{CAP}` line allows: {epic.cap}")) + else: + now = start_chunk(epic, chunk if chunk.issue not in parked else Chunk(chunk.issue, None), own, windows, run) + if isinstance(now, Failure): + return now + if isinstance(now, str): + waits.append(Waits(chunk.issue, now)) else: - now = start_chunk(epic, chunk if chunk.issue not in parked else Chunk(chunk.issue, None), own, windows, run) - if isinstance(now, Failure): - return now - if isinstance(now, str): - waits.append(Waits(chunk.issue, now)) - else: - windows = now - started.append(chunk.issue) - taken += slot - except OSError as failure: - return Failure(f"cannot lock {failure.filename or epic.folder / LOCK}: {failure.strerror}") + windows = now + started.append(chunk.issue) + taken += slot # The epic's finishing is no issue: its wait is listed after every issue's. order = [*(issue.key for issue in issues), epic.key] return Started(started, sorted(waits, key=lambda wait: order.index(wait.issue))) def start_chunk(epic: Epic, chunk: Chunk, own: str | None, windows: list[tmux.Listed], run: tmux.Tmux) -> list[tmux.Listed] | str | Failure: - """Starts chunk's window beside windows, the epic's session's, and returns the session's windows afterwards; the reason where its repo takes no chunk now, and a Failure where tmux started none. It holds the repo's lock from the check to the start. Raises OSError where that lock cannot be taken. A chunk without a repo starts in the epic's folder. So does a parked chunk, since it takes no room in its repo.""" + """Starts chunk's window beside windows, the epic's session's, and returns the session's windows afterwards; the reason where its repo takes no chunk now, and a Failure where tmux started none, or where the repo's lock cannot be taken. It holds that lock from the check to the start. A chunk without a repo starts in the epic's folder. So does a parked chunk, since it takes no room in its repo.""" session = tmux.session_name(tmux.EPIC, epic.key) handed = {**{name: value for name, value in epic.env.items() if name.startswith(HANDED)}, PARKS: "1"} command = [str(epic.launcher), CHUNK_FLAG, chunk.issue, epic.key] @@ -188,7 +187,9 @@ def start_chunk(epic: Epic, chunk: Chunk, own: str | None, windows: list[tmux.Li if isinstance(located, Failure): return located.reason checkout, limit = located - with starting(project_lock(str(checkout), epic.home)): + with starting(project_lock(str(checkout), epic.home)) as unlocked: + if unlocked: + return unlocked if reason := refusal(chunk.repo, room_in(str(checkout), limit, (session, own), epic.home, run)): return reason return tmux.start_chunk(tmux.Window(session, chunk.issue, ""), str(checkout), handed, command, windows, False, run) @@ -202,19 +203,20 @@ def room_in(checkout: str, limit: int, leaving: tuple[str, str | None], home: pa def resume(epic: Epic, repo: str, window: tmux.Window, run: tmux.Tmux) -> str | None: """Takes a running slot for the parked chunk in window, whose worker has something to do again in the repo called repo: None once the window counts as a running chunk, else why the chunk still waits. The limits are those a starting chunk meets, checked under the same locks. The window itself never counts against them: one started again for a parked chunk, as after a reboot, isn't named as parked until its first read of the gate.""" - try: - with starting(epic.folder): - running = [listed for listed in tmux.listed(window.session, run) if not listed.dead and not listed.parked and listed.name != window.name] - if len(running) >= epic.cap: - return f"the epic runs as many chunks as its `{CAP}` line allows: {epic.cap}" - located = epic.locate(repo) - if isinstance(located, Failure): - return located.reason - checkout, limit = located - with starting(project_lock(str(checkout), epic.home)): - if reason := refusal(repo, room_in(str(checkout), limit, (window.session, window.name), epic.home, run)): - return reason - tmux.unpark(window, str(checkout), run) - return None - except OSError as failure: - return f"cannot lock {failure.filename or epic.folder / LOCK}: {failure.strerror}" + with starting(epic.folder) as unlocked: + if unlocked: + return unlocked.reason + running = [listed for listed in tmux.listed(window.session, run) if not listed.dead and not listed.parked and listed.name != window.name] + if len(running) >= epic.cap: + return f"the epic runs as many chunks as its `{CAP}` line allows: {epic.cap}" + located = epic.locate(repo) + if isinstance(located, Failure): + return located.reason + checkout, limit = located + with starting(project_lock(str(checkout), epic.home)) as unlocked: + if unlocked: + return unlocked.reason + if reason := refusal(repo, room_in(str(checkout), limit, (window.session, window.name), epic.home, run)): + return reason + tmux.unpark(window, str(checkout), run) + return None diff --git a/bin/do_epic/test_finish.py b/bin/do_epic/test_finish.py index 18f75bb5..9e90405b 100644 --- a/bin/do_epic/test_finish.py +++ b/bin/do_epic/test_finish.py @@ -233,3 +233,11 @@ class TheFinishingChunk(DoEpicTest): self.assertIn("the epic ABC-12 holds an open issue again, so its finishing chunk ends until none is open\n", out) self.assertIn("the chunk finished; started ABC-31\n", out) self.assertEqual([seam for seam, _ in fake.calls if seam == "stream_claude"], []) + + def test_asking_for_the_finishing_chunks_work_names_a_reopened_epic_and_doesnt_start_any_window(self) -> None: + self.issues = [*REVIEWED, issue("ABC-31", "do-epic-claude")] + fake = FakeEffects(windows="ABC-12\t0\t@9\n") + out = io.StringIO() + with contextlib.redirect_stdout(out): + self.assertEqual(main.chunk_work("ABC-12", None, "English", self.epic_run(fake)), main.EpicReopened()) + self.assertEqual((out.getvalue(), fake.tmux_calls), ("", [])) diff --git a/bin/do_epic/test_main.py b/bin/do_epic/test_main.py index a983c4b0..76a1f4df 100644 --- a/bin/do_epic/test_main.py +++ b/bin/do_epic/test_main.py @@ -179,6 +179,20 @@ class TheCommand(DoEpicTest): class ACycleOfAChunkWithoutARepo(DoEpicTest): + def test_naming_the_chunks_folder_doesnt_make_it(self) -> None: + folder = self.home / ".local/state/do-epic/ABC-12" + self.assertEqual(main.chunk_root("ABC-21", self.issues[1], self.epic_run(FakeEffects())), (str(folder), None)) + self.assertFalse(folder.exists()) + self.assertIsNone(main.folder_made(str(folder))) + self.assertTrue(folder.is_dir()) + + def test_a_chunk_whose_folder_cannot_be_made_stops_with_the_reason(self) -> None: + (self.home / ".local/state").mkdir(parents=True, exist_ok=True) + (self.home / ".local/state/do-epic").write_text("", encoding="utf-8") + code, _, err = self.run_main(["--chunk", "ABC-21", "ABC-12"], FakeEffects(), {"TMUX_PANE": "%1"}) + self.assertEqual(code, 1) + self.assertRegex(err, f"could not make the chunk's folder {self.home}/.local/state/do-epic/ABC-12: \\w") + def cycle(self, env: dict[str, str] | None = None) -> main.Cycle: fake = FakeEffects(git=[Failure("no git"), Failure("no git")]) paths = paths_from(self.home) @@ -186,6 +200,7 @@ class ACycleOfAChunkWithoutARepo(DoEpicTest): place = main.chunk_root("ABC-21", self.issues[1], epic) assert isinstance(place, tuple) root, repo = place + self.assertIsNone(main.folder_made(root)) loop = Loop(root=root, session=main.session_named("A ABC-12 do-epic ABC-21", paths.claude_code_transcripts), window=tmux.Window("epic-ABC-12", "ABC-21", "%1"), paths=paths, env=env or {}, launcher=pathlib.Path("/b/do-epic"), effects=fake.effects()) with contextlib.redirect_stdout(io.StringIO()): found = main.next_cycle(ChunkRun(1), loop, repo is not None, epic) diff --git a/bin/do_epic/test_repos.py b/bin/do_epic/test_repos.py index fdd8d66e..1f59e1f7 100644 --- a/bin/do_epic/test_repos.py +++ b/bin/do_epic/test_repos.py @@ -2,10 +2,10 @@ import pathlib import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) +from test_env import assert_not_root from do_epic.config import DoEpicConfig from failure import Failure from do_epic.repos import locate, Located, Unsure @@ -14,9 +14,9 @@ from project_config.doubles import write_config class FakeGit: - """git as locate calls it: origins maps a checkout to its origin URL, cloned holds each `clone` that worked, and refused names a URL whose clone fails after it made the checkout's `.git`, as a stopped git does.""" - def __init__(self, origins: dict[str, str | Failure], refused: str | None = None) -> None: - self.origins, self.refused = origins, refused + """git as locate calls it: origins maps a checkout to its origin URL, cloned holds each `clone` that worked, and refused names a URL whose clone fails after it made the checkout's `.git`, as a stopped git does. Where locked, that clone also takes away the right to remove what it made.""" + def __init__(self, origins: dict[str, str | Failure], refused: str | None = None, locked: bool = False) -> None: + self.origins, self.refused, self.locked = origins, refused, locked self.cloned: list[tuple[str, str]] = [] self.clone_timeouts: list[float | None] = [] self.clone_options: list[tuple[str, ...]] = [] @@ -30,6 +30,8 @@ class FakeGit: url, destination = args[clone + 2:clone + 4] pathlib.Path(destination, ".git").mkdir(parents=True) if url == self.refused: + if self.locked: + pathlib.Path(destination).chmod(0o500) return Failure("git clone: repository not found") self.cloned.append((url, destination)) self.clone_timeouts.append(timeout) @@ -110,9 +112,12 @@ class LocateTest(unittest.TestCase): self.assertIn(f"{self.billing} has an origin, billing, with no host and owner to put another repo under", self.unsure(FakeGit(self.origins))) def test_asks_all_the_same_where_the_leftovers_of_a_failed_clone_cannot_be_removed(self) -> None: + assert_not_root(self) self.configure() - with unittest.mock.patch("do_epic.repos.shutil.rmtree", side_effect=lambda path, ignore_errors: None if ignore_errors is True else self.fail("rmtree must ignore errors")): - self.assertIn("cannot clone", self.unsure(FakeGit(self.origins, refused="git@github.com:acme/ledger"))) + left = self.home / "code" / "ledger" + self.addCleanup(lambda: left.chmod(0o700)) + self.assertIn("cannot clone", self.unsure(FakeGit(self.origins, refused="git@github.com:acme/ledger", locked=True))) + self.assertTrue((left / ".git").is_dir()) def test_asks_where_the_clone_fails_naming_the_url_and_the_reason(self) -> None: self.configure() diff --git a/bin/do_epic/test_scheduler.py b/bin/do_epic/test_scheduler.py index 34d8424e..c255053b 100644 --- a/bin/do_epic/test_scheduler.py +++ b/bin/do_epic/test_scheduler.py @@ -315,6 +315,22 @@ class StartingAnEpicsChunks(unittest.TestCase): assert isinstance(found, Failure) self.assertRegex(found.reason, f"^cannot lock {self.home}/.local/state/do-epic/ABC-12: \\w") + def test_says_which_repos_lock_it_could_not_take(self) -> None: + (self.home / ".local/state").mkdir(parents=True) + (self.home / ".local/state/loop-claude").write_text("", encoding="utf-8") + found = start_chunks(self.epic(1, api=1), [issue("ABC-20", "repo:api")], False, None, Tmux()) + assert isinstance(found, Failure) + self.assertRegex(found.reason, f"^cannot lock {self.home}/.local/state/loop-claude/\\S+: \\w") + self.assertRegex(self.resumed(self.epic(1, api=1), Tmux()) or "", f"^cannot lock {self.home}/.local/state/loop-claude/\\S+: \\w") + + def test_an_error_raised_under_the_lock_is_not_reported_as_the_locks(self) -> None: + def failing(args: list[str]) -> subprocess.CompletedProcess[str]: + raise OSError("tmux is gone") + with self.assertRaisesRegex(OSError, "tmux is gone"): + start_chunks(self.epic(1, api=1), [issue("ABC-20", "repo:api")], False, None, failing) + with self.assertRaisesRegex(OSError, "tmux is gone"): + resume(self.epic(1, api=1), "api", tmux.Window("epic-ABC-12", "ABC-20", "%0"), failing) + CONFIG = settings.ProjectConfig("api", pathlib.Path("/c/api"), pathlib.Path("/code/api"), {"changelog": pathlib.Path("/code/api/CHANGELOG.md")}, epic=settings.Epic("https://acme.atlassian.net", "API-1")) diff --git a/bin/forge/pulls.py b/bin/forge/pulls.py index 43f16ed6..42b1bd31 100644 --- a/bin/forge/pulls.py +++ b/bin/forge/pulls.py @@ -2,7 +2,6 @@ import dataclasses import fnmatch import json -import time import typing from failure import Failure @@ -104,7 +103,7 @@ def checks_now(forge: Forge, number: str, head: str, required: set[str], run: Ru return None if (count == 0 and settled or state == "success") and not missing else Pending(missing) -def checks_passed(forge: Forge, number: str, head: str, required: set[str], run: Run, sleep: typing.Callable[[float], None] = time.sleep, settle: int = 0) -> Failure | None: +def checks_passed(forge: Forge, number: str, head: str, required: set[str], run: Run, sleep: typing.Callable[[float], None], settle: int = 0) -> Failure | None: """None once every check on pull request number, whose head commit is head, has passed, or where it has none for settle seconds; a Failure where one fails, or where they have not all passed within WAIT_SECONDS. checks_now says what counts as passed on Gitea.""" if forge == "gh": for waited in range(0, settle + 1, POLL_SECONDS): diff --git a/bin/forge/reviews.py b/bin/forge/reviews.py index f5487011..331d811b 100644 --- a/bin/forge/reviews.py +++ b/bin/forge/reviews.py @@ -1,6 +1,6 @@ """A pull request's review state, read once from its forge: the review decision, whether it is a draft, whether the worker answered the changes a reviewer requested, and the review threads that wait for an answer. chunk-pr and do-epic decide on what this reads, and run no review query of their own.""" +import dataclasses import json -import typing from failure import Failure from forge.commands import Forge, gitea, Run @@ -42,28 +42,64 @@ def answered_decision(pull: str, head: str, run: Run) -> str | None | Failure: viewed = run(["gh", "pr", "view", pull, "--json", "isDraft,reviewDecision,author,reviews,comments"]) if isinstance(viewed, Failure): return viewed + view = pull_view(viewed) + if isinstance(view, Failure): + return view + if view.draft: + return None + return ANSWERED if view.decision == CHANGES_REQUESTED and answered(view, head) else view.decision + + +@dataclasses.dataclass(frozen=True) +class Review: + """One review of a pull request. GitHub lists a reply in a thread as a review of its own.""" + author: str + state: str + # Empty for a review its author has yet to submit. + submitted_at: str + # The commit a review that requested changes was made on; empty for any other review. + commit: str + + +@dataclasses.dataclass(frozen=True) +class PullView: + """A pull request as `gh pr view --json isDraft,reviewDecision,author,reviews,comments` prints it.""" + draft: bool + # Empty where its branch protection asks for no review. + decision: str + reviews: tuple[Review, ...] + # When its author submitted each review and made each comment. A review the author has yet to submit has no time: it is empty, and answers nobody. + said: tuple[str, ...] + + +def text(value: object) -> str: + """value, which a forge's answer must hold as a string. Raises TypeError where it is none.""" + if not isinstance(value, str): + raise TypeError(f"{value!r} is not a string") + return value + + +def pull_view(printed: str) -> PullView | Failure: + """The pull request that gh printed, with each field checked here, so what decides on it meets no other shape.""" try: - parsed = json.loads(viewed) - if parsed["isDraft"]: - return None - decision = str(parsed["reviewDecision"] or "") - return ANSWERED if decision == CHANGES_REQUESTED and answered(parsed, head) else decision + pull = json.loads(printed) + author = text(pull["author"]["login"]) + reviews = tuple(Review(text(review["author"]["login"]), text(review["state"]), text(review["submittedAt"] or ""), text(review["commit"]["oid"]) if review["state"] == CHANGES_REQUESTED else "") for review in pull["reviews"]) + commented = tuple(text(comment["createdAt"]) for comment in pull["comments"] if text(comment["author"]["login"]) == author) + return PullView(bool(pull["isDraft"]), text(pull["reviewDecision"] or ""), reviews, tuple(review.submitted_at for review in reviews if review.author == author) + commented) except (ValueError, KeyError, TypeError): - return Failure(f"gh pr view printed no review decision: {viewed.strip()[:200]}") + return Failure(f"gh pr view printed no review decision: {printed.strip()[:200]}") -def answered(pull: dict[str, typing.Any], head: str) -> bool: - """Whether the worker answered the newest review that requested changes on pull, as `gh pr view --json author,reviews,comments` prints it, whose head commit is head: the head is no longer the commit that review was made on, or the pull request's author commented or replied in a thread after it. GitHub lists a reply in a thread as a review of its own. False where it lists no review that requested changes. Raises KeyError or TypeError where pull has another shape. +def answered(pull: PullView, head: str) -> bool: + """Whether the worker answered the newest review that requested changes on pull, whose head commit is head: the head is no longer the commit that review was made on, or the pull request's author commented or replied in a thread after it. False where pull lists no review that requested changes. The review names its commit, so no clock tells whether the worker pushed: a commit's date says when it was made, never when it was pushed. """ - asked = max((review for review in pull["reviews"] if review["state"] == CHANGES_REQUESTED), key=lambda review: review["submittedAt"], default=None) + asked = max((review for review in pull.reviews if review.state == CHANGES_REQUESTED), key=lambda review: review.submitted_at, default=None) if asked is None: return False - author = pull["author"]["login"] - # A review the author has yet to submit has no time, and answers nobody. - said = [review["submittedAt"] or "" for review in pull["reviews"] if review["author"]["login"] == author] + [comment["createdAt"] for comment in pull["comments"] if comment["author"]["login"] == author] - return asked["commit"]["oid"] != head or any(at > asked["submittedAt"] for at in said) + return asked.commit != head or any(at > asked.submitted_at for at in pull.said) def gitea_decision(pull: str, run: Run) -> str | None | Failure: @@ -71,10 +107,10 @@ def gitea_decision(pull: str, run: Run) -> str | None | Failure: viewed = gitea(run, f"repos/{{owner}}/{{repo}}/pulls/{pull}") if isinstance(viewed, Failure): return viewed - try: - return None if viewed["title"].upper().startswith(GITEA_DRAFT) else "" - except (KeyError, TypeError, AttributeError): + title = viewed.get("title") if isinstance(viewed, dict) else None + if not isinstance(title, str): return Failure(f"tea api printed no title of pull request {pull}: {str(viewed)[:200]}") + return None if title.upper().startswith(GITEA_DRAFT) else "" def waiting_threads(number: str, run: Run) -> list[str] | Failure: @@ -82,9 +118,31 @@ def waiting_threads(number: str, run: Run) -> list[str] | Failure: read = run(["gh", "api", "graphql", "-f", f"query={THREADS}", "-F", "owner={owner}", "-F", "repo={repo}", "-F", f"number={number}"]) if isinstance(read, Failure): return read + threads = threads_read(read) + if isinstance(threads, Failure): + return threads + return waits(*threads) + + +@dataclasses.dataclass(frozen=True) +class Thread: + """One review thread of a pull request.""" + path: str + resolved: bool + # Who wrote its last comment; empty for a thread without one. + last: tuple[str, ...] + + +def threads_read(printed: str) -> tuple[str, tuple[Thread, ...]] | Failure: + """Who opened the pull request and its review threads, as gh printed THREADS' answer, with each field checked here.""" try: - pull = json.loads(read)["data"]["repository"]["pullRequest"] - author = pull["author"]["login"] - return sorted({thread["path"] for thread in pull["reviewThreads"]["nodes"] if not thread["isResolved"] and any(comment["author"]["login"] != author for comment in thread["comments"]["nodes"])}) + pull = json.loads(printed)["data"]["repository"]["pullRequest"] + threads = tuple(Thread(text(thread["path"]), bool(thread["isResolved"]), tuple(text(comment["author"]["login"]) for comment in thread["comments"]["nodes"])) for thread in pull["reviewThreads"]["nodes"]) + return text(pull["author"]["login"]), threads except (ValueError, KeyError, TypeError): - return Failure(f"gh api graphql printed no review threads: {read.strip()[:200]}") + return Failure(f"gh api graphql printed no review threads: {printed.strip()[:200]}") + + +def waits(author: str, threads: tuple[Thread, ...]) -> list[str]: + """The files of the threads that wait for an answer from author, who opened the pull request.""" + return sorted({thread.path for thread in threads if not thread.resolved and any(last != author for last in thread.last)}) diff --git a/bin/forge/test_reviews.py b/bin/forge/test_reviews.py new file mode 100644 index 00000000..f1467d26 --- /dev/null +++ b/bin/forge/test_reviews.py @@ -0,0 +1,102 @@ +import json +import pathlib +import sys +import unittest + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) + +from forge.reviews import ANSWERED, CHANGES_REQUESTED, PullView, Review, Thread +from failure import Failure +from forge import reviews + +ASKED = {"author": {"login": "rev"}, "state": CHANGES_REQUESTED, "submittedAt": "2026-11-02T10:00:00Z", "commit": {"oid": "abc"}} +PULL = {"isDraft": False, "reviewDecision": CHANGES_REQUESTED, "author": {"login": "me"}, "reviews": [ASKED], "comments": []} + + +def printing(answer: object): + """A run whose command prints answer as JSON.""" + return lambda args: json.dumps(answer) + + +class Faulty: + """A pull request's number or head whose use in a comparison or in a command raises a fault of our own.""" + + def __ne__(self, other: object) -> bool: + raise TypeError("ours") + + def __format__(self, spec: str) -> str: + raise TypeError("ours") + + +class ThePullRequestAsViewed(unittest.TestCase): + def test_parses_what_gh_prints_into_one_value(self): + pending = {"author": {"login": "me"}, "state": "PENDING", "submittedAt": None, "commit": None} + printed = json.dumps(PULL | {"reviews": [ASKED, pending], "comments": [{"author": {"login": "me"}, "createdAt": "2026-11-02T11:00:00Z"}, {"author": {"login": "rev"}, "createdAt": "2026-11-02T12:00:00Z"}]}) + self.assertEqual(reviews.pull_view(printed), PullView(False, CHANGES_REQUESTED, (Review("rev", CHANGES_REQUESTED, "2026-11-02T10:00:00Z", "abc"), Review("me", "PENDING", "", "")), ("", "2026-11-02T11:00:00Z"))) + + def test_an_answer_in_another_shape_is_a_failure_naming_what_gh_printed(self): + unshaped = ("not json", "[]", json.dumps(PULL | {"reviews": None}), json.dumps(PULL | {"author": None}), json.dumps(PULL | {"reviews": [ASKED | {"commit": None}]}), json.dumps(PULL | {"reviews": [ASKED | {"submittedAt": 7}]}), json.dumps({key: value for key, value in PULL.items() if key != "isDraft"})) + for printed in unshaped: + with self.subTest(printed=printed): + self.assertEqual(reviews.pull_view(printed), Failure(f"gh pr view printed no review decision: {printed[:200]}")) + self.assertEqual(reviews.answered_decision("7", "abc", lambda args: printed), Failure(f"gh pr view printed no review decision: {printed[:200]}")) + + +class WhetherTheWorkerAnswered(unittest.TestCase): + def view(self, *others: Review, said: tuple[str, ...] = ()) -> PullView: + return PullView(False, CHANGES_REQUESTED, (Review("rev", CHANGES_REQUESTED, "2026-11-02T10:00:00Z", "abc"), *others), tuple(review.submitted_at for review in others if review.author == "me") + said) + + def test_a_new_head_or_a_later_word_of_the_authors_answers_the_newest_request(self): + self.assertFalse(reviews.answered(self.view(), "abc")) + self.assertTrue(reviews.answered(self.view(), "def")) + self.assertTrue(reviews.answered(self.view(said=("2026-11-02T10:30:00Z",)), "abc")) + self.assertFalse(reviews.answered(self.view(said=("2026-11-02T09:00:00Z", "")), "abc")) + self.assertTrue(reviews.answered(self.view(Review("me", "COMMENTED", "2026-11-02T10:30:00Z", "")), "abc")) + self.assertFalse(reviews.answered(PullView(False, "", (), ()), "abc")) + + def test_the_decision_reads_answered_once_the_worker_answered(self): + self.assertEqual(reviews.answered_decision("7", "abc", printing(PULL)), CHANGES_REQUESTED) + self.assertEqual(reviews.answered_decision("7", "def", printing(PULL)), ANSWERED) + self.assertIsNone(reviews.answered_decision("7", "abc", printing(PULL | {"isDraft": True}))) + self.assertEqual(reviews.answered_decision("7", "abc", printing(PULL | {"reviewDecision": None, "reviews": []})), "") + + def test_a_fault_in_the_decision_is_not_reported_as_the_forges(self): + with self.assertRaisesRegex(TypeError, "ours"): + reviews.answered_decision("7", Faulty(), printing(PULL)) + + +class TheThreadsThatWait(unittest.TestCase): + def printed(self, *threads: tuple[str, bool, str]) -> str: + nodes = [{"isResolved": resolved, "path": path, "comments": {"nodes": [{"author": {"login": last}}]}} for path, resolved, last in threads] + return json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "me"}, "reviewThreads": {"nodes": nodes}}}}}) + + def test_parses_what_gh_prints_into_the_author_and_each_thread(self): + self.assertEqual(reviews.threads_read(self.printed(("a.py", False, "rev"), ("b.py", True, "me"))), ("me", (Thread("a.py", False, ("rev",)), Thread("b.py", True, ("me",))))) + + def test_a_thread_waits_while_unresolved_with_a_reviewers_last_word(self): + printed = self.printed(("b.py", False, "rev"), ("a.py", False, "rev"), ("a.py", False, "rev"), ("c.py", True, "rev"), ("d.py", False, "me")) + self.assertEqual(reviews.waiting_threads("7", lambda args: printed), ["a.py", "b.py"]) + + def test_an_answer_in_another_shape_is_a_failure_naming_what_gh_printed(self): + for printed in ("not json", "{}", json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "me"}, "reviewThreads": {"nodes": [{"isResolved": False}]}}}}})): + with self.subTest(printed=printed): + self.assertEqual(reviews.waiting_threads("7", lambda args: printed), Failure(f"gh api graphql printed no review threads: {printed[:200]}")) + + def test_a_fault_in_the_choice_is_not_reported_as_the_forges(self): + with self.assertRaisesRegex(TypeError, "ours"): + reviews.waiting_threads(Faulty(), lambda args: self.printed(("a.py", False, "rev"))) + + +class TheDecisionOnGitea(unittest.TestCase): + def decision(self, answer: object): + return reviews.gitea_decision("7", printing(answer)) + + def test_a_title_tells_a_draft_and_an_answer_without_one_is_a_failure(self): + self.assertIsNone(self.decision({"title": "wip: later"})) + self.assertEqual(self.decision({"title": "Fix it"}), "") + self.assertEqual(self.decision({"title": 7}), Failure("tea api printed no title of pull request 7: {'title': 7}")) + self.assertEqual(self.decision([]), Failure("tea api printed no title of pull request 7: []")) + + +if __name__ == "__main__": + unittest.main() diff --git a/bin/git_cli/git_repo.py b/bin/git_cli/git_repo.py index 82821b52..87fac04e 100644 --- a/bin/git_cli/git_repo.py +++ b/bin/git_cli/git_repo.py @@ -23,19 +23,20 @@ GUARD_SETTINGS = [ ] -def git(*args: str, cwd: str | None = None, timeout: float | None = None, guarded: bool = False, errors: str = "strict") -> str | Failure: +def git(*args: str, cwd: str | None = None, timeout: float | None = None, guarded: bool = False, errors: str = "strict", env: typing.Mapping[str, str] | None = None) -> str | Failure: """What git printed, run in cwd, else in the current directory. A Failure where git cannot start, exits non-zero, prints text that is not UTF-8, or still runs after timeout seconds, which stops it. errors "replace" reads each byte that is not UTF-8 as U+FFFD instead of failing. It is for output, such as a diff, whose file text the caller skips. + env is the environment git runs in, else this process's. guarded turns off the fsmonitor, the signature check, each transport GUARD_SETTINGS names and a partial clone's fetch of its missing objects. It is for a checkout whose config may name a program. """ command = " ".join(args) guards = [option for setting in GUARD_SETTINGS for option in ("-c", setting)] if guarded else [] # GIT_NO_LAZY_FETCH needs git 2.44. - env = {**os.environ, "GIT_NO_LAZY_FETCH": "1"} if guarded else None + child_env = {**(os.environ if env is None else env), "GIT_NO_LAZY_FETCH": "1"} if guarded else env try: - ran = subprocess.run(["git", *guards, *args], capture_output=True, cwd=cwd, encoding="utf-8", errors=errors, env=env, timeout=timeout) + ran = subprocess.run(["git", *guards, *args], capture_output=True, cwd=cwd, encoding="utf-8", errors=errors, env=child_env, timeout=timeout) except UnicodeDecodeError as failure: return Failure(f"git {command} printed text that is not UTF-8: {failure}") except (OSError, ValueError) as failure: @@ -52,8 +53,8 @@ class Git(typing.Protocol): -def top() -> str | Failure: - found = git("rev-parse", "--show-toplevel") +def top(run: Git = git) -> str | Failure: + found = run("rev-parse", "--show-toplevel") return found if isinstance(found, Failure) else found.strip() diff --git a/bin/judge-replay b/bin/judge-replay index 0252c222..f574a3fe 100755 --- a/bin/judge-replay +++ b/bin/judge-replay @@ -8,4 +8,4 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) from judge_replay import main from loop_effects.effects import system_effects -sys.exit(main.main(sys.argv[1:], os.environ, pathlib.Path.home(), system_effects())) +sys.exit(main.main(sys.argv[1:], os.environ, pathlib.Path.home(), system_effects(pathlib.Path.home(), os.environ))) diff --git a/bin/judge_replay/main.py b/bin/judge_replay/main.py index 42bd0a79..48693f33 100644 --- a/bin/judge_replay/main.py +++ b/bin/judge_replay/main.py @@ -56,15 +56,15 @@ import typing from chunk_driver.prompts import ANSWER_DESTINATIONS from transcript_archive.location import archive_root, transcript_of from transcript_archive.read import archived_outcomes -from rung_replay.archive import archiving, print_report, replay_folder -from rung_replay.command import arguments_or_exit, down_from, failed, limited_exit, projects_on +from rung_replay.archive import archiving, replay_folder +from rung_replay.command import arguments_or_exit, down_from, failed, limited_exit, print_report, projects_on, say +from rung_replay.search import AT_ONCE, first_reply, replayed from agent_cycle.judge import Brief, judge_archived, judge_once from judge_replay.replay import Case, case_from, Kind, report, reproduced, sample, verdict_kind from transcript_archive.project_key import checkout_of from loop_effects.effects import Effects from failure import Failure from judge_replay.offline import final_message, LEARNERS, offline_report, RULES, Sample -from rung_replay.search import first_reply, replayed from judge_replay.archived_prompt import grounds_of from transcript_archive.judge_run import JudgeRun from model_routing.ladder import Ladder, ProjectRung, REVIEW_STEPS, Rung @@ -101,10 +101,10 @@ def main(argv: list[str], env: typing.Mapping[str, str], home: pathlib.Path, eff return offline(project.rung.model, cases, archive_root(home, env), effects) cases = sample(cases, PER_KIND) folder = replay_folder(archive_root(home, env), TOOL, argv, effects.now()) - effects = archiving(effects, folder) + effects = archiving(effects, folder, say) reference = Rung(project.rung.model, REFERENCE_EFFORT) # The candidates stop at the lowest rung a worker routes to: no route uses a rung below it for the judge. - replay = replayed(cases, reference, down_from(ladder, project.rung, project.lowest_worker_rung()), lambda found, rung: rule(found, rung, home, effects), reproduced, "verdicts", climbs(project, reference)) + replay = replayed(cases, reference, down_from(ladder, project.rung, project.lowest_worker_rung()), lambda found, rung: rule(found, rung, home, effects), reproduced, "verdicts", say, AT_ONCE, climbs(project, reference)) if isinstance(replay, Limited): return limited_exit(TOOL, replay) print_report(folder, report(project.rung.model, reference, cases, replay)) diff --git a/bin/judge_replay/test_judge_replay.py b/bin/judge_replay/test_judge_replay.py index 314a0637..3560ed2e 100644 --- a/bin/judge_replay/test_judge_replay.py +++ b/bin/judge_replay/test_judge_replay.py @@ -13,7 +13,7 @@ from chunk_driver.prompts import ANSWER_DESTINATIONS from transcript_archive.outcomes import ANSWERED, FINISHED, INTERRUPTED, UNFINISHED from claude_code.entries import AssistantTurn, HumanTurn, LoopPrompt, Text from judge_replay.replay import Case, case_from, report, reproduced, sample, verdict_kind -from text_files import compressed +from text_files.doubles import compress from project_config.doubles import configured from loop_effects.effects import Effects from failure import Failure @@ -142,7 +142,7 @@ class Main(unittest.TestCase): result = reply(Rung(args[args.index("--model") + 1], args[args.index("--effort") + 1]), "said Verdict: interrupt" in prompt.rsplit("latest last:", 1)[-1]) return subprocess.CompletedProcess(args, 0, json.dumps(result), "") - effects = Effects(claude=claude, claude_on_terminal=unused, discard_typed=unused, dm=unused, execv=unused, git=unused, input=unused, local_time=unused, memory=unused, now=lambda: 1790000000.0, on_terminal=unused, python=unused, sleep=unused, stream_claude=unused, timer=unused, tmux=unused, tmux_on_terminal=unused) + effects = Effects(claude=claude, claude_on_terminal=unused, discard_typed=unused, dm=unused, execv=unused, git=unused, input=unused, local_time=unused, memory=unused, now=lambda: 1790000000.0, on_terminal=unused, python=unused, replace_file=unused, sleep=unused, stream_claude=unused, timer=unused, tmux=unused, tmux_on_terminal=unused) out, err = io.StringIO(), io.StringIO() with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): code = main.main(argv, {}, self.home, effects) @@ -276,7 +276,7 @@ class Main(unittest.TestCase): self.assertIn("replayed failed: could not read the transcript", out) def test_replays_a_verdict_whose_transcript_is_compressed(self): - self.assertIsNone(compressed.compress(self.home / ".claude" / "loop-claude-transcripts" / "proj" / "c1" / "t0.jsonl")) + self.assertIsNone(compress(self.home / ".claude" / "loop-claude-transcripts" / "proj" / "c1" / "t0.jsonl")) code, out, err = self.run_main(["claude-opus-5-5"], lambda rung, interrupted: self.verdict(interrupted, f"said {INTERRUPTED if interrupted else FINISHED}")) self.assertEqual(code, 0, err) self.assertNotIn("replayed failed", out) @@ -343,7 +343,7 @@ class JudgeArchivedRuns(unittest.TestCase): cwds.append(cwd) return subprocess.CompletedProcess(args, 0, json.dumps({"type": "result", "structured_output": output, "total_cost_usd": 0.25}), "") - effects = Effects(claude=claude, claude_on_terminal=unused, discard_typed=unused, dm=unused, execv=unused, git=unused, input=unused, local_time=unused, memory=unused, now=lambda: 1790000000.0, on_terminal=unused, python=unused, sleep=unused, stream_claude=unused, timer=unused, tmux=unused, tmux_on_terminal=unused) + effects = Effects(claude=claude, claude_on_terminal=unused, discard_typed=unused, dm=unused, execv=unused, git=unused, input=unused, local_time=unused, memory=unused, now=lambda: 1790000000.0, on_terminal=unused, python=unused, replace_file=unused, sleep=unused, stream_claude=unused, timer=unused, tmux=unused, tmux_on_terminal=unused) grounds = grounds_of(prompt, self.ENTRIES) return (grounds if isinstance(grounds, Failure) else judge_archived("/code/p", prompt, grounds, len(self.ENTRIES), REFERENCE, LOOP_CLAUDE, effects)), cwds diff --git a/bin/larv_review/test_larv_review.py b/bin/larv_review/test_larv_review.py index 88782485..8aba70b4 100644 --- a/bin/larv_review/test_larv_review.py +++ b/bin/larv_review/test_larv_review.py @@ -9,7 +9,6 @@ import sys import tempfile import textwrap import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -614,18 +613,18 @@ class LarvReviewTool(unittest.TestCase): (self.config.parent / "todo.md").write_text(todo(row, "| 2 | 0.2.0 | | **Reach the comprehension floor: do it.** | 1 | 1 | 3 | 1 | 1 | 1 | 9.0 |")) self.addCleanup(os.chdir, os.getcwd()) os.chdir(self.checkout) - with unittest.mock.patch.dict(os.environ, {"HOME": str(self.config.parents[2])}): - self.assertEqual(plan_reasons("origin/main", [2], pathlib.Path.home()), ["ships item 2, a floor item", "leaves release 0.2.0 with no plan item"]) - (self.config.parent / "decisions.md").write_text("# Decisions\n\n## review opt-out: the comprehension check\n\nServes goal 1.\n") - self.assertEqual(plan_reasons("origin/main", [2], pathlib.Path.home()), []) - (self.config.parent / "decisions.md").unlink() - gone = plan_reasons("origin/main", [1, 7], pathlib.Path.home()) - assert isinstance(gone, Failure), gone - self.assertEqual(gone.reason, f"the plan as it stands holds no item 7, and larv-review doesn't support a plan outside the checkout, so it cannot read the plan as it stood before the item was finished: {self.config.parent}/todo.md") - (self.config.parent / "todo.md").write_bytes(b"\xff") - unreadable = plan_reasons("origin/main", [2], pathlib.Path.home()) - assert isinstance(unreadable, Failure), unreadable - self.assertEqual(unreadable.reason, f"the project's plan: {self.config.parent}/todo.md: not UTF-8") + home = self.config.parents[2] + self.assertEqual(plan_reasons("origin/main", [2], home), ["ships item 2, a floor item", "leaves release 0.2.0 with no plan item"]) + (self.config.parent / "decisions.md").write_text("# Decisions\n\n## review opt-out: the comprehension check\n\nServes goal 1.\n") + self.assertEqual(plan_reasons("origin/main", [2], home), []) + (self.config.parent / "decisions.md").unlink() + gone = plan_reasons("origin/main", [1, 7], home) + assert isinstance(gone, Failure), gone + self.assertEqual(gone.reason, f"the plan as it stands holds no item 7, and larv-review doesn't support a plan outside the checkout, so it cannot read the plan as it stood before the item was finished: {self.config.parent}/todo.md") + (self.config.parent / "todo.md").write_bytes(b"\xff") + unreadable = plan_reasons("origin/main", [2], home) + assert isinstance(unreadable, Failure), unreadable + self.assertEqual(unreadable.reason, f"the project's plan: {self.config.parent}/todo.md: not UTF-8") ROW = "| 3 | 0.2.0 | | **And this.** | 1 | 1 | 3 | 1 | 1 | 1 | 9.0 |" @@ -992,7 +991,7 @@ class LarvReviewTool(unittest.TestCase): (checkout / "todo.md").write_text("the owners' own notes\n") self.enterContext(contextlib.chdir(checkout)) said: list[str] = [] - self.assertIsNone(rerank(pathlib.Path.home(), said.append)) + self.assertIsNone(rerank(config.parents[2], said.append)) self.assertEqual((said, (checkout / "todo.md").read_text()), ([], "the owners' own notes\n")) def test_reranks_todo_md_by_the_overall_it_records(self): diff --git a/bin/larv_review/test_scoring_setup.py b/bin/larv_review/test_scoring_setup.py index 59f97a15..51edfe61 100644 --- a/bin/larv_review/test_scoring_setup.py +++ b/bin/larv_review/test_scoring_setup.py @@ -1,4 +1,3 @@ -from unittest import mock import os import pathlib import re @@ -103,8 +102,7 @@ class PrepareScoring(unittest.TestCase): def prepare(self): """What prepare-scoring returns in the checkout, run in this process so mutation tests see the lines it runs.""" - with mock.patch.dict(os.environ, self.env, clear=True): - return prepare_scoring(str(self.checkout), pathlib.Path.home()) + return prepare_scoring(str(self.checkout), self.root) def run_tool(self, *args): return subprocess.run([sys.executable, str(TOOL), *args], capture_output=True, cwd=self.checkout, env=self.env, text=True) @@ -139,8 +137,7 @@ class PrepareScoring(unittest.TestCase): self.commit() checkout = self.root / ".local" / "state" / "larv-review" / "proj" / "scoring" / "checkout" checkout.mkdir(parents=True) - with mock.patch.dict(os.environ, self.env, clear=True): - removed = removed_setup(str(self.checkout), pathlib.Path.home()) + removed = removed_setup(str(self.checkout), self.root) self.assertIsInstance(removed, Failure) self.assertTrue(removed.reason.startswith(f"could not remove the scoring worktree {checkout}: "), removed.reason) self.assertTrue(checkout.exists()) diff --git a/bin/loop-claude b/bin/loop-claude index 8bfb9050..c9580a0f 100755 --- a/bin/loop-claude +++ b/bin/loop-claude @@ -10,4 +10,4 @@ from loop_effects import effects from loop_claude.loop import main from loop_state import paths -sys.exit(main.main(sys.argv[1:], os.environ, paths.paths_from(pathlib.Path.home()), LAUNCHER, pathlib.Path.cwd(), effects.system_effects())) +sys.exit(main.main(sys.argv[1:], os.environ, paths.paths_from(pathlib.Path.home()), LAUNCHER, pathlib.Path.cwd(), effects.system_effects(pathlib.Path.home(), os.environ))) diff --git a/bin/loop_claude/loop/chunk_windows.py b/bin/loop_claude/loop/chunk_windows.py index 61570bba..b9776378 100644 --- a/bin/loop_claude/loop/chunk_windows.py +++ b/bin/loop_claude/loop/chunk_windows.py @@ -1,5 +1,4 @@ """The chunks of one project that run at once, each in its own tmux window: which may start now, and how each starts (docs/decisions.md → `loop-claude: each chunk runs in its own tmux window`).""" -import contextlib import dataclasses import pathlib import re @@ -116,21 +115,19 @@ def start_chunks(loop_run: LoopRun, leaving: bool, paths: Paths, env: typing.Map return config session = loop_session(loop_run.root, paths.home) handed = {name: value for name, value in env.items() if name.startswith(HANDED)} - lock = project_lock(loop_run.root, paths.home) - try: - with starting(lock): - windows = tmux.listed(session, effects.tmux) - if refused := refusal(session, windows, loop_run.root, effects.tmux): - return refused - chunks = chunks_to_start(windows, loop_run, leaving, config.parallel_chunks, paths, effects) - for chunk in chunks: - started = tmux.start_chunk(tmux.Window(session, chunk, ""), loop_run.root, handed, [str(launcher), CHUNK_FLAG, chunk, loop_run.task], windows, True, effects.tmux) - if isinstance(started, Failure): - return started - windows = started - return chunks - except OSError as failure: - return Failure(f"cannot lock {lock / LOCK}: {failure.strerror}") + with starting(project_lock(loop_run.root, paths.home)) as unlocked: + if unlocked: + return unlocked + windows = tmux.listed(session, effects.tmux) + if refused := refusal(session, windows, loop_run.root, effects.tmux): + return refused + chunks = chunks_to_start(windows, loop_run, leaving, config.parallel_chunks, paths, effects) + for chunk in chunks: + started = tmux.start_chunk(tmux.Window(session, chunk, ""), loop_run.root, handed, [str(launcher), CHUNK_FLAG, chunk, loop_run.task], windows, True, effects.tmux) + if isinstance(started, Failure): + return started + windows = started + return chunks def refusal(session: str, windows: list[tmux.Listed], root: str, run: tmux.Tmux) -> Failure | None: @@ -165,5 +162,5 @@ def held_beside(chunk: str, root: str, paths: Paths, effects: Effects) -> list[i def wait_for_starts(root: str, home: pathlib.Path) -> None: """Returns once no loop of the project is starting windows: start_chunks holds LOCK while it starts them. Where the lock cannot be taken, it returns at once.""" - with contextlib.suppress(OSError), starting(project_lock(root, home)): + with starting(project_lock(root, home)): pass diff --git a/bin/loop_claude/loop/main.py b/bin/loop_claude/loop/main.py index 69632c74..aae44311 100644 --- a/bin/loop_claude/loop/main.py +++ b/bin/loop_claude/loop/main.py @@ -11,7 +11,7 @@ from loop_claude.chunk import chunk_items, fresh_prompt from loop_claude.loop.chunk_windows import chunk_session, item_of, start_chunks from loop_claude.loop.chunk_start import chunk_start, plan_item, scored_plan from chunk_driver.chunk_run import closed_cycle -from agent_cycle.run import Cycle, run_cycle +from agent_cycle.run import Concluded, Cycle, run_cycle from loop_claude.loop.reload import decode_handed, exec_if_edited, fingerprint, LoopRun, RELOAD_FLAG from loop_effects.effects import Effects, say from failure import Failure @@ -20,6 +20,7 @@ from loop_claude.loop.usage import HELP from chunk_driver.this_loop import Loop from loop_state.paths import Paths from transcript_archive.project_key import project_key +from model_routing.ladder import ProjectRung, Rung from chunk_driver.project.premises import read_premises from chunk_driver.project.records import read_records from model_routing.ladder_file import SHIPPED @@ -28,9 +29,31 @@ from agent_cycle.direct_message import tell from agent_cycle import tmux +# Starts the chunks that may start now, as chunk_windows.start_chunks does. +StartChunks = typing.Callable[[LoopRun, bool, Paths, typing.Mapping[str, str], pathlib.Path, Effects], list[str] | Failure] + + +@dataclasses.dataclass(frozen=True) +class Steps: + """The steps a loop takes, each the function of its name, which main binds.""" + start_chunks: StartChunks + fingerprint: typing.Callable[[pathlib.Path], str] + exec_if_edited: typing.Callable[[Loop, str, LoopRun], Stop | None] + next_cycle: typing.Callable[[LoopRun, Loop], Cycle | Stop] + run_cycle: typing.Callable[[Cycle], Stop | Concluded] + closed_cycle: typing.Callable[[LoopRun, ProjectRung | None, Concluded, pathlib.Path, str, Loop], LoopRun | Stop] + check_routing: typing.Callable[[pathlib.Path, Rung, Loop], Stop | None] + + def main(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, cwd: pathlib.Path, effects: Effects) -> int: + steps = Steps(start_chunks, fingerprint, exec_if_edited, next_cycle, run_cycle, closed_cycle, check_routing) + return exit_code(lambda: run_loop(argv, env, paths, launcher, cwd, effects, steps), cwd, effects) + + +def exit_code(run: typing.Callable[[], Stop], cwd: pathlib.Path, effects: Effects) -> int: + """The exit code of the loop that run runs in cwd, once its stop is said and sent as a DM.""" try: - stop = run_loop(argv, env, paths, launcher, cwd, effects) + stop = run() except KeyboardInterrupt: stop = Stop(130, "") except Exception as crash: @@ -44,7 +67,7 @@ def main(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launcher: return stop.code -def run_loop(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, cwd: pathlib.Path, effects: Effects) -> Stop: +def run_loop(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, cwd: pathlib.Path, effects: Effects, steps: Steps) -> Stop: loop_run = loop_run_from(argv, cwd, effects) if isinstance(loop_run, Stop): return loop_run @@ -52,38 +75,38 @@ def run_loop(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launc if reason := tmux.unusable(effects.tmux, fresh_prompt.DRIVER): return Stop(1, reason) if loop_run.chunk is None: - return windows_opened(loop_run, env, paths, launcher, effects) + return windows_opened(loop_run, env, paths, launcher, effects, steps.start_chunks) pane = env.get("TMUX_PANE") if not pane: return Stop(1, f"{CHUNK_FLAG} is how loop-claude starts itself in a tmux window; run loop-claude ") - baseline = fingerprint(launcher) + baseline = steps.fingerprint(launcher) key = project_key(loop_run.root, paths.home) window = tmux.Window(loop_session(loop_run.root, paths.home), loop_run.chunk, pane) loop = Loop(root=loop_run.root, session=chunk_session(key, loop_run.chunk, paths.claude_code_transcripts), window=window, paths=paths, env=env, launcher=launcher, effects=effects) while True: - if stop := exec_if_edited(loop, baseline, loop_run): + if stop := steps.exec_if_edited(loop, baseline, loop_run): return stop loop_run = dataclasses.replace(loop_run, cycle_number=loop_run.cycle_number + 1) - cycle = next_cycle(loop_run, loop) + cycle = steps.next_cycle(loop_run, loop) if isinstance(cycle, Stop): return cycle - concluded = run_cycle(cycle) + concluded = steps.run_cycle(cycle) if isinstance(concluded, Stop): return concluded - closed = closed_cycle(loop_run, cycle.project, concluded, cycle.project_archive, fresh_prompt.DRIVER, loop) + closed = steps.closed_cycle(loop_run, cycle.project, concluded, cycle.project_archive, fresh_prompt.DRIVER, loop) if isinstance(closed, Stop): return closed loop_run = closed # Off the ladder the worker runs on the project's model itself, which its route names. - if stop := check_routing(archive_root(paths.home, env), cycle.project.rung if cycle.project else cycle.route.rung, loop): + if stop := steps.check_routing(archive_root(paths.home, env), cycle.project.rung if cycle.project else cycle.route.rung, loop): return stop if isinstance(concluded.verdict, Finished): - return handed_on(loop_run, loop) + return handed_on(loop_run, loop, steps.start_chunks) -def windows_opened(loop_run: LoopRun, env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, effects: Effects) -> Stop: - """What the loop-claude the human runs does: starts each chunk that may start now in a window of the project's tmux session, then puts that session on the terminal until the human detaches.""" - started = start_chunks(loop_run, False, paths, env, launcher, effects) +def windows_opened(loop_run: LoopRun, env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, effects: Effects, start: StartChunks) -> Stop: + """What the loop-claude the human runs does: starts, through start, each chunk that may start now in a window of the project's tmux session, then puts that session on the terminal until the human detaches.""" + started = start(loop_run, False, paths, env, launcher, effects) if isinstance(started, Failure): return Stop(1, started.reason) session = loop_session(loop_run.root, paths.home) @@ -95,9 +118,9 @@ def windows_opened(loop_run: LoopRun, env: typing.Mapping[str, str], paths: Path return Stop(1, f"`tmux {' '.join(shown)}` failed; the chunks go on in the tmux session") if effects.tmux_on_terminal(shown) else Stop(0, "") -def handed_on(loop_run: LoopRun, loop: Loop) -> Stop: - """The stop of a loop whose chunk finished, once it has started each chunk that may start now. Its window closes as it exits.""" - started = start_chunks(loop_run, True, loop.paths, loop.env, loop.launcher, loop.effects) +def handed_on(loop_run: LoopRun, loop: Loop, start: StartChunks) -> Stop: + """The stop of a loop whose chunk finished, once it has started, through start, each chunk that may start now. Its window closes as it exits.""" + started = start(loop_run, True, loop.paths, loop.env, loop.launcher, loop.effects) if isinstance(started, Failure): return Stop(1, f"the chunk finished, but the next could not start: {started.reason}") say(f"the chunk finished; started {', '.join(started) or 'no other, since the chunks running fill what may run now'}", loop.effects, stdout=True) diff --git a/bin/loop_claude/loop/routing_check.py b/bin/loop_claude/loop/routing_check.py index d938f934..57e032b1 100644 --- a/bin/loop_claude/loop/routing_check.py +++ b/bin/loop_claude/loop/routing_check.py @@ -67,6 +67,6 @@ def raise_calls(root: pathlib.Path, new: list[Call], rung: Rung, loop: Loop) -> if unstarted: return Stop(1, f"could not start claude for the routing calls in {loop.paths.config_clone}, the setup's development clone: {unstarted.reason}") # Raised once the human has seen them, answered or not: an unanswered call reaches the plan through docs/decisions.md → "Working rules", so it is not asked twice. - if unmarked := state.mark_raised(root, new, utc(loop.effects.now())): + if unmarked := state.mark_raised(root, new, utc(loop.effects.now()), loop.effects.replace_file): say(f"routing figures: {unmarked.reason}", loop.effects) return None diff --git a/bin/loop_claude/loop/test_chunk_start.py b/bin/loop_claude/loop/test_chunk_start.py index 15e9fc6e..ea8ffbef 100644 --- a/bin/loop_claude/loop/test_chunk_start.py +++ b/bin/loop_claude/loop/test_chunk_start.py @@ -5,7 +5,6 @@ import pathlib import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) @@ -36,11 +35,23 @@ class RouteSources(unittest.TestCase): # An item that left the plan, as once it shipped. self.assertIsNone(chunk_start.plan_item(plan, 999, FakeEffects().effects())) # A chunk start reads the default branch once, for routing and for scoring the items the worker names. - with unittest.mock.patch.object(chunk_start, "session_open", return_value=True), unittest.mock.patch.object(chunk_start, "fetched_plan", return_value=plan) as fetched: - loop = a_loop(FakeEffects().effects()) - self.assertEqual(chunk_start.chunk_start(loop), ("", plan)) + with tempfile.TemporaryDirectory() as root: + folder = configured(self, pathlib.Path(root)) + config = folder / "config.toml" + config.write_text(config.read_text().replace('"checkout:todo.md"', '"folder:todo.md"')) + (folder / "todo.md").write_text(TODO) + opened = session.session_named("A proj loop-claude", pathlib.Path(root, "projects")) + transcript = pathlib.Path(root, "projects", "-fake", f"{opened.id}.jsonl") + transcript.parent.mkdir(parents=True) + transcript.write_text("") + unread = Failure("no git") + fake = FakeEffects(git=["origin\n", "origin/main\n", unread, unread, unread, unread]) + with contextlib.redirect_stderr(io.StringIO()) as stderr: + facts, held = chunk_start.chunk_start(a_loop(fake.effects(), root=root, session=opened, paths=paths.paths_from(folder.parents[1]))) + self.assertEqual((facts, held.plan), ("", plan_of(TODO))) + self.assertEqual([args for args in fake.made("git") if args[0] == "remote"], [["remote"]]) # An open session is no fresh chunk, so the human isn't told again of releases the plan cannot show. - fetched.assert_called_once_with(loop, False) + self.assertEqual(stderr.getvalue(), "") unreadable = Failure("git show origin/main:todo.md printed text that is not UTF-8") for plan, said in ((DefaultPlan(plan_of(TODO.replace("| 4 | 2 | 3 |", "| x | 2 | 3 |")), NO_CUT), "routing on the project's rung: item 3's L 'x' is not a whole number from 1 to 4\n"), (DefaultPlan(unreadable, NO_CUT), f"routing on the project's rung: {unreadable.reason}\n"), (Failure("no origin remote"), "routing on the project's rung: no origin remote\n")): with self.subTest(said=said): diff --git a/bin/loop_claude/loop/test_chunk_windows.py b/bin/loop_claude/loop/test_chunk_windows.py index 9376890f..71099dfa 100644 --- a/bin/loop_claude/loop/test_chunk_windows.py +++ b/bin/loop_claude/loop/test_chunk_windows.py @@ -1,11 +1,12 @@ import contextlib +import dataclasses import io import json import pathlib +import subprocess import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) @@ -13,11 +14,18 @@ from loop_effects.doubles import assistant, FakeEffects from loop_claude.loop import chunk_windows from project_config.doubles import configured from failure import Failure +from git_cli import git_repo +from test_env import isolated_env from loop_claude.loop import main from release_plan import next_chunk from loop_state import paths from agent_cycle import tmux -from touch_predictions.doubles import plan as touch_plan +from touch_predictions.doubles import todo, plan as touch_plan + + +def plan_text(*touched): + """A todo.md of items 1, 2 and on, each predicted to touch the path given for it.""" + return todo(*((number, "1.0.0", "", f"- `{path}`") for number, path in enumerate(touched, 1))) class ChunkWindows(unittest.TestCase): @@ -76,8 +84,8 @@ class ChunkWindows(unittest.TestCase): self.assertIsNone(chunk_windows.item_of("item-\u0663")) def test_starts_only_the_next_item_where_the_touch_predictions_cannot_be_read(self): - with unittest.mock.patch.object(chunk_windows, "chunks_beside", return_value="item 1's Touches: list cannot be read"): - self.assertEqual(chunk_windows.may_start(self.plan("a.py", "c.py"), [], set(), 3), ["item-1"]) + items = touch_plan((1, "1.0.0", "", "- a.py"), (2, "1.0.0", "", "- `c.py`")) + self.assertEqual(chunk_windows.may_start(next_chunk.NextChunk(items=items, first=items[0], text="", branch_template=""), [], set(), 3), ["item-1"]) def test_a_chunk_whose_transcript_cannot_be_read_holds_the_item_it_started_from(self): with tempfile.TemporaryDirectory() as temp: @@ -113,7 +121,7 @@ class StartChunks(unittest.TestCase): self.home = configured(self, self.root).parents[1] self.session = "loop-" + str(self.root).lstrip("/").replace("/", "-").replace(".", "-") # Items 1 and 2 overlap, so 2 waits; 3 runs beside 1. - self.plan = ChunkWindows.plan(self, "a.py", "a.py", "c.py") + self.plan = plan_text("a.py", "a.py", "c.py") def limit(self, chunks): config = self.home / ".claude-projects" / self.root.name / "config.toml" @@ -123,15 +131,31 @@ class StartChunks(unittest.TestCase): """start_chunks' answer, for a session whose @loop-root names owner, by default this checkout.""" fake.owner = str(self.root) if owner is None else owner run = main.LoopRun(0, str(self.root), "ship it", "", chunk=chunk) - with unittest.mock.patch.object(chunk_windows, "next_plan", return_value=plan or self.plan) as planned, contextlib.redirect_stderr(io.StringIO()) as said: - effects = fake.effects() + ran = [] + effects = dataclasses.replace(fake.effects(), git=self.git_reading(self.plan if plan is None else plan, ran)) + with contextlib.redirect_stderr(io.StringIO()) as said: started = chunk_windows.start_chunks(run, leaving, paths.paths_from(self.home), env or {}, self.LAUNCHER, effects) self.said = said.getvalue() - self.planned = planned.call_count - if planned.call_count: - planned.assert_called_once_with(str(self.root), self.home, effects) + fetches = [args for args in ran if args[0] == "fetch"] + self.planned = len(fetches) + self.assertIn(fetches, ([], [["fetch", "--quiet", "origin"]])) return started + def git_reading(self, plan, ran): + """A git that notes each call in ran, and reads plan, a todo.md's text, on the checkout's default branch; a Failure answers every call instead, and an error is raised by every call.""" + if isinstance(plan, str): + (self.root / "todo.md").write_text(plan) + for command in (["add", "todo.md"], ["-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "--quiet", "--allow-empty", "-m", "plan"]): + subprocess.run(["git", "-C", str(self.root), *command], check=True, env=isolated_env()) + + def git(*args, **options): + ran.append(list(args)) + if isinstance(plan, BaseException): + raise plan + return plan if isinstance(plan, Failure) else git_repo.git(*args, **options, env=isolated_env()) + + return git + def starts(self, fake): """The window starts of fake's tmux calls, each as (command, window name, what it runs).""" return [(call[0], call[call.index("-n") + 1], call[call.index("--") + 1:]) for call in fake.tmux_calls if call[0] in ("new-session", "new-window")] @@ -204,7 +228,7 @@ class StartChunks(unittest.TestCase): def test_a_plan_with_no_item_starts_nothing_beside_a_restarted_chunk(self): self.limit(2) fake = FakeEffects(windows="item-5\t1\t@3\n") - self.assertEqual(self.start(fake, plan=next_chunk.NoItem("release 1.0.0 has no items")), ["item-5"]) + self.assertEqual(self.start(fake, plan=plan_text()), ["item-5"]) def test_a_finishing_chunk_leaves_a_stopped_chunk_and_its_items_to_the_human(self): self.limit(2) @@ -218,7 +242,7 @@ class StartChunks(unittest.TestCase): self.assertEqual(self.start(fake, chunk="item-7", leaving=True), ["item-3"]) self.assertEqual([call for call in fake.tmux_calls if call[0] == "kill-window"], []) fake = FakeEffects(windows="item-7\t0\t@1\nno-item\t1\t@3\n") - self.assertEqual(self.start(fake, chunk="item-7", leaving=True, plan=next_chunk.NoItem("release 1.0.0 has no items")), []) + self.assertEqual(self.start(fake, chunk="item-7", leaving=True, plan=plan_text()), []) def test_starts_the_chunk_without_a_plan_item_where_the_plan_cannot_be_read(self): fake = FakeEffects() @@ -226,8 +250,9 @@ class StartChunks(unittest.TestCase): self.assertIn("starting a chunk without a plan item, since the plan cannot be read: no origin/main", self.said) def test_says_why_a_window_could_not_start(self): - with unittest.mock.patch.object(tmux, "start_window", return_value=Failure("tmux could not start the window")): - self.assertEqual(self.start(FakeEffects()), Failure("tmux could not start the window")) + fake = FakeEffects() + fake.tmux = lambda args: subprocess.CompletedProcess(args, 1, "", "no room") if args[0] == "new-session" else FakeEffects.tmux(fake, args) + self.assertEqual(self.start(fake), Failure(f"tmux could not start the window {self.session}:item-1: no room")) def test_says_why_it_cannot_take_the_lock_on_starting_windows(self): (self.home / ".local" / "state").mkdir(parents=True) @@ -236,6 +261,16 @@ class StartChunks(unittest.TestCase): self.assertIsInstance(started, Failure) self.assertTrue(started.reason.startswith(f"cannot lock {self.home}/.local/state/loop-claude/"), started.reason) + def test_an_error_raised_under_the_lock_is_not_reported_as_the_locks(self): + with self.assertRaisesRegex(OSError, "the plan's disk is gone"): + self.start(FakeEffects(), plan=OSError("the plan's disk is gone")) + + def test_a_chunk_reads_the_other_windows_where_it_cannot_take_the_lock(self): + (self.home / ".local" / "state").mkdir(parents=True) + (self.home / ".local" / "state" / "loop-claude").write_text("") + fake = FakeEffects(windows="item-7\t0\t@1\nitem-9\t1\t@3\n") + self.assertEqual(chunk_windows.held_beside("item-7", str(self.root), paths.paths_from(self.home), fake.effects()), [9]) + def test_a_chunk_holds_its_own_item_and_those_on_its_latest_chunk_items_line(self): with tempfile.TemporaryDirectory() as temp: transcripts = pathlib.Path(temp) diff --git a/bin/loop_claude/loop/test_main.py b/bin/loop_claude/loop/test_main.py index 0f37bee2..8e955dda 100644 --- a/bin/loop_claude/loop/test_main.py +++ b/bin/loop_claude/loop/test_main.py @@ -108,15 +108,28 @@ class LoopRuns(unittest.TestCase): self.assertEqual(reload.exec_if_edited(a_loop(fake.effects(), launcher=launcher), "stale", main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="")), Stop(1, "could not reload loop-claude: No such file or directory; rerun loop-claude ")) +def steps(start_chunks, **others): + """The steps of a loop that starts chunks through start_chunks, and takes each other step through the double given for it, else fails.""" + def unused(*args): + raise AssertionError(args) + + return main.Steps(**{"fingerprint": unused, "exec_if_edited": unused, "next_cycle": unused, "run_cycle": unused, "closed_cycle": unused, "check_routing": unused, **others}, start_chunks=start_chunks) + + class WindowStarts(unittest.TestCase): """The loop-claude the human runs, which starts the windows, and the stop of a window's loop whose chunk finished.""" HOME = pathlib.Path("/h") def run_loop(self, argv, fake, env=None, started=("item-3", "item-4")): """run_loop's stop and what it said on stdout, with start_chunks answering `started`; self.asked holds what start_chunks was asked.""" - with unittest.mock.patch.object(main, "start_chunks", return_value=list(started) if isinstance(started, tuple) else started) as start, contextlib.redirect_stdout(io.StringIO()) as said: - stop = main.run_loop(argv, env or {}, paths.paths_from(self.HOME), pathlib.Path("/b/loop-claude"), CWD, fake.effects()) - self.asked = [call.args[:2] for call in start.call_args_list] + self.asked = [] + + def start(*args): + self.asked.append(args[:2]) + return list(started) if isinstance(started, tuple) else started + + with contextlib.redirect_stdout(io.StringIO()) as said: + stop = main.run_loop(argv, env or {}, paths.paths_from(self.HOME), pathlib.Path("/b/loop-claude"), CWD, fake.effects(), steps(start)) return stop, said.getvalue() def test_refuses_to_start_anything_on_a_tmux_older_than_needed(self): @@ -141,9 +154,10 @@ class WindowStarts(unittest.TestCase): def test_hands_the_window_starts_its_paths_environment_launcher_and_effects(self): fake = FakeEffects(git=["/r\n"], on_terminal=False) effects, env, launcher = fake.effects(), {"LOOP_CLAUDE_X": "1"}, pathlib.Path("/b/loop-claude") - with unittest.mock.patch.object(main, "start_chunks", return_value=["item-3"]) as start, contextlib.redirect_stdout(io.StringIO()): - main.run_loop(["do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects) - self.assertEqual(start.call_args.args, (main.LoopRun(0, "/r", "do it", ""), False, paths.paths_from(self.HOME), env, launcher, effects)) + asked = [] + with contextlib.redirect_stdout(io.StringIO()): + main.run_loop(["do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects, steps(lambda *args: asked.append(args) or ["item-3"])) + self.assertEqual(asked, [(main.LoopRun(0, "/r", "do it", ""), False, paths.paths_from(self.HOME), env, launcher, effects)]) def test_a_window_loop_runs_its_chunk_in_its_own_session_and_window_and_hands_on_once_it_finished(self): fake = FakeEffects(git=["/r\n"]) @@ -153,14 +167,15 @@ class WindowStarts(unittest.TestCase): unfinished = unittest.mock.Mock(verdict=unittest.mock.Mock(spec=Unfinished)) finished = unittest.mock.Mock(verdict=unittest.mock.Mock(spec=Finished)) closed = [main.LoopRun(1, "/r", "do it", "", chunk="item-3"), main.LoopRun(2, "/r", "do it", "", chunk="item-3")] - with unittest.mock.patch.object(main, "fingerprint", return_value=""), unittest.mock.patch.object(main, "exec_if_edited", return_value=None), unittest.mock.patch.object(main, "next_cycle", return_value=cycle) as next_cycle, \ - unittest.mock.patch.object(main, "run_cycle", side_effect=[unfinished, finished]), unittest.mock.patch.object(main, "closed_cycle", side_effect=closed), \ - unittest.mock.patch.object(main, "check_routing", return_value=None), unittest.mock.patch.object(main, "handed_on", return_value=Stop(0, "")) as handed_on: - stop = main.run_loop(["--chunk", "item-3", "do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects) + concluded, cycles, started = [unfinished, finished], [], [] + taken = steps(lambda *args: started.append(args) or [], fingerprint=lambda launcher: "", exec_if_edited=lambda *args: None, next_cycle=lambda *args: cycles.append(args) or cycle, + run_cycle=lambda cycle: concluded.pop(0), closed_cycle=lambda *args: closed.pop(0), check_routing=lambda *args: None) + with contextlib.redirect_stdout(io.StringIO()): + stop = main.run_loop(["--chunk", "item-3", "do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects, taken) loop = this_loop.Loop(root="/r", session=session.session_named("A r loop-claude item-3", self.HOME / ".claude" / "projects"), window=tmux.Window("loop-r", "item-3", "%4"), paths=paths.paths_from(self.HOME), env=env, launcher=launcher, effects=effects) self.assertEqual(stop, Stop(0, "")) - self.assertEqual([call.args for call in next_cycle.call_args_list], [(main.LoopRun(1, "/r", "do it", "", chunk="item-3"), loop), (main.LoopRun(2, "/r", "do it", "", chunk="item-3"), loop)]) - handed_on.assert_called_once_with(closed[1], loop) + self.assertEqual(cycles, [(main.LoopRun(1, "/r", "do it", "", chunk="item-3"), loop), (main.LoopRun(2, "/r", "do it", "", chunk="item-3"), loop)]) + self.assertEqual(started, [(main.LoopRun(2, "/r", "do it", "", chunk="item-3"), True, loop.paths, env, launcher, effects)]) def test_a_window_loop_given_no_task_prints_the_help(self): self.assertEqual(main.loop_run_from(["--chunk", "item-3"], CWD, FakeEffects(git=["/r\n"]).effects()), Stop(1, usage.HELP)) @@ -184,8 +199,8 @@ class WindowStarts(unittest.TestCase): def test_stops_where_tmux_cannot_show_the_session(self): fake = FakeEffects(git=["/r\n"]) effects = dataclasses.replace(fake.effects(), tmux_on_terminal=lambda args: 1) - with unittest.mock.patch.object(main, "start_chunks", return_value=["item-3"]), contextlib.redirect_stdout(io.StringIO()): - stop = main.run_loop(["do it"], {}, paths.paths_from(self.HOME), pathlib.Path("/b/loop-claude"), CWD, effects) + with contextlib.redirect_stdout(io.StringIO()): + stop = main.run_loop(["do it"], {}, paths.paths_from(self.HOME), pathlib.Path("/b/loop-claude"), CWD, effects, steps(lambda *args: ["item-3"])) self.assertEqual(stop, Stop(1, "`tmux attach-session -t =loop-r` failed; the chunks go on in the tmux session")) def test_a_window_loop_outside_tmux_stops(self): @@ -201,11 +216,11 @@ class WindowStarts(unittest.TestCase): loop_run = main.LoopRun(4, "/r", "do it", "", chunk="item-7") for started, said, stop in ((["item-9", "item-12"], "the chunk finished; started item-9, item-12", Stop(0, "")), ([], "the chunk finished; started no other, since the chunks running fill what may run now", Stop(0, "")), (Failure("no plan"), "", Stop(1, "the chunk finished, but the next could not start: no plan"))): - with self.subTest(started=started), unittest.mock.patch.object(main, "start_chunks", return_value=started) as start, contextlib.redirect_stdout(io.StringIO()) as printed: - loop = a_loop(FakeEffects().effects(), env={"LOOP_CLAUDE_X": "1"}) - self.assertEqual(main.handed_on(loop_run, loop), stop) + with self.subTest(started=started), contextlib.redirect_stdout(io.StringIO()) as printed: + loop, asked = a_loop(FakeEffects().effects(), env={"LOOP_CLAUDE_X": "1"}), [] + self.assertEqual(main.handed_on(loop_run, loop, lambda *args: asked.append(args) or started), stop) self.assertEqual(printed.getvalue(), f"{STAMP} {said}\n" if said else "") - self.assertEqual(start.call_args.args, (loop_run, True, loop.paths, loop.env, loop.launcher, loop.effects)) + self.assertEqual(asked, [(loop_run, True, loop.paths, loop.env, loop.launcher, loop.effects)]) class StopDms(unittest.TestCase): @@ -214,28 +229,33 @@ class StopDms(unittest.TestCase): code = main.main(argv, {}, paths.paths_from(pathlib.Path(home)), pathlib.Path(home, "loop-claude"), CWD, fake.effects()) return code, said.getvalue() + def exit_code(self, run, fake): + with contextlib.redirect_stderr(io.StringIO()) as said, contextlib.redirect_stdout(io.StringIO()): + code = main.exit_code(run, CWD, fake.effects()) + return code, said.getvalue() + + def raising(self, error): + raise error + def test_dms_the_reason_of_a_stop_with_exit_2_such_as_a_chunk_left_unfinished_too_often(self): fake = FakeEffects() - with unittest.mock.patch.object(main, "run_loop", return_value=Stop(2, "the human is needed to look at the chunk, but it stayed unfinished")): - self.assertEqual(self.run_main(["do it"], fake)[0], 2) + self.assertEqual(self.exit_code(lambda: Stop(2, "the human is needed to look at the chunk, but it stayed unfinished"), fake)[0], 2) self.assertEqual(fake.dms, [f"loop-claude in {CWD} stopped: the human is needed to look at the chunk, but it stayed unfinished"]) def test_skips_the_dm_of_a_stop_without_a_reason_as_once_the_windows_started(self): fake = FakeEffects() - with unittest.mock.patch.object(main, "run_loop", return_value=Stop(0, "")): - self.assertEqual(self.run_main(["do it"], fake), (0, "")) + self.assertEqual(self.exit_code(lambda: Stop(0, ""), fake), (0, "")) self.assertEqual(fake.dms, []) def test_skips_the_dm_where_ctrl_c_stopped_the_loop(self): fake = FakeEffects() - with unittest.mock.patch.object(main, "run_loop", side_effect=KeyboardInterrupt): - self.assertEqual(self.run_main(["do it"], fake)[0], 130) + self.assertEqual(self.exit_code(lambda: self.raising(KeyboardInterrupt()), fake)[0], 130) self.assertEqual(fake.dms, []) def test_dms_a_crash_before_it_reaches_the_terminal(self): fake = FakeEffects() - with unittest.mock.patch.object(main, "run_loop", side_effect=RuntimeError("boom")), self.assertRaises(RuntimeError): - self.run_main(["do it"], fake) + with self.assertRaises(RuntimeError): + self.exit_code(lambda: self.raising(RuntimeError("boom")), fake) self.assertEqual(fake.dms, [f"loop-claude in {CWD} crashed: RuntimeError('boom')"]) def test_dms_the_reason_where_a_loop_given_a_task_stops_with_exit_1(self): diff --git a/bin/loop_claude/loop/test_routing_check.py b/bin/loop_claude/loop/test_routing_check.py index 4c510b97..6a925611 100644 --- a/bin/loop_claude/loop/test_routing_check.py +++ b/bin/loop_claude/loop/test_routing_check.py @@ -6,7 +6,6 @@ import shutil import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) @@ -140,12 +139,14 @@ class RoutingCheck(unittest.TestCase): def test_puts_every_new_call_to_the_human_and_says_one_it_cannot_keep(self): new = [Call("reports", ("Explore",)), Call("rounds", ("x", "y"))] fake = FakeEffects(on_terminal_claude=[0]) - with contextlib.redirect_stderr(io.StringIO()) as said, unittest.mock.patch.object(state, "mark_raised", return_value=Failure("full")): + self.root.mkdir() + fake.replace_file = lambda path, data: Failure("full") + with contextlib.redirect_stderr(io.StringIO()) as said: self.assertIsNone(routing_check.raise_calls(self.root, new, Rung("m", "high"), self.loop(fake))) ((args),) = fake.made("claude_on_terminal") self.assertIn(f"don't change anything until they answer:\n- {new[0].sentence()}\n- {new[1].sentence()}\nThe figures", args[6]) self.assertIn(f"call for a routing change:\n- {new[0].sentence()}\n- {new[1].sentence()}\n", said.getvalue()) - self.assertIn("routing figures: full", said.getvalue()) + self.assertIn(f"routing figures: could not write {self.root / state.RAISED}: full", said.getvalue()) [sent] = fake.dms self.assertTrue(sent.startswith(f"loop-claude in {self.loop(fake).root} asks:\n\nloop-claude's routing figures call for a routing change:\n- {new[0].sentence()}\n"), sent) diff --git a/bin/loop_claude/loop/test_usage.py b/bin/loop_claude/loop/test_usage.py index a6da1337..af996f1d 100644 --- a/bin/loop_claude/loop/test_usage.py +++ b/bin/loop_claude/loop/test_usage.py @@ -59,8 +59,7 @@ class ExitContract(unittest.TestCase): ("chunk_driver/cycle_start.py", "project_routing", "1", "ladder_file.reason", premises_), ("chunk_driver/cycle_start.py", "routing_on", "1", "ladder.reason", "the project's routing ladder cannot be read or holds a fault"), ("chunk_driver/project/model_choice.py", "get_or_ask_model", "1", "models.reason", models_), - ("chunk_driver/project/model_choice.py", "get_or_ask_model", "1", "models.reason", models_), - ("chunk_driver/project/model_choice.py", "get_or_ask_model", "1", "f'could not save the model in", models_), + ("chunk_driver/project/model_choice.py", "get_or_ask_model", "1", "failure.reason", models_), ("loop_claude/loop/checkout.py", "checkout_root", "1", "found.reason", git), ("loop_claude/loop/main.py", "run_loop", "1", "reason", "tmux is missing or older than 3.2"), ("loop_claude/loop/main.py", "run_loop", "1", "f'{CHUNK_FLAG} is how loop-claude starts itself", "--chunk is given outside a tmux window"), diff --git a/bin/loop_effects/child_shells.py b/bin/loop_effects/child_shells.py index 07648b8a..ee18379a 100644 --- a/bin/loop_effects/child_shells.py +++ b/bin/loop_effects/child_shells.py @@ -7,8 +7,8 @@ import os import pathlib import signal import stat +import typing -from machine_load.slots import slot_folder # The command names of a shell claude starts for a Bash call. SHELL_NAMES = frozenset({"bash", "dash", "sh", "zsh"}) @@ -149,15 +149,14 @@ def holds_slot(pid: int, slots: pathlib.Path) -> bool: return any(has_slot(each, slots) for each in descendants(pid)) -def shells_of(claude_pid: int) -> list[Shell]: - """The shells that are children of claude_pid; none where /proc doesn't exist, as off Linux.""" +def shells_of(claude_pid: int, slots: pathlib.Path, children: typing.Callable[[int], list[int]]) -> list[Shell]: + """The shells that are children of claude_pid, which children lists as children_of does, each knowing whether it holds a heavy slot in the folder slots; none where /proc doesn't exist, as off Linux.""" if not os.path.isdir("/proc"): return [] - slots = slot_folder(os.environ, pathlib.Path.home()) booted = boot_time() ticks = os.sysconf("SC_CLK_TCK") shells = [] - for pid in children_of(claude_pid): + for pid in children(claude_pid): found = parent_and_start(pid) if found is None or found[2] not in SHELL_NAMES: continue diff --git a/bin/loop_effects/doubles.py b/bin/loop_effects/doubles.py index 802abab9..1fd91b7a 100644 --- a/bin/loop_effects/doubles.py +++ b/bin/loop_effects/doubles.py @@ -9,6 +9,7 @@ from loop_effects.effects import Effects from claude_code import entries from failure import Failure from machine_load import memory +from text_files.file_writes import replace_file from loop_effects.child_shells import Shell @@ -51,6 +52,8 @@ class FakeEffects: self.terminal_folders: list[str] = [] # Each timer started, as its delay and action, which a test runs where the timer fires. self.timers: list[tuple[float, typing.Callable[[], None]]] = [] + # What writes a file: the real writer, until a test sets one that fails. + self.replace_file: typing.Callable[[pathlib.Path, bytes], Failure | None] = replace_file def effects(self) -> Effects: return Effects( @@ -66,6 +69,7 @@ class FakeEffects: now=lambda: NOW, on_terminal=lambda: self.terminal, python=lambda args, cwd: self.answer("python", (args, cwd)), + replace_file=lambda path, data: self.replace_file(path, data), sleep=lambda seconds: self.calls.append(("sleep", seconds)), stream_claude=self.stream, timer=self.timer, diff --git a/bin/loop_effects/effects.py b/bin/loop_effects/effects.py index dd32f0e2..d7d011f8 100644 --- a/bin/loop_effects/effects.py +++ b/bin/loop_effects/effects.py @@ -20,6 +20,8 @@ from failure import Failure from git_cli import git_repo from machine_load.memory import Memory, read_memory from project_config.settings import Notifier +from text_files.file_writes import replace_file +from machine_load.slots import slot_folder # Seconds before a Python script loop-claude runs is stopped, so a hung one, such as chunk-preflight, cannot hold the loop. PYTHON_TIMEOUT = 600 @@ -62,6 +64,8 @@ class Effects: on_terminal: typing.Callable[[], bool] # Runs a Python script in a directory, as run_python does. python: typing.Callable[[list[str], str], subprocess.CompletedProcess[str]] + # Writes a file whole, as text_files.file_writes.replace_file does. + replace_file: typing.Callable[[pathlib.Path, bytes], Failure | None] sleep: typing.Callable[[float], None] stream_claude: typing.Callable[[list[str], str, dict[str, str]], Streaming] # Starts a timer, as start_timer does. @@ -96,7 +100,7 @@ class StreamingClaude: yield pending.decode("utf-8", errors="replace") def shells(self) -> list[child_shells.Shell]: - return child_shells.shells_of(self.proc.pid) + return child_shells.shells_of(self.proc.pid, slot_folder(os.environ, pathlib.Path.home()), child_shells.children_of) def stop(self, shell: child_shells.Shell) -> None: child_shells.stop(shell) @@ -105,13 +109,16 @@ class StreamingClaude: return self.proc.wait() -def system_effects(backends: typing.Mapping[Notifier, Backend[typing.Any]] = BACKENDS) -> Effects: - """The effects on this machine; `backends` are the notifiers loop-claude's DM can go through, where a test swaps in one that sends nowhere.""" +def system_effects(home: pathlib.Path, env: typing.Mapping[str, str], backends: typing.Mapping[Notifier, Backend[typing.Any]] = BACKENDS) -> Effects: + """The effects on this machine: the configs of its DM are under home, and tmux runs in env. + + A test hands in `backends`, the notifiers loop-claude's DM can go through, that send nowhere. + """ return Effects( claude=lambda args, cwd, stdin: subprocess.run(["claude", *args], capture_output=True, cwd=cwd, input=stdin, text=True), claude_on_terminal=claude_on_terminal, discard_typed=lambda: termios.tcflush(sys.stdin, termios.TCIFLUSH), - dm=lambda text, directory: notify(text, directory, pathlib.Path.home(), backends), + dm=lambda text, directory: notify(text, directory, home, backends), execv=os.execv, git=git_repo.git, input=input, @@ -120,11 +127,12 @@ def system_effects(backends: typing.Mapping[Notifier, Backend[typing.Any]] = BAC now=time.time, on_terminal=lambda: sys.stdin.isatty() and sys.stdout.isatty(), python=lambda args, cwd: run_python(args, cwd, PYTHON_TIMEOUT), + replace_file=replace_file, sleep=time.sleep, stream_claude=StreamingClaude, timer=start_timer, - tmux=run_tmux, - tmux_on_terminal=lambda args: subprocess.run(["tmux", *args]).returncode, + tmux=lambda args: run_tmux(args, env), + tmux_on_terminal=lambda args: subprocess.run(["tmux", *args], env=env).returncode, ) @@ -162,11 +170,11 @@ def start_timer(delay: float, action: typing.Callable[[], None]) -> typing.Calla return stop -def run_tmux(args: list[str]) -> subprocess.CompletedProcess[str]: - """Runs tmux and returns what it printed. A tmux that cannot start, as where none is installed, reads as exit 127, saying why on stderr.""" +def run_tmux(args: list[str], env: typing.Mapping[str, str] | None) -> subprocess.CompletedProcess[str]: + """Runs tmux in env, else in this process's environment, and returns what it printed. A tmux that cannot start, as where none is installed, reads as exit 127, saying why on stderr.""" command = ["tmux", *args] try: - return subprocess.run(command, capture_output=True, encoding="utf-8", errors="replace", stdin=subprocess.DEVNULL) + return subprocess.run(command, capture_output=True, encoding="utf-8", env=env, errors="replace", stdin=subprocess.DEVNULL) except OSError as failure: return subprocess.CompletedProcess(command, 127, "", f"could not start: {failure}") diff --git a/bin/loop_effects/test_effects.py b/bin/loop_effects/test_effects.py index 0f58638e..b788d399 100644 --- a/bin/loop_effects/test_effects.py +++ b/bin/loop_effects/test_effects.py @@ -10,7 +10,6 @@ import tempfile import threading import time import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -24,7 +23,7 @@ STAMP = f"<{NOW}>" def clocked(): """The system's effects on a clock stopped at NOW, which renders a time as .""" - return dataclasses.replace(system_effects(), local_time=lambda seconds: f"<{seconds}>", now=lambda: NOW) + return dataclasses.replace(system_effects(pathlib.Path.home(), os.environ), local_time=lambda seconds: f"<{seconds}>", now=lambda: NOW) def ticks(pid): @@ -78,13 +77,10 @@ class Said(unittest.TestCase): class LocalTime(unittest.TestCase): def test_renders_seconds_in_the_machines_zone(self): - # A POSIX zone two hours ahead of UTC, so a time in UTC would read 14:13:20. Cleanups run last in, first out: the zone is restored before time.tzset makes the clock read it again. - self.addCleanup(time.tzset) - zone = unittest.mock.patch.dict(os.environ, {"TZ": "XXX-2"}) - zone.start() - self.addCleanup(zone.stop) - time.tzset() - self.assertEqual(system_effects().local_time(NOW), "2026-09-21 16:13:20") + # A POSIX zone two hours ahead of UTC, so a time in UTC would read 14:13:20. A process reads its zone as it starts, so the clock runs in one of its own. + script = f"import os\nimport pathlib\nimport sys\nsys.path.insert(0, {str(pathlib.Path(__file__).resolve().parents[1])!r})\nfrom loop_effects.effects import system_effects\nprint(system_effects(pathlib.Path.home(), os.environ).local_time({NOW}))" + ran = subprocess.run([sys.executable, "-c", script], capture_output=True, check=True, env=os.environ | {"TZ": "XXX-2"}, text=True) + self.assertEqual(ran.stdout, "2026-09-21 16:13:20\n") @@ -98,7 +94,7 @@ class ClaudeOnTerminal(unittest.TestCase): # A Ctrl-C on the terminal reaches every process in its foreground. kill returns once the signal is pending in the test's process, before claude exits and claude_on_terminal returns. claude.write_text(f"#!{sys.executable}\nimport os, signal, sys\nos.kill(os.getppid(), signal.SIGINT)\nsys.exit(3)\n") claude.chmod(0o755) - code = system_effects().claude_on_terminal([], folder, {"PATH": folder}) + code = system_effects(pathlib.Path.home(), os.environ).claude_on_terminal([], folder, {"PATH": folder}) self.assertEqual((code, caught, signal.getsignal(signal.SIGINT)), (3, [], before)) @@ -110,27 +106,28 @@ class TmuxEffect(unittest.TestCase): tmux = pathlib.Path(folder, "tmux") tmux.write_text(f"#!{sys.executable}\nimport sys\nprint(' '.join(sys.argv[1:]))\nsys.exit(4)\n") tmux.chmod(0o755) - return unittest.mock.patch.dict(os.environ, {"PATH": folder, "TMUX": "", "TMUX_PANE": ""}) + return system_effects(pathlib.Path.home(), os.environ | {"PATH": folder, "TMUX": "", "TMUX_PANE": ""}) def test_runs_tmux_and_returns_what_it_printed(self): - with tempfile.TemporaryDirectory() as folder, self.fake_tmux(folder): - done = system_effects().tmux(["list-windows", "-t", "=loop-proj"]) + with tempfile.TemporaryDirectory() as folder: + done = self.fake_tmux(folder).tmux(["list-windows", "-t", "=loop-proj"]) self.assertEqual((done.returncode, done.stdout), (4, "list-windows -t =loop-proj\n")) def test_reads_output_that_is_not_utf_8_with_each_bad_byte_replaced(self): - with tempfile.TemporaryDirectory() as folder, self.fake_tmux(folder): + with tempfile.TemporaryDirectory() as folder: + effects = self.fake_tmux(folder) pathlib.Path(folder, "tmux").write_text(f"#!{sys.executable}\nimport sys\nsys.stdout.buffer.write(b'caf\\xe9')\n") - self.assertEqual(system_effects().tmux(["list-windows"]).stdout, "caf\ufffd") + self.assertEqual(effects.tmux(["list-windows"]).stdout, "caf\ufffd") def test_reads_a_tmux_that_cannot_start_as_exit_127(self): - with tempfile.TemporaryDirectory() as folder, unittest.mock.patch.dict(os.environ, {"PATH": folder}): - done = system_effects().tmux(["-V"]) + with tempfile.TemporaryDirectory() as folder: + done = system_effects(pathlib.Path.home(), os.environ | {"PATH": folder}).tmux(["-V"]) self.assertEqual((done.args, done.returncode, done.stdout), (["tmux", "-V"], 127, "")) self.assertIn("could not start", done.stderr) def test_runs_tmux_on_the_terminal_and_returns_its_exit_code(self): - with tempfile.TemporaryDirectory() as folder, self.fake_tmux(folder), contextlib.redirect_stdout(io.StringIO()): - self.assertEqual(system_effects().tmux_on_terminal(["attach-session", "-t", "=loop-proj"]), 4) + with tempfile.TemporaryDirectory() as folder, contextlib.redirect_stdout(io.StringIO()): + self.assertEqual(self.fake_tmux(folder).tmux_on_terminal(["attach-session", "-t", "=loop-proj"]), 4) class PythonEffect(unittest.TestCase): @@ -169,8 +166,8 @@ class ChildShells(unittest.TestCase): os.killpg(proc.pid, signal.SIGKILL) proc.wait() - def shell_of(self, proc): - [found] = [each for each in child_shells.shells_of(os.getpid()) if each.pid == proc.pid] + def shell_of(self, proc, slots=pathlib.Path("/no-slots")): + [found] = [each for each in child_shells.shells_of(os.getpid(), slots, child_shells.children_of) if each.pid == proc.pid] return found def test_reads_a_child_shells_command_and_whether_a_busy_tool_runs_below_it(self): @@ -189,12 +186,12 @@ class ChildShells(unittest.TestCase): def test_a_shell_below_which_a_process_holds_a_heavy_slot_file_holds_a_slot_and_runs_no_busy_tool(self): # run-heavy replaces itself with its command, which keeps the slot's file open. - with tempfile.TemporaryDirectory() as state, unittest.mock.patch.dict(os.environ, {"XDG_STATE_HOME": state}): + with tempfile.TemporaryDirectory() as state: slot = pathlib.Path(state, "run-heavy", "slot-0") slot.parent.mkdir() holding = self.started(f"exec 9>{slot}; sleep 60; true") plain = self.started("sleep 60; true") - self.assertEqual([(self.shell_of(each).holds_slot, self.shell_of(each).runs_busy_tool) for each in (holding, plain)], [(True, False), (False, False)]) + self.assertEqual([(self.shell_of(each, slot.parent).holds_slot, self.shell_of(each, slot.parent).runs_busy_tool) for each in (holding, plain)], [(True, False), (False, False)]) def test_a_shell_written_output_is_the_modified_time_of_its_output_file_and_else_its_start(self): with tempfile.NamedTemporaryFile() as output: @@ -264,8 +261,8 @@ class ChildShells(unittest.TestCase): self.assertFalse(child_shells.has_slot(2**30, pathlib.Path("/"))) shell = self.started("sleep 60; true") real = child_shells.children_of - with unittest.mock.patch.object(child_shells, "children_of", side_effect=lambda pid: [2**30, shell.pid] if pid == os.getpid() else real(pid)): - self.assertEqual([each.pid for each in child_shells.shells_of(os.getpid())], [shell.pid]) + children = lambda pid: [2**30, shell.pid] if pid == os.getpid() else real(pid) + self.assertEqual([each.pid for each in child_shells.shells_of(os.getpid(), pathlib.Path("/no-slots"), children)], [shell.pid]) def test_command_of_takes_the_text_of_the_last_eval_wrapper_and_else_the_whole_line(self): wrapped = ["/bin/bash", "-c", "source /s && eval 'echo a && eval b < /dev/null' < /dev/null && pwd -P >| /t"] @@ -330,7 +327,7 @@ class Timer(unittest.TestCase): self.assertTrue(ran.wait(10)) def test_the_systems_timer_runs_on_a_daemon_thread_so_a_loop_that_exits_while_it_waits_does_not_wait_for_it(self): - self.assertIs(system_effects().timer, start_timer) + self.assertIs(system_effects(pathlib.Path.home(), os.environ).timer, start_timer) before = set(threading.enumerate()) stop = start_timer(60, lambda: None) self.addCleanup(stop) diff --git a/bin/machine_load/slots.py b/bin/machine_load/slots.py index 9b2e9d64..1b8b77f7 100644 --- a/bin/machine_load/slots.py +++ b/bin/machine_load/slots.py @@ -2,7 +2,6 @@ import fcntl import os import pathlib -import time import typing CPUS_PER_SLOT = 4 @@ -26,12 +25,16 @@ def slot_folder(env: typing.Mapping[str, str], home: pathlib.Path) -> pathlib.Pa return pathlib.Path(state) / "run-heavy" -def taken_slot(folder: pathlib.Path, slots: int) -> tuple[int, int] | None: - """The number and open file of the lowest slot this call locked; None while every slot is held. Raises OSError where a slot file cannot be opened or locked.""" +# Locks an open file, as fcntl.flock does. +Lock = typing.Callable[[int, int], None] + + +def taken_slot(folder: pathlib.Path, slots: int, lock: Lock) -> tuple[int, int] | None: + """The number and open file of the lowest slot this call locked, through lock; None while every slot is held. Raises OSError where a slot file cannot be opened or locked.""" for slot in range(slots): fd = os.open(folder / f"slot-{slot}", os.O_RDWR | os.O_CREAT, 0o644) try: - fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) + lock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) except BlockingIOError: os.close(fd) continue @@ -42,10 +45,10 @@ def taken_slot(folder: pathlib.Path, slots: int) -> tuple[int, int] | None: return None -def waited_slot(folder: pathlib.Path, slots: int, say: typing.Callable[[], None], sleep: typing.Callable[[float], None] = time.sleep) -> tuple[int, int]: - """The number and open file of a slot this call locked, once one frees. While every slot is held, it calls say once and tries again each POLL_SECONDS.""" +def waited_slot(folder: pathlib.Path, slots: int, say: typing.Callable[[], None], sleep: typing.Callable[[float], None], lock: Lock) -> tuple[int, int]: + """The number and open file of a slot this call locked, through lock, once one frees. While every slot is held, it calls say once and tries again each POLL_SECONDS.""" said = False - while (taken := taken_slot(folder, slots)) is None: + while (taken := taken_slot(folder, slots, lock)) is None: if not said: say() said = True diff --git a/bin/machine_load/test_machine_load.py b/bin/machine_load/test_machine_load.py index 9f434a9a..d79b2568 100644 --- a/bin/machine_load/test_machine_load.py +++ b/bin/machine_load/test_machine_load.py @@ -5,7 +5,6 @@ import pathlib import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -57,13 +56,19 @@ class SlotFolder(unittest.TestCase): class TakenSlot(unittest.TestCase): def test_a_lock_that_fails_for_another_reason_than_a_holder_closes_its_file_and_raises(self): - closed = [] - with tempfile.TemporaryDirectory() as folder, unittest.mock.patch("fcntl.flock", side_effect=OSError(errno.ENOLCK, "No locks available")), unittest.mock.patch("os.close", side_effect=closed.append): + locked = [] + + def lock(fd, operation): + locked.append(fd) + raise OSError(errno.ENOLCK, "No locks available") + + with tempfile.TemporaryDirectory() as folder: with self.assertRaises(OSError): - slots.taken_slot(pathlib.Path(folder), 2) - self.assertEqual(len(closed), 1) - self.assertIsInstance(closed[0], int) - os.close(closed[0]) + slots.taken_slot(pathlib.Path(folder), 2, lock) + self.assertEqual(len(locked), 1) + with self.assertRaises(OSError) as closed: + os.fstat(locked[0]) + self.assertEqual(closed.exception.errno, errno.EBADF) class WaitedSlot(unittest.TestCase): @@ -79,14 +84,14 @@ class WaitedSlot(unittest.TestCase): if len(slept) == 2: held[1].close() - slot, fd = slots.waited_slot(pathlib.Path(folder), 2, lambda: said.append(True), sleep) + slot, fd = slots.waited_slot(pathlib.Path(folder), 2, lambda: said.append(True), sleep, fcntl.flock) os.close(fd) held[0].close() self.assertEqual((slot, said, slept), (1, [True], [slots.POLL_SECONDS] * 2)) def test_takes_a_free_slot_at_once(self): with tempfile.TemporaryDirectory() as folder: - slot, fd = slots.waited_slot(pathlib.Path(folder), 2, lambda: self.fail("said it waits"), lambda seconds: self.fail("slept")) + slot, fd = slots.waited_slot(pathlib.Path(folder), 2, lambda: self.fail("said it waits"), lambda seconds: self.fail("slept"), fcntl.flock) os.close(fd) self.assertEqual(slot, 0) diff --git a/bin/mutate-changes b/bin/mutate-changes index 648ad8e8..855b131b 100755 --- a/bin/mutate-changes +++ b/bin/mutate-changes @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import os import pathlib import signal import sys @@ -11,4 +12,4 @@ from mutate_changes import main # SystemExit, the run unwinds as on Ctrl-C. for number in (signal.SIGHUP, signal.SIGTERM): signal.signal(number, lambda number, frame: sys.exit(128 + number)) -sys.exit(main.main(sys.argv[1:])) +sys.exit(main.main(sys.argv[1:], os.environ)) diff --git a/bin/mutate_changes/in_diff_tools.py b/bin/mutate_changes/in_diff_tools.py index 27f98db5..fbe9611a 100644 --- a/bin/mutate_changes/in_diff_tools.py +++ b/bin/mutate_changes/in_diff_tools.py @@ -3,16 +3,16 @@ import os import pathlib from mutate_changes.changed_lines import by_folder, label, runs -from mutate_changes.tool_process import failed, Ran, remove, run_tool, scratch_folder +from mutate_changes.tool_process import failed, Ran, remove, Run, scratch_folder from failure import Failure -from git_cli.git_repo import git +from git_cli.git_repo import Git # cargo-mutants' exit codes for a run that finished: every mutant caught, some missed, and some timed out. CARGO_FINISHED = frozenset({0, 2, 3}) CARGO_OUTCOMES = ("caught", "missed", "timeout", "unviable") -def run_cargo_mutants(top: str, base: str, paths: list[str], prefix: str | None, jobs: int) -> list[str] | Failure: +def run_cargo_mutants(top: str, base: str, paths: list[str], run: Run, git: Git, jobs: int) -> list[str] | Failure: """A line with cargo-mutants' mutant count, then a line per mutant it missed or that timed out, as `::: : `. cargo-mutants runs in top.""" @@ -26,7 +26,7 @@ def run_cargo_mutants(top: str, base: str, paths: list[str], prefix: str | None, try: pathlib.Path(scratch, "branch.diff").write_text(diff, encoding="utf-8") relative = os.path.relpath(scratch, top) - ran = run_tool(["cargo", "mutants", "--in-diff", os.path.join(relative, "branch.diff"), "--output", relative, "--jobs", str(jobs)], top, prefix) + ran = run(["cargo", "mutants", "--in-diff", os.path.join(relative, "branch.diff"), "--output", relative, "--jobs", str(jobs)], top) if isinstance(ran, Failure) or ran.code not in CARGO_FINISHED: return ran if isinstance(ran, Failure) else failed("cargo mutants", ran) outcomes = {outcome: outcome_lines(pathlib.Path(scratch, "mutants.out", f"{outcome}.txt")) for outcome in CARGO_OUTCOMES} @@ -44,7 +44,7 @@ def outcome_lines(path: pathlib.Path) -> list[str]: # Stryker and Infection also exit nonzero on a mutation score below a threshold the project sets, which their exit code doesn't # tell from a run that broke, so either fails the run. -def run_stryker(top: str, changed: dict[str, frozenset[int]], prefix: str | None, jobs: int) -> list[str] | Failure: +def run_stryker(top: str, changed: dict[str, frozenset[int]], run: Run, jobs: int) -> list[str] | Failure: """For each folder Stryker runs in, a line naming it, then Stryker's own report of the mutants on the changed lines. Last, a line naming the files with no package.json above them, if any. @@ -58,16 +58,16 @@ def run_stryker(top: str, changed: dict[str, frozenset[int]], prefix: str | None unplaced = sorted(files) continue mutate = ",".join(f"{path}:{first}-{last}" for path, lines in sorted(files.items()) for first, last in runs(lines)) - ran = run_tool(["npx", "--no", "--package=@stryker-mutator/core", "stryker", "run", "--mutate", mutate, "--concurrency", str(jobs), "--reporters", "clear-text"], os.path.join(top, folder), prefix) + ran = run(["npx", "--no", "--package=@stryker-mutator/core", "stryker", "run", "--mutate", mutate, "--concurrency", str(jobs), "--reporters", "clear-text"], os.path.join(top, folder)) if isinstance(ran, Failure) or ran.code: return ran if isinstance(ran, Failure) else whole_output(f"Stryker in {label(folder)}", ran) report.extend([f"Stryker in {label(folder)}:", *ran.output.splitlines()]) return [*report, *([f"Not mutated, as no package.json is above them: {', '.join(unplaced)}."] if unplaced else [])] -def run_infection(top: str, base: str, prefix: str | None, jobs: int) -> list[str] | Failure: +def run_infection(top: str, base: str, run: Run, jobs: int) -> list[str] | Failure: """A line naming where Infection ran, top, then its own report of the mutants on the lines the branch changes since base.""" - ran = run_tool(["vendor/bin/infection", "--git-diff-lines", f"--git-diff-base={base}", f"--threads={jobs}", "--show-mutations=max", "--no-progress"], top, prefix) + ran = run(["vendor/bin/infection", "--git-diff-lines", f"--git-diff-base={base}", f"--threads={jobs}", "--show-mutations=max", "--no-progress"], top) if isinstance(ran, Failure) or ran.code: return ran if isinstance(ran, Failure) else whole_output("Infection", ran) return ["Infection in the checkout's top:", *ran.output.splitlines()] diff --git a/bin/mutate_changes/main.py b/bin/mutate_changes/main.py index 1da6b37b..d4afaec5 100644 --- a/bin/mutate_changes/main.py +++ b/bin/mutate_changes/main.py @@ -42,22 +42,28 @@ Exit 1 too when git fails, a file cannot be read or copied, a tool cannot start, Infection also exit nonzero on a mutation score below a threshold the project sets, which fails the run as well and prints their whole report. """ +import functools +import os import sys import time +import typing from mutate_changes.changed_lines import added_lines, not_mutated, tool_for from machine_load.slots import CPUS_PER_SLOT -from git_cli.git_repo import default_ref, git, top +from git_cli.git_repo import default_ref, Git, top from failure import Failure +from git_cli import git_repo from mutate_changes.in_diff_tools import run_cargo_mutants, run_infection, run_stryker from mutate_changes.mutmut_tool import run_mutmut +from mutate_changes.tool_process import run_tool USAGE = "usage: mutate-changes [--base ] [--prefix ] [--jobs ]" # A panel of three readers, seated as the README's audience, picked the command's name and these options on 2026-11-11. OPTIONS = ("--base", "--jobs", "--prefix") -def main(argv: list[str]) -> int: +def main(argv: list[str], env: typing.Mapping[str, str]) -> int: + """Runs mutate-changes with the arguments argv; git and each tool run in the environment env.""" if argv in (["-h"], ["--help"]): print(__doc__.strip()) return 0 @@ -66,7 +72,7 @@ def main(argv: list[str]) -> int: print(f"{given.reason}; {USAGE}", file=sys.stderr) return 1 started = time.monotonic() - report = mutate(given) + report = mutate(given, env) if isinstance(report, Failure): print(report.reason, file=sys.stderr) return 1 @@ -89,12 +95,13 @@ def options(argv: list[str]) -> dict[str, str] | Failure: return given -def mutate(given: dict[str, str]) -> list[str] | Failure: +def mutate(given: dict[str, str], env: typing.Mapping[str, str]) -> list[str] | Failure: """Each tool's report on the lines the branch adds, then the lines naming the changed files no tool mutates.""" - checkout = top() + git = functools.partial(git_repo.git, env=env) + checkout = top(git) if isinstance(checkout, Failure): return checkout - base = given.get("--base") or default_ref(cwd=checkout) + base = given.get("--base") or default_ref(cwd=checkout, run=git) if isinstance(base, Failure): return base found = git("merge-base", base, "HEAD", cwd=checkout) @@ -105,27 +112,28 @@ def mutate(given: dict[str, str]) -> list[str] | Failure: if isinstance(names, Failure): return names paths = list(filter(None, names.split("\0"))) - changed = changed_by_tool(checkout, merge_base, paths) + changed = changed_by_tool(checkout, merge_base, paths, git) if isinstance(changed, Failure): return changed - prefix, jobs = given.get("--prefix"), int(given.get("--jobs", CPUS_PER_SLOT)) + jobs = int(given.get("--jobs", CPUS_PER_SLOT)) + run = functools.partial(run_tool, prefix=given.get("--prefix"), env=env, killpg=os.killpg) report = [] if changed else ["No tool ran: the branch adds no line to a file that a tool mutates."] for tool, files in sorted(changed.items()): if tool == "cargo-mutants": - lines = run_cargo_mutants(checkout, merge_base, sorted(files), prefix, jobs) + lines = run_cargo_mutants(checkout, merge_base, sorted(files), run, git, jobs) elif tool == "infection": - lines = run_infection(checkout, merge_base, prefix, jobs) + lines = run_infection(checkout, merge_base, run, jobs) elif tool == "stryker": - lines = run_stryker(checkout, files, prefix, jobs) + lines = run_stryker(checkout, files, run, jobs) else: - lines = run_mutmut(checkout, files, prefix, jobs) + lines = run_mutmut(checkout, files, run, git, jobs) if isinstance(lines, Failure): return lines report.extend(lines) return [*report, *not_mutated(paths)] -def changed_by_tool(checkout: str, merge_base: str, paths: list[str]) -> dict[str, dict[str, frozenset[int]]] | Failure: +def changed_by_tool(checkout: str, merge_base: str, paths: list[str], git: Git) -> dict[str, dict[str, frozenset[int]]] | Failure: """Each tool's files among paths, by path from the checkout's top, with the lines the working tree adds since merge_base.""" changed: dict[str, dict[str, frozenset[int]]] = {} for path in paths: diff --git a/bin/mutate_changes/mutmut_tool.py b/bin/mutate_changes/mutmut_tool.py index d49f2b19..f1f67c08 100644 --- a/bin/mutate_changes/mutmut_tool.py +++ b/bin/mutate_changes/mutmut_tool.py @@ -11,10 +11,10 @@ import shutil import tomllib from mutate_changes.changed_lines import by_folder, label -from mutate_changes.tool_process import failed, Ran, remove, run_tool, scratch_folder +from mutate_changes.tool_process import failed, Ran, remove, Run, scratch_folder from failure import Failure from mutate_changes.mutmut_mutants import function_keys, mutant_globs, mutated_line, named, statuses -from git_cli.git_repo import git +from git_cli.git_repo import Git from mutate_changes.mutmut_setup import left_out, mutmut_config CAUGHT = frozenset({"caught by type check", "killed"}) @@ -42,20 +42,20 @@ class Folder: return label(self.path) -def run_mutmut(top: str, changed: dict[str, frozenset[int]], prefix: str | None, jobs: int) -> list[str] | Failure: +def run_mutmut(top: str, changed: dict[str, frozenset[int]], run: Run, git: Git, jobs: int) -> list[str] | Failure: """For each folder mutmut runs in, a line on what it mutated and left out, then a line per mutant on a changed line that its tests don't kill. changed maps each file, by its path from top, to its changed lines.""" report: list[str] = [] for path, files in sorted(by_folder(top, changed, CONFIGS).items()): - lines = mutate_folder(top, path, files, prefix, jobs) + lines = mutate_folder(top, path, files, run, git, jobs) if isinstance(lines, Failure): return lines report.extend(lines) return report -def mutate_folder(top: str, path: str, files: dict[str, frozenset[int]], prefix: str | None, jobs: int) -> list[str] | Failure: +def mutate_folder(top: str, path: str, files: dict[str, frozenset[int]], run: Run, git: Git, jobs: int) -> list[str] | Failure: root = os.path.join(top, path) folder = read_folder(root, path, files) if isinstance(folder, Failure): @@ -72,7 +72,7 @@ def mutate_folder(top: str, path: str, files: dict[str, frozenset[int]], prefix: try: copy = os.path.join(scratch, "copy") copied = copy_files(root, listed.split("\0"), copy) - return mutate_copy(copy, copied, folder, ["mutmut", "run", "--max-children", str(jobs), *globs], prefix) + return mutate_copy(copy, copied, folder, ["mutmut", "run", "--max-children", str(jobs), *globs], run) except OSError as failure: return Failure(f"could not set mutmut up in {scratch}, a copy of {root}: {failure}") finally: @@ -122,8 +122,8 @@ def copy_files(root: str, paths: list[str], copy: str) -> list[str]: return sorted(copied) -def mutate_copy(copy: str, copied: list[str], folder: Folder, argv: list[str], prefix: str | None) -> list[str] | Failure: - ran = run_until_stats_pass(copy, copied, folder, argv, prefix) +def mutate_copy(copy: str, copied: list[str], folder: Folder, argv: list[str], run: Run) -> list[str] | Failure: + ran = run_until_stats_pass(copy, copied, folder, argv, run) if isinstance(ran, Failure): return ran output, left = ran @@ -131,7 +131,7 @@ def mutate_copy(copy: str, copied: list[str], folder: Folder, argv: list[str], p return Failure(f"mutmut in {folder.label} wrote no mutants/ in {copy}; --prefix must run it in $PWD") if NO_MUTANT in output: return [f"mutmut in {folder.label}: none of the functions the changed lines fall in has a mutant."] - results = run_tool(["mutmut", "results", "--all", "true"], copy, prefix) + results = run(["mutmut", "results", "--all", "true"], copy) if isinstance(results, Failure) or results.code: return results if isinstance(results, Failure) else failed(f"mutmut results in {folder.label}", results) shown, counted = not_killed(folder, statuses(results.output), copy) @@ -141,14 +141,14 @@ def mutate_copy(copy: str, copied: list[str], folder: Folder, argv: list[str], p return [f"mutmut in {folder.label}: {counted} mutants on changed lines, {len(shown)} not killed.{unreached}{left_note}", *shown] -def run_until_stats_pass(copy: str, copied: list[str], folder: Folder, argv: list[str], prefix: str | None) -> tuple[str, list[str]] | Failure: +def run_until_stats_pass(copy: str, copied: list[str], folder: Folder, argv: list[str], run: Run) -> tuple[str, list[str]] | Failure: """mutmut's output, and the options that left out each test that fails before any mutant: mutmut runs no mutant while one fails.""" - ran = configured_run(copy, copied, folder, [], argv, prefix) + ran = configured_run(copy, copied, folder, [], argv, run) left: list[str] = [] if isinstance(ran, Ran) and STATS_FAILED in ran.output: - ran = configured_run(copy, copied, folder, LIST_FAILURES, argv, prefix) + ran = configured_run(copy, copied, folder, LIST_FAILURES, argv, run) if isinstance(ran, Ran) and STATS_FAILED in ran.output and (left := left_out(ran.output)): - ran = configured_run(copy, copied, folder, left, argv, prefix) + ran = configured_run(copy, copied, folder, left, argv, run) if isinstance(ran, Failure): return ran if ran.code and NO_MUTANT not in ran.output and NO_TEST not in ran.output: @@ -156,12 +156,12 @@ def run_until_stats_pass(copy: str, copied: list[str], folder: Folder, argv: lis return ran.output, left -def configured_run(copy: str, copied: list[str], folder: Folder, options: list[str], argv: list[str], prefix: str | None) -> Ran | Failure: +def configured_run(copy: str, copied: list[str], folder: Folder, options: list[str], argv: list[str], run: Run) -> Ran | Failure: config = mutmut_config(folder.setup, sorted(folder.files), copied, options) if isinstance(config, Failure): return Failure(f"{folder.label}: {config.reason}") pathlib.Path(copy, "setup.cfg").write_text(config, encoding="utf-8") - return run_tool(argv, copy, prefix) + return run(argv, copy) def not_killed(folder: Folder, found: dict[str, str], copy: str) -> tuple[list[str], int]: diff --git a/bin/mutate_changes/test_mutate_changes.py b/bin/mutate_changes/test_mutate_changes.py index 3e9eeb68..59c3b01e 100644 --- a/bin/mutate_changes/test_mutate_changes.py +++ b/bin/mutate_changes/test_mutate_changes.py @@ -8,8 +8,8 @@ import tempfile import textwrap import threading import time +import typing import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -226,8 +226,8 @@ class ToolProcess(unittest.TestCase): self.assertTrue(pathlib.Path(folder, "stopped").is_file()) def test_an_interrupted_tool_that_ignores_sigint_is_killed_after_the_grace_period(self): - with tempfile.TemporaryDirectory() as folder, unittest.mock.patch("mutate_changes.tool_process.GRACE_SECONDS", 0.5): - self.assertLess(self.interrupted(folder, "trap '' INT; echo $$ > started; sleep 60;"), 30) + with tempfile.TemporaryDirectory() as folder: + self.assertLess(self.interrupted(folder, "trap '' INT; echo $$ > started; sleep 60;", grace=0.5), 30) self.assert_ends(int(pathlib.Path(folder, "started").read_text())) def test_an_interrupt_ends_the_run_where_the_tool_s_processes_end_first(self): @@ -237,10 +237,10 @@ class ToolProcess(unittest.TestCase): killpg(group, number) raise ProcessLookupError - with unittest.mock.patch("os.killpg", then_gone), tempfile.TemporaryDirectory() as folder: - self.interrupted(folder, "sleep 60 & echo $! > started; wait;") + with tempfile.TemporaryDirectory() as folder: + self.interrupted(folder, "sleep 60 & echo $! > started; wait;", killpg=then_gone) - def interrupted(self, folder: str, script: str) -> float: + def interrupted(self, folder: str, script: str, **seams: typing.Any) -> float: """Run script as the prefix of a tool in folder, interrupt run_tool once script writes folder/started, and return how long run_tool took.""" started = pathlib.Path(folder, "started") @@ -252,7 +252,7 @@ class ToolProcess(unittest.TestCase): threading.Thread(target=interrupt, daemon=True).start() began = time.monotonic() with self.assertRaises(KeyboardInterrupt): - run_tool(["true"], folder, script) + run_tool(["true"], folder, **{"prefix": script, "env": os.environ, "killpg": os.killpg, **seams}) return time.monotonic() - began def assert_ends(self, pid: int) -> None: diff --git a/bin/mutate_changes/test_mutate_changes_runs.py b/bin/mutate_changes/test_mutate_changes_runs.py index 31454165..610c38e6 100644 --- a/bin/mutate_changes/test_mutate_changes_runs.py +++ b/bin/mutate_changes/test_mutate_changes_runs.py @@ -1,5 +1,6 @@ """mutate-changes run on a branch, against fake mutation tools that log how they were called.""" import contextlib +import functools import io import os import pathlib @@ -11,14 +12,15 @@ import sys import tempfile import time import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from mutate_changes.test_mutate_changes import ended +from git_cli import git_repo from test_env import isolated_env from mutate_changes import main from mutate_changes.in_diff_tools import run_cargo_mutants +from mutate_changes.tool_process import run_tool TOOL = pathlib.Path(__file__).resolve().parents[1] / "mutate-changes" SETUP = "[mutmut]\ndo_not_mutate = *_old.py\n" @@ -138,8 +140,8 @@ class MutateChangesRuns(unittest.TestCase): before = os.getcwd() os.chdir(cwd or self.checkout) try: - with unittest.mock.patch.dict(os.environ, {**isolated_env(), **(env or {})}, clear=True), contextlib.redirect_stdout(io.StringIO()) as out, contextlib.redirect_stderr(io.StringIO()) as err: - code = main.main(list(args)) + with contextlib.redirect_stdout(io.StringIO()) as out, contextlib.redirect_stderr(io.StringIO()) as err: + code = main.main(list(args), {**isolated_env(), **(env or {})}) finally: os.chdir(before) return subprocess.CompletedProcess(args, code, out.getvalue(), err.getvalue()) @@ -361,8 +363,10 @@ class MutateChangesRuns(unittest.TestCase): (self.checkout / "-x.py").write_text("def f():\n\treturn 1\n") (self.checkout / "-x.rs").write_text("fn f() {}\n") base = self.git("rev-parse", "HEAD").strip() - self.assertEqual(main.changed_by_tool(str(self.checkout), base, ["-x.py", "-x.rs"]), {"cargo-mutants": {"-x.rs": frozenset({1})}, "mutmut": {"-x.py": frozenset({1, 2})}}) - ran = run_cargo_mutants(str(self.checkout), base, ["-x.rs"], f"python3 {self.temp / 'fake_tools.py'} {self.log} 0", 1) + git = functools.partial(git_repo.git, env=isolated_env()) + self.assertEqual(main.changed_by_tool(str(self.checkout), base, ["-x.py", "-x.rs"], git), {"cargo-mutants": {"-x.rs": frozenset({1})}, "mutmut": {"-x.py": frozenset({1, 2})}}) + run = functools.partial(run_tool, prefix=f"python3 {self.temp / 'fake_tools.py'} {self.log} 0", env=isolated_env(), killpg=os.killpg) + ran = run_cargo_mutants(str(self.checkout), base, ["-x.rs"], run, git, 1) self.assertIsInstance(ran, list) self.assertIn("+++ b/-x.rs\n", self.log.read_text()) diff --git a/bin/mutate_changes/tool_process.py b/bin/mutate_changes/tool_process.py index c5ce0023..674380a5 100644 --- a/bin/mutate_changes/tool_process.py +++ b/bin/mutate_changes/tool_process.py @@ -1,7 +1,6 @@ """Start a mutation tool, through --prefix's shell text where the run gives one, and wait for it; and make and remove its scratch folder.""" import contextlib import dataclasses -import os import pathlib import shlex import shutil @@ -9,6 +8,7 @@ import signal import subprocess import sys import tempfile +import typing from failure import Failure @@ -21,14 +21,21 @@ class Ran: output: str # stdout and stderr, as the tool interleaved them -def run_tool(argv: list[str], cwd: str, prefix: str | None) -> Ran | Failure: - """Run a tool in cwd, reading each byte of its output that is not UTF-8 as U+FFFD. +class Run(typing.Protocol): + """Runs a tool in cwd as `run_tool` does, with the run's prefix, environment and killpg bound.""" + + def __call__(self, argv: list[str], cwd: str) -> Ran | Failure: ... + + +# The tests hand in a shorter grace, and a killpg that finds the tool's processes gone. +def run_tool(argv: list[str], cwd: str, *, prefix: str | None, env: typing.Mapping[str, str], killpg: typing.Callable[[int, int], None], grace: float = GRACE_SECONDS) -> Ran | Failure: + """Run a tool in cwd with the environment env, reading each byte of its output that is not UTF-8 as U+FFFD. With a prefix, sh runs ` "$@"` with argv as its arguments, so the prefix keeps its own quoting and each of argv stays one word.""" command = ["sh", "-c", f'{prefix} "$@"', "sh", *argv] if prefix else argv try: # In a session of its own, the tool and each process it starts share one process group, which ends whole below. - process = subprocess.Popen(command, cwd=cwd, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8", errors="replace", start_new_session=True) + process = subprocess.Popen(command, cwd=cwd, env=env, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8", errors="replace", start_new_session=True) except OSError as failure: return Failure(f"{shlex.join(argv)} could not start: {failure.strerror}") try: @@ -38,12 +45,12 @@ def run_tool(argv: list[str], cwd: str, prefix: str | None) -> Ran | Failure: # reaches only this process, so pass the tool SIGINT first: a container's CLI, such as docker compose, then stops its container. try: with contextlib.suppress(ProcessLookupError): - os.killpg(process.pid, signal.SIGINT) + killpg(process.pid, signal.SIGINT) with contextlib.suppress(subprocess.TimeoutExpired): - process.wait(timeout=GRACE_SECONDS) + process.wait(timeout=grace) finally: with contextlib.suppress(ProcessLookupError): - os.killpg(process.pid, signal.SIGKILL) + killpg(process.pid, signal.SIGKILL) process.wait() raise return Ran(process.returncode, output) diff --git a/bin/notifier/mattermost.py b/bin/notifier/mattermost.py index 85ed5683..f2b8c6aa 100644 --- a/bin/notifier/mattermost.py +++ b/bin/notifier/mattermost.py @@ -1,10 +1,11 @@ """Sends a direct message from a Mattermost bot to one user, through Mattermost's REST API v4. The `notifier` setting `mattermost` picks it.""" import dataclasses import pathlib +import typing import urllib.parse from failure import Failure -from rest_client.json_request import Http +from rest_client.json_request import Http, MessageOf from rest_client import json_request from rest_client.env_file import read_env @@ -53,9 +54,10 @@ def id_of(answer: object, request: str) -> str | Failure: return found if isinstance(found, str) and found else Failure(f"{request} answered without an id") -def http_json(method: str, url: str, token: str, body: object) -> object: +# request is json_request.http_json, but in the test of what this hands it. +def http_json(method: str, url: str, token: str, body: object, request: typing.Callable[[str, str, str, object, int, MessageOf], object] = json_request.http_json) -> object: """The JSON that url answers to the bot's request, None where the answer has no body, or a Failure saying why there is none.""" - return json_request.http_json(method, url, f"Bearer {token}", body, TIMEOUT, error_message) + return request(method, url, f"Bearer {token}", body, TIMEOUT, error_message) def error_message(answer: object) -> str: diff --git a/bin/notifier/slack.py b/bin/notifier/slack.py index f96fe94c..0bf80c9a 100644 --- a/bin/notifier/slack.py +++ b/bin/notifier/slack.py @@ -2,9 +2,10 @@ import dataclasses import html import pathlib +import typing from failure import Failure -from rest_client.json_request import Http +from rest_client.json_request import Http, MessageOf from rest_client import json_request from rest_client.env_file import read_env @@ -41,9 +42,10 @@ def send(config: Config, message: str, http: Http) -> Failure | None: return Failure(f"POST /api/chat.postMessage refused the message: {error if isinstance(error, str) and error else 'no error code'}") -def http_json(method: str, url: str, token: str, body: object) -> object: +# request is json_request.http_json, but in the test of what this hands it. +def http_json(method: str, url: str, token: str, body: object, request: typing.Callable[[str, str, str, object, int, MessageOf], object] = json_request.http_json) -> object: """The JSON that url answers to the bot's request, None where the answer has no body, or a Failure saying why there is none.""" - return json_request.http_json(method, url, f"Bearer {token}", body, TIMEOUT, error_message) + return request(method, url, f"Bearer {token}", body, TIMEOUT, error_message) def error_message(answer: object) -> str: diff --git a/bin/notifier/test_mattermost.py b/bin/notifier/test_mattermost.py index f1619864..57b9c1e9 100644 --- a/bin/notifier/test_mattermost.py +++ b/bin/notifier/test_mattermost.py @@ -2,7 +2,6 @@ import pathlib import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -102,9 +101,9 @@ class Send(unittest.TestCase): class HttpJson(unittest.TestCase): def test_sends_the_token_as_a_bearer_and_reads_mattermosts_message_from_an_error(self): - with unittest.mock.patch.object(dm.json_request, "http_json", return_value={"id": "post-id"}) as sent: - self.assertEqual(dm.http_json("POST", "https://chat.example/api/v4/posts", "secret-token", {"message": "hi"}), {"id": "post-id"}) - sent.assert_called_once_with("POST", "https://chat.example/api/v4/posts", "Bearer secret-token", {"message": "hi"}, dm.TIMEOUT, dm.error_message) + sent = [] + self.assertEqual(dm.http_json("POST", "https://chat.example/api/v4/posts", "secret-token", {"message": "hi"}, lambda *request: sent.append(request) or {"id": "post-id"}), {"id": "post-id"}) + self.assertEqual(sent, [("POST", "https://chat.example/api/v4/posts", "Bearer secret-token", {"message": "hi"}, dm.TIMEOUT, dm.error_message)]) self.assertEqual(dm.TIMEOUT, 10) def test_reads_mattermosts_message_from_an_error_answer(self): diff --git a/bin/notifier/test_notifier.py b/bin/notifier/test_notifier.py index 1961e536..df3c9aa6 100644 --- a/bin/notifier/test_notifier.py +++ b/bin/notifier/test_notifier.py @@ -1,6 +1,7 @@ import contextlib import dataclasses import io +import os import pathlib import subprocess import sys @@ -73,14 +74,14 @@ class Notify(Project): self.name_notifier("slack", SLACK_KEYS) slack = Slack({"ok": True}) with contextlib.chdir(self.checkout): - self.assertIsNone(system_effects(through("slack", slack)).dm("Which one?", None)) + self.assertIsNone(system_effects(self.home, os.environ, through("slack", slack)).dm("Which one?", None)) self.assertEqual(slack.requests, [SLACK_POST]) def test_a_dm_that_names_a_directory_goes_through_the_notifier_for_the_project_holding_it_from_any_current_directory(self): self.name_notifier("slack", SLACK_KEYS) slack = Slack({"ok": True}) with contextlib.chdir(self.home): - self.assertIsNone(system_effects(through("slack", slack)).dm("Which one?", str(self.checkout))) + self.assertIsNone(system_effects(self.home, os.environ, through("slack", slack)).dm("Which one?", str(self.checkout))) self.assertEqual(slack.requests, [SLACK_POST]) def test_skips_the_notice_for_a_project_that_names_no_notifier_even_where_its_env_file_holds_the_keys(self): diff --git a/bin/notifier/test_slack.py b/bin/notifier/test_slack.py index 298f9837..69d173b4 100644 --- a/bin/notifier/test_slack.py +++ b/bin/notifier/test_slack.py @@ -2,7 +2,6 @@ import pathlib import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -76,9 +75,9 @@ class Send(unittest.TestCase): class HttpJson(unittest.TestCase): def test_sends_the_token_as_a_bearer_and_reads_slacks_code_from_an_error(self): - with unittest.mock.patch.object(dm.json_request, "http_json", return_value={"ok": True}) as sent: - self.assertEqual(dm.http_json("POST", "https://slack.com/api/chat.postMessage", "xoxb-secret", {"text": "hi"}), {"ok": True}) - sent.assert_called_once_with("POST", "https://slack.com/api/chat.postMessage", "Bearer xoxb-secret", {"text": "hi"}, dm.TIMEOUT, dm.error_message) + sent = [] + self.assertEqual(dm.http_json("POST", "https://slack.com/api/chat.postMessage", "xoxb-secret", {"text": "hi"}, lambda *request: sent.append(request) or {"ok": True}), {"ok": True}) + self.assertEqual(sent, [("POST", "https://slack.com/api/chat.postMessage", "Bearer xoxb-secret", {"text": "hi"}, dm.TIMEOUT, dm.error_message)]) for answer, code in (({"error": "ratelimited"}, "ratelimited"), ({"error": 7}, ""), ({}, ""), (["error"], "")): with self.subTest(answer=answer): self.assertEqual(dm.error_message(answer), code) diff --git a/bin/plan/test_ranking.py b/bin/plan/test_ranking.py index d73f707b..8c892838 100644 --- a/bin/plan/test_ranking.py +++ b/bin/plan/test_ranking.py @@ -293,7 +293,7 @@ class CodeHealth(unittest.TestCase): pathlib.Path(temp, "docs").mkdir() pathlib.Path(temp, "docs", "comprehension-history.md").write_text(SCORED.format(overall="7.00")) with contextlib.redirect_stderr(io.StringIO()): - ranked = file_plan(located(temp, pathlib.Path.home()), todo).rank() + ranked = file_plan(located(temp, folder.parents[1]), todo).rank() self.assertIsInstance(ranked, ranking.Ranked) self.assertEqual(ranked.under_bar, []) self.assertIn("| code health by 1 | 25.4 |", todo.read_text()) @@ -310,10 +310,10 @@ class CodeHealth(unittest.TestCase): pathlib.Path(temp, "docs").mkdir() pathlib.Path(temp, "docs", "comprehension-history.md").write_text(SCORED.format(overall="6.50")) with contextlib.redirect_stderr(io.StringIO()): - ranked = file_plan(located(temp, pathlib.Path.home()), todo).rank() + ranked = file_plan(located(temp, folder.parents[1]), todo).rank() self.assertIsInstance(ranked, ranking.Ranked) self.assertIn("| code health by 1 | 25.4 |", todo.read_text()) - self.assertEqual(file_plan(located(temp, pathlib.Path.home()), todo).check(), []) + self.assertEqual(file_plan(located(temp, folder.parents[1]), todo).check(), []) class Risk(unittest.TestCase): diff --git a/bin/plan_store/jira_epic.py b/bin/plan_store/jira_epic.py index fd5d5032..672a4936 100644 --- a/bin/plan_store/jira_epic.py +++ b/bin/plan_store/jira_epic.py @@ -12,7 +12,7 @@ from atlassian.client import basic_auth, Http, jira_json, ToAdf, ToMarkdown from plan.todo_table import BLOCKED_BY, Item, item_fields, ItemFields, MOVES, SHAPE from plan_store.markdown_edits import bold_cell, plain, unfit_text from plan_store.plans import cells, no_item, Plan, Written -from atlassian.jira import entry_of, finishing_transition, ISSUE_KEY, Link, move_refusal, PERHAPS_FILED, PERHAPS_MOVED, project_of, rank_refusal, searched, unshaped +from atlassian.jira import entry_of, finishing_transition, ISSUE_KEY, Link, MAX_PAGES, move_refusal, PERHAPS_FILED, PERHAPS_MOVED, project_of, rank_refusal, searched, unshaped from project_config.settings import Epic, Finish, JIRA_FINISH_STATUS, JIRA_FINISH_TRANSITION from failure import Failure from plan.release import parsed, Release, tagged @@ -71,6 +71,8 @@ class JiraPlan: to_adf: ToAdf = markdown_to_adf # How a finished item moves; None where the config doesn't say, and then `finish` is a Failure. finish_by: Finish | None = None + # The pages a search may take before the read fails. + max_pages: int = MAX_PAGES # How add scores an item: by the config's formula and bar, with the goal weights the project's records give. scoring: ReadsScoring = dataclasses.field(kw_only=True) @@ -86,7 +88,7 @@ class JiraPlan: authorization = basic_auth(self.env_file) if isinstance(authorization, Failure): return authorization - answered = children(self.epic, authorization, self.http) + answered = children(self.epic, authorization, self.http, self.max_pages) if isinstance(answered, Failure): return answered issues = [issue_of(raw) for raw in answered] @@ -115,7 +117,7 @@ class JiraPlan: version = self.fix_version(filing.release, authorization) if isinstance(version, Failure): return version - answered = children(self.epic, authorization, self.http) + answered = children(self.epic, authorization, self.http, self.max_pages) if isinstance(answered, Failure): return answered if missing := missing_blocker(filing.blockers, keys_of(answered), self.label): @@ -170,7 +172,7 @@ class JiraPlan: def child(self, id_: int, authorization: str) -> dict[str, typing.Any] | Failure: """Item id_'s issue, as the epic's open children answer it; a Failure where none of them is item id_.""" - answered = children(self.epic, authorization, self.http) + answered = children(self.epic, authorization, self.http, self.max_pages) return answered if isinstance(answered, Failure) else self.child_in(answered, id_) def child_in(self, answered: list[object], id_: int) -> dict[str, typing.Any] | Failure: @@ -200,7 +202,7 @@ class JiraPlan: authorization = basic_auth(self.env_file) if isinstance(authorization, Failure): return authorization - answered = children(self.epic, authorization, self.http) + answered = children(self.epic, authorization, self.http, self.max_pages) if isinstance(answered, Failure): return answered raw = self.child_in(answered, id_) @@ -376,13 +378,13 @@ def revision_of(issue: Issue, named: dict[str, str], detail: str | None, comment return Revision(body, release, tuple(dict.fromkeys(f"{project_of(issue.key)}-{blocker}" for blocker in checked.blocked_by)) if BLOCKED_BY in named else None) -def children(epic: Epic, authorization: str, http: Http) -> list[object] | Failure: +def children(epic: Epic, authorization: str, http: Http, max_pages: int) -> list[object] | Failure: """The epic's open child issues in backlog order, which the team ranks, apart from where add ranks an issue it files. An issue in a done status is a finished item, so it is no longer in the plan.""" # The search answers no issues for an epic the account cannot see, as for one whose children are all done; asking for the epic itself tells them apart. seen = http("GET", f"{epic.site}/rest/api/3/issue/{epic.key}?fields=summary", authorization, None) if isinstance(seen, Failure): return Failure(f"cannot read the epic {epic.key}: {seen.reason}") - return searched(epic.site, f"{epic.key}'s children", f'parent = "{epic.key}" AND statusCategory != Done ORDER BY Rank ASC', {"fields": ["summary", "description", "fixVersions", "issuelinks"], "properties": [PROPERTY]}, authorization, http) + return searched(epic.site, f"{epic.key}'s children", f'parent = "{epic.key}" AND statusCategory != Done ORDER BY Rank ASC', {"fields": ["summary", "description", "fixVersions", "issuelinks"], "properties": [PROPERTY]}, authorization, http, max_pages) def issue_of(raw: object) -> Issue | Failure: diff --git a/bin/plan_store/test_jira_epic.py b/bin/plan_store/test_jira_epic.py index 821e37f9..46b6bbc4 100644 --- a/bin/plan_store/test_jira_epic.py +++ b/bin/plan_store/test_jira_epic.py @@ -4,7 +4,6 @@ import pathlib import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -171,8 +170,7 @@ class JiraPlan(unittest.TestCase): def test_a_search_that_never_names_a_last_page_fails_the_read(self): self.pages = [{"issues": [], "nextPageToken": "more"}] * 2 - with unittest.mock.patch("atlassian.jira.MAX_PAGES", 2): - self.assertEqual(self.store().read(), Failure("the search for ABC-12's children did not end within 2 pages")) + self.assertEqual(dataclasses.replace(self.store(), max_pages=2).read(), Failure("the search for ABC-12's children did not end within 2 pages")) def test_an_issue_in_another_shape_fails_the_read_naming_it(self): good = issue("ABC-1") diff --git a/bin/project_config/doubles.py b/bin/project_config/doubles.py index d38ab31e..61a2a6b1 100644 --- a/bin/project_config/doubles.py +++ b/bin/project_config/doubles.py @@ -1,10 +1,8 @@ """Test doubles for the tools that read a project's config: a client's config text, and a checkout that a config names.""" -import os import pathlib import subprocess import tempfile import unittest -import unittest.mock from test_env import isolated_env @@ -34,13 +32,10 @@ def write_config(home: pathlib.Path, checkout: pathlib.Path, goals: str = "check def configured(test: unittest.TestCase, checkout: pathlib.Path, goals: str = "checkout:README.md", home: pathlib.Path | None = None) -> pathlib.Path: - """Makes checkout a git checkout that a config names, under home, else a HOME of its own, for the rest of the test; the config's folder.""" + """Makes checkout a git checkout that a config names, under home, else under a folder of its own for the rest of the test; the config's folder, two levels under that home.""" subprocess.run(["git", "init", "--quiet", str(checkout)], check=True, env=isolated_env()) if home is None: made = tempfile.TemporaryDirectory() test.addCleanup(made.cleanup) home = pathlib.Path(made.name) - patched = unittest.mock.patch.dict(os.environ, {"HOME": str(home)}) - patched.start() - test.addCleanup(patched.stop) return write_config(home, checkout, goals) diff --git a/bin/project_config/test_project_config.py b/bin/project_config/test_project_config.py index 00739d8e..b2bc6528 100644 --- a/bin/project_config/test_project_config.py +++ b/bin/project_config/test_project_config.py @@ -4,7 +4,6 @@ import subprocess import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) diff --git a/bin/project_record/test_project_record.py b/bin/project_record/test_project_record.py index 2f7a872f..5ef5cde3 100644 --- a/bin/project_record/test_project_record.py +++ b/bin/project_record/test_project_record.py @@ -130,17 +130,17 @@ class Act(unittest.TestCase): class BlackBox(unittest.TestCase): def setUp(self) -> None: self.top = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())).resolve() - configured(self, self.top) + self.home = configured(self, self.top).parents[1] def run_tool(self, *args: str) -> subprocess.CompletedProcess[str]: - return subprocess.run([sys.executable, str(TOOL), *args], capture_output=True, cwd=self.top, env=dict(os.environ), text=True) + return subprocess.run([sys.executable, str(TOOL), *args], capture_output=True, cwd=self.top, env=os.environ | {"HOME": str(self.home)}, text=True) def test_a_file_record_opens_as_its_own_file_and_save_has_nothing_to_store(self): opened = self.run_tool("open", "decisions") self.assertEqual((opened.returncode, opened.stdout, opened.stderr), (0, f"{self.top}/docs/decisions.md\n", "")) saved = self.run_tool("save", "decisions") self.assertEqual((saved.returncode, saved.stdout, saved.stderr), (0, f"{self.top}/docs/decisions.md is the record itself, so nothing is left to store.\n", "")) - self.assertFalse((pathlib.Path(os.environ["HOME"]) / ".local").exists(), "a file record gets no copy") + self.assertFalse((self.home / ".local").exists(), "a file record gets no copy") def test_the_goals_file_opens_as_the_config_names_it(self): opened = self.run_tool("open", "goals-and-audience") @@ -153,7 +153,7 @@ class BlackBox(unittest.TestCase): self.assertIn("usage: project-record open|save , where is one of decisions, score-anchors, comprehension-history, reading-cost, changelog, goals-and-audience", ran.stderr) def test_exits_1_saying_why_where_no_config_names_the_checkout(self): - (pathlib.Path(os.environ["HOME"]) / ".claude-projects" / self.top.name / "config.toml").unlink() + (self.home / ".claude-projects" / self.top.name / "config.toml").unlink() ran = self.run_tool("open", "decisions") self.assertEqual((ran.returncode, ran.stdout), (1, "")) self.assertIn("no config names the checkout", ran.stderr) diff --git a/bin/rank_todo.py b/bin/rank_todo.py index 47ad8c30..e193e20f 100755 --- a/bin/rank_todo.py +++ b/bin/rank_todo.py @@ -98,14 +98,14 @@ from plan_store.stores import file_plan from project_config.main import located -def main(argv: list[str]) -> int: +def main(argv: list[str], home: pathlib.Path) -> int: if argv[:1] in (["-h"], ["--help"]): print(__doc__.strip()) return 0 args = parse_args(argv) if isinstance(args, Failure): return fail(args) - config = located(None, pathlib.Path.home()) + config = located(None, home) if isinstance(config, Failure): return fail(config) if config.plan_file is None and args == (True, None): @@ -144,4 +144,4 @@ def parse_args(argv: list[str]) -> tuple[bool, pathlib.Path | None] | Failure: if __name__ == "__main__": - sys.exit(main(sys.argv[1:])) + sys.exit(main(sys.argv[1:], pathlib.Path.home())) diff --git a/bin/reading_cost/test_reading_cost.py b/bin/reading_cost/test_reading_cost.py index 101ab675..52e1a708 100644 --- a/bin/reading_cost/test_reading_cost.py +++ b/bin/reading_cost/test_reading_cost.py @@ -16,7 +16,7 @@ from transcript_archive.write import archive from claude_code.entries import AssistantTurn from project_config.templates import BRANCH from reading_cost.measure import Checkout, Reading -from text_files import compressed +from text_files.doubles import compress from failure import Failure from reading_cost import figures, main, measure from transcript_archive.read import finished_chunks, FinishedChunk @@ -26,6 +26,7 @@ from record_store.fakes import MemoryStore from transcript_archive import outcomes from transcript_archive.location import project_archive from reading_cost.shell_reads import Reads, shell_reads +from text_files.file_writes import replace_file from project_config.doubles import write_config TOOL = pathlib.Path(__file__).resolve().parents[1] / "reading-cost" @@ -154,7 +155,7 @@ class ReadingCostTool(unittest.TestCase): transcript.write_bytes(jsonl([{"timestamp": str(at)}, *worker])) (transcript.with_suffix("") / "subagents" / "agent-a.jsonl").write_bytes(jsonl([said("1", "s1", tokens=1)])) with contextlib.redirect_stderr(io.StringIO()): - self.assertIsNone(archive(self.archive, str(transcript), "Do it.", outcome("m", "merged", "j"), at)) + self.assertIsNone(archive(self.archive, str(transcript), "Do it.", outcome("m", "merged", "j"), at, replace=replace_file)) def run_tool(self, cwd, *args): return subprocess.run([sys.executable, str(TOOL), *args], capture_output=True, cwd=cwd, env=self.env, text=True) @@ -177,7 +178,7 @@ class ReadingCostTool(unittest.TestCase): def test_measures_a_chunk_whose_archived_transcripts_are_compressed(self): self.archive_chunk(1790000000) for copy in self.archive.rglob("*.jsonl"): - self.assertIsNone(compressed.compress(copy)) + self.assertIsNone(compress(copy)) ran = self.run_tool(self.checkout) self.assertEqual(ran.returncode, 0, ran.stderr) self.assertIn("33 new input tokens before the first commit.", (self.checkout / "docs" / "reading-cost.md").read_text()) diff --git a/bin/repo_checks/test_no_patched_globals.py b/bin/repo_checks/test_no_patched_globals.py new file mode 100644 index 00000000..56a7bc88 --- /dev/null +++ b/bin/repo_checks/test_no_patched_globals.py @@ -0,0 +1,36 @@ +import ast +import pathlib +import subprocess +import sys +import unittest + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) + +from repo_checks import ROOT + +def patches(source: str) -> list[int]: + """The line numbers where source names mock's patch, as an attribute of mock or imported from it.""" + found = [] + for node in ast.walk(ast.parse(source)): + if isinstance(node, ast.Attribute) and node.attr == "patch" and ast.unparse(node.value).split(".")[-1] == "mock": + found.append(node.lineno) + elif isinstance(node, ast.ImportFrom) and node.module == "unittest.mock" and any(alias.name == "patch" for alias in node.names): + found.append(node.lineno) + return sorted(found) + + +class NoPatchedGlobals(unittest.TestCase): + def test_no_python_file_under_bin_patches_a_global(self): + # A file not yet staged counts too, so a new suite is checked before its first commit. + listed = subprocess.run(["git", "ls-files", "--cached", "--others", "--exclude-standard", "bin/*.py"],capture_output=True, check=True, cwd=ROOT, text=True).stdout.split() + self.assertIn("bin/repo_checks/test_no_patched_globals.py", listed) + patching = {name for name in listed if patches((ROOT / name).read_text(encoding="utf-8"))} + self.assertEqual(patching, set(), "hand the code under test its effect as an argument instead") + + def test_each_spelling_of_patch_is_found(self): + source = "import unittest.mock\nfrom unittest import mock\nfrom unittest.mock import Mock, patch\n\nunittest.mock.patch.dict(a, b)\nmock.patch.object(a, 'b')\nmock.patch('a.b')\nmock.Mock()\n" + self.assertEqual(patches(source), [3, 5, 6, 7]) + + +if __name__ == "__main__": + unittest.main() diff --git a/bin/repo_checks/test_package_cycles.py b/bin/repo_checks/test_package_cycles.py index 6b261ce8..103fa4ae 100644 --- a/bin/repo_checks/test_package_cycles.py +++ b/bin/repo_checks/test_package_cycles.py @@ -81,6 +81,13 @@ def cycle(imported: dict[str, set[str]]) -> list[str]: return [] +def tree(folder: pathlib.Path, sources: dict[str, str]) -> None: + """Writes each source to its path under folder.""" + for path, source in sources.items(): + (folder / path).parent.mkdir(parents=True, exist_ok=True) + (folder / path).write_text(source, encoding="utf-8") + + class PackageCycles(unittest.TestCase): def test_no_package_under_bin_imports_one_that_imports_it_back(self): self.assertEqual(cycle(package_imports(ROOT / "bin")), [], "move what both packages need into a package that imports neither") @@ -94,17 +101,13 @@ class PackageCycles(unittest.TestCase): def test_an_import_of_the_layer_above_is_found_in_tests_too(self): with tempfile.TemporaryDirectory() as temp: folder = pathlib.Path(temp) - for package, name, source in (("low", "mod.py", "import other\n"), ("low", "test_mod.py", "from high.mod import x\n"), ("low", "run.py", "def f():\n\timport high\n"), ("high", "mod.py", "import low\n")): - (folder / package).mkdir(exist_ok=True) - (folder / package / name).write_text(source) + tree(folder, {"low/mod.py": "import other\n", "low/test_mod.py": "from high.mod import x\n", "low/run.py": "def f():\n\timport high\n", "high/mod.py": "import low\n"}) self.assertEqual(upward_imports(folder, {"low": ("high",)}), ["low/run.py imports high", "low/test_mod.py imports high"]) def test_a_driver_imported_from_another_package_is_found_in_tests_too(self): with tempfile.TemporaryDirectory() as temp: folder = pathlib.Path(temp) - for package, name, source in (("a", "mod.py", "import a.other\n"), ("b", "test_mod.py", "from a.mod import x\n"), ("b", "mod.py", "def f():\n\timport a\n"), ("c", "mod.py", "import b\n")): - (folder / package).mkdir(exist_ok=True) - (folder / package / name).write_text(source) + tree(folder, {"a/mod.py": "import a.other\n", "b/test_mod.py": "from a.mod import x\n", "b/mod.py": "def f():\n\timport a\n", "c/mod.py": "import b\n"}) self.assertEqual(driver_imports(folder, ("a",)), ["b/mod.py imports a", "b/test_mod.py imports a"]) def test_no_module_imports_a_test_module_of_another_package(self): @@ -113,26 +116,23 @@ class PackageCycles(unittest.TestCase): def test_a_test_module_imported_from_another_package_is_found_however_it_is_spelled(self): with tempfile.TemporaryDirectory() as temp: folder = pathlib.Path(temp) - sources = ( - ("a", "test_mod.py", "from a.test_other import x\nfrom test_env import y\nfrom a.doubles import z\n"), - ("b", "test_mod.py", "from a.test_mod import x\n"), - ("b", "mod.py", "def f():\n\timport a.deep.test_far\n"), - ("c", "test_mod.py", "from a import test_mod\nfrom a.test_mod import x, y\n"), - ) - for package, name, source in sources: - (folder / package).mkdir(exist_ok=True) - (folder / package / name).write_text(source) - (folder / "test_root.py").write_text("from a.test_mod import x\n") + tree(folder, { + "a/test_mod.py": "from a.test_other import x\nfrom test_env import y\nfrom a.doubles import z\n", + "b/test_mod.py": "from a.test_mod import x\n", + "b/mod.py": "def f():\n\timport a.deep.test_far\n", + "c/test_mod.py": "from a import test_mod\nfrom a.test_mod import x, y\n", + "test_root.py": "from a.test_mod import x\n", + }) self.assertEqual(imports_of_test_modules(folder), ["b/mod.py imports a.deep.test_far", "b/test_mod.py imports a.test_mod", "c/test_mod.py imports a.test_mod", "test_root.py imports a.test_mod"]) def test_reads_an_import_deferred_into_a_function_and_leaves_out_tests(self): with tempfile.TemporaryDirectory() as temp: folder = pathlib.Path(temp) - for package, source in (("a", "def f():\n\tfrom b.mod import x\n"), ("b", "import a.mod\n"), ("c", "")): - (folder / package).mkdir() - (folder / package / "__init__.py").write_text("") - (folder / package / "mod.py").write_text(source) - (folder / "c" / "test_mod.py").write_text("import a\n") + tree(folder, { + "a/__init__.py": "", "a/mod.py": "def f():\n\tfrom b.mod import x\n", + "b/__init__.py": "", "b/mod.py": "import a.mod\n", + "c/__init__.py": "", "c/mod.py": "", "c/test_mod.py": "import a\n", + }) self.assertEqual(package_imports(folder), {"a": {"b"}, "b": {"a"}, "c": set()}) def test_a_cycle_through_a_deferred_import_is_found(self): diff --git a/bin/review-replay b/bin/review-replay index d8a8dee9..d0bdd0c4 100755 --- a/bin/review-replay +++ b/bin/review-replay @@ -8,4 +8,4 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) from review_replay import main from loop_effects.effects import system_effects -sys.exit(main.main(sys.argv[1:], os.environ, pathlib.Path.home(), system_effects())) +sys.exit(main.main(sys.argv[1:], os.environ, pathlib.Path.home(), system_effects(pathlib.Path.home(), os.environ))) diff --git a/bin/review_replay/change_comparison.py b/bin/review_replay/change_comparison.py index 6eda0e9d..186e2ee1 100644 --- a/bin/review_replay/change_comparison.py +++ b/bin/review_replay/change_comparison.py @@ -1,5 +1,4 @@ """The experiment behind review-replay's --omit-claude-md, --bash-ceiling and --read-limit: three replays of a pass's rounds unchanged, one with the change, and the report that holds the one with it to what the others found: docs/decisions.md → `agents/: a reviewer starts without CLAUDE.md only where a replay of its pass missed no more findings than the most an unchanged replay missed`.""" -import sys import typing from review_replay.changes import Change @@ -10,8 +9,8 @@ from review_replay.rounds import Round from model_routing.ladder import Rung -def compared(reviewed: str, project: Rung, chosen: list[Round], change: Change, found_on: typing.Callable[[Round, Change | None], set[int] | Failure | Limited]) -> str | Limited: - """Replays chosen three times unchanged and once with change, as found_on(round, change or None) finds, and reports the comparison of the rounds no replay failed on; the Limited where claude hit its usage limit.""" +def compared(reviewed: str, project: Rung, chosen: list[Round], change: Change, found_on: typing.Callable[[Round, Change | None], set[int] | Failure | Limited], say: typing.Callable[[str], None], at_once: int) -> str | Limited: + """Replays chosen three times unchanged and once with change, as found_on(round, change or None) finds, and reports the comparison of the rounds no replay failed on; the Limited where claude hit its usage limit. It tells say each round a failed replay leaves out.""" runs: list[list[set[int] | Failure]] = [] for applied in (None, None, None, change): label = change.changed if applied else change.unchanged @@ -19,10 +18,10 @@ def compared(reviewed: str, project: Rung, chosen: list[Round], change: Change, def replay_one(one: Round, _: Rung, applied: Change | None = applied, label: str = label) -> set[int] | Failure | Limited: found = found_on(one, applied) if isinstance(found, Failure): - print(f" {one.root} {one.at} {label} failed, so the round is left out: {found.reason}", file=sys.stderr) + say(f" {one.root} {one.at} {label} failed, so the round is left out: {found.reason}") return found - found: list[set[int] | Failure] | Limited = replayed_on(chosen, project, replay_one, f"{reviewed} rounds {label}") + found: list[set[int] | Failure] | Limited = replayed_on(chosen, project, replay_one, f"{reviewed} rounds {label}", say, at_once) if isinstance(found, Limited): return found runs.append(found) diff --git a/bin/review_replay/main.py b/bin/review_replay/main.py index 87a6590b..8d9d4b40 100644 --- a/bin/review_replay/main.py +++ b/bin/review_replay/main.py @@ -67,22 +67,22 @@ none. Exit 1, saying why, on bad arguments, an unreadable archive or models file round to replay, or a usage limit. """ import pathlib -import sys import typing from transcript_archive.location import archive_root from review_replay.rounds import archived_rounds, Round, sample -from rung_replay.archive import archiving, print_report, replay_folder -from rung_replay.command import arguments_or_exit, failed, limited_exit +from rung_replay.archive import archiving, replay_folder +from rung_replay.command import arguments_or_exit, failed, limited_exit, print_report, say +from rung_replay.search import AT_ONCE from review_replay.changes import Change, CHANGES from review_replay.replay import codes_found, findings_replayed from review_replay.change_comparison import compared from loop_effects.effects import Effects +from review_replay.recall import ENOUGH_ROUNDS, recall_report, recalled from failure import Failure from model_routing.ladder import Ladder, Rung from claude_code.reply import Limited from model_routing.ladder_file import read_ladder, SHIPPED -from review_replay.recall import recall_report, recalled from review_rules.review_passes import ReviewedPass, REVIEWERS TOOL = "review-replay" @@ -90,7 +90,8 @@ USAGE = f"{TOOL} [{' | '.join(change.flag for change in CHANGES)} LATEST = 10 -def main(argv: list[str], env: typing.Mapping[str, str], home: pathlib.Path, effects: Effects) -> int: +# A test sets the three limits: the fewest rounds a rung is tried on, the rounds replayed at once, and the folder each clone is made in, the system's temporary folder where None. +def main(argv: list[str], env: typing.Mapping[str, str], home: pathlib.Path, effects: Effects, *, enough_rounds: int = ENOUGH_ROUNDS, at_once: int = AT_ONCE, scratch: pathlib.Path | None = None) -> int: # The shipped ladder, not a project's: the replay spans every project on the model. ladder = read_ladder(SHIPPED) if isinstance(ladder, Failure): @@ -105,29 +106,29 @@ def main(argv: list[str], env: typing.Mapping[str, str], home: pathlib.Path, eff rounds = archived_rounds(REVIEWERS[reviewed], project.model, archive_root(home, env), home) if isinstance(rounds, Failure): return failed(TOOL, rounds.reason) - codes = codes_found(rounds, effects) + codes = codes_found(rounds, effects, say) if not codes: return failed(TOOL, f"no archived {reviewed} round of projects on {project.model} in {archive_root(home, env)} holds a 🔴 or 🟡 on code it can find") chosen = sample(list(codes), LATEST) folder = replay_folder(archive_root(home, env), TOOL, argv, effects.now()) - effects = archiving(effects, folder) + effects = archiving(effects, folder, say) if change: - report = compared(reviewed, project, chosen, change, lambda found, applied: findings_replayed(found, codes[found], project, project, home, effects, applied)) + report = compared(reviewed, project, chosen, change, lambda found, applied: findings_replayed(found, codes[found], project, project, home, effects, applied, scratch), say, at_once) if isinstance(report, Limited): return limited_exit(TOOL, report) print_report(folder, report) return 0 def replay_one(found: Round, rung: Rung) -> set[int] | Failure | Limited: - matched = findings_replayed(found, codes[found], rung, project, home, effects, None) + matched = findings_replayed(found, codes[found], rung, project, home, effects, None, scratch) if isinstance(matched, Failure): - print(f" {found.root} {found.at} on {rung} failed: {matched.reason}", file=sys.stderr, flush=True) + say(f" {found.root} {found.at} on {rung} failed: {matched.reason}") return matched - recall = recalled(chosen, ladder, project, replay_one, f"{reviewed} rounds") + recall = recalled(chosen, ladder, project, replay_one, f"{reviewed} rounds", say, at_once, enough_rounds) if isinstance(recall, Limited): return limited_exit(TOOL, recall) - print_report(folder, recall_report(reviewed, project, recall)) + print_report(folder, recall_report(reviewed, project, recall, enough_rounds)) return 0 diff --git a/bin/review_replay/recall.py b/bin/review_replay/recall.py index cd95cc26..e2b1e695 100644 --- a/bin/review_replay/recall.py +++ b/bin/review_replay/recall.py @@ -43,10 +43,6 @@ def candidates(ladder: Ladder, project: Rung) -> list[Rung]: return lower_efforts + down_from(ladder, project, ladder.rungs[0])[1:] -def enough(rounds: list[Round]) -> bool: - return len(rounds) >= ENOUGH_ROUNDS - - def count(found: list[set[int]]) -> int: return sum(map(len, found)) @@ -72,14 +68,14 @@ def tried(rung: Rung, rounds: list[Round], reference: Reference, found: list[set return RungRecall(rung, count(found), missed_red, 0 < needed(rounds, reference) <= count(found) and not missed_red) -def recall_report(reviewed: str, project: Rung, recall: Recall) -> str: +def recall_report(reviewed: str, project: Rung, recall: Recall, enough_rounds: int) -> str: rounds, (first, second) = recall.rounds, recall.reference lines = [ f"Replayed {len(rounds)} {reviewed} rounds of {project.model} projects, which hold {held(rounds)} 🔴 and 🟡 findings, twice on the project's rung, {project}.", *([f"Left out {recall.left_out} more, which a replay on the project's rung failed on; stderr says why."] if recall.left_out else []), ] - if not enough(rounds): - return "\n".join([*lines, f"No rung tried: fewer than {ENOUGH_ROUNDS} rounds were replayed on the project's rung without a failure."]) + if len(rounds) < enough_rounds: + return "\n".join([*lines, f"No rung tried: fewer than {enough_rounds} rounds were replayed on the project's rung without a failure."]) if needed(rounds, recall.reference) <= 0: return "\n".join([*lines, "No rung tried: the project's rung found too few of them to hold a rung to."]) lines.append(f"The project's rung found {count(first)} and then {count(second)} of the {held(rounds)}, {percent(mean_share(rounds, recall.reference))} on average, so a rung qualifies finding {needed(rounds, recall.reference)} or more of them, missing no 🔴 that both of its replays found.") @@ -95,11 +91,11 @@ def percent(fraction: Fraction) -> str: return f"{float(fraction):.0%}" -def recalled(chosen: list[Round], ladder: Ladder, project: Rung, replay_one: typing.Callable[[Round, Rung], set[int] | Failure | Limited], what: str) -> Recall | Limited: - """Replays chosen twice on the project's rung, then each candidate, highest first, on the rounds neither of those failed on, until one doesn't qualify.""" +def recalled(chosen: list[Round], ladder: Ladder, project: Rung, replay_one: typing.Callable[[Round, Rung], set[int] | Failure | Limited], what: str, say: typing.Callable[[str], None], at_once: int, enough_rounds: int) -> Recall | Limited: + """Replays chosen twice on the project's rung, then each candidate, highest first, on the rounds neither of those failed on, until one doesn't qualify; no candidate where fewer than enough_rounds rounds are left.""" runs: list[list[set[int] | Failure]] = [] for _ in range(2): - found: list[set[int] | Failure] | Limited = replayed_on(chosen, project, replay_one, what) + found: list[set[int] | Failure] | Limited = replayed_on(chosen, project, replay_one, what, say, at_once) if isinstance(found, Limited): return found runs.append(found) @@ -112,8 +108,8 @@ def recalled(chosen: list[Round], ladder: Ladder, project: Rung, replay_one: typ reference[1].append(second) recalls: list[RungRecall] = [] lowest = None - for rung in candidates(ladder, project) if enough(rounds) and needed(rounds, reference) > 0 else []: - on_rung: list[set[int] | Failure] | Limited = replayed_on(rounds, rung, replay_one, what) + for rung in candidates(ladder, project) if len(rounds) >= enough_rounds and needed(rounds, reference) > 0 else []: + on_rung: list[set[int] | Failure] | Limited = replayed_on(rounds, rung, replay_one, what, say, at_once) if isinstance(on_rung, Limited): return on_rung # A failed replay on a candidate found none, so it lowers that rung's count and never lifts it to what a rung needs. diff --git a/bin/review_replay/replay.py b/bin/review_replay/replay.py index ee66be74..8e3606f7 100644 --- a/bin/review_replay/replay.py +++ b/bin/review_replay/replay.py @@ -4,8 +4,8 @@ import json import os import pathlib import shutil -import sys import tempfile +import typing from review_replay.rounds import authored_by, lost_inputs, reviewed_path, Round from review_replay.changes import Change @@ -34,12 +34,12 @@ class Code: base: str -def codes_found(rounds: list[Round], effects: Effects) -> dict[Round, Code]: - """The code each round reviewed, as code_of finds it; stderr names each round left out, and why.""" +def codes_found(rounds: list[Round], effects: Effects, say: typing.Callable[[str], None]) -> dict[Round, Code]: + """The code each round reviewed, as code_of finds it; say is told each round left out, and why.""" codes = {} for found in rounds: if isinstance(code := code_of(found, effects), Failure): - print(f"{found.root} {found.at} is left out: {code.reason}", file=sys.stderr, flush=True) + say(f"{found.root} {found.at} is left out: {code.reason}") else: codes[found] = code return codes @@ -75,9 +75,9 @@ def commit_log(listed: str) -> list[tuple[str, float]]: return [(commit, float(authored)) for commit, authored in (line.split() for line in listed.splitlines())] -def findings_replayed(found: Round, code: Code, rung: Rung, project: Rung, home: pathlib.Path, effects: Effects, change: Change | None) -> set[int] | Failure | Limited: - """The indexes of the round's findings that its replay on rung reports, as claude on the project's rung matches them, or the Failure of the review or the match; where change is given, the replay runs with it, as `launch` says.""" - reviewed: str | Failure | Limited = first_reply(lambda: review(found, code, rung, home, effects, change)) +def findings_replayed(found: Round, code: Code, rung: Rung, project: Rung, home: pathlib.Path, effects: Effects, change: Change | None, scratch: pathlib.Path | None) -> set[int] | Failure | Limited: + """The indexes of the round's findings that its replay on rung reports, as claude on the project's rung matches them, or the Failure of the review or the match; where change is given, the replay runs with it, as `launch` says. The clone is made under scratch, or under the system's temporary folder where it is None.""" + reviewed: str | Failure | Limited = first_reply(lambda: review(found, code, rung, home, effects, change, scratch)) if isinstance(reviewed, (Failure, Limited)): return reviewed return first_reply(lambda: match(found.findings, reviewed, found.root, project, effects)) @@ -91,10 +91,10 @@ class Copies: # The shell makes and removes the clone itself, and is tested on a temporary home as judge-replay's is. -def review(found: Round, code: Code, rung: Rung, home: pathlib.Path, effects: Effects, change: Change | None) -> str | Failure | Limited: +def review(found: Round, code: Code, rung: Rung, home: pathlib.Path, effects: Effects, change: Change | None, scratch: pathlib.Path | None) -> str | Failure | Limited: try: (home / REPORTS).mkdir(mode=0o700, parents=True, exist_ok=True) - with tempfile.TemporaryDirectory(prefix="review-replay-", ignore_cleanup_errors=True) as clone, tempfile.TemporaryDirectory(prefix="review-replay-", dir=home / REPORTS, ignore_cleanup_errors=True) as reports: + with tempfile.TemporaryDirectory(prefix="review-replay-", dir=scratch, ignore_cleanup_errors=True) as clone, tempfile.TemporaryDirectory(prefix="review-replay-", dir=home / REPORTS, ignore_cleanup_errors=True) as reports: return reviewed_in(found, code, rung, Copies(clone, reports), home, effects, change) except OSError as failure: return Failure(f"could not prepare the replay: {failure}") diff --git a/bin/review_replay/test_recall.py b/bin/review_replay/test_recall.py index 1b4a926b..0889f537 100644 --- a/bin/review_replay/test_recall.py +++ b/bin/review_replay/test_recall.py @@ -1,12 +1,11 @@ -import contextlib -import io import pathlib import sys import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) -from review_replay.recall import candidates, needed, Recall, recall_report, recalled, tried +from rung_replay.search import AT_ONCE +from review_replay.recall import candidates, ENOUGH_ROUNDS, needed, Recall, recall_report, recalled, tried from review_replay.rounds import is_red, Round from model_routing.ladder_file import read_ladder, SHIPPED from model_routing.ladder import Rung @@ -44,8 +43,7 @@ class Recalled(unittest.TestCase): def test_tries_the_rungs_below_where_the_project_rung_needs_one_finding(self): # Eight findings, of which both replays on the project's rung find one: a share of an eighth, a tenth under which leaves one finding needed. rounds = [a_round(at, ("🟡 a",)) for at in range(8)] - with contextlib.redirect_stderr(io.StringIO()): - recall = recalled(rounds, LADDER, REFERENCE, lambda one, rung: {0} if one == rounds[0] else set(), "prose rounds") + recall = recalled(rounds, LADDER, REFERENCE, lambda one, rung: {0} if one == rounds[0] else set(), "prose rounds", lambda text: None, AT_ONCE, ENOUGH_ROUNDS) self.assertEqual(([one.rung for one in recall.tried], recall.lowest), ([LOW, SONNET], SONNET)) @@ -96,7 +94,7 @@ class RecallReport(unittest.TestCase): rounds, reference = self.EIGHT low = tried(LOW, rounds, reference, [{0, 1, 2}, {0, 1, 2}] * 4) sonnet = tried(SONNET, rounds, reference, [{1, 2, 3, 4}, {0}] * 4) - lines = recall_report("prose", REFERENCE, Recall(rounds, reference, 1, [low, sonnet], LOW)).splitlines() + lines = recall_report("prose", REFERENCE, Recall(rounds, reference, 1, [low, sonnet], LOW), ENOUGH_ROUNDS).splitlines() self.assertEqual(lines, [ "Replayed 8 prose rounds of claude-opus-5-5 projects, which hold 40 🔴 and 🟡 findings, twice on the project's rung, claude-opus-5-5 at medium effort.", "Left out 1 more, which a replay on the project's rung failed on; stderr says why.", @@ -108,17 +106,17 @@ class RecallReport(unittest.TestCase): ]) def test_says_why_no_rung_was_tried(self): - few = recall_report("prose", REFERENCE, Recall(ROUNDS, (FIRST, SECOND), 0, [], None)).splitlines() + few = recall_report("prose", REFERENCE, Recall(ROUNDS, (FIRST, SECOND), 0, [], None), ENOUGH_ROUNDS).splitlines() self.assertEqual(few[1:], ["No rung tried: fewer than 8 rounds were replayed on the project's rung without a failure."]) rounds, _ = self.EIGHT for reference in (([set()] * 8, [set()] * 8), (TENTH[0] * 4, TENTH[1] * 4)): - too_few = recall_report("prose", REFERENCE, Recall(rounds, reference, 0, [], None)).splitlines() + too_few = recall_report("prose", REFERENCE, Recall(rounds, reference, 0, [], None), ENOUGH_ROUNDS).splitlines() self.assertEqual(too_few[1:], ["No rung tried: the project's rung found too few of them to hold a rung to."]) def test_names_no_rung_where_none_qualifies(self): rounds, reference = self.EIGHT low = tried(LOW, rounds, reference, [{0}, {0}] * 4) - lines = recall_report("prose", REFERENCE, Recall(rounds, reference, 0, [low], None)).splitlines() + lines = recall_report("prose", REFERENCE, Recall(rounds, reference, 0, [low], None), ENOUGH_ROUNDS).splitlines() self.assertEqual(lines[-2:], ["claude-opus-5-5 at low effort found 8, 20%, and missed no such 🔴: it doesn't qualify.", "Lowest rung that qualifies: none below the project's rung"]) diff --git a/bin/review_replay/test_review_replay.py b/bin/review_replay/test_review_replay.py index cd6004c6..feb9c22c 100644 --- a/bin/review_replay/test_review_replay.py +++ b/bin/review_replay/test_review_replay.py @@ -10,13 +10,12 @@ import subprocess import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from review_replay.rounds import authored_by, lost_inputs, recorded_findings, reviewed_path, Round, round_from, sample, stamp_seconds -from review_replay import changes, main, recall, replay -from text_files import compressed +from review_replay import changes, main, replay +from text_files.doubles import compress from project_config.doubles import configured, write_config from loop_effects.effects import Effects, system_effects from claude_code.entries import entry_from @@ -24,7 +23,6 @@ from failure import Failure from review_rules.reviewer_context import reviewer_context from review_rules.review_passes import REVIEWERS from model_routing.ladder import Rung -from rung_replay import search TOOL = pathlib.Path(__file__).resolve().parents[1] / "review-replay" REFERENCE = Rung("claude-opus-5-5", "medium") @@ -172,7 +170,7 @@ class Match(unittest.TestCase): self.asked.append((args, cwd, prompt)) return subprocess.CompletedProcess(args, 0, stdout, "") - return replay.match(("🔴 a", "🟡 b"), "the other report", "/code/p", REFERENCE, dataclasses.replace(system_effects(), claude=claude)) + return replay.match(("🔴 a", "🟡 b"), "the other report", "/code/p", REFERENCE, dataclasses.replace(system_effects(pathlib.Path.home(), os.environ), claude=claude)) def test_asks_for_the_numbered_findings_the_other_report_raises_as_json_with_no_tools(self): self.run_match("{}") @@ -222,7 +220,7 @@ class Main(unittest.TestCase): self.write_chunk(key, "c1", "2026-10-14T10:00:00Z") (self.home / ".claude" / "CLAUDE.md").write_text("## Shared goal\n\nHealth.\n\n## Prose\n\nShort.\n\n## Vocabulary\n\nWords.\n") # The fixture holds one round. - self.enterContext(unittest.mock.patch.object(recall, "ENOUGH_ROUNDS", 1)) + self.limits = {"enough_rounds": 1} self.reviews = [] self.review_settings = [] self.agents = set() @@ -270,10 +268,10 @@ class Main(unittest.TestCase): result = result if "api_error_status" in result else {"type": "result", "structured_output": {"found": [n + 1 for n in result["found"]]}} return subprocess.CompletedProcess(args, 0, json.dumps(result), "") - effects = Effects(claude=claude, claude_on_terminal=unused, discard_typed=unused, dm=unused, execv=unused, git=system_effects().git, input=unused, local_time=unused, memory=unused, now=lambda: 1790000000.0, on_terminal=unused, python=unused, sleep=unused, stream_claude=unused, timer=unused, tmux=unused, tmux_on_terminal=unused) + effects = Effects(claude=claude, claude_on_terminal=unused, discard_typed=unused, dm=unused, execv=unused, git=system_effects(pathlib.Path.home(), os.environ).git, input=unused, local_time=unused, memory=unused, now=lambda: 1790000000.0, on_terminal=unused, python=unused, replace_file=unused, sleep=unused, stream_claude=unused, timer=unused, tmux=unused, tmux_on_terminal=unused) out, err = io.StringIO(), io.StringIO() with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): - code = main.main(argv, env, self.home, effects) + code = main.main(argv, env, self.home, effects, **self.limits) return code, out.getvalue(), err.getvalue() def test_keeps_each_reviewer_and_match_run_and_the_printed_report_in_one_folder_of_the_archive(self): @@ -428,14 +426,14 @@ class Main(unittest.TestCase): def test_replays_rounds_whose_archived_transcripts_are_compressed(self): for copy in self.archive.rglob("*.jsonl"): - self.assertIsNone(compressed.compress(copy)) + self.assertIsNone(compress(copy)) code, out, err = self.run_main(["stability", "claude-opus-5-5"], lambda rung: {"found": [1] if rung == LOW else [0, 1]}) self.assertEqual(code, 0, err) self.assertTrue(out.startswith("Replayed 1 stability rounds of claude-opus-5-5 projects,"), out) def test_copies_a_compressed_report_from_before_the_round_under_its_plain_name(self): folder = self.review_folder_with_round_1() - self.assertIsNone(compressed.compress(folder / "stability-round-1.md")) + self.assertIsNone(compress(folder / "stability-round-1.md")) self.set_prompt(f"Review branch x in {self.home}/code/proj-x. Write your report to ~/.local/state/larv-review/code/proj-x/stability-round-2.md; the last round's is ~/.local/state/larv-review/code/proj-x/stability-round-1.md.") seen = [] @@ -513,7 +511,7 @@ class Main(unittest.TestCase): (folder / "stability-round-1.md").chmod(0) self.set_prompt(f"Review branch x in {self.home}/code/proj-x. Write your report to ~/.local/state/larv-review/code/proj-x/stability-round-2.md.") scratch = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) - self.enterContext(unittest.mock.patch.object(tempfile, "tempdir", str(scratch))) + self.limits["scratch"] = scratch code, _, err = self.run_main(["stability", "claude-opus-5-5"], unused) self.assertEqual(code, 0, err) self.assertEqual(self.reviews, []) @@ -529,7 +527,7 @@ class Main(unittest.TestCase): def test_leaves_out_a_round_that_either_replay_on_the_project_rung_failed_on(self): self.write_chunk("proj", "c2", "2026-10-14T10:30:00Z") - self.enterContext(unittest.mock.patch.object(search, "AT_ONCE", 1)) + self.limits["at_once"] = 1 # One round at a time, three tries on a round reply with no report: the first replay's on its first round, then the second replay's on its first, after the first replay's two reviews. Every other review replies. for failing in (range(3), range(2, 5)): with self.subTest(failing=failing): diff --git a/bin/routing_figures/state.py b/bin/routing_figures/state.py index 5db0255d..19054022 100644 --- a/bin/routing_figures/state.py +++ b/bin/routing_figures/state.py @@ -5,10 +5,11 @@ import dataclasses import json import pathlib import re +import typing from routing_figures.calls import Call, SENTENCES from failure import Failure -from text_files.file_writes import replace_file, replace_json +from text_files.file_writes import replace_json from text_files.utc_stamp import STAMPED CYCLES_PER_CHECK = 20 @@ -100,8 +101,8 @@ def raised(root: pathlib.Path) -> Raised | Failure: return Raised(since, frozenset(entry.call for entry in kept.raised if entry.at > since)) -def mark_raised(root: pathlib.Path, calls: list[Call], at: str) -> Failure | None: - """Keeps the calls in STATE, and adds a line for each to RAISED.""" +def mark_raised(root: pathlib.Path, calls: list[Call], at: str, replace: typing.Callable[[pathlib.Path, bytes], Failure | None]) -> Failure | None: + """Keeps the calls in STATE, and adds a line for each to RAISED, which replace writes as replace_file does.""" kept = read_state(root) if isinstance(kept, Failure): return kept @@ -112,7 +113,7 @@ def mark_raised(root: pathlib.Path, calls: list[Call], at: str) -> Failure | Non return unwritten # The first line opens a list below the heading; each later one joins it. gap = "\n" if RAISED_LINE.search(text) else "\n\n" - unwritten = replace_file(root / RAISED, (text.rstrip("\n") + gap + "".join(f"- {at} {call.sentence()}\n" for call in calls)).encode()) + unwritten = replace(root / RAISED, (text.rstrip("\n") + gap + "".join(f"- {at} {call.sentence()}\n" for call in calls)).encode()) return Failure(f"could not write {root / RAISED}: {unwritten.reason}") if unwritten else None diff --git a/bin/routing_figures/test_routing_figures.py b/bin/routing_figures/test_routing_figures.py index c7b9e68a..badf3b78 100644 --- a/bin/routing_figures/test_routing_figures.py +++ b/bin/routing_figures/test_routing_figures.py @@ -10,7 +10,6 @@ import subprocess import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -20,7 +19,7 @@ from project_config.templates import BARE, Titles from branch_chunks.cut import BranchChunk from routing_figures.calls import Call, calls, totals from routing_figures.chunk import Chunk, chunk_figures, ChunkFigures, Reports, Subagent -from text_files import compressed +from text_files.doubles import compress from project_config.doubles import configured, write_config from routing_figures.state import due from claude_code.entries import entry_from @@ -30,6 +29,7 @@ from plan.todo_table import Item from transcript_archive import outcomes, write from model_routing.ladder_file import read_ladder, SHIPPED from routing_figures.render import render +from text_files.file_writes import replace_file from model_routing.route import Route from model_routing.ladder import Rung @@ -356,7 +356,7 @@ class Archive(unittest.TestCase): def test_measures_a_chunk_whose_transcripts_are_compressed(self): self.chunk("home/x", "c1", stamped("2026-10-14T10:00:00Z", outcomes.finished(LOW_ROUTE, "m", "j")), [message("m1", "2026-10-14T09:00:00Z", call("Agent", "a1", subagent_type="Explore"))], ({"agentType": "Explore", "toolUseId": "a1"}, [message("s1", "2026-10-14T09:00:01Z")])) for copy in self.root.rglob("*.jsonl"): - self.assertIsNone(compressed.compress(copy)) + self.assertIsNone(compress(copy)) found = archive.measure_archive(self.root, "/h", "", no_checkout) self.assertEqual(found.chunks[0].dollars["subagent type"], {"worker": 2.0, "Explore": 2.0}) @@ -365,8 +365,7 @@ class Archive(unittest.TestCase): env = {**isolated_env(), "GIT_CONFIG_GLOBAL": os.devnull, "HOME": str(home)} self.ship_then_fix(home / "code" / "log", env) self.chunk("log", "c1", stamped("2026-10-14T10:00:00Z", outcomes.finished(LOW_ROUTE, "m", "j")), [message("m1", "2026-10-14T09:00:00Z")]) - with unittest.mock.patch.dict(os.environ, env, clear=True): - found = archive.measure_archive(self.root, str(home), "", git_repo.git) + found = archive.measure_archive(self.root, str(home), "", git_repo.git) self.assertEqual([(chunk.dollars["project"].keys(), chunk.fixed_later) for chunk in found.chunks], [({"log": 0}.keys(), 1)]) def test_reads_a_projects_round_titles_and_its_branchs_history_through_its_configs_templates(self): @@ -377,10 +376,9 @@ class Archive(unittest.TestCase): config = write_config(home, home / "code" / "log") / "config.toml" worker = [message(f"m{n}", "2026-10-14T09:00:00Z", call("Bash", command=f"git commit -m '{round_title.format(n)}'")) for n in (1, 2)] self.chunk("log", "c1", stamped("2026-10-14T10:00:00Z", outcomes.finished(LOW_ROUTE, "m", "j")), worker) - with unittest.mock.patch.dict(os.environ, env, clear=True): - bare = archive.measure_archive(self.root, str(home), "", git_repo.git) - config.write_text(config.read_text() + 'commit-title = "PAY-{id} - {summary}"\n') - templated = archive.measure_archive(self.root, str(home), "", git_repo.git) + bare = archive.measure_archive(self.root, str(home), "", git_repo.git) + config.write_text(config.read_text() + 'commit-title = "PAY-{id} - {summary}"\n') + templated = archive.measure_archive(self.root, str(home), "", git_repo.git) self.assertEqual([(chunk.rounds_past_first, chunk.fixed_later) for chunk in bare.chunks], [(0, 0)]) self.assertEqual([(chunk.rounds_past_first, chunk.fixed_later) for chunk in templated.chunks], [(1, 1)]) @@ -391,8 +389,7 @@ class Archive(unittest.TestCase): config = write_config(home, home / "code" / "log") / "config.toml" config.write_text(config.read_text() + 'commit-title = "PAY-{id}"\n') self.chunk("log", "c1", stamped("2026-10-14T10:00:00Z", outcomes.finished(LOW_ROUTE, "m", "j")), [message("m1", "2026-10-14T09:00:00Z")]) - with unittest.mock.patch.dict(os.environ, env, clear=True): - found = archive.measure_archive(self.root, str(home), "", git_repo.git) + found = archive.measure_archive(self.root, str(home), "", git_repo.git) self.assertEqual(found.chunks, []) self.assertEqual(len(found.unread), 1) self.assertRegex(found.unread[0], r"^log: none of its chunks is measured: .*`commit-title` is 'PAY-\{id\}'") @@ -407,8 +404,7 @@ class Archive(unittest.TestCase): def recording(*args, **options): made.append((args[0], options)) return git_repo.git(*args, **options) - with unittest.mock.patch.dict(os.environ, env, clear=True): - archive.measure_archive(self.root, str(home), "", recording) + archive.measure_archive(self.root, str(home), "", recording) self.assertTrue(all(options.get("guarded") is True for _, options in made)) read = [options.get("timeout") for command, options in made if command in ("log", "ls-tree", "show", "diff", "blame")] self.assertEqual(set(read), {archive.GIT_TIMEOUT}) @@ -420,8 +416,7 @@ class Archive(unittest.TestCase): (home / "code" / "log").mkdir(parents=True) subprocess.run(["git", "init", "-q"], cwd=home / "code" / "log", check=True, env=env, capture_output=True) self.chunk("log", "c1", stamped("2026-10-14T10:00:00Z", outcomes.finished(LOW_ROUTE, "m", "j")), [message("m1", "2026-10-14T09:00:00Z")]) - with unittest.mock.patch.dict(os.environ, env, clear=True): - found = archive.measure_archive(self.root, str(home), "", git_repo.git) + found = archive.measure_archive(self.root, str(home), "", git_repo.git) self.assertEqual(found.unread, ("log: no origin remote, and no main or master branch to take as the default branch",)) def test_names_each_project_whose_history_it_cannot_read(self): @@ -458,9 +453,9 @@ class Archive(unittest.TestCase): first = message("m1", "2026-09-21T00:00:00Z") transcript.write_text(json.dumps(first) + "\n") with contextlib.redirect_stderr(io.StringIO()): - self.assertIsNone(write.archive(kept / "log", str(transcript), "t", outcomes.unfinished(LOW_ROUTE, "e", "j", ""), 1790000000)) + self.assertIsNone(write.archive(kept / "log", str(transcript), "t", outcomes.unfinished(LOW_ROUTE, "e", "j", ""), 1790000000, replace=replace_file)) transcript.write_text(json.dumps(first) + "\n" + json.dumps(message("m2", "2099-01-01T00:00:00Z")) + "\n") - self.assertIsNone(write.archive(kept / "log", str(transcript), "t", outcomes.finished(Route(HIGH).worker_line(), "m", "j"), 4070908800)) + self.assertIsNone(write.archive(kept / "log", str(transcript), "t", outcomes.finished(Route(HIGH).worker_line(), "m", "j"), 4070908800, replace=replace_file)) (found,) = archive.measure_archive(kept, "/h", "", no_checkout).chunks self.assertEqual((found.start, found.unfinished), (LOW, 1)) self.assertEqual(found.dollars["rung"], {str(LOW): 2.0, str(HIGH): 2.0}) @@ -485,14 +480,14 @@ class Archive(unittest.TestCase): def test_reads_the_calls_raised_since_the_latest_reset(self): a, b, c = Call("reports", ("Explore",)), Call("rounds", ("x", "y")), Call("unfinished", ("x", "y")) self.assertEqual(state.raised(self.root), state.Raised("", frozenset())) - self.assertIsNone(state.mark_raised(self.root, [a], "2026-10-15T00:00:00Z")) - self.assertIsNone(state.mark_raised(self.root, [b, c], "2026-10-15T01:00:00Z")) + self.assertIsNone(state.mark_raised(self.root, [a], "2026-10-15T00:00:00Z", replace_file)) + self.assertIsNone(state.mark_raised(self.root, [b, c], "2026-10-15T01:00:00Z", replace_file)) self.assertEqual((self.root / state.RAISED).read_text(), f"# Routing calls raised\n\n- 2026-10-15T00:00:00Z {a.sentence()}\n- 2026-10-15T01:00:00Z {b.sentence()}\n- 2026-10-15T01:00:00Z {c.sentence()}\n") self.assertEqual(state.raised(self.root), state.Raised("", frozenset({a, b, c}))) with open(self.root / state.RAISED, "a") as handle: handle.write("- 2026-10-15T01:00:00Z Reset\n") self.assertEqual(state.raised(self.root), state.Raised("2026-10-15T01:00:00Z", frozenset()), "a call stamped at the reset's second was raised before it") - self.assertIsNone(state.mark_raised(self.root, [a], "2026-10-15T02:00:00Z")) + self.assertIsNone(state.mark_raised(self.root, [a], "2026-10-15T02:00:00Z", replace_file)) self.assertEqual(state.raised(self.root), state.Raised("2026-10-15T01:00:00Z", frozenset({a}))) def test_fails_when_it_cannot_keep_the_state(self): @@ -500,17 +495,15 @@ class Archive(unittest.TestCase): self.root.chmod(0o555) self.addCleanup(self.root.chmod, 0o755) self.assertIsInstance(state.mark_measured(self.root, 40), Failure) - self.assertIsInstance(state.mark_raised(self.root, [Call("reports", ("Explore",))], "2026-10-15T00:00:00Z"), Failure) + self.assertIsInstance(state.mark_raised(self.root, [Call("reports", ("Explore",))], "2026-10-15T00:00:00Z", replace_file), Failure) def test_fails_when_it_cannot_list_a_raised_call_for_the_human(self): self.assertIsNone(state.mark_measured(self.root, 40)) - written = state.replace_file - with unittest.mock.patch.object(state, "replace_file", lambda path, data: Failure("full") if path.name == state.RAISED else written(path, data)): - self.assertEqual(state.mark_raised(self.root, [Call("reports", ("Explore",))], "2026-10-15T00:00:00Z"), Failure(f"could not write {self.root / state.RAISED}: full")) + self.assertEqual(state.mark_raised(self.root, [Call("reports", ("Explore",))], "2026-10-15T00:00:00Z", replace=lambda path, data: Failure("full")), Failure(f"could not write {self.root / state.RAISED}: full")) def test_knows_a_raised_call_by_its_fields_whatever_its_line_reads(self): call = Call("reports", ("Explore",)) - self.assertIsNone(state.mark_raised(self.root, [call], "2026-10-15T00:00:00Z")) + self.assertIsNone(state.mark_raised(self.root, [call], "2026-10-15T00:00:00Z", replace_file)) (self.root / state.RAISED).write_text("# Routing calls raised\n\n- 2026-10-15T00:00:00Z Reworded.\n") self.assertEqual(state.raised(self.root).calls, frozenset({call})) diff --git a/bin/rule_hook/test_rule_hook.py b/bin/rule_hook/test_rule_hook.py index 7e969d38..ec9bbd9e 100644 --- a/bin/rule_hook/test_rule_hook.py +++ b/bin/rule_hook/test_rule_hook.py @@ -9,7 +9,6 @@ import sys import tempfile import typing import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -982,6 +981,12 @@ class Hook(unittest.TestCase): git(self.repo, "init", "-q", "-b", "main") git(self.repo, "commit", "-q", "--allow-empty", "-m", "base") git(self.repo, "checkout", "-q", "-b", "chunk") + self.env: dict[str, str] = {} + + def configured(self) -> pathlib.Path: + folder = configured(self, pathlib.Path(self.repo)) + self.env = {"HOME": str(folder.parents[1])} + return folder def test_runs_git_guarded(self): git(self.repo, "config", "core.fsmonitor", "/bin/false") @@ -1109,7 +1114,7 @@ class Hook(unittest.TestCase): self.assertEqual(hook(call, self.repo).returncode, 2) def round_commit(self, title: str, given: str = "") -> subprocess.CompletedProcess: - return hook({"cwd": self.repo, "tool_input": {"command": f"git commit {given}-m '{title}'"}, "tool_name": "Bash"}, self.repo) + return hook({"cwd": self.repo, "tool_input": {"command": f"git commit {given}-m '{title}'"}, "tool_name": "Bash"}, self.repo, self.env) def test_a_round_commit_whose_counts_differ_from_its_report_is_refused(self): home = tempfile.mkdtemp() @@ -1189,7 +1194,7 @@ class Hook(unittest.TestCase): self.assertEqual(self.round_commit("Answer stability round 1: 0 🔴, 1 🟡 (0 misread); c").returncode, 0, "another chunk's rounds, fast-forwarded into the default branch, are not this chunk's") def test_a_commit_is_titled_by_the_configs_template_and_its_round_title_read_through_it(self): - config = configured(self, pathlib.Path(self.repo)) / "config.toml" + config = self.configured() / "config.toml" config.write_text(config.read_text() + 'commit-title = "PAY-{id} - {summary}"\n') git(self.repo, "checkout", "-q", "-b", "item-7") for title in ["PAY-7 - Answer stability round 1: 1 🔴, 0 🟡 (0 misread); a", "PAY-7 - Answer stability round 2: 0 🔴, 1 🟡 (0 misread); c"]: @@ -1205,7 +1210,7 @@ class Hook(unittest.TestCase): self.assertEqual(self.round_commit("Tidy # rule-override commit-title: the client asked for this title").returncode, 0) def test_a_commit_on_a_branch_that_doesnt_fit_the_configs_branch_template_takes_a_bare_title(self): - config = configured(self, pathlib.Path(self.repo)) / "config.toml" + config = self.configured() / "config.toml" config.write_text(config.read_text() + 'commit-title = "PAY-{id} - {summary}"\nbranch = "feature/PAY-{id}"\n') git(self.repo, "checkout", "-q", "-b", "release-review-0.3.0") self.assertEqual(self.round_commit("Release review of 0.3.0", "--allow-empty ").returncode, 0) @@ -1217,7 +1222,7 @@ class Hook(unittest.TestCase): self.assertEqual(self.round_commit("PAY-7 - Tidy").returncode, 0) def test_a_commit_is_refused_naming_the_fault_while_a_config_cannot_be_read(self): - config = configured(self, pathlib.Path(self.repo)) / "config.toml" + config = self.configured() / "config.toml" config.write_text(config.read_text() + 'commit-title = "PAY-{id}"\n') refused = self.round_commit("PAY-7 - Tidy") self.assertEqual(refused.returncode, 2) @@ -1234,21 +1239,21 @@ class Hook(unittest.TestCase): self.assertEqual(self.round_commit("Tidy").returncode, 0) def test_a_commit_while_an_unranked_todo_has_changes_is_blocked(self): - configured(self, pathlib.Path(self.repo)) + self.configured() pathlib.Path(self.repo, "docs").mkdir() for name, content in [("README.md", README), ("docs/decisions.md", DECISIONS), ("todo.md", TODO)]: pathlib.Path(self.repo, name).write_text(content) git(self.repo, "add", "README.md", "docs", "todo.md") git(self.repo, "commit", "-qm", "x", "--no-verify") call = {"cwd": self.repo, "tool_input": {"command": "git add -A && git commit -m x"}, "tool_name": "Bash"} - self.assertEqual(hook(call, self.repo).returncode, 0, "todo.md is unchanged") + self.assertEqual(hook(call, self.repo, self.env).returncode, 0, "todo.md is unchanged") pathlib.Path(self.repo, "todo.md").write_text(TODO + "\n") - ran = hook(call, self.repo) + ran = hook(call, self.repo, self.env) self.assertEqual(ran.returncode, 2) self.assertIn("todo.md: not ranked", ran.stderr) def test_a_staged_todo_change_is_checked_and_no_clean_filter_runs(self): - configured(self, pathlib.Path(self.repo)) + self.configured() pathlib.Path(self.repo, "docs").mkdir() for name, content in [("README.md", README), ("docs/decisions.md", DECISIONS), ("todo.md", TODO), (".gitattributes", "todo.md filter=planted\n")]: pathlib.Path(self.repo, name).write_text(content) @@ -1257,7 +1262,7 @@ class Hook(unittest.TestCase): pathlib.Path(self.repo, "ran").unlink(missing_ok=True) # A stat-dirty index entry, as extracting an archive leaves, is what makes `git status` re-run the filter. os.utime(pathlib.Path(self.repo, "todo.md"), (1, 1)) - ran = hook({"cwd": self.repo, "tool_input": {"command": "git commit -m x"}, "tool_name": "Bash"}, self.repo) + ran = hook({"cwd": self.repo, "tool_input": {"command": "git commit -m x"}, "tool_name": "Bash"}, self.repo, self.env) self.assertEqual(ran.returncode, 2, ran.stderr) self.assertFalse(pathlib.Path(self.repo, "ran").exists()) @@ -1316,6 +1321,7 @@ class TodoRankCheck(unittest.TestCase): pathlib.Path(checkout, "bin", "rank_todo.py").write_text(stub) pathlib.Path(checkout, "todo.md").write_text("# todo\n") self.config = configured(self, pathlib.Path(checkout)) / "config.toml" + self.home = self.config.parents[2] return checkout def test_a_changed_plan_that_cannot_be_read_is_a_breach(self): @@ -1324,16 +1330,16 @@ class TodoRankCheck(unittest.TestCase): self.addCleanup(pathlib.Path(checkout, "todo.md").chmod, 0o600) self.assertEqual(["Permission denied" in detail for detail in self.details(checkout)], [True]) - def details(self, checkout: str) -> list[str]: - return [breach.detail for breach in todo_breaches(checkout, pathlib.Path.home(), pathlib.Path(self.own))] + def details(self, checkout: str, **limit: float) -> list[str]: + return [breach.detail for breach in todo_breaches(checkout, self.home, pathlib.Path(self.own), **limit)] def test_a_checkout_of_the_hooks_repo_runs_its_own_rank_todo(self): checkout = self.changed_plan(f"{self.folder}/clone", self.STUB) self.assertEqual(self.details(checkout), [f"own script checked {checkout}/todo.md"]) top = str(pathlib.Path(checkout).resolve()) - self.assertEqual([(breach.rule, breach.directory) for breach in todo_breaches(checkout, pathlib.Path.home(), pathlib.Path(self.own))], [("todo-rank-check", top)]) + self.assertEqual([(breach.rule, breach.directory) for breach in todo_breaches(checkout, self.home, pathlib.Path(self.own))], [("todo-rank-check", top)]) self.config.unlink() - self.assertEqual([(breach.rule, breach.directory) for breach in todo_breaches(checkout, pathlib.Path.home(), pathlib.Path(self.own))], [("todo-rank-check", top)], "a todo.md with no config") + self.assertEqual([(breach.rule, breach.directory) for breach in todo_breaches(checkout, self.home, pathlib.Path(self.own))], [("todo-rank-check", top)], "a todo.md with no config") def test_its_own_rank_todo_passing_passes(self): self.assertEqual(self.details(self.changed_plan(f"{self.folder}/clone", "")), []) @@ -1344,8 +1350,7 @@ class TodoRankCheck(unittest.TestCase): def test_its_own_rank_todo_running_past_the_limit_is_named(self): checkout = self.changed_plan(f"{self.folder}/clone", "import signal\nsignal.pause()\n") - with unittest.mock.patch("rule_hook.todo_check.OWN_CHECK_SECONDS", 0.2): - self.assertEqual(self.details(checkout), [f"{checkout}/bin/rank_todo.py --check did not finish in 0.2 seconds"]) + self.assertEqual(self.details(checkout, seconds=0.2), [f"{checkout}/bin/rank_todo.py --check did not finish in 0.2 seconds"]) def test_another_repos_rank_todo_never_runs(self): reasons = self.details(self.changed_plan(self.other, self.STUB)) @@ -1375,7 +1380,7 @@ class TodoRankCheck(unittest.TestCase): self.config.write_text(self.config.read_text().replace('"checkout:todo.md"', '"folder:todo.md"')) (self.config.parent / "todo.md").write_text("# todo\n") self.assertEqual(self.details(folder), [f"own script checked {folder}/todo.md"], "checked with the checkout's own rank_todo.py, as a commit there would be") - self.assertEqual([(breach.rule, breach.directory) for breach in todo_breaches(folder, pathlib.Path.home(), pathlib.Path(self.own))], [("todo-rank-check", str(pathlib.Path(folder).resolve()))]) + self.assertEqual([(breach.rule, breach.directory) for breach in todo_breaches(folder, self.home, pathlib.Path(self.own))], [("todo-rank-check", str(pathlib.Path(folder).resolve()))]) git(folder, "add", "-A") git(folder, "commit", "-q", "-m", "plan") self.assertEqual(self.details(folder), [], "committed, so unchanged") diff --git a/bin/rule_hook/todo_check.py b/bin/rule_hook/todo_check.py index 0a938af3..e40c3082 100644 --- a/bin/rule_hook/todo_check.py +++ b/bin/rule_hook/todo_check.py @@ -19,14 +19,14 @@ OWN = pathlib.Path(__file__).resolve().parents[2] OWN_CHECK_SECONDS = 20 -def todo_breaches(directory: str, home: pathlib.Path, own: pathlib.Path = OWN) -> list[Breach]: +def todo_breaches(directory: str, home: pathlib.Path, own: pathlib.Path = OWN, seconds: float = OWN_CHECK_SECONDS) -> list[Breach]: """Why the plan of directory's project is not ranked, where the plan has changed; the plan is the todo.md its config names. directory may also be in a config's folder, a git repo of its own.""" top = guarded_git(directory, "rev-parse", "--show-toplevel") if isinstance(top, Failure): return [] held_here = held(pathlib.Path(top), home) if held_here is not None: - return folder_breaches(top, held_here, own) + return folder_breaches(top, held_here, own, seconds) config = located(top, home, functools.partial(git, guarded=True)) if isinstance(config, Failure): # A checkout whose todo.md changes with no config is one whose config is still to write. An unchanged todo.md may be a third party's, whose clone needs no config. @@ -39,10 +39,10 @@ def todo_breaches(directory: str, home: pathlib.Path, own: pathlib.Path = OWN) - # A plan outside the checkout has no commit in the checkout to differ from, so it is checked on every call. if tracked is not None and not changed(top, todo, tracked): return [] - return [Breach("todo-rank-check", line, top) for line in check_failures(top, todo, config, own)] + return [Breach("todo-rank-check", line, top) for line in check_failures(top, todo, config, own, seconds)] -def folder_breaches(top: str, config: ProjectConfig | Failure, own: pathlib.Path) -> list[Breach]: +def folder_breaches(top: str, config: ProjectConfig | Failure, own: pathlib.Path, seconds: float) -> list[Breach]: """Why the plan kept in top, a config's folder, is not ranked, where the plan has changed in that folder's repo; ranked as for a commit in the project's checkout. A config that fails to load doesn't block anything here, so that the commit that fixes it goes through.""" @@ -53,18 +53,18 @@ def folder_breaches(top: str, config: ProjectConfig | Failure, own: pathlib.Path return [] if not todo.is_file() or not changed(str(config.folder), todo, todo.relative_to(config.folder).as_posix()): return [] - return [Breach("todo-rank-check", line, top) for line in check_failures(str(config.top), todo, config, own)] + return [Breach("todo-rank-check", line, top) for line in check_failures(str(config.top), todo, config, own, seconds)] -def check_failures(top: str, todo: pathlib.Path, config: ProjectConfig, own: pathlib.Path) -> list[str]: +def check_failures(top: str, todo: pathlib.Path, config: ProjectConfig, own: pathlib.Path, seconds: float) -> list[str]: """Why rank_todo.py --check fails on todo. A checkout of the repo the hook runs from checks with its own bin/rank_todo.py, so a branch that changes the table's shape is held to its own rules. Any other checkout checks with the hook's ranking: running its script would run its code before every commit.""" script = pathlib.Path(top) / "bin" / "rank_todo.py" if not script.is_file() or not same_repo(top, own): return file_plan(config, todo).check() try: - ran = subprocess.run([sys.executable, str(script), "--check", str(todo)], capture_output=True, cwd=top, text=True, timeout=OWN_CHECK_SECONDS) + ran = subprocess.run([sys.executable, str(script), "--check", str(todo)], capture_output=True, cwd=top, text=True, timeout=seconds) except subprocess.TimeoutExpired: - return [f"{script} --check did not finish in {OWN_CHECK_SECONDS} seconds"] + return [f"{script} --check did not finish in {seconds} seconds"] except OSError as failure: return [f"{script} --check could not run: {failure}"] if ran.returncode == 0: diff --git a/bin/run-tests b/bin/run-tests index e070a876..05f84ee1 100755 --- a/bin/run-tests +++ b/bin/run-tests @@ -6,4 +6,4 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) from run_tests import main -sys.exit(main.main(sys.argv[1:])) +sys.exit(main.main(sys.argv[1:], main.run)) diff --git a/bin/run_heavy/main.py b/bin/run_heavy/main.py index 27e7a965..4b12e509 100644 --- a/bin/run_heavy/main.py +++ b/bin/run_heavy/main.py @@ -28,15 +28,17 @@ runs its command at once, so a gate that wraps its own steps cannot wait on itse As GNU env, nice and timeout do, it exits 125, saying why, when no command is given or the slot folder or a slot file cannot be made or locked; 126 when the command is found but cannot run; and 127 when it is not found. """ +import fcntl import os import pathlib import shutil import sys +import time import typing from run_heavy.capped_docker import CANNOT_RUN, NOT_FOUND, PATH_ENTRY, SHARE_VARIABLE, without_path_entry from failure import Failure -from machine_load.slots import HELD, slot_count, slot_folder, waited_slot +from machine_load.slots import HELD, Lock, slot_count, slot_folder, waited_slot from machine_load.memory import Memory OWN_FAILURE = 125 @@ -51,7 +53,7 @@ def main(argv: list[str], env: typing.Mapping[str, str], cpus: int | None, memor print(f"run-heavy: no command given; {USAGE}", file=sys.stderr) return OWN_FAILURE if HELD not in env: - slot = held_slot(slot_folder(env, pathlib.Path.home()), slot_count(cpus)) + slot = held_slot(slot_folder(env, pathlib.Path.home()), slot_count(cpus), time.sleep, fcntl.flock) if isinstance(slot, Failure): print(f"run-heavy: {slot.reason}", file=sys.stderr) return OWN_FAILURE @@ -67,11 +69,11 @@ def capping(memory: Memory | None, slots: int, env: typing.Mapping[str, str]) -> return {SHARE_VARIABLE: str(memory.slot_share(slots)), "PATH": os.pathsep.join([str(PATH_ENTRY), path])} -def held_slot(folder: pathlib.Path, slots: int) -> int | Failure: - """The number of the slot this call holds, its lock left for the command to inherit, or why no slot could be taken.""" +def held_slot(folder: pathlib.Path, slots: int, sleep: typing.Callable[[float], None], lock: Lock) -> int | Failure: + """The number of the slot this call holds, its lock left for the command to inherit, or why no slot could be taken; it waits for one with sleep, and locks it with lock.""" try: folder.mkdir(parents=True, exist_ok=True) - slot, fd = waited_slot(folder, slots, lambda: print(f"run-heavy: waiting for a heavy slot, of {slots} on this machine, in {folder}; `fuser {folder}/slot-*` names the processes that hold them", file=sys.stderr, flush=True)) + slot, fd = waited_slot(folder, slots, lambda: print(f"run-heavy: waiting for a heavy slot, of {slots} on this machine, in {folder}; `fuser {folder}/slot-*` names the processes that hold them", file=sys.stderr, flush=True), sleep, lock) except OSError as failure: return Failure(f"cannot take a heavy slot in {folder}: {failure.strerror}") os.set_inheritable(fd, True) diff --git a/bin/run_tests/main.py b/bin/run_tests/main.py index 6bc51c5e..c339649a 100644 --- a/bin/run_tests/main.py +++ b/bin/run_tests/main.py @@ -17,6 +17,7 @@ import shutil import subprocess import sys import tempfile +import typing from machine_load.slots import CPUS_PER_SLOT @@ -92,7 +93,7 @@ def run(argv: list[str], pytest: list[str] = PYTEST, cwd: str | None = None) -> return 0 if code == 0 else 1 -def main(argv: list[str]) -> int: +def main(argv: list[str], run: typing.Callable[[list[str]], int]) -> int: if argv[:1] in (["-h"], ["--help"]): print(__doc__.strip()) return 0 diff --git a/bin/run_tests/test_run_tests.py b/bin/run_tests/test_run_tests.py index 538385b1..d8d1c151 100644 --- a/bin/run_tests/test_run_tests.py +++ b/bin/run_tests/test_run_tests.py @@ -1,6 +1,5 @@ import contextlib import io -from unittest import mock import os import pathlib import re @@ -57,13 +56,12 @@ class Main(unittest.TestCase): for argv in (["-h"], ["--help"], ["--help", "bin/x"]): with self.subTest(argv=argv): out = io.StringIO() - with contextlib.redirect_stdout(out), mock.patch.object(main, "run") as run: - self.assertEqual(main.main(argv), 0) + with contextlib.redirect_stdout(out): + self.assertEqual(main.main(argv, self.fail), 0) self.assertEqual(out.getvalue(), main.__doc__.strip() + "\n") - run.assert_not_called() - with mock.patch.object(main, "run", return_value=1) as run: - self.assertEqual(main.main(["bin/x", "-h"]), 1) - run.assert_called_once_with(["bin/x", "-h"]) + ran = [] + self.assertEqual(main.main(["bin/x", "-h"], lambda argv: ran.append(argv) or 1), 1) + self.assertEqual(ran, [["bin/x", "-h"]]) class RunWithAStandIn(unittest.TestCase): diff --git a/bin/rung_replay/archive.py b/bin/rung_replay/archive.py index 8ee9b28e..ff02578a 100644 --- a/bin/rung_replay/archive.py +++ b/bin/rung_replay/archive.py @@ -5,10 +5,11 @@ import json import pathlib import re import subprocess -import sys import time +import typing from loop_effects.effects import Effects +from failure import Failure # The type of the first line of a run's file, which holds what the stream lacks: claude's arguments, working directory, prompt, exit code and stderr. No event claude writes has this type. HEADER = "replay_run" @@ -21,8 +22,8 @@ def replay_folder(root: pathlib.Path, tool: str, argv: list[str], now: float) -> return root / "replays" / re.sub(r"[^A-Za-z0-9._-]", "_", name) -def archiving(effects: Effects, folder: pathlib.Path) -> Effects: - """effects whose claude also writes each run into folder: a header line, then the stream claude printed. A run is kept whether it failed or not, and a run that repeats another word for word is kept once. A folder that can't be written is reported on stderr, and the replay goes on.""" +def archiving(effects: Effects, folder: pathlib.Path, say: typing.Callable[[str], None]) -> Effects: + """effects whose claude also writes each run into folder: a header line, then the stream claude printed. A run is kept whether it failed or not, and a run that repeats another word for word is kept once. A folder that can't be written is reported to say, and the replay goes on.""" claude = effects.claude def run(args: list[str], cwd: str, prompt: str) -> subprocess.CompletedProcess[str]: @@ -32,17 +33,17 @@ def archiving(effects: Effects, folder: pathlib.Path) -> Effects: folder.mkdir(mode=0o700, parents=True, exist_ok=True) (folder / f"{hashlib.sha256(content.encode()).hexdigest()}.jsonl").write_text(content, encoding="utf-8") except OSError as failure: - print(f"could not keep the run in {folder}: {failure}", file=sys.stderr, flush=True) + say(f"could not keep the run in {folder}: {failure}") return proc return dataclasses.replace(effects, claude=run) -def print_report(folder: pathlib.Path, report: str) -> None: - """Prints the report a replay ends with, and keeps it in folder, beside the runs.""" - print(report) +def kept_report(folder: pathlib.Path, report: str) -> Failure | None: + """Keeps the report a replay ends with in folder, beside the runs; the Failure where it can't.""" try: folder.mkdir(mode=0o700, parents=True, exist_ok=True) (folder / RESULT).write_text(report + "\n", encoding="utf-8") except OSError as failure: - print(f"could not keep the result in {folder}: {failure}", file=sys.stderr, flush=True) + return Failure(f"could not keep the result in {folder}: {failure}") + return None diff --git a/bin/rung_replay/command.py b/bin/rung_replay/command.py index 241cbdae..89467082 100644 --- a/bin/rung_replay/command.py +++ b/bin/rung_replay/command.py @@ -4,6 +4,7 @@ import sys import typing from failure import Failure +from rung_replay.archive import kept_report from model_routing.ladder import Ladder, Rung from claude_code.reply import Limited from loop_state.paths import paths_from @@ -44,3 +45,15 @@ def limited_exit(tool: str, limited: Limited) -> int: def failed(tool: str, reason: str) -> int: print(f"{tool}: {reason}", file=sys.stderr) return 1 + + +def say(text: str) -> None: + """Writes text to stderr, as each replay's progress and each round it leaves out are told.""" + print(text, file=sys.stderr, flush=True) + + +def print_report(folder: pathlib.Path, report: str) -> None: + """Prints the report a replay ends with, and keeps it in folder, beside the runs.""" + print(report) + if kept := kept_report(folder, report): + say(kept.reason) diff --git a/bin/rung_replay/search.py b/bin/rung_replay/search.py index 0e34af0e..84416bbb 100644 --- a/bin/rung_replay/search.py +++ b/bin/rung_replay/search.py @@ -1,7 +1,6 @@ """Replay archived cases on a rung, as judge-replay and review-replay both do; and judge-replay's rung search, which holds each candidate rung to what a reference rung reproduced.""" import concurrent.futures import dataclasses -import sys import typing from failure import Failure @@ -49,6 +48,8 @@ def replayed( replay_one: typing.Callable[[Case, Rung], Answer | Limited], reproduced: typing.Callable[[Case, Answer], list[Aim]], what: str, + say: typing.Callable[[str], None], + at_once: int, climbs: typing.Sequence[Rung] = (), ) -> Replay[Case, Aim, Answer] | Limited: """Replays the cases on the reference rung, then on each climb and each candidate, on the cases where the reference reproduced anything. @@ -57,7 +58,7 @@ def replayed( The candidates are tried highest first, until one misses an item the reference reproduced. Going down, every candidate at or above the one named was tried and didn't miss anything, so the result needs no assumption that a higher rung does at least as well; and a miss high up spares the cheaper rungs their replays. """ - on_reference = replayed_on(cases, reference, replay_one, what) + on_reference = replayed_on(cases, reference, replay_one, what, say, at_once) if isinstance(on_reference, Limited): return on_reference held = [(case, aim) for case, reply in zip(cases, on_reference) if (aim := reproduced(case, reply))] @@ -66,14 +67,14 @@ def replayed( return Replay(on_reference, [], None) climbed: list[Tried[Case, Aim, Answer]] = [] for rung in climbs: - climb = tried_on(held, rung, replay_one, reproduced, what) + climb = tried_on(held, rung, replay_one, reproduced, what, say, at_once) if isinstance(climb, Limited): return climb climbed.append(climb) tried: list[Tried[Case, Aim, Answer]] = [] lowest = None for rung in candidates: - candidate = tried_on(held, rung, replay_one, reproduced, what) + candidate = tried_on(held, rung, replay_one, reproduced, what, say, at_once) if isinstance(candidate, Limited): return candidate tried.append(candidate) @@ -89,9 +90,11 @@ def tried_on( replay_one: typing.Callable[[Case, Rung], Answer | Limited], reproduced: typing.Callable[[Case, Answer], list[Aim]], what: str, + say: typing.Callable[[str], None], + at_once: int, ) -> Tried[Case, Aim, Answer] | Limited: """rung and each item, of those the reference reproduced in the held cases, that its replies missed.""" - replies = replayed_on([case for case, _ in held], rung, replay_one, what) + replies = replayed_on([case for case, _ in held], rung, replay_one, what, say, at_once) if isinstance(replies, Limited): return replies misses: list[Miss[Case, Aim, Answer]] = [] @@ -101,10 +104,10 @@ def tried_on( return Tried(rung, misses) -def replayed_on(cases: list[Case], rung: Rung, replay_one: typing.Callable[[Case, Rung], Answer | Limited], what: str) -> list[Answer] | Limited: - """Each case's reply on rung, AT_ONCE at a time; a usage limit cancels the rest.""" - print(f"replaying {len(cases)} {what} on {rung}", file=sys.stderr, flush=True) - with concurrent.futures.ThreadPoolExecutor(AT_ONCE) as pool: +def replayed_on(cases: list[Case], rung: Rung, replay_one: typing.Callable[[Case, Rung], Answer | Limited], what: str, say: typing.Callable[[str], None], at_once: int) -> list[Answer] | Limited: + """Each case's reply on rung, at_once at a time, after telling say what it replays; a usage limit cancels the rest.""" + say(f"replaying {len(cases)} {what} on {rung}") + with concurrent.futures.ThreadPoolExecutor(at_once) as pool: running = [pool.submit(replay_one, case, rung) for case in cases] replies: list[Answer] = [] for future in running: diff --git a/bin/rung_replay/test_rung_replay.py b/bin/rung_replay/test_rung_replay.py index f22a5082..21dd083b 100644 --- a/bin/rung_replay/test_rung_replay.py +++ b/bin/rung_replay/test_rung_replay.py @@ -9,11 +9,11 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) -from rung_replay.archive import archiving, print_report, replay_folder -from rung_replay.command import down_from +from rung_replay.archive import archiving, replay_folder +from rung_replay.search import AT_ONCE, first_reply, Miss, Replay, replayed, Tried +from rung_replay.command import down_from, print_report from loop_effects.effects import Effects from failure import Failure -from rung_replay.search import first_reply, Miss, Replay, replayed, Tried from claude_code.reply import Limited from model_routing.ladder_file import read_ladder, SHIPPED from model_routing.ladder import Rung @@ -34,8 +34,7 @@ def search(cases, candidates, replies, climbs=()): reply = replies[rung] return reply if isinstance(reply, Limited) else reply[case] - with contextlib.redirect_stderr(io.StringIO()): - return replayed(cases, REFERENCE, candidates, replay_one, lambda _, reply: sorted(reply), "cases", climbs), asked + return replayed(cases, REFERENCE, candidates, replay_one, lambda _, reply: sorted(reply), "cases", lambda text: None, AT_ONCE, climbs), asked class Replayed(unittest.TestCase): @@ -96,8 +95,8 @@ class ArchivedRuns(unittest.TestCase): def setUp(self): self.folder = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) / "replays" / "tool-1" - def run_archived(self, replies): - return archiving(effects(lambda args, cwd, prompt: replies.pop(0)), self.folder).claude + def run_archived(self, replies, say=lambda text: None): + return archiving(effects(lambda args, cwd, prompt: replies.pop(0)), self.folder, say).claude def kept(self): return sorted(self.folder.glob("*.jsonl")) @@ -134,16 +133,15 @@ class ArchivedRuns(unittest.TestCase): raise FileNotFoundError(2, "No such file or directory") with self.assertRaises(FileNotFoundError): - archiving(effects(missing), self.folder).claude([], "/r", "p") + archiving(effects(missing), self.folder, self.fail).claude([], "/r", "p") self.assertFalse(self.folder.exists()) # A file where the folder's parent should be. self.folder.parent.parent.mkdir(exist_ok=True) self.folder.parent.write_text("") proc = subprocess.CompletedProcess(["claude"], 0, "{}", "") - err = io.StringIO() - with contextlib.redirect_stderr(err): - self.assertIs(self.run_archived([proc])([], "/r", "p"), proc) - self.assertIn(f"could not keep the run in {self.folder}: ", err.getvalue()) + said = [] + self.assertIs(self.run_archived([proc], said.append)([], "/r", "p"), proc) + self.assertEqual([text.startswith(f"could not keep the run in {self.folder}: ") for text in said], [True]) def test_prints_the_report_and_keeps_it_beside_the_runs(self): out = io.StringIO() diff --git a/bin/score-outcomes b/bin/score-outcomes index b28b6bdd..f29ff785 100755 --- a/bin/score-outcomes +++ b/bin/score-outcomes @@ -6,4 +6,4 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) from score_outcomes import main -sys.exit(main.main(sys.argv[1:])) +sys.exit(main.main(sys.argv[1:], pathlib.Path.home())) diff --git a/bin/score_outcomes/main.py b/bin/score_outcomes/main.py index d3c9452f..4c79bb9f 100644 --- a/bin/score_outcomes/main.py +++ b/bin/score_outcomes/main.py @@ -100,8 +100,8 @@ from failure import Failure from score_outcomes.results import file_lines, fixed_later, item_text, misreads, nearest, render, report, Result, results_of from plan.todo_table import item_details, LEVELS, risk, TOP_SCORE from text_files.markdown_text import listed +from project_config.main import located, title_templates from plan.named_paths import named_files -from project_config.main import project, title_templates from transcript_archive.location import project_archive from score_outcomes.reversals import reversal_lines from record_store.stores import store @@ -119,7 +119,7 @@ UNMERGED = [ ] -def main(argv: list[str]) -> int: +def main(argv: list[str], home: pathlib.Path) -> int: if argv[:1] in (["-h"], ["--help"]): print(__doc__.strip()) return 0 @@ -133,11 +133,11 @@ def main(argv: list[str]) -> int: default, note = found if note: print(f"score-outcomes: {note}", file=sys.stderr) - results = read_results(default) + results = read_results(default, home) if isinstance(results, Failure): return failed(results) - anchored = read_anchors() - reversal = read_reversals() + anchored = read_anchors(home) + reversal = read_reversals(home) print(report_text(results, anchored, dict(zip(GIVEN, map(int, argv))) if argv else None, reversal)) unread = [found for found in (anchored, reversal) if isinstance(found, Failure)] for failure in unread: @@ -167,34 +167,34 @@ def failed(failure: Failure) -> int: return 1 -def read_anchors() -> Anchors | Failure: +def read_anchors(home: pathlib.Path) -> Anchors | Failure: """The project's anchors as they stand in the store its config names, so a branch that edits them shows its edit; none where it keeps none.""" - config = project() + config = located(None, home) if isinstance(config, Failure): return config text = store(config, "score-anchors").read() return text if isinstance(text, Failure) else parse_anchors(text) -def read_reversals() -> list[str] | Failure: +def read_reversals(home: pathlib.Path) -> list[str] | Failure: """The reversals in loop-claude's archive of this project, which the main checkout's path keys, whichever worktree this runs in.""" common = git("rev-parse", "--path-format=absolute", "--git-common-dir") if isinstance(common, Failure): return common - return reversal_lines(project_archive(str(pathlib.Path(common.strip()).parent), pathlib.Path.home(), os.environ)) + return reversal_lines(project_archive(str(pathlib.Path(common.strip()).parent), home, os.environ)) -def read_results(default: str) -> list[Result] | Failure: +def read_results(default: str, home: pathlib.Path) -> list[Result] | Failure: """What each chunk on the default branch's first-parent line measured.""" if partial := full_history(): return partial - if why := unsupported(None, pathlib.Path.home()): + if why := unsupported(None, home): return Failure(f"this project is not supported yet: {why}") root = top() if isinstance(root, Failure): return root run = functools.partial(git, cwd=root) - templates = title_templates(None, pathlib.Path.home()) + templates = title_templates(None, home) if isinstance(templates, Failure): return templates chunks = branch_chunks(default, run, templates) diff --git a/bin/score_outcomes/test_score_outcomes.py b/bin/score_outcomes/test_score_outcomes.py index fe9c8bfe..58c9de22 100644 --- a/bin/score_outcomes/test_score_outcomes.py +++ b/bin/score_outcomes/test_score_outcomes.py @@ -570,12 +570,12 @@ class Reversals(unittest.TestCase): class ReadAnchors(unittest.TestCase): def test_fails_on_anchors_it_cannot_read_rather_than_scoring_without_them(self): checkout = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) - configured(self, checkout) + home = configured(self, checkout).parents[1] self.enterContext(contextlib.chdir(checkout)) anchors = checkout.resolve() / "docs" / "score-anchors.md" anchors.parent.mkdir() anchors.write_bytes(b"\xff") - self.assertEqual(read_anchors(), Failure(f"cannot read {anchors}: not UTF-8 on line 1")) + self.assertEqual(read_anchors(home), Failure(f"cannot read {anchors}: not UTF-8 on line 1")) if __name__ == "__main__": diff --git a/bin/test_rank_todo.py b/bin/test_rank_todo.py index ba56c8d8..19053a6e 100644 --- a/bin/test_rank_todo.py +++ b/bin/test_rank_todo.py @@ -16,15 +16,23 @@ from todo_item.doubles import READABLE_TOUCHES, UNREADABLE_BULLETS class Main(unittest.TestCase): + # The home of a test that writes no config. + home = pathlib.Path("/no-home") + + def configured(self, checkout: str) -> pathlib.Path: + folder = configured(self, pathlib.Path(checkout)) + self.home = folder.parents[1] + return folder + def run_main(self, *argv): with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()) as said: - code = rank_todo.main(list(argv)) + code = rank_todo.main(list(argv), self.home) return code, said.getvalue() def test_ranks_the_file_in_place_and_check_writes_nothing(self): with tempfile.TemporaryDirectory() as temp: todo = pathlib.Path(temp, "todo.md") - configured(self, pathlib.Path(temp)) + self.configured(temp) self.enterContext(contextlib.chdir(temp)) todo.write_text(TODO) pathlib.Path(temp, "README.md").write_text(README) @@ -43,7 +51,7 @@ class Main(unittest.TestCase): def test_ranks_the_plan_and_reads_the_decisions_where_the_config_puts_them(self): with tempfile.TemporaryDirectory() as temp: - folder = configured(self, pathlib.Path(temp)) + folder = self.configured(temp) self.enterContext(contextlib.chdir(temp)) config = folder / "config.toml" config.write_text(config.read_text().replace('"checkout:todo.md"', '"folder:todo.md"').replace('"checkout:docs/decisions.md"', '"folder:decisions.md"')) @@ -61,7 +69,7 @@ class Main(unittest.TestCase): def test_refuses_a_project_whose_plan_is_in_jira_and_writes_no_file(self): with tempfile.TemporaryDirectory() as temp: - config = configured(self, pathlib.Path(temp)) / "config.toml" + config = self.configured(temp) / "config.toml" self.enterContext(contextlib.chdir(temp)) config.write_text(config.read_text().replace('"checkout:todo.md"', '"jira:https://acme.atlassian.net/browse/ABC-12"') + 'env-file = "~/acme.env"\njira-score-formula = "V"\njira-score-bar = 1\n') todo = pathlib.Path(temp, "todo.md") @@ -74,7 +82,7 @@ class Main(unittest.TestCase): def test_refuses_a_file_that_is_not_the_plan_the_config_names_and_a_checkout_no_config_names(self): with tempfile.TemporaryDirectory() as temp: - folder = configured(self, pathlib.Path(temp)) + folder = self.configured(temp) self.enterContext(contextlib.chdir(temp)) pathlib.Path(temp, "todo.md").write_text(TODO) pathlib.Path(temp, "README.md").write_text(README) @@ -94,7 +102,7 @@ class Main(unittest.TestCase): under = "item 2 scores 3.1, under the bar 4; delete it, or mark it with a reason the bar does not apply" with tempfile.TemporaryDirectory() as temp: todo = pathlib.Path(temp, "todo.md") - configured(self, pathlib.Path(temp)) + self.configured(temp) self.enterContext(contextlib.chdir(temp)) todo.write_text(TODO.replace("`Bar = 2`", "`Bar = 4`")) pathlib.Path(temp, "README.md").write_text(README) @@ -106,7 +114,7 @@ class Main(unittest.TestCase): def test_check_fails_on_each_item_whose_touches_list_it_cannot_read(self): with tempfile.TemporaryDirectory() as temp: todo = pathlib.Path(temp, "todo.md") - configured(self, pathlib.Path(temp)) + self.configured(temp) self.enterContext(contextlib.chdir(temp)) pathlib.Path(temp, "README.md").write_text(README) pathlib.Path(temp, "docs").mkdir() @@ -125,7 +133,7 @@ class Main(unittest.TestCase): def test_says_to_reshape_an_old_shape_table_but_passes_the_check(self): with tempfile.TemporaryDirectory() as temp: todo = pathlib.Path(temp, "todo.md") - configured(self, pathlib.Path(temp)) + self.configured(temp) self.enterContext(contextlib.chdir(temp)) todo.write_text(OLD_SHAPE) pathlib.Path(temp, "README.md").write_text(README) diff --git a/bin/text_files/compressed.py b/bin/text_files/compressed.py index d640f494..37b31c45 100644 --- a/bin/text_files/compressed.py +++ b/bin/text_files/compressed.py @@ -6,10 +6,10 @@ import errno import gzip import os import pathlib +import typing import zlib from failure import Failure -from text_files.file_writes import replace_file SUFFIX = ".gz" @@ -43,20 +43,20 @@ def stored_glob(folder: pathlib.Path, pattern: str) -> list[pathlib.Path]: return sorted({*folder.glob(pattern), *(path.with_name(path.name.removesuffix(SUFFIX)) for path in folder.glob(pattern + SUFFIX))}) -def compress(path: pathlib.Path) -> Failure | None: - """Replaces path with its gzip, which keeps path's times: writes the gzip, reads it back, and removes path only once it reads back path's bytes. A failure keeps path.""" +def compress(path: pathlib.Path, *, pack: typing.Callable[..., bytes], replace: typing.Callable[[pathlib.Path, bytes], Failure | None], sync: typing.Callable[[int], None]) -> Failure | None: + """Replaces path with its gzip, which keeps path's times: writes the gzip, reads it back, and removes path only once it reads back path's bytes. A failure keeps path. pack compresses as gzip.compress does, replace writes as replace_file does, and sync flushes as os.fsync does.""" packed = gzipped(path) try: before = path.stat() data = path.read_bytes() - if unwritten := replace_file(packed, gzip.compress(data, mtime=0)): + if unwritten := replace(packed, pack(data, mtime=0)): return Failure(f"could not compress {path}: {unwritten.reason}") if gzip.decompress(packed.read_bytes()) != data: return Failure(f"could not compress {path}: {packed} reads back other bytes") os.utime(packed, ns=(before.st_atime_ns, before.st_mtime_ns)) # On disk before the plain file goes, so a crash cannot leave an empty gzip in its place. - synced(packed) - synced(packed.parent) + synced(packed, sync) + synced(packed.parent, sync) after = path.stat() # Written while it compressed: the plain file keeps the newer bytes, which readers prefer, and a later run compresses them. if (after.st_size, after.st_mtime_ns) == (before.st_size, before.st_mtime_ns): @@ -66,10 +66,10 @@ def compress(path: pathlib.Path) -> Failure | None: return None -def synced(path: pathlib.Path) -> None: +def synced(path: pathlib.Path, sync: typing.Callable[[int], None]) -> None: handle = os.open(path, os.O_RDONLY) try: - os.fsync(handle) + sync(handle) finally: os.close(handle) diff --git a/bin/text_files/doubles.py b/bin/text_files/doubles.py new file mode 100644 index 00000000..c8563b8b --- /dev/null +++ b/bin/text_files/doubles.py @@ -0,0 +1,10 @@ +"""What the suites that compress a file share: compress, bound to what really compresses, writes and flushes.""" +import functools +import gzip +import os + +from text_files import compressed +from text_files.file_writes import replace_file + +# A test names pack, replace or sync to swap that one. +compress = functools.partial(compressed.compress, pack=gzip.compress, replace=replace_file, sync=os.fsync) diff --git a/bin/text_files/test_compressed.py b/bin/text_files/test_compressed.py index e2e01b10..479866f5 100644 --- a/bin/text_files/test_compressed.py +++ b/bin/text_files/test_compressed.py @@ -1,6 +1,5 @@ import errno import gzip -from unittest import mock import os import pathlib import sys @@ -9,6 +8,7 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) +from text_files.doubles import compress from text_files import compressed from failure import Failure @@ -19,7 +19,7 @@ class Compress(unittest.TestCase): plain = pathlib.Path(root, "t.jsonl") plain.write_bytes(b'{"a": 1}\n') os.utime(plain, (1000, 2000)) - self.assertIsNone(compressed.compress(plain)) + self.assertIsNone(compress(plain)) self.assertFalse(plain.exists()) packed = pathlib.Path(root, "t.jsonl.gz") # A gzip header's own time is 0, so compressing the same bytes twice writes the same file. @@ -37,31 +37,28 @@ class Compress(unittest.TestCase): synced.append((os.readlink(f"/proc/self/fd/{handle}"), plain.exists())) fsync(handle) - with mock.patch.object(compressed.os, "fsync", record): - self.assertIsNone(compressed.compress(plain)) + self.assertIsNone(compress(plain, sync=record)) self.assertEqual(synced, [(os.path.realpath(f"{plain}.gz"), True), (os.path.realpath(root), True)]) def test_a_gzip_it_cannot_write_keeps_the_original_and_says_why(self): with tempfile.TemporaryDirectory() as root: plain = pathlib.Path(root, "t.jsonl") plain.write_bytes(b"kept\n") - with mock.patch.object(compressed, "replace_file", return_value=Failure("disk full")): - self.assertEqual(compressed.compress(plain), Failure(f"could not compress {plain}: disk full")) + self.assertEqual(compress(plain, replace=lambda path, data: Failure("disk full")), Failure(f"could not compress {plain}: disk full")) self.assertEqual(plain.read_bytes(), b"kept\n") def test_a_file_it_cannot_read_fails_naming_it(self): with tempfile.TemporaryDirectory() as root: folder = pathlib.Path(root, "t.jsonl") folder.mkdir() - failed = compressed.compress(folder) + failed = compress(folder) self.assertTrue(failed.reason.startswith(f"could not compress {folder}: [Errno {errno.EISDIR}]"), failed.reason) def test_a_copy_that_reads_back_other_bytes_keeps_the_original_and_fails(self): with tempfile.TemporaryDirectory() as root: plain = pathlib.Path(root, "t.jsonl") plain.write_bytes(b"kept\n") - with mock.patch.object(compressed.gzip, "compress", return_value=gzip.compress(b"other\n")): - failed = compressed.compress(plain) + failed = compress(plain, pack=lambda data, **kwargs: gzip.compress(b"other\n")) self.assertIsInstance(failed, Failure) self.assertIn(str(plain), failed.reason) self.assertEqual(plain.read_bytes(), b"kept\n") @@ -70,15 +67,12 @@ class Compress(unittest.TestCase): with tempfile.TemporaryDirectory() as root: plain = pathlib.Path(root, "t.jsonl") plain.write_bytes(b"one\n") - packing = compressed.gzip.compress - def grow_meanwhile(data, **kwargs): with open(plain, "ab") as handle: handle.write(b"two\n") - return packing(data, **kwargs) + return gzip.compress(data, **kwargs) - with mock.patch.object(compressed.gzip, "compress", grow_meanwhile): - self.assertIsNone(compressed.compress(plain)) + self.assertIsNone(compress(plain, pack=grow_meanwhile)) self.assertEqual(plain.read_bytes(), b"one\ntwo\n") self.assertEqual(compressed.read_stored(plain), b"one\ntwo\n") diff --git a/bin/todo-item b/bin/todo-item index 1f73d950..09ba0244 100755 --- a/bin/todo-item +++ b/bin/todo-item @@ -6,4 +6,4 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) from todo_item import main -sys.exit(main.main(sys.argv[1:])) +sys.exit(main.main(sys.argv[1:], pathlib.Path(), pathlib.Path.home(), sys.stdin)) diff --git a/bin/todo_item/main.py b/bin/todo_item/main.py index 1749843d..f59fcfcf 100644 --- a/bin/todo_item/main.py +++ b/bin/todo_item/main.py @@ -94,6 +94,7 @@ from plan_store.plans import no_item, PlanStore, TAGLINE, Written from plan.release import parsed, Release, version as release_version from plan_store.stores import store as plan_store from plan.touches import predicted +from project_config.settings import ProjectConfig from record_store.stores import RecordStore, store from todo_item.table import shown, table from todo_item.edits import with_bullet @@ -104,16 +105,17 @@ LETTERS = ("L", "E", "I", "S") NAMED = {"release": "Release", "blocked_by": BLOCKED_BY, "exempt": "Exempt", "tagline": TAGLINE, "moves": MOVES} -def main(argv: list[str], folder: pathlib.Path = pathlib.Path(), stdin: typing.TextIO = sys.stdin) -> int: +# plan_of is plan_store, but in the tests of a plan in Jira, which hand it a plan in memory. +def main(argv: list[str], folder: pathlib.Path, home: pathlib.Path, stdin: typing.TextIO, plan_of: typing.Callable[[ProjectConfig], PlanStore] = plan_store) -> int: if argv[:1] in (["-h"], ["--help"]): print(__doc__.strip()) return 0 args = parser().parse_args(argv) - config = located(str(folder), pathlib.Path.home()) + config = located(str(folder), home) if isinstance(config, Failure): done: str | Failure = config else: - plan = plan_store(config) + plan = plan_of(config) match args.command: case "list": done = list_(args, plan) diff --git a/bin/todo_item/test_todo_item.py b/bin/todo_item/test_todo_item.py index c95157ff..1e88b82d 100644 --- a/bin/todo_item/test_todo_item.py +++ b/bin/todo_item/test_todo_item.py @@ -4,7 +4,6 @@ import pathlib import sys import tempfile import unittest -import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent)) @@ -41,6 +40,9 @@ class TodoItem(unittest.TestCase): self.addCleanup(temp.cleanup) self.folder = pathlib.Path(temp.name) self.config = configured(self, self.folder) / "config.toml" + self.home = self.config.parents[2] + # The plan main is handed in place of the one the config names. + self.plan_of = {} self.todo = self.folder / "todo.md" self.changelog = self.folder / "CHANGELOG.md" (self.folder / "README.md").write_text(README) @@ -52,16 +54,16 @@ class TodoItem(unittest.TestCase): def run_main(self, *argv, stdin: str | io.StringIO = ""): with contextlib.redirect_stdout(io.StringIO()) as out, contextlib.redirect_stderr(io.StringIO()) as err: - code = main.main(list(argv), self.folder, io.StringIO(stdin) if isinstance(stdin, str) else stdin) + code = main.main(list(argv), self.folder, self.home, io.StringIO(stdin) if isinstance(stdin, str) else stdin, **self.plan_of) return code, out.getvalue(), err.getvalue() def exit_code(self, *argv): with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()), self.assertRaises(SystemExit) as raised: - main.main(list(argv), self.folder, io.StringIO()) + main.main(list(argv), self.folder, self.home, io.StringIO()) return raised.exception.code def assert_ranked(self): - self.assertEqual(file_plan(located(str(self.folder), pathlib.Path.home()), self.todo).check(), []) + self.assertEqual(file_plan(located(str(self.folder), self.home), self.todo).check(), []) def details(self): text = self.todo.read_text() @@ -275,9 +277,7 @@ class FinishInJira(TodoItem): super().setUp() self.config.write_text(self.config.read_text().replace('"checkout:todo.md"', f'"{JIRA_PLAN}"') + 'env-file = "~/acme.env"\n' + JIRA_SCORING) self.plan = MemoryPlan({3: {"Release": "0.1.0", "Item": "**Count the runs.**", "L": "1", "E": "2", "I": "3", "S": "1"}}) - patched = unittest.mock.patch.object(main, "plan_store", return_value=self.plan) - patched.start() - self.addCleanup(patched.stop) + self.plan_of = {"plan_of": lambda config: self.plan} def test_finishes_without_either_flag_and_doesnt_touch_the_changelog(self): self.changelog.write_text(CHANGELOG) @@ -409,7 +409,7 @@ class List(TodoItem): def test_fails_on_a_release_that_is_no_version(self): errors = io.StringIO() with contextlib.redirect_stderr(errors), self.assertRaises(SystemExit) as raised: - main.main(["list", "--release", "next"], self.folder, io.StringIO()) + main.main(["list", "--release", "next"], self.folder, self.home, io.StringIO()) self.assertEqual(raised.exception.code, 2) self.assertIn("argument --release: 'next' is not a semver version", errors.getvalue()) @@ -513,7 +513,7 @@ class UnreadableTouches(TodoItem): def test_a_plan_in_jira_gets_no_unreadable_list_either(self): self.config.write_text(self.config.read_text().replace('"checkout:todo.md"', f'"{JIRA_PLAN}"') + 'env-file = "~/acme.env"\n' + JIRA_SCORING) plan = MemoryPlan({3: {"Release": "0.1.0", "Item": "**Count the runs.**", "L": "1", "E": "2", "I": "3", "S": "1"}}) - self.enterContext(unittest.mock.patch.object(main, "plan_store", return_value=plan)) + self.plan_of = {"plan_of": lambda config: plan} detail = f"Touches:\n\n{UNREADABLE_BULLETS[0]}\n" self.assertEqual(self.run_main(*ADD, stdin=detail)[:2], (1, "")) self.assertEqual(self.run_main("edit", "3", "--detail", stdin=detail)[:2], (1, "")) @@ -536,7 +536,7 @@ class Move(TodoItem): class OtherFile(TodoItem): def test_ranking_works_on_the_file_it_is_given_not_the_configured_plan(self): - config = located(str(self.folder), pathlib.Path.home()) + config = located(str(self.folder), self.home) other = self.folder / "other.md" other.write_text("not a plan\n") self.assertNotEqual(file_plan(config, other).check(), []) diff --git a/bin/touch_predictions/doubles.py b/bin/touch_predictions/doubles.py index 22189131..6798e7b3 100644 --- a/bin/touch_predictions/doubles.py +++ b/bin/touch_predictions/doubles.py @@ -3,10 +3,15 @@ from failure import Failure from plan_store.todo_md import plan_of -def plan(*items: tuple[int, str, str, str]) -> list: - """A plan's items, each given as its ID, release, the IDs that block it and the bullets of its prediction, none for an item without one.""" +def todo(*items: tuple[int, str, str, str]) -> str: + """A todo.md's text for items, each given as its ID, release, the IDs that block it and the bullets of its prediction, none for an item without one.""" rows = "".join(f"| {id_} | {release} | {blocked} | | **Do {id_}.** | 1 | 1 | 1 | 1 | 1 | 1 by 1 | 10.0 |\n" for id_, release, blocked, _ in items) details = "".join(f"### {id_}. Do {id_}.\n\nTouches:\n\n{bullets}\n\n" for id_, _, _, bullets in items if bullets) - found = plan_of(f"# todo\n\n## Scoring\n\n`Score = 100 * V / (2**S + 8)`\n\n`Bar = 0.1`\n\n`Next ID = 99`\n\n## Items\n\n| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |\n|---|---|---|---|---|---|---|---|---|---|---|---|\n{rows}\n## Details\n\n{details}") + return f"# todo\n\n## Scoring\n\n`Score = 100 * V / (2**S + 8)`\n\n`Bar = 0.1`\n\n`Next ID = 99`\n\n## Items\n\n| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |\n|---|---|---|---|---|---|---|---|---|---|---|---|\n{rows}\n## Details\n\n{details}" + + +def plan(*items: tuple[int, str, str, str]) -> list: + """The items of the plan that todo writes for items.""" + found = plan_of(todo(*items)) assert not isinstance(found, Failure), found return found.items diff --git a/bin/touch_predictions/main.py b/bin/touch_predictions/main.py index 2c77b66a..65314b33 100644 --- a/bin/touch_predictions/main.py +++ b/bin/touch_predictions/main.py @@ -66,7 +66,7 @@ def main(argv: list[str]) -> int: print("usage: touch-predictions record ...", file=sys.stderr) return 1 config = project() - done = config if isinstance(config, Failure) else recorded(config, sorted({int(id_) for id_ in argv[1:]})) + done = config if isinstance(config, Failure) else recorded(config, sorted({int(id_) for id_ in argv[1:]}), time.time, git) if isinstance(done, Failure): print(f"touch-predictions: {done.reason}", file=sys.stderr) return 1 @@ -74,7 +74,7 @@ def main(argv: list[str]) -> int: return 0 -def recorded(config: ProjectConfig, ids: list[int], now: typing.Callable[[], float] = time.time, run: Git = git) -> str | Failure: +def recorded(config: ProjectConfig, ids: list[int], now: typing.Callable[[], float], run: Git) -> str | Failure: """Appends the entry of the chunk that takes the items ids, stamped at now and read from the commits run shows, and says so; or says that the project keeps no record.""" store = touch_predictions_store(config) if store is None: diff --git a/bin/touch_predictions/test_touch_predictions.py b/bin/touch_predictions/test_touch_predictions.py index 1d2e1659..4c92aaac 100644 --- a/bin/touch_predictions/test_touch_predictions.py +++ b/bin/touch_predictions/test_touch_predictions.py @@ -343,7 +343,7 @@ class RecordCommandTest(unittest.TestCase): path = self.set_record() config = located(str(self.checkout), self.home) assert isinstance(config, ProjectConfig), config - self.assertNotIsInstance(main.recorded(config, [3], now=lambda: 0.0), Failure) + self.assertNotIsInstance(main.recorded(config, [3], lambda: 0.0, main.git), Failure) self.assertIn("## Chunk 3, 1970-01-01T00:00:00Z, at ", path.read_text(encoding="utf-8")) def test_a_second_run_replaces_the_entry_and_leaves_the_record_out_of_what_was_touched(self) -> None: diff --git a/bin/transcript_archive/test_transcript_archive.py b/bin/transcript_archive/test_transcript_archive.py index 6438b90b..438291d0 100644 --- a/bin/transcript_archive/test_transcript_archive.py +++ b/bin/transcript_archive/test_transcript_archive.py @@ -10,11 +10,12 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from transcript_archive.project_key import checkout_of, project_key -from text_files import compressed +from text_files.doubles import compress from failure import Failure from transcript_archive.judge_run import JudgeRun from transcript_archive import outcomes, read, write from transcript_archive.location import project_archive +from text_files.file_writes import replace_file from text_files.utc_stamp import utc NOW = 1790000000 @@ -27,10 +28,10 @@ class Archive(unittest.TestCase): transcript = pathlib.Path(root, "t.jsonl") transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n') with contextlib.redirect_stderr(io.StringIO()): - self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished("m", "working", "j", ""), NOW)) + self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished("m", "working", "j", ""), NOW, replace=replace_file)) (kept_outcomes,) = kept.glob("*/*.outcomes.md") kept_outcomes.unlink() - self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("m", "Ctrl-C"), NOW)) + self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("m", "Ctrl-C"), NOW, replace=replace_file)) self.assertEqual(kept_outcomes.read_text(), "Task: Do it.\n\n2026-09-21T14:13:20Z Stopped: Ctrl-C\n\nWorker: m\n") @@ -45,7 +46,7 @@ class JudgeRuns(unittest.TestCase): with contextlib.redirect_stderr(io.StringIO()): for offset in (0, 1): outcome = outcomes.finished("w", "merged", "j", judge_run=self.judged) - self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcome, NOW + offset, judge_run=self.judged)) + self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcome, NOW + offset, judge_run=self.judged, replace=replace_file)) (folder,) = kept.glob("*/") (file,) = folder.glob("judge/*") self.assertEqual(file, folder / self.judged.file) @@ -91,7 +92,7 @@ class JudgeRuns(unittest.TestCase): def test_names_the_folder_it_cannot_make_in_the_failure_to_keep_a_run(self): with tempfile.TemporaryDirectory() as root: (pathlib.Path(root) / "judge").write_text("a file where the folder should be") - failure = write.keep_judge_run(pathlib.Path(root), self.judged) + failure = write.keep_judge_run(pathlib.Path(root), self.judged, replace_file) self.assertRegex(failure.reason, f"^could not archive the transcript: {root}/judge: ") self.assertNotIn("None", failure.reason) @@ -101,10 +102,10 @@ class JudgeRuns(unittest.TestCase): transcript = pathlib.Path(root, "t.jsonl") transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n') with contextlib.redirect_stderr(io.StringIO()): - self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w", "x"), NOW)) + self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w", "x"), NOW, replace=replace_file)) (folder,) = kept.glob("*/") (folder / "judge").write_text("a file where the folder should be") - failure = write.archive(kept, str(transcript), "Do it.", outcomes.finished("w", "e", "j", judge_run=self.judged), NOW + 1, judge_run=self.judged) + failure = write.archive(kept, str(transcript), "Do it.", outcomes.finished("w", "e", "j", judge_run=self.judged), NOW + 1, judge_run=self.judged, replace=replace_file) self.assertIsInstance(failure, Failure) self.assertNotIn("Verdict: finished", next(folder.glob("*.outcomes.md")).read_text()) @@ -124,7 +125,7 @@ class CopyFor(unittest.TestCase): with tempfile.TemporaryDirectory() as temp: copy = pathlib.Path(temp, "agent-a.jsonl") copy.write_bytes(b"first\n") - self.assertIsNone(compressed.compress(copy)) + self.assertIsNone(compress(copy)) self.assertIsNone(write.copy_for(copy, b"first\n")) self.assertEqual(write.copy_for(copy, b"first\nsecond\n"), copy) self.assertEqual(write.copy_for(copy, b"other\n"), copy.with_name(f"agent-a.jsonl.{hashlib.sha256(b'other' + bytes([10])).hexdigest()}")) @@ -141,9 +142,9 @@ class Compressed(unittest.TestCase): subagent.parent.mkdir(parents=True) subagent.write_text('{"sub": 1}\n') with contextlib.redirect_stderr(io.StringIO()): - self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.finished("w", "merged", "j"), NOW)) + self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.finished("w", "merged", "j"), NOW, replace=replace_file)) for copy in kept.rglob("*.jsonl"): - self.assertIsNone(compressed.compress(copy)) + self.assertIsNone(compress(copy)) return transcript def test_a_compressed_copy_takes_the_next_outcome_and_is_not_copied_again(self): @@ -151,7 +152,7 @@ class Compressed(unittest.TestCase): transcript = self.archived(root) kept = pathlib.Path(root, "archive") with contextlib.redirect_stderr(io.StringIO()): - self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w", "Ctrl-C"), NOW + 1)) + self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w", "Ctrl-C"), NOW + 1, replace=replace_file)) self.assertEqual([path.name.endswith(".gz") for path in kept.rglob("*.jsonl*")], [True, True]) self.assertIn("Stopped: Ctrl-C", next(kept.glob("*/*.outcomes.md")).read_text()) @@ -208,7 +209,7 @@ class Outcomes(unittest.TestCase): transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n') with contextlib.redirect_stderr(io.StringIO()): for worker in ("z-first", "a-last"): - self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished(worker, "working", "j", ""), NOW)) + self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished(worker, "working", "j", ""), NOW, replace=replace_file)) self.assertEqual(read.latest_worker(kept, str(transcript)), "a-last") def test_names_the_worker_the_latest_outcome_of_the_transcripts_chunk_records(self): @@ -218,9 +219,9 @@ class Outcomes(unittest.TestCase): transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n') self.assertIsNone(read.latest_worker(kept, str(transcript))) with contextlib.redirect_stderr(io.StringIO()): - self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished("w1", "working", "j", ""), NOW)) + self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished("w1", "working", "j", ""), NOW, replace=replace_file)) transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n{"more": 1}\n') - self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w2", "Ctrl-C"), NOW + 1)) + self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w2", "Ctrl-C"), NOW + 1, replace=replace_file)) self.assertEqual(read.latest_worker(kept, str(transcript)), "w2") self.assertIsInstance(read.latest_worker(kept, str(pathlib.Path(root, "gone.jsonl"))), Failure) @@ -247,8 +248,8 @@ class Outcomes(unittest.TestCase): transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n') self.assertEqual(read.last_spent(kept, str(transcript)), "") with contextlib.redirect_stderr(io.StringIO()): - self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished("w", "e", "j", "", annotation=outcomes.Annotation("c", "Spent since the chunk began, in tokens of x:\n- worker, m: 1")), NOW)) - self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w", "Ctrl-C"), NOW + 1)) + self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished("w", "e", "j", "", annotation=outcomes.Annotation("c", "Spent since the chunk began, in tokens of x:\n- worker, m: 1")), NOW, replace=replace_file)) + self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w", "Ctrl-C"), NOW + 1, replace=replace_file)) self.assertEqual(read.last_spent(kept, str(transcript)), utc(NOW)) gone = read.last_spent(kept, str(pathlib.Path(root, "gone.jsonl"))) self.assertIsInstance(gone, Failure) diff --git a/bin/transcript_archive/write.py b/bin/transcript_archive/write.py index 683d857b..00837bff 100644 --- a/bin/transcript_archive/write.py +++ b/bin/transcript_archive/write.py @@ -6,15 +6,18 @@ import typing from transcript_archive.location import chunk_folder, outcomes_of from failure import Failure from transcript_archive.judge_run import JudgeRun -from text_files.file_writes import make_private, replace_file +from text_files.file_writes import make_private from text_files.compressed import read_stored, stored from claude_code.session import session_files from transcript_archive.outcomes import TASK from text_files.utc_stamp import utc +# Writes a file whole, as text_files.file_writes.replace_file does. +Replace = typing.Callable[[pathlib.Path, bytes], Failure | None] -def archive(project_archive: pathlib.Path, path: str, task: str, outcome: str, at: float, note: typing.Callable[[str], None] = lambda text: None, judge_run: JudgeRun | None = None) -> Failure | None: - """Keeps the transcript at path, its outcomes file and its session's files in project_archive, in the layout this package's docstring gives; the failure that stopped it, or None. The outcome is stamped and appended to the outcomes file, which opens with the task only when the copy is new. Where a new copy lands, note is told where. A judge_run the outcome names is kept first, so the outcome never names a run the archive lacks.""" + +def archive(project_archive: pathlib.Path, path: str, task: str, outcome: str, at: float, note: typing.Callable[[str], None] = lambda text: None, judge_run: JudgeRun | None = None, *, replace: Replace) -> Failure | None: + """Keeps the transcript at path, its outcomes file and its session's files in project_archive, in the layout this package's docstring gives; the failure that stopped it, or None. The outcome is stamped and appended to the outcomes file, which opens with the task only when the copy is new. Where a new copy lands, note is told where. replace writes each file, as replace_file does. A judge_run the outcome names is kept first, so the outcome never names a run the archive lacks.""" try: with open(path, "rb") as handle: raw = handle.read() @@ -23,24 +26,24 @@ def archive(project_archive: pathlib.Path, path: str, task: str, outcome: str, a except OSError as failure: return unarchived(failure.filename, failure.strerror) for sibling in session_files(path): - if sibling != path and (unkept := mirror(pathlib.Path(sibling), folder / "session" / os.path.basename(sibling))): + if sibling != path and (unkept := mirror(pathlib.Path(sibling), folder / "session" / os.path.basename(sibling), replace)): return unkept - if judge_run and (unkept := keep_judge_run(folder, judge_run)): + if judge_run and (unkept := keep_judge_run(folder, judge_run, replace)): return unkept copy = folder / f"{hashlib.sha256(raw).hexdigest()}.jsonl" outcomes = outcomes_of(copy) stamped = f"{utc(at)} {outcome}" if stored(copy) and outcomes.exists(): - return add_outcome(outcomes, stamped) + return add_outcome(outcomes, stamped, replace) # The copy lands last, so a copy on disk always has its outcomes and its session's files beside it. for dest, data in ((outcomes, f"{TASK}{task}\n\n{stamped}\n".encode()), (copy, raw)): - if unwritten := replace_file(dest, data): + if unwritten := replace(dest, data): return unarchived(dest, unwritten.reason) note(f"transcript: {copy}") return None -def keep_judge_run(folder: pathlib.Path, run: JudgeRun) -> Failure | None: +def keep_judge_run(folder: pathlib.Path, run: JudgeRun, replace: Replace) -> Failure | None: kept = folder / run.file if stored(kept): return None @@ -48,7 +51,7 @@ def keep_judge_run(folder: pathlib.Path, run: JudgeRun) -> Failure | None: make_private(kept.parent) except OSError as failure: return unarchived(failure.filename, failure.strerror) - if unwritten := replace_file(kept, run.content()): + if unwritten := replace(kept, run.content()): return unarchived(kept, unwritten.reason) return None @@ -57,17 +60,17 @@ def unarchived(where: object, why: str | None) -> Failure: return Failure(f"could not archive the transcript: {where}: {why}") -def add_outcome(outcomes: pathlib.Path, outcome: str) -> Failure | None: +def add_outcome(outcomes: pathlib.Path, outcome: str, replace: Replace) -> Failure | None: try: kept = outcomes.read_bytes() except OSError as failure: return unarchived(failure.filename, failure.strerror) - if unwritten := replace_file(outcomes, kept + f"\n{outcome}\n".encode()): + if unwritten := replace(outcomes, kept + f"\n{outcome}\n".encode()): return unarchived(outcomes, unwritten.reason) return None -def mirror(source: pathlib.Path, dest: pathlib.Path) -> Failure | None: +def mirror(source: pathlib.Path, dest: pathlib.Path, replace: Replace) -> Failure | None: try: files = [source] if source.is_file() else files_under(source) except OSError as failure: @@ -81,7 +84,7 @@ def mirror(source: pathlib.Path, dest: pathlib.Path) -> Failure | None: make_private(copy.parent) except OSError as failure: return unarchived(failure.filename, failure.strerror) - if unwritten := replace_file(copy, data): + if unwritten := replace(copy, data): return unarchived(copy, unwritten.reason) return None diff --git a/docs/decisions.md b/docs/decisions.md index 06cc0ebb..64a14f5f 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -4,7 +4,7 @@ Each project principle states one rule that the decisions below apply; their ent 1. **Cut cost only where a measurement of archived chunks shows quality holds.** Chunk bundling, the round cap, which fixes rerun a pass, a reviewer of its own for each pass, the review a branch changing only Markdown no agent follows gets, the passes kept for the release review, review opt-outs, the security pass, compaction and the routing ladder all apply it. 2. **A measurement files or ranks work, and never blocks a merge.** The comprehension floor and check, and a reading-cost rise, apply it. A merge waits on a scoring run the plan calls for only until the run is recorded, whatever it scores. -3. **Each file has one writer.** `~/.claude/loop-claude-models.json` is loop-claude's, `~/.claude.json` is Claude Code's, and `~/.claude` moves only through `deploy-release`. The exception is `~/.claude/settings.json`, which Claude Code writes from any session. Git's clean filter `bin/settings_filter.py` keeps each machine's model and Claude Code's key order out of every commit, and `README.md` → "Update" carries its other changes into this repo. +3. **Each file has one writer.** `~/.claude/loop-claude-models.json` is `save_model`'s, the one function loop-claude and do-epic both write it through, `~/.claude.json` is Claude Code's, and `~/.claude` moves only through `deploy-release`. The exception is `~/.claude/settings.json`, which Claude Code writes from any session. Git's clean filter `bin/settings_filter.py` keeps each machine's model and Claude Code's key order out of every commit, and `README.md` → "Update" carries its other changes into this repo. 4. **A rule a program can check is enforced by a program, and reviewers rule on the rest.** rule-hook and its overrides, `docker-run-memory`, the refused writes of reviewers under `agents/`, and CI with `main`'s branch protection apply it. 5. **A chunk can't vouch for itself.** What a chunk writes is approved by a review pass, by the human, or by a precedent the repo held before the chunk began. So an unanswered ask leaves the PR unmerged, the human approves the premises chunk and runs `deploy-release --cut`, and the judge holds a chunk open while an answer the human typed isn't recorded anywhere. 6. **An unattended run never waits on a prompt, and a lasting failure stops it.** Every claude loop-claude starts bypasses permissions inside its sandbox, each claude it opens on a terminal has `CLAUDE_CODE_SANDBOXED` set, and interactive sessions run in auto mode with the dangerous-mode prompt skipped. A failed archive stops the loop, while a `claude` that cannot start, as during an update, is retried. @@ -96,7 +96,7 @@ Valid while the archive's file names, the Vocabulary and the panel keep these wo ## bin/: the tools send no OTLP traces, logs or metrics, and loop-claude's archive holds the trace of each cycle -2026-11-16, lilleman, from the release review of 0.11.0. The tools send no traces, logs or metrics over OTLP, the OpenTelemetry protocol, to any collector. That departs from `principles/technical-principles.md` → "All critical flows should have good traces", whose sub-bullet asks for traceparent OTLP. lilleman ruled the departure permanent, so no plan item ends it. Serves goal 4: the tools run on the human's own machines, beside client code and transcripts, and whatever goes to a collector leaves the machine. loop-claude's archive holds the trace of each cycle instead: its transcripts and its `.outcomes.md` entry, from the worker's first run to the judge's verdict. Valid while goal 4 holds. +2026-11-16, lilleman, from the release review of 0.11.0. The tools send no traces, logs or metrics over OTLP, the OpenTelemetry protocol, to any collector. That applies `principles/technical-principles.md` → "All critical flows should have good traces", whose last sub-bullet keeps the trace of a tool that runs only on its user's own machine on that machine. lilleman ruled this permanent, and the release review of 0.12.0 wrote the sub-bullet from that ruling. Serves goal 4: the tools run on the human's own machines, beside client code and transcripts, and whatever goes to a collector leaves the machine. loop-claude's archive holds the trace of each cycle instead: its transcripts and its `.outcomes.md` entry, from the worker's first run to the judge's verdict. Valid while goal 4 holds. ## bin/: the tools support only the latest stable Python, and the tests, written in plain unittest, run on it in four pytest-xdist workers @@ -191,9 +191,9 @@ Valid while `~/.claude` is a checkout of this repo. 2026-10-30, lilleman, todo item 6. Serves goal 1. Until then loop-claude marked each worktree trusted in `~/.claude.json`, reading the file, changing it and writing it back. Claude Code owns and writes that file, so a write it made in between could be lost. Claude Code 2.1.314 treats every folder as trusted while `CLAUDE_CODE_SANDBOXED` is set, and doesn't persist that trust. A claude started in a new folder with the variable set showed no dialog, and Claude Code recorded the folder untrusted. `hand_over` sets the variable for every claude it opens on a terminal: for `/talk`, a release ready to cut and the routing calls. The variable tells Claude Code it runs inside a sandbox, which holds here: loop-claude runs only in the sandbox README → Install names (`loop-claude: every claude it starts bypasses permissions inside its sandbox`). Claude Code doesn't document the variable. Where a later version drops it, the trust dialog comes back on those claudes, and nothing is lost. Valid while Claude Code honours `CLAUDE_CODE_SANDBOXED`. -## loop-claude: each project's model lives in `~/.claude/loop-claude-models.json`, which only loop-claude writes +## loop-claude: each project's model lives in `~/.claude/loop-claude-models.json`, which only the chunk drivers write, through one locked function -2026-09-26, lilleman. Serves goal 1. Claude Code rewrites the user settings' `model` whenever one is picked inside a session, so a loop reading settings runs whatever was picked last; `~/.claude/loop-claude-models.json` has loop-claude as its one writer, and every claude it starts gets that model as `--model`. The file is per machine, outside the synced allowlist, since a tracked file the loop writes would dirty the checkout. 2026-10-02, lilleman: goal 2 shares the setup across machines, and a project's model is each machine's own choice, so a new machine asks once per project, which is not asking the same thing twice (goal 1.2). Valid while Claude Code keeps rewriting the settings' model from inside a session. +2026-09-26, lilleman. Serves goal 1. Claude Code rewrites the user settings' `model` whenever one is picked inside a session, so a loop reading settings runs whatever was picked last; `~/.claude/loop-claude-models.json` has one writer, `save_model` in `bin/chunk_driver/project/model_choice.py`, which loop-claude and do-epic both call and which holds a lock from its read to its write; every claude they start gets that model as `--model`. The file is per machine, outside the synced allowlist, since a tracked file the loop writes would dirty the checkout. 2026-10-02, lilleman: goal 2 shares the setup across machines, and a project's model is each machine's own choice, so a new machine asks once per project, which is not asking the same thing twice (goal 1.2). Valid while Claude Code keeps rewriting the settings' model from inside a session. ## loop-claude: every claude it starts bypasses permissions inside its sandbox diff --git a/principles/technical-principles.md b/principles/technical-principles.md index ca64ef0b..95e988a9 100644 --- a/principles/technical-principles.md +++ b/principles/technical-principles.md @@ -101,6 +101,7 @@ - Two spellings that can disagree are a correctness bug, whatever the reason for having both: derive one from the other, or reject the combination. * All critical flows should have good traces, from where something was initiated until it completed. - Use traceparent OTLP and standardized logging for this. + - A tool that runs only on its user's own machine keeps its trace on that machine, as an archive of each run, and doesn't send anything to a collector. * Prefer the vendor-neutral option, and know what leaving would cost. - A supplier is a component like any other, so compose-not-entangle's test is the whole test. - Where two options do the job, the one that ties us to no supplier wins by default — dependency, protocol, format, service, provider and storage alike. Choosing neutral is usually free; it is the leaving that is expensive, so the test is what an exit costs and whether anyone has priced it.