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 cc-compact
cd /path/to/your/project
cc-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

cc-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 cc-compact   # or: pip install -U cc-compact

If you prefer to copy files (e.g., for portability across machines without the package installed), use --copy:

cc-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 cc_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=cc_compact

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

cc_compact-0.1.9.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

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

cc_compact-0.1.9-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file cc_compact-0.1.9.tar.gz.

File metadata

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

File hashes

Hashes for cc_compact-0.1.9.tar.gz
Algorithm Hash digest
SHA256 0a70f7c362fe8a7adbe4a3acd2da783eb12cc0c615166c77537b2bb76182242a
MD5 23bb9893f31321cdfd77570f168e6fb5
BLAKE2b-256 56109894f852a1fa8ae75af476596d277c02bf85ce5e50f0fe1faeaa1ba4c3d1

See more details on using hashes here.

File details

Details for the file cc_compact-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: cc_compact-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for cc_compact-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 862a41bb97575c958fe0858bca0913b14604b906080351d983d8104b8f94c50e
MD5 3037f0f28c92f1b56a6e4e154ae5524f
BLAKE2b-256 bdedf0167f2c2e314005bde66969df793520695768eae617d49c190860aaa03e

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