lean-ctx 3.10.5

Local context engine, CLI, MCP server, and proxy for AI agents.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
use crate::core::agent_connector;
use crate::core::agent_connector::traits::AgentConnector;
use crate::core::benchmark_spec::evidence::{
    ArtifactRedaction, EvidenceArm, write_comparison_bundle,
};
use crate::core::benchmark_spec::profile_bridge;
use crate::core::benchmark_spec::types::{BenchmarkResult, BenchmarkSpecV1};
use crate::core::calibrator::candidate::CandidateProfile;
use crate::core::calibrator::pareto::CalibratedResult;
use crate::core::calibrator::selection::{
    ManualSelectionRecordV1, SelectionEvidenceArmV1, SelectionPolicyV1, SelectionRationaleV1,
    apply_record, read_record, rollback_record, write_record,
};
use crate::core::calibrator::{self, CalibrationConfig};
use crate::core::canonical::canonical_serialize;
use crate::core::local_runner::runner::{LocalRunner, RunConfig};
use crate::core::profiles;
use std::path::PathBuf;

struct LiveCandidate {
    candidate: CandidateProfile,
    profile_name: String,
}

struct LiveBenchmarkRun {
    candidate: CandidateProfile,
    profile_name: String,
    spec: BenchmarkSpecV1,
    benchmark: BenchmarkResult,
}

pub(crate) fn cmd_calibrate(args: &[String]) -> i32 {
    if args.iter().any(|a| a == "--help" || a == "-h") {
        print_help();
        return 0;
    }

    let profile_name = args
        .first()
        .filter(|a| !a.starts_with('-'))
        .map(String::as_str)
        .unwrap_or("coder");

    let quality_floor = parse_flag_f64(args, "--quality-floor").unwrap_or(0.95);
    let max_candidates = parse_flag_usize(args, "--max-candidates").unwrap_or(12);
    let live = args.iter().any(|arg| arg == "--live");
    let export_spec = args.iter().any(|arg| arg == "--export-spec");
    let evidence_out = parse_flag_str(args, "--evidence-out");
    let evidence_bundle = parse_flag_str(args, "--evidence-bundle");
    let selection_record_out = parse_flag_str(args, "--selection-record");
    let apply_selection = args.iter().any(|arg| arg == "--apply");
    let rollback_selection = args.iter().any(|arg| arg == "--rollback");
    if apply_selection && rollback_selection {
        eprintln!("error: --apply and --rollback cannot be combined");
        return 2;
    }
    if rollback_selection {
        let Some(path) = selection_record_out else {
            eprintln!("error: --rollback requires --selection-record PATH");
            return 2;
        };
        return match read_record(std::path::Path::new(&path))
            .and_then(|record| rollback_record(&record))
        {
            Ok(()) => {
                eprintln!("restored the profile recorded in {path}");
                0
            }
            Err(error) => {
                eprintln!("error: rollback selection: {error}");
                2
            }
        };
    }
    if apply_selection && !live {
        let Some(path) = selection_record_out.as_deref() else {
            eprintln!("error: --apply requires --selection-record PATH");
            return 2;
        };
        let Some(evidence_path) = evidence_bundle.as_deref() else {
            eprintln!(
                "error: --apply requires --evidence-bundle PATH to verify selection evidence"
            );
            return 2;
        };
        return match read_record(std::path::Path::new(path))
            .and_then(|record| apply_record(&record, std::path::Path::new(evidence_path)))
        {
            Ok(()) => {
                eprintln!("applied the profile recorded in {path}");
                0
            }
            Err(error) => {
                eprintln!("error: apply selection: {error}");
                2
            }
        };
    }
    if selection_record_out.is_some() && (!live || evidence_out.is_none()) {
        eprintln!(
            "error: --selection-record requires --live and --evidence-out with evaluated receipt-linked runs"
        );
        return 2;
    }
    if apply_selection && selection_record_out.is_none() {
        eprintln!("error: --apply requires --selection-record PATH");
        return 2;
    }
    if evidence_bundle.is_some() && (live || !apply_selection) {
        eprintln!(
            "error: --evidence-bundle is only valid with a later --apply; live apply verifies --evidence-out directly"
        );
        return 2;
    }
    let evidence_redaction = match parse_evidence_redaction(args, evidence_out.is_some()) {
        Ok(redaction) => redaction,
        Err(error) => {
            eprintln!("error: {error}");
            return 2;
        }
    };
    let requested_agent = parse_flag_str(args, "--agent");
    let mut selection_material: Option<(Vec<LiveBenchmarkRun>, String, PathBuf, PathBuf)> = None;

    eprintln!("lean-ctx calibrate");
    eprintln!("  Profile:        {profile_name}");
    eprintln!("  Quality floor:  {:.0}%", quality_floor * 100.0);
    eprintln!("  Max candidates: {max_candidates}");
    eprintln!(
        "  Mode:           {}",
        if live { "live" } else { "simulated" }
    );
    if let Some(agent) = &requested_agent {
        eprintln!("  Agent:          {agent}");
    }
    eprintln!();

    let Some(profile) = profiles::load_profile(profile_name) else {
        eprintln!("error: profile '{profile_name}' not found");
        eprintln!("  available: lean-ctx profile list");
        return 1;
    };
    let live_spec = if live {
        match live_calibration_spec(args, profile_name) {
            Ok(spec) => Some(spec),
            Err(error) => {
                eprintln!("error: {error}");
                return 2;
            }
        }
    } else {
        None
    };
    let suite = live_spec
        .as_ref()
        .map(|spec| spec.suite.clone())
        .unwrap_or_else(profile_bridge::default_coding_suite);
    if live && suite.tasks.iter().any(|task| task.evaluation.is_none()) {
        eprintln!(
            "  Warning: suite has no deterministic evaluator; results remain observed, not eligible."
        );
        eprintln!("  Supply --spec PATH with an explicitly evaluated suite for selection.");
        eprintln!();
    }

    let config = CalibrationConfig {
        quality_floor,
        max_candidates,
        budget_range: (
            profile.budget.max_context_tokens_effective() / 4,
            profile.budget.max_context_tokens_effective(),
        ),
        compression_levels: vec!["lossless".into(), "balanced".into(), "aggressive".into()],
        reuse_range: (0.70, 0.95),
        capability_variants: vec!["leanctx".into()],
    };

    let candidates = crate::core::calibrator::candidate::generate_candidates(&config);
    let live_candidates = if live {
        match build_live_candidates(args, max_candidates) {
            Ok(candidates) => candidates,
            Err(error) => {
                eprintln!("error: {error}");
                return 2;
            }
        }
    } else {
        Vec::new()
    };
    if live {
        eprintln!(
            "  Comparing profiles: {}",
            live_candidates
                .iter()
                .map(|candidate| candidate.profile_name.as_str())
                .collect::<Vec<_>>()
                .join(", ")
        );
    } else {
        eprintln!("  Generated {} simulated candidates", candidates.len());
    }
    eprintln!();

    let results = if live {
        let working_dir = match parse_working_dir(args) {
            Ok(path) => path,
            Err(error) => {
                eprintln!("error: {error}");
                return 2;
            }
        };
        let connectors = agent_connector::detect_and_create_connectors();
        let Some(connector) = connectors.into_iter().find(|connector| {
            connector.info().available
                && requested_agent
                    .as_deref()
                    .is_none_or(|agent| connector_matches(agent, connector.name()))
        }) else {
            if let Some(agent) = requested_agent {
                eprintln!("error: agent '{agent}' was not detected");
            } else {
                eprintln!("error: no supported agent detected for live calibration");
            }
            return 2;
        };

        eprintln!("  Using agent:    {}", connector.name());
        eprintln!();
        match run_live_benchmark_results(
            &live_candidates,
            &config,
            working_dir,
            connector,
            live_spec
                .as_ref()
                .expect("live spec exists when --live is set"),
        ) {
            Ok(runs) => {
                let evidence_bundle = match (evidence_out.as_deref(), evidence_redaction) {
                    (Some(path), Some(redaction)) => {
                        let arms = evidence_arms(&runs);
                        match write_comparison_bundle(std::path::Path::new(path), &arms, redaction)
                        {
                            Ok(bundle) => {
                                eprintln!(
                                    "  Evidence bundle: {} (sha256 {})",
                                    bundle.path.display(),
                                    bundle.sha256
                                );
                                Some((bundle.sha256, bundle.path))
                            }
                            Err(error) => {
                                eprintln!("error: evidence bundle failed: {error:#}");
                                return 2;
                            }
                        }
                    }
                    (Some(_), None) => {
                        eprintln!("error: --evidence-out requires --artifact-redaction");
                        return 2;
                    }
                    (None, _) => None,
                };
                if let Some(path) = selection_record_out.as_deref() {
                    let Some((bundle_sha256, bundle_path)) = evidence_bundle else {
                        eprintln!("error: --selection-record requires an evidence bundle");
                        return 2;
                    };
                    let calibrated = runs
                        .iter()
                        .map(|run| {
                            CalibratedResult::from_benchmark_result(
                                run.candidate.clone(),
                                &run.benchmark,
                            )
                        })
                        .collect();
                    selection_material =
                        Some((runs, bundle_sha256, PathBuf::from(path), bundle_path));
                    calibrated
                } else {
                    runs.iter()
                        .map(|run| {
                            CalibratedResult::from_benchmark_result(
                                run.candidate.clone(),
                                &run.benchmark,
                            )
                        })
                        .collect()
                }
            }
            Err(error) => {
                eprintln!("error: live calibration failed: {error}");
                return 2;
            }
        }
    } else {
        if evidence_out.is_some() {
            eprintln!("error: --evidence-out requires --live with evaluated receipt-linked runs");
            return 2;
        }
        simulate_benchmark_results(&candidates, &profile)
    };

    let report = calibrator::calibrate(results, &config);

    if export_spec {
        eprintln!("{}", report.report_text);
    } else {
        println!("{}", report.report_text);
    }

    if let Some(rec) = &report.recommendation {
        eprintln!();
        eprintln!("  Recommended profile: {}", rec.label);
        if let Some(bl) = &rec.vs_baseline {
            eprintln!("  Cost delta:    {:+.1}%", bl.cost_delta_pct);
            eprintln!("  Quality delta: {:+.4}", bl.quality_delta);
            eprintln!("  Latency delta: {:+.1}%", bl.latency_delta_pct);
        }
    }

    if let Some((runs, evidence_bundle_sha256, path, evidence_path)) = selection_material {
        let Some(recommendation) = &report.recommendation else {
            eprintln!(
                "error: selection record not written because no evidence-qualified recommendation was produced"
            );
            return 2;
        };
        let Some(selected) = runs
            .iter()
            .find(|run| run.candidate.id == recommendation.candidate_id)
        else {
            eprintln!("error: recommendation does not match a live candidate");
            return 2;
        };
        let previous_profile = crate::core::config::setter::current_value("profile")
            .unwrap_or_else(|| "coder".to_string());
        let record = match ManualSelectionRecordV1::create(
            previous_profile,
            selected.profile_name.clone(),
            recommendation.candidate_id.clone(),
            evidence_bundle_sha256,
            selection_policy(quality_floor),
            selection_rationale(recommendation),
            selection_evidence_arms(&runs),
        ) {
            Ok(record) => record,
            Err(error) => {
                eprintln!("error: create selection record: {error}");
                return 2;
            }
        };
        if let Err(error) = write_record(&path, &record) {
            eprintln!("error: write selection record: {error}");
            return 2;
        }
        eprintln!(
            "  Selection record: {} ({})",
            path.display(),
            record.selection_id
        );
        if apply_selection {
            if let Err(error) = apply_record(&record, &evidence_path) {
                eprintln!("error: apply selection: {error}");
                return 2;
            }
            eprintln!("  Applied profile:   {}", record.selected_profile);
        }
    }

    match profile_bridge::create_spec(profile_name, suite) {
        Ok(spec) => {
            let json = serde_json::to_string_pretty(&spec).unwrap_or_default();
            eprintln!();
            eprintln!("  BenchmarkSpec written to stdout (pipe to file):");
            eprintln!("  lean-ctx calibrate {profile_name} --export-spec > spec.json");
            if export_spec
                && spec
                    .suite
                    .tasks
                    .iter()
                    .any(|task| task.evaluation.is_none())
            {
                eprintln!(
                    "  Note: add a deterministic evaluation to every task before using this spec with --live."
                );
            }
            if export_spec {
                println!("{json}");
            }
        }
        Err(e) => {
            eprintln!("  warning: could not create BenchmarkSpec: {e}");
        }
    }

    0
}

