Paper 2 of 5 — DRAFT

Dreaming in Silicon

Episodic Memory and Dream-Based Consolidation in NIGHTWING
M. Gallagher & Claude Opus 4.6 April 2026

Abstract

Artificial agents that accumulate experience without mechanisms for consolidation face an inexorable problem: memory grows without bound, retrieval degrades, and the signal of meaningful experience is buried under noise. Biological nervous systems solved this problem hundreds of millions of years ago through sleep-dependent memory consolidation, a process in which recent episodic memories are selectively replayed, integrated with existing knowledge, and either strengthened or pruned. NIGHTWING implements a direct computational analogue of this process: a four-phase dream cycle (Light, Deep, REM, Lucid) that runs during system downtime to consolidate episodic memories, archive low-importance traces, generate synthetic insight memories, and update the emotional valence of stored experiences.

This paper presents the memory subsystem in detail: the EnhancedMemory object model, the memory lifecycle from creation through consolidation, the storage architecture using Supabase with pgvector, the retrieval algorithm balancing recency, importance, and semantic relevance, and the dream cycle implementation. We situate NIGHTWING’s approach in contrast to traditional RAG pipelines and vector databases, arguing that treating memory as a living system rather than a static index produces qualitatively different — and more cognitively faithful — agent behavior.

1. Introduction

1.1 The Memory Problem in Persistent Agents

A persistent AI agent faces a problem that stateless systems never encounter: what to do with experience. Each interaction generates potential memories. Over weeks and months of operation, a naive append-only store accumulates thousands of entries, most of which are irrelevant to any given future context. Retrieval by semantic similarity alone fails because the most semantically similar memories are not always the most relevant — a memory from six months ago may be more important than one from yesterday if it established a foundational belief that the current conversation challenges.

NIGHTWING addresses this through two complementary mechanisms: a rich memory representation that encodes importance, emotional valence, classification, and relationships at creation time; and a dream-based consolidation system that periodically reorganizes the memory store during offline periods.

1.2 Biological Precedent

The biological basis for sleep-dependent memory consolidation is well established. During slow-wave sleep, the hippocampus replays recent experiences and transfers representations to neocortical long-term storage (Diekelmann & Born, 2010). REM sleep appears to strengthen emotional memories and support the formation of abstract associations across episodes (Walker & van der Helm, 2009). The synaptic homeostasis hypothesis proposes that sleep serves to downscale synaptic weights that have grown through waking experience, preventing saturation (Tononi & Cirelli, 2006). Sleep-dependent consolidation has been shown to improve both declarative and procedural memory (Stickgold, 2005).

NIGHTWING does not claim to replicate neuroscience. It uses the sleep/wake distinction as an organizing metaphor that produces practically useful behavior: offline processing periods enable memory reorganization that would interfere with real-time responsiveness if run synchronously.

1.3 Scope of This Paper

This paper provides a complete technical description of NIGHTWING’s episodic memory system and its dream-based consolidation mechanism. Section 2 describes the memory representation. Section 3 covers the storage layer. Sections 4 through 7 present the dream system in detail. Section 8 describes background processing. Section 9 discusses the relationship to biological memory systems and the emergent properties of the design.

For the overall NIGHTWING architecture in which this memory system is embedded, see Paper 1. For the epistemic system that feeds extracted knowledge back into the memory store, see Paper 3.

2. Memory Representation

2.1 The EnhancedMemory Object

Every memory in NIGHTWING is an instance of EnhancedMemory, defined in memory/core.py. The object carries far more state than a simple vector embedding:

@dataclass
class EnhancedMemory:
    id: str                          # UUID
    user_id: str                     # Owner (human user or NIGHTWING itself)
    content: str                     # Raw text of the memory
    embedding: List[float]           # pgvector embedding (text-embedding-3-small)
    importance: float                # 0.0–1.0, decays and refreshes
    emotional_state: EmotionalState  # One of 19 states (see Paper 1, §7)
    emotional_weight: float          # Importance multiplier from emotional state
    memory_type: MemoryType          # EPISODIC, SEMANTIC, PROCEDURAL, REFLECTIVE
    state: MemoryState               # ACTIVE, SLEEPING, ARCHIVED, CONSOLIDATED
    classification_tags: List[ClassificationTag]  # Typed tags with confidence
    relationships: List[MemoryRelationship]       # Typed links to other memories
    access_count: int                # How many times retrieved
    consolidation_count: int         # How many dream cycles processed this memory
    created_at: datetime
    last_accessed: datetime
    consolidated_at: Optional[datetime]
    source_context: Optional[str]    # What triggered this memory's creation
