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): match characters referenced by first name only

Adam Eivy1a7378e46b06
diff --git a/server/services/pipeline/textStages.js b/server/services/pipeline/textStages.js
index ba75cc7166..6fadfad715 100644
--- a/server/services/pipeline/textStages.js
+++ b/server/services/pipeline/textStages.js
@@ -197,6 +197,25 @@ function resolveSourceStageIds({ issue, stageId, sourceStageIds }) {
// series principals than the whole 68-character cast.
const PRINCIPAL_ROLE_RE = /\b(main|lead|protagonist|principal|recurring|primary|hero|central)\b/i;
+// Word-boundary, case-insensitive containment test (same escaping the bible
+// matcher in scenePrompt.js uses). Local copy so the first-name supplement below
+// doesn't have to widen the shared matcher (which `canonReadiness` also calls).
+const wordInText = (needle, haystack) => {
+ if (!needle) return false;
+ const escaped = needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
+ return new RegExp(`\\b${escaped}\\b`, 'i').test(haystack);
+};
+
+// First-name token of a multi-word canon name ("Mira Reyes" → "Mira"). Drafts
+// routinely refer to a character by first name only after introduction, which
+// the full-name/alias matcher (whole-name word boundary) misses — so a clearly
+// in-issue character would lose their full record. Single-word names need no
+// supplement (the matcher already handles them).
+const firstNameToken = (c) => {
+ const parts = String(c?.name || '').trim().split(/\s+/);
+ return parts.length > 1 ? parts[0] : '';
+};
+
/**
* Scope the full-record character bible to the cast relevant to THIS issue (#1511).
*
@@ -204,18 +223,32 @@ const PRINCIPAL_ROLE_RE = /\b(main|lead|protagonist|principal|recurring|primary|
* 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), and let the always-present compact
- * `worldEntitiesSummary` roster carry the rest of the cast for continuity refs.
+ * 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.
+ *
+ * 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.
*
* Fallback order — never returns an empty block:
- * 1. characters named in the issue's source text;
+ * 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).
*/
export function scopeCharactersForIssue(allCharacters, scopeText) {
if (!Array.isArray(allCharacters) || allCharacters.length === 0) return [];
- const matched = matchCharactersInText(scopeText, allCharacters);
- if (matched.length) return matched;
+ const byKey = new Map();
+ for (const c of matchCharactersInText(scopeText, allCharacters)) byKey.set(c.id || c.name, c);
+ if (scopeText) {
+ for (const c of allCharacters) {
+ const key = c.id || c.name;
+ 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;
diff --git a/server/services/pipeline/textStages.test.js b/server/services/pipeline/textStages.test.js
index d910f30938..88fd899a8d 100644
--- a/server/services/pipeline/textStages.test.js
+++ b/server/services/pipeline/textStages.test.js
@@ -573,6 +573,17 @@ describe('pipeline text stage generator', () => {
.toEqual(['Jonas']);
});
+ 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
+ ];
+ // Draft says "Mira", not the full "Mira Reyes" — the full-name matcher misses,
+ // the first-name supplement catches it.
+ expect(textStages.__testing.scopeCharactersForIssue(cast, 'Mira crossed the yard alone').map((c) => c.name))
+ .toEqual(['Mira Reyes']);
+ });
+
it('scopeCharactersForIssue: falls back to principals when no name is matched', () => {
const cast = [
{ name: 'Mira', role: 'main protagonist' },