Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(facts): conversation entity hints skip retired alias pages and hint their canonical page (#6898)
garrytan3d4aad73f234
diff --git a/src/core/entities/resolve.ts b/src/core/entities/resolve.tsindex 640be17a52..910d9d1bd4 100644--- a/src/core/entities/resolve.ts+++ b/src/core/entities/resolve.ts@@ -200,7 +200,7 @@ function redirectSlug(value: string): string | null {* so it is followed whatever its type; the strict resolver still applies its* fact-entity check to the page it lands on.*/-async function followRetirement(+export async function followRetirement(engine: BrainEngine,source_id: string,slug: string,diff --git a/src/core/facts/conversation-entity-hints.ts b/src/core/facts/conversation-entity-hints.tsindex 5580218236..292f036613 100644--- a/src/core/facts/conversation-entity-hints.ts+++ b/src/core/facts/conversation-entity-hints.ts@@ -16,10 +16,15 @@* there) and name live fact-entity pages only. Hints are slugs, and a slug* carries the entity's name, so a private entity is hinted only when the* run's facts are private too.+ *+ * #6898: a linked page the resolver treats as retired (retiredPageSql) is+ * never hinted; the live page its `superseded_by` / `merged_into` redirect+ * names takes its place (followRetirement), and is checked like any other+ * hint. A retired page whose redirect cannot be followed drops out.*/import type { BrainEngine } from '../engine.ts';import type { FactVisibility } from './visibility.ts';-import { isFactEntityPage } from '../entities/resolve.ts';+import { followRetirement, isFactEntityPage, retiredPageSql } from '../entities/resolve.ts';import { isPrivatePage } from '../search/private-visibility.ts';/** Hints a conversation page forwards: a meeting's attendees plus what it links run to a few dozen (extract_facts keeps ENTITY_HINTS_CAP). */@@ -43,13 +48,23 @@ export async function conversationEntityHints(engine: BrainEngine, sourceId: str}ordered.delete(pageSlug);if (ordered.size === 0) return [];- const rows = await engine.executeRaw<{ slug: string; type: string | null; frontmatter: unknown }>(- 'SELECT slug, type, frontmatter FROM pages WHERE source_id = $1 AND slug = ANY($2::text[]) AND deleted_at IS NULL',- [sourceId, [...ordered]],+ const read = (slugs: string[]) => engine.executeRaw<{ slug: string; type: string | null; frontmatter: unknown; retired: boolean }>(+ `SELECT p.slug, p.type, p.frontmatter, ${retiredPageSql('p')} AS retired FROM pages p+ WHERE p.source_id = $1 AND p.slug = ANY($2::text[]) AND p.deleted_at IS NULL`,+ [sourceId, slugs],);- const pages = new Map(rows.map(row => [row.slug, row]));- return [...ordered].filter(slug => {- const page = pages.get(slug);- return !!page && isFactEntityPage(slug, page.type) && (factVisibility === 'private' || !isPrivatePage(page));- }).slice(0, CONVERSATION_ENTITY_HINTS_CAP);+ const pages = new Map((await read([...ordered])).map(row => [row.slug, row]));+ const canonical = new Map<string, string | null>();+ for (const page of pages.values()) {+ if (page.retired) canonical.set(page.slug, (await followRetirement(engine, sourceId, page.slug))?.slug ?? null);+ }+ const targets = [...new Set(canonical.values())].filter((slug): slug is string => slug !== null && !pages.has(slug));+ if (targets.length) for (const row of await read(targets)) pages.set(row.slug, row);+ const hints = new Set<string>();+ for (const linked of ordered) {+ const slug = canonical.has(linked) ? canonical.get(linked) : linked;+ const page = slug ? pages.get(slug) : undefined;+ if (page && !page.retired && isFactEntityPage(page.slug, page.type) && (factVisibility === 'private' || !isPrivatePage(page))) hints.add(page.slug);+ }+ return [...hints].slice(0, CONVERSATION_ENTITY_HINTS_CAP);}diff --git a/test/conversation-entity-hints-retired-6898.test.ts b/test/conversation-entity-hints-retired-6898.test.tsnew file mode 100644index 0000000000..721e5eb878--- /dev/null+++ b/test/conversation-entity-hints-retired-6898.test.ts@@ -0,0 +1,66 @@+/**+ * #6898 follow-up (wave 19 L3, item 5): the conversation entity hints never+ * name a retired alias page.+ *+ * Protects: a hinted page the #6898 resolver treats as retired+ * (retiredPageSql: `canonical: false`, a `superseded_by`/`merged_into`+ * target, or an alias/redirect `status`) is replaced by the live page its+ * redirect names, in its place and once; a retired page whose redirect+ * cannot be followed drops out; the canonical page's own type and privacy+ * decide whether it is hinted.+ * Regression: the hint query read only `deleted_at` and the page type, so a+ * meeting that linked the retired copy `root/people/alice-example` (type+ * concept, title "People Alice Example") sent that slug to the extractor,+ * and the extractor named the retired page instead of `people/alice-example`.+ */+import { afterAll, beforeAll, beforeEach, expect, test } from 'bun:test';+import { conversationEntityHints } from '../src/core/facts/conversation-entity-hints.ts';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { resetPgliteState } from './helpers/reset-pglite.ts';++const MEETING = 'meetings/2026-08-12-alice-sync';+let engine: PGLiteEngine;+beforeAll(async () => {+ engine = new PGLiteEngine();+ await engine.connect({});+ await engine.initSchema();+}, 60_000);+afterAll(async () => { await engine.disconnect(); });++const RETIRED = { status: 'historical-retired-alias', canonical: false, superseded_by: 'people/alice-example' };++beforeEach(async () => {+ await resetPgliteState(engine);+ const page = (slug: string, type: string, title: string, frontmatter: Record<string, unknown> = {}) =>+ engine.putPage(slug, { type: type as never, title, compiled_truth: `# ${title}`, timeline: '', frontmatter });+ await page(MEETING, 'meeting', 'Alice sync', { date: '2026-08-12' });+ await page('people/alice-example', 'person', 'Aliceexample', { canonical: true, aliases: ['alice-example', 'Alice'] });+ await page('root/people/alice-example', 'concept', 'People Alice Example', RETIRED);+ await page('people/beta-example', 'person', 'Beta Example');+ await page('root/people/beta-example', 'concept', 'People Beta Example', { status: 'redirect' });+ await page('people/secret-example', 'person', 'Secret Example', { visibility: 'private' });+ await page('root/people/secret-example', 'concept', 'People Secret Example', { canonical: false, superseded_by: '[[people/secret-example|Secret]]' });+});++test('a linked retired alias page hints its canonical page instead', async () => {+ await engine.addLink(MEETING, 'root/people/alice-example', '', 'mentions', 'wikilink-resolved');+ await engine.addLink(MEETING, 'people/beta-example', '', 'mentions', 'markdown');+ expect(await conversationEntityHints(engine, 'default', MEETING, 'private')).toEqual(['people/alice-example', 'people/beta-example']);+});++test('the canonical page is hinted once when the meeting links it and its retired copy', async () => {+ await engine.addLink('people/alice-example', MEETING, '', 'attended', 'frontmatter', MEETING, 'attendees');+ await engine.addLink(MEETING, 'root/people/alice-example', '', 'mentions', 'markdown');+ expect(await conversationEntityHints(engine, 'default', MEETING, 'private')).toEqual(['people/alice-example']);+});++test('a retired page without a followable target drops out', async () => {+ await engine.addLink(MEETING, 'root/people/beta-example', '', 'mentions', 'markdown');+ expect(await conversationEntityHints(engine, 'default', MEETING, 'private')).toEqual([]);+});++test('the canonical page keeps its own privacy: a private target is hinted into private facts only', async () => {+ await engine.addLink(MEETING, 'root/people/secret-example', '', 'mentions', 'markdown');+ expect(await conversationEntityHints(engine, 'default', MEETING, 'private')).toEqual(['people/secret-example']);+ expect(await conversationEntityHints(engine, 'default', MEETING, 'world')).toEqual([]);+});