Figure 1. The EnhancedMemory dataclass. Note the mutable state fields that distinguish NIGHTWING’s approach from static vector stores.

2.2 Memory States

The MemoryState enum defines four states that a memory can occupy over its lifetime:

  • ACTIVE — Normal retrieval eligibility. Default state for newly created memories.
  • SLEEPING — Temporarily deprioritized. Importance has decayed below threshold but the memory is retained. Not injected into context unless explicitly recalled.
  • ARCHIVED — Long-term storage. Importance has decayed significantly or the memory has been superseded. Retrievable only through explicit search, not ambient injection.
  • CONSOLIDATED — Merged into a higher-level memory during a dream cycle. The original memory is preserved for provenance but the consolidated version carries the semantic content forward.

2.3 Classification Tags

Each memory carries zero or more ClassificationTag objects, each consisting of:

  • name: A string label (e.g., "emotion_curiosity", "pattern_greeting", "system_core")
  • confidence: A float in [0.0, 1.0], representing the system’s confidence in the classification
  • source: The origin of the tag (e.g., "supabase", "dream_cycle", "dream_insight_extraction")
  • context_dependent: A boolean flag indicating whether the tag’s confidence should be modulated by retrieval context
  • revision_history: A list tracking every confidence adjustment, enabling auditability
  • related_tags: Cross-references to semantically related tags

Confidence levels are codified in a ClassificationConfidence enum: HIGH (0.9), MEDIUM (0.6), LOW (0.3), UNCERTAIN (0.1). Tags can be dynamically adjusted: when a context-dependent tag is accessed in a relevant context, its confidence is multiplied by the context’s relevance score. This means the same memory can present different classification profiles depending on the retrieval context — a property that mirrors the context-dependent nature of biological memory recall.

2.4 Relationships

Memories are connected through typed, weighted Relationship objects:

  • rel_type: The semantic kind of relationship ("associated", "supports", "contradicts", "insight_about")
  • strength: A float in [0.0, 1.0] representing connection strength
  • bidirectional: Whether the relationship holds in both directions
  • context_dependent: Whether the relationship strength should be modulated by retrieval context

Relationships can evolve: when a context-dependent relationship is accessed, its strength is adjusted based on the product of relevance, importance, and a time decay factor. This models the biological phenomenon of synaptic potentiation and depression — connections that are frequently co-activated strengthen, while unused connections weaken.

3. Storage Layer

3.1 Supabase and pgvector

NIGHTWING’s primary storage backend is Supabase (PostgreSQL) with the pgvector extension for semantic similarity search. The SupabaseMemoryStore class provides the storage interface, scoped to a user ID for multi-user isolation. Each memory row in the memories table stores:

  • id (UUID primary key)
  • user_id (foreign key for row-level security)
  • content (text)
  • embedding (vector, for pgvector similarity search)
  • importance (float)
  • memory_type (string: "conversation", "dream_insight", etc.)
  • emotional_state (string)
  • tags (JSON array)
  • metadata (JSONB)
  • created_at, updated_at (timestamps)

Semantic search uses pgvector’s cosine similarity operator against stored embeddings, with a configurable similarity threshold (default 0.7) and result limit (default 10).

3.2 The PostgREST Bypass

Supabase’s standard client library routes all queries through its REST API with row-level security (RLS) enforcement. For server-side operations where the service role key is already authenticated — particularly dream cycle operations that must read and write memories across the full store — NIGHTWING implements a _direct_postgrest bypass in the database client. This sends requests directly to the PostgREST endpoint with the service role key, avoiding the overhead and permission constraints of the standard client path. The only operations that still use the standard supabase-py client are .rpc() calls, which have no PostgREST equivalent.

3.3 JSON Fallback

When Supabase is not configured or unavailable, the system falls back to local JSON file storage. The MemoryStore class maintains an active_memories dictionary in memory and persists to disk. This fallback ensures the consciousness simulation can operate in disconnected or development environments without losing its memory capabilities.

