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
address review (codex): stamp the adopt guidance with its own epoch
[._.]/ Adam Eivyd1ab6d1e7212
diff --git a/client/src/hooks/useUniverseDraft.js b/client/src/hooks/useUniverseDraft.jsindex 50711de524..e9a2250c93 100644--- a/client/src/hooks/useUniverseDraft.js+++ b/client/src/hooks/useUniverseDraft.js@@ -125,6 +125,13 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {// 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 guide carries its OWN `guidanceEpoch` rather than riding the reference+ // epoch, because the two move independently: a plain add/remove bumps `epoch`+ // without writing the guide. Overlaying on the reference epoch alone would+ // let such a mutation resurrect a long-settled adopt over a newer peer edit+ // that G legitimately carries. Overlay only when a PATCH wrote the guide+ // AFTER G was issued.+ //// 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@@ -152,21 +159,27 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {}, []);const styleReferenceState = useCallback(- (id) => styleReferenceStatesRef.current.get(id) ?? { references: null, guidance: null, epoch: 0 },+ (id) => styleReferenceStatesRef.current.get(id)+ ?? { references: null, guidance: null, guidanceEpoch: 0, epoch: 0 },[],);// 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.+ // previous entry's guidance carries forward (so an adopt followed by a plain+ // add/remove during one GET doesn't drop the adopted values) but keeps its+ // ORIGINAL `guidanceEpoch` — the epoch at which a PATCH last actually wrote+ // the style guide. That stamp is what lets the hydration arbiter tell "this+ // guidance is newer than the GET" from "the GET already contains it."const applyStyleReferenceMutation = useCallback((id, references, guidance = null) => {const previous = styleReferenceState(id);+ const epoch = previous.epoch + 1;styleReferenceStatesRef.current.set(id, {references,+ epoch,guidance: guidance ?? previous.guidance,- epoch: previous.epoch + 1,+ guidanceEpoch: guidance ? epoch : previous.guidanceEpoch,});}, [styleReferenceState]);@@ -177,14 +190,19 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {const applyStyleReferenceHydration = useCallback((id, issuedEpoch, references) => {const state = styleReferenceState(id);if (state.epoch !== issuedEpoch) {- return { styleReferences: state.references, ...(state.guidance || {}) };+ // References always come from the winning mutation. The style guide only+ // does when a PATCH actually wrote it AFTER this GET was issued+ // (`guidanceEpoch > issuedEpoch`) — otherwise this GET already read that+ // guidance back from the server, and overlaying the stashed copy would+ // clobber a genuinely newer value the GET carries (a peer edit made while+ // the user was on another universe) and — via markDraftSaved — bank the+ // clobbered value so a later general Save pushes it back server-side.+ return {+ styleReferences: state.references,+ ...(state.guidanceEpoch > issuedEpoch ? state.guidance : null),+ };}- // This GET won, so its body IS the new baseline — including the style- // guide. Drop the stashed guidance: it describes an older PATCH the server- // has since confirmed, so carrying it past this point would let a LATER- // plain add/remove (which contributes no guidance of its own) overlay- // those spent values over a genuinely newer server styleNotes/influences.- styleReferenceStatesRef.current.set(id, { ...state, references, guidance: null });+ styleReferenceStatesRef.current.set(id, { ...state, references });return { styleReferences: references };}, [styleReferenceState]);diff --git a/client/src/hooks/useUniverseDraft.test.jsx b/client/src/hooks/useUniverseDraft.test.jsxindex e7b926d4f3..d0b2f48d5e 100644--- a/client/src/hooks/useUniverseDraft.test.jsx+++ b/client/src/hooks/useUniverseDraft.test.jsx@@ -468,4 +468,59 @@ describe('useUniverseDraft', () => {// value, which the removal's PATCH never wrote.expect(result.current.draft.styleNotes).toBe('Peer notes');});++ it('does not let a plain mutation resurrect an older adopt over a newer peer edit the GET carries (codex review finding)', async () => {+ const refX = { id: 'style-ref-x', title: 'Ref X', prompt: 'x', imageRefs: ['x.png'] };+ // Same class as the test above, but with NO hydration landing between the+ // adopt and the racing mutation — so the guard cannot rely on a winning+ // hydration to retire the stashed guidance. Only the guidance's own epoch+ // stamp distinguishes "written after this GET" from "the GET already has it."+ let u1Fetches = 0;+ let resolveLateHydration;+ apiMocks.getUniverse.mockImplementation(async (id) => {+ if (id === 'u2') return universeTwo;+ u1Fetches += 1;+ if (u1Fetches === 1) return { ...universe, styleReferences: [], styleNotes: '' };+ return new Promise((resolve) => { resolveLateHydration = resolve; });+ });++ const { result, rerender } = renderSelectable();+ await waitFor(() => expect(result.current.draft.id).toBe('u1'));++ // Adopt on u1 — stashes guidance at the epoch it was written.+ await act(async () => {+ await result.current.persistStyleReference({+ reference: refX,+ proposed: { styleNotes: 'Adopted notes', influences: { embrace: [], avoid: [] } },+ adopt: true,+ });+ });++ // u1 -> u2 -> u1 with the adopt already settled. The return's GET reads a+ // peer's newer style notes; it is issued AFTER the adopt, so it already+ // contains the adopted state plus the peer's later edit.+ await act(async () => { rerender({ selectedId: 'u2' }); });+ await waitFor(() => expect(result.current.draft.id).toBe('u2'));+ await act(async () => { rerender({ selectedId: 'u1' }); });+ await waitFor(() => expect(resolveLateHydration).toBeTypeOf('function'));++ // A plain removal races that GET and wins on references — but it wrote no+ // style guide, so it must not drag the older adopt's notes along with it.+ let resolveRemoval;+ apiMocks.updateUniverse.mockImplementationOnce(() => new Promise((resolve) => { resolveRemoval = resolve; }));+ const pendingRemoval = result.current.removeStyleReference(refX.id);+ await waitFor(() => expect(resolveRemoval).toBeTypeOf('function'));+ await act(async () => {+ resolveRemoval({ ...universe, styleReferences: [], styleNotes: 'Peer notes' });+ await pendingRemoval;+ });+ await act(async () => {+ resolveLateHydration({ ...universe, styleReferences: [refX], styleNotes: 'Peer notes' });+ await Promise.resolve();+ });++ await waitFor(() => expect(result.current.draft.id).toBe('u1'));+ expect(result.current.draft.styleReferences).toEqual([]);+ expect(result.current.draft.styleNotes).toBe('Peer notes');+ });});