Repository navigation
Conversation
REMEMBER_HOOK_CWD was set from the UserPromptSubmit stdin payload only inside the "cache missed" branch -- after _remember_env_cache_load had already run and failed for want of it. Every UserPromptSubmit on a host with no CLAUDE_PROJECT_DIR (Codex, Gemini CLI) wrote a cache file and none was ever read back, verbatim the #469 symptom on the hook that fires once per human turn. The stdin read now runs unconditionally, ahead of the cache-load attempt; measured at no cost above the noise floor of a bare process spawn, since it forks nothing. Also corrects tests/test_env_cache_hook_cwd_key_469.py's stale claim that its hand-rolled harness already matches user-prompt-hook.sh's shape, and adds an end-to-end regression against the real script. Co-Authored-By: Max <noreply>
…ft (#479) Explore review (self-review round) found two low-severity issues in the prior commit: the new regression test pointed at the wrong line range for the code it was asserting about (the actual gate is at scripts/user-prompt-hook.sh:207-220, not 132-140, which is prose); and the code comment justifying the now-unconditional stdin read argued only the average-case cost (a bounded builtin, no fork) without acknowledging that a cache-HIT invocation, which previously never touched stdin at all, now carries the same worst-case `read -t 1` ceiling the slow path already had. Both fixed: the comment corrected, and a new paragraph naming the worst-case shift explicitly, pointing at post-tool-hook.sh's own unconditional read as the same tradeoff already accepted there. Co-Authored-By: Max <noreply>
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.
Closes #479.
What was broken
#469 (merged as #476) made
_remember_env_cache_pathfall back toREMEMBER_HOOK_CWDwhenCLAUDE_PROJECT_DIRis unset, so the #227 fast path would stop being permanently dead on Codex and Gemini CLI. That fallback was itself inert inscripts/user-prompt-hook.sh: the hook setREMEMBER_HOOK_CWDfrom its own UserPromptSubmit stdin payload only inside the "cache missed" branch -- i.e. only after_remember_env_cache_loadhad already run and failed for want of it. Verified independently before writing any fix: every UserPromptSubmit on a host with noCLAUDE_PROJECT_DIRwrote a cache file and none was ever read back, verbatim the #469 symptom, on the hook that fires once per human turn.The issue also flagged that
tests/test_env_cache_hook_cwd_key_469.py's comment claimed its hand-rolled harness was "exactly the shape user-prompt-hook.sh and post-tool-hook.sh already offer" -- true of the second, false of the first. Fixing only that comment would have left the cache dead; the ordering had to move.The fix
The bounded, forkless stdin read (
read -t 1, a bash builtin -- no process spawned) now runs unconditionally inscripts/user-prompt-hook.sh, ahead of the cache-load attempt, matching the orderingscripts/post-tool-hook.shalready used. The judgment call the issue asked for: whether the bounded stdin read is acceptable on the fast path, given the file's own #227 cost reasoning. Measured rather than assumed: 50 runs of the exact read-and-parse loop against a realistic UserPromptSubmit payload averaged 8.5ms end-to-end, against 9.5ms for a no-op bash process consuming the same stdin and doing nothing else (macOS, bash 3.2) -- the loop's own marginal cost does not clear that baseline's noise floor, because it forks nothing and the #227 numbers this file cites are all per-FORK costs. Chose to pay the read on the fast path rather than accept this hook having no working fast path at all on Codex/Gemini, and said so explicitly in the code comment rather than leaving it implicit.A follow-up commit (after self-review, see below) also names the worst-case shift explicitly: before this change a cache-HIT invocation never touched stdin at all; after it, every invocation carries the same
read -t 1ceiling the slow path already had.post-tool-hook.shalready accepts this same tradeoff, unremarked, on the hook that fires roughly ten times more often -- this extends an already-accepted tradeoff to a second hook rather than introducing a new risk class.Tests
tests/test_env_cache_hook_cwd_key_469.pygains an end-to-end regression test that drives the real script (not the hand-rolled harness) twice viabash -x, and asserts the second invocation is a cache HIT -- it never re-sourcesresolve-paths.sh-- rather than merely checking a cache file was written. Red first: fails on the pre-fix script (resolve-paths.shfound in the second invocation's trace, i.e. still a miss); green after the fix (4 passed). The stale test comment that certified the fix against the wrong premise is corrected in the same commit.Self-review
Two agents reviewed the first commit against the diff:
oss:auditorreturned NO FINDINGS across 4 classes.Explore(as a reviewer) returned 2 low-severity findings -- a stale line-number reference in the new test's comment, and an unacknowledged worst-case-latency shift from moving the stdin read to the fast path -- both addressed in a second commit (9040211).Below-bar
None. Nothing found during this work was judged a real defect with no reachable caller.
Docs
changelog.d/479.fixed.mdadded.README.md's paragraph on the #469 fast-path fallback (around "The cache key falls back to REMEMBER_HOOK_CWD...") now says the fallback was itself inert inuser-prompt-hook.shuntil #479, and how the ordering was corrected to matchpost-tool-hook.sh.