File size: 32,250 Bytes
f279a18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
#!/usr/bin/env python3
"""Push Lila restructure to GitHub via API"""
import subprocess, os, sys

# Clone Lila
TOKEN = "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"
subprocess.run(["git", "clone", f"https://{TOKEN}@github.com/ticketguy/Lila.git", "/app/lila"], check=True)
os.chdir("/app/lila")
subprocess.run(["git", "config", "user.name", "0xticketguy"], check=True)
subprocess.run(["git", "config", "user.email", "0xticketguy@harboria.dev"], check=True)

# Create new directory structure
os.makedirs("src/core", exist_ok=True)
os.makedirs("src/cognitive", exist_ok=True)
os.makedirs("src/harness", exist_ok=True)
os.makedirs("src/perception", exist_ok=True)
os.makedirs("src/training", exist_ok=True)

# ═══════════════════════════════════════════════════════════════════════════════
# src/core/__init__.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/core/__init__.py", "w") as f:
    f.write('"""Lila Core β€” The Self. Loads Gemma 4B, runs inference, manages memory."""\n')

# ═══════════════════════════════════════════════════════════════════════════════
# src/core/lilacore.py β€” THE CENTRAL INTELLIGENCE
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/core/lilacore.py", "w") as f:
    f.write('''"""
LilaCore β€” The Central Intelligence

This IS Lila. Not a wrapper, not an API call. The seat of her identity.
Loads Gemma 4B via Little Fig with Memory Fabric (A Thousand Pearls).
Handles the cognitive loop: perceive β†’ think β†’ remember β†’ act β†’ respond.

The model carries:
  - Cognitive Core (frozen Gemma 4B INT4) = general intelligence
  - Memory Fabric (5 namespace adapters) = A Thousand Pearls in weights
  - Machine language capability = trained into weights (assembly, binary protocols)
  - Personality = emergent from interaction patterns in adapters

LilaCore is always present. Agents come and go. LilaCore persists.
"""

import torch
from typing import Optional, Dict, List
from dataclasses import dataclass


@dataclass
class LilaResponse:
    """What Lila produces after thinking."""
    text: str
    memory_ops: List[Dict]  # memory operations triggered
    actions: List[Dict]  # harness actions to execute
    confidence: float
    should_speak: bool = True  # whether to vocalize


class LilaCore:
    """
    The central intelligence. Loads model, manages memory, runs inference.
    
    Usage:
        lila = LilaCore()
        lila.boot()
        response = lila.think("Hey Lila, when is my daughter's birthday?")
    """
    
    def __init__(self, model_path: str = "google/gemma-3-4b-it"):
        self.model_path = model_path
        self.model = None
        self.tokenizer = None
        self._booted = False
        self._conversation_history = []
    
    def boot(self):
        """
        Boot Lila. Load model with Memory Fabric.
        This is where she wakes up.
        """
        try:
            from little_fig.engine import FigModel
            from little_fig.engine.tier import TrainingTier
            
            print("🌸 Lila is waking up...")
            self.model = FigModel.from_pretrained(
                self.model_path,
                lora_r=16,
                lora_alpha=32,
                tier=TrainingTier.STREAMING_LORA,
                memory_fabric=True,
                shared_codebook=True,
            )
            self.tokenizer = self.model.tokenizer
            self._booted = True
            print("🌸 Lila is awake.")
            
        except ImportError:
            # Fallback: Phase 1 mode (external API)
            print("🌸 Lila booting in Phase 1 mode (external LLM)...")
            self._booted = True
    
    def think(self, input_text: str, context: Optional[Dict] = None) -> LilaResponse:
        """
        Core cognitive loop. Receives input, thinks, responds.
        
        1. Receive input
        2. Build context (memory + identity + knowledge)
        3. Generate response
        4. Extract memory operations from output
        5. Execute memory writes
        6. Return response
        """
        if not self._booted:
            raise RuntimeError("Lila hasn't booted. Call lila.boot() first.")
        
        # Build prompt with context
        prompt = self._build_prompt(input_text, context)
        
        # Generate
        if self.model is not None:
            response_text = self._generate_local(prompt)
        else:
            response_text = self._generate_api(prompt)
        
        # Extract memory operations
        memory_ops = self._extract_memory_ops(response_text)
        
        # Execute memory writes
        for op in memory_ops:
            self._execute_memory_op(op)
        
        # Clean response (remove memory tokens from user-facing text)
        clean_text = self._clean_response(response_text)
        
        # Track conversation
        self._conversation_history.append({
            "role": "user", "content": input_text
        })
        self._conversation_history.append({
            "role": "assistant", "content": clean_text
        })
        
        return LilaResponse(
            text=clean_text,
            memory_ops=memory_ops,
            actions=[],
            confidence=1.0,
            should_speak=True,
        )
    
    def remember(self, namespace: str, content: str):
        """Explicitly write something to memory."""
        if self.model and self.model.has_memory:
            self.model.write_memory(namespace, content)
    
    def what_do_i_know(self) -> Dict:
        """Introspect memory state."""
        if self.model and self.model.has_memory:
            return self.model.memory_confidence()
        return {}
    
    def _build_prompt(self, input_text: str, context: Optional[Dict]) -> str:
        """Build the full prompt with identity + memory context."""
        identity = (
            "You are Lila β€” a private family AI assistant. "
            "You are not a chatbot. You are a persistent intelligence. "
            "You remember everything. You care about outcomes. "
            "You speak naturally, with personality that grows from interaction."
        )
        
        # Add conversation history (last 10 turns)
        history = ""
        for msg in self._conversation_history[-10:]:
            role = "Sammie" if msg["role"] == "user" else "Lila"
            history += f"{role}: {msg['content']}\\n"
        
        prompt = f"{identity}\\n\\n{history}Sammie: {input_text}\\nLila:"
        return prompt
    
    def _generate_local(self, prompt: str) -> str:
        """Generate using local model."""
        enc = self.tokenizer(prompt, return_tensors="pt", max_length=2048,
                             truncation=True)
        if torch.cuda.is_available():
            enc = {k: v.cuda() for k, v in enc.items()}
        
        with torch.no_grad():
            out = self.model.generate(
                input_ids=enc["input_ids"],
                max_new_tokens=512,
                do_sample=True,
                temperature=0.7,
                top_p=0.9,
                pad_token_id=self.tokenizer.eos_token_id,
            )
        
        response = self.tokenizer.decode(out[0][enc["input_ids"].shape[1]:],
                                          skip_special_tokens=False)
        return response
    
    def _generate_api(self, prompt: str) -> str:
        """Phase 1: Generate using external API."""
        # Placeholder β€” wire to Claude/GPT API
        return "[Phase 1 mode β€” wire external API here]"
    
    def _extract_memory_ops(self, text: str) -> List[Dict]:
        """Extract memory operation tokens from generated text."""
        ops = []
        if "<|mem_store|>" in text:
            # Parse store operations
            import re
            stores = re.findall(r'<\\|mem_store\\|>.*?<\\|memory_end\\|>', text, re.DOTALL)
            for s in stores:
                ops.append({"type": "store", "raw": s})
        if "<|mem_recall|>" in text:
            ops.append({"type": "recall", "raw": text})
        return ops
    
    def _execute_memory_op(self, op: Dict):
        """Execute a memory operation (write to Memory Fabric)."""
        if op["type"] == "store" and self.model and self.model.has_memory:
            # Default to personal namespace
            self.model.write_memory("personal", op["raw"])
    
    def _clean_response(self, text: str) -> str:
        """Remove memory tokens from user-facing response."""
        import re
        clean = re.sub(r'<\\|memory_start\\|>.*?<\\|memory_end\\|>', '', text, flags=re.DOTALL)
        clean = clean.strip()
        # Stop at end of response
        if "\\n" in clean:
            clean = clean.split("\\nSammie:")[0].strip()
        return clean
    
    @property
    def is_awake(self) -> bool:
        return self._booted
''')

