cargo-rail 0.13.4

Graph-aware testing, dependency unification, and crate extraction for Rust monorepos
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
//! `cargo rail plan` - deterministic file-first change planner.

use crate::change_detection::classify::{
  FileProfile, RC_FILE_KIND_CI, RC_FILE_KIND_CUSTOM, RC_FILE_KIND_DOCS, RC_FILE_KIND_INFRA_PATTERN,
  RC_FILE_KIND_REPO_CONFIG, RC_FILE_KIND_RUST_BENCH, RC_FILE_KIND_RUST_SRC, RC_FILE_KIND_RUST_TEST,
  RC_FILE_KIND_SCRIPT, RC_FILE_KIND_TOML_LOCKFILE, RC_FILE_KIND_TOML_MANIFEST, RC_FILE_KIND_TOML_TOOLING,
  RC_FILE_KIND_TOML_WORKSPACE, RC_FILE_KIND_UNCLASSIFIED, classify_path, compile_custom_patterns,
  compile_infrastructure_patterns, custom_surface_names, custom_surfaces_for_path, matches_infrastructure_patterns,
};
use crate::commands::common::{PlanOutputFormat, format_preview_list};
use crate::config::{ConfidenceProfile, UnknownFilePolicy};
use crate::error::{RailError, RailResult};
use crate::git::detect_default_base_ref;
use crate::utils::{config_fingerprint, toolchain_fingerprint};
use crate::workspace::WorkspaceContext;
use serde::Serialize;
use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::io::Write;
use std::path::{Path, PathBuf};

/// Options for the `plan` command.
#[derive(Debug, Default)]
pub struct PlanOptions {
  /// Git ref to compare against.
  pub since: Option<String>,
  /// Start of SHA range (used with `to` for SHA pair mode).
  pub from: Option<String>,
  /// End of SHA range (used with `from` for SHA pair mode).
  pub to: Option<String>,
  /// Use merge-base with default branch.
  pub merge_base: bool,
  /// Output format.
  pub format: PlanOutputFormat,
  /// Write output to file instead of stdout.
  pub output: Option<PathBuf>,
  /// Show concise human reasoning.
  pub explain: bool,
  /// Planner confidence profile override.
  pub confidence_profile: Option<String>,
}

#[derive(Debug, Clone)]
struct ResolvedRefs {
  since: Option<String>,
  from: Option<String>,
  to: Option<String>,
  merge_base: bool,
  resolved_base: String,
  resolved_head: String,
}

#[derive(Debug, Serialize)]
pub(crate) struct PlanOutput {
  pub(crate) plan_contract_version: u32,
  pub(crate) inputs: PlanInputs,
  pub(crate) files: Vec<PlannedFile>,
  pub(crate) impact: PlanImpact,
  pub(crate) scope: ExecutionScope,
  pub(crate) surfaces: BTreeMap<String, SurfaceDecision>,
  pub(crate) trace: Vec<TraceReason>,
  pub(crate) reproducibility: Reproducibility,
}

#[derive(Debug, Serialize)]
pub(crate) struct PlanInputs {
  pub(crate) refs: PlanRefs,
  pub(crate) workspace_root: String,
  pub(crate) config_fingerprint: String,
  pub(crate) toolchain_fingerprint: String,
  pub(crate) confidence_profile: String,
  pub(crate) confidence_profile_source: String,
}

#[derive(Debug, Serialize)]
pub(crate) struct PlanRefs {
  pub(crate) since: Option<String>,
  pub(crate) from: Option<String>,
  pub(crate) to: Option<String>,
  pub(crate) merge_base: bool,
  pub(crate) resolved_base: String,
  pub(crate) resolved_head: String,
}

#[derive(Debug, Serialize)]
pub(crate) struct PlannedFile {
  pub(crate) path: String,
  pub(crate) kind: String,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub(crate) sub_kind: Option<String>,
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub(crate) custom_surfaces: Vec<String>,
  pub(crate) owners: Vec<String>,
  pub(crate) owner_scope: String,
}

#[derive(Debug, Serialize)]
pub(crate) struct PlanImpact {
  pub(crate) direct_crates: Vec<String>,
  pub(crate) transitive_crates: Vec<String>,
  pub(crate) execution_transitive_crates: Vec<String>,
}

/// Execution-focused projection of the planner contract.
///
/// This is the stable handoff for runners and CI transport layers.
#[derive(Debug, Clone, Serialize)]
pub(crate) struct ExecutionScope {
  pub(crate) scope_contract_version: u32,
  pub(crate) resolved_base: String,
  pub(crate) resolved_head: String,
  pub(crate) mode: ExecutionScopeMode,
  pub(crate) crates: Vec<String>,
  pub(crate) surfaces: BTreeMap<String, bool>,
}

