Claude Code hooks that preserve task state and user preferences across auto-compaction
Project description
Claude Smart Compact
Two Claude Code CLI hooks that preserve task state, in-progress todos, and user preferences across auto-compaction, without bloating the context window.
How it works
- PreCompact runs when the CLI is about to auto-compact. It reads the
session transcript, extracts the last user message + in-flight turns + the
latest
TodoWritesnapshot, and writes a Markdown memory file to<project>/.claude/compact-memory/<session_id>.md. - UserPromptSubmit runs on every user prompt after the first compaction. It injects a short pointer telling the agent the memory file is available and may be read on demand.
- Preferences are agent-authored — the hook preserves a
## Preferencessection on every run, but does not populate it automatically. Append with the Edit tool when the user states a lasting preference.
Install
From PyPI:
pip install claude-smart-compact
cd /path/to/your/project
claude-smart-compact install
The installer automatically creates or updates .claude/settings.json with the required hook entries,
preserving any existing configuration. To skip this and manage settings.json manually, pass --no-settings.
Upgrading
claude-smart-compact install creates symbolic links into the installed
package by default. After upgrading the package, your hooks in every
project are automatically up-to-date — no need to re-run install per
project:
pipx upgrade claude-smart-compact # or: pip install -U claude-smart-compact
If you prefer to copy files (e.g., for portability across machines without
the package installed), use --copy:
claude-smart-compact install --copy # needs --force to redeploy after upgrades
Windows falls back to --copy automatically (symlinks require admin rights).
Manual (without pip)
If you prefer not to pip install, copy claude_smart_compact/ into
your project's .claude/hooks/ directly. The deployed layout is:
<project>/.claude/hooks/
├── pre_compact.py
├── user_prompt.py
└── lib/
├── core.py
├── memory.py
└── transcript.py
Verify
Run the manual trace script:
python3 tests/trace_run.py tests/fixtures/transcript_with_todos.jsonl
Run tests
pip install -e ".[dev]"
pytest --cov=hooks
Debug
Every hook run appends to <project>/.claude/compact-memory/<session_id>.trace.jsonl.
tail -f this file to watch the hooks work in real time.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file claude_smart_compact-0.1.8.tar.gz.
File metadata
- Download URL: claude_smart_compact-0.1.8.tar.gz
- Upload date:
- Size: 22.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cda123501d80832f4d84baea8446971df0fcc4af73a9b82347b6859ad6ab57f4
|
|
| MD5 |
f0fdb30d19beb48b285c8713d1b37069
|
|
| BLAKE2b-256 |
5a5f5b6b01ec7306be259170b4d789944da8d1f72e745bb1014015f933563844
|
File details
Details for the file claude_smart_compact-0.1.8-py3-none-any.whl.
File metadata
- Download URL: claude_smart_compact-0.1.8-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
261a611c0a4a3c2322e6afede40b2d4dd9881900e7cc3defec34b58c38a3465b
|
|
| MD5 |
af9725e8c9f1b47aaa50134678c2835d
|
|
| BLAKE2b-256 |
7f77c201d8ea52854fa33d15a7a92fbe01dc28f7eb1e67bf7c014e272885111d
|