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: surface and restore a universe `factual` conflict instead of journaling a diff that can't name it

[._.]/ Adam Eivy1ae9a7b61f92
diff --git a/server/lib/conflictJournal.js b/server/lib/conflictJournal.js
index e9018b420a..a6d47bfd84 100644
--- a/server/lib/conflictJournal.js
+++ b/server/lib/conflictJournal.js
@@ -460,13 +460,30 @@ export async function detectConflict({ kind, id, local, remote }) {
return { isConflict, baseHash, localHash, remoteHash, baseUnavailable: _persistBlocked };
}
+// Restorable fields whose ABSENCE from a wire snapshot is meaningful state
+// rather than "not captured". `sanitizeTemplate` persists universe `factual`
+// only when true, so a fiction snapshot simply omits the key — and a restore
+// that picks fields with a plain `in` check would skip it, silently turning
+// "restore this universe to fiction" into a no-op while the true-direction
+// restore worked. The resolver fills in the explicit `false`, which
+// `sanitizeTemplate` then drops back to absent.
+export const ABSENT_MEANS_FALSE_FIELDS = Object.freeze({
+ universe: Object.freeze(['factual']),
+});
+
// User-authored content fields a restore/merge may write back, per kind. This
// is the SINGLE SOURCE OF TRUTH — the resolver imports it for its merge-fields
// allowlist, and diffSummary filters to it so the Conflicts UI only ever offers
// fields the resolver will accept (server-owned fields like id/createdAt/
// schemaVersion/locked/origin/deleted are neither restorable nor shown).
export const RESTORABLE_FIELDS = Object.freeze({
- universe: ['name', 'starterPrompt', 'logline', 'premise', 'styleNotes', 'categories', 'compositeSheets', 'influences', 'characters', 'places', 'objects'],
+ // `factual` (#7616) is here because it is FEDERATED and therefore hashed by
+ // contentHashForRecord — a peer flipping the factual/fiction axis is a real
+ // 3-way divergence. Omitting it would journal that conflict with a diff that
+ // cannot mention the field (the phantom-divergence trap `styleImageRefs`
+ // avoids by being stripped from the wire instead) and leave the user no way
+ // to restore it. See ABSENT_MEANS_FALSE_FIELDS for the clear direction.
+ universe: ['name', 'starterPrompt', 'logline', 'premise', 'styleNotes', 'categories', 'compositeSheets', 'influences', 'characters', 'places', 'objects', 'factual'],
series: ['name', 'logline', 'premise', 'styleNotes', 'styleGuide', 'titleLogo', 'author', 'stylePromptOverride', 'stylePromptOverrideMode', 'targetFormat', 'issueCountTarget', 'arc', 'seasons'],
// mediaCollection restores only the user-authored content scalars. `items`
// are union-merged (never lost, nothing to restore); `universeId`/`seriesId`
diff --git a/server/lib/conflictJournal.test.js b/server/lib/conflictJournal.test.js
index 0639543a66..264699dd0b 100644
--- a/server/lib/conflictJournal.test.js
+++ b/server/lib/conflictJournal.test.js
@@ -108,6 +108,27 @@ describe('conflictJournal', () => {
expect(await cj.getSyncBaseHash('universe', 'u-1')).toBe(cj.contentHashForRecord('universe', remote));
});
+ it('a universe divergence on `factual` alone is journaled WITH the field in the diff', async () => {
+ // Regression (#7616): `factual` federates, so it feeds the wire projection
+ // contentHashForRecord hashes — a peer flipping the factual/fiction axis is
+ // a real 3-way divergence. When RESTORABLE_FIELDS.universe omitted it, that
+ // conflict journaled with an EMPTY diffSummary: the Conflicts UI showed a
+ // conflict the user could neither see nor resolve, and the flag this field
+ // exists to protect was lost to LWW in silence.
+ const base = uni();
+ await cj.setSyncBaseHash('universe', 'u-1', cj.contentHashForRecord('universe', base));
+ const local = uni({ factual: true, updatedAt: '2026-05-02T00:00:00Z' });
+ const remote = uni({ starterPrompt: 'REMOTE edit', updatedAt: '2026-05-03T00:00:00Z' });
+
+ await cj.maybeJournalBeforeOverwrite({ kind: 'universe', id: 'u-1', local, remote, source: { via: 'sync' } });
+ const [entry] = await pendingEntries();
+ const factualDiff = entry.diffSummary.find((d) => d.field === 'factual');
+ expect(factualDiff).toBeDefined();
+ expect(factualDiff.localValue).toBe(true);
+ // Absent on the remote side — fiction is the no-key shape sanitizeTemplate writes.
+ expect(factualDiff.remoteValue).toBeUndefined();
+ });
+
it('idempotent snapshot replay does not create a second entry', async () => {
const base = uni({ starterPrompt: 'base' });
await cj.setSyncBaseHash('universe', 'u-1', cj.contentHashForRecord('universe', base));
diff --git a/server/services/conflictJournalResolver.js b/server/services/conflictJournalResolver.js
index 09565d33b6..e4cb514d1e 100644
--- a/server/services/conflictJournalResolver.js
+++ b/server/services/conflictJournalResolver.js
@@ -30,7 +30,7 @@
import { existsSync } from 'fs';
import { isSafeRecordId } from '../lib/validation.js';
-import { conflictJournalStore, RESTORABLE_FIELDS } from '../lib/conflictJournal.js';
+import { ABSENT_MEANS_FALSE_FIELDS, conflictJournalStore, RESTORABLE_FIELDS } from '../lib/conflictJournal.js';
import { updateUniverse, ERR_NOT_FOUND as UNIVERSE_NOT_FOUND } from './universeBuilder.js';
import { updateSeries, ERR_NOT_FOUND as SERIES_NOT_FOUND } from './pipeline/series.js';
import { updateCollection, getCollection, ERR_NOT_FOUND as COLLECTION_NOT_FOUND } from './mediaCollections.js';
@@ -55,9 +55,18 @@ const makeErr = (message, code) => Object.assign(new Error(message), { code });
// per kind; id/createdAt/server-owned fields are never overlaid) is owned by
// conflictJournal.js so diffSummary and this validator stay in lockstep.
-const pick = (obj, fields) => {
+// `kind` is needed for ABSENT_MEANS_FALSE_FIELDS: a snapshot field that is
+// only persisted when true (universe `factual`) is simply ABSENT when false,
+// so an `in` check alone would drop it and make the clear-direction restore a
+// silent no-op. Filling the explicit `false` restores it faithfully; the
+// record sanitizer drops that back to absent on write.
+const pick = (obj, fields, kind) => {
+ const falseWhenAbsent = ABSENT_MEANS_FALSE_FIELDS[kind] || [];
const out = {};
- for (const f of fields) if (obj && f in obj) out[f] = obj[f];
+ for (const f of fields) {
+ if (obj && f in obj) out[f] = obj[f];
+ else if (falseWhenAbsent.includes(f)) out[f] = false;
+ }
return out;
};
@@ -211,14 +220,14 @@ export async function resolveConflict(id, { action, fields = [] } = {}) {
if (action === 'restore-all') {
// Faithful restore: replace the keyed fields (universe categories) wholesale.
- await applyToRecord(entry.recordKind, entry.recordId, pick(snapshot, allowed), { replace: true });
+ await applyToRecord(entry.recordKind, entry.recordId, pick(snapshot, allowed, entry.recordKind), { 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);
}
const invalid = fields.filter((f) => !allowed.includes(f));
if (invalid.length) throw makeErr(`Not restorable: ${invalid.join(', ')}`, ERR_VALIDATION);
- await applyToRecord(entry.recordKind, entry.recordId, pick(snapshot, fields));
+ await applyToRecord(entry.recordKind, entry.recordId, pick(snapshot, fields, entry.recordKind));
} else if (action !== 'discard') {
throw makeErr(`Unknown resolution action: ${action}`, ERR_VALIDATION);
}
diff --git a/server/services/conflictJournalResolver.test.js b/server/services/conflictJournalResolver.test.js
index 907643177a..cd113c3880 100644
--- a/server/services/conflictJournalResolver.test.js
+++ b/server/services/conflictJournalResolver.test.js
@@ -46,6 +46,24 @@ describe('conflictJournalResolver', () => {
cj.__resetBaseHashCacheForTests();
});
+ it('restores universe `factual` in BOTH directions, including the absent-means-false clear', async () => {
+ // Regression (#7616): sanitizeTemplate persists `factual` only when true, so
+ // a fiction snapshot simply has no key. `pick`'s plain `in` check dropped it,
+ // which made the true→fiction restore a silent no-op while the fiction→true
+ // direction worked — the user hit Restore, saw success, and the universe
+ // stayed misclassified as real. ABSENT_MEANS_FALSE_FIELDS fills the explicit
+ // false; the sanitizer drops that back to absent on write.
+ const setOn = await universeSvc.createUniverse({ name: 'Reality', factual: true });
+ const clearEntry = await seedEntry(setOn.id, { id: setOn.id, name: 'Reality' });
+ await resolver.resolveConflict(clearEntry, { action: 'restore-all' });
+ expect((await universeSvc.getUniverse(setOn.id)).factual).toBeUndefined();
+
+ const setOff = await universeSvc.createUniverse({ name: 'Clandestiny' });
+ const markEntry = await seedEntry(setOff.id, { id: setOff.id, name: 'Clandestiny', factual: true });
+ await resolver.resolveConflict(markEntry, { action: 'merge-fields', fields: ['factual'] });
+ expect((await universeSvc.getUniverse(setOff.id)).factual).toBe(true);
+ });
+
it('restore-all re-applies the archived snapshot content (bumps updatedAt)', async () => {
const u = await universeSvc.createUniverse({ name: 'Clandestiny', starterPrompt: 'REMOTE won' });
const entryId = await seedEntry(u.id, { id: u.id, name: 'Clandestiny', starterPrompt: 'MY local prompt' });