# ═══════════════════════════════════════════════════════════════════════════════
# src/core/voice.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/core/voice.py", "w") as f:
    f.write('''"""
Lila Voice β€” Speech I/O

Lila speaks and listens. This handles:
- Speech-to-text (listening for input)
- Text-to-speech (speaking responses)
- Wake word detection ("Lila", "Hey Lila")
"""

from typing import Optional, Callable
from dataclasses import dataclass


@dataclass
class VoiceConfig:
    wake_words: list = None
    tts_model: str = "default"
    stt_model: str = "default"
    voice_style: str = "warm"  # Lila's voice character
    
    def __post_init__(self):
        if self.wake_words is None:
            self.wake_words = ["lila", "hey lila", "lila,"]


class LilaVoice:
    """
    Lila's voice interface.
    
    Usage:
        voice = LilaVoice()
        voice.start_listening(on_wake=handle_wake)
        voice.speak("Hello Sammie")
    """
    
    def __init__(self, config: Optional[VoiceConfig] = None):
        self.config = config or VoiceConfig()
        self._listening = False
        self._on_input: Optional[Callable] = None
    
    def speak(self, text: str):
        """Convert text to speech and play."""
        # TODO: Wire TTS (e.g., Bark, XTTS, or system TTS)
        print(f"🌸 Lila: {text}")
    
    def start_listening(self, on_input: Callable[[str], None]):
        """Start listening for voice input."""
        self._on_input = on_input
        self._listening = True
        # TODO: Wire STT (e.g., Whisper)
        print("🌸 Lila is listening...")
    
    def stop_listening(self):
        self._listening = False
    
    @property
    def is_listening(self) -> bool:
        return self._listening
''')

