Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: ingest attaches an ambiguous name to neither namesake and writes its claim nowhere

garrytanaa62ad8688c5
diff --git a/docs/guides/ingest.md b/docs/guides/ingest.md
index 94c8b92a96..15d7279d8b 100644
--- a/docs/guides/ingest.md
+++ b/docs/guides/ingest.md
@@ -38,7 +38,9 @@ What gets written:
A name one edit or one acronym letter away from an existing entity name or alias (`ACMX` next to `ACME`) is
flagged in `near_names` and proposed, never merged
-([entity detection](entity-detection.md#ingest)).
+([entity detection](entity-detection.md#ingest)). A name that matches more than one existing page (two people
+named Alice Example) attaches to neither: it is `skipped: ambiguous_name` with the candidates in `near_names`, and
+a claim naming only that entity is `skipped: ambiguous_entity`.
## Keyless and keyed runs
diff --git a/src/core/ingest/plan.ts b/src/core/ingest/plan.ts
index 8455131bed..3295e9a4b4 100644
--- a/src/core/ingest/plan.ts
+++ b/src/core/ingest/plan.ts
@@ -7,7 +7,7 @@
import { createHash } from 'node:crypto';
import type { OperationContext } from '../ops/contract.ts';
import { enforceClientSlugFence, enforceSubagentSlugFence, validatePageSlug } from '../ops/context.ts';
-import { resolveStrictEntityReference } from '../entities/resolve.ts';
+import { entityReferenceCandidates, resolveStrictEntityReference } from '../entities/resolve.ts';
import {
buildNearNameIndex, classifyNearName, loadEntityNames, queryNearNames, shardCountFor, type NearNameMatch,
} from '../entities/near-name.ts';
@@ -126,8 +126,15 @@ export async function planIngest(input: PlanInput): Promise<IngestPlan> {
if (type === 'person' && !/\s/.test(e.name.trim())) { skip('bare_first_name'); continue; }
const strict = await resolveStrictEntityReference(ctx.engine, sourceId, e.name, { sameName: true, excludePrivate });
const near: NearNameMatch[] = indexes.flatMap(index => queryNearNames(index, e.name));
- const same = near.find(m => m.kind === 'same');
- const existingSlug = strict.slug ?? same?.slug ?? null;
+ const same = near.filter(m => m.kind === 'same');
+ // An ambiguous name attaches to no namesake: skipped with every candidate named, as remember refuses it.
+ if (strict.slug === null && (strict.miss === 'ambiguous' || new Set(same.map(m => m.slug)).size > 1)) {
+ const candidates = [...new Set([...same.map(m => m.slug), ...await entityReferenceCandidates(ctx.engine, sourceId, e.name, { excludePrivate })])];
+ const page: PlannedPage = { ...base, type, slug: '', status: 'skipped', reason: 'ambiguous_name', near_names: candidates.map(slug => ({ slug, name: e.name, kind: 'same' })) };
+ pages.push(page); byName.set(key, page);
+ continue;
+ }
+ const existingSlug = strict.slug ?? same[0]?.slug ?? null;
if (existingSlug) {
const page: PlannedPage = insideGrant(ctx, existingSlug)
? { ...base, type, slug: existingSlug, status: 'existing' }
@@ -171,7 +178,11 @@ export async function planIngest(input: PlanInput): Promise<IngestPlan> {
for (const n of f.entities) { const p = byName.get(n.toLowerCase()); if (p && p.status !== 'skipped') named.set(p.slug, p); }
for (const p of pages) if (p.status !== 'skipped' && !named.has(p.slug) && firstMention(f.fact, p.name) !== Number.POSITIVE_INFINITY) named.set(p.slug, p);
const targets = [...named.values()].sort((a, b) => (a.first_mention < 0 ? Infinity : a.first_mention) - (b.first_mention < 0 ? Infinity : b.first_mention)).slice(0, 2);
- if (!targets.length) { facts.push({ ...base, status: 'skipped', reason: 'no_entity', copies: [] }); continue; }
+ if (!targets.length) {
+ const ambiguous = f.entities.some(n => byName.get(n.toLowerCase())?.reason === 'ambiguous_name');
+ facts.push({ ...base, status: 'skipped', reason: ambiguous ? 'ambiguous_entity' : 'no_entity', copies: [] });
+ continue;
+ }
const copies: PlannedCopy[] = [];
for (const t of targets) {
if (t.status !== 'existing') { copies.push({ entity_slug: t.slug, status: 'planned' }); continue; }
diff --git a/test/e2e/ingest-postgres.test.ts b/test/e2e/ingest-postgres.test.ts
index 743a3eddd0..05c311cdd1 100644
--- a/test/e2e/ingest-postgres.test.ts
+++ b/test/e2e/ingest-postgres.test.ts
@@ -82,6 +82,7 @@ d('ingest and ingest_undo (live Postgres)', () => {
test('undo keeps changed pages (' + label + ')', async () => { fx = await fixture(storage); await S.undoKeepsChangedPagesScenario(engine); }, 90_000);
test('undo keeps relied-on claims (' + label + ')', async () => { fx = await fixture(storage); await S.undoKeepsReliedOnScenario(engine); }, 90_000);
test('undo unverifiable (' + label + ')', async () => { fx = await fixture(storage); await S.undoUnverifiableScenario(engine); }, 60_000);
+ test('ambiguous namesake (' + label + ')', async () => { fx = await fixture(storage); await S.ambiguousNamesakeScenario(engine); }, 60_000);
}
test('Git target unavailable refuses before writes', async () => { fx = await fixture('git'); await S.storageUnavailableScenario(engine); }, 60_000);
});
diff --git a/test/helpers/ingest-scenarios.ts b/test/helpers/ingest-scenarios.ts
index 2a0d2cd514..d20225a9ea 100644
--- a/test/helpers/ingest-scenarios.ts
+++ b/test/helpers/ingest-scenarios.ts
@@ -689,3 +689,30 @@ export async function failedWindowSpendScenario(engine: BrainEngine): Promise<vo
expect(first.spent_usd).toBeCloseTo(2 * Number(state.first), 6);
} finally { stub.restore(); }
}
+
+
+/**
+ * Item A (outside review of #8481): two distinct pages share the name the document uses. The strict resolver answers
+ * ambiguous, so ingest attaches the name to neither namesake: the name is skipped with both candidates named and its
+ * claim is written to no page.
+ */
+export async function ambiguousNamesakeScenario(engine: BrainEngine): Promise<void> {
+ const put = operationsByName.put_page!;
+ for (const suffix of ['a', 'b']) {
+ await put.handler(reviewCtx(engine), { slug: 'people/alice-example-' + suffix, content: '---\ntype: person\ntitle: Alice Example\n---\n\nAlice Example ' + suffix + ' is a founder.\n' });
+ }
+ await drain(engine);
+ const { resolveStrictEntityReference } = await import('../../src/core/entities/resolve.ts');
+ expect(await resolveStrictEntityReference(engine, 'default', 'Alice Example', { sameName: true })).toEqual({ slug: null, miss: 'ambiguous' });
+ const stub = stubChat({ rows: [{ marker: 'moved to Lisbon', entities: [{ name: 'Alice Example', type: 'person', context: 'Alice Example moved to Lisbon in March.' }],
+ facts: [{ fact: 'Alice Example moved to Lisbon in March.', kind: 'event', entities: ['Alice Example'] }] }] });
+ try {
+ const report = await ingest(engine, { content: 'Alice Example moved to Lisbon in March.', title: 'Call notes' });
+ const alice = report.pages.find((p: Report) => p.name === 'Alice Example');
+ expect(alice).toMatchObject({ status: 'skipped', reason: 'ambiguous_name' });
+ expect(alice.near_names.map((n: Report) => n.slug).sort()).toEqual(['people/alice-example-a', 'people/alice-example-b']);
+ expect(report.facts.find((f: Report) => f.fact === 'Alice Example moved to Lisbon in March.')).toMatchObject({ status: 'skipped', reason: 'ambiguous_entity' });
+ expect((await factsOn(engine, 'people/alice-example-a')).length).toBe(0);
+ expect((await factsOn(engine, 'people/alice-example-b')).length).toBe(0);
+ } finally { stub.restore(); }
+}
diff --git a/test/ingest.serial.test.ts b/test/ingest.serial.test.ts
index 388aea5c71..76924257a4 100644
--- a/test/ingest.serial.test.ts
+++ b/test/ingest.serial.test.ts
@@ -128,6 +128,10 @@ for (const storage of ['git', 'database'] as ReviewStorage[]) {
fx = await reviewDoorFixture(engine, storage);
await S.undoUnverifiableScenario(engine);
}, 60_000);
+ test('an ambiguous name attaches to neither namesake, and its claim is written nowhere', async () => {
+ fx = await reviewDoorFixture(engine, storage);
+ await S.ambiguousNamesakeScenario(engine);
+ }, 60_000);
});
}