Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

27.7 KB
Raw Blame
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,
  PACKAGED_DESKTOP_PRODUCT_NAMES,
} from '@inkeep/open-knowledge-core';
import { okUserHomeDir } from '@inkeep/open-knowledge-core/server';
import {
  DIAGNOSTIC_REPORT_WINDOW_DAYS,
  isOwnedProcessName,
  parseHeaderTimestamp,
} from './diagnostic-reports.ts';

export const OS_TERMINATION_EVIDENCE_PATH = 'state/os-termination-evidence.json';

const WINDOW_MS = DIAGNOSTIC_REPORT_WINDOW_DAYS * 24 * 60 * 60 * 1000;
const MAX_JETSAM_FILES = 50;
const MAX_EVENTS = 50;
const COMMAND_TIMEOUT_MS = 15_000;
const COMMAND_MAX_BUFFER = 16 * 1024 * 1024;

const MACOS_SYSTEM_DIAGNOSTIC_REPORTS_DIR = '/Library/Logs/DiagnosticReports';

const OWNED_NAME = /openknowledge/i;

const WINDOWS_EXECUTABLES = PACKAGED_DESKTOP_PRODUCT_NAMES.map((name) =>
  desktopWindowsExecutableName(DESKTOP_PRODUCTS[name]),
);

type OsTerminationSource = 'macos-jetsam' | 'linux-journal' | 'windows-event-log';

interface JetsamProcess {
  name: string;
  pid: number | null;
  killReason: string | null;
  residentPages: number | null;
  lifetimeMaxPages: number | null;
}

interface JetsamEvidenceEvent {
  kind: 'jetsam';
  at: string | null;
  file: string;
  pageSize: number | null;
  largestProcessOwned: boolean | null;
  ownedProcesses: JetsamProcess[];
}

interface JournalEvidenceEvent {
  kind: 'journal';
  at: string;
  origin: 'kernel' | 'systemd-oomd' | 'chromium';
  action: 'oom-invoked' | 'oom-kill' | 'oomd-kill' | 'display-connection-lost';
  pid: number | null;
  processName: string | null;
  unit: string | null;
  trigger: 'memory-pressure' | 'swap' | 'wayland' | 'x11' | null;
  detail?: string;
}

interface WindowsEvidenceEvent {
  kind: 'windows-event';
  at: string;
  log: 'System' | 'Application';
  provider: string;
  eventId: number;
  mentionsOpenKnowledge: boolean;
  data: string[];
}

type OsTerminationEvent = JetsamEvidenceEvent | JournalEvidenceEvent | WindowsEvidenceEvent;

export interface OsTerminationEvidence {
  schemaVersion: 1;
  platform: string;
  source: OsTerminationSource | null;
  outcome: 'unsupported' | 'unavailable' | 'partial' | 'none-in-window' | 'collected';
  readFrom: string[];
  unavailableReason?: string;
  restrictedToCurrentUser?: boolean;
  windowDays: number;
  foreignIgnored: number;
  unparseable: number;
  droppedOverCap: number;
  events: OsTerminationEvent[];
}

interface OsTerminationNotCollected {
  schemaVersion: 1;
  outcome: 'not-collected';
}

type OsTerminationEvidenceDocument = OsTerminationEvidence | OsTerminationNotCollected;

export interface CommandResult {
  status: number | null;
  stdout: string;
  stderr: string;
  errorCode?: string;
  signal?: string;
}

type RunCommand = (command: string, args: string[]) => Promise<CommandResult>;

export interface OsTerminationEvidenceDeps {
  platform?: NodeJS.Platform;
  now?: () => Date;
  macosReportsDir?: string;
  systemRoot?: string;
  desktopLogsDir?: string;
  runCommand?: RunCommand;
}

export function createRunCommand(
  options: { timeoutMs?: number; maxBuffer?: number } = {},
): RunCommand {
  return (command, args) =>
    new Promise((resolvePromise) => {
      try {
        execFile(
          command,
          args,
          {
            encoding: 'utf8',
            timeout: options.timeoutMs ?? COMMAND_TIMEOUT_MS,
            maxBuffer: options.maxBuffer ?? COMMAND_MAX_BUFFER,
            windowsHide: true,
          },
          (error: ExecFileException | null, stdout, stderr) => {
            resolvePromise({ ...describeExit(error), stdout, stderr });
          },
        );
      } catch (err) {
        const code = (err as NodeJS.ErrnoException).code;
        resolvePromise({
          status: null,
          stdout: '',
          stderr: '',
          errorCode: typeof code === 'string' ? code : 'spawn-failed',
        });
      }
    });
}

