Skip to content

fix: #469 env-cache fallback still inert in user-prompt-hook.sh (#479) - #489

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/479
Sep 1, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/479

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #479.

What was broken

#469 (merged as #476) made _remember_env_cache_path fall back to REMEMBER_HOOK_CWD when CLAUDE_PROJECT_DIR is unset, so the #227 fast path would stop being permanently dead on Codex and Gemini CLI. That fallback was itself inert in scripts/user-prompt-hook.sh: the hook set REMEMBER_HOOK_CWD from its own UserPromptSubmit stdin payload only inside the "cache missed" branch -- i.e. only after _remember_env_cache_load had already run and failed for want of it. Verified independently before writing any fix: every UserPromptSubmit on a host with no CLAUDE_PROJECT_DIR wrote 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 in scripts/user-prompt-hook.sh, ahead of the cache-load attempt, matching the ordering scripts/post-tool-hook.sh already 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 1 ceiling the slow path already had. post-tool-hook.sh already 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.py gains an end-to-end regression test that drives the real script (not the hand-rolled harness) twice via bash -x, and asserts the second invocation is a cache HIT -- it never re-sources resolve-paths.sh -- rather than merely checking a cache file was written. Red first: fails on the pre-fix script (resolve-paths.sh found 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:auditor returned 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.md added. 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 in user-prompt-hook.sh until #479, and how the ordering was corrected to match post-tool-hook.sh.

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>
@fdaviddpt
fdaviddpt merged commit 91727f2 into main Sep 1, 2026
13 checks passed
@fdaviddpt
fdaviddpt deleted the fix/479 branch September 1, 2026 19:50
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.

The #469 env-cache fix is inert in user-prompt-hook.sh, and the #469 test certifies it against a premise that hook does not meet

1 participant