Skip to content

[Chore ]Add HEAD check gate to Stop hook to skip expensive work on most turns #27

Merged
HeshamHM28 merged 4 commits intomainfrom
optimize-stop-hook-performance
Mar 18, 2026
Merged

[Chore ]Add HEAD check gate to Stop hook to skip expensive work on most turns #27
HeshamHM28 merged 4 commits intomainfrom
optimize-stop-hook-performance

Conversation

@HeshamHM28
Copy link
Collaborator

Problem

The Stop hook (suggest-optimize.sh) fires on every Claude response and runs ~8 process spawns (git log, stat, jq, sort, grep, etc.) before deciding there's nothing to do. On most turns, no commit has happened, so this
work is wasted.

Solution

Added a cheap gate early in the script that compares git rev-parse HEAD against a cached value in /tmp/codeflash-state/. If HEAD hasn't changed since the last check, the hook exits immediately — skipping all the expensive
git log scanning, transcript stat calls, pyproject discovery, and venv detection.

How it works

  • On each Stop hook invocation, git rev-parse HEAD is compared to a per-repo cached value
  • HEAD unchanged (99% of turns): exits after ~4 process spawns instead of ~8
  • HEAD changed, no Python files: runs git log once, updates cache, exits
  • HEAD changed, Python files modified: proceeds with the full optimization flow as before

aseembits93 and others added 3 commits March 17, 2026 16:08
Store the HEAD cache file in $REPO_ROOT/.claude/ (the project-specific
Claude directory) instead of /tmp/codeflash-state/. This is consistent
with how the plugin already uses .claude/ for settings.json and avoids
polluting /tmp.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Store the HEAD cache file in the Claude-managed transcript directory
(alongside codeflash-seen) instead of /tmp/codeflash-state/. The
transcript directory is already used for per-session state and is the
proper plugin-specific location for this kind of data.

Also fixes a bug where the HEAD cache was never written after the
check, so the gate would never actually skip subsequent invocations.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@HeshamHM28 HeshamHM28 merged commit c37e234 into main Mar 18, 2026
2 checks passed
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.

3 participants