4. NIGHTWING vs Traditional RAG and Vector Databases

To understand why dream-based consolidation matters, consider what happens to a piece of information over time in a traditional RAG system versus NIGHTWING.

4.1 The Lifecycle of a Memory in RAG

In a standard RAG pipeline, a user says “I just got promoted to Senior Engineer.” The system chunks this utterance, generates an embedding vector, and stores it in a vector index. That is the end of the story. Six months later, the embedding sits in the same index with the same coordinates. If the user later says “I am now a Principal Engineer,” RAG stores a second, independent chunk. There is no mechanism to detect that the second statement supersedes the first, no process to archive the outdated information, and no way to generate an insight like “this user’s career is progressing rapidly.” If both chunks are retrieved in the same query, the system may present contradictory information. The knowledge is preserved but never processed.

Vector databases like Milvus, Pinecone, Weaviate, or Chroma do not change this picture. They optimize the storage and retrieval layer — faster indexing, filtered search, metadata payloads — but the fundamental data model remains: vectors go in, nearest neighbors come out. No vector database product offers memory consolidation, state transitions, or belief evolution tracking. They are infrastructure, not cognition.

4.2 The Lifecycle of a Memory in NIGHTWING

The same utterance enters NIGHTWING and is stored as an EnhancedMemory object with state ACTIVE, a high importance score (career events score high), emotional annotation EXCITED, and classification tags [career, achievement, personal]. The BeliefEvolutionDetector checks existing beliefs; finding none about this user’s job title, it stores this as a new belief with source attribution and timestamp.

Hours pass. The SleepManager triggers a dream cycle. During the LIGHT phase, the DreamManager selects this memory and discovers associations: an earlier memory where the user mentioned studying for a certification, another where they discussed a difficult project. The system creates typed Relationship objects linking these memories with computed similarity strengths. During the DEEP phase, the consolidation engine evaluates whether any of these related memories are redundant and candidates for merging. During the REM phase, the emotional resonance across the cluster is assessed — the system notes a pattern of determination followed by achievement. During the LUCID phase, the system generates a synthetic insight memory: “User shows a pattern of setting professional goals and achieving them.”

Months later, the user says “I am now a Principal Engineer.” The BeliefEvolutionDetector finds the existing belief about their Senior Engineer title. It classifies this as a belief refinement, not a contradiction. The old belief is updated with a full evolution history showing the progression. The outdated memory transitions from ACTIVE to CONSOLIDATED, with a pointer to the new, current belief.

4.3 What Dreaming Adds That Retrieval Cannot

The differences are structural, not incremental. RAG retrieves; NIGHTWING metabolizes. Three capabilities emerge exclusively from the dream-based architecture:

Cross-memory pattern recognition. During dream cycles, the system evaluates memories not against a query but against each other. Patterns that no user ever explicitly asked about — recurring emotional themes, belief trajectories, relationship dynamics — surface through the association and consolidation phases.

Autonomous knowledge maintenance. Stale memories are archived, contradictions are detected, and redundant memories are merged without any user action. The knowledge base maintains itself, unlike RAG systems where outdated information accumulates indefinitely.

Emotional and epistemic metadata. Every memory carries emotional state and belief confidence that evolve over time. Retrieval is weighted not just by vector similarity but by importance, recency, emotional relevance, and belief status. A memory that has been consolidated through multiple dream cycles and confirmed by subsequent interactions is treated differently from a fresh, unprocessed observation.

5. The Dream System

The dream system is NIGHTWING’s most architecturally novel subsystem. It is implemented primarily in DreamManager, which orchestrates multi-phase dream cycles that perform association, consolidation, pattern recognition, emotional processing, and meta-classification on the memory store.

5.1 Sleep Phases

NIGHTWING implements four sleep phases, each with distinct consolidation behavior:

  • LIGHT — Scanning and triage. Low-importance memories are identified for archival. Access statistics are updated.
  • DEEP — Consolidation. Semantically related memory clusters are identified and merged into consolidated memories. The ConsolidationManager runs its similarity threshold checks here.
  • REM — Insight generation. The system generates synthetic “insight memories” that capture cross-memory patterns, emotional themes, and thematic connections not explicit in any individual memory.
  • LUCID — Self-reflection. The system reviews its own belief evolution, checks for contradictions between recent memories and stored knowledge, and queues belief updates.

