From 777b2a600f5e353c5715178bd56439bf0b0a573f Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 05:21:33 -0600 Subject: [PATCH] fix(aggregator): stores distinguish ENOENT from other read errors (#2980) --- pm-stack/aggregator/src/actions.ts | 29 +++++++++- pm-stack/aggregator/src/comments.ts | 29 +++++++++- .../aggregator/test/store-corrupt.test.ts | 54 ++++++++++++++++++- 3 files changed, 107 insertions(+), 5 deletions(-) diff --git a/pm-stack/aggregator/src/actions.ts b/pm-stack/aggregator/src/actions.ts index 6a74371b3..8f9021876 100644 --- a/pm-stack/aggregator/src/actions.ts +++ b/pm-stack/aggregator/src/actions.ts @@ -442,8 +442,33 @@ export class ActionsStore { let raw: string; try { raw = await readFile(this.file, "utf8"); - } catch { - this.actions = []; // no store yet — the first write creates it + } catch (err) { + if ((err as { code?: unknown })?.code === "ENOENT") { + this.actions = []; // no store yet — the first write creates it + return; + } + // #2980: a non-ENOENT read failure (EACCES, a transient I/O error, + // a directory in place of the file, ...) is NOT "no store yet": + // the on-disk audit history may be intact but unread. Handle it + // like the corrupt case below — quarantine the original bytes + // before starting empty, and log loudly — so the next persist + // cannot silently overwrite history the read never saw. + this.actions = []; + const quarantined = `${this.file}.corrupt-${new Date().toISOString().replace(/[:.]/g, "-")}`; + try { + await rename(this.file, quarantined); + console.error( + `[actions] WARNING: actions store ${this.file} could not be read (${err instanceof Error ? err.message : String(err)}); ` + + `quarantined the original bytes to ${quarantined} and started empty. ` + + `The quarantined file holds the prior audit history — do not delete it.`, + ); + } catch (renameErr) { + console.error( + `[actions] WARNING: actions store ${this.file} could not be read (${err instanceof Error ? err.message : String(err)}) ` + + `and could not be quarantined (${renameErr instanceof Error ? renameErr.message : String(renameErr)}); starting empty — ` + + `the next persist will overwrite it, so copy it aside now to preserve the audit history.`, + ); + } return; } try { diff --git a/pm-stack/aggregator/src/comments.ts b/pm-stack/aggregator/src/comments.ts index 2a5e19a7a..90a629c48 100644 --- a/pm-stack/aggregator/src/comments.ts +++ b/pm-stack/aggregator/src/comments.ts @@ -121,8 +121,33 @@ export class CommentsStore { let raw: string; try { raw = await readFile(this.file, "utf8"); - } catch { - this.comments = []; // no store yet — the first write creates it + } catch (err) { + if ((err as { code?: unknown })?.code === "ENOENT") { + this.comments = []; // no store yet — the first write creates it + return; + } + // #2980: a non-ENOENT read failure (EACCES, a transient I/O error, + // a directory in place of the file, ...) is NOT "no store yet": + // the on-disk audit history may be intact but unread. Handle it + // like the corrupt case below — quarantine the original bytes + // before starting empty, and log loudly — so the next persist + // cannot silently overwrite history the read never saw. + this.comments = []; + const quarantined = `${this.file}.corrupt-${new Date().toISOString().replace(/[:.]/g, "-")}`; + try { + await rename(this.file, quarantined); + console.error( + `[comments] WARNING: comments store ${this.file} could not be read (${err instanceof Error ? err.message : String(err)}); ` + + `quarantined the original bytes to ${quarantined} and started empty. ` + + `The quarantined file holds the prior audit history — do not delete it.`, + ); + } catch (renameErr) { + console.error( + `[comments] WARNING: comments store ${this.file} could not be read (${err instanceof Error ? err.message : String(err)}) ` + + `and could not be quarantined (${renameErr instanceof Error ? renameErr.message : String(renameErr)}); starting empty — ` + + `the next persist will overwrite it, so copy it aside now to preserve the audit history.`, + ); + } return; } try { diff --git a/pm-stack/aggregator/test/store-corrupt.test.ts b/pm-stack/aggregator/test/store-corrupt.test.ts index b83ee911b..ea2726d4b 100644 --- a/pm-stack/aggregator/test/store-corrupt.test.ts +++ b/pm-stack/aggregator/test/store-corrupt.test.ts @@ -4,7 +4,7 @@ // .corrupt- with its original bytes preserved, never // silently overwritten by the next persist. import { describe, expect, test } from "bun:test"; -import { existsSync, mkdtempSync, readFileSync, readdirSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { ActionsStore, type NewAction } from "../src/actions.ts"; @@ -65,6 +65,32 @@ describe("actions store corruption handling (#1452)", () => { expect(quarantinedIn(dir, "actions.json").length).toBe(1); }); + test("unreadable store (directory in place of file) -> loud quarantine, empty store, fresh persist leaves the quarantine untouched", async () => { + const dir = mkdtempSync(join(tmpdir(), "agg-actions-unreadable-")); + const file = join(dir, "actions.json"); + mkdirSync(file); // readFile on a directory fails with EISDIR — a non-ENOENT read error (#2980) + const errors: string[] = []; + const origError = console.error; + console.error = (...args: unknown[]) => { errors.push(args.map(String).join(" ")); }; + let store: ActionsStore; + try { + store = new ActionsStore(file); + expect(await store.all()).toEqual([]); + } finally { + console.error = origError; + } + expect(errors.some((m) => m.includes("could not be read"))).toBe(true); + const quarantined = quarantinedIn(dir, "actions.json"); + expect(quarantined.length).toBe(1); + expect(statSync(join(dir, quarantined[0])).isDirectory()).toBe(true); + expect(existsSync(file)).toBe(false); + await store!.add(newAction()); + const persisted = JSON.parse(readFileSync(file, "utf8")); + expect(persisted.version).toBe(1); + expect(persisted.actions.length).toBe(1); + expect(quarantinedIn(dir, "actions.json").length).toBe(1); + }); + test("valid file -> loads unchanged, no quarantine", async () => { const dir = mkdtempSync(join(tmpdir(), "agg-actions-valid-")); const file = join(dir, "actions.json"); @@ -114,6 +140,32 @@ describe("comments store corruption handling (#1452)", () => { expect(quarantinedIn(dir, "comments.json").length).toBe(1); }); + test("unreadable store (directory in place of file) -> loud quarantine, empty store, fresh persist leaves the quarantine untouched", async () => { + const dir = mkdtempSync(join(tmpdir(), "agg-comments-unreadable-")); + const file = join(dir, "comments.json"); + mkdirSync(file); // readFile on a directory fails with EISDIR — a non-ENOENT read error (#2980) + const errors: string[] = []; + const origError = console.error; + console.error = (...args: unknown[]) => { errors.push(args.map(String).join(" ")); }; + let store: CommentsStore; + try { + store = new CommentsStore(file); + expect(await store.all()).toEqual([]); + } finally { + console.error = origError; + } + expect(errors.some((m) => m.includes("could not be read"))).toBe(true); + const quarantined = quarantinedIn(dir, "comments.json"); + expect(quarantined.length).toBe(1); + expect(statSync(join(dir, quarantined[0])).isDirectory()).toBe(true); + expect(existsSync(file)).toBe(false); + await store!.add({ objectRef: "gh:developing-today/code#2980", kind: "note", author: drew, body: "fresh start" }); + const persisted = JSON.parse(readFileSync(file, "utf8")); + expect(persisted.version).toBe(1); + expect(persisted.comments.length).toBe(1); + expect(quarantinedIn(dir, "comments.json").length).toBe(1); + }); + test("valid file -> loads unchanged, no quarantine", async () => { const dir = mkdtempSync(join(tmpdir(), "agg-comments-valid-")); const file = join(dir, "comments.json");