function describeExit(
  error: ExecFileException | null,
): Pick<CommandResult, 'status' | 'errorCode' | 'signal'> {
  if (error === null) return { status: 0 };
  if (typeof error.code === 'string') return { status: null, errorCode: error.code };
  if (typeof error.code === 'number') return { status: error.code };
  if (error.killed === true) return { status: null, errorCode: 'ETIMEDOUT' };
  return error.signal ? { status: null, signal: error.signal } : { status: null };
}

function describeFailure(result: CommandResult): string {
  if (result.errorCode !== undefined) return result.errorCode;
  if (result.signal !== undefined) return `signal ${result.signal}`;
  const firstLine = (result.stderr.trim().split('\n')[0] ?? '')
    .replace(/\S*\/journal\/\S*/g, '<journal path>')
    .replace(/\b[0-9a-f]{32}\b/gi, '<id>');
  return `exit ${result.status}${firstLine === '' ? '' : `: ${firstLine}`}`;
}

function base(
  platform: string,
  source: OsTerminationSource | null,
  readFrom: string[],
): OsTerminationEvidence {
  return {
    schemaVersion: 1,
    platform,
    source,
    outcome: 'none-in-window',
    readFrom,
    windowDays: DIAGNOSTIC_REPORT_WINDOW_DAYS,
    foreignIgnored: 0,
    unparseable: 0,
    droppedOverCap: 0,
    events: [],
  };
}

function unavailable(evidence: OsTerminationEvidence, reason: string): OsTerminationEvidence {
  return { ...evidence, outcome: 'unavailable', unavailableReason: reason };
}

function settle(evidence: OsTerminationEvidence): OsTerminationEvidence {
  return { ...evidence, outcome: evidence.events.length > 0 ? 'collected' : 'none-in-window' };
}

function finiteOrNull(value: unknown): number | null {
  return typeof value === 'number' && Number.isFinite(value) ? value : null;
}

function asRecord(value: unknown): Record<string, unknown> | null {
  return typeof value === 'object' && value !== null && !Array.isArray(value)
    ? (value as Record<string, unknown>)
    : null;
}

export function parseJetsamReport(file: string, content: string): JetsamEvidenceEvent | null {
  const newline = content.indexOf('\n');
  if (newline === -1) return null;
  let header: Record<string, unknown> | null;
  let body: Record<string, unknown> | null;
  try {
    header = asRecord(JSON.parse(content.slice(0, newline)));
    body = asRecord(JSON.parse(content.slice(newline + 1)));
  } catch {
    return null;
  }
  if (header === null || body === null || !Array.isArray(body.processes)) return null;
  const ownedProcesses: JetsamProcess[] = [];
  for (const entry of body.processes) {
    const p = asRecord(entry);
    if (p === null || typeof p.name !== 'string' || !isOwnedProcessName(p.name)) continue;
    ownedProcesses.push({
      name: p.name,
      pid: finiteOrNull(p.pid),
      killReason: typeof p.reason === 'string' ? p.reason : null,
      residentPages: finiteOrNull(p.rpages),
      lifetimeMaxPages: finiteOrNull(p.lifetimeMax),
    });
  }
  const atMs = parseHeaderTimestamp(header.timestamp);
  return {
    kind: 'jetsam',
    at: atMs === null ? null : new Date(atMs).toISOString(),
    file,
    pageSize: finiteOrNull(asRecord(body.memoryStatus)?.pageSize),
    largestProcessOwned:
      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[];
  try {
    names = await readdir(dir);
  } catch (err) {
    return unavailable(evidence, (err as NodeJS.ErrnoException).code ?? 'unknown');
  }
  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'));
    } catch {}
    if (parsed === null) {
      evidence.unparseable += 1;
    } else if (parsed.ownedProcesses.length === 0) {
      evidence.foreignIgnored += 1;
    } else {
      evidence.events.push(parsed);
    }
  }
  return 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[] = [
  '--no-pager',
  '--output=short-iso-precise',
  '--since=-7d',
  `--grep=${LINUX_JOURNAL_GREP}`,
  '_TRANSPORT=kernel',
  '+',
  '_SYSTEMD_UNIT=systemd-oomd.service',
];

