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([merge-cascade-no-rollback]): repair survivor numbering on resume after a partial issue reassign

[._.]/ Adam Eivye4584768f865
diff --git a/server/services/recordMerge.js b/server/services/recordMerge.js
index 91b6400ac7..62984691be 100644
--- a/server/services/recordMerge.js
+++ b/server/services/recordMerge.js
@@ -32,7 +32,7 @@ import {
import { mergeExtractedBible, BIBLE_KIND, isStr } from '../lib/storyBible.js';
import { canonicalStringify, isEmptyScalar } from '../lib/objects.js';
import { getSeries, updateSeries, deleteSeries, listSeries } from './pipeline/series.js';
-import { reassignIssuesToSeries, listIssues } from './pipeline/issues.js';
+import { reassignIssuesToSeries, recomputeIssueNumbersForSeries, listIssues } from './pipeline/issues.js';
import {
findCollectionByUniverseId, findOrCreateUniverseCollection,
findCollectionBySeriesId, findOrCreateSeriesCollection,
@@ -485,17 +485,28 @@ export async function mergeSeries(survivorId, loserId, fieldChoices = {}, { dryR
const survivorSeasonId = survivorSeasonIdByNumber.get(ls.number);
if (survivorSeasonId) seasonIdMap[ls.id] = survivorSeasonId;
}
- // The reassign is one queued write that persists nothing until it succeeds,
- // so a failure leaves the issues fully under the loser. Abort BEFORE the
- // tombstone (deleting the loser now would orphan its un-moved issues) and
- // surface a resumable cascade error — the survivor already holds the union;
- // re-running reassigns only the issues still under the loser, then tombstones.
+ // The reassign moves the loser's issues onto the survivor and renumbers the
+ // combined set. Its per-issue saves are NOT atomic, so a failure can leave
+ // some issues already moved (and the survivor's numbering half-applied).
+ // Abort BEFORE the tombstone — deleting the loser now would orphan any
+ // un-moved issues — and surface a resumable cascade error: re-running moves
+ // whatever issues are still under the loser. Step 2.5 below repairs the
+ // survivor's numbering even when the partial move left zero issues to re-run.
await reassignIssuesToSeries(loserId, survivorId, { seasonIdMap }).catch((err) => {
throw cascadeError('Series', survivorId, loserId,
[{ id: loserId, name: loser.name, error: err?.message || String(err), step: 'reassign-issues' }]);
});
}
+ // 2.5. Repair the survivor's issue numbering before tombstoning. This is a
+ // no-op on the happy path (the reassign already renumbered), but a prior
+ // cascade that failed AFTER moving issues but BEFORE finishing the
+ // survivor renumber leaves zero loser issues to re-run — so the renumber
+ // above never fires on retry. Running it unconditionally here is
+ // idempotent (it skips the write when numbering is already contiguous)
+ // and closes that resumability gap.
+ await recomputeIssueNumbersForSeries(survivorId);
+
// 3. Fold the loser's series-collection into the survivor's, then tombstone it.
if (loserCollection && (loserCollection.items || []).length > 0) {
const survivorCollection = await findOrCreateSeriesCollection({ seriesId: survivorId, seriesName: record.name || survivor.name });
diff --git a/server/services/recordMerge.test.js b/server/services/recordMerge.test.js
index dd6cfd05aa..40694b43d2 100644
--- a/server/services/recordMerge.test.js
+++ b/server/services/recordMerge.test.js
@@ -370,6 +370,34 @@ describe('recordMerge — mergeSeries (integration)', () => {
await expect(seriesSvc.getSeries(loser.id)).rejects.toMatchObject({ code: seriesSvc.ERR_NOT_FOUND });
});
+ it('repairs survivor issue numbering on re-run when a prior reassign partially persisted', async () => {
+ const u = await universeSvc.createUniverse({ name: 'U' });
+ const survivor = await seriesSvc.createSeries({ name: 'Twin', universeId: u.id });
+ const loser = await seriesSvc.createSeries({ name: 'Twin', universeId: u.id });
+
+ // reassignIssuesToSeries persists per-issue (saveIssuesNow → Promise.all of
+ // per-record writes), so a mid-flight failure can leave the loser's issues
+ // already moved under the survivor but with the survivor renumber only
+ // half-applied — AND zero issues left under the loser. On re-run the
+ // `loserIssues.length > 0` branch is skipped, so without the step-2.5 repair
+ // the dirty numbering would never be fixed before the tombstone. Fabricate
+ // that exact post-partial-persist on-disk state (non-contiguous 1/5/9, loser
+ // empty) — a live Promise.all partial failure has non-deterministic save
+ // ordering, so reproducing the resulting state directly keeps this stable.
+ const store = issuesSvc.issueStore();
+ await store.saveOneNow('iss-surv-a', { id: 'iss-surv-a', seriesId: survivor.id, title: 'A', number: 1 });
+ await store.saveOneNow('iss-surv-b', { id: 'iss-surv-b', seriesId: survivor.id, title: 'B', number: 5 });
+ await store.saveOneNow('iss-surv-c', { id: 'iss-surv-c', seriesId: survivor.id, title: 'C', number: 9 });
+ expect(await issuesSvc.listIssues({ seriesId: loser.id })).toHaveLength(0);
+
+ const result = await merge.mergeSeries(survivor.id, loser.id, {});
+ expect(result.merged).toBe(true);
+ // Step 2.5 renumbered the survivor to a contiguous sequence and tombstoned the loser.
+ const survivorIssues = await issuesSvc.listIssues({ seriesId: survivor.id });
+ expect(survivorIssues.map((i) => i.number).sort((a, b) => a - b)).toEqual([1, 2, 3]);
+ await expect(seriesSvc.getSeries(loser.id)).rejects.toMatchObject({ code: seriesSvc.ERR_NOT_FOUND });
+ });
+
it('rejects merging series from different universes', async () => {
const u1 = await universeSvc.createUniverse({ name: 'U1' });
const u2 = await universeSvc.createUniverse({ name: 'U2' });