Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(storage): keep the Windows install lock when removing old backups

chengzhi-cd815ef15bb13
diff --git a/apps/desktop/electron/main/storage/files.ts b/apps/desktop/electron/main/storage/files.ts
index 32f1fdd1e..08056a804 100644
--- a/apps/desktop/electron/main/storage/files.ts
+++ b/apps/desktop/electron/main/storage/files.ts
@@ -10,7 +10,14 @@ import { STORAGE_PREFERENCE_FILE, type StorageRoots } from "./preferences";
const OWNER_FILE = ".pi-storage-owner.json";
export const DATA_CACHE_PATHS = ["cache", "plugins/cache/download", "plugins/cache/backup", "openable-attachments"];
const BROWSER_CACHE_PATHS = ["Cache", "Code Cache", "GPUCache", "DawnCache", "ShaderCache", "GrShaderCache", "GraphiteDawnCache"];
-const excluded = (name: string) => name === STORAGE_PREFERENCE_FILE || name.startsWith("Singleton") || /^\.storage-.*\.tmp$/.test(name);
+// Chromium names the single-instance lock `SingletonLock` on Linux and macOS
+// but `lockfile` on Windows. Backup cleanup runs while this process still holds
+// that lock, and Windows refuses to unlink a file this process opened without
+// delete sharing, so an omitted Windows name aborts the whole maintenance run
+// with EBUSY instead of skipping one regenerated file. The migration inventory
+// skips the same names: the lock belongs to the installation anchor, not to the
+// copied `browser/` root.
+const excluded = (name: string) => name === STORAGE_PREFERENCE_FILE || name.startsWith("Singleton") || name === "lockfile" || /^\.storage-.*\.tmp$/.test(name);
export function contains(parent: string, child: string): boolean {
const part = relative(parent, child);
return !part || (!part.startsWith(`..${sep}`) && part !== ".." && !isAbsolute(part));
diff --git a/apps/desktop/test/storage-maintenance.test.mjs b/apps/desktop/test/storage-maintenance.test.mjs
index b023ee75e..3bdb5cf1c 100644
--- a/apps/desktop/test/storage-maintenance.test.mjs
+++ b/apps/desktop/test/storage-maintenance.test.mjs
@@ -67,6 +67,13 @@ async function exists(path) {
catch (error) { if (error.code === "ENOENT") return false; throw error; }
}
const hash = (buffer) => createHash("sha256").update(buffer).digest("hex");
+// Chromium names the single-instance lock `lockfile` on Windows and
+// `SingletonLock` elsewhere, so a real installation anchor holds whichever name
+// its platform uses. Both are covered here on every platform: the cleanup and
+// the migration inventory must skip either name regardless of the host, and the
+// unit-test lane only runs on Linux. Fixturing the POSIX name alone is what let
+// the Windows delete failure ship.
+const installLockNames = ["lockfile", "SingletonLock"];
const migrate = (value, overrides = {}) => storage.migrateFiles({ source: value.roots, target: value.target, anchor: value.anchor, id: randomUUID(), progress: () => {}, relocate: async () => {}, ...overrides });
function resetHarness(anchor) {
harness.anchor = anchor; harness.paths = []; harness.windows = []; harness.errors = [];
@@ -86,7 +93,7 @@ test("cold migration copies real data, verifies hashes, preserves secret permiss
await chmod(value.roots.data, 0o700);
await put(join(value.roots.browser, "Local Storage/leveldb/test.ldb"), "browser state");
await put(join(value.roots.browser, storage.STORAGE_PREFERENCE_FILE), "bootstrap pointer");
- await put(join(value.roots.browser, "SingletonLock"), "installation lock");
+ for (const name of installLockNames) await put(join(value.roots.browser, name), "installation lock");
const events = []; const relocations = [];
const next = await migrate(value, { progress: (event) => events.push(event), relocate: async (...roots) => relocations.push(roots) });
assert.equal(hash(await readFile(join(next.data, "sessions/chat.jsonl"))), hash(payload));
@@ -94,7 +101,7 @@ test("cold migration copies real data, verifies hashes, preserves secret permiss
assert.equal((await lstat(next.data)).mode & 0o777, 0o700);
assert.equal(await readFile(join(next.browser, "Local Storage/leveldb/test.ldb"), "utf8"), "browser state");
assert.equal(await exists(join(next.browser, storage.STORAGE_PREFERENCE_FILE)), false);
- assert.equal(await exists(join(next.browser, "SingletonLock")), false);
+ for (const name of installLockNames) assert.equal(await exists(join(next.browser, name)), false);
assert.equal(await readFile(join(value.roots.data, "sessions/chat.jsonl")).then(hash), hash(payload));
assert.deepEqual(relocations, [[value.roots.data, next.data]]);
assert.deepEqual([...new Set(events.map((event) => event.stage))], ["scanning", "copying", "verifying", "relocating", "complete"]);
@@ -265,13 +272,13 @@ test("backup removal protects active storage and the installation bootstrap anch
await put(join(active.data, "session"), "active");
await put(join(value.roots.data, "old-session"), "old");
await put(join(value.anchor, storage.STORAGE_PREFERENCE_FILE), "pointer");
- await put(join(value.anchor, "SingletonLock"), "lock");
+ for (const name of installLockNames) await put(join(value.anchor, name), "lock");
await put(join(value.anchor, "Local Storage/old"), "old browser");
await assert.rejects(storage.removeBackups([active], active, value.anchor), /active storage/);
await storage.removeBackups([value.roots], active, value.anchor);
assert.equal(await exists(value.roots.data), false);
assert.equal(await readFile(join(value.anchor, storage.STORAGE_PREFERENCE_FILE), "utf8"), "pointer");
- assert.equal(await readFile(join(value.anchor, "SingletonLock"), "utf8"), "lock");
+ for (const name of installLockNames) assert.equal(await readFile(join(value.anchor, name), "utf8"), "lock");
assert.equal(await exists(join(value.anchor, "Local Storage/old")), false);
assert.equal(await readFile(join(active.data, "session"), "utf8"), "active");
});