Skip to content

post-tool-hook: the last 17 spawns are the merged config, and caching it is a credential-lifetime decision #232

Closed

Description

@fdaviddpt

Split out of #230, which took post-tool-hook.sh from 30 external spawns per tool call to 17 (16 in a real git checkout) without adding any caching. This is the remainder #230 deliberately did not take.

What is left, measured

Warm run, macOS bash 3.2.57, PATH-shim counter (tests/test_post_tool_hook_spawns.py carries the harness):

count command source
1 jq -s (three-layer deep merge) lib-memory-dir.sh
3 jq -r (.timezone, .model, .reject_pattern) log.sh, at source time
2 jq -r (.cooldowns.save_seconds, .thresholds.delta_lines_trigger) post-tool-hook.sh
1 rm -f (merged config, EXIT trap) lib-memory-dir.sh
1 sed (trap -p parsing, to chain the EXIT trap) lib-memory-dir.sh
1 sed (log message sanitisation) log.sh
2 date (+%Y-%m-%d, +%H:%M:%S) log.sh — bash 3.2 only, lib-clock.sh's builtin covers bash ≥ 4.2
1 date +%s routed to date on every bash: %s is a strftime extension lib-clock.sh does not trust the builtin with
1 python3 -V (validate the interpreter) detect-tools.sh
1 git rev-parse lib-memory-dir.sh — only when PROJECT_DIR has no .git directory; 0 in an ordinary checkout
1 ls -t + 1 head -1 post-tool-hook.sh, newest-transcript pick
1 wc -l line count of the transcript
1 mv -f atomic capture-alive write

Seven of the seventeen are the merged config: the jq -s merge, five jq -r reads against it, and the rm that cleans it up. That is the single biggest remaining block and the reason this is filed rather than fixed.

Why #230 did not cache it

lib-env-cache.sh (#227/#229) replays a completed path resolution. post-tool-hook.sh needs config(), which needs the merged config.json itself — today ${TMPDIR}/remember-config-$$.json, created per process, mode 0600, removed by a chaining EXIT trap that tests/test_tmpdir_cleanup.py pins.

Publishing that at a stable path changes three things at once, and only one of them is the obvious one:

  1. Lifetime. Same mtime-against-three-layers invalidation as lib-env-cache.sh would work, and PostToolUse's call rate does not break it — the check is [ -nt ], a builtin, and it is monotone. This part is fine.
  2. Concurrency. The publish path is a private temp file plus mv -f, which is rename(2) — atomic on POSIX, so a reader never sees a partial file even with many hooks racing. Also fine on Unix. Not fine on Windows, where rename over an existing file fails or requires MOVEFILE_REPLACE_EXISTING; Git Bash's mv -f handles it, but a reader holding the file open can make it fail, and this repo supports Git Bash as a first-class platform (Auto-save pipeline broken on Windows/Git Bash: CRLF + single-quote path wrapping in safe_eval #84, session_dir_slug() mis-slugs non-ASCII (CJK) paths under byte-wise sed, silently disabling saves #144, session_dir_slug does not truncate at 200 chars + hash, so deep project paths silently never save #157, Save pipeline silently no-ops when CLAUDE_CONFIG_DIR is set #166, CLAUDE_CONFIG_DIR is not path-normalised on Windows, unlike PROJECT_DIR #169).
  3. Cleanup, and what is inside the file. This is the one that stopped post-tool-hook pays the same resolution prefix on every tool call #230. The merged config can contain haiku.oauth_token — a live OAuth credential (which is exactly why lib-memory-dir.sh creates it 0600 before writing a byte, independent of the caller's umask, Security: set umask 077 at pipeline entry, files created world-readable today #68). Today it is per-PID and trap-removed. At a stable path it is a credential sitting in the system temp dir at a predictable name, with no owner and no removal, for as long as the machine keeps its temp dir. lib-env-cache.sh holds paths; this holds a token. That is not the same decision, and post-tool-hook pays the same resolution prefix on every tool call #230 was not the change to make it in.

What would need deciding

  • Publish the merged config with haiku.* stripped, and have config() fall through to the real merge for those keys? Cheap, and it makes the cached file no more sensitive than the paths cache already is — but it means two config readers, and Duplicate naive session_dir_slug in lib-memory-dir.sh carries the pre-#144 bugs for user-prompt-hook #158 is this repo's scar from exactly that shape.
  • Or leave the merge alone and collapse the reads: log.sh spends three jq -r at source time on .timezone, .model, .reject_pattern, and post-tool-hook.sh two more. One jq invocation returning all five would take 5 spawns to 1 or 2 with no new file, no new lifetime and no credential anywhere new. The cost is a second code path for the jq-less fallback (_jq_fallback / the inlined Python in config()), which tests/test_jq_free_config.py already exercises.

The second option looks like the better ratio and does not require answering the credential question at all. It is not free — config() is read by every script in the pipeline, not just this hook — which is why it wants its own measurement and its own review rather than being tacked onto #230.

Bar

Whatever is done here, the post-conditions in tests/test_post_tool_hook_spawns.py are the floor: PostToolUse is the write path, and a cheap hook that captures nothing is strictly worse than a slow one that works. Lower POST_TOOL_SPAWN_BUDGET in that file as part of the fix, so the improvement is pinned rather than described.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions