Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import {
3 type EnvironmentId,
4 type HostPressureLevel,
5 worstHostPressureLevel,
6} from "@t3tools/contracts";
7import { useGT, plural, part } from "gt-react";
8import { useState } from "react";
9
10import { formatBytes } from "../../lib/formatBytes";
11import { environmentHostPressureAtom, environmentPresentations } from "../../state/presentation";
12import { serverEnvironment } from "../../state/server";
13import { ThreadDetailsSection } from "./ThreadDetailsSection";
14import { UsageRow, type UsageTone } from "./ThreadDetailsUsage";
15
16const toneOf = (level: HostPressureLevel | undefined): UsageTone =>
17 level === "severe" ? "error" : level === "elevated" ? "warning" : null;
18
19const percent = (fraction: number) => Math.round(Math.max(0, Math.min(1, fraction)) * 100);
20
21/** CPU, memory and disk on the machine the thread runs on, colored by whether work there stalls. */
22export function ThreadDetailsMachineLoad({
23 environmentId,
24}: {
25 readonly environmentId: EnvironmentId;
26}) {
27 const gt = useGT();
28 const result = useAtomValue(serverEnvironment.liveHostResources({ environmentId, input: {} }));
29 const pressure = useAtomValue(environmentHostPressureAtom, (pressures) =>
30 pressures.get(environmentId),
31 );
32 const connected = useAtomValue(
33 environmentPresentations.presentationsAtom,
34 (presentations) => presentations.get(environmentId)?.connection.phase === "connected",
35 );
36 // A reconnecting query keeps its last sample, also briefly after it reconnects. Live gauges
37 // stay hidden until a sample newer than the one held when the connection dropped arrives.
38 const sample = result._tag === "Success" ? result.value : null;
39 const [staleSample, setStaleSample] = useState<typeof sample>(null);
40 if (!connected && sample !== null && sample !== staleSample) setStaleSample(sample);
41 if (!connected || sample === null || sample === staleSample) return null;
42 const snapshot = sample;
43 const usedMemory = snapshot.totalMemoryBytes - snapshot.availableMemoryBytes;
44 const diskFree = snapshot.diskFreeBytes ?? null;
45 const diskTotal = snapshot.diskTotalBytes ?? null;
46 // CPU and storage stalls both slow work down, so the CPU row carries the worse of the two.
47 const cpuTone =
48 pressure === undefined
49 ? null
50 : toneOf(worstHostPressureLevel({ ...pressure, memory: "none", disk: "none" }));
51 return (
52 <ThreadDetailsSection
53 headingId="thread-details-machine-load-heading"
54 title={gt`Machine load`}
55 showHeading={false}
56 >
57 <div className="grid grid-cols-[auto_minmax(3rem,1fr)_auto] gap-x-3 text-xs">
58 <UsageRow
59 label={gt({ context: "machine load row label" })`CPU`}
60 fill={snapshot.cpuUtilization === null ? null : percent(snapshot.cpuUtilization)}
61 tone={cpuTone}
62 muted
63 value={
64 snapshot.cpuUtilization === null
65 ? gt`Unavailable`
66 : `${percent(snapshot.cpuUtilization)}%`
67 }
68 tooltip={
69 <span className="text-foreground">
70 {cpuTone === null
71 ? gt`${plural(
72 { count: snapshot.cpuCount },
73 {
74 one: part`${{ count: snapshot.cpuCount }} CPU`,
75 other: part`${{ count: snapshot.cpuCount }} CPUs`,
76 },
77 )}`
78 : gt`Work waits for a free CPU or for the disk, so agents run slowly.`}
79 </span>
80 }
81 />
82 <UsageRow
83 label={gt({ context: "machine load row label" })`Memory`}
84 fill={
85 snapshot.totalMemoryBytes > 0 ? percent(usedMemory / snapshot.totalMemoryBytes) : null
86 }
87 tone={toneOf(pressure?.memory)}
88 muted
89 value={`${formatBytes(usedMemory)} / ${formatBytes(snapshot.totalMemoryBytes)}`}
90 tooltip={
91 <span className="text-foreground">
92 {pressure?.memory === undefined || pressure.memory === "none"
93 ? gt`${{ available: formatBytes(snapshot.availableMemoryBytes) }} available`
94 : gt`Agents keep waiting while the system frees up memory.`}
95 </span>
96 }
97 />
98 {diskFree !== null && diskTotal !== null && diskTotal > 0 ? (
99 <UsageRow
100 label={gt({ context: "machine load row label" })`Disk`}
101 fill={percent((diskTotal - diskFree) / diskTotal)}
102 tone={toneOf(pressure?.disk)}
103 muted
104 value={gt`${{ free: formatBytes(diskFree) }} free`}
105 tooltip={
106 <span className="text-foreground">
107 {gt`Where T3 Code keeps its data and worktrees`}
108 </span>
109 }
110 />
111 ) : null}
112 </div>
113 </ThreadDetailsSection>
114 );
115}