vta-service 0.38.0

Service for Verifiable Trust Agents operating in Verifiable Trust Communities
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
//! Backup-descriptor slice trust-task handlers.
//!
//! Five handlers for `spec/vta/backup/*`. Each is a thin wrapper:
//! parse payload → call `operations::backup::descriptors::*` →
//! serialize result. The op layer does the heavy lifting (auth
//! gates, caller-owns-bundle checks, state-machine transitions).
//!
//! See `docs/05-design-notes/backup-descriptor-pattern.md` for the
//! protocol design.

use super::helpers::TrustTaskOutcome;
use serde_json::{Value, json};
use trust_tasks_rs::{RejectReason, TrustTask};
use vta_sdk::protocols::backup_management::chunked::{
    ALGORITHM_CHUNKED, finalize_import_1_1, get_chunk, initiate_export_1_1, initiate_import_1_1,
    put_chunk,
};
use vta_sdk::protocols::backup_management::descriptors::{
    AbortBundleBody, CompleteExportBody, FinalizeImportBody, InitiateExportBody, InitiateImportBody,
};
use vti_common::error::AppError;

use crate::auth::AuthClaims;
use crate::operations::backup::{chunked, descriptors};
use crate::server::AppState;

use super::helpers::{
    TRANSPORT_TRUST_TASK, app_error_to_reject, parse_payload, reject_with, reject_with_code,
    success_response,
};

/// Slugs whose specifications declare `transportUnavailable`.
const INITIATE_EXPORT_SLUG: &str = "vta/backup/initiate-export";
const INITIATE_IMPORT_SLUG: &str = "vta/backup/initiate-import";

/// Refuse an `initiate-*` with the specification's own
/// `<slug>:transportUnavailable` code.
///
/// Both `initiate-export/1.0` and `initiate-import/1.0` require it of a
/// recipient with no address at which the bytes can move — the
/// DIDComm/TSP-only VTA, whose `public_url` is unset. This used to surface as
/// `internalError`, which tells the producer "not your fault, retry", when the
/// only fix is the agent's configuration: the opposite response to the one the
/// operator needs.
fn transport_unavailable(doc: &TrustTask<Value>, slug: &str) -> TrustTaskOutcome {
    tracing::warn!(
        slug,
        "backup descriptor refused: `public_url` is not configured, so the `stream` \
         algorithm has no blob URL to publish"
    );
    let code = trust_tasks_rs::TrustTaskCode::new_extended(slug, "transportUnavailable")
        .expect("backup extended code is grammar-valid");
    reject_with_code(doc, code, descriptors::TRANSPORT_UNAVAILABLE_MESSAGE, None)
}

/// The `initiate-*` preconditions the handler must answer itself, in the order
/// a caller should learn them: entitlement first (an unauthorized caller learns
/// nothing about this agent's deployment), then whether the transport exists.
async fn initiate_precheck(
    state: &AppState,
    auth: &AuthClaims,
    doc: &TrustTask<Value>,
    slug: &str,
) -> Result<(), TrustTaskOutcome> {
    auth.require_super_admin()
        .map_err(|e| app_error_to_reject(doc, e))?;
    if descriptors::blob_transport_base_url(&state.config)
        .await
        .is_none()
    {
        return Err(transport_unavailable(doc, slug));
    }
    Ok(())
}

