captrack-pgo 0.1.0

Profile-guided capacity optimization for Rust collections — measure with dhat or captrack, propose data-driven Vec::with_capacity(N) values, apply in-place.
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
//! CLI definitions for captrack-pgo.
//!
//! Subcommands:
//!   measure        — full PGO collection cycle: wire → instrument → bench →
//!                    merge → uninstrument → unwire (RAII cleanup on error).
//!   apply          — Dylint-based capacity rewrite via `cargo dylint --fix`.
//!   instrument     — Dylint-based auto-wrap via `cargo dylint --fix` +
//!                    `CAPTRACK_PGO_INSTRUMENT=1`.
//!   undo           — revert the most recent `apply` or `instrument` manifest.
//!   uninstrument   — revert the most recent `instrument` manifest (strict;
//!                    rejects `apply` manifests).

use anyhow::Context as _;
use clap::{Parser, Subcommand};
use std::path::PathBuf;

use crate::analyze;
use crate::lint_apply::{self, CapFromChoice, CapRoundChoice, HasherChoice};
use crate::lint_instrument;
use crate::lint_string_reuse;
use crate::measure;
use crate::merge;
use crate::wire;

#[derive(Parser, Debug)]
#[command(name = "captrack-pgo", version, about, long_about = None)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Command,
}

#[derive(Subcommand, Debug)]
pub enum Command {
    /// Run the full PGO collection cycle: wire → instrument → bench → merge →
    /// uninstrument → unwire, in one command.
    ///
    /// The target workspace is wired with the `captrack` dependency, instrumented
    /// via `cargo dylint --fix`, each bench binary is built and run with
    /// `CAPTRACK_DUMP_DIR` set to collect per-invocation capacity samples, and
    /// then all dumps are merged into a single profile JSON that `apply` consumes.
    ///
    /// RAII cleanup: if any step fails, uninstrument + unwire still run
    /// (best-effort) so the target workspace is always restored.
    Measure {
        /// Workspace root of the target project to instrument and measure.
        #[arg(long)]
        workspace: PathBuf,

        /// Path to a local `captrack` checkout (`path = "..."` dep injection).
        #[arg(long, value_name = "PATH")]
        captrack_path: PathBuf,

        /// Bench target name(s) to build and run.  Repeat `--bench` for multiple targets.
        #[arg(long, value_name = "NAME", num_args = 1..)]
        bench: Vec<String>,

        /// Path to the `captrack-pgo-lint` crate root.
        /// Defaults to a sibling `captrack-pgo-lint/` directory.
        #[arg(long)]
        lint_path: Option<PathBuf>,

        /// Output path for the merged profile JSON.
        /// Default: `<workspace>/target/captrack-pgo/merged.json`.
        #[arg(long, value_name = "PATH")]
        out: Option<PathBuf>,

        /// Per-bench binary timeout in seconds before the process is killed.
        #[arg(long, default_value = "60", value_name = "SECS")]
        bench_timeout_secs: u64,

        /// Override `CARGO_TARGET_DIR` for the target workspace builds.
        #[arg(long, value_name = "PATH")]
        cargo_target_dir: Option<PathBuf>,

        /// Maximum samples per site after merging (0 = unlimited).  Default: 4096.
        #[arg(long, default_value = "4096")]
        reservoir_cap: usize,

        /// Pass `--allow-dirty` to `cargo dylint` (allow dirty git trees).
        #[arg(long)]
        allow_dirty: bool,
    },

    /// Apply capacity suggestions via `cargo dylint --fix` (Dylint-based rewrite).
    ///
    /// Runs the captrack-pgo-lint Dylint plugin against the target workspace with
    /// `--fix`, then records before/after file snapshots in a manifest that `undo`
    /// can revert.
    ///
    /// Requires: `cargo install cargo-dylint dylint-link` and a nightly toolchain
    /// pinned in `captrack-pgo-lint/rust-toolchain.toml`.
    ///
    /// ## Staleness guard
    ///
    /// If `target/captrack-pgo/last-instrument-hashes.json` exists (written by
    /// a prior `instrument` run), every recorded file's current SHA-256 is
    /// checked against the snapshot.  A mismatch (or a deleted file) aborts
    /// `apply` before it touches anything, since the profile's `(file, line,
    /// column)` sites may no longer point at the intended code.  Pass
    /// `--force` to skip this check.  If the hashes file does not exist at
    /// all, the check is silently skipped (e.g. when `apply` is run against a
    /// profile that was never produced via this binary's `instrument` step).
    ///
    /// When `--hasher` is set to anything other than `none`, every matched
    /// `HashMap`/`HashSet` constructor is also upgraded to `with_capacity_and_hasher`.
    /// Note: sites with an explicit local type ascription (`let m: HashMap<K,V> = ...`)
    /// have the hasher injection skipped automatically to avoid compile errors;
    /// sites in struct fields or function return types are not detected and may
    /// need manual correction.  Remember to add the hasher crate to your Cargo.toml.
    Apply {
        /// Path to a captrack profile JSON (required).
        #[arg(long)]
        profile: PathBuf,

        /// Path to the captrack-pgo-lint crate root.
        /// Defaults to a sibling `captrack-pgo-lint/` directory.
        #[arg(long)]
        lint_path: Option<PathBuf>,

        /// Workspace root to patch (defaults to current directory).
        #[arg(long)]
        workspace: Option<PathBuf>,

        /// Show suggestions without applying or writing a manifest.
        #[arg(long)]
        dry_run: bool,

        /// Pass --allow-dirty to cargo dylint (allow dirty git trees).
        #[arg(long)]
        allow_dirty: bool,

        /// Inject a non-default hasher into HashMap/HashSet constructors.
        ///
        /// Accepted values: `fx` (FxBuildHasher), `ahash` (ahash::RandomState),
        /// `foldhash` (foldhash::fast::RandomState), `none` (default — no change).
        ///
        /// The chosen hasher crate must be added to the target workspace's
        /// Cargo.toml manually; captrack-pgo emits a reminder after apply.
        #[arg(long, value_name = "HASHER", default_value = "none", value_parser = parse_hasher)]
        hasher: HasherChoice,

        /// Source statistic used as the base capacity value.
        ///
        /// max    — peak observed value; guarantees zero reallocations but may
        ///          waste memory when outliers are rare.
        /// mean   — arithmetic mean.  Only meaningful with captrack-backed profiles;
        ///          dhat-loaded profiles set mean=peak.
        /// median — 50th percentile (p50).
        /// p95    — 95th percentile (default; covers 95% of cases with less waste
        ///          than max).
        /// p99    — 99th percentile.
        #[arg(long, default_value = "p95", value_parser = parse_cap_from)]
        cap_from: CapFromChoice,

        /// Multiplier applied to the source statistic before rounding.
        ///
        /// Examples: --cap-from median --cap-mul 2.0 uses median×2.
        /// Must be > 0.0.  Default: 1.0.
        #[arg(long, default_value = "1.0")]
        cap_mul: f64,

        /// Rounding mode applied after the multiplier.
        ///
        /// pow2  — round up to the next power of two (default; matches Vec
        ///         doubling strategy and is cache-friendly).
        /// to8   — round up to the nearest multiple of 8.
        /// exact — no rounding; use the exact computed value (truncated to usize).
        #[arg(long, default_value = "pow2", value_parser = parse_cap_round)]
        cap_round: CapRoundChoice,

        /// Bypass the staleness guard that refuses to apply when source
        /// files changed since the last `instrument` run recorded a hash
        /// snapshot (`target/captrack-pgo/last-instrument-hashes.json`).
        ///
        /// Use when you know the changes don't affect the profiled sites, or
        /// when you intentionally want to apply against an older snapshot.
        #[arg(long)]
        force: bool,
    },

    /// Auto-instrument every bare std collection constructor with
    /// `::captrack::TrackedX::with_capacity_named(...)` via `cargo dylint --fix`.
    ///
    /// Sets `CAPTRACK_PGO_INSTRUMENT=1` in the child environment and explicitly
    /// unsets `CAPTRACK_PGO_PROFILE` so the two lint modes can't conflict.
    ///
    /// After the run a manifest is written to
    /// `target/captrack-pgo/last-instrument.json` which `undo` (or a future
    /// `uninstrument` subcommand) can revert.
    ///
    /// Requires: `cargo install cargo-dylint dylint-link` and a nightly toolchain
    /// pinned in `captrack-pgo-lint/rust-toolchain.toml`.
    Instrument {
        /// Path to the captrack-pgo-lint crate root.
        /// Defaults to a sibling `captrack-pgo-lint/` directory.
        #[arg(long)]
        lint_path: Option<PathBuf>,

        /// Workspace root to instrument (defaults to current directory).
        #[arg(long)]
        workspace: Option<PathBuf>,

        /// Run `cargo dylint` WITHOUT `--fix` so suggestions appear in stderr
        /// without modifying any file; no manifest is written.
        #[arg(long)]
        dry_run: bool,

        /// Pass --allow-dirty to cargo dylint (allow dirty git trees).
        #[arg(long)]
        allow_dirty: bool,
    },

    /// Rewrite `String` bindings that are fully reassigned inside a loop into
    /// buffer-reusing `clear()` + `push_str(...)` form, via `cargo dylint --fix`.
    ///
    /// Sets `CAPTRACK_PGO_STRING_REUSE=1` in the child environment and unsets
    /// `CAPTRACK_PGO_PROFILE` / `CAPTRACK_PGO_INSTRUMENT` so only the
    /// string-reuse lint pass fires.
    ///
    /// The lint is conservative: it fires only for a single top-level
    /// `s = <expr>;` in a loop body where `<expr>` does not read `s`.  See the
    /// lint's rustdoc for the full list of intentional false negatives.
    ///
    /// After the run a manifest is written to
    /// `target/captrack-pgo/last-string-reuse.json` which `undo` can revert.
    ///
    /// Requires: `cargo install cargo-dylint dylint-link` and a nightly
    /// toolchain pinned in `captrack-pgo-lint/rust-toolchain.toml`.
    StringReuse {
        /// Path to the captrack-pgo-lint crate root.
        /// Defaults to a sibling `captrack-pgo-lint/` directory.
        #[arg(long)]
        lint_path: Option<PathBuf>,

        /// Workspace root to rewrite (defaults to current directory).
        #[arg(long)]
        workspace: Option<PathBuf>,

        /// Run `cargo dylint` WITHOUT `--fix` so suggestions appear in stderr
        /// without modifying any file; no manifest is written.
        #[arg(long)]
        dry_run: bool,

        /// Pass --allow-dirty to cargo dylint (allow dirty git trees).
        #[arg(long)]
        allow_dirty: bool,
    },

    /// Roll back the most recent `apply` or `instrument` using its manifest.
    ///
    /// Without `--manifest`, picks the most recently modified manifest among
    /// `target/captrack-pgo/last-apply.json` and
    /// `target/captrack-pgo/last-instrument.json`.
    Undo {
        /// Explicit manifest file to revert.
        #[arg(long)]
        manifest: Option<PathBuf>,
    },

    /// Roll back an `instrument` run using its manifest (strict variant).
    ///
    /// Unlike `undo`, this subcommand ONLY accepts manifests produced by the
    /// `instrument` subcommand (`operation.op == "instrument"`).  If the
    /// manifest was written by `apply`, it exits with an error pointing the
    /// user to `captrack-pgo undo`.
    ///
    /// Without `--manifest`, uses the conventional instrument manifest path
    /// `target/captrack-pgo/last-instrument.json` (never falls back to
    /// `last-apply.json`).
    Uninstrument {
        /// Workspace root (defaults to current directory).
        #[arg(long)]
        workspace: Option<PathBuf>,

        /// Explicit manifest path (defaults to
        /// `<workspace>/target/captrack-pgo/last-instrument.json`).
        #[arg(long)]
        manifest: Option<PathBuf>,
    },

    /// Add `captrack` as a dependency to every Cargo.toml in the target
    /// workspace so the post-`instrument` build can resolve `::captrack::*`
    /// paths.
    ///
    /// Patches the root manifest (`[workspace.dependencies] captrack = ...`)
    /// and every member manifest (`[dependencies] captrack = { workspace = true }`).
    /// A snapshot of every modified file is written to
    /// `target/captrack-pgo/last-wire.json` for `unwire` to revert losslessly.
    ///
    /// Already-wired manifests are skipped — `wire` is idempotent.
    Wire {
        /// Workspace root to patch (defaults to current directory).
        #[arg(long)]
        workspace: Option<PathBuf>,

        /// Use a local `captrack` checkout via `path = "..."`.  If omitted, the
        /// published `version = "0.1"` from crates.io is used instead.
        #[arg(long, value_name = "PATH")]
        captrack_path: Option<PathBuf>,
    },

    /// Remove the `captrack` dependency entries that `wire` added, restoring
    /// every Cargo.toml to its pre-wire state.
    ///
    /// Reads `target/captrack-pgo/last-wire.json` (or `--manifest <path>`),
    /// verifies each file is still in the post-wire state recorded there
    /// (refuses to revert if the user has manually edited any of them), and
    /// writes back the pre-wire contents.  The manifest file is deleted on
    /// successful revert.
    Unwire {
        /// Workspace root (defaults to current directory).
        #[arg(long)]
        workspace: Option<PathBuf>,

        /// Explicit manifest path (defaults to
        /// `<workspace>/target/captrack-pgo/last-wire.json`).
        #[arg(long)]
        manifest: Option<PathBuf>,
    },

    /// Merge multiple per-process captrack profile dumps into one.
    ///
    /// When `wire → instrument → bench/test` produces
    /// `profile-<binary>-<pid>-<start_ms>.json` files (one per OS process),
    /// this command deduplicates sites by `(file, line, column)`, sums
    /// `creation_count`, and concatenates (then optionally
    /// reservoir-samples) the per-site `samples` arrays.
    /// The output is sorted by `max(samples)` descending.
    ///
    /// Glob patterns in `--inputs` (e.g. `*.json`) are expanded automatically.
    Merge {
        /// One or more input profile JSON paths.  May contain `*`/`?` globs.
        #[arg(long = "inputs", value_name = "PATH", num_args = 1..)]
        inputs: Vec<String>,

        /// Destination path for the merged profile JSON.
        #[arg(long)]
        output: PathBuf,

        /// Maximum samples to keep per site after merging (Vitter R reservoir
        /// sampling).  0 disables sampling.  Default: 4096.
        #[arg(long, default_value = "4096")]
        reservoir_cap: usize,
    },

    /// Classify each site's sample distribution and recommend per-site capacity
    /// policy overrides.
    ///
    /// Reads a captrack profile JSON, classifies each site into one of:
    /// UnimodalTight, UnimodalSpread, Bimodal, HeavyTail, MostlyZero, or
    /// InsufficientData.  Prints a report (stdout or `--report <file>`) and
    /// optionally injects `policy` fields back into the profile JSON so
    /// `apply` picks them up per-site.
    Analyze {
        /// Path to a captrack profile JSON (raw dump or merged).
        #[arg(long)]
        profile: PathBuf,

        /// Inject recommended policy fields back into the profile JSON.
        ///
        /// When set, each site with a non-default recommendation gets a
        /// `policy: { cap_from, cap_mul, cap_round }` object written into
        /// the profile JSON in-place.  The `apply` subcommand reads this
        /// field per-site and overrides the global CLI defaults.
        #[arg(long)]
        write_policy: bool,

        /// Write the text report to this file instead of stdout.
        #[arg(long)]
        report: Option<PathBuf>,
    },
}

/// Clap value_parser for `--hasher`.
fn parse_hasher(s: &str) -> Result<HasherChoice, String> {
    match s.trim().to_ascii_lowercase().as_str() {
        "none" | "" => Ok(HasherChoice::None),
        "fx" => Ok(HasherChoice::Fx),
        "ahash" => Ok(HasherChoice::AHash),
        "foldhash" => Ok(HasherChoice::FoldHash),
        other => Err(format!(
            "unknown hasher {:?}; accepted values: fx, ahash, foldhash, none",
            other
        )),
    }
}

