selfware 0.6.1

Your personal AI workshop — software you own, software that lasts
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
//! Full flow integration test — exercises the complete user journey:
//!
//! 1. Auto-config endpoint detection
//! 2. Throughput benchmark (16 concurrent)
//! 3. SWE-bench agent loop (3 tasks)
//! 4. Error detection and self-healing validation
//!
//! Tests against a live endpoint and reports any unexpected errors.
//!
//! Run with:
//!   cargo run --features bench-harness --example full_flow_test -- http://127.0.0.1:8000/v1

use std::future::Future;
use std::path::PathBuf;
use std::time::{Duration, Instant};

use anyhow::Result;
use serde_json::json;

use selfware::api::types::Message;
use selfware::bench_harness::*;
use selfware::config::auto_config::AutoConfigurator;
use selfware::self_healing::{ErrorOccurrence, SelfHealingConfig, SelfHealingEngine};
use selfware::tools::vision::{encode_image_file, VisionCompare};
use selfware::tools::Tool;
use selfware::visual_verification::{VisualDiffResult, VisualVerifier};

#[derive(Debug)]
struct FlowResult {
    step: String,
    passed: bool,
    duration_secs: f64,
    details: String,
    errors: Vec<String>,
}

impl FlowResult {
    fn ok(step: &str, duration: f64, details: &str) -> Self {
        Self {
            step: step.to_string(),
            passed: true,
            duration_secs: duration,
            details: details.to_string(),
            errors: vec![],
        }
    }

    fn fail(step: &str, duration: f64, details: &str, errors: Vec<String>) -> Self {
        Self {
            step: step.to_string(),
            passed: false,
            duration_secs: duration,
            details: details.to_string(),
            errors,
        }
    }

    fn with_note(mut self, note: impl AsRef<str>) -> Self {
        let note = note.as_ref();
        if !note.is_empty() {
            self.details = format!("{} | {}", self.details, note);
        }
        self
    }
}

#[derive(Debug, Clone)]
struct EndpointTarget {
    endpoint: String,
    concurrent: usize,
}

fn default_targets() -> Vec<EndpointTarget> {
    vec![
        EndpointTarget {
            endpoint: "http://localhost:8000/v1".to_string(),
            concurrent: 16,
        },
        EndpointTarget {
            endpoint: "http://127.0.0.1:8000/v1".to_string(),
            concurrent: 64,
        },
    ]
}

fn is_local_endpoint(endpoint: &str) -> bool {
    endpoint.contains("localhost") || endpoint.contains("127.0.0.1") || endpoint.contains("[::1]")
}

fn infer_text_concurrency(endpoint: &str) -> usize {
    if is_local_endpoint(endpoint) {
        16
    } else {
        64
    }
}

fn infer_vision_concurrency(endpoint: &str) -> usize {
    if is_local_endpoint(endpoint) {
        4
    } else {
        8
    }
}

fn infer_vision_timeout(endpoint: &str) -> Duration {
    if is_local_endpoint(endpoint) {
        Duration::from_secs(30)
    } else {
        Duration::from_secs(45)
    }
}

fn sanitize_endpoint(endpoint: &str) -> String {
    endpoint
        .replace("://", "_")
        .replace(['/', ':', '.'], "_")
        .trim_matches('_')
        .to_string()
}

fn preview(text: &str, limit: usize) -> String {
    text.chars().take(limit).collect()
}

fn classify_flow_errors(errors: &[String]) -> (&'static str, String) {
    let message = if errors.is_empty() {
        "unknown endpoint step failure".to_string()
    } else {
        errors.join(" | ")
    };
    let lower = message.to_lowercase();

    let error_type = if lower.contains("429") || lower.contains("rate limit") {
        "rate_limit"
    } else if lower.contains("timeout") || lower.contains("timed out") {
        "timeout"
    } else if lower.contains("connection")
        || lower.contains("network")
        || lower.contains("refused")
        || lower.contains("reset")
        || lower.contains("circuit breaker")
    {
        "network"
    } else if lower.contains("parse") || lower.contains("json") || lower.contains("invalid") {
        "parse"
    } else {
        "unknown"
    };

    (error_type, message)
}

async fn retry_after_self_heal<F, Fut>(
    engine: &SelfHealingEngine,
    endpoint: &str,
    step: &str,
    errors: &[String],
    run: F,
) -> Option<FlowResult>
where
    F: FnOnce() -> Fut,
    Fut: Future<Output = FlowResult>,
{
    let (error_type, message) = classify_flow_errors(errors);
    let context = format!("{step}@{endpoint}");
    let recovery = engine
        .handle_error(ErrorOccurrence::new(error_type, &message, &context))
        .await?;

    if recovery.success {
        Some(run().await)
    } else {
        None
    }
}

