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: simplify relationship checks + dedicated type limit (#1287)

[._.]/ Adam Eivy4dd8bb2c07b8
diff --git a/client/src/components/universe/CharacterDetailEditor.jsx b/client/src/components/universe/CharacterDetailEditor.jsx
index 8ffb45b130..858a8aa12c 100644
--- a/client/src/components/universe/CharacterDetailEditor.jsx
+++ b/client/src/components/universe/CharacterDetailEditor.jsx
@@ -387,6 +387,13 @@ function RelationshipRow({ link, idx, others, onUpdate, onRemove, disabled }) {
// Relationships section — structured character-to-character links + opposing
// forces (#1287). Needs the sibling cast (`characters`) to populate the target
// picker; renders a prompt to add more cast when the character is the only one.
+//
+// Unlike the generic LIST_SECTIONS (ListSectionEditor), this section does NOT
+// use `usePendingListRows`: its primary control is a <select> that commits a
+// VALID row immediately (a new link defaults targetCharacterId to a real cast
+// id), so there's never a pending blank-row-awaiting-required-column to hold.
+// Patching the whole array on each mutation mirrors the list-section
+// onPatchList contract; text fields buffer via useFieldDraft and commit on blur.
function RelationshipsSection({ entry, characters, onPatch, disabled }) {
const links = Array.isArray(entry.relationshipLinks) ? entry.relationshipLinks : [];
const others = (Array.isArray(characters) ? characters : []).filter((c) => c?.id && c.id !== entry.id);
@@ -397,7 +404,10 @@ function RelationshipsSection({ entry, characters, onPatch, disabled }) {
};
const updateLink = (idx, patch) => commit(links.map((l, i) => (i === idx ? { ...l, ...patch } : l)));
const removeLink = (idx) => commit(links.filter((_, i) => i !== idx));
- const oppositionCount = links.filter((l) => l.opposition?.axis).length;
+ // `!!l.opposition` matches RelationshipRow's `hasOpposition` test — the toggle
+ // always seeds `axis: 'custom'`, so a tagged link always has an axis; using
+ // the same predicate keeps the summary count and the per-row border in sync.
+ const oppositionCount = links.filter((l) => !!l.opposition).length;
const summary = links.length === 0
? 'empty'
: `${links.length} link${links.length === 1 ? '' : 's'}${oppositionCount ? ` · ${oppositionCount} opposing` : ''}`;
diff --git a/client/src/lib/bibleLimits.js b/client/src/lib/bibleLimits.js
index a20ac62c09..bcb4bd8a0e 100644
--- a/client/src/lib/bibleLimits.js
+++ b/client/src/lib/bibleLimits.js
@@ -28,6 +28,7 @@ export const BIBLE_LIMITS = Object.freeze({
MANNERISMS_MAX: 1500,
RELATIONSHIPS_MAX: 2000,
RELATIONSHIP_TARGET_ID_MAX: 64,
+ RELATIONSHIP_TYPE_MAX: 60,
RELATIONSHIP_DESCRIPTION_MAX: 1000,
RELATIONSHIP_OPPOSITION_AXIS_MAX: 60,
RELATIONSHIP_OPPOSITION_ROLE_MAX: 120,
diff --git a/server/lib/editorial/checkRegistry.js b/server/lib/editorial/checkRegistry.js
index 5aac9897d7..731d0a3ee5 100644
--- a/server/lib/editorial/checkRegistry.js
+++ b/server/lib/editorial/checkRegistry.js
@@ -65,6 +65,28 @@ function nameSimilaritySignals(a, b) {
return signals;
}
+// ---------------------------------------------------------------------------
+// Shared scaffolding for the relationship-link checks (#1287). All three walk
+// `canon.characters × relationshipLinks`, so the id-bearing character list,
+// the id→name lookup, and the link iteration live here once.
+// ---------------------------------------------------------------------------
+
+// Id-bearing characters + an id→name lookup (falling back to the id when a
+// character is unnamed). The three checks index off this same pair.
+function relationshipCanon(ctx) {
+ const chars = (ctx.canon?.characters || []).filter((c) => c && c.id);
+ return { chars, nameById: new Map(chars.map((c) => [c.id, c.name || c.id])) };
+}
+
+// Yields every relationship link that points somewhere, as { c, link, targetId }.
+function* eachRelationshipLink(chars) {
+ for (const c of chars) {
+ for (const link of (Array.isArray(c.relationshipLinks) ? c.relationshipLinks : [])) {
+ if (link?.targetCharacterId) yield { c, link, targetId: link.targetCharacterId };
+ }
+ }
+}
+
// ---------------------------------------------------------------------------
// Registry entries.
// ---------------------------------------------------------------------------
@@ -132,36 +154,28 @@ export const EDITORIAL_CHECKS = [
defaultEnabled: true,
configSchema: z.object({}),
run: (ctx) => {
- const chars = (ctx.canon?.characters || []).filter((c) => c && c.id);
- const nameById = new Map(chars.map((c) => [c.id, c.name || c.id]));
+ const { chars, nameById } = relationshipCanon(ctx);
// For O(1) "does B link back to A?" lookups, index every link as a
// "<source>→<target>" pair key.
const linkPairs = new Set();
- for (const c of chars) {
- for (const link of (Array.isArray(c.relationshipLinks) ? c.relationshipLinks : [])) {
- if (link?.targetCharacterId) linkPairs.add(`${c.id}→${link.targetCharacterId}`);
- }
- }
+ for (const { c, targetId } of eachRelationshipLink(chars)) linkPairs.add(`${c.id}→${targetId}`);
const findings = [];
- for (const c of chars) {
- for (const link of (Array.isArray(c.relationshipLinks) ? c.relationshipLinks : [])) {
- const targetId = link?.targetCharacterId;
- // A dangling target (B doesn't exist) is the dangling-target check's
- // job; reciprocity only speaks to links between two real characters.
- if (!targetId || !nameById.has(targetId)) continue;
- if (linkPairs.has(`${targetId}→${c.id}`)) continue;
- const aName = nameById.get(c.id);
- const bName = nameById.get(targetId);
- findings.push({
- severity: ctx.severityDefault,
- category: 'continuity',
- location: `Characters: ${aName} → ${bName}`,
- problem: `"${aName}" has a ${link.type || 'custom'} link to "${bName}", but "${bName}" has no link back to "${aName}".`,
- suggestion: `Add a reciprocal relationship link from "${bName}" to "${aName}" (or remove the one-sided link if it's intentional).`,
- anchorQuote: aName,
- issueNumber: null,
- });
- }
+ for (const { c, link, targetId } of eachRelationshipLink(chars)) {
+ // A dangling target (B doesn't exist) is the dangling-target check's
+ // job; reciprocity only speaks to links between two real characters.
+ if (!nameById.has(targetId)) continue;
+ if (linkPairs.has(`${targetId}→${c.id}`)) continue;
+ const aName = nameById.get(c.id);
+ const bName = nameById.get(targetId);
+ findings.push({
+ severity: ctx.severityDefault,
+ category: 'continuity',
+ location: `Characters: ${aName} → ${bName}`,
+ problem: `"${aName}" has a ${link.type || 'custom'} link to "${bName}", but "${bName}" has no link back to "${aName}".`,
+ suggestion: `Add a reciprocal relationship link from "${bName}" to "${aName}" (or remove the one-sided link if it's intentional).`,
+ anchorQuote: aName,
+ issueNumber: null,
+ });
}
return findings;
},
@@ -178,25 +192,20 @@ export const EDITORIAL_CHECKS = [
defaultEnabled: true,
configSchema: z.object({}),
run: (ctx) => {
- const chars = (ctx.canon?.characters || []).filter((c) => c && c.id);
- const idSet = new Set(chars.map((c) => c.id));
- const nameById = new Map(chars.map((c) => [c.id, c.name || c.id]));
+ const { chars, nameById } = relationshipCanon(ctx);
const findings = [];
- for (const c of chars) {
- for (const link of (Array.isArray(c.relationshipLinks) ? c.relationshipLinks : [])) {
- const targetId = link?.targetCharacterId;
- if (!targetId || idSet.has(targetId)) continue;
- const aName = nameById.get(c.id);
- findings.push({
- severity: ctx.severityDefault,
- category: 'continuity',
- location: `Character: ${aName}`,
- problem: `"${aName}" has a ${link.type || 'custom'} relationship link pointing at a character id (${targetId}) that no longer exists in the canon.`,
- suggestion: 'Re-point the link at an existing character, or delete the stale link.',
- anchorQuote: aName,
- issueNumber: null,
- });
- }
+ for (const { c, link, targetId } of eachRelationshipLink(chars)) {
+ if (nameById.has(targetId)) continue;
+ const aName = nameById.get(c.id);
+ findings.push({
+ severity: ctx.severityDefault,
+ category: 'continuity',
+ location: `Character: ${aName}`,
+ problem: `"${aName}" has a ${link.type || 'custom'} relationship link pointing at a character id (${targetId}) that no longer exists in the canon.`,
+ suggestion: 'Re-point the link at an existing character, or delete the stale link.',
+ anchorQuote: aName,
+ issueNumber: null,
+ });
}
return findings;
},
@@ -213,33 +222,29 @@ export const EDITORIAL_CHECKS = [
defaultEnabled: false,
configSchema: z.object({}),
run: (ctx) => {
- const chars = (ctx.canon?.characters || []).filter((c) => c && c.id);
- const nameById = new Map(chars.map((c) => [c.id, c.name || c.id]));
+ const { chars, nameById } = relationshipCanon(ctx);
const findings = [];
// Dedupe by the unordered character pair so a reciprocally-tagged
// opposition (A→B and B→A both carry an axis) surfaces once, not twice.
const seenPairs = new Set();
- for (const c of chars) {
- for (const link of (Array.isArray(c.relationshipLinks) ? c.relationshipLinks : [])) {
- const targetId = link?.targetCharacterId;
- if (!targetId || !link.opposition?.axis || !nameById.has(targetId)) continue;
- const pairKey = [c.id, targetId].sort().join('|');
- if (seenPairs.has(pairKey)) continue;
- seenPairs.add(pairKey);
- const aName = nameById.get(c.id);
- const bName = nameById.get(targetId);
- const { axis, thisRole, targetRole } = link.opposition;
- const roles = thisRole && targetRole ? ` (${aName}: ${thisRole}, ${bName}: ${targetRole})` : '';
- findings.push({
- severity: ctx.severityDefault,
- category: 'arc',
- location: `Characters: ${aName} / ${bName}`,
- problem: `Opposing-force pair tagged on "${aName}" / "${bName}" — axis "${axis}"${roles}.`,
- suggestion: 'Confirm the reader sees these roles reverse at some point in the arc (or that holding them fixed is the intended payoff).',
- anchorQuote: aName,
- issueNumber: null,
- });
- }
+ for (const { c, link, targetId } of eachRelationshipLink(chars)) {
+ if (!link.opposition?.axis || !nameById.has(targetId)) continue;
+ const pairKey = [c.id, targetId].sort().join('|');
+ if (seenPairs.has(pairKey)) continue;
+ seenPairs.add(pairKey);
+ const aName = nameById.get(c.id);
+ const bName = nameById.get(targetId);
+ const { axis, thisRole, targetRole } = link.opposition;
+ const roles = thisRole && targetRole ? ` (${aName}: ${thisRole}, ${bName}: ${targetRole})` : '';
+ findings.push({
+ severity: ctx.severityDefault,
+ category: 'arc',
+ location: `Characters: ${aName} / ${bName}`,
+ problem: `Opposing-force pair tagged on "${aName}" / "${bName}" — axis "${axis}"${roles}.`,
+ suggestion: 'Confirm the reader sees these roles reverse at some point in the arc (or that holding them fixed is the intended payoff).',
+ anchorQuote: aName,
+ issueNumber: null,
+ });
}
return findings;
},
diff --git a/server/lib/storyBible.js b/server/lib/storyBible.js
index 1d81b4ae1b..c72be19404 100644
--- a/server/lib/storyBible.js
+++ b/server/lib/storyBible.js
@@ -54,6 +54,7 @@ export const BIBLE_LIMITS = Object.freeze({
// `description` is per-link prose; `opposition` captures a binary-tension
// axis (hunter/prey, winner/loser…) the reader watches to see reverse.
RELATIONSHIP_TARGET_ID_MAX: 64,
+ RELATIONSHIP_TYPE_MAX: 60,
RELATIONSHIP_DESCRIPTION_MAX: 1000,
RELATIONSHIP_OPPOSITION_AXIS_MAX: 60,
RELATIONSHIP_OPPOSITION_ROLE_MAX: 120,
@@ -656,7 +657,7 @@ function sanitizeRelationshipLink(raw) {
if (!raw || typeof raw !== 'object') return null;
const targetCharacterId = trimTo(raw.targetCharacterId, BIBLE_LIMITS.RELATIONSHIP_TARGET_ID_MAX);
if (!targetCharacterId) return null;
- const typeRaw = trimTo(raw.type, BIBLE_LIMITS.RELATIONSHIP_OPPOSITION_AXIS_MAX);
+ const typeRaw = trimTo(raw.type, BIBLE_LIMITS.RELATIONSHIP_TYPE_MAX);
const type = RELATIONSHIP_LINK_TYPE_SET.has(typeRaw) ? typeRaw : 'custom';
const out = {
id: ensureId(raw.id, 'rel-'),
diff --git a/server/lib/validation.js b/server/lib/validation.js
index 12cb51748b..dfd449a0c9 100644
--- a/server/lib/validation.js
+++ b/server/lib/validation.js
@@ -1099,7 +1099,7 @@ const wrOppositionField = z.object({
const wrRelationshipLinksField = z.array(z.object({
id: z.string().trim().max(64).optional(),
targetCharacterId: z.string().trim().min(1).max(BIBLE_LIMITS.RELATIONSHIP_TARGET_ID_MAX),
- type: z.enum(RELATIONSHIP_LINK_TYPES).or(z.string().trim().max(BIBLE_LIMITS.RELATIONSHIP_OPPOSITION_AXIS_MAX)).optional(),
+ type: z.enum(RELATIONSHIP_LINK_TYPES).or(z.string().trim().max(BIBLE_LIMITS.RELATIONSHIP_TYPE_MAX)).optional(),
description: z.string().max(BIBLE_LIMITS.RELATIONSHIP_DESCRIPTION_MAX).optional(),
opposition: wrOppositionField.nullable().optional(),
locked: z.boolean().optional(),