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([issue-851]): promote draft universe when a committed series is linked

[._.]/ Adam Eivy49433d354490
diff --git a/server/services/importer.test.js b/server/services/importer.test.js
index 8755756ed6..58ab166663 100644
--- a/server/services/importer.test.js
+++ b/server/services/importer.test.js
@@ -1678,3 +1678,88 @@ describe('mergeSeasons (pure helper)', () => {
expect(caught.message).toMatch(/duplicate/i);
});
});
+
+// ---------------------------------------------------------------------------
+// Reconcile draft parent universe (issue #851)
+//
+// An import-draft universe (ephemeral + importDraft, created by analyzeImport)
+// must be promoted to a normal syncing record when a COMMITTED (non-draft)
+// series is linked to it WITHOUT going through commitImport — otherwise the
+// universe holds real work but silently never syncs. The promotion mirrors
+// commitImport: clear BOTH importDraft and ephemeral. It must NOT fire when a
+// draft series is linked (that's commitImport's job) or when the parent isn't
+// a draft.
+// ---------------------------------------------------------------------------
+describe('reconcile draft parent universe (issue #851)', () => {
+ it('promotes a draft universe when a committed series is linked via updateSeries', async () => {
+ // Draft universe shell, exactly as analyzeImport would stamp it.
+ const draftUni = await universeSvc.createUniverse({
+ name: 'Draft Parent', ephemeral: true, importDraft: true,
+ });
+ // A separate, committed (non-draft) universe the series starts under.
+ const realUni = await universeSvc.createUniverse({ name: 'Committed Home' });
+ const committedSeries = await seriesSvc.createSeries({ name: 'Real Work', universeId: realUni.id });
+ expect(committedSeries.importDraft).not.toBe(true);
+
+ // Manually re-home the committed series onto the draft universe.
+ await seriesSvc.updateSeries(committedSeries.id, { universeId: draftUni.id });
+
+ const promoted = await universeSvc.getUniverse(draftUni.id);
+ expect(promoted.importDraft).not.toBe(true);
+ expect(promoted.ephemeral).not.toBe(true);
+ });
+
+ it('promotes a draft universe when a committed series is CREATED already linked to it', async () => {
+ const draftUni = await universeSvc.createUniverse({
+ name: 'Draft Parent 2', ephemeral: true, importDraft: true,
+ });
+ await seriesSvc.createSeries({ name: 'Born Committed', universeId: draftUni.id });
+
+ const promoted = await universeSvc.getUniverse(draftUni.id);
+ expect(promoted.importDraft).not.toBe(true);
+ expect(promoted.ephemeral).not.toBe(true);
+ });
+
+ it('does NOT promote a draft universe when a DRAFT series is linked (commitImport owns that)', async () => {
+ const draftUni = await universeSvc.createUniverse({
+ name: 'Still Draft', ephemeral: true, importDraft: true,
+ });
+ // A draft-shell series, as analyzeImport stamps it — linking it must leave
+ // the universe a draft so the orphan GC + commitImport keep their contract.
+ const draftSeries = await seriesSvc.createSeries({
+ name: 'Draft Child', universeId: draftUni.id, ephemeral: true, importDraft: true,
+ });
+ expect(draftSeries.importDraft).toBe(true);
+
+ const after = await universeSvc.getUniverse(draftUni.id);
+ expect(after.importDraft).toBe(true);
+ expect(after.ephemeral).toBe(true);
+ });
+
+ it('leaves a private (ephemeral-only, non-draft) universe untouched when a committed series is linked', async () => {
+ // `ephemeral` alone is a user's "keep local" choice — linking a series must
+ // not un-privatize it. Only `importDraft` parents get promoted.
+ const privateUni = await universeSvc.createUniverse({ name: 'Private', ephemeral: true });
+ expect(privateUni.importDraft).not.toBe(true);
+ const realUni = await universeSvc.createUniverse({ name: 'Elsewhere' });
+ const series = await seriesSvc.createSeries({ name: 'A Series', universeId: realUni.id });
+
+ await seriesSvc.updateSeries(series.id, { universeId: privateUni.id });
+
+ const after = await universeSvc.getUniverse(privateUni.id);
+ expect(after.ephemeral).toBe(true);
+ expect(after.importDraft).not.toBe(true);
+ });
+
+ it('leaves a non-draft universe untouched (no spurious write) when a committed series links to it', async () => {
+ const normalUni = await universeSvc.createUniverse({ name: 'Normal' });
+ const realUni = await universeSvc.createUniverse({ name: 'Origin' });
+ const series = await seriesSvc.createSeries({ name: 'Moving Series', universeId: realUni.id });
+
+ await seriesSvc.updateSeries(series.id, { universeId: normalUni.id });
+
+ const after = await universeSvc.getUniverse(normalUni.id);
+ expect(after.importDraft).not.toBe(true);
+ expect(after.ephemeral).not.toBe(true);
+ });
+});
diff --git a/server/services/pipeline/series.js b/server/services/pipeline/series.js
index 6006103f8c..b94b3ec081 100644
--- a/server/services/pipeline/series.js
+++ b/server/services/pipeline/series.js
@@ -243,6 +243,15 @@ export async function createSeries(input = {}) {
console.log(`⚠️ series: auto-subscribe after create failed: ${err.message}`);
});
}
+ // Reconcile a draft parent universe (issue #851) when a committed (non-draft)
+ // series is created already linked to it — the create-time twin of the
+ // updateSeries link path. Gated on `importDraft !== true` so analyzeImport's
+ // own draft-shell series (created WITH importDraft) never promotes its draft
+ // universe here; commitImport owns that promotion. See
+ // reconcileDraftParentUniverse for the contract.
+ if (created.universeId && created.importDraft !== true) {
+ await reconcileDraftParentUniverse(created.universeId);
+ }
return created;
}
@@ -289,6 +298,35 @@ export async function insertSeriesWithId(input = {}) {
return next;
}
+/**
+ * Promote an import-draft universe to a normal, syncing record when a
+ * committed (non-draft) series gets linked to it outside the commitImport
+ * path (issue #851). Mirrors commitImport's promotion: clears BOTH
+ * `importDraft` and `ephemeral` through `updateUniverse` so the
+ * ephemeral→non-ephemeral peer re-subscribe wiring fires.
+ *
+ * Gated STRICTLY on `importDraft === true` — a user's deliberately-private
+ * (`ephemeral`-only) universe must never be un-privatized as a side effect
+ * of linking a series, exactly as commitImport gates its own promotion.
+ *
+ * Best-effort: runs outside any write queue and swallows + logs failures so a
+ * universe-side error never fails the series link that triggered it. The
+ * dynamic import dodges the static cycle (universeBuilder imports listSeries
+ * from this module).
+ */
+async function reconcileDraftParentUniverse(universeId) {
+ await import('../universeBuilder.js')
+ .then(async ({ getUniverse, updateUniverse }) => {
+ const universe = await getUniverse(universeId).catch(() => null);
+ if (universe?.importDraft !== true) return;
+ await updateUniverse(universeId, { ephemeral: false, importDraft: false });
+ console.log(`🔗 series: promoted import-draft universe ${universeId.slice(0, 8)} — committed series linked outside commitImport`);
+ })
+ .catch((err) => {
+ console.log(`⚠️ series: reconcile draft parent universe ${universeId.slice(0, 8)} failed: ${err.message}`);
+ });
+}
+
export async function updateSeries(id, patch = {}) {
// Pre-B.4 canon (characters/settings/objects) lives on the universe, not the
// series — but a stale browser tab can still POST a legacy series shape and
@@ -298,7 +336,7 @@ export async function updateSeries(id, patch = {}) {
if (legacyFields.length > 0) {
console.warn(`⚠️ series PATCH ${id.slice(0, 8)} stripped legacy canon fields: ${legacyFields.join(', ')}`);
}
- const { merged, nameChanged, prevEphemeral, nextEphemeral } = await store().queueRecordWrite(id, async () => {
+ const { merged, nameChanged, prevEphemeral, nextEphemeral, linkedUniverseId } = await store().queueRecordWrite(id, async () => {
const cur = await store().loadOne(id);
if (!cur) throw makeErr(`Series not found: ${id}`, ERR_NOT_FOUND);
if (cur.deleted) throw makeErr(`Series not found: ${id}`, ERR_NOT_FOUND);
@@ -346,6 +384,13 @@ export async function updateSeries(id, patch = {}) {
});
if (!next) throw makeErr('Invalid series payload', ERR_VALIDATION);
await store().saveOneNow(next.id, next);
+ // Surface the universe this series ends up linked to ONLY when (a) this
+ // PATCH set/changed the link and (b) the series itself is NOT an import
+ // draft (committed work). The post-queue side effect reconciles a draft
+ // parent universe (issue #851) — see below. Gating on `importDraft !==
+ // true` is the load-bearing rule: an import-draft series linked to a
+ // draft universe must NOT promote it (that's commitImport's job).
+ const linkChanged = 'universeId' in patch && next.universeId && next.universeId !== cur.universeId;
return {
merged: next,
nameChanged: next.name !== cur.name,
@@ -353,6 +398,7 @@ export async function updateSeries(id, patch = {}) {
// side effects can wire subscribe / unsubscribe.
prevEphemeral: cur.ephemeral === true,
nextEphemeral: next.ephemeral === true,
+ linkedUniverseId: (linkChanged && next.importDraft !== true) ? next.universeId : null,
};
});
// Ephemeral lifecycle wiring — see updateUniverse for the rationale. false→true
@@ -387,6 +433,19 @@ export async function updateSeries(id, patch = {}) {
console.error(`❌ series-collection rename cascade failed for ${merged.id}: ${err?.message || err}`);
});
}
+ // Reconcile a draft parent universe (issue #851). When a committed (non-
+ // draft) series is linked to an import-draft universe WITHOUT going through
+ // commitImport, the universe stays `ephemeral` + `importDraft` forever and
+ // silently never syncs — even though it now holds real committed work. Mirror
+ // commitImport's promotion exactly: clear BOTH flags via updateUniverse so
+ // the ephemeral→non-ephemeral peer re-subscribe fires and the now-real
+ // universe reaches every peer. Best-effort + outside the series write queue
+ // (it touches a different record); a failure must not fail the series link.
+ // Dynamic import to dodge the static cycle (universeBuilder imports this
+ // module's listSeries).
+ if (linkedUniverseId) {
+ await reconcileDraftParentUniverse(linkedUniverseId);
+ }
emitRecordUpdated('series', merged.id);
return merged;
}