khive-db 0.12.0

SQLite storage backend: entities, edges, notes, events, FTS5, sqlite-vec vectors.
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
use super::*;

fn valid_config() -> S3BlobStoreConfig {
    S3BlobStoreConfig::new("khive-blobs", "us-east-1")
}

#[test]
fn config_rejects_empty_bucket() {
    let mut cfg = valid_config();
    cfg.bucket = String::new();
    assert!(cfg.validate().is_err());
}

#[test]
fn config_rejects_empty_region() {
    let mut cfg = valid_config();
    cfg.region = String::new();
    assert!(cfg.validate().is_err());
}

#[test]
fn config_default_prefix_is_blobs() {
    assert_eq!(valid_config().prefix, "blobs");
}

#[test]
fn config_rejects_leading_slash_prefix() {
    let cfg = valid_config().with_prefix("/blobs");
    assert!(cfg.validate().is_err());
}

#[test]
fn config_rejects_trailing_slash_prefix() {
    let cfg = valid_config().with_prefix("blobs/");
    assert!(cfg.validate().is_err());
}

#[test]
fn config_rejects_empty_prefix() {
    let cfg = valid_config().with_prefix("");
    assert!(cfg.validate().is_err());
}

#[test]
fn config_rejects_dot_segment_in_prefix() {
    let cfg = valid_config().with_prefix("a/./b");
    assert!(cfg.validate().is_err());
}

#[test]
fn config_rejects_dotdot_segment_in_prefix() {
    let cfg = valid_config().with_prefix("a/../b");
    assert!(cfg.validate().is_err());
}

#[test]
fn config_rejects_double_slash_prefix() {
    let cfg = valid_config().with_prefix("a//b");
    assert!(cfg.validate().is_err());
}

#[test]
fn config_accepts_nested_prefix() {
    let cfg = valid_config().with_prefix("a/b/c");
    assert!(cfg.validate().is_ok());
}

#[test]
fn config_rejects_http_endpoint_without_allow_http() {
    let cfg = valid_config().with_endpoint("http://localhost:9000");
    assert!(cfg.validate().is_err());
}

#[test]
fn config_accepts_http_endpoint_with_allow_http() {
    let cfg = valid_config()
        .with_endpoint("http://localhost:9000")
        .with_allow_http(true);
    assert!(cfg.validate().is_ok());
}

#[test]
fn config_accepts_https_endpoint_without_allow_http() {
    let cfg = valid_config().with_endpoint("https://objects.example.invalid");
    assert!(cfg.validate().is_ok());
}

// Retain the credential fixture lock within each isolated child. Store
// construction receives fixed credentials before child startup.
static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

fn clear_aws_env() {
    crate::test_process::remove_var("AWS_ACCESS_KEY_ID");
    crate::test_process::remove_var("AWS_SECRET_ACCESS_KEY");
    crate::test_process::remove_var("AWS_SESSION_TOKEN");
}

#[test]
fn credentials_from_env_errors_when_both_missing() {
    if crate::test_process::run_in_child(|_| {}) {
        return;
    }
    let _guard = ENV_LOCK.lock().unwrap();
    clear_aws_env();
    let err = S3Credentials::from_env().unwrap_err();
    assert!(err.to_string().contains("AWS_ACCESS_KEY_ID"));
    clear_aws_env();
}

#[test]
fn credentials_from_env_errors_when_only_access_key_set() {
    if crate::test_process::run_in_child(|_| {}) {
        return;
    }
    let _guard = ENV_LOCK.lock().unwrap();
    clear_aws_env();
    crate::test_process::set_var("AWS_ACCESS_KEY_ID", "AKIAEXAMPLE");
    let err = S3Credentials::from_env().unwrap_err();
    let msg = err.to_string();
    assert!(msg.contains("AWS_SECRET_ACCESS_KEY"));
    assert!(
        !msg.contains("AKIAEXAMPLE"),
        "error must never contain a credential value: {msg}"
    );
    clear_aws_env();
}

#[test]
fn credentials_from_env_errors_when_only_secret_key_set() {
    if crate::test_process::run_in_child(|_| {}) {
        return;
    }
    let _guard = ENV_LOCK.lock().unwrap();
    clear_aws_env();
    crate::test_process::set_var("AWS_SECRET_ACCESS_KEY", "supersecret");
    let err = S3Credentials::from_env().unwrap_err();
    let msg = err.to_string();
    assert!(msg.contains("AWS_ACCESS_KEY_ID"));
    assert!(
        !msg.contains("supersecret"),
        "error must never contain a credential value: {msg}"
    );
    clear_aws_env();
}

#[test]
fn credentials_from_env_accepts_the_required_pair() {
    if crate::test_process::run_in_child(|_| {}) {
        return;
    }
    let _guard = ENV_LOCK.lock().unwrap();
    clear_aws_env();
    crate::test_process::set_var("AWS_ACCESS_KEY_ID", "AKIAEXAMPLE");
    crate::test_process::set_var("AWS_SECRET_ACCESS_KEY", "supersecret");
    let creds = S3Credentials::from_env().unwrap();
    assert_eq!(creds.access_key_id, "AKIAEXAMPLE");
    assert_eq!(creds.secret_access_key, "supersecret");
    assert!(creds.session_token.is_none());
    clear_aws_env();
}

