Dream Cycle (Consolidation)
Relevant source files
The following files were used as context for generating this wiki page:
The CORTEX Dream Cycle executes nightly as a two-phase synthetic sleep process for AI agents. It manages memory maintenance by consolidating important signals, pruning noise, and synthesizing novel associations between disparate memories. The system bases this architecture on the biological two-stage model of sleep, consisting of Slow-Wave Sleep (SWS) for declarative maintenance and Rapid Eye Movement (REM) for creative synthesis.
Sources: README.md:144-150, src/dream/dream-cycle.ts:33-41
Architecture and Phases
The cycle divides into five distinct phases categorized under SWS and REM stages. You can trigger the full cycle or target specific phases through the runDreamCycle function.
Phase Workflow Diagram
This diagram shows the sequential execution of the five phases during a "full" dream cycle.
Slow-Wave Sleep (SWS) Stages
Phase 1: Resonance Analysis
The system updates memory resonance scores using an Ebbinghaus stability-adjusted decay curve. This mechanism ensures that frequently accessed and highly connected memories decay slower than isolated ones. The calculation incorporates access counts, connectivity scores (sum of synapse strengths), priority weights, and novelty.
- Stability Formula:
1 + (0.3 * ln(access_count + 1)) + (0.2 * connectivity_score) - Resonance Components: Decay (15%), Frequency (20%), Connectivity (25%), Priority (20%), Feedback (10%), and Novelty (10%).
Sources: src/dream/dream-cycle.ts:145-161, src/dream/dream-cycle.ts:167-184
Phase 2: Adaptive Pruning
The pruner uses adaptive percentile-based thresholds instead of hardcoded values to prevent over-pruning in sparse memory sets. It calculates boundaries based on the current agent's resonance distribution.
| Tier | Condition | Action |
|---|---|---|
| Tier 1 | Resonance < P5 AND Age > 30 days | DELETE |
| Tier 2 | Resonance < P15 AND Age > 14 days | ARCHIVE |
| Tier 3 | Connection strength < 0.1 | DELETE Synapse |
| Tier 4 | Observation source AND Age > 7 days | DELETE |
Note: P0/P1 priority memories and emotionally salient memories (decay resistance > 0.4) are protected from pruning.
Sources: src/dream/dream-cycle.ts:208-275, benchmarks/cogbench/tasks/emotional-recall.ts:21-25
Phase 3: Cluster Consolidation
The system identifies high-resonance clusters using a Union-Find algorithm on the synapse graph. For significant clusters (3+ members), the system:
- Generates an abstractive summary via LLM to synthesize insights.
- Stores the summary as a
cognitive_artifact. - Strengthens intra-cluster synapses by +0.2.
Sources: src/dream/dream-cycle.ts:286-407, src/db/schema.ts:203-219
Rapid Eye Movement (REM) Stages
Phase 4: Free Association
The system randomly activates memory nodes to discover novel connections. It evaluates pairwise similarity in two spaces:
- Dense Space: Uses cosine similarity (pgvector) to find connections in the 0.6–0.85 range.
- Sparse Space: Uses Hippocampal Dentate Gyrus (DG) overlap (0.15–0.45) to find structurally similar but semantically divergent connections.
Sources: src/dream/dream-cycle.ts:420-534, README.md:151-158
Phase 5: Synthesis
The system examines recently formed weak synapses (0.2–0.4 strength) from Phase 4. It extracts shared entities or semantic threads and records them as "synthesis" artifacts, identifying actionable implications between disparate sources.
Sources: src/dream/dream-cycle.ts:544-604, src/mcp/server.ts:684-696
API and Tool Integration
The Dream Cycle is accessible via REST API and MCP tools for external triggering and monitoring.
API Endpoints
POST /api/v1/dream
| Parameter | Type | Description |
|---|---|---|
agentId | string | External ID of the agent |
cycleType | string | "full", "sws_only", "rem_only", "resonance_only", "pruning_only", "consolidation_only" |
Sources: src/api/dream.ts:13-50, src/dream/dream-cycle.ts:42-44
Data Persistence (Logs)
The system tracks every cycle in the dream_cycle_logs table, storing duration, phase-specific counters (e.g., memoriesDeleted, clustersFound), and discovered insights.
Conclusion
The Dream Cycle (Consolidation) subsystem transforms static memory storage into a dynamic learning system. By nightly pruning irrelevant context and synthesizing new connections, it maintains agent performance over long durations, preventing identity drift and context window saturation.