Open History to see the last commit
1import * as Schema from "effect/Schema";
2
3import { NonNegativeInt, PositiveInt, TrimmedNonEmptyString } from "./baseSchemas.ts";
4import { HostPowerSnapshot } from "./background.ts";
5
6export const RESOURCE_MONITOR_PROTOCOL_VERSION = 2 as const;
7
8export const ResourceTelemetryIoSemantics = Schema.Literals([
9 "storage",
10 "logical",
11 "all-io",
12 "unavailable",
13]);
14export type ResourceTelemetryIoSemantics = typeof ResourceTelemetryIoSemantics.Type;
15
16export const ResourceTelemetryProcessCategory = Schema.Literals([
17 "server",
18 "server-child",
19 "provider-root",
20 "terminal-root",
21 "electron-main",
22 "electron-renderer",
23 "electron-gpu",
24 "electron-utility",
25 "resource-monitor",
26 "unknown-t3",
27]);
28export type ResourceTelemetryProcessCategory = typeof ResourceTelemetryProcessCategory.Type;
29
30export const ResourceTelemetrySourceStatus = Schema.Literals([
31 "starting",
32 "healthy",
33 "degraded",
34 "unavailable",
35 "stopped",
36]);
37export type ResourceTelemetrySourceStatus = typeof ResourceTelemetrySourceStatus.Type;
38
39export const ResourceTelemetryProcessIdentity = Schema.Struct({
40 pid: PositiveInt,
41 startTimeMs: NonNegativeInt,
42});
43export type ResourceTelemetryProcessIdentity = typeof ResourceTelemetryProcessIdentity.Type;
44
45export const ResourceMonitorExternalProcess = Schema.Struct({
46 pid: PositiveInt,
47 startTimeMs: Schema.optionalKey(NonNegativeInt),
48});
49export type ResourceMonitorExternalProcess = typeof ResourceMonitorExternalProcess.Type;
50
51export const ResourceMonitorCapabilities = Schema.Struct({
52 cumulativeCpuTime: Schema.Boolean,
53 currentCpuPercent: Schema.Boolean,
54 residentMemory: Schema.Boolean,
55 virtualMemory: Schema.Boolean,
56 ioBytes: Schema.Boolean,
57 processStartTime: Schema.Boolean,
58 processTree: Schema.Boolean,
59});
60export type ResourceMonitorCapabilities = typeof ResourceMonitorCapabilities.Type;
61
62export const ResourceMonitorProcessSample = Schema.Struct({
63 pid: PositiveInt,
64 ppid: NonNegativeInt,
65 startTimeMs: NonNegativeInt,
66 runTimeMs: NonNegativeInt,
67 name: Schema.String,
68 command: Schema.String,
69 status: Schema.String,
70 cpuPercent: Schema.Number,
71 cpuTimeMs: NonNegativeInt,
72 residentBytes: NonNegativeInt,
73 virtualBytes: NonNegativeInt,
74 ioReadBytes: NonNegativeInt,
75 ioWriteBytes: NonNegativeInt,
76 ioSemantics: Schema.Literals(["storage", "all-io"]),
77});
78export type ResourceMonitorProcessSample = typeof ResourceMonitorProcessSample.Type;
79
80export const ResourceMonitorConfigureCommand = Schema.Struct({
81 version: Schema.Literal(RESOURCE_MONITOR_PROTOCOL_VERSION),
82 type: Schema.Literal("configure"),
83 rootPid: PositiveInt,
84 sampleIntervalMs: NonNegativeInt,
85 externalProcesses: Schema.Array(ResourceMonitorExternalProcess),
86});
87export type ResourceMonitorConfigureCommand = typeof ResourceMonitorConfigureCommand.Type;
88
89export const ResourceMonitorSetExternalProcessesCommand = Schema.Struct({
90 version: Schema.Literal(RESOURCE_MONITOR_PROTOCOL_VERSION),
91 type: Schema.Literal("setExternalProcesses"),
92 processes: Schema.Array(ResourceMonitorExternalProcess),
93});
94export type ResourceMonitorSetExternalProcessesCommand =
95 typeof ResourceMonitorSetExternalProcessesCommand.Type;
96
97export const ResourceMonitorSampleNowCommand = Schema.Struct({
98 version: Schema.Literal(RESOURCE_MONITOR_PROTOCOL_VERSION),
99 type: Schema.Literal("sampleNow"),
100 requestId: TrimmedNonEmptyString,
101});
102export type ResourceMonitorSampleNowCommand = typeof ResourceMonitorSampleNowCommand.Type;
103
104export const ResourceMonitorSetSampleIntervalCommand = Schema.Struct({
105 version: Schema.Literal(RESOURCE_MONITOR_PROTOCOL_VERSION),
106 type: Schema.Literal("setSampleInterval"),
107 sampleIntervalMs: NonNegativeInt,
108});
109export type ResourceMonitorSetSampleIntervalCommand =
110 typeof ResourceMonitorSetSampleIntervalCommand.Type;
111
112export const ResourceMonitorSetStreamingCommand = Schema.Struct({
113 version: Schema.Literal(RESOURCE_MONITOR_PROTOCOL_VERSION),
114 type: Schema.Literal("setStreaming"),
115 enabled: Schema.Boolean,
116});
117export type ResourceMonitorSetStreamingCommand = typeof ResourceMonitorSetStreamingCommand.Type;
118
119export const ResourceMonitorReadHistoryCommand = Schema.Struct({
120 version: Schema.Literal(RESOURCE_MONITOR_PROTOCOL_VERSION),
121 type: Schema.Literal("readHistory"),
122 requestId: TrimmedNonEmptyString,
123 windowMs: NonNegativeInt,
124});
125export type ResourceMonitorReadHistoryCommand = typeof ResourceMonitorReadHistoryCommand.Type;
126
127export const ResourceMonitorShutdownCommand = Schema.Struct({
128 version: Schema.Literal(RESOURCE_MONITOR_PROTOCOL_VERSION),
129 type: Schema.Literal("shutdown"),
130});
131export type ResourceMonitorShutdownCommand = typeof ResourceMonitorShutdownCommand.Type;
132
133export const ResourceMonitorCommand = Schema.Union([
134 ResourceMonitorConfigureCommand,
135 ResourceMonitorSetExternalProcessesCommand,
136 ResourceMonitorSetSampleIntervalCommand,
137 ResourceMonitorSetStreamingCommand,
138 ResourceMonitorSampleNowCommand,
139 ResourceMonitorReadHistoryCommand,
140 ResourceMonitorShutdownCommand,
141]);
142export type ResourceMonitorCommand = typeof ResourceMonitorCommand.Type;
143
144export const ResourceMonitorHelloEvent = Schema.Struct({
145 version: Schema.Literal(RESOURCE_MONITOR_PROTOCOL_VERSION),
146 type: Schema.Literal("hello"),
147 sidecarVersion: TrimmedNonEmptyString,
148 sidecarPid: PositiveInt,
149 platform: TrimmedNonEmptyString,
150 arch: TrimmedNonEmptyString,
151 capabilities: ResourceMonitorCapabilities,
152});
153export type ResourceMonitorHelloEvent = typeof ResourceMonitorHelloEvent.Type;
154
155export const ResourceMonitorSnapshotEvent = Schema.Struct({
156 version: Schema.Literal(RESOURCE_MONITOR_PROTOCOL_VERSION),
157 type: Schema.Literal("snapshot"),
158 sequence: NonNegativeInt,
159 sampledAtUnixMs: NonNegativeInt,
160 collectionDurationMicros: NonNegativeInt,
161 scannedProcessCount: NonNegativeInt,
162 retainedProcessCount: NonNegativeInt,
163 inaccessibleProcessCount: NonNegativeInt,
164 requestId: Schema.optionalKey(TrimmedNonEmptyString),
165 externalProcesses: Schema.optionalKey(Schema.Array(ResourceMonitorExternalProcess)),
166 processes: Schema.Array(ResourceMonitorProcessSample),
167});
168export type ResourceMonitorSnapshotEvent = typeof ResourceMonitorSnapshotEvent.Type;
169
170export const ResourceMonitorHistoryChunkEvent = Schema.Struct({
171 version: Schema.Literal(RESOURCE_MONITOR_PROTOCOL_VERSION),
172 type: Schema.Literal("historyChunk"),
173 requestId: TrimmedNonEmptyString,
174 done: Schema.Boolean,
175 snapshots: Schema.Array(ResourceMonitorSnapshotEvent),
176});
177export type ResourceMonitorHistoryChunkEvent = typeof ResourceMonitorHistoryChunkEvent.Type;
178
179export const ResourceMonitorErrorEvent = Schema.Struct({
180 version: Schema.Literal(RESOURCE_MONITOR_PROTOCOL_VERSION),
181 type: Schema.Literal("error"),
182 code: TrimmedNonEmptyString,
183 message: TrimmedNonEmptyString,
184 recoverable: Schema.Boolean,
185});
186export type ResourceMonitorErrorEvent = typeof ResourceMonitorErrorEvent.Type;
187
188export const ResourceMonitorEvent = Schema.Union([
189 ResourceMonitorHelloEvent,
190 ResourceMonitorSnapshotEvent,
191 ResourceMonitorHistoryChunkEvent,
192 ResourceMonitorErrorEvent,
193]);
194export type ResourceMonitorEvent = typeof ResourceMonitorEvent.Type;
195
196export const DesktopElectronProcessType = Schema.Literals([
197 "Browser",
198 "Tab",
199 "Utility",
200 "Zygote",
201 "Sandbox helper",
202 "GPU",
203 "Pepper Plugin",
204 "Pepper Plugin Broker",
205 "Unknown",
206]);
207export type DesktopElectronProcessType = typeof DesktopElectronProcessType.Type;
208
209export const DesktopElectronProcessMetric = Schema.Struct({
210 pid: PositiveInt,
211 creationTimeMs: NonNegativeInt,
212 type: DesktopElectronProcessType,
213 name: Schema.optionalKey(Schema.String),
214 serviceName: Schema.optionalKey(Schema.String),
215 cpuPercent: Schema.Number,
216 cumulativeCpuSeconds: Schema.optionalKey(Schema.Number),
217 idleWakeupsPerSecond: Schema.Number,
218 workingSetBytes: NonNegativeInt,
219 peakWorkingSetBytes: NonNegativeInt,
220});
221export type DesktopElectronProcessMetric = typeof DesktopElectronProcessMetric.Type;
222
223const DesktopHostPowerSnapshot = Schema.Struct({
224 ...HostPowerSnapshot.fields,
225 updatedAt: Schema.DateTimeUtcFromString,
226});
227
228export const DesktopHostTelemetrySnapshot = Schema.Struct({
229 version: Schema.Literal(1),
230 type: Schema.Literal("desktopTelemetry"),
231 sequence: NonNegativeInt,
232 sampledAtUnixMs: NonNegativeInt,
233 electronPid: PositiveInt,
234 power: DesktopHostPowerSnapshot,
235 speedLimitPercent: Schema.OptionFromNullOr(Schema.Number),
236 electronProcesses: Schema.Array(DesktopElectronProcessMetric),
237});
238export type DesktopHostTelemetrySnapshot = typeof DesktopHostTelemetrySnapshot.Type;
239
240export const DesktopHostTelemetryHello = Schema.Struct({
241 version: Schema.Literal(1),
242 type: Schema.Literal("desktopTelemetryHello"),
243 electronPid: PositiveInt,
244});
245export type DesktopHostTelemetryHello = typeof DesktopHostTelemetryHello.Type;
246
247export const DesktopHostTelemetryMessage = Schema.Union([
248 DesktopHostTelemetryHello,
249 DesktopHostTelemetrySnapshot,
250]);
251export type DesktopHostTelemetryMessage = typeof DesktopHostTelemetryMessage.Type;
252
253export const DesktopTelemetrySetDiagnosticsDemand = Schema.Struct({
254 version: Schema.Literal(1),
255 type: Schema.Literal("setDiagnosticsDemand"),
256 enabled: Schema.Boolean,
257});
258export type DesktopTelemetrySetDiagnosticsDemand = typeof DesktopTelemetrySetDiagnosticsDemand.Type;
259
260export const DesktopTelemetrySetHostPowerIntervals = Schema.Struct({
261 version: Schema.Literal(1),
262 type: Schema.Literal("setHostPowerIntervals"),
263 activeIntervalMs: PositiveInt,
264 idleIntervalMs: PositiveInt,
265});
266export type DesktopTelemetrySetHostPowerIntervals =
267 typeof DesktopTelemetrySetHostPowerIntervals.Type;
268
269export const DesktopTelemetryControlMessage = Schema.Union([
270 DesktopTelemetrySetDiagnosticsDemand,
271 DesktopTelemetrySetHostPowerIntervals,
272]);
273export type DesktopTelemetryControlMessage = typeof DesktopTelemetryControlMessage.Type;
274
275export const ResourceTelemetryProcess = Schema.Struct({
276 identity: ResourceTelemetryProcessIdentity,
277 ppid: NonNegativeInt,
278 childPids: Schema.Array(PositiveInt),
279 depth: NonNegativeInt,
280 name: Schema.String,
281 command: Schema.String,
282 status: Schema.String,
283 category: ResourceTelemetryProcessCategory,
284 electronType: Schema.optionalKey(DesktopElectronProcessType),
285 electronServiceName: Schema.optionalKey(Schema.String),
286 cpuPercent: Schema.Number,
287 cpuTimeMs: NonNegativeInt,
288 residentBytes: NonNegativeInt,
289 peakResidentBytes: NonNegativeInt,
290 virtualBytes: NonNegativeInt,
291 ioReadBytes: NonNegativeInt,
292 ioWriteBytes: NonNegativeInt,
293 ioReadBytesPerSecond: Schema.Number,
294 ioWriteBytesPerSecond: Schema.Number,
295 ioSemantics: ResourceTelemetryIoSemantics,
296 idleWakeupsPerSecond: Schema.optionalKey(Schema.Number),
297 runTimeMs: NonNegativeInt,
298 firstSeenAt: Schema.DateTimeUtc,
299 lastSeenAt: Schema.DateTimeUtc,
300});
301export type ResourceTelemetryProcess = typeof ResourceTelemetryProcess.Type;
302
303export const ResourceTelemetryAggregate = Schema.Struct({
304 processCount: NonNegativeInt,
305 currentCpuPercent: Schema.Number,
306 cpuTimeMs: NonNegativeInt,
307 currentRssBytes: NonNegativeInt,
308 peakRssBytes: NonNegativeInt,
309 ioReadBytes: NonNegativeInt,
310 ioWriteBytes: NonNegativeInt,
311 ioReadBytesPerSecond: Schema.Number,
312 ioWriteBytesPerSecond: Schema.Number,
313 processStarts: NonNegativeInt,
314 processExits: NonNegativeInt,
315});
316export type ResourceTelemetryAggregate = typeof ResourceTelemetryAggregate.Type;
317
318export const ResourceTelemetryGroups = Schema.Struct({
319 backend: ResourceTelemetryAggregate,
320 electron: ResourceTelemetryAggregate,
321 monitor: ResourceTelemetryAggregate,
322 allT3: ResourceTelemetryAggregate,
323});
324export type ResourceTelemetryGroups = typeof ResourceTelemetryGroups.Type;
325
326export const ResourceTelemetrySourceHealth = Schema.Struct({
327 status: ResourceTelemetrySourceStatus,
328 lastSampleAt: Schema.Option(Schema.DateTimeUtc),
329 lastError: Schema.Option(TrimmedNonEmptyString),
330});
331export type ResourceTelemetrySourceHealth = typeof ResourceTelemetrySourceHealth.Type;
332
333export const ResourceTelemetryHealth = Schema.Struct({
334 native: ResourceTelemetrySourceHealth,
335 desktop: ResourceTelemetrySourceHealth,
336 sidecarVersion: Schema.Option(TrimmedNonEmptyString),
337 sidecarPid: Schema.Option(PositiveInt),
338 restartCount: NonNegativeInt,
339 collectionDurationMicros: NonNegativeInt,
340 scannedProcessCount: NonNegativeInt,
341 retainedProcessCount: NonNegativeInt,
342 inaccessibleProcessCount: NonNegativeInt,
343});
344export type ResourceTelemetryHealth = typeof ResourceTelemetryHealth.Type;
345
346export const ResourceAttributionEntry = Schema.Struct({
347 component: TrimmedNonEmptyString,
348 operation: TrimmedNonEmptyString,
349 logicalReadBytes: NonNegativeInt,
350 logicalWriteBytes: NonNegativeInt,
351 count: NonNegativeInt,
352 durationMs: NonNegativeInt,
353});
354export type ResourceAttributionEntry = typeof ResourceAttributionEntry.Type;
355
356export const ResourceAttributionSnapshot = Schema.Struct({
357 readAt: Schema.DateTimeUtc,
358 entries: Schema.Array(ResourceAttributionEntry),
359});
360export type ResourceAttributionSnapshot = typeof ResourceAttributionSnapshot.Type;
361
362export const ResourceTelemetrySnapshot = Schema.Struct({
363 readAt: Schema.DateTimeUtc,
364 sampleIntervalMs: NonNegativeInt,
365 processes: Schema.Array(ResourceTelemetryProcess),
366 groups: ResourceTelemetryGroups,
367 power: HostPowerSnapshot,
368 speedLimitPercent: Schema.Option(Schema.Number),
369 attribution: ResourceAttributionSnapshot,
370 health: ResourceTelemetryHealth,
371});
372export type ResourceTelemetrySnapshot = typeof ResourceTelemetrySnapshot.Type;
373
374export const ResourceTelemetryHistoryInput = Schema.Struct({
375 windowMs: NonNegativeInt,
376 bucketMs: NonNegativeInt,
377});
378export type ResourceTelemetryHistoryInput = typeof ResourceTelemetryHistoryInput.Type;
379
380export const ResourceTelemetryHistoryBucket = Schema.Struct({
381 startedAt: Schema.DateTimeUtc,
382 endedAt: Schema.DateTimeUtc,
383 avgCpuPercent: Schema.Number,
384 maxCpuPercent: Schema.Number,
385 maxRssBytes: NonNegativeInt,
386 ioReadBytes: NonNegativeInt,
387 ioWriteBytes: NonNegativeInt,
388 maxProcessCount: NonNegativeInt,
389});
390export type ResourceTelemetryHistoryBucket = typeof ResourceTelemetryHistoryBucket.Type;
391
392export const ResourceTelemetryProcessSummary = Schema.Struct({
393 identity: ResourceTelemetryProcessIdentity,
394 ppid: NonNegativeInt,
395 depth: NonNegativeInt,
396 name: Schema.String,
397 command: Schema.String,
398 category: ResourceTelemetryProcessCategory,
399 firstSeenAt: Schema.DateTimeUtc,
400 lastSeenAt: Schema.DateTimeUtc,
401 currentCpuPercent: Schema.Number,
402 avgCpuPercent: Schema.Number,
403 maxCpuPercent: Schema.Number,
404 cpuTimeMs: NonNegativeInt,
405 currentRssBytes: NonNegativeInt,
406 peakRssBytes: NonNegativeInt,
407 ioReadBytes: NonNegativeInt,
408 ioWriteBytes: NonNegativeInt,
409 ioSemantics: ResourceTelemetryIoSemantics,
410 sampleCount: NonNegativeInt,
411});
412export type ResourceTelemetryProcessSummary = typeof ResourceTelemetryProcessSummary.Type;
413
414export const ResourceTelemetryHistory = Schema.Struct({
415 readAt: Schema.DateTimeUtc,
416 windowMs: NonNegativeInt,
417 bucketMs: NonNegativeInt,
418 sampleIntervalMs: NonNegativeInt,
419 retainedSampleCount: NonNegativeInt,
420 buckets: Schema.Array(ResourceTelemetryHistoryBucket),
421 topProcesses: Schema.Array(ResourceTelemetryProcessSummary),
422 health: ResourceTelemetryHealth,
423});
424export type ResourceTelemetryHistory = typeof ResourceTelemetryHistory.Type;
425
426export const ResourceTelemetryRetryResult = Schema.Struct({
427 accepted: Schema.Boolean,
428 snapshot: ResourceTelemetrySnapshot,
429});
430export type ResourceTelemetryRetryResult = typeof ResourceTelemetryRetryResult.Type;