export const LINUX_DISPLAY_FATAL_GREP =
  'Fatal Wayland communication error|Wayland protocol error|X IO error|Missing X server';

const LINUX_USER_JOURNAL_ARGS: readonly string[] = [
  '--user',
  '--no-pager',
  '--output=short-iso-precise',
  '--since=-7d',
  `--grep=${LINUX_DISPLAY_FATAL_GREP}`,
];

const JOURNAL_LINE = /^(\S+) \S+ ([^:\s[]+)(?:\[\d+\])?: (.*)$/;

const CHROMIUM_LOG_LINE = /\[(\d+)(?::\d+)?:\d{4}\/\d{6}\.\d+:[A-Z0-9]+:[^\]]*\]\s*(.*)$/;

const MAX_DETAIL_LENGTH = 200;

const DESKTOP_LOG_FILE = /^desktop\.\d{4}-\d{2}-\d{2}\.log$/;

const DESKTOP_BOOT_LINE = '"event":"desktop.boot"';

async function readDesktopMainPids(
  logsDir: string,
  now: Date,
): Promise<{ pids: Set<number>; unreadable: boolean }> {
  const pids = new Set<number>();
  let names: string[];
  try {
    names = await readdir(logsDir);
  } catch {
    return { pids, unreadable: true };
  }
  let unreadable = false;
  for (const name of names) {
    if (!DESKTOP_LOG_FILE.test(name)) continue;
    const path = join(logsDir, name);
    try {
      if (now.getTime() - (await stat(path)).mtimeMs > WINDOW_MS) continue;
      for (const line of (await readFile(path, 'utf8')).split('\n')) {
        if (!line.includes(DESKTOP_BOOT_LINE)) continue;
        const pid = /"pid":(\d+)/.exec(line)?.[1];
        if (pid !== undefined) pids.add(Number(pid));
      }
    } catch {
      unreadable = true;
    }
  }
  return { pids, unreadable };
}

function parseJournalMessage(
  origin: string,
  message: string,
): Omit<JournalEvidenceEvent, 'kind' | 'at'> | null {
  if (origin === 'systemd-oomd') {
    const killed = /^Killed (\S+)/.exec(message);
    if (killed === null) return null;
    const leaf = killed[1]?.split('/').pop() ?? '';
    return {
      origin: 'systemd-oomd',
      action: 'oomd-kill',
      pid: null,
      processName: null,
      unit: OWNED_NAME.test(leaf) ? leaf : null,
      trigger: /swap used/.test(message)
        ? 'swap'
        : /memory pressure/.test(message)
          ? 'memory-pressure'
          : null,
    };
  }
  if (origin !== 'kernel') return null;
  const killedProcess = /Killed process (\d+) \(([^)]+)\)/.exec(message);
  if (killedProcess !== null) {
    return {
      origin: 'kernel',
      action: 'oom-kill',
      pid: Number(killedProcess[1]),
      processName: killedProcess[2] ?? null,
      unit: null,
      trigger: null,
    };
  }
  const task = /task=([^,]+),pid=(\d+)/.exec(message);
  if (task !== null) {
    return {
      origin: 'kernel',
      action: 'oom-kill',
      pid: Number(task[2]),
      processName: task[1] ?? null,
      unit: null,
      trigger: null,
    };
  }
  const invoked = /^(\S+) invoked oom-killer/.exec(message);
  if (invoked !== null) {
    return {
      origin: 'kernel',
      action: 'oom-invoked',
      pid: null,
      processName: invoked[1] ?? null,
      unit: null,
      trigger: null,
    };
  }
  return null;
}

