mkit-server 0.5.0

Runtime-agnostic core of the mkit server: operation model, errors, runtime and telemetry vocabulary
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
use base64::{Engine as _, engine::general_purpose::STANDARD};
use mkit_core::hash::{hash, to_hex};
use serde::{Deserialize, Serialize};
use serde_json::{Value as Json, json};

use crate::{
    Batch, BatchOutcome, Code, Key, NamespaceStore, Partition, Precondition, ServerError,
    StoreError, Value,
};

use super::{
    AUDIT_PATH, BodyCapture, Engine, Headers, MAX_BODY, Response,
    auth::{self, Verified},
    payload,
};

const RETRIES: usize = 16;
const PAGE_BYTES: usize = 256 * 1024;

#[derive(Default, Serialize, Deserialize)]
pub(super) struct Head {
    pub(super) seq: u64,
    pub(super) hash: String,
}
#[derive(Serialize, Deserialize)]
struct Nonce {
    digest: String,
    path: String,
    expiry_ms: u64,
    result: Option<Response>,
}
#[derive(Serialize, Deserialize)]
struct Operation {
    digest: String,
    path: String,
    result: Response,
    nonce: Option<String>,
}

/// A durable operation replay or an acceptance batch for a new operation.
#[derive(Debug)]
pub enum OperationReplay {
    /// The original response, including stable action identity and pending state.
    Existing(Response),
    /// Merge this guarded batch with the operation's action and audit acceptance.
    New(Batch),
}
/// Plan persistent operation-id deduplication after authentication and role checks.
/// A new action must commit this batch atomically with its audit and intent.
/// # Errors
/// Invalid identities, changed logical requests, or unavailable/corrupt storage.
pub async fn plan_operation<S: NamespaceStore>(
    store: &S,
    partition: &Partition,
    operation_id: &str,
    path: &str,
    digest: &str,
    result: Response,
) -> Result<OperationReplay, ServerError> {
    if !auth::identifier(operation_id, 128, true)
        || !path.starts_with(super::PREFIX)
        || digest
            .strip_prefix("body:")
            .and_then(auth::hex::<32>)
            .is_none()
    {
        return Err(auth::invalid("invalid operation replay identity"));
    }
    let key = key("ao", operation_id.as_bytes());
    if let Some(value) = store.get(partition, &key).await.map_err(store_error)? {
        let first: Operation = decode(&value)?;
        if first.digest != digest || first.path != path {
            return Err(auth::invalid(
                "operation id reused with a different request",
            ));
        }
        return Ok(OperationReplay::Existing(first.result));
    }
    let value = encode(&Operation {
        digest: digest.into(),
        path: path.into(),
        result,
        nonce: None,
    })?;
    Ok(OperationReplay::New(
        Batch::new()
            .require(Precondition::Absent(key.clone()))
            .put(key, value),
    ))
}

/// Retryable acceptance, carrying the stable identity while denial activation runs.
pub(crate) fn takedown_pending(response: &Response) -> Result<Response, ServerError> {
    let mut body: Json = serde_json::from_slice(&response.body)
        .map_err(|_| ServerError::unavailable("invalid takedown response"))?;
    if body["takedownId"]
        .as_str()
        .and_then(auth::hex::<32>)
        .is_none()
    {
        return Err(ServerError::unavailable("invalid takedown identity"));
    }
    body["code"] = json!("unavailable");
    body["message"] = json!("takedown denial activation is in flight");
    let mut pending = Response::json(&body);
    pending.status = 503;
    Ok(pending)
}

fn pending_takedown(response: &Response) -> bool {
    response.status == 503
        && serde_json::from_slice::<Json>(&response.body).is_ok_and(|body| {
            body["takedownId"]
                .as_str()
                .and_then(auth::hex::<32>)
                .is_some()
        })
}

