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: revert optimistic canon append when catalog-pick save fails

[._.]/ Adam Eivyd738f75f1de5
diff --git a/client/src/components/universe/UniverseCanonSection.jsx b/client/src/components/universe/UniverseCanonSection.jsx
index 3a82ebc331..f7a1d558f2 100644
--- a/client/src/components/universe/UniverseCanonSection.jsx
+++ b/client/src/components/universe/UniverseCanonSection.jsx
@@ -563,7 +563,16 @@ export default function UniverseCanonSection({
onUniverseChange({ ...universe, [kindKey]: nextList });
const updated = await updateUniverse(universeId, { [kindKey]: nextList })
.catch((err) => { toast.error(`Add from Catalog failed: ${err.message}`); return null; });
- if (!updated) { if (mountedRef.current) setCatalogLinking(false); return; }
+ if (!updated) {
+ // Revert the optimistic append — the save didn't land, so the phantom
+ // row must not linger. `list` is the pre-append snapshot from this
+ // closure (canon edits are user-driven one-at-a-time, so it's current).
+ if (mountedRef.current && currentUniverseIdRef.current === capturedId) {
+ onUniverseChange({ ...universe, [kindKey]: list });
+ }
+ if (mountedRef.current) setCatalogLinking(false);
+ return;
+ }
if (mountedRef.current && currentUniverseIdRef.current === capturedId) onUniverseChange(updated);
// Link the catalog ingredient back to the universe so the "Appears in"
// panel populates. Non-fatal: the embedded entry already carries the
diff --git a/client/src/components/universe/UniverseCanonSection.picker.test.jsx b/client/src/components/universe/UniverseCanonSection.picker.test.jsx
index 2580cbd1b5..5b272bed19 100644
--- a/client/src/components/universe/UniverseCanonSection.picker.test.jsx
+++ b/client/src/components/universe/UniverseCanonSection.picker.test.jsx
@@ -139,6 +139,29 @@ describe('UniverseCanonSection — Pick from Catalog', () => {
));
});
+ it('reverts the optimistic append when the save fails', async () => {
+ const onUniverseChange = vi.fn();
+ updateUniverse.mockRejectedValue(new Error('boom'));
+ listCatalogIngredients.mockResolvedValue({
+ items: [{ id: 'cat-chr-abc123', type: 'character', name: 'Rust Vega', payload: { physicalDescription: 'drifter' } }],
+ });
+
+ renderSection({ onUniverseChange });
+ fireEvent.click(screen.getByRole('button', { name: /pick character from catalog/i }));
+ const row = await screen.findByRole('button', { name: /rust vega/i });
+ fireEvent.click(row);
+
+ await waitFor(() => expect(updateUniverse).toHaveBeenCalled());
+ // Optimistic append fires first (characters carries the new entry); the
+ // failed save then reverts it, so the LAST onUniverseChange restores the
+ // pre-append (empty) list and the catalog link is never attempted.
+ await waitFor(() => {
+ const lastPatch = onUniverseChange.mock.calls.at(-1)[0];
+ expect(lastPatch.characters).toEqual([]);
+ });
+ expect(linkCatalogIngredient).not.toHaveBeenCalled();
+ });
+
it('refuses to add an ingredient already linked by ingredientId', async () => {
const onUniverseChange = vi.fn();
listCatalogIngredients.mockResolvedValue({