A desktop coding agent with free access to frontier models.
TypeScript
fix(desktop): workspace list changes never undo each other
Samrat Talukdarce74c635dbf8
diff --git a/apps/desktop/electron/workspace-store.mjs b/apps/desktop/electron/workspace-store.mjsindex dd9517ee..f86405b9 100644--- a/apps/desktop/electron/workspace-store.mjs+++ b/apps/desktop/electron/workspace-store.mjs@@ -79,13 +79,36 @@ function parseFirstJsonObject(raw) {return { ok: false, value: null };}+const RENAME_RETRY_DELAYS_MS = [50, 150, 400, 1_000];++// Windows refuses to replace a file another process holds open for a moment+// (antivirus, indexer, a concurrent reader): EPERM, EACCES or EBUSY. Those+// clear on their own, and a lost write here brings a removed workspace back.+async function renameWithRetry(from, to) {+ for (let attempt = 0; ; attempt += 1) {+ try {+ await rename(from, to);+ return;+ } catch (error) {+ const delay = RENAME_RETRY_DELAYS_MS[attempt];+ if (delay === undefined || !["EPERM", "EACCES", "EBUSY"].includes(error?.code)) throw error;+ await new Promise((resolve) => setTimeout(resolve, delay));+ }+ }+}+async function writeJsonFileAtomic(outputPath, value) {const content = `${JSON.stringify(value, null, 2)}\n`;JSON.parse(content);await mkdir(path.dirname(outputPath), { recursive: true });const tempPath = `${outputPath}.${process.pid}.${randomBytes(6).toString("hex")}.tmp`;await writeFile(tempPath, content, "utf8");- await rename(tempPath, outputPath);+ try {+ await renameWithRetry(tempPath, outputPath);+ } catch (error) {+ await rm(tempPath, { force: true }).catch(() => undefined);+ throw error;+ }}async function readJsonFile(targetPath, fallback) {@@ -925,10 +948,18 @@ export function createWorkspaceStore({return nextState;}- async function mutateWorkspaceState(mutator) {- const current = await readWorkspaceState();- const next = await mutator({ ...current, workspaces: [...current.workspaces] });- return writeWorkspaceState(next);+ // One read-modify-write at a time: two overlapping ones (a removal and a+ // selection change) would let the later write put back what the earlier+ // one removed.+ let workspaceStateMutations = Promise.resolve();+ function mutateWorkspaceState(mutator) {+ const run = workspaceStateMutations.catch(() => undefined).then(async () => {+ const current = await readWorkspaceState();+ const next = await mutator({ ...current, workspaces: [...current.workspaces] });+ return writeWorkspaceState(next);+ });+ workspaceStateMutations = run;+ return run;}async function bootstrapFirstLaunchWorkspace() {diff --git a/apps/desktop/electron/workspace-store.test.mjs b/apps/desktop/electron/workspace-store.test.mjsindex 5932d845..784d0634 100644--- a/apps/desktop/electron/workspace-store.test.mjs+++ b/apps/desktop/electron/workspace-store.test.mjs@@ -424,6 +424,31 @@ test("forgetting a local workspace removes its recovery token", async () => {assert.equal(tokens.workspaces[retainedWorkspace].token, "retained");});+test("a removal overlapping another workspace change is never undone by it", async () => {+ const root = await mkdtemp(path.join(tmpdir(), "omnirush-workspace-store-"));+ const userData = path.join(root, "userData");+ await mkdir(userData, { recursive: true });+ const workspaces = ["alpha", "beta", "gamma"].map((name) => ({ id: `ws_${name}`, path: path.join(root, name), workspaceType: "local" }));+ await writeFile(path.join(userData, "omnirush-workspaces.json"), JSON.stringify({ selectedId: "ws_alpha", workspaces }), "utf8");+ const store = createWorkspaceStore({+ app: { getPath: (name) => name === "userData" ? userData : root },+ defaultDenBaseUrl: "https://example.test",+ defaultRequireSignin: false,+ forceRequireSignin: false,+ });++ // The UI selects a workspace while the removal of another is still saving.+ await Promise.all([+ store.setSelectedWorkspace("ws_beta"),+ store.forgetWorkspace("ws_gamma"),+ store.setRuntimeActiveWorkspace("ws_beta"),+ ]);++ const saved = JSON.parse(await readFile(path.join(userData, "omnirush-workspaces.json"), "utf8"));+ assert.deepEqual(saved.workspaces.map((workspace) => workspace.id), ["ws_alpha", "ws_beta"]);+ assert.equal(saved.selectedId, "ws_beta");+});+test("desktop bootstrap prefers a newer canonical writtenAt over stale legacy", async () => {await withIsolatedBootstrapStore(async ({ store, canonicalPath, legacyPath }) => {await writeBootstrapConfig(canonicalPath, {