Memory Engine
Enhancement layer for the manual memory system. This skill does not replace the existing auto-memory behavior — it adds structure, budget awareness, and a session-end checkpoint on top of it.
Core philosophy: suggest, never auto-save. The human decides what's worth keeping.
Memory Directory
All memory files live in ~/.claude/projects/-Users-coni/memory/.
Index file: MEMORY.md (always loaded, pure pointers, no content).
Phase 1: Memory Capture
When saving any memory (whether triggered by you or the session checkpoint), follow this flow every time:
Step 1 — Classify
Five types. Pick the one that fits:
| Type | What it captures | Signals |
|---|---|---|
user | Identity, preferences, values, knowledge | "I'm a...", "I prefer...", personality shifts |
feedback | How to work with the user — corrections AND confirmations | "Don't do that", "Yes, exactly that", approach validated |
project | Active work context, deadlines, goals | "We're doing X by Thursday", milestone hit |
reference | External knowledge, tool configs, pointers | New tool discovered, API configured |
decision | Why X was chosen over Y | "Chose X over Y", comparing tradeoffs, architecture calls |
For decision type: read references/decision-type-spec.md for the frontmatter schema and body structure (Context → Options → Decision → Revisit When).
Step 2 — Deduplicate
Before writing, scan MEMORY.md index for overlapping entries:
- Same topic already covered? → Update the existing file, don't create a new one
- Overlaps with a CLAUDE.md rule? → Don't save (the rule is authoritative)
- Ephemeral task detail? → Don't save (git log / code is the source of truth)
Apply all rules from feedback-memory-hygiene.md — they remain authoritative.
Step 3 — Estimate Token Cost
Calculate: file_bytes / 3.2 (integer, rounds down). This gives a reasonable approximation for mixed CJK/English content without external dependencies.
Step 4 — Write
- Create the memory file with YAML frontmatter (name, description, type)
- Update MEMORY.md index — add entry under the correct section heading
- Append token estimate:
`~NNNNtk`at end of the index line
Example index line:
- [decision-deploy-cloudflare.md](decision-deploy-cloudflare.md) — Deploy to CF Pages instead of Vercel `~280tk`
What NOT to Save
These rules are non-negotiable (from memory-hygiene + auto-memory system):
- Code patterns, architecture, file paths — derivable from code
- Git history, recent changes —
git logis authoritative - Debug solutions — the fix is in the code
- Anything already in CLAUDE.md or
~/cc-personal/rules/ - Ephemeral task state — only useful in current session
- Project status snapshots — read from code and git
Phase 2: Memory Maintenance
/memory-check
Run the audit script and present results:
bash ~/.claude/skills/memory-engine/scripts/memory-health-check.sh
The script outputs a structured report covering:
- Budget: total tokens vs 40,000tk cap (20% of 200K context)
- Type distribution: count and tokens per type
- Top 5 by size: largest files consuming the most budget
- Issues: stale files (>30 days), orphans, broken links, empty types, budget warnings
After presenting the report, suggest specific actions for each issue found:
| Issue | Action |
|---|---|
| STALE | "Review or archive: {file} — still relevant?" |
| ORPHAN | "Add to MEMORY.md index, or delete if obsolete" |
| BROKEN LINK | "Remove from MEMORY.md, or recreate the file" |
| NO ENTRIES | Informational — not all types need entries at all times |
| BUDGET WARNING | "Largest files: ... — consider compacting or splitting" |
After resolving issues, update the MEMORY.md budget comment:
<!-- budget: ~NNNNtk / 40000tk (NN%) | updated: YYYY-MM-DD -->
Token Annotation Format
Every MEMORY.md index entry carries a token estimate suffix:
- [filename.md](filename.md) — description `~NNNNtk`
The budget comment sits at the very top of MEMORY.md (before the heading). It's an HTML comment — invisible in rendered markdown, parseable by scripts.
Phase 3: Seven-Dimension Consolidation Checkpoint
Trigger (Double Safety)
Primary trigger — Prompt-based (CLAUDE.md rules): When the following signals are perceived during conversation, proactively perform a seven-dimension scan:
- Dense tool calls (>15 tool calls)
- Completion of a substantive output (code, analysis, proposal)
- Major topic switch
- User explicitly says "consolidate", "review", "summarize"
Fallback trigger — Stop hook (settings.json):
consolidation-trigger.sh runs every time the agent stops, tracking stop count and interval. When stop count ≥ 20 and time since last trigger ≥ 30 minutes, output a seven-dimension scan prompt.
State file: ~/.claude/projects/-Users-coni/memory/.consolidate-state
Seven-Dimension Scan
Scan the current conversation for each dimension:
| # | Dimension | Hit Signal | Corresponding Memory Type |
|---|---|---|---|
| 1 | Task Closure | A complete task from proposal to delivery | project (milestone) |
| 2 | CLAUDE.md Update | Discovered new universal behavior rules | Write directly to CLAUDE.md |
| 3 | Rules Consolidation | Discovered domain/tool/file-type-specific rules | Write to ~/cc-personal/rules/ |
| 4 | Architecture Decision | Made tool selection, technical approach, deployment strategy — non-trivial choices | decision |
| 5 | Project Milestone | Substantial project progress (release, launch, key feature complete) | project |
| 6 | Collaboration Calibration | User corrected a practice or confirmed a non-obvious practice | feedback |
| 7 | High-Value Analysis | Produced reusable analysis framework, insight, or model across sessions | reference or user |
Execution Flow
Scan seven dimensions → Count hits
Hits 0-1 → Skip silently
Hits 2+ → Output checkpoint suggestions
Rules
- Apply the "What NOT to Save" list strictly — no exceptions at checkpoint time
- Check existing MEMORY.md entries before suggesting — no duplicates
- Maximum 3 candidates per checkpoint — quality over quantity
- If nothing qualifies, output nothing (silent skip, no forced suggestions)
- Never write files during checkpoint — only present candidates for user approval
- Each candidate must note the dimension number hit
Output Format
[Consolidation Check] This conversation hit 3/7 dimensions (④⑥⑦), recommend consolidating:
1. [decision] ④ Chose dual trigger approach (Stop hook + prompt) instead of pure hook — hook cannot perceive conversation semantics
2. [feedback] ⑥ User confirmed: the core of the flywheel is automatic triggering, not memory classification
3. [reference] ⑦ Entity vitality framework: human/company/AI three-entity time and energy allocation model
Save which? Reply with numbers (e.g., 1,2), or "skip" to skip.
When user confirms numbers, execute Phase 1 save flow for each selected item. When user says "skip" or equivalent, do nothing.
Gotchas
- Don't update MEMORY.md budget comment on every save — only during /memory-check. Frequent updates break cache and add noise to git diffs.
- Token estimates are approximations — don't treat them as exact. The /3.2 method is ~90% accurate for mixed CJK/EN content, which is good enough for budget awareness.
- decision type is for non-trivial choices — "I used vim instead of nano" is not a decision worth saving. Reserve it for choices that affect future sessions.
- Stop hook is fallback, not primary — if the user Ctrl+C exits, the hook won't trigger. Proactive scanning is the flywheel's engine.
- Proactive scanning must not interrupt the user's flow — trigger between tasks (phase completion, topic switch), not mid-execution.