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 (codex): make catalog→series linking best-effort after session save

[._.]/ Adam Eivya24b18859a0e
diff --git a/server/services/storyBuilder.js b/server/services/storyBuilder.js
index 77baad1bce..b52c004732 100644
--- a/server/services/storyBuilder.js
+++ b/server/services/storyBuilder.js
@@ -271,14 +271,18 @@ export async function createStorySession(input = {}) {
// ingredients (#1761). In the normal (no-ingredients) case this equals
// seedIdea, so the create path is byte-for-byte unchanged.
let effectiveSeed = seedIdea;
+ // Resolved catalog ingredients (#1761), hoisted to function scope so the
+ // series links can be created AFTER the session is saved (best-effort —
+ // see below). Stays empty in import mode and the no-ingredients case.
+ let remixIngredients = [];
if (intakeMode === 'seed') {
// Resolve any selected catalog ingredients up front: they feed both the
// fallback seed (so the minted shells aren't empty) and the series links
- // created once the series exists. Missing/deleted ids are skipped.
- const ingredients = await resolveCatalogIngredients(input.catalogIngredientIds);
- if (!effectiveSeed && ingredients.length > 0) {
- effectiveSeed = composeSeedFromIngredients(ingredients);
+ // created once the session is saved. Missing/deleted ids are skipped.
+ remixIngredients = await resolveCatalogIngredients(input.catalogIngredientIds);
+ if (!effectiveSeed && remixIngredients.length > 0) {
+ effectiveSeed = composeSeedFromIngredients(remixIngredients);
}
// Mint the shells the wizard fills in. The universe name doubles as the
@@ -308,14 +312,6 @@ export async function createStorySession(input = {}) {
});
seriesId = series.id;
}
-
- // Link the resolved ingredients to the (now-existing) series via
- // catalog_ingredient_refs — the convergence contract's single data model.
- // Role maps from the ingredient type; unknown types link as 'mentioned'.
- if (ingredients.length > 0) {
- const linked = await linkIngredientsToSeries(seriesId, ingredients);
- console.log(`🔗 StoryBuilder: linked ${linked.length} catalog ingredient(s) to series ${seriesId}`);
- }
}
const now = nowIso();
@@ -346,6 +342,18 @@ export async function createStorySession(input = {}) {
: {}),
});
await store().saveOne(created.id, created);
+
+ // Link the selected catalog ingredients to the new series via
+ // catalog_ingredient_refs — the convergence contract's single data model.
+ // Deliberately AFTER the session is saved and best-effort: the story is the
+ // primary artifact, so a transient ref-insert failure must not reject the
+ // create and orphan the just-minted universe/series (the link is recoverable
+ // by re-picking ingredients). Role maps from type; unknown types → 'mentioned'.
+ if (remixIngredients.length > 0) {
+ await linkIngredientsToSeries(seriesId, remixIngredients)
+ .then((linked) => console.log(`🔗 StoryBuilder: linked ${linked.length} catalog ingredient(s) to series ${seriesId}`))
+ .catch((err) => console.error(`⚠️ StoryBuilder: failed to link catalog ingredients to series ${seriesId}: ${err.message}`));
+ }
return created;
}
diff --git a/server/services/storyBuilder.test.js b/server/services/storyBuilder.test.js
index 66aefaa0b6..3fca7e8315 100644
--- a/server/services/storyBuilder.test.js
+++ b/server/services/storyBuilder.test.js
@@ -245,6 +245,25 @@ describe('storyBuilder — catalog ingredient linking (#1761)', () => {
expect(catalogMocks.linkIngredientsToSeries).toHaveBeenCalledWith(s.seriesId, [ing]);
});
+ it('still creates + saves the session when linking fails (best-effort, no orphan)', async () => {
+ const ing = { id: 'cat-c', type: 'character', name: 'Mira', payload: {} };
+ catalogMocks.listIngredients.mockResolvedValue({ items: [ing] });
+ // A transient ref-insert failure must NOT reject the create (that would
+ // orphan the just-minted universe/series with no session pointing at them).
+ catalogMocks.linkIngredientsToSeries.mockRejectedValue(new Error('ref insert boom'));
+
+ const s = await sb.createStorySession({
+ title: 'Resilient', seedIdea: 'seed',
+ catalogIngredientIds: ['cat-c'],
+ });
+
+ // Session was saved despite the link failure, and the shells exist.
+ expect(s.id).toMatch(/^stb-/);
+ expect((await sb.listStorySessions()).map((x) => x.id)).toContain(s.id);
+ expect(await seriesSvc.getSeries(s.seriesId)).toBeTruthy();
+ expect(catalogMocks.linkIngredientsToSeries).toHaveBeenCalledWith(s.seriesId, [ing]);
+ });
+
it('does not link in import mode', async () => {
const universe = await universeSvc.createUniverse({ name: 'U' });
const series = await seriesSvc.createSeries({ name: 'S', universeId: universe.id });