Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
Suppress the Windows crash prompt after a machine stop (PRD-8675) (#5925)
Andrew Mikofalvy71601f3d4fa0
diff --git a/.changeset/prd-8675-windows-machine-stop.md b/.changeset/prd-8675-windows-machine-stop.mdnew file mode 100644index 000000000..93f0d9b4d--- /dev/null+++ b/.changeset/prd-8675-windows-machine-stop.md@@ -0,0 +1,5 @@+---+"@inkeep/open-knowledge-desktop": patch+---++The desktop app no longer offers a crash report after a power loss, forced power-off or system crash stopped the computer, including on Windows.diff --git a/packages/desktop/src/main/boot-session.test.ts b/packages/desktop/src/main/boot-session.test.tsindex da1754399..79c6ab573 100644--- a/packages/desktop/src/main/boot-session.test.ts+++ b/packages/desktop/src/main/boot-session.test.ts@@ -1,5 +1,5 @@import { describe, expect, test } from 'vitest';-import { readBootSessionUuid } from './boot-session.ts';+import { readBootSessionUuid, readBootStartedAtMs } from './boot-session.ts';const onDarwin = process.platform === 'darwin' ? test : test.skip;const onLinux = process.platform === 'linux' ? test : test.skip;@@ -28,3 +28,26 @@ describe('readBootSessionUuid', () => {expect(crossPlatformProbe).toBeNull();});});++describe('readBootStartedAtMs', () => {+ test('dates the boot by subtracting uptime from now', () => {+ expect(readBootStartedAtMs(1_000_000_000, () => 120.4)).toBe(1_000_000_000 - 120_400);+ });++ test('an unusable uptime fails open to null rather than throwing', () => {+ expect(readBootStartedAtMs(1_000_000_000, () => 0)).toBeNull();+ expect(readBootStartedAtMs(1_000_000_000, () => Number.NaN)).toBeNull();+ expect(+ readBootStartedAtMs(1_000_000_000, () => {+ throw new Error('uptime unavailable');+ }),+ ).toBeNull();+ });++ test('the live boot instant precedes now', () => {+ const now = Date.now();+ const bootStartedAtMs = readBootStartedAtMs(now);+ expect(bootStartedAtMs).not.toBeNull();+ expect(bootStartedAtMs).toBeLessThan(now);+ });+});diff --git a/packages/desktop/src/main/boot-session.ts b/packages/desktop/src/main/boot-session.tsindex e1213d6ce..0e7acaea3 100644--- a/packages/desktop/src/main/boot-session.ts+++ b/packages/desktop/src/main/boot-session.ts@@ -1,5 +1,6 @@import { execFileSync } from 'node:child_process';import { readFileSync } from 'node:fs';+import { uptime } from 'node:os';export function readBootSessionUuid(platform: NodeJS.Platform = process.platform): string | null {try {@@ -20,6 +21,19 @@ export function readBootSessionUuid(platform: NodeJS.Platform = process.platform}}+export function readBootStartedAtMs(+ nowMs: number = Date.now(),+ readUptimeSeconds: () => number = uptime,+): number | null {+ try {+ const uptimeSeconds = readUptimeSeconds();+ if (!Number.isFinite(uptimeSeconds) || uptimeSeconds <= 0) return null;+ return nowMs - Math.round(uptimeSeconds * 1000);+ } catch {+ return null;+ }+}+function normalize(raw: string): string | null {const value = raw.trim();return value === '' ? null : value;diff --git a/packages/desktop/src/main/crash-detection.test.ts b/packages/desktop/src/main/crash-detection.test.tsindex 5dcd3dd49..44c8f8362 100644--- a/packages/desktop/src/main/crash-detection.test.ts+++ b/packages/desktop/src/main/crash-detection.test.ts@@ -65,6 +65,7 @@ interface Rig {ownSizedForeignDump: Buffer;setRendererAvailable(available: boolean): void;setBootSessionUuid(uuid: string | null): void;+ setBootStartedAtMs(ms: number | null): void;setInstallInFlight(inFlight: InstallInFlight | null): void;setAppVersion(version: string): void;setWatchdogRead(read: WatchdogRead): void;@@ -85,6 +86,7 @@ function makeRig(): Rig {const infos: Record<string, unknown>[] = [];let rendererAvailable = true;let bootSessionUuid: string | null = 'boot-epoch-a';+ let bootStartedAtMs: number | null = null;let installInFlight: InstallInFlight | null = null;let watchdogRead: WatchdogRead = { kind: 'absent' };let stallRead: StallRead = { kind: 'absent' };@@ -137,6 +139,9 @@ function makeRig(): Rig {setBootSessionUuid(uuid: string | null) {bootSessionUuid = uuid;},+ setBootStartedAtMs(ms: number | null) {+ bootStartedAtMs = ms;+ },setInstallInFlight(inFlight: InstallInFlight | null) {installInFlight = inFlight;},@@ -179,6 +184,7 @@ function makeRig(): Rig {return new Date(clockMs);},currentBootSessionUuid: () => bootSessionUuid,+ currentBootStartedAtMs: () => bootStartedAtMs,installInFlight: () => installInFlight,mainThreadWatchdog: {readPrevious: () => watchdogRead,@@ -4080,3 +4086,123 @@ describe('the dump a report invitation is bound to', () => {});});});++describe('machine stops the kernel boot session cannot name', () => {+ function machineDeathLine(rig: Rig): Record<string, unknown> | undefined {+ return rig.infos.find((line) => line.event === 'crash-detection.machine-level-death');+ }++ function windowsRig(): Rig {+ const rig = makeRig();+ rig.deps.platform = 'win32';+ rig.setBootSessionUuid(null);+ return rig;+ }++ test('a machine that booted after the session was last alive never prompts', () => {+ const rig = windowsRig();+ createCrashDetection(rig.deps).detectBootCrash();+ const lastAliveAt = readSentinel(rig).lastAliveAt;++ rig.advance(2 * 60 * 60_000);+ const bootStartedAtMs = rig.nowMs() - 60_000;+ rig.setBootStartedAtMs(bootStartedAtMs);+ const sessionB = createCrashDetection(rig.deps);+ expect(sessionB.detectBootCrash()).toBeNull();+ sessionB.notifyRendererReady();+ expect(rig.emitted).toHaveLength(0);++ const breadcrumb = machineDeathLine(rig);+ expect(breadcrumb?.reason).toBe('system-reboot');+ expect(breadcrumb?.machineCause).toBe('system-reboot');+ expect(breadcrumb?.lastAliveAt).toBe(lastAliveAt);+ expect(breadcrumb?.currentBootStartedAt).toBe(new Date(bootStartedAtMs).toISOString());+ });++ test('a session that died while the machine kept running still prompts', () => {+ const rig = windowsRig();+ rig.setBootStartedAtMs(rig.nowMs() - 60 * 60_000);+ createCrashDetection(rig.deps).detectBootCrash();++ rig.advance(2 * 60 * 60_000);+ const armed = createCrashDetection(rig.deps).detectBootCrash();++ expect(bootInvite(armed).context.dirtyShutdown).toBe(true);+ expect(machineDeathLine(rig)).toBeUndefined();+ });++ test('without a boot instant the dirty shutdown still prompts', () => {+ const rig = windowsRig();+ createCrashDetection(rig.deps).detectBootCrash();++ rig.advance(2 * 60 * 60_000);+ expect(bootInvite(createCrashDetection(rig.deps).detectBootCrash()).context.dirtyShutdown).toBe(+ true,+ );+ });++ test('a matching kernel boot session outranks a boot instant that disagrees', () => {+ const rig = makeRig();+ createCrashDetection(rig.deps).detectBootCrash();++ rig.advance(2 * 60 * 60_000);+ rig.setBootStartedAtMs(rig.nowMs() - 60_000);+ expect(bootInvite(createCrashDetection(rig.deps).detectBootCrash()).context.dirtyShutdown).toBe(+ true,+ );+ });++ test('a zero-filled sentinel the OS never flushed never prompts', () => {+ const rig = windowsRig();+ mkdirSync(dirname(rig.deps.sentinelPath), { recursive: true });+ writeFileSync(rig.deps.sentinelPath, Buffer.alloc(160));++ const sessionB = createCrashDetection(rig.deps);+ expect(sessionB.detectBootCrash()).toBeNull();+ sessionB.notifyRendererReady();+ expect(rig.emitted).toHaveLength(0);++ expect(rig.warnings.map((w) => w.event)).toContain('crash-detection.sentinel-parse-failed');+ const breadcrumb = machineDeathLine(rig);+ expect(breadcrumb?.reason).toBe('unflushed-sentinel');+ expect(breadcrumb?.class).toBe('external');+ expect(readSentinel(rig).bootId).toBe(rig.watchdogStarts.at(-1));+ });++ test('a sentinel torn by NUL bytes past its valid prefix never prompts', () => {+ const rig = makeRig();+ createCrashDetection(rig.deps).detectBootCrash();+ const valid = readFileSync(rig.deps.sentinelPath);+ writeFileSync(rig.deps.sentinelPath, Buffer.concat([valid.subarray(0, 40), Buffer.alloc(64)]));++ expect(createCrashDetection(rig.deps).detectBootCrash()).toBeNull();+ expect(machineDeathLine(rig)?.reason).toBe('unflushed-sentinel');+ });++ test('an unparseable sentinel without NUL bytes still prompts, and says what it compared', () => {+ const rig = windowsRig();+ const bootStartedAtMs = rig.nowMs() - 60_000;+ rig.setBootStartedAtMs(bootStartedAtMs);+ mkdirSync(dirname(rig.deps.sentinelPath), { recursive: true });+ writeFileSync(rig.deps.sentinelPath, '{"bootId":"17');++ const armed = bootInvite(createCrashDetection(rig.deps).detectBootCrash());+ expect(armed.eventId.startsWith('boot:unreadable:')).toBe(true);+ const boot = rig.infos.find((line) => line.event === 'crash-detection.boot');+ expect(boot?.bootSessionsComparable).toBe(false);+ expect(boot?.currentBootStartedAt).toBe(new Date(bootStartedAtMs).toISOString());+ expect(boot?.sentinelUnflushed).toBe(false);+ expect(boot?.lastAliveAt).toBeNull();+ });++ test('a fresh crash dump beside a zero-filled sentinel still prompts from the dump', () => {+ const rig = makeRig();+ createCrashDetection(rig.deps).detectBootCrash();+ writeFileSync(rig.deps.sentinelPath, Buffer.alloc(160));+ seedMinidump(rig, 'pending/native-crash.dmp', rig.tick());++ const armed = bootInvite(createCrashDetection(rig.deps).detectBootCrash());+ expect(armed.eventId.startsWith('boot:dump:')).toBe(true);+ expect(armed.context.dirtyShutdown).toBe(false);+ });+});diff --git a/packages/desktop/src/main/crash-detection.ts b/packages/desktop/src/main/crash-detection.tsindex c326310d0..ae5d363b7 100644--- a/packages/desktop/src/main/crash-detection.ts+++ b/packages/desktop/src/main/crash-detection.ts@@ -168,6 +168,7 @@ export interface CrashDetectionDeps {emit(event: OkBugReportCrashDetectedEvent): boolean;now(): Date;currentBootSessionUuid(): string | null;+ currentBootStartedAtMs?(): number | null;installInFlight?(span: { deathFromMs: number; deathToMs: number }): InstallInFlight | null;mainThreadWatchdog: Pick<MainThreadWatchdog, 'readPrevious' | 'readPreviousStall' | 'start'>;logger: CrashLogger;@@ -731,7 +732,7 @@ export function createCrashDetection(deps: CrashDetectionDeps): CrashDetection {) {deps.logger.warn({ event: 'crash-detection.boot-session-unavailable', platform: process.platform },- 'kernel boot-session identity unavailable — reboot suppression is disabled this launch',+ 'kernel boot-session identity unavailable — reboot detection falls back to the OS boot instant this launch',);}@@ -904,13 +905,24 @@ export function createCrashDetection(deps: CrashDetectionDeps): CrashDetection {);const boundDump = (attachableDumps.find(arming) ?? attachableDumps[0])?.entry ?? null;- const rebootedBetweenSessions =- prevBootSessionUuid !== null &&- bootSessionUuid !== null &&- prevBootSessionUuid !== bootSessionUuid;- const machineLevelDeath =- sentinelPresent &&- (rebootedBetweenSessions || prevPendingOsShutdownAt !== null || prevSuspendedAt !== null);+ const bootSessionsComparable = prevBootSessionUuid !== null && bootSessionUuid !== null;+ const bootStartedAtMs = bootSessionsComparable+ ? null+ : (deps.currentBootStartedAtMs?.() ?? null);+ const rebootedBetweenSessions = bootSessionsComparable+ ? prevBootSessionUuid !== bootSessionUuid+ : bootStartedAtMs !== null && lastAliveMs !== null && bootStartedAtMs > lastAliveMs;+ const sentinelUnflushed = sentinelRaw?.includes('\u0000') ?? false;+ const machineCause = rebootedBetweenSessions+ ? 'system-reboot'+ : prevPendingOsShutdownAt !== null+ ? 'os-shutdown'+ : prevSuspendedAt !== null+ ? 'suspended'+ : sentinelUnflushed+ ? 'unflushed-sentinel'+ : null;+ const machineLevelDeath = sentinelPresent && machineCause !== null;const updateInstallDeath = sentinelPresent && installInFlight !== null;const suppressibleDeath = machineLevelDeath || updateInstallDeath;@@ -934,15 +946,7 @@ export function createCrashDetection(deps: CrashDetectionDeps): CrashDetection {let armed: OkBugReportCrashDetectedEvent | null = null;if (machineSuppressed || crashTooOld) {- const reason = !machineSuppressed- ? 'stale-crash'- : rebootedBetweenSessions- ? 'system-reboot'- : prevPendingOsShutdownAt !== null- ? 'os-shutdown'- : prevSuspendedAt !== null- ? 'suspended'- : 'update-install';+ const reason = machineSuppressed ? (machineCause ?? 'update-install') : 'stale-crash';const breadcrumb = {event: 'crash-detection.machine-level-death',reason,@@ -957,6 +961,8 @@ export function createCrashDetection(deps: CrashDetectionDeps): CrashDetection {prevBootId,prevBootSessionUuid,currentBootSessionUuid: bootSessionUuid,+ currentBootStartedAt:+ bootStartedAtMs === null ? null : new Date(bootStartedAtMs).toISOString(),lastAliveAt: prevLastAliveAt,sentinelAgeMs,deathAgeMs,@@ -964,13 +970,7 @@ export function createCrashDetection(deps: CrashDetectionDeps): CrashDetection {deathAtSource,staleAfterMs: STALE_CRASH_AFTER_MS,class: machineSuppressed ? 'external' : 'stale',- machineCause: rebootedBetweenSessions- ? 'system-reboot'- : prevPendingOsShutdownAt !== null- ? 'os-shutdown'- : prevSuspendedAt !== null- ? 'suspended'- : null,+ machineCause,suspendedAt: prevSuspendedAt,pendingOsShutdownAt: prevPendingOsShutdownAt,osShutdownReasons: prevOsShutdownReasons,@@ -988,9 +988,11 @@ export function createCrashDetection(deps: CrashDetectionDeps): CrashDetection {? 'previous session was killed by a system reboot — suppressing the report prompt': reason === 'suspended'? 'previous session died asleep without resuming — suppressing the report prompt'- : reason === 'stale-crash'- ? 'previous session died too long ago to report usefully — suppressing the report prompt'- : 'previous session was killed by an update install — suppressing the report prompt',+ : reason === 'unflushed-sentinel'+ ? 'previous session left a sentinel the OS never flushed, so the machine stopped under it — suppressing the report prompt'+ : reason === 'stale-crash'+ ? 'previous session died too long ago to report usefully — suppressing the report prompt'+ : 'previous session was killed by an update install — suppressing the report prompt',);}} else if (somethingToReport) {@@ -1033,6 +1035,10 @@ export function createCrashDetection(deps: CrashDetectionDeps): CrashDetection {lastAliveAt: prevLastAliveAt,suspendedAt: prevSuspendedAt,pendingOsShutdownAt: prevPendingOsShutdownAt,+ bootSessionsComparable,+ currentBootStartedAt:+ bootStartedAtMs === null ? null : new Date(bootStartedAtMs).toISOString(),+ sentinelUnflushed,attemptedInstall: installInFlight?.attemptedVersion ?? null,crashedAppVersion,crashedAppVersionParseFailed: dumpVersion?.parseFailed ?? false,diff --git a/packages/desktop/src/main/index.ts b/packages/desktop/src/main/index.tsindex 206a03f1b..6d8cf481a 100644--- a/packages/desktop/src/main/index.ts+++ b/packages/desktop/src/main/index.ts@@ -195,7 +195,7 @@ import {resolveDesktopLocaleForPushed,} from './boot-locale.ts';import { resolveBootRestoreDecision, resolveRestoreActions } from './boot-restore-decision.ts';-import { readBootSessionUuid } from './boot-session.ts';+import { readBootSessionUuid, readBootStartedAtMs } from './boot-session.ts';import { runBootstrap } from './bootstrap.ts';import {type BranchInfoProxyDeps,@@ -6157,6 +6157,7 @@ function bootPrimaryInstance(): void {},now: () => new Date(),currentBootSessionUuid: readBootSessionUuid,+ currentBootStartedAtMs: () => readBootStartedAtMs(),installInFlight: (span) =>installWasInFlightDuring(bootStateSnapshot,