/* @jsxRuntime classic */ /* @jsx React.createElement */ // Orchestra — The Bench (Bench Phase-2, task 86banqh7q) // Per-Wright operating memory surface. Read-only by default; // Approve/Edit/Dismiss shown when the "advanced" toggle is on. // Loaded by index.html as a separate text/babel script; exposes window.AgentContextPage. (function () { const { useState, useEffect, useCallback } = React; // ── API helper (mirrors app.jsx's api()) ────────────────────────────────── function toCamel(s) { return s.replace(/_([a-z])/g, (_, c) => c.toUpperCase()); } function deepCamel(obj) { if (Array.isArray(obj)) return obj.map(deepCamel); if (obj && typeof obj === 'object') { const out = {}; for (const k of Object.keys(obj)) out[toCamel(k)] = deepCamel(obj[k]); return out; } return obj; } async function api(method, path, body) { const opts = { method, headers: { 'Content-Type': 'application/json' } }; if (body !== undefined) opts.body = JSON.stringify(body); const res = await fetch(path, opts); if (!res.ok) { const t = await res.text().catch(() => ''); throw new Error(`${res.status}: ${t}`); } if (res.status === 204) return null; return deepCamel(await res.json()); } // ── Lineage chip text for a learned item ────────────────────────────────── function lineageText(item) { if (item.provenance === 'kb-propagation') return 'Propagated from Knowledge Base'; if (item.provenance === 'human') return 'Human-authored'; const runPart = item.sourceRunId ? `Build #${item.sourceRunId}` : 'a Build'; const stagePart = item.sourceStage ? ` · ${item.sourceStage}` : ''; const reviewerPart = item.sourceReviewer ? ` → ${item.sourceReviewer} review gate` : ''; return `Learned from ${runPart}${stagePart}${reviewerPart}`; } // ── Category label prettifier ───────────────────────────────────────────── function catLabel(slug) { return (slug || 'general').replace(/_/g, ' ').replace(/\b\w/g, c => c.toUpperCase()); } // ── Confidence bar ──────────────────────────────────────────────────────── function ConfBar({ value }) { const pct = Math.round((value || 0) * 100); const color = pct >= 70 ? '#4ade80' : pct >= 40 ? '#c8a84a' : '#e08080'; return (
); } // ── Single Bench item card ──────────────────────────────────────────────── function ItemCard({ item, advanced, onApprove, onDismiss }) { const isPinned = item.itemType === 'pinned'; return (
{isPinned ? '📌 Pinned' : '💡 Learned'} {item.category && ( {catLabel(item.category)} )} {!isPinned && ( )}
{item.content}
{!isPinned && (
{lineageText(item)}
)} {advanced && (
{!isPinned && ( )}
)}
); } // ── Main page component ──────────────────────────────────────────────────── function AgentContextPage({ agents }) { const [summaries, setSummaries] = useState({}); // agent_id → summary const [selAgentId, setSelAgentId] = useState(null); const [items, setItems] = useState([]); const [loadingItems, setLoadingItems] = useState(false); const [advanced, setAdvanced] = useState(false); const [reconcileResult, setReconcileResult] = useState(null); const [reconciling, setReconciling] = useState(false); const [toast, setToast] = useState(null); useEffect(() => { injectStyles(); }, []); // Fetch all bench summaries on mount useEffect(() => { api('GET', '/api/bench').then(data => { const map = {}; (data.summaries || []).forEach(s => { map[s.agentId] = s; }); setSummaries(map); }).catch(() => {}); }, []); // Fetch items when a Wright is selected const selectAgent = useCallback((agentId) => { if (selAgentId === agentId) { setSelAgentId(null); setItems([]); return; } setSelAgentId(agentId); setItems([]); setLoadingItems(true); api('GET', `/api/bench/${agentId}`) .then(data => setItems(data.items || [])) .catch(() => setItems([])) .finally(() => setLoadingItems(false)); }, [selAgentId]); const showToast = (msg, ok = true) => { setToast({ msg, ok }); setTimeout(() => setToast(null), 3500); }; const handleReconcile = async () => { setReconciling(true); try { const r = await api('POST', '/api/bench/reconcile'); setReconcileResult(r); showToast(`Reconciled: ${r.archived} items archived, ${r.estimatedTokensSaved} tokens saved`); // Refresh summaries const data = await api('GET', '/api/bench'); const map = {}; (data.summaries || []).forEach(s => { map[s.agentId] = s; }); setSummaries(map); } catch (e) { showToast('Reconcile failed: ' + e.message, false); } finally { setReconciling(false); } }; const handleApprove = async (itemId) => { try { const r = await api('PATCH', `/api/bench/items/${itemId}/approve`); if (r.ok) { setItems(prev => prev.map(i => i.id === itemId ? r.item : i)); showToast('Item approved and pinned.'); } } catch (e) { showToast('Approve failed: ' + e.message, false); } }; const handleDismiss = async (itemId) => { try { const r = await api('DELETE', `/api/bench/items/${itemId}`); if (r.ok) { setItems(prev => prev.filter(i => i.id !== itemId)); showToast('Item dismissed.'); } } catch (e) { showToast('Dismiss failed: ' + e.message, false); } }; const visibleAgents = (agents || []).filter(a => !a.meta); const metaAgents = (agents || []).filter(a => a.meta); const pinnedItems = items.filter(i => i.itemType === 'pinned'); const learnedItems = items.filter(i => i.itemType === 'learned'); const selAgent = selAgentId ? (agents || []).find(a => a.id === selAgentId) : null; return (
{/* Header */}
The Bench
Per-Wright operating memory — Pinned (human-set) and Learned (auto-captured at review gates). Select a Wright to inspect its context.
{reconcileResult && (
Last reconcile: {reconcileResult.archived} items archived ·{' '} ~{reconcileResult.estimatedTokensSaved} tokens saved ·{' '} {reconcileResult.remainingItems} active items remaining
)}
{/* Left rail — Wright list */}
Wrights
{visibleAgents.map(agent => { const s = summaries[agent.id]; return (
selectAgent(agent.id)}>
{agent.initials || (agent.role || '').slice(0, 2).toUpperCase()}
{agent.role}
{s ? (
📌 {s.pinnedCount}  💡 {s.learnedCount}
) : (
No bench items
)}
); })} {metaAgents.length > 0 && ( <>
System
{metaAgents.map(agent => { const s = summaries[agent.id]; return (
selectAgent(agent.id)}>
{agent.initials || (agent.role || '').slice(0, 2).toUpperCase()}
{agent.role}
{s ? (
📌 {s.pinnedCount}  💡 {s.learnedCount}
) : (
No bench items
)}
); })} )}
{/* Main panel */}
{!selAgentId ? (
🧠
Select a Wright
Choose a Wright from the left rail to inspect its Pinned and Learned context.
) : loadingItems ? (
Loading bench…
) : (
{selAgent ? selAgent.role : selAgentId} — The Bench
📌 {pinnedItems.length} Pinned 💡 {learnedItems.length} Learned
{items.length === 0 ? (
📭
No bench items yet
Items appear here after Builds complete review gates, or when you pin a note manually (Advanced mode).
{advanced && (
setItems(prev => [item, ...prev])} showToast={showToast} />
)}
) : ( <> {pinnedItems.length > 0 && (
📌 Pinned
Human-authored context. Confidence stays at 100% and is never decayed.
{pinnedItems.map(item => ( ))}
)} {learnedItems.length > 0 && (
💡 Learned
Auto-captured at review gates. Confidence decays over time; low-confidence items are archived on reconcile.
{learnedItems.map(item => ( ))}
)} {advanced && (
setItems(prev => [item, ...prev])} showToast={showToast} />
)} )}
)}
{/* Toast */} {toast && (
{toast.msg}
)}
); } // ── Pin form (shown in advanced mode) ────────────────────────────────────── function PinForm({ agentId, onPinned, showToast }) { const [content, setContent] = useState(''); const [category, setCategory] = useState(''); const [saving, setSaving] = useState(false); const handlePin = async () => { if (!content.trim()) return; setSaving(true); try { const r = await (async () => { const opts = { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ content: content.trim(), category: category.trim() }) }; const res = await fetch(`/api/bench/${agentId}/pin`, opts); if (!res.ok) throw new Error(await res.text().catch(() => '')); return await res.json(); })(); if (r.ok && r.item) { const item = r.item; // convert snake_case to camelCase manually for the pinned item function toCamel(s) { return s.replace(/_([a-z])/g, (_, c) => c.toUpperCase()); } function dc(o) { if (Array.isArray(o)) return o.map(dc); if (o && typeof o === 'object') { const out = {}; for (const k of Object.keys(o)) out[toCamel(k)] = dc(o[k]); return out; } return o; } onPinned(dc(item)); setContent(''); setCategory(''); showToast('Item pinned.'); } else { showToast(r.message || 'Pin failed', false); } } catch (e) { showToast('Pin failed: ' + e.message, false); } finally { setSaving(false); } }; return (
+ Pin a note