Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

recall: one gate statement before the name-split resolution, so a topic-phrase miss costs one round trip, not eight (retrieval round-trip budget)

garrytanbc2f59c7c499
diff --git a/src/commands/recall.ts b/src/commands/recall.ts
index 8a439b1c9b..6c0bf8e2f3 100644
--- a/src/commands/recall.ts
+++ b/src/commands/recall.ts
@@ -576,8 +576,10 @@ function noteEntitySplit(positional: string, name: string, slug: string, topic:
async function entitySplitLocal(engine: BrainEngine, flags: ParsedFlags, sourceId: string, resolvedSince: Date | null): Promise<FactRow[]> {
if (!entitySplitApplies(flags)) return [];
+ const spans = entitySplitSpans(flags.entity!);
+ if (!await splitMayMatch(engine, sourceId, spans.map(s => s.name))) return [];
const { resolveEntitySlugWithSource } = await import('../core/entities/resolve.ts');
- for (const span of entitySplitSpans(flags.entity!)) {
+ for (const span of spans) {
const resolved = await resolveEntitySlugWithSource(engine, sourceId, span.name);
if (!resolved || resolved.source === 'fallback_slugify') continue;
const rows = await fetchRowsLocal(engine, { ...flags, entity: resolved.slug, entityExplicit: true }, sourceId, resolvedSince);
@@ -588,6 +590,30 @@ async function entitySplitLocal(engine: BrainEngine, flags: ParsedFlags, sourceI
return [];
}
+/**
+ * One statement before the per-span resolution (four statements a span): a
+ * span can only show facts when some fact is keyed under a slug whose last
+ * segment starts with its name, or the name is a page alias. A miss, the
+ * common case for a topic phrase, then costs one round trip instead of up to
+ * twenty-four (test/e2e/retrieval-round-trip-budget.test.ts).
+ */
+async function splitMayMatch(engine: BrainEngine, sourceId: string, names: string[]): Promise<boolean> {
+ const { slugify } = await import('../core/entities/resolve.ts');
+ const { normalizeAlias } = await import('../core/search/alias-normalize.ts');
+ const tokens = [...new Set(names.map(n => slugify(n)).filter(Boolean))];
+ if (!tokens.length) return false;
+ try {
+ const rows = await engine.executeRaw(
+ `SELECT 1 FROM facts WHERE source_id = $1 AND entity_slug ~ ANY($2::text[])
+ UNION ALL SELECT 1 FROM page_aliases WHERE source_id = $1 AND alias_norm = ANY($3::text[])
+ LIMIT 1`,
+ [sourceId, tokens.map(t => `(^|/)${t}(-|$)`), [...new Set(names.map(n => normalizeAlias(n)).filter(Boolean))]]);
+ return rows.length > 0;
+ } catch {
+ return true;
+ }
+}
+
async function runRecallOnce(
engine: BrainEngine,
flags: ParsedFlags,
diff --git a/test/recall-entity-keying-4828.test.ts b/test/recall-entity-keying-4828.test.ts
index af7eb5c65f..1528fdc558 100644
--- a/test/recall-entity-keying-4828.test.ts
+++ b/test/recall-entity-keying-4828.test.ts
@@ -108,6 +108,15 @@ describe('a name followed by topic words', () => {
expect((await recall(['alice enterprise pricing'])).facts).not.toContain('prefers morning meetings');
});
+ test('a leading name that is only a page alias still resolves', async () => {
+ await engine.putPage('people/robert-example', { title: 'Robert Example', type: 'person', compiled_truth: 'Advisor.' } as never);
+ await engine.setPageAliases('people/robert-example', 'default', ['bobby example']);
+ await fact('prefers tea at board meetings', 'people/robert-example', new Date('2026-03-03'));
+ const r = await recall(['Bobby Example board']);
+ expect(r.facts).toEqual(['prefers tea at board meetings']);
+ expect(r.err).toContain("'Bobby Example' is people/robert-example");
+ });
+
test('an explicit --entity is never split or widened to text', async () => {
const r = await recall(['--entity', 'alice example enterprise pricing']);
expect(r.facts).toEqual([]);