Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(facts): speaker labels and digit-led names no longer block subject inference (#7827)
garrytan264aac7b4f6d
diff --git a/src/core/facts/subject-infer.ts b/src/core/facts/subject-infer.tsindex 07a16b599f..c74b94b09e 100644--- a/src/core/facts/subject-infer.ts+++ b/src/core/facts/subject-infer.ts@@ -113,6 +113,9 @@ const SENTENCE_OPENERS = new Set([*/const LABEL_OPENERS = new Set(['work', 'invoice', 'invoiced', 'billed', 'receipt', 'payment']);+/** #7827: the speaker labels the fact extractor writes ("User accepted ...", "Assistant recommended ...", extract.ts ATTRIBUTION_BLOCK). */+const ATTRIBUTION_LABELS = new Set(['user', 'assistant']);+/** #7405: ISO 4217 codes; one counts as a unit (not a name) only where every use sits next to an amount ("CHF 80", "80 CHF"). */const CURRENCY_CODES = new Set(Intl.supportedValuesOf('currency'));@@ -130,24 +133,48 @@ const SLUG_IN_TEXT_RE = new RegExp(`(?<!${SLUG_EDGE})${SLUG_SEG}(?:/${SLUG_SEG})/** Two distinct entities already make the subject unknown; more resolution is wasted work. */const DISTINCT_ENTITY_LIMIT = 2;+/**+ * #7827: a run of capitalised and digit-led tokens ("Acme 42", "42 Acme"). extractCandidates keeps only the capitalised+ * words of such a name, so inference tries the whole run as well; it counts only when it resolves to a page.+ */+const NAME_TOKEN = "(?:\\p{Lu}[\\p{L}\\p{N}'’-]*|\\p{N}[\\p{L}\\p{N}'’-]*)";+const DIGIT_NAME_RUN_RE = new RegExp(`(?<![\\p{L}\\p{N}'’-])${NAME_TOKEN}(?:\\s+${NAME_TOKEN}){1,3}(?![\\p{L}\\p{N}'’-])`, 'gu');+export async function inferFactSubject(engine: BrainEngine, sourceId: string, input: InferSubjectInput): Promise<InferResult> {+ const memo = new Map<string, StrictResolution>();+ const resolve = async (query: string): Promise<StrictResolution> => {+ const key = query.toLowerCase();+ let r = memo.get(key);+ if (!r) {+ r = await resolveStrictEntityReference(engine, sourceId, query,+ { sameName: input.mode === 'relink', excludePrivate: input.excludePrivate });+ memo.set(key, r);+ }+ return r;+ };+ // A digit-led name that resolves replaces the capitalised words inside it, which alone name no page.+ const digitNames: Array<{ query: string; start: number; end: number }> = [];+ for (const m of input.fact.matchAll(DIGIT_NAME_RUN_RE)) {+ const tokens = m[0].split(/\s+/);+ if (!tokens.some(t => /^\p{N}/u.test(t)) || !tokens.some(t => /^\p{Lu}/u.test(t))) continue;+ const query = (SENTENCE_OPENERS.has(tokens[0]!.toLowerCase()) ? tokens.slice(1) : tokens).join(' ');+ if ((await resolve(query)).slug !== null) digitNames.push({ query, start: m.index, end: m.index + m[0].length });+ }+ const insideDigitName = (query: string) => {+ const uses = referenceUses(input.fact, query);+ return uses.length > 0 && uses.every(at => digitNames.some(d => at >= d.start && at + query.length <= d.end));+ };const candidates: Array<{ query: string; sentenceStart?: true }> = [...[...new Set(input.fact.match(SLUG_IN_TEXT_RE) ?? [])].map(query => ({ query })),- ...extractCandidates(input.fact).filter(c => !c.weak).map(shedOpener),+ ...digitNames.map(({ query }) => ({ query })),+ ...extractCandidates(input.fact).filter(c => !c.weak).map(shedOpener).filter(c => !insideDigitName(c.query)),];const resolved = new Set<string>();const subjects = new Set<string>();const misses: Array<Exclude<StrictResolution, { slug: string }>['miss']> = [];let vetoed = false;- const memo = new Map<string, StrictResolution>();for (const candidate of candidates) {- const key = candidate.query.toLowerCase();- let r = memo.get(key);- if (!r) {- r = await resolveStrictEntityReference(engine, sourceId, candidate.query,- { sameName: input.mode === 'relink', excludePrivate: input.excludePrivate });- memo.set(key, r);- }+ const r = await resolve(candidate.query);if (r.slug !== null) {resolved.add(r.slug);if (!onlyExampleUses(input.fact, candidate.query)) subjects.add(r.slug);@@ -186,7 +213,7 @@ function shedOpener<T extends { query: string }>(c: T): T {function isNonName(query: string): boolean {if (/\s/.test(query)) return false;- return isCommonCapitalizedWord(query) || ROLE_AND_TERM_TOKENS.has(query.toLowerCase());+ return isCommonCapitalizedWord(query) || ROLE_AND_TERM_TOKENS.has(query.toLowerCase()) || ATTRIBUTION_LABELS.has(query.toLowerCase());}const WORD_CHAR = /[\p{L}\p{N}_/-]/u;diff --git a/test/facts-subject-infer-7827.test.ts b/test/facts-subject-infer-7827.test.tsnew file mode 100644index 0000000000..0b739841da--- /dev/null+++ b/test/facts-subject-infer-7827.test.ts@@ -0,0 +1,105 @@+/**+ * #7827: write-time fact subject inference.+ *+ * 1. Protects: the speaker labels the fact extractor itself writes ("User accepted ...", "Assistant recommended ...",+ * src/core/facts/extract.ts ATTRIBUTION_BLOCK) are no competing name, so a fact that names one entity page links to+ * it; a page actually named like a label still competes, and an unknown second name still vetoes.+ * 2. Fails when: an attribution label with no page vetoes the link again.+ * 3. Why existing coverage misses it: facts-subject-infer*.test.ts never open a fact with a speaker label.+ * Synthetic data only; PGLite always, Postgres when DATABASE_URL is set (testBackends).+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import type { BrainEngine } from '../src/core/engine.ts';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { importFromContent } from '../src/core/import-file.ts';+import { inferFactSubject } from '../src/core/facts/subject-infer.ts';+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';+import { testBackends } from './helpers/test-backends.ts';++const backends = testBackends();+const engines: Array<{ backend: string; engine: BrainEngine }> = [];+let closePostgres: (() => Promise<void>) | undefined;++const page = (engine: BrainEngine, slug: string, title: string, type: string, sourceId = 'default') =>+ importFromContent(engine, slug, '---\ntitle: ' + title + '\ntype: ' + type + '\nfacts_backstop: false\n---\n\n# ' + title + '\n', { noEmbed: true, sourceId });++beforeAll(async () => {+ if (backends.includes('pglite')) {+ const engine = new PGLiteEngine();+ await engine.connect({}); await engine.initSchema();+ engines.push({ backend: 'pglite', engine });+ }+ if (backends.includes('postgres')) {+ const pg = await isolatedPersistencePostgres(process.env.DATABASE_URL!);+ engines.push({ backend: 'postgres', engine: pg.engine }); closePostgres = pg.close;+ }+ for (const { engine } of engines) {+ await engine.executeRaw("INSERT INTO sources (id, name) VALUES ('source-a', 'source-a') ON CONFLICT DO NOTHING");+ for (const sourceId of ['default', 'source-a']) {+ await page(engine, 'people/alice-example', 'Alice Example', 'person', sourceId);+ await page(engine, 'companies/acme-example', 'Acme Example', 'company', sourceId);+ }+ await engine.executeRaw("INSERT INTO sources (id, name) VALUES ('source-b', 'source-b') ON CONFLICT DO NOTHING");+ // source-b holds names with a digit-led token, beside one without.+ await page(engine, 'companies/acme-42', 'Acme 42', 'company', 'source-b');+ await page(engine, 'companies/42-northstar', '42 Northstar', 'company', 'source-b');+ await page(engine, 'people/alice-example', 'Alice Example', 'person', 'source-b');+ // source-a also holds real entities named like the two speaker labels.+ for (const title of ['User', 'Assistant']) await page(engine, 'companies/' + title.toLowerCase(), title, 'company', 'source-a');+ }+}, 120_000);++afterAll(async () => {+ for (const { engine } of engines) await engine.disconnect();+ await closePostgres?.();+});++const ALICE = 'people/alice-example';+const ACME = 'companies/acme-example';+const CASES: Array<{ id: string; fact: string; expect: string | null; sourceId?: string }> = [+ { id: 'user-label', fact: 'User met Alice Example on Monday', expect: ALICE },+ { id: 'user-accepted', fact: "User accepted the assistant's suggestion to email Alice Example", expect: ALICE },+ { id: 'assistant-label', fact: 'Assistant recommended Acme Example for the rollout', expect: ACME },+ { id: 'label-mid-sentence', fact: 'Alice Example asked User to send the deck', expect: ALICE },+ { id: 'label-and-unknown-name', fact: 'User met Northstar Example and Alice Example', expect: null },+ { id: 'label-and-two-entities', fact: 'Assistant said Acme Example hired Alice Example', expect: null },+ { id: 'label-page-competes', fact: 'User met Alice Example on Monday', expect: null, sourceId: 'source-a' },+ { id: 'assistant-page-competes', fact: 'Assistant recommended Acme Example for the rollout', expect: null, sourceId: 'source-a' },+];++const DIGIT_CASES: Array<{ id: string; fact: string; expect: string | null }> = [+ { id: 'trailing-digit-token', fact: 'Met Acme 42 on Monday', expect: 'companies/acme-42' },+ { id: 'leading-digit-token', fact: 'Signed the lease with 42 Northstar', expect: 'companies/42-northstar' },+ { id: 'digit-name-and-person', fact: 'Alice Example joined Acme 42', expect: null },+ { id: 'bare-prefix-still-vetoes', fact: 'Met Acme on Monday', expect: null },+ { id: 'other-number-still-vetoes', fact: 'Met Acme 43 on Monday', expect: null },+ { id: 'year-after-person', fact: 'Alice Example moved in 2026', expect: 'people/alice-example' },+];++describe('#7827 fact subject inference: names with a digit-led token', () => {+ for (const backend of backends) {+ const engineFor = () => engines.find(e => e.backend === backend)!.engine;+ for (const mode of ['relink', 'write'] as const) {+ for (const c of DIGIT_CASES) {+ test('[' + backend + '] ' + mode + ': ' + c.id, async () => {+ const result = await inferFactSubject(engineFor(), 'source-b', { fact: c.fact, mode });+ expect(result.slug).toBe(c.expect);+ });+ }+ }+ }+});++describe('#7827 fact subject inference: speaker labels are not names', () => {+ for (const backend of backends) {+ const engineFor = () => engines.find(e => e.backend === backend)!.engine;+ for (const mode of ['relink', 'write'] as const) {+ for (const c of CASES) {+ test('[' + backend + '] ' + mode + ': ' + c.id, async () => {+ const result = await inferFactSubject(engineFor(), c.sourceId ?? 'default', { fact: c.fact, mode });+ expect(result.slug).toBe(c.expect);+ });+ }+ }+ }+});