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

resourceTelemetry.ts

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