fn vision_fixture(name: &str) -> PathBuf {
    PathBuf::from("vlm_fixtures/l1_tui_state").join(name)
}

// ---- Step 1: Auto-config ----
async fn test_auto_config(endpoint: &str) -> FlowResult {
    let start = Instant::now();
    eprintln!("\n=== Step 1: Auto-Configuration ===");

    let configurator = AutoConfigurator::new(endpoint, None);

    // Fetch models
    let models = match configurator.fetch_models().await {
        Ok(m) => m,
        Err(e) => {
            return FlowResult::fail(
                "auto-config",
                start.elapsed().as_secs_f64(),
                "Failed to fetch models",
                vec![format!("{e}")],
            );
        }
    };

    if models.is_empty() {
        return FlowResult::fail(
            "auto-config",
            start.elapsed().as_secs_f64(),
            "No models found",
            vec!["Endpoint returned empty model list".into()],
        );
    }

    let model = &models[0];
    eprintln!("  Model: {} ({})", model.id, model.owned_by);
    eprintln!("  Context: {} tokens", model.max_model_len);

    // Run detection tests
    let results = match configurator.run_tests(&model.id).await {
        Ok(r) => r,
        Err(e) => {
            return FlowResult::fail(
                "auto-config",
                start.elapsed().as_secs_f64(),
                "Detection tests failed",
                vec![format!("{e}")],
            );
        }
    };

    let mut errors = vec![];
    if !results.chat_works {
        errors.push("Chat API not working".into());
    }

    let details = format!(
        "backend={}, model={}, context={}, fc={}, streaming={}, thinking={}, disable_thinking={}, text_concurrency={}, vision_concurrency={}",
        results
            .backend_type
            .map(|b| b.name())
            .unwrap_or("unknown"),
        model.id,
        model.max_model_len,
        results.function_calling,
        results.streaming,
        results.thinking_supported,
        results.thinking_eats_tokens,
        infer_text_concurrency(endpoint),
        infer_vision_concurrency(endpoint),
    );

    if errors.is_empty() {
        FlowResult::ok("auto-config", start.elapsed().as_secs_f64(), &details)
    } else {
        FlowResult::fail(
            "auto-config",
            start.elapsed().as_secs_f64(),
            &details,
            errors,
        )
    }
}

// ---- Step 2: Throughput benchmark ----
async fn test_throughput(endpoint: &str, model: &str, concurrent: usize) -> FlowResult {
    let start = Instant::now();
    eprintln!("\n=== Step 2: Throughput Benchmark ({concurrent} concurrent) ===");

    let config = HarnessConfig {
        endpoint: endpoint.to_string(),
        model: model.to_string(),
        max_concurrent: concurrent,
        max_tokens: 256,
        temperature: 0.2,
        timeout_secs: if concurrent > 32 { 180 } else { 120 },
        max_retries: 3,
        retry_delay_ms: 500,
        output_dir: "bench_results/flow_test".into(),
        api_key: None,
        extra_body: json!({"chat_template_kwargs": {"enable_thinking": false}}),
    };

    let runner = match HarnessRunner::new(config) {
        Ok(r) => r,
        Err(e) => {
            return FlowResult::fail(
                "throughput",
                start.elapsed().as_secs_f64(),
                "Runner creation failed",
                vec![format!("{e}")],
            );
        }
    };

    // Generate tasks
    let tasks: Vec<BenchTask> = (0..concurrent)
        .map(|i| {
            let prompts = [
                "What is 2+2? Answer with just the number.",
                "Name the capital of France in one word.",
                "Is Rust a compiled language? Yes or no.",
                "What color is the sky? One word.",
            ];
            BenchTask {
                id: format!("flow-{i}"),
                description: format!("Quick test {i}"),
                messages: vec![
                    Message::system("Answer concisely in one sentence."),
                    Message::user(prompts[i % prompts.len()]),
                ],
                evaluator: Box::new(NoopEvaluator),
            }
        })
        .collect();

    let report = match runner.run(tasks).await {
        Ok(r) => r,
        Err(e) => {
            return FlowResult::fail(
                "throughput",
                start.elapsed().as_secs_f64(),
                "Benchmark run failed",
                vec![format!("{e}")],
            );
        }
    };

    let mut errors = vec![];

    // Check for unexpected failures
    if report.error_rate > 0.5 {
        errors.push(format!(
            "High error rate: {:.0}% ({} failed)",
            report.error_rate * 100.0,
            report.tasks_failed
        ));
    }

    // Check for timeouts
    let timeouts = report
        .results
        .iter()
        .filter(|r| {
            r.error
                .as_ref()
                .map(|e| e.contains("timeout"))
                .unwrap_or(false)
        })
        .count();
    if timeouts > 0 {
        errors.push(format!("{timeouts} tasks timed out"));
    }

    // Check for empty responses (thinking mode issue)
    let empty_responses = report
        .results
        .iter()
        .filter(|r| r.response.is_empty() && r.error.is_none())
        .count();
    if empty_responses > 0 {
        errors.push(format!(
            "{empty_responses} empty responses (likely thinking mode eating tokens)"
        ));
    }

    // Check for connection errors
    let conn_errors = report
        .results
        .iter()
        .filter(|r| {
            r.error
                .as_ref()
                .map(|e| e.contains("connection") || e.contains("refused") || e.contains("reset"))
                .unwrap_or(false)
        })
        .count();
    if conn_errors > 0 {
        errors.push(format!("{conn_errors} connection errors"));
    }

    let details = format!(
        "passed={}/{}, tok/s={:.0}, p50={:.1}s, p95={:.1}s, errors={}",
        report.tasks_passed,
        report.tasks_total,
        report.tokens_per_sec,
        report.latency_p50_ms as f64 / 1000.0,
        report.latency_p95_ms as f64 / 1000.0,
        report.tasks_failed,
    );

    // Print per-task errors for debugging
    for r in &report.results {
        if let Some(err) = &r.error {
            eprintln!("  [{}] ERROR: {}", r.task_id, &err[..err.len().min(100)]);
        } else if r.response.is_empty() {
            eprintln!(
                "  [{}] WARN: empty response (tokens: {}+{})",
                r.task_id, r.prompt_tokens, r.completion_tokens
            );
        }
    }

    if errors.is_empty() {
        FlowResult::ok("throughput", start.elapsed().as_secs_f64(), &details)
    } else {
        FlowResult::fail(
            "throughput",
            start.elapsed().as_secs_f64(),
            &details,
            errors,
        )
    }
}

