diff --git a/loopx/capabilities/native_chat/steward_managed_mode.py b/loopx/capabilities/native_chat/steward_managed_mode.py index 2688f099c7..1aad862244 100644 --- a/loopx/capabilities/native_chat/steward_managed_mode.py +++ b/loopx/capabilities/native_chat/steward_managed_mode.py @@ -606,7 +606,9 @@ def _creation_allowance(self, request: dict[str, Any]) -> bool: goal_id=actor["goal_id"]), scheduler_execution_context=scheduler_execution_context_for_turn( host="codex-app-server", execution_mode="isolated-headless")) - agent = (decision.get("interaction_contract") or {}).get("agent_channel") or {} + interaction = decision.get("interaction_contract") + raw_agent = interaction.get("agent_channel") if isinstance(interaction, dict) else None + agent = raw_agent if isinstance(raw_agent, dict) else {} _, admitted = current_action_selection_admission(decision, requested_todo_id=request["todo_id"], agent_must_attempt=agent.get("must_attempt") is True, agent_delivery_allowed=agent.get("delivery_allowed")) diff --git a/loopx/control_plane/quota/live_decision.py b/loopx/control_plane/quota/live_decision.py index 5807a29277..171122a6e9 100644 --- a/loopx/control_plane/quota/live_decision.py +++ b/loopx/control_plane/quota/live_decision.py @@ -1,196 +1,105 @@ from __future__ import annotations -from .effective_action import EffectiveAction -import os -import shlex -from collections.abc import Callable, Mapping, Sequence -from functools import partial +from collections.abc import Mapping, Sequence from pathlib import Path -from typing import Any +from typing import cast -from ...quota import build_quota_should_run -from ...agent_registry import load_goal_from_registry, registered_agent_ids_for_goal -from ...command_invocation import ( - current_loopx_cli_argv, - loopx_cli_argv, - resolve_command_path, +from ...agent_registry import ( + registered_agent_ids_for_goal as registered_agent_ids_for_goal, ) -from ..agent_context import project_agent_context, project_goal_agent_context -from ..agents.identity import build_quota_agent_identity -from ..runtime.time import now_utc_iso -from ..goals.activation import goal_owner_resume_id -from ..goals.goal_frontier.fallback_source import live_fallback_authority_items +from ...quota import build_quota_should_run as build_quota_should_run from ..capability_hooks import ( InteractionProjectionHookRegistration, TurnStartContextProjector, dispatch_interaction_projection_hooks, ) -from .effect_program import ( - ReceiptBoundReplayPhase, - is_turn_reference, - turn_reference, -) -from .blocked_retry import overlay_active_turn_retries -from .settlement import ( - QuotaSettlementReadback, - attach_settlement_progress, - read_heartbeat_settlement, -) -from ..work_items.interaction_contract import build_interaction_contract +from ..scheduler.execution_context import SchedulerExecutionContextResolution +from ..scheduler.scheduler_hint import build_scheduler_hint +from ..scheduler.state import load_app_automation_scheduler_state +from ..todos.contract import normalize_todo_id from ..work_items.action_portfolio import reconcile_retained_action_selection from ..work_items.autonomous_replan_obligation import ( replan_obligation_id_from_packet, ) -from ..todos.contract import normalize_todo_id -from ..scheduler.arbitration import SchedulerArbitration -from ..scheduler.execution_context import ( - SchedulerExecutionContextResolution, - resolve_scheduler_execution_context, +from ..work_items.interaction_contract import ( + build_interaction_contract as build_interaction_contract, +) +from .effect_program import ReceiptBoundReplayPhase as ReceiptBoundReplayPhase +from .effective_action import EffectiveAction as EffectiveAction +from .live_decision_contracts import ( + BoundedResearchFrontierProjector as BoundedResearchFrontierProjector, + HostObservationResolver as HostObservationResolver, + OperatorInboxUrgencyProjector, + ProgressReviewContextReader as ProgressReviewContextReader, + ReconcileRetainedSelectionFn, + mapping_or_empty, + optional_json_map, + require_mapping, +) +from .live_decision_recovery import ( + apply_live_decision_post_hooks_recovery, + resolve_live_decision_preflight, + run_remote_authority_wait_preflight, +) +from .live_decision_replan import ( + _historical_ack_writeback_required as _historical_ack_writeback_required, + _original_replan_settlement_required as _original_replan_settlement_required, + _prepared_execution_needs_readback as _prepared_execution_needs_readback, + _project_receipt_replan_settlement as _project_receipt_replan_settlement, + _receipt_bound_guard_fields as _receipt_bound_guard_fields, + _receipt_bound_recovery_todo as _receipt_bound_recovery_todo, + _restore_replan_obligation_without_foreign_acks as _restore_replan_obligation_without_foreign_acks, + _split_receipt_bound_guard_fields, + prepare_live_decision_status_payload, + project_before_plan_agent_context, +) +from .live_decision_routes import ( + _bind_followup_turn_argument as _bind_followup_turn_argument, + _scheduler_fallback_launcher as _scheduler_fallback_launcher, + bind_action_selection_cli_routes as bind_action_selection_cli_routes, + bind_scheduler_followup_cli_routes as bind_scheduler_followup_cli_routes, +) +from .live_decision_turn_reads import ( + _fresh_operator_inbox_observation_count as _fresh_operator_inbox_observation_count, + _fresh_operator_inbox_read_required as _fresh_operator_inbox_read_required, + _fresh_read_covers_all_pending_material as _fresh_read_covers_all_pending_material, + _fresh_read_covers_pending_count, + _turn_start_required_reads, + merge_turn_start_required_reads, +) +from .settlement import ( + QuotaSettlementReadback as QuotaSettlementReadback, + attach_settlement_progress as attach_settlement_progress, + read_heartbeat_settlement as read_heartbeat_settlement, ) -from ..scheduler.scheduler_hint import build_scheduler_hint -from ..scheduler.state import load_app_automation_scheduler_state from .unsettled_host_turn import ( - apply_unsettled_host_turn_recovery_if_required, - apply_receipt_bound_wait_recovery, - restore_receipt_bound_todo_source, + apply_receipt_bound_wait_recovery as apply_receipt_bound_wait_recovery, + apply_unsettled_host_turn_recovery_if_required as apply_unsettled_host_turn_recovery_if_required, ) -HostObservationResolver = Callable[..., Mapping[str, Any]] -BoundedResearchFrontierProjector = Callable[..., Mapping[str, Any] | None] -ProgressReviewContextReader = Callable[[Mapping[str, Any], Path], dict[str, Any] | None] - - -def _receipt_bound_guard_fields( - readback: QuotaSettlementReadback | None, - obligation_id: str | None, -) -> dict[str, Any]: - """Adapt the recorded guard identity and time for the shared decision owner.""" - guard = getattr(readback, "semantic_replan_guard", None) - receipt = getattr(readback, "heartbeat_receipt", None) or {} - identity = readback.identity.value if readback is not None else None - guard_scoped = bool( - obligation_id and isinstance(guard, Mapping) and guard.get("scope") == "turn_guard" - and guard.get("selected_obligation_id") == obligation_id - ) - # The effective receipt stays the latest content. The admission clock is the - # first bound receipt already selected by the GoalRef-filtered readback. - admission_recorded_at = getattr(readback, "admission_recorded_at", None) - return { - "receipt_bound_replan_guard_scoped": guard_scoped, - "receipt_bound_replan_execution_pending": bool( - guard_scoped and readback and readback.replan_execution_pending - ), - "receipt_bound_guard_recorded_at": admission_recorded_at or receipt.get("recorded_at"), - "receipt_bound_admission_event_id": getattr(readback, "admission_event_id", None), - "receipt_bound_guard_turn_instance_id": identity.turn_instance_id if identity is not None else None, - "receipt_bound_replan_review": guard.get("selected_review_source") if isinstance(guard, Mapping) else None, - "receipt_bound_successor_replan": bool(getattr(readback, "successor_replan_pending", False)), - } - - -def _historical_ack_writeback_required(payload: Mapping[str, Any]) -> bool: - """Recognize the exact historical ACK closeout before durable writeback.""" - packet = payload.get("replan_action_packet") - return (isinstance(packet, Mapping) and packet.get("settlement_only") is True - and packet.get("decision") == "historical_ack_recorded") - - -def _prepared_execution_needs_readback( - payload: Mapping[str, Any], - readback: QuotaSettlementReadback | None, - receipt_todo_id: str | None, -) -> bool: - """Ask the receipt owner to check a prepared generation missing from the current projection.""" - if readback is None or readback.replay_phase is not ReceiptBoundReplayPhase.OPEN: - return False - prepared = readback.prepared_lease - if not prepared or prepared.get("prepared_lease_epoch") is None: - return False - current = payload.get("execution_preparation") or {} - return ( - current.get("todo_id") != receipt_todo_id - or current.get("lease_epoch") != prepared["prepared_lease_epoch"] - ) - - -def _original_replan_settlement_required( - payload: Mapping[str, Any], readback: QuotaSettlementReadback | None, -) -> bool: - if _historical_ack_writeback_required(payload): - return True - if readback is None or readback.replay_phase is not ReceiptBoundReplayPhase.SETTLEMENT_PENDING: - return False - packet = payload.get("replan_action_packet") - identity = readback.identity.value - return ( - isinstance(packet, Mapping) and packet.get("settlement_only") is True - and packet.get("decision") == readback.replay_phase.value - and identity is not None and identity.replan_obligation_id is not None - and identity.replan_obligation_id == packet.get("obligation_id") - and ((readback.progress or {}).get("next_step") == "quota_spend" - or (readback.progress or {}).get("checkpoint_recovery_required") is True) - and payload.get("should_run") is True - and payload.get("effective_action") == EffectiveAction.AUTONOMOUS_REPLAN_REQUIRED.value - ) - - -def _project_receipt_replan_settlement( - payload: dict[str, Any], readback: QuotaSettlementReadback | None, *, - available_capabilities: list[str] | None, - scheduler_execution_context: Mapping[str, Any] | SchedulerExecutionContextResolution | None, - turn_instance_id: str | None, registry_path: Path, runtime_root: Path, - goal_ref: Mapping[str, Any] | None, -) -> None: - """Project closeout from the original readback, never the current selection.""" - if not readback or not _original_replan_settlement_required(payload, readback): - return - attach_settlement_progress(payload, readback, registry_path=registry_path, - runtime_root=runtime_root, goal_ref=goal_ref) - identity = readback.identity.value - if identity is None: - raise ValueError("original replan settlement identity is unavailable") - payload["heartbeat_receipt"] = {**(readback.heartbeat_receipt or {}), "settlement_identity": identity.as_dict()} - if (readback.progress or {}).get("checkpoint_recovery_required") is True: - reason = ( - "Read checkpoint-context for the original blocked writeback. Reassess its evidence and submit " - "only the original checkpoint supplement. Preserve its delivery fields and quota." - ) - packet = {**payload["replan_action_packet"], "decision": "vision_outcome_checkpoint_required", - "required_outcome": "durable_writeback_receipt", - "writeback_contract": {"rule": reason}} - obligation = payload["autonomous_replan_obligation"] - payload["autonomous_replan_obligation"] = {**obligation, "recommended_action": reason, - "guidance_actions": [reason], "settlement_action_packet": packet} - payload.update(replan_action_packet=packet, recommended_action=reason) - payload["interaction_contract"] = build_interaction_contract(payload, - available_capabilities=available_capabilities, scheduler_execution_context=scheduler_execution_context, - turn_instance_id=turn_instance_id, registry_path=str(registry_path), runtime_root=str(runtime_root)) - - def _apply_retained_action_selection_reentry( - payload: dict[str, Any], + payload: dict[str, object], *, retained_todo_id: str | None, available_capabilities: list[str] | None, scheduler_execution_context: ( - Mapping[str, Any] | SchedulerExecutionContextResolution | None + Mapping[str, object] | SchedulerExecutionContextResolution | None ), turn_instance_id: str | None, registry_path: Path, runtime_root: Path, + reconcile_retained_fn: ReconcileRetainedSelectionFn = reconcile_retained_action_selection, ) -> None: """Fence a no-argument reentry with its last explicit Todo choice.""" normalized_retained = normalize_todo_id(retained_todo_id) if normalized_retained is None: return - selected_value = payload.get("selected_todo") - selected = selected_value if isinstance(selected_value, Mapping) else {} - projected_todo_id = normalize_todo_id(selected.get("todo_id")) - verdict = reconcile_retained_action_selection( + selected = mapping_or_empty(payload.get("selected_todo")) + verdict = reconcile_retained_fn( retained_todo_id=normalized_retained, - projected_todo_id=projected_todo_id, + projected_todo_id=normalize_todo_id(selected.get("todo_id")), effective_action=str(payload.get("effective_action") or ""), replan_obligation_id=replan_obligation_id_from_packet( payload.get("replan_action_packet") @@ -219,9 +128,7 @@ def _apply_retained_action_selection_reentry( elif disposition == "require_explicit_selection": projection = verdict.get("projection") if not isinstance(projection, Mapping): - raise RuntimeError( - "TypeScript retained action-selection projection is missing" - ) + raise RuntimeError("TypeScript retained action-selection projection is missing") decision_patch = projection.get("decision_patch") obligation_patch = projection.get("execution_obligation_patch") clear_fields = projection.get("clear_fields") @@ -231,106 +138,40 @@ def _apply_retained_action_selection_reentry( or not isinstance(clear_fields, list) or not all(isinstance(field, str) for field in clear_fields) ): - raise RuntimeError( - "TypeScript retained action-selection projection is malformed" - ) - payload.update(decision_patch) - obligation = ( - dict(payload.get("execution_obligation") or {}) - if isinstance(payload.get("execution_obligation"), Mapping) - else {} - ) - obligation.update(obligation_patch) + raise RuntimeError("TypeScript retained action-selection projection is malformed") + payload.update(cast(Mapping[str, object], decision_patch)) + obligation = dict(mapping_or_empty(payload.get("execution_obligation"))) + obligation.update(cast(Mapping[str, object], obligation_patch)) payload["execution_obligation"] = obligation for field in clear_fields: payload.pop(field, None) else: - raise RuntimeError( - "TypeScript retained action-selection disposition is unsupported" - ) + raise RuntimeError("TypeScript retained action-selection disposition is unsupported") payload["interaction_contract"] = build_interaction_contract( - payload, - available_capabilities=available_capabilities, + payload, available_capabilities=available_capabilities, scheduler_execution_context=scheduler_execution_context, turn_instance_id=turn_instance_id, - registry_path=str(registry_path), - runtime_root=str(runtime_root), + registry_path=str(registry_path), runtime_root=str(runtime_root), ) -def _fresh_read_covers_all_pending_material( - dispatch: Mapping[str, Any] | None, - projector: Callable[..., dict[str, Any]] | None, -) -> Callable[..., dict[str, Any]] | None: - if projector is None: - return None - fresh_count = _fresh_operator_inbox_observation_count(dispatch) - - def project(**kwargs: Any) -> dict[str, Any]: - urgency = dict(projector(**kwargs)) - pending_count = max(0, int(urgency.get("pending_count") or 0)) - if pending_count <= fresh_count: - urgency["material_review_due"] = False - return urgency - - return project - - -def _turn_start_required_reads( - dispatch: Mapping[str, Any] | None, -) -> list[dict[str, Any]]: - """Read only the generic, kernel-validated pre-work projection.""" - - if not isinstance(dispatch, Mapping): - return [] - reads = dispatch.get("required_reads") - if not isinstance(reads, list): - return [] - projected: list[dict[str, Any]] = [] - seen_commands: set[str] = set() - for read in reads: - if not isinstance(read, Mapping): - continue - command = read.get("command") - if not isinstance(command, str) or not command.strip(): - continue - normalized = command.strip() - if normalized in seen_commands: - continue - projected.append( - { - key: read[key] - for key in ( - "kind", "command", "reason", "source", "ordering", - "hook_id", "capability_id", "prompt_budget_bytes", - ) - if key in read - } - ) - seen_commands.add(normalized) - return projected - - def _project_turn_start_required_reads( - payload: dict[str, Any], - dispatch: Mapping[str, Any] | None, + payload: dict[str, object], + dispatch: Mapping[str, object] | None, *, available_capabilities: list[str] | None, scheduler_execution_context: ( - Mapping[str, Any] | SchedulerExecutionContextResolution | None + Mapping[str, object] | SchedulerExecutionContextResolution | None ), turn_instance_id: str | None, registry_path: Path, runtime_root: Path, context_projector: TurnStartContextProjector | None = None, -) -> Mapping[str, Any] | None: +) -> Mapping[str, object] | None: """Order evidence after selection and retain its source dispatch.""" if context_projector is not None: dispatch = context_projector(payload, dispatch) - - # Keep failure observations even when no evidence read was produced. The - # typed envelope projects their cache/dependent-action policy for the host. if dispatch: payload["turn_start_capability_hook_dispatch"] = { key: value for key, value in dispatch.items() if key != "contexts" @@ -338,85 +179,29 @@ def _project_turn_start_required_reads( projected = _turn_start_required_reads(dispatch) if not projected: return dispatch - existing = payload.get("required_reads") - required_reads = ( - [dict(item) for item in existing if isinstance(item, Mapping)] - if isinstance(existing, list) - else [] + payload["required_reads"] = merge_turn_start_required_reads( + payload.get("required_reads"), projected ) - seen_commands = { - str(item.get("command") or "").strip() - for item in required_reads - if str(item.get("command") or "").strip() - } - for required_read in projected: - command = str(required_read.get("command") or "").strip() - if command in seen_commands: - continue - required_reads.append(required_read) - seen_commands.add(command) - payload["required_reads"] = required_reads if _fresh_operator_inbox_read_required(dispatch): - recommendation = ( - dict(payload.get("heartbeat_recommendation") or {}) - if isinstance(payload.get("heartbeat_recommendation"), Mapping) - else {} - ) - recommendation.update( - { - "notify": "NOTIFY", - } - ) + recommendation = dict(mapping_or_empty(payload.get("heartbeat_recommendation"))) + recommendation.update({"notify": "NOTIFY"}) payload["heartbeat_recommendation"] = recommendation payload["interaction_contract"] = build_interaction_contract( - payload, - available_capabilities=available_capabilities, + payload, available_capabilities=available_capabilities, scheduler_execution_context=scheduler_execution_context, turn_instance_id=turn_instance_id, - registry_path=str(registry_path), - runtime_root=str(runtime_root), + registry_path=str(registry_path), runtime_root=str(runtime_root), ) return dispatch -def _fresh_operator_inbox_observation_count( - dispatch: Mapping[str, Any] | None, -) -> int: - if not isinstance(dispatch, Mapping): - return 0 - required_reads = dispatch.get("required_reads") - results = dispatch.get("results") - if not isinstance(required_reads, list) or not isinstance(results, list): - return 0 - operator_inbox_hook_ids = { - str(read.get("hook_id") or "") - for read in required_reads - if isinstance(read, Mapping) - and read.get("kind") == "operator_inbox" - and str(read.get("hook_id") or "") - } - return sum( - max(0, int(result.get("observation_count") or 0)) - for result in results - if isinstance(result, Mapping) - and result.get("agent_read_required") is True - and result.get("hook_id") in operator_inbox_hook_ids - ) - - -def _fresh_operator_inbox_read_required( - dispatch: Mapping[str, Any] | None, -) -> bool: - return _fresh_operator_inbox_observation_count(dispatch) > 0 - - def _apply_pending_capability_intent_precedence( - payload: dict[str, Any], - projection: Mapping[str, Any] | None, + payload: dict[str, object], + projection: Mapping[str, object] | None, *, - available_capabilities: Any = None, + available_capabilities: object = None, scheduler_execution_context: ( - Mapping[str, Any] | SchedulerExecutionContextResolution | None + Mapping[str, object] | SchedulerExecutionContextResolution | None ) = None, turn_instance_id: str | None = None, registry_path: Path | None = None, @@ -480,8 +265,7 @@ def _apply_pending_capability_intent_precedence( "spend_policy": "no external delivery; exact intent receipt is authoritative", } payload["interaction_contract"] = build_interaction_contract( - payload, - available_capabilities=available_capabilities, + payload, available_capabilities=available_capabilities, scheduler_execution_context=scheduler_execution_context, turn_instance_id=turn_instance_id, registry_path=str(registry_path) if registry_path is not None else None, @@ -490,327 +274,37 @@ def _apply_pending_capability_intent_precedence( return True -def _bind_followup_turn_argument( - cli_args: list[Any], - reference: str, - *, - authoritative: bool, -) -> str: - """Point a follow-up's ``--turn-instance-id`` value at ``reference`` in place. - - An authoritative Turn always wins, because it owns the receipt the follow-up - replays. Without one only a shell reference is re-rendered, so a concrete - projected Turn survives untouched. Returns the value the vector now carries, - so a mirrored ``args`` block cannot disagree with the executed command. - """ - - index = ( - cli_args.index("--turn-instance-id") - if "--turn-instance-id" in cli_args - else -1 - ) - if index < 0: - cli_args.extend(["--turn-instance-id", reference]) - return reference - if index + 1 >= len(cli_args): - cli_args.append(reference) - return reference - if authoritative or is_turn_reference(cli_args[index + 1]): - cli_args[index + 1] = reference - return str(cli_args[index + 1]) - - -def _scheduler_fallback_launcher() -> list[str]: - """Keep an executable-bound replay on its selected Core installation.""" - fallback = "loopx-apply-rrule" - route = current_loopx_cli_argv() - if route[1:] != ["--cli-bin", route[0]]: - return [fallback] - configured = route[0] - candidate = Path(configured) - resolved = candidate if candidate.is_absolute() else resolve_command_path(configured) - if resolved is None or not resolved.is_file(): - return [fallback, "--loopx", configured] - executable = resolved.resolve() - companion = executable.with_name(fallback) - if os.name != "nt" and companion.is_file() and os.access(companion, os.X_OK): - fallback = str(companion) - return [fallback, "--loopx", str(executable)] - - -def bind_scheduler_followup_cli_routes( - payload: dict[str, Any], - *, - registry_path: Path, - runtime_root: Path, - turn_instance_id: str | None = None, - source: str = "quota_cli_invocation", -) -> None: - """Bind scheduler follow-ups to the registry/runtime/Turn that built the hint.""" - - scheduler_hint = payload.get("scheduler_hint") - if not isinstance(scheduler_hint, dict): - return - app_packet = scheduler_hint.get("app_automation") - if not isinstance(app_packet, dict): - return - launcher_args = loopx_cli_argv()[1:] - safe_turn_instance_id = str(turn_instance_id or "").strip() - for hint_name in ("ack_hint", "failure_hint", "fallback_hint"): - followup_hint = app_packet.get(hint_name) - if not isinstance(followup_hint, dict): - continue - cli_args = followup_hint.get("cli_args") - if not isinstance(cli_args, list) or not cli_args: - continue - if hint_name == "fallback_hint": - if cli_args[0] != "loopx-apply-rrule" or "--registry" in cli_args: - continue - bound_cli_args = [ - *_scheduler_fallback_launcher(), - "--registry", - str(registry_path.expanduser().resolve()), - "--runtime-root", - str(runtime_root.expanduser().resolve()), - *cli_args[1:], - ] - # The fallback replays this decision through ``loopx quota - # should-run``, so it needs the Turn that owns the receipt. - # Projections leave a shell reference here; a native Windows - # host cannot expand the POSIX form and would replay an empty - # Turn instead of the decision it came from. - bound_turn = _bind_followup_turn_argument( - bound_cli_args, - turn_reference(safe_turn_instance_id), - authoritative=bool(safe_turn_instance_id), - ) - followup_hint["cli_args"] = bound_cli_args - args_value = followup_hint.get("args") - if isinstance(args_value, dict) and "turn_instance_id" in args_value: - args_value["turn_instance_id"] = bound_turn - followup_hint["route_binding"] = { - "schema_version": "codex_app_scheduler_fallback_route_v0", - "source": source, - "registry_bound": True, - "runtime_root_bound": True, - "turn_instance_bound": bool(safe_turn_instance_id), - } - continue - bound_cli_args = list(cli_args) - # Standalone Core emits an interpreter route. Its launcher - # arguments must precede the CLI's global options. - option_index = len(launcher_args) if cli_args[:len(launcher_args)] == launcher_args else 0 - if bound_cli_args[option_index:option_index + 1] != ["--registry"]: - bound_cli_args[option_index:option_index] = [ - "--registry", str(registry_path.expanduser().resolve()), - "--runtime-root", str(runtime_root.expanduser().resolve()), - ] - if safe_turn_instance_id and "--turn-instance-id" not in bound_cli_args: - execute_index = ( - bound_cli_args.index("--execute") - if "--execute" in bound_cli_args - else len(bound_cli_args) - ) - bound_cli_args[execute_index:execute_index] = [ - "--turn-instance-id", - safe_turn_instance_id, - ] - args_value = followup_hint.get("args") - if isinstance(args_value, dict): - args_value["turn_instance_id"] = safe_turn_instance_id - followup_hint["cli_args"] = bound_cli_args - followup_hint["route_binding"] = { - "schema_version": ( - "scheduler_ack_cli_route_v0" - if hint_name == "ack_hint" - else "scheduler_failure_cli_route_v0" - ), - "source": source, - "registry_bound": True, - "runtime_root_bound": True, - "turn_instance_bound": bool(safe_turn_instance_id), - } - - -def bind_action_selection_cli_routes( - payload: dict[str, Any], - *, - registry_path: Path, - runtime_root: Path, -) -> None: - """Bind two-phase action-selection commands to this live CLI source.""" - - interaction_value = payload.get("interaction_contract") - interaction: Mapping[str, Any] = ( - interaction_value if isinstance(interaction_value, Mapping) else {} - ) - cli_channel_value = interaction.get("cli_channel") - if not isinstance(cli_channel_value, dict): - return - cli_channel: dict[str, Any] = cli_channel_value - if cli_channel.get("selection_required") is not True: - return - selection_command = cli_channel.get("selection_command") - if not isinstance(selection_command, dict): - return - route_prefix = selection_command.get("route_prefix") - if not isinstance(route_prefix, str): - return - try: - tokens = shlex.split(route_prefix) - except ValueError: - return - if len(tokens) >= 3 and tokens[0] == "loopx": - try: - format_index = tokens.index("--format") - except ValueError: - return - if tokens[format_index : format_index + 2] != ["--format", "json"]: - return - if "--registry" not in tokens: - tokens[1:1] = ["--registry", str(registry_path.expanduser().resolve())] - if "--runtime-root" not in tokens: - format_index = tokens.index("--format") - tokens[format_index:format_index] = [ - "--runtime-root", - str(runtime_root.expanduser().resolve()), - ] - selection_command["route_prefix"] = shlex.join(tokens) - - -def _receipt_bound_recovery_todo( - payload: Mapping[str, Any], receipt_bound_todo_id: str | None, *, - agent_id: str | None, turn_instance_id: str | None, - replay_phase: ReceiptBoundReplayPhase | None, -) -> str | None: - """Keep finished/replan-only Turns out of generic Todo wait recovery.""" - if (not receipt_bound_todo_id or not agent_id or not turn_instance_id - or replay_phase is ReceiptBoundReplayPhase.SETTLED - or _historical_ack_writeback_required(payload) - or payload.get("effective_action") == EffectiveAction.UNSETTLED_HOST_TURN_RECOVERY.value): - return None - if replay_phase is ReceiptBoundReplayPhase.SETTLEMENT_PENDING: - return receipt_bound_todo_id - selected_value = payload.get("selected_todo") - selected = selected_value if isinstance(selected_value, Mapping) else {} - if (selected.get("todo_id") != receipt_bound_todo_id - or payload.get("normal_delivery_allowed") is not True): - return receipt_bound_todo_id - wait_kind = str(selected.get("resume_when") or "").split(":", 1)[0] - return receipt_bound_todo_id if wait_kind not in ("", "monitor_changed", "todo_done") else None - - -def _project_unbound_work_fields(payload: dict[str, Any]) -> None: +def _project_unbound_work_fields(payload: dict[str, object]) -> None: interaction = payload.get("interaction_contract") cli = interaction.get("cli_channel") if isinstance(interaction, dict) else None if not isinstance(cli, dict) or cli.get("turn_binding_required") is not True: return - - # Reconcile the legacy work fields after hooks and recovery have chosen - # the final interaction. The selected work stays visible but unadmitted. reason = cli["spend_policy"] + next_actions = cli["next_cli_actions"] + if not isinstance(next_actions, Sequence) or isinstance( + next_actions, (str, bytes, bytearray) + ): + raise TypeError( + "Expected sequence for cli_channel.next_cli_actions, " + f"got {type(next_actions).__name__}" + ) + recommended = next_actions[0] payload["normal_delivery_allowed"] = False - payload["recommended_action"] = cli["next_cli_actions"][0] + payload["recommended_action"] = recommended payload.pop("agent_lane_next_action", None) obligation = payload.get("execution_obligation") if isinstance(obligation, dict): obligation.update(must_attempt_work=False, delivery_allowed=False, reason=reason) lane = payload.get("work_lane_contract") if isinstance(lane, dict): - lane.update(must_attempt_work=False, action=payload["recommended_action"]) + lane.update(must_attempt_work=False, action=recommended) recommendation = payload.get("heartbeat_recommendation") if isinstance(recommendation, dict): recommendation["spend_policy"] = reason -def _restore_replan_obligation_without_foreign_acks( - status_payload: dict[str, Any], *, goal_id: str, agent_id: str, - goal_ref: Mapping[str, Any] | None, - registry_goal: Mapping[str, Any] | None, - registry_path: Path, - runtime_root: Path, - progress_review_context_reader: ProgressReviewContextReader | None, -) -> None: - """Recompute the open review after removing another instance's ACK rows.""" - from ..status.autonomous_replan_projection import autonomous_replan_obligation_from_runs - from ..work_items.autonomous_replan_ack import drop_unowned_recorded_replan_acks - from ..work_items.project_asset import attach_active_state_project_asset_fields - from ..work_items.replan_history_codec import effective_turn_cadence_context - - run_history = status_payload.get("run_history") - goals = run_history.get("goals") if isinstance(run_history, dict) else None - goal = next( - ( - item for item in goals - if isinstance(item, dict) and str(item.get("id") or "") == goal_id - ), - None, - ) if isinstance(goals, list) else None - if not isinstance(goal, dict) or not isinstance(goal.get("latest_runs"), list): - return - filtered, changed = drop_unowned_recorded_replan_acks( - goal["latest_runs"], goal_id=goal_id, goal_ref=goal_ref, - ) - if not changed: - return - goal["latest_runs"] = filtered - kept_ack_times = { - run.get("generated_at") - for run in filtered - if isinstance(run.get("autonomous_replan_ack"), dict) - } - semantic = goal.get("semantic_history") - agents = semantic.get("agents") if isinstance(semantic, dict) else None - if isinstance(agents, list): - for context in agents: - if not isinstance(context, dict): - continue - ack_run = context.get("latest_autonomous_replan_ack_run") - if ( - isinstance(ack_run, dict) - and ack_run.get("generated_at") not in kept_ack_times - ): - context.pop("latest_autonomous_replan_ack_run", None) - queue = status_payload.get("attention_queue") - items = queue.get("items") if isinstance(queue, dict) else None - item = next( - ( - entry for entry in items - if isinstance(entry, dict) and entry.get("goal_id") == goal_id - ), - None, - ) if isinstance(items, list) else None - if not isinstance(item, dict): - return - for target in (item, item.get("project_asset")): - if not isinstance(target, dict): - continue - target.pop("autonomous_replan_obligation", None) - target.pop("autonomous_replan_obligations_by_agent", None) - target.pop("autonomous_replan_ack", None) - source_goal = dict(registry_goal) if isinstance(registry_goal, Mapping) else {"id": goal_id} - source_goal.setdefault("id", goal_id) - review = ( - progress_review_context_reader(source_goal, runtime_root) - if progress_review_context_reader is not None else None - ) - cadence_goal_ref = dict(goal_ref) if isinstance(goal_ref, Mapping) else None - attach_active_state_project_asset_fields( - item, latest_runs=filtered, - autonomous_replan_obligation_from_runs=autonomous_replan_obligation_from_runs, - external_progress_review=review, - effective_turn_cadence=effective_turn_cadence_context( - source_goal, runtime_root, registry_path=registry_path, - goal_ref=cadence_goal_ref if all( - isinstance(value, str) for value in (cadence_goal_ref or {}).values() - ) else None, - ), - agent_lane_id=agent_id, - ) - - def build_live_quota_should_run_decision( - status_payload: dict[str, Any], + status_payload: dict[str, object], *, goal_id: str, agent_id: str | None, @@ -822,10 +316,10 @@ def build_live_quota_should_run_decision( runtime_root: Path, host_observation_resolver: HostObservationResolver | None = None, route_source: str = "quota_cli_invocation", - scheduler_execution_context: Mapping[str, Any] + scheduler_execution_context: Mapping[str, object] | SchedulerExecutionContextResolution | None = None, - operator_inbox_urgency_projector: Callable[..., dict[str, Any]] | None = None, + operator_inbox_urgency_projector: OperatorInboxUrgencyProjector | None = None, bounded_research_frontier_projector: BoundedResearchFrontierProjector | None = None, progress_review_context_reader: ProgressReviewContextReader | None = None, receipt_bound_todo_id: str | None = None, @@ -835,61 +329,34 @@ def build_live_quota_should_run_decision( turn_instance_id: str | None = None, interaction_projection_hooks: Sequence[InteractionProjectionHookRegistration] | None = None, - turn_start_hook_dispatch: Mapping[str, Any] | None = None, + turn_start_hook_dispatch: Mapping[str, object] | None = None, turn_context_projector: TurnStartContextProjector | None = None, context_navigation: bool = False, workspace_path: Path | None = None, goal_ref: Mapping[str, object] | None = None, inline_goal_source: bool = False, tolerate_malformed_settlement_history: bool = False, -) -> dict[str, Any]: +) -> dict[str, object]: """Build one live CLI decision while keeping host observation injectable.""" - resolved_context = resolve_scheduler_execution_context(scheduler_execution_context) - from ..coordination.remote_authority_gateway import remote_authority_config - registered_goal = load_goal_from_registry(registry_path, goal_id) - app_automation_applicable = ( - resolved_context.ok - and resolved_context.context is not None - and resolved_context.context.app_automation_applicable - ) - codex_app_host = bool( - app_automation_applicable - and resolved_context.context is not None - and resolved_context.context.host_surface.value == "codex_app" + ( + resolved_context, + registered_goal, + codex_app_host, + observed_rrule, + observed_automation_id, + decision_status_payload, + remote_config, + ) = resolve_live_decision_preflight( + status_payload, goal_id=goal_id, agent_id=agent_id, + codex_app_current_rrule=codex_app_current_rrule, + registry_path=registry_path, runtime_root=runtime_root, + scheduler_execution_context=scheduler_execution_context, + host_observation_resolver=host_observation_resolver, + bounded_research_frontier_projector=bounded_research_frontier_projector, ) - observed_rrule = str(codex_app_current_rrule or "").strip() - observed_automation_id = "" - if ( - codex_app_host - and not observed_rrule - and host_observation_resolver is not None - ): - observation = host_observation_resolver(goal_id=goal_id, agent_id=agent_id) - if observation.get("available") is True: - observed_rrule = str(observation.get("rrule") or "") - observed_automation_id = str(observation.get("automation_id") or "").strip() - decision_status_payload = { - **status_payload, - "runtime_root": str(runtime_root), - } - if bounded_research_frontier_projector is not None: - frontier = bounded_research_frontier_projector( - runtime_root=runtime_root, - goal_id=goal_id, - agent_id=agent_id, - status_payload=status_payload, - ) - if isinstance(frontier, Mapping): - decision_status_payload = { - **decision_status_payload, - "bounded_research_frontier": dict(frontier), - } - remote_config = remote_authority_config(registered_goal) if remote_config is not None: - # Use the same hard-pause precedence and keep fresh local selection - # outside the remote route, even when a TLS read succeeds. - payload = _build_remote_authority_wait_decision( + remote_payload = _build_remote_authority_wait_decision( decision_status_payload, configured_goal=registered_goal or {}, goal_id=goal_id, agent_id=agent_id, remote_config=remote_config, registry_path=registry_path, runtime_root=runtime_root, goal_ref=goal_ref, @@ -900,27 +367,17 @@ def build_live_quota_should_run_decision( receipt_bound_replan_obligation_id=receipt_bound_replan_obligation_id, ) bind_scheduler_followup_cli_routes( - payload, registry_path=registry_path, runtime_root=runtime_root, + remote_payload, registry_path=registry_path, runtime_root=runtime_root, turn_instance_id=turn_instance_id, source=route_source, ) - return payload + return remote_payload settlement_readback = read_heartbeat_settlement( - runtime_root, - goal_id=goal_id, - agent_id=agent_id, - todo_id=receipt_bound_todo_id, + runtime_root, goal_id=goal_id, agent_id=agent_id, todo_id=receipt_bound_todo_id, turn_instance_id=turn_instance_id, - # A Todo-bound receipt may also carry the semantic replan generation - # admitted by that Turn. Its settlement identity remains the Todo; - # passing both bindings makes the readback owner treat the receipt as a - # conflicting replan-only identity and drops its semantic guard. replan_obligation_id=( - None - if receipt_bound_todo_id - else receipt_bound_replan_obligation_id + None if receipt_bound_todo_id else receipt_bound_replan_obligation_id ), - registry_path=registry_path, - goal_ref=goal_ref, + registry_path=registry_path, goal_ref=goal_ref, tolerate_malformed_history=tolerate_malformed_settlement_history, ) receipt_bound_monitor_phase = ( @@ -929,84 +386,33 @@ def build_live_quota_should_run_decision( receipt_bound_replay_phase = ( settlement_readback.replay_phase if settlement_readback else None ) - receipt_bound_guard_fields = _receipt_bound_guard_fields( + guard_kwargs, admission_event_id = _split_receipt_bound_guard_fields( settlement_readback, receipt_bound_replan_obligation_id, ) - admission_event_id = receipt_bound_guard_fields.pop("receipt_bound_admission_event_id") - if ( - receipt_bound_guard_fields["receipt_bound_replan_guard_scoped"] - and receipt_bound_replay_phase is ReceiptBoundReplayPhase.OPEN - and receipt_bound_replan_obligation_id - and agent_id - ): - from ..work_items.autonomous_replan_ack import load_receipt_replan_ack_candidates - decision_status_payload["receipt_bound_replan_ack_candidates"] = load_receipt_replan_ack_candidates( - runtime_root, goal_id=goal_id, agent_id=agent_id, - obligation_id=receipt_bound_replan_obligation_id, - guard_recorded_at=receipt_bound_guard_fields["receipt_bound_guard_recorded_at"], - guard_turn_instance_id=receipt_bound_guard_fields["receipt_bound_guard_turn_instance_id"], - goal_ref=goal_ref, admission_event_id=admission_event_id) - _restore_replan_obligation_without_foreign_acks( + fresh_inbox_count = _fresh_operator_inbox_observation_count(turn_start_hook_dispatch) + decision_status_payload, authoritative_fallback_todo_items = ( + prepare_live_decision_status_payload( decision_status_payload, goal_id=goal_id, agent_id=agent_id, goal_ref=goal_ref, - registry_goal=registered_goal, registry_path=registry_path, runtime_root=runtime_root, + registered_goal=registered_goal, registry_path=registry_path, + runtime_root=runtime_root, guard_kwargs=guard_kwargs, + admission_event_id=admission_event_id, + receipt_bound_replay_phase=receipt_bound_replay_phase, + receipt_bound_todo_id=receipt_bound_todo_id, + requested_action_todo_id=requested_action_todo_id, + retained_action_selection_todo_id=retained_action_selection_todo_id, + receipt_bound_replan_obligation_id=receipt_bound_replan_obligation_id, progress_review_context_reader=progress_review_context_reader, ) - fresh_operator_inbox_read = _fresh_operator_inbox_read_required( - turn_start_hook_dispatch - ) - if ( - requested_action_todo_id or retained_action_selection_todo_id - ) and not receipt_bound_todo_id: - # Candidate discovery, admission, and retained-selection reentry use the - # same provider-first reader. A selection can be deferred by a hard - # frontier that is visible only in the complete Todo snapshot; reentry - # must not fall back to the earlier compact status projection and lose - # the obligation that caused the deferral. - # Keep the complete snapshot internal; presentation is bounded later. - from ..todos.list_readback import list_goal_todos - - source = list_goal_todos( - registry_path=registry_path, runtime_root_arg=str(runtime_root), goal_id=goal_id, - ) - todo_fields = {key: source[key] for key in ("user_todos", "agent_todos")} - queue = decision_status_payload.get("attention_queue") or {} - decision_status_payload["attention_queue"] = { - **queue, - "items": [ - {**item, **todo_fields} if item.get("goal_id") == goal_id else item - for item in queue.get("items") or [] - ], - } - decision_status_payload = restore_receipt_bound_todo_source( - decision_status_payload, registry_path=registry_path, runtime_root=runtime_root, - goal_id=goal_id, todo_id=receipt_bound_todo_id, - replay_phase=receipt_bound_replay_phase, - replan_guard_scoped=receipt_bound_guard_fields["receipt_bound_replan_guard_scoped"], - ) - decision_status_payload = overlay_active_turn_retries( - decision_status_payload, - goal_id=goal_id, - agent_id=agent_id, - observed_at=now_utc_iso(), - ) - authoritative_fallback_todo_items = live_fallback_authority_items( - decision_status_payload, - registry_path=registry_path, - runtime_root=runtime_root, - goal_id=goal_id, - agent_id=agent_id, ) - app_scheduler_state: dict[str, Any] | None = None + app_scheduler_state: dict[str, object] | None = None - def observe_app_scheduler_state(readback: Mapping[str, Any]) -> None: - # A later recovery wait uses the same state this decision actually read. + def observe_app_scheduler_state(readback: Mapping[str, object]) -> None: nonlocal app_scheduler_state - app_scheduler_state = readback.get("state") + state = readback.get("state") + app_scheduler_state = dict(state) if isinstance(state, Mapping) else None - payload: dict[str, Any] = build_quota_should_run( - decision_status_payload, - goal_id=goal_id, - agent_id=agent_id, + payload: dict[str, object] = build_quota_should_run( + decision_status_payload, goal_id=goal_id, agent_id=agent_id, available_capabilities=available_capabilities, include_scheduler_detail=include_scheduler_detail, include_agent_todo_detail=include_agent_todo_detail, @@ -1014,11 +420,10 @@ def observe_app_scheduler_state(readback: Mapping[str, Any]) -> None: codex_app_automation_id=observed_automation_id or None, scheduler_execution_context=resolved_context, operator_inbox_urgency_projector=( - _fresh_read_covers_all_pending_material( - turn_start_hook_dispatch, - operator_inbox_urgency_projector, + _fresh_read_covers_pending_count( + fresh_inbox_count, operator_inbox_urgency_projector, ) - if fresh_operator_inbox_read + if fresh_inbox_count > 0 else operator_inbox_urgency_projector ), receipt_bound_todo_id=receipt_bound_todo_id, @@ -1027,272 +432,196 @@ def observe_app_scheduler_state(readback: Mapping[str, Any]) -> None: receipt_bound_monitor_phase=receipt_bound_monitor_phase, receipt_bound_replay_phase=receipt_bound_replay_phase, receipt_bound_replan_obligation_id=receipt_bound_replan_obligation_id, - **receipt_bound_guard_fields, - turn_instance_id=turn_instance_id, - runtime_root=runtime_root, + **guard_kwargs, + turn_instance_id=turn_instance_id, runtime_root=runtime_root, app_scheduler_state_observer=observe_app_scheduler_state, - workspace_path=workspace_path, - goal_ref=goal_ref, + workspace_path=workspace_path, goal_ref=goal_ref, authoritative_fallback_todo_items=authoritative_fallback_todo_items, ) - _project_receipt_replan_settlement(payload, settlement_readback, - available_capabilities=available_capabilities, scheduler_execution_context=resolved_context, - turn_instance_id=turn_instance_id, registry_path=registry_path, runtime_root=runtime_root, - goal_ref=goal_ref) + _project_receipt_replan_settlement( + payload, settlement_readback, + available_capabilities=available_capabilities, + scheduler_execution_context=resolved_context, + turn_instance_id=turn_instance_id, registry_path=registry_path, + runtime_root=runtime_root, goal_ref=goal_ref, + attach_settlement_fn=attach_settlement_progress, + build_interaction_contract_fn=build_interaction_contract, + ) _apply_retained_action_selection_reentry( - payload, - retained_todo_id=retained_action_selection_todo_id, + payload, retained_todo_id=retained_action_selection_todo_id, available_capabilities=available_capabilities, scheduler_execution_context=resolved_context, - turn_instance_id=turn_instance_id, - registry_path=registry_path, + turn_instance_id=turn_instance_id, registry_path=registry_path, runtime_root=runtime_root, ) - remembered_runtime = (payload.get("agent_identity") or {}).get( - "runtime_available_capabilities" + raw_identity = payload.get("agent_identity") + agent_identity = ( + require_mapping(raw_identity, label="agent_identity") if raw_identity else {} ) + remembered_runtime = agent_identity.get("runtime_available_capabilities") if isinstance(remembered_runtime, list): - available_capabilities = remembered_runtime + available_capabilities = cast(list[str], remembered_runtime) if route_source.startswith("loopx_turn_"): payload["runtime_root"] = str(runtime_root) if codex_app_host and agent_id: from ..heartbeat.prompt_upgrade_hook import extend_prompt_upgrade_reads turn_start_hook_dispatch = extend_prompt_upgrade_reads( - turn_start_hook_dispatch, registry=registry_path, runtime_root=runtime_root, - goal_id=goal_id, agent_id=agent_id, + turn_start_hook_dispatch, registry=registry_path, + runtime_root=runtime_root, goal_id=goal_id, agent_id=agent_id, ) turn_start_hook_dispatch = _project_turn_start_required_reads( - payload, - turn_start_hook_dispatch, + payload, turn_start_hook_dispatch, available_capabilities=available_capabilities, scheduler_execution_context=resolved_context, - turn_instance_id=turn_instance_id, - registry_path=registry_path, - runtime_root=runtime_root, - context_projector=turn_context_projector, + turn_instance_id=turn_instance_id, registry_path=registry_path, + runtime_root=runtime_root, context_projector=turn_context_projector, ) hook_dispatch = dispatch_interaction_projection_hooks(interaction_projection_hooks) projections = hook_dispatch["projections"] if isinstance(projections, Mapping): _apply_pending_capability_intent_precedence( - payload, - projections.get("pending_capability_intent"), + payload, projections.get("pending_capability_intent"), available_capabilities=available_capabilities, scheduler_execution_context=resolved_context, - turn_instance_id=turn_instance_id, - registry_path=registry_path, + turn_instance_id=turn_instance_id, registry_path=registry_path, runtime_root=runtime_root, ) interaction = payload.get("interaction_contract") if isinstance(interaction, dict): interaction.update(projections) - # A settled receipt owns this host Turn until it ends. Looking for an older - # unsettled Turn here can overwrite the settled-skip route with a recovery - # obligation and then select a successor against the immutable receipt - # identity. Leave prior-Turn recovery to the next fresh Turn instead. - original_replan_settlement = _original_replan_settlement_required(payload, settlement_readback) - if receipt_bound_replay_phase is not ReceiptBoundReplayPhase.SETTLED and not original_replan_settlement: - apply_unsettled_host_turn_recovery_if_required( - payload, - inspect_current_execution_loss=_prepared_execution_needs_readback( - payload, settlement_readback, receipt_bound_todo_id - ), - registry_path=registry_path, - runtime_root=runtime_root, - goal_id=goal_id, - agent_id=agent_id, - current_turn_instance_id=turn_instance_id, - available_capabilities=available_capabilities, - project_scheduler_hint=partial( - build_scheduler_hint, include_detail=include_scheduler_detail, - scheduler_execution_context=resolved_context, - available_capabilities=available_capabilities, - codex_app_scheduler_state=app_scheduler_state, - codex_app_current_rrule=observed_rrule, - codex_app_automation_id=observed_automation_id or None, - ), - scheduler_execution_context=resolved_context, - goal_ref=goal_ref, - owner_action_id=goal_owner_resume_id(registered_goal), + apply_live_decision_post_hooks_recovery( + payload, settlement_readback=settlement_readback, + receipt_bound_monitor_phase=receipt_bound_monitor_phase, + receipt_bound_replay_phase=receipt_bound_replay_phase, + receipt_bound_todo_id=receipt_bound_todo_id, + registry_path=registry_path, runtime_root=runtime_root, + goal_id=goal_id, agent_id=agent_id, turn_instance_id=turn_instance_id, + available_capabilities=available_capabilities, + include_scheduler_detail=include_scheduler_detail, + resolved_context=resolved_context, + app_scheduler_state=app_scheduler_state, + observed_rrule=observed_rrule, + observed_automation_id=observed_automation_id, + goal_ref=goal_ref, registered_goal=registered_goal, + hook_dispatch=hook_dispatch, + apply_recovery_fn=apply_unsettled_host_turn_recovery_if_required, + attach_settlement_fn=attach_settlement_progress, + apply_wait_recovery_fn=apply_receipt_bound_wait_recovery, + build_scheduler_hint_fn=build_scheduler_hint, + ) + _project_unbound_work_fields(payload) + if goal_id and agent_id: + raw_boundary = payload.get("goal_boundary") + boundary = require_mapping(raw_boundary, label="goal_boundary") if raw_boundary else {} + raw_orch = boundary.get("orchestration") + orchestration = ( + require_mapping(raw_orch, label="goal_boundary.orchestration") + if raw_orch + else {} ) - if recovery_todo_id := _receipt_bound_recovery_todo( - payload, receipt_bound_todo_id, agent_id=agent_id, turn_instance_id=turn_instance_id, - replay_phase=receipt_bound_replay_phase, - ): - # A verified writeback seals this Turn even while its Todo stays open - # and selected. Let the existing typed owner project only the remaining - # settlement; another delivery cannot replace the immutable writeback. - if settlement_readback is not None: - attach_settlement_progress( - payload, settlement_readback, - registry_path=registry_path, runtime_root=runtime_root, goal_ref=goal_ref, - ) - apply_receipt_bound_wait_recovery( - payload, registry_path=registry_path, runtime_root=runtime_root, - goal_id=goal_id, agent_id=agent_id, todo_id=recovery_todo_id, - turn_instance_id=turn_instance_id, available_capabilities=available_capabilities, - scheduler_execution_context=resolved_context, - monitor_phase=(receipt_bound_monitor_phase.value if receipt_bound_monitor_phase else None), - settlement_progress=settlement_readback.progress if settlement_readback else None, - replay_phase=receipt_bound_replay_phase, - heartbeat_receipt=settlement_readback.heartbeat_receipt if settlement_readback else None, + project_before_plan_agent_context( + payload, goal_id=goal_id, agent_id=agent_id, + registered_goal=registered_goal, orchestration=orchestration, + registry_path=registry_path, runtime_root=runtime_root, ) - if hook_dispatch["failures"]: - payload["capability_hook_dispatch"] = { - key: value for key, value in hook_dispatch.items() if key != "projections" - } - _project_unbound_work_fields(payload) - interaction = payload.get("interaction_contract") - if isinstance(interaction, dict) and goal_id and agent_id: - scope = { - "goal_id": goal_id, - "agent_id": agent_id, - "todo_id": (payload.get("selected_todo") or {}).get("todo_id"), - } - goal = registered_goal - if goal is not None: - context = project_goal_agent_context( - phase="before_plan", - scope=scope, - goal=goal, - registry_path=registry_path, - runtime_root=runtime_root, - observations={"capability_improvement": { - "trigger": "replan" if payload.get("replan_action_packet") else "before_plan", - }}, - ) - else: - context = project_agent_context( - phase="before_plan", - scope=scope, - orchestration=(payload.get("goal_boundary") or {}).get("orchestration") or {}, - ) - if context is not None: - interaction["agent_context"] = context bind_scheduler_followup_cli_routes( - payload, - registry_path=registry_path, - runtime_root=runtime_root, - turn_instance_id=turn_instance_id, - source=route_source, + payload, registry_path=registry_path, runtime_root=runtime_root, + turn_instance_id=turn_instance_id, source=route_source, ) bind_action_selection_cli_routes( - payload, - registry_path=registry_path, - runtime_root=runtime_root, + payload, registry_path=registry_path, runtime_root=runtime_root, ) from ..work_items.context_readback import attach_work_context - attach_work_context(payload, registry_path=registry_path, runtime_root=runtime_root, - hook_dispatch=turn_start_hook_dispatch, context_navigation=context_navigation, - inline_goal_source=inline_goal_source) + attach_work_context( + payload, registry_path=registry_path, runtime_root=runtime_root, + hook_dispatch=optional_json_map(turn_start_hook_dispatch), + context_navigation=context_navigation, inline_goal_source=inline_goal_source, + ) return payload def _build_remote_authority_wait_decision( - status_payload: dict[str, Any], *, configured_goal: dict[str, Any], - goal_id: str, agent_id: str | None, remote_config: Mapping[str, Any], - registry_path: Path, runtime_root: Path, goal_ref: Mapping[str, Any] | None, + status_payload: dict[str, object], + *, + configured_goal: dict[str, object], + goal_id: str, + agent_id: str | None, + remote_config: Mapping[str, object], + registry_path: Path, + runtime_root: Path, + goal_ref: Mapping[str, object] | None, scheduler_context: SchedulerExecutionContextResolution, - available_capabilities: list[str] | None, turn_instance_id: str | None, - include_scheduler_detail: bool, codex_app_rrule: str, codex_app_automation_id: str | None, - receipt_bound_todo_id: str | None, receipt_bound_replan_obligation_id: str | None, -) -> dict[str, Any]: - from ...quota import _build_quota_plan_for_goal + available_capabilities: list[str] | None, + turn_instance_id: str | None, + include_scheduler_detail: bool, + codex_app_rrule: str, + codex_app_automation_id: str | None, + receipt_bound_todo_id: str | None, + receipt_bound_replan_obligation_id: str | None, +) -> dict[str, object]: + early_payload, payload, scope_matches, settled = run_remote_authority_wait_preflight( + status_payload, configured_goal=configured_goal, goal_id=goal_id, + agent_id=agent_id, registry_path=registry_path, runtime_root=runtime_root, + goal_ref=goal_ref, scheduler_context=scheduler_context, + available_capabilities=available_capabilities, + turn_instance_id=turn_instance_id, + include_scheduler_detail=include_scheduler_detail, + codex_app_rrule=codex_app_rrule, + codex_app_automation_id=codex_app_automation_id, + receipt_bound_todo_id=receipt_bound_todo_id, + receipt_bound_replan_obligation_id=receipt_bound_replan_obligation_id, + read_settlement_fn=read_heartbeat_settlement, + attach_settlement_fn=attach_settlement_progress, + apply_wait_recovery_fn=apply_receipt_bound_wait_recovery, + apply_recovery_fn=apply_unsettled_host_turn_recovery_if_required, + build_scheduler_hint_fn=build_scheduler_hint, + load_scheduler_state_fn=load_app_automation_scheduler_state, + ) + if early_payload is not None: + return early_payload + from ..coordination.remote_authority_gateway import ( check_remote_authority_gateway_status, get_remote_authority_projection, ) - from .decision_summary import quota_plan_items from .settlement_precedence import settled_replay_fields - from .should_run import build_quota_paused_should_run_payload - from .states import quota_item_is_paused - - plan, health_ok = _build_quota_plan_for_goal(status_payload, goal_id=goal_id) - item = next((row for row in quota_plan_items(plan) if row.get("goal_id") == goal_id), None) - if item is not None and quota_item_is_paused(item): - return build_quota_paused_should_run_payload( - status_payload, safe_goal_id=goal_id, requested_agent_id=agent_id, item=item, plan=plan, - goal_health_ok=health_ok, include_scheduler_detail=include_scheduler_detail, - codex_app_current_rrule=codex_app_rrule, codex_app_automation_id=codex_app_automation_id, - resolved_scheduler_context=scheduler_context, runtime_root=runtime_root, goal_ref=goal_ref, - ) - scope_matches = ( - (goal_ref is None or goal_ref.get("goal_instance_id") == configured_goal.get("goal_instance_id")) - and agent_id in registered_agent_ids_for_goal(configured_goal) - ) - readback = read_heartbeat_settlement( - runtime_root, goal_id=goal_id, agent_id=agent_id, todo_id=receipt_bound_todo_id, - turn_instance_id=turn_instance_id, registry_path=registry_path, goal_ref=goal_ref, - replan_obligation_id=None if receipt_bound_todo_id else receipt_bound_replan_obligation_id, - ) if scope_matches and turn_instance_id else None - settled = readback is not None and readback.replay_phase is ReceiptBoundReplayPhase.SETTLED - payload: dict[str, Any] = { - "ok": True, "mode": "should-run", "goal_id": goal_id, "agent_id": agent_id, - "should_run": False, "normal_delivery_allowed": False, "requires_user_action": False, - } - if goal_ref is not None: - payload["goal_ref"] = dict(goal_ref) - - def project_recovery_scheduler_hint( - decision: dict[str, Any], *, arbitration: SchedulerArbitration, - ) -> dict[str, Any]: - decision["agent_identity"] = build_quota_agent_identity(configured_goal, agent_id=agent_id) - host = scheduler_context.context - state = None - if scheduler_context.ok and host is not None and host.app_automation_applicable: - state = load_app_automation_scheduler_state( - runtime_root, goal_id=goal_id, agent_id=agent_id, surface=host.host_surface.value, - ) - return build_scheduler_hint( - decision, arbitration=arbitration, include_detail=include_scheduler_detail, - available_capabilities=available_capabilities, scheduler_execution_context=scheduler_context, - codex_app_scheduler_state=state, codex_app_current_rrule=codex_app_rrule, - codex_app_automation_id=codex_app_automation_id, - ) - - if scope_matches and not settled: - if (receipt_bound_todo_id and agent_id and turn_instance_id and readback is not None - and readback.replay_phase is ReceiptBoundReplayPhase.SETTLEMENT_PENDING): - attach_settlement_progress(payload, readback, registry_path=registry_path, - runtime_root=runtime_root, goal_ref=goal_ref) - if apply_receipt_bound_wait_recovery( - payload, registry_path=registry_path, runtime_root=runtime_root, goal_id=goal_id, - agent_id=agent_id, todo_id=receipt_bound_todo_id, turn_instance_id=turn_instance_id, - available_capabilities=available_capabilities, scheduler_execution_context=scheduler_context, - monitor_phase=readback.monitor_phase.value if readback.monitor_phase else None, - settlement_progress=readback.progress, replay_phase=readback.replay_phase, - heartbeat_receipt=readback.heartbeat_receipt, - ): - return payload - if apply_unsettled_host_turn_recovery_if_required( - payload, registry_path=registry_path, runtime_root=runtime_root, goal_id=goal_id, - agent_id=agent_id, current_turn_instance_id=turn_instance_id, - available_capabilities=available_capabilities, scheduler_execution_context=scheduler_context, - project_scheduler_hint=project_recovery_scheduler_hint, - goal_ref=goal_ref, - owner_action_id=goal_owner_resume_id(configured_goal), - ): - return payload if settled: - payload.update(settled_replay_fields()) + payload.update(cast(Mapping[str, object], settled_replay_fields())) else: - gateway = (check_remote_authority_gateway_status(goal_id, agent_id, remote_config) if scope_matches else { - **get_remote_authority_projection(goal_id, remote_config), - "status": "unavailable", "reason_code": "remote_authority_local_scope_mismatch", - }) - reason_code = gateway.get("reason_code", "remote_authority_execution_not_admitted") - reason = ("Remote authority read verified; shared Turn admission is unavailable" - if gateway["status"] == "verified" else "Waiting for a verified read from the configured remote authority") + gateway = ( + check_remote_authority_gateway_status(goal_id, agent_id, remote_config) + if scope_matches + else { + **get_remote_authority_projection(goal_id, remote_config), + "status": "unavailable", + "reason_code": "remote_authority_local_scope_mismatch", + } + ) + reason_code = gateway.get( + "reason_code", "remote_authority_execution_not_admitted" + ) + reason = ( + "Remote authority read verified; shared Turn admission is unavailable" + if gateway["status"] == "verified" + else "Waiting for a verified read from the configured remote authority" + ) action = EffectiveAction.BLOCKED_WAIT.value - payload.update(decision="wait", state="waiting", effective_action=action, - remote_gateway=gateway, reason_code=reason_code, reason=reason, recommended_action=reason, - execution_obligation={"kind": action, "must_attempt_work": False, "delivery_allowed": False, - "reason": reason, "spend_policy": "no quota spend for remote authority observation"}) + payload.update( + decision="wait", state="waiting", effective_action=action, + remote_gateway=gateway, reason_code=reason_code, + reason=reason, recommended_action=reason, + execution_obligation={ + "kind": action, "must_attempt_work": False, "delivery_allowed": False, + "reason": reason, + "spend_policy": "no quota spend for remote authority observation", + }, + ) payload["interaction_contract"] = build_interaction_contract( - payload, available_capabilities=available_capabilities, scheduler_execution_context=scheduler_context, - turn_instance_id=turn_instance_id, registry_path=str(registry_path), runtime_root=str(runtime_root), + payload, available_capabilities=available_capabilities, + scheduler_execution_context=scheduler_context, turn_instance_id=turn_instance_id, + registry_path=str(registry_path), runtime_root=str(runtime_root), ) return payload diff --git a/loopx/control_plane/quota/live_decision_contracts.py b/loopx/control_plane/quota/live_decision_contracts.py new file mode 100644 index 0000000000..4ffb4142fc --- /dev/null +++ b/loopx/control_plane/quota/live_decision_contracts.py @@ -0,0 +1,215 @@ +from __future__ import annotations + +from collections.abc import Callable, Mapping +from pathlib import Path +from typing import TYPE_CHECKING, Protocol, SupportsInt, TypedDict, cast + +if TYPE_CHECKING: + from .effect_program import ReceiptBoundReplayPhase + from .settlement import QuotaSettlementReadback + from ..scheduler.arbitration import SchedulerArbitration + from ..scheduler.execution_context import SchedulerExecutionContextResolution + from ..work_items.interaction_contract import InteractionContractPacket + + +JsonMap = dict[str, object] +JsonMapView = Mapping[str, object] +HostObservationResolver = Callable[..., Mapping[str, object]] +BoundedResearchFrontierProjector = Callable[..., Mapping[str, object] | None] +ProgressReviewContextReader = Callable[[dict[str, object], Path], dict[str, object] | None] +OperatorInboxUrgencyProjector = Callable[..., dict[str, object]] +SchedulerStateObserver = Callable[[Mapping[str, object]], None] + + +class ReceiptBoundGuardKwargs(TypedDict): + receipt_bound_replan_guard_scoped: bool + receipt_bound_replan_execution_pending: bool + receipt_bound_guard_recorded_at: str | None + receipt_bound_guard_turn_instance_id: str | None + receipt_bound_replan_review: Mapping[str, object] | None + receipt_bound_successor_replan: bool + + +class BuildInteractionContractFn(Protocol): + def __call__( + self, + payload: dict[str, object], + *, + available_capabilities: object = None, + scheduler_execution_context: Mapping[str, object] + | SchedulerExecutionContextResolution + | None = None, + turn_instance_id: str | None = None, + runtime_root: str | None = None, + registry_path: str | None = None, + ) -> InteractionContractPacket: ... + + +class AttachSettlementProgressFn(Protocol): + def __call__( + self, + payload: dict[str, object], + readback: QuotaSettlementReadback, + *, + registry_path: Path | None = None, + runtime_root: Path | None = None, + goal_ref: Mapping[str, object] | None = None, + ) -> None: ... + + +class ReadHeartbeatSettlementFn(Protocol): + def __call__( + self, + runtime_root: Path, + *, + goal_id: str, + agent_id: str | None, + todo_id: str | None, + turn_instance_id: str | None, + replan_obligation_id: str | None = None, + registry_path: Path | None = None, + goal_ref: Mapping[str, object] | None = None, + tolerate_malformed_history: bool = False, + ) -> QuotaSettlementReadback | None: ... + + +class ApplyWaitRecoveryFn(Protocol): + def __call__( + self, + payload: dict[str, object], + *, + registry_path: Path, + runtime_root: Path, + goal_id: str, + agent_id: str, + todo_id: str, + turn_instance_id: str, + available_capabilities: list[str] | None, + scheduler_execution_context: Mapping[str, object] + | SchedulerExecutionContextResolution + | None, + monitor_phase: str | None = None, + settlement_progress: dict[str, object] | None = None, + replay_phase: ReceiptBoundReplayPhase | None = None, + heartbeat_receipt: Mapping[str, object] | None = None, + ) -> bool: ... + + +class ApplyUnsettledRecoveryFn(Protocol): + def __call__( + self, + payload: dict[str, object], + *, + registry_path: Path, + runtime_root: Path, + goal_id: str, + agent_id: str | None, + current_turn_instance_id: str | None, + available_capabilities: list[str] | None, + project_scheduler_hint: Callable[..., dict[str, object]], + scheduler_execution_context: Mapping[str, object] + | SchedulerExecutionContextResolution + | None, + goal_ref: Mapping[str, object] | None = None, + owner_action_id: str | None = None, + inspect_current_execution_loss: bool = False, + ) -> bool: ... + + +class BuildSchedulerHintFn(Protocol): + def __call__( + self, + payload: dict[str, object], + *, + user_action_required: bool = False, + arbitration: SchedulerArbitration | None = None, + include_detail: bool = False, + codex_app_scheduler_state: dict[str, object] | None = None, + available_capabilities: object = None, + codex_app_current_rrule: object = None, + codex_app_automation_id: object = None, + scheduler_execution_context: Mapping[str, object] + | SchedulerExecutionContextResolution + | None = None, + ) -> dict[str, object]: ... + + +class LoadAppSchedulerStateFn(Protocol): + def __call__( + self, + runtime_root: Path, + *, + goal_id: str, + agent_id: str | None, + surface: str, + ) -> dict[str, object] | None: ... + + +class ReconcileRetainedSelectionFn(Protocol): + def __call__( + self, + *, + retained_todo_id: str, + projected_todo_id: str | None, + effective_action: str, + replan_obligation_id: str | None, + ) -> Mapping[str, object]: ... + + +def as_json_map(value: object) -> dict[str, object] | None: + """Narrow a dynamic object to its exact mutable dict without copying.""" + + return cast(dict[str, object], value) if isinstance(value, dict) else None + + +def optional_json_map(value: Mapping[str, object] | None) -> dict[str, object] | None: + """Return a dict view when given a mapping, keeping existing dict identity.""" + + if value is None: + return None + if isinstance(value, dict): + return cast(dict[str, object], value) + return dict(value) + + +def mapping_or_empty(value: object) -> Mapping[str, object]: + """Return a read-only Mapping view for a mapping candidate, else empty.""" + + return cast(Mapping[str, object], value) if isinstance(value, Mapping) else {} + + +def require_mapping(value: object, *, label: str = "mapping") -> Mapping[str, object]: + """Return a validated Mapping view or fail closed with TypeError on bad types.""" + + if isinstance(value, Mapping): + return cast(Mapping[str, object], value) + raise TypeError(f"Expected a mapping for {label}, got {type(value).__name__}") + + +def as_object_list(value: object) -> list[object] | None: + """Narrow a dynamic object to its mutable list without copying.""" + + return cast(list[object], value) if isinstance(value, list) else None + + +def non_negative_int(value: object) -> int: + """Convert an integer-like count matching `max(0, int(value or 0))`.""" + + if not value: + return 0 + if isinstance(value, (int, float, str, bytes, bytearray)): + return max(0, int(value)) + if isinstance(value, SupportsInt): + return max(0, int(value)) + raise TypeError( + f"int() argument must be a string, a bytes-like object or a real number, not {type(value).__name__!r}" + ) + + +def resolve_root_pair(registry_path: Path, runtime_root: Path) -> tuple[str, str]: + """Resolve registry and runtime-root paths once per route-binding pass.""" + + return ( + str(registry_path.expanduser().resolve()), + str(runtime_root.expanduser().resolve()), + ) diff --git a/loopx/control_plane/quota/live_decision_recovery.py b/loopx/control_plane/quota/live_decision_recovery.py new file mode 100644 index 0000000000..2beca75361 --- /dev/null +++ b/loopx/control_plane/quota/live_decision_recovery.py @@ -0,0 +1,386 @@ +from __future__ import annotations + +from collections.abc import Mapping +from functools import partial +from pathlib import Path +from typing import cast + +from ...agent_registry import load_goal_from_registry, registered_agent_ids_for_goal +from ..agents.identity import build_quota_agent_identity +from ..scheduler.arbitration import SchedulerArbitration +from ..scheduler.execution_context import ( + SchedulerExecutionContextResolution, + resolve_scheduler_execution_context, +) +from .effect_program import ReceiptBoundMonitorPhase, ReceiptBoundReplayPhase +from .live_decision_contracts import ( + ApplyUnsettledRecoveryFn, + ApplyWaitRecoveryFn, + AttachSettlementProgressFn, + BoundedResearchFrontierProjector, + BuildSchedulerHintFn, + HostObservationResolver, + LoadAppSchedulerStateFn, + ReadHeartbeatSettlementFn, + as_json_map, +) +from .live_decision_replan import ( + _original_replan_settlement_required, + _prepared_execution_needs_readback, + _receipt_bound_recovery_todo, + owner_resume_action_id, + resolve_host_rrule_and_automation, +) +from .settlement import QuotaSettlementReadback + + +def resolve_live_decision_preflight( + status_payload: dict[str, object], + *, + goal_id: str, + agent_id: str | None, + codex_app_current_rrule: str | None, + registry_path: Path, + runtime_root: Path, + scheduler_execution_context: ( + Mapping[str, object] | SchedulerExecutionContextResolution | None + ), + host_observation_resolver: HostObservationResolver | None, + bounded_research_frontier_projector: BoundedResearchFrontierProjector | None, +) -> tuple[ + SchedulerExecutionContextResolution, + dict[str, object] | None, + bool, + str, + str, + dict[str, object], + Mapping[str, object] | None, +]: + """Resolve host context, observed schedule state, frontier, and remote config.""" + + resolved_context = resolve_scheduler_execution_context(scheduler_execution_context) + from ..coordination.remote_authority_gateway import remote_authority_config + + registered_goal = load_goal_from_registry(registry_path, goal_id) + host_ctx = resolved_context.context + app_automation_applicable = ( + resolved_context.ok + and host_ctx is not None + and host_ctx.app_automation_applicable + ) + codex_app_host = bool( + app_automation_applicable + and host_ctx is not None + and host_ctx.host_surface.value == "codex_app" + ) + observed_rrule, observed_automation_id = resolve_host_rrule_and_automation( + codex_app_host=codex_app_host, + codex_app_current_rrule=codex_app_current_rrule, + host_observation_resolver=host_observation_resolver, + goal_id=goal_id, + agent_id=agent_id, + ) + decision_status_payload: dict[str, object] = { + **status_payload, + "runtime_root": str(runtime_root), + } + if bounded_research_frontier_projector is not None: + frontier = bounded_research_frontier_projector( + runtime_root=runtime_root, + goal_id=goal_id, + agent_id=agent_id, + status_payload=status_payload, + ) + if isinstance(frontier, Mapping): + decision_status_payload = { + **decision_status_payload, + "bounded_research_frontier": dict(frontier), + } + remote_config = remote_authority_config(registered_goal) + return ( + resolved_context, + registered_goal, + codex_app_host, + observed_rrule, + observed_automation_id, + decision_status_payload, + remote_config, + ) + + +def apply_live_decision_post_hooks_recovery( + payload: dict[str, object], + *, + settlement_readback: QuotaSettlementReadback | None, + receipt_bound_monitor_phase: ReceiptBoundMonitorPhase | None, + receipt_bound_replay_phase: ReceiptBoundReplayPhase | None, + receipt_bound_todo_id: str | None, + registry_path: Path, + runtime_root: Path, + goal_id: str, + agent_id: str | None, + turn_instance_id: str | None, + available_capabilities: list[str] | None, + include_scheduler_detail: bool, + resolved_context: SchedulerExecutionContextResolution, + app_scheduler_state: dict[str, object] | None, + observed_rrule: str, + observed_automation_id: str, + goal_ref: Mapping[str, object] | None, + registered_goal: dict[str, object] | None, + hook_dispatch: Mapping[str, object], + apply_recovery_fn: ApplyUnsettledRecoveryFn, + attach_settlement_fn: AttachSettlementProgressFn, + apply_wait_recovery_fn: ApplyWaitRecoveryFn, + build_scheduler_hint_fn: BuildSchedulerHintFn, +) -> None: + """Apply unsettled host recovery, receipt-bound wait recovery, and hook failure projection.""" + + original_replan_settlement = _original_replan_settlement_required( + payload, settlement_readback + ) + if ( + receipt_bound_replay_phase is not ReceiptBoundReplayPhase.SETTLED + and not original_replan_settlement + ): + apply_recovery_fn( + payload, + inspect_current_execution_loss=_prepared_execution_needs_readback( + payload, settlement_readback, receipt_bound_todo_id + ), + registry_path=registry_path, + runtime_root=runtime_root, + goal_id=goal_id, + agent_id=agent_id, + current_turn_instance_id=turn_instance_id, + available_capabilities=available_capabilities, + project_scheduler_hint=partial( + build_scheduler_hint_fn, + include_detail=include_scheduler_detail, + scheduler_execution_context=resolved_context, + available_capabilities=available_capabilities, + codex_app_scheduler_state=app_scheduler_state, + codex_app_current_rrule=observed_rrule, + codex_app_automation_id=observed_automation_id or None, + ), + scheduler_execution_context=resolved_context, + goal_ref=goal_ref, + owner_action_id=owner_resume_action_id(registered_goal), + ) + if ( + ( + recovery_todo_id := _receipt_bound_recovery_todo( + payload, + receipt_bound_todo_id, + agent_id=agent_id, + turn_instance_id=turn_instance_id, + replay_phase=receipt_bound_replay_phase, + ) + ) + and agent_id + and turn_instance_id + ): + if settlement_readback is not None: + attach_settlement_fn( + payload, + settlement_readback, + registry_path=registry_path, + runtime_root=runtime_root, + goal_ref=goal_ref, + ) + apply_wait_recovery_fn( + payload, + registry_path=registry_path, + runtime_root=runtime_root, + goal_id=goal_id, + agent_id=agent_id, + todo_id=recovery_todo_id, + turn_instance_id=turn_instance_id, + available_capabilities=available_capabilities, + scheduler_execution_context=resolved_context, + monitor_phase=( + receipt_bound_monitor_phase.value + if receipt_bound_monitor_phase + else None + ), + settlement_progress=as_json_map(settlement_readback.progress) + if settlement_readback + else None, + replay_phase=receipt_bound_replay_phase, + heartbeat_receipt=settlement_readback.heartbeat_receipt + if settlement_readback + else None, + ) + if hook_dispatch["failures"]: + payload["capability_hook_dispatch"] = { + key: value for key, value in hook_dispatch.items() if key != "projections" + } + + +def run_remote_authority_wait_preflight( + status_payload: dict[str, object], + *, + configured_goal: dict[str, object], + goal_id: str, + agent_id: str | None, + registry_path: Path, + runtime_root: Path, + goal_ref: Mapping[str, object] | None, + scheduler_context: SchedulerExecutionContextResolution, + available_capabilities: list[str] | None, + turn_instance_id: str | None, + include_scheduler_detail: bool, + codex_app_rrule: str, + codex_app_automation_id: str | None, + receipt_bound_todo_id: str | None, + receipt_bound_replan_obligation_id: str | None, + read_settlement_fn: ReadHeartbeatSettlementFn, + attach_settlement_fn: AttachSettlementProgressFn, + apply_wait_recovery_fn: ApplyWaitRecoveryFn, + apply_recovery_fn: ApplyUnsettledRecoveryFn, + build_scheduler_hint_fn: BuildSchedulerHintFn, + load_scheduler_state_fn: LoadAppSchedulerStateFn, +) -> tuple[dict[str, object] | None, dict[str, object], bool, bool]: + """Run hard-pause check and recovery preflight for remote-authority wait decisions.""" + + from ...quota import _build_quota_plan_for_goal + from .decision_summary import quota_plan_items + from .should_run import build_quota_paused_should_run_payload + from .states import quota_item_is_paused + + plan, health_ok = _build_quota_plan_for_goal(status_payload, goal_id=goal_id) + item = next( + (row for row in quota_plan_items(plan) if row.get("goal_id") == goal_id), + None, + ) + if item is not None and quota_item_is_paused(item): + paused = cast( + dict[str, object], + build_quota_paused_should_run_payload( + status_payload, + safe_goal_id=goal_id, + requested_agent_id=agent_id, + item=item, + plan=plan, + goal_health_ok=health_ok, + include_scheduler_detail=include_scheduler_detail, + codex_app_current_rrule=codex_app_rrule, + codex_app_automation_id=codex_app_automation_id, + resolved_scheduler_context=scheduler_context, + runtime_root=runtime_root, + goal_ref=goal_ref, + ), + ) + return paused, paused, False, False + + scope_matches = ( + goal_ref is None + or goal_ref.get("goal_instance_id") == configured_goal.get("goal_instance_id") + ) and agent_id in registered_agent_ids_for_goal(configured_goal) + readback = ( + read_settlement_fn( + runtime_root, + goal_id=goal_id, + agent_id=agent_id, + todo_id=receipt_bound_todo_id, + turn_instance_id=turn_instance_id, + registry_path=registry_path, + goal_ref=goal_ref, + replan_obligation_id=( + None if receipt_bound_todo_id else receipt_bound_replan_obligation_id + ), + ) + if scope_matches and turn_instance_id + else None + ) + settled = ( + readback is not None and readback.replay_phase is ReceiptBoundReplayPhase.SETTLED + ) + payload: dict[str, object] = { + "ok": True, + "mode": "should-run", + "goal_id": goal_id, + "agent_id": agent_id, + "should_run": False, + "normal_delivery_allowed": False, + "requires_user_action": False, + } + if goal_ref is not None: + payload["goal_ref"] = dict(goal_ref) + + def project_recovery_scheduler_hint( + decision: dict[str, object], + *, + arbitration: SchedulerArbitration, + ) -> dict[str, object]: + decision["agent_identity"] = build_quota_agent_identity( + configured_goal, agent_id=agent_id + ) + host = scheduler_context.context + state = None + if scheduler_context.ok and host is not None and host.app_automation_applicable: + state = load_scheduler_state_fn( + runtime_root, + goal_id=goal_id, + agent_id=agent_id, + surface=host.host_surface.value, + ) + return build_scheduler_hint_fn( + decision, + arbitration=arbitration, + include_detail=include_scheduler_detail, + available_capabilities=available_capabilities, + scheduler_execution_context=scheduler_context, + codex_app_scheduler_state=state, + codex_app_current_rrule=codex_app_rrule, + codex_app_automation_id=codex_app_automation_id, + ) + + if scope_matches and not settled: + if ( + receipt_bound_todo_id + and agent_id + and turn_instance_id + and readback is not None + and readback.replay_phase is ReceiptBoundReplayPhase.SETTLEMENT_PENDING + ): + attach_settlement_fn( + payload, + readback, + registry_path=registry_path, + runtime_root=runtime_root, + goal_ref=goal_ref, + ) + if apply_wait_recovery_fn( + payload, + registry_path=registry_path, + runtime_root=runtime_root, + goal_id=goal_id, + agent_id=agent_id, + todo_id=receipt_bound_todo_id, + turn_instance_id=turn_instance_id, + available_capabilities=available_capabilities, + scheduler_execution_context=scheduler_context, + monitor_phase=( + readback.monitor_phase.value if readback.monitor_phase else None + ), + settlement_progress=as_json_map(readback.progress), + replay_phase=readback.replay_phase, + heartbeat_receipt=readback.heartbeat_receipt, + ): + return payload, payload, scope_matches, settled + if apply_recovery_fn( + payload, + registry_path=registry_path, + runtime_root=runtime_root, + goal_id=goal_id, + agent_id=agent_id, + current_turn_instance_id=turn_instance_id, + available_capabilities=available_capabilities, + scheduler_execution_context=scheduler_context, + project_scheduler_hint=project_recovery_scheduler_hint, + goal_ref=goal_ref, + owner_action_id=owner_resume_action_id(configured_goal), + ): + return payload, payload, scope_matches, settled + return None, payload, scope_matches, settled diff --git a/loopx/control_plane/quota/live_decision_replan.py b/loopx/control_plane/quota/live_decision_replan.py new file mode 100644 index 0000000000..1e1b3ae3f0 --- /dev/null +++ b/loopx/control_plane/quota/live_decision_replan.py @@ -0,0 +1,578 @@ +from __future__ import annotations + +from collections.abc import Mapping +from pathlib import Path +from typing import cast + +from ..agent_context import project_agent_context, project_goal_agent_context +from ..goals.activation import goal_owner_resume_id +from ..goals.goal_frontier.fallback_source import ( + FallbackTodoSource, + live_fallback_authority_items, +) +from ..runtime.time import now_utc_iso +from ..scheduler.execution_context import SchedulerExecutionContextResolution +from ..work_items.interaction_contract import build_interaction_contract +from .blocked_retry import overlay_active_turn_retries +from .effect_program import ReceiptBoundReplayPhase +from .effective_action import EffectiveAction +from .live_decision_contracts import ( + AttachSettlementProgressFn, + BuildInteractionContractFn, + HostObservationResolver, + ProgressReviewContextReader, + ReceiptBoundGuardKwargs, + as_json_map, + as_object_list, + mapping_or_empty, + require_mapping, +) +from .settlement import QuotaSettlementReadback, attach_settlement_progress +from .unsettled_host_turn import restore_receipt_bound_todo_source + + +def _split_receipt_bound_guard_fields( + readback: QuotaSettlementReadback | None, + obligation_id: str | None, +) -> tuple[ReceiptBoundGuardKwargs, str | None]: + """Build typed replan-guard kwargs together with the admission event id.""" + + guard = getattr(readback, "semantic_replan_guard", None) + raw_receipt = getattr(readback, "heartbeat_receipt", None) + receipt = require_mapping(raw_receipt, label="heartbeat_receipt") if raw_receipt else {} + identity = readback.identity.value if readback is not None else None + guard_scoped = bool( + obligation_id + and isinstance(guard, Mapping) + and guard.get("scope") == "turn_guard" + and guard.get("selected_obligation_id") == obligation_id + ) + # The effective receipt stays the latest content. The admission clock is the + # first bound receipt already selected by the GoalRef-filtered readback. + admission_recorded_at = getattr(readback, "admission_recorded_at", None) + raw_recorded = admission_recorded_at or receipt.get("recorded_at") + recorded_at = raw_recorded if isinstance(raw_recorded, str) else None + raw_admission_event_id = getattr(readback, "admission_event_id", None) + admission_event_id = ( + raw_admission_event_id if isinstance(raw_admission_event_id, str) else None + ) + raw_review = ( + guard.get("selected_review_source") if isinstance(guard, Mapping) else None + ) + kwargs: ReceiptBoundGuardKwargs = { + "receipt_bound_replan_guard_scoped": guard_scoped, + "receipt_bound_replan_execution_pending": bool( + guard_scoped and readback and readback.replan_execution_pending + ), + "receipt_bound_guard_recorded_at": recorded_at, + "receipt_bound_guard_turn_instance_id": ( + identity.turn_instance_id if identity is not None else None + ), + "receipt_bound_replan_review": ( + mapping_or_empty(raw_review) if isinstance(raw_review, Mapping) else None + ), + "receipt_bound_successor_replan": bool( + getattr(readback, "successor_replan_pending", False) + ), + } + return kwargs, admission_event_id + + +def _receipt_bound_guard_fields( + readback: QuotaSettlementReadback | None, + obligation_id: str | None, +) -> dict[str, object]: + """Adapt the recorded guard identity and time for the shared decision owner.""" + + guard_kwargs, admission_event_id = _split_receipt_bound_guard_fields( + readback, obligation_id + ) + return { + **guard_kwargs, + "receipt_bound_admission_event_id": admission_event_id, + } + + +def _historical_ack_writeback_required(payload: Mapping[str, object]) -> bool: + """Recognize the exact historical ACK closeout before durable writeback.""" + + packet = payload.get("replan_action_packet") + return ( + isinstance(packet, Mapping) + and packet.get("settlement_only") is True + and packet.get("decision") == "historical_ack_recorded" + ) + + +def _prepared_execution_needs_readback( + payload: Mapping[str, object], + readback: QuotaSettlementReadback | None, + receipt_todo_id: str | None, +) -> bool: + """Ask the receipt owner to check a prepared generation missing from the current projection.""" + + if readback is None or readback.replay_phase is not ReceiptBoundReplayPhase.OPEN: + return False + prepared = readback.prepared_lease + if not prepared or prepared.get("prepared_lease_epoch") is None: + return False + raw_current = payload.get("execution_preparation") + current = ( + require_mapping(raw_current, label="execution_preparation") + if raw_current + else {} + ) + return ( + current.get("todo_id") != receipt_todo_id + or current.get("lease_epoch") != prepared["prepared_lease_epoch"] + ) + + +def _original_replan_settlement_required( + payload: Mapping[str, object], + readback: QuotaSettlementReadback | None, +) -> bool: + if _historical_ack_writeback_required(payload): + return True + if ( + readback is None + or readback.replay_phase is not ReceiptBoundReplayPhase.SETTLEMENT_PENDING + ): + return False + packet = payload.get("replan_action_packet") + identity = readback.identity.value + progress = mapping_or_empty(readback.progress) + return ( + isinstance(packet, Mapping) + and packet.get("settlement_only") is True + and packet.get("decision") == readback.replay_phase.value + and identity is not None + and identity.replan_obligation_id is not None + and identity.replan_obligation_id == packet.get("obligation_id") + and ( + progress.get("next_step") == "quota_spend" + or progress.get("checkpoint_recovery_required") is True + ) + and payload.get("should_run") is True + and payload.get("effective_action") + == EffectiveAction.AUTONOMOUS_REPLAN_REQUIRED.value + ) + + +def _project_receipt_replan_settlement( + payload: dict[str, object], + readback: QuotaSettlementReadback | None, + *, + available_capabilities: list[str] | None, + scheduler_execution_context: ( + Mapping[str, object] | SchedulerExecutionContextResolution | None + ), + turn_instance_id: str | None, + registry_path: Path, + runtime_root: Path, + goal_ref: Mapping[str, object] | None, + attach_settlement_fn: AttachSettlementProgressFn = attach_settlement_progress, + build_interaction_contract_fn: BuildInteractionContractFn = build_interaction_contract, +) -> None: + """Project closeout from the original readback, never the current selection.""" + + if not readback or not _original_replan_settlement_required(payload, readback): + return + attach_settlement_fn( + payload, + readback, + registry_path=registry_path, + runtime_root=runtime_root, + goal_ref=goal_ref, + ) + identity = readback.identity.value + if identity is None: + raise ValueError("original replan settlement identity is unavailable") + payload["heartbeat_receipt"] = { + **mapping_or_empty(readback.heartbeat_receipt), + "settlement_identity": identity.as_dict(), + } + if mapping_or_empty(readback.progress).get("checkpoint_recovery_required") is True: + reason = ( + "Read checkpoint-context for the original blocked writeback. Reassess its evidence and submit " + "only the original checkpoint supplement. Preserve its delivery fields and quota." + ) + packet = { + **require_mapping( + payload["replan_action_packet"], label="replan_action_packet" + ), + "decision": "vision_outcome_checkpoint_required", + "required_outcome": "durable_writeback_receipt", + "writeback_contract": {"rule": reason}, + } + obligation = require_mapping( + payload["autonomous_replan_obligation"], + label="autonomous_replan_obligation", + ) + payload["autonomous_replan_obligation"] = { + **obligation, + "recommended_action": reason, + "guidance_actions": [reason], + "settlement_action_packet": packet, + } + payload.update(replan_action_packet=packet, recommended_action=reason) + payload["interaction_contract"] = build_interaction_contract_fn( + payload, + available_capabilities=available_capabilities, + scheduler_execution_context=scheduler_execution_context, + turn_instance_id=turn_instance_id, + registry_path=str(registry_path), + runtime_root=str(runtime_root), + ) + + +def _receipt_bound_recovery_todo( + payload: Mapping[str, object], + receipt_bound_todo_id: str | None, + *, + agent_id: str | None, + turn_instance_id: str | None, + replay_phase: ReceiptBoundReplayPhase | None, +) -> str | None: + """Keep finished/replan-only Turns out of generic Todo wait recovery.""" + + if ( + not receipt_bound_todo_id + or not agent_id + or not turn_instance_id + or replay_phase is ReceiptBoundReplayPhase.SETTLED + or _historical_ack_writeback_required(payload) + or payload.get("effective_action") + == EffectiveAction.UNSETTLED_HOST_TURN_RECOVERY.value + ): + return None + if replay_phase is ReceiptBoundReplayPhase.SETTLEMENT_PENDING: + return receipt_bound_todo_id + selected = mapping_or_empty(payload.get("selected_todo")) + if ( + selected.get("todo_id") != receipt_bound_todo_id + or payload.get("normal_delivery_allowed") is not True + ): + return receipt_bound_todo_id + wait_kind = str(selected.get("resume_when") or "").split(":", 1)[0] + return ( + receipt_bound_todo_id + if wait_kind not in ("", "monitor_changed", "todo_done") + else None + ) + + +def _restore_replan_obligation_without_foreign_acks( + status_payload: dict[str, object], + *, + goal_id: str, + agent_id: str, + goal_ref: Mapping[str, object] | None, + registry_goal: Mapping[str, object] | None, + registry_path: Path, + runtime_root: Path, + progress_review_context_reader: ProgressReviewContextReader | None, +) -> None: + """Recompute the open review after removing another instance's ACK rows.""" + + from ..status.autonomous_replan_projection import autonomous_replan_obligation_from_runs + from ..work_items.autonomous_replan_ack import drop_unowned_recorded_replan_acks + from ..work_items.project_asset import attach_active_state_project_asset_fields + from ..work_items.replan_history_codec import effective_turn_cadence_context + + run_history = as_json_map(status_payload.get("run_history")) + goals = as_object_list(run_history.get("goals")) if run_history is not None else None + goal: dict[str, object] | None = None + if goals is not None: + for entry in goals: + entry_map = as_json_map(entry) + if entry_map is not None and str(entry_map.get("id") or "") == goal_id: + goal = entry_map + break + if goal is None: + return + latest_runs_raw = as_object_list(goal.get("latest_runs")) + if latest_runs_raw is None: + return + filtered, changed = drop_unowned_recorded_replan_acks( + cast(list[dict[str, object]], latest_runs_raw), + goal_id=goal_id, + goal_ref=goal_ref, + ) + if not changed: + return + goal["latest_runs"] = filtered + kept_ack_times = { + run.get("generated_at") + for run in filtered + if isinstance(run.get("autonomous_replan_ack"), dict) + } + semantic = as_json_map(goal.get("semantic_history")) + agents = as_object_list(semantic.get("agents")) if semantic is not None else None + if agents is not None: + for context_item in agents: + context = as_json_map(context_item) + if context is None: + continue + ack_run = as_json_map(context.get("latest_autonomous_replan_ack_run")) + if ( + ack_run is not None + and ack_run.get("generated_at") not in kept_ack_times + ): + context.pop("latest_autonomous_replan_ack_run", None) + queue = as_json_map(status_payload.get("attention_queue")) + items = as_object_list(queue.get("items")) if queue is not None else None + item: dict[str, object] | None = None + if items is not None: + for entry in items: + entry_map = as_json_map(entry) + if entry_map is not None and entry_map.get("goal_id") == goal_id: + item = entry_map + break + if item is None: + return + for target in (item, as_json_map(item.get("project_asset"))): + if target is None: + continue + target.pop("autonomous_replan_obligation", None) + target.pop("autonomous_replan_obligations_by_agent", None) + target.pop("autonomous_replan_ack", None) + source_goal: dict[str, object] = ( + dict(registry_goal) if isinstance(registry_goal, Mapping) else {"id": goal_id} + ) + source_goal.setdefault("id", goal_id) + review = ( + progress_review_context_reader(source_goal, runtime_root) + if progress_review_context_reader is not None + else None + ) + cadence_goal_ref = ( + { + str(key): value + for key, value in goal_ref.items() + if isinstance(value, str) + } + if isinstance(goal_ref, Mapping) + and all(isinstance(value, str) for value in goal_ref.values()) + else None + ) + attach_active_state_project_asset_fields( + item, + latest_runs=filtered, + autonomous_replan_obligation_from_runs=autonomous_replan_obligation_from_runs, + external_progress_review=review, + effective_turn_cadence=effective_turn_cadence_context( + source_goal, + runtime_root, + registry_path=registry_path, + goal_ref=cadence_goal_ref, + ), + agent_lane_id=agent_id, + ) + + +def resolve_host_rrule_and_automation( + *, + codex_app_host: bool, + codex_app_current_rrule: str | None, + host_observation_resolver: HostObservationResolver | None, + goal_id: str, + agent_id: str | None, +) -> tuple[str, str]: + """Resolve observed Codex App RRULE and automation id from inputs or host.""" + + observed_rrule = str(codex_app_current_rrule or "").strip() + observed_automation_id = "" + if ( + codex_app_host + and not observed_rrule + and host_observation_resolver is not None + ): + observation = host_observation_resolver(goal_id=goal_id, agent_id=agent_id) + if observation.get("available") is True: + observed_rrule = str(observation.get("rrule") or "") + observed_automation_id = str( + observation.get("automation_id") or "" + ).strip() + return observed_rrule, observed_automation_id + + +def attach_goal_todos_to_attention_queue( + decision_status_payload: dict[str, object], + *, + registry_path: Path, + runtime_root: Path, + goal_id: str, +) -> None: + """Hydrate complete Goal Todos into the matching attention-queue item.""" + + from ..todos.list_readback import list_goal_todos + + source = list_goal_todos( + registry_path=registry_path, + runtime_root_arg=str(runtime_root), + goal_id=goal_id, + ) + todo_fields: dict[str, object] = { + key: source[key] for key in ("user_todos", "agent_todos") + } + raw_queue = decision_status_payload.get("attention_queue") + queue = require_mapping(raw_queue, label="attention_queue") if raw_queue else {} + items = queue.get("items") or [] + if not isinstance(items, list): + raise TypeError( + f"Expected list for attention_queue.items, got {type(items).__name__}" + ) + updated_items: list[object] = [] + for item in items: + item_map = require_mapping(item, label="attention_queue.item") + updated_items.append( + {**item_map, **todo_fields} if item_map.get("goal_id") == goal_id else item + ) + decision_status_payload["attention_queue"] = { + **queue, + "items": updated_items, + } + + +def prepare_live_decision_status_payload( + decision_status_payload: dict[str, object], + *, + goal_id: str, + agent_id: str | None, + goal_ref: Mapping[str, object] | None, + registered_goal: dict[str, object] | None, + registry_path: Path, + runtime_root: Path, + guard_kwargs: ReceiptBoundGuardKwargs, + admission_event_id: str | None, + receipt_bound_replay_phase: ReceiptBoundReplayPhase | None, + receipt_bound_todo_id: str | None, + requested_action_todo_id: str | None, + retained_action_selection_todo_id: str | None, + receipt_bound_replan_obligation_id: str | None, + progress_review_context_reader: ProgressReviewContextReader | None, +) -> tuple[dict[str, object], FallbackTodoSource]: + """Hydrate status payload with replan ACK cleanup, Todo snapshots, retries and fallbacks.""" + + if ( + guard_kwargs["receipt_bound_replan_guard_scoped"] + and receipt_bound_replay_phase is ReceiptBoundReplayPhase.OPEN + and receipt_bound_replan_obligation_id + and agent_id + ): + from ..work_items.autonomous_replan_ack import load_receipt_replan_ack_candidates + + decision_status_payload["receipt_bound_replan_ack_candidates"] = ( + load_receipt_replan_ack_candidates( + runtime_root, + goal_id=goal_id, + agent_id=agent_id, + obligation_id=receipt_bound_replan_obligation_id, + guard_recorded_at=guard_kwargs["receipt_bound_guard_recorded_at"], + guard_turn_instance_id=guard_kwargs[ + "receipt_bound_guard_turn_instance_id" + ], + goal_ref=goal_ref, + admission_event_id=admission_event_id, + ) + ) + _restore_replan_obligation_without_foreign_acks( + decision_status_payload, + goal_id=goal_id, + agent_id=agent_id, + goal_ref=goal_ref, + registry_goal=registered_goal, + registry_path=registry_path, + runtime_root=runtime_root, + progress_review_context_reader=progress_review_context_reader, + ) + if ( + requested_action_todo_id or retained_action_selection_todo_id + ) and not receipt_bound_todo_id: + attach_goal_todos_to_attention_queue( + decision_status_payload, + registry_path=registry_path, + runtime_root=runtime_root, + goal_id=goal_id, + ) + decision_status_payload = restore_receipt_bound_todo_source( + decision_status_payload, + registry_path=registry_path, + runtime_root=runtime_root, + goal_id=goal_id, + todo_id=receipt_bound_todo_id, + replay_phase=receipt_bound_replay_phase, + replan_guard_scoped=guard_kwargs["receipt_bound_replan_guard_scoped"], + ) + decision_status_payload = overlay_active_turn_retries( + decision_status_payload, + goal_id=goal_id, + agent_id=agent_id, + observed_at=now_utc_iso(), + ) + authoritative_fallback_todo_items = live_fallback_authority_items( + decision_status_payload, + registry_path=registry_path, + runtime_root=runtime_root, + goal_id=goal_id, + agent_id=agent_id, + ) + return decision_status_payload, authoritative_fallback_todo_items + + +def project_before_plan_agent_context( + payload: dict[str, object], + *, + goal_id: str, + agent_id: str, + registered_goal: dict[str, object] | None, + orchestration: Mapping[str, object], + registry_path: Path, + runtime_root: Path, +) -> None: + """Attach the agent capability-improvement context to an active interaction contract.""" + + interaction = payload.get("interaction_contract") + if not isinstance(interaction, dict): + return + raw_selected = payload.get("selected_todo") + selected_todo = ( + require_mapping(raw_selected, label="selected_todo") if raw_selected else {} + ) + scope: dict[str, object] = { + "goal_id": goal_id, + "agent_id": agent_id, + "todo_id": selected_todo.get("todo_id"), + } + if registered_goal is not None: + context = project_goal_agent_context( + phase="before_plan", + scope=scope, + goal=registered_goal, + registry_path=registry_path, + runtime_root=runtime_root, + observations={ + "capability_improvement": { + "trigger": ( + "replan" + if payload.get("replan_action_packet") + else "before_plan" + ), + } + }, + ) + else: + context = project_agent_context( + phase="before_plan", + scope=scope, + orchestration=dict(orchestration), + ) + if context is not None: + interaction["agent_context"] = context + + +def owner_resume_action_id(registered_goal: dict[str, object] | None) -> str | None: + """Return owner-resume action id from the registered goal.""" + + return goal_owner_resume_id(registered_goal) diff --git a/loopx/control_plane/quota/live_decision_routes.py b/loopx/control_plane/quota/live_decision_routes.py new file mode 100644 index 0000000000..f28802b527 --- /dev/null +++ b/loopx/control_plane/quota/live_decision_routes.py @@ -0,0 +1,233 @@ +from __future__ import annotations + +import os +import shlex +from pathlib import Path + +from ...command_invocation import ( + current_loopx_cli_argv, + loopx_cli_argv, + resolve_command_path, +) +from .effect_program import is_turn_reference, turn_reference +from .live_decision_contracts import ( + as_json_map, + as_object_list, + mapping_or_empty, + resolve_root_pair, +) + + +def _bind_followup_turn_argument( + cli_args: list[object], + reference: str, + *, + authoritative: bool, +) -> str: + """Point a follow-up's ``--turn-instance-id`` value at ``reference`` in place. + + An authoritative Turn always wins, because it owns the receipt the follow-up + replays. Without one only a shell reference is re-rendered, so a concrete + projected Turn survives untouched. Returns the value the vector now carries, + so a mirrored ``args`` block cannot disagree with the executed command. + """ + + index = ( + cli_args.index("--turn-instance-id") + if "--turn-instance-id" in cli_args + else -1 + ) + if index < 0: + cli_args.extend(["--turn-instance-id", reference]) + return reference + if index + 1 >= len(cli_args): + cli_args.append(reference) + return reference + current_arg = cli_args[index + 1] + if authoritative or is_turn_reference( + current_arg if isinstance(current_arg, str) else None + ): + cli_args[index + 1] = reference + return str(cli_args[index + 1]) + + +def _scheduler_fallback_launcher() -> list[str]: + """Keep an executable-bound replay on its selected Core installation.""" + + fallback = "loopx-apply-rrule" + route = current_loopx_cli_argv() + if route[1:] != ["--cli-bin", route[0]]: + return [fallback] + configured = route[0] + candidate = Path(configured) + resolved = candidate if candidate.is_absolute() else resolve_command_path(configured) + if resolved is None or not resolved.is_file(): + return [fallback, "--loopx", configured] + executable = resolved.resolve() + companion = executable.with_name(fallback) + if os.name != "nt" and companion.is_file() and os.access(companion, os.X_OK): + fallback = str(companion) + return [fallback, "--loopx", str(executable)] + + +def bind_scheduler_followup_cli_routes( + payload: dict[str, object], + *, + registry_path: Path, + runtime_root: Path, + turn_instance_id: str | None = None, + source: str = "quota_cli_invocation", +) -> None: + """Bind scheduler follow-ups to the registry/runtime/Turn that built the hint.""" + + scheduler_hint = as_json_map(payload.get("scheduler_hint")) + if scheduler_hint is None: + return + app_packet = as_json_map(scheduler_hint.get("app_automation")) + if app_packet is None: + return + launcher_args = loopx_cli_argv()[1:] + safe_turn_instance_id = str(turn_instance_id or "").strip() + resolved_roots: tuple[str, str] | None = None + + def bound_roots() -> tuple[str, str]: + nonlocal resolved_roots + if resolved_roots is None: + resolved_roots = resolve_root_pair(registry_path, runtime_root) + return resolved_roots + + for hint_name in ("ack_hint", "failure_hint", "fallback_hint"): + followup_hint = as_json_map(app_packet.get(hint_name)) + if followup_hint is None: + continue + cli_args = as_object_list(followup_hint.get("cli_args")) + if not cli_args: + continue + if hint_name == "fallback_hint": + if cli_args[0] != "loopx-apply-rrule" or "--registry" in cli_args: + continue + resolved_registry, resolved_runtime_root = bound_roots() + bound_cli_args: list[object] = [ + *_scheduler_fallback_launcher(), + "--registry", + resolved_registry, + "--runtime-root", + resolved_runtime_root, + *cli_args[1:], + ] + # The fallback replays this decision through ``loopx quota + # should-run``, so it needs the Turn that owns the receipt. + # Projections leave a shell reference here; a native Windows + # host cannot expand the POSIX form and would replay an empty + # Turn instead of the decision it came from. + bound_turn = _bind_followup_turn_argument( + bound_cli_args, + turn_reference(safe_turn_instance_id), + authoritative=bool(safe_turn_instance_id), + ) + followup_hint["cli_args"] = bound_cli_args + args_value = as_json_map(followup_hint.get("args")) + if args_value is not None and "turn_instance_id" in args_value: + args_value["turn_instance_id"] = bound_turn + followup_hint["route_binding"] = { + "schema_version": "codex_app_scheduler_fallback_route_v0", + "source": source, + "registry_bound": True, + "runtime_root_bound": True, + "turn_instance_bound": bool(safe_turn_instance_id), + } + continue + bound_cli_args = list(cli_args) + # Standalone Core emits an interpreter route. Its launcher + # arguments must precede the CLI's global options. + option_index = ( + len(launcher_args) + if cli_args[: len(launcher_args)] == launcher_args + else 0 + ) + if bound_cli_args[option_index : option_index + 1] != ["--registry"]: + resolved_registry, resolved_runtime_root = bound_roots() + bound_cli_args[option_index:option_index] = [ + "--registry", + resolved_registry, + "--runtime-root", + resolved_runtime_root, + ] + if safe_turn_instance_id and "--turn-instance-id" not in bound_cli_args: + execute_index = ( + bound_cli_args.index("--execute") + if "--execute" in bound_cli_args + else len(bound_cli_args) + ) + bound_cli_args[execute_index:execute_index] = [ + "--turn-instance-id", + safe_turn_instance_id, + ] + args_value = as_json_map(followup_hint.get("args")) + if args_value is not None: + args_value["turn_instance_id"] = safe_turn_instance_id + followup_hint["cli_args"] = bound_cli_args + followup_hint["route_binding"] = { + "schema_version": ( + "scheduler_ack_cli_route_v0" + if hint_name == "ack_hint" + else "scheduler_failure_cli_route_v0" + ), + "source": source, + "registry_bound": True, + "runtime_root_bound": True, + "turn_instance_bound": bool(safe_turn_instance_id), + } + + +def bind_action_selection_cli_routes( + payload: dict[str, object], + *, + registry_path: Path, + runtime_root: Path, +) -> None: + """Bind two-phase action-selection commands to this live CLI source.""" + + interaction = mapping_or_empty(payload.get("interaction_contract")) + cli_channel = as_json_map(interaction.get("cli_channel")) + if cli_channel is None: + return + if cli_channel.get("selection_required") is not True: + return + selection_command = as_json_map(cli_channel.get("selection_command")) + if selection_command is None: + return + route_prefix = selection_command.get("route_prefix") + if not isinstance(route_prefix, str): + return + try: + tokens = shlex.split(route_prefix) + except ValueError: + return + if len(tokens) >= 3 and tokens[0] == "loopx": + try: + format_index = tokens.index("--format") + except ValueError: + return + if tokens[format_index : format_index + 2] != ["--format", "json"]: + return + needs_registry = "--registry" not in tokens + needs_runtime = "--runtime-root" not in tokens + if not needs_registry and not needs_runtime: + selection_command["route_prefix"] = shlex.join(tokens) + return + resolved_registry = ( + str(registry_path.expanduser().resolve()) if needs_registry else None + ) + resolved_runtime = ( + str(runtime_root.expanduser().resolve()) if needs_runtime else None + ) + if resolved_registry is not None: + tokens[1:1] = ["--registry", resolved_registry] + format_index += 2 + if resolved_runtime is not None: + tokens[format_index:format_index] = [ + "--runtime-root", + resolved_runtime, + ] + selection_command["route_prefix"] = shlex.join(tokens) diff --git a/loopx/control_plane/quota/live_decision_turn_reads.py b/loopx/control_plane/quota/live_decision_turn_reads.py new file mode 100644 index 0000000000..56bd345f00 --- /dev/null +++ b/loopx/control_plane/quota/live_decision_turn_reads.py @@ -0,0 +1,141 @@ +from __future__ import annotations + +from collections.abc import Mapping + +from .live_decision_contracts import ( + OperatorInboxUrgencyProjector, + mapping_or_empty, + non_negative_int, +) + + +def _fresh_operator_inbox_observation_count( + dispatch: Mapping[str, object] | None, +) -> int: + """Count operator-inbox observations in the current dispatch marked for agent read.""" + + if not isinstance(dispatch, Mapping): + return 0 + required_reads = dispatch.get("required_reads") + results = dispatch.get("results") + if not isinstance(required_reads, list) or not isinstance(results, list): + return 0 + operator_inbox_hook_ids = { + str(read.get("hook_id") or "") + for read in required_reads + if isinstance(read, Mapping) + and read.get("kind") == "operator_inbox" + and str(read.get("hook_id") or "") + } + return sum( + non_negative_int(result.get("observation_count")) + for result in results + if isinstance(result, Mapping) + and result.get("agent_read_required") is True + and result.get("hook_id") in operator_inbox_hook_ids + ) + + +def _fresh_operator_inbox_read_required( + dispatch: Mapping[str, object] | None, +) -> bool: + """Return whether any projected operator-inbox read still demands agent review.""" + + return _fresh_operator_inbox_observation_count(dispatch) > 0 + + +def _fresh_read_covers_pending_count( + fresh_count: int, + projector: OperatorInboxUrgencyProjector | None, +) -> OperatorInboxUrgencyProjector | None: + """Wrap the urgency projector using a precomputed fresh inbox observation count.""" + + if projector is None: + return None + + def project(**kwargs: object) -> dict[str, object]: + urgency = dict(projector(**kwargs)) + pending_count = non_negative_int(urgency.get("pending_count")) + if pending_count <= fresh_count: + urgency["material_review_due"] = False + return urgency + + return project + + +def _fresh_read_covers_all_pending_material( + dispatch: Mapping[str, object] | None, + projector: OperatorInboxUrgencyProjector | None, +) -> OperatorInboxUrgencyProjector | None: + """Clear material-review-due when current turn-start reads cover all pending inbox rows.""" + + if projector is None: + return None + fresh_count = _fresh_operator_inbox_observation_count(dispatch) + return _fresh_read_covers_pending_count(fresh_count, projector) + + +def _turn_start_required_reads( + dispatch: Mapping[str, object] | None, +) -> list[dict[str, object]]: + """Read only the generic, kernel-validated pre-work projection.""" + + if not isinstance(dispatch, Mapping): + return [] + reads = dispatch.get("required_reads") + if not isinstance(reads, list): + return [] + projected: list[dict[str, object]] = [] + seen_commands: set[str] = set() + for read in reads: + if not isinstance(read, Mapping): + continue + command = read.get("command") + if not isinstance(command, str) or not command.strip(): + continue + normalized = command.strip() + if normalized in seen_commands: + continue + projected.append( + { + key: read[key] + for key in ( + "kind", + "command", + "reason", + "source", + "ordering", + "hook_id", + "capability_id", + "prompt_budget_bytes", + ) + if key in read + } + ) + seen_commands.add(normalized) + return projected + + +def merge_turn_start_required_reads( + existing: object, + projected: list[dict[str, object]], +) -> list[dict[str, object]]: + """Append newly projected turn-start reads while preserving command order and uniqueness.""" + + required_reads = ( + [dict(mapping_or_empty(item)) for item in existing if isinstance(item, Mapping)] + if isinstance(existing, list) + else [] + ) + seen_commands = { + str(item.get("command") or "").strip() + for item in required_reads + if str(item.get("command") or "").strip() + } + for required_read in projected: + command = str(required_read.get("command") or "").strip() + if command in seen_commands: + continue + required_reads.append(required_read) + seen_commands.add(command) + return required_reads diff --git a/tests/control_plane/test_live_decision_contract.py b/tests/control_plane/test_live_decision_contract.py new file mode 100644 index 0000000000..759ae89876 --- /dev/null +++ b/tests/control_plane/test_live_decision_contract.py @@ -0,0 +1,566 @@ +from __future__ import annotations + +import ast +import json +from pathlib import Path + +import pytest + +from loopx.control_plane.quota import ( + live_decision, + live_decision_replan, + live_decision_routes, + live_decision_turn_reads, +) +from loopx.control_plane.quota.effect_program import ( + ReceiptBoundReplayPhase, + SettlementIdentity, + SettlementResult, +) +from loopx.control_plane.quota.effective_action import EffectiveAction +from loopx.control_plane.quota.live_decision_contracts import ( + non_negative_int, + require_mapping, +) +from loopx.control_plane.quota.settlement import QuotaSettlementReadback + +_REPO_ROOT = Path(__file__).resolve().parents[2] +_LIVE_DECISION_MODULES = ( + "loopx/control_plane/quota/live_decision.py", + "loopx/control_plane/quota/live_decision_contracts.py", + "loopx/control_plane/quota/live_decision_routes.py", + "loopx/control_plane/quota/live_decision_turn_reads.py", + "loopx/control_plane/quota/live_decision_recovery.py", + "loopx/control_plane/quota/live_decision_replan.py", +) + + +def _write_registry( + tmp_path: Path, + *, + goal_id: str = "g1", + extra_goal: dict[str, object] | None = None, +) -> tuple[Path, Path]: + runtime_root = tmp_path / "runtime" + repo_root = tmp_path / "repo" + runtime_root.mkdir(parents=True, exist_ok=True) + repo_root.mkdir(parents=True, exist_ok=True) + state_file = repo_root / "TODOS.md" + state_file.write_text("# Todos\n", encoding="utf-8") + registry_path = tmp_path / "registry.json" + goal: dict[str, object] = { + "id": goal_id, + "goal_instance_id": "gi-1", + "repo": str(repo_root), + "state_file": "TODOS.md", + "enabled": True, + "registered_agents": ["agent-1"], + **(extra_goal or {}), + } + registry_path.write_text(json.dumps({"goals": [goal]}), encoding="utf-8") + return registry_path, runtime_root + + +def _make_readback( + *, + replay_phase: ReceiptBoundReplayPhase | None = None, + heartbeat_receipt: dict[str, object] | None = None, + admission_recorded_at: str | None = None, + admission_event_id: str | None = None, + semantic_replan_guard: dict[str, object] | None = None, + progress: dict[str, object] | None = None, +) -> QuotaSettlementReadback: + identity = SettlementIdentity( + goal_id="g1", + agent_id="agent-1", + todo_id=None, + turn_instance_id="turn-1", + replan_obligation_id="ob-1", + ) + none_result: SettlementResult[dict[str, object]] = SettlementResult(value=None) + return QuotaSettlementReadback( + identity=SettlementResult.pure(identity), + writeback=none_result, + spend=none_result, + delivery=none_result, + settlement=none_result, + terminal_closeout=none_result, + terminal_settlement=none_result, + workspace_causality=None, + semantic_replan_guard=semantic_replan_guard, + writeback_run=None, + spend_run=None, + heartbeat_receipt=heartbeat_receipt, + writeback_event=None, + spend_event=None, + completion_event=None, + monitor_phase=None, + replay_phase=replay_phase, + progress=progress, + admission_recorded_at=admission_recorded_at, + admission_event_id=admission_event_id, + ) + + +def test_live_decision_module_layout_and_zero_any_budgets() -> None: + test_file = Path(__file__).resolve() + for rel_path in [*_LIVE_DECISION_MODULES, str(test_file.relative_to(_REPO_ROOT))]: + source = (_REPO_ROOT / rel_path).read_text(encoding="utf-8") + lines = source.splitlines() + tree = ast.parse(source, filename=rel_path) + any_nodes = [ + node.lineno + for node in ast.walk(tree) + if isinstance(node, ast.Name) and node.id == "Any" + ] + suppressions = [ + idx + for idx, line in enumerate(lines, start=1) + if ("type:" + " ignore") in line or ("no" + "qa") in line + ] + assert not any_nodes, f"{rel_path} still references Any at lines {any_nodes}" + assert not suppressions, f"{rel_path} has suppressions at {suppressions}" + if rel_path.endswith("live_decision.py"): + assert len(lines) <= 650, f"{rel_path} exceeded 650 LOC ({len(lines)})" + elif rel_path != str(test_file.relative_to(_REPO_ROOT)): + assert len(lines) < 750, f"{rel_path} exceeded 750 LOC ({len(lines)})" + + live_source = (_REPO_ROOT / "loopx/control_plane/quota/live_decision.py").read_text( + encoding="utf-8" + ) + assert live_source.count("work_lane_contract_v1") == 1 + for helper_rel in _LIVE_DECISION_MODULES[1:]: + helper_src = (_REPO_ROOT / helper_rel).read_text(encoding="utf-8") + assert "work_lane_contract_v1" not in helper_src + + +def test_live_decision_exports_and_monkeypatch_seams() -> None: + expected = ( + "EffectiveAction", + "HostObservationResolver", + "BoundedResearchFrontierProjector", + "ProgressReviewContextReader", + "QuotaSettlementReadback", + "registered_agent_ids_for_goal", + "read_heartbeat_settlement", + "apply_unsettled_host_turn_recovery_if_required", + "apply_receipt_bound_wait_recovery", + "attach_settlement_progress", + "bind_scheduler_followup_cli_routes", + "bind_action_selection_cli_routes", + "_apply_pending_capability_intent_precedence", + "_build_remote_authority_wait_decision", + "_receipt_bound_guard_fields", + "_historical_ack_writeback_required", + "_prepared_execution_needs_readback", + "_original_replan_settlement_required", + "_project_receipt_replan_settlement", + "_apply_retained_action_selection_reentry", + "_fresh_read_covers_all_pending_material", + "_turn_start_required_reads", + "_project_turn_start_required_reads", + "_fresh_operator_inbox_observation_count", + "_fresh_operator_inbox_read_required", + "_bind_followup_turn_argument", + "_scheduler_fallback_launcher", + "_receipt_bound_recovery_todo", + "_project_unbound_work_fields", + "_restore_replan_obligation_without_foreign_acks", + "build_live_quota_should_run_decision", + ) + for name in expected: + assert hasattr(live_decision, name), f"Missing exported symbol {name}" + + +def test_remote_authority_path_honors_monkeypatched_seams( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + registry_path, runtime_root = _write_registry( + tmp_path, + extra_goal={ + "control_plane": { + "coordination": { + "remote_authority_gateway": { + "enabled": True, + "base_url": "https://example.invalid", + } + } + } + }, + ) + calls: list[str] = [] + + def fake_read_settlement( + runtime: Path, + *, + goal_id: str, + agent_id: str | None, + todo_id: str | None, + turn_instance_id: str | None, + replan_obligation_id: str | None = None, + registry_path: Path | None = None, + goal_ref: object = None, + tolerate_malformed_history: bool = False, + ) -> QuotaSettlementReadback | None: + calls.append(f"read:{turn_instance_id}") + return None + + def fake_recovery( + payload: dict[str, object], + **kwargs: object, + ) -> bool: + calls.append(f"recover:{kwargs.get('current_turn_instance_id')}") + payload.update( + decision="wait", + state="waiting", + effective_action=EffectiveAction.UNSETTLED_HOST_TURN_RECOVERY.value, + ) + return True + + monkeypatch.setattr(live_decision, "read_heartbeat_settlement", fake_read_settlement) + monkeypatch.setattr( + live_decision, + "apply_unsettled_host_turn_recovery_if_required", + fake_recovery, + ) + + from loopx.control_plane.coordination import remote_authority_gateway as gateway_mod + + monkeypatch.setattr( + gateway_mod, + "remote_authority_config", + lambda goal: {"enabled": True, "base_url": "https://example.invalid"}, + ) + monkeypatch.setattr( + "loopx.quota._build_quota_plan_for_goal", + lambda status_payload, goal_id: ({"ok": True, "items": []}, True), + ) + + payload = live_decision.build_live_quota_should_run_decision( + {"attention_queue": {"items": []}}, + goal_id="g1", + agent_id="agent-1", + available_capabilities=None, + include_scheduler_detail=False, + codex_app_current_rrule=None, + registry_path=registry_path, + runtime_root=runtime_root, + turn_instance_id="turn-remote-1", + ) + assert calls == ["read:turn-remote-1", "recover:turn-remote-1"] + assert payload["effective_action"] == EffectiveAction.UNSETTLED_HOST_TURN_RECOVERY.value + + +def test_route_binding_key_presence_and_single_resolve_pass( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + registry_path, runtime_root = _write_registry(tmp_path) + monkeypatch.setattr( + live_decision_routes, + "_scheduler_fallback_launcher", + lambda: ["loopx-apply-rrule"], + ) + + empty_payload: dict[str, object] = {"scheduler_hint": {"app_automation": {}}} + live_decision.bind_scheduler_followup_cli_routes( + empty_payload, registry_path=registry_path, runtime_root=runtime_root + ) + assert empty_payload == {"scheduler_hint": {"app_automation": {}}} + + resolve_calls: list[Path] = [] + original_resolve = Path.resolve + + def tracked_resolve(self: Path, strict: bool = False) -> Path: + resolve_calls.append(self) + return original_resolve(self, strict=strict) + + monkeypatch.setattr(Path, "resolve", tracked_resolve) + + ack_args: dict[str, object] = {} + fallback_args_absent: dict[str, object] = {"keep": True} + fallback_hint: dict[str, object] = { + "cli_args": [ + "loopx-apply-rrule", + "--turn-instance-id", + "${LOOPX_TURN_INSTANCE_ID}", + ], + "args": fallback_args_absent, + } + ack_hint: dict[str, object] = { + "cli_args": ["loopx", "quota", "scheduler-ack", "--execute"], + "args": ack_args, + } + failure_hint: dict[str, object] = { + "cli_args": ["loopx", "quota", "scheduler-failure", "--execute"], + } + payload: dict[str, object] = { + "scheduler_hint": { + "app_automation": { + "ack_hint": ack_hint, + "failure_hint": failure_hint, + "fallback_hint": fallback_hint, + } + } + } + live_decision.bind_scheduler_followup_cli_routes( + payload, + registry_path=registry_path, + runtime_root=runtime_root, + turn_instance_id="turn-42", + ) + assert len(resolve_calls) == 2 + assert ack_args["turn_instance_id"] == "turn-42" + assert "turn_instance_id" not in fallback_args_absent + + fallback_args_present: dict[str, object] = { + "turn_instance_id": "${LOOPX_TURN_INSTANCE_ID}" + } + payload2: dict[str, object] = { + "scheduler_hint": { + "app_automation": { + "fallback_hint": { + "cli_args": [ + "loopx-apply-rrule", + "--turn-instance-id", + "${LOOPX_TURN_INSTANCE_ID}", + ], + "args": fallback_args_present, + } + } + } + } + live_decision.bind_scheduler_followup_cli_routes( + payload2, + registry_path=registry_path, + runtime_root=runtime_root, + turn_instance_id="turn-99", + ) + assert fallback_args_present["turn_instance_id"] == "turn-99" + + resolve_calls.clear() + no_select: dict[str, object] = { + "interaction_contract": {"cli_channel": {"selection_required": False}} + } + live_decision.bind_action_selection_cli_routes( + no_select, registry_path=registry_path, runtime_root=runtime_root + ) + assert resolve_calls == [] + + bad_quotes_cmd: dict[str, object] = {"route_prefix": "loopx 'unclosed"} + live_decision.bind_action_selection_cli_routes( + { + "interaction_contract": { + "cli_channel": { + "selection_required": True, + "selection_command": bad_quotes_cmd, + } + } + }, + registry_path=registry_path, + runtime_root=runtime_root, + ) + assert bad_quotes_cmd["route_prefix"] == "loopx 'unclosed" + + selection_cmd: dict[str, object] = { + "route_prefix": "loopx quota should-run --format json --goal-id g1" + } + live_decision.bind_action_selection_cli_routes( + { + "interaction_contract": { + "cli_channel": { + "selection_required": True, + "selection_command": selection_cmd, + } + } + }, + registry_path=registry_path, + runtime_root=runtime_root, + ) + route_prefix = str(selection_cmd["route_prefix"]) + assert "--registry" in route_prefix and "--runtime-root" in route_prefix + assert route_prefix.index("--runtime-root") < route_prefix.index("--format") + + +def test_single_inbox_scan_in_live_decision_and_guard_empty_string_parity( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + registry_path, runtime_root = _write_registry(tmp_path) + scan_calls: list[object] = [] + real_scan = live_decision_turn_reads._fresh_operator_inbox_observation_count + + def counted_scan(dispatch: object) -> int: + scan_calls.append(dispatch) + assert isinstance(dispatch, dict) or dispatch is None + return real_scan(dispatch) + + monkeypatch.setattr( + live_decision, "_fresh_operator_inbox_observation_count", counted_scan + ) + captured: dict[str, object] = {} + + def fake_build_quota_should_run( + status_payload: dict[str, object], **kwargs: object + ) -> dict[str, object]: + captured["pre_scans"] = len(scan_calls) + captured["projector"] = kwargs["operator_inbox_urgency_projector"] + return { + "ok": True, + "decision": "run", + "should_run": True, + "state": "eligible", + "effective_action": EffectiveAction.NORMAL_RUN.value, + } + + monkeypatch.setattr(live_decision, "build_quota_should_run", fake_build_quota_should_run) + monkeypatch.setattr( + live_decision, + "build_interaction_contract", + lambda *args, **kwargs: { + "schema_version": "loopx_interaction_contract_v0", + "mode": "bounded_delivery", + "user_channel": {}, + "agent_channel": {}, + "cli_channel": {}, + }, + ) + + hook_dispatch = { + "required_reads": [{"kind": "operator_inbox", "hook_id": "h-inbox"}], + "results": [{"hook_id": "h-inbox", "agent_read_required": True, "observation_count": 2}], + } + live_decision.build_live_quota_should_run_decision( + {"attention_queue": {"items": []}}, + goal_id="g1", + agent_id="agent-1", + available_capabilities=None, + include_scheduler_detail=False, + codex_app_current_rrule=None, + registry_path=registry_path, + runtime_root=runtime_root, + turn_start_hook_dispatch=hook_dispatch, + operator_inbox_urgency_projector=lambda **_: { + "pending_count": 2, + "material_review_due": True, + }, + ) + assert captured["pre_scans"] == 1 + assert len(scan_calls) == 1 + projector = captured["projector"] + assert callable(projector) + assert projector() == {"pending_count": 2, "material_review_due": False} + + readback = _make_readback( + heartbeat_receipt={"recorded_at": "2026-11-26T00:00:00Z"}, + admission_recorded_at="", + admission_event_id="ev-1", + semantic_replan_guard={"scope": "turn_guard", "selected_obligation_id": "ob-1"}, + ) + fields = live_decision._receipt_bound_guard_fields(readback, "ob-1") + assert fields["receipt_bound_guard_recorded_at"] == "2026-11-26T00:00:00Z" + + +def test_malformed_inputs_fail_closed( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + registry_path, runtime_root = _write_registry(tmp_path) + + with pytest.raises(RuntimeError, match="malformed"): + live_decision._apply_retained_action_selection_reentry( + {"effective_action": "normal_run", "selected_todo": {"todo_id": "todo_alpha_1"}}, + retained_todo_id="todo_alpha_1", + available_capabilities=None, + scheduler_execution_context=None, + turn_instance_id=None, + registry_path=registry_path, + runtime_root=runtime_root, + reconcile_retained_fn=lambda **_: {"clear_fields": "bad"}, + ) + + with pytest.raises(RuntimeError, match="unsupported"): + live_decision._apply_retained_action_selection_reentry( + {"effective_action": "normal_run", "selected_todo": {"todo_id": "todo_alpha_1"}}, + retained_todo_id="todo_alpha_1", + available_capabilities=None, + scheduler_execution_context=None, + turn_instance_id=None, + registry_path=registry_path, + runtime_root=runtime_root, + reconcile_retained_fn=lambda **_: { + "disposition": "unexpected_disposition", + "clear_fields": [], + }, + ) + + with pytest.raises(TypeError): + live_decision._project_unbound_work_fields( + { + "interaction_contract": { + "cli_channel": { + "turn_binding_required": True, + "spend_policy": "blocked", + "next_cli_actions": "not-a-list", + } + } + } + ) + + monkeypatch.setattr( + "loopx.control_plane.todos.list_readback.list_goal_todos", + lambda **_: {"user_todos": [], "agent_todos": []}, + ) + with pytest.raises(TypeError): + live_decision_replan.attach_goal_todos_to_attention_queue( + {"attention_queue": "malformed"}, + registry_path=registry_path, + runtime_root=runtime_root, + goal_id="g1", + ) + with pytest.raises(TypeError): + live_decision_replan.attach_goal_todos_to_attention_queue( + {"attention_queue": {"items": ["not-a-mapping"]}}, + registry_path=registry_path, + runtime_root=runtime_root, + goal_id="g1", + ) + + with pytest.raises(TypeError): + require_mapping("not-a-mapping", label="packet") + + readback = _make_readback( + replay_phase=ReceiptBoundReplayPhase.SETTLEMENT_PENDING, + heartbeat_receipt={}, + progress={ + "schema_version": "quota_settlement_progress_v0", + "checkpoint_recovery_required": True, + }, + ) + with pytest.raises(TypeError): + live_decision._project_receipt_replan_settlement( + { + "should_run": True, + "effective_action": EffectiveAction.AUTONOMOUS_REPLAN_REQUIRED.value, + "replan_action_packet": { + "settlement_only": True, + "decision": ReceiptBoundReplayPhase.SETTLEMENT_PENDING.value, + "obligation_id": "ob-1", + }, + "autonomous_replan_obligation": "malformed", + }, + readback, + available_capabilities=None, + scheduler_execution_context=None, + turn_instance_id="turn-1", + registry_path=registry_path, + runtime_root=runtime_root, + goal_ref=None, + attach_settlement_fn=lambda *args, **kwargs: None, + ) + + assert non_negative_int(None) == 0 + assert non_negative_int(False) == 0 + assert non_negative_int(-3) == 0 + assert non_negative_int("7") == 7 + with pytest.raises(ValueError): + non_negative_int("not-an-int") + with pytest.raises(TypeError): + non_negative_int(object())