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: key the lock write tail per universe so one stall can't block another (#3290)

[._.]/ Adam Eivy24e158de3b27
diff --git a/client/src/hooks/useUniverseDraft.js b/client/src/hooks/useUniverseDraft.js
index 880fb8ea89..7d0409e901 100644
--- a/client/src/hooks/useUniverseDraft.js
+++ b/client/src/hooks/useUniverseDraft.js
@@ -469,15 +469,20 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {
});
}, [canonDirty]);
- // Serializes lock PATCHes. The server replaces the stored `locked` map
- // wholesale on every arrival, so ORDER is load-bearing: two toggles fired
- // back-to-back that land reversed persist the older map, leaving a field
- // locked on disk while the UI shows it unlocked. Chaining them means the
- // second is not even sent until the first settles. The style-reference
- // mutators need no such tail — they send deltas the server applies in any
- // order (#3109) — which is exactly the difference that makes one necessary
- // here.
- const lockWriteTailRef = useRef(Promise.resolve());
+ // Serializes lock PATCHes, PER UNIVERSE. The server replaces the stored
+ // `locked` map wholesale on every arrival, so ORDER is load-bearing: two
+ // toggles fired back-to-back that land reversed persist the older map,
+ // leaving a field locked on disk while the UI shows it unlocked. Chaining
+ // them means the second is not even sent until the first settles. The
+ // style-reference mutators need no such tail — they send deltas the server
+ // applies in any order (#3109) — which is exactly the difference that makes
+ // one necessary here.
+ //
+ // Keyed by id rather than held as one tail for the hook, because a request
+ // has no timeout: a stalled write for the universe the user just left would
+ // otherwise block every lock on the one they navigated to, indefinitely and
+ // silently (the draft still updates optimistically, so nothing surfaces it).
+ const lockWriteTailsRef = useRef(new Map());
const toggleLock = useCallback((field) => {
if (!WORLD_LOCKABLE_FIELDS.includes(field)) return;
@@ -500,9 +505,10 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {
draftRef.current = { ...current, locked: nextLocked };
setDraft((value) => ({ ...value, locked: nextLocked }));
if (selectedId && current.name?.trim()) {
- lockWriteTailRef.current = lockWriteTailRef.current
+ const tail = lockWriteTailsRef.current.get(selectedId) || Promise.resolve();
+ lockWriteTailsRef.current.set(selectedId, tail
.then(() => updateUniverse(selectedId, { locked: nextLocked }, { silent: true }))
- .catch((error) => toast.error(`Lock save failed: ${error.message}`));
+ .catch((error) => toast.error(`Lock save failed: ${error.message}`)));
}
}, [draft, selectedId]);
diff --git a/client/src/hooks/useUniverseDraft.test.jsx b/client/src/hooks/useUniverseDraft.test.jsx
index c6f3909729..b81937ced0 100644
--- a/client/src/hooks/useUniverseDraft.test.jsx
+++ b/client/src/hooks/useUniverseDraft.test.jsx
@@ -732,6 +732,32 @@ describe('useUniverseDraft', () => {
);
});
+ // The tail is per universe, not per hook. A request has no timeout, so one
+ // stalled write for the universe the user just left must not block every
+ // lock on the one they navigated to — silently, since the draft still
+ // updates optimistically either way.
+ it('does not let a stalled lock write for one universe block another', async () => {
+ apiMocks.getUniverse.mockImplementation(async (id) => (id === 'u2' ? universeTwo : universe));
+ const { result, rerender } = renderSelectable();
+ await waitFor(() => expect(result.current.draft.id).toBe('u1'));
+
+ // u1's lock write never settles.
+ apiMocks.updateUniverse.mockImplementationOnce(() => new Promise(() => {}));
+ await act(async () => { result.current.toggleLock('logline'); });
+ expect(apiMocks.updateUniverse).toHaveBeenCalledTimes(1);
+
+ await act(async () => { rerender({ selectedId: 'u2' }); });
+ await waitFor(() => expect(result.current.draft.id).toBe('u2'));
+ await act(async () => { result.current.toggleLock('premise'); });
+
+ expect(apiMocks.updateUniverse).toHaveBeenCalledTimes(2);
+ expect(apiMocks.updateUniverse).toHaveBeenLastCalledWith(
+ 'u2',
+ { locked: { premise: true } },
+ { silent: true },
+ );
+ });
+
it('accepts the per-influence-list lock keys, not just the bible scalars', async () => {
const { result } = renderDraft();
await waitFor(() => expect(result.current.draft.id).toBe('u1'));