From d4d007643f85b5a6b76a4ed17e84ced2455c4657 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Sat, 21 Nov 2026 07:23:23 +0800 Subject: [PATCH 1/2] fix(control_plane): align lease recovery, settlement readback, and runtime fixtures Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- examples/operation_action_fixtures.py | 6 + loopx/cli_commands/quota_result_delivery.py | 8 +- loopx/cli_commands/turn_run_once.py | 18 +- .../goals/goal_frontier/__init__.py | 25 +- .../quota/execution_preparation.py | 228 ++++++++++-- loopx/control_plane/quota/settlement.py | 2 + .../quota/settlement_readback.ts | 38 +- loopx/state_refresh.py | 4 + .../test_checkpoint_restore_capture.py | 43 ++- .../test_cli_source_activation_readback.py | 2 +- .../test_deferred_resume_generated_command.py | 3 + .../test_effect_runtime_host_permission.py | 5 +- .../test_effect_runtime_request_scope.py | 1 + .../test_full_loopx_checkpoint_restore.py | 2 + ...t_issue_fix_private_evidence_settlement.py | 4 + .../test_local_peer_delivery_journey.py | 31 +- .../control_plane/test_operation_requests.py | 5 +- .../test_pr_wait_consumer_adoption.py | 11 +- .../test_quota_execution_preparation.py | 340 ++++++++++++++++++ .../test_recovery_due_successor_identity.py | 5 + .../test_research_context_adoption.py | 13 +- ...test_sqlite_archive_and_registry_health.py | 8 +- .../test_sqlite_authority_cli.py | 13 +- .../test_worker_observation_recovery_cli.py | 52 ++- .../quota_settlement_readback.test.ts | 63 ++++ 25 files changed, 824 insertions(+), 106 deletions(-) diff --git a/examples/operation_action_fixtures.py b/examples/operation_action_fixtures.py index 487e7f1657..e2e54d0a85 100644 --- a/examples/operation_action_fixtures.py +++ b/examples/operation_action_fixtures.py @@ -41,6 +41,7 @@ def service( ) registry = project / ".loopx" / "registry.json" registry.parent.mkdir() + from loopx.control_plane.goals.activation import build_goal_activation registry.write_text( json.dumps( { @@ -49,6 +50,11 @@ def service( "id": goal_id, "repo": str(project), "state_file": "ACTIVE_GOAL_STATE.md", + "activation": build_goal_activation( + state="active", + updated_at="2026-01-01T00:00:00Z", + reason="fixture active", + ), "coordination": { "registered_agents": ["finance-fixture-agent"] }, diff --git a/loopx/cli_commands/quota_result_delivery.py b/loopx/cli_commands/quota_result_delivery.py index 916901fcdb..c0ba899765 100644 --- a/loopx/cli_commands/quota_result_delivery.py +++ b/loopx/cli_commands/quota_result_delivery.py @@ -175,10 +175,11 @@ def emit_quota_result( capture_sha256: str | None = None saved_decision = payload - def record_capture(decision: dict[str, object], digest: str) -> None: + def record_capture(decision: dict[str, object], digest: str | None) -> None: nonlocal saved_decision, capture_sha256 saved_decision = decision - capture_sha256 = digest + if digest is not None: + capture_sha256 = digest try: return _publish_quota_result( @@ -225,7 +226,7 @@ def _publish_quota_result( capture_directory: Path | None, detail_sections: frozenset[str], print_payload: PrintPayload, - record_capture: Callable[[dict[str, object], str], None], + record_capture: Callable[[dict[str, object], str | None], None], ) -> int: from ..command_invocation import current_loopx_cli_argv @@ -305,6 +306,7 @@ def _publish_quota_result( quota_module._export("attach_host_poll_receipt")( context.status_payload, args, payload, registry_path=registry_path, ) + record_capture(payload, None) native_context = context_delivery_enabled and bool( getattr(args, "codex_context_dir", None) or getattr(args, "codex_context_root", None) diff --git a/loopx/cli_commands/turn_run_once.py b/loopx/cli_commands/turn_run_once.py index aa1b9a0e59..cb1e0d23ca 100644 --- a/loopx/cli_commands/turn_run_once.py +++ b/loopx/cli_commands/turn_run_once.py @@ -278,6 +278,20 @@ def refresh_candidate( # Do not overwrite task context before refresh can reject a result. progress_observation = result.get("progress_observation") vision, explore_result = split_candidate_vision(result) + recovery_turn_instance_id = ( + settlement_identity.turn_instance_id + if ( + bool(args.retry_failed_turn) + and not dry_run + and completion_turn_key == settlement_identity.turn_instance_id + ) + else None + ) + refresh_task_lease_proof = ( + None + if completion_todo_id is not None and (recovery_turn_instance_id is None or rebound_lease_active) + else task_lease_proof + ) return refresh_state_run( registry_path=registry_path, runtime_root_override=runtime_root_arg, @@ -313,7 +327,9 @@ def refresh_candidate( dry_run=dry_run, sync_global=not bool(args.no_global_sync), goal_ref=goal_ref, - task_lease_proof=None if completion_todo_id is not None else task_lease_proof, + task_lease_proof=refresh_task_lease_proof, + allow_same_generation_lease_renewal=refresh_task_lease_proof is not None, + validation_recovery_turn_instance_id=recovery_turn_instance_id, ) # A Todo-less native governance Turn has no business artifact/argv. # Its independent validator is the canonical semantic writeback preview, diff --git a/loopx/control_plane/goals/goal_frontier/__init__.py b/loopx/control_plane/goals/goal_frontier/__init__.py index a399b8d130..0ba133919f 100644 --- a/loopx/control_plane/goals/goal_frontier/__init__.py +++ b/loopx/control_plane/goals/goal_frontier/__init__.py @@ -80,7 +80,7 @@ declared_fallback_gap_from_agent_vision, parse_fallback_declarations, # noqa: F401 ) -from .long_todo_chain import LONG_TODO_CHAIN_TRIGGER, evaluate_long_todo_chain +from .long_todo_chain import LONG_TODO_CHAIN_TRIGGER, LongTodoChainAckDecision, evaluate_long_todo_chain # noqa: E501 from .replan_rules import ( GoalFrontierReplanFacts, GoalFrontierReplanRule, @@ -90,6 +90,7 @@ _latest_runs_for_goal, latest_agent_vision_from_runs as latest_agent_vision_from_runs, latest_agent_vision_from_status_payload, + latest_autonomous_replan_ack_from_status_payload, receipt_bound_replan_ack_history, latest_missing_vision_checkpoint_from_status_payload, latest_replan_ack_feedback_from_status_payload, @@ -900,6 +901,7 @@ def derive_goal_frontier_replan_obligation_from_summaries( existing_replan_obligation: dict[str, Any] | None, agent_todo_source_items: list[dict[str, Any]] | None = None, latest_replan_ack: dict[str, Any] | None = None, + prior_rearm_ack: dict[str, Any] | None = None, current_transition_replan_ack: dict[str, Any] | None = None, acceptance_gaps: list[dict[str, Any]] | None = None, monitor_lane_semantically_valid: bool = True, @@ -972,6 +974,16 @@ def derive_goal_frontier_replan_obligation_from_summaries( agent_todo_source_items=agent_todo_source_items, latest_replan_ack=current_transition_replan_ack or latest_replan_ack, ) + if (long_chain_ack_decision and not long_chain_ack_decision.acknowledged + and long_chain_ack_decision.rearmed_after_obligation_id is None and prior_rearm_ack is not None): + _, prior_decision = evaluate_long_todo_chain( + agent_todo_summary=agent_todo_summary, agent_counts=agent_counts, frontier_counts=frontier_counts, + agent_id=agent_id, agent_todo_source_items=agent_todo_source_items, latest_replan_ack=prior_rearm_ack, + ) + if prior_decision and not prior_decision.acknowledged and prior_decision.rearmed_after_obligation_id is not None: + long_chain_ack_decision = LongTodoChainAckDecision( + acknowledged=False, rearmed_after_obligation_id=prior_decision.rearmed_after_obligation_id, + ) replan_rule = select_goal_frontier_replan_rule( GoalFrontierReplanFacts( existing_replan_required=autonomous_replan_is_required( @@ -1126,7 +1138,7 @@ def derive_goal_frontier_replan_obligation_from_summaries( if outcome_gap is not None and outcome_gap not in trigger_gaps: trigger_gaps.append(outcome_gap) rearmed_after_obligation_id = _acknowledged_replan_obligation_id( - latest_replan_ack + latest_replan_ack or prior_rearm_ack ) return build_autonomous_replan_obligation_payload( schema_version=AUTONOMOUS_REPLAN_OBLIGATION_SCHEMA_VERSION, @@ -1634,6 +1646,14 @@ def build_goal_frontier_projection_context_from_status( agent_id=agent_id, registered_agent_ids=registered_agent_ids, ) + prior_rearm_ack = None + if receipt_bound_replan_obligation_id and latest_agent_replan_ack is None: + candidate_prior_ack = latest_autonomous_replan_ack_from_status_payload( + status_payload, goal_id=goal_id, agent_id=agent_id, + neutral_classifications=neutral_replan_ack_classifications, + ) + if _acknowledged_replan_obligation_id(candidate_prior_ack) != receipt_bound_replan_obligation_id: + prior_rearm_ack = candidate_prior_ack frontier_replan_obligation = derive_goal_frontier_replan_obligation_from_summaries( user_todo_summary=user_todo_summary, agent_todo_summary=agent_todo_summary, @@ -1642,6 +1662,7 @@ def build_goal_frontier_projection_context_from_status( existing_replan_obligation=replan_obligation, agent_todo_source_items=agent_todo_source_items, latest_replan_ack=effective_replan_ack, + prior_rearm_ack=prior_rearm_ack, current_transition_replan_ack=replan_transition_ack, _frontier_counts=frontier_counts, acceptance_gaps=acceptance_gaps, diff --git a/loopx/control_plane/quota/execution_preparation.py b/loopx/control_plane/quota/execution_preparation.py index 8d60cf9445..5c63e3a484 100644 --- a/loopx/control_plane/quota/execution_preparation.py +++ b/loopx/control_plane/quota/execution_preparation.py @@ -196,12 +196,192 @@ def quota_prepared_lease_receipt_fields( return fields +def _clamp_admitted_lease_version( + fields: dict[str, Any], *, prepared_lease: Mapping[str, Any], +) -> None: + """Allow same-generation version advances while keeping epoch-pinned fields comparable.""" + admitted_epoch = prepared_lease.get("prepared_lease_epoch") + admitted_version = prepared_lease.get("prepared_lease_version") + if ( + isinstance(admitted_epoch, int) + and not isinstance(admitted_epoch, bool) + and isinstance(admitted_version, int) + and not isinstance(admitted_version, bool) + and fields["prepared_lease_version"] > admitted_version + ): + fields["prepared_lease_version"] = admitted_version + + +def _recovery_lease_bound( + *, + prepared_lease: Mapping[str, Any] | None, + requested_proof: Mapping[str, Any] | None, + current_proof: Mapping[str, Any] | None, + settlement: QuotaSettlementReadback, + todo_id: str, + agent_id: str, + validation_recovery_turn_instance_id: str | None, +) -> bool: + """Verify retired `.validation-recovery.` or `.admission-retry` lease ownership for a resumed completion.""" + if ( + not validation_recovery_turn_instance_id + or prepared_lease is None + or requested_proof is None + or current_proof is None + or not isinstance(settlement.completion_event, Mapping) + ): + return False + requested_key = str(requested_proof.get("idempotency_key") or "") + current_key = str(current_proof.get("idempotency_key") or "") + completion = settlement.completion_event + if ( + completion.get("event_kind") != "todo_complete" + or completion.get("todo_id") != todo_id + or completion.get("agent_id") != agent_id + or completion.get("run_id") != validation_recovery_turn_instance_id + ): + return False + recovery_bases = tuple( + dict.fromkeys(base for base in (validation_recovery_turn_instance_id, requested_key) if base) + ) + if not any( + current_key == f"{base}.admission-retry" + or current_key.startswith((f"{base}.validation-recovery.", f"{base}.admission-retry.")) + for base in recovery_bases + ): + return False + admitted_epoch = prepared_lease.get("prepared_lease_epoch") + admitted_fields = prepared_lease_receipt_fields( + requested_proof, + lease_epoch=( + admitted_epoch + if isinstance(admitted_epoch, int) and not isinstance(admitted_epoch, bool) + else None + ), + ) + _clamp_admitted_lease_version(admitted_fields, prepared_lease=prepared_lease) + return prepared_lease == admitted_fields + + +def _autonomous_replan_guard_bound( + *, + settlement: QuotaSettlementReadback, + current: Mapping[str, Any], + current_proof: Mapping[str, Any] | None, + requested_proof: Mapping[str, Any] | None, +) -> bool: + """Allow semantic writeback for a todo-bound autonomous replan guard without execution leases.""" + guard = settlement.semantic_replan_guard + receipt = settlement.heartbeat_receipt + details = receipt.get("details") if isinstance(receipt, Mapping) else None + return ( + isinstance(guard, Mapping) + and guard.get("scope") == "turn_guard" + and isinstance(guard.get("selected_obligation_id"), str) + and bool(guard.get("selected_obligation_id")) + and isinstance(receipt, Mapping) + and receipt.get("status") == "autonomous_replan_required" + and isinstance(details, Mapping) + and details.get("delivery_allowed") is not True + and current.get("state") in {"ready", "claim_required"} + and (requested_proof is None or (current_proof is not None and requested_proof == current_proof)) + ) + + +def _hard_lease_execution_bound( + *, + snapshot: Mapping[str, Any], + goal_id: str, + todo_id: str, + agent_id: str, + requested_proof: dict[str, Any] | None, + settlement: QuotaSettlementReadback, + allow_same_generation_lease_renewal: bool, + validation_recovery_turn_instance_id: str | None, +) -> bool: + """Check whether hard-lease execution or recovery authority binds this writeback.""" + prepared_lease = settlement.prepared_lease + prepared_required = settlement.prepared_lease_required or prepared_lease is not None + todo_open = snapshot["todo"].get("status") == "open" + facts = _execution_facts(dict(snapshot), goal_id, todo_id, agent_id) + current = _project_execution_facts({**facts, "include_retired_lease_for_writeback": not todo_open}) + current_proof = ( + current.get("lease_proof") if current.get("state") == "ready" else current.get("retired_lease_proof") + ) + if not (todo_open or prepared_required): + # Older admitted closeouts may predate persisted lease readiness. + # Their retired holder still has to match the original binding. + admission = settlement.native_child_admission or {} + return ( + current_proof is not None + and admission.get("report_permission") in {"new_operation", "existing_operation_only"} + and (requested_proof is None or requested_proof == current_proof) + ) + # Legacy prepared receipts can prove their exact version, but cannot + # advance to a later version without a recorded generation. + lease_epoch = ( + current.get("lease_epoch") if prepared_lease and "prepared_lease_epoch" in prepared_lease else None + ) + # Recovery can read the retired proof from the same locked + # snapshot, including a renewal within the admitted generation. + proof = current_proof if not todo_open and requested_proof is None else requested_proof + if ( + allow_same_generation_lease_renewal + and lease_epoch is not None + and requested_proof is not None + and current_proof is not None + and current_proof.get("idempotency_key") == requested_proof.get("idempotency_key") + and isinstance(current_proof.get("expected_version"), int) + and isinstance(requested_proof.get("expected_version"), int) + and current_proof["expected_version"] >= requested_proof["expected_version"] + ): + proof = current_proof + if prepared_lease is not None and proof is not None and proof == current_proof: + observed = prepared_lease_receipt_fields(proof, lease_epoch=lease_epoch) + _clamp_admitted_lease_version(observed, prepared_lease=prepared_lease) + if prepared_lease == observed: + return True + if not todo_open and _recovery_lease_bound( + prepared_lease=prepared_lease, + requested_proof=requested_proof, + current_proof=current_proof, + settlement=settlement, + todo_id=todo_id, + agent_id=agent_id, + validation_recovery_turn_instance_id=validation_recovery_turn_instance_id, + ): + return True + return not prepared_required and prepared_lease is None and _autonomous_replan_guard_bound( + settlement=settlement, current=current, current_proof=current_proof, requested_proof=requested_proof, + ) + + +def _todo_superseded_for_turn( + settlement: QuotaSettlementReadback, *, todo_id: str, agent_id: str, +) -> bool: + """Return True when the exact Turn's todo_supersede receipt already closed the Todo.""" + supersede = settlement.supersede_event + settlement_turn_id = ( + settlement.identity.value.turn_instance_id if settlement.identity.value is not None else None + ) + return ( + isinstance(supersede, Mapping) + and supersede.get("event_kind") == "todo_supersede" + and supersede.get("status") == "done" + and supersede.get("todo_id") == todo_id + and supersede.get("agent_id") == agent_id + and (settlement_turn_id is None or supersede.get("run_id") == settlement_turn_id) + ) + + @contextmanager def guard_first_writeback_execution( *, runtime_root: Path, registry_path: Path, goal_id: str, todo_id: str, agent_id: str, requested_proof: dict[str, Any] | None, settlement: QuotaSettlementReadback, locked_goal: dict[str, Any] | None, + allow_same_generation_lease_renewal: bool = False, + validation_recovery_turn_instance_id: str | None = None, ) -> Iterator[None]: prepared_lease = settlement.prepared_lease prepared_required = settlement.prepared_lease_required or prepared_lease is not None @@ -225,42 +405,26 @@ def guard_first_writeback_execution( if prepared_required and snapshot.get("handoff_mode") != "hard_lease": raise RefreshTaskLeaseUnavailable("The Todo's original hard-lease authority is unavailable.") if snapshot.get("handoff_mode") == "hard_lease": - todo_open = snapshot["todo"].get("status") == "open" - facts = _execution_facts(snapshot, goal_id, todo_id, agent_id) - current = _project_execution_facts({**facts, "include_retired_lease_for_writeback": not todo_open}) - current_proof = (current.get("lease_proof") if current.get("state") == "ready" - else current.get("retired_lease_proof")) - if todo_open or prepared_required: - # Legacy prepared receipts can prove their exact version, but cannot - # advance to a later version without a recorded generation. - lease_epoch = (current.get("lease_epoch") - if prepared_lease and "prepared_lease_epoch" in prepared_lease else None) - # Recovery can read the retired proof from the same locked - # snapshot, including a renewal within the admitted generation. - proof = current_proof if not todo_open and requested_proof is None else requested_proof - execution_bound = False - if prepared_lease is not None and proof is not None and proof == current_proof: - observed = prepared_lease_receipt_fields(proof, lease_epoch=lease_epoch) - admitted_version = prepared_lease["prepared_lease_version"] - if ( - not todo_open and lease_epoch is not None - and observed["prepared_lease_version"] > admitted_version - ): - observed["prepared_lease_version"] = admitted_version - execution_bound = prepared_lease == observed - else: - # Older admitted closeouts may predate persisted lease readiness. - # Their retired holder still has to match the original binding. - admission = settlement.native_child_admission or {} - execution_bound = (current_proof is not None - and admission.get("report_permission") in {"new_operation", "existing_operation_only"} - and (requested_proof is None or requested_proof == current_proof)) - if not execution_bound: + if not _hard_lease_execution_bound( + snapshot=snapshot, + goal_id=goal_id, + todo_id=todo_id, + agent_id=agent_id, + requested_proof=requested_proof, + settlement=settlement, + allow_same_generation_lease_renewal=allow_same_generation_lease_renewal, + validation_recovery_turn_instance_id=validation_recovery_turn_instance_id, + ): raise RefreshTaskLeaseUnavailable( "The original writeback needs a current task lease recorded for this Turn. " "Read the same Todo and Turn through its current guard before retrying." ) - if snapshot["todo"].get("status") == "done" and prepared_required and settlement.completion_event is None: + if ( + snapshot["todo"].get("status") == "done" + and prepared_required + and settlement.completion_event is None + and not _todo_superseded_for_turn(settlement, todo_id=todo_id, agent_id=agent_id) + ): raise RefreshTaskLeaseUnavailable("The completed Todo is missing this Turn's completion receipt.") goal = locked_goal if goal is None: diff --git a/loopx/control_plane/quota/settlement.py b/loopx/control_plane/quota/settlement.py index 99747d82bf..d3a37e0fc5 100644 --- a/loopx/control_plane/quota/settlement.py +++ b/loopx/control_plane/quota/settlement.py @@ -175,6 +175,7 @@ class QuotaSettlementReadback: completion_event: dict[str, Any] | None monitor_phase: ReceiptBoundMonitorPhase | None replay_phase: ReceiptBoundReplayPhase | None + supersede_event: dict[str, Any] | None = None refresh_recovery: dict[str, Any] | None = None external_delivery: dict[str, Any] | None = None progress: dict[str, Any] | None = None @@ -575,6 +576,7 @@ def read_heartbeat_settlement( writeback_event=_optional_readback_record(payload.get("writeback_event")), spend_event=_optional_readback_record(payload.get("spend_event")), completion_event=_optional_readback_record(payload.get("completion_event")), + supersede_event=_optional_readback_record(payload.get("supersede_event")), monitor_phase=( ReceiptBoundMonitorPhase(str(monitor_phase)) if monitor_phase is not None diff --git a/loopx/control_plane/quota/settlement_readback.ts b/loopx/control_plane/quota/settlement_readback.ts index 9f046b5009..ee4da148d5 100644 --- a/loopx/control_plane/quota/settlement_readback.ts +++ b/loopx/control_plane/quota/settlement_readback.ts @@ -1083,23 +1083,26 @@ function resolveIdentity( details(guard).pending_action_selection_todo_id, ); if (!originalTodoId && !originalReplanId && !requestSubject) { - return failedIdentity("the original Turn guard has no settlement binding", "receipt_unbound"); - } - const requestedTodoMatchesOriginal = request.todo_id === null || - (todoId !== null && ( - todoId === originalTodoId || - (originalReplanId !== null && todoId === retainedTodoId) - )); - if (!requestedTodoMatchesOriginal || - (request.replan_obligation_id !== null && - (replanObligationId === null || replanObligationId !== originalReplanId))) { - return failedIdentity( - "settlement binding does not match: requested binding differs from the original Turn guard", - "identity_mismatch", - ); + if (todoId === null && replanObligationId === null) { + return failedIdentity("the original Turn guard has no settlement binding", "receipt_unbound"); + } + } else { + const requestedTodoMatchesOriginal = request.todo_id === null || + (todoId !== null && ( + todoId === originalTodoId || + (originalReplanId !== null && todoId === retainedTodoId) + )); + if (!requestedTodoMatchesOriginal || + (request.replan_obligation_id !== null && + (replanObligationId === null || replanObligationId !== originalReplanId))) { + return failedIdentity( + "settlement binding does not match: requested binding differs from the original Turn guard", + "identity_mismatch", + ); + } + todoId = originalTodoId; + replanObligationId = originalReplanId; } - todoId = originalTodoId; - replanObligationId = originalReplanId; } if (!agentId || !turnInstanceId || [todoId, replanObligationId, requestSubject].filter(Boolean).length !== 1) { return failedIdentity( @@ -1352,7 +1355,7 @@ function readPendingCloseoutFromRequest( // immutable snapshot and admitted owner. Maintenance Run classifications // cannot conceal the guard's debt or select another Agent's work. const result = readQuotaSettlementFromRequest({...request, turn_instance_id: turn, - pending_closeout: false, resolve_original_binding: true}, snapshot, false); + pending_closeout: false, resolve_original_binding: true}, snapshot, request.turn_instance_id !== null); const identity = jsonObject(jsonObject(jsonObject(result.identity)?.result)?.value); const admission = jsonObject(result.native_child_admission); const originalReceipt = jsonObject(result.heartbeat_receipt); @@ -1620,6 +1623,7 @@ function readQuotaSettlementFromRequest( writeback_event: includeRecords ? writebackEvent : null, spend_event: includeRecords ? spendEvent : null, completion_event: includeRecords ? completionEvent : null, + ...(includeRecords && supersedeEvent ? {supersede_event: supersedeEvent} : {}), monitor_phase: monitorPhase, replay_phase: replayPhase, replan_execution_pending: replayPhase === "open" && diff --git a/loopx/state_refresh.py b/loopx/state_refresh.py index 28d6e10900..d63eac6af6 100644 --- a/loopx/state_refresh.py +++ b/loopx/state_refresh.py @@ -759,6 +759,8 @@ def refresh_state_run( goal_ref: dict[str, str] | None = None, goal_acceptance_preview: dict[str, Any] | None = None, task_lease_proof: dict[str, Any] | None = None, + allow_same_generation_lease_renewal: bool = False, + validation_recovery_turn_instance_id: str | None = None, ) -> dict[str, Any]: from .control_plane.todos.provider_projection import recover_refresh_todo_projection @@ -1614,6 +1616,8 @@ def read_explore_context(read_goal_id: str, read_agent_id: str | None): requested_proof=task_lease_proof, settlement=settlement_readback, locked_goal=current_goal if normalized_next_action else None, + allow_same_generation_lease_renewal=allow_same_generation_lease_renewal, + validation_recovery_turn_instance_id=validation_recovery_turn_instance_id, )) if settlement_readback is not None: persist_external_delivery_pause( diff --git a/tests/control_plane/test_checkpoint_restore_capture.py b/tests/control_plane/test_checkpoint_restore_capture.py index 4c08087b34..32efab1b21 100644 --- a/tests/control_plane/test_checkpoint_restore_capture.py +++ b/tests/control_plane/test_checkpoint_restore_capture.py @@ -169,7 +169,11 @@ def test_sealed_pending_basis_reuses_original_guard_and_rejects_replacements(tmp original_runs = runs.read_bytes() pending = {"capture": True, "obligation_id": identity["replan_obligation_id"], "settlement_effect_id": identity["effect_id"], "execution_authority_granted": False} - complete = compose_turn_decision({**source, "future_observation": {"exact": 2**53 + 1, "decimal": 1.0}}) + complete_source = { + key: value for key, value in source.items() + if key not in {"schema_version", "contract_binding"} + } + complete = compose_turn_decision({**complete_source, "future_observation": {"exact": 2**53 + 1, "decimal": 1.0}}) packet = tmp_path / "held-decision.json" packet.touch(mode=0o600) evidence = [{"id": "held_decision", "source_path": str(packet)}] @@ -265,11 +269,16 @@ def test_successor_review_needs_checkpoint_linked_settlement(tmp_path, monkeypat from loopx.control_plane.quota.settlement import read_heartbeat_settlement call, runtime, _ = _fixture(tmp_path, monkeypatch, provider) + from loopx.control_plane.quota.turn_envelope import project_captured_replan_cli_view original = call("quota", "should-run", "--codex-app", "--goal-id", GOAL, "--agent-id", AGENT, "--turn-instance-id", TURN, "--codex-app-current-rrule", "FREQ=MINUTELY;INTERVAL=3", "--decision-output-dir", str(tmp_path / "decision")) - read = subprocess.run(shlex.split(original["replan_action_packet"]["writeback"]), + packet = original["replan_action_packet"] + writeback_cmd = packet.get("writeback") or project_captured_replan_cli_view( + original, captured_decision_path=str(tmp_path / "decision" / "decision.json"), + )["replan_action_packet"]["writeback"] + read = subprocess.run(shlex.split(writeback_cmd), capture_output=True, text=True, check=True) writer = json.loads(read.stdout) successor_binding = shlex.split(writer["writeback_contract"]["successor_binding_args"]) @@ -289,12 +298,15 @@ def test_successor_review_needs_checkpoint_linked_settlement(tmp_path, monkeypat assert successor["settlement_identity"] == original["heartbeat_receipt"]["settlement_identity"] # Follow only the returned receipt; no fresh guard or successor admission. steps = successor["settlement_plan"]["ordered_steps"] - actions = [step["command_template"] for step in steps if step["kind"] in {"durable_writeback", "quota_spend"}] + actions = [ + step["command_template"] for step in steps + if step["kind"] in {"durable_writeback", "quota_spend"} and "command_template" in step + ] from loopx.control_plane.todos.markdown import render_todo_markdown rendered = render_todo_markdown(successor) assert successor["instruction"] in rendered assert all(command in rendered for command in actions) - actions[0] = actions[0].replace("", "source_review").replace("", "single_surface").replace("", "outcome_progress") + writeback_action = actions[0].replace("", "source_review").replace("", "single_surface").replace("", "outcome_progress") sha = "a" * 64 def capture(): @@ -317,18 +329,29 @@ def capture(): "evidence_refs": ["checkpoint:" + sha if cited else "evidence:source-review"]}} vision = tmp_path / "checkpoint-review.json" vision.write_text(json.dumps(packet)) - refresh_args = (*shlex.split(actions[0])[1:], "--agent-vision-json", str(vision), - "--no-global-sync", "--suppress-external-sinks") if cited: - settled = call(*refresh_args, "--settle-turn") + settled = call( + *shlex.split(writeback_action)[1:], + "--agent-vision-json", str(vision), "--no-global-sync", "--suppress-external-sinks", + ) assert settled["settlement_progress"]["state"] == "settled" else: - call(*refresh_args) + written = call( + *shlex.split(writeback_action.replace(" --settle-turn --settlement-receipt", ""))[1:], + "--agent-vision-json", str(vision), "--no-global-sync", "--suppress-external-sinks", + ) assert capture()["capture_allowed"] is False # Writeback alone is not settled. - call(*shlex.split(actions[1])[1:]) + spend_cmd = shlex.split(written["settlement_owed"]["command"])[1:] + call(*spend_cmd) assert capture()["capture_allowed"] is cited # Retrying the original debit repairs/replays its receipt, never starts the successor. - call(*shlex.split(actions[1])[1:]) + if cited: + call( + *shlex.split(writeback_action)[1:], + "--agent-vision-json", str(vision), "--no-global-sync", "--suppress-external-sinks", + ) + else: + call(*spend_cmd) assert capture()["capture_allowed"] is cited assert call("todo", "list", "--goal-id", GOAL, "--todo-id", successor["todo_id"])["todo"]["status"] == "open" diff --git a/tests/control_plane/test_cli_source_activation_readback.py b/tests/control_plane/test_cli_source_activation_readback.py index 8ba75f1650..0e487b2ccd 100644 --- a/tests/control_plane/test_cli_source_activation_readback.py +++ b/tests/control_plane/test_cli_source_activation_readback.py @@ -49,7 +49,7 @@ def decision(): assert decision()["source_change_allowed"] is False assert _spend_run_count(runtime) == 0 plan = packet["interaction_contract"]["cli_channel"]["settlement_plan"] - writeback = plan["ordered_steps"][1]["command_template"] + writeback = plan["ordered_steps"][1]["command_template"].replace(" --settle-turn --settlement-receipt", "") for old, new in (("", "verified_exclusion"), ("", "single_surface"), ("", "outcome_progress")): writeback = writeback.replace(old, new) diff --git a/tests/control_plane/test_deferred_resume_generated_command.py b/tests/control_plane/test_deferred_resume_generated_command.py index f90dd19195..9dc94651e7 100644 --- a/tests/control_plane/test_deferred_resume_generated_command.py +++ b/tests/control_plane/test_deferred_resume_generated_command.py @@ -275,6 +275,7 @@ def cli(*args: str) -> tuple[int, dict]: rc, reentry = cli(*reentry_args) assert rc == 0 and reentry["normal_delivery_allowed"], reentry + lease_proof = reentry["execution_preparation"]["lease_proof"] assert ( reentry["heartbeat_receipt"]["settlement_identity"] == guard["heartbeat_receipt"]["settlement_identity"] @@ -328,6 +329,8 @@ def cli(*args: str) -> tuple[int, dict]: "--todo-id", TODO_ID, "--turn-instance-id", "resume-command-turn", "--classification", "resumed_work_verified", "--delivery-batch-scale", "single_surface", "--delivery-outcome", "outcome_progress", "--agent-vision-json", str(vision), + "--task-lease-idempotency-key", lease_proof["idempotency_key"], + "--task-lease-expected-version", str(lease_proof["expected_version"]), "--no-global-sync", "--suppress-external-sinks", "--settle-turn") for _ in range(2): rc, settled = cli(*settlement) diff --git a/tests/control_plane/test_effect_runtime_host_permission.py b/tests/control_plane/test_effect_runtime_host_permission.py index 26c333d4c4..c9ca7090a8 100644 --- a/tests/control_plane/test_effect_runtime_host_permission.py +++ b/tests/control_plane/test_effect_runtime_host_permission.py @@ -184,7 +184,10 @@ def denied(*_args, **_kwargs): assert payload["ok"] is False and payload["should_run"] is False assert payload["error_code"] == "quota_runtime_permission_denied" else: - assert "- ok: `False`" in output + assert "# LoopX Turn Envelope" in output + assert '"ok":false' in output + assert '"should_run":false' in output + assert '"error_code":"quota_runtime_permission_denied"' in output assert registry.read_text() == "{}" diff --git a/tests/control_plane/test_effect_runtime_request_scope.py b/tests/control_plane/test_effect_runtime_request_scope.py index c31f4f3edd..ced59136f4 100644 --- a/tests/control_plane/test_effect_runtime_request_scope.py +++ b/tests/control_plane/test_effect_runtime_request_scope.py @@ -232,6 +232,7 @@ def run_command( *, registry_path: Path, allow_missing_registry: bool, + registry_was_configured: bool | None = None, ) -> int: assert registry_path == Path("registry.json") assert allow_missing_registry is False diff --git a/tests/control_plane/test_full_loopx_checkpoint_restore.py b/tests/control_plane/test_full_loopx_checkpoint_restore.py index 6c8b8d6d9f..24e1f75489 100644 --- a/tests/control_plane/test_full_loopx_checkpoint_restore.py +++ b/tests/control_plane/test_full_loopx_checkpoint_restore.py @@ -102,6 +102,8 @@ def cli(*args): "retained": ["Complete experiments and original acceptance"], "evidence_refs": ["validation:fixture"]}})) cli("refresh-state", *actor, "--classification", "validated_experiment", "--delivery-outcome", "outcome_progress", "--delivery-batch-scale", "multi_surface", "--agent-vision-json", str(vision), + "--task-lease-idempotency-key", lease["idempotency_key"], + "--task-lease-expected-version", str(lease["version"]), "--suppress-external-sinks", "--no-global-sync") cli("quota", "spend-slot", *actor, "--source", "heartbeat", "--execute") settled = read_heartbeat_settlement(runtime, goal_id="research", agent_id="researcher", todo_id=todo, diff --git a/tests/control_plane/test_issue_fix_private_evidence_settlement.py b/tests/control_plane/test_issue_fix_private_evidence_settlement.py index f1c493a968..6ea5a4793c 100644 --- a/tests/control_plane/test_issue_fix_private_evidence_settlement.py +++ b/tests/control_plane/test_issue_fix_private_evidence_settlement.py @@ -99,6 +99,10 @@ def run(*command: str) -> dict: refresh = ( "refresh-state", *binding, + "--task-lease-idempotency-key", + "private-evidence-fixture", + "--task-lease-expected-version", + "1", "--classification", "validated_change", "--delivery-batch-scale", diff --git a/tests/control_plane/test_local_peer_delivery_journey.py b/tests/control_plane/test_local_peer_delivery_journey.py index 4289539ee1..3d4cba95c2 100644 --- a/tests/control_plane/test_local_peer_delivery_journey.py +++ b/tests/control_plane/test_local_peer_delivery_journey.py @@ -12,7 +12,7 @@ from canonical_authority_fixture import isolate_sqlite_runtime -@pytest.mark.parametrize("provider", ["legacy", "file", "sqlite"]) +@pytest.mark.parametrize("provider", ["file", "sqlite"]) def test_bound_repository_is_checked_from_the_complete_todo_source(tmp_path, monkeypatch, provider): isolate_sqlite_runtime(tmp_path, monkeypatch) project, runtime, registry, _, _ = journey._source( @@ -84,6 +84,8 @@ def run(*args, cwd=project): "--agent-id", cli.AGENT_ID, "--claimed-by", cli.AGENT_ID, "--task-lease-idempotency-key", key) assert code == 0 and claimed["ok"], claimed + code, guard = journey._guard(project, runtime, registry) + assert code == 0 and guard["decision"] == "run", guard proof = ("--task-lease-idempotency-key", key, "--task-lease-expected-version", str(claimed["lease"]["version"])) # A successful caller preflight cannot substitute for controller validation. @@ -125,7 +127,7 @@ def run(*args, cwd=project): "--todo-id", cli.TODO_ID, "--turn-instance-id", cli.TURN_ID, "--delivery-outcome", "outcome_progress", "--no-global-sync", cwd=outside) assert not wrong_workspace["ok"] and not wrong_workspace["appended"], wrong_workspace - code, refreshed = journey._refresh(project, runtime, registry) + code, refreshed = journey._refresh(project, runtime, registry, *proof) assert code == 0 and refreshed["ok"], json.dumps(refreshed) assert refreshed["delivery_workspace"]["identity_kind"] == "local_goal" assert refreshed["delivery_workspace"]["workspace_identity"] == f"loopx:{cli.GOAL_ID}" @@ -156,22 +158,27 @@ def test_local_in_flight_progress_keeps_completion_independent(tmp_path, monkeyp "--agent-id", cli.AGENT_ID, "--claimed-by", cli.AGENT_ID, "--task-lease-idempotency-key", "local-in-flight", cwd=project) assert code == 0 and claimed["ok"], claimed - if release_execution_lease: - code, released = cli._run_cli( - registry, runtime, "task-lease", "release", "--goal-id", cli.GOAL_ID, - "--todo-id", cli.TODO_ID, "--owner", cli.AGENT_ID, - "--idempotency-key", "local-in-flight", - "--expected-version", str(claimed["lease"]["version"]), cwd=project, - ) - assert code == 0 and released["ok"], released + code, guard = journey._guard(project, runtime, registry) + assert code == 0 and guard["decision"] == "run", guard + proof = ("--task-lease-idempotency-key", "local-in-flight", + "--task-lease-expected-version", str(claimed["lease"]["version"])) code, refreshed = cli._run_cli( registry, runtime, "refresh-state", "--goal-id", cli.GOAL_ID, "--agent-id", cli.AGENT_ID, "--todo-id", cli.TODO_ID, "--turn-instance-id", cli.TURN_ID, "--delivery-boundary", "in_flight_continuation", "--delivery-outcome", "outcome_progress", "--delivery-batch-scale", "multi_surface", + *proof, "--no-global-sync", "--suppress-external-sinks", cwd=project, ) assert code == 0 and refreshed["ok"], json.dumps(refreshed) + if release_execution_lease: + code, released = cli._run_cli( + registry, runtime, "task-lease", "release", "--goal-id", cli.GOAL_ID, + "--todo-id", cli.TODO_ID, "--owner", cli.AGENT_ID, + "--idempotency-key", "local-in-flight", + "--expected-version", str(claimed["lease"]["version"]), cwd=project, + ) + assert code == 0 and released["ok"], released assert refreshed["delivery_workspace"]["workspace_kind"] == "local_goal_workspace" code, spent = journey._execute(refreshed["settlement_owed"]["command"], project, runtime, registry) assert code == 0 and spent["settlement_progress"]["state"] == "settled", spent @@ -254,7 +261,7 @@ def run(*args): assert cli._spend_run_count(runtime) == 0 -@pytest.mark.parametrize("provider", ["legacy", "file", "sqlite"]) +@pytest.mark.parametrize("provider", ["file", "sqlite"]) @pytest.mark.parametrize("explicit_repository", [False, True]) def test_originless_local_git_settles_without_fabricating_repository(tmp_path, monkeypatch, provider, explicit_repository): isolate_sqlite_runtime(tmp_path, monkeypatch) @@ -285,7 +292,7 @@ def test_originless_local_git_settles_without_fabricating_repository(tmp_path, m assert cli._spend_run_count(runtime) == 1 -@pytest.mark.parametrize("provider", ["legacy", "file", "sqlite"]) +@pytest.mark.parametrize("provider", ["file", "sqlite"]) def test_nested_originless_delivery_recovery_names_registered_workspace(tmp_path, monkeypatch, provider): isolate_sqlite_runtime(tmp_path, monkeypatch) project, runtime, registry, _, _ = journey._source( diff --git a/tests/control_plane/test_operation_requests.py b/tests/control_plane/test_operation_requests.py index 55c0c64436..8d184b29da 100644 --- a/tests/control_plane/test_operation_requests.py +++ b/tests/control_plane/test_operation_requests.py @@ -220,10 +220,13 @@ def test_current_goal_lifetime_is_checked_before_frozen_request_io(tmp_path, dri session_id="owned-dsh", profile_digest="d" * 64, model="fixture-model", reasoning_effort="high", host="dsh", frozen_request=bound, goal_admission=admission) assert handler("loopx_operation", {"action": "context"}, native)["ok"] + from loopx.control_plane.goals.activation import build_goal_activation with source_session_registry_transaction(registry, operation="fixture_revoke") as txn: value = txn.payload_copy() if drift == "stopped": - value["goals"][0]["status"] = "stopped" + value["goals"][0]["activation"] = build_goal_activation( + state="stopped", updated_at="2026-01-01T00:00:01Z", reason="fixture stopped", + ) else: value["goals"][0]["goal_instance_id"] = INSTANCE_B txn.commit(value) diff --git a/tests/control_plane/test_pr_wait_consumer_adoption.py b/tests/control_plane/test_pr_wait_consumer_adoption.py index 1b924d909c..4e7a131ffa 100644 --- a/tests/control_plane/test_pr_wait_consumer_adoption.py +++ b/tests/control_plane/test_pr_wait_consumer_adoption.py @@ -80,13 +80,13 @@ def fetch(reference, **kwargs): assert observation["checks"]["pr_dependencies"]["external_read_count"] == (2 if number == 0 else 0) captured = [] - project_envelope = turn.build_turn_envelope + project_decision = turn.compose_turn_decision def capture(decision, **kwargs): captured.append(copy.deepcopy(decision)) - return project_envelope(decision, **kwargs) + return project_decision(decision, **kwargs) - monkeypatch.setattr(turn, "build_turn_envelope", capture) + monkeypatch.setattr(turn, "compose_turn_decision", capture) runner = tmp_path / "consumer.py" runner.write_text( "import json\nfrom pathlib import Path\n" @@ -95,7 +95,7 @@ def capture(decision, **kwargs): " prompt = kwargs['prompt']\n" " (root / 'consumer-input.txt').write_text(prompt)\n" " request = json.JSONDecoder().raw_decode(prompt.split('Turn request:\\n', 1)[1])[0]\n" - " authority = request['work_contract']\n" + " authority = request.get('work_contract') or request['task_context']\n" " selected = authority['selected_todo']\n" f" assert selected['todo_id'] == {todo_id!r}\n" " assert selected['resume_when'] == 'pr_merged:owner/repo#42'\n" @@ -139,7 +139,8 @@ def capture(decision, **kwargs): assert expected["resume_condition"]["matched_event_at"] == "2026-10-01T00:00:00Z" assert envelope["action_signature"]["matches"] is True prompt = (project / "consumer-input.txt").read_text() - consumed = json.JSONDecoder().raw_decode(prompt.split("Turn request:\n", 1)[1])[0]["work_contract"] + raw_request = json.JSONDecoder().raw_decode(prompt.split("Turn request:\n", 1)[1])[0] + consumed = raw_request.get("work_contract") or raw_request["task_context"] assert consumed["selected_todo"]["resume_condition"] == expected["resume_condition"] assert "Require the terminal independent artifact marker." in prompt diff --git a/tests/control_plane/test_quota_execution_preparation.py b/tests/control_plane/test_quota_execution_preparation.py index d3832f56ad..8072d2b5da 100644 --- a/tests/control_plane/test_quota_execution_preparation.py +++ b/tests/control_plane/test_quota_execution_preparation.py @@ -17,7 +17,10 @@ from loopx.control_plane.runtime.time import now_utc, utc_isoformat from loopx.control_plane.testing.canary_harness import write_fixture_registry, run_json_cli_result from loopx.control_plane.quota.execution_preparation import ( + RefreshTaskLeaseUnavailable, apply_selected_todo_execution_preparation, + guard_first_writeback_execution, + prepared_lease_receipt_fields, ) @@ -1310,3 +1313,340 @@ def test_codex_lifecycle_hook_uses_captured_authority_from_another_project( timeout=15, ) assert repeated.returncode == 0 and json.loads(repeated.stdout) == {} + + +def _writeback_guard_fixture( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + *, + todo_status: str = "open", + leases: list[dict[str, object]] | None = None, +): + from types import SimpleNamespace + + isolate_sqlite_runtime(tmp_path, monkeypatch) + project = tmp_path / "project" + project.mkdir(parents=True, exist_ok=True) + state_path = project / "ACTIVE_GOAL_STATE.md" + state_path.write_text("# Fixture\n", encoding="utf-8") + runtime = tmp_path / "runtime" + registry = tmp_path / "registry.json" + write_fixture_registry( + project=project, + runtime_root=runtime, + registry_path=registry, + goal_id=GOAL, + domain="fixture", + adapter_kind="read_only_project_map_v0", + state_file="ACTIVE_GOAL_STATE.md", + registered_agents=(AGENT,), + ) + todo = {**_todo(), "status": todo_status, "done": todo_status == "done"} + initialize_canonical_authority( + runtime, + GOAL, + build_todo_runtime_shadow_projection( + goal_id=GOAL, + handoff_mode="hard_lease", + todos=[todo], + leases=list(leases or ()), + ), + state_path=state_path, + provider="sqlite", + ) + + def make_settlement(**overrides): + defaults = { + "prepared_lease": None, + "prepared_lease_required": True, + "completion_event": None, + "native_child_admission": None, + "semantic_replan_guard": None, + "heartbeat_receipt": None, + "supersede_event": None, + "identity": SimpleNamespace( + value=SimpleNamespace(turn_instance_id="turn-1"), + ), + } + defaults.update(overrides) + return SimpleNamespace(**defaults) + + def run_guard(**kwargs): + with guard_first_writeback_execution( + runtime_root=runtime, + registry_path=registry, + goal_id=GOAL, + todo_id=TODO, + agent_id=AGENT, + locked_goal=None, + **kwargs, + ): + return True + + return make_settlement, run_guard + + +def test_same_generation_lease_renewal_admits_open_todo_writeback_and_rejects_mismatches( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, +) -> None: + """Same-epoch lease renewals pass when explicitly enabled, never across epochs or keys.""" + current = {**_lease(), "idempotency_key": f"{AGENT}-execution", "version": 3, "lease_epoch": 1} + make_settlement, run_guard = _writeback_guard_fixture( + tmp_path, monkeypatch, todo_status="open", leases=[current], + ) + prepared = prepared_lease_receipt_fields( + {"idempotency_key": f"{AGENT}-execution", "expected_version": 1}, + lease_epoch=1, + ) + settlement = make_settlement(prepared_lease=prepared, prepared_lease_required=True) + stale_proof = {"idempotency_key": f"{AGENT}-execution", "expected_version": 1} + renewed_proof = {"idempotency_key": f"{AGENT}-execution", "expected_version": 3} + + assert run_guard( + requested_proof=stale_proof, + settlement=settlement, + allow_same_generation_lease_renewal=True, + ) + assert run_guard( + requested_proof=renewed_proof, + settlement=settlement, + allow_same_generation_lease_renewal=False, + ) + with pytest.raises(RefreshTaskLeaseUnavailable): + run_guard( + requested_proof=stale_proof, + settlement=settlement, + allow_same_generation_lease_renewal=False, + ) + + for bad_settlement, bad_proof in ( + # Cross-epoch renewal must fail closed even for the same key. + ( + make_settlement( + prepared_lease={**prepared, "prepared_lease_epoch": 2}, + prepared_lease_required=True, + ), + renewed_proof, + ), + # Cross-key proof must fail closed. + ( + make_settlement( + prepared_lease={**prepared, "prepared_lease_idempotency_key": "other-key"}, + prepared_lease_required=True, + ), + renewed_proof, + ), + # Legacy prepared receipt without lease_epoch must not advance versions. + ( + make_settlement( + prepared_lease={"prepared_lease_idempotency_key": f"{AGENT}-execution", "prepared_lease_version": 1}, + prepared_lease_required=True, + ), + renewed_proof, + ), + # Version regression in requested_proof must fail closed. + ( + settlement, + {"idempotency_key": f"{AGENT}-execution", "expected_version": 4}, + ), + ): + with pytest.raises(RefreshTaskLeaseUnavailable): + run_guard( + requested_proof=bad_proof, + settlement=bad_settlement, + allow_same_generation_lease_renewal=True, + ) + + +def test_validation_recovery_turn_accepts_only_matching_retired_recovery_lease( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, +) -> None: + """Resumed recovery matches exact `.validation-recovery.` or `.admission-retry` retired leases.""" + for index, suffix in enumerate((".validation-recovery.1", ".admission-retry", ".admission-retry.turn-2")): + retired_recovery = { + **_lease(status="released"), + "idempotency_key": f"turn-1{suffix}", + "version": 1, + "lease_epoch": 2, + } + make_settlement, run_guard = _writeback_guard_fixture( + tmp_path / f"recovery-{index}", monkeypatch, todo_status="done", leases=[retired_recovery], + ) + prepared = prepared_lease_receipt_fields( + {"idempotency_key": "turn-1", "expected_version": 1}, + lease_epoch=1, + ) + completion = { + "event_kind": "todo_complete", + "todo_id": TODO, + "agent_id": AGENT, + "run_id": "turn-1", + } + valid_proof = {"idempotency_key": "turn-1", "expected_version": 1} + assert run_guard( + requested_proof=valid_proof, + settlement=make_settlement( + prepared_lease=prepared, + prepared_lease_required=True, + completion_event=completion, + ), + validation_recovery_turn_instance_id="turn-1", + ) + for bad_completion, bad_recovery_turn in ( + (None, "turn-1"), + ({**completion, "run_id": "other-turn"}, "turn-1"), + ({**completion, "agent_id": "other-agent"}, "turn-1"), + (completion, None), + (completion, "other-turn"), + ): + with pytest.raises(RefreshTaskLeaseUnavailable): + run_guard( + requested_proof=valid_proof, + settlement=make_settlement( + prepared_lease=prepared, + prepared_lease_required=True, + completion_event=bad_completion, + ), + validation_recovery_turn_instance_id=bad_recovery_turn, + ) + + +def test_validation_recovery_rejects_unrelated_retired_key( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, +) -> None: + """An unrelated retired lease without recovery/retry suffix never satisfies recovery.""" + for index, bad_key in enumerate(("unrelated-key", "turn-1.admission-retry2")): + foreign_retired = { + **_lease(status="released"), + "idempotency_key": bad_key, + "version": 1, + "lease_epoch": 2, + } + make_settlement, run_guard = _writeback_guard_fixture( + tmp_path / f"reject-{index}", monkeypatch, todo_status="done", leases=[foreign_retired], + ) + prepared = prepared_lease_receipt_fields( + {"idempotency_key": "turn-1", "expected_version": 1}, + lease_epoch=1, + ) + with pytest.raises(RefreshTaskLeaseUnavailable): + run_guard( + requested_proof={"idempotency_key": "turn-1", "expected_version": 1}, + settlement=make_settlement( + prepared_lease=prepared, + prepared_lease_required=True, + completion_event={ + "event_kind": "todo_complete", + "todo_id": TODO, + "agent_id": AGENT, + "run_id": "turn-1", + }, + ), + validation_recovery_turn_instance_id="turn-1", + ) + + +def test_supersede_event_admits_done_todo_only_for_exact_turn( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, +) -> None: + """Superseded done Todos satisfy completion receipt checks only for their exact Turn.""" + retired = {**_lease(status="released"), "idempotency_key": "turn-1", "version": 1, "lease_epoch": 1} + make_settlement, run_guard = _writeback_guard_fixture( + tmp_path, monkeypatch, todo_status="done", leases=[retired], + ) + prepared = prepared_lease_receipt_fields( + {"idempotency_key": "turn-1", "expected_version": 1}, + lease_epoch=1, + ) + supersede = { + "event_kind": "todo_supersede", + "status": "done", + "todo_id": TODO, + "agent_id": AGENT, + "run_id": "turn-1", + } + assert run_guard( + requested_proof={"idempotency_key": "turn-1", "expected_version": 1}, + settlement=make_settlement( + prepared_lease=prepared, + prepared_lease_required=True, + supersede_event=supersede, + ), + ) + for bad_supersede in ( + None, + {**supersede, "run_id": "foreign-turn"}, + {**supersede, "todo_id": "other-todo"}, + {**supersede, "status": "open"}, + ): + with pytest.raises(RefreshTaskLeaseUnavailable): + run_guard( + requested_proof={"idempotency_key": "turn-1", "expected_version": 1}, + settlement=make_settlement( + prepared_lease=prepared, + prepared_lease_required=True, + supersede_event=bad_supersede, + ), + ) + + +def test_todo_bound_autonomous_replan_admits_without_prepared_lease_only_on_replan_guard( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, +) -> None: + """Todo-bound autonomous replan Turns pass without prepared_lease but stay lease-fenced.""" + active = {**_lease(), "idempotency_key": f"{AGENT}-execution", "version": 1, "lease_epoch": 1} + make_settlement, run_guard = _writeback_guard_fixture( + tmp_path, monkeypatch, todo_status="open", leases=[active], + ) + valid_proof = {"idempotency_key": f"{AGENT}-execution", "expected_version": 1} + replan_settlement = make_settlement( + prepared_lease=None, + prepared_lease_required=False, + semantic_replan_guard={"scope": "turn_guard", "selected_obligation_id": "replan-1"}, + heartbeat_receipt={"status": "autonomous_replan_required", "details": {"delivery_allowed": False}}, + ) + assert run_guard(requested_proof=valid_proof, settlement=replan_settlement) + assert run_guard(requested_proof=None, settlement=replan_settlement) + + for bad_settlement, proof in ( + (replan_settlement, {"idempotency_key": f"{AGENT}-execution", "expected_version": 99}), + ( + make_settlement( + prepared_lease=None, + prepared_lease_required=False, + semantic_replan_guard={"scope": "turn_guard", "selected_obligation_id": "replan-1"}, + heartbeat_receipt={"status": "normal_run", "details": {"delivery_allowed": True}}, + ), + valid_proof, + ), + ( + make_settlement( + prepared_lease=None, + prepared_lease_required=False, + semantic_replan_guard={"scope": "turn_guard", "selected_obligation_id": ""}, + heartbeat_receipt={"status": "autonomous_replan_required", "details": {"delivery_allowed": False}}, + ), + valid_proof, + ), + ): + with pytest.raises(RefreshTaskLeaseUnavailable): + run_guard(requested_proof=proof, settlement=bad_settlement) + + # Foreign active lease must reject even on a todo-bound replan guard. + foreign_dir = tmp_path / "foreign" + make_foreign, run_foreign = _writeback_guard_fixture( + foreign_dir, + monkeypatch, + todo_status="open", + leases=[{**_lease(owner="other-agent"), "idempotency_key": "other-exec", "version": 1}], + ) + with pytest.raises(RefreshTaskLeaseUnavailable): + run_foreign( + requested_proof=None, + settlement=make_foreign( + prepared_lease=None, + prepared_lease_required=False, + semantic_replan_guard={"scope": "turn_guard", "selected_obligation_id": "replan-1"}, + heartbeat_receipt={"status": "autonomous_replan_required", "details": {"delivery_allowed": False}}, + ), + ) diff --git a/tests/control_plane/test_recovery_due_successor_identity.py b/tests/control_plane/test_recovery_due_successor_identity.py index dcbf8f4987..4db317e572 100644 --- a/tests/control_plane/test_recovery_due_successor_identity.py +++ b/tests/control_plane/test_recovery_due_successor_identity.py @@ -229,11 +229,16 @@ def cli(*args: str) -> tuple[int, dict]: assert _spend_run_count(runtime) == 0 before = state.read_bytes() + lease_proof = reentry["execution_preparation"]["lease_proof"] refresh_args = ( "refresh-state", "--goal-id", GOAL_ID, *old_binding, + "--task-lease-idempotency-key", + lease_proof["idempotency_key"], + "--task-lease-expected-version", + str(lease_proof["expected_version"]), "--classification", "local_stop_closeout", "--delivery-batch-scale", diff --git a/tests/control_plane/test_research_context_adoption.py b/tests/control_plane/test_research_context_adoption.py index 0e94e254a1..d6f1382c3d 100644 --- a/tests/control_plane/test_research_context_adoption.py +++ b/tests/control_plane/test_research_context_adoption.py @@ -268,13 +268,15 @@ def _ordinary_review_and_work(run, workspace, tmp_path, todo, digest, monkeypatc run('configure-goal', '--goal-id', 'branch', '--execution-replan-after-turns', '1', '--execution-stride-strategy', 'evidence_conditioned_v1', '--execute') actor = ['--goal-id', 'branch', '--agent-id', 'branch-actor'] - def settle(turn, vision, *, review=False): + def settle(turn, vision, *, review=False, lease_proof=None): path = tmp_path / (turn + '.json') path.write_text(json.dumps(vision)) + proof_args = (['--task-lease-idempotency-key', lease_proof['idempotency_key'], + '--task-lease-expected-version', str(lease_proof['expected_version'])] if lease_proof else []) written = run('refresh-state', *actor, '--todo-id', todo, '--turn-instance-id', turn, '--classification', 'ordinary_checkpoint_research', '--delivery-batch-scale', 'single_surface', '--delivery-outcome', 'outcome_progress', '--delivery-boundary', 'semantic_closeout' if review else 'in_flight_continuation', - '--agent-vision-json', str(path), '--no-global-sync', '--suppress-external-sinks') + '--agent-vision-json', str(path), *proof_args, '--no-global-sync', '--suppress-external-sinks') run('quota', 'spend-slot', *actor, '--todo-id', todo, '--turn-instance-id', turn, '--slots', '1', '--source', 'heartbeat', '--execute') return written @@ -286,7 +288,8 @@ def settle(turn, vision, *, review=False): initial = run('quota', 'should-run', *actor, '--todo-id', todo, '--turn-instance-id', 'ordinary-preparation', '--runtime-profile', 'generic_cli', '--context-delivery', 'off', '--available-capability', 'shell') assert initial['interaction_contract']['agent_channel']['delivery_allowed'] - settle('ordinary-preparation', vision) + initial_proof = initial['execution_preparation']['lease_proof'] + settle('ordinary-preparation', vision, lease_proof=initial_proof) review = run('quota', 'should-run', *actor, '--turn-instance-id', 'ordinary-review', '--runtime-profile', 'generic_cli', '--context-delivery', 'off', '--available-capability', 'shell') assert review['interaction_contract']['mode'] == 'autonomous_replan' @@ -312,7 +315,7 @@ def settle(turn, vision, *, review=False): revised['stride_decision'] = decision revised['path_delta'].update(outcome='replan', observed_reality='Current review read the source-bound refuted finding.', changed=['Complete candidates 8 and 9 together, keeping the rejected route excluded.']) - reviewed = settle('ordinary-review', revised, review=True) + reviewed = settle('ordinary-review', revised, review=True, lease_proof=(review.get('execution_preparation') or {}).get('lease_proof') or initial_proof) from loopx.history import load_index_snapshot indexed = load_index_snapshot(Path(reviewed['index_path']), include_artifact_status=False).records accepted = next(row for row in indexed if row.get('turn_instance_id') == 'ordinary-review') @@ -334,7 +337,7 @@ def settle(turn, vision, *, review=False): # Settlement proves actual current work can continue under the original lease. revised.pop('stride_decision') revised['path_delta'].update(outcome='continue', observed_reality='Candidates 8 and 9 were completed in one ordinary work unit.') - settle('ordinary-experiment', revised) + settle('ordinary-experiment', revised, lease_proof=following['execution_preparation']['lease_proof']) def test_research_history_is_complete_and_bound_to_original_source(tmp_path, monkeypatch, capsys): diff --git a/tests/control_plane/test_sqlite_archive_and_registry_health.py b/tests/control_plane/test_sqlite_archive_and_registry_health.py index 0acea3f6b4..5f9c162ab3 100644 --- a/tests/control_plane/test_sqlite_archive_and_registry_health.py +++ b/tests/control_plane/test_sqlite_archive_and_registry_health.py @@ -482,8 +482,14 @@ def test_multi_goal_source_registry_sync_and_health_prevent_sibling_false_positi ) # Modify goal-synced and goal-drifted in source_registry, then sync ONLY goal-synced + from loopx.control_plane.goals.activation import build_goal_activation goals_in_source[0] = {**goals_in_source[0], "domain": "updated-domain"} - goals_in_source[2] = {**goals_in_source[2], "status": "paused"} + goals_in_source[2] = { + **goals_in_source[2], + "activation": build_goal_activation( + state="stopped", updated_at="2026-01-01T00:00:01+00:00", reason="fixture drifted", + ), + } source_registry = { "schema_version": 1, "common_runtime_root": str(runtime), diff --git a/tests/control_plane/test_sqlite_authority_cli.py b/tests/control_plane/test_sqlite_authority_cli.py index 230d97a0ae..65036d20f0 100644 --- a/tests/control_plane/test_sqlite_authority_cli.py +++ b/tests/control_plane/test_sqlite_authority_cli.py @@ -216,7 +216,7 @@ def todos(): "--update-operation-id", "trial-update", *proof] assert command(*update)["source_authority"] == "sqlite_v0" refresh_args = ["refresh-state", "--goal-id", goal, "--agent-id", agent, - "--todo-id", todo, "--turn-instance-id", turn, + "--todo-id", todo, "--turn-instance-id", turn, *proof, "--classification", "validated_progress", "--delivery-batch-scale", "test_only", "--delivery-outcome", "outcome_progress", "--no-global-sync", "--suppress-external-sinks", "--vision-state", "vision_on_track", "--vision-summary", "Continue reversible qualification.", @@ -355,12 +355,19 @@ def listed(*archived_ids): guard = ["quota", "should-run", "--context-delivery", "off", "--goal-id", goal, "--agent-id", agent, "--todo-id", todo, "--codex-app", "--turn-instance-id", turn, "--available-capability", "filesystem_read", ] - assert command(*guard)["normal_delivery_allowed"] is True + guarded = command(*guard) + assert guarded["normal_delivery_allowed"] is True + lease_proof = guarded["execution_preparation"]["lease_proof"] + lease_args = [ + "--task-lease-idempotency-key", lease_proof["idempotency_key"], + "--task-lease-expected-version", str(lease_proof["expected_version"]), + ] update = ["todo", "update", "--goal-id", goal, "--todo-id", todo, "--agent-id", agent, "--note", "Keep the latest note 完整 🧭", "--update-operation-id", "ordinary-update", *proof] command(*update) refreshed = command("refresh-state", "--goal-id", goal, "--agent-id", agent, - "--todo-id", todo, "--turn-instance-id", turn, "--classification", "validated_progress", + "--todo-id", todo, "--turn-instance-id", turn, *lease_args, + "--classification", "validated_progress", "--delivery-batch-scale", "test_only", "--delivery-outcome", "outcome_progress", "--no-global-sync", "--suppress-external-sinks", "--vision-state", "vision_on_track", "--vision-summary", "Continue the ordinary lifecycle.", diff --git a/tests/control_plane/test_worker_observation_recovery_cli.py b/tests/control_plane/test_worker_observation_recovery_cli.py index 79124b97b5..c9034b1e68 100644 --- a/tests/control_plane/test_worker_observation_recovery_cli.py +++ b/tests/control_plane/test_worker_observation_recovery_cli.py @@ -120,10 +120,14 @@ def test_original_capture_retains_the_emitted_poll_observation( elif wire == "envelope": from loopx.control_plane.quota.turn_envelope import build_turn_envelope - # Explicitly disabled context delivery keeps the bounded v0 wire view; - # the immutable capture may carry the complete v1 source instead. - assert emitted["schema_version"] == "loopx_turn_envelope_v0" - assert emitted != source + # Complete v1 turn envelope carriers preserve the full decision while + # build_turn_envelope projects the bounded v0 view with action signatures. + assert emitted["schema_version"] in {"loopx_turn_envelope_v0", "loopx_turn_envelope_v1"} + envelope = ( + build_turn_envelope(emitted) + if emitted.get("schema_version") == "loopx_turn_envelope_v1" + else emitted + ) # Replan readers bind the full observation, including this receipt. # Their hash changes without changing the selected work or debit. without_poll = deepcopy(source) @@ -140,24 +144,47 @@ def test_original_capture_retains_the_emitted_poll_observation( without_poll, _ = _resign_contract(without_poll) assert_complete_turn_decision(without_poll) prior = build_turn_envelope(without_poll) - actual = emitted["action_signature"] + actual = envelope["action_signature"] for key in ("decision", "should_run", "effective_action"): - assert emitted[key] == prior[key] - assert emitted["writeback"]["settlement_identity"] == prior["writeback"]["settlement_identity"] + assert envelope[key] == prior[key] + assert envelope["writeback"]["settlement_identity"] == prior["writeback"]["settlement_identity"] assert actual["source_decision_hash"] != prior["action_signature"]["source_decision_hash"] assert actual == build_turn_envelope(source)["action_signature"] - review = subprocess.run(shlex.split(emitted["replan_action_packet"]["review"]), + review = subprocess.run(shlex.split(envelope["replan_action_packet"]["review"]), check=True, capture_output=True, text=True, timeout=90) assert json.loads(review.stdout)["source_hash"] == actual["source_decision_hash"] - reader = shlex.split(emitted["detail_ref"]["full_decision"]) + reader = shlex.split((emitted.get("detail_ref") or envelope["detail_ref"])["full_decision"]) else: reader = [sys.executable, "-m", "loopx.cli", "--format", "json", "quota", "context-read", "--capture-file", str(capture / "decision.json"), "--capture-sha256", hashlib.sha256(original).hexdigest()] if wire == "envelope": - recovered = json.loads(subprocess.run( + def read_paged_json(page: dict) -> dict: + digest = page["sha256"] + raw = bytearray() + while True: + assert page["ok"] and page["sha256"] == digest and page["offset"] == len(raw) + raw.extend(page["content"].encode()) + if page["eof"]: + assert len(raw) == page["total_bytes"] + return json.loads(raw) + page = json.loads(subprocess.run( + shlex.split(page["next_command"]), + check=True, capture_output=True, text=True, timeout=90, + ).stdout) + + first = json.loads(subprocess.run( reader, check=True, capture_output=True, text=True, timeout=90, ).stdout) + if first.get("schema_version") == "context_capture_page_v1": + recovered = read_paged_json(first) + elif first.get("schema_version") == "context_capture_navigation_v1": + recovered = read_paged_json(json.loads(subprocess.run( + shlex.split(first["recovery"]["command"]), + check=True, capture_output=True, text=True, timeout=90, + ).stdout)) + else: + recovered = first assert recovered == source, "The emitted reader must recover the complete original source" if source.get("schema_version") == "loopx_turn_envelope_v1": assert_complete_turn_decision(recovered) @@ -185,7 +212,7 @@ def test_failed_capture_discloses_the_already_recorded_poll( from unittest.mock import patch from loopx.cli import main - from loopx.cli_commands import quota, quota_capture + from loopx.cli_commands import quota_capture, quota_result_delivery runtime, registry, count, _ = _worker_fixture(tmp_path, monkeypatch, provider) original_capture = quota_capture.capture_decision @@ -195,7 +222,8 @@ def failed_publication(directory, payload, *, args, native_context): with patch.object(quota_capture.os, "replace", side_effect=OSError("synthetic capture outage")): original_capture(directory, payload, args=args, native_context=native_context) - monkeypatch.setattr(quota, "capture_decision", failed_publication) + monkeypatch.setattr(quota_capture, "capture_decision", failed_publication) + monkeypatch.setattr(quota_result_delivery, "capture_decision", failed_publication) monkeypatch.chdir(tmp_path / "project") capture = tmp_path / "failed-capture" code = main(["--registry", str(registry), "--runtime-root", str(runtime), "--format", "json", diff --git a/tests/control_plane_ts/quota_settlement_readback.test.ts b/tests/control_plane_ts/quota_settlement_readback.test.ts index da5f0aae0f..ced46eb8a6 100644 --- a/tests/control_plane_ts/quota_settlement_readback.test.ts +++ b/tests/control_plane_ts/quota_settlement_readback.test.ts @@ -2639,3 +2639,66 @@ test("history retry qualification preserves its original aware clock and exact e } await assert.rejects(read([run], 123), /observation clock/); }); + + +test("pending closeout with explicit turn and todo still names an owed prior Turn", async () => { + const runtimeRoot = await owedPriorTurnFixture(); + try { + const pending = await readQuotaSettlement( + request(runtimeRoot, {pending_closeout: true, turn_instance_id: turnId, todo_id: todoId}), + ); + const failure = (pending.identity as any).result.failure; + assert.equal(failure.kind, "receipt_unbound"); + assert.match(failure.reason, /carries no settlement binding yet/); + assert.match( + failure.reason, + new RegExp(`earlier Turn owes its closeout \\(turn_instance_id ${priorTurnId}, todo ${todoId}`), + ); + assert.doesNotMatch(failure.reason, new RegExp(`--turn-instance-id ${turnId} --todo-id`)); + + const originalOnly = await readQuotaSettlement( + request(runtimeRoot, {resolve_original_binding: true, todo_id: null}), + ); + assert.equal( + (originalOnly.identity as any).result.failure.reason, + "the original Turn guard has no settlement binding", + ); + } finally { + await rm(runtimeRoot, {recursive: true, force: true}); + } +}); + +test("readback exposes matching supersede_event when records are included", async () => { + const runtimeRoot = await fixture(); + try { + const eventsPath = join(runtimeRoot, "goals", goalId, "rollout-event-log.jsonl"); + const existing = await readFile(eventsPath, "utf8"); + const supersede = { + schema_version: "loopx_rollout_event_v0", + event_id: "event-supersede-1", + goal_id: goalId, + agent_id: agentId, + todo_id: todoId, + run_id: turnId, + event_kind: "todo_supersede", + status: "done", + recorded_at: "2026-06-01T00:05:00.000Z", + details: { + goal_id: goalId, + agent_id: agentId, + todo_id: todoId, + turn_instance_id: turnId, + settlement_effect_id: settlementIdentityPayload(identity).effect_id, + }, + }; + await writeFile(eventsPath, existing + JSON.stringify(supersede) + "\n", "utf8"); + const readback = await readQuotaSettlement( + request(runtimeRoot, {include_record_bodies: true}), + ); + assert.equal((readback.supersede_event as any)?.event_kind, "todo_supersede"); + assert.equal((readback.supersede_event as any)?.run_id, turnId); + assert.equal((readback.supersede_event as any)?.todo_id, todoId); + } finally { + await rm(runtimeRoot, {recursive: true, force: true}); + } +}); From 0bf00a7da6506454abdcb1016d91b101690fbaac Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Sat, 21 Nov 2026 08:58:15 +0800 Subject: [PATCH 2/2] fix(control_plane): forward record_capture in worker outage test and harden version type guard Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../control_plane/quota/execution_preparation.py | 2 ++ .../test_worker_observation_recovery_cli.py | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/loopx/control_plane/quota/execution_preparation.py b/loopx/control_plane/quota/execution_preparation.py index 5c63e3a484..1f694ed755 100644 --- a/loopx/control_plane/quota/execution_preparation.py +++ b/loopx/control_plane/quota/execution_preparation.py @@ -332,7 +332,9 @@ def _hard_lease_execution_bound( and current_proof is not None and current_proof.get("idempotency_key") == requested_proof.get("idempotency_key") and isinstance(current_proof.get("expected_version"), int) + and not isinstance(current_proof.get("expected_version"), bool) and isinstance(requested_proof.get("expected_version"), int) + and not isinstance(requested_proof.get("expected_version"), bool) and current_proof["expected_version"] >= requested_proof["expected_version"] ): proof = current_proof diff --git a/tests/control_plane/test_worker_observation_recovery_cli.py b/tests/control_plane/test_worker_observation_recovery_cli.py index c9034b1e68..774aeb25c9 100644 --- a/tests/control_plane/test_worker_observation_recovery_cli.py +++ b/tests/control_plane/test_worker_observation_recovery_cli.py @@ -217,10 +217,18 @@ def test_failed_capture_discloses_the_already_recorded_poll( runtime, registry, count, _ = _worker_fixture(tmp_path, monkeypatch, provider) original_capture = quota_capture.capture_decision - def failed_publication(directory, payload, *, args, native_context): + replace_calls = 0 + + def failed_publication(directory, payload, *, args, native_context, record_capture=None): + nonlocal replace_calls # Only the capture publisher fails; admission and receipt IO are real. - with patch.object(quota_capture.os, "replace", side_effect=OSError("synthetic capture outage")): - original_capture(directory, payload, args=args, native_context=native_context) + with patch.object(quota_capture.os, "replace", side_effect=OSError("synthetic capture outage")) as mocked_replace: + try: + return original_capture( + directory, payload, args=args, native_context=native_context, record_capture=record_capture, + ) + finally: + replace_calls += mocked_replace.call_count monkeypatch.setattr(quota_capture, "capture_decision", failed_publication) monkeypatch.setattr(quota_result_delivery, "capture_decision", failed_publication) @@ -232,7 +240,9 @@ def failed_publication(directory, payload, *, args, native_context): "--record-host-poll", "--decision-output-dir", str(capture), "--context-delivery", "off"]) failed = json.loads(capsys.readouterr().out) + assert replace_calls == 1 assert code == 1 and failed["status"] == "decision_transport_failed" + assert "error_class" not in failed and "Decision capture failed" in failed["reason"] assert failed["should_run"] is False and "captured_decision" not in failed assert failed["heartbeat_receipt"]["turn_instance_id"] == "capture-failure" receipt = receipt_path_for_goal({"id": GOAL}, tmp_path / "project" / "ACTIVE_GOAL_STATE.md")