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([create-issue-preload-series-for-batch]): preload series into createIssue renumber for batch episode seeding

[._.]/ Adam Eivy9e032adb77dc
diff --git a/server/routes/pipeline.js b/server/routes/pipeline.js
index e4b0f194b4..189d9d871a 100644
--- a/server/routes/pipeline.js
+++ b/server/routes/pipeline.js
@@ -1256,13 +1256,18 @@ router.post('/series/:id/seasons/:seasonId/episodes/generate', asyncHandler(asyn
// already accepts `seasonId` + `arcPosition`; the shared helper owns the
// per-episode → createIssue mapping so the Story Builder's batch path
// mints identical issue shapes.
- createdIssues = await arcPlanner.commitEpisodesToIssues(req.params.id, req.params.seasonId, result.episodes);
+ // Fetch the series once and thread it through both the issue-creation
+ // batch (so each createIssue's renumber pass skips a redundant read) and
+ // the continuity extraction below.
+ const series = await seriesSvc.getSeries(req.params.id).catch(() => null);
+ createdIssues = await arcPlanner.commitEpisodesToIssues(
+ req.params.id, req.params.seasonId, result.episodes, { preloadedSeries: series },
+ );
// Non-fatal: episode creation already succeeded, so a noisy extraction
// failure must not invalidate the user's accepted breakdown. Phase B.4:
// canon lives on the linked universe — orphan series (no universeId)
// skip extraction.
- const series = await seriesSvc.getSeries(req.params.id).catch(() => null);
const corpus = result.episodes
.map((ep) => `## E${ep.number} — ${ep.title}\n\n${ep.logline || ''}\n\n${ep.synopsis || ''}`.trim())
.filter(Boolean)
diff --git a/server/services/pipeline/arcPlanner.js b/server/services/pipeline/arcPlanner.js
index 770f37c401..ad98dd23fa 100644
--- a/server/services/pipeline/arcPlanner.js
+++ b/server/services/pipeline/arcPlanner.js
@@ -1108,7 +1108,12 @@ export async function generateSeasonEpisodes(seriesId, seasonId, options = {}) {
* Builder's "generate issues from arc" action so both mint byte-identical
* issue shapes. The episode's logline + synopsis land in `stages.idea.input`.
*/
-export async function commitEpisodesToIssues(seriesId, seasonId, episodes = []) {
+export async function commitEpisodesToIssues(seriesId, seasonId, episodes = [], { preloadedSeries = null } = {}) {
+ // Fetch the series once for the whole batch (unless the caller already holds
+ // it) and thread it into each createIssue's renumber pass. The series record
+ // doesn't change as issues are appended, so an N-episode season otherwise
+ // pays N redundant getSeries reads of an unchanging record.
+ const series = preloadedSeries || await getSeries(seriesId).catch(() => null);
const created = [];
for (const ep of episodes) {
const issue = await createIssue({
@@ -1132,7 +1137,7 @@ export async function commitEpisodesToIssues(seriesId, seasonId, episodes = [])
input: [ep.logline, ep.synopsis].filter(Boolean).join('\n\n'),
},
},
- });
+ }, { preloadedSeries: series });
created.push(issue);
}
return created;
diff --git a/server/services/pipeline/arcPlanner.test.js b/server/services/pipeline/arcPlanner.test.js
index 94f50a0483..eb5d0c645c 100644
--- a/server/services/pipeline/arcPlanner.test.js
+++ b/server/services/pipeline/arcPlanner.test.js
@@ -375,6 +375,58 @@ describe('arcPlanner — generateSeasonEpisodes', () => {
});
});
+describe('arcPlanner — commitEpisodesToIssues', () => {
+ beforeEach(() => {
+ fileStore.clear();
+ uuidCounter = 0;
+ });
+
+ it('mints one issue per episode with arc pointers + idea seed', async () => {
+ const s = await setupSeries();
+ const episodes = [
+ { number: 1, title: 'Pilot', arcRole: 'pilot', logline: 'L1', synopsis: 'S1', lengthProfile: 'standard' },
+ { number: 2, title: 'Rising', arcRole: 'complication', logline: 'L2', synopsis: '', lengthProfile: 'short' },
+ ];
+ const created = await planner.commitEpisodesToIssues(s.id, null, episodes);
+ expect(created).toHaveLength(2);
+ expect(created[0].title).toBe('Pilot');
+ expect(created[0].arcPosition).toBe(1);
+ expect(created[0].arcRole).toBe('pilot');
+ expect(created[0].stages.idea.input).toBe('L1\n\nS1');
+ expect(created[0].stages.idea.status).toBe('edited');
+ // No synopsis ⇒ idea seed is the bare logline and the stage stays 'empty'.
+ expect(created[1].stages.idea.input).toBe('L2');
+ expect(created[1].stages.idea.status).toBe('empty');
+ });
+
+ it('reads the series once for the whole batch instead of once per episode', async () => {
+ const s = await setupSeries();
+ const episodes = [1, 2, 3, 4].map((n) => ({
+ number: n, title: `E${n}`, arcRole: 'beat', logline: `L${n}`, synopsis: `S${n}`,
+ }));
+ const spy = vi.spyOn(seriesSvc, 'getSeries');
+ const created = await planner.commitEpisodesToIssues(s.id, null, episodes);
+ const callCount = spy.mock.calls.length;
+ spy.mockRestore();
+ expect(created).toHaveLength(4);
+ // The preload fetches once; each createIssue's renumber pass reuses it —
+ // the pre-fix shape was one getSeries per episode (4 reads).
+ expect(callCount).toBe(1);
+ });
+
+ it('honors a caller-supplied preloadedSeries with zero getSeries reads', async () => {
+ const s = await setupSeries();
+ const series = await seriesSvc.getSeries(s.id);
+ const episodes = [{ number: 1, title: 'Solo', arcRole: 'pilot', logline: 'L', synopsis: 'S' }];
+ const spy = vi.spyOn(seriesSvc, 'getSeries');
+ const created = await planner.commitEpisodesToIssues(s.id, null, episodes, { preloadedSeries: series });
+ const callCount = spy.mock.calls.length;
+ spy.mockRestore();
+ expect(created).toHaveLength(1);
+ expect(callCount).toBe(0);
+ });
+});
+
describe('arcPlanner — verifyArc', () => {
beforeEach(() => {
fileStore.clear();
diff --git a/server/services/pipeline/issues.js b/server/services/pipeline/issues.js
index a744791674..6a6d1d925e 100644
--- a/server/services/pipeline/issues.js
+++ b/server/services/pipeline/issues.js
@@ -602,7 +602,7 @@ export async function getIssue(id, { includeDeleted = false } = {}) {
return found;
}
-export function createIssue(input = {}) {
+export function createIssue(input = {}, { preloadedSeries = null } = {}) {
const seriesId = trimTo(input.seriesId, SERIES_ID_MAX);
if (!seriesId) return Promise.reject(makeErr('seriesId is required', ERR_VALIDATION));
const title = trimTo(input.title, TITLE_MAX);
@@ -631,7 +631,7 @@ export function createIssue(input = {}) {
});
if (!next) throw makeErr('Invalid issue payload', ERR_VALIDATION);
state.issues.push(next);
- await renumberInline(state, seriesId, next.seasonId || UNSCOPED_ANCHOR);
+ await renumberInline(state, seriesId, next.seasonId || UNSCOPED_ANCHOR, preloadedSeries);
await saveIssuesNow(state.issues.filter((i) => i.seriesId === seriesId));
// New issue = series-level change for any active share subscription.
emitRecordUpdated('series', next.seriesId);
@@ -639,8 +639,14 @@ export function createIssue(input = {}) {
});
}
-async function renumberInline(state, seriesId, fromSeasonId = null) {
- const series = await seriesSvc.getSeries(seriesId).catch(() => null);
+async function renumberInline(state, seriesId, fromSeasonId = null, preloadedSeries = null) {
+ // Batch callers (e.g. commitEpisodesToIssues seeding a whole season) thread
+ // the already-fetched series so an N-episode loop doesn't pay N redundant
+ // getSeries reads of an unchanging record. Guard on id match so a mismatched
+ // preload can never renumber against the wrong season list.
+ const series = (preloadedSeries && preloadedSeries.id === seriesId)
+ ? preloadedSeries
+ : await seriesSvc.getSeries(seriesId).catch(() => null);
// Exclude tombstones from numbering — surviving issues should keep a
// contiguous sequence regardless of how many deletes happened.
// applyVolumeOrderedNumbers mutates each issue's `number` in place, so the
diff --git a/server/services/storyBuilder.js b/server/services/storyBuilder.js
index d2619b87ae..87f5a0205f 100644
--- a/server/services/storyBuilder.js
+++ b/server/services/storyBuilder.js
@@ -496,7 +496,7 @@ export async function generateIssuesFromArc(id, options = {}) {
});
continue;
}
- const issues = await commitEpisodesToIssues(session.seriesId, season.id, res.episodes);
+ const issues = await commitEpisodesToIssues(session.seriesId, season.id, res.episodes, { preloadedSeries: series });
createdIssues.push(...issues);
seasonResults.push({ seasonId: season.id, title: label, created: issues.length, skipped: false, failed: false, runId: res.runId });
}