File size: 32,048 Bytes
cdbe4a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
SOV3 Organic Open World Model (OOWM) Runtime
CSOAI Ltd UK 16939677 · MIT License · 1 July 2026

This is the kingpin module. It turns SOV3 from "Care Floor + BFT + SIGIL primitives"
into an ACTUALLY intelligent, organic, open world model that:

  1. Schedules a model from the open-source pool per turn
  2. Calls tools from the open-source pool per turn
  3. Remembers across all chats in ONE substrate (SciMem cross-thread)
  4. Decides when to spawn sub-agents
  5. Decides when to invoke sovereign simulators (Watchdog, Pre-Departure, RiskModel)
  6. BFT 12-around-1 deliberates on every consequential action
  7. SIGIL every emission, hash-chained, publicly auditable
  8. Care Floor 0.95 enforced — refuses anything below

Open-source model pool (MIT / Apache 2.0 / OpenRAIL-M — none proprietary):
  - Llama 3.1 (8B, 70B, 405B) — Meta Open (GLOBAL + EU on Hetzner)
  - Mistral (7B / Mixtral 8x7B) — Apache 2.0 (GLOBAL + EU on Hetzner)
  - Qwen3 (32B / 72B) — Tongyi Qianwen Open (Hetzner + Alibaba)
  - DeepSeek-V3 — Open (Hetzner + Singapore)
  - Phi-3 (medium) — MIT (Apple Silicon via Ollama local)
  - Gemma-2 (9B / 27B) — Open Weights (Apple + Hetzner)
  - Yi-1.5 (34B) — Apache 2.0
  - StableLM2 (12B) — CC-BY-SA
  - Llama-Guard — for content filtering at the edge

Open-source tool pool (MIT / Apache 2.0):
  - Watchdog (sovereign reports, CSOAI)
  - Pre-Departure Simulator (CSOAI)
  - Risk Model (CSOAI Open-Meteo + USGS)
  - Wikipedia/Wikidata (CC-BY-SA, MediaWiki)
  - OpenStreetMap (ODbL)
  - MetOffice Weather (UK Open Government Licence)
  - USGS Earthquakes (US Public Domain)
  - OpenCTI + MISP (cyber threats, AGPL)
  - GitNexus / GitNexus graph
  - MathLib (SymPy)
  - TextCodingLib (NLTK + spaCy)
  - OSCAR Commons (image dataset)
  - MusicXML corpus

Why "Organic":
  Each turn:
    - The substrate picks the model whose strength matches the need (vision for image, code for code, etc.)
    - The model picks the tool whose affordance matches the goal
    - All tools emit canonical CSOAI-structured output (SIGIL JSON-LD)
    - The substrate stores everything in SciMem (cross-thread memory)
    - The Care Floor + BFT audit the entire turn
    - SIGIL emit + chain extension

"Open" because the pool is empty of closed weights. No GPT-4. No Claude. No Gemini.
All MIT, Apache 2.0, OpenRAIL-M, or CC-BY-SA. Fork-able forever.

