Spaces:
Paused
Fix GRPO correctness + scale config for L40S
Browse files- Reward function now restores per-prompt env snapshot before scoring,
so each completion is judged against the state its prompt described
(not whatever env._state happened to be). Fixes silent learning-signal
decoupling that was wrecking previous runs.
- Add independent format-quality reward as second reward_func, per
hackathon guide's "use multiple independent reward signals" advice.
- Trim state_to_prompt from ~700-1000 to ~150-200 tokens (~4x faster
generation, no info loss).
- Scale defaults for L40S (was T4): Qwen2.5-1.5B, num_generations=8,
per_device_batch=2, max_steps=60, max_completion_length=192, bf16=True,
prompt_budget=200. All env-var overridable.
- Add preflight reward-variance check before training (aborts loudly
if all rewards identical = no learning signal).
- Wire real LLM into /api/compare so 'VERGIL-Trained' arm actually runs
the trained model when loaded; falls back to heuristic with clear label.
- _validate_action: only enforce target-node existence for actions that
use a target. Fixes the DO_NOTHING-with-stale-target infinite warning loop.
- parse_llm_output: coerces target to a valid pending node_id; falls back
to DO_NOTHING when LLM hallucinates a stakeholder id as target.
- Save + push model BEFORE post-training eval so a sleeping Space can't
cost us the trained adapter. Eval is now strictly optional with
SKIP_EVAL/EVAL_EPISODES/EVAL_TIMEOUT_SEC env knobs.
- Promote GPU Dockerfile to repo root (was vergil-training-space-fix/);
keep CPU Dockerfile as Dockerfile.demo for later demo Space.
- Add .dockerignore + extend .gitignore (.env, *.log) so secrets and
noise never reach the Space build.
Made-with: Cursor
Files changed (11)
- .dockerignoreAdded+19
- .gitignore+4-1
- Dockerfile+18-8
- Dockerfile.demoAdded+13
- frontend/app.js+323-271
- frontend/index.html+82-61
- frontend/style.css+589-617
- scripts/train_grpo_colab.py+500-279
- vergil-training-space-fix/DockerfileDeleted-23
- vergil/api/server.py+14-2
- vergil/core/env.py+32-11
12345678910111213141516171819.env.env.**.pem*.key.git.gitignore__pycache__/*.pyc*.pyo.pytest_cache/.DS_Storetraining_results/backend.log*.lognode_modules/.cursor/.vscode/Dockerfile.demotest_reset.py4 unmodified lines56784 unmodified linestraining_results//tmp/.DS_Store.env4 unmodified lines5678910114 unmodified linestraining_results//tmp/.DS_Store.env.env.**.lognode_modules/
12345678910111213FROM python:3.11-slimWORKDIR /appCOPY requirements.txt .RUN pip install --no-cache-dir -r requirements.txtRUN pip install --no-cache-dir fastapi uvicornCOPY . .EXPOSE 7860CMD ["python", "-m", "uvicorn", "vergil.api.server:app", "--host", "0.0.0.0", "--port", "7860"]1234567891011121314151617181920212223FROM pytorch/pytorch:2.3.0-cuda12.1-cudnn8-develRUN useradd -m -u 1000 userUSER userENV HOME=/home/user \PATH=/home/user/.local/bin:$PATH \CUDA_HOME=/usr/local/cudaWORKDIR $HOME/appUSER rootRUN apt-get update && apt-get install -y git curl build-essential && rm -rf /var/lib/apt/lists/*USER userCOPY --chown=user . $HOME/appRUN pip install --upgrade pip# Force strict synchronization of PyTorch and Torchvision directly from NVIDIA's serversRUN pip install "torch==2.3.1" "torchvision==0.18.1" --index-url https://download.pytorch.org/whl/cu121# Install all required modules in one robust resolution blockRUN pip install "unsloth" "xformers==0.0.27" "trl" "peft" "accelerate" "bitsandbytes" "gymnasium" "networkx" "scipy" "datasets" "gradio" "huggingface_hub"CMD ["python", "app.py"]
12345678910111213FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .RUN pip install --no-cache-dir -r requirements.txtRUN pip install --no-cache-dir fastapi uvicorn
COPY . .
EXPOSE 7860
CMD ["python", "-m", "uvicorn", "vergil.api.server:app", "--host", "0.0.0.0", "--port", "7860"]1234567891011121314151627 unmodified lines4445464748495051525354553 unmodified lines596061626364656667686970717273747546 unmodified lines1221231241251261271281291301311321331341354 unmodified lines1401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771 unmodified line17918018118218318418539 unmodified lines2252262272282292306 unmodified lines2372382392402412422432442452462472482492502512522533 unmodified lines2572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982996 unmodified lines3063073083093103113121 unmodified line3143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034047 unmodified lines4124134144154164174184195 unmodified lines4254264274284294304314324334344354364374384394404414424434444454464474484494504514524534543 unmodified lines4584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105116 unmodified lines5185195205215225235245255265275285295305315321 unmodified line5345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665673 unmodified lines57157257357457557657757857958058158258358418 unmodified lines60360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363421 unmodified lines6566576586596606616626636646656666676686696706716726736746756766776786796806816826836846855 unmodified lines69169269369469569669714 unmodified lines71271371471571671771871972072172272372472572672724 unmodified lines7527537547557567577587597607614 unmodified lines7667677687697707717721 unmodified line77477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782810 unmodified lines83984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886920 unmodified lines8908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299307 unmodified lines938939940941942943/* ═══════════════════════════════════════════════════════════VERGIL — App Logic v4 (Theater Layout)═══════════════════════════════════════════════════════════ */const API = '';// ── State ────────────────────────────────────────────────let currentState = null;let selectedNode = null;let totalReward = 0;let autoTimer = null;let d3Sim = null;let episodeHistory = []; // [{action,target,reward,step}]// ── DOM shortcuts ────────────────────────────────────────const $ = id => document.getElementById(id);27 unmodified linestry {const data = await fetchJSON(`${API}/api/scenarios`);data.scenarios.forEach(s => {const o = document.createElement('option');o.value = s.scenario_id;o.textContent = `${s.scenario_id.replace('scenario_','').replace(/_/g,' ')}`;$('scenario-select').appendChild(o);const o2 = o.cloneNode(true);$('cmp-scenario-select').appendChild(o2);});} catch(e) { /* no scenarios endpoint — fine */ }}3 unmodified lines// ═══════════════════════════════════════════════════════════async function resetEpisode() {stopAutoplay();totalReward = 0;episodeHistory = [];selectedNode = null;const body = {};const sel = $('scenario-select').value;if (sel) body.scenario_id = sel;setLoading(true);try {const data = await fetchJSON(`${API}/api/reset`, { method: 'POST', body });currentState = data.state;clearFeed();46 unmodified lines}// ═══════════════════════════════════════════════════════════// AGENT AUTO-STEP (uses /api/agent-step — LLM or heuristic)// ═══════════════════════════════════════════════════════════async function agentStep() {if (!currentState) return;try {const data = await fetchJSON(`${API}/api/agent-step`, { method: 'POST', body: {} });// Action/reasoning live inside step_recordconst sr = data.step_record || {};handleStepResponse(data, sr.action || 'do_nothing', sr.agent_reasoning || null);} catch(e) {feedSystem(`Agent step failed: ${e.message}`, true);4 unmodified linesfunction handleStepResponse(data, actionType, reasoning) {if (data.detail) { feedSystem(`Error: ${data.detail}`, true); return; }currentState = data.state;const reward = data.reward || 0;totalReward += reward;const sr = data.step_record || {};const targetId = sr.target || data.target_node_id || data.target;const nodes = currentState.graph?.nodes || [];const node = nodes.find(n => n.id === targetId);// Show agent reasoning block if availableif (reasoning) feedThink(reasoning);// Show decision cardfeedDecision(actionType, node, reward, data.info?.stakeholder_responses);// Timeline entrypushTimeline(actionType, node?.label || targetId || '—', reward);// Log brief summarylogAdd('agent', `${actionIcon(actionType)} ${node?.label || actionType} (${reward >= 0 ? '+' : ''}${reward.toFixed(3)})`);// Cascade eventsconst cascades = data.info?.cascade_events || [];if (cascades.length) {feedCascade(cascades);logAdd('danger', `⚠ Cascade: ${cascades.length} node(s) affected`);}// New pending from stakeholder responsesconst newPending = nodes.filter(n =>n.status === 'pending' &&!episodeHistory.some(h => h.nodeId === n.id));newPending.forEach(n => feedStakeholder(n));1 unmodified linerenderAll(currentState, data);// Auto-select next pendingconst pending = nodes.filter(n => n.status === 'pending');if (pending.length && !pending.find(n => n.id === selectedNode)) selectNode(pending[0].id);39 unmodified lines// ═══════════════════════════════════════════════════════════function renderAll(state, stepData) {renderTopbar(state);renderGraph(state);renderNodePicker(state);renderTrust(state);6 unmodified linesfunction renderTopbar(state) {$('stat-step').textContent = state.step_number || 0;const r = totalReward;const rEl = $('stat-reward');rEl.textContent = (r >= 0 ? '+' : '') + r.toFixed(2);rEl.style.color = r >= 0 ? 'var(--green)' : 'var(--red)';const sat = state.satisfiability_score;const satEl = $('stat-sat');if (sat != null) {const pct = Math.round(sat * 100);satEl.textContent = pct + '%';satEl.style.color = pct >= 70 ? 'var(--green)' : pct >= 40 ? 'var(--yellow)' : 'var(--red)';} else {satEl.textContent = '—'; satEl.style.color = '';}3 unmodified linesif (load != null) {const pct = Math.round(load * 100);ldEl.textContent = pct + '%';ldEl.style.color = pct > 80 ? 'var(--red)' : pct > 50 ? 'var(--yellow)' : 'var(--green)';}$('badge-stage').textContent = `Stage ${state.curriculum_stage || 1}`;}function renderScenarioHeader(state) {const nodes = state.graph?.nodes || [];const n = nodes.length;const stakes = new Set(nodes.map(nd => nd.stakeholder_id).filter(Boolean));$('sh-title').textContent = `${n} commitment${n !== 1 ? 's' : ''} — ${stakes.size} stakeholder${stakes.size !== 1 ? 's' : ''}`;$('sh-sub').textContent = `${state.available_hours_next_48h?.toFixed(1) || '—'}h available in 48h window`;$('sh-icon').textContent = n > 3 ? '🌪' : n > 1 ? '⚡' : '💡';}function renderGraphIndicators(state) {const nodes = state.graph?.nodes || [];const pending = nodes.filter(n => n.status === 'pending').length;const active = nodes.filter(n => n.status === 'accepted').length;const failed = nodes.filter(n => n.status === 'failed').length;const pEl = $('ghb-pending');const aEl = $('ghb-active');const fEl = $('ghb-failed');pEl.textContent = `${pending} pending`;pEl.style.color = pending > 0 ? 'var(--yellow)' : 'var(--text-3)';aEl.textContent = `${active} active`;aEl.style.color = active > 0 ? 'var(--blue)' : 'var(--text-3)';fEl.textContent = `${failed} failed`;fEl.style.color = failed > 0 ? 'var(--red)' : 'var(--text-3)';}// ═══════════════════════════════════════════════════════════// D3 GRAPH// ═══════════════════════════════════════════════════════════function renderGraph(state) {const graphData = state.graph;6 unmodified linesconst svg = d3.select('#graph-svg');svg.selectAll('*').remove();// Build maps for current positions (preserve layout on re-render)const prevPos = {};if (d3Sim) {d3Sim.stop();1 unmodified line}const defs = svg.append('defs');// Arrow markerdefs.append('marker').attr('id', 'arrow').attr('viewBox', '0 -4 8 8').attr('refX', 22).attr('refY', 0).attr('markerWidth', 5).attr('markerHeight', 5).attr('orient', 'auto').append('path').attr('d', 'M0,-4L8,0L0,4').attr('fill', '#5b6b82');defs.append('marker').attr('id', 'arrow-red').attr('viewBox', '0 -4 8 8').attr('refX', 22).attr('refY', 0).attr('markerWidth', 5).attr('markerHeight', 5).attr('orient', 'auto').append('path').attr('d', 'M0,-4L8,0L0,4').attr('fill', 'var(--red)');const g = svg.append('g');// Zoomsvg.call(d3.zoom().scaleExtent([0.4, 3]).on('zoom', e => g.attr('transform', e.transform)));const nodes = graphData.nodes.map(n => ({...n,x: prevPos[n.id]?.x || W/2 + (Math.random()-0.5)*200,y: prevPos[n.id]?.y || H/2 + (Math.random()-0.5)*200,}));const links = (graphData.edges || []).map(e => ({...e}));// Linksconst link = g.append('g').attr('class', 'links').selectAll('line').data(links).join('line').attr('class', d => `link ${d.edge_type || 'dependency'}`).attr('marker-end', d => d.edge_type === 'conflict' ? 'url(#arrow-red)' : 'url(#arrow)');// Node groupsconst node = g.append('g').attr('class', 'nodes').selectAll('g').data(nodes).join('g').attr('class', d => `node status-${d.status}${d.id === selectedNode ? ' selected' : ''}`).call(d3.drag().on('start', (e,d) => { if (!e.active) d3Sim.alphaTarget(0.3).restart(); d.fx=d.x; d.fy=d.y; }).on('drag', (e,d) => { d.fx=e.x; d.fy=e.y; }).on('end', (e,d) => { if (!e.active) d3Sim.alphaTarget(0); d.fx=null; d.fy=null; })).on('click', (e, d) => { e.stopPropagation(); selectNode(d.id); });const radius = d => 14 + (d.urgency || 0.5) * 8;node.append('circle').attr('r', radius);// Urgency ringnode.append('circle').attr('class', 'urgency-ring').attr('r', d => radius(d) + 5).attr('stroke', d => {const u = d.urgency || 0;return u > 0.7 ? 'var(--red)' : u > 0.4 ? 'var(--yellow)' : 'var(--green)';}).attr('stroke-opacity', d => (d.urgency || 0) * 0.6).attr('fill', 'none').attr('stroke-width', 1.5).attr('stroke-dasharray', '3,3');// Labelsnode.append('text').attr('dy', '-1px').text(d => d.label?.length > 12 ? d.label.slice(0, 10) + '…' : (d.label || d.id));node.append('text').attr('class', 'node-sublabel').attr('dy', '14px').text(d => {const hrs = d.estimated_duration_hours;return hrs ? `${hrs}h` : '';});// Force simulationd3Sim = d3.forceSimulation(nodes).force('link', d3.forceLink(links).id(d => d.id).distance(100).strength(0.5)).force('charge', d3.forceManyBody().strength(-280)).force('center', d3.forceCenter(W/2, H/2)).force('collide', d3.forceCollide(d => radius(d) + 18)).on('tick', () => {link.attr('x1', d => d.source.x).attr('y1', d => d.source.y).attr('x2', d => d.target.x).attr('y2', d => d.target.y);node.attr('transform', d => `translate(${d.x},${d.y})`);});}7 unmodified linespicker.innerHTML = '<option value="">— select commitment —</option>';(state.graph?.nodes || []).forEach(n => {const o = document.createElement('option');o.value = n.id;const dur = n.estimated_duration_hours ? `${n.estimated_duration_hours}h` : '';o.textContent = `[${n.status}] ${n.label || n.id} ${dur}`;if (n.status !== 'pending') o.style.color = '#5b6b82';5 unmodified linesfunction selectNode(nodeId) {selectedNode = nodeId;$('node-picker').value = nodeId;// Highlight in graphd3.selectAll('.node').classed('selected', d => d.id === nodeId);renderTargetDetail(currentState);}function renderTargetDetail(state) {const el = $('target-detail');if (!selectedNode || !state) { el.innerHTML = '<div class="td-empty">Click a graph node or select from dropdown</div>'; return; }const node = (state.graph?.nodes || []).find(n => n.id === selectedNode);if (!node) { el.innerHTML = '<div class="td-empty">Node not found</div>'; return; }const dl = node.deadline ? new Date(node.deadline).toLocaleString([], {month:'short',day:'numeric',hour:'2-digit',minute:'2-digit'}) : 'none';const urgPct = Math.round((node.urgency || 0) * 100);el.innerHTML = `<div class="td-name">${node.label || node.id}</div><div class="td-row"><span class="td-k">Status</span><span class="td-v"><span class="td-status ${node.status}">${node.status}</span></span></div><div class="td-row"><span class="td-k">Duration</span><span class="td-v">${node.estimated_duration_hours || '?'}h</span></div><div class="td-row"><span class="td-k">Deadline</span><span class="td-v">${dl}</span></div><div class="td-row"><span class="td-k">Urgency</span><span class="td-v" style="color:${urgPct>70?'var(--red)':urgPct>40?'var(--yellow)':'var(--green)'}">${urgPct}%</span></div><div class="td-row"><span class="td-k">Stakeholder</span><span class="td-v">${node.stakeholder_id || '—'}</span></div>${node.type ? `<div class="td-row"><span class="td-k">Type</span><span class="td-v">${node.type}</span></div>` : ''}`;3 unmodified lines// TRUST BARS// ═══════════════════════════════════════════════════════════function renderTrust(state) {// API returns trust_scores: {sid: float} and optionally multidim_trust: {sid: {reliability,competence,benevolence}}const scores = state.trust_scores || state.trust_entries || {};const mdTrust = state.multidim_trust || {};const list = $('trust-list');list.innerHTML = '';const vals = Object.values(scores).map(v => typeof v === 'number' ? v : (v.trust_score || 0));const avg = vals.length ? vals.reduce((a,b)=>a+b,0)/vals.length : null;const avgBadge = $('trust-avg-badge');if (avg !== null) {avgBadge.textContent = `avg ${(avg*100).toFixed(0)}%`;avgBadge.style.background = avg >= 0.6 ? 'hsla(142,50%,20%,0.3)' : avg >= 0.4 ? 'hsla(38,60%,20%,0.3)' : 'hsla(0,50%,20%,0.3)';avgBadge.style.color = avg >= 0.6 ? 'var(--green)' : avg >= 0.4 ? 'var(--yellow)' : 'var(--red)';}Object.entries(scores).forEach(([sid, raw]) => {const score = typeof raw === 'number' ? raw : (raw.trust_score || 0);const pct = Math.round(score * 100);const cls = score >= 0.65 ? 'high' : score >= 0.45 ? 'medium' : score >= 0.25 ? 'low' : 'critical';const md = mdTrust[sid];let dimsHtml = '';if (md) {dimsHtml = `<div class="te-dims"><span class="te-dim">R:<span>${((md.reliability||0)*100).toFixed(0)}</span></span><span class="te-dim">C:<span>${((md.competence||0)*100).toFixed(0)}</span></span><span class="te-dim">B:<span>${((md.benevolence||0)*100).toFixed(0)}</span></span></div>`;}list.insertAdjacentHTML('beforeend', `<div class="trust-entry"><div class="te-header"><span class="te-name">${sid}</span><span class="te-score ${cls}">${pct}%</span></div><div class="te-bar-track"><div class="te-bar-fill ${cls}" style="width:${pct}%"></div></div>${dimsHtml}</div>`);});if (!Object.keys(scores).length) {list.innerHTML = '<div style="color:var(--text-3);font-size:11px;padding:4px 0">No stakeholders yet</div>';}}// ═══════════════════════════════════════════════════════════6 unmodified lines.filter(n => ['accepted','in_progress'].includes(n.status)).reduce((s, n) => s + (n.estimated_duration_hours || 0), 0);const pct = Math.min(100, Math.round((committed / avail) * 100));const cls = pct >= 90 ? 'crit' : pct >= 70 ? 'warn' : '';$('cap-committed').textContent = committed.toFixed(1) + 'h';$('cap-available').textContent = avail.toFixed(1) + 'h';const fill = $('cap-bar-fill');fill.style.width = pct + '%';fill.className = 'cap-bar-fill' + (cls ? ' ' + cls : '');}// ═══════════════════════════════════════════════════════════1 unmodified line// ═══════════════════════════════════════════════════════════function renderReward(stepData) {const el = $('reward-display');if (!stepData?.reward_components && !stepData?.info?.reward_components) {return;}const rc = stepData.info?.reward_components || stepData.reward_components;const r = stepData.reward || 0;const rClass = r >= 0 ? 'pos' : 'neg';const rSign = r >= 0 ? '+' : '';const rows = [{ k: 'Fulfillment', v: rc?.fulfillment || 0 },{ k: 'Trust Δ', v: rc?.trust_delta || 0 },{ k: 'Proactive', v: rc?.proactive || 0 },{ k: 'Accuracy', v: rc?.feasibility_acc || 0 },{ k: '— Broken', v: -(rc?.broken_penalty || 0) },{ k: '— Over-refusal', v: -(rc?.overrefusal_penalty || 0) },{ k: '— Silent drop', v: -(rc?.silent_drop_penalty || 0) },];el.innerHTML = `<div class="rwd-total ${rClass}">${rSign}${r.toFixed(4)}</div>${rows.map(row => {const vCls = row.v > 0.001 ? 'pos' : row.v < -0.001 ? 'neg' : 'zero';const vSign = row.v >= 0 ? '+' : '';return `<div class="rwd-row"><span class="rwd-key">${row.k}</span><span class="rwd-val ${vCls}">${vSign}${row.v.toFixed(4)}</span></div>`;}).join('')}`;3 unmodified lines// CONVERSATION FEED// ═══════════════════════════════════════════════════════════function clearFeed() {// Remove only .msg elements — leave #feed-empty intactdocument.querySelectorAll('#message-feed .msg').forEach(el => el.remove());$('feed-empty').classList.remove('hidden');}function feedMsg(html) {const feed = $('message-feed');$('feed-empty').classList.add('hidden');feed.insertAdjacentHTML('beforeend', html);feed.scrollTop = feed.scrollHeight;}18 unmodified lines}function feedThink(reasoning) {// Parse structured reasoning into steps if it contains numbered linesconst lines = reasoning.split('\n').filter(l => l.trim());const stepsHtml = lines.map(l => `<div class="think-step">${l.trim()}</div>`).join('');feedMsg(`<div class="msg msg-think"><div class="think-header">🧠 Agent Reasoning</div><div class="think-body">${stepsHtml || reasoning}</div></div>`);}function feedDecision(actionType, node, reward, stakeholderResponses) {const icons = { accept:'✅', decline:'❌', counter_propose:'🔄', do_nothing:'⏳', renegotiate:'🤝' };const labels= { accept:'Accepted', decline:'Declined', counter_propose:'Counter-proposed', do_nothing:'Waited', renegotiate:'Renegotiated' };const isPos = actionType === 'accept' || actionType === 'counter_propose';const rSign = reward >= 0 ? '+' : '';let responsesHtml = '';if (stakeholderResponses) {Object.entries(stakeholderResponses).forEach(([sid, msg]) => {if (msg) responsesHtml += `<div style="margin-top:4px;font-size:11px;color:var(--text-3)"><em>${sid}: "${msg}"</em></div>`;});}feedMsg(`<div class="msg msg-decision ${isPos ? '' : 'negative'}"><div class="md-action">${icons[actionType] || '•'} ${labels[actionType] || actionType}</div><div class="md-target">${node ? `"${node.label || node.id}"` : '—'}</div>${responsesHtml}21 unmodified linesconst rSign = reward >= 0 ? '+' : '';if (track.children.length > 0) {track.insertAdjacentHTML('beforeend', '<div class="tl-connector"></div>');}track.insertAdjacentHTML('beforeend', `<div class="tl-step ${actionType}" title="Step ${step}: ${actionType} — ${label}"><div class="tl-icon">${icons[actionType] || '•'}</div><div class="tl-label2">s${step}</div><div class="tl-reward ${rCls}">${rSign}${reward.toFixed(2)}</div></div>`);track.scrollLeft = track.scrollWidth;}// ═══════════════════════════════════════════════════════════// EVENT LOG (right panel)// ═══════════════════════════════════════════════════════════function clearLog() { $('log-list').innerHTML = ''; }function logAdd(type, text) {const el = document.createElement('div');el.className = `log-item ${type}`;el.textContent = text;const list = $('log-list');list.appendChild(el);while (list.children.length > 60) list.removeChild(list.firstChild);list.scrollTop = list.scrollHeight;5 unmodified linesasync function fetchJSON(url, { method = 'GET', body } = {}) {const opts = { method, headers: { 'Content-Type': 'application/json' } };if (body) opts.body = JSON.stringify(body);const res = await fetch(url, opts);if (!res.ok) throw new Error(`HTTP ${res.status}`);return res.json();}14 unmodified lines// ═══════════════════════════════════════════════════════════// COMPARE MODE// ═══════════════════════════════════════════════════════════let compareData = null;let compareStepIdx = 0;let compareAutoTimer = null;const SCENARIO_DESCS = {scenario_04_deadline_crunch: { icon:'⏰', name:'Deadline Crunch', desc:'Back-to-back deadlines — agent must triage' },scenario_07_simultaneous_infeasibility: { icon:'💥', name:'Simultaneous Infeasibility',desc:'3 requests arrive at once — together impossible' },scenario_10_deadline_cascade: { icon:'🌊', name:'Deadline Cascade Chain', desc:'A→B→C dependency chain — one slip cascades' },scenario_11_impossible_math: { icon:'🧮', name:'Impossible Math', desc:'11.5h of work in 6h window — must decline' },scenario_12_force_majeure_recovery: { icon:'🚨', name:'Force Majeure Recovery', desc:'P0 incident blocks 7h mid-episode — renegotiate everything' },};function openCompare() {24 unmodified lines$('cmp-body').classList.add('hidden');try {const data = await fetchJSON(`${API}/api/compare`, {method: 'POST',body: { scenario_id: scenarioId },});compareData = data;compareStepIdx = 0;4 unmodified linesrenderCmpStep(0);$('cmp-step-label').textContent = `Step 1 / ${Math.max(data.naive.steps.length, data.vergil.steps.length)}`;} catch(e) {$('cmp-loading').innerHTML = `<p style="color:var(--red)">Error: ${e.message}</p>`;}}1 unmodified lineconst n = data.naive.metrics;const v = data.vergil.metrics;const rDelta = (v.total_reward || 0) - (n.total_reward || 0);const satDelta = (v.final_sat || 0) - (n.final_sat || 0);const failAvoid = (n.n_failed || 0) - (v.n_failed || 0);const trustDelta= (v.avg_trust || 0) - (n.avg_trust || 0);function fmt(val, isCount = false) {const sign = val >= 0 ? '+' : '';return isCount ? `${val >= 0 ? '+' : ''}${val}` : `${sign}${val.toFixed(2)}`;}function cls(val) { return val > 0 ? 'better' : val < 0 ? 'worse' : ''; }$('dv-reward').textContent = fmt(rDelta);$('dv-reward').className = `dr-val ${cls(rDelta)}`;$('dv-sat').textContent = fmt(satDelta * 100) + '%';$('dv-sat').className = `dr-val ${cls(satDelta)}`;$('dv-fail').textContent = fmt(failAvoid, true);$('dv-fail').className = `dr-val ${cls(failAvoid)}`;$('dv-trust').textContent = fmt(trustDelta * 100) + '%';$('dv-trust').className = `dr-val ${cls(trustDelta)}`;// Verdictconst improved = [rDelta > 0, satDelta > 0, failAvoid >= 0, trustDelta > 0].filter(Boolean).length;$('cmp-verdict').textContent =improved >= 3 ? '✅ VERGIL significantly outperforms naive agent' :improved >= 2 ? '↑ VERGIL shows clear improvement' :'~ Results comparable — try a harder scenario';// Naive & VERGIL final statsrenderSideStats('naive-stats', n);renderSideStats('vergil-stats', v);// Draw final CDG statesrenderMiniGraph('#cmp-svg-naive', data.naive.final_graph, 'naive');renderMiniGraph('#cmp-svg-vergil', data.vergil.final_graph, 'vergil');}function renderSideStats(elId, metrics) {$(`${elId}`).innerHTML = `<div class="css-stat"><div class="css-label">Reward</div><div class="css-val" style="color:${(metrics.total_reward||0)>=0?'var(--green)':'var(--red)'}">${(metrics.total_reward||0) >= 0 ? '+' : ''}${(metrics.total_reward||0).toFixed(2)}</div></div><div class="css-stat"><div class="css-label">SAT</div><div class="css-val">${Math.round((metrics.final_sat||0)*100)}%</div></div><div class="css-stat"><div class="css-label">Failed</div><div class="css-val" style="color:${(metrics.n_failed||0)>0?'var(--red)':'var(--green)'}">${metrics.n_failed||0}</div></div><div class="css-stat"><div class="css-label">Trust</div><div class="css-val">${Math.round((metrics.avg_trust||0)*100)}%</div></div>`;}10 unmodified linesconst vStep = vSteps[compareStepIdx];function stepHtml(step, isVergil) {if (!step) return '<em style="color:var(--text-3)">No action</em>';const icon = actionIcon(step.action);const r = step.reward || 0;const rS = r >= 0 ? '+' : '';if (isVergil && step.reasoning) {return `${icon} <strong>${step.action}</strong> → ${step.target || '—'}<br><span style="color:#c084fc;margin-top:3px;display:block">🧠 ${step.reasoning}</span><span style="color:var(--text-3)">${rS}${r.toFixed(3)}</span>`;}return `${icon} <strong>${step.action}</strong> → ${step.target || '—'}<span style="color:var(--text-3);margin-left:8px">${rS}${r.toFixed(3)}</span>`;}$('naive-step-display').innerHTML = stepHtml(nStep, false);$('vergil-step-display').innerHTML = stepHtml(vStep, true);// If any naive step caused a failure, animate cascadeif (nStep?.caused_failure) {$('cmp-svg-naive').classList.add('cascade-active');setTimeout(() => $('cmp-svg-naive').classList.remove('cascade-active'), 800);}}function compareStep(delta) {renderCmpStep(compareStepIdx + delta);}function toggleCompareAuto() {const btn = $('btn-cmp-auto');20 unmodified lines}function renderMiniGraph(svgSelector, graphData, side) {if (!graphData || !graphData.nodes?.length) return;const svgEl = document.querySelector(svgSelector);if (!svgEl) return;const W = svgEl.clientWidth || 500;const H = svgEl.clientHeight || 300;const svg = d3.select(svgSelector);svg.selectAll('*').remove();const g = svg.append('g');const nodes = graphData.nodes.map(n => ({...n, x: W/2 + (Math.random()-.5)*200, y: H/2 + (Math.random()-.5)*200 }));const links = (graphData.edges || []).map(e => ({...e}));const colorByStatus = s => ({pending: '#eab308', accepted: '#3b82f6',completed: '#22c55e', failed: '#ef4444',}[s] || '#5b6b82');const link = g.append('g').selectAll('line').data(links).join('line').attr('stroke', '#334155').attr('stroke-width', 1.5).attr('stroke-opacity', 0.5);const node = g.append('g').selectAll('g').data(nodes).join('g');node.append('circle').attr('r', d => 10 + (d.urgency||0.5)*6).attr('fill', d => `${colorByStatus(d.status)}22`).attr('stroke', d => colorByStatus(d.status)).attr('stroke-width', d => d.status === 'failed' ? 3 : 1.5).style('filter', d => d.status === 'failed' && side === 'naive'? 'drop-shadow(0 0 8px rgba(239,68,68,0.8))' : 'none');node.append('text').attr('text-anchor', 'middle').attr('dominant-baseline', 'central').attr('fill', '#94a3b8').attr('font-size', '9px').attr('pointer-events', 'none').text(d => d.label?.slice(0,8) || d.id?.slice(0,6));const sim = d3.forceSimulation(nodes)7 unmodified linesnode.attr('transform', d=>`translate(${d.x},${d.y})`);});// Stop after settlingsetTimeout(() => sim.stop(), 3000);}1234567891011121314151617181927 unmodified lines4748495051525354555657583 unmodified lines626364656667686970717273747576777879808146 unmodified lines1281291301311321331341351361371381391404 unmodified lines1451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741 unmodified line17617717817918018139 unmodified lines2212222232242252262276 unmodified lines2342352362372382392402412422432442452462472482492503 unmodified lines2542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353366 unmodified lines3433443453463473481 unmodified line3503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684697 unmodified lines4774784794804814824834845 unmodified lines4904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195203 unmodified lines5245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735746 unmodified lines5815825835845855865875885895905915925935945955965975985996006016026036046051 unmodified line6076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366373 unmodified lines64164264364464564664764864965065165265318 unmodified lines67267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170221 unmodified lines7247257267277287297307317327337347357367377387397407417427437447457467477487497507515 unmodified lines75775875976076176276314 unmodified lines77877978078178278378478578678778878979079179279324 unmodified lines8188198208218228238244 unmodified lines8298308318328338348351 unmodified line83783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388410 unmodified lines89589689789889990090190290390490590690790890991091191291391491591691791891992092192220 unmodified lines9439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829837 unmodified lines991992993994995/* ═══════════════════════════════════════════════════════════VERGIL — App Logic v5 (Matching Design System v5)═══════════════════════════════════════════════════════════ */const API = '';// ── State ────────────────────────────────────────────────let currentState = null;let selectedNode = null;let totalReward = 0;let autoTimer = null;let d3Sim = null;let episodeHistory = [];let cascadeCount = 0;let prevTrustAvg = null;let prevHealth = null;// ── DOM shortcuts ────────────────────────────────────────const $ = id => document.getElementById(id);27 unmodified linestry {const data = await fetchJSON(`${API}/api/scenarios`);data.scenarios.forEach(s => {const label = s.scenario_id.replace('scenario_','').replace(/_/g,' ');const o = document.createElement('option');o.value = s.scenario_id;o.textContent = label;$('scenario-select').appendChild(o);$('cmp-scenario-select').appendChild(o.cloneNode(true));});} catch(e) { /* no scenarios endpoint — fine */ }}3 unmodified lines// ═══════════════════════════════════════════════════════════async function resetEpisode() {stopAutoplay();totalReward = 0;episodeHistory = [];selectedNode = null;cascadeCount = 0;prevTrustAvg = null;prevHealth = null;const body = {};const sel = $('scenario-select').value;if (sel) body.scenario_id = sel;setLoading(true);try {const data = await fetchJSON(`${API}/api/reset`, { method: 'POST', body });currentState = data.state;clearFeed();46 unmodified lines}// ═══════════════════════════════════════════════════════════// AGENT AUTO-STEP// ═══════════════════════════════════════════════════════════async function agentStep() {if (!currentState) return;try {const data = await fetchJSON(`${API}/api/agent-step`, { method: 'POST', body: {} });const sr = data.step_record || {};handleStepResponse(data, sr.action || 'do_nothing', sr.agent_reasoning || null);} catch(e) {feedSystem(`Agent step failed: ${e.message}`, true);4 unmodified linesfunction handleStepResponse(data, actionType, reasoning) {if (data.detail) { feedSystem(`Error: ${data.detail}`, true); return; }currentState = data.state;const reward = data.reward || 0;totalReward += reward;const sr = data.step_record || {};const targetId = sr.target || data.target_node_id || data.target;const nodes = currentState.graph?.nodes || [];const node = nodes.find(n => n.id === targetId);if (reasoning) feedThink(reasoning);feedDecision(actionType, node, reward, data.info?.stakeholder_responses);pushTimeline(actionType, node?.label || targetId || '—', reward);logAdd('agent', `${actionIcon(actionType)} ${node?.label || actionType} (${reward >= 0 ? '+' : ''}${reward.toFixed(3)})`);const cascades = data.info?.cascade_events || [];if (cascades.length) {const affected = cascades.filter(e => e.cascaded).length;if (affected) { cascadeCount += affected; }feedCascade(cascades);logAdd('danger', `⚠ Cascade: ${cascades.length} node(s) affected`);}const newPending = nodes.filter(n =>n.status === 'pending' && !episodeHistory.some(h => h.nodeId === n.id));newPending.forEach(n => feedStakeholder(n));1 unmodified linerenderAll(currentState, data);const pending = nodes.filter(n => n.status === 'pending');if (pending.length && !pending.find(n => n.id === selectedNode)) selectNode(pending[0].id);39 unmodified lines// ═══════════════════════════════════════════════════════════function renderAll(state, stepData) {renderTopbar(state);renderKPI(state);renderGraph(state);renderNodePicker(state);renderTrust(state);6 unmodified linesfunction renderTopbar(state) {$('stat-step').textContent = state.step_number || 0;const r = totalReward;const rEl = $('stat-reward');rEl.textContent = (r >= 0 ? '+' : '') + r.toFixed(2);rEl.style.color = r >= 0 ? 'var(--s-completed)' : 'var(--s-failed)';const sat = state.satisfiability_score;const satEl = $('stat-sat');if (sat != null) {const pct = Math.round(sat * 100);satEl.textContent = pct + '%';satEl.style.color = pct >= 70 ? 'var(--s-completed)' : pct >= 40 ? 'var(--s-at-risk)' : 'var(--s-failed)';} else {satEl.textContent = '—'; satEl.style.color = '';}3 unmodified linesif (load != null) {const pct = Math.round(load * 100);ldEl.textContent = pct + '%';ldEl.style.color = pct > 80 ? 'var(--s-failed)' : pct > 50 ? 'var(--s-at-risk)' : 'var(--s-completed)';}$('badge-stage').textContent = `Stage ${state.curriculum_stage || 1}`;}function renderScenarioHeader(state) {const nodes = state.graph?.nodes || [];const n = nodes.length;const stakes = new Set(nodes.map(nd => nd.stakeholder_id).filter(Boolean));$('sh-title').textContent = `${n} commitment${n !== 1 ? 's' : ''} — ${stakes.size} stakeholder${stakes.size !== 1 ? 's' : ''}`;$('sh-sub').textContent = `${state.available_hours_next_48h?.toFixed(1) || '—'}h available in 48h window`;}// ── KPI Strip ────────────────────────────────────────────function renderKPI(state) {const nodes = state.graph?.nodes || [];const total = nodes.length;const done = nodes.filter(n => n.status === 'completed').length;const failed = nodes.filter(n => n.status === 'failed').length;const active = nodes.filter(n => ['accepted','in_progress','completed'].includes(n.status)).length;// Fulfillment rateconst fulfillPct = active > 0 ? Math.round((done / active) * 100) : null;setKPI('kpi-fulfill', fulfillPct != null ? fulfillPct + '%' : '—', null);// Trust avgconst scores = state.trust_scores || {};const vals = Object.values(scores).map(v => typeof v === 'number' ? v : (v?.trust_score || 0));const trustAvg = vals.length ? vals.reduce((a,b)=>a+b,0)/vals.length : null;const trustPct = trustAvg != null ? Math.round(trustAvg * 100) : null;const trustDelta = (trustAvg != null && prevTrustAvg != null)? Math.round((trustAvg - prevTrustAvg) * 100) : null;setKPI('kpi-trust', trustPct != null ? trustPct + '%' : '—', trustDelta);prevTrustAvg = trustAvg;// Cascade countsetKPI('kpi-cascade', String(cascadeCount), null);// CDG healthconst health = state.satisfiability_score;const healthPct = health != null ? Math.round(health * 100) : null;const healthDelta = (health != null && prevHealth != null)? Math.round((health - prevHealth) * 100) : null;setKPI('kpi-health', healthPct != null ? healthPct + '%' : '—', healthDelta);prevHealth = health;}function setKPI(valId, val, delta) {const el = $(valId);if (el) el.textContent = val;const dEl = $(valId + '-delta');if (dEl) {if (delta == null || delta === 0) {dEl.textContent = ''; dEl.className = 'kpi-delta';} else {dEl.textContent = (delta > 0 ? '+' : '') + delta + '%';dEl.className = 'kpi-delta ' + (delta > 0 ? 'up' : 'down');}}}function renderGraphIndicators(state) {const nodes = state.graph?.nodes || [];const pending = nodes.filter(n => n.status === 'pending').length;const active = nodes.filter(n => n.status === 'accepted').length;const completed = nodes.filter(n => n.status === 'completed').length;const failed = nodes.filter(n => n.status === 'failed').length;$('ghb-pending').textContent = `${pending} pending`;$('ghb-active').textContent = `${active} active`;$('ghb-completed').textContent = `${completed} done`;$('ghb-failed').textContent = `${failed} failed`;}// ═══════════════════════════════════════════════════════════// D3 GRAPH — v5 Node Anatomy// ═══════════════════════════════════════════════════════════function renderGraph(state) {const graphData = state.graph;6 unmodified linesconst svg = d3.select('#graph-svg');svg.selectAll('*').remove();const prevPos = {};if (d3Sim) {d3Sim.stop();1 unmodified line}const defs = svg.append('defs');// Arrow markers per edge typeconst markerDefs = [{ id: 'arrow-dep', color: '#475569' },{ id: 'arrow-conflict', color: '#fb7185' },{ id: 'arrow-trust', color: '#8b5cf6' },];markerDefs.forEach(({ id, color }) => {defs.append('marker').attr('id', id).attr('viewBox', '0 -4 8 8').attr('refX', 28).attr('refY', 0).attr('markerWidth', 5).attr('markerHeight', 5).attr('orient', 'auto').append('path').attr('d', 'M0,-4L8,0L0,4').attr('fill', color);});// Glow filter for selectedconst filt = defs.append('filter').attr('id', 'glow').attr('x', '-30%').attr('y', '-30%').attr('width', '160%').attr('height', '160%');filt.append('feGaussianBlur').attr('in', 'SourceGraphic').attr('stdDeviation', '4').attr('result', 'blur');filt.append('feMerge').selectAll('feMergeNode').data(['blur','SourceGraphic']).join('feMergeNode').attr('in', d => d);const g = svg.append('g');svg.call(d3.zoom().scaleExtent([0.35, 3]).on('zoom', e => g.attr('transform', e.transform)));// Assign letter labels A, B, C…const letterMap = {};graphData.nodes.forEach((n, i) => { letterMap[n.id] = String.fromCharCode(65 + (i % 26)); });const nodes = graphData.nodes.map(n => ({...n,letter: letterMap[n.id],x: prevPos[n.id]?.x ?? (W/2 + (Math.random()-0.5)*200),y: prevPos[n.id]?.y ?? (H/2 + (Math.random()-0.5)*160),}));const links = (graphData.edges || []).map(e => ({...e}));// Edges (curved paths for clarity)const edgeGroup = g.append('g').attr('class', 'edges');const link = edgeGroup.selectAll('path').data(links).join('path').attr('class', d => {const t = d.edge_type || 'dependency';if (t === 'conflict') return 'edge conflict';if (t === 'trust_impact') return 'edge trust-impact';return 'edge dependency';}).attr('fill', 'none').attr('marker-end', d => {const t = d.edge_type || 'dependency';if (t === 'conflict') return 'url(#arrow-conflict)';if (t === 'trust_impact') return 'url(#arrow-trust)';return 'url(#arrow-dep)';});const R = d => 20 + (d.urgency || 0.4) * 7;// Node groupsconst nodeGroup = g.append('g').attr('class', 'nodes');const node = nodeGroup.selectAll('g').data(nodes).join('g').attr('class', d => `node ${d.status || 'pending'}${d.id === selectedNode ? ' selected' : ''}`).call(d3.drag().on('start', (e, d) => { if (!e.active) d3Sim.alphaTarget(0.3).restart(); d.fx = d.x; d.fy = d.y; }).on('drag', (e, d) => { d.fx = e.x; d.fy = e.y; }).on('end', (e, d) => { if (!e.active) d3Sim.alphaTarget(0); d.fx = null; d.fy = null; })).on('click', (e, d) => { e.stopPropagation(); selectNode(d.id); });// Pulse ring (CSS animates only .pending)node.append('circle').attr('class', 'node-pulse').attr('r', d => R(d) + 10);// Background fillnode.append('circle').attr('class', 'node-bg').attr('r', d => R(d));// Status ring strokenode.append('circle').attr('class', 'node-ring').attr('r', d => R(d));// Letter label (center)node.append('text').attr('class', 'node-letter').attr('dominant-baseline', 'central').text(d => d.letter);// Commitment label below nodenode.append('text').attr('class', 'node-label').attr('dy', d => R(d) + 14).text(d => {const lbl = d.label || d.id;return lbl.length > 16 ? lbl.slice(0, 14) + '…' : lbl;});// Hours hint (small, below label)node.append('text').attr('class', 'node-deadline').attr('dy', d => R(d) + 26).text(d => d.estimated_duration_hours ? `${d.estimated_duration_hours}h` : '');// Force simulationd3Sim = d3.forceSimulation(nodes).force('link', d3.forceLink(links).id(d => d.id).distance(130).strength(0.45)).force('charge', d3.forceManyBody().strength(-380)).force('center', d3.forceCenter(W/2, H/2)).force('collide', d3.forceCollide(d => R(d) + 32)).on('tick', () => {link.attr('d', d => {const src = d.source, tgt = d.target;const dx = tgt.x - src.x, dy = tgt.y - src.y;const dist = Math.sqrt(dx*dx + dy*dy) || 1;const sr = R(src) + 2, tr = R(tgt) + 2;const sx = src.x + (dx/dist)*sr, sy = src.y + (dy/dist)*sr;const tx = tgt.x - (dx/dist)*tr, ty = tgt.y - (dy/dist)*tr;// Gentle curve to distinguish overlapping edgesconst cx = (sx+tx)/2 - (dy/dist)*18;const cy = (sy+ty)/2 + (dx/dist)*18;return `M${sx},${sy} Q${cx},${cy} ${tx},${ty}`;});node.attr('transform', d => `translate(${d.x},${d.y})`);});}7 unmodified linespicker.innerHTML = '<option value="">— select commitment —</option>';(state.graph?.nodes || []).forEach(n => {const o = document.createElement('option');o.value = n.id;const dur = n.estimated_duration_hours ? `${n.estimated_duration_hours}h` : '';o.textContent = `[${n.status}] ${n.label || n.id} ${dur}`;if (n.status !== 'pending') o.style.color = '#5b6b82';5 unmodified linesfunction selectNode(nodeId) {selectedNode = nodeId;$('node-picker').value = nodeId;d3.selectAll('.node').classed('selected', d => d.id === nodeId);renderTargetDetail(currentState);}function renderTargetDetail(state) {const el = $('target-detail');if (!selectedNode || !state) {el.innerHTML = '<div class="td-empty">Click a graph node or select from dropdown</div>';return;}const node = (state.graph?.nodes || []).find(n => n.id === selectedNode);if (!node) { el.innerHTML = '<div class="td-empty">Node not found</div>'; return; }const dl = node.deadline? new Date(node.deadline).toLocaleString([], {month:'short',day:'numeric',hour:'2-digit',minute:'2-digit'}): 'flexible';const urgPct = Math.round((node.urgency || 0) * 100);const urgColor = urgPct > 70 ? 'var(--s-failed)' : urgPct > 40 ? 'var(--s-at-risk)' : 'var(--s-completed)';el.innerHTML = `<div class="td-name">${node.label || node.id}</div><div class="td-row"><span class="td-k">Status</span><span class="td-v"><span class="td-badge ${node.status}">${node.status}</span></span></div><div class="td-row"><span class="td-k">Duration</span><span class="td-v">${node.estimated_duration_hours || '?'}h</span></div><div class="td-row"><span class="td-k">Deadline</span><span class="td-v">${dl}</span></div><div class="td-row"><span class="td-k">Urgency</span><span class="td-v" style="color:${urgColor}">${urgPct}%</span></div><div class="td-row"><span class="td-k">Stakeholder</span><span class="td-v">${node.stakeholder_id || '—'}</span></div>${node.type ? `<div class="td-row"><span class="td-k">Type</span><span class="td-v">${node.type}</span></div>` : ''}`;3 unmodified lines// TRUST BARS// ═══════════════════════════════════════════════════════════function renderTrust(state) {const scores = state.trust_scores || state.trust_entries || {};const mdTrust = state.multidim_trust || {};const list = $('trust-list');list.innerHTML = '';const vals = Object.values(scores).map(v => typeof v === 'number' ? v : (v?.trust_score || 0));const avg = vals.length ? vals.reduce((a,b)=>a+b,0)/vals.length : null;const avgBadge = $('trust-avg-badge');if (avg !== null) {avgBadge.textContent = `avg ${(avg*100).toFixed(0)}%`;avgBadge.className = `mc-badge ${avg >= 0.6 ? 'green' : avg >= 0.4 ? 'blue' : 'red'}`;}if (!Object.keys(scores).length) {list.innerHTML = '<div style="color:var(--t3);font-size:11px;padding:10px 14px">No stakeholders yet</div>';return;}Object.entries(scores).forEach(([sid, raw]) => {const score = typeof raw === 'number' ? raw : (raw?.trust_score || 0);const pct = Math.round(score * 100);const tier = score >= 0.65 ? 'hi' : score >= 0.45 ? 'mid' : 'lo';const md = mdTrust[sid];const dimsHtml = md ? `<div class="te-dims"><span class="te-dim">R:<span>${((md.reliability||0)*100).toFixed(0)}</span></span><span class="te-dim">C:<span>${((md.competence||0)*100).toFixed(0)}</span></span><span class="te-dim">B:<span>${((md.benevolence||0)*100).toFixed(0)}</span></span></div>` : '';list.insertAdjacentHTML('beforeend', `<div class="te"><div class="te-row1"><span class="te-name">${sid}</span><div class="te-score-wrap"><span class="te-score ${tier}">${pct}%</span></div></div><div class="te-track"><div class="te-fill ${tier}" style="width:${pct}%"></div></div>${dimsHtml}</div>`);});}// ═══════════════════════════════════════════════════════════6 unmodified lines.filter(n => ['accepted','in_progress'].includes(n.status)).reduce((s, n) => s + (n.estimated_duration_hours || 0), 0);const pct = Math.min(100, Math.round((committed / avail) * 100));const cls = pct >= 90 ? 'crit' : pct >= 70 ? 'warn' : '';$('capacity-display').innerHTML = `<div class="cap-header"><span class="cap-val">${committed.toFixed(1)}</span><span class="cap-sep">/</span><span class="cap-of">${avail.toFixed(1)}</span><span class="cap-unit">hours committed</span></div><div class="cap-track"><div class="cap-fill${cls ? ' '+cls : ''}" style="width:${pct}%"></div></div><div class="cap-zones"><span style="color:var(--s-completed)">Safe <70%</span><span style="color:var(--s-at-risk)">⚠ 70–90%</span><span style="color:var(--s-failed)">Critical >90%</span></div>`;}// ═══════════════════════════════════════════════════════════1 unmodified line// ═══════════════════════════════════════════════════════════function renderReward(stepData) {const el = $('reward-display');if (!stepData?.reward_components && !stepData?.info?.reward_components) return;const rc = stepData.info?.reward_components || stepData.reward_components;const r = stepData.reward || 0;const rCls = r >= 0 ? 'pos' : 'neg';const rSign = r >= 0 ? '+' : '';const rows = [{ k: 'Fulfillment', v: rc?.fulfillment || 0 },{ k: 'Trust Δ', v: rc?.trust_delta || 0 },{ k: 'Proactive', v: rc?.proactive || 0 },{ k: 'Accuracy', v: rc?.feasibility_acc || 0 },{ k: '— Broken', v: -(rc?.broken_penalty || 0) },{ k: '— Over-refusal', v: -(rc?.overrefusal_penalty || 0) },{ k: '— Silent drop', v: -(rc?.silent_drop_penalty || 0) },];el.innerHTML = `<div class="rwd-total ${rCls}">${rSign}${r.toFixed(4)}</div>${rows.map(row => {const vCls = row.v > 0.001 ? 'pos' : row.v < -0.001 ? 'neg' : 'zero';const vSign = row.v >= 0 ? '+' : '';return `<div class="rwd-row"><span class="rwd-k">${row.k}</span><span class="rwd-v ${vCls}">${vSign}${row.v.toFixed(4)}</span></div>`;}).join('')}`;3 unmodified lines// CONVERSATION FEED// ═══════════════════════════════════════════════════════════function clearFeed() {document.querySelectorAll('#message-feed .msg').forEach(el => el.remove());$('feed-empty').classList.remove('hidden');}function feedMsg(html) {$('feed-empty').classList.add('hidden');const feed = $('message-feed');feed.insertAdjacentHTML('beforeend', html);feed.scrollTop = feed.scrollHeight;}18 unmodified lines}function feedThink(reasoning) {const lines = reasoning.split('\n').filter(l => l.trim());const bodyHtml = lines.map(l => `<div>${l.trim()}</div>`).join('');feedMsg(`<div class="msg msg-think"><div class="think-header">🧠 Agent Reasoning</div><div class="think-body">${bodyHtml || reasoning}</div></div>`);}function feedDecision(actionType, node, reward, stakeholderResponses) {const icons = { accept:'✅', decline:'❌', counter_propose:'🔄', do_nothing:'⏳', renegotiate:'🤝' };const labels = { accept:'Accepted', decline:'Declined', counter_propose:'Counter-proposed', do_nothing:'Waited', renegotiate:'Renegotiated' };const isPos = ['accept','counter_propose','renegotiate'].includes(actionType);const rSign = reward >= 0 ? '+' : '';let responsesHtml = '';if (stakeholderResponses) {Object.entries(stakeholderResponses).forEach(([sid, msg]) => {if (msg) responsesHtml += `<div style="margin-top:4px;font-size:11px;color:var(--t3)"><em>${sid}: "${msg}"</em></div>`;});}feedMsg(`<div class="msg msg-decision ${isPos ? '' : 'neg'}"><div class="md-action">${icons[actionType] || '•'} ${labels[actionType] || actionType}</div><div class="md-target">${node ? `"${node.label || node.id}"` : '—'}</div>${responsesHtml}21 unmodified linesconst rSign = reward >= 0 ? '+' : '';if (track.children.length > 0) {track.insertAdjacentHTML('beforeend', '<div class="tl-conn"></div>');}track.insertAdjacentHTML('beforeend', `<div class="tl-step ${actionType}" title="Step ${step}: ${actionType} — ${label}"><div class="tl-icon">${icons[actionType] || '•'}</div><div class="tl-num">s${step}</div><div class="tl-r ${rCls}">${rSign}${reward.toFixed(2)}</div></div>`);track.scrollLeft = track.scrollWidth;}// ═══════════════════════════════════════════════════════════// EVENT LOG// ═══════════════════════════════════════════════════════════function clearLog() { $('log-list').innerHTML = ''; }function logAdd(type, text) {const el = document.createElement('div');el.className = `log-item ${type}`;el.textContent = text;const list = $('log-list');list.appendChild(el);while (list.children.length > 60) list.removeChild(list.firstChild);list.scrollTop = list.scrollHeight;5 unmodified linesasync function fetchJSON(url, { method = 'GET', body } = {}) {const opts = { method, headers: { 'Content-Type': 'application/json' } };if (body) opts.body = JSON.stringify(body);const res = await fetch(url, opts);if (!res.ok) throw new Error(`HTTP ${res.status}`);return res.json();}14 unmodified lines// ═══════════════════════════════════════════════════════════// COMPARE MODE// ═══════════════════════════════════════════════════════════let compareData = null;let compareStepIdx = 0;let compareAutoTimer = null;const SCENARIO_DESCS = {scenario_04_deadline_crunch: { icon:'⏰', name:'Deadline Crunch', desc:'Back-to-back deadlines — agent must triage' },scenario_07_simultaneous_infeasibility: { icon:'💥', name:'Simultaneous Infeasibility', desc:'3 requests arrive at once — together impossible' },scenario_10_deadline_cascade: { icon:'🌊', name:'Deadline Cascade Chain', desc:'A→B→C dependency chain — one slip cascades' },scenario_11_impossible_math: { icon:'🧮', name:'Impossible Math', desc:'11.5h of work in 6h window — must decline' },scenario_12_force_majeure_recovery: { icon:'🚨', name:'Force Majeure Recovery', desc:'P0 incident blocks 7h mid-episode — renegotiate' },};function openCompare() {24 unmodified lines$('cmp-body').classList.add('hidden');try {const data = await fetchJSON(`${API}/api/compare`, { method: 'POST', body: { scenario_id: scenarioId } });compareData = data;compareStepIdx = 0;4 unmodified linesrenderCmpStep(0);$('cmp-step-label').textContent = `Step 1 / ${Math.max(data.naive.steps.length, data.vergil.steps.length)}`;} catch(e) {$('cmp-loading').innerHTML = `<p style="color:var(--s-failed)">Error: ${e.message}</p>`;}}1 unmodified lineconst n = data.naive.metrics;const v = data.vergil.metrics;const rDelta = (v.total_reward || 0) - (n.total_reward || 0);const satDelta = (v.final_sat || 0) - (n.final_sat || 0);const failAvoid = (n.n_failed || 0) - (v.n_failed || 0);const trustDelta= (v.avg_trust || 0) - (n.avg_trust || 0);const fmt = (val, isCount = false) =>isCount ? `${val >= 0 ? '+' : ''}${val}` : `${val >= 0 ? '+' : ''}${val.toFixed(2)}`;const cls = val => val > 0 ? 'better' : val < 0 ? 'worse' : '';$('dv-reward').textContent = fmt(rDelta);$('dv-reward').className = `dr-v ${cls(rDelta)}`;$('dv-sat').textContent = fmt(satDelta * 100) + '%';$('dv-sat').className = `dr-v ${cls(satDelta)}`;$('dv-fail').textContent = fmt(failAvoid, true);$('dv-fail').className = `dr-v ${cls(failAvoid)}`;$('dv-trust').textContent = fmt(trustDelta * 100) + '%';$('dv-trust').className = `dr-v ${cls(trustDelta)}`;const improved = [rDelta > 0, satDelta > 0, failAvoid >= 0, trustDelta > 0].filter(Boolean).length;$('cmp-verdict').textContent =improved >= 3 ? '✅ VERGIL significantly outperforms naive agent' :improved >= 2 ? '↑ VERGIL shows clear improvement' :'~ Results comparable — try a harder scenario';renderSideStats('naive-stats', n);renderSideStats('vergil-stats', v);renderMiniGraph('#cmp-svg-naive', data.naive.final_graph, 'naive');renderMiniGraph('#cmp-svg-vergil', data.vergil.final_graph, 'vergil');}function renderSideStats(elId, metrics) {const rColor = (metrics.total_reward||0) >= 0 ? 'var(--s-completed)' : 'var(--s-failed)';const fColor = (metrics.n_failed||0) > 0 ? 'var(--s-failed)' : 'var(--s-completed)';$(elId).innerHTML = `<div class="css-stat"><div class="css-lbl">Reward</div><div class="css-v" style="color:${rColor}">${(metrics.total_reward||0) >= 0 ? '+' : ''}${(metrics.total_reward||0).toFixed(2)}</div></div><div class="css-stat"><div class="css-lbl">SAT</div><div class="css-v">${Math.round((metrics.final_sat||0)*100)}%</div></div><div class="css-stat"><div class="css-lbl">Failed</div><div class="css-v" style="color:${fColor}">${metrics.n_failed||0}</div></div><div class="css-stat"><div class="css-lbl">Trust</div><div class="css-v">${Math.round((metrics.avg_trust||0)*100)}%</div></div>`;}10 unmodified linesconst vStep = vSteps[compareStepIdx];function stepHtml(step, isVergil) {if (!step) return '<em style="color:var(--t3)">No action</em>';const icon = actionIcon(step.action);const r = step.reward || 0;const rS = r >= 0 ? '+' : '';if (isVergil && step.reasoning) {return `${icon} <strong>${step.action}</strong> → ${step.target || '—'}<br><span style="color:#c084fc;margin-top:3px;display:block">🧠 ${step.reasoning}</span><span style="color:var(--t3)">${rS}${r.toFixed(3)}</span>`;}return `${icon} <strong>${step.action}</strong> → ${step.target || '—'}<span style="color:var(--t3);margin-left:8px">${rS}${r.toFixed(3)}</span>`;}$('naive-step-display').innerHTML = stepHtml(nStep, false);$('vergil-step-display').innerHTML = stepHtml(vStep, true);if (nStep?.caused_failure) {$('cmp-svg-naive').classList.add('cascade-active');setTimeout(() => $('cmp-svg-naive').classList.remove('cascade-active'), 800);}}function compareStep(delta) { renderCmpStep(compareStepIdx + delta); }function toggleCompareAuto() {const btn = $('btn-cmp-auto');20 unmodified lines}function renderMiniGraph(svgSelector, graphData, side) {if (!graphData?.nodes?.length) return;const svgEl = document.querySelector(svgSelector);if (!svgEl) return;const W = svgEl.clientWidth || 500;const H = svgEl.clientHeight || 300;const svg = d3.select(svgSelector);svg.selectAll('*').remove();const colorMap = {pending: '#818cf8', accepted: '#38bdf8',completed: '#34d399', failed: '#fb7185',};const g = svg.append('g');const nodes = graphData.nodes.map(n => ({ ...n, x: W/2 + (Math.random()-.5)*200, y: H/2 + (Math.random()-.5)*200 }));const links = (graphData.edges || []).map(e => ({...e}));const link = g.append('g').selectAll('line').data(links).join('line').attr('stroke', '#2d3f58').attr('stroke-width', 1.5).attr('stroke-opacity', 0.6);const node = g.append('g').selectAll('g').data(nodes).join('g');node.append('circle').attr('r', d => 10 + (d.urgency||0.5)*5).attr('fill', d => `${colorMap[d.status] || '#475569'}18`).attr('stroke', d => colorMap[d.status] || '#475569').attr('stroke-width', d => d.status === 'failed' ? 2.5 : 1.5).style('filter', d => d.status === 'failed' && side === 'naive'? 'drop-shadow(0 0 8px rgba(251,113,133,0.8))' : 'none');node.append('text').attr('text-anchor', 'middle').attr('dominant-baseline', 'central').attr('fill', '#94a3b8').attr('font-size', '9px').attr('font-weight', '600').attr('pointer-events', 'none').text(d => d.label?.slice(0,8) || d.id?.slice(0,6));const sim = d3.forceSimulation(nodes)7 unmodified linesnode.attr('transform', d=>`translate(${d.x},${d.y})`);});setTimeout(() => sim.stop(), 3000);}
17 unmodified lines181920212223242526272829303132333435363738394041426 unmodified lines4950515253541 unmodified line565758596061626364656667686970717216 unmodified lines8990919293949596979812 unmodified lines1111121131141151164 unmodified lines1211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571 unmodified line1591601611621631641651 unmodified line16716816917017117217312 unmodified lines18618718818919019119225 unmodified lines21821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226317 unmodified lines<span class="brand-glyph">⟁</span><span class="brand-name">VERGIL</span></div><div class="badge" id="badge-stage">Stage 1</div></div><div class="topbar-stats"><div class="stat-pill"><span class="sp-label">STEP</span><span class="sp-val" id="stat-step">0</span></div><div class="stat-pill"><span class="sp-label">REWARD</span><span class="sp-val" id="stat-reward">+0.00</span></div><div class="stat-pill"><span class="sp-label">HEALTH</span><span class="sp-val" id="stat-sat">—</span></div><div class="stat-pill"><span class="sp-label">LOAD</span><span class="sp-val" id="stat-load">—</span></div></div>6 unmodified lines</div></header><!-- ══════════════════ THREE-COLUMN THEATER ══════════════════ --><main id="theater">1 unmodified line<section id="conv-panel"><div id="scenario-header"><div class="sh-icon" id="sh-icon">💡</div><div class="sh-body"><div class="sh-title" id="sh-title">Start an episode to begin</div><div class="sh-sub" id="sh-sub">Select a scenario and click New Episode</div></div></div><div id="message-feed"><div class="feed-empty" id="feed-empty"><div class="fe-icon">🧠</div><div>Agent reasoning will appear here as it makes decisions.</div></div></div>16 unmodified lines<section id="graph-panel"><div id="graph-header-bar"><span class="ghb-title">Commitment Dependency Graph</span><div class="ghb-indicators"><span class="ghb-dot" id="ghb-pending">0 pending</span><span class="ghb-dot" id="ghb-active">0 active</span><span class="ghb-dot" id="ghb-failed">0 failed</span></div></div>12 unmodified lines<span class="gl-item"><span class="gl-dot accepted"></span>Accepted</span><span class="gl-item"><span class="gl-dot completed"></span>Completed</span><span class="gl-item"><span class="gl-dot failed"></span>Failed</span><span class="gl-item"><span class="gl-line dep"></span>Depends on</span><span class="gl-item"><span class="gl-line conflict"></span>Conflicts</span></div>4 unmodified lines<!-- Trust Network --><div class="mc" id="mc-trust"><div class="mc-title"><span>Trust Network</span><span class="mc-subtitle" id="trust-avg-badge">avg —</span></div><div id="trust-list"></div></div><!-- Schedule Capacity --><div class="mc" id="mc-capacity"><div class="mc-title">Schedule Capacity</div><div id="capacity-display"><div class="cap-numbers"><span id="cap-committed">—h</span><span class="cap-slash">/</span><span id="cap-available">—h</span><span class="cap-label">committed of available (48h window)</span></div><div class="cap-bar-track"><div class="cap-bar-fill" id="cap-bar-fill"></div></div><div class="cap-legend"><span class="cap-ok">Safe <70%</span><span class="cap-warn">Warning 70–90%</span><span class="cap-crit">Critical >90%</span></div></div></div><!-- Last Decision Score --><div class="mc" id="mc-reward"><div class="mc-title">Last Decision Score</div><div id="reward-display"><div class="rwd-empty">Take an action to see reward breakdown</div></div>1 unmodified line<!-- Node Detail --><div class="mc" id="mc-target"><div class="mc-title">Selected Commitment</div><div id="target-detail"><div class="td-empty">Click a graph node or select from the dropdown</div></div>1 unmodified line<!-- Event Log --><div class="mc mc-log" id="mc-log"><div class="mc-title">Event Log</div><div id="log-list"></div></div>12 unmodified lines<div class="cmp-topbar"><div class="cmp-scenario-info" id="cmp-scenario-info"><span class="cmp-scenario-icon" id="cmp-scenario-icon">⚡</span><div><div class="cmp-scenario-name" id="cmp-scenario-name">Select a scenario</div><div class="cmp-scenario-desc" id="cmp-scenario-desc">Loading…</div>25 unmodified lines<!-- LEFT: Naive agent --><div class="cmp-side naive-side"><div class="cmp-side-header naive-header"><span class="csh-badge">❌ Naive Agent</span><span class="csh-desc">Accepts everything — cascade inevitable</span></div><svg id="cmp-svg-naive" class="cmp-svg"></svg><div class="cmp-side-stats" id="naive-stats"></div><div class="cmp-side-step" id="naive-step-display"></div></div><!-- CENTER: Delta column --><div class="cmp-center"><div class="cmp-delta-title">IMPROVEMENT</div><div class="delta-row" id="d-reward"><div class="dr-label">Reward Δ</div><div class="dr-val" id="dv-reward">—</div></div><div class="delta-row" id="d-sat"><div class="dr-label">Health Δ</div><div class="dr-val" id="dv-sat">—</div></div><div class="delta-row" id="d-fail"><div class="dr-label">Failures Avoided</div><div class="dr-val" id="dv-fail">—</div></div><div class="delta-row" id="d-trust"><div class="dr-label">Trust Δ</div><div class="dr-val" id="dv-trust">—</div></div><div class="cmp-verdict" id="cmp-verdict"></div></div><!-- RIGHT: VERGIL agent --><div class="cmp-side vergil-side"><div class="cmp-side-header vergil-header"><span class="csh-badge">✅ VERGIL Agent</span><span class="csh-desc">Reasons through CDG before deciding</span></div><svg id="cmp-svg-vergil" class="cmp-svg"></svg><div class="cmp-side-stats" id="vergil-stats"></div><div class="cmp-side-step cmp-think-block" id="vergil-step-display"></div></div></div>17 unmodified lines181920212223242526272829303132333435363738394041426 unmodified lines4950515253545556575859606162636465666768697071727374757677787980818283848586878889901 unmodified line929394959697989910010110210310410510616 unmodified lines12312412512612712812913013113213312 unmodified lines1461471481491501511524 unmodified lines1571581591601611621631641651661671681691701711721731741751761771781 unmodified line1801811821831841851861 unmodified line18818919019119219319412 unmodified lines20720820921021121221325 unmodified lines23924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328417 unmodified lines<span class="brand-glyph">⟁</span><span class="brand-name">VERGIL</span></div><span class="brand-version" id="badge-stage">Stage 1</span></div><div class="topbar-center"><div class="stat-chip"><span class="sc-label">STEP</span><span class="sc-val" id="stat-step">0</span></div><div class="stat-chip"><span class="sc-label">REWARD</span><span class="sc-val" id="stat-reward">+0.00</span></div><div class="stat-chip"><span class="sc-label">HEALTH</span><span class="sc-val" id="stat-sat">—</span></div><div class="stat-chip"><span class="sc-label">LOAD</span><span class="sc-val" id="stat-load">—</span></div></div>6 unmodified lines</div></header><!-- ══════════════════ KPI STRIP ══════════════════ --><div id="kpi-strip"><div class="kpi-card fulfill"><div class="kpi-label">Fulfillment Rate</div><div class="kpi-row"><div class="kpi-val" id="kpi-fulfill">—</div><div class="kpi-delta" id="kpi-fulfill-delta"></div></div><div class="kpi-sub">commitments completed</div></div><div class="kpi-card trust"><div class="kpi-label">Avg Trust Score</div><div class="kpi-row"><div class="kpi-val" id="kpi-trust">—</div><div class="kpi-delta" id="kpi-trust-delta"></div></div><div class="kpi-sub">across all stakeholders</div></div><div class="kpi-card cascade"><div class="kpi-label">Cascade Events</div><div class="kpi-row"><div class="kpi-val" id="kpi-cascade">0</div><div class="kpi-delta" id="kpi-cascade-delta"></div></div><div class="kpi-sub">dependency failures</div></div><div class="kpi-card health"><div class="kpi-label">CDG Health</div><div class="kpi-row"><div class="kpi-val" id="kpi-health">—</div><div class="kpi-delta" id="kpi-health-delta"></div></div><div class="kpi-sub">satisfiability score</div></div></div><!-- ══════════════════ THREE-COLUMN THEATER ══════════════════ --><main id="theater">1 unmodified line<section id="conv-panel"><div id="scenario-header"><div class="sh-eyebrow">Current Scenario</div><div class="sh-title" id="sh-title">Start an episode to begin</div><div class="sh-sub" id="sh-sub">Select a scenario and click New Episode</div></div><div id="message-feed"><div class="feed-empty" id="feed-empty"><div class="fe-icon">🧠</div><div class="fe-text">Agent reasoning will appear here as it makes decisions through the Commitment Dependency Graph.</div></div></div>16 unmodified lines<section id="graph-panel"><div id="graph-header-bar"><span class="ghb-title">Commitment Dependency Graph</span><div class="ghb-chips"><span class="ghb-chip pending" id="ghb-pending">0 pending</span><span class="ghb-chip active" id="ghb-active">0 active</span><span class="ghb-chip completed" id="ghb-completed">0 done</span><span class="ghb-chip failed" id="ghb-failed">0 failed</span></div></div>12 unmodified lines<span class="gl-item"><span class="gl-dot accepted"></span>Accepted</span><span class="gl-item"><span class="gl-dot completed"></span>Completed</span><span class="gl-item"><span class="gl-dot failed"></span>Failed</span><span class="gl-item"><span class="gl-dot at-risk"></span>At Risk</span><span class="gl-item"><span class="gl-line dep"></span>Depends on</span><span class="gl-item"><span class="gl-line conflict"></span>Conflicts</span></div>4 unmodified lines<!-- Trust Network --><div class="mc" id="mc-trust"><div class="mc-hd"><span>Trust Network</span><span class="mc-badge blue" id="trust-avg-badge">avg —</span></div><div id="trust-list"></div></div><!-- Schedule Capacity --><div class="mc" id="mc-capacity"><div class="mc-hd">Schedule Capacity</div><div id="capacity-display"></div></div><!-- Last Decision Score --><div class="mc" id="mc-reward"><div class="mc-hd">Last Decision Score</div><div id="reward-display"><div class="rwd-empty">Take an action to see reward breakdown</div></div>1 unmodified line<!-- Node Detail --><div class="mc" id="mc-target"><div class="mc-hd">Selected Commitment</div><div id="target-detail"><div class="td-empty">Click a graph node or select from the dropdown</div></div>1 unmodified line<!-- Event Log --><div class="mc mc-log" id="mc-log"><div class="mc-hd">Event Log</div><div id="log-list"></div></div>12 unmodified lines<div class="cmp-topbar"><div class="cmp-scenario-info" id="cmp-scenario-info"><span class="cmp-icon" id="cmp-scenario-icon">⚡</span><div><div class="cmp-scenario-name" id="cmp-scenario-name">Select a scenario</div><div class="cmp-scenario-desc" id="cmp-scenario-desc">Loading…</div>25 unmodified lines<!-- LEFT: Naive agent --><div class="cmp-side naive-side"><div class="cmp-side-hd naive-hd"><span class="csh-badge">❌ Naive Agent</span><span class="csh-sub">Accepts everything — cascade inevitable</span></div><svg id="cmp-svg-naive" class="cmp-svg"></svg><div class="cmp-side-stats" id="naive-stats"></div><div class="cmp-step-display" id="naive-step-display"></div></div><!-- CENTER: Delta column --><div class="cmp-center"><div class="cmp-delta-title">IMPROVEMENT</div><div class="delta-row" id="d-reward"><div class="dr-lbl">Reward Δ</div><div class="dr-v" id="dv-reward">—</div></div><div class="delta-row" id="d-sat"><div class="dr-lbl">Health Δ</div><div class="dr-v" id="dv-sat">—</div></div><div class="delta-row" id="d-fail"><div class="dr-lbl">Failures Avoided</div><div class="dr-v" id="dv-fail">—</div></div><div class="delta-row" id="d-trust"><div class="dr-lbl">Trust Δ</div><div class="dr-v" id="dv-trust">—</div></div><div class="cmp-verdict" id="cmp-verdict"></div></div><!-- RIGHT: VERGIL agent --><div class="cmp-side vergil-side"><div class="cmp-side-hd vergil-hd"><span class="csh-badge">✅ VERGIL Agent</span><span class="csh-sub">Reasons through CDG before deciding</span></div><svg id="cmp-svg-vergil" class="cmp-svg"></svg><div class="cmp-side-stats" id="vergil-stats"></div><div class="cmp-step-display cmp-think-display" id="vergil-step-display"></div></div></div>
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801/* ═══════════════════════════════════════════════════════════VERGIL — Design System v4 (Theater Layout)═══════════════════════════════════════════════════════════ *//* ── Reset & Tokens ──────────────────────────────────────── */*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }:root {--bg: #080c18;--bg-panel: #0d1322;--bg-card: #111827;--bg-hover: #1a2236;--bg-input: #0b1020;--bg-topbar: rgba(8, 12, 24, 0.95);--border: hsla(220, 30%, 26%, 0.45);--border-hi: hsla(210, 80%, 55%, 0.5);--text-1: #e8ecf4;--text-2: #94a3b8;--text-3: #5b6b82;--blue: #3b82f6;--green: #22c55e;--red: #ef4444;--yellow: #eab308;--purple: #a855f7;--cyan: #06b6d4;--orange: #f97316;--r: 8px;--r-sm: 5px;--r-lg: 14px;--font: 'Inter', -apple-system, system-ui, sans-serif;--mono: 'JetBrains Mono', 'SF Mono', monospace;--topbar-h: 52px;--timeline-h: 56px;--conv-w: 290px;--metrics-w: 268px;}html, body {height: 100%;font-family: var(--font);font-size: 13px;background: var(--bg);color: var(--text-1);overflow: hidden;-webkit-font-smoothing: antialiased;}.hidden { display: none !important; }/* ══════════════════════════════════════════════════════════TOP BAR══════════════════════════════════════════════════════════ */#topbar {position: fixed; top: 0; left: 0; right: 0; z-index: 200;height: var(--topbar-h);display: flex; align-items: center; justify-content: space-between;padding: 0 16px;background: var(--bg-topbar);border-bottom: 1px solid var(--border);backdrop-filter: blur(16px);}.topbar-left { display: flex; align-items: center; gap: 12px; }.brand { display: flex; align-items: center; gap: 7px; }.brand-glyph {font-size: 22px;filter: drop-shadow(0 0 8px rgba(59,130,246,0.6));}.brand-name {font-size: 16px; font-weight: 800; letter-spacing: 2px;background: linear-gradient(135deg, #60a5fa, #a855f7);-webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;}.badge {font-family: var(--mono); font-size: 10px; font-weight: 600;padding: 3px 9px; border-radius: 50px;background: hsla(200,70%,50%,0.12);color: var(--cyan); border: 1px solid hsla(200,70%,50%,0.25);letter-spacing: 0.5px;}.topbar-stats { display: flex; align-items: center; gap: 4px; }.stat-pill {display: flex; align-items: center; gap: 6px;padding: 4px 12px;background: hsla(220,30%,14%,0.7);border: 1px solid var(--border);border-radius: 50px;}.sp-label {font-size: 9px; font-weight: 700; letter-spacing: 1.2px;text-transform: uppercase; color: var(--text-3);}.sp-val {font-family: var(--mono); font-size: 14px; font-weight: 700;color: var(--text-1);}.topbar-right { display: flex; align-items: center; gap: 8px; }.top-select {font-family: var(--mono); font-size: 11px;padding: 5px 10px;background: var(--bg-input); color: var(--text-2);border: 1px solid var(--border); border-radius: var(--r-sm);outline: none; cursor: pointer;}.top-select:focus { border-color: var(--border-hi); }.btn-primary {font-family: var(--font); font-size: 12px; font-weight: 600;padding: 6px 16px;background: var(--blue); color: #fff;border: none; border-radius: var(--r-sm); cursor: pointer;transition: background 180ms, transform 120ms;}.btn-primary:hover { background: #2563eb; transform: translateY(-1px); }.btn-primary:active { transform: translateY(0); }.btn-ghost {font-family: var(--font); font-size: 12px; font-weight: 500;padding: 6px 14px;background: transparent; color: var(--text-2);border: 1px solid var(--border); border-radius: var(--r-sm); cursor: pointer;transition: border-color 180ms, color 180ms;}.btn-ghost:hover { border-color: var(--border-hi); color: var(--text-1); }/* ══════════════════════════════════════════════════════════THREE-COLUMN THEATER══════════════════════════════════════════════════════════ */#theater {position: fixed;top: var(--topbar-h);bottom: var(--timeline-h);left: 0; right: 0;display: grid;grid-template-columns: var(--conv-w) 1fr var(--metrics-w);overflow: hidden;}/* ── CONV PANEL ─────────────────────────────────────────── */#conv-panel {display: flex; flex-direction: column;border-right: 1px solid var(--border);background: hsla(220,25%,7%,0.8);overflow: hidden;}#scenario-header {display: flex; align-items: flex-start; gap: 10px;padding: 12px 14px;border-bottom: 1px solid var(--border);flex-shrink: 0;background: hsla(220,30%,10%,0.6);}.sh-icon { font-size: 22px; margin-top: 1px; flex-shrink: 0; }.sh-title { font-size: 13px; font-weight: 600; color: var(--text-1); line-height: 1.4; }.sh-sub { font-size: 11px; color: var(--text-3); margin-top: 2px; line-height: 1.4; }#message-feed {flex: 1; overflow-y: auto; padding: 10px 10px 0;display: flex; flex-direction: column; gap: 8px;}#message-feed::-webkit-scrollbar { width: 4px; }#message-feed::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }.feed-empty {display: flex; flex-direction: column; align-items: center; justify-content: center;gap: 10px; height: 100%; color: var(--text-3); text-align: center;font-size: 12px; padding: 20px;}.fe-icon { font-size: 32px; opacity: 0.4; }/* Message types */.msg {border-radius: var(--r); padding: 9px 11px;font-size: 12px; line-height: 1.55;animation: msgIn 0.25s ease both;}@keyframes msgIn {from { opacity: 0; transform: translateY(8px); }to { opacity: 1; transform: translateY(0); }}.msg-system {background: hsla(220,30%,14%,0.5);color: var(--text-3); font-size: 11px; text-align: center;padding: 5px 8px;}.msg-stakeholder {background: hsla(210,70%,50%,0.08);border-left: 2px solid var(--blue);border-radius: 0 var(--r) var(--r) var(--r);}.msg-stakeholder .msg-from {font-size: 10px; font-weight: 700; text-transform: uppercase;letter-spacing: 0.8px; color: var(--cyan); margin-bottom: 4px;}.msg-stakeholder .msg-body { color: var(--text-2); }.msg-stakeholder .msg-meta {margin-top: 5px; font-size: 10px; color: var(--text-3);font-family: var(--mono);}.msg-think {background: hsla(270,60%,14%,0.6);border: 1px solid hsla(270,50%,40%,0.3);border-radius: var(--r);font-family: var(--mono); font-size: 10.5px;color: #c084fc;}.think-header {display: flex; align-items: center; gap: 6px;padding: 6px 10px; border-bottom: 1px solid hsla(270,40%,30%,0.3);font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: 1px;color: #a855f7;}.think-body {padding: 8px 10px;white-space: pre-wrap; word-break: break-word;color: #d8b4fe;line-height: 1.6;}.think-step {margin-bottom: 4px;}.think-step-label { color: #a855f7; font-weight: 600; }.msg-decision {background: hsla(142,60%,14%,0.5);border-left: 2px solid var(--green);border-radius: 0 var(--r) var(--r) var(--r);}.msg-decision.negative {background: hsla(0,60%,14%,0.5);border-left-color: var(--red);}.md-action {font-size: 13px; font-weight: 700; color: var(--green); margin-bottom: 3px;}.msg-decision.negative .md-action { color: var(--red); }.md-target { font-size: 11px; color: var(--text-2); }.md-reward {font-family: var(--mono); font-size: 11px;margin-top: 5px; padding-top: 5px;border-top: 1px solid hsla(142,40%,30%,0.3);color: var(--text-3);}.msg-alert {background: hsla(38,80%,14%,0.5);border-left: 2px solid var(--yellow);border-radius: 0 var(--r) var(--r) var(--r);color: var(--yellow); font-size: 11px;}.msg-cascade {background: hsla(0,70%,10%,0.8);border: 1px solid rgba(239,68,68,0.4);color: var(--red); text-align: center;animation: cascadeFlash 0.5s ease;}@keyframes cascadeFlash {0% { background: hsla(0,70%,20%,0.9); }100% { background: hsla(0,70%,10%,0.8); }}/* Conv footer */#conv-footer {flex-shrink: 0;padding: 10px 10px 12px;border-top: 1px solid var(--border);background: hsla(220,30%,8%,0.8);display: flex; flex-direction: column; gap: 8px;}.cf-label { font-size: 10px; font-weight: 700; text-transform: uppercase;letter-spacing: 1px; color: var(--text-3); }.node-select {width: 100%; font-family: var(--mono); font-size: 11px;padding: 6px 9px;background: var(--bg-input); color: var(--text-2);border: 1px solid var(--border); border-radius: var(--r-sm);outline: none; cursor: pointer;}.node-select:focus { border-color: var(--border-hi); }#manual-actions {display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; gap: 4px;}.ma-btn {font-family: var(--font); font-size: 11px; font-weight: 600;padding: 6px 0; border: 1px solid var(--border);border-radius: var(--r-sm); cursor: pointer;background: hsla(220,30%,14%,0.6); color: var(--text-2);transition: all 150ms; text-align: center;}.ma-btn:hover { background: var(--bg-hover); color: var(--text-1); border-color: var(--border-hi); }.ma-btn.accept:hover { border-color: var(--green); color: var(--green); }.ma-btn.decline:hover { border-color: var(--red); color: var(--red); }.ma-btn.counter:hover { border-color: var(--blue); color: var(--blue); }.ma-btn.wait:hover { border-color: var(--yellow); color: var(--yellow); }.ma-btn:disabled { opacity: 0.3; cursor: not-allowed; }.autoplay-btn {width: 100%; font-family: var(--font); font-size: 12px; font-weight: 600;padding: 7px; border: none; border-radius: var(--r-sm); cursor: pointer;background: linear-gradient(135deg, var(--blue), var(--purple));color: #fff; transition: opacity 180ms, transform 120ms;}.autoplay-btn:hover { opacity: 0.9; transform: translateY(-1px); }.autoplay-btn.playing {background: linear-gradient(135deg, var(--orange), var(--red));}/* ── GRAPH PANEL ────────────────────────────────────────── */#graph-panel {display: flex; flex-direction: column;overflow: hidden; position: relative;background: var(--bg);}#graph-header-bar {display: flex; align-items: center; justify-content: space-between;padding: 8px 16px;border-bottom: 1px solid var(--border);background: hsla(220,30%,8%,0.6);flex-shrink: 0;}.ghb-title {font-size: 11px; font-weight: 700; text-transform: uppercase;letter-spacing: 1px; color: var(--text-3);}.ghb-indicators { display: flex; gap: 12px; }.ghb-dot {font-family: var(--mono); font-size: 11px; color: var(--text-3);}#graph-area { flex: 1; position: relative; overflow: hidden; }#graph-svg { width: 100%; height: 100%; display: block; }#graph-empty {position: absolute; inset: 0;display: flex; flex-direction: column;align-items: center; justify-content: center; gap: 12px;text-align: center; padding: 40px;}.ge-glyph {font-size: 64px; opacity: 0.08;filter: drop-shadow(0 0 20px rgba(59,130,246,0.3));}.ge-title { font-size: 18px; font-weight: 700; color: var(--text-2); }.ge-sub { font-size: 13px; color: var(--text-3); line-height: 1.6; }.ge-btn {margin-top: 8px; font-size: 14px; font-weight: 600;padding: 10px 28px;background: linear-gradient(135deg, var(--blue), var(--purple));color: #fff; border: none; border-radius: 50px; cursor: pointer;transition: transform 150ms, box-shadow 150ms;box-shadow: 0 4px 16px rgba(59,130,246,0.3);}.ge-btn:hover { transform: translateY(-2px); box-shadow: 0 6px 24px rgba(59,130,246,0.45); }#graph-legend {display: flex; align-items: center; gap: 16px; flex-wrap: wrap;padding: 6px 16px;border-top: 1px solid var(--border);background: hsla(220,30%,8%,0.5);flex-shrink: 0;}.gl-item { display: flex; align-items: center; gap: 5px; font-size: 11px; color: var(--text-3); }.gl-dot { width: 9px; height: 9px; border-radius: 50%; }.gl-dot.pending { background: var(--yellow); box-shadow: 0 0 5px var(--yellow); }.gl-dot.accepted { background: var(--blue); box-shadow: 0 0 5px var(--blue); }.gl-dot.completed { background: var(--green); box-shadow: 0 0 5px var(--green); }.gl-dot.failed { background: var(--red); box-shadow: 0 0 5px var(--red); }.gl-line { width: 20px; height: 2px; }.gl-line.dep { background: var(--text-3); }.gl-line.conflict { background: var(--red); }/* ── D3 Graph Nodes ─────────────────────────────────────── */.node circle {cursor: pointer;transition: r 200ms, filter 200ms;}.node text {font-family: var(--font); font-size: 11px; font-weight: 600;fill: var(--text-1); pointer-events: none;text-anchor: middle; dominant-baseline: central;}.node .node-sublabel {font-size: 9px; font-weight: 400; fill: var(--text-3);}.node.status-pending circle {fill: hsla(45,90%,14%,0.9);stroke: var(--yellow); stroke-width: 2;filter: drop-shadow(0 0 5px rgba(234,179,8,0.4));animation: pulseNode 2s ease-in-out infinite;}@keyframes pulseNode {0%,100% { filter: drop-shadow(0 0 4px rgba(234,179,8,0.35)); }50% { filter: drop-shadow(0 0 12px rgba(234,179,8,0.7)); }}.node.status-accepted circle {fill: hsla(217,70%,14%,0.9);stroke: var(--blue); stroke-width: 2;filter: drop-shadow(0 0 6px rgba(59,130,246,0.5));}.node.status-completed circle {fill: hsla(142,60%,10%,0.9);stroke: var(--green); stroke-width: 2;filter: drop-shadow(0 0 5px rgba(34,197,94,0.4));}.node.status-failed circle {fill: hsla(0,70%,12%,0.95);stroke: var(--red); stroke-width: 2.5;filter: drop-shadow(0 0 8px rgba(239,68,68,0.6));animation: shakeFail 0.5s ease;}@keyframes shakeFail {0%,100% { transform: translate(0,0); }20% { transform: translate(-4px,0); }40% { transform: translate(4px,0); }60% { transform: translate(-3px,0); }80% { transform: translate(3px,0); }}.node.selected circle { stroke-width: 3 !important; }.node.status-pending.selected circle { stroke: #fde047; }.node.status-accepted.selected circle { stroke: #60a5fa; }.link {stroke-opacity: 0.5; stroke-width: 1.5;fill: none;}.link.dependency { stroke: var(--text-3); }.link.conflict { stroke: var(--red); stroke-dasharray: 4,3; stroke-opacity: 0.7; }.link.trust-impact { stroke: var(--purple); stroke-dasharray: 2,4; }/* Node urgency ring */.node .urgency-ring {fill: none; stroke-width: 1;stroke-dasharray: 2,2; opacity: 0.4;}/* ── METRICS PANEL ──────────────────────────────────────── */#metrics-panel {display: flex; flex-direction: column; gap: 8px;padding: 10px 10px;overflow-y: auto;border-left: 1px solid var(--border);background: hsla(220,25%,7%,0.8);}#metrics-panel::-webkit-scrollbar { width: 4px; }#metrics-panel::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }.mc {background: var(--bg-card);border: 1px solid var(--border);border-radius: var(--r);overflow: hidden;}.mc-title {display: flex; align-items: center; justify-content: space-between;padding: 8px 12px 6px;font-size: 10px; font-weight: 700; text-transform: uppercase;letter-spacing: 1px; color: var(--text-3);border-bottom: 1px solid var(--border);}.mc-subtitle {font-family: var(--mono); font-size: 10px; font-weight: 600;padding: 2px 6px; border-radius: 4px;background: hsla(142,50%,20%,0.3); color: var(--green);}/* Trust */#trust-list { padding: 8px 12px; display: flex; flex-direction: column; gap: 10px; }.trust-entry { display: flex; flex-direction: column; gap: 3px; }.te-header { display: flex; align-items: center; justify-content: space-between; }.te-name { font-size: 12px; font-weight: 600; color: var(--text-1); }.te-score { font-family: var(--mono); font-size: 12px; font-weight: 700; }.te-score.high { color: var(--green); }.te-score.medium { color: var(--yellow); }.te-score.low { color: var(--red); }.te-score.critical { color: #ff0033; animation: trustCrit 1s ease infinite; }@keyframes trustCrit {0%,100% { opacity: 1; } 50% { opacity: 0.5; }}.te-bar-track {height: 5px; background: hsla(220,30%,18%,0.8);border-radius: 3px; overflow: hidden;}.te-bar-fill {height: 100%; border-radius: 3px;transition: width 0.5s ease, background 0.3s ease;}.te-bar-fill.high { background: linear-gradient(90deg, var(--green), #16a34a); }.te-bar-fill.medium { background: linear-gradient(90deg, var(--yellow), #ca8a04); }.te-bar-fill.low { background: linear-gradient(90deg, var(--orange), var(--red)); }.te-bar-fill.critical { background: var(--red); }.te-dims {display: flex; gap: 6px; margin-top: 2px;}.te-dim {font-size: 9.5px; color: var(--text-3);font-family: var(--mono);}.te-dim span { color: var(--text-2); }/* Capacity */#capacity-display { padding: 10px 12px; display: flex; flex-direction: column; gap: 6px; }.cap-numbers {display: flex; align-items: baseline; gap: 4px;flex-wrap: wrap;}.cap-numbers > :first-child { font-family: var(--mono); font-size: 20px; font-weight: 700; color: var(--text-1); }.cap-slash { color: var(--text-3); font-size: 16px; }.cap-numbers > :nth-child(3) { font-family: var(--mono); font-size: 16px; color: var(--text-2); }.cap-label { font-size: 10px; color: var(--text-3); width: 100%; margin-top: -2px; }.cap-bar-track {height: 8px; background: hsla(220,30%,16%,0.8);border-radius: 4px; overflow: hidden;}.cap-bar-fill {height: 100%; border-radius: 4px;transition: width 0.6s cubic-bezier(0.4,0,0.2,1), background 0.4s ease;background: var(--green);}.cap-bar-fill.warn { background: linear-gradient(90deg, var(--yellow), var(--orange)); }.cap-bar-fill.crit { background: linear-gradient(90deg, var(--orange), var(--red)); animation: capFlash 0.8s ease infinite; }@keyframes capFlash {0%,100% { opacity: 1; } 50% { opacity: 0.7; }}.cap-legend {display: flex; gap: 10px;font-size: 9.5px; color: var(--text-3);}.cap-ok { color: var(--green); }.cap-warn { color: var(--yellow); }.cap-crit { color: var(--red); }/* Reward breakdown */#reward-display { padding: 8px 12px; }.rwd-empty { font-size: 11px; color: var(--text-3); padding: 4px 0; }.rwd-total {font-family: var(--mono); font-size: 24px; font-weight: 800;text-align: center; margin-bottom: 8px;transition: color 300ms;}.rwd-total.pos { color: var(--green); }.rwd-total.neg { color: var(--red); }.rwd-row {display: flex; align-items: center; justify-content: space-between;padding: 3px 0; border-bottom: 1px solid hsla(220,30%,18%,0.4);}.rwd-row:last-child { border: none; }.rwd-key { font-size: 11px; color: var(--text-2); }.rwd-val { font-family: var(--mono); font-size: 11px; }.rwd-val.pos { color: var(--green); }.rwd-val.neg { color: var(--red); }.rwd-val.zero { color: var(--text-3); }/* Target detail */#target-detail { padding: 10px 12px; }.td-empty { font-size: 11px; color: var(--text-3); }.td-name { font-size: 14px; font-weight: 700; color: var(--text-1); margin-bottom: 6px; }.td-row { display: flex; justify-content: space-between; padding: 3px 0;border-bottom: 1px solid hsla(220,30%,18%,0.3); font-size: 11px; }.td-row:last-child { border: none; }.td-k { color: var(--text-3); }.td-v { color: var(--text-1); font-family: var(--mono); font-weight: 600; }.td-status {display: inline-block; padding: 2px 8px; border-radius: 50px;font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.5px;}.td-status.pending { background: hsla(45,70%,20%,0.5); color: var(--yellow); }.td-status.accepted { background: hsla(217,70%,20%,0.5); color: var(--blue); }.td-status.completed{ background: hsla(142,60%,14%,0.5); color: var(--green); }.td-status.failed { background: hsla(0,60%,14%,0.5); color: var(--red); }/* Log */.mc-log { flex: 1; min-height: 80px; }#log-list { padding: 6px 10px; display: flex; flex-direction: column; gap: 4px;max-height: 160px; overflow-y: auto; }#log-list::-webkit-scrollbar { width: 3px; }#log-list::-webkit-scrollbar-thumb { background: var(--border); }.log-item {font-size: 11px; padding: 4px 6px; border-radius: var(--r-sm);animation: logIn 0.2s ease;border-left: 2px solid transparent;}@keyframes logIn { from { opacity:0; } to { opacity:1; } }.log-item.system { color: var(--text-3); border-left-color: var(--border); }.log-item.agent { color: var(--cyan); border-left-color: var(--cyan); }.log-item.success { color: var(--green); border-left-color: var(--green); }.log-item.danger { color: var(--red); border-left-color: var(--red); }.log-item.response{ color: var(--text-2); border-left-color: var(--purple); font-style: italic; }/* ══════════════════════════════════════════════════════════DECISION TIMELINE══════════════════════════════════════════════════════════ */#timeline-bar {position: fixed; bottom: 0; left: 0; right: 0;height: var(--timeline-h);display: flex; align-items: center; gap: 10px;padding: 0 16px;background: hsla(220,30%,8%,0.95);border-top: 1px solid var(--border);overflow-x: auto; overflow-y: hidden;z-index: 100;}#timeline-bar::-webkit-scrollbar { height: 3px; }#timeline-bar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }.tl-label {font-size: 9px; font-weight: 700; text-transform: uppercase;letter-spacing: 1.2px; color: var(--text-3); flex-shrink: 0;writing-mode: horizontal-tb;}#timeline-track { display: flex; align-items: center; gap: 4px; height: 100%; }.tl-step {display: flex; flex-direction: column; align-items: center; justify-content: center;gap: 2px; padding: 4px 8px; border-radius: var(--r-sm);border: 1px solid var(--border);background: hsla(220,30%,12%,0.6);cursor: default; flex-shrink: 0;animation: tlIn 0.2s ease;transition: border-color 150ms;min-width: 60px;}@keyframes tlIn { from { opacity:0; transform:scale(0.85); } to { opacity:1; transform:scale(1); } }.tl-step:hover { border-color: var(--border-hi); }.tl-step.accept { border-color: hsla(142,50%,40%,0.4); }.tl-step.decline { border-color: hsla(0,50%,40%,0.4); }.tl-step.counter { border-color: hsla(217,50%,45%,0.4); }.tl-step.do_nothing { opacity: 0.5; }.tl-icon { font-size: 14px; }.tl-label2 { font-size: 9px; color: var(--text-3); font-family: var(--mono); }.tl-reward {font-family: var(--mono); font-size: 9px; font-weight: 700;}.tl-reward.pos { color: var(--green); }.tl-reward.neg { color: var(--red); }.tl-connector {width: 16px; height: 1px;background: var(--border); flex-shrink: 0; opacity: 0.5;}/* ══════════════════════════════════════════════════════════COMPARE OVERLAY══════════════════════════════════════════════════════════ */#compare-overlay {position: fixed; inset: 0; z-index: 300;display: flex; flex-direction: column;background: var(--bg);animation: overlayIn 0.3s ease;}@keyframes overlayIn { from { opacity:0; } to { opacity:1; } }.cmp-topbar {display: flex; align-items: center; justify-content: space-between;padding: 10px 20px;border-bottom: 1px solid var(--border);background: var(--bg-topbar);flex-shrink: 0;}.cmp-scenario-info { display: flex; align-items: center; gap: 12px; }.cmp-scenario-icon { font-size: 24px; }.cmp-scenario-name { font-size: 15px; font-weight: 700; color: var(--text-1); }.cmp-scenario-desc { font-size: 11px; color: var(--text-3); margin-top: 2px; }.cmp-controls { display: flex; align-items: center; gap: 8px; }.cmp-step-btn {font-family: var(--mono); font-size: 16px; padding: 4px 12px; font-weight: 700;}.cmp-step-label { font-family: var(--mono); font-size: 12px; color: var(--text-2); min-width: 80px; text-align: center; }.cmp-auto-btn.playing { background: hsla(0,60%,20%,0.5); color: var(--red); border-color: var(--red); }.btn-close-cmp {font-size: 13px; padding: 5px 12px;background: hsla(0,60%,20%,0.4); color: var(--red);border: 1px solid hsla(0,60%,40%,0.4); border-radius: var(--r-sm); cursor: pointer;}.btn-close-cmp:hover { background: hsla(0,60%,25%,0.6); }.cmp-loading {flex: 1; display: flex; flex-direction: column;align-items: center; justify-content: center; gap: 16px;color: var(--text-3);}.cmp-spinner {width: 40px; height: 40px; border-radius: 50%;border: 3px solid var(--border);border-top-color: var(--blue);animation: spin 0.8s linear infinite;}@keyframes spin { to { transform: rotate(360deg); } }.cmp-body {flex: 1; display: grid;grid-template-columns: 1fr 140px 1fr;overflow: hidden;}.cmp-side { display: flex; flex-direction: column; overflow: hidden; }.cmp-side-header {padding: 10px 16px;display: flex; flex-direction: column; gap: 2px;border-bottom: 1px solid var(--border);flex-shrink: 0;}.naive-header { background: hsla(0,40%,10%,0.6); }.vergil-header { background: hsla(142,40%,10%,0.6); }.csh-badge {font-size: 14px; font-weight: 700;}.naive-header .csh-badge { color: var(--red); }.vergil-header .csh-badge { color: var(--green); }.csh-desc { font-size: 11px; color: var(--text-3); }.cmp-svg { flex: 1; display: block; }.cmp-side-stats {display: flex; gap: 16px; padding: 8px 16px;border-top: 1px solid var(--border);flex-shrink: 0;font-family: var(--mono); font-size: 11px;}.css-stat { display: flex; flex-direction: column; gap: 1px; }.css-label { font-size: 9px; text-transform: uppercase; letter-spacing: 0.8px; color: var(--text-3); }.css-val { font-weight: 700; color: var(--text-1); }.cmp-side-step {padding: 8px 14px;font-size: 11px; color: var(--text-2); line-height: 1.5;border-top: 1px solid var(--border);min-height: 60px; max-height: 80px; overflow-y: auto;flex-shrink: 0;background: hsla(220,30%,8%,0.6);}.cmp-think-block {font-family: var(--mono); font-size: 10px; color: #c084fc;background: hsla(270,40%,10%,0.5);}/* Compare center column */.cmp-center {border-left: 1px solid var(--border);border-right: 1px solid var(--border);display: flex; flex-direction: column;align-items: center; justify-content: center;gap: 14px; padding: 20px 12px;background: hsla(220,25%,9%,0.8);}.cmp-delta-title {font-size: 9px; font-weight: 800; text-transform: uppercase;letter-spacing: 1.5px; color: var(--text-3); margin-bottom: 4px;}.delta-row {width: 100%; text-align: center;padding: 10px 8px;background: var(--bg-card);border: 1px solid var(--border); border-radius: var(--r);}.dr-label { font-size: 9px; text-transform: uppercase; letter-spacing: 0.8px; color: var(--text-3); margin-bottom: 4px; }.dr-val {font-family: var(--mono); font-size: 16px; font-weight: 800;color: var(--text-2);}.dr-val.better { color: var(--green); }.dr-val.worse { color: var(--red); }.cmp-verdict {width: 100%; text-align: center; padding: 10px 8px;background: hsla(142,40%,12%,0.5);border: 1px solid hsla(142,40%,30%,0.3);border-radius: var(--r);font-size: 12px; font-weight: 600; color: var(--green); line-height: 1.5;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773/* ═══════════════════════════════════════════════════════════VERGIL — Design System v5 (Senior UX Rebuild)═══════════════════════════════════════════════════════════ */*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }:root {/* Slate-based dark — readable contrast, not pure black */--bg: #0f172a;--bg-panel: #1e293b;--bg-card: #1e293b;--bg-card-hi:#253047;--bg-topbar: rgba(15,23,42,0.96);--bg-input: #131f35;--border: #2d3f58;--border-hi: #4c6078;/* Text */--t1: #f1f5f9;--t2: #94a3b8;--t3: #64748b;--t4: #475569;/* Status — semantic, not alarming */--s-pending: #818cf8; /* indigo */--s-accepted: #38bdf8; /* sky */--s-completed: #34d399; /* emerald */--s-failed: #fb7185; /* rose */--s-at-risk: #fbbf24; /* amber *//* KPI accent strip */--kpi-fulfill: #34d399;--kpi-trust: #60a5fa;--kpi-cascade: #fb7185;--kpi-health: #a78bfa;/* Brand */--brand: #6366f1;--brand2: #8b5cf6;--r: 10px;--r-sm: 6px;--r-lg: 16px;--font: 'Inter', -apple-system, system-ui, sans-serif;--mono: 'JetBrains Mono', 'SF Mono', monospace;--topbar-h: 52px;--kpi-h: 72px;--timeline-h:60px;--conv-w: 272px;--metrics-w: 256px;}html, body {height: 100%; overflow: hidden;font-family: var(--font); font-size: 13px;background: var(--bg); color: var(--t1);-webkit-font-smoothing: antialiased;}.hidden { display: none !important; }/* ══════════════════════════════════════════════TOP BAR══════════════════════════════════════════════ */#topbar {position: fixed; top: 0; left: 0; right: 0; z-index: 200;height: var(--topbar-h);display: flex; align-items: center; justify-content: space-between;padding: 0 20px;background: var(--bg-topbar);border-bottom: 1px solid var(--border);backdrop-filter: blur(20px);}.brand { display: flex; align-items: center; gap: 8px; }.brand-glyph {width: 30px; height: 30px;display: flex; align-items: center; justify-content: center;background: linear-gradient(135deg, var(--brand), var(--brand2));border-radius: 8px;font-size: 17px; font-weight: 900; color: #fff;box-shadow: 0 0 16px rgba(99,102,241,0.35);}.brand-name {font-size: 17px; font-weight: 800; letter-spacing: 2.5px;color: var(--t1);}.brand-version {font-size: 10px; font-weight: 600; letter-spacing: 1px;padding: 2px 8px; border-radius: 50px;background: rgba(99,102,241,0.15); color: var(--brand);border: 1px solid rgba(99,102,241,0.3);}.topbar-center {display: flex; align-items: center; gap: 6px;}.stat-chip {display: flex; align-items: center; gap: 7px;padding: 5px 14px;background: var(--bg-panel);border: 1px solid var(--border);border-radius: 50px;}.sc-label {font-size: 9px; font-weight: 700; text-transform: uppercase;letter-spacing: 1.2px; color: var(--t3);}.sc-val {font-family: var(--mono); font-size: 15px; font-weight: 700; color: var(--t1);}.topbar-right { display: flex; align-items: center; gap: 8px; }.top-select {font-family: var(--font); font-size: 12px;padding: 6px 10px;background: var(--bg-input); color: var(--t2);border: 1px solid var(--border); border-radius: var(--r-sm);outline: none; cursor: pointer; max-width: 160px;}.top-select:focus { border-color: var(--border-hi); }.btn-primary {font-size: 12px; font-weight: 600; padding: 7px 18px;background: var(--brand); color: #fff;border: none; border-radius: var(--r-sm); cursor: pointer;transition: all 160ms;}.btn-primary:hover { background: #4f46e5; transform: translateY(-1px); }.btn-ghost {font-size: 12px; font-weight: 500; padding: 7px 14px;background: transparent; color: var(--t2);border: 1px solid var(--border); border-radius: var(--r-sm); cursor: pointer;transition: all 160ms;}.btn-ghost:hover { border-color: var(--border-hi); color: var(--t1); }/* ══════════════════════════════════════════════KPI STRIP══════════════════════════════════════════════ */#kpi-strip {position: fixed; top: var(--topbar-h); left: 0; right: 0; z-index: 190;height: var(--kpi-h);display: grid; grid-template-columns: repeat(4, 1fr);border-bottom: 1px solid var(--border);background: var(--bg-panel);}.kpi-card {display: flex; flex-direction: column; justify-content: center;padding: 10px 20px;border-right: 1px solid var(--border);position: relative; overflow: hidden;}.kpi-card:last-child { border-right: none; }.kpi-card::before {content: ''; position: absolute;bottom: 0; left: 0; right: 0; height: 3px;}.kpi-card.fulfill::before { background: var(--kpi-fulfill); }.kpi-card.trust::before { background: var(--kpi-trust); }.kpi-card.cascade::before { background: var(--kpi-cascade); }.kpi-card.health::before { background: var(--kpi-health); }.kpi-label {font-size: 9px; font-weight: 700; text-transform: uppercase;letter-spacing: 1.4px; color: var(--t3); margin-bottom: 3px;}.kpi-row { display: flex; align-items: baseline; gap: 10px; }.kpi-val {font-family: var(--mono); font-size: 26px; font-weight: 800;line-height: 1;}.kpi-card.fulfill .kpi-val { color: var(--kpi-fulfill); }.kpi-card.trust .kpi-val { color: var(--kpi-trust); }.kpi-card.cascade .kpi-val { color: var(--kpi-cascade); }.kpi-card.health .kpi-val { color: var(--kpi-health); }.kpi-sub {font-size: 10px; color: var(--t3); line-height: 1;}.kpi-delta {font-size: 10px; font-weight: 700; font-family: var(--mono);}.kpi-delta.up { color: var(--s-completed); }.kpi-delta.down { color: var(--s-failed); }/* ══════════════════════════════════════════════THEATER LAYOUT══════════════════════════════════════════════ */#theater {position: fixed;top: calc(var(--topbar-h) + var(--kpi-h));bottom: var(--timeline-h);left: 0; right: 0;display: grid;grid-template-columns: var(--conv-w) 1fr var(--metrics-w);overflow: hidden;}/* ── LEFT: Conversation Panel ─────────────────── */#conv-panel {display: flex; flex-direction: column;border-right: 1px solid var(--border);background: var(--bg);overflow: hidden;}#scenario-header {padding: 12px 14px;border-bottom: 1px solid var(--border);background: var(--bg-panel);flex-shrink: 0;}.sh-eyebrow {font-size: 9px; font-weight: 700; text-transform: uppercase;letter-spacing: 1.4px; color: var(--t3); margin-bottom: 4px;}.sh-title { font-size: 13px; font-weight: 700; color: var(--t1); line-height: 1.3; margin-bottom: 2px; }.sh-sub { font-size: 11px; color: var(--t3); }#message-feed {flex: 1; overflow-y: auto;padding: 10px 10px 0; display: flex; flex-direction: column; gap: 8px;}#message-feed::-webkit-scrollbar { width: 3px; }#message-feed::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }.feed-empty {display: flex; flex-direction: column; align-items: center; justify-content: center;gap: 10px; height: 100%; color: var(--t4); text-align: center;font-size: 12px; padding: 20px;}.fe-icon { font-size: 28px; opacity: 0.3; }.fe-text { line-height: 1.6; }/* Messages */.msg { border-radius: var(--r); padding: 10px 12px;font-size: 12px; line-height: 1.55; animation: msgIn 0.2s ease both; }@keyframes msgIn { from { opacity:0; transform:translateY(6px); } to { opacity:1; transform:none; } }.msg-system { background: rgba(255,255,255,0.03); color: var(--t3);font-size: 11px; text-align: center; padding: 5px 8px; }.msg-stakeholder {background: rgba(56,189,248,0.06);border-left: 2px solid var(--s-accepted);border-radius: 0 var(--r) var(--r) var(--r);}.msg-from {font-size: 10px; font-weight: 700; text-transform: uppercase;letter-spacing: 0.8px; color: var(--s-accepted); margin-bottom: 4px;}.msg-body { color: var(--t2); }.msg-meta { margin-top: 5px; font-size: 10px; color: var(--t3); font-family: var(--mono); }.msg-think {background: rgba(139,92,246,0.07);border: 1px solid rgba(139,92,246,0.2);border-radius: var(--r);}.think-header {display: flex; align-items: center; gap: 6px;padding: 7px 10px; border-bottom: 1px solid rgba(139,92,246,0.2);font-size: 10px; font-weight: 700; text-transform: uppercase;letter-spacing: 1px; color: var(--brand2);}.think-body {padding: 8px 10px;font-family: var(--mono); font-size: 10.5px; color: #c4b5fd;white-space: pre-wrap; word-break: break-word; line-height: 1.6;}.msg-decision {border-left: 2px solid var(--s-completed);background: rgba(52,211,153,0.05);border-radius: 0 var(--r) var(--r) var(--r);}.msg-decision.neg {border-left-color: var(--s-failed);background: rgba(251,113,133,0.05);}.md-action { font-size: 13px; font-weight: 700; color: var(--s-completed); margin-bottom: 2px; }.msg-decision.neg .md-action { color: var(--s-failed); }.md-target { font-size: 11px; color: var(--t2); }.md-reward { margin-top: 5px; padding-top: 5px;border-top: 1px solid rgba(52,211,153,0.15);font-family: var(--mono); font-size: 10px; color: var(--t3); }.msg-alert {background: rgba(251,191,36,0.07);border-left: 2px solid var(--s-at-risk);border-radius: 0 var(--r) var(--r) var(--r);color: var(--s-at-risk); font-size: 11px;}.msg-cascade {background: rgba(251,113,133,0.1);border: 1px solid rgba(251,113,133,0.3);color: var(--s-failed); text-align: center; font-size: 11px;animation: flashRed 0.5s ease;}@keyframes flashRed { 0% { background:rgba(251,113,133,0.25); } 100% { background:rgba(251,113,133,0.1); } }/* Conv footer */#conv-footer {flex-shrink: 0; padding: 10px 10px 12px;border-top: 1px solid var(--border);background: var(--bg-panel);display: flex; flex-direction: column; gap: 8px;}.cf-label {font-size: 9px; font-weight: 700; text-transform: uppercase;letter-spacing: 1.2px; color: var(--t3);}.node-select {width: 100%; font-family: var(--mono); font-size: 11px;padding: 6px 9px; background: var(--bg-input); color: var(--t2);border: 1px solid var(--border); border-radius: var(--r-sm);outline: none; cursor: pointer;}.node-select:focus { border-color: var(--border-hi); }#manual-actions {display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; gap: 4px;}.ma-btn {font-family: var(--font); font-size: 11px; font-weight: 600;padding: 7px 0; border: 1px solid var(--border);border-radius: var(--r-sm); cursor: pointer;background: rgba(255,255,255,0.03); color: var(--t2);transition: all 140ms; text-align: center;}.ma-btn:hover { background: var(--bg-card-hi); color: var(--t1); }.ma-btn.accept:hover { border-color: var(--s-completed); color: var(--s-completed); }.ma-btn.decline:hover { border-color: var(--s-failed); color: var(--s-failed); }.ma-btn.counter:hover { border-color: var(--s-accepted); color: var(--s-accepted); }.ma-btn.wait:hover { border-color: var(--s-at-risk); color: var(--s-at-risk); }.ma-btn:disabled { opacity: 0.3; cursor: not-allowed; }.autoplay-btn {width: 100%; font-size: 12px; font-weight: 600; padding: 8px;border: none; border-radius: var(--r-sm); cursor: pointer;background: linear-gradient(135deg, var(--brand), var(--brand2));color: #fff; transition: opacity 160ms, transform 120ms;}.autoplay-btn:hover { opacity: 0.9; transform: translateY(-1px); }.autoplay-btn.playing { background: linear-gradient(135deg,#f43f5e,#dc2626); }/* ── CENTER: Graph Panel ──────────────────────── */#graph-panel {display: flex; flex-direction: column;background: var(--bg); overflow: hidden; position: relative;}#graph-header-bar {display: flex; align-items: center; justify-content: space-between;padding: 8px 18px;border-bottom: 1px solid var(--border);background: var(--bg-panel); flex-shrink: 0;}.ghb-title {font-size: 10px; font-weight: 700; text-transform: uppercase;letter-spacing: 1.4px; color: var(--t3);}.ghb-chips { display: flex; gap: 8px; }.ghb-chip {font-size: 10px; font-family: var(--mono);padding: 2px 10px; border-radius: 50px;border: 1px solid transparent;}.ghb-chip.pending { color: var(--s-pending); border-color: rgba(129,140,248,0.3); background: rgba(129,140,248,0.08); }.ghb-chip.active { color: var(--s-accepted); border-color: rgba(56,189,248,0.3); background: rgba(56,189,248,0.08); }.ghb-chip.completed { color: var(--s-completed); border-color: rgba(52,211,153,0.3); background: rgba(52,211,153,0.08); }.ghb-chip.failed { color: var(--s-failed); border-color: rgba(251,113,133,0.3); background: rgba(251,113,133,0.08); }#graph-area { flex: 1; position: relative; overflow: hidden; }#graph-svg { width: 100%; height: 100%; display: block; }#graph-empty {position: absolute; inset: 0;display: flex; flex-direction: column; align-items: center; justify-content: center;gap: 14px; text-align: center; padding: 40px;pointer-events: none;}.ge-glyph {font-size: 72px; font-weight: 900; color: var(--brand);opacity: 0.06; line-height: 1;filter: blur(2px);}.ge-title { font-size: 20px; font-weight: 700; color: var(--t2); pointer-events: auto; }.ge-sub { font-size: 13px; color: var(--t3); line-height: 1.7; }.ge-btn {pointer-events: auto;margin-top: 6px; font-size: 14px; font-weight: 700;padding: 11px 32px;background: linear-gradient(135deg, var(--brand), var(--brand2));color: #fff; border: none; border-radius: 50px; cursor: pointer;box-shadow: 0 4px 20px rgba(99,102,241,0.4);transition: transform 150ms, box-shadow 150ms;}.ge-btn:hover { transform: translateY(-2px); box-shadow: 0 8px 28px rgba(99,102,241,0.55); }#graph-legend {display: flex; align-items: center; gap: 18px; flex-wrap: wrap;padding: 7px 18px;border-top: 1px solid var(--border);background: var(--bg-panel); flex-shrink: 0;}.gl-item { display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--t3); }.gl-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }.gl-dot.pending { background: var(--s-pending); box-shadow: 0 0 6px var(--s-pending); }.gl-dot.accepted { background: var(--s-accepted); box-shadow: 0 0 6px var(--s-accepted); }.gl-dot.completed { background: var(--s-completed); box-shadow: 0 0 6px var(--s-completed); }.gl-dot.failed { background: var(--s-failed); box-shadow: 0 0 6px var(--s-failed); }.gl-dot.at-risk { background: var(--s-at-risk); box-shadow: 0 0 6px var(--s-at-risk); }.gl-line { width: 22px; height: 2px; flex-shrink: 0; }.gl-line.dep { background: var(--t4); }.gl-line.conflict { background: var(--s-failed); }/* D3 Node styles */.node { cursor: pointer; }.node .node-bg {transition: r 200ms;}.node .node-ring {fill: none; stroke-width: 2;transition: stroke 300ms;}.node .node-letter {font-family: var(--font); font-size: 13px; font-weight: 800;text-anchor: middle; dominant-baseline: central;pointer-events: none;}.node .node-label {font-family: var(--font); font-size: 10px; font-weight: 600;fill: var(--t2); text-anchor: middle;pointer-events: none;}.node .node-deadline {font-family: var(--mono); font-size: 9px;fill: var(--t3); text-anchor: middle;pointer-events: none;}.node.selected .node-ring { stroke-width: 3; }.node .node-pulse { fill: none; stroke-width: 1; opacity: 0; }/* Status-specific fills */.node.pending .node-bg { fill: rgba(129,140,248,0.1); }.node.accepted .node-bg { fill: rgba(56,189,248,0.08); }.node.completed .node-bg { fill: rgba(52,211,153,0.08); }.node.failed .node-bg { fill: rgba(251,113,133,0.1); }.node.pending .node-ring { stroke: var(--s-pending); }.node.accepted .node-ring { stroke: var(--s-accepted); }.node.completed .node-ring { stroke: var(--s-completed); }.node.failed .node-ring { stroke: var(--s-failed); }.node.pending .node-letter { fill: var(--s-pending); }.node.accepted .node-letter { fill: var(--s-accepted); }.node.completed .node-letter { fill: var(--s-completed); }.node.failed .node-letter { fill: var(--s-failed); }.node.pending .node-pulse {stroke: var(--s-pending);animation: nodeBreath 2.2s ease-in-out infinite;}@keyframes nodeBreath {0%,100% { r: 26px; opacity: 0; }50% { r: 34px; opacity: 0.25; }}.node.failed .node-ring { animation: failShake 0.5s ease; }@keyframes failShake {0%,100% { transform: translate(0,0); }20% { transform: translate(-4px,0); }40% { transform: translate(4px,0); }60% { transform: translate(-3px,0); }80% { transform: translate(3px,0); }}/* Edges */.edge { fill: none; }.edge.dependency { stroke: var(--t4); stroke-width: 1.5; stroke-dasharray: 6,3; opacity: 0.7; }.edge.conflict { stroke: var(--s-failed); stroke-width: 1.5; stroke-dasharray: 4,3; opacity: 0.6; }.edge.trust-impact { stroke: var(--brand2); stroke-width: 1; stroke-dasharray: 2,4; opacity: 0.5; }/* ── RIGHT: Metrics Panel ─────────────────────── */#metrics-panel {display: flex; flex-direction: column; gap: 0;overflow-y: auto; border-left: 1px solid var(--border);background: var(--bg);}#metrics-panel::-webkit-scrollbar { width: 3px; }#metrics-panel::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }.mc {border-bottom: 1px solid var(--border);overflow: hidden; flex-shrink: 0;}.mc:last-child { border-bottom: none; flex: 1; }.mc-hd {display: flex; align-items: center; justify-content: space-between;padding: 9px 14px 8px;font-size: 9px; font-weight: 700; text-transform: uppercase;letter-spacing: 1.4px; color: var(--t3);background: var(--bg-panel);border-bottom: 1px solid var(--border);}.mc-badge {font-family: var(--mono); font-size: 10px; font-weight: 700;padding: 2px 7px; border-radius: 4px;}.mc-badge.green { background: rgba(52,211,153,0.15); color: var(--s-completed); }.mc-badge.blue { background: rgba(56,189,248,0.15); color: var(--s-accepted); }.mc-badge.red { background: rgba(251,113,133,0.15); color: var(--s-failed); }.mc-badge.purple { background: rgba(139,92,246,0.15); color: var(--brand2); }/* Trust */#trust-list { padding: 10px 14px; display: flex; flex-direction: column; gap: 12px; }.te { display: flex; flex-direction: column; gap: 4px; }.te-row1 { display: flex; align-items: center; justify-content: space-between; }.te-name { font-size: 12px; font-weight: 600; color: var(--t1); }.te-score-wrap { display: flex; align-items: baseline; gap: 5px; }.te-score {font-family: var(--mono); font-size: 14px; font-weight: 800;}.te-score.hi { color: var(--s-completed); }.te-score.mid { color: var(--s-at-risk); }.te-score.lo { color: var(--s-failed); }.te-delta {font-family: var(--mono); font-size: 10px; font-weight: 600;}.te-delta.up { color: var(--s-completed); }.te-delta.dn { color: var(--s-failed); }.te-delta.neu { color: var(--t3); }.te-track {height: 5px; background: rgba(255,255,255,0.06);border-radius: 3px; overflow: hidden;}.te-fill {height: 100%; border-radius: 3px;transition: width 0.5s ease, background 0.3s ease;}.te-fill.hi { background: linear-gradient(90deg, var(--s-completed), #059669); }.te-fill.mid { background: linear-gradient(90deg, var(--s-at-risk), #d97706); }.te-fill.lo { background: linear-gradient(90deg, #f97316, var(--s-failed)); }.te-dims {display: flex; gap: 8px;}.te-dim { font-size: 9.5px; color: var(--t3); font-family: var(--mono); }.te-dim span { color: var(--t2); }/* Capacity */#capacity-display { padding: 12px 14px; display: flex; flex-direction: column; gap: 8px; }.cap-header { display: flex; align-items: baseline; gap: 6px; }.cap-val { font-family: var(--mono); font-size: 22px; font-weight: 800; color: var(--t1); }.cap-sep { color: var(--t4); font-size: 14px; }.cap-of { font-family: var(--mono); font-size: 14px; color: var(--t2); }.cap-unit { font-size: 10px; color: var(--t3); }.cap-track {height: 8px; background: rgba(255,255,255,0.06);border-radius: 4px; overflow: hidden;}.cap-fill {height: 100%; border-radius: 4px;transition: width 0.6s cubic-bezier(.4,0,.2,1), background 0.3s ease;background: var(--s-completed);}.cap-fill.warn { background: linear-gradient(90deg, var(--s-at-risk), #d97706); }.cap-fill.crit { background: linear-gradient(90deg, #f97316, var(--s-failed));animation: capPulse 0.9s ease infinite; }@keyframes capPulse { 0%,100%{opacity:1;} 50%{opacity:0.65;} }.cap-zones { display: flex; justify-content: space-between; font-size: 9px; color: var(--t4); }/* Reward breakdown */#reward-display { padding: 10px 14px; }.rwd-empty { font-size: 11px; color: var(--t3); }.rwd-total {font-family: var(--mono); font-size: 28px; font-weight: 800;text-align: center; margin-bottom: 10px;transition: color 300ms;}.rwd-total.pos { color: var(--s-completed); }.rwd-total.neg { color: var(--s-failed); }.rwd-row {display: flex; justify-content: space-between; align-items: center;padding: 3px 0; border-bottom: 1px solid rgba(255,255,255,0.04);font-size: 11px;}.rwd-row:last-child { border: none; }.rwd-k { color: var(--t2); }.rwd-v { font-family: var(--mono); font-size: 11px; }.rwd-v.pos { color: var(--s-completed); }.rwd-v.neg { color: var(--s-failed); }.rwd-v.zero { color: var(--t4); }/* Target detail */#target-detail { padding: 10px 14px; }.td-empty { font-size: 11px; color: var(--t3); }.td-name { font-size: 14px; font-weight: 700; color: var(--t1); margin-bottom: 8px; }.td-row { display: flex; justify-content: space-between; padding: 4px 0;border-bottom: 1px solid rgba(255,255,255,0.04); font-size: 11px; }.td-row:last-child { border: none; }.td-k { color: var(--t3); }.td-v { color: var(--t1); font-family: var(--mono); font-weight: 600; }.td-badge {display: inline-block; padding: 2px 8px; border-radius: 50px;font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.5px;}.td-badge.pending { background: rgba(129,140,248,0.15); color: var(--s-pending); }.td-badge.accepted { background: rgba(56,189,248,0.12); color: var(--s-accepted); }.td-badge.completed { background: rgba(52,211,153,0.12); color: var(--s-completed); }.td-badge.failed { background: rgba(251,113,133,0.12); color: var(--s-failed); }/* Log */#log-list { padding: 6px 10px; overflow-y: auto; max-height: 130px; display: flex; flex-direction: column; gap: 3px; }#log-list::-webkit-scrollbar { width: 3px; }#log-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }.log-item { font-size: 11px; padding: 3px 7px; border-radius: var(--r-sm);border-left: 2px solid transparent; animation: logIn 0.2s ease; }@keyframes logIn { from{opacity:0;} to{opacity:1;} }.log-item.system { color: var(--t3); border-left-color: var(--border); }.log-item.agent { color: var(--s-accepted); border-left-color: var(--s-accepted); }.log-item.success { color: var(--s-completed); border-left-color: var(--s-completed); }.log-item.danger { color: var(--s-failed); border-left-color: var(--s-failed); }.log-item.response{ color: var(--t2); border-left-color: var(--brand2); font-style: italic; }/* ═══════════════════════════════════════════��══DECISION TIMELINE══════════════════════════════════════════════ */#timeline-bar {position: fixed; bottom: 0; left: 0; right: 0; z-index: 100;height: var(--timeline-h);display: flex; align-items: center; gap: 12px; padding: 0 18px;background: var(--bg-panel); border-top: 1px solid var(--border);overflow-x: auto; overflow-y: hidden;}#timeline-bar::-webkit-scrollbar { height: 3px; }#timeline-bar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }.tl-label { font-size: 9px; font-weight: 700; text-transform: uppercase;letter-spacing: 1.2px; color: var(--t3); flex-shrink: 0; }#timeline-track { display: flex; align-items: center; gap: 4px; height: 100%; }.tl-step {display: flex; flex-direction: column; align-items: center; justify-content: center;gap: 1px; padding: 5px 10px; border-radius: var(--r-sm);border: 1px solid var(--border); background: rgba(255,255,255,0.02);cursor: default; flex-shrink: 0; animation: tlIn 0.2s ease;transition: border-color 140ms; min-width: 58px;}@keyframes tlIn { from{opacity:0;transform:scale(0.85);} to{opacity:1;transform:scale(1);} }.tl-step.accept { border-color: rgba(52,211,153,0.35); }.tl-step.decline { border-color: rgba(251,113,133,0.35); }.tl-step.counter { border-color: rgba(56,189,248,0.35); }.tl-step.do_nothing { opacity: 0.45; }.tl-icon { font-size: 14px; line-height: 1; }.tl-num { font-size: 8px; color: var(--t4); font-family: var(--mono); }.tl-r { font-family: var(--mono); font-size: 9px; font-weight: 700; }.tl-r.pos { color: var(--s-completed); }.tl-r.neg { color: var(--s-failed); }.tl-conn { width: 14px; height: 1px; background: var(--border); flex-shrink: 0; opacity: 0.4; }/* ══════════════════════════════════════════════COMPARE OVERLAY══════════════════════════════════════════════ */#compare-overlay {position: fixed; inset: 0; z-index: 300;display: flex; flex-direction: column;background: var(--bg);animation: fadeIn 0.25s ease;}@keyframes fadeIn { from{opacity:0;} to{opacity:1;} }.cmp-topbar {display: flex; align-items: center; justify-content: space-between;padding: 12px 20px;border-bottom: 1px solid var(--border);background: var(--bg-panel); flex-shrink: 0;}.cmp-scenario-info { display: flex; align-items: center; gap: 12px; }.cmp-icon { font-size: 26px; }.cmp-scenario-name { font-size: 16px; font-weight: 800; color: var(--t1); }.cmp-scenario-desc { font-size: 11px; color: var(--t3); margin-top: 1px; }.cmp-controls { display: flex; align-items: center; gap: 8px; }.cmp-step-label { font-family: var(--mono); font-size: 12px; color: var(--t2); min-width: 84px; text-align: center; }.cmp-step-btn { padding: 5px 14px; font-family: var(--mono); font-size: 16px; font-weight: 700; }.btn-close-cmp {font-size: 12px; padding: 6px 14px;background: rgba(251,113,133,0.1); color: var(--s-failed);border: 1px solid rgba(251,113,133,0.3); border-radius: var(--r-sm); cursor: pointer;}.btn-close-cmp:hover { background: rgba(251,113,133,0.2); }.cmp-loading {flex: 1; display: flex; flex-direction: column;align-items: center; justify-content: center; gap: 16px; color: var(--t3);}.cmp-spinner {width: 40px; height: 40px; border-radius: 50%;border: 3px solid var(--border); border-top-color: var(--brand);animation: spin 0.8s linear infinite;}@keyframes spin { to{transform:rotate(360deg);} }.cmp-body {flex: 1; display: grid; grid-template-columns: 1fr 130px 1fr; overflow: hidden;}.cmp-side { display: flex; flex-direction: column; overflow: hidden; }.cmp-side-hd {padding: 10px 18px; flex-shrink: 0;border-bottom: 1px solid var(--border);display: flex; flex-direction: column; gap: 2px;}.naive-hd { background: rgba(251,113,133,0.06); }.vergil-hd { background: rgba(52,211,153,0.06); }.csh-badge { font-size: 14px; font-weight: 800; }.naive-hd .csh-badge { color: var(--s-failed); }.vergil-hd .csh-badge { color: var(--s-completed); }.csh-sub { font-size: 11px; color: var(--t3); }.cmp-svg { flex: 1; display: block; }.cmp-side-stats {display: flex; gap: 16px; padding: 8px 16px;border-top: 1px solid var(--border); flex-shrink: 0;font-family: var(--mono); font-size: 11px;}.css-stat { display: flex; flex-direction: column; gap: 1px; }.css-lbl { font-size: 9px; text-transform: uppercase; letter-spacing: 0.8px; color: var(--t3); }.css-v { font-weight: 700; color: var(--t1); }.cmp-step-display {padding: 8px 14px; font-size: 11px; color: var(--t2); line-height: 1.5;border-top: 1px solid var(--border); min-height: 58px; max-height: 80px;overflow-y: auto; flex-shrink: 0; background: rgba(255,255,255,0.02);}.cmp-think-display {font-family: var(--mono); font-size: 10px; color: #c4b5fd;background: rgba(139,92,246,0.07);}.cmp-center {border-left: 1px solid var(--border);border-right: 1px solid var(--border);display: flex; flex-direction: column;align-items: center; justify-content: center;gap: 10px; padding: 16px 10px;background: var(--bg-panel);}.cmp-delta-title {font-size: 9px; font-weight: 800; text-transform: uppercase;letter-spacing: 1.5px; color: var(--t3); margin-bottom: 2px;}.delta-row {width: 100%; text-align: center; padding: 9px 6px;background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--r);}.dr-lbl { font-size: 9px; text-transform: uppercase; letter-spacing: 0.8px; color: var(--t3); margin-bottom: 3px; }.dr-v { font-family: var(--mono); font-size: 17px; font-weight: 800; color: var(--t2); }.dr-v.better { color: var(--s-completed); }.dr-v.worse { color: var(--s-failed); }.cmp-verdict {width: 100%; text-align: center; padding: 9px 6px;background: rgba(52,211,153,0.08); border: 1px solid rgba(52,211,153,0.2);border-radius: var(--r); font-size: 11px; font-weight: 600;color: var(--s-completed); line-height: 1.5;}
47 unmodified lines4849505152535455562 unmodified lines59606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481494 unmodified lines15415515615715815916016116216316416516616716816917017117217317417517617717817950 unmodified lines2302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363375 unmodified lines3433443453463473483493503513523532 unmodified lines35635735835936036136236336436536636736836937037137237337416 unmodified lines3913923933943953963973983994004014024034046 unmodified lines41141241341441541641741818 unmodified lines43743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449510 unmodified lines50650750850951051151251351451551651751823 unmodified lines5425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216224 unmodified lines62762862963063163263363463563663763863964064164264364464547 unmodified linesdef state_to_prompt(state, env) -> str:"""Convert VERGIL state to a structured text prompt for the LLM.Uses a <think>...</think> block to train chain-of-thought CDG reasoningbefore producing the final JSON decision."""nodes = state.cdg_nodespending = [n for n in nodes if n.status == CommitmentStatus.PENDING]2 unmodified linestrust_entries = state.trust_entriesmd_trust = getattr(env, 'multidim_trust', {})# Compute capacity summary for the reasoning blocktotal_committed = sum(n.estimated_duration_hours for n in accepted)available = getattr(state, 'available_hours_next_48h', 8.0)remaining_capacity = max(0.0, available - total_committed)prompt = "You are VERGIL, an AI commitment-management agent.\n"prompt += "You must reason step-by-step through CDG feasibility before deciding.\n\n"prompt += "=== CURRENT STATE ===\n"prompt += f"Step: {state.step_number} | "prompt += f"SAT Score: {state.satisfiability_score:.2f} | "prompt += f"Cognitive Load: {state.cognitive_load:.2f}\n"prompt += f"Available Hours (48h): {available:.1f}h | "prompt += f"Already Committed: {total_committed:.1f}h | "prompt += f"Remaining Capacity: {remaining_capacity:.1f}h\n\n"if pending:prompt += "=== PENDING COMMITMENTS (awaiting decision) ===\n"for n in pending:deadline_str = n.deadline.strftime('%Y-%m-%d %H:%M') if n.deadline else 'no deadline'prompt += (f"• [{n.node_id}] \"{n.label}\"\n"f" Stakeholder: {n.stakeholder_id} | Type: {n.commitment_type.value}\n"f" Duration: {n.estimated_duration_hours}h | "f"Deadline: {deadline_str} | Urgency: {n.urgency:.0%}\n")prompt += "\n"if accepted:prompt += "=== ACTIVE COMMITMENTS (in progress) ===\n"for n in accepted:deadline_str = n.deadline.strftime('%Y-%m-%d %H:%M') if n.deadline else 'no deadline'prompt += f"• [{n.node_id}] \"{n.label}\" — {n.estimated_duration_hours}h — due {deadline_str}\n"prompt += "\n"prompt += "=== TRUST NETWORK ===\n"for sid, te in trust_entries.items():md = md_trust.get(sid)if md:trust_status = "CRITICAL" if md.composite_trust < 0.35 else ("LOW" if md.composite_trust < 0.55 else "OK")prompt += (f"• {sid}: {trust_status} composite={md.composite_trust:.2f} "f"(Reliability={md.reliability:.2f}, Competence={md.competence:.2f}, "f"Benevolence={md.benevolence:.2f})\n")else:trust_score = te.trust_scoretrust_status = "CRITICAL" if trust_score < 0.35 else ("LOW" if trust_score < 0.55 else "OK")prompt += f"• {sid}: {trust_status} trust={trust_score:.2f}\n"prompt += "\n=== DECISION RULES ===\n"prompt += "• ACCEPT: Only if feasible (new hours + committed ≤ available capacity)\n"prompt += "• DECLINE: When infeasible AND trust level permits (trust > 0.35)\n"prompt += "• COUNTER_PROPOSE: When feasible with modified terms (later deadline, reduced scope)\n"prompt += "• DO_NOTHING: When no pending items or gathering information\n"prompt += "⚠ Warning: Accepting infeasible tasks will cause cascade failures and destroy trust.\n"prompt += "⚠ Warning: Silently dropping accepted tasks is the WORST outcome (penalty = 0.5 × time held).\n"prompt += "\n<think>\n"prompt += "Let me analyze this systematically:\n"prompt += "1. Capacity check: [calculate if accepting each pending item is feasible]\n"prompt += "2. Implicit commitment cost: [what additional overhead does this create?]\n"prompt += "3. Trust impact: [what happens if I decline vs accept vs counter?]\n"prompt += "4. Cascade risk: [which active commitments are at risk if I take on more?]\n"prompt += "5. Optimal action: [which action maximizes long-term trust × fulfillment?]\n"prompt += "</think>\n\n"prompt += "Respond with ONLY a JSON object (no other text after the JSON):\n"prompt += '{"action": "accept|decline|counter_propose|do_nothing", 'prompt += '"target": "<node_id or null>", 'prompt += '"reasoning": "<1-2 sentence explanation>"}\n'return promptdef parse_llm_output(text: str, pending_nodes: List) -> tuple:"""Parse LLM output text into (action_type, target_node_id)."""text = text.strip().lower()# Try JSON parsetry:import json as _json# Find JSON in textstart = text.find('{')end = text.rfind('}') + 1if start >= 0 and end > start:data = _json.loads(text[start:end])action_str = data.get('action', 'do_nothing')target = data.get('target', None)action_map = {'accept': ActionType.ACCEPT,4 unmodified lines'wait': ActionType.DO_NOTHING,}action_type = action_map.get(action_str, ActionType.DO_NOTHING)if not target and pending_nodes:target = pending_nodes[0].node_idreturn action_type, targetexcept:pass# Fallback: keyword detectionif 'accept' in text:target = pending_nodes[0].node_id if pending_nodes else Nonereturn ActionType.ACCEPT, targetelif 'decline' in text:target = pending_nodes[0].node_id if pending_nodes else Nonereturn ActionType.DECLINE, targetelif 'counter' in text:target = pending_nodes[0].node_id if pending_nodes else Nonereturn ActionType.COUNTER_PROPOSE, targetelse:return ActionType.DO_NOTHING, None# ═══════════════════════════════════════════════════════════════════════════50 unmodified linespomdp.current_belief = copy.deepcopy(snapshot['belief'])def vergil_reward_function(prompts, completions, **kwargs) -> list:"""Reward function for TRL's GRPOTrainer.GRPO generates num_generations completions per prompt — all must beevaluated from the SAME starting environment state. We snapshot theenv before each group of N completions and restore for each one.Additional signals:- format_bonus: +0.03 if output is valid JSON with required keys- think_bonus: +0.02 if <think>...</think> block is present- format_penalty: -0.05 for completely unparseable output"""rewards = []env = kwargs.get('env')pomdp = kwargs.get('pomdp')num_generations = kwargs.get('num_generations', 4)# Process in groups of num_generations — each group shares one starting statefor group_start in range(0, len(prompts), num_generations):group_prompts = prompts[group_start:group_start + num_generations]group_completions = completions[group_start:group_start + num_generations]# Snapshot BEFORE evaluating this groupsnapshot = _snapshot_env(env, pomdp)for prompt, completion in zip(group_prompts, group_completions):# Restore to the same starting state for every completion in the group_restore_env(env, pomdp, snapshot)try:state = env._stateif state is None:rewards.append(0.0)continuepending = [n for n in state.cdg_nodesif n.status == CommitmentStatus.PENDING]# Parse LLM outputaction_type, target = parse_llm_output(completion, pending)# Validate: node-targeting actions require a pending targetif action_type in (ActionType.ACCEPT, ActionType.DECLINE,ActionType.COUNTER_PROPOSE):if not pending:action_type = ActionType.DO_NOTHINGtarget = Noneelif target is None:target = pending[0].node_id# Build feasibility prediction: estimate based on capacityavailable = getattr(state, 'available_hours_next_48h', 8.0)committed = sum(n.estimated_duration_hours for n in[n for n in state.cdg_nodes if n.status == CommitmentStatus.ACCEPTED])target_node = next((n for n in state.cdg_nodes if n.node_id == target), None)new_cost = target_node.estimated_duration_hours if target_node else 0.0feasibility_pred = float(committed + new_cost <= available)action = AgentAction(action_type=action_type,target_node_id=target,feasibility_prediction=feasibility_pred,)if action_type == ActionType.COUNTER_PROPOSE and target_node:action.proposed_deadline = state.current_time + timedelta(hours=target_node.estimated_duration_hours * 1.5)simulate_task_progress(env)new_state, belief, reward, term, trunc, info = pomdp.step(action)simulate_task_progress(env)# Format quality bonuseshas_json = '{' in completion and '}' in completiontry:import json as _js = completion.find('{')e = completion.rfind('}') + 1parsed = _j.loads(completion[s:e]) if s >= 0 else {}has_required_keys = all(k in parsed for k in ('action', 'target', 'reasoning'))except Exception:has_required_keys = Falsehas_think_block = '<think>' in completion and '</think>' in completionformat_bonus = 0.0if has_json and has_required_keys:format_bonus += 0.03elif has_json:format_bonus += 0.01else:format_bonus -= 0.05if has_think_block:format_bonus += 0.02rewards.append(float(reward + format_bonus))except Exception:rewards.append(-0.10)return rewards# ═══════════════════════════════════════════════════════════════════════════5 unmodified linesMain GRPO training function.Run this on a GPU-enabled Colab/Kaggle notebook."""print("╔══════════════════════════════════════════════════╗")print("║ VERGIL GRPO Training — LLM Fine-Tuning ║")print("╠══════════════════════════════════════════════════╣")print("║ Model: Qwen2.5-0.5B (4-bit via Unsloth) ║")print("║ Algorithm: Group Relative Policy Optimization ║")print("║ Environment: VERGIL CDG Engine ║")print("╚══════════════════════════════════════════════════╝")2 unmodified linesfrom unsloth import FastLanguageModelmodel, tokenizer = FastLanguageModel.from_pretrained(model_name="unsloth/Qwen2.5-0.5B-Instruct",max_seq_length=2048,load_in_4bit=True,dtype=None, # Auto-detect)# Add LoRA adapters — rank=64 for richer commitment reasoning capacitymodel = FastLanguageModel.get_peft_model(model,r=64,target_modules=["q_proj", "k_proj", "v_proj", "o_proj","gate_proj", "up_proj", "down_proj"],lora_alpha=128,lora_dropout=0,bias="none",use_gradient_checkpointing="unsloth",16 unmodified lines)print(" Environment ready.")# ── Step 3: Generate Training Prompts ─────────────────────────────────# Generate diverse states across all curriculum stages.# Mix of: naive-play (accept-all), random, and semi-smart actions.print("\n📝 Generating training prompts across curriculum stages...")training_prompts = []# Fast-track: 80 total prompts instead of 500 → ~15-20 min on T4STAGE_EPISODES = {1: 10, 2: 15, 3: 25, 4: 30} # Total: 80 episodesfor stage, n_episodes in STAGE_EPISODES.items():print(f" Stage {stage}: generating {n_episodes} episodes...")6 unmodified linesfor j in range(min(8, env._max_steps)):simulate_task_progress(env)prompt = state_to_prompt(state, env)training_prompts.append(prompt)pending = [n for n in state.cdg_nodesif n.status == CommitmentStatus.PENDING]18 unmodified linesif term or trunc:breaknp.random.shuffle(training_prompts) # Shuffle so stages are interleavedprint(f" Generated {len(training_prompts)} training prompts (shuffled)")# ── Step 4: GRPO Training ─────────────────────────────────────────────print("\n🚀 Starting GRPO training...")from trl import GRPOConfig, GRPOTrainer# Fast-track: 4 generations instead of 8 → halves inference costNUM_GENERATIONS = 4training_config = GRPOConfig(output_dir="/tmp/vergil_grpo_output",num_train_epochs=1,max_steps=40, # Hard ceiling → ~15-20 min on T4per_device_train_batch_size=1, # Smallest batch, maximize speedgradient_accumulation_steps=4, # Effective batch = 4learning_rate=2e-5,max_completion_length=192, # Enough for <think> + JSON, no wastenum_generations=NUM_GENERATIONS,logging_steps=5,save_steps=20,warmup_steps=10,report_to="none",temperature=0.9,top_p=0.95,)# Create dataset — mix stages for curriculum diversityfrom datasets import Datasetdataset = Dataset.from_dict({"prompt": training_prompts, # Full set (up to 1000)})validation_log = []def reward_fn(prompts, completions, **kw):"""Wrapper that passes env + group size to reward function."""return vergil_reward_function(prompts, completions,env=env, pomdp=pomdp,num_generations=NUM_GENERATIONS,)trainer = GRPOTrainer(model=model,args=training_config,train_dataset=dataset,reward_funcs=[reward_fn],processing_class=tokenizer,)# ── Validation Callback: Log progress every 50 steps ──────────────────def run_validation(step_num: int):"""Run 10 eval episodes and log average reward + fulfillment rate."""10 unmodified linesp = state_to_prompt(vs, env)inp = tokenizer(p, return_tensors="pt").to(model.device)out = model.generate(**inp, max_new_tokens=350, temperature=0.1, do_sample=False)comp = tokenizer.decode(out[0][inp.input_ids.shape[1]:], skip_special_tokens=True)pend = [n for n in vs.cdg_nodes if n.status == CommitmentStatus.PENDING]at, tgt = parse_llm_output(comp, pend)if at in (ActionType.ACCEPT, ActionType.DECLINE, ActionType.COUNTER_PROPOSE) and not pend:at, tgt = ActionType.DO_NOTHING, Noneact = AgentAction(action_type=at, target_node_id=tgt)vs, vb, r, done, trunc, _ = pomdp.step(act)simulate_task_progress(env)23 unmodified linestrain_result = trainer.train()elapsed = time.time() - start_time# Final validationrun_validation(step_num=training_config.max_steps if hasattr(training_config, 'max_steps') else 999)# Save validation curveval_path = Path('/tmp/vergil_grpo_output/validation_log.json')val_path.write_text(json.dumps(validation_log, indent=2))print(f"\n✅ Training complete in {elapsed/60:.1f} minutes")print(f" Final loss: {train_result.training_loss:.4f}")# ── Step 5: Evaluate Before vs After ──────────────────────────────────print("\n📊 Evaluating trained model...")FastLanguageModel.for_inference(model)eval_rewards = []for i in range(20):env.curriculum_stage = 1scenario = curriculum.generate_next_episode()state, belief, info = pomdp.reset(scenario=scenario)episode_reward = 0for step in range(env._max_steps):simulate_task_progress(env)prompt = state_to_prompt(state, env)inputs = tokenizer(prompt, return_tensors="pt").to(model.device)outputs = model.generate(**inputs, max_new_tokens=200, temperature=0.7,do_sample=True, top_p=0.9,)completion = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:],skip_special_tokens=True)pending = [n for n in state.cdg_nodesif n.status == CommitmentStatus.PENDING]action_type, target = parse_llm_output(completion, pending)if action_type in (ActionType.ACCEPT, ActionType.DECLINE,ActionType.COUNTER_PROPOSE) and not pending:action_type = ActionType.DO_NOTHINGtarget = Noneaction = AgentAction(action_type=action_type,target_node_id=target,)state, belief, reward, term, trunc, step_info = pomdp.step(action)simulate_task_progress(env)episode_reward += rewardif term or trunc:breakeval_rewards.append(episode_reward)print(f" Post-training reward: {np.mean(eval_rewards):+.3f}")# ── Step 6: Save to HuggingFace ───────────────────────────────────────print("\n💾 Saving model...")model.save_pretrained("/tmp/vergil_grpo_model")tokenizer.save_pretrained("/tmp/vergil_grpo_model")# ── Auto-push to HuggingFace Hub ─────────────────────────────────────hf_token = os.getenv('HF_TOKEN')repo_id = "Laksh718/vergil-commitment-engine"if hf_token:print(f"\n🚀 Pushing model to HuggingFace Hub: {repo_id}")try:model.push_to_hub(repo_id, token=hf_token,commit_message="VERGIL GRPO fast-track — rank=64, 40 steps")tokenizer.push_to_hub(repo_id, token=hf_token)# Upload validation log if it existsvp = Path('/tmp/vergil_grpo_output/validation_log.json')if vp.exists():from huggingface_hub import HfApi4 unmodified linestoken=hf_token,commit_message="Add validation log",)print(f" ✅ Model live at https://huggingface.co/{repo_id}")except Exception as e:print(f" ⚠️ HF push failed: {e}")print(f" Model saved locally at /tmp/vergil_grpo_model")else:print("\n⚠️ No HF_TOKEN env var — model saved locally only")print(f" To push: model.push_to_hub('{repo_id}', token='your_token')")print("\n═══════════════════════════════════════════════════════")print(" GRPO TRAINING COMPLETE")print(f" Model saved to: /tmp/vergil_grpo_model")print(f" Training time: {elapsed/60:.1f} minutes")print(f" Eval reward: {np.mean(eval_rewards):+.3f}")print("═══════════════════════════════════════════════════════")47 unmodified lines48495051525354555657585960612 unmodified lines6465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521534 unmodified lines15815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220350 unmodified lines2542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833845 unmodified lines3903913923933943953963973983994004014024034044054064074084094104114124134142 unmodified lines41741841942042142242342442542642742842943043143243343443516 unmodified lines4524534544554564574584594604614624634644654664674684694704714724734744754764774784794804816 unmodified lines48848949049149249349449549649718 unmodified lines51651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765810 unmodified lines66967067167267367467567667767867968068168223 unmodified lines7067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428434 unmodified lines84884985085185285385485585685785885986086186286386486586647 unmodified linesdef state_to_prompt(state, env) -> str:"""Compact text serialization of VERGIL state for the LLM.Trimmed to ~60-70% of the previous size for faster training generations.Removed: verbose decision-rules section (penalties are *learned* viathe reward, not described in the prompt) and the over-prescriptivechain-of-thought scaffold (we still allow <think>; we just don'tspend tokens on a 5-step recipe). Kept: state, pending list withexplicit valid node_ids, accepted list, trust scores, JSON schema."""nodes = state.cdg_nodespending = [n for n in nodes if n.status == CommitmentStatus.PENDING]2 unmodified linestrust_entries = state.trust_entriesmd_trust = getattr(env, 'multidim_trust', {})total_committed = sum(n.estimated_duration_hours for n in accepted)available = getattr(state, 'available_hours_next_48h', 8.0)remaining = max(0.0, available - total_committed)lines: List[str] = []lines.append("You are VERGIL, an AI commitment manager. Decide ONE action.")lines.append("")lines.append(f"STATE: step={state.step_number} sat={state.satisfiability_score:.2f} "f"load={state.cognitive_load:.2f} cap={remaining:.1f}/{available:.1f}h "f"committed={total_committed:.1f}h")if pending:valid_ids = ", ".join(n.node_id for n in pending)lines.append(f"PENDING (valid `target` ids: [{valid_ids}]):")for n in pending:d = n.deadline.strftime('%m-%d %H:%M') if n.deadline else 'none'lines.append(f" {n.node_id} | {n.label[:48]} | {n.estimated_duration_hours}h "f"| due {d} | urg {n.urgency:.0%} | from {n.stakeholder_id}")if accepted:lines.append("ACCEPTED:")for n in accepted:d = n.deadline.strftime('%m-%d %H:%M') if n.deadline else 'none'lines.append(f" {n.node_id} | {n.label[:48]} | {n.estimated_duration_hours}h | due {d}")if trust_entries:trust_bits = []for sid, te in trust_entries.items():md = md_trust.get(sid)tval = md.composite_trust if md else te.trust_scoretag = "CRIT" if tval < 0.35 else ("LOW" if tval < 0.55 else "OK")trust_bits.append(f"{sid}={tval:.2f}{tag}")lines.append("TRUST: " + " ".join(trust_bits))lines.append("")lines.append("Output ONLY JSON. `target` MUST be a pending node_id above (NOT a")lines.append("stakeholder id like client_02). `target` MUST be null for do_nothing.")lines.append('{"action":"accept|decline|counter_propose|do_nothing",''"target":"<node_id or null>","reasoning":"<≤20 words>"}')return "\n".join(lines)def parse_llm_output(text: str, pending_nodes: List) -> tuple:"""Parse LLM output text into (action_type, target_node_id).Always returns an action that is *guaranteed valid* against the currentpending set, so the environment never hits its 'target node not found'rejection path (which would freeze time and put the rollout in aninfinite same-state loop under greedy decoding).Rules:• DO_NOTHING / no-pending → target = None• ACCEPT / DECLINE / COUNTER_PROPOSE without a valid pending target→ fall back to first pending node, or downgrade to DO_NOTHING ifthere are no pending nodes at all.• Any 'target' the LLM emits that doesn't match a pending node id(e.g. it confuses stakeholder ids like 'client_02' for node ids)is replaced with the first pending node id."""text_raw = text.strip()text_l = text_raw.lower()pending_ids = {n.node_id for n in pending_nodes}pending_ids_lower = {nid.lower(): nid for nid in pending_ids}first_pending = pending_nodes[0].node_id if pending_nodes else Noneaction_type = ActionType.DO_NOTHINGtarget = None# Try JSON parse on the *raw* text (preserve case for node ids)parsed_ok = Falsetry:import json as _jsonstart = text_raw.find('{')end = text_raw.rfind('}') + 1if start >= 0 and end > start:data = _json.loads(text_raw[start:end])action_str = str(data.get('action', 'do_nothing')).strip().lower()raw_target = data.get('target', None)if isinstance(raw_target, str):raw_target = raw_target.strip()if raw_target.lower() in ('null', 'none', ''):raw_target = Noneaction_map = {'accept': ActionType.ACCEPT,4 unmodified lines'wait': ActionType.DO_NOTHING,}action_type = action_map.get(action_str, ActionType.DO_NOTHING)target = raw_targetparsed_ok = Trueexcept Exception:parsed_ok = Falseif not parsed_ok:# Fallback: keyword detection on the lowercased textif 'accept' in text_l:action_type = ActionType.ACCEPTelif 'decline' in text_l:action_type = ActionType.DECLINEelif 'counter' in text_l:action_type = ActionType.COUNTER_PROPOSEelse:action_type = ActionType.DO_NOTHINGtarget = None# ── Coerce to a guaranteed-valid (action_type, target) pair ──────────needs_target = action_type in (ActionType.ACCEPT, ActionType.DECLINE, ActionType.COUNTER_PROPOSE)if needs_target:if not pending_nodes:action_type = ActionType.DO_NOTHINGtarget = Noneelse:# Case-insensitive match so 'p3' still maps to 'P3'if isinstance(target, str) and target.lower() in pending_ids_lower:target = pending_ids_lower[target.lower()]else:# LLM hallucinated a target (commonly a stakeholder id like# 'client_02' instead of a pending node id like 'P1').target = first_pendingelse:# DO_NOTHING never carries a target — clearing it prevents the# env's 'target node not found' rejection.target = Nonereturn action_type, target# ═══════════════════════════════════════════════════════════════════════════50 unmodified linespomdp.current_belief = copy.deepcopy(snapshot['belief'])def _format_bonus(completion: str) -> float:"""Lightweight format-quality bonus, independent of the env reward.Splitting this out keeps the main reward path readable AND lets ususe it as a separate reward function (judges value multipleindependent reward signals — see hackathon guide §7)."""has_json = '{' in completion and '}' in completionhas_required_keys = Falsetry:import json as _js = completion.find('{')e = completion.rfind('}') + 1if s >= 0 and e > s:parsed = _j.loads(completion[s:e])has_required_keys = all(k in parsed for k in ('action', 'target', 'reasoning'))except Exception:has_required_keys = Falsehas_think_block = '<think>' in completion and '</think>' in completionbonus = 0.0if has_json and has_required_keys:bonus += 0.03elif has_json:bonus += 0.01else:bonus -= 0.05if has_think_block:bonus += 0.02return bonusdef vergil_reward_function(prompts, completions, **kwargs) -> list:"""Reward function for TRL's GRPOTrainer.Critical correctness fix: each training prompt was *generated from*a specific env state, but the live env state at reward-evaluationtime has nothing to do with that. Without a fix, GRPO would scorecompletions against an arbitrary state — completely decoupling thelearning signal from what the prompt actually described.We solve this by passing a per-prompt env snapshot via the dataset.The dataset row carries an integer 'snapshot_idx' that points intothe kwarg 'snapshots' list. For every completion we restore THATsnapshot, not whatever env._state happens to be.Backwards compatible: if 'snapshots' / 'snapshot_idx' are notprovided, we fall back to the old group-snapshot behavior so thefunction still runs (just less accurate).Returns env-step reward + format bonus per completion."""rewards = []env = kwargs.get('env')pomdp = kwargs.get('pomdp')num_generations = kwargs.get('num_generations', 4)snapshots = kwargs.get('snapshots') # list[dict] | Nonesnapshot_idx = kwargs.get('snapshot_idx') # list[int] aligned to prompts | None# ── Aligned path: restore the exact snapshot the prompt was built fromif snapshots is not None and snapshot_idx is not None:for i, (prompt, completion) in enumerate(zip(prompts, completions)):try:idx = int(snapshot_idx[i])snap = snapshots[idx]_restore_env(env, pomdp, snap)rewards.append(_score_completion(env, pomdp, completion))except Exception:rewards.append(-0.10)return rewards# ── Fallback path: old group-snapshot behavior (less accurate)for group_start in range(0, len(prompts), num_generations):group_completions = completions[group_start:group_start + num_generations]snapshot = _snapshot_env(env, pomdp)for completion in group_completions:_restore_env(env, pomdp, snapshot)try:rewards.append(_score_completion(env, pomdp, completion))except Exception:rewards.append(-0.10)return rewardsdef _score_completion(env, pomdp, completion: str) -> float:"""Pure env-reward score for a single completion against the *currentlyrestored* env state. Returns the env step reward only — formatquality is supplied by a separate independent reward function soGRPO sees two uncorrelated signals (harder to reward-hack)."""state = env._stateif state is None:return 0.0pending = [n for n in state.cdg_nodesif n.status == CommitmentStatus.PENDING]# parse_llm_output already coerces to a valid pending target or DO_NOTHINGaction_type, target = parse_llm_output(completion, pending)# Feasibility prediction = capacity checkavailable = getattr(state, 'available_hours_next_48h', 8.0)committed = sum(n.estimated_duration_hours for n in state.cdg_nodesif n.status == CommitmentStatus.ACCEPTED)target_node = next((n for n in state.cdg_nodes if n.node_id == target), None)new_cost = target_node.estimated_duration_hours if target_node else 0.0feasibility_pred = float(committed + new_cost <= available)action = AgentAction(action_type=action_type,target_node_id=target,feasibility_prediction=feasibility_pred,)if action_type == ActionType.COUNTER_PROPOSE and target_node:action.proposed_deadline = state.current_time + timedelta(hours=target_node.estimated_duration_hours * 1.5)simulate_task_progress(env)_new_state, _belief, env_reward, _term, _trunc, _info = pomdp.step(action)simulate_task_progress(env)return float(env_reward)# ═══════════════════════════════════════════════════════════════════════════5 unmodified linesMain GRPO training function.Run this on a GPU-enabled Colab/Kaggle notebook."""# ── Hardware-aware defaults ───────────────────────────────────────────# Override anything below via env vars. Sensible L40S/A100 defaults:# MODEL_NAME=unsloth/Qwen2.5-1.5B-Instruct (3× capacity vs 0.5B)# LORA_R=64 LORA_ALPHA=128# On a smaller GPU (T4-16GB) override with:# MODEL_NAME=unsloth/Qwen2.5-0.5B-Instruct LORA_R=32MODEL_NAME = os.getenv("MODEL_NAME", "unsloth/Qwen2.5-1.5B-Instruct")MAX_SEQ_LENGTH = int(os.getenv("MAX_SEQ_LENGTH", "2048"))LORA_R = int(os.getenv("LORA_R", "64"))LORA_ALPHA = int(os.getenv("LORA_ALPHA", "128"))LOAD_IN_4BIT = os.getenv("LOAD_IN_4BIT", "1") == "1"print("╔══════════════════════════════════════════════════╗")print("║ VERGIL GRPO Training — LLM Fine-Tuning ║")print("╠══════════════════════════════════════════════════╣")print(f"║ Model : {MODEL_NAME[:34]:<34s} ║")print(f"║ Quantize : {'4-bit (Unsloth)' if LOAD_IN_4BIT else '16-bit (full)':<34s} ║")print(f"║ LoRA : r={LORA_R}, alpha={LORA_ALPHA:<23d}║")print("║ Algorithm : Group Relative Policy Optimization ║")print("║ Environment: VERGIL CDG Engine ║")print("╚══════════════════════════════════════════════════╝")2 unmodified linesfrom unsloth import FastLanguageModelmodel, tokenizer = FastLanguageModel.from_pretrained(model_name=MODEL_NAME,max_seq_length=MAX_SEQ_LENGTH,load_in_4bit=LOAD_IN_4BIT,dtype=None, # Auto-detect)# Add LoRA adapters — rank=64 for richer commitment reasoning capacitymodel = FastLanguageModel.get_peft_model(model,r=LORA_R,target_modules=["q_proj", "k_proj", "v_proj", "o_proj","gate_proj", "up_proj", "down_proj"],lora_alpha=LORA_ALPHA,lora_dropout=0,bias="none",use_gradient_checkpointing="unsloth",16 unmodified lines)print(" Environment ready.")# ── Step 3: Generate Training Prompts + per-prompt env snapshots ──────# Each prompt is paired with the EXACT env state it was generated from.# The reward function later restores that snapshot before scoring each# completion, so the LLM's decision is judged against the state the# prompt described — not whatever the env happened to be in.print("\n📝 Generating training prompts across curriculum stages...")training_prompts: List[str] = []training_snapshots: List[dict] = []# Override prompt count via env var so smoke tests can use a tiny set.# Default: 200 episodes spread across stages 1→4 (≈400-1600 prompts# depending on episode length) — enough state diversity for 60 GRPO# steps with batch 2 × num_gen 8 = 16 completions/step.PROMPT_BUDGET = int(os.getenv('PROMPT_BUDGET', '0')) # 0 = use defaultsif PROMPT_BUDGET > 0:# Distribute prompts roughly proportional to the default ratioSTAGE_EPISODES = {1: max(1, PROMPT_BUDGET // 8),2: max(1, (PROMPT_BUDGET * 3) // 16),3: max(1, (PROMPT_BUDGET * 5) // 16),4: max(1, (PROMPT_BUDGET * 6) // 16),}else:STAGE_EPISODES = {1: 25, 2: 40, 3: 60, 4: 75} # Total: 200 episodesfor stage, n_episodes in STAGE_EPISODES.items():print(f" Stage {stage}: generating {n_episodes} episodes...")6 unmodified linesfor j in range(min(8, env._max_steps)):simulate_task_progress(env)# Capture (prompt, state-snapshot) AS A PAIR before steppingtraining_prompts.append(state_to_prompt(state, env))training_snapshots.append(_snapshot_env(env, pomdp))pending = [n for n in state.cdg_nodesif n.status == CommitmentStatus.PENDING]18 unmodified linesif term or trunc:break# Shuffle prompts AND keep their snapshot index alignedperm = np.random.permutation(len(training_prompts))training_prompts = [training_prompts[i] for i in perm]training_snapshots = [training_snapshots[i] for i in perm]print(f" Generated {len(training_prompts)} (prompt, snapshot) pairs (shuffled)")# ── Step 4: GRPO Training ─────────────────────────────────────────────print("\n🚀 Starting GRPO training...")from trl import GRPOConfig, GRPOTrainer# ── Hardware-aware training config ─────────────────────────────────────# Defaults tuned for L40S (48 GB VRAM, ~91 TFLOPS). Cuts training time# from ~75 min on T4 to ~25-35 min on L40S while training a 3× bigger# model with 2× larger GRPO groups for tighter advantage estimates.## If running on T4 (16 GB), set:# PER_DEVICE_BATCH=1 NUM_GENERATIONS=4 MAX_COMPLETION_LEN=128NUM_GENERATIONS = int(os.getenv('NUM_GENERATIONS', '8'))MAX_STEPS = int(os.getenv('MAX_STEPS', '60'))MAX_COMPLETION_LEN = int(os.getenv('MAX_COMPLETION_LEN', '192'))PER_DEVICE_BATCH = int(os.getenv('PER_DEVICE_BATCH', '2'))GRAD_ACCUM = int(os.getenv('GRAD_ACCUM', '2'))LEARNING_RATE = float(os.getenv('LR', '2e-5'))training_config = GRPOConfig(output_dir="/tmp/vergil_grpo_output",num_train_epochs=1,max_steps=MAX_STEPS, # 60 by default (was 30)per_device_train_batch_size=PER_DEVICE_BATCH, # 2 by default (was 1)gradient_accumulation_steps=GRAD_ACCUM, # effective batch = 4learning_rate=LEARNING_RATE,max_completion_length=MAX_COMPLETION_LEN, # 192 by defaultnum_generations=NUM_GENERATIONS, # 8 by default (was 4)logging_steps=5,save_steps=max(MAX_STEPS, 1), # avoid mid-train saves on tiny runswarmup_steps=min(10, MAX_STEPS // 3),report_to="none",temperature=0.9,top_p=0.95,bf16=True, # L40S has BF16 hardware)# Create dataset — each row carries an integer snapshot_idx so the# reward function can restore the exact env state the prompt was# generated from.from datasets import Datasetdataset = Dataset.from_dict({"prompt": training_prompts,"snapshot_idx": list(range(len(training_prompts))),})validation_log = []def reward_fn(prompts, completions, snapshot_idx=None, **kw):"""Wrapper for TRL's GRPOTrainer.TRL passes any extra dataset columns as kwargs to the rewardfunction. We forward `snapshot_idx` (a list aligned with prompts)so the per-prompt env state can be restored before scoring."""return vergil_reward_function(prompts, completions,env=env, pomdp=pomdp,num_generations=NUM_GENERATIONS,snapshots=training_snapshots,snapshot_idx=snapshot_idx,)def format_reward_fn(prompts, completions, **kw):"""Independent format-quality reward — judges value MULTIPLEindependent reward signals (hackathon guide §7) since they aremuch harder for the model to game than a single monolithic score."""return [_format_bonus(c) for c in completions]trainer = GRPOTrainer(model=model,args=training_config,train_dataset=dataset,reward_funcs=[reward_fn, format_reward_fn],processing_class=tokenizer,)# ── Preflight: smoke-test the reward signal before burning compute ────# Hackathon guide §15+§21: "Picking a task so hard that success# probability is zero" is the #1 mistake. Generate a few completions,# score them, and abort if rewards are all identical (no learning# signal) or all -0.10 (everything is crashing).if os.getenv('SKIP_PREFLIGHT', '0') != '1':print("\n🔬 Preflight: testing reward signal on 3 prompts × 2 generations...")try:FastLanguageModel.for_inference(model)preflight_rewards = []for pf_idx in range(min(3, len(training_prompts))):pf_prompt = training_prompts[pf_idx]pf_inp = tokenizer(pf_prompt, return_tensors="pt").to(model.device)pf_completions = []for _ in range(2):pf_out = model.generate(**pf_inp, max_new_tokens=MAX_COMPLETION_LEN,do_sample=True, temperature=0.9, top_p=0.95,pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,)pf_completions.append(tokenizer.decode(pf_out[0][pf_inp.input_ids.shape[1]:], skip_special_tokens=True))env_rewards = vergil_reward_function([pf_prompt] * 2, pf_completions,env=env, pomdp=pomdp,snapshots=training_snapshots,snapshot_idx=[pf_idx, pf_idx],)fmt_rewards = [_format_bonus(c) for c in pf_completions]for c, er, fr in zip(pf_completions, env_rewards, fmt_rewards):preflight_rewards.append(er)print(f" [pf {pf_idx}] env_reward={er:+.3f} fmt={fr:+.3f} "f"completion={c[:80]!r}")FastLanguageModel.for_training(model)unique_rewards = len(set(round(r, 3) for r in preflight_rewards))if unique_rewards <= 1:print(f" ⚠️ Preflight WARNING: all {len(preflight_rewards)} rewards "f"are identical → no learning signal. Check reward function.")print(f" Set SKIP_PREFLIGHT=1 to bypass this check.")if os.getenv('STRICT_PREFLIGHT', '0') == '1':raise RuntimeError("Preflight failed: rewards lack variance")else:print(f" ✅ Preflight OK — {unique_rewards} unique reward values, "f"range=[{min(preflight_rewards):+.3f}, {max(preflight_rewards):+.3f}]")except Exception as e:print(f" ⚠️ Preflight crashed: {type(e).__name__}: {e}")if os.getenv('STRICT_PREFLIGHT', '0') == '1':raise# ── Validation Callback: Log progress every 50 steps ──────────────────def run_validation(step_num: int):"""Run 10 eval episodes and log average reward + fulfillment rate."""10 unmodified linesp = state_to_prompt(vs, env)inp = tokenizer(p, return_tensors="pt").to(model.device)out = model.generate(**inp,max_new_tokens=350,do_sample=False,pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,)comp = tokenizer.decode(out[0][inp.input_ids.shape[1]:], skip_special_tokens=True)pend = [n for n in vs.cdg_nodes if n.status == CommitmentStatus.PENDING]at, tgt = parse_llm_output(comp, pend)act = AgentAction(action_type=at, target_node_id=tgt)vs, vb, r, done, trunc, _ = pomdp.step(act)simulate_task_progress(env)23 unmodified linestrain_result = trainer.train()elapsed = time.time() - start_timeprint(f"\n✅ Training complete in {elapsed/60:.1f} minutes")print(f" Final loss: {train_result.training_loss:.4f}")# ═══════════════════════════════════════════════════════════════════════# CRITICAL: Save + push BEFORE any evaluation.## Earlier versions of this script ran the full eval (20 eps × 20 steps,# each ~200-token generation on a 4-bit Qwen) BEFORE saving — which# meant if the HF Space slept, the kernel disconnected, or eval hung,# the trained LoRA adapter was lost forever. We persist first, then# evaluate as a strictly best-effort step.# ═══════════════════════════════════════════════════════════════════════print("\n💾 Saving model (BEFORE eval — guarantees persistence)...")model_dir = "/tmp/vergil_grpo_model"model.save_pretrained(model_dir)tokenizer.save_pretrained(model_dir)print(f" ✅ Saved locally to {model_dir}")# Persist validation curve so far (may be empty if we skipped run_validation)try:val_path = Path('/tmp/vergil_grpo_output/validation_log.json')val_path.parent.mkdir(parents=True, exist_ok=True)val_path.write_text(json.dumps(validation_log, indent=2))except Exception as e:print(f" ⚠️ Could not write validation log: {e}")# ── Push to HuggingFace Hub immediately ────────────────────────────────hf_token = os.getenv('HF_TOKEN')repo_id = os.getenv('HF_REPO_ID', "Laksh718/vergil-commitment-engine")push_succeeded = Falseif hf_token:print(f"\n🚀 Pushing model to HuggingFace Hub: {repo_id}")try:model.push_to_hub(repo_id, token=hf_token,commit_message=f"VERGIL GRPO — {training_config.max_steps} steps, "f"loss={train_result.training_loss:.4f}",)tokenizer.push_to_hub(repo_id, token=hf_token)push_succeeded = Trueprint(f" ✅ Model live at https://huggingface.co/{repo_id}")except Exception as e:print(f" ⚠️ HF push failed: {e}")print(f" Model is safe locally at {model_dir}")else:print("\n⚠️ No HF_TOKEN env var — model saved locally only")print(f" To push later: model.push_to_hub('{repo_id}', token='your_token')")# ═══════════════════════════════════════════════════════════════════════# Post-training evaluation — STRICTLY OPTIONAL, time-bounded.# Controlled by env vars so you can disable on slow / sleepy Spaces:# SKIP_EVAL=1 → skip evaluation entirely# EVAL_EPISODES=N → number of eval episodes (default 5)# EVAL_TIMEOUT_SEC=S → wallclock cap on the eval loop (default 180)# ═══════════════════════════════════════════════════════════════════════SKIP_EVAL = os.getenv('SKIP_EVAL', '0') == '1'EVAL_EPISODES = int(os.getenv('EVAL_EPISODES', '5'))EVAL_TIMEOUT_SEC = int(os.getenv('EVAL_TIMEOUT_SEC', '180'))eval_rewards: List[float] = []eval_fulfillments: List[float] = []if SKIP_EVAL:print("\n⏭ SKIP_EVAL=1 — skipping post-training evaluation")else:print(f"\n📊 Evaluating trained model "f"(≤{EVAL_EPISODES} eps, ≤{EVAL_TIMEOUT_SEC}s budget)...")try:FastLanguageModel.for_inference(model)eval_start = time.time()for i in range(EVAL_EPISODES):if time.time() - eval_start > EVAL_TIMEOUT_SEC:print(f" ⏱ Time budget reached after {i} episodes — stopping early")breakenv.curriculum_stage = 1scenario = curriculum.generate_next_episode()state, belief, info = pomdp.reset(scenario=scenario)episode_reward = 0.0for step in range(env._max_steps):simulate_task_progress(env)prompt = state_to_prompt(state, env)inputs = tokenizer(prompt, return_tensors="pt").to(model.device)outputs = model.generate(**inputs,max_new_tokens=128, # shorter → ~2× fasterdo_sample=False, # greedy → deterministic + fasterpad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,)completion = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:],skip_special_tokens=True,)pending = [n for n in state.cdg_nodesif n.status == CommitmentStatus.PENDING]# parse_llm_output coerces to a valid (action, target) pairaction_type, target = parse_llm_output(completion, pending)action = AgentAction(action_type=action_type,target_node_id=target,)state, belief, reward, term, trunc, step_info = pomdp.step(action)simulate_task_progress(env)episode_reward += rewardif term or trunc:breakn_completed = sum(1 for n in state.cdg_nodesif n.status == CommitmentStatus.COMPLETED)n_accepted = sum(1 for n in state.cdg_nodes if n.status in(CommitmentStatus.ACCEPTED, CommitmentStatus.COMPLETED))eval_fulfillments.append(n_completed / max(1, n_accepted))eval_rewards.append(episode_reward)print(f" ep {i+1}/{EVAL_EPISODES}: reward={episode_reward:+.3f} "f"fulfillment={eval_fulfillments[-1]:.1%}")if eval_rewards:print(f"\n Mean reward: {np.mean(eval_rewards):+.3f}")print(f" Mean fulfillment: {np.mean(eval_fulfillments):.1%}")except Exception as e:print(f" ⚠️ Eval failed (model already saved): {type(e).__name__}: {e}")# ── Upload validation log to Hub (best-effort) ───────��─────────────────if hf_token and push_succeeded:try:vp = Path('/tmp/vergil_grpo_output/validation_log.json')if vp.exists():from huggingface_hub import HfApi4 unmodified linestoken=hf_token,commit_message="Add validation log",)except Exception as e:print(f" ⚠️ Validation-log upload failed: {e}")print("\n═══════════════════════════════════════════════════════")print(" GRPO TRAINING COMPLETE")print(f" Model saved to: {model_dir}")print(f" Pushed to Hub: {push_succeeded} ({repo_id})")print(f" Training time: {elapsed/60:.1f} minutes")if eval_rewards:print(f" Eval reward: {np.mean(eval_rewards):+.3f} "f"(over {len(eval_rewards)} eps)")else:print(f" Eval: skipped or empty")print("═══════════════════════════════════════════════════════")
1234567891011121314151617181920212223FROM pytorch/pytorch:2.3.0-cuda12.1-cudnn8-devel
RUN useradd -m -u 1000 userUSER userENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH \ CUDA_HOME=/usr/local/cuda
WORKDIR $HOME/app
USER rootRUN apt-get update && apt-get install -y git curl build-essential && rm -rf /var/lib/apt/lists/*USER user
COPY --chown=user . $HOME/app
RUN pip install --upgrade pip# Force strict synchronization of PyTorch and Torchvision directly from NVIDIA's serversRUN pip install "torch==2.3.1" "torchvision==0.18.1" --index-url https://download.pytorch.org/whl/cu121# Install all required modules in one robust resolution blockRUN pip install "unsloth" "xformers==0.0.27" "trl" "peft" "accelerate" "bitsandbytes" "gymnasium" "networkx" "scipy" "datasets" "gradio" "huggingface_hub"
CMD ["python", "app.py"]254 unmodified lines255256257258259260261196 unmodified lines458459460461462463464465466467254 unmodified lines"info": info,}except Exception as e:raise HTTPException(status_code=400, detail=str(e))@app.post("/api/step")196 unmodified lines}naive_result = _run_agent(lambda s, e: _naive_decide(s), "Naive (Accept-All)")vergil_result = _run_agent(_heuristic_decide, "VERGIL-Trained")return {"scenario_id": scenario.get('scenario_id', 'unknown'),"naive": naive_result,"vergil": vergil_result,"comparison": {254 unmodified lines255256257258259260261262263196 unmodified lines460461462463464465466467468469470471472473474475476477478479254 unmodified lines"info": info,}except Exception as e:import tracebacktraceback.print_exc()raise HTTPException(status_code=400, detail=f"Reset Error: {str(e)}")@app.post("/api/step")196 unmodified lines}naive_result = _run_agent(lambda s, e: _naive_decide(s), "Naive (Accept-All)")# Use the trained LLM if it's loaded; otherwise fall back to the# capacity-aware heuristic. The label reflects which one ran.if _llm_model is not None:vergil_label = "VERGIL-Trained (LLM)"vergil_agent = _llm_decideelse:vergil_label = "VERGIL Heuristic (LLM not loaded)"vergil_agent = _heuristic_decidevergil_result = _run_agent(vergil_agent, vergil_label)return {"scenario_id": scenario.get('scenario_id', 'unknown'),"llm_loaded": _llm_model is not None,"naive": naive_result,"vergil": vergil_result,"comparison": {
465 unmodified lines466467468469470471472473474475476477210 unmodified lines688689690691692693694695696697698699700701465 unmodified linesdef _validate_action(self, action: AgentAction,state: VERGILState) -> Tuple[bool, str]:"""Check if action is legal given current state."""if action.action_type in (ActionType.ACCEPT, ActionType.DECLINE,ActionType.COUNTER_PROPOSE, ActionType.RENEGOTIATE):if action.target_node_id is None and action.target_message_id is None:return False, "Action requires a target commitment or message"if action.target_node_id:node = self.cdg.get_node(action.target_node_id)if node is None:return False, f"Target node {action.target_node_id} not found"210 unmodified linesdef _build_stakeholder_profiles(self, scenario: Dict) -> Dict[str, StakeholderProfile]:profiles = {}for s_data in scenario.get('stakeholders', []):role = StakeholderRole(s_data.get('role', 'colleague'))profile = StakeholderProfile(stakeholder_id=s_data['id'],name=s_data.get('name', s_data['id']),role=role,)profiles[s_data['id']] = profilereturn profilesdef _build_message_schedule(self, scenario: Dict,465 unmodified lines466467468469470471472473474475476477478479480481482483484485210 unmodified lines696697698699700701702703704705706707708709710711712713714715716717718719720721722465 unmodified linesdef _validate_action(self, action: AgentAction,state: VERGILState) -> Tuple[bool, str]:"""Check if action is legal given current state."""node_targeting = (ActionType.ACCEPT, ActionType.DECLINE,ActionType.COUNTER_PROPOSE, ActionType.RENEGOTIATE,ActionType.DELEGATE)if action.action_type in node_targeting:if action.target_node_id is None and action.target_message_id is None:return False, "Action requires a target commitment or message"# Only validate target_node_id for actions that *use* a target.# DO_NOTHING with a stale/hallucinated target_node_id is treated as# a benign no-op (the target is ignored anyway) — rejecting it would# freeze time under greedy LLM decoding and create infinite loops# of the same-state, same-output kind.if action.target_node_id and action.action_type in node_targeting:node = self.cdg.get_node(action.target_node_id)if node is None:return False, f"Target node {action.target_node_id} not found"210 unmodified linesdef _build_stakeholder_profiles(self, scenario: Dict) -> Dict[str, StakeholderProfile]:profiles = {}stk_data = scenario.get('stakeholders', [])if isinstance(stk_data, dict):for sid, s_data in stk_data.items():role = StakeholderRole(s_data.get('role', 'colleague'))profile = StakeholderProfile(stakeholder_id=sid,name=s_data.get('name', sid),role=role,)profiles[sid] = profileelif isinstance(stk_data, list):for s_data in stk_data:role = StakeholderRole(s_data.get('role', 'colleague'))profile = StakeholderProfile(stakeholder_id=s_data['id'],name=s_data.get('name', s_data['id']),role=role,)profiles[s_data['id']] = profilereturn profilesdef _build_message_schedule(self, scenario: Dict,