Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(embed): mixed-space warning skips current-model vectors on unstamped pages (#5527)

garrytan05a7f504dfb0
diff --git a/src/commands/embed.ts b/src/commands/embed.ts
index c837b97fe2..8eacf09331 100644
--- a/src/commands/embed.ts
+++ b/src/commands/embed.ts
@@ -1707,7 +1707,11 @@ async function embedAllStale(
try {
const wide = await engine.countStaleChunks({ ...sourceOpt, signature, includeNullSignature: true });
const narrow = await engine.countStaleChunks({ ...sourceOpt, signature });
- const leftBehind = wide - narrow;
+ // #5527: a NULL-signature page whose vectors already match the current
+ // model, width and text is only missing its stamp, not in another space.
+ const currentSpace = await countRestampOnlyChunks(engine, { signature, sourceId, includeNullSignature: true })
+ - await countRestampOnlyChunks(engine, { signature, sourceId });
+ const leftBehind = wide - narrow - currentSpace;
if (leftBehind > 0) {
serr(
` [embed] WARNING: ${leftBehind} embedded chunk(s) sit on pages with no recorded ` +
diff --git a/test/embed-null-signature-warning-5527.serial.test.ts b/test/embed-null-signature-warning-5527.serial.test.ts
new file mode 100644
index 0000000000..34a5ef47f8
--- /dev/null
+++ b/test/embed-null-signature-warning-5527.serial.test.ts
@@ -0,0 +1,70 @@
+import { installFixtureChunks } from './helpers/page-projection.ts';
+/**
+ * #5527: the "previous model's embedding space" warning counts only chunks
+ * that really are in another space.
+ *
+ * Protects: an `embed --stale` run (no `--include-null-signature`) on a brain
+ * whose NULL-signature pages carry vectors already embedded by the current
+ * model, width and text does not tell the operator those vectors sit in the
+ * previous model's space; chunks embedded by another model still trigger it.
+ * Fails when: the warning subtracts only the narrow stale count, so
+ * current-space chunks on unstamped pages are reported as mixed-space.
+ * Installs the process-global gateway transport seam (always fake), so this
+ * stays a `.serial.test.ts`.
+ */
+import { afterAll, afterEach, beforeAll, describe, expect, spyOn, test } from 'bun:test';
+import { runEmbedCore } from '../src/commands/embed.ts';
+import { __setEmbedTransportForTests, configureGateway, resetGateway } from '../src/core/ai/gateway.ts';
+import type { BrainEngine } from '../src/core/engine.ts';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+
+const DIMS = 1536;
+const MODEL = 'openai:text-embedding-3-large';
+
+async function seed(engine: BrainEngine, slug: string, model: string): Promise<void> {
+ await engine.putPage(slug, { type: 'note', title: slug, compiled_truth: `# ${slug}` });
+ await installFixtureChunks(engine, slug, [{
+ chunk_index: 0, chunk_text: `${slug} body`, chunk_source: 'compiled_truth', token_count: 4,
+ embedding: new Float32Array(DIMS).fill(0.002), model,
+ }]);
+}
+
+beforeAll(() => {
+ configureGateway({ embedding_model: MODEL, embedding_dimensions: DIMS, env: { ...process.env, OPENAI_API_KEY: 'sk-test-fake' } });
+ __setEmbedTransportForTests((async (input: { values: string[] }) =>
+ ({ embeddings: input.values.map(() => new Array(DIMS).fill(0.001)), usage: { tokens: input.values.length * 4 } })) as never);
+});
+afterAll(() => { __setEmbedTransportForTests(null); resetGateway(); });
+
+describe('#5527 null-signature mixed-space warning', () => {
+ let engine: BrainEngine;
+ let stderr: string[] = [];
+ const capture = () => {
+ stderr = [];
+ const spy = spyOn(console, 'error').mockImplementation((...args: unknown[]) => { stderr.push(args.join(' ')); });
+ return spy;
+ };
+ beforeAll(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({ embedding_dimensions: DIMS } as never);
+ await engine.initSchema();
+ }, 120_000);
+ afterAll(async () => { await engine.disconnect(); });
+ afterEach(() => { stderr = []; });
+
+ test('current-model vectors on unstamped pages are not reported as another space', async () => {
+ await seed(engine, 'current-a', MODEL);
+ await seed(engine, 'current-b', MODEL);
+ const spy = capture();
+ try { await runEmbedCore(engine, { stale: true, catchUp: true, quiet: true }); } finally { spy.mockRestore(); }
+ expect(stderr.filter(line => line.includes("previous model's"))).toEqual([]);
+ });
+
+ test('vectors from another model still warn, counting only those chunks', async () => {
+ await seed(engine, 'legacy-a', 'legacy:model');
+ const spy = capture();
+ try { await runEmbedCore(engine, { stale: true, catchUp: true, quiet: true }); } finally { spy.mockRestore(); }
+ const warning = stderr.find(line => line.includes("previous model's"));
+ expect(warning).toContain('WARNING: 1 embedded chunk(s)');
+ });
+});