diff --git a/bin/build_release/loop/chunk_windows.py b/bin/build_release/loop/chunk_windows.py index ac412b9d..42ee5942 100644 --- a/bin/build_release/loop/chunk_windows.py +++ b/bin/build_release/loop/chunk_windows.py @@ -5,7 +5,7 @@ import re import typing from touch_predictions.beside import Beside, chunks_beside -from chunk_driver.claims import CHUNK_FLAG, epic_chunks, HANDED, loop_session, project_lock, starting +from chunk_driver.claims import CHUNK_FLAG, epic_chunks, handed, loop_session, project_lock, starting from build_release.chunk import chunk_items from git_cli.git_repo import default_ref from loop_effects.effects import Effects, say @@ -118,7 +118,6 @@ def start_chunks(loop_run: LoopRun, leaving: bool, paths: Paths, env: typing.Map if isinstance(config, Failure): return config session = loop_session(loop_run.root, paths.home) - handed = {name: value for name, value in env.items() if name.startswith(HANDED)} with starting(project_lock(loop_run.root, paths)) as unlocked: if unlocked: return unlocked @@ -129,7 +128,7 @@ def start_chunks(loop_run: LoopRun, leaving: bool, paths: Paths, env: typing.Map if isinstance(chunks, AwaitedMerges): return 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(env), [str(launcher), CHUNK_FLAG, chunk, loop_run.task], windows, True, effects.tmux) if isinstance(started, Failure): return started windows = started diff --git a/bin/chunk_driver/claims.py b/bin/chunk_driver/claims.py index 7be1b4ed..aad014c5 100644 --- a/bin/chunk_driver/claims.py +++ b/bin/chunk_driver/claims.py @@ -1,6 +1,6 @@ """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 build-release runs. -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. +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 `handed` picks. """ import contextlib import fcntl @@ -8,7 +8,7 @@ import pathlib import typing from failure import Failure -from loop_state.paths import Paths +from loop_state.paths import Paths, XDG_CONFIG_HOME, XDG_STATE_HOME from transcript_archive.project_key import project_key from agent_cycle import tmux @@ -16,8 +16,14 @@ from agent_cycle import tmux LOCK = "starting" # The flag that tells the entry point in a window its chunk, as `--chunk item-372` for build-release and `--chunk ABC-34 ABC-12` for do-epic. Nobody types it, so no panel picked it. CHUNK_FLAG = "--chunk" -# The prefix of the variables both entry points read. A window gets the tmux server's environment, not the starting terminal's, so they are handed to each window by name. -HANDED = "BUILD_RELEASE_" +# The prefix of the variables both entry points read. +OWN = "BUILD_RELEASE_" + + +# A window gets the tmux server's environment, not the starting terminal's. Without the two XDG variables, a window would lock and write in other folders than the entry point that started it. +def handed(env: typing.Mapping[str, str]) -> dict[str, str]: + """The variables of env that an entry point hands each window it starts: those both entry points read, and the two that move the config folder and the state folder.""" + return {name: value for name, value in env.items() if name.startswith(OWN) or name in (XDG_CONFIG_HOME, XDG_STATE_HOME)} @contextlib.contextmanager diff --git a/bin/chunk_driver/test_claims.py b/bin/chunk_driver/test_claims.py index 9ccb694d..b7346b04 100644 --- a/bin/chunk_driver/test_claims.py +++ b/bin/chunk_driver/test_claims.py @@ -6,7 +6,16 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from failure import Failure -from chunk_driver.claims import LOCK, starting +from chunk_driver.claims import handed, LOCK, starting + + +class Handed(unittest.TestCase): + def test_hands_a_window_the_setups_own_variables_and_the_two_that_move_its_folders(self) -> None: + env = {"BUILD_RELEASE_JUDGE_MODEL": "judge", "XDG_CONFIG_HOME": "/configs", "XDG_STATE_HOME": "/state", "XDG_DATA_HOME": "/data", "PATH": "/bin"} + self.assertEqual(handed(env), {"BUILD_RELEASE_JUDGE_MODEL": "judge", "XDG_CONFIG_HOME": "/configs", "XDG_STATE_HOME": "/state"}) + + def test_hands_no_variable_the_starter_lacks(self) -> None: + self.assertEqual(handed({"PATH": "/bin"}), {}) class Starting(unittest.TestCase): diff --git a/bin/do_epic/scheduler.py b/bin/do_epic/scheduler.py index 9713ae41..9887d118 100644 --- a/bin/do_epic/scheduler.py +++ b/bin/do_epic/scheduler.py @@ -6,7 +6,7 @@ import pathlib import typing from do_epic.epic_record import CAP -from chunk_driver.claims import CHUNK_FLAG, epic_chunks, HANDED, loop_chunks, project_lock, starting +from chunk_driver.claims import CHUNK_FLAG, epic_chunks, handed, loop_chunks, project_lock, starting from do_epic.backend.protocol import EpicIssue from failure import Failure from do_epic.split import finished, is_split, SPLIT_LABEL @@ -225,10 +225,10 @@ def start_chunks(epic: Epic, issues: list[EpicIssue], finishing: bool, own: str def start_chunk(epic: Epic, chunk: Chunk, own: str | None, windows: list[tmux.Listed], run: tmux.Tmux) -> list[tmux.Listed] | Waits | Failure: """Starts chunk's window beside windows, the epic's session's, and returns the session's windows afterwards; a Waits with the reason where its repo takes no chunk now, and a Failure where tmux started none, or where the repo's lock cannot be taken. It holds that lock from the check to the start. A chunk without a repo starts in the epic's folder. So does a parked chunk, since it takes no room in its repo.""" session = tmux.session_name(tmux.EPIC, epic.key) - handed = {**{name: value for name, value in epic.env.items() if name.startswith(HANDED)}, PARKS: "1"} + env = {**handed(epic.env), PARKS: "1"} command = [str(epic.launcher), CHUNK_FLAG, chunk.issue, epic.key] if chunk.repo is None: - return tmux.start_chunk(tmux.Window(session, chunk.issue, ""), str(epic.folder), handed, command, windows, False, run) + return tmux.start_chunk(tmux.Window(session, chunk.issue, ""), str(epic.folder), env, command, windows, False, run) located = epic.locate(chunk.repo) if isinstance(located, Failure): return Waits(chunk.issue, located.reason) @@ -238,7 +238,7 @@ def start_chunk(epic: Epic, chunk: Chunk, own: str | None, windows: list[tmux.Li return unlocked if reason := refusal(chunk.repo, room_in(str(checkout), limit, (session, own), epic.paths.home, run)): return Waits(chunk.issue, 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), env, command, windows, False, run) def room_in(checkout: str, limit: int, leaving: tuple[str, str | None], home: pathlib.Path, run: tmux.Tmux) -> RepoRoom: diff --git a/bin/notifier/test_notifier.py b/bin/notifier/test_notifier.py index 2cb4ade8..805c88c3 100644 --- a/bin/notifier/test_notifier.py +++ b/bin/notifier/test_notifier.py @@ -1,7 +1,6 @@ import contextlib import dataclasses import io -import os import pathlib import subprocess import sys @@ -74,14 +73,14 @@ class Notify(Project): self.name_notifier("slack", SLACK_KEYS) slack = Slack({"ok": True}) with contextlib.chdir(self.checkout): - self.assertIsNone(system_effects(self.home, os.environ, through("slack", slack)).dm("Which one?", None)) + self.assertIsNone(system_effects(self.home, {}, through("slack", slack)).dm("Which one?", None)) self.assertEqual(slack.requests, [SLACK_POST]) def test_a_dm_that_names_a_directory_goes_through_the_notifier_for_the_project_holding_it_from_any_current_directory(self): self.name_notifier("slack", SLACK_KEYS) slack = Slack({"ok": True}) with contextlib.chdir(self.home): - self.assertIsNone(system_effects(self.home, os.environ, through("slack", slack)).dm("Which one?", str(self.checkout))) + self.assertIsNone(system_effects(self.home, {}, through("slack", slack)).dm("Which one?", str(self.checkout))) self.assertEqual(slack.requests, [SLACK_POST]) def test_skips_the_notice_for_a_project_that_names_no_notifier_even_where_its_env_file_holds_the_keys(self): diff --git a/bin/project_record/test_project_record.py b/bin/project_record/test_project_record.py index 5090db2d..7d8779be 100644 --- a/bin/project_record/test_project_record.py +++ b/bin/project_record/test_project_record.py @@ -13,6 +13,7 @@ from failure import Failure from project_record import main from record_store.fakes import MemoryStore from project_config.settings import Page, ProjectConfig +from loop_state.paths import without_xdg_folders TOOL = pathlib.Path(__file__).resolve().parents[1] / "project-record" @@ -135,7 +136,7 @@ class BlackBox(unittest.TestCase): self.home = configured(self, self.top).parents[2] def run_tool(self, *args: str) -> subprocess.CompletedProcess[str]: - return subprocess.run([sys.executable, str(TOOL), *args], capture_output=True, cwd=self.top, env=os.environ | {"HOME": str(self.home)}, text=True) + return subprocess.run([sys.executable, str(TOOL), *args], capture_output=True, cwd=self.top, env=without_xdg_folders(os.environ) | {"HOME": str(self.home)}, text=True) def test_a_file_record_opens_as_its_own_file_and_save_has_nothing_to_store(self): opened = self.run_tool("open", "decisions") diff --git a/bin/rule_hook/test_rule_hook.py b/bin/rule_hook/test_rule_hook.py index 98f35529..805de0b8 100644 --- a/bin/rule_hook/test_rule_hook.py +++ b/bin/rule_hook/test_rule_hook.py @@ -413,6 +413,20 @@ class ReviewerRung(unittest.TestCase): self.assertEqual(reviewer_breaches(reviewer, environ, home), []) self.assertEqual(skill_breaches(Call("Skill", {"skill": "security-review"}, checkout), environ, home), []) + def test_the_projects_ladder_is_read_from_a_config_under_xdg_config_home(self): + checkout, home = self.project(self.TWO_RUNGS) + pathlib.Path(home, ".config").rename(pathlib.Path(home, "moved")) + moved = {"XDG_CONFIG_HOME": f"{home}/moved"} + reviewer = Call("Agent", {"subagent_type": "stability-reviewer", "model": "fable", "effort": "medium"}, checkout) + self.assertEqual(rules(reviewer_breaches(reviewer, self.ON_TWO_RUNGS | moved, home)), ["reviewer-rung"]) + self.assertEqual(rules(reviewer_breaches(reviewer, self.ON_TWO_RUNGS, home)), [], "without the variable the shipped ladder holds") + # On the shipped ladder the worker's high effort ranks above the project's low one; the project's ladder holds them the other way round. + pathlib.Path(home, "moved", "build-release", "acme", "routing-ladder.json").write_text('[{"model": "claude-fable-5-2", "effort": "high"}, {"model": "claude-fable-5-2", "effort": "low"}]') + environ = {"BUILD_RELEASE_PROJECT_RUNG": "claude-fable-5-2 at low effort", "BUILD_RELEASE_WORKER_RUNG": "claude-fable-5-2 at high effort"} + skill = Call("Skill", {"skill": "security-review"}, checkout) + self.assertEqual(rules(skill_breaches(skill, environ | moved, home)), ["reviewer-rung"]) + self.assertEqual(skill_breaches(skill, environ, home), []) + def test_a_security_skill_is_held_to_the_projects_rung_on_the_projects_own_ladder(self): checkout, home = self.project('[{"model": "claude-fable-5-2", "effort": "high"}, {"model": "claude-fable-5-2", "effort": "low"}]') # On this ladder the worker's low effort ranks above the project's high one; on the shipped ladder it ranks below. @@ -745,6 +759,13 @@ class ReviewerReport(unittest.TestCase): with self.subTest(path): self.assertEqual(self.breached("Write", path), ["reviewer-report"]) + def test_the_reports_folder_follows_xdg_state_home(self): + env = {"XDG_STATE_HOME": "/state"} + for path, expected in [("/state/build-release/larv-review/x/prose-round-1.md", []), ("/h/.local/state/build-release/larv-review/x/prose-round-1.md", ["reviewer-report"])]: + with self.subTest(path): + found = breaches_of(Call("Write", {"file_path": path, "content": "x"}, "/r", "prose-reviewer"), "/h", env) + self.assertEqual(rules(found[2]), expected) + def test_a_reviewer_edits_no_file_even_in_the_reports_folder(self): self.assertEqual(self.breached("Edit", "/h/.local/state/build-release/larv-review/x/prose-round-1.md"), ["reviewer-report"]) @@ -1233,6 +1254,21 @@ class Hook(unittest.TestCase): self.assertEqual(self.round_commit("PAY-7 - Answer prose round 1: 0 🔴, 0 🟡 (0 misread); x").returncode, 0) self.assertEqual(self.round_commit("Tidy # rule-override commit-title: the client asked for this title").returncode, 0) + def test_a_commit_is_checked_against_the_config_and_the_report_under_the_xdg_folders(self): + home = pathlib.Path(tempfile.mkdtemp()) + self.addCleanup(subprocess.run, ["rm", "-rf", str(home)]) + config = configured(self, pathlib.Path(self.repo), home=home) / "config.toml" + config.write_text(config.read_text() + 'commit-title = "PAY-{id} - {summary}"\n') + (home / ".config").rename(home / "configs") + git(self.repo, "checkout", "-q", "-b", "item-7") + folder = home / "state" / "build-release" / "larv-review" / self.repo.lstrip("/") + folder.mkdir(parents=True) + (folder / "stability-round-1.md").write_text("BLOCK\n\n### 🟡 `b.py:2`: worse\n") + self.env = {"HOME": str(home), "XDG_CONFIG_HOME": str(home / "configs"), "XDG_STATE_HOME": str(home / "state")} + self.assertIn("Blocked by rule commit-title: title the commit as the project's config asks, `PAY-{id} - {summary}`", self.round_commit("Tidy").stderr) + self.assertIn("Blocked by rule round-title-counts", self.round_commit("PAY-7 - Answer stability round 1: 0 🔴, 0 🟡 (0 misread); x").stderr) + self.assertEqual(self.round_commit("PAY-7 - Answer stability round 1: 0 🔴, 1 🟡 (0 misread); x").returncode, 0) + def test_a_commit_on_a_branch_that_doesnt_fit_the_configs_branch_template_takes_a_bare_title(self): config = self.configured() / "config.toml" config.write_text(config.read_text() + 'commit-title = "PAY-{id} - {summary}"\nbranch = "feature/PAY-{id}"\n') @@ -1381,6 +1417,22 @@ class TodoRankCheck(unittest.TestCase): self.assertTrue(reasons) self.assertNotIn("own script", " ".join(reasons)) + def test_a_config_under_xdg_config_home_names_the_checkout_and_holds_its_plan(self): + checkout = self.changed_plan(f"{self.folder}/clone", self.STUB) + moved = self.home / "moved" + (self.home / ".config").rename(moved) + env = {"XDG_CONFIG_HOME": str(moved)} + + def details(directory: str) -> list[str]: + return [breach.detail for breach in todo_breaches(directory, self.home, env, pathlib.Path(self.own))] + + self.assertEqual(details(checkout), [f"own script checked {checkout}/todo.md"]) + config = moved / self.config.relative_to(self.home / ".config") + config.write_text(config.read_text().replace('"checkout:todo.md"', '"folder:todo.md"')) + (config.parent / "todo.md").write_text("# todo\n") + git(str(config.parent), "init", "-q", "-b", "main") + self.assertEqual(details(str(config.parent)), [f"own script checked {config.parent}/todo.md"], "a commit in the config's folder checks the plan it holds") + def test_a_plan_the_config_puts_outside_the_checkout_is_checked_in_place_of_the_checkouts_todo_md(self): checkout = self.changed_plan(f"{self.folder}/clone", self.STUB) self.config.write_text(self.config.read_text().replace('"checkout:todo.md"', '"folder:todo.md"'))