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([issue-718]): key useCanonPatch PATCH + staleness guard on universe.id
[._.]/ Adam Eivy52607c763874
diff --git a/client/src/components/pipeline/stages/NounsStage.jsx b/client/src/components/pipeline/stages/NounsStage.jsxindex e1326b035c..0cad8d0c62 100644--- a/client/src/components/pipeline/stages/NounsStage.jsx+++ b/client/src/components/pipeline/stages/NounsStage.jsx@@ -376,7 +376,7 @@ export default function NounsStage({ issue, series, onStageUpdate }) {// setting intExt + timeOfDay chips). Optimistic — UI updates before the// server roundtrip so chip clicks feel instant.const { patchEntry: handlePatchEntry } = useCanonPatch({- universe, universeId: series?.universeId, apply: setUniverse, mountedRef,+ universe, apply: setUniverse, mountedRef,});const handleRefFailed = useCallback((entryId, errMsg) => {diff --git a/client/src/components/universe/UniverseCanonSection.jsx b/client/src/components/universe/UniverseCanonSection.jsxindex fd62e2b552..df5f6daa83 100644--- a/client/src/components/universe/UniverseCanonSection.jsx+++ b/client/src/components/universe/UniverseCanonSection.jsx@@ -595,7 +595,7 @@ export default function UniverseCanonSection({// edits). Optimistic — UI updates before the server roundtrip so chip// clicks feel instant.const { patchEntry: handlePatchEntry } = useCanonPatch({- universe, universeId, apply: onUniverseChange, mountedRef,+ universe, apply: onUniverseChange, mountedRef,});if (!universe) return null;diff --git a/client/src/hooks/README.md b/client/src/hooks/README.mdindex 3b9683bb91..9e87645b18 100644--- a/client/src/hooks/README.md+++ b/client/src/hooks/README.md@@ -106,7 +106,7 @@ grep -i "what you want to do" client/src/hooks/README.md|---|---|---|| `useAppDeploy` | Stream `deploy.sh` output via Socket.IO. | App deployment surfaces only. || `useAppOperation` | Socket-based app ops (update, standardize) with step tracking. | App operations UI. |-| `useCanonPatch` | Optimistic canon-entry patch: rebuild the kind list with one entry mutated, apply locally, PATCH the universe, re-apply the server copy (with a captured-id staleness guard). `apply` is `setUniverse` or `onUniverseChange`. | Inline canon-field edits on a universe (UniverseCanonSection, NounsStage). Don't re-roll the optimistic-then-confirm dance. |+| `useCanonPatch` | Optimistic canon-entry patch: rebuild the kind list with one entry mutated, apply locally, PATCH the universe, re-apply the server copy. Targets + staleness-guards on the loaded record's `universe.id` so a mid-flight universe swap can't cross-PATCH or resurrect stale state. `apply` is `setUniverse` or `onUniverseChange`. | Inline canon-field edits on a universe (UniverseCanonSection, NounsStage). Don't re-roll the optimistic-then-confirm dance. || `useCityAudio` | CyberCity ambient audio. | CyberCity only. || `useCityData` | CyberCity environment data + physics. | CyberCity only. || `useCitySettings` | CyberCity quality presets + persistence. | CyberCity only. |diff --git a/client/src/hooks/useCanonPatch.js b/client/src/hooks/useCanonPatch.jsindex e51334c03e..3b336dea56 100644--- a/client/src/hooks/useCanonPatch.js+++ b/client/src/hooks/useCanonPatch.js@@ -11,20 +11,26 @@ import { updateUniverse } from '../services/apiUniverseBuilder';// `universe` via `useUniverse`) or `onUniverseChange` (when a parent owns the// draft). It's called with the full next universe object.//-// Staleness guard: the captured `universeId` is compared against the live id on-// completion, so a slow PATCH from a previous universe can't repopulate state-// after the caller navigates to a different world.+// The PATCH targets and is staleness-guarded on the LOADED record's `id`+// (`universe.id`), NOT a separately-passed prop. NounsStage drives `universe`+// from `useUniverse(series.universeId)`, which can briefly expose the previous+// universe while a series/universe switch is in flight — keying off the record+// we actually built the list from means we never send one universe's canon to+// another's id, and a re-apply that lands after the loaded universe swapped (or+// cleared) is dropped instead of resurrecting stale state.//// Usage:-// const { patchEntry } = useCanonPatch({ universe, universeId, apply: setUniverse, mountedRef });+// const { patchEntry } = useCanonPatch({ universe, apply: setUniverse, mountedRef });// <Card onPatchEntry={(entryId, patch) => patchEntry(kind, entryId, patch)} />-export function useCanonPatch({ universe, universeId, apply, mountedRef }) {- const currentUniverseIdRef = useRef(universeId);- useEffect(() => { currentUniverseIdRef.current = universeId; }, [universeId]);+export function useCanonPatch({ universe, apply, mountedRef }) {+ // Live mirror of the loaded universe's id so a PATCH that resolves after the+ // record swapped can compare against the current id and bow out.+ const currentUniverseIdRef = useRef(universe?.id);+ useEffect(() => { currentUniverseIdRef.current = universe?.id; }, [universe?.id]);const patchEntry = useCallback(async (kind, entryId, patch) => {if (!universe || !patch || typeof patch !== 'object') return;- const capturedId = universeId;+ const capturedId = universe.id;const kindKey = kind.key;const list = (universe[kindKey] || []).map((e) =>e.id === entryId ? { ...e, ...patch } : e@@ -33,12 +39,12 @@ export function useCanonPatch({ universe, universeId, apply, mountedRef }) {// `{ silent: true }` because the .catch below owns the failure toast —// without it the apiCore request() helper fires a second, duplicate one// (CLAUDE.md "Custom catch ⇒ silent: true").- const updated = await updateUniverse(universeId, { [kindKey]: list }, { silent: true })+ const updated = await updateUniverse(capturedId, { [kindKey]: list }, { silent: true }).catch((err) => { toast.error(`Save failed: ${err.message}`); return null; });if (updated && mountedRef.current && currentUniverseIdRef.current === capturedId) {apply(updated);}- }, [universe, universeId, apply, mountedRef]);+ }, [universe, apply, mountedRef]);return { patchEntry };}diff --git a/client/src/hooks/useCanonPatch.test.jsx b/client/src/hooks/useCanonPatch.test.jsxindex 30c8b638be..eb849b3901 100644--- a/client/src/hooks/useCanonPatch.test.jsx+++ b/client/src/hooks/useCanonPatch.test.jsx@@ -24,7 +24,6 @@ function setup(overrides = {}) {const mountedRef = { current: true };const props = {universe: baseUniverse,- universeId: 'u1',apply,mountedRef,...overrides,@@ -108,18 +107,21 @@ describe('useCanonPatch', () => {expect(apply).toHaveBeenCalledTimes(1); // server copy NOT re-applied});- it('does not re-apply when the universe id changed mid-flight', async () => {+ it('PATCHes the loaded record id and drops a re-apply after the loaded universe swaps', async () => {let resolveUpdate;updateUniverse.mockReturnValue(new Promise((res) => { resolveUpdate = res; }));const apply = vi.fn();const mountedRef = { current: true };- const props = { universe: baseUniverse, universeId: 'u1', apply, mountedRef };+ const props = { universe: baseUniverse, apply, mountedRef };const { result, rerender } = renderHook((p) => useCanonPatch(p), { initialProps: props });let pending;act(() => { pending = result.current.patchEntry(KIND, 'c1', { intExt: 'EXT' }); });- // Navigate to a different world before the PATCH settles.- rerender({ ...props, universeId: 'u2' });+ // PATCH targets the loaded record's id, not a separately-passed prop.+ expect(updateUniverse).toHaveBeenCalledWith('u1', expect.any(Object), { silent: true });+ // The loaded universe swaps to a different world before the PATCH settles+ // (e.g. useUniverse refetched for a new series.universeId).+ rerender({ ...props, universe: { id: 'u2', characters: [] } });await waitFor(() => {}); // flush the id-sync effectawait act(async () => { resolveUpdate({ id: 'u1', characters: [] }); await pending; });expect(apply).toHaveBeenCalledTimes(1); // stale server copy dropped