/// Offline simulation — used when --live is not passed.
/// Uses profile settings to generate plausible cost/quality/latency curves.
fn simulate_benchmark_results(
    candidates: &[CandidateProfile],
    profile: &crate::core::profiles::types::Profile,
) -> Vec<CalibratedResult> {
    let base_cost = profile.budget.max_cost_usd_effective();
    let base_budget = profile.budget.max_context_tokens_effective() as f64;

    candidates
        .iter()
        .map(|c| {
            let budget_ratio = c.budget_tokens as f64 / base_budget;
            let compression_factor = match c.compression.as_str() {
                "lossless" => 1.0,
                "balanced" => 0.65,
                "aggressive" => 0.40,
                _ => 0.75,
            };
            let cost = base_cost * budget_ratio * compression_factor;
            let quality = match c.compression.as_str() {
                "lossless" => 0.98 - (1.0 - c.reuse_threshold) * 0.05,
                "balanced" => 0.96 - (1.0 - c.reuse_threshold) * 0.08,
                "aggressive" => 0.91 - (1.0 - c.reuse_threshold) * 0.15,
                _ => 0.94,
            };
            let latency = 100.0 * budget_ratio;

            CalibratedResult {
                candidate: c.clone(),
                cost_per_task: cost,
                mean_quality: quality.clamp(0.0, 1.0),
                mean_latency_ms: latency,
                pass_rate: if quality >= 0.95 { 1.0 } else { 0.8 },
                quality_floor_met: quality >= 0.95,
                quality_evaluated: false,
                receipt_evidence_complete: false,
            }
        })
        .collect()
}

