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: keep UltraWiki sources fresh automatically

Ruben Lütkee50d7b42bc60
diff --git a/jarvis/ultrawiki/connectors/custom_source.py b/jarvis/ultrawiki/connectors/custom_source.py
index 6354e72da2..7aa6d00545 100644
--- a/jarvis/ultrawiki/connectors/custom_source.py
+++ b/jarvis/ultrawiki/connectors/custom_source.py
@@ -408,6 +408,8 @@ class CustomSourceConnector:
# idempotent, so the worst case is repeated work, never duplicates.
incremental=IncrementalMode.CURSOR,
deletes=False,
+ refresh_interval_s=900.0,
+ reconcile_interval_s=86_400.0,
)
async def backfill(
diff --git a/jarvis/ultrawiki/connectors/jarvis_conversations.py b/jarvis/ultrawiki/connectors/jarvis_conversations.py
index 385bab89d9..44fd24a546 100644
--- a/jarvis/ultrawiki/connectors/jarvis_conversations.py
+++ b/jarvis/ultrawiki/connectors/jarvis_conversations.py
@@ -181,6 +181,8 @@ class JarvisConversationsConnector:
backfill=True,
incremental=IncrementalMode.CURSOR,
deletes=False,
+ refresh_interval_s=60.0,
+ reconcile_interval_s=86_400.0,
)
# ------------------------------------------------------------------
diff --git a/jarvis/ultrawiki/connectors/local_folder.py b/jarvis/ultrawiki/connectors/local_folder.py
index 4bb13eef4e..8406267c96 100644
--- a/jarvis/ultrawiki/connectors/local_folder.py
+++ b/jarvis/ultrawiki/connectors/local_folder.py
@@ -327,6 +327,8 @@ class LocalFolderConnector:
backfill=True,
incremental=IncrementalMode.CURSOR,
deletes=True,
+ refresh_interval_s=300.0,
+ reconcile_interval_s=86_400.0,
)
DEFAULT_EXTENSIONS: tuple[str, ...] = (
diff --git a/jarvis/ultrawiki/connectors/plugin_bridge.py b/jarvis/ultrawiki/connectors/plugin_bridge.py
index 942aadd7ff..bad4193e62 100644
--- a/jarvis/ultrawiki/connectors/plugin_bridge.py
+++ b/jarvis/ultrawiki/connectors/plugin_bridge.py
@@ -283,6 +283,8 @@ class PluginBridgeConnector:
backfill=True,
incremental=IncrementalMode.NONE, # prototype: freshness = re-run backfill
deletes=False,
+ refresh_interval_s=3_600.0,
+ reconcile_interval_s=86_400.0,
)
async def backfill(
diff --git a/jarvis/ultrawiki/service.py b/jarvis/ultrawiki/service.py
index 4f0cee20e0..a18ba83e47 100644
--- a/jarvis/ultrawiki/service.py
+++ b/jarvis/ultrawiki/service.py
@@ -91,6 +91,12 @@ SYNC_PHASES: tuple[str, ...] = (
#: Items per ``upsert_items`` transaction / sync-state checkpoint.
SYNC_CHUNK_SIZE = 200
+#: Source freshness runs off the boot path and wakes cheaply to find due work.
+#: Connector capabilities own the actual per-source cadence.
+FRESHNESS_STARTUP_GRACE_S = 30.0
+FRESHNESS_TICK_S = 30.0
+FRESHNESS_MAX_CONCURRENT_SYNCS = 2
+
#: What a plugin-bridge source is told when its integration has no pull
#: adapter yet: the sync really ran, it really imported nothing, and that is
#: not the user's fault or a broken credential.
@@ -158,6 +164,31 @@ def _iso_now() -> str:
return datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%SZ")
+def _seconds_since(value: Any, now: datetime) -> float | None:
+ """Age of an ISO timestamp, or ``None`` when absent/unusable."""
+ text = str(value or "").strip()
+ if not text:
+ return None
+ try:
+ parsed = datetime.fromisoformat(text.replace("Z", "+00:00"))
+ except ValueError:
+ return None
+ if parsed.tzinfo is None:
+ parsed = parsed.replace(tzinfo=UTC)
+ return max(0.0, (now - parsed.astimezone(UTC)).total_seconds())
+
+
+def _is_due(value: Any, interval_s: Any, now: datetime) -> bool:
+ try:
+ interval = float(interval_s)
+ except (TypeError, ValueError):
+ return False
+ if interval <= 0:
+ return False
+ age = _seconds_since(value, now)
+ return age is None or age >= interval
+
+
def _counts_dict(counts: PipelineCounts) -> dict[str, int]:
return {**dataclasses.asdict(counts), "total": counts.total}
@@ -359,6 +390,7 @@ class UltraWikiService:
self._backend_in_use = ""
self._pipeline: Any = None
self._pipeline_task: asyncio.Task[None] | None = None
+ self._freshness_task: asyncio.Task[None] | None = None
self._cancel_event: asyncio.Event | None = None
self._start_lock = asyncio.Lock()
self._sync_tasks: dict[str, asyncio.Task[None]] = {}
@@ -478,6 +510,20 @@ class UltraWikiService:
),
name="ultrawiki-pipeline",
)
+ if (
+ self._uw_enabled()
+ and self._cancel_event is not None
+ and self._freshness_task is None
+ ):
+ freshness_grace = max(
+ FRESHNESS_STARTUP_GRACE_S, float(pipeline_grace_s or 0.0)
+ )
+ self._freshness_task = asyncio.create_task(
+ self._run_freshness_after_grace(
+ self._cancel_event, freshness_grace
+ ),
+ name="ultrawiki-source-freshness",
+ )
@staticmethod
async def _run_pipeline_after_grace(
@@ -499,6 +545,111 @@ class UltraWikiService:
return
await pipeline.run(cancel_event)
+ async def _run_freshness_after_grace(
+ self, cancel_event: asyncio.Event, grace_s: float
+ ) -> None:
+ """Schedule due source reads without entering the boot critical path."""
+ try:
+ await asyncio.wait_for(cancel_event.wait(), timeout=max(0.0, grace_s))
+ except TimeoutError:
+ pass
+ if cancel_event.is_set():
+ return
+ while not cancel_event.is_set():
+ try:
+ await self._sync_due_sources()
+ except asyncio.CancelledError:
+ raise
+ except Exception: # noqa: BLE001 — one scheduler tick cannot kill freshness
+ log.warning("UltraWiki source freshness tick failed", exc_info=True)
+ try:
+ await asyncio.wait_for(
+ cancel_event.wait(), timeout=FRESHNESS_TICK_S
+ )
+ except TimeoutError:
+ pass
+
+ async def _sync_due_sources(
+ self, *, now: datetime | None = None
+ ) -> list[str]:
+ """Start due connector jobs and return their ids.
+
+ The helper is intentionally a single scheduler for every connector.
+ Capability declarations choose cadence and mode; connector code never
+ owns timers. Recent failed attempts count as attempts too, preventing a
+ broken remote source from being hammered every scheduler tick.
+ """
+ if not self._uw_enabled() or self._store is None:
+ return []
+ instant = (now or datetime.now(UTC)).astimezone(UTC)
+ store = self._require_store()
+ sources = await store.list_sources()
+ active_count = sum(
+ 1 for job in _JOBS.values() if job.status in JOB_ACTIVE_STATUSES
+ )
+ started: list[str] = []
+ for source in sources:
+ if active_count >= FRESHNESS_MAX_CONCURRENT_SYNCS:
+ break
+ source_id = str(source.get("id") or "")
+ if (
+ not source_id
+ or source.get("consent") != ConsentState.APPROVED.value
+ or not source.get("enabled", False)
+ or _active_job_for(source_id) is not None
+ ):
+ continue
+ try:
+ connector = self._build_connector(
+ str(source.get("connector") or "")
+ )
+ except ValueError as exc:
+ log.warning(
+ "UltraWiki freshness skipped source %s: %s", source_id, exc
+ )
+ continue
+ capabilities = getattr(connector, "capabilities", None)
+ refresh_s = getattr(capabilities, "refresh_interval_s", None)
+ reconcile_s = getattr(capabilities, "reconcile_interval_s", None)
+ if refresh_s is None and reconcile_s is None:
+ continue
+ sync_state = await store.get_sync_state(source_id) or {}
+ last_success = sync_state.get("last_success_at") or source.get(
+ "last_sync_at"
+ )
+ refresh_due = _is_due(last_success, refresh_s, instant)
+ reconcile_due = _is_due(
+ sync_state.get("backfill_complete_at"), reconcile_s, instant
+ )
+ if not refresh_due and not reconcile_due:
+ continue
+ intervals = [
+ float(value)
+ for value in (refresh_s, reconcile_s)
+ if value is not None and float(value) > 0
+ ]
+ retry_interval = min(intervals) if intervals else FRESHNESS_TICK_S
+ if not _is_due(
+ sync_state.get("last_outcome_at"), retry_interval, instant
+ ):
+ continue
+ mode = getattr(
+ capabilities, "incremental", IncrementalMode.NONE
+ )
+ full = reconcile_due or mode == IncrementalMode.NONE
+ try:
+ job_id = await self.start_sync(source_id, full=full)
+ except (SyncAlreadyRunningError, ValueError) as exc:
+ log.info(
+ "UltraWiki freshness could not start source %s: %s",
+ source_id,
+ exc,
+ )
+ continue
+ started.append(job_id)
+ active_count += 1
+ return started
+
async def _open_store(self) -> Any:
from jarvis.ultrawiki import store as store_mod # noqa: PLC0415 — lazy
@@ -543,6 +694,16 @@ class UltraWikiService:
async with self._start_lock:
if self._cancel_event is not None:
self._cancel_event.set()
+ freshness_task = self._freshness_task
+ if freshness_task is not None:
+ freshness_task.cancel()
+ try:
+ await asyncio.wait_for(freshness_task, timeout=2.0)
+ except (TimeoutError, asyncio.CancelledError):
+ pass
+ except Exception as exc: # noqa: BLE001 — shutdown best-effort
+ log.warning("UltraWiki freshness shutdown error: %s", exc)
+ self._freshness_task = None
task = self._pipeline_task
if task is not None:
task.cancel()
diff --git a/jarvis/ultrawiki/types.py b/jarvis/ultrawiki/types.py
index e9774cf2d0..aed60a5a8c 100644
--- a/jarvis/ultrawiki/types.py
+++ b/jarvis/ultrawiki/types.py
@@ -145,11 +145,19 @@ class RawItem:
@dataclass(frozen=True, slots=True)
class ConnectorCapabilities:
"""Declaration that drives the scheduler; connector authors never
- write scheduling logic themselves."""
+ write scheduling logic themselves.
+
+ ``refresh_interval_s`` asks the runtime to run the cheapest available
+ incremental read. ``reconcile_interval_s`` asks for a from-scratch walk
+ that repairs missed edits and, when ``deletes`` is true, tombstones
+ source-side deletions. ``None`` means manual-only for that lane.
+ """
backfill: bool = True
incremental: IncrementalMode = IncrementalMode.NONE
deletes: bool = False
+ refresh_interval_s: float | None = None
+ reconcile_interval_s: float | None = None
@dataclass(slots=True)
diff --git a/tests/unit/ultrawiki/test_service.py b/tests/unit/ultrawiki/test_service.py
index db43f6a252..e145ebc125 100644
--- a/tests/unit/ultrawiki/test_service.py
+++ b/tests/unit/ultrawiki/test_service.py
@@ -11,6 +11,7 @@ from __future__ import annotations
import asyncio
import time
+from datetime import UTC, datetime, timedelta
from pathlib import Path
from types import SimpleNamespace
from typing import Any
@@ -92,6 +93,18 @@ class FakeConnector:
yield item
+class ScheduledFakeConnector(FakeConnector):
+ """Fake with both scheduler lanes enabled."""
+
+ capabilities = ConnectorCapabilities(
+ backfill=True,
+ incremental=IncrementalMode.CURSOR,
+ deletes=True,
+ refresh_interval_s=60.0,
+ reconcile_interval_s=86_400.0,
+ )
+
+
class ExportFileConnector:
"""Backfill-only fake (IncrementalMode.NONE) — the export-file shape."""
@@ -289,6 +302,61 @@ async def test_approved_sync_ingests_with_checkpoints_and_cursor(service, monkey
assert ("incremental", "1449") in calls
+async def test_freshness_scheduler_runs_incremental_then_full_reconcile(
+ service, monkeypatch
+):
+ """Approved sources stay fresh without pressing Import again."""
+ calls: list[tuple[str, Any]] = []
+ patch_connectors(
+ monkeypatch,
+ {"scheduled": lambda: ScheduledFakeConnector(make_items(3), calls)},
+ )
+ source = await service.add_source("scheduled", "Scheduled Source")
+ await service.approve_source(source["id"], auto_sync=False)
+ await wait_for_job(
+ service, await service.start_sync(source["id"], full=True)
+ )
+
+ assert await service._sync_due_sources(now=datetime.now(UTC)) == [] # noqa: SLF001
+
+ incremental_at = datetime.now(UTC) + timedelta(minutes=2)
+ [incremental_id] = await service._sync_due_sources( # noqa: SLF001
+ now=incremental_at
+ )
+ incremental = await wait_for_job(service, incremental_id)
+ assert incremental["mode"] == "incremental"
+
+ reconcile_at = datetime.now(UTC) + timedelta(days=2)
+ [reconcile_id] = await service._sync_due_sources( # noqa: SLF001
+ now=reconcile_at
+ )
+ reconcile = await wait_for_job(service, reconcile_id)
+ assert reconcile["mode"] == "backfill"
+ assert [kind for kind, _cursor in calls] == [
+ "backfill",
+ "incremental",
+ "backfill",
+ ]
+
+
+async def test_manual_export_source_is_not_polled(service, monkeypatch):
+ calls: list[tuple[str, Any]] = []
+ patch_connectors(
+ monkeypatch,
+ {"export-conn": lambda: ExportFileConnector(make_items(2), calls)},
+ )
+ source = await service.add_source("export-conn", "Export File")
+ await service.approve_source(source["id"], auto_sync=False)
+
+ assert (
+ await service._sync_due_sources( # noqa: SLF001
+ now=datetime.now(UTC) + timedelta(days=30)
+ )
+ == []
+ )
+ assert calls == []
+
+
# ---------------------------------------------------------------------------
# Cancellation
# ---------------------------------------------------------------------------
@@ -334,7 +402,9 @@ async def test_shutdown_leaves_no_stray_tasks(service, monkeypatch):
)
await service.activate({})
pipeline_task = service._pipeline_task
+ freshness_task = service._freshness_task
assert pipeline_task is not None and not pipeline_task.done()
+ assert freshness_task is not None and not freshness_task.done()
source = await service.add_source("blocking-conn", "Blocked Source")
await service.approve_source(source["id"], auto_sync=False)
@@ -345,8 +415,10 @@ async def test_shutdown_leaves_no_stray_tasks(service, monkeypatch):
await service.shutdown()
assert pipeline_task.done()
+ assert freshness_task.done()
assert sync_task.done()
assert service._pipeline_task is None
+ assert service._freshness_task is None
assert service._sync_tasks == {}
assert service._store is None
snap = service.job_snapshot(job_id)