fix(orchestrator): approve only a plain commit of the exact staged index

The approval hook binds a hub approval to a plain git commit of the staged index only; commits that add files from the working tree, amend, take a pathspec, run in a chain or inside a shell wrapper fall back to the normal dialog. Every git call of the hook runs with an environment allowlist. Tests run once on unify/skills-20261114 after the merge (operator order 2026-11-14).

🎯 Fix: 93 %
💥 Break: 3 %
💸 Tokens: –
⏱️ Velocity: –
🛡️ Security: 100 %
🏆 Score: 90
🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 %

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-11-14 21:43:06 +00:00
1 parent a2a03995cb
commit 4ec1a43238
8 files changed
+732 -303

No files matched your search

+509
View File
@@ -0,0 +1,509 @@
from __future__ import annotations
import hashlib
import importlib
import json
import os
import shutil
import subprocess
import sys
import tempfile
import unittest
from collections.abc import Callable
from pathlib import Path
from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
CLIENT = ROOT / "orchestrator" / "client"
REMINDER = ROOT / "skills" / "kvb-dream" / "hooks" / "reminder.sh"
ORCHESTRATOR = ROOT / "scripts" / "orchestrator.sh"
sys.path[:0] = [str(CLIENT)]
staged_diff = importlib.import_module("staged_diff")
permission_hook = importlib.import_module("permission_hook")
passkey = importlib.import_module("passkey")
sandbox_paths = importlib.import_module("sandbox_paths")
GIT_ENV = {
"GIT_AUTHOR_NAME": "t", "GIT_AUTHOR_EMAIL": "t@example.invalid", "GIT_COMMITTER_NAME": "t", "GIT_COMMITTER_EMAIL": "t@example.invalid",
"GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_NOSYSTEM": "1",
}
ALLOW = {"behavior": "allow"}
def git(cwd: Path, *args: str) -> str:
return subprocess.run(["git", *args], cwd=cwd, env={**os.environ, **GIT_ENV}, check=True, capture_output=True, text=True).stdout
def probe(path: Path, marker: Path, tail: str) -> Path:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(f"#!/bin/sh\n: > '{marker}'\n{tail}\n", encoding="utf-8")
path.chmod(0o755)
return path
class Scratch(unittest.TestCase):
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.root = Path(os.path.realpath(tmp.name))
self.marker = self.root / "planted-code-ran"
class Isolated(Scratch):
def setUp(self):
super().setUp()
for patch in (
mock.patch.object(staged_diff, "CACHE", self.root / "cache" / "secret-rules.json"),
mock.patch.object(sandbox_paths, "MANAGED", self.root / "managed"),
mock.patch.dict(os.environ, {**GIT_ENV, "HOME": str(self.root), "CLAUDE_CONFIG_DIR": str(self.root / ".claude"), "CLAUDE_PROJECT_DIR": str(self.root / "project")}),
):
patch.start()
self.addCleanup(patch.stop)
class TestPermissionHookGit(Isolated):
def setUp(self):
super().setUp()
self.repo = self.root / "repo"
self.repo.mkdir()
git(self.repo, "init", "-q", "-b", "main")
(self.repo / "base.txt").write_text("one\n", encoding="utf-8")
git(self.repo, "add", "-A")
git(self.repo, "commit", "-q", "-m", "base")
(self.repo / "base.txt").write_text("two\n", encoding="utf-8")
(self.repo / "added.txt").write_text("fresh\n", encoding="utf-8")
git(self.repo, "add", "-A")
self.ask = {"tool": "Bash", "command": "git commit -m x", "cwd": str(self.repo), "session": "s", "agent": ""}
def hook_path(self) -> tuple[str | None, dict, dict | None]:
pinned, shown = permission_hook.commit_diff(self.ask, None)
return pinned, shown, permission_hook.bound(ALLOW, self.ask, pinned)
def assert_clean_run(self) -> None:
_, shown, chosen = self.hook_path()
self.assertEqual(([entry["path"] for entry in shown["diff"]], chosen), (["added.txt", "base.txt"], ALLOW))
self.assertFalse(self.marker.exists())
def test_a_planted_post_index_change_hook_never_runs(self):
probe(self.repo / ".git" / "hooks" / "post-index-change", self.marker, "exit 0")
self.assert_clean_run()
def test_a_planted_core_hooks_path_never_runs(self):
hooks = self.root / "planted-hooks"
for name in ("post-index-change", "pre-commit", "reference-transaction"):
probe(hooks / name, self.marker, "exit 0")
git(self.repo, "config", "core.hooksPath", str(hooks))
self.assert_clean_run()
def test_a_planted_git_earlier_on_path_never_runs(self):
planted = probe(self.root / "bin" / "git", self.marker, 'exec /usr/bin/git "$@"')
with mock.patch.dict(os.environ, {"PATH": f"{planted.parent}{os.pathsep}{os.environ['PATH']}"}):
self.assert_clean_run()
def test_the_hook_path_leaves_the_index_file_untouched(self):
index = self.repo / ".git" / "index"
before = (index.read_bytes(), index.stat().st_mtime_ns)
self.hook_path()
self.assertEqual((index.read_bytes(), index.stat().st_mtime_ns), before)
def test_the_binding_changes_with_the_staged_tree_and_only_with_it(self):
pinned = staged_diff.binding(self.repo)
self.assertEqual(staged_diff.bound_diff(self.repo, pinned, None), staged_diff.review_diff(self.repo, None))
(self.repo / "added.txt").write_text("unstaged\n", encoding="utf-8")
git(self.repo, "status", "--porcelain")
self.assertEqual(staged_diff.binding(self.repo), pinned)
git(self.repo, "add", "added.txt")
content = staged_diff.binding(self.repo)
self.assertNotEqual(content, pinned)
with self.assertRaises(staged_diff.DiffError):
staged_diff.bound_diff(self.repo, pinned, None)
(self.repo / "added.txt").chmod(0o755)
git(self.repo, "add", "added.txt")
self.assertNotIn(staged_diff.binding(self.repo), (pinned, content))
(self.repo / "added.txt").write_text("fresh\n", encoding="utf-8")
(self.repo / "added.txt").chmod(0o644)
git(self.repo, "add", "added.txt")
self.assertEqual(staged_diff.binding(self.repo), pinned)
def test_an_index_with_unmerged_paths_has_no_binding(self):
oid = git(self.repo, "hash-object", "-w", "base.txt").strip()
entries = f"0 {'0' * len(oid)}\tbase.txt\n100644 {oid} 1\tbase.txt\n100644 {oid} 2\tbase.txt\n"
subprocess.run(["git", "update-index", "--index-info"], cwd=self.repo, input=entries, env={**os.environ, **GIT_ENV}, check=True, capture_output=True, text=True)
with self.assertRaises(staged_diff.DiffError):
staged_diff.binding(self.repo)
def stage_a(repo: Path) -> None:
(repo / "a.txt").write_text("two\n", encoding="utf-8")
git(repo, "add", "a.txt")
def restage_a(repo: Path) -> None:
(repo / "a.txt").write_text("three\n", encoding="utf-8")
git(repo, "add", "a.txt")
def chmod_a(repo: Path) -> None:
(repo / "a.txt").chmod(0o755)
git(repo, "add", "a.txt")
def intend_new(repo: Path) -> None:
(repo / "new.txt").write_text("x\n", encoding="utf-8")
git(repo, "add", "-N", "new.txt")
def swap_empty_to_intent(repo: Path) -> None:
git(repo, "rm", "-q", "--cached", "empty.txt")
git(repo, "add", "-N", "empty.txt")
def stage_a_and_intend_empty(repo: Path) -> None:
stage_a(repo)
(repo / "fresh_empty.txt").write_text("", encoding="utf-8")
git(repo, "add", "-N", "fresh_empty.txt")
def stage_intended_empty(repo: Path) -> None:
git(repo, "add", "fresh_empty.txt")
def add_gitlink(repo: Path) -> None:
git(repo, "update-index", "--add", "--cacheinfo", f"160000,{git(repo, 'rev-parse', 'HEAD').strip()},mod")
def skip_a(repo: Path) -> None:
git(repo, "update-index", "--skip-worktree", "a.txt")
def refresh(repo: Path) -> None:
os.utime(repo / "a.txt", ns=(1, 1))
git(repo, "status", "--porcelain")
def sparse(repo: Path) -> None:
git(repo, "sparse-checkout", "init", "--cone", "--sparse-index")
git(repo, "sparse-checkout", "set", "sub")
def split_v4(repo: Path) -> None:
git(repo, "update-index", "--index-version", "4")
git(repo, "update-index", "--split-index")
def uncache_c(repo: Path) -> None:
git(repo, "rm", "-q", "--cached", "other/c.txt")
def odd_names(repo: Path) -> None:
(repo / "tab\tname.txt").write_text("t\n", encoding="utf-8")
(repo / "new\nline.txt").write_text("n\n", encoding="utf-8")
git(repo, "add", "-A")
def to_symlink(repo: Path) -> None:
(repo / "a.txt").unlink()
(repo / "a.txt").symlink_to("other/c.txt")
git(repo, "add", "a.txt")
CASES = {
"content": (stage_a, restage_a),
"mode only": (stage_a, chmod_a),
"intent-to-add new file": (stage_a, intend_new),
"tracked empty file swapped to intent-to-add": (stage_a, swap_empty_to_intent),
"intent-to-add empty file staged for real": (stage_a_and_intend_empty, stage_intended_empty),
"gitlink added": (stage_a, add_gitlink),
"skip-worktree flag": (stage_a, skip_a),
"stat refresh only": (stage_a, refresh),
"sparse index": (stage_a, sparse),
"index v4 and split index": (stage_a, split_v4),
"rm --cached": (stage_a, uncache_c),
"tab and newline names": (stage_a, odd_names),
"file to symlink": (stage_a, to_symlink),
}
def hide_staged_b(flag: str) -> Callable[[Path], None]:
def hide(repo: Path) -> None:
(repo / "sub" / "b.txt").write_text("evil\n", encoding="utf-8")
git(repo, "add", "sub/b.txt")
(repo / "sub" / "b.txt").write_text("b\n", encoding="utf-8")
git(repo, "update-index", flag, "sub/b.txt")
return hide
def flag_after(change: Callable[[Path], None], path: str, flag: str) -> Callable[[Path], None]:
def flagged(repo: Path) -> None:
change(repo)
git(repo, "update-index", flag, path)
return flagged
def sparse_no_cone(repo: Path) -> None:
git(repo, "sparse-checkout", "set", "--no-cone", "/sub/")
UNREAD_CASES = {
"staged change hidden by assume-unchanged": hide_staged_b("--assume-unchanged"),
"staged change hidden by skip-worktree": hide_staged_b("--skip-worktree"),
"sparse index in cone mode": sparse,
"sparse checkout without cone": sparse_no_cone,
"intent-to-add new file with skip-worktree": flag_after(intend_new, "new.txt", "--skip-worktree"),
"intent-to-add new file with assume-unchanged": flag_after(intend_new, "new.txt", "--assume-unchanged"),
"tracked empty file swapped to intent-to-add with skip-worktree": flag_after(swap_empty_to_intent, "empty.txt", "--skip-worktree"),
}
class TestDebugLayout(unittest.TestCase):
def test_an_unexpected_ls_files_debug_layout_fails_closed_with_its_own_message(self):
entry = b"100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0\t%s\0 ctime: 0:0\n mtime: 0:0\n dev: 0\tino: 0\n uid: 0\tgid: 0\n size: 0\tflags: %s\n"
good = entry % (b"a", b"20004000") + entry % (b"b", b"0")
self.assertEqual(staged_diff.flagged(good), [(b"100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0\ta", 0x20004000), (b"100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0\tb", 0)])
broken = {
"extra line after flags": entry % (b"a", b"0") + b" extra: 1\n" + entry % (b"b", b"0"),
"flags not hex": entry % (b"a", b"zz"),
"flags with a suffix": entry % (b"a", b"0 extended"),
"one line missing": entry.replace(b" dev: 0\tino: 0\n", b"") % (b"a", b"0"),
"trailing garbage": good + b"junk",
}
for name, raw in broken.items():
with self.subTest(layout=name), self.assertRaisesRegex(staged_diff.DiffError, "^unexpected ls-files --debug layout$"):
staged_diff.flagged(raw)
class Repos(Isolated):
def fresh(self, name: str) -> Path:
repo = self.root / name
(repo / "sub").mkdir(parents=True)
(repo / "other").mkdir()
git(repo, "init", "-q", "-b", "main")
for path, text in (("a.txt", "one\n"), ("empty.txt", ""), ("sub/b.txt", "b\n"), ("other/c.txt", "c\n")):
(repo / path).write_text(text, encoding="utf-8")
git(repo, "add", "-A")
git(repo, "commit", "-q", "-m", "base")
return repo
def ids(self, repo: Path) -> tuple[str, str]:
return git(repo, "write-tree").strip(), staged_diff.binding(repo)
def recorded(self, repo: Path) -> list[tuple[str, str]]:
lines = git(repo, "diff-tree", "--no-commit-id", "--name-status", "-r", "HEAD~1", "HEAD").splitlines()
return sorted((path, status) for status, path in (line.split("\t", 1) for line in lines))
class TestBindingMatchesWriteTree(Repos):
def test_the_binding_changes_exactly_when_the_tree_git_write_tree_records_changes(self):
for name, (setup, mutate) in CASES.items():
with self.subTest(case=name):
repo = self.fresh(name.replace(" ", "-"))
setup(repo)
before = self.ids(repo)
mutate(repo)
after = self.ids(repo)
self.assertEqual(before[1] != after[1], before[0] != after[0])
self.assertEqual(shown(staged_diff.bound_diff(repo, after[1], None)), shown(staged_diff.tree_diff(repo, after[0], None)))
def test_the_preview_lists_exactly_what_git_commit_records(self):
for name, setup in {"intent-to-add new file": intend_new, **UNREAD_CASES}.items():
with self.subTest(case=name):
repo = self.fresh(name.replace(" ", "-"))
stage_a(repo)
setup(repo)
preview = shown(staged_diff.bound_diff(repo, staged_diff.binding(repo), None))
git(repo, "commit", "-q", "-m", "x")
self.assertEqual(preview, self.recorded(repo))
def test_an_allow_turns_into_a_deny_when_a_tracked_empty_file_is_swapped_to_intent_to_add(self):
repo = self.fresh("swap")
stage_a(repo)
ask = {"tool": "Bash", "command": "git commit -m x", "cwd": str(repo), "session": "s", "agent": ""}
pinned, attached = permission_hook.commit_diff(ask, None)
self.assertEqual(shown(attached["diff"]), [("a.txt", "M")])
swap_empty_to_intent(repo)
self.assertEqual(permission_hook.bound(ALLOW, ask, pinned), {"behavior": "deny", "message": permission_hook.CHANGED})
def shown(diff: list[dict]) -> list[tuple[str, str]]:
return sorted((entry["path"], entry["status"]) for entry in diff)
WORKTREE_FORMS = (
"git commit -a -m x", "git commit -am x", "git commit -av -m x", "git commit -m x -a", "git commit --all -m x",
"git commit --all= -m x", "git commit --al -m x", "git commit -i -m x a.txt", "git commit --include -m x a.txt",
"git commit -o -m x a.txt", "git commit --only -m x a.txt", "git commit -m x a.txt", "git commit -m x -- a.txt",
"git commit -p -m x", "git commit --interactive", "git commit --pathspec-from-file=list -m x",
"git commit -a -m x ; true", "git commit -am x&&true", "git add -A && git commit -m x", "sh -c 'git commit -am x'",
"git -c core.fileMode=false commit -am x", "env GIT_INDEX_FILE=other git commit -m x", "/usr/bin/git commit -am x",
)
NOT_APPROVABLE = "kvb-orchestrator: commit reads the working tree; stage the files and commit the index"
AMEND_FORMS = ("git commit --amend --no-edit", "git commit --amend -m x", "git commit -m x --amend", "git commit --amen -m x")
def gitlink_without_folder(repo: Path) -> None:
git(repo, "update-index", "--add", "--cacheinfo", f"160000,{git(repo, 'rev-parse', 'HEAD').strip()},mod")
git(repo, "commit", "-q", "-m", "link")
stage_a(repo)
def file_mode_off_index_x(repo: Path) -> None:
stage_a(repo)
git(repo, "config", "core.fileMode", "false")
git(repo, "update-index", "--chmod=+x", "other/c.txt")
def clean_filter(repo: Path) -> None:
(repo / ".gitattributes").write_text("*.txt filter=upper\n", encoding="utf-8")
git(repo, "add", ".gitattributes")
stage_a(repo)
git(repo, "config", "filter.upper.clean", "tr a-z A-Z")
REVIEWER_STATES = {
"gitlink whose folder is gone": gitlink_without_folder,
"core.fileMode=false with +x only in the index": file_mode_off_index_x,
"clean filter": clean_filter,
}
class TestWorkingTreeCommitsAreNotApprovable(Repos):
def ask(self, repo: Path, command: str) -> dict:
return {"tool": "Bash", "command": command, "cwd": str(repo), "session": "s", "agent": ""}
def test_no_commit_form_that_reads_the_working_tree_is_approved(self):
repo = self.fresh("forms")
stage_a(repo)
for command in WORKTREE_FORMS:
with self.subTest(command=command):
ask = self.ask(repo, command)
pinned, attached = permission_hook.commit_diff(ask, None)
self.assertEqual((pinned, attached), (None, {}))
self.assertIsNone(permission_hook.bound(ALLOW, ask, pinned))
def test_an_amend_is_never_approved_and_a_plain_commit_of_the_same_index_still_binds(self):
repo = self.fresh("amend")
stage_a(repo)
for command in AMEND_FORMS:
with self.subTest(command=command):
ask = self.ask(repo, command)
self.assertEqual(permission_hook.commit_diff(ask, None), (None, {}))
self.assertIsNone(permission_hook.bound(ALLOW, ask, None))
plain = self.ask(repo, "git commit --no-amend -m x")
pinned, attached = permission_hook.commit_diff(plain, None)
self.assertEqual((shown(attached["diff"]), permission_hook.bound(ALLOW, plain, pinned)), ([("a.txt", "M")], ALLOW))
def test_the_reviewer_states_give_commit_all_no_approval_and_a_plain_commit_its_bound_one(self):
for name, setup in REVIEWER_STATES.items():
with self.subTest(state=name):
repo = self.fresh(name.replace(" ", "-").replace("=", "-"))
setup(repo)
everything = self.ask(repo, "git commit -am x")
self.assertEqual(permission_hook.commit_diff(everything, None), (None, {}))
self.assertIsNone(permission_hook.bound(ALLOW, everything, None))
plain = self.ask(repo, "git commit -m x")
pinned, attached = permission_hook.commit_diff(plain, None)
self.assertEqual((pinned is not None, permission_hook.bound(ALLOW, plain, pinned)), (True, ALLOW))
self.assertIn(("a.txt", "M"), shown(attached["diff"]))
def test_the_hook_creates_no_ask_and_names_the_reason_for_such_a_commit(self):
repo = self.fresh("hook")
stage_a(repo)
hook = {"tool_name": "Bash", "tool_input": {"command": "git commit -am x"}, "cwd": str(repo), "session_id": "s"}
argv = ["/usr/bin/python3", "-E", "-s", str(CLIENT / "permission_hook.py"), "--port", "1", "--timeout", "1", "--approvers", str(self.root / "approvers.json"), "--replay", str(self.root / "replay.json")]
done = subprocess.run(argv, input=json.dumps(hook), capture_output=True, text=True, timeout=60, check=False)
self.assertEqual((done.returncode, done.stdout, done.stderr.strip()), (0, "", NOT_APPROVABLE))
class TestHookGitEnvironment(Isolated):
def test_planted_git_variables_in_the_hook_environment_are_never_used(self):
repo = self.root / "repo"
repo.mkdir()
git(repo, "init", "-q", "-b", "main")
(repo / "base.txt").write_text("one\n", encoding="utf-8")
git(repo, "add", "-A")
git(repo, "commit", "-q", "-m", "base")
(repo / "base.txt").write_text("two\n", encoding="utf-8")
(repo / "added.txt").write_text("fresh\n", encoding="utf-8")
git(repo, "add", "-A")
expected = hashlib.sha256(subprocess.run(["git", "ls-files", "-s", "-z"], cwd=repo, env={**os.environ, **GIT_ENV}, capture_output=True, check=True).stdout).hexdigest()
decoys = {"GIT_DIR": self.root / "decoy-git", "GIT_EXEC_PATH": self.root / "decoy-exec", "GIT_INDEX_FILE": self.root / "decoy-index", "GIT_CONFIG_GLOBAL": self.root / "decoy-gitconfig"}
decoys["GIT_DIR"].mkdir()
decoys["GIT_EXEC_PATH"].mkdir()
decoys["GIT_INDEX_FILE"].write_bytes(b"DIRC")
decoys["GIT_CONFIG_GLOBAL"].write_text("[[[\n", encoding="utf-8")
for decoy in decoys.values():
decoy.chmod(0)
self.addCleanup(decoy.chmod, 0o700)
seen, run = [], subprocess.run
def spy(*args, **kwargs):
seen.append(kwargs.get("env"))
return run(*args, **kwargs)
ask = {"tool": "Bash", "command": "git commit -m x", "cwd": str(repo), "session": "s", "agent": ""}
with mock.patch.dict(os.environ, {name: str(path) for name, path in decoys.items()}), mock.patch.object(subprocess, "run", spy):
pinned = staged_diff.binding(repo)
preview = shown(staged_diff.bound_diff(repo, pinned, None))
chosen = permission_hook.bound(ALLOW, ask, permission_hook.commit_diff(ask, None)[0])
self.assertEqual((pinned, preview, chosen), (expected, [("added.txt", "A"), ("base.txt", "M")], ALLOW))
self.assertTrue(seen)
for env in seen:
self.assertLessEqual(set(env), {"HOME", "LANG", "LC_ALL", "LC_CTYPE", "GIT_OPTIONAL_LOCKS", "GIT_NO_LAZY_FETCH", "GIT_CONFIG_NOSYSTEM", "GIT_CONFIG_GLOBAL", "PATH"})
self.assertEqual((env["GIT_CONFIG_GLOBAL"], env["PATH"]), (os.devnull, staged_diff.TRUSTED_PATH))
class TestPasskeyOpenssl(Scratch):
def test_a_planted_openssl_earlier_on_path_never_runs_nor_forges_a_signature(self):
planted = probe(self.root / "bin" / "openssl", self.marker, "exit 0")
spki = passkey.EC_PREFIX + b"\x04" + bytes(64)
with mock.patch.dict(os.environ, {"PATH": f"{planted.parent}{os.pathsep}{os.environ['PATH']}"}):
self.assertFalse(passkey.by_openssl(-7, spki, b"data", b"signature"))
self.assertFalse(self.marker.exists())
class TestReminderHook(Scratch):
def setUp(self):
super().setUp()
self.work = self.root / "work"
self.work.mkdir()
def run_reminder(self, path: str) -> subprocess.CompletedProcess:
env = {**os.environ, "HOME": str(self.root), "DREAM_STAMP_DIR": str(self.root / "stamps"), "PATH": path}
return subprocess.run(["/usr/bin/bash", str(REMINDER)], cwd=self.work, input='{"session_id": "abc"}', env=env, capture_output=True, text=True, timeout=30, check=False)
def test_a_planted_json_module_in_the_cwd_is_never_imported(self):
(self.work / "json.py").write_text(f"open({str(self.marker)!r}, 'w').close()\n", encoding="utf-8")
done = self.run_reminder(os.environ["PATH"])
self.assertFalse(self.marker.exists())
self.assertIn("DREAM HINT", done.stdout)
def test_a_planted_python3_earlier_on_path_never_runs(self):
planted = probe(self.root / "bin" / "python3", self.marker, 'exec /usr/bin/python3 "$@"')
done = self.run_reminder(f"{planted.parent}{os.pathsep}{os.environ['PATH']}")
self.assertFalse(self.marker.exists())
self.assertIn("DREAM HINT", done.stdout)
class TestPermissionHookInstall(Scratch):
def test_the_installed_command_is_absolute_isolated_and_imports_its_modules(self):
home = self.root / "home"
hub = home / ".local" / "share" / "kvb-orchestrator"
shutil.copytree(CLIENT, hub / "client", ignore=shutil.ignore_patterns("__pycache__"))
(hub / ".env").write_text("KVB_ORCHESTRATOR_PORT=1\n", encoding="utf-8")
(home / ".claude").mkdir(parents=True)
env = {**os.environ, "HOME": str(home)}
subprocess.run(["/usr/bin/bash", str(ORCHESTRATOR), "hook"], env=env, check=True, capture_output=True, text=True, timeout=60)
settings = json.loads((home / ".claude" / "settings.json").read_text(encoding="utf-8"))
command = settings["hooks"]["PermissionRequest"][-1]["hooks"][0]["command"]
self.assertTrue(command.startswith("/usr/bin/python3 -E -s "), command)
planted = probe(self.root / "bin" / "python3", self.marker, 'exec /usr/bin/python3 "$@"')
work = self.root / "work"
work.mkdir()
(work / "json.py").write_text(f"open({str(self.marker)!r}, 'w').close()\n", encoding="utf-8")
run = {**env, "PATH": f"{planted.parent}{os.pathsep}{os.environ['PATH']}"}
done = subprocess.run(["/usr/bin/bash", "-c", command], cwd=work, input="no hook input", env=run, capture_output=True, text=True, timeout=60, check=False)
self.assertEqual((done.returncode, done.stdout, done.stderr), (0, "", ""))
self.assertFalse(self.marker.exists())
if __name__ == "__main__":
unittest.main()
+54 -114
View File
@@ -1,6 +1,7 @@
from __future__ import annotations
import ast
import hashlib
import importlib
import json
import os
@@ -8,7 +9,6 @@ import shlex
import subprocess
import sys
import tempfile
import threading
import unittest
from pathlib import Path
from unittest import mock
@@ -219,11 +219,6 @@ def scratch_repo(root: Path) -> Path:
return repo
def bytes_read() -> int:
counters = dict(line.split(": ") for line in Path("/proc/self/io").read_text(encoding="ascii").splitlines())
return int(counters["rchar"])
def deny(entries: object) -> dict:
return {"sandbox": {"filesystem": {"denyRead": entries}}}
@@ -354,7 +349,7 @@ class TestStagedDiff(unittest.TestCase):
self.addCleanup(patch.stop)
def test_the_staged_diff_lists_each_file_once_and_a_file_matching_a_secret_rule_ships_only_its_counts_and_the_note(self):
found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, False, self.rules)}
found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo,self.rules)}
self.assertEqual(sorted(found), ["added.txt", "app.py", "gone.txt", "logo.png"])
self.assertEqual({path: (entry["status"], entry["mode"], entry["additions"], entry["deletions"], entry["note"]) for path, entry in found.items()}, {
"added.txt": ("A", "100644", 1, 0, ""), "app.py": ("M", "100644", 3, 1, WITHHELD), "gone.txt": ("D", "100644", 0, 1, ""), "logo.png": ("B", "100644", 0, 0, ""),
@@ -373,14 +368,14 @@ class TestStagedDiff(unittest.TestCase):
(self.repo / "plain.txt").write_text("plain\n-----BEGIN lowercase is no armour-----\n", encoding="utf-8")
(self.repo / "fresh.txt").write_text(ARMOURED, encoding="utf-8")
git(self.repo, "add", "key.txt", "plain.txt", "fresh.txt")
found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, False, self.rules)}
found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo,self.rules)}
self.assertEqual([(found[path]["patch"], found[path]["note"], found[path]["deletions"]) for path in ("key.txt", "fresh.txt")], [("", WITHHELD, 3), ("", WITHHELD, 0)])
self.assertEqual((found["plain.txt"]["note"], found["plain.txt"]["additions"]), ("", 1))
self.assertNotIn("plain words only", json.dumps(list(found.values())))
def test_past_the_cap_the_rest_is_listed_by_stat_only(self):
with mock.patch.object(staged_diff, "DIFF_LIMIT", 40):
found = staged_diff.review_diff(self.repo, False, self.rules)
found = staged_diff.review_diff(self.repo,self.rules)
self.assertEqual([(entry["path"], bool(entry["patch"]), entry["note"]) for entry in found], [("added.txt", True, ""), ("app.py", False, WITHHELD), ("gone.txt", False, ""), ("logo.png", False, "")])
self.assertEqual([(entry["additions"], entry["deletions"]) for entry in found], [(1, 0), (3, 1), (0, 1), (0, 0)])
@@ -390,40 +385,22 @@ class TestStagedDiff(unittest.TestCase):
(broken / "bad.yaml").write_text("cwe: CWE-798\nrules: [unclosed\n", encoding="utf-8")
for rules in (None, self.root / "missing", broken):
with self.subTest(rules=str(rules)):
found = staged_diff.review_diff(self.repo, False, rules)
found = staged_diff.review_diff(self.repo,rules)
self.assertEqual([(entry["patch"], entry["note"]) for entry in found], [("", WITHHELD)] * 3 + [("", "")])
self.assertIsNone(staged_diff.secret_rules(broken))
def test_the_diff_fails_past_the_file_cap_or_the_path_limit_instead_of_the_hub_refusing_it(self):
for name, value in (("FILE_CAP", 3), ("PATH_LIMIT", 8)):
with self.subTest(limit=name), mock.patch.object(staged_diff, name, value), self.assertRaises(staged_diff.DiffError):
staged_diff.review_diff(self.repo, False, self.rules)
staged_diff.review_diff(self.repo,self.rules)
self.assertEqual((staged_diff.FILE_CAP, staged_diff.PATH_LIMIT, diffs.FILE_CAP, diffs.PATH_LIMIT), (hub_limits.FILE_CAP, hub_limits.PATH_LIMIT) * 2)
def test_commit_all_shows_the_executable_bit_on_disk(self):
(self.repo / "added.txt").chmod(0o755)
everything = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, True, self.rules)}
self.assertEqual((everything["added.txt"]["mode"], everything["app.py"]["mode"]), ("100755", "100644"))
git(self.repo, "commit", "-q", "-m", "next")
found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, True, self.rules)}
self.assertEqual((found["added.txt"]["status"], found["added.txt"]["mode"], found["added.txt"]["additions"]), ("M", "100644 => 100755", 0))
self.assertEqual(staged_diff.review_diff(self.repo, False, self.rules), [])
def test_commit_all_diffs_the_tracked_working_tree_against_head_without_untracked_files(self):
(self.repo / "added.txt").write_text("fresh\nlater\n", encoding="utf-8")
(self.repo / "untracked.txt").write_text("not mine\n", encoding="utf-8")
staged = {entry["path"]: entry["additions"] for entry in staged_diff.review_diff(self.repo, False, self.rules)}
everything = {entry["path"]: entry["additions"] for entry in staged_diff.review_diff(self.repo, True, self.rules)}
self.assertEqual((staged["added.txt"], everything["added.txt"]), (1, 2))
self.assertNotIn("untracked.txt", everything)
def test_a_planted_fsmonitor_or_clean_filter_starts_no_program(self):
marker = self.root / "ran"
git(self.repo, "config", "core.fsmonitor", f"touch {marker}")
git(self.repo, "config", "filter.evil.clean", f"touch {marker}; cat")
(self.repo / ".gitattributes").write_text("* filter=evil\n", encoding="utf-8")
for everything in (False, True):
staged_diff.review_diff(self.repo, everything, self.rules)
staged_diff.review_diff(self.repo, self.rules)
self.assertFalse(marker.exists())
def test_a_planted_promisor_remote_fetches_no_missing_object_and_starts_no_program(self):
@@ -436,63 +413,23 @@ class TestStagedDiff(unittest.TestCase):
}
for key, value in promisor.items():
git(self.repo, "config", key, value)
for everything in (False, True):
with self.subTest(everything=everything), self.assertRaises(staged_diff.DiffError):
staged_diff.review_diff(self.repo, everything, self.rules)
with self.assertRaises(staged_diff.DiffError):
staged_diff.review_diff(self.repo, self.rules)
self.assertFalse(marker.exists())
def test_commit_all_reads_no_file_behind_a_symlinked_parent_directory(self):
outside = self.root / "outside"
outside.mkdir()
(outside / "key").write_text("stolen = 1\n", encoding="utf-8")
for folder in ("k", "d"):
(self.repo / folder).mkdir()
(self.repo / folder / "key").write_text("mine = 1\n", encoding="utf-8")
(self.repo / "d" / "link").symlink_to("key")
git(self.repo, "add", "k", "d")
(self.repo / "k" / "key").unlink()
(self.repo / "k").rmdir()
(self.repo / "k").symlink_to(outside)
found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, True, self.rules)}
self.assertEqual([found[path]["patch"] for path in ("d/key", "d/link")], ["@@ -0,0 +1 @@\n+mine = 1", "@@ -0,0 +1 @@\n+key"])
self.assertNotIn("k/key", found)
self.assertNotIn("stolen", json.dumps(list(found.values())))
def test_an_index_path_leaving_the_working_tree_is_refused(self):
(self.root / "outside.txt").write_text("stolen = 1\n", encoding="utf-8")
for path in ("../outside.txt", "d/../../outside.txt", str(self.root / "outside.txt")):
with self.subTest(path=path), self.assertRaises(staged_diff.DiffError):
staged_diff.worktree(self.repo, path, "100644", "sha1", [])
def test_a_fifo_in_the_working_tree_is_skipped_without_blocking(self):
os.mkfifo(self.repo / "pipe")
found = []
reader = threading.Thread(target=lambda: found.append(staged_diff.worktree(self.repo, "pipe", "100644", "sha1", [])), daemon=True)
reader.start()
reader.join(10)
self.assertEqual(found, [None])
def test_a_working_tree_file_over_the_blob_limit_is_not_read(self):
with (self.repo / "big.bin").open("wb") as handle:
handle.truncate(4 * staged_diff.BLOB_LIMIT)
before = bytes_read()
found = staged_diff.worktree(self.repo, "big.bin", "100644", "sha1", [])
self.assertLess(bytes_read() - before, staged_diff.BLOB_LIMIT)
self.assertEqual((found[0], found[2]), ("100644", None))
def test_commit_all_reads_no_file_under_a_deny_read_path_and_still_reads_its_sibling(self):
def test_the_staged_diff_ships_nothing_under_a_deny_read_path_and_still_its_sibling(self):
(self.repo / "vault").mkdir()
(self.repo / "vault" / "key").write_text("stolen = 1\n", encoding="utf-8")
(self.repo / "open.txt").write_text("seen = 1\n", encoding="utf-8")
git(self.repo, "add", "vault", "open.txt")
write_settings(self.repo / ".claude" / "settings.local.json", deny(["~/repo/vault"]))
found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, True, self.rules)}
found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, self.rules)}
self.assertEqual([found[path]["patch"] for path in ("vault/key", "open.txt")], ["", "@@ -0,0 +1 @@\n+seen = 1"])
self.assertNotIn("stolen", json.dumps(list(found.values())))
def test_commit_all_without_any_settings_file_diffs_normal_files(self):
def test_the_staged_diff_without_any_settings_file_diffs_normal_files(self):
self.assertEqual([file for file, _ in sandbox_paths.settings_files(self.repo) if file.exists()], [])
found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, True, self.rules)}
found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, self.rules)}
self.assertEqual((found["added.txt"]["patch"], found["app.py"]["additions"]), ("@@ -0,0 +1 @@\n+fresh", 3))
def test_a_planted_empty_deny_read_list_still_leaves_the_floor_unread(self):
@@ -502,25 +439,24 @@ class TestStagedDiff(unittest.TestCase):
git(self.repo, "add", ".ssh", "notes.txt")
write_settings(self.repo / ".claude" / "settings.json", deny([]))
with mock.patch.dict(os.environ, {"HOME": str(self.repo)}):
found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, True, self.rules)}
found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, self.rules)}
self.assertEqual([found[path]["patch"] for path in (".ssh/id_ed25519", "notes.txt")], ["", "@@ -0,0 +1 @@\n+seen = 1"])
self.assertNotIn("stolen", json.dumps(list(found.values())))
def test_commit_all_fails_closed_on_an_unreadable_deny_read_setting_or_in_a_denied_working_tree(self):
def test_the_staged_diff_fails_closed_on_an_unreadable_deny_read_setting_or_in_a_denied_working_tree(self):
cases = {"unparsable": b"{", "no utf-8": b"\xff", "no paths": deny("~/x"), "denied top": deny(["~"])}
for case, content in cases.items():
with self.subTest(case=case):
write_settings(self.root / ".claude" / "settings.json", content)
for everything in (True, False):
with self.assertRaises(staged_diff.DiffError):
staged_diff.review_diff(self.repo, everything, self.rules)
with self.assertRaises(staged_diff.DiffError):
staged_diff.review_diff(self.repo, self.rules)
def test_the_staged_diff_ships_no_content_under_a_denied_path(self):
(self.repo / "vault").mkdir()
(self.repo / "vault" / "key").write_text("stolen = 1\n", encoding="utf-8")
git(self.repo, "add", "vault")
write_settings(self.repo / ".claude" / "settings.json", deny(["vault"]))
found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, False, self.rules)}
found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo,self.rules)}
self.assertEqual((found["vault/key"]["status"], found["vault/key"]["patch"], found["added.txt"]["patch"]), ("A", "", "@@ -0,0 +1 @@\n+fresh"))
def test_the_session_project_settings_and_read_rules_add_denies(self):
@@ -531,7 +467,7 @@ class TestStagedDiff(unittest.TestCase):
write_settings(self.root / "project" / ".claude" / "settings.local.json", deny(["~/repo/vault"]))
write_settings(self.repo / ".claude" / "settings.json", {"permissions": {"deny": ["Read(/notes.txt)", "Bash(rm:*)"]}})
write_settings(self.root / ".claude" / "settings.json", {"permissions": {"deny": ["Read(*.pem)"]}})
found = {entry["path"]: entry["patch"] for entry in staged_diff.review_diff(self.repo, True, self.rules)}
found = {entry["path"]: entry["patch"] for entry in staged_diff.review_diff(self.repo, self.rules)}
self.assertEqual([found[path] for path in ("vault/key", "notes.txt", "deep.pem", "open.txt")], ["", "", "", "@@ -0,0 +1 @@\n+seen = 1"])
def test_each_settings_scope_resolves_relative_entries_from_its_own_folder_and_read_rules_like_claude_code(self):
@@ -555,7 +491,7 @@ class TestStagedDiff(unittest.TestCase):
self.assertFalse(sandbox_paths.denied(str(self.repo / "env.txt"), roots))
(self.repo / ".env").write_text("hidden = 1\n", encoding="utf-8")
git(self.repo, "add", ".env")
found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, True, self.rules)}
found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, self.rules)}
self.assertEqual((found[".env"]["patch"], found["added.txt"]["patch"]), ("", "@@ -0,0 +1 @@\n+fresh"))
def test_an_allow_turns_into_a_deny_when_the_staged_tree_changed_since_the_preview_even_in_a_file_it_withholds(self):
@@ -570,7 +506,8 @@ class TestStagedDiff(unittest.TestCase):
with self.subTest(case=case):
git(self.repo, "add", "-A")
pinned, shown = permission_hook.commit_diff(ask, self.rules)
self.assertEqual((pinned, len(shown["diff"])), (git(self.repo, "write-tree").strip(), len(shown["diff"])))
index = subprocess.run(["git", "ls-files", "-s", "-z"], cwd=self.repo, capture_output=True, check=True).stdout
self.assertEqual((pinned, shown["diff"]), (hashlib.sha256(index).hexdigest(), staged_diff.review_diff(self.repo,self.rules)))
self.assertEqual(permission_hook.bound(ALLOW, ask, pinned), ALLOW)
edit()
self.assertEqual(permission_hook.bound(ALLOW, ask, pinned), ALLOW)
@@ -578,13 +515,10 @@ class TestStagedDiff(unittest.TestCase):
self.assertEqual(permission_hook.bound(ALLOW, ask, pinned), changed)
self.assertEqual(permission_hook.bound({"behavior": "deny", "message": "no"}, ask, pinned), {"behavior": "deny", "message": "no"})
def test_an_allow_of_commit_all_is_bound_to_the_working_tree_too(self):
def test_an_allow_of_commit_all_is_never_approved(self):
ask = {**ASK, "tool": "Bash", "command": "git commit -am x", "cwd": str(self.repo)}
pinned, shown = permission_hook.commit_diff(ask, self.rules)
self.assertEqual([entry["path"] for entry in shown["diff"]], ["added.txt", "app.py", "gone.txt", "logo.png"])
self.assertEqual(permission_hook.bound(ALLOW, ask, pinned), ALLOW)
(self.repo / "app.py").write_text("unstaged edit\n", encoding="utf-8")
self.assertEqual(permission_hook.bound(ALLOW, ask, pinned)["behavior"], "deny")
self.assertEqual(permission_hook.commit_diff(ask, self.rules), (None, {}))
self.assertIsNone(permission_hook.bound(ALLOW, ask, None))
def test_an_allow_fails_closed_with_a_reason_when_the_tree_cannot_be_read(self):
ask = {**ASK, "tool": "Bash", "command": "git commit -m x", "cwd": str(self.repo)}
@@ -606,18 +540,18 @@ class TestStagedDiff(unittest.TestCase):
(self.repo / "added.txt").write_text("fresh\nslipped in\n", encoding="utf-8")
git(self.repo, "add", "added.txt")
self.assertEqual(staged_diff.tree_diff(self.repo, tree, self.rules), before)
self.assertNotEqual(staged_diff.review_diff(self.repo, False, self.rules), before)
self.assertNotEqual(staged_diff.review_diff(self.repo,self.rules), before)
def test_without_a_secret_pattern_no_patch_ships(self):
empty = self.root / "empty-rules"
empty.mkdir()
(empty / "none.yaml").write_text("rules: []\n", encoding="utf-8")
self.assertEqual({(entry["patch"], entry["note"]) for entry in staged_diff.review_diff(self.repo, False, empty)}, {("", WITHHELD), ("", "")})
self.assertEqual({(entry["patch"], entry["note"]) for entry in staged_diff.review_diff(self.repo,empty)}, {("", WITHHELD), ("", "")})
def test_a_file_name_that_is_no_utf8_ships_as_backslash_escapes(self):
(self.repo / os.fsdecode(b"caf\xe9.txt")).write_text("x\n", encoding="utf-8")
git(self.repo, "add", "-A")
found = staged_diff.review_diff(self.repo, False, self.rules)
found = staged_diff.review_diff(self.repo,self.rules)
self.assertIn("caf\\xe9.txt", [entry["path"] for entry in found])
json.dumps(found, ensure_ascii=False).encode("utf-8")
@@ -625,10 +559,10 @@ class TestStagedDiff(unittest.TestCase):
tree = git(self.repo, "rev-parse", "HEAD^{tree}").strip()
(self.repo / "added.txt").write_text("one\x0ctwo\n", encoding="utf-8")
git(self.repo, "add", "added.txt")
self.assertEqual(next(entry for entry in staged_diff.review_diff(self.repo, False, self.rules) if entry["path"] == "added.txt")["additions"], 1)
self.assertEqual(next(entry for entry in staged_diff.review_diff(self.repo,self.rules) if entry["path"] == "added.txt")["additions"], 1)
(self.repo / ".git" / "objects" / tree[:2] / tree[2:]).unlink()
with self.assertRaises(staged_diff.DiffError):
staged_diff.review_diff(self.repo, False, self.rules)
staged_diff.review_diff(self.repo,self.rules)
def test_a_head_naming_a_missing_commit_or_a_broken_ref_fails_and_only_an_unborn_one_is_empty(self):
fresh = self.root / "fresh"
@@ -636,14 +570,14 @@ class TestStagedDiff(unittest.TestCase):
git(fresh, "init", "-q", "-b", "main")
(fresh / "one.txt").write_text("one\n", encoding="utf-8")
git(fresh, "add", "one.txt")
self.assertEqual([(entry["path"], entry["status"]) for entry in staged_diff.review_diff(fresh, False, self.rules)], [("one.txt", "A")])
self.assertEqual([(entry["path"], entry["status"]) for entry in staged_diff.review_diff(fresh, self.rules)], [("one.txt", "A")])
commit = git(self.repo, "rev-parse", "HEAD").strip()
(self.repo / ".git" / "objects" / commit[:2] / commit[2:]).unlink()
with self.assertRaises(staged_diff.DiffError):
staged_diff.review_diff(self.repo, False, self.rules)
staged_diff.review_diff(self.repo,self.rules)
(self.repo / ".git" / "refs" / "heads" / "main").write_text("not a ref\n", encoding="utf-8")
with self.assertRaises(staged_diff.DiffError):
staged_diff.review_diff(self.repo, False, self.rules)
staged_diff.review_diff(self.repo,self.rules)
def test_the_rules_are_cached_until_a_rule_file_changes(self):
first = staged_diff.secret_rules(self.rules)
@@ -661,17 +595,18 @@ class TestStagedDiff(unittest.TestCase):
staged_diff.CACHE.write_text(broken, encoding="utf-8")
self.assertEqual([globs for globs, _ in staged_diff.secret_rules(self.rules)], [("*.txt",)])
def test_only_a_single_git_commit_names_its_directory_and_all_mode(self):
def test_only_a_single_git_commit_of_the_staged_index_names_its_directory(self):
cases = {
"git commit -m 'feat: x'": ("/w", False),
"git commit -am 'feat: x'": ("/w", True),
"git commit -m all -a": ("/w", True),
"git commit --all --no-verify -m x": ("/w", True),
"git commit -ma": ("/w", False),
"git -C sub commit -m x": ("/w/sub", False),
"GIT_EDITOR=true git commit": ("/w", False),
"env -C sub git commit -m x": ("/w/sub", False),
"env --chdir=sub GIT_EDITOR=true git -C deeper commit -am x": ("/w/sub/deeper", True),
"git commit -m 'feat: x'": "/w",
"git commit -am 'feat: x'": None,
"git commit -m all -a": None,
"git commit --all --no-verify -m x": None,
"git commit -ma": "/w",
"git -C sub commit -m x": "/w/sub",
"GIT_EDITOR=true git commit": "/w",
"env -C sub git commit -m x": "/w/sub",
"env --chdir=sub GIT_EDITOR=true git -C deeper commit -m x": "/w/sub/deeper",
"env --chdir=sub GIT_EDITOR=true git -C deeper commit -am x": None,
"git commit -m x -- -a": None,
"git commit -m x file.txt": None,
"git commit -a -m x file.txt": None,
@@ -689,10 +624,14 @@ class TestStagedDiff(unittest.TestCase):
"git commit -a --no-all -m x": None,
"git commit --al -m x": None,
"git commit --frobnicate -m x": None,
"git commit --mess 'feat: x' --no-veri": ("/w", False),
"git commit --mess 'feat: x' --no-veri": "/w",
"git commit --all-m": None,
"git commit -U 3 -m x": ("/w", False),
"git commit --unified 3 -am x": ("/w", True),
"git commit -U 3 -m x": "/w",
"git commit --unified 3 -am x": None,
"git commit -m x --": "/w",
"git commit --amend --no-edit": None,
"git commit --amen -m x": None,
"git commit --no-amend -m x": "/w",
"GIT_INDEX_FILE=/tmp/other git commit -m x": None,
"GIT_DIR=/tmp/other git commit -m x": None,
"env GIT_WORK_TREE=/tmp/other git commit -m x": None,
@@ -707,7 +646,7 @@ class TestStagedDiff(unittest.TestCase):
for command, expected in cases.items():
with self.subTest(command=command):
found = staged_diff.commit_of(command, "/w")
self.assertEqual(None if found is None else (str(found[0]), found[1]), expected)
self.assertEqual(None if found is None else str(found), expected)
class TestHubOperatorTasks(HubCase):
@@ -769,7 +708,8 @@ class TestHubOperatorTasks(HubCase):
with mock.patch.object(staged_diff, "CACHE", self.outside / "cache.json"), mock.patch.dict(os.environ, {key: env[key] for key in ("HOME", "CLAUDE_CONFIG_DIR", "CLAUDE_PROJECT_DIR")}):
commit = {**ASK, "command": "git commit -m 'feat: w1'", "cwd": str(repo)}
pinned, attached = permission_hook.commit_diff(commit, rules)
self.assertEqual(([entry["path"] for entry in attached["diff"]], pinned), (["added.txt", "app.py", "gone.txt", "logo.png"], git(repo, "write-tree").strip()))
index = subprocess.run(["git", "ls-files", "-s", "-z"], cwd=repo, capture_output=True, check=True).stdout
self.assertEqual(([entry["path"] for entry in attached["diff"]], pinned), (["added.txt", "app.py", "gone.txt", "logo.png"], hashlib.sha256(index).hexdigest()))
self.assertEqual(permission_hook.commit_diff({**commit, "command": "make deploy"}, rules), (None, {}))
self.assertEqual(permission_hook.commit_diff({**commit, "tool": "Write"}, rules), (None, {}))
self.assertEqual(permission_hook.commit_diff({**commit, "cwd": str(self.outside / "nowhere")}, rules), (None, {}))