File size: 28,408 Bytes
dbb04e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
"""

MnemoCore Subconscious Daemon

=========================

Continuous background processing using Gemma 1B via Ollama.

Performs: concept extraction, parallel drawing, memory valuation, thought sorting.

Integrates with Redis Subconscious Bus to publish insights.

"""

import asyncio
import aiohttp
import json
import random
import time
from datetime import datetime, timezone
from typing import List, Dict, Any, Optional
import sys
import os

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from mnemocore.core.engine import HAIMEngine
from mnemocore.core.async_storage import AsyncRedisStorage
from mnemocore.core.config import get_config
from mnemocore.meta.learning_journal import LearningJournal
from mnemocore.core.node import MemoryNode
from mnemocore.core.metrics import (
    DREAM_LOOP_TOTAL,
    DREAM_LOOP_ITERATION_SECONDS,
    DREAM_LOOP_INSIGHTS_GENERATED,
    DREAM_LOOP_ACTIVE
)

# Default Config (overridden by config.yaml)
DEFAULT_OLLAMA_URL = "http://localhost:11434/api/generate"
DEFAULT_MODEL = "gemma3:1b"
HAIM_DATA_PATH = "./data/memory.jsonl"
DEFAULT_CYCLE_INTERVAL = 60  # seconds between thought cycles
LOG_PATH = "/tmp/subconscious.log"
EVOLUTION_STATE_PATH = "./data/subconscious_evolution.json"


def _write_state_to_disk(state: Dict[str, Any], filepath: str):
    """Write state to disk synchronously (to be used in executor)."""
    os.makedirs(os.path.dirname(filepath), exist_ok=True)
    with open(filepath, "w") as f:
        json.dump(state, f, indent=2)


