Install
openclaw skills install sleep-consolidationUse this skill to consolidate an AI agent's daily experiences and learnings into structured long-term memory, mimicking human sleep-based memory consolidatio...
openclaw skills install sleep-consolidationBiologically-grounded memory consolidation for AI agents, based on:
See references/workspace_layout.md for full file format examples.
See references/memory_schema.md for Markdown conventions and type system.
~/.agent_workspace/
├── MEMORY.md ← long-term: durable facts, preferences, decisions
├── memory/
│ └── YYYY-MM-DD.md ← daily log (append-only; load today + yesterday)
└── bank/
├── entities/ ← per-person or per-project pages
└── concepts/ ← per-topic deep-dives
Core rule: The agent only "remembers" what gets written to disk. Never keep important things in RAM.
| Mode | Biological analog | When to use | Script |
|---|---|---|---|
| micro-rest | Waking neural replay (Eichenlaub 2020) | Mid-session, after any significant exchange | scripts/micro_rest.py |
| nrem | NREM deep sleep, hippocampus→cortex | End of session, context nearing limit | scripts/sleep_cycle.py --phase nrem |
| rem | REM dream synthesis | After NREM, or standalone for insight | scripts/sleep_cycle.py --phase rem |
| both | Full overnight cycle | End of day / long session | scripts/sleep_cycle.py --phase both |
Quick append to today's daily log. No LLM call — pure write. Based on the paper's finding that waking replay occurs during rest blocks immediately after learning.
python scripts/micro_rest.py \
--note "User prefers TypeScript; rejected Python suggestion" \
--type O \
--workspace ~/.agent_workspace
# Pre-compaction flush: extract worth-retaining from raw context
python scripts/micro_rest.py \
--flush \
--context-dump "$(cat session_log.txt)" \
--workspace ~/.agent_workspace
Memory type prefixes (W/B/O/S system from OpenClaw research):
| Prefix | Meaning | Example |
|---|---|---|
W | World fact (objective) | W: Redis default port is 6379 |
B | Biographical/experience | B: Fixed the auth bug by wrapping in try/catch |
O(c=N) | Opinion/preference + confidence 0–1 | O(c=0.9): User prefers concise replies under 200 words |
S | Summary/synthesis (generated) | S: Three sessions used streaming; all had lower latency |
Each entry is appended to memory/YYYY-MM-DD.md under a ## Retain section.
Two-track processing based on the paper's temporal replay findings:
MEMORY.mdbank/ for deeper storagepython scripts/sleep_cycle.py --workspace ~/.agent_workspace --phase nrem
The script reads today's daily log + current MEMORY.md, then calls Claude:
You simulate NREM deep-sleep memory consolidation for an AI agent.
Two-track processing:
FAST TRACK (compressed replay — high-confidence, clear facts):
- Distill into single-sentence durable memories for MEMORY.md
- Prune ~20% as redundant (synaptic homeostasis: sleep removes weak connections)
- Use prefixes: W (world fact), B (experience), O(c=N) (opinion + confidence), S (summary)
SLOW TRACK (dilated replay — weakly-learned, uncertain, needing reinforcement):
- Expand on low-confidence or partial-knowledge items
- Write to bank/ as entity or concept pages
- These are what human NREM prioritizes (Schapiro et al. 2018)
Respond ONLY with valid JSON:
{
"memory_md_updates": [
{
"action": "add|update|remove",
"type": "W|B|O|S",
"confidence": 0.0,
"text": "single sentence",
"replaces": "old text if action=update"
}
],
"bank_updates": [
{
"slug": "topic-slug",
"kind": "entity|concept",
"section": "## Section heading",
"content": "markdown block"
}
],
"pruned_count": 0,
"weakly_learned": ["items for slow-track attention"],
"open_questions": ["gaps for next session"]
}
Creative cross-domain connections. The dreaming brain ignores conventional logic.
python scripts/sleep_cycle.py --workspace ~/.agent_workspace --phase rem
You simulate REM dream-sleep creative synthesis for an AI agent.
Find non-obvious connections. Update beliefs where evidence shifted.
Tasks:
1. Cross-domain links between today's memories and MEMORY.md
2. Update O(c=N) confidence values where evidence has shifted
3. Generate concrete next-session actions
4. Propose new bank/ pages if a topic recurred 3+ times this session
Respond ONLY with valid JSON:
{
"aha_moments": ["string"],
"creative_connections": [{"link": "A ↔ B", "why": "string"}],
"confidence_updates": [
{"item": "text excerpt", "old_c": 0.0, "new_c": 0.0, "reason": "string"}
],
"new_bank_suggestions": [
{"slug": "string", "kind": "entity|concept", "seed_content": "markdown"}
],
"next_session_actions": ["string"]
}
Mirrors OpenClaw's memoryFlush mechanism. Triggered when context approaches token limit. Runs silently (no user output).
Configure the threshold (equivalent to OpenClaw's softThresholdTokens):
# When session token estimate > (context_window - 20000 - 4000):
python scripts/micro_rest.py --flush --context-dump "..." --workspace ~/.agent_workspace
The --flush flag calls Claude to extract durable memories from the raw context dump, then writes them to the daily log. One flush per compaction cycle.
from scripts.load_memory import get_context
system_addendum = get_context(
workspace="~/.agent_workspace",
max_memory_chars=3000, # MEMORY.md snippet (fits ~2k tokens)
load_yesterday=True # also load yesterday's daily log
)
# Inject system_addendum into system prompt
Loaded content:
MEMORY.md (trimmed to max_memory_chars)load_yesterday=True)# Session start: inject memory into system prompt (in Python)
# During session: micro-rest after significant exchanges
python scripts/micro_rest.py --note "..." --type W --workspace ~/.agent_workspace
# If context > 80% full: flush before compaction
python scripts/micro_rest.py --flush --context-dump "..." --workspace ~/.agent_workspace
# Session end: full sleep cycle
python scripts/sleep_cycle.py --phase both --workspace ~/.agent_workspace
| Problem | Solution |
|---|---|
MEMORY.md growing too large | Run with --prune-memory flag — NREM removes redundant entries |
| Bank file stale | Delete it; NREM regenerates next session |
| REM generating hallucinations | Add "Be strictly grounded. No invention." to REM prompt |
| Micro-rest called too often | Add 5-min cooldown check in calling code |
| Flush extracting too little | Lower the softThresholdTokens equivalent; flush earlier |
| Replay not strengthening weak items | Check that NREM prompt slow-track section explicitly lists weakly_learned items |