/// Clap value_parser for `--cap-from`.
fn parse_cap_from(s: &str) -> Result<CapFromChoice, String> {
    match s.trim().to_ascii_lowercase().as_str() {
        "max" => Ok(CapFromChoice::Max),
        "mean" => Ok(CapFromChoice::Mean),
        "median" => Ok(CapFromChoice::Median),
        "p95" => Ok(CapFromChoice::P95),
        "p99" => Ok(CapFromChoice::P99),
        other => Err(format!(
            "unknown cap-from {:?}; accepted values: max, mean, median, p95, p99",
            other
        )),
    }
}

/// Clap value_parser for `--cap-round`.
fn parse_cap_round(s: &str) -> Result<CapRoundChoice, String> {
    match s.trim().to_ascii_lowercase().as_str() {
        "pow2" => Ok(CapRoundChoice::Pow2),
        "to8" => Ok(CapRoundChoice::To8),
        "exact" => Ok(CapRoundChoice::Exact),
        other => Err(format!(
            "unknown cap-round {:?}; accepted values: pow2, to8, exact",
            other
        )),
    }
}

pub fn dispatch(cli: Cli) -> anyhow::Result<()> {
    match cli.command {
        Command::Measure {
            workspace,
            captrack_path,
            bench,
            lint_path,
            out,
            bench_timeout_secs,
            cargo_target_dir,
            reservoir_cap,
            allow_dirty,
        } => {
            use crate::workspace as ws;
            // Resolve workspace root for the default --out path.
            let workspace_start = workspace.clone();
            let workspace_root = ws::find_workspace_root(&workspace_start).with_context(|| {
                format!("locate workspace root from {}", workspace_start.display())
            })?;
            let out_resolved = out.unwrap_or_else(|| {
                workspace_root
                    .join("target")
                    .join("captrack-pgo")
                    .join("merged.json")
            });
            let report = measure::run_measure(measure::MeasureArgs {
                workspace,
                captrack_path,
                benches: bench,
                lint_path,
                out: out_resolved,
                bench_timeout: std::time::Duration::from_secs(bench_timeout_secs),
                cargo_target_dir,
                reservoir_cap,
                allow_dirty,
            })?;
            println!("measure: done");
            println!("  merged profile: {}", report.merged_path.display());
            println!("  unique sites:   {}", report.unique_sites);
            println!("  benches run:    {}", report.benches_run.len());
            for b in &report.benches_run {
                println!(
                    "    {} (exit={}) → {} dump file{}",
                    b.name,
                    b.exit_code,
                    b.profile_paths.len(),
                    if b.profile_paths.len() == 1 { "" } else { "s" },
                );
            }
        }
        Command::Apply {
            profile,
            lint_path,
            workspace,
            dry_run,
            allow_dirty,
            hasher,
            cap_from,
            cap_mul,
            cap_round,
            force,
        } => {
            // Pre-flight: cap_mul must be positive and finite.
            if cap_mul <= 0.0 || !cap_mul.is_finite() {
                return Err(anyhow::anyhow!(
                    "--cap-mul must be > 0.0 and finite; got {}",
                    cap_mul
                ));
            }
            run_apply(
                profile,
                lint_path,
                workspace,
                dry_run,
                allow_dirty,
                hasher,
                cap_from,
                cap_mul,
                cap_round,
                force,
            )?;
        }
        Command::Instrument {
            lint_path,
            workspace,
            dry_run,
            allow_dirty,
        } => {
            run_instrument(lint_path, workspace, dry_run, allow_dirty)?;
        }
        Command::StringReuse {
            lint_path,
            workspace,
            dry_run,
            allow_dirty,
        } => {
            run_string_reuse(lint_path, workspace, dry_run, allow_dirty)?;
        }
        Command::Undo { manifest } => {
            run_undo(manifest)?;
        }
        Command::Uninstrument {
            workspace,
            manifest,
        } => {
            run_uninstrument(workspace, manifest)?;
        }
        Command::Wire {
            workspace,
            captrack_path,
        } => {
            run_wire(workspace, captrack_path)?;
        }
        Command::Unwire {
            workspace,
            manifest,
        } => {
            run_unwire(workspace, manifest)?;
        }
        Command::Merge {
            inputs,
            output,
            reservoir_cap,
        } => {
            run_merge(inputs, output, reservoir_cap)?;
        }
        Command::Analyze {
            profile,
            write_policy,
            report,
        } => {
            analyze::run_analyze(analyze::AnalyzeArgs {
                profile,
                write_policy,
                report,
            })?;
        }
    }
    Ok(())
}

