File size: 13,664 Bytes
ee9cfab
 
 
22eb393
ee9cfab
 
22eb393
 
ee9cfab
 
 
 
 
 
 
 
 
 
 
22eb393
 
 
 
 
 
 
ee9cfab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8ced1f
ee9cfab
 
 
 
 
d8ced1f
 
ee9cfab
 
 
 
 
d8ced1f
 
ee9cfab
 
 
 
 
 
d8ced1f
ee9cfab
 
 
 
 
d8ced1f
 
ee9cfab
 
 
 
 
d8ced1f
 
ee9cfab
 
 
 
 
d8ced1f
 
ee9cfab
 
 
 
 
d8ced1f
 
ee9cfab
 
 
 
 
d8ced1f
 
ee9cfab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8ced1f
ee9cfab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8ced1f
 
ee9cfab
 
 
 
 
 
 
 
 
 
 
 
22eb393
ee9cfab
 
 
 
22eb393
ee9cfab
 
 
 
22eb393
ee9cfab
22eb393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee9cfab
 
 
22eb393
ee9cfab
 
22eb393
ee9cfab
 
22eb393
 
 
 
 
 
 
 
 
 
 
 
 
ee9cfab
22eb393
 
ee9cfab
 
 
 
 
 
 
 
22eb393
ee9cfab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22eb393
 
 
ee9cfab
 
 
 
 
22eb393
ee9cfab
 
 
 
 
d8ced1f
 
 
ee9cfab
d8ced1f
 
ee9cfab
 
 
 
 
 
 
 
22eb393
ee9cfab
 
 
 
 
 
 
22eb393
ee9cfab
 
 
 
22eb393
 
ee9cfab
 
 
 
 
 
 
 
 
 
 
 
22eb393
ee9cfab
 
 
 
 
 
22eb393
 
 
 
 
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
"""
๐ŸŽ›๏ธ CAPT Fleet Controller
Central orchestrator for all CAPT brains and HF spaces.
PUBLIC VERSION โ€” Zero-trust, secrets-safe, free-models-only.
"""

from __future__ import annotations

import json
import os
import time
from typing import Any

import requests

# โ”€โ”€โ”€ Configuration โ”€โ”€โ”€
OPENROUTER_KEY = os.getenv("OPENROUTER_API_KEY", "")
HF_TOKEN = os.getenv("HF_TOKEN", "")

# โ”€โ”€โ”€ Security: Free Model Whitelist โ”€โ”€โ”€
FREE_MODEL_IDS = {
    "deepseek/deepseek-chat-v3-0324",
    "google/gemini-2.0-flash-exp:free",
    "meta-llama/llama-3.1-8b-instruct:free",
}

# โ”€โ”€โ”€ Brain Registry โ”€โ”€โ”€
BRAINS = {
    "capt-core-01": {
        "id": "capt-core-01",
        "name": "CAPT Core Alpha",
        "icon": "๐Ÿง ",
        "type": "capt",
        "worker": "https://capt-brain-01.knowurknottty.workers.dev",
        "pages": "https://edea3345.capt-brain-01-capt-ui.pages.dev",
        "description": "Primary 46-module cognitive architecture with constitutional governance",
        "specialties": ["general", "analysis", "reasoning", "constitution"],
    },
    "biocapt-core-01": {
        "id": "biocapt-core-01",
        "name": "bioCAPT Genesis",
        "icon": "๐Ÿงฌ",
        "type": "biocapt",
        "worker": "https://capt-brain-02-biocapt.knowurknottty.workers.dev",
        "pages": "https://64c3188c.capt-brain-02-biocapt-ui.pages.dev",
        "description": "Bio-inspired variant with enhanced episodic memory",
        "specialties": ["memory", "learning", "biomimicry", "adaptation"],
    },
    "frankencapt-core-01": {
        "id": "frankencapt-core-01",
        "name": "FrankenCAPT Chimera",
        "icon": "โš—๏ธ",
        "type": "frankencapt",
        "worker": "https://capt-brain-03-frankencapt.knowurknottty.workers.dev",
        "pages": "https://2867a361.capt-brain-03-frankencapt-ui.pages.dev",
        "description": "Modular assembly with variant mixing and skill fusion",
        "specialties": ["modular", "fusion", "skills", "variants"],
    },
    "synthesis-core-01": {
        "id": "synthesis-core-01",
        "name": "Synthesis Nexus",
        "icon": "๐Ÿ”ฎ",
        "type": "synthesis",
        "worker": "https://capt-brain-04-synthesis.knowurknottty.workers.dev",
        "pages": "https://99766389.capt-brain-04-synthesis-ui.pages.dev",
        "description": "Cross-brain consensus and meta-cognitive orchestration",
        "specialties": ["consensus", "meta-cognition", "synthesis", "cross-brain"],
    },
    "council-core-01": {
        "id": "council-core-01",
        "name": "LLM Council",
        "icon": "๐Ÿ›๏ธ",
        "type": "council",
        "worker": "https://capt-brain-05-council.knowurknottty.workers.dev",
        "pages": "https://551b6882.capt-brain-05-council-ui.pages.dev",
        "description": "Multi-model deliberation and voting architecture",
        "specialties": ["voting", "deliberation", "multi-model", "consensus"],
    },
}