/// Record a backup-lifecycle event against its bundle.
///
/// Both `initiate-*` verbs succeeded silently until the audit-coverage census
/// could see them — which it could not until #347 specced the family and gave
/// it conformance witnesses. They are the two most consequential successes in
/// this service to lose: an export mints a **fetchable copy of the entire
/// agent** at a known address, and an import opens a **writable endpoint into
/// it**. Neither alters stored state, which is why neither was caught by any
/// state-shaped check, and why the trail is the only place the event exists at
/// all.
///
/// Recorded here rather than in the op layer because the op layer is shared
/// with the REST blob routes, which have their own logging; and recorded
/// against the bundle id so the row joins to the later complete/finalize/abort.
///
/// The other three verbs are audited here too, and they were found by
/// *reasoning* rather than by the census — which is worth saying plainly,
/// because it marks the sweep's blind spot. `complete-export`, `finalize-import`
/// and `abort` all need a real bundle to succeed, and the census drives an
/// empty store, so it only ever sees their not-found refusals. It would have
/// reported this family green while every success path stayed silent. A test
/// that cannot reach a path cannot vouch for it.
///
/// Best-effort, as everywhere: a failed audit write must never fail the
/// operation.
async fn record_bundle_event(
    state: &AppState,
    auth: &AuthClaims,
    action: &str,
    bundle_id: &str,
    detail: String,
) {
    if let Err(e) = crate::audit::record_with_detail(
        &state.audit_sink,
        action,
        &auth.did,
        Some(bundle_id),
        "success",
        Some(TRANSPORT_TRUST_TASK),
        None,
        Some(&detail),
    )
    .await
    {
        tracing::warn!(error = %e, action, "audit record failed for {action}");
    }
}

/// `spec/vta/backup/initiate-export/1.0` — mint an export bundle.
/// Auth: super-admin.
pub(super) async fn handle_initiate_export(
    state: &AppState,
    auth: &AuthClaims,
    doc: TrustTask<Value>,
) -> TrustTaskOutcome {
    let req: InitiateExportBody = match parse_payload(&doc) {
        Ok(r) => r,
        Err(resp) => return resp,
    };
    if let Err(resp) = initiate_precheck(state, auth, &doc, INITIATE_EXPORT_SLUG).await {
        return resp;
    }
    let deps = crate::operations::descriptor_deps_from_app_state(state);
    // `include_audit` is read BEFORE the request moves into the op: it is the
    // one member that changes what leaves the agent — the trail records the
    // agent's dealings with counterparties who were never party to this export
    // — so an operator reviewing this row later needs it.
    let include_audit = req.include_audit;
    match descriptors::initiate_export(&deps, auth, req).await {
        Ok(body) => {
            record_bundle_event(
                state,
                auth,
                "backup.initiate-export",
                &body.descriptor.bundle_id,
                format!(
                    "includeAudit={include_audit} bytes={} expires={}",
                    body.descriptor.expected_size_bytes, body.descriptor.expires_at
                ),
            )
            .await;
            success_response(&doc, body)
        }
        Err(e) => app_error_to_reject(&doc, e),
    }
}

/// `spec/vta/backup/complete-export/1.0` — optional client ack.
/// Auth: super-admin (must match the initiator's DID).
pub(super) async fn handle_complete_export(
    state: &AppState,
    auth: &AuthClaims,
    doc: TrustTask<Value>,
) -> TrustTaskOutcome {
    let req: CompleteExportBody = match parse_payload(&doc) {
        Ok(r) => r,
        Err(resp) => return resp,
    };
    let deps = crate::operations::descriptor_deps_from_app_state(state);
    match descriptors::complete_export(&deps, auth, req).await {
        Ok(body) => {
            // `downloaded` is the whole evidentiary content of this row. It is
            // the difference between "a copy of this agent left here" and "a
            // bundle expired unfetched", and after the bytes are released it is
            // the only place that difference survives — which is exactly where
            // an investigation into a leaked copy has to start.
            record_bundle_event(
                state,
                auth,
                "backup.complete-export",
                &body.bundle_id,
                format!("downloaded={}", body.downloaded),
            )
            .await;
            success_response(&doc, body)
        }
        Err(e) => app_error_to_reject(&doc, e),
    }
}

