From 34315320bcc61b7c7a9a1480f6b4180fb8914a0b Mon Sep 17 00:00:00 2001 From: Chris Graf Date: Wed, 1 Jul 2026 22:02:57 -0500 Subject: [PATCH] Keep the classifier cache in the test tmpdir so the suite is hermetic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary Scrub and re-point `XDG_CACHE_HOME` in the hook test harness so the protected-bare classifier cache no longer writes into the developer's real `~/.cache` during a test run. The harness already redirects hook state (`XDG_RUNTIME_DIR`/ `XDG_STATE_HOME`) at the test tmpdir, but `_protected_bare_nested_cache_file` resolves its cache from `XDG_CACHE_HOME` (falling back to `~/.cache`), which the harness left untouched. So running the suite rewrote a real `~/.cache/agentguard/protected-bare-nested-roots-*` file — a pre-existing hermeticity leak surfaced during review of the hook-state change. - add `XDG_CACHE_HOME` to the ambient XDG scrub and export it at `$TEST_TMPDIR` alongside `XDG_RUNTIME_DIR`, matching the same pattern and rationale Testing - before/after check: the real `~/.cache/agentguard` mtime is unchanged across a full suite run (was rewritten at test time before this change) - full suite green (`./test/agentguard-test`: ok, all suites 0 failed); `checkrun lint` and `shellcheck -x` clean - no test asserts the classifier cache path, so redirecting it is safe --- test/suites/agent-hook-helpers.sh | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/test/suites/agent-hook-helpers.sh b/test/suites/agent-hook-helpers.sh index f7f558e..985ea29 100644 --- a/test/suites/agent-hook-helpers.sh +++ b/test/suites/agent-hook-helpers.sh @@ -16,11 +16,12 @@ export AGENTGUARD_PROCESS_DETECT=0 # it explicitly in their own subshell. unset CLAUDE_CODE_SESSION_ID CLAUDE_CODE_CURRENT_SESSION_ID \ CODEX_THREAD_ID CODEX_INTERNAL_ORIGINATOR_OVERRIDE GEMINI_PROJECT_DIR -# The hook state root resolves from XDG_RUNTIME_DIR/XDG_STATE_HOME. Scrub the -# ambient values (devservers set XDG_RUNTIME_DIR=/run/user/) so nothing -# leaks into the real per-user state; the harness re-points XDG_RUNTIME_DIR at -# the test tmpdir once TEST_TMPDIR exists (see below). -unset XDG_RUNTIME_DIR XDG_STATE_HOME +# The hook state root resolves from XDG_RUNTIME_DIR/XDG_STATE_HOME, and the +# protected-bare classifier cache resolves from XDG_CACHE_HOME. Scrub the ambient +# values (devservers set XDG_RUNTIME_DIR=/run/user/) so nothing leaks into +# the real per-user state or cache; the harness re-points them at the test tmpdir +# once TEST_TMPDIR exists (see below). +unset XDG_RUNTIME_DIR XDG_STATE_HOME XDG_CACHE_HOME # Dotfiles and other consumers may tune or bypass edit-churn globally. Test # default behavior against repo defaults; individual tests set custom values # where that contract is under test. @@ -54,10 +55,11 @@ HELPERS="$AGENTGUARD_ROOT/lib/agentguard/hook-helpers.sh" TEST_TMPDIR=$(_tmpdir) TEST_SID="agent-hook-test-$$" -# Point the hook state root at the test tmpdir for every fixture and child -# process (hooks resolve state from XDG_RUNTIME_DIR first). Exporting once here -# keeps both spawned hooks and the test process's own _hook_* helpers hermetic, -# so nothing writes into the real ~/.local/state. +# Point the hook state root and the classifier cache at the test tmpdir for every +# fixture and child process (hooks resolve state from XDG_RUNTIME_DIR first; the +# protected-bare classifier cache resolves from XDG_CACHE_HOME). Exporting once +# here keeps both spawned hooks and the test process's own _hook_* helpers +# hermetic, so nothing writes into the real ~/.local/state or ~/.cache. # # Hermeticity contract for new fixtures: rely on this inherited export. A fixture # that unsets/overrides XDG_RUNTIME_DIR (e.g. to exercise a fallback tier) must @@ -65,6 +67,7 @@ TEST_SID="agent-hook-test-$$" # temp dir (`_mock_home`) so a resolved ~/.local/state still can't reach the real # home. Never run a hook that writes state with the ambient HOME and no XDG root. export XDG_RUNTIME_DIR="$TEST_TMPDIR" +export XDG_CACHE_HOME="$TEST_TMPDIR" PRE_MCP="$BIN_DIR/agent-hook-pre-mcp" POST_MCP="$BIN_DIR/agent-hook-post-mcp"