Repository navigation
SCA-458: cut Firestore memory unbounded queries and NOT_FOUND misses - #13233
Git-on-my-level merged 5 commits into
Conversation
Skip historical-override batch reads when the canonical item is present and valid, hydrate chat search by candidate and lineage ids, and keep dual-store daily counts as an ID union. Label the QUERY collections that were collapsing into other. Failure-Class: none Co-authored-by: Cursor <[email protected]>
Failure-Class: none Co-authored-by: Cursor <[email protected]>
callable() narrowed the helper to object and pyright rejected .get(). Failure-Class: none Co-authored-by: Cursor <[email protected]>
The suppressed-prefix walk is already grandfathered for the second test in this file; the first test pays the same ~0.5s plus module-reload amortization. Failure-Class: none Co-authored-by: Cursor <[email protected]>
There was a problem hiding this comment.
2 issues found across 13 files
Confidence score: 3/5
backend/utils/memory/canonical_memory_adapter.pycan silently discard vector-only matches when keyword search returns 60 IDs, causing relevant results to be missing after hydration; preserve a bounded but complete merged candidate set or explicitly merge the result sets.backend/tests/unit/test_desktop_daily_usage.pydoes not cover the legacy-only usage path when the canonical store is empty, leaving theif legacy_anycounting behavior less protected; add a focused regression test for that branch.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="backend/utils/memory/canonical_memory_adapter.py">
<violation number="1" location="backend/utils/memory/canonical_memory_adapter.py:1000">
P2: When keyword search returns 60 IDs, vector-only matches are silently dropped before hydration, even though the vector query also returned them. Preserve a bounded but complete merged candidate set, or explicitly merge and rank both provider result sets before applying the cap.</violation>
</file>
<file name="backend/tests/unit/test_desktop_daily_usage.py">
<violation number="1" location="backend/tests/unit/test_desktop_daily_usage.py:155">
P3: The new tests cover the both-stores union and the legacy-empty case, but the symmetric branch where only the legacy store has rows (canonical empty) is untested. That branch has its own logic (`if legacy_any: counted = _aggregation_count(legacy_query)`) and a regression there would ship silently. Add a case with an empty canonical query and non-empty legacy query asserting the aggregation count path, mirroring the existing legacy-empty test.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
|
|
||
| now = datetime.now(timezone.utc) | ||
| policy = MemoryAccessPolicy.for_omi_chat(archive_capability=False) | ||
| hydration = _hydrate_bounded_ledger_search_items( |
There was a problem hiding this comment.
P2: When keyword search returns 60 IDs, vector-only matches are silently dropped before hydration, even though the vector query also returned them. Preserve a bounded but complete merged candidate set, or explicitly merge and rank both provider result sets before applying the cap.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At backend/utils/memory/canonical_memory_adapter.py, line 1000:
<comment>When keyword search returns 60 IDs, vector-only matches are silently dropped before hydration, even though the vector query also returned them. Preserve a bounded but complete merged candidate set, or explicitly merge and rank both provider result sets before applying the cap.</comment>
<file context>
@@ -997,33 +997,23 @@ def search_canonical_memories(
now = datetime.now(timezone.utc)
policy = MemoryAccessPolicy.for_omi_chat(archive_capability=False)
+ hydration = _hydrate_bounded_ledger_search_items(
+ uid,
+ merged_ids,
</file context>
There was a problem hiding this comment.
Skipped. merge_memory_search_ids is pre-existing ranking (keyword first, then vector fill). This PR only changed hydration from a full memory_items stream to merged_ids + lineage IDs. Changing the merge order would be a search-quality change, not a read-cost cut.
|
|
||
| assert result == 3 | ||
|
|
||
| def test_memories_created_uses_sot_count_when_legacy_store_is_empty(): |
There was a problem hiding this comment.
P3: The new tests cover the both-stores union and the legacy-empty case, but the symmetric branch where only the legacy store has rows (canonical empty) is untested. That branch has its own logic (if legacy_any: counted = _aggregation_count(legacy_query)) and a regression there would ship silently. Add a case with an empty canonical query and non-empty legacy query asserting the aggregation count path, mirroring the existing legacy-empty test.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At backend/tests/unit/test_desktop_daily_usage.py, line 155:
<comment>The new tests cover the both-stores union and the legacy-empty case, but the symmetric branch where only the legacy store has rows (canonical empty) is untested. That branch has its own logic (`if legacy_any: counted = _aggregation_count(legacy_query)`) and a regression there would ship silently. Add a case with an empty canonical query and non-empty legacy query asserting the aggregation count path, mirroring the existing legacy-empty test.</comment>
<file context>
@@ -116,9 +149,18 @@ def test_memories_created_counts_canonical_and_legacy_shapes_without_duplicates(
- assert result == 3
+
+def test_memories_created_uses_sot_count_when_legacy_store_is_empty():
+ canonical_query = _CreatedCountQuery(['canonical-only', 'shared'])
+ legacy_query = _CreatedCountQuery([])
</file context>
There was a problem hiding this comment.
Skipped. Dual-store ID union already covers the both-nonempty path; adding the empty-canonical case would not change the count() vs union split. Not blocking the cut.
…llback Cubic P1: delete_batch writes historical suppression before the canonical tombstone, so a live item still needs the override probe. P2: aggregation failure must not restore an unbounded ID stream. Failure-Class: none
Summary
Cuts two production Firestore bleed classes from the 2026-09-08 DATA_READ window (backend SA
nik-164@):NOT_FOUND pair (~20.4M
users/memory_items+ ~20.5Musers/memory_historical_overrides/ day).canonical_statusesno longerget_alls override docs for IDs whose canonical item is present and parseable. Override reads run only for the missing-item subset. Search historical dedup uses that batch instead of per-match_canonical_status. Legacyappend_commitstill probes the override when the item is live (delete_batch writes suppression first).Unbounded memory QUERY on request paths. Chat/product
search_canonical_memorieshydratesmerged_idsplus lineage pointers viafetch_authoritative_product_memory_items_by_idsinstead of a fullmemory_items.stream().count_memories_createdkeeps a date-range ID union when both stores have rows (nocount()+count()double-count of dual-store IDs) and uses aggregationcount()only when the other store is empty.Also labels
users/memories,users/candidates,users/knowledge_nodes, andusers/knowledge_edgesonomi_firestore_document_reads_totalso the QUERY slice is not dumped intoother. Knowledge-graph assertion loads now requirescan_limit(GET already passed 500).Addresses #11282 #9889
SCA-458
Does not close #9889 (broader epic). Does not close #11282:
account_deletionsNOT_FOUND (~4M/day) and other unbounded privacy/deletion scans are intentionally left alone.Product invariants affected
Path-matched (not intent-matched). This PR does not change tier inventory, promotion routes, or fail-open historical admission: present-but-unparseable canonical authority still 503s, and override-only tombstones still suppress.
503 vs suppression contract
Pinned in unit tests:
memory_state/apply_controlis not used as a "no overrides" selector. Historical-only privacy deletes write an override without apply-control.Out of scope (deliberate)
Query.streamcapaccount_deletionsskip / negative cacheTest plan
backend/.venv/bin/python -m pytest backend/tests/unit/test_universal_memory_service.py— triad, skip-override, batched search suppressionbackend/.venv/bin/python -m pytest backend/tests/unit/test_ws_m_atom_keyword_index.py— product search hydrates candidate+lineage without full collection scan; supersede chain still collapses to the survivorbackend/.venv/bin/python -m pytest backend/tests/unit/test_memory_ledger.py— legacy commit skips override when item exists; override-only tombstone still fencedbackend/.venv/bin/python -m pytest backend/tests/unit/test_desktop_daily_usage.py— dual-store ID union still dedupes; SoT-only uses aggregation countbackend/.venv/bin/python -m pytest backend/tests/unit/test_firestore_document_probe.py— new collection patterns stay in the bounded label setbackend/.venv/bin/python -m pytest backend/tests/unit/test_memory_offset_read_cost.py backend/tests/unit/test_memory_graph_assertion_read.py backend/tests/unit/test_memory_visibility_export_fixes.pyFailure-Class
Failure-Class: none
Line-Count-Exception: backend/database/memories.py | 1527 -> 1560 | date-range existence probe plus SoT-only aggregation count without dual-store double-count
Line-Count-Exception: backend/utils/memory/memory_service.py | 4057 -> 4063 | skip override get_all except missing-item subset; batched search suppression