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): disable Gemini Automatic Function Calling (Jarvis owns the tool loop)

Ruben Lütke838764af2351
diff --git a/jarvis/plugins/brain/gemini.py b/jarvis/plugins/brain/gemini.py
index 6b053fa339..4715b47728 100644
--- a/jarvis/plugins/brain/gemini.py
+++ b/jarvis/plugins/brain/gemini.py
@@ -438,6 +438,25 @@ class GeminiBrain:
if tools_payload:
config_dict["tools"] = tools_payload
+ # Jarvis runs its OWN tool-use loop, so Gemini's Automatic Function
+ # Calling (AFC) must be OFF. With AFC on (the SDK default) Gemini makes
+ # its own tool round-trips and, finding no executable Python callable for
+ # a declaration-only tool, leaks the function_call as response TEXT.
+ # Forensic 2026-06-27: a voice "switch the worker from antigravity to
+ # codex" hit AFC ("AFC is enabled with max remote calls: 10"), the call
+ # leaked, the recovery path ran the WRONG tool, and the turn stalled
+ # 108 s into the brain-timeout fallback. ``disable=True`` makes Gemini
+ # return a clean structured function_call the stream loop consumes — fast
+ # and exact. Best-effort: an SDK too old to know the field must not kill
+ # the call (it just keeps the slower legacy behavior).
+ try:
+ from google.genai import types as _genai_types
+ config_dict["automatic_function_calling"] = (
+ _genai_types.AutomaticFunctionCallingConfig(disable=True)
+ )
+ except Exception: # noqa: BLE001 — never break the brain call over an SDK shape change
+ log.debug("could not disable Gemini automatic_function_calling", exc_info=True)
+
# Latenz-Sprint-1: Thinking-Budget. Nur wenn explizit gesetzt — sonst
# ueberlassen wir die Wahl dem SDK-Default (vorheriges Verhalten).
# ``ThinkingConfig`` ist ab google-genai >= 0.7 verfuegbar; aelteren
diff --git a/tests/unit/brain/test_gemini_stale_cache_bug019.py b/tests/unit/brain/test_gemini_stale_cache_bug019.py
index 0cc03e7aa0..40d4e21764 100644
--- a/tests/unit/brain/test_gemini_stale_cache_bug019.py
+++ b/tests/unit/brain/test_gemini_stale_cache_bug019.py
@@ -258,3 +258,39 @@ async def test_invalidate_cache_clears_state_so_next_call_can_succeed(
"stale cache id."
)
assert "system_instruction" in only_call
+
+
+# ---------------------------------------------------------------------------
+# Test 3 — Automatic Function Calling must be OFF (Jarvis owns the tool loop).
+# ---------------------------------------------------------------------------
+
+
+async def test_automatic_function_calling_is_disabled() -> None:
+ """Gemini's Automatic Function Calling must be disabled on every call.
+
+ Jarvis runs its own tool-use loop. With AFC on (the SDK default) Gemini
+ makes its own tool round-trips and, finding no executable Python callable for
+ a declaration-only tool, leaks the function_call as response TEXT. Live
+ forensic 2026-06-27: a voice "switch the worker from antigravity to codex"
+ triggered AFC ("AFC is enabled with max remote calls: 10"), the call leaked,
+ the recovery path ran the WRONG tool (list_mutable_settings), and the turn
+ stalled 108 s into the brain-timeout fallback ("ich habe die Antwort nicht
+ rechtzeitig fertigbekommen"). disable=True makes Gemini return a clean
+ structured function_call the stream loop consumes directly — fast and exact.
+ """
+ provider = GeminiBrain(model="gemini-3-flash-preview")
+ fake = _FakeGeminiClient()
+ provider._client = fake # type: ignore[assignment]
+
+ req = BrainRequest(
+ messages=(BrainMessage(role="user", content="ping"),), tools=(),
+ )
+ await _drain(provider.complete(req))
+
+ assert fake.calls, "no generate_content_stream call captured"
+ afc = fake.calls[0].get("automatic_function_calling")
+ assert afc is not None, (
+ "automatic_function_calling not set — Gemini will run its own tool loop"
+ )
+ assert getattr(afc, "disable", None) is True