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(brain): keep the GPT-Live key out of text turns and routines

rubenluetke10-beepa2ea51e20331
diff --git a/jarvis/brain/manager.py b/jarvis/brain/manager.py
index 05a2334bae..6f8480570f 100644
--- a/jarvis/brain/manager.py
+++ b/jarvis/brain/manager.py
@@ -2918,6 +2918,9 @@ class BrainManager:
# reset). Prevents each voice turn from running through 8 sequential
# "no API key" failures.
self._dead_providers: set[str] = set()
+ # The provider a person switched to at runtime ("switch to openai").
+ # A deliberate pick keeps answering even on the realtime voice key.
+ self._switched_to: str | None = None
# Model-scoped twin of `_dead_providers`: a billing-style rejection
# (account_blocked, e.g. HTTP 402) on ONE model must not dead-list a
# provider that has other, untried models still in this turn's chain
@@ -4710,6 +4713,7 @@ class BrainManager:
return
previous = self._active_name
self._active_name = canonical
+ self._switched_to = canonical
# Keep deep_brain following the active provider on a runtime switch
# when there is no explicit cross-provider deep split (deep_brain
# tracked the previous active, or was never configured) — so switching
@@ -10537,6 +10541,11 @@ class BrainManager:
if db:
order.append(db)
order += ["gemini", "claude-api", "openai", "openrouter", "grok", "nvidia"]
+ from jarvis.brain.voice_key import bills_voice_key
+
+ # The realtime voice key leads a turn only when no other provider can
+ # call tools (mandate 2026-09-29: that key pays for the voice call).
+ on_voice_key: tuple[str, str | None] | None = None
seen: set[str] = set()
for name in order:
if name in seen or name == skip or name not in available:
@@ -10559,8 +10568,11 @@ class BrainManager:
if not self._rate_tracker.is_available(name, model):
continue
if self._brain_can_call_tools(name, model):
+ if bills_voice_key(self._config, name):
+ on_voice_key = on_voice_key or (name, model)
+ continue
return (name, model)
- return None
+ return on_voice_key
def _turn_has_action_intent(self, user_text: str) -> bool:
"""Best-effort, provider-agnostic 'this turn wants a tool/desktop action'
@@ -10621,6 +10633,7 @@ class BrainManager:
# make the loop wrongly fall through). Set below only when we prepend an
# intelligent-router lead.
self._router_lead_key: tuple[str, str | None] | None = None
+ tool_lead: tuple[str, str | None] | None = None
override = _TURN_OVERRIDE.get()
if override is not None:
@@ -10651,6 +10664,7 @@ class BrainManager:
active, helper[0], active,
)
self._router_lead_key = helper
+ tool_lead = helper
chain.append(helper)
elif getattr(self, "_turn_needs_tools", False):
# Flag OFF (kill switch): the narrower action-intent delegation —
@@ -10662,6 +10676,7 @@ class BrainManager:
"Tool delegation (legacy): %s cannot call tools — leading "
"this action turn with %s.", active, helper[0],
)
+ tool_lead = helper
chain.append(helper)
# 0. Deep/code intents: dedicated deep_brain first (e.g. gemini via
@@ -10749,7 +10764,52 @@ class BrainManager:
continue
seen.add(item)
deduped.append(item)
- return deduped
+ # A tool lead no other family could take, and a provider the person
+ # switched to themselves, stay on the chain.
+ keep = [entry for entry in deduped if entry[0] == self._switched_to == active]
+ if tool_lead is not None:
+ keep.append(tool_lead)
+ return self._keep_off_voice_key(
+ deduped,
+ keep=keep,
+ needs_tools=bool(getattr(self, "_turn_needs_tools", False)),
+ )
+
+ def _keep_off_voice_key(
+ self,
+ chain: list[tuple[str, str | None]],
+ *,
+ keep: Iterable[tuple[str, str | None]] = (),
+ needs_tools: bool = False,
+ ) -> list[tuple[str, str | None]]:
+ """Drop the realtime voice key's family while another provider remains.
+
+ Text turns, Telegram and scheduled routines never spend the key that
+ pays for the live voice call (mandate 2026-09-29). A single-key install
+ keeps it, and so do the ``keep`` entries (a tool lead no other family
+ could take, a provider the person switched to). Only a
+ provider that can take the turn right now counts as the alternative:
+ not rate-limited, and able to call tools when the turn needs them.
+ """
+ from jarvis.brain.voice_key import without_voice_key
+
+ models: dict[str, str | None] = {}
+ for name, model in chain:
+ models.setdefault(name, model)
+
+ def can_answer(name: str) -> bool:
+ model = models.get(name)
+ if not self._rate_tracker.is_available(name, model):
+ return False
+ return not needs_tools or self._brain_can_call_tools(name, model)
+
+ kept = without_voice_key(self._config, chain, is_alternative=can_answer, keep=keep)
+ if len(kept) < len(chain):
+ log.debug(
+ "Voice key reserved for the live call — left out of this chain: %s",
+ sorted({name for name, _ in chain} - {name for name, _ in kept}),
+ )
+ return kept
# ------------------------------------------------------------------
# Generate — Haupt-Entrypoint
@@ -13534,6 +13594,9 @@ class BrainManager:
except Exception: # noqa: BLE001 — a broken probe must not kill the task
log.debug("run_task: tool-model chain unavailable", exc_info=True)
ready = []
+ # A scheduled routine runs unattended; it spends the realtime voice key
+ # only when no other ready provider exists (mandate 2026-09-29).
+ ready = self._keep_off_voice_key(ready)
chain: list[tuple[str, str | None]] = []
for name, model in ready[:_TASK_MAX_ATTEMPTS]:
if intent == "deep":
diff --git a/jarvis/brain/resolver.py b/jarvis/brain/resolver.py
index 141ffdee1e..4b4d064e0d 100644
--- a/jarvis/brain/resolver.py
+++ b/jarvis/brain/resolver.py
@@ -586,8 +586,10 @@ def _resolve_chain(config: JarvisConfig) -> list[tuple[str, str | None]]:
# Stage 1 — override (power user)
bio_cfg = getattr(getattr(config, "board", None), "bio", None)
+ override_entry: tuple[str, str | None] | None = None
if bio_cfg is not None and bio_cfg.override_provider:
- chain.append((bio_cfg.override_provider, bio_cfg.override_model or None))
+ override_entry = (bio_cfg.override_provider, bio_cfg.override_model or None)
+ chain.append(override_entry)
brain_cfg = config.brain
# Wave 4 migration / 2026-06-29 Jarvis-Agents rename: the field was
@@ -678,17 +680,30 @@ def _resolve_chain(config: JarvisConfig) -> list[tuple[str, str | None]]:
if has_reachable_alternative:
deduped = [entry for entry in deduped if entry[0] != "claude-api"]
- # The realtime voice call owns its key (user mandate 2026-09-29). Families
- # that bill it move behind every other cloud family, so skill drafting,
- # bios and other background resolves reach it only when nothing else can
- # answer. They still precede the trailing keyless local tail: a single-key
- # install without a local server must keep a working resolve (AP-22).
- from jarvis.brain.voice_key import bills_voice_key
-
+ # The realtime voice call owns its key (user mandate 2026-09-29). While a
+ # reachable cloud family or subscription remains, families that bill it
+ # leave the chain entirely: skill drafting, bios and other background
+ # resolves never spend it. Without such an alternative they move behind
+ # the other entries but still precede the trailing keyless local tail, so
+ # a single-key install without a local server keeps a working resolve
+ # (AP-22).
+ from jarvis.brain.voice_key import bills_voice_key, without_voice_key
+
+ local_ids = {provider for provider, _ in _local_tail(config)}
+ kept = without_voice_key(
+ config,
+ deduped,
+ is_alternative=lambda provider: (
+ provider not in local_ids and _provider_reachable(config, provider)
+ ),
+ # A power-user override is a deliberate pick, not a fallback.
+ keep=[override_entry] if override_entry else (),
+ )
+ if len(kept) < len(deduped):
+ return kept
on_voice_key = [entry for entry in deduped if bills_voice_key(config, entry[0])]
if on_voice_key and len(on_voice_key) < len(deduped):
rest = [entry for entry in deduped if entry not in on_voice_key]
- local_ids = {provider for provider, _ in _local_tail(config)}
# Only the tail run counts: a local provider the user put first (a
# deliberate local primary or override) keeps its lead.
lead_kept = 1 if rest[0] == deduped[0] else 0
diff --git a/jarvis/brain/voice_key.py b/jarvis/brain/voice_key.py
index c2f044f720..d23c7f56a7 100644
--- a/jarvis/brain/voice_key.py
+++ b/jarvis/brain/voice_key.py
@@ -15,6 +15,7 @@ in ``PROVIDER_SECRET_CANDIDATES`` — never from a provider name (AP-21).
from __future__ import annotations
import logging
+from collections.abc import Callable, Iterable
from typing import Any
log = logging.getLogger(__name__)
@@ -50,10 +51,82 @@ def voice_key_slots(cfg: Any) -> frozenset[str]:
def bills_voice_key(cfg: Any, brain_provider: str | None) -> bool:
"""Whether ``brain_provider`` pays with the key the voice call depends on."""
- slots = voice_key_slots(cfg)
+ return _bills(voice_key_slots(cfg), brain_provider)
+
+
+def _bills(slots: frozenset[str], brain_provider: str | None) -> bool:
if not slots or not brain_provider:
return False
from jarvis.core.config import PROVIDER_SECRET_CANDIDATES
own = PROVIDER_SECRET_CANDIDATES.get(str(brain_provider).strip(), ())
return any(slot in slots for slot, _env in own)
+
+
+def _signed_in_on_subscription(brain_provider: str) -> bool:
+ """Whether the provider offers a subscription login AND one is present.
+
+ Such a brain (a coding CLI on its plan) spends a plan, not the voice key,
+ so the chain filter never drops it. Without the login it would fall back
+ to the shared key slot, so it counts as billing the key like any other.
+ Never raises: an unreadable card or a failed probe counts as key-only.
+ """
+ try:
+ from jarvis.ui.web.provider_spec import PROVIDERS, provider_billing
+ except Exception: # noqa: BLE001 - no cards means no subscription to protect
+ log.debug("voice_key: provider cards unavailable", exc_info=True)
+ return False
+ if not any(
+ spec.id == brain_provider and provider_billing(spec).startswith("subscription")
+ for spec in PROVIDERS
+ ):
+ return False
+ try:
+ from jarvis.brain.manager import _keyless_provider_is_rescued_by_oauth
+
+ return _keyless_provider_is_rescued_by_oauth(brain_provider)
+ except Exception: # noqa: BLE001 - a failed probe is not a login
+ log.debug("voice_key: login probe failed for %s", brain_provider, exc_info=True)
+ return False
+
+
+def without_voice_key(
+ cfg: Any,
+ chain: Iterable[tuple[str, Any]],
+ *,
+ is_alternative: Callable[[str], bool] | None = None,
+ keep: Iterable[tuple[str, Any]] = (),
+) -> list[tuple[str, Any]]:
+ """``chain`` without the entries that bill the voice key, when others remain.
+
+ The realtime voice key pays for the voice call and its thinking model only
+ (user mandate 2026-09-29). Every other request — chat turns, scheduled
+ routines, background resolves — leaves it out while another entry can
+ answer; moving it to the back was not enough, because a depleted fallback
+ ahead of it still handed the turn to the key (live 2026-09-29: five test
+ chat turns spent $6.82 on the voice key's deep model after OpenRouter ran
+ out of credits).
+
+ ``is_alternative`` decides which remaining providers count as able to
+ answer (default: every one). When none does, the chain is returned
+ unchanged, so a single-key install keeps working on that key (AP-22).
+ ``keep`` names entries that stay regardless, such as a tool-calling lead
+ no other provider could take or a provider the person switched to. A brain signed in on its subscription never
+ counts as billing the key.
+ """
+ items = list(chain)
+ slots = voice_key_slots(cfg)
+ if not slots:
+ return items
+ billed = {
+ provider
+ for provider, _model in items
+ if _bills(slots, provider) and not _signed_in_on_subscription(provider)
+ }
+ if not billed:
+ return items
+ accepts = is_alternative or (lambda _provider: True)
+ if not any(provider not in billed and accepts(provider) for provider, _ in items):
+ return items
+ kept = set(keep)
+ return [entry for entry in items if entry in kept or entry[0] not in billed]
diff --git a/tests/unit/brain/test_manager_voice_key_chain.py b/tests/unit/brain/test_manager_voice_key_chain.py
new file mode 100644
index 0000000000..a8d696fe04
--- /dev/null
+++ b/tests/unit/brain/test_manager_voice_key_chain.py
@@ -0,0 +1,165 @@
+"""Text turns and routines stay off the realtime voice key (mandate 2026-09-29).
+
+Live 2026-09-29: five chat turns reached the OpenAI key a user had added for
+GPT-Live after OpenRouter ran out of credits, and its deep model spent $6.82.
+The key pays for the voice call and its thinking model; any other turn uses it
+only when no other provider is left.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+from jarvis.brain.manager import BrainManager
+from jarvis.core.bus import EventBus
+from jarvis.core.config import BrainProviderConfig, BrainTierConfig, JarvisConfig
+
+
+def _manager(
+ monkeypatch: pytest.MonkeyPatch,
+ *,
+ active: str,
+ available: list[str],
+ mode: str = "realtime",
+) -> BrainManager:
+ config = JarvisConfig()
+ config.brain.primary = active
+ config.brain.realtime = BrainTierConfig(provider="openai-live", model="gpt-live-1")
+ config.brain.providers["openai"] = BrainProviderConfig(
+ model="gpt-5.5", deep_model="gpt-5.5-pro"
+ )
+ config.voice.mode = mode
+ manager = BrainManager(config=config, bus=EventBus(), tools={})
+ manager._registry._loaded = True
+ monkeypatch.setattr(manager._registry, "available", lambda: list(available))
+ manager._active_name = active
+ manager._dead_providers = set()
+ manager._configured_fallbacks = []
+ return manager
+
+
+def _providers(chain: list[tuple[str, str | None]]) -> set[str]:
+ return {provider for provider, _ in chain}
+
+
[email protected]("level", ["fast", "deep"])
+def test_voice_key_leaves_the_turn_chain_while_another_provider_remains(
+ monkeypatch: pytest.MonkeyPatch, level: str
+) -> None:
+ manager = _manager(monkeypatch, active="grok", available=["grok", "openrouter", "openai"])
+
+ chain = manager._build_fallback_chain(level)
+
+ assert "openai" not in _providers(chain)
+ assert chain[0][0] == "grok"
+
+
+def test_voice_key_primary_hands_the_turn_to_another_family(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ manager = _manager(monkeypatch, active="openai", available=["grok", "openai"])
+
+ chain = manager._build_fallback_chain("deep")
+
+ assert _providers(chain) == {"grok"}
+
+
+def test_single_key_install_keeps_answering_on_the_voice_key(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ manager = _manager(monkeypatch, active="openai", available=["openai"])
+
+ chain = manager._build_fallback_chain("deep")
+
+ assert chain[0] == ("openai", "gpt-5.5-pro")
+
+
+def test_pipeline_voice_leaves_openai_in_the_chain(monkeypatch: pytest.MonkeyPatch) -> None:
+ manager = _manager(
+ monkeypatch, active="openai", available=["grok", "openai"], mode="pipeline"
+ )
+
+ chain = manager._build_fallback_chain("fast")
+
+ assert chain[0] == ("openai", "gpt-5.5")
+
+
+def test_tool_lead_prefers_a_family_off_the_voice_key(monkeypatch: pytest.MonkeyPatch) -> None:
+ manager = _manager(monkeypatch, active="codex", available=["codex", "openai", "grok"])
+ monkeypatch.setattr(manager, "_brain_can_call_tools", lambda name, model: True)
+
+ assert manager._first_tool_capable_provider("fast")[0] == "grok"
+
+
+def test_voice_key_still_leads_tools_when_no_other_family_can(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ manager = _manager(monkeypatch, active="codex", available=["codex", "openai"])
+ monkeypatch.setattr(manager, "_brain_can_call_tools", lambda name, model: name != "codex")
+
+ assert manager._first_tool_capable_provider("fast")[0] == "openai"
+
+
+def test_scheduled_routine_chain_skips_the_voice_key(monkeypatch: pytest.MonkeyPatch) -> None:
+ manager = _manager(monkeypatch, active="grok", available=["grok", "openai"])
+ monkeypatch.setattr(
+ manager,
+ "_hoist_tool_model",
+ lambda chain: [("openai", "gpt-5.5"), ("grok", "grok-4.3")],
+ )
+
+ chain = manager._task_provider_chain("deep")
+
+ assert _providers(chain) == {"grok"}
+
+
+def test_voice_key_stays_when_the_alternative_is_rate_limited(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ manager = _manager(monkeypatch, active="openai", available=["grok", "openai"])
+ monkeypatch.setattr(
+ manager._rate_tracker, "is_available", lambda name, model=None: name != "grok"
+ )
+
+ chain = manager._build_fallback_chain("fast")
+
+ assert "openai" in _providers(chain)
+
+
+def test_action_turn_keeps_the_voice_key_when_no_alternative_calls_tools(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ manager = _manager(monkeypatch, active="openai", available=["antigravity", "openai"])
+ monkeypatch.setattr(
+ manager, "_brain_can_call_tools", lambda name, model: name != "antigravity"
+ )
+ manager._turn_needs_tools = True
+
+ chain = manager._build_fallback_chain("fast")
+
+ assert chain[0][0] == "openai"
+
+
+def test_legacy_tool_delegation_keeps_its_voice_key_lead(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ manager = _manager(monkeypatch, active="codex", available=["codex", "openai", "grok"])
+ manager._config.brain.routing.intelligent_router = False
+ manager._turn_needs_tools = True
+ monkeypatch.setattr(manager, "_brain_can_call_tools", lambda name, model: name == "openai")
+
+ chain = manager._build_fallback_chain("fast")
+
+ assert chain[0][0] == "openai"
+
+
+def test_a_runtime_switch_to_the_voice_key_family_is_honoured(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """A person who says "switch to openai" gets openai, not a silent stand-in."""
+ manager = _manager(monkeypatch, active="openai", available=["grok", "openai"])
+ manager._switched_to = "openai"
+
+ chain = manager._build_fallback_chain("deep")
+
+ assert chain[0] == ("openai", "gpt-5.5-pro")
diff --git a/tests/unit/brain/test_resolver_voice_key.py b/tests/unit/brain/test_resolver_voice_key.py
index 6b3b548f02..e5274c26b0 100644
--- a/tests/unit/brain/test_resolver_voice_key.py
+++ b/tests/unit/brain/test_resolver_voice_key.py
@@ -45,14 +45,16 @@ def _providers(config: JarvisConfig) -> list[str]:
return [provider for provider, _ in resolver._resolve_chain(config)]
-def test_voice_key_primary_moves_behind_other_cloud_families(monkeypatch) -> None:
+def test_voice_key_primary_leaves_the_chain_while_other_families_are_keyed(
+ monkeypatch,
+) -> None:
_keys(monkeypatch, "openai", "gemini", "grok")
providers = _providers(_gpt_live("openai"))
- assert providers[0] != "openai"
- assert providers.index("gemini") < providers.index("openai")
- assert providers.index("grok") < providers.index("openai")
+ assert "openai" not in providers
+ assert "gemini" in providers
+ assert "grok" in providers
def test_voice_key_still_precedes_the_keyless_local_tail(monkeypatch) -> None:
@@ -79,3 +81,13 @@ def test_pipeline_voice_leaves_an_openai_primary_first(monkeypatch) -> None:
providers = _providers(_gpt_live("openai", mode="pipeline"))
assert providers[0] == "openai"
+
+
+def test_codex_without_a_login_is_no_alternative_to_the_voice_key(monkeypatch) -> None:
+ """Codex falls back to the same OpenAI key slot when nobody is signed in."""
+ _keys(monkeypatch, "openai", "codex")
+
+ providers = _providers(_gpt_live("openai"))
+
+ assert "openai" in providers
+ assert providers.index("openai") < providers.index("ollama")
diff --git a/tests/unit/brain/test_voice_key.py b/tests/unit/brain/test_voice_key.py
index 07b435319c..e6fa85eeb2 100644
--- a/tests/unit/brain/test_voice_key.py
+++ b/tests/unit/brain/test_voice_key.py
@@ -4,7 +4,7 @@ from __future__ import annotations
from types import SimpleNamespace
-from jarvis.brain.voice_key import bills_voice_key, voice_key_slots
+from jarvis.brain.voice_key import bills_voice_key, voice_key_slots, without_voice_key
def _cfg(mode: str, realtime_provider: str) -> SimpleNamespace:
@@ -34,3 +34,34 @@ def test_unknown_or_missing_realtime_provider_reserves_nothing() -> None:
assert voice_key_slots(_cfg("realtime", "")) == frozenset()
assert voice_key_slots(_cfg("realtime", "no-such-voice-plugin")) == frozenset()
assert not bills_voice_key(None, "openai")
+
+
+def test_chain_drops_the_voice_key_while_another_provider_remains() -> None:
+ cfg = _cfg("realtime", "openai-live")
+ chain = [("openai", "gpt-5.5-pro"), ("grok", "grok-4.3"), ("openai", "gpt-5.5")]
+
+ assert without_voice_key(cfg, chain) == [("grok", "grok-4.3")]
+
+
+def test_chain_keeps_the_voice_key_when_nothing_else_can_answer() -> None:
+ cfg = _cfg("realtime", "openai-live")
+ chain = [("openai", "gpt-5.5"), ("ollama", None)]
+
+ kept = without_voice_key(cfg, chain, is_alternative=lambda provider: provider != "ollama")
+
+ assert kept == chain
+
+
+def test_chain_keeps_a_named_entry_and_subscription_brains() -> None:
+ cfg = _cfg("realtime", "openai-live")
+ lead = ("openai", "gpt-5.5")
+ chain = [lead, ("codex", None), ("grok", "grok-4.3")]
+
+ assert without_voice_key(cfg, chain, keep=[lead]) == chain
+ assert without_voice_key(cfg, chain) == [("codex", None), ("grok", "grok-4.3")]
+
+
+def test_pipeline_voice_leaves_the_chain_untouched() -> None:
+ chain = [("openai", "gpt-5.5"), ("grok", "grok-4.3")]
+
+ assert without_voice_key(_cfg("pipeline", "openai-live"), chain) == chain