railwayapp 5.30.4

Interact with Railway via CLI
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
use crate::{
    commands::{
        queries::{self},
        subscriptions::{
            self, build_logs, deployment, deployment_logs, dns_query_logs, http_logs,
            network_flow_logs,
        },
    },
    post_graphql,
    subscription::subscribe_graphql,
    util::retry::RetryConfig,
};
use anyhow::{Context, Result, anyhow};
use chrono::{DateTime, Duration as ChronoDuration, SecondsFormat, Utc};
use futures::StreamExt;
use reqwest::Client;
use std::collections::{HashSet, VecDeque};
use std::time::{Duration, Instant};
use tokio::time::sleep;

const LOGS_RETRY_CONFIG: RetryConfig = RetryConfig {
    max_attempts: 12,
    initial_delay_ms: 1000,
    max_delay_ms: 8000,
    backoff_multiplier: 1.5,
    on_retry: None,
};

const HTTP_LOG_STREAM_AFTER_WINDOW: Duration = Duration::from_secs(60 * 60);
const HTTP_LOG_STREAM_BATCH_SIZE: i64 = 500;
const STREAM_STABLE_CONNECTION_DURATION: Duration = Duration::from_secs(30);
const ANCHORED_LOG_DEFAULT_LIMIT: i64 = 500;
const STREAM_LOOKBACK_SECONDS: i64 = 30;
const STREAM_DEDUPE_CACHE_SIZE: usize = 10_000;

pub struct FetchLogsParams<'a> {
    pub client: &'a Client,
    pub backboard: &'a str,
    pub deployment_id: String,
    pub limit: Option<i64>,
    pub filter: Option<String>,
    pub start_date: Option<DateTime<Utc>>,
    pub end_date: Option<DateTime<Utc>>,
}

pub struct FetchNetworkFlowLogsParams<'a> {
    pub client: &'a Client,
    pub backboard: &'a str,
    pub environment_id: String,
    pub service_id: Option<String>,
    pub limit: Option<i64>,
    pub filter: Option<String>,
    pub start_date: Option<DateTime<Utc>>,
    pub end_date: Option<DateTime<Utc>>,
}

pub struct FetchDnsQueryLogsParams<'a> {
    pub client: &'a Client,
    pub backboard: &'a str,
    pub environment_id: String,
    pub service_id: Option<String>,
    pub limit: Option<i64>,
    pub filter: Option<String>,
    pub start_date: Option<DateTime<Utc>>,
    pub end_date: Option<DateTime<Utc>>,
}

// Helper to handle the API's off-by-one bug where it returns limit+1 logs
fn take_last_n_logs<T>(mut logs: Vec<T>, limit: Option<i64>) -> Vec<T> {
    if let Some(l) = limit {
        let l = l as usize;
        if logs.len() > l {
            // Remove items from the beginning to keep only the last l items
            logs.drain(0..logs.len() - l);
        }
    }
    logs
}

#[derive(Debug, PartialEq, Eq)]
struct AnchoredLogWindow {
    before_limit: Option<i64>,
    before_date: Option<String>,
    anchor_date: Option<String>,
    after_date: Option<String>,
    after_limit: Option<i64>,
}

fn format_anchored_log_timestamp(date: DateTime<Utc>) -> String {
    date.to_rfc3339_opts(SecondsFormat::Nanos, true)
}

fn anchored_log_window(
    limit: Option<i64>,
    start_date: Option<DateTime<Utc>>,
    end_date: Option<DateTime<Utc>>,
    now: DateTime<Utc>,
) -> AnchoredLogWindow {
    let before_limit = Some(limit.unwrap_or(ANCHORED_LOG_DEFAULT_LIMIT));

    match (start_date, end_date) {
        (Some(start), Some(end)) => AnchoredLogWindow {
            before_limit,
            before_date: Some(format_anchored_log_timestamp(start)),
            anchor_date: Some(format_anchored_log_timestamp(end)),
            after_date: Some(format_anchored_log_timestamp(end)),
            after_limit: Some(0),
        },
        (Some(start), None) => AnchoredLogWindow {
            before_limit,
            before_date: Some(format_anchored_log_timestamp(start)),
            anchor_date: Some(format_anchored_log_timestamp(now)),
            after_date: Some(format_anchored_log_timestamp(now)),
            after_limit: Some(0),
        },
        (None, Some(end)) => AnchoredLogWindow {
            before_limit,
            before_date: Some(format_anchored_log_timestamp(
                DateTime::<Utc>::from_timestamp(0, 0)
                    .expect("Unix epoch should be a valid timestamp"),
            )),
            anchor_date: Some(format_anchored_log_timestamp(end)),
            after_date: Some(format_anchored_log_timestamp(end)),
            after_limit: Some(0),
        },
        (None, None) => AnchoredLogWindow {
            before_limit,
            before_date: None,
            anchor_date: None,
            after_date: None,
            after_limit: None,
        },
    }
}

pub async fn fetch_build_logs(
    params: FetchLogsParams<'_>,
    mut on_log: impl FnMut(queries::build_logs::BuildLogsBuildLogs),
) -> Result<()> {
    let vars = queries::build_logs::Variables {
        deployment_id: params.deployment_id,
        limit: params.limit,
        start_date: params.start_date,
        end_date: params.end_date,
        filter: params.filter,
    };

    let response =
        post_graphql::<queries::BuildLogs, _>(params.client, params.backboard, vars).await?;

    // Take only the requested number of logs from the end (the API has a bug and returns limit+1)
    let logs = response.build_logs;
    let logs_to_process = take_last_n_logs(logs, params.limit);

    for log in logs_to_process {
        on_log(log);
    }

    Ok(())
}

pub async fn fetch_deploy_logs(
    params: FetchLogsParams<'_>,
    mut on_log: impl FnMut(queries::deployment_logs::LogFields),
) -> Result<()> {
    let vars = queries::deployment_logs::Variables {
        deployment_id: params.deployment_id,
        limit: params.limit,
        filter: params.filter,
        start_date: params.start_date,
        end_date: params.end_date,
    };

    let response =
        post_graphql::<queries::DeploymentLogs, _>(params.client, params.backboard, vars).await?;

    // Take only the requested number of logs from the end (the API has a bug and returns limit+1)
    let logs = response.deployment_logs;
    let logs_to_process = take_last_n_logs(logs, params.limit);

    for log in logs_to_process {
        on_log(log);
    }

    Ok(())
}

pub async fn fetch_http_logs(
    params: FetchLogsParams<'_>,
    mut on_log: impl FnMut(queries::http_logs::HttpLogFields),
) -> Result<()> {
    let before_limit = params.limit.unwrap_or(500);
    let vars = queries::http_logs::Variables {
        deployment_id: params.deployment_id,
        filter: params.filter,
        before_limit,
        before_date: params.start_date.map(|date| date.to_rfc3339()),
        anchor_date: params.end_date.map(|date| date.to_rfc3339()),
        after_date: None,
        after_limit: None,
    };

    let response =
        post_graphql::<queries::HttpLogs, _>(params.client, params.backboard, vars).await?;

    let logs = response.http_logs;
    let logs = take_last_n_logs(logs, Some(before_limit));

    for log in logs {
        on_log(log);
    }

    Ok(())
}

pub async fn fetch_network_flow_logs(
    params: FetchNetworkFlowLogsParams<'_>,
    mut on_log: impl FnMut(queries::network_flow_logs::NetworkFlowLogFields),
) -> Result<()> {
    let window = anchored_log_window(params.limit, params.start_date, params.end_date, Utc::now());
    let vars = queries::network_flow_logs::Variables {
        environment_id: params.environment_id,
        service_id: params.service_id,
        filter: params.filter,
        before_limit: window.before_limit,
        before_date: window.before_date,
        anchor_date: window.anchor_date,
        after_date: window.after_date,
        after_limit: window.after_limit,
    };

    let response =
        post_graphql::<queries::NetworkFlowLogs, _>(params.client, params.backboard, vars).await?;

    let logs = take_last_n_logs(response.network_flow_logs, window.before_limit);

    for log in logs {
        on_log(log);
    }

    Ok(())
}

pub async fn fetch_dns_query_logs(
    params: FetchDnsQueryLogsParams<'_>,
    mut on_log: impl FnMut(queries::dns_query_logs::DnsQueryLogFields),
) -> Result<()> {
    let window = anchored_log_window(params.limit, params.start_date, params.end_date, Utc::now());
    let vars = queries::dns_query_logs::Variables {
        environment_id: params.environment_id,
        service_id: params.service_id,
        filter: params.filter,
        before_limit: window.before_limit,
        before_date: window.before_date,
        anchor_date: window.anchor_date,
        after_date: window.after_date,
        after_limit: window.after_limit,
    };

    let response =
        post_graphql::<queries::DnsQueryLogs, _>(params.client, params.backboard, vars).await?;

    let logs = take_last_n_logs(response.dns_query_logs, window.before_limit);

    for log in logs {
        on_log(log);
    }

    Ok(())
}

pub async fn stream_build_logs(
    deployment_id: String,
    filter: Option<String>,
    mut on_log: impl FnMut(build_logs::LogFields),
) -> Result<()> {
    let mut last_timestamp: Option<String> = None;
    let mut attempt = 0;
    let mut delay_ms = LOGS_RETRY_CONFIG.initial_delay_ms;
    let mut received_any_logs = false;

    loop {
        attempt += 1;

        let vars = subscriptions::build_logs::Variables {
            deployment_id: deployment_id.clone(),
            filter: filter.clone().or_else(|| Some(String::new())),
            limit: Some(500),
        };

        let result = async {
            let mut stream = subscribe_graphql::<subscriptions::BuildLogs>(vars).await?;

            while let Some(response) = stream.next().await {
                let log = response
                    .context("Build log stream error")?
                    .data
                    .context("Failed to retrieve build log")?;

                for line in log.build_logs {
                    if let Some(ref ts) = last_timestamp {
                        if line.timestamp <= *ts {
                            continue;
                        }
                    }
                    last_timestamp = Some(line.timestamp.clone());
                    received_any_logs = true;
                    on_log(line);
                }
            }
            Ok::<(), anyhow::Error>(())
        }
        .await;

        match result {
            Ok(()) => return Ok(()),
            Err(e) if attempt >= LOGS_RETRY_CONFIG.max_attempts => {
                // If we received some logs before the error, treat as success
                // (the build likely finished and the stream closed)
                if received_any_logs {
                    return Ok(());
                }
                return Err(e);
            }
            Err(_) => {
                // If we've received logs and then get an error, the build likely completed
                // and the stream was closed by the server. Treat this as success.
                if received_any_logs {
                    return Ok(());
                }
                sleep(Duration::from_millis(delay_ms)).await;
                delay_ms = ((delay_ms as f64 * LOGS_RETRY_CONFIG.backoff_multiplier) as u64)
                    .min(LOGS_RETRY_CONFIG.max_delay_ms);
            }
        }
    }
}

pub async fn stream_http_logs(
    deployment_id: String,
    filter: Option<String>,
    on_log: impl FnMut(http_logs::HttpLogFields),
) -> Result<()> {
    tokio::select! {
        result = stream_http_logs_inner(deployment_id.clone(), filter, on_log) => result,
        _ = wait_for_deployment_removal(&deployment_id) => {
            eprintln!("\nDeployment was removed. HTTP log stream closed.");
            Ok(())
        }
    }
}

struct StreamMessages {
    stream_error: &'static str,
    data_error: &'static str,
    closed_without_events: &'static str,
}

/// Drives an anchored log subscription (network flow, DNS) with the same
/// reconnect policy as `stream_http_logs_inner`: mid-stream errors are retried
/// with backoff, every reconnect waits at least the initial delay, and retry
/// state only resets once a connection proves stable. Each reconnect re-anchors
/// `beforeDate` to the newest timestamp seen minus a lookback window; rows at
/// or before the previous connection's watermark are flagged as replays via
/// `on_line`'s second argument. The callback returns whether the row was
/// emitted so replay-only connections do not reset retry state.
async fn stream_anchored_logs<Q, Line>(
    mut build_vars: impl FnMut(String) -> Q::Variables,
    extract_lines: impl Fn(Q::ResponseData) -> Vec<Line>,
    line_timestamp: impl Fn(&Line) -> &str,
    mut on_line: impl FnMut(Line, Option<DateTime<Utc>>) -> bool,
    messages: StreamMessages,
) -> Result<()>
where
    Q: graphql_client::GraphQLQuery + Send + Sync + Unpin + 'static,
    Q::Variables: Send + Sync + Unpin,
    Q::ResponseData: std::fmt::Debug,
{
    let mut max_timestamp: Option<DateTime<Utc>> = None;
    let mut replay_cutoff: Option<DateTime<Utc>> = None;
    let mut attempt = 0;
    let mut delay_ms = LOGS_RETRY_CONFIG.initial_delay_ms;

    loop {
        let connected_at = Instant::now();
        let mut emitted_any_logs = false;
        let vars = build_vars(stream_before_date(max_timestamp));

        let mut stream = match subscribe_graphql::<Q>(vars).await {
            Ok(stream) => stream,
            Err(e) => {
                attempt += 1;

                if attempt >= LOGS_RETRY_CONFIG.max_attempts {
                    return Err(e);
                }

                sleep(Duration::from_millis(delay_ms)).await;
                delay_ms = ((delay_ms as f64 * LOGS_RETRY_CONFIG.backoff_multiplier) as u64)
                    .min(LOGS_RETRY_CONFIG.max_delay_ms);
                continue;
            }
        };

        let result = async {
            while let Some(response) = stream.next().await {
                let log = response
                    .context(messages.stream_error)?
                    .data
                    .context(messages.data_error)?;

                emitted_any_logs |= process_anchored_stream_lines(
                    extract_lines(log),
                    &line_timestamp,
                    replay_cutoff,
                    &mut max_timestamp,
                    &mut on_line,
                );
            }

            Ok::<(), anyhow::Error>(())
        }
        .await;

        replay_cutoff = max_timestamp;

        let should_reset_retry_state =
            should_reset_stream_retry_state(emitted_any_logs, connected_at.elapsed());

        if should_reset_retry_state {
            attempt = 0;
            delay_ms = LOGS_RETRY_CONFIG.initial_delay_ms;
        } else {
            attempt += 1;

            match result {
                Err(e) if attempt >= LOGS_RETRY_CONFIG.max_attempts => return Err(e),
                Ok(()) if attempt >= LOGS_RETRY_CONFIG.max_attempts => {
                    return Err(anyhow!(messages.closed_without_events));
                }
                _ => {}
            }
        }

        sleep(Duration::from_millis(delay_ms)).await;

        if !should_reset_retry_state {
            delay_ms = ((delay_ms as f64 * LOGS_RETRY_CONFIG.backoff_multiplier) as u64)
                .min(LOGS_RETRY_CONFIG.max_delay_ms);
        }
    }
}

fn process_anchored_stream_lines<Line>(
    lines: Vec<Line>,
    line_timestamp: &impl Fn(&Line) -> &str,
    replay_cutoff: Option<DateTime<Utc>>,
    max_timestamp: &mut Option<DateTime<Utc>>,
    on_line: &mut impl FnMut(Line, Option<DateTime<Utc>>) -> bool,
) -> bool {
    let mut emitted_any_logs = false;

    for line in lines {
        update_max_stream_timestamp(line_timestamp(&line), max_timestamp);
        emitted_any_logs |= on_line(line, replay_cutoff);
    }

    emitted_any_logs
}

pub async fn stream_network_flow_logs(
    environment_id: String,
    service_id: Option<String>,
    filter: Option<String>,
    mut on_log: impl FnMut(network_flow_logs::NetworkFlowLogFields),
) -> Result<()> {
    let mut seen_flow_ids = StreamedLogDedupe::new(STREAM_DEDUPE_CACHE_SIZE);

    stream_anchored_logs::<subscriptions::NetworkFlowLogs, _>(
        |before_date| subscriptions::network_flow_logs::Variables {
            environment_id: environment_id.clone(),
            service_id: service_id.clone(),
            filter: filter.clone(),
            before_limit: Some(ANCHORED_LOG_DEFAULT_LIMIT),
            before_date: Some(before_date),
            anchor_date: None,
            after_date: None,
            after_limit: Some(0),
        },
        |data| data.network_flow_logs,
        |line: &network_flow_logs::NetworkFlowLogFields| line.capture_end.as_str(),
        |line, _replay_cutoff| {
            // Flow IDs are unique, so any repeated ID is a replay regardless
            // of when it arrives
            if seen_flow_ids.insert(line.flow_id.clone()) {
                on_log(line);
                true
            } else {
                false
            }
        },
        StreamMessages {
            stream_error: "Network flow log stream error",
            data_error: "Failed to retrieve network flow logs",
            closed_without_events: "Network flow log stream closed before receiving any events",
        },
    )
    .await
}

pub async fn stream_dns_query_logs(
    environment_id: String,
    service_id: Option<String>,
    filter: Option<String>,
    mut on_log: impl FnMut(dns_query_logs::DnsQueryLogFields),
) -> Result<()> {
    // DNS query logs carry no unique row ID, so replays after a reconnect are
    // recognized by their full serialized contents. Only rows at or before the
    // reconnect watermark are eligible for deduplication: identical queries
    // arriving live on a healthy connection are all emitted.
    let mut seen_rows = StreamedLogDedupe::new(STREAM_DEDUPE_CACHE_SIZE);

    stream_anchored_logs::<subscriptions::DnsQueryLogs, _>(
        |before_date| subscriptions::dns_query_logs::Variables {
            environment_id: environment_id.clone(),
            service_id: service_id.clone(),
            filter: filter.clone(),
            before_limit: Some(ANCHORED_LOG_DEFAULT_LIMIT),
            before_date: Some(before_date),
            anchor_date: None,
            after_date: None,
            after_limit: Some(0),
        },
        |data| data.dns_query_logs,
        |line: &dns_query_logs::DnsQueryLogFields| line.queried_at.as_str(),
        |line, replay_cutoff| {
            if let Ok(row_key) = serde_json::to_string(&line) {
                let already_seen = !seen_rows.insert(row_key);
                if already_seen && is_replayed_row(&line.queried_at, replay_cutoff) {
                    return false;
                }
            }

            on_log(line);
            true
        },
        StreamMessages {
            stream_error: "DNS query log stream error",
            data_error: "Failed to retrieve DNS query logs",
            closed_without_events: "DNS query log stream closed before receiving any events",
        },
    )
    .await
}

fn is_replayed_row(timestamp: &str, replay_cutoff: Option<DateTime<Utc>>) -> bool {
    let Some(cutoff) = replay_cutoff else {
        return false;
    };

    DateTime::parse_from_rfc3339(timestamp)
        .map(|ts| ts.with_timezone(&Utc) <= cutoff)
        .unwrap_or(false)
}

struct StreamedLogDedupe {
    seen: HashSet<String>,
    order: VecDeque<String>,
    max_size: usize,
}

impl StreamedLogDedupe {
    fn new(max_size: usize) -> Self {
        Self {
            seen: HashSet::new(),
            order: VecDeque::new(),
            max_size,
        }
    }

    fn insert(&mut self, flow_id: String) -> bool {
        if self.seen.contains(&flow_id) {
            return false;
        }

        self.seen.insert(flow_id.clone());
        self.order.push_back(flow_id);

        while self.order.len() > self.max_size {
            if let Some(oldest) = self.order.pop_front() {
                self.seen.remove(&oldest);
            }
        }

        true
    }
}

fn stream_before_date(max_timestamp: Option<DateTime<Utc>>) -> String {
    let anchor = max_timestamp.unwrap_or_else(Utc::now);
    format_anchored_log_timestamp(anchor - ChronoDuration::seconds(STREAM_LOOKBACK_SECONDS))
}

fn update_max_stream_timestamp(timestamp: &str, max_timestamp: &mut Option<DateTime<Utc>>) {
    let Ok(timestamp) =
        DateTime::parse_from_rfc3339(timestamp).map(|date| date.with_timezone(&Utc))
    else {
        return;
    };

    if max_timestamp.is_none_or(|max| timestamp > max) {
        *max_timestamp = Some(timestamp);
    }
}

async fn wait_for_deployment_removal(deployment_id: &str) {
    loop {
        if let Ok(mut stream) =
            subscribe_graphql::<subscriptions::Deployment>(deployment::Variables {
                id: deployment_id.to_owned(),
            })
            .await
        {
            while let Some(response) = stream.next().await {
                let removed = response.ok().and_then(|r| r.data).is_some_and(|data| {
                    matches!(
                        data.deployment.status,
                        deployment::DeploymentStatus::REMOVED
                            | deployment::DeploymentStatus::REMOVING
                    )
                });
                if removed {
                    return;
                }
            }
        }
        // Subscription failed or ended without seeing removal — retry
        sleep(Duration::from_secs(5)).await;
    }
}

async fn stream_http_logs_inner(
    deployment_id: String,
    filter: Option<String>,
    mut on_log: impl FnMut(http_logs::HttpLogFields),
) -> Result<()> {
    let mut last_timestamp = Some(Utc::now().to_rfc3339_opts(SecondsFormat::Nanos, true));
    let mut seen_request_ids = HashSet::new();
    let mut attempt = 0;
    let mut delay_ms = LOGS_RETRY_CONFIG.initial_delay_ms;

    loop {
        let connected_at = Instant::now();
        let mut received_any_logs = false;
        let anchor_timestamp = last_timestamp
            .clone()
            .unwrap_or_else(|| Utc::now().to_rfc3339_opts(SecondsFormat::Nanos, true));
        let after_timestamp = (Utc::now()
            + chrono::Duration::from_std(HTTP_LOG_STREAM_AFTER_WINDOW).unwrap())
        .to_rfc3339_opts(SecondsFormat::Nanos, true);
        let vars = subscriptions::http_logs::Variables {
            deployment_id: deployment_id.clone(),
            filter: filter.clone(),
            anchor_date: Some(anchor_timestamp),
            after_date: Some(after_timestamp),
            after_limit: Some(HTTP_LOG_STREAM_BATCH_SIZE),
        };

        let mut stream = match subscribe_graphql::<subscriptions::HttpLogs>(vars).await {
            Ok(stream) => stream,
            Err(e) => {
                attempt += 1;

                if attempt >= LOGS_RETRY_CONFIG.max_attempts {
                    return Err(e);
                }

                sleep(Duration::from_millis(delay_ms)).await;
                delay_ms = ((delay_ms as f64 * LOGS_RETRY_CONFIG.backoff_multiplier) as u64)
                    .min(LOGS_RETRY_CONFIG.max_delay_ms);
                continue;
            }
        };

        let result = async {
            while let Some(response) = stream.next().await {
                let log = response
                    .context("HTTP log stream error")?
                    .data
                    .context("Failed to retrieve HTTP logs")?;

                for line in log.http_logs {
                    if !is_new_http_log(
                        &line.timestamp,
                        &line.request_id,
                        &mut last_timestamp,
                        &mut seen_request_ids,
                    ) {
                        continue;
                    }

                    received_any_logs = true;
                    on_log(line);
                }
            }

            Ok::<(), anyhow::Error>(())
        }
        .await;

        let should_reset_retry_state =
            should_reset_stream_retry_state(received_any_logs, connected_at.elapsed());

        if should_reset_retry_state {
            attempt = 0;
            delay_ms = LOGS_RETRY_CONFIG.initial_delay_ms;
        } else {
            attempt += 1;

            match result {
                Err(e) if attempt >= LOGS_RETRY_CONFIG.max_attempts => return Err(e),
                Ok(()) if attempt >= LOGS_RETRY_CONFIG.max_attempts => {
                    return Err(anyhow!(
                        "HTTP log stream closed before receiving any events"
                    ));
                }
                _ => {}
            }
        }

        sleep(Duration::from_millis(delay_ms)).await;

        if !should_reset_retry_state {
            delay_ms = ((delay_ms as f64 * LOGS_RETRY_CONFIG.backoff_multiplier) as u64)
                .min(LOGS_RETRY_CONFIG.max_delay_ms);
        }
    }
}

fn should_reset_stream_retry_state(received_any_logs: bool, connection_duration: Duration) -> bool {
    received_any_logs || connection_duration >= STREAM_STABLE_CONNECTION_DURATION
}

fn is_new_http_log(
    timestamp: &str,
    request_id: &str,
    last_timestamp: &mut Option<String>,
    seen_request_ids: &mut HashSet<String>,
) -> bool {
    if let Some(previous_timestamp) = last_timestamp.as_ref() {
        if timestamp < previous_timestamp.as_str() {
            return false;
        }

        if timestamp == previous_timestamp.as_str() && seen_request_ids.contains(request_id) {
            return false;
        }
    }

    if last_timestamp
        .as_ref()
        .is_none_or(|previous_timestamp| timestamp > previous_timestamp.as_str())
    {
        *last_timestamp = Some(timestamp.to_owned());
        seen_request_ids.clear();
    }

    seen_request_ids.insert(request_id.to_owned());
    true
}

pub async fn stream_deploy_logs(
    deployment_id: String,
    filter: Option<String>,
    mut on_log: impl FnMut(deployment_logs::LogFields),
) -> Result<()> {
    let mut last_timestamp: Option<String> = None;
    let mut attempt = 0;
    let mut delay_ms = LOGS_RETRY_CONFIG.initial_delay_ms;
    let mut received_any_logs = false;

    loop {
        attempt += 1;

        let vars = subscriptions::deployment_logs::Variables {
            deployment_id: deployment_id.clone(),
            filter: filter.clone().or_else(|| Some(String::new())),
            limit: Some(500),
        };

        let result = async {
            let mut stream = subscribe_graphql::<subscriptions::DeploymentLogs>(vars).await?;

            while let Some(response) = stream.next().await {
                let log = response
                    .context("Deploy log stream error")?
                    .data
                    .context("Failed to retrieve deploy log")?;

                for line in log.deployment_logs {
                    if let Some(ref ts) = last_timestamp {
                        if line.timestamp <= *ts {
                            continue;
                        }
                    }
                    last_timestamp = Some(line.timestamp.clone());
                    received_any_logs = true;
                    on_log(line);
                }
            }
            Ok::<(), anyhow::Error>(())
        }
        .await;

        match result {
            Ok(()) => return Ok(()),
            Err(e) if attempt >= LOGS_RETRY_CONFIG.max_attempts => {
                // If we received some logs before the error, treat as success
                // (the deployment likely finished and the stream closed)
                if received_any_logs {
                    return Ok(());
                }
                return Err(e);
            }
            Err(_) => {
                // If we've received logs and then get an error, the deployment likely completed
                // and the stream was closed by the server. Treat this as success.
                if received_any_logs {
                    return Ok(());
                }
                sleep(Duration::from_millis(delay_ms)).await;
                delay_ms = ((delay_ms as f64 * LOGS_RETRY_CONFIG.backoff_multiplier) as u64)
                    .min(LOGS_RETRY_CONFIG.max_delay_ms);
            }
        }
    }
}

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

    fn dt(value: &str) -> DateTime<Utc> {
        DateTime::parse_from_rfc3339(value)
            .unwrap()
            .with_timezone(&Utc)
    }

    #[test]
    fn test_take_last_n_logs_with_limit() {
        let logs = vec!["log1", "log2", "log3", "log4", "log5"];

        // Request 3 logs from 5
        let result = take_last_n_logs(logs.clone(), Some(3));
        assert_eq!(result, vec!["log3", "log4", "log5"]);

        // Request 2 logs from 5
        let result = take_last_n_logs(logs.clone(), Some(2));
        assert_eq!(result, vec!["log4", "log5"]);
    }

    #[test]
    fn test_take_last_n_logs_limit_exceeds_size() {
        let logs = vec!["log1", "log2", "log3"];

        // Request 5 logs but only 3 available
        let result = take_last_n_logs(logs.clone(), Some(5));
        assert_eq!(result, vec!["log1", "log2", "log3"]);
    }

    #[test]
    fn test_take_last_n_logs_no_limit() {
        let logs = vec!["log1", "log2", "log3"];

        // No limit specified, return all
        let result = take_last_n_logs(logs.clone(), None);
        assert_eq!(result, vec!["log1", "log2", "log3"]);
    }

    #[test]
    fn test_take_last_n_logs_empty_vec() {
        let logs: Vec<String> = vec![];

        // Empty input with limit
        let result = take_last_n_logs(logs.clone(), Some(5));
        assert_eq!(result, Vec::<String>::new());

        // Empty input without limit
        let result = take_last_n_logs(logs, None);
        assert_eq!(result, Vec::<String>::new());
    }

    #[test]
    fn test_take_last_n_logs_limit_zero() {
        let logs = vec!["log1", "log2", "log3"];

        // Limit of 0 should return empty vec
        let result = take_last_n_logs(logs, Some(0));
        assert_eq!(result, Vec::<&str>::new());
    }

    #[test]
    fn test_network_flow_log_window_uses_explicit_bounded_range() {
        let start = dt("2026-06-18T04:41:00Z");
        let end = dt("2026-06-18T04:42:00Z");
        let now = dt("2026-06-18T04:43:00Z");

        let window = anchored_log_window(Some(100), Some(start), Some(end), now);

        assert_eq!(
            window,
            AnchoredLogWindow {
                before_limit: Some(100),
                before_date: Some("2026-06-18T04:41:00.000000000Z".to_string()),
                anchor_date: Some("2026-06-18T04:42:00.000000000Z".to_string()),
                after_date: Some("2026-06-18T04:42:00.000000000Z".to_string()),
                after_limit: Some(0),
            }
        );
    }

    #[test]
    fn test_network_flow_log_window_resolves_open_ended_ranges() {
        let start = dt("2026-06-18T04:41:00Z");
        let end = dt("2026-06-18T04:42:00Z");
        let now = dt("2026-06-18T04:43:00Z");

        let since_window = anchored_log_window(None, Some(start), None, now);
        assert_eq!(
            since_window,
            AnchoredLogWindow {
                before_limit: Some(ANCHORED_LOG_DEFAULT_LIMIT),
                before_date: Some("2026-06-18T04:41:00.000000000Z".to_string()),
                anchor_date: Some("2026-06-18T04:43:00.000000000Z".to_string()),
                after_date: Some("2026-06-18T04:43:00.000000000Z".to_string()),
                after_limit: Some(0),
            }
        );

        let until_window = anchored_log_window(None, None, Some(end), now);
        assert_eq!(
            until_window,
            AnchoredLogWindow {
                before_limit: Some(ANCHORED_LOG_DEFAULT_LIMIT),
                before_date: Some("1970-01-01T00:00:00.000000000Z".to_string()),
                anchor_date: Some("2026-06-18T04:42:00.000000000Z".to_string()),
                after_date: Some("2026-06-18T04:42:00.000000000Z".to_string()),
                after_limit: Some(0),
            }
        );
    }

    #[test]
    fn test_network_flow_log_window_leaves_unbounded_snapshot_to_api_defaults() {
        let now = dt("2026-06-18T04:43:00Z");

        let window = anchored_log_window(Some(20), None, None, now);

        assert_eq!(
            window,
            AnchoredLogWindow {
                before_limit: Some(20),
                before_date: None,
                anchor_date: None,
                after_date: None,
                after_limit: None,
            }
        );
    }

    #[test]
    fn test_network_flow_dedupe_keeps_out_of_order_sibling_flows() {
        let mut dedupe = StreamedLogDedupe::new(10);

        assert!(dedupe.insert("newer-flow".to_string()));
        assert!(dedupe.insert("older-sibling-flow".to_string()));
        assert!(!dedupe.insert("newer-flow".to_string()));
    }

    #[test]
    fn test_network_flow_dedupe_bounds_cache_size() {
        let mut dedupe = StreamedLogDedupe::new(2);

        assert!(dedupe.insert("flow-1".to_string()));
        assert!(dedupe.insert("flow-2".to_string()));
        assert!(dedupe.insert("flow-3".to_string()));
        assert!(dedupe.insert("flow-1".to_string()));
    }

    #[test]
    fn test_network_flow_stream_before_date_uses_lookback() {
        let before_date = stream_before_date(Some(dt("2026-06-18T04:43:00Z")));

        assert_eq!(before_date, "2026-06-18T04:42:30.000000000Z");
    }

    #[test]
    fn test_update_max_network_flow_capture_end_ignores_older_rows() {
        let mut max_capture_end = Some(dt("2026-06-18T04:43:00Z"));

        update_max_stream_timestamp("2026-06-18T04:42:30Z", &mut max_capture_end);
        assert_eq!(max_capture_end, Some(dt("2026-06-18T04:43:00Z")));

        update_max_stream_timestamp("2026-06-18T04:43:30Z", &mut max_capture_end);
        assert_eq!(max_capture_end, Some(dt("2026-06-18T04:43:30Z")));
    }

    #[test]
    fn test_is_new_http_log_skips_old_and_duplicate_entries() {
        let mut last_timestamp = Some("2025-01-01T00:00:00Z".to_string());
        let mut seen_request_ids = HashSet::from(["req-1".to_string()]);

        assert!(!is_new_http_log(
            "2024-12-31T23:59:59Z",
            "req-old",
            &mut last_timestamp,
            &mut seen_request_ids,
        ));
        assert!(!is_new_http_log(
            "2025-01-01T00:00:00Z",
            "req-1",
            &mut last_timestamp,
            &mut seen_request_ids,
        ));
        assert!(is_new_http_log(
            "2025-01-01T00:00:00Z",
            "req-2",
            &mut last_timestamp,
            &mut seen_request_ids,
        ));
    }

    #[test]
    fn test_is_new_http_log_advances_timestamp_window() {
        let mut last_timestamp = Some("2025-01-01T00:00:00Z".to_string());
        let mut seen_request_ids = HashSet::from(["req-1".to_string()]);

        assert!(is_new_http_log(
            "2025-01-01T00:00:01Z",
            "req-3",
            &mut last_timestamp,
            &mut seen_request_ids,
        ));
        assert_eq!(last_timestamp.as_deref(), Some("2025-01-01T00:00:01Z"));
        assert_eq!(seen_request_ids, HashSet::from(["req-3".to_string()]));
    }

    #[test]
    fn test_should_reset_stream_retry_state_after_logs() {
        assert!(should_reset_stream_retry_state(
            true,
            Duration::from_secs(1),
        ));
    }

    #[test]
    fn test_should_reset_stream_retry_state_after_stable_connection() {
        assert!(should_reset_stream_retry_state(
            false,
            STREAM_STABLE_CONNECTION_DURATION,
        ));
    }

    #[test]
    fn test_should_not_reset_stream_retry_state_for_short_empty_connection() {
        assert!(!should_reset_stream_retry_state(
            false,
            Duration::from_secs(1),
        ));
    }

    #[test]
    fn test_replay_only_anchored_rows_do_not_reset_retry_state() {
        let replay_cutoff = Some(dt("2026-06-18T04:43:00Z"));
        let mut max_timestamp = replay_cutoff;
        let mut seen_rows = HashSet::from(["replayed-row".to_string()]);

        let emitted_any_logs = process_anchored_stream_lines(
            vec![(
                "replayed-row".to_string(),
                "2026-06-18T04:43:00Z".to_string(),
            )],
            &|line: &(String, String)| line.1.as_str(),
            replay_cutoff,
            &mut max_timestamp,
            &mut |line, cutoff| {
                let already_seen = !seen_rows.insert(line.0);
                !(already_seen && is_replayed_row(&line.1, cutoff))
            },
        );

        assert!(!emitted_any_logs);
        assert!(!should_reset_stream_retry_state(
            emitted_any_logs,
            Duration::from_secs(1),
        ));
    }

    #[test]
    fn test_is_replayed_row_only_flags_rows_at_or_before_cutoff() {
        let cutoff = Some(dt("2026-06-18T04:43:00Z"));

        assert!(is_replayed_row("2026-06-18T04:42:59Z", cutoff));
        assert!(is_replayed_row("2026-06-18T04:43:00Z", cutoff));
        assert!(!is_replayed_row("2026-06-18T04:43:01Z", cutoff));
    }

    #[test]
    fn test_is_replayed_row_fails_open_without_cutoff_or_valid_timestamp() {
        assert!(!is_replayed_row("2026-06-18T04:42:59Z", None));
        assert!(!is_replayed_row(
            "not-a-timestamp",
            Some(dt("2026-06-18T04:43:00Z"))
        ));
    }
}