Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(enrich): GBRA-130 F8 reuse a long name stored at the untagged cut slug an older gbrain minted
garrytana98d964d2b44
diff --git a/docs/architecture/key-files/page-identity-and-administration.md b/docs/architecture/key-files/page-identity-and-administration.mdindex e9500d573b..a392079374 100644--- a/docs/architecture/key-files/page-identity-and-administration.md+++ b/docs/architecture/key-files/page-identity-and-administration.md@@ -81,4 +81,4 @@ upgrade consequence and engine parity respectively.- `src/core/persistence/suffixed-slug.ts` — `.md`/`.mdx`-suffixed slugs (#4807). `preparePageAdmission` refuses a new put_page/put_pages slug ending in `.md`/`.mdx` before any journal row (`invalid_params`, why, suggestion naming the bare slug, read-only `get_page` fix and verify); a live page with that exact (source_id, slug) still updates and the put_page response carries `slug_advisory` (put under the bare slug, then delete_page). `suffixedSlugDryRun` serves put_page dry runs; `suffixedFrontmatterSlugHold` holds a file import whose frontmatter `slug:` identity ends in `.md` as `frontmatter_slug_conflict`. The same admission refuses a NEW slug with a Windows reserved device segment or a segment over 240 bytes (`slug_not_portable`, suggestion from `portable-slug.ts`); a live page with that slug still updates and gets a `slug_advisory` naming the move. `:` stays valid slug grammar. `validateSlug` and path-derived import slugs are unchanged. Tests: `test/ingestion/put-page-write-through.test.ts`, `test/pathological/b5-export-unsafe-slugs.test.ts`, `test/pathological/b12-slug-segment-bytes.test.ts`.-- `src/core/portable-slug.ts` — the one slug portability rule (GBRA-130 B-5/B-12): `FILE_NAME_MAX_BYTES` (240, a name's byte budget; a leaf also reserves `.json`), `RESERVED_DEVICE_NAME` (con, prn, aux, nul, com1-9, lpt1-9, alone or before an extension), `truncateUtf8`, `portableSegment` (a reserved name gets `-<suffix>`, an over-budget segment is cut at a code point boundary) and `slugPortabilityProblem` (reason, first segment, suggested slug). Used by `persistence/suffixed-slug.ts` admission; `enrichment-service.ts` `slugifyEntity` uses its parts, so a reserved entity name mints `people/con-person` or `companies/com1-company` and an over-budget one ends in a hash of the full name. `enrichEntity` reuses a page at a slug another entity also maps to (`Aux Company` at `companies/aux-company`) only when its title names the entity, preferring the matching legacy page, else uses the hash-tagged slug or skips with `entity_slug_taken` (F8, `test/pathological/b5-enrich-entity-identity.test.ts`).+- `src/core/portable-slug.ts` — the one slug portability rule (GBRA-130 B-5/B-12): `FILE_NAME_MAX_BYTES` (240, a name's byte budget; a leaf also reserves `.json`), `RESERVED_DEVICE_NAME` (con, prn, aux, nul, com1-9, lpt1-9, alone or before an extension), `truncateUtf8`, `portableSegment` (a reserved name gets `-<suffix>`, an over-budget segment is cut at a code point boundary) and `slugPortabilityProblem` (reason, first segment, suggested slug). Used by `persistence/suffixed-slug.ts` admission; `enrichment-service.ts` `slugifyEntity` uses its parts, so a reserved entity name mints `people/con-person` or `companies/com1-company` and an over-budget one ends in a hash of the full name. `enrichEntity` reuses a page at a slug another entity also maps to (`Aux Company` at `companies/aux-company`) only when its title names the entity, preferring the matching legacy page (or, for a long name, the untagged cut slug an older gbrain minted, when its title maps to the same full slug), else uses the hash-tagged slug or skips with `entity_slug_taken` (F8, `test/pathological/b5-enrich-entity-identity.test.ts`).diff --git a/src/core/enrichment-service.ts b/src/core/enrichment-service.tsindex 6b323978fb..c5af18a70c 100644--- a/src/core/enrichment-service.ts+++ b/src/core/enrichment-service.ts@@ -20,7 +20,7 @@ import { waitForCapacity } from './backoff.ts';import { quarantineMarkers } from './extraction-review.ts';import { SLUG_WORD_CHARS, SLUG_VARIATION_SELECTORS_RE } from './cjk.ts';import { createHash } from 'node:crypto';-import { RESERVED_DEVICE_NAME, segmentByteBudget, truncateUtf8 } from './portable-slug.ts';+import { portableSegment, RESERVED_DEVICE_NAME, segmentByteBudget, truncateUtf8 } from './portable-slug.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@@ -223,6 +223,13 @@ export async function enrichEntity(const same = (t: string) => t.normalize('NFC').trim().toLowerCase();if (legacyPage && same(legacyPage.title ?? '') === same(request.entityName)) { slug = legacySlug; existingPage = legacyPage; legacyFound = true; }}+ // An over-budget name an older gbrain minted at its cut slug with no hash tag; that slug is shared by every name with the same prefix.+ const untagged = `${prefix}/${portableSegment(base, true, request.entityType)}`;+ if (!legacyFound && !existingPage && untagged !== candidateSlug) {+ const untaggedSlug = await engine.resolveSlugWithAlias(untagged, sourceId);+ const untaggedPage = await engine.getPage(untaggedSlug, scope);+ if (untaggedPage && namesEntity(untaggedPage.title)) { slug = untaggedSlug; existingPage = untaggedPage; legacyFound = true; }+ }// An escaped slug (Aux -> companies/aux-company) can be another entity's natural slug, and a natural slug can be// the escaped slug another entity was minted at (Prn Company's companies/prn-company held by Prn): either way the// page names another entity, so this one takes its hash-tagged slug. A page a person titled differently at thisdiff --git a/test/pathological/b5-enrich-entity-identity.test.ts b/test/pathological/b5-enrich-entity-identity.test.tsindex 1de4aaa140..21ac423f56 100644--- a/test/pathological/b5-enrich-entity-identity.test.ts+++ b/test/pathological/b5-enrich-entity-identity.test.ts@@ -10,7 +10,8 @@* enriching `Aux` wrote its timeline row and backlink to `Aux Company` and* never looked at the legacy `companies/aux` page that names it. The fix tags* a shortened name with a hash of the full name, checks that a page found at an- * escaped slug names this entity, prefers the matching legacy page, and gives+ * escaped slug names this entity, prefers the matching legacy page (also the+ * untagged cut slug an older gbrain minted for a long name), and gives* an entity whose slug another entity holds its own hash-tagged page.** Real PGLite, plus the Postgres arm when DATABASE_URL is set@@ -27,6 +28,10 @@ const page = (type: string, title: string) => `---\ntitle: ${JSON.stringify(titlconst PREFIX = 'acme-example-holdings-'.repeat(11);const LONG_ONE = `${PREFIX}North`;const LONG_TWO = `${PREFIX}South`;+const OLD_PREFIX = 'globex-example-'.repeat(17);+const OTHER_PREFIX = 'initech-example-'.repeat(16);+/** The slug the previous slugifyEntity minted for an over-budget name: cut to 235 bytes with no hash. */+const untagged = (name: string) => `companies/${name.toLowerCase().slice(0, 235).replace(/[-._:]+$/, '')}`;async function seed(engine: BrainEngine): Promise<void> {const pages: Array<[string, string, string]> = [@@ -36,6 +41,8 @@ async function seed(engine: BrainEngine): Promise<void> {['companies/lpt1-company', 'company', 'Lpt1 Company'],[`companies/lpt1-company-${createHash('sha256').update('lpt1').digest('hex').slice(0, 8)}`, 'company', 'Other Example'],['notes/source-example', 'note', 'Source Example'],+ [untagged(`${OLD_PREFIX}Alpha`), 'company', `${OLD_PREFIX}Alpha`],+ [untagged(`${OTHER_PREFIX}Beta`), 'company', `${OTHER_PREFIX}Beta`],];for (const [slug, type, title] of pages) await importFromContent(engine, slug, page(type, title), { noEmbed: true });}@@ -115,5 +122,24 @@ for (const { backend } of arms) {expect({ slug: again.slug, action: again.action }).toEqual({ slug: one.slug, action: 'updated' });expect(await counts(engine(), two.slug)).toEqual({ timeline: 1, links: 1 });});++ test('F8: a long name already stored at the untagged slug an older gbrain minted is updated there, with no tagged duplicate', async () => {+ const name = `${OLD_PREFIX}Alpha`;+ expect(untagged(name)).not.toBe(slugifyEntity(name, 'company'));+ const result = await enrich(name, 'company');+ expect({ slug: result.slug, action: result.action }).toEqual({ slug: untagged(name), action: 'updated' });+ expect(await counts(engine(), untagged(name))).toEqual({ timeline: 1, links: 1 });+ expect(await counts(engine(), slugifyEntity(name, 'company'))).toBeNull();+ });++ test('F8: an untagged slug holding the other long name with the same prefix is not reused', async () => {+ const name = `${OTHER_PREFIX}Gamma`;+ const holder = untagged(`${OTHER_PREFIX}Beta`);+ expect(untagged(name)).toBe(holder);+ const result = await enrich(name, 'company');+ expect({ slug: result.slug, action: result.action }).toEqual({ slug: slugifyEntity(name, 'company'), action: 'created' });+ expect(await title(engine(), result.slug)).toBe(name);+ expect(await counts(engine(), holder)).toEqual({ timeline: 0, links: 0 });+ });});}