link-assistant-router 0.108.0

Link.Assistant.Router — Claude MAX OAuth proxy and token gateway for Anthropic APIs
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
//! Tests for [`crate::log_analysis`].

use super::*;

fn write_log(root: &Path, token: &str, records: &[Value]) {
    let directory = root.join(token);
    std::fs::create_dir_all(&directory).expect("create token directory");
    let contents = records
        .iter()
        .map(|record| serde_json::to_string(record).expect("serialize"))
        .collect::<Vec<_>>()
        .join("\n");
    std::fs::write(directory.join("requests.jsonl"), contents + "\n").expect("write log");
}

/// The false negative from issue #234: searching for `error|warn` found nothing
/// because a stream dying mid-flight is logged at `INFO`. The analyser reads
/// the terminal record instead, so it finds what a text search cannot.
#[test]
fn a_truncated_stream_is_found_without_searching_for_error_text() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &[
            json!({"correlation_id": "a", "phase": "client_request", "uri": "/v1/messages"}),
            json!({"correlation_id": "a", "phase": "client_response", "status": 200}),
            json!({"correlation_id": "a", "phase": "upstream_response_body", "body": "data: x"}),
            json!({
                "correlation_id": "a",
                "phase": "stream_end",
                "outcome": "ended_without_terminator",
                "complete": false,
                "frames": 444
            }),
        ],
    );

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read the log");
    assert_eq!(unparsable, 0);
    let summary = summarise(&exchanges, unparsable, 0);
    assert_eq!(summary.incomplete_streams, 1, "{summary:?}");
    // Status alone says the opposite, which is the whole point.
    assert_eq!(summary.statuses.get(&200).copied(), Some(1));

    let found = anomalies(&exchanges);
    let cut = found
        .iter()
        .find(|anomaly| anomaly.kind == "stream_ended_without_terminator")
        .expect("the truncated stream is named");
    assert_eq!(cut.correlation_ids, vec!["a".to_string()]);
}

/// The false positive from issue #234: counting streams without `message_stop`
/// as a substring reported 100%, because compressed bodies cannot contain it.
/// A compressed body is reported as undecodable, never as a missing terminator.
#[test]
fn a_compressed_body_is_reported_as_undecodable_not_as_a_failure() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &[
            json!({"correlation_id": "b", "phase": "client_request", "uri": "/v1/messages"}),
            json!({"correlation_id": "b", "phase": "client_response", "status": 200}),
            json!({
                "correlation_id": "b",
                "phase": "upstream_response_body",
                "body": {"base64": "H4sIAAAA", "bytes": 6}
            }),
            json!({
                "correlation_id": "b",
                "phase": "stream_end",
                "outcome": "completed",
                "complete": true,
                "frames": 1
            }),
        ],
    );

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read");
    let summary = summarise(&exchanges, unparsable, 0);
    // The turn completed: the compressed body must not make it look truncated.
    assert_eq!(summary.incomplete_streams, 0, "{summary:?}");
    assert_eq!(summary.undecodable_bodies, 1);

    let found = anomalies(&exchanges);
    assert!(
        found
            .iter()
            .any(|anomaly| anomaly.kind == "undecodable_bodies"),
        "undecodable bodies must be stated, not silently ignored"
    );
    assert!(
        !found
            .iter()
            .any(|anomaly| anomaly.kind == "stream_ended_without_terminator"),
        "a completed stream must not be reported as truncated"
    );
}

/// Records the analyser cannot parse are counted rather than skipped: silence
/// about unreadable data is what produced the original false positive.
#[test]
fn unparsable_records_are_reported() {
    let root = tempfile::tempdir().expect("temporary log root");
    let directory = root.path().join("tokenhash");
    std::fs::create_dir_all(&directory).expect("create");
    std::fs::write(
        directory.join("requests.jsonl"),
        "{\"correlation_id\":\"c\",\"phase\":\"client_response\",\"status\":200}\nnot json\n",
    )
    .expect("write");

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read");
    assert_eq!(unparsable, 1);
    let rendered = summarise(&exchanges, unparsable, 0).render();
    assert!(rendered.contains("1 unparsable"), "{rendered}");
}

