/** * Conduit /brain — Amaru Brain panel. * * Live state of all 7 chakra kernels (root → crown): * – Last evaluation (output / error / stub status / receipt seq) per chakra * – Fire a manual scheduler tick and watch the receipt chain extend * – Static SVG of the chakana wiring (Andean cross + ouroboros closure) * – Live tripwire (huklla-10) status * * Data sources: * – GET /amaru/state — per-chakra last evaluation snapshot (polled) * – GET /amaru/tripwires — huklla-10 status (polled) * – POST /amaru/scheduler/tick — manual tick * * The page subscribes to /amaru/events (SSE) for `amaru.chakra` and * `amaru.scheduler` envelopes — the exact topic names Amaru publishes to the * yawar-bus (Prism Bus). A 2-second poll on /amaru/state is kept as a * fallback for when the SSE connection drops (proxies, restarts, etc.). */ import { useCallback, useEffect, useMemo, useState } from 'react'; import { Brain } from 'lucide-react'; const AMARU_BASE = '/api/amaru'; const CHAKRAS = ['root', 'sacral', 'solar', 'heart', 'throat', 'third_eye', 'crown'] as const; type ChakraName = (typeof CHAKRAS)[number]; const CHAKRA_LABELS: Record = { root: 'Root · muladhara', sacral: 'Sacral · svadhisthana', solar: 'Solar · manipura', heart: 'Heart · anahata', throat: 'Throat · vishuddha', third_eye: 'Third-Eye · ajna', crown: 'Crown · sahasrara', }; interface ChakraEvaluation { chakra: string; output: Record | null; error: string | null; stubbed: boolean; receipt?: { seq?: number }; via?: string; tick_id?: number; } interface RuntimeState { chakras: ChakraName[]; last_evaluation: Record; scheduler_ticks: number; receipts: number; bus: { publishes: number; failures: number }; } interface TripwireRow { id: string; title: string; status: 'pass' | 'warn' | 'trip'; detail: string; } interface TripwireResponse { summary: { pass: number; warn: number; trip: number; total: number }; tripwires: TripwireRow[]; } interface TickStep { chakra: string; output: Record | null; error: string | null; stubbed: boolean; receipt_seq: number; } interface TickResult { tick_id: number; steps: TickStep[]; closure: number | null; handoff: { to: string; via: string } | null; } async function safeFetch(url: string, init?: RequestInit): Promise { try { const r = await fetch(url, init); if (!r.ok) return null; return (await r.json()) as T; } catch { return null; } } function statusBadge(status: 'pass' | 'warn' | 'trip') { const palette: Record = { pass: { bg: 'rgba(201,183,135,0.12)', fg: '#c9b787', label: 'PASS' }, warn: { bg: 'rgba(245,245,245,0.10)', fg: '#f5f5f5', label: 'WARN' }, trip: { bg: 'rgba(245,80,80,0.14)', fg: '#ff8a8a', label: 'TRIP' }, }; const p = palette[status]; return ( {p.label} ); } function ChakraCard({ name, evaluation, }: { name: ChakraName; evaluation: ChakraEvaluation | null; }) { const stub = evaluation?.stubbed; const error = evaluation?.error; const output = evaluation?.output; const seq = evaluation?.receipt?.seq; return (
{CHAKRA_LABELS[name]}
{stub ? ( STUBBED · upstream not vendored ) : evaluation ? ( {error ? 'ERROR' : 'OK'} ) : ( idle )}
{error && (
{error}
)} {output && (
          {JSON.stringify(output, null, 2)}
        
)}
receipt seq: {seq ?? '—'} {evaluation?.tick_id ? ` · tick ${evaluation.tick_id}` : ''}
); } function ChakanaWiringSvg() { // Static topology — chakana ascent root→crown + ouroboros closure crown→root. const cx = 280; const top = 40; const dy = 56; const order: ChakraName[] = [...CHAKRAS]; return ( {order.map((name, i) => { const y = top + i * dy; return ( {i + 1} {CHAKRA_LABELS[name]} {i < order.length - 1 && ( )} ); })} {/* Ouroboros closure: crown → root */} ouroboros ); } export default function BrainPage() { const [state, setState] = useState(null); const [tripwires, setTripwires] = useState(null); const [tickHistory, setTickHistory] = useState([]); const [ticking, setTicking] = useState(false); const [runtimeUp, setRuntimeUp] = useState(null); const [sseOpen, setSseOpen] = useState(false); const refresh = useCallback(async () => { const [s, t] = await Promise.all([ safeFetch(`${AMARU_BASE}/state`), safeFetch(`${AMARU_BASE}/tripwires`), ]); if (s) setState(s); if (t) setTripwires(t); setRuntimeUp(s !== null || t !== null); }, []); useEffect(() => { void refresh(); const id = setInterval(refresh, 2000); return () => clearInterval(id); }, [refresh]); // Subscribe to Amaru's SSE stream so chakra/tripwire state updates push, // not poll. /amaru/events emits exact topic names: amaru.chakra (per // evaluation) and amaru.scheduler (per tick). The polling above is kept // as a fallback for when this stream is unavailable. useEffect(() => { const url = `${AMARU_BASE}/events`; let es: EventSource | null = null; try { es = new EventSource(url); } catch { return; } es.addEventListener('hello', () => setSseOpen(true)); es.addEventListener('amaru.chakra', (e) => { try { const env = JSON.parse((e as MessageEvent).data) as { payload?: ChakraEvaluation & { chakra?: string }; }; const ev = env.payload; if (!ev?.chakra) return; setState((prev) => prev ? { ...prev, last_evaluation: { ...prev.last_evaluation, [ev.chakra as ChakraName]: ev }, receipts: Math.max(prev.receipts, ev.receipt?.seq ?? prev.receipts), } : prev, ); } catch { /* ignore malformed frame */ } }); es.addEventListener('amaru.scheduler', (e) => { try { const env = JSON.parse((e as MessageEvent).data) as { payload?: TickResult }; const tick = env.payload; if (!tick?.tick_id) return; setTickHistory((h) => { if (h[0]?.tick_id === tick.tick_id) return h; return [tick, ...h].slice(0, 8); }); } catch { /* ignore */ } }); es.onerror = () => setSseOpen(false); return () => { es?.close(); setSseOpen(false); }; }, []); const tick = useCallback(async () => { setTicking(true); const r = await safeFetch(`${AMARU_BASE}/scheduler/tick`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ envelope: { signals: { grounded: 0.85, integrity: 0.9, novelty: 0.55, fluency: 0.78, care: 0.82, harm: 0.12, clarity: 0.84, truth: 0.88, }, }, }), }); if (r) setTickHistory((h) => [r, ...h].slice(0, 8)); setTicking(false); void refresh(); }, [refresh]); const headSeq = state?.receipts ?? 0; const ticks = state?.scheduler_ticks ?? 0; const busOk = state ? state.bus.publishes - state.bus.failures : 0; const sortedChakras = useMemo(() => CHAKRAS, []); return (
AMARU BRAIN {runtimeUp === null ? 'CHECKING' : runtimeUp ? 'RUNTIME UP' : 'RUNTIME DOWN'} {sseOpen ? 'SSE LIVE' : 'POLL FALLBACK'}

7-chakra kernels · live runtime

Andean Ouroboros · root → crown · chakana wiring · huklla-10 tripwires

{runtimeUp === false && (
Amaru runtime is not reachable on /amaru/*. Start the artifacts/api-server: amaru workflow to bring it up (autoStart is intentionally false per task #5176).
)}
Scheduler ticks
{ticks}
Receipts in chain
{headSeq}
Yawar bus ok
{busOk}
Tripwires
{tripwires ? `${tripwires.summary.pass}/${tripwires.summary.total}` : '—'}
{sortedChakras.map((name) => ( ))}
Chakana wiring
huklla-10 tripwires {tripwires && ( pass {tripwires.summary.pass} · warn {tripwires.summary.warn} · trip {tripwires.summary.trip} )}
{(tripwires?.tripwires ?? []).map((t) => (
{statusBadge(t.status)} {t.title}
{t.detail}
))} {!tripwires && (
No tripwire data yet.
)}
Recent scheduler ticks (newest first)
{tickHistory.length === 0 && (
Fire a tick to extend the receipt chain.
)}
{tickHistory.map((tick) => (
tick #{tick.tick_id} closure {tick.closure?.toFixed(3) ?? '—'} · receipts seq {tick.steps[0]?.receipt_seq}–{tick.steps.at(-1)?.receipt_seq}
{tick.steps.map((s) => ( {s.chakra}#{s.receipt_seq} ))}
))}
); }