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(marketplace): propagate refreshed tokens into the live MCP session so plugins survive token expiry

Ruben Lütke5e98ce607e6e
diff --git a/jarvis/marketplace/refresh_scheduler.py b/jarvis/marketplace/refresh_scheduler.py
index 399597c2f0..8545199f8a 100644
--- a/jarvis/marketplace/refresh_scheduler.py
+++ b/jarvis/marketplace/refresh_scheduler.py
@@ -101,6 +101,7 @@ async def refresh_due_tokens(
*,
threshold_seconds: int = 600,
keep_alive_seconds: int | None = None,
+ on_refreshed: Callable[[str], None] | None = None,
) -> dict[str, str]:
"""Refresh every connected plugin whose token is due.
@@ -109,6 +110,12 @@ async def refresh_due_tokens(
the keep-alive sweep that keeps refresh tokens warm so a connection survives
forever, even for providers whose access token is long-lived / never expires.
+ ``on_refreshed``, when given, fires once per plugin id right after its fresh
+ token is saved — the hook the live MCP session uses to pick up the new token
+ instead of continuing to 401 with the stale one. A callback failure is
+ swallowed and logged; a UI-refresh hiccup must never break the token save
+ that already succeeded, nor take down the rest of the cycle.
+
Returns a ``{plugin_id: outcome}`` map (outcomes from the module constants).
Never raises — a single plugin's failure is isolated so one dead connection
cannot stall the whole cycle.
@@ -165,6 +172,12 @@ async def refresh_due_tokens(
dataclasses.replace(new_tokens, extra=merged_extra, needs_reauth=False),
)
outcomes[pid] = REFRESHED
+
+ if on_refreshed is not None:
+ try:
+ on_refreshed(pid)
+ except Exception as exc: # noqa: BLE001 — a UI-refresh hiccup must not kill the loop
+ log.warning("refresh: on_refreshed callback failed for %s: %s", pid, exc)
return outcomes
@@ -180,6 +193,7 @@ class RefreshScheduler:
interval_seconds: float = 300.0,
threshold_seconds: int = 600,
keep_alive_seconds: int | None = 43_200, # 12h — keep refresh tokens warm
+ on_refreshed: Callable[[str], None] | None = None,
) -> None:
self._plugin_ids_fn = plugin_ids_fn
self._store = store
@@ -187,6 +201,7 @@ class RefreshScheduler:
self._interval = interval_seconds
self._threshold = threshold_seconds
self._keep_alive_seconds = keep_alive_seconds
+ self._on_refreshed = on_refreshed
self._task: asyncio.Task[None] | None = None
async def run_once(self) -> dict[str, str]:
@@ -196,6 +211,7 @@ class RefreshScheduler:
self._build_handler,
threshold_seconds=self._threshold,
keep_alive_seconds=self._keep_alive_seconds,
+ on_refreshed=self._on_refreshed,
)
async def _loop(self) -> None:
diff --git a/jarvis/ui/web/launcher.py b/jarvis/ui/web/launcher.py
index 3e91563dd4..14da19052c 100644
--- a/jarvis/ui/web/launcher.py
+++ b/jarvis/ui/web/launcher.py
@@ -694,11 +694,19 @@ async def _run_headless(args) -> int:
from jarvis.marketplace.refresh_scheduler import RefreshScheduler
from jarvis.marketplace.token_store import TokenStore
+ def _refresh_live_session(plugin_id: str) -> None:
+ # Lazy import: keeps this off the boot-critical import path (AP-26) —
+ # it is only ever needed once a background refresh actually succeeds.
+ from jarvis.ui.web.marketplace_routes import _refresh_plugin_in_live_registry
+
+ _refresh_plugin_in_live_registry(plugin_id)
+
_token_store = TokenStore()
_refresh_scheduler = RefreshScheduler(
plugin_ids_fn=lambda: connected_plugin_ids(_token_store),
store=_token_store,
build_handler=build_handler_from_catalog,
+ on_refreshed=_refresh_live_session,
)
_refresh_scheduler.start()
server.app.state.refresh_scheduler = _refresh_scheduler
diff --git a/tests/unit/marketplace/test_refresh_scheduler.py b/tests/unit/marketplace/test_refresh_scheduler.py
index 6402c1fd47..b8ae4f87ca 100644
--- a/tests/unit/marketplace/test_refresh_scheduler.py
+++ b/tests/unit/marketplace/test_refresh_scheduler.py
@@ -313,6 +313,54 @@ def test_log_cycle_no_warning_when_all_healthy(caplog) -> None: # noqa: ANN001
assert not [r for r in caplog.records if r.levelno == logging.WARNING]
+async def test_on_refreshed_fires_after_successful_refresh() -> None:
+ # The scheduler writes the fresh token to the store, but a live MCP client
+ # session (e.g. an open Notion connection) never learns the token rotated
+ # unless something tells it to. on_refreshed is that "something" — fired
+ # once per plugin id right after its token save succeeds.
+ store = _store()
+ store.save("notion", _tokens(60))
+ handler = _FakeHandler("notion", new_tokens=Tokens(access="a1", refresh="r1"))
+ seen: list[str] = []
+ sched = RefreshScheduler(
+ plugin_ids_fn=lambda: ["notion"],
+ store=store,
+ build_handler=lambda pid: handler,
+ on_refreshed=seen.append,
+ )
+
+ outcomes = await sched.run_once()
+
+ assert outcomes == {"notion": REFRESHED}
+ assert seen == ["notion"]
+
+
+async def test_on_refreshed_exception_does_not_break_the_loop(caplog) -> None: # noqa: ANN001
+ # A UI-refresh hiccup (e.g. the live registry lookup throwing) must never
+ # take down the refresh cycle itself — the token is already safely saved.
+ store = _store()
+ store.save("notion", _tokens(60))
+ handler = _FakeHandler("notion", new_tokens=Tokens(access="a1", refresh="r1"))
+
+ def _boom(plugin_id: str) -> None:
+ raise RuntimeError("ui refresh failed")
+
+ sched = RefreshScheduler(
+ plugin_ids_fn=lambda: ["notion"],
+ store=store,
+ build_handler=lambda pid: handler,
+ on_refreshed=_boom,
+ )
+
+ caplog.set_level(logging.WARNING)
+ outcomes = await sched.run_once() # must not raise
+
+ assert outcomes == {"notion": REFRESHED}
+ assert store.load("notion").access == "a1" # token save is unaffected
+
+
@pytest.mark.asyncio
async def test_scheduler_keep_alive_refreshes_long_lived_token() -> None:
# The scheduler must apply keep-alive so a long-lived / no-expiry token still