The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(local-runtime): review-deferral audit — bare /refine immediacy, cloud fast path, dispatch-time enabled gate
emozillacccc03815265
diff --git a/agent/review_idle_queue.py b/agent/review_idle_queue.pyindex 9c962cf5848..2d31501c8e4 100644--- a/agent/review_idle_queue.py+++ b/agent/review_idle_queue.py@@ -82,9 +82,21 @@ def review_targets_managed_local(agent: Any,exact-matches its netloc against the supervisor state file — thematcher that cannot false-positive on external local servers. Anyfailure reads False: immediate spawn is always the safe default.++ Order matters: the netloc probe (one TTL-cached state-file read)+ runs FIRST, so machines with no managed server — every cloud-only+ install — return False without resolving the review runtime at all.+ This wrapper runs on the turn's tail; runtime resolution belongs on+ that path only when a managed server actually exists."""try:- from agent.auxiliary_client import _is_managed_local_endpoint+ from agent.auxiliary_client import (+ _is_managed_local_endpoint,+ _managed_local_netloc,+ )++ if not _managed_local_netloc():+ return Falsefrom agent.background_review import _resolve_review_runtimeruntime = _resolve_review_runtime(agent, task_cfg)@@ -205,6 +217,12 @@ class ReviewIdleQueue:try:item = self._pop_dispatchable()if item is not None:+ if not self._still_enabled(item):+ logger.info(+ "Deferred background review dropped: reviews "+ "were disabled while it was queued (session=%s)",+ item.session_key[-12:])+ continuelogger.info("Dispatching deferred background review ""(session=%s, waited=%.0fs, queued=%d)",@@ -218,6 +236,23 @@ class ReviewIdleQueue:if item is None:time.sleep(_POLL_INTERVAL_S)+ @staticmethod+ def _still_enabled(item: _PendingReview) -> bool:+ """Re-check the enabled gate at DISPATCH time.++ The entry wrapper gates at enqueue time, but minutes may pass in+ the queue — a user who sets background_review.enabled: false while+ a review waits means it, and the dispatch must not resurrect it.+ Fail-open like the gate itself (a broken config never silently+ disables reviews)."""+ try:+ from agent.background_review import load_background_review_settings++ enabled, _ = load_background_review_settings()+ return enabled+ except Exception: # noqa: BLE001+ return True+def _managed_server_idle() -> bool:"""Machine-level idle: no processing slot on any loaded model of thediff --git a/hermes_cli/cli_commands_mixin.py b/hermes_cli/cli_commands_mixin.pyindex 3ee64cd6829..17058ba5ad4 100644--- a/hermes_cli/cli_commands_mixin.py+++ b/hermes_cli/cli_commands_mixin.py@@ -2793,6 +2793,7 @@ class CLICommandsMixin:review_memory=True,review_skills=review_skills,focus=focus or None,+ explicit=True,)except Exception as exc:_cprint(f" /refine failed to start: {exc}")diff --git a/run_agent.py b/run_agent.pyindex 296ece8b7f3..40aaffcd528 100644--- a/run_agent.py+++ b/run_agent.py@@ -1886,6 +1886,7 @@ class AIAgent:review_memory: bool = False,review_skills: bool = False,focus: Optional[str] = None,+ explicit: bool = False,) -> None:"""Post-turn review entry point: decide WHEN, then spawn.@@ -1896,9 +1897,16 @@ class AIAgent:(auxiliary.background_review.defer: auto|never). Everything else —cloud runtimes, external local servers, explicit /refine — spawnsimmediately, exactly as before.++ ``explicit`` marks a user-initiated review (/refine, with or+ without focus text): never deferred. It does NOT touch the+ delegate/enabled gates below — those stay keyed on ``focus`` so a+ bare /refine keeps its historical gating behavior."""- # Delegation-subagent and enabled gates live here so a DEFERRED- # dispatch cannot resurrect a review the config has since disabled.+ # Delegation-subagent and enabled gates run here at enqueue/spawn+ # time; the idle dispatcher re-checks the enabled gate again at+ # dispatch time so a review queued for minutes cannot be+ # resurrected after the user disables reviews.if focus is None and getattr(self, "_delegate_depth", 0) > 0:returntask_cfg = None@@ -1915,7 +1923,7 @@ class AIAgent:focus=focus,task_cfg=task_cfg,)- if focus is None:+ if focus is None and not explicit:from agent.review_idle_queue import (QUEUE,defer_mode,diff --git a/tests/agent/test_review_idle_queue.py b/tests/agent/test_review_idle_queue.pyindex 21645d1bea1..43082f76e97 100644--- a/tests/agent/test_review_idle_queue.py+++ b/tests/agent/test_review_idle_queue.py@@ -231,6 +231,56 @@ def test_wrapper_refine_bypasses_queue(monkeypatch):assert calls["spawned"][0]["focus"] == "save the deploy workflow"+def test_wrapper_bare_refine_bypasses_queue(monkeypatch):+ """/refine with no focus text is still explicit: never deferred."""+ spawn, calls = _wrapper_agent(monkeypatch, defer="auto", managed=True)+ spawn([{"role": "user", "content": "hi"}], review_memory=True,+ focus=None, explicit=True)+ assert calls["enqueued"] == []+ assert len(calls["spawned"]) == 1+++def test_wrapper_cloud_fast_path_skips_runtime_resolution(monkeypatch):+ """No managed server on the machine -> the classifier answers from the+ TTL-cached netloc probe alone, without resolving the review runtime.+ Guards the cloud-only turn tail from growing new work."""+ from agent import review_idle_queue as riq++ resolved = {"count": 0}++ def _explode(agent, cfg):+ resolved["count"] += 1+ raise AssertionError("runtime resolution must not run")++ monkeypatch.setattr(+ "agent.auxiliary_client._managed_local_netloc", lambda: "")+ monkeypatch.setattr(+ "agent.background_review._resolve_review_runtime", _explode)+ assert riq.review_targets_managed_local(object(), {}) is False+ assert resolved["count"] == 0+++def test_dispatcher_rechecks_enabled_gate(monkeypatch):+ """A review disabled while queued must not be resurrected at dispatch."""+ from agent import review_idle_queue as riq++ q, clock = _make_queue()+ agent = _FakeAgent()+ q.enqueue(agent, "s1", {"task_cfg": {}})+ monkeypatch.setattr(+ "agent.background_review.load_background_review_settings",+ lambda: (False, {}),+ )+ item = None+ clock["t"] += _IDLE_SETTLE_S + 1+ q.note_turn_started()+ q.note_turn_finished()+ clock["t"] += _IDLE_SETTLE_S + 1+ item = q._pop_dispatchable()+ assert item is not None+ assert q._still_enabled(item) is False++# ── requeue on preemption ────────────────────────────────────────