/// `spec/vta/backup/initiate-import/1.0` — mint an upload slot.
/// Auth: super-admin.
pub(super) async fn handle_initiate_import(
    state: &AppState,
    auth: &AuthClaims,
    doc: TrustTask<Value>,
) -> TrustTaskOutcome {
    let req: InitiateImportBody = match parse_payload(&doc) {
        Ok(r) => r,
        Err(resp) => return resp,
    };
    if let Err(resp) = initiate_precheck(state, auth, &doc, INITIATE_IMPORT_SLUG).await {
        return resp;
    }
    let deps = crate::operations::descriptor_deps_from_app_state(state);
    match descriptors::initiate_import(&deps, auth, req).await {
        Ok(body) => {
            // The digest identifies the exact bytes the operator committed to
            // upload, which is what lets a later review say *which* bundle was
            // brought in rather than only that one was.
            record_bundle_event(
                state,
                auth,
                "backup.initiate-import",
                &body.descriptor.bundle_id,
                format!(
                    "sha256={} bytes={} expires={}",
                    body.descriptor.expected_sha256,
                    body.descriptor.expected_size_bytes,
                    body.descriptor.expires_at
                ),
            )
            .await;
            success_response(&doc, body)
        }
        Err(e) => app_error_to_reject(&doc, e),
    }
}

/// `spec/vta/backup/finalize-import/1.0` — apply uploaded bytes
/// (preview or commit). Auth: super-admin (must match the
/// initiator's DID).
pub(super) async fn handle_finalize_import(
    state: &AppState,
    auth: &AuthClaims,
    doc: TrustTask<Value>,
) -> TrustTaskOutcome {
    let req: FinalizeImportBody = match parse_payload(&doc) {
        Ok(r) => r,
        Err(resp) => return resp,
    };
    // A chunked upload is assembled and verified here, before the password is
    // used; a no-op for a stream bundle. 1.0 declares none of the chunked codes,
    // so its refusals ride out as general failures — 1.1 renders them properly.
    if let Err(e) = chunked::finalize_precheck(&state.backup_bundles_ks, auth, &req.bundle_id).await
    {
        return match e {
            chunked::ChunkedError::App(app) => app_error_to_reject(&doc, app),
            chunked::ChunkedError::NotFound => app_error_to_reject(
                &doc,
                AppError::NotFound(format!("bundle not found: {}", req.bundle_id)),
            ),
            other => app_error_to_reject(&doc, AppError::Conflict(other.to_string())),
        };
    }
    let deps = crate::operations::descriptor_deps_from_app_state(state);
    match descriptors::finalize_import(&deps, auth, req).await {
        Ok(body) => {
            // The most consequential row this service writes. On commit the
            // agent's keys, ACLs, contexts and trail are REPLACED with the
            // bundle's — including, note, the audit trail itself. Anything this
            // agent recorded before the commit is gone with it, so a row
            // written into imported state would document its own erasure.
            //
            // This one is written after the op returns, to the sink, which is
            // outside the state the import replaced. That is the point, and it
            // is what the specification means by the response being the record
            // the *operator* holds.
            //
            // `status` distinguishes a rehearsal from the real thing, so a
            // preview does not read as a replacement that happened.
            record_bundle_event(
                state,
                auth,
                "backup.finalize-import",
                &body.bundle_id,
                format!(
                    "status={} source={} keys={} acls={} contexts={}",
                    body.status,
                    body.source_did.as_deref().unwrap_or("unknown"),
                    body.key_count,
                    body.acl_count,
                    body.context_count
                ),
            )
            .await;
            success_response(&doc, body)
        }
        Err(e) => app_error_to_reject(&doc, e),
    }
}

