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.4.tar.gz (33.4 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.4-py3-none-any.whl (28.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: knowhook-0.1.4.tar.gz
  • Upload date:
  • Size: 33.4 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.4.tar.gz
Algorithm Hash digest
SHA256 5f1b111f13d26cedd1365f68ebc048108a59e445737d93188e3762474114cdc2
MD5 ee5acfab7cbaf639bd694a7f36169e6f
BLAKE2b-256 d717d13b4ca12b8b6be85a626923d7e2bb42a9199d0c75ecfb0985af3ff50207

See more details on using hashes here.

File details

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

File metadata

  • Download URL: knowhook-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 28.6 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 83ff82d03a58b7a2f0535e523bc0140ca26f943fe95eadddc2ab13fa8df8b25f
MD5 180e80d0be01849fa1292647c8b72795
BLAKE2b-256 8e0986084f90156ab3baff5ed74aac8b65c7ccf43e62ff80e5362c374d62f4bd

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