Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: make slugifyEntity Unicode-aware instead of ASCII-only

masashiono0611b129ec74863c
diff --git a/src/core/enrichment-service.ts b/src/core/enrichment-service.ts
index 2651bc5394..0aa869edc7 100644
--- a/src/core/enrichment-service.ts
+++ b/src/core/enrichment-service.ts
@@ -16,6 +16,7 @@
import type { BrainEngine } from './engine.ts';
import { waitForCapacity } from './backoff.ts';
import { quarantineMarkers } from './extraction-review.ts';
+import { SLUG_WORD_CHARS, SLUG_VARIATION_SELECTORS_RE } from './cjk.ts';
// #3994: created stubs route through serializeMarkdown + importFromContent
// (the same parse→chunk→embed pipeline put_page uses) instead of a bare
// engine.putPage, so fresh entity pages land in the retrieval surface
@@ -78,12 +79,31 @@ export interface EnrichmentResult {
// Entity naming utilities
// ---------------------------------------------------------------------------
-/** Convert an entity name to a URL-safe slug. */
+// Keep-set mirrors sync.ts:slugifySegment (single grammar, see cjk.ts
+// docstring) so an entity minted here and a file synced under brain/people/
+// never diverge on what counts as a slug character.
+const SLUGIFY_ENTITY_KEEP_RE = new RegExp(`[^${SLUG_WORD_CHARS}]`, 'gu');
+
+/**
+ * Convert an entity name to a URL-safe slug.
+ *
+ * Was ASCII-only ([a-z0-9]), which silently dropped every non-Latin
+ * character — Cyrillic/CJK/Arabic/etc. names slugified to '' or a bare
+ * '-', producing empty or colliding people/companies slugs. Now mirrors
+ * sync.ts:slugifySegment's Unicode-aware keep-set: strip Latin accents to
+ * their base letter (fold, don't drop), keep every script's own letters
+ * as-is (Cyrillic, CJK, Devanagari, …).
+ */
export function slugifyEntity(name: string, type: 'person' | 'company'): string {
const slug = name
+ .replace(/['‘’]/g, '')
+ .normalize('NFD')
+ .replace(/[̀-ͯ]/g, '')
+ .normalize('NFC')
+ .replace(SLUG_VARIATION_SELECTORS_RE, '')
.toLowerCase()
- .replace(/['']/g, '')
- .replace(/[^a-z0-9]+/g, '-')
+ .replace(SLUGIFY_ENTITY_KEEP_RE, '-')
+ .replace(/-+/g, '-')
.replace(/^-+|-+$/g, '');
const prefix = type === 'person' ? 'people' : 'companies';
diff --git a/test/enrichment-service.test.ts b/test/enrichment-service.test.ts
index d53e2f97ff..a39dcd8dda 100644
--- a/test/enrichment-service.test.ts
+++ b/test/enrichment-service.test.ts
@@ -15,8 +15,16 @@ describe('enrichment-service', () => {
expect(slugifyEntity("O'Brien", 'person')).toBe('people/obrien');
});
- test('handles special characters', () => {
- expect(slugifyEntity('José García', 'person')).toBe('people/jos-garc-a');
+ test('folds Latin accents to their base letter', () => {
+ expect(slugifyEntity('José García', 'person')).toBe('people/jose-garcia');
+ });
+
+ test('preserves non-Latin scripts instead of dropping them', () => {
+ // Was ASCII-only ([a-z0-9]): Cyrillic names slugified to '' or a bare
+ // '-', so every non-Latin person/company collided on the same empty
+ // slug. Assert parsed structure (exact slug), not just non-emptiness.
+ expect(slugifyEntity('Иван Петров', 'person')).toBe('people/иван-петров');
+ expect(slugifyEntity('Тестовая Компания', 'company')).toBe('companies/тестовая-компания');
});
test('trims leading/trailing hyphens', () => {