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.

perf(ultrawiki): let the app finish starting before the backlog grinds

Ruben Lütke27c055f5d079
diff --git a/jarvis/ui/web/server.py b/jarvis/ui/web/server.py
index c63e09d685..e6d7ebaa9e 100644
--- a/jarvis/ui/web/server.py
+++ b/jarvis/ui/web/server.py
@@ -71,6 +71,17 @@ ASSETS_DIR = DIST_DIR / "assets"
# re-throttled every event; the browser reconnects on its own.
_WS_SEND_TIMEOUT_S = 3.0
+# How long the UltraWiki pipeline WORKER is held back after boot. Its store is
+# opened immediately (reads, search and recall all need it), but the worker
+# grinds through the whole ingest backlog and was measured holding ~1.3 CPU
+# cores continuously for as long as items remain queued. Starting it the
+# instant the backend comes up made that backlog compete with the app's own
+# startup, which is exactly the "everything got sluggish" report AP-26 exists
+# to prevent. The window covers a typical cold start with headroom; nothing is
+# skipped, the same work simply begins once the app is usable. A shutdown
+# inside the window cancels the wait rather than delaying it.
+ULTRAWIKI_PIPELINE_GRACE_S = 20.0
+
class WebServer:
"""In-process uvicorn + FastAPI, run by the orchestrator loop."""
@@ -2689,7 +2700,8 @@ class WebServer:
set_active_service(service)
if bool(getattr(getattr(self.cfg, "ultrawiki", None), "enabled", False)):
task = asyncio.create_task(
- service.ensure_started(), name="ultrawiki-start"
+ service.ensure_started(pipeline_grace_s=ULTRAWIKI_PIPELINE_GRACE_S),
+ name="ultrawiki-start",
)
self._ultrawiki_start_task = task
task.add_done_callback(self._on_ultrawiki_start_done)
diff --git a/tests/unit/ultrawiki/test_pipeline_startup_grace.py b/tests/unit/ultrawiki/test_pipeline_startup_grace.py
new file mode 100644
index 0000000000..c2b016cb8c
--- /dev/null
+++ b/tests/unit/ultrawiki/test_pipeline_startup_grace.py
@@ -0,0 +1,122 @@
+"""The pipeline worker's startup grace period — start fast, shut down fast.
+
+Why this exists (2026-07-26): the ingest worker was started the instant the
+backend came up. With a large backlog it holds ~1.3 CPU cores continuously, so
+it competed with the app's own startup and made every launch feel sluggish
+(AP-26). The store still opens immediately — reads, search and recall need it —
+but the worker now waits out a grace period first.
+
+The trap this pins shut: implementing the grace as a plain ``asyncio.sleep``
+would turn a startup win into a shutdown stall, because ``stop()`` would then
+block for the remainder of the window. The wait must therefore be a cancellable
+wait ON the shutdown event, which is what the third test proves.
+
+These exercise the scheduling helper directly with a recording double, so they
+need no store, no config and no credentials.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import time
+
+import pytest
+
+from jarvis.ultrawiki.service import UltraWikiService
+
+
+class RecordingPipeline:
+ """Stands in for PipelineWorker, recording whether/when run() was entered."""
+
+ def __init__(self) -> None:
+ self.started = False
+ self.started_at: float | None = None
+
+ async def run(self, cancel_event: asyncio.Event) -> None:
+ self.started = True
+ self.started_at = time.perf_counter()
+ await cancel_event.wait()
+
+
+async def test_without_a_grace_period_the_worker_starts_straight_away() -> None:
+ """An in-app enable passes no grace: the user is waiting for the work."""
+ pipeline = RecordingPipeline()
+ cancel = asyncio.Event()
+
+ task = asyncio.create_task(
+ UltraWikiService._run_pipeline_after_grace(pipeline, cancel, 0.0)
+ )
+ # One scheduler turn is all an ungraced start may need.
+ await asyncio.sleep(0)
+ await asyncio.sleep(0)
+
+ assert pipeline.started, "the worker should run immediately without a grace"
+
+ cancel.set()
+ await asyncio.wait_for(task, timeout=1.0)
+
+
+async def test_a_grace_period_holds_the_worker_back_then_runs_it() -> None:
+ """The backlog is deferred, never skipped: it starts once the window ends."""
+ pipeline = RecordingPipeline()
+ cancel = asyncio.Event()
+ grace = 0.15
+
+ task = asyncio.create_task(
+ UltraWikiService._run_pipeline_after_grace(pipeline, cancel, grace)
+ )
+ await asyncio.sleep(grace / 3)
+ assert not pipeline.started, "the worker must not run during the grace window"
+
+ # Comfortably past the window, but still far below any test timeout.
+ await asyncio.sleep(grace)
+ assert pipeline.started, "the worker must start once the grace window elapses"
+
+ cancel.set()
+ await asyncio.wait_for(task, timeout=1.0)
+
+
+async def test_a_shutdown_inside_the_grace_window_returns_at_once() -> None:
+ """The grace must not delay shutdown — the regression this guards.
+
+ A blind sleep would keep stop() blocked for the rest of the window. With a
+ cancellable wait the helper returns immediately and never starts the worker.
+ """
+ pipeline = RecordingPipeline()
+ cancel = asyncio.Event()
+ # Long enough that waiting it out would blow the assertion below by 20x.
+ grace = 30.0
+
+ started_at = time.perf_counter()
+ task = asyncio.create_task(
+ UltraWikiService._run_pipeline_after_grace(pipeline, cancel, grace)
+ )
+ await asyncio.sleep(0)
+ cancel.set()
+
+ await asyncio.wait_for(task, timeout=1.0)
+ elapsed = time.perf_counter() - started_at
+
+ assert not pipeline.started, "a worker cancelled mid-grace must never run"
+ assert elapsed < 1.0, (
+ f"shutdown waited {elapsed:.2f}s — the grace period is blocking stop(), "
+ "which means it is a sleep rather than a wait on the cancel event"
+ )
+
+
[email protected]("grace", [0.0, 0.05])
+async def test_the_helper_always_hands_control_to_the_worker_it_was_given(
+ grace: float,
+) -> None:
+ """Whatever the grace, the worker object passed in is the one that runs."""
+ pipeline = RecordingPipeline()
+ cancel = asyncio.Event()
+
+ task = asyncio.create_task(
+ UltraWikiService._run_pipeline_after_grace(pipeline, cancel, grace)
+ )
+ await asyncio.sleep(grace + 0.05)
+
+ assert pipeline.started
+ cancel.set()
+ await asyncio.wait_for(task, timeout=1.0)