#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum ExecutionScopeMode {
  Empty,
  Crates,
  Workspace,
}

#[derive(Debug, Serialize)]
pub(crate) struct SurfaceDecision {
  pub(crate) enabled: bool,
  pub(crate) reasons: Vec<u32>,
}

#[derive(Debug, Serialize)]
pub(crate) struct TraceReason {
  pub(crate) id: u32,
  pub(crate) code: &'static str,
  pub(crate) description: &'static str,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub(crate) file: Option<String>,
  #[serde(rename = "crate", skip_serializing_if = "Option::is_none")]
  pub(crate) crate_name: Option<String>,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub(crate) depends_on: Option<String>,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub(crate) surface: Option<String>,
}

#[derive(Debug, Serialize)]
pub(crate) struct Reproducibility {
  pub(crate) cargo_rail_version: &'static str,
  pub(crate) config_hash: String,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub(crate) git_merge_base: Option<String>,
  pub(crate) git_shallow_clone: bool,
}

const RC_FILE_OWNS_CRATE_DIRECT: &str = "FILE_OWNS_CRATE_DIRECT";
const RC_TRANSITIVE_DEPENDS_ON_DIRECT: &str = "TRANSITIVE_DEPENDS_ON_DIRECT";
const RC_OWNER_UNCERTAIN_FALLBACK: &str = "OWNER_UNCERTAIN_FALLBACK";
const RC_CONFIDENCE_PROFILE_STRICT: &str = "CONFIDENCE_PROFILE_STRICT";
const RC_CONFIDENCE_PROFILE_BALANCED: &str = "CONFIDENCE_PROFILE_BALANCED";
const RC_CONFIDENCE_PROFILE_FAST: &str = "CONFIDENCE_PROFILE_FAST";
const RC_CONFIDENCE_STRICT_OWNER_EXPANSION: &str = "CONFIDENCE_STRICT_OWNER_EXPANSION";
const RC_CONFIDENCE_FAST_SKIP_TRANSITIVE: &str = "CONFIDENCE_FAST_SKIP_TRANSITIVE";
const RC_BOT_PR_CONFIDENCE_OVERRIDE: &str = "BOT_PR_CONFIDENCE_OVERRIDE";
const PLAN_CONTRACT_VERSION: u32 = 3;
const SCOPE_CONTRACT_VERSION: u32 = 1;
const PACKAGE_SCOPED_SURFACES: &[&str] = &["build", "test", "bench"];

#[derive(Debug, Clone, Copy)]
struct EffectiveConfidenceProfile {
  profile: ConfidenceProfile,
  source: &'static str,
  bot_override: bool,
}

fn json_err(error: serde_json::Error) -> RailError {
  RailError::message(format!("JSON serialization failed: {}", error))
}

fn to_json<T: serde::Serialize>(value: &T) -> RailResult<String> {
  serde_json::to_string(value).map_err(json_err)
}

fn to_json_pretty<T: serde::Serialize>(value: &T) -> RailResult<String> {
  serde_json::to_string_pretty(value).map_err(json_err)
}

/// Run the plan command.
pub fn run_plan(ctx: &WorkspaceContext, opts: PlanOptions) -> RailResult<()> {
  if opts.format.is_json_like() {
    crate::output::set_json_mode(true);
  }

  let output = build_plan_output(ctx, &opts)?;

  let rendered = match opts.format {
    PlanOutputFormat::Text => format_text(&output, opts.explain),
    PlanOutputFormat::Json => {
      let payload = serde_json::to_value(&output).map_err(json_err)?;
      let envelope = crate::output::machine_json_envelope("plan", "inspect", "success", 0, payload);
      to_json_pretty(&envelope)?
    }
    PlanOutputFormat::GitHub => format_github(&output, false)?,
    PlanOutputFormat::GitHubDebug => format_github(&output, true)?,
  };

  write_output(&rendered, opts.output.as_ref())
}

/// Build the planner output contract without rendering it.
pub(crate) fn build_plan_output(ctx: &WorkspaceContext, opts: &PlanOptions) -> RailResult<PlanOutput> {
  let refs = resolve_refs(ctx, opts)?;
  let changed_files = collect_changed_files(ctx, &refs)?;
  let confidence = resolve_confidence_profile(ctx, opts)?;

  let change_detection_config = ctx.config.as_ref().map(|config| &config.change_detection);
  let infrastructure_patterns = compile_infrastructure_patterns(change_detection_config);
  let custom_patterns = compile_custom_patterns(change_detection_config);
  let configured_custom_surfaces = custom_surface_names(&custom_patterns);

  let changed_file_count = changed_files.len();
  let mut trace = Vec::with_capacity(changed_file_count * 4); // Multiple trace entries per file
  let mut surface_refs: BTreeMap<String, BTreeSet<u32>> = BTreeMap::new();

  let mut planned_files = Vec::with_capacity(changed_file_count);
  let mut direct_crates: BTreeSet<String> = BTreeSet::new();
  let mut build_test_seed_crates: BTreeSet<String> = BTreeSet::new();
  let unknown_policy = unknown_file_policy(ctx);

  push_trace(
    &mut trace,
    &mut surface_refs,
    profile_reason_code(confidence.profile),
    None,
    None,
    None,
    &[] as &[String],
  );

  if confidence.bot_override {
    push_trace(
      &mut trace,
      &mut surface_refs,
      RC_BOT_PR_CONFIDENCE_OVERRIDE,
      None,
      None,
      None,
      &[] as &[String],
    );
  }

  for path in &changed_files {
    let file_kind = classify_path(Path::new(path));
    let custom_surfaces = custom_surfaces_for_path(path, &custom_patterns);
    let mut owners: Vec<String> = ctx.graph.files_to_crates(&[Path::new(path)]).into_iter().collect();
    owners.sort();

    for owner in &owners {
      direct_crates.insert(owner.clone());
      push_trace(
        &mut trace,
        &mut surface_refs,
        RC_FILE_OWNS_CRATE_DIRECT,
        Some(path),
        Some(owner),
        None,
        &[] as &[String],
      );
    }

    let owner_scope = owner_scope(path, &owners);

    let mut kind_surfaces: Vec<String> = file_kind
      .default_surfaces()
      .iter()
      .map(|surface| (*surface).to_string())
      .collect();
    let has_builtin_infra_surface = kind_surfaces.iter().any(|surface| surface == "infra");
    let extra_infra_surface =
      !has_builtin_infra_surface && matches_infrastructure_patterns(path, &infrastructure_patterns);

    let unknown_owner_build_test = file_kind == FileProfile::Unknown
      && !owners.is_empty()
      && confidence.profile != ConfidenceProfile::Fast
      && matches!(
        unknown_policy,
        UnknownFilePolicy::OwnedBuildTest | UnknownFilePolicy::Strict
      );

    if file_kind == FileProfile::Unknown {
      for surface in unknown_surfaces_for_path(unknown_policy, &owners) {
        ensure_surface(&mut kind_surfaces, surface);
      }
    }

    let apply_strict_owner_expansion = confidence.profile == ConfidenceProfile::Strict && !owners.is_empty();

    if unknown_owner_build_test {
      let fallback_surfaces = vec!["build".to_string(), "test".to_string()];
      for owner in &owners {
        push_trace(
          &mut trace,
          &mut surface_refs,
          RC_OWNER_UNCERTAIN_FALLBACK,
          Some(path),
          Some(owner),
          None,
          &fallback_surfaces,
        );
      }
    }

    if apply_strict_owner_expansion {
      ensure_surface(&mut kind_surfaces, "build");
      ensure_surface(&mut kind_surfaces, "test");
      let strict_surfaces = vec!["build".to_string(), "test".to_string()];
      for owner in &owners {
        push_trace(
          &mut trace,
          &mut surface_refs,
          RC_CONFIDENCE_STRICT_OWNER_EXPANSION,
          Some(path),
          Some(owner),
          None,
          &strict_surfaces,
        );
      }
    }

    let baseline_transitive_seed = file_kind.seeds_build_test_transitive() || unknown_owner_build_test;
    let should_seed_build_test_transitive = match confidence.profile {
      ConfidenceProfile::Strict => !owners.is_empty() || baseline_transitive_seed,
      ConfidenceProfile::Balanced => baseline_transitive_seed,
      ConfidenceProfile::Fast => false,
    };

    if confidence.profile == ConfidenceProfile::Fast && baseline_transitive_seed && !owners.is_empty() {
      push_trace(
        &mut trace,
        &mut surface_refs,
        RC_CONFIDENCE_FAST_SKIP_TRANSITIVE,
        Some(path),
        None,
        None,
        &[] as &[String],
      );
    }

    if should_seed_build_test_transitive {
      build_test_seed_crates.extend(owners.iter().cloned());
    }

    push_trace(
      &mut trace,
      &mut surface_refs,
      file_kind.reason_code(),
      Some(path),
      None,
      None,
      &kind_surfaces,
    );

    if extra_infra_surface {
      let infra_surfaces = vec!["infra".to_string()];
      push_trace(
        &mut trace,
        &mut surface_refs,
        RC_FILE_KIND_INFRA_PATTERN,
        Some(path),
        None,
        None,
        &infra_surfaces,
      );
    }

    if !custom_surfaces.is_empty() {
      push_trace(
        &mut trace,
        &mut surface_refs,
        RC_FILE_KIND_CUSTOM,
        Some(path),
        None,
        None,
        &custom_surfaces,
      );
    }

    planned_files.push(PlannedFile {
      path: path.clone(),
      kind: file_kind.planned_kind().to_string(),
      sub_kind: file_kind.planned_sub_kind().map(str::to_string),
      custom_surfaces,
      owners,
      owner_scope,
    });
  }

  let transitive_crates = compute_transitive_impact(ctx, &direct_crates)?;
  let execution_transitive_pairs = compute_transitive_impact_pairs(ctx, &build_test_seed_crates)?;
  let execution_transitive_crates = if build_test_seed_crates == direct_crates {
    transitive_crates.clone()
  } else {
    execution_transitive_pairs
      .iter()
      .map(|(_, dependent)| dependent.clone())
      .collect::<BTreeSet<_>>()
      .into_iter()
      .collect()
  };
  emit_transitive_build_test_trace(&execution_transitive_pairs, &mut trace, &mut surface_refs);

  let surfaces = build_surfaces(&surface_refs, &configured_custom_surfaces);

  // Compute reproducibility metadata
  let git_merge_base = if refs.merge_base {
    Some(refs.resolved_base.clone())
  } else {
    None
  };

  let impact = PlanImpact {
    direct_crates: direct_crates.into_iter().collect(),
    transitive_crates,
    execution_transitive_crates: execution_transitive_crates.clone(),
  };
  let scope = build_execution_scope(
    &impact.direct_crates,
    &execution_transitive_crates,
    &surfaces,
    ctx.cargo.metadata().workspace_packages().len(),
    &refs.resolved_base,
    &refs.resolved_head,
  );

  let output = PlanOutput {
    plan_contract_version: PLAN_CONTRACT_VERSION,
    inputs: PlanInputs {
      refs: PlanRefs {
        since: refs.since,
        from: refs.from,
        to: refs.to,
        merge_base: refs.merge_base,
        resolved_base: refs.resolved_base,
        resolved_head: refs.resolved_head,
      },
      workspace_root: ctx.workspace_root().display().to_string(),
      config_fingerprint: config_fingerprint(ctx.workspace_root()),
      toolchain_fingerprint: toolchain_fingerprint(ctx.workspace_root()),
      confidence_profile: confidence_profile_name(confidence.profile).to_string(),
      confidence_profile_source: confidence.source.to_string(),
    },
    files: planned_files,
    impact,
    scope,
    surfaces,
    trace,
    reproducibility: Reproducibility {
      cargo_rail_version: env!("CARGO_PKG_VERSION"),
      config_hash: config_fingerprint(ctx.workspace_root()),
      git_merge_base,
      git_shallow_clone: is_shallow_clone(ctx.workspace_root()),
    },
  };

  validate_surface_reason_invariants(&output)?;

  Ok(output)
}

