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: gate per-series export + extend filer re-link recovery

[._.]/ Adam Eivye4f6c058e16e
diff --git a/server/services/pipeline/coverUniverseFiler.js b/server/services/pipeline/coverUniverseFiler.js
index b4763a42f4..bd84f904ba 100644
--- a/server/services/pipeline/coverUniverseFiler.js
+++ b/server/services/pipeline/coverUniverseFiler.js
@@ -156,11 +156,20 @@ export async function fileCoverIntoSeriesCollection({ seriesId, filename }) {
console.error(`❌ cover → series collection filing failed for ${filename}: ${err?.message || err}`);
});
- // Delete-race recovery: deleteSeries may have fired between getSeries and
- // the collection write. Unlink the stamped collection so the user can
- // rename or delete it via normal flows; covers added above are preserved.
+ // Delete- and re-link-race recovery. Two failure modes after the addItem:
+ // (a) deleteSeries fired between getSeries and the collection write
+ // → leaves a seriesId-stamped collection bound to a now-deleted
+ // series (rename-locked with no series to cascade from);
+ // (b) updateSeries linked the series to a universe between the earlier
+ // liveSeries read and the write → leaves a seriesId-stamped
+ // collection on a universe-linked series, which violates the
+ // "linked series export under the universe contract" invariant and
+ // would otherwise be picked up by the exporter's per-series fallback.
+ // Both recoveries unlink the per-series collection so it becomes a normal
+ // user-owned bucket; the covers added above are preserved either way.
const stillExists = await seriesSvc.getSeries(seriesId).catch(() => null);
- if (!stillExists || stillExists.id !== liveSeries.id) {
+ const gainedUniverse = stillExists && stillExists.id === liveSeries.id && stillExists.universeId;
+ if (!stillExists || stillExists.id !== liveSeries.id || gainedUniverse) {
await unlinkCollectionsForSeries(seriesId).catch((err) => {
console.error(`❌ cover → series collection orphan-unlink failed for series=${seriesId}: ${err?.message || err}`);
});
diff --git a/server/services/pipeline/coverUniverseFiler.test.js b/server/services/pipeline/coverUniverseFiler.test.js
index 0ff8c14237..c134284103 100644
--- a/server/services/pipeline/coverUniverseFiler.test.js
+++ b/server/services/pipeline/coverUniverseFiler.test.js
@@ -153,6 +153,41 @@ describe('fileCoverIntoSeriesCollection', () => {
expect(await collections.listCollections()).toEqual([]);
});
+ it('unlinks the per-series collection when the series gains a universe link mid-flight', async () => {
+ // Re-link race: the series was universeless at the initial fetch (so we
+ // committed to the per-series path), but `updateSeries` linked it to a
+ // universe between then and the recovery check. Leaving the per-series
+ // collection bound would violate the "linked series export under the
+ // universe contract" invariant; recovery must unlink so the collection
+ // becomes a normal user-owned bucket. The cover render added above is
+ // preserved in the now-orphaned bucket either way.
+ const series = await seriesSvc.createSeries({ name: 'Pre-Link' });
+ const universe = await universeSvc.createUniverse({ name: 'Late' });
+ // First getSeries (the one inside fileCoverIntoSeriesCollection that
+ // commits to the per-series path) sees the universeless original.
+ // Second getSeries (the recovery check after addItem) sees the linked
+ // version — simulates an updateSeries landing during the collection
+ // write window.
+ const realGet = seriesSvc.getSeries;
+ let call = 0;
+ const spy = vi.spyOn(seriesSvc, 'getSeries').mockImplementation(async (id) => {
+ call += 1;
+ const fresh = await realGet(id);
+ if (call >= 2 && fresh) return { ...fresh, universeId: universe.id };
+ return fresh;
+ });
+ await fileCoverIntoSeriesCollection({ seriesId: series.id, filename: 'racy.png' });
+ spy.mockRestore();
+ // Recovery fired: the seriesId stamp was unlinked so the collection
+ // is no longer attached to the series (orphaned-but-preserved bucket).
+ expect(await collections.findCollectionBySeriesId(series.id)).toBeNull();
+ // The cover render itself survives in the now-orphaned collection.
+ const all = await collections.listCollections();
+ const orphan = all.find((c) => c.items.some((it) => it.ref === 'racy.png'));
+ expect(orphan).toBeTruthy();
+ expect(orphan.seriesId).toBeFalsy();
+ });
+
it('re-routes to the universe collection when the series is universe-linked', async () => {
// Guard against (a) a direct caller invoking this helper for an
// already-linked series and (b) a parallel `updateSeries` adding a
diff --git a/server/services/sharing/exporter.js b/server/services/sharing/exporter.js
index 7aec4915d2..f1450e08bc 100644
--- a/server/services/sharing/exporter.js
+++ b/server/services/sharing/exporter.js
@@ -305,8 +305,15 @@ export async function exportSeries(seriesId, bucketId, opts = {}) {
if (universe) linkedCollection = await findCollectionByUniverseId(universe.id);
}
// Per-series fallback so a universeless series still ships with its
- // auto-filed cover renders via the seriesId-stamped collection.
- if (!linkedCollection) linkedCollection = await findCollectionBySeriesId(series.id);
+ // auto-filed cover renders via the seriesId-stamped collection. Gate on
+ // `series.universeId` being absent — a series that has been linked to a
+ // universe must always export under the universe-collection contract,
+ // even if a stale seriesId-stamped collection survives from before the
+ // link (orphaned mid-flight by `unlinkCollectionsForUniverse` recovery, or
+ // a legacy bucket from an earlier universeless state).
+ if (!linkedCollection && !series.universeId) {
+ linkedCollection = await findCollectionBySeriesId(series.id);
+ }
const [source, sourceBio, senderInstanceId, producedByVersion] = await Promise.all([
resolveSourceName(bucket),
diff --git a/server/services/sharing/integration.test.js b/server/services/sharing/integration.test.js
index 63d1d4328f..66c23dd5bb 100644
--- a/server/services/sharing/integration.test.js
+++ b/server/services/sharing/integration.test.js
@@ -341,6 +341,36 @@ describe('sharing round-trip', () => {
expect(manifest.collection.items.map((i) => i.ref)).toEqual(['uni.png']);
});
+ it('linked series never falls through to a stray seriesId-stamped collection', async () => {
+ // A series can be linked to a universe AND have a stray per-series
+ // collection from a prior universeless phase (or an orphan after
+ // `unlinkCollectionsForUniverse` recovery). When the universe-linked
+ // collection happens to be absent at export time (mid-migration, or
+ // a manual deletion), the exporter must NOT silently fall back to the
+ // stale seriesId-stamped collection — linked series export under the
+ // universe-collection contract or no collection payload at all.
+ const bucket = await buckets.createBucket({ name: 'LinkedNoUniColl', path: tempBucket, mode: 'auto-merge' });
+ const universeBuilder = await import('../universeBuilder.js');
+ const mediaCollections = await import('../mediaCollections.js');
+ const u = await universeBuilder.createUniverse({ name: 'CanonOnly' });
+ const s = await series.createSeries({ name: 'Bound', universeId: u.id });
+
+ const fs = await import('fs');
+ fs.writeFileSync(join(tempData, 'images', 'stale.png'), 'STALE');
+ // Stamp a seriesId collection directly (simulating a leftover from a
+ // prior universeless phase). No universe collection exists.
+ const serColl = await mediaCollections.findOrCreateSeriesCollection({
+ seriesId: s.id, seriesName: s.name,
+ });
+ await mediaCollections.addItem(serColl.id, { kind: 'image', ref: 'stale.png' });
+ expect(await mediaCollections.findCollectionByUniverseId(u.id)).toBeNull();
+
+ const exp = await exporter.exportSeries(s.id, bucket.id);
+ const manifest = JSON.parse(fs.readFileSync(join(tempBucket, 'manifests', exp.filename), 'utf-8'));
+ // No collection payload — the stale seriesId bucket was correctly ignored.
+ expect(manifest.collection).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