Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(migrate): the legacy engine copier carries image chunks' embedding_image (#6871)

garrytan3758d230504d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 07205181ed..d920438173 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -50,6 +50,7 @@ Nothing runs on upgrade. Each repair below is preview-first, and you choose when
- `embed --stale --images` skips sealable pages and reports `reimport`, `incomplete_sealed`, `ocr_required`, `protected` and `archived`.
- `gbrain projections drain` reports per-class `remaining_by_class` and never says "empty" while image rows remain.
- A read-only `[AGENT]` notice after upgrading names the drain preview.
+- `gbrain migrate --to <engine>` (the legacy copier) now carries an image chunk's `embedding_image` vector (#6871). Before, a migrated image page arrived without it and needed a paid re-embed to show up in image search.
#### Held embedding budgets (part 3, #6847)
diff --git a/src/commands/migrate-engine.ts b/src/commands/migrate-engine.ts
index 8776a290b1..7a3ab3a612 100644
--- a/src/commands/migrate-engine.ts
+++ b/src/commands/migrate-engine.ts
@@ -547,6 +547,7 @@ export async function copyPageToTarget(
chunk_text: c.chunk_text,
chunk_source: c.chunk_source,
embedding: c.embedding || undefined,
+ embedding_image: c.embedding_image || undefined,
model: c.model,
token_count: c.token_count || undefined,
})), sourceOpts);
diff --git a/src/core/types.ts b/src/core/types.ts
index 899231fc62..2c613cb57a 100644
--- a/src/core/types.ts
+++ b/src/core/types.ts
@@ -691,13 +691,11 @@ 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.
- */
+ /** 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) and vanish from the image arm. */
modality?: 'text' | 'image';
+ /** #6871: the image vector; only getChunksWithEmbeddings reads it, so `gbrain migrate` carries it. */
+ embedding_image?: Float32Array | null;
/** v244: UTF-8 byte span in the chunk_source's projected text (see ChunkInput); null when unrecorded. */
projected_byte_start?: number | null;
projected_byte_end?: number | null;
diff --git a/src/core/utils.ts b/src/core/utils.ts
index e0f81484ab..c0c9ad6120 100644
--- a/src/core/utils.ts
+++ b/src/core/utils.ts
@@ -503,6 +503,7 @@ export function rowToChunk(row: Record<string, unknown>, includeEmbedding = fals
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,
+ ...(includeEmbedding && row.embedding_image != null && { embedding_image: parseEmbedding(row.embedding_image) }),
// v244: projected UTF-8 span (bigint on Postgres); only when the SELECT carried the column.
...(row.projected_byte_start !== undefined && { projected_byte_start: row.projected_byte_start == null ? null : Number(row.projected_byte_start) }),
...(row.projected_byte_end !== undefined && { projected_byte_end: row.projected_byte_end == null ? null : Number(row.projected_byte_end) }),
diff --git a/test/migrate-engine-image-vectors-6871.serial.test.ts b/test/migrate-engine-image-vectors-6871.serial.test.ts
new file mode 100644
index 0000000000..9119606883
--- /dev/null
+++ b/test/migrate-engine-image-vectors-6871.serial.test.ts
@@ -0,0 +1,65 @@
+/**
+ * #6871: `gbrain migrate --to <engine>` (the legacy copier) must carry an image chunk's
+ * `embedding_image` vector. It used to copy the chunk with only the text embedding, so a
+ * migrated image page lost its visual vector and needed a paid re-embed to be found by image
+ * search again.
+ */
+import { afterEach, describe, expect, test } from 'bun:test';
+import { mkdtempSync, rmSync } from 'fs';
+import { tmpdir } from 'os';
+import { join } from 'path';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { runMigrateEngine } from '../src/commands/migrate-engine.ts';
+import { saveConfig } from '../src/core/config.ts';
+import { _resetCliExitVerdictForTests, currentExitCode } from '../src/core/cli-force-exit.ts';
+import { withEnv } from './helpers/with-env.ts';
+
+describe('gbrain migrate carries image vectors (#6871)', () => {
+ afterEach(() => { _resetCliExitVerdictForTests(); });
+
+ test('an image chunk arrives on the target with its embedding_image vector', async () => {
+ const home = mkdtempSync(join(tmpdir(), 'gbrain-migrate-image-home-'));
+ const targetDir = mkdtempSync(join(tmpdir(), 'gbrain-migrate-image-target-'));
+ const targetPath = join(targetDir, 'brain.pglite');
+ let source: PGLiteEngine | null = null;
+ let target: PGLiteEngine | null = null;
+ try {
+ await withEnv({ GBRAIN_HOME: home, DATABASE_URL: undefined, GBRAIN_DATABASE_URL: undefined }, async () => {
+ saveConfig({ engine: 'postgres', database_url: 'postgresql://unused/guard-only' });
+ source = new PGLiteEngine();
+ await source.connect({});
+ await source.initSchema();
+ await source.putPage('images/diagram-example.png', { type: 'image', title: 'diagram-example.png', compiled_truth: '', timeline: '', frontmatter: {} });
+ const visual = new Float32Array(1024);
+ visual[0] = 0.75;
+ visual[1023] = -0.5;
+ await source.upsertChunks('images/diagram-example.png', [{
+ chunk_index: 0, chunk_text: 'diagram-example.png', chunk_source: 'image_asset', modality: 'image', embedding_image: visual,
+ }]);
+
+ const log = console.log;
+ console.log = () => {};
+ try { await runMigrateEngine(source, ['--to', 'pglite', '--path', targetPath]); } finally { console.log = log; }
+ expect(currentExitCode()).toBe(0);
+
+ target = new PGLiteEngine();
+ await target.connect({ database_path: targetPath });
+ const rows = await target.executeRaw<{ modality: string; image: string | null }>(
+ `SELECT cc.modality, cc.embedding_image::text AS image FROM content_chunks cc JOIN pages p ON p.id = cc.page_id
+ WHERE p.slug = 'images/diagram-example.png'`);
+ expect(rows.length).toBe(1);
+ expect(rows[0]!.modality).toBe('image');
+ expect(rows[0]!.image).not.toBeNull();
+ const copied = JSON.parse(rows[0]!.image!) as number[];
+ expect(copied.length).toBe(1024);
+ expect(copied[0]).toBeCloseTo(0.75);
+ expect(copied[1023]).toBeCloseTo(-0.5);
+ });
+ } finally {
+ await (target as PGLiteEngine | null)?.disconnect();
+ await (source as PGLiteEngine | null)?.disconnect();
+ rmSync(home, { recursive: true, force: true });
+ rmSync(targetDir, { recursive: true, force: true });
+ }
+ }, 120_000);
+});