/// Repeated authentication failures are misconfiguration, and are named as
/// such rather than left to be read out of a status histogram.
#[test]
fn repeated_authentication_failures_are_named() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &[
            json!({"correlation_id": "d", "phase": "client_response", "status": 401}),
            json!({"correlation_id": "e", "phase": "client_response", "status": 401}),
        ],
    );
    let (exchanges, _, _) = read_exchanges(root.path(), None).expect("read");
    let found = anomalies(&exchanges);
    let refused = found
        .iter()
        .find(|anomaly| anomaly.kind == "repeated_authentication_failure")
        .expect("named");
    assert_eq!(refused.correlation_ids.len(), 2);
}

/// A healthy log yields no anomalies, so the command works as a health gate.
#[test]
fn a_healthy_log_reports_nothing() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &[
            json!({"correlation_id": "f", "phase": "client_response", "status": 200}),
            json!({
                "correlation_id": "f",
                "phase": "stream_end",
                "outcome": "completed",
                "complete": true,
                "frames": 3
            }),
        ],
    );
    let (exchanges, _, _) = read_exchanges(root.path(), None).expect("read");
    assert!(anomalies(&exchanges).is_empty());
}

/// One exchange can be rendered in full, which is what closes the loop from a
/// named correlation id back to the records.
#[test]
fn a_single_exchange_can_be_shown() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &[
            json!({"correlation_id": "g", "phase": "client_request", "uri": "/v1/messages"}),
            json!({"correlation_id": "h", "phase": "client_request", "uri": "/other"}),
        ],
    );
    let rendered = show(root.path(), None, "g").expect("show");
    assert!(rendered.contains("/v1/messages"), "{rendered}");
    assert!(!rendered.contains("/other"), "{rendered}");
    let missing = show(root.path(), None, "nope").expect("show");
    assert!(missing.contains("no records"), "{missing}");
}

/// The bug in issue #252: a complete non-streamed exchange was counted as a
/// stream whose ending is unknown.
///
/// Six phases, both statuses 200, both bodies recorded, and a JSON
/// `content-type`. A non-streamed response has no dialect terminator by
/// construction, so "no terminal record" is the expected shape, not an anomaly.
#[test]
fn a_complete_non_streamed_exchange_is_not_an_anomaly() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &[
            json!({"correlation_id": "j", "phase": "client_request", "uri": "/v1/messages"}),
            json!({"correlation_id": "j", "phase": "upstream_request"}),
            json!({
                "correlation_id": "j",
                "phase": "upstream_response",
                "status": 200,
                "headers": {"content-type": "application/json"}
            }),
            json!({"correlation_id": "j", "phase": "upstream_response_body", "body": "{\"ok\":true}"}),
            json!({
                "correlation_id": "j",
                "phase": "client_response",
                "status": 200,
                "headers": {"content-type": "application/json"}
            }),
            json!({"correlation_id": "j", "phase": "client_response_body", "body": "{\"ok\":true}"}),
        ],
    );

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read the log");
    assert_eq!(unparsable, 0);

    let summary = summarise(&exchanges, unparsable, 0);
    assert_eq!(
        summary.streamed, 0,
        "a JSON response is not a stream: {summary:?}"
    );
    assert_eq!(summary.unterminated_streams, 0, "{summary:?}");
    assert_eq!(summary.incomplete_streams, 0, "{summary:?}");

    let found = anomalies(&exchanges);
    assert!(
        found.is_empty(),
        "a complete non-streamed exchange must raise nothing: {found:?}"
    );
}

