Skip to main content

Compaction-resistant context layer for AI coding agents

Project description

knowhook

Compaction-resistant context layer for AI coding agents.

When you use Claude Code (or any AI agent) in long sessions, auto-compaction summarizes and drops older messages to free up the context window. After compaction, the agent forgets architectural decisions, file dependencies, rejected approaches, and what work is done vs pending.

knowhook fixes this. It hooks into Claude Code's lifecycle to snapshot context before compaction and inject it back after — so Claude picks up exactly where it left off.

Install

pip install knowhook

Setup (2 commands)

know init                  # Create .context.yaml in your project
know claude install        # Install Claude Code hooks + teach Claude about know

That's it. Everything else is automatic.

What happens automatically

After know claude install, three hooks run in the background:

Hook Trigger What it does
PostToolUse Every file Edit/Write Tracks which files Claude touched
PreCompact Before compaction Snapshots all context to .know/snapshots/
SessionStart After compaction Injects ~500 token context briefing back into Claude

Claude also gets a rules file (.claude/rules/know-context.md) that it re-reads every turn — this survives compaction by design.

Commands

Core workflow

know decide "Using JWT not sessions" -r "Stateless for API gateway"   # Log a decision
know map                    # Generate dependency map of your project
know snapshot               # Manually create a context snapshot
know restore                # Print latest snapshot (pipe-friendly)
know sync                   # Regenerate .claude/rules/know-context.md

Project setup

know init [DIRECTORY]       # Create .context.yaml
know show                   # Display current config
know edit                   # Edit .context.yaml interactively
know update                 # Quick-edit task, goals, rules from CLI

Claude Code integration

know claude install         # Install hooks + usage rules
know claude uninstall       # Remove hooks + usage rules

Interactive TUI

know                        # Launch interactive dashboard (no arguments)

Slash commands inside the TUI: /snapshot, /restore, /decide, /map, /sync, /init, /show, /edit, /update, /help, /quit

How it works

  DURING SESSION
  Claude edits files --> PostToolUse hook --> know tracks touched files
  User runs `know decide` --> logs decision to .know/decisions.json

  BEFORE COMPACTION
  Auto-compaction triggers --> PreCompact hook --> know snapshot
    Saves: task, goals, decisions, files touched, dependency edges

  AFTER COMPACTION
  SessionStart hook --> know restore --> injects context briefing into Claude
    Claude sees: structured ~500 token briefing with everything it needs

  ALWAYS LOADED
  .claude/rules/know-context.md --> auto-generated rules file
    Contains: current task, decisions, rules, key files
    Survives compaction because Claude re-reads rules from disk

What Claude gets after compaction

=== KNOW: Post-Compaction Context Recovery ===
PROJECT: my-project
CURRENT TASK [42]: Implement JWT auth (status: active)
GOALS: Stateless API auth, Token refresh endpoint

DECISIONS:
- [D1] Using JWT not sessions — stateless for API gateway
- [D2] Refresh tokens in Redis — fast TTL expiry

RULES:
- No print statements
- All endpoints require auth middleware

FILES TOUCHED THIS SESSION:
- auth.py (edit), db.py (edit), models.py (write)

DEPENDENCIES:
- auth.py -> db.py (imports UserModel)
- routes/api.py -> auth.py (imports require_auth)

COMPLETED: Set up JWT signing, Created User model
PENDING: Refresh token endpoint, Token revocation
REJECTED: Session-based auth (horizontal scaling issues)
=== END KNOW CONTEXT ===

.context.yaml schema

project_name: my-project
goals:
  - Define project goals
current_task:
  id: "1"
  description: Initial setup
  status: active        # active | skipped | completed
tech_stack:
  - python
  - fastapi
rules:
  - No print statements in production code

Data layout

.know/                          # Project-local, created automatically
  snapshots/                    # Timestamped JSON snapshots
  decisions.json                # Architectural decisions log
  map.json                      # Dependency map cache
  session.json                  # Current session file tracker

.claude/
  rules/know-context.md         # Auto-generated, always loaded by Claude
  rules/know-usage.md           # Teaches Claude about know commands
  settings.local.json           # Hook configuration

Who benefits most

  • Long sessions (1hr+) — multiple compactions, high context loss risk
  • Multi-file refactors — touching 10+ files, easy to forget earlier changes
  • Architecture-heavy work — decisions that must stay consistent
  • Multi-day projectsknow restore brings yesterday's context back

Requirements

  • Python 3.10+
  • Claude Code (for hooks integration)

License

MIT

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

knowhook-0.1.3.tar.gz (30.7 kB view details)

Uploaded Source

Built Distribution

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

knowhook-0.1.3-py3-none-any.whl (26.4 kB view details)

Uploaded Python 3

File details

Details for the file knowhook-0.1.3.tar.gz.

File metadata

  • Download URL: knowhook-0.1.3.tar.gz
  • Upload date:
  • Size: 30.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for knowhook-0.1.3.tar.gz
Algorithm Hash digest
SHA256 865dcbf0e8e36355032927f2a3d47e43a872d3fb6b79331d2202671fefa64654
MD5 799a27e6af7bba462c0688b225518ff9
BLAKE2b-256 d2548d60c82d44c641630bd521fba12a52e2cd22fd7a65b6f53a199b85d5e582

See more details on using hashes here.

File details

Details for the file knowhook-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: knowhook-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 26.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for knowhook-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 05d76c8d7a4acbfe73edc1ef754af280438c4283aa82fb57b14e4cdae9004092
MD5 ac01b4d9a50427a96b5f9edb629ade46
BLAKE2b-256 bb20fad622c44fcd472b64f79dc2ed71805d373515c52f02082c6ae89a4ef68f

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