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

fix([issue-12601]): skip similar-memory lookup when links fill the limit

[._.]/ Adam Eivy521b0f8647a2
diff --git a/server/services/memoryDB.db.test.js b/server/services/memoryDB.db.test.js
index 85e05d96bc..922a6d5d91 100644
--- a/server/services/memoryDB.db.test.js
+++ b/server/services/memoryDB.db.test.js
@@ -605,6 +605,25 @@ describe.skipIf(!runDb)('memoryDB search (#3447)', () => {
// A record that isn't in the table has no relations (and doesn't throw).
expect(await memoryDB.getRelatedMemories('00000000-0000-4000-8000-00000000dead')).toEqual([]);
});
+
+ it('resolves when explicit links exceed the limit, and still fills a larger limit with neighbours', async () => {
+ // 11 explicit links from one embedded hub: more than the default limit of 10,
+ // which used to make the similarity query's LIMIT negative and throw.
+ const hub = await memoryDB.createMemory({ type: 'fact', content: 'Hub memory with many explicit links.' }, VEC_FAR);
+ for (let i = 0; i < 11; i++) {
+ const spoke = await memoryDB.createMemory({ type: 'fact', content: `Spoke ${i} linked to the hub.` }, VEC_UNSEEN);
+ await memoryDB.linkMemories(hub.id, spoke.id);
+ }
+
+ const capped = await memoryDB.getRelatedMemories(hub.id, 10);
+ 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);
+ });
});
// ---------------------------------------------------------------------------
diff --git a/server/services/memoryDB.js b/server/services/memoryDB.js
index 1a1676d4a2..4a248f0337 100644
--- a/server/services/memoryDB.js
+++ b/server/services/memoryDB.js
@@ -834,11 +834,12 @@ export async function getRelatedMemories(id, limit = 10) {
});
}
- // Get similar by embedding
+ // Get similar by embedding. Explicit links can already fill or exceed the
+ // limit; Postgres rejects a negative LIMIT, so skip the similarity query then.
const embedding = existing.rows[0].embedding;
- if (embedding) {
+ const remaining = limit - related.length;
+ if (embedding && remaining > 0) {
const seenIds = new Set([id, ...related.map(r => r.id)]);
- const remaining = limit - related.length;
const similar = await query(`
SELECT id, type, category, tags, summary, importance, created_at, status, source_app_id,