diff --git a/CHANGELOG.md b/CHANGELOG.md index 62094079..b69105ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,16 @@ explicitly under **Changed**, with what to do about it. ## [Unreleased] +### Fixed + +- `.brain/` (a vault's local search index) can no longer enter a vault's git history. + The cycle and the agents' `vault-sync` now exclude it by name instead of trusting + `.gitignore`, and untrack it if it was ever committed. After a hard reboot left the + vaults' `.gitignore` files empty, both sides committed the 118 MB index every run: + the brain disk filled and every agent sync failed. +- A manifest rewrite that is cut short (for example on a full disk) now leaves the old + manifest whole. It is written to a temp file and renamed into place. + ## [0.9.1] - 2026-10-30 ### Fixed diff --git a/deploy/agents-box/scripts/vault-sync b/deploy/agents-box/scripts/vault-sync index c5a1a769..788e7d32 100755 --- a/deploy/agents-box/scripts/vault-sync +++ b/deploy/agents-box/scripts/vault-sync @@ -30,8 +30,16 @@ fi # --- steady state ------------------------------------------------------------- # Commit anything the agent left uncommitted so it can never block the pull. -if [ -n "$(git -C "$V" status --porcelain)" ]; then - git -C "$V" add -A +# Except .brain/, the live search index: never committed, whatever .gitignore +# says, and untracked if a crash ever let it in (2026-10-29: an emptied +# .gitignore got the 118 MB index tracked, and since `brain index` rewrites it +# between this commit and the pull, every pull then failed). +if [ -n "$(git -C "$V" ls-files .brain)" ]; then + git -C "$V" rm -r -q --cached .brain +fi +if [ -n "$(git -C "$V" status --porcelain -- . ':(exclude).brain')" ] \ + || ! git -C "$V" diff --cached --quiet; then + git -C "$V" add -A -- . ':(exclude).brain' git -C "$V" commit -q -m "agent: sync $(date -u +%FT%TZ)" || true fi diff --git a/src/brain/compiler.py b/src/brain/compiler.py index 8e6769db..85180abf 100644 --- a/src/brain/compiler.py +++ b/src/brain/compiler.py @@ -41,8 +41,19 @@ def write_manifest(path: Path, manifest: dict) -> None: """Serialize a vault manifest the one way every writer must agree on, so a rewrite (e.g. clearing the busy key) reproduces the committed bytes - exactly.""" - path.write_text(json.dumps(manifest, indent=2)) + exactly. + + Written to a temp file and renamed into place: the cycle rewrites a live + manifest in place to set and clear the busy key, and a write cut short + (a full disk, 2026-10-30) must leave the old manifest whole, not a + truncated one every reader then rejects.""" + tmp = path.with_name(f".{path.name}.tmp") + try: + tmp.write_text(json.dumps(manifest, indent=2)) + os.replace(tmp, path) + except BaseException: + tmp.unlink(missing_ok=True) + raise # A person's hold record (People//.held.json): server-side bookkeeping diff --git a/src/brain/holds.py b/src/brain/holds.py index ae3cca07..49dc2d4a 100644 --- a/src/brain/holds.py +++ b/src/brain/holds.py @@ -52,15 +52,27 @@ def vault_head(vault: Path) -> str | None: return None +# The vault's local search index. Ignored by the vault's .gitignore, but that +# file is not enough: a hard reboot (2026-10-29) left it empty, the next +# `add -A` tracked the 118 MB index, and every cycle then committed a fresh +# copy until the disk filled. So it is excluded here by name, and untracked if +# a crash ever let it in. +BRAIN_DIR = ".brain" + + def commit_vault_strays(vault: Path) -> bool: """Commit uncommitted edits in the vault repo so every held path is reachable by SHA. The manifest is excluded: during a cycle it carries - the busy marker, which must stay uncommitted.""" + the busy marker, which must stay uncommitted. `.brain/` never enters + history (see BRAIN_DIR).""" if not (vault / ".git").exists(): return False - spec = ("--", ".", f":(exclude){MANIFEST_NAME}") - if not _git(vault, "status", "--porcelain", *spec).stdout.strip(): + spec = ("--", ".", f":(exclude){MANIFEST_NAME}", f":(exclude){BRAIN_DIR}") + untrack = bool(_git(vault, "ls-files", BRAIN_DIR).stdout.strip()) + if not untrack and not _git(vault, "status", "--porcelain", *spec).stdout.strip(): return False + if untrack: + _git(vault, "rm", "-r", "-q", "--cached", BRAIN_DIR) _git(vault, "add", "-A", *spec) _git(vault, "-c", f"user.name={CYCLE_NAME}", "-c", f"user.email={CYCLE_EMAIL}", "commit", "-q", "-m", "cycle: keep uncommitted vault edits") diff --git a/tests/test_brain_dir_never_committed.py b/tests/test_brain_dir_never_committed.py new file mode 100644 index 00000000..60bff45b --- /dev/null +++ b/tests/test_brain_dir_never_committed.py @@ -0,0 +1,106 @@ +"""`.brain/` (the search index) must never enter a vault's git history. + +Live-found 2026-10-30: a hard reboot of embark-brain left every vault's +`.gitignore` empty, the next `git add -A` tracked the 118 MB `index.db`, and +from then on the cycle and every agent's vault-sync committed a fresh copy each +run: the brain disk filled, three manifests were cut off mid-write, and every +agent sync failed (the live index changes between the commit and the pull). +Nothing may depend on `.gitignore` for this. +""" +import json +import os +import subprocess +from pathlib import Path + +import pytest + +from brain.compiler import write_manifest +from brain.holds import commit_vault_strays + +SYNC = Path(__file__).resolve().parents[1] / "deploy/agents-box/scripts/vault-sync" + + +def _g(repo, *args): + return subprocess.run(["git", "-C", str(repo), *args], capture_output=True, + text=True, check=True).stdout + + +def _repo(path: Path) -> Path: + path.mkdir() + _g(path, "init", "-q", "-b", "main") + _g(path, "config", "user.name", "t") + _g(path, "config", "user.email", "t@t") + (path / "a.md").write_text("a\n") + _g(path, "add", "-A") + _g(path, "commit", "-qm", "seed") + return path + + +def test_the_cycle_never_commits_brain_dir_even_without_a_gitignore(tmp_path): + vault = _repo(tmp_path / "v") + (vault / ".brain").mkdir() + (vault / ".brain/index.db").write_bytes(b"index") + (vault / "note.md").write_text("edit\n") + assert commit_vault_strays(vault) + assert _g(vault, "ls-files").split() == ["a.md", "note.md"] + + +def test_the_cycle_untracks_a_brain_dir_a_crash_already_committed(tmp_path): + vault = _repo(tmp_path / "v") + (vault / ".brain").mkdir() + (vault / ".brain/index.db").write_bytes(b"v1") + _g(vault, "add", "-A") + _g(vault, "commit", "-qm", "the bad commit") + (vault / ".brain/index.db").write_bytes(b"v2") + commit_vault_strays(vault) + assert ".brain/index.db" not in _g(vault, "ls-files") + assert (vault / ".brain/index.db").read_bytes() == b"v2" # the live index stays + + +def test_a_manifest_rewrite_cut_short_leaves_the_old_manifest_whole(tmp_path, monkeypatch): + path = tmp_path / ".brain-manifest.json" + write_manifest(path, {"compiled": "x", "files": ["a"] * 50}) + before = path.read_bytes() + real = Path.write_text + + def disk_full(self, data, *a, **k): + real(self, data[: len(data) // 2], *a, **k) + raise OSError(28, "No space left on device") + + monkeypatch.setattr(Path, "write_text", disk_full) + with pytest.raises(OSError): + write_manifest(path, {"compiled": "y", "files": ["b"] * 50}) + assert path.read_bytes() == before + json.loads(before) + + +def test_vault_sync_never_commits_brain_dir_and_untracks_a_committed_one(tmp_path): + server = _repo(tmp_path / "server") + (server / ".brain-manifest.json").write_text("{}") + _g(server, "add", "-A") + _g(server, "commit", "-qm", "manifest") + _g(server, "config", "receive.denyCurrentBranch", "updateInstead") + vault = tmp_path / "vault" + subprocess.run(["git", "clone", "-q", str(server), str(vault)], check=True) + _g(vault, "config", "user.name", "bob (agent)") + _g(vault, "config", "user.email", "bob@agents.brain.local") + # The crash: an index already committed in the agent's clone. + (vault / ".brain").mkdir() + (vault / ".brain/index.db").write_bytes(b"v1") + _g(vault, "add", "-A") + _g(vault, "commit", "-qm", "agent: sync (bad)") + bindir = tmp_path / "bin" + bindir.mkdir() + # The agent's own `brain index` rewrites the live index on every run. + (bindir / "brain").write_text(f'#!/bin/sh\necho v2 > "{vault}/.brain/index.db"\n') + (bindir / "brain").chmod(0o755) + env = {**os.environ, "BRAIN_VAULT_DIR": str(vault), "PATH": f"{bindir}:{os.environ['PATH']}"} + + (vault / "note.md").write_text("agent work\n") + for _ in range(2): + r = subprocess.run(["sh", str(SYNC)], capture_output=True, text=True, env=env) + assert "pull failed" not in r.stderr and "push refused" not in r.stderr, r.stderr + assert ".brain/" not in _g(vault, "ls-files") + assert ".brain/" not in _g(server, "ls-files") + assert (server / "note.md").read_text() == "agent work\n" + assert (vault / ".brain/index.db").exists()