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

address review: importer re-routes seriesId payload when local series is now universe-linked

[._.]/ Adam Eivy1cf8602cdf1a
diff --git a/server/services/sharing/importer.js b/server/services/sharing/importer.js
index ce18cc724e..69fea82b64 100644
--- a/server/services/sharing/importer.js
+++ b/server/services/sharing/importer.js
@@ -121,19 +121,43 @@ async function mergeCollectionPayload(payload, availableAssetKeys = null) {
if (!localSeries) {
return { itemsAdded: 0, itemsDeferred: payload.items.length, missingSeries: true };
}
- const fallbackRaw = typeof payload.name === 'string' ? payload.name : '';
- const fallbackName = fallbackRaw.replace(/^Series:\s*/i, '').trim() || payload.seriesId;
- const seriesName = typeof localSeries.name === 'string' && localSeries.name.trim()
- ? localSeries.name
- : fallbackName;
- collection = await findOrCreateSeriesCollection({
- seriesId: payload.seriesId,
- seriesName,
- description: payload.description || '',
- }).catch((err) => {
- console.log(`⚠️ sharing.importer: findOrCreateSeriesCollection failed: ${err.message}`);
- return null;
- });
+ // If the local series has since been linked to a universe (peer's
+ // manifest is from an older universeless phase, or local user linked it
+ // after the manifest was produced), re-route into the universe
+ // collection — same contract the exporter and cover filer enforce.
+ // Minting a fresh seriesId-stamped collection here would leave a
+ // rename-locked stale per-series bucket attached to a linked series.
+ if (localSeries.universeId) {
+ const localUniverse = await getUniverse(localSeries.universeId).catch(() => null);
+ if (!localUniverse) {
+ // Dangling universe link — defer so a later sync of the universe
+ // record unblocks the merge under the universe contract. Treat as
+ // missingSeries (same defer semantics) so the manifest stays pending.
+ return { itemsAdded: 0, itemsDeferred: payload.items.length, missingSeries: true };
+ }
+ collection = await findOrCreateUniverseCollection({
+ universeId: localUniverse.id,
+ universeName: localUniverse.name,
+ description: payload.description || '',
+ }).catch((err) => {
+ console.log(`⚠️ sharing.importer: findOrCreateUniverseCollection (series re-route) failed: ${err.message}`);
+ return null;
+ });
+ } else {
+ const fallbackRaw = typeof payload.name === 'string' ? payload.name : '';
+ const fallbackName = fallbackRaw.replace(/^Series:\s*/i, '').trim() || payload.seriesId;
+ const seriesName = typeof localSeries.name === 'string' && localSeries.name.trim()
+ ? localSeries.name
+ : fallbackName;
+ collection = await findOrCreateSeriesCollection({
+ seriesId: payload.seriesId,
+ seriesName,
+ description: payload.description || '',
+ }).catch((err) => {
+ console.log(`⚠️ sharing.importer: findOrCreateSeriesCollection failed: ${err.message}`);
+ return null;
+ });
+ }
}
if (!collection) return { itemsAdded: 0 };
let added = 0;
diff --git a/server/services/sharing/integration.test.js b/server/services/sharing/integration.test.js
index 66c23dd5bb..2e1e0fd15d 100644
--- a/server/services/sharing/integration.test.js
+++ b/server/services/sharing/integration.test.js
@@ -371,6 +371,48 @@ describe('sharing round-trip', () => {
expect(manifest.collection).toBeNull();
});
+ it('series collection import re-routes to the universe collection when the local series is now universe-linked', async () => {
+ // Peer's manifest was produced when the series was universeless (so it
+ // carries `collection.seriesId`), but the local series has since been
+ // linked to a universe. Importer must NOT mint a fresh seriesId-stamped
+ // collection — that would leave a rename-locked stale per-series bucket
+ // attached to a linked series, breaking the contract the exporter and
+ // cover filer enforce. Re-route the payload into the universe collection.
+ const bucket = await buckets.createBucket({ name: 'StaleSeriesPayload', path: tempBucket, mode: 'auto-merge' });
+ const mediaCollections = await import('../mediaCollections.js');
+ const universeBuilder = await import('../universeBuilder.js');
+ const fs = await import('fs');
+
+ // Sender side: universeless series + per-series collection with one cover.
+ const s = await series.createSeries({ name: 'WillLink', logline: 'L' });
+ fs.writeFileSync(join(tempData, 'images', 'stale-cover.png'), 'STALE');
+ const senderColl = await mediaCollections.findOrCreateSeriesCollection({
+ seriesId: s.id, seriesName: s.name,
+ });
+ await mediaCollections.addItem(senderColl.id, { kind: 'image', ref: 'stale-cover.png' });
+ const exp = await exporter.exportSeries(s.id, bucket.id);
+
+ // Now (still on the sender — same env, simpler test setup) link the
+ // series to a universe and drop the per-series collection. The "local"
+ // state from the importer's perspective is: linked series, no per-series
+ // collection. Recipient re-applies the manifest.
+ const u = await universeBuilder.createUniverse({ name: 'NewCanon' });
+ await series.updateSeries(s.id, { universeId: u.id });
+ await mediaCollections.deleteCollection(senderColl.id);
+
+ simulateRemoteSender(tempBucket, exp.filename);
+ const r = await importer.processManifest(bucket.id, exp.filename);
+ expect(r.processed).toBe(true);
+ expect(r.outcome.collectionItemsAdded).toBe(1);
+
+ // The cover landed in the UNIVERSE collection, not a new seriesId bucket.
+ const universeColl = await mediaCollections.findCollectionByUniverseId(u.id);
+ expect(universeColl).toBeTruthy();
+ expect(universeColl.items.map((i) => i.ref)).toEqual(['stale-cover.png']);
+ // No seriesId-stamped collection exists for the now-linked series.
+ expect(await mediaCollections.findCollectionBySeriesId(s.id)).toBeNull();
+ });
+
it('series collection import defers when the local series is missing', async () => {
// Mirrors the universe-pending case: the manifest references a
// seriesId we haven't imported locally. The cursor must stay