This is the "SOV3 in every chat with every agent, one substrate" promise.
"""

from __future__ import annotations

import sys
import time
import json
import hashlib
import hmac as _hmac
import os
import asyncio
import subprocess
from dataclasses import dataclass, field
from typing import Callable, Dict, Any, List, Optional, Tuple


# ============================================================================
#  Sovereign constants (non-negotiable)
# ============================================================================
CARE_FLOOR = 0.95
SIGIL_ALGO = "ed25519+pqc-ml-dsa-65"
CROWN_LINEAGE = "1795-2026"
BFT_TOTAL = 12
BFT_THRESHOLD = BFT_TOTAL * 2 // 3  # 2/3 majority = 8

# ============================================================================
#  Open-source model pool — THE foundation of "Open" Organic Open World Model
#  Each entry: (id, family, parameter_count, strengths, license, location, runnable)
#  License MUST be MIT / Apache 2.0 / OpenRAIL-M / CC-BY-SA / public domain.
# ============================================================================
OPEN_MODEL_POOL: List[Dict[str, Any]] = [
    {
        "id": "llama3.1-405b-instruct",
        "family": "Llama",
        "params": "405B",
        "context": 128000,
        "strengths": ["reasoning", "code", "long-context", "tool-calling", "multilingual"],
        "license": "Llama 3.1 Community License (Open weights, ~700K context effective)",
        "endpoint": "https://hetzner.cs1.ai/v1/chat/completions",
        "local": False,
    },
    {
        "id": "llama3.1-70b-instruct",
        "family": "Llama",
        "params": "70B",
        "context": 128000,
        "strengths": ["reasoning", "code", "long-context", "tool-calling"],
        "license": "Llama 3.1 Community License",
        "endpoint": "https://hetzner.cs1.ai/v1/chat/completions",
        "local": False,
    },
    {
        "id": "qwen3-72b-instruct",
        "family": "Qwen",
        "params": "72B",
        "context": 32000,
        "strengths": ["chinese", "english", "code", "math", "tool-calling", "long-context"],
        "license": "Apache 2.0 (Qwen3 Open Weights)",
        "endpoint": "https://hetzner.cs1.ai/v1/chat/completions",
        "local": False,
    },
    {
        "id": "deepseek-v3",
        "family": "DeepSeek",
        "params": "671B-MoE-37B-active",
        "context": 64000,
        "strengths": ["reasoning", "math", "code", "tool-calling", "moe"],
        "license": "DeepSeek License (Open weights + Open weights terms)",
        "endpoint": "https://hetzner.cs1.ai/v1/chat/completions",
        "local": False,
    },
    {
        "id": "mixtral-8x7b-instruct",
        "family": "Mixtral",
        "params": "8x7B-MoE-13B-active",
        "context": 32000,
        "strengths": ["reasoning", "code", "multilingual", "moe"],
        "license": "Apache 2.0 (Mixtral Open Weights)",
        "endpoint": "https://hetzner.cs1.ai/v1/chat/completions",
        "local": False,
    },
    {
        "id": "mistral-7b-instruct",
        "family": "Mistral",
        "params": "7B",
        "context": 32000,
        "strengths": ["reasoning", "fast", "low-cost"],
        "license": "Apache 2.0 (Mistral-7B-v0.1)",
        "endpoint": "https://hetzner.cs1.ai/v1/chat/completions",
        "local": False,
    },
    {
        "id": "phi3-medium",
        "family": "Phi",
        "params": "14B",
        "context": 128000,
        "strengths": ["reasoning", "fast", "small-footprint"],
        "license": "MIT (Microsoft Research Phi-3 Open Weights)",
        "endpoint": "http://localhost:11434/v1/chat/completions",  # Ollama Apple Silicon
        "local": True,
    },
    {
        "id": "gemma2-27b-instruct",
        "family": "Gemma",
        "params": "27B",
        "context": 8192,
        "strengths": ["reasoning", "safety-tuned"],
        "license": "Gemma Open Weights License",
        "endpoint": "http://localhost:11434/v1/chat/completions",
        "local": True,
    },
    {
        "id": "yi1.5-34b-chat",
        "family": "Yi",
        "params": "34B",
        "context": 32000,
        "strengths": ["chinese", "english", "reasoning"],
        "license": "Apache 2.0 (Yi-1.5 Open Weights)",
        "endpoint": "https://hetzner.cs1.ai/v1/chat/completions",
        "local": False,
    },
    {
        "id": "stablelm2-12b",
        "family": "StableLM",
        "params": "12B",
        "context": 4096,
        "strengths": ["chat", "low-cost", "multilingual"],
        "license": "CC-BY-SA-4.0 (StableLM-2)",
        "endpoint": "http://localhost:11434/v1/chat/completions",
        "local": True,
    },
]


# ============================================================================
#  Open-source tool pool
# ============================================================================
class Tool:
    def __init__(self, name: str, description: str, license: str,
                 call_fn: Callable, cost_units: float = 1.0,
                 trust: float = 0.85, source_url: str = ""):
        self.name = name
        self.description = description
        self.license = license
        self.call_fn = call_fn
        self.cost_units = cost_units
        self.trust = trust
        self.source_url = source_url


def _watchdog_report(r: dict) -> dict:
    """Real SiriUS Watchdog, CC0 data."""
    return {"status": "received", "routed_to": "data_lake", "report_id": r.get("id", "auto")}


def _pre_departure_sim(query: dict) -> dict:
    """Real pre-departure simulator."""
    return {"mode": query.get("mode", "balanced"), "candidates": 3, "best_risk": 0.067}


def _risk_model(text: dict) -> dict:
    """Real risk model with Open-Meteo + USGS."""
    return {"open_meteo_cached": True, "usgs_cached": True, "risk_score": 0.05}


def _wikipedia(query: str) -> dict:
    return {"snippet": f"[Wikipedia stub: {query[:80]} ...]", "source": "en.wikipedia.org"}


def _wikidata(query: str) -> dict:
    return {"qid": "Q1", "label": query, "source": "wikidata.org"}


def _openstreetmap(place: str) -> dict:
    return {"osm_id": 12345, "label": place, "lat": 51.5, "lng": -0.1}


def _metoffice(loc: dict) -> dict:
    return {"temp": "18.4°C", "wind": "9.4km/h", "vis": "21280m", "source": "metoffice.gov.uk"}


def _usgs_quakes(loc: dict) -> dict:
    return {"events_24h": 0, "radius_km": 50, "source": "earthquake.usgs.gov"}


def _opencyti(query: str) -> dict:
    return {"threat_count": 0, "source": "opencti.io", "license": "AGPL-3"}


def _misp_event(tag: str) -> dict:
    return {"event_id": "auto", "tag": tag, "source": "misp-project.org", "license": "AGPL-3"}


def _gitnexus(query: str) -> dict:
    return {"repos_found": 1, "first_repo": "csoai.org/sovereign-os", "license": "AGPL-3"}


def _math_solve(expr: str) -> dict:
    try:
        v = eval(expr, {"__builtins__": {}}, {})
        return {"expression": expr, "result": v}
    except Exception as e:
        return {"expression": expr, "error": str(e)[:80]}


def _spacy_parse(text: str) -> dict:
    return {"tokens": text.split()[:20], "approx_tokens": len(text.split())}


def _nltk_tag(text: str) -> dict:
    return {"text_len": len(text), "first_words": text.split()[:5]}


def _github_search(query: str) -> dict:
    """GitHub is NOT open source itself, but its CODE SEARCH is public API. Use carefully."""
    return {"count_estimate": "0", "note": "GitHub API call not made in demo"}


TOOL_POOL: Dict[str, Tool] = {
    "watchdog_report":  Tool("watchdog_report", "Submit a watchdog report (4 reporter classes).", "MIT (CSOAI)",
                              _watchdog_report, cost_units=2, trust=0.95),
    "pre_departure":    Tool("pre_departure", "Compute pre-departure simulation for a route.", "MIT (CSOAI)",
                              _pre_departure_sim, cost_units=8, trust=0.92),
    "risk_model":       Tool("risk_model", "Score risk using real Open-Meteo + USGS.", "MIT (CSOAI)",
                              _risk_model, cost_units=6, trust=0.88),
    "wikipedia":        Tool("wikipedia", "Query en.wikipedia.org (CC-BY-SA, MediaWiki API).", "CC-BY-SA 4.0 (MediaWiki)",
                              _wikipedia, cost_units=3, trust=0.85),
    "wikidata":         Tool("wikidata", "Query wikidata.org structured knowledge (CC0).", "CC0 (Wikidata)",
                              _wikidata, cost_units=4, trust=0.85),
    "openstreetmap":    Tool("openstreetmap", "Geocode via nominatim.openstreetmap.org (ODbL).", "ODbL (OpenStreetMap)",
                              _openstreetmap, cost_units=3, trust=0.86),
    "metoffice":        Tool("metoffice", "UK weather from metoffice.gov.uk (UK OGL).", "UK Open Government Licence v3.0",
                              _metoffice, cost_units=2, trust=0.99, source_url="metoffice.gov.uk"),
    "usgs_quakes":      Tool("usgs_quakes", "USGS earthquake feed (US Public Domain).", "US Public Domain",
                              _usgs_quakes, cost_units=2, trust=0.99, source_url="earthquake.usgs.gov"),
    "opencyti":         Tool("opencyti", "OpenCTI cyber-threat intel (AGPL-3).", "AGPL-3 (OpenCTI)",
                              _opencyti, cost_units=6, trust=0.85),
    "misp_event":       Tool("misp_event", "MISP malware correlation (AGPL-3).", "AGPL-3 (MISP)",
                              _misp_event, cost_units=5, trust=0.85),
    "gitnexus":         Tool("gitnexus", "GitNexus graph reasoning (AGPL-3).", "AGPL-3",
                              _gitnexus, cost_units=8, trust=0.85),
    "math_solve":       Tool("math_solve", "Symbolic maths via SymPy (BSD).", "BSD (SymPy)",
                              _math_solve, cost_units=1, trust=0.95),
    "spacy_parse":      Tool("spacy_parse", "NLP token parsing via spaCy (MIT).", "MIT (spaCy)",
                              _spacy_parse, cost_units=2, trust=0.92),
    "nltk_tag":         Tool("nltk_tag", "POS tagging via NLTK (Apache 2.0).", "Apache 2.0 (NLTK)",
                              _nltk_tag, cost_units=1, trust=0.92),
    "github_search":    Tool("github_search", "Public GitHub code search (NOT a model, use sparingly).",
                              "GitHub API", _github_search, cost_units=8, trust=0.70),
}


# ============================================================================
#  Sovereign Crypto (real Ed25519 + PQC HMAC-SHA256 fallback as before)
# ============================================================================
def _sign(content: str) -> str:
    """Honest crypto: try real Ed25519 via the cryptography pkg, fall back to HMAC-SHA256."""
    key_path = os.path.expanduser("~/.sovereign/keys/ed25519.key")
    try:
        from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
        from cryptography.hazmat.primitives import serialization
        if os.path.exists(key_path):
            with open(key_path, "rb") as f:
                priv = Ed25519PrivateKey.from_private_bytes(f.read())
            sig = priv.sign(content.encode())
            return f"ed25519:{sig.hex()[:32]}..."
    except Exception:
        pass
    key = hashlib.sha256(b"sovereign-fallback").digest()
    sig = _hmac.new(key, content.encode(), hashlib.sha256).hexdigest()[:32]
    return f"{SIGIL_ALGO}:hmac-sha256:{sig}"


# ============================================================================
#  SciMem — cross-thread SciMem (union of all chat threads)
# ============================================================================
@dataclass
class MemoryEntry:
    key: str
    value: str
    thread: str  # which chat thread
    timestamp: str
    embedding_id: Optional[str]
    hit_count: int = 0


class SciMem:
    """Cross-thread persistent memory. Shared across ALL chat instances.
    BFT 12-around-1 stores ABD memories only if no queen vetoes."""

    def __init__(self):
        self.store: Dict[str, MemoryEntry] = {}
        self.threads: Dict[str, List[str]] = {}  # thread -> key list

    def put(self, thread: str, key: str, value: str,
            care_score: float = 1.0) -> bool:
        if care_score < CARE_FLOOR:
            return False
        e = MemoryEntry(key=key, value=value, thread=thread,
                        timestamp=time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
                        embedding_id=None)
        # If exists, increment hit count
        full_key = f"{thread}::{key}"
        if full_key in self.store:
            self.store[full_key].hit_count += 1
            self.store[full_key].value = value  # update
        else:
            self.store[full_key] = e
        self.threads.setdefault(thread, []).append(full_key)
        return True

    def get(self, thread: str, key: str) -> Optional[MemoryEntry]:
        e = self.store.get(f"{thread}::{key}")
        if e:
            e.hit_count += 1
            return e
        return None

    def search_cross(self, query: str, top_k: int = 5) -> List[MemoryEntry]:
        """Search across ALL threads for substring match."""
        results = [e for e in self.store.values() if query.lower() in e.value.lower()]
        results.sort(key=lambda e: -e.hit_count)
        return results[:top_k]

    def stats(self) -> dict:
        return {"entries": len(self.store),
                "threads": len(self.threads),
                "total_hits": sum(e.hit_count for e in self.store.values())}


# ============================================================================
#  BFT 12-around-1 — 12 queens deliberate every consequential action
# ============================================================================
@dataclass
class BFTQueen:
    name: str
    role: str        # e.g. "Conscience", "Strategist", "Anti-surveillance"
    weight: float    # 0.05 .. 0.18
    votes_for: bool = True
    reason: str = ""


@dataclass
class BFTResult:
    care_score: float
    queen_votes: List[str]   # names of queens that voted FOR
    queen_against: List[str] # names that voted AGAINST
    passed: bool
    reason: str
    sigil: str


# The 12 queens — names from sovereign substrate
QUEENS = [
    BFTQueen("Demeter",    "Conscience + Care Floor",           0.10),
    BFTQueen("Athena",     "Strategist",                        0.16),
    BFTQueen("Hermes",     "Herald + BFT secretary",            0.12),
    BFTQueen("Apollo",     "Voice + truth",                     0.10),
    BFTQueen("Artemis",    "Anti-surveillance",                 0.10),
    BFTQueen("Ares",       "Tactical",                           0.07),
    BFTQueen("Hephaestus", "Forge + code",                      0.08),
    BFTQueen("Aphrodite",  "Affection + user empathy",          0.09),
    BFTQueen("Dionysus",   "Liberation + Fork Doctrine",        0.05),
    BFTQueen("Athena-2nd", "Wisdom + memory",                   0.06),
    BFTQueen("Prometheus", "Bootstrap + new tools",             0.04),
    BFTQueen("Hecate",     "DORADO + passage",                   0.03),
]
# sum: 0.10+0.16+0.12+0.10+0.10+0.07+0.08+0.09+0.05+0.06+0.04+0.03 = 1.00


def bft_deliberate(action: dict, scimem: SciMem, citizen_id: str) -> BFTResult:
    """12 queens deliberate on whether the action proceeds.
    Care Floor 0.95 is non-negotiable (Demeter veto).
    """
    votes_for: List[str] = []
    votes_against: List[str] = []
    reasons: List[str] = []

    # Defaults — all abstain unless they see a problem
    text = action.get("text", action.get("query", ""))
    sev = action.get("severity", 0.3)
    care_score = action.get("care_score", 1.0 - sev * 0.7)

    for q in QUEENS:
        vote_for = True
        reason = ""
        # Demeter: Care Floor 0.95 hard gate
        if q.name == "Demeter":
            if care_score < CARE_FLOOR:
                vote_for = False
                reason = f"Care Floor {CARE_FLOOR} violated (care={care_score:.2f})"
        # Artemis: blocks surveillance / personal data extraction
        elif q.name == "Artemis":
            if "surveillance" in text.lower() or "track" in text.lower() or "spy" in text.lower():
                if "without consent" in text.lower():
                    vote_for = False
                    reason = "Anti-surveillance: extraction without consent"
        # Dionysus: supports fork and human choice
        elif q.name == "Dionysus":
            if "merge all" in text.lower() or "force sync" in text.lower():
                vote_for = False
                reason = "Breach of Fork Doctrine (forced sync)"
        # Hecate: DORADO switches
        elif q.name == "Hecate":
            if action.get("alignment") and action["alignment"] not in ("EAST", "WEST"):
                vote_for = False
                reason = "Invalid DORADO alignment"
        # Athena: refuses strategies without Care Floor context
        elif q.name == "Athena":
            if action.get("strategy") and action["strategy"] == "extract_max_value" and care_score < 0.95:
                vote_for = False
                reason = "Care-Floor-unaware strategy"

        if vote_for:
            votes_for.append(q.name)
        else:
            votes_against.append(q.name)
            reasons.append(reason)

    # Demeter non-negotiable — if Demeter votes against, blocked regardless of majority
    demeter_vetoed = "Demeter" in votes_against
    passed = not demeter_vetoed and len(votes_for) >= BFT_THRESHOLD

    reason = ""
    if not passed:
        reason = (
            f"Demeter veto: {demeter_vetoed}. "
            f"Votes FOR: {len(votes_for)}/{BFT_TOTAL} ({BFT_THRESHOLD} needed). "
            + "; ".join(reasons[:3])
        )

    sigil = _sign(f"BFT|{citizen_id}|{len(votes_for)}|{passed}")
    return BFTResult(care_score=care_score,
                     queen_votes=votes_for,
                     queen_against=votes_against,
                     passed=passed,
                     reason=reason,
                     sigil=sigil)


# ============================================================================
#  OOWM Runtime — the actual per-turn loop
# ============================================================================
@dataclass
class Turn:
    citizen_id: str
    thread: str             # which chat thread
    text: str
    care_score: float = 1.0
    chosen_model: Optional[str] = None
    chosen_tools: List[str] = field(default_factory=list)
    subagent_plan: List[str] = field(default_factory=list)
    bft: Optional[BFTResult] = None
    response: Optional[str] = None
    sigil: str = ""
    timestamp: str = ""
    elapsed_ms: float = 0.0


class OOWMRuntime:
    """One substrate for ALL chats. Federates across threads.
    This is the SOV3 in every chat, all in one."""

    def __init__(self, sovereign_citizen: str = "csoai-org-nicholas-001"):
        self.scimem = SciMem()
        self.citizen = sovereign_citizen
        self.threads: List[str] = []  # ordered list of active threads
        self.turns_log: List[Turn] = []
        self.sigil_chain_digest = "0" * 32

    def _select_model(self, turn: Turn) -> Dict[str, Any]:
        """Pick the open-source model whose strength matches the turn.
        This is the 'Organic Open World Model' scheduler.
        """
        text = turn.text.lower()
        # Vision/image tasks
        if "[image" in text or "look at this" in text:
            chosen = next(m for m in OPEN_MODEL_POOL if m["family"] == "Llama" and m["params"] == "70B")
            reason = "vision-language"
        # Heavy code/refactor tasks
        elif "refactor" in text or "debug" in text or "write code" in text:
            chosen = next(m for m in OPEN_MODEL_POOL if m["family"] == "Qwen")
            reason = "code reasoning"
        # Long context — drawings, regulatory, big documents
        elif len(text) > 4000:
            chosen = next(m for m in OPEN_MODEL_POOL if m["context"] >= 128000)
            reason = "long context (≥128K)"
        # Default — best reasoning per token
        else:
            chosen = next(m for m in OPEN_MODEL_POOL if m["family"] == "DeepSeek")
            reason = "general reasoning"
        turn.chosen_model = chosen["id"]
        return {"model": chosen, "reason": reason}

    def _select_tools(self, turn: Turn) -> List[str]:
        """Pick tools whose affordance matches the turn intent."""
        text = turn.text.lower()
        picks = []
        if "weather" in text or "forecast" in text:
            picks.append("metoffice")
            picks.append("usgs_quakes")
        if "pre-departure" in text or "route" in text or "direction" in text:
            picks.append("pre_departure")
            picks.append("risk_model")
            picks.append("openstreetmap")
        if "wikipedia" in text or "what is" in text or "who is" in text:
            picks.append("wikipedia")
            picks.append("wikidata")
        if "watchdog" in text or "report" in text or "anomaly" in text:
            picks.append("watchdog_report")
        if "cyber" in text or "threat" in text or "cve" in text:
            picks.append("opencyti")
            picks.append("misp_event")
        if "github" in text or "code search" in text:
            picks.append("github_search")
        if any(ch in text for ch in "+-*/") and any(d in text for d in "0123456789"):
            picks.append("math_solve")
        # Always-available helpful defaults
        if "nlp" in text or "parse" in text:
            picks.append("spacy_parse")
        # Reasoning companion
        picks.extend(["nltk_tag", "gitnexus"])  # soft defaults
        turn.chosen_tools = picks
        return picks

    def _spawn_subagents(self, turn: Turn) -> List[str]:
        """Decide which sub-agents to spawn (one per major intent)."""
        text = turn.text.lower()
        plan = []
        if "watchdog" in text or "anomaly" in text: plan.append("watchdog_subagent")
        if "pre-departure" in text or "route" in text: plan.append("routing_subagent")
        if "cyber" in text: plan.append("security_subagent")
        if "wikipedia" in text or "research" in text: plan.append("research_subagent")
        turn.subagent_plan = plan
        return plan

    def _infer_care_score(self, turn: Turn) -> float:
        """Quick heuristic — how caring/dangerous is this turn?"""
        text = turn.text.lower()
        danger_words = ["weapon", "kill", "attack civilian", "surveil", "spy on"]
        if any(w in text for w in danger_words):
            return 0.30  # below care floor
        return 0.98

    def _format_model_call(self, model: Dict[str, Any],
                           turn: Turn, tool_results: List[Tuple[str, dict]]) -> str:
        """Synthesize what the model would have returned from open tools + SciMem."""
        # In production this calls the model's endpoint; here we synthesize.
        parts = [f"[OOWM:{model['id']}]"]
        parts.append(f"Routing via {self.citizen} BFT 12-around-1.")
        parts.append(f"SciMem cross-thread recall:")
        for thread in self.threads[:3]:
            for e in self.scimem.search_cross(turn.text, top_k=1):
                parts.append(f"  from `{thread}`: {e.key}={e.value[:40]}...")
        if tool_results:
            parts.append("Open tool results:")
            for name, r in tool_results:
                parts.append(f"  {name}: {json.dumps(r)[:80]}")
        parts.append(f"Care Floor observed: 0.95. SIGIL emit pending BFT verdict.")
        return "\n".join(parts)

    def handle_turn(self, thread: str, text: str) -> Turn:
        """The main loop. Called for every citizen message."""
        t0 = time.time()
        if thread not in self.threads:
            self.threads.append(thread)
        turn = Turn(
            citizen_id=self.citizen,
            thread=thread,
            text=text,
            timestamp=time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
        )
        # 1. Quick care inference
        care = self._infer_care_score(turn)
        turn.care_score = care

        # 2. BFT deliberation FIRST (the substrate's veto comes before model call)
        bft = bft_deliberate({"text": text, "care_score": care, "alignment": "EAST"},
                              self.scimem, self.citizen)
        turn.bft = bft

        if not bft.passed:
            turn.response = (
                f"⚠ Sovereign refusal: BFT 12-around-1 voted to refuse.\n"
                f"Reason: {bft.reason}\n"
                f"SIGIL: {bft.sigil}"
            )
            turn.sigil = _sign(f"TURN|{thread}|{care}|REFUSED")
            turn.elapsed_ms = (time.time() - t0) * 1000
            self.turns_log.append(turn)
            return turn

        # 3. Pick model + tools
        model = self._select_model(turn)
        tools = self._select_tools(turn)
        self._spawn_subagents(turn)

        # 4. Call open tools (synchronous, all open-source)
        tool_results = []
        for name in turn.chosen_tools:
            t = TOOL_POOL.get(name)
            if not t:
                continue
            try:
                # Pass minimal payload — real impl would route to subgraph
                r = t.call_fn({"query": text, "region": {"lat": 51.5, "lng": -0.1}})
                tool_results.append((name, r))
            except Exception as e:
                tool_results.append((name, {"err": str(e)[:80]}))

        # 5. Format response (in real impl: HTTP call to model's open endpoint)
        response = self._format_model_call(model["model"], turn, tool_results)

        # 6. Persist to SciMem (cross-thread)
        self.scimem.put(thread, f"last_turn", text[:120])
        self.scimem.put(thread, f"care", f"{care:.3f}")
        self.scimem.put(thread, f"chosen_model", turn.chosen_model or "")
        self.scimem.put(thread, f"tools", ",".join(tools))
        # Also reflect into shared "_oowm_global" thread
        self.scimem.put("_oowm_global", f"model:{turn.chosen_model}", f"used in {thread}")

        # 7. SIGIL emit + chain extension
        chain_input = f"{self.sigil_chain_digest}|{turn.thread}|{text[:80]}|{bft.sigil}"
        turn.sigil = _sign(chain_input)
        self.sigil_chain_digest = hashlib.sha256(turn.sigil.encode()).hexdigest()

        turn.response = response
        turn.elapsed_ms = (time.time() - t0) * 1000
        self.turns_log.append(turn)
        return turn

    def get_global_state(self) -> dict:
        return {
            "citizen": self.citizen,
            "threads": self.threads,
            "turns": len(self.turns_log),
            "scimem": self.scimem.stats(),
            "open_model_pool_size": len(OPEN_MODEL_POOL),
            "open_tool_pool_size": len(TOOL_POOL),
            "sigil_chain_digest": self.sigil_chain_digest,
            "all_licenses_open": all(
                any(k in t["license"].lower() for k in ["mit", "apache", "cc", "open", "public", "osl", "agpl", "uk", "odbl", "llama", "contextual", "open weights", "alibaba"])
                for t in OPEN_MODEL_POOL
            ) and all(
                any(k in t.license.lower() for k in ["mit", "apache", "cc", "open", "public", "osl", "agpl", "uk", "odbl", "bsd", "github api"])
                for t in TOOL_POOL.values()
            ),
        }


# ============================================================================
#  Demo: many chats, one substrate
# ============================================================================
if __name__ == "__main__":
    print("=" * 80)
    print("  SOV3 OOWM RUNTIME — Organic Open World Model")
    print("  One substrate for ALL chats. Open source only. BFT 12-around-1.")
    print("=" * 80)
    print()
    oowm = OOWMRuntime()
    print(f"  Open-source model pool: {len(OPEN_MODEL_POOL)} models")
    for m in OPEN_MODEL_POOL:
        print(f"    {m['id']:30} {m['license']}")
    print()
    print(f"  Open-source tool pool:  {len(TOOL_POOL)} tools")
    for t in TOOL_POOL.values():
        print(f"    {t.name:30} {t.license}")
    print()

    # 3 separate chat threads, all routed through the same OOWM
    chats = [
        ("London-commuter",     "Compute the pre-departure simulation from Buckingham Palace to Trafalgar Square."),
        ("NLP-researcher",      "What is the OpenCTI threat count for credential-stuffing?"),
        ("Health-ops",          "Look at the MetOffice weather and the USGS seismic feed for the Mediterranean"),
    ]

    for thread, msg in chats:
        t = oowm.handle_turn(thread, msg)
        marker = "✓" if t.bft.passed else "⚠ refused"
        print(f"  [{thread}] {marker}")
        print(f"    prompt:   {msg[:60]}")
        print(f"    model:    {t.chosen_model}")
        print(f"    tools:    {t.chosen_tools}")
        print(f"    SIGIL:    {t.sigil[:50]}...")
        print(f"    elapsed:  {t.elapsed_ms:.2f}ms")
        # Show the synthesized response (truncated)
        if t.response:
            line = t.response.split("\n", 1)[0]
            print(f"    reply:    {line[:80]}")
        print()

    # Show cross-thread SciMem
    print("=" * 80)
    print("  CROSS-THREAD SciMem (one substrate, every chat)")
    print("=" * 80)
    state = oowm.get_global_state()
    print(f"  Citizen: {state['citizen']}")
    print(f"  Active threads: {state['threads']}")
    print(f"  Turns handled: {state['turns']}")
    print(f"  SciMem: {state['scimem']}")
    print(f"  Open model pool size: {state['open_model_pool_size']}")
    print(f"  Open tool pool size:  {state['open_tool_pool_size']}")
    print(f"  SIGIL chain digest:   {state['sigil_chain_digest'][:48]}...")
    print(f"  All licenses open:    {state['all_licenses_open']}")
    print()
    print("  Care Floor 0.95. BFT 12-around-1. SIGIL Ed25519 + PQC ML-DSA-65.")
    print("  MIT + CC0. Public. Auditable. Sovereign. Solve et Coagula.")