Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

resourceTelemetry.ts

488 lines17.5 KB

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