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(ultrawiki): rest the embed/distill stages on a rate-limited provider

Ruben Lütked4839bb60ffd
diff --git a/jarvis/ultrawiki/pipeline.py b/jarvis/ultrawiki/pipeline.py
index 103aa7add2..b692522202 100644
--- a/jarvis/ultrawiki/pipeline.py
+++ b/jarvis/ultrawiki/pipeline.py
@@ -70,6 +70,29 @@ KEYWORD_BATCH = 200
EMBED_BATCH = 32
DISTILL_BATCH = 4
+#: How long the embedding-dependent stages sleep after the provider answered
+#: with a rate/quota rejection. A depleted quota is GLOBAL: retrying the batch
+#: members individually just multiplied one 429 into 33 per pass, thousands of
+#: pointless HTTP calls and write transactions per hour (observed live
+#: 2026-07-26), starving the read path's event loop in the process.
+EMBED_COOLDOWN_S = 600.0
+
+#: Provider-answer shapes that mean "stop asking for a while" rather than
+#: "this item is poisoned": rate limit, out of credit, service unavailable.
+_RATE_LIMIT_MARKERS = ("HTTP 429", "HTTP 402", "HTTP 503")
+
+
+def _rate_limited_embed(exc: BaseException) -> bool:
+ """True for an EmbeddingError carrying a rate/quota/unavailable status.
+
+ Checked by class NAME so this module keeps its lazy-import discipline
+ (AP-26); the marker strings come from the adapters' honest error texts.
+ """
+ if type(exc).__name__ != "EmbeddingError":
+ return False
+ message = str(exc)
+ return any(marker in message for marker in _RATE_LIMIT_MARKERS)
+
#: Media enrichment claims ONE item per pass, and only when every other stage
#: found nothing to do. Describing a picture costs a model call, and a photo
#: library is tens of thousands of them — so this lane is deliberately the
@@ -177,6 +200,9 @@ class PipelineWorker:
#: stage -> the pause reason last logged, so a persistent gap logs once.
self._pause_reasons: dict[str, str] = {}
self._source_kind_cache: dict[str, str] = {}
+ #: monotonic deadline until which the embedding-dependent stages rest
+ #: after a rate/quota rejection (0.0 = no cooldown).
+ self._embed_cooldown_until = 0.0
# -- public surface ------------------------------------------------------
@@ -259,6 +285,33 @@ class PipelineWorker:
def _clear_stage_pause(self, stage: str) -> None:
self._pause_reasons.pop(stage, None)
+ def _begin_embed_cooldown(self, exc: BaseException) -> None:
+ """One rate/quota rejection rests BOTH embedding-dependent stages.
+
+ No attempt is charged and no retry is written: the failure is the
+ provider's global state, not any item's fault — the claims were
+ read-only, so every item simply becomes claimable again when the
+ cooldown ends.
+ """
+ self._embed_cooldown_until = time.monotonic() + EMBED_COOLDOWN_S
+ log.warning(
+ "UltraWiki embedding provider rejected with a rate/quota answer "
+ "(%s) — resting the embed and distill stages for %.0f minutes "
+ "instead of hammering it per item",
+ exc,
+ EMBED_COOLDOWN_S / 60.0,
+ )
+
+ def _embed_cooldown_reason(self) -> str:
+ """Honest pause reason while the cooldown runs, else ``""``."""
+ remaining = self._embed_cooldown_until - time.monotonic()
+ if remaining <= 0:
+ return ""
+ return (
+ "embedding provider is rate-limited or out of quota — retrying "
+ f"in {max(1, int(remaining // 60))} minute(s)"
+ )
+
def _note_lost_claim(self, item: dict[str, Any], stage: str) -> None:
"""A concurrent content change invalidated this claim — not an error.
@@ -481,6 +534,10 @@ class PipelineWorker:
if backend is None:
self._note_stage_pause("embed", reason)
return 0
+ cooldown = self._embed_cooldown_reason()
+ if cooldown:
+ self._note_stage_pause("embed", cooldown)
+ return 0
self._clear_stage_pause("embed")
items = await self._store.claim_batch(
ItemState.EMBEDDED, limit=EMBED_BATCH, now=self._claim_now()
@@ -501,6 +558,12 @@ class PipelineWorker:
except asyncio.CancelledError:
raise
except Exception as exc: # noqa: BLE001 — fall back to per-item embedding
+ # A rate/quota rejection is GLOBAL, not a poison item: retrying
+ # the members individually would multiply one 429 into 33 calls
+ # per pass. Rest instead; the read-only claims release themselves.
+ if _rate_limited_embed(exc):
+ self._begin_embed_cooldown(exc)
+ return 0
# ONE unembeddable text (provider 400 on some content) used to
# charge an attempt to all 32 batch members, so a single poison
# item dead-lettered a whole healthy batch every five passes.
@@ -552,6 +615,11 @@ class PipelineWorker:
except asyncio.CancelledError:
raise
except Exception as exc: # noqa: BLE001 — only this item is charged
+ if _rate_limited_embed(exc):
+ # Mid-loop quota exhaustion: stop charging anyone and
+ # rest — the remaining claims are read-only anyway.
+ self._begin_embed_cooldown(exc)
+ break
await self._retry(item, "embed", exc)
return len(items)
@@ -603,6 +671,12 @@ class PipelineWorker:
if backend is None:
self._note_stage_pause("distill", reason)
return 0
+ cooldown = self._embed_cooldown_reason()
+ if cooldown:
+ # The distilled summary must be embedded too, so this stage rests
+ # through the same provider cooldown as the embed pass.
+ self._note_stage_pause("distill", cooldown)
+ return 0
distill_ok, distill_reason = await self._distill_ready()
if not distill_ok:
self._note_stage_pause("distill", distill_reason)
@@ -622,6 +696,9 @@ class PipelineWorker:
except asyncio.CancelledError:
raise
except Exception as exc: # noqa: BLE001 — one poisoned item blocks nothing
+ if _rate_limited_embed(exc):
+ self._begin_embed_cooldown(exc)
+ break
await self._retry(item, "distill", exc)
return len(items)
diff --git a/tests/unit/ultrawiki/test_pipeline.py b/tests/unit/ultrawiki/test_pipeline.py
index d8ded21961..a1e297ade8 100644
--- a/tests/unit/ultrawiki/test_pipeline.py
+++ b/tests/unit/ultrawiki/test_pipeline.py
@@ -358,6 +358,95 @@ async def test_batch_embed_failure_retries_members_individually(store):
assert len(backend.embed_calls) == 1 + count
+async def test_quota_rejection_rests_the_stage_instead_of_hammering(store):
+ """A 429/quota answer is GLOBAL: no per-item fan-out (one 429 used to
+ become 33 calls per pass), no attempts charged, and the next pass claims
+ nothing while the cooldown runs."""
+ from jarvis.ultrawiki.embeddings import EmbeddingError
+
+ await store.upsert_items("src1", [make_item(i) for i in range(5)])
+
+ class QuotaBackend(FakeEmbeddingBackend):
+ async def embed(self, texts: list[str], *, model: str) -> list[list[float]]:
+ self.embed_calls.append(list(texts))
+ raise EmbeddingError("fake: embedding request failed with HTTP 429")
+
+ backend = QuotaBackend()
+ worker = PipelineWorker(
+ store,
+ make_cfg(),
+ embedding_backend_factory=lambda: backend,
+ distill_fn=distill_never,
+ distill_ready_fn=DISTILL_READY,
+ )
+
+ await worker._keyword_pass() # noqa: SLF001 — drive one stage deliberately
+ await worker._embed_pass() # noqa: SLF001 — the 429 batch call
+ await worker._embed_pass() # noqa: SLF001 — cooldown: must not call again
+
+ assert len(backend.embed_calls) == 1, "no per-item fan-out, no re-claim"
+ item = await store.get_item_by_external_id("src1", "ext-0000")
+ assert item["attempt_count"] == 0, "a global outage charges nobody"
+ assert item["state"] == ItemState.KEYWORD_INDEXED.value
+ assert item["last_error"] is None
+
+
+async def test_embed_work_resumes_after_the_cooldown(store):
+ from jarvis.ultrawiki.embeddings import EmbeddingError
+
+ await store.upsert_items("src1", [make_item(1)])
+
+ class HealingBackend(FakeEmbeddingBackend):
+ def __init__(self) -> None:
+ super().__init__()
+ self.fail_next = True
+
+ async def embed(self, texts: list[str], *, model: str) -> list[list[float]]:
+ if self.fail_next:
+ self.fail_next = False
+ raise EmbeddingError("fake: embedding request failed with HTTP 429")
+ return await super().embed(texts, model=model)
+
+ backend = HealingBackend()
+ worker = PipelineWorker(
+ store,
+ make_cfg(),
+ embedding_backend_factory=lambda: backend,
+ distill_fn=distill_never,
+ distill_ready_fn=DISTILL_READY,
+ )
+
+ await worker._keyword_pass() # noqa: SLF001
+ await worker._embed_pass() # noqa: SLF001 — 429, cooldown starts
+ worker._embed_cooldown_until = 0.0 # noqa: SLF001 — cooldown elapsed
+ await worker._embed_pass() # noqa: SLF001
+
+ assert worker.processed_counts()["embed"] == 1
+ item = await store.get_item_by_external_id("src1", "ext-0001")
+ assert item["state"] == ItemState.EMBEDDED.value
+
+
+async def test_distill_pass_rests_through_the_embed_cooldown(store):
+ """The distilled summary must be embedded too, so the distill stage
+ shares the provider cooldown instead of burning chat-model calls whose
+ result cannot be stored."""
+ from jarvis.ultrawiki.embeddings import EmbeddingError
+
+ backend = FakeEmbeddingBackend()
+ worker = PipelineWorker(
+ store,
+ make_cfg(),
+ embedding_backend_factory=lambda: backend,
+ distill_fn=distill_never, # asserts if the stage claims work
+ distill_ready_fn=DISTILL_READY,
+ )
+ worker._begin_embed_cooldown( # noqa: SLF001
+ EmbeddingError("fake: embedding request failed with HTTP 429")
+ )
+
+ assert await worker._distill_pass() == 0 # noqa: SLF001
+
+
async def test_a_long_item_is_embedded_as_many_passages_not_truncated(store):
"""The change that makes full-depth ingestion mean anything.