Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(enrichment): reuse an entity page created under the legacy ASCII slug

masashiono061123bd185c88c7
diff --git a/src/core/enrichment-service.ts b/src/core/enrichment-service.ts
index 49cf23936c..251b418126 100644
--- a/src/core/enrichment-service.ts
+++ b/src/core/enrichment-service.ts
@@ -110,6 +110,22 @@ export function slugifyEntity(name: string, type: 'person' | 'company'): string
return `${prefix}/${slug}`;
}
+/**
+ * The slug the pre-Unicode slugifier produced for the same name (ASCII only:
+ * every non-[a-z0-9] run became '-'). Kept so enrichment can find entity pages
+ * that existing brains created before slugifyEntity learned other scripts,
+ * instead of minting a second page for the same entity.
+ */
+export function legacyAsciiEntitySlug(name: string, type: 'person' | 'company'): string {
+ const slug = name
+ .toLowerCase()
+ .replace(/['']/g, '')
+ .replace(/[^a-z0-9]+/g, '-')
+ .replace(/^-+|-+$/g, '');
+ const prefix = type === 'person' ? 'people' : 'companies';
+ return `${prefix}/${slug}`;
+}
+
/** Get the brain page path for an entity. */
export function entityPagePath(name: string, type: 'person' | 'company'): string {
return slugifyEntity(name, type);
@@ -130,7 +146,7 @@ export async function enrichEntity(
): Promise<EnrichmentResult> {
const candidateSlug = slugifyEntity(request.entityName, request.entityType);
const sourceId = opts?.sourceId ?? 'default';
- const slug = await engine.resolveSlugWithAlias(candidateSlug, sourceId);
+ let slug = await engine.resolveSlugWithAlias(candidateSlug, sourceId);
// Fail-closed: only an explicit `trusted: true` writes authoritative pages.
const trusted = opts?.trusted === true;
const scope = opts?.sourceId ? { sourceId: opts.sourceId } : undefined;
@@ -144,7 +160,16 @@ export async function enrichEntity(
const tierEscalated = suggestedTier < (request.tier || 3); // lower tier number = higher importance
// 3. Check if entity page exists
- const existingPage = await engine.getPage(slug, scope);
+ let existingPage = await engine.getPage(slug, scope);
+ if (!existingPage) {
+ // A page an older brain created under the ASCII-only slug is the same entity.
+ const legacy = legacyAsciiEntitySlug(request.entityName, request.entityType);
+ if (legacy !== candidateSlug && !/^(people|companies)\/$/.test(legacy)) {
+ const legacySlug = await engine.resolveSlugWithAlias(legacy, sourceId);
+ const legacyPage = await engine.getPage(legacySlug, scope);
+ if (legacyPage) { slug = legacySlug; existingPage = legacyPage; }
+ }
+ }
let action: 'created' | 'updated' | 'skipped';
if (existingPage) {
diff --git a/test/enrichment-legacy-slug.test.ts b/test/enrichment-legacy-slug.test.ts
new file mode 100644
index 0000000000..ec061ca4d5
--- /dev/null
+++ b/test/enrichment-legacy-slug.test.ts
@@ -0,0 +1,53 @@
+import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { enrichEntity, legacyAsciiEntitySlug, slugifyEntity } from '../src/core/enrichment-service.ts';
+
+let engine: PGLiteEngine;
+
+beforeAll(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({});
+ await engine.initSchema();
+}, 120_000);
+
+afterAll(async () => {
+ if (engine) await engine.disconnect();
+}, 60_000);
+
+beforeEach(async () => {
+ for (const t of ['content_chunks', 'links', 'tags', 'timeline_entries', 'page_versions', 'ingest_log', 'pages']) {
+ await (engine as unknown as { db: { exec(q: string): Promise<unknown> } }).db.exec(`DELETE FROM ${t}`);
+ }
+});
+
+async function personSlugs(): Promise<string[]> {
+ const rows = await engine.executeRaw<{ slug: string }>("SELECT slug FROM pages WHERE slug LIKE 'people/%' AND deleted_at IS NULL ORDER BY slug");
+ return rows.map(r => r.slug);
+}
+
+describe('entity enrichment keeps pages created under the ASCII-only slug', () => {
+ test('the Unicode-aware slug differs from the legacy ASCII slug for accented names', () => {
+ expect(slugifyEntity('José García', 'person')).toBe('people/jose-garcia');
+ expect(legacyAsciiEntitySlug('José García', 'person')).toBe('people/jos-garc-a');
+ });
+
+ test('an existing legacy-slug page is updated instead of minting a duplicate', async () => {
+ await engine.putPage('people/jos-garc-a', { type: 'person', title: 'José García', compiled_truth: 'Existing page from an older brain.', frontmatter: {} });
+ const result = await enrichEntity(engine, { entityName: 'José García', entityType: 'person', context: 'met at the conference', sourceSlug: 'notes/day1' }, { trusted: true });
+ expect(result.action).toBe('updated');
+ expect(result.slug).toBe('people/jos-garc-a');
+ expect(await personSlugs()).toEqual(['people/jos-garc-a']);
+ });
+
+ test('with no legacy page, a new page is created under the Unicode-aware slug', async () => {
+ const result = await enrichEntity(engine, { entityName: 'José García', entityType: 'person', context: 'first mention', sourceSlug: 'notes/day1' }, { trusted: true });
+ expect(result.slug).toBe('people/jose-garcia');
+ expect(await personSlugs()).toEqual(['people/jose-garcia']);
+ });
+
+ test('a name whose legacy slug is empty never matches a bare prefix page', async () => {
+ expect(legacyAsciiEntitySlug('Иван Петров', 'person')).toBe('people/');
+ const result = await enrichEntity(engine, { entityName: 'Иван Петров', entityType: 'person', context: 'c', sourceSlug: 'notes/day1' }, { trusted: true });
+ expect(result.slug).toBe(slugifyEntity('Иван Петров', 'person'));
+ });
+});