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: converge autobiography usedPrompts on order, and drop peer stories with no id (#3531)

[._.]/ Adam Eivy25017af401c0
diff --git a/server/services/autobiography.tombstone.test.js b/server/services/autobiography.tombstone.test.js
index b83c4f37a8..4e57cbf31d 100644
--- a/server/services/autobiography.tombstone.test.js
+++ b/server/services/autobiography.tombstone.test.js
@@ -146,6 +146,21 @@ describe('mergeAutobiographyStories tombstones (#3531)', () => {
expect(mergeAutobiographyStories(local, remote).changed).toBe(false);
});
+ it('rewrites a merely-unsorted local usedPrompts so two peers converge on one checksum', () => {
+ // saveStory appends in write order, so the on-disk list is often unsorted —
+ // a length-only comparison would call this a no-op and never converge.
+ const local = { stories: [], usedPrompts: ['family-0', 'childhood-0'], deletedStories: [] };
+ const { merged, changed } = mergeAutobiographyStories(local, { stories: [], usedPrompts: ['childhood-0'], deletedStories: [] });
+ expect(changed).toBe(true);
+ expect(merged.usedPrompts).toEqual(['childhood-0', 'family-0']);
+ });
+
+ it('drops a peer story with no usable id rather than keying the union on undefined', () => {
+ const local = { stories: [], usedPrompts: [], deletedStories: [] };
+ const remote = { stories: [{ content: 'no id' }, { id: '', content: 'empty id' }], usedPrompts: [], deletedStories: [] };
+ expect(mergeAutobiographyStories(local, remote)).toEqual({ merged: { ...local, stories: [], usedPrompts: [], deletedStories: [] }, changed: false });
+ });
+
it('tolerates a legacy peer that sends no tombstone key at all', () => {
const local = { stories: [{ id: 's1', createdAt: '2026-01-01T00:00:00.000Z' }], usedPrompts: [] };
const { merged, changed } = mergeAutobiographyStories(local, { stories: [{ id: 's1', createdAt: '2026-01-01T00:00:00.000Z' }], usedPrompts: [] });
diff --git a/server/services/digital-twin-sync.js b/server/services/digital-twin-sync.js
index 49d36efd04..7cae31aa0e 100644
--- a/server/services/digital-twin-sync.js
+++ b/server/services/digital-twin-sync.js
@@ -690,7 +690,10 @@ export function mergeAutobiographyStories(local, remote) {
const byId = new Map((Array.isArray(local.stories) ? local.stories : []).map((s) => [s.id, s]));
for (const rs of Array.isArray(remote.stories) ? remote.stories : []) {
- if (!isPlainObject(rs)) continue;
+ // A peer story without a usable id would key the map on `undefined`, so a
+ // second malformed one silently overwrites the first and neither can ever be
+ // tombstoned (the tombstone key would be `undefined` too). Drop them.
+ if (!isPlainObject(rs) || typeof rs.id !== 'string' || rs.id === '') continue;
const ls = byId.get(rs.id);
if (!ls) { byId.set(rs.id, rs); continue; }
const lt = ls.updatedAt || ls.createdAt || '';
@@ -704,7 +707,12 @@ export function mergeAutobiographyStories(local, remote) {
const localUsed = Array.isArray(local.usedPrompts) ? local.usedPrompts : [];
const usedPrompts = [...new Set([...localUsed, ...(Array.isArray(remote.usedPrompts) ? remote.usedPrompts : [])])].sort();
- let changed = usedPrompts.length !== localUsed.length;
+ // Compare CONTENT, not just length: saveStory appends prompt ids in write
+ // order, so a local list that is merely unsorted has the same length as the
+ // sorted union and would never be written back — leaving two peers with the
+ // same prompts emitting different JSON, different checksums, and a sync that
+ // retries forever (the same convergence trap the story sort above avoids).
+ let changed = usedPrompts.length !== localUsed.length || usedPrompts.some((p, i) => p !== localUsed[i]);
// Prune against the same live stamp the filter used, or a story kept alive by
// an edit would leave its tombstone behind to retry on every cycle.