diff --git a/loopx/cli_commands/quota_monitor_poll.py b/loopx/cli_commands/quota_monitor_poll.py index 030e6c353e..49b58f44b8 100644 --- a/loopx/cli_commands/quota_monitor_poll.py +++ b/loopx/cli_commands/quota_monitor_poll.py @@ -13,6 +13,7 @@ find_heartbeat_receipt, heartbeat_receipt_settlement_todo_id, ) +from ..control_plane.scheduler.arbitration import monitor_quiet_stall_observation_required from ..control_plane.scheduler.execution_context import ( SchedulerExecutionContextResolution, ) @@ -201,10 +202,9 @@ def record_automatic_heartbeat_stall( turn_instance_id=turn_id, goal_ref=goal_ref, ) - if ( - payload.get("effective_action") - != EffectiveAction.MONITOR_QUIET_SKIP.value - and existing_stall is None + if existing_stall is None and ( + payload.get("effective_action") != EffectiveAction.MONITOR_QUIET_SKIP.value + or not monitor_quiet_stall_observation_required(payload) ): return payload, status_payload, cache_metadata, "not_applicable" from ..capabilities.explore.composition_frontier import ( diff --git a/loopx/control_plane/scheduler/arbitration.py b/loopx/control_plane/scheduler/arbitration.py index d9ee86f085..400f669fe3 100644 --- a/loopx/control_plane/scheduler/arbitration.py +++ b/loopx/control_plane/scheduler/arbitration.py @@ -6,6 +6,7 @@ from typing import Any from ..agents.agent_scope_frontier import AGENT_SCOPE_FRONTIER_SCHEMA_VERSION, AgentScopeFrontierAction +from ..effect_runtime import effect_runtime_result from ..goals.goal_vision_wait import exact_blocked_successor_wait_state SCHEDULER_ARBITRATION_SCHEMA_VERSION = "scheduler_arbitration_v0" @@ -110,3 +111,14 @@ def _scheduler_arbitration_facts( "vision_wait_state": wait or None, "scope_wait_todo": scoped_wait, "heartbeat_receipt": payload.get("heartbeat_receipt")}) return facts + + +def monitor_quiet_stall_observation_required(payload: dict[str, Any]) -> bool: + """Keep automatic stall supervision unless typed arbitration proves a future wait.""" + if not exact_blocked_successor_wait_state(payload): + return True + result = effect_runtime_result( + "work_item.blocked_wait_observation.project", + _scheduler_arbitration_facts(payload.get("interaction_contract"), payload=payload), + ) + return result["required"] is not False diff --git a/loopx/control_plane/work_items/interaction_contract.ts b/loopx/control_plane/work_items/interaction_contract.ts index 84b4e4bb96..ef03a88d9b 100644 --- a/loopx/control_plane/work_items/interaction_contract.ts +++ b/loopx/control_plane/work_items/interaction_contract.ts @@ -59,6 +59,11 @@ function pendingAutomaticSuccessor(request: JsonObject, wait: JsonObject | null) * This read model never relaxes an existing Turn binding or settlement fence. * Unknown dependencies retain their bounded observation/replan policy. */ export function projectBlockedSuccessorWaitObservation(request: JsonObject): JsonObject { + if (request.effective_action === EffectiveAction.MONITOR_QUIET_SKIP) { + // The automatic heartbeat must distinguish a proven future clock from a + // quiet Monitor result that still counts towards the blocked-wait limit. + return {required: projectSchedulerArbitration(request).scheduled_recheck_after_seconds === undefined}; + } const wait = exactBlockedSuccessorWait(request); return {required: !!wait && pendingAutomaticSuccessor(request, wait) === null}; } diff --git a/tests/control_plane/test_dated_vision_wait_cli.py b/tests/control_plane/test_dated_vision_wait_cli.py index 54645e7b36..74bd62229d 100644 --- a/tests/control_plane/test_dated_vision_wait_cli.py +++ b/tests/control_plane/test_dated_vision_wait_cli.py @@ -194,6 +194,78 @@ def test_multiple_future_waits_keep_the_original_unbound_turn_quiet( assert not any(run.get("classification") in ("quota_monitor_poll", "quota_slot_spent") for run in runs) +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("scenario", ["before_due", "successor_due", "monitor_due", "unproven"]) +def test_watch_only_monitor_preserves_dated_successor_wait( + tmp_path, isolated_effect, monkeypatch, provider, scenario, +): + past = "2026-01-01T00:00:00Z" + monitor = "todo_watch_only" + monitor_due = past if scenario == "monitor_due" else FUTURE.removeprefix("resume_at:") + resume = f"resume_at:{past}" if scenario == "successor_due" else FUTURE + if scenario == "unproven": + resume = "capacity_available:external_runner" + registry, runtime, state = fixture(tmp_path, resume) + review = "todo_independent_review" + with state.open("a", encoding="utf-8") as output: + output.write( + "\n- [ ] [P1] Keep the independent review at its own scheduled instant.\n" + f" \n" + "\n- [ ] [P1] Observe feedback at its own scheduled instant.\n" + f" \n" + ) + vision = runtime / f"goals/{GOAL}/runs/vision.json" + run = json.loads(vision.read_text()) + run["agent_vision"]["todo_delta"] = [f"retain:{TODO}", f"retain:{review}"] + vision.write_text(json.dumps(run), encoding="utf-8") + index = vision.parent / "index.jsonl" + index.write_text(json.dumps({**run, "json_path": str(vision)}) + "\n", encoding="utf-8") + if provider == "sqlite": + monkeypatch.setenv("NODE_OPTIONS", os.environ.get("NODE_OPTIONS", "") + " --experimental-sqlite") + initialize_canonical_source_fixture(runtime, GOAL, state_path=state, provider=provider) + original = state.read_bytes() + args = ("quota", "should-run", "--codex-app", "--goal-id", GOAL, "--agent-id", AGENT, + "--available-capability", "network", "--available-capability", "external_evidence_poll", + "--turn-instance-id") + rc, first = cli(registry, runtime, *args, "watch-first") + assert rc == 0, first + if scenario == "successor_due": + assert first["effective_action"] == "successor_replan_required" + assert first["selected_todo"]["todo_id"] == TODO + elif scenario == "monitor_due": + assert first["effective_action"] == "normal_run" + assert first["work_lane_contract"]["lane"] == "continuous_monitor" + assert first["selected_todo"]["todo_id"] == monitor + elif scenario == "before_due": + assert first["heartbeat_receipt"]["status"] == "committed" + rc, replay = cli(registry, runtime, *args, "watch-first") + assert rc == 0, replay + assert replay["heartbeat_receipt"]["status"] == "replayed" + rc, later = cli(registry, runtime, *args, "watch-second") + assert rc == 0, later + for guard in (first, replay, later): + assert guard["effective_action"] == "monitor_quiet_skip" + receipt = guard["heartbeat_receipt"] + assert receipt["stall_observation"] == "not_applicable" + assert "settlement_identity" not in receipt + assert guard["goal_frontier_projection"]["replan_required"] is False + assert guard["interaction_contract"]["agent_channel"]["quiet_noop_allowed"] is True + assert guard["vision_wait_state"]["resume_when"] == FUTURE + else: + assert first["heartbeat_receipt"]["stall_observation"] == "appended" + rc, replay = cli(registry, runtime, *args, "watch-first") + assert rc == 0, replay + assert replay["heartbeat_receipt"]["status"] == "replayed" + assert state.read_bytes() == original + runs = [json.loads(line) for line in index.read_text().splitlines()] + assert not any(run.get("classification") == "quota_slot_spent" for run in runs) + assert sum(run.get("classification") == "quota_monitor_poll" for run in runs) == (scenario == "unproven") + + def test_due_vision_wait_restores_explicit_resume(tmp_path, isolated_effect): registry, runtime, state = fixture(tmp_path, "resume_at:2026-01-01T00:00:00Z") initialize_canonical_source_fixture(runtime, GOAL, state_path=state, provider="sqlite")