Skip to content

Correct the below-threshold and cooldown fork counts in post-tool-hook.sh (round 2 of #1011) - #1028

Merged
fdaviddpt merged 6 commits into
mainfrom
fix/1011-process-count-regression
Oct 11, 2026
Merged

fdaviddpt merged 6 commits into
mainfrom
fix/1011-process-count-regression

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Part of #1011. Non-blocking gate-3 release-audit follow-up (trap.d/1011.below-threshold-path-regressed-and-process-count-misreported.md): v0.43.0's below-threshold no-save path in scripts/post-tool-hook.sh regressed in total process count (forks included) relative to v0.42.4, while the shipped changelog/docs claimed an improvement by counting only exec'd binaries, never bash subshell forks.

Fixes:

  • _pt_scan_lines now sets PT_SCAN_OFFSET/PT_SCAN_LINES globals instead of running inside an outer command substitution (removes one subshell fork).
  • The newline-counting line moved from a printf-piped-to-wc pipeline to a herestring (removes the pipe's extra subshell fork).
  • Self-review follow-up (Explore + oss:auditor, both spawned per agents/developer/review.md) found and fixed one more avoidable fork on the cooldown path (line 894, date +%s) using the existing _remember_date_into helper, tightened the new test's threshold, hardened strace fork counting against a zero-match trace, and added clone3 to the strace trace set.

Measured (empirical PID-delta harness, interleaved min-of-9 runs, not committed): fixed below-threshold path is below v0.42.4's own baseline on both /bin/bash 3.2.57 (75 vs 76) and Homebrew bash 5.3.15 (68 vs 69); fixed cooldown path is below the pre-fix v0.43.0 HEAD on bash >= 4.2 (64 vs 65 on bash 5), unaffected on bash 3.2 since _remember_date_into falls back to the same external date call there either way.

This issue (#1011) is already closed; this PR does not claim to close it.

Closes nothing: this is a release-audit follow-up fragment, not a fix for an open issue.

[AI-generated]

fdaviddpt and others added 5 commits October 11, 2026 12:29
… than before, and the v0.43.0 release notes misreported the totals

_pt_scan_lines was wrapped in an outer command substitution (one extra
subshell fork) and counted newlines through a printf-piped-to-wc pipe (a
second extra subshell fork on top of the wc exec), on top of the tail chunk
read #913 already added. Counted the right way (forks included, not just
exec'd binaries), the below-threshold path forked more processes than
v0.42.4 did before #913/#1017 ever touched it, while the changelog and
CHANGELOG.md kept saying "1 (tail only)" / "a second spawn, wc" -- both
counting only external binaries that got exec'd, never the bash subshell
forks a $(...) or | also creates even when nothing inside execs anything
new. On an affected Windows 11 25H2 build (#1011's own report) each of
those forks leaks a kernel Token object until reboot, so this was a real
regression, not just a miscount.

_pt_scan_lines now sets globals (PT_SCAN_OFFSET, PT_SCAN_LINES) instead of
being called inside $(...), and gets its newline count via a herestring
into wc -l instead of a pipe, removing both extra subshell forks without
reintroducing #1017's quadratic per-byte newline scan. Measured with an
interleaved comparison against the v0.42.4 tag (PID-delta, minimum of 9
runs, /bin/bash 3.2 and Homebrew bash 5): the below-threshold path now
forks fewer processes than v0.42.4 did, on both shells; the cooldown
path's own real reduction is unaffected either way.

Adds tests/test_post_tool_total_processes_1011.py, a fork-inclusive budget
test using strace -f -e trace=clone,fork,vfork on Linux CI legs (skipped
elsewhere, with a windows-skip-triage.md row), with a positive control
that the method detects the cooldown path's own lower fork count. Shifts
tests/test_locale_ranges_695.py's two post-tool-hook.sh line pins for the
5 added/changed lines. Adds a changelog fragment correcting the prior
"processes" wording for the next release; does not rewrite already-
released CHANGELOG.md history.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…den strace counting, fix one more avoidable fork, correct a stale comment

Explore and oss:auditor review of commit 0d09f6a found:
- tests/test_post_tool_total_processes_1011.py's below-threshold assertion
  used "<= 9" (the pre-fix round-1 regression figure), which a revert of
  the fix would still satisfy. Tightened to "< 6" -- v0.42.4's own
  pre-#913 baseline for this decision -- so a reintroduced fork actually
  fails the test.
- _count_forks returned 0 (not None) whenever strace ran but produced no
  usable trace (an unsupported flag combination, a ptrace restriction),
  which the caller could not distinguish from "the hook genuinely forked
  nothing" -- never true on this code path. It now checks the subprocess
  return code and treats a zero-match trace as None too, and both test
  functions assert None is absent before taking min().
- The strace invocation's -e trace= list omitted clone3, which some
  modern glibc/kernel combinations route process creation through
  instead of clone; added to both the flag list and the counting regex.
- scripts/post-tool-hook.sh:894's cooldown-elapsed check still used
  $(_remember_date +%s), the exact avoidable-subshell-fork pattern this
  whole round exists to remove, on the cooldown no-save path the brief
  asked to sweep. Switched to _remember_date_into, same as
  session-start-hook.sh's own two +%s call sites already do.
- The comment directly above the herestring in _pt_scan_lines still
  described the pipe mechanism this same commit removed. Corrected.
- Shifted tests/test_locale_ranges_695.py's two post-tool-hook.sh line
  pins again for the added lines (996->1001, 1099->1103), and fixed one
  of my own off-by-one errors in the previous shift (1000, not the true
  bracket-range line 1001) that a fresh pytest run caught immediately.

Not changed: the dead PT_SCAN_OFFSET/PT_SCAN_LINES=0 initialization
Explore flagged as unreachable cosmetic noise -- left as harmless
defensive initialization rather than a third edit to a file whose line
numbers have already shifted twice from review follow-up.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…e process off the cooldown path on bash >= 4.2

f80765d's _remember_date_into fix shaves one fork off the cooldown path on
bash 4.2+ (measured: one fewer process on Homebrew bash 5), not just the
below-threshold path the original fragment described -- bash 3.2 falls back
to the same external date call either way, so its own cooldown count is
unchanged. Updates the fragment to say so accurately rather than leave it
claiming the cooldown count was untouched by this round.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…ce totals against tagged v0.42.4/v0.43.0 baselines instead of the wrong 6-process threshold; add a scratch #1011 attribution diagnostic step (to be removed before merge) that maps clone/fork/vfork/execve syscalls back to script:line via BASH_XTRACEFD+PS4 correlation\n\nCo-Authored-By: Claude Sonnet 5 <[email protected]>

Co-Authored-By: Max <noreply>
… totals survive a PASSING run, and drop the scratch attribution diagnostic step from tests.yml now that its one-off output has been captured for the follow-up issue\n\nCo-Authored-By: Claude Sonnet 5 <[email protected]>

Co-Authored-By: Max <noreply>
…d strace totals (v0.42.4=68, v0.43.0=69, HEAD=67) and name the still-open ~67-process-per-call issue (#1029) rather than claiming the fix changes the order of magnitude\n\nCo-Authored-By: Claude Sonnet 5 <[email protected]>

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt merged commit 9773848 into main Oct 11, 2026
33 of 34 checks passed
@fdaviddpt
fdaviddpt deleted the fix/1011-process-count-regression branch October 11, 2026 12:52
fdaviddpt added a commit that referenced this pull request Oct 11, 2026
…ok.sh call (#1031)

* Follow-up investigation for #1029: the ~67-process whole-hook total is cold-start-only, not a per-call cost

#1011/#1028's own CI measurement (strace -f -e trace=clone,clone3,fork,vfork,
whole-hook total for post-tool-hook.sh's no-save path, minimum of 9 runs) is
67 on HEAD, down from v0.43.0's 69 and v0.42.4's 68 -- but still on the order
of several dozen processes for one no-save PostToolUse call, per #1029.

Traced to the test's own fixture: tests/test_post_tool_total_processes_1011.py
creates a brand-new tmp_path (HOME, PROJECT, REMEMBER_DIR, TMPDIR all freshly
created) for every one of the n repeated runs, so every measured run takes
the slow/cold path -- _remember_env_cache_load misses, so resolve-paths.sh,
detect-tools.sh, bootstrap-dirs.sh and log.sh all get sourced, pulling in
lib-memory-dir.sh's worktree-detection forks, _config_load's jq read, and
several mktemp+mv atomic cache publishes. On the warm path -- any call after
the first in a real session, the common case once a project has run once --
_remember_env_cache_load succeeds and none of those four files are sourced
at all; the only forks left are _pt_scan_lines's own tail+wc pair, which the
code's own comment directly above the wc call already documents as
unavoidable without reintroducing #1017's quadratic bash-expansion
regression.

No source change lands in this commit: every candidate #1029 named
(bootstrap-dirs.sh's directory creation, the mktemp+mv atomic-write pattern,
the jq config read, _pt_scan_lines's two forks) traced to either
already-gated cold-path-only work (#230/#666), work inside the byte-pinned
lib-memory-dir.sh (tests/test_case_divergence_298.py forbids touching it), or
a fork already proven, in-repo, unavoidable without a worse regression.
Forcing a cut here would mean either reaching into the pinned file or
reintroducing one of #1013/#1017/#1021's own fixed regressions, neither of
which is a safe trade for a performance-only, non-blocking issue.

tests/test_post_tool_total_processes_1011.py now asserts an absolute budget
(69 -- the measured 67 plus 2 of slack) for both the below-threshold and
cooldown paths, in addition to the existing relative tag-comparisons, so a
real future reduction or regression on this cold path is still caught even
on a CI leg where neither tagged comparison tree can be materialized (no
network reaching origin). Adds a changelog fragment documenting the
corrected attribution with the CI-measured totals.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* Fix #1029: port the fork-free stdin-field extractor to post-tool-hook.sh, removing three unconditional subshell forks per call

Self-review (Explore + oss:auditor review of commit 3116fcf) found a real,
low-risk cut this round's investigation missed: _stdin_json_string in
scripts/post-tool-hook.sh was called through X=$(...) command substitution
at three sites (REMEMBER_HOOK_CWD, STDIN_SESSION_ID, STDIN_TRANSCRIPT_PATH),
all above the _REMEMBER_FAST if/else, so all three ran unconditionally on
EVERY invocation of this hook -- cold-start and warm path alike -- each
forking a subshell even though the function itself is pure parameter
expansion with no external exec at all. session-start-hook.sh already
carries a fork-free twin of this exact extractor, _stdin_json_string_into
(#665, part of #660), written into VARNAME via printf -v instead of printed
through a subshell.

Ports that same function (renamed in place, same matching algorithm: same
first-occurrence scan, same #344 nested-key gap, same #829 double-backslash
collapse) into post-tool-hook.sh and switches all three call sites to it.
No behaviour change: tests/test_hook_cwd_end_to_end_411.py,
tests/test_codex_stdin_session_id_468.py and
tests/test_session_end_hook_345.py, which all exercise this extraction
end-to-end, pass unchanged, along with 47 other post-tool-hook.sh tests run
locally (test_post_tool_* family, test_hot_path_cost_pin_330,
test_pt_scan_lines_wall_clock_1017, test_post_tool_fast_path_350) and
tests/test_release_branch_check_900.py's byte-budget pin.

tests/test_locale_ranges_695.py's five post-tool-hook.sh line pins shift by
+4 for the lines this change adds above them. Tightens
tests/test_post_tool_total_processes_1011.py's new absolute-budget
assertions (added in 3116fcf) from <69 to <67 -- strictly below #1028's own
prior total, reflecting this cut -- for both the below-threshold and
cooldown paths; the exact new total is reasoned, not re-measured locally
(this file's own strace gate needs Linux, unavailable here), and this pull
request's own CI run will confirm it. Updates the changelog fragment and
this file's own module docstring to describe the fix landed rather than
"no further cut was found."

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* Fix #447 tests: follow post-tool-hook.sh's extractor rename to _stdin_json_string_into

00f26b7 ported the fork-free printf-v extractor into post-tool-hook.sh, renaming
_stdin_json_string to _stdin_json_string_into there (same shape as
session-start-hook.sh's own #665/#660 port). The #447 characterization tests
still looked for the old name by string match, so all 7 post-tool/* parametrized
cases failed collection-time assertions on every CI leg ("_stdin_json_string not
found in scripts/post-tool-hook.sh"), not a behaviour regression.

Confirmed the new extractor keeps the same first-occurrence scan, the same
documented nested-before/after gap, and the same #829 double-backslash collapse
by reading its body directly (post-tool-hook.sh:212-230): identical mechanism to
_stdin_json_string, just printf -v into a named variable instead of printed and
captured via command substitution. No product fix needed -- only the test's
CASES table, which already supported the "_into" calling convention via
session-start-hook.sh's own rows in the same table.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* Fix #829's post-tool EXTRACTORS row to match the same extractor rename

The previous commit fixed test_stdin_extractor_top_level_wins_447.py's own
CASES table but missed this file's separate EXTRACTORS table, which also
names post-tool-hook.sh's extractor by the old "_stdin_json_string" /
keyed=True / into=False shape. CI's first re-run surfaced the gap directly:
test_windows_cwd_is_decoded[post-tool], test_unc_prefix_decodes_pairwise[post-tool]
and test_posix_cwd_is_unchanged[post-tool] all failed the same
"_stdin_json_string not found in scripts/post-tool-hook.sh" assertion.

Same fix as before: the function is now _stdin_json_string_into (printf -v,
no subshell), so this row flips to into=True, matching session-start-hook.sh's
own "session-start/into" row in the same table.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* Fix #1029 review: _sjsi_-prefix every local in the ported extractor

printf -v resolves against the innermost local, so the port left a latent
collision: a future call site passing a VARNAME of value, dq, rest, raw,
field or prefix would write the function own local, not the caller.
session-start-hook.sh copy already prefixes every local for this reason.
Same line count, so the locale-range pins do not move.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Sonnet 5 <[email protected]>
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.

1 participant