// ---- Step 3: Error resilience ----
async fn test_error_resilience(endpoint: &str, model: &str, burst_size: usize) -> FlowResult {
    let start = Instant::now();
    eprintln!("\n=== Step 3: Error Resilience ===");

    let client = reqwest::Client::builder()
        .timeout(std::time::Duration::from_secs(30))
        .build()
        .unwrap();

    let mut errors = vec![];
    let mut tests_run = 0;
    let mut tests_passed = 0;

    // Test 3a: Invalid model name
    eprintln!("  [3a] Testing invalid model name...");
    tests_run += 1;
    let resp = client
        .post(format!("{}/chat/completions", endpoint))
        .json(&json!({
            "model": "nonexistent-model-12345",
            "messages": [{"role": "user", "content": "hi"}],
            "max_tokens": 16,
        }))
        .send()
        .await;
    match resp {
        Ok(r) => {
            if r.status().is_success() {
                errors.push("Invalid model accepted (should return error)".into());
            } else {
                eprintln!("    OK: HTTP {} for invalid model", r.status());
                tests_passed += 1;
            }
        }
        Err(e) => {
            errors.push(format!("Connection error on invalid model test: {e}"));
        }
    }

    // Test 3b: Empty messages
    eprintln!("  [3b] Testing empty messages...");
    tests_run += 1;
    let resp = client
        .post(format!("{}/chat/completions", endpoint))
        .json(&json!({
            "model": model,
            "messages": [],
            "max_tokens": 16,
        }))
        .send()
        .await;
    match resp {
        Ok(r) => {
            let status = r.status();
            if status.is_server_error() {
                errors.push(format!("Server error on empty messages: HTTP {status}"));
            } else {
                eprintln!("    OK: HTTP {} for empty messages", status);
                tests_passed += 1;
            }
        }
        Err(e) => {
            errors.push(format!("Connection error on empty messages: {e}"));
        }
    }

    // Test 3c: max_tokens=1 (minimal response)
    eprintln!("  [3c] Testing max_tokens=1...");
    tests_run += 1;
    let resp = client
        .post(format!("{}/chat/completions", endpoint))
        .json(&json!({
            "model": model,
            "messages": [{"role": "user", "content": "hi"}],
            "max_tokens": 1,
            "chat_template_kwargs": {"enable_thinking": false},
        }))
        .send()
        .await;
    match resp {
        Ok(r) => {
            if r.status().is_success() {
                eprintln!("    OK: max_tokens=1 handled gracefully");
                tests_passed += 1;
            } else {
                errors.push(format!("max_tokens=1 failed: HTTP {}", r.status()));
            }
        }
        Err(e) => {
            errors.push(format!("max_tokens=1 connection error: {e}"));
        }
    }

    // Test 3d: Very long prompt (near context limit)
    eprintln!("  [3d] Testing large prompt (50K tokens)...");
    tests_run += 1;
    let large_prompt = "Repeat this word: hello. ".repeat(10000); // ~50K tokens
    let resp = client
        .post(format!("{}/chat/completions", endpoint))
        .json(&json!({
            "model": model,
            "messages": [{"role": "user", "content": large_prompt}],
            "max_tokens": 16,
            "chat_template_kwargs": {"enable_thinking": false},
        }))
        .send()
        .await;
    match resp {
        Ok(r) => {
            let status = r.status();
            let body = r.text().await.unwrap_or_default();
            if status.is_success() || status.as_u16() == 400 {
                // 400 is acceptable (prompt too long)
                eprintln!("    OK: HTTP {} for large prompt", status);
                tests_passed += 1;
            } else {
                errors.push(format!(
                    "Large prompt unexpected: HTTP {status}: {}",
                    &body[..body.len().min(200)]
                ));
            }
        }
        Err(e) => {
            errors.push(format!("Large prompt connection error: {e}"));
        }
    }

    // Test 3e: Concurrent burst (stress test)
    let burst_size = burst_size.max(4);
    eprintln!("  [3e] Testing concurrent burst ({burst_size} simultaneous)...");
    tests_run += 1;
    let mut handles = vec![];
    for i in 0..burst_size {
        let c = client.clone();
        let url = format!("{}/chat/completions", endpoint);
        let m = model.to_string();
        handles.push(tokio::spawn(async move {
            c.post(&url)
                .json(&json!({
                    "model": m,
                    "messages": [{"role": "user", "content": format!("Say {i}")}],
                    "max_tokens": 16,
                    "chat_template_kwargs": {"enable_thinking": false},
                }))
                .send()
                .await
                .map(|r| r.status().is_success())
                .unwrap_or(false)
        }));
    }

    let mut burst_ok = 0;
    let mut burst_fail = 0;
    for h in handles {
        match h.await {
            Ok(true) => burst_ok += 1,
            _ => burst_fail += 1,
        }
    }
    if burst_fail > burst_size / 2 {
        // More than half failed
        errors.push(format!("Burst test: {burst_fail}/{burst_size} failed"));
    } else {
        eprintln!("    OK: {burst_ok}/{burst_size} succeeded in burst");
        tests_passed += 1;
    }

    let details = format!("{tests_passed}/{tests_run} resilience tests passed");

    if errors.is_empty() {
        FlowResult::ok("error-resilience", start.elapsed().as_secs_f64(), &details)
    } else {
        FlowResult::fail(
            "error-resilience",
            start.elapsed().as_secs_f64(),
            &details,
            errors,
        )
    }
}

