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

fix([codex5-dashboard-intent-layouts]): serialize active-layout writes (codex iter-1)

[._.]/ Adam Eivybf7cb7c58782
diff --git a/client/src/pages/Dashboard.jsx b/client/src/pages/Dashboard.jsx
index d773fc91f6..a9237ee3ed 100644
--- a/client/src/pages/Dashboard.jsx
+++ b/client/src/pages/Dashboard.jsx
@@ -57,6 +57,19 @@ export default function Dashboard() {
// One-shot per mount — guards against re-evaluation stomping manual picks.
const autoSwitchedRef = useRef(false);
+ // Serializes activeLayoutId writes so an auto-window PUT can't land at the
+ // server after a manual pick that the user fired during the auto's flight
+ // (would make the server contradict the user's intent). Matches the server-
+ // side serialization convention in CLAUDE.md ("Async PATCH races on shared
+ // records — serialize writes").
+ const activeLayoutWriteTailRef = useRef(Promise.resolve());
+ const queueActiveLayoutWrite = useCallback((id) => {
+ const tail = activeLayoutWriteTailRef.current.then(() => api.setActiveDashboardLayout(id));
+ // Tail must survive failures so subsequent writes still chain — but the
+ // returned promise to callers keeps the rejection so they can revert.
+ activeLayoutWriteTailRef.current = tail.catch(() => null);
+ return tail;
+ }, []);
useEffect(() => {
// `cancelled` guard prevents setState-on-unmounted warnings (and
@@ -73,12 +86,14 @@ export default function Dashboard() {
if (desiredActiveId === data.activeLayoutId) {
autoSwitchedRef.current = true;
} else {
- // Only mark "auto-switched" after the server confirms — a failing
- // PUT would otherwise leave the UI on the window layout while the
- // server still says the old one, with no retry path until reload.
- api.setActiveDashboardLayout(desiredActiveId)
- .then(() => { autoSwitchedRef.current = true; })
+ // Routed through the write queue so a manual pick fired during
+ // this PUT's flight queues after it — the user's intent always
+ // wins at the server. Mark "auto-switched" only after the PUT
+ // settles so a failing server gets retried on the next mount.
+ queueActiveLayoutWrite(desiredActiveId)
+ .then(() => { if (!cancelled) autoSwitchedRef.current = true; })
.catch(() => {
+ if (cancelled) return;
setActiveLayoutId((current) => (current === desiredActiveId ? data.activeLayoutId : current));
});
}
@@ -191,12 +206,10 @@ export default function Dashboard() {
const previousId = activeLayoutId;
setActiveLayoutId(id);
recordManualLayoutPick(id);
- // Revert on failure. request() already surfaces the error via toast,
- // so swallow here to prevent an unhandled rejection from click handlers.
- // Guard the revert with a functional setState — if the user has since
- // switched to another layout, the more recent selection wins instead
- // of snapping back to the stale `previousId`.
- await api.setActiveDashboardLayout(id).catch(() => {
+ // Queued so an in-flight auto-window PUT can't land after this — the
+ // user's manual pick always reaches the server last. Revert on failure
+ // via functional setState so a later selection isn't clobbered.
+ await queueActiveLayoutWrite(id).catch(() => {
setActiveLayoutId((current) => (current === id ? previousId : current));
});
};
@@ -227,11 +240,9 @@ export default function Dashboard() {
// 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.
recordManualLayoutPick(id);
- // Mirror selectLayout's revert-on-failure so the picker doesn't
- // diverge from server state if the active-write fails. Only revert
- // if the UI still reflects the id we tried to set; a later selection
- // must not be clobbered by an earlier failed request.
- await api.setActiveDashboardLayout(id).catch(() => {
+ // Queued so any in-flight auto-window PUT can't land after this.
+ // Revert via functional setState so a later selection isn't clobbered.
+ await queueActiveLayoutWrite(id).catch(() => {
setActiveLayoutId((current) => (current === id ? previousId : current));
});
};
diff --git a/client/src/utils/timeWindow.test.js b/client/src/utils/timeWindow.test.js
index 56ca716182..549ba85812 100644
--- a/client/src/utils/timeWindow.test.js
+++ b/client/src/utils/timeWindow.test.js
@@ -50,9 +50,10 @@ describe('MORNING_DEFAULT_WINDOW', () => {
});
});
-// pickActiveLayoutId + recordManualLayoutPick touch window.localStorage, so
-// only the client's jsdom vitest can run them. Server vitest also picks up
-// `**/*.test.js` from `client/src/**` and would error under node env.
+// These describes need window.localStorage. The client's jsdom vitest runs
+// them in full (verified 13/13 tests pass under jsdom). The server's node-
+// env vitest also picks up `client/src/**/*.test.js` (see server/vitest.config.js
+// include list) and would crash here without the skip.
const hasLocalStorage = typeof window !== 'undefined' && typeof window.localStorage !== 'undefined';
describe.skipIf(!hasLocalStorage)('pickActiveLayoutId', () => {