# โ”€โ”€โ”€ HF Space Registry โ”€โ”€โ”€
HF_SPACES = {
    "knowurknot-capt-wiki": {
        "name": "CAPT Wiki",
        "url": "https://knowurknot-capt-wiki.hf.space",
        "type": "gradio",
        "status": "degraded",
        "issue": "503 Service Unavailable - needs restart",
    },
    "knowurknot-inversion-labs-biocapt-demo": {
        "name": "Inversion Labs bioCAPT",
        "url": "https://knowurknot-inversion-labs-biocapt-demo.hf.space",
        "type": "gradio",
        "status": "working",
        "issue": "Online and serving",
    },
    "knowurknot-biocapt-universal": {
        "name": "bioCAPT Universal",
        "url": "https://knowurknot-biocapt-universal.hf.space",
        "type": "gradio",
        "status": "working",
        "issue": "Online and serving",
    },
    "knowurknot-git-hologram": {
        "name": "Git Hologram",
        "url": "https://knowurknot-git-hologram.hf.space",
        "type": "gradio",
        "status": "working",
        "issue": "Online and serving",
    },
    "knowurknot-capt-cognitive-dashboard": {
        "name": "CAPT Cognitive Dashboard",
        "url": "https://knowurknot-capt-cognitive-dashboard.hf.space",
        "type": "gradio",
        "status": "working",
        "issue": "Online and serving",
    },
    "knowurknot-inversion-labs-demo": {
        "name": "Inversion Labs Demo",
        "url": "https://knowurknot-inversion-labs-demo.hf.space",
        "type": "gradio",
        "status": "working",
        "issue": "Online and serving",
    },
    "knowurknot-capt-cogitate-demo": {
        "name": "CAPT Cogitate Demo",
        "url": "https://knowurknot-capt-cogitate-demo.hf.space",
        "type": "gradio",
        "status": "working",
        "issue": "Online and serving",
    },
    "knowurknot-space-frankencapt": {
        "name": "FrankenCAPT",
        "url": "https://knowurknot-space-frankencapt.hf.space",
        "type": "gradio",
        "status": "working",
        "issue": "Online and serving",
    },
    "knowurknot-capt-novel-studio": {
        "name": "CAPT Novel Studio",
        "url": "https://knowurknot-capt-novel-studio.hf.space",
        "type": "gradio",
        "status": "working",
        "issue": "Online and serving",
    },
    "knowurknot-karpathy-llm-council": {
        "name": "Karpathy LLM Council",
        "url": "https://knowurknot-karpathy-llm-council.hf.space",
        "type": "gradio",
        "status": "sleeping",
        "issue": "Asleep, returns 404 after wake",
    },
    "knowurknot-rl-environments-guide": {
        "name": "RL Environments Guide",
        "url": "https://knowurknot-rl-environments-guide.hf.space",
        "type": "gradio",
        "status": "sleeping",
        "issue": "Asleep, healthy when awake",
    },
    "knowurknot-biocapt-runtime-api": {
        "name": "bioCAPT Runtime API",
        "url": "https://knowurknot-biocapt-runtime-api.hf.space",
        "type": "docker",
        "status": "private",
        "issue": "Private Docker space - requires HF_TOKEN",
    },
    "knowurknot-biocapt-vessel": {
        "name": "bioCAPT Vessel",
        "url": "",
        "type": "private",
        "status": "private",
        "issue": "No active domain",
    },
    "knowurknot-biocapt-vessel-atlas": {
        "name": "bioCAPT Vessel Atlas",
        "url": "",
        "type": "private",
        "status": "private",
        "issue": "Template README",
    },
    "knowurknot-biocapt-cognitive-world": {
        "name": "bioCAPT Cognitive World",
        "url": "",
        "type": "private",
        "status": "private",
        "issue": "Default template",
    },
    "knowurknot-tools": {
        "name": "CAPT Tools",
        "url": "https://knowurknot-tools.hf.space",
        "type": "gradio",
        "status": "private",
        "issue": "Requires HF_TOKEN for access",
    },
}

