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([issue-2172]): tighten foreshadowing ledger after /simplify review

[._.]/ Adam Eivy4688ad207b44
diff --git a/server/lib/editorial/checkInfra.js b/server/lib/editorial/checkInfra.js
index dbd33d295b..9f674b671c 100644
--- a/server/lib/editorial/checkInfra.js
+++ b/server/lib/editorial/checkInfra.js
@@ -322,13 +322,20 @@ export const ENDINGS_CLIFFHANGER_STAGE = 'pipeline-editorial-endings-cliffhanger
// logged. Pure + deterministic so it's unit-testable and so its token cost can be
// counted into the per-chunk overhead. Returns '' when nothing is authored (the
// prompt's `{{#authoredSetups}}` section then renders nothing).
+// Shared preamble for the authored-entry renderers below: an entry's
+// `label — note` (or whichever is present), '' when neither is usable so
+// callers can `.filter(Boolean)`.
+function entryLabelNoteText(e) {
+ const label = typeof e?.label === 'string' ? e.label.trim() : '';
+ const note = typeof e?.note === 'string' ? e.note.trim() : '';
+ return label && note ? `${label} — ${note}` : (label || note);
+}
+
// Render one reader-map entry (hook or payoff) to a `- text (arc position N)` line.
// Shared by authoredSetupPayoffSummary + authoredPayoffsSummary. Returns '' for an
// entry with no usable label/note so callers can `.filter(Boolean)`.
function renderReaderMapEntryLine(e) {
- const label = typeof e?.label === 'string' ? e.label.trim() : '';
- const note = typeof e?.note === 'string' ? e.note.trim() : '';
- const text = label && note ? `${label} — ${note}` : (label || note);
+ const text = entryLabelNoteText(e);
if (!text) return '';
// A coarse expected-location hint so the model can reason about WHERE an
// authored hook should have paid off (reconciliation signal, #1299).
@@ -342,9 +349,7 @@ function renderReaderMapEntryLine(e) {
// inferring every seed from scratch. Returns '' for an entry with no usable
// label/note so callers can `.filter(Boolean)`.
function renderForeshadowingEntryLine(e) {
- const label = typeof e?.label === 'string' ? e.label.trim() : '';
- const note = typeof e?.note === 'string' ? e.note.trim() : '';
- const text = label && note ? `${label} — ${note}` : (label || note);
+ const text = entryLabelNoteText(e);
if (!text) return '';
const span = [];
if (Number.isFinite(e?.plantIssue)) span.push(`plant issue ${e.plantIssue}`);
diff --git a/server/lib/storyArc.js b/server/lib/storyArc.js
index 88ba41b81e..e3604ba05c 100644
--- a/server/lib/storyArc.js
+++ b/server/lib/storyArc.js
@@ -95,12 +95,6 @@ export const FORESHADOW_LIMITS = Object.freeze({
ENTRIES_MAX: 60,
REINFORCE_MAX: 20,
ISSUE_MAX: 9999,
- // The craft target the arc-overview prompt is instructed to hit: a plant and
- // its payoff should sit at least this many issues apart so the reader has
- // forgotten the seed by the time it fires. Exposed so the sanitizer can stamp
- // a `distanceOk` flag (informative, non-destructive — a too-close entry is
- // kept, not dropped) and so the check/tests share one source of truth.
- MIN_PLANT_PAYOFF_DISTANCE: 3,
});
// Per-episode arc roles produced by the season-episodes generator and
@@ -461,9 +455,9 @@ function cleanReinforceIssues(raw) {
/**
* Sanitize one foreshadowing-ledger entry. Returns `null` when the entry
* carries no identifying content (no label/note and no plant/payoff position)
- * so an empty row from the LLM is dropped. A too-close plant→payoff span is
- * NOT dropped — it's kept with `distanceOk: false` so the data stays faithful
- * to what the author/LLM declared and the editorial check can still see it.
+ * so an empty row from the LLM is dropped. The plant-to-payoff distance rule
+ * (>= 3 issues) is a generation instruction enforced in the arc-overview prompt,
+ * not here — the sanitizer stores the ledger faithfully as authored.
*/
export function sanitizeForeshadowingEntry(raw) {
if (!raw || typeof raw !== 'object') return null;
@@ -471,15 +465,9 @@ export function sanitizeForeshadowingEntry(raw) {
const note = trimTo(raw.note, FORESHADOW_LIMITS.NOTE_MAX);
const plantIssue = optPosition(raw.plantIssue, FORESHADOW_LIMITS.ISSUE_MAX);
const payoffIssue = optPosition(raw.payoffIssue, FORESHADOW_LIMITS.ISSUE_MAX);
- const reinforceIssues = cleanReinforceIssues(raw.reinforceIssues);
if (!label && !note && plantIssue == null && payoffIssue == null) return null;
- // distanceOk is only meaningful when BOTH ends are pinned; when either is
- // absent we can't judge the span, so report `null` (unknown) rather than a
- // misleading `false`.
- const distanceOk = plantIssue != null && payoffIssue != null
- ? payoffIssue - plantIssue >= FORESHADOW_LIMITS.MIN_PLANT_PAYOFF_DISTANCE
- : null;
- return { id: ensureFsId(raw.id), label, plantIssue, reinforceIssues, payoffIssue, note, distanceOk };
+ const reinforceIssues = cleanReinforceIssues(raw.reinforceIssues);
+ return { id: ensureFsId(raw.id), label, plantIssue, reinforceIssues, payoffIssue, note };
}
/**
diff --git a/server/lib/storyArc.test.js b/server/lib/storyArc.test.js
index 2fff42f48e..1107c84867 100644
--- a/server/lib/storyArc.test.js
+++ b/server/lib/storyArc.test.js
@@ -578,13 +578,14 @@ describe('storyArc — sanitizeForeshadowing (#2172)', () => {
expect(entry.reinforceIssues).toEqual([0, 3, 5]);
});
- it('flags distanceOk true when payoff is >= MIN distance after plant, false when too close, null when unknown', () => {
- expect(sanitizeForeshadowingEntry({ label: 'a', plantIssue: 1, payoffIssue: 1 + FORESHADOW_LIMITS.MIN_PLANT_PAYOFF_DISTANCE }).distanceOk).toBe(true);
- expect(sanitizeForeshadowingEntry({ label: 'a', plantIssue: 1, payoffIssue: 2 }).distanceOk).toBe(false);
- // A too-close entry is kept (not dropped), just flagged.
- expect(sanitizeForeshadowingEntry({ label: 'a', plantIssue: 1, payoffIssue: 2 })).not.toBe(null);
- // Unknown span (one end missing) → null, not a misleading false.
- expect(sanitizeForeshadowingEntry({ label: 'a', plantIssue: 1 }).distanceOk).toBe(null);
+ it('stores the ledger faithfully — a too-close plant/payoff span is kept, not dropped (distance is a prompt rule)', () => {
+ // The >= 3-issue distance rule lives in the arc-overview prompt, not the
+ // sanitizer, so a too-close entry round-trips unchanged.
+ const entry = sanitizeForeshadowingEntry({ label: 'a', plantIssue: 1, payoffIssue: 2 });
+ expect(entry).not.toBe(null);
+ expect(entry.plantIssue).toBe(1);
+ expect(entry.payoffIssue).toBe(2);
+ expect(entry).not.toHaveProperty('distanceOk');
});
it('caps the ledger at ENTRIES_MAX', () => {
diff --git a/server/routes/pipeline/series.js b/server/routes/pipeline/series.js
index 157d209794..1d372fb137 100644
--- a/server/routes/pipeline/series.js
+++ b/server/routes/pipeline/series.js
@@ -66,6 +66,11 @@ const arcSchema = z.object({
// sanitizeTickingClock (storyArc.js). Listed here so Zod doesn't strip it on
// an arc PATCH and updateSeries's wholesale arc replace doesn't wipe it.
tickingClock: z.object({}).passthrough().nullable().optional(),
+ // Foreshadowing ledger (#2172): the plant → reinforce → payoff seeds. Same
+ // defer-to-service pattern — an ARRAY of opaque entries sanitized by
+ // sanitizeForeshadowing (storyArc.js). Listed here so Zod doesn't strip it on
+ // an arc PATCH and updateSeries's wholesale arc replace doesn't wipe it.
+ foreshadowing: z.array(z.object({}).passthrough()).nullable().optional(),
status: z.enum(ARC_STATUSES).optional(),
});
diff --git a/server/services/pipeline/series.js b/server/services/pipeline/series.js
index ffb388cfc1..1c51000f71 100644
--- a/server/services/pipeline/series.js
+++ b/server/services/pipeline/series.js
@@ -836,9 +836,11 @@ async function cascadeDeleteSideEffects(id) {
// the `universeId` hierarchy guard. See issue #1361.
const ADDITIVE_SERIES_FIELDS = ['arc', 'seasons', 'styleGuide', 'styleNotes', 'characterArcs', 'factReference', 'factCritical', 'editorialCheckConfig', 'severityWeights', 'blockingSeverities'];
// Additive sub-fields nested inside `arc`. A peer that predates these (readerMap
-// shipped at schema v2, tickingClock at #1289/v3) still sends an `arc` object —
-// just without these keys — so the erasure for them happens one level down.
-const ADDITIVE_ARC_FIELDS = ['readerMap', 'tickingClock'];
+// shipped at schema v2, tickingClock at #1289/v3, foreshadowing at #2172/v10)
+// still sends an `arc` object — just without these keys — so the erasure for
+// them happens one level down. (The v10 gate rejects OLDER peers; this list
+// additionally guards a SAME-version peer whose arc simply omits the key.)
+const ADDITIVE_ARC_FIELDS = ['readerMap', 'tickingClock', 'foreshadowing'];
const keyAbsent = (obj, key) => !obj || typeof obj !== 'object' || !(key in obj) || obj[key] === undefined;