Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions pm-stack/aggregator/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 27 additions & 2 deletions pm-stack/aggregator/src/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
54 changes: 53 additions & 1 deletion pm-stack/aggregator/test/store-corrupt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// <file>.corrupt-<timestamp> 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";
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down