/// `spec/vta/backup/abort/1.0` — cancel an in-flight bundle. Auth:
/// super-admin (must match the initiator's DID).
pub(super) async fn handle_abort(
    state: &AppState,
    auth: &AuthClaims,
    doc: TrustTask<Value>,
) -> TrustTaskOutcome {
    let req: AbortBundleBody = match parse_payload(&doc) {
        Ok(r) => r,
        Err(resp) => return resp,
    };
    let deps = crate::operations::descriptor_deps_from_app_state(state);
    match descriptors::abort_bundle(&deps, auth, req).await {
        Ok(body) => {
            // A bundle that simply stops existing is indistinguishable from one
            // that was quietly fetched. `aborted` is what separates "no copy
            // was ever made" from "a copy left and nobody logged it", and it is
            // recorded on the idempotent no-op too so a repeat is visible as a
            // repeat rather than as a second cancellation.
            record_bundle_event(
                state,
                auth,
                "backup.abort",
                &body.bundle_id,
                format!("aborted={}", body.aborted),
            )
            .await;
            success_response(&doc, body)
        }
        Err(e) => app_error_to_reject(&doc, e),
    }
}

// ─── The `chunkedTrustTask` algorithm (1.1 initiators, get/put-chunk) ────────
//
// Specified in `vta/backup/initiate-export/1.1` § Chunked transfer. A `stream`
// request to a 1.1 initiator is served by the 1.0 path unchanged — the two
// shapes are wire-identical — and only `chunkedTrustTask` takes the new ops.
//
// These handlers await the 1.0 handler (or a full state export) inline. They
// used to `Box::pin` those awaits, because `dispatch_typed` matched every
// handler inline and this handler — awaiting the whole 1.0 handler plus a full
// export — became its largest arm, overflowing the debug worker stack on the
// first chunked export. That boxing moved to the dispatch seam itself
// (`dispatch_typed` now `Box::pin`s every arm), so each handler's future is
// heap-allocated there and no longer sums into the match frame; the
// per-handler boxes here were redundant once the seam was fixed.

/// Task slug (`vta/backup/<op>`) of the incoming document, so an extended code
/// is namespaced to whichever task raised it (SPEC §8.5).
fn backup_slug(doc: &TrustTask<Value>) -> String {
    doc.type_uri
        .to_string()
        .strip_prefix("https://trusttasks.org/spec/")
        .and_then(|rest| rest.rsplit_once('/'))
        .map(|(slug, _ver)| slug.to_string())
        .unwrap_or_else(|| "vta/backup".to_string())
}

/// Render a [`chunked::ChunkedError`] as the code its specification declares.
fn chunked_reject(doc: &TrustTask<Value>, err: chunked::ChunkedError) -> TrustTaskOutcome {
    use chunked::ChunkedError as E;
    let slug = backup_slug(doc);
    let message = err.to_string();
    let (local, details) = match err {
        E::App(e) => return app_error_to_reject(doc, e),
        // `unavailable` with a `retryAfter` is what `VtaClient::idempotent`
        // waits on, so a client over the budget slows down rather than failing.
        E::RateLimited { retry_after_secs } => {
            return reject_with(
                doc,
                RejectReason::Unavailable {
                    retry_after: Some(
                        chrono::Utc::now() + chrono::Duration::seconds(retry_after_secs as i64),
                    ),
                },
            );
        }
        E::NotFound => ("notFound", None),
        E::TerminalState(_) => ("terminalState", None),
        E::ChunkOutOfRange { .. } => ("chunkOutOfRange", None),
        E::DigestMismatch {
            expected_digest_multibase,
        } => (
            "digestMismatch",
            // Declared by put-chunk only; get-chunk never raises it.
            Some(json!({ "expectedDigestMultibase": expected_digest_multibase })),
        ),
        E::ChunkSizeMismatch { .. } => ("chunkSizeMismatch", None),
        E::IncompleteUpload {
            missing_count,
            missing_indices,
        } => (
            "incompleteUpload",
            Some(json!({ "missingCount": missing_count, "missingIndices": missing_indices })),
        ),
        E::BundleDigestMismatch => ("bundleDigestMismatch", None),
        E::BundleTooLarge { .. } => ("bundleTooLarge", None),
        E::InvalidManifest(_) => ("invalidManifest", None),
    };
    let code = trust_tasks_rs::TrustTaskCode::new_extended(&slug, local)
        .expect("backup extended code is grammar-valid");
    reject_with_code(doc, code, message, details)
}