function parseJournalOutput(stdout: string): {
  events: JournalEvidenceEvent[];
  foreignIgnored: number;
  unparseable: number;
} {
  const events: JournalEvidenceEvent[] = [];
  let foreignIgnored = 0;
  let unparseable = 0;
  for (const line of stdout.split('\n')) {
    if (line.trim() === '' || line.startsWith('-- ')) continue;
    const match = JOURNAL_LINE.exec(line);
    if (match === null) {
      unparseable += 1;
      continue;
    }
    const [, at = '', origin = '', message = ''] = match;
    if (!OWNED_NAME.test(message)) {
      foreignIgnored += 1;
      continue;
    }
    const parsed = parseJournalMessage(origin, message);
    if (parsed === null) {
      unparseable += 1;
      continue;
    }
    events.push({ kind: 'journal', at, ...parsed });
  }
  return { events, foreignIgnored, unparseable };
}

interface DisplayFatalLine {
  at: string;
  pid: number;
  detail: string;
}

function parseDisplayFatalOutput(stdout: string): {
  lines: DisplayFatalLine[];
  foreignIgnored: number;
  unparseable: number;
} {
  const lines: DisplayFatalLine[] = [];
  let foreignIgnored = 0;
  let unparseable = 0;
  for (const line of stdout.split('\n')) {
    if (line.trim() === '' || line.startsWith('-- ')) continue;
    const match = JOURNAL_LINE.exec(line);
    if (match === null) {
      unparseable += 1;
      continue;
    }
    const [, at = '', , message = ''] = match;
    const chromium = CHROMIUM_LOG_LINE.exec(message);
    if (chromium === null) {
      foreignIgnored += 1;
      continue;
    }
    lines.push({
      at,
      pid: Number(chromium[1]),
      detail: (chromium[2] ?? '').slice(0, MAX_DETAIL_LENGTH),
    });
  }
  return { lines, foreignIgnored, unparseable };
}

async function attributeDisplayFatalLines(
  stdout: string,
  desktopLogsDir: string,
  now: Date,
): Promise<{
  events: JournalEvidenceEvent[];
  foreignIgnored: number;
  unparseable: number;
  unattributed: string | null;
}> {
  const parsed = parseDisplayFatalOutput(stdout);
  if (parsed.lines.length === 0) return { ...parsed, events: [], unattributed: null };
  const owned = await readDesktopMainPids(desktopLogsDir, now);
  const events: JournalEvidenceEvent[] = [];
  let unmatched = 0;
  for (const line of parsed.lines) {
    if (!owned.pids.has(line.pid)) {
      unmatched += 1;
      continue;
    }
    events.push({
      kind: 'journal',
      at: line.at,
      origin: 'chromium',
      action: 'display-connection-lost',
      pid: line.pid,
      processName: null,
      unit: null,
      trigger: /Wayland/.test(line.detail) ? 'wayland' : 'x11',
      detail: line.detail,
    });
  }
  const ownershipProvable = owned.pids.size > 0 && !owned.unreadable;
  if (unmatched > 0 && !ownershipProvable) {
    const why = owned.unreadable
      ? 'desktop logs unreadable'
      : 'no desktop.boot pid in desktop logs';
    return {
      events,
      foreignIgnored: parsed.foreignIgnored,
      unparseable: parsed.unparseable,
      unattributed: `${unmatched} Chromium display line(s) not attributed, ${why}`,
    };
  }
  return {
    events,
    foreignIgnored: parsed.foreignIgnored + unmatched,
    unparseable: parsed.unparseable,
    unattributed: null,
  };
}

function journalRead(result: CommandResult): boolean {
  return result.status === 0 || (result.status === 1 && /^-- No entries --$/m.test(result.stdout));
}

async function collectLinuxJournal(
  run: RunCommand,
  now: Date,
  desktopLogsDir: string,
): Promise<OsTerminationEvidence> {
  const evidence = base('linux', 'linux-journal', [
    `journalctl ${LINUX_JOURNAL_ARGS.join(' ')}`,
    `journalctl ${LINUX_USER_JOURNAL_ARGS.join(' ')}`,
  ]);
  const [system, user] = await Promise.all([
    run('journalctl', [...LINUX_JOURNAL_ARGS]),
    run('journalctl', [...LINUX_USER_JOURNAL_ARGS]),
  ]);
  const failures: string[] = [];
  if (journalRead(system)) {
    const parsed = parseJournalOutput(system.stdout);
    evidence.droppedOverCap += Math.max(0, parsed.events.length - MAX_EVENTS);
    evidence.events.push(...parsed.events.slice(-MAX_EVENTS));
    evidence.foreignIgnored += parsed.foreignIgnored;
    evidence.unparseable += parsed.unparseable;
    if (/not seeing messages from other users/i.test(`${system.stdout}\n${system.stderr}`)) {
      evidence.restrictedToCurrentUser = true;
    }
  } else {
    failures.push(`system journal: ${describeFailure(system)}`);
  }
  if (journalRead(user)) {
    const parsed = await attributeDisplayFatalLines(user.stdout, desktopLogsDir, now);
    evidence.droppedOverCap += Math.max(0, parsed.events.length - MAX_EVENTS);
    evidence.events.push(...parsed.events.slice(-MAX_EVENTS));
    evidence.foreignIgnored += parsed.foreignIgnored;
    evidence.unparseable += parsed.unparseable;
    if (parsed.unattributed !== null) failures.push(`user journal: ${parsed.unattributed}`);
  } else {
    failures.push(`user journal: ${describeFailure(user)}`);
  }
  if (failures.length === 2) return unavailable(evidence, failures.join('; '));
  if (failures.length === 1) {
    return { ...evidence, outcome: 'partial', unavailableReason: failures.join('; ') };
  }
  return settle(evidence);
}

const WINDOWS_SYSTEM_PROVIDERS: Record<number, string> = {
  41: 'Microsoft-Windows-Kernel-Power',
  1074: 'User32',
  2004: 'Microsoft-Windows-Resource-Exhaustion-Detector',
  6008: 'EventLog',
};

const WINDOWS_APPLICATION_PROVIDERS: Record<number, string> = {
  1000: 'Application Error',
  1001: 'Windows Error Reporting',
  1002: 'Application Hang',
};

type WindowsLog = 'System' | 'Application';

function providersFor(log: WindowsLog): Record<number, string> {
  return log === 'System' ? WINDOWS_SYSTEM_PROVIDERS : WINDOWS_APPLICATION_PROVIDERS;
}

function windowsQuery(log: WindowsLog): string {
  const ids = Object.keys(providersFor(log))
    .map((id) => `EventID=${id}`)
    .join(' or ');
  const system = `System[(${ids}) and TimeCreated[timediff(@SystemTime) <= ${WINDOW_MS}]]`;
  if (log === 'System') return `*[${system}]`;
  const names = WINDOWS_EXECUTABLES.map((exe) => `Data='${exe}'`).join(' or ');
  return `*[${system} and EventData[${names}]]`;
}

export function windowsEventLogArgs(log: WindowsLog): string[] {
  return ['qe', log, `/q:${windowsQuery(log)}`, '/f:xml', '/rd:true', '/c:200'];
}

export function windowsEventLogExecutable(systemRoot: string | undefined): string {
  return win32.join(systemRoot ?? 'C:\\Windows', 'System32', 'wevtutil.exe');
}

function decodeXml(value: string): string {
  return value
    .replace(/&lt;/g, '<')
    .replace(/&gt;/g, '>')
    .replace(/&quot;/g, '"')
    .replace(/&apos;/g, "'")
    .replace(/&amp;/g, '&');
}

function looksLikePath(value: string): boolean {
  return /[\\/]/.test(value) || /^[a-z]:/i.test(value);
}

function elementTexts(record: string, element: string): string[] {
  const pattern = new RegExp(`<${element}(?:\\s[^>]*)?>([\\s\\S]*?)</${element}>`, 'g');
  return [...record.matchAll(pattern)].map((m) => decodeXml(m[1] ?? '').trim());
}

function parseWindowsEventXml(
  log: WindowsLog,
  xml: string,
): { events: WindowsEvidenceEvent[]; foreignIgnored: number; unparseable: number } {
  const providers = providersFor(log);
  const events: WindowsEvidenceEvent[] = [];
  let foreignIgnored = 0;
  let unparseable = 0;
  for (const record of xml.match(/<Event[\s>][\s\S]*?<\/Event>/g) ?? []) {
    const provider = /<Provider\s+Name=['"]([^'"]+)['"]/.exec(record)?.[1];
    const eventId = Number(/<EventID[^>]*>(\d+)<\/EventID>/.exec(record)?.[1]);
    const at = /<TimeCreated\s+SystemTime=['"]([^'"]+)['"]/.exec(record)?.[1];
    if (provider === undefined || !Number.isInteger(eventId) || at === undefined) {
      unparseable += 1;
      continue;
    }
    if (providers[eventId] !== provider) {
      foreignIgnored += 1;
      continue;
    }
    const values = elementTexts(record, 'Data');
    const processNames = elementTexts(record, 'Name');
    const mentionsOpenKnowledge = [...values, ...processNames].some((v) => OWNED_NAME.test(v));
    if (log === 'Application' && !mentionsOpenKnowledge) {
      foreignIgnored += 1;
      continue;
    }
    events.push({
      kind: 'windows-event',
      at,
      log,
      provider,
      eventId,
      mentionsOpenKnowledge,
      data: log === 'Application' ? values.filter((v) => v !== '' && !looksLikePath(v)) : [],
    });
  }
  return { events, foreignIgnored, unparseable };
}

