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): harden the Codex API-to-CLI crossover per review

Ruben Lütke25a24837f6e4
diff --git a/jarvis/memory/wiki/session_rollup.py b/jarvis/memory/wiki/session_rollup.py
index b5ca006cd6..56ae955688 100644
--- a/jarvis/memory/wiki/session_rollup.py
+++ b/jarvis/memory/wiki/session_rollup.py
@@ -439,10 +439,21 @@ class SessionRollupWorker:
try:
async with self._brain_lock:
if self._brain is None:
+ # Same curator-tier instantiation as the extractor/judge:
+ # disables Gemini thinking AND flags structured prompts so
+ # a subscription-CLI brain forwards the digest instructions
+ # verbatim instead of the conversational "1-3 short
+ # sentences" wrapper that contradicts the requested
+ # flowing paragraph.
+ from jarvis.memory.wiki.curator_llm import (
+ instantiate_curator_brain,
+ )
+
self._brain = await asyncio.to_thread(
- self._registry.instantiate,
+ instantiate_curator_brain,
+ self._registry,
provider_name,
- model=model,
+ model,
)
except Exception as exc: # noqa: BLE001
log.warning(
diff --git a/jarvis/plugins/brain/codex.py b/jarvis/plugins/brain/codex.py
index ddc0446344..d2d1589146 100644
--- a/jarvis/plugins/brain/codex.py
+++ b/jarvis/plugins/brain/codex.py
@@ -368,7 +368,7 @@ class CodexBrain:
emitted = False
try:
async for delta in stream_complete(client, self._model, req):
- if delta.content or getattr(delta, "tool_calls", None):
+ if delta.content or delta.tool_call:
emitted = True
yield delta
return
@@ -385,7 +385,20 @@ class CodexBrain:
# stream produced nothing a listener could have consumed.
if emitted or status not in (401, 402, 403, 429):
raise
- if not _codex_oauth_connected():
+ if req.tools:
+ # The CLI path is tool-blind; answering a tool turn with
+ # plausible prose would LOOK like "model chose no tool"
+ # and defeat BrainManager's delegation to a genuinely
+ # tool-capable provider. Let the chain handle it instead.
+ log.warning(
+ "CodexBrain: API-key path failed (HTTP %s) with %d "
+ "tool(s) requested — not crossing to the tool-blind "
+ "CLI; surfacing the error for provider fallback",
+ status,
+ len(req.tools),
+ )
+ raise
+ if not await asyncio.to_thread(_codex_oauth_connected):
raise
log.warning(
"CodexBrain: API-key path failed (HTTP %s) — falling back "
@@ -395,7 +408,9 @@ class CodexBrain:
async for delta in self._complete_via_cli(req):
yield delta
return
- oauth = _codex_oauth_connected()
+ # to_thread: the probe shells out to `codex --version` on its first
+ # call per process (up to ~4s) — never block the event loop for it.
+ oauth = await asyncio.to_thread(_codex_oauth_connected)
log.info(
"CodexBrain.complete: no API key — oauth=%s, %d tool(s) requested "
"(ignored on the CLI path)", oauth, len(req.tools or ()),
diff --git a/tests/unit/plugins/brain/test_codex_brain.py b/tests/unit/plugins/brain/test_codex_brain.py
index 12e1787fc0..01fd4de102 100644
--- a/tests/unit/plugins/brain/test_codex_brain.py
+++ b/tests/unit/plugins/brain/test_codex_brain.py
@@ -98,6 +98,46 @@ async def test_throttled_api_key_crosses_over_to_the_subscription_cli(
assert calls == ["cli"]
+async def test_partial_tool_call_stream_never_crosses_over(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """A stream that already emitted a tool_call delta must re-raise on a
+ later 429 — crossing over would append CLI prose behind a half-consumed
+ tool turn (double yield into the aggregator)."""
+ brain = CodexBrain()
+ calls = _arm_api_and_oauth(monkeypatch, brain, status=429)
+
+ async def _tool_then_429(client: Any, model: str, req: BrainRequest):
+ yield BrainDelta(tool_call={"name": "open_app", "arguments": "{}"})
+ raise _StatusError(429)
+
+ monkeypatch.setattr(codex_module, "stream_complete", _tool_then_429)
+
+ with pytest.raises(_StatusError):
+ await _collect(brain.complete(_wiki_request()))
+ assert calls == []
+
+
+async def test_tool_turns_surface_the_error_instead_of_going_tool_blind(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """With tools requested, the tool-blind CLI must not answer in prose that
+ looks like 'model chose no tool' — the error surfaces so BrainManager's
+ fallback can pick a genuinely tool-capable provider."""
+ brain = CodexBrain()
+ calls = _arm_api_and_oauth(monkeypatch, brain, status=429)
+ req = BrainRequest(
+ messages=(BrainMessage(role="user", content="Open the calculator."),),
+ tools=({"name": "open_app"},),
+ )
+
+ with pytest.raises(_StatusError):
+ await _collect(brain.complete(req))
+ assert calls == []
+
+
@pytest.mark.asyncio
async def test_non_account_errors_still_raise(
monkeypatch: pytest.MonkeyPatch,