fn run_live_benchmark_results(
    candidates: &[LiveCandidate],
    calibration_config: &CalibrationConfig,
    working_dir: PathBuf,
    connector: Box<dyn AgentConnector>,
    source_spec: &BenchmarkSpecV1,
) -> anyhow::Result<Vec<LiveBenchmarkRun>> {
    let agent_name = connector.name().to_owned();
    let runner = LocalRunner::new(
        RunConfig {
            agent_name,
            profile_name: candidates
                .first()
                .map(|candidate| candidate.profile_name.clone())
                .unwrap_or_default(),
            suite_name: Some("coding-v1".into()),
            timeout_override_ms: None,
            working_dir,
            repeats: 1,
        },
        connector,
    );

    let mut runs = Vec::with_capacity(candidates.len());
    for (index, candidate) in candidates.iter().enumerate() {
        eprintln!(
            "Running candidate {}/{}: {}...",
            index + 1,
            candidates.len(),
            candidate.candidate.label
        );
        let profile = profiles::load_profile(&candidate.profile_name)
            .ok_or_else(|| anyhow::anyhow!("profile '{}' not found", candidate.profile_name))?;
        let mut spec = source_spec.clone();
        spec.id = format!("{}-{}", source_spec.id, candidate.candidate.id);
        spec.name = format!("{} ({})", source_spec.name, candidate.candidate.label);
        spec.configuration.profile_hash = Some(profile_bridge::profile_hash(&profile));
        spec.configuration.quality_floor = calibration_config.quality_floor;

        let benchmark = runner.run_with_profile(&spec, &candidate.profile_name)?;
        runs.push(LiveBenchmarkRun {
            candidate: candidate.candidate.clone(),
            profile_name: candidate.profile_name.clone(),
            spec,
            benchmark,
        });
    }
    Ok(runs)
}

fn evidence_arms(runs: &[LiveBenchmarkRun]) -> Vec<EvidenceArm<'_>> {
    runs.iter()
        .enumerate()
        .map(|(index, run)| EvidenceArm {
            label: match index {
                0 => "baseline".into(),
                1 => "treatment".into(),
                _ => format!("candidate-{index}"),
            },
            spec: &run.spec,
            result: &run.benchmark,
        })
        .collect()
}

fn selection_evidence_arms(runs: &[LiveBenchmarkRun]) -> Vec<SelectionEvidenceArmV1> {
    runs.iter()
        .map(|run| SelectionEvidenceArmV1 {
            candidate_id: run.candidate.id.clone(),
            profile_name: run.profile_name.clone(),
            profile_hash: run.benchmark.profile_hash.clone(),
            spec_id: run.spec.id.clone(),
            spec_version: run.spec.version.clone(),
            spec_digest: format!(
                "blake3:{}",
                blake3::hash(&canonical_serialize(&run.spec)).to_hex()
            ),
            result_digest: format!(
                "blake3:{}",
                blake3::hash(&canonical_serialize(&run.benchmark)).to_hex()
            ),
            receipt_refs: run
                .benchmark
                .outcomes
                .iter()
                .filter_map(|outcome| outcome.execution_receipt_ref.clone())
                .collect::<std::collections::BTreeSet<_>>()
                .into_iter()
                .collect(),
        })
        .collect()
}

