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
"""The tool surface fits the answering brain's context window (BUG-187).
Live 2026-08-27 10:11 (and 2026-08-26 20:32, 20:58): the front page's chat
ran on a local 32k model, the registry held 221 tools (78 built-in, 131 from
three connected MCP servers, 12 CLIs) and a plain "Hallo" went out as 67,852
tokens. The server refused it with a 400 that named the cause, the
one-provider chain was exhausted, and the user read "I can't reach my provider
— check the network". Two defects, two guards:
1. Nothing read ``Brain.context_window`` — ``_fit_tools_to_context_window``
now sizes the surface: connected-server tools leave first, then the largest
remaining ones, mandated tools never.
2. The 400 was classified as a generic call failure —
``_classify_provider_error`` now returns ``context_overflow`` and the cause
phrase / diagnostic name the size, not the network.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any
import pytest
from dodaios.brain.manager import (
_DEAD_LIST_KINDS,
_PROVIDER_DOWN_CAUSE_PHRASES,
BrainManager,
_classify_provider_error,
_fit_tools_to_context_window,
_format_provider_chain_error,
_primary_provider_down_cause,
_provider_down_phrase,
_tool_surface_tokens,
)
# The exact server refusal from the live log (Ollama over the OpenAI route).
_LIVE_OLLAMA_400 = (
"Error code: 400 - {'error': {'message': '{\"error\":{\"code\":400,"
"\"message\":\"request (67852 tokens) exceeds the available context size "
"(32768 tokens), try increasing it\",\"type\":\"exceed_context_size_error\","
"\"n_prompt_tokens\":67852,\"n_ctx\":32768}}', 'type': 'invalid_request_error'}}"
)
@dataclass
class _FakeTool:
name: str
description: str = "does a thing"
schema: dict[str, Any] = field(default_factory=lambda: {"type": "object", "properties": {}})
is_mcp_tool: bool = False
def _tool(name: str, *, size: int = 1, mcp: bool = False) -> _FakeTool:
"""A tool whose wire size scales with ``size`` (schema padding)."""
props = {f"arg{i}": {"type": "string", "description": "x" * 40} for i in range(size)}
return _FakeTool(
name=name,
schema={"type": "object", "properties": props},
is_mcp_tool=mcp,
)
class _FakeBrain:
name = "fake"
context_window = 0
supports_vision = False
def __init__(self, window: int) -> None:
self.context_window = window
# --------------------------------------------------------------- the fit
class TestFitToolsToContextWindow:
def test_no_declared_window_leaves_the_surface_alone(self) -> None:
tools = {"a": _tool("a", size=50), "b": _tool("b", size=50)}
fitted, dropped = _fit_tools_to_context_window(
tools, context_window=0, used_tokens=1_000_000
)
assert fitted is tools
assert dropped == []
def test_a_surface_that_fits_is_untouched(self) -> None:
tools = {"a": _tool("a"), "b": _tool("b", mcp=True)}
fitted, dropped = _fit_tools_to_context_window(
tools, context_window=100_000, used_tokens=10_000
)
assert fitted is tools
assert dropped == []
def test_connected_server_tools_leave_before_built_in_ones(self) -> None:
# Two small built-ins and two LARGE MCP tools; the budget holds only
# the built-ins. Size alone would keep nothing built-in either way —
# the group order is what protects Jarvis's own hands.
tools = {
"run_shell": _tool("run_shell", size=2),
"search_web": _tool("search_web", size=2),
"github/create_issue": _tool("github/create_issue", size=20, mcp=True),
"linear/list_issues": _tool("linear/list_issues", size=20, mcp=True),
}
built_in_cost = sum(
_tool_surface_tokens(n, t) for n, t in tools.items() if not t.is_mcp_tool
)
fitted, dropped = _fit_tools_to_context_window(
tools, context_window=built_in_cost + 100, used_tokens=100
)
assert set(fitted) == {"run_shell", "search_web"}
assert dropped == ["linear/list_issues", "github/create_issue"] or dropped == [
"github/create_issue",
"linear/list_issues",
]
def test_a_tool_that_does_not_fit_is_passed_over_for_the_next(self) -> None:
tools = {
"tiny": _tool("tiny", size=1),
"huge": _tool("huge", size=40),
"medium": _tool("medium", size=5),
}
budget = _tool_surface_tokens("tiny", tools["tiny"]) + _tool_surface_tokens(
"medium", tools["medium"]
)
fitted, dropped = _fit_tools_to_context_window(
tools, context_window=budget, used_tokens=0
)
assert dropped == ["huge"]
assert list(fitted) == ["tiny", "medium"] # registry order preserved
def test_kept_names_are_never_dropped(self) -> None:
tools = {
"mandated": _tool("mandated", size=40),
"other": _tool("other", size=1),
}
fitted, dropped = _fit_tools_to_context_window(
tools, context_window=10, used_tokens=0, keep={"mandated"}
)
assert "mandated" in fitted
assert dropped == ["other"]
def test_prompt_larger_than_window_drops_every_droppable_tool(self) -> None:
tools = {"a": _tool("a"), "b": _tool("b", mcp=True)}
fitted, dropped = _fit_tools_to_context_window(
tools, context_window=1_000, used_tokens=5_000
)
assert fitted == {}
assert set(dropped) == {"a", "b"}
class TestFitOnTheManager:
def _mgr(self) -> BrainManager:
m = BrainManager.__new__(BrainManager) # bypass heavy __init__
m._tools = {}
m._evidence_required_tool = None
m._skill_turn_match = None
return m
def test_cloud_sized_window_keeps_the_whole_surface(self) -> None:
m = self._mgr()
tools = {f"t{i}": _tool(f"t{i}", size=10, mcp=i % 2 == 0) for i in range(221)}
fitted, deferred = m._shortlist_tools(
tools, _FakeBrain(1_048_576), system_prompt="x" * 40_000, history=None
)
assert fitted is tools and not deferred
def test_local_window_trims_the_live_shape(self) -> None:
# 221 tools, a long system prompt, a 32k window: the surface shrinks
# and the connected-server tools are the ones that went.
m = self._mgr()
tools = {f"t{i}": _tool(f"t{i}", size=10, mcp=i % 2 == 0) for i in range(221)}
surface, deferred = m._shortlist_tools(
tools, _FakeBrain(32_768), system_prompt="x" * 30_000, history=None
)
shown = {n: t for n, t in surface.items() if n not in deferred}
assert 0 < len(shown) < 221
assert not any(getattr(t, "is_mcp_tool", False) for t in shown.values())
# Deferred, not dropped: every tool is still there to be called.
assert set(tools) <= set(surface)
def test_mandated_tool_survives_a_local_window(self) -> None:
m = self._mgr()
m._evidence_required_tool = "search_web"
m._skill_turn_match = object()
tools = {
"search_web": _tool("search_web", size=60),
"run-skill": _tool("run-skill", size=60),
"github/x": _tool("github/x", size=60, mcp=True),
}
surface, deferred = m._shortlist_tools(
tools, _FakeBrain(4_200), system_prompt="", history=None
)
shown = set(surface) - deferred
assert {"search_web", "run-skill"} <= shown
assert "github/x" not in shown
def test_undeclared_window_is_left_alone(self) -> None:
m = self._mgr()
class _Windowless:
name = "w"
tools = {"a": _tool("a", size=60)}
surface, deferred = m._shortlist_tools(
tools, _Windowless(), system_prompt="", history=None
)
assert surface is tools and not deferred
# ---------------------------------------------------------- the cause
class TestContextOverflowCause:
@pytest.mark.parametrize(
"msg",
[
_LIVE_OLLAMA_400,
"Error code: 400 - This model's maximum context length is 128000 tokens.",
"Error code: 400 - context_length_exceeded",
"prompt is too long: 213462 tokens > 200000 maximum",
"400 INVALID_ARGUMENT. The input token count (2000000) exceeds the "
"maximum number of tokens allowed (1048576).",
],
)
def test_size_refusals_classify_as_context_overflow(self, msg: str) -> None:
assert _classify_provider_error(msg, default="call_fail") == "context_overflow"
def test_overflow_never_dead_lists_the_provider(self) -> None:
assert "context_overflow" not in _DEAD_LIST_KINDS
def test_a_plain_400_still_falls_through(self) -> None:
kind = _classify_provider_error("Error code: 400 - bad request", default="call_fail")
assert kind == "call_fail"
def test_spoken_cause_names_the_size_not_the_network(self) -> None:
errors = [("ollama", "qwen3.8-16gb:latest", "context_overflow", _LIVE_OLLAMA_400)]
cause = _primary_provider_down_cause(errors)
assert cause == "context_overflow"
for lang in ("de", "en", "es"):
phrase = _provider_down_phrase(lang, 0, cause)
assert phrase == _PROVIDER_DOWN_CAUSE_PHRASES["context_overflow"][lang]
low = phrase.lower()
assert "netzwerk" not in low and "network" not in low and "red" != low
assert "ollama" not in low # voice-safe: no provider names
def test_missing_key_still_outranks_an_overflow(self) -> None:
errors = [
("ollama", "m", "context_overflow", "…"),
("gemini", "g", "missing_key", "…"),
]
assert _primary_provider_down_cause(errors) == "missing_key"
def test_developer_diagnostic_stops_blaming_the_network(self) -> None:
text = _format_provider_chain_error(
[("ollama", "qwen3.8-16gb:latest", "context_overflow", _LIVE_OLLAMA_400)]
)
assert "Kontextfenster" in text # i18n-allow: the diagnostic is German
assert "ollama" in text
assert "Netzwerk" not in text # i18n-allow
class TestLocalContextCause:
"""A local server runs the model with a window too small for the request.
The remedy is one setting on the person's own machine, not another model
or a new chat, so the cause is its own and says so.
"""
@staticmethod
def _error() -> str:
from dodaios.brain.local_context import LocalContextError, fix_hint
return str(
LocalContextError(
provider="local-openai",
model="qwen",
needed_tokens=18_000,
window_tokens=4_096,
how_to_fix=fix_hint("lm-studio", 18_000),
)
)
def test_the_guards_own_sentence_classifies_as_local_context(self) -> None:
assert _classify_provider_error(self._error(), default="call_fail") == "local_context"
def test_it_never_dead_lists_the_provider(self) -> None:
assert "local_context" not in _DEAD_LIST_KINDS
def test_spoken_cause_names_the_setting_to_change(self) -> None:
errors = [("local-openai", "qwen", "local_context", self._error())]
cause = _primary_provider_down_cause(errors)
assert cause == "local_context"
phrase = _provider_down_phrase("en", 0, cause)
assert phrase == _PROVIDER_DOWN_CAUSE_PHRASES["local_context"]["en"]
low = phrase.lower()
assert "context length" in low and "local" in low
assert "network" not in low
def test_it_outranks_the_generic_overflow_wording(self) -> None:
errors = [
("openrouter", "m", "context_overflow", "…"),
("ollama", "qwen", "local_context", self._error()),
]
assert _primary_provider_down_cause(errors) == "local_context"
def test_developer_diagnostic_keeps_the_size_and_the_setting(self) -> None:
text = _format_provider_chain_error(
[("local-openai", "qwen", "local_context", self._error())]
)
assert "4096" in text and "18000" in text
assert "Context Length" in text
assert "Netzwerk" not in text # i18n-allow
class TestWindowIsSettledBeforeFitting:
"""A local server's window is whatever it was started with, so the brain
has to ask. That must happen before tools are fitted, or the first turn is
budgeted for a figure the server does not run."""
async def test_a_brain_that_can_ask_is_asked_first(self) -> None:
from dodaios.brain.manager import _settle_context_window
class _Asking(_FakeBrain):
async def prepare_context_window(self) -> None:
self.context_window = 4_096
brain = _Asking(32_768)
await _settle_context_window(brain)
assert brain.context_window == 4_096
async def test_a_hosted_brain_is_not_touched(self) -> None:
from dodaios.brain.manager import _settle_context_window
brain = _FakeBrain(1_000_000)
await _settle_context_window(brain)
assert brain.context_window == 1_000_000
async def test_a_failing_or_slow_probe_never_costs_the_turn(self, monkeypatch) -> None:
import asyncio
import dodaios.brain.manager as manager
class _Broken(_FakeBrain):
async def prepare_context_window(self) -> None:
raise RuntimeError("server gone")
class _Slow(_FakeBrain):
async def prepare_context_window(self) -> None:
await asyncio.sleep(5)
monkeypatch.setattr(manager, "_CONTEXT_SETTLE_TIMEOUT_S", 0.01)
for brain in (_Broken(32_768), _Slow(32_768)):
await manager._settle_context_window(brain)
assert brain.context_window == 32_768
class TestToolCountCap:
"""Live 2026-09-29: xAI refused 378 tools ("the maximum is 350")."""
def test_count_cap_trims_without_a_window(self) -> None:
tools = {f"t{i}": _tool(f"t{i}", mcp=i < 3) for i in range(10)}
fitted, dropped = _fit_tools_to_context_window(
tools, context_window=0, used_tokens=0, max_tools=7
)
assert len(fitted) == 7
# Connected-server tools leave first.
assert set(dropped) == {"t0", "t1", "t2"}
def test_count_cap_never_drops_kept_tools(self) -> None:
tools = {f"t{i}": _tool(f"t{i}") for i in range(5)}
fitted, _ = _fit_tools_to_context_window(
tools, context_window=0, used_tokens=0, keep={"t0"}, max_tools=2
)
assert "t0" in fitted and len(fitted) == 2
def test_under_the_cap_is_untouched(self) -> None:
tools = {f"t{i}": _tool(f"t{i}") for i in range(5)}
fitted, dropped = _fit_tools_to_context_window(
tools, context_window=0, used_tokens=0, max_tools=5
)
assert fitted is tools and dropped == []
def test_grok_declares_its_cap(self) -> None:
from dodaios.plugins.brain.grok import GrokBrain
assert GrokBrain.max_tools == 350