Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(outbox): timestamped non-overwriting quarantine and fail-closed quarantine failures (#2284 review)
koeria7987bab0cb8f
diff --git a/apps/desktop/electron/main/persistence-outbox.ts b/apps/desktop/electron/main/persistence-outbox.tsindex 78693e7e7..72aa31d96 100644--- a/apps/desktop/electron/main/persistence-outbox.ts+++ b/apps/desktop/electron/main/persistence-outbox.ts@@ -1,4 +1,4 @@-import { mkdir, readFile, rename, writeFile } from "node:fs/promises";+import { link, mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";import { dirname, join } from "node:path";import { decodeBinaryStorage, encodeBinaryStorage } from "@pi-desktop/host-runtime";import { ErrorCodes } from "@pi-desktop/shared";@@ -303,16 +303,61 @@ export class PersistenceOutbox {}private async quarantineCorruptFile(path: string, error: unknown): Promise<void> {- const quarantinePath = `${path}.corrupt`;- try {- await rename(path, quarantinePath);- } catch (renameError) {- this.logger("warn", "session persistence outbox corrupt file could not be quarantined", String(renameError));+ // Never reuse a fixed `.corrupt` name: a second corruption would rename+ // over the first quarantine and destroy its evidence (#2284 review).+ // Timestamp the quarantine and fall back to incrementing suffixes. `link`+ // is refuse-if-exists, so concurrent loads can never silently overwrite+ // one another the way `rename` onto an existing target would.+ const stamp = new Date(this.now()).toISOString().replace(/[:.]/g, "-");+ for (let attempt = 0; attempt < 1000; attempt++) {+ const quarantinePath = attempt === 0 ? `${path}.corrupt.${stamp}` : `${path}.corrupt.${stamp}.${attempt}`;+ try {+ await link(path, quarantinePath);+ } catch (linkError: unknown) {+ const code = linkError instanceof Error && "code" in linkError ? String(linkError.code) : "";+ if (code === "EEXIST") continue;+ if (code === "ENOENT") {+ // The original vanished between the read and the link — almost+ // always a concurrent load that already quarantined (or removed)+ // it. There are no un-backed-up bytes left to protect, so an empty+ // queue cannot overwrite anything.+ this.logger(+ "warn",+ "session persistence outbox corrupt file vanished before quarantine; nothing to protect, starting with an empty queue",+ { file: path, data: String(linkError) },+ );+ return;+ }+ // Fail closed: the corrupted bytes are still in place but could not be+ // backed up (e.g. EISDIR/EACCES). Continuing here would boot with an+ // empty queue and the next persist would overwrite the un-backed-up+ // file. Surface the original parse error so the boot fails exactly+ // like before the resilient-load change and nothing is written over it.+ this.logger(+ "error",+ "session persistence outbox corrupt file could not be quarantined; failing boot to protect the original bytes",+ { file: path, quarantinePath, data: String(linkError) },+ );+ throw error;+ }+ // The corrupted bytes are now duplicated into the quarantine; only then+ // remove the original so the app can start writing a fresh queue.+ try {+ await unlink(path);+ } catch (unlinkError: unknown) {+ this.logger(+ "warn",+ "session persistence outbox corrupt file quarantined but the original could not be removed",+ { file: path, quarantinePath, data: String(unlinkError) },+ );+ }+ this.logger("warn", "session persistence outbox corrupt file quarantined, starting with an empty queue", {+ file: quarantinePath,+ data: String(error),+ });+ return;}- this.logger("warn", "session persistence outbox corrupt file quarantined, starting with an empty queue", {- file: quarantinePath,- data: String(error),- });+ throw error;}private async loadEntries(): Promise<void> {