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

refactor([story-builder-arc-refine-feedback]): /simplify — share trimChanges, drop redundant fallback + getSeries

[._.]/ Adam Eivyb9bff725f883
diff --git a/server/services/pipeline/arcPlanner.js b/server/services/pipeline/arcPlanner.js
index 8000d5294d..5354db4118 100644
--- a/server/services/pipeline/arcPlanner.js
+++ b/server/services/pipeline/arcPlanner.js
@@ -39,7 +39,7 @@ import {
renderArcShapeGuidance,
renderArcShapePositionSummary,
} from '../../lib/storyArc.js';
-import { runPromptRefineRaw } from './refineHelpers.js';
+import { runPromptRefineRaw, trimChanges } from './refineHelpers.js';
import { recommendStructure, describeStructure } from '../../lib/seasonStructure.js';
import { LENGTH_PROFILE_NAMES, DEFAULT_LENGTH_PROFILE } from '../../lib/issueLength.js';
import { getUniverse } from '../universeBuilder.js';
@@ -936,10 +936,7 @@ export async function refineReaderMap(seriesId, feedback, options = {}) {
if (!safeReaderMap) {
throw makeErr('LLM returned an empty reader map and there is none to preserve', ERR_VALIDATION);
}
- const changes = Array.isArray(content.changes)
- ? content.changes.map((c) => String(c).slice(0, 240)).filter(Boolean).slice(0, 12)
- : [];
- return { readerMap: safeReaderMap, changes, rationale, runId, providerId, model };
+ return { readerMap: safeReaderMap, changes: trimChanges(content.changes), rationale, runId, providerId, model };
}
/**
@@ -998,17 +995,15 @@ export async function refineArc(seriesId, feedback, options = {}) {
readerMap: arc.readerMap ?? null,
status: 'draft',
});
- // sanitizeArc returns null only when every identifying field is empty —
- // impossible here unless the current arc was already empty AND the LLM
- // returned nothing. Preserve the current arc in that degenerate case.
- const safeArc = refinedArc || (arc.logline || arc.summary ? arc : null);
- if (!safeArc) {
+ // sanitizeArc returns null only when every identifying field is empty — which,
+ // because every field above falls back to the current arc, means the current
+ // arc was ALSO empty and the LLM added nothing. Nothing to preserve, so error.
+ if (!refinedArc) {
throw makeErr('LLM returned an empty arc and there is none to preserve', ERR_VALIDATION);
}
- const changes = Array.isArray(content.changes)
- ? content.changes.map((c) => String(c).slice(0, 240)).filter(Boolean).slice(0, 12)
- : [];
- return { arc: safeArc, changes, rationale, runId, providerId, model };
+ // Return the freshly-read series so the caller can persist without a second
+ // getSeries round-trip (it needs series.seasons for commitSeasonsWithRemap).
+ return { arc: refinedArc, changes: trimChanges(content.changes), rationale, runId, providerId, model, series };
}
/**
diff --git a/server/services/pipeline/refineHelpers.js b/server/services/pipeline/refineHelpers.js
index a07ced5b12..e671c1a4f3 100644
--- a/server/services/pipeline/refineHelpers.js
+++ b/server/services/pipeline/refineHelpers.js
@@ -34,6 +34,16 @@ export async function runPromptRefineRaw({
return { content, rationale, runId: result.runId, providerId: result.providerId, model: result.model };
}
+// Normalize an LLM-returned `changes` array (the short "what I changed" bullet
+// list every refine prompt emits) to trimmed, non-empty, capped strings. Shared
+// by the raw-refine callers (arc / reader-map) that hand-rolled this identically;
+// `runPromptRefine` applies the same shape inline via `changesLimit`.
+export function trimChanges(raw, limit = 12) {
+ return Array.isArray(raw)
+ ? raw.map((c) => String(c).slice(0, 240)).filter(Boolean).slice(0, limit)
+ : [];
+}
+
// Single-field refine wrapper for the comic-panel / storyboard-scene /
// character-physicalDescription paths. `resultField` keeps the helper
// prompt-agnostic; `changes` are trimmed + capped on the way out.
diff --git a/server/services/storyBuilder.js b/server/services/storyBuilder.js
index 0b6ffd72fb..ce49efdd69 100644
--- a/server/services/storyBuilder.js
+++ b/server/services/storyBuilder.js
@@ -672,13 +672,13 @@ export async function refineStep(id, stepId, { feedback, entryId, providerId, mo
if (stepId === 'plotArc') {
if (!session.seriesId) throw makeErr('No series linked', ERR_VALIDATION);
emit('Refining the plot arc…', 'generate');
- const { arc, changes, rationale, runId } = await refineArc(session.seriesId, feedback, { providerId: reqProviderId, model: reqModel });
+ // refineArc returns the series it read, so we persist without a 2nd fetch.
+ const { arc, changes, rationale, runId, series } = await refineArc(session.seriesId, feedback, { providerId: reqProviderId, model: reqModel });
emit('Saving…', 'persist');
// Persist through commitSeasonsWithRemap (passing the EXISTING seasons
// unchanged) so per-field arc locks are honored, exactly like the plotArc
// generate path — refine only rewrites the arc's narrative fields, never
// the season breakdown.
- const series = await getSeries(session.seriesId);
const { series: updated } = await commitSeasonsWithRemap(series, { arc, seasons: series.seasons || [] });
return { result: updated, changes, rationale, runId };
}