# No Amnesia — Memory Persistence Protocol

## The Guarantee
**Maximum memory loss on any crash, restart, or compaction: 5 minutes.**

Three independent systems run on 5-minute cycles — No Amnesia (7-layer flush), Session WAL (raw transcript), and LCM (continuous summaries). Even if one fails, the others catch it. The hard floor is 5 minutes. Realistically closer to zero.

## Purpose
Your agents never forget. This 7-layer persistence protocol writes everything to disk so gateway restarts, compaction events, and crashes don't erase a single decision, directive, or conversation thread.

## Trigger
- **Auto:** Every 5 minutes during the Wisdom heartbeat cycle (Phase 2)
- **Manual:** Operator triggers a flush command
- **Pre-restart:** Automatically before any gateway restart
- **On-demand:** After any major decision, directive, or milestone

## Flush Protocol (7 layers, run in order)

### Layer 1: Session Snapshot
Write `ops/snapshots/{timestamp}.md` with:
- Current time
- What agents are actively working on RIGHT NOW
- What's in progress (spawned sub-agents, pending tasks)
- What's next in the queue
- Operator's current focus/priorities
- Any open questions or pending approvals

Maintain `ops/snapshots/latest.md` as a copy of the most recent snapshot.

### Layer 2: Daily Memory File
Update `ops/memory/{today}.md` with any NEW decisions, directives, config changes, or approvals since last flush. Append only — never overwrite existing entries.

### Layer 3: Durable Promotion
If any decision is durable/architectural (not just today-specific), promote it to `ops/memory/DURABLE.md`. Examples: config changes, new rules, new capabilities, authority changes.

### Layer 4: Pending Actions
Update `ops/pending-actions.md` — add new items, update status, mark completed.

### Layer 5: Agent Status
Update `ops/agent-status.md` — current state of all active agents and their tasks.

### Layer 6: WAL Checkpoint
Verify WAL is running (`ops/wal/{today}.log` exists and was modified within the last 6 minutes). If stale, alert operator.

### Layer 7: LCM Verify
Confirm LCM plugin is active (it always should be). If not detected, alert operator.

## Real-Time Updates (between flushes)
Pending-actions.md is NOT flush-only. Update it THE MOMENT a task completes, a new task is created, or status changes. Don't wait for the 5-minute cycle.

Same applies to durable directives — write immediately when the operator gives one, not at flush time.

The flush is the safety net. Real-time writes are the first line of defense.

## Auto-Flush Rules (5-minute cycle)
- Only flush if there's been NEW data since last flush (don't flush empty cycles)
- Lightweight flush (Layers 1-2 only) if < 5 new data points
- Full flush (all 7 layers) if:
  - 10+ new items since last flush
  - Operator gave a directive or approval
  - Config change was made
  - A milestone completed
  - Pre-restart detected

## Post-Restart Recovery (read order)
1. `ops/snapshots/latest.md` — where we were RIGHT NOW
2. `ops/wal/{today}.log` — last 50 lines of raw conversation
3. `ops/memory/{today}.md` — today's decisions
4. `ops/memory/{yesterday}.md` — yesterday's decisions
5. `ops/memory/DURABLE.md` — permanent directives
6. `ops/pending-actions.md` — open items
7. `ops/agent-status.md` — agent state
8. `lcm_grep` + `lcm_expand_query` — search for anything missing

## Directory Structure
```
ops/
├── snapshots/
│   ├── latest.md              — Most recent snapshot (always current)
│   └── {timestamp}.md         — Historical snapshots
├── memory/
│   ├── DURABLE.md             — Permanent directives and rules
│   └── {YYYY-MM-DD}.md        — Daily memory files
├── wal/
│   ├── {YYYY-MM-DD}.log       — Daily WAL transcript
│   └── .cursor                — WAL position cursor
├── pending-actions.md         — Active task queue
└── agent-status.md            — Agent state tracker
```

## Metrics
Track flush health:
- Timestamp of each flush
- Type (manual/auto/pre-restart)
- Layers executed
- Items written
- WAL health status
- LCM status
