Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix: coalesce duplicate UltraWiki vector searches

Ruben Lütke557216dcce6f
diff --git a/jarvis/ultrawiki/search.py b/jarvis/ultrawiki/search.py
index 6de056b94a..73857c354f 100644
--- a/jarvis/ultrawiki/search.py
+++ b/jarvis/ultrawiki/search.py
@@ -57,6 +57,7 @@ import time
from dataclasses import replace
from datetime import UTC, datetime
from typing import Any
+from weakref import WeakKeyDictionary
from jarvis.ultrawiki.types import SearchResult
@@ -394,6 +395,28 @@ def _configured_model(ultrawiki: Any, provider: str, defaults: dict[str, str]) -
#: exists to prevent.
_QUERY_VECTOR_CACHE: dict[tuple[str, str, str], list[float]] = {}
_QUERY_VECTOR_CACHE_MAX = 256
+_QUERY_VECTOR_INFLIGHT: dict[
+ tuple[str, str, str], asyncio.Task[list[list[float]]]
+] = {}
+
+#: The exact same semantic query often arrives twice in one turn (context
+#: injection + explicit tool/UI). sqlite-vec is an exact scan on the universal
+#: SQLite floor, so two concurrent 3,072-dimensional scans made each other
+#: several times slower on the live corpus. Coalesce in-flight work and retain
+#: a tiny, short-lived result cache; it is memory-only and bounded.
+_VECTOR_RESULT_TTL_S = 10.0
+_VECTOR_RESULT_CACHE_MAX = 64
+_VECTOR_RESULT_CACHE: WeakKeyDictionary[
+ Any,
+ dict[tuple[str, str, str, str], tuple[float, tuple[list[SearchResult], str]]],
+] = WeakKeyDictionary()
+_VECTOR_RESULT_INFLIGHT: WeakKeyDictionary[
+ Any,
+ dict[
+ tuple[str, str, str, str],
+ asyncio.Task[tuple[list[SearchResult], str]],
+ ],
+] = WeakKeyDictionary()
def _query_cache_key(provider: str, model: str, query: str) -> tuple[str, str, str]:
@@ -414,6 +437,81 @@ def _remember_query_vector(key: tuple[str, str, str], vector: list[float]) -> No
del _QUERY_VECTOR_CACHE[next(iter(_QUERY_VECTOR_CACHE))]
+def _consume_task_exception(task: asyncio.Task[Any]) -> None:
+ """Retrieve a detached shared task's exception after all waiters cancel."""
+ if task.cancelled():
+ return
+ task.exception()
+
+
+async def _embed_query_once(
+ key: tuple[str, str, str], backend: Any, query: str, model: str
+) -> list[list[float]]:
+ """Coalesce concurrent misses for one query embedding."""
+ task = _QUERY_VECTOR_INFLIGHT.get(key)
+ if task is None:
+ task = asyncio.create_task(
+ backend.embed([query], model=model),
+ name="ultrawiki-query-embedding",
+ )
+ _QUERY_VECTOR_INFLIGHT[key] = task
+ task.add_done_callback(_consume_task_exception)
+ try:
+ return await asyncio.shield(task)
+ finally:
+ if task.done() and _QUERY_VECTOR_INFLIGHT.get(key) is task:
+ _QUERY_VECTOR_INFLIGHT.pop(key, None)
+
+
+async def _run_vector_search_once(
+ store: Any,
+ key: tuple[str, str, str, str],
+ query_vector: list[float],
+ area_id: str | None,
+) -> tuple[list[SearchResult], str]:
+ """Own one physical ANN scan and publish its short-lived result."""
+ try:
+ result = await store.vector_search(
+ query_vector, k=LEG_POOL, area_id=area_id
+ )
+ cache = _VECTOR_RESULT_CACHE.setdefault(store, {})
+ cache[key] = (time.monotonic() + _VECTOR_RESULT_TTL_S, result)
+ while len(cache) > _VECTOR_RESULT_CACHE_MAX:
+ del cache[next(iter(cache))]
+ return result
+ finally:
+ inflight = _VECTOR_RESULT_INFLIGHT.get(store)
+ current = asyncio.current_task()
+ if inflight is not None and inflight.get(key) is current:
+ inflight.pop(key, None)
+
+
+async def _vector_search_once(
+ store: Any,
+ key: tuple[str, str, str, str],
+ query_vector: list[float],
+ area_id: str | None,
+) -> tuple[list[SearchResult], str]:
+ """Serve a recent identical scan or share the one already running."""
+ cache = _VECTOR_RESULT_CACHE.setdefault(store, {})
+ cached = cache.get(key)
+ if cached is not None:
+ if cached[0] > time.monotonic():
+ cache[key] = cache.pop(key)
+ return cached[1]
+ cache.pop(key, None)
+ inflight = _VECTOR_RESULT_INFLIGHT.setdefault(store, {})
+ task = inflight.get(key)
+ if task is None:
+ task = asyncio.create_task(
+ _run_vector_search_once(store, key, query_vector, area_id),
+ name="ultrawiki-vector-search",
+ )
+ inflight[key] = task
+ task.add_done_callback(_consume_task_exception)
+ return await asyncio.shield(task)
+
+
async def _bounded_vector_leg(
leg: Any, timeout_s: float
) -> list[SearchResult]:
@@ -469,7 +567,9 @@ async def _vector_leg(
if query_vector is None:
try:
vectors = await _timed(
- "vector_embed_ms", factory(cfg).embed([query], model=model), sink
+ "vector_embed_ms",
+ _embed_query_once(cache_key, factory(cfg), query, model),
+ sink,
)
except EmbeddingError as exc:
log.info("vector leg skipped: %s", exc)
@@ -487,9 +587,10 @@ async def _vector_leg(
_remember_query_vector(cache_key, query_vector)
else:
log.debug("query vector served from the in-process cache")
+ result_key = (*cache_key, str(area_id or ""))
results, reason = await _timed(
"vector_ann_ms",
- store.vector_search(query_vector, k=LEG_POOL, area_id=area_id),
+ _vector_search_once(store, result_key, query_vector, area_id),
sink,
)
if reason:
diff --git a/tests/unit/ultrawiki/conftest.py b/tests/unit/ultrawiki/conftest.py
index ac8977ae4c..436d124f6b 100644
--- a/tests/unit/ultrawiki/conftest.py
+++ b/tests/unit/ultrawiki/conftest.py
@@ -13,5 +13,11 @@ def _fresh_query_vector_cache():
must never satisfy (or mask) another test's embedding path — that would
make the suite order-dependent."""
search_mod._QUERY_VECTOR_CACHE.clear()
+ search_mod._QUERY_VECTOR_INFLIGHT.clear()
+ search_mod._VECTOR_RESULT_CACHE.clear()
+ search_mod._VECTOR_RESULT_INFLIGHT.clear()
yield
search_mod._QUERY_VECTOR_CACHE.clear()
+ search_mod._QUERY_VECTOR_INFLIGHT.clear()
+ search_mod._VECTOR_RESULT_CACHE.clear()
+ search_mod._VECTOR_RESULT_INFLIGHT.clear()
diff --git a/tests/unit/ultrawiki/test_search.py b/tests/unit/ultrawiki/test_search.py
index 170a92db14..1e06039dda 100644
--- a/tests/unit/ultrawiki/test_search.py
+++ b/tests/unit/ultrawiki/test_search.py
@@ -11,6 +11,7 @@ network, no credentials, no models.
from __future__ import annotations
+import asyncio
import importlib.util
import logging
from datetime import UTC, datetime, timedelta
@@ -1060,6 +1061,40 @@ async def test_repeated_query_embeds_only_once(store, monkeypatch):
assert len(fake.calls) == 1
+async def test_concurrent_identical_searches_share_embedding_and_ann(monkeypatch):
+ """One UI/brain turn must not multiply the two expensive vector steps."""
+ gate = asyncio.Event()
+
+ class SlowEmbedding(FakeEmbedding):
+ async def embed(self, texts, *, model):
+ await gate.wait()
+ return await super().embed(texts, model=model)
+
+ class SlowStore(FakeStore):
+ async def vector_search(self, query_vector, k=10, *, area_id=None):
+ await asyncio.sleep(0.02)
+ return await super().vector_search(
+ query_vector, k=k, area_id=area_id
+ )
+
+ fake = SlowEmbedding()
+ register_fake_embedding(monkeypatch, fake)
+ store = SlowStore(
+ keyword_hits=[make_result(1, matched_by=("keyword",))],
+ vector_hits=[make_result(1, matched_by=("vector",))],
+ )
+ cfg = make_cfg(embedding_provider="fake", embedding_model=EMBED_MODEL)
+
+ first = asyncio.create_task(hybrid_search(store, cfg, "alpha"))
+ second = asyncio.create_task(hybrid_search(store, cfg, "alpha"))
+ await asyncio.sleep(0)
+ gate.set()
+ await asyncio.gather(first, second)
+
+ assert len(fake.calls) == 1
+ assert len(store.vector_calls) == 1
+
+
async def test_cache_is_scoped_by_model_and_query(store, monkeypatch):
await add_source(store)
await store.upsert_items("src1", [raw_item("a", body="alpha content")])