Repository navigation
fix(backend): push the completed filter server-side in the visible action-item ID scan, and meter it - #11849
Merged
Conversation
…tion-item ID scan, and meter it
get_visible_action_item_ids() streamed the entire per-user action_items
collection and filtered `completed` in Python; a projection query still
bills one document read per doc scanned. Push `completed` server-side via
FieldFilter so Firestore only streams (and bills) the requested bucket.
`deleted` is deliberately NOT pushed into the query: Firestore equality
never matches a document where the field is absent, and most rows have no
`deleted` field at all, so a server-side `.where('deleted', '==', False)`
would silently drop every undeleted row. That check stays in Python.
`status` was in the projection but never read in the function body, so it
is dropped.
Also adds the record_firestore_read() call this path was missing, so it
is finally visible in the Firestore read metrics like every other
reviewed read path in this module.
|
Preview deployment for your docs. Learn more about Mintlify Previews.
💡 Tip: Enable Workflows to automatically generate PRs for you. |
kodjima33
approved these changes
Aug 19, 2026
kodjima33
left a comment
Collaborator
There was a problem hiding this comment.
Server-side completed filter + read metering on the visible action-item ID scan; approving (cost/perf change rather than a user-facing defect, so not auto-merged).
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
get_visible_action_item_ids()(backend/database/action_items.py) backsGET /v1/action-items/ids?completed=...and used to stream the entireper-user
action_itemscollection with a bare projection query, filteringcompletedin Python. A projection query still bills one document read perdocument scanned, so every call cost one read per action item the user has
ever created, regardless of how many were actually in the requested bucket.
The function also never called
record_firestore_read, so this scan wasinvisible in
backend/database/firestore_read_metrics.py, unlike everyother reviewed read path in the module.
This PR:
completedfilter server-side with.where(filter=FieldFilter('completed', '==', completed)), so Firestoreonly streams (and bills) documents in the requested bucket instead of the
whole collection.
record_firestore_readcall with a newFirestoreReadFamily.ACTION_ITEMS_VISIBLE_IDSfamily, so this path isfinally counted.
statusfrom the field projection — it is fetched but never readin the function body.
What this does NOT change: the
deletedsemanticsThe trap in this change is
deleted. It is tempting to also push.where('deleted', '==', False)server-side. Don't — Firestore equalityfilters never match a document where the field is absent, and most action
items have no
deletedfield at all (it is only ever set on soft-deletedrows). A server-side
deletedfilter would silently drop every ordinary,undeleted row from every bucket. The
if data.get('deleted'): continuecheck stays in Python, and the diff adds a comment on the function saying
why.
Does the
completedsemantics shift?Current code matched with
completed_value is completed— Python identityagainst a bool, so a doc with
completedabsent/Noneor any non-boolvalue (e.g. a stray
1/0) never matched either bucket, sinceisrequires the exact
True/Falsesingleton, not just equality. Firestore's== True/== Falsealso never matches a missing field, and Firestoreequality does not coerce an int
1/0into a bool match either. So theserver-side filter returns exactly the same document set the old Python
identity check selected, and that check is now redundant and was removed.
The new unit test
test_visible_action_item_ids_matches_explicit_bucket_and_excludes_deletedstill exercises the same legacy-row fixtures (
completed: None,status-only rows) to pin this.
Value — please read this section literally
This is a correctness and instrumentation fix, not a measured cost win.
PR #11835's follow-up notes attributed a large share of the Windows app's
backend request volume to this function, estimating "24 full collection
scans per idle hour per running client." Production log sampling on
2026-08-19 found that attribution is wrong: there is effectively zero
traffic to
GET /v1/action-items/ids. The ~60k req/hr Windows volume goesto
GET /v1/action-items(the listing endpoint), a different code pathalready addressed by #11830. This PR does not claim to reduce measured
load.
The reason to land it anyway: the scan this function runs is unbounded per
call and, until this PR, completely unmetered. It is a latent cliff — cost
grows linearly with each user's lifetime action-item count — that we
currently cannot see coming because nothing records it. Fixing the query
shape and adding the metric closes both problems at once, independent of
today's traffic level.
Testing
backend/tests/unit/test_action_item_ids_endpoint.pyalready coveredget_visible_action_item_ids; extended rather than duplicated:_Query) to implement realequality-filter semantics for
.where()— a filter only matches adocument where the field is present and matches by both value and type
(so a stray
int1/0 is never treated asTrue/False), same as realFirestore. This lets the tests actually distinguish server-side filtering
from the old Python-side filtering it replaces.
test_visible_action_item_ids_matches_explicit_bucket_and_excludes_deleted:now also asserts the projection is
['completed', 'deleted'](notstatus) and that thecompletedfilter was pushed to Firestore.test_visible_action_item_ids_excludes_legacy_null_completion_rows:unchanged behavior, kept as a regression pin.
test_visible_action_item_ids_includes_docs_with_absent_deleted_field:the trap case — rows with no
deletedfield, and rows with an explicitdeleted: False, must both still come back; onlydeleted: Trueisexcluded, and no
deletedfilter reaches Firestore.test_visible_action_item_ids_records_firestore_read: asserts theomi_firestore_read_operations_total{family="action_items_visible_ids", mode="unbounded"}counter increments by exactly one per call.Ran locally via
backend/test.shwith a scoped file list(
BACKEND_UNIT_TEST_FILE_LIST) covering the touched test file plus theneighboring action-item pagination and Firestore-read-cache suites — all
pass.
Mutation testing
Four mutations against
database/action_items.py, each turned red by anexisting or new test, then reverted:
.where(filter=FieldFilter('completed', ...))push-down (revert to scanning the whole collection)test_visible_action_item_ids_matches_explicit_bucket_and_excludes_deleted,test_visible_action_item_ids_excludes_legacy_null_completion_rows,test_visible_action_item_ids_includes_docs_with_absent_deleted_field.where('deleted', '==', False)server-side filtertest_visible_action_item_ids_matches_explicit_bucket_and_excludes_deleted,test_visible_action_item_ids_includes_docs_with_absent_deleted_fieldif data.get('deleted'): continueguardtest_visible_action_item_ids_matches_explicit_bucket_and_excludes_deleted,test_visible_action_item_ids_includes_docs_with_absent_deleted_fieldrecord_firestore_read(...)calltest_visible_action_item_ids_records_firestore_readProduct invariants affected
None —
scripts/pr-preflight --suggestreports no locked invariant pathstouched by this diff.
Failure class
scripts/pr-preflight --suggestconfirms this is afix:commitrequiring a declaration. I checked
.github/failure-classes/for agenuine match: the closest candidates —
FC-bounded-read-exceeds-request-budget(about per-query limits composingagainst a request-scoped middleware budget, not applicable here — there's
no request budget in play, just an unbounded per-user scan),
FC-unbounded-user-collection-in-prompt(about rendering a full collectioninto a provider/LLM prompt, not a Firestore billing concern), and
FC-unthrottled-read-revalidation(about a client's revalidation cadencelacking a throttle; that class already exists and its
canonical_prevention_artifacttargetsdesktop/windows— this PR is thebackend-side query fix, not the client cadence fix, and doesn't produce a
new reusable guard surface) — none genuinely describe this specific
defect (a per-user Firestore scan filtering in Python instead of pushing
an equality predicate server-side, paired with a missing metering call).
Declaring
newisn't warranted either: this is a narrow, already-testedquery and instrumentation fix, not a recurring class worth a permanent
registry entry on its own evidence.
Failure-Class: none