Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
Attribute macOS memory-pressure kills at boot (PRD-9148) (#5970)
Andrew Mikofalvyd4c6541036ea
diff --git a/.changeset/prd-9148-macos-memory-pressure-kill.md b/.changeset/prd-9148-macos-memory-pressure-kill.mdnew file mode 100644index 000000000..3bafccc2d--- /dev/null+++ b/.changeset/prd-9148-macos-memory-pressure-kill.md@@ -0,0 +1,5 @@+---+"@inkeep/open-knowledge-desktop": patch+---++On macOS the desktop app no longer offers a crash report after the system ended it to free memory that another app was using.diff --git a/packages/cli/src/diagnose/os-termination-evidence.test.ts b/packages/cli/src/diagnose/os-termination-evidence.test.tsindex a6d014835..ba623af86 100644--- a/packages/cli/src/diagnose/os-termination-evidence.test.ts+++ b/packages/cli/src/diagnose/os-termination-evidence.test.ts@@ -1,4 +1,4 @@-import { mkdtempSync, rmSync, utimesSync, writeFileSync } from 'node:fs';+import { chmodSync, mkdtempSync, rmSync, symlinkSync, utimesSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join, resolve } from 'node:path';import { afterEach, describe, expect, test } from 'vitest';@@ -7,6 +7,7 @@ import {collectOsTerminationEvidence,createRunCommand,describeOsTerminationEvidence,+ findJetsamKillSync,LINUX_JOURNAL_GREP,parseJetsamReport,renderOsTerminationEvidence,@@ -157,6 +158,209 @@ describe('macOS jetsam collection', () => {});});+describe('the jetsam kill of one process', () => {+ const LAST_ALIVE_MS = Date.parse('2026-10-05T00:14:00.000Z');+ const DETECTED_MS = Date.parse('2026-10-05T00:15:30.000Z');+ const QUERY = { pid: 4406, fromMs: LAST_ALIVE_MS, toMs: DETECTED_MS };++ function writeAt(dir: string, name: string, content: string, atMs: number): void {+ const path = join(dir, name);+ writeFileSync(path, content);+ utimesSync(path, new Date(atMs), new Date(atMs));+ }++ const MAIN_KILLED = { name: 'OpenKnowledge', pid: 4406, rpages: 900, reason: 'vm-pageshortage' };+ const NOTHING_MISSED = { unreadable: 0, unparseable: 0, skippedNewerOverCap: 0 };++ test('names the reason jetsam killed the queried pid and who held the most memory', () => {+ const dir = makeTmpDir();+ writeAt(dir, 'JetsamEvent-a.ips', jetsamReport([MAIN_KILLED], 'Xcode'), LAST_ALIVE_MS + 40_000);++ expect(findJetsamKillSync(QUERY, dir)).toEqual({+ outcome: 'killed',+ source: 'macos-jetsam',+ at: '2026-10-05T00:14:39.000Z',+ killReason: 'vm-pageshortage',+ victimChosenByPriority: true,+ largestProcessOwned: false,+ residentBytes: 900 * 16384,+ reportsRead: 1,+ });+ });++ test.each([+ [+ 'a compressor-space kill, which macOS aims at the largest compressed-memory holder',+ 'vm-compressor-space-shortage',+ ],+ ['a kill for the process’s own memory limit', 'per-process-limit'],+ ])('does not call %s a priority-order kill', (_case, reason) => {+ const dir = makeTmpDir();+ writeAt(+ dir,+ 'JetsamEvent-a.ips',+ jetsamReport([{ ...MAIN_KILLED, reason }], 'Xcode'),+ LAST_ALIVE_MS + 40_000,+ );++ expect(findJetsamKillSync(QUERY, dir)).toMatchObject({+ outcome: 'killed',+ killReason: reason,+ victimChosenByPriority: false,+ });+ });++ test('leaves the largest process unknown when the report names none', () => {+ const dir = makeTmpDir();+ const header = { bug_type: '298', timestamp: '2026-10-04 17:14:39.00 -0700' };+ const body = { memoryStatus: { pageSize: 16384 }, processes: [MAIN_KILLED] };+ writeAt(+ dir,+ 'JetsamEvent-a.ips',+ `${JSON.stringify(header)}\n${JSON.stringify(body)}`,+ LAST_ALIVE_MS + 40_000,+ );++ expect(findJetsamKillSync(QUERY, dir)).toMatchObject({+ outcome: 'killed',+ largestProcessOwned: null,+ });+ });++ test('does not attribute a report that names the pid alive, another pid, or predates the session’s last heartbeat', () => {+ const dir = makeTmpDir();+ writeAt(+ dir,+ 'JetsamEvent-alive.ips',+ jetsamReport([{ name: 'OpenKnowledge', pid: 4406, rpages: 900 }]),+ LAST_ALIVE_MS + 40_000,+ );+ writeAt(+ dir,+ 'JetsamEvent-other.ips',+ jetsamReport([{ ...MAIN_KILLED, pid: 4410 }]),+ LAST_ALIVE_MS + 41_000,+ );+ writeAt(dir, 'JetsamEvent-stale.ips', jetsamReport([MAIN_KILLED]), LAST_ALIVE_MS - 60_000);++ expect(findJetsamKillSync(QUERY, dir)).toEqual({+ outcome: 'not-killed',+ reportsRead: 2,+ ...NOTHING_MISSED,+ });+ });++ test('dates a report by the kill it records, not by when the file was written', () => {+ const dir = makeTmpDir();+ writeAt(dir, 'JetsamEvent-a.ips', jetsamReport([MAIN_KILLED]), LAST_ALIVE_MS + 60_000);++ const afterTheKill = { ...QUERY, fromMs: Date.parse('2026-10-05T00:14:45.000Z') };+ expect(findJetsamKillSync(afterTheKill, dir)).toEqual({+ outcome: 'not-killed',+ reportsRead: 1,+ ...NOTHING_MISSED,+ });+ });++ test('is inconclusive, not a miss, when the kill sits past the boot cap', () => {+ const dir = makeTmpDir();+ for (let i = 0; i < 5; i += 1) {+ writeAt(+ dir,+ `JetsamEvent-${i}.ips`,+ jetsamReport([{ name: 'Slack', pid: 77 }]),+ LAST_ALIVE_MS + i * 1_000,+ );+ }+ writeAt(dir, 'JetsamEvent-5.ips', jetsamReport([MAIN_KILLED]), LAST_ALIVE_MS + 40_000);++ expect(findJetsamKillSync(QUERY, dir)).toEqual({+ outcome: 'inconclusive',+ reportsRead: 5,+ ...NOTHING_MISSED,+ skippedNewerOverCap: 1,+ });+ });++ test('is inconclusive when a report in the window would not parse', () => {+ const dir = makeTmpDir();+ writeAt(dir, 'JetsamEvent-garbage.ips', 'garbage', LAST_ALIVE_MS + 40_000);++ expect(findJetsamKillSync(QUERY, dir)).toEqual({+ outcome: 'inconclusive',+ reportsRead: 1,+ ...NOTHING_MISSED,+ unparseable: 1,+ });+ });++ test.skipIf(process.platform === 'win32' || process.getuid?.() === 0)(+ 'is inconclusive beside an unreadable report, and unavailable when none could be read',+ () => {+ const dir = makeTmpDir();+ writeAt(dir, 'JetsamEvent-locked.ips', jetsamReport([MAIN_KILLED]), LAST_ALIVE_MS + 40_000);+ chmodSync(join(dir, 'JetsamEvent-locked.ips'), 0o000);+ expect(findJetsamKillSync(QUERY, dir)).toEqual({+ outcome: 'unavailable',+ unavailableReason: 'EACCES',+ });++ writeAt(+ dir,+ 'JetsamEvent-open.ips',+ jetsamReport([{ name: 'Slack', pid: 77 }]),+ LAST_ALIVE_MS + 41_000,+ );+ expect(findJetsamKillSync(QUERY, dir)).toEqual({+ outcome: 'inconclusive',+ reportsRead: 1,+ ...NOTHING_MISSED,+ unreadable: 1,+ });+ },+ );++ test.skipIf(process.platform === 'win32')(+ 'counts a report it could not stat as unreadable, and is unavailable only when nothing could be statted',+ () => {+ const dir = makeTmpDir();+ symlinkSync(join(dir, 'gone.ips'), join(dir, 'JetsamEvent-dangling.ips'));+ expect(findJetsamKillSync(QUERY, dir)).toEqual({+ outcome: 'unavailable',+ unavailableReason: 'ENOENT',+ });++ writeAt(dir, 'JetsamEvent-old.ips', jetsamReport([MAIN_KILLED]), LAST_ALIVE_MS - 60_000);+ expect(findJetsamKillSync(QUERY, dir)).toEqual({+ outcome: 'inconclusive',+ reportsRead: 0,+ ...NOTHING_MISSED,+ unreadable: 1,+ });++ writeAt(+ dir,+ 'JetsamEvent-open.ips',+ jetsamReport([{ name: 'Slack', pid: 77 }]),+ LAST_ALIVE_MS + 41_000,+ );+ expect(findJetsamKillSync(QUERY, dir)).toEqual({+ outcome: 'inconclusive',+ reportsRead: 1,+ ...NOTHING_MISSED,+ unreadable: 1,+ });+ },+ );++ test('reports an unreadable directory as unavailable rather than as no kill', () => {+ expect(findJetsamKillSync(QUERY, join(makeTmpDir(), 'missing'))).toEqual({+ outcome: 'unavailable',+ unavailableReason: 'ENOENT',+ });+ });+});+function runner(results: Record<string, CommandResult>) {const calls: { command: string; args: string[] }[] = [];let inFlight = 0;diff --git a/packages/cli/src/diagnose/os-termination-evidence.ts b/packages/cli/src/diagnose/os-termination-evidence.tsindex a771817f6..cea1fc541 100644--- a/packages/cli/src/diagnose/os-termination-evidence.ts+++ b/packages/cli/src/diagnose/os-termination-evidence.ts@@ -1,4 +1,5 @@import { type ExecFileException, execFile } from 'node:child_process';+import { readdirSync, readFileSync, statSync } from 'node:fs';import { readdir, readFile, stat } from 'node:fs/promises';import { join, win32 } from 'node:path';import { DESKTOP_PRODUCTS, desktopWindowsExecutableName } from '@inkeep/open-knowledge-core';@@ -37,7 +38,7 @@ interface JetsamEvidenceEvent {at: string | null;file: string;pageSize: number | null;- largestProcessOwned: boolean;+ largestProcessOwned: boolean | null;ownedProcesses: JetsamProcess[];}@@ -222,11 +223,43 @@ export function parseJetsamReport(file: string, content: string): JetsamEvidencefile,pageSize: finiteOrNull(asRecord(body.memoryStatus)?.pageSize),largestProcessOwned:- typeof body.largestProcess === 'string' && isOwnedProcessName(body.largestProcess),+ typeof body.largestProcess === 'string' ? isOwnedProcessName(body.largestProcess) : null,ownedProcesses,};}+const JETSAM_REPORT_NAME = /^JetsamEvent-.*\.ips$/;++interface JetsamReportStat {+ file: string;+ mtimeMs: number | null;+}++interface JetsamSelection {+ files: string[];+ statFailed: number;+ droppedOverCap: number;+}++function selectJetsamReports(+ stats: JetsamReportStat[],+ sinceMs: number,+ cap: number,+ order: 'newest-first' | 'oldest-first',+): JetsamSelection {+ const inWindow = stats.flatMap(({ file, mtimeMs }) =>+ mtimeMs !== null && mtimeMs >= sinceMs ? [{ file, mtimeMs }] : [],+ );+ inWindow.sort((a, b) =>+ order === 'newest-first' ? b.mtimeMs - a.mtimeMs : a.mtimeMs - b.mtimeMs,+ );+ return {+ files: inWindow.slice(0, cap).map(({ file }) => file),+ statFailed: stats.filter(({ mtimeMs }) => mtimeMs === null).length,+ droppedOverCap: Math.max(0, inWindow.length - cap),+ };+}+async function collectMacosJetsam(dir: string, now: Date): Promise<OsTerminationEvidence> {const evidence = base('darwin', 'macos-jetsam', [join(dir, 'JetsamEvent-*.ips')]);let names: string[];@@ -235,20 +268,26 @@ async function collectMacosJetsam(dir: string, now: Date): Promise<OsTermination} catch (err) {return unavailable(evidence, (err as NodeJS.ErrnoException).code ?? 'unknown');}- const cutoff = now.getTime() - WINDOW_MS;- const candidates: { file: string; mtimeMs: number }[] = [];- for (const file of names) {- if (!/^JetsamEvent-.*\.ips$/.test(file)) continue;- try {- const { mtimeMs } = await stat(join(dir, file));- if (mtimeMs >= cutoff) candidates.push({ file, mtimeMs });- } catch {- evidence.unparseable += 1;- }- }- candidates.sort((a, b) => b.mtimeMs - a.mtimeMs);- evidence.droppedOverCap = Math.max(0, candidates.length - MAX_JETSAM_FILES);- for (const { file } of candidates.slice(0, MAX_JETSAM_FILES)) {+ const stats = await Promise.all(+ names+ .filter((file) => JETSAM_REPORT_NAME.test(file))+ .map(async (file): Promise<JetsamReportStat> => {+ try {+ return { file, mtimeMs: (await stat(join(dir, file))).mtimeMs };+ } catch {+ return { file, mtimeMs: null };+ }+ }),+ );+ const selection = selectJetsamReports(+ stats,+ now.getTime() - WINDOW_MS,+ MAX_JETSAM_FILES,+ 'newest-first',+ );+ evidence.unparseable += selection.statFailed;+ evidence.droppedOverCap = selection.droppedOverCap;+ for (const file of selection.files) {let parsed: JetsamEvidenceEvent | null = null;try {parsed = parseJetsamReport(file, await readFile(join(dir, file), 'utf8'));@@ -264,6 +303,138 @@ async function collectMacosJetsam(dir: string, now: Date): Promise<OsTerminationreturn settle(evidence);}+const MAX_JETSAM_FILES_AT_BOOT = 5;++const JETSAM_TIMESTAMP_SLACK_MS = 1_000;++const PRIORITY_ORDER_KILL_REASONS = new Set([+ 'vm-pageshortage',+ 'vm-compressor-thrashing',+ 'vm-pageout-starvation',+ 'low-swap',+ 'sustained-memory-pressure',+]);++export interface ProcessTerminationQuery {+ pid: number;+ fromMs: number;+ toMs: number;+}++export type ProcessTerminationLookup =+ | {+ outcome: 'killed';+ source: Extract<OsTerminationSource, 'macos-jetsam'>;+ at: string | null;+ killReason: string;+ victimChosenByPriority: boolean;+ largestProcessOwned: boolean | null;+ residentBytes: number | null;+ reportsRead: number;+ }+ | {+ outcome: 'not-killed';+ reportsRead: number;+ unreadable: 0;+ unparseable: 0;+ skippedNewerOverCap: 0;+ }+ | {+ outcome: 'inconclusive';+ reportsRead: number;+ unreadable: number;+ unparseable: number;+ skippedNewerOverCap: number;+ }+ | { outcome: 'unavailable'; unavailableReason: string };++export function findJetsamKillSync(+ query: ProcessTerminationQuery,+ dir: string = MACOS_SYSTEM_DIAGNOSTIC_REPORTS_DIR,+): ProcessTerminationLookup {+ let names: string[];+ try {+ names = readdirSync(dir);+ } catch (err) {+ return {+ outcome: 'unavailable',+ unavailableReason: (err as NodeJS.ErrnoException).code ?? 'unknown',+ };+ }+ const earliestMs = query.fromMs - JETSAM_TIMESTAMP_SLACK_MS;+ let statError: string | null = null;+ const stats = names+ .filter((file) => JETSAM_REPORT_NAME.test(file))+ .map((file): JetsamReportStat => {+ try {+ return { file, mtimeMs: statSync(join(dir, file)).mtimeMs };+ } catch (err) {+ statError ??= (err as NodeJS.ErrnoException).code ?? 'unknown';+ return { file, mtimeMs: null };+ }+ });+ const selection = selectJetsamReports(+ stats,+ earliestMs,+ MAX_JETSAM_FILES_AT_BOOT,+ 'oldest-first',+ );+ let reportsRead = 0;+ let unreadable = selection.statFailed;+ let unparseable = 0;+ let readError: string | null = null;+ for (const file of selection.files) {+ let content: string;+ try {+ content = readFileSync(join(dir, file), 'utf8');+ } catch (err) {+ unreadable += 1;+ readError ??= (err as NodeJS.ErrnoException).code ?? 'unknown';+ continue;+ }+ reportsRead += 1;+ const parsed = parseJetsamReport(file, content);+ if (parsed === null) {+ unparseable += 1;+ continue;+ }+ const atMs = parsed.at === null ? null : Date.parse(parsed.at);+ if (atMs !== null && (atMs < earliestMs || atMs > query.toMs)) continue;+ const killed = parsed.ownedProcesses.find((p) => p.pid === query.pid && p.killReason !== null);+ if (killed === undefined || killed.killReason === null) continue;+ return {+ outcome: 'killed',+ source: 'macos-jetsam',+ at: parsed.at,+ killReason: killed.killReason,+ victimChosenByPriority: PRIORITY_ORDER_KILL_REASONS.has(killed.killReason),+ largestProcessOwned: parsed.largestProcessOwned,+ residentBytes:+ killed.residentPages === null || parsed.pageSize === null+ ? null+ : killed.residentPages * parsed.pageSize,+ reportsRead,+ };+ }+ if (statError !== null && selection.statFailed === stats.length) {+ return { outcome: 'unavailable', unavailableReason: statError };+ }+ if (reportsRead === 0 && readError !== null) {+ return { outcome: 'unavailable', unavailableReason: readError };+ }+ const skippedNewerOverCap = selection.droppedOverCap;+ if (unreadable === 0 && unparseable === 0 && skippedNewerOverCap === 0) {+ return {+ outcome: 'not-killed',+ reportsRead,+ unreadable: 0,+ unparseable: 0,+ skippedNewerOverCap: 0,+ };+ }+ return { outcome: 'inconclusive', reportsRead, unreadable, unparseable, skippedNewerOverCap };+}+export const LINUX_JOURNAL_GREP = 'Out of memory|oom-kill|Killed process|Killed /|memory pressure';const LINUX_JOURNAL_ARGS: readonly string[] = [diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.tsindex 076fe8015..ce07d6294 100644--- a/packages/cli/src/index.ts+++ b/packages/cli/src/index.ts@@ -75,6 +75,11 @@ export {export { runStop } from './commands/stop.ts';export { type LoadConfigResult, loadConfig } from './config/loader.ts';export { type PreviewResult, previewContent } from './content/preview.ts';+export {+ findJetsamKillSync,+ type ProcessTerminationLookup,+ type ProcessTerminationQuery,+} from './diagnose/os-termination-evidence.ts';export {type ExpectedShareRepo,type ShareFolderValidationResult,diff --git a/packages/desktop/src/main/crash-detection.test.ts b/packages/desktop/src/main/crash-detection.test.tsindex 44c8f8362..b5cf72f2b 100644--- a/packages/desktop/src/main/crash-detection.test.ts+++ b/packages/desktop/src/main/crash-detection.test.ts@@ -10,6 +10,7 @@ import {} from 'node:fs';import { tmpdir } from 'node:os';import { dirname, join, resolve } from 'node:path';+import type { ProcessTerminationLookup, ProcessTerminationQuery } from '@inkeep/open-knowledge';import type { OkBugReportCrashDetectedEvent } from '@inkeep/open-knowledge-core';import { afterEach, describe, expect, test, vi } from 'vitest';import { installWasInFlightDuring } from './auto-updater.ts';@@ -67,6 +68,8 @@ interface Rig {setBootSessionUuid(uuid: string | null): void;setBootStartedAtMs(ms: number | null): void;setInstallInFlight(inFlight: InstallInFlight | null): void;+ setProcessTermination(lookup: ProcessTerminationLookup): void;+ processTerminationQueries: ProcessTerminationQuery[];setAppVersion(version: string): void;setWatchdogRead(read: WatchdogRead): void;setStallRead(read: StallRead): void;@@ -88,6 +91,14 @@ function makeRig(): Rig {let bootSessionUuid: string | null = 'boot-epoch-a';let bootStartedAtMs: number | null = null;let installInFlight: InstallInFlight | null = null;+ let processTermination: ProcessTerminationLookup = {+ outcome: 'not-killed',+ reportsRead: 0,+ unreadable: 0,+ unparseable: 0,+ skippedNewerOverCap: 0,+ };+ const processTerminationQueries: ProcessTerminationQuery[] = [];let watchdogRead: WatchdogRead = { kind: 'absent' };let stallRead: StallRead = { kind: 'absent' };const watchdogStarts: string[] = [];@@ -145,6 +156,10 @@ function makeRig(): Rig {setInstallInFlight(inFlight: InstallInFlight | null) {installInFlight = inFlight;},+ setProcessTermination(lookup: ProcessTerminationLookup) {+ processTermination = lookup;+ },+ processTerminationQueries,setAppVersion(version: string) {rig.deps.appVersion = version;},@@ -173,6 +188,7 @@ function makeRig(): Rig {crashDumpsDir: join(dir, 'crash-dumps'),appBundleRoot,appVersion: CRASHED_VERSION,+ processId: 20480,platform: 'darwin',emit(event) {if (!rendererAvailable) return false;@@ -186,6 +202,10 @@ function makeRig(): Rig {currentBootSessionUuid: () => bootSessionUuid,currentBootStartedAtMs: () => bootStartedAtMs,installInFlight: () => installInFlight,+ processTermination: (query) => {+ processTerminationQueries.push(query);+ return processTermination;+ },mainThreadWatchdog: {readPrevious: () => watchdogRead,readPreviousStall: () => stallRead,@@ -4206,3 +4226,121 @@ describe('machine stops the kernel boot session cannot name', () => {expect(armed.context.dirtyShutdown).toBe(false);});});++describe('a session the OS ended to relieve memory pressure', () => {+ const JETSAM_KILL: ProcessTerminationLookup = {+ outcome: 'killed',+ source: 'macos-jetsam',+ at: '2026-07-10T00:00:30.000Z',+ killReason: 'vm-pageshortage',+ victimChosenByPriority: true,+ largestProcessOwned: false,+ residentBytes: 62_914_560,+ reportsRead: 1,+ };++ function bootLine(rig: Rig): Record<string, unknown> | undefined {+ return rig.infos.find((line) => line.event === 'crash-detection.boot');+ }++ function machineDeathLine(rig: Rig): Record<string, unknown> | undefined {+ return rig.infos.find((line) => line.event === 'crash-detection.machine-level-death');+ }++ test('the sentinel names the main process it watches', () => {+ const rig = makeRig();+ createCrashDetection(rig.deps).detectBootCrash();++ expect(JSON.parse(readFileSync(rig.deps.sentinelPath, 'utf8')).pid).toBe(20480);+ });++ test('a priority-order jetsam kill while another process held the most memory never prompts', () => {+ const rig = makeRig();+ createCrashDetection(rig.deps).detectBootCrash();+ const lastAliveAt = readSentinel(rig).lastAliveAt ?? '';++ rig.setProcessTermination(JETSAM_KILL);+ rig.deps.processId = 30111;+ const sessionB = createCrashDetection(rig.deps);+ expect(sessionB.detectBootCrash()).toBeNull();+ sessionB.notifyRendererReady();+ expect(rig.emitted).toHaveLength(0);++ expect(rig.processTerminationQueries).toEqual([+ { pid: 20480, fromMs: Date.parse(lastAliveAt), toMs: rig.nowMs() },+ ]);+ expect(machineDeathLine(rig)).toMatchObject({+ reason: 'os-memory-pressure',+ class: 'external',+ machineCause: 'os-memory-pressure',+ previousPid: 20480,+ processTermination: JETSAM_KILL,+ });+ expect(JSON.parse(readFileSync(rig.deps.sentinelPath, 'utf8')).pid).toBe(30111);+ });++ test.each([+ ['this app held the most memory', { largestProcessOwned: true }],+ ['the report named no largest process', { largestProcessOwned: null }],+ [+ 'the kill targeted the largest compressed-memory holder',+ { killReason: 'vm-compressor-space-shortage', victimChosenByPriority: false },+ ],+ [+ 'the kill was for the process’s own memory limit',+ { killReason: 'per-process-limit', victimChosenByPriority: false },+ ],+ ])('a jetsam kill still prompts when %s, and the invitation records it', (_case, overrides) => {+ const rig = makeRig();+ createCrashDetection(rig.deps).detectBootCrash();++ const kill: ProcessTerminationLookup = { ...JETSAM_KILL, ...overrides };+ rig.setProcessTermination(kill);+ expect(bootInvite(createCrashDetection(rig.deps).detectBootCrash()).context.dirtyShutdown).toBe(+ true,+ );+ expect(bootLine(rig)).toMatchObject({ previousPid: 20480, processTermination: kill });+ expect(machineDeathLine(rig)).toBeUndefined();+ });++ test('a death the OS has no record of still prompts, and says the OS was asked', () => {+ const rig = makeRig();+ createCrashDetection(rig.deps).detectBootCrash();++ bootInvite(createCrashDetection(rig.deps).detectBootCrash());+ expect(bootLine(rig)).toMatchObject({+ previousPid: 20480,+ processTermination: {+ outcome: 'not-killed',+ reportsRead: 0,+ unreadable: 0,+ unparseable: 0,+ skippedNewerOverCap: 0,+ },+ });+ });++ test('the OS is not asked when a reboot, a fresh crash dump or a pid-less sentinel decides first', () => {+ const rebooted = makeRig();+ createCrashDetection(rebooted.deps).detectBootCrash();+ rebooted.setBootSessionUuid('boot-epoch-b');+ expect(createCrashDetection(rebooted.deps).detectBootCrash()).toBeNull();+ expect(rebooted.processTerminationQueries).toEqual([]);++ const dumped = makeRig();+ createCrashDetection(dumped.deps).detectBootCrash();+ seedMinidump(dumped, 'pending/native-crash.dmp', dumped.tick());+ dumped.setProcessTermination(JETSAM_KILL);+ bootInvite(createCrashDetection(dumped.deps).detectBootCrash());+ expect(dumped.processTerminationQueries).toEqual([]);++ const older = makeRig();+ createCrashDetection(older.deps).detectBootCrash();+ const { pid: _pid, ...withoutPid } = JSON.parse(readFileSync(older.deps.sentinelPath, 'utf8'));+ writeFileSync(older.deps.sentinelPath, `${JSON.stringify(withoutPid)}\n`);+ older.setProcessTermination(JETSAM_KILL);+ bootInvite(createCrashDetection(older.deps).detectBootCrash());+ expect(older.processTerminationQueries).toEqual([]);+ expect(bootLine(older)).toMatchObject({ previousPid: null, processTermination: null });+ });+});diff --git a/packages/desktop/src/main/crash-detection.ts b/packages/desktop/src/main/crash-detection.tsindex ae5d363b7..728f46636 100644--- a/packages/desktop/src/main/crash-detection.ts+++ b/packages/desktop/src/main/crash-detection.ts@@ -8,6 +8,7 @@ import {writeFileSync,} from 'node:fs';import { basename, dirname, join } from 'node:path';+import type { ProcessTerminationLookup, ProcessTerminationQuery } from '@inkeep/open-knowledge';import {MINIDUMP_FILE_EXTENSION,type OkBugReportCrashDetectedEvent,@@ -111,6 +112,7 @@ interface SentinelState {osShutdownReasons?: string[];suspendedAt?: string;appVersion?: string;+ pid?: number;}export interface MainExitRecord {@@ -164,12 +166,14 @@ export interface CrashDetectionDeps {crashDumpsDir: string;appBundleRoot: string;appVersion: string;+ processId: number;platform?: NodeJS.Platform;emit(event: OkBugReportCrashDetectedEvent): boolean;now(): Date;currentBootSessionUuid(): string | null;currentBootStartedAtMs?(): number | null;installInFlight?(span: { deathFromMs: number; deathToMs: number }): InstallInFlight | null;+ processTermination?(query: ProcessTerminationQuery): ProcessTerminationLookup;mainThreadWatchdog: Pick<MainThreadWatchdog, 'readPrevious' | 'readPreviousStall' | 'start'>;logger: CrashLogger;}@@ -757,6 +761,7 @@ export function createCrashDetection(deps: CrashDetectionDeps): CrashDetection {let prevOsShutdownReasons: string[] | null = null;let prevSuspendedAt: string | null = null;let prevAppVersion: string | null = null;+ let prevPid: number | null = null;if (sentinelRaw !== null) {let parsed: Record<string, unknown> | null = null;try {@@ -784,6 +789,9 @@ export function createCrashDetection(deps: CrashDetectionDeps): CrashDetection {}prevSuspendedAt = field('suspendedAt');prevAppVersion = asReportableAppVersion(field('appVersion'));+ const rawPid = parsed?.pid;+ prevPid =+ typeof rawPid === 'number' && Number.isInteger(rawPid) && rawPid > 0 ? rawPid : null;}const detectedAtMs = detectedAt.getTime();@@ -913,7 +921,7 @@ export function createCrashDetection(deps: CrashDetectionDeps): CrashDetection {? prevBootSessionUuid !== bootSessionUuid: bootStartedAtMs !== null && lastAliveMs !== null && bootStartedAtMs > lastAliveMs;const sentinelUnflushed = sentinelRaw?.includes('\u0000') ?? false;- const machineCause = rebootedBetweenSessions+ const announcedMachineCause = rebootedBetweenSessions? 'system-reboot': prevPendingOsShutdownAt !== null? 'os-shutdown'@@ -922,7 +930,24 @@ export function createCrashDetection(deps: CrashDetectionDeps): CrashDetection {: sentinelUnflushed? 'unflushed-sentinel': null;+ const processTermination =+ sentinelPresent &&+ announcedMachineCause === null &&+ installInFlight === null &&+ newDumps.length === 0 &&+ prevPid !== null &&+ lastAliveMs !== null &&+ deps.processTermination !== undefined+ ? deps.processTermination({ pid: prevPid, fromMs: lastAliveMs, toMs: detectedAtMs })+ : null;+ const killedByPriorityUnderOthersMemory =+ processTermination?.outcome === 'killed' &&+ processTermination.victimChosenByPriority &&+ processTermination.largestProcessOwned === false;+ const machineCause =+ announcedMachineCause ?? (killedByPriorityUnderOthersMemory ? 'os-memory-pressure' : null);const machineLevelDeath = sentinelPresent && machineCause !== null;+ const terminationFields = { previousPid: prevPid, processTermination };const updateInstallDeath = sentinelPresent && installInFlight !== null;const suppressibleDeath = machineLevelDeath || updateInstallDeath;@@ -974,6 +999,7 @@ export function createCrashDetection(deps: CrashDetectionDeps): CrashDetection {suspendedAt: prevSuspendedAt,pendingOsShutdownAt: prevPendingOsShutdownAt,osShutdownReasons: prevOsShutdownReasons,+ ...terminationFields,...previousSessionFields,};if (reason === 'os-shutdown') {@@ -988,11 +1014,13 @@ 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 === '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',+ : reason === 'os-memory-pressure'+ ? 'previous session was ended by the OS to relieve memory pressure another process caused — 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) {@@ -1049,6 +1077,7 @@ export function createCrashDetection(deps: CrashDetectionDeps): CrashDetection {}: {}),detectingAppVersion: deps.appVersion,+ ...terminationFields,...previousSessionFields,},'previous session ended uncleanly — arming report invitation',@@ -1067,6 +1096,7 @@ export function createCrashDetection(deps: CrashDetectionDeps): CrashDetection {startedAt: detectedAt.toISOString(),lastAliveAt: detectedAt.toISOString(),appVersion: deps.appVersion,+ pid: deps.processId,...(bootSessionUuid !== null ? { bootSessionUuid } : {}),};writeSentinel('arm');diff --git a/packages/desktop/src/main/index.ts b/packages/desktop/src/main/index.tsindex 6d8cf481a..86e5a451a 100644--- a/packages/desktop/src/main/index.ts+++ b/packages/desktop/src/main/index.ts@@ -24,6 +24,7 @@ import {EDITOR_TARGETS,editorConfigPathDisplay,editorEntryLocator,+ findJetsamKillSync,getOkArtifactPaths,isEntryUpToDate,isOwnManagedEntry,@@ -6142,6 +6143,7 @@ function bootPrimaryInstance(): void {crashDumpsDir: app.getPath('crashDumps'),appBundleRoot: appBundleRootFromExecutable(app.getPath('exe')),appVersion: app.getVersion(),+ processId: process.pid,emit: (event) => {const focused = BrowserWindow.getFocusedWindow();const candidates = focused@@ -6164,6 +6166,9 @@ function bootPrimaryInstance(): void {span,bootStateSnapshot.versionPendingInstallStagedAt,),+ ...(process.platform === 'darwin'+ ? { processTermination: (query) => findJetsamKillSync(query) }+ : {}),mainThreadWatchdog: createMainThreadWatchdog({path: join(app.getPath('userData'), 'bug-report-main-thread-liveness.json'),stallPath: join(app.getPath('userData'), 'bug-report-main-thread-stall.json'),diff --git a/packages/desktop/src/main/ipc/bug-report.test.ts b/packages/desktop/src/main/ipc/bug-report.test.tsindex e09b17779..b351a6a88 100644--- a/packages/desktop/src/main/ipc/bug-report.test.ts+++ b/packages/desktop/src/main/ipc/bug-report.test.ts@@ -153,6 +153,7 @@ function makeDeps(overrides: Partial<BugReportCreateDeps> = {}): BugReportCreatedesktopMeta: DESKTOP_META,outputPath: join(makeTmpDir(), 'report.zip'),userLogsDir: makeTmpDir(),+ cachesDir: makeTmpDir(),...overrides,};}@@ -3474,6 +3475,7 @@ describe('handleBugReportCreate — the crash dump that armed the invitation', (crashDumpsDir: join(dir, 'dumps'),appBundleRoot,appVersion: '0.9.9',+ processId: 4242,platform: 'darwin' as const,emit: () => true,now: () => {diff --git a/packages/desktop/src/main/ipc/bug-report.ts b/packages/desktop/src/main/ipc/bug-report.tsindex 861c1f525..f92485f11 100644--- a/packages/desktop/src/main/ipc/bug-report.ts+++ b/packages/desktop/src/main/ipc/bug-report.ts@@ -119,6 +119,7 @@ export interface BugReportCreateDeps {readLanguage?: () => LanguageMetadata;outputPath?: string;userLogsDir?: string;+ cachesDir?: string;newestMinidumpForReport?: () => MinidumpReportLookup;minidumpForCrashEvent?: (eventId: string) => CrashEventMinidumpLookup;screenshotPngBytes?: () => Buffer | null;@@ -497,6 +498,7 @@ export async function handleBugReportCreate(outputPath,bugReportsDir: dirname(outputPath),userLogsDir: deps.userLogsDir,+ cachesDir: deps.cachesDir,extraFiles: extraFiles.length === 0 ? undefined : extraFiles,logger: deps.logger,readDesktopEnv: () => ({