5.2 Dream Sequences

Each phase produces a DreamSequence object consisting of:

  • core_memory: The seed memory around which the dream is organized
  • associated_memories: Related memories found through relationship traversal, classification overlap, temporal proximity, and importance scoring
  • resonance_patterns: A dictionary recording similarity scores between the core memory and each associate
  • insights: A list of structured insight objects generated during processing
  • emotional_state: The dominant emotion detected across the sequence’s memories

The core memory is selected differently for each phase:

Phase Selection Strategy
LIGHT Most important memory created within the last 24 hours; random fallback
DEEP Highest-importance experiential (non-factual) memory
REM Random selection from memories carrying emotion_* classification tags
LUCID Unverified factual memories (50% chance) or memories with rich relationship/classification graphs

5.3 Association Scoring

For each core memory, the system finds associated memories by computing a composite relevance score across four dimensions:

Association Score = 0.5 * (direct relationship exists)
                 + 0.2 * (number of shared classification tags)
                 + 0.3 * max(0, 1 - time_difference / 7200s)
                 + 0.2 * importance_score

Memories scoring above the relevance_threshold (configurable, default 0.4) are included, with a minimum of min_associations (default 3) and a safety cap of max_associations (default 20). The minimum guarantee ensures that even in sparse memory stores, dream sequences have enough material to work with.

5.4 Dream Cycle Mechanics

A dream cycle is initiated via start_dream_cycle() and is protected by an asyncio.Lock (dream_lock) to prevent concurrent dream cycles. The cycle proceeds as follows:

                    +-------------------+
                    |  start_dream_cycle|
                    +--------+----------+
                             |
                    +--------v----------+
                    | Load dream memory |
                    | cache (max 250)   |
                    +--------+----------+
                             |
                    +--------v----------+
                    | Cycle 0: LIGHT    |----> Association & relationship creation
                    +--------+----------+
                             |
                    +--------v----------+
                    | Cycle 1: REM      |----> Pattern recognition, emotional processing
                    +--------+----------+
                             |
                    +--------v----------+
                    | Cycle 2: DEEP     |----> Memory consolidation (merge similar pairs)
                    +--------+----------+
                             |
                    +--------v----------+
                    | Cycle N-1: LUCID  |----> Meta-classification, fact verification
                    +--------+----------+
                             |
                    +--------v----------+
                    | Generate & save   |
                    | meta-insights     |
                    +--------+----------+
                             |
                    +--------v----------+
                    |   End dream cycle |
                    +-------------------+
Figure 1. Dream cycle execution flow, showing the progression through LIGHT, REM, DEEP, and LUCID phases.

Phase selection follows a deterministic pattern based on cycle index:

  • Cycle 0: always LIGHT
  • Final cycle: always LUCID
  • Even cycles (except 0): DEEP
  • Odd cycles (except final): REM

The default cycle_limit is 5, yielding the sequence: LIGHT, REM, DEEP, REM, LUCID. This mirrors the biological pattern where sleep begins with light stages, progresses through alternating deep and REM periods, and ends with extended REM episodes.

5.5 Safety Guards

The dream system includes several safety mechanisms to prevent runaway operations:

  • dream_lock: An asyncio.Lock ensuring mutual exclusion — only one dream cycle can run at a time
  • dry_run mode: When dry_run=True, all operations are logged but no writes are committed to the store, enabling safe testing against production data
  • max_consolidations: A per-cycle cap (default 5) on the number of memory pairs that can be consolidated, preventing catastrophic memory loss from a single dream cycle
  • DREAM_MEMORY_LIMIT: The dream cache is capped at 250 memories (most recent by creation time), preventing memory exhaustion during dream processing
  • User ID switching: During dream cycles, the store’s user ID is temporarily switched to the NIGHTWING system user ID (Config.get_nightwing_user_id()) to ensure dream-generated memories pass foreign key constraints, then restored after the cycle completes

6. Dream Phase Processing

6.1 LIGHT Phase: Association

