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): park a mission whose critic has no capacity instead of failing it

Dennis Heide de Ramirez1b3d5faa3c4e
diff --git a/jarvis/missions/capacity.py b/jarvis/missions/capacity.py
index fb1c0abae6..6bc1769274 100644
--- a/jarvis/missions/capacity.py
+++ b/jarvis/missions/capacity.py
@@ -128,6 +128,24 @@ class WorkerCapacityUnavailable(RuntimeError):
self.detail = detail
+class CriticCapacityUnavailable(RuntimeError):
+ """The critic's own subscription has no capacity (spent window, dead or
+ missing login). Raised instead of grading on another family or a paid
+ key — a worker's paid approval never covers critic calls."""
+
+ def __init__(self, reason: CapacityWaitReason, provider: str, detail: str = "") -> None:
+ super().__init__(f"critic {provider}: {reason}" + (f" ({detail})" if detail else ""))
+ self.reason: CapacityWaitReason = reason
+ self.provider = provider
+ self.detail = detail
+
+
+#: How often a parked "review only" step may hit a critic failure that is NOT
+#: about capacity before the mission fails honestly (critic_unavailable)
+#: instead of waiting forever on a critic that is broken for another reason.
+MAX_REVIEW_RETRIES = 3
+
+
class CapacityDecisionRejected(RuntimeError):
"""A capacity decision that cannot apply: the mission is no longer
parked, there is no paid option, or the option changed since it was
@@ -233,6 +251,8 @@ __all__ = [
"CapacityDecision",
"CapacityDecisionRejected",
"CapacityWaitReason",
+ "CriticCapacityUnavailable",
+ "MAX_REVIEW_RETRIES",
"PaidOffer",
"PaidOption",
"WorkerCapacityUnavailable",
diff --git a/jarvis/missions/critic/runner.py b/jarvis/missions/critic/runner.py
index e8c5f23160..098380727e 100644
--- a/jarvis/missions/critic/runner.py
+++ b/jarvis/missions/critic/runner.py
@@ -1513,6 +1513,24 @@ class CriticRunner:
)
primary_provider, primary_model = _resolve_critic_provider_model()
+ # Capacity policy (jarvis/missions/capacity.py): a mission pinned to a
+ # subscription is graded on that subscription's critic ONLY. The
+ # cross-family walk below (codex, then per-token API critics) would
+ # bill a key nobody approved exactly when the subscription is spent —
+ # and a worker's paid approval never covers critic calls.
+ self._capacity_signal: tuple[str, str, str] | None = None
+ if _critic_subscription_only(primary_provider):
+ return await self._invoke_pinned_critic(
+ primary_provider=primary_provider,
+ primary_model=primary_model,
+ model=model,
+ prompt=prompt_for_subprocess,
+ codex_prompt=codex_prompt_for_subprocess,
+ worktree=worktree,
+ env=env,
+ iteration=iteration,
+ adversarial_reframe=adversarial_reframe,
+ )
# Auth-viability gate (2026-07-07, mission 019f3d18 / BUG-042 defect 5):
# this branch used to spawn `claude --print` UNCONDITIONALLY — with a
# dead Claude CLI auth both critic attempts exited 1, the mission died
@@ -1682,6 +1700,127 @@ class CriticRunner:
)
return None
+ # --- Internal: subscription-pinned critic (jarvis/missions/capacity.py) ---
+
+ def critic_family(self) -> str:
+ """The provider family that grades this install's missions."""
+ primary, _model = _resolve_critic_provider_model()
+ return _critic_family(primary)
+
+ async def _invoke_pinned_critic(
+ self,
+ *,
+ primary_provider: str | None,
+ primary_model: str | None,
+ model: str,
+ prompt: str,
+ codex_prompt: str,
+ worktree: Path,
+ env: dict[str, str],
+ iteration: int,
+ adversarial_reframe: bool,
+ ) -> CriticVerdict | None:
+ """Grade on the pinned subscription's own critic, or nowhere.
+
+ No codex/claude cross-over and no per-token API critic. A critic that
+ has no capacity raises :class:`CriticCapacityUnavailable` so the
+ mission parks; any other failure returns None as before.
+ """
+ from jarvis.missions.capacity import CriticCapacityUnavailable
+
+ family = _critic_family(primary_provider)
+ if family == "codex":
+ from jarvis.codex_auth_state import codex_needs_reauth
+ from jarvis.codex_quota_state import codex_in_quota_cooldown
+ from jarvis.missions.workers.codex_direct_worker import (
+ _normalize_model_for_codex,
+ )
+
+ if codex_needs_reauth():
+ raise CriticCapacityUnavailable("provider_auth", "codex")
+ if codex_in_quota_cooldown():
+ raise CriticCapacityUnavailable("provider_quota", "codex")
+ verdict = await self._invoke_via_codex_direct(
+ prompt=codex_prompt,
+ worktree=worktree,
+ env=env,
+ model=_normalize_model_for_codex(primary_model or model),
+ iteration=iteration,
+ adversarial_reframe=adversarial_reframe,
+ )
+ elif family == "claude":
+ 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:
+ raise CriticCapacityUnavailable(
+ "provider_unavailable", "claude", "the claude CLI is not installed"
+ )
+ if claude_in_quota_cooldown():
+ raise CriticCapacityUnavailable("provider_quota", "claude")
+ # The mission env never carries a per-token Anthropic key for a
+ # pinned install (missions/init.py), so a viable CLI here means
+ # the subscription login; a dead one parks the mission.
+ if not _claude_cli_critic_viable():
+ raise CriticCapacityUnavailable("provider_auth", "claude")
+ verdict = await self._invoke_via_claude_direct(
+ prompt=prompt,
+ worktree=worktree,
+ env=env,
+ model=(primary_model or model) if primary_provider == "claude-api" else model,
+ iteration=iteration,
+ adversarial_reframe=adversarial_reframe,
+ )
+ else:
+ # A pinned install whose worker is an API provider the user chose
+ # grades on that same provider only — never on another family.
+ api_provider, api_model = _resolve_api_critic_provider(
+ primary_provider, primary_model, excluded_providers=set()
+ )
+ if not api_provider or api_provider != primary_provider:
+ raise CriticCapacityUnavailable("provider_unavailable", family)
+ verdict = await self._invoke_via_api_critic(
+ prompt=prompt,
+ model=api_model,
+ provider=api_provider,
+ iteration=iteration,
+ adversarial_reframe=adversarial_reframe,
+ )
+ if verdict is not None:
+ return verdict
+ signal = self._capacity_signal
+ if signal is not None:
+ reason, signal_family, detail = signal
+ raise CriticCapacityUnavailable(reason, signal_family, detail) # type: ignore[arg-type]
+ return None
+
+ def _note_capacity_signal(self, family: str, text: str) -> None:
+ """Remember that a critic CLI said it has no capacity, and arm the
+ family's cooldown so an automatic resume waits for it."""
+ low = (text or "").lower()
+ if any(marker in low for marker in _CRITIC_AUTH_MARKERS):
+ reason = "provider_auth"
+ elif any(marker in low for marker in _CRITIC_QUOTA_MARKERS):
+ reason = "provider_quota"
+ else:
+ return
+ self._capacity_signal = (reason, family, (text or "").strip()[:200])
+ if reason == "provider_quota":
+ if family == "claude":
+ from jarvis.claude_quota_state import mark_claude_quota_cooldown
+
+ mark_claude_quota_cooldown()
+ elif family == "codex":
+ from jarvis.codex_quota_state import mark_codex_quota_cooldown
+
+ mark_codex_quota_cooldown()
+ elif family == "codex":
+ from jarvis.codex_auth_state import mark_codex_needs_reauth
+
+ mark_codex_needs_reauth()
+
# --- Internal: direct claude --print path (CRIT-1, 2026-05-17) ---
async def _invoke_via_claude_direct(
@@ -1857,6 +1996,7 @@ class CriticRunner:
"CriticRunner: claude-direct returncode=%d wall_ms=%d stderr=%r",
proc.returncode, wall_ms, stderr_text,
)
+ self._note_capacity_signal("claude", stderr_text + " " + stdout_text[:1000])
return None
stdout_text = stdout_b.decode("utf-8", errors="replace")
@@ -1882,6 +2022,11 @@ class CriticRunner:
"object in output) iter=%d adv=%s output[:300]=%r",
iteration, adversarial_reframe, stdout_text[:300],
)
+ # A spent window can exit 0 with just the limit notice on stdout. Only
+ # a short output counts: a long one is a review that merely mentions
+ # quotas or rate limits.
+ if len(stdout_text.strip()) <= _CAPACITY_NOTICE_MAX_CHARS:
+ self._note_capacity_signal("claude", stdout_text)
return None
# --- Internal: direct codex exec path (Welle 6, 2026-05-18) ---
@@ -2128,6 +2273,7 @@ class CriticRunner:
"returncode=%d wall_ms=%d stderr=%r",
proc.returncode, wall_ms, stderr_text,
)
+ self._note_capacity_signal("codex", stderr_text + " " + stdout_text[:1000])
return None
if proc.returncode != 0 and not terminal_ok:
@@ -2138,6 +2284,7 @@ class CriticRunner:
"returncode=%d wall_ms=%d stderr=%r",
proc.returncode, wall_ms, stderr_text,
)
+ self._note_capacity_signal("codex", stderr_text + " " + stdout_text[:1000])
return None
# Last agent_message wins. With --output-schema codex returns the
@@ -2165,6 +2312,49 @@ class CriticRunner:
# --- Helpers ---
+#: Critic CLI output that means "no capacity", not "bad review".
+_CRITIC_QUOTA_MARKERS: tuple[str, ...] = (
+ "session limit", "usage limit", "rate limit", "rate_limit", "too many requests",
+ "429", "out of credits", "out_of_credits", "credit balance", "quota", "overloaded",
+)
+_CRITIC_AUTH_MARKERS: tuple[str, ...] = (
+ "not logged in", "please log in", "log in again", "/login", "401",
+ "authentication_error", "invalid api key", "oauth token has expired",
+)
+#: A limit notice is one short line; longer output is a review.
+_CAPACITY_NOTICE_MAX_CHARS = 400
+
+
+def _critic_family(primary_provider: str | None) -> str:
+ """The subscription family behind a critic provider slug."""
+ if primary_provider == "claude-api":
+ return "claude"
+ if primary_provider in ("chatgpt", "openai-codex", "codex"):
+ return "codex"
+ return primary_provider or "claude"
+
+
+def _critic_subscription_only(primary_provider: str | None) -> bool:
+ """Whether the critic must stay on the mission's own subscription.
+
+ Same rule as the worker factory: the claude CLI or codex as the configured
+ backend pins the critic, and so does any install that connected a
+ subscription. Fails closed — an unreadable signal never opens paid keys.
+ """
+ try:
+ from jarvis.missions.capacity import pinned_to_subscription
+ from jarvis.missions.workers.claude_direct_worker import _resolve_claude_binary
+
+ family = _critic_family(primary_provider)
+ configured = family == "codex" or (
+ family == "claude" and _resolve_claude_binary() is not None
+ )
+ return pinned_to_subscription(configured_is_subscription=configured)
+ except Exception: # noqa: BLE001 - logged; waiting is the safe answer
+ logger.warning("CriticRunner: pinning signal unreadable — pinned critic", exc_info=True)
+ return True
+
+
def _claude_cli_critic_viable() -> bool:
"""True when the ``claude`` CLI can actually AUTHENTICATE for a critic spawn.
diff --git a/jarvis/missions/kontrollierer/orchestrator.py b/jarvis/missions/kontrollierer/orchestrator.py
index fc9f6320d6..1e1b134856 100644
--- a/jarvis/missions/kontrollierer/orchestrator.py
+++ b/jarvis/missions/kontrollierer/orchestrator.py
@@ -39,8 +39,10 @@ from typing import Any, Final, Literal
from ...core.process_utils import NO_WINDOW_CREATIONFLAGS
from ..budget import BudgetExceeded, BudgetTracker
from ..capacity import (
+ MAX_REVIEW_RETRIES,
CapacityDecision,
CapacityDecisionRejected,
+ CriticCapacityUnavailable,
PaidOffer,
PaidOption,
WorkerCapacityUnavailable,
@@ -424,6 +426,10 @@ class _ResumeClaim:
done: frozenset[str]
restore_dirs: dict[str, Path]
checkpoint: dict[str, Any]
+ #: Steps whose worker delivered and only the critic is owed.
+ review_only: frozenset[str] = frozenset()
+ #: Non-capacity critic failures so far, per step (bounded retries).
+ review_failures: dict[str, int] | None = None
def _plan_from_checkpoint(checkpoint: dict[str, Any] | None) -> MissionPlan | None:
@@ -913,6 +919,11 @@ class Kontrollierer:
self._resume_family: dict[str, str] = {}
self._resume_attempt: dict[str, int] = {}
self._resume_done_before: dict[str, int] = {}
+ # Steps whose worker delivered but whose critic could not run: the
+ # checkpoint marks them "pending_review" so a resume only reviews.
+ # ``_review_failures`` counts non-capacity critic failures per step.
+ self._pending_review: dict[str, set[str]] = {}
+ self._review_failures: dict[str, dict[str, int]] = {}
# Approved paid runs: the exact offer the user accepted, and what the
# run has spent so far. In memory on purpose — an approval covers one
# run of one mission and dies with it (or with the process).
@@ -1115,6 +1126,7 @@ class Kontrollierer:
*,
done_task_ids: frozenset[str] = frozenset(),
restore_dirs: dict[str, Path] | None = None,
+ review_only_task_ids: frozenset[str] = frozenset(),
) -> MissionState:
"""Run the plan's open steps, then aggregate every step's outcome.
@@ -1155,6 +1167,7 @@ class Kontrollierer:
reflections=reflections,
sem=sem,
restore_dir=restore_dirs.get(step.task_id),
+ review_only=step.task_id in review_only_task_ids,
)
task_outcomes.append(outcome)
step_outcomes[step.task_id] = outcome
@@ -1298,6 +1311,7 @@ class Kontrollierer:
reflections: ReflectionMemory,
sem: asyncio.Semaphore,
restore_dir: Path | None = None,
+ review_only: bool = False,
) -> str:
"""Runs a step through the Worker+Critic loop (max MAX_CRITIC_LOOPS).
@@ -1383,6 +1397,7 @@ class Kontrollierer:
worktree=worktree,
reflections=reflections,
resumed=restored,
+ review_only=review_only and restored,
)
finally:
# Archive + teardown run in a worker thread (git and file
@@ -1439,6 +1454,7 @@ class Kontrollierer:
worktree: Path,
reflections: ReflectionMemory,
resumed: bool = False,
+ review_only: bool = False,
) -> str:
"""Inner critic-loop body. Extracted so the worktree-finally in the
caller wraps every return path. ``resumed`` means the workspace holds
@@ -1471,7 +1487,7 @@ class Kontrollierer:
for iteration in range(MAX_CRITIC_LOOPS):
# Per-iteration: render reflections, spawn worker, capture diff+log
prior_block = reflections.render_for_worker_prompt(n=3)
- if resumed and iteration == 0:
+ if resumed and iteration == 0 and not review_only:
prior_block = "\n\n".join(b for b in (_RESUME_NOTE, prior_block) if b)
# Lead every worker prompt with the artifact-language directive so
# generated code defaults to English regardless of the request
@@ -1526,52 +1542,66 @@ class Kontrollierer:
mission_id, MissionState.CRITIQUING, f"iter-{iteration}-start"
)
- # Worker spawn (real or fake depending on the factory). A factory
- # that refuses to switch to a billing fallback parks the task.
- try:
- worker = self._make_worker(mission_id, step)
- except WorkerCapacityUnavailable as exc:
- logger.warning(
- "Task %s iter %d: no worker capacity (%s) — parking the "
- "mission instead of switching provider",
- step.task_id, iteration, exc,
- )
- self._capacity_wait[mission_id] = {
- "reason": exc.reason,
- "provider": exc.provider,
- "error_detail": exc.detail or None,
- }
- return TaskOutcome.WAITING_CAPACITY
log_dir = mission_dir / "tasks" / step.task_id[:13] / "logs"
log_dir.mkdir(parents=True, exist_ok=True)
-
- try:
- # BUG-LIVE-03 (2026-05-14): never reuse the `openclaw` session-id
- # across critic iterations. Live repro mission_019e2605
- # showed that `openclaw` 2026.5.7 prefers the failover chain
- # persisted in the session-state file over the explicit
- # `--model` CLI flag on resume — so iter1 silently retried
- # `openai/gpt-5.5` instead of `xai/grok-4.3` and died with
- # `chain_exhausted`. The Critic's correction context is
- # already injected into the worker prompt via
- # `prior_block` (see line 250), so a fresh session loses
- # nothing.
- spawn_result = await self._spawn_worker_collect(
- worker=worker,
- worker_prompt=worker_prompt,
- worktree=worktree,
- mission_dir=mission_dir,
- log_dir=log_dir,
- mission_id=mission_id,
- step=step,
- iteration=iteration,
- resume_session_id=None,
+ if review_only and iteration == 0:
+ # The worker delivered before the mission parked on its
+ # critic: that result is restored in the workspace and its log
+ # is still archived. Only the review is missing — run it, never
+ # the worker again (and never on a paid approval).
+ worker = None
+ spawn_result = self._SpawnResult(
+ worker_id=f"review-{step.task_id[:13]}",
+ cost_usd=0.0,
+ tokens_used=0,
+ session_id=None,
)
- except Exception: # noqa: BLE001
- logger.exception("Task %s iter %d: worker spawn failed", step.task_id, iteration)
- if iteration == MAX_CRITIC_LOOPS - 1:
- return TaskOutcome.ERROR
- continue
+ else:
+ # Worker spawn (real or fake depending on the factory). A factory
+ # that refuses to switch to a billing fallback parks the task.
+ try:
+ worker = self._make_worker(mission_id, step)
+ except WorkerCapacityUnavailable as exc:
+ logger.warning(
+ "Task %s iter %d: no worker capacity (%s) — parking the "
+ "mission instead of switching provider",
+ step.task_id, iteration, exc,
+ )
+ self._capacity_wait[mission_id] = {
+ "reason": exc.reason,
+ "provider": exc.provider,
+ "error_detail": exc.detail or None,
+ }
+ return TaskOutcome.WAITING_CAPACITY
+ try:
+ # BUG-LIVE-03 (2026-05-14): never reuse the `openclaw` session-id
+ # across critic iterations. Live repro mission_019e2605
+ # showed that `openclaw` 2026.5.7 prefers the failover chain
+ # persisted in the session-state file over the explicit
+ # `--model` CLI flag on resume — so iter1 silently retried
+ # `openai/gpt-5.5` instead of `xai/grok-4.3` and died with
+ # `chain_exhausted`. The Critic's correction context is
+ # already injected into the worker prompt via
+ # `prior_block` (see line 250), so a fresh session loses
+ # nothing.
+ spawn_result = await self._spawn_worker_collect(
+ worker=worker,
+ worker_prompt=worker_prompt,
+ worktree=worktree,
+ mission_dir=mission_dir,
+ log_dir=log_dir,
+ mission_id=mission_id,
+ step=step,
+ iteration=iteration,
+ resume_session_id=None,
+ )
+ except Exception: # noqa: BLE001
+ logger.exception(
+ "Task %s iter %d: worker spawn failed", step.task_id, iteration
+ )
+ if iteration == MAX_CRITIC_LOOPS - 1:
+ return TaskOutcome.ERROR
+ continue
approval = self._paid_approval.get(mission_id)
if approval is not None:
@@ -1923,6 +1953,20 @@ class Kontrollierer:
env=env,
security_tag=_detect_security_tag(step.prompt),
)
+ except CriticCapacityUnavailable as exc:
+ # The critic's own subscription has no capacity. The worker's
+ # result is kept (archived with the checkpoint) and only the
+ # review resumes later — no worker re-run, no other family,
+ # no paid critic.
+ logger.warning(
+ "Task %s iter %d: critic has no capacity (%s) — parking the "
+ "mission with the review pending",
+ step.task_id, iteration, exc,
+ )
+ return self._park_for_review(
+ mission_id, step, exc.reason, exc.provider, exc.detail,
+ counts_as_failure=False,
+ )
except CriticTimeout as exc:
# A critic timeout is TRANSIENT (the critic also shells out to
# `claude` over the same Claude Max OAuth; under concurrent load
@@ -1934,6 +1978,13 @@ class Kontrollierer:
"Task %s iter %d: critic timed out (transient, likely OAuth "
"contention): %s", step.task_id, iteration, exc,
)
+ if not _real_diff_is_empty(diff_text):
+ # Delivered work is never redone for a critic that did
+ # not answer: park and review it later (bounded).
+ return self._park_for_review(
+ mission_id, step, "provider_unavailable", self._critic_family(),
+ str(exc)[:200], counts_as_failure=True,
+ )
if iteration == MAX_CRITIC_LOOPS - 1:
# No iterations left to retry the critic.
return (
@@ -1955,6 +2006,14 @@ class Kontrollierer:
continue
except (CriticSchemaInvalid, CriticVerdictInconsistent) as exc:
logger.warning("Task %s iter %d: critic failed: %s", step.task_id, iteration, exc)
+ if not _real_diff_is_empty(diff_text):
+ # The worker delivered; the critic could not judge it.
+ # Keep the work, never re-run the worker, review it later
+ # — and fail honestly once the critic stays broken.
+ return self._park_for_review(
+ mission_id, step, "provider_unavailable", self._critic_family(),
+ str(exc)[:200], counts_as_failure=True,
+ )
# Live forensic 2026-05-16 (mission_019e3288): a Critic
# subprocess crash on iter0 (EPERM symlink + Unknown
# agent id) currently degrades to `continue` and the
@@ -3183,12 +3242,25 @@ class Kontrollierer:
await self._manager.store.touch_heartbeat(mission_id, now_ms())
except Exception as hb_exc: # noqa: BLE001 - ownership stamp is advisory
logger.debug("Resume heartbeat failed (non-fatal): %s", hb_exc)
+ review_only: set[str] = set()
+ review_failures: dict[str, int] = {}
+ for entry in checkpoint.get("steps") or []:
+ if not isinstance(entry, dict) or entry.get("task_id") not in plan_ids:
+ continue
+ task_id = str(entry["task_id"])
+ if entry.get("pending_review") is True and task_id not in done:
+ review_only.add(task_id)
+ failures = entry.get("review_failures")
+ if isinstance(failures, int) and failures > 0:
+ review_failures[task_id] = failures
return _ResumeClaim(
prompt=view.prompt,
plan=plan,
done=frozenset(done),
restore_dirs=restore_dirs,
checkpoint=checkpoint,
+ review_only=frozenset(review_only),
+ review_failures=review_failures,
)
async def _run_claimed_resume(
@@ -3215,6 +3287,8 @@ class Kontrollierer:
if paid is not None:
self._paid_approval[mission_id] = paid
self._paid_spent[mission_id] = 0.0
+ if claim.review_failures:
+ self._review_failures[mission_id] = dict(claim.review_failures)
logger.info(
"resume_mission: %s resumes on %s — %d/%d step(s) already done, "
@@ -3231,6 +3305,7 @@ class Kontrollierer:
claim.plan,
done_task_ids=claim.done,
restore_dirs=claim.restore_dirs,
+ review_only_task_ids=claim.review_only,
)
finally:
await self._record_paid_usage(mission_id)
@@ -3242,6 +3317,8 @@ class Kontrollierer:
self._resume_done_before.pop(mission_id, None)
self._paid_approval.pop(mission_id, None)
self._paid_spent.pop(mission_id, None)
+ self._pending_review.pop(mission_id, None)
+ self._review_failures.pop(mission_id, None)
# --- Paid-API approval ---------------------------------------------------
@@ -3410,6 +3487,57 @@ class Kontrollierer:
except Exception: # noqa: BLE001 - the log line above still records it
logger.exception("Mission %s: paid-usage event could not be stored", mission_id)
+ def _drop_review_state(self, mission_id: str) -> None:
+ """Forget a mission's pending-review bookkeeping (terminal paths).
+ ``getattr``: bare test fixtures build the class without __init__."""
+ for attr in ("_pending_review", "_review_failures"):
+ getattr(self, attr, {}).pop(mission_id, None)
+
+ def _critic_family(self) -> str:
+ """The family that grades missions (for the capacity readback)."""
+ family_fn = getattr(self._runner, "critic_family", None)
+ if callable(family_fn):
+ try:
+ return str(family_fn())
+ except Exception: # noqa: BLE001 - a label only; logged
+ logger.warning("critic family unreadable", exc_info=True)
+ return "claude"
+
+ def _park_for_review(
+ self,
+ mission_id: str,
+ step: Step,
+ reason: str,
+ provider: str,
+ detail: str,
+ *,
+ counts_as_failure: bool,
+ ) -> str:
+ """Park a step whose worker delivered but whose critic could not run.
+
+ The step is marked ``pending_review`` in the checkpoint so a resume
+ runs only the critic on the archived result. Capacity waits are
+ unbounded (the window comes back); other critic failures count, and
+ after ``MAX_REVIEW_RETRIES`` the task fails as critic_unavailable.
+ """
+ failures = self._review_failures.setdefault(mission_id, {})
+ if counts_as_failure:
+ failures[step.task_id] = failures.get(step.task_id, 0) + 1
+ if failures[step.task_id] >= MAX_REVIEW_RETRIES:
+ logger.error(
+ "Task %s: critic failed %d times on delivered work — "
+ "surfacing critic_unavailable",
+ step.task_id, failures[step.task_id],
+ )
+ return TaskOutcome.CRITIC_UNAVAILABLE
+ self._pending_review.setdefault(mission_id, set()).add(step.task_id)
+ self._capacity_wait[mission_id] = {
+ "reason": reason,
+ "provider": provider,
+ "error_detail": detail or None,
+ }
+ return TaskOutcome.WAITING_CAPACITY
+
def _check_resume_family(self, mission_id: str, worker: Any) -> None:
"""A resumed mission runs only on the provider family it was parked
on; anything else parks it again instead of switching unasked."""
@@ -3494,6 +3622,8 @@ class Kontrollierer:
# Answers of approved read-only steps feed the final summary; they go
# into the checkpoint so a resumed run can still report them.
task_answers = self._task_answers.pop(mission_id, [])
+ pending_review = self._pending_review.pop(mission_id, set())
+ review_failures = self._review_failures.pop(mission_id, {})
self._mission_failure_context.pop(mission_id, None)
attempt = self._resume_attempt.get(mission_id, 0)
done_before = self._resume_done_before.get(mission_id, 0)
@@ -3520,6 +3650,9 @@ class Kontrollierer:
"prompt": step.prompt,
"outcome": outcome,
"done": outcome == TaskOutcome.APPROVED,
+ # Worker delivered, critic still owed: a resume only reviews.
+ "pending_review": step.task_id in pending_review,
+ "review_failures": review_failures.get(step.task_id, 0),
"files_saved": n_files,
"artifacts_dir": str(files_dir.parent),
})
@@ -3590,6 +3723,7 @@ class Kontrollierer:
# Hygiene: a retried-then-approved mission must not leak a stale
# worker-failure context into a later run (mirror of _fail_mission).
self._mission_failure_context.pop(mission_id, None)
+ self._drop_review_state(mission_id)
transitioned = await self._safe_transition(
mission_id,
MissionState.APPROVED,
@@ -3706,6 +3840,7 @@ class Kontrollierer:
partial_artifacts: list[str] | None = None,
) -> None:
self._task_answers.pop(mission_id, None) # hygiene: drop captured answers
+ self._drop_review_state(mission_id)
# Consume the classified worker-failure context (if any) so the
# terminal MissionFailed event names the real cause instead of the
# bare mission-level reason (2026-07-06 incident: error_class was
diff --git a/tests/missions/critic/test_runner_claude_direct.py b/tests/missions/critic/test_runner_claude_direct.py
index f25c8c9744..0ed88a182d 100644
--- a/tests/missions/critic/test_runner_claude_direct.py
+++ b/tests/missions/critic/test_runner_claude_direct.py
@@ -1019,7 +1019,14 @@ async def test_dead_claude_cli_critic_crosses_to_api_family(
auth, the critic used to spawn ``claude --print`` unconditionally — two
returncode=1 runs → ``critic_unavailable`` → the mission FAILED even though
the worker (running on OpenRouter after the family walk) had delivered.
- A non-viable Claude CLI must fall through to the in-process API critic."""
+ A non-viable Claude CLI must fall through to the in-process API critic.
+
+ Only for an install NOT pinned to a subscription (key-only, no claude
+ CLI): a pinned one parks instead of grading on a paid key — see
+ tests/missions/test_capacity_critic.py."""
+ monkeypatch.setattr(
+ "jarvis.missions.critic.runner._critic_subscription_only", lambda _p: False
+ )
monkeypatch.setattr(
"jarvis.missions.critic.runner._resolve_critic_provider_model",
lambda: ("claude-api", "claude-sonnet-4-6"),
diff --git a/tests/missions/kontrollierer/test_loop.py b/tests/missions/kontrollierer/test_loop.py
index 69f6ff079d..92ebfea76a 100644
--- a/tests/missions/kontrollierer/test_loop.py
+++ b/tests/missions/kontrollierer/test_loop.py
@@ -307,22 +307,18 @@ async def test_iter1_does_not_resume_jarvis_agent_session(
@pytest.mark.asyncio
-async def test_critic_unavailable_short_circuits_when_iter0_has_real_diff(
+async def test_critic_crash_on_delivered_work_parks_and_fails_only_after_retries(
manager: MissionManager, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
- """Live forensic 2026-05-16 (mission_019e3288): the Critic subprocess
- crashed on iter0 (EPERM symlink on `plugin-skills/browser-automation`,
- then `Unknown agent id "critic"`), but the worker had already produced
- a real 1237-byte diff. The old behavior swallowed the crash via
- `continue`, let iter1+iter2 land no-op edits that reverted the diff
- back to empty, then exited with `critic_loop_exhausted` — the user
- heard a generic failure phrase and the iter0 work was lost.
-
- With the new short-circuit, iter0 crash + non-empty real diff yields
- a `MissionFailed` carrying ``reason="critic_unavailable"`` after
- exactly one critic call. The on-disk `diff.iter0.patch` records the
- work so the user can replay it.
+ """Live forensic 2026-05-16 (mission_019e3288): the Critic crashed on
+ iter0 while the worker had already produced a real diff. Delivered work
+ is never thrown away or redone for a critic that could not judge it
+ (2026-10-06): the mission parks with the review pending, a resume runs
+ ONLY the critic on the saved result, and after ``MAX_REVIEW_RETRIES``
+ failed reviews it fails honestly as ``critic_unavailable`` — with the
+ ``diff.iter0.patch`` it kept.
"""
+ from jarvis.missions.capacity import MAX_REVIEW_RETRIES
from jarvis.missions.critic.verdict import CriticSchemaInvalid
class _CrashingCritic:
@@ -341,7 +337,6 @@ async def test_critic_unavailable_short_circuits_when_iter0_has_real_diff(
critic=critic, # type: ignore[arg-type]
worker_factory_fn=lambda step: shared_worker,
)
- # Fake a non-empty real diff so the short-circuit condition fires.
monkeypatch.setattr(
Kontrollierer,
"_capture_diff",
@@ -352,25 +347,27 @@ async def test_critic_unavailable_short_circuits_when_iter0_has_real_diff(
"+real BUG-021 content\n"
),
)
+ # The fake worktrees are plain dirs: report the archived work as restored.
+ monkeypatch.setattr(Kontrollierer, "_restore_task_workspace", lambda self, wt, art: True)
mid = await manager.dispatch(prompt="add BUG-021 entry to BUGS.md")
- end_state = await k.run_mission(mid)
+ assert await k.run_mission(mid) == MissionState.WAITING_CAPACITY
+ assert (len(critic.calls), len(shared_worker.spawn_calls)) == (1, 1)
- assert end_state == MissionState.FAILED
- # Exactly one critic call: short-circuit fires immediately.
- assert len(critic.calls) == 1
- # Mission-failed event carries the new reason.
+ for attempt in range(2, MAX_REVIEW_RETRIES):
+ assert await k.resume_mission(mid) == MissionState.WAITING_CAPACITY
+ assert len(critic.calls) == attempt
+ assert await k.resume_mission(mid) == MissionState.FAILED
+
+ # Every retry was a review only — the worker ran exactly once.
+ assert len(shared_worker.spawn_calls) == 1
+ assert len(critic.calls) == MAX_REVIEW_RETRIES
events = await manager.store.events_for_mission(mid)
- failed = [
- e.payload for e in events if e.payload.event_type == "MissionFailed"
- ]
+ failed = [e.payload for e in events if e.payload.event_type == "MissionFailed"]
assert len(failed) == 1
assert failed[0].reason == "critic_unavailable" # type: ignore[attr-defined]
- # Partial-artifacts records the iter0 diff file we kept.
artifacts = failed[0].partial_artifacts # type: ignore[attr-defined]
- assert any("diff.iter0.patch" in str(p) for p in artifacts), (
- f"expected diff.iter0.patch in partial_artifacts, got {artifacts!r}"
- )
+ assert any("diff.iter0.patch" in str(p) for p in artifacts), artifacts
@pytest.mark.asyncio
diff --git a/tests/missions/test_capacity_critic.py b/tests/missions/test_capacity_critic.py
new file mode 100644
index 0000000000..324d6cf735
--- /dev/null
+++ b/tests/missions/test_capacity_critic.py
@@ -0,0 +1,309 @@
+"""A critic without capacity parks the mission; it never fails delivered work,
+re-runs a worker, or grades on another family or a paid key (2026-10-06).
+
+- The pinned critic stays on its subscription: no codex/claude cross-over,
+ no per-token API critic — a worker's paid approval never covers critic calls.
+- A critic that says "no capacity" parks the mission with the review pending.
+- A resume runs ONLY the missing review on the saved worker result.
+"""
+from __future__ import annotations
+
+from pathlib import Path
+from typing import Any
+
+import pytest
+
+from jarvis.claude_quota_state import claude_in_quota_cooldown, clear_claude_quota_cooldown
+from jarvis.missions.capacity import (
+ PAID_MISSION_CAP_USD,
+ CriticCapacityUnavailable,
+ PaidOffer,
+ WorkerCapacityUnavailable,
+ read_checkpoint,
+)
+from jarvis.missions.critic.runner import CriticRunner
+from jarvis.missions.critic.verdict import REQUIRED_AXES, CriticAxis, CriticVerdict
+from jarvis.missions.events import MissionFailed, MissionPaidUsage, MissionWaitingCapacity
+from jarvis.missions.kontrollierer.decomposer import MissionPlan, Step
+from jarvis.missions.kontrollierer.orchestrator import Kontrollierer
+from jarvis.missions.manager import MissionManager
+from jarvis.missions.state_machine import MissionState
+from tests.fakes.fake_mission_runtime import (
+ FakeMissionWorker,
+ FakePaidOption,
+ make_kontrollierer,
+)
+from tests.missions.critic.test_runner_claude_direct import _patch_direct
+
+SESSION_LIMIT = "You've hit your session limit · resets 11:10pm"
+
+
[email protected](autouse=True)
+def _fresh_cooldowns():
+ clear_claude_quota_cooldown()
+ yield
+ clear_claude_quota_cooldown()
+
+
+def _pin(monkeypatch: pytest.MonkeyPatch) -> None:
+ """A Claude-subscription install: the claude CLI is present."""
+ monkeypatch.setattr(
+ "jarvis.missions.workers.claude_direct_worker._resolve_claude_binary",
+ lambda: "/usr/local/bin/claude",
+ )
+
+
+def _no_paid_critic(monkeypatch: pytest.MonkeyPatch) -> None:
+ async def _forbidden(self: Any, **_k: Any) -> None:
+ raise AssertionError("a pinned critic must never grade on a paid API key")
+
+ monkeypatch.setattr(CriticRunner, "_invoke_via_api_critic", _forbidden)
+ monkeypatch.setattr(
+ "jarvis.missions.critic.runner._resolve_api_critic_provider",
+ lambda *a, **k: ("openrouter", "some/model"),
+ )
+
+
+async def _review(tmp_path: Path) -> CriticVerdict:
+ return await CriticRunner().run(
+ mission_prompt="Build X",
+ worker_diff="diff --git a/x b/x\n+x\n",
+ worker_log="log",
+ prior_reflections="",
+ iteration=0,
+ worktree=tmp_path,
+ env={},
+ )
+
+
+# --- The critic runner ------------------------------------------------------------
+
+
+async def test_spent_window_on_the_critic_raises_capacity_not_a_paid_fallback(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ _patch_direct(monkeypatch, stdout=SESSION_LIMIT, returncode=1)
+ _pin(monkeypatch)
+ _no_paid_critic(monkeypatch)
+
+ with pytest.raises(CriticCapacityUnavailable) as exc:
+ await _review(tmp_path)
+
+ assert (exc.value.reason, exc.value.provider) == ("provider_quota", "claude")
+ # The resume waits for the window instead of retrying at once.
+ assert claude_in_quota_cooldown()
+
+
+async def test_dead_claude_login_parks_instead_of_crossing_families(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ _patch_direct(monkeypatch, stdout="")
+ _pin(monkeypatch)
+ _no_paid_critic(monkeypatch)
+ monkeypatch.setattr("jarvis.missions.critic.runner._claude_cli_critic_viable", lambda: False)
+
+ with pytest.raises(CriticCapacityUnavailable) as exc:
+ await _review(tmp_path)
+ assert (exc.value.reason, exc.value.provider) == ("provider_auth", "claude")
+
+
+async def test_codex_critic_never_falls_back_to_claude(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ monkeypatch.setattr(
+ "jarvis.missions.critic.runner._resolve_critic_provider_model",
+ lambda: ("chatgpt", ""),
+ )
+ monkeypatch.setattr("jarvis.codex_auth_state.codex_needs_reauth", lambda: False)
+ monkeypatch.setattr("jarvis.codex_quota_state.codex_in_quota_cooldown", lambda **_k: True)
+
+ async def _no_claude(self: Any, **_k: Any) -> None:
+ raise AssertionError("a codex-pinned critic must not run on Claude")
+
+ monkeypatch.setattr(CriticRunner, "_invoke_via_claude_direct", _no_claude)
+ _no_paid_critic(monkeypatch)
+
+ with pytest.raises(CriticCapacityUnavailable) as exc:
+ await _review(tmp_path)
+ assert (exc.value.reason, exc.value.provider) == ("provider_quota", "codex")
+
+
+async def test_a_long_review_that_mentions_quotas_is_not_a_capacity_signal(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ """Only a short limit notice counts; a review about rate limiting does not."""
+ from jarvis.missions.critic.verdict import CriticSchemaInvalid
+
+ review_prose = "The patch adds a rate limit and a quota check. " * 30
+ _patch_direct(monkeypatch, stdout=review_prose)
+ _pin(monkeypatch)
+ _no_paid_critic(monkeypatch)
+
+ with pytest.raises(CriticSchemaInvalid):
+ await _review(tmp_path)
+ assert not claude_in_quota_cooldown()
+
+
+# --- The orchestrator: park, then review only -------------------------------------
+
+
+class ScriptedCritic:
+ """Raises the queued errors in order, then approves; records each call."""
+
+ def __init__(self, *errors: Exception) -> None:
+ self._errors = list(errors)
+ self.calls: list[dict[str, Any]] = []
+
+ def critic_family(self) -> str:
+ return "claude"
+
+ async def run(self, **kwargs: Any) -> CriticVerdict:
+ self.calls.append(kwargs)
+ if self._errors:
+ raise self._errors.pop(0)
+ return CriticVerdict(
+ verdict="approve",
+ axes={ax: CriticAxis(status="pass", evidence=["x:1"]) for ax in REQUIRED_AXES},
+ issues=[],
+ correction_instruction="",
+ summary="ok",
+ summary_de="ok",
+ confidence=0.9,
+ suggested_next_action="accept",
+ )
+
+
+DELIVERED_DIFF = "diff --git a/report.md b/report.md\n@@ -0,0 +1 @@\n+the report\n"
+
+
+async def manager(tmp_missions_db: Path):
+ m = MissionManager(tmp_missions_db)
+ await m.start()
+ yield m
+ await m.stop()
+
+
[email protected](autouse=False)
+def delivered(monkeypatch: pytest.MonkeyPatch) -> None:
+ """The worker's result is a real diff, restorable after a park."""
+ monkeypatch.setattr(Kontrollierer, "_capture_diff", lambda self, wt: DELIVERED_DIFF)
+ monkeypatch.setattr(Kontrollierer, "_restore_task_workspace", lambda self, wt, art: True)
+
+
+class CountingFactory:
+ def __init__(self) -> None:
+ self.capacity = True
+ self.workers: list[FakeMissionWorker] = []
+
+ def __call__(self, _step: Step) -> FakeMissionWorker:
+ if not self.capacity:
+ raise WorkerCapacityUnavailable("provider_quota", "claude")
+ worker = FakeMissionWorker(family="claude")
+ self.workers.append(worker)
+ return worker
+
+ def spawned(self) -> int:
+ return sum(len(w.prompts) for w in self.workers)
+
+
+async def _payloads(manager: MissionManager, mission_id: str) -> list[Any]:
+ return [e.payload for e in await manager.store.events_for_mission(mission_id)]
+
+
+async def test_spent_critic_parks_delivered_work_and_resume_only_reviews(
+ manager: MissionManager, tmp_path: Path, delivered: None
+) -> None:
+ factory = CountingFactory()
+ critic = ScriptedCritic(CriticCapacityUnavailable("provider_quota", "claude"))
+ plan = MissionPlan(steps=[Step(slug="report", prompt="write it")], n_workers=1)
+ k = make_kontrollierer(manager, tmp_path, plan, factory, critic=critic)
+ mission_id = await manager.dispatch(prompt="write the report")
+
+ assert await k.run_mission(mission_id) == MissionState.WAITING_CAPACITY
+ payloads = await _payloads(manager, mission_id)
+ assert not any(isinstance(p, MissionFailed) for p in payloads)
+ wait = [p for p in payloads if isinstance(p, MissionWaitingCapacity)][-1]
+ assert (wait.reason, wait.provider) == ("provider_quota", "claude")
+ checkpoint = read_checkpoint(tmp_path / "missions" / f"mission_{mission_id[:13]}")
+ assert checkpoint is not None
+ assert checkpoint["steps"][0]["pending_review"] is True
+ assert factory.spawned() == 1
+
+ # Capacity is back: the resume runs the critic on the saved result only.
+ assert await k.resume_waiting_missions() == [mission_id]
+ assert await manager.mission(mission_id) is not None
+ view = await manager.mission(mission_id)
+ assert view is not None and view.state == MissionState.APPROVED
+ assert factory.spawned() == 1 # the worker never ran again
+ assert len(critic.calls) == 2
+ assert critic.calls[-1]["worker_diff"] == DELIVERED_DIFF
+
+
+async def test_capacity_waits_never_count_toward_failing_the_review(
+ manager: MissionManager, tmp_path: Path, delivered: None
+) -> None:
+ from jarvis.missions.capacity import MAX_REVIEW_RETRIES
+
+ factory = CountingFactory()
+ spent = [CriticCapacityUnavailable("provider_quota", "claude")] * (MAX_REVIEW_RETRIES + 2)
+ critic = ScriptedCritic(*spent)
+ plan = MissionPlan(steps=[Step(slug="report", prompt="write it")], n_workers=1)
+ k = make_kontrollierer(manager, tmp_path, plan, factory, critic=critic)
+ mission_id = await manager.dispatch(prompt="write the report")
+
+ assert await k.run_mission(mission_id) == MissionState.WAITING_CAPACITY
+ for _ in range(MAX_REVIEW_RETRIES + 1):
+ assert await k.resume_mission(mission_id) == MissionState.WAITING_CAPACITY
+ assert await k.resume_mission(mission_id) == MissionState.APPROVED
+ assert factory.spawned() == 1
+
+
+async def test_paid_run_with_spent_critic_parks_without_paying_again(
+ manager: MissionManager, tmp_path: Path, delivered: None
+) -> None:
+ """The approved paid worker runs once; its critic is spent → park. The
+ approval dies with the run: the review later resumes on the subscription,
+ and neither the paid worker nor a paid critic runs again."""
+ offer = PaidOffer(
+ provider="claude-api",
+ model="claude-sonnet-4-6",
+ estimated_cost_usd=1.0,
+ cost_cap_usd=PAID_MISSION_CAP_USD,
+ reason="provider_quota",
+ open_steps=1,
+ )
+ paid = FakePaidOption(offer, cost_per_spawn=0.4)
+ factory = CountingFactory()
+ factory.capacity = False # Claude Pro is spent: the first run parks
+ critic = ScriptedCritic(CriticCapacityUnavailable("provider_quota", "claude"))
+ plan = MissionPlan(steps=[Step(slug="report", prompt="write it")], n_workers=1)
+ k = make_kontrollierer(manager, tmp_path, plan, factory, critic=critic, paid_option=paid)
+ mission_id = await manager.dispatch(prompt="write the report")
+ assert await k.run_mission(mission_id) == MissionState.WAITING_CAPACITY
+ assert len(critic.calls) == 0
+
+ await k.decide_capacity(mission_id, "approve_paid", provider=offer.provider, model=offer.model)
+ for run in list(k._background_runs):
+ await run
+
+ view = await manager.mission(mission_id)
+ assert view is not None and view.state == MissionState.WAITING_CAPACITY
+ assert sum(len(w.prompts) for w in paid.workers) == 1 # paid worker ran once
+ assert len(critic.calls) == 1
+ payloads = await _payloads(manager, mission_id)
+ assert [p.cost_usd for p in payloads if isinstance(p, MissionPaidUsage)] == [0.4]
+ checkpoint = read_checkpoint(tmp_path / "missions" / f"mission_{mission_id[:13]}")
+ assert checkpoint is not None
+ assert checkpoint["provider"] == "claude" # waits for the subscription, not the key
+ assert checkpoint["steps"][0]["pending_review"] is True
+ assert k._paid_approval == {}
+
+ # Subscription back: only the review runs — no paid worker, no new worker.
+ factory.capacity = True
+ assert await k.resume_waiting_missions() == [mission_id]
+ view = await manager.mission(mission_id)
+ assert view is not None and view.state == MissionState.APPROVED
+ assert sum(len(w.prompts) for w in paid.workers) == 1
+ assert factory.spawned() == 0
+ assert len(critic.calls) == 2