Skip to content

Fewer warm-path forks (#511), env-cache key parity on Windows (#504), nested-cwd reachability research (#494) - #514

Merged
fdaviddpt merged 3 commits into
mainfrom
fix/511
Sep 3, 2026
Merged

fdaviddpt merged 3 commits into
mainfrom
fix/511

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #511, closes #504, closes #494.

#511 -- user-prompt-hook.sh forks fewer subshells on the warm path

The warm/fast path still paid a bash subshell fork for the cwd extraction and the clock read even where nothing inside them shells out to an external process -- $( ... ) forks a subshell for the substitution itself regardless of the function body, cheap on Linux/macOS and 50-300ms+ on Windows Git Bash per the original report. Both now write into a variable directly (_stdin_cwd_into, _remember_date_into) instead of being captured through $( ... ). The original _stdin_cwd is left byte-for-byte unchanged, because tests/test_stdin_extractor_top_level_wins_447.py extracts and calls it verbatim by name. README now documents prompt_stamp: "stable" as the cheapest remaining option.

Reasoned, not observed: no Windows box was available to this lane to measure actual milliseconds saved. tests/test_fast_path_subshells_511.py pins the mechanism (parity between the old and new extractors on five payload shapes, and that the $(_stdin_cwd ...) / $(_remember_date ...) call sites are actually gone from the hot path).

#504 -- the env-cache key now survives Windows path normalization consistently

user-prompt-hook.sh's fast-path cache lookup keys on CLAUDE_PROJECT_DIR before resolve-paths.sh has normalized a Windows-native drive path; session-start-hook.sh's publish keys on it after. On a platform where normalization actually rewrites the string (Windows Git Bash, forward-slash drive form -> backslash drive form) the two hooks could derive different cache keys for the same project, so the fast path would never hit what the slow path had just published.

lib-env-cache.sh now normalizes the key the same way resolve-paths.sh does (mirrored logic, same case "$OSTYPE" in msys|cygwin) guard) before it is pinned, via printf -v rather than $( ... ) so the normalization itself adds no subshell fork on the fast path this pairs with. tests/test_env_cache_key_windows_normalize_504.py forces OSTYPE=msys and shows the raw and normalized spellings of the same project now key the same cache file (red before the fix), with a positive control that a genuinely different project still keys differently.

#494 -- researched whether any host's payload can nest cwd ahead of the top-level field

The open half of #447/#493: does any of Claude Code's, Codex's or Gemini CLI's hook payload schemas nest a same-named cwd key ahead of the top-level one? Source-verified for Codex (serde struct declaration order), docs-observed for the other two: no. cwd is always a top-level field and the only nested object a hook payload carries (tool_input/tool_response) is always declared after it -- so even a hypothetical third-party MCP tool naming one of its own parameters cwd would still land in the safe "nested-after" case. Documented in the extractor's own header comment across all four hook scripts. tests/test_stdin_extractor_top_level_wins_447.py's docstrings are updated to reflect this; no assertions changed -- the test stays a characterization against a synthetic payload rather than becoming a hard assertion, since a host is free to reorder its own schema in a future release.

Review

Two spawns against the first commit (3b864f4): Explore (reviewer) and oss:auditor. Explore found 4 real issues, the most significant being that #504's own fix used $(_remember_env_cache_normalize ...) -- reintroducing, on the exact fast path, the same subshell-fork pattern #511 (this same lane) removes elsewhere. oss:auditor found that the new test file's module-level skipif(BASH is None) would silently skip two pure-source-text regression checks on a leg with no bash. All 5 findings fixed in a follow-up commit (58458d5), verified with a positive control (monkeypatched resolve_bash() to None, confirmed the two static checks still run and pass: 2 passed, 7 skipped where before this fix the whole 9-test module would have skipped). Tree snapshot before/after both spawns shows no mutation beyond this lane's own review-response commit.

Testing

Targeted: pytest tests/ -k "prompt or env_cache or stdin or hook_cwd or windows_native or clock or session_start or post_tool or codex_stdin" -> 276 passed, 3 skipped (unrelated platform/bash-version reasons). python3 .oss/assemble_changelog.py --check passes with three new fragments (494.changed.md, 504.fixed.md, 511.fixed.md). shellcheck --severity=warning on every touched script shows no new findings. Full test_command (whole pytest suite) deliberately not run locally, per this repo's own CLAUDE.md -- left for CI.

…#494 nested-cwd research

#511 -- user-prompt-hook.sh's warm path still paid a subshell fork for the
cwd extraction and the clock read even where nothing inside them shells out
to an external process. Both now write into a variable directly
(`_stdin_cwd_into`, `_remember_date_into`) instead of being captured
through `$( ... )`. `_stdin_cwd` itself is left untouched, byte for byte,
because tests/test_stdin_extractor_top_level_wins_447.py extracts and calls
it verbatim by name. Reasoned, not observed: no Windows box available to
this lane to measure actual milliseconds saved.

#504 -- user-prompt-hook.sh's fast-path env-cache lookup keys on
CLAUDE_PROJECT_DIR before resolve-paths.sh has normalized a Windows drive
path; session-start-hook.sh's publish keys on it after. On a platform where
normalization actually rewrites the string the two hooks could derive
different cache keys for the same project, so the fast path would never
hit what the slow path had just published. lib-env-cache.sh now normalizes
the key the same way resolve-paths.sh does, scoped to the same
`case "$OSTYPE" in msys|cygwin)` guard so it stays a no-op fork-wise on
every other platform.