/// The case from the issue comment: a gzip-compressed non-streamed reply.
///
/// The compressed body arrives in several transfer chunks, and counting those
/// as SSE frames reported a truncated stream — and emitted a WARN — for a
/// request that succeeded. A test written only against an uncompressed JSON
/// response would pass while this kept happening.
#[test]
fn a_gzip_compressed_json_reply_is_not_a_truncated_stream() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &[
            json!({"correlation_id": "g", "phase": "client_request", "uri": "/v1/messages"}),
            json!({
                "correlation_id": "g",
                "phase": "upstream_response",
                "status": 200,
                "headers": {"content-type": "application/json", "content-encoding": "gzip"}
            }),
            // Two transfer chunks of one compressed body, not two stream frames.
            json!({"correlation_id": "g", "phase": "upstream_response_body", "body": {"base64": "H4sIAAAAAAAA"}}),
            json!({"correlation_id": "g", "phase": "upstream_response_body", "body": {"base64": "A/3VRy07DMBD8"}}),
            json!({
                "correlation_id": "g",
                "phase": "client_response",
                "status": 200,
                "headers": {"content-type": "application/json", "content-encoding": "gzip"}
            }),
            json!({
                "correlation_id": "g",
                "phase": "stream_end",
                "outcome": "ended_without_terminator",
                "complete": false,
                "frames": 2
            }),
        ],
    );

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read the log");
    let summary = summarise(&exchanges, unparsable, 0);

    assert_eq!(
        summary.streamed, 0,
        "a compressed JSON reply is not a stream: {summary:?}"
    );
    assert_eq!(summary.incomplete_streams, 0, "{summary:?}");
    assert_eq!(summary.unterminated_streams, 0, "{summary:?}");
    // `undecodable_bodies` is expected and correct here (issue #231): the log
    // says plainly that a compressed body cannot be inspected. What must not
    // appear is a verdict about how the "stream" ended, since there was none.
    let found = anomalies(&exchanges);
    assert!(
        !found
            .iter()
            .any(|anomaly| anomaly.kind.starts_with("stream_")
                || anomaly.kind == "no_terminal_record"),
        "a successful compressed reply must raise no stream verdict: {found:?}"
    );
}

/// A genuine SSE stream must still be classified as one — the fix must not
/// silence the detection issue #230 added.
#[test]
fn an_sse_response_is_still_a_stream() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &[
            json!({"correlation_id": "s", "phase": "client_request", "uri": "/v1/messages"}),
            json!({
                "correlation_id": "s",
                "phase": "client_response",
                "status": 200,
                "headers": {"content-type": "text/event-stream"}
            }),
            json!({"correlation_id": "s", "phase": "upstream_response_body", "body": "data: x"}),
            json!({
                "correlation_id": "s",
                "phase": "stream_end",
                "outcome": "ended_without_terminator",
                "complete": false,
                "frames": 12
            }),
        ],
    );

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read the log");
    let summary = summarise(&exchanges, unparsable, 0);

    assert_eq!(summary.streamed, 1, "{summary:?}");
    assert_eq!(
        summary.incomplete_streams, 1,
        "a truncated SSE stream must still be reported: {summary:?}"
    );
    let found = anomalies(&exchanges);
    assert!(
        found
            .iter()
            .any(|anomaly| anomaly.kind == "stream_ended_without_terminator"),
        "{found:?}"
    );
}

/// A media type with parameters is still recognised: servers commonly send
/// `text/event-stream; charset=utf-8`.
#[test]
fn a_parameterised_media_type_is_recognised() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &[
            json!({"correlation_id": "p", "phase": "client_request"}),
            json!({
                "correlation_id": "p",
                "phase": "client_response",
                "status": 200,
                "headers": {"content-type": "text/event-stream; charset=utf-8"}
            }),
            json!({"correlation_id": "p", "phase": "upstream_response_body", "body": "data: x"}),
        ],
    );

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read the log");

    assert_eq!(summarise(&exchanges, unparsable, 0).streamed, 1);
}

/// With no recorded media type, the request's `stream: true` decides — an older
/// log has no response headers, and must not silently lose its streams.
#[test]
fn a_requested_stream_counts_when_the_response_type_is_unknown() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &[
            json!({
                "correlation_id": "r",
                "phase": "client_request",
                "body": {"json": {"stream": true}}
            }),
            json!({"correlation_id": "r", "phase": "client_response", "status": 200}),
            json!({"correlation_id": "r", "phase": "upstream_response_body", "body": "data: x"}),
            json!({
                "correlation_id": "r",
                "phase": "stream_end",
                "outcome": "ended_without_terminator",
                "complete": false,
                "frames": 3
            }),
        ],
    );

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read the log");
    let summary = summarise(&exchanges, unparsable, 0);

    assert_eq!(summary.streamed, 1, "{summary:?}");
    assert_eq!(summary.incomplete_streams, 1, "{summary:?}");
}

/// A response that was not a stream outranks a request that asked for one: the
/// upstream may answer a `stream: true` request with a single JSON document.
#[test]
fn a_json_answer_to_a_streaming_request_is_not_a_stream() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &[
            json!({
                "correlation_id": "m",
                "phase": "client_request",
                "body": {"json": {"stream": true}}
            }),
            json!({
                "correlation_id": "m",
                "phase": "client_response",
                "status": 200,
                "headers": {"content-type": "application/json"}
            }),
            json!({"correlation_id": "m", "phase": "upstream_response_body", "body": "{}"}),
        ],
    );

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read the log");

    assert_eq!(
        summarise(&exchanges, unparsable, 0).streamed,
        0,
        "the response decides, not the request"
    );
}

