piano 0.8.2

Automated instrumentation-based profiling for Rust
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
use std::collections::HashSet;
use std::io::IsTerminal;
use std::path::{Path, PathBuf};
use std::process;

use clap::{Parser, Subcommand};

use piano::build::{
    build_instrumented, find_bin_entry_point, find_project_root, find_workspace_root,
    inject_runtime_dependency, inject_runtime_path_dependency, prepare_staging,
};
use piano::error::Error;
use piano::report::{
    diff_runs, find_latest_run_file, find_ndjson_by_run_id, format_frames_table, format_table,
    format_table_with_frames, load_latest_run, load_ndjson, load_run, load_run_by_id,
    load_tagged_run, load_two_latest_runs, relative_time, resolve_tag, reverse_resolve_tag,
    save_tag,
};
use piano::resolve::{ResolveResult, SkippedFunction, TargetSpec, resolve_targets};
use piano::rewrite::{
    inject_global_allocator, inject_registrations, inject_shutdown, instrument_source,
};

#[derive(Parser)]
#[command(
    name = "piano",
    about = "Automated instrumentation-based profiling for Rust",
    version,
    after_help = "Workflow: piano profile [OPTIONS] (or: piano build, piano run, piano report)"
)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Instrument and build the project. Profiles all functions by default;
    /// use --fn, --file, or --mod to narrow scope.
    Build {
        /// Instrument functions whose name contains PATTERN (repeatable).
        /// e.g. --fn parse matches parse, parse_line, MyStruct::try_parse.
        #[arg(long = "fn", value_name = "PATTERN")]
        fn_patterns: Vec<String>,

        /// Match --fn patterns exactly instead of by substring.
        #[arg(long, requires = "fn_patterns")]
        exact: bool,

        /// Instrument all functions in a file (repeatable).
        #[arg(long = "file", value_name = "PATH")]
        file_patterns: Vec<PathBuf>,

        /// Instrument all functions in a module (repeatable).
        #[arg(long = "mod", value_name = "NAME")]
        mod_patterns: Vec<String>,

        /// Project root (auto-detected from Cargo.toml).
        #[arg(long)]
        project: Option<PathBuf>,

        /// Path to piano-runtime source (for development before publishing).
        #[arg(long)]
        runtime_path: Option<PathBuf>,

        /// Capture per-thread CPU time alongside wall time (Unix only).
        #[arg(long)]
        cpu_time: bool,

        /// Show functions excluded from instrumentation and exit.
        #[arg(long)]
        list_skipped: bool,
    },
    /// Execute the last-built instrumented binary.
    /// Pass arguments to the binary after --.
    Run {
        /// Arguments to pass to the instrumented binary (after --).
        #[arg(last = true)]
        args: Vec<String>,
    },
    /// Build, execute, and report in one step.
    /// Pass arguments to the binary after --.
    Profile {
        /// Instrument functions whose name contains PATTERN (repeatable).
        /// e.g. --fn parse matches parse, parse_line, MyStruct::try_parse.
        #[arg(long = "fn", value_name = "PATTERN")]
        fn_patterns: Vec<String>,

        /// Match --fn patterns exactly instead of by substring.
        #[arg(long, requires = "fn_patterns")]
        exact: bool,

        /// Instrument all functions in a file (repeatable).
        #[arg(long = "file", value_name = "PATH")]
        file_patterns: Vec<PathBuf>,

        /// Instrument all functions in a module (repeatable).
        #[arg(long = "mod", value_name = "NAME")]
        mod_patterns: Vec<String>,

        /// Project root (auto-detected from Cargo.toml).
        #[arg(long)]
        project: Option<PathBuf>,

        /// Path to piano-runtime source (for development before publishing).
        #[arg(long)]
        runtime_path: Option<PathBuf>,

        /// Capture per-thread CPU time alongside wall time (Unix only).
        #[arg(long)]
        cpu_time: bool,

        /// Show functions excluded from instrumentation and exit.
        #[arg(long)]
        list_skipped: bool,

        /// Show all functions, including those with zero calls.
        #[arg(long)]
        all: bool,

        /// Show per-frame breakdown with spike detection.
        #[arg(long)]
        frames: bool,

        /// Suppress warning when program exits with non-zero code.
        #[arg(long)]
        ignore_exit_code: bool,

        /// Arguments to pass to the instrumented binary (after --).
        #[arg(last = true)]
        args: Vec<String>,
    },
    /// Show the latest profiling run (or a specific one).
    Report {
        /// Path to a specific run file. If omitted, shows the latest.
        run: Option<PathBuf>,

        /// Show all functions, including those with zero calls.
        #[arg(long)]
        all: bool,

        /// Show per-frame breakdown with spike detection.
        #[arg(long)]
        frames: bool,
    },
    /// Compare two profiling runs.
    Diff {
        /// First run (path or tag; omit both to compare two most recent runs).
        a: Option<PathBuf>,
        /// Second run (path or tag).
        b: Option<PathBuf>,
    },
    /// Tag the latest run, or list existing tags (no args).
    Tag {
        /// Tag name. If omitted, lists all saved tags.
        name: Option<String>,
    },
}

fn main() {
    let cli = Cli::parse();
    if let Err(e) = run(cli) {
        eprintln!("error: {e}");
        process::exit(1);
    }
}

fn run(cli: Cli) -> Result<(), Error> {
    match cli.command {
        Commands::Build {
            fn_patterns,
            exact,
            file_patterns,
            mod_patterns,
            project,
            runtime_path,
            cpu_time,
            list_skipped,
        } => cmd_build(
            fn_patterns,
            exact,
            file_patterns,
            mod_patterns,
            project,
            runtime_path,
            cpu_time,
            list_skipped,
        ),
        Commands::Run { args } => cmd_run(args),
        Commands::Profile {
            fn_patterns,
            exact,
            file_patterns,
            mod_patterns,
            project,
            runtime_path,
            cpu_time,
            list_skipped,
            all,
            frames,
            ignore_exit_code,
            args,
        } => cmd_profile(
            fn_patterns,
            exact,
            file_patterns,
            mod_patterns,
            project,
            runtime_path,
            cpu_time,
            list_skipped,
            all,
            frames,
            ignore_exit_code,
            args,
        ),
        Commands::Report { run, all, frames } => cmd_report(run, all, frames),
        Commands::Diff { a, b } => cmd_diff(a, b),
        Commands::Tag { name } => cmd_tag(name),
    }
}

/// Deduplicate and sort skip reasons into a comma-separated string.
fn unique_skip_reasons(skipped: &[SkippedFunction]) -> String {
    skipped
        .iter()
        .map(|s| s.reason.to_string())
        .collect::<std::collections::BTreeSet<_>>()
        .into_iter()
        .collect::<Vec<_>>()
        .join(", ")
}

/// Build an instrumented binary and return (binary_path, runs_dir).
///
/// Returns `Ok(None)` when `--list-skipped` is used (early exit after printing).
#[allow(clippy::too_many_arguments)]
fn build_project(
    fn_patterns: Vec<String>,
    exact: bool,
    file_patterns: Vec<PathBuf>,
    mod_patterns: Vec<String>,
    project: PathBuf,
    runtime_path: Option<PathBuf>,
    cpu_time: bool,
    list_skipped: bool,
) -> Result<Option<(PathBuf, PathBuf)>, Error> {
    if !project.exists() {
        return Err(Error::BuildFailed(format!(
            "project directory does not exist: {}",
            project.display()
        )));
    }
    let project = std::fs::canonicalize(&project)?;

    // Build target specs from CLI args.
    let mut specs: Vec<TargetSpec> = Vec::new();
    for p in fn_patterns {
        specs.push(TargetSpec::Fn(p));
    }
    for p in file_patterns {
        specs.push(TargetSpec::File(p));
    }
    for m in mod_patterns {
        specs.push(TargetSpec::Mod(m));
    }

    // Resolve targets against the project source.
    let src_dir = project.join("src");
    if !src_dir.is_dir() {
        return Err(Error::BuildFailed(format!(
            "no src/ directory found in {} — is this a Rust project?",
            project.display()
        )));
    }
    let ResolveResult { targets, skipped } = resolve_targets(&src_dir, &specs, exact)?;

    if list_skipped {
        if skipped.is_empty() {
            eprintln!("no functions skipped");
        } else {
            for s in &skipped {
                println!("{}: {} ({})", s.path.display(), s.name, s.reason);
            }
        }
        return Ok(None);
    }

    if !specs.is_empty() && !skipped.is_empty() {
        eprintln!(
            "warning: {} function(s) skipped ({}) -- run piano build --list-skipped to see which",
            skipped.len(),
            unique_skip_reasons(&skipped)
        );
    }

    let total_fns: usize = targets.iter().map(|t| t.functions.len()).sum();
    eprintln!(
        "found {} function(s) across {} file(s)",
        total_fns,
        targets.len()
    );
    const INSTRUMENT_ALL_WARN_THRESHOLD: usize = 200;
    if specs.is_empty() && total_fns > INSTRUMENT_ALL_WARN_THRESHOLD {
        eprintln!(
            "warning: instrumenting {total_fns} functions may add overhead — \
             use --fn, --file, or --mod to narrow scope"
        );
    }
    for target in &targets {
        let relative = target.file.strip_prefix(&src_dir).unwrap_or(&target.file);
        eprintln!("  {}:", relative.display());
        for f in &target.functions {
            eprintln!("    {f}");
        }
    }

    // Detect workspace membership. If the project is a workspace member,
    // stage from the workspace root so inherited fields and cross-member
    // path dependencies resolve correctly.
    let workspace_root = find_workspace_root(&project);
    let (staging_root, member_subdir, package_name) = if let Some(ref ws_root) = workspace_root {
        let relative = project
            .strip_prefix(ws_root)
            .map_err(|e| std::io::Error::other(e.to_string()))?
            .to_path_buf();
        // Read package name from the member's Cargo.toml.
        let member_toml = std::fs::read_to_string(project.join("Cargo.toml"))?;
        let doc: toml_edit::DocumentMut = member_toml
            .parse()
            .map_err(|e| Error::BuildFailed(format!("failed to parse member Cargo.toml: {e}")))?;
        let pkg_name = doc
            .get("package")
            .and_then(|p| p.get("name"))
            .and_then(|n| n.as_str())
            .ok_or_else(|| Error::BuildFailed("member Cargo.toml missing package.name".into()))?
            .to_string();
        (ws_root.clone(), Some(relative), Some(pkg_name))
    } else {
        (project.clone(), None, None)
    };

    // Prepare staging directory.
    let staging = tempfile::tempdir()?;
    prepare_staging(&staging_root, staging.path())?;

    // Determine the member directory within staging (workspace root for standalone).
    let member_staging = match &member_subdir {
        Some(sub) => staging.path().join(sub),
        None => staging.path().to_path_buf(),
    };

    // Inject piano-runtime dependency.
    let features: Vec<&str> = if cpu_time { vec!["cpu-time"] } else { vec![] };
    match runtime_path {
        Some(ref path) => {
            let abs_path = std::fs::canonicalize(path)?;
            inject_runtime_path_dependency(&member_staging, &abs_path, &features)?;
        }
        None => {
            inject_runtime_dependency(&member_staging, env!("PIANO_RUNTIME_VERSION"), &features)?;
        }
    }

    // Rewrite each target file in staging.
    let instrument_macros = specs.is_empty();
    let mut all_concurrency: Vec<(String, String)> = Vec::new();
    for target in &targets {
        let target_set: HashSet<String> = target.functions.iter().cloned().collect();
        let relative = target.file.strip_prefix(&src_dir).unwrap_or(&target.file);
        let staged_file = member_staging.join("src").join(relative);
        let display_path = PathBuf::from("src").join(relative);
        let source =
            std::fs::read_to_string(&staged_file).map_err(|source| Error::RunReadError {
                path: display_path.clone(),
                source,
            })?;

        let result =
            instrument_source(&source, &target_set, instrument_macros).map_err(|source| {
                Error::ParseError {
                    path: display_path,
                    source,
                }
            })?;

        all_concurrency.extend(result.concurrency);
        std::fs::write(&staged_file, result.source)?;
    }

    // Warn if parallel code was detected without --cpu-time.
    if !cpu_time && !all_concurrency.is_empty() {
        for (func, _pattern) in &all_concurrency {
            eprintln!(
                "warning: {func} spawns parallel work -- add --cpu-time to see computation time"
            );
        }
    }

    // Inject register calls into the binary entry point for all instrumented functions.
    let bin_entry = find_bin_entry_point(&member_staging)?;
    let main_file = member_staging.join(&bin_entry);
    let target_dir = project.join("target").join("piano");
    let runs_dir = target_dir.join("runs");
    std::fs::create_dir_all(&runs_dir)?;
    {
        let all_fn_names: Vec<String> = targets
            .iter()
            .flat_map(|t| t.functions.iter().cloned())
            .collect();
        let main_source =
            std::fs::read_to_string(&main_file).map_err(|source| Error::RunReadError {
                path: bin_entry.clone(),
                source,
            })?;
        let rewritten = inject_registrations(&main_source, &all_fn_names).map_err(|source| {
            Error::ParseError {
                path: bin_entry.clone(),
                source,
            }
        })?;

        // Inject global allocator for allocation tracking.
        let existing = if rewritten.contains("#[global_allocator]") {
            Some("existing")
        } else {
            None
        };
        let rewritten =
            inject_global_allocator(&rewritten, existing).map_err(|source| Error::ParseError {
                path: bin_entry.clone(),
                source,
            })?;

        let runs_dir_str = runs_dir.to_string_lossy().to_string();
        let rewritten = inject_shutdown(&rewritten, Some(&runs_dir_str)).map_err(|source| {
            Error::ParseError {
                path: bin_entry.clone(),
                source,
            }
        })?;
        std::fs::write(&main_file, rewritten)?;
    }

    // Build the instrumented binary.
    let binary = build_instrumented(staging.path(), &target_dir, package_name.as_deref())?;

    Ok(Some((binary, runs_dir)))
}

