[P1] Add Memory Links Cleanup #11

Closed
opened 2025-12-05 13:49:13 +00:00 by blightbow · 1 comment
Owner

Problem

  • memory_links dictionary grows indefinitely
  • Never cleaned when Mem0 entries are deleted
  • consolidated_entry_ids set only grows, orphaned IDs never pruned

Suggested Fix

Prune orphaned links when Mem0 entries are deleted.

Priority

P1 — High Priority

Source

Architecture Audit 2025-12-03, Section 2: Memory Leak Potential

## Problem - `memory_links` dictionary grows indefinitely - Never cleaned when Mem0 entries are deleted - `consolidated_entry_ids` set only grows, orphaned IDs never pruned ## Suggested Fix Prune orphaned links when Mem0 entries are deleted. ## Priority **P1 — High Priority** ## Source Architecture Audit 2025-12-03, Section 2: Memory Leak Potential
Author
Owner

Resolution Summary

Implemented two cleanup mechanisms to prevent unbounded growth of memory links and consolidated entry IDs during the dreaming phase:

Added prune_orphaned_memory_links() function that:

  • Gets all current memory IDs from Mem0 via memory_client.get_all()
  • Removes memory_links entries whose keys reference deleted memories
  • Cleans linked_to arrays by removing references to deleted memories
  • Called during dreaming phase after run_dreaming_tick()

2. Consolidated Entry IDs Cleanup (helpers.py)

Enhanced prune_low_importance_entries() to:

  • Track which journal entries are being pruned
  • Remove pruned entry IDs from consolidated_entry_ids set
  • Prevents the consolidated set from growing with stale references

Tests Added

11 new tests in test_memory_link_cleanup.py:

  • Skip behavior when memory client unavailable
  • Empty memory_links handling
  • Orphaned key removal
  • linked_to array cleanup
  • Combined cleanup scenarios
  • Error handling for Mem0 failures
  • consolidated_entry_ids synchronization

Commit: b412b8156

## Resolution Summary Implemented two cleanup mechanisms to prevent unbounded growth of memory links and consolidated entry IDs during the dreaming phase: ### 1. Orphaned Memory Links Cleanup (`rag_memory.py`) Added `prune_orphaned_memory_links()` function that: - Gets all current memory IDs from Mem0 via `memory_client.get_all()` - Removes `memory_links` entries whose keys reference deleted memories - Cleans `linked_to` arrays by removing references to deleted memories - Called during dreaming phase after `run_dreaming_tick()` ### 2. Consolidated Entry IDs Cleanup (`helpers.py`) Enhanced `prune_low_importance_entries()` to: - Track which journal entries are being pruned - Remove pruned entry IDs from `consolidated_entry_ids` set - Prevents the consolidated set from growing with stale references ### Tests Added 11 new tests in `test_memory_link_cleanup.py`: - Skip behavior when memory client unavailable - Empty memory_links handling - Orphaned key removal - linked_to array cleanup - Combined cleanup scenarios - Error handling for Mem0 failures - consolidated_entry_ids synchronization **Commit:** b412b8156
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
blightbow/evennia_ai#11
No description provided.