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(dictation): stop sending Gemini a deadline its server always rejects

Ruben Lütke843a3ca5c1f1
diff --git a/jarvis/dictation/polish_client.py b/jarvis/dictation/polish_client.py
index 6a3d167a04..a40cbbffd2 100644
--- a/jarvis/dictation/polish_client.py
+++ b/jarvis/dictation/polish_client.py
@@ -785,6 +785,14 @@ def _relax_payload(payload: dict[str, Any], body: str) -> dict[str, Any] | None:
return adjusted if changed else None
+#: Shortest request deadline the generate-content endpoint accepts. Anything
+#: under it is rejected BEFORE the model runs, with
+#: ``400 INVALID_ARGUMENT: Manually set deadline Ns is too short. Minimum
+#: allowed deadline is 10s.`` — so a polish budget below this cannot buy a
+#: faster answer, only a guaranteed failure.
+_GEMINI_MIN_DEADLINE_S = 10.0
+
+
class GeminiPolishClient:
"""Adapter for Google's generate-content schema."""
@@ -804,9 +812,22 @@ class GeminiPolishClient:
# an explicit http_options timeout is the ONLY thing below the
# caller's wait_for that can stop a hung request from holding a
# connection open after we have already given up on it.
+ #
+ # It is deliberately NOT the polish budget. This family's budget is
+ # 1.5 s, and sending that as the request deadline made the server
+ # reject every single call with a 400 before the model ran: measured
+ # on the live log, Gemini failed 56 times and succeeded 0 times, so a
+ # user who pinned it never once got the provider they chose — every
+ # dictation crossed silently to the next family (AP-31: a switch
+ # whose value is ignored). The deadline is what the SERVER is willing
+ # to accept; the caller's ``wait_for`` is what actually bounds the
+ # wait, and it is unchanged. Giving up at 1.5 s and letting the
+ # socket close on its own is strictly better than not asking at all.
self._client = genai.Client(
api_key=self._api_key,
- http_options={"timeout": int(max(timeout_s, 0.1) * 1000)},
+ http_options={
+ "timeout": int(max(timeout_s, _GEMINI_MIN_DEADLINE_S) * 1000)
+ },
)
return self._client
diff --git a/tests/unit/dictation/test_polish_chain.py b/tests/unit/dictation/test_polish_chain.py
index 70d577bc38..9f15f6ec9f 100644
--- a/tests/unit/dictation/test_polish_chain.py
+++ b/tests/unit/dictation/test_polish_chain.py
@@ -23,6 +23,8 @@ from __future__ import annotations
import asyncio
import json
+import sys
+import types
from dataclasses import dataclass, field
from typing import Any
@@ -568,6 +570,64 @@ async def test_a_gemini_sdk_failure_becomes_a_polish_provider_error() -> None:
)
+def test_the_gemini_request_deadline_never_goes_under_the_server_minimum(
+ monkeypatch,
+) -> None:
+ """The polish budget must not become the REQUEST deadline.
+
+ generate-content rejects anything under 10 s outright — ``400
+ INVALID_ARGUMENT: Manually set deadline 2s is too short`` — before the model
+ runs. This family's budget is 1.5 s, so sending it as the deadline made every
+ call fail: measured on the live log, 56 failures and 0 successes, i.e. a user
+ who pinned Gemini never once got it and every dictation crossed silently to
+ another family (AP-31). The caller's ``wait_for`` still bounds the wait.
+ """
+ from jarvis.dictation import polish_client as pc
+
+ captured: dict[str, object] = {}
+
+ class _Client:
+ def __init__(self, **kwargs: object) -> None:
+ captured.update(kwargs)
+
+ monkeypatch.setitem(
+ sys.modules, "google", types.SimpleNamespace(genai=types.SimpleNamespace(Client=_Client))
+ )
+ monkeypatch.setitem(
+ sys.modules, "google.genai", types.SimpleNamespace(Client=_Client)
+ )
+
+ client = GeminiPolishClient(GEMINI, model="m", api_key="k")
+ client._ensure_client(1.5)
+
+ timeout_ms = captured["http_options"]["timeout"] # type: ignore[index]
+ assert timeout_ms >= pc._GEMINI_MIN_DEADLINE_S * 1000, (
+ f"a {timeout_ms} ms deadline is rejected by the server before the "
+ "model runs, so the pinned provider can never answer"
+ )
+
+
+def test_a_generous_caller_budget_is_still_honoured(monkeypatch) -> None:
+ """The floor raises a too-short deadline; it does not cap a long one."""
+ captured: dict[str, object] = {}
+
+ class _Client:
+ def __init__(self, **kwargs: object) -> None:
+ captured.update(kwargs)
+
+ monkeypatch.setitem(
+ sys.modules, "google", types.SimpleNamespace(genai=types.SimpleNamespace(Client=_Client))
+ )
+ monkeypatch.setitem(
+ sys.modules, "google.genai", types.SimpleNamespace(Client=_Client)
+ )
+
+ client = GeminiPolishClient(GEMINI, model="m", api_key="k")
+ client._ensure_client(30.0)
+
+ assert captured["http_options"]["timeout"] == 30_000 # type: ignore[index]
+
+
@dataclass
class _FakeGenaiResponse:
text: str