fn selection_policy(quality_floor: f64) -> SelectionPolicyV1 {
    SelectionPolicyV1 {
        quality_floor,
        requires_evaluated_quality: true,
        requires_complete_receipts: true,
    }
}

fn selection_rationale(
    recommendation: &crate::core::calibrator::recommendation::Recommendation,
) -> SelectionRationaleV1 {
    let kind = match &recommendation.reason {
        crate::core::calibrator::recommendation::RecommendationReason::LowestCostAboveFloor => {
            "lowest-cost-above-floor"
        }
        crate::core::calibrator::recommendation::RecommendationReason::OnlyCandidate => {
            "only-candidate"
        }
    };
    SelectionRationaleV1 {
        kind: kind.to_string(),
        selected_cost_per_task: recommendation.cost_per_task,
        selected_mean_quality: recommendation.mean_quality,
        selected_mean_latency_ms: recommendation.mean_latency_ms,
    }
}

fn live_calibration_spec(args: &[String], profile_name: &str) -> Result<BenchmarkSpecV1, String> {
    let Some(path) = parse_flag_str(args, "--spec") else {
        return profile_bridge::create_spec(profile_name, profile_bridge::default_coding_suite())
            .map_err(|error| error.to_string());
    };
    let source = std::fs::read_to_string(&path)
        .map_err(|error| format!("cannot read benchmark spec '{path}': {error}"))?;
    let spec: BenchmarkSpecV1 = serde_json::from_str(&source)
        .map_err(|error| format!("invalid benchmark spec '{path}': {error}"))?;
    spec.validate_evidence()?;
    Ok(spec)
}

fn build_live_candidates(
    args: &[String],
    max_candidates: usize,
) -> Result<Vec<LiveCandidate>, String> {
    let Some(raw_profiles) = parse_flag_str(args, "--profiles") else {
        return Err(
            "live calibration requires --profiles PROFILE_A,PROFILE_B (at least two existing profiles)"
                .into(),
        );
    };
    let profile_names = parse_profile_names(&raw_profiles)?;
    if profile_names.len() < 2 {
        return Err("live calibration requires at least two distinct profiles".into());
    }
    if profile_names.len() > max_candidates {
        return Err(format!(
            "{} profiles exceed --max-candidates {max_candidates}",
            profile_names.len()
        ));
    }

    profile_names
        .into_iter()
        .map(|profile_name| {
            let profile = profiles::load_profile(&profile_name)
                .ok_or_else(|| format!("profile '{profile_name}' not found"))?;
            let capability_variant = profile
                .capabilities
                .compression
                .as_ref()
                .and_then(|binding| binding.provider.clone())
                .unwrap_or_else(|| "leanctx".into());
            let candidate = CandidateProfile {
                id: format!("profile-{profile_name}"),
                label: profile_name.clone(),
                budget_tokens: profile
                    .budget
                    .max_context_tokens_effective()
                    .min(profile.constraints.max_context_tokens_effective()),
                compression: profile.compression.crp_mode_effective().to_owned(),
                // Reuse threshold is not a named Profile field; live measurements
                // come from the selected profile rather than this display record.
                reuse_threshold: 0.0,
                capability_variant,
            };
            Ok(LiveCandidate {
                candidate,
                profile_name,
            })
        })
        .collect()
}

fn parse_profile_names(raw_profiles: &str) -> Result<Vec<String>, String> {
    let mut profile_names = Vec::new();
    for profile_name in raw_profiles.split(',').map(str::trim) {
        if profile_name.is_empty() {
            return Err("--profiles cannot contain an empty profile name".into());
        }
        if profile_names.iter().any(|known| known == profile_name) {
            return Err(format!(
                "--profiles contains duplicate profile '{profile_name}'"
            ));
        }
        profile_names.push(profile_name.to_owned());
    }
    Ok(profile_names)
}

fn parse_evidence_redaction(
    args: &[String],
    evidence_requested: bool,
) -> Result<Option<ArtifactRedaction>, String> {
    let redaction = parse_flag_str(args, "--artifact-redaction")
        .map(|value| ArtifactRedaction::parse(&value).map_err(|error| error.to_string()))
        .transpose()?;
    match (evidence_requested, redaction) {
        (true, None) => Err(
            "--evidence-out requires --artifact-redaction self-contained|redacted|restricted"
                .into(),
        ),
        (false, Some(_)) => Err("--artifact-redaction requires --evidence-out".into()),
        (_, redaction) => Ok(redaction),
    }
}

fn parse_flag_f64(args: &[String], flag: &str) -> Option<f64> {
    args.iter()
        .position(|a| a == flag)
        .and_then(|i| args.get(i + 1))
        .and_then(|v| v.parse().ok())
}

fn parse_flag_usize(args: &[String], flag: &str) -> Option<usize> {
    args.iter()
        .position(|a| a == flag)
        .and_then(|i| args.get(i + 1))
        .and_then(|v| v.parse().ok())
}

fn parse_flag_str(args: &[String], flag: &str) -> Option<String> {
    args.iter()
        .position(|arg| arg == flag)
        .and_then(|index| args.get(index + 1))
        .cloned()
}

fn parse_working_dir(args: &[String]) -> Result<PathBuf, String> {
    let path = match parse_flag_str(args, "--working-dir") {
        Some(path) => PathBuf::from(path),
        None => std::env::current_dir()
            .map_err(|error| format!("cannot determine working directory: {error}"))?,
    };
    if path.is_dir() {
        Ok(path)
    } else {
        Err(format!(
            "working directory '{}' does not exist",
            path.display()
        ))
    }
}

fn connector_matches(requested: &str, detected: &str) -> bool {
    let requested = requested.to_lowercase();
    let detected = detected.to_lowercase();
    detected.contains(&requested) || requested.contains(&detected)
}

