Skip to content

perf(disk-hygiene): Stop-event guard_launch_monitor times out 52x via run-python-hook.sh launcher overhead #3516

Closed

Description

@kyle-sexton

Summary

Child of #3508. This issue was originally filed against guardrails with shell-oriented criteria. Both were wrong — the hook is disk-hygiene's and it is Python. Corrected below. A fix is already in flight, so this is close to a verify-and-close.

The hook

Plugin disk-hygiene
Hook plugins/disk-hygiene/skills/clean/scripts/guard_launch_monitor.py
Launcher plugins/disk-hygiene/hooks/run-python-hook.sh
Registration plugins/disk-hygiene/hooks/hooks.json, Stop event
Configured timeout 20 s
Timeouts in window 52
Average recorded duration 31.8 s

Root cause and status

The cost was launcher overhead, not the hook body. run-python-hook.sh re-derived its Python interpreter on every invocation: a sed read of a 3,500-line file, dirname, and an entire extra Python process spawned only to evaluate a sys.version_info >= MIN_PYTHON predicate. Four process spawns per warm call, of which one was the actual work.

Fixed on branch perf/disk-hygiene-guard-hook-spawns (4 spawns -> 1), measured with an interleaved A/B, 24 pairs alternating within one run so the host's ~6x drift hits both arms equally:

BEFORE  p50 = 5446 ms   p95 = 16991 ms
AFTER   p50 = 1418 ms   p95 =  7874 ms    median paired ratio 3.71x
AFTER   min =  124 ms

This hook is fixed as a side effect because it shares that launcher.

The bar it must still meet

docs/conventions/hook-budget/README.md sets the per-turn ceiling for the whole always-on Stop set at <= 500 ms parallel wall — shared with #3512 (claude-ops) and #3515 (autonomy). At AFTER p50 = 1418 ms this hook alone is still ~3x that ceiling, though the remaining wall-clock is dominated by the one irreducible interpreter spawn (AFTER min = 124 ms shows the floor when the host is briefly uncontended).

The convention also notes that interpreter choice is a budget decision — a Python Stop hook spends roughly a third of the whole per-turn budget on interpreter startup alone on the reference host.

Acceptance criteria

  • Confirm the timeout count drops after perf/disk-hygiene-guard-hook-spawns lands.
  • Spawn count on the warm path is 1, verified by PATH shim.
  • Measured share stated in the disk-hygiene README per hook-budget Rule 1.
  • The always-on per-turn set is re-measured across all three Stop hooks against the 500 ms ceiling, reported with the spawn-cost baseline.
  • Validated with scripts/affected-tests.sh --run.

Close as covered once that PR lands unless the re-measurement shows the per-turn set still over budget.

Note

Criteria in the original body ("collapse jq calls", "replace grep/sed with bash builtins") were written for a shell hook and do not apply to a Python one. The launcher they would apply to is already fixed.

