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 principals an always-on floor so an incidental match can't suppress the leads

Adam Eivye48642d12591
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 26ab197407..37beed5c23 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -2,7 +2,7 @@
## Per-issue story generation
-- **[issue-1511] Per-issue prose and comic-script generation now sees only the characters that issue involves.** Generating an issue used to load the entire series character bible into every prose and comic-script prompt — on a large-cast series that's tens of thousands of tokens of character detail (including one-off bit players from unrelated issues) re-sent on every issue and every stage. Generation now sends full character records only for the cast named in that issue's beats, synopsis, and source material, with a compact one-line roster of everyone else kept for continuity. This sharply cuts token cost on big-cast series and, on context-bounded providers, leaves more room for the actual draft. If an issue is too thinly seeded to name anyone yet, it falls back to the series' lead/recurring characters rather than an empty cast.
+- **[issue-1511] Per-issue prose and comic-script generation now sees only the characters that issue involves.** Generating an issue used to load the entire series character bible into every prose and comic-script prompt — on a large-cast series that's tens of thousands of tokens of character detail (including one-off bit players from unrelated issues) re-sent on every issue and every stage. Generation now sends full character records only for the cast named in that issue's beats, synopsis, and source material, with a compact one-line roster of everyone else kept for continuity. This sharply cuts token cost on big-cast series and, on context-bounded providers, leaves more room for the actual draft. The series' lead/recurring characters stay in context for every issue, so the core cast is never dropped — generation just stops carrying the full record of every bit player from unrelated issues.
## Fixed
diff --git a/server/services/pipeline/textStages.js b/server/services/pipeline/textStages.js
index e8589649bc..195f8e003c 100644
--- a/server/services/pipeline/textStages.js
+++ b/server/services/pipeline/textStages.js
@@ -224,25 +224,30 @@ const firstNameToken = (c) => {
* Injecting every canon character's full record into every issue's prose and
* comic-script prompt is the token-bloat this addresses: a 68-character bible is
* ~52K tokens re-sent on every issue × every stage, when most issues feature a
- * handful of the cast. We keep full records only for characters the issue's own
- * source text names (via the bible matcher PLUS a first-name supplement), and let
- * the always-present compact `worldEntitiesSummary` roster carry the rest of the
- * cast for continuity refs.
+ * handful of the cast. We keep full records for: (a) the series PRINCIPALS (always
+ * — the lead/recurring core is in play every issue), plus (b) any character the
+ * issue's own source text names. The always-present compact `worldEntitiesSummary`
+ * roster carries the rest of the cast for continuity refs.
*
- * Matching errs toward INCLUSION: the first-name supplement can over-match a
- * shared/common-word first name, but the ceiling is the whole cast (the old
- * behavior), so it never drops a featured character — only ever costs a little
- * extra budget. Under-matching, by contrast, would silently strip an in-issue
- * character's bible from a draft that features them.
+ * Principals are an unconditional FLOOR, not a fallback. That matters because the
+ * first-name supplement errs toward inclusion (a common-word token like "Will" in
+ * "the team will regroup" can spuriously match "Will Stone"): if principals were a
+ * fallback, one such false-positive would make the issue look "fully named" and
+ * SUPPRESS the leads. As a floor, a spurious match only ever ADDS an extra record
+ * (bounded by the whole cast — the old behavior) and can never drop the core cast.
*
- * Fallback order — never returns an empty block:
- * 1. characters named in the issue's source text (full name/alias OR first name);
- * 2. else the series principals (lead/recurring `role`);
- * 3. else the whole cast (no signal at all — preserve prior behavior).
+ * Returns the whole cast only when there is no signal at all (no principals tagged
+ * AND nothing matched — a thinly-seeded early issue with an untagged cast), so the
+ * block is never empty.
*/
export function scopeCharactersForIssue(allCharacters, scopeText) {
if (!Array.isArray(allCharacters) || allCharacters.length === 0) return [];
const byKey = new Map();
+ // (a) Principals floor — always in play, never suppressible by an incidental match.
+ for (const c of allCharacters) {
+ if (PRINCIPAL_ROLE_RE.test(c?.role || '')) byKey.set(c.id || c.name, c);
+ }
+ // (b) Characters named in the issue's source text — full name/alias, then first name.
for (const c of matchCharactersInText(scopeText, allCharacters)) byKey.set(c.id || c.name, c);
if (scopeText) {
for (const c of allCharacters) {
@@ -250,10 +255,7 @@ export function scopeCharactersForIssue(allCharacters, scopeText) {
if (!byKey.has(key) && wordInText(firstNameToken(c), scopeText)) byKey.set(key, c);
}
}
- if (byKey.size) return [...byKey.values()];
- const principals = allCharacters.filter((c) => PRINCIPAL_ROLE_RE.test(c?.role || ''));
- if (principals.length) return principals;
- return allCharacters;
+ return byKey.size ? [...byKey.values()] : allCharacters;
}
/**
diff --git a/server/services/pipeline/textStages.test.js b/server/services/pipeline/textStages.test.js
index ee168f5d04..e0696898c8 100644
--- a/server/services/pipeline/textStages.test.js
+++ b/server/services/pipeline/textStages.test.js
@@ -564,19 +564,31 @@ describe('pipeline text stage generator', () => {
expect(ctx.worldEntitiesSummary).not.toMatch(/Characters:.*\(\+\d+ more\)/);
});
- it('scopeCharactersForIssue: matched names win over principals', () => {
+ it('scopeCharactersForIssue: principals are a floor — always present, plus the named cast', () => {
const cast = [
- { name: 'Mira', role: 'lead' },
- { name: 'Jonas', role: 'foreman' },
+ { name: 'Mira', role: 'lead' }, // principal — always in the floor
+ { name: 'Jonas', role: 'extra' }, // non-principal — only in because named
];
expect(textStages.__testing.scopeCharactersForIssue(cast, 'Jonas barks an order').map((c) => c.name))
- .toEqual(['Jonas']);
+ .toEqual(['Mira', 'Jonas']);
+ });
+
+ it('scopeCharactersForIssue: an incidental name match never SUPPRESSES the principals', () => {
+ // "will" in ordinary text spuriously matches the "Will Stone" first-name token.
+ // The principal (Lena) must still survive — a false-positive can only ADD a
+ // record, never drop the leads.
+ const cast = [
+ { name: 'Will Stone', role: 'side' },
+ { name: 'Lena', role: 'lead protagonist' },
+ ];
+ const got = textStages.__testing.scopeCharactersForIssue(cast, 'the team will regroup').map((c) => c.name);
+ expect(got).toContain('Lena');
});
it('scopeCharactersForIssue: matches a character referenced by first name only', () => {
const cast = [
- { name: 'Mira Reyes', role: 'surveyor' },
- { name: 'Jonas Vale', role: 'lead' }, // principal — proves the match isn't the fallback
+ { name: 'Mira Reyes', role: 'surveyor' }, // non-principal — only in via first-name match
+ { name: 'Jonas Vale', role: 'deckhand' }, // non-principal, not named — excluded
];
// Draft says "Mira", not the full "Mira Reyes" — the full-name matcher misses,
// the first-name supplement catches it.
@@ -586,9 +598,9 @@ describe('pipeline text stage generator', () => {
it('scopeCharactersForIssue: matches non-ASCII names (accented) the ASCII \\b matcher would miss', () => {
const cast = [
- { name: 'José Marín', role: 'pilot' },
- { name: 'Élodie', role: 'lead' }, // principal — match must come from text, not fallback
- { name: 'Mira', role: 'extra' },
+ { name: 'José Marín', role: 'pilot' }, // non-principal — only in via accented first-name match
+ { name: 'Élodie', role: 'navigator' },
+ { name: 'Mira', role: 'extra' }, // not named, not principal — excluded
];
// Source names José (by first name) and Élodie (accented single name).
const got = textStages.__testing.scopeCharactersForIssue(cast, 'José and Élodie shared a look').map((c) => c.name);
@@ -597,7 +609,7 @@ describe('pipeline text stage generator', () => {
expect(got).not.toContain('Mira');
});
- it('scopeCharactersForIssue: falls back to principals when no name is matched', () => {
+ it('scopeCharactersForIssue: with nothing named, the scope is exactly the principals', () => {
const cast = [
{ name: 'Mira', role: 'main protagonist' },
{ name: 'Jonas', role: 'recurring foreman' },
@@ -629,11 +641,12 @@ describe('pipeline text stage generator', () => {
it('scopes idea-stage characters from the UNSAVED seed text, not just saved fields', async () => {
const { series } = await seed();
const world = await universeSvc.createUniverse({ name: 'Seed Verse' });
- // Lead exists so the principal fallback is non-empty — proving the match comes
- // from the seed text, not the fallback. The issue carries no saved idea text.
+ // Both are non-principal (no lead/recurring role) so the principals floor is
+ // empty — the only character in scope must come from the seed text match. The
+ // issue carries no saved idea text.
await universeSvc.updateUniverse(world.id, {
characters: [
- { name: 'Lead', role: 'protagonist' },
+ { name: 'Bram', role: 'clerk' },
{ name: 'Mira', role: 'surveyor' },
],
});