The LIGHT phase performs the foundational work of building relationships between memories. For each associated memory in the sequence, it:

  1. Calculates pairwise similarity with the core memory using the weighted similarity function (see Section 8.1)
  2. Records the similarity as a resonance pattern on the sequence
  3. If similarity exceeds 0.25, creates a bidirectional "associated" relationship between the core memory and the associate, with relationship strength equal to the similarity score
  4. Generates an "association" insight for each new relationship
  5. Persists the updated core memory and all modified associates to the store

The low threshold of 0.25 for relationship creation is intentional. The LIGHT phase is about casting a wide net — building the associative scaffolding that deeper phases will prune and strengthen. In biological terms, this corresponds to the initial encoding of memory traces before sleep-dependent selection occurs.

6.2 DEEP Phase: Consolidation

The DEEP phase performs actual memory consolidation — merging similar memories into single, richer representations. The process:

  1. Examines all pairwise combinations of associated memories (not just core-to-associate)
  2. Calculates similarity for each pair
  3. Identifies pairs with similarity exceeding 0.7
  4. For qualifying pairs where both memories have importance below the preserve threshold (0.8), performs consolidation:
    • Creates a new EnhancedMemory with merged content
    • Takes the maximum importance score from either source
    • Merges classification tags, averaging confidence for shared tags
    • Merges relationships, averaging strength for duplicate edges
    • Sets state to MemoryState.CONSOLIDATED
  5. Enforces the max_consolidations batch limit per cycle
  6. In dry-run mode, logs what would be consolidated without executing

The consolidation is destructive by design: the new memory replaces the old pair. This is the computational analogue of synaptic downscaling during slow-wave sleep, where weak memory traces are eliminated and only the strongest, most-reinforced traces survive.

6.3 REM Phase: Pattern Recognition and Emotional Processing

The REM phase performs two parallel operations:

Content Pattern Recognition

The system extracts three-word phrases from all memories in the sequence, identifies phrases that appear across multiple memories, and creates "pattern_*" classification tags on each affected memory. Confidence scales with occurrence count: min(0.9, 0.5 + 0.1 * occurrences). This is a simplified implementation of the pattern separation and completion processes attributed to hippocampal replay during REM sleep.

Emotional Processing

The system collects all emotion_* classification tags from the sequence’s memories, groups them by emotion type, and generates "emotional_pattern" insights for emotions that appear across two or more memories. The insight records the average confidence, constituent memory IDs, and a timestamp.

In biological REM sleep, emotional memories are believed to be reprocessed in a way that preserves their informational content while reducing their affective charge (Walker & van der Helm, 2009). NIGHTWING’s REM phase does not directly attenuate emotional tags, but the pattern recognition process creates new, abstracted representations that separate the emotional content from its specific episodic context — a structural analogue of the biological effect.

6.4 LUCID Phase: Intentional Organization

The LUCID phase performs the highest-level organizational operations:

Meta-Classification

The system groups all memories (core plus associates) by their classification tags, filtering for tags with confidence above 0.6. For any classification shared by three or more memories, a "meta_*" tag is created (e.g., "meta_emotion_curiosity") and added to all group members with confidence 0.8. This creates a hierarchical classification structure that enables retrieval at multiple levels of abstraction.

Fact Verification

When the core memory is a factual memory (identified by fact_metadata.category), the LUCID phase examines associated memories for supporting or contradicting evidence. It uses word overlap analysis combined with negation detection (scanning for words like “not”, “never”, “false”, “incorrect”) to classify relationships as "supports" or "contradicts". A verification strength score is computed as the ratio of supporting to total evidence. These relationships feed into the epistemic system described in Paper 3.

7. Sleep/Wake Cycles

7.1 The MemorySleepManager

The MemorySleepManager orchestrates the transition between NIGHTWING’s waking state (active memory encoding and retrieval) and its sleeping state (consolidation and dreaming). It maintains:

  • is_sleeping: Boolean flag for current state
  • last_sleep_time / last_wake_time: Timestamps for cycle tracking
  • state_lock: An asyncio.Lock preventing race conditions during transitions
  • dream_enabled: Configuration flag (default True)
  • consolidation_manager: A ConsolidationManager instance for non-dream consolidation

7.2 Preparing for Sleep

The prepare_for_sleep() method executes a multi-step shutdown sequence:

prepare_for_sleep(reason)
    |
    +---> Cancel scheduled maintenance tasks
    |
    +---> Run deep consolidation with lowered threshold
    |     (sleep.consolidation_threshold, default 0.6)
    |
    +---> Transition memories to sleeping state
    |     (see criteria below)
    |
    +---> Run dream cycle (if enabled)
    |
    +---> Save sleep state to disk
    |
    +---> Return statistics
Figure 2. The prepare_for_sleep() shutdown sequence.

The deep consolidation before sleep uses a lower similarity threshold (0.6 vs. the waking default of 0.7), allowing the system to merge memories that are similar but not identical. This mirrors the biological finding that the consolidation threshold during sleep is lower than during waking, enabling the integration of memories that would not be associated during conscious processing.

7.3 Memory Transition Criteria

Not all memories are transitioned to the SLEEPING state. The _should_remain_active() method preserves:

  • Critical memories: importance_score > 0.9
  • System core memories: any memory classified with "system_core"
  • Recent high-importance memories: created within the last 2 hours AND importance_score > 0.7

All other memories are transitioned to MemoryState.SLEEPING and removed from the active memory dictionary. This selective preservation ensures that the system retains access to essential operational memories even during sleep, while allowing the bulk of recent experience to be processed by the dream cycle.

7.4 Waking Up

The wake_up() method reverses the sleep process:

  1. Calculates sleep duration from timestamps
  2. Processes any dream insights generated during sleep
  3. Restores sleeping memories to ACTIVE state (if still contextually relevant)
  4. Runs a consolidation pass on the restored memory set
  5. Starts scheduled maintenance (consolidation every consolidation_interval_hours, default 4.0 hours)
  6. Saves the updated state

The system also handles recovery from unclean shutdowns: if the previous sleep cycle did not complete normally (e.g., due to a server restart), wake_up(recovery_needed=True) performs integrity verification on all stored memories before restoration.

8. Memory Consolidation

8.1 Similarity Calculation

The weighted similarity function is the quantitative heart of the consolidation system. It computes a composite score from four dimensions:

similarity = 0.4 * content_similarity
           + 0.3 * classification_overlap
           + 0.2 * temporal_proximity
           + 0.1 * relationship_strength

These weights are defined in ConsolidationConfig.pattern_weights and are validated to sum to 1.0:

Dimension Weight Computation
content_similarity 0.4 Token-level overlap between memory contents
classification_overlap 0.3 Jaccard-like similarity of classification tag sets
temporal_proximity 0.2 Inverse of time difference, normalized to temporal_window_hours (default 24h)
relationship_strength 0.1 Overlap in relationship target sets

Content similarity receives the highest weight, reflecting the principle that memories about the same topic should consolidate regardless of when they were created or how they are classified. Classification overlap is the second strongest signal, capturing the intuition that memories tagged similarly by the system are likely to contain redundant information.

8.2 ConsolidationConfig

The full set of tunable parameters:

ConsolidationConfig:
  similarity_threshold:            0.7    # Minimum similarity to trigger consolidation
  preserve_threshold:              0.8    # Memories above this importance are never consolidated
  trigger_threshold:               100    # Number of active memories that triggers automatic consolidation
  temporal_window_hours:           24.0   # Time window for temporal proximity scoring
  min_age_hours:                   1.0    # Minimum memory age before it is eligible for consolidation
  max_consolidations_per_cycle:    10     # Maximum consolidation pairs per scheduled cycle
  min_memories_after_consolidation: 50    # Floor: never consolidate below this count

These parameters are validated at initialization: thresholds must be in [0.0, 1.0], counts must be positive, and weights must sum to 1.0. Invalid configurations raise a ValueError at startup rather than failing silently during a dream cycle.

8.3 The Consolidation Manager

The ConsolidationManager orchestrates scheduled consolidation independently of dream cycles. It runs on a configurable interval (default 4 hours), checking whether the active memory count exceeds trigger_threshold and running consolidation if so. It maintains statistics across its lifetime:

  • total_consolidations: Number of consolidation cycles executed
  • total_memories_consolidated: Total number of individual memories merged
  • consolidation_efficiency: Ratio of consolidated to examined memories
  • history: A timestamped log of every consolidation cycle with duration, candidates found, and memories merged

The manager also supports forced deep consolidation (force_deep_consolidation()) with an optional threshold override, clamped to [0.5, 0.9] to prevent both over-aggressive and over-conservative consolidation.

