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.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
perf(config): stop rebuilding the config model per provider client
Ruben Lütke0d10a37673ad
diff --git a/jarvis/core/config.py b/jarvis/core/config.pyindex 61a2ea42e0..95929d4717 100644--- a/jarvis/core/config.py+++ b/jarvis/core/config.py@@ -333,6 +333,19 @@ class TriggerConfig(BaseModel):# Hangup key. Was hardcoded ("f1+f2",) at the SpeechPipeline call sites; now# user-editable via /api/settings/keybinds. Read directly at bootstrap.hotkey_hangup: str = "f1+f2"+ # Dictation key: hold (or toggle, see [dictation].mode) to speak; the+ # transcript is inserted into whatever text field currently has focus.+ #+ # Deliberately EMPTY by default. There is no combination that is free on+ # every machine and every OS — the well-known ones are refused by+ # ``validate_hotkey`` (Windows key, Ctrl+C, Alt+F4, the macOS Command+ # shortcuts), and silently claiming a chord the user needs elsewhere is the+ # worse failure. The setup wizard and Settings → Keybinds ask for it.+ # Unbound is a valid, fully usable state: dictation still starts from the+ # bar, from the UI, and from ``jarvis api dictation start`` — the last of+ # which is the documented Wayland path, where the compositor (not the app)+ # owns global shortcuts.+ hotkey_dictate: str = ""wake_word: WakeWordConfig = Field(default_factory=WakeWordConfig)# When false (default), the pipeline keeps the mic open after the# response (conversation mode) and only hangs up via HANGUP_RE, the idle@@ -2432,6 +2445,88 @@ class SpeechConfig(BaseModel):vad_silence_ms: int = Field(default=1500, ge=500, le=5000)+class DictationConfig(BaseModel):+ """Dictation mode: speak into whatever text field currently has focus.++ TOML path: ``[dictation]`` · Attribute path: ``JarvisConfig.dictation``++ The shortcut itself lives in ``[trigger].hotkey_dictate`` with every other+ keybind; this block owns the behaviour. ``extra="allow"`` keeps the self-mod+ pre-validate pipeline safe when a future key is added (AP-16).+ """++ model_config = ConfigDict(extra="allow")++ #: ``hold`` = record while the key is down, submit on release (the mode+ #: every reference tool ships and the one people expect). ``toggle`` = one+ #: press starts, the next stops — kept for accessibility and long dictations+ #: where holding a key for minutes is not reasonable.+ mode: Literal["hold", "toggle"] = "hold"++ #: Where the finished transcript goes. ``auto`` inserts into the focused+ #: field of whatever application is in front, EXCEPT when that application+ #: is Jarvis itself — then it goes to the app's own input, because inserting+ #: into the window the user just left is both surprising and unrecoverable.+ #: ``insert`` always inserts, ``chat`` never does (transcript event only).+ target: Literal["auto", "insert", "chat"] = "auto"++ #: ``clipboard`` writes the text, sends the paste chord and restores the+ #: previous clipboard — one keystroke regardless of length, and it survives+ #: editor autocomplete. ``type`` synthesises the text character by character;+ #: correct for the rare control that ignores paste, but ~40 ms per character+ #: on Windows and easily mangled by autocomplete, so it is opt-in.+ insert_method: Literal["clipboard", "type"] = "clipboard"++ #: Which paste chord to send. ``auto`` = Cmd+V on macOS, Ctrl+V elsewhere.+ #: Many terminals do not paste on Ctrl+V, which is why the two explicit+ #: alternatives exist.+ paste_chord: Literal["auto", "ctrl_v", "ctrl_shift_v", "shift_insert"] = "auto"++ #: Pause after writing the clipboard, before sending the chord. Too short+ #: and the target app pastes the PREVIOUS clipboard content.+ paste_delay_ms: int = Field(default=120, ge=0, le=2000)++ #: Pause after the chord, before restoring the previous clipboard. Too short+ #: and the target app has not read the clipboard yet.+ paste_delay_after_ms: int = Field(default=120, ge=0, le=2000)++ #: Put back whatever was on the clipboard before dictation. Text only — the+ #: platform clipboard layer does not carry images, so an image on the+ #: clipboard is lost either way; turning this off at least leaves the+ #: transcript there.+ restore_clipboard: bool = True++ #: Remove filler sounds with the deterministic per-language rules in+ #: ``jarvis.dictation.cleanup``. No model call, no rephrasing.+ remove_fillers: bool = True++ #: Safety ceiling for the cleanup: if the rules would drop more than this+ #: fraction of the words, the RAW transcript is used instead. A cleanup that+ #: eats a quarter of a sentence is a bug, not a cleanup.+ filler_max_removed_fraction: float = Field(default=0.25, ge=0.0, le=1.0)++ #: Hard cap on one dictation, in seconds. Nothing records forever.+ max_seconds: float = Field(default=300.0, gt=0.0, le=3600.0)++ #: Live-transcript refresh interval while speaking. ``0`` disables the live+ #: preview entirely (the final transcription still happens).+ partial_interval_s: float = Field(default=1.2, ge=0.0, le=10.0)++ #: Segment length for streaming-style transcription. The old dictation lane+ #: re-transcribed the whole growing buffer on every tick, which costs+ #: O(n²) audio-seconds and, on a paid API, real money. Closed segments are+ #: transcribed once and never again; only the open tail is re-sent.+ #: ``0`` restores the legacy full-buffer behaviour.+ segment_seconds: float = Field(default=8.0, ge=0.0, le=60.0)++ #: Keep a local history of dictations (raw + cleaned, for auditing what the+ #: cleanup changed). Dictated text is among the most sensitive data this app+ #: holds, so it is local-only, capped, and purgeable from the UI.+ history_enabled: bool = True+ history_max_entries: int = Field(default=200, ge=0, le=5000)+ history_retention_days: int = Field(default=30, ge=0, le=3650)++class MarketplaceConfig(BaseModel):"""Plugin-marketplace connect settings (OAuth redirect mode).@@ -2725,6 +2820,8 @@ class JarvisConfig(BaseModel):# Speech pipeline sub-configs (completeness classifier, …).# TOML path: [speech] / [speech.completeness]speech: SpeechConfig = Field(default_factory=SpeechConfig)+ # Dictation mode (hold to speak, transcript lands in the focused field).+ dictation: DictationConfig = Field(default_factory=DictationConfig)# Voice-flow knobs (incomplete-prompt completion buffer settings).# Spec: docs/superpowers/specs/2026-05-25-incomplete-prompt-completion-design.mdvoice: VoiceConfig = Field(default_factory=VoiceConfig)@@ -2784,15 +2881,21 @@ def _copy_toml_data(value: Any) -> Any:def clear_config_cache() -> None:- """Forget every parsed TOML payload.+ """Forget everything derived from the config file.- The cache invalidates itself off the file's identity, so this exists for- the two cases that identity cannot see: a test that rewrites a fixture+ Both caches invalidate themselves off the file's identity, so this exists+ for the two cases that identity cannot see: a test that rewrites a fixturewithin one filesystem timestamp tick, and :mod:`jarvis.core.config_writer`announcing a write it just made rather than waiting to be found out.++ The endpoint-routing cache is cleared with it, and must stay that way: it is+ derived from the same file, so anything that can leave one stale leaves the+ other stale too — and a stale route sends a provider's traffic to an address+ the user has already changed."""with _TOML_CACHE_LOCK:_TOML_CACHE.clear()+ _ENDPOINT_ROUTE_CACHE.clear()def _load_toml(path: Path) -> dict[str, Any]:@@ -3604,6 +3707,78 @@ class ResolvedEndpoint:via_proxy: bool+@dataclass(frozen=True)+class _EndpointRoute:+ """Where a provider's traffic goes — the part decided purely by config."""++ base_url: str | None+ via_proxy: bool+++#: Config-derived routing per (config identity, provider, vendor default).+#:+#: This is the OTHER half of the freeze whose TOML half ``_TOML_CACHE`` fixed.+#: ``resolve_provider_endpoint`` runs on every provider client build, on the+#: event loop, and a key-aware fallback chain builds several providers per turn.+#: Reaching ``load_config()`` for it rebuilt the whole ``JarvisConfig`` model+#: each time: 281 fresh objects per call, handed straight to the garbage+#: collector. Measured live on 2026-07-28 (two independent sessions, same+#: verdict): the backend thread sat ``active+gil`` inside ``JarvisConfig(**data)``+#: reached through exactly this function, and while it did, the Tk-drawn overlay+#: stopped pumping and Windows replaced the frozen window with a ``Ghost``+#: (BUG-118). Caching the model itself is not an option — a hundred sites mutate+#: the object they are handed — but the routing decision is small, immutable and+#: derived only from the file, so it can be remembered safely.+_ENDPOINT_ROUTE_CACHE: dict[+ tuple[tuple[int, int] | None, str, str | None], _EndpointRoute+] = {}+++def _endpoint_route(+ cfg_obj: JarvisConfig,+ provider_id: str,+ vendor_default_base_url: str | None,+) -> _EndpointRoute:+ """Pure routing decision for one provider — no secrets, no I/O."""+ team = cfg_obj.team_proxy+ if team.enabled and team.url and provider_id not in team.local_providers:+ return _EndpointRoute(+ base_url=f"{team.url.rstrip('/')}/p/{provider_id}", via_proxy=True+ )+ prov = cfg_obj.brain.providers.get(provider_id)+ override = prov.base_url if prov is not None and prov.base_url else None+ return _EndpointRoute(base_url=override or vendor_default_base_url, via_proxy=False)+++def _cached_endpoint_route(+ provider_id: str,+ vendor_default_base_url: str | None,+) -> _EndpointRoute:+ """The routing decision, without rebuilding the config model to get it.++ Keyed on the config file's identity, so an edit is picked up exactly as it+ was before — and ``clear_config_cache`` drops this alongside the parsed TOML,+ which is what ``config_writer`` announces after every write.+ """+ identity: tuple[int, int] | None = None+ try:+ info = resolve_config_path().stat()+ identity = (info.st_mtime_ns, info.st_size)+ except OSError:+ identity = None++ key = (identity, provider_id, vendor_default_base_url)+ if identity is not None:+ cached = _ENDPOINT_ROUTE_CACHE.get(key)+ if cached is not None:+ return cached++ route = _endpoint_route(load_config(), provider_id, vendor_default_base_url)+ if identity is not None:+ _ENDPOINT_ROUTE_CACHE[key] = route+ return route++def resolve_provider_endpoint(provider_id: str,*,@@ -3626,21 +3801,22 @@ def resolve_provider_endpoint(``{url}/p/{provider_id}`` and the credential becomes the per-user team token(``team_proxy_token``) — the same flip for every provider class."""- cfg_obj = config if config is not None else load_config()+ if config is not None:+ route = _endpoint_route(config, provider_id, vendor_default_base_url)+ else:+ route = _cached_endpoint_route(provider_id, vendor_default_base_url)- team = cfg_obj.team_proxy- if team.enabled and team.url and provider_id not in team.local_providers:- base_url = f"{team.url.rstrip('/')}/p/{provider_id}"+ # The credential is deliberately NOT part of what is remembered above. It+ # comes from the keyring and can be replaced, revoked or repaired while the+ # app runs — a cached one would keep a provider dead after the user fixed+ # its key in the UI, which is the opposite of what this project promises.+ if route.via_proxy:token = get_secret("team_proxy_token", "TEAM_PROXY_TOKEN")- return ResolvedEndpoint(base_url=base_url, credential=token, via_proxy=True)-- override: str | None = None- prov = cfg_obj.brain.providers.get(provider_id)- if prov is not None and prov.base_url:- override = prov.base_url- base_url = override or vendor_default_base_url+ return ResolvedEndpoint(base_url=route.base_url, credential=token, via_proxy=True)credential = get_provider_secret(provider_id)- return ResolvedEndpoint(base_url=base_url, credential=credential, via_proxy=False)+ return ResolvedEndpoint(+ base_url=route.base_url, credential=credential, via_proxy=False+ )def set_secret(key: str, value: str) -> bool:diff --git a/tests/conftest.py b/tests/conftest.pyindex 85e02cb7fe..c23770981c 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -123,6 +123,28 @@ def _reset_bus():reset_default_bus()[email protected](autouse=True)+def _reset_config_caches():+ """Start every test with a cold config cache.++ ``jarvis.core.config`` remembers the parsed TOML and the routing decision+ derived from it, both keyed on the config FILE's identity — which is what+ makes them safe in production and unsafe across tests: a suite that swaps+ the config by monkeypatching ``load_config`` changes nothing about any file,+ so without this the second test is answered with the first one's config.++ Cheap (two dict clears) and applied to every test rather than the ones that+ happen to need it today, because the failure mode is a test passing on the+ previous test's data — which is silent, order-dependent, and exactly what+ caught three model-catalog tests when the routing cache was introduced.+ """+ from jarvis.core.config import clear_config_cache++ clear_config_cache()+ yield+ clear_config_cache()++@pytest_asyncio.fixtureasync def fresh_bus():"""Fresh EventBus for each test."""diff --git a/tests/unit/core/test_provider_endpoint_cache.py b/tests/unit/core/test_provider_endpoint_cache.pynew file mode 100644index 0000000000..cc21d2cb56--- /dev/null+++ b/tests/unit/core/test_provider_endpoint_cache.py@@ -0,0 +1,165 @@+"""Routing cache in front of ``resolve_provider_endpoint``.++This is the second half of the 2026-07-28 freeze. The first half was the TOML+parse (``test_config_toml_cache.py``); this is the ``JarvisConfig(**data)``+rebuild behind it — 281 fresh objects on every provider client build, on the+event loop, several times per turn. Two independent sessions caught the backend+thread ``active+gil`` inside exactly that call, and while it was held the+Tk-drawn overlay stopped pumping and Windows swapped the frozen window for a+``Ghost`` (BUG-118).++Two things must never regress here, and they pull in opposite directions: the+routing decision has to be remembered, and the CREDENTIAL must not be — a key+the user repairs in the UI has to take effect on the next call, not the next+restart.+"""+from __future__ import annotations++import os+from pathlib import Path++import pytest++from jarvis.core import config as config_module+from jarvis.core.config import clear_config_cache, resolve_provider_endpoint++[email protected](autouse=True)+def _clean(monkeypatch, tmp_path):+ """Point the resolver at a config of our own, and start from cold."""+ target = tmp_path / "jarvis.toml"+ target.write_text('[brain]\nprimary = "gemini"\n', encoding="utf-8")+ monkeypatch.setattr(config_module, "resolve_config_path", lambda: target)+ clear_config_cache()+ yield target+ clear_config_cache()+++def _write(path: Path, body: str, *, mtime_ns: int | None = None) -> None:+ path.write_text(body, encoding="utf-8")+ if mtime_ns is not None:+ os.utime(path, ns=(mtime_ns, mtime_ns))+++def test_the_config_model_is_not_rebuilt_on_a_repeat_call(monkeypatch):+ """The whole point: a second call must not go near ``load_config``."""+ resolve_provider_endpoint("claude-api") # warm++ calls: list[int] = []+ real = config_module.load_config++ def counting(*args, **kwargs):+ calls.append(1)+ return real(*args, **kwargs)++ monkeypatch.setattr(config_module, "load_config", counting)+ resolve_provider_endpoint("claude-api")+ assert calls == [], "a cached route must not rebuild the config model"+++def test_the_credential_is_never_cached(monkeypatch):+ """A repaired key must work on the NEXT call, not the next restart."""+ seen: list[str] = []++ def fake_secret(provider_id: str) -> str | None:+ seen.append(provider_id)+ return f"key-{len(seen)}"++ monkeypatch.setattr(config_module, "get_provider_secret", fake_secret)++ first = resolve_provider_endpoint("claude-api")+ second = resolve_provider_endpoint("claude-api")++ assert first.credential == "key-1"+ assert second.credential == "key-2", "the credential was served from a cache"+ assert len(seen) == 2+++def test_a_changed_base_url_takes_effect(_clean):+ """Editing the config must still change where traffic goes."""+ target = _clean+ assert resolve_provider_endpoint("openrouter").base_url is None++ _write(+ target,+ '[brain.providers.openrouter]\nbase_url = "https://proxy.invalid/v1"\n',+ mtime_ns=10_000_000_000,+ )++ assert (+ resolve_provider_endpoint("openrouter").base_url == "https://proxy.invalid/v1"+ )+++def test_clear_drops_the_route_as_well(_clean):+ """``config_writer`` announces its writes — that must reach this cache too."""+ target = _clean+ assert resolve_provider_endpoint("openrouter").base_url is None++ # Same identity as far as the cache can tell: only clearing can save it.+ _write(target, '[brain.providers.openrouter]\nbase_url = "https://b.invalid"\n')+ os.utime(target, ns=(0, 0))+ clear_config_cache()++ assert resolve_provider_endpoint("openrouter").base_url == "https://b.invalid"+++def test_the_vendor_default_is_part_of_the_key(_clean):+ """Two callers with different defaults must not be served each other's."""+ a = resolve_provider_endpoint("openrouter", vendor_default_base_url="https://a.invalid")+ b = resolve_provider_endpoint("openrouter", vendor_default_base_url="https://b.invalid")+ assert a.base_url == "https://a.invalid"+ assert b.base_url == "https://b.invalid"+++def test_providers_do_not_share_an_entry(_clean):+ """Keyed per provider — one override must not leak onto another."""+ target = _clean+ _write(+ target,+ '[brain.providers.openrouter]\nbase_url = "https://only-openrouter.invalid"\n',+ mtime_ns=10_000_000_000,+ )+ assert (+ resolve_provider_endpoint("openrouter").base_url+ == "https://only-openrouter.invalid"+ )+ assert resolve_provider_endpoint("gemini").base_url is None+++def test_team_proxy_still_flips_every_provider(_clean, monkeypatch):+ """Team mode is config-derived too, so it must survive the cache."""+ target = _clean+ _write(+ target,+ "[team_proxy]\nenabled = true\nurl = \"https://team.invalid/\"\n"+ "local_providers = [\"ollama\"]\n",+ mtime_ns=10_000_000_000,+ )+ monkeypatch.setattr(config_module, "get_secret", lambda *a, **k: "team-token")++ routed = resolve_provider_endpoint("claude-api")+ assert routed.via_proxy is True+ assert routed.base_url == "https://team.invalid/p/claude-api"+ assert routed.credential == "team-token"++ # A local provider is exempt and must not be routed through the proxy.+ local = resolve_provider_endpoint("ollama")+ assert local.via_proxy is False+++def test_an_explicit_config_bypasses_the_cache(_clean):+ """The test seam still answers from what it was handed, not from the file."""+ from jarvis.core.config import JarvisConfig++ resolve_provider_endpoint("openrouter") # populate the cache from the file++ supplied = JarvisConfig(+ **{"brain": {"providers": {"openrouter": {"base_url": "https://given.invalid"}}}}+ )+ assert (+ resolve_provider_endpoint("openrouter", config=supplied).base_url+ == "https://given.invalid"+ )+ # ...and doing so must not have poisoned the cached answer for everyone else.+ assert resolve_provider_endpoint("openrouter").base_url is None