fn unknown_file_policy(ctx: &WorkspaceContext) -> UnknownFilePolicy {
  ctx
    .config
    .as_ref()
    .map(|config| config.change_detection.unknown_file_policy)
    .unwrap_or_default()
}

fn unknown_surfaces_for_path(policy: UnknownFilePolicy, owners: &[String]) -> &'static [&'static str] {
  const BUILD_TEST: &[&str] = &["build", "test"];
  const INFRA: &[&str] = &["infra"];
  const DOCS: &[&str] = &["docs"];

  match policy {
    UnknownFilePolicy::Docs => DOCS,
    UnknownFilePolicy::OwnedBuildTest => {
      if owners.is_empty() {
        DOCS
      } else {
        BUILD_TEST
      }
    }
    UnknownFilePolicy::WorkspaceInfra => {
      if owners.is_empty() {
        INFRA
      } else {
        DOCS
      }
    }
    UnknownFilePolicy::Strict => {
      if owners.is_empty() {
        INFRA
      } else {
        BUILD_TEST
      }
    }
  }
}

fn resolve_confidence_profile(ctx: &WorkspaceContext, opts: &PlanOptions) -> RailResult<EffectiveConfidenceProfile> {
  if let Some(raw) = opts.confidence_profile.as_deref() {
    let profile = parse_confidence_profile(raw)?;
    return Ok(EffectiveConfidenceProfile {
      profile,
      source: "cli",
      bot_override: false,
    });
  }

  let mut profile = ConfidenceProfile::default();
  let mut source = "default";
  let mut bot_override = false;

  if let Some(config) = &ctx.config {
    profile = config.change_detection.confidence_profile;
    source = "config";

    if let Some(bot_profile) = config.change_detection.bot_pr_confidence_profile
      && is_bot_authored_pull_request()
    {
      profile = bot_profile;
      source = "bot_pr_policy";
      bot_override = true;
    }
  }

  Ok(EffectiveConfidenceProfile {
    profile,
    source,
    bot_override,
  })
}