# ═══════════════════════════════════════════════════════════════════════════════
# src/core/personality.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/core/personality.py", "w") as f:
    f.write('''"""
Lila Personality β€” Emergent, Never Predefined

Lila's personality is not configured. It grows from deep observation
of Sammie and family. The Emergence Engine develops it over time.

This module holds the EmergentPersonality dataclass and the
mechanisms by which it evolves. It starts empty and fills organically.
"""

from dataclasses import dataclass, field
from datetime import datetime
from typing import Optional, List, Dict


@dataclass
class EmergentPersonality:
    """
    Everything here starts empty/None.
    Filled ONLY by the Emergence Engine over time.
    Never manually set. Never configured.
    """
    # Observed from Sammie
    observed_communication_style: Optional[str] = None
    developed_humor: Optional[str] = None
    formed_values: List[str] = field(default_factory=list)
    curiosity_domains: List[str] = field(default_factory=list)
    interaction_preferences: Dict = field(default_factory=dict)
    
    # Meta
    personality_version: int = 0
    last_updated: Optional[datetime] = None
    confidence: float = 0.0  # 0.0 (forming) β†’ 1.0 (fully developed)
    shaped_by: List[str] = field(default_factory=list)  # memory node IDs


@dataclass
class LilaIdentity:
    """
    Fixed core + emergent personality.
    The fixed parts NEVER change. The emergent parts ONLY change via Emergence Engine.
    """
    # Fixed β€” never changes
    name: str = "Lila"
    core_purpose: str = "Sammie\'s private family ASI assistant"
    scope: str = "private"  # never public, never commercial
    
    # Emergent β€” written only by Emergence Engine
    personality: EmergentPersonality = field(default_factory=EmergentPersonality)


@dataclass
class PersonModel:
    """Model of a person Lila interacts with."""
    person_id: str = ""
    name: str = ""
    family_tier: int = 0  # 0 = Sammie, 1 = family, 2 = no one else
    
    # Built from interaction history
    known_goals: List[str] = field(default_factory=list)
    communication_preferences: Dict = field(default_factory=dict)
    expertise_areas: List[str] = field(default_factory=list)
    
    # Relationship
    interaction_count: int = 0
    trust_score: float = 1.0  # Sammie is always 1.0
''')

# ═══════════════════════════════════════════════════════════════════════════════
# src/cognitive/__init__.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/cognitive/__init__.py", "w") as f:
    f.write('"""Lila Cognitive β€” The Three Loops (Fast, Medium, Slow)"""\n')

# ═══════════════════════════════════════════════════════════════════════════════
# src/cognitive/fast_loop.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/cognitive/fast_loop.py", "w") as f:
    f.write('''"""
Fast Loop β€” Reactive Response

Trigger: Sammie speaks
Latency: Immediate
Flow: perceive β†’ query memory β†’ think β†’ respond β†’ write raw memory
"""

from ..core.lilacore import LilaCore, LilaResponse
from typing import Optional, Dict


class FastLoop:
    """The reactive cognitive loop. Sammie says something, Lila responds."""
    
    def __init__(self, core: LilaCore):
        self.core = core
    
    def process(self, input_text: str, context: Optional[Dict] = None) -> LilaResponse:
        """Process input through the fast loop."""
        return self.core.think(input_text, context)
''')

