notedthat-write 0.12.1

Shared write path (commit, patch, replace) for NotedThat HTTP API and WebDAV surfaces
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
//! Shared commit operations for object writes and deletes.

use notedthat_core::{
    ConditionalHeaders, CopyObjectOptions, KbSlug, ObjectEvent, ObjectPath, PutOutcome, StagedBody,
    Storage, StorageError,
};
use notedthat_indexer::IndexEvent;
use tokio::sync::mpsc::Sender;
use tokio::sync::mpsc::error::TrySendError;

use crate::WriteError;
use crate::error::WriteEffect;
use crate::mime::sniff_content_type;
use crate::sinks::WriteSinks;

/// Maximum upload size accepted by shared write paths: 5 GiB.
pub const MAX_UPLOAD_BYTES: u64 = 5 * 1024 * 1024 * 1024;

/// Validate an upload size against a byte limit.
pub fn check_size(size: u64, limit: u64) -> Result<(), WriteError> {
    if size > limit {
        Err(WriteError::TooLarge { size, limit })
    } else {
        Ok(())
    }
}

/// Store an object, enqueue a best-effort index upsert event and publish the
/// change.
pub async fn commit<B: Into<StagedBody>>(
    storage: &dyn Storage,
    sinks: &WriteSinks<'_>,
    kb: &KbSlug,
    path: &ObjectPath,
    body: B,
    caller_content_type: Option<&str>,
    conditionals: ConditionalHeaders,
) -> Result<PutOutcome, WriteError> {
    let body = body.into();
    let size = body.len();
    check_size(size, MAX_UPLOAD_BYTES)?;
    crate::manifest::check_staged_manifest(kb, path, &body).await?;
    let _prefix = body.prefix(512).await.map_err(|source| {
        WriteError::Storage(StorageError::Other {
            source: Box::new(source),
        })
    })?;
    let mime = sniff_content_type(caller_content_type, path);
    let outcome = storage
        .put_staged_object(kb, path, body, Some(&mime), conditionals)
        .await?;

    after_write(sinks, kb, path, &outcome, size, &mime).await?;
    Ok(outcome)
}

/// Copy an object natively, enqueue its destination for indexing and publish
/// the change.
pub async fn commit_copy(
    storage: &dyn Storage,
    sinks: &WriteSinks<'_>,
    kb: &KbSlug,
    source: &ObjectPath,
    destination: &ObjectPath,
    options: CopyObjectOptions,
) -> Result<PutOutcome, WriteError> {
    let content_type = options.content_type.clone();
    if crate::manifest::is_manifest(destination) {
        // A copy never has the bytes in hand, so the source is read to check
        // them — bounded by its HEAD first, so an oversized source is refused
        // unread. The source can change between this read and the copy; the
        // `.notedthat` namespace is the credential holder's alone (D51), so
        // that is a race with oneself, not a bypass.
        let meta = storage
            .head_object(kb, source, ConditionalHeaders::default())
            .await?;
        if meta.size > crate::manifest::MANIFEST_MAX_BYTES {
            return Err(WriteError::InvalidManifest {
                message: format!(
                    "manifest is {} bytes; a manifest is at most {}",
                    meta.size,
                    crate::manifest::MANIFEST_MAX_BYTES
                ),
            });
        }
        let read = storage
            .get_object(kb, source, None, ConditionalHeaders::default())
            .await?;
        crate::manifest::check_manifest_bytes(kb, destination, &read.bytes)?;
    }
    let outcome = storage
        .copy_object(kb, source, destination, options)
        .await?;
    // A copy returns only the new ETag. The event wants the size too, and only
    // a subscriber cares, so the extra HEAD is paid only when one can exist.
    // The copy is already durable by now, so a HEAD that fails — a racing
    // delete, a transient error — degrades the stamp rather than the copy.
    let (size, mime) = if sinks.events.is_some() {
        match storage
            .head_object(kb, destination, ConditionalHeaders::default())
            .await
        {
            Ok(meta) => (meta.size, meta.content_type.or(content_type)),
            Err(error) => {
                tracing::warn!(
                    target: "notedthat::events",
                    kb = %kb, path = %destination, %error,
                    "could not HEAD the copy destination for its event; publishing without a size"
                );
                (0, content_type)
            }
        }
    } else {
        (0, content_type)
    };
    after_write(
        sinks,
        kb,
        destination,
        &outcome,
        size,
        mime.as_deref().unwrap_or_default(),
    )
    .await?;
    Ok(outcome)
}

/// Holds the `Upsert` for a write whose bytes are already stored, and enqueues
/// it from `Drop` if the normal path never got there.
///
/// Only ever fires on cancellation: [`after_write`] disarms it the moment it
/// takes the event to send properly, so the ordinary path — including every
/// queue-full and queue-closed case, which need the health view and the error
/// this cannot produce — is untouched. What it covers is the future being
/// dropped between the store and the enqueue.
struct EnqueueOnDrop {
    indexer_tx: Sender<IndexEvent>,
    event: Option<IndexEvent>,
}

impl EnqueueOnDrop {
    /// Take the event for the normal enqueue; nothing happens on drop after.
    fn disarm(&mut self) -> IndexEvent {
        self.event.take().expect("disarmed once")
    }
}

impl Drop for EnqueueOnDrop {
    fn drop(&mut self) {
        let Some(event) = self.event.take() else {
            return;
        };
        // Best effort by construction: there is no caller left to return an
        // error to, and a full queue here means the same as it does anywhere
        // else — the reconciliation pass is the backstop.
        if self.indexer_tx.try_send(event).is_err() {
            tracing::warn!(
                target: "notedthat::indexing",
                "INDEX_ENQUEUE_LOST_ON_CANCEL: a write was cancelled after its bytes were stored \
                 and its Upsert could not be queued; reconciliation will repair it"
            );
        }
    }
}

/// Report a durable write: publish, then index.
///
/// Publishing first is what lets the worker's `object.indexed` for this
/// `ETag` always follow `object.written` in the log (D65): the write is on
/// the log before its `Upsert` is even in the queue. The enqueue happens
/// whether or not the publish succeeded, so the index never depends on the
/// broker; a refused publish is still the write's answer, and the retry
/// re-announces and re-indexes. D38's promise holds either way — either
/// failure returns an error after the bytes are stored, and a retried write
/// re-runs both — so an event may be published twice, never zero times.
pub(crate) async fn after_write(
    sinks: &WriteSinks<'_>,
    kb: &KbSlug,
    path: &ObjectPath,
    outcome: &PutOutcome,
    size: u64,
    mime: &str,
) -> Result<(), WriteError> {
    // Publish first, so `object.indexed` for this `ETag` always follows the
    // `object.written` it answers (D65) — but enqueue regardless of the
    // outcome: the index is what search depends on and the broker is the
    // optional component (§5 principle 9), so a refused publish must not
    // leave stored bytes unsearchable until a client happens to retry. The
    // refusal is still the write's answer, after the enqueue; the retry then
    // announces the version and, an `Upsert` never being skipped, indexes it
    // again — an `object.indexed` with no `object.written` before it is the
    // same class of thing as a duplicate, and subscribers tolerate both.
    // Armed before the publish, disarmed by the enqueue below.
    //
    // `publish` can await for seconds (the NATS client's own timeout), and a
    // caller that gives up in that window — a client disconnect, or the
    // request timeout D71 added — drops this future where it stands. The bytes
    // are already stored by then, so without this the object would be durable
    // and unsearchable, with nothing to repair it on `s3` until the next
    // reconciliation pass. `try_send` is synchronous, so `Drop` can still do
    // it; this is the same bargain D69 strikes for metered calls, for the same
    // reason.
    let mut enqueue = EnqueueOnDrop {
        indexer_tx: sinks.indexer_tx.clone(),
        event: Some(IndexEvent::Upsert {
            kb: kb.clone(),
            object_key: path.clone(),
            etag: outcome.etag.clone().unwrap_or_default(),
            mtime: current_unix_seconds(),
        }),
    };

    let published = match sinks.events {
        Some(events) => {
            let event = ObjectEvent::written(
                kb.clone(),
                path.clone(),
                outcome.etag.clone().unwrap_or_default(),
                size,
                mime.to_string(),
                current_unix_seconds(),
                sinks.source,
            );
            publish(events, event, WriteEffect::Stored).await
        }
        None => Ok(()),
    };

    let event = enqueue.disarm();
    match sinks.indexer_tx.try_send(event) {
        Ok(()) => {
            if let Some(health) = sinks.index_health {
                health.enqueued(kb.as_str());
            }
        }
        Err(TrySendError::Full(ev)) => {
            tracing::warn!(target: "notedthat::indexing", kb = %kb, path = %path, "INDEX_QUEUE_FULL");
            let _ = ev;
            if let Some(health) = sinks.index_health {
                health.backpressured(kb.as_str());
            }
            return Err(WriteError::IndexerBackpressureUpsert);
        }
        Err(TrySendError::Closed(ev)) => {
            tracing::error!(target: "notedthat::indexing", kb = %kb, path = %path, "INDEX_QUEUE_CLOSED");
            let _ = ev;
            if let Some(health) = sinks.index_health {
                health.worker_stopped();
            }
            // Closed = indexer worker ended (shutdown OR panic). v1 preserves success-with-error-log
            // until post-v1 worker liveness detection is added.
        }
    }
    published
}

/// Delete an object idempotently, publish the change and enqueue a
/// best-effort tombstone event — in that order, the one rule `after_write`
/// follows (D65).
pub async fn commit_delete(
    storage: &dyn Storage,
    sinks: &WriteSinks<'_>,
    kb: &KbSlug,
    path: &ObjectPath,
    conditionals: ConditionalHeaders,
) -> Result<(), WriteError> {
    match storage.delete_object(kb, path, conditionals).await {
        Ok(()) | Err(StorageError::NotFound { .. }) => {}
        Err(e) => return Err(WriteError::Storage(e)),
    }

    // As in `after_write`: publish first, enqueue regardless, answer with the
    // publish's outcome — a refused publish must not leave a deleted object's
    // points searchable until a client retries.
    let published = match sinks.events {
        Some(events) => {
            let event = ObjectEvent::deleted(kb.clone(), path.clone(), sinks.source);
            publish(events, event, WriteEffect::Deleted).await
        }
        None => Ok(()),
    };

    let event = IndexEvent::Tombstone {
        kb: kb.clone(),
        object_key: path.clone(),
    };
    match sinks.indexer_tx.try_send(event) {
        Ok(()) => {
            if let Some(health) = sinks.index_health {
                health.enqueued(kb.as_str());
            }
        }
        Err(TrySendError::Full(ev)) => {
            tracing::warn!(target: "notedthat::indexing", kb = %kb, path = %path, "INDEX_QUEUE_FULL");
            let _ = ev;
            if let Some(health) = sinks.index_health {
                health.backpressured(kb.as_str());
            }
            return Err(WriteError::IndexerBackpressureTombstone);
        }
        // Closed means the indexer worker task ended via shutdown OR panic; v1 deliberately preserves success-with-error-log so writes are not blocked by a crashed worker. The health view reports the stopped worker instead (#97).
        Err(TrySendError::Closed(ev)) => {
            tracing::error!(target: "notedthat::indexing", kb = %kb, path = %path, "INDEX_QUEUE_CLOSED");
            let _ = ev;
            if let Some(health) = sinks.index_health {
                health.worker_stopped();
            }
        }
    }
    published
}

async fn publish(
    events: &dyn notedthat_core::EventPublisher,
    event: ObjectEvent,
    after: WriteEffect,
) -> Result<(), WriteError> {
    let (kb, path) = (event.kb.clone(), event.object_key.clone());
    match events.publish(event).await {
        Ok(_) => Ok(()),
        Err(error) => {
            tracing::error!(target: "notedthat::events", kb = %kb, path = %path, %error, "EVENT_PUBLISH_FAILED");
            Err(WriteError::EventPublishFailed { after })
        }
    }
}

pub(crate) fn current_unix_seconds() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| i64::try_from(d.as_secs()).unwrap_or(i64::MAX))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::WriteEffect;
    use async_trait::async_trait;
    use bytes::Bytes;
    use notedthat_core::{
        EventId, EventPublisher, EventSource, EventStream, KbManifest, ListResponse, ObjectEvent,
        ObjectMeta, ObjectRead, PublishError, SubscribeError,
    };
    use std::collections::HashMap;
    use std::sync::{Arc, Mutex};
    use tokio::sync::mpsc;

    #[derive(Default)]
    struct TestStorage {
        objects: Mutex<HashMap<String, String>>,
    }

    #[async_trait]
    impl Storage for TestStorage {
        async fn probe(&self, _kb: &KbSlug) -> Result<(), StorageError> {
            Ok(())
        }

        async fn ensure_bucket(&self, _kb: &KbSlug) -> Result<(), StorageError> {
            unimplemented!()
        }

        async fn read_manifest(&self, _kb: &KbSlug) -> Result<KbManifest, StorageError> {
            unimplemented!()
        }

        async fn write_manifest(
            &self,
            _kb: &KbSlug,
            _manifest: &KbManifest,
        ) -> Result<(), StorageError> {
            unimplemented!()
        }

        async fn head_object(
            &self,
            kb: &KbSlug,
            path: &ObjectPath,
            _conditionals: ConditionalHeaders,
        ) -> Result<ObjectMeta, StorageError> {
            let key = format!("{}/{}", kb.as_str(), path.as_str());
            let objects = self.objects.lock().expect("mutex not poisoned");
            let etag = objects
                .get(&key)
                .ok_or_else(|| StorageError::NotFound { key: key.clone() })?;
            Ok(ObjectMeta {
                key: path.as_str().to_string(),
                size: 7,
                last_modified: Some(1_700_000_000),
                content_type: Some("text/markdown".into()),
                etag: Some(etag.clone()),
            })
        }

        async fn get_object(
            &self,
            _kb: &KbSlug,
            _path: &ObjectPath,
            _range: Option<notedthat_core::ByteRange>,
            _conditionals: ConditionalHeaders,
        ) -> Result<ObjectRead, StorageError> {
            unimplemented!()
        }

        async fn get_object_stream(
            &self,
            _kb: &KbSlug,
            _path: &ObjectPath,
            _range: Option<notedthat_core::ByteRange>,
            _conditionals: ConditionalHeaders,
        ) -> Result<notedthat_core::ObjectStream, StorageError> {
            unimplemented!()
        }

        async fn put_object(
            &self,
            kb: &KbSlug,
            path: &ObjectPath,
            _bytes: Bytes,
            _content_type: Option<&str>,
            conditionals: ConditionalHeaders,
        ) -> Result<PutOutcome, StorageError> {
            let key = format!("{}/{}", kb.as_str(), path.as_str());
            let mut objects = self.objects.lock().expect("mutex not poisoned");
            let existing = objects.get(&key);
            if let Some(if_match) = conditionals.if_match
                && existing.is_none_or(|etag| etag != &if_match)
            {
                return Err(StorageError::PreconditionFailed);
            }

            let etag = format!("\"etag-{}\"", objects.len() + 1);
            objects.insert(key, etag.clone());
            Ok(PutOutcome { etag: Some(etag) })
        }

        async fn put_staged_object(
            &self,
            kb: &KbSlug,
            path: &ObjectPath,
            body: StagedBody,
            content_type: Option<&str>,
            conditionals: ConditionalHeaders,
        ) -> Result<PutOutcome, StorageError> {
            let bytes =
                body.memory_bytes()
                    .cloned()
                    .ok_or_else(|| StorageError::BackendUnavailable {
                        message: "file staging is outside this commit unit test".into(),
                    })?;
            self.put_object(kb, path, bytes, content_type, conditionals)
                .await
        }

        async fn copy_object(
            &self,
            kb: &KbSlug,
            _source: &ObjectPath,
            destination: &ObjectPath,
            _options: CopyObjectOptions,
        ) -> Result<PutOutcome, StorageError> {
            let key = format!("{}/{}", kb.as_str(), destination.as_str());
            let mut objects = self.objects.lock().expect("mutex not poisoned");
            let etag = format!("\"etag-{}\"", objects.len() + 1);
            objects.insert(key, etag.clone());
            Ok(PutOutcome { etag: Some(etag) })
        }

        async fn delete_object(
            &self,
            kb: &KbSlug,
            path: &ObjectPath,
            _conditionals: ConditionalHeaders,
        ) -> Result<(), StorageError> {
            let key = format!("{}/{}", kb.as_str(), path.as_str());
            self.objects
                .lock()
                .expect("mutex not poisoned")
                .remove(&key);
            Ok(())
        }

        async fn list_objects(
            &self,
            _kb: &KbSlug,
            _prefix: Option<&str>,
            _limit: u32,
            _cursor: Option<&str>,
        ) -> Result<ListResponse, StorageError> {
            unimplemented!()
        }
    }

    fn kb() -> KbSlug {
        KbSlug::try_new("test-kb").expect("valid kb slug")
    }

    fn path() -> ObjectPath {
        ObjectPath::try_from_str("test.md").expect("valid path")
    }

    fn path_named(value: &str) -> ObjectPath {
        ObjectPath::try_from_str(value).expect("valid path")
    }

    #[tokio::test]
    async fn successful_put_enqueues_event() {
        let storage = TestStorage::default();
        let kb = kb();
        let path = path();
        let (indexer_tx, mut rx) = mpsc::channel(1024);

        let outcome = commit(
            &storage,
            &WriteSinks::indexer_only(&indexer_tx),
            &kb,
            &path,
            Bytes::from_static(b"# Test"),
            Some("text/markdown"),
            ConditionalHeaders::default(),
        )
        .await;

        assert!(outcome.is_ok());
        assert!(outcome.unwrap().etag.is_some());

        let event = rx.recv().await.expect("event should be enqueued");
        assert_eq!(event.kb().as_str(), "test-kb");
        assert_eq!(event.object_key().as_str(), "test.md");
    }

    #[tokio::test]
    async fn successful_native_copy_enqueues_destination_event() {
        let storage = TestStorage::default();
        let kb = kb();
        let source = path_named("source.md");
        let destination = path_named("destination.md");
        let (indexer_tx, mut rx) = mpsc::channel(1);

        let outcome = commit_copy(
            &storage,
            &WriteSinks::indexer_only(&indexer_tx),
            &kb,
            &source,
            &destination,
            CopyObjectOptions::default(),
        )
        .await
        .expect("copy succeeds");

        assert!(outcome.etag.is_some());
        let event = rx.recv().await.expect("destination event");
        assert_eq!(event.object_key(), &destination);
    }

    #[tokio::test]
    async fn full_queue_returns_indexer_backpressure() {
        let storage = TestStorage::default();
        let kb = kb();
        let path = path();
        let (indexer_tx, _rx) = mpsc::channel(1);

        let dummy_event = IndexEvent::Upsert {
            kb: kb.clone(),
            object_key: path.clone(),
            etag: "dummy".to_string(),
            mtime: 0,
        };
        indexer_tx
            .try_send(dummy_event)
            .expect("first send should succeed");

        let outcome = commit(
            &storage,
            &WriteSinks::indexer_only(&indexer_tx),
            &kb,
            &path,
            Bytes::from_static(b"# Test"),
            Some("text/markdown"),
            ConditionalHeaders::default(),
        )
        .await;

        let err = outcome.unwrap_err();
        assert!(
            matches!(err, WriteError::IndexerBackpressureUpsert),
            "expected IndexerBackpressureUpsert, got {err:?}"
        );
    }

    #[tokio::test]
    async fn burst_write_returns_backpressure_after_capacity() {
        let storage = Arc::new(TestStorage::default());
        let kb = kb();
        let path_a = path_named("a.md");
        let path_b = path_named("b.md");
        let path_c = path_named("c.md");
        let (indexer_tx, _rx) = mpsc::channel::<IndexEvent>(2);

        let first = commit(
            storage.as_ref(),
            &WriteSinks::indexer_only(&indexer_tx),
            &kb,
            &path_a,
            Bytes::from_static(b"# A"),
            Some("text/markdown"),
            ConditionalHeaders::default(),
        )
        .await;
        let second = commit(
            storage.as_ref(),
            &WriteSinks::indexer_only(&indexer_tx),
            &kb,
            &path_b,
            Bytes::from_static(b"# B"),
            Some("text/markdown"),
            ConditionalHeaders::default(),
        )
        .await;
        let third = commit(
            storage.as_ref(),
            &WriteSinks::indexer_only(&indexer_tx),
            &kb,
            &path_c,
            Bytes::from_static(b"# C"),
            Some("text/markdown"),
            ConditionalHeaders::default(),
        )
        .await;

        assert!(first.is_ok(), "first write should fill queue slot one");
        assert!(second.is_ok(), "second write should fill queue slot two");
        let err = third.unwrap_err();
        assert!(
            matches!(err, WriteError::IndexerBackpressureUpsert),
            "expected IndexerBackpressureUpsert, got {err:?}"
        );
        assert!(
            storage
                .objects
                .lock()
                .expect("mutex not poisoned")
                .contains_key("test-kb/c.md"),
            "stored object should remain after enqueue backpressure"
        );
    }

    /// A write cancelled between storing the bytes and enqueuing the `Upsert`
    /// must still enqueue it. The window is `publish`, which can await for
    /// seconds against a slow or unreachable broker, and D71's request timeout
    /// drops the handler where it stands — leaving, without the guard, an
    /// object that is durable and unsearchable with nothing on `s3` to repair
    /// it until the next reconciliation pass.
    #[tokio::test]
    async fn a_write_cancelled_while_publishing_still_enqueues_its_upsert() {
        /// A broker that accepts the connection and then never answers.
        struct NeverAnswers;

        #[async_trait::async_trait]
        impl EventPublisher for NeverAnswers {
            async fn publish(&self, _event: ObjectEvent) -> Result<EventId, PublishError> {
                std::future::pending().await
            }
            async fn subscribe(
                &self,
                _kb: &KbSlug,
                _after: Option<EventId>,
            ) -> Result<EventStream, SubscribeError> {
                unreachable!("this test never subscribes")
            }
            fn ready(&self) -> bool {
                true
            }
            fn backend_name(&self) -> &'static str {
                "never-answers"
            }
        }

        let storage = TestStorage::default();
        let kb = kb();
        let path = path_named("cancelled.md");
        let (indexer_tx, mut rx) = mpsc::channel(4);
        let events = NeverAnswers;
        let sinks = WriteSinks {
            indexer_tx: &indexer_tx,
            events: Some(&events),
            index_health: None,
            source: EventSource::Http,
        };

        // Given a commit whose publish never returns, when the caller gives up
        // on it — exactly what the request timeout does.
        let cancelled = tokio::time::timeout(
            std::time::Duration::from_millis(50),
            commit(
                &storage,
                &sinks,
                &kb,
                &path,
                Bytes::from_static(b"# Cancelled"),
                Some("text/markdown"),
                ConditionalHeaders::default(),
            ),
        )
        .await;
        assert!(cancelled.is_err(), "the publish should never have returned");

        // Then: the bytes are stored …
        assert!(
            storage
                .head_object(&kb, &path, ConditionalHeaders::default())
                .await
                .is_ok(),
            "the store completed before the publish that was cancelled"
        );
        // … and the index was told about them anyway.
        match rx.try_recv().expect("an Upsert must have been enqueued") {
            IndexEvent::Upsert { object_key, .. } => assert_eq!(object_key, path),
            other => panic!("expected an Upsert, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn commit_delete_full_queue_returns_backpressure() {
        let storage = TestStorage::default();
        let kb = kb();
        let path = path_named("delete.md");
        storage
            .put_object(
                &kb,
                &path,
                Bytes::from_static(b"# Delete"),
                Some("text/markdown"),
                ConditionalHeaders::default(),
            )
            .await
            .expect("prepopulate object");
        let (indexer_tx, _rx) = mpsc::channel(1);
        let dummy_event = IndexEvent::Tombstone {
            kb: kb.clone(),
            object_key: path.clone(),
        };
        indexer_tx
            .try_send(dummy_event)
            .expect("first send should succeed");

        let outcome = commit_delete(
            &storage,
            &WriteSinks::indexer_only(&indexer_tx),
            &kb,
            &path,
            ConditionalHeaders::default(),
        )
        .await;

        let err = outcome.unwrap_err();
        assert!(
            matches!(err, WriteError::IndexerBackpressureTombstone),
            "expected IndexerBackpressureTombstone, got {err:?}"
        );
        assert!(
            !storage
                .objects
                .lock()
                .expect("mutex not poisoned")
                .contains_key("test-kb/delete.md"),
            "deleted object should remain deleted after enqueue backpressure"
        );
    }

    #[tokio::test]
    async fn closed_queue_returns_write_success() {
        let storage = TestStorage::default();
        let kb = kb();
        let path = path();
        let (indexer_tx, rx) = mpsc::channel(1024);

        drop(rx);

        let outcome = commit(
            &storage,
            &WriteSinks::indexer_only(&indexer_tx),
            &kb,
            &path,
            Bytes::from_static(b"# Test"),
            Some("text/markdown"),
            ConditionalHeaders::default(),
        )
        .await;

        assert!(
            outcome.is_ok(),
            "write should succeed even if queue is closed"
        );
    }

    #[tokio::test]
    async fn put_failure_returns_error_no_event() {
        let storage = TestStorage::default();
        let kb = kb();
        let path = path();
        let (indexer_tx, mut rx) = mpsc::channel(1024);

        let conditionals = ConditionalHeaders {
            if_match: Some("\"wrong-etag\"".to_string()),
            ..ConditionalHeaders::default()
        };

        let outcome = commit(
            &storage,
            &WriteSinks::indexer_only(&indexer_tx),
            &kb,
            &path,
            Bytes::from_static(b"# Test"),
            Some("text/markdown"),
            conditionals,
        )
        .await;

        assert!(outcome.is_err(), "put should fail with precondition");
        assert!(
            rx.try_recv().is_err(),
            "no event should be enqueued on put failure"
        );
    }

    #[test]
    fn check_size_over_limit_returns_too_large() {
        let err = check_size(MAX_UPLOAD_BYTES + 1, MAX_UPLOAD_BYTES).expect_err("too large");
        assert!(matches!(
            err,
            WriteError::TooLarge {
                size,
                limit
            } if size == MAX_UPLOAD_BYTES + 1 && limit == MAX_UPLOAD_BYTES
        ));
    }

    #[test]
    fn check_size_at_limit_returns_ok() {
        assert!(check_size(MAX_UPLOAD_BYTES, MAX_UPLOAD_BYTES).is_ok());
    }

    #[test]
    fn check_size_below_limit_returns_ok() {
        assert!(check_size(1024, MAX_UPLOAD_BYTES).is_ok());
    }

    /// Records what was published, or refuses everything.
    struct RecordingPublisher {
        published: Mutex<Vec<ObjectEvent>>,
        refuse: bool,
    }

    impl RecordingPublisher {
        fn recording() -> Self {
            Self {
                published: Mutex::new(Vec::new()),
                refuse: false,
            }
        }

        fn refusing() -> Self {
            Self {
                published: Mutex::new(Vec::new()),
                refuse: true,
            }
        }

        fn events(&self) -> Vec<ObjectEvent> {
            self.published.lock().expect("mutex not poisoned").clone()
        }
    }

    #[async_trait]
    impl notedthat_core::EventPublisher for RecordingPublisher {
        async fn publish(
            &self,
            event: ObjectEvent,
        ) -> Result<notedthat_core::EventId, notedthat_core::PublishError> {
            if self.refuse {
                return Err(notedthat_core::PublishError::Unavailable {
                    message: "broker down".into(),
                });
            }
            let mut published = self.published.lock().expect("mutex not poisoned");
            published.push(event);
            Ok(notedthat_core::EventId(published.len() as u64))
        }

        async fn subscribe(
            &self,
            _kb: &KbSlug,
            _after: Option<notedthat_core::EventId>,
        ) -> Result<notedthat_core::EventStream, notedthat_core::SubscribeError> {
            unimplemented!()
        }

        fn ready(&self) -> bool {
            true
        }

        fn backend_name(&self) -> &'static str {
            "recording"
        }
    }

    fn sinks<'a>(
        indexer_tx: &'a mpsc::Sender<IndexEvent>,
        events: &'a RecordingPublisher,
        source: notedthat_core::EventSource,
    ) -> WriteSinks<'a> {
        WriteSinks {
            indexer_tx,
            events: Some(events),
            index_health: None,
            source,
        }
    }

    #[tokio::test]
    async fn a_committed_write_is_published_with_its_stamp_and_source() {
        let storage = TestStorage::default();
        let events = RecordingPublisher::recording();
        let (indexer_tx, mut rx) = mpsc::channel(8);

        let outcome = commit(
            &storage,
            &sinks(&indexer_tx, &events, notedthat_core::EventSource::Webdav),
            &kb(),
            &path(),
            Bytes::from_static(b"# Test"),
            Some("text/markdown"),
            ConditionalHeaders::default(),
        )
        .await
        .expect("write succeeds");

        rx.recv().await.expect("the index event, after the publish");
        let published = events.events();
        assert_eq!(published.len(), 1);
        let event = &published[0];
        assert_eq!(event.kb.as_str(), "test-kb");
        assert_eq!(event.object_key.as_str(), "test.md");
        assert_eq!(event.source, notedthat_core::EventSource::Webdav);
        assert_eq!(
            event.kind,
            notedthat_core::ObjectEventKind::Written {
                etag: outcome.etag.expect("etag"),
                size: 6,
                mime: "text/markdown".into(),
                mtime: match &event.kind {
                    notedthat_core::ObjectEventKind::Written { mtime, .. } => *mtime,
                    other => unreachable!("a write publishes object.written, got {other:?}"),
                },
            }
        );
    }

    /// The event goes out before the queue is tried, so a write the queue
    /// refuses has been announced; the client's retry (D38) announces it
    /// again — at least once, never zero times.
    #[tokio::test]
    async fn a_full_queue_still_refuses_the_write_after_its_event_was_published() {
        let storage = TestStorage::default();
        let events = RecordingPublisher::recording();
        let (indexer_tx, _rx) = mpsc::channel(1);
        indexer_tx
            .try_send(IndexEvent::Tombstone {
                kb: kb(),
                object_key: path(),
            })
            .expect("fill the queue");

        let err = commit(
            &storage,
            &sinks(&indexer_tx, &events, notedthat_core::EventSource::Http),
            &kb(),
            &path(),
            Bytes::from_static(b"# Test"),
            Some("text/markdown"),
            ConditionalHeaders::default(),
        )
        .await
        .unwrap_err();

        assert!(matches!(err, WriteError::IndexerBackpressureUpsert));
        let published = events.events();
        assert_eq!(
            published.len(),
            1,
            "announced before the 503: {published:?}"
        );
        assert_eq!(published[0].kind.name(), "object.written");
    }

    #[tokio::test]
    async fn a_refused_publish_fails_the_write_but_the_object_stays_stored() {
        let storage = TestStorage::default();
        let events = RecordingPublisher::refusing();
        let (indexer_tx, mut rx) = mpsc::channel(8);

        let err = commit(
            &storage,
            &sinks(&indexer_tx, &events, notedthat_core::EventSource::Http),
            &kb(),
            &path(),
            Bytes::from_static(b"# Test"),
            Some("text/markdown"),
            ConditionalHeaders::default(),
        )
        .await
        .unwrap_err();

        assert!(
            matches!(
                err,
                WriteError::EventPublishFailed {
                    after: WriteEffect::Stored
                }
            ),
            "{err:?}"
        );
        assert!(
            storage
                .objects
                .lock()
                .expect("mutex not poisoned")
                .contains_key("test-kb/test.md"),
            "the bytes were stored before publishing failed"
        );
        assert!(
            matches!(rx.try_recv(), Ok(IndexEvent::Upsert { .. })),
            "the index work was enqueued regardless: the broker is optional, the index is not"
        );
    }

    #[tokio::test]
    async fn a_refused_publish_still_enqueues_the_tombstone() {
        let storage = TestStorage::default();
        let events = RecordingPublisher::refusing();
        let (indexer_tx, mut rx) = mpsc::channel(8);

        let err = commit_delete(
            &storage,
            &sinks(&indexer_tx, &events, notedthat_core::EventSource::Http),
            &kb(),
            &path_named("gone.md"),
            ConditionalHeaders::default(),
        )
        .await
        .unwrap_err();

        assert!(
            matches!(
                err,
                WriteError::EventPublishFailed {
                    after: WriteEffect::Deleted
                }
            ),
            "{err:?}"
        );
        assert!(
            matches!(rx.try_recv(), Ok(IndexEvent::Tombstone { .. })),
            "the points come out of the index whether or not the deletion was announced"
        );
    }

    #[tokio::test]
    async fn a_delete_publishes_a_deleted_event_and_a_refusal_says_deleted() {
        let storage = TestStorage::default();
        let (indexer_tx, _rx) = mpsc::channel(8);

        let events = RecordingPublisher::recording();
        commit_delete(
            &storage,
            &sinks(&indexer_tx, &events, notedthat_core::EventSource::Mcp),
            &kb(),
            &path_named("gone.md"),
            ConditionalHeaders::default(),
        )
        .await
        .expect("delete succeeds");
        let published = events.events();
        assert_eq!(published.len(), 1);
        assert_eq!(published[0].kind, notedthat_core::ObjectEventKind::Deleted);
        assert_eq!(published[0].source, notedthat_core::EventSource::Mcp);

        let refusing = RecordingPublisher::refusing();
        let err = commit_delete(
            &storage,
            &sinks(&indexer_tx, &refusing, notedthat_core::EventSource::Http),
            &kb(),
            &path_named("gone.md"),
            ConditionalHeaders::default(),
        )
        .await
        .unwrap_err();
        assert!(matches!(
            err,
            WriteError::EventPublishFailed {
                after: WriteEffect::Deleted
            }
        ));
    }

    #[tokio::test]
    async fn a_copy_heads_the_destination_for_the_stamp_only_when_publishing() {
        let storage = TestStorage::default();
        let (indexer_tx, _rx) = mpsc::channel(8);
        let events = RecordingPublisher::recording();

        let outcome = commit_copy(
            &storage,
            &sinks(&indexer_tx, &events, notedthat_core::EventSource::Webdav),
            &kb(),
            &path_named("src.md"),
            &path_named("dst.md"),
            CopyObjectOptions::default(),
        )
        .await
        .expect("copy succeeds");

        let published = events.events();
        assert_eq!(published.len(), 1);
        assert_eq!(published[0].object_key.as_str(), "dst.md");
        match &published[0].kind {
            notedthat_core::ObjectEventKind::Written {
                etag, size, mime, ..
            } => {
                assert_eq!(Some(etag), outcome.etag.as_ref());
                assert_eq!(*size, 7, "size comes from the HEAD after the copy");
                assert_eq!(mime, "text/markdown");
            }
            other => panic!("a copy is a write, got {other:?}"),
        }
    }
}