rust-anything 1.5.1

Rewrite your app in Rust, one piece at a time, checked against the original's recorded behavior at every step. TypeScript/JavaScript, Python, C, Go, Ruby and Java
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
use rust_anything::walk::Canonical;
use std::io::Write as _;
use std::path::PathBuf;
use std::process::ExitCode;

/// `println!` that stops quietly when stdout is closed (`rust-anything ... | head`).
macro_rules! say {
    ($($arg:tt)*) => {{
        let _ = writeln!(std::io::stdout().lock(), $($arg)*);
    }};
}

macro_rules! say_raw {
    ($($arg:tt)*) => {{
        let _ = write!(std::io::stdout().lock(), $($arg)*);
    }};
}

use anyhow::{Context, Result};
use clap::{Parser, Subcommand, ValueEnum};
use rust_anything::{analyze, app, bridge, front_door, gate, harness, lift, mcp, report, shadow};

#[derive(Parser)]
#[command(
    name = "rust-anything",
    version,
    about = "Rewrite TypeScript/JavaScript, Python, C, Go, Ruby or Java apps in Rust, one verified piece at a time",
    after_help = "Loop per unit: analyze -> bridge -> record -> port src/port.rs -> build -> diff -> shadow -> gate"
)]
struct Cli {
    #[command(subcommand)]
    command: Cmd,
}

#[derive(Clone, Copy, ValueEnum)]
enum KindArg {
    Napi,
    Wasm,
}

