""" 🎛️ 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