/// Complete existing replay rows in the same guarded apply as the activation proof.
pub(crate) async fn plan_takedown_completion<S: NamespaceStore>(
    store: &S,
    partition: &Partition,
    operation_id: &str,
    digest: &str,
) -> Result<Batch, ServerError> {
    let operation_key = key("ao", operation_id.as_bytes());
    let Some(old) = store
        .get(partition, &operation_key)
        .await
        .map_err(store_error)?
    else {
        // The intent service can also be exercised without the signed admin engine.
        return Ok(Batch::new());
    };
    let mut operation: Operation = decode(&old)?;
    if operation.path != super::TAKEDOWN_PATH || operation.digest != digest {
        return Err(ServerError::unavailable("invalid takedown replay binding"));
    }
    if operation.result.status == 200 {
        return Ok(Batch::new());
    }
    if !pending_takedown(&operation.result) {
        return Err(ServerError::unavailable("invalid takedown pending result"));
    }
    let body: Json = serde_json::from_slice(&operation.result.body)
        .map_err(|_| ServerError::unavailable("invalid takedown result"))?;
    operation.result = Response::json(&json!({"takedownId":body["takedownId"],"complete":false}));
    let mut batch = guarded(Batch::new(), operation_key.clone(), Some(old));
    if let Some(nonce) = &operation.nonce {
        let nonce_key = key("an", nonce.as_bytes());
        let raw = store
            .get(partition, &nonce_key)
            .await
            .map_err(store_error)?
            .ok_or_else(|| ServerError::unavailable("missing takedown nonce"))?;
        let mut nonce: Nonce = decode(&raw)?;
        if nonce.digest != digest
            || nonce.path != super::TAKEDOWN_PATH
            || !nonce.result.as_ref().is_some_and(pending_takedown)
        {
            return Err(ServerError::unavailable("invalid takedown nonce binding"));
        }
        nonce.result = Some(operation.result.clone());
        batch = guarded(batch, nonce_key.clone(), Some(raw)).put(nonce_key, encode(&nonce)?);
    }
    Ok(batch.put(operation_key, encode(&operation)?))
}

fn key(tag: &str, suffix: &[u8]) -> Key {
    Key::new([tag.as_bytes(), b"\0", suffix].concat())
}
pub(super) fn head_key() -> Key {
    key("ah", b"")
}
fn entry_key(seq: u64) -> Key {
    key("ae", &seq.to_be_bytes())
}
fn store_error(_: StoreError) -> ServerError {
    ServerError::new(Code::Unavailable, "admin storage unavailable")
}
pub(super) fn encode<T: Serialize>(value: &T) -> Result<Value, ServerError> {
    let bytes = serde_json::to_vec(value)
        .map_err(|_| ServerError::new(Code::Internal, "admin encoding failed"))?;
    if bytes.len() > crate::MAX_VALUE_BYTES {
        return Err(ServerError::new(
            Code::Internal,
            "admin replay result exceeds storage bound",
        ));
    }
    Ok(Value::new(bytes))
}
pub(super) fn decode<T: serde::de::DeserializeOwned>(value: &Value) -> Result<T, ServerError> {
    serde_json::from_slice(value.as_bytes())
        .map_err(|_| ServerError::new(Code::DataLoss, "corrupt admin ledger"))
}
pub(super) fn guarded(batch: Batch, key: Key, old: Option<Value>) -> Batch {
    batch.require(match old {
        Some(value) => Precondition::Equals(key, value),
        None => Precondition::Absent(key),
    })
}
fn previous(head: &Head) -> String {
    if head.seq == 0 {
        "00".repeat(32)
    } else {
        head.hash.clone()
    }
}
pub(super) fn decode_head(value: Option<&Value>) -> Result<Head, ServerError> {
    let head: Head = value.map_or_else(|| Ok(Head::default()), decode)?;
    if head.seq > 0 && auth::hex::<32>(&head.hash).is_none() {
        return Err(ServerError::new(Code::DataLoss, "corrupt audit head"));
    }
    Ok(head)
}
fn hash_entry(entry: &Json) -> Result<String, ServerError> {
    let mut value = entry.clone();
    value
        .as_object_mut()
        .ok_or_else(|| ServerError::new(Code::DataLoss, "corrupt audit entry"))?
        .remove("entryHash");
    // Every key is fixed ASCII, integers are decimal strings, and there are no
    // floats: serde_json's sorted map and UTF-8 string encoding are JCS here.
    let mut bytes = b"mkit-admin-audit:v1".to_vec();
    bytes.extend(
        serde_json::to_vec(&value)
            .map_err(|_| ServerError::new(Code::Internal, "audit encoding failed"))?,
    );
    Ok(to_hex(&hash(&bytes)))
}
#[allow(clippy::too_many_arguments)] // Every fixed canonical audit field is explicit at its three append sites.
pub(super) fn audit_entry(
    head: &Head,
    actor: &str,
    path: &str,
    digest: &str,
    nonce: &str,
    operation: &str,
    label: &str,
    targets: &[String],
    result: &Response,
    details: &str,
    now: u64,
) -> Result<(Json, Head), ServerError> {
    let seq = head
        .seq
        .checked_add(1)
        .ok_or_else(|| ServerError::new(Code::Internal, "audit sequence exhausted"))?;
    let outcome = if result.status == 200 {
        json!({"code":"ok","message":""})
    } else {
        serde_json::from_slice::<Json>(&result.body)
            .map_err(|_| ServerError::new(Code::Internal, "invalid audit result"))?
    };
    let mut entry = json!({"seq":seq.to_string(),"recordedAtMs":now.to_string(),"actor":actor,"procedure":path,
        "requestDigest":digest,"nonce":nonce,"targets":targets,"result":outcome,"details":details,"prevHash":previous(head)});
    if !operation.is_empty() {
        entry["operationId"] = json!(operation);
    }
    if !label.is_empty() {
        entry["operatorLabel"] = json!(label);
    }
    let entry_hash = hash_entry(&entry)?;
    entry["entryHash"] = json!(entry_hash);
    Ok((
        entry,
        Head {
            seq,
            hash: entry_hash,
        },
    ))
}

