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

refactor([merge-cascade-no-rollback]): drop unused repointed field, share cascade-context builder

[._.]/ Adam Eivy098b7492edc1
diff --git a/server/routes/pipeline.js b/server/routes/pipeline.js
index fcf6b72a23..e4b0f194b4 100644
--- a/server/routes/pipeline.js
+++ b/server/routes/pipeline.js
@@ -60,7 +60,7 @@ import {
import { extractCanonFromProse, summarizeCanonExtraction } from '../services/universeCanon.js';
import { getSeriesCanon } from '../services/pipeline/seriesCanon.js';
import { findDuplicateSeriesGroups, findSameNameSeries } from '../services/duplicateDetection.js';
-import { mergeSeries, ERR_CASCADE as MERGE_CASCADE_INCOMPLETE_CODE } from '../services/recordMerge.js';
+import { mergeSeries, buildCascadeContext } from '../services/recordMerge.js';
import { mergeFieldsWithAI } from '../services/recordMergeAI.js';
import { startEpisodeVideoForIssue, ERR_NO_STORYBOARDS } from '../services/pipeline/episodeVideo.js';
import { generateSeriesTitleLogo } from '../services/pipeline/seriesTitleLogo.js';
@@ -143,10 +143,7 @@ const mapServiceError = (err) => {
if (status) {
// An incomplete merge cascade forwards the survivor/loser ids + which step
// failed so the UI can tell the user exactly what didn't move.
- const context = err?.code === MERGE_CASCADE_INCOMPLETE_CODE
- ? { survivorId: err.survivorId, loserId: err.loserId, failed: err.failed }
- : undefined;
- return new ServerError(err.message, { status, code: err.code, context });
+ return new ServerError(err.message, { status, code: err.code, context: buildCascadeContext(err) });
}
return err;
};
diff --git a/server/routes/universeBuilder.js b/server/routes/universeBuilder.js
index f1e2240a75..00ea1000fc 100644
--- a/server/routes/universeBuilder.js
+++ b/server/routes/universeBuilder.js
@@ -34,7 +34,7 @@ import { enqueueJob } from '../services/mediaJobQueue/index.js';
import { getSettings } from '../services/settings.js';
import { findOrCreateUniverseCollection } from '../services/mediaCollections.js';
import { findDuplicateUniverseGroups, findSameNameUniverses } from '../services/duplicateDetection.js';
-import { mergeUniverses, ERR_CASCADE as MERGE_CASCADE_INCOMPLETE_CODE } from '../services/recordMerge.js';
+import { mergeUniverses, buildCascadeContext } from '../services/recordMerge.js';
import { mergeFieldsWithAI } from '../services/recordMergeAI.js';
import { registerUniverseBuilderRun } from '../services/universeBuilderCollectionHook.js';
import { getImageModels, isFlux2 } from '../lib/mediaModels.js';
@@ -63,12 +63,10 @@ const mapServiceError = (err) => {
if (status) {
// Propagate diagnostic context onto the response body via `context`: the
// blocking-series list for a delete-guard 409, or the survivor/loser ids +
- // which children re-pointed vs. failed for an incomplete merge cascade.
+ // which children failed to re-point for an incomplete merge cascade.
const context = err?.blockingSeries
? { blockingSeries: err.blockingSeries }
- : err?.code === MERGE_CASCADE_INCOMPLETE_CODE
- ? { survivorId: err.survivorId, loserId: err.loserId, repointed: err.repointed, failed: err.failed }
- : undefined;
+ : buildCascadeContext(err);
return new ServerError(err.message, { status, code: err.code, context });
}
return err;
diff --git a/server/services/recordMerge.js b/server/services/recordMerge.js
index 82d02d2906..91b6400ac7 100644
--- a/server/services/recordMerge.js
+++ b/server/services/recordMerge.js
@@ -51,24 +51,30 @@ export const ERR_VALIDATION = 'MERGE_VALIDATION';
// it converges cleanly because every step is idempotent: buildXUnion dedupes
// its list unions, the child re-point / issue-reassign only touch records still
// pointing at the loser, and the survivor write is a verbatim overwrite. Both
-// routers map it to 409 and forward its `{ survivorId, loserId, repointed,
-// failed }` context so the UI can name exactly which children are stuck.
+// routers map it to 409 and forward its `{ survivorId, loserId, failed }`
+// context (via `buildCascadeContext`) so the UI can name which children stuck.
export const ERR_CASCADE = 'MERGE_CASCADE_INCOMPLETE';
const makeErr = (message, code) => Object.assign(new Error(message), { code });
-// Build the resumable cascade error. `failed` is `[{ id, name, error, step? }]`;
-// `repointed` (universes only) is the children that DID move on this pass.
-const cascadeError = (kind, survivorId, loserId, failed, repointed = []) =>
+// Build the resumable cascade error. `failed` is `[{ id, name, error, step? }]`.
+const cascadeError = (kind, survivorId, loserId, failed) =>
Object.assign(
new Error(
`${kind} merge could not complete: ${failed.length} cascade step(s) failed ` +
`(${failed.map((f) => f.name || f.id).join(', ')}). The survivor already holds the ` +
`merged data and the loser was left intact — resolve the failure and re-run the merge to finish.`,
),
- { code: ERR_CASCADE, survivorId, loserId, failed, repointed },
+ { code: ERR_CASCADE, survivorId, loserId, failed },
);
+// Extract the response `context` both routers forward for a cascade 409, so the
+// shape stays identical across `universeBuilder.js` and `pipeline.js`.
+export const buildCascadeContext = (err) =>
+ err?.code === ERR_CASCADE
+ ? { survivorId: err.survivorId, loserId: err.loserId, failed: err.failed }
+ : undefined;
+
// ---- pure union helpers (exported for unit tests) ----
const lc = (s) => (typeof s === 'string' ? s.trim().toLowerCase() : '');
@@ -379,15 +385,13 @@ export async function mergeUniverses(survivorId, loserId, fieldChoices = {}, { d
// surface a resumable cascade error (the survivor already holds the union;
// re-running re-points only the children still under the loser, then
// tombstones). Best-effort-tombstoning instead would orphan those children.
- const repointed = [];
const repointFailed = [];
for (const s of childSeries) {
await updateSeries(s.id, { universeId: survivorId })
- .then(() => repointed.push({ id: s.id, name: s.name }))
.catch((err) => repointFailed.push({ id: s.id, name: s.name, error: err?.message || String(err) }));
}
if (repointFailed.length > 0) {
- throw cascadeError('Universe', survivorId, loserId, repointFailed, repointed);
+ throw cascadeError('Universe', survivorId, loserId, repointFailed);
}
// 3. Fold the loser's auto-collection into the survivor's. The deterministic