#[derive(Subcommand)]
enum Cmd {
    /// 1. Map a repository and write a ranked, ordered migration plan.
    Analyze {
        /// Repository root.
        #[arg(default_value = ".")]
        root: PathBuf,
        /// Print the machine-readable report (schema `rust-anything/analyze@1`) instead of Markdown.
        #[arg(long)]
        json: bool,
        /// Write to this file instead of stdout.
        #[arg(long, short)]
        out: Option<PathBuf>,
        /// Source language; detected from the repository when omitted.
        #[arg(long, value_enum)]
        lang: Option<rust_anything::lang::Lang>,
        /// Plan a whole-app port: entry points and shape, every dependency (imported or
        /// declared) with its crate, pieces in port order, and decisions for the user.
        #[arg(long)]
        whole_app: bool,
    },
    /// 2. Scaffold a Rust crate with JS bindings for one unit.
    Bridge {
        /// Repository root.
        root: PathBuf,
        /// Unit id, as `analyze` lists it.
        unit: String,
        /// Bridge to generate; defaults to the plan's choice.
        #[arg(long, value_enum)]
        kind: Option<KindArg>,
        /// Crate folder; defaults to `<root>/rust/<name>`.
        #[arg(long, short)]
        out: Option<PathBuf>,
        /// Crate name; defaults to `<unit>-rs`.
        #[arg(long)]
        name: Option<String>,
        /// Bridge a unit the plan marks blocked or keep.
        #[arg(long)]
        force: bool,
        /// Keep an export in TS: `name` or `"name: reason"`. Repeatable.
        #[arg(long)]
        keep: Vec<String>,
        /// Add a batch class: `function:param` runs `function` over many `param` values kept in
        /// Rust, on all cores (what `profile` recommends for leaves called in loops).
        #[arg(long)]
        batch: Vec<String>,
    },
    /// 2b. Regenerate the bindings (`lib.rs`, `index.cjs`, `js.rs`) from the signatures in `port.rs`.
    Refresh {
        #[arg(default_value = ".")]
        crate_dir: PathBuf,
        /// Add a batch class: `function:param` runs `function` over many `param` values kept in
        /// Rust, on all cores. Repeat for more.
        #[arg(long)]
        batch: Vec<String>,
    },
    /// 3a. Record what the TS functions return on generated and given inputs.
    Record {
        /// Crate folder made by `bridge`.
        #[arg(default_value = ".")]
        crate_dir: PathBuf,
        /// Generated cases per function.
        #[arg(long, default_value_t = 200)]
        count: usize,
        /// Extra cases: JSONL of `{"fn": name, "args": [...]}`.
        #[arg(long)]
        inputs: Option<PathBuf>,
        /// Import the original from this JS module (e.g. built output) instead of the TS source.
        #[arg(long)]
        module: Option<PathBuf>,
        /// Resolve npm packages from this folder's `node_modules` (when the repo has none installed).
        #[arg(long)]
        resolve_from: Option<PathBuf>,
        /// Extra arguments for node, e.g. `--node-arg=--import=tsx`.
        #[arg(long = "node-arg", allow_hyphen_values = true)]
        node_args: Vec<String>,
        /// Floats match when they differ by at most this times the larger magnitude (absolute
        /// below 1), e.g. 1e-9 for numeric code. Saved with the crate for diff and gate; 0 removes it.
        #[arg(long)]
        float_tolerance: Option<f64>,
    },
    /// 3b. Build the crate's JS module (napi-rs `.node` or wasm-bindgen output).
    Build {
        #[arg(default_value = ".")]
        crate_dir: PathBuf,
    },
    /// 3c. Replay recorded cases against the Rust module.
    Diff {
        #[arg(default_value = ".")]
        crate_dir: PathBuf,
        /// Test this JS module instead of the crate's `index.cjs`.
        #[arg(long)]
        module: Option<PathBuf>,
        /// Compare objects regardless of key order.
        #[arg(long)]
        ignore_key_order: bool,
        /// Floats match within this tolerance for this run (the one saved by `record` otherwise).
        #[arg(long)]
        float_tolerance: Option<f64>,
        #[arg(long)]
        json: bool,
        #[arg(long = "node-arg", allow_hyphen_values = true)]
        node_args: Vec<String>,
    },
    /// 3d. Time the recorded cases on the TS original and on Rust, bridge included.
    Bench {
        #[arg(default_value = ".")]
        crate_dir: PathBuf,
        /// Minimum measured time per function and side, in milliseconds.
        #[arg(long, default_value_t = 200)]
        min_ms: u64,
        /// Resolve npm packages the original imports from this folder's `node_modules`.
        #[arg(long)]
        resolve_from: Option<PathBuf>,
        #[arg(long)]
        json: bool,
        #[arg(long = "node-arg", allow_hyphen_values = true)]
        node_args: Vec<String>,
    },
    /// Run a real workload under the language's profiler and say what to port: hot functions,
    /// their callers, and loops to batch. `rust-anything profile <repo> -- node bench.mjs` (also
    /// `python3 x.py`, `go test -bench . ./pkg`, `java ...`, `ruby x.rb`)
    Profile {
        /// Repository root.
        root: PathBuf,
        #[arg(long)]
        json: bool,
        /// Source language; detected from the repository when omitted.
        #[arg(long, value_enum)]
        lang: Option<rust_anything::lang::Lang>,
        /// The workload, after `--`.
        #[arg(last = true, required = true)]
        command: Vec<String>,
    },
    /// 4. Generate a drop-in module that runs TS and Rust side by side.
    Shadow {
        #[arg(default_value = ".")]
        crate_dir: PathBuf,
        /// Where to write the module; defaults to `<crate>/shadow/<name>.shadow.ts`.
        #[arg(long, short)]
        out: Option<PathBuf>,
    },
    /// 5. Check the definition of done for a port.
    Gate {
        #[arg(default_value = ".")]
        crate_dir: PathBuf,
        #[arg(long)]
        json: bool,
        #[arg(long = "node-arg", allow_hyphen_values = true)]
        node_args: Vec<String>,
    },
    /// Record the whole app's behavior and replay any build against it (CLIs).
    App {
        #[command(subcommand)]
        command: AppCmd,
    },
    /// Serve every step as MCP tools over stdio.
    Mcp,
    /// Register rust-anything with your coding agents (Claude Code, Codex, Cursor, Gemini CLI):
    /// the MCP server and, where the agent reads skills, the workflow skill. Keeps your other
    /// settings; run it again any time.
    Setup {
        /// Agents to set up (repeatable); every agent found by default.
        #[arg(long = "agent", value_enum)]
        agents: Vec<rust_anything::setup::Agent>,
        /// Take the rust-anything entries out again.
        #[arg(long)]
        remove: bool,
        /// Show what would change, write nothing.
        #[arg(long)]
        dry_run: bool,
        #[arg(long)]
        json: bool,
    },
}

#[derive(Subcommand)]
enum AppCmd {
    /// Run the original CLI on cases (its own examples, `--inputs`, semantic traps), twice
    /// each, and store them as trycmd files in `<repo>/rust/app-cases/`.
    /// `rust-anything app record . -- python3 cli.py`
    Record {
        /// Repository root.
        #[arg(default_value = ".")]
        repo: PathBuf,
        /// Extra cases: JSONL of `{"name", "args": [...], "stdin", "env": {...}, "files": {path: text}}`.
        #[arg(long)]
        inputs: Option<PathBuf>,
        /// Normalizer: `timestamps`, `uuids`, `key-order`, or `name=regex` for your own. Repeatable;
        /// saved with the cases.
        #[arg(long)]
        normalize: Vec<String>,
        /// The name the app is invoked by in its docs (found from the command and manifests when omitted).
        #[arg(long)]
        name: Option<String>,
        /// Leave out the semantic-trap inputs.
        #[arg(long)]
        no_traps: bool,
        /// Seconds before a run is killed (default 30).
        #[arg(long)]
        timeout: Option<u64>,
        /// The app is a server: the command starts it, taking its port from PORT or `{port}`;
        /// cases are requests (curl lines in its docs, `--inputs`, traps), kept in `http/`.
        #[arg(long)]
        http: bool,
        /// A port the server always listens on (implies --http).
        #[arg(long)]
        port: Option<u16>,
        /// Numbers in the output match when they differ by at most this times the larger
        /// magnitude (absolute below 1), e.g. 1e-9 for numeric code. Saved with the cases; 0
        /// removes it.
        #[arg(long)]
        float_tolerance: Option<f64>,
        #[arg(long)]
        json: bool,
        /// The command that runs the original, after `--`; paths are relative to the repository.
        /// Taken from the saved cases when omitted.
        #[arg(last = true)]
        run: Vec<String>,
    },
    /// Run a build against `<repo>/rust/app-cases/`, print passed/total, write a report with a
    /// diff per failing case; exits non-zero on any mismatch.
    Replay {
        /// Repository root.
        #[arg(default_value = ".")]
        repo: PathBuf,
        /// Command line of the build to test (the original, the front door, the final binary);
        /// the original when omitted.
        #[arg(long)]
        build: Option<String>,
        /// Seconds before a run is killed.
        #[arg(long)]
        timeout: Option<u64>,
        /// Servers: the port the build always listens on; otherwise it gets a free one through
        /// PORT and `{port}`.
        #[arg(long)]
        port: Option<u16>,
        /// Replay only this case, by the name the report gives it (a library: only this unit).
        /// The fast, repeatable loop for fixing one mismatch.
        #[arg(long)]
        case: Option<String>,
        #[arg(long)]
        json: bool,
    },
    /// Scaffold `<repo>/rust/app`, the Rust binary that owns the CLI's entry point: a clap parser
    /// for the recorded commands and flags, every command running the original until a unit is
    /// wired. Builds it and replays the cases (100% passed, 100% fallback).
    FrontDoor {
        /// Repository root.
        #[arg(default_value = ".")]
        repo: PathBuf,
        /// Only write the files; skip the build and replay.
        #[arg(long)]
        no_check: bool,
        /// Treat the repository as a library even if it has a `main`: the front door is a Rust
        /// crate with the public API (found by default when there is no main and no server).
        #[arg(long)]
        library: bool,
        #[arg(long)]
        json: bool,
    },
    /// Hand commands to a unit's `port.rs` (after it passes gate): adds the crate to `rust/app`,
    /// a glue file to write once, the routing and a kill switch
    /// (`RUST_ANYTHING_UNIT_<NAME>=original`), then builds and replays.
    Wire {
        /// Repository root.
        repo: PathBuf,
        /// Unit crate: its folder, its name under `rust/`, or the unit id it was bridged from.
        unit: String,
        /// A command the unit answers. Repeatable; kept from the last wire when omitted.
        #[arg(long = "command")]
        commands: Vec<String>,
        /// Servers: a route the unit answers, axum syntax (`GET /users/{id}`, `/path` for any
        /// method, `*` for everything else). Repeatable; kept from the last wire when omitted.
        #[arg(long = "route")]
        routes: Vec<String>,
        /// Do not run gate on the unit first (it just passed).
        #[arg(long)]
        skip_gate: bool,
        /// Only write the files; skip the build and replay.
        #[arg(long)]
        no_check: bool,
        #[arg(long)]
        json: bool,
    },
    /// Make `rust/app` a pure Rust app, once replay passes with fallback rate 0 and every unit
    /// passes gate: removes the fallback and the kill switches, turns the units into plain
    /// libraries, runs the lift checklist and replays the pure binary. Checked on a copy first;
    /// the original stays in place.
    Lift {
        /// Repository root.
        #[arg(default_value = ".")]
        repo: PathBuf,
        #[arg(long)]
        json: bool,
    },
}

fn say_replay(replay: &app::ReplayOutcome) {
    say!("{}/{} cases passed", replay.passed, replay.total);
    if let Some(f) = &replay.fallback {
        say!(
            "Fallback rate: {:.1}% ({}/{})",
            f.rate * 100.0,
            f.cases,
            f.total
        );
    }
    for f in replay.failures.iter().take(20) {
        let fields: Vec<&str> = f.problems.iter().map(|p| p.field.as_str()).collect();
        say!("- {}: {}", f.case, fields.join(", "));
    }
    if replay.failures.len() > 20 {
        say!("- ... and {} more", replay.failures.len() - 20);
    }
    say!("Report: {}", replay.report.display());
}

fn main() -> ExitCode {
    match run() {
        Ok(code) => code,
        Err(e) => {
            eprintln!("error: {e:#}");
            ExitCode::from(2)
        }
    }
}

fn run() -> Result<ExitCode> {
    match Cli::parse().command {
        Cmd::Analyze {
            root,
            json,
            out,
            lang,
            whole_app,
        } => {
            let lang = lang.unwrap_or_else(|| rust_anything::lang::Lang::detect(&root));
            let mut report = analyze::analyze_lang(&root, lang)
                .with_context(|| format!("analyzing {}", root.display()))?;
            if whole_app {
                report.whole_app = Some(rust_anything::whole_app::whole_app(&root, &report));
            }
            let text = if json {
                serde_json::to_string_pretty(&report)? + "\n"
            } else {
                let title = root
                    .canonical()
                    .ok()
                    .and_then(|p| p.file_name().map(|n| n.to_string_lossy().into_owned()))
                    .unwrap_or_else(|| root.display().to_string());
                report::markdown(&report, &title)
            };
            match out {
                Some(path) => std::fs::write(&path, text)
                    .with_context(|| format!("writing {}", path.display()))?,
                None => say_raw!("{text}"),
            }
        }
        Cmd::Bridge {
            root,
            unit,
            kind,
            out,
            name,
            force,
            keep,
            batch,
        } => {
            let outcome = bridge::bridge(&bridge::Options {
                root: &root,
                unit: &unit,
                kind: kind.map(|k| match k {
                    KindArg::Napi => bridge::Kind::Napi,
                    KindArg::Wasm => bridge::Kind::Wasm,
                }),
                out,
                name,
                force,
                keep,
                lang: None,
                batch,
            })?;
            say!(
                "Created {} ({:?}) for {}",
                outcome.crate_dir.display(),
                outcome.kind,
                outcome.exports.join(", ")
            );
            for file in &outcome.files {
                say!("  {file}");
            }
            for note in &outcome.notes {
                say!("- {note}");
            }
        }
        Cmd::Record {
            crate_dir,
            count,
            inputs,
            module,
            resolve_from,
            node_args,
            float_tolerance,
        } => {
            if let Some(t) = float_tolerance {
                let mut manifest = bridge::Manifest::load(&crate_dir)?;
                manifest.float_tolerance = (rust_anything::tolerance::check(t)? > 0.0).then_some(t);
                manifest.save(&crate_dir)?;
            }
            let outcome = harness::record(&harness::RecordOptions {
                crate_dir: &crate_dir,
                count,
                inputs: inputs.as_deref(),
                module: module.as_deref(),
                resolve_from: resolve_from.as_deref(),
                node_args: &node_args,
            })?;
            say!(
                "Recorded {} cases ({} threw) in {}",
                outcome.cases,
                outcome.errors,
                outcome.path.display()
            );
            for (name, n) in &outcome.per_function {
                say!("  {name}: {n}");
            }
            if !outcome.regex_functions.is_empty() {
                say!(
                    "Regex traps (non-ASCII \\w, \\b and case folding) added for: {}",
                    outcome.regex_functions.join(", ")
                );
            }
            if !outcome.timed_out.is_empty() {
                say!(
                    "Left out {} input(s) the original ran too long on (over the per-case limit, \
                     RUST_ANYTHING_CASE_TIMEOUT seconds, 10 by default). The original does this \
                     itself, so the port need not match it there:",
                    outcome.timed_out.len()
                );
                for c in &outcome.timed_out {
                    say!("  {}({})", c["fn"].as_str().unwrap_or("?"), c["args"]);
                }
            }
        }
        Cmd::Refresh { crate_dir, batch } => {
            let r = if batch.is_empty() {
                bridge::refresh(&crate_dir)?
            } else {
                bridge::add_batches(&crate_dir, &batch)?
            };
            say!(
                "Regenerated {} for {}",
                r.files.join(", "),
                r.functions.join(", ")
            );
        }
        Cmd::Build { crate_dir } => {
            say!("Built {}", harness::build(&crate_dir)?.display());
        }
        Cmd::Diff {
            crate_dir,
            module,
            ignore_key_order,
            float_tolerance,
            json,
            node_args,
        } => {
            let d = harness::diff(&harness::DiffOptions {
                crate_dir: &crate_dir,
                module: module.as_deref(),
                ignore_key_order,
                node_args: &node_args,
                float_tolerance,
            })?;
            if json {
                say!("{}", serde_json::to_string_pretty(&d)?);
            } else {
                say!("{}/{} cases match", d.passed, d.total);
                if !d.missing.is_empty() {
                    say!("Missing exports: {}", d.missing.join(", "));
                }
                for m in &d.mismatches {
                    say!(
                        "- {}({}): expected {}, got {}",
                        m["fn"].as_str().unwrap_or_default(),
                        m["args"]
                            .as_array()
                            .map(|a| a
                                .iter()
                                .map(ToString::to_string)
                                .collect::<Vec<_>>()
                                .join(", "))
                            .unwrap_or_default(),
                        m["expected"],
                        m["actual"]
                    );
                }
            }
            if d.failed > 0 {
                return Ok(ExitCode::FAILURE);
            }
        }
        Cmd::Bench {
            crate_dir,
            min_ms,
            resolve_from,
            json,
            node_args,
        } => {
            let rows = harness::bench(&harness::BenchOptions {
                crate_dir: &crate_dir,
                min_ms,
                resolve_from: resolve_from.as_deref(),
                node_args: &node_args,
            })?;
            if json {
                say!("{}", serde_json::to_string_pretty(&rows)?);
            } else {
                say!(
                    "{:<24} {:>6} {:>12} {:>12} {:>9}",
                    "function",
                    "cases",
                    "orig ns/call",
                    "Rust ns/call",
                    "speedup"
                );
                for r in &rows {
                    say!(
                        "{:<24} {:>6} {:>12.0} {:>12.0} {:>8.2}x",
                        r.name,
                        r.cases,
                        r.original_ns,
                        r.rust_ns,
                        r.speedup
                    );
                }
                let lang = bridge::Manifest::load(&crate_dir)
                    .map(|m| m.language)
                    .unwrap_or_default();
                let v = harness::verdict(&rows, lang);
                say!(
                    "\nTimed as callers call it, crossing included. Overall {:.2}x (geometric mean); {} of {} functions at least 1.1x faster.",
                    v.speedup,
                    v.faster.len(),
                    rows.len()
                );
                say!("{}", v.advice);
            }
        }
        Cmd::Profile {
            root,
            json,
            lang,
            command,
        } => {
            let report = rust_anything::profile::profile(&root, &command, lang)?;
            if json {
                say!("{}", serde_json::to_string_pretty(&report)?);
            } else {
                say!("{}", rust_anything::profile::markdown(&report));
            }
        }
        Cmd::Shadow { crate_dir, out } => {
            let outcome = shadow::shadow(&crate_dir, out.as_deref())?;
            say!(
                "Wrote {} ({})",
                outcome.path.display(),
                outcome.exports.join(", ")
            );
            if outcome.path.extension().is_some_and(|e| e == "java") {
                say!(
                    "Call the <Class>Shadow classes in place of the originals; compile them with the originals and java/rust-anything. -Drust-anything.mode=old|shadow|new switches at runtime."
                );
            } else if outcome.path.extension().is_some_and(|e| e == "rb") {
                say!(
                    "Require it after the original files. RUST_ANYTHING_MODE=old|shadow|new switches at runtime."
                );
            } else if outcome.path.extension().is_some_and(|e| e == "go") {
                say!(
                    "Import it in place of the original package (require and replace both modules in go.mod). RUST_ANYTHING_MODE=old|shadow|new switches at runtime."
                );
            } else if outcome.path.extension().is_some_and(|e| e == "c") {
                say!(
                    "Compile the original with the -D flags in shadow/flags.txt, build the crate with --features shadow, and link all three."
                );
            } else {
                say!(
                    "Import it in place of the original. RUST_ANYTHING_MODE=old|shadow|new switches at runtime."
                );
            }
        }
        Cmd::Gate {
            crate_dir,
            json,
            node_args,
        } => {
            let outcome = gate::gate(&crate_dir, &node_args)?;
            if json {
                say!("{}", serde_json::to_string_pretty(&outcome)?);
            } else {
                for check in &outcome.checks {
                    let mark = if check.passed { "pass" } else { "FAIL" };
                    let first = check.detail.lines().next().unwrap_or_default();
                    say!("{mark}  {:<10} {first}", check.name);
                    if !check.passed {
                        for line in check.detail.lines().skip(1) {
                            say!("            {line}");
                        }
                    }
                }
                say!(
                    "{}",
                    if outcome.passed {
                        "Gate passed."
                    } else {
                        "Gate failed."
                    }
                );
            }
            if !outcome.passed {
                return Ok(ExitCode::FAILURE);
            }
        }
        Cmd::App { command } => return run_app(command),
        Cmd::Mcp => mcp::serve()?,
        Cmd::Setup {
            agents,
            remove,
            dry_run,
            json,
        } => {
            let home = rust_anything::setup::home()?;
            let command = std::env::current_exe()
                .and_then(|p| p.canonical())
                .context("finding the rust-anything executable")?;
            let report = rust_anything::setup::setup(&rust_anything::setup::Options {
                home: &home,
                agents,
                command,
                remove,
                dry_run,
            })?;
            if json {
                println!("{}", serde_json::to_string_pretty(&report)?);
            } else {
                print!("{}", rust_anything::setup::text(&report));
            }
        }
    }
    Ok(ExitCode::SUCCESS)
}

