Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
fix(desktop): log auto-update and quit lifecycle to the diagnostic file (#1890)
Andrew Mikofalvy5e264631f83f
diff --git a/.changeset/desktop-updater-lifecycle-logging.md b/.changeset/desktop-updater-lifecycle-logging.mdnew file mode 100644index 000000000..ce6f3a63d--- /dev/null+++ b/.changeset/desktop-updater-lifecycle-logging.md@@ -0,0 +1,5 @@+---+"@inkeep/open-knowledge-desktop": patch+---++Desktop auto-update and app-quit events are now recorded in the diagnostic logs (`~/.ok/logs/desktop.<date>.log`). Previously the auto-updater logged only to the console — which packaged builds don't persist — and its async log buffer was lost when the process was killed for an update swap, so an unexpected restart left no trace of whether it came from the "Relaunch now" prompt, an install-on-quit, or a crash. The updater now writes through the file logger; `before-quit`, `before-quit-for-update`, and `will-quit` are logged; and the buffer is flushed to disk before the process exits.diff --git a/packages/desktop/src/main/auto-updater.ts b/packages/desktop/src/main/auto-updater.tsindex cdfb6ce7c..a436a0cc5 100644--- a/packages/desktop/src/main/auto-updater.ts+++ b/packages/desktop/src/main/auto-updater.ts@@ -361,10 +361,6 @@ export function startAutoUpdater(opts: StartAutoUpdaterOpts): StartAutoUpdaterHaconst onUpdateAvailable = (info: { version?: string }): void => {logger.info('update-available', { version: info.version });- try {- const { logger: fl } = require('./desktop-logger.ts');- fl.child('updater').info({ version: info.version ?? 'unknown' }, 'update available');- } catch {}const offerClass = classifyOffer(info.version);if (offerClass !== 'same-channel') {logger.warn('update-available vetoed', {@@ -407,10 +403,6 @@ export function startAutoUpdater(opts: StartAutoUpdaterOpts): StartAutoUpdaterHaconst onUpdateNotAvailable = (info: { version?: string }): void => {logger.info('update-not-available', { version: info.version });- try {- const { logger: fl } = require('./desktop-logger.ts');- fl.child('updater').info({ version: info.version ?? 'unknown' }, 'no update available');- } catch {}markCheckSucceeded();if (menuCheckPending) {menuCheckPending = false;@@ -429,10 +421,7 @@ export function startAutoUpdater(opts: StartAutoUpdaterOpts): StartAutoUpdaterHa};const onUpdateDownloaded = (info: { version?: string }): void => {- try {- const { logger: fl } = require('./desktop-logger.ts');- fl.child('updater').info({ version: info.version ?? 'unknown' }, 'update downloaded');- } catch {}+ logger.info('update-downloaded', { version: info.version });const version = typeof info.version === 'string' ? info.version : '';if (!version) {logger.warn('update-downloaded with empty version — skipping dispatch');diff --git a/packages/desktop/src/main/desktop-logger.test.ts b/packages/desktop/src/main/desktop-logger.test.tsnew file mode 100644index 000000000..f9496bedd--- /dev/null+++ b/packages/desktop/src/main/desktop-logger.test.ts@@ -0,0 +1,14 @@+import { describe, expect, test } from 'bun:test';+import { flushDesktopLogger, getLogger } from './desktop-logger.ts';++describe('flushDesktopLogger', () => {+ test('does not throw when called before any logging has initialized the destination', () => {+ expect(() => flushDesktopLogger()).not.toThrow();+ });++ test('does not throw after the destination has been initialized by a log call', () => {+ getLogger('test-flush').info({}, 'init destination');+ expect(() => flushDesktopLogger()).not.toThrow();+ expect(() => flushDesktopLogger()).not.toThrow();+ });+});diff --git a/packages/desktop/src/main/desktop-logger.ts b/packages/desktop/src/main/desktop-logger.tsindex b9d3eaf63..fc4866103 100644--- a/packages/desktop/src/main/desktop-logger.ts+++ b/packages/desktop/src/main/desktop-logger.ts@@ -82,6 +82,7 @@ const loggerName = 'desktop';const logFileName = `${loggerName}.${todayDateString()}.log`;let rootLogger: pino.Logger | undefined;+let rootDest: { flushSync: () => void } | undefined;function getRootLogger(): pino.Logger {if (rootLogger) return rootLogger;@@ -91,6 +92,7 @@ function getRootLogger(): pino.Logger {const filePath = join(OK_LOGS_DIR, logFileName);const dest = pino.destination({ dest: filePath, append: true, sync: false });+ rootDest = dest as unknown as { flushSync: () => void };rootLogger = pino({@@ -133,3 +135,9 @@ export function getLogger(subsystem: string): DesktopLogger {export function getRootDesktopLogger(): pino.Logger {return getRootLogger();}++export function flushDesktopLogger(): void {+ try {+ rootDest?.flushSync();+ } catch {}+}diff --git a/packages/desktop/src/main/index.ts b/packages/desktop/src/main/index.tsindex df7edd71f..ea451ec57 100644--- a/packages/desktop/src/main/index.ts+++ b/packages/desktop/src/main/index.ts@@ -64,6 +64,7 @@ import {BrowserWindow,clipboard,dialog,+ autoUpdater as electronAutoUpdater,ipcMain,Menu,nativeImage,@@ -117,7 +118,7 @@ import {runCreateNew,} from './create-new-project.ts';import { createDebugIpc, type DebugIpcHandle } from './debug-ipc.ts';-import { getLogger, getRootDesktopLogger } from './desktop-logger.ts';+import { flushDesktopLogger, getLogger, getRootDesktopLogger } from './desktop-logger.ts';import { promptForExistingFolder } from './dialog-helpers.ts';import {type DriverUtilityLike,@@ -2573,6 +2574,16 @@ function bootPrimaryInstance(): void {});autoUpdaterHandle = await bootAutoUpdater(() => import('electron-updater'), {+ logger: {+ info: (msg: string, ctx?: object) =>+ getLogger('updater').info((ctx ?? {}) as Record<string, unknown>, msg),+ warn: (msg: string, ctx?: object) =>+ getLogger('updater').warn((ctx ?? {}) as Record<string, unknown>, msg),+ error: (msg: string, ctx?: object) =>+ getLogger('updater').error((ctx ?? {}) as Record<string, unknown>, msg),+ debug: (msg: string, ctx?: object) =>+ getLogger('updater').debug((ctx ?? {}) as Record<string, unknown>, msg),+ },ipcMain,readState: () => appState,writeState: (next) => {@@ -2622,6 +2633,7 @@ function bootPrimaryInstance(): void {});}await wm?.stopAllOwnedServers();+ flushDesktopLogger();},showCheckNowResult: (result) => {const target = BrowserWindow.getFocusedWindow() ?? BrowserWindow.getAllWindows()[0];@@ -2675,13 +2687,24 @@ function bootPrimaryInstance(): void {console.error(JSON.stringify({ event: 'whenReady-unhandled-rejection', message, stack }));});+ app.on('before-quit', () => {+ getLogger('lifecycle').info({}, 'before-quit');+ flushDesktopLogger();+ });+ electronAutoUpdater.on('before-quit-for-update', () => {+ getLogger('updater').info({}, 'before-quit-for-update — update install will relaunch the app');+ flushDesktopLogger();+ });+app.on('will-quit', () => {+ getLogger('lifecycle').info({}, 'will-quit');autoUpdaterHandle?.destroy();autoUpdaterHandle = null;bundleReplaceWatcherHandle?.stop();bundleReplaceWatcherHandle = null;mcpWiringHandle?.destroy();mcpWiringHandle = null;+ flushDesktopLogger();});app.on('window-all-closed', () => {