From fffba3d4540ab7c74ec9f2e480d2fc477d4efab3 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sun, 15 Nov 2026 23:06:50 +0100 Subject: [PATCH] =?UTF-8?q?Answer=20stability=20round=201:=200=20?= =?UTF-8?q?=F0=9F=94=B4,=201=20=F0=9F=9F=A1=20(0=20misread);=20prune=20the?= =?UTF-8?q?=20copies=20of=20removed=20worktrees?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/project_record/main.py | 27 +++++++++--- bin/project_record/test_project_record.py | 52 ++++++++++++++++++++--- docs/decisions.md | 2 +- 3 files changed, 70 insertions(+), 11 deletions(-) diff --git a/bin/project_record/main.py b/bin/project_record/main.py index 733f406d..9fe67444 100644 --- a/bin/project_record/main.py +++ b/bin/project_record/main.py @@ -10,9 +10,10 @@ save refuses them: edit the epic in Jira. open prints the path of a file holding the record's text, and nothing else. Read and edit that file. Where the record is kept as a file, the path is that file. Where it is kept elsewhere, the -path is a copy under ~/.local/state/project-record///, a folder of the worktree -that ran open, so two worktrees of one project don't share a copy. The copy is written afresh with -the record as it stands, so open it again before each edit. A record that doesn't exist yet has no file at the path: +path is a copy under ~/.local/state/project-record///, a folder named for the +worktree that ran open, so two worktrees of one project don't share a copy. The copy is written +afresh with the record as it stands, so open it again before each edit. open also removes the +folders of worktrees git no longer lists. A record that doesn't exist yet has no file at the path: create it there. save stores the file's text in the record, and prints one line saying so. Where the record is kept @@ -27,10 +28,12 @@ Exit 2 on bad arguments, and 1, saying why, on any other failure. """ import hashlib import pathlib +import shutil import sys from failure import Failure from record_store.stores import FileStore, goals_store, local_file, RECORDS, RecordStore, store, stored +from git_cli.git_repo import git, Git from project_config.main import located from project_config.settings import ProjectConfig @@ -51,6 +54,8 @@ def main(argv: list[str]) -> int: return 2 home = pathlib.Path.home() config = located(None, home) + if not isinstance(config, Failure) and argv[0] == "open": + pruned(config, home) done = config if isinstance(config, Failure) else act(argv[0], *opened(config, argv[1], home)) if isinstance(done, Failure): print(f"project-record: {done.reason}", file=sys.stderr) @@ -60,16 +65,28 @@ def main(argv: list[str]) -> int: def opened(config: ProjectConfig, name: str, home: pathlib.Path) -> tuple[RecordStore, pathlib.Path]: - """The store of the record called name, and the copy it gets where it isn't kept as a file. A store that keeps the version it read, for a later save, keeps it beside the copy. The copy is the worktree's own, so two chunks of one project don't overwrite each other's unsaved edit.""" + """The store of the record called name, and the copy it gets where it isn't kept as a file. A store that keeps the version it read, for a later save, keeps it beside the copy.""" copy = home / COPIES / config.key / worktree_folder(config.top) / f"{name}.md" return (goals_store(config) if name == GOALS else store(config, name, copy.with_suffix(".version"))), copy def worktree_folder(top: pathlib.Path) -> str: - """The folder name of the worktree at top: its own name, for a reader, and a hash of its path, since two worktrees in different folders may share a name.""" + """The name of the folder that holds the copies of the worktree at top: the worktree's own name, for a reader, and a hash of its path, since two worktrees in different folders may share a name.""" return f"{top.name}-{hashlib.sha256(str(top).encode()).hexdigest()[:8]}" +def pruned(config: ProjectConfig, home: pathlib.Path, run: Git = git) -> None: + """Remove the copies of the worktrees git no longer lists, so the folders don't pile up one per chunk. Best effort: a folder that stays only takes disk, and where git can't list the worktrees nothing is removed.""" + listed = run("worktree", "list", "--porcelain") + folders = home / COPIES / config.key + if isinstance(listed, Failure) or not folders.is_dir(): + return + worktrees = [config.top, *(pathlib.Path(line.removeprefix("worktree ")) for line in listed.splitlines() if line.startswith("worktree "))] + kept = {worktree_folder(worktree) for worktree in worktrees} + for stale in (folder for folder in folders.iterdir() if folder.is_dir() and folder.name not in kept): + shutil.rmtree(stale, ignore_errors=True) + + def act(command: str, record: RecordStore, copy: pathlib.Path) -> str | Failure: """What command, one of COMMANDS, prints for record. copy is where a record kept outside files gets its file.""" if command == "open": diff --git a/bin/project_record/test_project_record.py b/bin/project_record/test_project_record.py index 2f3c3230..3cbf55aa 100644 --- a/bin/project_record/test_project_record.py +++ b/bin/project_record/test_project_record.py @@ -37,19 +37,61 @@ class Opened(unittest.TestCase): record, copy = main.opened(worktree_config("/code/acme"), "goals-and-audience", pathlib.Path("/home/me")) self.assertEqual((record.label, copy.parent.parent, copy.name), ("/code/acme/README.md", COPIES, "goals-and-audience.md")) - def test_two_worktrees_of_one_project_get_copies_that_differ_and_one_worktree_always_the_same(self): + def test_two_worktrees_of_one_project_get_copies_that_differ(self): home = pathlib.Path("/home/me") - main_checkout, other = (main.opened(worktree_config(top), "goals-and-audience", home)[1] for top in ("/code/acme", "/code/acme-item-5")) - again = main.opened(worktree_config("/code/acme"), "goals-and-audience", home)[1] + main_checkout = main.opened(worktree_config("/code/acme"), "goals-and-audience", home)[1] + other = main.opened(worktree_config("/code/acme-item-5"), "goals-and-audience", home)[1] self.assertNotEqual(main_checkout, other) - self.assertEqual(main_checkout, again) + + def test_one_worktree_always_gets_the_same_copy(self): + home = pathlib.Path("/home/me") + first = main.opened(worktree_config("/code/acme"), "goals-and-audience", home)[1] + self.assertEqual(first, main.opened(worktree_config("/code/acme"), "goals-and-audience", home)[1]) def test_two_worktrees_named_alike_in_different_folders_get_copies_that_differ(self): home = pathlib.Path("/home/me") - first, second = (main.opened(worktree_config(top), "goals-and-audience", home)[1] for top in ("/code/acme-item-5", "/other/acme-item-5")) + first = main.opened(worktree_config("/code/acme-item-5"), "goals-and-audience", home)[1] + second = main.opened(worktree_config("/other/acme-item-5"), "goals-and-audience", home)[1] self.assertNotEqual(first, second) +class Pruned(unittest.TestCase): + def setUp(self) -> None: + self.home = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) + self.config = worktree_config("/code/acme-item-5") + self.folders = self.home / ".local/state/project-record/acme" + for top in ("/code/acme", "/code/acme-item-5", "/code/acme-item-6"): + copy = main.opened(worktree_config(top), "goals-and-audience", self.home)[1] + copy.parent.mkdir(parents=True) + copy.write_text("edit") + + def kept(self) -> set[str]: + return {folder.name for folder in self.folders.iterdir()} + + def listed(self, *tops: str): + return lambda *args, **kwargs: "".join(f"worktree {top}\nHEAD abc\nbranch refs/heads/x\n\n" for top in tops) + + def test_removes_the_copies_of_worktrees_git_no_longer_lists_and_keeps_the_others(self): + main.pruned(self.config, self.home, self.listed("/code/acme", "/code/acme-item-5")) + self.assertEqual(self.kept(), {main.worktree_folder(pathlib.Path(top)) for top in ("/code/acme", "/code/acme-item-5")}) + + def test_always_keeps_the_copy_of_the_worktree_that_asked(self): + main.pruned(self.config, self.home, self.listed("/code/acme")) + self.assertIn(main.worktree_folder(pathlib.Path("/code/acme-item-5")), self.kept()) + + def test_removes_nothing_where_git_cannot_list_the_worktrees(self): + main.pruned(self.config, self.home, lambda *args, **kwargs: Failure("not a repository")) + self.assertEqual(len(self.kept()), 3) + + def test_leaves_files_beside_the_folders_alone(self): + (self.folders / "decisions.md").write_text("from before the folders") + main.pruned(self.config, self.home, self.listed("/code/acme")) + self.assertTrue((self.folders / "decisions.md").exists()) + + def test_does_nothing_where_the_project_has_no_copies(self): + main.pruned(worktree_config("/code/acme"), self.home / "elsewhere", self.listed("/code/acme")) + + class Act(unittest.TestCase): def setUp(self) -> None: self.copy = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory()), "acme", "decisions.md") diff --git a/docs/decisions.md b/docs/decisions.md index 35076569..ce810584 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -299,7 +299,7 @@ Valid while the later fixes per finished chunk, which `score-outcomes` and the r - **A bench of readers picked the spelling.** Three fresh readers, seated as the person who writes a client's `config.toml`, each picked `decisions = "confluence:https:///wiki/spaces//pages/"`, pasted from the browser with or without the title at its end, over the bare address, a page ID beside a `confluence-site` key, and a table. Each named the match with `jira:`. Any of the five records may name a page; the goals file, the plan, the working rules and the principles may not. - **`decisions` and `comprehension-history` may name a page only where `plan` names a Jira epic.** rule-hook ranks a plan kept as a file before tool calls, inside its 30-second limit, and ranking reads those two records. A Confluence read and two adf-codec container runs would hold up every guarded tool call, and fail it while Confluence is down. `load` refuses goals in Jira beside a plan in a file for the same reason. - **A write sends the version number the store read, plus one, and Confluence refuses it with 409 where someone saved the page since.** The store then fails, saying to ask the human, and doesn't write the page; the loop never overwrites a teammate's edit on its own (lilleman, 2026-11-12). A write with no read before it replaces the page as it stands, as the record contract asks. -- **`project-record` keeps the version `open` read in a file beside its copy**, `~/.local/state/project-record///.version`, since `save` runs in a later process. The store keeps the version itself, so `write` still takes only the text. Every other tool reads and writes a record in one process, and its store keeps the version in memory, so a read by another tool, such as larv-review's, never moves the version an edit in `project-record` rests on. +- **`project-record` keeps the version `open` read in a file beside its copy**, `~/.local/state/project-record///.version`, since `save` runs in a later process. The store keeps the version itself, so `write` still takes only the text. Every other tool reads and writes a record in one process, and its store keeps the version in memory, so a read by another tool, such as larv-review's, never moves the version an edit in `project-record` rests on. The folder `` is named for the worktree that ran `open`, and `open` removes the folders of worktrees git no longer lists, so a chunk's copy doesn't outlive its worktree. - **The page holds ADF, which adf-codec converts both ways**, so a teammate reads the record as Confluence renders it. Where adf-codec cannot convert the markdown, the write fails with adf-codec's error. A node markdown cannot hold reads as text: a mention as its name, a macro as `_( not included)_`. So the next write replaces it with that text, and a record page should hold only what markdown can. ## project config: a tool that reads a file's commits says where it does not support that file, and the work goes on