mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
Clarify watchdog snapshot types and validated parser conversions
This commit is contained in:
@@ -59,7 +59,9 @@ export function readAvailableHangDetectionMarker(
|
||||
}
|
||||
raw = readFileSync(markerPath, 'utf8')
|
||||
} catch (error) {
|
||||
return (error as NodeJS.ErrnoException).code === 'ENOENT' ? null : undefined
|
||||
return typeof error === 'object' && error !== null && 'code' in error && error.code === 'ENOENT'
|
||||
? null
|
||||
: undefined
|
||||
}
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(raw)
|
||||
|
||||
@@ -10,8 +10,10 @@ import { consumeHangDetectionMarker, writeHangDetectionMarker } from './hang-det
|
||||
import { runWatchdog } from './main-thread-hang-watchdog-entry'
|
||||
import { preservePreviousHangDetection } from './previous-hang-detection'
|
||||
|
||||
const probes = vi.hoisted(() => ({ pending: [] as ((value: HangMachineSnapshot) => void)[] }))
|
||||
const files = vi.hoisted(() => ({ unavailablePath: null as string | null }))
|
||||
const probes = vi.hoisted<{ pending: ((value: HangMachineSnapshot) => void)[] }>(() => ({
|
||||
pending: []
|
||||
}))
|
||||
const files = vi.hoisted<{ unavailablePath: string | null }>(() => ({ unavailablePath: null }))
|
||||
vi.mock('node:fs', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof FileSystem>()
|
||||
return {
|
||||
|
||||
@@ -19,6 +19,7 @@ export type {
|
||||
} from './hang-observation-schema'
|
||||
|
||||
function object(value: unknown): Record<string, unknown> | null {
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the branch excludes null, primitives and arrays before reading unknown properties.
|
||||
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
||||
? (value as Record<string, unknown>)
|
||||
: null
|
||||
@@ -34,6 +35,7 @@ function numericFields<K extends string>(
|
||||
if (!data) {
|
||||
return null
|
||||
}
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: every supplied key is emitted once with a finite number or null.
|
||||
return Object.fromEntries(
|
||||
keys.map((key) => [key, finite(data[key]) ? data[key] : null])
|
||||
) as Record<K, number | null>
|
||||
@@ -49,6 +51,7 @@ export function parsePowerReceipt(value: unknown): HangPowerReceipt | null {
|
||||
return null
|
||||
}
|
||||
return {
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the fixed event allowlist above validates this string union.
|
||||
event: data.event as HangPowerReceipt['event'],
|
||||
observedAt: data.observedAt,
|
||||
receivedAt: data.receivedAt
|
||||
@@ -111,6 +114,7 @@ function parseObservation(value: unknown): HangObservation | null {
|
||||
const gap = object(data.watchdogGap)
|
||||
return {
|
||||
id: data.id,
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the fixed observation-kind allowlist above validates this string union.
|
||||
kind: data.kind as HangObservation['kind'],
|
||||
detectedAt: data.detectedAt,
|
||||
parentPid: data.parentPid,
|
||||
|
||||
@@ -5,8 +5,7 @@ export function captureHangSnapshot(): HangSnapshot {
|
||||
const cpu = process.cpuUsage()
|
||||
let watchdog: NodeJS.CpuUsage | null = null
|
||||
try {
|
||||
const read = (process as NodeJS.Process & { threadCpuUsage?: () => NodeJS.CpuUsage })
|
||||
.threadCpuUsage
|
||||
const read = process.threadCpuUsage
|
||||
watchdog = read?.call(process) ?? null
|
||||
} catch {
|
||||
// Some Electron runtimes do not expose per-thread CPU counters.
|
||||
|
||||
@@ -67,17 +67,18 @@ export function installMainThreadHangWatchdog(options: {
|
||||
// The worker already exited.
|
||||
}
|
||||
}
|
||||
const powerEvents: NodeJS.EventEmitter = powerMonitor
|
||||
const powerListeners = (['suspend', 'resume', 'lock-screen', 'unlock-screen'] as const).map(
|
||||
(event) => {
|
||||
const listener = (): void =>
|
||||
postMessage({ type: 'power-event', event, observedAt: Date.now() })
|
||||
powerMonitor.on(event, listener)
|
||||
powerEvents.on(event, listener)
|
||||
return { event, listener }
|
||||
}
|
||||
)
|
||||
const removePowerListeners = (): void => {
|
||||
for (const { event, listener } of powerListeners) {
|
||||
powerMonitor.off(event, listener)
|
||||
powerEvents.off(event, listener)
|
||||
}
|
||||
}
|
||||
const heartbeatTimer = setInterval(() => {
|
||||
|
||||
Reference in New Issue
Block a user