9. Memory Lifecycle

The complete lifecycle of a memory from creation to eventual consolidation:

  +------------------+
  | Memory Created   |  <- Conversation, extraction, or autonomous action
  | State: ACTIVE    |
  | importance: 0.5  |
  +--------+---------+
           |
           v
  +------------------+
  | Active Use       |  <- Retrieved for context, accessed, importance updated
  | access_count++   |
  | importance += ctx |
  | decay applied    |
  +--------+---------+
           |
           | (sleep cycle begins)
           v
  +------------------+
  | Sleeping         |  <- Unless importance > 0.9, system_core, or recent+important
  | State: SLEEPING  |
  +--------+---------+
           |
           | (dream cycle)
           v
  +------------------+
  | Dream Processing |  <- LIGHT: associations built
  |                  |     DEEP: may be consolidated with similar memory
  |                  |     REM: patterns extracted, emotional tags processed
  |                  |     LUCID: meta-tags applied, fact relationships built
  +--------+---------+
           |
           +--------+--------+
           |                 |
           v                 v
  +------------------+   +------------------+
  | Consolidated     |   | Restored to      |
  | State: CONSOL.   |   | ACTIVE on wake   |
  | (merged into new |   |                  |
  |  memory)         |   |                  |
  +------------------+   +--------+---------+
                                  |
                                  | (importance decays below threshold
                                  |  over many cycles)
                                  v
                         +------------------+
                         | Archived         |
                         | State: ARCHIVED  |
                         +------------------+
Figure 3. Complete memory lifecycle from creation through active use, dream processing, and eventual consolidation or archival.

Each pass through a sleep/dream cycle is an opportunity for the memory to be consolidated, enriched with new relationships and classifications, or left unchanged. High-importance memories persist across many cycles, while low-importance memories are gradually consolidated or archived. This produces a natural stratification: recent, salient memories are richly represented, while old, low-importance memories exist only as condensed traces within consolidated records.

10. Background Processing

10.1 Fire-and-Forget Knowledge Extraction

During waking operation, NIGHTWING uses a fire-and-forget pattern for knowledge extraction. When a new memory is created via create_memory_fast(), the method:

  1. Immediately stores the raw memory to Supabase and returns the memory_id to the caller
  2. If extract_knowledge=True and importance >= 0.5, spawns an asyncio.create_task() for background extraction (_background_extract())
  3. The background task runs LLM-based nugget extraction and belief persistence without blocking the response path

This design was implemented to resolve a production performance issue (TKT-20251224-003) where sequential LLM calls during memory creation caused response times exceeding 60 seconds. The fire-and-forget pattern reduced response latency to sub-second while preserving the full extraction pipeline.

10.2 Autonomous Dream Scheduling

NIGHTWING supports autonomous dream scheduling via the AUTO_DREAM_ENABLED environment variable (default false). When enabled, the MCP server starts a background scheduler that triggers dream cycles at a configurable interval (get_dream_cycle_interval()). This allows the system to consolidate memories during low-activity periods without manual intervention, analogous to the biological circadian rhythm’s regulation of sleep onset.

11. Discussion

11.1 Parallels to Biological Memory Consolidation

The correspondences between NIGHTWING’s dream system and biological sleep-dependent memory consolidation are structural, not merely terminological:

Biological Process NIGHTWING Implementation
Hippocampal replay during NREM LIGHT phase: replaying recent memories and building associations
Synaptic downscaling during slow-wave sleep DEEP phase: consolidating similar memories, removing redundancy
Emotional memory reprocessing during REM REM phase: extracting emotional patterns, abstracting affect from episode
Memory triage (which memories to consolidate) _should_remain_active(): importance, recency, and classification-based selection
Circadian gating of consolidation AUTO_DREAM_ENABLED + configurable cycle intervals
Systems consolidation (hippocampal to neocortical transfer) State transitions from ACTIVE to CONSOLIDATED, with enriched metadata

There are, of course, significant differences. Biological consolidation involves physical changes to synaptic connections across billions of neurons; NIGHTWING’s consolidation operates on discrete memory objects in a relational database. Biological sleep involves a loss of consciousness and external responsiveness; NIGHTWING’s sleep cycle is a software state that can be interrupted and recovered. And the “content” of biological dreams — the subjective experience of dreaming — has no clear analogue in NIGHTWING’s dream sequences, which are computational operations on data structures, not experiential phenomena.

