This repository was archived by the owner on Jun 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Add fast raw memory search path #194
Open
cnguyen14
wants to merge
3
commits into
XortexAI:main
Choose a base branch
from
cnguyen14:codex/fast-search-path-163-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,8 @@ | |||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| import asyncio | ||||||||||||||||||||||||||||||||||||
| import logging | ||||||||||||||||||||||||||||||||||||
| import threading | ||||||||||||||||||||||||||||||||||||
| import time | ||||||||||||||||||||||||||||||||||||
| from typing import Any, Callable, Dict, List, Optional | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| from dotenv import load_dotenv | ||||||||||||||||||||||||||||||||||||
|
|
@@ -133,6 +135,12 @@ def __init__( | |||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| self.embed_fn = embed_fn | ||||||||||||||||||||||||||||||||||||
| self._snippet_stores: Dict[str, BaseVectorStore] = {} | ||||||||||||||||||||||||||||||||||||
| self._profile_catalog_cache: Dict[str, tuple[float, List[Dict[str, str]], list]] = {} | ||||||||||||||||||||||||||||||||||||
| self._raw_retrieval_plan_cache: Dict[tuple[tuple[str, ...], bool], tuple[str, ...]] = {} | ||||||||||||||||||||||||||||||||||||
| self._cache_ttl_seconds = 60.0 | ||||||||||||||||||||||||||||||||||||
| self._profile_catalog_cache_max_users = 256 | ||||||||||||||||||||||||||||||||||||
| self._profile_catalog_cache_lock = threading.Lock() | ||||||||||||||||||||||||||||||||||||
| self._raw_retrieval_plan_cache_lock = threading.Lock() | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| logger.info("RetrievalPipeline initialized") | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
@@ -494,6 +502,17 @@ def _fetch_profile_catalog(self, user_id: str): | |||||||||||||||||||||||||||||||||||
| catalog — list of {topic, sub_topic} for the prompt | ||||||||||||||||||||||||||||||||||||
| raw_results — the full SearchResult list, cached for _search_profile | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| now = time.monotonic() | ||||||||||||||||||||||||||||||||||||
| with self._profile_catalog_cache_lock: | ||||||||||||||||||||||||||||||||||||
| self._prune_profile_catalog_cache(now) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| cached = self._profile_catalog_cache.get(user_id) | ||||||||||||||||||||||||||||||||||||
| if cached and now - cached[0] < self._cache_ttl_seconds: | ||||||||||||||||||||||||||||||||||||
| catalog, results = cached[1], cached[2] | ||||||||||||||||||||||||||||||||||||
| self._profile_catalog_cache.pop(user_id) | ||||||||||||||||||||||||||||||||||||
| self._profile_catalog_cache[user_id] = (now, catalog, results) | ||||||||||||||||||||||||||||||||||||
| return catalog, results | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||
| results = self.vector_store.search_by_metadata( | ||||||||||||||||||||||||||||||||||||
| filters={"user_id": user_id, "domain": "profile"}, | ||||||||||||||||||||||||||||||||||||
|
|
@@ -524,8 +543,35 @@ def _fetch_profile_catalog(self, user_id: str): | |||||||||||||||||||||||||||||||||||
| "sub_topic": "", | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| with self._profile_catalog_cache_lock: | ||||||||||||||||||||||||||||||||||||
| self._prune_profile_catalog_cache(now) | ||||||||||||||||||||||||||||||||||||
| self._profile_catalog_cache[user_id] = (now, catalog, results) | ||||||||||||||||||||||||||||||||||||
| return catalog, results | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def _prune_profile_catalog_cache(self, now: float) -> None: | ||||||||||||||||||||||||||||||||||||
| """Bound profile catalog cache by TTL and number of cached users.""" | ||||||||||||||||||||||||||||||||||||
| expired_user_ids = [ | ||||||||||||||||||||||||||||||||||||
| cached_user_id | ||||||||||||||||||||||||||||||||||||
| for cached_user_id, (cached_at, _, _) in self._profile_catalog_cache.items() | ||||||||||||||||||||||||||||||||||||
| if now - cached_at >= self._cache_ttl_seconds | ||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||
| for cached_user_id in expired_user_ids: | ||||||||||||||||||||||||||||||||||||
| self._profile_catalog_cache.pop(cached_user_id, None) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| while len(self._profile_catalog_cache) >= self._profile_catalog_cache_max_users: | ||||||||||||||||||||||||||||||||||||
| oldest_user_id = next(iter(self._profile_catalog_cache)) | ||||||||||||||||||||||||||||||||||||
| self._profile_catalog_cache.pop(oldest_user_id, None) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def raw_retrieval_plan(self, domains: List[str], answer: bool = False) -> tuple[str, ...]: | ||||||||||||||||||||||||||||||||||||
| """Return a cached deterministic raw-search plan for the requested domains.""" | ||||||||||||||||||||||||||||||||||||
| ordered_allowed = ("profile", "temporal", "summary", "snippet", "code") | ||||||||||||||||||||||||||||||||||||
| normalized = tuple(d for d in ordered_allowed if d in set(domains)) | ||||||||||||||||||||||||||||||||||||
| key = (normalized, answer) | ||||||||||||||||||||||||||||||||||||
| with self._raw_retrieval_plan_cache_lock: | ||||||||||||||||||||||||||||||||||||
| if key not in self._raw_retrieval_plan_cache: | ||||||||||||||||||||||||||||||||||||
| self._raw_retrieval_plan_cache[key] = normalized | ||||||||||||||||||||||||||||||||||||
| return self._raw_retrieval_plan_cache[key] | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+565
to
+573
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def _format_catalog(self, catalog: List[Dict[str, str]]) -> str: | ||||||||||||||||||||||||||||||||||||
| """Format profile catalog for the system prompt.""" | ||||||||||||||||||||||||||||||||||||
| if not catalog: | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When two concurrent calls for the same
user_idboth miss the cache in the firstwithblock, they both release the lock and both execute the expensivesearch_by_metadataquery. When the secondwithblock is reached, there's no re-check of whether another thread already populated the entry. The second writer overwrites the first with a stalenowtimestamp (captured at the very start of the function). A simple check likeif user_id not in self._profile_catalog_cache:before assigning would avoid the redundant overwrite and protect against cache stampede on concurrent first-hit requests for the same user.