From 2f4c4e92c33f592188bfed50a1276f784f65f5bf Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 20 Nov 2026 04:53:22 +0800 Subject: [PATCH 1/6] Fence C1 first writebacks with the prepared task lease Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../project_lifecycle_refresh_state.py | 10 + loopx/cli_commands/turn_run_once.py | 3 + .../quota/execution_preparation.py | 240 +++++++++++++----- .../control_plane/quota/heartbeat_receipt.py | 20 +- .../quota/heartbeat_receipt_identity.ts | 35 ++- .../quota/refresh_external_delivery.py | 26 +- loopx/control_plane/quota/settlement.py | 4 + loopx/control_plane/quota/settlement_cli.py | 8 + loopx/control_plane/quota/settlement_plan.ts | 14 +- .../quota/settlement_readback.ts | 40 ++- .../codex_execution_preparation.py | 1 + .../work_items/execution_preparation.ts | 9 +- .../work_items/interaction_contract.py | 2 +- loopx/state_refresh.py | 21 +- ...test_quota_authority_settlement_journey.py | 90 ++++++- .../execution_preparation.test.ts | 18 ++ 16 files changed, 459 insertions(+), 82 deletions(-) diff --git a/loopx/cli_commands/project_lifecycle_refresh_state.py b/loopx/cli_commands/project_lifecycle_refresh_state.py index 8a5be1decb..9a7e5135b8 100644 --- a/loopx/cli_commands/project_lifecycle_refresh_state.py +++ b/loopx/cli_commands/project_lifecycle_refresh_state.py @@ -205,6 +205,8 @@ def register_refresh_state_command( ), ) refresh_state_parser.add_argument("--goal-instance-id", help=argparse.SUPPRESS) + refresh_state_parser.add_argument("--task-lease-idempotency-key", help=argparse.SUPPRESS) + refresh_state_parser.add_argument("--task-lease-expected-version", type=int, help=argparse.SUPPRESS) refresh_state_parser.add_argument("--completion-todo-id", help=argparse.SUPPRESS) refresh_state_parser.add_argument("--completion-turn-key", help=argparse.SUPPRESS) refresh_state_parser.add_argument( @@ -459,7 +461,14 @@ def handle_refresh_state_command( outcome_update = None outcome_update_result = None goal_acceptance_preview = None + task_lease_proof = None try: + key = getattr(args, "task_lease_idempotency_key", None) + version = getattr(args, "task_lease_expected_version", None) + if key is not None or version is not None: + if key is None or not key or version is None or version < 1: + raise ValueError("First writeback requires the task lease key and positive version together.") + task_lease_proof = {"idempotency_key": key, "expected_version": version} inputs = parse_refresh_state_inputs(args, fmt=fmt, registry_path=registry_path) outcome_update = inputs.outcome_update agent_vision_packet = inputs.agent_vision_packet @@ -546,6 +555,7 @@ def handle_refresh_state_command( dry_run=bool(args.dry_run), sync_global=not bool(args.no_global_sync), goal_ref=goal_ref, + task_lease_proof=task_lease_proof, **({"goal_acceptance_preview": goal_acceptance_preview} if goal_acceptance_preview is not None else {}), ) diff --git a/loopx/cli_commands/turn_run_once.py b/loopx/cli_commands/turn_run_once.py index bd75698823..f243972b84 100644 --- a/loopx/cli_commands/turn_run_once.py +++ b/loopx/cli_commands/turn_run_once.py @@ -116,6 +116,7 @@ def execute_turn_run_once( ) selected_todo = execution_context["selected_todo"] expected_todo_text_sha256 = execution_context["sha256"] + task_lease_proof = execution_context.get("task_lease_proof") or None transaction = ( payload.get("transaction") if isinstance(payload.get("transaction"), Mapping) @@ -288,6 +289,7 @@ def refresh_candidate( dry_run=dry_run, sync_global=not bool(args.no_global_sync), goal_ref=goal_ref, + task_lease_proof=task_lease_proof, ) # A Todo-less native governance Turn has no business artifact/argv. # Its independent validator is the canonical semantic writeback preview, @@ -762,6 +764,7 @@ def on_managed_start_admitted() -> None: else None ), goal_ref=goal_ref, + task_lease_proof=task_lease_proof, ) managed_cadence = managed_cadence_start( diff --git a/loopx/control_plane/quota/execution_preparation.py b/loopx/control_plane/quota/execution_preparation.py index 9382711dc6..17d532ad2b 100644 --- a/loopx/control_plane/quota/execution_preparation.py +++ b/loopx/control_plane/quota/execution_preparation.py @@ -1,20 +1,33 @@ from __future__ import annotations +import hashlib import shlex -from collections.abc import Mapping +from collections.abc import Iterator, Mapping +from contextlib import ExitStack, contextmanager from pathlib import Path -from typing import Any +from typing import TYPE_CHECKING, Any +from ...file_lock import exclusive_cross_runtime_file_lock, exclusive_mutation_file_lock from ..coordination.local_authority import ( + local_authority_is_promoted, LOCAL_AUTHORITY_SOURCES, LOCAL_COORDINATION_TODO_LIST_TIMEOUT_SECONDS, ) +from ..coordination.shadow_management import ( + require_shadow_primary_write_allowed, + shadow_maintenance_lock_target, +) from ..effect_runtime import effect_runtime_result +from ..goals.activation import goal_is_stopped from ..todos.contract import normalize_todo_claimed_by, normalize_todo_id from ..work_items.action_selection_contract import ( action_portfolio_requires_explicit_selection, ) from ..work_items.task_lease import lease_is_active +from ..work_items.recommendation_source_io import load_recommendation_source_goal + +if TYPE_CHECKING: + from .settlement import QuotaSettlementReadback def _unavailable_facts(*, goal_id: str, todo_id: str, agent_id: str) -> dict[str, Any]: @@ -48,73 +61,184 @@ def project_selected_todo_execution_preparation( if not todo_id or not safe_agent_id or runtime_root is None: return None try: - response = effect_runtime_result( - "todo.read", - {"schema_version": "todo_read_request_v0", "kind": "execution_preparation_source", - "source": {"runtime_root": str(Path(runtime_root).expanduser().resolve(strict=False)), - "goal_id": goal_id}, - "todo_id": todo_id}, - timeout=LOCAL_COORDINATION_TODO_LIST_TIMEOUT_SECONDS, - ) - snapshot = ( - response.get("execution_preparation_source") if isinstance(response, dict) - and response.get("schema_version") == "todo_read_result_v0" else None - ) - if ( - not isinstance(snapshot, dict) or snapshot.get("goal_id") != goal_id - or snapshot.get("todo_id") != todo_id or snapshot.get("source_authority") not in LOCAL_AUTHORITY_SOURCES - or snapshot.get("decision_read_from_provider") is not True - or snapshot.get("legacy_fallback_used") is not False - or not isinstance(snapshot.get("provider_revision"), str) or not snapshot["provider_revision"] - or not isinstance(snapshot.get("todo"), dict) or snapshot["todo"].get("todo_id") != todo_id - or not isinstance(snapshot.get("leases"), list) - ): - facts = _unavailable_facts( - goal_id=goal_id, todo_id=todo_id, agent_id=safe_agent_id, - ) - else: - todo = snapshot["todo"] - leases = [] - for lease in snapshot["leases"]: - if not isinstance(lease, dict) or lease.get("todo_id") != todo_id: - raise ValueError("execution preparation returned a lease for a different Todo") - status = str(lease.get("status") or "unknown").strip().lower() - leases.append({ - "status": status if status in {"active", "released"} else "unknown", - "active": lease_is_active(dict(lease)), - **({"owner": lease["owner"]} if normalize_todo_claimed_by(lease.get("owner")) else {}), - "lease_proof": {"idempotency_key": lease.get("idempotency_key"), - "expected_version": lease.get("version")}, - }) - facts = { - "schema_version": "execution_preparation_facts_v0", - "goal_id": goal_id, - "agent_id": safe_agent_id, - "handoff_mode": str(snapshot.get("handoff_mode") or "hard_lease"), - "authority_available": True, - "selected_todo": { - "todo_id": todo_id, - "status": todo.get("status"), - "claimed_by": normalize_todo_claimed_by(todo.get("claimed_by")), - }, - "leases": leases, - } + snapshot = _read_execution_snapshot(runtime_root, goal_id, todo_id) + facts = _execution_facts(snapshot, goal_id, todo_id, safe_agent_id) except (OSError, RuntimeError, TypeError, ValueError): - facts = _unavailable_facts( - goal_id=goal_id, todo_id=todo_id, agent_id=safe_agent_id, - ) + facts = _unavailable_facts(goal_id=goal_id, todo_id=todo_id, agent_id=safe_agent_id) + return _project_execution_facts(facts) + + +def _read_execution_snapshot( + runtime_root: str | Path, goal_id: str, todo_id: str, +) -> dict[str, Any] | None: + response = effect_runtime_result( + "todo.read", + {"schema_version": "todo_read_request_v0", "kind": "execution_preparation_source", + "source": {"runtime_root": str(Path(runtime_root).expanduser().resolve(strict=False)), + "goal_id": goal_id}, + "todo_id": todo_id}, + timeout=LOCAL_COORDINATION_TODO_LIST_TIMEOUT_SECONDS, + ) + snapshot = ( + response.get("execution_preparation_source") if isinstance(response, dict) + and response.get("schema_version") == "todo_read_result_v0" else None + ) + if ( + not isinstance(snapshot, dict) or snapshot.get("goal_id") != goal_id + or snapshot.get("todo_id") != todo_id or snapshot.get("source_authority") not in LOCAL_AUTHORITY_SOURCES + or snapshot.get("decision_read_from_provider") is not True + or snapshot.get("legacy_fallback_used") is not False + or not isinstance(snapshot.get("provider_revision"), str) or not snapshot["provider_revision"] + or not isinstance(snapshot.get("todo"), dict) or snapshot["todo"].get("todo_id") != todo_id + or not isinstance(snapshot.get("leases"), list) + ): + return None + return snapshot + + +def _execution_facts( + snapshot: dict[str, Any] | None, goal_id: str, todo_id: str, agent_id: str, +) -> dict[str, Any]: + if snapshot is None: + return _unavailable_facts(goal_id=goal_id, todo_id=todo_id, agent_id=agent_id) + todo = snapshot["todo"] + leases = [] + for lease in snapshot["leases"]: + if not isinstance(lease, dict) or lease.get("todo_id") != todo_id: + raise ValueError("execution preparation returned a lease for a different Todo") + status = str(lease.get("status") or "unknown").strip().lower() + leases.append({ + "status": status if status in {"active", "released"} else "unknown", + "active": lease_is_active(dict(lease)), + **({"owner": lease["owner"]} if normalize_todo_claimed_by(lease.get("owner")) else {}), + "lease_proof": {"idempotency_key": lease.get("idempotency_key"), + "expected_version": lease.get("version")}, + }) + return { + "schema_version": "execution_preparation_facts_v0", + "goal_id": goal_id, + "agent_id": agent_id, + "handoff_mode": str(snapshot.get("handoff_mode") or "hard_lease"), + "authority_available": True, + "selected_todo": { + "todo_id": todo_id, + "status": todo.get("status"), + "claimed_by": normalize_todo_claimed_by(todo.get("claimed_by")), + }, + "leases": leases, + } + +def _project_execution_facts(facts: dict[str, Any]) -> dict[str, Any]: result = effect_runtime_result("work_item.execution_preparation.project", facts) if not isinstance(result, dict) or result.get("schema_version") != "execution_preparation_v0": raise TypeError("typed execution preparation returned an invalid projection") claim = result.get("claim") if isinstance(claim, dict) and isinstance(claim.get("cli_args"), list): - # The displayed command selects a smaller receipt. Hosts still receive - # the exact mutation argv in cli_args, including on historical captures. claim["command_line"] = shlex.join(["loopx", *map(str, claim["cli_args"]), "--thin"]) return result +class RefreshTaskLeaseUnavailable(ValueError): + code = "refresh_task_lease_unavailable" + + +def prepared_lease_receipt_fields(proof: Mapping[str, Any]) -> dict[str, Any]: + """Serialize the prepared lease without persisting its operation key.""" + return { + "prepared_lease_key_sha256": hashlib.sha256(proof["idempotency_key"].encode("utf-8")).hexdigest(), + "prepared_lease_version": proof["expected_version"], + } + + +def quota_prepared_lease_receipt_fields( + preparation: object, *, todo_id: str | None, selected_todo_id: object, + agent_id: str | None, delivery_admitted: bool, +) -> dict[str, Any]: + if ( + todo_id is None or selected_todo_id != todo_id + or not isinstance(preparation, Mapping) + or preparation.get("handoff_mode") != "hard_lease" + or preparation.get("todo_id") != todo_id + or preparation.get("agent_id") != agent_id + ): + return {} + fields: dict[str, Any] = {"prepared_lease_required": True} + if delivery_admitted and preparation.get("state") == "ready" and preparation.get("delivery_allowed") is True: + fields.update(prepared_lease_receipt_fields(preparation["lease_proof"])) + return fields + + +@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, +) -> Iterator[None]: + prepared_lease = settlement.prepared_lease + prepared_required = settlement.prepared_lease_required or prepared_lease is not None + if (requested_proof is None and not prepared_required + and not local_authority_is_promoted(runtime_root=runtime_root, goal_id=goal_id)): + yield + return + + # Legacy spend acquires maintenance before the run index. Try maintenance + # once here so this order cannot block a competing accounting process. + with ExitStack() as locks: + locks.enter_context(exclusive_mutation_file_lock( + shadow_maintenance_lock_target(runtime_root, goal_id), + timeout_seconds=0, + operation="refresh-state-execution", + )) + require_shadow_primary_write_allowed(runtime_root, goal_id) + snapshot = _read_execution_snapshot(runtime_root, goal_id, todo_id) + if snapshot is None: + raise RefreshTaskLeaseUnavailable("The selected Todo's execution authority could not be read.") + 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: + execution_bound = ( + requested_proof is not None + and current_proof == requested_proof + and prepared_lease == prepared_lease_receipt_fields(requested_proof) + ) + 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: + 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: + raise RefreshTaskLeaseUnavailable("The completed Todo is missing this Turn's completion receipt.") + goal = locked_goal + if goal is None: + source, _ = load_recommendation_source_goal(registry_path, goal_id) + invoked = registry_path.resolve() + for registry in dict.fromkeys((source, invoked)): + locks.enter_context(exclusive_cross_runtime_file_lock( + registry, timeout_seconds=0, operation="refresh-state-execution", + )) + current_source, goal = load_recommendation_source_goal(registry_path, goal_id) + if current_source != source or goal is None: + raise RefreshTaskLeaseUnavailable("The Goal source changed before this writeback committed.") + if goal_is_stopped(goal): + raise RefreshTaskLeaseUnavailable("The Goal was stopped before this writeback committed.") + yield + + def apply_selected_todo_execution_preparation( payload: dict[str, Any], *, diff --git a/loopx/control_plane/quota/heartbeat_receipt.py b/loopx/control_plane/quota/heartbeat_receipt.py index fe40b09a8d..52ceb47a56 100644 --- a/loopx/control_plane/quota/heartbeat_receipt.py +++ b/loopx/control_plane/quota/heartbeat_receipt.py @@ -206,6 +206,8 @@ def ensure_turn_heartbeat_settlement_receipt( semantic_replan_obligation_id: str | None, semantic_replan_capability_guard: Mapping[str, object] | None = None, quota_spend_source: str | None = None, + task_lease_proof: Mapping[str, object] | None = None, + prepared_lease_required: bool = False, goal_ref: Mapping[str, object] | None = None, before_bind: Callable[[], None] | None = None, ) -> dict[str, object]: @@ -316,6 +318,12 @@ def ensure_turn_heartbeat_settlement_receipt( "stall_observation": "not_applicable", "source": "loopx_turn_run_once", } + if identity.todo_id is not None and (prepared_lease_required or task_lease_proof is not None): + details["prepared_lease_required"] = True + if task_lease_proof is not None and identity.todo_id is not None: + from .execution_preparation import prepared_lease_receipt_fields + + details.update(prepared_lease_receipt_fields(task_lease_proof)) if semantic_replan_guard_scoped: details["semantic_replan_obligation_id"] = ( normalized_semantic_replan_obligation_id or "" @@ -697,14 +705,14 @@ def requalify_bound_heartbeat_receipt( receipt_revision = str( qualified_details.get("settlement_receipt_revision") or "" ) - summary = ( - "same-Turn review guard upgraded without rebinding" - if receipt_revision == "review_guard_upgrade" - else "same-Turn work admission requalified without rebinding" - ) + summary = { + "review_guard_upgrade": "same-Turn review guard upgraded without rebinding", + "lease_readiness": "same-Turn lease readiness recorded without readmission", + }.get(receipt_revision, "same-Turn work admission requalified without rebinding") corrected = build_rollout_event( goal_id=goal_id, goal_ref=goal_ref, event_kind="quota_should_run", - agent_id=agent_id, todo_id=todo_id, run_id=turn_instance_id, status=status, + agent_id=agent_id, todo_id=todo_id, run_id=turn_instance_id, + status=str(original.get("status") or status) if receipt_revision == "lease_readiness" else status, summary=summary, source_event_id=event_id, caused_by=event_id, details=dict(qualified_details), ) diff --git a/loopx/control_plane/quota/heartbeat_receipt_identity.ts b/loopx/control_plane/quota/heartbeat_receipt_identity.ts index 2943929bc1..ba21a44e74 100644 --- a/loopx/control_plane/quota/heartbeat_receipt_identity.ts +++ b/loopx/control_plane/quota/heartbeat_receipt_identity.ts @@ -224,6 +224,26 @@ export function selectAdmissionHeartbeatReceipt( return identities.size === 1 ? [...identities.values()][0]! : null; } +interface HeartbeatPreparedLease extends JsonObject { + prepared_lease_key_sha256: string; + prepared_lease_version: number; +} + +export function heartbeatPreparedLease(details: JsonObject): HeartbeatPreparedLease | null { + const key = details.prepared_lease_key_sha256; + const version = details.prepared_lease_version; + if (key === undefined && version === undefined) return null; + if (typeof key !== "string" || !/^[a-f0-9]{64}$/.test(key) || + typeof version !== "number" || !Number.isSafeInteger(version) || version < 1) { + throw new EffectRuntimeRequestError("Turn readiness has a malformed lease proof", "malformed_settlement_state"); + } + return {prepared_lease_key_sha256: key, prepared_lease_version: version}; +} + +export function isHeartbeatLeaseReadiness(event: JsonObject): boolean { + return heartbeatReceiptDetails(event).settlement_receipt_revision === "lease_readiness"; +} + /** Reentry can qualify work on the original binding, never replace its authority. * The caller commits this append-only projection under the receipt-log lock. */ export function heartbeatWorkRequalification( @@ -253,12 +273,23 @@ export function heartbeatWorkRequalification( const currentReplan = normalizeHeartbeatReplanObligationId(currentDetails.semantic_replan_obligation_id); if (nativeChildReportAdmission(currentDetails, null, identity.effect_id, phase, false, currentReplan) .report_permission !== "new_operation") return replay; + const preparedLease = identity.todo_id === null ? null : heartbeatPreparedLease(currentDetails); if (nativeChildReportAdmission(oldDetails, original.status, identity.effect_id, phase, false, originalReplan) - .report_permission === "new_operation") return replay; + .report_permission === "new_operation") { + if (preparedLease === null || originalReplan !== currentReplan) return replay; + const previous = heartbeatPreparedLease(oldDetails); + const previousVersion = previous?.prepared_lease_version ?? 0; + // A delayed readiness evaluation can never replace a newer lease. Only + // the preparation proof changes; admission, billing and workspace stay bound. + if (preparedLease.prepared_lease_version <= previousVersion) return replay; + return {append: true, details: { + ...oldDetails, ...preparedLease, settlement_receipt_revision: "lease_readiness", + }}; + } if (currentReplan !== null && currentReplan !== originalReplan) { throw receiptIdentityConflict("work requalification cannot replace the original semantic replan guard"); } - const qualified = {...oldDetails}; + const qualified = {...oldDetails, ...preparedLease}; // The guarded route becomes durable when this binding first admits work. const spendSource = currentDetails.quota_spend_source; if (spendSource != null) { diff --git a/loopx/control_plane/quota/refresh_external_delivery.py b/loopx/control_plane/quota/refresh_external_delivery.py index bf9ecadbcb..7620f7ca04 100644 --- a/loopx/control_plane/quota/refresh_external_delivery.py +++ b/loopx/control_plane/quota/refresh_external_delivery.py @@ -38,6 +38,18 @@ def finish_external_delivery_refresh( return payload +def persist_external_delivery_pause( + readback: QuotaSettlementReadback, + runtime_root: Path, *, + goal_ref: Mapping[str, Any] | None = None, +) -> None: + plan = readback.external_delivery + if isinstance(plan, dict) and (plan.get("transition") or {}).get("state") == "paused": + finish_external_delivery_refresh( + {"ok": True}, readback, runtime_root, dry_run=False, goal_ref=goal_ref, + ) + + def refresh_recovery_payload( readback: QuotaSettlementReadback, *, registry_path: Path, runtime_root: Path, goal_id: str, dry_run: bool, @@ -51,16 +63,10 @@ def refresh_recovery_payload( decision = recovery["decision"] delivery_error = plan.get("error_code") if decision != "reject" else None if decision not in {"replay", "repair_receipt", "reject"} and not delivery_error: - # Record a requested pause before a new writeback can commit. If later - # validation fails, retaining the pause is conservative and retryable. - if (plan.get("transition") or {}).get("state") == "paused": - finish_external_delivery_refresh( - {"ok": True}, - readback, - runtime_root, - dry_run=dry_run, - goal_ref=goal_ref, - ) + # First Todo writeback persists its pause under the same execution + # fence as the business effects. Other recovery retains its early pause. + if not dry_run and (decision != "append" or identity.todo_id is None): + persist_external_delivery_pause(readback, runtime_root, goal_ref=goal_ref) return None payload = { **(readback.writeback_run or {}), "ok": decision != "reject" and not delivery_error, diff --git a/loopx/control_plane/quota/settlement.py b/loopx/control_plane/quota/settlement.py index 689ed011b7..865c8dac81 100644 --- a/loopx/control_plane/quota/settlement.py +++ b/loopx/control_plane/quota/settlement.py @@ -183,6 +183,8 @@ class QuotaSettlementReadback: replan_execution_pending: bool = False admission_recorded_at: str | None = None admission_event_id: str | None = None + prepared_lease: dict[str, Any] | None = None + prepared_lease_required: bool = False def render_owed_host_command(argv: list[str]) -> str: @@ -582,4 +584,6 @@ def read_heartbeat_settlement( ), admission_recorded_at=_optional_readback_text(payload.get("admission_recorded_at")), admission_event_id=_optional_readback_text(payload.get("admission_event_id")), + prepared_lease=_optional_readback_record(payload.get("prepared_lease")), + prepared_lease_required=payload.get("prepared_lease_required") is True, ) diff --git a/loopx/control_plane/quota/settlement_cli.py b/loopx/control_plane/quota/settlement_cli.py index 7972cf955e..c73eb4b760 100644 --- a/loopx/control_plane/quota/settlement_cli.py +++ b/loopx/control_plane/quota/settlement_cli.py @@ -20,6 +20,7 @@ from .effect_program import SettlementIdentity from .effective_action import EffectiveAction from .error_codes import HeartbeatReceiptIdentityConflictError +from .execution_preparation import quota_prepared_lease_receipt_fields from .heartbeat_receipt import ( heartbeat_receipt_view, requalify_bound_heartbeat_receipt, @@ -545,6 +546,13 @@ def quota_rollout_details( ) if workspace_causality: details.update(delivery_workspace_causality_event_fields(workspace_causality)) + details.update(quota_prepared_lease_receipt_fields( + agent_channel.get("execution_preparation"), + todo_id=todo_id, + selected_todo_id=details["selected_todo_id"], + agent_id=getattr(args, "agent_id", None), + delivery_admitted=closeout_required and payload.get("normal_delivery_allowed") is True, + )) return details diff --git a/loopx/control_plane/quota/settlement_plan.ts b/loopx/control_plane/quota/settlement_plan.ts index c338df35f5..56ec3158ac 100644 --- a/loopx/control_plane/quota/settlement_plan.ts +++ b/loopx/control_plane/quota/settlement_plan.ts @@ -12,6 +12,8 @@ import {MATERIAL_DELIVERY_OUTCOMES} from "../work_items/delivery_outcome.ts"; import {DELIVERY_BOUNDARIES} from "../turn_driver/delivery_continuity.ts"; import {PROGRESS_RESULT_CLASSES} from "../work_items/replan_history.ts"; import {HANDOFF_MODES} from "../coordination/handoff_mode_vocabulary.ts"; +import {decodeTaskLeaseProof} from "../coordination/task_lease_proof.ts"; +import {normalizeIdempotencyKey} from "../work_items/task_lease_acquire.ts"; import {EXECUTION_PREPARATION_SCHEMA_VERSION} from "../work_items/execution_preparation.ts"; import {EffectiveAction} from "./effective_action.generated.ts"; @@ -36,7 +38,17 @@ export function composedCliSettlementChannel(cli: JsonObject): JsonObject { if (actions.filter(command => command === writebackCommand).length !== 1 || actions.filter(command => command === spendCommand).length !== 1 || actions.indexOf(writebackCommand) >= actions.indexOf(spendCommand)) return cli; - const composed = (command: string) => command + " --settle-turn --settlement-receipt"; + const preparation = jsonObject(cli.execution_preparation); + const proof = preparation?.handoff_mode === "hard_lease" + ? decodeTaskLeaseProof(preparation.lease_proof) : null; + if (preparation?.handoff_mode === "hard_lease" && !proof) { + throw new Error("Prepared settlement requires the current task lease proof"); + } + const fence = proof + ? " --task-lease-idempotency-key=" + normalizeIdempotencyKey(proof.idempotency_key) + + " --task-lease-expected-version " + proof.expected_version + : ""; + const composed = (command: string) => command + " --settle-turn --settlement-receipt" + fence; const newWriteback = {...writeback, command_template: composed(writebackCommand), ...(typeof writeback.semantic_closeout_command_template === "string" ? {semantic_closeout_command_template: composed(writeback.semantic_closeout_command_template)} : {})}; diff --git a/loopx/control_plane/quota/settlement_readback.ts b/loopx/control_plane/quota/settlement_readback.ts index 231f67965f..9f046b5009 100644 --- a/loopx/control_plane/quota/settlement_readback.ts +++ b/loopx/control_plane/quota/settlement_readback.ts @@ -65,6 +65,8 @@ import { selectAdmissionHeartbeatReceipt, selectEffectiveHeartbeatReceipt, heartbeatWorkRequalification, + heartbeatPreparedLease, + isHeartbeatLeaseReadiness, type HeartbeatReceiptFact, } from "./heartbeat_receipt_identity.ts"; @@ -785,6 +787,32 @@ function admissionHeartbeatReceipt( : selectAdmissionHeartbeatReceipt(identity.goal_id, identity.agent_id, entries); } +function heartbeatValidationEvent( + events: readonly JsonObject[], + receipt: JsonObject, + identity: SettlementIdentity, +): JsonObject | null { + if (!isHeartbeatLeaseReadiness(receipt)) return receipt; + // Readiness appends to the same Turn; only its original positive work + // admission can serve as validation. Follow durable links toward that root. + let validation = receipt; + for (let index = events.lastIndexOf(receipt) - 1; index >= 0; index -= 1) { + const event = events[index]!; + const lineage = jsonObject(validation.causality); + const parentId = optionalString(lineage?.source_event_id); + if (parentId === null || lineage?.caused_by !== parentId) return null; + if (event.event_id !== parentId) continue; + const fact = heartbeatReceiptFactFromEvent(event); + if (event.event_kind !== "quota_should_run" || + event.goal_id !== identity.goal_id || event.agent_id !== identity.agent_id || + fact.run_id !== identity.turn_instance_id || fact.todo_id !== identity.todo_id || + !effectIdsMatch(fact.settlement_effect_id, identity.effect_id)) return null; + if (!isHeartbeatLeaseReadiness(event)) return event; + validation = event; + } + return null; +} + function writebackResult( identity: SettlementIdentity, run: JsonObject | null, @@ -1184,7 +1212,11 @@ function resolveIdentity( "identity_mismatch", ); } - const eventId = optionalString(receiptEvent.event_id); + const validationEvent = heartbeatValidationEvent(events, receiptEvent, identity); + if (validationEvent === null) { + return failedIdentity("matching work admission is missing for Turn readiness", "receipt_missing"); + } + const eventId = optionalString(validationEvent.event_id); return settlementPure( settlementIdentityPayload(identity), [settlementReceipt(identity, "validation", eventId ? `rollout_event:${eventId}` : null)], @@ -1427,6 +1459,10 @@ function readQuotaSettlementFromRequest( ); } const receiptDetails = details(heartbeatReceipt); + const preparedLease = heartbeatPreparedLease(receiptDetails); + if (Object.hasOwn(receiptDetails, "prepared_lease_required") && receiptDetails.prepared_lease_required !== true) { + throw new EffectRuntimeRequestError("Turn readiness requirement is malformed", "malformed_settlement_state"); + } const writebackRun = findWriteback(runs, identity); const writebackEvent = findStepEvent(events, identity, "refresh_state"); const spendRun = findSpend( @@ -1575,6 +1611,8 @@ function readQuotaSettlementFromRequest( ), spend_run: includeRecords ? spendRun : null, heartbeat_receipt: includeRecords ? heartbeatReceipt : null, + ...(preparedLease === null ? {} : {prepared_lease: preparedLease}), + ...(receiptDetails.prepared_lease_required === true ? {prepared_lease_required: true} : {}), admission_recorded_at: typeof admissionReceipt?.recorded_at === "string" && admissionReceipt.recorded_at.trim() ? admissionReceipt.recorded_at : null, admission_event_id: typeof admissionReceipt?.event_id === "string" diff --git a/loopx/control_plane/turn_driver/codex_execution_preparation.py b/loopx/control_plane/turn_driver/codex_execution_preparation.py index 7375ead06b..3d935a8bb5 100644 --- a/loopx/control_plane/turn_driver/codex_execution_preparation.py +++ b/loopx/control_plane/turn_driver/codex_execution_preparation.py @@ -344,6 +344,7 @@ def before_bind() -> None: settlement_identity, semantic_replan_guard_scoped=False, semantic_replan_obligation_id=None, + prepared_lease_required=True, goal_ref=goal_ref, **({"before_bind": before_bind} if admit_start is not None else {}), ) diff --git a/loopx/control_plane/work_items/execution_preparation.ts b/loopx/control_plane/work_items/execution_preparation.ts index 30c1f4ce17..8a067c2ede 100644 --- a/loopx/control_plane/work_items/execution_preparation.ts +++ b/loopx/control_plane/work_items/execution_preparation.ts @@ -97,9 +97,16 @@ export function projectExecutionPreparation(value: unknown): JsonObject { reason_code: retired, reason: HANDOFF_MODE_RETIRED_REASON}; } if (status !== "open") { + const retained = leases.length === 1 ? leases[0] : null; + const retiredProof = request.include_retired_lease_for_writeback === true && + status !== null && handoffMode === "hard_lease" && + (claimedBy === null || claimedBy === agentId) && + retained?.status === "released" && retained.owner === agentId + ? retained.lease_proof : undefined; return {...base, state: "blocked_by_conflict", delivery_allowed: false, reason_code: "selected_todo_not_open", - reason: "The selected Todo is no longer open; select current work before execution."}; + reason: "The selected Todo is no longer open; select current work before execution.", + ...(retiredProof === undefined ? {} : {retired_lease_proof: retiredProof})}; } if (claimedBy !== null && claimedBy !== agentId) { return {...base, state: "blocked_by_conflict", delivery_allowed: false, diff --git a/loopx/control_plane/work_items/interaction_contract.py b/loopx/control_plane/work_items/interaction_contract.py index 561f6c1208..74ec33dba7 100644 --- a/loopx/control_plane/work_items/interaction_contract.py +++ b/loopx/control_plane/work_items/interaction_contract.py @@ -762,7 +762,7 @@ def _successor_replan_required_cli_actions( goal_id: str, lifecycle_actor_args: str, typed_quota_guard: str, - settlement_plan: Mapping[str, object], + settlement_plan: Mapping[str, object] | None, scheduler_args: str, settlement_args: str, scoped_cli_args: str, diff --git a/loopx/state_refresh.py b/loopx/state_refresh.py index a84e15503b..e661917587 100644 --- a/loopx/state_refresh.py +++ b/loopx/state_refresh.py @@ -32,10 +32,13 @@ ) from .control_plane.agents.delivery_workspace import qualify_delivery_workspace_isolation from .control_plane.quota.refresh_external_delivery import ( - finish_external_delivery_refresh, refresh_recovery_payload, + finish_external_delivery_refresh, + persist_external_delivery_pause, + refresh_recovery_payload, ) from .control_plane.quota.blocked_retry import require_blocked_retry_wait from .control_plane.quota.accounting_admission import quota_accounting_admission +from .control_plane.quota.execution_preparation import guard_first_writeback_execution from .control_plane.coordination.local_authority import local_authority_is_promoted from .control_plane.todos.active_state_todo_parser import parse_active_state_todos from .control_plane.quota.settlement import ( @@ -755,6 +758,7 @@ def refresh_state_run( external_delivery: dict[str, Any] | None = None, goal_ref: dict[str, str] | None = None, goal_acceptance_preview: dict[str, Any] | None = None, + task_lease_proof: dict[str, Any] | None = None, ) -> dict[str, Any]: from .control_plane.todos.provider_projection import recover_refresh_todo_projection @@ -1593,6 +1597,21 @@ def read_explore_context(read_goal_id: str, read_agent_id: str | None): projection_status_surface=f"refresh-state dry-run: {classification}", ) if not dry_run: + if ( + settlement_identity is not None and settlement_identity.todo_id + and not checkpoint_supplement and blocked_retry is None + ): + usage_booking_guard.enter_context(guard_first_writeback_execution( + runtime_root=runtime_root, registry_path=registry_path, goal_id=safe_goal_id, + todo_id=settlement_identity.todo_id, agent_id=settlement_identity.agent_id, + requested_proof=task_lease_proof, + settlement=settlement_readback, + locked_goal=current_goal if normalized_next_action else None, + )) + if settlement_readback is not None: + persist_external_delivery_pause( + settlement_readback, runtime_root, goal_ref=goal_ref, + ) runs_dir.mkdir(parents=True, exist_ok=True) json_path, markdown_path = reserve_unique_run_paths(runs_dir, generated_at) index_record["json_path"] = str(json_path) diff --git a/tests/control_plane/test_quota_authority_settlement_journey.py b/tests/control_plane/test_quota_authority_settlement_journey.py index 5cff09bba4..f786edcd24 100644 --- a/tests/control_plane/test_quota_authority_settlement_journey.py +++ b/tests/control_plane/test_quota_authority_settlement_journey.py @@ -621,7 +621,21 @@ def run(*args): "--task-lease-expected-version", version) assert code == 0 and updated["ok"] is True, updated - code, refreshed = _refresh(project, runtime, registry) + proof_args = ("--task-lease-idempotency-key", lease_key, "--task-lease-expected-version", version) + code, unprepared = _refresh(project, runtime, registry, *proof_args) + assert code == 1 and unprepared["error_code"] == "refresh_task_lease_unavailable", unprepared + assert unprepared["appended"] is False and cli._spend_run_count(runtime) == 0 + before_ready = read_heartbeat_settlement(runtime, goal_id=cli.GOAL_ID, agent_id=cli.AGENT_ID, + todo_id=cli.TODO_ID, turn_instance_id=cli.TURN_ID, registry_path=registry) + assert before_ready is not None and not before_ready.writeback.receipts and not before_ready.spend.receipts + + code, ready = _guard(project, runtime, registry) + assert code == 0 and ready["normal_delivery_allowed"], ready + assert ready["heartbeat_receipt"]["settlement_identity"] == guard["heartbeat_receipt"]["settlement_identity"] + prep = ready["interaction_contract"]["cli_channel"]["execution_preparation"] + assert prep["state"] == "ready" + assert prep["lease_proof"] == {"idempotency_key": lease_key, "expected_version": int(version)} + code, refreshed = _refresh(project, runtime, registry, *proof_args) assert code == 0, refreshed command = refreshed["settlement_owed"]["command"] code, spent = _execute(command, project, runtime, registry) @@ -649,6 +663,80 @@ def run(*args): assert cli._spend_run_count(runtime) == 1 +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("transition", ["complete", "defer"]) +def test_first_writeback_refuses_generation_retired_by_later_work(tmp_path, provider, transition): + project, runtime, registry, _, _ = _source( + tmp_path, provider=provider, handoff_mode="hard_lease", + extra=f"claimed_by={cli.AGENT_ID}", + ) + scope = ("--goal-id", cli.GOAL_ID, "--todo-id", cli.TODO_ID) + + def run(*args): + return cli._run_cli(registry, runtime, *args, *scope, cwd=project) + + code, claim = run("todo", "claim", "--agent-id", cli.AGENT_ID, "--claimed-by", cli.AGENT_ID) + assert code == 0 and claim["ok"], claim + code, guard = _guard(project, runtime, registry) + assert code == 0 and guard["normal_delivery_allowed"], guard + proof = guard["execution_preparation"]["lease_proof"] + code, released = run("task-lease", "release", "--owner", cli.AGENT_ID, + "--idempotency-key", proof["idempotency_key"], + "--expected-version", str(proof["expected_version"])) + assert code == 0 and released["released"], released + code, newer = run("todo", "claim", "--agent-id", cli.AGENT_ID, "--claimed-by", cli.AGENT_ID) + assert code == 0 and newer["ok"] and newer["claim_operation_id"] != claim["claim_operation_id"], newer + lifecycle = ("todo", "complete", "--turn-instance-id", cli.TURN_ID, + "--evidence", "evidence:fixture-complete") if transition == "complete" else ( + "todo", "update", "--status", "deferred", "--resume-when", "resume_at:2099-01-01T00:00:00Z", + "--reason", "Wait for the fixture's external dependency", + ) + code, committed = run(*lifecycle, "--agent-id", cli.AGENT_ID, + "--claim-operation-id", newer["claim_operation_id"]) + assert code == 0 and committed["ok"], committed + journal = rollout_event_log_path(runtime, cli.GOAL_ID) + before_rejection = journal.read_bytes() + code, denied = _refresh(project, runtime, registry, + "--task-lease-idempotency-key", proof["idempotency_key"], + "--task-lease-expected-version", str(proof["expected_version"])) + assert code == 1 and not denied["appended"], denied + assert denied["error_code"] == "refresh_task_lease_unavailable", denied + # The rejected operation must not change the external delivery preference. + assert journal.read_bytes() == before_rejection + result = read_heartbeat_settlement(runtime, goal_id=cli.GOAL_ID, agent_id=cli.AGENT_ID, + todo_id=cli.TODO_ID, turn_instance_id=cli.TURN_ID, registry_path=registry) + assert result is not None and result.identity.value is not None + assert result.identity.value.as_dict() == guard["heartbeat_receipt"]["settlement_identity"] + assert not result.writeback.receipts and not result.spend.receipts + assert cli._spend_run_count(runtime) == 0 + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_unprepared_turn_does_not_become_admitted_after_deferral(tmp_path, provider): + project, runtime, registry, _, _ = _source( + tmp_path, provider=provider, handoff_mode="hard_lease", + extra=f"claimed_by={cli.AGENT_ID}", + ) + code, pending = _guard(project, runtime, registry) + assert code == 0 and pending["execution_preparation"]["state"] == "claim_required", pending + scope = ("--goal-id", cli.GOAL_ID, "--todo-id", cli.TODO_ID, "--agent-id", cli.AGENT_ID) + code, claim = cli._run_cli(registry, runtime, "todo", "claim", *scope, + "--claimed-by", cli.AGENT_ID, cwd=project) + assert code == 0 and claim["ok"], claim + code, deferred = cli._run_cli(registry, runtime, "todo", "update", *scope, "--status", "deferred", + "--resume-when", "resume_at:2099-01-01T00:00:00Z", "--reason", "Wait for the fixture's dependency", + "--claim-operation-id", claim["claim_operation_id"], cwd=project) + assert code == 0 and deferred["ok"], deferred + code, denied = _refresh(project, runtime, registry) + assert code == 1 and not denied["appended"], denied + assert denied["error_code"] == "refresh_task_lease_unavailable", denied + readback = read_heartbeat_settlement(runtime, goal_id=cli.GOAL_ID, agent_id=cli.AGENT_ID, + todo_id=cli.TODO_ID, turn_instance_id=cli.TURN_ID, registry_path=registry) + assert readback is not None and readback.prepared_lease_required and readback.prepared_lease is None + assert not readback.writeback.receipts and not readback.spend.receipts + assert cli._spend_run_count(runtime) == 0 + + def test_returned_command_settles_and_repairs_receipts_without_another_debit(tmp_path): project, runtime, registry = cli._write_fixture(tmp_path) cli._initialize_fixture_authority(project, runtime) diff --git a/tests/control_plane_ts/execution_preparation.test.ts b/tests/control_plane_ts/execution_preparation.test.ts index 3cd3ccf7ee..f1396c4e81 100644 --- a/tests/control_plane_ts/execution_preparation.test.ts +++ b/tests/control_plane_ts/execution_preparation.test.ts @@ -90,6 +90,24 @@ test("malformed facts are rejected", () => { ); }); +test("retired lease proof is available for writeback without admitting delivery", () => { + const proof = {idempotency_key: "original-attempt", expected_version: 7}; + const retired = {status: "released", active: false, owner: "agent-a", lease_proof: proof}; + for (const status of ["done", "deferred"]) { + const selected_todo = {...facts().selected_todo, status}; + const ordinary = projectExecutionPreparation(facts({selected_todo, leases: [retired]})); + const writeback = (leases: unknown[]) => projectExecutionPreparation(facts({ + selected_todo, leases, include_retired_lease_for_writeback: true, + })); + assert.equal(ordinary.retired_lease_proof, undefined); + assert.equal(writeback([retired]).delivery_allowed, false); + assert.deepEqual(writeback([retired]).retired_lease_proof, proof); + assert.equal(writeback([{...retired, owner: "agent-b"}]).retired_lease_proof, undefined); + assert.equal(writeback([retired, {...retired, owner: "agent-b"}]).retired_lease_proof, undefined); + assert.equal(writeback([{...retired, status: "active"}]).retired_lease_proof, undefined); + } +}); + test("legacy facts stay readable and an old matching lease grants no new execution", () => { for (const mode of EXECUTION_HANDOFF_MODES) { assert.doesNotThrow(() => projectExecutionPreparation(facts({handoff_mode: mode}))); From d6442fdea8a3a12845defd28f6f98b054ffd8c49 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 20 Nov 2026 05:47:55 +0800 Subject: [PATCH 2/6] Retain the original task lease generation on Turn readiness Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../quota/execution_preparation.py | 18 ++++++-- .../quota/heartbeat_receipt_identity.ts | 19 ++++++-- loopx/control_plane/quota/settlement.py | 3 ++ .../work_items/execution_preparation.ts | 8 ++-- ...test_quota_authority_settlement_journey.py | 46 +++++++++++++++++++ .../native_child_admission.test.ts | 31 +++++++++++++ 6 files changed, 114 insertions(+), 11 deletions(-) diff --git a/loopx/control_plane/quota/execution_preparation.py b/loopx/control_plane/quota/execution_preparation.py index 17d532ad2b..b77e382767 100644 --- a/loopx/control_plane/quota/execution_preparation.py +++ b/loopx/control_plane/quota/execution_preparation.py @@ -113,6 +113,7 @@ def _execution_facts( **({"owner": lease["owner"]} if normalize_todo_claimed_by(lease.get("owner")) else {}), "lease_proof": {"idempotency_key": lease.get("idempotency_key"), "expected_version": lease.get("version")}, + "lease_epoch": lease.get("lease_epoch"), }) return { "schema_version": "execution_preparation_facts_v0", @@ -143,11 +144,14 @@ class RefreshTaskLeaseUnavailable(ValueError): code = "refresh_task_lease_unavailable" -def prepared_lease_receipt_fields(proof: Mapping[str, Any]) -> dict[str, Any]: - """Serialize the prepared lease without persisting its operation key.""" +def prepared_lease_receipt_fields( + proof: Mapping[str, Any], *, lease_epoch: int | None = None, +) -> dict[str, Any]: + """Serialize the prepared generation without persisting its operation key.""" return { "prepared_lease_key_sha256": hashlib.sha256(proof["idempotency_key"].encode("utf-8")).hexdigest(), "prepared_lease_version": proof["expected_version"], + **({"prepared_lease_epoch": lease_epoch} if lease_epoch is not None else {}), } @@ -165,7 +169,9 @@ def quota_prepared_lease_receipt_fields( return {} fields: dict[str, Any] = {"prepared_lease_required": True} if delivery_admitted and preparation.get("state") == "ready" and preparation.get("delivery_allowed") is True: - fields.update(prepared_lease_receipt_fields(preparation["lease_proof"])) + fields.update(prepared_lease_receipt_fields( + preparation["lease_proof"], lease_epoch=preparation.get("lease_epoch"), + )) return fields @@ -204,10 +210,14 @@ def guard_first_writeback_execution( 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) execution_bound = ( requested_proof is not None and current_proof == requested_proof - and prepared_lease == prepared_lease_receipt_fields(requested_proof) + and prepared_lease == prepared_lease_receipt_fields(requested_proof, lease_epoch=lease_epoch) ) else: # Older admitted closeouts may predate persisted lease readiness. diff --git a/loopx/control_plane/quota/heartbeat_receipt_identity.ts b/loopx/control_plane/quota/heartbeat_receipt_identity.ts index ba21a44e74..b2eed924de 100644 --- a/loopx/control_plane/quota/heartbeat_receipt_identity.ts +++ b/loopx/control_plane/quota/heartbeat_receipt_identity.ts @@ -227,17 +227,21 @@ export function selectAdmissionHeartbeatReceipt( interface HeartbeatPreparedLease extends JsonObject { prepared_lease_key_sha256: string; prepared_lease_version: number; + prepared_lease_epoch?: number; } export function heartbeatPreparedLease(details: JsonObject): HeartbeatPreparedLease | null { const key = details.prepared_lease_key_sha256; const version = details.prepared_lease_version; - if (key === undefined && version === undefined) return null; + const epoch = details.prepared_lease_epoch; + if (key === undefined && version === undefined && epoch === undefined) return null; if (typeof key !== "string" || !/^[a-f0-9]{64}$/.test(key) || - typeof version !== "number" || !Number.isSafeInteger(version) || version < 1) { + typeof version !== "number" || !Number.isSafeInteger(version) || version < 1 || + (epoch !== undefined && (typeof epoch !== "number" || !Number.isSafeInteger(epoch) || epoch < 1))) { throw new EffectRuntimeRequestError("Turn readiness has a malformed lease proof", "malformed_settlement_state"); } - return {prepared_lease_key_sha256: key, prepared_lease_version: version}; + return {prepared_lease_key_sha256: key, prepared_lease_version: version, + ...(epoch === undefined ? {} : {prepared_lease_epoch: epoch})}; } export function isHeartbeatLeaseReadiness(event: JsonObject): boolean { @@ -274,10 +278,17 @@ export function heartbeatWorkRequalification( if (nativeChildReportAdmission(currentDetails, null, identity.effect_id, phase, false, currentReplan) .report_permission !== "new_operation") return replay; const preparedLease = identity.todo_id === null ? null : heartbeatPreparedLease(currentDetails); + const previous = identity.todo_id === null ? null : heartbeatPreparedLease(oldDetails); + if (previous !== null && ( + preparedLease === null || preparedLease.prepared_lease_key_sha256 !== previous.prepared_lease_key_sha256 || + (previous.prepared_lease_epoch === undefined + ? preparedLease.prepared_lease_version !== previous.prepared_lease_version + : preparedLease.prepared_lease_epoch !== previous.prepared_lease_epoch))) { + throw receiptIdentityConflict("work requalification must preserve this Turn's original task lease generation"); + } if (nativeChildReportAdmission(oldDetails, original.status, identity.effect_id, phase, false, originalReplan) .report_permission === "new_operation") { if (preparedLease === null || originalReplan !== currentReplan) return replay; - const previous = heartbeatPreparedLease(oldDetails); const previousVersion = previous?.prepared_lease_version ?? 0; // A delayed readiness evaluation can never replace a newer lease. Only // the preparation proof changes; admission, billing and workspace stay bound. diff --git a/loopx/control_plane/quota/settlement.py b/loopx/control_plane/quota/settlement.py index 865c8dac81..99747d82bf 100644 --- a/loopx/control_plane/quota/settlement.py +++ b/loopx/control_plane/quota/settlement.py @@ -12,6 +12,7 @@ from ..effect_runtime import EffectRuntimeRejected, effect_runtime_result from ..settlement_driver import decode_settlement_result from .accounting_admission import quota_accounting_admission +from .error_codes import HeartbeatReceiptIdentityConflictError from .effect_program import ( SETTLEMENT_IDENTITY_SCHEMA_VERSION, SETTLEMENT_PLAN_SCHEMA_VERSION, @@ -396,6 +397,8 @@ def read(admission: Mapping[str, Any] | None) -> Any: large_local_snapshot=True, ) except EffectRuntimeRejected as exc: + if exc.diagnostic_code == "heartbeat_receipt_identity_conflict": + raise HeartbeatReceiptIdentityConflictError(str(exc)) from None raise ValueError(str(exc)) from None if source_admission is not None: diff --git a/loopx/control_plane/work_items/execution_preparation.ts b/loopx/control_plane/work_items/execution_preparation.ts index 8a067c2ede..1109beb630 100644 --- a/loopx/control_plane/work_items/execution_preparation.ts +++ b/loopx/control_plane/work_items/execution_preparation.ts @@ -1,6 +1,7 @@ import {HANDOFF_MODES} from "../coordination/handoff_mode_vocabulary.ts"; import {HANDOFF_MODE_RETIRED_REASON, retiredHandoffModeRejection} from "../coordination/handoff_mode_policy.ts"; import type {JsonObject} from "../effect_program.ts"; +import {leaseEpoch} from "./task_lease_acquire.ts"; import {EffectRuntimeRequestError} from "../effect_runtime_errors.ts"; import { optionalNonEmptyString, @@ -37,7 +38,7 @@ function leaseFact(value: unknown, label: string): JsonObject { proof = {idempotency_key: key, expected_version: version}; } return {status, active, ...(owner === null ? {} : {owner}), - ...(proof === undefined ? {} : {lease_proof: proof})}; + ...(proof === undefined ? {} : {lease_proof: proof, lease_epoch: leaseEpoch(lease)})}; } function claimAction(goalId: string, todoId: string, agentId: string): JsonObject { @@ -106,7 +107,7 @@ export function projectExecutionPreparation(value: unknown): JsonObject { return {...base, state: "blocked_by_conflict", delivery_allowed: false, reason_code: "selected_todo_not_open", reason: "The selected Todo is no longer open; select current work before execution.", - ...(retiredProof === undefined ? {} : {retired_lease_proof: retiredProof})}; + ...(retiredProof === undefined ? {} : {retired_lease_proof: retiredProof, lease_epoch: retained?.lease_epoch})}; } if (claimedBy !== null && claimedBy !== agentId) { return {...base, state: "blocked_by_conflict", delivery_allowed: false, @@ -135,7 +136,8 @@ export function projectExecutionPreparation(value: unknown): JsonObject { reason_code: "matching_active_task_lease", // Capture the admitted generation once. Recovery must not borrow a // replacement lease after the original Host result was produced. - ...(active[0].lease_proof === undefined ? {} : {lease_proof: active[0].lease_proof})}; + ...(active[0].lease_proof === undefined ? {} : + {lease_proof: active[0].lease_proof, lease_epoch: active[0].lease_epoch})}; } const prior = leases[0]; diff --git a/tests/control_plane/test_quota_authority_settlement_journey.py b/tests/control_plane/test_quota_authority_settlement_journey.py index f786edcd24..799883e4d3 100644 --- a/tests/control_plane/test_quota_authority_settlement_journey.py +++ b/tests/control_plane/test_quota_authority_settlement_journey.py @@ -711,6 +711,52 @@ def run(*args): assert cli._spend_run_count(runtime) == 0 +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_original_turn_refuses_readiness_from_a_replacement_lease(tmp_path, provider): + project, runtime, registry, _, _ = _source( + tmp_path, provider=provider, handoff_mode="hard_lease", + extra=f"claimed_by={cli.AGENT_ID}", + ) + scope = ("--goal-id", cli.GOAL_ID, "--todo-id", cli.TODO_ID) + + def run(*args): + return cli._run_cli(registry, runtime, *args, *scope, cwd=project) + + code, claim = run("todo", "claim", "--agent-id", cli.AGENT_ID, "--claimed-by", cli.AGENT_ID) + assert code == 0 and claim["ok"], claim + code, admitted = _guard(project, runtime, registry) + assert code == 0 and admitted["normal_delivery_allowed"], admitted + original_proof = admitted["execution_preparation"]["lease_proof"] + journal = rollout_event_log_path(runtime, cli.GOAL_ID) + code, released = run("task-lease", "release", "--owner", cli.AGENT_ID, + "--idempotency-key", original_proof["idempotency_key"], + "--expected-version", str(original_proof["expected_version"])) + assert code == 0 and released["released"], released + code, replacement = run("todo", "claim", "--agent-id", cli.AGENT_ID, "--claimed-by", cli.AGENT_ID) + assert code == 0 and replacement["ok"], replacement + assert replacement["claim_operation_id"] != claim["claim_operation_id"] + code, inspection = run("task-lease", "inspect") + assert code == 0 and inspection["active"], inspection + replacement_lease = inspection["lease"] + assert replacement_lease["lease_epoch"] > admitted["execution_preparation"]["lease_epoch"] + before_retry = journal.read_bytes() + + code, refused = _guard(project, runtime, registry) + assert code != 0 and refused["ok"] is False, refused + assert refused["error_code"] == "heartbeat_receipt_identity_conflict", refused + assert journal.read_bytes() == before_retry + readback = read_heartbeat_settlement( + runtime, goal_id=cli.GOAL_ID, agent_id=cli.AGENT_ID, todo_id=cli.TODO_ID, + turn_instance_id=cli.TURN_ID, registry_path=registry, + ) + assert readback is not None and readback.heartbeat_receipt["event_id"] == admitted["heartbeat_receipt"]["event_id"] + assert not readback.writeback.receipts and not readback.spend.receipts + assert cli._spend_run_count(runtime) == 0 + + assert readback.prepared_lease["prepared_lease_epoch"] == admitted["execution_preparation"]["lease_epoch"] + assert readback.prepared_lease["prepared_lease_version"] == original_proof["expected_version"] + + @pytest.mark.parametrize("provider", ["file", "sqlite"]) def test_unprepared_turn_does_not_become_admitted_after_deferral(tmp_path, provider): project, runtime, registry, _, _ = _source( diff --git a/tests/control_plane_ts/native_child_admission.test.ts b/tests/control_plane_ts/native_child_admission.test.ts index f92a746644..a3e570586b 100644 --- a/tests/control_plane_ts/native_child_admission.test.ts +++ b/tests/control_plane_ts/native_child_admission.test.ts @@ -259,6 +259,37 @@ test("a retained binding cannot replace fresh same-Todo executable selection", a } finally {await rm(root, {recursive: true, force: true});} }); +test("one prepared Turn admits only renewal of its original lease generation", async () => { + const {root, identity, event} = await fixture(); + try { + const originalDetails = {...event.details, prepared_lease_key_sha256: "a".repeat(64), + prepared_lease_version: 3, prepared_lease_epoch: 2}; + const original = {...event, details: originalDetails}; + const renewal = {...originalDetails, prepared_lease_version: 4}; + assert.deepEqual(heartbeatWorkRequalification(original, renewal, identity, "open", false), { + append: true, + details: {...renewal, settlement_receipt_revision: "lease_readiness"}, + }); + for (const changed of [ + {prepared_lease_epoch: 3}, + {prepared_lease_key_sha256: "b".repeat(64)}, + {prepared_lease_epoch: undefined}, + ]) { + const replacement = Object.fromEntries(Object.entries({...renewal, ...changed}) + .filter(([, value]) => value !== undefined)); + assert.throws(() => heartbeatWorkRequalification(original, replacement, identity, "open", false), + /original task lease generation/); + assert.equal(heartbeatWorkRequalification(original, replacement, identity, "settlement_pending", false).append, false); + assert.equal(heartbeatWorkRequalification(original, replacement, identity, "open", true).append, false); + } + const {prepared_lease_epoch: _, ...historicalDetails} = originalDetails; + const historical = {...event, details: historicalDetails}; + assert.equal(heartbeatWorkRequalification(historical, historicalDetails, identity, "open", false).append, false); + assert.throws(() => heartbeatWorkRequalification(historical, renewal, identity, "open", false), + /original task lease generation/); + } finally {await rm(root, {recursive: true, force: true});} +}); + test("work requalification fails closed on binding and semantic guard conflicts", async () => { const {root, identity, event} = await fixture(); try { From 6db6697447053bf0c17e0edf53fe5d1c5381fe7b Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 20 Nov 2026 06:19:59 +0800 Subject: [PATCH 3/6] Recover deferred Turn closeout with its retained original lease Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- loopx/control_plane/quota/execution_preparation.py | 9 ++++++--- .../test_quota_authority_settlement_journey.py | 11 +++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/loopx/control_plane/quota/execution_preparation.py b/loopx/control_plane/quota/execution_preparation.py index b77e382767..5a547f6ed6 100644 --- a/loopx/control_plane/quota/execution_preparation.py +++ b/loopx/control_plane/quota/execution_preparation.py @@ -214,10 +214,13 @@ def guard_first_writeback_execution( # 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 after an authoritative lifecycle transition can read + # the original retired proof from the same locked snapshot. + proof = current_proof if not todo_open and requested_proof is None else requested_proof execution_bound = ( - requested_proof is not None - and current_proof == requested_proof - and prepared_lease == prepared_lease_receipt_fields(requested_proof, lease_epoch=lease_epoch) + proof is not None + and current_proof == proof + and prepared_lease == prepared_lease_receipt_fields(proof, lease_epoch=lease_epoch) ) else: # Older admitted closeouts may predate persisted lease readiness. diff --git a/tests/control_plane/test_quota_authority_settlement_journey.py b/tests/control_plane/test_quota_authority_settlement_journey.py index 799883e4d3..7f67e14cb5 100644 --- a/tests/control_plane/test_quota_authority_settlement_journey.py +++ b/tests/control_plane/test_quota_authority_settlement_journey.py @@ -665,7 +665,8 @@ def run(*args): @pytest.mark.parametrize("provider", ["file", "sqlite"]) @pytest.mark.parametrize("transition", ["complete", "defer"]) -def test_first_writeback_refuses_generation_retired_by_later_work(tmp_path, provider, transition): +@pytest.mark.parametrize("explicit_proof", [False, True]) +def test_first_writeback_refuses_generation_retired_by_later_work(tmp_path, provider, transition, explicit_proof): project, runtime, registry, _, _ = _source( tmp_path, provider=provider, handoff_mode="hard_lease", extra=f"claimed_by={cli.AGENT_ID}", @@ -696,9 +697,11 @@ def run(*args): assert code == 0 and committed["ok"], committed journal = rollout_event_log_path(runtime, cli.GOAL_ID) before_rejection = journal.read_bytes() - code, denied = _refresh(project, runtime, registry, - "--task-lease-idempotency-key", proof["idempotency_key"], - "--task-lease-expected-version", str(proof["expected_version"])) + arguments = ( + "--task-lease-idempotency-key", proof["idempotency_key"], + "--task-lease-expected-version", str(proof["expected_version"]), + ) if explicit_proof else () + code, denied = _refresh(project, runtime, registry, *arguments) assert code == 1 and not denied["appended"], denied assert denied["error_code"] == "refresh_task_lease_unavailable", denied # The rejected operation must not change the external delivery preference. From 81739ad12764ccfc79b0815799e49158a8ae53b0 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 20 Nov 2026 06:32:22 +0800 Subject: [PATCH 4/6] Preserve canonical lease epochs through deferred Turn recovery Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../quota/execution_preparation.py | 19 ++-- .../quota/heartbeat_receipt_identity.ts | 3 +- loopx/control_plane/todos/read_transaction.ts | 4 +- .../work_items/execution_preparation.ts | 2 +- .../work_items/task_lease_acquire.ts | 97 +------------------ .../work_items/task_lease_record.ts | 94 ++++++++++++++++++ ...test_quota_authority_settlement_journey.py | 11 +++ .../test_quota_settlement_cli.py | 26 +++++ .../content_digest_single_owner.test.ts | 1 + 9 files changed, 152 insertions(+), 105 deletions(-) create mode 100644 loopx/control_plane/work_items/task_lease_record.ts diff --git a/loopx/control_plane/quota/execution_preparation.py b/loopx/control_plane/quota/execution_preparation.py index 5a547f6ed6..43e4cea981 100644 --- a/loopx/control_plane/quota/execution_preparation.py +++ b/loopx/control_plane/quota/execution_preparation.py @@ -214,14 +214,19 @@ def guard_first_writeback_execution( # 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 after an authoritative lifecycle transition can read - # the original retired proof from the same locked snapshot. + # 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 = ( - proof is not None - and current_proof == proof - and prepared_lease == prepared_lease_receipt_fields(proof, lease_epoch=lease_epoch) - ) + 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. diff --git a/loopx/control_plane/quota/heartbeat_receipt_identity.ts b/loopx/control_plane/quota/heartbeat_receipt_identity.ts index b2eed924de..4a0cb70da2 100644 --- a/loopx/control_plane/quota/heartbeat_receipt_identity.ts +++ b/loopx/control_plane/quota/heartbeat_receipt_identity.ts @@ -12,6 +12,7 @@ import type { JsonObject } from "../effect_program.ts"; import {executionRequestSubject, settlementIdentity, type SettlementIdentity, type ExecutionRequestSubject} from "../effect_program.ts"; import { EffectRuntimeRequestError } from "../effect_runtime_errors.ts"; +import { BARE_SHA256_PATTERN } from "../content_digest.ts"; import { jsonObject } from "../runtime_decode.ts"; import {nativeChildReportAdmission} from "../capabilities/native_child_admission.ts"; import type {ReceiptBoundReplayPhase} from "./settlement_phase.ts"; @@ -235,7 +236,7 @@ export function heartbeatPreparedLease(details: JsonObject): HeartbeatPreparedLe const version = details.prepared_lease_version; const epoch = details.prepared_lease_epoch; if (key === undefined && version === undefined && epoch === undefined) return null; - if (typeof key !== "string" || !/^[a-f0-9]{64}$/.test(key) || + if (typeof key !== "string" || !BARE_SHA256_PATTERN.test(key) || typeof version !== "number" || !Number.isSafeInteger(version) || version < 1 || (epoch !== undefined && (typeof epoch !== "number" || !Number.isSafeInteger(epoch) || epoch < 1))) { throw new EffectRuntimeRequestError("Turn readiness has a malformed lease proof", "malformed_settlement_state"); diff --git a/loopx/control_plane/todos/read_transaction.ts b/loopx/control_plane/todos/read_transaction.ts index bcd04563f7..9603109140 100644 --- a/loopx/control_plane/todos/read_transaction.ts +++ b/loopx/control_plane/todos/read_transaction.ts @@ -239,8 +239,8 @@ export async function readExecutionPreparationSource(input: JsonObject, dependen const todo = originalRecords(canonical.todos).find(row => row.todo_id === todoId); if (!todo) return unavailable; const leases = originalRecords(canonical.leases).filter(row => row.todo_id === todoId) - .map(({todo_id, schema_version, status, owner, expires_at, idempotency_key, version}) => - ({todo_id, schema_version, status, owner, expires_at, idempotency_key, version})); + .map(({todo_id, schema_version, status, owner, expires_at, idempotency_key, version, lease_epoch}) => + ({todo_id, schema_version, status, owner, expires_at, idempotency_key, version, lease_epoch})); return {schema_version: TODO_READ_RESULT, execution_preparation_source: { goal_id: goalId, todo_id: todoId, handoff_mode: canonical.handoff_mode, source_authority: canonical.source_authority, provider_revision: canonical.provider_revision, diff --git a/loopx/control_plane/work_items/execution_preparation.ts b/loopx/control_plane/work_items/execution_preparation.ts index 1109beb630..a942b66431 100644 --- a/loopx/control_plane/work_items/execution_preparation.ts +++ b/loopx/control_plane/work_items/execution_preparation.ts @@ -1,7 +1,7 @@ import {HANDOFF_MODES} from "../coordination/handoff_mode_vocabulary.ts"; import {HANDOFF_MODE_RETIRED_REASON, retiredHandoffModeRejection} from "../coordination/handoff_mode_policy.ts"; import type {JsonObject} from "../effect_program.ts"; -import {leaseEpoch} from "./task_lease_acquire.ts"; +import {leaseEpoch} from "./task_lease_record.ts"; import {EffectRuntimeRequestError} from "../effect_runtime_errors.ts"; import { optionalNonEmptyString, diff --git a/loopx/control_plane/work_items/task_lease_acquire.ts b/loopx/control_plane/work_items/task_lease_acquire.ts index bba12844c6..360d7a7208 100644 --- a/loopx/control_plane/work_items/task_lease_acquire.ts +++ b/loopx/control_plane/work_items/task_lease_acquire.ts @@ -9,7 +9,6 @@ import { evaluateTaskLeaseWriteScopesOverlap, } from "./task_lease_acquire_decision.ts"; import { ShadowManagementError } from "../coordination/shadow_management.ts"; -import { parseIsoTimestamp } from "../runtime_timestamp.ts"; import { createHash } from "node:crypto"; import { readFile } from "node:fs/promises"; import { join } from "node:path"; @@ -29,11 +28,12 @@ import { } from "../coordination/local_authority_shadow_outbox.ts"; import { TASK_LEASE_ACQUIRE_REQUEST_SCHEMA, TASK_LEASE_CANONICAL_ACQUIRE_REQUEST_SCHEMA } from "../coordination/coordination_state_contract.generated.ts"; import { BARE_SHA256_PATTERN } from "../content_digest.ts"; +import {TASK_LEASE_SCHEMA_VERSION, TaskLeaseAcquireError, type LeaseRecord} from "./task_lease_record.ts"; +export {TASK_LEASE_SCHEMA_VERSION, TaskLeaseAcquireError, leaseInteger, leaseVersion, + leaseEpoch, leaseIsActive, type LeaseRecord} from "./task_lease_record.ts"; export const TASK_LEASE_ACQUIRE_REQUEST_SCHEMA_VERSION = TASK_LEASE_ACQUIRE_REQUEST_SCHEMA; -export const TASK_LEASE_SCHEMA_VERSION = "task_lease_v0"; - const DEFAULT_TTL_SECONDS = 45 * 60; const MAX_TTL_SECONDS = 24 * 60 * 60; type FileState = "file" | "missing"; @@ -91,23 +91,6 @@ interface AcquireRequest { runtime_shadow: LocalAuthorityShadowBinding | null; } -export interface LeaseRecord extends JsonObject { - schema_version?: unknown; - goal_id?: unknown; - todo_id?: unknown; - owner?: unknown; - idempotency_key?: unknown; - write_scopes?: unknown; - write_repository?: unknown; - acquire_ttl_seconds?: unknown; - version?: unknown; - lease_epoch?: unknown; - status?: unknown; - acquired_at?: unknown; - updated_at?: unknown; - expires_at?: unknown; -} - interface TaskLeaseFailure { code: string; message: string; @@ -129,18 +112,6 @@ export interface TaskLeaseAcquireDependencies { export type TaskLeaseAcquireEnvelope = JsonObject; -export class TaskLeaseAcquireError extends Error { - readonly code: string; - readonly payload: JsonObject; - - constructor(message: string, code: string, payload: JsonObject = {}) { - super(message); - this.name = "TaskLeaseAcquireError"; - this.code = code; - this.payload = payload; - } -} - function stringValue(value: unknown, label: string): string { if (typeof value !== "string") { throw new EffectRuntimeRequestError(`${label} must be a string`); @@ -472,68 +443,6 @@ export async function readLease(path: string): Promise { } } -export function leaseInteger( - lease: LeaseRecord | null, - field: "version" | "lease_epoch" | "acquire_ttl_seconds", -): number | null { - const raw = lease?.[field]; - if (raw === null || raw === undefined) return null; - let number = Number.NaN; - if (typeof raw === "number") { - number = raw; - } else if (typeof raw === "string" && /^-?\d+$/u.test(raw)) { - number = Number(raw); - } - const minimum = field === "version" ? 0 : 1; - if ( - typeof raw === "boolean" || !Number.isSafeInteger(number) || number < minimum - ) { - let message = "lease acquire_ttl_seconds must be a positive integer"; - if (field === "lease_epoch") { - message = "lease epoch must be a positive integer"; - } else if (field === "version") { - message = "lease version must be a non-negative integer"; - } - throw new TaskLeaseAcquireError(message, "corrupt_lease", { [field]: raw ?? null }); - } - return number; -} - -export function leaseVersion(lease: LeaseRecord | null): number { - return leaseInteger(lease, "version") ?? 0; -} - -export function leaseEpoch(lease: LeaseRecord | null): number { - if (lease === null) return 0; - return leaseInteger(lease, "lease_epoch") ?? 1; -} - - -export function leaseIsActive(lease: LeaseRecord | null, at: Date): boolean { - if ( - lease?.schema_version !== TASK_LEASE_SCHEMA_VERSION || - lease.status !== "active" - ) { - return false; - } - if (typeof lease.expires_at !== "string") { - throw new TaskLeaseAcquireError( - "active lease expires_at must be a valid timestamp", - "corrupt_lease", - { expires_at: lease.expires_at ?? null }, - ); - } - const expiresAt = parseIsoTimestamp(lease.expires_at); - if (expiresAt === null) { - throw new TaskLeaseAcquireError( - "active lease expires_at must be a valid timestamp", - "corrupt_lease", - { expires_at: lease.expires_at }, - ); - } - return expiresAt.valueOf() > at.valueOf(); -} - export function utcIsoformat(value: Date): string { return value.toISOString().replace(/\.\d{3}Z$/u, "Z"); } diff --git a/loopx/control_plane/work_items/task_lease_record.ts b/loopx/control_plane/work_items/task_lease_record.ts new file mode 100644 index 0000000000..19109eb1a2 --- /dev/null +++ b/loopx/control_plane/work_items/task_lease_record.ts @@ -0,0 +1,94 @@ +import type {JsonObject} from "../effect_program.ts"; +import {parseIsoTimestamp} from "../runtime_timestamp.ts"; + +export const TASK_LEASE_SCHEMA_VERSION = "task_lease_v0"; + +export interface LeaseRecord extends JsonObject { + schema_version?: unknown; + goal_id?: unknown; + todo_id?: unknown; + owner?: unknown; + idempotency_key?: unknown; + write_scopes?: unknown; + write_repository?: unknown; + acquire_ttl_seconds?: unknown; + version?: unknown; + lease_epoch?: unknown; + status?: unknown; + acquired_at?: unknown; + updated_at?: unknown; + expires_at?: unknown; +} + +export class TaskLeaseAcquireError extends Error { + readonly code: string; + readonly payload: JsonObject; + + constructor(message: string, code: string, payload: JsonObject = {}) { + super(message); + this.name = "TaskLeaseAcquireError"; + this.code = code; + this.payload = payload; + } +} + +export function leaseInteger( + lease: LeaseRecord | null, + field: "version" | "lease_epoch" | "acquire_ttl_seconds", +): number | null { + const raw = lease?.[field]; + if (raw === null || raw === undefined) return null; + let number = Number.NaN; + if (typeof raw === "number") { + number = raw; + } else if (typeof raw === "string" && /^-?\d+$/u.test(raw)) { + number = Number(raw); + } + const minimum = field === "version" ? 0 : 1; + if ( + typeof raw === "boolean" || !Number.isSafeInteger(number) || number < minimum + ) { + let message = "lease acquire_ttl_seconds must be a positive integer"; + if (field === "lease_epoch") { + message = "lease epoch must be a positive integer"; + } else if (field === "version") { + message = "lease version must be a non-negative integer"; + } + throw new TaskLeaseAcquireError(message, "corrupt_lease", { [field]: raw ?? null }); + } + return number; +} + +export function leaseVersion(lease: LeaseRecord | null): number { + return leaseInteger(lease, "version") ?? 0; +} + +export function leaseEpoch(lease: LeaseRecord | null): number { + if (lease === null) return 0; + return leaseInteger(lease, "lease_epoch") ?? 1; +} + +export function leaseIsActive(lease: LeaseRecord | null, at: Date): boolean { + if ( + lease?.schema_version !== TASK_LEASE_SCHEMA_VERSION || + lease.status !== "active" + ) { + return false; + } + if (typeof lease.expires_at !== "string") { + throw new TaskLeaseAcquireError( + "active lease expires_at must be a valid timestamp", + "corrupt_lease", + { expires_at: lease.expires_at ?? null }, + ); + } + const expiresAt = parseIsoTimestamp(lease.expires_at); + if (expiresAt === null) { + throw new TaskLeaseAcquireError( + "active lease expires_at must be a valid timestamp", + "corrupt_lease", + { expires_at: lease.expires_at }, + ); + } + return expiresAt.valueOf() > at.valueOf(); +} diff --git a/tests/control_plane/test_quota_authority_settlement_journey.py b/tests/control_plane/test_quota_authority_settlement_journey.py index 7f67e14cb5..70ccead4ff 100644 --- a/tests/control_plane/test_quota_authority_settlement_journey.py +++ b/tests/control_plane/test_quota_authority_settlement_journey.py @@ -660,6 +660,17 @@ def run(*args): assert code == 0 and next_wake["decision"] == "run", next_wake assert next_wake["selected_todo"]["todo_id"] == cli.TODO_ID assert next_wake["effective_action"] != "unsettled_host_turn_recovery" + code, newer = run("todo", "claim", "--todo-id", cli.TODO_ID, "--agent-id", cli.AGENT_ID, + "--claimed-by", cli.AGENT_ID) + assert code == 0 and newer["execution_managed"], newer + code, inspected = run("task-lease", "inspect", "--todo-id", cli.TODO_ID) + assert code == 0 and inspected["active"], inspected + newer_epoch = inspected["lease"]["lease_epoch"] + assert newer_epoch > acquired["lease"]["lease_epoch"] + code, qualified = _guard(project, runtime, registry, turn_id="turn-after-policy-migration") + assert code == 0 and qualified["normal_delivery_allowed"], qualified + assert qualified["execution_preparation"]["lease_epoch"] == newer_epoch + assert qualified["heartbeat_receipt"]["settlement_identity"] == next_wake["heartbeat_receipt"]["settlement_identity"] assert cli._spend_run_count(runtime) == 1 diff --git a/tests/control_plane/test_quota_settlement_cli.py b/tests/control_plane/test_quota_settlement_cli.py index 556196ef36..9b3f26e9d4 100644 --- a/tests/control_plane/test_quota_settlement_cli.py +++ b/tests/control_plane/test_quota_settlement_cli.py @@ -5660,10 +5660,12 @@ def test_pending_deferred_p0_allows_independent_p1_selection( @pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("renew_before_wait", [False, True]) def test_same_turn_bound_p0_does_not_project_p1_after_p0_becomes_deferred( tmp_path: Path, provider: str, monkeypatch: pytest.MonkeyPatch, + renew_before_wait: bool, ) -> None: from canonical_authority_fixture import ( isolate_sqlite_runtime, @@ -5702,6 +5704,19 @@ def test_same_turn_bound_p0_does_not_project_p1_after_p0_becomes_deferred( assert prepared["heartbeat_receipt"]["settlement_identity"] == ( first["heartbeat_receipt"]["settlement_identity"] ) + original_proof = prepared["execution_preparation"]["lease_proof"] + lease_version = original_proof["expected_version"] + if renew_before_wait: + renew_rc, renewed = _run_cli( + registry_path, runtime, "task-lease", "renew", "--goal-id", GOAL_ID, + "--todo-id", TODO_ID, "--owner", AGENT_ID, + "--idempotency-key", original_proof["idempotency_key"], + "--expected-version", str(lease_version), "--ttl-seconds", "3600", + ) + assert renew_rc == 0 and renewed["renewed"], renewed + assert renewed["lease"]["idempotency_key"] == original_proof["idempotency_key"] + assert renewed["lease"]["version"] > lease_version + lease_version = renewed["lease"]["version"] # This result exists before the lifecycle wait; recovery must account for # it without executing more work or rebinding the original Turn. artifact = project / "verified-before-wait.txt" @@ -5723,6 +5738,8 @@ def test_same_turn_bound_p0_does_not_project_p1_after_p0_becomes_deferred( "--todo-id", TODO_ID, ) assert inspect_rc == 0 and inspected["lease"]["status"] == "released", inspected + assert inspected["lease"]["version"] == lease_version + assert inspected["lease"]["idempotency_key"] == original_proof["idempotency_key"] replay_rc, replay = _run_cli(registry_path, runtime, *guard) assert replay_rc == 0, replay assert replay["effective_action"] == "unsettled_host_turn_recovery" @@ -5777,6 +5794,15 @@ def test_same_turn_bound_p0_does_not_project_p1_after_p0_becomes_deferred( write_args = ("refresh-state", *binding, "--classification", "verified_before_wait", "--delivery-batch-scale", "single_surface", "--delivery-outcome", "outcome_progress", "--agent-vision-json", str(vision), "--no-global-sync", "--suppress-external-sinks") + if renew_before_wait: + stale_rc, stale = _run_cli( + registry_path, runtime, *write_args, + "--task-lease-idempotency-key", original_proof["idempotency_key"], + "--task-lease-expected-version", str(original_proof["expected_version"]), cwd=project, + ) + assert stale_rc == 1 and stale["error_code"] == "refresh_task_lease_unavailable", stale + assert stale["appended"] is False + assert _classification_count(runtime, "verified_before_wait") == _spend_run_count(runtime) == 0 rc, written = _run_cli(registry_path, runtime, *write_args, cwd=project) assert rc == 0, written rc, replayed_write = _run_cli(registry_path, runtime, *write_args, cwd=project) diff --git a/tests/control_plane_ts/content_digest_single_owner.test.ts b/tests/control_plane_ts/content_digest_single_owner.test.ts index 1ad78cc4f2..eccc73e8ae 100644 --- a/tests/control_plane_ts/content_digest_single_owner.test.ts +++ b/tests/control_plane_ts/content_digest_single_owner.test.ts @@ -162,6 +162,7 @@ const CANONICAL_CONSUMERS = [ "control_plane/presentation/action_review_plan.ts", "control_plane/presentation/goal_source_links.ts", "control_plane/presentation/report_review.ts", + "control_plane/quota/heartbeat_receipt_identity.ts", "control_plane/quota/refresh_external_delivery.ts", "control_plane/quota/settlement_readback.ts", "control_plane/quota/turn_envelope.ts", From 5744a1893783b608601c9edc45923e1415b50b72 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 20 Nov 2026 06:57:18 +0800 Subject: [PATCH 5/6] Fence prepared blocked Turn writebacks with their original lease Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- loopx/cli_commands/turn_run_once.py | 1 + .../control_plane/quota/heartbeat_receipt.py | 3 +- loopx/control_plane/quota/turn_envelope.ts | 5 +- loopx/state_refresh.py | 9 ++- ...test_quota_authority_settlement_journey.py | 67 ++++++++++++++++++- tests/control_plane/test_quota_settlement.py | 21 ++++++ tests/control_plane_ts/turn_envelope.test.ts | 6 ++ 7 files changed, 107 insertions(+), 5 deletions(-) diff --git a/loopx/cli_commands/turn_run_once.py b/loopx/cli_commands/turn_run_once.py index f243972b84..f43ddb5062 100644 --- a/loopx/cli_commands/turn_run_once.py +++ b/loopx/cli_commands/turn_run_once.py @@ -765,6 +765,7 @@ def on_managed_start_admitted() -> None: ), goal_ref=goal_ref, task_lease_proof=task_lease_proof, + task_lease_epoch=execution_context.get("task_lease_epoch"), ) managed_cadence = managed_cadence_start( diff --git a/loopx/control_plane/quota/heartbeat_receipt.py b/loopx/control_plane/quota/heartbeat_receipt.py index 52ceb47a56..3874436caa 100644 --- a/loopx/control_plane/quota/heartbeat_receipt.py +++ b/loopx/control_plane/quota/heartbeat_receipt.py @@ -207,6 +207,7 @@ def ensure_turn_heartbeat_settlement_receipt( semantic_replan_capability_guard: Mapping[str, object] | None = None, quota_spend_source: str | None = None, task_lease_proof: Mapping[str, object] | None = None, + task_lease_epoch: int | None = None, prepared_lease_required: bool = False, goal_ref: Mapping[str, object] | None = None, before_bind: Callable[[], None] | None = None, @@ -323,7 +324,7 @@ def ensure_turn_heartbeat_settlement_receipt( if task_lease_proof is not None and identity.todo_id is not None: from .execution_preparation import prepared_lease_receipt_fields - details.update(prepared_lease_receipt_fields(task_lease_proof)) + details.update(prepared_lease_receipt_fields(task_lease_proof, lease_epoch=task_lease_epoch)) if semantic_replan_guard_scoped: details["semantic_replan_obligation_id"] = ( normalized_semantic_replan_obligation_id or "" diff --git a/loopx/control_plane/quota/turn_envelope.ts b/loopx/control_plane/quota/turn_envelope.ts index acae4d618b..a39e9e5ba9 100644 --- a/loopx/control_plane/quota/turn_envelope.ts +++ b/loopx/control_plane/quota/turn_envelope.ts @@ -1,4 +1,5 @@ import {EXECUTION_PREPARATION_SCHEMA_VERSION} from "../work_items/execution_preparation.ts"; +import {leaseEpoch} from "../work_items/task_lease_record.ts"; import {capturedContextSelection} from "./context_selection.ts"; import {contextReadSectionDigests, initialContextReadPointers, prepareProgressiveContext, prepareProgressiveContextDelivery, progressiveContextPlan, scopedReplanYardstickReversal, @@ -2700,11 +2701,13 @@ export function evaluateTurnEnvelope(value: unknown): JsonObject { const work = {selected_todo: object(projection.action).selected_todo, task_orchestration_contract: projection.task_orchestration_contract}; const sha256 = completeTaskTextSha256(decision, selectedWorkTodoId(work) ?? ""); + const preparation = object(decision.execution_preparation); return {selected_todo: selectedWorkTodo(work), sha256, selected_work_basis: selectedWorkLaunchBasis(decision, sha256), effective_action: decision.effective_action ?? null, writeback: object(projection.writeback), - task_lease_proof: object(object(decision.execution_preparation).lease_proof), + task_lease_proof: object(preparation.lease_proof), + ...(preparation.lease_epoch == null ? {} : {task_lease_epoch: leaseEpoch(preparation)}), replan_guard_scoped: Object.hasOwn(projection, "replan_action_packet"), replan_action_packet: projection.replan_action_packet ?? null}; } diff --git a/loopx/state_refresh.py b/loopx/state_refresh.py index e661917587..28d6e10900 100644 --- a/loopx/state_refresh.py +++ b/loopx/state_refresh.py @@ -1599,7 +1599,14 @@ def read_explore_context(read_goal_id: str, read_agent_id: str | None): if not dry_run: if ( settlement_identity is not None and settlement_identity.todo_id - and not checkpoint_supplement and blocked_retry is None + and not checkpoint_supplement + # An administrative closeout can report inability to start. + # A prepared Turn's blocker is still a first writeback. + and ( + blocked_retry is None + or task_lease_proof is not None + or (settlement_readback is not None and settlement_readback.prepared_lease is not None) + ) ): usage_booking_guard.enter_context(guard_first_writeback_execution( runtime_root=runtime_root, registry_path=registry_path, goal_id=safe_goal_id, diff --git a/tests/control_plane/test_quota_authority_settlement_journey.py b/tests/control_plane/test_quota_authority_settlement_journey.py index 70ccead4ff..02995e599e 100644 --- a/tests/control_plane/test_quota_authority_settlement_journey.py +++ b/tests/control_plane/test_quota_authority_settlement_journey.py @@ -194,11 +194,11 @@ def test_failed_canonical_read_cannot_fall_back_to_markdown(tmp_path): -def _refresh(project: Path, runtime: Path, registry: Path, *extra: str): +def _refresh(project: Path, runtime: Path, registry: Path, *extra: str, outcome: str = "outcome_progress"): return cli._run_cli( registry, runtime, "refresh-state", "--goal-id", cli.GOAL_ID, "--classification", "validated_progress", "--delivery-batch-scale", "implementation", - "--delivery-outcome", "outcome_progress", "--agent-id", cli.AGENT_ID, + "--delivery-outcome", outcome, "--agent-id", cli.AGENT_ID, "--todo-id", cli.TODO_ID, "--turn-instance-id", cli.TURN_ID, "--no-global-sync", "--suppress-external-sinks", *extra, cwd=project, ) @@ -725,6 +725,69 @@ def run(*args): assert cli._spend_run_count(runtime) == 0 +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("replace_lease", [False, True]) +@pytest.mark.parametrize("retire_for_blocker", [False, True]) +def test_first_blocked_writeback_checks_original_lease( + tmp_path, provider, replace_lease, retire_for_blocker, +): + project, runtime, registry, _, _ = _source( + tmp_path, provider=provider, handoff_mode="hard_lease", + extra=f"claimed_by={cli.AGENT_ID}", + ) + scope = ("--goal-id", cli.GOAL_ID, "--todo-id", cli.TODO_ID) + + def run(*args): + return cli._run_cli(registry, runtime, *args, *scope, cwd=project) + + code, claim = run("todo", "claim", "--agent-id", cli.AGENT_ID, "--claimed-by", cli.AGENT_ID) + assert code == 0 and claim["execution_managed"], claim + code, guard = _guard(project, runtime, registry) + assert code == 0 and guard["normal_delivery_allowed"], guard + identity = guard["heartbeat_receipt"]["settlement_identity"] + proof = guard["execution_preparation"]["lease_proof"] + proof_args = ( + "--task-lease-idempotency-key", proof["idempotency_key"], + "--task-lease-expected-version", str(proof["expected_version"]), + ) + if replace_lease: + code, released = run("task-lease", "release", "--owner", cli.AGENT_ID, + "--idempotency-key", proof["idempotency_key"], + "--expected-version", str(proof["expected_version"])) + assert code == 0 and released["released"], released + prior_claim = claim["claim_operation_id"] + code, claim = run("todo", "claim", "--agent-id", cli.AGENT_ID, "--claimed-by", cli.AGENT_ID) + assert code == 0 and claim["claim_operation_id"] != prior_claim, claim + if retire_for_blocker: + code, updated = run("todo", "update", "--status", "blocked", "--agent-id", cli.AGENT_ID, + "--reason", "The fixture's dependency requires a later decision.", + "--claim-operation-id", claim["claim_operation_id"]) + assert code == 0 and updated["ok"], updated + arguments = ( + "--progress-result-class", "blocked", "--progress-blocker-id", "blocker:fixture-dependency", + "--progress-evidence-id", "evidence:fixture-dependency", "--settle-turn", "--settlement-receipt", + ) + journal = rollout_event_log_path(runtime, cli.GOAL_ID) + before = journal.read_bytes() + if replace_lease: + for supplied_proof in ((), proof_args): + code, refused = _refresh(project, runtime, registry, *arguments, *supplied_proof, outcome="outcome_gap") + assert code == 1 and refused["error_code"] == "refresh_task_lease_unavailable", refused + assert refused["appended"] is False + assert journal.read_bytes() == before + assert cli._classification_count(runtime, "validated_progress") == 0 + else: + arguments += () if retire_for_blocker else proof_args + code, closed = _refresh(project, runtime, registry, *arguments, outcome="outcome_gap") + assert code == 0 and closed["settlement_identity"] == identity, closed + assert closed["settlement_progress"]["closeout_kind"] == "typed_blocked_writeback_no_spend" + code, replay = _refresh(project, runtime, registry, *arguments, outcome="outcome_gap") + assert code == 0 and replay["idempotent_replay"] and not replay["appended"], replay + assert closed["settlement_result"] == replay["settlement_result"] + assert cli._classification_count(runtime, "validated_progress") == 1 + assert cli._spend_run_count(runtime) == 0 + + @pytest.mark.parametrize("provider", ["file", "sqlite"]) def test_original_turn_refuses_readiness_from_a_replacement_lease(tmp_path, provider): project, runtime, registry, _, _ = _source( diff --git a/tests/control_plane/test_quota_settlement.py b/tests/control_plane/test_quota_settlement.py index ed2651f365..3053e19960 100644 --- a/tests/control_plane/test_quota_settlement.py +++ b/tests/control_plane/test_quota_settlement.py @@ -437,6 +437,27 @@ def test_turn_guard_remains_readable_after_a_torn_rollout_tail( assert readback["event_id"] == written["event_id"] +@pytest.mark.parametrize("lease_epoch", [None, 3]) +def test_managed_turn_binding_preserves_qualified_lease_epoch(tmp_path: Path, lease_epoch: int | None) -> None: + runtime_root = tmp_path / "runtime" + identity = SettlementIdentity(GOAL_ID, AGENT_ID, TODO_ID, TURN_ID) + receipt = ensure_turn_heartbeat_settlement_receipt( + runtime_root, identity, semantic_replan_guard_scoped=False, + semantic_replan_obligation_id=None, + task_lease_proof={"idempotency_key": "fixture-managed-lease", "expected_version": 7}, + task_lease_epoch=lease_epoch, + ) + assert receipt["details"]["prepared_lease_version"] == 7 + assert receipt["details"].get("prepared_lease_epoch") == lease_epoch + readback = read_heartbeat_settlement( + runtime_root, goal_id=GOAL_ID, agent_id=AGENT_ID, todo_id=TODO_ID, turn_instance_id=TURN_ID, + ) + assert readback is not None and readback.prepared_lease_required + assert readback.prepared_lease is not None + assert readback.prepared_lease["prepared_lease_version"] == 7 + assert readback.prepared_lease.get("prepared_lease_epoch") == lease_epoch + + def test_turn_guard_refuses_to_change_an_existing_semantic_selection( tmp_path: Path, ) -> None: diff --git a/tests/control_plane_ts/turn_envelope.test.ts b/tests/control_plane_ts/turn_envelope.test.ts index 64d16ff6cc..42e8a4e754 100644 --- a/tests/control_plane_ts/turn_envelope.test.ts +++ b/tests/control_plane_ts/turn_envelope.test.ts @@ -1880,6 +1880,10 @@ test("complete execution reads one original selection, task commitment and effec complete: true, selected_todo_ref: "selected_todo", selected_todo_text_verified: true}; (interaction.cli_channel as JsonObject).delivery_workspace_causality = { requirement: "required", future: {empty: [], approved: false}}; + const proof = {idempotency_key: "managed-fixture-lease", expected_version: 7}; + source.execution_preparation = {schema_version: "execution_preparation_v0", state: "ready", + delivery_allowed: true, handoff_mode: "hard_lease", goal_id: source.goal_id, agent_id: source.agent_id, + todo_id: selected.todo_id, lease_proof: proof, lease_epoch: 3}; const contract = evaluateTurnEnvelope({operation: "compose_complete", payload: source, scheduler_execution_args: ""}); const envelope = buildTurnEnvelope({payload: contract, scheduler_execution_args: ""}); const before = structuredClone({contract, envelope}); @@ -1889,6 +1893,8 @@ test("complete execution reads one original selection, task commitment and effec assert.equal((result.selected_todo as JsonObject).text, selected.text); assert.equal(result.sha256, createHash("sha256").update(String(selected.text), "utf8").digest("hex")); assert.equal(result.effective_action, "normal_run"); + assert.deepEqual(result.task_lease_proof, proof); + assert.equal(result.task_lease_epoch, 3); assert.deepEqual((result.writeback as JsonObject).delivery_workspace_causality, (interaction.cli_channel as JsonObject).delivery_workspace_causality); assert.equal(result.replan_guard_scoped, true); From 55c5d256f9741a403f0c6f17976cc05cd4f2fbd2 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 20 Nov 2026 07:30:23 +0800 Subject: [PATCH 6/6] Reuse the hard-lease settlement test fixture Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- ...test_quota_authority_settlement_journey.py | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/tests/control_plane/test_quota_authority_settlement_journey.py b/tests/control_plane/test_quota_authority_settlement_journey.py index 02995e599e..6d6e0c259c 100644 --- a/tests/control_plane/test_quota_authority_settlement_journey.py +++ b/tests/control_plane/test_quota_authority_settlement_journey.py @@ -674,10 +674,8 @@ def run(*args): assert cli._spend_run_count(runtime) == 1 -@pytest.mark.parametrize("provider", ["file", "sqlite"]) -@pytest.mark.parametrize("transition", ["complete", "defer"]) -@pytest.mark.parametrize("explicit_proof", [False, True]) -def test_first_writeback_refuses_generation_retired_by_later_work(tmp_path, provider, transition, explicit_proof): +@pytest.fixture +def hard_lease_todo_cli(tmp_path, provider): project, runtime, registry, _, _ = _source( tmp_path, provider=provider, handoff_mode="hard_lease", extra=f"claimed_by={cli.AGENT_ID}", @@ -687,6 +685,17 @@ def test_first_writeback_refuses_generation_retired_by_later_work(tmp_path, prov def run(*args): return cli._run_cli(registry, runtime, *args, *scope, cwd=project) + return project, runtime, registry, run + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("transition", ["complete", "defer"]) +@pytest.mark.parametrize("explicit_proof", [False, True]) +def test_first_writeback_refuses_generation_retired_by_later_work( + hard_lease_todo_cli, transition, explicit_proof, +): + project, runtime, registry, run = hard_lease_todo_cli + code, claim = run("todo", "claim", "--agent-id", cli.AGENT_ID, "--claimed-by", cli.AGENT_ID) assert code == 0 and claim["ok"], claim code, guard = _guard(project, runtime, registry) @@ -729,16 +738,9 @@ def run(*args): @pytest.mark.parametrize("replace_lease", [False, True]) @pytest.mark.parametrize("retire_for_blocker", [False, True]) def test_first_blocked_writeback_checks_original_lease( - tmp_path, provider, replace_lease, retire_for_blocker, + hard_lease_todo_cli, replace_lease, retire_for_blocker, ): - project, runtime, registry, _, _ = _source( - tmp_path, provider=provider, handoff_mode="hard_lease", - extra=f"claimed_by={cli.AGENT_ID}", - ) - scope = ("--goal-id", cli.GOAL_ID, "--todo-id", cli.TODO_ID) - - def run(*args): - return cli._run_cli(registry, runtime, *args, *scope, cwd=project) + project, runtime, registry, run = hard_lease_todo_cli code, claim = run("todo", "claim", "--agent-id", cli.AGENT_ID, "--claimed-by", cli.AGENT_ID) assert code == 0 and claim["execution_managed"], claim @@ -789,15 +791,8 @@ def run(*args): @pytest.mark.parametrize("provider", ["file", "sqlite"]) -def test_original_turn_refuses_readiness_from_a_replacement_lease(tmp_path, provider): - project, runtime, registry, _, _ = _source( - tmp_path, provider=provider, handoff_mode="hard_lease", - extra=f"claimed_by={cli.AGENT_ID}", - ) - scope = ("--goal-id", cli.GOAL_ID, "--todo-id", cli.TODO_ID) - - def run(*args): - return cli._run_cli(registry, runtime, *args, *scope, cwd=project) +def test_original_turn_refuses_readiness_from_a_replacement_lease(hard_lease_todo_cli): + project, runtime, registry, run = hard_lease_todo_cli code, claim = run("todo", "claim", "--agent-id", cli.AGENT_ID, "--claimed-by", cli.AGENT_ID) assert code == 0 and claim["ok"], claim