Skip to content
This repository was archived by the owner on Apr 4, 2026. It is now read-only.

perf: optimize Cognee memory system for faster recall and dashboard#1

Merged
Nafania merged 16 commits into
mainfrom
perf/cognee-memory-optimization
Mar 16, 2026
Merged

perf: optimize Cognee memory system for faster recall and dashboard#1
Nafania merged 16 commits into
mainfrom
perf/cognee-memory-optimization

Conversation

@Nafania
Copy link
Copy Markdown
Owner

@Nafania Nafania commented Mar 16, 2026

Summary

  • Parallel search: _multi_cognee_search (recall extension) and Memory._multi_search now run all search types concurrently via asyncio.gather with per-type timeouts (15s), reducing recall latency from ~30s to ~5-10s
  • Startup initialization: Moved Cognee DB setup and module imports from runtime (every search call) to application startup in prepare.py, eliminating redundant initialization overhead
  • Dashboard caching + pagination: Added in-memory cache with 60s TTL for memory listings and true server-side pagination, making dashboard loads near-instant after first hit
  • O(1) bulk delete: Optimized delete_documents_by_ids from O(N×areas) individual lookups to O(areas) with single scan per dataset using set-based ID matching

Test plan

  • All 25 dashboard tests pass (including 11 restored pre-existing tests)
  • Parallel search tests verify concurrent execution and timeout handling
  • Startup initialization tests verify one-time setup and error on missing init
  • Bulk delete tests verify single-scan optimization and partial match handling
  • Full test suite passes (pytest — 37 tests)

Made with Cursor

Nafania added 16 commits March 16, 2026 11:27
Addresses 30+ second recall delays during chat with 500+ memories.
Covers parallel search, startup init, dashboard caching, and O(1) delete.

Made-with: Cursor
6 tasks: parallel search, startup init, dashboard cache, bulk delete.
TDD approach with bite-sized steps.

Made-with: Cursor
Replace sequential for-loop in Memory._multi_search with
asyncio.gather for concurrent execution. Add SEARCH_TIMEOUT=15
class attribute with per-type asyncio.wait_for protection.

Made-with: Cursor
Cache formatted memories in-memory for 60s, keyed by (subdir, area).
Pagination slices from cache instead of loading all items from disk.
Cache is invalidated on insert/delete/update via memory.py hooks.

Made-with: Cursor
…tion

- Restore 20 pre-existing tests for helper methods (_read_data_item_content,
  _format_memory_for_dashboard, _get_knowledge_graph, _get_cognify_status,
  _get_memory_subdirs, _get_current_memory_subdir, _search_memories offset,
  _delete_memory, _update_memory, setup-before-action) that were removed
  during Task 4 cache implementation
- Update TestDashboardCallsSetup tests to use current cognee_init module vars
  instead of obsolete memory.py module-level vars
- Remove redundant _invalidate_dashboard_cache() call from update_documents
  since both delete_documents_by_ids and insert_documents already invalidate

Made-with: Cursor
Replace loop-per-id approach in delete_documents_by_ids with a single
list_datasets + list_data call per area. For N IDs across 3 areas this
reduces from N*3 dataset scans to at most 3, with early exit when all
IDs are found.

Made-with: Cursor
Reflect startup init, parallel search, dashboard cache, and bulk delete
optimizations in architecture docs and fork changes list.

Made-with: Cursor
beautifulsoup4>=4.13 breaks transformers import via bs4.__spec__=None.
Regression tests updated to test new startup-init architecture instead
of the removed runtime configure_cognee() pattern.

Made-with: Cursor
Sequential pip install lets cognee pull in beautifulsoup4>=4.13 which
overrides the <4.13 pin from requirements.txt. A single pip install
with all requirement files resolves constraints together.

Made-with: Cursor
Single pip install causes openai version conflict between browser-use
and requirements2.txt. Use sequential installs with a shared constraint
file to pin beautifulsoup4<4.13 across all install steps.

Made-with: Cursor
Docker uses uv which doesn't upgrade already-installed packages on
subsequent installs. Regular pip does, causing beautifulsoup4 to get
upgraded to 4.13+ (broken __spec__) when cognee is installed.

Made-with: Cursor
Production uses uv pip install inside a venv. CI was using plain pip
in system Python, causing bs4.__spec__=None (venv initializes it
correctly). Now CI mirrors Docker: uv + venv, sequential installs.

Made-with: Cursor
beautifulsoup4 4.13+ has bs4.__spec__=None which causes transformers'
import_utils.py to crash (huggingface/transformers#31798). Docker
worked because older cached version was installed; fresh installs get
4.14.3 which is broken. Pin <4.13 in requirements.txt.

Made-with: Cursor
Root cause: conftest.py unconditionally injected _MockModule into
sys.modules for ALL optional deps (including bs4, flask, requests).
_MockModule lacks a proper __spec__, so importlib.util.find_spec()
raised ValueError when transformers checked for bs4 availability.

Fix: check importlib.util.find_spec(top_level) before mocking.
In CI (all deps installed) nothing gets mocked; locally (partial
deps) only genuinely missing packages are stubbed.

Also reverts the beautifulsoup4<4.13 pin — no longer needed.

Made-with: Cursor
@Nafania Nafania merged commit e9d0a2c into main Mar 16, 2026
2 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant