export const CH = { snapshot: "crash:snapshot", crash: "crash:renderer", reload: "crash:reload", reportError: "crash:report-error", ready: "crash:ready", refresh: "crash:refresh", clearHistory: "crash:clear-history", } as const; export type EventRow = { at: number; kind: string; detail: string; }; export type Check = { id: string; label: string; pass: boolean; detail: string; }; export type CrashRow = { id: string; at: number; reason: string; exitCode: number; recovered: boolean; recoveryMs: number; dumpCount: number; }; export type CrashReporterState = { started: boolean; uploadToServer: boolean; directory: string; dumpCount: number; pendingCount: number; }; export type ErrorInfo = { message: string; source: string; line: number; at: number; }; export type Snapshot = { platform: string; electronVersion: string; nodeVersion: string; checks: Check[]; score: number; crashReporter: CrashReporterState; history: CrashRow[]; recovery: { autoRecover: boolean; lastRecoveryMs: number; recoveredCount: number; }; lastError: ErrorInfo | null; errorHookReady: boolean; events: EventRow[]; }; export function assertNoArgs(value: unknown): asserts value is undefined { if (value !== undefined) throw new Error("BAD_ARGS"); } export function assertErrorInfo(value: unknown): asserts value is ErrorInfo { if (typeof value !== "object" || value === null) { throw new Error("BAD_ERROR_REPORT"); } const report = value as Partial; if ( typeof report.message !== "string" || report.message.length === 0 || report.message.length > 2000 || typeof report.source !== "string" || typeof report.line !== "number" ) { throw new Error("BAD_ERROR_REPORT"); } } export type RecoveryAction = "reload" | "safe-mode" | "quit"; export function decideRecovery( reason: string, attempts: number, ): { action: RecoveryAction; delayMs: number; detail: string } { if (reason === "clean-exit" || reason === "killed") { return { action: "reload", delayMs: 300, detail: "进程被结束,直接重建渲染进程", }; } if (attempts >= 3) { return { action: "safe-mode", delayMs: 1200, detail: "短时间内多次崩溃,进入安全模式并停止自动恢复", }; } const delayMs = Math.min(300 * 2 ** attempts, 5000); return { action: "reload", delayMs, detail: `第 ${attempts + 1} 次恢复,退避 ${delayMs}ms`, }; }