#[allow(clippy::too_many_arguments)]
fn cmd_build(
    fn_patterns: Vec<String>,
    exact: bool,
    file_patterns: Vec<PathBuf>,
    mod_patterns: Vec<String>,
    project: Option<PathBuf>,
    runtime_path: Option<PathBuf>,
    cpu_time: bool,
    list_skipped: bool,
) -> Result<(), Error> {
    let project = match project {
        Some(p) => p,
        None => find_project_root(&std::env::current_dir()?)?,
    };
    let Some((binary, _runs_dir)) = build_project(
        fn_patterns,
        exact,
        file_patterns,
        mod_patterns,
        project,
        runtime_path,
        cpu_time,
        list_skipped,
    )?
    else {
        return Ok(());
    };
    let display_name = binary
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_else(|| binary.display().to_string());
    eprintln!("built: {display_name}");
    if !std::io::stdout().is_terminal() {
        println!("{}", binary.display());
    }

    Ok(())
}

fn find_latest_binary() -> Result<PathBuf, Error> {
    let project = find_project_root(&std::env::current_dir()?).map_err(|_| Error::NoBinary)?;
    let dir = project.join("target/piano/debug");
    if !dir.is_dir() {
        return Err(Error::NoBinary);
    }
    let mut best: Option<(PathBuf, std::time::SystemTime)> = None;
    for entry in std::fs::read_dir(&dir)? {
        let entry = entry?;
        let path = entry.path();
        if !path.is_file() {
            continue;
        }
        // Skip files with extensions (e.g. .d, .fingerprint) -- binaries have no extension on unix
        if path.extension().is_some() {
            continue;
        }
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            if entry.metadata()?.permissions().mode() & 0o111 == 0 {
                continue; // not executable
            }
        }
        let mtime = entry.metadata()?.modified()?;
        if best.as_ref().is_none_or(|(_, t)| mtime > *t) {
            best = Some((path, mtime));
        }
    }
    best.map(|(p, _)| p).ok_or(Error::NoBinary)
}

fn cmd_run(args: Vec<String>) -> Result<(), Error> {
    let binary = find_latest_binary()?;
    eprintln!("running: {}", binary.display());

    let status = std::process::Command::new(&binary)
        .args(&args)
        .status()
        .map_err(|e| Error::RunFailed(format!("failed to run {}: {e}", binary.display())))?;

    std::process::exit(status.code().unwrap_or(1));
}

#[allow(clippy::too_many_arguments)]
fn cmd_profile(
    fn_patterns: Vec<String>,
    exact: bool,
    file_patterns: Vec<PathBuf>,
    mod_patterns: Vec<String>,
    project: Option<PathBuf>,
    runtime_path: Option<PathBuf>,
    cpu_time: bool,
    list_skipped: bool,
    show_all: bool,
    frames: bool,
    ignore_exit_code: bool,
    args: Vec<String>,
) -> Result<(), Error> {
    let project = match project {
        Some(p) => p,
        None => find_project_root(&std::env::current_dir()?)?,
    };
    let Some((binary, runs_dir)) = build_project(
        fn_patterns,
        exact,
        file_patterns,
        mod_patterns,
        project,
        runtime_path,
        cpu_time,
        list_skipped,
    )?
    else {
        return Ok(());
    };
    let display_name = binary
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_else(|| binary.display().to_string());
    eprintln!("built: {display_name}");

    let status = std::process::Command::new(&binary)
        .args(&args)
        .status()
        .map_err(|e| Error::RunFailed(format!("failed to run {}: {e}", binary.display())))?;

    if !status.success() && !ignore_exit_code {
        if let Some(code) = status.code() {
            eprintln!(
                "warning: program exited with code {code} — profiling results may be incomplete"
            );
        } else {
            eprintln!(
                "warning: program terminated by signal — profiling results may be incomplete"
            );
        }
    }

    // Point cmd_report at the project's runs directory so it works even when
    // CWD differs from the --project path. Skip if already set — the user
    // or test harness may have overridden it, and the runtime's shutdown_to()
    // respects PIANO_RUNS_DIR too.
    if std::env::var_os("PIANO_RUNS_DIR").is_none() {
        // SAFETY: single-threaded CLI at this point — no concurrent env reads.
        unsafe { std::env::set_var("PIANO_RUNS_DIR", &runs_dir) };
    }

    eprintln!();
    match cmd_report(None, show_all, frames) {
        Ok(()) => Ok(()),
        Err(Error::NoRuns) if !status.success() && !ignore_exit_code => {
            // Program failed and produced no data. The program's own error
            // output is the primary affordance (UX principle 6). Suppress
            // Piano's NoRuns to avoid cascading errors.
            Ok(())
        }
        Err(Error::NoRuns) => {
            // Program exited successfully but no data was written.
            // Something went wrong with the runtime's write -- give an
            // actionable message.
            Err(Error::NoDataWritten(runs_dir))
        }
        Err(e) => Err(e),
    }
}

fn cmd_report(run_path: Option<PathBuf>, show_all: bool, frames: bool) -> Result<(), Error> {
    // Resolve the run file path.
    let resolved_path = match &run_path {
        Some(p) if p.exists() => Some(p.clone()),
        Some(p) => {
            // Tag lookup — resolve to NDJSON file if available.
            let tag = p.to_string_lossy();
            let tags_dir = default_tags_dir()?;
            let runs_dir = default_runs_dir()?;
            let run_id = resolve_tag(&tags_dir, &tag)?;
            match find_ndjson_by_run_id(&runs_dir, &run_id)? {
                Some(ndjson_path) => Some(ndjson_path),
                None => {
                    // No NDJSON — fall back to basic JSON table.
                    let run = load_run_by_id(&runs_dir, &run_id).map_err(|e| match e {
                        Error::NoRuns => Error::RunNotFound {
                            tag: tag.to_string(),
                        },
                        other => other,
                    })?;
                    anstream::print!("{}", format_table(&run, show_all));
                    return Ok(());
                }
            }
        }
        None => {
            // Find the latest run file.
            let dir = default_runs_dir()?;
            find_latest_run_file(&dir)?
        }
    };

    // If we have a .ndjson file, load frame data for richer output.
    if let Some(path) = &resolved_path
        && path.extension().and_then(|e| e.to_str()) == Some("ndjson")
    {
        let (_run, frame_data) = load_ndjson(path)?;
        if frames {
            anstream::print!("{}", format_frames_table(&frame_data));
        } else {
            anstream::print!("{}", format_table_with_frames(&frame_data, show_all));
        }
        return Ok(());
    }

    // Fall back to JSON table.
    let run = match resolved_path {
        Some(p) => load_run(&p)?,
        None => {
            let dir = default_runs_dir()?;
            load_latest_run(&dir)?
        }
    };
    anstream::print!("{}", format_table(&run, show_all));
    Ok(())
}

/// Derive a display label from a diff argument.
///
/// Tag names pass through as-is; file paths use the filename stem.
fn diff_label(arg: &Path) -> String {
    if arg.exists() {
        arg.file_stem()
            .map(|s| s.to_string_lossy().into_owned())
            .unwrap_or_else(|| arg.to_string_lossy().into_owned())
    } else {
        arg.to_string_lossy().into_owned()
    }
}

fn cmd_diff(a: Option<PathBuf>, b: Option<PathBuf>) -> Result<(), Error> {
    match (a, b) {
        (Some(a), Some(b)) => {
            let label_a = diff_label(&a);
            let label_b = diff_label(&b);
            let run_a = resolve_run_arg(&a)?;
            let run_b = resolve_run_arg(&b)?;
            anstream::print!("{}", diff_runs(&run_a, &run_b, &label_a, &label_b));
        }
        (None, None) => {
            let runs_dir = default_runs_dir()?;
            let tags_dir = default_tags_dir().ok();
            let (previous, latest) = load_two_latest_runs(&runs_dir)?;

            let label_a = resolve_diff_label(&tags_dir, &previous, &runs_dir, "previous");
            let label_b = resolve_diff_label(&tags_dir, &latest, &runs_dir, "latest");
            eprintln!("comparing: {label_a} vs {label_b}");

            anstream::print!("{}", diff_runs(&previous, &latest, &label_a, &label_b));
        }
        _ => {
            return Err(Error::DiffArgCount);
        }
    }
    Ok(())
}

/// Determine a display label for an auto-selected diff run.
///
/// Uses the tag name if a tag points to this run, otherwise falls back
/// to a relative timestamp with a role prefix ("previous (2 min ago)").
fn resolve_diff_label(
    tags_dir: &Option<PathBuf>,
    run: &piano::report::Run,
    runs_dir: &Path,
    role: &str,
) -> String {
    // Try reverse-resolving a tag.
    if let (Some(tags), Some(run_id)) = (tags_dir, &run.run_id) {
        if let Some(tag) = reverse_resolve_tag(tags, run_id) {
            return tag;
        }
    }

    // Fall back to relative timestamp from file modified time.
    let stem = run.timestamp_ms.to_string();
    for ext in &["ndjson", "json"] {
        let path = runs_dir.join(format!("{stem}.{ext}"));
        if let Ok(meta) = std::fs::metadata(&path) {
            if let Ok(modified) = meta.modified() {
                let rel = relative_time(modified);
                return format!("{role} ({rel})");
            }
        }
    }

    // Last resort: use the raw timestamp with role prefix.
    format!("{role} ({stem})")
}

fn cmd_tag(name: Option<String>) -> Result<(), Error> {
    let Some(name) = name else {
        let tags_dir = match default_tags_dir() {
            Ok(dir) => dir,
            Err(Error::NoRuns) => {
                eprintln!("no tags saved");
                return Ok(());
            }
            Err(e) => return Err(e),
        };
        let mut entries: Vec<String> = std::fs::read_dir(&tags_dir)
            .map_err(|source| Error::RunReadError {
                path: tags_dir.clone(),
                source,
            })?
            .filter_map(|entry| {
                let entry = entry.ok()?;
                if entry.file_type().ok()?.is_file() {
                    Some(entry.file_name().to_string_lossy().into_owned())
                } else {
                    None
                }
            })
            .collect();
        if entries.is_empty() {
            eprintln!("no tags saved");
            return Ok(());
        }
        entries.sort();
        for tag in &entries {
            println!("{tag}");
        }
        return Ok(());
    };

    let runs_dir = default_runs_dir()?;
    let tags_dir = default_tags_dir()?;
    let latest = load_latest_run(&runs_dir)?;
    let run_id = latest.run_id.ok_or(Error::NoRuns)?;
    save_tag(&tags_dir, &name, &run_id)?;
    eprintln!("tagged '{name}'");
    Ok(())
}

fn resolve_run_arg(arg: &Path) -> Result<piano::report::Run, Error> {
    if arg.exists() {
        return load_run(arg);
    }
    let tag = arg.to_string_lossy();
    let tags_dir = default_tags_dir()?;
    let runs_dir = default_runs_dir()?;
    load_tagged_run(&tags_dir, &runs_dir, &tag)
}

fn default_runs_dir() -> Result<PathBuf, Error> {
    if let Ok(dir) = std::env::var("PIANO_RUNS_DIR") {
        return Ok(PathBuf::from(dir));
    }
    let project = find_project_root(&std::env::current_dir()?).map_err(|_| Error::NoRuns)?;
    let local = project.join("target/piano/runs");
    if local.is_dir() {
        return Ok(local);
    }
    Err(Error::NoRuns)
}

fn default_tags_dir() -> Result<PathBuf, Error> {
    if let Ok(dir) = std::env::var("PIANO_TAGS_DIR") {
        return Ok(PathBuf::from(dir));
    }
    let project = find_project_root(&std::env::current_dir()?).map_err(|_| Error::NoRuns)?;
    let local = project.join("target/piano/tags");
    if local.is_dir() {
        return Ok(local);
    }
    // Auto-create tags dir if runs exist (tags live alongside runs)
    let runs_local = project.join("target/piano/runs");
    if runs_local.is_dir() {
        std::fs::create_dir_all(&local)?;
        return Ok(local);
    }
    Err(Error::NoRuns)
}