fn print_help() {
    eprintln!(
        "\
lean-ctx calibrate — find the optimal Performance Profile

USAGE:
    lean-ctx calibrate [PROFILE] [OPTIONS]

ARGS:
    PROFILE             Profile name (default: current active profile)

OPTIONS:
    --quality-floor N   Minimum quality score 0.0-1.0 (default: 0.95)
    --max-candidates N  Maximum candidate profiles to test (default: 12)
    --live              Compare named profiles against a detected local agent
    --profiles A,B      Existing profiles to compare with --live (at least two)
    --agent NAME        Agent to use with --live (default: first detected)
    --working-dir PATH  Agent working directory with --live (default: current directory)
    --spec PATH         Evaluated BenchmarkSpec JSON for --live selection
    --evidence-out PATH Write signed offline evidence bundle for live comparison
    --artifact-redaction CLASS
                        Required with --evidence-out: self-contained, redacted, or restricted
    --selection-record PATH
                        Write an immutable selection record from a live evidence bundle
    --apply             Apply the selected profile after writing --selection-record
    --evidence-bundle PATH
                        Required for a later --apply; verifies the immutable evidence bundle
    --rollback          Restore the prior profile from --selection-record
    --export-spec       Print BenchmarkSpec JSON to stdout
    -h, --help          Show this help

EXAMPLES:
    lean-ctx calibrate
    lean-ctx calibrate coder --quality-floor 0.90
    lean-ctx calibrate --live --profiles coder,exploration --agent codex --spec bench.json
    lean-ctx calibrate --live --profiles coder,exploration --spec bench.json --evidence-out proof.zip --artifact-redaction redacted
    lean-ctx calibrate --live --profiles coder,exploration --spec bench.json --evidence-out proof.zip --artifact-redaction restricted --selection-record selection.json --apply
    lean-ctx calibrate --selection-record selection.json --apply --evidence-bundle proof.zip
    lean-ctx calibrate --selection-record selection.json --rollback
    lean-ctx calibrate monorepo --max-candidates 20 --export-spec > spec.json

NOTE:
    Live calibration applies each selected profile through LEAN_CTX_PROFILE.
    --spec requires a deterministic evaluator per task; otherwise runs are observed only.
    --selection-record requires --live, --evidence-out, and complete receipt evidence.
    A later --apply requires --evidence-bundle and fails closed on unavailable, tampered,
    unsigned, or semantically mismatched evidence.
    --apply and --rollback refuse when LEAN_CTX_PROFILE overrides config.
    Without --live, calibration uses simulated benchmark results."
    );
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::benchmark_spec::types::{
        BenchmarkSuite, BenchmarkTask, EvaluationSpecV1, TaskKind,
    };
    use crate::core::local_runner::runner::MockConnector;

    fn candidate() -> CandidateProfile {
        CandidateProfile {
            id: "candidate-001".into(),
            label: "leanctx/balanced/32k/r85".into(),
            budget_tokens: 32_000,
            compression: "balanced".into(),
            reuse_threshold: 0.85,
            capability_variant: "leanctx".into(),
        }
    }

    fn evaluated_suite() -> BenchmarkSuite {
        BenchmarkSuite {
            kind: crate::core::benchmark_spec::types::BenchmarkKind::TaskScore,
            tasks: vec![BenchmarkTask {
                id: "task".into(),
                name: "Task".into(),
                description: "Return task output".into(),
                kind: TaskKind::Custom,
                timeout_ms: None,
                evaluation: Some(EvaluationSpecV1::Qa {
                    answers: vec!["task output".into()],
                    minimum_f1: 1.0,
                }),
            }],
        }
    }

    #[test]
    fn live_calibration_runs_mock_connector_for_each_candidate() {
        let config = CalibrationConfig::default();
        let mut source_spec = profile_bridge::create_spec("coder", evaluated_suite()).unwrap();
        source_spec.id = "fixture-manifest".into();
        source_spec.name = "Fixture manifest".into();
        source_spec.configuration.model = Some("fixture-model".into());
        let results = run_live_benchmark_results(
            &[LiveCandidate {
                candidate: candidate(),
                profile_name: "coder".into(),
            }],
            &config,
            std::env::current_dir().expect("test working directory"),
            Box::new(MockConnector::new(true)),
            &source_spec,
        )
        .expect("mock connector must complete live calibration");

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].candidate.id, "candidate-001");
        assert_eq!(results[0].spec.id, "fixture-manifest-candidate-001");
        assert_eq!(
            results[0].spec.configuration.model.as_deref(),
            Some("fixture-model")
        );
        assert_eq!(results[0].benchmark.summary.pass_rate, 1.0);
        assert!(results[0].benchmark.summary.quality_floor_met);
    }

    #[test]
    fn live_selection_requires_an_evaluator_for_each_task() {
        let observed = profile_bridge::create_spec("coder", profile_bridge::default_coding_suite())
            .expect("built-in profile creates a benchmark spec");
        let evaluated = profile_bridge::create_spec("coder", evaluated_suite())
            .expect("built-in profile creates a benchmark spec");
        assert!(observed.validate_evidence().is_err());
        assert!(evaluated.validate_evidence().is_ok());
    }

    #[test]
    fn live_profiles_require_distinct_named_profiles() {
        assert!(parse_profile_names("coder,exploration").is_ok());
        assert!(parse_profile_names("coder,coder").is_err());
        assert!(parse_profile_names("coder,").is_err());
    }

    #[test]
    fn evidence_export_requires_an_explicit_artifact_classification() {
        let no_classification = vec!["--evidence-out".into(), "proof.zip".into()];
        assert!(parse_evidence_redaction(&no_classification, true).is_err());

        let class_without_bundle = vec!["--artifact-redaction".into(), "redacted".into()];
        assert!(parse_evidence_redaction(&class_without_bundle, false).is_err());

        let classified = vec![
            "--evidence-out".into(),
            "proof.zip".into(),
            "--artifact-redaction".into(),
            "redacted".into(),
        ];
        assert_eq!(
            parse_evidence_redaction(&classified, true).unwrap(),
            Some(ArtifactRedaction::Redacted)
        );
    }

    #[test]
    fn live_candidates_resolve_each_selected_profile() {
        let args = vec!["--profiles".into(), "coder,exploration".into()];
        let candidates = build_live_candidates(&args, 12).expect("built-in profiles resolve");

        assert_eq!(candidates.len(), 2);
        assert_eq!(candidates[0].profile_name, "coder");
        assert_eq!(candidates[1].profile_name, "exploration");
        assert_eq!(candidates[0].candidate.label, "coder");
    }
}