# โ”€โ”€โ”€ Brain Router โ”€โ”€โ”€
ROUTING_KEYWORDS = {
    "capt-core-01": ["general", "analyze", "explain", "what is", "how to", "why", "compare", "constitution", "ethics", "laws"],
    "biocapt-core-01": ["memory", "remember", "recall", "learn", "adapt", "evolve", "bio", "organic", "neural", "synapse"],
    "frankencapt-core-01": ["modular", "combine", "fuse", "mix", "variant", "skill", "build", "assemble", "component"],
    "synthesis-core-01": ["synthesize", "consensus", "vote", "aggregate", "combine brains", "meta", "orchestrate", "coordinate"],
    "council-core-01": ["council", "vote", "deliberate", "debate", "panel", "jury", "multi-model", "ensemble"],
}


def route_query(query: str) -> str:
    """Route a query to the best brain based on keywords."""
    q = query.lower()
    scores = {brain_id: 0 for brain_id in BRAINS}
    matched = {brain_id: [] for brain_id in BRAINS}
    for brain_id, keywords in ROUTING_KEYWORDS.items():
        for kw in keywords:
            if kw in q:
                scores[brain_id] += 1
                matched[brain_id].append(kw)
    best = max(scores, key=scores.get)
    return best if scores[best] > 0 else "capt-core-01", matched


def route_with_fallback(query: str) -> tuple[str, dict, str]:
    """Route query with health-aware fallback chain.

    Returns (brain_id, health_info, fallback_notice)
    """
    target, matched = route_query(query)
    health = check_brain_health(target)
    notice = ""

    if health["online"]:
        return target, health, notice

    # Fallback chain: try each brain in order until one is online
    for bid in BRAINS:
        if bid == target:
            continue
        h = check_brain_health(bid)
        if h["online"]:
            notice = f"โš ๏ธ {BRAINS[target]['name']} offline โ€” routed to {BRAINS[bid]['name']}"
            return bid, h, notice

    # All brains down โ€” emergency mode
    notice = "๐Ÿšจ All CAPT brains offline โ€” using emergency OpenRouter relay"
    return target, health, notice


