Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import type {
3 DesktopHostTelemetrySnapshot,
4 ResourceMonitorSnapshotEvent,
5} from "@t3tools/contracts";
6import * as DateTime from "effect/DateTime";
7import * as Effect from "effect/Effect";
8import * as Layer from "effect/Layer";
9import * as Option from "effect/Option";
10import * as Stream from "effect/Stream";
11
12import * as DesktopTelemetryReceiver from "../resourceTelemetry/DesktopTelemetryReceiver.ts";
13import * as NativeTelemetryClient from "../resourceTelemetry/NativeTelemetryClient.ts";
14import * as ResourceAttribution from "../resourceTelemetry/ResourceAttribution.ts";
15import * as ResourceTelemetry from "../resourceTelemetry/ResourceTelemetry.ts";
16import * as ProcessDiagnostics from "./ProcessDiagnostics.ts";
17
18// ProcessDiagnostics treats process.pid as the server, so the fixture PID is an
19// offset from it. A fixed PID collides whenever the test worker is assigned it.
20const SERVER_PID = process.pid;
21const CHILD_PID = SERVER_PID + 1;
22
23function makeNativeSnapshot(
24 processes: ResourceMonitorSnapshotEvent["processes"],
25): ResourceMonitorSnapshotEvent {
26 return {
27 version: 3,
28 type: "snapshot",
29 sequence: 1,
30 sampledAtUnixMs: DateTime.toEpochMillis(DateTime.makeUnsafe("2026-05-05T10:00:00.000Z")),
31 collectionDurationMicros: 250,
32 scannedProcessCount: processes.length,
33 retainedProcessCount: processes.length,
34 inaccessibleProcessCount: 0,
35 processes,
36 };
37}
38
39function layerTelemetryFor(
40 snapshot: ResourceMonitorSnapshotEvent,
41 desktopSnapshot?: DesktopHostTelemetrySnapshot,
42) {
43 const layerNative = NativeTelemetryClient.layerTest({
44 sampleNow: Effect.succeed({ generation: 0, snapshot }),
45 health: Effect.succeed({
46 status: "healthy",
47 hello: Option.none(),
48 lastSampleAt: Option.some(DateTime.makeUnsafe(snapshot.sampledAtUnixMs)),
49 lastError: Option.none(),
50 restartCount: 0,
51 sampleIntervalMs: 1_000,
52 }),
53 });
54 const layerDesktop = desktopSnapshot
55 ? DesktopTelemetryReceiver.layerTest({
56 latest: Effect.succeedSome(desktopSnapshot),
57 health: Effect.succeed({
58 status: "healthy",
59 lastSampleAt: Option.some(DateTime.makeUnsafe(desktopSnapshot.sampledAtUnixMs)),
60 lastError: Option.none(),
61 }),
62 })
63 : DesktopTelemetryReceiver.layerTest();
64 return ResourceTelemetry.layer.pipe(
65 Layer.provide(Layer.mergeAll(layerNative, layerDesktop, ResourceAttribution.layer)),
66 );
67}
68
69describe("ProcessDiagnostics", () => {
70 it.effect("rejects stale process identities before signaling", () =>
71 Effect.gen(function* () {
72 const snapshot = makeNativeSnapshot([]);
73 const layerTelemetry = layerTelemetryFor(snapshot);
74 const layer = ProcessDiagnostics.layer.pipe(Layer.provide(layerTelemetry));
75
76 const result = yield* Effect.service(ProcessDiagnostics.ProcessDiagnostics).pipe(
77 Effect.flatMap((processDiagnostics) =>
78 processDiagnostics.signal({
79 pid: CHILD_PID,
80 startTimeMs: 2_000,
81 signal: "SIGINT",
82 }),
83 ),
84 Effect.provide(layer),
85 );
86
87 expect(result).toEqual({
88 pid: CHILD_PID,
89 signal: "SIGINT",
90 signaled: false,
91 message: Option.some(
92 `Process ${CHILD_PID} no longer matches the selected process identity.`,
93 ),
94 });
95 }),
96 );
97
98 it.effect("refuses to signal when a fresh identity check cannot be collected", () =>
99 Effect.gen(function* () {
100 const snapshot = makeNativeSnapshot([
101 {
102 pid: CHILD_PID,
103 ppid: SERVER_PID,
104 startTimeMs: 2_000,
105 runTimeMs: 4_000,
106 name: "agent",
107 command: "codex app-server",
108 status: "Running",
109 cpuPercent: 1.5,
110 cpuTimeMs: 60,
111 residentBytes: 2_048,
112 virtualBytes: 4_096,
113 ioReadBytes: 300,
114 ioWriteBytes: 400,
115 ioSemantics: "storage",
116 },
117 ]);
118 const staleTelemetry = yield* Effect.service(ResourceTelemetry.ResourceTelemetry).pipe(
119 Effect.flatMap((telemetry) => telemetry.latest),
120 Effect.provide(layerTelemetryFor(snapshot)),
121 );
122 const layerTelemetry = Layer.succeed(
123 ResourceTelemetry.ResourceTelemetry,
124 ResourceTelemetry.ResourceTelemetry.of({
125 latest: Effect.succeed(staleTelemetry),
126 changes: Stream.empty,
127 subscribe: Effect.die("unused"),
128 readHistory: () => Effect.die("unused"),
129 refresh: Effect.fail(
130 new ResourceTelemetry.ResourceTelemetryRefreshFailed({
131 operation: "refresh",
132 cause: new Error("collector unavailable"),
133 }),
134 ),
135 validateProcessIdentity: () => Effect.die("unused"),
136 retry: Effect.die("unused"),
137 }),
138 );
139 const layer = ProcessDiagnostics.layer.pipe(Layer.provide(layerTelemetry));
140
141 const result = yield* Effect.service(ProcessDiagnostics.ProcessDiagnostics).pipe(
142 Effect.flatMap((processDiagnostics) =>
143 processDiagnostics.signal({
144 pid: CHILD_PID,
145 startTimeMs: 2_000,
146 signal: "SIGINT",
147 }),
148 ),
149 Effect.provide(layer),
150 );
151
152 expect(result).toEqual({
153 pid: CHILD_PID,
154 signal: "SIGINT",
155 signaled: false,
156 message: Option.some(
157 `Could not refresh process ${CHILD_PID}; refusing to signal a stale identity.`,
158 ),
159 });
160 }),
161 );
162
163 it.effect("rejects Electron processes as signal targets", () =>
164 Effect.gen(function* () {
165 const sampledAtUnixMs = DateTime.toEpochMillis(
166 DateTime.makeUnsafe("2026-05-05T10:00:00.000Z"),
167 );
168 const snapshot = makeNativeSnapshot([
169 {
170 pid: CHILD_PID,
171 ppid: 1,
172 startTimeMs: 2_000,
173 runTimeMs: 4_000,
174 name: "electron",
175 command: "electron",
176 status: "Running",
177 cpuPercent: 1.5,
178 cpuTimeMs: 60,
179 residentBytes: 2_048,
180 virtualBytes: 4_096,
181 ioReadBytes: 300,
182 ioWriteBytes: 400,
183 ioSemantics: "storage",
184 },
185 ]);
186 const sampledAt = DateTime.makeUnsafe(sampledAtUnixMs);
187 const layerTelemetry = layerTelemetryFor(snapshot, {
188 version: 1,
189 type: "desktopTelemetry",
190 sequence: 1,
191 sampledAtUnixMs,
192 electronPid: CHILD_PID,
193 power: {
194 source: "electron-main",
195 idle: "false",
196 idleSeconds: 0,
197 locked: "false",
198 suspended: false,
199 onBattery: "false",
200 lowPowerMode: "unknown",
201 thermalState: "nominal",
202 stale: false,
203 updatedAt: sampledAt,
204 },
205 speedLimitPercent: Option.none(),
206 electronProcesses: [
207 {
208 pid: CHILD_PID,
209 creationTimeMs: 2_000,
210 type: "Browser",
211 name: "electron",
212 cpuPercent: 1.5,
213 idleWakeupsPerSecond: 0,
214 workingSetBytes: 2_048,
215 peakWorkingSetBytes: 2_048,
216 },
217 ],
218 });
219 const layer = ProcessDiagnostics.layer.pipe(Layer.provide(layerTelemetry));
220
221 const result = yield* Effect.service(ProcessDiagnostics.ProcessDiagnostics).pipe(
222 Effect.flatMap((processDiagnostics) =>
223 processDiagnostics.signal({
224 pid: CHILD_PID,
225 startTimeMs: 2_000,
226 signal: "SIGKILL",
227 }),
228 ),
229 Effect.provide(layer),
230 );
231
232 expect(result).toEqual({
233 pid: CHILD_PID,
234 signal: "SIGKILL",
235 signaled: false,
236 message: Option.some(`Process ${CHILD_PID} is not a signalable T3 backend descendant.`),
237 });
238
239 const diagnostics = yield* Effect.service(ProcessDiagnostics.ProcessDiagnostics).pipe(
240 Effect.flatMap((processDiagnostics) => processDiagnostics.read),
241 Effect.provide(layer),
242 );
243 expect(diagnostics.processes).toEqual([]);
244 expect(diagnostics.processCount).toBe(0);
245 expect(diagnostics.totalCpuPercent).toBe(0);
246 expect(diagnostics.totalRssBytes).toBe(0);
247 }),
248 );
249});