| name | memory-compaction |
| description | MUST use when user says 'compact memory', 'compact [file]', 'check budgets', 'set budget', 'restore compaction', or before any 'save' whose target memory file exceeds its budget in compaction/compaction-policy.md. |
Memory Compaction — Budget-Aware Memory Skill
Keep memory small without making it forget.
Activation
When this skill activates for a compaction pass, output:
Checking memory budgets before saving.
Never rewrite a memory file without first writing a snapshot.
Context Guard
| Context | Status |
|---|
| User says "compact memory" | ACTIVE — compact all over-budget files |
| User says "compact [file]" | ACTIVE — compact one file |
| User says "check budgets" | ACTIVE — report sizes vs budgets |
| User says "set budget ..." | ACTIVE — update policy |
| User says "restore compaction ..." | ACTIVE — roll back from snapshot |
| Generic "save" on a budgeted file over budget | ACTIVE — compact then save |
| Save on a file under budget | DORMANT — save normally |
| No save or compaction request | DORMANT — no action |
Budget Check Protocol
Before writing any save to a budgeted file:
- Read
compaction/compaction-policy.md for the file's budget and newest-tier size.
- Measure the current file (lines or chars per the policy unit).
- If under budget → save normally, no compaction.
- If over budget → run the Compaction Protocol below, then save.
Compaction Protocol
Step 1: Snapshot
Step 2: Split Into Tiers
Deterministic boundary: the newest tier is the last N entries named in the policy.
The oldest tier is everything below the newest tier, down to 30% of the file's total
line count (rounded to whole entries). Anything between is the mid-age tier. This keeps
compaction results consistent across sessions.
Step 3: Compact the Oldest Tier
Step 4: Rewrite
## Compacted History
*Distilled from [N] entries on [YYYY-MM-DD]. Full detail in compaction/snapshots/[file]-[date].md.*
- [fact / decision / preference, one dense bullet each]
Step 5: Verify and Report
Compacted [N] entries ([X] lines) into a [Y]-line history block.
[file] is now [Z]/[budget] lines. Snapshot: compaction/snapshots/[file]-[date].md
Restore Protocol
When the user says restore compaction [snapshot]:
- Confirm the snapshot exists in
compaction/snapshots/.
- Show the user the snapshot date and size.
- Ask for confirmation before overwriting the current file.
- Replace the current file with the snapshot contents.
Safety Rules
- Snapshot before every compaction — no exceptions.
- Never compact the newest tier.
- Preserve all facts, decisions, and preferences.
- Never summarize secrets into a shared
Compacted History block.
- Confirm before first compaction of a user-customized file.
- Compaction is summarization, not deletion — knowledge moves to a denser form, it does not disappear.
A good compaction is one the user never notices, except that memory stopped overflowing.