Skip to content

fix(backend): push the completed filter server-side in the visible action-item ID scan, and meter it - #11849

Merged
Git-on-my-level merged 2 commits into
mainfrom
fix/action-items-visible-ids-scan
Aug 19, 2026
Merged

Git-on-my-level merged 2 commits into
mainfrom
fix/action-items-visible-ids-scan

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

What

get_visible_action_item_ids() (backend/database/action_items.py) backs
GET /v1/action-items/ids?completed=... and used to stream the entire
per-user action_items collection with a bare projection query, filtering
completed in Python. A projection query still bills one document read per
document 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 was
invisible in backend/database/firestore_read_metrics.py, unlike every
other reviewed read path in the module.

This PR:

  1. Pushes the completed filter server-side with
    .where(filter=FieldFilter('completed', '==', completed)), so Firestore
    only streams (and bills) documents in the requested bucket instead of the
    whole collection.
  2. Adds a record_firestore_read call with a new
    FirestoreReadFamily.ACTION_ITEMS_VISIBLE_IDS family, so this path is
    finally counted.
  3. Drops status from the field projection — it is fetched but never read
    in the function body.

What this does NOT change: the deleted semantics

The trap in this change is deleted. It is tempting to also push
.where('deleted', '==', False) server-side. Don't — Firestore equality
filters never match a document where the field is absent, and most action
items have no deleted field at all (it is only ever set on soft-deleted
rows). A server-side deleted filter would silently drop every ordinary,
undeleted row from every bucket. The if data.get('deleted'): continue
check stays in Python, and the diff adds a comment on the function saying
why.

Does the completed semantics shift?

Current code matched with completed_value is completed — Python identity
against a bool, so a doc with completed absent/None or any non-bool
value (e.g. a stray 1/0) never matched either bucket, since is
requires the exact True/False singleton, not just equality. Firestore's
== True / == False also never matches a missing field, and Firestore
equality does not coerce an int 1/0 into a bool match either. So the
server-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_deleted
still 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 goes
to GET /v1/action-items (the listing endpoint), a different code path
already 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.py already covered
get_visible_action_item_ids; extended rather than duplicated:

  • Updated the fake Firestore query fixture (_Query) to implement real
    equality-filter semantics for .where() — a filter only matches a
    document where the field is present and matches by both value and type
    (so a stray int 1/0 is never treated as True/False), same as real
    Firestore. 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'] (not
    status) and that the completed filter was pushed to Firestore.
  • test_visible_action_item_ids_excludes_legacy_null_completion_rows:
    unchanged behavior, kept as a regression pin.
  • New test_visible_action_item_ids_includes_docs_with_absent_deleted_field:
    the trap case — rows with no deleted field, and rows with an explicit
    deleted: False, must both still come back; only deleted: True is
    excluded, and no deleted filter reaches Firestore.
  • New test_visible_action_item_ids_records_firestore_read: asserts the
    omi_firestore_read_operations_total{family="action_items_visible_ids", mode="unbounded"} counter increments by exactly one per call.

Ran locally via backend/test.sh with a scoped file list
(BACKEND_UNIT_TEST_FILE_LIST) covering the touched test file plus the
neighboring action-item pagination and Firestore-read-cache suites — all
pass.

Mutation testing

Four mutations against database/action_items.py, each turned red by an
existing or new test, then reverted:

Mutation Caught by
Remove the .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
Add the forbidden .where('deleted', '==', False) server-side filter test_visible_action_item_ids_matches_explicit_bucket_and_excludes_deleted, test_visible_action_item_ids_includes_docs_with_absent_deleted_field
Remove the Python if data.get('deleted'): continue guard test_visible_action_item_ids_matches_explicit_bucket_and_excludes_deleted, test_visible_action_item_ids_includes_docs_with_absent_deleted_field
Remove the record_firestore_read(...) call test_visible_action_item_ids_records_firestore_read

Product invariants affected

None — scripts/pr-preflight --suggest reports no locked invariant paths
touched by this diff.

Failure class

scripts/pr-preflight --suggest confirms this is a fix: commit
requiring a declaration. I checked .github/failure-classes/ for a
genuine match: the closest candidates —
FC-bounded-read-exceeds-request-budget (about per-query limits composing
against 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 collection
into a provider/LLM prompt, not a Firestore billing concern), and
FC-unthrottled-read-revalidation (about a client's revalidation cadence
lacking a throttle; that class already exists and its
canonical_prevention_artifact targets desktop/windows — this PR is the
backend-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 new isn't warranted either: this is a narrow, already-tested
query and instrumentation fix, not a recurring class worth a permanent
registry entry on its own evidence.

Failure-Class: none

Review in cubic

…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.
@mintlify

mintlify Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
omi 🟢 Ready View Preview Aug 19, 2026, 6:04 AM

💡 Tip: Enable Workflows to automatically generate PRs for you.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

1 active deployment
staging - docs — d8b845a7 Deployed Aug 19, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants