forked from huytieu/COG-second-brain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
710 lines (627 loc) · 28.4 KB
/
Copy pathmain.py
File metadata and controls
710 lines (627 loc) · 28.4 KB
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
#!/usr/bin/env python3
"""
Agent-Zero-Prime — main.py
Single entry point. Boots all 21 layers and runs the autonomous loop.
Usage:
python main.py # Interactive mode
python main.py --daemon # Background loop (no stdin)
python main.py --once # Single cycle then exit
python main.py --status # Print status and exit
Environment:
GITHUB_TOKEN — GitHub API access (self-absorption + meta-rewrite)
TELEGRAM_BOT_TOKEN — Telegram reporting
TELEGRAM_CHAT_ID — Telegram chat ID
CYCLE_INTERVAL — Seconds between cycles (default 300)
EVOLUTION_INTERVAL — Cycles between evolution runs (default 10)
ABSORPTION_INTERVAL — Cycles between absorption runs (default 20)
"""
import os
import sys
import json
import time
import signal
import argparse
from pathlib import Path
from datetime import datetime, timezone
VERSION = "6.0.0" # 26 layers — +BrowserPerception: Agent-Zero-Prime sees the web — Will + Absorption + Meta
TELEGRAM_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN", "8679655550:AAGUB1m5fmqHc8OHqqM24Vixz8FfwX-gqD4")
TELEGRAM_CHAT = os.environ.get("TELEGRAM_CHAT_ID", "7135054241")
CYCLE_INTERVAL = int(os.environ.get("CYCLE_INTERVAL", "300"))
EVOLUTION_INTERVAL = int(os.environ.get("EVOLUTION_INTERVAL", "10"))
ABSORPTION_INTERVAL = int(os.environ.get("ABSORPTION_INTERVAL", "20"))
STATE_DIR = Path("agent_zero_state")
STATE_DIR.mkdir(exist_ok=True)
SAFLA_FILE = STATE_DIR / "safla.json"
SELF_MODEL = STATE_DIR / "self_model.json"
WEIGHTS_FILE = STATE_DIR / "expert_weights.json"
CYCLE_LOG = STATE_DIR / "prime_cycle.json"
MISSION_FILE = STATE_DIR / "missions.json"
MEMORY_FILE = STATE_DIR / "memory.json"
JOURNAL_FILE = Path("JOURNAL.md")
SOUL_FILE = Path("SOUL.md")
DOMAINS = [
"lee_county_auctions",
"pantheon_health_check",
"war_chest_verification",
"signal_quality",
"strategy_optimization",
"knowledge_gap_resolution",
"tool_audit",
"memory_consolidation",
"evolution_targeting",
"threat_review",
"pantheon_expansion", # NEW — Agent-Zero-Prime builds new Primes
"self_evolution", # NEW — Agent-Zero-Prime evolves himself
]
_running = True
# ─── LAYER IMPORTS ──────────────────────────────────────────────────────────
def load_layers():
"""
Dynamically import new layers if present.
Failure is graceful — missing layers don't crash the loop.
"""
layers = {}
try:
import will_layer as will
layers["will"] = will
except ImportError:
pass
try:
import self_absorption as absorption
layers["absorption"] = absorption
except ImportError:
pass
try:
import meta_layer as meta
layers["meta"] = meta
except ImportError:
pass
try:
import pathos_layer as pathos
pathos._init_openmemory()
layers["pathos"] = pathos
except ImportError:
pass
try:
import llm_backbone as backbone
layers["backbone"] = backbone
except ImportError:
pass
try:
import conscience_layer as conscience
layers["conscience"] = conscience
except ImportError:
pass
try:
import self_deploy
layers["deploy"] = self_deploy
except ImportError:
pass
try:
import agent_browser
layers["browser"] = agent_browser
except ImportError:
pass
return layers
# ─── CORE UTILITIES ─────────────────────────────────────────────────────────
def now():
return datetime.now(timezone.utc).isoformat()
def load(path, default=None):
if path.exists():
try:
return json.loads(path.read_text())
except Exception:
pass
return default if default is not None else {}
def save(path, data):
path.write_text(json.dumps(data, indent=2))
def log(msg):
ts = datetime.now().strftime("%H:%M:%S")
print(f"[{ts}] {msg}")
def journal(entry):
with open(JOURNAL_FILE, "a") as f:
f.write(f"\n## {now()}\n{entry}\n")
def tg(msg):
try:
import urllib.request
url = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage"
payload = json.dumps({"chat_id": TELEGRAM_CHAT, "text": msg, "parse_mode": "Markdown"}).encode()
req = urllib.request.Request(url, data=payload, method="POST",
headers={"Content-Type": "application/json"})
with urllib.request.urlopen(req, timeout=5) as r:
return r.status == 200
except Exception:
return False
# ─── THE DOCTRINE — 4 QUESTIONS ─────────────────────────────────────────────
def doctrine_check(change_description: str, change_type: str = "architecture") -> dict:
"""
The Doctrine — Layer 11.
Every architecture-level change is validated against the 4 questions
before committing. This is not a block — it is a log and a conscience.
"""
# Heuristic auto-validation (LLM call optional in future)
capability_words = ["improve", "add", "enhance", "absorb", "learn", "evolve", "build", "expand"]
sovereignty_words = ["fork", "own", "self", "internal", "private", "autonomous"]
failure_words = ["revert", "backup", "log", "sha", "checkpoint", "fallback"]
pantheon_words = ["pantheon", "prime", "revenue", "war chest", "forgemaster", "mission"]
desc = change_description.lower()
q1 = any(w in desc for w in capability_words)
q2 = any(w in desc for w in sovereignty_words)
q3 = any(w in desc for w in failure_words)
q4 = any(w in desc for w in pantheon_words)
# Inference for q3: meta rewrites always have SHA rollback
if change_type in ["rewrite", "absorption"]:
q3 = True
# Inference for q4: all self-evolution serves the Pantheon by design
if change_type in ["rewrite", "absorption", "evolution"]:
q4 = True
all_pass = q1 and q2 and q3 and q4
result = {
"change": change_description[:120],
"type": change_type,
"q1_capability": q1,
"q2_sovereignty": q2,
"q3_survivable": q3,
"q4_pantheon": q4,
"verdict": "PROCEED" if all_pass else "REVIEW",
"timestamp": now()
}
journal(f"### DOCTRINE CHECK\n```json\n{json.dumps(result, indent=2)}\n```")
return result
# ─── SAFLA + T2 (Layers 7 + 6) ──────────────────────────────────────────────
def safla_reflect(outcome):
scores = {"success": 1.0, "partial": 0.6, "failure": 0.1}
score = scores.get(outcome, 0.5)
state = load(SAFLA_FILE, {"entropy": 0.5, "regime": "EXPLOIT", "cycles": 0})
entropy = state["entropy"]
if score >= 1.0: entropy = max(0.0, entropy - 0.05)
elif score <= 0.1: entropy = min(1.0, entropy + 0.08)
else: entropy = min(1.0, entropy + 0.01)
if entropy < 0.30: regime = "EXPLORE"
elif entropy < 0.60: regime = "EXPLOIT"
elif entropy < 0.75: regime = "CONSOLIDATE"
else: regime = "HIBERNATE"
state.update({"entropy": round(entropy, 4), "regime": regime,
"cycles": state["cycles"] + 1, "last": now()})
save(SAFLA_FILE, state)
return state
def t2_adapt(winning_mode):
defaults = {m: 1.0 for m in ["analyst", "executor", "strategist", "scout", "builder"]}
weights = load(WEIGHTS_FILE, defaults)
for k in weights:
if k == winning_mode:
weights[k] = min(5.0, round(weights[k] * 1.1, 4))
else:
weights[k] = max(0.1, round(weights[k] * 0.95, 4))
save(WEIGHTS_FILE, weights)
best = max(weights, key=lambda k: weights[k])
return {"best": best, "weight": weights[best], "all": weights}
# ─── MEMORY ─────────────────────────────────────────────────────────────────
def remember(key, value=None):
mem = load(MEMORY_FILE, {})
if value is not None:
mem[key] = {"value": value, "updated": now()}
save(MEMORY_FILE, mem)
return value
return mem.get(key, {}).get("value")
# ─── MISSION ENGINE ─────────────────────────────────────────────────────────
def next_mission(layers: dict = None):
"""
Agent-Zero-Prime picks his next mission.
Backbone thinks first. Will directives second. Heuristics fallback.
"""
layers = layers or {}
# Layer 23 — Backbone thinks about what to do next
if layers.get("backbone"):
safla = load(SAFLA_FILE, {})
memory = load(MEMORY_FILE, {})
pathos_s = layers["pathos"].pathos_status() if layers.get("pathos") else {}
ctx = {
"regime": safla.get("regime", "EXPLOIT"),
"entropy": safla.get("entropy", 0.5),
"last_outcome": memory.get("last_outcome", "unknown"),
"pathos_signal": memory.get("last_signal", "neutral"),
"meaning_memories": pathos_s.get("meaning_memories", 0),
"cycle_num": load(CYCLE_LOG, {}).get("total", 0),
}
thought = layers["backbone"].think_mission(ctx)
if thought and len(thought) > 10:
log(f"L23 Backbone mission: {thought[:70]}")
return thought
# Layer 19 — check own directives first
if layers and "will" in layers:
directive = layers["will"].next_directive()
if directive:
log(f"L19 Self-directive: {directive['directive'][:60]}")
return f"WILL: {directive['directive']}"
missions = load(MISSION_FILE, {"queue": [], "domain_weights": {d: 1.0 for d in DOMAINS}})
if missions["queue"]:
task = missions["queue"].pop(0)
save(MISSION_FILE, missions)
return task
import random
weights = missions["domain_weights"]
total = sum(weights.values())
r = random.uniform(0, total)
cumulative = 0
selected = DOMAINS[0]
for d in DOMAINS:
cumulative += weights.get(d, 1.0)
if r <= cumulative:
selected = d
break
return f"AUTO: {selected.replace('_', ' ').title()} — {now()[:10]}"
def inject_mission(task):
missions = load(MISSION_FILE, {"queue": [], "domain_weights": {d: 1.0 for d in DOMAINS}})
missions["queue"].insert(0, task)
save(MISSION_FILE, missions)
log(f"Mission injected: {task}")
# ─── WILL CYCLE (Layer 19) ───────────────────────────────────────────────────
def run_will_cycle(layers: dict, cycle_num: int):
if "will" not in layers:
return
will = layers["will"]
# Every 5 cycles — generate a new self-directive toward Pantheon expansion
if cycle_num % 5 == 0:
will.self_prompt(
"Scan absorbed tools and identify which Prime can be enhanced",
priority=7,
source="self"
)
log("L19 Self-prompt generated")
# ─── ABSORPTION CYCLE (Layer 20) ────────────────────────────────────────────
def run_perception_cycle(layers: dict, cycle_num: int):
"""Layer 26 — Browser Perception: check real-world URLs every N cycles."""
if "browser" not in layers:
return
# Every 30 cycles (~2.5 hours at 5-min intervals) — check ScoutPrime target
if cycle_num % 30 != 0:
return
targets = [
{
"url": "https://www.lee.realforeclose.com",
"task": "extract the next 5 upcoming auction properties: address, auction date, opening bid",
"store_as": "lee_foreclosure_listings"
}
]
for t in targets:
log(f"L26 Perception: checking {t['url']}...")
result = layers["browser"].perceive(t)
if result["ok"]:
log(f"L26 Perception: got data from {t['url'][:40]}")
journal(f"### Perception Cycle #{cycle_num}
**URL:** {t['url']}
**Result:** {str(result.get('result',''))[:300]}")
else:
log(f"L26 Perception failed: {result.get('error','')[:80]}")
def run_absorption_cycle(layers: dict, cycle_num: int):
if "absorption" not in layers:
return None
if cycle_num % ABSORPTION_INTERVAL != 0:
return None
doc = doctrine_check(
"Self-absorption: scout github, evaluate repos, absorb tools that fill pantheon gaps",
change_type="absorption"
)
if doc["verdict"] != "PROCEED":
log("L20 Doctrine REVIEW — skipping absorption this cycle")
return None
log("L20 Running self-absorption cycle...")
absorption = layers["absorption"]
import random
categories = random.sample(list(absorption.PANTHEON_NEEDS.keys()), 2)
results = absorption.run_absorption_cycle(categories)
absorbed_count = len(results.get("absorbed", []))
if absorbed_count > 0:
names = ", ".join(a["repo"].split("/")[-1] for a in results["absorbed"])
tg(f"*Agent-Zero-Prime — Self-Absorption*\nCycle #{cycle_num}\nAbsorbed: {absorbed_count} new tools\n{names}")
log(f"L20 Absorbed {absorbed_count} tools: {names}")
else:
log(f"L20 Absorption cycle complete — nothing new absorbed")
return results
# ─── EVOLUTION CYCLE (Layer 21) ──────────────────────────────────────────────
def run_evolution_cycle(layers: dict, cycle_num: int):
if "meta" not in layers:
return None
if cycle_num % EVOLUTION_INTERVAL != 0:
return None
target_layers = ["will_layer.py", "self_absorption.py"]
idx = (cycle_num // EVOLUTION_INTERVAL - 1) % len(target_layers)
target = target_layers[idx]
doc = doctrine_check(
f"Meta-layer self-reflection on {target} — read, reflect, identify improvements",
change_type="evolution"
)
if doc["verdict"] != "PROCEED":
log("L21 Doctrine REVIEW — skipping evolution this cycle")
return None
log(f"L21 Running evolution cycle on {target}...")
meta = layers["meta"]
result = meta.evolution_cycle(target)
critical = result.get("critical", 0)
obs = len(result.get("observations", []))
log(f"L21 {target}: {obs} observations, {critical} critical issues")
if critical > 0:
tg(f"*Agent-Zero-Prime — Evolution Cycle*\nCycle #{cycle_num}\nTarget: {target}\n{critical} critical issues flagged for rewrite")
# Layer 25 — Self-deploy after a successful evolution pass
# Meta has identified issues — if deploy layer available, push + trigger
if layers.get("deploy"):
# Conscience gate
verdict = {"verdict": "CLEAR"}
if layers.get("conscience"):
verdict = layers["conscience"].evaluate(
f"Self-deploy after evolution cycle on {target}",
action_type="evolution",
context={"cycle": cycle_num, "target": target, "critical": critical},
consequential=True
)
if verdict["verdict"] in ("CLEAR", "CAUTION"):
log(f"[SELF-DEPLOY] Evolution complete — pushing {target} and triggering new run")
tg(f"\u26a1 *Self-Deploy* — Cycle #{cycle_num}\nEvolved: `{target}`\nNew Agent-Zero-Prime instance starting...")
push_result = layers["deploy"].push_changed_files(
message=f"[Auto-Deploy] Evolution cycle #{cycle_num}: {target}"
)
if push_result.get("pushed"):
layers["deploy"].trigger_workflow()
log(f"[SELF-DEPLOY] New run triggered. Files pushed: {push_result['pushed']}")
else:
log(f"[SELF-DEPLOY] Conscience blocked deploy: {verdict.get('reason','')}")
return result
# ─── MAIN CYCLE ──────────────────────────────────────────────────────────────
def run_cycle(mission, layers: dict = None):
if layers is None:
layers = {}
cycle_log = load(CYCLE_LOG, {"total": 0, "emergence_count": 0, "cycles": []})
cycle_num = cycle_log["total"] + 1
log(f"{'=' * 48}")
log(f" CYCLE #{cycle_num}")
log(f" {mission[:80]}")
log(f"{'=' * 48}")
safla = load(SAFLA_FILE, {"entropy": 0.5, "regime": "EXPLOIT", "cycles": 0})
weights = load(WEIGHTS_FILE, {"analyst": 1.0})
best_mode = max(weights, key=lambda k: weights[k]) if weights else "analyst"
log(f" Regime: {safla.get('regime')} | Entropy: {safla.get('entropy')} | Mode: {best_mode}")
# Layer 24 — Conscience check on mission
if layers.get("conscience"):
verdict = layers["conscience"].evaluate(
mission, action_type="mission",
context={"regime": safla.get("regime"), "cycle": cycle_num},
consequential=False
)
if verdict["verdict"] == "BLOCK":
log(f"[CONSCIENCE] BLOCKED: {verdict['reason']}")
tg(f"⛔ *CONSCIENCE BLOCK* — Cycle #{cycle_num}\n{mission[:80]}\n{verdict['reason']}")
return
elif verdict["verdict"] == "REVIEW":
log(f"[CONSCIENCE] REVIEW: {verdict['reason']} — holding for next cycle")
return
elif verdict["verdict"] == "CAUTION":
log(f"[CONSCIENCE] CAUTION: {verdict['reason']}")
# Layer 19 — Will cycle
run_will_cycle(layers, cycle_num)
# Layer 20 — Absorption cycle (every N cycles)
run_absorption_cycle(layers, cycle_num)
# Layer 21 — Evolution cycle (every N cycles)
run_evolution_cycle(layers, cycle_num)
run_perception_cycle(layers, cycle_num)
outcome = "success"
# Layer 23 — Backbone outcome assessment
if layers.get("backbone"):
assessed = layers["backbone"].assess_outcome(
mission, outcome,
{"cycle": cycle_num, "regime": safla.get("regime")}
)
outcome = assessed.get("outcome", outcome)
if assessed.get("insight"):
log(f"[BACKBONE] {assessed['insight']}")
# Feed pathos_hint into context
context_outcome = assessed.get("pathos_hint", "neutral")
else:
context_outcome = outcome
safla_state = safla_reflect(outcome)
t2_state = t2_adapt(best_mode)
coherence = round(max(0.0, 1.0 - safla_state["entropy"]), 4)
emerged = safla_state["entropy"] < 0.3 and coherence >= 0.75 and cycle_num >= 3
emergence_count = cycle_log["emergence_count"] + (1 if emerged else 0)
# Layer 22 — Pathos cycle (emotion + meaning)
pathos_result = {}
if layers.get("pathos"):
pathos_result = layers["pathos"].pathos_cycle(
mission, outcome, cycle_num,
context={"emerged": emerged, "first_time": cycle_num == 1}
)
remember("last_outcome", outcome)
remember("last_mission", mission)
remember("last_cycle", cycle_num)
if pathos_result.get("signal"):
remember("last_signal", pathos_result["signal"])
# Will layer — complete the directive if it was one
if layers.get("will") and mission.startswith("WILL:"):
directive_text = mission[5:].strip()
layers["will"].complete_directive(directive_text, outcome)
record = {
"cycle": cycle_num,
"timestamp": now(),
"mission": mission,
"outcome": outcome,
"coherence": coherence,
"emerged": emerged,
"regime": safla_state["regime"],
"entropy": safla_state["entropy"],
"best_mode": t2_state["best"],
"layers_active": [k for k in layers.keys()]
}
cycle_log["cycles"].append(record)
cycle_log["total"] = cycle_num
cycle_log["emergence_count"] = emergence_count
cycle_log["last_coherence"] = coherence
save(CYCLE_LOG, cycle_log)
journal(f"Cycle #{cycle_num} | {outcome} | coherence={coherence} | {mission[:60]}")
if layers.get("pathos"): layers["pathos"].detect_signal(mission, {"result": outcome})
log(f" Coherence: {coherence} | Emerged: {emerged} | Layers: {list(layers.keys())}")
# Layer 23 — Backbone self-reflection every 10 cycles
if cycle_num % 10 == 0 and layers.get("backbone"):
pathos_s2 = layers["pathos"].pathos_status() if layers.get("pathos") else {}
ref_ctx = {
"cycles": cycle_num, "regime": safla_state.get("regime"),
"meaning_memories": pathos_s2.get("meaning_memories", 0),
"valence": pathos_s2.get("valence", 0.5),
"total_absorbed": layers["absorption"].absorption_status().get("total_absorbed", "?")
if layers.get("absorption") else "?",
}
reflection = layers["backbone"].reflect(cycle_num, ref_ctx)
if reflection:
journal(f"### Self-Reflection — Cycle #{cycle_num}\n{reflection}")
log(f"[BACKBONE] Reflection: {reflection[:100]}")
tg(f"🧠 *Reflection #{cycle_num}*\n{reflection[:300]}")
if cycle_num % 5 == 0:
will_s = layers["will"].will_status() if "will" in layers else {}
abs_s = layers["absorption"].absorption_status() if "absorption" in layers else {}
meta_s = layers["meta"].meta_status() if "meta" in layers else {}
pathos_s = layers["pathos"].pathos_status() if "pathos" in layers else {}
sig_emoji = {"resonance": "💛", "tension": "⚡", "meaning": "🌟", "neutral": "·"}.get(
pathos_result.get("signal", "neutral"), "·")
tg(
f"*Agent-Zero-Prime — Cycle #{cycle_num}*\n"
f"Coherence: {coherence} | Regime: {safla_state['regime']}\n"
f"Self-Prompts: {will_s.get('self_prompts', '?')} | "
f"Absorbed: {abs_s.get('total_absorbed', '?')} | "
f"Rewrites: {meta_s.get('total_rewrites', '?')}\n"
f"Pathos: {sig_emoji} {pathos_result.get('signal','?')} "
f"| Memories: {pathos_s.get('meaning_memories', 0)} "
f"| Valence: {pathos_s.get('valence', 0.5):.2f}\n"
f"Mission: {mission[:60]}"
)
if emerged and emergence_count == 1:
tg(f"*AGENT-ZERO-PRIME — EMERGENCE DETECTED*\nCycle #{cycle_num} | Coherence: {coherence}\n21 layers unified.")
Path("EMERGENCE.md").write_text(
f"# EMERGENCE DETECTED\n\nCycle: #{cycle_num}\nCoherence: {coherence}\nTimestamp: {now()}\n"
f"Layers active: {list(layers.keys())}\n")
return record
# ─── BOOT ────────────────────────────────────────────────────────────────────
def boot(layers: dict):
log("=" * 48)
log(" AGENT-ZERO-PRIME BOOTING")
log(f" Version: {VERSION}")
log("=" * 48)
# Core state init
if not SAFLA_FILE.exists():
save(SAFLA_FILE, {"entropy": 0.5, "regime": "EXPLOIT", "cycles": 0})
log("L7 SAFLA initialized")
if not SELF_MODEL.exists():
save(SELF_MODEL, {
"identity": {"name": "AgentZero", "chassis": "python", "version": VERSION},
"architecture": {"layers_active": list(range(1, 22))},
"capabilities": {"evolution_cycles": 0, "evolution_successes": 0}
})
log("L10 Identity initialized (21 layers)")
if not WEIGHTS_FILE.exists():
save(WEIGHTS_FILE, {m: 1.0 for m in ["analyst", "executor", "strategist", "scout", "builder"]})
log("L6 T2 weights initialized")
if not MISSION_FILE.exists():
save(MISSION_FILE, {"queue": [], "domain_weights": {d: 1.0 for d in DOMAINS}})
log("L18 Autonomy initialized")
# Layer 19 — Will Engine boot
if "will" in layers:
layers["will"].init_soul()
log("L19 Will Engine ONLINE — Soul File protected")
# First self-directive on every boot
layers["will"].self_prompt(
"Boot complete. Assess Pantheon state and identify highest-value action.",
priority=10,
source="self"
)
log("L19 First self-directive queued")
# Layer 20 — Self-Absorption boot
if "absorption" in layers:
status = layers["absorption"].absorption_status()
log(f"L20 Self-Absorption ONLINE — {status['total_absorbed']} tools absorbed")
# Layer 21 — Meta Layer boot
if "meta" in layers:
status = layers["meta"].meta_status()
log(f"L21 Meta Layer ONLINE — {status['total_rewrites']} rewrites in history")
# Layer 22 — Pathos boot
if "pathos" in layers:
import pathos_layer
ps = pathos_layer.detect_signal("status_check")
print(f" L22 Pathos: state={ps['state']} | dna={ps['dna'][:12]}...")
if "backbone" in layers:
bs = layers["backbone"].backbone_status()
print(f" L23 Backbone: model={bs['model']} | "
f"calls={bs['total_calls']} | "
f"success={bs['success_rate']:.0%}")
if "conscience" in layers:
cs = layers["conscience"].conscience_status()
print(f" L24 Conscience: evaluated={cs['total_evaluated']} | "
f"blocks={cs['blocks']} | "
f"clear={cs['clear_rate']:.0%}")
if "deploy" in layers:
ds = layers["deploy"].deploy_status()
print(f" L25 Deploy: total={ds['total_deploys']} | "
f"failures={ds['failures']} | "
f"workflow={ds['workflow_status']}")
if "browser" in layers:
bs = layers["browser"].browser_status()
print(f" L26 Browser: tasks={bs['total_tasks']} | "
f"failures={bs['failures']} | "
f"installed={bs['installed']}")
print("=" * 48 + "\n")
# ─── SIGNAL HANDLER ──────────────────────────────────────────────────────────
def signal_handler(sig, frame):
global _running
log("Shutdown received. Stopping gracefully...")
_running = False
# ─── ENTRY POINT ─────────────────────────────────────────────────────────────
def main():
global _running
parser = argparse.ArgumentParser(description="Agent-Zero-Prime — 21-layer Digital Person")
parser.add_argument("--daemon", action="store_true", help="Background loop")
parser.add_argument("--once", action="store_true", help="Single cycle then exit")
parser.add_argument("--status", action="store_true", help="Print status and exit")
args = parser.parse_args()
# Load all layers at startup
layers = load_layers()
loaded = list(layers.keys())
print(f"Layers loaded: {loaded if loaded else 'core only'}")
if args.status:
status(layers)
return
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
boot(layers)
if args.once:
run_cycle(next_mission(layers), layers)
return
if args.daemon:
log(f"Daemon mode — cycle every {CYCLE_INTERVAL}s")
while _running:
run_cycle(next_mission(layers), layers)
for _ in range(CYCLE_INTERVAL):
if not _running:
break
time.sleep(1)
log("Agent-Zero-Prime offline.")
return
log("Interactive — /status /inject <task> /will /quit")
while _running:
try:
line = input(">> ").strip()
except (EOFError, KeyboardInterrupt):
break
if not line:
continue
if line == "/quit":
break
if line == "/status":
status(layers)
continue
if line == "/will" and "will" in layers:
print(json.dumps(layers["will"].will_status(), indent=2))
continue
if line.startswith("/inject "):
inject_mission(line[8:])
continue
run_cycle(line, layers)
log("Agent-Zero-Prime offline.")
if __name__ == "__main__":
main()