Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(import): normalize mixed-case slugs before chunk upsert (#430) (#3143)
Time Attakcb5675437c0b3
diff --git a/src/core/import-file.ts b/src/core/import-file.tsindex 55dd7bd8bd..e44d7ed3e4 100644--- a/src/core/import-file.ts+++ b/src/core/import-file.ts@@ -36,7 +36,7 @@ import {} from './embedding-context.ts';import { loadSearchModeConfig, resolveSearchMode } from './search/mode.ts';import { normalizeAliasList } from './search/alias-normalize.ts';-import { isUndefinedTableError, warnOncePerProcess } from './utils.ts';+import { isUndefinedTableError, warnOncePerProcess, validateSlug } from './utils.ts';import { computeCorpusGeneration } from './contextual-retrieval-service.ts';import { runGuardrails } from './guardrails.ts';@@ -295,6 +295,12 @@ export async function importFromContent(remote?: boolean;} = {},): Promise<ImportResult> {+ // Normalize BEFORE any tx write: putPage lowercases via validateSlug but+ // upsertChunks used to query by the caller's raw slug, so a mixed-case slug+ // created the page row then failed the chunk upsert with "Page not found",+ // rolling back the whole import (#430).+ slug = validateSlug(slug);+// v0.18.0+ multi-source: when caller is syncing under a non-default source,// every per-page tx call must carry `sourceId` so writes target the right// (source_id, slug) row. Pre-fix, putPage relied on the schema DEFAULT anddiff --git a/src/core/pglite-engine.ts b/src/core/pglite-engine.tsindex 84fd8fe03f..5ca8c0f242 100644--- a/src/core/pglite-engine.ts+++ b/src/core/pglite-engine.ts@@ -2269,6 +2269,9 @@ export class PGLiteEngine implements BrainEngine {}private async _upsertChunksOnce(slug: string, chunks: ChunkInput[], opts?: { sourceId?: string }): Promise<void> {+ // Normalize the same way putPage does — pages.slug is stored lowercased,+ // so a raw mixed-case slug here would miss the row it just wrote (#430).+ slug = validateSlug(slug);const sourceId = opts?.sourceId ?? 'default';// Source-scope the page-id lookup so duplicate slugs in different sourcesdiff --git a/src/core/postgres-engine.ts b/src/core/postgres-engine.tsindex 3d3dd262fc..645bdea56e 100644--- a/src/core/postgres-engine.ts+++ b/src/core/postgres-engine.ts@@ -2402,6 +2402,9 @@ export class PostgresEngine implements BrainEngine {}private async _upsertChunksOnce(slug: string, chunks: ChunkInput[], opts?: { sourceId?: string }): Promise<void> {+ // Normalize the same way putPage does — pages.slug is stored lowercased,+ // so a raw mixed-case slug here would miss the row it just wrote (#430).+ slug = validateSlug(slug);const sql = this.sql;const sourceId = opts?.sourceId ?? 'default';diff --git a/test/pglite-engine.test.ts b/test/pglite-engine.test.tsindex 620067a8d9..fe24f1463e 100644--- a/test/pglite-engine.test.ts+++ b/test/pglite-engine.test.ts@@ -6,6 +6,7 @@import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test';import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { importFromContent } from '../src/core/import-file.ts';import type { BrainEngine } from '../src/core/engine.ts';import type { PageInput, ChunkInput } from '../src/core/types.ts';@@ -205,6 +206,24 @@ describe('PGLiteEngine: Pages', () => {const page = await engine.putPage('Test/UPPER', testPage);expect(page.slug).toBe('test/upper');});++ test('importFromContent normalizes mixed-case slugs before all tx writes (#430)', async () => {+ const result = await importFromContent(+ engine,+ 'TestNamespace/Page-Name',+ '---\ntype: note\ntitle: Mixed Case\n---\n\nbody text',+ { noEmbed: true },+ );+ expect(result.status).toBe('imported');+ expect(result.slug).toBe('testnamespace/page-name');++ const page = await engine.getPage('testnamespace/page-name');+ expect(page).not.toBeNull();+ expect(page!.title).toBe('Mixed Case');++ const chunks = await engine.getChunks('testnamespace/page-name');+ expect(chunks.length).toBeGreaterThan(0);+ });});// ─────────────────────────────────────────────────────────────────@@ -504,6 +523,17 @@ describe('PGLiteEngine: Chunks', () => {expect(chunks[1].chunk_text).toBe('Chunk one');});+ test('upsertChunks normalizes mixed-case slugs like putPage (#430)', async () => {+ await engine.putPage('Test/ChunkCase', testPage);+ await engine.upsertChunks('Test/ChunkCase', [+ { chunk_index: 0, chunk_text: 'Mixed-case chunk', chunk_source: 'compiled_truth' },+ ]);++ const chunks = await engine.getChunks('test/chunkcase');+ expect(chunks.length).toBe(1);+ expect(chunks[0].chunk_text).toBe('Mixed-case chunk');+ });+test('upsertChunks removes orphan chunks', async () => {await engine.putPage('test/orphan', testPage);await engine.upsertChunks('test/orphan', [