(
repo: &Repository,
method: &str,
meta: &RequestMeta,
input: Value,
action: impl FnOnce() -> Result<T, Status>,
)
| 64 | } |
| 65 | |
| 66 | pub(super) fn run<T: Message + Default>( |
| 67 | repo: &Repository, |
| 68 | method: &str, |
| 69 | meta: &RequestMeta, |
| 70 | input: Value, |
| 71 | action: impl FnOnce() -> Result<T, Status>, |
| 72 | ) -> Result<(T, ResponseMeta), Status> { |
| 73 | let result = (|| { |
| 74 | let _boundary = repo.lock_review_publication().map_err(repository_error)?; |
| 75 | let root = blake3::hash(repo.root().to_string_lossy().as_bytes()).to_hex(); |
| 76 | let path = format!( |
| 77 | "review-service-requests/{root}/{method}/{}/request.md", |
| 78 | meta.request_id |
| 79 | ); |
| 80 | let fingerprint = atomic_canonical::hash::content_hash( |
| 81 | &json!({"method":method,"input":input,"observed_at":meta.observed_at.as_ref().map(|time| (time.seconds,time.nanos))}), |
| 82 | ); |
| 83 | let old = repo.vault_retrieve(&path).map_err(repository_error)?; |
| 84 | let replayed = old.is_some(); |
| 85 | if let Some(old) = old { |
| 86 | let value = atomic_canonical::jcs::admit_document(&old.content_bytes) |
| 87 | .map_err(|error| Status::data_loss(error.to_string()))?; |
| 88 | let receipt: Receipt = serde_json::from_value(value) |
| 89 | .map_err(|error| Status::data_loss(error.to_string()))?; |
| 90 | if receipt.schema != "atomic.review.request.v1" |
| 91 | || receipt.response.is_some() != receipt.response_hash.is_some() |
| 92 | { |
| 93 | return Err(Status::data_loss( |
| 94 | "unsupported or inconsistent review request receipt", |
| 95 | )); |
| 96 | } |
| 97 | if receipt.fingerprint != fingerprint { |
| 98 | return Err(Status::already_exists( |
| 99 | "IDEMPOTENCY_MISMATCH: request_id was used with different review inputs", |
| 100 | )); |
| 101 | } |
| 102 | if let Some(response) = receipt.response { |
| 103 | let bytes = data_encoding::BASE64 |
| 104 | .decode(response.as_bytes()) |
| 105 | .map_err(|error| Status::data_loss(error.to_string()))?; |
| 106 | if receipt.response_hash.as_deref() != Some(blake3::hash(&bytes).to_hex().as_str()) |
| 107 | { |
| 108 | return Err(Status::data_loss( |
| 109 | "review request receipt response hash mismatch", |
| 110 | )); |
| 111 | } |
| 112 | let response = T::decode(bytes.as_slice()) |
| 113 | .map_err(|error| Status::data_loss(error.to_string()))?; |
| 114 | return Ok(( |
| 115 | response, |
| 116 | ResponseMeta { |
| 117 | request_id: meta.request_id.clone(), |
| 118 | replayed: true, |
| 119 | snapshot: None, |
| 120 | }, |
| 121 | )); |
| 122 | } |
| 123 | } else { |
no test coverage detected