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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix([dashboard-active-layout-revert-tracks-server-confirmed-id]): revert failed layout switch to server-confirmed id
Adam Eivy3e85bcc5f97a
diff --git a/client/src/pages/Dashboard.jsx b/client/src/pages/Dashboard.jsxindex 828f17e00b..46ad4f9e8d 100644--- a/client/src/pages/Dashboard.jsx+++ b/client/src/pages/Dashboard.jsx@@ -27,6 +27,15 @@ export default function Dashboard() {const [layoutsLoading, setLayoutsLoading] = useState(true);const [layoutLimits, setLayoutLimits] = useState(null);const [activeLayoutId, setActiveLayoutId] = useState(null);+ // Last active-layout id the SERVER confirmed (initial GET, a successful+ // setActive PUT, or a delete response). Failed optimistic switches revert+ // to this — never to a `previousId` snapshot, which can itself be an+ // optimistic-but-uncommitted id when two switches fail back-to-back (the+ // 2nd switch's `previousId` is the 1st's never-landed value). A ref, not+ // state: it's read inside async `.catch` revert callbacks, so it must+ // always reflect current truth without stale-closure capture, and it+ // never needs to trigger a re-render on its own.+ const serverConfirmedLayoutIdRef = useRef(null);const [editorOpen, setEditorOpen] = useState(false);// Grid edit mode is local — entered via the "Arrange" button. Holds an// in-flight grid snapshot the user can Save/Cancel without touching the@@ -71,6 +80,9 @@ export default function Dashboard() {.then((data) => {if (cancelled) return;setLayouts(data.layouts);+ // The server's stored active id is the confirmed baseline until/unless+ // the auto-switch PUT below lands.+ serverConfirmedLayoutIdRef.current = data.activeLayoutId;const desiredActiveId = pickActiveLayoutId(data.activeLayoutId, data.layouts, autoSwitchedRef.current);setActiveLayoutId(desiredActiveId);if (desiredActiveId === data.activeLayoutId) {@@ -81,7 +93,11 @@ export default function Dashboard() {// mark "auto-switched" after the PUT settles so a failing server// gets retried on the next mount.api.setActiveDashboardLayout(desiredActiveId)- .then(() => { if (!cancelled) autoSwitchedRef.current = true; })+ .then(() => {+ if (cancelled) return;+ autoSwitchedRef.current = true;+ serverConfirmedLayoutIdRef.current = desiredActiveId;+ }).catch(() => {if (cancelled) return;setActiveLayoutId((current) => (current === desiredActiveId ? data.activeLayoutId : current));@@ -193,15 +209,19 @@ export default function Dashboard() {};const selectLayout = async (id) => {- const previousId = activeLayoutId;setActiveLayoutId(id);recordManualLayoutPick(id);// Server-side write tail serializes against any in-flight auto-window- // PUT so this write always lands after it. Revert via functional- // setState so a later selection isn't clobbered.- await api.setActiveDashboardLayout(id).catch(() => {- setActiveLayoutId((current) => (current === id ? previousId : current));- });+ // PUT so this write always lands after it. On success the id is now the+ // server-confirmed baseline; on failure revert (via functional setState+ // so a later selection isn't clobbered) to that baseline — not to a+ // local snapshot, which after a prior failed switch may be an id the+ // server never accepted.+ await api.setActiveDashboardLayout(id)+ .then(() => { serverConfirmedLayoutIdRef.current = id; })+ .catch(() => {+ setActiveLayoutId((current) => (current === id ? serverConfirmedLayoutIdRef.current : current));+ });};// Preserve the existing grid on widget add/remove so positional edits@@ -219,7 +239,6 @@ export default function Dashboard() {};const duplicateLayout = async ({ id, name, widgets, activateWindow }) => {- const previousId = activeLayoutId;// 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);@@ -231,16 +250,23 @@ export default function Dashboard() {// doesn't bump the user off the brand-new layout they just created.recordManualLayoutPick(id);// Server-side write tail serializes against any in-flight auto-window- // PUT. Revert via functional setState so a later selection isn't clobbered.- await api.setActiveDashboardLayout(id).catch(() => {- setActiveLayoutId((current) => (current === id ? previousId : current));- });+ // PUT. On success the new layout is the server-confirmed baseline; on+ // failure revert (functional setState) to the prior confirmed baseline+ // rather than a possibly-uncommitted local snapshot.+ await api.setActiveDashboardLayout(id)+ .then(() => { serverConfirmedLayoutIdRef.current = id; })+ .catch(() => {+ setActiveLayoutId((current) => (current === id ? serverConfirmedLayoutIdRef.current : current));+ });};const deleteLayoutById = async (id) => {const result = await api.deleteDashboardLayout(id);setLayouts(result.layouts);setActiveLayoutId(result.activeLayoutId);+ // The server chose the post-delete active layout — it's now the+ // confirmed baseline a later failed switch should revert to.+ serverConfirmedLayoutIdRef.current = result.activeLayoutId;toast.success('Layout deleted');};diff --git a/client/src/pages/Dashboard.layoutRevert.test.js b/client/src/pages/Dashboard.layoutRevert.test.jsnew file mode 100644index 0000000000..eb7e771279--- /dev/null+++ b/client/src/pages/Dashboard.layoutRevert.test.js@@ -0,0 +1,75 @@+import { describe, it, expect } from 'vitest';++// Faithful inline model of Dashboard.jsx's active-layout switch+revert state+// machine (selectLayout + the serverConfirmedLayoutIdRef). The real Dashboard+// page renders lazy Suspense widgets and a live socket, which makes the+// optimistic-set / async-revert timing hard to assert deterministically — so+// this models just the revert logic. Keep it in sync with Dashboard.jsx:+// - optimistic setActiveLayoutId(id) up front+// - on PUT success: serverConfirmedLayoutIdRef.current = id+// - on PUT failure: revert via functional setState, current === id ? confirmed : current+function createLayoutSwitcher(serverActiveId) {+ let displayed = serverActiveId; // activeLayoutId+ const serverConfirmed = { current: serverActiveId }; // serverConfirmedLayoutIdRef++ // Mirrors: await api.setActiveDashboardLayout(id).then(...).catch(...)+ // `put` is a promise the caller resolves/rejects to stand in for the PUT.+ const selectLayout = (id, put) => {+ displayed = id; // optimistic+ return put+ .then(() => { serverConfirmed.current = id; })+ .catch(() => {+ // functional setState — only revert if still showing the failed id+ displayed = displayed === id ? serverConfirmed.current : displayed;+ });+ };++ return {+ selectLayout,+ get displayed() { return displayed; },+ get confirmed() { return serverConfirmed.current; },+ };+}++describe('Dashboard active-layout revert', () => {+ it('reverts to the server-confirmed id on a single failed switch', async () => {+ const sw = createLayoutSwitcher('A');+ await sw.selectLayout('B', Promise.reject(new Error('boom')));+ expect(sw.displayed).toBe('A');+ expect(sw.confirmed).toBe('A');+ });++ it('reverts to the server-confirmed id (A) after TWO consecutive failed switches — not the never-committed intermediate id (B)', async () => {+ // The bug: selectLayout used a per-call `previousId` snapshot. The 2nd+ // switch's previousId was 'B' (the 1st switch's optimistic-but-uncommitted+ // value), so a double failure snapped the UI to 'B' — a layout the server+ // never accepted. Tracking the last *server-confirmed* id fixes it.+ const sw = createLayoutSwitcher('A');+ const pB = sw.selectLayout('B', Promise.reject(new Error('boom'))); // displayed -> B+ const pC = sw.selectLayout('C', Promise.reject(new Error('boom'))); // displayed -> C+ await Promise.allSettled([pB, pC]);+ expect(sw.displayed).toBe('A'); // server truth, not the orphaned 'B'+ expect(sw.confirmed).toBe('A');+ });++ it('advances the confirmed baseline on a successful switch, then reverts to it on a later failure', async () => {+ const sw = createLayoutSwitcher('A');+ await sw.selectLayout('B', Promise.resolve()); // server now active = B+ expect(sw.confirmed).toBe('B');+ await sw.selectLayout('C', Promise.reject(new Error('boom')));+ expect(sw.displayed).toBe('B'); // reverts to the now-confirmed B, not A+ expect(sw.confirmed).toBe('B');+ });++ it('a stale failure does not clobber a later in-flight selection', async () => {+ const sw = createLayoutSwitcher('A');+ let failB;+ const pB = sw.selectLayout('B', new Promise((_, reject) => { failB = reject; }));+ const pC = sw.selectLayout('C', Promise.resolve()); // C selected + confirmed first+ await pC;+ failB(new Error('boom')); // B's PUT now fails, but user is on C+ await pB;+ expect(sw.displayed).toBe('C'); // functional-setState guard preserves C+ expect(sw.confirmed).toBe('C');+ });+});