// ---- Step 4: Tool calling validation ----
async fn test_tool_calling(endpoint: &str, model: &str) -> FlowResult {
    let start = Instant::now();
    eprintln!("\n=== Step 4: Tool Calling Validation ===");

    let client = reqwest::Client::builder()
        .timeout(std::time::Duration::from_secs(60))
        .build()
        .unwrap();

    let mut errors = vec![];

    // Test with tools + thinking disabled
    eprintln!("  [4a] Testing tool calling (thinking disabled)...");
    let resp = client
        .post(format!("{}/chat/completions", endpoint))
        .json(&json!({
            "model": model,
            "messages": [{"role": "user", "content": "Read the file README.md"}],
            "tools": [{
                "type": "function",
                "function": {
                    "name": "file_read",
                    "description": "Read a file from disk",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "path": {"type": "string", "description": "File path to read"}
                        },
                        "required": ["path"]
                    }
                }
            }],
            "max_tokens": 512,
            "temperature": 0.0,
            "chat_template_kwargs": {"enable_thinking": false},
        }))
        .send()
        .await;

    match resp {
        Ok(r) => {
            let status = r.status();
            let body: serde_json::Value = r.json().await.unwrap_or(json!({}));

            if !status.is_success() {
                errors.push(format!("Tool calling HTTP {status}"));
            } else {
                let tool_calls = body["choices"][0]["message"]["tool_calls"]
                    .as_array()
                    .map(|a| a.len())
                    .unwrap_or(0);
                let content = body["choices"][0]["message"]["content"]
                    .as_str()
                    .unwrap_or("");
                let has_xml_tool = content.contains("<tool") || content.contains("file_read");

                if tool_calls > 0 {
                    eprintln!("    OK: Native FC returned {} tool_calls", tool_calls);
                } else if has_xml_tool {
                    eprintln!("    OK: Model outputs tool calls in text (XML format)");
                } else {
                    errors.push(format!(
                        "No tool calls in response (native={}, content={}chars)",
                        tool_calls,
                        content.len()
                    ));
                }
            }
        }
        Err(e) => {
            errors.push(format!("Tool calling request failed: {e}"));
        }
    }

    // Test with tools + thinking enabled
    eprintln!("  [4b] Testing tool calling (thinking enabled)...");
    let resp = client
        .post(format!("{}/chat/completions", endpoint))
        .json(&json!({
            "model": model,
            "messages": [{"role": "user", "content": "Read the file README.md"}],
            "tools": [{
                "type": "function",
                "function": {
                    "name": "file_read",
                    "description": "Read a file",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "path": {"type": "string"}
                        },
                        "required": ["path"]
                    }
                }
            }],
            "max_tokens": 1024,
            "temperature": 0.0,
        }))
        .send()
        .await;

    match resp {
        Ok(r) => {
            let status = r.status();
            let body: serde_json::Value = r.json().await.unwrap_or(json!({}));

            if !status.is_success() {
                errors.push(format!("Tool calling with thinking HTTP {status}"));
            } else {
                let tool_calls = body["choices"][0]["message"]["tool_calls"]
                    .as_array()
                    .map(|a| a.len())
                    .unwrap_or(0);
                let content = body["choices"][0]["message"]["content"]
                    .as_str()
                    .unwrap_or("");
                let reasoning = body["choices"][0]["message"]["reasoning_content"]
                    .as_str()
                    .or(body["choices"][0]["message"]["reasoning"].as_str());
                let content_null = content.is_empty();

                eprintln!(
                    "    Thinking: reasoning={}chars, content={}chars, tool_calls={}, content_null={}",
                    reasoning.map(|r| r.len()).unwrap_or(0),
                    content.len(),
                    tool_calls,
                    content_null,
                );

                if content_null && tool_calls == 0 && reasoning.is_some() {
                    errors.push("Thinking mode: content is null AND no tool_calls (all tokens consumed by reasoning)".into());
                }
            }
        }
        Err(e) => {
            errors.push(format!("Tool calling with thinking failed: {e}"));
        }
    }

    let details = format!("{} issues found", errors.len());
    if errors.is_empty() {
        FlowResult::ok("tool-calling", start.elapsed().as_secs_f64(), &details)
    } else {
        FlowResult::fail(
            "tool-calling",
            start.elapsed().as_secs_f64(),
            &details,
            errors,
        )
    }
}

