sniff-cli 0.1.0

An exhaustive LLM-backed slop finder for codebases
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
use super::*;

#[tokio::test]
async fn method_review_keeps_llm_verdict() {
    let body = r#"{"choices":[{"message":{"content":"{\"smelly\":true,\"tier\":\"kinda_slop\",\"evidence\":\"return 1\",\"reason\":\"small helper\"}"}}]}"#;
    let (endpoint, hits) = spawn_openai_style_server(body);
    let analyzer = Analyzer {
        llm_client: Arc::new(LLMClient::new(cfg(&endpoint), Some("test-key".to_string()))),
        in_tok: AtomicUsize::new(0),
        out_tok: AtomicUsize::new(0),
    };
    let method = MethodRecord {
        name: "sample".to_string(),
        file_path: "sample.py".to_string(),
        source: "def sample():\n    return 1\n".to_string(),
        loc: 2,
        param_count: 0,
        start_line: 1,
        end_line: 2,
        is_exported: false,
        language: "python".to_string(),
        nesting_depth: 0,
        references: vec![],
        real_ref_count: 0,
    };

    let (verdict, _, _) = analyzer.analyze_method_review(&method, &[]).await.unwrap();
    let verdict = verdict.expect("expected method verdict");
    assert_eq!(verdict.tier, FindingTier::KindaSlop);
    assert_eq!(hits.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn speculative_method_reasons_are_cleared() {
    let body = r#"{"choices":[{"message":{"content":"{\"smelly\":true,\"tier\":\"slop\",\"evidence\":\"return Err(err.to_string());\",\"reason\":\"format string uses placeholder and indicates a previous version copy-paste\"}"}}]}"#;
    let (endpoint, hits) = spawn_openai_style_server(body);
    let analyzer = Analyzer {
        llm_client: Arc::new(LLMClient::new(cfg(&endpoint), Some("test-key".to_string()))),
        in_tok: AtomicUsize::new(0),
        out_tok: AtomicUsize::new(0),
    };
    let method = MethodRecord {
        name: "probe".to_string(),
        file_path: "src/llm_impl.rs".to_string(),
        source: "pub async fn probe(&self) -> Result<(), String> {\n    return Err(err.to_string());\n}\n"
            .to_string(),
        loc: 3,
        param_count: 1,
        start_line: 1,
        end_line: 3,
        is_exported: false,
        language: "rust".to_string(),
        nesting_depth: 0,
        references: vec![],
        real_ref_count: 0,
    };

    let (verdict, _, _) = analyzer.analyze_method_review(&method, &[]).await.unwrap();
    let verdict = verdict.expect("expected method verdict");
    assert_eq!(verdict.tier, FindingTier::Clean);
    assert!(verdict.reason.is_empty());
    assert!(verdict.evidence.is_empty());
    assert_eq!(hits.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn empty_method_reasons_are_cleared() {
    let body = r#"{"choices":[{"message":{"content":"{\"smelly\":true,\"tier\":\"slop\",\"evidence\":\"return 1\",\"reason\":\"\"}"}}]}"#;
    let (endpoint, hits) = spawn_openai_style_server(body);
    let analyzer = Analyzer {
        llm_client: Arc::new(LLMClient::new(cfg(&endpoint), Some("test-key".to_string()))),
        in_tok: AtomicUsize::new(0),
        out_tok: AtomicUsize::new(0),
    };
    let method = MethodRecord {
        name: "sample".to_string(),
        file_path: "sample.py".to_string(),
        source: "def sample():\n    return 1\n".to_string(),
        loc: 2,
        param_count: 0,
        start_line: 1,
        end_line: 2,
        is_exported: false,
        language: "python".to_string(),
        nesting_depth: 0,
        references: vec![],
        real_ref_count: 0,
    };

    let (verdict, _, _) = analyzer.analyze_method_review(&method, &[]).await.unwrap();
    let verdict = verdict.expect("expected method verdict");
    assert_eq!(verdict.tier, FindingTier::Clean);
    assert!(verdict.reason.is_empty());
    assert!(verdict.evidence.is_empty());
    assert_eq!(hits.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn thin_wrapper_methods_are_reviewed_instead_of_skipped() {
    let body = r#"{"choices":[{"message":{"content":"{\"smelly\":false,\"tier\":\"clean\",\"evidence\":\"\",\"reason\":\"clean\"}"}}]}"#;
    let (endpoint, hits) = spawn_openai_style_server(body);
    let analyzer = Analyzer {
        llm_client: Arc::new(LLMClient::new(cfg(&endpoint), Some("test-key".to_string()))),
        in_tok: AtomicUsize::new(0),
        out_tok: AtomicUsize::new(0),
    };
    let method = MethodRecord {
        name: "render_label".to_string(),
        file_path: "src/labels.py".to_string(),
        source: "from labels_impl import render_label as _render_label_impl\n\n\
def render_label(value):\n    return _render_label_impl(value)\n"
            .to_string(),
        loc: 4,
        param_count: 1,
        start_line: 1,
        end_line: 4,
        is_exported: true,
        language: "python".to_string(),
        nesting_depth: 0,
        references: vec![],
        real_ref_count: 0,
    };

    let (verdict, _, _) = analyzer.analyze_method_review(&method, &[]).await.unwrap();
    let verdict = verdict.expect("expected method verdict");
    assert_eq!(verdict.tier, FindingTier::Clean);
    assert!(hits.load(Ordering::SeqCst) > 0);
}

#[tokio::test]
async fn file_review_keeps_llm_verdict() {
    let body = r#"{"choices":[{"message":{"content":"{\"smelly\":false,\"tier\":\"clean\",\"evidence\":\"def sample()\",\"cohesive\":true,\"name_accurate\":true,\"reason\":\"clean\"}"}}]}"#;
    let (endpoint, hits) = spawn_openai_style_server(body);
    let analyzer = Analyzer {
        llm_client: Arc::new(LLMClient::new(cfg(&endpoint), Some("test-key".to_string()))),
        in_tok: AtomicUsize::new(0),
        out_tok: AtomicUsize::new(0),
    };
    let file = FileRecord {
        file_path: "sample.py".to_string(),
        source: "def sample():\n    return 1\n".to_string(),
        language: "python".to_string(),
        methods: vec![],
    };

    let (verdict, _, _) = analyzer.analyze_file(&file, &[]).await.unwrap();
    let verdict = verdict.expect("expected file verdict");
    assert_eq!(verdict.tier, FindingTier::Clean);
    assert_eq!(hits.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn empty_file_reasons_are_cleared() {
    let body = r#"{"choices":[{"message":{"content":"{\"smelly\":true,\"tier\":\"slop\",\"evidence\":\"def sample():\",\"cohesive\":true,\"name_accurate\":true,\"reason\":\"\"}"}}]}"#;
    let (endpoint, hits) = spawn_openai_style_server(body);
    let analyzer = Analyzer {
        llm_client: Arc::new(LLMClient::new(cfg(&endpoint), Some("test-key".to_string()))),
        in_tok: AtomicUsize::new(0),
        out_tok: AtomicUsize::new(0),
    };
    let file = FileRecord {
        file_path: "sample.py".to_string(),
        source: "def sample():\n    return 1\n".to_string(),
        language: "python".to_string(),
        methods: vec![],
    };

    let (verdict, _, _) = analyzer.analyze_file(&file, &[]).await.unwrap();
    let verdict = verdict.expect("expected file verdict");
    assert_eq!(verdict.tier, FindingTier::Clean);
    assert!(verdict.reason.is_empty());
    assert!(verdict.evidence.is_empty());
    assert_eq!(hits.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn llm_error_fails_scan_without_starting_later_review_jobs() {
    let lock = ENV_LOCK.get_or_init(|| Mutex::new(())).lock().unwrap();
    unsafe {
        env::set_var("SNIFF_LLM_MAX_CONCURRENCY", "1");
        env::set_var("SNIFF_LLM_MAX_ATTEMPTS", "1");
    }

    let (endpoint, hits) = spawn_http_status_sequence_server(
        vec![500, 200],
        r#"{"choices":[{"message":{"content":"{\"smelly\":false,\"tier\":\"clean\",\"evidence\":\"def beta():\",\"cohesive\":true,\"name_accurate\":true,\"reason\":\"clean\"}"}}]}"#,
    );
    let client = Arc::new(LLMClient::new(cfg(&endpoint), Some("test-key".to_string())));

    let files = vec![
        FileRecord {
            file_path: "src/alpha.py".to_string(),
            source: "def alpha():\n    return 1\n".to_string(),
            language: "python".to_string(),
            methods: vec![],
        },
        FileRecord {
            file_path: "src/beta.py".to_string(),
            source: "def beta():\n    return 2\n".to_string(),
            language: "python".to_string(),
            methods: vec![],
        },
    ];

    let result = analyze_with_client(&files, &[], client, true, None).await;

    unsafe {
        env::remove_var("SNIFF_LLM_MAX_CONCURRENCY");
        env::remove_var("SNIFF_LLM_MAX_ATTEMPTS");
    }
    drop(lock);

    let err = result.expect_err("a partial AI scan must not produce a successful report");
    assert!(err.contains("LLM review failed"));
    assert!(err.contains("src/alpha.py"));
    assert_eq!(hits.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn llm_probe_surfaces_provider_failure() {
    let (endpoint, hits) = spawn_http_status_server(402, r#"{"error":"insufficient balance"}"#);
    let client = LLMClient::new(cfg(&endpoint), Some("test-key".to_string()));

    let err = client.probe().await.expect_err("expected probe failure");
    assert!(err.contains("LLM preflight failed"));
    assert!(err.contains("LLM provider balance is insufficient"));
    assert!(err.contains("HTTP 402"));
    assert_eq!(hits.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn missing_api_key_fails_when_reviews_are_required() {
    let _lock = ENV_LOCK.get_or_init(|| Mutex::new(())).lock().unwrap();
    let file = FileRecord {
        file_path: "sample.py".to_string(),
        source: "def sample():\n    return 1\n".to_string(),
        language: "python".to_string(),
        methods: vec![MethodRecord {
            name: "sample".to_string(),
            file_path: "sample.py".to_string(),
            source: "def sample():\n    return 1\n".to_string(),
            loc: 2,
            param_count: 0,
            start_line: 1,
            end_line: 2,
            is_exported: false,
            language: "python".to_string(),
            nesting_depth: 0,
            references: vec![],
            real_ref_count: 0,
        }],
    };

    unsafe {
        env::remove_var("SNIFF_API_KEY");
    }

    let err = analyze(&[file], &[], ResolvedConfig::default(), false, None)
        .await
        .expect_err("expected missing api key to fail when reviews are needed");
    assert!(err.contains("AI config is missing"));
}

#[tokio::test]
async fn file_review_includes_method_inventory() {
    let body = r#"{"choices":[{"message":{"content":"{\"smelly\":false,\"tier\":\"clean\",\"evidence\":\"def process_webhook()\",\"cohesive\":true,\"name_accurate\":true,\"reason\":\"clean\"}"}}]}"#;
    let (endpoint, hits, captured) = spawn_openai_style_server_with_capture(body);
    let analyzer = Analyzer {
        llm_client: Arc::new(LLMClient::new(cfg(&endpoint), Some("test-key".to_string()))),
        in_tok: AtomicUsize::new(0),
        out_tok: AtomicUsize::new(0),
    };
    let file = FileRecord {
        file_path: "src/services/webhook_service.py".to_string(),
        source: "def process_webhook():\n    return None\n".to_string(),
        language: "python".to_string(),
        methods: vec![MethodRecord {
            name: "process_webhook".to_string(),
            file_path: "src/services/webhook_service.py".to_string(),
            source: "def process_webhook():\n    return None\n".to_string(),
            loc: 126,
            param_count: 3,
            start_line: 1,
            end_line: 2,
            is_exported: true,
            language: "python".to_string(),
            nesting_depth: 0,
            references: vec![],
            real_ref_count: 0,
        }],
    };

    let (_verdict, _, _) = analyzer.analyze_file(&file, &[]).await.unwrap();
    let request = captured.lock().unwrap().clone();
    assert!(request.contains("Method inventory:"));
    assert!(request.contains("- process_webhook (126 LOC, 3 params)"));
    assert_eq!(hits.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn method_review_includes_file_path() {
    let body = r#"{"choices":[{"message":{"content":"{\"smelly\":false,\"tier\":\"clean\",\"evidence\":\"\",\"reason\":\"clean\"}"}}]}"#;
    let (endpoint, hits, captured) = spawn_openai_style_server_with_capture(body);
    let analyzer = Analyzer {
        llm_client: Arc::new(LLMClient::new(cfg(&endpoint), Some("test-key".to_string()))),
        in_tok: AtomicUsize::new(0),
        out_tok: AtomicUsize::new(0),
    };
    let method = MethodRecord {
        name: "process_webhook".to_string(),
        file_path: "src/services/webhook_service.py".to_string(),
        source: "def process_webhook():\n    return None\n".to_string(),
        loc: 126,
        param_count: 3,
        start_line: 1,
        end_line: 2,
        is_exported: true,
        language: "python".to_string(),
        nesting_depth: 0,
        references: vec![],
        real_ref_count: 0,
    };

    let (_verdict, _, _) = analyzer
        .analyze_method_review_with_context(
            &method,
            &[],
            "class WebhookService:\n    def other_handler(self):\n        return None",
            None,
        )
        .await
        .unwrap();
    let request = captured.lock().unwrap().clone();
    assert!(request.contains("File path:"));
    assert!(request.contains("src/services/webhook_service.py"));
    assert!(request.contains("Surrounding file context"));
    assert!(request.contains("class WebhookService:"));
    assert_eq!(hits.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn method_review_sends_the_complete_method_source() {
    let body = r#"{"choices":[{"message":{"content":"{\"smelly\":false,\"tier\":\"clean\",\"evidence\":\"\",\"reason\":\"clean\"}"}}]}"#;
    let (endpoint, _hits, captured) = spawn_openai_style_server_with_capture(body);
    let analyzer = Analyzer {
        llm_client: Arc::new(LLMClient::new(cfg(&endpoint), Some("test-key".to_string()))),
        in_tok: AtomicUsize::new(0),
        out_tok: AtomicUsize::new(0),
    };
    let method_tail = "METHOD_SOURCE_TAIL_SENTINEL";
    let source = format!(
        "def long_method(value):\n{}\n    return {method_tail}\n",
        "    value = value\n".repeat(260)
    );
    let method = MethodRecord {
        name: "long_method".to_string(),
        file_path: "src/long.py".to_string(),
        source,
        loc: 262,
        param_count: 1,
        start_line: 1,
        end_line: 263,
        is_exported: true,
        language: "python".to_string(),
        nesting_depth: 0,
        references: vec![],
        real_ref_count: 0,
    };

    analyzer.analyze_method_review(&method, &[]).await.unwrap();
    let request = captured.lock().unwrap().clone();
    assert!(
        request.contains(method_tail),
        "the method review prompt must contain the tail of a long method"
    );
}

#[tokio::test]
async fn file_review_sends_the_complete_file_source() {
    let body = r#"{"choices":[{"message":{"content":"{\"smelly\":false,\"tier\":\"clean\",\"evidence\":\"\",\"cohesive\":true,\"name_accurate\":true,\"reason\":\"clean\"}"}}]}"#;
    let (endpoint, _hits, captured) = spawn_openai_style_server_with_capture(body);
    let analyzer = Analyzer {
        llm_client: Arc::new(LLMClient::new(cfg(&endpoint), Some("test-key".to_string()))),
        in_tok: AtomicUsize::new(0),
        out_tok: AtomicUsize::new(0),
    };
    let file_tail = "FILE_SOURCE_TAIL_SENTINEL";
    let source = format!(
        "{}\n# {file_tail}\n",
        "def helper(value):\n    return value\n".repeat(180)
    );
    let file = FileRecord {
        file_path: "src/large.py".to_string(),
        source,
        language: "python".to_string(),
        methods: vec![],
    };

    analyzer.analyze_file(&file, &[]).await.unwrap();
    let request = captured.lock().unwrap().clone();
    assert!(
        request.contains(file_tail),
        "the file review prompt must contain the tail of a long file"
    );
}

#[tokio::test]
async fn file_review_rejects_evidence_only_present_in_rust_cfg_test_code() {
    let body = r#"{"choices":[{"message":{"content":"{\"smelly\":true,\"tier\":\"slop\",\"evidence\":\"fn test_only_helper() {}\",\"cohesive\":false,\"name_accurate\":true,\"reason\":\"file does too much\"}"}}]}"#;
    let (endpoint, hits) = spawn_openai_style_server(body);
    let analyzer = Analyzer {
        llm_client: Arc::new(LLMClient::new(cfg(&endpoint), Some("test-key".to_string()))),
        in_tok: AtomicUsize::new(0),
        out_tok: AtomicUsize::new(0),
    };
    let file = FileRecord {
        file_path: "src/lib.rs".to_string(),
        source:
            "pub fn production() {}\n\n#[cfg(test)]\nmod tests {\n    fn test_only_helper() {}\n}\n"
                .to_string(),
        language: "rust".to_string(),
        methods: vec![],
    };

    let (verdict, _, _) = analyzer.analyze_file(&file, &[]).await.unwrap();
    let verdict = verdict.expect("expected file verdict");
    assert_eq!(verdict.tier, FindingTier::Clean);
    assert!(!verdict.smelly);
    assert_eq!(hits.load(Ordering::SeqCst), 4);
}

#[tokio::test]
async fn invalid_file_evidence_is_rejected() {
    let body = r#"{"choices":[{"message":{"content":"{\"smelly\":true,\"tier\":\"slop\",\"evidence\":\"dict[str, str] = 1\",\"cohesive\":false,\"name_accurate\":false,\"reason\":\"type annotation mismatch\"}"}}]}"#;
    let (endpoint, hits) = spawn_openai_style_server(body);
    let analyzer = Analyzer {
        llm_client: Arc::new(LLMClient::new(cfg(&endpoint), Some("test-key".to_string()))),
        in_tok: AtomicUsize::new(0),
        out_tok: AtomicUsize::new(0),
    };
    let file = FileRecord {
        file_path: "sample.py".to_string(),
        source: "def sample():\n    return 1\n".to_string(),
        language: "python".to_string(),
        methods: vec![],
    };

    let (verdict, _, _) = analyzer.analyze_file(&file, &[]).await.unwrap();
    let verdict = verdict.expect("expected file verdict");
    assert_eq!(verdict.tier, FindingTier::Clean);
    assert!(!verdict.smelly);
    assert!(verdict.reason.is_empty());
    assert!(verdict.evidence.is_empty());
    assert_eq!(hits.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn invalid_method_evidence_is_rejected() {
    let body = r#"{"choices":[{"message":{"content":"{\"smelly\":true,\"tier\":\"slop\",\"evidence\":\"dict[str, list[JsTsFunctionSignature]] = 2\",\"reason\":\"type annotation mismatch\"}"}}]}"#;
    let (endpoint, hits) = spawn_openai_style_server(body);
    let analyzer = Analyzer {
        llm_client: Arc::new(LLMClient::new(cfg(&endpoint), Some("test-key".to_string()))),
        in_tok: AtomicUsize::new(0),
        out_tok: AtomicUsize::new(0),
    };
    let method = MethodRecord {
        name: "run_js_ts_export_detection".to_string(),
        file_path: "sample.ts".to_string(),
        source: "export function run_js_ts_export_detection() {\n    return [];\n}\n".to_string(),
        loc: 3,
        param_count: 0,
        start_line: 1,
        end_line: 3,
        is_exported: true,
        language: "typescript".to_string(),
        nesting_depth: 0,
        references: vec![],
        real_ref_count: 0,
    };

    let (verdict, _, _) = analyzer.analyze_method_review(&method, &[]).await.unwrap();
    let verdict = verdict.expect("expected method verdict");
    assert_eq!(verdict.tier, FindingTier::Clean);
    assert!(!verdict.smelly);
    assert!(verdict.reason.is_empty());
    assert!(verdict.evidence.is_empty());
    assert_eq!(hits.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn invalid_evidence_retries_can_rescue_a_valid_slop_verdict() {
    let invalid = r#"{"choices":[{"message":{"content":"{\"smelly\":true,\"tier\":\"kinda_slop\",\"evidence\":\"not in source\",\"cohesive\":false,\"name_accurate\":false,\"reason\":\"small helper\"}"}}]}"#;
    let valid = r#"{"choices":[{"message":{"content":"{\"smelly\":true,\"tier\":\"kinda_slop\",\"evidence\":\"return 1\",\"cohesive\":false,\"name_accurate\":false,\"reason\":\"small helper\"}"}}]}"#;
    let (endpoint, hits) = spawn_openai_style_server_sequence(vec![invalid, invalid, valid, valid]);
    let analyzer = Analyzer {
        llm_client: Arc::new(LLMClient::new(cfg(&endpoint), Some("test-key".to_string()))),
        in_tok: AtomicUsize::new(0),
        out_tok: AtomicUsize::new(0),
    };
    let file = FileRecord {
        file_path: "sample.py".to_string(),
        source: "def sample():\n    return 1\n".to_string(),
        language: "python".to_string(),
        methods: vec![],
    };

    let events = Arc::new(Mutex::new(Vec::new()));
    let events_sink = Arc::clone(&events);
    let on_progress: ReviewProgressCallback = Arc::new(move |event| {
        events_sink.lock().unwrap().push(event);
    });
    let (verdicts, _, _) = analyze_with_client(
        std::slice::from_ref(&file),
        &[],
        Arc::clone(&analyzer.llm_client),
        true,
        Some(on_progress),
    )
    .await
    .unwrap();
    let verdict = verdicts.into_iter().next().expect("expected file verdict");
    assert_eq!(verdict.tier, FindingTier::KindaSlop);
    assert!(verdict.smelly);
    assert_eq!(verdict.reason, "small helper");
    assert_eq!(verdict.evidence, "return 1");
    assert_eq!(hits.load(Ordering::SeqCst), 4);
    assert_eq!(
        *events.lock().unwrap(),
        vec![
            ReviewProgress::Started {
                label: "file sample.py".to_string(),
            },
            ReviewProgress::RetryingEvidence {
                label: "file sample.py".to_string(),
            },
            ReviewProgress::Started {
                label: "file sample.py".to_string(),
            },
            ReviewProgress::Completed,
        ]
    );
}

#[tokio::test]
async fn no_json_response_retries_same_request_once() {
    let _lock = ENV_LOCK.get_or_init(|| Mutex::new(())).lock().unwrap();
    let first = r#"{"choices":[{"message":{"content":"I am thinking aloud, not JSON."}}]}"#;
    let second = r#"{"choices":[{"message":{"content":"{\"smelly\":true,\"tier\":\"kinda_slop\",\"evidence\":\"return 1\",\"reason\":\"small helper\"}"}}]}"#;
    let third = second;
    let (endpoint, hits) = spawn_openai_style_server_sequence(vec![first, second, third]);
    let analyzer = Analyzer {
        llm_client: Arc::new(LLMClient::new(cfg(&endpoint), Some("test-key".to_string()))),
        in_tok: AtomicUsize::new(0),
        out_tok: AtomicUsize::new(0),
    };
    let method = MethodRecord {
        name: "sample".to_string(),
        file_path: "sample.py".to_string(),
        source: "def sample():\n    return 1\n".to_string(),
        loc: 2,
        param_count: 0,
        start_line: 1,
        end_line: 2,
        is_exported: false,
        language: "python".to_string(),
        nesting_depth: 0,
        references: vec![],
        real_ref_count: 0,
    };

    let (verdict, _, _) = analyzer.analyze_method_review(&method, &[]).await.unwrap();
    let verdict = verdict.expect("expected method verdict");
    assert_eq!(verdict.tier, FindingTier::KindaSlop);
    assert_eq!(hits.load(Ordering::SeqCst), 3);
}