Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: fill related memories after approximate search filtering
[._.]/ Adam Eivycf322275d710
diff --git a/server/services/memoryDB.db.test.js b/server/services/memoryDB.db.test.jsindex 922a6d5d91..a97dbaa5c2 100644--- a/server/services/memoryDB.db.test.js+++ b/server/services/memoryDB.db.test.js@@ -619,10 +619,38 @@ describe.skipIf(!runDb)('memoryDB search (#3447)', () => {expect(capped).toHaveLength(10);expect(capped.every((r) => r.relationship === 'linked')).toBe(true);- // Limit above the link count: the 11 links plus neighbours fill the remainder.- const filled = await memoryDB.getRelatedMemories(hub.id, 13);- expect(filled).toHaveLength(13);- expect(filled.filter((r) => r.relationship === 'similar')).toHaveLength(2);+ // Force the ANN candidate shortage independently of vacuum timing, table+ // statistics and physical tie order. The service must still fill both slots.+ const originalQuery = pg.Pool.prototype.query;+ let indexedRows = Infinity;+ const probe = vi.spyOn(pg.Pool.prototype, 'query').mockImplementation(async function (sql, params) {+ if (typeof sql !== 'string' || !sql.includes('ORDER BY embedding <=> $1')) {+ return originalQuery.call(this, sql, params);+ }+ const client = await this.connect();+ try {+ await client.query('BEGIN');+ await client.query('SET LOCAL enable_seqscan = off');+ await client.query('SET LOCAL enable_sort = off');+ await client.query('SET LOCAL hnsw.ef_search = 1');+ const iterative = await client.query("SELECT current_setting('hnsw.iterative_scan', true) AS mode");+ if (iterative.rows[0].mode !== null) await client.query('SET LOCAL hnsw.iterative_scan = off');+ const plan = await client.query(`EXPLAIN (FORMAT JSON) ${sql}`, params);+ expect(JSON.stringify(plan.rows)).toContain('idx_memories_embedding');+ const result = await client.query(sql, params);+ indexedRows = result.rows.length;+ return result;+ } finally {+ try { await client.query('ROLLBACK'); }+ finally { client.release(); }+ }+ });+ try {+ const filled = await memoryDB.getRelatedMemories(hub.id, 13);+ expect(indexedRows).toBeLessThan(2);+ expect(filled).toHaveLength(13);+ expect(filled.filter((r) => r.relationship === 'similar')).toHaveLength(2);+ } finally { probe.mockRestore(); }});});diff --git a/server/services/memoryDB.js b/server/services/memoryDB.jsindex 6b528e053a..f869579eca 100644--- a/server/services/memoryDB.js+++ b/server/services/memoryDB.js@@ -844,15 +844,19 @@ export async function getRelatedMemories(id, limit = 10) {// memories ranking among the nearest neighbours can't under-fill (#12768).const linkedIds = related.map(r => r.id);- const similar = await query(`+ const readSimilar = (exact = false) => query(`SELECT id, type, category, tags, summary, importance, created_at, status, source_app_id,1 - (embedding <=> $1) AS similarityFROM memoriesWHERE id != $2 AND embedding IS NOT NULL AND status = 'active'AND NOT (id = ANY($4::uuid[]))- ORDER BY embedding <=> $1+ ORDER BY ${exact ? '(embedding <=> $1) + 0' : 'embedding <=> $1'}LIMIT $3`, [embedding, id, remaining, linkedIds]);+ let similar = await readSimilar();+ // ANN scans filter after their bounded candidate search. If they under-fill,+ // an expression sort bypasses the ANN index and scores all eligible rows.+ if (similar.rows.length < remaining) similar = await readSimilar(true);for (const row of similar.rows) {related.push({