Skip to content

perf(guardrails): resolve delete targets with fewer processes - #13441

Merged
kyle-sexton merged 6 commits into
mainfrom
fix/13276-delete-guard-spawns
Nov 5, 2026
Merged

kyle-sexton merged 6 commits into
mainfrom
fix/13276-delete-guard-spawns

Conversation

@kyle-sexton

Copy link
Copy Markdown
Member

Closes #13276

Summary

On Windows, a judged delete in block-root-delete-target (rm -rf, mv, find | xargs rm, eval/watch/wsl/cmd /c, a shell fed by echo or a heredoc) started 8 processes. This cuts it to 1 and leaves every verdict the guard returns unchanged. The measured saving is 126 to 152 ms per call.

Fix

  • Every host: a bound inside the shell replaces timeout. rdt_bounded_to (new block-root-delete-target-resolve.sh) puts the bound inside the command substitution. The command holds a spare descriptor on a process substitution, which reaches end of file only when the command exits. read -t waits on it, and on expiry the command is killed and reaped. The guard refuses as too-slow, as before. This removes the 3 timeout processes and the no-timeout branch (owner answer Q6 in the interview).
  • Windows Git Bash: realpath and cygpath answered in the shell where that is exact.
    • rdt_phys_to stands in for realpath -m: a lexical collapse, valid only when no part of the path is a symlink or junction.
    • rdt_win_to stands in for cygpath -l -m: one cd -P into the nearest existing ancestor.
    • Both decline, and the path takes the process, for: a ~ (8.3 name), a non-ASCII byte, a component ending in . or a space, a UNC path, a drive /proc/mounts does not list as local NTFS or ReFS, and a .. below a missing part. rdt_win_to also declines any path holding a link, because cygpath does not follow a link in the middle of a path (measured).
    • This is the owner-approved revision of interview answer Q7: one cd -P per path instead of per component, plus the extra fallbacks.
  • Temp roots are read with --no-drive. The drive spellings only named candidates the guard already resolves; on Git Bash the 3 candidates resolve to one set.
  • CI: the new suite is a step of test-windows-guards in pr-test-platforms.yml, with the pinned shfmt install it needs.

Test changes the PR reviewer should know about:

  • The timeout-stub cases in block-root-delete-target.test.sh now use slow realpath and git stand-ins started with SECONDS=44 in the guard's environment. Bash imports SECONDS, so the hook starts 44 s into its 50 s bound and the case refuses in about 6 s, with no test switch in the guard.
  • Three assertions pinned the no-timeout branch, which Q6 removed: "with no timeout an ordinary delete in the tree is allowed", "with no timeout an escape from the tree still blocks", and "with no timeout the deadline is checked after the batch call". The first two verdicts are still covered by ordinary in-tree and escape cases in the same suite.
  • The slow-realpath case and the judgment-error case now name a path with a ~ (./x~1/build, build~1). Otherwise Git Bash resolves the path in the shell and the batch they test never runs. The expected verdicts are unchanged.