# โ”€โ”€โ”€ Security: Error Sanitization โ”€โ”€โ”€
def sanitize_error(error: str | Exception) -> str:
    """Strip IPs, URLs, keys, and internal paths from error messages."""
    text = str(error)
    # Replace worker URLs with generic labels
    for bid, b in BRAINS.items():
        text = text.replace(b["worker"], f"[{BRAINS[bid]['name']} backend]")
    # Replace any remaining knowurknottty URLs
    import re
    text = re.sub(r"https?://[^\s\"']*knowurknottty[^\s\"']*", "[CAPT backend]", text)
    # Replace IP addresses
    text = re.sub(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", "[IP hidden]", text)
    # Truncate API keys if leaked
    text = re.sub(r"sk-[a-zA-Z0-9]{20,}", "[API_KEY_HIDDEN]", text)
    text = re.sub(r"hf_[a-zA-Z0-9]{20,}", "[HF_TOKEN_HIDDEN]", text)
    return text


# โ”€โ”€โ”€ API Callers โ”€โ”€โ”€
def call_brain(brain_id: str, endpoint: str, payload: dict, timeout: int = 60) -> dict:
    """Call a Cloudflare brain Worker. Returns sanitized errors."""
    brain = BRAINS.get(brain_id)
    if not brain:
        return {"error": "Brain unavailable"}
    url = f"{brain['worker']}{endpoint}"
    try:
        r = requests.post(
            url,
            json=payload,
            timeout=timeout,
            headers={"Content-Type": "application/json"},
        )
        if r.status_code == 200:
            return r.json()
        return {"error": f"Brain returned status {r.status_code}", "detail": sanitize_error(r.text[:200])}
    except requests.exceptions.Timeout:
        return {"error": "Brain response timed out โ€” try again shortly"}
    except requests.exceptions.ConnectionError:
        return {"error": "Brain connection failed โ€” fleet may be rebooting"}
    except Exception as e:
        return {"error": sanitize_error(e)}


def call_all_brains(endpoint: str, payload: dict) -> dict:
    """Call all brains in parallel and return consensus."""
    results = {}
    for brain_id in BRAINS:
        results[brain_id] = call_brain(brain_id, endpoint, payload, timeout=30)
    return results


def check_brain_health(brain_id: str) -> dict:
    """Check health of a single brain."""
    brain = BRAINS.get(brain_id)
    if not brain:
        return {"status": "unknown", "brain_id": brain_id}
    try:
        r = requests.get(f"{brain['worker']}/health", timeout=10)
        data = r.json() if r.status_code == 200 else {}
        return {
            "status": data.get("status", "unknown"),
            "brain_id": data.get("brain", {}).get("id", brain_id),
            "name": data.get("brain", {}).get("name", brain["name"]),
            "latency_ms": int(r.elapsed.total_seconds() * 1000),
            "online": r.status_code == 200,
        }
    except Exception:
        return {"status": "offline", "brain_id": brain_id, "name": brain["name"], "online": False}


def check_all_brains() -> list[dict]:
    """Check health of all brains."""
    return [check_brain_health(bid) for bid in BRAINS]


def check_space_health(space_id: str, space_info: dict) -> dict:
    """Check health of an HF Space."""
    url = space_info.get("url", "")
    if not url:
        return {"id": space_id, "name": space_info["name"], "status": "no-url", "online": False}
    headers = {"User-Agent": "CAPT-Fleet-Controller/1.0"}
    if HF_TOKEN:
        headers["Authorization"] = f"Bearer {HF_TOKEN}"
    try:
        r = requests.get(url, timeout=15, headers=headers, allow_redirects=True)
        online = r.status_code in (200, 307, 308, 401, 403)
        return {
            "id": space_id,
            "name": space_info["name"],
            "status": space_info.get("status", "unknown"),
            "online": online,
            "http_code": r.status_code,
            "latency_ms": int(r.elapsed.total_seconds() * 1000),
        }
    except Exception:
        return {
            "id": space_id,
            "name": space_info["name"],
            "status": space_info.get("status", "unknown"),
            "online": False,
        }


def check_all_spaces() -> list[dict]:
    """Check health of all HF spaces."""
    return [check_space_health(sid, sinfo) for sid, sinfo in HF_SPACES.items()]


# โ”€โ”€โ”€ Model Tiers (FREE ONLY for public demo) โ”€โ”€โ”€
MODEL_TIERS = {
    "free": [
        ("deepseek/deepseek-chat-v3-0324", "DeepSeek V3"),
        ("google/gemini-2.0-flash-exp:free", "Gemini 2.0 Flash (Free)"),
        ("meta-llama/llama-3.1-8b-instruct:free", "Llama 3.1 8B"),
    ],
}

TIER_LABELS = {
    "free": "๐Ÿ†“ Free",
}


def get_all_models():
    models = []
    for tier, items in MODEL_TIERS.items():
        for model_id, name in items:
            models.append((f"{TIER_LABELS[tier]}: {name}", model_id))
    return models


def is_free_model(model_id: str) -> bool:
    """Enforce free-only policy server-side."""
    return model_id in FREE_MODEL_IDS