/// Build a generated response type from its JSON form. The generated types are
/// `#[non_exhaustive]`, so a struct literal is unavailable; deserializing also
/// runs the members' own pattern and range checks, so a response this agent
/// could not legally send is caught here rather than by the client.
fn typed<R: serde::de::DeserializeOwned>(value: Value) -> Result<R, AppError> {
    serde_json::from_value(value)
        .map_err(|e| AppError::Internal(format!("backup response does not fit its schema: {e}")))
}

fn manifest_json(bundle: &chunked::ChunkedBundle) -> Value {
    json!({
        "bundleId": bundle.bundle_id.to_string(),
        "algorithm": ALGORITHM_CHUNKED,
        "chunks": {
            "chunkSize": bundle.chunk_size,
            "chunkCount": bundle.chunk_count,
            "chunkDigests": bundle.digests,
        },
        "expectedSha256": bundle.expected_sha256,
        "expectedSizeBytes": bundle.expected_size_bytes,
        "expiresAt": bundle.expires_at,
    })
}

fn is_chunked(algorithm: Option<&str>) -> bool {
    algorithm == Some(ALGORITHM_CHUNKED)
}

/// `spec/vta/backup/initiate-export/1.1` — `stream` exactly as 1.0, or a
/// `chunkedTrustTask` bundle whose chunks are pulled with `get-chunk`. The
/// chunked path needs no `public_url`, which is the point of it.
pub(super) async fn handle_initiate_export_1_1(
    state: &AppState,
    auth: &AuthClaims,
    doc: TrustTask<Value>,
) -> TrustTaskOutcome {
    let req: initiate_export_1_1::Payload = match parse_payload(&doc) {
        Ok(r) => r,
        Err(resp) => return resp,
    };
    let algorithm = req.algorithm.as_ref().map(|a| a.as_str());
    if !is_chunked(algorithm) {
        // The recipient returns what was asked for or refuses; a `stream`
        // request never receives a chunked descriptor.
        return handle_initiate_export(state, auth, doc).await;
    }
    if let Err(e) = auth.require_super_admin() {
        return app_error_to_reject(&doc, e);
    }
    let include_audit = req.include_audit.unwrap_or(false);
    let deps = crate::operations::descriptor_deps_from_app_state(state);
    let bundle = match chunked::initiate_export(
        &deps,
        auth,
        req.password.as_str(),
        include_audit,
        req.max_chunk_size.map(|s| s.0.max(0) as u64),
    )
    .await
    {
        Ok(b) => b,
        Err(e) => return chunked_reject(&doc, e),
    };
    record_bundle_event(
        state,
        auth,
        "backup.initiate-export",
        &bundle.bundle_id.to_string(),
        format!(
            "algorithm={ALGORITHM_CHUNKED} includeAudit={include_audit} bytes={} chunks={} expires={}",
            bundle.expected_size_bytes, bundle.chunk_count, bundle.expires_at
        ),
    )
    .await;
    match typed::<initiate_export_1_1::Response>(json!({
        "descriptor": manifest_json(&bundle),
        "completionHint": format!(
            "Send get-chunk for indices 0 to {}, verify each, then send complete-export.",
            bundle.chunk_count - 1
        ),
    })) {
        Ok(r) => success_response(&doc, r),
        Err(e) => app_error_to_reject(&doc, e),
    }
}

