-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpre-compact.sh
More file actions
executable file
·51 lines (40 loc) · 1.52 KB
/
pre-compact.sh
File metadata and controls
executable file
·51 lines (40 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
# PreCompact hook (Claude Code v2.1.105+).
# Fires before context compaction. Saves a tiny note about what was
# happening so the post-compact session can pick up where the pre-compact
# session left off.
#
# Exit codes:
# 0 = allow compaction
# 2 = block compaction (printed message goes to the model)
#
# This default implementation is permissive (always exits 0) and just
# writes a checkpoint file. Customize block-conditions to fit your team
# (e.g., block if there's an in-progress refactor with no tests yet).
set -u
payload="$(cat)"
if ! command -v jq >/dev/null 2>&1; then
exit 0
fi
mkdir -p .claude/checkpoints
# Pull useful context from the payload to drop a checkpoint.
session_id="$(printf '%s' "$payload" | jq -r '.session_id // "unknown"')"
ts="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
checkpoint=".claude/checkpoints/pre-compact-${ts}-${session_id:0:8}.md"
# Best-effort git context for the checkpoint.
branch="$(git branch --show-current 2>/dev/null || echo unknown)"
dirty="$(git status --porcelain 2>/dev/null | wc -l | tr -d ' ')"
last_commit="$(git log -1 --oneline 2>/dev/null || echo none)"
cat > "$checkpoint" <<EOF
# Pre-compact checkpoint
Session: $session_id
Timestamp: $ts
Branch: $branch
Uncommitted files: $dirty
Last commit: $last_commit
(Compaction is about to drop earlier context. Use this checkpoint plus
the conversation transcript to reconstruct what was in flight.)
EOF
# Tell the model that a checkpoint was saved (optional notice).
echo "Pre-compact checkpoint saved to $checkpoint" >&2
exit 0