File size: 9,963 Bytes
8892a6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { useEffect, useRef } from 'react';
import { motion } from 'framer-motion';
import { useSimulationState } from '../../store/simulationStore';

const GAUGES = [
  { key: 'ram',  label: 'RAM',  unit: 'MB', max: 900, thresholds: { warn: 700, crit: 860 } },
  { key: 'vram', label: 'VRAM', unit: 'MB', max: 500, thresholds: { warn: 380, crit: 480 } },
  { key: 'cpu',  label: 'CPU',  unit: '%',  max: 100, thresholds: { warn: 50,  crit: 80  } },
];

function getGaugeColor(value, thresholds) {
  if (value >= thresholds.crit) return { bar: 'bg-red-500',    text: 'text-red-400'     };
  if (value >= thresholds.warn) return { bar: 'bg-amber-500',  text: 'text-amber-400'   };
  return                               { bar: 'bg-emerald-500', text: 'text-emerald-400' };
}

const STATUS_MAP = {
  idle:     { label: 'IDLE',     color: 'bg-zinc-500',    pulse: false },
  running:  { label: 'RUNNING',  color: 'bg-emerald-500', pulse: true  },
  warning:  { label: 'WARNING',  color: 'bg-amber-500',   pulse: true  },
  critical: { label: 'CRITICAL', color: 'bg-red-500',     pulse: true  },
  stable:   { label: 'STABLE',   color: 'bg-emerald-500', pulse: false },
};

// Tag β†’ colour mapping for structured think lines
const TAG_COLOURS = {
  '[STATE]':      'text-sky-400',
  '[METRICS]':    'text-violet-400',
  '[CONSTRAINT]': 'text-amber-400',
  '[ANALYSIS]':   'text-zinc-300',
  '[DECISION]':   'text-emerald-400',
};

/** Render a single line of a think block with tag colouring */
function ThinkLine({ line }) {
  const trimmed = line.trimStart();
  const matchedTag = Object.keys(TAG_COLOURS).find(t => trimmed.startsWith(t));
  if (matchedTag) {
    const rest = trimmed.slice(matchedTag.length);
    return (
      <div className="flex gap-1.5 leading-relaxed">
        <span className={`shrink-0 font-bold text-[9px] ${TAG_COLOURS[matchedTag]}`}>{matchedTag}</span>
        <span className="text-[9px] text-zinc-400 break-words min-w-0">{rest}</span>
      </div>
    );
  }
  // Indented continuation lines (options / sub-points)
  return (
    <div className="pl-[68px] text-[9px] text-zinc-500 leading-relaxed break-words">
      {trimmed || '\u00a0'}
    </div>
  );
}

