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: exclude linked ids in SQL so related memories fill the limit (#12768)

[._.]/ Adam Eivy8f711fa22237
diff --git a/server/services/memoryDB.js b/server/services/memoryDB.js
index 4a248f0337..11595624b9 100644
--- a/server/services/memoryDB.js
+++ b/server/services/memoryDB.js
@@ -839,19 +839,21 @@ export async function getRelatedMemories(id, limit = 10) {
const embedding = existing.rows[0].embedding;
const remaining = limit - related.length;
if (embedding && remaining > 0) {
- const seenIds = new Set([id, ...related.map(r => r.id)]);
+ // Exclude already-linked ids in SQL (not after the query) so linked
+ // memories ranking among the nearest neighbours can't under-fill (#12768).
+ const linkedIds = related.map(r => r.id);
const similar = await query(`
SELECT id, type, category, tags, summary, importance, created_at, status, source_app_id,
1 - (embedding <=> $1) AS similarity
FROM memories
WHERE id != $2 AND embedding IS NOT NULL AND status = 'active'
+ AND NOT (id = ANY($4::uuid[]))
ORDER BY embedding <=> $1
LIMIT $3
- `, [embedding, id, remaining]);
+ `, [embedding, id, remaining, linkedIds]);
for (const row of similar.rows) {
- if (seenIds.has(row.id)) continue;
related.push({
...rowToMeta(row),
relationship: 'similar',