/// The case from issue #255: a gzip-compressed SSE stream that did complete.
///
/// The recorded frames are the compressed bytes that were relayed, so scanning
/// them for `message_stop` searches gzip and always fails. Counting that as a
/// missing terminator reported 315 of 400 streams as failing on a log whose
/// sampled exchanges had all succeeded.
#[test]
fn a_compressed_sse_stream_is_not_reported_as_truncated() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &[
            json!({
                "correlation_id": "z",
                "phase": "client_request",
                "uri": "/v1/messages?beta=true",
                "body": {"json": {"stream": true}}
            }),
            json!({
                "correlation_id": "z",
                "phase": "client_response",
                "status": 200,
                "headers": {
                    "content-type": "text/event-stream; charset=utf-8",
                    "content-encoding": "gzip"
                }
            }),
            json!({"correlation_id": "z", "phase": "upstream_response_body", "body": {"base64": "H4sIAAAAAAAA"}}),
            json!({
                "correlation_id": "z",
                "phase": "stream_end",
                "outcome": "encoded_not_verifiable",
                "streamed": true,
                "inspectable": false,
                "complete": false,
                "frames": 11
            }),
        ],
    );

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read the log");
    let summary = summarise(&exchanges, unparsable, 0);

    assert_eq!(summary.streamed, 1, "it is still a stream: {summary:?}");
    assert_eq!(
        summary.incomplete_streams, 0,
        "an unreadable stream is not a demonstrated truncation: {summary:?}"
    );
    assert_eq!(summary.unterminated_streams, 0, "{summary:?}");
    assert_eq!(
        summary.unverifiable_streams, 1,
        "it is reported as its own class: {summary:?}"
    );

    let found = anomalies(&exchanges);
    assert!(
        !found
            .iter()
            .any(|anomaly| anomaly.kind == "stream_ended_without_terminator"),
        "a healthy compressed stream must not be called truncated: {found:?}"
    );
    let named = found
        .iter()
        .find(|anomaly| anomaly.kind == "stream_not_verifiable")
        .expect("the unverifiable stream is named");
    assert_eq!(named.correlation_ids, vec!["z".to_string()]);
}

/// A compressed stream is recognised from the recorded headers alone, so a log
/// written before the relay stated `inspectable` is read correctly too.
#[test]
fn a_compressed_stream_is_recognised_from_its_headers() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &[
            json!({"correlation_id": "h", "phase": "client_request"}),
            json!({
                "correlation_id": "h",
                "phase": "client_response",
                "status": 200,
                "headers": {"content-type": "text/event-stream", "content-encoding": "gzip"}
            }),
            json!({"correlation_id": "h", "phase": "upstream_response_body", "body": {"base64": "H4sI"}}),
            json!({
                "correlation_id": "h",
                "phase": "stream_end",
                "outcome": "ended_without_terminator",
                "complete": false,
                "frames": 11
            }),
        ],
    );

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read the log");
    let summary = summarise(&exchanges, unparsable, 0);

    assert_eq!(summary.unverifiable_streams, 1, "{summary:?}");
    assert_eq!(summary.incomplete_streams, 0, "{summary:?}");
}

/// An uncompressed SSE stream that stops early must still be reported: this is
/// the signal from issue #230, and the fix must not silence it.
#[test]
fn an_uncompressed_truncated_stream_is_still_reported() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &[
            json!({"correlation_id": "t", "phase": "client_request"}),
            json!({
                "correlation_id": "t",
                "phase": "client_response",
                "status": 200,
                "headers": {"content-type": "text/event-stream"}
            }),
            json!({"correlation_id": "t", "phase": "upstream_response_body", "body": "data: x"}),
            json!({
                "correlation_id": "t",
                "phase": "stream_end",
                "outcome": "ended_without_terminator",
                "streamed": true,
                "inspectable": true,
                "complete": false,
                "frames": 444
            }),
        ],
    );

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read the log");
    let summary = summarise(&exchanges, unparsable, 0);

    assert_eq!(summary.incomplete_streams, 1, "{summary:?}");
    assert_eq!(summary.unverifiable_streams, 0, "{summary:?}");
    assert!(
        anomalies(&exchanges)
            .iter()
            .any(|anomaly| anomaly.kind == "stream_ended_without_terminator"),
        "a real truncation must still be named"
    );
}

