The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { |
| 2 | DesktopHostTelemetrySnapshot, |
| 3 | ResourceMonitorProcessSample, |
| 4 | ResourceMonitorSnapshotEvent, |
| 5 | ResourceTelemetryHealth, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { describe, expect, it } from "@effect/vitest"; |
| 8 | import * as DateTime from "effect/DateTime"; |
| 9 | import * as Option from "effect/Option"; |
| 10 | |
| 11 | import { buildResourceTelemetryHistory } from "./ResourceTelemetryHistory.ts"; |
| 12 | |
| 13 | const SERVER_PID = 100; |
| 14 | const ELECTRON_PID = 200; |
| 15 | const CHILD_PID = 300; |
| 16 | const STARTED_AT_MS = DateTime.toEpochMillis(DateTime.makeUnsafe("2026-06-17T12:00:00.000Z")); |
| 17 | |
| 18 | function processSample( |
| 19 | input: Partial<ResourceMonitorProcessSample> & |
| 20 | Pick<ResourceMonitorProcessSample, "pid" | "ppid" | "startTimeMs">, |
| 21 | ): ResourceMonitorProcessSample { |
| 22 | return { |
| 23 | runTimeMs: 1_000, |
| 24 | name: `process-${input.pid}`, |
| 25 | command: `process-${input.pid}`, |
| 26 | status: "Running", |
| 27 | cpuPercent: 0, |
| 28 | cpuTimeMs: 0, |
| 29 | residentBytes: 1_024, |
| 30 | virtualBytes: 2_048, |
| 31 | ioReadBytes: 0, |
| 32 | ioWriteBytes: 0, |
| 33 | ioSemantics: "storage", |
| 34 | ...input, |
| 35 | }; |
| 36 | } |
| 37 | |
| 38 | function snapshot( |
| 39 | sequence: number, |
| 40 | sampledAtUnixMs: number, |
| 41 | childCpuTimeMs: number, |
| 42 | childWriteBytes: number, |
| 43 | ): ResourceMonitorSnapshotEvent { |
| 44 | const processes = [ |
| 45 | processSample({ pid: SERVER_PID, ppid: 1, startTimeMs: 10 }), |
| 46 | processSample({ |
| 47 | pid: ELECTRON_PID, |
| 48 | ppid: 1, |
| 49 | startTimeMs: 20, |
| 50 | name: "electron", |
| 51 | command: "electron", |
| 52 | }), |
| 53 | processSample({ |
| 54 | pid: CHILD_PID, |
| 55 | ppid: SERVER_PID, |
| 56 | startTimeMs: 30, |
| 57 | name: "codex", |
| 58 | command: "codex app-server", |
| 59 | cpuTimeMs: childCpuTimeMs, |
| 60 | ioWriteBytes: childWriteBytes, |
| 61 | }), |
| 62 | ]; |
| 63 | return { |
| 64 | version: 3, |
| 65 | type: "snapshot", |
| 66 | sequence, |
| 67 | sampledAtUnixMs, |
| 68 | collectionDurationMicros: 100, |
| 69 | scannedProcessCount: processes.length, |
| 70 | retainedProcessCount: processes.length, |
| 71 | inaccessibleProcessCount: 0, |
| 72 | processes, |
| 73 | }; |
| 74 | } |
| 75 | |
| 76 | const health: ResourceTelemetryHealth = { |
| 77 | native: { |
| 78 | status: "healthy", |
| 79 | lastSampleAt: Option.none(), |
| 80 | lastError: Option.none(), |
| 81 | }, |
| 82 | desktop: { |
| 83 | status: "healthy", |
| 84 | lastSampleAt: Option.none(), |
| 85 | lastError: Option.none(), |
| 86 | }, |
| 87 | sidecarVersion: Option.some("0.1.0"), |
| 88 | sidecarPid: Option.some(400), |
| 89 | restartCount: 0, |
| 90 | collectionDurationMicros: 100, |
| 91 | scannedProcessCount: 3, |
| 92 | retainedProcessCount: 3, |
| 93 | inaccessibleProcessCount: 0, |
| 94 | }; |
| 95 | |
| 96 | function desktopSnapshot(): DesktopHostTelemetrySnapshot { |
| 97 | const sampledAt = DateTime.makeUnsafe(STARTED_AT_MS + 1_000); |
| 98 | return { |
| 99 | version: 1, |
| 100 | type: "desktopTelemetry", |
| 101 | sequence: 1, |
| 102 | sampledAtUnixMs: STARTED_AT_MS + 1_000, |
| 103 | electronPid: ELECTRON_PID, |
| 104 | power: { |
| 105 | source: "electron-main", |
| 106 | idle: "false", |
| 107 | idleSeconds: 0, |
| 108 | locked: "false", |
| 109 | suspended: false, |
| 110 | onBattery: "false", |
| 111 | lowPowerMode: "unknown", |
| 112 | thermalState: "nominal", |
| 113 | stale: false, |
| 114 | updatedAt: sampledAt, |
| 115 | }, |
| 116 | speedLimitPercent: Option.none(), |
| 117 | electronProcesses: [ |
| 118 | { |
| 119 | pid: ELECTRON_PID, |
| 120 | creationTimeMs: 20, |
| 121 | type: "Browser", |
| 122 | cpuPercent: 999, |
| 123 | idleWakeupsPerSecond: 999, |
| 124 | workingSetBytes: 999_999, |
| 125 | peakWorkingSetBytes: 999_999, |
| 126 | }, |
| 127 | ], |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | describe("buildResourceTelemetryHistory", () => { |
| 132 | it("replays native snapshots on demand without applying current Electron metrics", () => { |
| 133 | const history = buildResourceTelemetryHistory({ |
| 134 | readAt: DateTime.makeUnsafe(STARTED_AT_MS + 2_000), |
| 135 | windowMs: 10_000, |
| 136 | bucketMs: 10_000, |
| 137 | sampleIntervalMs: 1_000, |
| 138 | serverPid: SERVER_PID, |
| 139 | sidecarPid: Option.some(400), |
| 140 | desktopSnapshot: Option.some(desktopSnapshot()), |
| 141 | snapshots: [ |
| 142 | snapshot(1, STARTED_AT_MS, 100, 1_000), |
| 143 | snapshot(2, STARTED_AT_MS + 1_000, 350, 5_000), |
| 144 | ], |
| 145 | health, |
| 146 | }); |
| 147 | |
| 148 | const child = history.topProcesses.find((process) => process.identity.pid === CHILD_PID); |
| 149 | const electron = history.topProcesses.find((process) => process.identity.pid === ELECTRON_PID); |
| 150 | expect(child?.sampleCount).toBe(2); |
| 151 | expect(child?.cpuTimeMs).toBe(250); |
| 152 | expect(child?.ioWriteBytes).toBe(4_000); |
| 153 | expect(electron?.category).toBe("electron-main"); |
| 154 | expect(electron?.currentRssBytes).toBe(1_024); |
| 155 | expect(history.buckets.reduce((total, bucket) => total + bucket.ioWriteBytes, 0)).toBe(4_000); |
| 156 | }); |
| 157 | |
| 158 | it("uses observed RSS for the history-window peak instead of the lifetime process peak", () => { |
| 159 | const first = snapshot(1, STARTED_AT_MS, 100, 1_000); |
| 160 | const second = snapshot(2, STARTED_AT_MS + 1_000, 200, 2_000); |
| 161 | const history = buildResourceTelemetryHistory({ |
| 162 | readAt: DateTime.makeUnsafe(STARTED_AT_MS + 2_000), |
| 163 | windowMs: 10_000, |
| 164 | bucketMs: 10_000, |
| 165 | sampleIntervalMs: 1_000, |
| 166 | serverPid: SERVER_PID, |
| 167 | sidecarPid: Option.none(), |
| 168 | desktopSnapshot: Option.none(), |
| 169 | snapshots: [ |
| 170 | { |
| 171 | ...first, |
| 172 | processes: first.processes.map((process) => |
| 173 | process.pid === CHILD_PID |
| 174 | ? { ...process, residentBytes: 2_000, peakResidentBytes: 50_000 } |
| 175 | : process, |
| 176 | ), |
| 177 | }, |
| 178 | { |
| 179 | ...second, |
| 180 | processes: second.processes.map((process) => |
| 181 | process.pid === CHILD_PID |
| 182 | ? { ...process, residentBytes: 3_000, peakResidentBytes: 60_000 } |
| 183 | : process, |
| 184 | ), |
| 185 | }, |
| 186 | ], |
| 187 | health, |
| 188 | }); |
| 189 | |
| 190 | expect( |
| 191 | history.topProcesses.find((process) => process.identity.pid === CHILD_PID)?.peakRssBytes, |
| 192 | ).toBe(3_000); |
| 193 | }); |
| 194 | |
| 195 | it("retains cumulative baselines while a process is absent from an intermediate sample", () => { |
| 196 | const first = snapshot(1, STARTED_AT_MS, 100, 1_000); |
| 197 | const absent = snapshot(2, STARTED_AT_MS + 1_000, 0, 0); |
| 198 | const returned = snapshot(3, STARTED_AT_MS + 2_000, 350, 5_000); |
| 199 | const history = buildResourceTelemetryHistory({ |
| 200 | readAt: DateTime.makeUnsafe(STARTED_AT_MS + 3_000), |
| 201 | windowMs: 10_000, |
| 202 | bucketMs: 10_000, |
| 203 | sampleIntervalMs: 1_000, |
| 204 | serverPid: SERVER_PID, |
| 205 | sidecarPid: Option.none(), |
| 206 | desktopSnapshot: Option.none(), |
| 207 | snapshots: [ |
| 208 | first, |
| 209 | { |
| 210 | ...absent, |
| 211 | processes: absent.processes.filter((process) => process.pid !== CHILD_PID), |
| 212 | }, |
| 213 | returned, |
| 214 | ], |
| 215 | health, |
| 216 | }); |
| 217 | |
| 218 | const child = history.topProcesses.find((process) => process.identity.pid === CHILD_PID); |
| 219 | expect(child?.cpuTimeMs).toBe(250); |
| 220 | expect(child?.ioWriteBytes).toBe(4_000); |
| 221 | }); |
| 222 | |
| 223 | it("uses the preceding sample as a baseline without attributing pre-window deltas", () => { |
| 224 | const readAtMs = STARTED_AT_MS + 10_000; |
| 225 | const history = buildResourceTelemetryHistory({ |
| 226 | readAt: DateTime.makeUnsafe(readAtMs), |
| 227 | windowMs: 5_000, |
| 228 | bucketMs: 5_000, |
| 229 | sampleIntervalMs: 1_000, |
| 230 | serverPid: SERVER_PID, |
| 231 | sidecarPid: Option.none(), |
| 232 | desktopSnapshot: Option.none(), |
| 233 | snapshots: [ |
| 234 | snapshot(1, STARTED_AT_MS, 100, 1_000), |
| 235 | snapshot(2, STARTED_AT_MS + 5_000, 600, 6_000), |
| 236 | snapshot(3, readAtMs + 1_000, 10_000, 20_000), |
| 237 | ], |
| 238 | health, |
| 239 | }); |
| 240 | |
| 241 | const child = history.topProcesses.find((process) => process.identity.pid === CHILD_PID); |
| 242 | expect(child?.sampleCount).toBe(1); |
| 243 | expect(child?.cpuTimeMs).toBe(0); |
| 244 | expect(child?.ioWriteBytes).toBe(0); |
| 245 | expect(history.buckets.reduce((total, bucket) => total + bucket.ioWriteBytes, 0)).toBe(0); |
| 246 | expect( |
| 247 | history.buckets.every((bucket) => DateTime.toEpochMillis(bucket.startedAt) <= readAtMs), |
| 248 | ).toBe(true); |
| 249 | }); |
| 250 | |
| 251 | it("prorates a cumulative delta that crosses the history window boundary", () => { |
| 252 | const readAtMs = STARTED_AT_MS + 10_000; |
| 253 | const history = buildResourceTelemetryHistory({ |
| 254 | readAt: DateTime.makeUnsafe(readAtMs), |
| 255 | windowMs: 5_000, |
| 256 | bucketMs: 5_000, |
| 257 | sampleIntervalMs: 1_000, |
| 258 | serverPid: SERVER_PID, |
| 259 | sidecarPid: Option.none(), |
| 260 | desktopSnapshot: Option.none(), |
| 261 | snapshots: [ |
| 262 | snapshot(1, STARTED_AT_MS, 100, 1_000), |
| 263 | snapshot(2, STARTED_AT_MS + 7_500, 850, 8_500), |
| 264 | ], |
| 265 | health, |
| 266 | }); |
| 267 | |
| 268 | const child = history.topProcesses.find((process) => process.identity.pid === CHILD_PID); |
| 269 | expect(child?.cpuTimeMs).toBe(250); |
| 270 | expect(child?.ioWriteBytes).toBe(2_500); |
| 271 | expect(history.buckets.reduce((total, bucket) => total + bucket.ioWriteBytes, 0)).toBe(2_500); |
| 272 | }); |
| 273 | |
| 274 | it("replays the Electron root identity recorded with each native sample", () => { |
| 275 | const oldElectron = snapshot(1, STARTED_AT_MS, 100, 1_000); |
| 276 | const restartedElectron = snapshot(2, STARTED_AT_MS + 1_000, 200, 2_000); |
| 277 | const history = buildResourceTelemetryHistory({ |
| 278 | readAt: DateTime.makeUnsafe(STARTED_AT_MS + 2_000), |
| 279 | windowMs: 10_000, |
| 280 | bucketMs: 10_000, |
| 281 | sampleIntervalMs: 1_000, |
| 282 | serverPid: SERVER_PID, |
| 283 | sidecarPid: Option.none(), |
| 284 | desktopSnapshot: Option.none(), |
| 285 | snapshots: [ |
| 286 | { |
| 287 | ...oldElectron, |
| 288 | externalProcesses: [{ pid: ELECTRON_PID, startTimeMs: 20 }], |
| 289 | }, |
| 290 | { |
| 291 | ...restartedElectron, |
| 292 | externalProcesses: [{ pid: 201, startTimeMs: 40 }], |
| 293 | processes: [ |
| 294 | ...restartedElectron.processes.filter((process) => process.pid !== ELECTRON_PID), |
| 295 | processSample({ |
| 296 | pid: ELECTRON_PID, |
| 297 | ppid: SERVER_PID, |
| 298 | startTimeMs: 999, |
| 299 | name: "reused", |
| 300 | command: "unrelated process", |
| 301 | }), |
| 302 | processSample({ |
| 303 | pid: 201, |
| 304 | ppid: 1, |
| 305 | startTimeMs: 40, |
| 306 | name: "electron", |
| 307 | command: "electron", |
| 308 | }), |
| 309 | ], |
| 310 | }, |
| 311 | ], |
| 312 | health, |
| 313 | }); |
| 314 | |
| 315 | expect( |
| 316 | history.topProcesses.find( |
| 317 | (process) => process.identity.pid === ELECTRON_PID && process.identity.startTimeMs === 20, |
| 318 | )?.category, |
| 319 | ).toBe("electron-main"); |
| 320 | expect( |
| 321 | history.topProcesses.find( |
| 322 | (process) => process.identity.pid === ELECTRON_PID && process.identity.startTimeMs === 999, |
| 323 | )?.category, |
| 324 | ).toBe("server-child"); |
| 325 | expect(history.topProcesses.find((process) => process.identity.pid === 201)?.category).toBe( |
| 326 | "electron-main", |
| 327 | ); |
| 328 | }); |
| 329 | }); |