// ---- Step 5: Vision workflows ----
async fn test_vision_workflows(
    endpoint: &str,
    model: &str,
    detail: &str,
    max_tokens: usize,
) -> FlowResult {
    let start = Instant::now();
    let request_timeout = infer_vision_timeout(endpoint);
    eprintln!("\n=== Step 5: Vision Workflows (detail={detail}, max_tokens={max_tokens}) ===");

    let compare = VisionCompare;
    let mut errors = vec![];
    let mut tests_run = 0;
    let mut tests_passed = 0;
    let mut analyze_latency_secs = 0.0;
    let mut compare_latency_secs = 0.0;

    let dashboard_normal = vision_fixture("dashboard_normal.png");
    let help_panel = vision_fixture("help_panel.png");
    let dashboard_path = dashboard_normal.display().to_string();
    let help_panel_path = help_panel.display().to_string();

    let dashboard_base64 = match encode_image_file(&dashboard_path) {
        Ok(image) => image,
        Err(e) => {
            return FlowResult::fail(
                "vision-workflows",
                start.elapsed().as_secs_f64(),
                "Failed to load dashboard fixture",
                vec![e.to_string()],
            );
        }
    };
    let help_panel_base64 = match encode_image_file(&help_panel_path) {
        Ok(image) => image,
        Err(e) => {
            return FlowResult::fail(
                "vision-workflows",
                start.elapsed().as_secs_f64(),
                "Failed to load help-panel fixture",
                vec![e.to_string()],
            );
        }
    };

    let verifier_extra_body = json!({
        "chat_template_kwargs": { "enable_thinking": false }
    });
    let verifier = VisualVerifier::new(endpoint, model)
        .with_timeout(request_timeout.as_secs())
        .with_generation(max_tokens, 0.0)
        .with_image_detail(detail)
        .with_extra_body(verifier_extra_body.as_object().cloned());

    tests_run += 1;
    eprintln!("  [5a] Testing single-image analysis...");
    let analyze_start = Instant::now();
    match tokio::time::timeout(
        request_timeout,
        verifier.verify_screenshot(
            &help_panel_base64,
            "A terminal dashboard with a help or shortcut panel open and readable shortcut text visible inside the panel.",
        ),
    )
    .await
    {
        Ok(Ok(analysis)) => {
            analyze_latency_secs = analyze_start.elapsed().as_secs_f64();
            let informative = analysis.passed && !analysis.description.trim().is_empty();
            let preview_text = preview(&analysis.description, 100);

            if informative {
                eprintln!("    OK: {preview_text}");
                tests_passed += 1;
            } else {
                let issue_text = if analysis.issues.is_empty() {
                    "no issues reported".to_string()
                } else {
                    analysis.issues.join("; ")
                };
                errors.push(format!(
                    "Single-image analysis mismatch: passed={}, description={}, issues={}",
                    analysis.passed,
                    preview_text,
                    preview(&issue_text, 120),
                ));
            }
        }
        Ok(Err(e)) => errors.push(format!("Single-image analysis failed: {e}")),
        Err(_) => errors.push(format!(
            "Single-image analysis timed out after {}s",
            request_timeout.as_secs()
        )),
    }

    tests_run += 1;
    eprintln!("  [5b] Testing image comparison workflow...");
    let compare_start = Instant::now();
    match tokio::time::timeout(
        request_timeout,
        compare.execute(json!({
            "image_a": dashboard_path,
            "image_b": help_panel_path,
            "threshold": 99.9,
        })),
    )
    .await
    {
        Ok(Ok(value)) => {
            compare_latency_secs = compare_start.elapsed().as_secs_f64();
            let pixel_similarity = value["pixel_similarity"].as_f64().unwrap_or(0.0);
            let semantic = match tokio::time::timeout(
                request_timeout,
                verifier.compare_screenshots(
                    &dashboard_base64,
                    &help_panel_base64,
                    "Image 2 should show a help or shortcut panel opened over the dashboard with additional visible shortcut text.",
                ),
            )
            .await
            {
                Ok(Ok(result)) => result,
                Ok(Err(e)) => {
                    errors.push(format!("Structured semantic compare failed: {e}"));
                    VisualDiffResult {
                        changes_detected: false,
                        expected_change_found: false,
                        description: String::new(),
                        unexpected_changes: vec![],
                    }
                }
                Err(_) => {
                    errors.push(format!(
                        "Structured semantic compare timed out after {}s",
                        request_timeout.as_secs()
                    ));
                    VisualDiffResult {
                        changes_detected: false,
                        expected_change_found: false,
                        description: String::new(),
                        unexpected_changes: vec![],
                    }
                }
            };
            let semantic_ok = semantic.changes_detected && semantic.expected_change_found;
            let semantic_preview = preview(&semantic.description, 120);
            let unexpected = if semantic.unexpected_changes.is_empty() {
                String::new()
            } else {
                format!(
                    ", unexpected={}",
                    preview(&semantic.unexpected_changes.join("; "), 120)
                )
            };

            if pixel_similarity < 99.9 && semantic_ok {
                eprintln!(
                    "    OK: pixel_similarity={:.1}, semantic={}",
                    pixel_similarity, semantic_preview
                );
                tests_passed += 1;
            } else {
                errors.push(format!(
                    "Image comparison too weak: pixel_similarity={:.1}, semantic={}{}",
                    pixel_similarity, semantic_preview, unexpected
                ));
            }
        }
        Ok(Err(e)) => errors.push(format!("Image comparison failed: {e}")),
        Err(_) => errors.push(format!(
            "Image comparison timed out after {}s",
            request_timeout.as_secs()
        )),
    }

    let details = format!(
        "{tests_passed}/{tests_run} vision checks passed, detail={detail}, max_tokens={max_tokens}, timeout={}s, analyze={:.1}s, compare={:.1}s",
        request_timeout.as_secs(),
        analyze_latency_secs,
        compare_latency_secs,
    );

    if errors.is_empty() {
        FlowResult::ok("vision-workflows", start.elapsed().as_secs_f64(), &details)
    } else {
        FlowResult::fail(
            "vision-workflows",
            start.elapsed().as_secs_f64(),
            &details,
            errors,
        )
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    eprintln!("=== Full Flow Integration Test ===");
    std::fs::create_dir_all("bench_results/flow_test")?;

    let args: Vec<String> = std::env::args().skip(1).collect();
    let mut endpoint_arg: Option<String> = None;
    let mut concurrent_arg: Option<usize> = None;
    let mut vision_only = false;
    let mut i = 0usize;
    while i < args.len() {
        match args[i].as_str() {
            "--concurrent" => {
                if let Some(value) = args.get(i + 1).and_then(|s| s.parse().ok()) {
                    concurrent_arg = Some(value);
                }
                i += 2;
            }
            "--vision-only" => {
                vision_only = true;
                i += 1;
            }
            arg if arg.starts_with("--") => {
                i += 1;
            }
            _ => {
                if endpoint_arg.is_none() {
                    endpoint_arg = Some(args[i].clone());
                }
                i += 1;
            }
        }
    }

    let targets = if endpoint_arg.is_none() && concurrent_arg.is_none() && !vision_only {
        default_targets()
    } else {
        let endpoint = endpoint_arg.unwrap_or_else(|| "http://127.0.0.1:8000/v1".to_string());
        let concurrent = concurrent_arg.unwrap_or_else(|| infer_text_concurrency(&endpoint));
        vec![EndpointTarget {
            endpoint,
            concurrent,
        }]
    };

    let mut combined_reports = Vec::new();
    let mut any_failed = false;

    for target in targets {
        eprintln!("\n{}", "=".repeat(70));
        eprintln!(
            "Endpoint: {} | Text concurrency: {} | Vision concurrency: {}",
            target.endpoint,
            target.concurrent,
            infer_vision_concurrency(&target.endpoint),
        );

        let total_start = Instant::now();
        let healer = SelfHealingEngine::new(SelfHealingConfig {
            max_healing_attempts: 3,
            ..SelfHealingConfig::default()
        });
        let mut results = vec![];

        // Step 1: Auto-config
        results.push(test_auto_config(&target.endpoint).await);

        // Get model name for subsequent tests
        let configurator = AutoConfigurator::new(&target.endpoint, None);
        let model = configurator
            .fetch_models()
            .await
            .ok()
            .and_then(|m| m.first().map(|m| m.id.clone()))
            .unwrap_or_else(|| "unknown".to_string());

        let _ = healer.checkpoint(
            "endpoint-calibration",
            json!({
                "endpoint": target.endpoint,
                "model": model,
                "text_concurrency": target.concurrent,
                "vision_concurrency": infer_vision_concurrency(&target.endpoint),
            }),
        );

        if vision_only {
            let vision = test_vision_workflows(&target.endpoint, &model, "low", 192).await;
            let vision = if vision.passed {
                vision
            } else {
                match retry_after_self_heal(
                    &healer,
                    &target.endpoint,
                    "vision-workflows",
                    &vision.errors,
                    || async {
                        test_vision_workflows(&target.endpoint, &model, "high", 256)
                            .await
                            .with_note("self-heal retry switched to detail=high max_tokens=256")
                    },
                )
                .await
                {
                    Some(recovered) => recovered,
                    None => vision.with_note("self-heal exhausted"),
                }
            };
            results.push(vision);
        } else {
            // Step 2: Throughput with self-heal fallback
            let throughput = test_throughput(&target.endpoint, &model, target.concurrent).await;
            let throughput = if throughput.passed {
                throughput
            } else {
                let fallback_concurrent = (target.concurrent / 2).max(4);
                match retry_after_self_heal(
                    &healer,
                    &target.endpoint,
                    "throughput",
                    &throughput.errors,
                    || async {
                        test_throughput(&target.endpoint, &model, fallback_concurrent)
                            .await
                            .with_note(format!(
                                "self-heal retry reduced concurrency to {fallback_concurrent}"
                            ))
                    },
                )
                .await
                {
                    Some(recovered) => recovered,
                    None => throughput.with_note("self-heal exhausted"),
                }
            };
            results.push(throughput);

            // Step 3: Vision workflows with self-heal fallback
            let vision = test_vision_workflows(&target.endpoint, &model, "low", 192).await;
            let vision = if vision.passed {
                vision
            } else {
                match retry_after_self_heal(
                    &healer,
                    &target.endpoint,
                    "vision-workflows",
                    &vision.errors,
                    || async {
                        test_vision_workflows(&target.endpoint, &model, "high", 256)
                            .await
                            .with_note("self-heal retry switched to detail=high max_tokens=256")
                    },
                )
                .await
                {
                    Some(recovered) => recovered,
                    None => vision.with_note("self-heal exhausted"),
                }
            };
            results.push(vision);

            // Step 4: Error resilience with self-heal retry
            let error_resilience =
                test_error_resilience(&target.endpoint, &model, target.concurrent).await;
            let error_resilience = if error_resilience.passed {
                error_resilience
            } else {
                match retry_after_self_heal(
                    &healer,
                    &target.endpoint,
                    "error-resilience",
                    &error_resilience.errors,
                    || async {
                        test_error_resilience(&target.endpoint, &model, target.concurrent)
                            .await
                            .with_note("self-heal retry reran resilience checks")
                    },
                )
                .await
                {
                    Some(recovered) => recovered,
                    None => error_resilience.with_note("self-heal exhausted"),
                }
            };
            results.push(error_resilience);

            // Step 5: Tool calling with self-heal retry
            let tool_calling = test_tool_calling(&target.endpoint, &model).await;
            let tool_calling = if tool_calling.passed {
                tool_calling
            } else {
                match retry_after_self_heal(
                    &healer,
                    &target.endpoint,
                    "tool-calling",
                    &tool_calling.errors,
                    || async {
                        test_tool_calling(&target.endpoint, &model)
                            .await
                            .with_note("self-heal retry reran tool-calling checks")
                    },
                )
                .await
                {
                    Some(recovered) => recovered,
                    None => tool_calling.with_note("self-heal exhausted"),
                }
            };
            results.push(tool_calling);
        }

        // Print summary
        let total_duration = total_start.elapsed().as_secs_f64();
        let passed = results.iter().filter(|r| r.passed).count();
        let failed = results.iter().filter(|r| !r.passed).count();
        let heal_summary = healer.summary();

        eprintln!("\n{}", "=".repeat(70));
        eprintln!("FULL FLOW TEST RESULTS — {}", target.endpoint);
        eprintln!("{}", "=".repeat(70));
        eprintln!();

        for r in &results {
            let icon = if r.passed { "PASS" } else { "FAIL" };
            eprintln!(
                "  {} [{icon}] {} ({:.1}s)",
                if r.passed { "+" } else { "-" },
                r.step,
                r.duration_secs,
            );
            eprintln!("       {}", r.details);
            for err in &r.errors {
                eprintln!("       ! {err}");
            }
        }

        eprintln!();
        eprintln!(
            "  {passed}/{} passed, {failed} failed, {:.1}s total",
            results.len(),
            total_duration
        );
        eprintln!(
            "  self-heal: executions={}, successes={}, failures={}",
            heal_summary.executor.executions,
            heal_summary.executor.successes,
            heal_summary.executor.failures,
        );
        eprintln!("{}", "=".repeat(70));

        let report = json!({
            "endpoint": target.endpoint,
            "model": model,
            "text_concurrency": target.concurrent,
            "vision_concurrency": infer_vision_concurrency(&target.endpoint),
            "vision_only": vision_only,
            "total_duration_secs": total_duration,
            "passed": passed,
            "failed": failed,
            "results": results.iter().map(|r| json!({
                "step": r.step,
                "passed": r.passed,
                "duration_secs": r.duration_secs,
                "details": r.details,
                "errors": r.errors,
            })).collect::<Vec<_>>(),
            "self_healing": {
                "executions": heal_summary.executor.executions,
                "successes": heal_summary.executor.successes,
                "failures": heal_summary.executor.failures,
                "success_rate": heal_summary.executor.success_rate,
            }
        });

        let report_path = format!(
            "bench_results/flow_test/report_{}.json",
            sanitize_endpoint(&target.endpoint)
        );
        std::fs::write(&report_path, serde_json::to_string_pretty(&report)?)?;
        eprintln!("\nReport saved to {report_path}");

        any_failed |= failed > 0;
        combined_reports.push(report);
    }

    std::fs::write(
        "bench_results/flow_test/report.json",
        serde_json::to_string_pretty(&combined_reports)?,
    )?;
    eprintln!("\nCombined report saved to bench_results/flow_test/report.json");

    if any_failed {
        std::process::exit(1);
    }

    Ok(())
}