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([series-merge-season-rehoming]): build season map from the persisted survivor

[._.]/ Adam Eivyaf6115adaa7a
diff --git a/server/services/recordMerge.js b/server/services/recordMerge.js
index 2ac91efa10..431d62841b 100644
--- a/server/services/recordMerge.js
+++ b/server/services/recordMerge.js
@@ -416,18 +416,23 @@ export async function mergeSeries(survivorId, loserId, fieldChoices = {}, { dryR
}
requireResolved(conflicts);
- // 1. Write the unioned survivor.
- await updateSeries(survivorId, record);
+ // 1. Write the unioned survivor. Capture the PERSISTED record — `updateSeries`
+ // runs `sanitizeSeasonList` (which caps at SEASONS_PER_SERIES_MAX), so the
+ // saved seasons can differ from the in-memory union; the season map below
+ // must pair against what actually landed on disk.
+ const persistedSurvivor = await updateSeries(survivorId, record);
// 2. Re-point the loser's issues to the survivor before tombstone, preserving
- // season grouping. The unioned `record.seasons` carries a season for every
- // loser season number (a number collision gap-fills the survivor's
- // same-number season; a non-colliding loser season is appended verbatim),
- // so pair each loser season to the survivor season sharing its number. An
- // issue whose loser season has no number, or maps nowhere, lands un-grouped.
+ // season grouping. The persisted survivor carries a season for every loser
+ // season number that survived the union (a number collision gap-fills the
+ // survivor's same-number season; a non-colliding loser season is appended
+ // verbatim), so pair each loser season to the survivor season sharing its
+ // number. An issue whose loser season has no number, or maps to a season
+ // that didn't persist (e.g. dropped at the season cap), lands un-grouped —
+ // the map only ever holds ids that exist on the saved survivor.
if (loserIssues.length > 0) {
const survivorSeasonIdByNumber = new Map(
- (record.seasons || [])
+ (persistedSurvivor.seasons || [])
.filter((s) => Number.isFinite(s?.number) && isStr(s?.id))
.map((s) => [s.number, s.id]),
);
diff --git a/server/services/recordMerge.test.js b/server/services/recordMerge.test.js
index ef698b6a3d..9901e8895b 100644
--- a/server/services/recordMerge.test.js
+++ b/server/services/recordMerge.test.js
@@ -292,6 +292,29 @@ describe('recordMerge — mergeSeries (integration)', () => {
expect(merged.seasons.map((s) => s.number).sort()).toEqual([1, 2]);
});
+ it('drops an over-cap loser season from the map so its issues land un-grouped (not dangling)', async () => {
+ const u = await universeSvc.createUniverse({ name: 'U' });
+ // Survivor already at the 50-season cap (numbers 1..50).
+ const survSeasons = Array.from({ length: 50 }, (_, i) => ({ id: `sea-s${i + 1}`, number: i + 1, title: `S${i + 1}` }));
+ const survivor = await seriesSvc.createSeries({ name: 'Big', universeId: u.id, seasons: survSeasons });
+ // Loser adds one NON-colliding season (number 99) — it can't survive the
+ // union's season cap, so it must never end up in the season map.
+ const loser = await seriesSvc.createSeries({ name: 'Big', universeId: u.id, seasons: [{ id: 'sea-overflow', number: 99, title: 'Overflow' }] });
+ const loserSeason = (await seriesSvc.getSeries(loser.id)).seasons.find((s) => s.number === 99);
+ const iss = await issuesSvc.createIssue({ seriesId: loser.id, title: 'Overflow Issue' });
+ await issuesSvc.updateIssue(iss.id, { seasonId: loserSeason.id });
+
+ await merge.mergeSeries(survivor.id, loser.id, {});
+
+ // The survivor stays at the cap; the number-99 season never persisted.
+ const merged = await seriesSvc.getSeries(survivor.id);
+ expect(merged.seasons).toHaveLength(50);
+ expect(merged.seasons.some((s) => s.number === 99)).toBe(false);
+ // The moved issue maps to no persisted season → un-grouped, not a dangling ref.
+ const [moved] = (await issuesSvc.listIssues({ seriesId: survivor.id })).filter((i) => i.title === 'Overflow Issue');
+ expect(moved.seasonId).toBeNull();
+ });
+
it('rejects merging series from different universes', async () => {
const u1 = await universeSvc.createUniverse({ name: 'U1' });
const u2 = await universeSvc.createUniverse({ name: 'U2' });