Skip to content

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

fix(storage): keep cold maintenance off the profile being copied

chengzhi-c866837ce6af0
diff --git a/apps/desktop/electron/main/storage/bootstrap.ts b/apps/desktop/electron/main/storage/bootstrap.ts
index 82e5a0be0..9f602297f 100644
--- a/apps/desktop/electron/main/storage/bootstrap.ts
+++ b/apps/desktop/electron/main/storage/bootstrap.ts
@@ -48,7 +48,11 @@ export async function prepareStorage(defaultData: string, overridden: boolean):
return current;
}
const job = preferences.pending;
- // Never open a persistent session against a source being copied/cleaned.
+ // Chromium initializes the default session when the first window is created.
+ // That must not land in the profile this job is about to copy or clean.
+ const maintenanceSession = join(app.getPath("temp"), `pi-desktop-storage-${job.id}`);
+ mkdirSync(maintenanceSession, { recursive: true });
+ app.setPath("sessionData", maintenanceSession);
await app.whenReady();
const copy = catalogs[resolveLocale(job.language)].settings.storage;
const window = new BrowserWindow({ width: 560, height: 330, resizable: false, closable: false,
diff --git a/apps/desktop/test/storage-maintenance.test.mjs b/apps/desktop/test/storage-maintenance.test.mjs
index 9ccbbb900..b023ee75e 100644
--- a/apps/desktop/test/storage-maintenance.test.mjs
+++ b/apps/desktop/test/storage-maintenance.test.mjs
@@ -27,7 +27,7 @@ const result = await build({
const exitSignal = new Error("test process exited");
const harness = {
anchor: "", selection: null, paths: [], windows: [], errors: [], restarts: 0, hostError: null,
- app: { getPath: () => harness.anchor, getLocale: () => "en", setPath: (...value) => harness.paths.push(value), whenReady: async () => {}, relaunch: () => { harness.restarts++; }, exit: () => { throw exitSignal; } },
+ app: { getPath: (name) => name === "temp" ? maintenanceTemp : harness.anchor, getLocale: () => "en", setPath: (...value) => harness.paths.push(value), whenReady: async () => {}, relaunch: () => { harness.restarts++; }, exit: () => { throw exitSignal; } },
dialog: { showOpenDialog: async () => ({ canceled: harness.selection === null, filePaths: [harness.selection] }), showMessageBox: async (...value) => { harness.errors.push(value); return { response: 0 }; } },
BrowserWindow: class {
constructor(options) { this.options = options; this.scripts = []; harness.windows.push(this); this.webContents = { setWindowOpenHandler: () => {}, on: () => {}, executeJavaScript: async (script) => { this.scripts.push(script); } }; }
@@ -39,7 +39,11 @@ const harness = {
};
globalThis.__storageHarness = harness;
const bundleDirectory = await mkdtemp(join(tmpdir(), "pi-storage-bundle-"));
-after(() => rm(bundleDirectory, { recursive: true, force: true }));
+const maintenanceTemp = await mkdtemp(join(tmpdir(), "pi-storage-session-"));
+after(() => Promise.all([
+ rm(bundleDirectory, { recursive: true, force: true }),
+ rm(maintenanceTemp, { recursive: true, force: true }),
+]));
const bundleFile = join(bundleDirectory, "storage-maintenance.mjs");
await writeFile(bundleFile, result.outputFiles[0].text);
const storage = await import(pathToFileURL(bundleFile).href);
@@ -325,6 +329,28 @@ test("setting the path schedules a cold restart, migration switches the pointer
assert.ok(harness.windows[0].scripts.some((script) => script.includes(storage.catalogs.en.settings.storage.stages.relocating)));
});
+test("pending maintenance redirects Chromium away from the profile being copied", { timeout: 60_000 }, async (t) => {
+ const value = await fixture(t); resetHarness(value.anchor);
+ const leveldb = join(value.roots.browser, "Local Storage/leveldb/LOG");
+ await put(leveldb, "leveldb log");
+ const id = randomUUID();
+ storage.writeStoragePreferences(join(value.anchor, storage.STORAGE_PREFERENCE_FILE), {
+ version: 1, roots: value.roots, backups: [],
+ pending: { id, kind: "migrate", target: value.target, language: "en" },
+ });
+ // The maintenance window is created at readiness. Chromium initializes its
+ // default session there, so the profile must already point elsewhere.
+ const ready = harness.app.whenReady;
+ t.after(() => { harness.app.whenReady = ready; });
+ harness.app.whenReady = async () => {
+ const redirected = harness.paths.some(([name, path]) => name === "sessionData" && path !== value.roots.browser && path !== value.anchor);
+ if (!redirected) await writeFile(leveldb, "written by the maintenance window session");
+ };
+ await assert.rejects(storage.prepareStorage(value.roots.data, false), (error) => error === exitSignal);
+ assert.equal(await readFile(leveldb, "utf8"), "leveldb log");
+ assert.deepEqual(harness.paths[0], ["sessionData", join(maintenanceTemp, `pi-desktop-storage-${id}`)]);
+});
+
test("bootstrap refuses missing custom storage without silently creating an empty profile", { timeout: 60_000 }, async (t) => {
const value = await fixture(t); resetHarness(value.anchor);
const unavailable = { data: join(value.target, "disconnected-data"), browser: join(value.target, "disconnected-browser") };
diff --git a/docs/spec/03-runtime/04-data-storage.md b/docs/spec/03-runtime/04-data-storage.md
index e9bc11c0b..7411cafb3 100644
--- a/docs/spec/03-runtime/04-data-storage.md
+++ b/docs/spec/03-runtime/04-data-storage.md
@@ -69,8 +69,10 @@ The location is machine-local and never part of cloud configuration sync.
Migration is cold: the accepted settings action journals pending work, then uses
existing ordered shutdown to settle turns/outbox and stop writers. The next launch
-opens only a sandboxed, nonpersistent maintenance window before importing the
-application composition root. It inventories bytes/files, checks free space, streams
+points Chromium `sessionData` at a temporary directory, then opens only a
+sandboxed, nonpersistent maintenance window before importing the application
+composition root. The default session initializes with that first window, so it
+must not be inside a profile the job is about to copy or clean. It inventories bytes/files, checks free space, streams
the copy, preserves permissions and internal/external links, and SHA-256 verifies
both source and copied files. An interrupted copy may be retried only with its
matching ownership marker; nonempty/unrelated destinations and overlapping roots