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

address review (claude): extend the epoch guard to the adopt PATCH's style guide

[._.]/ Adam Eivyd297ecad1bdb
diff --git a/client/src/hooks/useUniverseDraft.js b/client/src/hooks/useUniverseDraft.js
index 038b6af007..d94ba5230e 100644
--- a/client/src/hooks/useUniverseDraft.js
+++ b/client/src/hooks/useUniverseDraft.js
@@ -106,22 +106,32 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {
// entry so back-to-back removals on one universe never overlap; different
// universes' queues are independent.
//
- // Each entry is `{ references, epoch }`, where `epoch` counts the MUTATION
- // responses folded into `references` so far. The epoch version-gates the
- // other writer — the hydration effect's `getUniverse` — against the last
- // reordering race: a mutation M issued before the user navigates away and
- // back, and the re-hydration GET G issued on the return. When M resolves
- // first, G's body is a PRE-mutation read, so letting it write would silently
- // revert M client-side. G captures the epoch when it is ISSUED and writes its
- // own array back only if no mutation landed while it was in flight.
+ // Each entry is `{ references, guidance, epoch }`, where `epoch` counts the
+ // MUTATION responses folded in so far. The epoch version-gates the other
+ // writer — the hydration effect's `getUniverse` — against the last reordering
+ // race: a mutation M issued before the user navigates away and back, and the
+ // re-hydration GET G issued on the return. When M resolves first, G's body is
+ // a PRE-mutation read, so letting it write would silently revert M
+ // client-side. G captures the epoch when it is ISSUED and keeps its own body
+ // only if no mutation landed while it was in flight.
+ //
+ // `guidance` is `{ styleNotes, influences }` when the mutation was an ADOPT —
+ // that PATCH writes the style guide in the SAME request as the references, so
+ // both halves are equally stale in a losing G. Overriding only the references
+ // would let G revert the adopted guidance AND (via markDraftSaved) bank the
+ // reverted values as the saved baseline, so the draft reads clean and the
+ // next general Save pushes the stale guidance back server-side — a worse
+ // outcome than the client-only revert this guard exists to stop. The override
+ // is scoped to exactly the fields the winning PATCH wrote; every other field
+ // still comes from G, which may legitimately be fresher (peer sync, canon).
//
// The guard is deliberately "a mutation always beats a concurrent GET", not a
// monotonic issued-at sequence shared by all writers: G is issued AFTER M yet
// reads state the server may not have written M into yet, so ordering by
// issue time would make a late-resolving M lose to an earlier-issued G —
// reintroducing the A → B → A loss fixed above. Keeping `epoch` in the same
- // record as `references` is what makes "the epoch moves whenever the array
- // does" structural rather than a convention writers must remember.
+ // record as the values it guards is what makes "the epoch moves whenever
+ // those values do" structural rather than a convention writers must remember.
const styleReferenceQueuesRef = useRef(new Map());
const styleReferenceStatesRef = useRef(new Map());
// Mirrors `selectedId` synchronously during render (not via a passive
@@ -142,25 +152,35 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {
}, []);
const styleReferenceState = useCallback(
- (id) => styleReferenceStatesRef.current.get(id) ?? { references: null, epoch: 0 },
+ (id) => styleReferenceStatesRef.current.get(id) ?? { references: null, guidance: null, epoch: 0 },
[],
);
- // Record a mutation's confirmed styleReferences for `id`, bumping its epoch so
- // any hydration GET still in flight for that universe knows its body is stale.
- const applyStyleReferenceMutation = useCallback((id, references) => {
- styleReferenceStatesRef.current.set(id, { references, epoch: styleReferenceState(id).epoch + 1 });
+ // Record a mutation's confirmed values for `id`, bumping its epoch so any
+ // hydration GET still in flight for that universe knows its body is stale.
+ // `guidance` is null for a mutation that didn't write the style guide; the
+ // previous entry's guidance carries forward so a later plain add/remove
+ // doesn't drop an earlier adopt's confirmed values.
+ const applyStyleReferenceMutation = useCallback((id, references, guidance = null) => {
+ const previous = styleReferenceState(id);
+ styleReferenceStatesRef.current.set(id, {
+ references,
+ guidance: guidance ?? previous.guidance,
+ epoch: previous.epoch + 1,
+ });
}, [styleReferenceState]);
// Fold a hydration GET's styleReferences into `id`'s state, unless a mutation
- // resolved while the GET was in flight (epoch moved) — in which case the
- // mutation's result stands. Returns whichever array is authoritative, so the
- // hydrated draft renders that one rather than the stale server read.
+ // resolved while the GET was in flight (epoch moved) — in which case that
+ // mutation's values stand. Returns the draft fields the winning writer owns,
+ // to be spread LAST over the hydrated draft so they override the server read.
const applyStyleReferenceHydration = useCallback((id, issuedEpoch, references) => {
- const { references: current, epoch } = styleReferenceState(id);
- if (epoch !== issuedEpoch) return current;
- styleReferenceStatesRef.current.set(id, { references, epoch });
- return references;
+ const state = styleReferenceState(id);
+ if (state.epoch !== issuedEpoch) {
+ return { styleReferences: state.references, ...(state.guidance || {}) };
+ }
+ styleReferenceStatesRef.current.set(id, { ...state, references });
+ return { styleReferences: references };
}, [styleReferenceState]);
const markDraftSaved = useCallback((snapshotSource) => {
@@ -232,19 +252,6 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {
]).then(([universe, nextRuns]) => {
if (cancelled) return;
if (universe) {
- // Refresh this universe's styleReferences snapshot from the server on
- // every successful hydration — a stale map entry from an earlier
- // local mutation (or absence of one, on first visit) must not shadow
- // changes made while this universe wasn't selected (peer sync, the
- // image-delete purge route). The next add/remove needs this fresh
- // baseline, not a leftover cache (codex review finding) — except when
- // a mutation resolved while this GET was in flight, which the epoch
- // guard defers to (see styleReferenceStatesRef).
- const styleReferences = applyStyleReferenceHydration(
- selectedId,
- issuedEpoch,
- universe.styleReferences || [],
- );
const hydrated = {
...universe,
categories: ensureDraftCategories(universe.categories),
@@ -253,9 +260,21 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {
premise: universe.premise || '',
styleNotes: universe.styleNotes || '',
influences: ensureInfluences(universe.influences),
- styleReferences,
+ styleReferences: universe.styleReferences || [],
locked: universe.locked || {},
llm: universe.llm || { provider: null, model: null },
+ // Refresh this universe's styleReferences snapshot from the server on
+ // every successful hydration — a stale map entry from an earlier
+ // local mutation (or absence of one, on first visit) must not shadow
+ // changes made while this universe wasn't selected (peer sync, the
+ // image-delete purge route). The next add/remove needs this fresh
+ // baseline, not a leftover cache (codex review finding) — except when
+ // a mutation resolved while this GET was in flight, in which case
+ // this spread overrides the fields THAT PATCH wrote with its newer
+ // confirmed values (see styleReferenceStatesRef). Spread LAST so it
+ // wins over the server read above; markDraftSaved then banks the
+ // overridden values, not the stale ones.
+ ...applyStyleReferenceHydration(selectedId, issuedEpoch, universe.styleReferences || []),
};
setDraft(hydrated);
markDraftSaved(hydrated);
@@ -408,8 +427,13 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {
// user navigates away and back) must build from this result, not a
// pre-save list (codex review finding: an A→B→A round trip must not lose
// A's in-flight save). The epoch bump invalidates any re-hydration GET for
- // targetId still in flight — see styleReferenceStatesRef.
- applyStyleReferenceMutation(targetId, updated.styleReferences || []);
+ // targetId still in flight — see styleReferenceStatesRef. On the adopt
+ // path the SAME PATCH wrote the style guide, so hand that over too or a
+ // losing GET reverts the adopted guidance while the references survive.
+ applyStyleReferenceMutation(targetId, updated.styleReferences || [], adopt ? {
+ styleNotes: updated.styleNotes || '',
+ influences: ensureInfluences(updated.influences),
+ } : null);
// But only touch the currently DISPLAYED draft if the user is still on
// targetId — applying it otherwise would poison a DIFFERENT, now-selected
// universe's state with targetId's references (codex review finding).
diff --git a/client/src/hooks/useUniverseDraft.test.jsx b/client/src/hooks/useUniverseDraft.test.jsx
index 6f9c3ad016..c6be7947f9 100644
--- a/client/src/hooks/useUniverseDraft.test.jsx
+++ b/client/src/hooks/useUniverseDraft.test.jsx
@@ -333,6 +333,11 @@ describe('useUniverseDraft', () => {
});
it('keeps a mutation that resolves BEFORE the re-hydration GET it raced (codex review finding)', async () => {
+ // u1 starts with refA already saved, so the post-mutation list ([refA, refX])
+ // and G's stale list ([refA]) differ in a way the FINAL removal payload can
+ // discriminate — with an empty starting list both branches would emit the
+ // same `{ styleReferences: [] }` and the assertion would prove nothing.
+ const refA = { id: 'style-ref-a', title: 'Ref A', prompt: 'a', imageRefs: ['a.png'] };
const refX = { id: 'style-ref-x', title: 'Ref X', prompt: 'x', imageRefs: ['x.png'] };
// The re-hydration fetch on the return to u1 is held open so it can be
// resolved AFTER the mutation, with the PRE-mutation body the server would
@@ -343,16 +348,21 @@ describe('useUniverseDraft', () => {
if (id === 'u2') return universeTwo;
u1Fetches += 1;
if (u1Fetches === 2) return new Promise((resolve) => { resolveU1Hydration = resolve; });
- return { ...universe, styleReferences: [] };
+ return { ...universe, styleReferences: [refA] };
});
const { result, rerender } = renderSelectable();
- await waitFor(() => expect(result.current.draft.id).toBe('u1'));
+ await waitFor(() => expect(result.current.draft.styleReferences).toEqual([refA]));
- // Mutation M on u1, held open.
+ // Adopt mutation M on u1, held open — adopt so the assertions also cover the
+ // style guide the same PATCH writes, not just the references.
let resolveU1Save;
apiMocks.updateUniverse.mockImplementationOnce(() => new Promise((resolve) => { resolveU1Save = resolve; }));
- const pendingSave = result.current.persistStyleReference({ reference: refX, adopt: false });
+ const pendingSave = result.current.persistStyleReference({
+ reference: refX,
+ proposed: { styleNotes: 'Adopted notes', influences: { embrace: ['ink'], avoid: [] } },
+ adopt: true,
+ });
// u1 -> u2 -> u1; the return issues re-hydration GET G, which stays pending.
await act(async () => { rerender({ selectedId: 'u2' }); });
@@ -362,24 +372,35 @@ describe('useUniverseDraft', () => {
// M resolves FIRST — the reordering this guard exists for.
await act(async () => {
- resolveU1Save({ ...universe, styleReferences: [refX] });
+ resolveU1Save({
+ ...universe,
+ styleReferences: [refA, refX],
+ styleNotes: 'Adopted notes',
+ influences: { embrace: ['ink'], avoid: [] },
+ });
await pendingSave;
});
- // ...then G resolves carrying the stale, pre-mutation reference list.
+ // ...then G resolves carrying the stale, pre-mutation body.
await act(async () => {
- resolveU1Hydration({ ...universe, styleReferences: [] });
+ resolveU1Hydration({ ...universe, styleReferences: [refA], styleNotes: '' });
await Promise.resolve();
});
- // G must not have reverted M: the displayed draft still shows refX...
+ // G must not have reverted M: the displayed draft still shows refX, and the
+ // style guide the SAME adopt PATCH wrote survives alongside it.
await waitFor(() => expect(result.current.draft.id).toBe('u1'));
- expect(result.current.draft.styleReferences).toEqual([refX]);
+ expect(result.current.draft.styleReferences).toEqual([refA, refX]);
+ expect(result.current.draft.styleNotes).toBe('Adopted notes');
+ // markDraftSaved banked the overridden values, so the adopted guidance is
+ // not silently re-saved as the stale '' on the next general Save.
+ expect(result.current.isDraftDirty()).toBe(false);
- // ...and the next mutation builds from [refX], not G's empty snapshot —
- // otherwise this PATCH would be a no-op and refX would linger server-side.
- await act(async () => { await result.current.removeStyleReference(refX.id); });
- expect(apiMocks.updateUniverse.mock.calls.at(-1)).toEqual(['u1', { styleReferences: [] }, { silent: true }]);
- expect(result.current.draft.styleReferences).toEqual([]);
+ // The next mutation builds from [refA, refX], not G's stale [refA] — with a
+ // stale base this PATCH would emit `{ styleReferences: [] }` and leave refX
+ // lingering server-side.
+ await act(async () => { await result.current.removeStyleReference(refA.id); });
+ expect(apiMocks.updateUniverse.mock.calls.at(-1)).toEqual(['u1', { styleReferences: [refX] }, { silent: true }]);
+ expect(result.current.draft.styleReferences).toEqual([refX]);
});
});