# context-mem — Context Optimization

context-mem is active in this project. It compresses tool outputs via 14 content-aware summarizers (99% token savings) and serves optimized context through MCP.

## What it does
- Captures Bash, Read, Write, Edit, Grep, Glob outputs via hooks
- Compresses content (99% token savings) using specialized summarizers
- Provides search, knowledge base, budget tracking, and session continuity
- Runs a real-time dashboard at http://localhost:51893

## Workflow (IMPORTANT — follow this order)

1. **Session start**: Call `restore_session` to recover prior context
2. **Before re-reading files**: Call `search` first — the answer may already be stored
3. **After large outputs**: Call `observe` to compress and store content
4. **Need details on a search result?**: Call `get` with the ID — never guess content
5. **Need chronological context?**: Call `timeline` — optionally with `anchor` ID for before/after view
6. **When learning patterns**: Call `save_knowledge` for decisions, error fixes, API patterns
7. **Periodically**: Call `budget_status` — if >80%, call `restore_session` to save state and reclaim context

## Rules

- ALWAYS `search` before `get` — never guess observation IDs
- ALWAYS `observe` outputs over 500 tokens — keep context clean
- NEVER call `get` without first finding the ID via `search` or `timeline`
- When `budget_status` shows >80%: save your work, call `restore_session`
- When `save_knowledge` returns `contradictions` — review them before proceeding. Do NOT silently overwrite conflicting knowledge. Ask the user or note the conflict.
- Use `source_type` when saving knowledge: `explicit` (user stated directly), `inferred` (you derived from context), `observed` (captured automatically). Trust explicit > inferred > observed.

## Available MCP Tools

- `observe` — store and compress content (auto-summarized)
- `search` / `get` / `timeline` — retrieve stored context (use in this order)
- `stats` — view compression statistics
- `save_knowledge` / `search_knowledge` — persistent knowledge base (with contradiction detection and source tracking)
- `update_profile` — update the project quick profile shown at session start
- `budget_status` / `budget_configure` — token budget management
- `emit_event` / `query_events` — event tracking
- `restore_session` — session continuity + context reclaim
