arete-server 0.29.0

WebSocket server and projection handlers for Arete streaming pipelines
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
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::time::Duration;
use tokio::sync::{mpsc, oneshot};
use tokio::time::{interval, Instant, MissedTickBehavior};
use tracing::{debug, error, warn};
use uuid::Uuid;

const MAX_IN_MEMORY_RETRIES: u32 = 3;

/// Billing and policy identity copied from one verified session token. The
/// legacy fields remain present during the compatibility window; V2 fields
/// are emitted only as a complete signed tuple.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct UsageIdentity {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metering_key: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subject: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub key_class: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub actor_key: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub account_key: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub consumer_key: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub plan_code: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub policy_version: Option<u32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum WebSocketUsageEvent {
    ConnectionEstablished {
        client_id: String,
        remote_addr: String,
        deployment_id: Option<String>,
        #[serde(flatten)]
        identity: UsageIdentity,
    },
    ConnectionClosed {
        client_id: String,
        deployment_id: Option<String>,
        #[serde(flatten)]
        identity: UsageIdentity,
        duration_secs: Option<f64>,
        subscription_count: u32,
    },
    SubscriptionCreated {
        client_id: String,
        deployment_id: Option<String>,
        #[serde(flatten)]
        identity: UsageIdentity,
        view_id: String,
    },
    SubscriptionRemoved {
        client_id: String,
        deployment_id: Option<String>,
        #[serde(flatten)]
        identity: UsageIdentity,
        view_id: String,
    },
    SnapshotSent {
        client_id: String,
        deployment_id: Option<String>,
        #[serde(flatten)]
        identity: UsageIdentity,
        view_id: String,
        rows: u32,
        messages: u32,
        bytes: u64,
    },
    UpdateSent {
        client_id: String,
        deployment_id: Option<String>,
        #[serde(flatten)]
        identity: UsageIdentity,
        view_id: String,
        messages: u32,
        bytes: u64,
    },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebSocketUsageEnvelope {
    pub event_id: String,
    pub occurred_at_ms: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub build_id: Option<String>,
    pub event: WebSocketUsageEvent,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebSocketUsageBatch {
    pub events: Vec<WebSocketUsageEnvelope>,
}

#[async_trait]
pub trait WebSocketUsageEmitter: Send + Sync {
    async fn emit(&self, event: WebSocketUsageEvent);

    /// Finish or durably preserve every event accepted before this call while
    /// leaving the emitter available for later events.
    async fn flush(&self) {}

    /// Finish or durably preserve every event accepted before this call.
    async fn shutdown(&self) {
        self.flush().await;
    }
}

#[derive(Clone)]
pub struct ChannelUsageEmitter {
    sender: mpsc::UnboundedSender<WebSocketUsageEvent>,
}

impl ChannelUsageEmitter {
    pub fn new(sender: mpsc::UnboundedSender<WebSocketUsageEvent>) -> Self {
        Self { sender }
    }
}

#[async_trait]
impl WebSocketUsageEmitter for ChannelUsageEmitter {
    async fn emit(&self, event: WebSocketUsageEvent) {
        let _ = self.sender.send(event);
    }
}

pub struct HttpUsageEmitter {
    sender: mpsc::UnboundedSender<UsageEmitterCommand>,
}

enum UsageEmitterCommand {
    Event(Box<WebSocketUsageEvent>),
    Flush(oneshot::Sender<()>),
    Shutdown(oneshot::Sender<()>),
}

#[derive(Debug, thiserror::Error)]
#[error("WebSocket usage build ID must be a positive integer")]
pub struct InvalidUsageBuildId;

#[derive(Debug, Clone)]
struct RetryState {
    batch: WebSocketUsageBatch,
    attempts: u32,
    next_retry_at: Instant,
}

impl HttpUsageEmitter {
    pub fn new(endpoint: String, auth_token: Option<String>) -> Self {
        Self::with_config(endpoint, auth_token, 50, Duration::from_secs(2))
    }

    pub fn new_attributed(
        endpoint: String,
        auth_token: Option<String>,
        build_id: impl Into<String>,
    ) -> Result<Self, InvalidUsageBuildId> {
        let build_id = validate_build_id(build_id.into())?;
        Ok(Self::with_full_config(
            endpoint,
            auth_token,
            50,
            Duration::from_secs(2),
            None,
            Some(build_id),
        ))
    }

    pub fn with_spool_dir(
        endpoint: String,
        auth_token: Option<String>,
        spool_dir: impl Into<PathBuf>,
    ) -> Self {
        Self::with_full_config(
            endpoint,
            auth_token,
            50,
            Duration::from_secs(2),
            Some(spool_dir.into()),
            None,
        )
    }

    pub fn with_attributed_spool_dir(
        endpoint: String,
        auth_token: Option<String>,
        spool_dir: impl Into<PathBuf>,
        build_id: impl Into<String>,
    ) -> Result<Self, InvalidUsageBuildId> {
        let build_id = validate_build_id(build_id.into())?;
        Ok(Self::with_full_config(
            endpoint,
            auth_token,
            50,
            Duration::from_secs(2),
            Some(spool_dir.into()),
            Some(build_id),
        ))
    }

    pub fn with_config(
        endpoint: String,
        auth_token: Option<String>,
        batch_size: usize,
        flush_interval: Duration,
    ) -> Self {
        Self::with_full_config(endpoint, auth_token, batch_size, flush_interval, None, None)
    }

    fn with_full_config(
        endpoint: String,
        auth_token: Option<String>,
        batch_size: usize,
        flush_interval: Duration,
        spool_dir: Option<PathBuf>,
        build_id: Option<String>,
    ) -> Self {
        let (sender, mut receiver) = mpsc::unbounded_channel::<UsageEmitterCommand>();
        let client = reqwest::Client::new();

        tokio::spawn(async move {
            let mut ticker = interval(flush_interval);
            ticker.set_missed_tick_behavior(MissedTickBehavior::Delay);
            let mut pending: Vec<WebSocketUsageEnvelope> = Vec::new();
            let mut retry_state: Option<RetryState> = None;

            if let Some(dir) = spool_dir.as_ref() {
                if let Err(error) = ensure_spool_dir(dir) {
                    warn!(error = %error, path = %dir.display(), "failed to initialize websocket usage spool directory");
                }
            }

            loop {
                tokio::select! {
                    maybe_command = receiver.recv() => {
                        match maybe_command {
                            Some(UsageEmitterCommand::Event(event)) => {
                                pending.push(WebSocketUsageEnvelope {
                                    event_id: Uuid::new_v4().to_string(),
                                    occurred_at_ms: current_time_ms(),
                                    build_id: build_id.clone(),
                                    event: *event,
                                });

                                if retry_state.is_none() && pending.len() >= batch_size {
                                    flush_pending_batch(
                                        &client,
                                        &endpoint,
                                        auth_token.as_deref(),
                                        &mut pending,
                                        &mut retry_state,
                                        spool_dir.as_deref(),
                                        batch_size,
                                    ).await;
                                }
                            }
                            Some(UsageEmitterCommand::Flush(done)) => {
                                flush_on_shutdown(
                                    &client,
                                    &endpoint,
                                    auth_token.as_deref(),
                                    &mut pending,
                                    &mut retry_state,
                                    LifecycleFlushOptions {
                                        spool_dir: spool_dir.as_deref(),
                                        batch_size,
                                        preserve_failed_for_retry: true,
                                    },
                                ).await;
                                let _ = done.send(());
                            }
                            Some(UsageEmitterCommand::Shutdown(done)) => {
                                flush_on_shutdown(
                                    &client,
                                    &endpoint,
                                    auth_token.as_deref(),
                                    &mut pending,
                                    &mut retry_state,
                                    LifecycleFlushOptions {
                                        spool_dir: spool_dir.as_deref(),
                                        batch_size,
                                        preserve_failed_for_retry: false,
                                    },
                                ).await;
                                let _ = done.send(());
                                break;
                            }
                            None => {
                                flush_on_shutdown(
                                    &client,
                                    &endpoint,
                                    auth_token.as_deref(),
                                    &mut pending,
                                    &mut retry_state,
                                    LifecycleFlushOptions {
                                        spool_dir: spool_dir.as_deref(),
                                        batch_size,
                                        preserve_failed_for_retry: false,
                                    },
                                ).await;
                                break;
                            }
                        }
                    }
                    _ = ticker.tick() => {
                        if let Some(dir) = spool_dir.as_deref() {
                            if retry_state.is_none() {
                                if let Err(error) = flush_one_spooled_batch(
                                    &client,
                                    &endpoint,
                                    auth_token.as_deref(),
                                    dir,
                                    batch_size,
                                ).await {
                                    warn!(error = %error, path = %dir.display(), "failed to process spooled websocket usage batch");
                                }
                            }
                        }

                        if let Some(state) = retry_state.take() {
                            if Instant::now() >= state.next_retry_at {
                                match flush_existing_batch(
                                    &client,
                                    &endpoint,
                                    auth_token.as_deref(),
                                    state,
                                ).await {
                                    Ok(()) => {
                                        if !pending.is_empty() {
                                            flush_pending_batch(
                                                &client,
                                                &endpoint,
                                                auth_token.as_deref(),
                                                &mut pending,
                                                &mut retry_state,
                                                spool_dir.as_deref(),
                                                batch_size,
                                            ).await;
                                        }
                                    }
                                    Err(state) => {
                                        if state.attempts >= MAX_IN_MEMORY_RETRIES {
                                            if let Some(dir) = spool_dir.as_deref() {
                                                if let Err(error) = spool_retry_state(dir, &state) {
                                                    warn!(error = %error, count = state.batch.events.len(), "failed to spool websocket usage batch after retries");
                                                    retry_state = Some(state);
                                                }
                                            } else {
                                                retry_state = Some(state);
                                            }
                                        } else {
                                            retry_state = Some(state)
                                        }
                                    }
                                }
                            } else {
                                retry_state = Some(state);
                            }
                        } else if !pending.is_empty() {
                            flush_pending_batch(
                                &client,
                                &endpoint,
                                auth_token.as_deref(),
                                &mut pending,
                                &mut retry_state,
                                spool_dir.as_deref(),
                                batch_size,
                            ).await;
                        }
                    }
                }
            }
        });

        Self { sender }
    }
}

fn validate_build_id(value: String) -> Result<String, InvalidUsageBuildId> {
    if value.trim() != value || value.parse::<i32>().ok().is_none_or(|value| value <= 0) {
        return Err(InvalidUsageBuildId);
    }
    Ok(value)
}

#[async_trait]
impl WebSocketUsageEmitter for HttpUsageEmitter {
    async fn emit(&self, event: WebSocketUsageEvent) {
        if let Err(error) = self
            .sender
            .send(UsageEmitterCommand::Event(Box::new(event)))
        {
            warn!(error = %error, "failed to queue websocket usage event");
        }
    }

    async fn flush(&self) {
        let (done, completed) = oneshot::channel();
        if self.sender.send(UsageEmitterCommand::Flush(done)).is_ok() {
            let _ = completed.await;
        }
    }

    async fn shutdown(&self) {
        let (done, completed) = oneshot::channel();
        if self
            .sender
            .send(UsageEmitterCommand::Shutdown(done))
            .is_ok()
        {
            let _ = completed.await;
        }
    }
}

fn current_time_ms() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis() as u64
}

async fn flush_batch(
    client: &reqwest::Client,
    endpoint: &str,
    auth_token: Option<&str>,
    batch: &WebSocketUsageBatch,
) -> bool {
    if batch.events.is_empty() {
        return true;
    }

    let mut request = client.post(endpoint).json(batch);
    if let Some(token) = auth_token {
        request = request.header("Authorization", format!("Bearer {}", token));
    }

    match request.send().await {
        Ok(response) if response.status().is_success() => {
            debug!(count = batch.events.len(), "flushed websocket usage batch");
            true
        }
        Ok(response) => {
            error!(status = %response.status(), count = batch.events.len(), "failed to ingest websocket usage batch");
            false
        }
        Err(error) => {
            error!(error = %error, count = batch.events.len(), "failed to post websocket usage batch");
            false
        }
    }
}

async fn flush_pending_batch(
    client: &reqwest::Client,
    endpoint: &str,
    auth_token: Option<&str>,
    pending: &mut Vec<WebSocketUsageEnvelope>,
    retry_state: &mut Option<RetryState>,
    spool_dir: Option<&Path>,
    batch_size: usize,
) {
    let batch = take_pending_batch(pending, batch_size);

    if !flush_batch(client, endpoint, auth_token, &batch).await {
        let state = RetryState {
            batch,
            attempts: 1,
            next_retry_at: Instant::now() + retry_delay(1),
        };

        if let Some(dir) = spool_dir.filter(|_| MAX_IN_MEMORY_RETRIES <= 1) {
            if let Err(error) = spool_retry_state(dir, &state) {
                warn!(error = %error, count = state.batch.events.len(), "failed to spool websocket usage batch after first failure");
                *retry_state = Some(state);
            }
        } else {
            *retry_state = Some(state);
        }
    }
}

/// Exhaust every bounded in-memory batch for a flush barrier or shutdown.
///
/// A recovered retry can leave several batches queued behind it. Shutdown must
/// continue after that retry succeeds instead of sending only one more chunk
/// and dropping the rest when no spool directory is configured. On the first
/// failed shutdown retry, preserve the failed and not-yet-attempted batches on
/// disk when possible. A flush barrier that cannot persist its events keeps
/// the failed batch in memory because the worker remains alive to retry it.
struct LifecycleFlushOptions<'a> {
    spool_dir: Option<&'a Path>,
    batch_size: usize,
    preserve_failed_for_retry: bool,
}

async fn flush_on_shutdown(
    client: &reqwest::Client,
    endpoint: &str,
    auth_token: Option<&str>,
    pending: &mut Vec<WebSocketUsageEnvelope>,
    retry_state: &mut Option<RetryState>,
    options: LifecycleFlushOptions<'_>,
) {
    if let Some(dir) = options.spool_dir {
        // The runtime explicitly awaits this shutdown path, but process-level
        // termination still has a bounded grace period. Persist first so no
        // accepted event depends on completing a sequence of HTTP requests.
        if let Some(state) = retry_state.take() {
            if let Err(error) = spool_retry_state(dir, &state) {
                warn!(error = %error, count = state.batch.events.len(), "failed to spool websocket usage retry during shutdown");
                if !flush_batch(client, endpoint, auth_token, &state.batch).await {
                    if options.preserve_failed_for_retry {
                        warn!(
                            count = state.batch.events.len(),
                            attempts = state.attempts,
                            "websocket usage flush failed; retaining batch for retry"
                        );
                        *retry_state = Some(state);
                        return;
                    }
                    error!(
                        count = state.batch.events.len(),
                        "failed to deliver websocket usage retry after shutdown spool failure"
                    );
                }
            }
        }
        while !pending.is_empty() {
            let batch = take_pending_batch(pending, options.batch_size);
            if let Err(error) = spool_batch(dir, &batch) {
                warn!(error = %error, count = batch.events.len(), "failed to spool pending websocket usage batch during shutdown");
                if !flush_batch(client, endpoint, auth_token, &batch).await {
                    if options.preserve_failed_for_retry {
                        let state = RetryState {
                            batch,
                            attempts: 1,
                            next_retry_at: Instant::now() + retry_delay(1),
                        };
                        warn!(
                            count = state.batch.events.len(),
                            "websocket usage flush failed; retaining batch for retry"
                        );
                        *retry_state = Some(state);
                        return;
                    }
                    error!(
                        count = batch.events.len(),
                        "failed to deliver pending websocket usage after shutdown spool failure"
                    );
                }
            }
        }
        return;
    }

    loop {
        if let Some(state) = retry_state.take() {
            if let Err(retry_state_failed) =
                flush_existing_batch(client, endpoint, auth_token, state).await
            {
                if options.preserve_failed_for_retry {
                    warn!(
                        count = retry_state_failed.batch.events.len(),
                        attempts = retry_state_failed.attempts,
                        "websocket usage flush failed; retaining batch for retry"
                    );
                    *retry_state = Some(retry_state_failed);
                    return;
                }
                warn!(
                    count = retry_state_failed.batch.events.len(),
                    attempts = retry_state_failed.attempts,
                    "dropping websocket usage batch during shutdown after failed retry"
                );
                break;
            }
            continue;
        }

        if pending.is_empty() {
            return;
        }

        flush_pending_batch(
            client,
            endpoint,
            auth_token,
            pending,
            retry_state,
            None,
            options.batch_size,
        )
        .await;
    }

    if !pending.is_empty() {
        warn!(
            count = pending.len(),
            "dropping pending websocket usage events during shutdown without spool directory"
        );
    }
}

/// Remove at most one configured HTTP batch from the pending queue.
///
/// A failed request can leave the emitter retrying while new events continue
/// to arrive. Draining the entire pending queue after recovery would turn that
/// backlog into one unbounded request and can permanently wedge delivery on a
/// `413 Payload Too Large`. Keep the wire-size invariant at every flush, not
/// only on the fast path that first reaches `batch_size`.
fn take_pending_batch(
    pending: &mut Vec<WebSocketUsageEnvelope>,
    batch_size: usize,
) -> WebSocketUsageBatch {
    let batch_size = batch_size.max(1);
    let remainder = if pending.len() > batch_size {
        pending.split_off(batch_size)
    } else {
        Vec::new()
    };
    let events = std::mem::replace(pending, remainder);
    WebSocketUsageBatch { events }
}

async fn flush_existing_batch(
    client: &reqwest::Client,
    endpoint: &str,
    auth_token: Option<&str>,
    mut state: RetryState,
) -> Result<(), RetryState> {
    if flush_batch(client, endpoint, auth_token, &state.batch).await {
        Ok(())
    } else {
        state.attempts += 1;
        state.next_retry_at = Instant::now() + retry_delay(state.attempts);
        Err(state)
    }
}

fn retry_delay(attempt: u32) -> Duration {
    let capped_attempt = attempt.min(6);
    Duration::from_secs(1_u64 << capped_attempt)
}

fn ensure_spool_dir(path: &Path) -> std::io::Result<()> {
    std::fs::create_dir_all(path)
}

fn spool_retry_state(path: &Path, state: &RetryState) -> std::io::Result<PathBuf> {
    spool_batch(path, &state.batch)
}

fn spool_batch(path: &Path, batch: &WebSocketUsageBatch) -> std::io::Result<PathBuf> {
    ensure_spool_dir(path)?;

    let file_name = format!(
        "ws-usage-{}-{}.json",
        current_time_ms(),
        Uuid::new_v4().simple()
    );
    let final_path = path.join(file_name);
    let temp_path = final_path.with_extension("tmp");
    let data = serde_json::to_vec(batch).map_err(std::io::Error::other)?;
    std::fs::write(&temp_path, data)?;
    std::fs::rename(&temp_path, &final_path)?;
    Ok(final_path)
}

fn load_batch_from_file(path: &Path) -> std::io::Result<WebSocketUsageBatch> {
    let data = std::fs::read(path)?;
    serde_json::from_slice(&data).map_err(std::io::Error::other)
}

fn oldest_spooled_batch(path: &Path) -> std::io::Result<Option<PathBuf>> {
    if !path.exists() {
        return Ok(None);
    }

    let mut entries: Vec<PathBuf> = std::fs::read_dir(path)?
        .filter_map(|entry| entry.ok().map(|entry| entry.path()))
        .filter(|entry| entry.extension().and_then(|ext| ext.to_str()) == Some("json"))
        .collect();
    entries.sort();
    Ok(entries.into_iter().next())
}

async fn flush_one_spooled_batch(
    client: &reqwest::Client,
    endpoint: &str,
    auth_token: Option<&str>,
    spool_dir: &Path,
    batch_size: usize,
) -> std::io::Result<()> {
    let Some(path) = oldest_spooled_batch(spool_dir)? else {
        return Ok(());
    };

    let mut batch = load_batch_from_file(&path)?;
    let batch_size = batch_size.max(1);
    let remainder = if batch.events.len() > batch_size {
        Some(WebSocketUsageBatch {
            events: batch.events.split_off(batch_size),
        })
    } else {
        None
    };

    if flush_batch(client, endpoint, auth_token, &batch).await {
        if let Some(remainder) = remainder {
            // Replacing the file after the successful request can repeat the
            // first chunk after a crash, but every envelope keeps its event ID
            // and ingestion is idempotent. Rewriting before the request could
            // lose billable usage instead.
            replace_spooled_batch(&path, &remainder)?;
        } else {
            std::fs::remove_file(path)?;
        }
    }

    Ok(())
}

fn replace_spooled_batch(path: &Path, batch: &WebSocketUsageBatch) -> std::io::Result<()> {
    let temp_path = path.with_extension("tmp");
    let data = serde_json::to_vec(batch).map_err(std::io::Error::other)?;
    std::fs::write(&temp_path, data)?;
    std::fs::rename(temp_path, path)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    use tokio::net::TcpListener;

    fn usage_envelope(index: u64) -> WebSocketUsageEnvelope {
        WebSocketUsageEnvelope {
            event_id: format!("evt-{index}"),
            occurred_at_ms: index,
            build_id: None,
            event: WebSocketUsageEvent::ConnectionEstablished {
                client_id: format!("client-{index}"),
                remote_addr: "127.0.0.1:1234".to_string(),
                deployment_id: Some("1".to_string()),
                identity: UsageIdentity::default(),
            },
        }
    }

    fn usage_event(index: u64) -> WebSocketUsageEvent {
        WebSocketUsageEvent::ConnectionEstablished {
            client_id: format!("client-{index}"),
            remote_addr: "127.0.0.1:1234".to_string(),
            deployment_id: Some("1".to_string()),
            identity: UsageIdentity::default(),
        }
    }

    async fn successful_batch_server(
        request_count: usize,
    ) -> (String, tokio::task::JoinHandle<Vec<Vec<String>>>) {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("test server should bind");
        let address = listener.local_addr().expect("test server address");
        let server = tokio::spawn(async move {
            let mut received = Vec::with_capacity(request_count);
            for _ in 0..request_count {
                let (mut stream, _) = listener.accept().await.expect("request should connect");
                let mut bytes = Vec::new();
                let body_start = loop {
                    let mut chunk = [0_u8; 4096];
                    let count = stream.read(&mut chunk).await.expect("request should read");
                    assert!(count > 0, "request ended before its headers");
                    bytes.extend_from_slice(&chunk[..count]);
                    if let Some(index) = bytes.windows(4).position(|window| window == b"\r\n\r\n") {
                        break index + 4;
                    }
                };
                let headers = std::str::from_utf8(&bytes[..body_start])
                    .expect("request headers should be UTF-8");
                let content_length = headers
                    .lines()
                    .find_map(|line| {
                        let (name, value) = line.split_once(':')?;
                        name.eq_ignore_ascii_case("content-length")
                            .then(|| value.trim().parse::<usize>().expect("content length"))
                    })
                    .expect("request should have content length");
                while bytes.len() - body_start < content_length {
                    let mut chunk = [0_u8; 4096];
                    let count = stream.read(&mut chunk).await.expect("body should read");
                    assert!(count > 0, "request ended before its body");
                    bytes.extend_from_slice(&chunk[..count]);
                }
                let batch: WebSocketUsageBatch =
                    serde_json::from_slice(&bytes[body_start..body_start + content_length])
                        .expect("request should contain a usage batch");
                received.push(
                    batch
                        .events
                        .into_iter()
                        .map(|event| event.event_id)
                        .collect(),
                );
                stream
                    .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n")
                    .await
                    .expect("response should write");
            }
            received
        });
        (format!("http://{address}"), server)
    }

    fn temp_spool_dir() -> PathBuf {
        let dir = std::env::temp_dir().join(format!("arete-usage-test-{}", Uuid::new_v4()));
        fs::create_dir_all(&dir).expect("temp dir should be created");
        dir
    }

    #[tokio::test]
    async fn channel_usage_emitter_forwards_events() {
        let (tx, mut rx) = mpsc::unbounded_channel();
        let emitter = ChannelUsageEmitter::new(tx);

        emitter
            .emit(WebSocketUsageEvent::SubscriptionCreated {
                client_id: "client-1".to_string(),
                deployment_id: Some("deployment-1".to_string()),
                identity: UsageIdentity {
                    metering_key: Some("meter-1".to_string()),
                    subject: Some("subject-1".to_string()),
                    ..Default::default()
                },
                view_id: "OreRound/latest".to_string(),
            })
            .await;

        let event = rx.recv().await.expect("event should be forwarded");
        match event {
            WebSocketUsageEvent::SubscriptionCreated { view_id, .. } => {
                assert_eq!(view_id, "OreRound/latest");
            }
            other => panic!("unexpected event: {other:?}"),
        }
    }

    #[tokio::test]
    async fn flush_is_an_ordered_barrier_and_keeps_the_emitter_open() {
        let (endpoint, server) = successful_batch_server(2).await;
        let emitter = HttpUsageEmitter::with_config(endpoint, None, 50, Duration::from_secs(3_600));

        emitter.emit(usage_event(1)).await;
        emitter.flush().await;
        emitter.emit(usage_event(2)).await;
        emitter.shutdown().await;

        let received = server.await.expect("test server should finish");
        assert_eq!(received.len(), 2);
        assert_eq!(received[0].len(), 1);
        assert_eq!(received[1].len(), 1);
    }

    #[test]
    fn retry_delay_grows_and_caps() {
        assert_eq!(retry_delay(1), Duration::from_secs(2));
        assert_eq!(retry_delay(2), Duration::from_secs(4));
        assert_eq!(retry_delay(6), Duration::from_secs(64));
        assert_eq!(retry_delay(9), Duration::from_secs(64));
    }

    #[test]
    fn spooled_batches_round_trip() {
        let dir = temp_spool_dir();
        let batch = WebSocketUsageBatch {
            events: vec![WebSocketUsageEnvelope {
                event_id: "evt_1".to_string(),
                occurred_at_ms: 123,
                build_id: Some("7".to_string()),
                event: WebSocketUsageEvent::UpdateSent {
                    client_id: "client-1".to_string(),
                    deployment_id: Some("1".to_string()),
                    identity: UsageIdentity {
                        metering_key: Some("api_key:1".to_string()),
                        subject: Some("user:1".to_string()),
                        ..Default::default()
                    },
                    view_id: "OreRound/latest".to_string(),
                    messages: 1,
                    bytes: 42,
                },
            }],
        };

        let path = spool_batch(&dir, &batch).expect("batch should spool");
        let loaded = load_batch_from_file(&path).expect("batch should load");
        assert_eq!(loaded.events.len(), 1);
        assert_eq!(loaded.events[0].build_id.as_deref(), Some("7"));

        fs::remove_dir_all(dir).expect("temp dir should be removed");
    }

    #[test]
    fn pending_backlog_is_drained_in_bounded_batches() {
        let mut pending = (0..1250).map(usage_envelope).collect::<Vec<_>>();

        let first = take_pending_batch(&mut pending, 50);
        assert_eq!(first.events.len(), 50);
        assert_eq!(pending.len(), 1200);
        assert_eq!(first.events[0].event_id, "evt-0");
        assert_eq!(pending[0].event_id, "evt-50");
    }

    #[tokio::test]
    async fn oversized_legacy_spool_is_delivered_in_successive_bounded_chunks() {
        let dir = temp_spool_dir();
        let path = dir.join("ws-usage-100-a.json");
        let batch = WebSocketUsageBatch {
            events: (0..5).map(usage_envelope).collect(),
        };
        fs::write(&path, serde_json::to_vec(&batch).unwrap()).expect("legacy spool should write");
        let (endpoint, server) = successful_batch_server(3).await;
        let client = reqwest::Client::new();

        flush_one_spooled_batch(&client, &endpoint, None, &dir, 2)
            .await
            .expect("first chunk should flush");
        let loaded = load_batch_from_file(&path).expect("remainder should be readable");
        assert_eq!(loaded.events.len(), 3);
        assert_eq!(loaded.events[0].event_id, "evt-2");

        flush_one_spooled_batch(&client, &endpoint, None, &dir, 2)
            .await
            .expect("second chunk should flush");
        let loaded = load_batch_from_file(&path).expect("final remainder should be readable");
        assert_eq!(loaded.events.len(), 1);
        assert_eq!(loaded.events[0].event_id, "evt-4");

        flush_one_spooled_batch(&client, &endpoint, None, &dir, 2)
            .await
            .expect("final chunk should flush");
        assert!(!path.exists());

        let received = server.await.expect("test server should finish");
        assert_eq!(
            received,
            vec![
                vec!["evt-0".to_string(), "evt-1".to_string()],
                vec!["evt-2".to_string(), "evt-3".to_string()],
                vec!["evt-4".to_string()],
            ]
        );

        fs::remove_dir_all(dir).expect("temp dir should be removed");
    }

    #[tokio::test]
    async fn shutdown_drains_every_chunk_after_a_retry_recovers() {
        let (endpoint, server) = successful_batch_server(4).await;
        let client = reqwest::Client::new();
        let mut pending = (1..=5).map(usage_envelope).collect::<Vec<_>>();
        let mut retry_state = Some(RetryState {
            batch: WebSocketUsageBatch {
                events: vec![usage_envelope(0)],
            },
            attempts: 1,
            next_retry_at: Instant::now(),
        });

        flush_on_shutdown(
            &client,
            &endpoint,
            None,
            &mut pending,
            &mut retry_state,
            LifecycleFlushOptions {
                spool_dir: None,
                batch_size: 2,
                preserve_failed_for_retry: false,
            },
        )
        .await;

        assert!(pending.is_empty());
        assert!(retry_state.is_none());
        let received = server.await.expect("test server should finish");
        assert_eq!(
            received,
            vec![
                vec!["evt-0".to_string()],
                vec!["evt-1".to_string(), "evt-2".to_string()],
                vec!["evt-3".to_string(), "evt-4".to_string()],
                vec!["evt-5".to_string()],
            ]
        );
    }

    #[tokio::test]
    async fn flush_barrier_retains_failed_batch_for_later_retry() {
        let client = reqwest::Client::new();
        let mut pending = vec![usage_envelope(0)];
        let mut retry_state = None;

        flush_on_shutdown(
            &client,
            "http://127.0.0.1:1/unreachable",
            None,
            &mut pending,
            &mut retry_state,
            LifecycleFlushOptions {
                spool_dir: None,
                batch_size: 2,
                preserve_failed_for_retry: true,
            },
        )
        .await;

        assert!(pending.is_empty());
        assert_eq!(
            retry_state
                .as_ref()
                .expect("failed flush should retain its retry")
                .batch
                .events[0]
                .event_id,
            "evt-0"
        );

        let (endpoint, server) = successful_batch_server(1).await;
        flush_on_shutdown(
            &client,
            &endpoint,
            None,
            &mut pending,
            &mut retry_state,
            LifecycleFlushOptions {
                spool_dir: None,
                batch_size: 2,
                preserve_failed_for_retry: true,
            },
        )
        .await;

        assert!(retry_state.is_none());
        assert_eq!(
            server.await.expect("test server should finish"),
            vec![vec!["evt-0".to_string()]]
        );
    }

    #[tokio::test]
    async fn flush_barrier_retains_failed_batch_when_spool_is_unwritable() {
        let dir = temp_spool_dir();
        let invalid_spool_dir = dir.join("not-a-directory");
        fs::write(&invalid_spool_dir, b"occupied").expect("spool blocker should write");
        let client = reqwest::Client::new();
        let mut pending = vec![usage_envelope(0)];
        let mut retry_state = None;

        flush_on_shutdown(
            &client,
            "http://127.0.0.1:1/unreachable",
            None,
            &mut pending,
            &mut retry_state,
            LifecycleFlushOptions {
                spool_dir: Some(&invalid_spool_dir),
                batch_size: 2,
                preserve_failed_for_retry: true,
            },
        )
        .await;

        assert!(pending.is_empty());
        assert_eq!(
            retry_state
                .as_ref()
                .expect("failed flush should retain its retry")
                .batch
                .events[0]
                .event_id,
            "evt-0"
        );

        fs::remove_dir_all(dir).expect("temp dir should be removed");
    }

    #[tokio::test]
    async fn shutdown_persists_every_chunk_before_network_drain_when_spooling() {
        let dir = temp_spool_dir();
        let client = reqwest::Client::new();
        let mut pending = (1..=5).map(usage_envelope).collect::<Vec<_>>();
        let mut retry_state = Some(RetryState {
            batch: WebSocketUsageBatch {
                events: vec![usage_envelope(0)],
            },
            attempts: 1,
            next_retry_at: Instant::now(),
        });

        flush_on_shutdown(
            &client,
            "http://127.0.0.1:1/unreachable",
            None,
            &mut pending,
            &mut retry_state,
            LifecycleFlushOptions {
                spool_dir: Some(&dir),
                batch_size: 2,
                preserve_failed_for_retry: false,
            },
        )
        .await;

        assert!(pending.is_empty());
        assert!(retry_state.is_none());
        let mut event_ids = Vec::new();
        let mut batch_sizes = Vec::new();
        for entry in fs::read_dir(&dir).expect("spool directory should be readable") {
            let batch = load_batch_from_file(&entry.unwrap().path()).expect("spool should load");
            batch_sizes.push(batch.events.len());
            event_ids.extend(batch.events.into_iter().map(|event| event.event_id));
        }
        batch_sizes.sort_unstable();
        event_ids.sort();
        assert_eq!(batch_sizes, vec![1, 1, 2, 2]);
        assert_eq!(
            event_ids,
            (0..=5)
                .map(|index| format!("evt-{index}"))
                .collect::<Vec<_>>()
        );

        fs::remove_dir_all(dir).expect("temp dir should be removed");
    }

    #[tokio::test]
    async fn shutdown_uses_http_when_spool_storage_is_unwritable() {
        let dir = temp_spool_dir();
        let invalid_spool_dir = dir.join("not-a-directory");
        fs::write(&invalid_spool_dir, b"occupied").expect("spool blocker should write");
        let (endpoint, server) = successful_batch_server(4).await;
        let client = reqwest::Client::new();
        let mut pending = (1..=5).map(usage_envelope).collect::<Vec<_>>();
        let mut retry_state = Some(RetryState {
            batch: WebSocketUsageBatch {
                events: vec![usage_envelope(0)],
            },
            attempts: 1,
            next_retry_at: Instant::now(),
        });

        flush_on_shutdown(
            &client,
            &endpoint,
            None,
            &mut pending,
            &mut retry_state,
            LifecycleFlushOptions {
                spool_dir: Some(&invalid_spool_dir),
                batch_size: 2,
                preserve_failed_for_retry: false,
            },
        )
        .await;

        let received = server.await.expect("test server should finish");
        assert_eq!(
            received,
            vec![
                vec!["evt-0".to_string()],
                vec!["evt-1".to_string(), "evt-2".to_string()],
                vec!["evt-3".to_string(), "evt-4".to_string()],
                vec!["evt-5".to_string()],
            ]
        );

        fs::remove_dir_all(dir).expect("temp dir should be removed");
    }

    #[test]
    fn usage_identity_is_flattened_and_old_spool_json_remains_readable() {
        let legacy: WebSocketUsageBatch = serde_json::from_value(serde_json::json!({
            "events": [{
                "event_id": "evt-old",
                "occurred_at_ms": 123,
                "event": {
                    "type": "update_sent",
                    "client_id": "client-1",
                    "deployment_id": "1",
                    "metering_key": "api_key:1",
                    "subject": "user:1",
                    "view_id": "Round/latest",
                    "messages": 1,
                    "bytes": 42
                }
            }]
        }))
        .expect("old-format spool remains readable");
        let value = serde_json::to_value(&legacy).unwrap();
        assert_eq!(value["events"][0]["event"]["metering_key"], "api_key:1");
        assert!(value["events"][0]["event"].get("actor_key").is_none());

        let event = WebSocketUsageEvent::UpdateSent {
            client_id: "client-2".to_string(),
            deployment_id: Some("2".to_string()),
            identity: UsageIdentity {
                metering_key: Some("account:42".to_string()),
                subject: Some("user:7".to_string()),
                key_class: Some("secret".to_string()),
                actor_key: Some("user:7".to_string()),
                account_key: Some("account:42".to_string()),
                consumer_key: Some("consumer:key-9".to_string()),
                plan_code: Some("agent_trial".to_string()),
                policy_version: Some(3),
            },
            view_id: "Round/latest".to_string(),
            messages: 1,
            bytes: 42,
        };
        let value = serde_json::to_value(event).unwrap();
        assert_eq!(value["account_key"], "account:42");
        assert_eq!(value["plan_code"], "agent_trial");
        assert_eq!(value["policy_version"], 3);
    }

    #[test]
    fn legacy_and_attributed_envelopes_are_compatible() {
        let legacy = serde_json::json!({
            "event_id": "legacy",
            "occurred_at_ms": 123,
            "event": {
                "type": "update_sent",
                "client_id": "client",
                "deployment_id": "1",
                "metering_key": null,
                "subject": null,
                "view_id": "view",
                "messages": 1,
                "bytes": 2
            }
        });
        let decoded: WebSocketUsageEnvelope = serde_json::from_value(legacy).unwrap();
        assert_eq!(decoded.build_id, None);

        let mut attributed = serde_json::to_value(decoded).unwrap();
        assert!(attributed.get("build_id").is_none());
        attributed["build_id"] = serde_json::json!("42");
        let decoded: WebSocketUsageEnvelope = serde_json::from_value(attributed).unwrap();
        assert_eq!(decoded.build_id.as_deref(), Some("42"));
    }

    #[test]
    fn attributed_constructor_validates_build_identity() {
        assert!(
            HttpUsageEmitter::new_attributed("http://localhost".to_string(), None, "0").is_err()
        );
        assert!(
            HttpUsageEmitter::new_attributed("http://localhost".to_string(), None, " 7").is_err()
        );
    }

    #[test]
    fn oldest_spooled_batch_prefers_lexicographically_oldest_file() {
        let dir = temp_spool_dir();
        fs::write(dir.join("ws-usage-100-a.json"), b"{\"events\":[]}").expect("first batch");
        fs::write(dir.join("ws-usage-200-b.json"), b"{\"events\":[]}").expect("second batch");

        let oldest = oldest_spooled_batch(&dir)
            .expect("listing should succeed")
            .expect("batch should exist");
        assert!(oldest.ends_with("ws-usage-100-a.json"));

        fs::remove_dir_all(dir).expect("temp dir should be removed");
    }
}