Verification

  • Process census (Git Bash, melo-lap-001): rm -rf ./build from a repository logs git alone. Main logged 8: two cygpath, then git, realpath and cygpath, each under timeout.
  • Timing (Git Bash, feat(guardrails): scope xargs and parallel deletes to allowed roots #9754 method): main (3bb182755) and the branch staged side by side, each payload timed in one hyperfine -N session, 5 alternating blocks of 40 runs per arm. Every judged payload was lower in all 5 blocks, by 126.4 to 152.4 ms; rm -rf ./build went from 369.9 to 221.1 ms. The common call moved +0.6 ms (branch lower in 2 of 5 blocks). The figures are in the README's Hook budget accounting entry. All 20 verdicts matched (exit 0 on both arms for every payload).
  • Linux, branch:
    • block-root-delete-target.test.sh PASS=1568 FAIL=0 and -e PASS=789 FAIL=0.
    • -b, -c and -d fail 87, 8 and 16 cases; main at 3bb182755 fails the same sets on this host. Those are the fake-HOME fixtures, where the mise shim for shfmt cannot read its config.
    • run-guards.test.sh PASS=729.
    • block-root-delete-target-resolve.test.sh PASS=28 SKIP=1 (the differential needs Git Bash).
  • Git Bash, branch:
    • block-root-delete-target.test.sh PASS=1582 FAIL=0; -b, -c and -d 0 failures.
    • -e fails the same 2 message-text cases main fails on Windows ("attended: the reason names the outside-tree delete", "unwatched: the reason names the target").
    • block-root-delete-target-resolve.test.sh PASS=115 FAIL=0. That includes the comparison against realpath/cygpath over symlink, junction, 8.3, --named, trailing-dot, non-ASCII, mixed-case, drive-spelled, /tmp and UNC paths, and the temp-root set check.
  • Timing gate for the bound: an in-shell read -d '' form cost about 46 µs per byte on Git Bash (3 s against 0.6 s at 512 paths), so the bound moved inside $(...). That form is level with a bare $(realpath …) and never slower than $(timeout …).
  • Repo gates: scripts/plan-test-lanes.test.sh PASS=58, check-silent-skips.sh and check-discriminating-test-skips.sh exit 0, check-shell-portability.sh 0 hits, shellcheck and shfmt clean on the new files, markdownlint clean on the README.

Related

🤖 Generated with Claude Code

kyle-sexton and others added 5 commits November 4, 2026 16:49
block-root-delete-target bounds realpath, cygpath and git inside the
shell instead of under timeout: the bound runs in the command
substitution, a spare descriptor on a process substitution reaches end
of file when the command exits, and read -t waits on it. That removes
the 3 timeout processes on every host and the no-timeout branch.

On Windows Git Bash, rdt_phys_to and rdt_win_to answer realpath -m and
cygpath -l -m in the shell for paths with no symlink, junction, short
name, non-ASCII name, trailing dot or space, or network drive; every
other path still takes the process. The temp roots are read without
their drive spellings, which named candidates the guard resolves
itself. An ordinary rm -rf ./build now starts 1 process (git) on Git
Bash instead of 8.

block-root-delete-target-resolve.test.sh checks the bound against
$(...) and its kill, the too-slow refusal through the whole guard, and
on Git Bash the in-shell answers against the processes. It runs as a
step of the Windows guards job.

Refs: #13276

Co-Authored-By: Claude Opus 5.5 <[email protected]>
On Git Bash the shell now resolves an ordinary operand, so the slow
realpath case, its resolve-suite twin and the judgment-error case named
paths that never reached the realpath batch. Each now names a parent or
target with a `~`, which the in-shell resolver declines on every host.
rdt_bounded_to reaps the command it kills, so no zombie outlives the
call.

Refs: #13276

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Hook budget accounting gains the #13276 entry. On Windows Git Bash,
`rm -rf ./build` now starts 1 process instead of 8. Every judged payload
is 126 to 152 ms faster, lower in all 5 alternating blocks, and the
common call is unchanged. The entry also states the remaining overage
and that the Windows differential lane is informational. A
portability-ok note marks a literal backslash in the resolve suite's
printf data.

Refs: #13276

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Both files start with a shebang, which the exec-bit check requires to
carry mode 755. The suite's Windows comparison now takes the helper's
status directly instead of through a subshell variable.

Refs: #13276

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kyle-sexton
kyle-sexton marked this pull request as ready for review November 5, 2026 05:39
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-05T05:42:05.129015Z 3f1903f Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3f1903f909

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

claude-review: 1 inline finding(s) on 3f1903f9098c557973889eb3fe7bc31648e5bc9a; the summary is in this lane's pull request comment.

claude-lane: claude-review

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

claude-review reviewed 47b64d9fbedf8c84186ccedb1c0e21a06af59b60: read 2 of 2 changed file(s); 0 finding(s), 0 as inline comments.

Code review (incremental, 3f1903f → 47b64d9): no blocking findings

I read every hunk of both files in the incremental scope: plugins/guardrails/hooks/block-root-delete-target-resolve.sh and plugins/guardrails/hooks/block-root-delete-target-resolve.test.sh. I also read the whole helper file at head and the resolve suite around the changed declines block. This lane already reviewed an earlier head (blocking only: yes), so this review reports only blocking findings and drops advisory ones.

What changed

  • Killing the whole process tree on expiry. rdt_bounded_to now calls the new rdt_kill_tree, which runs these steps:

    • It stops the command.
    • It builds a parent-to-children map from /proc/[0-9]*/stat using builtins only. It takes the ppid after the last ) , so a comm that contains ) still parses correctly.
    • It kills the command and everything under it.
    • On a host with no /proc (macOS), the glob matches nothing, read fails, and only the command is killed, as before.

    This fixes the settled thread about a child started without exec holding the substitution open. The new suite case pins it: a wrapper that backgrounds sleep 60 must return 124 within 3 s, and its child must be gone.

  • Checking the drive before touching the disk. rdt_cdp_plain is the first step of both rdt_phys_to and rdt_win_to. It now calls rdt_local_path before any -L, -e or cd -P. That function takes the drive in one of two ways:

    • from the spelling (/x/…, X:/…);
    • otherwise from the longest /proc/mounts mount point that holds the path. A UNC source maps to an empty drive, so the path is declined.

    This fixes this lane's earlier inline finding (discussion_r4477035280) and the matching settled thread. The RDT_FIXED_DRIVES comment's guarantee now holds for the paths it names.

  • Test declines. The suite now has these decline cases:

    • An unmapped drive letter (X:/rdt-x, /x/rdt-x) is declined by both helpers.
    • The symlink declines are guarded by -L, with a visible SKIP line when ln -s made copies.
    • The junction decline is guarded by -L "$W/junc".

