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
feat([conflict-restore-additive]): faithful restore-all replaces universe categories
[._.]/ Adam Eivy05e06b853a4f
diff --git a/server/services/conflictJournalResolver.js b/server/services/conflictJournalResolver.jsindex ce459c306c..e16cb927d7 100644--- a/server/services/conflictJournalResolver.js+++ b/server/services/conflictJournalResolver.js@@ -13,15 +13,19 @@* file directly) so updatedAt bumps and the change propagates via the existing* push pipeline.*- * NOTE — restore-all is an ADDITIVE overlay, not a byte-for-byte rollback.- * updateUniverse merges some fields per-key rather than replacing them wholesale- * (notably `categories`: a partial patch unions keys so an unrelated category- * isn't wiped — see universeBuilder.js mergedCategories). So restoring a snapshot- * re-applies the archived values for every restorable field, but a category the- * snapshot lacked that the live record gained since detection is preserved, not- * removed. This is intentional: union-on-restore can't silently destroy data the- * journal never captured. The UI labels it "restore" (re-apply mine), not a- * destructive "revert to exact snapshot."+ * NOTE — restore-all is a FAITHFUL replace of the restorable fields, matching+ * the UI promise ("restore your whole version"). Most restorable fields already+ * patch wholesale through `update*`; the one exception is universe `categories`,+ * which `updateUniverse` normally unions per-key (so a partial PATCH of one+ * category can't wipe the others). restore-all passes `{ replaceCategories: true }`+ * so the archived snapshot's category map replaces the live one wholesale — a+ * category the live record gained since the conflict (e.g. one the user kept+ * after the LWW overwrite) is dropped, because "my whole version" didn't have it.+ *+ * merge-fields stays ADDITIVE: it overlays only the chosen fields onto the+ * CURRENT record, so a selected `categories` field unions per-key (the default+ * `update*` semantics) rather than replacing — you're merging your version of+ * that field in, not reverting the record to the snapshot.*/import { existsSync } from 'fs';@@ -64,7 +68,10 @@ export async function getConflict(id) {}// Apply a content patch through the right service's normal update path.-async function applyToRecord(kind, recordId, patch) {+// `replace` (restore-all only) makes per-key-merged fields replace wholesale —+// currently just universe `categories` (every other restorable field already+// patches wholesale through its service).+async function applyToRecord(kind, recordId, patch, { replace = false } = {}) {// The target record may have been tombstoned between conflict-archive time and// resolution. Translate the services' not-found codes into ERR_TARGET_GONE so// the route maps it to a clean 409 ("discard the entry") instead of a 500.@@ -77,8 +84,10 @@ async function applyToRecord(kind, recordId, patch) {};if (kind === 'universe') {// Mutator form bypasses the literal-patch reference-sheet preservation- // guard — the restored snapshot is the trusted writer here.- await updateUniverse(recordId, () => patch).catch(translateGone);+ // guard — the restored snapshot is the trusted writer here. `replace`+ // (restore-all) swaps the additive per-key categories merge for a wholesale+ // replace so the restore is faithful to the snapshot.+ await updateUniverse(recordId, () => patch, { replaceCategories: replace }).catch(translateGone);} else if (kind === 'series') {await updateSeries(recordId, patch).catch(translateGone);} else if (kind === 'mediaCollection') {@@ -116,7 +125,8 @@ export async function resolveConflict(id, { action, fields = [] } = {}) {const snapshot = entry.localSnapshot || {};if (action === 'restore-all') {- await applyToRecord(entry.recordKind, entry.recordId, pick(snapshot, allowed));+ // Faithful restore: replace the keyed fields (universe categories) wholesale.+ await applyToRecord(entry.recordKind, entry.recordId, pick(snapshot, allowed), { replace: true });} else if (action === 'merge-fields') {if (!Array.isArray(fields) || fields.length === 0) {throw makeErr('merge-fields requires a non-empty `fields` array', ERR_VALIDATION);diff --git a/server/services/conflictJournalResolver.test.js b/server/services/conflictJournalResolver.test.jsindex 213d229dd2..50814a0336 100644--- a/server/services/conflictJournalResolver.test.js+++ b/server/services/conflictJournalResolver.test.js@@ -68,6 +68,36 @@ describe('conflictJournalResolver', () => {expect(fresh.starterPrompt).toBe('REMOTE prompt'); // untouched});+ it('restore-all REPLACES universe categories wholesale — a category the live record gained since the conflict is dropped', async () => {+ const u = await universeSvc.createUniverse({ name: 'Cats' });+ // Post-conflict, the live record gained a custom category the snapshot never had.+ await universeSvc.updateUniverse(u.id, { categories: { gadgets: { variations: [{ label: 'Ray', prompt: 'a ray gun' }] } } });+ expect((await universeSvc.getUniverse(u.id)).categories.gadgets).toBeTruthy();+ const entryId = await seedEntry(u.id, {+ id: u.id, name: 'Cats',+ categories: { landscapes: { variations: [{ label: 'Hill', prompt: 'a green hill' }] } },+ });++ await resolver.resolveConflict(entryId, { action: 'restore-all' });+ const fresh = await universeSvc.getUniverse(u.id);+ expect(fresh.categories.gadgets).toBeUndefined(); // faithful replace dropped the live-only category+ expect(fresh.categories.landscapes.variations).toHaveLength(1); // the snapshot's category landed+ });++ it('merge-fields KEEPS a live-only category (additive overlay, not a replace)', async () => {+ const u = await universeSvc.createUniverse({ name: 'Cats2' });+ await universeSvc.updateUniverse(u.id, { categories: { gadgets: { variations: [{ label: 'Ray', prompt: 'a ray gun' }] } } });+ const entryId = await seedEntry(u.id, {+ id: u.id, name: 'Cats2',+ categories: { landscapes: { variations: [{ label: 'Hill', prompt: 'a green hill' }] } },+ });++ await resolver.resolveConflict(entryId, { action: 'merge-fields', fields: ['categories'] });+ const fresh = await universeSvc.getUniverse(u.id);+ expect(fresh.categories.gadgets).toBeTruthy(); // additive: live-only category preserved+ expect(fresh.categories.landscapes.variations).toHaveLength(1); // snapshot's category merged in+ });+it('restore-all re-applies an archived ISSUE snapshot (title + prose stage)', async () => {const issue = await issueSvc.createIssue({ seriesId: 'ser-restore', title: 'Remote Title' });// Remote LWW-overwrote both the title and the prose output.diff --git a/server/services/universeBuilder.js b/server/services/universeBuilder.jsindex 25a1ec5360..1455eaaa2d 100644--- a/server/services/universeBuilder.js+++ b/server/services/universeBuilder.js@@ -1071,7 +1071,12 @@ export async function updateUniverse(id, patchOrMutator = {}, options = {}) {// ORIGINATED from a catalog→canon projection. Threaded into projectToCatalog// below so the originating catalog row isn't written back a second time// (breaks the projectToCanon → updateUniverse → projectToCatalog loop).- const { silent = false, canonProjectionGuard = null } = options;+ // `options.replaceCategories: true` replaces the whole `categories` map with+ // the patch's instead of unioning it per-key. Used by the conflict-journal+ // "restore mine" path so a faithful restore of the archived snapshot drops a+ // category the live record gained since the conflict, rather than resurrecting+ // it under the normal additive-PATCH semantics (see `mergedCategories` below).+ const { silent = false, canonProjectionGuard = null, replaceCategories = false } = options;const s = store();const { merged, nameChanged, skipped, removedCharacterIds, prevEphemeral, nextEphemeral } = await s.queueRecordWrite(id, async () => {const cur = await s.loadOne(id);@@ -1096,8 +1101,11 @@ export async function updateUniverse(id, patchOrMutator = {}, options = {}) {// Merge `categories` per-key — a partial PATCH that only includes// `landscapes` must NOT wipe characters/structures/etc. Whole categories// not present in the patch are kept as-is from the current universe.+ // EXCEPT under `replaceCategories` (conflict-journal restore), where the+ // patch's categories map replaces the current one wholesale so a faithful+ // restore can drop a category the live record gained since the conflict.const mergedCategories = 'categories' in patch- ? { ...cur.categories, ...(patch.categories || {}) }+ ? (replaceCategories ? (patch.categories || {}) : { ...cur.categories, ...(patch.categories || {}) }): cur.categories;// Merge `llm` field-by-field — sending only `{ provider }` shouldn'tdiff --git a/server/services/universeBuilder.test.js b/server/services/universeBuilder.test.jsindex 5907afd13b..740def4270 100644--- a/server/services/universeBuilder.test.js+++ b/server/services/universeBuilder.test.js@@ -277,6 +277,24 @@ describe("universeBuilder service", () => {expect(patched.influences.avoid).toEqual(w.influences.avoid);});+ it("updateUniverse with { replaceCategories } swaps the whole categories map (drops live-only categories)", async () => {+ const w = await seedWorld(); // seeds `landscapes` (2 vars) + custom `outfits` (1 var)+ expect(w.categories.outfits).toBeTruthy();+ // Default PATCH unions per-key — the live-only `outfits` survives.+ const unioned = await svc.updateUniverse(w.id, {+ categories: { landscapes: { variations: [{ label: "New", prompt: "p" }] } },+ });+ expect(unioned.categories.outfits).toBeTruthy();+ // With replaceCategories the patch's map replaces wholesale — `outfits` is dropped.+ const replaced = await svc.updateUniverse(+ w.id,+ { categories: { landscapes: { variations: [{ label: "New2", prompt: "p2" }] } } },+ { replaceCategories: true },+ );+ expect(replaced.categories.outfits).toBeUndefined();+ expect(replaced.categories.landscapes.variations).toHaveLength(1);+ });+it("bumps updatedAt on a canon entry whose content changed, leaves an untouched entry's timestamp", async () => {// Two canon characters; edit only the first's content. The edited entry's// updatedAt must advance (so the canon→catalog projection's LWW clock is