Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(entities): accept Unicode letters in looksLikeSlug so non-Latin slugs take the exact-match path (#5422)
time-attacke89a36f9c636
diff --git a/src/core/entities/resolve.ts b/src/core/entities/resolve.tsindex 7e2cec5e2b..e6809bef99 100644--- a/src/core/entities/resolve.ts+++ b/src/core/entities/resolve.ts@@ -618,11 +618,13 @@ async function tryPrefixExpansion(}export function looksLikeSlug(s: string): boolean {- // Slug shape: lowercase letters/digits with at least one slash OR matches- // [a-z0-9-]+ exactly. Anything with whitespace or capital letters fails.+ // Slug shape: lowercase Unicode letters/numbers plus the path and slug+ // separators. Slugs are not ASCII-only: a non-Latin entity page such as+ // `people/<hangul-name>` must take the exact-match path before fuzzy+ // matching. Anything with whitespace or uppercase letters still fails.if (/\s/.test(s)) return false;if (s !== s.toLowerCase()) return false;- return /^[a-z0-9/_-]+$/.test(s);+ return /^[\p{L}\p{N}/_-]+$/u.test(s);}async function tryExactSlug(diff --git a/test/entity-resolve.test.ts b/test/entity-resolve.test.tsindex 1b61971d22..6cf1f63b33 100644--- a/test/entity-resolve.test.ts+++ b/test/entity-resolve.test.ts@@ -2,6 +2,7 @@ import { describe, it, expect, beforeAll, afterAll } from 'bun:test';import {resolveEntitySlug,resolveEntitySlugWithSource,+ looksLikeSlug,slugify,type ResolutionSource,} from '../src/core/entities/resolve.ts';@@ -39,6 +40,8 @@ beforeAll(async () => {{ slug: 'people/charlie-example', title: 'Charlie Example', type: 'person' },{ slug: 'people/charlie-bankcroft', title: 'Charlie Bankcroft', type: 'person' },{ slug: 'people/dave-example', title: 'Dave Example', type: 'person' },+ { slug: 'people/한글-예시', title: '한글 예시', type: 'person' },+ { slug: '한글-개념', title: '한글 개념', type: 'concept' },{ slug: 'companies/stripe', title: 'Stripe', type: 'company' },{ slug: 'companies/stripe-atlas', title: 'Stripe Atlas', type: 'company' },{ slug: 'companies/benton-capital', title: 'Benton Capital', type: 'company' },@@ -139,6 +142,11 @@ describe('resolveEntitySlug — prefix expansion', () => {expect(result).toBe('people/alice-example');});+ it('exact match works for fully-qualified Unicode slugs', async () => {+ const result = await resolveEntitySlug(engine as unknown as BrainEngine, 'default', 'people/한글-예시');+ expect(result).toBe('people/한글-예시');+ });+it('multi-word input does NOT trigger prefix expansion', async () => {// "Alice Example" should go through fuzzy match, not prefix expansionconst result = await resolveEntitySlug(engine as unknown as BrainEngine, 'default', 'Alice Example');@@ -469,3 +477,41 @@ describe('alias_exact — liveness before uniqueness (v0.46.15 codex ship-reviewexpect(r!.source).not.toBe<ResolutionSource>('alias_exact');});});++describe('Unicode slug shape (#5421)', () => {+ it('a non-Latin slug without a slash takes the exact-match path', async () => {+ expect(await resolveEntitySlugWithSource(engine as unknown as BrainEngine, 'default', '한글-개념'))+ .toEqual({ slug: '한글-개념', source: 'exact_page' });+ });++ it('a fully-qualified non-Latin slug takes the exact-match path', async () => {+ expect(await resolveEntitySlugWithSource(engine as unknown as BrainEngine, 'default', 'people/한글-예시'))+ .toEqual({ slug: 'people/한글-예시', source: 'exact_page' });+ });++ it('accepts Unicode letters and numbers but still rejects uppercase, whitespace and punctuation', () => {+ expect(looksLikeSlug('people/한글-예시')).toBe(true);+ expect(looksLikeSlug('한글_예시2')).toBe(true);+ expect(looksLikeSlug('people/alice-example')).toBe(true);+ expect(looksLikeSlug('People/Alice-Example')).toBe(false);+ expect(looksLikeSlug('people/Alice-example')).toBe(false);+ expect(looksLikeSlug('people/한글 예시')).toBe(false);+ expect(looksLikeSlug('people/alice.example')).toBe(false);+ });++ it('mixed-case Latin input never takes the exact-match path', async () => {+ const r = await resolveEntitySlugWithSource(engine as unknown as BrainEngine, 'default', 'People/Alice-Example');+ expect(r!.source).not.toBe<ResolutionSource>('exact_page');+ });++ it('a non-Latin slug that exists only in another source never wins', async () => {+ await engine.executeRaw(`INSERT INTO sources (id, name) VALUES ('unicode-other', 'Unicode other')`);+ await engine.putPage('people/다른-예시', {+ type: 'person', title: '다른 예시', compiled_truth: 'Other source', frontmatter: {},+ }, { sourceId: 'unicode-other' });+ const r = await resolveEntitySlugWithSource(engine as unknown as BrainEngine, 'default', 'people/다른-예시');+ expect(r!.source).not.toBe<ResolutionSource>('exact_page');+ expect(await resolveEntitySlugWithSource(engine as unknown as BrainEngine, 'unicode-other', 'people/다른-예시'))+ .toEqual({ slug: 'people/다른-예시', source: 'exact_page' });+ });+});