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.

chore: auto-save WIP via Claude stop-hook safety net (2026-07-01 13:58)

Ruben Lütke422d68f74727
diff --git a/jarvis/plugins/tool/_http_pool.py b/jarvis/plugins/tool/_http_pool.py
new file mode 100644
index 0000000000..6a4b55d2fd
--- /dev/null
+++ b/jarvis/plugins/tool/_http_pool.py
@@ -0,0 +1,57 @@
+"""Shared connection-pool helper for the REST-backed router tools.
+
+Opening a fresh ``httpx.AsyncClient`` per request forces a new TCP + TLS
+handshake to the same host on every call. The REST plugins (gmail, vercel)
+issue several such calls per user turn and across a session, so that repeated
+handshake is a real, avoidable slice of the "plugin call takes ages" latency.
+This helper hands back ONE client per tool instance and keeps its connections
+warm (HTTP keep-alive) across calls.
+
+The client is rebound whenever the running event loop changes, so a cached
+client is never reused across loops — each ``pytest-asyncio`` test runs in its
+own loop, and a tool instance reused across loops would otherwise raise
+``RuntimeError: Event loop is closed``.
+"""
+from __future__ import annotations
+
+from typing import Any
+
+
+class HttpClientPool:
+ """Lazily create and reuse one ``httpx.AsyncClient`` (keep-alive pool).
+
+ ``transport`` is forwarded verbatim so tests can inject an
+ ``httpx.MockTransport`` exactly as they did with the per-request clients.
+ """
+
+ def __init__(
+ self, *, timeout_s: float = 20.0, transport: Any | None = None
+ ) -> None:
+ self._timeout_s = timeout_s
+ self._transport = transport
+ self._client: Any | None = None
+ self._loop: Any | None = None
+
+ def client(self) -> Any:
+ """Return the pooled client, (re)binding it to the current loop."""
+ import asyncio
+
+ import httpx
+
+ loop = asyncio.get_running_loop()
+ client = self._client
+ if client is None or self._loop is not loop or client.is_closed:
+ client = httpx.AsyncClient(
+ timeout=self._timeout_s, transport=self._transport
+ )
+ self._client = client
+ self._loop = loop
+ return client
+
+ async def aclose(self) -> None:
+ """Close the pooled client (best-effort; safe to call repeatedly)."""
+ client = self._client
+ self._client = None
+ self._loop = None
+ if client is not None and not client.is_closed:
+ await client.aclose()
diff --git a/jarvis/plugins/tool/gmail_rest.py b/jarvis/plugins/tool/gmail_rest.py
index bc41f12136..1d190c4d7b 100644
--- a/jarvis/plugins/tool/gmail_rest.py
+++ b/jarvis/plugins/tool/gmail_rest.py
@@ -172,9 +172,16 @@ class GmailRestTool:
transport: Any | None = None,
token_refresher: Callable[[], Awaitable[bool]] | None = None,
) -> None:
+ from ._http_pool import HttpClientPool
+
self._token_provider = access_token_provider or _default_token_provider
self._transport = transport
self._refresher = token_refresher or _default_refresher
+ # Keep-alive pool: reuse one client (one warm TLS connection to Gmail)
+ # across list/get/send instead of a fresh handshake per request. The
+ # tool is built once per BrainManager, so the connection stays warm for
+ # the whole session (a read is list_messages + get_message = 2 hops).
+ self._pool = HttpClientPool(transport=transport)
# -- internal helpers ---------------------------------------------------
@@ -216,18 +223,14 @@ class GmailRestTool:
return await do_call(headers)
async def _get(self, path: str, params: dict[str, Any], headers: dict[str, str]):
- import httpx
-
- async with httpx.AsyncClient(timeout=20.0, transport=self._transport) as client:
- resp = await client.get(f"{_GMAIL_BASE}{path}", params=params, headers=headers)
+ client = self._pool.client()
+ resp = await client.get(f"{_GMAIL_BASE}{path}", params=params, headers=headers)
resp.raise_for_status()
return resp.json()
async def _post(self, path: str, json_body: dict[str, Any], headers: dict[str, str]):
- import httpx
-
- async with httpx.AsyncClient(timeout=20.0, transport=self._transport) as client:
- resp = await client.post(f"{_GMAIL_BASE}{path}", json=json_body, headers=headers)
+ client = self._pool.client()
+ resp = await client.post(f"{_GMAIL_BASE}{path}", json=json_body, headers=headers)
resp.raise_for_status()
return resp.json()
diff --git a/tests/unit/speech/test_rolling_whisper_wake_quiet_mic.py b/tests/unit/speech/test_rolling_whisper_wake_quiet_mic.py
index 6bd9a535e5..6f65bb56db 100644
--- a/tests/unit/speech/test_rolling_whisper_wake_quiet_mic.py
+++ b/tests/unit/speech/test_rolling_whisper_wake_quiet_mic.py
@@ -89,6 +89,56 @@ async def test_very_quiet_wake_below_legacy_peak_gate_reaches_whisper() -> None:
pass
+# The custom-word path is transcription + fuzzy match, so it is WORD-AGNOSTIC:
+# whatever the user types is transcribed and compared, never a fixed vocabulary.
+# This proves it across a deliberately diverse spread — a 3-char name (short-token
+# tolerance), long names, a German noun with an umlaut (diacritics folding), an
+# English noun, and a two-word "Hey X" phrase (prefix stripped) — including the
+# realistic case where the small model mishears one character.
+ ("phrase", "transcript"),
+ [
+ ("Leo", "leo"), # 3-char name — short-token relaxed ratio
+ ("Ruben", "ruben"), # name
+ ("Herz", "herz"), # German common noun
+ ("Björn", "björn"), # umlaut name — diacritics folded for match
+ ("Athena", "athena"), # longer name
+ ("Computer", "computer"), # English noun
+ ("Hey Zeus", "hey zeus"), # two-word phrase, prefix stripped
+ ("Ruben", "ruhben"), # small-model one-char mishearing still matches
+ ],
+)
+async def test_arbitrary_custom_wakes_are_word_agnostic(
+ phrase: str, transcript: str
+) -> None:
+ """ANY user-chosen wake word triggers at quiet volume — the level fix is
+ pegel-based (word-independent) and the matcher transcribes + fuzzy-compares
+ rather than using a fixed vocabulary. No word is special-cased."""
+ stt = _PhraseSTT(transcript)
+ wake = RollingWhisperWake(
+ stt,
+ pattern=compile_wake_matcher(phrase),
+ poll_interval_s=0.01,
+ cooldown_s=0.0,
+ save_debug_wavs=False,
+ )
+ src: asyncio.Queue = asyncio.Queue()
+ stop = asyncio.Event()
+ feeder = asyncio.create_task(_feed_until(src, stop, _const_chunk(295))) # peak ~0.009
+ try:
+ kw = await asyncio.wait_for(_first_keyword(wake, src), timeout=3.0)
+ assert kw, f"{phrase!r} did not wake at quiet volume"
+ assert stt.calls >= 1, f"{phrase!r} window never reached Whisper"
+ finally:
+ stop.set()
+ await src.put(None)
+ feeder.cancel()
+ try:
+ await feeder
+ except (asyncio.CancelledError, Exception): # noqa: BLE001, S110
+ pass
+
+
@pytest.mark.parametrize(
("phrase", "transcript"),
[