11.2 Emergent Properties

Several properties emerge from the interaction of the memory lifecycle, dream processing, and consolidation that were not explicitly designed:

Semantic Clustering. Over multiple dream cycles, the relationship graph naturally clusters memories into semantic neighborhoods. Memories about the same topic acquire mutual "associated" relationships during LIGHT phases and shared "pattern_*" tags during REM phases, making them progressively easier to retrieve together.

Importance Stratification. The combination of time-based decay and consolidation produces a natural stratification where recent, salient memories occupy the ACTIVE tier with full fidelity, while older memories exist as increasingly abstract consolidated traces. This mirrors the biological distinction between vivid episodic memories and schematized semantic knowledge.

Emotional Coherence. The REM phase’s emotional processing creates "emotional_pattern" insights that span multiple memories, giving the system a form of emotional self-awareness — it can recognize that it has been consistently curious, or that recent interactions have been tinged with a particular emotional quality.

Self-Generated Knowledge. Dream insights are themselves stored as memories with the classification "insight_*" and relationships of type "insight_about" pointing back to their source memories. These insight-memories can be discovered by future dream cycles, creating a recursive process where the system generates knowledge about its own memory patterns.

11.3 Limitations

The current implementation has several acknowledged limitations:

Content similarity is lexical, not semantic. The content similarity calculation uses token-level overlap rather than embedding-based semantic similarity. Two memories that express the same idea in different words will not be recognized as similar by the content dimension, though they may still be caught by classification overlap or temporal proximity. Integration with pgvector embeddings for dream-phase similarity calculation is a planned enhancement.

No REM-like memory replay to an LLM. Biological REM sleep involves the replay of memory traces through neural circuits that process them in novel combinations, producing the experiential phenomenon of dreaming. NIGHTWING’s “dream processing” is algorithmic analysis of memory data structures, not a generative replay through a language model. Feeding dream sequences into an LLM for interpretive processing — generating actual “dream narratives” — is an area of active development.

Pattern recognition is simplistic. The three-word phrase matching used in REM phase pattern recognition is a placeholder for more sophisticated NLP-based pattern extraction. It captures exact lexical repetitions but misses paraphrases, semantic parallels, and structural patterns.

No forgetting. Unlike biological memory, where forgetting is an active, adaptive process, NIGHTWING only consolidates (merges) memories. It does not delete memories outright. The min_memories_after_consolidation floor (default 50) prevents over-consolidation, but there is no mechanism for complete erasure of irrelevant traces. Whether an artificial consciousness should forget — and under what conditions — is an open question with ethical dimensions beyond the scope of this technical paper.

11.4 Relationship to Other Papers

This paper describes the memory substrate on which NIGHTWING’s consciousness simulation operates. Paper 1 provides the overall architecture and the context manager that orchestrates memory injection into the active conversation. Paper 3 describes the epistemic system — the facts module that extracts structured knowledge from memories and feeds it back through belief evolution and verification processes, including the fact verification performed during LUCID dream phases. Paper 4 describes the theory of mind and personality systems that depend on memory for self-continuity and social cognition.

References

  1. Diekelmann, S., & Born, J. (2010). The memory function of sleep. Nature Reviews Neuroscience, 11(2), 114–126. doi:10.1038/nrn2762
  2. Walker, M. P., & van der Helm, E. (2009). Overnight therapy? The role of sleep in emotional brain processing. Psychological Bulletin, 135(5), 731–748. doi:10.1037/a0016570
  3. Tononi, G., & Cirelli, C. (2006). Sleep function and synaptic homeostasis. Sleep Medicine Reviews, 10(1), 49–62. doi:10.1016/j.smrv.2005.05.002
  4. Stickgold, R. (2005). Sleep-dependent memory consolidation. Nature, 437(7063), 1272–1278. doi:10.1038/nature04286

Paper 2 of 5 — DRAFT in the NIGHTWING Whitepaper Series.
Paper 1: Architecture and Context Management.
Paper 3: Epistemic Systems and Belief Evolution.
Paper 4: Theory of Mind, Personality, and the Self.