class SubconsciousDaemon:
    """The always-running background mind."""

    def __init__(self, storage: Optional[AsyncRedisStorage] = None, config: Optional[Any] = None):
        """

        Initialize SubconsciousDaemon with optional dependency injection.



        Args:

            storage: AsyncRedisStorage instance. If None, creates one in run().

            config: Configuration object. If None, loads from get_config().

        """
        # Load configuration
        self._config = config or get_config()

        # Dream loop configuration from config.yaml
        dream_loop_config = getattr(self._config, 'dream_loop', None)
        if dream_loop_config:
            self.ollama_url = getattr(dream_loop_config, 'ollama_url', DEFAULT_OLLAMA_URL)
            self.model = getattr(dream_loop_config, 'model', DEFAULT_MODEL)
            self.frequency_seconds = getattr(dream_loop_config, 'frequency_seconds', DEFAULT_CYCLE_INTERVAL)
            self.batch_size = getattr(dream_loop_config, 'batch_size', 10)
            self.max_iterations = getattr(dream_loop_config, 'max_iterations', 0)
            self.dream_loop_enabled = getattr(dream_loop_config, 'enabled', True)
        else:
            self.ollama_url = DEFAULT_OLLAMA_URL
            self.model = DEFAULT_MODEL
            self.frequency_seconds = DEFAULT_CYCLE_INTERVAL
            self.batch_size = 10
            self.max_iterations = 0
            self.dream_loop_enabled = True

        self.engine = HAIMEngine(persist_path=HAIM_DATA_PATH)
        self.journal = LearningJournal()

        # Graceful shutdown support using asyncio.Event
        self._stop_event = asyncio.Event()
        self.running = False

        self.cycle_count = 0
        self.insights_generated = 0
        self.current_cycle_interval = self.frequency_seconds
        self.schedule = {
            "concept_every": 5,
            "parallel_every": 3,
            "value_every": 10,
            "meta_every": 7,
            "cleanup_every": 20
        }
        self.activity_window: List[int] = []
        self.low_activity_streak = 0
        self.last_cycle_metrics: Dict[str, Any] = {}
        self._load_evolution_state()

        # Async Redis Storage (injected or initialized in run)
        self.storage: Optional[AsyncRedisStorage] = storage

    def _should_stop(self) -> bool:
        """Check if the daemon should stop (non-blocking check)."""
        return self._stop_event.is_set()

    async def request_stop(self):
        """Request graceful stop of the daemon (async-safe)."""
        self._stop_event.set()
        self.running = False
        
    def log(self, msg: str):
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        line = f"[{timestamp}] {msg}"
        print(line)
        with open(LOG_PATH, "a") as f:
            f.write(line + "\n")

    def _load_evolution_state(self):
        """Load persistent evolution state from disk."""
        if not os.path.exists(EVOLUTION_STATE_PATH):
            return
        try:
            with open(EVOLUTION_STATE_PATH, "r") as f:
                state = json.load(f)
            self.cycle_count = int(state.get("cycle_count", self.cycle_count))
            self.insights_generated = int(state.get("insights_generated", self.insights_generated))
            self.current_cycle_interval = int(state.get("current_cycle_interval", self.current_cycle_interval))
            saved_schedule = state.get("schedule", {})
            if isinstance(saved_schedule, dict):
                for k in self.schedule:
                    if k in saved_schedule:
                        self.schedule[k] = max(2, int(saved_schedule[k]))
            self.activity_window = list(state.get("activity_window", []))[-12:]
            self.low_activity_streak = int(state.get("low_activity_streak", 0))
        except Exception as e:
            self.log(f"Failed to load evolution state: {e}")

    async def _save_evolution_state(self):
        """Persist state so evolution continues across restarts."""
        state = {
            "updated_at": datetime.now(timezone.utc).isoformat(),
            "cycle_count": self.cycle_count,
            "insights_generated": self.insights_generated,
            "current_cycle_interval": self.current_cycle_interval,
            "schedule": self.schedule,
            "activity_window": self.activity_window[-12:],
            "low_activity_streak": self.low_activity_streak,
            "last_cycle_metrics": self.last_cycle_metrics,
        }
        try:
            loop = asyncio.get_running_loop()
            await loop.run_in_executor(None, _write_state_to_disk, state, EVOLUTION_STATE_PATH)
        except Exception as e:
            self.log(f"Failed to save evolution state: {e}")

    def _compute_surprise(self, metrics: Dict[str, Any]) -> float:
        """Estimate surprise from novelty/output dynamics."""
        score = 0.0
        score += 0.12 * metrics.get("concepts", 0)
        score += 0.20 * metrics.get("parallels", 0)
        score += 0.30 * metrics.get("meta_insights", 0)
        if metrics.get("adaptation") and metrics.get("adaptation") != "none":
            score += 0.25
        return min(1.0, score)

    def _adapt_evolution_policy(self, metrics: Dict[str, Any]):
        """

        Adapt cadence and schedule so the subconscious keeps evolving.

        Low activity -> stimulate exploration.

        High sustained activity -> stabilize to preserve quality.

        """
        activity_score = (
            metrics.get("concepts", 0)
            + metrics.get("parallels", 0)
            + metrics.get("meta_insights", 0)
        )
        self.activity_window.append(activity_score)
        self.activity_window = self.activity_window[-12:]

        if activity_score == 0:
            self.low_activity_streak += 1
        else:
            self.low_activity_streak = 0

        adaptation = "none"
        avg_activity = sum(self.activity_window) / max(1, len(self.activity_window))

        if self.low_activity_streak >= 4:
            self.schedule["concept_every"] = max(2, self.schedule["concept_every"] - 1)
            self.schedule["parallel_every"] = max(2, self.schedule["parallel_every"] - 1)
            self.schedule["meta_every"] = max(3, self.schedule["meta_every"] - 1)
            self.current_cycle_interval = max(35, self.current_cycle_interval - 5)
            self.low_activity_streak = 0
            adaptation = "stimulate"
        elif avg_activity >= 2.0:
            self.current_cycle_interval = min(90, self.current_cycle_interval + 5)
            self.schedule["value_every"] = min(15, self.schedule["value_every"] + 1)
            adaptation = "stabilize"

        metrics["activity_score"] = activity_score
        metrics["avg_activity"] = round(avg_activity, 3)
        metrics["adaptation"] = adaptation

    def _record_cycle_learning(self, metrics: Dict[str, Any]):
        """Write periodic learning traces so evolution is continuous and explicit."""
        should_record = (
            self.cycle_count % 5 == 0
            or metrics.get("meta_insights", 0) > 0
            or metrics.get("adaptation", "none") != "none"
        )
        if not should_record:
            return

        surprise = self._compute_surprise(metrics)
        lesson = (
            f"Cycle {self.cycle_count}: concepts={metrics.get('concepts', 0)}, "
            f"parallels={metrics.get('parallels', 0)}, meta={metrics.get('meta_insights', 0)}, "
            f"adaptation={metrics.get('adaptation', 'none')}, interval={self.current_cycle_interval}s."
        )
        context = (
            f"memories={metrics.get('memories', 0)}, synapses={metrics.get('synapses', 0)}, "
            f"schedule={self.schedule}"
        )
        self.journal.record(
            lesson=lesson,
            context=context,
            outcome="success",
            confidence=0.7,
            tags=["subconscious", "continuous-evolution"],
            surprise=surprise,
        )
    
    async def query_ollama(self, prompt: str, max_tokens: int = 200) -> str:
        """Query local Gemma model."""
        payload = {
            "model": self.model,
            "prompt": prompt,
            "stream": False,
            "options": {
                "num_predict": max_tokens,
                "temperature": 0.7
            }
        }

        try:
            async with aiohttp.ClientSession() as session:
                async with session.post(self.ollama_url, json=payload, timeout=30) as resp:
                    if resp.status == 200:
                        data = await resp.json()
                        return data.get("response", "").strip()
                    else:
                        self.log(f"Ollama error: {resp.status}")
                        return ""
        except Exception as e:
            self.log(f"Ollama connection error: {e}")
            return ""
    
    async def extract_concepts(self, memories: List[MemoryNode]) -> List[Dict]:
        """Extract concepts from recent memories."""
        if not memories:
            return []
            
        # Sample up to 5 memories
        sample = random.sample(memories, min(5, len(memories)))
        contents = [m.content[:200] for m in sample]
        
        prompt = f"""Analyze these memory fragments and extract key concepts.

Output JSON array of concepts with attributes.



Memories:

{chr(10).join(f'- {c}' for c in contents)}



Output format: [{{"name": "concept", "category": "type", "connections": ["related1", "related2"]}}]

Only output valid JSON array, nothing else."""

        response = await self.query_ollama(prompt, max_tokens=300)
        
        try:
            # Try to parse JSON
            if "[" in response:
                start = response.index("[")
                end = response.rindex("]") + 1
                concepts = json.loads(response[start:end])
                return concepts
        except:
            pass
        return []
    
    async def draw_parallels(self, memories: List[MemoryNode]) -> List[str]:
        """Find unexpected connections between memories."""
        if len(memories) < 2:
            return []
            
        # Pick 2 random memories
        sample = random.sample(memories, 2)
        
        prompt = f"""Find a non-obvious parallel or connection between these two ideas:



1: {sample[0].content[:200]}



2: {sample[1].content[:200]}



Output ONE insight about how these connect. Be creative but logical. Max 50 words."""

        response = await self.query_ollama(prompt, max_tokens=100)
        
        if response and len(response) > 20:
            return [response]
        return []
    
    async def value_memories(self, memories: List[MemoryNode]) -> Dict[str, float]:
        """Re-evaluate memory importance based on patterns."""
        if not memories:
            return {}
            
        # Sample memories for valuation
        sample = random.sample(memories, min(10, len(memories)))
        
        prompt = f"""Rate each memory's strategic value (0.0-1.0) for a tech entrepreneur focused on expansion.



Memories:

{chr(10).join(f'{i+1}. {m.content[:100]}' for i, m in enumerate(sample))}



Output format: {{"1": 0.8, "2": 0.3, ...}}

Only output valid JSON object."""

        response = await self.query_ollama(prompt, max_tokens=200)
        
        try:
            if "{" in response:
                start = response.index("{")
                end = response.rindex("}") + 1
                values = json.loads(response[start:end])
                # Map back to memory IDs
                result = {}
                for i, m in enumerate(sample):
                    key = str(i + 1)
                    if key in values:
                        result[m.id] = float(values[key])
                return result
        except:
            pass
        return {}
    
    async def generate_insight(self, memories: List[MemoryNode]) -> Optional[str]:
        """Generate a meta-insight from memory patterns."""
        if len(memories) < 3:
            return None
            
        sample = random.sample(memories, min(8, len(memories)))
        contents = [m.content[:150] for m in sample]
        
        prompt = f"""You are analyzing patterns in an entrepreneur's memory system.

        

Recent memories:

{chr(10).join(f'- {c}' for c in contents)}



Generate ONE actionable insight or pattern you notice. Focus on:

- Recurring themes

- Opportunities being missed

- Contradictions to resolve

- Strategic blind spots



Output just the insight, max 60 words."""

        response = await self.query_ollama(prompt, max_tokens=120)
        
        if response and len(response) > 30:
            return response
        return None
    
    async def store_insight(self, content: str, meta: Dict[str, Any]):
        """Helper to store insight and publish event."""
        # Store in Engine (Sync)
        # Offload sync I/O to thread to avoid blocking loop
        mem_id = await asyncio.to_thread(self.engine.store, content, metadata=meta)
        
        # Publish Event (Async)
        if self.storage:
            try:
                await self.storage.publish_event(
                    "insight.generated", 
                    {"id": mem_id, "type": meta.get("type", "insight"), "content": content[:50]}
                )
            except Exception as e:
                self.log(f"Failed to publish event: {e}")
        return mem_id

    async def run_cycle(self):
        """Execute one thought cycle."""
        iteration_start_time = time.time()
        self.cycle_count += 1
        self.log(f"=== Cycle {self.cycle_count} ===")
        metrics: Dict[str, Any] = {
            "concepts": 0,
            "parallels": 0,
            "meta_insights": 0,
            "valuations": 0,
            "memories": len(self.engine.tier_manager.hot),
            "synapses": len(self.engine.synapses),
        }


        # Get all hot memories as list (references only, no copy)
        memories = list(self.engine.tier_manager.hot.values())

        if not memories:
            self.log("No memories to process")
            metrics["adaptation"] = "none"
            self.last_cycle_metrics = metrics
            await self._save_evolution_state()
            # Record metrics
            DREAM_LOOP_TOTAL.labels(status="success").inc()
            return

        self.log(f"Processing {len(memories)} memories")

        # 1. Extract concepts (every 5 cycles)
        if self.cycle_count % self.schedule["concept_every"] == 0:
            concepts = await self.extract_concepts(memories)
            for concept in concepts:
                if "name" in concept:
                    attrs = {k: str(v) for k, v in concept.items() if k != "name"}
                    self.engine.define_concept(concept["name"], attrs)
                    metrics["concepts"] += 1
                    self.log(f"Concept extracted: {concept['name']}")
                    # Record insight metric
                    DREAM_LOOP_INSIGHTS_GENERATED.labels(type="concept").inc()
                    # Publish concept event?
                    if self.storage:
                        await self.storage.publish_event("concept.extracted", {"name": concept["name"]})

        # 2. Draw parallels (every 3 cycles)
        if self.cycle_count % self.schedule["parallel_every"] == 0:
            parallels = await self.draw_parallels(memories)
            for p in parallels:
                # Store parallel as new memory
                await self.store_insight(
                    f"[PARALLEL] {p}",
                    meta={"type": "insight", "source": "subconscious", "cycle": self.cycle_count}
                )
                self.insights_generated += 1
                metrics["parallels"] += 1
                self.log(f"Parallel found: {p[:80]}...")
                # Record insight metric
                DREAM_LOOP_INSIGHTS_GENERATED.labels(type="parallel").inc()

        # 3. Value memories (every 10 cycles)
        if self.cycle_count % self.schedule["value_every"] == 0:
            values = await self.value_memories(memories)
            for mem_id, value in values.items():
                if mem_id in self.engine.tier_manager.hot:
                    self.engine.tier_manager.hot[mem_id].pragmatic_value = value
                    metrics["valuations"] += 1
            self.log(f"Valued {len(values)} memories")

        # 4. Generate meta-insight (every 7 cycles)
        if self.cycle_count % self.schedule["meta_every"] == 0:
            insight = await self.generate_insight(memories)
            if insight:
                await self.store_insight(
                    f"[META-INSIGHT] {insight}",
                    meta={"type": "meta", "source": "subconscious", "cycle": self.cycle_count}
                )
                self.insights_generated += 1
                metrics["meta_insights"] += 1
                self.log(f"Meta-insight: {insight[:80]}...")
                # Record insight metric
                DREAM_LOOP_INSIGHTS_GENERATED.labels(type="meta").inc()

        # 5. Cleanup decayed synapses (every 20 cycles)
        if self.cycle_count % self.schedule["cleanup_every"] == 0:
            before = len(self.engine.synapses)
            self.engine.cleanup_decay(threshold=0.1)
            removed = max(0, before - len(self.engine.synapses))
            self.log(f"Synapse cleanup complete (removed {removed})")

        metrics["memories"] = len(self.engine.tier_manager.hot)
        metrics["synapses"] = len(self.engine.synapses)
        self._adapt_evolution_policy(metrics)
        self._record_cycle_learning(metrics)
        self.last_cycle_metrics = metrics
        await self._save_evolution_state()

        # Record iteration duration metric
        iteration_duration = time.time() - iteration_start_time
        DREAM_LOOP_ITERATION_SECONDS.observe(iteration_duration)
        DREAM_LOOP_TOTAL.labels(status="success").inc()

        self.log(
            "Cycle complete. "
            f"Insights={self.insights_generated} "
            f"(concepts={metrics['concepts']}, parallels={metrics['parallels']}, meta={metrics['meta_insights']}) "
            f"adaptation={metrics.get('adaptation', 'none')} interval={self.current_cycle_interval}s "
            f"duration={iteration_duration:.2f}s"
        )
    
    async def _consume_events(self):
        """Consume events from the Subconscious Bus (Redis Stream)."""
        if not self.storage: return

        last_id = "$" # New events only
        config = get_config()
        stream_key = config.redis.stream_key

        self.log(f"Starting event consumer on {stream_key}")

        while self.running:
            try:
                # XREAD is blocking
                streams = await self.storage.redis_client.xread(
                    {stream_key: last_id}, count=1, block=1000
                )

                if not streams:
                    await asyncio.sleep(0.1)
                    continue

                for _, events in streams:
                    for event_id, event_data in events:
                        last_id = event_id
                        await self._process_event(event_data)

            except Exception as e:
                self.log(f"Event consumer error: {e}")
                await asyncio.sleep(1)

    async def _process_event(self, event_data: Dict[str, Any]):
        """Handle incoming events."""
        event_type = event_data.get("type")
        
        if event_type == "memory.created":
            mem_id = event_data.get("id")
            if not mem_id: return
            
            # Check if we already have it (created by us?)
            if mem_id in self.engine.tier_manager.hot:
                return
                
            self.log(f"Received sync event: memory.created ({mem_id})")
            
            # Fetch full memory from Redis
            data = await self.storage.retrieve_memory(mem_id)
            if not data:
                self.log(f"Could not retrieve memory {mem_id} from storage")
                return
                
            # Reconstruct and add to Engine
            try:
                # Need to handle HDV reconstruction. 
                # For now, we might need to load it via Engine's logic or construct manually.
                # Engine's logic is best to ensure consistency.
                # But Engine doesn't have a "load_from_redis" method readily available on single node.
                # TierManager has _load_from_warm, but that's for Qdrant/File.
                # We can manually reconstruct ephemeral node for HOT tier.
                
                # Check if it has HDV vector in Redis? 
                # AsyncRedisStorage store_memory stores metadata + content.
                # It does NOT store the vector currently in the metadata payload in `store_memory` in `api/main.py`.
                # API calls engine.store -> which creates node -> then API calls storage.store_memory.
                # The node in engine has the vector.
                # But Daemon is a separate process. It needs the vector.
                
                # Critical Gap: Redis payload doesn't have the vector.
                # We need to fetch it from Qdrant/Warm if it was persisted there?
                # Engine.store puts it in HOT (RAM) and Appends to `memory.jsonl` (Legacy).
                # It does NOT immediately put it in Qdrant (Warm).
                
                # So Daemon cannot load it from Qdrant yet.
                # It can load it from `memory.jsonl` if it reads the file?
                # Or we must include the vector in the Redis payload or `memory.created` event?
                # Including vector in Redis event is heavy.
                
                # Option A: Read from `memory.jsonl` tail?
                # Option B: Pass vector in Redis (might be large).
                # Option C: API should also save to Qdrant immediately if we want shared state?
                # But TierManager logic says "Starts in HOT".
                
                # Workaround for Phase 3.5:
                # Since Engine appends to `memory.jsonl`, we can try to re-load from there.
                # Or, we update API to include the vector/seed in Redis?
                # Re-encoding in Daemon is an option if we have the content.
                # HAIM is distinct: Same content = Same Vector (if deterministic).
                
                # Let's use re-encoding for now.
                content = data.get("content", "")
                if content:
                    # Encode
                    hdv = self.engine.encode_content(content)
                    
                    # Create Node
                    node = MemoryNode(
                        id=data["id"],
                        hdv=hdv,
                        content=content,
                        metadata=data.get("metadata", {})
                    )
                    node.ltp_strength = float(data.get("ltp_strength", 0.5))
                    node.created_at = datetime.fromisoformat(data["created_at"])
                    
                    # Add to Daemon's Engine
                    self.engine.tier_manager.add_memory(node)
                    self.log(f"Synced memory {mem_id} to HOT tier")

            except Exception as e:
                self.log(f"Failed to process sync for {mem_id}: {e}")

    async def run(self):
        """Main daemon loop."""
        if not self.dream_loop_enabled:
            self.log("Dream loop is disabled in configuration. Exiting.")
            return

        # Clear stop event for restart support
        self._stop_event.clear()
        self.running = True
        DREAM_LOOP_ACTIVE.set(1)

        if not self.storage:
            # Create storage from config if not injected
            config = get_config()
            self.storage = AsyncRedisStorage(
                url=config.redis.url,
                stream_key=config.redis.stream_key,
                max_connections=config.redis.max_connections,
                socket_timeout=config.redis.socket_timeout,
                password=config.redis.password,
            )
        self.log("Subconscious daemon starting...")
        self.log(f"Model: {self.model} | Cycle interval: {self.frequency_seconds}s | Max iterations: {self.max_iterations or 'unlimited'}")

        # Start event consumer task
        asyncio.create_task(self._consume_events())

        iterations = 0
        while self.running and not self._should_stop():
            # Check max_iterations limit (0 = unlimited)
            if self.max_iterations > 0 and iterations >= self.max_iterations:
                self.log(f"Reached max iterations ({self.max_iterations}). Stopping.")
                break

            try:
                await self.run_cycle()
                iterations += 1
            except Exception as e:
                self.log(f"Cycle error: {e}")
                DREAM_LOOP_TOTAL.labels(status="error").inc()

            # Non-blocking sleep with periodic stop check
            sleep_interval = self.current_cycle_interval
            sleep_remaining = sleep_interval
            check_interval = 0.5  # Check for stop every 0.5 seconds

            while sleep_remaining > 0 and not self._should_stop():
                sleep_time = min(check_interval, sleep_remaining)
                await asyncio.sleep(sleep_time)
                sleep_remaining -= sleep_time

        self.running = False
        DREAM_LOOP_ACTIVE.set(0)
        self.log("Daemon stopped.")

    def stop(self):
        """Request daemon stop (can be called from signal handler)."""
        self._stop_event.set()
        self.running = False
        self.log("Daemon stop requested...")


async def main():
    daemon = SubconsciousDaemon()

    # Handle graceful shutdown
    import signal
    def shutdown(sig, frame):
        daemon.stop()

    signal.signal(signal.SIGINT, shutdown)
    signal.signal(signal.SIGTERM, shutdown)

    await daemon.run()


if __name__ == "__main__":
    asyncio.run(main())