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 (agy): revert a failed canon removal against the live draft

[._.]/ Adam Eivyabacfffb1d53
diff --git a/client/src/components/universe/UniverseCanonSection.jsx b/client/src/components/universe/UniverseCanonSection.jsx
index 8bf0f6f7e3..7a58afbca0 100644
--- a/client/src/components/universe/UniverseCanonSection.jsx
+++ b/client/src/components/universe/UniverseCanonSection.jsx
@@ -667,11 +667,18 @@ export default function UniverseCanonSection({
const stillCurrent = mountedRef.current && currentUniverseIdRef.current === capturedId;
if (!stillCurrent) return;
if (!result) {
- // Revert the optimistic removal off the pre-removal snapshot — the same
- // one-liner as `handleCreateEntry` / `handlePickFromCatalog`, and safe
- // for the same documented reason (canon edits are user-driven one at a
- // time). Restores the entry at its original position for free.
- onUniverseChange({ ...latest, [kindKey]: list });
+ // Revert against the LIVE draft, not the pre-removal snapshot. The
+ // sibling add paths revert off their snapshot on the grounds that canon
+ // edits are user-driven one-at-a-time — but that doesn't hold here: a
+ // reference render completing mid-request stamps a SIBLING entry's
+ // imageRefs[] through `applyOptimisticCanonRef` with no user action at
+ // all, and a snapshot-based revert would roll that stamp back out of
+ // view. `splice` clamps its own index, so a list that shrank meanwhile
+ // just appends.
+ const cur = latestUniverseRef.current ?? latest;
+ const restored = [...getKindList(cur, kindKey)];
+ restored.splice(list.indexOf(target), 0, target);
+ onUniverseChange({ ...cur, [kindKey]: restored });
return;
}
if (result.universe) onUniverseChange(result.universe);
diff --git a/client/src/components/universe/UniverseCanonSection.remove.test.jsx b/client/src/components/universe/UniverseCanonSection.remove.test.jsx
index 24575b6477..60908c93db 100644
--- a/client/src/components/universe/UniverseCanonSection.remove.test.jsx
+++ b/client/src/components/universe/UniverseCanonSection.remove.test.jsx
@@ -70,8 +70,11 @@ const baseUniverse = (over = {}) => ({
});
// Stateful host so `onUniverseChange` actually swaps the rendered draft.
-function Harness({ initial, onChange, kindFilter }) {
+// `api.editLive(fn)` lets a test write into the live draft the way an async
+// render completion does — i.e. without going through the component.
+function Harness({ initial, onChange, kindFilter, api }) {
const [universe, setUniverse] = useState(baseUniverse(initial));
+ if (api) api.editLive = setUniverse;
return (
<UniverseCanonSection
universe={universe}
@@ -83,10 +86,10 @@ function Harness({ initial, onChange, kindFilter }) {
);
}
-const renderSection = async ({ universe, onChange, kindFilter } = {}) => {
+const renderSection = async ({ universe, onChange, kindFilter, api } = {}) => {
const result = render(
<MemoryRouter>
- <Harness initial={universe} onChange={onChange} kindFilter={kindFilter} />
+ <Harness initial={universe} onChange={onChange} kindFilter={kindFilter} api={api} />
</MemoryRouter>,
);
// Settle the mount-time canon-usage fetch before the test interacts.
@@ -154,6 +157,39 @@ describe('UniverseCanonSection — remove entry', () => {
expect(screen.getByText('Alex')).toBeInTheDocument();
});
+ it('reverts without clobbering a sibling edit that landed mid-request', async () => {
+ // A reference render completing during the request stamps a SIBLING's
+ // imageRefs[] with no user action at all, so the revert must merge against
+ // the LIVE draft rather than the pre-removal snapshot — otherwise the
+ // failed removal rolls that stamp back out of view. `editLive` stands in
+ // for that async completion writing into the parent's draft.
+ let rejectRemove;
+ removeUniverseCanonEntry.mockReturnValue(new Promise((_, rej) => { rejectRemove = rej; }));
+ const onChange = vi.fn();
+ const api = {};
+ await renderSection({ universe: { characters: [ALEX, BLAIR] }, onChange, api });
+
+ fireEvent.click(screen.getByRole('button', { name: /remove alex/i }));
+
+ // Mid-flight: a render completion stamps Blair with a new image ref.
+ await act(async () => {
+ api.editLive((u) => ({
+ ...u,
+ characters: u.characters.map((c) =>
+ (c.id === 'chr-2' ? { ...c, imageRefs: ['blair-new.png'] } : c)),
+ }));
+ });
+
+ await act(async () => { rejectRemove(new Error('nope')); });
+
+ await waitFor(() => expect(screen.getByText('Alex')).toBeInTheDocument());
+ const final = onChange.mock.calls.at(-1)[0].characters;
+ // Alex is back at index 0...
+ expect(final.map((c) => c.id)).toEqual(['chr-1', 'chr-2']);
+ // ...and the sibling's mid-flight stamp survived the revert.
+ expect(final.find((c) => c.id === 'chr-2').imageRefs).toEqual(['blair-new.png']);
+ });
+
it('offers Remove on locked entries — the lock guards AI rewrites, not removal', async () => {
await renderSection({ universe: { characters: [{ ...ALEX, locked: true }] } });
expect(screen.getByRole('button', { name: /remove alex/i })).toBeEnabled();