fn run_merge(inputs: Vec<String>, output: PathBuf, reservoir_cap: usize) -> anyhow::Result<()> {
    let resolved = merge::expand_inputs(&inputs).context("expand --inputs")?;
    let report = merge::run_merge(merge::MergeArgs {
        inputs: resolved,
        output: output.clone(),
        reservoir_cap,
    })?;
    println!(
        "merged {} input{} → {} unique site{}, {} samples → {} samples (reservoir_cap={}) → {}",
        report.inputs_count,
        if report.inputs_count == 1 { "" } else { "s" },
        report.unique_sites,
        if report.unique_sites == 1 { "" } else { "s" },
        report.total_samples_pre_reservoir,
        report.total_samples_post,
        if reservoir_cap == 0 {
            "disabled".to_string()
        } else {
            reservoir_cap.to_string()
        },
        output.display(),
    );
    Ok(())
}

fn run_wire(workspace: Option<PathBuf>, captrack_path: Option<PathBuf>) -> anyhow::Result<()> {
    use crate::workspace as ws;
    let workspace_start = workspace.unwrap_or_else(|| std::env::current_dir().expect("cwd"));
    let workspace_root = ws::find_workspace_root(&workspace_start)
        .with_context(|| format!("locate workspace root from {}", workspace_start.display()))?;
    wire::run_wire(wire::WireArgs {
        workspace_root,
        captrack_path,
    })
}

fn run_unwire(workspace: Option<PathBuf>, manifest: Option<PathBuf>) -> anyhow::Result<()> {
    use crate::workspace as ws;
    let workspace_start = workspace.unwrap_or_else(|| std::env::current_dir().expect("cwd"));
    let workspace_root = ws::find_workspace_root(&workspace_start)
        .with_context(|| format!("locate workspace root from {}", workspace_start.display()))?;
    wire::run_unwire(wire::UnwireArgs {
        workspace_root,
        manifest,
    })
}

// Flat mapper from the clap `Apply` variant's fields to `LintApplyArgs` —
// mirrors the CLI surface one-to-one, so the argument count tracks the flag
// count by design.
#[allow(clippy::too_many_arguments)]
fn run_apply(
    profile: PathBuf,
    lint_path: Option<PathBuf>,
    workspace: Option<PathBuf>,
    dry_run: bool,
    allow_dirty: bool,
    hasher: HasherChoice,
    cap_from: CapFromChoice,
    cap_mul: f64,
    cap_round: CapRoundChoice,
    force: bool,
) -> anyhow::Result<()> {
    use crate::workspace as ws;

    // Resolve workspace root.
    let workspace_start = workspace.unwrap_or_else(|| std::env::current_dir().expect("cwd"));
    let workspace_root = ws::find_workspace_root(&workspace_start)
        .with_context(|| format!("locate workspace root from {}", workspace_start.display()))?;

    // Resolve lint-path (use provided or discover default).
    let resolved_lint_path = match lint_path {
        Some(p) => p,
        None => lint_apply::resolve_default_lint_path()?,
    };

    lint_apply::run_lint_apply(lint_apply::LintApplyArgs {
        profile_path: profile,
        lint_path: resolved_lint_path,
        workspace_root,
        dry_run,
        allow_dirty,
        hasher,
        cap_from,
        cap_mul,
        cap_round,
        force,
    })
}

