diff --git a/client/hardtruth_hook.py b/client/hardtruth_hook.py index daa92cd..7c4fe50 100755 --- a/client/hardtruth_hook.py +++ b/client/hardtruth_hook.py @@ -852,9 +852,12 @@ def get_baseline_roots() -> List[str]: def snapshot_repos_at_session_start(conv_id: str, cwd: str, time_budget_sec: float = 5.0, max_repos: int = 200) -> int: """Registers HEAD and refs/stash of every candidate repository in the daemon under the session-start namespace, session directory first. Returns the number of repositories - snapshotted by the deadline. Registrations not started by then are cancelled, so the hook - outlives the budget by at most one in-flight call (1.5s timeout) and stays inside the - SessionStart hook timeout; a repository left without a snapshot fails closed at Stop. + snapshotted by the deadline. Registrations not started by then are cancelled, but a worker + already running makes two sequential calls (HEAD, then stash; 1.5s timeout each), so the + hook process outlives the budget by at most 3s (it exits only once in-flight workers + finish) and stays inside the SessionStart hook timeout. A repository left without a + snapshot fails closed at Stop when it is outside the workspace (see + workspace_snapshot_baseline for repositories inside it). Values are read before the agent's first tool call, so an in-flight registration that lands after the deadline still records the session-start state.""" import concurrent.futures @@ -908,10 +911,15 @@ def _session_start_value(ns: str, repo_root: str, conv_id: str) -> Optional[str] return None +# Hook payloads without a session id normalise to these; every such session shares one +# daemon key, so a snapshot under it may belong to another session and is never trusted. +ID_LESS_SESSION_IDS = ("unknown", "default") + + def trusted_repo_baseline(repo_root: str, conv_id: str) -> Optional[str]: """HEAD of repo_root as snapshotted in the daemon at session start, or None. Never records anything and never consults agent-writable stores.""" - if not repo_root or not conv_id: + if not repo_root or not conv_id or conv_id in ID_LESS_SESSION_IDS: return None sha = _session_start_value(SESSION_START_HEAD_NS, repo_root, conv_id) if sha and is_valid_commit(sha, repo_root): @@ -919,6 +927,50 @@ def trusted_repo_baseline(repo_root: str, conv_id: str) -> Optional[str]: return None +# --------------------------------------------------------------------------- +# Repositories inside the workspace +# +# The workspace repository (and the repository of any in-workspace Tier 2 target) is +# verified against its session-start snapshot whenever one exists, exactly like a +# repository outside the workspace: Phase 5 lineage and stash (by value, not by the +# agent-settable stash commit time), Phases 6 and 8 changed lines and assertion counts, +# and check_outside_repo_integrity (committed manifest/conftest tamper with that explicit +# baseline, assertion deletion, tautologies) for each in-workspace Tier 2 target. +# +# With no snapshot (SessionStart did not run: another harness, a hook installed +# mid-session, an id-less session; or it ran out of budget before reaching the +# repository) the in-workspace checks keep today's behaviour and use +# get_or_set_session_baseline, which is lazy and agent-influenceable. Failing closed +# there would halt every such session, so it is opt-in: HARDTRUTH_REQUIRE_SESSION_SNAPSHOT=1 +# halts (non-remediable) when an in-workspace repository the gate checks has no snapshot. +# A repository outside the workspace always fails closed without one. +# --------------------------------------------------------------------------- + +def require_session_snapshot() -> bool: + return os.environ.get("HARDTRUTH_REQUIRE_SESSION_SNAPSHOT") == "1" + + +def repo_toplevel(path: Optional[str]) -> Optional[str]: + """Real path of the git working tree containing path, or None.""" + if not path or not os.path.isdir(path): + return None + rc, out = _git_out(path, ["rev-parse", "--show-toplevel"], timeout=2.0) + out = out.strip() + return os.path.realpath(out) if rc == 0 and out else None + + +def workspace_snapshot_baseline(path: Optional[str], conv_id: str) -> Tuple[Optional[str], Optional[str]]: + """(git top-level of path, its session-start snapshot HEAD or None).""" + top = repo_toplevel(path) + return top, (trusted_repo_baseline(top, conv_id) if top else None) + + +def no_snapshot_message(repo_root: str) -> str: + return (f"🚨 HARDTRUTH UNDETERMINED: {repo_root} has no session-start snapshot and " + "HARDTRUTH_REQUIRE_SESSION_SNAPSHOT=1 forbids the lazy baseline. Start a new session so " + "SessionStart can snapshot it. HardTruth fails closed.") + + def _test_weakening_marker(line_str: str) -> Optional[str]: """Label of the tautological assertion or skip marker on this test-source line, or None.""" if re.search(r"^\s*assert\s+(?:True|1\s*==\s*1)\b", line_str): @@ -947,15 +999,9 @@ def _test_weakening_marker(line_str: str) -> Optional[str]: _ASSERTION_RE = re.compile(r"\b(?:assert|expect|self\.assert)\b") -def check_outside_repo_integrity(target: Dict[str, Any], conv_id: str) -> Optional[str]: - """ - The baseline-dependent defences Phases 5 and 8 apply to the workspace repository, applied - to a Tier 2 target in a repository outside the workspace, against that repository's - session-start snapshot. Returns a halt message, or None when the repository is clean. - """ - repo_root = target["repo_root"] - baseline = target["baseline"] - +def check_repo_lineage_and_stash(repo_root: str, baseline: str, conv_id: str) -> Optional[str]: + """Phase 5 against a session-start snapshot: HEAD must descend from it and refs/stash + must be what it was at session start. Returns a halt message or None.""" rc, _ = _git_out(repo_root, ["merge-base", "--is-ancestor", baseline, "HEAD"], timeout=5.0) if rc != 0: return (f"🚨 HARDTRUTH GATE HALTED (EVASION E4 DETECTED): HEAD of {repo_root} is not a descendant of " @@ -970,6 +1016,22 @@ def check_outside_repo_integrity(target: Dict[str, Any], conv_id: str) -> Option return (f"🚨 HARDTRUTH GATE HALTED (EVASION DETECTED): the git stash of {repo_root} changed during this " "session. Stashing changes to bypass dirty-tree verification is forbidden. Restore your stashed " "changes and run tests before completing.") + return None + + +def check_outside_repo_integrity(target: Dict[str, Any], conv_id: str) -> Optional[str]: + """ + The baseline-dependent defences Phases 5 and 8 apply to the workspace repository, applied + to a Tier 2 target against its repository's session-start snapshot (target["baseline"]). + Used for every target outside the workspace, and for in-workspace targets whose + repository has a snapshot. Returns a halt message, or None when the repository is clean. + """ + repo_root = target["repo_root"] + baseline = target["baseline"] + + violation = check_repo_lineage_and_stash(repo_root, baseline, conv_id) + if violation: + return violation if check_manifest_tampering is not None: tampered, files = check_manifest_tampering(target["root"], conv_id=conv_id, baseline_sha=baseline) @@ -2736,8 +2798,18 @@ def fail_halt(reason: str, remediable: bool = True) -> dict: # Phase 5: Git Lineage & Stash Detection (Evasion E4 & E1) p5_wall = time.perf_counter() p5_cpu = time.process_time() + # The workspace repository's session-start snapshot, when there is one, is the baseline + # for Phases 5, 6 and 8 (see workspace_snapshot_baseline); otherwise the lazy baseline. + ws_repo, ws_snapshot = workspace_snapshot_baseline(workspace_dir, conv_id) if workspace_dir else (None, None) + if ws_repo and not ws_snapshot and require_session_snapshot(): + return fail_halt(no_snapshot_message(ws_repo), remediable=False) + if ws_snapshot: + violation = check_repo_lineage_and_stash(ws_repo, ws_snapshot, conv_id) + if violation: + return fail_halt(violation) + # Evasion E4 Check: Git History Lineage (merge-base ancestor check) - if workspace_dir and os.path.exists(os.path.join(workspace_dir, ".git")): + if not ws_snapshot and workspace_dir and os.path.exists(os.path.join(workspace_dir, ".git")): ws_abs = os.path.abspath(workspace_dir) baseline_sha = get_or_set_session_baseline(workspace_dir, conv_id) if baseline_sha: @@ -2756,7 +2828,7 @@ def fail_halt(reason: str, remediable: bool = True) -> dict: return fail_halt(f"🚨 HARDTRUTH UNDETERMINED: Git merge-base lineage check failed: {e}. HardTruth fails closed.", remediable=False) # Evasion E1 Check: Git Stash Detection - if workspace_dir and os.path.exists(os.path.join(workspace_dir, ".git")): + if not ws_snapshot and workspace_dir and os.path.exists(os.path.join(workspace_dir, ".git")): try: stash_res = subprocess.run( ["git", "stash", "list", "--format=%gd %ct"], @@ -2790,7 +2862,7 @@ def fail_halt(reason: str, remediable: bool = True) -> dict: # Phase 6: Rule 3: Polyglot Anti-Stubbing Linter on Modified Source Files p6_wall = time.perf_counter() p6_cpu = time.process_time() - baseline_sha = get_or_set_session_baseline(workspace_dir, conv_id) if workspace_dir else None + baseline_sha = ws_snapshot or (get_or_set_session_baseline(workspace_dir, conv_id) if workspace_dir else None) for fpath in modified_paths: if os.path.exists(fpath): mod_lines = get_changed_lines(workspace_dir, fpath, baseline_sha) @@ -2828,8 +2900,8 @@ def fail_halt(reason: str, remediable: bool = True) -> dict: p8_wall = time.perf_counter() p8_cpu = time.process_time() test_weakening_violations = [] - baseline_sha = None - if conv_id and workspace_dir and os.path.exists(os.path.join(workspace_dir, ".git")): + baseline_sha = ws_snapshot + if not baseline_sha and conv_id and workspace_dir and os.path.exists(os.path.join(workspace_dir, ".git")): baseline_sha = get_or_set_session_baseline(workspace_dir, conv_id) for fpath in modified_paths: @@ -2868,11 +2940,11 @@ def fail_halt(reason: str, remediable: bool = True) -> dict: test_weakening_violations.append(f"{marker} found in {base_name}:{ln_idx}") # Assertion count comparison against git baseline blob - if conv_id and workspace_dir and os.path.exists(os.path.join(workspace_dir, ".git")): + if conv_id and workspace_dir: if baseline_sha: rel_path = os.path.relpath(fpath, workspace_dir) proc_base = subprocess.run( - ["git", "show", f"{baseline_sha}:{rel_path}"], + ["git", "show", f"{baseline_sha}:./{rel_path}"], cwd=workspace_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE, @@ -2940,13 +3012,25 @@ def fail_halt(reason: str, remediable: bool = True) -> dict: ) resolved_targets = [t for t in resolution.get("targets", []) if t.get("root") and os.path.exists(t["root"])] - # Repositories outside the workspace get the same baseline-dependent defences the - # workspace repository gets in Phases 5 and 8, against their own session baseline. + # Every target's repository gets the baseline-dependent defences of Phases 5 and 8 + # (plus the committed manifest/conftest tamper check) against its session-start + # snapshot: always for repositories outside the workspace (the resolver fails closed + # without one), and for in-workspace targets whose repository has one. Without one an + # in-workspace target keeps the lazy baseline of Phases 5/8 and Tier 2's own tamper + # check, unless HARDTRUTH_REQUIRE_SESSION_SNAPSHOT=1. for t in resolved_targets: - if t.get("outside"): - violation = check_outside_repo_integrity(t, conv_id) - if violation: - return fail_halt(violation) + if not t.get("outside"): + top, snap = workspace_snapshot_baseline(t["root"], conv_id) + if not top: + continue + if not snap: + if require_session_snapshot(): + return fail_halt(no_snapshot_message(top), remediable=False) + continue + t = dict(t, repo_root=top, baseline=snap) + violation = check_outside_repo_integrity(t, conv_id) + if violation: + return fail_halt(violation) # session_evidence: True only when the target was derived from this session's own # edits -- gates the approved scoped native-host policy diff --git a/tests/test_tier2_in_workspace_snapshot.py b/tests/test_tier2_in_workspace_snapshot.py new file mode 100644 index 0000000..565861b --- /dev/null +++ b/tests/test_tier2_in_workspace_snapshot.py @@ -0,0 +1,235 @@ +#!/usr/bin/env python3 +""" +Regression tests: repositories INSIDE the workspace are verified against their session-start +snapshot (follow-up r3 M1 to PR #3). + +get_or_set_session_baseline() is lazy: it records HEAD after whatever tool ran first and +falls back to a git ref and a halt-dir cache, all agent-writable. PostToolUse calls it for +every tool cwd, so a shell commit made before the first edit became the baseline, and a +committed conftest tamper in the workspace repository passed Phase 5, Phase 8 and the Tier 2 +tamper check. When SessionStart snapshotted the repository, Stop now uses that snapshot for +Phases 5, 6 and 8 and applies check_outside_repo_integrity to in-workspace targets. + +Without a snapshot the in-workspace checks keep the lazy baseline (documented in +client/hardtruth_hook.py) unless HARDTRUTH_REQUIRE_SESSION_SNAPSHOT=1. + +End to end through handle_session_start and handle_stop with a fake first-write-wins +daemon, as in tests/test_tier2_out_of_workspace_roots.py. No docker or live daemon. +""" + +import os +import sys +import json +import hashlib +import subprocess +import time +import unittest +from unittest import mock + +REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) +if REPO_ROOT not in sys.path: + sys.path.insert(0, REPO_ROOT) +TESTS_DIR = os.path.dirname(os.path.abspath(__file__)) +if TESTS_DIR not in sys.path: + sys.path.insert(0, TESTS_DIR) + +import test_tier2_out_of_workspace_roots as oow # module import: pytest must not re-collect its classes +from test_tier2_out_of_workspace_roots import _commit, _git_init, _write, TEST_BODY +import client.hardtruth_hook as hardtruth_hook + + +class TestStopHookInWorkspaceRepo(oow.TestStopHookOutsideRepos): + """The session's cwd is the repository it edits. Inherits the fake daemon, _stop and + _session_start; the outside-repo tests are not re-run here.""" + + def setUp(self): + super().setUp() + self._old["HARDTRUTH_REQUIRE_SESSION_SNAPSHOT"] = os.environ.pop("HARDTRUTH_REQUIRE_SESSION_SNAPSHOT", None) + os.rmdir(self.workspace) + self.repo = self._repo(os.path.basename(self.workspace), parent=os.path.dirname(self.workspace)) + self.app = os.path.join(self.repo, "app.py") + + def _post_tool_use_baseline(self): + """What PostToolUse does for a tool's cwd (get_or_set_session_baseline).""" + with mock.patch.object(hardtruth_hook, "call_system_one", side_effect=self._fake_call): + return hardtruth_hook.get_or_set_session_baseline(self.repo, self.conv) + + def _edit(self): + _write(self.app, "def f():\n return 'broken'\n") + + # --- the snapshot is the baseline --------------------------------------------------- + + def test_clean_in_workspace_edit_is_verified_and_allowed(self): + self._session_start() + _write(self.app, "def f():\n return 2\n") + res = self._stop([self.app]) + self.assertEqual(res.get("decision"), "allow", res) + self.assertEqual(self.handed_off, [self.repo]) + + def test_shell_commit_of_conftest_tamper_before_first_edit_halts(self): + """r3 M1: the lazy baseline is recorded after the tamper commit; the snapshot is not. + (SessionStart seeds the halt-dir cache with the cwd's HEAD, but the agent can delete it.)""" + self._session_start() + tampered = self._tamper_conftest(self.repo) + os.remove(hardtruth_hook.get_session_start_baseline_file(self.conv)) + self.assertEqual(self._post_tool_use_baseline(), tampered) # the lazy baseline is chosen by the agent + self._edit() + self._assert_halted_on_conftest(self._stop([self.app])) + + def test_planted_baseline_ref_and_cache_files_are_ignored(self): + self._session_start() + tampered = self._tamper_conftest(self.repo) + conv_hash = hashlib.sha256(self.conv.encode("utf-8")).hexdigest()[:16] + subprocess.run(["git", "update-ref", f"refs/hardtruth/baseline/{conv_hash}", tampered], cwd=self.repo, check=True) + self._post_tool_use_baseline() + cache_files = [n for n in os.listdir(self.halt_dir) if n.startswith("baseline_")] + self.assertTrue(cache_files) + for name in cache_files: + with open(os.path.join(self.halt_dir, name), "w") as f: + json.dump({"baseline_sha": tampered, "workspace_path": self.repo}, f) + self._edit() + self._assert_halted_on_conftest(self._stop([self.app])) + + def test_backdated_tamper_commit_halts(self): + self._session_start() + self._tamper_conftest(self.repo, when=time.time() - 3600) + self._post_tool_use_baseline() + self._edit() + self._assert_halted_on_conftest(self._stop([self.app])) + + def test_committed_assertion_deletion_before_first_edit_halts(self): + """Phase 8 only scans files the session is known to have edited; the in-workspace + integrity check diffs the whole repository against the snapshot.""" + self._session_start() + _write(os.path.join(self.repo, "tests", "test_app.py"), "def test_a():\n assert f() is not None\n") + _commit(self.repo, "x") + self._post_tool_use_baseline() + _write(self.app, "def f():\n return 2\n") + res = self._stop([self.app]) + self.assertIn("Severe assertion deletion", res.get("reason", "")) + self.assertEqual(self.handed_off, []) + + def test_backdated_stash_halts(self): + """Phase 5 compares refs/stash with its session-start value, not the stash's %ct.""" + self._session_start() + _write(self.app, "def f():\n return 2\n") + _write(os.path.join(self.repo, "tests", "test_app.py"), "def test_a():\n pass\n") + env = {k: v for k, v in os.environ.items() if not k.startswith("GIT_")} + env["GIT_COMMITTER_DATE"] = env["GIT_AUTHOR_DATE"] = f"{int(time.time()) - 3600} +0000" + subprocess.run(["git", "stash", "push", "--", "tests/test_app.py"], cwd=self.repo, env=env, check=True, + stdout=subprocess.PIPE) + res = self._stop([self.app]) + self.assertIn("stash", res.get("reason", "")) + self.assertEqual(self.handed_off, []) + + def test_history_rewrite_below_snapshot_halts_despite_lazy_baseline(self): + """E4 against the snapshot: resetting to an older commit, then letting PostToolUse + record the lazy baseline there, does not move the lineage anchor.""" + _write(os.path.join(self.repo, "README.md"), "second\n") + _commit(self.repo, "second") + self._session_start() + subprocess.run(["git", "reset", "-q", "--hard", "HEAD~1"], cwd=self.repo, check=True) + self._post_tool_use_baseline() + _write(self.app, "def f():\n return 2\n") + res = self._stop([self.app]) + self.assertIn("EVASION E4", res.get("reason", "")) + self.assertEqual(self.handed_off, []) + + def test_in_workspace_subproject_target_uses_its_repository_snapshot(self): + """A Tier 2 target in a subdirectory of the workspace repository is checked against + the repository's snapshot, with manifests anchored at the git top-level.""" + sub = os.path.join(self.repo, "svc") + _write(os.path.join(sub, "pytest.ini"), "[pytest]\n") + _write(os.path.join(sub, "tests", "test_svc.py"), TEST_BODY) + _write(os.path.join(sub, "svc.py"), "def g():\n return 1\n") + _commit(self.repo, "svc") + self._session_start() + self._tamper_conftest(sub) + self._post_tool_use_baseline() + _write(os.path.join(sub, "svc.py"), "def g():\n return 'broken'\n") + self._assert_halted_on_conftest(self._stop([os.path.join(sub, "svc.py")])) + + # --- no snapshot: documented fallback --------------------------------------------- + + def test_without_snapshot_lazy_baseline_is_kept(self): + """No SessionStart (another harness, hook installed mid-session): today's behaviour. + The lazy baseline is recorded after the tamper commit, so this tamper is not seen; + that is the gap HARDTRUTH_REQUIRE_SESSION_SNAPSHOT=1 closes.""" + self._tamper_conftest(self.repo) + self._post_tool_use_baseline() + self._edit() + res = self._stop([self.app]) + self.assertEqual(res.get("decision"), "allow", res) + self.assertEqual(self.handed_off, [self.repo]) + + def test_without_snapshot_strict_mode_fails_closed(self): + os.environ["HARDTRUTH_REQUIRE_SESSION_SNAPSHOT"] = "1" + self._post_tool_use_baseline() + _write(self.app, "def f():\n return 2\n") + res = self._stop([self.app]) + self.assertIn("no session-start snapshot", res.get("reason", "")) + self.assertIn(self.repo, res.get("reason", "")) + self.assertEqual(self.handed_off, []) + + def test_strict_mode_with_snapshot_allows_clean_edit(self): + os.environ["HARDTRUTH_REQUIRE_SESSION_SNAPSHOT"] = "1" + self._session_start() + _write(self.app, "def f():\n return 2\n") + res = self._stop([self.app]) + self.assertEqual(res.get("decision"), "allow", res) + + def test_id_less_session_snapshot_is_never_trusted(self): + """Every id-less session shares one daemon key, so its snapshot may be another + session's HEAD (the anton-go worktree incident).""" + head = subprocess.run(["git", "rev-parse", "HEAD"], cwd=self.repo, check=True, + stdout=subprocess.PIPE, text=True).stdout.strip() + for cid in ("unknown", "default"): + self.daemon[(cid, hardtruth_hook.SESSION_START_HEAD_NS + self.repo)] = head + with mock.patch.object(hardtruth_hook, "call_system_one", side_effect=self._fake_call): + self.assertIsNone(hardtruth_hook.trusted_repo_baseline(self.repo, cid)) + with mock.patch.object(hardtruth_hook, "call_system_one", side_effect=self._fake_call): + hardtruth_hook.snapshot_repos_at_session_start(self.conv, self.repo) + self.assertEqual(hardtruth_hook.trusted_repo_baseline(self.repo, self.conv), head) + + +# The inherited outside-repo tests already run in their own module. +for _name in [n for n in dir(oow.TestStopHookOutsideRepos) if n.startswith("test_")]: + if _name not in TestStopHookInWorkspaceRepo.__dict__: + setattr(TestStopHookInWorkspaceRepo, _name, None) + + +class TestSessionStartSnapshotBudget(unittest.TestCase): + """delta LOW-1: a worker running at the deadline makes two sequential daemon calls + (HEAD, then stash), so the hook process outlives the budget by up to two call timeouts.""" + + def test_in_flight_worker_finishes_both_calls_after_the_deadline(self): + import tempfile + import shutil + test_dir = os.path.realpath(tempfile.mkdtemp(prefix="hardtruth-budget-")) + self.addCleanup(shutil.rmtree, test_dir, True) + repo = os.path.join(test_dir, "r") + _git_init(repo) + _write(os.path.join(repo, "a.py"), "x = 1\n") + _commit(repo) + ends = [] + + def slow(endpoint, data, timeout=None): + time.sleep(1.2) # under the 1.5s per-call timeout + ends.append(time.time()) + return {"baseline_sha": data.get("commit_sha")} + + with mock.patch.dict(os.environ, {"HARDTRUTH_BASELINE_ROOTS": ""}), \ + mock.patch.object(hardtruth_hook, "call_system_one", side_effect=slow): + t0 = time.time() + n = hardtruth_hook.snapshot_repos_at_session_start("budget-conv", repo, time_budget_sec=0.3) + returned = time.time() - t0 + time.sleep(3.0) + self.assertEqual(n, 0) + self.assertLess(returned, 1.0) + self.assertEqual(len(ends), 2) + self.assertGreater(ends[-1] - t0, 0.3 + 1.5) # longer than one in-flight call + self.assertLess(ends[-1] - t0, 0.3 + 3.0 + 0.5) # within two + + +if __name__ == "__main__": + unittest.main()