Skip to content

Fix #1017: linear wc -l scan count; fix #1019 marker mtime freeze - #1020

Merged
fdaviddpt merged 3 commits into
mainfrom
worktree-agent-a65976d4df3f691e5
Oct 11, 2026
Merged

fdaviddpt merged 3 commits into
mainfrom
worktree-agent-a65976d4df3f691e5

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

#1017 (blocking): quadratic transcript-scan newline count

_pt_scan_lines in scripts/post-tool-hook.sh (added by #913) counted newlines in an appended transcript chunk with a per-byte parameter substitution, which is quadratic in chunk size on bash 3.2 and bash 5.3 alike. The release auditor measured 110.81s for a 2MB chunk on bash 5.3.15, and 415.67s for a 50KB chunk on /bin/bash 3.2.57 (this repo's documented floor shell and macOS's shipped default) -- regressing the exact #913 scenario (the first post-upgrade call on an existing session rescans the whole transcript) to far worse than the 229ms whole-file wc -l #913 replaced.

Fix: replaced the per-byte count with a printf '%s' "$_complete" | wc -l pipeline (a linear C loop), with a digit-strip and zero-default for defensive parsing of wc's output. Costs one extra fork per call on the below-threshold/warm-save scan path; the cooldown-active no-save path never reaches this code and stays at 1 spawn.

Closes #1017

#1019 (non-blocking): capture-alive marker mtime freeze on empty SESSION_ID

The capture-alive marker stopped refreshing its mtime when SESSION_ID comes back empty (the basename-sanitiser-rejected route) and the marker already held that same empty content, because #1016's "already holds it, write in place" optimization does a 0-byte write(2) in that case, and a 0-byte write never touches mtime (POSIX) -- so doctor.sh's "PostToolUse marker present (Ns old)" check could misreport a healthy, actively-firing session as stale.

Fix: widened the in-place-write branch's guard to also force the atomic tmp+mv branch (which always does a real mv and thus always touches mtime regardless of content) whenever SESSION_ID is empty. The common non-empty-id case is unaffected and keeps #1016's no-fork optimization.

Closes #1019

TDD

New tests in tests/test_pt_scan_lines_wall_clock_1017.py pin hard wall-clock bounds using the release auditor's own measured sizes directly (bound well under the pre-fix measured times in every case; longest observed post-fix run was 1.77s against a 2.5s bound). tests/test_capture_alive_marker_mtime_1019.py carries a positive control (a non-empty SESSION_ID still refreshes mtime) alongside the real red/green case (an empty SESSION_ID now also refreshes mtime). Targeted suite run: 462 passed (full command list in the developer's own report).

Self-review findings

Two reviewers (Explore, oss:auditor) ran against the commit. One cosmetic finding was fixed in a follow-up commit (stale arithmetic in a test comment). Three further findings were judged below this pull request's bar or routed for separate filing, recorded here so the receipts are checkable against the body rather than only against prose elsewhere:

  • Below-bar: if the new wc -l fork in _pt_scan_lines fails or its pipe breaks, _nl_count defaults to 0, the same way every other external fork in this hook (tail, mv, date) already fails silently into a safe default -- changing only this one fork's failure handling would be an inconsistent, unrequested change needing a maintainer decision on the whole file's fail-safe posture, not a unilateral change in this lane.
  • Below-bar: no CI leg exercises either fix's POSIX-specific claims on Windows, because the new test files correctly skip there by design (post-tool-hook.sh is bash/POSIX-only), matching every other test in tests/test_post_tool_incremental_scan_913.py -- not a new gap this diff introduces, and there is no reachable Windows execution path to close it against.
  • Report-for-filing (not fixed here, out of scope for this pull request): scripts/post-tool-hook.sh around line 1179 uses the same non-anchored parameter-expansion shape that scripts/lib-memory-context.sh already documents as quadratic and was rewritten away from, applied over _LAST_SAVE_CONTENT (unbounded over a long-lived project). Neither measured; recommend filing as a new issue or a comment tying it to post-tool-hook.sh _pt_scan_lines: quadratic-cost scan hangs the hook on real transcripts (v0.42.5 release-audit, round 1, BLOCKING) #1017's lineage, maintainer's call.

#1018 (the scan sidecar never pruned) was explicitly left alone -- not a one-liner, out of scope for this pull request.

[AI-generated]

