-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcouncil.py
More file actions
940 lines (784 loc) · 39.6 KB
/
council.py
File metadata and controls
940 lines (784 loc) · 39.6 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
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
"""
council.py — The Council of Minds
===================================
All 9 agents + the Council Meeting protocol.
Imports from: universe.py | memory.py
Agents:
1. Perceiver — Object segmentation (WorldState)
2. Dreamer — World-model-based latent hypothesis generation
3. Scientist — Latent transformation discovery (z-vector search)
4. Skeptic — Adversarial falsification
5. Philosopher — Ontological re-perception & Basis rotation
6. CausalReasoner — Counterfactual causal testing
7. CuriosityEngine — Active-inference exploration drive
8. Metacognitor — Session monitor, chair, and convergence vote
9. Archivist — Episode memory, hints, and latent skill extraction
"""
import numpy as np
import random
import time
import hashlib
import logging
from dataclasses import dataclass
from typing import List, Dict, Optional, Tuple, Any, Generator
from universe import ARCTask, perceive_objects, DifficultyLevel, Universe
from memory import (
Blackboard, WorldState, Hypothesis,
HypothesisStatus, ContradictionEntry,
EpisodeMemory, EpisodeRecord, LatentSkillLibrary,
LatentSkill, SurpriseTracker,
)
from latent_dictionary import LatentDictionary
from meta_learner import MetaLearner
log = logging.getLogger("council")
logging.basicConfig(level=logging.INFO, format="%(levelname)s [%(name)s] %(message)s")
# ─── AGENT RESULTS ────────────────────────────────────────────────────────────
@dataclass
class AgentResult:
agent: str
success: bool
message: str
data: Dict = None
def __post_init__(self):
if self.data is None:
self.data = {}
# ─── AGENT 1: PERCEIVER ───────────────────────────────────────────────────────
class Perceiver:
"""Segments the raw ARC grid into discrete objects → WorldState."""
name = "Perceiver"
def perceive(self, grid: np.ndarray, bb: Blackboard, bg: int = 0) -> AgentResult:
objects_raw = perceive_objects(grid, bg=bg)
objects_list = [
{
"id": o.id,
"color": o.color,
"cells": o.cells,
"bbox": o.bbox,
"size": o.size,
}
for o in objects_raw
]
ws = WorldState(objects=objects_list, grid_shape=grid.shape, background_color=bg)
bb.set_world_state(ws)
return AgentResult(
agent=self.name, success=True,
message=f"Perceived {ws.object_count} objects.",
data=ws.to_dict()
)
# ─── AGENT 2: DREAMER ─────────────────────────────────────────────────────────
class Dreamer:
"""
Generates K imagined output hypotheses.
Sampling entirely from the learned continuous latent space.
"""
name = "Dreamer"
K: int = 8 # total hypotheses per round
def __init__(self, rng: random.Random = None):
self.rng = rng or random.Random()
def imagine(
self,
task: ARCTask,
bb: Blackboard,
skill_lib: LatentSkillLibrary,
latent_dict: LatentDictionary = None,
meta_learner: MetaLearner = None,
) -> AgentResult:
"""Generate K hypotheses from latent space."""
generated = 0
if latent_dict is not None:
# Bias toward skills that were useful in Prior Art
hints = bb.prior_art_hints
prior_z = None
if hints:
# Try to extract the first winning z vector from hints
for hint in hints:
z_val = hint.get("winning_z")
if z_val:
prior_z = np.array(z_val, dtype=np.float32)
break
# If meta_learner is active, use its prior
prior_z = meta_learner.get_prior_z() if meta_learner is not None else prior_z
z_samples = latent_dict.sample_z(n=self.K, temperature=1.0, prior_z=prior_z)
for k, z in enumerate(z_samples):
predicted = latent_dict.decode_z(z, task.test_input)
# Confidence inversely proportional to z norm (simpler = better)
z_norm = float(np.linalg.norm(z))
confidence = 1.0 / (1.0 + z_norm * 0.5 + k * 0.05)
# Penalize empty outputs
if np.all(predicted == 0):
confidence = 0.01
# Generate program string
z_mdl = float(np.count_nonzero(z > 0.01))
z_str = f"z[{z_mdl:.0f} active dims, dreamer=True]"
h = bb.push_hypothesis(predicted, confidence=confidence, agent=self.name)
bb.update_hypothesis(h.id, winning_z=z.tolist(), program=z_str, program_mdl=z_mdl)
generated += 1
return AgentResult(
agent=self.name, success=True,
message=f"Imagined {generated} latent hypotheses.",
data={"n_hypotheses": generated}
)
# ─── AGENT 3: SCIENTIST ────────────────────────────────────────────────────────
class Scientist:
"""
Finds the shortest latent vector that explains the top Dreamer hypothesis
and generalizes across all training pairs. No explicitly defined rules.
"""
name = "Scientist"
LATENT_ERROR_THRESHOLD = 0.5 # max reconstruction MSE to accept a latent solution
def __init__(self, rng: random.Random = None):
self.rng = rng or random.Random()
def synthesize(
self,
task: ARCTask,
bb: Blackboard,
skill_lib: LatentSkillLibrary,
latent_dict: LatentDictionary = None,
meta_learner: MetaLearner = None,
extended_search: bool = False,
) -> AgentResult:
"""Search for a generalizing latent transformation."""
if latent_dict is not None and latent_dict.is_ready:
# If meta_learner is active, use its prior
prior_z = meta_learner.get_prior_z() if meta_learner is not None else None
# Curiosity Engine can request a deeper search
n_cand = 120 if extended_search else 40
n_ref = 60 if extended_search else 20
# Try to find a z vector that generalizes across all training pairs
z_candidates = []
for inp, out in task.train_pairs:
# Use Meta-Learned prior if available
z, err = latent_dict.search_z(inp, out, n_cand, n_ref, prior_z)
if z is not None:
z_candidates.append((z, err))
if z_candidates:
# Average all per-pair z vectors to find a common transformation
avg_z = np.mean([zc[0] for zc in z_candidates], axis=0)
avg_z = np.maximum(avg_z, 0)
avg_err = float(np.mean([zc[1] for zc in z_candidates]))
# Check if the averaged z generalises across training pairs
# Accept if mean pixel error <= 2.0 (soft tolerance, not pixel-perfect)
GENERALISATION_THRESHOLD = 5.0 # Raised: ARC is hard, allow approximate z-transforms
latent_generalises = True
total_err = 0.0
pair_count = 0
for inp, expected in task.train_pairs:
predicted = latent_dict.decode_z(avg_z, inp)
if predicted.shape != expected.shape:
latent_generalises = False
break
if np.all(predicted == 0) and np.any(expected != 0):
latent_generalises = False
break
mse = float(np.mean((predicted.astype(float) - expected.astype(float)) ** 2))
total_err += mse
pair_count += 1
if latent_generalises and pair_count > 0:
mean_train_err = total_err / pair_count
if mean_train_err > GENERALISATION_THRESHOLD:
latent_generalises = False
if latent_generalises:
z_mdl = float(np.count_nonzero(avg_z > 0.01)) # MDL = active components
z_str = f"z[{z_mdl:.0f} active dims, err={avg_err:.4f}]"
top_h = bb.get_top_hypothesis()
if top_h:
predicted = latent_dict.decode_z(avg_z, task.test_input)
bb.update_hypothesis(
top_h.id,
program=z_str,
program_mdl=z_mdl,
grid=predicted,
status=HypothesisStatus.TESTING,
winning_z=avg_z.tolist() # Custom internal passing mechanism
)
return AgentResult(
agent=self.name, success=True,
message=f"Latent program: {z_str} (MDL={z_mdl:.1f})",
data={"program": z_str, "mdl": z_mdl, "source": "latent", "winning_z": avg_z.tolist()}
)
return AgentResult(
agent=self.name, success=False,
message="No generalizing latent program found in this round.",
)
# ─── AGENT 4: SKEPTIC ─────────────────────────────────────────────────────────
class Skeptic:
"""
Adversarially falsifies the Scientist's program using input mutations.
Implements Popper's Falsificationism.
"""
name = "Skeptic"
MUTATION_COUNT = 12
def __init__(self, rng: random.Random = None):
self.rng = rng or random.Random()
def challenge(
self,
task: ARCTask,
bb: Blackboard,
latent_dict: LatentDictionary = None,
) -> AgentResult:
top_h = bb.get_top_hypothesis()
if top_h is None or top_h.program is None:
return AgentResult(
agent=self.name, success=False,
message="No program to falsify.",
)
winning_z = getattr(top_h, 'winning_z', None)
if winning_z is None or latent_dict is None or not latent_dict.is_ready:
return AgentResult(
agent=self.name, success=True,
message="PASS — No explicit z-vector to falsify.",
)
z_vec = np.array(winning_z, dtype=np.float32)
for m in range(self.MUTATION_COUNT):
mutant_inp = self._mutate(task.train_pairs[0][0])
expected_out = latent_dict.decode_z(z_vec, task.train_pairs[0][0])
try:
mutant_out = latent_dict.decode_z(z_vec, mutant_inp)
except Exception:
mutant_out = np.zeros_like(mutant_inp)
# The mutation test: if the program's behavior changes predictably, fine.
# If the mutation produces nonsense / crashes, flag it.
if mutant_out.shape != task.test_input.shape and mutant_inp.shape == task.test_input.shape:
entry = ContradictionEntry(
hypothesis_id=top_h.id,
counter_example_input=mutant_inp,
produced_output=mutant_out,
expected_output=expected_out,
failure_mode="shape_mismatch_under_mutation",
agent=self.name,
)
bb.add_contradiction(entry)
return AgentResult(
agent=self.name, success=False,
message=f"FALSIFIED on mutation {m}. Mode: shape_mismatch_under_mutation.",
data={"mutation": m, "failure": "shape_mismatch"}
)
# All mutations survived → mark hypothesis as TESTING passed
bb.update_hypothesis(top_h.id, status=HypothesisStatus.TESTING)
return AgentResult(
agent=self.name, success=True,
message=f"PASS — program survived {self.MUTATION_COUNT} adversarial mutations.",
data={"mutations_tested": self.MUTATION_COUNT}
)
def _mutate(self, grid: np.ndarray) -> np.ndarray:
"""Apply a random structural mutation to a grid."""
mutation = self.rng.choice(["swap_colors", "add_noise", "shift"])
out = grid.copy()
if mutation == "swap_colors":
colors = list(np.unique(out[out != 0]))
if len(colors) >= 2:
a, b = self.rng.sample(colors, 2)
tmp = out.copy()
tmp[grid == a] = b
tmp[grid == b] = a
out = tmp
elif mutation == "add_noise":
n_points = self.rng.randint(1, 3)
for _ in range(n_points):
r = self.rng.randint(0, out.shape[0] - 1)
c = self.rng.randint(0, out.shape[1] - 1)
out[r, c] = self.rng.randint(1, 9)
elif mutation == "shift":
shift_r = self.rng.randint(-1, 1)
shift_c = self.rng.randint(-1, 1)
out = np.roll(out, shift_r, axis=0)
out = np.roll(out, shift_c, axis=1)
return out
# ─── AGENT 5: PHILOSOPHER ─────────────────────────────────────────────────────
class Philosopher:
"""
Challenges the Perceiver's object decomposition.
Proposes an alternative WorldState when falsification cannot be explained.
"""
name = "Philosopher"
def reframe(
self,
grid: np.ndarray,
bb: Blackboard,
revision: int = 0,
latent_dict: LatentDictionary = None,
) -> AgentResult:
"""
Rotates the basis of the top latent hypotheses to provide an orthogonal 'perspective'.
"""
top_h = bb.get_top_hypothesis()
if top_h is None or not hasattr(top_h, 'winning_z') or top_h.winning_z is None:
return AgentResult(
agent=self.name, success=False,
message="No latent vector to reframe.",
)
if latent_dict is None or not latent_dict.is_ready:
return AgentResult(
agent=self.name, success=False,
message="Latent dictionary not ready for basis rotation.",
)
z_orig = np.array(top_h.winning_z, dtype=np.float32)
z_rot = latent_dict.rotate_basis(z_orig, angle_idx=revision)
# Test if the rotated perspective offers a simpler explanation (lower norm)
# Note: In a pure rotation, norm should be identical. But because we clip
# to np.maximum(..., 0) during rotation (non-negative constraints), the norm
# effectively changes, sometimes sparsifying the vector further.
if np.linalg.norm(z_rot) < np.linalg.norm(z_orig) + 0.1:
# Update the hypothesis with this new rotated latent vector
top_h.winning_z = z_rot.tolist()
return AgentResult(
agent=self.name, success=True,
message=f"Reframed latent vector (revision {revision + 1}). Norm: {np.linalg.norm(z_rot):.2f}",
data={"revision": revision + 1}
)
return AgentResult(
agent=self.name, success=False,
message="Philosopher rotation did not yield a simpler explanation.",
)
# ─── AGENT 6: CAUSAL REASONER ─────────────────────────────────────────────────
class CausalReasoner:
"""
Tests whether the Scientist's program is a CAUSAL_LAW or a COINCIDENCE
using counterfactual interventions.
"""
name = "CausalReasoner"
COUNTERFACTUAL_COUNT = 8
def __init__(self, rng: random.Random = None):
self.rng = rng or random.Random()
def verify(
self,
task: ARCTask,
bb: Blackboard,
latent_dict: LatentDictionary = None,
) -> AgentResult:
top_h = bb.get_top_hypothesis()
if top_h is None or not hasattr(top_h, 'winning_z') or top_h.winning_z is None:
return AgentResult(
agent=self.name, success=False,
message="No latent vector to verify causally.",
)
if latent_dict is None or not latent_dict.is_ready:
return AgentResult(
agent=self.name, success=False,
message="Latent dictionary not ready for causal intervention.",
)
z_arr = np.array(top_h.winning_z, dtype=np.float32)
failures = 0
for _ in range(self.COUNTERFACTUAL_COUNT):
cf_input = self._intervene(task.train_pairs[0][0])
try:
original_pred = latent_dict.decode_z(z_arr, task.train_pairs[0][0])
cf_pred = latent_dict.decode_z(z_arr, cf_input)
# A causal transformation vector should produce *different* outputs for different inputs
if np.array_equal(original_pred, cf_pred) and not np.array_equal(task.train_pairs[0][0], cf_input):
failures += 1 # The transformation is insensitive (coincidence)
except Exception:
failures += 1
verdict = "COINCIDENCE" if failures > self.COUNTERFACTUAL_COUNT // 2 else "CAUSAL_LAW"
bb.update_hypothesis(
top_h.id,
causal_verdict=verdict,
status=HypothesisStatus.CAUSAL_LAW if verdict == "CAUSAL_LAW" else HypothesisStatus.COINCIDENCE,
)
return AgentResult(
agent=self.name, success=(verdict == "CAUSAL_LAW"),
message=f"Verdict: {verdict} (failures={failures}/{self.COUNTERFACTUAL_COUNT})",
data={"verdict": verdict, "failures": failures}
)
def _intervene(self, grid: np.ndarray) -> np.ndarray:
"""Single-variable counterfactual: change one cell's color."""
out = grid.copy()
r = self.rng.randint(0, out.shape[0] - 1)
c = self.rng.randint(0, out.shape[1] - 1)
old_val = out[r, c]
new_val = self.rng.choice([v for v in range(0, 10) if v != old_val])
out[r, c] = new_val
return out
# ─── AGENT 7: CURIOSITY ENGINE ────────────────────────────────────────────────
class CuriosityEngine:
"""
Monitors the SurpriseMetric and fires Exploration Directives
when the Council is stuck (plateau detected).
Implements Active Inference — drives toward minimal free energy.
"""
name = "CuriosityEngine"
def __init__(self):
self.tracker = SurpriseTracker()
self.intervention_count = 0
def observe(
self,
predicted: np.ndarray,
actual: np.ndarray,
bb: Blackboard,
) -> AgentResult:
error = self.tracker.compute(predicted, actual)
bb.record_surprise(error, agent=self.name)
if self.tracker.is_plateauing:
self.intervention_count += 1
directive = self._pick_directive(bb)
return AgentResult(
agent=self.name, success=False,
message=f"PLATEAU detected (error={error:.3f}). Directive: {directive}",
data={"directive": directive, "error": error}
)
return AgentResult(
agent=self.name, success=True,
message=f"Surprise: {error:.3f} ({'resolved' if self.tracker.is_resolved else 'ongoing'})",
data={"error": error, "resolved": self.tracker.is_resolved}
)
def _pick_directive(self, bb: Blackboard) -> str:
n_falsified = sum(
1 for h in bb.hypothesis_stack
if h.status == HypothesisStatus.FALSIFIED
)
if n_falsified >= 3:
return "PHILOSOPHER_REFRAME"
if self.intervention_count % 2 == 0:
return "DREAMER_EXPLORE_LOW_CONFIDENCE"
return "SCIENTIST_EXTEND_SEARCH"
# ─── AGENT 8: METACOGNITOR ────────────────────────────────────────────────────
class Metacognitor:
"""
God's-eye view of the Council.
Chairs the meeting, detects lazy behavior, manages the Convergence Vote.
"""
name = "Metacognitor"
LAZY_THRESHOLD = 2 # same output in N consecutive rounds = lazy behavior
def __init__(self):
self._last_outputs: Dict[str, Any] = {}
self._lazy_counts: Dict[str, int] = {}
def arbitrate(
self,
bb: Blackboard,
curiosity_directive: Optional[str],
) -> AgentResult:
"""Decide the next meeting agenda based on current state."""
if bb.final_verdict != "pending":
return AgentResult(
agent=self.name, success=True,
message="Meeting concluded.",
data={"verdict": bb.final_verdict}
)
# Budget critical → convergence vote
if bb.budget_critical:
return self._convergence_vote(bb)
# Curiosity engine requested intervention
if curiosity_directive == "PHILOSOPHER_REFRAME":
agenda = ["Philosopher", "Perceiver", "Dreamer", "Scientist", "Skeptic", "CausalReasoner"]
elif curiosity_directive == "DREAMER_EXPLORE_LOW_CONFIDENCE":
agenda = ["Dreamer", "Scientist", "Skeptic", "CausalReasoner"]
elif curiosity_directive == "SCIENTIST_EXTEND_SEARCH":
agenda = ["Scientist", "Skeptic", "CausalReasoner"]
else:
# Default flow
top_h = bb.get_top_hypothesis()
if top_h is None:
agenda = ["Dreamer", "Scientist", "Skeptic", "CausalReasoner"]
elif top_h.status == HypothesisStatus.PENDING:
agenda = ["Scientist", "Skeptic", "CausalReasoner"]
elif top_h.status == HypothesisStatus.FALSIFIED:
agenda = ["Dreamer", "Scientist", "Skeptic", "CausalReasoner"]
else:
agenda = ["Skeptic", "CausalReasoner"]
bb.set_agenda(agenda)
return AgentResult(
agent=self.name, success=True,
message=f"Agenda set: {' → '.join(agenda)}",
data={"agenda": agenda}
)
def _convergence_vote(self, bb: Blackboard) -> AgentResult:
"""Force a convergence vote among all surviving hypotheses."""
candidates = [
h for h in bb.hypothesis_stack
if h.status not in (HypothesisStatus.FALSIFIED, HypothesisStatus.COINCIDENCE)
]
if not candidates:
bb.declare_answer(None, "unknown", self.name)
return AgentResult(
agent=self.name, success=False,
message="Convergence vote: NO surviving hypotheses. Declaring UNKNOWN.",
data={"vote": "unknown"}
)
winner = max(candidates, key=lambda h: h.confidence * (1.0 if h.causal_verdict == "CAUSAL_LAW" else 0.5))
# Only accept a winner that has an actual synthesized program (not a raw Dreamer phantom)
if winner.confidence < 0.40 or winner.program is None:
# Still declare best-guess grid for display (not a solved answer, just best effort)
best_grid = winner.grid if winner.grid is not None else None
bb.declare_answer(best_grid, "unknown", self.name)
return AgentResult(
agent=self.name, success=False,
message=f"Convergence vote: No real program found (confidence={winner.confidence:.2f}). Best guess displayed.",
data={"vote": "unknown"}
)
bb.update_hypothesis(winner.id, status=HypothesisStatus.ACCEPTED)
bb.declare_answer(winner.grid, "solved", self.name)
return AgentResult(
agent=self.name, success=True,
message=f"Convergence vote: {winner.id} wins (confidence={winner.confidence:.2f}).",
data={"winner": winner.id, "confidence": winner.confidence}
)
# ─── AGENT 9: ARCHIVIST ───────────────────────────────────────────────────────
class Archivist:
"""
Long-term memory, Prior Art hint generation, and Skill Primitive extraction.
"""
name = "Archivist"
def __init__(self, memory: EpisodeMemory, skill_lib: LatentSkillLibrary,
latent_dict: LatentDictionary = None, meta_learner: MetaLearner = None):
self.memory = memory
self.skill_lib = skill_lib
self.latent_dict = latent_dict
self.meta_learner = meta_learner
def inject_hints(self, task: ARCTask, bb: Blackboard) -> AgentResult:
"""Retrieve similar past episodes and inject hints into the Blackboard."""
priors = [p.value for p in task.priors_used]
similar = self.memory.retrieve_similar(priors, k=3)
hints = [ep.to_dict() for ep in similar]
bb.set_prior_art(hints)
return AgentResult(
agent=self.name, success=True,
message=f"Injected {len(hints)} prior art hints.",
data={"hints": hints}
)
def archive(
self,
task: ARCTask,
bb: Blackboard,
) -> AgentResult:
"""Store this episode, extract skill primitives, and feed the latent dictionary."""
top_accepted = next(
(h for h in bb.hypothesis_stack if h.status == HypothesisStatus.ACCEPTED),
None,
)
winning_program = top_accepted.program if top_accepted else None
winning_z = getattr(top_accepted, 'winning_z', None) if top_accepted else None
causal_label = top_accepted.causal_verdict or "UNKNOWN" if top_accepted else "UNKNOWN"
record = EpisodeRecord(
task_id=task.task_id,
task_fingerprint=task.fingerprint,
priors_used=[p.value for p in task.priors_used],
winning_program=winning_program,
winning_z=winning_z,
causal_label=causal_label,
rounds_to_solve=bb.round,
budget_used=bb.budget_used,
surprise_arc=list(bb.surprise_history),
verdict=bb.final_verdict,
)
self.memory.store(record)
# Extract latent skill from the winning z-vector
# Fallback: if Scientist didn't win, derive z from the test pair directly
effective_z = winning_z
if effective_z is None and self.latent_dict is not None and self.latent_dict.is_ready:
fallback_z, fallback_err = self.latent_dict.search_z(
task.test_input, task.test_output, 20, 10
)
if fallback_z is not None:
effective_z = fallback_z.tolist()
if effective_z:
self.skill_lib.add_skill(LatentSkill(
name=f"z_{task.task_id}",
description=f"Transformation for {task.task_id} ({task.transformation_description})",
z_vector=effective_z,
origin_task_id=task.task_id,
))
# ── Feed the Latent Dictionary with all training pairs + test pair ──
if self.latent_dict is not None:
for inp, out in task.train_pairs:
self.latent_dict.register_pair(
inp, out, task_id=task.task_id,
label=task.transformation_description[:40]
)
# Also register the test pair (ground truth)
self.latent_dict.register_pair(
task.test_input, task.test_output,
task_id=task.task_id,
label=f"{task.transformation_description[:30]}_test"
)
# ── Feed the Meta-Learner ──
if effective_z is not None and self.meta_learner is not None:
self.meta_learner.update(winning_z=np.array(effective_z), rounds_to_solve=bb.round)
return AgentResult(
agent=self.name, success=True,
message=f"Archived episode. Verdict: {bb.final_verdict}. Total: {self.memory.total_episodes}.",
data={"verdict": bb.final_verdict, "total_episodes": self.memory.total_episodes}
)
# ─── THE COUNCIL MEETING LOOP ─────────────────────────────────────────────────
class Council:
"""
The orchestrator of the 9-agent Council Meeting.
Drives the Socratic loop until consensus or budget exhaustion.
"""
MAX_ROUNDS = 30
def __init__(self, seed: int = None):
rng = random.Random(seed)
self.perceiver = Perceiver()
self.dreamer = Dreamer(rng)
self.scientist = Scientist(rng)
self.skeptic = Skeptic(rng)
self.philosopher = Philosopher()
self.causal = CausalReasoner(rng)
self.curiosity = CuriosityEngine()
self.metacognitor = Metacognitor()
self.memory = EpisodeMemory()
self.skill_lib = LatentSkillLibrary()
self.latent_dict = LatentDictionary(seed=seed or 42)
self.meta_learner = MetaLearner()
self.archivist = Archivist(self.memory, self.skill_lib, self.latent_dict, self.meta_learner)
def solve(self, task: ARCTask, stream: bool = False) -> Generator[Dict, None, Dict]:
"""
Run the full Council Meeting for a given task.
Yields a log dict after each agent action (for live dashboard streaming).
Returns the final Blackboard snapshot.
"""
bb = Blackboard(task.task_id)
philosopher_revision = 0
# ── PHASE 0: ORIENTATION ──────────────────────────────────────────────
self._emit(bb, "Orientation", "Meeting begins.")
yield bb.snapshot()
result = self.perceiver.perceive(task.test_input, bb)
self._emit(bb, result.agent, result.message)
yield bb.snapshot()
result = self.archivist.inject_hints(task, bb)
self._emit(bb, result.agent, result.message)
yield bb.snapshot()
# ── PRE-SEED: feed current task's training pairs into the dictionary NOW ──
# This makes the dictionary warm during the search, not cold.
for inp, out in task.train_pairs:
self.latent_dict.register_pair(
inp, out, task_id=task.task_id,
label=task.transformation_description[:40]
)
# ── PHASE 1: FIRST IMAGINATION ───────────────────────────────────────
bb.advance_round()
result = self.dreamer.imagine(task, bb, self.skill_lib, self.latent_dict, self.meta_learner)
self._emit(bb, result.agent, result.message)
yield bb.snapshot()
# ── MAIN DEBATE LOOP ──────────────────────────────────────────────────
curiosity_directive: Optional[str] = None
while bb.final_verdict == "pending" and bb.round < self.MAX_ROUNDS:
bb.advance_round()
# Metacognitor sets the agenda
meta_result = self.metacognitor.arbitrate(bb, curiosity_directive)
self._emit(bb, meta_result.agent, meta_result.message)
yield bb.snapshot()
if bb.final_verdict != "pending":
break
agenda = bb.meeting_agenda
for agent_name in agenda:
if agent_name == "Scientist":
extended = (curiosity_directive == "SCIENTIST_EXTEND_SEARCH")
result = self.scientist.synthesize(task, bb, self.skill_lib, self.latent_dict, self.meta_learner, extended_search=extended)
self._emit(bb, result.agent, result.message)
yield bb.snapshot()
elif agent_name == "Skeptic":
result = self.skeptic.challenge(task, bb, self.latent_dict)
self._emit(bb, result.agent, result.message)
yield bb.snapshot()
if not result.success:
# Skeptic found a contradiction → curiosity engine evaluates
top_h = bb.get_top_hypothesis()
if top_h is not None:
predicted = top_h.grid
curiosity_result = self.curiosity.observe(
predicted, task.test_output, bb
)
self._emit(bb, curiosity_result.agent, curiosity_result.message)
curiosity_directive = curiosity_result.data.get("directive")
yield bb.snapshot()
break # restart round
elif agent_name == "CausalReasoner":
# Capture the top pending hypothesis BEFORE causal.verify changes its status
top_h_before = bb.get_top_hypothesis()
result = self.causal.verify(task, bb, self.latent_dict)
self._emit(bb, result.agent, result.message)
yield bb.snapshot()
if result.success and top_h_before is not None:
# Causal law confirmed → accept and declare solved
bb.update_hypothesis(top_h_before.id, status=HypothesisStatus.ACCEPTED)
bb.declare_answer(top_h_before.grid, "solved", "Council")
self._emit(bb, "Council", f"SOLVED in {bb.round} rounds! Program: {top_h_before.program}")
yield bb.snapshot()
elif not result.success:
curiosity_directive = "DREAMER_EXPLORE_LOW_CONFIDENCE"
break
elif agent_name == "Dreamer":
result = self.dreamer.imagine(task, bb, self.skill_lib, self.latent_dict, self.meta_learner)
self._emit(bb, result.agent, result.message)
yield bb.snapshot()
elif agent_name == "Philosopher":
result = self.philosopher.reframe(task.test_input, bb, philosopher_revision, self.latent_dict)
philosopher_revision += 1
self._emit(bb, result.agent, result.message)
yield bb.snapshot()
# After reframe, re-perceive
result = self.perceiver.perceive(task.test_input, bb)
self._emit(bb, result.agent, result.message)
yield bb.snapshot()
elif agent_name == "Perceiver":
result = self.perceiver.perceive(task.test_input, bb)
self._emit(bb, result.agent, result.message)
yield bb.snapshot()
if bb.final_verdict != "pending":
break
# Curiosity engine end-of-round observation (once per round, best hypothesis)
if bb.final_verdict == "pending":
top_h = bb.get_top_hypothesis()
if top_h is None:
# No PENDING hypotheses; pick best non-falsified
candidates = [h for h in bb.hypothesis_stack
if h.status not in (HypothesisStatus.FALSIFIED, HypothesisStatus.COINCIDENCE)
and h.grid is not None]
if candidates:
top_h = max(candidates, key=lambda h: h.confidence)
if top_h is not None:
curiosity_result = self.curiosity.observe(top_h.grid, task.test_output, bb)
curiosity_directive = curiosity_result.data.get("directive")
self._emit(bb, curiosity_result.agent, curiosity_result.message)
# ── PHASE 5: ARCHIVAL ─────────────────────────────────────────────────
archive_result = self.archivist.archive(task, bb)
self._emit(bb, archive_result.agent, archive_result.message)
yield bb.snapshot()
def stats(self) -> Dict:
return {
"total_episodes": self.memory.total_episodes,
"solved": self.memory.solved_count,
"avg_rounds": round(self.memory.avg_rounds, 1),
"skill_library_size": len(self.skill_lib.get_all()),
"generalization_series": self.memory.get_generalization_series(),
"latent_skills": self.skill_lib.to_dict(),
"latent_dictionary": self.latent_dict.stats(),
"meta_learner": self.meta_learner.stats() if self.meta_learner else {},
}
@staticmethod
def _emit(bb: Blackboard, agent: str, message: str) -> None:
"""Log an agent action to the Blackboard audit log."""
# Store with 'message' key so the dashboard can display it directly
bb.agent_call_log.append({
"round": bb.round,
"agent": agent,
"action": "speak",
"message": message,
"data": {},
"timestamp": round(time.time(), 3),
})
log.info("[Round %02d | %s] %s", bb.round, agent, message)
# ─── SELF-TEST ────────────────────────────────────────────────────────────────
if __name__ == "__main__":
print("=" * 60)
print("Council Self-Test — 3 tasks")
print("=" * 60)
universe = Universe(seed=99)
council = Council(seed=99)
for i, level in enumerate([DifficultyLevel.L1, DifficultyLevel.L2, DifficultyLevel.L1]):
task = universe.generate_task(level)
print(f"\n── Task {i+1}: {task.task_id} ──")
print(f" Rule: {task.transformation_description}")
final_snapshot = None
for snapshot in council.solve(task):
final_snapshot = snapshot
print(f" Verdict : {final_snapshot['final_verdict']}")
print(f" Rounds : {final_snapshot['round']}")
print(f" Budget : {final_snapshot['budget_used']}/100")
s = council.stats()
print(f"\n── Council Stats ──")
print(f" Episodes : {s['total_episodes']}")
print(f" Solved : {s['solved']}")
print(f" Avg Rounds : {s['avg_rounds']}")
print(f" Skills : {s['skill_library_size']}")
print("\n✓ council.py self-test passed.")