Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (copilot): bump switch generation at the switch, not before the save

Adam Eivy96eea5a09814
diff --git a/client/src/pages/Dashboard.jsx b/client/src/pages/Dashboard.jsx
index 1683231d17..5d23b9cad3 100644
--- a/client/src/pages/Dashboard.jsx
+++ b/client/src/pages/Dashboard.jsx
@@ -264,13 +264,18 @@ export default function Dashboard() {
};
const duplicateLayout = async ({ id, name, widgets, activateWindow }) => {
- const switchGen = ++switchGenerationRef.current;
// New layouts inherit the current renderGrid so "Save as new…" from a
// visually-arranged dashboard captures what the user actually sees.
const sourceGrid = renderGrid && renderGrid.length > 0 ? renderGrid : synthesizeGrid(widgets);
const grid = reconcileGrid(sourceGrid, widgets);
const result = await api.saveDashboardLayout(id, { name, widgets, grid, activateWindow });
setLayouts(result.layouts);
+ // Bump the switch generation only now — AFTER the save resolved and right
+ // before the actual active-layout switch. Bumping at function entry would
+ // let a still-in-flight save prematurely supersede (and thus suppress the
+ // failure revert of) an unrelated in-flight selectLayout that fails in the
+ // meantime — and if the save itself rejects, no switch happens at all.
+ const switchGen = ++switchGenerationRef.current;
setActiveLayoutId(id);
// Lock in for the day so a window-driven auto-switch on the next mount
// doesn't bump the user off the brand-new layout they just created.
diff --git a/client/src/pages/Dashboard.layoutRevert.test.js b/client/src/pages/Dashboard.layoutRevert.test.js
index 451666078a..20f543d44c 100644
--- a/client/src/pages/Dashboard.layoutRevert.test.js
+++ b/client/src/pages/Dashboard.layoutRevert.test.js
@@ -39,8 +39,25 @@ function createLayoutSwitcher(serverActiveId) {
});
};
+ // Mirrors duplicateLayout: a save step (savePut) precedes the active-layout
+ // switch. The generation must bump only AFTER savePut resolves — at the real
+ // switch — so a save in flight (or one that rejects) can't prematurely
+ // supersede an in-flight selectLayout's failure revert.
+ const duplicateLayout = (id, savePut, setActivePut) =>
+ savePut.then(() => {
+ const myGen = ++generation.current;
+ displayed = id;
+ return setActivePut
+ .then(() => { serverConfirmed.current = id; })
+ .catch(() => {
+ if (generation.current !== myGen) return;
+ displayed = displayed === id ? serverConfirmed.current : displayed;
+ });
+ });
+
return {
selectLayout,
+ duplicateLayout,
get displayed() { return displayed; },
get confirmed() { return serverConfirmed.current; },
};
@@ -124,4 +141,23 @@ describe('Dashboard active-layout revert', () => {
expect(sw.displayed).toBe('B'); // NOT reverted to A
expect(sw.confirmed).toBe('B');
});
+
+ it('a duplicate-layout save in flight does not prematurely supersede an in-flight switch failure', async () => {
+ // duplicateLayout saves the new layout BEFORE switching to it, so the
+ // generation must bump only at that later switch — not at function entry.
+ // While the save is in flight, an unrelated selectLayout that fails must
+ // still revert (it's still the latest switch until the duplicate commits).
+ const sw = createLayoutSwitcher('A');
+ let failX;
+ let resolveSave;
+ const pX = sw.selectLayout('X', new Promise((_, reject) => { failX = reject; })); // gen 1, displayed X
+ const pDup = sw.duplicateLayout('D', new Promise((r) => { resolveSave = r; }), Promise.resolve()); // save pending — no gen bump yet
+ failX(new Error('boom')); // X fails while the duplicate's save is still in flight
+ await pX;
+ expect(sw.displayed).toBe('A'); // X's failure still reverts (no premature gen bump)
+ resolveSave(); // duplicate's save resolves → only now does it switch
+ await pDup;
+ expect(sw.displayed).toBe('D');
+ expect(sw.confirmed).toBe('D');
+ });
});