Skip to content

fix(hooks/gateguard): close destructive-detector gaps, stop gating scratch cleanup - #2624

Closed
rbotte wants to merge 1 commit into
affaan-m:mainfrom
rbotte:fix/gateguard-destructive-detector-precision
Closed

rbotte wants to merge 1 commit into
affaan-m:mainfrom
rbotte:fix/gateguard-destructive-detector-precision

Conversation

@rbotte

@rbotte rbotte commented Jul 29, 2026 •

Copy link
Copy Markdown

The destructive-command gate had drifted in both directions: it missed
several operations that really do destroy, and it fired on prose and on
throwaway directories.

Coverage gaps closed:

  • dd could never fire on a real invocation. /\bdd\s+if=\b/ needs a
    word character after =, but every real use continues with a path, so
    dd if=/dev/zero of=/dev/sda did NOT match while the inert dd if=x
    did. of= — the operand that actually writes — was never matched at
    all. dd now has a command-position rule covering both operands.
  • rm -r without -f was waved through: the rule required BOTH flags,
    so a recursive delete of a source tree passed unchallenged.
  • find -delete had no rule; only -exec <destroyer> was covered.
  • shred, a standalone unlink, and the coreutils truncate had no
    rule. unlink was recognised only inside find -exec.

Precision fixes:

  • truncate was matched as a bare word, so every mention of it was
    destructive: the Jinja/Django filter of the same name, a branch name,
    a file name, a line of prose in a commit body. SQL truncation always
    names a table, so the pair is now required; the coreutils binary is
    caught in command position instead.
  • Delete targets that resolve to scratch space, build caches,
    virtualenvs or test artefacts no longer raise the gate. Bare /tmp is
    deliberately excluded, so rm -rf /tmp/junk is still challenged.

Supporting changes:

  • stripQuotedStrings can now stash quoted literals in a slot table
    instead of blanking them. A quoted argument still tokenizes as one
    opaque token, so git commit -m '(rm -rf x)' stays inert, but the
    literal is recoverable — without it rm -rf "$SCRATCH/build" cannot
    be judged at all. Both quote styles are matched in one left-to-right
    alternation rather than two sequential passes, which is how the shell
    reads mixed quoting.
  • NAME=value assignments in the same command line are resolved before
    a target is judged, since scripts define the scratch root in the very
    command that deletes from it. $(mktemp -d) is treated as disposable.
    A target still carrying an unresolved $VAR, a bare root, or any
    operand that is not clearly derived keeps the gate loud.
  • isDestructiveQuoteAware now receives the same context and the new
    rules. Without this the disposable-target decision made in the main
    pass was silently reversed by the later one, and sh -c could have
    bypassed the additions. The GHSA-4v57-ph3x-gf55 regression tests still
    pass.

Measured against 20,737 distinct Bash commands from 14 days of real
agent traffic: denials fall from 273 to 138 (-49%) with zero commands
newly challenged. Every one of the 41 distinct paths that stopped
raising the gate is a scratchpad, mktemp directory, .venv,
test-results, .playwright-mcp or gitignored plan directory.

19 tests added; suite goes 144 -> 163, all passing.

…ratch cleanup

The destructive-command gate had drifted in both directions: it missed
several operations that really do destroy, and it fired on prose and on
throwaway directories.

Coverage gaps closed:

- `dd` could never fire on a real invocation. `/\bdd\s+if=\b/` needs a
  word character after `=`, but every real use continues with a path, so
  `dd if=/dev/zero of=/dev/sda` did NOT match while the inert `dd if=x`
  did. `of=` — the operand that actually writes — was never matched at
  all. `dd` now has a command-position rule covering both operands.
- `rm -r` without `-f` was waved through: the rule required BOTH flags,
  so a recursive delete of a source tree passed unchallenged.
- `find -delete` had no rule; only `-exec <destroyer>` was covered.
- `shred`, a standalone `unlink`, and the coreutils `truncate` had no
  rule. `unlink` was recognised only inside `find -exec`.

Precision fixes:

- `truncate` was matched as a bare word, so every mention of it was
  destructive: the Jinja/Django filter of the same name, a branch name,
  a file name, a line of prose in a commit body. SQL truncation always
  names a table, so the pair is now required; the coreutils binary is
  caught in command position instead.
