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
fix(missions): honor cooldowns and cross-family fallback in worker resolution
Ruben Lütke9df2dd545c30
diff --git a/jarvis/missions/init.py b/jarvis/missions/init.pyindex 92eb69f114..41d7ba5569 100644--- a/jarvis/missions/init.py+++ b/jarvis/missions/init.py@@ -644,6 +644,85 @@ def _cross_family_last_resort_worker(task_text: str) -> Any | None:return None+def _resolve_api_agent_worker(provider: str, task_text: str) -> Any:+ """Worker for an ``api_agent``-kind subagent provider (openai / openrouter /+ nvidia run ON their OWN provider via the in-process ApiAgentWorker).++ Viability-gated (not existence-gated, ``_api_key_family_viable``): a bare+ key-existence check re-picked a family a worker already proved+ quota-depleted / auth-dead every single critic round instead of crossing+ to a healthy one (BUG-042 twin, AP-22). When the provider is not viable,+ tries the user's other provider families before the honest Claude last+ resort — never a guaranteed-fail worker.+ """+ try:+ has_key = _api_key_family_viable(provider)+ except Exception: # noqa: BLE001 — any resolve failure => no usable key+ has_key = False+ if has_key:+ logger.info(+ "Mission worker -> ApiAgentWorker on %r (in-process tool-use "+ "loop over the provider's own API, writes files in the worktree).",+ provider,+ )+ return ApiAgentWorker(provider)+ logger.warning(+ "Mission worker: subagent provider %r has no API key configured, "+ "so it cannot run — trying the user's other provider families "+ "before the Claude last resort (open-source AP-22/AP-23).",+ provider,+ )+ cross = _cross_family_last_resort_worker(task_text)+ if cross is not None:+ return cross+ return ClaudeDirectWorker(mcp_servers=_assemble_worker_mcp_servers(task_text=task_text))+++def _resolve_codex_dead_login_worker(task_text: str) -> Any:+ """Worker to use when the codex ChatGPT login is flagged dead this session+ (``kind == "codex_direct"`` and ``codex_needs_reauth()`` is True).++ Mirrors ``_resolve_api_agent_worker`` / the claude_direct branch's Claude+ viability gates (CLI binary + auth, quota cooldown, or an Anthropic API+ key) instead of handing back an unconditional ``ClaudeDirectWorker`` —+ and crosses to another provider family when Claude itself is not+ reachable either (open-source AP-22/AP-23).+ """+ from jarvis.claude_quota_state import claude_in_quota_cooldown+ from jarvis.missions.workers.claude_direct_worker import _resolve_claude_binary++ if _resolve_claude_binary() is None and _api_key_family_viable("claude-api"):+ logger.info(+ "Mission worker -> ApiAgentWorker('claude-api'): codex login is "+ "dead and no `claude` CLI binary is present, running in-process "+ "on the Anthropic API key."+ )+ return ApiAgentWorker("claude-api")+ claude_cli_ready = (+ _resolve_claude_binary() is not None+ and _claude_cli_auth_viable()+ and not claude_in_quota_cooldown()+ )+ if claude_cli_ready:+ logger.warning(+ "Mission worker -> ClaudeDirectWorker: codex ChatGPT login "+ "is flagged dead this session — running on Claude Max until "+ "`codex login` restores it (avoids the dead-provider double "+ "fallback)."+ )+ return ClaudeDirectWorker(mcp_servers=_assemble_worker_mcp_servers(task_text=task_text))+ logger.warning(+ "Mission worker: codex login is dead and Claude is not reachable "+ "either (no CLI auth, quota cooldown, or no Anthropic key) — "+ "crossing provider families instead of a guaranteed-fail worker "+ "(open-source AP-22/AP-23)."+ )+ cross = _cross_family_last_resort_worker(task_text)+ if cross is not None:+ return cross+ return ClaudeDirectWorker(mcp_servers=_assemble_worker_mcp_servers(task_text=task_text))++async def bootstrap_missions(*,db_path: Path,@@ -781,7 +860,11 @@ async def bootstrap_missions(_spawn_boot_cleanup(_bg_worktree_sweep(), name="mission-boot-worktree-sweep")# 5. CriticRunner- critic_runner = CriticRunner()+ # Same containment job factory the orchestrator (Kontrollierer) uses for+ # workers below (AP-10) — a critic subprocess (claude-direct / codex-direct)+ # previously escaped every ambient job (create_worker_subprocess sets+ # CREATE_BREAKAWAY_FROM_JOB) and never got one of its own.+ critic_runner = CriticRunner(job_factory=_default_job_factory)# 6. MissionDecomposerdecomposer = MissionDecomposer(brain=brain_caller)@@ -1052,17 +1135,13 @@ async def bootstrap_missions(from jarvis.codex_auth_state import codex_needs_reauthif codex_needs_reauth():- logger.warning(- "Mission worker -> ClaudeDirectWorker: codex ChatGPT login "- "is flagged dead this session — running on Claude Max until "- "`codex login` restores it (avoids the dead-provider double "- "fallback)."+ # Mirror of the claude_direct branch above (open-source+ # AP-22/AP-23): a dead codex login must not hand back an+ # unconditional ClaudeDirectWorker — see+ # `_resolve_codex_dead_login_worker`.+ return _resolve_codex_dead_login_worker(+ getattr(step, "prompt", "") or "")- return ClaudeDirectWorker(- mcp_servers=_assemble_worker_mcp_servers(- task_text=getattr(step, "prompt", "") or ""- )- )return CodexDirectWorker()if kind == "antigravity":# "antigravity" (Google subscription): drive the official `agy` CLI@@ -1080,40 +1159,12 @@ async def bootstrap_missions()return GoogleCliWorker()if kind == "api_agent":- # grok / openai / openrouter: run ON the selected provider via the- # in-process ApiAgentWorker. Honest credential gate — if no API key- # is configured the provider CANNOT run, so fall back to Claude Max- # (mission still completes) instead of spawning a guaranteed-fail- # worker (e.g. openai/openrouter with no key).+ # grok / openai / openrouter / nvidia: run ON the selected+ # provider via the in-process ApiAgentWorker — see+ # `_resolve_api_agent_worker` (viability-gated, cross-family+ # fallback, honest Claude last resort).provider = live_provider or ""- try:- from jarvis.core import config as _cfg-- ep = _cfg.resolve_provider_endpoint(provider, vendor_default_base_url="")- has_key = bool(getattr(ep, "credential", None))- except Exception: # noqa: BLE001 — any resolve failure => no usable key- has_key = False- if has_key:- logger.info(- "Mission worker -> ApiAgentWorker on %r (in-process tool-use "- "loop over the provider's own API, writes files in the worktree).",- provider,- )- return ApiAgentWorker(provider)- logger.warning(- "Mission worker: subagent provider %r has no API key configured, "- "so it cannot run — trying the user's other provider families "- "before the Claude last resort (open-source AP-22/AP-23).",- provider,- )- cross = _cross_family_last_resort_worker(getattr(step, "prompt", "") or "")- if cross is not None:- return cross- return ClaudeDirectWorker(- mcp_servers=_assemble_worker_mcp_servers(- task_text=getattr(step, "prompt", "") or ""- )- )+ return _resolve_api_agent_worker(provider, getattr(step, "prompt", "") or "")if kind == "gemini":# B4 (open-source AP-22): no Gemini CLI but a Gemini API key → run the# heavy worker IN-PROCESS via ApiAgentWorker instead of failing on thediff --git a/tests/missions/test_api_agent_worker_resolution.py b/tests/missions/test_api_agent_worker_resolution.pynew file mode 100644index 0000000000..54f8ac8c7f--- /dev/null+++ b/tests/missions/test_api_agent_worker_resolution.py@@ -0,0 +1,173 @@+"""Regression: two mission worker-factory branches must be VIABILITY-gated+(not existence-gated) and cross provider families instead of re-picking a+dead/depleted provider every critic round or handing back a guaranteed-fail+worker.++1. ``kind == "api_agent"`` (openai/openrouter/nvidia run on their own API key)+ previously used a bare ``bool(getattr(ep, "credential", None))`` existence+ check — a family a worker already proved quota-depleted / auth-dead this+ session was re-picked EVERY critic round instead of crossing to a healthy+ one (BUG-042 twin, AP-22). Fixed by routing through the same+ ``_api_key_family_viable`` helper the sibling branches already use.+2. ``kind == "codex_direct"`` with a dead ChatGPT login previously returned+ ``ClaudeDirectWorker`` unconditionally, without checking whether Claude+ itself is actually reachable (CLI binary + auth, quota cooldown, or an+ API key) — mirrors the claude_direct branch's own gates and crosses to+ another provider family when Claude is not reachable either (AP-22/AP-23).++Both branches were extracted into standalone, directly-testable helpers+(``_resolve_api_agent_worker`` / ``_resolve_codex_dead_login_worker``) so the+worker-factory closure inside ``bootstrap_missions`` stays a thin dispatcher —+same pattern as ``_cross_family_last_resort_worker``.+"""+from __future__ import annotations++import pytest++from jarvis.missions import init as mi+from jarvis.missions.workers.api_agent_worker import ApiAgentWorker+from jarvis.missions.workers.claude_direct_worker import ClaudeDirectWorker++# --- _resolve_api_agent_worker ------------------------------------------------+++def test_viable_provider_runs_in_process(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(mi, "_api_key_family_viable", lambda p: p == "openrouter")+ worker = mi._resolve_api_agent_worker("openrouter", "do the task")+ assert isinstance(worker, ApiAgentWorker)+ assert worker.provider == "openrouter"+++def test_unviable_provider_crosses_family_instead_of_being_reused(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """BUG-042 twin: a provider a worker already proved quota-depleted /+ auth-dead this session must NOT be re-picked just because a key exists —+ `_api_key_family_viable` reports False, so the branch crosses family."""+ monkeypatch.setattr(mi, "_api_key_family_viable", lambda p: False)+ monkeypatch.setattr(+ mi, "_cross_family_last_resort_worker",+ lambda task_text: ApiAgentWorker("gemini"),+ )+ worker = mi._resolve_api_agent_worker("openrouter", "do the task")+ assert isinstance(worker, ApiAgentWorker)+ assert worker.provider == "gemini"+++def test_no_family_reachable_falls_back_to_claude_honest_fail(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setattr(mi, "_api_key_family_viable", lambda p: False)+ monkeypatch.setattr(mi, "_cross_family_last_resort_worker", lambda task_text: None)+ monkeypatch.setattr(mi, "_assemble_worker_mcp_servers", lambda **_k: {})+ worker = mi._resolve_api_agent_worker("openrouter", "do the task")+ assert isinstance(worker, ClaudeDirectWorker)+++def test_viability_check_failure_is_treated_as_no_key(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ def _boom(_p: str) -> bool:+ raise RuntimeError("keyring unavailable")++ monkeypatch.setattr(mi, "_api_key_family_viable", _boom)+ monkeypatch.setattr(+ mi, "_cross_family_last_resort_worker",+ lambda task_text: ApiAgentWorker("gemini"),+ )+ worker = mi._resolve_api_agent_worker("openrouter", "do the task")+ assert isinstance(worker, ApiAgentWorker)+ assert worker.provider == "gemini"+++# --- _resolve_codex_dead_login_worker ----------------------------------------+++def _patch_codex_dead_login(+ monkeypatch: pytest.MonkeyPatch,+ *,+ claude_binary: str | None,+ claude_auth_viable: bool = True,+ claude_quota_capped: bool = False,+ claude_api_key_viable: bool = False,+) -> None:+ monkeypatch.setattr(+ "jarvis.missions.workers.claude_direct_worker._resolve_claude_binary",+ lambda: claude_binary,+ )+ monkeypatch.setattr(mi, "_claude_cli_auth_viable", lambda: claude_auth_viable)+ monkeypatch.setattr(+ "jarvis.claude_quota_state.claude_in_quota_cooldown",+ lambda: claude_quota_capped,+ )+ monkeypatch.setattr(+ mi, "_api_key_family_viable",+ lambda p: claude_api_key_viable if p == "claude-api" else False,+ )+ monkeypatch.setattr(mi, "_assemble_worker_mcp_servers", lambda **_k: {})+++def test_dead_codex_login_no_binary_uses_claude_api_key_in_process(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ _patch_codex_dead_login(monkeypatch, claude_binary=None, claude_api_key_viable=True)+ worker = mi._resolve_codex_dead_login_worker("do the task")+ assert isinstance(worker, ApiAgentWorker)+ assert worker.provider == "claude-api"+++def test_dead_codex_login_viable_claude_cli_runs_claude_direct(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ _patch_codex_dead_login(+ monkeypatch, claude_binary="/usr/bin/claude", claude_auth_viable=True,+ )+ worker = mi._resolve_codex_dead_login_worker("do the task")+ assert isinstance(worker, ClaudeDirectWorker)+++def test_dead_codex_login_claude_quota_cooldown_crosses_family(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Both subscription lanes are dead this session (codex needs reauth,+ Claude in its OWN quota cooldown) — must cross to another provider+ family instead of handing back a doomed ClaudeDirectWorker."""+ _patch_codex_dead_login(+ monkeypatch,+ claude_binary="/usr/bin/claude",+ claude_auth_viable=True,+ claude_quota_capped=True,+ )+ monkeypatch.setattr(+ mi, "_cross_family_last_resort_worker",+ lambda task_text: ApiAgentWorker("openrouter"),+ )+ worker = mi._resolve_codex_dead_login_worker("do the task")+ assert isinstance(worker, ApiAgentWorker)+ assert worker.provider == "openrouter"+++def test_dead_codex_login_and_dead_claude_auth_crosses_family(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ _patch_codex_dead_login(+ monkeypatch, claude_binary="/usr/bin/claude", claude_auth_viable=False,+ )+ monkeypatch.setattr(+ mi, "_cross_family_last_resort_worker",+ lambda task_text: ApiAgentWorker("gemini"),+ )+ worker = mi._resolve_codex_dead_login_worker("do the task")+ assert isinstance(worker, ApiAgentWorker)+ assert worker.provider == "gemini"+++def test_dead_codex_login_nothing_reachable_falls_back_to_claude_honest_fail(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ _patch_codex_dead_login(+ monkeypatch, claude_binary="/usr/bin/claude", claude_auth_viable=False,+ )+ monkeypatch.setattr(mi, "_cross_family_last_resort_worker", lambda task_text: None)+ worker = mi._resolve_codex_dead_login_worker("do the task")+ assert isinstance(worker, ClaudeDirectWorker)