diff --git a/LifeOS/install/LIFEOS/TOOLS/IsaAlign.approval.test.ts b/LifeOS/install/LIFEOS/TOOLS/IsaAlign.approval.test.ts new file mode 100644 index 0000000000..7485703525 --- /dev/null +++ b/LifeOS/install/LIFEOS/TOOLS/IsaAlign.approval.test.ts @@ -0,0 +1,45 @@ +// Per-install approval phrases for restate answers (public issue #2595). Each test pins one of the +// conditions in that thread: CLEAN_YES unchanged, phrases from a USER/CONFIG file, accents folded (NFD), +// and approval plus a request kept unclean. +import { describe, expect, test } from "bun:test"; +import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "fs"; +import { tmpdir } from "os"; +import { join } from "path"; +import { approvalPhrases, isListedApproval, restateIsClean } from "./IsaAlign"; + +const block = (answer: string) => `Principal: "${answer}"`; +const dir = mkdtempSync(join(tmpdir(), "approval-")); +const file = (body: string) => { const p = join(dir, `p-${Math.random().toString(36).slice(2)}.txt`); writeFileSync(p, body); return p; }; +const pt = approvalPhrases(file("# Portuguese answers\nsim\naceito\né isso\nsim, por favor\nmanda ver # also a yes\n")); + +describe("CLEAN_YES is unchanged", () => { + test("an English yes is clean with no phrase file", () => expect(restateIsClean(block("yes"), new Set())).toBe(true)); + test("an English hedge stays unclean", () => expect(restateIsClean(block("yes but change L2"), new Set())).toBe(false)); +}); + +describe("phrases come from the install's file", () => { + test("no file means no extra phrases", () => expect(approvalPhrases(join(dir, "absent.txt")).size).toBe(0)); + test("without the file a Portuguese yes is unclean, as today", () => expect(restateIsClean(block("sim"), new Set())).toBe(false)); + test("comments and blank lines are ignored", () => expect([...pt].sort()).toEqual(["aceito", "e isso", "manda ver", "sim", "sim por favor"])); + test("a listed phrase is a clean yes", () => expect(restateIsClean(block("sim"), pt)).toBe(true)); + test("a read error is thrown, never read as no phrases", () => { + const d = join(dir, "is-a-dir.txt"); mkdirSync(d); + expect(() => approvalPhrases(d)).toThrow(); + }); +}); + +describe("accents do not matter (NFD)", () => { + test("listed with an accent, typed without", () => expect(isListedApproval("e isso", pt)).toBe(true)); + test("case and trailing punctuation", () => expect(isListedApproval("É ISSO.", pt)).toBe(true)); + test("listed without an accent, typed with one", () => expect(isListedApproval("é isso", approvalPhrases(file("e isso\n")))).toBe(true)); +}); + +describe("approval plus a request stays unclean", () => { + test("sim, mas o L2...", () => expect(restateIsClean(block("sim, mas o L2 precisa mudar"), pt)).toBe(false)); + test("a yes and then a change in a second sentence", () => expect(isListedApproval("sim. muda o L2.", pt)).toBe(false)); + test("two listed sentences are clean", () => expect(isListedApproval("sim. aceito.", pt)).toBe(true)); + test("a question is not a yes", () => expect(isListedApproval("sim?", pt)).toBe(false)); + test("commas do not split a listed phrase", () => expect(isListedApproval("Sim, por favor!", pt)).toBe(true)); +}); + +process.on("exit", () => rmSync(dir, { recursive: true, force: true })); diff --git a/LifeOS/install/LIFEOS/TOOLS/IsaAlign.ts b/LifeOS/install/LIFEOS/TOOLS/IsaAlign.ts index d4f33c1f45..15c23b4c1c 100644 --- a/LifeOS/install/LIFEOS/TOOLS/IsaAlign.ts +++ b/LifeOS/install/LIFEOS/TOOLS/IsaAlign.ts @@ -121,6 +121,40 @@ export function logBlocks(text: string): LogBlock[] { /** A restate answer that is a plain yes: the principal confirmed the ladder and nothing else. */ const CLEAN_YES = /^(?:y|yes|yep|yeah|yup|ok|okay|sure|correct|right|that'?s (?:it|right)|exactly|looks good|sounds good|lgtm|perfect|confirmed|go|do it|ship it|proceed|build (?:it|this)|yes,? (?:do it|build (?:it|this)|go|ship it))[\s.!]*$/i; + +/** + * This install's own approval phrases, for principals who answer in another language (public issue + * #2595): one phrase per line in USER/CONFIG/approval-phrases.txt, `#` starts a comment. No file, no + * extra phrases, so an install without one behaves exactly as CLEAN_YES alone. + */ +export const approvalPhrasesPath = (): string => join(getClaudeDir(), "LIFEOS", "USER", "CONFIG", "approval-phrases.txt"); + +/** Case, accents (NFD, marks dropped), quotes and commas do not matter; spaces collapse. */ +export function foldApproval(s: string): string { + return s.normalize("NFD").replace(/\p{M}+/gu, "").toLowerCase().replace(/[,"“”]/g, " ").replace(/\s+/g, " ").trim(); +} + +let approvalCache: Set | null = null; +export function approvalPhrases(path = approvalPhrasesPath()): Set { + if (approvalCache && path === approvalPhrasesPath()) return approvalCache; + // A missing file is the default (no extra phrases); any other read error is thrown, never read as empty. + const set = new Set(existsSync(path) + ? readFileSync(path, "utf8").split("\n").map((l) => foldApproval(l.replace(/#.*$/, ""))).filter(Boolean) + : []); + if (path === approvalPhrasesPath()) approvalCache = set; + return set; +} + +/** + * True when every sentence of the answer is one of the listed phrases: "sim. aceito." passes when both + * are listed, and "sim, mas o L2..." stays unclean because "sim mas o l2" is not a phrase. A question + * mark is kept, so "sim?" never matches "sim". + */ +export function isListedApproval(answer: string, phrases: Set): boolean { + if (!phrases.size) return false; + const sentences = answer.split(/[.;!]+/).map(foldApproval).filter(Boolean); + return sentences.length > 0 && sentences.every((s) => phrases.has(s)); +} /** * Answer lines are labelled "Principal", "A", or the configured principal's name (full or first). * The name comes from identity at first use, never from a literal in source: the release sanitizer @@ -148,11 +182,11 @@ function answerWords(line: string): string { * second answer in the same block, and some blocks carry unrelated follow-up lines after the yes. * A correction made after a yes belongs in a new `restate` block. */ -export function restateIsClean(body: string): boolean | null { +export function restateIsClean(body: string, phrases: Set = approvalPhrases()): boolean | null { if (/\bpending\b/i.test(body)) return null; const answers = [...body.matchAll(answerRegex())].map((m) => answerWords(m[1] ?? m[2] ?? "")); if (!answers.length) return null; - return answers.some((a) => CLEAN_YES.test(a)); + return answers.some((a) => CLEAN_YES.test(a) || isListedApproval(a, phrases)); } /** A discuss block that actually held a conversation: rounds or answers, and nothing left pending. */