#494 -- researched whether any of Claude Code's, Codex's or Gemini CLI's
hook payload schemas can nest a same-named `cwd` key ahead of the
top-level one, the open half of #447/#493. Source-verified for Codex,
docs-observed for the other two: no. `cwd` is always top-level and the
only nested object a hook payload carries is always declared after it, so
even a hypothetical third-party MCP tool naming a parameter `cwd` would
still land in the safe "nested-after" case. Documented in the extractor's
own header comment across all four hooks; the #447 test stays a
characterization against a synthetic payload rather than becoming a hard
assertion, since a host is free to reorder its own schema.

Tests: tests/test_env_cache_key_windows_normalize_504.py (red before the
normalize fix -- raw and normalized Windows spellings keyed different cache
files; green after, with a positive control that a genuinely different
project still keys differently) and tests/test_fast_path_subshells_511.py
(parity between the print-and-capture and set-a-variable extractors, plus
that the `$(_stdin_cwd ...)` / `$(_remember_date ...)` call sites are
actually gone from the hot path). All targeted suites green locally
(pytest tests/ -k "prompt or env_cache or stdin or hook_cwd or
windows_native or clock or session_start or post_tool or codex_stdin" --
276 passed, 3 skipped for unrelated platform reasons). Full test_command
left for CI per this repo's own CLAUDE.md.

Co-Authored-By: Max <noreply>
Spawned Explore + oss:auditor against the previous commit. Both returned
real findings, fixed here rather than argued down.

Explore (3 findings, one already covered by design, two applied):
- #504's own fix (`_remember_env_cache_normalize`) was called as
  `_key="$(_remember_env_cache_normalize "$_key")"` -- a `$( )` command
  substitution, which forks a subshell for the substitution itself
  regardless of the function body, on the SAME fast path #511 (this same
  lane) exists to stop forking. Renamed to
  `_remember_env_cache_normalize_into` and switched to `printf -v`, the
  same technique already used for `_stdin_cwd_into`/`_remember_date_into`.
- The file's header comment ("every check below is a bash builtin") was
  left stale by that same bug; now states the one real exception (`tr` on
  a matched Windows drive letter, msys/cygwin only) explicitly.
- A dead `assert ... or True` in the new parity test
  (test_remember_date_into_matches_remember_date_builtin_path) is removed;
  the retry block immediately below it already implements the real check.

oss:auditor (1 finding):
- tests/test_fast_path_subshells_511.py's module-level
  `pytestmark = pytest.mark.skipif(BASH is None, ...)` skipped two
  pure-source-text regression guards
  (test_stdin_cwd_is_no_longer_captured_through_a_subshell and its
  sibling) that read the script file and grep it -- no subprocess, no bash
  needed. If resolve_bash() ever returns None on a leg, those two checks
  -- the ones the previous commit message leans on hardest -- would go
  dark silently. Split into a per-test `_needs_bash` marker applied only
  to the three tests that actually invoke bash via subprocess. Verified
  with a positive control: monkeypatching resolve_bash() to return None
  shows the two static checks still run and pass (2 passed, 7 skipped),
  where before this fix the whole module would have skipped (9 skipped).

Tree snapshot taken before spawning both reviewers and compared after:
the only diffs are these two files, matching exactly the fixes applied
here -- neither spawn (both told explicitly not to edit, Explore has Bash
but no Edit/Write) mutated anything.

Targeted suite still green: pytest tests/ -k "prompt or env_cache or
stdin or hook_cwd or windows_native or clock or session_start or
post_tool or codex_stdin" -- 276 passed, 3 skipped for unrelated platform
reasons. assemble_changelog.py --check passes; shellcheck --severity=warning
on the touched files shows no new findings.

Co-Authored-By: Max <noreply>
…s tests (job #100829698217)\n\n#504's test file hardcoded the literal string "bash" instead of this suite's established resolve_bash() (tests/_bash_runner.py, #432) -- on windows-latest, bare "bash" commonly resolves to the WSL launcher stub rather than Git Bash, which prints a UTF-16 nag and exits nonzero when no WSL distro is installed, exactly matching the corrupted-looking tail in the job log's CompletedProcess repr.\n\n#511's parity test passed a raw JSON string containing embedded double quotes as a Windows argv positional. subprocess.run's no-shell argv list is re-flattened by Python's own list2cmdline (MSVCRT quoting rules) before CreateProcess hands it to bash.exe, and MSYS's own argv re-parsing does not agree with that quoting on a string containing literal '"' characters -- the quotes can be stripped before $1 reaches the function under test, so `"cwd"` no longer appears in $1 and both the old and new extractor report a miss (matching the observed OLD_RC=1/NEW_RC=1 on every case whose raw JSON contains a quote). Routed through an environment variable instead, which Windows passes as an already-decoded block with no command-line re-quoting step.\n\nBoth REASONED, not OBSERVED -- no Windows box available to confirm either mechanism directly; the fixes remove the hazard in the harness itself; the functions under test (_stdin_cwd, _stdin_cwd_into, _remember_env_cache_normalize_into) are unchanged.\n\nVerified: 276 passed, 3 skipped locally (macOS) on the same targeted -k run the lane's own report used; no behavior change to product code, so no red-then-green cycle applies here -- the bug was in the test harness' own subprocess plumbing, not in an assertion.

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt merged commit 13f5445 into main Sep 3, 2026
13 checks passed
@fdaviddpt
fdaviddpt deleted the fix/511 branch September 3, 2026 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment