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