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(missions): never let a spent Claude subscription fall back to a stored API key

Dennis Heide de Ramirez3435f347905a
diff --git a/jarvis/missions/init.py b/jarvis/missions/init.py
index 8772d19928..048bd85cea 100644
--- a/jarvis/missions/init.py
+++ b/jarvis/missions/init.py
@@ -517,6 +517,20 @@ def _live_subagent_provider(boot_snapshot: str | None) -> str | None:
return boot_snapshot
+def _anthropic_key_for_cli_env(key: str | None, *, cli_present: bool) -> str | None:
+ """The Anthropic credential a mission's ``claude`` CLI may see, or None.
+
+ With the CLI installed, missions are pinned to its subscription login
+ (jarvis/missions/capacity.py), so only that login's OAuth bearer may
+ reach the process — worker and critic alike. A classic per-token key in
+ ``ANTHROPIC_API_KEY`` would make the CLI bill it silently; paid use runs
+ only in an approved run, in-process, never through this environment.
+ """
+ if key and cli_present and not key.startswith("sk-ant-oat"):
+ return None
+ return key
+
+
def _claude_subscription_login_state() -> bool | None:
"""The ``claude`` CLI's SUBSCRIPTION login: usable (True), proven dead this
session (False), or absent (None).
@@ -1242,6 +1256,15 @@ async def bootstrap_missions(
# viability gate) or fails with the honest "Not logged in".
anthropic_key = None
+ if live_provider in {"claude", "claude-api"}:
+ from jarvis.missions.workers.claude_direct_worker import (
+ _resolve_claude_binary,
+ )
+
+ anthropic_key = _anthropic_key_for_cli_env(
+ anthropic_key, cli_present=_resolve_claude_binary() is not None
+ )
+
return build_worker_env(
run_dir=mission_dir,
anthropic_api_key=anthropic_key,
@@ -1317,11 +1340,13 @@ async def bootstrap_missions(
# install the mission stays on Claude's subscription login — a spent
# window or a dead login parks it in WAITING_CAPACITY instead of
# moving it to codex, another family, or the Anthropic API key.
+ # Pinned whenever the CLI exists — NOT only when the login probe
+ # answers: a logged-out or unreadable subscription login (probe ->
+ # None) must park the mission too, never reopen the legacy chain
+ # that runs the CLI on a stored per-token Anthropic key.
binary_present = _resolve_claude_binary() is not None
login = _claude_subscription_login_state() if binary_present else None
- if pinned_to_subscription(
- configured_is_subscription=binary_present and login is not None
- ):
+ if pinned_to_subscription(configured_is_subscription=binary_present):
return _pinned_claude_worker(
capability_inventory, binary_present=binary_present, login=login
)
diff --git a/tests/missions/test_capacity_no_key_escape.py b/tests/missions/test_capacity_no_key_escape.py
new file mode 100644
index 0000000000..9f0a2f697e
--- /dev/null
+++ b/tests/missions/test_capacity_no_key_escape.py
@@ -0,0 +1,188 @@
+"""No automatic escape from the Claude subscription to a stored Anthropic key.
+
+User rule (2026-10-06): under NO condition may a mission whose Claude
+subscription is spent, logged out or unreadable run on a stored per-token
+Anthropic API key on its own — not on the first run, not on any resume. Only
+an explicit per-mission approval may bill a key.
+
+These tests drive the REAL worker factory and env builder from
+``bootstrap_missions``. Every credential is a fake string; no secret store is
+read.
+"""
+from __future__ import annotations
+
+from pathlib import Path
+from typing import Any
+
+import pytest
+
+from jarvis.missions import init as mi
+from jarvis.missions.capacity import WorkerCapacityUnavailable
+from jarvis.missions.kontrollierer.decomposer import MissionPlan, Step
+from jarvis.missions.manager import MissionManager
+from jarvis.missions.state_machine import MissionState
+from jarvis.missions.workers.api_agent_worker import ApiAgentWorker
+from jarvis.missions.workers.claude_direct_worker import ClaudeDirectWorker
+from tests.fakes.fake_mission_runtime import FakePaidOption, make_kontrollierer
+
+FAKE_API_KEY = "sk-ant-api03-FAKE-NOT-A-SECRET"
+FAKE_OAUTH = "sk-ant-oat01-FAKE-NOT-A-SECRET"
+
+
+class World:
+ """The probes the factory and env builder read, pinned to a known state."""
+
+ def __init__(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ self.login: bool | None = None # None = logged out / probe unreadable
+ self.cooldown = False
+ self.live_oauth: str | None = None
+ self.native_subscription: object | None = None
+ mp = monkeypatch
+ mp.setattr(mi, "_live_subagent_provider", lambda _snapshot: "claude-api")
+ mp.setattr(
+ "jarvis.missions.workers.claude_direct_worker._resolve_claude_binary",
+ lambda: "/usr/local/bin/claude",
+ )
+ mp.setattr(mi, "_claude_subscription_login_state", lambda: self.login)
+ mp.setattr(
+ "jarvis.claude_quota_state.claude_in_quota_cooldown",
+ lambda **_k: self.cooldown,
+ )
+ # Every paid / cross-family door is open, so only the policy can close it.
+ mp.setattr(mi, "_claude_cli_auth_viable", lambda: True)
+ mp.setattr(mi, "_api_key_family_viable", lambda _p: True)
+ mp.setattr(
+ "jarvis.missions.workers.codex_direct_worker._codex_oauth_available",
+ lambda: True,
+ )
+ mp.setattr("jarvis.codex_auth_state.codex_needs_reauth", lambda: False)
+ mp.setattr("jarvis.codex_quota_state.codex_in_quota_cooldown", lambda **_k: False)
+ mp.setattr("jarvis.core.config.get_jarvis_agent_secret", lambda _p: FAKE_API_KEY)
+ mp.setattr(mi, "read_live_claude_oauth_token", lambda: self.live_oauth)
+ mp.setattr(
+ "jarvis.claude_auth.usable_native_claude_subscription",
+ lambda: self.native_subscription,
+ )
+ mp.setattr(mi, "_assemble_worker_mcp_servers", lambda **_k: ())
+
+
+def world(monkeypatch: pytest.MonkeyPatch) -> World:
+ return World(monkeypatch)
+
+
+async def stack(tmp_path: Path, world: World):
+ result = await mi.bootstrap_missions(
+ db_path=tmp_path / "missions.db", isolation_root=tmp_path / "sub-agents"
+ )
+ yield result
+ await mi.shutdown_missions(result)
+
+
+def _step() -> Step:
+ return Step(slug="task", prompt="write the report")
+
+
+# --- The factory never escapes to the key -----------------------------------------
+
+
+ "login,cooldown,reason",
+ [
+ (None, False, "provider_auth"), # logged out / unreadable login
+ (False, False, "provider_auth"), # login proven dead
+ (None, True, "provider_quota"), # spent window AND unreadable login
+ (True, True, "provider_quota"), # spent Claude Pro window
+ ],
+)
+async def test_spent_or_unreadable_subscription_parks_instead_of_using_the_key(
+ stack: dict[str, Any], world: World, login: bool | None, cooldown: bool, reason: str
+) -> None:
+ world.login, world.cooldown = login, cooldown
+ factory = stack["kontrollierer"]._worker_factory
+
+ with pytest.raises(WorkerCapacityUnavailable) as exc:
+ factory(_step())
+
+ assert (exc.value.reason, exc.value.provider) == (reason, "claude")
+
+
+async def test_live_subscription_runs_claude_without_any_fallback(
+ stack: dict[str, Any], world: World
+) -> None:
+ world.login = True
+ worker = stack["kontrollierer"]._worker_factory(_step())
+ assert isinstance(worker, ClaudeDirectWorker)
+ assert worker.backend_fallback is False
+ assert not isinstance(worker, ApiAgentWorker)
+
+
+# --- The CLI environment never carries a per-token key ----------------------------
+
+
[email protected]("native", [None, object()])
+async def test_cli_env_never_carries_a_classic_api_key(
+ stack: dict[str, Any], world: World, tmp_path: Path, native: object | None
+) -> None:
+ world.native_subscription = native # with or without a readable login
+ env = stack["kontrollierer"]._env_builder(tmp_path / "mission")
+ assert env.get("ANTHROPIC_API_KEY") != FAKE_API_KEY
+ assert FAKE_API_KEY not in env.values()
+
+
+async def test_cli_env_keeps_the_subscription_login(
+ stack: dict[str, Any], world: World, tmp_path: Path
+) -> None:
+ world.live_oauth = FAKE_OAUTH
+ env = stack["kontrollierer"]._env_builder(tmp_path / "mission")
+ assert FAKE_OAUTH in env.values()
+ assert FAKE_API_KEY not in env.values()
+
+
+def test_key_filter_rule() -> None:
+ assert mi._anthropic_key_for_cli_env(FAKE_API_KEY, cli_present=True) is None
+ assert mi._anthropic_key_for_cli_env(FAKE_OAUTH, cli_present=True) == FAKE_OAUTH
+ assert mi._anthropic_key_for_cli_env(None, cli_present=True) is None
+ # Without the CLI there is no subscription to protect; the key is untouched.
+ assert mi._anthropic_key_for_cli_env(FAKE_API_KEY, cli_present=False) == FAKE_API_KEY
+
+
+# --- First run and every resume stay parked until approval -------------------------
+
+
+async def manager(tmp_missions_db: Path):
+ m = MissionManager(tmp_missions_db)
+ await m.start()
+ yield m
+ await m.stop()
+
+
+async def test_no_resume_ever_falls_back_to_the_key(
+ stack: dict[str, Any], world: World, manager: MissionManager, tmp_path: Path
+) -> None:
+ """Park on a logged-out login, then run resume passes in every non-live
+ state: the mission stays parked and no paid worker is ever built."""
+ paid = FakePaidOption(None)
+ real_factory = stack["kontrollierer"]._worker_factory
+ built: list[Any] = []
+
+ def _factory(step: Step) -> Any:
+ worker = real_factory(step)
+ built.append(worker)
+ return worker
+
+ plan = MissionPlan(steps=[_step()], n_workers=1)
+ k = make_kontrollierer(manager, tmp_path / "k", plan, _factory, paid_option=paid)
+ mission_id = await manager.dispatch(prompt="write the report")
+ assert await k.run_mission(mission_id) == MissionState.WAITING_CAPACITY
+
+ for login, cooldown in [(None, False), (False, False), (True, True), (None, True)]:
+ world.login, world.cooldown = login, cooldown
+ assert await k.resume_waiting_missions() == []
+ view = await manager.mission(mission_id)
+ assert view is not None and view.state == MissionState.WAITING_CAPACITY
+
+ assert built == [] # no worker of any kind, and certainly no key worker
+ assert paid.workers == []