Activity

  1. added
    agent-ready
    on Aug 31, 2026
  2. changed the title [-]perf(guardrails): Stop hook: silent destructive-guard failures averages 31.8s and timed out 52x - reduce process spawns[/-] [+]perf(disk-hygiene): Stop-event guard_launch_monitor times out 52x via run-python-hook.sh launcher overhead[/+] on Aug 31, 2026
  3. kyle-sexton commented on Aug 31, 2026

    @kyle-sexton
    MemberAuthor

    Misattributed on filing — this is disk-hygiene, not guardrails. Retitled. It is also already fixed.

    Corrected by the session that owns the file:

    • The hook is disk-hygiene's skills/clean/scripts/guard_launch_monitor.py, launched through disk-hygiene/hooks/run-python-hook.sh. It is not a guardrails hook.
    • Its 52 timeouts at avg 31.8 s are launcher overhead, the same root cause as the disk-hygiene destructive guard — the shell wrapper re-derived its Python interpreter on every invocation (a sed read of a 3,500-line file, plus an entire extra Python spawned only to evaluate a version predicate, plus dirname).
    • A fix is already in flight on branch perf/disk-hygiene-guard-hook-spawns, taking the warm path from 4 process spawns to 1. This hook is fixed as a side effect, since it shares the launcher.

    Measured on that branch (interleaved A/B, 24 pairs alternating within one run):

    BEFORE  p50 = 5446 ms   p95 = 16991 ms
    AFTER   p50 = 1418 ms   p95 =  7874 ms    median paired ratio 3.71x
    AFTER   min =  124 ms
    

    Do not work this as a guardrails issue. It should be closed as covered once that PR lands, or kept open only to verify the timeout count drops. Leaving it here rather than closing immediately so the PR can link to it.

  4. kyle-sexton commented on Sep 6, 2026

    @kyle-sexton
    MemberAuthor

    Verification: FULLY FIXED on main. Recommending closure as covered.

    Independent read-and-report verification against origin/main @ 1e1e3bc52fd2fc335ce86fa398d6802893df6a2a. I did not assume the in-flight fix landed; I traced it, read both trees, and measured process creation directly.

    Which commit did it

    PR #3523 (perf(disk-hygiene): cut the destructive guard's per-call spawns 4 to 1...) merged 2026-08-31T21:57:57Z from head branch perf/disk-hygiene-guard-hook-spawns — the exact branch this issue names. Squash commit 5b5c676b, confirmed an ancestor of origin/main via git merge-base --is-ancestor. The branch no longer exists on the remote (git ls-remote --heads origin perf/disk-hygiene-guard-hook-spawns returns nothing), which is consistent with a merged-and-deleted branch, not an abandoned one. Shipped in disk-hygiene 0.21.0; plugin is now at 0.21.9.

    Nothing else touched the launcher after it — git log --follow on plugins/disk-hygiene/hooks/run-python-hook.sh has 5b5c676b as the most recent change.

    Claim-by-claim against current main

    This issue claimed State on origin/main Evidence
    sed read of the ~3,500-line engine to recover MIN_PYTHON Gone. The floor is now recovered inside the candidate interpreter by PYTHON_VERSION_PROBE, which opens the engine itself and regex-matches ^MIN_PYTHON = (d, d)$. hygiene.MIN_PYTHON is still the single origin. run-python-hook.sh L~76-90; the sed was at L51 of 5b5c676b^
    A whole extra Python spawned only to evaluate sys.version_info >= MIN_PYTHON Gone from the warm path. It survives on the cold path only, folded into the same spawn that reads the floor (one probe spawn per candidate instead of a sed plus a Python). resolve_python3(); PYTHON_VERSION_PROBE at L~76
    dirname Gone. Replaced by pure parameter expansion (${BASH_SOURCE[0]%/*}) with the $(cd … && pwd) fork kept only for the relative-path spelling, which no registered caller uses — hooks.json and the skill frontmatter both pass "${CLAUDE_PLUGIN_ROOT}"/hooks/.... run-python-hook.sh L~42-50; the dirname was at L33 of 5b5c676b^
    Warm path should be 1 spawn Confirmed, and it is stronger than claimed. A $HOME/.cache/disk-hygiene interpreter cache (schema tag, verbatim PATH compare, [[ -x ]]/[[ -s ]], -nt mtime check, 24 h TTL) reaches exec having spawned nothing — every hot-path check is a bash builtin, and helpers report through globals specifically to avoid command-substitution subshells. the cache block, L~150-300
    Cost was launcher overhead, not the hook body Correct. guard_launch_monitor.py was not modified by 5b5c676b; its last change is dd95c789 (#2536). git log on the script

    Measured, not asserted

    strace -f -e trace=clone,clone3,fork,vfork,execve over the real Stop-hook invocation (run-python-hook.sh .../guard_launch_monitor.py --data-root …), old launcher (5b5c676b^) vs. main, same host, same interpreter:

    OLD (5b5c676b^)   execve: bash, dirname, sed, python3 (probe), python3 (guard)
                      -> 3 extra spawns + 1 real work = 4 processes; 6 clone/fork
    NEW cold (main)   execve: bash, python3 (probe), mkdir, chmod, mv, python3 (guard)
                      -> one-time cache-population cost only
    NEW warm (main)   execve: bash, python3 (guard)
                      -> 0 extra spawns, 0 clone/fork
    

    The exec'd interpreter replaces the launcher's own bash process (same PID), so the warm path is genuinely one process spawn, the guard itself — the issue's acceptance criterion, met exactly, verified by syscall trace rather than by PATH shim.

    Interleaved A/B wall clock, 20 alternating pairs, this Linux container:

    OLD  min=62  p50=66  p95=70  max=75   ms
    NEW  min=45  p50=48  p95=51  max=66   ms
    

    Only ~1.38x here, versus the 3.71x median paired ratio reported on the Windows reference host. That gap is expected and does not weaken the result: this host charges ~3 ms for bash -c true, where the reference host charged 283–1825 ms for the same call. Removing three spawns buys back three times whatever the host charges per spawn, so the direction is identical and the magnitude is a property of the host, which is precisely why #3523 made the spawn census, not a duration, the headline figure. That was the right call.

    Decomposing the remaining 48 ms on this host: ~3 ms bash, ~13 ms bare CPython startup, ~30 ms guard_launch_monitor.py body, ~2 ms launcher. The launcher is no longer a measurable term. Any further work on this hook would be against interpreter startup or the detector body, not the wrapper.

    Acceptance criteria status

    Two things worth recording

    One item in the issue body is inaccurate. The Stop hook's own measured share is not stated in the README; what is stated is the PreToolUse engine-gate hook's share (≈ 190–300 ms per tool call) plus the launcher spawn census that both hooks share. Since the fix is in the shared launcher and the census is the durable figure, I read Rule 1 as satisfied — but if a per-hook share for the Stop detector is wanted, that is a small doc follow-up, not a reason to hold this issue open.

    The 20 s Stop timeout is unchanged in hooks.json. Given the reference host's post-fix p50 of 1418 ms and p95 of 7874 ms, that headroom is now adequate, so the 52 timeouts should not recur. The residual concern the issue raises — that 1418 ms is still ~3x the ≤ 500 ms per-turn ceiling — is real, but it is dominated by the one irreducible interpreter spawn on a host charging up to 2.8 s per process launch. That is the interpreter-choice budget question, and it lives at #3508 with the other two Stop hooks, where it can be judged against the whole parallel set rather than one hook in isolation.

    Recommendation

    Close #3516 as covered by #3523. The named root cause is verifiably absent from main, and the acceptance criterion that this issue could actually own — warm-path spawn count of 1 — is met and independently measured. The per-turn budget re-measurement is live but belongs to #3508; closing this one does not drop it.

    This lane does not close issues. Closure is the triage lane's call. I am reporting evidence and a recommendation only, and I have made no change to any file, branch, or issue state.


    Generated by Claude Code

  5. kyle-sexton commented on Sep 26, 2026

    @kyle-sexton
    MemberAuthor

    Closing in the backlog-drain close sweep: obsolete. Evidence: PR #3523 merged (guard per-call spawns 4 to 1, watchdog stops blocking); strace verification comment 2026-09-06

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-ready

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions