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): run structured codex work on the ChatGPT subscription before the paid key

rubenluetke10-beepd814cd9731c4
diff --git a/jarvis/plugins/brain/codex.py b/jarvis/plugins/brain/codex.py
index 98df0884d7..208c1fe3b0 100644
--- a/jarvis/plugins/brain/codex.py
+++ b/jarvis/plugins/brain/codex.py
@@ -745,6 +745,42 @@ class CodexBrain:
return
api_key = self._api_key()
+ # Background structured work (wiki extraction, judge, briefs) is plain
+ # JSON text with no tools, and the ChatGPT subscription answers it at
+ # no per-token charge. The key lookup above also accepts the generic
+ # OPENAI_API_KEY, so without this a key added for voice silently
+ # billed every memory pass on the most expensive model (live
+ # 2026-09-29: ~$0.30 of wiki calls after a two-minute voice chat).
+ # The key stays the fallback when the subscription turn fails.
+ if (
+ api_key
+ and self._structured_prompts
+ and not req.tools
+ and await asyncio.to_thread(_codex_oauth_connected)
+ ):
+ log.info(
+ "CodexBrain.complete: structured request on the ChatGPT subscription (model=%s)",
+ self._cli_model or "default",
+ )
+ sub_emitted = False
+ try:
+ stream = (
+ self._complete_via_cli(req)
+ if self._subscription_text_only
+ else self._complete_via_app_server(req)
+ )
+ async for delta in stream:
+ if delta.content:
+ sub_emitted = True
+ yield delta
+ return
+ except Exception as exc: # noqa: BLE001 — re-raised once output was emitted
+ if sub_emitted:
+ raise
+ log.warning(
+ "CodexBrain: subscription path failed (%s) — using the API key",
+ type(exc).__name__,
+ )
if api_key:
log.info("CodexBrain.complete: API-key path (model=%s)", self._model)
client = self._ensure_client(api_key)
diff --git a/tests/unit/brain/test_codex_brain.py b/tests/unit/brain/test_codex_brain.py
index 00dcb98045..8182959886 100644
--- a/tests/unit/brain/test_codex_brain.py
+++ b/tests/unit/brain/test_codex_brain.py
@@ -119,6 +119,74 @@ async def test_complete_raises_without_any_key_or_oauth(
pass
+def _key_and_subscription(monkeypatch: pytest.MonkeyPatch, api_calls: list[str]) -> None:
+ monkeypatch.setattr("jarvis.core.config.get_provider_secret", lambda _p: "sk-paid")
+ monkeypatch.setattr("jarvis.plugins.brain.codex._codex_oauth_connected", lambda: True)
+
+ async def _api_stream(_client, _model, _req) -> AsyncIterator[BrainDelta]:
+ api_calls.append("api")
+ yield BrainDelta(content="from-api")
+
+ monkeypatch.setattr("jarvis.plugins.brain.codex.stream_complete", _api_stream)
+ monkeypatch.setattr(CodexBrain, "_ensure_client", lambda self, _key: object())
+
+
+async def test_structured_request_prefers_subscription_over_paid_key(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ api_calls: list[str] = []
+ _key_and_subscription(monkeypatch, api_calls)
+
+ async def _subscription(self, _req) -> AsyncIterator[BrainDelta]:
+ yield BrainDelta(content="from-subscription")
+
+ monkeypatch.setattr(CodexBrain, "_complete_via_app_server", _subscription)
+ brain = CodexBrain(structured_prompts=True)
+ req = BrainRequest(messages=(BrainMessage(role="user", content="extract facts"),))
+
+ text = "".join([d.content or "" async for d in brain.complete(req)])
+
+ assert text == "from-subscription"
+ assert api_calls == []
+
+
+async def test_structured_request_falls_back_to_key_when_subscription_fails(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ api_calls: list[str] = []
+ _key_and_subscription(monkeypatch, api_calls)
+
+ async def _broken(self, _req) -> AsyncIterator[BrainDelta]:
+ raise RuntimeError("app server down")
+ yield BrainDelta() # pragma: no cover - makes this an async generator
+
+ monkeypatch.setattr(CodexBrain, "_complete_via_app_server", _broken)
+ brain = CodexBrain(structured_prompts=True)
+ req = BrainRequest(messages=(BrainMessage(role="user", content="extract facts"),))
+
+ text = "".join([d.content or "" async for d in brain.complete(req)])
+
+ assert text == "from-api"
+ assert api_calls == ["api"]
+
+
+async def test_conversational_request_keeps_the_api_key_path(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ api_calls: list[str] = []
+ _key_and_subscription(monkeypatch, api_calls)
+ brain = CodexBrain()
+ req = BrainRequest(messages=(BrainMessage(role="user", content="hello"),))
+
+ text = "".join([d.content or "" async for d in brain.complete(req)])
+
+ assert text == "from-api"
+ assert api_calls == ["api"]
+
+
def test_ensure_client_uses_given_key(monkeypatch: pytest.MonkeyPatch) -> None:
captured: dict[str, str] = {}