Skip to content

fix(backend): call get_firestore_client() in typesense dual-write resolver - #15107

Merged
Git-on-my-level merged 2 commits into
BasedHardware:mainfrom
Git-on-my-level:fix/typesense-client-resolution
Sep 20, 2026
Merged

Git-on-my-level merged 2 commits into
BasedHardware:mainfrom
Git-on-my-level:fix/typesense-client-resolution

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Sep 20, 2026 •

Copy link
Copy Markdown
Member

Root cause

_resolve_firestore_client() in backend/utils/conversations/typesense_index.py imported the memoized factory get_firestore_client and returned the function itself instead of calling it. Every first-party Typesense dual-write that reached the projection read then died with 'function' object has no attribute 'collection', which the module's fail-open contract swallowed — visible in production as 500+ omi_fallback_event … to=fail_open entries per 24h in backend logs (serving SHA 7d8a0093e0f7, still on origin/main). A live read-only coverage gate over 30 users / 173 conversations confirmed the consequence: 171 conversations were matched in the index via the Firebase Typesense extension only, and 2 were missing entirely — the first-party writer contributed nothing.

Failure class (fixes)

Failure-Class: none

Fix

One line (plus a comment): call the factory.

 def _resolve_firestore_client() -> Any:
-    from database._client import get_firestore_client as client
+    from database._client import get_firestore_client
-    return client
+    return get_firestore_client()

Sibling check: _resolve_firestore_client has exactly one caller — _fetch_index_projection, which immediately does client.collection(...) — and no other module imports the resolver, so the one-liner is sufficient. The sibling _resolve_default_db_client returns data_plane_db, which is already a lazy proxy object (not a factory), so it is correct as-is and untouched. The fail-open contract is deliberately unchanged here; making sync failures louder than a swallowed fallback deserves its own design pass (follow-up), since it trades search freshness signal against write-path availability.

Tests

Extends the module's existing suite backend/tests/unit/test_conversation_typesense_index.py (existing tests always injected firestore_client, which is why the broken default seam stayed green):

  • test_resolver_calls_the_factory_and_returns_its_client — monkeypatches database._client.get_firestore_client with a sentinel-returning fake; asserts the sentinel is returned (i.e. the factory is actually called, once).
  • test_default_path_sync_reads_firestore_through_the_factory — end-to-end default-path regression: syncs with no injected firestore_client; asserts the document is upserted. On the unfixed resolver this test fails (False is True), reproducing the swallowed production failure.
  • test_typesense_outage_does_not_raise_out_of_write_paths now also fakes the projection-read client, matching the test's other I/O seams: pre-fix, its sync died at the broken resolver before ever reaching the Typesense outage, so this very bug had silently narrowed that test's coverage; post-fix it exercises the real outage path.

Both new tests were verified to fail against the pre-fix resolver and pass after the fix. Full file via backend/test.sh (file-isolated, duration guard on): 55 passed.

Rollout note

This unblocks the Typesense extension retirement: after merge + deploy, re-run the saved coverage gate (30 users / 173 conversations) and confirm first-party writes close the gap before removing the Eventarc trigger. Rollback = revert this PR.

Review in cubic

@Git-on-my-level
Git-on-my-level marked this pull request as ready for review September 20, 2026 14:42
@Git-on-my-level

Copy link
Copy Markdown
Member Author

Coordinator: rebasing onto latest main after 'base branch was modified' on merge.

@Git-on-my-level
Git-on-my-level merged commit c912575 into BasedHardware:main Sep 20, 2026
39 checks passed
Git-on-my-level added a commit that referenced this pull request Sep 21, 2026
## Root cause

Prod first-party Typesense dual-write (`typesense_index.py`) started
running after #15107, then fail-opened on every upsert whose
`structured` blob still carried Firestore `DatetimeWithNanoseconds`
(`Object of type DatetimeWithNanoseconds is not JSON serializable`). 37
of those in 2h on serving `16fc207`. Search stayed on the Firebase
extension.

## Failure class (fixes)

Failure-Class: none

## Fix

Sanitize `structured` (and nested lists/dicts) to JSON-safe values
before upsert: datetimes → unix seconds, GeoPoints → `[lat, lng]`.
Top-level timestamps were already epoch-coerced; nested
`structured.events.*.created_at` was not.

## Test

`pytest
tests/unit/test_conversation_typesense_index.py::TestDocumentShape` — 8
passed, including
`test_structured_firestore_datetime_is_json_serializable` (`json.dumps`
of a DatetimeWithNanoseconds subclass).

## Rollout

Does not uninstall the Typesense extension. After merge+prod deploy,
re-run the C018 coverage gate; remove the Eventarc trigger only if that
PASSes.


<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/BasedHardware/omi/pull/15328?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
This was referenced Sep 24, 2026
This was referenced Sep 24, 2026
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.

1 participant