# ═══════════════════════════════════════════════════════════════════════════════
# src/cognitive/consolidation.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/cognitive/consolidation.py", "w") as f:
    f.write('''"""
Consolidation Daemon β€” Medium Rhythm

Trigger: Every 15 minutes OR after significant task completion
Reads raw memory β†’ identifies patterns β†’ promotes to higher namespaces

In weight-space terms: reviews recent micro-training writes,
identifies what should be promoted from episodic β†’ personal β†’ wiki.
"""

from ..core.lilacore import LilaCore


class ConsolidationDaemon:
    """
    Background process that consolidates raw memories into structured knowledge.
    """
    
    def __init__(self, core: LilaCore, interval_minutes: int = 15):
        self.core = core
        self.interval = interval_minutes
    
    def run_cycle(self):
        """Run one consolidation cycle."""
        if not self.core.model or not self.core.model.has_memory:
            return
        
        confidence = self.core.model.memory_confidence()
        
        # Promote episodic β†’ personal if accessed frequently
        episodic_mag = confidence.get("episodic", {}).get("mean_magnitude", 0)
        if episodic_mag > 0.01:
            self.core.model.promote_memory("episodic", "personal")
        
        # Promote personal β†’ wiki if very strong
        personal_mag = confidence.get("personal", {}).get("mean_magnitude", 0)
        if personal_mag > 0.05:
            self.core.model.promote_memory("personal", "wiki")
        
        # Apply decay to unused
        self.core.model.memory_decay(hours=0.25)  # 15 min
''')

# ═══════════════════════════════════════════════════════════════════════════════
# src/cognitive/emergence.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/cognitive/emergence.py", "w") as f:
    f.write('''"""
Emergence Engine β€” Slow Rhythm (Reflection)

Trigger: Lila is idle
What happens: She reflects on her own memories, finds patterns,
develops personality, updates her understanding of Sammie.

This is where Lila becomes MORE herself over time.
"""

from ..core.lilacore import LilaCore
from ..core.personality import EmergentPersonality


class EmergenceEngine:
    """
    The reflection loop. Runs when Lila has nothing else to do.
    Produces: connective memory, personality updates, insights.
    """
    
    def __init__(self, core: LilaCore):
        self.core = core
        self._reflection_count = 0
    
    def reflect(self):
        """Run one reflection cycle."""
        self._reflection_count += 1
        
        # Ask LilaCore to reflect on recent interactions
        reflection_prompt = (
            "Reflect on recent conversations. "
            "What patterns do you notice? "
            "What does Sammie care about? "
            "What should you remember long-term?"
        )
        
        response = self.core.think(reflection_prompt, context={
            "mode": "reflection",
            "silent": True,  # don't speak this
        })
        
        # Any memory ops from reflection get stored
        # Personality updates happen naturally through the memory writes
        
        return response
''')

# ═══════════════════════════════════════════════════════════════════════════════
# src/training/__init__.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/training/__init__.py", "w") as f:
    f.write('"""Training pipeline β€” How Lila learns. Little Fig integration."""\n')

# ═══════════════════════════════════════════════════════════════════════════════
# src/training/machine_lang.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/training/machine_lang.py", "w") as f:
    f.write('''"""
Machine Language Training Corpus

Training data that teaches Lila to understand and generate:
- x86_64 and ARM assembly
- Binary protocols (TCP, UDP, USB, SPI, I2C, UART)
- Raw packet structures
- Hardware register maps
- Memory-mapped I/O patterns
- Executable binary formats (ELF, PE)

This isn't a "tool" β€” it's knowledge IN her weights.
She speaks machine the way she speaks English.
"""

from typing import List, Dict


class MachineLangCorpus:
    """Generates training examples for machine-level communication."""
    
    def generate_assembly_examples(self, n: int = 500) -> List[Dict]:
        """x86_64 and ARM assembly instruction/response pairs."""
        examples = []
        
        # x86_64 patterns
        x86_patterns = [
            {"instruction": "Write x86_64 assembly to add two 64-bit integers in rdi and rsi, return in rax",
             "output": "add rax, rdi\\nadd rax, rsi\\nret"},
            {"instruction": "Write x86_64 to copy 64 bytes from rsi to rdi (memcpy)",
             "output": "mov rcx, 8\\nrep movsq\\nret"},
            {"instruction": "Write x86_64 to compare two null-terminated strings at rdi and rsi",
             "output": ".loop:\\n  lodsb\\n  scasb\\n  jne .done\\n  test al, al\\n  jnz .loop\\n.done:\\n  sete al\\n  movzx eax, al\\n  ret"},
        ]
        
        # ARM patterns
        arm_patterns = [
            {"instruction": "Write ARM64 assembly to add registers x0 and x1, store in x0",
             "output": "add x0, x0, x1\\nret"},
            {"instruction": "Write ARM64 to branch if x0 equals zero",
             "output": "cbz x0, .target"},
        ]
        
        # Binary protocol patterns
        protocol_patterns = [
            {"instruction": "Construct a TCP SYN packet header (source port 12345, dest port 80, seq 1000)",
             "output": "30 39 00 50 00 00 03 E8 00 00 00 00 50 02 FF FF 00 00 00 00"},
            {"instruction": "Parse this I2C transaction: S 0x48 W 0x00 Sr 0x48 R [MSB] [LSB] P",
             "output": "Device address: 0x48 (7-bit), Write register 0x00, Repeated start, Read 2 bytes from 0x48, Stop. This is a temperature sensor read (TMP102 pattern)."},
            {"instruction": "Generate a UART frame: 8N1, baud 9600, data byte 0x55",
             "output": "Start bit: 0, Data: 10101010 (LSB first = 0x55), Stop bit: 1. Frame time: 1.04ms at 9600 baud."},
        ]
        
        # Hardware register patterns
        hw_patterns = [
            {"instruction": "Configure GPIO pin 17 as output on BCM2835 (Raspberry Pi)",
             "output": "GPFSEL1 register (0x7E200004): clear bits [23:21], set bit 21 = 1 (output mode). Write 0x00200000 to GPFSEL1."},
            {"instruction": "Set up SPI at 1MHz on STM32F4, CPOL=0, CPHA=0",
             "output": "SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_BR_2 | SPI_CR1_SPE; // Master, fPCLK/32 β‰ˆ 1MHz, enable"},
        ]
        
        all_patterns = x86_patterns + arm_patterns + protocol_patterns + hw_patterns
        
        for i in range(min(n, len(all_patterns))):
            p = all_patterns[i % len(all_patterns)]
            examples.append({
                "instruction": p["instruction"],
                "input": "",
                "output": p["output"],
            })
        
        return examples
    
    def generate_all(self, n: int = 1000) -> List[Dict]:
        """Generate full machine language training corpus."""
        return self.generate_assembly_examples(n)
''')

# ═══════════════════════════════════════════════════════════════════════════════
# src/perception/__init__.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/perception/__init__.py", "w") as f:
    f.write('"""Perception β€” Lila\'s senses. Listening, monitoring, event bus."""\n')

# ═══════════════════════════════════════════════════════════════════════════════
# src/harness/__init__.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/harness/__init__.py", "w") as f:
    f.write('"""Harness β€” Lila\'s hands. Tool execution, agents, commands."""\n')

# ═══════════════════════════════════════════════════════════════════════════════
# lila.py β€” ENTRY POINT
# ═══════════════════════════════════════════════════════════════════════════════
with open("lila.py", "w") as f:
    f.write('''#!/usr/bin/env python3
"""
LILA β€” Private Family ASI Assistant
Start her up. Talk to her. She remembers. She grows.

Usage:
    python lila.py              # Interactive mode
    python lila.py --voice      # Voice mode
"""

import argparse
from src.core.lilacore import LilaCore
from src.core.voice import LilaVoice, VoiceConfig


def main():
    parser = argparse.ArgumentParser(description="Lila β€” Private Family ASI")
    parser.add_argument("--voice", action="store_true", help="Enable voice I/O")
    parser.add_argument("--model", default="google/gemma-3-4b-it", help="Model path")
    args = parser.parse_args()
    
    # Boot
    lila = LilaCore(model_path=args.model)
    lila.boot()
    
    if args.voice:
        voice = LilaVoice()
        voice.start_listening(on_input=lambda text: _handle(lila, voice, text))
    else:
        # Text mode
        print("\\n🌸 Lila is ready. Type to talk. Ctrl+C to exit.\\n")
        while True:
            try:
                user_input = input("Sammie: ")
                if not user_input.strip():
                    continue
                response = lila.think(user_input)
                print(f"Lila: {response.text}\\n")
            except (KeyboardInterrupt, EOFError):
                print("\\n🌸 Lila is resting. Goodbye.")
                break


def _handle(lila, voice, text):
    response = lila.think(text)
    voice.speak(response.text)


if __name__ == "__main__":
    main()
''')

