Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s

This commit is contained in:
2026-11-19 12:42:03 +01:00
parent 8ca612b7a4
commit cb3135bc8c
124 changed files with 1437 additions and 793 deletions
+1 -1
View File
@@ -219,7 +219,7 @@ Install puts only `loop-claude` on `PATH`; call the others by their path, such a
### Packages under `bin/` ### 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. - `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. - `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.
+22 -8
View File
@@ -8,16 +8,18 @@ adf-codec has two pairs of calls, and each function here names the one it runs:
""" """
import contextlib import contextlib
import dataclasses import dataclasses
import fcntl
import hashlib import hashlib
import json import json
import os import os
import pathlib import pathlib
import subprocess import subprocess
import sys import sys
import time
import typing import typing
from failure import Failure 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 = pathlib.Path(__file__).resolve().parent / "node_shim"
SHIM_FILES = ("Dockerfile", "package.json", "package-lock.json", "shim.mjs") 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] 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: try:
done = call(args, input=text, capture_output=True, text=True, encoding="utf-8", timeout=seconds) done = call(args, input=text, capture_output=True, text=True, encoding="utf-8", timeout=seconds)
except FileNotFoundError: except FileNotFoundError:
@@ -73,16 +81,22 @@ def docker(args: list[str], text: str, seconds: int, call: typing.Callable[...,
@contextlib.contextmanager @contextlib.contextmanager
def heavy_slot(env: typing.Mapping[str, str] = os.environ, slots: int | None = None) -> typing.Iterator[Failure | None]: def machine_slot() -> 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.""" """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: if HELD in env:
yield None yield None
return return
folder = slot_folder(env, pathlib.Path.home()) folder = slot_folder(env, home)
count = slot_count(usable_cpus()) if slots is None else slots
try: try:
folder.mkdir(parents=True, exist_ok=True) 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: except OSError as failure:
yield Failure(f"cannot take a heavy slot in {folder}: {failure.strerror}") yield Failure(f"cannot take a heavy slot in {folder}: {failure.strerror}")
return return
@@ -92,7 +106,7 @@ def heavy_slot(env: typing.Mapping[str, str] = os.environ, slots: int | None = N
os.close(fd) 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 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.""" 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."""
+18 -9
View File
@@ -1,4 +1,5 @@
import contextlib import contextlib
import fcntl
import json import json
import os import os
import subprocess import subprocess
@@ -161,7 +162,7 @@ def test_docker_passes_the_text_on_stdin_and_returns_the_output():
seen.update(kwargs, args=args) seen.update(kwargs, args=args)
return subprocess.CompletedProcess(args, 3, "out", "err") 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} 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): def call(args, **kwargs):
raise FileNotFoundError 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") 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): def call(args, **kwargs):
raise subprocess.TimeoutExpired(args, 7) 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): 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 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): 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 held is None
assert slots.taken_slot(tmp_path / "run-heavy", 1) is None assert slots.taken_slot(tmp_path / "run-heavy", 1, fcntl.flock) is None
slot, fd = slots.taken_slot(tmp_path / "run-heavy", 1) slot, fd = slots.taken_slot(tmp_path / "run-heavy", 1, fcntl.flock)
os.close(fd) os.close(fd)
def test_a_process_under_run_heavy_takes_no_second_slot(tmp_path): def test_a_process_under_run_heavy_takes_no_second_slot(tmp_path):
(tmp_path / "run-heavy").mkdir() (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: 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 assert held is None
finally: finally:
os.close(fd) 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): 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") (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'}: ") assert isinstance(held, Failure) and held.reason.startswith(f"cannot take a heavy slot in {tmp_path / 'run-heavy'}: ")
+1 -1
View File
@@ -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: 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.""" """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: def step(cycle: Cycle, state: Active) -> Event | Stop:
-1
View File
@@ -7,7 +7,6 @@ import shutil
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
import uuid import uuid
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
-1
View File
@@ -3,7 +3,6 @@ import json
import pathlib import pathlib
import sys import sys
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
-1
View File
@@ -3,7 +3,6 @@ import io
import pathlib import pathlib
import sys import sys
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
+4 -7
View File
@@ -11,7 +11,6 @@ import sys
import tempfile import tempfile
import types import types
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 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 model_routing.ladder import ProjectRung, Rung
from agent_cycle.doubles import PROMPTS, WINDOW from agent_cycle.doubles import PROMPTS, WINDOW
from model_routing.ladder_file import read_ladder, SHIPPED from model_routing.ladder_file import read_ladder, SHIPPED
from text_files.file_writes import replace_file
from model_routing.route import Route from model_routing.route import Route
from agent_cycle import routing from agent_cycle import routing
from agent_cycle import run, stuck_workers, worker_prompts 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): def test_stops_when_a_judge_run_cannot_be_archived(self):
self.archive.mkdir(parents=True) self.archive.mkdir(parents=True)
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("finished")]) fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("finished")])
def replace_file(path, data): fake.replace_file = lambda path, data: Failure("disk full") if "/judge/" in str(path) else replace_file(path, data)
return Failure("disk full") if "/judge/" in str(path) else real_replace_file(path, data) stop = run.run_cycle(self.cycle(fake))
real_replace_file = write.replace_file
with unittest.mock.patch("transcript_archive.write.replace_file", replace_file):
stop = run.run_cycle(self.cycle(fake))
self.assertEqual(stop.code, 1) self.assertEqual(stop.code, 1)
self.assertRegex(stop.reason, r"^could not archive the transcript: .*/judge/[0-9a-f]{64}\.json: disk full$") 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}}} 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") 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()): 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.")]) 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)) run.run_cycle(self.cycle(fake))
latest = max(self.archived_outcomes(), key=len) latest = max(self.archived_outcomes(), key=len)
-1
View File
@@ -3,7 +3,6 @@ import pathlib
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
-1
View File
@@ -1,7 +1,6 @@
import pathlib import pathlib
import sys import sys
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
-1
View File
@@ -2,7 +2,6 @@ import pathlib
import subprocess import subprocess
import sys import sys
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
-1
View File
@@ -6,7 +6,6 @@ import pathlib
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
+38 -15
View File
@@ -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. # 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" 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. # 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) @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 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): 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") 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. # 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.
try: fields, properties = raw.get("fields"), raw.get("properties", {})
fields = raw["fields"] if not isinstance(fields, dict):
return Entry(key, fields, blocked_by_links(fields), raw.get("properties", {}).get(property_, {})) return Failure(f"{key} {SHAPE}: its fields are no object")
except (AttributeError, KeyError, TypeError) as failure: if not isinstance(properties, dict):
return Failure(unshaped(key, failure)) 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: 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.""" """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: 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 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: 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. 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.""" """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 body: dict[str, object] = {"jql": jql, "maxResults": PAGE} | asked
found: list[object] = [] 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) page = http("POST", f"{site}/rest/api/3/search/jql", authorization, body)
if isinstance(page, Failure): if isinstance(page, Failure):
return page return page
@@ -129,9 +134,27 @@ def searched(site: str, what: str, jql: str, asked: dict[str, object], authoriza
if not page.get("nextPageToken"): if not page.get("nextPageToken"):
return found return found
body = body | {"nextPageToken": page["nextPageToken"]} 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, ...]: def blocked_by_links(fields: dict[str, typing.Any]) -> tuple[Link, ...] | Failure:
"""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.""" """The "is blocked by" links among an issue's `issuelinks` field, or how that field is not in the shape Jira documents."""
return tuple(Link(link["inwardIssue"]["key"], link["id"]) for link in fields["issuelinks"] if "inwardIssue" in link and link["type"]["inward"] == "is blocked by") 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)
+57
View File
@@ -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()
+1 -3
View File
@@ -5,7 +5,6 @@ import subprocess
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 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): def read(self, run=None, titles=BARE):
"""branch_chunks over main, run by git_repo's git, or else by run.""" """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): 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. """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.
+24 -7
View File
@@ -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. """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 contextlib
import fcntl import fcntl
import pathlib import pathlib
import typing import typing
from failure import Failure
from transcript_archive.project_key import project_key from transcript_archive.project_key import project_key
from agent_cycle import tmux from agent_cycle import tmux
@@ -21,12 +22,28 @@ HANDED = "LOOP_CLAUDE_"
@contextlib.contextmanager @contextlib.contextmanager
def starting(folder: pathlib.Path) -> typing.Iterator[None]: 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. Raises OSError where the file cannot be opened or locked.""" """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."""
folder.mkdir(parents=True, exist_ok=True) with locked(folder / LOCK) as failure:
with open(folder / LOCK, "a") as lock: yield failure
fcntl.flock(lock, fcntl.LOCK_EX)
yield
@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: def project_lock(root: str, home: pathlib.Path) -> pathlib.Path:
+21 -8
View File
@@ -5,11 +5,11 @@ import sys
import typing import typing
from claude_code.model_catalog import catalog_models from claude_code.model_catalog import catalog_models
from chunk_driver.claims import CLAIMS, locked
from loop_effects.effects import Effects, say from loop_effects.effects import Effects, say
from failure import Failure from failure import Failure
from agent_cycle.stop import needs_human, needs_terminal, Stop from agent_cycle.stop import needs_human, needs_terminal, Stop
from loop_state.paths import Paths from loop_state.paths import Paths
from text_files.file_writes import replace_json
@dataclasses.dataclass(frozen=True) @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. # 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" JUDGE_MODEL = "LOOP_CLAUDE_JUDGE_MODEL"
WORKER_MODEL = "LOOP_CLAUDE_WORKER_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: 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) chosen = ask_for_model(project_key, saved, paths.model_catalog, driver, effects)
if isinstance(chosen, Stop): if isinstance(chosen, Stop):
return chosen return chosen
# Reread: another loop may have saved its project while this one waited on the human. if failure := save_model(project_key, chosen, paths, effects.replace_file):
models = read_models_file(saved) return Stop(1, failure.reason)
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}")
say(f"{project_key} runs on {chosen}", effects) say(f"{project_key} runs on {chosen}", effects)
return chosen 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: 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: try:
with open(saved, encoding="utf-8") as handle: with open(saved, encoding="utf-8") as handle:
models = json.load(handle) models = json.load(handle)
+67 -1
View File
@@ -1,8 +1,10 @@
import dataclasses
import fcntl
import json
import pathlib import pathlib
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) 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 loop_effects.doubles import FakeEffects
from chunk_driver.drivers import LOOP_CLAUDE from chunk_driver.drivers import LOOP_CLAUDE
from chunk_driver.project import model_choice 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 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")) 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): class ModelPrompt(unittest.TestCase):
OFFERED = [("claude-opus-5-5", "Opus 5.5"), ("claude-haiku-4-5", "Haiku 4.5")] OFFERED = [("claude-opus-5-5", "Opus 5.5"), ("claude-haiku-4-5", "Haiku 4.5")]
-1
View File
@@ -2,7 +2,6 @@ import pathlib
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2]))
+3 -2
View File
@@ -16,6 +16,7 @@ from agent_cycle.verdicts import Finished, TakenIn, Unfinished
from transcript_archive import outcomes, write from transcript_archive import outcomes, write
from model_routing.ladder import ProjectRung, Rung from model_routing.ladder import ProjectRung, Rung
from model_routing.ladder_file import read_ladder, SHIPPED from model_routing.ladder_file import read_ladder, SHIPPED
from text_files.file_writes import replace_file
from model_routing.route import Route from model_routing.route import Route
from agent_cycle import run from agent_cycle import run
from claude_code import session from claude_code import session
@@ -59,7 +60,7 @@ class Steps(unittest.TestCase):
transcript.write_text(json.dumps({"type": "queue-operation", "timestamp": "1"}) + "\n") transcript.write_text(json.dumps({"type": "queue-operation", "timestamp": "1"}) + "\n")
before = Route(self.FABLE_LOW, 3, 3, 4) before = Route(self.FABLE_LOW, 3, 3, 4)
with contextlib.redirect_stderr(io.StringIO()): 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)) 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)) 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()): 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") transcript.write_text(json.dumps({"type": "queue-operation", "timestamp": "1"}) + "\n")
before = Route(self.FABLE_LOW, 3, 3, 4) before = Route(self.FABLE_LOW, 3, 3, 4)
with contextlib.redirect_stderr(io.StringIO()): 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)) 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)) 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))
+41
View File
@@ -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()
+17 -17
View File
@@ -1,4 +1,5 @@
import contextlib import contextlib
import functools
import io import io
import json import json
import os import os
@@ -7,7 +8,6 @@ import sys
import tempfile import tempfile
import types import types
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 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 chunk_driver import cycle_start
from failure import Failure from failure import Failure
from loop_effects.doubles import FakeEffects, NOW, STAMP 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 transcript_archive import outcomes, write
from model_routing.ladder import ProjectRung, Rung from model_routing.ladder import ProjectRung, Rung
from model_routing.ladder_file import read_ladder, SHIPPED 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 model_routing.route import Route, WorkerLine
from claude_code import session 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): class SetupVersion(unittest.TestCase):
def test_names_the_release_and_the_commit_or_why_git_cannot_tell(self): def test_names_the_release_and_the_commit_or_why_git_cannot_tell(self):
with tempfile.TemporaryDirectory() as root: with tempfile.TemporaryDirectory() as root:
isolated = {"GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_NOSYSTEM": "1", "GIT_CEILING_DIRECTORIES": root} git = functools.partial(git_repo.git, env={**os.environ, "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)
gone = cycle_start.setup_version(pathlib.Path(root), git) self.assertRegex(gone, r"^unknown: [^\n]*not a git repository")
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"]):
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)
self.assertIsInstance(git(*step, cwd=root), str) head = git("rev-parse", "HEAD", cwd=root).strip()
head = git("rev-parse", "HEAD", cwd=root).strip() self.assertEqual(cycle_start.setup_version(pathlib.Path(root), git), f"{head[:7]} ({head})")
self.assertEqual(cycle_start.setup_version(pathlib.Path(root), git), f"{head[:7]} ({head})") git("tag", "v0.1.0", cwd=root)
git("tag", "v0.1.0", cwd=root) self.assertEqual(cycle_start.setup_version(pathlib.Path(root), git), f"v0.1.0 ({head})")
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)
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()
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})")
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 test_names_why_git_cannot_name_the_commit_on_one_line(self):
def fake_git(*args, cwd=None): 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") transcript.write_text(json.dumps({"type": "queue-operation", "timestamp": "1"}) + "\n")
self.assertIsNone(cycle_start.archived_route(archive, a_loop(FakeEffects().effects(), session=opened))) self.assertIsNone(cycle_start.archived_route(archive, a_loop(FakeEffects().effects(), session=opened)))
with contextlib.redirect_stderr(io.StringIO()): 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): with contextlib.redirect_stderr(stderr):
self.assertEqual(cycle_start.archived_route(archive, a_loop(FakeEffects().effects(), session=opened)), route) self.assertEqual(cycle_start.archived_route(archive, a_loop(FakeEffects().effects(), session=opened)), route)
self.assertEqual(stderr.getvalue(), "") self.assertEqual(stderr.getvalue(), "")
with contextlib.redirect_stderr(io.StringIO()): 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: with contextlib.redirect_stderr(io.StringIO()) as stderr:
self.assertIsNone(cycle_start.archived_route(archive, a_loop(FakeEffects().effects(), session=opened))) 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') 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')
+1 -2
View File
@@ -1,5 +1,4 @@
"""The human approval that `chunk-pr merge` waits for where the project's config sets `wait-for-approval`.""" """The human approval that `chunk-pr merge` waits for where the project's config sets `wait-for-approval`."""
import time
import typing import typing
from forge.reviews import APPROVED, CHANGES_REQUESTED, github_decision, REVIEW_REQUIRED, waiting_threads 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 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.""" """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): for waited in range(0, WAIT_SECONDS + 1, POLL_SECONDS):
read = approval(number, run) read = approval(number, run)
+2 -2
View File
@@ -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) 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]: case ["merge", *given]:
finishes = finishes_of(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"]: case ["clean"]:
return cleaned(run) return cleaned(run)
return Failure(USAGE) return Failure(USAGE)
@@ -305,7 +305,7 @@ def finishes_of(given: list[str]) -> list[list[str]] | None:
return finishes 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.""" """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) found = pull(run)
if isinstance(found, Failure): if isinstance(found, Failure):
+18 -18
View File
@@ -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): def test_a_branch_behind_the_default_branch_merges_it_in_and_pushes_before_the_checks(self):
checkout = self.moved(Checkout(GITHUB, open=True)) 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.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)) 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 = self.moved(Checkout(GITHUB, open=True))
checkout.answers[tuple(MERGE)] = Failure("git merge --quiet: CONFLICT") checkout.answers[tuple(MERGE)] = Failure("git merge --quiet: CONFLICT")
checkout.answers[("git", "diff", "--name-only", "--diff-filter=U")] = "bin/tool.py\n" 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.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.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)) 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): 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)) checkout, folder = self.with_plan(Checkout(GITHUB, open=True))
files = [f"{folder}/todo.md", f"{folder}/CHANGELOG.md"] 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)[:1], [[self.TODO_ITEM, "finish", "7", "--changelog", "Exports keep their order."]])
self.assertEqual(changes(checkout)[-4:], [ self.assertEqual(changes(checkout)[-4:], [
["git", "-C", folder, "add", "--", *files], ["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): 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) 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.assertNotIn([self.TODO_ITEM, "finish", "7"], checkout.ran)
self.assertEqual([command[3] for command in changes(checkout)[:-1]], ["add", "commit", "origin"]) 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"]) 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): def test_each_item_the_plan_no_longer_holds_is_named(self):
checkout, _ = self.with_plan(Checkout(GITHUB, open=True), held=False) 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") 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): def test_a_plan_that_turns_unreadable_inside_the_turn_leaves_it_unmerged(self):
checkout, _ = self.with_plan(Checkout(GITHUB, open=True)) 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: def run_whose_second_show_fails(command: list[str]) -> str | Failure:
shows.extend([command] if command[1:2] == ["show"] else []) 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) 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) self.assertNotIn([self.TODO_ITEM, "finish", "7"], checkout.ran)
def test_a_finish_that_changes_no_file_commits_and_pushes_nothing(self): 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" checkout.answers[("git", "-C", folder, "status", "--porcelain", "--", f"{folder}/todo.md", f"{folder}/CHANGELOG.md")] = "\n"
else: else:
checkout.answers.update({(self.CONFIG, "plan"): "PAY-1\n", (self.CONFIG, "changelog"): f"{folder}/none.md\n"}) 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]]) 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): 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, folder = self.with_plan(Checkout(GITHUB, open=True))
checkout.answers[tuple(BRANCH)] = "feature/PAY-{id}\n" 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"]]) 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): 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): with self.subTest(failing):
checkout, _ = self.with_plan(Checkout(GITHUB, open=True)) 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") 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) self.assertNotIn(self.PUSHED, checkout.ran)
def test_a_step_of_the_turn_that_fails_leaves_it_unmerged_with_that_failure(self): 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: def failing_run(command: list[str]) -> str | Failure:
asked = ["<plan>" if part == folder else part for part in command] asked = ["<plan>" if part == folder else part for part in command]
return Failure("that step failed") if tuple(asked[:len(failing)]) == tuple(failing) else run(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"]) 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): 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, _ = 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") 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]) 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): 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)) checkout, _ = self.with_plan(Checkout(GITHUB, open=True))
commit = lambda: next(index for index, command in enumerate(checkout.ran) if command[3:4] == ["commit"]) 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)) self.assertLess(commit(), checkout.ran.index(OWED))
checkout, _ = self.with_plan(Checkout(GITHUB, open=True)) checkout, _ = self.with_plan(Checkout(GITHUB, open=True))
checkout.answers[tuple(OWED)] = Failure("the chunk owes 1 scoring run") 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) 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): 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 = Checkout(GITHUB, open=True)
checkout.answers[tuple(WAITS)] = "true\n" checkout.answers[tuple(WAITS)] = "true\n"
checkout.threads = [self.thread("bin/asked.py", "reviewer")] 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.assertIsInstance(failed, Failure)
self.assertIn("waits for an answer, in bin/asked.py;", failed.reason) self.assertIn("waits for an answer, in bin/asked.py;", failed.reason)
self.assertEqual(changes(checkout), []) self.assertEqual(changes(checkout), [])
@@ -766,7 +766,7 @@ class MergeQueue(unittest.TestCase):
with self.subTest(name): with self.subTest(name):
checkout = Checkout(GITHUB, open=True) checkout = Checkout(GITHUB, open=True)
checkout.answers.update({(self.CONFIG, "comprehension-history"): f"{history}\n", **answers}) 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(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]]) 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") ruled = ("git", "log", "-1", "--format=%H", "-G", "^Ruling: ", "origin/main..HEAD", "--", "/checkout/history.md")
checkout = Checkout(GITHUB, open=True) checkout = Checkout(GITHUB, open=True)
checkout.answers[(self.CONFIG, "comprehension-history")] = "/elsewhere/history.md\n" 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]) 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")): for failing in (ruled, ("git", "rev-list", "--count", "bead..origin/main"), ("git", "rev-parse", "--show-toplevel")):
with self.subTest(failing): with self.subTest(failing):
checkout = Checkout(GITHUB, open=True) checkout = Checkout(GITHUB, open=True)
checkout.answers.update({(self.CONFIG, "comprehension-history"): "/checkout/history.md\n", ruled: "bead\n"}) checkout.answers.update({(self.CONFIG, "comprehension-history"): "/checkout/history.md\n", ruled: "bead\n"})
checkout.answers[failing] = Failure("git failed") 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): def test_a_turn_keeps_every_other_merge_out_until_it_ends_even_by_an_error(self):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
@@ -809,7 +809,7 @@ class MergeQueue(unittest.TestCase):
def test_a_queue_it_cannot_open_leaves_it_unmerged(self): def test_a_queue_it_cannot_open_leaves_it_unmerged(self):
checkout = Checkout(GITHUB, open=True) checkout = Checkout(GITHUB, open=True)
checkout.answers[tuple(COMMON)] = "/nonexistent/folder\n" 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(), []) self.assertEqual(checkout.acted(), [])
+7 -1
View File
@@ -1,9 +1,15 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import functools
import gzip
import os
import pathlib import pathlib
import sys import sys
import time
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
from compress_archives import main 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)))
+3 -6
View File
@@ -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, 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. and at the first file it cannot compress, leaving that file and every one after it plain.
""" """
import os
import pathlib import pathlib
import stat import stat
import sys import sys
import time
import typing import typing
from transcript_archive.location import archive_root from transcript_archive.location import archive_root
from text_files.compressed import compress, SUFFIX
from failure import Failure from failure import Failure
from review_rules.review_folder import REPORTS 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`. # 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 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"]): if argv in (["-h"], ["--help"]):
print(__doc__.strip()) print(__doc__.strip())
return 0 return 0
if argv: if argv:
print(f"unknown arguments {' '.join(argv)}; usage: compress-archives", file=sys.stderr) print(f"unknown arguments {' '.join(argv)}; usage: compress-archives", file=sys.stderr)
return 1 return 1
home = home or pathlib.Path.home() cutoff = now() - AGE_DAYS * 24 * 60 * 60
cutoff = (time.time() if now is None else now) - AGE_DAYS * 24 * 60 * 60
count = 0 count = 0
for path in [*old_files(archive_root(home, env), "*.jsonl", cutoff), *old_files(home / REPORTS, "*", cutoff)]: for path in [*old_files(archive_root(home, env), "*.jsonl", cutoff), *old_files(home / REPORTS, "*", cutoff)]:
if failed := compress(path): if failed := compress(path):
@@ -11,6 +11,7 @@ import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from text_files.doubles import compress
from failure import Failure from failure import Failure
from compress_archives import main from compress_archives import main
@@ -43,10 +44,10 @@ class Main(unittest.TestCase):
path.write_bytes(f"{path.name}\n".encode()) path.write_bytes(f"{path.name}\n".encode())
os.utime(path, (NOW - seconds_ago, NOW - seconds_ago)) 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() out, err = io.StringIO(), io.StringIO()
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): 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() return code, out.getvalue(), err.getvalue()
def kept(self): def kept(self):
@@ -80,9 +81,8 @@ class Main(unittest.TestCase):
self.assertEqual(self.run_main(now=now), (0, "Compressed 0 files.\n", "")) self.assertEqual(self.run_main(now=now), (0, "Compressed 0 files.\n", ""))
self.assertTrue((self.archive / "a.jsonl").is_file()) self.assertTrue((self.archive / "a.jsonl").is_file())
def test_measures_the_30_days_from_the_clock_by_default(self): def test_measures_the_30_days_from_the_clock_it_is_given(self):
with mock.patch.object(main.time, "time", return_value=NOW): self.assertEqual(self.run_main(now=NOW), (0, "Compressed 4 files.\n", ""))
self.assertEqual(self.run_main(now=None), (0, "Compressed 4 files.\n", ""))
def test_walks_on_past_a_gzip_a_link_and_a_file_removed_while_it_walks(self): 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}) 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", "")) 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): 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(compress=lambda path: Failure("could not compress x: full"))
code, out, err = self.run_main()
self.assertEqual((code, out, err), (1, "", "could not compress x: full\n")) self.assertEqual((code, out, err), (1, "", "could not compress x: full\n"))
self.assertTrue((self.archive / "a.jsonl").is_file()) self.assertTrue((self.archive / "a.jsonl").is_file())
+5 -4
View File
@@ -6,6 +6,7 @@ import pathlib
import shutil import shutil
import sys import sys
import tempfile import tempfile
import typing
from release_plan.release_standing import chunk_item, Cut, default_plan, Next, ReadyToCut, ReviewDue, Unplanned from release_plan.release_standing import chunk_item, Cut, default_plan, Next, ReadyToCut, ReviewDue, Unplanned
from project_config.settings import DEFAULT_FLOOR from project_config.settings import DEFAULT_FLOOR
@@ -21,7 +22,7 @@ from deploy_release import release_pull
from project_config import templates 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.""" """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): if isinstance(same := same_origin(clone, checkout), Failure):
return same return same
@@ -39,7 +40,7 @@ def cut(clone: str, checkout: str) -> str | Failure:
title, tag = f"Cut release {named}", f"v{named}" title, tag = f"Cut release {named}", f"v{named}"
merged = commit_titled(title, ref, clone) merged = commit_titled(title, ref, clone)
if merged is None: if merged is None:
landed = land(clone, ref, named, title) landed = land(clone, ref, named, title, sleep)
if isinstance(landed, Failure): if isinstance(landed, Failure):
return landed return landed
if isinstance(fetched := git("fetch", "--quiet", "origin", cwd=clone), Failure): 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) 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-<named> with the cut commit and open its pull request, where an earlier run hasn't; then wait for its checks and merge it.""" """Push the branch release-<named> 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) url = git("config", "--get", "remote.origin.url", cwd=clone)
if isinstance(url, Failure): if isinstance(url, Failure):
@@ -182,7 +183,7 @@ def land(clone: str, ref: str, named: str, title: str) -> Failure | None:
if isinstance(required, Failure): if isinstance(required, Failure):
return required return required
said(f"waiting for the checks of pull request {number}" + (f"; {base} requires {', '.join(sorted(required))}" if required else "")) 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 return passed
said(f"the checks of pull request {number} passed") said(f"the checks of pull request {number} passed")
if isinstance(merged := release_pull.merge(on, number, head, run), Failure): if isinstance(merged := release_pull.merge(on, number, head, run), Failure):
+2 -1
View File
@@ -39,6 +39,7 @@ move stopped, fix what it names and run a bare deploy-release.
import os import os
import pathlib import pathlib
import sys import sys
import time
from deploy_release.cut import cut from deploy_release.cut import cut
from deploy_release.deploy import deploy from deploy_release.deploy import deploy
@@ -55,7 +56,7 @@ def main(argv: list[str]) -> int:
print("deploy-release: usage: deploy-release [--cut] [<checkout>]", file=sys.stderr) print("deploy-release: usage: deploy-release [--cut] [<checkout>]", file=sys.stderr)
return 1 return 1
checkout = rest[0] if rest else str(pathlib.Path.home() / ".claude") 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): if isinstance(moved, Failure):
print(f"deploy-release: {moved.reason}", file=sys.stderr) print(f"deploy-release: {moved.reason}", file=sys.stderr)
return 1 return 1
+1 -1
View File
@@ -10,4 +10,4 @@ from loop_effects import effects
from do_epic import main from do_epic import main
from loop_state import paths 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)))
+2 -2
View File
@@ -6,6 +6,6 @@ import sys
LAUNCHER = pathlib.Path(__file__).resolve() LAUNCHER = pathlib.Path(__file__).resolve()
sys.path.insert(0, str(LAUNCHER.parent)) 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))
+4 -2
View File
@@ -5,7 +5,7 @@ import typing
from adf_codec.convert import adfs_to_markdown, markdown_to_adf, Refused 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.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 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 do_epic.backend.protocol import EpicIssue, no_child, no_parent, no_words, unfit, unfit_label
from failure import Failure from failure import Failure
@@ -33,6 +33,8 @@ class JiraEpic:
to_markdown: ToMarkdown = adfs_to_markdown to_markdown: ToMarkdown = adfs_to_markdown
to_adf: ToAdf = markdown_to_adf to_adf: ToAdf = markdown_to_adf
finish: Finish | None = None finish: Finish | None = None
# The pages a search may take before the read fails.
max_pages: int = MAX_PAGES
@property @property
def epic_key(self) -> str: def epic_key(self) -> str:
@@ -223,7 +225,7 @@ class JiraEpic:
def searched(self, what: str, parents: tuple[str, ...], authorization: str) -> list[Found] | Failure: def searched(self, what: str, parents: tuple[str, ...], authorization: str) -> list[Found] | Failure:
"""The issues whose parent is one of parents, in rank order.""" """The issues whose parent is one of parents, in rank order."""
named = ", ".join(f'"{key}"' for key in parents) 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): if isinstance(answered, Failure):
return answered return answered
found = [found_of(raw) for raw in answered] found = [found_of(raw) for raw in answered]
+8 -9
View File
@@ -5,7 +5,6 @@ import re
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) 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": 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": {"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 ( *((({"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 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": {"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): with self.subTest(raw=raw):
self.jira.answers[("POST", SEARCH)] = {"issues": [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)) 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: 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)] children = [store.file("ABC-12", "Count the runs.", "", ()) for _ in range(2)]
for _ in range(3): for _ in range(3):
store.file(str(children[0]), "A task.", "", ()) 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"))
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.", "", ())
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 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: 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}}) self.jira.issues.append({"key": "ABC-5", "fields": {"parent": {"key": "ABC-1"}, "status": TO_DO}})
+7 -3
View File
@@ -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())} {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.""" 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: if argv:
print(HELP, file=sys.stdout if argv in (["-h"], ["--help"]) else sys.stderr) print(HELP, file=sys.stdout if argv in (["-h"], ["--help"]) else sys.stderr)
return 0 if argv in (["-h"], ["--help"]) else 1 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() report = scenario.Report()
left: sandbox.Leftover | None = None left: sandbox.Leftover | None = None
try: 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): if isinstance(key, Failure):
print(f"do-epic-e2e: {key.reason}", file=sys.stderr) print(f"do-epic-e2e: {key.reason}", file=sys.stderr)
return 1 return 1
print(f"Sandbox epic {key} in {found.jira_url}") 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: except sandbox.Leftover as failure:
left = failure left = failure
# The checks are printed even where the epic could not be deleted. # The checks are printed even where the epic could not be deleted.
+9 -6
View File
@@ -1,6 +1,5 @@
import contextlib import contextlib
import io import io
from unittest import mock
import pathlib import pathlib
import sys import sys
import tempfile import tempfile
@@ -18,7 +17,7 @@ class MainTest(unittest.TestCase):
def run_main(self, argv: list[str]) -> tuple[int, str, str]: def run_main(self, argv: list[str]) -> tuple[int, str, str]:
out, err = io.StringIO(), io.StringIO() out, err = io.StringIO(), io.StringIO()
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): 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() return code, out.getvalue(), err.getvalue()
def test_fails_naming_each_missing_setting_before_it_touches_anything(self) -> None: 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() out, err = io.StringIO(), io.StringIO()
report = report or scenario.Report(["a check held"], ["a check broke"]) 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: self.ran_with = None
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
code = main.main([], self.env, pathlib.Path("do-epic")) def ran(*args: object) -> scenario.Report:
self.ran_with = ran.call_args.args if ran.called else None 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() return code, out.getvalue(), err.getvalue()
def test_prints_each_check_and_fails_where_one_broke(self) -> None: def test_prints_each_check_and_fails_where_one_broke(self) -> None:
+26 -10
View File
@@ -251,6 +251,8 @@ def chunk_run(key: str, epic: EpicRun) -> Stop:
if isinstance(place, Stop): if isinstance(place, Stop):
return place return place
root, repo = 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) 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) 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() 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) cycle = next_cycle(run, loop, repo is not None, epic, news)
if isinstance(cycle, Stop): if isinstance(cycle, Stop):
return cycle 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) run = dataclasses.replace(run, task=cycle.task)
concluded = run_cycle(cycle) concluded = run_cycle(cycle)
if isinstance(concluded, Stop): 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): if isinstance(pick, Failure):
return Stop(1, pick.reason) return Stop(1, pick.reason)
if isinstance(pick, ClaudeWork): if isinstance(pick, ClaudeWork):
folder = epic.paths.home / scheduler.STATE / epic.key return str(epic.paths.home / scheduler.STATE / epic.key), None
folder.mkdir(parents=True, exist_ok=True)
return str(folder), None
if not isinstance(pick, Repo): if not isinstance(pick, Repo):
return Stop(1, f"{key} is no chunk: no label names its 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) 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) 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: 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 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`. # 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 return issues
def next_cycle(run: ChunkRun, loop: Loop, in_repo: bool, epic: EpicRun, news: str = "") -> Cycle | Stop: 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. 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.""" """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) premises = premises_read(loop.root if in_repo else None, epic)
if isinstance(premises, Failure): if isinstance(premises, Failure):
return Stop(1, premises.reason) 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): if isinstance(records, Failure):
return Stop(1, records.reason) return Stop(1, records.reason)
work = chunk_work(loop.window.name, loop.root if in_repo else None, language or epic.config.language, epic) 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 return work
task, issue = work task, issue = work
routing = project_routing(loop, chunk_prompt.DRIVER) if in_repo else routing_on(repoless_models(epic), SHIPPED) 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()) 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: @dataclasses.dataclass(frozen=True)
"""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.""" 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: if key != epic.key:
issue = issue_read(key, epic) issue = issue_read(key, epic)
if isinstance(issue, Stop): if isinstance(issue, Stop):
@@ -434,8 +451,7 @@ def chunk_work(key: str, checkout: str | None, language: str, epic: EpicRun) ->
if isinstance(issues, Failure): if isinstance(issues, Failure):
return Stop(1, issues.reason) return Stop(1, issues.reason)
if not finish.due(issues): 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 EpicReopened()
return handed_on(key, epic)
return finish.task(epic.key, issues, epic.launcher, language), None return finish.task(epic.key, issues, epic.launcher, language), None
+51 -49
View File
@@ -140,45 +140,44 @@ def start_chunks(epic: Epic, issues: list[EpicIssue], finishing: bool, own: str
parked = parked_chunks(epic.folder) parked = parked_chunks(epic.folder)
if isinstance(parked, Failure): if isinstance(parked, Failure):
return parked return parked
try: with starting(epic.folder) as unlocked:
with starting(epic.folder): if unlocked:
windows = tmux.listed(session, run) return unlocked
live = {window.name: window for window in windows if not window.dead and window.name != own} windows = tmux.listed(session, run)
running = {window.name for window in windows if not window.dead and not window.parked} - {own} live = {window.name: window for window in windows if not window.dead and window.name != own}
stopped = {window.name for window in windows if window.dead} - set(live) running = {window.name for window in windows if not window.dead and not window.parked} - {own}
# The windows this call started that take a running slot. stopped = {window.name for window in windows if window.dead} - set(live)
taken = 0 # The windows this call started that take a running slot.
for chunk in found: taken = 0
if chunk.issue in running or chunk.issue == own: for chunk in found:
continue if chunk.issue in running or chunk.issue == own:
slot = chunk.issue not in parked and not chunk.human continue
if chunk.human and chunk.issue in live: slot = chunk.issue not in parked and not chunk.human
waits.append(Waits(chunk.issue, HUMAN_WAIT)) if chunk.human and chunk.issue in live:
elif chunk.issue in parked and chunk.issue in live: waits.append(Waits(chunk.issue, HUMAN_WAIT))
waits.append(Waits(chunk.issue, parked[chunk.issue].waits(live[chunk.issue].awaits))) elif chunk.issue in parked and chunk.issue in live:
elif own and (chunk.issue in stopped or chunk.issue in parked): waits.append(Waits(chunk.issue, parked[chunk.issue].waits(live[chunk.issue].awaits)))
waits.append(Waits(chunk.issue, f"its window stopped; run `do-epic {epic.key}` to start it again")) elif own and (chunk.issue in stopped or chunk.issue in parked):
elif slot and len(running) + taken >= epic.cap: waits.append(Waits(chunk.issue, f"its window stopped; run `do-epic {epic.key}` to start it again"))
waits.append(Waits(chunk.issue, f"the epic runs as many chunks as its `{CAP}` line allows: {epic.cap}")) 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: else:
now = start_chunk(epic, chunk if chunk.issue not in parked else Chunk(chunk.issue, None), own, windows, run) windows = now
if isinstance(now, Failure): started.append(chunk.issue)
return now taken += slot
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}")
# The epic's finishing is no issue: its wait is listed after every issue's. # The epic's finishing is no issue: its wait is listed after every issue's.
order = [*(issue.key for issue in issues), epic.key] order = [*(issue.key for issue in issues), epic.key]
return Started(started, sorted(waits, key=lambda wait: order.index(wait.issue))) 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: 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) session = tmux.session_name(tmux.EPIC, epic.key)
handed = {**{name: value for name, value in epic.env.items() if name.startswith(HANDED)}, PARKS: "1"} 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] 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): if isinstance(located, Failure):
return located.reason return located.reason
checkout, limit = located 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)): if reason := refusal(chunk.repo, room_in(str(checkout), limit, (session, own), epic.home, run)):
return reason return reason
return tmux.start_chunk(tmux.Window(session, chunk.issue, ""), str(checkout), handed, command, windows, False, run) 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: 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.""" """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) as unlocked:
with starting(epic.folder): if unlocked:
running = [listed for listed in tmux.listed(window.session, run) if not listed.dead and not listed.parked and listed.name != window.name] return unlocked.reason
if len(running) >= epic.cap: running = [listed for listed in tmux.listed(window.session, run) if not listed.dead and not listed.parked and listed.name != window.name]
return f"the epic runs as many chunks as its `{CAP}` line allows: {epic.cap}" if len(running) >= epic.cap:
located = epic.locate(repo) return f"the epic runs as many chunks as its `{CAP}` line allows: {epic.cap}"
if isinstance(located, Failure): located = epic.locate(repo)
return located.reason if isinstance(located, Failure):
checkout, limit = located return located.reason
with starting(project_lock(str(checkout), epic.home)): checkout, limit = located
if reason := refusal(repo, room_in(str(checkout), limit, (window.session, window.name), epic.home, run)): with starting(project_lock(str(checkout), epic.home)) as unlocked:
return reason if unlocked:
tmux.unpark(window, str(checkout), run) return unlocked.reason
return None if reason := refusal(repo, room_in(str(checkout), limit, (window.session, window.name), epic.home, run)):
except OSError as failure: return reason
return f"cannot lock {failure.filename or epic.folder / LOCK}: {failure.strerror}" tmux.unpark(window, str(checkout), run)
return None
+8
View File
@@ -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 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.assertIn("the chunk finished; started ABC-31\n", out)
self.assertEqual([seam for seam, _ in fake.calls if seam == "stream_claude"], []) 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), ("", []))
+15
View File
@@ -179,6 +179,20 @@ class TheCommand(DoEpicTest):
class ACycleOfAChunkWithoutARepo(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: def cycle(self, env: dict[str, str] | None = None) -> main.Cycle:
fake = FakeEffects(git=[Failure("no git"), Failure("no git")]) fake = FakeEffects(git=[Failure("no git"), Failure("no git")])
paths = paths_from(self.home) paths = paths_from(self.home)
@@ -186,6 +200,7 @@ class ACycleOfAChunkWithoutARepo(DoEpicTest):
place = main.chunk_root("ABC-21", self.issues[1], epic) place = main.chunk_root("ABC-21", self.issues[1], epic)
assert isinstance(place, tuple) assert isinstance(place, tuple)
root, repo = place 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()) 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()): with contextlib.redirect_stdout(io.StringIO()):
found = main.next_cycle(ChunkRun(1), loop, repo is not None, epic) found = main.next_cycle(ChunkRun(1), loop, repo is not None, epic)
+11 -6
View File
@@ -2,10 +2,10 @@ import pathlib
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 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 do_epic.config import DoEpicConfig
from failure import Failure from failure import Failure
from do_epic.repos import locate, Located, Unsure from do_epic.repos import locate, Located, Unsure
@@ -14,9 +14,9 @@ from project_config.doubles import write_config
class FakeGit: 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.""" """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) -> None: def __init__(self, origins: dict[str, str | Failure], refused: str | None = None, locked: bool = False) -> None:
self.origins, self.refused = origins, refused self.origins, self.refused, self.locked = origins, refused, locked
self.cloned: list[tuple[str, str]] = [] self.cloned: list[tuple[str, str]] = []
self.clone_timeouts: list[float | None] = [] self.clone_timeouts: list[float | None] = []
self.clone_options: list[tuple[str, ...]] = [] self.clone_options: list[tuple[str, ...]] = []
@@ -30,6 +30,8 @@ class FakeGit:
url, destination = args[clone + 2:clone + 4] url, destination = args[clone + 2:clone + 4]
pathlib.Path(destination, ".git").mkdir(parents=True) pathlib.Path(destination, ".git").mkdir(parents=True)
if url == self.refused: if url == self.refused:
if self.locked:
pathlib.Path(destination).chmod(0o500)
return Failure("git clone: repository not found") return Failure("git clone: repository not found")
self.cloned.append((url, destination)) self.cloned.append((url, destination))
self.clone_timeouts.append(timeout) 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))) 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: def test_asks_all_the_same_where_the_leftovers_of_a_failed_clone_cannot_be_removed(self) -> None:
assert_not_root(self)
self.configure() 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")): left = self.home / "code" / "ledger"
self.assertIn("cannot clone", self.unsure(FakeGit(self.origins, refused="git@github.com:acme/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: def test_asks_where_the_clone_fails_naming_the_url_and_the_reason(self) -> None:
self.configure() self.configure()
+16
View File
@@ -315,6 +315,22 @@ class StartingAnEpicsChunks(unittest.TestCase):
assert isinstance(found, Failure) assert isinstance(found, Failure)
self.assertRegex(found.reason, f"^cannot lock {self.home}/.local/state/do-epic/ABC-12: \\w") 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")) 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"))
+1 -2
View File
@@ -2,7 +2,6 @@
import dataclasses import dataclasses
import fnmatch import fnmatch
import json import json
import time
import typing import typing
from failure import Failure 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) 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.""" """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": if forge == "gh":
for waited in range(0, settle + 1, POLL_SECONDS): for waited in range(0, settle + 1, POLL_SECONDS):
+79 -21
View File
@@ -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.""" """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 json
import typing
from failure import Failure from failure import Failure
from forge.commands import Forge, gitea, Run 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"]) viewed = run(["gh", "pr", "view", pull, "--json", "isDraft,reviewDecision,author,reviews,comments"])
if isinstance(viewed, Failure): if isinstance(viewed, Failure):
return viewed 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: try:
parsed = json.loads(viewed) pull = json.loads(printed)
if parsed["isDraft"]: author = text(pull["author"]["login"])
return None 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"])
decision = str(parsed["reviewDecision"] or "") commented = tuple(text(comment["createdAt"]) for comment in pull["comments"] if text(comment["author"]["login"]) == author)
return ANSWERED if decision == CHANGES_REQUESTED and answered(parsed, head) else decision 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): 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: def answered(pull: PullView, 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. """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. 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: if asked is None:
return False return False
author = pull["author"]["login"] return asked.commit != head or any(at > asked.submitted_at for at in pull.said)
# 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)
def gitea_decision(pull: str, run: Run) -> str | None | Failure: 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}") viewed = gitea(run, f"repos/{{owner}}/{{repo}}/pulls/{pull}")
if isinstance(viewed, Failure): if isinstance(viewed, Failure):
return viewed return viewed
try: title = viewed.get("title") if isinstance(viewed, dict) else None
return None if viewed["title"].upper().startswith(GITEA_DRAFT) else "" if not isinstance(title, str):
except (KeyError, TypeError, AttributeError):
return Failure(f"tea api printed no title of pull request {pull}: {str(viewed)[:200]}") 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: 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}"]) read = run(["gh", "api", "graphql", "-f", f"query={THREADS}", "-F", "owner={owner}", "-F", "repo={repo}", "-F", f"number={number}"])
if isinstance(read, Failure): if isinstance(read, Failure):
return read 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: try:
pull = json.loads(read)["data"]["repository"]["pullRequest"] pull = json.loads(printed)["data"]["repository"]["pullRequest"]
author = pull["author"]["login"] 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 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"])}) return text(pull["author"]["login"]), threads
except (ValueError, KeyError, TypeError): 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)})
+102
View File
@@ -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()
+6 -5
View File
@@ -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. """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. 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. 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. 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) command = " ".join(args)
guards = [option for setting in GUARD_SETTINGS for option in ("-c", setting)] if guarded else [] guards = [option for setting in GUARD_SETTINGS for option in ("-c", setting)] if guarded else []
# GIT_NO_LAZY_FETCH needs git 2.44. # 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: 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: except UnicodeDecodeError as failure:
return Failure(f"git {command} printed text that is not UTF-8: {failure}") return Failure(f"git {command} printed text that is not UTF-8: {failure}")
except (OSError, ValueError) as failure: except (OSError, ValueError) as failure:
@@ -52,8 +53,8 @@ class Git(typing.Protocol):
def top() -> str | Failure: def top(run: Git = git) -> str | Failure:
found = git("rev-parse", "--show-toplevel") found = run("rev-parse", "--show-toplevel")
return found if isinstance(found, Failure) else found.strip() return found if isinstance(found, Failure) else found.strip()
+1 -1
View File
@@ -8,4 +8,4 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
from judge_replay import main from judge_replay import main
from loop_effects.effects import system_effects 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)))
+5 -5
View File
@@ -56,15 +56,15 @@ import typing
from chunk_driver.prompts import ANSWER_DESTINATIONS from chunk_driver.prompts import ANSWER_DESTINATIONS
from transcript_archive.location import archive_root, transcript_of from transcript_archive.location import archive_root, transcript_of
from transcript_archive.read import archived_outcomes from transcript_archive.read import archived_outcomes
from rung_replay.archive import archiving, print_report, replay_folder from rung_replay.archive import archiving, replay_folder
from rung_replay.command import arguments_or_exit, down_from, failed, limited_exit, projects_on 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 agent_cycle.judge import Brief, judge_archived, judge_once
from judge_replay.replay import Case, case_from, Kind, report, reproduced, sample, verdict_kind from judge_replay.replay import Case, case_from, Kind, report, reproduced, sample, verdict_kind
from transcript_archive.project_key import checkout_of from transcript_archive.project_key import checkout_of
from loop_effects.effects import Effects from loop_effects.effects import Effects
from failure import Failure from failure import Failure
from judge_replay.offline import final_message, LEARNERS, offline_report, RULES, Sample 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 judge_replay.archived_prompt import grounds_of
from transcript_archive.judge_run import JudgeRun from transcript_archive.judge_run import JudgeRun
from model_routing.ladder import Ladder, ProjectRung, REVIEW_STEPS, Rung 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) return offline(project.rung.model, cases, archive_root(home, env), effects)
cases = sample(cases, PER_KIND) cases = sample(cases, PER_KIND)
folder = replay_folder(archive_root(home, env), TOOL, argv, effects.now()) 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) 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. # 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): if isinstance(replay, Limited):
return limited_exit(TOOL, replay) return limited_exit(TOOL, replay)
print_report(folder, report(project.rung.model, reference, cases, replay)) print_report(folder, report(project.rung.model, reference, cases, replay))
+4 -4
View File
@@ -13,7 +13,7 @@ from chunk_driver.prompts import ANSWER_DESTINATIONS
from transcript_archive.outcomes import ANSWERED, FINISHED, INTERRUPTED, UNFINISHED from transcript_archive.outcomes import ANSWERED, FINISHED, INTERRUPTED, UNFINISHED
from claude_code.entries import AssistantTurn, HumanTurn, LoopPrompt, Text from claude_code.entries import AssistantTurn, HumanTurn, LoopPrompt, Text
from judge_replay.replay import Case, case_from, report, reproduced, sample, verdict_kind 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 project_config.doubles import configured
from loop_effects.effects import Effects from loop_effects.effects import Effects
from failure import Failure 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]) 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), "") 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() out, err = io.StringIO(), io.StringIO()
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
code = main.main(argv, {}, self.home, effects) 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) self.assertIn("replayed failed: could not read the transcript", out)
def test_replays_a_verdict_whose_transcript_is_compressed(self): 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}")) 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.assertEqual(code, 0, err)
self.assertNotIn("replayed failed", out) self.assertNotIn("replayed failed", out)
@@ -343,7 +343,7 @@ class JudgeArchivedRuns(unittest.TestCase):
cwds.append(cwd) cwds.append(cwd)
return subprocess.CompletedProcess(args, 0, json.dumps({"type": "result", "structured_output": output, "total_cost_usd": 0.25}), "") 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) 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 return (grounds if isinstance(grounds, Failure) else judge_archived("/code/p", prompt, grounds, len(self.ENTRIES), REFERENCE, LOOP_CLAUDE, effects)), cwds
+13 -14
View File
@@ -9,7 +9,6 @@ import sys
import tempfile import tempfile
import textwrap import textwrap
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 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.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()) self.addCleanup(os.chdir, os.getcwd())
os.chdir(self.checkout) os.chdir(self.checkout)
with unittest.mock.patch.dict(os.environ, {"HOME": str(self.config.parents[2])}): home = 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.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.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.assertEqual(plan_reasons("origin/main", [2], home), [])
(self.config.parent / "decisions.md").unlink() (self.config.parent / "decisions.md").unlink()
gone = plan_reasons("origin/main", [1, 7], pathlib.Path.home()) gone = plan_reasons("origin/main", [1, 7], home)
assert isinstance(gone, Failure), gone 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.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") (self.config.parent / "todo.md").write_bytes(b"\xff")
unreadable = plan_reasons("origin/main", [2], pathlib.Path.home()) unreadable = plan_reasons("origin/main", [2], home)
assert isinstance(unreadable, Failure), unreadable assert isinstance(unreadable, Failure), unreadable
self.assertEqual(unreadable.reason, f"the project's plan: {self.config.parent}/todo.md: not UTF-8") 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 |" 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") (checkout / "todo.md").write_text("the owners' own notes\n")
self.enterContext(contextlib.chdir(checkout)) self.enterContext(contextlib.chdir(checkout))
said: list[str] = [] 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")) self.assertEqual((said, (checkout / "todo.md").read_text()), ([], "the owners' own notes\n"))
def test_reranks_todo_md_by_the_overall_it_records(self): def test_reranks_todo_md_by_the_overall_it_records(self):
+2 -5
View File
@@ -1,4 +1,3 @@
from unittest import mock
import os import os
import pathlib import pathlib
import re import re
@@ -103,8 +102,7 @@ class PrepareScoring(unittest.TestCase):
def prepare(self): def prepare(self):
"""What prepare-scoring returns in the checkout, run in this process so mutation tests see the lines it runs.""" """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), self.root)
return prepare_scoring(str(self.checkout), pathlib.Path.home())
def run_tool(self, *args): def run_tool(self, *args):
return subprocess.run([sys.executable, str(TOOL), *args], capture_output=True, cwd=self.checkout, env=self.env, text=True) 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() self.commit()
checkout = self.root / ".local" / "state" / "larv-review" / "proj" / "scoring" / "checkout" checkout = self.root / ".local" / "state" / "larv-review" / "proj" / "scoring" / "checkout"
checkout.mkdir(parents=True) checkout.mkdir(parents=True)
with mock.patch.dict(os.environ, self.env, clear=True): removed = removed_setup(str(self.checkout), self.root)
removed = removed_setup(str(self.checkout), pathlib.Path.home())
self.assertIsInstance(removed, Failure) self.assertIsInstance(removed, Failure)
self.assertTrue(removed.reason.startswith(f"could not remove the scoring worktree {checkout}: "), removed.reason) self.assertTrue(removed.reason.startswith(f"could not remove the scoring worktree {checkout}: "), removed.reason)
self.assertTrue(checkout.exists()) self.assertTrue(checkout.exists())
+1 -1
View File
@@ -10,4 +10,4 @@ from loop_effects import effects
from loop_claude.loop import main from loop_claude.loop import main
from loop_state import paths 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)))
+14 -17
View File
@@ -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`).""" """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 dataclasses
import pathlib import pathlib
import re import re
@@ -116,21 +115,19 @@ def start_chunks(loop_run: LoopRun, leaving: bool, paths: Paths, env: typing.Map
return config return config
session = loop_session(loop_run.root, paths.home) session = loop_session(loop_run.root, paths.home)
handed = {name: value for name, value in env.items() if name.startswith(HANDED)} handed = {name: value for name, value in env.items() if name.startswith(HANDED)}
lock = project_lock(loop_run.root, paths.home) with starting(project_lock(loop_run.root, paths.home)) as unlocked:
try: if unlocked:
with starting(lock): return unlocked
windows = tmux.listed(session, effects.tmux) windows = tmux.listed(session, effects.tmux)
if refused := refusal(session, windows, loop_run.root, effects.tmux): if refused := refusal(session, windows, loop_run.root, effects.tmux):
return refused return refused
chunks = chunks_to_start(windows, loop_run, leaving, config.parallel_chunks, paths, effects) chunks = chunks_to_start(windows, loop_run, leaving, config.parallel_chunks, paths, effects)
for chunk in chunks: 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) 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): if isinstance(started, Failure):
return started return started
windows = started windows = started
return chunks return chunks
except OSError as failure:
return Failure(f"cannot lock {lock / LOCK}: {failure.strerror}")
def refusal(session: str, windows: list[tmux.Listed], root: str, run: tmux.Tmux) -> Failure | None: 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: 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.""" """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 pass
+40 -17
View File
@@ -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_windows import chunk_session, item_of, start_chunks
from loop_claude.loop.chunk_start import chunk_start, plan_item, scored_plan from loop_claude.loop.chunk_start import chunk_start, plan_item, scored_plan
from chunk_driver.chunk_run import closed_cycle 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_claude.loop.reload import decode_handed, exec_if_edited, fingerprint, LoopRun, RELOAD_FLAG
from loop_effects.effects import Effects, say from loop_effects.effects import Effects, say
from failure import Failure from failure import Failure
@@ -20,6 +20,7 @@ from loop_claude.loop.usage import HELP
from chunk_driver.this_loop import Loop from chunk_driver.this_loop import Loop
from loop_state.paths import Paths from loop_state.paths import Paths
from transcript_archive.project_key import project_key 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.premises import read_premises
from chunk_driver.project.records import read_records from chunk_driver.project.records import read_records
from model_routing.ladder_file import SHIPPED from model_routing.ladder_file import SHIPPED
@@ -28,9 +29,31 @@ from agent_cycle.direct_message import tell
from agent_cycle import tmux 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: 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: try:
stop = run_loop(argv, env, paths, launcher, cwd, effects) stop = run()
except KeyboardInterrupt: except KeyboardInterrupt:
stop = Stop(130, "") stop = Stop(130, "")
except Exception as crash: except Exception as crash:
@@ -44,7 +67,7 @@ def main(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launcher:
return stop.code 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) loop_run = loop_run_from(argv, cwd, effects)
if isinstance(loop_run, Stop): if isinstance(loop_run, Stop):
return loop_run 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): if reason := tmux.unusable(effects.tmux, fresh_prompt.DRIVER):
return Stop(1, reason) return Stop(1, reason)
if loop_run.chunk is None: 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") pane = env.get("TMUX_PANE")
if not pane: if not pane:
return Stop(1, f"{CHUNK_FLAG} is how loop-claude starts itself in a tmux window; run loop-claude <task>") return Stop(1, f"{CHUNK_FLAG} is how loop-claude starts itself in a tmux window; run loop-claude <task>")
baseline = fingerprint(launcher) baseline = steps.fingerprint(launcher)
key = project_key(loop_run.root, paths.home) key = project_key(loop_run.root, paths.home)
window = tmux.Window(loop_session(loop_run.root, paths.home), loop_run.chunk, pane) 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) 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: while True:
if stop := exec_if_edited(loop, baseline, loop_run): if stop := steps.exec_if_edited(loop, baseline, loop_run):
return stop return stop
loop_run = dataclasses.replace(loop_run, cycle_number=loop_run.cycle_number + 1) 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): if isinstance(cycle, Stop):
return cycle return cycle
concluded = run_cycle(cycle) concluded = steps.run_cycle(cycle)
if isinstance(concluded, Stop): if isinstance(concluded, Stop):
return concluded 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): if isinstance(closed, Stop):
return closed return closed
loop_run = closed loop_run = closed
# Off the ladder the worker runs on the project's model itself, which its route names. # 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 return stop
if isinstance(concluded.verdict, Finished): 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: 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 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.""" """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_chunks(loop_run, False, paths, env, launcher, effects) started = start(loop_run, False, paths, env, launcher, effects)
if isinstance(started, Failure): if isinstance(started, Failure):
return Stop(1, started.reason) return Stop(1, started.reason)
session = loop_session(loop_run.root, paths.home) 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, "") 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: def handed_on(loop_run: LoopRun, loop: Loop, start: StartChunks) -> 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.""" """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_chunks(loop_run, True, loop.paths, loop.env, loop.launcher, loop.effects) started = start(loop_run, True, loop.paths, loop.env, loop.launcher, loop.effects)
if isinstance(started, Failure): if isinstance(started, Failure):
return Stop(1, f"the chunk finished, but the next could not start: {started.reason}") 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) say(f"the chunk finished; started {', '.join(started) or 'no other, since the chunks running fill what may run now'}", loop.effects, stdout=True)
+1 -1
View File
@@ -67,6 +67,6 @@ def raise_calls(root: pathlib.Path, new: list[Call], rung: Rung, loop: Loop) ->
if unstarted: 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}") 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. # 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) say(f"routing figures: {unmarked.reason}", loop.effects)
return None return None
+16 -5
View File
@@ -5,7 +5,6 @@ import pathlib
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) 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. # An item that left the plan, as once it shipped.
self.assertIsNone(chunk_start.plan_item(plan, 999, FakeEffects().effects())) 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. # 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: with tempfile.TemporaryDirectory() as root:
loop = a_loop(FakeEffects().effects()) folder = configured(self, pathlib.Path(root))
self.assertEqual(chunk_start.chunk_start(loop), ("", plan)) 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. # 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") 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")): 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): with self.subTest(said=said):
+49 -14
View File
@@ -1,11 +1,12 @@
import contextlib import contextlib
import dataclasses
import io import io
import json import json
import pathlib import pathlib
import subprocess
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) 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 loop_claude.loop import chunk_windows
from project_config.doubles import configured from project_config.doubles import configured
from failure import Failure from failure import Failure
from git_cli import git_repo
from test_env import isolated_env
from loop_claude.loop import main from loop_claude.loop import main
from release_plan import next_chunk from release_plan import next_chunk
from loop_state import paths from loop_state import paths
from agent_cycle import tmux 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): class ChunkWindows(unittest.TestCase):
@@ -76,8 +84,8 @@ class ChunkWindows(unittest.TestCase):
self.assertIsNone(chunk_windows.item_of("item-\u0663")) self.assertIsNone(chunk_windows.item_of("item-\u0663"))
def test_starts_only_the_next_item_where_the_touch_predictions_cannot_be_read(self): 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"): items = touch_plan((1, "1.0.0", "", "- a.py"), (2, "1.0.0", "", "- `c.py`"))
self.assertEqual(chunk_windows.may_start(self.plan("a.py", "c.py"), [], set(), 3), ["item-1"]) 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): def test_a_chunk_whose_transcript_cannot_be_read_holds_the_item_it_started_from(self):
with tempfile.TemporaryDirectory() as temp: with tempfile.TemporaryDirectory() as temp:
@@ -113,7 +121,7 @@ class StartChunks(unittest.TestCase):
self.home = configured(self, self.root).parents[1] self.home = configured(self, self.root).parents[1]
self.session = "loop-" + str(self.root).lstrip("/").replace("/", "-").replace(".", "-") self.session = "loop-" + str(self.root).lstrip("/").replace("/", "-").replace(".", "-")
# Items 1 and 2 overlap, so 2 waits; 3 runs beside 1. # 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): def limit(self, chunks):
config = self.home / ".claude-projects" / self.root.name / "config.toml" 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.""" """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 fake.owner = str(self.root) if owner is None else owner
run = main.LoopRun(0, str(self.root), "ship it", "", chunk=chunk) 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: ran = []
effects = fake.effects() 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) started = chunk_windows.start_chunks(run, leaving, paths.paths_from(self.home), env or {}, self.LAUNCHER, effects)
self.said = said.getvalue() self.said = said.getvalue()
self.planned = planned.call_count fetches = [args for args in ran if args[0] == "fetch"]
if planned.call_count: self.planned = len(fetches)
planned.assert_called_once_with(str(self.root), self.home, effects) self.assertIn(fetches, ([], [["fetch", "--quiet", "origin"]]))
return started 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): def starts(self, fake):
"""The window starts of fake's tmux calls, each as (command, window name, what it runs).""" """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")] 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): def test_a_plan_with_no_item_starts_nothing_beside_a_restarted_chunk(self):
self.limit(2) self.limit(2)
fake = FakeEffects(windows="item-5\t1\t@3\n") 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): def test_a_finishing_chunk_leaves_a_stopped_chunk_and_its_items_to_the_human(self):
self.limit(2) 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(self.start(fake, chunk="item-7", leaving=True), ["item-3"])
self.assertEqual([call for call in fake.tmux_calls if call[0] == "kill-window"], []) 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") 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): def test_starts_the_chunk_without_a_plan_item_where_the_plan_cannot_be_read(self):
fake = FakeEffects() 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) 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): 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")): fake = FakeEffects()
self.assertEqual(self.start(FakeEffects()), Failure("tmux could not start the window")) 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): def test_says_why_it_cannot_take_the_lock_on_starting_windows(self):
(self.home / ".local" / "state").mkdir(parents=True) (self.home / ".local" / "state").mkdir(parents=True)
@@ -236,6 +261,16 @@ class StartChunks(unittest.TestCase):
self.assertIsInstance(started, Failure) self.assertIsInstance(started, Failure)
self.assertTrue(started.reason.startswith(f"cannot lock {self.home}/.local/state/loop-claude/"), started.reason) 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): def test_a_chunk_holds_its_own_item_and_those_on_its_latest_chunk_items_line(self):
with tempfile.TemporaryDirectory() as temp: with tempfile.TemporaryDirectory() as temp:
transcripts = pathlib.Path(temp) transcripts = pathlib.Path(temp)
+46 -26
View File
@@ -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 <task>")) 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 <task>"))
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): 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.""" """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") HOME = pathlib.Path("/h")
def run_loop(self, argv, fake, env=None, started=("item-3", "item-4")): 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.""" """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: self.asked = []
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] 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() return stop, said.getvalue()
def test_refuses_to_start_anything_on_a_tmux_older_than_needed(self): 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): def test_hands_the_window_starts_its_paths_environment_launcher_and_effects(self):
fake = FakeEffects(git=["/r\n"], on_terminal=False) fake = FakeEffects(git=["/r\n"], on_terminal=False)
effects, env, launcher = fake.effects(), {"LOOP_CLAUDE_X": "1"}, pathlib.Path("/b/loop-claude") 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()): asked = []
main.run_loop(["do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects) with contextlib.redirect_stdout(io.StringIO()):
self.assertEqual(start.call_args.args, (main.LoopRun(0, "/r", "do it", ""), False, paths.paths_from(self.HOME), env, launcher, effects)) 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): 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"]) fake = FakeEffects(git=["/r\n"])
@@ -153,14 +167,15 @@ class WindowStarts(unittest.TestCase):
unfinished = unittest.mock.Mock(verdict=unittest.mock.Mock(spec=Unfinished)) unfinished = unittest.mock.Mock(verdict=unittest.mock.Mock(spec=Unfinished))
finished = unittest.mock.Mock(verdict=unittest.mock.Mock(spec=Finished)) 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")] 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, \ concluded, cycles, started = [unfinished, finished], [], []
unittest.mock.patch.object(main, "run_cycle", side_effect=[unfinished, finished]), unittest.mock.patch.object(main, "closed_cycle", side_effect=closed), \ 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,
unittest.mock.patch.object(main, "check_routing", return_value=None), unittest.mock.patch.object(main, "handed_on", return_value=Stop(0, "")) as handed_on: run_cycle=lambda cycle: concluded.pop(0), closed_cycle=lambda *args: closed.pop(0), check_routing=lambda *args: None)
stop = main.run_loop(["--chunk", "item-3", "do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects) 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) 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(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)]) self.assertEqual(cycles, [(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(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): 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)) 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): def test_stops_where_tmux_cannot_show_the_session(self):
fake = FakeEffects(git=["/r\n"]) fake = FakeEffects(git=["/r\n"])
effects = dataclasses.replace(fake.effects(), tmux_on_terminal=lambda args: 1) 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()): with contextlib.redirect_stdout(io.StringIO()):
stop = main.run_loop(["do it"], {}, paths.paths_from(self.HOME), pathlib.Path("/b/loop-claude"), CWD, effects) 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")) 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): 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") 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, "")), 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"))): (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: with self.subTest(started=started), contextlib.redirect_stdout(io.StringIO()) as printed:
loop = a_loop(FakeEffects().effects(), env={"LOOP_CLAUDE_X": "1"}) loop, asked = a_loop(FakeEffects().effects(), env={"LOOP_CLAUDE_X": "1"}), []
self.assertEqual(main.handed_on(loop_run, loop), stop) 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(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): 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()) code = main.main(argv, {}, paths.paths_from(pathlib.Path(home)), pathlib.Path(home, "loop-claude"), CWD, fake.effects())
return code, said.getvalue() 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): def test_dms_the_reason_of_a_stop_with_exit_2_such_as_a_chunk_left_unfinished_too_often(self):
fake = FakeEffects() 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.exit_code(lambda: Stop(2, "the human is needed to look at the chunk, but it stayed unfinished"), fake)[0], 2)
self.assertEqual(self.run_main(["do it"], 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"]) 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): def test_skips_the_dm_of_a_stop_without_a_reason_as_once_the_windows_started(self):
fake = FakeEffects() fake = FakeEffects()
with unittest.mock.patch.object(main, "run_loop", return_value=Stop(0, "")): self.assertEqual(self.exit_code(lambda: Stop(0, ""), fake), (0, ""))
self.assertEqual(self.run_main(["do it"], fake), (0, ""))
self.assertEqual(fake.dms, []) self.assertEqual(fake.dms, [])
def test_skips_the_dm_where_ctrl_c_stopped_the_loop(self): def test_skips_the_dm_where_ctrl_c_stopped_the_loop(self):
fake = FakeEffects() fake = FakeEffects()
with unittest.mock.patch.object(main, "run_loop", side_effect=KeyboardInterrupt): self.assertEqual(self.exit_code(lambda: self.raising(KeyboardInterrupt()), fake)[0], 130)
self.assertEqual(self.run_main(["do it"], fake)[0], 130)
self.assertEqual(fake.dms, []) self.assertEqual(fake.dms, [])
def test_dms_a_crash_before_it_reaches_the_terminal(self): def test_dms_a_crash_before_it_reaches_the_terminal(self):
fake = FakeEffects() fake = FakeEffects()
with unittest.mock.patch.object(main, "run_loop", side_effect=RuntimeError("boom")), self.assertRaises(RuntimeError): with self.assertRaises(RuntimeError):
self.run_main(["do it"], fake) self.exit_code(lambda: self.raising(RuntimeError("boom")), fake)
self.assertEqual(fake.dms, [f"loop-claude in {CWD} crashed: RuntimeError('boom')"]) 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): def test_dms_the_reason_where_a_loop_given_a_task_stops_with_exit_1(self):
+4 -3
View File
@@ -6,7 +6,6 @@ import shutil
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) 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): def test_puts_every_new_call_to_the_human_and_says_one_it_cannot_keep(self):
new = [Call("reports", ("Explore",)), Call("rounds", ("x", "y"))] new = [Call("reports", ("Explore",)), Call("rounds", ("x", "y"))]
fake = FakeEffects(on_terminal_claude=[0]) 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))) self.assertIsNone(routing_check.raise_calls(self.root, new, Rung("m", "high"), self.loop(fake)))
((args),) = fake.made("claude_on_terminal") ((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"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(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 [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) 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)
+1 -2
View File
@@ -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", "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/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", "models.reason", models_), ("chunk_driver/project/model_choice.py", "get_or_ask_model", "1", "failure.reason", models_),
("chunk_driver/project/model_choice.py", "get_or_ask_model", "1", "f'could not save the model in", models_),
("loop_claude/loop/checkout.py", "checkout_root", "1", "found.reason", git), ("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", "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"), ("loop_claude/loop/main.py", "run_loop", "1", "f'{CHUNK_FLAG} is how loop-claude starts itself", "--chunk is given outside a tmux window"),
+4 -5
View File
@@ -7,8 +7,8 @@ import os
import pathlib import pathlib
import signal import signal
import stat import stat
import typing
from machine_load.slots import slot_folder
# The command names of a shell claude starts for a Bash call. # The command names of a shell claude starts for a Bash call.
SHELL_NAMES = frozenset({"bash", "dash", "sh", "zsh"}) 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)) return any(has_slot(each, slots) for each in descendants(pid))
def shells_of(claude_pid: int) -> list[Shell]: 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; none where /proc doesn't exist, as off Linux.""" """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"): if not os.path.isdir("/proc"):
return [] return []
slots = slot_folder(os.environ, pathlib.Path.home())
booted = boot_time() booted = boot_time()
ticks = os.sysconf("SC_CLK_TCK") ticks = os.sysconf("SC_CLK_TCK")
shells = [] shells = []
for pid in children_of(claude_pid): for pid in children(claude_pid):
found = parent_and_start(pid) found = parent_and_start(pid)
if found is None or found[2] not in SHELL_NAMES: if found is None or found[2] not in SHELL_NAMES:
continue continue
+4
View File
@@ -9,6 +9,7 @@ from loop_effects.effects import Effects
from claude_code import entries from claude_code import entries
from failure import Failure from failure import Failure
from machine_load import memory from machine_load import memory
from text_files.file_writes import replace_file
from loop_effects.child_shells import Shell from loop_effects.child_shells import Shell
@@ -51,6 +52,8 @@ class FakeEffects:
self.terminal_folders: list[str] = [] self.terminal_folders: list[str] = []
# Each timer started, as its delay and action, which a test runs where the timer fires. # Each timer started, as its delay and action, which a test runs where the timer fires.
self.timers: list[tuple[float, typing.Callable[[], None]]] = [] 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: def effects(self) -> Effects:
return Effects( return Effects(
@@ -66,6 +69,7 @@ class FakeEffects:
now=lambda: NOW, now=lambda: NOW,
on_terminal=lambda: self.terminal, on_terminal=lambda: self.terminal,
python=lambda args, cwd: self.answer("python", (args, cwd)), 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)), sleep=lambda seconds: self.calls.append(("sleep", seconds)),
stream_claude=self.stream, stream_claude=self.stream,
timer=self.timer, timer=self.timer,
+17 -9
View File
@@ -20,6 +20,8 @@ from failure import Failure
from git_cli import git_repo from git_cli import git_repo
from machine_load.memory import Memory, read_memory from machine_load.memory import Memory, read_memory
from project_config.settings import Notifier 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. # 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 PYTHON_TIMEOUT = 600
@@ -62,6 +64,8 @@ class Effects:
on_terminal: typing.Callable[[], bool] on_terminal: typing.Callable[[], bool]
# Runs a Python script in a directory, as run_python does. # Runs a Python script in a directory, as run_python does.
python: typing.Callable[[list[str], str], subprocess.CompletedProcess[str]] 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] sleep: typing.Callable[[float], None]
stream_claude: typing.Callable[[list[str], str, dict[str, str]], Streaming] stream_claude: typing.Callable[[list[str], str, dict[str, str]], Streaming]
# Starts a timer, as start_timer does. # Starts a timer, as start_timer does.
@@ -96,7 +100,7 @@ class StreamingClaude:
yield pending.decode("utf-8", errors="replace") yield pending.decode("utf-8", errors="replace")
def shells(self) -> list[child_shells.Shell]: 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: def stop(self, shell: child_shells.Shell) -> None:
child_shells.stop(shell) child_shells.stop(shell)
@@ -105,13 +109,16 @@ class StreamingClaude:
return self.proc.wait() return self.proc.wait()
def system_effects(backends: typing.Mapping[Notifier, Backend[typing.Any]] = BACKENDS) -> Effects: 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; `backends` are the notifiers loop-claude's DM can go through, where a test swaps in one that sends nowhere.""" """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( return Effects(
claude=lambda args, cwd, stdin: subprocess.run(["claude", *args], capture_output=True, cwd=cwd, input=stdin, text=True), claude=lambda args, cwd, stdin: subprocess.run(["claude", *args], capture_output=True, cwd=cwd, input=stdin, text=True),
claude_on_terminal=claude_on_terminal, claude_on_terminal=claude_on_terminal,
discard_typed=lambda: termios.tcflush(sys.stdin, termios.TCIFLUSH), 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, execv=os.execv,
git=git_repo.git, git=git_repo.git,
input=input, input=input,
@@ -120,11 +127,12 @@ def system_effects(backends: typing.Mapping[Notifier, Backend[typing.Any]] = BAC
now=time.time, now=time.time,
on_terminal=lambda: sys.stdin.isatty() and sys.stdout.isatty(), on_terminal=lambda: sys.stdin.isatty() and sys.stdout.isatty(),
python=lambda args, cwd: run_python(args, cwd, PYTHON_TIMEOUT), python=lambda args, cwd: run_python(args, cwd, PYTHON_TIMEOUT),
replace_file=replace_file,
sleep=time.sleep, sleep=time.sleep,
stream_claude=StreamingClaude, stream_claude=StreamingClaude,
timer=start_timer, timer=start_timer,
tmux=run_tmux, tmux=lambda args: run_tmux(args, env),
tmux_on_terminal=lambda args: subprocess.run(["tmux", *args]).returncode, 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 return stop
def run_tmux(args: list[str]) -> subprocess.CompletedProcess[str]: def run_tmux(args: list[str], env: typing.Mapping[str, str] | None) -> 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.""" """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] command = ["tmux", *args]
try: 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: except OSError as failure:
return subprocess.CompletedProcess(command, 127, "", f"could not start: {failure}") return subprocess.CompletedProcess(command, 127, "", f"could not start: {failure}")
+23 -26
View File
@@ -10,7 +10,6 @@ import tempfile
import threading import threading
import time import time
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
@@ -24,7 +23,7 @@ STAMP = f"<{NOW}>"
def clocked(): def clocked():
"""The system's effects on a clock stopped at NOW, which renders a time as <seconds>.""" """The system's effects on a clock stopped at NOW, which renders a time as <seconds>."""
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): def ticks(pid):
@@ -78,13 +77,10 @@ class Said(unittest.TestCase):
class LocalTime(unittest.TestCase): class LocalTime(unittest.TestCase):
def test_renders_seconds_in_the_machines_zone(self): 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. # 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.
self.addCleanup(time.tzset) 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}))"
zone = unittest.mock.patch.dict(os.environ, {"TZ": "XXX-2"}) ran = subprocess.run([sys.executable, "-c", script], capture_output=True, check=True, env=os.environ | {"TZ": "XXX-2"}, text=True)
zone.start() self.assertEqual(ran.stdout, "2026-09-21 16:13:20\n")
self.addCleanup(zone.stop)
time.tzset()
self.assertEqual(system_effects().local_time(NOW), "2026-09-21 16:13:20")
@@ -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. # 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.write_text(f"#!{sys.executable}\nimport os, signal, sys\nos.kill(os.getppid(), signal.SIGINT)\nsys.exit(3)\n")
claude.chmod(0o755) 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)) self.assertEqual((code, caught, signal.getsignal(signal.SIGINT)), (3, [], before))
@@ -110,27 +106,28 @@ class TmuxEffect(unittest.TestCase):
tmux = pathlib.Path(folder, "tmux") tmux = pathlib.Path(folder, "tmux")
tmux.write_text(f"#!{sys.executable}\nimport sys\nprint(' '.join(sys.argv[1:]))\nsys.exit(4)\n") tmux.write_text(f"#!{sys.executable}\nimport sys\nprint(' '.join(sys.argv[1:]))\nsys.exit(4)\n")
tmux.chmod(0o755) 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): def test_runs_tmux_and_returns_what_it_printed(self):
with tempfile.TemporaryDirectory() as folder, self.fake_tmux(folder): with tempfile.TemporaryDirectory() as folder:
done = system_effects().tmux(["list-windows", "-t", "=loop-proj"]) done = self.fake_tmux(folder).tmux(["list-windows", "-t", "=loop-proj"])
self.assertEqual((done.returncode, done.stdout), (4, "list-windows -t =loop-proj\n")) 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): 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") 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): 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}): with tempfile.TemporaryDirectory() as folder:
done = system_effects().tmux(["-V"]) done = system_effects(pathlib.Path.home(), os.environ | {"PATH": folder}).tmux(["-V"])
self.assertEqual((done.args, done.returncode, done.stdout), (["tmux", "-V"], 127, "")) self.assertEqual((done.args, done.returncode, done.stdout), (["tmux", "-V"], 127, ""))
self.assertIn("could not start", done.stderr) self.assertIn("could not start", done.stderr)
def test_runs_tmux_on_the_terminal_and_returns_its_exit_code(self): 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()): with tempfile.TemporaryDirectory() as folder, contextlib.redirect_stdout(io.StringIO()):
self.assertEqual(system_effects().tmux_on_terminal(["attach-session", "-t", "=loop-proj"]), 4) self.assertEqual(self.fake_tmux(folder).tmux_on_terminal(["attach-session", "-t", "=loop-proj"]), 4)
class PythonEffect(unittest.TestCase): class PythonEffect(unittest.TestCase):
@@ -169,8 +166,8 @@ class ChildShells(unittest.TestCase):
os.killpg(proc.pid, signal.SIGKILL) os.killpg(proc.pid, signal.SIGKILL)
proc.wait() proc.wait()
def shell_of(self, proc): def shell_of(self, proc, slots=pathlib.Path("/no-slots")):
[found] = [each for each in child_shells.shells_of(os.getpid()) if each.pid == proc.pid] [found] = [each for each in child_shells.shells_of(os.getpid(), slots, child_shells.children_of) if each.pid == proc.pid]
return found return found
def test_reads_a_child_shells_command_and_whether_a_busy_tool_runs_below_it(self): 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): 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. # 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 = pathlib.Path(state, "run-heavy", "slot-0")
slot.parent.mkdir() slot.parent.mkdir()
holding = self.started(f"exec 9>{slot}; sleep 60; true") holding = self.started(f"exec 9>{slot}; sleep 60; true")
plain = self.started("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): def test_a_shell_written_output_is_the_modified_time_of_its_output_file_and_else_its_start(self):
with tempfile.NamedTemporaryFile() as output: with tempfile.NamedTemporaryFile() as output:
@@ -264,8 +261,8 @@ class ChildShells(unittest.TestCase):
self.assertFalse(child_shells.has_slot(2**30, pathlib.Path("/"))) self.assertFalse(child_shells.has_slot(2**30, pathlib.Path("/")))
shell = self.started("sleep 60; true") shell = self.started("sleep 60; true")
real = child_shells.children_of 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)): 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())], [shell.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): 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"] 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)) 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): 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()) before = set(threading.enumerate())
stop = start_timer(60, lambda: None) stop = start_timer(60, lambda: None)
self.addCleanup(stop) self.addCleanup(stop)
+10 -7
View File
@@ -2,7 +2,6 @@
import fcntl import fcntl
import os import os
import pathlib import pathlib
import time
import typing import typing
CPUS_PER_SLOT = 4 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" return pathlib.Path(state) / "run-heavy"
def taken_slot(folder: pathlib.Path, slots: int) -> tuple[int, int] | None: # Locks an open file, as fcntl.flock does.
"""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.""" 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): for slot in range(slots):
fd = os.open(folder / f"slot-{slot}", os.O_RDWR | os.O_CREAT, 0o644) fd = os.open(folder / f"slot-{slot}", os.O_RDWR | os.O_CREAT, 0o644)
try: try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) lock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError: except BlockingIOError:
os.close(fd) os.close(fd)
continue continue
@@ -42,10 +45,10 @@ def taken_slot(folder: pathlib.Path, slots: int) -> tuple[int, int] | None:
return 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]: 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, once one frees. While every slot is held, it calls say once and tries again each POLL_SECONDS.""" """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 said = False
while (taken := taken_slot(folder, slots)) is None: while (taken := taken_slot(folder, slots, lock)) is None:
if not said: if not said:
say() say()
said = True said = True
+14 -9
View File
@@ -5,7 +5,6 @@ import pathlib
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
@@ -57,13 +56,19 @@ class SlotFolder(unittest.TestCase):
class TakenSlot(unittest.TestCase): class TakenSlot(unittest.TestCase):
def test_a_lock_that_fails_for_another_reason_than_a_holder_closes_its_file_and_raises(self): def test_a_lock_that_fails_for_another_reason_than_a_holder_closes_its_file_and_raises(self):
closed = [] locked = []
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):
def lock(fd, operation):
locked.append(fd)
raise OSError(errno.ENOLCK, "No locks available")
with tempfile.TemporaryDirectory() as folder:
with self.assertRaises(OSError): with self.assertRaises(OSError):
slots.taken_slot(pathlib.Path(folder), 2) slots.taken_slot(pathlib.Path(folder), 2, lock)
self.assertEqual(len(closed), 1) self.assertEqual(len(locked), 1)
self.assertIsInstance(closed[0], int) with self.assertRaises(OSError) as closed:
os.close(closed[0]) os.fstat(locked[0])
self.assertEqual(closed.exception.errno, errno.EBADF)
class WaitedSlot(unittest.TestCase): class WaitedSlot(unittest.TestCase):
@@ -79,14 +84,14 @@ class WaitedSlot(unittest.TestCase):
if len(slept) == 2: if len(slept) == 2:
held[1].close() 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) os.close(fd)
held[0].close() held[0].close()
self.assertEqual((slot, said, slept), (1, [True], [slots.POLL_SECONDS] * 2)) self.assertEqual((slot, said, slept), (1, [True], [slots.POLL_SECONDS] * 2))
def test_takes_a_free_slot_at_once(self): def test_takes_a_free_slot_at_once(self):
with tempfile.TemporaryDirectory() as folder: 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) os.close(fd)
self.assertEqual(slot, 0) self.assertEqual(slot, 0)
+2 -1
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os
import pathlib import pathlib
import signal import signal
import sys import sys
@@ -11,4 +12,4 @@ from mutate_changes import main
# SystemExit, the run unwinds as on Ctrl-C. # SystemExit, the run unwinds as on Ctrl-C.
for number in (signal.SIGHUP, signal.SIGTERM): for number in (signal.SIGHUP, signal.SIGTERM):
signal.signal(number, lambda number, frame: sys.exit(128 + number)) 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))
+8 -8
View File
@@ -3,16 +3,16 @@ import os
import pathlib import pathlib
from mutate_changes.changed_lines import by_folder, label, runs 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 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-mutants' exit codes for a run that finished: every mutant caught, some missed, and some timed out.
CARGO_FINISHED = frozenset({0, 2, 3}) CARGO_FINISHED = frozenset({0, 2, 3})
CARGO_OUTCOMES = ("caught", "missed", "timeout", "unviable") 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 `<path>:<line>:<column>: <outcome>: <mutation>`. """A line with cargo-mutants' mutant count, then a line per mutant it missed or that timed out, as `<path>:<line>:<column>: <outcome>: <mutation>`.
cargo-mutants runs in top.""" cargo-mutants runs in top."""
@@ -26,7 +26,7 @@ def run_cargo_mutants(top: str, base: str, paths: list[str], prefix: str | None,
try: try:
pathlib.Path(scratch, "branch.diff").write_text(diff, encoding="utf-8") pathlib.Path(scratch, "branch.diff").write_text(diff, encoding="utf-8")
relative = os.path.relpath(scratch, top) 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: if isinstance(ran, Failure) or ran.code not in CARGO_FINISHED:
return ran if isinstance(ran, Failure) else failed("cargo mutants", ran) 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} 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 # 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. # 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 """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. 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) unplaced = sorted(files)
continue continue
mutate = ",".join(f"{path}:{first}-{last}" for path, lines in sorted(files.items()) for first, last in runs(lines)) 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: if isinstance(ran, Failure) or ran.code:
return ran if isinstance(ran, Failure) else whole_output(f"Stryker in {label(folder)}", ran) 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()]) 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 [])] 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.""" """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: if isinstance(ran, Failure) or ran.code:
return ran if isinstance(ran, Failure) else whole_output("Infection", ran) return ran if isinstance(ran, Failure) else whole_output("Infection", ran)
return ["Infection in the checkout's top:", *ran.output.splitlines()] return ["Infection in the checkout's top:", *ran.output.splitlines()]
+21 -13
View File
@@ -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 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. whole report.
""" """
import functools
import os
import sys import sys
import time import time
import typing
from mutate_changes.changed_lines import added_lines, not_mutated, tool_for from mutate_changes.changed_lines import added_lines, not_mutated, tool_for
from machine_load.slots import CPUS_PER_SLOT 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 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.in_diff_tools import run_cargo_mutants, run_infection, run_stryker
from mutate_changes.mutmut_tool import run_mutmut from mutate_changes.mutmut_tool import run_mutmut
from mutate_changes.tool_process import run_tool
USAGE = "usage: mutate-changes [--base <ref>] [--prefix <shell text>] [--jobs <n>]" USAGE = "usage: mutate-changes [--base <ref>] [--prefix <shell text>] [--jobs <n>]"
# A panel of three readers, seated as the README's audience, picked the command's name and these options on 2026-11-11. # 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") 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"]): if argv in (["-h"], ["--help"]):
print(__doc__.strip()) print(__doc__.strip())
return 0 return 0
@@ -66,7 +72,7 @@ def main(argv: list[str]) -> int:
print(f"{given.reason}; {USAGE}", file=sys.stderr) print(f"{given.reason}; {USAGE}", file=sys.stderr)
return 1 return 1
started = time.monotonic() started = time.monotonic()
report = mutate(given) report = mutate(given, env)
if isinstance(report, Failure): if isinstance(report, Failure):
print(report.reason, file=sys.stderr) print(report.reason, file=sys.stderr)
return 1 return 1
@@ -89,12 +95,13 @@ def options(argv: list[str]) -> dict[str, str] | Failure:
return given 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.""" """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): if isinstance(checkout, Failure):
return checkout 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): if isinstance(base, Failure):
return base return base
found = git("merge-base", base, "HEAD", cwd=checkout) 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): if isinstance(names, Failure):
return names return names
paths = list(filter(None, names.split("\0"))) 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): if isinstance(changed, Failure):
return changed 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."] 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()): for tool, files in sorted(changed.items()):
if tool == "cargo-mutants": 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": elif tool == "infection":
lines = run_infection(checkout, merge_base, prefix, jobs) lines = run_infection(checkout, merge_base, run, jobs)
elif tool == "stryker": elif tool == "stryker":
lines = run_stryker(checkout, files, prefix, jobs) lines = run_stryker(checkout, files, run, jobs)
else: else:
lines = run_mutmut(checkout, files, prefix, jobs) lines = run_mutmut(checkout, files, run, git, jobs)
if isinstance(lines, Failure): if isinstance(lines, Failure):
return lines return lines
report.extend(lines) report.extend(lines)
return [*report, *not_mutated(paths)] 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.""" """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]]] = {} changed: dict[str, dict[str, frozenset[int]]] = {}
for path in paths: for path in paths:
+15 -15
View File
@@ -11,10 +11,10 @@ import shutil
import tomllib import tomllib
from mutate_changes.changed_lines import by_folder, label 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 failure import Failure
from mutate_changes.mutmut_mutants import function_keys, mutant_globs, mutated_line, named, statuses 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 from mutate_changes.mutmut_setup import left_out, mutmut_config
CAUGHT = frozenset({"caught by type check", "killed"}) CAUGHT = frozenset({"caught by type check", "killed"})
@@ -42,20 +42,20 @@ class Folder:
return label(self.path) 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. """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.""" changed maps each file, by its path from top, to its changed lines."""
report: list[str] = [] report: list[str] = []
for path, files in sorted(by_folder(top, changed, CONFIGS).items()): 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): if isinstance(lines, Failure):
return lines return lines
report.extend(lines) report.extend(lines)
return report 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) root = os.path.join(top, path)
folder = read_folder(root, path, files) folder = read_folder(root, path, files)
if isinstance(folder, Failure): if isinstance(folder, Failure):
@@ -72,7 +72,7 @@ def mutate_folder(top: str, path: str, files: dict[str, frozenset[int]], prefix:
try: try:
copy = os.path.join(scratch, "copy") copy = os.path.join(scratch, "copy")
copied = copy_files(root, listed.split("\0"), 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: except OSError as failure:
return Failure(f"could not set mutmut up in {scratch}, a copy of {root}: {failure}") return Failure(f"could not set mutmut up in {scratch}, a copy of {root}: {failure}")
finally: finally:
@@ -122,8 +122,8 @@ def copy_files(root: str, paths: list[str], copy: str) -> list[str]:
return sorted(copied) return sorted(copied)
def mutate_copy(copy: str, copied: list[str], folder: Folder, argv: list[str], prefix: str | None) -> list[str] | Failure: 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, prefix) ran = run_until_stats_pass(copy, copied, folder, argv, run)
if isinstance(ran, Failure): if isinstance(ran, Failure):
return ran return ran
output, left = 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") return Failure(f"mutmut in {folder.label} wrote no mutants/ in {copy}; --prefix must run it in $PWD")
if NO_MUTANT in output: if NO_MUTANT in output:
return [f"mutmut in {folder.label}: none of the functions the changed lines fall in has a mutant."] 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: if isinstance(results, Failure) or results.code:
return results if isinstance(results, Failure) else failed(f"mutmut results in {folder.label}", results) return results if isinstance(results, Failure) else failed(f"mutmut results in {folder.label}", results)
shown, counted = not_killed(folder, statuses(results.output), copy) 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] 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.""" """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] = [] left: list[str] = []
if isinstance(ran, Ran) and STATS_FAILED in ran.output: 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)): 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): if isinstance(ran, Failure):
return ran return ran
if ran.code and NO_MUTANT not in ran.output and NO_TEST not in ran.output: 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 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) config = mutmut_config(folder.setup, sorted(folder.files), copied, options)
if isinstance(config, Failure): if isinstance(config, Failure):
return Failure(f"{folder.label}: {config.reason}") return Failure(f"{folder.label}: {config.reason}")
pathlib.Path(copy, "setup.cfg").write_text(config, encoding="utf-8") 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]: def not_killed(folder: Folder, found: dict[str, str], copy: str) -> tuple[list[str], int]:
+7 -7
View File
@@ -8,8 +8,8 @@ import tempfile
import textwrap import textwrap
import threading import threading
import time import time
import typing
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 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()) self.assertTrue(pathlib.Path(folder, "stopped").is_file())
def test_an_interrupted_tool_that_ignores_sigint_is_killed_after_the_grace_period(self): 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): with tempfile.TemporaryDirectory() as folder:
self.assertLess(self.interrupted(folder, "trap '' INT; echo $$ > started; sleep 60;"), 30) 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())) 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): 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) killpg(group, number)
raise ProcessLookupError raise ProcessLookupError
with unittest.mock.patch("os.killpg", then_gone), tempfile.TemporaryDirectory() as folder: with tempfile.TemporaryDirectory() as folder:
self.interrupted(folder, "sleep 60 & echo $! > started; wait;") 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.""" """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") started = pathlib.Path(folder, "started")
@@ -252,7 +252,7 @@ class ToolProcess(unittest.TestCase):
threading.Thread(target=interrupt, daemon=True).start() threading.Thread(target=interrupt, daemon=True).start()
began = time.monotonic() began = time.monotonic()
with self.assertRaises(KeyboardInterrupt): 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 return time.monotonic() - began
def assert_ends(self, pid: int) -> None: def assert_ends(self, pid: int) -> None:
@@ -1,5 +1,6 @@
"""mutate-changes run on a branch, against fake mutation tools that log how they were called.""" """mutate-changes run on a branch, against fake mutation tools that log how they were called."""
import contextlib import contextlib
import functools
import io import io
import os import os
import pathlib import pathlib
@@ -11,14 +12,15 @@ import sys
import tempfile import tempfile
import time import time
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from mutate_changes.test_mutate_changes import ended from mutate_changes.test_mutate_changes import ended
from git_cli import git_repo
from test_env import isolated_env from test_env import isolated_env
from mutate_changes import main from mutate_changes import main
from mutate_changes.in_diff_tools import run_cargo_mutants 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" TOOL = pathlib.Path(__file__).resolve().parents[1] / "mutate-changes"
SETUP = "[mutmut]\ndo_not_mutate = *_old.py\n" SETUP = "[mutmut]\ndo_not_mutate = *_old.py\n"
@@ -138,8 +140,8 @@ class MutateChangesRuns(unittest.TestCase):
before = os.getcwd() before = os.getcwd()
os.chdir(cwd or self.checkout) os.chdir(cwd or self.checkout)
try: 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: with contextlib.redirect_stdout(io.StringIO()) as out, contextlib.redirect_stderr(io.StringIO()) as err:
code = main.main(list(args)) code = main.main(list(args), {**isolated_env(), **(env or {})})
finally: finally:
os.chdir(before) os.chdir(before)
return subprocess.CompletedProcess(args, code, out.getvalue(), err.getvalue()) 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.py").write_text("def f():\n\treturn 1\n")
(self.checkout / "-x.rs").write_text("fn f() {}\n") (self.checkout / "-x.rs").write_text("fn f() {}\n")
base = self.git("rev-parse", "HEAD").strip() 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})}}) git = functools.partial(git_repo.git, env=isolated_env())
ran = run_cargo_mutants(str(self.checkout), base, ["-x.rs"], f"python3 {self.temp / 'fake_tools.py'} {self.log} 0", 1) 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.assertIsInstance(ran, list)
self.assertIn("+++ b/-x.rs\n", self.log.read_text()) self.assertIn("+++ b/-x.rs\n", self.log.read_text())
+14 -7
View File
@@ -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.""" """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 contextlib
import dataclasses import dataclasses
import os
import pathlib import pathlib
import shlex import shlex
import shutil import shutil
@@ -9,6 +8,7 @@ import signal
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
import typing
from failure import Failure from failure import Failure
@@ -21,14 +21,21 @@ class Ran:
output: str # stdout and stderr, as the tool interleaved them output: str # stdout and stderr, as the tool interleaved them
def run_tool(argv: list[str], cwd: str, prefix: str | None) -> Ran | Failure: class Run(typing.Protocol):
"""Run a tool in cwd, reading each byte of its output that is not UTF-8 as U+FFFD. """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 `<prefix> "$@"` with argv as its arguments, so the prefix keeps its own quoting and each of argv stays one word.""" With a prefix, sh runs `<prefix> "$@"` 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 command = ["sh", "-c", f'{prefix} "$@"', "sh", *argv] if prefix else argv
try: try:
# In a session of its own, the tool and each process it starts share one process group, which ends whole below. # 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: except OSError as failure:
return Failure(f"{shlex.join(argv)} could not start: {failure.strerror}") return Failure(f"{shlex.join(argv)} could not start: {failure.strerror}")
try: 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. # reaches only this process, so pass the tool SIGINT first: a container's CLI, such as docker compose, then stops its container.
try: try:
with contextlib.suppress(ProcessLookupError): with contextlib.suppress(ProcessLookupError):
os.killpg(process.pid, signal.SIGINT) killpg(process.pid, signal.SIGINT)
with contextlib.suppress(subprocess.TimeoutExpired): with contextlib.suppress(subprocess.TimeoutExpired):
process.wait(timeout=GRACE_SECONDS) process.wait(timeout=grace)
finally: finally:
with contextlib.suppress(ProcessLookupError): with contextlib.suppress(ProcessLookupError):
os.killpg(process.pid, signal.SIGKILL) killpg(process.pid, signal.SIGKILL)
process.wait() process.wait()
raise raise
return Ran(process.returncode, output) return Ran(process.returncode, output)
+5 -3
View File
@@ -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.""" """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 dataclasses
import pathlib import pathlib
import typing
import urllib.parse import urllib.parse
from failure import Failure 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 import json_request
from rest_client.env_file import read_env 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") 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.""" """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: def error_message(answer: object) -> str:
+5 -3
View File
@@ -2,9 +2,10 @@
import dataclasses import dataclasses
import html import html
import pathlib import pathlib
import typing
from failure import Failure 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 import json_request
from rest_client.env_file import read_env 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'}") 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.""" """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: def error_message(answer: object) -> str:
+3 -4
View File
@@ -2,7 +2,6 @@ import pathlib
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
@@ -102,9 +101,9 @@ class Send(unittest.TestCase):
class HttpJson(unittest.TestCase): class HttpJson(unittest.TestCase):
def test_sends_the_token_as_a_bearer_and_reads_mattermosts_message_from_an_error(self): 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: sent = []
self.assertEqual(dm.http_json("POST", "https://chat.example/api/v4/posts", "secret-token", {"message": "hi"}), {"id": "post-id"}) 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"})
sent.assert_called_once_with("POST", "https://chat.example/api/v4/posts", "Bearer secret-token", {"message": "hi"}, dm.TIMEOUT, dm.error_message) 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) self.assertEqual(dm.TIMEOUT, 10)
def test_reads_mattermosts_message_from_an_error_answer(self): def test_reads_mattermosts_message_from_an_error_answer(self):
+3 -2
View File
@@ -1,6 +1,7 @@
import contextlib import contextlib
import dataclasses import dataclasses
import io import io
import os
import pathlib import pathlib
import subprocess import subprocess
import sys import sys
@@ -73,14 +74,14 @@ class Notify(Project):
self.name_notifier("slack", SLACK_KEYS) self.name_notifier("slack", SLACK_KEYS)
slack = Slack({"ok": True}) slack = Slack({"ok": True})
with contextlib.chdir(self.checkout): 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]) 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): 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) self.name_notifier("slack", SLACK_KEYS)
slack = Slack({"ok": True}) slack = Slack({"ok": True})
with contextlib.chdir(self.home): 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]) 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): def test_skips_the_notice_for_a_project_that_names_no_notifier_even_where_its_env_file_holds_the_keys(self):
+3 -4
View File
@@ -2,7 +2,6 @@ import pathlib
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
@@ -76,9 +75,9 @@ class Send(unittest.TestCase):
class HttpJson(unittest.TestCase): class HttpJson(unittest.TestCase):
def test_sends_the_token_as_a_bearer_and_reads_slacks_code_from_an_error(self): 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: sent = []
self.assertEqual(dm.http_json("POST", "https://slack.com/api/chat.postMessage", "xoxb-secret", {"text": "hi"}), {"ok": True}) 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})
sent.assert_called_once_with("POST", "https://slack.com/api/chat.postMessage", "Bearer xoxb-secret", {"text": "hi"}, dm.TIMEOUT, dm.error_message) 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"], "")): for answer, code in (({"error": "ratelimited"}, "ratelimited"), ({"error": 7}, ""), ({}, ""), (["error"], "")):
with self.subTest(answer=answer): with self.subTest(answer=answer):
self.assertEqual(dm.error_message(answer), code) self.assertEqual(dm.error_message(answer), code)
+3 -3
View File
@@ -293,7 +293,7 @@ class CodeHealth(unittest.TestCase):
pathlib.Path(temp, "docs").mkdir() pathlib.Path(temp, "docs").mkdir()
pathlib.Path(temp, "docs", "comprehension-history.md").write_text(SCORED.format(overall="7.00")) pathlib.Path(temp, "docs", "comprehension-history.md").write_text(SCORED.format(overall="7.00"))
with contextlib.redirect_stderr(io.StringIO()): 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.assertIsInstance(ranked, ranking.Ranked)
self.assertEqual(ranked.under_bar, []) self.assertEqual(ranked.under_bar, [])
self.assertIn("| code health by 1 | 25.4 |", todo.read_text()) 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").mkdir()
pathlib.Path(temp, "docs", "comprehension-history.md").write_text(SCORED.format(overall="6.50")) pathlib.Path(temp, "docs", "comprehension-history.md").write_text(SCORED.format(overall="6.50"))
with contextlib.redirect_stderr(io.StringIO()): 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.assertIsInstance(ranked, ranking.Ranked)
self.assertIn("| code health by 1 | 25.4 |", todo.read_text()) 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): class Risk(unittest.TestCase):
+9 -7
View File
@@ -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.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.markdown_edits import bold_cell, plain, unfit_text
from plan_store.plans import cells, no_item, Plan, Written 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 project_config.settings import Epic, Finish, JIRA_FINISH_STATUS, JIRA_FINISH_TRANSITION
from failure import Failure from failure import Failure
from plan.release import parsed, Release, tagged from plan.release import parsed, Release, tagged
@@ -71,6 +71,8 @@ class JiraPlan:
to_adf: ToAdf = markdown_to_adf to_adf: ToAdf = markdown_to_adf
# How a finished item moves; None where the config doesn't say, and then `finish` is a Failure. # How a finished item moves; None where the config doesn't say, and then `finish` is a Failure.
finish_by: Finish | None = None 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. # 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) scoring: ReadsScoring = dataclasses.field(kw_only=True)
@@ -86,7 +88,7 @@ class JiraPlan:
authorization = basic_auth(self.env_file) authorization = basic_auth(self.env_file)
if isinstance(authorization, Failure): if isinstance(authorization, Failure):
return authorization return authorization
answered = children(self.epic, authorization, self.http) answered = children(self.epic, authorization, self.http, self.max_pages)
if isinstance(answered, Failure): if isinstance(answered, Failure):
return answered return answered
issues = [issue_of(raw) for raw in answered] issues = [issue_of(raw) for raw in answered]
@@ -115,7 +117,7 @@ class JiraPlan:
version = self.fix_version(filing.release, authorization) version = self.fix_version(filing.release, authorization)
if isinstance(version, Failure): if isinstance(version, Failure):
return version return version
answered = children(self.epic, authorization, self.http) answered = children(self.epic, authorization, self.http, self.max_pages)
if isinstance(answered, Failure): if isinstance(answered, Failure):
return answered return answered
if missing := missing_blocker(filing.blockers, keys_of(answered), self.label): 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: 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_.""" """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_) 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: 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) authorization = basic_auth(self.env_file)
if isinstance(authorization, Failure): if isinstance(authorization, Failure):
return authorization return authorization
answered = children(self.epic, authorization, self.http) answered = children(self.epic, authorization, self.http, self.max_pages)
if isinstance(answered, Failure): if isinstance(answered, Failure):
return answered return answered
raw = self.child_in(answered, id_) 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) 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 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. # 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) seen = http("GET", f"{epic.site}/rest/api/3/issue/{epic.key}?fields=summary", authorization, None)
if isinstance(seen, Failure): if isinstance(seen, Failure):
return Failure(f"cannot read the epic {epic.key}: {seen.reason}") 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: def issue_of(raw: object) -> Issue | Failure:
+1 -3
View File
@@ -4,7 +4,6 @@ import pathlib
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 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): def test_a_search_that_never_names_a_last_page_fails_the_read(self):
self.pages = [{"issues": [], "nextPageToken": "more"}] * 2 self.pages = [{"issues": [], "nextPageToken": "more"}] * 2
with unittest.mock.patch("atlassian.jira.MAX_PAGES", 2): self.assertEqual(dataclasses.replace(self.store(), max_pages=2).read(), Failure("the search for ABC-12's children did not end within 2 pages"))
self.assertEqual(self.store().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): def test_an_issue_in_another_shape_fails_the_read_naming_it(self):
good = issue("ABC-1") good = issue("ABC-1")
+1 -6
View File
@@ -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.""" """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 pathlib
import subprocess import subprocess
import tempfile import tempfile
import unittest import unittest
import unittest.mock
from test_env import isolated_env 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: 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()) subprocess.run(["git", "init", "--quiet", str(checkout)], check=True, env=isolated_env())
if home is None: if home is None:
made = tempfile.TemporaryDirectory() made = tempfile.TemporaryDirectory()
test.addCleanup(made.cleanup) test.addCleanup(made.cleanup)
home = pathlib.Path(made.name) 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) return write_config(home, checkout, goals)
@@ -4,7 +4,6 @@ import subprocess
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
+4 -4
View File
@@ -130,17 +130,17 @@ class Act(unittest.TestCase):
class BlackBox(unittest.TestCase): class BlackBox(unittest.TestCase):
def setUp(self) -> None: def setUp(self) -> None:
self.top = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())).resolve() 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]: 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): def test_a_file_record_opens_as_its_own_file_and_save_has_nothing_to_store(self):
opened = self.run_tool("open", "decisions") opened = self.run_tool("open", "decisions")
self.assertEqual((opened.returncode, opened.stdout, opened.stderr), (0, f"{self.top}/docs/decisions.md\n", "")) self.assertEqual((opened.returncode, opened.stdout, opened.stderr), (0, f"{self.top}/docs/decisions.md\n", ""))
saved = self.run_tool("save", "decisions") 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.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): def test_the_goals_file_opens_as_the_config_names_it(self):
opened = self.run_tool("open", "goals-and-audience") opened = self.run_tool("open", "goals-and-audience")
@@ -153,7 +153,7 @@ class BlackBox(unittest.TestCase):
self.assertIn("usage: project-record open|save <record>, where <record> is one of decisions, score-anchors, comprehension-history, reading-cost, changelog, goals-and-audience", ran.stderr) self.assertIn("usage: project-record open|save <record>, where <record> 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): 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") ran = self.run_tool("open", "decisions")
self.assertEqual((ran.returncode, ran.stdout), (1, "")) self.assertEqual((ran.returncode, ran.stdout), (1, ""))
self.assertIn("no config names the checkout", ran.stderr) self.assertIn("no config names the checkout", ran.stderr)
+3 -3
View File
@@ -98,14 +98,14 @@ from plan_store.stores import file_plan
from project_config.main import located 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"]): if argv[:1] in (["-h"], ["--help"]):
print(__doc__.strip()) print(__doc__.strip())
return 0 return 0
args = parse_args(argv) args = parse_args(argv)
if isinstance(args, Failure): if isinstance(args, Failure):
return fail(args) return fail(args)
config = located(None, pathlib.Path.home()) config = located(None, home)
if isinstance(config, Failure): if isinstance(config, Failure):
return fail(config) return fail(config)
if config.plan_file is None and args == (True, None): 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__": if __name__ == "__main__":
sys.exit(main(sys.argv[1:])) sys.exit(main(sys.argv[1:], pathlib.Path.home()))
+4 -3
View File
@@ -16,7 +16,7 @@ from transcript_archive.write import archive
from claude_code.entries import AssistantTurn from claude_code.entries import AssistantTurn
from project_config.templates import BRANCH from project_config.templates import BRANCH
from reading_cost.measure import Checkout, Reading from reading_cost.measure import Checkout, Reading
from text_files import compressed from text_files.doubles import compress
from failure import Failure from failure import Failure
from reading_cost import figures, main, measure from reading_cost import figures, main, measure
from transcript_archive.read import finished_chunks, FinishedChunk 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 import outcomes
from transcript_archive.location import project_archive from transcript_archive.location import project_archive
from reading_cost.shell_reads import Reads, shell_reads from reading_cost.shell_reads import Reads, shell_reads
from text_files.file_writes import replace_file
from project_config.doubles import write_config from project_config.doubles import write_config
TOOL = pathlib.Path(__file__).resolve().parents[1] / "reading-cost" 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.write_bytes(jsonl([{"timestamp": str(at)}, *worker]))
(transcript.with_suffix("") / "subagents" / "agent-a.jsonl").write_bytes(jsonl([said("1", "s1", tokens=1)])) (transcript.with_suffix("") / "subagents" / "agent-a.jsonl").write_bytes(jsonl([said("1", "s1", tokens=1)]))
with contextlib.redirect_stderr(io.StringIO()): 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): def run_tool(self, cwd, *args):
return subprocess.run([sys.executable, str(TOOL), *args], capture_output=True, cwd=cwd, env=self.env, text=True) 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): def test_measures_a_chunk_whose_archived_transcripts_are_compressed(self):
self.archive_chunk(1790000000) self.archive_chunk(1790000000)
for copy in self.archive.rglob("*.jsonl"): for copy in self.archive.rglob("*.jsonl"):
self.assertIsNone(compressed.compress(copy)) self.assertIsNone(compress(copy))
ran = self.run_tool(self.checkout) ran = self.run_tool(self.checkout)
self.assertEqual(ran.returncode, 0, ran.stderr) 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()) self.assertIn("33 new input tokens before the first commit.", (self.checkout / "docs" / "reading-cost.md").read_text())
@@ -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()
+21 -21
View File
@@ -81,6 +81,13 @@ def cycle(imported: dict[str, set[str]]) -> list[str]:
return [] 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): class PackageCycles(unittest.TestCase):
def test_no_package_under_bin_imports_one_that_imports_it_back(self): 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") 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): def test_an_import_of_the_layer_above_is_found_in_tests_too(self):
with tempfile.TemporaryDirectory() as temp: with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(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")): 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"})
(folder / package).mkdir(exist_ok=True)
(folder / package / name).write_text(source)
self.assertEqual(upward_imports(folder, {"low": ("high",)}), ["low/run.py imports high", "low/test_mod.py imports high"]) 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): def test_a_driver_imported_from_another_package_is_found_in_tests_too(self):
with tempfile.TemporaryDirectory() as temp: with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(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")): 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"})
(folder / package).mkdir(exist_ok=True)
(folder / package / name).write_text(source)
self.assertEqual(driver_imports(folder, ("a",)), ["b/mod.py imports a", "b/test_mod.py imports a"]) 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): 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): def test_a_test_module_imported_from_another_package_is_found_however_it_is_spelled(self):
with tempfile.TemporaryDirectory() as temp: with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(temp) folder = pathlib.Path(temp)
sources = ( tree(folder, {
("a", "test_mod.py", "from a.test_other import x\nfrom test_env import y\nfrom a.doubles import z\n"), "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/test_mod.py": "from a.test_mod import x\n",
("b", "mod.py", "def f():\n\timport a.deep.test_far\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"), "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",
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")
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"]) 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): def test_reads_an_import_deferred_into_a_function_and_leaves_out_tests(self):
with tempfile.TemporaryDirectory() as temp: with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(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", "")): tree(folder, {
(folder / package).mkdir() "a/__init__.py": "", "a/mod.py": "def f():\n\tfrom b.mod import x\n",
(folder / package / "__init__.py").write_text("") "b/__init__.py": "", "b/mod.py": "import a.mod\n",
(folder / package / "mod.py").write_text(source) "c/__init__.py": "", "c/mod.py": "", "c/test_mod.py": "import a\n",
(folder / "c" / "test_mod.py").write_text("import a\n") })
self.assertEqual(package_imports(folder), {"a": {"b"}, "b": {"a"}, "c": set()}) self.assertEqual(package_imports(folder), {"a": {"b"}, "b": {"a"}, "c": set()})
def test_a_cycle_through_a_deferred_import_is_found(self): def test_a_cycle_through_a_deferred_import_is_found(self):
+1 -1
View File
@@ -8,4 +8,4 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
from review_replay import main from review_replay import main
from loop_effects.effects import system_effects 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)))
+4 -5
View File
@@ -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`.""" """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 import typing
from review_replay.changes import Change from review_replay.changes import Change
@@ -10,8 +9,8 @@ from review_replay.rounds import Round
from model_routing.ladder import Rung 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: 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.""" """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]] = [] runs: list[list[set[int] | Failure]] = []
for applied in (None, None, None, change): for applied in (None, None, None, change):
label = change.changed if applied else change.unchanged 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: def replay_one(one: Round, _: Rung, applied: Change | None = applied, label: str = label) -> set[int] | Failure | Limited:
found = found_on(one, applied) found = found_on(one, applied)
if isinstance(found, Failure): 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 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): if isinstance(found, Limited):
return found return found
runs.append(found) runs.append(found)
+13 -12
View File
@@ -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. round to replay, or a usage limit.
""" """
import pathlib import pathlib
import sys
import typing import typing
from transcript_archive.location import archive_root from transcript_archive.location import archive_root
from review_replay.rounds import archived_rounds, Round, sample from review_replay.rounds import archived_rounds, Round, sample
from rung_replay.archive import archiving, print_report, replay_folder from rung_replay.archive import archiving, replay_folder
from rung_replay.command import arguments_or_exit, failed, limited_exit 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.changes import Change, CHANGES
from review_replay.replay import codes_found, findings_replayed from review_replay.replay import codes_found, findings_replayed
from review_replay.change_comparison import compared from review_replay.change_comparison import compared
from loop_effects.effects import Effects from loop_effects.effects import Effects
from review_replay.recall import ENOUGH_ROUNDS, recall_report, recalled
from failure import Failure from failure import Failure
from model_routing.ladder import Ladder, Rung from model_routing.ladder import Ladder, Rung
from claude_code.reply import Limited from claude_code.reply import Limited
from model_routing.ladder_file import read_ladder, SHIPPED 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 from review_rules.review_passes import ReviewedPass, REVIEWERS
TOOL = "review-replay" TOOL = "review-replay"
@@ -90,7 +90,8 @@ USAGE = f"{TOOL} <pass> <model> [{' | '.join(change.flag for change in CHANGES)}
LATEST = 10 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. # The shipped ladder, not a project's: the replay spans every project on the model.
ladder = read_ladder(SHIPPED) ladder = read_ladder(SHIPPED)
if isinstance(ladder, Failure): 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) rounds = archived_rounds(REVIEWERS[reviewed], project.model, archive_root(home, env), home)
if isinstance(rounds, Failure): if isinstance(rounds, Failure):
return failed(TOOL, rounds.reason) return failed(TOOL, rounds.reason)
codes = codes_found(rounds, effects) codes = codes_found(rounds, effects, say)
if not codes: 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") 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) chosen = sample(list(codes), LATEST)
folder = replay_folder(archive_root(home, env), TOOL, argv, effects.now()) folder = replay_folder(archive_root(home, env), TOOL, argv, effects.now())
effects = archiving(effects, folder) effects = archiving(effects, folder, say)
if change: 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): if isinstance(report, Limited):
return limited_exit(TOOL, report) return limited_exit(TOOL, report)
print_report(folder, report) print_report(folder, report)
return 0 return 0
def replay_one(found: Round, rung: Rung) -> set[int] | Failure | Limited: 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): 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 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): if isinstance(recall, Limited):
return limited_exit(TOOL, recall) 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 return 0
+8 -12
View File
@@ -43,10 +43,6 @@ def candidates(ladder: Ladder, project: Rung) -> list[Rung]:
return lower_efforts + down_from(ladder, project, ladder.rungs[0])[1:] 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: def count(found: list[set[int]]) -> int:
return sum(map(len, found)) 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) 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 rounds, (first, second) = recall.rounds, recall.reference
lines = [ 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"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 []), *([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): 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."]) 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: 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."]) 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.") 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%}" 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: 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.""" """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]] = [] runs: list[list[set[int] | Failure]] = []
for _ in range(2): 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): if isinstance(found, Limited):
return found return found
runs.append(found) runs.append(found)
@@ -112,8 +108,8 @@ def recalled(chosen: list[Round], ladder: Ladder, project: Rung, replay_one: typ
reference[1].append(second) reference[1].append(second)
recalls: list[RungRecall] = [] recalls: list[RungRecall] = []
lowest = None lowest = None
for rung in candidates(ladder, project) if enough(rounds) and needed(rounds, reference) > 0 else []: 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) on_rung: list[set[int] | Failure] | Limited = replayed_on(rounds, rung, replay_one, what, say, at_once)
if isinstance(on_rung, Limited): if isinstance(on_rung, Limited):
return on_rung 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. # A failed replay on a candidate found none, so it lowers that rung's count and never lifts it to what a rung needs.
+9 -9
View File
@@ -4,8 +4,8 @@ import json
import os import os
import pathlib import pathlib
import shutil import shutil
import sys
import tempfile import tempfile
import typing
from review_replay.rounds import authored_by, lost_inputs, reviewed_path, Round from review_replay.rounds import authored_by, lost_inputs, reviewed_path, Round
from review_replay.changes import Change from review_replay.changes import Change
@@ -34,12 +34,12 @@ class Code:
base: str base: str
def codes_found(rounds: list[Round], effects: Effects) -> dict[Round, Code]: 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; stderr names each round left out, and why.""" """The code each round reviewed, as code_of finds it; say is told each round left out, and why."""
codes = {} codes = {}
for found in rounds: for found in rounds:
if isinstance(code := code_of(found, effects), Failure): 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: else:
codes[found] = code codes[found] = code
return codes 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())] 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: 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 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)) reviewed: str | Failure | Limited = first_reply(lambda: review(found, code, rung, home, effects, change, scratch))
if isinstance(reviewed, (Failure, Limited)): if isinstance(reviewed, (Failure, Limited)):
return reviewed return reviewed
return first_reply(lambda: match(found.findings, reviewed, found.root, project, effects)) 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. # 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: try:
(home / REPORTS).mkdir(mode=0o700, parents=True, exist_ok=True) (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) return reviewed_in(found, code, rung, Copies(clone, reports), home, effects, change)
except OSError as failure: except OSError as failure:
return Failure(f"could not prepare the replay: {failure}") return Failure(f"could not prepare the replay: {failure}")
+7 -9
View File
@@ -1,12 +1,11 @@
import contextlib
import io
import pathlib import pathlib
import sys import sys
import unittest import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 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 review_replay.rounds import is_red, Round
from model_routing.ladder_file import read_ladder, SHIPPED from model_routing.ladder_file import read_ladder, SHIPPED
from model_routing.ladder import Rung 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): 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. # 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)] 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", lambda text: None, AT_ONCE, ENOUGH_ROUNDS)
recall = recalled(rounds, LADDER, REFERENCE, lambda one, rung: {0} if one == rounds[0] else set(), "prose rounds")
self.assertEqual(([one.rung for one in recall.tried], recall.lowest), ([LOW, SONNET], SONNET)) 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 rounds, reference = self.EIGHT
low = tried(LOW, rounds, reference, [{0, 1, 2}, {0, 1, 2}] * 4) low = tried(LOW, rounds, reference, [{0, 1, 2}, {0, 1, 2}] * 4)
sonnet = tried(SONNET, rounds, reference, [{1, 2, 3, 4}, {0}] * 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, [ 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.", "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.", "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): 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."]) self.assertEqual(few[1:], ["No rung tried: fewer than 8 rounds were replayed on the project's rung without a failure."])
rounds, _ = self.EIGHT rounds, _ = self.EIGHT
for reference in (([set()] * 8, [set()] * 8), (TENTH[0] * 4, TENTH[1] * 4)): 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."]) 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): def test_names_no_rung_where_none_qualifies(self):
rounds, reference = self.EIGHT rounds, reference = self.EIGHT
low = tried(LOW, rounds, reference, [{0}, {0}] * 4) 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"]) 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"])
+10 -12
View File
@@ -10,13 +10,12 @@ import subprocess
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 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.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 review_replay import changes, main, replay
from text_files import compressed from text_files.doubles import compress
from project_config.doubles import configured, write_config from project_config.doubles import configured, write_config
from loop_effects.effects import Effects, system_effects from loop_effects.effects import Effects, system_effects
from claude_code.entries import entry_from 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.reviewer_context import reviewer_context
from review_rules.review_passes import REVIEWERS from review_rules.review_passes import REVIEWERS
from model_routing.ladder import Rung from model_routing.ladder import Rung
from rung_replay import search
TOOL = pathlib.Path(__file__).resolve().parents[1] / "review-replay" TOOL = pathlib.Path(__file__).resolve().parents[1] / "review-replay"
REFERENCE = Rung("claude-opus-5-5", "medium") REFERENCE = Rung("claude-opus-5-5", "medium")
@@ -172,7 +170,7 @@ class Match(unittest.TestCase):
self.asked.append((args, cwd, prompt)) self.asked.append((args, cwd, prompt))
return subprocess.CompletedProcess(args, 0, stdout, "") 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): def test_asks_for_the_numbered_findings_the_other_report_raises_as_json_with_no_tools(self):
self.run_match("{}") self.run_match("{}")
@@ -222,7 +220,7 @@ class Main(unittest.TestCase):
self.write_chunk(key, "c1", "2026-10-14T10:00:00Z") 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") (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. # The fixture holds one round.
self.enterContext(unittest.mock.patch.object(recall, "ENOUGH_ROUNDS", 1)) self.limits = {"enough_rounds": 1}
self.reviews = [] self.reviews = []
self.review_settings = [] self.review_settings = []
self.agents = set() 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"]]}} 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), "") 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() out, err = io.StringIO(), io.StringIO()
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): 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() 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): 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): def test_replays_rounds_whose_archived_transcripts_are_compressed(self):
for copy in self.archive.rglob("*.jsonl"): 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]}) 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.assertEqual(code, 0, err)
self.assertTrue(out.startswith("Replayed 1 stability rounds of claude-opus-5-5 projects,"), out) 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): def test_copies_a_compressed_report_from_before_the_round_under_its_plain_name(self):
folder = self.review_folder_with_round_1() 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.") 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 = [] seen = []
@@ -513,7 +511,7 @@ class Main(unittest.TestCase):
(folder / "stability-round-1.md").chmod(0) (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.") 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())) 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) code, _, err = self.run_main(["stability", "claude-opus-5-5"], unused)
self.assertEqual(code, 0, err) self.assertEqual(code, 0, err)
self.assertEqual(self.reviews, []) 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): 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.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. # 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)): for failing in (range(3), range(2, 5)):
with self.subTest(failing=failing): with self.subTest(failing=failing):
+5 -4
View File
@@ -5,10 +5,11 @@ import dataclasses
import json import json
import pathlib import pathlib
import re import re
import typing
from routing_figures.calls import Call, SENTENCES from routing_figures.calls import Call, SENTENCES
from failure import Failure 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 from text_files.utc_stamp import STAMPED
CYCLES_PER_CHECK = 20 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)) 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: 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.""" """Keeps the calls in STATE, and adds a line for each to RAISED, which replace writes as replace_file does."""
kept = read_state(root) kept = read_state(root)
if isinstance(kept, Failure): if isinstance(kept, Failure):
return kept return kept
@@ -112,7 +113,7 @@ def mark_raised(root: pathlib.Path, calls: list[Call], at: str) -> Failure | Non
return unwritten return unwritten
# The first line opens a list below the heading; each later one joins it. # The first line opens a list below the heading; each later one joins it.
gap = "\n" if RAISED_LINE.search(text) else "\n\n" 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 return Failure(f"could not write {root / RAISED}: {unwritten.reason}") if unwritten else None
+18 -25
View File
@@ -10,7 +10,6 @@ import subprocess
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 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 branch_chunks.cut import BranchChunk
from routing_figures.calls import Call, calls, totals from routing_figures.calls import Call, calls, totals
from routing_figures.chunk import Chunk, chunk_figures, ChunkFigures, Reports, Subagent 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 project_config.doubles import configured, write_config
from routing_figures.state import due from routing_figures.state import due
from claude_code.entries import entry_from from claude_code.entries import entry_from
@@ -30,6 +29,7 @@ from plan.todo_table import Item
from transcript_archive import outcomes, write from transcript_archive import outcomes, write
from model_routing.ladder_file import read_ladder, SHIPPED from model_routing.ladder_file import read_ladder, SHIPPED
from routing_figures.render import render from routing_figures.render import render
from text_files.file_writes import replace_file
from model_routing.route import Route from model_routing.route import Route
from model_routing.ladder import Rung from model_routing.ladder import Rung
@@ -356,7 +356,7 @@ class Archive(unittest.TestCase):
def test_measures_a_chunk_whose_transcripts_are_compressed(self): 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")])) 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"): 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) found = archive.measure_archive(self.root, "/h", "", no_checkout)
self.assertEqual(found.chunks[0].dollars["subagent type"], {"worker": 2.0, "Explore": 2.0}) 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)} env = {**isolated_env(), "GIT_CONFIG_GLOBAL": os.devnull, "HOME": str(home)}
self.ship_then_fix(home / "code" / "log", env) 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")]) 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)]) 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): 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" 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)] 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) 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)
bare = archive.measure_archive(self.root, str(home), "", git_repo.git) config.write_text(config.read_text() + 'commit-title = "PAY-{id} - {summary}"\n')
config.write_text(config.read_text() + 'commit-title = "PAY-{id} - {summary}"\n') templated = archive.measure_archive(self.root, str(home), "", git_repo.git)
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 bare.chunks], [(0, 0)])
self.assertEqual([(chunk.rounds_past_first, chunk.fixed_later) for chunk in templated.chunks], [(1, 1)]) 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_config(home, home / "code" / "log") / "config.toml"
config.write_text(config.read_text() + 'commit-title = "PAY-{id}"\n') 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")]) 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(found.chunks, [])
self.assertEqual(len(found.unread), 1) self.assertEqual(len(found.unread), 1)
self.assertRegex(found.unread[0], r"^log: none of its chunks is measured: .*`commit-title` is 'PAY-\{id\}'") 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): def recording(*args, **options):
made.append((args[0], options)) made.append((args[0], options))
return git_repo.git(*args, **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)) 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")] 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}) self.assertEqual(set(read), {archive.GIT_TIMEOUT})
@@ -420,8 +416,7 @@ class Archive(unittest.TestCase):
(home / "code" / "log").mkdir(parents=True) (home / "code" / "log").mkdir(parents=True)
subprocess.run(["git", "init", "-q"], cwd=home / "code" / "log", check=True, env=env, capture_output=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")]) 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",)) 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): 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") first = message("m1", "2026-09-21T00:00:00Z")
transcript.write_text(json.dumps(first) + "\n") transcript.write_text(json.dumps(first) + "\n")
with contextlib.redirect_stderr(io.StringIO()): 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") 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 (found,) = archive.measure_archive(kept, "/h", "", no_checkout).chunks
self.assertEqual((found.start, found.unfinished), (LOW, 1)) self.assertEqual((found.start, found.unfinished), (LOW, 1))
self.assertEqual(found.dollars["rung"], {str(LOW): 2.0, str(HIGH): 2.0}) 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): 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")) a, b, c = Call("reports", ("Explore",)), Call("rounds", ("x", "y")), Call("unfinished", ("x", "y"))
self.assertEqual(state.raised(self.root), state.Raised("", frozenset())) 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, [a], "2026-10-15T00:00:00Z", replace_file))
self.assertIsNone(state.mark_raised(self.root, [b, c], "2026-10-15T01:00:00Z")) 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((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}))) self.assertEqual(state.raised(self.root), state.Raised("", frozenset({a, b, c})))
with open(self.root / state.RAISED, "a") as handle: with open(self.root / state.RAISED, "a") as handle:
handle.write("- 2026-10-15T01:00:00Z Reset\n") 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.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}))) 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): def test_fails_when_it_cannot_keep_the_state(self):
@@ -500,17 +495,15 @@ class Archive(unittest.TestCase):
self.root.chmod(0o555) self.root.chmod(0o555)
self.addCleanup(self.root.chmod, 0o755) self.addCleanup(self.root.chmod, 0o755)
self.assertIsInstance(state.mark_measured(self.root, 40), Failure) 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): def test_fails_when_it_cannot_list_a_raised_call_for_the_human(self):
self.assertIsNone(state.mark_measured(self.root, 40)) self.assertIsNone(state.mark_measured(self.root, 40))
written = state.replace_file 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"))
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"))
def test_knows_a_raised_call_by_its_fields_whatever_its_line_reads(self): def test_knows_a_raised_call_by_its_fields_whatever_its_line_reads(self):
call = Call("reports", ("Explore",)) 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.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})) self.assertEqual(state.raised(self.root).calls, frozenset({call}))
+22 -17
View File
@@ -9,7 +9,6 @@ import sys
import tempfile import tempfile
import typing import typing
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 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, "init", "-q", "-b", "main")
git(self.repo, "commit", "-q", "--allow-empty", "-m", "base") git(self.repo, "commit", "-q", "--allow-empty", "-m", "base")
git(self.repo, "checkout", "-q", "-b", "chunk") 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): def test_runs_git_guarded(self):
git(self.repo, "config", "core.fsmonitor", "/bin/false") git(self.repo, "config", "core.fsmonitor", "/bin/false")
@@ -1109,7 +1114,7 @@ class Hook(unittest.TestCase):
self.assertEqual(hook(call, self.repo).returncode, 2) self.assertEqual(hook(call, self.repo).returncode, 2)
def round_commit(self, title: str, given: str = "") -> subprocess.CompletedProcess: 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): def test_a_round_commit_whose_counts_differ_from_its_report_is_refused(self):
home = tempfile.mkdtemp() 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") 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): 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') config.write_text(config.read_text() + 'commit-title = "PAY-{id} - {summary}"\n')
git(self.repo, "checkout", "-q", "-b", "item-7") 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"]: 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) 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): 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') 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") 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) 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) 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): 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') config.write_text(config.read_text() + 'commit-title = "PAY-{id}"\n')
refused = self.round_commit("PAY-7 - Tidy") refused = self.round_commit("PAY-7 - Tidy")
self.assertEqual(refused.returncode, 2) self.assertEqual(refused.returncode, 2)
@@ -1234,21 +1239,21 @@ class Hook(unittest.TestCase):
self.assertEqual(self.round_commit("Tidy").returncode, 0) self.assertEqual(self.round_commit("Tidy").returncode, 0)
def test_a_commit_while_an_unranked_todo_has_changes_is_blocked(self): 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() pathlib.Path(self.repo, "docs").mkdir()
for name, content in [("README.md", README), ("docs/decisions.md", DECISIONS), ("todo.md", TODO)]: for name, content in [("README.md", README), ("docs/decisions.md", DECISIONS), ("todo.md", TODO)]:
pathlib.Path(self.repo, name).write_text(content) pathlib.Path(self.repo, name).write_text(content)
git(self.repo, "add", "README.md", "docs", "todo.md") git(self.repo, "add", "README.md", "docs", "todo.md")
git(self.repo, "commit", "-qm", "x", "--no-verify") 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"} 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") 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.assertEqual(ran.returncode, 2)
self.assertIn("todo.md: not ranked", ran.stderr) self.assertIn("todo.md: not ranked", ran.stderr)
def test_a_staged_todo_change_is_checked_and_no_clean_filter_runs(self): 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() 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")]: 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) 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) 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. # 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)) 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.assertEqual(ran.returncode, 2, ran.stderr)
self.assertFalse(pathlib.Path(self.repo, "ran").exists()) 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, "bin", "rank_todo.py").write_text(stub)
pathlib.Path(checkout, "todo.md").write_text("# todo\n") pathlib.Path(checkout, "todo.md").write_text("# todo\n")
self.config = configured(self, pathlib.Path(checkout)) / "config.toml" self.config = configured(self, pathlib.Path(checkout)) / "config.toml"
self.home = self.config.parents[2]
return checkout return checkout
def test_a_changed_plan_that_cannot_be_read_is_a_breach(self): 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.addCleanup(pathlib.Path(checkout, "todo.md").chmod, 0o600)
self.assertEqual(["Permission denied" in detail for detail in self.details(checkout)], [True]) self.assertEqual(["Permission denied" in detail for detail in self.details(checkout)], [True])
def details(self, checkout: str) -> list[str]: def details(self, checkout: str, **limit: float) -> list[str]:
return [breach.detail for breach in todo_breaches(checkout, pathlib.Path.home(), pathlib.Path(self.own))] 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): def test_a_checkout_of_the_hooks_repo_runs_its_own_rank_todo(self):
checkout = self.changed_plan(f"{self.folder}/clone", self.STUB) checkout = self.changed_plan(f"{self.folder}/clone", self.STUB)
self.assertEqual(self.details(checkout), [f"own script checked {checkout}/todo.md"]) self.assertEqual(self.details(checkout), [f"own script checked {checkout}/todo.md"])
top = str(pathlib.Path(checkout).resolve()) 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.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): def test_its_own_rank_todo_passing_passes(self):
self.assertEqual(self.details(self.changed_plan(f"{self.folder}/clone", "")), []) 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): 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") 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, seconds=0.2), [f"{checkout}/bin/rank_todo.py --check did not finish in 0.2 seconds"])
self.assertEqual(self.details(checkout), [f"{checkout}/bin/rank_todo.py --check did not finish in 0.2 seconds"])
def test_another_repos_rank_todo_never_runs(self): def test_another_repos_rank_todo_never_runs(self):
reasons = self.details(self.changed_plan(self.other, self.STUB)) 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.write_text(self.config.read_text().replace('"checkout:todo.md"', '"folder:todo.md"'))
(self.config.parent / "todo.md").write_text("# todo\n") (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(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, "add", "-A")
git(folder, "commit", "-q", "-m", "plan") git(folder, "commit", "-q", "-m", "plan")
self.assertEqual(self.details(folder), [], "committed, so unchanged") self.assertEqual(self.details(folder), [], "committed, so unchanged")
+8 -8
View File
@@ -19,14 +19,14 @@ OWN = pathlib.Path(__file__).resolve().parents[2]
OWN_CHECK_SECONDS = 20 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.""" """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") top = guarded_git(directory, "rev-parse", "--show-toplevel")
if isinstance(top, Failure): if isinstance(top, Failure):
return [] return []
held_here = held(pathlib.Path(top), home) held_here = held(pathlib.Path(top), home)
if held_here is not None: 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)) config = located(top, home, functools.partial(git, guarded=True))
if isinstance(config, Failure): 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. # 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. # 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): if tracked is not None and not changed(top, todo, tracked):
return [] 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. """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.""" 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 [] return []
if not todo.is_file() or not changed(str(config.folder), todo, todo.relative_to(config.folder).as_posix()): if not todo.is_file() or not changed(str(config.folder), todo, todo.relative_to(config.folder).as_posix()):
return [] 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.""" """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" script = pathlib.Path(top) / "bin" / "rank_todo.py"
if not script.is_file() or not same_repo(top, own): if not script.is_file() or not same_repo(top, own):
return file_plan(config, todo).check() return file_plan(config, todo).check()
try: 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: 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: except OSError as failure:
return [f"{script} --check could not run: {failure}"] return [f"{script} --check could not run: {failure}"]
if ran.returncode == 0: if ran.returncode == 0:
+1 -1
View File
@@ -6,4 +6,4 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
from run_tests import main from run_tests import main
sys.exit(main.main(sys.argv[1:])) sys.exit(main.main(sys.argv[1:], main.run))

Some files were not shown because too many files have changed in this diff Show More