Repository navigation
[codex] Defer Typesense client setup for conversation search - #7662
tianmind-studio wants to merge 8 commits into
Conversation
Greptile SummaryThis PR defers Typesense client construction from module-import time to first use, so importing
Confidence Score: 4/5Safe to merge — the lazy-init change is self-contained, the tests cover the main scenarios, and the identified concerns are non-blocking quality improvements. The core change is straightforward and achieves its goal. The singleton lacks a threading lock (benign in practice but not future-proof), and the broad exception handler silently changes the type of config errors, making them harder to distinguish from runtime search failures. Neither issue causes incorrect behavior today. backend/utils/conversations/search.py — the singleton initialization pattern and exception-wrapping logic are the two spots worth a second look before this pattern is replicated elsewhere. Important Files Changed
Sequence DiagramsequenceDiagram
participant Caller
participant search_conversations
participant _get_typesense_client
participant typesense.Client
Caller->>search_conversations: search_conversations(uid, query)
search_conversations->>_get_typesense_client: _get_typesense_client()
alt _typesense_client is None
_get_typesense_client->>_get_typesense_client: _get_required_env_var(TYPESENSE_HOST)
_get_typesense_client->>_get_typesense_client: _get_required_env_var(TYPESENSE_HOST_PORT)
_get_typesense_client->>_get_typesense_client: _get_required_env_var(TYPESENSE_API_KEY)
note over _get_typesense_client: raises RuntimeError if any var missing
_get_typesense_client->>typesense.Client: Client(config)
typesense.Client-->>_get_typesense_client: client instance
_get_typesense_client->>_get_typesense_client: "_typesense_client = client (cached)"
else _typesense_client already initialized
note over _get_typesense_client: return cached singleton
end
_get_typesense_client-->>search_conversations: client
search_conversations->>typesense.Client: collections['conversations'].documents.search(params)
typesense.Client-->>search_conversations: results
search_conversations-->>Caller: "{items, total_pages, current_page, per_page}"
|
kodjima33
left a comment
There was a problem hiding this comment.
Lazy Typesense client init — backend refactor with tests, approve only per backend policy.
b3a0c51 to
a02fc14
Compare
|
I rebased and force-pushed this branch onto the current |
kodjima33
left a comment
There was a problem hiding this comment.
re-approved on new sha: backend bug fix (lazy Typesense client init + concurrency lock) — approve only
a02fc14 to
3bd28ca
Compare
kodjima33
left a comment
There was a problem hiding this comment.
Re-approved on new sha: lazy Typesense init — has merge conflicts, please rebase
3bd28ca to
361a79c
Compare
kodjima33
left a comment
There was a problem hiding this comment.
Lazy Typesense init w/ tests — re-approved on new sha
d266b5b to
b767d69
Compare
kodjima33
left a comment
There was a problem hiding this comment.
Lazy Typesense init — approve only (re-approve)
b767d69 to
2312c5c
Compare
kodjima33
left a comment
There was a problem hiding this comment.
Re-approved on new commits; approve-only per area policy. (auto-review)
Git-on-my-level
left a comment
There was a problem hiding this comment.
Clean, well-tested improvement. Lazy-initializing the Typesense client with double-checked locking is the right call — it removes an unnecessary import-time dependency on TYPESENSE_* env vars while preserving clear RuntimeError validation on first use. The concurrency test confirming a single shared client across 8 threads is a nice touch, and the Greptile thread-safety feedback was addressed correctly. Both CI checks pass. Thanks @tianmind-studio!
Git-on-my-level
left a comment
There was a problem hiding this comment.
Thanks @tianmind-studio — the lazy-init refactor itself is clean and well-tested. The double-checked locking pattern is correct, and the concurrency test confirming a single shared client across 8 threads is a nice touch.
One gap before this can land: this PR removes the module-level client symbol, but 5 tests in tests/unit/test_lock_bypass_fixes.py (the TestSearchRedaction class) still patch it:
with patch('utils.conversations.search.client', mock_client):Since client no longer exists, patch() raises AttributeError at test time. This isn't caught by the hermetic E2E CI check (which runs testing/e2e/), but backend/test.sh runs test_lock_bypass_fixes.py on line 128 and would fail there.
The fix is straightforward — update those 5 patch sites to target the new lazy-init API, e.g.:
with patch('utils.conversations.search._get_typesense_client', return_value=mock_client):
from utils.conversations.search import search_conversations
result = search_conversations(uid='test-uid', query='test')Once that's updated, this is good to go. Happy to re-review quickly after the fix.
Automated approval dismissed: this PR falls into a category that should receive human maintainer/product/security judgment rather than formal auto-approval. No changes are being requested by this dismissal; prior automated feedback should be treated as an implementation review/positive signal only.
2312c5c to
5dfa90a
Compare
|
Updated this branch for the June 26 What changed:
Validation from
I also reran the async blocker scan and pre-commit; both completed with exit 0. |
5dfa90a to
78a144b
Compare
|
Thanks @tianmind-studio — the lazy-init refactor is clean and well-tested, and the previous review feedback has been fully addressed. What landed well:
One operational note for maintainers (not a blocker): shifting client construction from import time to first use means a missing The |
78a144b to
a2decaa
Compare
Git-on-my-level
left a comment
There was a problem hiding this comment.
Thanks @tianmind-studio — I re-reviewed the latest head (a2decaa41f7b504b928731795f098c0d7e9c7e12). The previous requested test-update gap is addressed.
What I checked:
utils.conversations.search._get_typesense_client()now uses lock-guarded lazy initialization, so importing the module no longer requiresTYPESENSE_*config.- Missing Typesense config is validated on first actual search and the
RuntimeErrorremains unwrapped because the client setup happens before the broad searchtry/except. - The five
TestSearchRedactionpatch sites now target_get_typesense_client, so they still exercise the realsearch_conversations()path without relying on the removed module-levelclientsymbol. - Added coverage exercises import-time safety, missing config, first-use construction, and concurrent first-use singleton behavior.
Verification I ran locally from backend/ on this head:
PYTHONPATH=. pytest tests/unit/test_conversation_search_typesense_config.py tests/unit/test_lock_bypass_fixes.py::TestSearchRedaction -q --tb=short→ 9 passedPYTHONPATH=. python3 -m py_compile utils/conversations/search.py tests/unit/test_conversation_search_typesense_config.py tests/unit/test_lock_bypass_fixes.py→ passed- Searched for remaining
utils.conversations.search.clientpatch/import references → none found
One operational note for maintainers: this intentionally moves missing TYPESENSE_* failures from startup/import time to first search use. That seems like the right tradeoff for router import safety, but it is worth keeping in mind for monitoring.
I’m removing the needs-tests label because the added/updated tests cover the prior gap. Not formally approving here because this PR was still carrying a blocking human-review label in the review context, but from a backend implementation/testing perspective this looks ready for maintainer final review.
a2decaa to
dd8d8ca
Compare
|
Reviewed the lazy Typesense client refactor on the current head ( |
305463f to
71a3402
Compare
Git-on-my-level
left a comment
There was a problem hiding this comment.
Thanks @tianmind-studio — I re-reviewed the current head (71a3402). This remains a small, self-contained backend refactor: the Typesense client is initialized lazily with a lock, missing config now fails clearly on first real search rather than import, and the protocol override is preserved.
I verified the targeted backend coverage locally with a stripped environment: \ -> 26 passed, plus py_compile on the touched Python files passed. GitHub checks are also green and the PR is mergeable.
No agent-behavior or instruction changes are involved. Good to go from my review.
71a3402 to
e51670f
Compare
|
Hey @tianmind-studio 👋 Thank you so much for taking the time to contribute to Omi! We truly appreciate you putting in the effort to submit this pull request. After careful review, we've decided not to merge this particular PR. Please don't take this personally — we genuinely try to merge as many contributions as possible, but sometimes we have to make tough calls based on:
Your contribution is still valuable to us, and we'd love to see you contribute again in the future! If you'd like feedback on how to improve this PR or want to discuss alternative approaches, please don't hesitate to reach out. Thank you for being part of the Omi community! 💜 |
Git-on-my-level
left a comment
There was a problem hiding this comment.
Thanks for the rebase and the well-structured lazy-init refactor — the double-checked locking and the env-var validation on first use are good.
There's one blocker that's breaking the Backend unit suite right now: the module-level client symbol is removed from backend/utils/conversations/search.py, but another module still imports it.
backend/utils/memory/atom_keyword_index.py:
def _typesense_client() -> Any:
from utils.conversations.search import client # <-- ImportError now
return clientThis is the pyright error: "client" is unknown import symbol that fails the check, and it would also be a runtime ImportError whenever keyword_search_memory_ids() is exercised. It looks like it was missed because the search-result tests were updated but this keyword-index consumer wasn't.
Suggested fix — make atom_keyword_index._typesense_client() go through the new factory:
def _typesense_client() -> Any:
from utils.conversations.search import _get_typesense_client
return _get_typesense_client()(or return _get_typesense_client() if you prefer keeping it lazy). Worth a quick grep for any other from utils.conversations.search import client / search.client references too.
Requesting changes until the build is green. Separately, there's a generic github-actions[bot] "not merging" comment above that doesn't match the review history here — flagging for a maintainer since this PR is still open and previously approved.
375e7a1 to
31a58cd
Compare
|
Hey @tianmind-studio 👋 Thank you so much for taking the time to contribute to Omi! We truly appreciate you putting in the effort to submit this pull request. After careful review, we've decided not to merge this particular PR. Please don't take this personally — we genuinely try to merge as many contributions as possible, but sometimes we have to make tough calls based on:
Before your next PR, please skim:
If this was declined for direction or taste, maintainers should cite an invariant ID or open a proposed one — ask if that citation is missing. Your contribution is still valuable to us, and we'd love to see you contribute again in the future! If you'd like feedback on how to improve this PR or want to discuss alternative approaches, please don't hesitate to reach out. Thank you for being part of the Omi community! |
0ce00bf to
b9eb80c
Compare
Git-on-my-level
left a comment
There was a problem hiding this comment.
Thanks @tianmind-studio — the latest fix correctly updates atom_keyword_index.py to use the new shared lazy accessor, and the targeted tests you listed are green.
I found one remaining blocker before this can land: another existing backend unit test file still patches the removed module-level client symbol:
backend/tests/unit/test_search_conversations_typesense_timeout_failsoft.py
Running it on the current head fails all 3 tests with AttributeError because utils.conversations.search.client no longer exists:
PYTHONPATH=. python3 -m pytest tests/unit/test_search_conversations_typesense_timeout_failsoft.py -q --tb=short
# 3 failed: patch('utils.conversations.search.client') does not have the attribute 'client'
Please update those tests to patch the lazy accessor path (for example patch('utils.conversations.search._get_typesense_client', return_value=mock_client)), similar to the lock-bypass and hybrid-search test updates already in this PR. After that, please rerun at least:
PYTHONPATH=. python3 -m pytest \
tests/unit/test_conversation_search_typesense_config.py \
tests/unit/test_search_conversations_typesense_timeout_failsoft.py \
tests/unit/test_lock_bypass_fixes.py::TestSearchRedaction \
tests/unit/test_conversation_hybrid_search.py \
-q --tb=short
Separate non-blocking note for maintainers: missing TYPESENSE_* config is still raised during first search use rather than import, but because _get_typesense_client() is called inside search_conversations()'s broad try/except, the public exception type is currently wrapped as a generic Exception with the original RuntimeError as __cause__. That may be acceptable, but it is worth being explicit about if callers rely on exception type.
|
Closing this PR to respect the project decision above and keep the contributor queue tidy. Thanks for the reviews; I will carry the feedback into smaller, issue-backed Windows contributions. |
|
Hey @tianmind-studio 👋 Thank you so much for taking the time to contribute to Omi! We truly appreciate you putting in the effort to submit this pull request. After careful review, we've decided not to merge this particular PR. Please don't take this personally — we genuinely try to merge as many contributions as possible, but sometimes we have to make tough calls based on:
Before your next PR, please skim:
If this was declined for direction or taste, maintainers should cite an invariant ID or open a proposed one — ask if that citation is missing. Your contribution is still valuable to us, and we'd love to see you contribute again in the future! If you'd like feedback on how to improve this PR or want to discuss alternative approaches, please don't hesitate to reach out. Thank you for being part of the Omi community! |
Summary
main(45fe68b82f).main's transient Typesense error handling while keepingTYPESENSE_PROTOCOLoverride support.get_typesense_client()so the canonical atom keyword index can share the same lazy singleton instead of importing the removed eagerclientsymbol.Product invariants affected
backend/utils/memory/atom_keyword_index.pystill gates canonical long-term atom keyword indexing through existing canonical/data-protection checks; this PR only changes how the shared Typesense client is acquired after lazy initialization.Why
utils.conversations.searchpreviously built a Typesense client at import time, so importing conversation search requiredTYPESENSE_HOST,TYPESENSE_HOST_PORT, andTYPESENSE_API_KEYeven in test paths that never searched. The refresh also fixes the backend typecheck regression whereutils.memory.atom_keyword_indexstill imported the old eagerclientsymbol.Validation
python -m py_compile utils\conversations\search.py utils\memory\atom_keyword_index.py tests\unit\test_conversation_search_typesense_config.pypython -m black --check utils\conversations\search.py utils\memory\atom_keyword_index.py tests\unit\test_conversation_search_typesense_config.py tests\unit\test_conversation_hybrid_search.py tests\unit\test_lock_bypass_fixes.pypython -m pytest tests\unit\test_conversation_search_typesense_config.py tests\unit\test_ws_m_atom_keyword_index.py -q --tb=short-> 27 passedpython -m pytest tests\unit\test_conversation_hybrid_search.py tests\unit\test_lock_bypass_fixes.py -q --tb=short-> 75 passedpython -m pyright -p pyrightconfig.json utils\conversations\search.py utils\memory\atom_keyword_index.py --pythonpath D:\codex-omi-work\.venvs\omi-backend-311\Scripts\python.exe-> 0 errors, 1 existing warninggit diff --check