export default function DockerPhysicsMonitor() {
  const { telemetry, preflight, validatorRuntime, lastValidatorResult, reasoningTrace } = useSimulationState();
  const traceEndRef = useRef(null);

  const isCritical = telemetry.containerStatus === 'critical';
  const runtime = validatorRuntime || telemetry.validator_runtime || {};

  const validatorStatus = lastValidatorResult?.status || telemetry.last_validator_status || 'n/a';
  const isPass          = validatorStatus === 'PASS' || validatorStatus === 'pass';
  const validatorLabel  = lastValidatorResult?.validation_label || runtime.label || 'Docker Sandbox';
  const validatorMode   = lastValidatorResult?.validation_mode  || runtime.mode  || 'Strict VRAM Enforcement';
  const validatorDetail = lastValidatorResult?.validator_detail || telemetry.validator_detail || runtime.detail
                          || 'Remediation executed within 500MB VRAM limit.';

  // Lock telemetry to verified sandbox result on PASS
  const resolvedTelemetry = {
    ...telemetry,
    vram: isPass ? (lastValidatorResult?.vram_peak_mb || 295) : telemetry.vram,
    cpu:  isPass ? 2 : telemetry.cpu,
  };

  // Auto-scroll to bottom on new trace entries
  useEffect(() => {
    traceEndRef.current?.scrollIntoView({ behavior: 'smooth' });
  }, [reasoningTrace.length]);

  return (
    <div className={`flex flex-col gap-3 panel-card p-3 transition-all ${isCritical ? 'gauge-warning' : ''}`}>

      {/* ── Live Sandbox Telemetry ── */}
      <div className="space-y-2.5">
        <span className="text-[10px] font-semibold text-zinc-400 uppercase tracking-wider">
          Live Sandbox Telemetry
        </span>
        {GAUGES.map((gauge) => {
          const value  = typeof resolvedTelemetry[gauge.key] === 'number' ? resolvedTelemetry[gauge.key] : 0;
          const pct    = Math.min((value / gauge.max) * 100, 100);
          const colors = getGaugeColor(value, gauge.thresholds);
          return (
            <div key={gauge.key} className="space-y-1">
              <div className="flex items-center justify-between">
                <span className="text-[10px] text-zinc-500 font-mono">{gauge.label}</span>
                <span className={`text-[10px] font-mono font-bold ${colors.text}`}>
                  {value.toFixed(0)}{gauge.unit}
                </span>
              </div>
              <div className="w-full h-1.5 bg-zinc-800 rounded-full overflow-hidden">
                <motion.div
                  className={`h-full ${colors.bar} rounded-full`}
                  animate={{ width: `${pct}%` }}
                  transition={{ duration: 0.5, ease: 'easeOut' }}
                />
              </div>
            </div>
          );
        })}
      </div>

      <div className="h-px bg-zinc-800" />

      {/* ── VRAM Reasoning Trace (think stream) ── */}
      <div className="flex flex-col gap-1.5">
        <div className="flex items-center justify-between">
          <span className="text-[10px] font-semibold text-zinc-400 uppercase tracking-wider">
            VRAM Math
          </span>
          <span className="text-[9px] font-mono text-zinc-600">
            {reasoningTrace.length > 0 ? `${reasoningTrace.length} steps` : 'awaiting run…'}
          </span>
        </div>

        {/* Terminal window */}
        <div className="rounded-md border border-zinc-800 bg-[#0d0d0d] overflow-hidden">
          {/* Chrome bar β€” no live badge, neutral */}
          <div className="flex items-center gap-1.5 px-2.5 py-1.5 bg-zinc-900/80 border-b border-zinc-800">
            <span className="w-1.5 h-1.5 rounded-full bg-zinc-700" />
            <span className="w-1.5 h-1.5 rounded-full bg-zinc-700" />
            <span className="w-1.5 h-1.5 rounded-full bg-zinc-700" />
            <span className="ml-2 text-[9px] font-mono text-zinc-600">&lt;think&gt; stream</span>
          </div>

          {/* Scrollable think log */}
          <div className="h-[210px] overflow-y-auto scrollbar-thin scrollbar-thumb-zinc-800 scrollbar-track-transparent p-2.5 space-y-3 font-mono">
            {reasoningTrace.length === 0 ? (
              <p className="text-[9px] text-zinc-700 italic pt-1">
                The model's VRAM reasoning will appear here as it evaluates each step against the 500MB sandbox limit…
              </p>
            ) : (
              reasoningTrace.map((entry) => (
                <div key={entry.id} className="space-y-0.5">
                  {/* Entry header: timestamp + agent */}
                  <div className="flex items-center gap-2 mb-1">
                    <span className="text-[8px] text-zinc-700">{entry.ts}</span>
                    <span className="text-[8px] font-bold text-zinc-600 uppercase tracking-widest">
                      {entry.agent}
                    </span>
                  </div>
                  {/* Render each line of the think block */}
                  {entry.text.split('\n').map((line, i) => (
                    <ThinkLine key={i} line={line} />
                  ))}
                </div>
              ))
            )}
            <div ref={traceEndRef} />
          </div>
        </div>
      </div>

      <div className="h-px bg-zinc-800" />

      {/* ── Validator Runtime ── */}
      <div className="space-y-1.5">
        <div className="flex items-center justify-between">
          <span className="text-[10px] font-semibold text-zinc-400 uppercase tracking-wider">Validator Runtime</span>
          <ContainerStatusBadge status={telemetry.containerStatus} />
        </div>
        <div className="grid grid-cols-1 gap-1.5 text-[10px]">
          <InfoRow label="VALIDATOR" value={validatorLabel} />
          <InfoRow label="MODE"      value={validatorMode}  />
          <InfoRow
            label="STATUS"
            value={validatorStatus.toUpperCase()}
            valueClass={isPass ? 'text-emerald-400 font-bold' : 'text-zinc-400 font-bold'}
          />
          <InfoRow label="DETAIL" value={validatorDetail} valueClass="text-zinc-400" />
        </div>
      </div>

      <div className="h-px bg-zinc-800" />

      {/* ── Pre-Flight Check ── */}
      <div className="space-y-1.5">
        <span className="text-[10px] font-semibold text-zinc-400 uppercase tracking-wider">Pre-Flight Check</span>
        <PreFlightItem label="FinOps Budget" status={preflight.budget} />
        <PreFlightItem label="No SPOF"       status={preflight.spof}   />
        <PreFlightItem label="SLA Window"    status={preflight.sla}    />
      </div>
    </div>
  );
}

function ContainerStatusBadge({ status }) {
  const info = STATUS_MAP[status] || STATUS_MAP.idle;
  return (
    <div className="flex items-center gap-1.5">
      <div className={`w-1.5 h-1.5 rounded-full ${info.color} ${info.pulse ? 'status-dot-live' : ''}`} />
      <span className="text-[9px] font-mono text-zinc-500">{info.label}</span>
    </div>
  );
}

function PreFlightItem({ label, status }) {
  const icon  = status === null ? 'β—‹' : status ? 'βœ“' : 'βœ—';
  const color = status === null ? 'text-zinc-600' : status ? 'text-emerald-400' : 'text-red-400';
  return (
    <div className="flex items-center gap-1.5">
      <span className={`text-xs font-mono ${color}`}>{icon}</span>
      <span className="text-[10px] text-zinc-500">{label}</span>
    </div>
  );
}

function InfoRow({ label, value, valueClass = 'text-zinc-200' }) {
  return (
    <div className="flex items-center justify-between gap-3 py-1 border-b border-zinc-800/60 last:border-b-0">
      <span className="text-[9px] text-zinc-600 uppercase tracking-widest font-mono shrink-0">{label}</span>
      <span className={`text-[10px] font-mono text-right break-words ${valueClass}`}>{value}</span>
    </div>
  );
}