denoize 0.86.0

Pure-Rust audio denoiser with classical DSP and optional RNNoise
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
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
//! Authenticated, length-delimited bundles for closed-network model installs.

#[cfg(test)]
use super::install_catalog_model_from_verified_bundle_for_test;
use super::{
    catalog, install_catalog_model_from_bundle, open_existing_regular_file, path_for_catalog_model,
    remove_catalog_model, validate_model_storage_path, verify_catalog_model, CatalogBundleFile,
    CatalogModel, ModelCatalog,
};
use crate::{AtomicOutput, CommitMode};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashSet;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use url::Url;

const BUNDLE_MAGIC: &[u8] = b"denoize-model-bundle-v1\n";
const BUNDLE_SCHEMA: &str = "denoize-model-bundle-v1";
const BUNDLE_VERSION: u32 = 1;
const MAX_MANIFEST_BYTES: u64 = 1024 * 1024;
const MAX_CATALOG_BYTES: u64 = 1024 * 1024;
const MAX_SIGNATURE_BYTES: u64 = 16 * 1024;
const MAX_TRUST_ROOT_BYTES: u64 = 64 * 1024;
const MAX_MODELS: usize = 256;
const MAX_MODEL_BYTES: u64 = 64 * 1024 * 1024 * 1024;
const MAX_METADATA_BYTES: u64 = 1024 * 1024;

/// Verified identity and contents of one closed-network model bundle.
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct OfflineBundleInfo {
    pub format_version: u32,
    pub bundle_sha256: String,
    pub size_bytes: u64,
    pub catalog_sequence: u64,
    pub catalog_sha256: String,
    pub catalog_signing_key_id: String,
    pub catalog_issued_at_unix_seconds: Option<u64>,
    pub catalog_expires_at_unix_seconds: Option<u64>,
    pub trust_root_version: u64,
    pub trust_root_sha256: String,
    pub models: Vec<OfflineBundleModelInfo>,
}

/// Verified model and notice files carried by an offline bundle.
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct OfflineBundleModelInfo {
    pub name: String,
    pub backend: String,
    pub artifact_filename: String,
    pub artifact_sha256: String,
    pub artifact_size_bytes: u64,
    pub license_filename: String,
    pub license_sha256: String,
    pub license_size_bytes: u64,
    pub provenance_filename: String,
    pub provenance_sha256: String,
    pub provenance_size_bytes: u64,
}

