Device state: warm the slow caches in the background after login

Warm opens are instant, but the first open in a session still paid the
one-time reads (contacts, the channel-slot probe) in the navigation path —
so opening the menu after login, then the first Chat, read as two separate
stalls. Fold them into one quiet wait behind the menu instead.

DeviceState.prewarm() spawns a tracked background task that fetches contacts
then the channel slots (sequential — concurrent reads collide on the BLE
UART), best-effort and cancelled on reset/close. _resume_monitor kicks it off
once the link is up (only when connected — a deferred connect warms lazily on
first use). A getter reached before the warm finishes awaits the same
in-flight fetch, so it never doubles a read.

The channel probe itself still walks every slot on firmware that doesn't
reject out-of-range indices; bounding that is a separate change, deferred on
purpose — a long one-time poll behind the menu is fine for now.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Opus 4.8 committed 2026-07-13 23:03:22 -04:00
1 parent e07ef3b379
commit 2f0e2687b0
3 files changed
+55

No files matched your search

+27
View File
@@ -187,6 +187,33 @@ class DeviceState:
self._channels = await read_channel_slots(device)
return self._channels
# -- prewarm (fill the slow caches in the background, off the navigation path) --
def prewarm(self) -> None:
"""Warm the slow caches (contacts, channel slots) in the background after connect.
Called once the session's link is up (see :func:`meshterm.ui.menu._resume_monitor`) so
the first screen that reads them — Chat, Trace, the Dashboard — is served from cache
instantly, rather than paying the round-trips in the navigation path where the user is
waiting on the screen to open. It folds the two unavoidable first reads into one quiet
wait behind the menu instead of surfacing them on the first open.
The work runs as a tracked background task: **sequential** (never gathered — concurrent
reads collide on the BLE UART; see the module note), best-effort (a failure just leaves
the cache cold for a normal lazy fetch later), and cancelled on :meth:`reset` /
:meth:`aclose`. If a getter is reached before this finishes, it awaits the *same*
in-flight fetch — the per-fetch locks dedupe — so prewarming never doubles a read.
"""
self._spawn(self._prewarm())
async def _prewarm(self) -> None:
"""Fetch the slow caches one after another, swallowing failures (best-effort warm)."""
for label, fetch in (("contacts", self.contacts), ("channel slots", self.channel_slots)):
try:
await fetch()
except Exception as exc: # noqa: BLE001 - a warm miss just falls back to a lazy fetch
self._ctx.log.debug("devstate: prewarm of %s failed: %s", label, exc)
# -- invalidation (called by the code that writes device state) ------------
def invalidate_contacts(self) -> None:
+7
View File
@@ -612,6 +612,13 @@ async def _resume_monitor(ctx: AppContext) -> None:
await ctx.chat.start()
except Exception: # noqa: BLE001 - surface via the header, don't crash the menu
pass
# Warm the slow session caches (contacts, the channel-slot probe) behind the menu now that
# the link is up, so the first Chat/Trace/Dashboard open is served from cache instead of
# paying those round-trips in the navigation path — one quiet wait after login rather than a
# stall on the first open (see DeviceState.prewarm). Only with a live link: a deferred
# connect (connect_on_start off) returns above and warms lazily on first use instead.
if ctx.is_connected:
ctx.devstate.prewarm()
@asynccontextmanager
+21
View File
@@ -158,3 +158,24 @@ def test_reset_clears_everything() -> None:
assert dev.self_info_calls == 2
assert dev.contacts_calls == 2
assert dev.mode_calls == 2
def test_prewarm_fills_the_slow_caches_off_the_read_path() -> None:
"""prewarm() warms contacts and channels in the background so the first read hits no wire."""
dev = FakeDevice()
ds = _devstate(dev)
async def run() -> None:
ds.prewarm()
await asyncio.gather(*list(ds._tasks)) # let the background warm finish
# Both slow caches were filled by the prewarm: contacts once, the channel probe once
# (idx 0 ok, idx 1 rejected).
assert dev.contacts_calls == 1
assert dev.channel_calls == 2
# A screen opening now is served from cache — no additional round-trips.
await ds.contacts()
await ds.channel_slots()
assert dev.contacts_calls == 1
assert dev.channel_calls == 2
asyncio.run(run())