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

feat([series-merge-season-rehoming]): preserve issue season grouping on series merge

[._.]/ Adam Eivy0bf071f4eaf2
diff --git a/server/services/pipeline/issues.js b/server/services/pipeline/issues.js
index ec8c9f2054..f39e214df4 100644
--- a/server/services/pipeline/issues.js
+++ b/server/services/pipeline/issues.js
@@ -719,17 +719,20 @@ export function bulkReassignSeason(seriesId, fromSeasonId, toSeasonId = null, {
* series-merge engine (recordMerge.js) so a duplicate series' issues survive
* the merge instead of being orphaned under the tombstoned loser.
*
- * Moved issues land UN-GROUPED (`seasonId: null`): seasons are series-scoped,
- * so the loser's season ids don't exist on the survivor. The survivor's
- * seasons were unioned by number separately; re-homing issues to specific
- * survivor seasons is left to the user. A single renumber pass on the survivor
- * sequences the combined set. Returns `{ reassigned }`.
+ * Issue→season grouping is preserved across the move via `seasonIdMap` — a
+ * `{ loserSeasonId: survivorSeasonId }` map the caller (`mergeSeries`) builds by
+ * pairing the loser's seasons to the survivor's by `number`. An issue whose
+ * `seasonId` resolves through the map lands in the matching survivor season; one
+ * with no `seasonId`, or a `seasonId` the map doesn't cover (a stale ref, or a
+ * loser season that didn't survive the union), lands UN-GROUPED (`seasonId:
+ * null`). A single renumber pass on the survivor sequences the combined set.
+ * Returns `{ reassigned }`.
*
* Serialized on the SURVIVOR's issues queue so the renumber can't race a
* concurrent survivor edit. Tombstoned issues are skipped (moving them would
* bump updatedAt and lose the originator's delete LWW race).
*/
-export function reassignIssuesToSeries(fromSeriesId, toSeriesId) {
+export function reassignIssuesToSeries(fromSeriesId, toSeriesId, { seasonIdMap = {} } = {}) {
if (!isStr(fromSeriesId) || !isStr(toSeriesId) || fromSeriesId === toSeriesId) {
return Promise.reject(makeErr('reassignIssuesToSeries: fromSeriesId and toSeriesId must differ', ERR_VALIDATION));
}
@@ -746,7 +749,10 @@ export function reassignIssuesToSeries(fromSeriesId, toSeriesId) {
for (let i = 0; i < state.issues.length; i += 1) {
const iss = state.issues[i];
if (iss.seriesId !== fromSeriesId || iss.deleted) continue;
- const merged = sanitizeIssue({ ...iss, seriesId: toSeriesId, seasonId: null, updatedAt: now });
+ // Map the issue's loser season to the survivor's same-number season so the
+ // grouping survives; fall back to un-grouped when there's no mapping.
+ const seasonId = (iss.seasonId && seasonIdMap[iss.seasonId]) || null;
+ const merged = sanitizeIssue({ ...iss, seriesId: toSeriesId, seasonId, updatedAt: now });
if (!merged) continue;
state.issues[i] = merged;
moved.push(merged);
diff --git a/server/services/pipeline/issues.test.js b/server/services/pipeline/issues.test.js
index e012e657c5..646506be44 100644
--- a/server/services/pipeline/issues.test.js
+++ b/server/services/pipeline/issues.test.js
@@ -1106,6 +1106,40 @@ describe('pipeline issues service', () => {
});
});
+ describe('reassignIssuesToSeries (season mapping)', () => {
+ it('maps loser seasonIds to survivor seasonIds, leaving unmapped/absent ones un-grouped', async () => {
+ const from = await seriesSvc.createSeries({ name: 'From' });
+ const to = await seriesSvc.createSeries({ name: 'To' });
+ const mapped = await svc.createIssue({ seriesId: from.id, title: 'mapped' });
+ const unmapped = await svc.createIssue({ seriesId: from.id, title: 'unmapped' });
+ await svc.createIssue({ seriesId: from.id, title: 'none' });
+ await svc.updateIssue(mapped.id, { seasonId: 'sea-loser-1' });
+ await svc.updateIssue(unmapped.id, { seasonId: 'sea-loser-x' }); // absent from the map
+
+ const result = await svc.reassignIssuesToSeries(from.id, to.id, {
+ seasonIdMap: { 'sea-loser-1': 'sea-surv-1' },
+ });
+ expect(result.reassigned).toBe(3);
+
+ const byTitle = Object.fromEntries(
+ (await svc.listIssues({ seriesId: to.id })).map((i) => [i.title, i]),
+ );
+ expect(byTitle.mapped.seasonId).toBe('sea-surv-1'); // remapped
+ expect(byTitle.unmapped.seasonId).toBeNull(); // not in the map → un-grouped
+ expect(byTitle.none.seasonId).toBeNull(); // had no season → un-grouped
+ });
+
+ it('defaults to un-grouped when no seasonIdMap is supplied (back-compat)', async () => {
+ const from = await seriesSvc.createSeries({ name: 'From2' });
+ const to = await seriesSvc.createSeries({ name: 'To2' });
+ const iss = await svc.createIssue({ seriesId: from.id, title: 'x' });
+ await svc.updateIssue(iss.id, { seasonId: 'sea-whatever' });
+ await svc.reassignIssuesToSeries(from.id, to.id);
+ const [moved] = await svc.listIssues({ seriesId: to.id });
+ expect(moved.seasonId).toBeNull();
+ });
+ });
+
describe('bulkReassignSeason', () => {
const setup = async () => {
const series = await seriesSvc.createSeries({ name: 'Saga' });
diff --git a/server/services/recordMerge.js b/server/services/recordMerge.js
index aeaad11ee2..2ac91efa10 100644
--- a/server/services/recordMerge.js
+++ b/server/services/recordMerge.js
@@ -29,7 +29,7 @@ import {
normalizeLabelKey,
VARIATIONS_PER_CATEGORY_MAX, COMPOSITE_SHEETS_MAX, INFLUENCES_PER_LIST_MAX, IMAGE_REFS_PER_ENTRY_MAX,
} from './universeBuilder.js';
-import { mergeExtractedBible, BIBLE_KIND } from '../lib/storyBible.js';
+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';
@@ -402,7 +402,9 @@ export async function mergeSeries(survivorId, loserId, fieldChoices = {}, { dryR
const { record, conflicts, autoResolved, unionSummary } = buildSeriesUnion(survivor, loser, fieldChoices, fieldOverrides);
const loserCollection = await findCollectionBySeriesId(loserId);
- // Issues are reassigned to the survivor un-grouped; count for the preview.
+ // Issues are reassigned to the survivor, keeping their season grouping where
+ // the loser's season maps to a survivor season of the same number; count for
+ // the preview.
const loserIssues = await listIssues({ seriesId: loserId });
const cascade = {
issuesToRepoint: loserIssues.length,
@@ -417,9 +419,25 @@ export async function mergeSeries(survivorId, loserId, fieldChoices = {}, { dryR
// 1. Write the unioned survivor.
await updateSeries(survivorId, record);
- // 2. Re-point the loser's issues to the survivor (un-grouped) before tombstone.
+ // 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.
if (loserIssues.length > 0) {
- await reassignIssuesToSeries(loserId, survivorId);
+ const survivorSeasonIdByNumber = new Map(
+ (record.seasons || [])
+ .filter((s) => Number.isFinite(s?.number) && isStr(s?.id))
+ .map((s) => [s.number, s.id]),
+ );
+ const seasonIdMap = {};
+ for (const ls of loser.seasons || []) {
+ if (!isStr(ls?.id) || !Number.isFinite(ls?.number)) continue;
+ const survivorSeasonId = survivorSeasonIdByNumber.get(ls.number);
+ if (survivorSeasonId) seasonIdMap[ls.id] = survivorSeasonId;
+ }
+ await reassignIssuesToSeries(loserId, survivorId, { seasonIdMap });
}
// 3. Fold the loser's series-collection into the survivor's, then tombstone it.
diff --git a/server/services/recordMerge.test.js b/server/services/recordMerge.test.js
index d4f1b3033e..ef698b6a3d 100644
--- a/server/services/recordMerge.test.js
+++ b/server/services/recordMerge.test.js
@@ -21,6 +21,7 @@ const merge = await import('./recordMerge.js');
const universeSvc = await import('./universeBuilder.js');
const seriesSvc = await import('./pipeline/series.js');
const issuesSvc = await import('./pipeline/issues.js');
+const seasonsSvc = await import('./pipeline/seasons.js');
const collectionsSvc = await import('./mediaCollections.js');
afterAll(() => rmSync(TEST_DATA_ROOT, { recursive: true, force: true }));
@@ -257,6 +258,40 @@ describe('recordMerge — mergeSeries (integration)', () => {
await expect(seriesSvc.getSeries(loser.id)).rejects.toMatchObject({ code: seriesSvc.ERR_NOT_FOUND });
});
+ it('preserves issue→season grouping by number across the merge', 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 });
+ // Survivor has season 1; loser has season 1 (collides) + season 2 (new).
+ const survS1 = await seasonsSvc.createSeason(survivor.id, { title: 'Surv S1', number: 1 });
+ const loseS1 = await seasonsSvc.createSeason(loser.id, { title: 'Lose S1', number: 1 });
+ const loseS2 = await seasonsSvc.createSeason(loser.id, { title: 'Lose S2', number: 2 });
+ // Loser issues: A in season 1, B in season 2, C un-grouped.
+ const a = await issuesSvc.createIssue({ seriesId: loser.id, title: 'A' });
+ const b = await issuesSvc.createIssue({ seriesId: loser.id, title: 'B' });
+ await issuesSvc.createIssue({ seriesId: loser.id, title: 'C' });
+ await issuesSvc.updateIssue(a.id, { seasonId: loseS1.id });
+ await issuesSvc.updateIssue(b.id, { seasonId: loseS2.id });
+
+ await merge.mergeSeries(survivor.id, loser.id, {});
+
+ const survivorIssues = await issuesSvc.listIssues({ seriesId: survivor.id });
+ const byTitle = Object.fromEntries(survivorIssues.map((i) => [i.title, i]));
+ // A's loser season 1 collided → re-homed to the SURVIVOR's season 1 (its id),
+ // never the tombstoned loser season id.
+ expect(byTitle.A.seasonId).toBe(survS1.id);
+ expect(byTitle.A.seasonId).not.toBe(loseS1.id);
+ // B's loser season 2 didn't collide → appended to the survivor verbatim, so
+ // the issue stays under that (now-survivor) season id.
+ expect(byTitle.B.seasonId).toBe(loseS2.id);
+ // C had no season → stays un-grouped.
+ expect(byTitle.C.seasonId).toBeNull();
+
+ // The survivor now carries both seasons (1 from the collision, 2 appended).
+ const merged = await seriesSvc.getSeries(survivor.id);
+ expect(merged.seasons.map((s) => s.number).sort()).toEqual([1, 2]);
+ });
+
it('rejects merging series from different universes', async () => {
const u1 = await universeSvc.createUniverse({ name: 'U1' });
const u2 = await universeSvc.createUniverse({ name: 'U2' });