fn run_instrument(
    lint_path: Option<PathBuf>,
    workspace: Option<PathBuf>,
    dry_run: bool,
    allow_dirty: bool,
) -> anyhow::Result<()> {
    use crate::workspace as ws;

    // Resolve workspace root.
    let workspace_start = workspace.unwrap_or_else(|| std::env::current_dir().expect("cwd"));
    let workspace_root = ws::find_workspace_root(&workspace_start)
        .with_context(|| format!("locate workspace root from {}", workspace_start.display()))?;

    // Resolve lint-path.
    let resolved_lint_path = lint_instrument::resolve_lint_path(lint_path)?;

    lint_instrument::run_lint_instrument(lint_instrument::LintInstrumentArgs {
        lint_path: resolved_lint_path,
        workspace_root,
        dry_run,
        allow_dirty,
    })
}

fn run_string_reuse(
    lint_path: Option<PathBuf>,
    workspace: Option<PathBuf>,
    dry_run: bool,
    allow_dirty: bool,
) -> anyhow::Result<()> {
    use crate::workspace as ws;

    let workspace_start = workspace.unwrap_or_else(|| std::env::current_dir().expect("cwd"));
    let workspace_root = ws::find_workspace_root(&workspace_start)
        .with_context(|| format!("locate workspace root from {}", workspace_start.display()))?;

    let resolved_lint_path = lint_string_reuse::resolve_lint_path(lint_path)?;

    lint_string_reuse::run_lint_string_reuse(lint_string_reuse::LintStringReuseArgs {
        lint_path: resolved_lint_path,
        workspace_root,
        dry_run,
        allow_dirty,
    })
}

fn run_undo(manifest: Option<PathBuf>) -> anyhow::Result<()> {
    use crate::workspace as ws;

    let path = match manifest {
        Some(p) => p,
        None => {
            let root = ws::find_workspace_root(&std::env::current_dir()?)?;
            match lint_apply::latest_manifest_path(&root) {
                Some(p) => p,
                None => {
                    return Err(anyhow::anyhow!(
                        "no manifest found; expected one of:\n  \
                         {}\n  {}",
                        lint_apply::default_apply_manifest_path(&root).display(),
                        lint_apply::default_instrument_manifest_path(&root).display()
                    ));
                }
            }
        }
    };

    let n = lint_apply::undo_lint_apply(&path)?;
    println!(
        "reverted {n} file{} from {}",
        if n == 1 { "" } else { "s" },
        path.display()
    );

    Ok(())
}

fn run_uninstrument(workspace: Option<PathBuf>, manifest: Option<PathBuf>) -> anyhow::Result<()> {
    use crate::lint_apply::Operation;
    use crate::workspace as ws;

    // 1. Resolve workspace root (needed only when --manifest is omitted).
    let manifest_path = match manifest {
        Some(p) => p,
        None => {
            let workspace_start =
                workspace.unwrap_or_else(|| std::env::current_dir().expect("cwd"));
            let workspace_root = ws::find_workspace_root(&workspace_start).with_context(|| {
                format!("locate workspace root from {}", workspace_start.display())
            })?;
            lint_apply::default_instrument_manifest_path(&workspace_root)
        }
    };

    // 2. Read + parse the manifest; surface helpful errors.
    let manifest = lint_apply::read_manifest(&manifest_path)?;

    // 3. Validate the operation type.
    match &manifest.operation {
        Operation::Instrument => {
            // Correct type — proceed.
        }
        Operation::Apply { .. } => {
            return Err(anyhow::anyhow!(
                "manifest at {} is from `apply` (capacity rewrite), not `instrument`. \
                 Use `captrack-pgo undo` to revert it.",
                manifest_path.display()
            ));
        }
    }

    // 4. Delegate to the generic revert (re-reads + verifies the manifest).
    let n = lint_apply::undo_lint_apply(&manifest_path)?;

    // 5. Summary (manifest file is intentionally left in place).
    println!(
        "uninstrument: reverted {n} file{} from {}",
        if n == 1 { "" } else { "s" },
        manifest_path.display()
    );

    Ok(())
}