/// `spec/vta/backup/initiate-import/1.1` — `stream` exactly as 1.0, or a
/// `chunkedTrustTask` slot for the manifest the request pre-commits.
pub(super) async fn handle_initiate_import_1_1(
    state: &AppState,
    auth: &AuthClaims,
    doc: TrustTask<Value>,
) -> TrustTaskOutcome {
    let req: initiate_import_1_1::Payload = match parse_payload(&doc) {
        Ok(r) => r,
        Err(resp) => return resp,
    };
    let algorithm = req.algorithm.as_ref().map(|a| a.as_str());
    if let Err(e) = auth.require_super_admin() {
        return app_error_to_reject(&doc, e);
    }
    if !is_chunked(algorithm) {
        // `chunks` belongs to `chunkedTrustTask` alone (the spec's
        // `invalidManifest`), so a stream request carrying one is refused rather
        // than having the manifest silently ignored.
        if req.chunks.is_some() {
            return chunked_reject(
                &doc,
                chunked::ChunkedError::InvalidManifest(
                    "`chunks` is only meaningful with algorithm chunkedTrustTask".into(),
                ),
            );
        }
        return handle_initiate_import(state, auth, doc).await;
    }
    let Some(manifest) = req.chunks else {
        return chunked_reject(
            &doc,
            chunked::ChunkedError::InvalidManifest(
                "algorithm chunkedTrustTask requires a `chunks` manifest".into(),
            ),
        );
    };
    let slot = match chunked::initiate_import(
        &state.backup_bundles_ks,
        auth,
        req.expected_sha256.as_str(),
        req.expected_size_bytes.0.get(),
        manifest.chunk_size.0.max(0) as u64,
        manifest.chunk_count.0.get(),
        manifest
            .chunk_digests
            .iter()
            .map(|d| d.as_str().to_string())
            .collect(),
    )
    .await
    {
        Ok(s) => s,
        Err(e) => return chunked_reject(&doc, e),
    };
    record_bundle_event(
        state,
        auth,
        "backup.initiate-import",
        &slot.bundle_id.to_string(),
        format!(
            "algorithm={ALGORITHM_CHUNKED} sha256={} bytes={} chunks={} expires={}",
            slot.expected_sha256, slot.expected_size_bytes, slot.chunk_count, slot.expires_at
        ),
    )
    .await;
    match typed::<initiate_import_1_1::Response>(json!({
        "descriptor": manifest_json(&slot),
        "completionHint": format!(
            "Send put-chunk for indices 0 to {}, then send finalize-import.",
            slot.chunk_count - 1
        ),
    })) {
        Ok(r) => success_response(&doc, r),
        Err(e) => app_error_to_reject(&doc, e),
    }
}

/// `spec/vta/backup/finalize-import/1.1` — as 1.0, with a chunked upload's
/// completeness and assembled digest checked first, answered with the codes 1.1
/// declares, before the password is used.
pub(super) async fn handle_finalize_import_1_1(
    state: &AppState,
    auth: &AuthClaims,
    doc: TrustTask<Value>,
) -> TrustTaskOutcome {
    let req: finalize_import_1_1::Payload = match parse_payload(&doc) {
        Ok(r) => r,
        Err(resp) => return resp,
    };
    if let Err(e) =
        chunked::finalize_precheck(&state.backup_bundles_ks, auth, req.bundle_id.as_str()).await
    {
        return chunked_reject(&doc, e);
    }
    handle_finalize_import(state, auth, doc).await
}

/// `spec/vta/backup/get-chunk/1.0` — one chunk of a chunked export, by index.
///
/// Not audited per chunk: the specification's retention section keeps the
/// durable facts (an export was made; whether it was retrieved) on
/// `initiate-export` and `complete-export`, and a row per chunk would add
/// nothing to them but a timeline of the operator's connection. Refusals are
/// still recorded by the dispatch spine.
pub(super) async fn handle_get_chunk(
    state: &AppState,
    auth: &AuthClaims,
    doc: TrustTask<Value>,
) -> TrustTaskOutcome {
    let req: get_chunk::Payload = match parse_payload(&doc) {
        Ok(r) => r,
        Err(resp) => return resp,
    };
    let served = match chunked::get_chunk(
        &state.backup_bundles_ks,
        chunked::ChunkRateLimiter::global(),
        auth,
        req.bundle_id.as_str(),
        req.index.0.max(0) as u64,
    )
    .await
    {
        Ok(c) => c,
        Err(e) => return chunked_reject(&doc, e),
    };
    use base64::Engine;
    match typed::<get_chunk::Response>(json!({
        "bundleId": served.bundle_id.to_string(),
        "index": served.index,
        "digestMultibase": served.digest_multibase,
        "data": base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(&served.data),
        "expiresAt": served.expires_at,
    })) {
        Ok(r) => success_response(&doc, r),
        Err(e) => app_error_to_reject(&doc, e),
    }
}

