Files
Rosea/packages/content/src/forbidden.ts
mnovak 6885a217cd [repo] Workspace: Review Phase 0 and record gaps, lessons and handover
Adversarial review with per-package counts and 13 reintroduced bugs, all
caught. Fixes from the review: the trace geocell is capped at 6 characters
(~1 km; 16 allowed centimetres), one forbidden-word hit per place, and an
e2e guard that meta charset stays within the first 1024 bytes.

CLAUDE.md §6.2 no longer contradicts itself about care acts; §2 and §3
say what Phase 0 actually built.
2026-11-01 00:36:06 +01:00

87 lines
3.8 KiB
TypeScript

/**
* The one list of words and names that never reach a player (CLAUDE.md §5.6
* rules 3 and 4). `lint:copy` and the library validator both read it, so the two
* can never disagree.
*
* Matching is whole-word and case-insensitive, with inflections spelled out.
* A stem match (`heal*`) would also catch *health*, and a rose can be healthy.
*
* `treat` is banned outright, including the fox's food: he eats *berries*.
* `patient` is banned too. Patience is the cure Gandalf's spirit offers, so the
* product may say *patience* and *patiently* but never *patient*.
*/
export type ForbiddenCategory = 'medical' | 'unnamed-state' | 'little-prince' | 'tolkien';
export interface ForbiddenTerm {
readonly term: string;
readonly category: ForbiddenCategory;
readonly why: string;
}
const MEDICAL =
'A medical claim pulls the product toward EU medical-device rules (CLAUDE.md §5.6 rule 3). Rosea comforts; it does not treat.';
const UNNAMED =
'The game never names the state for the player (CLAUDE.md §5.6 rule 4). It lets them feel seen instead.';
const PRINCE = 'Nothing from The Little Prince by name or quote: one idea is borrowed, never its text (CLAUDE.md §5.6 rule 3).';
const TOLKIEN = "Gandalf's spirit, never a name or a quote from Tolkien's works (CLAUDE.md §5.6 rule 3).";
const terms = (category: ForbiddenCategory, why: string, list: readonly string[]): ForbiddenTerm[] =>
list.map((term) => ({ term, category, why }));
export const FORBIDDEN: readonly ForbiddenTerm[] = [
...terms('medical', MEDICAL, [
'therapy', 'therapies', 'therapist', 'therapists', 'therapeutic', 'therapeutically',
'treat', 'treats', 'treated', 'treating', 'treatment', 'treatments',
'heal', 'heals', 'healed', 'healing', 'healer', 'healers',
'cure', 'cures', 'cured', 'curing', 'curative',
'diagnose', 'diagnoses', 'diagnosed', 'diagnosing', 'diagnosis', 'diagnostic',
'symptom', 'symptoms', 'symptomatic',
'patient', 'patients',
'disorder', 'disorders'
]),
...terms('unnamed-state', UNNAMED, ['lonely', 'lonelier', 'loneliest', 'loneliness']),
...terms('little-prince', PRINCE, [
'Little Prince', 'Petit Prince', 'Malý princ', 'Saint-Exupéry', 'Saint-Exupery', 'Exupéry', 'Exupery', 'B-612', 'B612'
]),
...terms('tolkien', TOLKIEN, [
'Tolkien', 'Gandalf', 'Mithrandir', 'Frodo', 'Bilbo', 'Samwise', 'Aragorn', 'Galadriel', 'Sauron',
// 'Middle-earth' also matches 'Middle earth': spaces and hyphens are interchangeable in a term.
'Mordor', 'Rivendell', 'Middle-earth', 'hobbit', 'hobbits', 'Lothlórien', 'Lothlorien'
])
];
export interface ForbiddenHit {
readonly term: string;
readonly category: ForbiddenCategory;
readonly why: string;
/** Index into the scanned text. */
readonly index: number;
}
const escape = (s: string): string => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/[\s-]+/g, '[\\s\\u00a0-]+');
// Letters and digits on either side mean it is part of a longer word: *treaty*
// does not contain *treat*. Hyphens and apostrophes are boundaries, so
// *self-healing* is caught.
const PATTERNS = FORBIDDEN.map((entry) => ({
entry,
regex: new RegExp(`(?<![\\p{L}\\p{N}])${escape(entry.term)}(?![\\p{L}\\p{N}])`, 'giu')
}));
/**
* Every forbidden term in `text`, in order of appearance, once per place.
* `Middle-earth` and `Middle earth` both match either spelling, so a second hit
* at the same index is the same word and is dropped.
*/
export function findForbidden(text: string): ForbiddenHit[] {
const byIndex = new Map<number, ForbiddenHit>();
for (const { entry, regex } of PATTERNS) {
regex.lastIndex = 0;
for (let m = regex.exec(text); m !== null; m = regex.exec(text)) {
if (!byIndex.has(m.index)) byIndex.set(m.index, { ...entry, index: m.index });
}
}
return [...byIndex.values()].sort((a, b) => a.index - b.index);
}