Skip to content

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

perf: scope Brain graph similarity reads to bridged memories (#10953)

Adam Eivya4b43a10c584
diff --git a/server/services/brainGraph.js b/server/services/brainGraph.js
index f413da1907..19af5f429b 100644
--- a/server/services/brainGraph.js
+++ b/server/services/brainGraph.js
@@ -207,12 +207,28 @@ async function loadGraphContext() {
reverseBridge[memId] = bKey.split(':')[1];
}
+ // Only memories bridged to a CURRENT node can contribute an edge, so scope the
+ // memory graph read to them (#10953): similarity work drops from scoring every
+ // embedded memory as a source to scoring just these. The backend keeps the
+ // global candidate population, so the edges are identical to the full graph's
+ // filtered to these endpoints. Fewer than two participating nodes cannot form
+ // an edge (self-edges are dropped below), so skip the read entirely.
+ const sourceIds = [];
+ const participating = new Set();
+ for (const [memId, brainId] of Object.entries(reverseBridge)) {
+ if (!brainIdSet.has(brainId)) continue;
+ sourceIds.push(memId);
+ participating.add(brainId);
+ }
+
// Remap CoS similar/linked edges to brain ids. ~3 similar/node + links — not
// the explosion (that's shared_tag, computed per bounded set below).
const remappedEdges = [];
const seen = new Set();
let hasEmbeddings = false;
- const cosGraph = await memoryBackend.getGraphData().catch(() => null);
+ const cosGraph = participating.size < 2
+ ? null
+ : await memoryBackend.getGraphData({ sourceIds }).catch(() => null);
if (cosGraph) {
for (const edge of cosGraph.edges) {
const source = reverseBridge[edge.source];
diff --git a/server/services/brainGraph.test.js b/server/services/brainGraph.test.js
index 729a281632..72c5f71bb3 100644
--- a/server/services/brainGraph.test.js
+++ b/server/services/brainGraph.test.js
@@ -308,10 +308,52 @@ describe('getBrainGraphOverview', () => {
});
it('treats a getGraphData failure as no edges', async () => {
- onlyType('people', [{ id: 'p1', name: 'A' }]);
+ onlyType('people', [{ id: 'p1', name: 'A' }, { id: 'p2', name: 'B' }]);
+ loadBridgeMap.mockResolvedValue({ 'people:p1': 'mem-1', 'people:p2': 'mem-2' });
memoryBackend.getGraphData.mockRejectedValue(new Error('embeddings unavailable'));
const result = await getBrainGraphOverview({ limit: 100 });
- expect(result.nodes).toHaveLength(1);
+ expect(memoryBackend.getGraphData).toHaveBeenCalledTimes(1);
+ expect(result.nodes).toHaveLength(2);
+ expect(result.edges).toEqual([]);
+ expect(result.hasEmbeddings).toBe(false);
+ });
+
+ // #10953: the memory graph read is scoped to memories bridged to a node in
+ // the current set — archived records and bridge entries whose record is gone
+ // contribute nothing, so they are never submitted as similarity sources.
+ it('scopes the memory graph read to memories bridged to current nodes', async () => {
+ brainStorage.getAll.mockImplementation(async (type) => {
+ if (type === 'people') return [{ id: 'p1', name: 'A' }, { id: 'p2', name: 'B', archived: true }];
+ if (type === 'ideas') return [{ id: 'i1', title: 'Idea' }];
+ return [];
+ });
+ loadBridgeMap.mockResolvedValue({
+ 'people:p1': 'mem-1',
+ 'people:p2': 'mem-archived',
+ 'ideas:i1': 'mem-2',
+ 'projects:gone': 'mem-missing'
+ });
+ memoryBackend.getGraphData.mockResolvedValue({ nodes: [], edges: [] });
+ await getBrainGraphOverview({ limit: 100 });
+ expect(memoryBackend.getGraphData).toHaveBeenCalledTimes(1);
+ const [{ sourceIds }] = memoryBackend.getGraphData.mock.calls[0];
+ expect([...sourceIds].sort()).toEqual(['mem-1', 'mem-2']);
+
+ memoryBackend.getGraphData.mockClear();
+ await getBrainGraphNeighborhood({ focusId: 'p1' });
+ expect(memoryBackend.getGraphData).toHaveBeenCalledTimes(1);
+ expect([...memoryBackend.getGraphData.mock.calls[0][0].sourceIds].sort()).toEqual(['mem-1', 'mem-2']);
+ });
+
+ it('skips the memory graph read when fewer than two current nodes are bridged', async () => {
+ onlyType('people', [{ id: 'p1', name: 'A' }, { id: 'p2', name: 'B' }]);
+ // Zero bridged nodes.
+ await getBrainGraphOverview({ limit: 100 });
+ // One bridged node — its other mapping points at a record that is not a node.
+ loadBridgeMap.mockResolvedValue({ 'people:p1': 'mem-1', 'people:gone': 'mem-2' });
+ const result = await getBrainGraphOverview({ limit: 100 });
+ await getBrainGraphNeighborhood({ focusId: 'p1' });
+ expect(memoryBackend.getGraphData).not.toHaveBeenCalled();
expect(result.edges).toEqual([]);
expect(result.hasEmbeddings).toBe(false);
});
diff --git a/server/services/memory.js b/server/services/memory.js
index 9afaee8cef..4b18131ca9 100644
--- a/server/services/memory.js
+++ b/server/services/memory.js
@@ -638,13 +638,26 @@ export async function getRelatedMemories(id, limit = 10) {
}
/**
- * Get graph data for visualization
+ * Get graph data for visualization.
+ *
+ * `sourceIds` mirrors the PostgreSQL backend (#10953): absent (undefined/null)
+ * returns the whole graph; an array — even an empty one — returns only the
+ * nodes in it and the edges whose BOTH endpoints are in it. Similarity is
+ * still ranked against every stored vector, so an out-of-scope nearest
+ * neighbour keeps its top-three slot.
*/
-export async function getGraphData() {
+export async function getGraphData({ sourceIds } = {}) {
+ if (sourceIds !== undefined && sourceIds !== null && !Array.isArray(sourceIds)) {
+ throw new TypeError('getGraphData: sourceIds must be an array');
+ }
+ const scope = Array.isArray(sourceIds) ? new Set(sourceIds) : null;
+ if (scope && scope.size === 0) return { nodes: [], edges: [] };
+ const inScope = (id) => !scope || scope.has(id);
+
const index = await loadIndex();
const embeddings = await loadEmbeddings();
- const activeMemories = index.memories.filter(m => m.status === 'active');
+ const activeMemories = index.memories.filter(m => m.status === 'active' && inScope(m.id));
// Build nodes
const nodes = activeMemories.map(m => ({
@@ -675,6 +688,7 @@ export async function getGraphData() {
// Explicit links
for (const targetId of full.relatedMemories) {
+ if (!inScope(targetId)) continue;
const edgeKey = [memory.id, targetId].sort().join('-');
if (!seenEdges.has(edgeKey)) {
seenEdges.add(edgeKey);
@@ -695,6 +709,9 @@ export async function getGraphData() {
for (const item of similar) {
if (item.id === memory.id) continue;
if (item.similarity < 0.8) continue;
+ // Filtered only after the global top-K: an out-of-scope neighbour still
+ // consumes its slot rather than letting a lower-ranked one replace it.
+ if (!inScope(item.id)) continue;
const edgeKey = [memory.id, item.id].sort().join('-');
if (!seenEdges.has(edgeKey)) {
diff --git a/server/services/memory.test.js b/server/services/memory.test.js
index 387faf23d5..1d87f36c43 100644
--- a/server/services/memory.test.js
+++ b/server/services/memory.test.js
@@ -1564,6 +1564,59 @@ describe('memory service', () => {
expect(result.nodes).toHaveLength(1);
});
+
+ // #10953: a scoped read equals the full graph filtered to scoped endpoints,
+ // ranking similarity against every vector. m1's global top three are
+ // m2, m3, m5 (m3/m5 out of scope); the in-scope m4 is fourth. A scope applied
+ // BEFORE the top-K would promote m4 and invent an m1–m4 edge.
+ it('scopes to sourceIds without promoting a lower-ranked in-scope neighbour', async () => {
+ const { findTopK: realFindTopK } = await vi.importActual('../lib/vectorMath.js');
+ findTopK.mockImplementation(realFindTopK);
+ try {
+ const meta = (id, status = 'active') => ({ id, type: 'fact', category: 'other', summary: id, importance: 0.5, status });
+ const ids = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8'];
+ const mockIndex = { version: 1, lastUpdated: '', count: 9, memories: [...ids.map(id => meta(id)), meta('m9', 'archived')] };
+ const vectors = {
+ m1: [1, 0, 0, 0], m2: [1, 0.1, 0, 0], m3: [1, 0.15, 0, 0], m5: [1, 0, 0.2, 0],
+ m4: [1, 0, 0, 0.45], m6: [1, 0, 0, 0.5], m7: [1, 0, 0, 0.55], m8: [1, 0, 0, 0.6]
+ };
+ const files = {
+ m2: { id: 'm2', relatedMemories: ['m4'], typedLinks: [{ id: 'l1', targetId: 'm4', linkType: 'supersedes' }] },
+ m4: { id: 'm4', relatedMemories: ['m2', 'm6', 'm9'] }
+ };
+ readJSONFile.mockImplementation((path, def) => {
+ if (path.includes('index.json')) return Promise.resolve(mockIndex);
+ if (path.includes('embeddings.json')) return Promise.resolve({ model: 'test', dimension: 4, vectors });
+ const id = path.match(/[\\/](m\d)[\\/]memory\.json$/)?.[1];
+ if (id) return Promise.resolve(files[id] ?? { id, relatedMemories: [] });
+ return Promise.resolve(def);
+ });
+
+ const scope = new Set(['m1', 'm2', 'm4', 'm9', 'not-a-memory']);
+ const full = await getGraphData();
+ const scoped = await getGraphData({ sourceIds: [...scope] });
+
+ expect(full.edges.length).toBeGreaterThan(scoped.edges.length);
+ expect(scoped).toEqual({
+ nodes: full.nodes.filter(n => scope.has(n.id)),
+ edges: full.edges.filter(e => scope.has(e.source) && scope.has(e.target))
+ });
+ const pairs = scoped.edges.map(e => `${[e.source, e.target].sort().join('-')}:${e.type}`);
+ expect(pairs).toContain('m1-m2:similar');
+ expect(pairs).not.toContain('m1-m4:similar');
+ // The archived m9 is linked from m4 in BOTH modes (the full graph does not
+ // gate relatedMemories targets on status); scoping must not change that.
+ expect(pairs).toContain('m4-m9:linked');
+
+ // Empty scope stays empty (no file reads); absent scope stays the full graph.
+ readJSONFile.mockClear();
+ expect(await getGraphData({ sourceIds: [] })).toEqual({ nodes: [], edges: [] });
+ expect(readJSONFile).not.toHaveBeenCalled();
+ expect(await getGraphData({})).toEqual(full);
+ } finally {
+ findTopK.mockReturnValue([]);
+ }
+ });
});
// ===========================================================================
diff --git a/server/services/memoryBackend.js b/server/services/memoryBackend.js
index 44acfc89e1..8102c85c30 100644
--- a/server/services/memoryBackend.js
+++ b/server/services/memoryBackend.js
@@ -205,9 +205,11 @@ export async function getRelatedMemories(id, limit) {
return b.getRelatedMemories(id, limit);
}
-export async function getGraphData() {
+// `options.sourceIds` (internal): absent → the full CoS graph; an array (even
+// empty) → only nodes/edges inside that memory-id scope (#10953).
+export async function getGraphData(options) {
const b = await getBackend();
- return b.getGraphData();
+ return b.getGraphData(options);
}
export async function getMemoryIdsMissingEmbedding() {
diff --git a/server/services/memoryDB.db.test.js b/server/services/memoryDB.db.test.js
index 6140bc5276..5e203a4f9f 100644
--- a/server/services/memoryDB.db.test.js
+++ b/server/services/memoryDB.db.test.js
@@ -749,9 +749,9 @@ describe.skipIf(!runDb)('memoryDB getGraphData (#3447)', () => {
// Observe real graph statements on their pool connection; do not replace SQL
// results. This also checks settings immediately after COMMIT/ROLLBACK, before
// that same connection is released.
-function observeGraph({ afterNodes, failSimilarity = false } = {}) {
+function observeGraph({ afterNodes, failSimilarity = false, explain = false } = {}) {
const original = pg.Client.prototype.query;
- const observation = { statements: [], settings: null, restored: null, directed: [] };
+ const observation = { statements: [], settings: null, restored: null, directed: [], plan: null };
let graphClient;
const spy = vi.spyOn(pg.Client.prototype, 'query').mockImplementation(function (...args) {
const sql = args[0];
@@ -765,6 +765,8 @@ function observeGraph({ afterNodes, failSimilarity = false } = {}) {
current_setting('transaction_isolation') AS isolation,
current_setting('transaction_read_only') AS read_only`)).rows[0];
if (failSimilarity) await original.call(this, 'SELECT 1 / 0');
+ // The real statement and parameters, planned inside the same snapshot.
+ if (explain) observation.plan = (await original.call(this, 'EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) ' + sql, args[1])).rows[0]['QUERY PLAN'][0].Plan;
}
const result = await original.apply(this, args);
if (sql.includes('SELECT id, type, category, summary, importance')) await afterNodes?.();
@@ -992,6 +994,125 @@ describe.skipIf(!runDb)('memoryDB bounded graph snapshot (#9526)', () => {
}, 60000);
});
+// #10953: Brain graph reads pass the memory ids bridged to visible Brain nodes.
+// Scoped output must equal the full graph filtered to those endpoints.
+const planNodes = (node) => [node, ...(node.Plans ?? []).flatMap(planNodes)];
+const graphKey = (e) => `${[e.source, e.target].sort().join('/')}:${e.type}:${e.linkType ?? ''}:${e.weight}`;
+const filteredGraph = (graph, scope) => ({
+ nodes: graph.nodes.filter(n => scope.has(n.id)).map(n => n.id).sort(),
+ edges: graph.edges.filter(e => scope.has(e.source) && scope.has(e.target)).map(graphKey).sort(),
+});
+const graphShape = (graph) => ({ nodes: graph.nodes.map(n => n.id).sort(), edges: graph.edges.map(graphKey).sort() });
+
+describe.skipIf(!runDb)('memoryDB scoped graph sources (#10953)', () => {
+ beforeEach(resetMemories);
+
+ it('keeps an out-of-scope neighbour in its global top-three slot, link precedence and archived rules', async () => {
+ // m1's global top three are m2, m3, m5 (m3/m5 out of scope); in-scope m4 is
+ // fourth, and m4's own top three are m6–m8. Restricting candidates before
+ // LIMIT 3 would promote m4 and invent an m1–m4 edge.
+ const vec = (x, y = 0, z = 0) => { const v = axis(0); v[1] = x; v[2] = y; v[3] = z; return v; };
+ const make = (name, embedding, status) => memoryDB.createMemory({ type: 'fact', content: `Scope ${name}.`, status }, embedding);
+ const m1 = await make('m1', vec(0));
+ const m2 = await make('m2', vec(0.1));
+ const m3 = await make('m3', vec(0.15));
+ const m5 = await make('m5', vec(0, 0.2));
+ const m4 = await make('m4', vec(0, 0, 0.45));
+ const m6 = await make('m6', vec(0, 0, 0.5));
+ await make('m7', vec(0, 0, 0.55));
+ await make('m8', vec(0, 0, 0.6));
+ const gone = await make('archived', vec(0.05), 'archived');
+ const unembedded = await memoryDB.createMemory({ type: 'fact', content: 'Scope no vector.' });
+ await memoryDB.linkMemories(m1.id, m2.id); // precedence over similar
+ await memoryDB.linkMemories(m4.id, m2.id, { linkType: 'supersedes' }); // directed orientation
+ await memoryDB.linkMemories(m4.id, unembedded.id); // in-scope null vector
+ await memoryDB.linkMemories(m4.id, m6.id); // out-of-scope endpoint
+ await memoryDB.linkMemories(m1.id, gone.id); // archived endpoint
+
+ const scope = new Set([m1.id, m2.id, m4.id, unembedded.id, gone.id]);
+ const full = await memoryDB.getGraphData();
+ const scoped = await memoryDB.getGraphData({ sourceIds: [...scope, 'not-a-uuid'] });
+ expect(graphShape(scoped)).toEqual(filteredGraph(full, scope));
+ expect(scoped.nodes.some(n => n.id === gone.id)).toBe(false);
+
+ const pair = (a, b) => scoped.edges.filter(e => [e.source, e.target].sort().join('/') === [a.id, b.id].sort().join('/'));
+ expect(pair(m1, m2)).toEqual([expect.objectContaining({ type: 'linked', linkType: 'related' })]);
+ expect(pair(m1, m4)).toEqual([]);
+ expect(pair(m2, m4).find(e => e.linkType === 'supersedes')).toMatchObject({ source: m4.id, target: m2.id });
+ expect(pair(m4, unembedded)).toHaveLength(1);
+ expect(full.edges.some(e => [e.source, e.target].includes(m3.id) || [e.source, e.target].includes(m5.id))).toBe(true);
+
+ // Empty scope never reaches the database; absent scope is the full graph.
+ const probe = observeGraph();
+ try {
+ expect(await memoryDB.getGraphData({ sourceIds: [] })).toEqual({ nodes: [], edges: [] });
+ expect(await memoryDB.getGraphData({ sourceIds: ['not-a-uuid'] })).toEqual({ nodes: [], edges: [] });
+ expect(probe.observation.statements).toEqual([]);
+ } finally {
+ probe.restore();
+ }
+ expect(graphShape(await memoryDB.getGraphData({}))).toEqual(graphShape(full));
+ await expect(memoryDB.getGraphData({ sourceIds: 'nope' })).rejects.toThrow(TypeError);
+ });
+
+ it('plans fewer outer similarity loops with unchanged relevant rows and no spill', async () => {
+ await seedGraphVectors(40);
+ const scopeIds = (await query('SELECT md5(i::text)::uuid::text AS id FROM generate_series(1, 10) i')).rows.map(r => r.id);
+ const scope = new Set(scopeIds);
+ const legacy = await withTransaction(async client => {
+ await client.query('SET LOCAL enable_indexscan = off');
+ return (await client.query(legacyGraphSimilarity)).rows;
+ });
+
+ const run = async (options) => {
+ const probe = observeGraph({ explain: true });
+ try {
+ const graph = await memoryDB.getGraphData(options);
+ return { graph, ...probe.observation };
+ } finally {
+ probe.restore();
+ }
+ };
+ const full = await run();
+ const scoped = await run({ sourceIds: scopeIds });
+
+ const lateralLoops = (plan) => planNodes(plan).find(n => n['Node Type'] === 'Limit')['Actual Loops'];
+ expect(lateralLoops(full.plan)).toBe(40);
+ expect(lateralLoops(scoped.plan)).toBe(scope.size);
+ for (const plan of [full.plan, scoped.plan]) {
+ expect(plan['Temp Written Blocks']).toBe(0);
+ expect(plan['Temp Read Blocks']).toBe(0);
+ }
+ expect(scoped.settings).toEqual({ work_mem: '32MB', isolation: 'repeatable read', read_only: 'on' });
+
+ const relevant = rows => sortedDirected(rows.filter(r => scope.has(r.source_id) && scope.has(r.target_id)));
+ expect(relevant(legacy).length).toBeGreaterThan(0);
+ expect(sortedDirected(scoped.directed)).toEqual(relevant(legacy));
+ expect(graphShape(scoped.graph)).toEqual(filteredGraph(full.graph, scope));
+ });
+
+ it.each([2048, 2049])('admits a scoped read by the whole active count (%i nodes)', async count => {
+ await query(`INSERT INTO memories (id, type, content, summary, embedding)
+ SELECT md5(i::text)::uuid, 'fact', 'Boundary fixture', 'Boundary fixture',
+ CASE WHEN i <= 6 THEN $2::vector ELSE NULL END
+ FROM generate_series(1, $1::int) i`, [count, JSON.stringify(VEC_A)]);
+ await query('ANALYZE memories');
+ // Every embedded node plus two null-vector nodes: all ties stay in scope.
+ const scopeIds = (await query('SELECT md5(i::text)::uuid::text AS id FROM generate_series(1, 8) i')).rows.map(r => r.id);
+ const probe = observeGraph();
+ try {
+ const graph = await memoryDB.getGraphData({ sourceIds: scopeIds });
+ expect(graph.nodes.map(n => n.id).sort()).toEqual([...scopeIds].sort());
+ expect(probe.observation.statements.some(sql => sql.includes('AS MATERIALIZED'))).toBe(count === 2048);
+ expect(probe.observation.directed).toHaveLength(18);
+ expect(graph.edges).toHaveLength(undirected(probe.observation.directed).length);
+ expect(graph.edges.every(e => e.type === 'similar' && e.weight === 1)).toBe(true);
+ } finally {
+ probe.restore();
+ }
+ });
+});
+
describe.skipIf(!runDb)('memory history and stale-write contract (#10494)', () => {
beforeEach(resetMemories);
diff --git a/server/services/memoryDB.js b/server/services/memoryDB.js
index 6ff2485ac6..1fe6396536 100644
--- a/server/services/memoryDB.js
+++ b/server/services/memoryDB.js
@@ -11,6 +11,7 @@ import { ServerError } from '../lib/errorHandler.js';
import { PERSISTENT_MIND_MEMORY_PROTECTION_TAGS } from '../lib/persistentMindMemory.js';
import { v4 as uuidv4 } from '../lib/uuid.js';
import { query, withTransaction, pgvectorToArray, arrayToPgvector } from '../lib/db.js';
+import { UUID_RE } from '../lib/fileCore.js';
import { cosEvents } from './cosEvents.js';
import * as notifications from './notifications.js';
import { DEFAULT_MEMORY_CONFIG, generateSummary, decrementAgentPendingApproval } from './memoryConfig.js';
@@ -789,10 +790,31 @@ export async function getRelatedMemories(id, limit = 10) {
return related.slice(0, limit);
}
+// Optional caller scope for getGraphData: absent (undefined/null) means the
+// whole CoS graph; an array — even an empty one — restricts output to edges
+// whose BOTH endpoints are in it. A non-UUID id can never name a memory row, so
+// dropping it before the ::uuid[] cast changes no result and avoids a cast error.
+function graphScope(sourceIds) {
+ if (sourceIds === undefined || sourceIds === null) return null;
+ if (!Array.isArray(sourceIds)) throw new TypeError('getGraphData: sourceIds must be an array');
+ return [...new Set(sourceIds.filter(id => typeof id === 'string' && UUID_RE.test(id)))];
+}
+
/**
- * Get graph data for visualization
+ * Get graph data for visualization.
+ *
+ * `sourceIds` (internal, optional) scopes the read to the records a caller can
+ * actually display (#10953): nodes and explicit links are restricted to the
+ * scope, and only scoped memories are scored as similarity SOURCES. The target
+ * population, the global top-three cutoff, the threshold and the active-record
+ * rules are unchanged, so an out-of-scope nearest neighbour still occupies its
+ * slot — the scoped edges are exactly the full graph's edges with both
+ * endpoints in scope. Omitted, the full graph is returned unchanged.
*/
-export async function getGraphData() {
+export async function getGraphData({ sourceIds } = {}) {
+ const scope = graphScope(sourceIds);
+ if (scope && scope.length === 0) return { nodes: [], edges: [] };
+ const scopeParams = scope ? [scope] : [];
return withTransaction(async (client) => {
// Admission and all graph reads must share a snapshot: a concurrent insert
// cannot grow the optimized projection beyond the node count checked here.
@@ -800,8 +822,8 @@ export async function getGraphData() {
// Build nodes
const nodesResult = await client.query(`
SELECT id, type, category, summary, importance
- FROM memories WHERE status = 'active'
- `);
+ FROM memories WHERE status = 'active'${scope ? ' AND id = ANY($1::uuid[])' : ''}
+ `, scopeParams);
const nodes = nodesResult.rows.map(r => ({
id: r.id,
@@ -811,6 +833,12 @@ export async function getGraphData() {
importance: r.importance
}));
+ // The materialization bound counts EVERY active record, scoped or not: the
+ // CTE below still projects the whole active embedding population.
+ const activeCount = scope
+ ? (await client.query("SELECT count(*)::int AS count FROM memories WHERE status = 'active'")).rows[0].count
+ : nodes.length;
+
// Build edges from explicit links
// A symmetric 'related' pair is stored twice; keep one row per pair. Directed
// types are one row each and keep their source -> target orientation.
@@ -819,8 +847,9 @@ export async function getGraphData() {
FROM memory_links ml
JOIN memories ms ON ms.id = ml.source_id AND ms.status = 'active'
JOIN memories mt ON mt.id = ml.target_id AND mt.status = 'active'
- WHERE ml.link_type <> 'related' OR ml.source_id < ml.target_id
- `);
+ WHERE (ml.link_type <> 'related' OR ml.source_id < ml.target_id)
+ ${scope ? 'AND ml.source_id = ANY($1::uuid[]) AND ml.target_id = ANY($1::uuid[])' : ''}
+ `, scopeParams);
const edges = linksResult.rows.map(r => ({
source: r.source,
@@ -835,11 +864,15 @@ export async function getGraphData() {
// unpack each stored vector once, avoiding repeated TOAST reads per candidate.
// A bare materialized embedding retains its external storage pointer; the
// real[] round trip forces an inline vector without changing its float values.
- const materialize = nodes.length <= 2048;
+ const materialize = activeCount <= 2048;
if (materialize) await client.query("SET LOCAL work_mem = '32MB'");
const relation = materialize ? 'active_embeddings' : 'memories';
const seenEdges = new Set(edges.map(e => [e.source, e.target].sort().join('-')));
+ // A scope restricts the OUTER sources before the lateral loop (so only M
+ // sources are scored against all N candidates) and filters targets only
+ // AFTER the LIMIT 3 — filtering candidates first would promote a fourth-
+ // ranked in-scope memory into a slot an out-of-scope neighbour holds.
const simResult = await client.query(`
${materialize ? `WITH active_embeddings AS MATERIALIZED (
SELECT id, embedding::real[]::vector AS embedding
@@ -858,7 +891,8 @@ export async function getGraphData() {
) b
WHERE 1 - (a.embedding <=> b.embedding) >= 0.8
${materialize ? '' : "AND a.embedding IS NOT NULL AND a.status = 'active'"}
- `);
+ ${scope ? 'AND a.id = ANY($1::uuid[]) AND b.id = ANY($1::uuid[])' : ''}
+ `, scopeParams);
for (const row of simResult.rows) {
const edgeKey = [row.source_id, row.target_id].sort().join('-');