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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import type { ReactElement } from "react";
/** System view: queue depth, budgets, environment, and host telemetry. */

import { useEffect, useState } from "react";
import {
  Server,
  Database,
  HardDrive,
  Activity,
  Cpu,
  Layers,
  GitCommit,
  ShieldCheck,
  CheckCircle2,
  AlertCircle,
  RefreshCw,
  Info,
} from "lucide-react";
import { api } from "../api/client";
import type { SystemStatus } from "../api/client";
import { ErrorBox, bytes, pct } from "./primitives";
import { Card, CardHeader, CardTitle, CardDescription, CardContent } from "./ui/card";
import { Badge } from "./ui/badge";
import { cn } from "../lib/utils";

export function SystemView(): ReactElement {
  const [status, setStatus] = useState<SystemStatus | null>(null);
  const [info, setInfo] = useState<Record<string, unknown> | null>(null);
  const [error, setError] = useState<unknown>(null);
  const [lastRefreshed, setLastRefreshed] = useState<Date>(new Date());

  useEffect(() => {
    let stop = false;
    const load = async () => {
      try {
        const s = await api.systemStatus();
        if (!stop) {
          setStatus(s);
          setLastRefreshed(new Date());
          setError(null);
        }
      } catch (e) {
        if (!stop) setError(e);
      }
    };
    void load();
    const t = setInterval(() => void load(), 2000);
    void api.info().then((i) => !stop && setInfo(i));
    return () => {
      stop = true;
      clearInterval(t);
    };
  }, []);

  return (
    <div className="space-y-6">
      {/* Top Header & Polling Indicator */}
      <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3">
        <div>
          <h2 className="text-base font-semibold tracking-tight text-zinc-100">
            System Infrastructure & Telemetry
          </h2>
          <p className="text-xs text-zinc-400">
            Real-time worker telemetry, allocation budgets, and execution host health
          </p>
        </div>

        <div className="flex items-center gap-2 text-xs text-zinc-400">
          <span className="flex items-center gap-1.5 bg-zinc-900/80 border border-zinc-800 rounded-md px-2.5 py-1">
            <span className="size-2 rounded-full bg-emerald-400 animate-pulse" />
            <span className="text-[11px] font-mono text-zinc-300">Live 2s polling</span>
          </span>
          <span className="text-zinc-500 text-[11px] font-mono">
            Updated {lastRefreshed.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", second: "2-digit" })}
          </span>
        </div>
      </div>

      <ErrorBox error={error} />

      {/* 4 Executive Metric Cards Grid */}
      <div className="grid grid-cols-1 md:grid-cols-2 gap-5">
        {/* Card 1: Runtime Mode & Engine */}
        <Card className="border-zinc-800 bg-zinc-900/60 shadow-md backdrop-blur-sm flex flex-col justify-between">
          <div>
            <CardHeader className="p-5 pb-3">
              <div className="flex items-center justify-between">
                <div className="flex items-center gap-2.5">
                  <div className="flex size-9 items-center justify-center rounded-lg bg-sky-500/10 text-sky-400 border border-sky-500/20">
                    <Server className="size-4.5" />
                  </div>
                  <div>
                    <CardTitle className="text-sm font-semibold text-zinc-100">
                      Runtime Mode & Engine
                    </CardTitle>
                    <CardDescription className="text-xs text-zinc-400">
                      Inference orchestration & persistent storage
                    </CardDescription>
                  </div>
                </div>
                <div className="flex size-7 items-center justify-center rounded-md bg-zinc-800/60 text-zinc-400">
                  <Database className="size-3.5" />
                </div>
              </div>
            </CardHeader>

            <CardContent className="p-5 pt-2 space-y-4">
              {status ? (
                <div className="space-y-3">
                  <div className="grid grid-cols-2 gap-3">
                    <div className="rounded-lg bg-zinc-950/60 border border-zinc-800/80 p-3 space-y-1">
                      <span className="text-[11px] font-medium text-zinc-500 uppercase tracking-wider block">
                        Runtime Mode
                      </span>
                      <div className="flex items-center gap-2">
                        <Badge
                          variant={status.mode === "live" ? "success" : "info"}
                          className="font-mono text-xs uppercase"
                        >
                          {status.mode}
                        </Badge>
                      </div>
                    </div>

                    <div className="rounded-lg bg-zinc-950/60 border border-zinc-800/80 p-3 space-y-1">
                      <span className="text-[11px] font-medium text-zinc-500 uppercase tracking-wider block">
                        Database Engine
                      </span>
                      <span className="font-mono text-xs font-semibold text-zinc-200 uppercase">
                        {status.dialect}
                      </span>
                    </div>
                  </div>

                  <div className="rounded-lg bg-zinc-950/60 border border-zinc-800/80 p-3 flex items-center justify-between">
                    <div>
                      <span className="text-xs text-zinc-400 block font-medium">
                        Global Queue Limit
                      </span>
                      <span className="text-[11px] text-zinc-500">
                        Maximum admitted pending requests
                      </span>
                    </div>
                    <span className="font-mono text-sm font-semibold text-zinc-200">
                      {status.queue_limit} items
                    </span>
                  </div>

                  {Boolean(info?.synthetic_demo_data) && (
                    <div className="flex items-center gap-2 rounded-md border border-purple-500/20 bg-purple-500/10 px-3 py-2 text-xs text-purple-300">
                      <Info className="size-3.5 shrink-0 text-purple-400" />
                      <span>Synthetic demo dataset generator active</span>
                    </div>
                  )}
                </div>
              ) : (
                <div className="flex items-center justify-center py-8 text-zinc-500 text-xs">
                  <RefreshCw className="size-4 animate-spin mr-2" /> Loading runtime metrics…
                </div>
              )}
            </CardContent>
          </div>
        </Card>

        {/* Card 2: Memory Budget & Safety Margin */}
        <Card className="border-zinc-800 bg-zinc-900/60 shadow-md backdrop-blur-sm flex flex-col justify-between">
          <div>
            <CardHeader className="p-5 pb-3">
              <div className="flex items-center justify-between">
                <div className="flex items-center gap-2.5">
                  <div className="flex size-9 items-center justify-center rounded-lg bg-emerald-500/10 text-emerald-400 border border-emerald-500/20">
                    <HardDrive className="size-4.5" />
                  </div>
                  <div>
                    <CardTitle className="text-sm font-semibold text-zinc-100">
                      Memory Budget & Safety Margin
                    </CardTitle>
                    <CardDescription className="text-xs text-zinc-400">
                      Worker RSS headroom and host capacity limits
                    </CardDescription>
                  </div>
                </div>
                <div className="flex size-7 items-center justify-center rounded-md bg-zinc-800/60 text-zinc-400">
                  <Activity className="size-3.5" />
                </div>
              </div>
            </CardHeader>

            <CardContent className="p-5 pt-2 space-y-4">
              {status ? (
                <div className="space-y-3">
                  <div className="grid grid-cols-2 gap-3">
                    <div className="rounded-lg bg-zinc-950/60 border border-zinc-800/80 p-3 space-y-1">
                      <span className="text-[11px] font-medium text-zinc-500 uppercase tracking-wider block">
                        Worker Memory Budget
                      </span>
                      <span className="font-mono text-sm font-semibold text-zinc-200">
                        {bytes(status.worker_mem_budget_bytes)}
                      </span>
                    </div>

                    <div className="rounded-lg bg-zinc-950/60 border border-zinc-800/80 p-3 space-y-1">
                      <span className="text-[11px] font-medium text-zinc-500 uppercase tracking-wider block">
                        Safety Margin
                      </span>
                      <div className="flex items-center gap-1.5">
                        <span className="font-mono text-sm font-semibold text-emerald-400">
                          {pct(status.memory_safety_margin, 0)}
                        </span>
                        <Badge variant="outline" className="text-[10px] px-1.5 py-0 text-zinc-400">
                          Headroom
                        </Badge>
                      </div>
                    </div>
                  </div>

                  <div className="grid grid-cols-2 gap-3">
                    <div className="rounded-lg bg-zinc-950/60 border border-zinc-800/80 p-3 space-y-1">
                      <span className="text-[11px] font-medium text-zinc-500 uppercase tracking-wider block">
                        Host Free RAM
                      </span>
                      <span className="font-mono text-sm font-semibold text-zinc-200">
                        {bytes(status.host_free_bytes)}
                      </span>
                    </div>

                    <div className="rounded-lg bg-zinc-950/60 border border-zinc-800/80 p-3 space-y-1">
                      <span className="text-[11px] font-medium text-zinc-500 uppercase tracking-wider block">
                        Mispredictions
                      </span>
                      <div className="flex items-center gap-2">
                        <span className="font-mono text-sm font-semibold text-zinc-200">
                          {status.admission_mispredictions}
                        </span>
                        {status.admission_mispredictions === 0 ? (
                          <Badge variant="success" className="text-[10px] px-1.5 py-0">
                            Zero
                          </Badge>
                        ) : (
                          <Badge variant="warning" className="text-[10px] px-1.5 py-0">
                            Elevated
                          </Badge>
                        )}
                      </div>
                    </div>
                  </div>
                </div>
              ) : (
                <div className="flex items-center justify-center py-8 text-zinc-500 text-xs">
                  <RefreshCw className="size-4 animate-spin mr-2" /> Loading memory metrics…
                </div>
              )}
            </CardContent>
          </div>
        </Card>

        {/* Card 3: Worker Pools & Queue Depths */}
        <Card className="border-zinc-800 bg-zinc-900/60 shadow-md backdrop-blur-sm flex flex-col justify-between">
          <div>
            <CardHeader className="p-5 pb-3">
              <div className="flex items-center justify-between">
                <div className="flex items-center gap-2.5">
                  <div className="flex size-9 items-center justify-center rounded-lg bg-indigo-500/10 text-indigo-400 border border-indigo-500/20">
                    <Cpu className="size-4.5" />
                  </div>
                  <div>
                    <CardTitle className="text-sm font-semibold text-zinc-100">
                      Worker Pools & Queue Depths
                    </CardTitle>
                    <CardDescription className="text-xs text-zinc-400">
                      Process isolation across execution pools
                    </CardDescription>
                  </div>
                </div>
                <div className="flex size-7 items-center justify-center rounded-md bg-zinc-800/60 text-zinc-400">
                  <Layers className="size-3.5" />
                </div>
              </div>
            </CardHeader>

            <CardContent className="p-5 pt-2 space-y-3">
              {status ? (
                status.queue_depth.length > 0 ? (
                  <div className="space-y-2">
                    <div className="overflow-x-auto rounded-lg border border-zinc-800/80 bg-zinc-950/60">
                      <table className="w-full text-xs text-left">
                        <thead className="bg-zinc-900/70 text-zinc-400 border-b border-zinc-800">
                          <tr>
                            <th className="py-2 px-3 font-medium">Pool Identifier</th>
                            <th className="py-2 px-3 font-medium text-right">Queued</th>
                            <th className="py-2 px-3 font-medium text-right">Running</th>
                            <th className="py-2 px-3 font-medium text-right">State</th>
                          </tr>
                        </thead>
                        <tbody className="divide-y divide-zinc-800/50">
                          {status.queue_depth.map((q) => {
                            const isActive = q.running > 0;
                            const hasQueue = q.queued > 0;

                            return (
                              <tr key={q.pool} className="hover:bg-zinc-900/40">
                                <td className="py-2 px-3 font-mono font-medium text-zinc-200">
                                  {q.pool}
                                </td>
                                <td className="py-2 px-3 font-mono text-right">
                                  <span
                                    className={cn(
                                      hasQueue ? "text-amber-400 font-semibold" : "text-zinc-500",
                                    )}
                                  >
                                    {q.queued}
                                  </span>
                                </td>
                                <td className="py-2 px-3 font-mono text-right">
                                  <span
                                    className={cn(
                                      isActive ? "text-sky-400 font-semibold" : "text-zinc-500",
                                    )}
                                  >
                                    {q.running}
                                  </span>
                                </td>
                                <td className="py-2 px-3 text-right">
                                  {isActive ? (
                                    <Badge
                                      variant="info"
                                      className="text-[10px] font-mono px-1.5 py-0"
                                    >
                                      Processing
                                    </Badge>
                                  ) : hasQueue ? (
                                    <Badge
                                      variant="warning"
                                      className="text-[10px] font-mono px-1.5 py-0"
                                    >
                                      Queued
                                    </Badge>
                                  ) : (
                                    <Badge
                                      variant="outline"
                                      className="text-[10px] font-mono px-1.5 py-0 text-zinc-500"
                                    >
                                      Idle
                                    </Badge>
                                  )}
                                </td>
                              </tr>
                            );
                          })}
                        </tbody>
                      </table>
                    </div>
                    <p className="text-[11px] text-zinc-500 leading-tight">
                      One worker process per pool ensures peak RSS is isolated to a single runtime.
                    </p>
                  </div>
                ) : (
                  <p className="text-xs text-zinc-500 italic py-4 text-center">
                    No active worker pools registered.
                  </p>
                )
              ) : (
                <div className="flex items-center justify-center py-8 text-zinc-500 text-xs">
                  <RefreshCw className="size-4 animate-spin mr-2" /> Loading pool telemetry…
                </div>
              )}
            </CardContent>
          </div>
        </Card>

        {/* Card 4: Host Telemetry & Git SHA */}
        <Card className="border-zinc-800 bg-zinc-900/60 shadow-md backdrop-blur-sm flex flex-col justify-between">
          <div>
            <CardHeader className="p-5 pb-3">
              <div className="flex items-center justify-between">
                <div className="flex items-center gap-2.5">
                  <div className="flex size-9 items-center justify-center rounded-lg bg-amber-500/10 text-amber-400 border border-amber-500/20">
                    <GitCommit className="size-4.5" />
                  </div>
                  <div>
                    <CardTitle className="text-sm font-semibold text-zinc-100">
                      Host Telemetry & Git SHA
                    </CardTitle>
                    <CardDescription className="text-xs text-zinc-400">
                      Deployment release fingerprint & host metadata
                    </CardDescription>
                  </div>
                </div>
                <div className="flex size-7 items-center justify-center rounded-md bg-zinc-800/60 text-zinc-400">
                  <ShieldCheck className="size-3.5" />
                </div>
              </div>
            </CardHeader>

            <CardContent className="p-5 pt-2 space-y-4">
              {status ? (
                <div className="space-y-3">
                  <div className="rounded-lg bg-zinc-950/60 border border-zinc-800/80 p-3 space-y-1">
                    <span className="text-[11px] font-medium text-zinc-500 uppercase tracking-wider block">
                      Release Commit SHA
                    </span>
                    <div className="flex items-center justify-between">
                      <span className="font-mono text-xs font-semibold text-sky-400 tracking-wider">
                        {(status.git_sha ?? "unknown").slice(0, 12)}
                      </span>
                      <Badge variant="outline" className="font-mono text-[10px] text-zinc-400">
                        {status.git_sha ? status.git_sha.slice(0, 7) : "HEAD"}
                      </Badge>
                    </div>
                  </div>

                  <div className="grid grid-cols-2 gap-3">
                    <div className="rounded-lg bg-zinc-950/60 border border-zinc-800/80 p-3 space-y-1">
                      <span className="text-[11px] font-medium text-zinc-500 uppercase tracking-wider block">
                        API Gateway
                      </span>
                      <div className="flex items-center gap-1.5">
                        <CheckCircle2 className="size-3.5 text-emerald-400" />
                        <span className="text-xs font-medium text-zinc-200">v0.2.0 (FastAPI)</span>
                      </div>
                    </div>

                    <div className="rounded-lg bg-zinc-950/60 border border-zinc-800/80 p-3 space-y-1">
                      <span className="text-[11px] font-medium text-zinc-500 uppercase tracking-wider block">
                        Host Status
                      </span>
                      <div className="flex items-center gap-1.5">
                        <Badge variant="success" className="text-[10px] px-1.5 py-0">
                          HEALTHY
                        </Badge>
                      </div>
                    </div>
                  </div>

                  {Array.isArray(info?.notes) && info.notes.length > 0 && (
                    <div className="rounded-lg bg-zinc-950/40 border border-zinc-800/60 p-3 text-[11px] text-zinc-400 space-y-1">
                      <span className="font-medium text-zinc-300 block">Deployment Notes:</span>
                      <ul className="list-disc list-inside space-y-0.5 text-zinc-400">
                        {info.notes.map((note, idx) => (
                          <li key={idx} className="line-clamp-2">
                            {String(note)}
                          </li>
                        ))}
                      </ul>
                    </div>
                  )}
                </div>
              ) : (
                <div className="flex items-center justify-center py-8 text-zinc-500 text-xs">
                  <RefreshCw className="size-4 animate-spin mr-2" /> Loading host telemetry…
                </div>
              )}
            </CardContent>
          </div>
        </Card>
      </div>
    </div>
  );
}