Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(ok): guard session-end reasons and add the changeset (PRD-8174) (#3778)

Andrew Mikofalvy010e21f109e0
diff --git a/.changeset/windows-machine-death-suppression.md b/.changeset/windows-machine-death-suppression.md
new file mode 100644
index 000000000..bc8a72449
--- /dev/null
+++ b/.changeset/windows-machine-death-suppression.md
@@ -0,0 +1,11 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Shutting down, restarting, or signing out of Windows no longer makes Open Knowledge report the previous session as a crash. On the next launch you were asked to send a bug report for a session that had ended perfectly normally, and the report carried no evidence of anything going wrong, because nothing had.
+
+Open Knowledge already suppressed that prompt when the machine ended a session rather than the app, but it recognized only signals that Windows does not send. Windows announces an ending its own way, to each window rather than to the application, and the app was not listening for it. That left Windows the only platform where a normal shutdown was indistinguishable from a crash, so it was always reported as one.
+
+The app now records that announcement the moment it arrives, along with what Windows says about it — a sign-out is distinguishable from a machine powering down, which is detail the other platforms never had. Windows does not separate a restart from a shutdown, so neither do we.
+
+Suppression stays deliberately narrow. A session that ends without any announcement at all, as in a power cut or a forced power-off, still prompts, and so does anything that leaves a genuine crash record behind. A shutdown you cancel does not leave a mark that would quietly hide a later crash.
diff --git a/packages/desktop/src/main/crash-detection.test.ts b/packages/desktop/src/main/crash-detection.test.ts
index f6271c5cd..d2c6ddc7f 100644
--- a/packages/desktop/src/main/crash-detection.test.ts
+++ b/packages/desktop/src/main/crash-detection.test.ts
@@ -789,6 +789,42 @@ describe('machine-level death suppression', () => {
expect(breadcrumb?.osShutdownReasons).toBeNull();
});
+ test('a non-array from the native boundary still records the marker', () => {
+ const rig = makeRig();
+ const sessionA = createCrashDetection(rig.deps);
+ sessionA.detectBootCrash();
+
+ // The parameter type is a compile-time claim about a value that arrives
+ // from Electron's win32 event, not a runtime guarantee. If this threw, it
+ // would take `writeSentinel` with it and lose the marker entirely — the
+ // one thing this path exists to write.
+ expect(() => sessionA.noteOsShutdown(null as unknown as readonly string[])).not.toThrow();
+
+ expect(readSentinel(rig).pendingOsShutdownAt).toBeTruthy();
+ expect(readSentinel(rig).osShutdownReasons).toBeUndefined();
+
+ // And the marker still does its job.
+ const armed = createCrashDetection(rig.deps).detectBootCrash();
+ expect(armed).toBeNull();
+ });
+
+ test('junk elements are filtered on the way IN, not just on the way out', () => {
+ const rig = makeRig();
+ const sessionA = createCrashDetection(rig.deps);
+ sessionA.detectBootCrash();
+
+ sessionA.noteOsShutdown(['shutdown', '', 42, 'logoff'] as unknown as readonly string[]);
+
+ // Asserted against the sentinel rather than the breadcrumb on purpose: the
+ // read path filters again on every boot, so reading through a second
+ // session would pass even if this write had persisted the junk. Only the
+ // file says whether the write side did its job.
+ expect((readSentinel(rig) as Record<string, unknown>).osShutdownReasons).toEqual([
+ 'shutdown',
+ 'logoff',
+ ]);
+ });
+
test('a malformed reasons array degrades to null rather than throwing', () => {
const rig = makeRig();
const warnLines: Array<Record<string, unknown>> = [];
diff --git a/packages/desktop/src/main/crash-detection.ts b/packages/desktop/src/main/crash-detection.ts
index dea6ee6f2..bf3caf5af 100644
--- a/packages/desktop/src/main/crash-detection.ts
+++ b/packages/desktop/src/main/crash-detection.ts
@@ -972,10 +972,22 @@ export function createCrashDetection(deps: CrashDetectionDeps): CrashDetection {
noteOsShutdown(reasons?: readonly string[]): void {
if (sentinel === null || cleanQuitMarked) return;
sentinel.pendingOsShutdownAt = deps.now().toISOString();
- // Absent rather than empty when the caller had nothing to say, so the
- // next boot can tell "the OS did not name a cause" from "it named none".
- if (reasons !== undefined && reasons.length > 0) {
- sentinel.osShutdownReasons = [...reasons];
+ // No cause and an empty cause both collapse to an absent field: the next
+ // boot reads them identically ("the OS named no cause"), so writing an
+ // empty array would only add a second spelling of the same state.
+ //
+ // Validated on the way in, mirroring the read path, because these values
+ // cross a native boundary out of Electron's win32 event rather than
+ // coming from our own code — and the parameter type is a compile-time
+ // claim about that, not a runtime guarantee. Getting it wrong is not a
+ // cosmetic failure: an exception here would skip `writeSentinel` below
+ // and lose the marker entirely, defeating the suppression this whole
+ // path exists to provide.
+ const usable = Array.isArray(reasons)
+ ? reasons.filter((value): value is string => typeof value === 'string' && value !== '')
+ : [];
+ if (usable.length > 0) {
+ sentinel.osShutdownReasons = usable;
} else {
delete sentinel.osShutdownReasons;
}