Prior findings

  • The cd -P-before-fixed-drive inline finding: fixed by this push (the check now runs in rdt_cdp_plain before any disk access), so it is not re-raised.
  • The earlier summary finding (no test would fail if the Windows process count went back up) was advisory. In blocking-only mode it is not restated, and the incremental change does not touch it.

Notes (non-blocking, not counted)

  • In rdt_kill_tree, read … <"$__kt_f" 2>/dev/null applies the < redirection before 2>/dev/null. So if a process exits between the glob and the read, bash's "No such file or directory" message still reaches the hook's stderr. It is cosmetic only: the loop continues correctly.
  • The junction decline is added with no SKIP line when mklink /J did not create the junction. The link declines do print one.

No plugin manifest changed in this push. No embedded instructions were found in the PR context, settled-threads or prior-findings files.

Spend: the two new suite cases add about 1 to 3 s to the Windows test-windows-guards job per run (hosted Actions minutes on the existing runner).

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

claude-security-review: 1 inline finding(s) on 3f1903f9098c557973889eb3fe7bc31648e5bc9a; the summary is in this lane's pull request comment.

claude-lane: claude-security-review

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

claude-security-review reviewed 47b64d9fbedf8c84186ccedb1c0e21a06af59b60: read 2 of 2 changed file(s); 0 finding(s), 0 as inline comments.

Security review: PR #13441, incremental since 3f1903f9

critical-findings: 0

Scope. This was an incremental review covering the 2 files in .claude-lane/incremental-files.txt: plugins/guardrails/hooks/block-root-delete-target-resolve.sh and its .test.sh. I read every hunk of both. This lane reviewed an earlier head, so the review was blocking only: it reports CRITICAL and IMPORTANT findings and drops SUGGESTIONs. No changed file matches a trusted-path glob. The push changes no plugin manifest, trust control, token scope, kill switch or merge hold, and it deletes no standing instruction.

No security issues found in this push.

The prior finding (stalled network drive accessed without a bound) is fixed. It is also a settled thread with lines changed since resolution: no, so this review does not raise it again. I checked the fix at head:

  • rdt_cdp_plain calls rdt_local_path (L83) before any disk access.
  • rdt_phys_to calls rdt_cdp_plain first (L100), before the -L/-e walk at L120-121.
  • rdt_win_to calls rdt_phys_to first (L182), before its -e loop and cd -P.

A path on an unlisted or network drive now returns 1 and goes to the bounded realpath/cygpath batch. This change only adds ways to decline, and every decline takes the bounded process, so it fails closed.

Other code in this push, checked with no issue found:

  • rdt_kill_tree (L51-L68):
    • A hostile process name cannot plant a parent id. ${l##*) } strips through the last ) , so it removes the whole command name, and the parent id must also match ^[0-9]+$.
    • The pids it kills are only the bounded command and the processes under it.
    • A process stopped with -STOP is also in the -KILL list, so the wait at L38 cannot hang on it.
    • This makes the time bound stricter than before; it loosens nothing.
  • rdt_read_mounts / rdt_local_path: for paths not spelled with a drive (such as /tmp or /), the drive is taken from the longest matching mount point. A mount whose source is not a drive letter maps to an empty drive, and rdt_fixed_drive rejects that.
  • Test changes: test-only. The free-drive probe and the symlink-capability skip add no surface.

Spend: nothing new in this push.

Settled threads: none raised again. Prior findings: the one inline finding (stalled network drive) is resolved, and its lines changed, so it is not restated.

…ves off the shell

Review findings on #13441:

- An expired rdt_bounded_to now stops the command, then kills it and
  every process under it (rdt_kill_tree, from /proc/<pid>/stat parent
  ids). A child started without exec had kept the substitution open: a
  1 s bound over `bash -c 'sleep 3'` returned after 3 s.
- rdt_phys_to and rdt_win_to check that a path's drive is a local fixed
  one before any disk access: from its /x/ or X:/ spelling, or from the
  longest /proc/mounts mount point holding it. A network drive goes to
  the bounded process without being touched in the guard's own shell.
- The resolve suite pins the link declines only where `ln -s` made a
  link (the Windows runner's Git Bash makes a copy), adds a declined
  path on an unmapped drive, and adds a wrapper whose child is not
  exec'd.

Refs: #13276

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kyle-sexton
kyle-sexton added this pull request to the merge queue Nov 5, 2026
Merged via the queue into main with commit 30a55c0 Nov 5, 2026
69 checks passed
@kyle-sexton
kyle-sexton deleted the fix/13276-delete-guard-spawns branch November 5, 2026 06:04
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.

perf(guardrails): cut the processes a judged delete starts in block-root-delete-target

1 participant