/// Build a complete uncompressed SSE exchange with no `stream_end` record.
///
/// The relay writes that terminal record only on the Anthropic path, so an
/// `OpenAI` or Gemini stream reaches the log without one (issue #258).
fn stream_without_a_terminal_record(id: &str, uri: &str, body: &str) -> Vec<Value> {
    vec![
        json!({"correlation_id": id, "phase": "client_request", "uri": uri}),
        json!({
            "correlation_id": id,
            "phase": "client_response",
            "status": 200,
            "headers": {"content-type": "text/event-stream"}
        }),
        json!({"correlation_id": id, "phase": "upstream_response_body", "body": body}),
    ]
}

/// The bug in issue #258: a stream whose terminator is right there in the
/// recorded body was reported as ending in an unknown state.
///
/// 239 of 251 uncompressed streams carried a valid terminator, so the class was
/// ~95% healthy traffic — and the 12 that deserved attention were buried in it.
#[test]
fn a_terminator_in_the_recorded_body_settles_the_ending() {
    let root = tempfile::tempdir().expect("temporary log root");
    let mut records = Vec::new();
    // One per dialect, which is what the issue asks to pin at once.
    records.extend(stream_without_a_terminal_record(
        "openai",
        "/v1/chat/completions",
        "data: {\"choices\":[{\"delta\":{}}]}\n\ndata: [DONE]\n\n",
    ));
    records.extend(stream_without_a_terminal_record(
        "gemini",
        "/api/gemini/v1beta/models/gemini-2.0:streamGenerateContent",
        "data: {\"candidates\":[{\"content\":{\"parts\":[{\"text\":\"hi\"}]},\
         \"finishReason\":\"STOP\",\"index\":0}]}\n\n",
    ));
    records.extend(stream_without_a_terminal_record(
        "anthropic",
        "/v1/messages",
        "event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n",
    ));
    records.extend(stream_without_a_terminal_record(
        "responses",
        "/v1/responses",
        "event: response.completed\ndata: {\"type\":\"response.completed\"}\n\n",
    ));
    write_log(root.path(), "tokenhash", &records);

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read the log");
    let summary = summarise(&exchanges, unparsable, 0);

    assert_eq!(summary.streamed, 4, "{summary:?}");
    assert_eq!(
        summary.unterminated_streams, 0,
        "every one of these carries its own dialect's terminator: {summary:?}"
    );
    let found = anomalies(&exchanges);
    assert!(
        !found
            .iter()
            .any(|anomaly| anomaly.kind == "no_terminal_record"),
        "a completed stream must not be reported as unknown: {found:?}"
    );
}

/// A readable stream with no terminator anywhere is still a genuine anomaly —
/// the 12 empty-bodied exchanges the issue says are worth alerting on.
#[test]
fn a_stream_with_no_terminator_is_still_reported() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &stream_without_a_terminal_record("empty", "/v1/chat/completions", ""),
    );

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read the log");
    let summary = summarise(&exchanges, unparsable, 0);

    assert_eq!(
        summary.unterminated_streams, 1,
        "an empty body settles nothing: {summary:?}"
    );
    assert!(
        anomalies(&exchanges)
            .iter()
            .any(|anomaly| anomaly.kind == "no_terminal_record"),
        "the genuinely unaccounted-for stream must still be named"
    );
}

/// A `stream_end` record still outranks the body scan when one is present.
///
/// The relay watched the frames go past; the analyser is reading what was
/// captured afterwards. Where they disagree, the relay's verdict wins.
#[test]
fn a_terminal_record_outranks_the_recorded_body() {
    let root = tempfile::tempdir().expect("temporary log root");
    let mut records = stream_without_a_terminal_record(
        "cut",
        "/v1/chat/completions",
        "data: {\"choices\":[{\"delta\":{}}]}\n\n",
    );
    records.push(json!({
        "correlation_id": "cut",
        "phase": "stream_end",
        "outcome": "ended_without_terminator",
        "streamed": true,
        "inspectable": true,
        "complete": false,
        "frames": 12
    }));
    write_log(root.path(), "tokenhash", &records);

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read the log");
    let summary = summarise(&exchanges, unparsable, 0);

    assert_eq!(
        summary.incomplete_streams, 1,
        "the relay saw the stream stop early: {summary:?}"
    );
    assert_eq!(summary.unterminated_streams, 0, "{summary:?}");
}

