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
fix([conflict-journal-deep-diff]): unique part keys + bail on duplicate identities
[._.]/ Adam Eivy391ba910f2b2
diff --git a/client/src/components/sharing/ConflictsTab.jsx b/client/src/components/sharing/ConflictsTab.jsxindex 5610ba6b2d..67bcd6e700 100644--- a/client/src/components/sharing/ConflictsTab.jsx+++ b/client/src/components/sharing/ConflictsTab.jsx@@ -127,7 +127,7 @@ function ConflictEntry({ entry, onResolved }) {{d.parts.map((p) => (<div key={p.path} className="pl-2 border-l-2 border-port-border"><div className="font-mono text-[10px] text-gray-400 mb-1">- {p.path} <span className="text-gray-600">({p.changed})</span>+ {p.label ?? p.path} <span className="text-gray-600">({p.changed})</span></div><InlineDiff oldText={asText(p.localValue)} newText={asText(p.remoteValue)} /></div>diff --git a/client/src/components/sharing/ConflictsTab.test.jsx b/client/src/components/sharing/ConflictsTab.test.jsxindex 53ac04cc4a..27d41d6ccf 100644--- a/client/src/components/sharing/ConflictsTab.test.jsx+++ b/client/src/components/sharing/ConflictsTab.test.jsx@@ -77,8 +77,8 @@ describe('ConflictsTab', () => {diffSummary: [{field: 'characters', changed: 'both',parts: [- { path: 'Alice', changed: 'both', localValue: { bio: 'mine' }, remoteValue: { bio: 'theirs' } },- { path: 'Bob', changed: 'local-only', localValue: { bio: 'gone' }, remoteValue: undefined },+ { path: 'c1', label: 'Alice', changed: 'both', localValue: { bio: 'mine' }, remoteValue: { bio: 'theirs' } },+ { path: 'c2', label: 'Bob', changed: 'local-only', localValue: { bio: 'gone' }, remoteValue: undefined },],}],status: 'pending',diff --git a/server/lib/conflictJournal.js b/server/lib/conflictJournal.jsindex b9b8269fb6..a27e2416f8 100644--- a/server/lib/conflictJournal.js+++ b/server/lib/conflictJournal.js@@ -240,47 +240,63 @@ const entryMatchKey = (el) => firstStringField(el, ['id', 'key', 'slug', 'name']// name/title is preferred for display even when the match used the id).const entryLabel = (el, fallback) => firstStringField(el, ['name', 'title', 'label', 'key', 'slug', 'id']) ?? fallback;-// Diff two key→value maps: one part per key whose value differs. `label(key,-// lv, rv)` names the part for display. Returns the parts array, or null when-// nothing differs.+// Diff two key→value maps: one part per key whose value differs. Each part+// carries a STABLE, unique `path` (the map key — used as the React render key)+// and a human `label(key, lv, rv)` for display; for an object map the two are+// the same, but for an identity-paired array the path is the (unique) match key+// while the label is a friendly name that may collide. Returns the parts array,+// or null when nothing differs.const diffEntryMaps = (lMap, rMap, label) => {const parts = [];for (const key of new Set([...lMap.keys(), ...rMap.keys()])) {const lv = lMap.get(key);const rv = rMap.get(key);if (canonicalStringify(lv) === canonicalStringify(rv)) continue;- parts.push({ path: label(key, lv, rv), localValue: lv, remoteValue: rv, changed: changedFlag(lv, rv) });+ parts.push({ path: key, label: label(key, lv, rv), localValue: lv, remoteValue: rv, changed: changedFlag(lv, rv) });}return parts.length ? parts : null;};+// Build a Map keyed by each element's stable match key. Returns null — so the+// caller falls back to the whole-field diff — when ANY element is identity-less+// (null key) OR two elements share a key (a duplicate id, or two entries that+// both fall back to the same name): identity pairing isn't reliable, and a+// last-wins Map would silently drop the collided element from the diff.+const buildKeyedMap = (arr) => {+ const map = new Map();+ for (const el of arr) {+ const key = entryMatchKey(el);+ if (key === null || map.has(key)) return null;+ map.set(key, el);+ }+ return map;+};+/*** One-level structural diff of a single restorable field, used to render the* Conflicts tab as "which sub-entry changed" instead of one giant JSON blob.- * Returns an array of changed sub-entries `[{ path, localValue, remoteValue,- * changed }]`, or `null` when the field isn't deepenable — a scalar, an array- * of scalars, an array of objects without any stable identity, or a- * shape-mismatch (object vs array) — in which case the caller keeps the- * whole-field diff. Only entries that actually differ are emitted.+ * Returns an array of changed sub-entries `[{ path, label, localValue,+ * remoteValue, changed }]`, or `null` when the field isn't deepenable — a+ * scalar, an array of scalars, an array of objects without unique stable+ * identities, or a shape-mismatch (object vs array) — in which case the caller+ * keeps the whole-field diff. Only entries that actually differ are emitted.** - Object map / structured object (`categories`, `stages`, `arc`): one part- * per key whose value differs.+ * per key whose value differs (path = label = key).* - Array of identity-bearing objects (`characters`, `places`, `seasons`):* paired by `entryMatchKey`; one part per identity that differs (a side- * missing ⇒ added/removed). The `path` is a human label.+ * missing ⇒ added/removed). The `path` is the unique match key; the `label`+ * is a human name.*/export function deepFieldDiff(localVal, remoteVal) {if (isPlainObject(localVal) && isPlainObject(remoteVal)) {return diffEntryMaps(new Map(Object.entries(localVal)), new Map(Object.entries(remoteVal)), (key) => key);}if (Array.isArray(localVal) && Array.isArray(remoteVal)) {- // Require every present element to carry a match key on BOTH sides — a- // mixed/identity-less array (e.g. array of strings) text-diffs fine as a- // whole, so bail to the whole-field path for those.- const keyed = (arr) => arr.every((el) => entryMatchKey(el) !== null);- if (!keyed(localVal) || !keyed(remoteVal) || (localVal.length === 0 && remoteVal.length === 0)) return null;- const lMap = new Map(localVal.map((el) => [entryMatchKey(el), el]));- const rMap = new Map(remoteVal.map((el) => [entryMatchKey(el), el]));+ if (localVal.length === 0 && remoteVal.length === 0) return null;+ const lMap = buildKeyedMap(localVal);+ const rMap = buildKeyedMap(remoteVal);+ if (!lMap || !rMap) return null; // identity-less or duplicate keys → whole-field diffreturn diffEntryMaps(lMap, rMap, (key, lv, rv) => entryLabel(lv ?? rv, key));}return null;diff --git a/server/lib/conflictJournal.test.js b/server/lib/conflictJournal.test.jsindex ee386967b3..ae0884cbc9 100644--- a/server/lib/conflictJournal.test.js+++ b/server/lib/conflictJournal.test.js@@ -113,16 +113,26 @@ describe('conflictJournal', () => {expect(byPath.mood.changed).toBe('remote-only'); // added on remote});- it('array of identity-bearing objects → paired by id, labelled by name', () => {+ it('array of identity-bearing objects → paired by id (path), labelled by name (label)', () => {const local = [{ id: 'c1', name: 'Alice', age: 30 }, { id: 'c2', name: 'Bob', age: 40 }];const remote = [{ id: 'c1', name: 'Alice', age: 31 }, { id: 'c3', name: 'Cara', age: 22 }];const parts = cj.deepFieldDiff(local, remote);- const byPath = Object.fromEntries(parts.map((p) => [p.path, p]));- // c1 changed (age) → labelled 'Alice'; c2 removed → 'Bob'; c3 added → 'Cara'.- expect(Object.keys(byPath).sort()).toEqual(['Alice', 'Bob', 'Cara']);- expect(byPath.Alice.changed).toBe('both');- expect(byPath.Bob.changed).toBe('local-only');- expect(byPath.Cara.changed).toBe('remote-only');+ // path = the unique match key (id); label = the friendly name.+ const byId = Object.fromEntries(parts.map((p) => [p.path, p]));+ expect(Object.keys(byId).sort()).toEqual(['c1', 'c2', 'c3']);+ expect(byId.c1).toMatchObject({ label: 'Alice', changed: 'both' });+ expect(byId.c2).toMatchObject({ label: 'Bob', changed: 'local-only' }); // removed+ expect(byId.c3).toMatchObject({ label: 'Cara', changed: 'remote-only' }); // added+ });++ it('distinct ids with a COLLIDING display name keep distinct (unique) paths', () => {+ // Two different characters both named "Alice" must not collapse — the+ // React key is `path` (the id), which stays unique even when labels clash.+ const local = [{ id: 'c1', name: 'Alice', bio: 'one' }, { id: 'c2', name: 'Alice', bio: 'two' }];+ const remote = [{ id: 'c1', name: 'Alice', bio: 'ONE' }, { id: 'c2', name: 'Alice', bio: 'TWO' }];+ const parts = cj.deepFieldDiff(local, remote);+ expect(parts.map((p) => p.path).sort()).toEqual(['c1', 'c2']);+ expect(parts.every((p) => p.label === 'Alice')).toBe(true);});it('reordering identity-bearing objects with no content change → null (no spurious parts)', () => {@@ -131,6 +141,12 @@ describe('conflictJournal', () => {expect(cj.deepFieldDiff(local, remote)).toBeNull();});+ it('duplicate match keys within an array → null (pairing unreliable, fall back to whole-field)', () => {+ const local = [{ id: 'dup', name: 'A' }, { id: 'dup', name: 'B' }];+ const remote = [{ id: 'dup', name: 'C' }];+ expect(cj.deepFieldDiff(local, remote)).toBeNull();+ });+it('returns null for scalars, arrays of scalars, identity-less arrays, and shape mismatches', () => {expect(cj.deepFieldDiff('a', 'b')).toBeNull();expect(cj.deepFieldDiff(['x', 'y'], ['x', 'z'])).toBeNull();@@ -164,11 +180,12 @@ describe('conflictJournal', () => {// scalar field → whole-field values, no parts.expect(byField.starterPrompt.parts).toBeUndefined();expect(byField.starterPrompt.localValue).toBe('LOCAL prompt');- // object-map field → parts keyed by category.+ // object-map field → parts keyed by category (path = label = key).expect(byField.categories.localValue).toBeUndefined();expect(byField.categories.parts.map((p) => p.path)).toEqual(['tone']);- // array-of-objects field → parts labelled by name.- expect(byField.characters.parts.map((p) => p.path)).toEqual(['Alice']);+ // array-of-objects field → path is the id, label is the name.+ expect(byField.characters.parts.map((p) => p.path)).toEqual(['c1']);+ expect(byField.characters.parts.map((p) => p.label)).toEqual(['Alice']);expect(byField.characters.parts[0].remoteValue.bio).toBe('REMOTE bio');});