fn run_app(command: AppCmd) -> Result<ExitCode> {
    match command {
        AppCmd::Record {
            repo,
            inputs,
            normalize,
            name,
            no_traps,
            timeout,
            http,
            port,
            float_tolerance,
            json,
            run,
        } => {
            let outcome = app::record(&app::RecordOptions {
                repo: &repo,
                run: &run,
                inputs: inputs.as_deref(),
                normalize: &normalize,
                name: name.as_deref(),
                traps: !no_traps,
                timeout_secs: timeout,
                http,
                port,
                float_tolerance,
            })?;
            if json {
                say!("{}", serde_json::to_string_pretty(&outcome)?);
                return Ok(ExitCode::SUCCESS);
            }
            say!(
                "Recorded {} cases in {} ({} from examples, {} from inputs, {} traps)",
                outcome.cases,
                outcome.dir.display(),
                outcome.examples,
                outcome.inputs,
                outcome.traps
            );
            for (kind, what) in [
                (
                    app::NoiseKind::Lines,
                    "Lines (a different number of lines between two runs; now `...`, which matches anything: check these first)",
                ),
                (
                    app::NoiseKind::Order,
                    "Order (the same lines in another order between two runs; replay compares them in any order)",
                ),
                (
                    app::NoiseKind::Values,
                    "Values (parts of lines differed between two runs; now `[..]` wildcards)",
                ),
            ] {
                let hits: Vec<String> = outcome
                    .noise
                    .iter()
                    .filter(|n| n.kind == kind)
                    .map(|n| format!("{} ({})", n.case, n.field))
                    .collect();
                if !hits.is_empty() {
                    say!("Noise, {what}: {}", hits.join(", "));
                }
            }
            if !outcome.unstable.is_empty() {
                say!(
                    "Unstable (exit code or files differed between runs; check them): {}",
                    outcome.unstable.join(", ")
                );
            }
            if !outcome.timed_out.is_empty() {
                say!("Timed out, not recorded: {}", outcome.timed_out.join(", "));
            }
        }
        AppCmd::Replay {
            repo,
            build,
            timeout,
            port,
            case,
            json,
        } => {
            let outcome = app::replay(&app::ReplayOptions {
                repo: &repo,
                build: build.as_deref(),
                timeout_secs: timeout,
                port,
                case: case.as_deref(),
            })?;
            if json {
                say!("{}", serde_json::to_string_pretty(&outcome)?);
            } else {
                say_replay(&outcome);
            }
            if !outcome.failures.is_empty() {
                return Ok(ExitCode::FAILURE);
            }
        }
        AppCmd::FrontDoor {
            repo,
            no_check,
            library,
            json,
        } => {
            let outcome = front_door::front_door(&front_door::FrontDoorOptions {
                repo: &repo,
                library,
                check: !no_check,
            })?;
            if json {
                say!("{}", serde_json::to_string_pretty(&outcome)?);
            } else {
                say!(
                    "Created {} ({} `{}`), {}",
                    outcome.dir.display(),
                    if outcome.api.is_empty() {
                        "binary"
                    } else {
                        "crate"
                    },
                    outcome.binary,
                    if !outcome.api.is_empty() {
                        format!(
                            "a library; public API: {}",
                            outcome
                                .api
                                .iter()
                                .map(|a| format!("{} ({} exports)", a.unit, a.exports.len()))
                                .collect::<Vec<_>>()
                                .join(", ")
                        )
                    } else if !outcome.routes.is_empty() {
                        format!("requests recorded: {}", outcome.routes.join(", "))
                    } else if outcome.commands.is_empty() {
                        "commands: none (one root command)".to_string()
                    } else {
                        format!("commands: {}", outcome.commands.join(", "))
                    }
                );
                for file in &outcome.files {
                    say!("  {file}");
                }
                if let Some(replay) = &outcome.replay {
                    say_replay(replay);
                }
                if !outcome.api.is_empty() {
                    say!(
                        "Every export is still only in the original. After a unit's crate (from `bridge`) passes gate: rust-anything app wire {} <unit crate>",
                        repo.display()
                    );
                } else if outcome.routes.is_empty() {
                    say!(
                        "Every command runs the original. After a unit passes gate: rust-anything app wire {} <unit> --command <name>",
                        repo.display()
                    );
                } else {
                    say!(
                        "Every request goes to the original. After a unit passes gate: rust-anything app wire {} <unit> --route 'GET /path/{{param}}'",
                        repo.display()
                    );
                }
            }
            if outcome
                .replay
                .as_ref()
                .is_some_and(|r| !r.failures.is_empty())
            {
                return Ok(ExitCode::FAILURE);
            }
        }
        AppCmd::Wire {
            repo,
            unit,
            commands,
            routes,
            skip_gate,
            no_check,
            json,
        } => {
            let outcome = front_door::wire(&front_door::WireOptions {
                repo: &repo,
                unit: &unit,
                commands: &commands,
                routes: &routes,
                skip_gate,
                check: !no_check,
            })?;
            if json {
                say!("{}", serde_json::to_string_pretty(&outcome)?);
            } else {
                say!(
                    "Wired {} ({}) to {}; {}=original sends it back to the original",
                    outcome.unit,
                    outcome.crate_dir.display(),
                    if outcome.shadow.is_some() {
                        "the public API".to_string()
                    } else if outcome.routes.is_empty() {
                        outcome.commands.join(", ")
                    } else {
                        outcome.routes.join(", ")
                    },
                    outcome.switch
                );
                if let Some(shadow) = &outcome.shadow {
                    say!(
                        "The original package imports {} in place of the unit until the lift.",
                        shadow.display()
                    );
                    if shadow.extension().is_some_and(|e| e == "c") {
                        say!(
                            "C switches at build time: compile without the -D flags in shadow/flags.txt to send it back."
                        );
                    }
                }
                if outcome.glue_pending {
                    say!(
                        "Write the glue in {}, then run this again.",
                        outcome.glue.display()
                    );
                }
                if let Some(replay) = &outcome.replay {
                    say_replay(replay);
                }
            }
            if outcome
                .replay
                .as_ref()
                .is_some_and(|r| !r.failures.is_empty())
            {
                return Ok(ExitCode::FAILURE);
            }
        }
        AppCmd::Lift { repo, json } => {
            let outcome = lift::lift(&lift::LiftOptions { repo: &repo })?;
            if json {
                say!("{}", serde_json::to_string_pretty(&outcome)?);
            } else {
                for item in &outcome.checklist {
                    let mark = match item.mark {
                        lift::Mark::Pass => "pass",
                        lift::Mark::Warn => "warn",
                        lift::Mark::Fail => "FAIL",
                        lift::Mark::Skip => "skip",
                    };
                    let mut lines = item.detail.lines();
                    say!(
                        "{mark}  {:<24} {}",
                        item.name,
                        lines.next().unwrap_or_default()
                    );
                    if item.mark == lift::Mark::Fail {
                        for line in lines {
                            say!("      {line}");
                        }
                    }
                }
                if outcome.lifted {
                    say!(
                        "Lifted: {} is a pure Rust app ({}). Removed {} generated files; the original is untouched.",
                        repo.join(front_door::APP_DIR).display(),
                        outcome
                            .binary
                            .as_ref()
                            .map(|b| b.display().to_string())
                            .unwrap_or_default(),
                        outcome.removed.len()
                    );
                } else {
                    say!("Not lifted:");
                    for b in &outcome.blockers {
                        say!("- {b}");
                    }
                    if let Some(stage) = &outcome.stage {
                        say!("The lifted copy is in {} for a look.", stage.display());
                    }
                }
            }
            if !outcome.lifted {
                return Ok(ExitCode::FAILURE);
            }
        }
    }
    Ok(ExitCode::SUCCESS)
}