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 { 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/**
322 * Server -> desktop main: run the app's own update flow now (check ->
323 * download -> quit-and-install) with no local confirmation. The remote click
324 * on the machine that sent the RPC is the consent.
325 */
326export const DesktopTelemetryRequestDesktopUpdate = Schema.Struct({
327 version: Schema.Literal(1),
328 type: Schema.Literal("requestDesktopUpdate"),
329 requestId: TrimmedNonEmptyString,
330});
331export type DesktopTelemetryRequestDesktopUpdate = typeof DesktopTelemetryRequestDesktopUpdate.Type;
332
333export const DesktopTelemetryCommitDesktopUpdate = Schema.Struct({
334 version: Schema.Literal(1),
335 type: Schema.Literal("commitDesktopUpdate"),
336 requestId: TrimmedNonEmptyString,
337});
338export type DesktopTelemetryCommitDesktopUpdate = typeof DesktopTelemetryCommitDesktopUpdate.Type;
339
340export const DesktopTelemetryCancelDesktopUpdate = Schema.Struct({
341 version: Schema.Literal(1),
342 type: Schema.Literal("cancelDesktopUpdate"),
343 requestId: TrimmedNonEmptyString,
344});
345export type DesktopTelemetryCancelDesktopUpdate = typeof DesktopTelemetryCancelDesktopUpdate.Type;
346
347export const DesktopTelemetryControlMessage = Schema.Union([
348 DesktopTelemetrySetDiagnosticsDemand,
349 DesktopTelemetrySetHostPowerIntervals,
350 DesktopTelemetryRequestDesktopUpdate,
351 DesktopTelemetryCommitDesktopUpdate,
352 DesktopTelemetryCancelDesktopUpdate,
353]);
354export type DesktopTelemetryControlMessage = typeof DesktopTelemetryControlMessage.Type;
355
356export const ResourceTelemetryProcess = Schema.Struct({
357 identity: ResourceTelemetryProcessIdentity,
358 ppid: NonNegativeInt,
359 childPids: Schema.Array(PositiveInt),
360 depth: NonNegativeInt,
361 name: Schema.String,
362 command: Schema.String,
363 status: Schema.String,
364 category: ResourceTelemetryProcessCategory,
365 electronType: Schema.optionalKey(DesktopElectronProcessType),
366 electronServiceName: Schema.optionalKey(Schema.String),
367 cpuPercent: Schema.Number,
368 cpuTimeMs: NonNegativeInt,
369 residentBytes: NonNegativeInt,
370 peakResidentBytes: NonNegativeInt,
371 virtualBytes: NonNegativeInt,
372 ioReadBytes: NonNegativeInt,
373 ioWriteBytes: NonNegativeInt,
374 ioReadBytesPerSecond: Schema.Number,
375 ioWriteBytesPerSecond: Schema.Number,
376 ioSemantics: ResourceTelemetryIoSemantics,
377 idleWakeupsPerSecond: Schema.optionalKey(Schema.Number),
378 runTimeMs: NonNegativeInt,
379 firstSeenAt: Schema.DateTimeUtc,
380 lastSeenAt: Schema.DateTimeUtc,
381});
382export type ResourceTelemetryProcess = typeof ResourceTelemetryProcess.Type;
383
384export const ResourceTelemetryAggregate = Schema.Struct({
385 processCount: NonNegativeInt,
386 currentCpuPercent: Schema.Number,
387 cpuTimeMs: NonNegativeInt,
388 currentRssBytes: NonNegativeInt,
389 peakRssBytes: NonNegativeInt,
390 ioReadBytes: NonNegativeInt,
391 ioWriteBytes: NonNegativeInt,
392 ioReadBytesPerSecond: Schema.Number,
393 ioWriteBytesPerSecond: Schema.Number,
394 processStarts: NonNegativeInt,
395 processExits: NonNegativeInt,
396});
397export type ResourceTelemetryAggregate = typeof ResourceTelemetryAggregate.Type;
398
399export const ResourceTelemetryGroups = Schema.Struct({
400 backend: ResourceTelemetryAggregate,
401 electron: ResourceTelemetryAggregate,
402 monitor: ResourceTelemetryAggregate,
403 allT3: ResourceTelemetryAggregate,
404});
405export type ResourceTelemetryGroups = typeof ResourceTelemetryGroups.Type;
406
407export const ResourceTelemetrySourceHealth = Schema.Struct({
408 status: ResourceTelemetrySourceStatus,
409 lastSampleAt: Schema.Option(Schema.DateTimeUtc),
410 lastError: Schema.Option(TrimmedNonEmptyString),
411});
412export type ResourceTelemetrySourceHealth = typeof ResourceTelemetrySourceHealth.Type;
413
414export const ResourceTelemetryHealth = Schema.Struct({
415 native: ResourceTelemetrySourceHealth,
416 desktop: ResourceTelemetrySourceHealth,
417 sidecarVersion: Schema.Option(TrimmedNonEmptyString),
418 sidecarPid: Schema.Option(PositiveInt),
419 restartCount: NonNegativeInt,
420 collectionDurationMicros: NonNegativeInt,
421 scannedProcessCount: NonNegativeInt,
422 retainedProcessCount: NonNegativeInt,
423 inaccessibleProcessCount: NonNegativeInt,
424});
425export type ResourceTelemetryHealth = typeof ResourceTelemetryHealth.Type;
426
427export const ResourceAttributionEntry = Schema.Struct({
428 component: TrimmedNonEmptyString,
429 operation: TrimmedNonEmptyString,
430 logicalReadBytes: NonNegativeInt,
431 logicalWriteBytes: NonNegativeInt,
432 count: NonNegativeInt,
433 durationMs: NonNegativeInt,
434});
435export type ResourceAttributionEntry = typeof ResourceAttributionEntry.Type;
436
437export const ResourceAttributionSnapshot = Schema.Struct({
438 readAt: Schema.DateTimeUtc,
439 entries: Schema.Array(ResourceAttributionEntry),
440});
441export type ResourceAttributionSnapshot = typeof ResourceAttributionSnapshot.Type;
442
443export const ResourceTelemetrySnapshot = Schema.Struct({
444 readAt: Schema.DateTimeUtc,
445 sampleIntervalMs: NonNegativeInt,
446 processes: Schema.Array(ResourceTelemetryProcess),
447 groups: ResourceTelemetryGroups,
448 power: HostPowerSnapshot,
449 speedLimitPercent: Schema.Option(Schema.Number),
450 attribution: ResourceAttributionSnapshot,
451 health: ResourceTelemetryHealth,
452});
453export type ResourceTelemetrySnapshot = typeof ResourceTelemetrySnapshot.Type;
454
455export const ResourceTelemetryHistoryInput = Schema.Struct({
456 windowMs: NonNegativeInt,
457 bucketMs: NonNegativeInt,
458});
459export type ResourceTelemetryHistoryInput = typeof ResourceTelemetryHistoryInput.Type;
460
461export const ResourceTelemetryHistoryBucket = Schema.Struct({
462 startedAt: Schema.DateTimeUtc,
463 endedAt: Schema.DateTimeUtc,
464 avgCpuPercent: Schema.Number,
465 maxCpuPercent: Schema.Number,
466 maxRssBytes: NonNegativeInt,
467 ioReadBytes: NonNegativeInt,
468 ioWriteBytes: NonNegativeInt,
469 maxProcessCount: NonNegativeInt,
470});
471export type ResourceTelemetryHistoryBucket = typeof ResourceTelemetryHistoryBucket.Type;
472
473export const ResourceTelemetryProcessSummary = Schema.Struct({
474 identity: ResourceTelemetryProcessIdentity,
475 ppid: NonNegativeInt,
476 depth: NonNegativeInt,
477 name: Schema.String,
478 command: Schema.String,
479 category: ResourceTelemetryProcessCategory,
480 firstSeenAt: Schema.DateTimeUtc,
481 lastSeenAt: Schema.DateTimeUtc,
482 currentCpuPercent: Schema.Number,
483 avgCpuPercent: Schema.Number,
484 maxCpuPercent: Schema.Number,
485 cpuTimeMs: NonNegativeInt,
486 currentRssBytes: NonNegativeInt,
487 peakRssBytes: NonNegativeInt,
488 ioReadBytes: NonNegativeInt,
489 ioWriteBytes: NonNegativeInt,
490 ioSemantics: ResourceTelemetryIoSemantics,
491 sampleCount: NonNegativeInt,
492});
493export type ResourceTelemetryProcessSummary = typeof ResourceTelemetryProcessSummary.Type;
494
495export const ResourceTelemetryHistory = Schema.Struct({
496 readAt: Schema.DateTimeUtc,
497 windowMs: NonNegativeInt,
498 bucketMs: NonNegativeInt,
499 sampleIntervalMs: NonNegativeInt,
500 retainedSampleCount: NonNegativeInt,
501 buckets: Schema.Array(ResourceTelemetryHistoryBucket),
502 topProcesses: Schema.Array(ResourceTelemetryProcessSummary),
503 health: ResourceTelemetryHealth,
504});
505export type ResourceTelemetryHistory = typeof ResourceTelemetryHistory.Type;
506
507export const ResourceTelemetryRetryResult = Schema.Struct({
508 accepted: Schema.Boolean,
509 snapshot: ResourceTelemetrySnapshot,
510});
511export type ResourceTelemetryRetryResult = typeof ResourceTelemetryRetryResult.Type;