async function collectWindowsEventLog(
  run: RunCommand,
  systemRoot: string | undefined,
): Promise<OsTerminationEvidence> {
  const executable = windowsEventLogExecutable(systemRoot);
  const logs = ['System', 'Application'] as const;
  const evidence = base(
    'win32',
    'windows-event-log',
    logs.map((log) => `${executable} ${windowsEventLogArgs(log).join(' ')}`),
  );
  const results = await Promise.all(
    logs.map(async (log) => ({ log, result: await run(executable, windowsEventLogArgs(log)) })),
  );
  const failures: string[] = [];
  for (const { log, result } of results) {
    if (result.status !== 0) {
      failures.push(`${log}: ${describeFailure(result)}`);
      continue;
    }
    const parsed = parseWindowsEventXml(log, result.stdout);
    evidence.droppedOverCap += Math.max(0, parsed.events.length - MAX_EVENTS);
    evidence.events.push(...parsed.events.slice(0, MAX_EVENTS));
    evidence.foreignIgnored += parsed.foreignIgnored;
    evidence.unparseable += parsed.unparseable;
  }
  if (failures.length === logs.length) return unavailable(evidence, failures.join('; '));
  if (failures.length > 0) {
    return { ...evidence, outcome: 'partial', unavailableReason: failures.join('; ') };
  }
  return settle(evidence);
}

export async function collectOsTerminationEvidence(
  deps: OsTerminationEvidenceDeps = {},
): Promise<OsTerminationEvidence> {
  const platform = deps.platform ?? process.platform;
  const run = deps.runCommand ?? createRunCommand();
  const now = (deps.now ?? (() => new Date()))();
  switch (platform) {
    case 'darwin':
      return collectMacosJetsam(deps.macosReportsDir ?? MACOS_SYSTEM_DIAGNOSTIC_REPORTS_DIR, now);
    case 'linux':
      return collectLinuxJournal(run, now, deps.desktopLogsDir ?? join(okUserHomeDir(), 'logs'));
    case 'win32':
      return collectWindowsEventLog(run, deps.systemRoot ?? process.env.SystemRoot);
    default:
      return { ...base(platform, null, []), outcome: 'unsupported' };
  }
}

const NOT_COLLECTED: OsTerminationNotCollected = { schemaVersion: 1, outcome: 'not-collected' };

export function renderOsTerminationEvidence(evidence: OsTerminationEvidence | undefined): string {
  const document: OsTerminationEvidenceDocument = evidence ?? NOT_COLLECTED;
  return `${JSON.stringify(document, null, 2)}\n`;
}

const SOURCE_NAMES: Record<OsTerminationSource, string> = {
  'macos-jetsam': 'macOS low-memory reports',
  'linux-journal': 'system and user journals',
  'windows-event-log': 'Windows event logs',
};

export function describeOsTerminationEvidence(evidence: OsTerminationEvidence): string {
  const counts = [
    `${evidence.windowDays}d`,
    `${evidence.events.length} kept`,
    `${evidence.foreignIgnored} other-process record(s) ignored`,
    `${evidence.unparseable} unparseable`,
  ];
  if (evidence.droppedOverCap > 0) counts.push(`${evidence.droppedOverCap} older dropped over cap`);
  const source = evidence.source ? ` from ${SOURCE_NAMES[evidence.source]}` : '';
  const reason = evidence.unavailableReason ? `, ${evidence.unavailableReason}` : '';
  return `${evidence.outcome}${source}${reason} (${counts.join('; ')})`;
}