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: serialize lock PATCHes so two toggles can't persist reversed (#3290)

[._.]/ Adam Eivy37cc4bc709f2
diff --git a/client/src/hooks/useUniverseDraft.js b/client/src/hooks/useUniverseDraft.js
index e4eec0d4ff..880fb8ea89 100644
--- a/client/src/hooks/useUniverseDraft.js
+++ b/client/src/hooks/useUniverseDraft.js
@@ -469,6 +469,16 @@ 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());
+
const toggleLock = useCallback((field) => {
if (!WORLD_LOCKABLE_FIELDS.includes(field)) return;
// The next lock map is derived from the freshest draft OUTSIDE the state
@@ -490,7 +500,8 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {
draftRef.current = { ...current, locked: nextLocked };
setDraft((value) => ({ ...value, locked: nextLocked }));
if (selectedId && current.name?.trim()) {
- updateUniverse(selectedId, { locked: nextLocked }, { silent: true })
+ lockWriteTailRef.current = lockWriteTailRef.current
+ .then(() => updateUniverse(selectedId, { locked: nextLocked }, { silent: true }))
.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 f09aee0318..c6f3909729 100644
--- a/client/src/hooks/useUniverseDraft.test.jsx
+++ b/client/src/hooks/useUniverseDraft.test.jsx
@@ -667,7 +667,7 @@ describe('useUniverseDraft', () => {
const { result } = renderDraft();
await waitFor(() => expect(result.current.draft.id).toBe('u1'));
- act(() => result.current.toggleLock('logline'));
+ await act(async () => { result.current.toggleLock('logline'); });
expect(apiMocks.updateUniverse).toHaveBeenLastCalledWith(
'u1',
{ locked: { logline: true } },
@@ -677,7 +677,7 @@ describe('useUniverseDraft', () => {
// Unlocking DELETES the key rather than writing `false` — the stored map
// is sparse, and lockedSchema is strict about nothing else riding along.
- act(() => result.current.toggleLock('logline'));
+ await act(async () => { result.current.toggleLock('logline'); });
expect(apiMocks.updateUniverse).toHaveBeenLastCalledWith(
'u1',
{ locked: {} },
@@ -694,22 +694,49 @@ describe('useUniverseDraft', () => {
const { result } = renderDraft();
await waitFor(() => expect(result.current.draft.id).toBe('u1'));
- act(() => {
+ await act(async () => {
result.current.toggleLock('logline');
result.current.toggleLock('logline');
});
- expect(apiMocks.updateUniverse).toHaveBeenCalledTimes(2);
expect(apiMocks.updateUniverse.mock.calls.map((call) => call[1]))
.toEqual([{ locked: { logline: true } }, { locked: {} }]);
expect(result.current.draft.locked).toEqual({});
});
+ // The server replaces `locked` wholesale per PATCH, so if those two writes
+ // reached it reversed the record would persist the lock the user just
+ // cleared. Holding the first request open proves the second is not even
+ // issued until it settles.
+ it('holds the second lock write until the first has settled', async () => {
+ const { result } = renderDraft();
+ await waitFor(() => expect(result.current.draft.id).toBe('u1'));
+ let releaseFirst;
+ apiMocks.updateUniverse.mockImplementationOnce(
+ () => new Promise((resolve) => { releaseFirst = resolve; }),
+ );
+
+ await act(async () => {
+ result.current.toggleLock('logline');
+ result.current.toggleLock('logline');
+ });
+ expect(apiMocks.updateUniverse).toHaveBeenCalledTimes(1);
+
+ await act(async () => { releaseFirst({ ...universe, locked: { logline: true } }); });
+
+ expect(apiMocks.updateUniverse).toHaveBeenCalledTimes(2);
+ expect(apiMocks.updateUniverse).toHaveBeenLastCalledWith(
+ 'u1',
+ { locked: {} },
+ { 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'));
- act(() => result.current.toggleLock('influencesEmbrace'));
+ await act(async () => { result.current.toggleLock('influencesEmbrace'); });
// `influencesEmbrace` / `influencesAvoid` are real LOCKABLE_FIELDS keys;
// the legacy whole-block `influences` key is not what the UI writes.
@@ -724,7 +751,7 @@ describe('useUniverseDraft', () => {
const { result } = renderDraft();
await waitFor(() => expect(result.current.draft.id).toBe('u1'));
- act(() => result.current.toggleLock('categories'));
+ await act(async () => { result.current.toggleLock('categories'); });
expect(apiMocks.updateUniverse).not.toHaveBeenCalled();
expect(result.current.draft.locked).toEqual({});
@@ -734,7 +761,7 @@ describe('useUniverseDraft', () => {
const { result } = renderUnsaved();
await waitFor(() => expect(result.current.loading).toBe(false));
- act(() => result.current.toggleLock('premise'));
+ await act(async () => { result.current.toggleLock('premise'); });
expect(result.current.draft.locked).toEqual({ premise: true });
expect(apiMocks.updateUniverse).not.toHaveBeenCalled();
@@ -749,7 +776,7 @@ describe('useUniverseDraft', () => {
const { result } = renderDraft({ wrapper: StrictMode });
await waitFor(() => expect(result.current.draft.id).toBe('u1'));
- act(() => result.current.toggleLock('logline'));
+ await act(async () => { result.current.toggleLock('logline'); });
expect(apiMocks.updateUniverse).toHaveBeenCalledTimes(1);
expect(result.current.draft.locked).toEqual({ logline: true });