lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

push.rs (60978B)


      1 //! Signing, durable enqueue, delivery, and satisfaction orchestration.
      2 
      3 use core::num::{NonZeroU32, NonZeroU64};
      4 use radroots_event::admission::RawEvent;
      5 use radroots_event_codec::{
      6     authoring::AuthoredEventPlan,
      7     verify::{self, Nip01SignatureVerifier},
      8 };
      9 use radroots_protocol::runtime::v1::OperationId;
     10 use radroots_signing::{
     11     Actor, AuthoredArtifactId as SigningArtifactId, SigningIntentId, SigningOperationId,
     12     recovery::{RecoveryDisposition, ReplayCapability, recovery_disposition},
     13     request::{CancellationPolicy, SignPolicy},
     14 };
     15 use radroots_storage::{
     16     atomic::{AtomicCommitDigest, AtomicCommitDisposition},
     17     authored::{
     18         AdmissionState, AuthoredArtifact, AuthoredArtifactId, AuthoredOperation, FailureClass,
     19         OperationSettlement, RetrySchedule, SigningState, WorkClaim, WorkFailure, WorkPhase,
     20     },
     21     authored_atomic::{
     22         ApplyAdmissionResult, ApplyWorkFailure, AuthoredAtomicCommand, AuthoredAtomicOutcome,
     23         AuthoredWorkTarget, CancelAuthoredTarget, CancelAuthoredWork, ClaimAuthoredTarget,
     24         ClaimAuthoredWork, PrepareAuthoredOperation, RecordSignedArtifact, WorkFence,
     25     },
     26     authored_delivery::{
     27         AuthoredDeliveryHistory, AuthoredDeliveryIntent, AuthoredDeliveryPlan,
     28         AuthoredDeliveryPlanId, AuthoredDeliveryState, DELIVERY_PLAN_ATTEMPTS_MAX,
     29         DeliveryAttemptOutcome,
     30     },
     31     event::{AdmissionDisposition, EventAdmission},
     32     journal::{IdempotencyKey, OperationInstanceId},
     33 };
     34 use radroots_transport::{
     35     DeliveryRequest, SinkFailure, Target, TransportId,
     36     outcome::Retryability,
     37     policy::{SatisfactionPolicy, SatisfactionState},
     38     source::{EventProvenance, ObservedEvent},
     39     target::TargetSet,
     40 };
     41 use sha2::{Digest, Sha256};
     42 
     43 use crate::{
     44     Engine,
     45     ingest::{AdmissionDecision, AdmissionPolicy, RegistryPolicy},
     46     policy::{Error, OperationKind, SyncId},
     47 };
     48 
     49 mod admission;
     50 mod delivery;
     51 
     52 const SIGNING_CLAIM_OWNER_EXACT: &str = "radroots-sync-signing-exact";
     53 const SIGNING_CLAIM_OWNER_LOCAL: &str = "radroots-sync-signing-local";
     54 const SIGNING_CLAIM_OWNER_NON_REPLAYABLE: &str = "radroots-sync-signing-non-replayable";
     55 const ADMISSION_CLAIM_OWNER: &str = "radroots-sync-admission";
     56 const DELIVERY_CLAIM_OWNER: &str = "radroots-sync-delivery";
     57 const WORK_RETRY_DELAY_MS: u64 = 1_000;
     58 
     59 /// Caller-owned, replay-stable inputs for one outbound operation.
     60 #[derive(Clone)]
     61 pub struct PushRequest {
     62     operation_id: SyncId,
     63     idempotency_key: IdempotencyKey,
     64     actor: Actor,
     65     plan: AuthoredEventPlan,
     66     targets: TargetSet,
     67     satisfaction: SatisfactionPolicy,
     68     delivery_deadline_unix_ms: u64,
     69     cancellation: CancellationPolicy,
     70 }
     71 
     72 impl PushRequest {
     73     #[allow(clippy::too_many_arguments)]
     74     pub fn new(
     75         operation_id: SyncId,
     76         idempotency_key: IdempotencyKey,
     77         actor: Actor,
     78         plan: AuthoredEventPlan,
     79         targets: TargetSet,
     80         satisfaction: SatisfactionPolicy,
     81         delivery_deadline_unix_ms: u64,
     82         cancellation: CancellationPolicy,
     83     ) -> Result<Self, Error> {
     84         if satisfaction.validate_for(&targets).is_err()
     85             || delivery_deadline_unix_ms == 0
     86             || actor.public_key() != *plan.author()
     87         {
     88             return Err(Error::InvalidPushRequest);
     89         }
     90         Ok(Self {
     91             operation_id,
     92             idempotency_key,
     93             actor,
     94             plan,
     95             targets,
     96             satisfaction,
     97             delivery_deadline_unix_ms,
     98             cancellation,
     99         })
    100     }
    101 
    102     /// Builds the existing pure preparation at a caller-captured stable time.
    103     /// No storage, signer, clock or network is invoked. Retain this value for
    104     /// atomic submission replay, which compares captured timestamps exactly.
    105     pub fn authored_preparation(
    106         &self,
    107         captured_at_unix_ms: u64,
    108     ) -> Result<PrepareAuthoredOperation, Error> {
    109         let (operation_id, artifact_id, delivery_plan_id) = authored_ids(self.operation_id)?;
    110         let operation =
    111             AuthoredOperation::new(operation_id, vec![artifact_id], captured_at_unix_ms)
    112                 .map_err(map_storage_error)?;
    113         let artifact = AuthoredArtifact::planned(
    114             artifact_id,
    115             operation_id,
    116             0,
    117             self.plan(),
    118             captured_at_unix_ms,
    119         )
    120         .map_err(map_storage_error)?;
    121         let intent = AuthoredDeliveryIntent::new(
    122             delivery_request_id(self.operation_id),
    123             self.targets.clone(),
    124             self.satisfaction.clone(),
    125             self.delivery_deadline_unix_ms,
    126         )
    127         .map_err(map_storage_error)?;
    128         let delivery_plan =
    129             AuthoredDeliveryPlan::new(delivery_plan_id, artifact_id, intent, captured_at_unix_ms)
    130                 .map_err(map_storage_error)?;
    131         PrepareAuthoredOperation::new(
    132             operation,
    133             vec![artifact],
    134             vec![delivery_plan],
    135             authored_push_input_digest(self)?,
    136             captured_at_unix_ms,
    137         )
    138         .map_err(map_storage_error)
    139     }
    140 
    141     pub const fn operation_id(&self) -> SyncId {
    142         self.operation_id
    143     }
    144     pub const fn idempotency_key(&self) -> &IdempotencyKey {
    145         &self.idempotency_key
    146     }
    147     pub const fn actor(&self) -> &Actor {
    148         &self.actor
    149     }
    150     pub const fn plan(&self) -> &AuthoredEventPlan {
    151         &self.plan
    152     }
    153     pub const fn targets(&self) -> &TargetSet {
    154         &self.targets
    155     }
    156     pub const fn satisfaction(&self) -> &SatisfactionPolicy {
    157         &self.satisfaction
    158     }
    159     pub const fn delivery_deadline_unix_ms(&self) -> u64 {
    160         self.delivery_deadline_unix_ms
    161     }
    162     pub const fn cancellation(&self) -> CancellationPolicy {
    163         self.cancellation
    164     }
    165 }
    166 
    167 impl core::fmt::Debug for PushRequest {
    168     fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    169         formatter
    170             .debug_struct("PushRequest")
    171             .field("operation_id", &self.operation_id)
    172             .field("idempotency_key", &self.idempotency_key)
    173             .field("actor", &self.actor)
    174             .field("plan", &"[redacted exact authored plan]")
    175             .field("targets", &self.targets)
    176             .field("satisfaction", &self.satisfaction)
    177             .field("delivery_deadline_unix_ms", &self.delivery_deadline_unix_ms)
    178             .field("cancellation", &self.cancellation)
    179             .finish()
    180     }
    181 }
    182 
    183 /// Complete durable intent created before any signer or transport effect.
    184 #[derive(Clone, Debug, Eq, PartialEq)]
    185 pub struct PushPreparation {
    186     operation: AuthoredOperation,
    187     artifact: AuthoredArtifact,
    188     delivery_plan: AuthoredDeliveryPlan,
    189     replay: bool,
    190 }
    191 
    192 impl PushPreparation {
    193     pub const fn operation(&self) -> &AuthoredOperation {
    194         &self.operation
    195     }
    196     pub const fn artifact(&self) -> &AuthoredArtifact {
    197         &self.artifact
    198     }
    199     pub const fn delivery_plan(&self) -> &AuthoredDeliveryPlan {
    200         &self.delivery_plan
    201     }
    202     pub const fn is_replay(&self) -> bool {
    203         self.replay
    204     }
    205 }
    206 
    207 /// Current durable authored state for one push operation.
    208 #[derive(Clone, Debug, Eq, PartialEq)]
    209 pub struct PushStatus {
    210     operation: AuthoredOperation,
    211     artifact: AuthoredArtifact,
    212     delivery_history: AuthoredDeliveryHistory,
    213     settlement: OperationSettlement,
    214 }
    215 
    216 impl PushStatus {
    217     pub const fn operation(&self) -> &AuthoredOperation {
    218         &self.operation
    219     }
    220     pub const fn artifact(&self) -> &AuthoredArtifact {
    221         &self.artifact
    222     }
    223     pub const fn delivery_plan(&self) -> &AuthoredDeliveryPlan {
    224         self.delivery_history.plan()
    225     }
    226     /// Consistent original-claim history; missing provenance remains unknown.
    227     pub const fn delivery_history(&self) -> &AuthoredDeliveryHistory {
    228         &self.delivery_history
    229     }
    230     pub const fn settlement(&self) -> OperationSettlement {
    231         self.settlement
    232     }
    233 }
    234 
    235 /// Durable result of one bounded signing execution.
    236 #[derive(Clone, Debug, Eq, PartialEq)]
    237 pub struct SigningRunReceipt {
    238     artifact: AuthoredArtifact,
    239     replay: bool,
    240 }
    241 
    242 impl SigningRunReceipt {
    243     pub const fn artifact(&self) -> &AuthoredArtifact {
    244         &self.artifact
    245     }
    246     pub const fn is_replay(&self) -> bool {
    247         self.replay
    248     }
    249 }
    250 
    251 /// Durable result of one bounded local-admission execution.
    252 #[derive(Clone, Debug, Eq, PartialEq)]
    253 pub struct AdmissionRunReceipt {
    254     artifact: AuthoredArtifact,
    255     replay: bool,
    256 }
    257 
    258 /// Durable result of one bounded authored-delivery execution.
    259 #[derive(Clone, Debug, Eq, PartialEq)]
    260 pub struct DeliveryExecutionReceipt {
    261     plan: AuthoredDeliveryPlan,
    262     replay: bool,
    263 }
    264 
    265 /// Durable result of cancelling all still-cancellable phases of one push.
    266 #[derive(Clone, Debug, Eq, PartialEq)]
    267 pub struct PushCancellationReceipt {
    268     status: PushStatus,
    269     changed: bool,
    270 }
    271 
    272 impl PushCancellationReceipt {
    273     pub const fn status(&self) -> &PushStatus {
    274         &self.status
    275     }
    276     pub const fn changed(&self) -> bool {
    277         self.changed
    278     }
    279 }
    280 
    281 impl DeliveryExecutionReceipt {
    282     pub const fn plan(&self) -> &AuthoredDeliveryPlan {
    283         &self.plan
    284     }
    285     pub const fn is_replay(&self) -> bool {
    286         self.replay
    287     }
    288 }
    289 
    290 impl AdmissionRunReceipt {
    291     pub const fn artifact(&self) -> &AuthoredArtifact {
    292         &self.artifact
    293     }
    294     pub const fn is_replay(&self) -> bool {
    295         self.replay
    296     }
    297 }
    298 
    299 impl Engine {
    300     /// Atomically persists the complete parent, exact plan, and delivery intent.
    301     ///
    302     /// This method invokes neither a signer nor a transport. Exact request
    303     /// replay returns the original durable preparation; conflicting reuse of
    304     /// the operation identity fails closed.
    305     pub async fn prepare_push(&self, request: PushRequest) -> Result<PushPreparation, Error> {
    306         let prepared_at = self.clock.now_unix_ms()?;
    307         let (operation_id, artifact_id, delivery_plan_id) = authored_ids(request.operation_id)?;
    308         let command = AuthoredAtomicCommand::Prepare(request.authored_preparation(prepared_at)?);
    309         let receipt = self
    310             .storage
    311             .execute_authored(command)
    312             .await
    313             .map_err(map_storage_error)?;
    314         let AuthoredAtomicOutcome::Prepared {
    315             operation,
    316             artifacts,
    317             delivery_plans,
    318         } = receipt.outcome()
    319         else {
    320             return Err(Error::StorageFailed);
    321         };
    322         let [artifact] = artifacts.as_slice() else {
    323             return Err(Error::StorageFailed);
    324         };
    325         let [delivery_plan] = delivery_plans.as_slice() else {
    326             return Err(Error::StorageFailed);
    327         };
    328         if operation.operation_id() != operation_id
    329             || artifact.artifact_id() != artifact_id
    330             || delivery_plan.plan_id() != delivery_plan_id
    331         {
    332             return Err(Error::StorageFailed);
    333         }
    334         Ok(PushPreparation {
    335             operation: operation.clone(),
    336             artifact: artifact.clone(),
    337             delivery_plan: delivery_plan.clone(),
    338             replay: receipt.disposition() == AtomicCommitDisposition::Replay,
    339         })
    340     }
    341 
    342     /// Loads the complete durable state for one prepared push operation.
    343     pub async fn push_status(&self, operation_id: SyncId) -> Result<Option<PushStatus>, Error> {
    344         let (operation_id, expected_artifact_id, expected_plan_id) = authored_ids(operation_id)?;
    345         let Some(operation) = self
    346             .storage
    347             .authored_operation(operation_id)
    348             .await
    349             .map_err(map_storage_error)?
    350         else {
    351             return Ok(None);
    352         };
    353         if operation.artifact_ids() != [expected_artifact_id] {
    354             return Err(Error::StorageFailed);
    355         }
    356         let artifact = self
    357             .storage
    358             .authored_artifact(expected_artifact_id)
    359             .await
    360             .map_err(map_storage_error)?
    361             .ok_or(Error::StorageFailed)?;
    362         let delivery_history = self
    363             .storage
    364             .authored_delivery_history(expected_plan_id)
    365             .await
    366             .map_err(map_storage_error)?
    367             .ok_or(Error::StorageFailed)?;
    368         delivery_history.validate().map_err(map_storage_error)?;
    369         let delivery_plan = delivery_history.plan();
    370         if artifact.artifact_id() != expected_artifact_id
    371             || delivery_plan.plan_id() != expected_plan_id
    372             || artifact.operation_id() != operation.operation_id()
    373             || delivery_plan.artifact_id() != artifact.artifact_id()
    374         {
    375             return Err(Error::StorageFailed);
    376         }
    377         let settlement = OperationSettlement::evaluate_complete(
    378             &operation,
    379             core::slice::from_ref(&artifact),
    380             core::slice::from_ref(delivery_plan),
    381         )
    382         .map_err(map_storage_error)?;
    383         Ok(Some(PushStatus {
    384             operation,
    385             artifact,
    386             delivery_history,
    387             settlement,
    388         }))
    389     }
    390 
    391     /// Cancels every remaining local phase without discarding signed bytes or
    392     /// delivery evidence. Re-entry after a phase-boundary crash finishes any
    393     /// remaining cancellation and terminal replays are no-ops.
    394     pub async fn cancel_push(
    395         &self,
    396         operation_id: SyncId,
    397     ) -> Result<PushCancellationReceipt, Error> {
    398         let mut status = self
    399             .push_status(operation_id)
    400             .await?
    401             .ok_or(Error::StorageFailed)?;
    402         let mut changed = false;
    403         let now = self
    404             .clock
    405             .now_unix_ms()?
    406             .max(status.artifact.updated_at_unix_ms())
    407             .max(status.delivery_plan().updated_at_unix_ms());
    408 
    409         let artifact_target = match status.artifact.signing_state() {
    410             SigningState::Planned | SigningState::Retryable => Some(
    411                 CancelAuthoredTarget::ArtifactSigning(status.artifact.artifact_id()),
    412             ),
    413             SigningState::Signed
    414                 if matches!(
    415                     status.artifact.admission_state(),
    416                     AdmissionState::Pending | AdmissionState::Retryable
    417                 ) =>
    418             {
    419                 Some(CancelAuthoredTarget::ArtifactAdmission(
    420                     status.artifact.artifact_id(),
    421                 ))
    422             }
    423             SigningState::Signed
    424             | SigningState::Indeterminate
    425             | SigningState::FailedTerminal
    426             | SigningState::Cancelled => None,
    427         };
    428         if let Some(target) = artifact_target {
    429             self.storage
    430                 .execute_authored(AuthoredAtomicCommand::Cancel(
    431                     CancelAuthoredWork::new(target, status.artifact.revision(), now)
    432                         .map_err(map_storage_error)?,
    433                 ))
    434                 .await
    435                 .map_err(map_storage_error)?;
    436             changed = true;
    437             status = self
    438                 .push_status(operation_id)
    439                 .await?
    440                 .ok_or(Error::StorageFailed)?;
    441         }
    442 
    443         if status.delivery_plan().stop_requested_at_unix_ms().is_none() {
    444             let command = AuthoredAtomicCommand::Cancel(
    445                 CancelAuthoredWork::new(
    446                     CancelAuthoredTarget::DeliveryPlan(status.delivery_plan().plan_id()),
    447                     status.delivery_plan().revision(),
    448                     now,
    449                 )
    450                 .map_err(map_storage_error)?,
    451             );
    452             let receipt = self
    453                 .storage
    454                 .execute_authored(command.clone())
    455                 .await
    456                 .map_err(map_storage_error)?;
    457             if !receipt.matches_command(&command) {
    458                 return Err(Error::StorageFailed);
    459             }
    460             let AuthoredAtomicOutcome::DeliveryPlan(stopped) = receipt.outcome() else {
    461                 return Err(Error::StorageFailed);
    462             };
    463             if stopped.validate().is_err()
    464                 || stopped.plan_id() != status.delivery_plan().plan_id()
    465                 || stopped.artifact_id() != status.artifact.artifact_id()
    466                 || stopped.request() != status.delivery_plan().request()
    467                 || stopped.stop_requested_at_unix_ms() != Some(now)
    468                 || status.delivery_plan().revision().get().checked_add(1)
    469                     != Some(stopped.revision().get())
    470             {
    471                 return Err(Error::StorageFailed);
    472             }
    473             changed = true;
    474             status = self
    475                 .push_status(operation_id)
    476                 .await?
    477                 .ok_or(Error::StorageFailed)?;
    478             if status.delivery_plan().stop_requested_at_unix_ms().is_none() {
    479                 return Err(Error::StorageFailed);
    480             }
    481         }
    482 
    483         Ok(PushCancellationReceipt { status, changed })
    484     }
    485 
    486     /// Claims and executes one prepared signing artifact.
    487     pub async fn sign_prepared(&self, request: PushRequest) -> Result<SigningRunReceipt, Error> {
    488         self.prepare_push(request.clone()).await?;
    489         let status = self
    490             .push_status(request.operation_id)
    491             .await?
    492             .ok_or(Error::StorageFailed)?;
    493         if status.delivery_plan().state() == AuthoredDeliveryState::Cancelled {
    494             self.cancel_push(request.operation_id).await?;
    495             return Err(Error::SigningCancelled);
    496         }
    497         let artifact = status.artifact;
    498         match artifact.signing_state() {
    499             SigningState::Signed => {
    500                 return Ok(SigningRunReceipt {
    501                     artifact,
    502                     replay: true,
    503                 });
    504             }
    505             SigningState::Indeterminate => return Err(Error::SigningIndeterminate),
    506             SigningState::FailedTerminal | SigningState::Cancelled => {
    507                 return Err(Error::SignerFailed);
    508             }
    509             SigningState::Planned | SigningState::Retryable => {}
    510         }
    511         let signer = self.signer.as_deref().ok_or(Error::MissingSigner)?;
    512 
    513         let now = self.clock.now_unix_ms()?.max(artifact.updated_at_unix_ms());
    514         if let Some(existing) = artifact.signing_claim() {
    515             if now < existing.expires_at_unix_ms() {
    516                 return Err(Error::WorkClaimConflict);
    517             }
    518             if existing.owner() == SIGNING_CLAIM_OWNER_NON_REPLAYABLE {
    519                 let (claimed, fence) = self
    520                     .claim_artifact(
    521                         artifact,
    522                         ClaimAuthoredTarget::ArtifactSigning,
    523                         SIGNING_CLAIM_OWNER_NON_REPLAYABLE,
    524                         now,
    525                     )
    526                     .await?;
    527                 let failure = WorkFailure::new(
    528                     "signing_effect_unknown_after_restart",
    529                     WorkPhase::Signing,
    530                     FailureClass::Indeterminate,
    531                     None,
    532                     None,
    533                 )
    534                 .map_err(map_storage_error)?;
    535                 self.apply_artifact_failure(claimed.artifact_id(), fence, failure, None, now)
    536                     .await?;
    537                 return Err(Error::SigningIndeterminate);
    538             }
    539         }
    540 
    541         let signer_status = signer.status().await.map_err(|_| Error::SignerFailed)?;
    542         let replay_capability = signer_replay_capability(&signer_status)?;
    543         let (claimed, fence) = self
    544             .claim_artifact(
    545                 artifact,
    546                 ClaimAuthoredTarget::ArtifactSigning,
    547                 signing_claim_owner(replay_capability),
    548                 now,
    549             )
    550             .await?;
    551         let persisted_plan = claimed
    552             .plan()
    553             .ok_or(Error::StorageFailed)?
    554             .decode()
    555             .map_err(map_storage_error)?
    556             .into_plan();
    557         if persisted_plan != request.plan {
    558             return Err(Error::StorageConflict);
    559         }
    560         let signing_claim = claimed.signing_claim().ok_or(Error::StorageFailed)?.clone();
    561         let deadline = self.deadlines.deadline_unix_ms(OperationKind::Sign, now)?;
    562         let signing_operation = SigningOperationId::new(*request.operation_id.as_bytes())
    563             .map_err(|_| Error::InvalidPushRequest)?;
    564         let signing_artifact = SigningArtifactId::new(*claimed.artifact_id().as_bytes())
    565             .map_err(|_| Error::InvalidPushRequest)?;
    566         let sign_request = radroots_signing::SignRequest::new(
    567             OperationId::SyncPush,
    568             SigningIntentId::new(signing_operation, signing_artifact),
    569             request.actor,
    570             persisted_plan,
    571             SignPolicy::new(deadline, request.cancellation)
    572                 .map_err(|_| Error::InvalidPushRequest)?,
    573         )
    574         .map_err(|_| Error::InvalidPushRequest)?;
    575 
    576         let expected_request = sign_request.clone();
    577         let signer_result = match signer.sign_authored_evidence(sign_request).await {
    578             Ok(receipt) => {
    579                 // A missing observation time leaves the durable attempt unresolved;
    580                 // it is not evidence that signing failed without an effect.
    581                 let observed_at_unix_ms = self.clock.now_unix_ms()?;
    582                 receipt.revalidate(&expected_request, observed_at_unix_ms)
    583             }
    584             Err(error) => Err(error),
    585         };
    586 
    587         match signer_result {
    588             Ok(receipt) => {
    589                 let command = AuthoredAtomicCommand::RecordSigned(
    590                     RecordSignedArtifact::new(
    591                         claimed.operation_id(),
    592                         claimed.artifact_id(),
    593                         signing_claim,
    594                         receipt.signed_event().clone(),
    595                         receipt.observed_at_unix_ms(),
    596                     )
    597                     .map_err(map_storage_error)?,
    598                 );
    599                 let applied = self
    600                     .storage
    601                     .execute_authored(command.clone())
    602                     .await
    603                     .map_err(map_storage_error)?;
    604                 if !applied.matches_command(&command) {
    605                     return Err(Error::StorageFailed);
    606                 }
    607                 let status = self
    608                     .push_status(request.operation_id)
    609                     .await?
    610                     .ok_or(Error::StorageFailed)?;
    611                 if status
    612                     .artifact
    613                     .signed()
    614                     .is_none_or(|signed| signed.event() != receipt.signed_event())
    615                 {
    616                     return Err(Error::StorageFailed);
    617                 }
    618                 if expected_request.cancellation_signal().is_cancelled()
    619                     || status.delivery_plan().state() == AuthoredDeliveryState::Cancelled
    620                 {
    621                     let stopped = self.cancel_push(request.operation_id).await?;
    622                     if stopped
    623                         .status
    624                         .artifact
    625                         .signed()
    626                         .is_none_or(|signed| signed.event() != receipt.signed_event())
    627                     {
    628                         return Err(Error::StorageFailed);
    629                     }
    630                     return Err(Error::SigningCancelled);
    631                 }
    632                 match status.artifact.signing_state() {
    633                     SigningState::Cancelled => return Err(Error::SigningCancelled),
    634                     SigningState::FailedTerminal => return Err(Error::SignerFailed),
    635                     SigningState::Signed => {}
    636                     _ => return Err(Error::StorageFailed),
    637                 }
    638                 if receipt.observed_at_unix_ms() >= deadline {
    639                     return Err(Error::SignerDeadlineExceeded);
    640                 }
    641                 Ok(SigningRunReceipt {
    642                     artifact: status.artifact,
    643                     replay: applied.disposition() == AtomicCommitDisposition::Replay,
    644                 })
    645             }
    646             Err(error) => {
    647                 let applied_at = self.clock.now_unix_ms()?;
    648                 let current = self
    649                     .push_status(request.operation_id)
    650                     .await?
    651                     .ok_or(Error::StorageFailed)?;
    652                 if current.artifact.signed().is_some()
    653                     || current.artifact.signing_claim() != claimed.signing_claim()
    654                 {
    655                     // Another worker may have committed evidence or a stop while this one waited.
    656                     return Err(match error.kind() {
    657                         radroots_signing::error::Kind::DeadlineExceeded => {
    658                             Error::SignerDeadlineExceeded
    659                         }
    660                         radroots_signing::error::Kind::SignerCancelled => Error::SigningCancelled,
    661                         _ => Error::SignerFailed,
    662                     });
    663                 }
    664                 if error.kind() == radroots_signing::error::Kind::DeadlineExceeded
    665                     && claimed
    666                         .signing_claim()
    667                         .is_some_and(|claim| applied_at >= claim.expires_at_unix_ms())
    668                 {
    669                     // The signer result arrived after this worker's fence
    670                     // expired. It is rejected, but this stale worker must not
    671                     // mutate durable state; recovery will reclaim the exact
    672                     // request under a fresh fence.
    673                     return Err(Error::SignerDeadlineExceeded);
    674                 }
    675                 if error.kind() == radroots_signing::error::Kind::SignerCancelled {
    676                     let command = AuthoredAtomicCommand::Cancel(
    677                         CancelAuthoredWork::new(
    678                             CancelAuthoredTarget::ArtifactSigning(claimed.artifact_id()),
    679                             claimed.revision(),
    680                             applied_at,
    681                         )
    682                         .map_err(map_storage_error)?,
    683                     );
    684                     self.storage
    685                         .execute_authored(command)
    686                         .await
    687                         .map_err(map_storage_error)?;
    688                     return Err(Error::SigningCancelled);
    689                 }
    690                 let disposition = recovery_disposition(
    691                     replay_capability,
    692                     error.remote_effect(),
    693                     error.retryable(),
    694                 );
    695                 let class = match disposition {
    696                     RecoveryDisposition::RetryExactRequest | RecoveryDisposition::RetryLocal => {
    697                         FailureClass::Retryable
    698                     }
    699                     RecoveryDisposition::Indeterminate => FailureClass::Indeterminate,
    700                     RecoveryDisposition::Failed => FailureClass::Terminal,
    701                     _ => FailureClass::Indeterminate,
    702                 };
    703                 let retry_at = if class == FailureClass::Retryable {
    704                     Some(
    705                         applied_at
    706                             .checked_add(WORK_RETRY_DELAY_MS)
    707                             .ok_or(Error::DeadlineOverflow)?,
    708                     )
    709                 } else {
    710                     None
    711                 };
    712                 let failure =
    713                     WorkFailure::new(error.code(), WorkPhase::Signing, class, retry_at, None)
    714                         .map_err(map_storage_error)?;
    715                 let retry = retry_schedule(claimed.signing_retry(), &failure, retry_at)?;
    716                 self.apply_artifact_failure(
    717                     claimed.artifact_id(),
    718                     fence,
    719                     failure,
    720                     retry,
    721                     applied_at,
    722                 )
    723                 .await?;
    724                 match disposition {
    725                     RecoveryDisposition::Indeterminate => Err(Error::SigningIndeterminate),
    726                     RecoveryDisposition::RetryExactRequest
    727                     | RecoveryDisposition::RetryLocal
    728                     | RecoveryDisposition::Failed => {
    729                         if error.kind() == radroots_signing::error::Kind::DeadlineExceeded {
    730                             Err(Error::SignerDeadlineExceeded)
    731                         } else {
    732                             Err(Error::SignerFailed)
    733                         }
    734                     }
    735                     _ => Err(Error::SigningIndeterminate),
    736                 }
    737             }
    738         }
    739     }
    740 
    741     /// Claims and executes local admission for one durably signed artifact.
    742     pub async fn admit_signed(&self, operation_id: SyncId) -> Result<AdmissionRunReceipt, Error> {
    743         let status = self
    744             .push_status(operation_id)
    745             .await?
    746             .ok_or(Error::StorageFailed)?;
    747         let delivery_stopped = status.delivery_plan().stop_requested_at_unix_ms().is_some();
    748         let artifact = status.artifact;
    749         if artifact.signing_state() != SigningState::Signed {
    750             return Err(Error::InvalidSignerOutput);
    751         }
    752         if artifact.admission_state().is_admitted() {
    753             return Ok(AdmissionRunReceipt {
    754                 artifact,
    755                 replay: true,
    756             });
    757         }
    758         if delivery_stopped {
    759             self.cancel_push(operation_id).await?;
    760             return Err(Error::AdmissionFailed);
    761         }
    762         if matches!(
    763             artifact.admission_state(),
    764             AdmissionState::Rejected | AdmissionState::Cancelled
    765         ) {
    766             return Err(Error::AdmissionFailed);
    767         }
    768         let now = self.clock.now_unix_ms()?.max(artifact.updated_at_unix_ms());
    769         if artifact
    770             .admission_claim()
    771             .is_some_and(|claim| now < claim.expires_at_unix_ms())
    772         {
    773             return Err(Error::WorkClaimConflict);
    774         }
    775         let (claimed, fence) = self
    776             .claim_artifact(
    777                 artifact,
    778                 ClaimAuthoredTarget::ArtifactAdmission,
    779                 ADMISSION_CLAIM_OWNER,
    780                 now,
    781             )
    782             .await?;
    783         let event = claimed
    784             .signed()
    785             .ok_or(Error::InvalidSignerOutput)?
    786             .event()
    787             .clone();
    788         let persisted_plan = claimed
    789             .plan()
    790             .ok_or(Error::StorageFailed)?
    791             .decode()
    792             .map_err(map_storage_error)?
    793             .into_plan();
    794         let contract_id = persisted_plan.body().contract().contract_id().as_str();
    795         let admission = match outbound_admission(&event, contract_id, now) {
    796             Ok(admission) => admission,
    797             Err(error) => {
    798                 let failure = WorkFailure::new(
    799                     "invalid_signed_artifact",
    800                     WorkPhase::Admission,
    801                     FailureClass::Terminal,
    802                     None,
    803                     None,
    804                 )
    805                 .map_err(map_storage_error)?;
    806                 self.apply_artifact_failure(claimed.artifact_id(), fence, failure, None, now)
    807                     .await?;
    808                 return Err(error);
    809             }
    810         };
    811         let admission_receipt = match admission::admit(
    812             self.storage.as_ref(),
    813             admission,
    814             &event,
    815             contract_id,
    816             now,
    817         )
    818         .await
    819         {
    820             Ok(receipt) => receipt,
    821             Err(error) => {
    822                 let applied_at = self.clock.now_unix_ms()?.max(claimed.updated_at_unix_ms());
    823                 let terminal = matches!(
    824                     error,
    825                     radroots_storage::Error::EventConflict
    826                         | radroots_storage::Error::AdmissionRegression
    827                 );
    828                 let retry_at = if terminal {
    829                     None
    830                 } else {
    831                     Some(
    832                         applied_at
    833                             .checked_add(WORK_RETRY_DELAY_MS)
    834                             .ok_or(Error::DeadlineOverflow)?,
    835                     )
    836                 };
    837                 let failure = WorkFailure::new(
    838                     if terminal {
    839                         "admission_conflict"
    840                     } else {
    841                         "admission_storage_unavailable"
    842                     },
    843                     WorkPhase::Admission,
    844                     if terminal {
    845                         FailureClass::Terminal
    846                     } else {
    847                         FailureClass::Retryable
    848                     },
    849                     retry_at,
    850                     None,
    851                 )
    852                 .map_err(map_storage_error)?;
    853                 let retry = retry_schedule(claimed.admission_retry(), &failure, retry_at)?;
    854                 self.apply_artifact_failure(
    855                     claimed.artifact_id(),
    856                     fence,
    857                     failure,
    858                     retry,
    859                     applied_at,
    860                 )
    861                 .await?;
    862                 return Err(match error {
    863                     radroots_storage::Error::SpaceInsufficient => Error::StorageSpaceInsufficient,
    864                     _ => Error::AdmissionFailed,
    865                 });
    866             }
    867         };
    868         let state = match admission_receipt.disposition() {
    869             AdmissionDisposition::Duplicate => AdmissionState::Duplicate,
    870             AdmissionDisposition::Inserted | AdmissionDisposition::Advanced => {
    871                 AdmissionState::Inserted
    872             }
    873         };
    874         let applied_at = self.clock.now_unix_ms()?.max(claimed.updated_at_unix_ms());
    875         let command = AuthoredAtomicCommand::ApplyAdmission(
    876             ApplyAdmissionResult::new(claimed.artifact_id(), fence, state, None, None, applied_at)
    877                 .map_err(map_storage_error)?,
    878         );
    879         let applied = self
    880             .storage
    881             .execute_authored(command)
    882             .await
    883             .map_err(map_storage_error)?;
    884         let AuthoredAtomicOutcome::Artifact(artifact) = applied.outcome() else {
    885             return Err(Error::StorageFailed);
    886         };
    887         Ok(AdmissionRunReceipt {
    888             artifact: artifact.clone(),
    889             replay: false,
    890         })
    891     }
    892 
    893     async fn claim_artifact(
    894         &self,
    895         artifact: AuthoredArtifact,
    896         target: fn(AuthoredArtifactId) -> ClaimAuthoredTarget,
    897         owner: &'static str,
    898         acquired_at: u64,
    899     ) -> Result<(AuthoredArtifact, WorkFence), Error> {
    900         let existing = match target(artifact.artifact_id()) {
    901             ClaimAuthoredTarget::ArtifactSigning(_) => artifact.signing_claim(),
    902             ClaimAuthoredTarget::ArtifactAdmission(_) => artifact.admission_claim(),
    903             ClaimAuthoredTarget::DeliveryPlan(_) => return Err(Error::StorageFailed),
    904         };
    905         let generation = existing.map_or(1, |claim| claim.generation().get().saturating_add(1));
    906         let generation = NonZeroU64::new(generation).ok_or(Error::StorageFailed)?;
    907         let expires_at = acquired_at
    908             .checked_add(self.deadlines.timeout_ms(OperationKind::Sign))
    909             .ok_or(Error::DeadlineOverflow)?;
    910         let claim = WorkClaim::new(
    911             *self.ids.next_id(OperationKind::Sign)?.as_bytes(),
    912             owner,
    913             generation,
    914             acquired_at,
    915             expires_at,
    916             artifact.revision(),
    917         )
    918         .map_err(map_storage_error)?;
    919         let fence = WorkFence::new(*claim.token(), claim.generation(), claim.row_revision())
    920             .map_err(map_storage_error)?;
    921         let command = AuthoredAtomicCommand::Claim(ClaimAuthoredWork::new(
    922             target(artifact.artifact_id()),
    923             claim,
    924         ));
    925         let receipt = self
    926             .storage
    927             .execute_authored(command)
    928             .await
    929             .map_err(map_claim_error)?;
    930         let AuthoredAtomicOutcome::Artifact(claimed) = receipt.outcome() else {
    931             return Err(Error::StorageFailed);
    932         };
    933         Ok((claimed.clone(), fence))
    934     }
    935 
    936     async fn apply_artifact_failure(
    937         &self,
    938         artifact_id: AuthoredArtifactId,
    939         fence: WorkFence,
    940         failure: WorkFailure,
    941         retry: Option<RetrySchedule>,
    942         applied_at: u64,
    943     ) -> Result<AuthoredArtifact, Error> {
    944         let command = AuthoredAtomicCommand::ApplyFailure(
    945             ApplyWorkFailure::new(
    946                 AuthoredWorkTarget::Artifact(artifact_id),
    947                 fence,
    948                 failure,
    949                 retry,
    950                 applied_at,
    951             )
    952             .map_err(map_storage_error)?,
    953         );
    954         let receipt = self
    955             .storage
    956             .execute_authored(command)
    957             .await
    958             .map_err(map_storage_error)?;
    959         let AuthoredAtomicOutcome::Artifact(artifact) = receipt.outcome() else {
    960             return Err(Error::StorageFailed);
    961         };
    962         Ok(artifact.clone())
    963     }
    964 }
    965 
    966 fn signer_replay_capability(
    967     status: &radroots_signing::SignerStatus,
    968 ) -> Result<ReplayCapability, Error> {
    969     let mut capabilities = status.capabilities().iter();
    970     let first = capabilities
    971         .next()
    972         .ok_or(Error::SignerCapabilityUnavailable)?
    973         .replay();
    974     if capabilities.all(|capability| capability.replay() == first) {
    975         Ok(first)
    976     } else {
    977         Ok(ReplayCapability::NonReplayable)
    978     }
    979 }
    980 
    981 fn signing_claim_owner(replay: ReplayCapability) -> &'static str {
    982     match replay {
    983         ReplayCapability::ExactReplayByRequestId => SIGNING_CLAIM_OWNER_EXACT,
    984         ReplayCapability::LocalReplaySafe => SIGNING_CLAIM_OWNER_LOCAL,
    985         ReplayCapability::NonReplayable => SIGNING_CLAIM_OWNER_NON_REPLAYABLE,
    986         _ => SIGNING_CLAIM_OWNER_NON_REPLAYABLE,
    987     }
    988 }
    989 
    990 fn retry_schedule(
    991     previous: Option<&RetrySchedule>,
    992     failure: &WorkFailure,
    993     retry_at: Option<u64>,
    994 ) -> Result<Option<RetrySchedule>, Error> {
    995     let Some(retry_at) = retry_at else {
    996         return Ok(None);
    997     };
    998     let schedule = match previous {
    999         Some(previous) => previous.next_attempt(retry_at, failure.clone()),
   1000         None => RetrySchedule::new(NonZeroU32::MIN, retry_at, failure.clone()),
   1001     }
   1002     .map_err(map_storage_error)?;
   1003     Ok(Some(schedule))
   1004 }
   1005 
   1006 fn normalize_sink_failure(
   1007     request: &DeliveryRequest,
   1008     failure: SinkFailure,
   1009     attempted_at_unix_ms: u64,
   1010 ) -> Result<SinkFailure, Error> {
   1011     if failure.retryability() != Retryability::Retryable {
   1012         return Ok(failure);
   1013     }
   1014     let default_retry_at = attempted_at_unix_ms
   1015         .checked_add(WORK_RETRY_DELAY_MS)
   1016         .ok_or(Error::DeadlineOverflow)?;
   1017     let retry_at = failure
   1018         .retry_after_unix_ms()
   1019         .unwrap_or(default_retry_at)
   1020         .max(
   1021             attempted_at_unix_ms
   1022                 .checked_add(1)
   1023                 .ok_or(Error::DeadlineOverflow)?,
   1024         );
   1025     if failure.retry_after_unix_ms() == Some(retry_at) {
   1026         return Ok(failure);
   1027     }
   1028     SinkFailure::for_request(
   1029         request,
   1030         failure.code(),
   1031         failure.retryability(),
   1032         Some(retry_at),
   1033         failure.message().map(str::to_owned),
   1034         failure.partial_evidence().to_vec(),
   1035     )
   1036     .map_err(|_| Error::InvalidDeliveryRequest)
   1037 }
   1038 
   1039 fn delivery_retry_schedule(
   1040     attempt: NonZeroU32,
   1041     outcome: &DeliveryAttemptOutcome,
   1042     satisfaction: SatisfactionState,
   1043     attempted_at_unix_ms: u64,
   1044 ) -> Result<Option<RetrySchedule>, Error> {
   1045     if satisfaction != SatisfactionState::Pending || attempt.get() >= DELIVERY_PLAN_ATTEMPTS_MAX {
   1046         return Ok(None);
   1047     }
   1048     let failure = match outcome {
   1049         DeliveryAttemptOutcome::Receipt(_) => {
   1050             let retry_at = attempted_at_unix_ms
   1051                 .checked_add(WORK_RETRY_DELAY_MS)
   1052                 .ok_or(Error::DeadlineOverflow)?;
   1053             WorkFailure::new(
   1054                 "delivery_pending",
   1055                 WorkPhase::Delivery,
   1056                 FailureClass::Retryable,
   1057                 Some(retry_at),
   1058                 None,
   1059             )
   1060             .map_err(map_storage_error)?
   1061         }
   1062         DeliveryAttemptOutcome::SinkFailure(failure)
   1063             if failure.retryability() == Retryability::Retryable =>
   1064         {
   1065             WorkFailure::new(
   1066                 failure.code(),
   1067                 WorkPhase::Delivery,
   1068                 FailureClass::Retryable,
   1069                 failure.retry_after_unix_ms(),
   1070                 failure.message().map(str::to_owned),
   1071             )
   1072             .map_err(map_storage_error)?
   1073         }
   1074         DeliveryAttemptOutcome::SinkFailure(_) => return Ok(None),
   1075     };
   1076     let retry_at = failure.retry_after_unix_ms().unwrap_or(
   1077         attempted_at_unix_ms
   1078             .checked_add(WORK_RETRY_DELAY_MS)
   1079             .ok_or(Error::DeadlineOverflow)?,
   1080     );
   1081     RetrySchedule::new(attempt, retry_at, failure)
   1082         .map(Some)
   1083         .map_err(map_storage_error)
   1084 }
   1085 
   1086 fn outbound_admission(
   1087     event: &radroots_event::SignedEvent,
   1088     contract_id: &str,
   1089     observed_at_unix_ms: u64,
   1090 ) -> Result<EventAdmission, Error> {
   1091     let verified = verify::signature(
   1092         verify::id(RawEvent::new(event.envelope().clone()))
   1093             .map_err(|_| Error::InvalidSignerOutput)?,
   1094         &Nip01SignatureVerifier,
   1095     )
   1096     .map_err(|_| Error::InvalidSignerOutput)?;
   1097     let validated = verified
   1098         .validate_contract_for_admission(contract_id)
   1099         .map_err(|_| Error::InvalidSignerOutput)?;
   1100     let policy = RegistryPolicy::visible();
   1101     if policy.decide(&validated) != AdmissionDecision::Visible {
   1102         return Err(Error::InvalidSignerOutput);
   1103     }
   1104     struct Evidence;
   1105     impl radroots_event::admission::AdmissionPolicy for Evidence {
   1106         type Error = core::convert::Infallible;
   1107         fn policy_id(&self) -> &'static str {
   1108             "radroots.registry_v7"
   1109         }
   1110         fn admit(
   1111             &self,
   1112             _: &radroots_event::admission::ContractValidatedEvent,
   1113         ) -> Result<(), Self::Error> {
   1114             Ok(())
   1115         }
   1116     }
   1117     impl radroots_event::admission::VisibilityPolicy for Evidence {
   1118         type Error = core::convert::Infallible;
   1119         fn policy_id(&self) -> &'static str {
   1120             "radroots.registry_v7"
   1121         }
   1122         fn make_visible(
   1123             &self,
   1124             _: &radroots_event::admission::AdmittedEvent,
   1125         ) -> Result<(), Self::Error> {
   1126             Ok(())
   1127         }
   1128     }
   1129     let visible = validated
   1130         .admit_with(&Evidence)
   1131         .and_then(|event| event.make_visible_with(&Evidence))
   1132         .map_err(|never| match never {})?;
   1133     let local = Target::new(TransportId::LOCAL, "local:sync-authored")
   1134         .map_err(|_| Error::InvalidPushRequest)?;
   1135     let provenance = EventProvenance::new(
   1136         TransportId::LOCAL,
   1137         local.fingerprint().clone(),
   1138         observed_at_unix_ms,
   1139     )
   1140     .map_err(|_| Error::InvalidPushRequest)?;
   1141     EventAdmission::visible(ObservedEvent::new(event.clone(), provenance), visible)
   1142         .map_err(map_storage_error)
   1143 }
   1144 
   1145 fn authored_push_input_digest(request: &PushRequest) -> Result<AtomicCommitDigest, Error> {
   1146     let mut hasher = Sha256::new();
   1147     hash_field(&mut hasher, b"radroots.sync.authored-push.v2");
   1148     hash_field(&mut hasher, request.operation_id.as_bytes());
   1149     hash_field(&mut hasher, request.idempotency_key.as_str().as_bytes());
   1150     hash_field(&mut hasher, request.plan.digest().as_bytes());
   1151     hash_field(&mut hasher, request.actor.public_key().as_bytes());
   1152     let source = match request.actor.source() {
   1153         radroots_signing::actor::ActorSource::LocalAccount(_) => 0,
   1154         radroots_signing::actor::ActorSource::ExplicitPublicKey => 1,
   1155         radroots_signing::actor::ActorSource::RemoteSigner(_) => 2,
   1156         radroots_signing::actor::ActorSource::Service(_) => 3,
   1157         _ => return Err(Error::InvalidPushRequest),
   1158     };
   1159     hasher.update([source]);
   1160     if let Some(account_id) = request.actor.account_id() {
   1161         hash_field(&mut hasher, account_id.as_bytes());
   1162     }
   1163     for role in request.actor.roles() {
   1164         hash_field(&mut hasher, role.as_str().as_bytes());
   1165     }
   1166     for target in request.targets.targets() {
   1167         hash_field(&mut hasher, target.fingerprint().as_str().as_bytes());
   1168     }
   1169     hash_satisfaction(&mut hasher, &request.satisfaction);
   1170     hasher.update(request.delivery_deadline_unix_ms.to_be_bytes());
   1171     let cancellation = match request.cancellation {
   1172         CancellationPolicy::PreservePublishedRequest => 0,
   1173         CancellationPolicy::LocalCooperative => 1,
   1174         _ => return Err(Error::InvalidPushRequest),
   1175     };
   1176     hasher.update([cancellation]);
   1177     Ok(AtomicCommitDigest::new(hasher.finalize().into()))
   1178 }
   1179 
   1180 fn hash_satisfaction(hasher: &mut Sha256, policy: &SatisfactionPolicy) {
   1181     hasher.update([match policy.class() {
   1182         radroots_transport::policy::SatisfactionClass::Accepted => 0,
   1183         radroots_transport::policy::SatisfactionClass::Delivered => 1,
   1184     }]);
   1185     let targets = policy.targets();
   1186     if targets.is_any() {
   1187         hasher.update([0]);
   1188     } else if targets.is_all() {
   1189         hasher.update([1]);
   1190     } else if let Some(threshold) = targets.quorum_threshold() {
   1191         hasher.update([2]);
   1192         hasher.update(threshold.to_be_bytes());
   1193     } else if let Some(required) = targets.required_targets() {
   1194         hasher.update([3]);
   1195         for target in required {
   1196             hash_field(hasher, target.as_str().as_bytes());
   1197         }
   1198     }
   1199 }
   1200 
   1201 fn authored_ids(
   1202     operation_id: SyncId,
   1203 ) -> Result<
   1204     (
   1205         OperationInstanceId,
   1206         AuthoredArtifactId,
   1207         AuthoredDeliveryPlanId,
   1208     ),
   1209     Error,
   1210 > {
   1211     let operation =
   1212         OperationInstanceId::new(*operation_id.as_bytes()).map_err(map_storage_error)?;
   1213     let artifact = AuthoredArtifactId::new(derive_child_id(
   1214         b"radroots.sync.authored-artifact.v2",
   1215         operation_id.as_bytes(),
   1216     ))
   1217     .map_err(map_storage_error)?;
   1218     let delivery = AuthoredDeliveryPlanId::new(derive_child_id(
   1219         b"radroots.sync.authored-delivery.v2",
   1220         artifact.as_bytes(),
   1221     ))
   1222     .map_err(map_storage_error)?;
   1223     Ok((operation, artifact, delivery))
   1224 }
   1225 
   1226 fn derive_child_id(domain: &[u8], parent: &[u8; 16]) -> [u8; 16] {
   1227     let mut hasher = Sha256::new();
   1228     hash_field(&mut hasher, domain);
   1229     hash_field(&mut hasher, parent);
   1230     let digest: [u8; 32] = hasher.finalize().into();
   1231     let mut child = [0_u8; 16];
   1232     child.copy_from_slice(&digest[..16]);
   1233     child
   1234 }
   1235 
   1236 fn hash_field(hasher: &mut Sha256, value: &[u8]) {
   1237     hasher.update(u64::try_from(value.len()).unwrap_or(u64::MAX).to_be_bytes());
   1238     hasher.update(value);
   1239 }
   1240 
   1241 fn delivery_request_id(id: SyncId) -> String {
   1242     const HEX: &[u8; 16] = b"0123456789abcdef";
   1243     let mut value = String::from("push-");
   1244     for byte in id.as_bytes() {
   1245         value.push(HEX[(byte >> 4) as usize] as char);
   1246         value.push(HEX[(byte & 0x0f) as usize] as char);
   1247     }
   1248     value
   1249 }
   1250 
   1251 fn map_storage_error(error: radroots_storage::Error) -> Error {
   1252     match error {
   1253         radroots_storage::Error::SpaceInsufficient => Error::StorageSpaceInsufficient,
   1254         radroots_storage::Error::IdempotencyConflict
   1255         | radroots_storage::Error::OperationIdentityMismatch
   1256         | radroots_storage::Error::JournalRevisionConflict
   1257         | radroots_storage::Error::OutboxPlanConflict
   1258         | radroots_storage::Error::AtomicCommitConflict => Error::StorageConflict,
   1259         _ => Error::StorageFailed,
   1260     }
   1261 }
   1262 
   1263 fn map_claim_error(error: radroots_storage::Error) -> Error {
   1264     match error {
   1265         radroots_storage::Error::AtomicCommitConflict
   1266         | radroots_storage::Error::InvalidAuthoredTransition
   1267         | radroots_storage::Error::InvalidWorkClaim
   1268         | radroots_storage::Error::DeliveryPlanClaimConflict => Error::WorkClaimConflict,
   1269         other => map_storage_error(other),
   1270     }
   1271 }
   1272 
   1273 #[cfg(test)]
   1274 #[cfg_attr(coverage_nightly, coverage(off))]
   1275 mod tests {
   1276     use super::*;
   1277     use radroots_event::{GenericEventDraft, contract::AuthorRole};
   1278     use radroots_signing::{
   1279         SignerStatus,
   1280         actor::ActorSource,
   1281         capability::{CancellationSupport, SignerCapability, SignerKind},
   1282         status::SignerAvailability,
   1283     };
   1284     use radroots_transport::{
   1285         DeliveryReceipt,
   1286         outcome::DeliveryOutcome,
   1287         policy::{SatisfactionClass, TargetPolicy},
   1288         sink::{DeliveryPayload, DeliveryTargetReceipt},
   1289     };
   1290 
   1291     const AUTHOR: &str = "585591529da0bab31b3b1b1f986611cf5f435dca84f978c89ee8a40cca7103df";
   1292     const OTHER_AUTHOR: &str = "e0266e3cfb0d2886f91c73f5f868f3b98273713e5fcd97c081663f5518a4b3af";
   1293 
   1294     fn request_with(
   1295         target_policy: TargetPolicy,
   1296         deadline: u64,
   1297         cancellation: CancellationPolicy,
   1298     ) -> PushRequest {
   1299         let plan = AuthoredEventPlan::from_generic(
   1300             GenericEventDraft::new(
   1301                 "radroots.social.geochat.v1",
   1302                 20_000,
   1303                 1_800_000_100,
   1304                 Vec::new(),
   1305                 "push helper coverage",
   1306                 AUTHOR,
   1307             )
   1308             .unwrap(),
   1309         )
   1310         .unwrap();
   1311         let actor =
   1312             Actor::from_public_key_hex(AUTHOR, ActorSource::ExplicitPublicKey, [AuthorRole::Any])
   1313                 .unwrap();
   1314         PushRequest::new(
   1315             SyncId::new([31; 16]).unwrap(),
   1316             IdempotencyKey::parse("push-helper-coverage").unwrap(),
   1317             actor,
   1318             plan,
   1319             TargetSet::new(vec![Target::nostr_relay("wss://helper.example").unwrap()]).unwrap(),
   1320             SatisfactionPolicy::new(SatisfactionClass::Accepted, target_policy),
   1321             deadline,
   1322             cancellation,
   1323         )
   1324         .unwrap()
   1325     }
   1326 
   1327     fn delivery_plan(request: &PushRequest) -> AuthoredDeliveryPlan {
   1328         let (_, artifact_id, plan_id) = authored_ids(request.operation_id()).unwrap();
   1329         let delivery_request = DeliveryRequest::new(
   1330             delivery_request_id(request.operation_id()),
   1331             DeliveryPayload::new(
   1332                 radroots_event_codec::Codec::decode_signed_event(
   1333                     r#"{"id":"762bee187e9e645b81ec26ade05a69b5e8398caf527be8de0d9a45311ed0c7a0","pubkey":"585591529da0bab31b3b1b1f986611cf5f435dca84f978c89ee8a40cca7103df","created_at":1800000100,"kind":0,"tags":[],"content":"{\"display_name\":\"Moss Street Farm\",\"bot\":false,\"website\":\"https://mossstreet.example\",\"picture\":42}","sig":"4290da0bb6422986647bc8cd5f63bd52d49f41e7b665d3b47105b8109183e8d596f322c531d4061df53e1d2b70fda12d5d1c14f3720d7a56d9d0a03746af5109"}"#,
   1334                 )
   1335                 .unwrap(),
   1336             ),
   1337             request.targets().clone(),
   1338             request.satisfaction().clone(),
   1339             request.delivery_deadline_unix_ms(),
   1340         )
   1341         .unwrap();
   1342         AuthoredDeliveryPlan::new_bound(plan_id, artifact_id, delivery_request, 10).unwrap()
   1343     }
   1344 
   1345     fn capability(replay: ReplayCapability) -> SignerCapability {
   1346         SignerCapability::new(
   1347             SignerKind::Local,
   1348             replay,
   1349             CancellationSupport::BeforePublication,
   1350             false,
   1351             false,
   1352         )
   1353     }
   1354 
   1355     #[test]
   1356     fn push_request_validates_every_binding_and_exposes_redacted_accessors() {
   1357         let request = request_with(
   1358             TargetPolicy::any(),
   1359             1_800_000_300_000,
   1360             CancellationPolicy::LocalCooperative,
   1361         );
   1362         assert_eq!(request.idempotency_key().as_str(), "push-helper-coverage");
   1363         assert_eq!(request.actor().public_key(), *request.plan().author());
   1364         assert_eq!(request.targets().len(), 1);
   1365         assert!(request.satisfaction().targets().is_any());
   1366         assert_eq!(request.delivery_deadline_unix_ms(), 1_800_000_300_000);
   1367         assert_eq!(request.cancellation(), CancellationPolicy::LocalCooperative);
   1368         let debug = format!("{request:?}");
   1369         assert!(debug.contains("[redacted exact authored plan]"));
   1370         assert!(!debug.contains("push helper coverage"));
   1371 
   1372         let invalid_policy = TargetPolicy::required(vec![
   1373             Target::nostr_relay("wss://absent.example")
   1374                 .unwrap()
   1375                 .fingerprint()
   1376                 .clone(),
   1377         ])
   1378         .unwrap();
   1379         let mut invalid = request.clone();
   1380         invalid.satisfaction = SatisfactionPolicy::new(SatisfactionClass::Accepted, invalid_policy);
   1381         assert!(matches!(
   1382             PushRequest::new(
   1383                 invalid.operation_id,
   1384                 invalid.idempotency_key,
   1385                 invalid.actor,
   1386                 invalid.plan,
   1387                 invalid.targets,
   1388                 invalid.satisfaction,
   1389                 invalid.delivery_deadline_unix_ms,
   1390                 invalid.cancellation,
   1391             ),
   1392             Err(Error::InvalidPushRequest)
   1393         ));
   1394 
   1395         let valid = request_with(
   1396             TargetPolicy::any(),
   1397             1_800_000_300_000,
   1398             CancellationPolicy::PreservePublishedRequest,
   1399         );
   1400         assert!(matches!(
   1401             PushRequest::new(
   1402                 valid.operation_id,
   1403                 valid.idempotency_key.clone(),
   1404                 valid.actor.clone(),
   1405                 valid.plan.clone(),
   1406                 valid.targets.clone(),
   1407                 valid.satisfaction.clone(),
   1408                 0,
   1409                 valid.cancellation,
   1410             ),
   1411             Err(Error::InvalidPushRequest)
   1412         ));
   1413         let wrong_actor = Actor::from_public_key_hex(
   1414             OTHER_AUTHOR,
   1415             ActorSource::ExplicitPublicKey,
   1416             [AuthorRole::Any],
   1417         )
   1418         .unwrap();
   1419         assert!(matches!(
   1420             PushRequest::new(
   1421                 valid.operation_id,
   1422                 valid.idempotency_key,
   1423                 wrong_actor,
   1424                 valid.plan,
   1425                 valid.targets,
   1426                 valid.satisfaction,
   1427                 valid.delivery_deadline_unix_ms,
   1428                 valid.cancellation,
   1429             ),
   1430             Err(Error::InvalidPushRequest)
   1431         ));
   1432     }
   1433 
   1434     #[test]
   1435     fn signer_and_retry_helpers_cover_every_stable_policy_variant() {
   1436         let empty = SignerStatus::new(SignerAvailability::Ready, Vec::new(), None);
   1437         assert_eq!(
   1438             signer_replay_capability(&empty),
   1439             Err(Error::SignerCapabilityUnavailable)
   1440         );
   1441         let exact = SignerStatus::new(
   1442             SignerAvailability::Ready,
   1443             vec![capability(ReplayCapability::ExactReplayByRequestId)],
   1444             None,
   1445         );
   1446         assert_eq!(
   1447             signer_replay_capability(&exact).unwrap(),
   1448             ReplayCapability::ExactReplayByRequestId
   1449         );
   1450         let mixed = SignerStatus::new(
   1451             SignerAvailability::Ready,
   1452             vec![
   1453                 capability(ReplayCapability::ExactReplayByRequestId),
   1454                 capability(ReplayCapability::LocalReplaySafe),
   1455             ],
   1456             None,
   1457         );
   1458         assert_eq!(
   1459             signer_replay_capability(&mixed).unwrap(),
   1460             ReplayCapability::NonReplayable
   1461         );
   1462         assert_eq!(
   1463             signing_claim_owner(ReplayCapability::ExactReplayByRequestId),
   1464             SIGNING_CLAIM_OWNER_EXACT
   1465         );
   1466         assert_eq!(
   1467             signing_claim_owner(ReplayCapability::LocalReplaySafe),
   1468             SIGNING_CLAIM_OWNER_LOCAL
   1469         );
   1470         assert_eq!(
   1471             signing_claim_owner(ReplayCapability::NonReplayable),
   1472             SIGNING_CLAIM_OWNER_NON_REPLAYABLE
   1473         );
   1474 
   1475         let failure = WorkFailure::new(
   1476             "retry",
   1477             WorkPhase::Signing,
   1478             FailureClass::Retryable,
   1479             Some(20),
   1480             None,
   1481         )
   1482         .unwrap();
   1483         assert!(retry_schedule(None, &failure, None).unwrap().is_none());
   1484         let first = retry_schedule(None, &failure, Some(20)).unwrap().unwrap();
   1485         assert_eq!(first.attempt(), NonZeroU32::MIN);
   1486         let next_failure = WorkFailure::new(
   1487             "retry",
   1488             WorkPhase::Signing,
   1489             FailureClass::Retryable,
   1490             Some(21),
   1491             None,
   1492         )
   1493         .unwrap();
   1494         assert_eq!(
   1495             retry_schedule(Some(&first), &next_failure, Some(21))
   1496                 .unwrap()
   1497                 .unwrap()
   1498                 .attempt()
   1499                 .get(),
   1500             2
   1501         );
   1502     }
   1503 
   1504     #[test]
   1505     fn delivery_failure_and_retry_helpers_normalize_all_outcome_classes() {
   1506         let request = request_with(
   1507             TargetPolicy::any(),
   1508             1_800_000_300_000,
   1509             CancellationPolicy::PreservePublishedRequest,
   1510         );
   1511         let plan = delivery_plan(&request);
   1512         let delivery_request = plan.request().unwrap();
   1513         let terminal = SinkFailure::for_request(
   1514             delivery_request,
   1515             "terminal",
   1516             Retryability::Terminal,
   1517             None,
   1518             None,
   1519             Vec::new(),
   1520         )
   1521         .unwrap();
   1522         assert_eq!(
   1523             normalize_sink_failure(delivery_request, terminal.clone(), 100).unwrap(),
   1524             terminal
   1525         );
   1526         let retryable = SinkFailure::for_request(
   1527             delivery_request,
   1528             "retryable",
   1529             Retryability::Retryable,
   1530             None,
   1531             Some("retry".to_owned()),
   1532             Vec::new(),
   1533         )
   1534         .unwrap();
   1535         let normalized = normalize_sink_failure(delivery_request, retryable, 100).unwrap();
   1536         assert_eq!(normalized.retry_after_unix_ms(), Some(1_100));
   1537         assert_eq!(
   1538             normalize_sink_failure(delivery_request, normalized.clone(), 100).unwrap(),
   1539             normalized
   1540         );
   1541         let past = SinkFailure::for_request(
   1542             delivery_request,
   1543             "past",
   1544             Retryability::Retryable,
   1545             Some(99),
   1546             None,
   1547             Vec::new(),
   1548         )
   1549         .unwrap();
   1550         assert_eq!(
   1551             normalize_sink_failure(delivery_request, past, 100)
   1552                 .unwrap()
   1553                 .retry_after_unix_ms(),
   1554             Some(101)
   1555         );
   1556 
   1557         let receipt = DeliveryReceipt::for_request(
   1558             delivery_request,
   1559             delivery_request
   1560                 .target_set()
   1561                 .targets()
   1562                 .iter()
   1563                 .cloned()
   1564                 .map(|target| {
   1565                     DeliveryTargetReceipt::attempted(target, DeliveryOutcome::unavailable())
   1566                 })
   1567                 .collect(),
   1568         )
   1569         .unwrap();
   1570         let receipt_outcome = DeliveryAttemptOutcome::Receipt(receipt);
   1571         assert!(
   1572             delivery_retry_schedule(
   1573                 NonZeroU32::new(plan.attempt_count() + 1).unwrap(),
   1574                 &receipt_outcome,
   1575                 SatisfactionState::Satisfied,
   1576                 100
   1577             )
   1578             .unwrap()
   1579             .is_none()
   1580         );
   1581         let schedule = delivery_retry_schedule(
   1582             NonZeroU32::new(plan.attempt_count() + 1).unwrap(),
   1583             &receipt_outcome,
   1584             SatisfactionState::Pending,
   1585             100,
   1586         )
   1587         .unwrap()
   1588         .unwrap();
   1589         assert_eq!(schedule.not_before_unix_ms(), 1_100);
   1590         let retry_outcome = DeliveryAttemptOutcome::SinkFailure(normalized);
   1591         assert!(
   1592             delivery_retry_schedule(
   1593                 NonZeroU32::new(plan.attempt_count() + 1).unwrap(),
   1594                 &retry_outcome,
   1595                 SatisfactionState::Pending,
   1596                 100
   1597             )
   1598             .unwrap()
   1599             .is_some()
   1600         );
   1601         let terminal_outcome = DeliveryAttemptOutcome::SinkFailure(terminal);
   1602         assert!(
   1603             delivery_retry_schedule(
   1604                 NonZeroU32::new(plan.attempt_count() + 1).unwrap(),
   1605                 &terminal_outcome,
   1606                 SatisfactionState::Pending,
   1607                 100
   1608             )
   1609             .unwrap()
   1610             .is_none()
   1611         );
   1612 
   1613         for policy in [
   1614             TargetPolicy::any(),
   1615             TargetPolicy::all(),
   1616             TargetPolicy::quorum(1).unwrap(),
   1617             TargetPolicy::required(vec![request.targets().targets()[0].fingerprint().clone()])
   1618                 .unwrap(),
   1619         ] {
   1620             let request = request_with(
   1621                 policy,
   1622                 1_800_000_300_000,
   1623                 CancellationPolicy::LocalCooperative,
   1624             );
   1625             assert_ne!(
   1626                 authored_push_input_digest(&request).unwrap().as_bytes(),
   1627                 &[0; 32]
   1628             );
   1629         }
   1630     }
   1631 
   1632     #[test]
   1633     fn storage_error_maps_are_explicit_and_fail_closed() {
   1634         assert_eq!(
   1635             map_storage_error(radroots_storage::Error::SpaceInsufficient),
   1636             Error::StorageSpaceInsufficient
   1637         );
   1638         assert_eq!(
   1639             map_claim_error(radroots_storage::Error::SpaceInsufficient),
   1640             Error::StorageSpaceInsufficient
   1641         );
   1642         assert_eq!(
   1643             map_storage_error(radroots_storage::Error::AtomicCommitConflict),
   1644             Error::StorageConflict
   1645         );
   1646         assert_eq!(
   1647             map_storage_error(radroots_storage::Error::BackendUnavailable),
   1648             Error::StorageFailed
   1649         );
   1650         assert_eq!(
   1651             map_claim_error(radroots_storage::Error::DeliveryPlanClaimConflict),
   1652             Error::WorkClaimConflict
   1653         );
   1654         assert_eq!(
   1655             map_claim_error(radroots_storage::Error::BackendUnavailable),
   1656             Error::StorageFailed
   1657         );
   1658         assert_eq!(
   1659             delivery_request_id(SyncId::new([0xab; 16]).unwrap()).len(),
   1660             37
   1661         );
   1662     }
   1663 }