fn parse_confidence_profile(value: &str) -> RailResult<ConfidenceProfile> {
  match value {
    "strict" => Ok(ConfidenceProfile::Strict),
    "balanced" => Ok(ConfidenceProfile::Balanced),
    "fast" => Ok(ConfidenceProfile::Fast),
    _ => Err(RailError::with_help(
      format!("unknown confidence profile '{}'", value),
      "use --confidence-profile strict|balanced|fast",
    )),
  }
}

fn confidence_profile_name(profile: ConfidenceProfile) -> &'static str {
  match profile {
    ConfidenceProfile::Strict => "strict",
    ConfidenceProfile::Balanced => "balanced",
    ConfidenceProfile::Fast => "fast",
  }
}

fn profile_reason_code(profile: ConfidenceProfile) -> &'static str {
  match profile {
    ConfidenceProfile::Strict => RC_CONFIDENCE_PROFILE_STRICT,
    ConfidenceProfile::Balanced => RC_CONFIDENCE_PROFILE_BALANCED,
    ConfidenceProfile::Fast => RC_CONFIDENCE_PROFILE_FAST,
  }
}

fn is_bot_authored_pull_request() -> bool {
  let event = std::env::var("GITHUB_EVENT_NAME").ok();
  let is_pr_event = matches!(event.as_deref(), Some("pull_request") | Some("pull_request_target"));
  if !is_pr_event {
    return false;
  }

  std::env::var("GITHUB_ACTOR")
    .map(|actor| actor.ends_with("[bot]"))
    .unwrap_or(false)
}

fn validate_surface_reason_invariants(output: &PlanOutput) -> RailResult<()> {
  let trace_ids: BTreeSet<u32> = output.trace.iter().map(|reason| reason.id).collect();
  for (surface, decision) in &output.surfaces {
    if decision.enabled && decision.reasons.is_empty() {
      return Err(RailError::message(format!(
        "planner invariant violated: enabled surface '{}' has no trace reasons",
        surface
      )));
    }

    for reason in &decision.reasons {
      if !trace_ids.contains(reason) {
        return Err(RailError::message(format!(
          "planner invariant violated: surface '{}' references missing trace reason id {}",
          surface, reason
        )));
      }
    }
  }

  Ok(())
}

fn ensure_surface(surfaces: &mut Vec<String>, surface: &str) {
  if !surfaces.iter().any(|existing| existing == surface) {
    surfaces.push(String::from(surface));
  }
}

fn build_execution_scope(
  direct_crates: &[String],
  execution_transitive_crates: &[String],
  surfaces: &BTreeMap<String, SurfaceDecision>,
  workspace_package_count: usize,
  resolved_base: &str,
  resolved_head: &str,
) -> ExecutionScope {
  let crates = impacted_crates_for_scope(direct_crates, execution_transitive_crates);
  let package_scoped_surface_enabled = surfaces
    .iter()
    .any(|(name, decision)| decision.enabled && PACKAGE_SCOPED_SURFACES.contains(&name.as_str()));

  let (mode, crates) = if !package_scoped_surface_enabled {
    (ExecutionScopeMode::Empty, Vec::new())
  } else if crates.is_empty() || crates.len() == workspace_package_count {
    (ExecutionScopeMode::Workspace, Vec::new())
  } else {
    (ExecutionScopeMode::Crates, crates)
  };

  ExecutionScope {
    scope_contract_version: SCOPE_CONTRACT_VERSION,
    resolved_base: resolved_base.to_string(),
    resolved_head: resolved_head.to_string(),
    mode,
    crates,
    surfaces: scope_surfaces(surfaces),
  }
}

fn impacted_crates_for_scope(direct_crates: &[String], execution_transitive_crates: &[String]) -> Vec<String> {
  direct_crates
    .iter()
    .chain(execution_transitive_crates)
    .cloned()
    .collect::<BTreeSet<_>>()
    .into_iter()
    .collect()
}

fn scope_surfaces(surfaces: &BTreeMap<String, SurfaceDecision>) -> BTreeMap<String, bool> {
  surfaces
    .iter()
    .map(|(name, decision)| (name.clone(), decision.enabled))
    .collect()
}

/// Render concise planner explain text used by command surfaces that consume planning.
pub(crate) fn render_plan_explain(output: &PlanOutput) -> String {
  format_text(output, true)
}

fn reason_description(code: &str) -> &'static str {
  match code {
    RC_FILE_KIND_RUST_SRC => "Rust source file changed",
    RC_FILE_KIND_RUST_TEST => "Rust test file changed",
    RC_FILE_KIND_RUST_BENCH => "Rust benchmark file changed",
    RC_FILE_KIND_TOML_MANIFEST => "Cargo.toml changed",
    RC_FILE_KIND_TOML_WORKSPACE => "Workspace Cargo.toml changed",
    RC_FILE_KIND_TOML_TOOLING => "Tooling config changed",
    RC_FILE_KIND_TOML_LOCKFILE => "Cargo.lock changed",
    RC_FILE_KIND_CI => "CI or workflow file changed",
    RC_FILE_KIND_SCRIPT => "Script file changed",
    RC_FILE_KIND_DOCS => "Documentation changed",
    RC_FILE_KIND_REPO_CONFIG => "Repository config changed",
    RC_FILE_KIND_INFRA_PATTERN => "Configured infrastructure pattern matched",
    RC_FILE_KIND_CUSTOM => "Custom pattern matched",
    RC_FILE_KIND_UNCLASSIFIED => "Unclassified file changed",
    RC_FILE_OWNS_CRATE_DIRECT => "File directly owns a crate",
    RC_TRANSITIVE_DEPENDS_ON_DIRECT => "Transitive dependency of changed crate",
    RC_OWNER_UNCERTAIN_FALLBACK => "Conservative fallback for uncertain ownership",
    RC_CONFIDENCE_PROFILE_STRICT => "Strict confidence profile active",
    RC_CONFIDENCE_PROFILE_BALANCED => "Balanced confidence profile active",
    RC_CONFIDENCE_PROFILE_FAST => "Fast confidence profile active",
    RC_CONFIDENCE_STRICT_OWNER_EXPANSION => "Strict mode expands owned crates",
    RC_CONFIDENCE_FAST_SKIP_TRANSITIVE => "Fast mode skips transitive expansion",
    RC_BOT_PR_CONFIDENCE_OVERRIDE => "Bot PR confidence override applied",
    _ => "Planner reason",
  }
}

fn reason_lookup(output: &PlanOutput) -> BTreeMap<u32, &TraceReason> {
  output.trace.iter().map(|reason| (reason.id, reason)).collect()
}

fn summarize_reason_ids(reason_ids: &[u32], lookup: &BTreeMap<u32, &TraceReason>) -> String {
  let mut counts: BTreeMap<&str, usize> = BTreeMap::new();
  for reason_id in reason_ids {
    if let Some(reason) = lookup.get(reason_id) {
      *counts.entry(reason.code.as_ref()).or_default() += 1;
    }
  }

  let mut ranked: Vec<(&str, usize)> = counts.into_iter().collect();
  ranked.sort_by(|(left_code, left_count), (right_code, right_count)| {
    right_count.cmp(left_count).then_with(|| left_code.cmp(right_code))
  });

  let parts: Vec<String> = ranked
    .into_iter()
    .take(3)
    .map(|(code, count)| {
      let description = reason_description(code);
      if count > 1 {
        format!("{} ({count}x)", description)
      } else {
        description.to_string()
      }
    })
    .collect();

  if parts.is_empty() {
    "No planner reasons".to_string()
  } else {
    parts.join("; ")
  }
}

fn collect_active_reason_ids(output: &PlanOutput) -> Vec<u32> {
  let mut ids = BTreeSet::new();
  for decision in output.surfaces.values() {
    if decision.enabled {
      ids.extend(decision.reasons.iter().copied());
    }
  }
  ids.into_iter().collect()
}

fn active_surface_names(output: &PlanOutput) -> Vec<&str> {
  output
    .scope
    .surfaces
    .iter()
    .filter(|(_, enabled)| **enabled)
    .map(|(name, _)| name.as_str())
    .collect()
}

fn summarize_surface_reason(output: &PlanOutput, surface: &str) -> Option<String> {
  let decision = output.surfaces.get(surface)?;
  if !decision.enabled {
    return None;
  }

  let lookup = reason_lookup(output);
  Some(summarize_reason_ids(&decision.reasons, &lookup))
}

fn resolve_refs(ctx: &WorkspaceContext, opts: &PlanOptions) -> RailResult<ResolvedRefs> {
  if let (Some(from), Some(to)) = (&opts.from, &opts.to) {
    return Ok(ResolvedRefs {
      since: opts.since.clone(),
      from: Some(from.clone()),
      to: Some(to.clone()),
      merge_base: opts.merge_base,
      resolved_base: from.clone(),
      resolved_head: to.clone(),
    });
  }

  let resolved_base = if opts.merge_base {
    let default_branch = detect_default_base_ref(ctx.git()?.git())?;
    ctx.git()?.git().get_merge_base(&default_branch, "HEAD")?
  } else if let Some(since) = &opts.since {
    since.clone()
  } else {
    detect_default_base_ref(ctx.git()?.git())?
  };

  Ok(ResolvedRefs {
    since: opts.since.clone(),
    from: opts.from.clone(),
    to: opts.to.clone(),
    merge_base: opts.merge_base,
    resolved_base,
    resolved_head: "WORKTREE".to_string(),
  })
}

fn collect_changed_files(ctx: &WorkspaceContext, refs: &ResolvedRefs) -> RailResult<Vec<String>> {
  let raw = if let (Some(from), Some(to)) = (refs.from.as_deref(), refs.to.as_deref()) {
    ctx.git()?.git().get_changed_files_between(from, Some(to))?
  } else {
    ctx.git()?.git().get_changed_files_between(&refs.resolved_base, None)?
  };

  let mut files: Vec<String> = raw
    .into_iter()
    .filter_map(|(git_path, _)| ctx.to_workspace_path(&git_path))
    .map(|p| crate::utils::path_to_git_format(&p))
    .collect();

  files.sort();
  files.dedup();
  Ok(files)
}

fn owner_scope(path: &str, owners: &[String]) -> String {
  if !owners.is_empty() {
    return "crate".to_string();
  }

  if path.starts_with(".github/")
    || path.starts_with("docs/")
    || path.starts_with("scripts/")
    || path.starts_with(".config/")
    || path.starts_with(".cargo/")
    || !path.contains('/')
  {
    "workspace".to_string()
  } else {
    "unowned".to_string()
  }
}

fn compute_transitive_impact(ctx: &WorkspaceContext, direct_crates: &BTreeSet<String>) -> RailResult<Vec<String>> {
  let direct_set: HashSet<String> = direct_crates.iter().cloned().collect();
  let mut transitive: Vec<String> = ctx
    .graph
    .transitive_dependents_of_set(&direct_set)?
    .into_iter()
    .collect();
  transitive.sort();

  Ok(transitive)
}

fn compute_transitive_impact_pairs(
  ctx: &WorkspaceContext,
  direct_crates: &BTreeSet<String>,
) -> RailResult<Vec<(String, String)>> {
  let direct_set: HashSet<String> = direct_crates.iter().cloned().collect();
  ctx.graph.transitive_dependent_pairs_of_set(&direct_set)
}

fn emit_transitive_build_test_trace(
  execution_transitive_pairs: &[(String, String)],
  trace: &mut Vec<TraceReason>,
  surface_refs: &mut BTreeMap<String, BTreeSet<u32>>,
) {
  // Use owned Strings once, reuse via slice reference
  static BUILD_TEST_SURFACES: &[&str] = &["build", "test"];
  let surfaces: Vec<String> = BUILD_TEST_SURFACES.iter().map(|&s| String::from(s)).collect();
  for (depends_on, dependent) in execution_transitive_pairs {
    push_trace(
      trace,
      surface_refs,
      RC_TRANSITIVE_DEPENDS_ON_DIRECT,
      None,
      Some(dependent),
      Some(depends_on),
      &surfaces,
    );
  }
}

fn build_surfaces(
  surface_refs: &BTreeMap<String, BTreeSet<u32>>,
  custom_surfaces: &[String],
) -> BTreeMap<String, SurfaceDecision> {
  // Use static slice and map to owned strings
  static BUILTIN_SURFACES: &[&str] = &["build", "test", "bench", "docs", "infra"];
  let mut surface_names: Vec<String> = BUILTIN_SURFACES.iter().map(|&s| String::from(s)).collect();
  surface_names.extend(custom_surfaces.iter().cloned());

  let mut result = BTreeMap::new();
  for surface in surface_names {
    let reasons: Vec<u32> = surface_refs
      .get(&surface)
      .map(|set| set.iter().copied().collect())
      .unwrap_or_default();

    result.insert(
      surface,
      SurfaceDecision {
        enabled: !reasons.is_empty(),
        reasons,
      },
    );
  }

  result
}

fn push_trace(
  trace: &mut Vec<TraceReason>,
  surface_refs: &mut BTreeMap<String, BTreeSet<u32>>,
  code: &'static str,
  file: Option<&str>,
  crate_name: Option<&str>,
  depends_on: Option<&str>,
  surfaces: &[String],
) -> u32 {
  let id = (trace.len() + 1) as u32;

  for surface in surfaces {
    surface_refs.entry(surface.clone()).or_default().insert(id);
  }

  trace.push(TraceReason {
    id,
    code,
    description: reason_description(code),
    file: file.map(ToString::to_string),
    crate_name: crate_name.map(ToString::to_string),
    depends_on: depends_on.map(ToString::to_string),
    surface: surfaces.first().cloned(),
  });

  id
}

fn format_text(output: &PlanOutput, explain: bool) -> String {
  use std::fmt::Write as _;

  let estimated_capacity = 256 + (output.impact.direct_crates.len() * 24) + (output.scope.crates.len() * 24);
  let mut out = String::with_capacity(estimated_capacity);
  let active_surfaces = active_surface_names(output);
  let lookup = reason_lookup(output);
  let top_reasons = summarize_reason_ids(&collect_active_reason_ids(output), &lookup);

  out.push_str("plan\n\n");
  let _ = writeln!(out, "base: {}", output.scope.resolved_base);
  let _ = writeln!(out, "changed files: {}", output.files.len());
  let _ = writeln!(
    out,
    "surfaces: {}",
    if active_surfaces.is_empty() {
      "none".to_string()
    } else {
      active_surfaces.join(", ")
    }
  );
  match output.scope.mode {
    ExecutionScopeMode::Workspace => {
      let _ = writeln!(out, "scope: workspace");
    }
    ExecutionScopeMode::Crates => {
      let _ = writeln!(out, "scope: crates ({})", output.scope.crates.len());
    }
    ExecutionScopeMode::Empty => {
      let _ = writeln!(out, "scope: empty");
    }
  }
  if !output.impact.direct_crates.is_empty() {
    let _ = writeln!(
      out,
      "direct crates ({}): {}",
      output.impact.direct_crates.len(),
      format_preview_list(&output.impact.direct_crates, 12)
    );
  }
  if matches!(output.scope.mode, ExecutionScopeMode::Crates) && !output.scope.crates.is_empty() {
    let _ = writeln!(
      out,
      "execution crates ({}): {}",
      output.scope.crates.len(),
      format_preview_list(&output.scope.crates, 12)
    );
  }
  let _ = writeln!(out, "why: {}", top_reasons);

  if explain {
    out.push('\n');
    out.push_str("explain:\n");
    for surface in active_surfaces {
      if let Some(summary) = summarize_surface_reason(output, surface) {
        let _ = writeln!(out, "  {}: {}", surface, summary);
      }
    }
  }

  out
}

fn format_github(output: &PlanOutput, debug: bool) -> RailResult<String> {
  use std::fmt::Write as _;

  let scope_json = to_json(&output.scope)?;
  let plan_json = if debug { Some(to_json(output)?) } else { None };
  let mut out = String::with_capacity(160 + scope_json.len() + plan_json.as_ref().map_or(0, String::len));
  let _ = writeln!(out, "build={}", surface_enabled(output, "build"));
  let _ = writeln!(out, "test={}", surface_enabled(output, "test"));
  let _ = writeln!(out, "bench={}", surface_enabled(output, "bench"));
  let _ = writeln!(out, "docs={}", surface_enabled(output, "docs"));
  let _ = writeln!(out, "infra={}", surface_enabled(output, "infra"));
  let _ = writeln!(out, "base_ref={}", output.inputs.refs.resolved_base);
  let _ = writeln!(out, "scope_json={}", scope_json);
  if let Some(plan_json) = plan_json {
    let _ = writeln!(out, "plan_json={}", plan_json);
  }

  Ok(out)
}

fn surface_enabled(output: &PlanOutput, key: &str) -> bool {
  output.surfaces.get(key).map(|s| s.enabled).unwrap_or(false)
}

fn is_shallow_clone(workspace_root: &Path) -> bool {
  // Check for .git/shallow file which indicates a shallow clone
  let shallow_file = workspace_root.join(".git/shallow");
  shallow_file.exists()
}

fn write_output(content: &str, output_file: Option<&PathBuf>) -> RailResult<()> {
  match output_file {
    Some(path) => {
      let mut file = std::fs::OpenOptions::new()
        .create(true)
        .truncate(true)
        .write(true)
        .open(path)
        .map_err(|e| RailError::message(format!("failed to open '{}': {}", path.display(), e)))?;
      writeln!(file, "{}", content)
        .map_err(|e| RailError::message(format!("failed to write '{}': {}", path.display(), e)))?;
      crate::progress!("output: {}", path.display());
    }
    None => {
      println!("{}", content);
    }
  }

  Ok(())
}