fdaviddpt and others added 2 commits October 11, 2026 02:05
…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]>
…95.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]>
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]>
@fdaviddpt
fdaviddpt merged commit 2692ff9 into main Oct 11, 2026
33 of 34 checks passed
@fdaviddpt
fdaviddpt deleted the worktree-agent-a65976d4df3f691e5 branch October 11, 2026 02:25
fdaviddpt added a commit that referenced this pull request Oct 11, 2026
…ames its test

A Windows pytest leg was cancelled twice by the job-level timeout-minutes:
30 (PR #1013 job 114288023016, PR #1020 job 114358206393), and a job
cancelled by its own timeout writes NO log at all, so we could not tell
which test hung or whether it was the runner or our code (#1002's real
wscript.exe tests have already hung 40+ minutes once).

- pyproject.toml: faulthandler_timeout = 900 under [tool.pytest.ini_options]
  (built into pytest, no new dependency). Dumps every thread's traceback,
  naming the hung test, once any single test runs longer than 900s. Sized
  with ~30x margin over the slowest single test observed on a recent green
  windows-latest run (30.14s, run 38091996750 job 114330142530), and below
  the new step timeout so the dump lands in a kept log.
- .github/workflows/tests.yml: a step-level timeout-minutes: 25 on both
  "Run tests" and "Run the full suite against the compiled hooks" -- below
  the job's 30-minute one, so a hang fails the STEP and the job still
  completes with its log (including the faulthandler dump) rather than
  being cancelled with no log at all.
- A trailing, Windows-only, if: always() step on both jobs that lists any
  surviving wscript.exe/bash.exe/conhost.exe/powershell.exe processes with
  PID/PPID/command line, so a leaked child is visible. Not hypothetical:
  the same green run above still had to force-terminate a leaked bash and
  a leaked wscript process at job teardown.

PROVEN on PR #1022 (same branch, run 38103924282, before this commit): a
temporary faulthandler_timeout=5 plus a throwaway test sleeping 8s produced
a log on BOTH a Windows leg (pytest (windows-latest, 3.10), job
114365322297, lines 703-708: "Timeout (0:00:05)!" naming
tests/test_proof_1014_faulthandler.py::test_proof_sleep_past_faulthandler_timeout
with its full traceback) and a Linux leg (pytest (ubuntu-latest, 3.10), job
114365322191, lines 1072-1079: same dump, same test). The throwaway test
and the lowered value are removed/restored in this commit; the real values
above are what ships.

No user-visible behavior changes -- CI-only -- so no changelog fragment;
PR labeled no-changelog instead.

faulthandler_timeout is independent of --cov (coverage collection is
unaffected) and of pytest-xdist (confirmed via repo-wide grep: this repo
has no xdist configured, and the setting is per-worker/per-process either
way if that changes).

Co-Authored-By: Max <noreply>
fdaviddpt added a commit that referenced this pull request Oct 11, 2026
…ames its test

A Windows pytest leg was cancelled twice by the job-level timeout-minutes:
30 (PR #1013 job 114288023016, PR #1020 job 114358206393), and a job
cancelled by its own timeout writes NO log at all, so we could not tell
which test hung or whether it was the runner or our code (#1002's real
wscript.exe tests have already hung 40+ minutes once).

- pyproject.toml: faulthandler_timeout = 900 under [tool.pytest.ini_options]
  (built into pytest, no new dependency). Dumps every thread's traceback,
  naming the hung test, once any single test runs longer than 900s. Sized
  with ~30x margin over the slowest single test observed on a recent green
  windows-latest run (30.14s, run 38091996750 job 114330142530), and below
  the new step timeout so the dump lands in a kept log.
- .github/workflows/tests.yml: a step-level timeout-minutes: 25 on both
  "Run tests" and "Run the full suite against the compiled hooks" -- below
  the job's 30-minute one, so a hang fails the STEP and the job still
  completes with its log (including the faulthandler dump) rather than
  being cancelled with no log at all.
- A trailing, Windows-only, if: always() step on both jobs that lists any
  surviving wscript.exe/bash.exe/conhost.exe/powershell.exe processes with
  PID/PPID/command line, so a leaked child is visible. Not hypothetical:
  the same green run above still had to force-terminate a leaked bash and
  a leaked wscript process at job teardown.

PROVEN on PR #1022 (same branch, run 38103924282, before this commit): a
temporary faulthandler_timeout=5 plus a throwaway test sleeping 8s produced
a log on BOTH a Windows leg (pytest (windows-latest, 3.10), job
114365322297, lines 703-708: "Timeout (0:00:05)!" naming
tests/test_proof_1014_faulthandler.py::test_proof_sleep_past_faulthandler_timeout
with its full traceback) and a Linux leg (pytest (ubuntu-latest, 3.10), job
114365322191, lines 1072-1079: same dump, same test). The throwaway test
and the lowered value are removed/restored in this commit; the real values
above are what ships.

No user-visible behavior changes -- CI-only -- so no changelog fragment;
PR labeled no-changelog instead.

faulthandler_timeout is independent of --cov (coverage collection is
unaffected) and of pytest-xdist (confirmed via repo-wide grep: this repo
has no xdist configured, and the setting is per-worker/per-process either
way if that changes).

Co-Authored-By: Max <noreply>
fdaviddpt added a commit that referenced this pull request Oct 11, 2026
…ames its test

A Windows pytest leg was cancelled twice by the job-level timeout-minutes:
30 (PR #1013 job 114288023016, PR #1020 job 114358206393), and a job
cancelled by its own timeout writes NO log at all, so we could not tell
which test hung or whether it was the runner or our code (#1002's real
wscript.exe tests have already hung 40+ minutes once).

- pyproject.toml: faulthandler_timeout = 900 under [tool.pytest.ini_options]
  (built into pytest, no new dependency). Dumps every thread's traceback,
  naming the hung test, once any single test runs longer than 900s. Sized
  with ~30x margin over the slowest single test observed on a recent green
  windows-latest run (30.14s, run 38091996750 job 114330142530), and below
  the new step timeout so the dump lands in a kept log.
- .github/workflows/tests.yml: a step-level timeout-minutes: 25 on both
  "Run tests" and "Run the full suite against the compiled hooks" -- below
  the job's 30-minute one, so a hang fails the STEP and the job still
  completes with its log (including the faulthandler dump) rather than
  being cancelled with no log at all.
- A trailing, Windows-only, if: always() step on both jobs that lists any
  surviving wscript.exe/bash.exe/conhost.exe/powershell.exe processes with
  PID/PPID/command line, so a leaked child is visible. Not hypothetical:
  the same green run above still had to force-terminate a leaked bash and
  a leaked wscript process at job teardown.

PROVEN on PR #1022 (same branch, run 38103924282, before this commit): a
temporary faulthandler_timeout=5 plus a throwaway test sleeping 8s produced
a log on BOTH a Windows leg (pytest (windows-latest, 3.10), job
114365322297, lines 703-708: "Timeout (0:00:05)!" naming
tests/test_proof_1014_faulthandler.py::test_proof_sleep_past_faulthandler_timeout
with its full traceback) and a Linux leg (pytest (ubuntu-latest, 3.10), job
114365322191, lines 1072-1079: same dump, same test). The throwaway test
and the lowered value are removed/restored in this commit; the real values
above are what ships.

No user-visible behavior changes -- CI-only -- so no changelog fragment;
PR labeled no-changelog instead.

faulthandler_timeout is independent of --cov (coverage collection is
unaffected) and of pytest-xdist (confirmed via repo-wide grep: this repo
has no xdist configured, and the setting is per-worker/per-process either
way if that changes).

Co-Authored-By: Max <noreply>
fdaviddpt added a commit that referenced this pull request Oct 11, 2026
…ames its test (#1022)

* PROOF (temporary) for #1014: lower faulthandler_timeout to prove the dump fires

Temporary commit to capture CI evidence that pytest's faulthandler_timeout
names a hung test and dumps its traceback. Will be removed before the
final PR: this lowers faulthandler_timeout to 5s and adds a throwaway
test that sleeps 8s so the dump fires on a real CI run (windows + linux).

Co-Authored-By: Max <noreply>

* Fix #1014: faulthandler_timeout + step timeout so a hung pytest leg names its test

A Windows pytest leg was cancelled twice by the job-level timeout-minutes:
30 (PR #1013 job 114288023016, PR #1020 job 114358206393), and a job
cancelled by its own timeout writes NO log at all, so we could not tell
which test hung or whether it was the runner or our code (#1002's real
wscript.exe tests have already hung 40+ minutes once).

- pyproject.toml: faulthandler_timeout = 900 under [tool.pytest.ini_options]
  (built into pytest, no new dependency). Dumps every thread's traceback,
  naming the hung test, once any single test runs longer than 900s. Sized
  with ~30x margin over the slowest single test observed on a recent green
  windows-latest run (30.14s, run 38091996750 job 114330142530), and below
  the new step timeout so the dump lands in a kept log.
- .github/workflows/tests.yml: a step-level timeout-minutes: 25 on both
  "Run tests" and "Run the full suite against the compiled hooks" -- below
  the job's 30-minute one, so a hang fails the STEP and the job still
  completes with its log (including the faulthandler dump) rather than
  being cancelled with no log at all.
- A trailing, Windows-only, if: always() step on both jobs that lists any
  surviving wscript.exe/bash.exe/conhost.exe/powershell.exe processes with
  PID/PPID/command line, so a leaked child is visible. Not hypothetical:
  the same green run above still had to force-terminate a leaked bash and
  a leaked wscript process at job teardown.

PROVEN on PR #1022 (same branch, run 38103924282, before this commit): a
temporary faulthandler_timeout=5 plus a throwaway test sleeping 8s produced
a log on BOTH a Windows leg (pytest (windows-latest, 3.10), job
114365322297, lines 703-708: "Timeout (0:00:05)!" naming
tests/test_proof_1014_faulthandler.py::test_proof_sleep_past_faulthandler_timeout
with its full traceback) and a Linux leg (pytest (ubuntu-latest, 3.10), job
114365322191, lines 1072-1079: same dump, same test). The throwaway test
and the lowered value are removed/restored in this commit; the real values
above are what ships.

No user-visible behavior changes -- CI-only -- so no changelog fragment;
PR labeled no-changelog instead.

faulthandler_timeout is independent of --cov (coverage collection is
unaffected) and of pytest-xdist (confirmed via repo-wide grep: this repo
has no xdist configured, and the setting is per-worker/per-process either
way if that changes).

Co-Authored-By: Max <noreply>

* PROOF (temporary) for #1014 round 2: faulthandler_exit_on_timeout kills a genuine hang

This PR's own CI (run 38122057767, job 114418645594, pytest windows-latest/3.9)
ran the "Run tests" step ~33.6 min past its own 25min timeout and still ended
`cancelled` with no log at all -- the exact failure #1014 set out to fix, on
this PR's own branch. Neither the step's nor the job's timeout-minutes
visibly stopped it.

Temporarily lowers faulthandler_timeout to 20s and adds a test that sleeps
forever (never returns on its own) to prove faulthandler_exit_on_timeout
kills the pytest PROCESS itself well inside that bound, converting a hang
into a failed step with a traceback in a kept log -- rather than depending
on a runner-level kill already observed not to fire. Removed in the
finalizing commit on this branch; the real values ship there.

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

* Tee pytest output + upload as artifact; clean up v0.42.3 wscript test orphans (#1014)

Belt-and-braces alongside faulthandler_exit_on_timeout: the "Run tests" and
"Run the full suite against the compiled hooks" steps now tee pytest's
output to a file and upload it with actions/upload-artifact under
`if: always()`, so a failed or timed-out step still leaves the log behind
even if the step's own captured output is somehow lost. Does not cover a
job-level `cancelled` -- GitHub Actions skips steps entirely in that case --
which is exactly why the process-level kill is the primary fix.

Also fixes the orphan processes a green leg of this PR's own CI already
surfaced (run 38122057767, job 114418645663): the OLD v0.42.3 shape
test_windows_real_wscript_v0_42_3_1002.py drives backgrounds wscript.exe
with a trailing `&` and disowns it by design (reproducing the shipped
trap-B silent skip), so nothing in that shape or this test module ever
waited on or killed what it launched -- the job's own teardown had to
force-terminate a leftover bash.exe/wscript.exe pair at the end of the
whole run. Adds an autouse fixture that force-terminates, after every test
in the module, any wscript.exe/bash.exe/conhost.exe whose command line
mentions that test's own tmp_path.

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

* Finalize #1014: real values + changelog, after round-2 proof narrowed the Linux-only claim

The round-2 proof (run 38122057767, job 114445674357) showed the faulthandler
traceback naming the stuck test IS kept in the log on Linux, followed by the
step's own 25-minute timeout error -- i.e. the step timeout is what keeps the
log, not faulthandler_exit_on_timeout: pytest was not observed to exit on its
own before the step timeout fired. Windows was not proven.

- Removes tests/test_proof_1014_round2_exit_on_timeout.py (the deliberately
  hanging proof test) and restores faulthandler_timeout to its real value
  (900s).
- Drops faulthandler_exit_on_timeout: unproven by the round-2 evidence, so
  this repo does not rely on it.
- Updates the workflow comments that had claimed faulthandler_exit_on_timeout
  does the killing, to instead say what is actually proven: the step timeout
  keeps the log, proven on Linux only.
- Adds the changelog.d/1014.fixed.md fragment this PR was missing.

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

* Add changelog fragment for #1014

Co-Authored-By: Claude Sonnet 5 <[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

1 participant