You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
recent_activity hydrate_context has N+1 entity lookup pattern #710
recent_activity is called frequently (112 times in the last 12h) and its shape_response phase has the same N+1 entity lookup pattern as search hydration (issue #707).
Inside shape_response, memory.hydrate_context.lookup_entities fetches entities one-by-one, paying Neon network latency per lookup. This averages 0.3s but spikes to 1.8s for larger result sets.
In all cases, lookup_entities dominates the phase.
Proposed Fix
Same approach as #707: collect all entity IDs/permalinks needed for hydration upfront, then batch-fetch in a single find_by_ids query instead of one query per entity.
Impact
recent_activity is one of the most-called endpoints (112 calls/12h). Batching the entity lookup would save ~0.3s avg per call and eliminate the 1.8s worst-case spikes.
Related: #707 (same N+1 pattern in search hydration)
Analysis:to_graph_context (used by recent_activity) already has the batch find_by_ids fix in place on main. The remaining N+1 issue is in to_search_results (used by search — related #707), which also had a pre-existing ordering bug where from_entity and to_entity both incorrectly pointed to entities[0] due to unordered IN-query results.
Analyze issue and classify
Read relevant source files
Verified to_graph_context (recent_activity) already uses batch find_by_ids — no change needed there
Fixed to_search_results N+1: collect all unique IDs (entity_id, from_id, to_id) upfront, batch-fetch in one query, use lookup dict — also fixes the pre-existing from_entity/to_entity ordering bug
Updated test_utils_telemetry.py: mock returns entities with .id, asserts from_entity/to_entity are now correctly resolved
Committed and pushed
Changes:
src/basic_memory/api/v2/utils.py — replaced N+1 loop with single batch find_by_ids call
tests/api/v2/test_utils_telemetry.py — updated mock and added assertions for correct from_entity/to_entity resolution
Checked this on current main: the recent_activity / to_graph_context() hydration path is already batching entity lookups with a single find_by_ids(...) call, so there was no additional runtime fix needed here.
I opened PR #716 to add regression coverage for that path so we keep the behavior locked in:
mixed entity/observation/relation hydration stays at one batched lookup
Activity
claude commentedon Apr 1, 2026
Claude finished @phernandez's task in 7m 17s —— View job •
claude/issue-710-20260401-2152• Create PR ➔Triage & Implementation
Triage:
Analysis:
to_graph_context(used byrecent_activity) already has the batchfind_by_idsfix in place onmain. The remaining N+1 issue is into_search_results(used by search — related #707), which also had a pre-existing ordering bug wherefrom_entityandto_entityboth incorrectly pointed toentities[0]due to unorderedIN-query results.to_graph_context(recent_activity) already uses batchfind_by_ids— no change needed thereto_search_resultsN+1: collect all unique IDs (entity_id, from_id, to_id) upfront, batch-fetch in one query, use lookup dict — also fixes the pre-existingfrom_entity/to_entityordering bugtest_utils_telemetry.py: mock returns entities with.id, assertsfrom_entity/to_entityare now correctly resolvedChanges:
src/basic_memory/api/v2/utils.py— replaced N+1 loop with single batchfind_by_idscalltests/api/v2/test_utils_telemetry.py— updated mock and added assertions for correctfrom_entity/to_entityresolution|
perf: batch entity lookup in to_search_results to fix N+1 queries
phernandez commentedon Apr 3, 2026
Checked this on current
main: therecent_activity/to_graph_context()hydration path is already batching entity lookups with a singlefind_by_ids(...)call, so there was no additional runtime fix needed here.I opened PR #716 to add regression coverage for that path so we keep the behavior locked in:
PR: #716