Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(embedding): scale the default Ollama timeout with the batch size (#3578)
Cestercian3cf1b8ebf4bf
diff --git a/docs/reference/CONFIG.md b/docs/reference/CONFIG.mdindex 877d81201..72db5bdb1 100644--- a/docs/reference/CONFIG.md+++ b/docs/reference/CONFIG.md@@ -1002,7 +1002,7 @@ The `.repowise/.env` file is gitignored automatically.| `EDENAI_EMBEDDING_TIMEOUT` | As above, `edenai` only || `OLLAMA_EMBEDDING_MODEL` | Ollama embedding model (also selects the `ollama` embedder) || `OLLAMA_EMBEDDING_DIMS` | Ollama embedding output dimensions (optional; inferred from the model otherwise) |-| `OLLAMA_EMBEDDING_TIMEOUT` | As above, `ollama` only; raise it for long pages on slow local models |+| `OLLAMA_EMBEDDING_TIMEOUT` | As above, `ollama` only. With neither variable set, an Ollama request gets 30s plus 2ms per input character, so a full chunk of long pages on a CPU-only model is not cut off. Setting either variable pins the timeout to that value |### Server and databasediff --git a/packages/core/src/repowise/core/providers/embedding/ollama.py b/packages/core/src/repowise/core/providers/embedding/ollama.pyindex 77a445ed6..c5d433aff 100644--- a/packages/core/src/repowise/core/providers/embedding/ollama.py+++ b/packages/core/src/repowise/core/providers/embedding/ollama.py@@ -17,12 +17,31 @@ from repowise.core.providers.embedding.base import resolve_embedding_timeout_DEFAULT_BASE_URL = "http://localhost:11434"_DEFAULT_MODEL = "embeddinggemma"_DEFAULT_TIMEOUT = 30.0+# Extra seconds per input character when no timeout is configured. A CPU-only+# Ollama measured ~0.8 ms/char on a full 16-page chunk (171.6s for ~210k chars),+# so a flat 30s dropped every such chunk. 2 ms/char leaves room for slower boxes.+_SECONDS_PER_CHAR = 0.002+_TIMEOUT_ENV_VARS = ("OLLAMA_EMBEDDING_TIMEOUT", "REPOWISE_EMBEDDING_TIMEOUT")def _normalize_base_url(url: str) -> str:return url.rstrip("/")+def _timeout_env_is_set() -> bool:+ for name in _TIMEOUT_ENV_VARS:+ raw = os.environ.get(name)+ if not raw:+ continue+ try:+ value = float(raw)+ except ValueError:+ continue+ if math.isfinite(value) and value > 0:+ return True+ return False++def _infer_dimensions(model: str) -> int:"""Best-effort dimension hint for common Ollama embedding models."""name = model.lower()@@ -51,8 +70,9 @@ class OllamaEmbedder:``dimensions`` when provided.timeout: Per-request timeout in seconds. Falls back to the``OLLAMA_EMBEDDING_TIMEOUT`` / ``REPOWISE_EMBEDDING_TIMEOUT`` env- vars, then ``30.0``. Raise it when embedding long pages on a slow- local model that would otherwise exceed the default and be dropped.+ vars. With neither set, each request gets ``30.0`` seconds plus time+ for the size of the batch, so a full chunk of long pages on a+ CPU-only model is not cancelled and dropped."""def __init__(@@ -92,11 +112,17 @@ class OllamaEmbedder:self._timeout = resolve_embedding_timeout(timeout, _DEFAULT_TIMEOUT, provider_env="OLLAMA_EMBEDDING_TIMEOUT")+ self._scale_timeout = timeout is None and not _timeout_env_is_set()@propertydef dimensions(self) -> int:return self._dimensions+ def _request_timeout(self, texts: list[str]) -> float:+ if not self._scale_timeout:+ return self._timeout+ return self._timeout + sum(len(text) for text in texts) * _SECONDS_PER_CHAR+async def embed(self, texts: list[str]) -> list[list[float]]:"""Embed a batch of texts using Ollama's native API."""if not texts:@@ -109,7 +135,7 @@ class OllamaEmbedder:if self._requested_dimensions is not None:payload["dimensions"] = self._requested_dimensions- async with httpx.AsyncClient(timeout=self._timeout) as client:+ async with httpx.AsyncClient(timeout=self._request_timeout(texts)) as client:response = await client.post(f"{self._base_url}/api/embed", json=payload)response.raise_for_status()data = response.json()diff --git a/tests/unit/test_persistence/test_ollama_embedder.py b/tests/unit/test_persistence/test_ollama_embedder.pyindex 3f02616a9..bb0743334 100644--- a/tests/unit/test_persistence/test_ollama_embedder.py+++ b/tests/unit/test_persistence/test_ollama_embedder.py@@ -93,7 +93,7 @@ async def test_embed_posts_batch_to_native_endpoint() -> None:"input": ["first", "second"],"dimensions": 1024,},- "timeout": 30.0,+ "timeout": pytest.approx(30.0 + len("firstsecond") * 0.002),}]assert len(vectors) == 2@@ -159,6 +159,56 @@ async def test_env_timeout_is_applied_to_the_request(monkeypatch: pytest.MonkeyPassert _FakeAsyncClient.calls[0]["timeout"] == 300.0+async def test_default_timeout_grows_with_the_batch(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.delenv("OLLAMA_EMBEDDING_TIMEOUT", raising=False)+ monkeypatch.delenv("REPOWISE_EMBEDDING_TIMEOUT", raising=False)+ _FakeAsyncClient.response_data = {"embeddings": [[1.0, 0.0]] * 16}+ embedder = OllamaEmbedder(model="embeddinggemma", dimensions=2)++ # A full chunk of long pages measured 171.6s on a CPU-only Ollama.+ await embedder.embed(["x" * 16_000] * 16)++ assert _FakeAsyncClient.calls[0]["timeout"] > 171.6+ assert _FakeAsyncClient.calls[0]["timeout"] == pytest.approx(30.0 + 16 * 16_000 * 0.002)+++async def test_short_batch_keeps_about_the_default_timeout(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.delenv("OLLAMA_EMBEDDING_TIMEOUT", raising=False)+ monkeypatch.delenv("REPOWISE_EMBEDDING_TIMEOUT", raising=False)+ embedder = OllamaEmbedder(model="embeddinggemma", dimensions=2)++ await embedder.embed(["one"])++ assert _FakeAsyncClient.calls[0]["timeout"] == pytest.approx(30.006)+++async def test_configured_timeout_is_not_scaled(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.delenv("OLLAMA_EMBEDDING_TIMEOUT", raising=False)+ monkeypatch.setenv("REPOWISE_EMBEDDING_TIMEOUT", "45")+ _FakeAsyncClient.response_data = {"embeddings": [[1.0, 0.0]] * 4}++ await OllamaEmbedder(model="embeddinggemma", dimensions=2).embed(["x" * 16_000] * 4)+ await OllamaEmbedder(model="embeddinggemma", dimensions=2, timeout=5.0).embed(+ ["x" * 16_000] * 4+ )++ assert [call["timeout"] for call in _FakeAsyncClient.calls] == [45.0, 5.0]+++async def test_invalid_env_timeout_still_scales(+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]+) -> None:+ monkeypatch.setenv("OLLAMA_EMBEDDING_TIMEOUT", "soon")+ monkeypatch.delenv("REPOWISE_EMBEDDING_TIMEOUT", raising=False)+ embedder = OllamaEmbedder(model="embeddinggemma", dimensions=2)++ await embedder.embed(["x" * 1_000])++ assert _FakeAsyncClient.calls[0]["timeout"] == pytest.approx(32.0)++# ---------------------------------------------------------------------------# Width verification — the check added in fix/embedder-width-verification# ---------------------------------------------------------------------------@@ -176,8 +226,8 @@ async def test_embed_raises_when_server_returns_wrong_width_inferred() -> None:await embedder.embed(["hello"])msg = str(exc_info.value)- assert "3" in msg # actual width named- assert "OLLAMA_EMBEDDING_DIMS" in msg # points at the env var fix+ assert "3" in msg # actual width named+ assert "OLLAMA_EMBEDDING_DIMS" in msg # points at the env var fixasync def test_embed_raises_when_server_returns_wrong_width_explicit() -> None:@@ -199,4 +249,3 @@ async def test_embed_width_check_not_triggered_on_empty() -> None:# embed([]) short-circuits and returns [] without calling the server.embedder = OllamaEmbedder(model="embeddinggemma")assert await embedder.embed([]) == []-