Repository navigation
Part of #1011: reduce post-tool-hook.sh no-save forks - #1016
Merged
Merged
Conversation
…l scan A previous lane implemented #1011's fork-reduction half on a branch whose merge-base predated #913 (which rewrote the same region of post-tool-hook.sh to scan the transcript incrementally rather than with a whole-file `wc -l`, and moved the save-cooldown check ahead of that scan). This ports the same two fixes -- by hand, re-applied to the post-#913 code, not cherry-picked -- and re-measures against what main actually does today. Two forkless changes to post-tool-hook.sh's no-save path: - The transcript lookup (`ls -t "$SESSION_DIR"/*.jsonl | head -1`) is now a glob-plus-mtime walk, falling back to `ls -t` only on a genuine same-second mtime tie -- bash 3.2's `-nt` compares mtimes at whole-second resolution, so two transcripts written moments apart inside the same second can tie in either direction even though the filesystem's own finer-grained timestamp orders them correctly. An unresolved tie would silently keep whichever same-second file lexical glob order visited first, which is exactly the misattribution #204/#129 already named as worse than capturing nothing. - The capture-alive marker's atomic write (`mv -f`) is skipped whenever it already holds the current session id -- every tool call after the first in a session -- but still WRITTEN in place (no tmp file, no `mv`, so no fork added back) rather than doing nothing at all: doctor.sh reads this marker's mtime, not just its content, as "is PostToolUse still firing", and skipping the write outright would freeze that mtime at a session's first tool call. Measured on real `/bin/bash` 3.2 (not the Homebrew bash 5 normally ahead of it on PATH) against current `main`, which already carries #913's own spawn-shape change (`wc` -> `tail`, cooldown-gated): the cooldown path dropped from 4 spawns (`ls`, `head`, `mv`, `date`) to 1 (`date` only); the below-threshold path from 4 spawns (`head`, `ls`, `mv`, `tail`) to 1 (`tail` only). Re-pinned to the newly measured values, all re-measured on real bash 3.2, none copied from the pre-#913 lane: - tests/test_post_tool_fast_path_350.py: FAST_PATH_SPAWN_MEASURED 5 -> 1 - tests/test_post_tool_hook_spawns.py: POST_TOOL_SPAWN_MEASURED 19 -> 14 - tests/test_hot_path_cost_pin_330.py: READ_BUILTIN_MEASURED 17 -> 18, SIDECAR_READ_BUILTIN_MEASURED 18 -> 19 (the new capture-alive `read -r _ptl_capture_current` builtin) - tests/test_locale_ranges_695.py: re-pinned the ALLOWLIST line numbers the edits shifted (719/762/868/970 -> 759/802/940/1042) Several existing tests used `ls` spawning as the positive control for "the hook looked for a transcript" -- no longer valid once that lookup is forkless on the common path, and the incremental scan it used to also prove is itself skipped entirely under an active save-cooldown (#913). Both affected files (test_post_tool_fast_path_350.py, test_post_tool_incremental_scan_913.py) now check the capture-alive marker's existence instead: a filesystem effect written unconditionally for any run that found a transcript at all, reachable whether the lookup or the scan forked or not. Added two regression tests: the same-second mtime-tie fallback (red confirmed by reverting the tie-detection branch in place and re-running), and the capture-alive marker's mtime still advancing on an unchanged session id (doctor.sh's own "is PostToolUse still firing" check). #913's own incremental-scan sidecar, the cooldown-before-scan ordering, the #353 lookup running unconditionally, and #1012's Windows detach watchdog/rc=137 PID_FILE guard are all untouched -- confirmed by diff hunk boundaries, not just by intent. Co-Authored-By: Claude Sonnet 5 <[email protected]>
Spawned Explore and oss:auditor against commit 401c733 (the fork-reduction port). oss:auditor returned NO FINDINGS; Explore found three real, low-severity issues, none touching the tree (confirmed via tree_snapshot before/after and the auditor's own independent compare): 1. A comment just above the forkless glob-walk still described the common path as an "ls -t/slug-match dance" after #1011 made that path forkless -- stale prose left behind when the two surrounding comment blocks were rewritten but this one in between was not. Reworded to describe the actual behaviour. 2. The same-second mtime-tie flag (_ptl_jsonl_tie) stayed set for the rest of the glob walk even after a later, unambiguous comparison found a strictly newer file -- so the walk paid for the `ls -t` fallback in a case (3+ live transcripts, an early tie superseded by a clear later winner) that was never actually ambiguous by the time the walk ended. Not a correctness bug (`ls -t` always recomputes the right answer), but it contradicted the design's own claim to pay the fallback "only on a genuine same-second tie". Fixed by clearing the flag whenever a later comparison produces an unambiguous winner. 3. The capture-alive in-place write (the `else` branch, content unchanged) used a plain `>` redirect, which truncates the file to zero bytes before `printf` writes anything -- a real (if brief) window in which a concurrent PostToolUse invocation's own `read` could see an empty marker and misread it as a session change. Fixed with `1<>`, which opens the file for read+write with no truncation; safe specifically because this branch only runs when the new bytes are byte-identical in length to what is already there, so there is no length change for the lack of atomicity to expose. Re-pinned tests/test_locale_ranges_695.py's ALLOWLIST line numbers again (759/802/940/1042 -> 767/810/957/1059), since both the comment fix and the longer in-place-write comment shift lines above them. Full targeted suite (499 tests, the same set as the prior commit) re-run green on real /bin/bash 3.2 after every change in this commit. Co-Authored-By: Claude Sonnet 5 <[email protected]>
fdaviddpt
added a commit
that referenced
this pull request
Oct 11, 2026
) * Fix #1017: replace quadratic transcript-scan newline count with wc -l; fix #1019 marker mtime freeze #1017 (blocking, v0.42.5 release-audit): _pt_scan_lines (scripts/post-tool-hook.sh, added by #913/be8561a) counted newlines in the appended chunk with a per-byte bash parameter substitution, which is quadratic in chunk size on bash 3.2 and 5.3 alike -- measured 110.81s for a 2MB chunk on bash 5.3.15, 415.67s for a 50KB chunk on /bin/bash 3.2.57 (this repo's documented floor, macOS's shipped default). This regressed the exact #913 scenario (the first post-upgrade call on an existing session, which rescans the whole transcript) to far worse than the 229ms whole-file wc -l #913 replaced. Replaced the per-byte count with wc -l over the chunk -- one extra fork per call (wc, alongside the tail #913 already added), not a cost that grows with chunk size. New wall-clock tests pin a hard bound using the auditor's own measured sizes, on both a real /bin/bash 3.2 (skipped when absent) and whatever bash resolves off PATH, plus a doubling-cost scaling check. #1011's own spawn-count claims for the below-threshold/warm-save path are updated in its changelog fragment to say the count there is now 2 (tail, wc), not 1. #1019 (non-blocking, same release audit): the capture-alive marker's mtime stopped refreshing when SESSION_ID comes back empty (the basename-sanitiser route) and the marker already held that same empty content -- #1016's in-place write for the "already holds it" case writes zero bytes there, and a 0-byte write(2) never touches mtime (POSIX), so doctor.sh's "PostToolUse still firing" check could misreport a healthy session as stale. An empty SESSION_ID now always takes the atomic tmp+mv branch, which touches mtime regardless of content; the common non-empty case keeps #1016's no-fork optimization. Positive-control test added alongside the red case. #1018 (scan sidecar never pruned) is left alone -- not a one-liner, out of scope here. Closes #1017 Closes #1019 Co-Authored-By: Claude Sonnet 5 <[email protected]> * Fix review nit: correct line-shift arithmetic in test_locale_ranges_695.py comment Self-review (Explore spawn) found the comment above the two re-pinned ALLOWLIST entries said "+26" when the actual shift (957->984, 1059->1086) is +27. Cosmetic-only; the pinned line numbers themselves were already correct and the test passed either way. Co-Authored-By: Claude Sonnet 5 <[email protected]> * Fix #1017 review: triage the two new win32 blanket-skip modules tests/test_capture_alive_marker_mtime_1019.py and tests/test_pt_scan_lines_wall_clock_1017.py skip on win32 like the rest of the post-tool bash-subprocess family, but had no row in docs/windows-skip-triage.md, so tests/test_windows_skip_triage_497.py failed on every leg. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> --------- Co-authored-by: Claude Sonnet 5 <[email protected]>
This was referenced Oct 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
main(which already carries post-tool-hook: ~1.2 s per tool call on Windows (0.33.0) - whole-transcript wc -l grows with the session; lib-slug spawns cygpath twice per call #913's incremental transcript scan):ls -t "$SESSION_DIR"/*.jsonl | head -1) is now a forkless glob-plus-mtime walk, falling back tols -tonly on a genuine same-second mtime tie (bash 3.2's-ntcompares at whole-second resolution).mv -f) is skipped -- written in place instead, with no temp file or rename -- whenever it already holds the current session id, while still writing (not skipping entirely) so its mtime keeps moving for doctor.sh's "PostToolUse still firing" check./bin/bash3.2 (not the Homebrew bash 5 normally ahead of it on PATH): the cooldown-active path dropped from 4 spawns (ls,head,mv,date) to 1 (date); the below-threshold path from 4 spawns (head,ls,mv,tail) to 1 (tail). On Windows 11 25H2, each avoided fork is a kernel Token object that never leaks, not just saved latency -- thanks to @NanoInG for tracing the root cause.1<>instead of>).Test plan
FAST_PATH_SPAWN_MEASURED(5->1),POST_TOOL_SPAWN_MEASURED(19->14),READ_BUILTIN_MEASURED(17->18),SIDECAR_READ_BUILTIN_MEASURED(18->19) and the shiftedALLOWLISTline numbers intests/test_locale_ranges_695.py, all re-measured on real bash 3.2, none copied from an earlier (pre-post-tool-hook: ~1.2 s per tool call on Windows (0.33.0) - whole-transcript wc -l grows with the session; lib-slug spawns cygpath twice per call #913-rebase) lane./bin/bash3.2:tests/test_post_tool_incremental_scan_913.py,tests/test_hot_path_cost_pin_330.py,tests/test_post_tool_fast_path_350.py,tests/test_post_tool_hook_spawns.py,tests/test_post_tool_cooldown.py,tests/test_windows_hidden_detach_1002.py,tests/test_doctor.py,tests/test_locale_ranges_695.py,tests/test_scanner_shapes_source_898.py-- 499 passed, 0 failed.python3 .oss/assemble_changelog.py --check-- ok, fragment names Windows 25H2: every hook fork leaks a kernel Token object until reboot (win32k CForegroundLaunch bug) — fork tax is a persistent leak, not just latency #1011 in its own body.python3 .github/scripts/compile_hooks.py --repo .--post-tool-hook.shcompiles to 79,206 bytes, well under the 120 KiB (122,880 byte) release budget;python3 .github/scripts/build_release_tree.py+check_release_tree.pyreport OK with only pre-existing, unrelated REVIEW items.pytestsuite -- targeted run only, per project convention; CI is the full-matrix gate.Closes #1011
🤖 Generated with Claude Code
[AI-generated]