#[test]
fn credentials_from_env_reads_optional_session_token() {
    if crate::test_process::run_in_child(|_| {}) {
        return;
    }
    let _guard = ENV_LOCK.lock().unwrap();
    clear_aws_env();
    crate::test_process::set_var("AWS_ACCESS_KEY_ID", "AKIAEXAMPLE");
    crate::test_process::set_var("AWS_SECRET_ACCESS_KEY", "supersecret");
    crate::test_process::set_var("AWS_SESSION_TOKEN", "sessiontoken");
    let creds = S3Credentials::from_env().unwrap();
    assert_eq!(creds.session_token.as_deref(), Some("sessiontoken"));
    clear_aws_env();
}

fn store_for_key_tests() -> S3BlobStore {
    let _guard = ENV_LOCK.lock().unwrap();
    S3BlobStore::new(valid_config()).unwrap()
}

#[test]
fn shard_key_matches_fs_blob_store_shape() {
    if crate::test_process::run_in_child(|command| {
        command
            .env("AWS_ACCESS_KEY_ID", "AKIAEXAMPLE")
            .env("AWS_SECRET_ACCESS_KEY", "supersecret")
            .env_remove("AWS_SESSION_TOKEN");
    }) {
        return;
    }
    let store = store_for_key_tests();
    let content_ref = ContentRef::from_hex("a".repeat(64)).unwrap();
    let key = store.shard_key(&content_ref);
    let key_str: &str = key.as_ref();
    assert_eq!(key_str, format!("blobs/aa/aa/{}", "a".repeat(64)));
}

#[test]
fn parse_shard_key_roundtrips_a_valid_key() {
    if crate::test_process::run_in_child(|command| {
        command
            .env("AWS_ACCESS_KEY_ID", "AKIAEXAMPLE")
            .env("AWS_SECRET_ACCESS_KEY", "supersecret")
            .env_remove("AWS_SESSION_TOKEN");
    }) {
        return;
    }
    let store = store_for_key_tests();
    let content_ref = ContentRef::from_hex("b".repeat(64)).unwrap();
    let key = store.shard_key(&content_ref);
    assert_eq!(store.parse_shard_key(&key), Some(content_ref));
}

#[test]
fn parse_shard_key_rejects_a_foreign_key_under_the_prefix() {
    if crate::test_process::run_in_child(|command| {
        command
            .env("AWS_ACCESS_KEY_ID", "AKIAEXAMPLE")
            .env("AWS_SECRET_ACCESS_KEY", "supersecret")
            .env_remove("AWS_SESSION_TOKEN");
    }) {
        return;
    }
    let store = store_for_key_tests();
    let foreign = ObjectPath::from("blobs/README.txt");
    assert_eq!(store.parse_shard_key(&foreign), None);
}

#[test]
fn parse_shard_key_rejects_mismatched_shard_segments() {
    if crate::test_process::run_in_child(|command| {
        command
            .env("AWS_ACCESS_KEY_ID", "AKIAEXAMPLE")
            .env("AWS_SECRET_ACCESS_KEY", "supersecret")
            .env_remove("AWS_SESSION_TOKEN");
    }) {
        return;
    }
    let store = store_for_key_tests();
    let hex = "c".repeat(64);
    // Shard segments deliberately don't match the hex's own prefix.
    let bad = ObjectPath::from(format!("blobs/00/00/{hex}"));
    assert_eq!(store.parse_shard_key(&bad), None);
}

#[test]
fn parse_shard_key_rejects_extra_path_segments() {
    if crate::test_process::run_in_child(|command| {
        command
            .env("AWS_ACCESS_KEY_ID", "AKIAEXAMPLE")
            .env("AWS_SECRET_ACCESS_KEY", "supersecret")
            .env_remove("AWS_SESSION_TOKEN");
    }) {
        return;
    }
    let store = store_for_key_tests();
    let hex = "d".repeat(64);
    let bad = ObjectPath::from(format!("blobs/dd/dd/{hex}/extra"));
    assert_eq!(store.parse_shard_key(&bad), None);
}

#[test]
fn parse_shard_key_rejects_key_outside_the_configured_prefix() {
    if crate::test_process::run_in_child(|command| {
        command
            .env("AWS_ACCESS_KEY_ID", "AKIAEXAMPLE")
            .env("AWS_SECRET_ACCESS_KEY", "supersecret")
            .env_remove("AWS_SESSION_TOKEN");
    }) {
        return;
    }
    let store = store_for_key_tests();
    let hex = "e".repeat(64);
    let bad = ObjectPath::from(format!("other-prefix/ee/ee/{hex}"));
    assert_eq!(store.parse_shard_key(&bad), None);
}

// ── Fake-client error-mapping tests (ADR-111 Amendment 2) ─
//
// `S3BlobStore` only ever holds an `Arc<dyn ObjectStore>` -- `ObjectStore`
// (from the `object_store` crate, not a khive-authored trait) is already
// the seam. `FakeObjectStore` implements it with fully scripted
// outcomes, so these tests exercise the retry/timeout/classification
// logic in `put`/`get_bounded_verified`/`exists` without any network
// dependency.

mod fake_client {
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Mutex;

    use bytes::Bytes;
    use futures::stream::{self, BoxStream};
    use object_store::{
        GetOptions, GetResult, GetResultPayload, ListResult, MultipartUpload, PutMultipartOptions,
        PutOptions, PutResult,
    };

    use super::*;

    /// One scripted outcome for a single call to the fake's `put_opts`
    /// or `get_opts`.
    #[derive(Clone, Debug)]
    pub enum Outcome {
        Ok,
        NotFound,
        AlreadyExists,
        PermissionDenied,
        /// The catch-all shape a real exhausted/timed-out request lands
        /// in once `object_store`'s own retry budget is spent (see
        /// `is_outer_retryable`'s doc comment) -- what our own outer
        /// retry treats as transient.
        Generic,
        /// Never resolves within the test's configured
        /// `request_timeout`, so the caller's `tokio::time::timeout`
        /// elapses first.
        Hang,
    }

    fn outcome_to_result<T>(outcome: &Outcome, make_ok: impl FnOnce() -> T) -> Result<T> {
        match outcome {
            Outcome::Ok => Ok(make_ok()),
            Outcome::NotFound => Err(ObjectStoreError::NotFound {
                path: "fake".into(),
                source: "not found".into(),
            }),
            Outcome::AlreadyExists => Err(ObjectStoreError::AlreadyExists {
                path: "fake".into(),
                source: "already exists".into(),
            }),
            Outcome::PermissionDenied => Err(ObjectStoreError::PermissionDenied {
                path: "fake".into(),
                source: "access denied".into(),
            }),
            Outcome::Generic => Err(ObjectStoreError::Generic {
                store: "fake",
                source: "exhausted transient failure".into(),
            }),
            Outcome::Hang => unreachable!("Hang must be intercepted before this call"),
        }
    }

    type Result<T> = std::result::Result<T, ObjectStoreError>;

    /// One scripted outcome for a single entry the fake's `list` stream
    /// yields (ADR-111 Amendment 2 -- partial-page
    /// error/timeout coverage for `orphan_sweep`).
    #[derive(Clone, Debug)]
    pub enum ListOutcome {
        /// Yield one valid `ObjectMeta` at this object-store key.
        Entry(String),
        /// The list stream errors at this position (mid-page failure).
        Err,
        /// Never resolves within the test's configured `request_timeout`.
        Hang,
    }

    /// A scripted `ObjectStore` fake. `put_script`/`get_opts_script` are
    /// consumed in order, one entry per call; running past the end of a
    /// script repeats its last entry (so a short script can still cover
    /// an unbounded outer-retry loop in a test). `list_script` and
    /// `delete_script` are Arc-wrapped because `delete_stream`/`list`
    /// must return a `'static` stream that outlives the `&self` borrow.
    #[derive(Debug)]
    pub struct FakeObjectStore {
        put_script: Mutex<Vec<Outcome>>,
        pub(super) put_payloads: Mutex<Vec<Bytes>>,
        get_script: Mutex<Vec<Outcome>>,
        list_script: Arc<Mutex<Vec<ListOutcome>>>,
        delete_script: Arc<Mutex<Vec<Outcome>>>,
        /// Overrides the default empty-body `Outcome::Ok` response for
        /// `get_opts`: `(reported_meta_size, pre-chunked stream body,
        /// per-chunk delay)`. Lets tests simulate an object store whose
        /// response metadata under-reports the real body length
        /// (ADR-160: bounded verified reads must cap the actual stream,
        /// not trust this), control exactly how many
        /// `stream.next()` calls the body arrives over (e.g. one
        /// oversized chunk vs many small ones), and — via the delay —
        /// simulate a peer trickling bytes slower than the total
        /// hydration deadline while still resolving each individual
        /// `stream.next()` call well within it (end-to-end vs per-chunk
        /// deadline coverage).
        get_body_override: Mutex<Option<(u64, Vec<Bytes>, Duration)>>,
        /// Sleep applied inside `get_opts` before resolving an `Ok`
        /// outcome — simulates a slow initial request that still
        /// resolves within `request_timeout`, so tests can prove the
        /// hydration deadline spans request + consume rather than
        /// granting each phase a fresh window.
        get_pre_delay: Mutex<Option<Duration>>,
        pub put_calls: AtomicUsize,
        /// Total `get_opts` calls, retained for existing retry/error
        /// tests whose script is shared by GET and HEAD.
        pub get_calls: AtomicUsize,
        pub head_calls: AtomicUsize,
        pub body_get_calls: AtomicUsize,
        pub body_polls: Arc<AtomicUsize>,
        pub delete_calls: Arc<AtomicUsize>,
        /// How long a `Hang` outcome sleeps before resolving --
        /// deliberately far longer than the test's configured
        /// `request_timeout`, so `tokio::time::timeout` always wins the
        /// race deterministically instead of depending on scheduler
        /// timing.
        hang_delay: Duration,
    }

    impl std::fmt::Display for FakeObjectStore {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "FakeObjectStore")
        }
    }

    impl FakeObjectStore {
        pub fn new(put_script: Vec<Outcome>, get_script: Vec<Outcome>) -> Self {
            Self {
                put_script: Mutex::new(put_script),
                put_payloads: Mutex::new(Vec::new()),
                get_script: Mutex::new(get_script),
                list_script: Arc::new(Mutex::new(Vec::new())),
                delete_script: Arc::new(Mutex::new(vec![Outcome::Ok])),
                get_body_override: Mutex::new(None),
                get_pre_delay: Mutex::new(None),
                put_calls: AtomicUsize::new(0),
                get_calls: AtomicUsize::new(0),
                head_calls: AtomicUsize::new(0),
                body_get_calls: AtomicUsize::new(0),
                body_polls: Arc::new(AtomicUsize::new(0)),
                delete_calls: Arc::new(AtomicUsize::new(0)),
                hang_delay: Duration::from_secs(3600),
            }
        }

        /// Script the fake's `list` stream: the sweep
        /// tests below use this to yield N valid entries then an error,
        /// or a `Hang` entry to exercise the per-page timeout.
        pub fn with_list_script(self, script: Vec<ListOutcome>) -> Self {
            *self.list_script.lock().unwrap() = script;
            self
        }

        /// Script the fake's `delete` outcomes. Defaults
        /// to always-`Ok` so existing sweep behavior needs no script.
        pub fn with_delete_script(self, script: Vec<Outcome>) -> Self {
            *self.delete_script.lock().unwrap() = script;
            self
        }

        /// Override the body an `Outcome::Ok` `get_opts` response
        /// streams, independent of the `reported_size` its `ObjectMeta`
        /// claims — used to simulate an object store whose response
        /// metadata under-reports the real streamed length. `chunks`
        /// controls exactly how many `stream.next()` calls the body
        /// arrives over (e.g. one oversized chunk vs many small ones),
        /// so tests can exercise the running-total bound both within a
        /// single chunk and across several.
        pub fn with_get_body_chunks(self, reported_size: u64, chunks: Vec<Bytes>) -> Self {
            *self.get_body_override.lock().unwrap() = Some((reported_size, chunks, Duration::ZERO));
            self
        }

        /// Same as [`Self::with_get_body_chunks`], but sleeps
        /// `per_chunk_delay` before yielding each chunk from the stream —
        /// lets a test drive each `stream.next()` call under a per-chunk
        /// timeout while the whole stream still exceeds an end-to-end one.
        pub fn with_get_body_chunks_delayed(
            self,
            reported_size: u64,
            chunks: Vec<Bytes>,
            per_chunk_delay: Duration,
        ) -> Self {
            *self.get_body_override.lock().unwrap() =
                Some((reported_size, chunks, per_chunk_delay));
            self
        }

        /// Sleep `delay` inside `get_opts` before resolving an `Ok`
        /// outcome (see `get_pre_delay`).
        pub fn with_get_pre_delay(self, delay: Duration) -> Self {
            *self.get_pre_delay.lock().unwrap() = Some(delay);
            self
        }

        fn next_outcome(script: &Mutex<Vec<Outcome>>, calls: &AtomicUsize) -> Outcome {
            let idx = calls.fetch_add(1, Ordering::SeqCst);
            let script = script.lock().unwrap();
            script[idx.min(script.len().saturating_sub(1))].clone()
        }
    }

    #[async_trait::async_trait]
    impl ObjectStore for FakeObjectStore {
        async fn put_opts(
            &self,
            _location: &ObjectPath,
            payload: PutPayload,
            _opts: PutOptions,
        ) -> Result<PutResult> {
            self.put_payloads
                .lock()
                .unwrap()
                .push(payload.iter().next().cloned().unwrap_or_default());
            let outcome = Self::next_outcome(&self.put_script, &self.put_calls);
            if matches!(outcome, Outcome::Hang) {
                tokio::time::sleep(self.hang_delay).await;
            }
            outcome_to_result(&outcome, || PutResult {
                e_tag: None,
                version: None,
                extensions: Default::default(),
            })
        }

        async fn put_multipart_opts(
            &self,
            _location: &ObjectPath,
            _opts: PutMultipartOptions,
        ) -> Result<Box<dyn MultipartUpload>> {
            unimplemented!("not exercised by these tests")
        }

        async fn get_opts(&self, location: &ObjectPath, opts: GetOptions) -> Result<GetResult> {
            if opts.head {
                self.head_calls.fetch_add(1, Ordering::SeqCst);
            } else {
                self.body_get_calls.fetch_add(1, Ordering::SeqCst);
            }
            let outcome = Self::next_outcome(&self.get_script, &self.get_calls);
            if matches!(outcome, Outcome::Hang) {
                tokio::time::sleep(self.hang_delay).await;
            }
            let pre_delay = *self.get_pre_delay.lock().unwrap();
            if let Some(delay) = pre_delay {
                tokio::time::sleep(delay).await;
            }
            let override_body = self.get_body_override.lock().unwrap().clone();
            let body_polls = Arc::clone(&self.body_polls);
            outcome_to_result(&outcome, || {
                let (reported_size, body_chunks, per_chunk_delay) =
                    override_body.unwrap_or((0, Vec::new(), Duration::ZERO));
                let body_stream = if per_chunk_delay.is_zero() {
                    stream::iter(body_chunks.into_iter().map(move |chunk| {
                        body_polls.fetch_add(1, Ordering::SeqCst);
                        Ok(chunk)
                    }))
                    .boxed()
                } else {
                    stream::iter(body_chunks)
                        .then(move |chunk| {
                            let body_polls = Arc::clone(&body_polls);
                            async move {
                                body_polls.fetch_add(1, Ordering::SeqCst);
                                tokio::time::sleep(per_chunk_delay).await;
                                Ok(chunk)
                            }
                        })
                        .boxed()
                };
                GetResult {
                    payload: GetResultPayload::Stream(body_stream),
                    meta: ObjectMeta {
                        location: location.clone(),
                        last_modified: chrono::Utc::now(),
                        size: reported_size,
                        e_tag: None,
                        version: None,
                    },
                    range: 0..0,
                    attributes: Default::default(),
                    extensions: Default::default(),
                }
            })
        }

        fn delete_stream(
            &self,
            locations: BoxStream<'static, Result<ObjectPath>>,
        ) -> BoxStream<'static, Result<ObjectPath>> {
            let delete_script = Arc::clone(&self.delete_script);
            let delete_calls = Arc::clone(&self.delete_calls);
            let hang_delay = self.hang_delay;
            locations
                .then(move |loc_result| {
                    let delete_script = Arc::clone(&delete_script);
                    let delete_calls = Arc::clone(&delete_calls);
                    async move {
                        let location = loc_result?;
                        let outcome =
                            Self::next_outcome(delete_script.as_ref(), delete_calls.as_ref());
                        if matches!(outcome, Outcome::Hang) {
                            tokio::time::sleep(hang_delay).await;
                        }
                        outcome_to_result(&outcome, || location.clone())
                    }
                })
                .boxed()
        }

        fn list(&self, _prefix: Option<&ObjectPath>) -> BoxStream<'static, Result<ObjectMeta>> {
            let script = self.list_script.lock().unwrap().clone();
            let hang_delay = self.hang_delay;
            if script.is_empty() {
                return stream::empty().boxed();
            }
            stream::iter(script)
                .then(move |outcome| async move {
                    match outcome {
                        ListOutcome::Entry(location) => Ok(ObjectMeta {
                            location: ObjectPath::from(location),
                            last_modified: chrono::Utc::now(),
                            size: 0,
                            e_tag: None,
                            version: None,
                        }),
                        ListOutcome::Err => Err(ObjectStoreError::Generic {
                            store: "fake",
                            source: "list exhausted transient failure".into(),
                        }),
                        ListOutcome::Hang => {
                            tokio::time::sleep(hang_delay).await;
                            unreachable!("Hang must be intercepted before this resolves")
                        }
                    }
                })
                .boxed()
        }

        async fn list_with_delimiter(&self, _prefix: Option<&ObjectPath>) -> Result<ListResult> {
            unimplemented!("not exercised by these tests")
        }

        async fn copy_opts(
            &self,
            _from: &ObjectPath,
            _to: &ObjectPath,
            _opts: object_store::CopyOptions,
        ) -> Result<()> {
            unimplemented!("not exercised by these tests")
        }
    }
}

use bytes::Bytes;
use fake_client::{FakeObjectStore, ListOutcome, Outcome};
use std::sync::atomic::Ordering;

fn fake_store(
    put_script: Vec<Outcome>,
    get_script: Vec<Outcome>,
) -> (Arc<FakeObjectStore>, S3BlobStore) {
    let fake = Arc::new(FakeObjectStore::new(put_script, get_script));
    let store = S3BlobStore::from_client_for_test(
        Arc::clone(&fake) as Arc<dyn ObjectStore>,
        "blobs",
        3,
        Duration::from_millis(50),
        Duration::from_millis(1),
    )
    .unwrap();
    (fake, store)
}

#[tokio::test]
async fn put_succeeds_on_first_attempt() {
    let (fake, store) = fake_store(vec![Outcome::Ok], vec![Outcome::NotFound]);
    let result = store.put(b"hello".to_vec()).await;
    assert!(result.is_ok());
    assert_eq!(fake.put_calls.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn put_already_exists_is_dedup_success_not_conflict() {
    let (_fake, store) = fake_store(vec![Outcome::AlreadyExists], vec![Outcome::NotFound]);
    let result = store.put(b"hello".to_vec()).await;
    assert!(result.is_ok(), "AlreadyExists must be treated as success");
}

#[tokio::test]
async fn put_head_timeout_then_already_exists_is_dedup_success() {
    let (fake, store) = fake_store(vec![Outcome::AlreadyExists], vec![Outcome::Hang]);
    assert!(store.put(b"head timeout dedup".to_vec()).await.is_ok());
    assert_eq!(fake.head_calls.load(Ordering::SeqCst), 1);
    assert_eq!(fake.put_calls.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn put_head_timeout_then_create_succeeds() {
    let (fake, store) = fake_store(vec![Outcome::Ok], vec![Outcome::Hang]);
    assert!(store.put(b"head timeout create".to_vec()).await.is_ok());
    assert_eq!(fake.head_calls.load(Ordering::SeqCst), 1);
    assert_eq!(fake.put_calls.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn put_head_permission_denied_still_fails_before_create() {
    let (fake, store) = fake_store(vec![Outcome::Ok], vec![Outcome::PermissionDenied]);
    let error = store
        .put(b"head permission denied".to_vec())
        .await
        .unwrap_err();
    assert!(matches!(error, StorageError::Driver { .. }), "{error:?}");
    assert_eq!(fake.head_calls.load(Ordering::SeqCst), 1);
    assert_eq!(fake.put_calls.load(Ordering::SeqCst), 0);
}

#[tokio::test]
async fn put_permission_denied_maps_to_driver_without_retry() {
    let (fake, store) = fake_store(vec![Outcome::PermissionDenied], vec![Outcome::NotFound]);
    let err = store.put(b"hello".to_vec()).await.unwrap_err();
    assert!(matches!(err, StorageError::Driver { .. }), "got {err:?}");
    assert_eq!(
        fake.put_calls.load(Ordering::SeqCst),
        1,
        "a credential failure must never be retried"
    );
}

#[tokio::test]
async fn put_exhausted_transient_failure_maps_to_driver() {
    let (fake, store) = fake_store(vec![Outcome::Generic], vec![Outcome::NotFound]);
    let err = store.put(b"hello".to_vec()).await.unwrap_err();
    assert!(matches!(err, StorageError::Driver { .. }), "got {err:?}");
    assert_eq!(
        fake.put_calls.load(Ordering::SeqCst),
        3,
        "must retry up to the configured max attempts before giving up"
    );
}

#[tokio::test]
async fn put_transient_then_success_recovers_within_the_retry_budget() {
    let (fake, store) = fake_store(vec![Outcome::Generic, Outcome::Ok], vec![Outcome::NotFound]);
    let result = store.put(b"hello".to_vec()).await;
    assert!(result.is_ok());
    assert_eq!(fake.put_calls.load(Ordering::SeqCst), 2);
    let payloads = fake.put_payloads.lock().unwrap();
    assert_eq!(payloads.len(), 2);
    assert_eq!(payloads[0], Bytes::from_static(b"hello"));
    assert_eq!(payloads[1], payloads[0]);
    assert_eq!(
        payloads[0].as_ptr(),
        payloads[1].as_ptr(),
        "retry payloads must share the same backing buffer"
    );
}

#[tokio::test]
async fn put_exhausted_timeout_maps_to_timeout_not_driver() {
    let (fake, store) = fake_store(vec![Outcome::Hang], vec![Outcome::NotFound]);
    let err = store.put(b"hello".to_vec()).await.unwrap_err();
    assert!(matches!(err, StorageError::Timeout { .. }), "got {err:?}");
    assert_eq!(fake.put_calls.load(Ordering::SeqCst), 3);
}

#[tokio::test]
async fn bounded_get_rejects_invalid_limit_before_provider_work() {
    let (fake, store) = fake_store(vec![], vec![Outcome::Hang]);
    let content_ref = ContentRef::from_hex("b".repeat(64)).unwrap();
    let err = store
        .get_bounded_verified(&content_ref, MAX_BLOB_WHOLE_BYTES + 1)
        .await
        .unwrap_err();
    assert!(
        matches!(err, StorageError::InvalidInput { .. }),
        "got {err:?}"
    );
    assert_eq!(
        fake.get_calls.load(Ordering::SeqCst),
        0,
        "argument validation must not construct or poll a provider GET"
    );
}

#[tokio::test]
async fn bounded_get_not_found_precedes_object_inspection() {
    let (_fake, store) = fake_store(vec![], vec![Outcome::NotFound]);
    let content_ref = ContentRef::from_hex("b".repeat(64)).unwrap();
    let err = store
        .get_bounded_verified(&content_ref, 0)
        .await
        .unwrap_err();
    assert!(matches!(err, StorageError::NotFound { .. }), "got {err:?}");
}

#[tokio::test]
async fn bounded_get_accepts_a_digest_matching_body_at_the_exact_limit() {
    let body = b"verified body".to_vec();
    let content_ref = ContentRef::from_digest_bytes(blake3::hash(&body).as_bytes());
    let fake = Arc::new(
        FakeObjectStore::new(vec![], vec![Outcome::Ok]).with_get_body_chunks(
            body.len() as u64,
            vec![
                Bytes::from(body[..4].to_vec()),
                Bytes::from(body[4..].to_vec()),
            ],
        ),
    );
    let store = S3BlobStore::from_client_for_test(
        Arc::clone(&fake) as Arc<dyn ObjectStore>,
        "blobs",
        3,
        Duration::from_secs(1),
        Duration::from_millis(1),
    )
    .unwrap();

    let result = store
        .get_bounded_verified(&content_ref, body.len() as u64)
        .await
        .unwrap();
    assert_eq!(result, body);
    assert_eq!(fake.body_get_calls.load(Ordering::SeqCst), 1);
    assert_eq!(fake.head_calls.load(Ordering::SeqCst), 0);
}

#[tokio::test]
async fn bounded_get_rejects_over_limit_metadata_before_consuming_body() {
    let fake = Arc::new(
        FakeObjectStore::new(vec![], vec![Outcome::Ok])
            .with_get_body_chunks(5, vec![Bytes::from_static(b"x")]),
    );
    let store = S3BlobStore::from_client_for_test(
        Arc::clone(&fake) as Arc<dyn ObjectStore>,
        "blobs",
        3,
        Duration::from_secs(1),
        Duration::from_millis(1),
    )
    .unwrap();
    let content_ref = ContentRef::from_hex("c".repeat(64)).unwrap();

    let err = store
        .get_bounded_verified(&content_ref, 4)
        .await
        .unwrap_err();
    assert!(matches!(
        err,
        StorageError::BlobTooLarge {
            content_ref: ref got,
            max_bytes: 4,
            observed_at_least: 5,
        } if got == &content_ref
    ));
    assert_eq!(
        fake.body_polls.load(Ordering::SeqCst),
        0,
        "over-limit metadata must refuse before the body stream is polled"
    );
}

#[tokio::test]
async fn bounded_get_stops_false_small_metadata_at_the_actual_byte_limit() {
    let fake = Arc::new(
        FakeObjectStore::new(vec![], vec![Outcome::Ok]).with_get_body_chunks(
            1,
            vec![
                Bytes::from_static(b"abc"),
                Bytes::from_static(b"de"),
                Bytes::from_static(b"must-not-be-polled"),
            ],
        ),
    );
    let store = S3BlobStore::from_client_for_test(
        Arc::clone(&fake) as Arc<dyn ObjectStore>,
        "blobs",
        3,
        Duration::from_secs(1),
        Duration::from_millis(1),
    )
    .unwrap();
    let content_ref = ContentRef::from_hex("d".repeat(64)).unwrap();

    let err = store
        .get_bounded_verified(&content_ref, 4)
        .await
        .unwrap_err();
    assert!(matches!(
        err,
        StorageError::BlobTooLarge {
            content_ref: ref got,
            max_bytes: 4,
            observed_at_least: 5,
        } if got == &content_ref
    ));
    assert_eq!(
        fake.body_polls.load(Ordering::SeqCst),
        2,
        "the stream must stop at the first chunk that crosses the limit"
    );
}

#[tokio::test]
async fn bounded_get_reports_metadata_body_size_mismatch_before_digest() {
    let fake = Arc::new(
        FakeObjectStore::new(vec![], vec![Outcome::Ok])
            .with_get_body_chunks(3, vec![Bytes::from_static(b"four")]),
    );
    let store = S3BlobStore::from_client_for_test(
        Arc::clone(&fake) as Arc<dyn ObjectStore>,
        "blobs",
        3,
        Duration::from_secs(1),
        Duration::from_millis(1),
    )
    .unwrap();
    let content_ref = ContentRef::from_hex("e".repeat(64)).unwrap();

    let err = store
        .get_bounded_verified(&content_ref, 4)
        .await
        .unwrap_err();
    assert!(matches!(
        err,
        StorageError::BlobSizeMismatch {
            content_ref: ref got,
            metadata_bytes: 3,
            actual_bytes: 4,
        } if got == &content_ref
    ));
}

#[tokio::test]
async fn bounded_get_reports_truncated_body_size_mismatch_before_digest() {
    let fake = Arc::new(
        FakeObjectStore::new(vec![], vec![Outcome::Ok])
            .with_get_body_chunks(5, vec![Bytes::from_static(b"four")]),
    );
    let store = S3BlobStore::from_client_for_test(
        Arc::clone(&fake) as Arc<dyn ObjectStore>,
        "blobs",
        3,
        Duration::from_secs(1),
        Duration::from_millis(1),
    )
    .unwrap();
    let content_ref = ContentRef::from_hex("e".repeat(64)).unwrap();

    let err = store
        .get_bounded_verified(&content_ref, 5)
        .await
        .unwrap_err();
    assert!(matches!(
        err,
        StorageError::BlobSizeMismatch {
            content_ref: ref got,
            metadata_bytes: 5,
            actual_bytes: 4,
        } if got == &content_ref
    ));
}

#[tokio::test]
async fn bounded_get_reports_digest_mismatch_only_after_size_consistency() {
    let expected_bytes = b"expected";
    let actual_bytes = b"mutated!";
    let expected = ContentRef::from_digest_bytes(blake3::hash(expected_bytes).as_bytes());
    let actual = ContentRef::from_digest_bytes(blake3::hash(actual_bytes).as_bytes());
    let fake = Arc::new(
        FakeObjectStore::new(vec![], vec![Outcome::Ok]).with_get_body_chunks(
            actual_bytes.len() as u64,
            vec![Bytes::from_static(actual_bytes)],
        ),
    );
    let store = S3BlobStore::from_client_for_test(
        Arc::clone(&fake) as Arc<dyn ObjectStore>,
        "blobs",
        3,
        Duration::from_secs(1),
        Duration::from_millis(1),
    )
    .unwrap();

    let err = store
        .get_bounded_verified(&expected, actual_bytes.len() as u64)
        .await
        .unwrap_err();
    assert!(matches!(
        err,
        StorageError::BlobDigestMismatch {
            expected: ref got_expected,
            actual: ref got_actual,
        } if got_expected == &expected && got_actual == &actual
    ));
}

#[tokio::test]
async fn bounded_get_deadline_spans_provider_get_and_body_consumption() {
    let chunks = vec![
        Bytes::from_static(b"12345678"),
        Bytes::from_static(b"abcdefgh"),
    ];
    let fake = Arc::new(
        FakeObjectStore::new(vec![], vec![Outcome::Ok])
            .with_get_pre_delay(Duration::from_millis(80))
            .with_get_body_chunks_delayed(16, chunks, Duration::from_millis(50)),
    );
    let store = S3BlobStore::from_client_for_test(
        Arc::clone(&fake) as Arc<dyn ObjectStore>,
        "blobs",
        3,
        Duration::from_millis(150),
        Duration::from_millis(1),
    )
    .unwrap();
    let content_ref = ContentRef::from_hex("f".repeat(64)).unwrap();

    let err = store
        .get_bounded_verified(&content_ref, 16)
        .await
        .unwrap_err();
    assert!(matches!(err, StorageError::Timeout { .. }), "got {err:?}");
}

#[tokio::test]
async fn size_returns_none_for_a_missing_object() {
    let (_fake, store) = fake_store(vec![], vec![Outcome::NotFound]);
    let content_ref = ContentRef::from_hex("1".repeat(64)).unwrap();
    assert_eq!(store.size(&content_ref).await.unwrap(), None);
}

#[tokio::test]
async fn size_timeout_maps_to_storage_timeout() {
    let (_fake, store) = fake_store(vec![], vec![Outcome::Hang]);
    let content_ref = ContentRef::from_hex("2".repeat(64)).unwrap();
    let err = store.size(&content_ref).await.unwrap_err();
    assert!(matches!(err, StorageError::Timeout { .. }), "got {err:?}");
}

#[tokio::test]
async fn exists_authorization_failure_maps_to_driver() {
    let (_fake, store) = fake_store(vec![], vec![Outcome::PermissionDenied]);
    let content_ref = ContentRef::from_hex("c".repeat(64)).unwrap();
    let err = store.exists(&content_ref).await.unwrap_err();
    assert!(matches!(err, StorageError::Driver { .. }), "got {err:?}");
}

#[tokio::test]
async fn exists_timeout_maps_to_storage_timeout() {
    let (_fake, store) = fake_store(vec![], vec![Outcome::Hang]);
    let content_ref = ContentRef::from_hex("d".repeat(64)).unwrap();
    let err = store.exists(&content_ref).await.unwrap_err();
    assert!(matches!(err, StorageError::Timeout { .. }), "got {err:?}");
}

// ── `orphan_sweep` timeout/error-mapping tests (ADR-111 Amendment 2) ──

fn valid_shard_key(byte: char) -> String {
    let hex = byte.to_string().repeat(64);
    format!("blobs/{}/{}/{}", &hex[0..2], &hex[2..4], hex)
}

fn fake_sweep_store(list_script: Vec<ListOutcome>) -> (Arc<FakeObjectStore>, S3BlobStore) {
    let fake = Arc::new(FakeObjectStore::new(vec![], vec![]).with_list_script(list_script));
    let store = S3BlobStore::from_client_for_test(
        Arc::clone(&fake) as Arc<dyn ObjectStore>,
        "blobs",
        3,
        Duration::from_millis(50),
        Duration::from_millis(1),
    )
    .unwrap();
    (fake, store)
}

#[tokio::test]
async fn orphan_sweep_list_timeout_maps_to_storage_timeout() {
    let (_fake, store) = fake_sweep_store(vec![ListOutcome::Hang]);
    let config = BlobOrphanSweepConfig {
        live_refs: Default::default(),
        dry_run: true,
    };
    let err = store.orphan_sweep(&config).await.unwrap_err();
    assert!(matches!(err, StorageError::Timeout { .. }), "got {err:?}");
}

#[tokio::test]
async fn orphan_sweep_partial_page_then_error_returns_err_not_partial_success() {
    // Two valid entries followed by a list error -- the sweep must
    // never report the two valid entries as a completed (partial) scan.
    let (_fake, store) = fake_sweep_store(vec![
        ListOutcome::Entry(valid_shard_key('1')),
        ListOutcome::Entry(valid_shard_key('2')),
        ListOutcome::Err,
    ]);
    let config = BlobOrphanSweepConfig {
        live_refs: Default::default(),
        dry_run: true,
    };
    let err = store.orphan_sweep(&config).await.unwrap_err();
    assert!(matches!(err, StorageError::Driver { .. }), "got {err:?}");
}

#[tokio::test]
async fn orphan_sweep_delete_timeout_maps_to_storage_timeout() {
    let fake = Arc::new(
        FakeObjectStore::new(vec![], vec![])
            .with_list_script(vec![ListOutcome::Entry(valid_shard_key('3'))])
            .with_delete_script(vec![Outcome::Hang]),
    );
    let store = S3BlobStore::from_client_for_test(
        Arc::clone(&fake) as Arc<dyn ObjectStore>,
        "blobs",
        3,
        Duration::from_millis(50),
        Duration::from_millis(1),
    )
    .unwrap();
    // Empty `live_refs`: the single scripted entry is orphaned, so a
    // non-dry-run sweep issues exactly one delete, which hangs past the
    // configured request_timeout.
    let config = BlobOrphanSweepConfig {
        live_refs: Default::default(),
        dry_run: false,
    };
    let err = store.orphan_sweep(&config).await.unwrap_err();
    assert!(matches!(err, StorageError::Timeout { .. }), "got {err:?}");
}