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.

feat(brain): a writer chain that degrades honestly instead of demoting to a fast model

Ruben Lütke0e87eb6b5d3f
diff --git a/jarvis/brain/resolver.py b/jarvis/brain/resolver.py
index 468602915a..4058197b1f 100644
--- a/jarvis/brain/resolver.py
+++ b/jarvis/brain/resolver.py
@@ -149,6 +149,89 @@ def resolve_frontier_brain(
)
+def _is_fast_tier(config: JarvisConfig, provider: str, model: str | None) -> bool:
+ """Whether ``(provider, model)`` is this install's latency-first tier.
+
+ Classification comes from the tier tables and from the user's OWN router
+ configuration — never from a model name or a provider name (AP-21). A model
+ the tables do not know is treated as qualified: we filter what we know, we
+ do not guess from a name like "flash" or "mini", because that is exactly the
+ name-based gating that breaks silently for every provider we did not think
+ of.
+ """
+ name = (model or "").strip()
+ if not name:
+ return False
+
+ router_cfg = getattr(getattr(config, "brain", None), "router", None)
+ if (
+ getattr(router_cfg, "provider", None) == provider
+ and (getattr(router_cfg, "model", None) or "").strip() == name
+ ):
+ return True
+
+ fast_default = TIER_DEFAULTS_BY_PROVIDER.get("router", {}).get(provider, "")
+ deep_default = TIER_DEFAULTS_BY_PROVIDER.get("deep", {}).get(provider, "")
+ # A provider whose fast and deep defaults coincide has only one model worth
+ # having; filtering it away would leave that provider with nothing.
+ return bool(fast_default) and name == fast_default and name != deep_default
+
+
+def resolve_quality_brain(
+ config: JarvisConfig,
+ *,
+ bus: EventBus | None = None,
+) -> Brain | None:
+ """A Brain for work that must not be done by a small model, or None.
+
+ ``resolve_frontier_brain`` walks the whole fallback chain, and that chain
+ deliberately ends in small, fast, cheap models so a core path never dies.
+ For callers whose OUTPUT is the product rather than a step towards it, that
+ trade is wrong: the Agentic IDE's prompt composer writes the brief a coding
+ agent then works from, and a brief written by a mini model on a depleted
+ primary is worse than an honestly plain deterministic one — and worse
+ *invisibly*, which is the part that matters. Nobody inspects a prompt that
+ looks fine.
+
+ So this resolver skips the latency-first stages and returns None when none
+ of the remaining ones can be instantiated, leaving the caller to degrade
+ openly. Never raises: None is the answer, not an error.
+ """
+ _ensure_bus_subscription(bus)
+ try:
+ chain = list(_resolve_chain(config))
+ except Exception: # noqa: BLE001 - a config problem must not kill the caller
+ log.info("resolve_quality_brain: chain could not be built", exc_info=True)
+ return None
+
+ for provider, model in chain:
+ if _is_fast_tier(config, provider, model):
+ log.debug(
+ "resolve_quality_brain: skipping %s/%s (latency-first tier)",
+ provider, model,
+ )
+ continue
+ cache_key = (provider, model or "")
+ cached = _cache.get(cache_key)
+ if cached is not None:
+ return cached
+ try:
+ brain = _get_registry().instantiate(
+ provider, **({"model": model} if model else {}),
+ )
+ except Exception as exc: # noqa: BLE001
+ log.info(
+ "resolve_quality_brain: %s/%s not instantiable (%s)",
+ provider, model or "<default>", type(exc).__name__,
+ )
+ continue
+ _cache[cache_key] = brain
+ return brain
+
+ log.info("resolve_quality_brain: no quality-tier provider reachable")
+ return None
+
+
# ----------------------------------------------------------------------
# Internal — Chain-Building
# ----------------------------------------------------------------------
diff --git a/tests/unit/brain/test_resolver_quality_chain.py b/tests/unit/brain/test_resolver_quality_chain.py
new file mode 100644
index 0000000000..6c18386d63
--- /dev/null
+++ b/tests/unit/brain/test_resolver_quality_chain.py
@@ -0,0 +1,181 @@
+"""The writer chain that refuses to demote a quality task to a fast model.
+
+``resolve_frontier_brain`` walks the whole fallback chain, and that chain
+deliberately ends in small, fast, cheap models so a core path never dies. For
+callers whose OUTPUT is the product — the Agentic IDE's prompt composer is the
+first — that trade is wrong: a prompt written by a mini model on a depleted
+primary is worse than an honestly plain deterministic one, and worse
+*invisibly*. This resolver skips the latency-first stages and answers None
+rather than quietly handing back something weaker than asked for.
+"""
+from __future__ import annotations
+
+import pytest
+
+from jarvis.brain import resolver
+
+
+class _FakeRegistry:
+ """Records what the resolver tried to instantiate."""
+
+ def __init__(self, fail: bool = False) -> None:
+ self.attempts: list[tuple[str, str]] = []
+ self.fail = fail
+
+ def instantiate(self, provider: str, **kwargs: object) -> object:
+ self.attempts.append((provider, str(kwargs.get("model", ""))))
+ if self.fail:
+ raise RuntimeError("no key")
+ return object()
+
+
+def _config(router_provider: str = "gemini", router_model: str = "") -> object:
+ """A stand-in carrying only what the chain filter reads."""
+
+ class Router:
+ provider = router_provider
+ model = router_model
+
+ class Brain:
+ router = Router()
+
+ class Config:
+ brain = Brain()
+
+ return Config()
+
+
[email protected](autouse=True)
+def _clean_cache(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(resolver, "_cache", {})
+
+
+def test_a_built_in_fast_tier_stage_is_skipped(monkeypatch: pytest.MonkeyPatch) -> None:
+ """claude-haiku is claude-api's ROUTER default — it must not write prompts."""
+ monkeypatch.setattr(
+ resolver,
+ "_resolve_chain",
+ lambda cfg: [
+ ("claude-api", "claude-haiku-4-5-20251001"),
+ ("gemini", "gemini-3.1-pro-preview"),
+ ],
+ )
+ fake = _FakeRegistry()
+ monkeypatch.setattr(resolver, "_get_registry", lambda: fake)
+
+ brain = resolver.resolve_quality_brain(_config())
+
+ assert brain is not None
+ assert fake.attempts == [("gemini", "gemini-3.1-pro-preview")]
+
+
+def test_the_users_own_configured_fast_model_is_skipped_too(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """The install's configured router tier counts, not only the built-in
+ defaults — otherwise a user who picked their own fast model is not
+ protected."""
+ monkeypatch.setattr(
+ resolver,
+ "_resolve_chain",
+ lambda cfg: [("gemini", "gemini-3.5-flash"), ("openai", "gpt-5.5-pro")],
+ )
+ fake = _FakeRegistry()
+ monkeypatch.setattr(resolver, "_get_registry", lambda: fake)
+
+ resolver.resolve_quality_brain(_config("gemini", "gemini-3.5-flash"))
+
+ assert ("gemini", "gemini-3.5-flash") not in fake.attempts
+ assert ("openai", "gpt-5.5-pro") in fake.attempts
+
+
+def test_a_providers_own_fast_default_is_skipped_even_mid_chain(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """gpt-5.5 is openai's ROUTER default while gpt-5.5-pro is its deep one, so
+ the plain model is a latency-first stage even though it looks capable."""
+ monkeypatch.setattr(
+ resolver,
+ "_resolve_chain",
+ lambda cfg: [("openai", "gpt-5.5"), ("openai", "gpt-5.5-pro")],
+ )
+ fake = _FakeRegistry()
+ monkeypatch.setattr(resolver, "_get_registry", lambda: fake)
+
+ resolver.resolve_quality_brain(_config())
+
+ assert fake.attempts == [("openai", "gpt-5.5-pro")]
+
+
+def test_returns_none_when_only_fast_models_are_reachable(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """Honestly plain beats silently poor: the caller degrades openly."""
+ monkeypatch.setattr(
+ resolver,
+ "_resolve_chain",
+ lambda cfg: [("claude-api", "claude-haiku-4-5-20251001")],
+ )
+ fake = _FakeRegistry()
+ monkeypatch.setattr(resolver, "_get_registry", lambda: fake)
+
+ assert resolver.resolve_quality_brain(_config()) is None
+ assert fake.attempts == []
+
+
+def test_returns_none_rather_than_raising_when_nothing_instantiates(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr(
+ resolver, "_resolve_chain", lambda cfg: [("gemini", "gemini-3.1-pro-preview")]
+ )
+ monkeypatch.setattr(resolver, "_get_registry", lambda: _FakeRegistry(fail=True))
+
+ assert resolver.resolve_quality_brain(_config()) is None
+
+
+def test_returns_none_rather_than_raising_on_a_broken_config(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ def explode(cfg: object) -> list[tuple[str, str]]:
+ raise ValueError("config is nonsense")
+
+ monkeypatch.setattr(resolver, "_resolve_chain", explode)
+
+ assert resolver.resolve_quality_brain(_config()) is None
+
+
+def test_a_provider_whose_fast_and_deep_defaults_coincide_is_not_filtered_away(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """Filtering must never leave a provider with nothing. grok's router and
+ deep defaults are the same model, so it is that provider's only option."""
+ from jarvis.brain.manager import TIER_DEFAULTS_BY_PROVIDER
+
+ router_default = TIER_DEFAULTS_BY_PROVIDER["router"].get("grok")
+ deep_default = TIER_DEFAULTS_BY_PROVIDER["deep"].get("grok")
+ if router_default != deep_default:
+ pytest.skip("grok's tier defaults have diverged; the case moved elsewhere")
+
+ monkeypatch.setattr(
+ resolver, "_resolve_chain", lambda cfg: [("grok", router_default)]
+ )
+ fake = _FakeRegistry()
+ monkeypatch.setattr(resolver, "_get_registry", lambda: fake)
+
+ assert resolver.resolve_quality_brain(_config()) is not None
+
+
+def test_an_unknown_model_is_treated_as_qualified(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """We filter what the tier tables KNOW is fast; we do not guess from names.
+ Gating on a name would be the anti-pattern AP-21 forbids."""
+ monkeypatch.setattr(
+ resolver, "_resolve_chain", lambda cfg: [("someprovider", "some-mini-flash")]
+ )
+ fake = _FakeRegistry()
+ monkeypatch.setattr(resolver, "_get_registry", lambda: fake)
+
+ assert resolver.resolve_quality_brain(_config()) is not None
+ assert fake.attempts == [("someprovider", "some-mini-flash")]