- Delete targets that resolve to scratch space, build caches,
  virtualenvs or test artefacts no longer raise the gate. Bare `/tmp` is
  deliberately excluded, so `rm -rf /tmp/junk` is still challenged.

Supporting changes:

- `stripQuotedStrings` can now stash quoted literals in a slot table
  instead of blanking them. A quoted argument still tokenizes as one
  opaque token, so `git commit -m '(rm -rf x)'` stays inert, but the
  literal is recoverable — without it `rm -rf "$SCRATCH/build"` cannot
  be judged at all. Both quote styles are matched in one left-to-right
  alternation rather than two sequential passes, which is how the shell
  reads mixed quoting.
- `NAME=value` assignments in the same command line are resolved before
  a target is judged, since scripts define the scratch root in the very
  command that deletes from it. `$(mktemp -d)` is treated as disposable.
  A target still carrying an unresolved `$VAR`, a bare root, or any
  operand that is not clearly derived keeps the gate loud.
- `isDestructiveQuoteAware` now receives the same context and the new
  rules. Without this the disposable-target decision made in the main
  pass was silently reversed by the later one, and `sh -c` could have
  bypassed the additions. The GHSA-4v57-ph3x-gf55 regression tests still
  pass.

Measured against 20,737 distinct Bash commands from 14 days of real
agent traffic: denials fall from 273 to 138 (-49%) with zero commands
newly challenged. Every one of the 41 distinct paths that stopped
raising the gate is a scratchpad, mktemp directory, .venv,
test-results, .playwright-mcp or gitignored plan directory.

19 tests added; suite goes 144 -> 163, all passing.
@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 26cc6c4a-cbd1-4d19-bda3-a69405450c50

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@rbotte

rbotte commented Jul 29, 2026

Copy link
Copy Markdown
Author

Separate from this PR's diff, the same measurement turned up something structural that I think is worth a maintainer's opinion before anyone writes code for it. Filing it here rather than as a patch, because the fix is an architecture choice, not a detector tweak.

The retry key is set by the denial itself

In all three gates, markChecked(key) runs before denyResult(...):

if (!isChecked(key)) {
  if (!markChecked(key)) return allowWithStateWarning();
  return denyResult(...);   // key already recorded above
}
return rawInput;             // second attempt: allowed

So the second attempt always succeeds, whether or not a single fact was presented. A PreToolUse hook only ever sees the tool input — never the assistant's reply — so nothing at this event can actually read the facts the message asks for. "Present the facts, then retry" is satisfied by the retry alone.

Two consequences, both measured over 14 days of one operator's real traffic (2,605 denials, 120 sessions):

  1. Byte-identical resubmission passes. 37 of 139 destructive denials (27%) were followed by the exact same command, which then ran.
  2. Refining a command re-blocks it. The destructive key is sha256(command), so editing the command — adding a flag, narrowing a path, fixing a typo — produces a new key and a fresh denial. The gate is cheaper to satisfy by repeating than by thinking, which is the opposite of the intent.

The first-touch gates have the same shape with a per-path key, so the cost is per file rather than per command: 2,454 of the 2,605 denials (94.6%) came from those two gates, and none of them blocked anything a second unchanged attempt did not immediately perform.

A possible direction

The signal for "did it investigate" is not text — it is an action, and actions are observable at PostToolUse. Recording Read/Grep/Glob calls there, and allowing a retry only when at least one of them touched the target since the denial, would turn "be denied once" into "actually go and look". Still gameable, but it demands a verifiable act instead of a ceremonial paragraph, and it inverts the incentive: refining stops being punished.

That is a bigger change than this PR, and it interacts with GATEGUARD_FACT_FORCE_FULL_DENIALS, GATEGUARD_EXEMPT_GLOBS and GATEGUARD_BASH_ROUTINE_DISABLED, which already give operators an escape hatch for the friction. Happy to prototype it if you think the direction is right — or to drop it if you would rather the gate stay a deliberate speed bump, which is a legitimate design choice as long as the docs do not promise fact verification.

@github-actions

Copy link
Copy Markdown

This PR is stale due to inactivity.

@github-actions github-actions Bot added the stale No activity for 30+ days label Aug 31, 2026
@github-actions github-actions Bot closed this Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale No activity for 30+ days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant