Skip to content

refactor(hooks): audit at hook entry points so tests stay off $HOME - #3645

Closed
xantorres wants to merge 1 commit into
rtk-ai:developfrom
xantorres:fix/hook-audit-log-test-hygiene
Closed

xantorres wants to merge 1 commit into
rtk-ai:developfrom
xantorres:fix/hook-audit-log-test-hygiene

Conversation

@xantorres

Copy link
Copy Markdown

Four pre-existing test-hygiene defects in src/hooks/hook_cmd.rs. Hook responses, audit records and exit paths are unchanged.

audit_log ran inside the *_response_from_decision builders, which are exactly what the unit tests call. With RTK_HOOK_AUDIT=1 exported, roughly 30 hook tests appended to the real ~/.local/share/rtk/hook-audit.log. Each hook entry point now logs the decision once and the builders are pure, so the cmd argument they only carried for logging is gone. Gemini keeps its own action mapping rather than being folded into the shared one.

audit_log_to_dir takes its directory as a parameter, mirroring check_command_with_rules and evaluate_with_verdict. The audit test now exercises that real create/open/sanitize/write path, which had no coverage anywhere: the old test hand-rolled its own OpenOptions write and asserted against its own literal. Its temp dir is also process-id suffixed now, as heal_legacy_hook_file already does, so two concurrent cargo test processes can't delete each other's file mid-write.

Dropped test_audit_log_silent_when_disabled: it asserted nothing and cleared RTK_HOOK_AUDIT process-wide with no restore and no synchronization while ~30 sibling tests read it concurrently, silently disabling auditing for the rest of the run.

Added coverage for run_gemini's own payload parse, tool-name gate and dispatch, which had no direct test — the Gemini tests went through a test-local helper that reimplemented the dispatch. That helper now delegates to the production renderer.

Verified with cargo fmt --all && cargo clippy --all-targets && cargo test --all (clean, zero warnings), and by running the hook tests with RTK_HOOK_AUDIT=1 and HOME pointed at an empty scratch directory, which stays empty.

audit_log ran inside the *_response_from_decision builders, which are
exactly what the unit tests call. On a machine with RTK_HOOK_AUDIT=1
exported, roughly 30 hook tests appended to the real
~/.local/share/rtk/hook-audit.log. Each entry point now logs the decision
once and the builders are pure, so the command argument they only carried
for logging is gone. Gemini keeps its own action mapping (ask vs rewrite,
silent on deny) rather than being folded into the shared one.

The audit writer takes its directory as a parameter, mirroring
check_command_with_rules and evaluate_with_verdict, so a test can exercise
the real create/open/sanitize/write path. The previous test hand-rolled its
own OpenOptions write and asserted against its own literal, leaving that
path with no coverage anywhere. It also named its temp dir with a fixed
string shared by every concurrent cargo test process, where one run's
cleanup can delete another's file mid-write; it now carries a process-id
suffix, as heal_legacy_hook_file already does for its own temp file.

Drop test_audit_log_silent_when_disabled. It asserted nothing, and cleared
RTK_HOOK_AUDIT process-wide with no restore and no synchronization while
~30 sibling tests read that variable concurrently — unsynchronized env
mutation is why set_var/remove_var became unsafe in the 2024 edition, and
it silently disabled auditing for the rest of the run.

Cover run_gemini's own payload parse, tool-name gate and dispatch, which
had no direct test: the Gemini tests all went through a test-local helper
that reimplemented the dispatch. That helper now delegates to the
production renderer.
@KuSh

KuSh commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator

Thank you for tracking these down. #3758 (merged 2026-10-01) moved the hook audit log onto the test-isolated home directory, so hook tests no longer write to ~/.local/share/rtk, made test_audit_log_format_four_fields drive the real audit write, and scoped RTK_HOOK_AUDIT per test, while #3751 (merged 2026-08-30) already extracted run_gemini_inner and run_gemini_inner_with_rules with a different signature, so the restructuring here no longer fits the current code. Develop still has no test for the Gemini hook's tool-name gate or its empty-command passthrough in run_gemini_inner_impl (src/hooks/hook_cmd.rs); an issue or a narrow test PR there would be welcome. The test-only gemini_render helper also still reimplements the dispatch instead of going through run_gemini_inner_impl, which would fit the same narrow PR. If I missed something this PR still covers, please comment and it can be reopened.

@KuSh KuSh closed this Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants