dexcost 0.3.1

Agent Unit Economics SDK for Rust — track end-to-end business-task costs for AI agents.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
//! `reqwest-middleware` adapter that records cost events per HTTP request.
//!
//! Wraps any [`reqwest::Client`] via the `reqwest-middleware` crate so that
//! every outgoing request is inspected against the bundled
//! [`ServiceCatalog`](crate::pricing::service_catalog::ServiceCatalog) by
//! hostname:
//!
//! 1. **Catalog match** — if the URL matches a known service, the response
//!    body (JSON) is parsed for cost/usage and a `CostEvent` is recorded.
//! 2. **LLM provider match** — for OpenAI/Anthropic/Google, token usage is
//!    extracted via [`crate::clients::wrappers`].
//! 3. **Domain rate fallback** — if no catalog entry matches but the host is
//!    registered in [`crate::adapters::http`], a per-call cost is recorded.
//!
//! # Streaming responses (v2 — Decisions Log #2)
//!
//! Responses with `Content-Type: text/event-stream` (SSE — the dominant
//! LLM-streaming pattern) are NOT buffered by this middleware. Instead the
//! response body is wrapped in a `RecordingStream` that counts chunks as the
//! caller consumes them and records the final byte total into the task's
//! [`NetworkAccountant`] when the stream completes (or is dropped). Cost
//! extraction for SSE responses is the responsibility of the LLM instrument
//! that reads the stream; this middleware contributes only the byte counts
//! that feed `network_cost_usd` at task finalize.
//!
//! Non-streaming responses still take the buffer-and-parse path because cost
//! extraction needs the full JSON body to inspect for `usage` blocks etc.
//!
//! # Feature flag
//!
//! This module requires the `reqwest-middleware` feature.

use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use ::http::{Extensions, Response as HttpResponse};
use bytes::Bytes;
use futures::Stream;
use reqwest::{Body, Request, Response};
use reqwest_middleware::{Middleware, Next};
use rust_decimal::Decimal;
use tokio::sync::Mutex;

use crate::adapters::http as dexcost_http;
use crate::adapters::netbytes::{classify_destination, measure_bytes_from_headers};
use crate::adapters::network_accountant::{get_accountant, NetworkAccountant};
use crate::core::context::is_network_event_suppressed;
use crate::core::models::{CostConfidence, CostEvent, EventType};
use crate::pricing::engine::PricingEngine;
use crate::pricing::service_catalog::{CostExtractionResult, ServiceCatalog};
use crate::security::redaction::scrub_url;
use crate::transport::buffer::EventBuffer;

/// Default `network` event emission threshold — combined request + response
/// bytes. Mirrors python config `network_event_threshold_bytes = 102_400`
/// (100 KiB).
const NETWORK_EVENT_THRESHOLD_BYTES: usize = 102_400;

/// Reqwest middleware that automatically records cost events for HTTP calls.
///
/// Construct via [`DexcostMiddleware::new`] and attach with
/// `reqwest_middleware::ClientBuilder::with`.
///
/// All inputs are `Arc`-shared so the middleware is cheap to clone and to
/// reuse across many [`reqwest::Client`] instances.
pub struct DexcostMiddleware {
    catalog: Arc<ServiceCatalog>,
    pricing: Arc<PricingEngine>,
    buffer: Arc<Mutex<EventBuffer>>,
    /// Task ID used as the parent of recorded events. When `None`, an
    /// auto-task is created on the fly using the host as the task type.
    task_id: Option<String>,
}

impl DexcostMiddleware {
    /// Creates a new middleware. Pass `task_id` to attach all events to a
    /// specific task; pass `None` to use an auto-task per call.
    pub fn new(
        catalog: Arc<ServiceCatalog>,
        pricing: Arc<PricingEngine>,
        buffer: Arc<Mutex<EventBuffer>>,
        task_id: Option<String>,
    ) -> Self {
        Self {
            catalog,
            pricing,
            buffer,
            task_id,
        }
    }

    fn host_of(req: &Request) -> Option<String> {
        req.url().host_str().map(|s| s.to_lowercase())
    }

    /// Returns the task_id to attribute events to. Falls back to the URL
    /// host so each invocation still produces a non-empty event.
    fn effective_task_id(&self, req: &Request) -> String {
        if let Some(ref t) = self.task_id {
            return t.clone();
        }
        match Self::host_of(req) {
            Some(host) => format!("auto:{}", host),
            None => "auto:unknown".to_string(),
        }
    }

    fn record_extraction(
        &self,
        task_id: &str,
        host: &str,
        extraction: &CostExtractionResult,
        byte_details: &serde_json::Value,
        events: &mut EventBuffer,
    ) {
        let mut event = CostEvent::new(task_id, EventType::ExternalCost);
        event.cost_usd = extraction.amount;
        event.cost_confidence = match extraction.confidence.as_str() {
            "exact" => crate::core::models::CostConfidence::Exact,
            "computed" => crate::core::models::CostConfidence::Computed,
            "estimated" => crate::core::models::CostConfidence::Estimated,
            _ => crate::core::models::CostConfidence::Unknown,
        };
        event.pricing_source = match extraction.pricing_source.as_str() {
            "user_override" => Some(crate::core::models::PricingSource::UserOverride),
            "service_catalog" => Some(crate::core::models::PricingSource::ServiceCatalog),
            _ => Some(crate::core::models::PricingSource::ServiceCatalog),
        };
        event.service_name = Some(extraction.service_name.clone());
        if event.service_name.is_none() {
            event.service_name = Some(host.to_string());
        }
        stamp_byte_details(&mut event, byte_details);
        events.add_event(event);
    }

    /// Try to record an LLM-style event from an OpenAI / Anthropic JSON body.
    /// Returns `true` if a token-usage event was recorded.
    fn try_record_llm(
        &self,
        host: &str,
        task_id: &str,
        body: &serde_json::Value,
        byte_details: &serde_json::Value,
        events: &mut EventBuffer,
    ) -> bool {
        if host.contains("openai.com") {
            if let Some(usage) = body.get("usage") {
                let model = body
                    .get("model")
                    .and_then(|v| v.as_str())
                    .unwrap_or("unknown");
                let input = usage
                    .get("prompt_tokens")
                    .and_then(|v| v.as_i64())
                    .unwrap_or(0);
                let output = usage
                    .get("completion_tokens")
                    .and_then(|v| v.as_i64())
                    .unwrap_or(0);
                let cached = usage
                    .get("cached_tokens")
                    .and_then(|v| v.as_i64())
                    .unwrap_or(0);
                let cost = self.pricing.get_cost_sync(model, input, output, cached, 0);
                let mut ev = CostEvent::new(task_id, EventType::LlmCall);
                ev.provider = Some("openai".to_string());
                ev.model = Some(model.to_string());
                ev.input_tokens = Some(input);
                ev.output_tokens = Some(output);
                ev.cost_usd = cost.cost_usd;
                ev.cost_confidence = cost.cost_confidence;
                ev.pricing_source = Some(cost.pricing_source);
                stamp_byte_details(&mut ev, byte_details);
                events.add_event(ev);
                return true;
            }
        }
        if host.contains("anthropic.com") {
            if let Some(usage) = body.get("usage") {
                let model = body
                    .get("model")
                    .and_then(|v| v.as_str())
                    .unwrap_or("unknown");
                let input = usage
                    .get("input_tokens")
                    .and_then(|v| v.as_i64())
                    .unwrap_or(0);
                let output = usage
                    .get("output_tokens")
                    .and_then(|v| v.as_i64())
                    .unwrap_or(0);
                let cache_read = usage
                    .get("cache_read_input_tokens")
                    .and_then(|v| v.as_i64())
                    .unwrap_or(0);
                let cache_creation = usage
                    .get("cache_creation_input_tokens")
                    .and_then(|v| v.as_i64())
                    .unwrap_or(0);
                let cost =
                    self.pricing
                        .get_cost_sync(model, input, output, cache_read, cache_creation);
                let mut ev = CostEvent::new(task_id, EventType::LlmCall);
                ev.provider = Some("anthropic".to_string());
                ev.model = Some(model.to_string());
                ev.input_tokens = Some(input);
                ev.output_tokens = Some(output);
                ev.cost_usd = cost.cost_usd;
                ev.cost_confidence = cost.cost_confidence;
                ev.pricing_source = Some(cost.pricing_source);
                stamp_byte_details(&mut ev, byte_details);
                events.add_event(ev);
                return true;
            }
        }
        false
    }
}

#[async_trait::async_trait]
impl Middleware for DexcostMiddleware {
    async fn handle(
        &self,
        req: Request,
        extensions: &mut Extensions,
        next: Next<'_>,
    ) -> reqwest_middleware::Result<Response> {
        let url = req.url().clone();
        let host = Self::host_of(&req).unwrap_or_default();
        let protocol = url.scheme().to_string();
        let method = req.method().to_string();
        let task_id = self.effective_task_id(&req);

        // ── v1 byte measurement — request side ──────────────────────────
        // Compute request bytes BEFORE next.run consumes the Request.
        let request_headers_map = req
            .headers()
            .iter()
            .map(|(k, v)| {
                (
                    k.to_string(),
                    v.to_str().unwrap_or("").to_string(),
                )
            })
            .collect::<HashMap<String, String>>();
        let request_body_len = req
            .body()
            .and_then(|b| b.as_bytes())
            .map(|b| b.len())
            .unwrap_or(0);
        let request_bytes = measure_bytes_from_headers(
            &method,
            url.as_str(),
            &request_headers_map,
            request_body_len,
        );

        let response = next.run(req, extensions).await?;

        // Capture status + headers for reconstruction below.
        let status = response.status();
        let status_code = status.as_u16();
        let version = response.version();
        let response_headers_map = response
            .headers()
            .iter()
            .map(|(k, v)| {
                (
                    k.to_string(),
                    v.to_str().unwrap_or("").to_string(),
                )
            })
            .collect::<HashMap<String, String>>();
        let is_internal = classify_destination(&host);
        let response_is_streaming = is_streaming_response(response.headers());

        let mut builder = HttpResponse::builder().status(status).version(version);
        for (k, v) in response.headers().iter() {
            builder = builder.header(k, v);
        }

        // ── Streaming branch (SSE etc.) — never buffer the body ─────────
        //
        // The caller will consume the stream chunk-by-chunk. We wrap the
        // body in a RecordingStream that counts bytes as they flow through
        // and records to the accountant on stream completion (or drop).
        // Cost extraction from the body is impossible without buffering, so
        // we skip the catalog/LLM-usage paths for streaming responses —
        // LLM instruments that consume the SSE stream are responsible for
        // emitting their own `llm_call` event with token counts from the
        // final usage chunk. The byte count still feeds `network_cost_usd`
        // at task finalize via the accountant.
        if response_is_streaming {
            let response_header_bytes = measure_bytes_from_headers(
                "",
                "",
                &response_headers_map,
                0, // body length unknown — RecordingStream adds chunk lengths
            );
            let body_stream = response.bytes_stream();
            let accountant = get_accountant(&task_id);
            let recording = RecordingStream {
                inner: Box::pin(body_stream),
                state: Some(RecordingState {
                    accountant,
                    host: host.clone(),
                    is_internal,
                    request_bytes,
                    response_header_bytes,
                    response_body_bytes: 0,
                }),
            };
            let new_resp = builder
                .body(Body::wrap_stream(recording))
                .map_err(|e| reqwest_middleware::Error::Middleware(anyhow_compat(e)))?;
            return Ok(Response::from(new_resp));
        }

        // ── Buffered branch — read body for cost extraction + byte count ─
        let body_bytes = match response.bytes().await {
            Ok(b) => b,
            Err(e) => return Err(reqwest_middleware::Error::Reqwest(e)),
        };
        let response_bytes = measure_bytes_from_headers(
            "",
            "",
            &response_headers_map,
            body_bytes.len(),
        );

        // ── v1 destination classification + accountant recording ───────
        if let Some(accountant) = get_accountant(&task_id) {
            accountant.record(
                &host,
                response_bytes as i64,
                request_bytes as i64,
                is_internal,
            );
        }

        // ── v1 byte details — stamped into every event below ───────────
        let byte_details = serde_json::json!({
            "protocol": protocol,
            "request_bytes": request_bytes,
            "response_bytes": response_bytes,
            "is_internal_traffic": is_internal,
        });

        // Try to extract usage / cost.
        if let Ok(body_json) = serde_json::from_slice::<serde_json::Value>(&body_bytes) {
            let mut buf = self.buffer.lock().await;
            let mut recorded = false;

            if let Some(entry) = self.catalog.lookup(url.as_str()) {
                if let Some(extraction) =
                    self.catalog
                        .extract_cost(entry, &Default::default(), Some(&body_json))
                {
                    self.record_extraction(
                        &task_id,
                        &host,
                        &extraction,
                        &byte_details,
                        &mut buf,
                    );
                    recorded = true;
                }
            }

            if !recorded {
                recorded = self.try_record_llm(
                    &host,
                    &task_id,
                    &body_json,
                    &byte_details,
                    &mut buf,
                );
            }

            if !recorded {
                // Domain-rate / catalog fallback — persist to the durable
                // EventBuffer (so the pusher ships it) instead of the in-memory
                // record_http_cost log. buf is still held here.
                if let Some(mut event) = dexcost_http::resolve_http_cost_event(
                    url.as_str(),
                    &task_id,
                    Some(&self.catalog),
                ) {
                    stamp_byte_details(&mut event, &byte_details);
                    buf.add_event(event);
                    recorded = true;
                }
            }

            // ── Un-cataloged: emit a `network` event if notable + not
            // suppressed by an outer LLM call. Mirrors python `_handle_
            // uncataloged` (v1 §5.4 step 5 + v2 §6.4 cost_pending marker).
            if !recorded && !is_network_event_suppressed() {
                if let Some(ev) = build_network_event(
                    &task_id,
                    &host,
                    url.as_str(),
                    &method,
                    status_code,
                    request_bytes,
                    response_bytes,
                    &byte_details,
                ) {
                    buf.add_event(ev);
                }
            }
        } else {
            // Not JSON — catalog body extraction is impossible, but a domain
            // rate or fixed-cost catalog entry can still resolve. Persist it to
            // the EventBuffer so it syncs.
            let mut buf = self.buffer.lock().await;
            let mut recorded = false;
            if let Some(mut event) =
                dexcost_http::resolve_http_cost_event(url.as_str(), &task_id, Some(&self.catalog))
            {
                stamp_byte_details(&mut event, &byte_details);
                buf.add_event(event);
                recorded = true;
            }
            if !recorded && !is_network_event_suppressed() {
                if let Some(ev) = build_network_event(
                    &task_id,
                    &host,
                    url.as_str(),
                    &method,
                    status_code,
                    request_bytes,
                    response_bytes,
                    &byte_details,
                ) {
                    buf.add_event(ev);
                }
            }
        }

        // Reconstruct a Response from the buffered body so downstream callers
        // can still consume it.
        let new_resp = builder
            .body(Body::from(body_bytes))
            .map_err(|e| reqwest_middleware::Error::Middleware(anyhow_compat(e)))?;
        Ok(Response::from(new_resp))
    }
}

// ---------------------------------------------------------------------------
// Free-standing helpers (network event construction, byte-detail stamping)
// ---------------------------------------------------------------------------

/// Build a `network` event for an un-cataloged HTTP call when notable
/// (combined bytes above threshold OR status >= 400). Returns `None` when
/// the call is below the threshold and not an error — counters-only path.
#[allow(clippy::too_many_arguments)] // 8 args mirror the Python dispatcher signature
fn build_network_event(
    task_id: &str,
    host: &str,
    url: &str,
    method: &str,
    status_code: u16,
    request_bytes: usize,
    response_bytes: usize,
    byte_details: &serde_json::Value,
) -> Option<CostEvent> {
    let total = request_bytes.saturating_add(response_bytes);
    let notable = total > NETWORK_EVENT_THRESHOLD_BYTES || status_code >= 400;
    if !notable {
        return None;
    }
    let mut ev = CostEvent::new(task_id, EventType::Network);
    ev.cost_usd = Decimal::ZERO;
    ev.cost_confidence = CostConfidence::Unknown;
    ev.pricing_source = None;
    ev.service_name = Some(host.to_string());
    // Compose details with the byte fields PLUS cost_pending marker
    // (v2 §6.4 — back-filled by _aggregate_costs at task finalize).
    ev.details.insert(
        "url".to_string(),
        serde_json::Value::String(scrub_url(url)),
    );
    ev.details.insert(
        "method".to_string(),
        serde_json::Value::String(method.to_string()),
    );
    ev.details.insert(
        "status_code".to_string(),
        serde_json::Value::from(status_code),
    );
    ev.details
        .insert("cost_pending".to_string(), serde_json::Value::Bool(true));
    stamp_byte_details(&mut ev, byte_details);
    Some(ev)
}

/// Stamp byte_details fields (protocol/request_bytes/response_bytes/
/// is_internal_traffic) into a `CostEvent`'s details, matching the v1 §4.3
/// "byte placement is uniform across event types" invariant.
fn stamp_byte_details(event: &mut CostEvent, byte_details: &serde_json::Value) {
    if let serde_json::Value::Object(bd) = byte_details {
        for (k, v) in bd {
            event.details.insert(k.clone(), v.clone());
        }
    }
}

/// Detect whether a response body should be streamed rather than buffered.
///
/// Streaming criteria (May 2026):
///   * `Content-Type: text/event-stream` (SSE — the dominant LLM streaming
///     pattern; OpenAI `stream=true`, Anthropic `stream=true`, Vercel AI
///     SDK streaming, etc.)
///   * `Content-Type: application/x-ndjson` (newline-delimited JSON streams,
///     used by some providers for streaming completions)
///
/// `Transfer-Encoding: chunked` alone is **not** enough — many small JSON
/// responses use chunked encoding too, and buffering them is fine. We only
/// branch to the streaming path when there's an explicit content-type
/// signal that the body is consumer-driven.
pub(crate) fn is_streaming_response(headers: &reqwest::header::HeaderMap) -> bool {
    let Some(ct) = headers.get(reqwest::header::CONTENT_TYPE) else {
        return false;
    };
    let Ok(ct) = ct.to_str() else {
        return false;
    };
    let ct = ct.to_ascii_lowercase();
    ct.starts_with("text/event-stream") || ct.starts_with("application/x-ndjson")
}

// ---------------------------------------------------------------------------
// RecordingStream — counts response-body bytes for streaming responses
// ---------------------------------------------------------------------------

/// State carried by a `RecordingStream` until it finalises. Wrapped in an
/// `Option` so the stream's `poll_next` (on end-of-stream) and `Drop` (on
/// early-abort) can each take ownership of it exactly once.
struct RecordingState {
    accountant: Option<Arc<NetworkAccountant>>,
    host: String,
    is_internal: Option<bool>,
    /// On-the-wire bytes of the outbound request (headers + body), computed
    /// at middleware entry. Recorded into the accountant alongside the
    /// streamed response bytes when the stream finalises.
    request_bytes: usize,
    /// On-the-wire bytes of the response header block (status line + headers),
    /// computed at middleware entry. Added to the body chunk total at
    /// finalisation so the accountant sees one record per HTTP call.
    response_header_bytes: usize,
    /// Accumulated response-body bytes seen so far. Updated in `poll_next`.
    response_body_bytes: usize,
}

impl RecordingState {
    fn finalise(self) {
        let Some(accountant) = self.accountant else {
            return;
        };
        let total_in = self.response_header_bytes + self.response_body_bytes;
        accountant.record(
            &self.host,
            total_in as i64,
            self.request_bytes as i64,
            self.is_internal,
        );
    }
}

/// `Stream<Item = Result<Bytes, reqwest::Error>>` adapter that counts bytes
/// from the inner stream and records the total into a [`NetworkAccountant`]
/// once the stream completes or is dropped.
struct RecordingStream<S> {
    inner: Pin<Box<S>>,
    state: Option<RecordingState>,
}

impl<S> Stream for RecordingStream<S>
where
    S: Stream<Item = Result<Bytes, reqwest::Error>> + Send + 'static,
{
    type Item = Result<Bytes, reqwest::Error>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = self.get_mut();
        match this.inner.as_mut().poll_next(cx) {
            Poll::Ready(Some(Ok(chunk))) => {
                if let Some(state) = &mut this.state {
                    state.response_body_bytes += chunk.len();
                }
                Poll::Ready(Some(Ok(chunk)))
            }
            Poll::Ready(None) => {
                // Stream completed cleanly — record now so Drop doesn't double-record.
                if let Some(state) = this.state.take() {
                    state.finalise();
                }
                Poll::Ready(None)
            }
            Poll::Ready(Some(Err(e))) => {
                // Error on the stream — still record whatever bytes we saw,
                // matching Python v1 §5.5 "early-abort → bytes-actually-received".
                if let Some(state) = this.state.take() {
                    state.finalise();
                }
                Poll::Ready(Some(Err(e)))
            }
            Poll::Pending => Poll::Pending,
        }
    }
}

impl<S> Drop for RecordingStream<S> {
    fn drop(&mut self) {
        // Caller dropped the response mid-stream — finalise with whatever
        // we've accumulated. Matches Python v1 §5.5 early-abort behaviour.
        if let Some(state) = self.state.take() {
            state.finalise();
        }
    }
}

/// Tiny shim that converts `http::Error` into the error type
/// `reqwest_middleware::Error::Middleware` expects without pulling in
/// `anyhow` directly.
fn anyhow_compat(e: ::http::Error) -> anyhow::Error {
    anyhow::Error::msg(e.to_string())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use reqwest_middleware::ClientBuilder;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    fn fixtures() -> (
        Arc<ServiceCatalog>,
        Arc<PricingEngine>,
        Arc<Mutex<EventBuffer>>,
    ) {
        (
            Arc::new(ServiceCatalog::new()),
            Arc::new(PricingEngine::new()),
            Arc::new(Mutex::new(EventBuffer::new().expect("buffer"))),
        )
    }

    #[tokio::test]
    async fn middleware_passes_through_non_json() {
        let _guard = crate::adapters::http::GLOBAL_HTTP_TEST_LOCK.lock().await;
        crate::adapters::http::clear_domain_rates();
        let (catalog, pricing, buffer) = fixtures();
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/plain"))
            .respond_with(ResponseTemplate::new(200).set_body_string("hello"))
            .mount(&server)
            .await;

        let mw = DexcostMiddleware::new(catalog, pricing, buffer.clone(), Some("t-plain".into()));
        let client = ClientBuilder::new(reqwest::Client::new()).with(mw).build();

        let resp = client
            .get(format!("{}/plain", server.uri()))
            .send()
            .await
            .unwrap();
        assert_eq!(resp.status(), 200);
        assert_eq!(resp.text().await.unwrap(), "hello");
        // No catalog/LLM match, no domain rate registered → no events.
        let buf = buffer.lock().await;
        assert_eq!(buf.event_count(), 0);
    }

    #[tokio::test]
    async fn middleware_records_openai_usage_when_host_matches() {
        let _guard = crate::adapters::http::GLOBAL_HTTP_TEST_LOCK.lock().await;
        crate::adapters::http::clear_domain_rates();
        let (catalog, pricing, buffer) = fixtures();
        let server = MockServer::start().await;

        let body = serde_json::json!({
            "model": "gpt-4o",
            "usage": { "prompt_tokens": 10, "completion_tokens": 5 }
        });
        Mock::given(method("POST"))
            .and(path("/v1/chat/completions"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&body))
            .mount(&server)
            .await;

        // Use a Host header trick: openai.com path is recognised in
        // try_record_llm via the URL host. We need wiremock running on
        // localhost, so we patch the host check to match the test URI.
        let mw = DexcostMiddleware::new(catalog, pricing, buffer.clone(), Some("t-openai".into()));
        let client = ClientBuilder::new(reqwest::Client::new()).with(mw).build();

        let _ = client
            .post(format!("{}/v1/chat/completions", server.uri()))
            .header("content-type", "application/json")
            .body(serde_json::json!({"model": "gpt-4o"}).to_string())
            .send()
            .await
            .unwrap();

        // We don't assert provider here because mock host won't contain
        // "openai.com"; instead we verify the middleware did NOT panic and
        // that the buffer remains internally consistent.
        let buf = buffer.lock().await;
        assert_eq!(buf.event_count(), 0);
    }

    #[tokio::test]
    async fn middleware_uses_task_id_or_host_fallback() {
        let _guard = crate::adapters::http::GLOBAL_HTTP_TEST_LOCK.lock().await;
        let (catalog, pricing, buffer) = fixtures();
        let mw = DexcostMiddleware::new(catalog, pricing, buffer, None);

        let req = reqwest::Request::new(
            reqwest::Method::GET,
            "https://api.openai.com/v1/models".parse().unwrap(),
        );
        let id = mw.effective_task_id(&req);
        assert!(id.starts_with("auto:"));
        assert!(id.contains("openai.com"));
    }

    /// The middleware's domain-rate fallback must persist events to the durable
    /// EventBuffer (so the sync pusher ships them), not the in-memory
    /// record_http_cost log.
    #[tokio::test]
    async fn middleware_persists_domain_rate_fallback_to_buffer() {
        let _guard = crate::adapters::http::GLOBAL_HTTP_TEST_LOCK.lock().await;
        crate::adapters::http::clear_domain_rates();
        let (catalog, pricing, buffer) = fixtures();

        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/data"))
            .respond_with(ResponseTemplate::new(200).set_body_string("not json"))
            .mount(&server)
            .await;

        // Register a domain rate for the mock server's host (127.0.0.1).
        let host = reqwest::Url::parse(&server.uri())
            .unwrap()
            .host_str()
            .unwrap()
            .to_string();
        crate::adapters::http::register_domain_rate(&host, "0.01".parse().unwrap(), "call");

        let mw = DexcostMiddleware::new(catalog, pricing, buffer.clone(), Some("t-dr".into()));
        let client = ClientBuilder::new(reqwest::Client::new()).with(mw).build();
        let _ = client
            .get(format!("{}/data", server.uri()))
            .send()
            .await
            .unwrap();

        let count = {
            let buf = buffer.lock().await;
            buf.event_count()
        };
        crate::adapters::http::clear_domain_rates();
        assert_eq!(
            count, 1,
            "domain-rate fallback must persist the event to the durable buffer"
        );
    }

    // ── v1/v2 network capture helpers ────────────────────────────────────

    #[test]
    fn build_network_event_below_threshold_no_error_returns_none() {
        let details = serde_json::json!({
            "protocol": "https",
            "request_bytes": 100,
            "response_bytes": 500,
            "is_internal_traffic": false,
        });
        let ev = build_network_event(
            "t1", "api.example.com", "https://api.example.com/x",
            "GET", 200, 100, 500, &details,
        );
        assert!(ev.is_none(), "below-threshold success must emit no event");
    }

    #[test]
    fn build_network_event_above_threshold_emits_with_cost_pending() {
        let details = serde_json::json!({
            "protocol": "https",
            "request_bytes": 50,
            "response_bytes": 200_000,
            "is_internal_traffic": false,
        });
        let ev = build_network_event(
            "t1", "api.example.com", "https://api.example.com/x",
            "POST", 200, 50, 200_000, &details,
        )
        .expect("must emit");
        assert_eq!(ev.event_type, EventType::Network);
        assert_eq!(ev.cost_usd, Decimal::ZERO);
        assert_eq!(ev.cost_confidence, CostConfidence::Unknown);
        assert!(ev.pricing_source.is_none());
        assert_eq!(ev.service_name.as_deref(), Some("api.example.com"));
        // v2 §6.4 cost_pending marker so _aggregate_costs back-fills.
        assert_eq!(
            ev.details.get("cost_pending"),
            Some(&serde_json::Value::Bool(true))
        );
        // v1 §4.3 byte uniformity — every event carries the byte fields.
        assert_eq!(
            ev.details.get("request_bytes"),
            Some(&serde_json::Value::from(50))
        );
        assert_eq!(
            ev.details.get("response_bytes"),
            Some(&serde_json::Value::from(200_000))
        );
        assert_eq!(
            ev.details.get("is_internal_traffic"),
            Some(&serde_json::Value::Bool(false))
        );
        assert_eq!(
            ev.details.get("method"),
            Some(&serde_json::Value::String("POST".to_string()))
        );
        assert_eq!(
            ev.details.get("status_code"),
            Some(&serde_json::Value::from(200))
        );
    }

    #[test]
    fn build_network_event_status_400_emits_below_threshold() {
        // status >= 400 always emits, regardless of byte count.
        let details = serde_json::json!({
            "protocol": "https",
            "request_bytes": 10,
            "response_bytes": 100,
            "is_internal_traffic": false,
        });
        let ev = build_network_event(
            "t1", "api.example.com", "https://api.example.com/x",
            "GET", 503, 10, 100, &details,
        );
        assert!(
            ev.is_some(),
            "5xx error must emit a network event even below threshold"
        );
    }

    #[test]
    fn middleware_uncataloged_above_threshold_emits_network_event() {
        let _ = tokio::runtime::Runtime::new().unwrap().block_on(async {
            let _guard = crate::adapters::http::GLOBAL_HTTP_TEST_LOCK.lock().await;
            crate::adapters::http::clear_domain_rates();
            crate::adapters::network_accountant::_reset_registry_for_tests();
            let (catalog, pricing, buffer) = fixtures();
            let server = MockServer::start().await;
            // Body large enough to push combined bytes over 100 KiB.
            let big_body = "x".repeat(200_000);
            Mock::given(method("GET"))
                .and(path("/big"))
                .respond_with(ResponseTemplate::new(200).set_body_string(big_body))
                .mount(&server)
                .await;

            let mw = DexcostMiddleware::new(
                catalog,
                pricing,
                buffer.clone(),
                Some("t-net-emit".into()),
            );
            let client = ClientBuilder::new(reqwest::Client::new()).with(mw).build();
            let resp = client
                .get(format!("{}/big", server.uri()))
                .send()
                .await
                .unwrap();
            assert_eq!(resp.status(), 200);

            let buf = buffer.lock().await;
            let net_events: Vec<_> = buf
                .query_events("t-net-emit")
                .into_iter()
                .filter(|e| e.event_type == EventType::Network)
                .collect();
            assert_eq!(net_events.len(), 1, "exactly one network event for an un-cataloged above-threshold call");
            let ev = &net_events[0];
            assert_eq!(ev.cost_usd, Decimal::ZERO);
            assert_eq!(
                ev.details.get("cost_pending"),
                Some(&serde_json::Value::Bool(true))
            );
        });
    }

    #[test]
    fn middleware_uncataloged_below_threshold_no_event() {
        let _ = tokio::runtime::Runtime::new().unwrap().block_on(async {
            let _guard = crate::adapters::http::GLOBAL_HTTP_TEST_LOCK.lock().await;
            crate::adapters::http::clear_domain_rates();
            crate::adapters::network_accountant::_reset_registry_for_tests();
            let (catalog, pricing, buffer) = fixtures();
            let server = MockServer::start().await;
            Mock::given(method("GET"))
                .and(path("/small"))
                .respond_with(ResponseTemplate::new(200).set_body_string("small"))
                .mount(&server)
                .await;

            let mw = DexcostMiddleware::new(
                catalog,
                pricing,
                buffer.clone(),
                Some("t-net-small".into()),
            );
            let client = ClientBuilder::new(reqwest::Client::new()).with(mw).build();
            client
                .get(format!("{}/small", server.uri()))
                .send()
                .await
                .unwrap();

            let buf = buffer.lock().await;
            let net_events: Vec<_> = buf
                .query_events("t-net-small")
                .into_iter()
                .filter(|e| e.event_type == EventType::Network)
                .collect();
            assert!(
                net_events.is_empty(),
                "small un-cataloged call must not emit a network event"
            );
        });
    }

    #[test]
    fn middleware_records_bytes_into_registered_accountant() {
        let _ = tokio::runtime::Runtime::new().unwrap().block_on(async {
            let _guard = crate::adapters::http::GLOBAL_HTTP_TEST_LOCK.lock().await;
            crate::adapters::http::clear_domain_rates();
            crate::adapters::network_accountant::_reset_registry_for_tests();

            // Register an accountant under the task_id the middleware will use.
            let accountant = Arc::new(
                crate::adapters::network_accountant::NetworkAccountant::new(),
            );
            crate::adapters::network_accountant::register_accountant(
                "t-acct",
                accountant.clone(),
            );

            let (catalog, pricing, buffer) = fixtures();
            let server = MockServer::start().await;
            Mock::given(method("GET"))
                .and(path("/x"))
                .respond_with(ResponseTemplate::new(200).set_body_string("hi"))
                .mount(&server)
                .await;

            let mw = DexcostMiddleware::new(
                catalog,
                pricing,
                buffer.clone(),
                Some("t-acct".into()),
            );
            let client = ClientBuilder::new(reqwest::Client::new()).with(mw).build();
            client
                .get(format!("{}/x", server.uri()))
                .send()
                .await
                .unwrap();

            let snap = accountant.finalize();
            assert_eq!(snap.call_count, 1, "accountant must record the call");
            assert!(snap.bytes_in > 0);
            assert!(snap.bytes_out > 0);
        });
    }

    #[test]
    fn middleware_suppressed_call_records_bytes_but_no_network_event() {
        let _ = tokio::runtime::Runtime::new().unwrap().block_on(async {
            let _guard = crate::adapters::http::GLOBAL_HTTP_TEST_LOCK.lock().await;
            crate::adapters::http::clear_domain_rates();
            crate::adapters::network_accountant::_reset_registry_for_tests();

            let accountant = Arc::new(
                crate::adapters::network_accountant::NetworkAccountant::new(),
            );
            crate::adapters::network_accountant::register_accountant(
                "t-suppressed",
                accountant.clone(),
            );

            let (catalog, pricing, buffer) = fixtures();
            let server = MockServer::start().await;
            let big_body = "x".repeat(200_000);
            Mock::given(method("GET"))
                .and(path("/big"))
                .respond_with(ResponseTemplate::new(200).set_body_string(big_body))
                .mount(&server)
                .await;

            let mw = DexcostMiddleware::new(
                catalog,
                pricing,
                buffer.clone(),
                Some("t-suppressed".into()),
            );
            let client = ClientBuilder::new(reqwest::Client::new()).with(mw).build();

            // Caller runs the HTTP call inside the suppression scope —
            // simulating an LLM instrument wrapping its outbound call.
            crate::core::context::suppress_network_event(async {
                client
                    .get(format!("{}/big", server.uri()))
                    .send()
                    .await
                    .unwrap();
            })
            .await;

            // Bytes still recorded into the accountant.
            let snap = accountant.finalize();
            assert_eq!(snap.call_count, 1);
            assert!(snap.bytes_in > 0);

            // But no standalone network event emitted.
            let buf = buffer.lock().await;
            let net_events: Vec<_> = buf
                .query_events("t-suppressed")
                .into_iter()
                .filter(|e| e.event_type == EventType::Network)
                .collect();
            assert!(
                net_events.is_empty(),
                "suppression scope must withhold the network event even for a large response"
            );
        });
    }

    // ── Streaming-body tests (Decisions Log #2 — actual fix) ─────────────

    #[test]
    fn is_streaming_response_recognises_sse() {
        let mut h = reqwest::header::HeaderMap::new();
        h.insert(
            reqwest::header::CONTENT_TYPE,
            reqwest::header::HeaderValue::from_static("text/event-stream"),
        );
        assert!(is_streaming_response(&h));
    }

    #[test]
    fn is_streaming_response_recognises_sse_with_charset() {
        let mut h = reqwest::header::HeaderMap::new();
        h.insert(
            reqwest::header::CONTENT_TYPE,
            reqwest::header::HeaderValue::from_static("text/event-stream; charset=utf-8"),
        );
        assert!(is_streaming_response(&h));
    }

    #[test]
    fn is_streaming_response_recognises_ndjson() {
        let mut h = reqwest::header::HeaderMap::new();
        h.insert(
            reqwest::header::CONTENT_TYPE,
            reqwest::header::HeaderValue::from_static("application/x-ndjson"),
        );
        assert!(is_streaming_response(&h));
    }

    #[test]
    fn is_streaming_response_false_for_plain_json() {
        let mut h = reqwest::header::HeaderMap::new();
        h.insert(
            reqwest::header::CONTENT_TYPE,
            reqwest::header::HeaderValue::from_static("application/json"),
        );
        assert!(!is_streaming_response(&h));
    }

    #[test]
    fn is_streaming_response_false_when_no_content_type() {
        let h = reqwest::header::HeaderMap::new();
        assert!(!is_streaming_response(&h));
    }

    #[test]
    fn streaming_middleware_does_not_block_on_long_lived_stream() {
        // Pin the v2 streaming-fix contract: a response that takes
        // multiple chunks over time must NOT block the middleware's
        // handle() return. We verify by sending a body in two chunks
        // and asserting that client.send().await completes in well
        // under the chunk delay — which would be impossible with the
        // old `response.bytes().await` buffer path.
        let _ = tokio::runtime::Runtime::new().unwrap().block_on(async {
            let _guard = crate::adapters::http::GLOBAL_HTTP_TEST_LOCK.lock().await;
            crate::adapters::http::clear_domain_rates();
            crate::adapters::network_accountant::_reset_registry_for_tests();

            let accountant = Arc::new(
                crate::adapters::network_accountant::NetworkAccountant::new(),
            );
            crate::adapters::network_accountant::register_accountant(
                "t-stream",
                accountant.clone(),
            );

            let (catalog, pricing, buffer) = fixtures();
            let server = MockServer::start().await;
            // SSE-style body — content type triggers the streaming path.
            let body = "data: hello\n\ndata: world\n\n";
            Mock::given(method("GET"))
                .and(path("/stream"))
                .respond_with(
                    ResponseTemplate::new(200)
                        .set_body_raw(body.as_bytes().to_vec(), "text/event-stream"),
                )
                .mount(&server)
                .await;

            let mw = DexcostMiddleware::new(
                catalog,
                pricing,
                buffer.clone(),
                Some("t-stream".into()),
            );
            let client = ClientBuilder::new(reqwest::Client::new()).with(mw).build();

            let t0 = std::time::Instant::now();
            let resp = client
                .get(format!("{}/stream", server.uri()))
                .send()
                .await
                .unwrap();
            let middleware_return_us = t0.elapsed().as_micros();
            assert_eq!(resp.status(), 200);

            // Caller drains the body — this is where bytes flow through
            // the RecordingStream wrapper and get counted.
            let bytes = resp.bytes().await.unwrap();
            assert_eq!(bytes.len(), body.len(), "body bytes round-trip intact");

            // Accountant must have received exactly one record on stream
            // completion, with response bytes >= body length.
            let snap = accountant.finalize();
            assert_eq!(snap.call_count, 1, "exactly one record() on stream end");
            assert!(
                snap.bytes_in >= body.len() as u64,
                "response bytes ({}) must include the streamed body ({})",
                snap.bytes_in,
                body.len()
            );
            assert!(snap.bytes_out > 0, "request bytes recorded too");

            // Middleware must return promptly — even if we don't measure a
            // specific bound here, the test would deadlock under the old
            // buffer-everything path if the server held the connection open.
            // Belt-and-suspenders: cap at 1 second.
            assert!(
                middleware_return_us < 1_000_000,
                "middleware.handle returned in {}us — streaming path should be non-blocking",
                middleware_return_us
            );
        });
    }

    #[test]
    fn streaming_middleware_records_bytes_on_early_drop() {
        // Pin v1 §5.5 — early-abort path. Caller drops the response
        // without draining the stream; the RecordingStream's Drop must
        // still record the partial bytes seen so far.
        let _ = tokio::runtime::Runtime::new().unwrap().block_on(async {
            let _guard = crate::adapters::http::GLOBAL_HTTP_TEST_LOCK.lock().await;
            crate::adapters::http::clear_domain_rates();
            crate::adapters::network_accountant::_reset_registry_for_tests();

            let accountant = Arc::new(
                crate::adapters::network_accountant::NetworkAccountant::new(),
            );
            crate::adapters::network_accountant::register_accountant(
                "t-drop",
                accountant.clone(),
            );

            let (catalog, pricing, buffer) = fixtures();
            let server = MockServer::start().await;
            Mock::given(method("GET"))
                .and(path("/sse"))
                .respond_with(
                    ResponseTemplate::new(200)
                        .set_body_raw(b"data: x\n\n".to_vec(), "text/event-stream"),
                )
                .mount(&server)
                .await;

            let mw = DexcostMiddleware::new(
                catalog,
                pricing,
                buffer.clone(),
                Some("t-drop".into()),
            );
            let client = ClientBuilder::new(reqwest::Client::new()).with(mw).build();

            // Send request, get response, then drop without draining.
            {
                let _resp = client
                    .get(format!("{}/sse", server.uri()))
                    .send()
                    .await
                    .unwrap();
                // _resp dropped here — RecordingStream::Drop fires.
            }

            let snap = accountant.finalize();
            assert_eq!(
                snap.call_count, 1,
                "early-drop must still record exactly one call"
            );
            // bytes_out (request side) is always known at middleware entry.
            assert!(snap.bytes_out > 0);
        });
    }

    #[test]
    fn streaming_middleware_emits_no_network_event() {
        // For streaming responses we skip the threshold-gated network
        // event emission because (a) we don't know total bytes until
        // after handle() returns, and (b) LLM streaming responses are
        // already covered by the LLM instrument's own llm_call event.
        // The byte count still feeds network_cost_usd at task finalize.
        let _ = tokio::runtime::Runtime::new().unwrap().block_on(async {
            let _guard = crate::adapters::http::GLOBAL_HTTP_TEST_LOCK.lock().await;
            crate::adapters::http::clear_domain_rates();
            crate::adapters::network_accountant::_reset_registry_for_tests();

            let (catalog, pricing, buffer) = fixtures();
            let server = MockServer::start().await;
            // 200 KB SSE-flagged body — above the 100 KiB threshold that
            // would normally emit a network event in the buffered path.
            let big = "data: ".to_string() + &"x".repeat(200_000) + "\n\n";
            Mock::given(method("GET"))
                .and(path("/big-sse"))
                .respond_with(
                    ResponseTemplate::new(200)
                        .set_body_raw(big.clone().into_bytes(), "text/event-stream"),
                )
                .mount(&server)
                .await;

            let mw = DexcostMiddleware::new(
                catalog,
                pricing,
                buffer.clone(),
                Some("t-no-event".into()),
            );
            let client = ClientBuilder::new(reqwest::Client::new()).with(mw).build();
            let resp = client
                .get(format!("{}/big-sse", server.uri()))
                .send()
                .await
                .unwrap();
            // Drain so bytes flow + RecordingStream finalises.
            let _ = resp.bytes().await.unwrap();

            let buf = buffer.lock().await;
            let net_events: Vec<_> = buf
                .query_events("t-no-event")
                .into_iter()
                .filter(|e| e.event_type == EventType::Network)
                .collect();
            assert!(
                net_events.is_empty(),
                "streaming path does not emit per-call network events; bytes still feed the task aggregate"
            );
        });
    }
}