Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(scale): dense seeded vectors in 16 dimensions so the injected-vector known answer holds on an HNSW plan
garrytan421836c2065c
diff --git a/docs/TESTING.md b/docs/TESTING.mdindex 85ac56027a..0aa4b2bec6 100644--- a/docs/TESTING.md+++ b/docs/TESTING.md@@ -277,7 +277,7 @@ O-CEO-9) in the Foundations 1 plan; `scripts/scale/gates.ts` and[--corpus-dir <dir>] [--import-mode cli|content] [--enforce] [--out <file.json>]`(`scripts/scale/run.ts`) generates a deterministic two-source brain from theseed (`scripts/scale/fixture.ts`: links, timeline bullets, `## Facts` and-`## Takes` fences, partly overlapping bodies, island pages, a two-hot vector+`## Takes` fences, partly overlapping bodies, island pages, a seeded dense vector in 16 dimensionsper page), and imports it into a fresh brain under a temporary `GBRAIN_HOME`:PGLite in a fresh data dir, or Postgres in a fresh database created from`DATABASE_URL` and dropped afterwards. The default `--import-mode cli` writesdiff --git a/scripts/scale/fixture.ts b/scripts/scale/fixture.tsindex 05de4e1da9..2294b579ef 100644--- a/scripts/scale/fixture.ts+++ b/scripts/scale/fixture.ts@@ -9,7 +9,7 @@* pages of its own source, carries two dated timeline bullets and a unique* search token. A few island pages have no links in or out (known orphans).* Every tenth page (a person) carries a `## Facts` and a `## Takes` fence with- * two rows each. Each page has a deterministic two-hot query vector+ * two rows each. Each page has a deterministic query vector* (`scaleVector`) that the harness writes onto its chunks and injects through* `queryEmbedFn`, so the vector arm runs keylessly.*/@@ -66,19 +66,26 @@ function kindFor(i: number): 'people' | 'companies' | 'notes' {return r < 2 ? 'people' : r < 3 ? 'companies' : 'notes';}+/** Dimensions a scale vector occupies: a low intrinsic dimension an HNSW graph can navigate. */+export const SCALE_VECTOR_DIMS = 16;+/**- * The two dimensions set in page `index`'s vector. Distinct for every index- * below dim * (dim - 1), so the target page is the only chunk at cosine 1.+ * Page `index`'s vector: a seeded random unit vector in the first+ * SCALE_VECTOR_DIMS dimensions, zero elsewhere. Distinct per index, so the+ * target page is the only chunk at cosine 1. Dense on purpose: with sparse+ * two-hot vectors almost every pair sits at the same distance, so an HNSW+ * search has no gradient to follow and misses the target in most index builds.*/-export function scaleVectorDims(index: number, dim: number): [number, number] {- const a = index % dim;- const b = (a + 1 + (Math.floor(index / dim) % (dim - 1))) % dim;- return [a, b];-}-export function scaleVector(index: number, dim: number): Float32Array {+ const rand = mulberry32(Math.imul(index + 1, 0x9e3779b1));+ const k = Math.min(SCALE_VECTOR_DIMS, dim);const v = new Float32Array(dim);- for (const d of scaleVectorDims(index, dim)) v[d] = Math.SQRT1_2;+ let norm = 0;+ for (let i = 0; i < k; i++) {+ v[i] = rand() * 2 - 1;+ norm += v[i]! * v[i]!;+ }+ for (let i = 0; i < k; i++) v[i] = v[i]! / Math.sqrt(norm);return v;}diff --git a/test/scripts/scale-fixture.test.ts b/test/scripts/scale-fixture.test.tsindex 40c1e811ac..45b99d8503 100644--- a/test/scripts/scale-fixture.test.ts+++ b/test/scripts/scale-fixture.test.ts@@ -9,7 +9,7 @@ import { expect, test } from 'bun:test';import { mkdtempSync, readFileSync, rmSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';-import { generateScaleFixture, scaleVector, scaleVectorDims, writeScaleCorpus } from '../../scripts/scale/fixture.ts';+import { generateScaleFixture, SCALE_VECTOR_DIMS, scaleVector, writeScaleCorpus } from '../../scripts/scale/fixture.ts';import { parseFactsFence } from '../../src/core/facts-fence.ts';import { parseTakesFence } from '../../src/core/takes-fence.ts';@@ -55,11 +55,11 @@ test('fixture additions: facts/takes fences parse, vectors single out their pageexpect(facts).toBeGreaterThan(0);expect(f.expected).toEqual({ facts, takes });- // Two-hot vectors: the probe page is the only page at cosine 1 to its own vector.+ // Dense unit vectors in a low-dimensional subspace: the probe page is the only page at cosine 1 to its own vector.const dim = 64;- const keys = f.pages.map(p => scaleVectorDims(p.index, dim).join(','));- expect(new Set(keys).size).toBe(f.pages.length);const probe = scaleVector(f.vectorProbe.index, dim);+ expect(probe.slice(SCALE_VECTOR_DIMS).every(x => x === 0)).toBe(true);+ expect(Math.abs(probe.reduce((s, x) => s + x * x, 0) - 1)).toBeLessThan(1e-5);const cosines = f.pages.map(p => scaleVector(p.index, dim).reduce((s, x, i) => s + x * probe[i]!, 0));expect(cosines.filter(c => c > 0.999)).toHaveLength(1);expect(f.pages[f.vectorProbe.index]).toMatchObject({ sourceId: f.vectorProbe.sourceId, slug: f.vectorProbe.slug });