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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
simplify([issue-1291]): single-pass analyzeNamePair + tokenLabel helper
[._.]/ Adam Eivya7562d68505c
diff --git a/server/lib/editorial/checkRegistry.js b/server/lib/editorial/checkRegistry.jsindex 37dab18465..730455c2c9 100644--- a/server/lib/editorial/checkRegistry.js+++ b/server/lib/editorial/checkRegistry.js@@ -26,12 +26,7 @@import { z } from 'zod';import { estimateTokens } from '../contextBudget.js';-import {- nameSimilaritySignals,- levenshtein,- soundex,- findFirstLetterClusters,-} from './nameSimilarity.js';+import { analyzeNamePair, findFirstLetterClusters } from './nameSimilarity.js';export const CHECK_SCOPES = Object.freeze(['series', 'issue', 'scene', 'noun']);export const CHECK_KINDS = Object.freeze(['deterministic', 'llm']);@@ -129,16 +124,18 @@ function castNameTokens(ctx) {return tokens;}+// A name token's display label — an alias is annotated with its owning character+// so the finding text and the rename suggestion always name the source.+const tokenLabel = (t) => (t.isAlias ? `${t.token} (alias of ${t.ownerName})` : t.token);+// How to phrase the rename suggestion given which of the two characters are// locked — always steer the author toward renaming an UNLOCKED one (#1291).function renameSuggestion(a, b) {- const aLabel = a.isAlias ? `${a.token} (alias of ${a.ownerName})` : a.token;- const bLabel = b.isAlias ? `${b.token} (alias of ${b.ownerName})` : b.token;if (a.locked && b.locked) {return `Both ${a.ownerName} and ${b.ownerName} are locked — unlock one to rename it so readers can tell them apart.`;}- if (a.locked) return `Rename ${b.ownerName} (${a.ownerName} is locked) so it reads less like "${aLabel}".`;- if (b.locked) return `Rename ${a.ownerName} (${b.ownerName} is locked) so it reads less like "${bLabel}".`;+ if (a.locked) return `Rename ${b.ownerName} (${a.ownerName} is locked) so it reads less like "${tokenLabel(a)}".`;+ if (b.locked) return `Rename ${a.ownerName} (${b.ownerName} is locked) so it reads less like "${tokenLabel(b)}".`;return `Rename one of ${a.ownerName} / ${b.ownerName} so readers can tell them apart at a glance.`;}@@ -741,21 +738,21 @@ export const EDITORIAL_CHECKS = [const a = tokens[i];const b = tokens[j];if (a.owner === b.owner) continue;- const signals = nameSimilaritySignals(a.token, b.token, signalOpts);+ // Single pass yields the signals AND the severity metrics (edit distance,+ // phonetic match) so neither is recomputed below.+ const { signals, distance, phoneticMatch } = analyzeNamePair(a.token, b.token, signalOpts);// The user-controlled shared-signal count is the single gate (edit// distance and phonetic match are already among the counted signals).if (signals.length < min) continue;- const dist = signalOpts.minEditDistance > 0 ? levenshtein(a.token, b.token) : Infinity;- const phonetic = signalOpts.usePhonetic && soundex(a.token) !== '' && soundex(a.token) === soundex(b.token);// Severity scales with how confusable the pair really is, above the// check's low floor: edit distance ≤1 is a near-typo (escalate 2), a// phonetic match or 4+ signals is strong (escalate 1).- const steps = dist <= 1 ? 2 : (phonetic || signals.length >= 4 ? 1 : 0);+ const steps = distance <= 1 ? 2 : (phoneticMatch || signals.length >= 4 ? 1 : 0);findings.push({severity: escalateSeverity(ctx.severityDefault, steps),category: 'naming',location: `Characters: ${a.ownerName} / ${b.ownerName}`,- problem: `Character names "${a.token}"${a.isAlias ? ` (alias of ${a.ownerName})` : ''} and "${b.token}"${b.isAlias ? ` (alias of ${b.ownerName})` : ''} are easy to confuse (${signals.join(', ')}).`,+ problem: `Character names "${tokenLabel(a)}" and "${tokenLabel(b)}" are easy to confuse (${signals.join(', ')}).`,suggestion: renameSuggestion(a, b),anchorQuote: a.token,issueNumber: null,diff --git a/server/lib/editorial/nameSimilarity.js b/server/lib/editorial/nameSimilarity.jsindex 947510869e..762cc5862a 100644--- a/server/lib/editorial/nameSimilarity.js+++ b/server/lib/editorial/nameSimilarity.js@@ -79,18 +79,20 @@ export const DEFAULT_SIGNAL_OPTS = Object.freeze({usePhonetic: true,});-// The confusability signals two names share. Each present signal is one reason a-// reader could blur the two on the page; the registry check counts them (plus the-// strong standalone signals — near-identical spelling and phonetic match — which-// also drive severity). Returns [] when either name has no letters.-export function nameSimilaritySignals(a, b, opts = {}) {+// Analyze a name pair in a single pass: the confusability `signals` (each one a+// reason a reader could blur the two on the page) plus the two metrics that also+// drive the check's severity — the Levenshtein `distance` and whether the names+// share a `phoneticMatch`. Computing them here once lets the caller score severity+// without re-running soundex/levenshtein. Returns empty/Infinity/false when either+// name has no letters or the two normalize equal.+export function analyzeNamePair(a, b, opts = {}) {const { minEditDistance, flagSameLength, vowelSkeletonCollision, usePhonetic } = {...DEFAULT_SIGNAL_OPTS,...opts,};const la = normalizeName(a);const lb = normalizeName(b);- if (!la || !lb || la === lb) return [];+ if (!la || !lb || la === lb) return { signals: [], distance: Infinity, phoneticMatch: false };const signals = [];if (la[0] === lb[0]) signals.push('same first letter');if (flagSameLength && la.length === lb.length) signals.push('same length');@@ -100,17 +102,20 @@ export function nameSimilaritySignals(a, b, opts = {}) {}if (la.length >= 3 && lb.length >= 3 && la.slice(0, 3) === lb.slice(0, 3)) signals.push('same opening');if (la.length >= 2 && la.slice(-2) === lb.slice(-2)) signals.push('same ending');- if (minEditDistance > 0) {- const dist = levenshtein(la, lb);- if (dist <= minEditDistance) signals.push(`near-identical spelling (edit distance ${dist})`);+ const distance = levenshtein(la, lb);+ if (minEditDistance > 0 && distance <= minEditDistance) {+ signals.push(`near-identical spelling (edit distance ${distance})`);}- if (usePhonetic) {- const ka = soundex(a);- if (ka && ka === soundex(b)) signals.push('same phonetic key');- }- return signals;+ const ka = usePhonetic ? soundex(a) : '';+ const phoneticMatch = ka !== '' && ka === soundex(b);+ if (phoneticMatch) signals.push('same phonetic key');+ return { signals, distance, phoneticMatch };}+// The confusability signal list for a name pair — the `signals` view of+// analyzeNamePair, for callers (and tests) that only need the reasons.+export const nameSimilaritySignals = (a, b, opts = {}) => analyzeNamePair(a, b, opts).signals;+// A first-letter histogram over a list of names: Map<letter, name[]> keyed by the// lowercased first letter (entries with no letters are skipped). The check uses// this to spot first-letter crowding — a cast where many names start the same way.diff --git a/server/lib/editorial/nameSimilarity.test.js b/server/lib/editorial/nameSimilarity.test.jsindex 07e7be3034..4f3f11268d 100644--- a/server/lib/editorial/nameSimilarity.test.js+++ b/server/lib/editorial/nameSimilarity.test.js@@ -4,6 +4,7 @@ import {vowelSkeleton,soundex,levenshtein,+ analyzeNamePair,nameSimilaritySignals,firstLetterHistogram,findFirstLetterClusters,@@ -102,6 +103,27 @@ describe('nameSimilaritySignals', () => {});});+describe('analyzeNamePair', () => {+ it('returns signals plus the severity metrics in one pass', () => {+ const r = analyzeNamePair('Alina', 'Alana');+ expect(r.distance).toBe(1);+ expect(r.phoneticMatch).toBe(true); // both Soundex A450+ expect(r.signals).toContain('near-identical spelling (edit distance 1)');+ expect(nameSimilaritySignals('Alina', 'Alana')).toEqual(r.signals); // wrapper agrees+ });++ it('reports no phonetic match when usePhonetic is off, and Infinity distance is harmless', () => {+ const r = analyzeNamePair('Smith', 'Smyth', { usePhonetic: false });+ expect(r.phoneticMatch).toBe(false);+ expect(r.signals).not.toContain('same phonetic key');+ });++ it('is inert for letterless / equal names', () => {+ expect(analyzeNamePair('', 'Sam')).toEqual({ signals: [], distance: Infinity, phoneticMatch: false });+ expect(analyzeNamePair('Sam', 'sam')).toEqual({ signals: [], distance: Infinity, phoneticMatch: false });+ });+});+describe('firstLetterHistogram', () => {it('buckets names by their first normalized letter', () => {const hist = firstLetterHistogram(['Mike', 'Mark', 'Matt', 'Zog', "'Aaron"]);