Skip to main content

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 TodoWrite snapshot, 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 ## Preferences section 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.

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

claude_smart_compact-0.1.1.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

claude_smart_compact-0.1.1-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file claude_smart_compact-0.1.1.tar.gz.

File metadata

  • Download URL: claude_smart_compact-0.1.1.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for claude_smart_compact-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c626f1d7d69f4ea93d26304342b6fd881964c5225d824c0867ad094c365105dd
MD5 e5bc5f18942dd2e6e617cf23360e8dd7
BLAKE2b-256 23374f5f13a0e4f970b459715bcd6730d08965001e5ebf4f6619d11a3f22c085

See more details on using hashes here.

File details

Details for the file claude_smart_compact-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for claude_smart_compact-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9b9946b60f6feb981556ae7e46a3816cea83a93cb8eae4ab704262109ce9a461
MD5 a1f329cbf7eb40ad7b59b7b855f4bfb7
BLAKE2b-256 626f0ad8c2038626b8cba34d5687818102c913f1de67f6b7515e98034437695d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page