/// The terminator may be recorded on the client side rather than upstream, so
/// both body phases settle the question.
#[test]
fn a_client_side_body_can_settle_the_ending() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &[
            json!({"correlation_id": "c", "phase": "client_request", "uri": "/v1/chat/completions"}),
            json!({
                "correlation_id": "c",
                "phase": "client_response",
                "status": 200,
                "headers": {"content-type": "text/event-stream"}
            }),
            json!({"correlation_id": "c", "phase": "client_response_body", "body": "data: [DONE]\n\n"}),
        ],
    );

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read the log");

    assert_eq!(summarise(&exchanges, unparsable, 0).unterminated_streams, 0);
}

/// A compressed stream stays unverifiable rather than being settled by a
/// terminator that happens to appear in its base64 text (issue #255).
#[test]
fn a_compressed_body_is_not_settled_by_its_encoded_text() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "tokenhash",
        &[
            json!({"correlation_id": "gz", "phase": "client_request", "uri": "/v1/messages"}),
            json!({
                "correlation_id": "gz",
                "phase": "client_response",
                "status": 200,
                "headers": {"content-type": "text/event-stream", "content-encoding": "gzip"}
            }),
            json!({"correlation_id": "gz", "phase": "upstream_response_body", "body": {"base64": "bWVzc2FnZV9zdG9w"}}),
        ],
    );

    let (exchanges, unparsable, _) = read_exchanges(root.path(), None).expect("read the log");
    let summary = summarise(&exchanges, unparsable, 0);

    assert_eq!(summary.unverifiable_streams, 1, "{summary:?}");
    assert_eq!(summary.unterminated_streams, 0, "{summary:?}");
}

/// Unparsable lines are counted rather than silently skipped, and the readable
/// records around them are still assembled.
#[test]
fn a_damaged_line_does_not_discard_the_records_around_it() {
    let root = tempfile::tempdir().expect("temporary log root");
    let directory = root.path().join("tokenhash");
    std::fs::create_dir_all(&directory).expect("create token directory");
    std::fs::write(
        directory.join("requests.jsonl"),
        "{\"correlation_id\":\"ok\",\"phase\":\"client_response\",\"status\":200}\n\
         not json at all\n\
         \n\
         {\"correlation_id\":\"ok\",\"phase\":\"upstream_response_body\",\"body\":\"data: [DONE]\"}\n",
    )
    .expect("write log");

    let (exchanges, unparsable, bytes) = read_exchanges(root.path(), None).expect("read the log");

    assert_eq!(unparsable, 1, "the damaged line is counted");
    assert!(bytes > 0, "the bytes read are reported");
    assert_eq!(exchanges.len(), 1, "the readable records still assemble");
    assert_eq!(exchanges[0].correlation_id, "ok");
}

/// A token filter narrows the analysis to one token's directory, so a busy
/// store can be examined one caller at a time.
#[test]
fn a_token_filter_selects_one_directory() {
    let root = tempfile::tempdir().expect("temporary log root");
    write_log(
        root.path(),
        "aaaa",
        &[json!({"correlation_id": "a", "phase": "client_response", "status": 200})],
    );
    write_log(
        root.path(),
        "bbbb",
        &[json!({"correlation_id": "b", "phase": "client_response", "status": 500})],
    );

    let (all, _, _) = read_exchanges(root.path(), None).expect("read every token");
    assert_eq!(all.len(), 2);

    let (one, _, _) = read_exchanges(root.path(), Some("aaaa")).expect("read one token");
    assert_eq!(one.len(), 1, "only the named token is read");
    assert_eq!(one[0].correlation_id, "a");
}

/// A log directory that does not exist is not readable data, and must not be
/// reported as a healthy empty store.
#[test]
fn a_missing_root_yields_nothing() {
    let (exchanges, unparsable, bytes) =
        read_exchanges(std::path::Path::new("/nonexistent-log-root-258"), None)
            .expect("a missing root is not an error");

    assert!(exchanges.is_empty());
    assert_eq!(unparsable, 0);
    assert_eq!(bytes, 0);
}