@@ -581,6 +581,7 @@ def __init__(
581581 self._durable_linear_snapshot: tuple[Any, ...] = ()
582582 self._durable_program_snapshot: tuple[ProgramCheckpoint, ...] = ()
583583 self._durable_pending_snapshot: Optional[Any] = None
584+ self._active_delivery_acknowledgers: tuple[Any, ...] = ()
584585 self._durable_continuation_guard: Optional[Any] = None
585586 # API/tool actuator -- the TOP of the capability ladder (RFC section 4
586587 # `api` tier). When set, a step carrying an `api_binding` has its write
@@ -1183,15 +1184,36 @@ def run(
11831184 ):
11841185 from openadapt_flow.runtime.durable.authority import DurableAuthority
11851186
1187+ managed_durable_run = durable_run
1188+
11861189 class _ManagedInitialDeliveryGuard:
1190+ def __init__(self_nonlocal) -> None:
1191+ self_nonlocal.authority = DurableAuthority(
1192+ run_dir, managed_durable_run.store
1193+ )
1194+ self_nonlocal.pending_remote_permit: Any = None
1195+
11871196 def before_delivery(self_nonlocal) -> None:
1188- assert durable_run is not None
1189- DurableAuthority(
1190- run_dir, durable_run.store
1191- ).before_initial_delivery(
1192- durable_run._manifest # noqa: SLF001 - exact retained manifest
1197+ if self_nonlocal.pending_remote_permit is not None:
1198+ raise RuntimeError(
1199+ "a prior production delivery lacks an acknowledgment receipt"
1200+ )
1201+ self_nonlocal.pending_remote_permit = (
1202+ self_nonlocal.authority.before_initial_delivery(
1203+ managed_durable_run._manifest # noqa: SLF001 - exact retained manifest
1204+ )
11931205 )
11941206
1207+ def acknowledge_delivery(self_nonlocal) -> None:
1208+ pending = self_nonlocal.pending_remote_permit
1209+ if pending is None:
1210+ return
1211+ self_nonlocal.authority.acknowledge_remote_delivery(
1212+ managed_durable_run._manifest, # noqa: SLF001
1213+ pending,
1214+ )
1215+ self_nonlocal.pending_remote_permit = None
1216+
11951217 self._durable_initial_delivery_guard = _ManagedInitialDeliveryGuard()
11961218 (run_dir / "steps").mkdir(parents=True, exist_ok=True)
11971219
@@ -8790,8 +8812,8 @@ def _resolve_drag_end(
87908812 )
87918813 return resolution, region, error
87928814
8793- @staticmethod
87948815 def _deliver_backend_call(
8816+ self,
87958817 result: StepResult,
87968818 call: Callable[[], _DeliveryResultT],
87978819 ) -> _DeliveryResultT:
@@ -8804,6 +8826,8 @@ def _deliver_backend_call(
88048826 """
88058827
88068828 attempted_before = result.delivery_attempted
8829+ acknowledgers = self._active_delivery_acknowledgers
8830+ self._active_delivery_acknowledgers = ()
88078831 try:
88088832 delivered = call()
88098833 except (FreshActuationRequired, StructuralResolutionRefused):
@@ -8813,6 +8837,8 @@ def _deliver_backend_call(
88138837 result.delivery_attempted = True
88148838 raise
88158839 result.delivery_attempted = True
8840+ for guard in acknowledgers:
8841+ guard.acknowledge_delivery()
88168842 return delivered
88178843
88188844 @staticmethod
@@ -9222,6 +9248,8 @@ def _delivery_authorization_refusal(
92229248 ) -> Optional[str]:
92239249 """Recheck exact authority at the last point before input delivery."""
92249250
9251+ self._active_delivery_acknowledgers = ()
9252+ acknowledgers: list[Any] = []
92259253 refusal = self._managed_dispatch_refusal()
92269254 if refusal is None:
92279255 refusal = self._fresh_actuation_authorization_refusal(
@@ -9231,6 +9259,7 @@ def _delivery_authorization_refusal(
92319259 if refusal is None and initial_guard is not None:
92329260 try:
92339261 initial_guard.before_delivery()
9262+ acknowledgers.append(initial_guard)
92349263 except Exception as exc: # noqa: BLE001 - durable fencing boundary
92359264 refusal = (
92369265 f"managed initial delivery was preempted before delivery: {exc}"
@@ -9239,6 +9268,7 @@ def _delivery_authorization_refusal(
92399268 if refusal is None and self._durable_continuation_guard is not None:
92409269 try:
92419270 self._durable_continuation_guard.before_delivery()
9271+ acknowledgers.append(self._durable_continuation_guard)
92429272 except Exception as exc: # noqa: BLE001 - durable fencing boundary
92439273 refusal = f"durable continuation was preempted before delivery: {exc}"
92449274 result.failure_category = "continuation_preempted"
@@ -9252,6 +9282,8 @@ def _delivery_authorization_refusal(
92529282 if self.governed_authorization is not None
92539283 else "safety_halt"
92549284 )
9285+ else:
9286+ self._active_delivery_acknowledgers = tuple(acknowledgers)
92559287 return refusal
92569288
92579289 def _act(
@@ -10731,6 +10763,9 @@ def _handle_interstitials(
1073110763 # well: every backend input edge must cross the same lease.
1073210764 try:
1073310765 self._durable_continuation_guard.before_delivery()
10766+ self._active_delivery_acknowledgers = (
10767+ self._durable_continuation_guard,
10768+ )
1073410769 except Exception as exc: # noqa: BLE001 - fencing boundary
1073510770 result.failure_category = "continuation_preempted"
1073610771 result.safety_halt = True
0 commit comments