/// `spec/vta/backup/put-chunk/1.0` — write one chunk of a chunked import.
/// Not audited per chunk, for the reason [`handle_get_chunk`] gives.
pub(super) async fn handle_put_chunk(
    state: &AppState,
    auth: &AuthClaims,
    doc: TrustTask<Value>,
) -> TrustTaskOutcome {
    let req: put_chunk::Payload = match parse_payload(&doc) {
        Ok(r) => r,
        Err(resp) => return resp,
    };
    use base64::Engine;
    let data = match base64::engine::general_purpose::URL_SAFE_NO_PAD.decode(req.data.as_str()) {
        Ok(d) => d,
        Err(e) => {
            return reject_with(
                &doc,
                RejectReason::MalformedRequest {
                    reason: format!("`data` is not unpadded base64url: {e}"),
                },
            );
        }
    };
    let index = req.index.0.max(0) as u64;
    let outcome = match chunked::put_chunk(
        &state.backup_bundles_ks,
        &state.backup_blob_dir,
        chunked::ChunkRateLimiter::global(),
        auth,
        chunked::ChunkWrite {
            bundle_id: req.bundle_id.as_str(),
            index,
            digest_multibase: req.digest_multibase.as_str(),
            data: &data,
        },
    )
    .await
    {
        Ok(o) => o,
        Err(e) => return chunked_reject(&doc, e),
    };
    match typed::<put_chunk::Response>(json!({
        "bundleId": req.bundle_id.as_str(),
        "index": index,
        "stored": outcome.stored,
        "remainingCount": outcome.remaining_count,
        "expiresAt": outcome.expires_at,
    })) {
        Ok(r) => success_response(&doc, r),
        Err(e) => app_error_to_reject(&doc, e),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use trust_tasks_rs::TypeUri;

    fn doc(uri: &str) -> TrustTask<Value> {
        let uri: TypeUri = uri.parse().expect("backup uri");
        TrustTask::new("urn:uuid:test", uri, serde_json::json!({}))
    }

    /// A VTA with no public HTTPS address answers `initiate-*` with the code
    /// its specification declares, not `internalError`.
    #[test]
    fn transport_unavailable_uses_the_specified_extended_code() {
        for (uri, slug) in [
            (
                vta_sdk::trust_tasks::TASK_BACKUP_INITIATE_EXPORT_1_0,
                INITIATE_EXPORT_SLUG,
            ),
            (
                vta_sdk::trust_tasks::TASK_BACKUP_INITIATE_IMPORT_1_0,
                INITIATE_IMPORT_SLUG,
            ),
        ] {
            assert!(
                uri.contains(slug),
                "{uri}: the slug constant must match the dispatched URI"
            );
            let outcome = transport_unavailable(&doc(uri), slug);
            let parsed: Value = serde_json::from_slice(&outcome.body).expect("error doc");
            assert_eq!(
                parsed["payload"]["code"],
                format!("{slug}:transportUnavailable"),
                "{parsed}"
            );
            assert_ne!(
                outcome.status,
                axum::http::StatusCode::INTERNAL_SERVER_ERROR,
                "transportUnavailable is not an internal error"
            );
            assert!(
                !parsed["payload"]["message"]
                    .as_str()
                    .unwrap_or_default()
                    .contains("public_url"),
                "the wire message must not name configuration: {parsed}"
            );
        }
    }
}