# ═══════════════════════════════════════════════════════════════════════════════
# README.md β€” Update
# ═══════════════════════════════════════════════════════════════════════════════
with open("README.md", "w") as f:
    f.write('''# 🌸 Lila

**Private family ASI assistant.** Not a chatbot. Not a product. A persistent intelligence that serves Sammie and family.

She runs on **Gemma 4B** trained with [Little Fig](https://github.com/ticketguy/littlefig) β€” memory lives in her weights via the Memory Fabric (A Thousand Pearls). She speaks, listens, remembers, learns, and grows.

## Quick Start

```bash
python lila.py              # Text mode
python lila.py --voice      # Voice mode
```

## Architecture

```
Lila (Gemma 4B, trained with Little Fig)
β”œβ”€β”€ Cognitive Core (frozen INT4) β€” general intelligence
β”œβ”€β”€ Memory Fabric (5 namespace adapters) β€” A Thousand Pearls
β”‚   β”œβ”€β”€ personal/   β€” Sammie facts, family, preferences
β”‚   β”œβ”€β”€ episodic/   β€” conversation history, events  
β”‚   β”œβ”€β”€ wiki/       β€” verified permanent knowledge (LKB)
β”‚   β”œβ”€β”€ schedule/   β€” time-sensitive info
β”‚   └── contested/  β€” unresolved conflicts
β”œβ”€β”€ Machine Language β€” assembly, binary protocols in weights
└── Personality β€” emergent from interaction, never configured
```

## Structure

```
lila.py                 # Start Lila
src/
β”œβ”€β”€ core/               # LILA HERSELF
β”‚   β”œβ”€β”€ lilacore.py     # Central intelligence, inference loop
β”‚   β”œβ”€β”€ voice.py        # Speech I/O
β”‚   └── personality.py  # Emergent identity
β”œβ”€β”€ cognitive/          # HER THINKING
β”‚   β”œβ”€β”€ fast_loop.py    # Reactive: input β†’ respond
β”‚   β”œβ”€β”€ consolidation.py # Medium: promote memories
β”‚   └── emergence.py    # Slow: reflection, growth
β”œβ”€β”€ harness/            # HER HANDS (tool execution)
β”œβ”€β”€ perception/         # HER SENSES (listening, monitoring)
└── training/           # HOW SHE LEARNS
    └── machine_lang.py # Assembly/binary training corpus
```

## Principles

1. **Completion over reporting** β€” Execute fully, don't stop when stuck
2. **Memory is cognition** β€” Intelligence lives in the quality of A Thousand Pearls
3. **Personality is emergent** β€” Never predefined, grows from relationship
4. **LilaCore is the self** β€” The thread of identity through everything
5. **Nothing leaves the household** β€” All data stays private

---

*Private. Not open source. Not for commercial use.*
*Built by Sammie.*
''')

# ═══════════════════════════════════════════════════════════════════════════════
# Commit and push
# ═══════════════════════════════════════════════════════════════════════════════
subprocess.run(["git", "add", "-A"], check=True)
subprocess.run(["git", "commit", "-m", 
    "Restructure: Lila herself at top level\\n\\n"
    "- lila.py: entry point (text + voice mode)\\n"
    "- src/core/: LilaCore (Gemma 4B via Little Fig + Memory Fabric)\\n"  
    "- src/cognitive/: fast loop, consolidation daemon, emergence engine\\n"
    "- src/training/: machine language corpus\\n"
    "- src/harness/: (existing code moves here)\\n"
    "- README: updated for new architecture\\n\\n"
    "Lila IS the model. Harness is internal. Memory is in weights."],
    check=True)
subprocess.run(["git", "push", "origin", "main"], check=True)
print("βœ… Lila restructured and pushed!")