Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(search): a private exact title no longer hides a readable alias (#6688 salvage)
time-attackaabb1706db75
diff --git a/docs/architecture/key-files/entity-recall.md b/docs/architecture/key-files/entity-recall.mdindex d749ad14d4..e34aabebf1 100644--- a/docs/architecture/key-files/entity-recall.md+++ b/docs/architecture/key-files/entity-recall.md@@ -24,4 +24,4 @@ card and `get_backlinks`. Guide: [entity recall](../../guides/entity-recall.md).- `src/core/search/entity-facts.ts` — `entitySavedFacts`: when a search names an entity, its newest active saved facts (`factEntitySlugs`, 5) lead the model-visible saved-facts block, then up to 3 facts each of up to 2 entities those facts name (`linked_from`), before the word-overlap matches. Same active, audit-row and visibility rules as recall. Tests: `test/memory-sibling-facts.test.ts`.- `src/core/schema-migrations/v206-entity-mention-index.ts` — additive: `page_aliases.origin` (`frontmatter` default, `declared`, `subject`), `case_sensitive`, `alias_text`, unique key `(source_id, alias_norm, slug, origin)`; tables `page_mention_state`, `mention_gazetteer_entries`, `mention_index_status` (RLS on when the role bypasses RLS). `GUARDED_TABLE_COLUMNS` lists the new `page_aliases` columns.-Alias readers apply precedence frontmatter > declared > subject per source, and a derived alias never answers for another live page's exact title (`readAliases` in `src/core/search/read-enrichment.ts`, shared by both engines' `resolveAliases`), so `resolveEntitySlug`, the retrieval reflex, the alias hop, intent weights and `search-diagnose` rank an exact title first. `buildGazetteer` (`src/core/by-mention.ts`) applies the same precedence, drops an alias claimed by two pages at its best origin (unless the claimants are identity siblings, which all keep it), yields to a same-source title, drops a single-word alias equal to the first word of another multi-word entity name, applies the generic-word list to every single-word name, and carries `caseTokens` for case-sensitive entries; `opts.dropped` records every dropped name with its reason.+Alias readers apply precedence frontmatter > declared > subject per source, and a derived alias never answers for another live page's exact title that the caller can read (the title competitor gets the caller's `pageReadFilter`, so a private title a private-excluding caller can't see never hides a readable alias) (`readAliases` in `src/core/search/read-enrichment.ts`, shared by both engines' `resolveAliases`), so `resolveEntitySlug`, the retrieval reflex, the alias hop, intent weights and `search-diagnose` rank an exact title first. `buildGazetteer` (`src/core/by-mention.ts`) applies the same precedence, drops an alias claimed by two pages at its best origin (unless the claimants are identity siblings, which all keep it), yields to a same-source title, drops a single-word alias equal to the first word of another multi-word entity name, applies the generic-word list to every single-word name, and carries `caseTokens` for case-sensitive entries; `opts.dropped` records every dropped name with its reason.diff --git a/src/core/search/read-enrichment.ts b/src/core/search/read-enrichment.tsindex a18a1e614c..8372c53cd2 100644--- a/src/core/search/read-enrichment.ts+++ b/src/core/search/read-enrichment.ts@@ -346,13 +346,16 @@ export async function readAliases(query: ReadQuery, aliases: string[], scope?: Pconst params: unknown[] = [aliases];const aliasScope = pageReadFilter('m', scope && { sourceId: scope.sourceId, sourceIds: scope.sourceIds }, params);const filter = pageReadFilter('p', scope, params, true);+ const titleFilter = pageReadFilter('t', scope, params, true);// Alias matches first, then one unique-key page lookup each (OFFSET 0 keeps the// read filter out of that lookup): without planner statistics (PGLite has no// autovacuum) the page_aliases -> sources foreign key otherwise makes the planner// walk every readable page and probe aliases per page.// Precedence: within a source, only the claims at the best origin answer// (frontmatter > declared > subject), and a derived (declared or subject)- // alias never answers for a name that is another live page's exact title,+ // alias never answers for a name that is another live page's exact title+ // the caller can read (a private title a remote caller can't see must not+ // hide a readable alias),// so "Acme Example" stays the company page beside "CRM record: Acme Example".const rows = await query<PageRef & { alias_norm: string; rank: number }>(`WITH a AS MATERIALIZED (@@ -364,7 +367,7 @@ export async function readAliases(query: ReadQuery, aliases: string[], scope?: PSELECT a.alias_norm, a.slug, a.source_id, a.rank FROM aCROSS JOIN LATERAL (SELECT * FROM pages WHERE source_id = a.source_id AND slug = a.slug OFFSET 0) pWHERE ${filter}- AND (a.rank = 0 OR NOT EXISTS (SELECT 1 FROM pages t WHERE t.source_id = a.source_id AND t.deleted_at IS NULL+ AND (a.rank = 0 OR NOT EXISTS (SELECT 1 FROM pages t WHERE t.source_id = a.source_id AND ${titleFilter}AND lower(t.title) = a.alias_norm AND t.slug <> a.slug))ORDER BY a.alias_norm, a.source_id, a.rank, a.slug`, params);const best = new Map<string, number>();diff --git a/test/read-aliases-title-scope.test.ts b/test/read-aliases-title-scope.test.tsnew file mode 100644index 0000000000..ad7855b750--- /dev/null+++ b/test/read-aliases-title-scope.test.ts@@ -0,0 +1,39 @@+/**+ * PR #6688 salvage: a derived alias yields to another live page's exact+ * title only when the caller can read that page. A private (or other-source)+ * title the caller cannot see must not hide a readable alias.+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { importFromContent } from '../src/core/import-file.ts';++let engine: PGLiteEngine;++beforeAll(async () => {+ engine = new PGLiteEngine();+ await engine.connect({});+ await engine.initSchema();+ await importFromContent(engine, 'projects/title-alias-example',+ '---\ntitle: Title alias example\ntype: project\naliases: [titleprivacycode]\n---\n\n# Synthetic entity\n', { noEmbed: true });+ await engine.executeRaw("UPDATE page_aliases SET origin='declared' WHERE slug='projects/title-alias-example' AND alias_norm='titleprivacycode'");+ await importFromContent(engine, 'projects/exact-private-title-example',+ '---\ntitle: titleprivacycode\ntype: project\nvisibility: private\n---\n\n# Synthetic private title\n', { noEmbed: true });+}, 60_000);+afterAll(async () => { await engine.disconnect(); });++describe('readAliases title competitor honors the read scope (#6688)', () => {+ test('a private exact title the caller cannot read does not hide a readable derived alias', async () => {+ expect((await engine.resolveAliases(['titleprivacycode'], { sourceId: 'default', excludePrivate: true })).get('titleprivacycode'))+ .toEqual([{ slug: 'projects/title-alias-example', source_id: 'default' }]);+ });++ test('a caller who can read the private title still sees the title win', async () => {+ expect((await engine.resolveAliases(['titleprivacycode'], { sourceId: 'default' })).has('titleprivacycode')).toBe(false);+ });++ test('a visible exact title still wins for a private-excluding caller', async () => {+ await importFromContent(engine, 'projects/exact-visible-title-example',+ '---\ntitle: titleprivacycode\ntype: project\n---\n\n# Synthetic visible title\n', { noEmbed: true });+ expect((await engine.resolveAliases(['titleprivacycode'], { sourceId: 'default', excludePrivate: true })).has('titleprivacycode')).toBe(false);+ });+});