/// Result of importing every model from an authenticated bundle.
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct OfflineBundleImportReport {
    pub bundle: OfflineBundleInfo,
    pub installed: Vec<PathBuf>,
    pub already_present: Vec<PathBuf>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct BundleManifest {
    schema: String,
    version: u32,
    catalog: BundleFileRecord,
    catalog_signature: BundleFileRecord,
    trust_root: BundleFileRecord,
    models: Vec<BundleModelRecord>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct BundleFileRecord {
    size_bytes: u64,
    sha256: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct BundleNamedFileRecord {
    filename: String,
    size_bytes: u64,
    sha256: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct BundleModelRecord {
    name: String,
    artifact: BundleNamedFileRecord,
    license: BundleNamedFileRecord,
    provenance: BundleNamedFileRecord,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct SourceProvenanceDocument {
    schema: String,
    model_name: String,
    upstream_repository: String,
    upstream_revision: String,
    artifact: SourceArtifactDocument,
    license: SourceLicenseDocument,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct SourceArtifactDocument {
    url: String,
    sha256: String,
    size_bytes: u64,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct SourceLicenseDocument {
    spdx: String,
    url: String,
    sha256: String,
    size_bytes: u64,
}

struct OpenBuildModel {
    record: BundleModelRecord,
    artifact: File,
    license: File,
    provenance: File,
    artifact_path: PathBuf,
    license_path: PathBuf,
    provenance_path: PathBuf,
}

struct PreparedModel {
    name: String,
    artifact: tempfile::NamedTempFile,
    existed: bool,
}

struct PreparedBundle {
    info: OfflineBundleInfo,
    catalog: ModelCatalog,
    catalog_bytes: Vec<u8>,
    signature: Vec<u8>,
    models: Vec<PreparedModel>,
}

/// Build a deterministic bundle from a signed catalog and a component tree.
///
/// `components_dir` must contain, for every catalog package, the exact paths
/// `<MODEL>/<artifact>`, `<MODEL>/<license>`, and `<MODEL>/<provenance>`.
pub fn build_offline_bundle(
    output: impl AsRef<Path>,
    catalog_path: impl AsRef<Path>,
    signature_path: impl AsRef<Path>,
    trust_root_path: impl AsRef<Path>,
    components_dir: impl AsRef<Path>,
) -> Result<OfflineBundleInfo, String> {
    build_offline_bundle_with_verifier(
        output.as_ref(),
        catalog_path.as_ref(),
        signature_path.as_ref(),
        trust_root_path.as_ref(),
        components_dir.as_ref(),
        catalog::verify_bundle_catalog,
    )
}

/// Authenticate every byte in a bundle without changing catalog or cache
/// state. The input must be a seekable regular file.
pub fn inspect_offline_bundle(path: impl AsRef<Path>) -> Result<OfflineBundleInfo, String> {
    Ok(
        prepare_offline_bundle_with_verifier(path.as_ref(), false, catalog::verify_bundle_catalog)?
            .info,
    )
}

/// Authenticate a bundle, activate its signed catalog, and atomically install
/// each missing model. All bundle bytes are verified before persistent model or
/// catalog state changes. A storage failure rolls back models created by this
/// invocation; the monotonic catalog rollback floor may remain advanced and a
/// retry is safe.
pub fn import_offline_bundle(path: impl AsRef<Path>) -> Result<OfflineBundleImportReport, String> {
    let prepared =
        prepare_offline_bundle_with_verifier(path.as_ref(), true, catalog::verify_bundle_catalog)?;
    commit_prepared_bundle(prepared)
}

/// Import only if the fully authenticated bundle still has `expected_sha256`.
/// This binds a prior user-visible inspection to the bytes committed by a later
/// confirmation step without trusting the pathname to remain unchanged.
pub fn import_offline_bundle_if_sha256(
    path: impl AsRef<Path>,
    expected_sha256: &str,
) -> Result<OfflineBundleImportReport, String> {
    if !valid_sha256(expected_sha256) {
        return Err(
            "expected offline bundle SHA-256 must be 64 lowercase hexadecimal characters".into(),
        );
    }
    let prepared =
        prepare_offline_bundle_with_verifier(path.as_ref(), true, catalog::verify_bundle_catalog)?;
    if prepared.info.bundle_sha256 != expected_sha256 {
        return Err(format!(
            "offline bundle changed after inspection: expected {expected_sha256}, got {}",
            prepared.info.bundle_sha256
        ));
    }
    commit_prepared_bundle(prepared)
}

fn commit_prepared_bundle(prepared: PreparedBundle) -> Result<OfflineBundleImportReport, String> {
    let active = catalog::activate_bundle_catalog(&prepared.catalog_bytes, &prepared.signature)?;
    if active.sequence() != prepared.catalog.sequence()
        || active.sha256() != prepared.catalog.sha256()
        || active.signing_key_id() != prepared.catalog.signing_key_id()
    {
        return Err("active model catalog changed while importing the offline bundle".into());
    }

    let mut installed = Vec::new();
    let mut already_present = Vec::new();
    let mut newly_installed = Vec::<CatalogModel>::new();
    let result = (|| {
        for staged in &prepared.models {
            let model = active.find(&staged.name).ok_or_else(|| {
                format!(
                    "offline bundle model disappeared from active catalog: {}",
                    staged.name
                )
            })?;
            if staged.existed {
                already_present.push(verify_catalog_model(model)?);
                continue;
            }
            let destination = install_catalog_model_from_bundle(
                model,
                staged.artifact.path(),
                &prepared.info.bundle_sha256,
            )?;
            newly_installed.push(model.clone());
            installed.push(destination);
        }
        Ok(())
    })();
    if let Err(error) = result {
        for model in newly_installed.iter().rev() {
            let _ = remove_catalog_model(model);
        }
        return Err(error);
    }
    Ok(OfflineBundleImportReport {
        bundle: prepared.info,
        installed,
        already_present,
    })
}

fn build_offline_bundle_with_verifier<F>(
    output: &Path,
    catalog_path: &Path,
    signature_path: &Path,
    trust_root_path: &Path,
    components_dir: &Path,
    mut verify_catalog: F,
) -> Result<OfflineBundleInfo, String>
where
    F: FnMut(&[u8], &[u8]) -> Result<catalog::VerifiedBundleCatalog, String>,
{
    let catalog_bytes = read_bounded_regular(catalog_path, MAX_CATALOG_BYTES, "model catalog")?;
    let signature = read_bounded_regular(
        signature_path,
        MAX_SIGNATURE_BYTES,
        "model catalog signature",
    )?;
    let trust_root =
        read_bounded_regular(trust_root_path, MAX_TRUST_ROOT_BYTES, "model trust root")?;
    let verified = verify_catalog(&catalog_bytes, &signature)?;
    let trust_root_record = file_record(&trust_root);
    if trust_root_record.sha256 != verified.trust_root_sha256 {
        return Err(format!(
            "bundle trust root does not match active trust root {}",
            verified.trust_root_sha256
        ));
    }

    let mut open_models = Vec::with_capacity(verified.catalog.models().len());
    for model in verified.catalog.models() {
        let metadata = require_bundle_metadata(model)?;
        let directory = components_dir.join(model.name());
        let artifact_path = directory.join(model.filename());
        let license_path = directory.join(metadata.license().filename());
        let provenance_path = directory.join(metadata.provenance().filename());
        let mut artifact = open_required_regular(&artifact_path, "bundle model artifact")?;
        let mut license = open_required_regular(&license_path, "bundle model license")?;
        let mut provenance = open_required_regular(&provenance_path, "bundle model provenance")?;
        let artifact_record = named_record_for_open_file(
            &mut artifact,
            &artifact_path,
            model.filename(),
            model.size_bytes(),
            model.sha256(),
        )?;
        let license_record =
            named_record_for_catalog_file(&mut license, &license_path, metadata.license())?;
        let provenance_record = named_record_for_catalog_file(
            &mut provenance,
            &provenance_path,
            metadata.provenance(),
        )?;
        let provenance_bytes = read_open_component_bytes(
            &mut provenance,
            &provenance_path,
            metadata.provenance().size_bytes(),
        )?;
        validate_source_provenance(model, metadata, &provenance_bytes)?;
        open_models.push(OpenBuildModel {
            record: BundleModelRecord {
                name: model.name().to_string(),
                artifact: artifact_record,
                license: license_record,
                provenance: provenance_record,
            },
            artifact,
            license,
            provenance,
            artifact_path,
            license_path,
            provenance_path,
        });
    }

    let manifest = BundleManifest {
        schema: BUNDLE_SCHEMA.to_string(),
        version: BUNDLE_VERSION,
        catalog: file_record(&catalog_bytes),
        catalog_signature: file_record(&signature),
        trust_root: trust_root_record,
        models: open_models
            .iter()
            .map(|model| model.record.clone())
            .collect(),
    };
    validate_manifest_shape(&manifest)?;
    let mut manifest_bytes = serde_json::to_vec_pretty(&manifest)
        .map_err(|error| format!("failed to encode offline bundle manifest: {error}"))?;
    manifest_bytes.push(b'\n');
    if manifest_bytes.len() as u64 > MAX_MANIFEST_BYTES {
        return Err("offline bundle manifest exceeds the 1 MiB limit".into());
    }

    let mut staged = AtomicOutput::new(output)?;
    staged
        .file_mut()
        .write_all(BUNDLE_MAGIC)
        .and_then(|_| {
            staged
                .file_mut()
                .write_all(&(manifest_bytes.len() as u64).to_be_bytes())
        })
        .and_then(|_| staged.file_mut().write_all(&manifest_bytes))
        .and_then(|_| staged.file_mut().write_all(&catalog_bytes))
        .and_then(|_| staged.file_mut().write_all(&signature))
        .and_then(|_| staged.file_mut().write_all(&trust_root))
        .map_err(|error| {
            format!(
                "failed to write offline bundle {}: {error}",
                output.display()
            )
        })?;
    for model in &mut open_models {
        copy_open_file(
            &mut model.artifact,
            &model.artifact_path,
            staged.file_mut(),
            model.record.artifact.size_bytes,
            &model.record.artifact.sha256,
        )?;
        copy_open_file(
            &mut model.license,
            &model.license_path,
            staged.file_mut(),
            model.record.license.size_bytes,
            &model.record.license.sha256,
        )?;
        copy_open_file(
            &mut model.provenance,
            &model.provenance_path,
            staged.file_mut(),
            model.record.provenance.size_bytes,
            &model.record.provenance.sha256,
        )?;
    }
    staged.file_mut().flush().map_err(|error| {
        format!(
            "failed to flush offline bundle {}: {error}",
            output.display()
        )
    })?;
    let staged_size = staged
        .file_mut()
        .metadata()
        .map_err(|error| format!("failed to inspect staged offline bundle: {error}"))?
        .len();
    let info = prepare_open_offline_bundle_with_verifier(
        staged.file_mut(),
        output,
        staged_size,
        false,
        &mut verify_catalog,
    )?
    .info;
    staged.commit(CommitMode::Replace)?;
    Ok(info)
}

#[cfg(test)]
pub(super) fn build_offline_bundle_for_test(
    output: &Path,
    catalog_path: &Path,
    signature_path: &Path,
    trust_root_path: &Path,
    components_dir: &Path,
) -> Result<OfflineBundleInfo, String> {
    let trust_root_sha256 = sha256_bytes(
        &std::fs::read(trust_root_path)
            .map_err(|error| format!("failed to read {}: {error}", trust_root_path.display()))?,
    );
    build_offline_bundle_with_verifier(
        output,
        catalog_path,
        signature_path,
        trust_root_path,
        components_dir,
        move |catalog_bytes, _signature| {
            Ok(catalog::VerifiedBundleCatalog {
                catalog: catalog::parse_catalog(
                    catalog_bytes,
                    super::CatalogOrigin::Signed {
                        source: "local-import".into(),
                    },
                )?,
                trust_root_version: 1,
                trust_root_sha256: trust_root_sha256.clone(),
            })
        },
    )
}

#[cfg(test)]
pub(super) fn inspect_offline_bundle_for_test(
    path: &Path,
    trust_root_sha256: &str,
) -> Result<OfflineBundleInfo, String> {
    prepare_offline_bundle_with_verifier(path, false, |catalog_bytes, _signature| {
        Ok(catalog::VerifiedBundleCatalog {
            catalog: catalog::parse_catalog(
                catalog_bytes,
                super::CatalogOrigin::Signed {
                    source: "local-import".into(),
                },
            )?,
            trust_root_version: 1,
            trust_root_sha256: trust_root_sha256.to_string(),
        })
    })
    .map(|prepared| prepared.info)
}

#[cfg(test)]
pub(super) fn import_offline_bundle_for_test(
    path: &Path,
    trust_root_sha256: &str,
) -> Result<OfflineBundleImportReport, String> {
    let prepared =
        prepare_offline_bundle_with_verifier(path, true, |catalog_bytes, _signature| {
            Ok(catalog::VerifiedBundleCatalog {
                catalog: catalog::parse_catalog(
                    catalog_bytes,
                    super::CatalogOrigin::Signed {
                        source: "local-import".into(),
                    },
                )?,
                trust_root_version: 1,
                trust_root_sha256: trust_root_sha256.to_string(),
            })
        })?;
    commit_prepared_bundle_for_test(prepared)
}

#[cfg(test)]
pub(super) fn import_offline_bundle_for_test_if_sha256(
    path: &Path,
    trust_root_sha256: &str,
    expected_sha256: &str,
) -> Result<OfflineBundleImportReport, String> {
    let prepared =
        prepare_offline_bundle_with_verifier(path, true, |catalog_bytes, _signature| {
            Ok(catalog::VerifiedBundleCatalog {
                catalog: catalog::parse_catalog(
                    catalog_bytes,
                    super::CatalogOrigin::Signed {
                        source: "local-import".into(),
                    },
                )?,
                trust_root_version: 1,
                trust_root_sha256: trust_root_sha256.to_string(),
            })
        })?;
    if prepared.info.bundle_sha256 != expected_sha256 {
        return Err(format!(
            "offline bundle changed after inspection: expected {expected_sha256}, got {}",
            prepared.info.bundle_sha256
        ));
    }
    commit_prepared_bundle_for_test(prepared)
}

#[cfg(test)]
fn commit_prepared_bundle_for_test(
    prepared: PreparedBundle,
) -> Result<OfflineBundleImportReport, String> {
    let mut installed = Vec::new();
    let mut already_present = Vec::new();
    let mut newly_installed = Vec::<CatalogModel>::new();
    let result = (|| {
        for staged in &prepared.models {
            let model = prepared.catalog.find(&staged.name).unwrap();
            if staged.existed {
                already_present.push(verify_catalog_model(model)?);
            } else {
                installed.push(install_catalog_model_from_verified_bundle_for_test(
                    model,
                    staged.artifact.path(),
                    &prepared.info.bundle_sha256,
                )?);
                newly_installed.push(model.clone());
            }
        }
        Ok(())
    })();
    if let Err(error) = result {
        for model in newly_installed.iter().rev() {
            let _ = remove_catalog_model(model);
        }
        return Err(error);
    }
    Ok(OfflineBundleImportReport {
        bundle: prepared.info,
        installed,
        already_present,
    })
}

fn prepare_offline_bundle_with_verifier<F>(
    path: &Path,
    stage_models: bool,
    mut verify_catalog: F,
) -> Result<PreparedBundle, String>
where
    F: FnMut(&[u8], &[u8]) -> Result<catalog::VerifiedBundleCatalog, String>,
{
    let mut input = open_required_regular(path, "offline model bundle")?;
    let size_bytes = input
        .metadata()
        .map_err(|error| format!("failed to inspect {}: {error}", path.display()))?
        .len();
    prepare_open_offline_bundle_with_verifier(
        &mut input,
        path,
        size_bytes,
        stage_models,
        &mut verify_catalog,
    )
}

fn prepare_open_offline_bundle_with_verifier<F>(
    mut input: &mut File,
    path: &Path,
    size_bytes: u64,
    stage_models: bool,
    verify_catalog: &mut F,
) -> Result<PreparedBundle, String>
where
    F: FnMut(&[u8], &[u8]) -> Result<catalog::VerifiedBundleCatalog, String>,
{
    let bundle_sha256 = hash_open_file(&mut input, path, size_bytes)?;

    let mut magic = vec![0_u8; BUNDLE_MAGIC.len()];
    read_exact_described(&mut input, &mut magic, "offline bundle magic")?;
    if magic != BUNDLE_MAGIC {
        return Err("unsupported offline model bundle magic".into());
    }
    let manifest_size = read_u64(&mut input, "offline bundle manifest length")?;
    if manifest_size == 0 || manifest_size > MAX_MANIFEST_BYTES {
        return Err("offline bundle manifest size must be between 1 byte and 1 MiB".into());
    }
    let mut manifest_bytes = vec![
        0_u8;
        usize::try_from(manifest_size).map_err(|_| {
            "offline bundle manifest does not fit in memory".to_string()
        })?
    ];
    read_exact_described(&mut input, &mut manifest_bytes, "offline bundle manifest")?;
    let manifest: BundleManifest = serde_json::from_slice(&manifest_bytes)
        .map_err(|error| format!("invalid offline bundle manifest JSON: {error}"))?;
    validate_manifest_shape(&manifest)?;
    let expected_size = expected_bundle_size(manifest_size, &manifest)?;
    if expected_size != size_bytes {
        return Err(format!(
            "offline bundle length mismatch: manifest describes {expected_size} bytes, file has {size_bytes}"
        ));
    }

    let catalog_bytes = read_record_bytes(
        &mut input,
        &manifest.catalog,
        MAX_CATALOG_BYTES,
        "model catalog",
    )?;
    let signature = read_record_bytes(
        &mut input,
        &manifest.catalog_signature,
        MAX_SIGNATURE_BYTES,
        "model catalog signature",
    )?;
    let _trust_root = read_record_bytes(
        &mut input,
        &manifest.trust_root,
        MAX_TRUST_ROOT_BYTES,
        "model trust root",
    )?;
    let verified = verify_catalog(&catalog_bytes, &signature)?;
    if manifest.catalog.sha256 != verified.catalog.sha256() {
        return Err("offline bundle catalog digest does not match its signed contents".into());
    }
    if manifest.trust_root.sha256 != verified.trust_root_sha256 {
        return Err(format!(
            "offline bundle trust root does not match active trust root {}",
            verified.trust_root_sha256
        ));
    }
    validate_manifest_catalog(&manifest, &verified.catalog)?;

    let mut staged_models = Vec::with_capacity(manifest.models.len());
    let mut model_infos = Vec::with_capacity(manifest.models.len());
    for (record, model) in manifest.models.iter().zip(verified.catalog.models()) {
        let existed = if stage_models {
            let destination = path_for_catalog_model(model)?;
            preflight_existing_model(model, &destination)?
        } else {
            false
        };
        if stage_models {
            let mut artifact = tempfile::NamedTempFile::new()
                .map_err(|error| format!("failed to create offline model staging file: {error}"))?;
            read_record_to_writer(
                &mut input,
                &record.artifact,
                MAX_MODEL_BYTES,
                "model artifact",
                artifact.as_file_mut(),
            )?;
            artifact.as_file_mut().flush().map_err(|error| {
                format!("failed to flush staged model {}: {error}", model.name())
            })?;
            staged_models.push(PreparedModel {
                name: model.name().to_string(),
                artifact,
                existed,
            });
        } else {
            let mut sink = std::io::sink();
            read_record_to_writer(
                &mut input,
                &record.artifact,
                MAX_MODEL_BYTES,
                "model artifact",
                &mut sink,
            )?;
        }
        let license = read_named_record_bytes(
            &mut input,
            &record.license,
            MAX_METADATA_BYTES,
            "model license",
        )?;
        let provenance = read_named_record_bytes(
            &mut input,
            &record.provenance,
            MAX_METADATA_BYTES,
            "model provenance",
        )?;
        validate_source_provenance(model, require_bundle_metadata(model)?, &provenance)?;
        // Keep the authenticated non-executable bytes in the verification
        // path even though installation provenance records their signed
        // digests rather than copying them into the executable model cache.
        drop((license, provenance));
        model_infos.push(model_info(model)?);
    }
    let mut trailing = [0_u8; 1];
    if input
        .read(&mut trailing)
        .map_err(|error| format!("failed to finish reading {}: {error}", path.display()))?
        != 0
    {
        return Err("offline bundle contains trailing bytes".into());
    }
    let final_sha256 = hash_open_file(&mut input, path, size_bytes)?;
    if final_sha256 != bundle_sha256 {
        return Err(format!(
            "offline bundle changed while reading {}",
            path.display()
        ));
    }
    Ok(PreparedBundle {
        info: OfflineBundleInfo {
            format_version: BUNDLE_VERSION,
            bundle_sha256,
            size_bytes,
            catalog_sequence: verified.catalog.sequence(),
            catalog_sha256: verified.catalog.sha256().to_string(),
            catalog_signing_key_id: verified.catalog.signing_key_id().to_string(),
            catalog_issued_at_unix_seconds: verified.catalog.issued_at_unix_seconds(),
            catalog_expires_at_unix_seconds: verified.catalog.expires_at_unix_seconds(),
            trust_root_version: verified.trust_root_version,
            trust_root_sha256: verified.trust_root_sha256,
            models: model_infos,
        },
        catalog: verified.catalog,
        catalog_bytes,
        signature,
        models: staged_models,
    })
}

fn validate_manifest_shape(manifest: &BundleManifest) -> Result<(), String> {
    if manifest.schema != BUNDLE_SCHEMA || manifest.version != BUNDLE_VERSION {
        return Err("unsupported offline model bundle manifest version".into());
    }
    if manifest.models.is_empty() || manifest.models.len() > MAX_MODELS {
        return Err(format!(
            "offline bundle must contain between 1 and {MAX_MODELS} models"
        ));
    }
    validate_record(&manifest.catalog, MAX_CATALOG_BYTES, "model catalog")?;
    validate_record(
        &manifest.catalog_signature,
        MAX_SIGNATURE_BYTES,
        "model catalog signature",
    )?;
    validate_record(
        &manifest.trust_root,
        MAX_TRUST_ROOT_BYTES,
        "model trust root",
    )?;
    let mut names = HashSet::with_capacity(manifest.models.len());
    for model in &manifest.models {
        if !names.insert(model.name.as_str()) {
            return Err(format!("duplicate offline bundle model: {}", model.name));
        }
        validate_named_record(&model.artifact, MAX_MODEL_BYTES, "model artifact")?;
        validate_named_record(&model.license, MAX_METADATA_BYTES, "model license")?;
        validate_named_record(&model.provenance, MAX_METADATA_BYTES, "model provenance")?;
    }
    Ok(())
}

fn validate_manifest_catalog(
    manifest: &BundleManifest,
    catalog: &ModelCatalog,
) -> Result<(), String> {
    if manifest.models.len() != catalog.models().len() {
        return Err("offline bundle model count does not match the signed catalog".into());
    }
    for (record, model) in manifest.models.iter().zip(catalog.models()) {
        let metadata = require_bundle_metadata(model)?;
        if record.name != model.name()
            || !named_record_matches(
                &record.artifact,
                model.filename(),
                model.size_bytes(),
                model.sha256(),
            )
            || !named_record_matches_catalog_file(&record.license, metadata.license())
            || !named_record_matches_catalog_file(&record.provenance, metadata.provenance())
        {
            return Err(format!(
                "offline bundle entry does not match signed catalog model {}",
                model.name()
            ));
        }
    }
    Ok(())
}

fn require_bundle_metadata(model: &CatalogModel) -> Result<&super::CatalogBundleMetadata, String> {
    model.offline_bundle().ok_or_else(|| {
        format!(
            "signed catalog model {} has no offline bundle license/provenance metadata",
            model.name()
        )
    })
}

fn preflight_existing_model(model: &CatalogModel, destination: &Path) -> Result<bool, String> {
    validate_model_storage_path(destination)?;
    let Some(mut existing) = open_existing_regular_file(destination, "installed model")? else {
        return Ok(false);
    };
    let actual = hash_open_file(&mut existing, destination, model.size_bytes())?;
    if actual != model.sha256() {
        return Err(format!(
            "refusing to replace an existing mismatched model during bundle import: {}",
            destination.display()
        ));
    }
    Ok(true)
}

fn model_info(model: &CatalogModel) -> Result<OfflineBundleModelInfo, String> {
    let metadata = require_bundle_metadata(model)?;
    Ok(OfflineBundleModelInfo {
        name: model.name().to_string(),
        backend: model.backend().to_string(),
        artifact_filename: model.filename().to_string(),
        artifact_sha256: model.sha256().to_string(),
        artifact_size_bytes: model.size_bytes(),
        license_filename: metadata.license().filename().to_string(),
        license_sha256: metadata.license().sha256().to_string(),
        license_size_bytes: metadata.license().size_bytes(),
        provenance_filename: metadata.provenance().filename().to_string(),
        provenance_sha256: metadata.provenance().sha256().to_string(),
        provenance_size_bytes: metadata.provenance().size_bytes(),
    })
}

fn validate_source_provenance(
    model: &CatalogModel,
    metadata: &super::CatalogBundleMetadata,
    bytes: &[u8],
) -> Result<(), String> {
    let document: SourceProvenanceDocument = serde_json::from_slice(bytes).map_err(|error| {
        format!(
            "invalid offline provenance JSON for model {}: {error}",
            model.name()
        )
    })?;
    if document.schema != "denoize-model-source-provenance-v1"
        || document.model_name != model.name()
        || document.upstream_revision != model.revision()
        || document.artifact.url != model.url()
        || document.artifact.sha256 != model.sha256()
        || document.artifact.size_bytes != model.size_bytes()
        || document.license.spdx != model.license()
        || document.license.sha256 != metadata.license().sha256()
        || document.license.size_bytes != metadata.license().size_bytes()
        || !valid_public_https_url(&document.upstream_repository)
        || !valid_public_https_url(&document.license.url)
    {
        return Err(format!(
            "offline provenance does not match signed catalog model {}",
            model.name()
        ));
    }
    Ok(())
}

fn valid_public_https_url(value: &str) -> bool {
    Url::parse(value).is_ok_and(|url| {
        url.scheme() == "https"
            && url.host_str().is_some()
            && url.username().is_empty()
            && url.password().is_none()
            && url.query().is_none()
            && url.fragment().is_none()
    })
}

fn expected_bundle_size(manifest_size: u64, manifest: &BundleManifest) -> Result<u64, String> {
    let mut total = (BUNDLE_MAGIC.len() as u64)
        .checked_add(8)
        .and_then(|value| value.checked_add(manifest_size))
        .ok_or_else(|| "offline bundle size overflow".to_string())?;
    for size in [
        manifest.catalog.size_bytes,
        manifest.catalog_signature.size_bytes,
        manifest.trust_root.size_bytes,
    ] {
        total = total
            .checked_add(size)
            .ok_or_else(|| "offline bundle size overflow".to_string())?;
    }
    for model in &manifest.models {
        for size in [
            model.artifact.size_bytes,
            model.license.size_bytes,
            model.provenance.size_bytes,
        ] {
            total = total
                .checked_add(size)
                .ok_or_else(|| "offline bundle size overflow".to_string())?;
        }
    }
    Ok(total)
}

fn validate_named_record(
    record: &BundleNamedFileRecord,
    maximum: u64,
    description: &str,
) -> Result<(), String> {
    if record.filename.is_empty()
        || record.filename.len() > 128
        || !record.filename.as_bytes()[0].is_ascii_alphanumeric()
        || !record
            .filename
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'-' | b'_'))
    {
        return Err(format!(
            "offline bundle {description} has an invalid filename"
        ));
    }
    validate_record(
        &BundleFileRecord {
            size_bytes: record.size_bytes,
            sha256: record.sha256.clone(),
        },
        maximum,
        description,
    )
}

fn validate_record(
    record: &BundleFileRecord,
    maximum: u64,
    description: &str,
) -> Result<(), String> {
    if record.size_bytes == 0 || record.size_bytes > maximum {
        return Err(format!(
            "offline bundle {description} size must be between 1 and {maximum} bytes"
        ));
    }
    if !valid_sha256(&record.sha256) {
        return Err(format!(
            "offline bundle {description} has an invalid lowercase SHA-256"
        ));
    }
    Ok(())
}

fn valid_sha256(value: &str) -> bool {
    value.len() == 64
        && value
            .bytes()
            .all(|byte| byte.is_ascii_digit() || matches!(byte, b'a'..=b'f'))
}

fn named_record_matches(
    record: &BundleNamedFileRecord,
    filename: &str,
    size_bytes: u64,
    sha256: &str,
) -> bool {
    record.filename == filename && record.size_bytes == size_bytes && record.sha256 == sha256
}

fn named_record_matches_catalog_file(
    record: &BundleNamedFileRecord,
    file: &CatalogBundleFile,
) -> bool {
    named_record_matches(record, file.filename(), file.size_bytes(), file.sha256())
}

fn file_record(bytes: &[u8]) -> BundleFileRecord {
    BundleFileRecord {
        size_bytes: bytes.len() as u64,
        sha256: sha256_bytes(bytes),
    }
}

fn named_record_for_catalog_file(
    file: &mut File,
    path: &Path,
    expected: &CatalogBundleFile,
) -> Result<BundleNamedFileRecord, String> {
    named_record_for_open_file(
        file,
        path,
        expected.filename(),
        expected.size_bytes(),
        expected.sha256(),
    )
}

fn named_record_for_open_file(
    file: &mut File,
    path: &Path,
    filename: &str,
    expected_size: u64,
    expected_sha256: &str,
) -> Result<BundleNamedFileRecord, String> {
    let actual = hash_open_file(file, path, expected_size)?;
    if actual != expected_sha256 {
        return Err(format!(
            "offline bundle component checksum mismatch for {}: expected {expected_sha256}, got {actual}",
            path.display()
        ));
    }
    Ok(BundleNamedFileRecord {
        filename: filename.to_string(),
        size_bytes: expected_size,
        sha256: expected_sha256.to_string(),
    })
}

fn read_record_bytes(
    input: &mut File,
    record: &BundleFileRecord,
    maximum: u64,
    description: &str,
) -> Result<Vec<u8>, String> {
    validate_record(record, maximum, description)?;
    let mut bytes = vec![
        0_u8;
        usize::try_from(record.size_bytes).map_err(|_| format!(
            "offline bundle {description} does not fit in memory"
        ))?
    ];
    read_exact_described(input, &mut bytes, description)?;
    let actual = sha256_bytes(&bytes);
    if actual != record.sha256 {
        return Err(format!(
            "offline bundle {description} checksum mismatch: expected {}, got {actual}",
            record.sha256
        ));
    }
    Ok(bytes)
}

fn read_named_record_bytes(
    input: &mut File,
    record: &BundleNamedFileRecord,
    maximum: u64,
    description: &str,
) -> Result<Vec<u8>, String> {
    validate_named_record(record, maximum, description)?;
    read_record_bytes(
        input,
        &BundleFileRecord {
            size_bytes: record.size_bytes,
            sha256: record.sha256.clone(),
        },
        maximum,
        description,
    )
}

fn read_record_to_writer(
    input: &mut File,
    record: &BundleNamedFileRecord,
    maximum: u64,
    description: &str,
    output: &mut dyn Write,
) -> Result<(), String> {
    validate_named_record(record, maximum, description)?;
    let mut remaining = record.size_bytes;
    let mut digest = Sha256::new();
    let mut buffer = [0_u8; 64 * 1024];
    while remaining > 0 {
        let count = usize::try_from(remaining.min(buffer.len() as u64))
            .map_err(|_| "offline bundle chunk size overflow".to_string())?;
        read_exact_described(input, &mut buffer[..count], description)?;
        digest.update(&buffer[..count]);
        output
            .write_all(&buffer[..count])
            .map_err(|error| format!("failed to stage offline {description}: {error}"))?;
        remaining -= count as u64;
    }
    let actual = format!("{:x}", digest.finalize());
    if actual != record.sha256 {
        return Err(format!(
            "offline bundle {description} checksum mismatch: expected {}, got {actual}",
            record.sha256
        ));
    }
    Ok(())
}

fn copy_open_file(
    input: &mut File,
    path: &Path,
    output: &mut File,
    expected_size: u64,
    expected_sha256: &str,
) -> Result<(), String> {
    input
        .seek(SeekFrom::Start(0))
        .map_err(|error| format!("failed to rewind {}: {error}", path.display()))?;
    let mut remaining = expected_size;
    let mut digest = Sha256::new();
    let mut buffer = [0_u8; 64 * 1024];
    while remaining > 0 {
        let count = usize::try_from(remaining.min(buffer.len() as u64))
            .map_err(|_| "offline bundle component size overflow".to_string())?;
        read_exact_described(input, &mut buffer[..count], "offline bundle component")?;
        digest.update(&buffer[..count]);
        output
            .write_all(&buffer[..count])
            .map_err(|error| format!("failed to write offline bundle: {error}"))?;
        remaining -= count as u64;
    }
    let after = input
        .metadata()
        .map_err(|error| format!("failed to inspect {}: {error}", path.display()))?
        .len();
    let actual = format!("{:x}", digest.finalize());
    if after != expected_size || actual != expected_sha256 {
        return Err(format!(
            "offline bundle component changed while copying {}",
            path.display()
        ));
    }
    Ok(())
}

fn hash_open_file(file: &mut File, path: &Path, expected_size: u64) -> Result<String, String> {
    let before = file
        .metadata()
        .map_err(|error| format!("failed to inspect {}: {error}", path.display()))?
        .len();
    if before != expected_size {
        return Err(format!(
            "offline bundle component size mismatch for {}: expected {expected_size}, got {before}",
            path.display()
        ));
    }
    file.seek(SeekFrom::Start(0))
        .map_err(|error| format!("failed to rewind {}: {error}", path.display()))?;
    let mut digest = Sha256::new();
    let mut read = 0_u64;
    let mut buffer = [0_u8; 64 * 1024];
    loop {
        let count = file
            .read(&mut buffer)
            .map_err(|error| format!("failed to read {}: {error}", path.display()))?;
        if count == 0 {
            break;
        }
        read = read
            .checked_add(count as u64)
            .ok_or_else(|| "offline bundle component size overflow".to_string())?;
        if read > expected_size {
            return Err(format!(
                "offline bundle component grew while reading {}",
                path.display()
            ));
        }
        digest.update(&buffer[..count]);
    }
    let after = file
        .metadata()
        .map_err(|error| format!("failed to inspect {}: {error}", path.display()))?
        .len();
    if read != expected_size || after != expected_size {
        return Err(format!(
            "offline bundle component changed while reading {}",
            path.display()
        ));
    }
    file.seek(SeekFrom::Start(0))
        .map_err(|error| format!("failed to rewind {}: {error}", path.display()))?;
    Ok(format!("{:x}", digest.finalize()))
}

fn read_open_component_bytes(
    file: &mut File,
    path: &Path,
    expected_size: u64,
) -> Result<Vec<u8>, String> {
    file.seek(SeekFrom::Start(0))
        .map_err(|error| format!("failed to rewind {}: {error}", path.display()))?;
    let mut bytes = Vec::with_capacity(usize::try_from(expected_size).map_err(|_| {
        format!(
            "offline component does not fit in memory: {}",
            path.display()
        )
    })?);
    file.take(expected_size + 1)
        .read_to_end(&mut bytes)
        .map_err(|error| format!("failed to read {}: {error}", path.display()))?;
    if bytes.len() as u64 != expected_size {
        return Err(format!(
            "offline component changed while reading {}",
            path.display()
        ));
    }
    file.seek(SeekFrom::Start(0))
        .map_err(|error| format!("failed to rewind {}: {error}", path.display()))?;
    Ok(bytes)
}

fn open_required_regular(path: &Path, description: &str) -> Result<File, String> {
    open_existing_regular_file(path, description)?
        .ok_or_else(|| format!("failed to open {}: file not found", path.display()))
}

fn read_bounded_regular(path: &Path, maximum: u64, description: &str) -> Result<Vec<u8>, String> {
    let mut file = open_required_regular(path, description)?;
    let length = file
        .metadata()
        .map_err(|error| format!("failed to inspect {}: {error}", path.display()))?
        .len();
    if length == 0 || length > maximum {
        return Err(format!(
            "{description} size must be between 1 and {maximum} bytes"
        ));
    }
    let mut bytes = vec![
        0_u8;
        usize::try_from(length)
            .map_err(|_| format!("{description} does not fit in memory"))?
    ];
    read_exact_described(&mut file, &mut bytes, description)?;
    let after = file
        .metadata()
        .map_err(|error| format!("failed to inspect {}: {error}", path.display()))?
        .len();
    if after != length {
        return Err(format!(
            "{description} changed while reading {}",
            path.display()
        ));
    }
    Ok(bytes)
}

fn read_u64(input: &mut File, description: &str) -> Result<u64, String> {
    let mut bytes = [0_u8; 8];
    read_exact_described(input, &mut bytes, description)?;
    Ok(u64::from_be_bytes(bytes))
}

fn read_exact_described(
    input: &mut File,
    buffer: &mut [u8],
    description: &str,
) -> Result<(), String> {
    input
        .read_exact(buffer)
        .map_err(|error| format!("failed to read {description}: {error}"))
}

fn sha256_bytes(bytes: &[u8]) -> String {
    let mut digest = Sha256::new();
    digest.update(bytes);
    format!("{:x}", digest.finalize())
}

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

    #[test]
    fn production_bundle_evidence_matches_the_catalog() {
        let catalog = catalog::parse_catalog(
            include_bytes!("../../models/catalog-v1.json"),
            crate::models::CatalogOrigin::Embedded,
        )
        .unwrap();
        let model = catalog.find("gtcrn-dns3").unwrap();
        let metadata = require_bundle_metadata(model).unwrap();
        let license = include_bytes!("../../models/licenses/gtcrn-dns3-MIT.txt");
        let provenance = include_bytes!("../../models/provenance/gtcrn-dns3.json");

        assert_eq!(license.len() as u64, metadata.license().size_bytes());
        assert_eq!(sha256_bytes(license), metadata.license().sha256());
        assert_eq!(provenance.len() as u64, metadata.provenance().size_bytes());
        assert_eq!(sha256_bytes(provenance), metadata.provenance().sha256());
        validate_source_provenance(model, metadata, provenance).unwrap();
    }
}