/// Plan an automatic action's audit append; combine this batch with the action
/// acceptance batch and retry planning if any CAS guard loses.
///
/// # Errors
/// Invalid automatic actor/target or corrupt/unavailable audit storage.
pub async fn plan_system<S: NamespaceStore>(
    store: &S,
    partition: &Partition,
    actor: &str,
    procedure: &str,
    targets: &[String],
    now_ms: u64,
) -> Result<Batch, StoreError> {
    if !matches!(actor, "system:inspector" | "system:timer" | "system:relay")
        || !procedure.starts_with(&format!("{actor}/"))
        || procedure.len() > 256
        || procedure.chars().any(char::is_control)
        || targets.len() > 256
        || targets
            .iter()
            .any(|t| t.len() > 1024 || t.chars().any(char::is_control))
    {
        return Err(StoreError::Invalid("invalid system audit identity".into()));
    }
    let old = store.get(partition, &head_key()).await?;
    let head =
        decode_head(old.as_ref()).map_err(|_| StoreError::Invalid("corrupt audit head".into()))?;
    let (entry, next) = audit_entry(
        &head,
        actor,
        procedure,
        "",
        "",
        "",
        "",
        targets,
        &Response::json(&json!({})),
        "",
        now_ms,
    )
    .map_err(|_| StoreError::Invalid("invalid audit entry".into()))?;
    let batch = guarded(Batch::new(), head_key(), old)
        .put(
            entry_key(next.seq),
            encode(&entry).map_err(|_| StoreError::Invalid("audit entry too large".into()))?,
        )
        .put(
            head_key(),
            encode(&next).map_err(|_| StoreError::Invalid("invalid audit head".into()))?,
        );
    Ok(batch)
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct ReadInput {
    #[serde(alias = "from_seq")]
    from_seq: Json,
    #[serde(alias = "page_size")]
    page_size: Json,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct PurgeInput {
    #[serde(alias = "operation_id")]
    operation_id: String,
    #[serde(default)]
    repository: String,
    #[serde(default)]
    namespace: String,
    #[serde(default, alias = "url_paths")]
    url_paths: Vec<String>,
    #[serde(default, alias = "object_ids")]
    object_ids: Vec<String>,
    #[serde(default)]
    refs: Vec<String>,
    reason: String,
    #[serde(default, alias = "operator_label")]
    operator_label: String,
}
enum Action {
    Read(u64, u32),
    Failure(ServerError),
    Extension(Json),
}
impl<S: NamespaceStore> Engine<S> {
    #[allow(clippy::too_many_lines)] // Authentication, durable nonce reservation and terminal audited dispatch share one lifecycle.
    pub(super) async fn dispatch(
        &self,
        path: &str,
        headers: &Headers,
        wire: &BodyCapture,
        decoded: Option<Result<Vec<u8>, ServerError>>,
        now: i64,
        streaming: bool,
    ) -> Result<Response, ServerError> {
        let verified = self.config.verify(path, headers, wire, now)?;
        let budget = crate::indexed::budget::SliceBudget::new(9_000);
        let nonce_key = key("an", verified.replay_key.as_bytes());
        let nonce = Nonce {
            digest: verified.digest.clone(),
            path: path.to_owned(),
            expiry_ms: verified.expiry_ms,
            result: None,
        };
        let nonce_value = encode(&nonce)?;
        match self
            .store
            .apply(
                &self.partition,
                Batch::new()
                    .require(Precondition::Absent(nonce_key.clone()))
                    .put(nonce_key.clone(), nonce_value.clone()),
            )
            .await
            .map_err(store_error)?
        {
            BatchOutcome::Committed => {}
            BatchOutcome::PreconditionFailed {
                observed: Some(existing),
                ..
            } => {
                let old: Nonce = decode(&existing)?;
                if old.digest != verified.digest || old.path != path {
                    return self
                        .conflict(
                            &verified,
                            now,
                            "admin nonce reused with a different request",
                        )
                        .await;
                }
                if path == super::READ_PRESERVED_PATH && !streaming {
                    return self
                        .record_result(
                            &verified,
                            now,
                            Response::error(&ServerError::failed_precondition(
                                "streaming admin adapter required",
                            )),
                        )
                        .await;
                }
                let Some(mut result) = old.result else {
                    return self
                        .record_result(
                            &verified,
                            now,
                            Response::error(&ServerError::new(
                                Code::Aborted,
                                "admin request is in flight",
                            )),
                        )
                        .await;
                };
                if path == super::TAKEDOWN_PATH && pending_takedown(&result) {
                    let bytes = decoded.as_ref().map_or(Ok(wire.bytes.as_slice()), |d| {
                        d.as_ref().map(Vec::as_slice).map_err(Clone::clone)
                    })?;
                    let input: Json = serde_json::from_slice(payload(path, bytes)?)
                        .map_err(|_| auth::invalid("invalid admin JSON"))?;
                    let operation = input["operationId"]
                        .as_str()
                        .or_else(|| input["operation_id"].as_str())
                        .ok_or_else(|| auth::invalid("invalid operation id"))?;
                    if let OperationReplay::Existing(stored) = plan_operation(
                        &self.store,
                        &self.partition,
                        operation,
                        path,
                        &verified.digest,
                        result.clone(),
                    )
                    .await?
                    {
                        if stored.status == 200 {
                            return self.record_takedown_success(&verified, now, stored).await;
                        }
                        result = stored;
                    }
                }
                return self
                    .finish_extension(
                        &verified,
                        wire,
                        decoded.as_ref(),
                        result,
                        now,
                        &budget,
                        true,
                    )
                    .await;
            }
            _ => {
                return Err(ServerError::new(
                    Code::Unavailable,
                    "admin nonce reservation failed",
                ));
            }
        }
        let decoded_reply = decoded.clone();
        let action = if !verified.roles.contains("all")
            && !verified.roles.contains(if path == AUDIT_PATH {
                "audit"
            } else {
                "moderation"
            }) {
            Action::Failure(ServerError::permission_denied(
                "admin key lacks required role",
            ))
        } else if path == super::READ_PRESERVED_PATH && !streaming {
            Action::Failure(ServerError::failed_precondition(
                "streaming admin adapter required",
            ))
        } else if wire.oversized {
            Action::Failure(auth::invalid("admin request exceeds 1 MiB"))
        } else {
            let bytes = decoded.unwrap_or_else(|| Ok(wire.bytes.clone()));
            match bytes.and_then(|bytes| {
                if bytes.len() > MAX_BODY {
                    return Err(auth::invalid("decoded admin request exceeds 1 MiB"));
                }
                let body = payload(path, &bytes)?;
                if path == AUDIT_PATH {
                    let input: ReadInput = serde_json::from_slice(body)
                        .map_err(|_| auth::invalid("invalid ReadAuditLog JSON"))?;
                    let number = |j: &Json| {
                        j.as_str()
                            .and_then(auth::decimal_u64)
                            .or_else(|| j.as_u64())
                    };
                    let from = number(&input.from_seq)
                        .filter(|n| *n >= 1)
                        .ok_or_else(|| auth::invalid("invalid audit sequence"))?;
                    let size = number(&input.page_size)
                        .filter(|n| (1..=100).contains(n))
                        .ok_or_else(|| auth::invalid("invalid audit page size"))?;
                    Ok(Action::Read(
                        from,
                        u32::try_from(size)
                            .map_err(|_| auth::invalid("invalid audit page size"))?,
                    ))
                } else if path == super::PURGE_PATH
                    || super::extension_path(path) && self.operations.is_some()
                {
                    let input = serde_json::from_slice(body)
                        .map_err(|_| auth::invalid("invalid admin JSON"))?;
                    Ok(Action::Extension(input))
                } else {
                    Err(ServerError::new(
                        Code::Unimplemented,
                        "admin operation is outside the launch subset",
                    ))
                }
            }) {
                Ok(action) => action,
                Err(error) => Action::Failure(error),
            }
        };
        let response = self
            .complete(&verified, &nonce_key, &nonce_value, action, now, &budget)
            .await?;
        self.finish_extension(
            &verified,
            wire,
            decoded_reply.as_ref(),
            response,
            now,
            &budget,
            false,
        )
        .await
    }

    async fn conflict(
        &self,
        verified: &Verified,
        now: i64,
        message: &str,
    ) -> Result<Response, ServerError> {
        self.record_result(verified, now, Response::error(&auth::invalid(message)))
            .await
    }

    pub(super) async fn record_result(
        &self,
        verified: &Verified,
        now: i64,
        result: Response,
    ) -> Result<Response, ServerError> {
        self.record_result_inner(verified, now, result, false).await
    }

    async fn record_takedown_success(
        &self,
        verified: &Verified,
        now: i64,
        result: Response,
    ) -> Result<Response, ServerError> {
        self.record_result_inner(verified, now, result, true).await
    }

    async fn record_result_inner(
        &self,
        verified: &Verified,
        now: i64,
        result: Response,
        finalize_nonce: bool,
    ) -> Result<Response, ServerError> {
        for _ in 0..RETRIES {
            let old = self
                .store
                .get(&self.partition, &head_key())
                .await
                .map_err(store_error)?;
            let head = decode_head(old.as_ref())?;
            let (entry, next) = audit_entry(
                &head,
                &verified.actor,
                &verified.path,
                &verified.digest,
                &verified.nonce,
                "",
                "",
                &[],
                &result,
                "",
                u64::try_from(now).unwrap_or(0),
            )?;
            let mut batch = guarded(Batch::new(), head_key(), old)
                .put(entry_key(next.seq), encode(&entry)?)
                .put(head_key(), encode(&next)?);
            if finalize_nonce {
                let nonce_key = key("an", verified.replay_key.as_bytes());
                let raw = self
                    .store
                    .get(&self.partition, &nonce_key)
                    .await
                    .map_err(store_error)?
                    .ok_or_else(|| ServerError::unavailable("missing takedown nonce"))?;
                let mut nonce: Nonce = decode(&raw)?;
                if nonce.digest != verified.digest
                    || nonce.path != verified.path
                    || !nonce
                        .result
                        .as_ref()
                        .is_some_and(|r| r.status == 200 || pending_takedown(r))
                {
                    return Err(ServerError::unavailable("invalid takedown nonce binding"));
                }
                nonce.result = Some(result.clone());
                batch =
                    guarded(batch, nonce_key.clone(), Some(raw)).put(nonce_key, encode(&nonce)?);
            }
            if self
                .store
                .apply(&self.partition, batch)
                .await
                .map_err(store_error)?
                == BatchOutcome::Committed
            {
                return Ok(result);
            }
        }
        Err(ServerError::new(Code::Unavailable, "audit contention"))
    }

    #[allow(clippy::too_many_lines)] // Audit, action acceptance, replay and purge effects must be assembled in one guarded transaction.
    async fn complete(
        &self,
        verified: &Verified,
        nonce_key: &Key,
        nonce_value: &Value,
        action: Action,
        now: i64,
        budget: &crate::indexed::budget::SliceBudget,
    ) -> Result<Response, ServerError> {
        let now = u64::try_from(now)
            .map_err(|_| ServerError::new(Code::Unavailable, "invalid backend clock"))?;
        for _ in 0..RETRIES {
            let old = self
                .store
                .get(&self.partition, &head_key())
                .await
                .map_err(store_error)?;
            let head = decode_head(old.as_ref())?;
            let mut batch = guarded(Batch::new(), head_key(), old);
            let mut metadata = (String::new(), String::new(), Vec::new(), String::new());
            let response = match &action {
                Action::Failure(error) => Response::error(error),
                Action::Read(from, size) => {
                    let result = self.read_page(&head, *from, *size).await;
                    result.unwrap_or_else(|e| Response::error(&e))
                }
                Action::Extension(input) => {
                    let planned = if verified.path == super::PURGE_PATH {
                        self.plan_purge(input, &verified.digest, now).await
                    } else if let Some(service) = &self.operations {
                        service
                            .plan(&verified.path, input, &verified.digest, now, budget)
                            .await
                    } else {
                        Err(ServerError::new(
                            Code::Unimplemented,
                            "admin operation unavailable",
                        ))
                    };
                    match planned {
                        Err(error) => Response::error(&error),
                        Ok(mut prepared) => {
                            if verified.path == super::TAKEDOWN_PATH
                                && prepared.response.status == 200
                            {
                                prepared.response = takedown_pending(&prepared.response)?;
                            }
                            metadata = (
                                prepared.operation_id,
                                prepared.label,
                                prepared.targets,
                                prepared.details,
                            );
                            let replay = if metadata.0.is_empty() {
                                Ok(OperationReplay::New(Batch::new()))
                            } else {
                                plan_operation(
                                    &self.store,
                                    &self.partition,
                                    &metadata.0,
                                    &verified.path,
                                    &verified.digest,
                                    prepared.response.clone(),
                                )
                                .await
                            };
                            match replay {
                                Ok(OperationReplay::Existing(result)) => result,
                                Ok(OperationReplay::New(mut replay)) => {
                                    if verified.path == super::TAKEDOWN_PATH
                                        && pending_takedown(&prepared.response)
                                    {
                                        for write in &mut replay.writes {
                                            if let crate::store::Write::Put(_, value) = write {
                                                let mut operation: Operation = decode(value)?;
                                                operation.nonce = Some(verified.replay_key.clone());
                                                *value = encode(&operation)?;
                                            }
                                        }
                                    }
                                    batch.preconditions.extend(prepared.batch.preconditions);
                                    batch.writes.extend(prepared.batch.writes);
                                    batch.preconditions.extend(replay.preconditions);
                                    batch.writes.extend(replay.writes);
                                    prepared.response
                                }
                                Err(error) => Response::error(&error),
                            }
                        }
                    }
                }
            };
            let (entry, next) = audit_entry(
                &head,
                &verified.actor,
                &verified.path,
                &verified.digest,
                &verified.nonce,
                &metadata.0,
                &metadata.1,
                &metadata.2,
                &response,
                &metadata.3,
                now,
            )?;
            let terminal = Nonce {
                digest: verified.digest.clone(),
                path: verified.path.clone(),
                expiry_ms: verified.expiry_ms,
                result: Some(response.clone()),
            };
            let batch = batch
                .require(Precondition::Equals(nonce_key.clone(), nonce_value.clone()))
                .put(entry_key(next.seq), encode(&entry)?)
                .put(head_key(), encode(&next)?)
                .put(nonce_key.clone(), encode(&terminal)?);
            if self
                .store
                .apply(&self.partition, batch)
                .await
                .map_err(store_error)?
                == BatchOutcome::Committed
            {
                return Ok(response);
            }
        }
        Err(ServerError::new(
            Code::Unavailable,
            "admin acceptance contention",
        ))
    }

    async fn finish_extension(
        &self,
        verified: &Verified,
        wire: &BodyCapture,
        decoded: Option<&Result<Vec<u8>, ServerError>>,
        response: Response,
        now: i64,
        budget: &crate::indexed::budget::SliceBudget,
        replayed: bool,
    ) -> Result<Response, ServerError> {
        let pending = verified.path == super::TAKEDOWN_PATH && pending_takedown(&response);
        if (response.status != 200 && !pending)
            || !matches!(
                verified.path.as_str(),
                super::TAKEDOWN_PATH | super::READ_PRESERVED_PATH
            )
        {
            return Ok(response);
        }
        if !pending && verified.path == super::TAKEDOWN_PATH {
            // Success is stored only after activation. Completed replay does
            // not depend on current roles or runtime operations.
            return Ok(response);
        }
        let Some(service) = &self.operations else {
            return Err(ServerError::unavailable("takedown service unavailable"));
        };
        if verified.path == super::READ_PRESERVED_PATH
            && !verified.roles.contains("moderation")
            && !verified.roles.contains("all")
        {
            return self
                .record_result(
                    verified,
                    now,
                    Response::error(&ServerError::permission_denied(
                        "admin key lacks required role",
                    )),
                )
                .await;
        }
        let bytes = match decoded {
            Some(Ok(bytes)) => bytes.as_slice(),
            Some(Err(error)) => {
                return self
                    .record_result(verified, now, Response::error(error))
                    .await;
            }
            None => &wire.bytes,
        };
        let input = serde_json::from_slice(payload(&verified.path, bytes)?)
            .map_err(|_| auth::invalid("invalid admin JSON"))?;
        if verified.path == super::READ_PRESERVED_PATH {
            // The stored descriptor carries no bytes. Every retry performs fresh
            // policy/ownership checks and commits its own acceptance audit.
            let result = service
                .plan(
                    &verified.path,
                    &input,
                    &verified.digest,
                    u64::try_from(now).map_err(|_| auth::invalid("invalid clock"))?,
                    budget,
                )
                .await
                .map_or_else(|e| Response::error(&e), |p| p.response);
            return if replayed || result.status != 200 {
                self.record_result(verified, now, result).await
            } else {
                Ok(result)
            };
        }
        let pending_response = response.clone();
        match service
            .after_commit(
                &verified.path,
                &input,
                response,
                u64::try_from(now).map_err(|_| auth::invalid("invalid clock"))?,
                budget,
            )
            .await
        {
            Ok(response) if response.status == 200 => {
                self.record_takedown_success(verified, now, response).await
            }
            Ok(response) => Ok(response),
            Err(error) => {
                self.record_result(
                    verified,
                    now,
                    if pending {
                        pending_response
                    } else {
                        Response::error(&error)
                    },
                )
                .await
            }
        }
    }

    async fn plan_purge(
        &self,
        input: &Json,
        digest: &str,
        now: u64,
    ) -> Result<super::Prepared, ServerError> {
        let input: PurgeInput = serde_json::from_value(input.clone())
            .map_err(|_| auth::invalid("invalid PurgeCache JSON"))?;
        if !auth::identifier(&input.operation_id, 128, true)
            || input.reason.is_empty()
            || input.reason.len() > 512
            || input.operator_label.len() > 128
            || input
                .reason
                .chars()
                .chain(input.operator_label.chars())
                .any(char::is_control)
        {
            return Err(auth::invalid("invalid purge identity or reason"));
        }
        let id = to_hex(&hash(
            format!(
                "mkit-manual-purge:v1\0{}\0{}",
                self.config.audience, input.operation_id
            )
            .as_bytes(),
        ));
        let request = crate::purge::Request {
            purge_id: id.clone(),
            audience: self.config.audience.clone(),
            repository: input.repository,
            namespace: input.namespace,
            trigger: crate::purge::Trigger::Manual,
            url_paths: input.url_paths,
            object_ids: input.object_ids,
            refs: input.refs,
        };
        request
            .validate()
            .map_err(|_| auth::invalid("invalid purge selectors"))?;
        let mut prepared = super::Prepared {
            batch: Batch::new(),
            response: Response::json(&json!({"purgeId":id})),
            operation_id: input.operation_id,
            label: input.operator_label,
            targets: vec![request.scope().into()],
            details: input.reason,
        };
        if let OperationReplay::Existing(response) = plan_operation(
            &self.store,
            &self.partition,
            &prepared.operation_id,
            super::PURGE_PATH,
            digest,
            prepared.response.clone(),
        )
        .await?
        {
            prepared.response = response;
            return Ok(prepared);
        }
        if !self.purge_enabled {
            return Err(ServerError::failed_precondition(
                "purge interface not configured",
            ));
        }
        let rows = self
            .store
            .get_many(
                &self.partition,
                &[
                    crate::store::keys::outcome_backlog(),
                    crate::store::keys::cache_purge_generation(request.scope()),
                ],
            )
            .await
            .map_err(store_error)?;
        if rows.len() != 2 {
            return Err(ServerError::new(Code::DataLoss, "invalid purge state"));
        }
        prepared.batch =
            crate::purge::plan_enqueue(&request, now, rows[0].as_ref(), rows[1].as_ref())
                .map_err(store_error)?;
        Ok(prepared)
    }

    async fn read_page(&self, head: &Head, from: u64, size: u32) -> Result<Response, ServerError> {
        if from > head.seq.saturating_add(1) {
            return Err(auth::invalid("audit sequence beyond chain head"));
        }
        let mut entries = Vec::new();
        let mut next = from;
        let mut previous_hash = if from == 1 {
            "00".repeat(32)
        } else {
            let row = self
                .store
                .get(&self.partition, &entry_key(from - 1))
                .await
                .map_err(store_error)?
                .ok_or_else(|| ServerError::new(Code::DataLoss, "audit sequence gap"))?;
            let entry: Json = decode(&row)?;
            if hash_entry(&entry)? != entry["entryHash"].as_str().unwrap_or("") {
                return Err(ServerError::new(Code::DataLoss, "audit hash mismatch"));
            }
            entry["entryHash"]
                .as_str()
                .ok_or_else(|| ServerError::new(Code::DataLoss, "invalid audit hash"))?
                .to_owned()
        };
        let mut bytes = 0usize;
        // Eight bounded values use at most 4 MiB before decoding. A full
        // 100-entry page costs at most thirteen DO reads, rather than 100.
        'pages: while next <= head.seq && entries.len() < size as usize {
            let count = (size as usize - entries.len()).min(8).min(
                usize::try_from(head.seq - next)
                    .unwrap_or(usize::MAX)
                    .saturating_add(1),
            );
            let keys: Vec<_> = (0..count).map(|i| entry_key(next + i as u64)).collect();
            let rows = self
                .store
                .get_many(&self.partition, &keys)
                .await
                .map_err(store_error)?;
            if rows.len() != count {
                return Err(ServerError::new(Code::DataLoss, "invalid audit read count"));
            }
            for row in rows {
                let row =
                    row.ok_or_else(|| ServerError::new(Code::DataLoss, "audit sequence gap"))?;
                if bytes.saturating_add(row.as_bytes().len()) > PAGE_BYTES && !entries.is_empty() {
                    break 'pages;
                }
                bytes = bytes.saturating_add(row.as_bytes().len());
                let mut entry: Json = decode(&row)?;
                let entry_hash = hash_entry(&entry)?;
                if entry["seq"].as_str().and_then(auth::decimal_u64) != Some(next)
                    || entry["prevHash"] != previous_hash
                    || entry["entryHash"] != entry_hash
                {
                    return Err(ServerError::new(
                        Code::DataLoss,
                        "audit chain continuity failure",
                    ));
                }
                previous_hash = entry_hash;
                for field in ["prevHash", "entryHash"] {
                    let raw = auth::hex::<32>(entry[field].as_str().unwrap_or(""))
                        .ok_or_else(|| ServerError::new(Code::DataLoss, "invalid audit hash"))?;
                    entry[field] = json!(STANDARD.encode(raw));
                }
                entries.push(entry);
                next = next
                    .checked_add(1)
                    .ok_or_else(|| ServerError::new(Code::DataLoss, "audit sequence overflow"))?;
            }
        }
        if next > head.seq && previous_hash != previous(head) {
            return Err(ServerError::new(
                Code::DataLoss,
                "audit chain head mismatch",
            ));
        }
        let head_hash = auth::hex::<32>(&previous(head))
            .ok_or_else(|| ServerError::new(Code::DataLoss, "corrupt audit head"))?;
        Response::stream(
            &json!({"entries":entries,"nextSeq":next.to_string(),"chainHead":STANDARD.encode(head_hash),
            "chainHeadSeq":head.seq.to_string(),"checkpointHash":STANDARD.encode([0u8;32]),"checkpointSeq":"0"}),
        )
    }
}