Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Preserve modality and symbol metadata in embed-stale merge (#2969)
zsimovanforgeops4df77960619d
diff --git a/src/core/embed-stale.ts b/src/core/embed-stale.tsindex 484f3ea559..474a3ea035 100644--- a/src/core/embed-stale.ts+++ b/src/core/embed-stale.ts@@ -206,6 +206,22 @@ export async function embedStaleForSource(chunk_source: c.chunk_source,embedding: staleIdxToEmbedding.get(c.chunk_index) ?? undefined,token_count: c.token_count || Math.ceil(c.chunk_text.length / 4),+ // Carry through per-chunk metadata. upsertChunks writes these as+ // EXCLUDED.<col> (not COALESCE), so omitting them here resets image+ // rows to modality='text' (breaking the image search arm's+ // modality='image' filter) and wipes code-chunk symbol metadata on+ // every embed-stale pass. embedding_image is deliberately NOT+ // carried: the upsert COALESCEs it, and getChunks returns the+ // pgvector as a string which upsertChunks would mis-serialize.+ modality: c.modality ?? undefined,+ language: c.language ?? undefined,+ symbol_name: c.symbol_name ?? undefined,+ symbol_type: c.symbol_type ?? undefined,+ start_line: c.start_line ?? undefined,+ end_line: c.end_line ?? undefined,+ parent_symbol_path: c.parent_symbol_path ?? undefined,+ doc_comment: c.doc_comment ?? undefined,+ symbol_name_qualified: c.symbol_name_qualified ?? undefined,}));await observed(pacer, () => engine.upsertChunks(slug, merged, { sourceId: keySourceId }));// v0.41.31: stamp provenance only when EVERY chunk was stale (fullydiff --git a/src/core/types.ts b/src/core/types.tsindex a9fa993942..9464d16dec 100644--- a/src/core/types.ts+++ b/src/core/types.ts@@ -581,6 +581,13 @@ export interface Chunk {parent_symbol_path?: string[] | null;doc_comment?: string | null;symbol_name_qualified?: string | null;+ /**+ * v0.27.1 multimodal. Read side of ChunkInput.modality — must round-trip+ * through getChunks → embed-stale merge → upsertChunks or image rows get+ * reset to 'text' (EXCLUDED.modality on the upsert) and vanish from the+ * image search arm.+ */+ modality?: 'text' | 'image';}/**diff --git a/src/core/utils.ts b/src/core/utils.tsindex 04989263b1..65ecf84ea2 100644--- a/src/core/utils.ts+++ b/src/core/utils.ts@@ -329,6 +329,7 @@ export function rowToChunk(row: Record<string, unknown>, includeEmbedding = falsparent_symbol_path: (row.parent_symbol_path as string[] | null | undefined) ?? null,doc_comment: (row.doc_comment as string | null | undefined) ?? null,symbol_name_qualified: (row.symbol_name_qualified as string | null | undefined) ?? null,+ modality: (row.modality as 'text' | 'image' | undefined) ?? undefined,};}diff --git a/test/embed-stale.serial.test.ts b/test/embed-stale.serial.test.tsindex b2f703a5dc..9c3a882213 100644--- a/test/embed-stale.serial.test.ts+++ b/test/embed-stale.serial.test.ts@@ -227,4 +227,53 @@ describe('embedStaleForSource', () => {const otherStale = await engine.countStaleChunks({ sourceId: 'other' });expect(otherStale).toBe(3);});++ test('preserves modality and code-symbol metadata across the merge round-trip', async () => {+ // Regression: the merged ChunkInput[] used to rebuild rows with only 5+ // fields; upsertChunks writes modality/symbol columns as EXCLUDED.<col>,+ // so an image page with one stale TEXT chunk got its image row reset to+ // modality='text' — permanently invisible to the image search arm.+ await engine.putPage('media/mixed-page', {+ type: 'image',+ title: 'mixed',+ compiled_truth: 'mixed modality page',+ });+ const imgVec = new Float32Array(1024).fill(0.03);+ await engine.upsertChunks('media/mixed-page', [+ {+ chunk_index: 0,+ chunk_text: 'field-photo.jpg',+ chunk_source: 'image_asset',+ modality: 'image',+ embedding_image: imgVec,+ // embedding intentionally present so this row is NOT stale.+ embedding: new Float32Array(1536).fill(0.01),+ token_count: 4,+ },+ {+ chunk_index: 1,+ chunk_text: 'ocr caption text needing embed',+ chunk_source: 'compiled_truth',+ language: 'python',+ symbol_name: 'kept_symbol',+ symbol_type: 'function',+ symbol_name_qualified: 'mod::kept_symbol',+ token_count: 6,+ embedding: undefined, // stale — triggers the merge path+ },+ ]);++ const result = await embedStaleForSource(engine, 'default', { embedFn: fakeEmbedFn });+ expect(result.embedded).toBe(1);++ const after = await engine.getChunks('media/mixed-page');+ const imgRow = after.find((c) => c.chunk_index === 0)!;+ const txtRow = after.find((c) => c.chunk_index === 1)!;+ expect(imgRow.modality).toBe('image');+ expect(txtRow.language).toBe('python');+ expect(txtRow.symbol_name).toBe('kept_symbol');+ expect(txtRow.symbol_name_qualified).toBe('mod::kept_symbol');+ // The stale text row actually got its embedding.+ expect(txtRow.embedded_at).not.toBeNull();+ });});