Local-first 🧠 memory for Hermes Agent that lives in your Obsidian vault and remembers project context. Nightly 😴 dream passes turn repeat corrections into confirmed preferences with measurable confidence. Adapters ship for Claude Code, Codex, and OpenClaw, with an MCP server for anything else.
TypeScript
/**
* The quote-span finder and the comparison-only normaliser.
*
* Every case is built from code points, never from a list of words or
* languages: a delimiter is any `\p{Quotation_Mark}` code point, pairing is
* decided by position (what stands on either side of the mark), and a mark
* with a letter on both sides is an apostrophe whatever script the letters
* come from.
*/
import { describe, expect, test } from "bun:test";
import {
findQuoteSpans,
normalizeForQuoteComparison,
QUOTATION_MARK_RE,
spanOccursIn,
splitEllipsisFragments,
unquoteSpans,
} from "../../../../src/core/brain/distill/quote-spans.ts";
import { LINEAR_CEILING_MS } from "../../../helpers/linear-time.ts";
const cp = (code: number): string => String.fromCodePoint(code);
/** Opener and closer pairs, every one a `Quotation_Mark` code point. */
const PAIRS: ReadonlyArray<readonly [number, number]> = [
[0x22, 0x22],
[0x27, 0x27],
[0xab, 0xbb],
[0xbb, 0xab],
[0x201c, 0x201d],
[0x201e, 0x201c],
[0x2018, 0x2019],
[0x201a, 0x2018],
[0x300c, 0x300d],
[0x300e, 0x300f],
[0x301d, 0x301e],
];
/** One letter from each of three scripts, generated by code point. */
const LETTERS: ReadonlyArray<string> = [0x61, 0x430, 0x3b1].map(cp);
describe("QUOTATION_MARK_RE", () => {
test("every pair member is a quotation mark", () => {
for (const [open, close] of PAIRS) {
expect(QUOTATION_MARK_RE.test(cp(open))).toBe(true);
expect(QUOTATION_MARK_RE.test(cp(close))).toBe(true);
}
});
});
describe("findQuoteSpans", () => {
test("every pair forms exactly one span with the right inner text", () => {
for (const [open, close] of PAIRS) {
const inner = `x${LETTERS[1]} y`;
const text = `said ${cp(open)}${inner}${cp(close)} today`;
const scan = findQuoteSpans(text);
expect(scan.unpaired).toBe(0);
expect(scan.spans).toHaveLength(1);
const span = scan.spans[0];
expect(span?.inner).toBe(inner);
expect(span?.open).toBe(5);
expect(span?.close).toBe(5 + 1 + inner.length);
}
});
test("a non-directional mark between two letters of any script is never a delimiter", () => {
// Corner brackets and low-9 marks carry their direction in their own
// category (Ps/Pe) and are never apostrophes; they are covered below.
const ambiguous = PAIRS.filter(([, close]) => !/[\p{Ps}\p{Pe}]/u.test(cp(close)));
expect(ambiguous.length).toBeGreaterThan(0);
for (const [, close] of ambiguous) {
for (const left of LETTERS) {
for (const right of LETTERS) {
const scan = findQuoteSpans(`${left}${cp(close)}${right}`);
expect(scan.spans).toHaveLength(0);
expect(scan.unpaired).toBe(0);
}
}
}
});
test("an apostrophe inside a quote does not end it", () => {
const scan = findQuoteSpans(`${cp(0x201c)}it${cp(0x2019)}s fine${cp(0x201d)}`);
expect(scan.spans.map((s) => s.inner)).toEqual([`it${cp(0x2019)}s fine`]);
});
test("a nested quote yields only the outer span", () => {
const text = `${cp(0x201c)}a ${cp(0x2018)}b${cp(0x2019)} c${cp(0x201d)}`;
const scan = findQuoteSpans(text);
expect(scan.spans).toHaveLength(1);
expect(scan.spans[0]?.inner).toBe(`a ${cp(0x2018)}b${cp(0x2019)} c`);
expect(scan.unpaired).toBe(0);
});
test("a lone opener is unpaired and not a span", () => {
const scan = findQuoteSpans(`he said ${cp(0x201c)}never closed`);
expect(scan.spans).toHaveLength(0);
expect(scan.unpaired).toBe(1);
});
test("an empty or whitespace-only pair is not a span", () => {
expect(findQuoteSpans(`x "" y`).spans).toHaveLength(0);
expect(findQuoteSpans(`x ${cp(0x201c)} ${cp(0x201d)} y`).spans).toHaveLength(0);
});
test("corner brackets glued to letters on both sides still pair (CJK running text)", () => {
const han = [0x4ed6, 0x8bf4].map(cp).join("");
const inner = [0x4f60, 0x597d].map(cp).join("");
for (const [open, close] of [
[0x300c, 0x300d],
[0x300e, 0x300f],
] as const) {
const scan = findQuoteSpans(`${han}${cp(open)}${inner}${cp(close)}${cp(0x3002)}`);
expect(scan.spans.map((s) => s.inner)).toEqual([inner]);
expect(scan.unpaired).toBe(0);
}
});
test("an opener that never closes does not hide a later quote", () => {
const scan = findQuoteSpans(`the '90s were "great"`);
expect(scan.spans.map((s) => s.inner)).toEqual(["great"]);
expect(scan.unpaired).toBe(1);
});
test("a lone closer is unpaired and not a span", () => {
const scan = findQuoteSpans(`said today${cp(0x201d)}`);
expect(scan.spans).toHaveLength(0);
expect(scan.unpaired).toBe(1);
});
test("guillemets with inner spaces are counted as unpaired, never as a span", () => {
const scan = findQuoteSpans(`Il a dit ${cp(0xab)} bonjour ${cp(0xbb)} hier.`);
expect(scan.spans).toHaveLength(0);
expect(scan.unpaired).toBe(2);
});
test("corner brackets pair in CJK running text after letters and full-width punctuation", () => {
for (const [text, inner] of [
["他说「你好」。", "你好"],
["他说:「你好」。", "你好"],
["「こんにちは」と言った", "こんにちは"],
] as const) {
const scan = findQuoteSpans(text);
expect(scan.spans.map((s) => s.inner)).toEqual([inner]);
expect(scan.unpaired).toBe(0);
}
});
test("a possessive apostrophe never closes a double quote, and unquoting keeps it", () => {
const text = `"the students' work" is fine`;
const scan = findQuoteSpans(text);
expect(scan.spans.map((s) => s.inner)).toEqual(["the students' work"]);
expect(unquoteSpans(text, scan.spans)).toBe("the students' work is fine");
});
test("a single-width opener left open inside a double quote does not hide it", () => {
const scan = findQuoteSpans(`"back in the '90s it was" ok`);
expect(scan.spans.map((s) => s.inner)).toEqual(["back in the '90s it was"]);
expect(scan.unpaired).toBe(1);
});
test("an opener directly after a dash or a colon opens", () => {
expect(findQuoteSpans(`${cp(0x2014)}"quote" here`).spans.map((s) => s.inner)).toEqual([
"quote",
]);
expect(findQuoteSpans(`He said:"xy"`).spans.map((s) => s.inner)).toEqual(["xy"]);
const scan = findQuoteSpans(
`${cp(0x201c)}Aa${cp(0x201d)}${cp(0x2014)}${cp(0x201c)}Bb${cp(0x201d)}`,
);
expect(scan.spans.map((s) => s.inner)).toEqual(["Aa", "Bb"]);
expect(scan.unpaired).toBe(0);
});
test("one no-break, narrow no-break or thin space inside guillemets is transparent", () => {
for (const space of [0xa0, 0x202f, 0x2009].map(cp)) {
const scan = findQuoteSpans(`Il a dit ${cp(0xab)}${space}bonjour${space}${cp(0xbb)} hier.`);
expect(scan.spans.map((s) => s.inner.trim())).toEqual(["bonjour"]);
expect(scan.unpaired).toBe(0);
}
});
test("a pair holding fewer than two letters or digits is not a span", () => {
const scan = findQuoteSpans(`rock 'n' roll`);
expect(scan.spans).toHaveLength(0);
expect(scan.unpaired).toBe(2);
expect(findQuoteSpans(`said "ok" twice`).spans.map((s) => s.inner)).toEqual(["ok"]);
});
test("stays linear on long runs of marks", () => {
const size = 256 * 1024;
for (const text of [
`${" 'a".repeat(size / 3)} "x"`,
`"ab" `.repeat(size / 5),
cp(0x201c).repeat(size),
`${" 'a".repeat(size / 6)}${'b" '.repeat(size / 6)}`,
]) {
const started = performance.now();
findQuoteSpans(text);
expect(performance.now() - started).toBeLessThan(LINEAR_CEILING_MS);
}
});
test("two quotes in one text are two spans in order", () => {
const scan = findQuoteSpans(`"one" and "two".`);
expect(scan.spans.map((s) => s.inner)).toEqual(["one", "two"]);
});
});
describe("unquoteSpans", () => {
test("removes exactly the two marks of each span and keeps every other byte", () => {
const text = `A ${cp(0x201e)}b c${cp(0x201c)}, then "dd" and it${cp(0x2019)}s "e`;
const scan = findQuoteSpans(text);
expect(scan.spans).toHaveLength(2);
expect(scan.unpaired).toBe(1);
expect(unquoteSpans(text, scan.spans)).toBe(`A b c, then dd and it${cp(0x2019)}s "e`);
});
test("removing a subset leaves the other spans quoted", () => {
const text = `"one" and "two"`;
const scan = findQuoteSpans(text);
expect(unquoteSpans(text, scan.spans.slice(1))).toBe(`"one" and two`);
});
});
describe("normalizeForQuoteComparison", () => {
test("folds NFD to NFC", () => {
expect(normalizeForQuoteComparison("été")).toBe("été");
});
test("reduces inline Markdown to display text", () => {
expect(normalizeForQuoteComparison("a **b** _i_ `c` ~~s~~ [t](u) [[p|a2]] [[p]]")).toBe(
"a b i c s t a2 p",
);
});
test("removes line-leading block markers and the trailing block id", () => {
expect(normalizeForQuoteComparison("> quoted line\n- item one\n1. item two ^blk-1")).toBe(
"quoted line item one item two",
);
});
test("removes only paired emphasis delimiters, never an underscore or star inside a word", () => {
expect(normalizeForQuoteComparison("snake_case 2*3")).toBe("snake_case 2*3");
expect(normalizeForQuoteComparison("a *b* __c__ d_e_f")).toBe("a b c d_e_f");
});
test("a numbered line inside a paragraph is text; a list after a blank line is a list", () => {
expect(normalizeForQuoteComparison("Intro line\n1999) was a year")).toBe(
"Intro line 1999) was a year",
);
expect(normalizeForQuoteComparison("Intro line\n\n1999) was a year")).toBe(
"Intro line was a year",
);
expect(normalizeForQuoteComparison("Key points:\n- one\n2. two")).toBe("Key points: one two");
});
test("removes a trailing block id before a CR", () => {
expect(normalizeForQuoteComparison("first line ^p1\r\nsecond")).toBe("first line second");
expect(normalizeForQuoteComparison("glued^p1")).toBe("glued^p1");
});
test("bracket-type quotation marks fold to the double mark for comparison", () => {
const low9 = `Er sagte ${cp(0x201e)}ja${cp(0x201c)} dann`;
const curly = `Er sagte ${cp(0x201c)}ja${cp(0x201d)} dann`;
expect(normalizeForQuoteComparison(low9)).toBe(normalizeForQuoteComparison(curly));
expect(normalizeForQuoteComparison(`${cp(0x300c)}ja${cp(0x300d)} (x)`)).toBe(`"ja" (x)`);
});
test("stays linear on long runs of spaces and brackets", () => {
const size = 256 * 1024;
for (const text of [
`a${" ".repeat(size)}b`,
"[[".repeat(size / 2),
"[".repeat(size),
"*a ".repeat(size / 3),
`${"^".repeat(size)} x`,
]) {
const started = performance.now();
normalizeForQuoteComparison(text);
expect(performance.now() - started).toBeLessThan(LINEAR_CEILING_MS);
}
});
test("collapses whitespace and trims", () => {
expect(normalizeForQuoteComparison(" a \n\n\t b ")).toBe("a b");
});
test("folds quote-mark width but keeps case and other punctuation", () => {
expect(normalizeForQuoteComparison(`It${cp(0x2019)}s ${cp(0x201c)}A${cp(0x201d)}, B!`)).toBe(
`It's "A", B!`,
);
expect(normalizeForQuoteComparison("Case Stays.")).toBe("Case Stays.");
});
});
describe("ellipsis fragments", () => {
test("splits on U+2026 and on three or more dots, dropping empty fragments", () => {
expect(splitEllipsisFragments(`a${cp(0x2026)} b .... c...`)).toEqual(["a", "b", "c"]);
expect(splitEllipsisFragments("no ellipsis. here..")).toEqual(["no ellipsis. here.."]);
expect(splitEllipsisFragments(`${cp(0x2026)}`)).toEqual([]);
});
test("fragments must occur in order and without overlap", () => {
const haystack = normalizeForQuoteComparison("the quick brown fox jumps over the dog");
expect(spanOccursIn("quick brown fox", haystack)).toBe(true);
expect(spanOccursIn(`quick${cp(0x2026)}jumps over`, haystack)).toBe(true);
expect(spanOccursIn(`jumps${cp(0x2026)}quick`, haystack)).toBe(false);
expect(spanOccursIn("brown fox...fox jumps", haystack)).toBe(false);
expect(spanOccursIn("quick [sic] brown", haystack)).toBe(false);
expect(spanOccursIn("Quick brown", haystack)).toBe(false);
expect(spanOccursIn(`${cp(0x2026)}`, haystack)).toBe(false);
});
test("the span side is normalised the same way as the haystack", () => {
const haystack = normalizeForQuoteComparison("he said **very** clearly");
expect(spanOccursIn("said very clearly", haystack)).toBe(true);
});
});
describe("word boundaries at the outer ends of a span", () => {
test("a span inside a longer word does not verify", () => {
const haystack = normalizeForQuoteComparison("The method is unsafe.");
expect(spanOccursIn("safe", haystack)).toBe(false);
expect(spanOccursIn("method i", haystack)).toBe(false);
});
test("a span that starts and ends on word boundaries verifies", () => {
const haystack = normalizeForQuoteComparison("The method is unsafe.");
expect(spanOccursIn("unsafe", haystack)).toBe(true);
expect(spanOccursIn("is unsafe.", haystack)).toBe(true);
expect(spanOccursIn("The method", haystack)).toBe(true);
});
test("a span of word-segmented text without spaces verifies inside running text", () => {
const sentence = [0x4ed6, 0x8bf4, 0x4f60, 0x597d, 0x5417, 0x3002].map(cp).join("");
const span = [0x4f60, 0x597d].map(cp).join("");
expect(spanOccursIn(span, normalizeForQuoteComparison(sentence))).toBe(true);
});
test("an ellipsis edge may cut a word, an outer edge may not", () => {
const haystack = normalizeForQuoteComparison("the reconfiguration was unsafe today");
expect(spanOccursIn(`the reconfig${cp(0x2026)}unsafe today`, haystack)).toBe(true);
expect(spanOccursIn(`${cp(0x2026)}configuration was`, haystack)).toBe(true);
expect(spanOccursIn(`was uns${cp(0x2026)}`, haystack)).toBe(true);
expect(spanOccursIn(`he reconfig${cp(0x2026)}today`, haystack)).toBe(false);
expect(spanOccursIn(`the${cp(0x2026)}safe today`, haystack)).toBe(true);
expect(spanOccursIn(`the${cp(0x2026)}today unsafe`, haystack)).toBe(false);
});
});
describe("line markers on the span side", () => {
test("a span's own leading list number is wording, not a marker", () => {
expect(spanOccursIn("3. the cat sat", normalizeForQuoteComparison("the cat sat"))).toBe(false);
expect(spanOccursIn("1) the cat sat", normalizeForQuoteComparison("the cat sat"))).toBe(false);
});
test("a list marker on the evidence side is still removed", () => {
expect(spanOccursIn("the cat sat", normalizeForQuoteComparison("3. the cat sat"))).toBe(true);
});
test("a span's own leading quote or bullet marker is wording, not a marker", () => {
expect(spanOccursIn("> the cat sat", normalizeForQuoteComparison("the cat sat"))).toBe(false);
expect(spanOccursIn("- the cat sat", normalizeForQuoteComparison("the cat sat"))).toBe(false);
});
});