Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(entities): B-7 an organization reference reaches its page under another organization directory (#6925)

garrytan5d7b2e4f418d
diff --git a/src/core/entities/resolve.ts b/src/core/entities/resolve.ts
index 910d9d1bd4..b36e9aa2ec 100644
--- a/src/core/entities/resolve.ts
+++ b/src/core/entities/resolve.ts
@@ -40,7 +40,10 @@ import { rankingLinkSql } from '../link-ranking-policy.ts';
* 1. If `raw` is already a page slug shape (contains a "/" or matches an
* exact pages.slug row in this source), return it untouched. A mention
* that is exactly one live page's own name (slug basename) resolves to
- * it next, before any other page's alias.
+ * it next, before any other page's alias, and so does (#6925) a
+ * slug-shaped organization reference's one live page under another
+ * organization directory (`companies/x` → `orgs/x`). Then an
+ * unambiguous alias.
* 2. Resolve a bare name only when prefix expansion finds one candidate,
* or one candidate whose title is exactly that name.
* 3. For multi-token input, take a fuzzy candidate within the source only
@@ -80,6 +83,9 @@ export async function resolveEntitySlug(
// page_aliases hit resolves BEFORE prefix expansion / fuzzy — the
// alias table is curated ground truth ("saoirse" → people/saoirse-x)
// while fuzzy is a guess. Live-page verified (page_aliases has no FK).
+ const orgVariant = await tryOrgDirVariant(engine, source_id, trimmed);
+ if (orgVariant) return orgVariant;
+
const aliased = await tryAliasExact(engine, source_id, trimmed);
if (aliased) return aliased;
@@ -131,6 +137,31 @@ function fallbackSegment(segment: string): string {
const IDENTITY_TYPES = new Set(['person', 'company', 'fund', 'organization']);
const IDENTITY_DIRS = ['people/', 'companies/', 'funds/', 'orgs/', 'organizations/'];
+/** #6925: directories that hold the same organization pages, so `companies/x`, `orgs/x` and `organizations/x` name one org. */
+const ORG_DIRS = ['companies', 'orgs', 'organizations'];
+
+function sameEntityDir(a: string | null, b: string | null): boolean {
+ return a === b || (a !== null && b !== null && ORG_DIRS.includes(a) && ORG_DIRS.includes(b));
+}
+
+/** #6925: the same slug under the other organization directories; empty for any other slug. */
+function orgDirVariants(raw: string): string[] {
+ const slug = fallbackSlugify(raw.trim());
+ const [dir, name, ...rest] = slug?.split('/') ?? [];
+ if (!name || rest.length || !ORG_DIRS.includes(dir)) return [];
+ return ORG_DIRS.filter(d => d !== dir).map(d => `${d}/${name}`);
+}
+
+/** #6925: the one live page a slug-shaped organization reference names under another organization directory. */
+async function tryOrgDirVariant(engine: BrainEngine, source_id: string, raw: string): Promise<string | null> {
+ const variants = orgDirVariants(raw);
+ if (!variants.length) return null;
+ const rows = await engine.executeRaw<{ slug: string }>(
+ 'SELECT slug FROM pages WHERE source_id = $1 AND deleted_at IS NULL AND slug = ANY($2::text[])', [source_id, variants]);
+ const live = await canonicalPages(engine, source_id, rows.map(r => r.slug));
+ return live.length === 1 ? live[0].slug : null;
+}
+
const NAME_NOISE_TOKENS = new Set(['the', 'inc', 'llc', 'ltd', 'co', 'corp', 'corporation', 'plc', 'gmbh']);
/** True for pages that name one real-world person, company, fund or organization. */
@@ -259,7 +290,9 @@ function aliasSpellings(raw: string): { literal: string; folded: string[]; dir:
* Slugs of the pages whose page_aliases row carries the reference: the
* literal spelling's hits when it has any, else the separator-folded
* spellings'. A folded hit for a slug-shaped reference must sit in the same
- * directory (`projects/acme-example` never lands on a `companies/` alias).
+ * directory (`projects/acme-example` never lands on a `companies/` alias);
+ * the organization directories count as one (#6925: `companies/acme-example`
+ * reaches an `orgs/` page's `acme example` alias).
* Throws what resolveAliases throws.
*/
async function aliasHitSlugs(engine: BrainEngine, source_id: string, raw: string): Promise<string[]> {
@@ -269,7 +302,7 @@ async function aliasHitSlugs(engine: BrainEngine, source_id: string, raw: string
const exact = (hits.get(literal) ?? []).map(h => h.slug);
if (exact.length) return exact;
const parentDir = (slug: string) => slug.split('/').slice(-2, -1)[0] ?? null;
- return folded.flatMap(s => hits.get(s) ?? []).map(h => h.slug).filter(slug => dir === null || parentDir(slug) === dir);
+ return folded.flatMap(s => hits.get(s) ?? []).map(h => h.slug).filter(slug => dir === null || sameEntityDir(parentDir(slug), dir));
}
/**
@@ -394,6 +427,9 @@ export async function resolveEntitySlugWithSource(
const basenames = await findExactBasenameCandidates(engine, source_id, trimmed);
if (basenames.length === 1) return { slug: basenames[0].slug, source: 'fuzzy_match' };
+ const orgVariant = await tryOrgDirVariant(engine, source_id, trimmed);
+ if (orgVariant) return { slug: orgVariant, source: 'fuzzy_match' };
+
const aliased = await tryAliasExact(engine, source_id, trimmed);
if (aliased) return { slug: aliased, source: 'alias_exact' };
@@ -486,6 +522,8 @@ export async function resolveStrictEntityReference(
const basename = await pick(await live([...PREFIX_EXPANSION_DIRS, 'concepts'].map(dir => `${dir}/${token}`)), 'basename');
if (basename) return basename;
}
+ const orgVariant = await pick(await live(orgDirVariants(trimmed)), 'basename');
+ if (orgVariant) return orgVariant;
try {
const aliased = await pick(await live(await aliasHitSlugs(engine, source_id, trimmed)), 'alias_exact');
if (aliased) return aliased;