# pre-compact-state: an agent hook by Scalably

Canonical: https://scalably.io/hooks/pre-compact-state
Source: https://github.com/scalably-io/agent-hooks/blob/v1.0.0/hooks/pre-compact-state.sh
Runs on: PreCompact
Release: v1.0.0 at commit 53dcece
Integrity: sha256 of the script served at https://scalably.io/hooks/pre-compact-state.sh is e8757173eb22e4b903fe5fd20278d31c104271e726f88c7607eda48487a97436. Verify: curl -s https://scalably.io/hooks/pre-compact-state.sh | shasum -a 256
This is the machine-readable representation of the page at the canonical URL. Same facts, denser format.

## Summary

PreCompact hook: write a small state snapshot before the context is compacted.

## Install

```
/plugin marketplace add scalably-io/agent-skills
/plugin install agent-hooks@scalably-agent-skills
```

## What it does

Why this exists: compaction is not a save point. Anything the session knew but
never wrote down is gone afterwards, and a session that is killed before it
finishes leaves nothing behind at all. This writes the cheap half of that
problem to disk, so the next session can read where the work actually was.

It deliberately does NOT re-inject rules or instructions. Always-loaded context
survives compaction on its own, and duplicating it wastes the window this hook
is trying to protect.

Reads:  PreCompact JSON on stdin (session_id, cwd, compaction_trigger)
Writes: <cwd>/.claude/compact-state.md
Exit:   always 0. A snapshot failure must never block compaction.

## The whole script

```bash
#!/usr/bin/env bash
# PreCompact hook: write a small state snapshot before the context is compacted.
#
# Why this exists: compaction is not a save point. Anything the session knew but
# never wrote down is gone afterwards, and a session that is killed before it
# finishes leaves nothing behind at all. This writes the cheap half of that
# problem to disk, so the next session can read where the work actually was.
#
# It deliberately does NOT re-inject rules or instructions. Always-loaded context
# survives compaction on its own, and duplicating it wastes the window this hook
# is trying to protect.
#
# Reads:  PreCompact JSON on stdin (session_id, cwd, compaction_trigger)
# Writes: <cwd>/.claude/compact-state.md
# Exit:   always 0. A snapshot failure must never block compaction.

input=$(cat)
session_id=$(printf '%s' "$input" | jq -r '.session_id // "unknown"' 2>/dev/null) || session_id="unknown"
cwd=$(printf '%s' "$input" | jq -r '.cwd // "."' 2>/dev/null) || cwd="."
trigger=$(printf '%s' "$input" | jq -r '.compaction_trigger // "auto"' 2>/dev/null) || trigger="auto"

[ -n "$cwd" ] || cwd="."
[ -d "$cwd" ] || exit 0

mkdir -p "${cwd}/.claude" 2>/dev/null || exit 0
STATE_FILE="${cwd}/.claude/compact-state.md"

{
  echo "# Compaction snapshot"
  echo "session: ${session_id}"
  echo "compacted: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
  echo "trigger: ${trigger}"
  echo "cwd: ${cwd}"
} > "$STATE_FILE" 2>/dev/null || exit 0

if git -C "$cwd" rev-parse --git-dir >/dev/null 2>&1; then
  {
    echo ""
    echo "## Git state"
    echo '```'
    echo "Branch: $(git -C "$cwd" branch --show-current 2>/dev/null)"
    git -C "$cwd" status --short 2>/dev/null
    echo "Recent commits:"
    git -C "$cwd" log --oneline -5 2>/dev/null
    echo '```'
  } >> "$STATE_FILE" 2>/dev/null
fi

exit 0
```
