Skip to content

Don't rewrite it in Rust. Rewrite the 5% that's slow: profile your app, port the hot functions with your coding agent, prove they behave the same. Python, JS/TS, Ruby, Java, Kotlin, Go, PHP, C/C++, C#, R.

40.8 KB
Raw Blame
//! The steps (scan, port, verify, enable, bench, report) shared by the CLI and the MCP server.

use crate::backend::{self, Lang};
use crate::bench::{self, BenchResult};
use crate::hotspot::{self, Hotspot};
use crate::report::{self, ReportOut};
use crate::state::{Ctx, Port, Verify};
use crate::{agent, show, ui, util};
use anyhow::{Context, Result, bail, ensure};
use serde::Serialize;
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Serialize)]
pub struct ScanOut {
    pub command: Vec<String>,
    pub wall: f64,
    pub hotspots: Vec<Hotspot>,
    pub notes: Vec<String>,
    /// Where what the app printed while it was profiled is kept; None when it printed nothing.
    pub app_output: Option<PathBuf>,
    /// Median time of the app without the profiler, when the scan was calibrated.
    pub unprofiled: Option<f64>,
}

impl ScanOut {
    /// How many times slower the profiled run was than the app on its own.
    pub fn overhead(&self) -> Option<f64> {
        self.unprofiled.filter(|&u| u > 0.0 && self.wall > 0.0).map(|u| self.wall / u)
    }
}

/// Runs of the app without the profiler for  scan --calibrate.
pub const CALIBRATION_RUNS: usize = 3;

/// `duration`: stop the app after that many seconds (a GUI or a server that doesn't exit on its own).
pub fn scan(root: &Path, command: Vec<String>, duration: Option<f64>) -> Result<ScanOut> {
    scan_with(root, command, duration, false)
}

/// `calibrate`: also time the app without the profiler, so the profiler's overhead is known (#142).
pub fn scan_with(root: &Path, command: Vec<String>, duration: Option<f64>, calibrate: bool) -> Result<ScanOut> {
    ensure!(
        !command.is_empty(),
        "put the command that runs your app after --, e.g.  fast-anything scan -- python app.py"
    );
    ensure!(
        !(calibrate && duration.is_some()),
        "--calibrate times the app until it exits, so it can't be used with --duration"
    );
    let mut ctx = Ctx::open(root)?;
    let command = match crate::python::unwrap_env_runner(&command, |argv| env_runner_output(&ctx.root, argv))? {
        Some(unwrapped) => {
            ui::step(format!("{} runs {}", ui::bold(command[..2].join(" ")), ui::bold(&unwrapped[0])));
            unwrapped
        }
        None => command,
    };
    let lang = crate::cfamily::refine(Lang::detect(&command[0])?, &command[1..]);
    let lang = crate::kotlin::refine(lang, &command[0], &ctx.root);
    if let Some(d) = duration {
        ensure!(lang.stops_after_duration(), "--duration stops R apps with SIGINT, which Windows doesn't have");
        ensure!(d.is_finite() && d > 0.0, "--duration takes a number of seconds, e.g. --duration 30");
    }
    // a failed scan of an Android project says why: its app runs on ART, not on a JVM (#136)
    let android = (crate::java::runner(&command[0]) == Some(crate::java::Runner::Gradle))
        .then(|| crate::java::android_module(&ctx.root))
        .flatten();
    let explain = |e: anyhow::Error| match &android {
        Some(module) => {
            let module =
                if module.as_os_str().is_empty() { "this project".into() } else { module.display().to_string() };
            e.context(format!(
                "{module} is an Android module, and Android apps run on ART, which this scan can't profile: it scans Java and Kotlin on a desktop JVM ({})",
                crate::device::hint(None)
            ))
        }
        None => e,
    };
    let exe = lang.resolve(&command[0], &command[1..], &ctx.root).map_err(explain)?;
    ctx.state.command = command.clone();
    ui::step(format!("profiling {}", ui::bold(command.join(" "))));
    if let Some(d) = duration.filter(|_| lang != Lang::CSharp) {
        ui::line(format!("  stopping it after {d}s"));
    }
    let app_output = ctx.fast.join("app.log");
    let _ = fs::remove_file(&app_output);
    let profile = lang.profile(&ctx, &exe, &command[1..], duration).map_err(explain)?;
    let app_output = keep_output(&app_output, &profile.output)?;
    let hotspots = hotspot::rank(profile.functions, profile.wall);
    let mut notes = profile.notes;
    notes.extend(attribution_notes(&hotspots));
    ctx.state.lang = lang;
    ctx.state.runtime = Some(exe);
    ctx.state.base = profile.base;
    ctx.state.wall = profile.wall;
    ctx.state.hotspots = hotspots.clone();
    ctx.state.unprofiled = None;
    ctx.save()?;
    let unprofiled = if calibrate {
        ui::step(format!("timing it {CALIBRATION_RUNS} times without the profiler"));
        let u = bench::unprofiled(&ctx, CALIBRATION_RUNS).context("timing the app without the profiler")?;
        ctx.state.unprofiled = Some(u);
        ctx.save()?;
        Some(u)
    } else {
        None
    };
    Ok(ScanOut { command, wall: profile.wall, hotspots, notes, app_output, unprofiled })
}

/// What  scan --explain  finds without running the app (#136).
#[derive(Serialize)]
pub struct Explain {
    pub command: Vec<String>,
    pub lang: Option<Lang>,
    pub runtime: Option<String>,
    /// Why the scan can't run as given; None when it can.
    pub problem: Option<String>,
    /// An Android module in the project, relative to it ("" for the project itself).
    pub android_module: Option<String>,
    pub notes: Vec<String>,
}

/// What a scan of `command` would profile, and what would stop it, without running the app.
pub fn explain(root: &Path, command: &[String]) -> Result<Explain> {
    ensure!(
        !command.is_empty(),
        "put the command that runs your app after --, e.g.  fast-anything scan --explain -- python app.py"
    );
    let root = util::absolute(root);
    let android = crate::java::android_module(&root).map(|m| crate::util::forward_slashes(&m));
    let mut notes = Vec::new();
    if let Some(m) = &android {
        let module = if m.is_empty() { "this project".to_string() } else { m.clone() };
        notes.push(format!(
            "{module} is an Android module: it runs on ART, which the desktop JVM scan can't profile. Android Studio keeps Kotlin in app/src/main/java; the JVM scan needs a desktop run target (Gradle's application plugin, src/main/kotlin)"
        ));
        notes.push(crate::device::hint(None));
    }
    let (lang, runtime, problem) = match Lang::detect(&command[0]) {
        Err(e) => (None, None, Some(format!("{e:#}"))),
        Ok(lang) => {
            let lang = crate::cfamily::refine(lang, &command[1..]);
            let lang = crate::kotlin::refine(lang, &command[0], &root);
            match lang.resolve(&command[0], &command[1..], &root) {
                Ok(exe) => (Some(lang), Some(exe), None),
                Err(e) => (Some(lang), None, Some(format!("{e:#}"))),
            }
        }
    };
    Ok(Explain { command: command.to_vec(), lang, runtime, problem, android_module: android, notes })
}

/// Keep what the app printed, so a successful scan doesn't swallow it (#142).
fn keep_output(path: &Path, printed: &str) -> Result<Option<PathBuf>> {
    if printed.trim().is_empty() {
        return Ok(None);
    }
    fs::write(path, printed).with_context(|| format!("saving the app's output to {}", path.display()))?;
    Ok(Some(path.to_path_buf()))
}

/// What the ranking can't show by itself: no call counts from a sampling profiler, and a run that
/// mostly happens outside the app's own functions (#142).
fn attribution_notes(hotspots: &[Hotspot]) -> Vec<String> {
    let mut notes = Vec::new();
    if hotspots.is_empty() {
        return notes;
    }
    if hotspots.iter().all(|h| h.ncalls == 0) {
        notes.push("this profiler samples the stack instead of counting calls, so there are no call counts".into());
    }
    let top = hotspots.iter().map(|h| h.share).fold(0.0, f64::max);
    if top < 0.5 {
        notes.push(format!(
            "your biggest function takes only {:.0}% of the run: the rest is outside your functions (top-level code, the runtime, libraries, startup), which a port can't speed up",
            top * 100.0
        ));
    }
    notes
}

/// What an env runner (uv, poetry, ...) prints for a command run in the project.
fn env_runner_output(root: &Path, argv: &[&str]) -> Result<String> {
    let out = util::output(std::process::Command::new(argv[0]).args(&argv[1..]).current_dir(root), argv[0])?;
    ensure!(out.status.success(), "{} failed:\n{}", argv.join(" "), util::tail(&util::combined(&out), 10));
    Ok(String::from_utf8_lossy(&out.stdout).into_owned())
}

fn pick<'a>(hotspots: &'a [Hotspot], id: Option<&str>) -> Result<&'a Hotspot> {
    let Some(id) = id else {
        return hotspots
            .iter()
            .find(|h| h.worth_it())
            .context("no function worth porting in the last scan (see  fast-anything status  for why)");
    };
    if let Some(h) = hotspots.iter().find(|h| h.id == id) {
        return Ok(h);
    }
    let hits: Vec<&Hotspot> = hotspots.iter().filter(|h| h.name == id || h.qualname == id).collect();
    match hits.as_slice() {
        [one] => Ok(one),
        [] => bail!("{id} wasn't in the last scan; run  fast-anything scan -- ...  again, or check the name"),
        many => bail!("{id} is ambiguous: {}", many.iter().map(|h| h.id.as_str()).collect::<Vec<_>>().join(", ")),
    }
}

#[derive(Serialize)]
pub struct PortOut {
    pub id: String,
    pub crate_dir: PathBuf,
    pub lib_rs: PathBuf,
    pub task_md: PathBuf,
    pub recorded: usize,
    pub calls_seen: u64,
    pub skipped: usize,
    pub task: String,
}

pub fn port(root: &Path, id: Option<&str>, samples: usize) -> Result<PortOut> {
    let mut ctx = Ctx::open(root)?;
    let exe = ctx.runtime()?.to_string();
    let lang = ctx.state.lang;
    let hs = pick(&ctx.state.hotspots, id)?.clone();
    if !hs.portable() {
        bail!("{} can't be ported yet: {}", hs.id, hs.blockers.join("; "));
    }
    let module = hs.module.clone().unwrap_or_default();
    let crate_rel = format!(".fast/ports/{}", hs.id.replace([':', '/', '\\'], "."));
    let dir = ctx.root.join(&crate_rel);
    fs::create_dir_all(&dir)?;

    ui::step(format!("running the app again to record real calls to {}", ui::bold(&hs.id)));
    let app_args = ctx.state.command[1..].to_vec();
    let rec = lang.record(&ctx, &exe, &app_args, &hs, &dir.join("recordings.jsonl"), samples.max(1))?;
    if rec.mutates {
        bail!(
            "{} changes its arguments in place (seen while recording). A Rust port can only return a value, so it would drop those changes. Have the function return the new value instead, then scan again.",
            hs.id
        );
    }
    if rec.kept == 0 {
        let types =
            if rec.unsupported.is_empty() { String::new() } else { format!(" (got: {})", rec.unsupported.join(", ")) };
        bail!(
            "recorded no usable calls to {} out of {}. Arguments and results must be plain data: numbers, strings, bytes, lists, tuples, dicts, sets{types}",
            hs.id,
            rec.calls
        );
    }
    let enabled = ctx.state.ports.get(&hs.id).is_some_and(|p| p.enabled);
    let helpers: Vec<Hotspot> = hs
        .calls
        .iter()
        .filter_map(|name| hotspot::helpers_named(&ctx.state.hotspots, &hs, name).first().copied())
        .cloned()
        .collect();
    let text = fs::read_to_string(ctx.root.join(&hs.file)).unwrap_or_default();
    let sources: Vec<&str> = std::iter::once(&hs).chain(&helpers).map(|h| h.source.as_str()).collect();
    let port = Port {
        id: hs.id.clone(),
        file: hs.file.clone(),
        module,
        func: hs.name.clone(),
        ext: format!("fa_{}", util::slug(&hs.id)),
        crate_dir: crate_rel,
        recorded: rec.kept,
        calls_seen: rec.calls,
        verify: None,
        enabled,
        lang,
        owner: hs.owner.clone(),
        line: hs.line,
        sources: crate::state::verbatim(&text, &sources),
    };
    backend::hold_out(&dir.join("recordings.jsonl"), &ctx.holdout(&port))?;
    let task = lang.scaffold(&ctx, &hs, &helpers, &port)?;
    ctx.state.ports.insert(port.id.clone(), port);
    ctx.save()?;
    Ok(PortOut {
        id: hs.id,
        lib_rs: dir.join("src").join(if lang.c_bridge() { "port.rs" } else { "lib.rs" }),
        task_md: dir.join("TASK.md"),
        crate_dir: dir,
        recorded: rec.kept,
        calls_seen: rec.calls,
        skipped: rec.skipped,
        task,
    })
}

pub fn verify(root: &Path, id: Option<&str>) -> Result<(String, Verify)> {
    let mut ctx = Ctx::open(root)?;
    let id = ctx.resolve_port(id)?;
    let port = ctx.state.ports[&id].clone();
    let source_hash = backend::source_hash(&ctx.crate_dir(&port))?;
    if ctx.stale(&port) {
        ui::warn(format!("{} changed since {id} was recorded: {}", port.file, stale_hint(&id)));
    }
    ui::step(format!("building {} (release)", ui::bold(&port.ext)));
    let v = match backend::build(&ctx, &port)? {
        Err(log) => Verify { built: false, build_log: Some(log), ..Verify::default() },
        Ok(_) => {
            ui::step(format!(
                "replaying {} recorded call{} against the Rust port",
                port.recorded,
                if port.recorded == 1 { "" } else { "s" }
            ));
            let visible = util::count_lines(&ctx.crate_dir(&port).join("recordings.jsonl"));
            let mut v = port.lang.verify(&ctx, &port)?;
            backend::finish_replay(&ctx, &port, visible, &mut v)?;
            v
        }
    };
    let mut v = v;
    v.lang = port.lang;
    v.source_hash = Some(source_hash);
    if !v.ok() && port.enabled {
        port.lang.disable(&ctx, &port)?;
        if let Some(p) = ctx.state.ports.get_mut(&id) {
            p.enabled = false;
        }
        ui::warn(format!("{id} was enabled; switched it back to {} until it passes again", port.lang.label()));
    }
    if let Some(p) = ctx.state.ports.get_mut(&id) {
        p.verify = Some(v.clone());
    }
    ctx.save()?;
    Ok((id, v))
}

/// Verify every port; an enabled one that fails is switched back, as with a single verify.
pub fn verify_all(root: &Path) -> Result<Vec<(String, Verify)>> {
    let ids: Vec<String> = Ctx::open(root)?.state.ports.keys().cloned().collect();
    ensure!(!ids.is_empty(), "nothing ported yet: run  fast-anything port");
    let mut results = Vec::with_capacity(ids.len());
    for id in &ids {
        let (id, v) = verify(root, Some(id))?;
        if !ui::quiet() {
            show::verify(&id, &v);
        }
        results.push((id, v));
    }
    Ok(results)
}

/// What to do about a port whose original changed after it was recorded.
pub fn stale_hint(id: &str) -> String {
    format!(
        "its recorded calls and Rust may be out of date. Run  fast-anything port {id}  to record again, update the Rust, then verify (or --force)"
    )
}

/// What to do about a port that's right but slower than the original.
pub fn slower_hint() -> &'static str {
    "calling into Rust costs more than this function's work, so it won't be switched on. Port its caller instead (fewer, bigger calls), or  enable --force"
}

/// Why a port too cheap to time (see Verify::too_cheap) isn't switched on.
pub fn too_cheap_hint(id: &str, v: &Verify) -> String {
    let m = v.micro.unwrap_or_default();
    format!(
        "{id} takes about as long as reading the clock ({} per call in {}, the clock {}), so verify can't tell whether Rust saves anything: {}",
        util::nanos(m.original_ns),
        v.lang.label(),
        util::nanos(m.clock_ns),
        slower_hint()
    )
}

/// Whether the crate is not exactly the one covered by the last verify. Old state without a hash
/// keeps its legacy behavior; re-porting clears verify and therefore always needs a new one.
fn crate_needs_verify(ctx: &Ctx, port: &Port) -> Result<bool> {
    let Some(verify) = &port.verify else { return Ok(true) };
    let Some(expected) = verify.source_hash.as_deref() else { return Ok(false) };
    Ok(backend::source_hash(&ctx.crate_dir(port))? != expected)
}

pub fn enable(root: &Path, id: Option<&str>, force: bool) -> Result<String> {
    let mut ctx = Ctx::open(root)?;
    let id = ctx.resolve_port(id)?;
    let port = ctx.state.ports[&id].clone();
    if ctx.stale(&port) && !force {
        bail!("{} changed since {id} was recorded: {}", port.file, stale_hint(&id));
    }
    let verified = port.verify.as_ref().is_some_and(Verify::ok);
    if !verified && !force {
        bail!("{id} hasn't passed verify yet: run  fast-anything verify {id}  (or --force)");
    }
    if !force && crate_needs_verify(&ctx, &port)? {
        bail!("{id}'s Rust changed since it passed verify: run  fast-anything verify {id}  (or --force)");
    }
    // too cheap to time comes first: a few-ns function measures 1.0x either way, so "slower" is noise
    if let Some(v) = port.verify.as_ref().filter(|v| v.too_cheap() && !force) {
        bail!("{}", too_cheap_hint(&id, v));
    }
    if let Some(m) = port.verify.as_ref().filter(|v| v.slower() && !force).and_then(|v| v.micro) {
        bail!("{id} is {} slower per call in Rust: {}", util::times(1.0 / m.speedup), slower_hint());
    }
    port.lang.enable(&ctx, &port)?;
    if let Some(p) = ctx.state.ports.get_mut(&id) {
        p.enabled = true;
    }
    ctx.save()?;
    Ok(id)
}

fn markers_for_port(port: &Port) -> Vec<(String, String)> {
    let (comment, ids): (&str, Vec<String>) = match port.lang {
        Lang::Python | Lang::Ruby | Lang::R => ("#", vec![port.id.clone()]),
        Lang::Php | Lang::C | Lang::Cpp => ("//", vec![port.id.clone(), format!("{} bridge", port.id)]),
        _ => ("//", vec![port.id.clone()]),
    };
    ids.iter().map(|id| backend::markers(comment, id)).collect()
}

pub fn disable(root: &Path, id: Option<&str>) -> Result<String> {
    let mut ctx = Ctx::open(root)?;
    let id = ctx.resolve_port(id)?;
    let port = &ctx.state.ports[&id];
    let file = ctx.root.join(&port.file);
    let before = fs::read_to_string(&file).with_context(|| format!("reading {}", port.file))?;
    for (begin, end) in markers_for_port(port) {
        ensure!(
            before.contains(&begin) == before.contains(&end),
            "the fast-anything block for {id} is incomplete; restore its begin/end markers before disabling"
        );
    }
    port.lang.disable(&ctx, port)?;
    let after = fs::read_to_string(&file).with_context(|| format!("reading {}", port.file))?;
    for (begin, end) in markers_for_port(port) {
        ensure!(!after.contains(&begin) && !after.contains(&end), "the fast-anything block for {id} wasn't removed");
    }
    if let Some(p) = ctx.state.ports.get_mut(&id) {
        p.enabled = false;
    }
    ctx.save()?;
    Ok(id)
}

/// Switch every enabled port back to the original code; returns their ids.
pub fn disable_all(root: &Path) -> Result<Vec<String>> {
    let ids: Vec<String> = Ctx::open(root)?.state.ports.values().filter(|p| p.enabled).map(|p| p.id.clone()).collect();
    for id in &ids {
        disable(root, Some(id))?;
    }
    Ok(ids)
}

pub fn build_all(root: &Path) -> Result<()> {
    let ctx = Ctx::open(root)?;
    ensure!(!ctx.state.ports.is_empty(), "nothing ported yet");
    let mut failed = 0;
    for port in ctx.state.ports.values() {
        if port.enabled && crate_needs_verify(&ctx, port)? {
            failed += 1;
            ui::error(format!(
                "{} is switched on and its Rust hasn't passed verify in its current state; run  fast-anything verify {}",
                port.id, port.id
            ));
            continue;
        }
        ui::step(format!("building {}", ui::bold(&port.id)));
        match backend::build(&ctx, port)? {
            Ok(path) => ui::ok(format!("{}", path.display())),
            Err(log) => {
                failed += 1;
                ui::error(format!("{} doesn't compile:\n{log}", port.id));
            }
        }
    }
    ensure!(failed == 0, "{failed} port(s) didn't build");
    Ok(())
}

/// Remove the build cache (.fast/target) or reset the entire .fast directory.
pub fn clean(root: &Path, all: bool) -> Result<()> {
    let fast_dir = root.join(".fast");
    if !fast_dir.exists() {
        ui::ok("nothing to clean (.fast folder does not exist)");
        return Ok(());
    }

    if all {
        let disabled = disable_all(root)?;
        for id in disabled {
            ui::ok(format!("{} is back to the original code", ui::bold(&id)));
        }
        if fast_dir.exists() {
            fs::remove_dir_all(&fast_dir).with_context(|| format!("removing {}", fast_dir.display()))?;
        }
        ui::ok("removed .fast and reset all state");
    } else {
        let target_dir = fast_dir.join("target");
        if target_dir.exists() {
            fs::remove_dir_all(&target_dir).with_context(|| format!("removing {}", target_dir.display()))?;
            ui::ok("removed .fast/target build cache");
        } else {
            ui::ok(".fast/target build cache is already clean");
        }
    }
    Ok(())
}

pub fn bench(root: &Path, runs: usize) -> Result<BenchResult> {
    let mut ctx = Ctx::open(root)?;
    for p in ctx.state.ports.values().filter(|p| p.enabled && ctx.stale(p)) {
        ui::warn(format!("{} changed since {} was recorded: {}", p.file, p.id, stale_hint(&p.id)));
    }
    let result = bench::run(&ctx, runs.max(1))?;
    ctx.state.bench = Some(result.clone());
    ctx.save()?;
    Ok(result)
}

pub fn report(root: &Path, include_android: bool) -> Result<ReportOut> {
    report::write(&Ctx::open(root)?, include_android)
}

/// What the agent gets told after a failed attempt, or a right one that's slower than the original.
pub fn feedback(v: &Verify) -> String {
    if v.ok()
        && let Some(m) = v.micro.filter(|_| v.slower())
    {
        return format!(
            "\n\n## Your last attempt is right but slow\n\n\
             All {} cases match, but a call takes {} in Rust against {} in the original ({} slower).\n\
             Make it faster without changing any result: borrow inputs instead of converting them, \
             no allocation or cloning per call, `with_capacity`, fast paths for common cases.\n{}",
            v.total,
            util::nanos(m.rust_ns),
            util::nanos(m.original_ns),
            util::times(1.0 / m.speedup),
            if v.lang.c_bridge() { "\nFix src/port.rs.\n" } else { "\nFix src/lib.rs.\n" }
        );
    }
    let mut out = String::from("\n\n## Your last attempt failed\n\n");
    if let Some(log) = &v.build_log {
        out.push_str("It doesn't compile:\n\n```\n");
        out.push_str(log);
        out.push_str("\n```\n");
    } else {
        out.push_str(&format!("{} of {} recorded calls came out different:\n\n", v.failed, v.total));
        for f in &v.failures {
            out.push_str(&format!("- `{}` should give `{}`, the port gave `{}`\n", f.call, f.expected, f.got));
        }
        if v.failures.iter().any(|f| f.call.starts_with("(held out) ")) {
            out.push_str(
                "\nCalls marked (held out) aren't in recordings.jsonl: port the logic, not the calls you were shown.\n",
            );
        }
    }
    out.push_str(if v.lang.c_bridge() { "\nFix src/port.rs.\n" } else { "\nFix src/lib.rs.\n" });
    out
}

pub fn agent_port(root: &Path, id: &str, agent_spec: &str, attempts: usize) -> Result<Verify> {
    let attempts = attempts.max(1);
    let mut last: Option<Verify> = None;
    // A port that's right but slower goes back to the agent to speed up; if no later attempt is both
    // right and faster, its sources are put back. One too cheap to time can't be shown faster.
    let mut right: Option<(Verify, Snapshot)> = None;
    for attempt in 1..=attempts {
        let ctx = Ctx::open(root)?;
        let port = ctx.state.ports.get(id).with_context(|| format!("{id} isn't ported"))?.clone();
        let dir = ctx.crate_dir(&port);
        let mut prompt = fs::read_to_string(dir.join("TASK.md")).context("reading TASK.md")?;
        if let Some(v) = &last {
            prompt.push_str(&feedback(v));
        }
        ui::step(format!("{} is writing the Rust port (attempt {attempt}/{attempts})", ui::bold(agent_spec)));
        agent::run(agent_spec, &dir, &prompt, &dir.join("agent.log"))?;
        let (_, v) = verify(root, Some(id))?;
        if !ui::quiet() {
            show::verify(id, &v);
        }
        if v.ok() && (!v.slower() || v.too_cheap()) {
            return Ok(v);
        }
        if v.ok() && right.as_ref().is_none_or(|(best, _)| speedup(&v) > speedup(best)) {
            right = Some((v.clone(), snapshot(&dir)?));
        }
        last = Some(v);
    }
    if let Some((best, files)) = right
        && last.as_ref().is_none_or(|v| !v.ok() || speedup(v) < speedup(&best))
    {
        ui::step(format!("putting back the right attempt of {id} ({} per call)", util::times(speedup(&best))));
        let ctx = Ctx::open(root)?;
        let port = ctx.state.ports.get(id).with_context(|| format!("{id} isn't ported"))?;
        restore_snapshot(&ctx.crate_dir(port), &files)?;
        let (_, v) = verify(root, Some(id))?;
        return Ok(v);
    }
    Ok(last.unwrap_or_default())
}

fn speedup(v: &Verify) -> f64 {
    v.micro.map(|m| m.speedup).unwrap_or(0.0)
}

/// Cargo inputs for one attempt; recordings, TASK.md and agent.log are deliberately excluded.
type Snapshot = Vec<(PathBuf, Vec<u8>)>;

fn snapshot_files(dir: &Path, out: &mut Snapshot) -> Result<()> {
    for entry in fs::read_dir(dir).with_context(|| format!("reading {}", dir.display()))? {
        let path = entry?.path();
        if path.is_dir() {
            snapshot_files(&path, out)?;
        } else {
            out.push((path.clone(), fs::read(&path)?));
        }
    }
    Ok(())
}

fn snapshot(dir: &Path) -> Result<Snapshot> {
    let mut out = vec![];
    for name in ["Cargo.toml", "Cargo.lock", "build.rs"] {
        let path = dir.join(name);
        if path.is_file() {
            out.push((path.clone(), fs::read(path)?));
        }
    }
    let src = dir.join("src");
    if src.is_dir() {
        snapshot_files(&src, &mut out)?;
    }
    Ok(out)
}

fn restore_snapshot(dir: &Path, files: &Snapshot) -> Result<()> {
    for name in ["Cargo.lock", "build.rs"] {
        let path = dir.join(name);
        if !files.iter().any(|(saved, _)| saved == &path) {
            match fs::remove_file(&path) {
                Ok(()) => {}
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
                Err(e) => return Err(e).with_context(|| format!("removing {}", path.display())),
            }
        }
    }
    let src = dir.join("src");
    if src.exists() {
        fs::remove_dir_all(&src)?;
    }
    for (path, bytes) in files {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }
        fs::write(path, bytes)?;
    }
    Ok(())
}

pub fn go(
    root: &Path,
    command: Vec<String>,
    agent_spec: Option<&str>,
    top: usize,
    attempts: usize,
    runs: usize,
    samples: usize,
) -> Result<()> {
    let scanned = scan(root, command, None)?;
    show::scan(&scanned);
    let picks: Vec<String> =
        scanned.hotspots.iter().filter(|h| h.worth_it()).take(top.max(1)).map(|h| h.id.clone()).collect();
    ensure!(
        !picks.is_empty(),
        "nothing worth porting: no portable function takes {}% or more of the run",
        hotspot::WORTH_IT * 100.0
    );

    let Some(agent_spec) = agent_spec else {
        for id in &picks {
            show::port(&port(root, Some(id), samples)?, false);
        }
        ui::line("");
        ui::line(format!(
            "  Now write the Rust: hand each TASK.md to your coding agent (or pass {}), then:",
            ui::bold("--agent claude")
        ));
        for id in &picks {
            show::next(&format!("fast-anything verify {id} && fast-anything enable {id}"));
        }
        show::next("fast-anything bench");
        return Ok(());
    };

    let mut enabled = 0;
    for id in &picks {
        show::port(&port(root, Some(id), samples)?, false);
        let v = agent_port(root, id, agent_spec, attempts)?;
        if v.ok() && v.too_cheap() {
            ui::warn(too_cheap_hint(id, &v));
        } else if v.ok() && v.slower() {
            ui::warn(format!("{id}: right, but slower in Rust per call; {}", slower_hint()));
        } else if v.ok() {
            enable(root, Some(id), false)?;
            ui::ok(format!("{} now runs in Rust", ui::bold(id)));
            enabled += 1;
        } else {
            ui::warn(format!(
                "{id}: not right after {attempts} attempts; fix src/lib.rs by hand (see TASK.md), then  fast-anything verify {id}"
            ));
        }
    }
    ensure!(enabled > 0, "no port both passed verification and ran faster");
    show::bench(&bench(root, runs)?);
    show::report(&report(root, false)?);
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::state::Micro;

    #[test]
    fn scan_notes_say_what_the_ranking_cant() {
        let h = |share: f64, ncalls: u64| Hotspot { share, ncalls, ..Hotspot::default() };
        assert!(attribution_notes(&[]).is_empty());
        assert!(attribution_notes(&[h(0.99, 1), h(0.6, 1_000)]).is_empty(), "cProfile counts calls");
        let sampled = attribution_notes(&[h(0.84, 0), h(0.83, 0)]);
        assert_eq!(sampled.len(), 1);
        assert!(sampled[0].contains("no call counts"), "{sampled:?}");
        let low = attribution_notes(&[h(0.203, 0), h(0.1, 0)]);
        assert!(low.len() == 2 && low[1].starts_with("your biggest function takes only 20% of the run"), "{low:?}");
    }

    #[test]
    fn explain_names_an_android_module_without_running_anything() {
        let dir = std::env::temp_dir().join(format!("fa-explain-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(dir.join("app/src/main")).unwrap();
        fs::write(dir.join("app/build.gradle.kts"), r#"plugins { id("com.android.application") }"#).unwrap();
        let e = explain(&dir, &["./gradlew".into(), "run".into()]).unwrap();
        assert_eq!(e.android_module.as_deref(), Some("app"));
        assert!(e.notes[0].contains("app/src/main/java") && e.notes[1].contains("scan --android"), "{:?}", e.notes);
        let unknown = explain(&dir, &["nosuchtool-fa".into()]).unwrap();
        assert!(unknown.lang.is_none() && unknown.problem.is_some());
        assert!(!dir.join(".fast").exists(), "explain writes nothing");
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn the_apps_output_is_kept_when_it_printed_something() {
        let dir = std::env::temp_dir().join(format!("fa-app-log-{}", std::process::id()));
        fs::create_dir_all(&dir).unwrap();
        let log = dir.join("app.log");
        assert_eq!(keep_output(&log, "  \n").unwrap(), None);
        assert!(!log.exists());
        assert_eq!(keep_output(&log, "done: 42\n").unwrap(), Some(log.clone()));
        assert_eq!(fs::read_to_string(&log).unwrap(), "done: 42\n");
        let _ = fs::remove_dir_all(dir);
    }

    #[test]
    fn slow_but_right_gets_asked_for_speed() {
        let v = Verify {
            built: true,
            total: 10,
            passed: 10,
            micro: Some(Micro { original_ns: 1000.0, rust_ns: 4000.0, speedup: 0.25, clock_ns: 20.0 }),
            ..Verify::default()
        };
        let text = feedback(&v);
        assert!(
            text.contains("right but slow") && text.contains("All 10 cases match") && text.contains("4.0× slower"),
            "{text}"
        );
        let wrong = Verify { failed: 1, ..v };
        assert!(feedback(&wrong).contains("failed"));
    }

    #[test]
    fn feedback_never_shows_a_held_out_call() {
        let failure = |case: usize, value: &str| crate::state::Failure {
            case,
            call: format!("f({value}, \"secret-{value}\")"),
            expected: format!("{value}.25"),
            got: format!("{value}.5"),
        };
        let mut v = Verify {
            built: true,
            total: 600,
            held_out: 100,
            failed: 2,
            failures: vec![failure(7, "31"), failure(450, "8675309")],
            ..Verify::default()
        };
        backend::redact_held_out(&mut v, "f", 400);
        let text = feedback(&v);
        assert!(text.contains("`(held out) f(…)`") && text.contains("port the logic"), "{text}");
        assert!(!text.contains("8675309"), "{text}");
        assert!(text.contains("f(31, \"secret-31\")") && text.contains("31.25"), "{text}");
    }

    fn project(name: &str) -> (PathBuf, Port) {
        let root = std::env::temp_dir().join(format!("fa-pipeline-{name}-{}", std::process::id()));
        let _ = fs::remove_dir_all(&root);
        let crate_dir = ".fast/ports/demo";
        fs::create_dir_all(root.join(crate_dir).join("src")).unwrap();
        fs::write(root.join(crate_dir).join("Cargo.toml"), "[package]\nname = \"demo\"\nversion = \"0.1.0\"\n")
            .unwrap();
        fs::write(root.join(crate_dir).join("src/lib.rs"), "pub fn f() {}\n").unwrap();
        fs::write(root.join("app.py"), "def f():\n    pass\n").unwrap();
        let port = Port {
            id: "demo".into(),
            file: "app.py".into(),
            module: "app".into(),
            func: "f".into(),
            ext: "fa_demo".into(),
            crate_dir: crate_dir.into(),
            recorded: 1,
            calls_seen: 1,
            verify: None,
            enabled: false,
            lang: Lang::Python,
            owner: None,
            line: 1,
            sources: vec![],
        };
        (root, port)
    }

    #[test]
    fn enable_requires_the_crate_that_passed_verify() {
        let (root, mut port) = project("enable-hash");
        let hash = backend::source_hash(&root.join(&port.crate_dir)).unwrap();
        fs::write(root.join(&port.crate_dir).join("src/lib.rs"), "pub fn f() { panic!() }\n").unwrap();
        port.verify = Some(Verify { built: true, total: 1, passed: 1, source_hash: Some(hash), ..Verify::default() });
        let mut ctx = Ctx::open(&root).unwrap();
        ctx.state.ports.insert(port.id.clone(), port);
        ctx.save().unwrap();

        let err = enable(&root, Some("demo"), false).unwrap_err();
        assert!(err.to_string().contains("Rust changed since it passed verify"), "{err}");
        assert!(!Ctx::open(&root).unwrap().state.ports["demo"].enabled);
        let _ = fs::remove_dir_all(root);
    }

    #[test]
    fn live_build_guard_covers_missing_changed_and_legacy_verify() {
        let (root, mut port) = project("build-hash");
        let ctx = Ctx::open(&root).unwrap();
        assert!(crate_needs_verify(&ctx, &port).unwrap());

        port.verify = Some(Verify::default());
        assert!(!crate_needs_verify(&ctx, &port).unwrap(), "legacy verify without a hash keeps working");

        let hash = backend::source_hash(&root.join(&port.crate_dir)).unwrap();
        port.verify = Some(Verify { source_hash: Some(hash), ..Verify::default() });
        assert!(!crate_needs_verify(&ctx, &port).unwrap());
        fs::write(root.join(&port.crate_dir).join("Cargo.toml"), "not a valid manifest\n").unwrap();
        assert!(crate_needs_verify(&ctx, &port).unwrap());
        let _ = fs::remove_dir_all(root);
    }

    #[test]
    fn snapshot_restores_crate_inputs_without_touching_run_artifacts() {
        let dir = std::env::temp_dir().join(format!("fa-agent-snapshot-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(dir.join("src/nested")).unwrap();
        fs::write(dir.join("Cargo.toml"), "best manifest\n").unwrap();
        fs::write(dir.join("Cargo.lock"), "best lock\n").unwrap();
        fs::write(dir.join("src/lib.rs"), "best lib\n").unwrap();
        fs::write(dir.join("src/nested/helper.rs"), "best helper\n").unwrap();
        for name in ["TASK.md", "recordings.jsonl", "agent.log"] {
            fs::write(dir.join(name), "before\n").unwrap();
        }
        let saved = snapshot(&dir).unwrap();

        fs::write(dir.join("Cargo.toml"), "later manifest\n").unwrap();
        fs::write(dir.join("Cargo.lock"), "later lock\n").unwrap();
        fs::write(dir.join("src/lib.rs"), "later lib\n").unwrap();
        fs::write(dir.join("src/extra.rs"), "later extra\n").unwrap();
        fs::write(dir.join("build.rs"), "later build script\n").unwrap();
        fs::remove_file(dir.join("src/nested/helper.rs")).unwrap();
        for name in ["TASK.md", "recordings.jsonl", "agent.log"] {
            fs::write(dir.join(name), "later evidence\n").unwrap();
        }

        restore_snapshot(&dir, &saved).unwrap();
        assert_eq!(fs::read_to_string(dir.join("Cargo.toml")).unwrap(), "best manifest\n");
        assert_eq!(fs::read_to_string(dir.join("Cargo.lock")).unwrap(), "best lock\n");
        assert_eq!(fs::read_to_string(dir.join("src/lib.rs")).unwrap(), "best lib\n");
        assert_eq!(fs::read_to_string(dir.join("src/nested/helper.rs")).unwrap(), "best helper\n");
        assert!(!dir.join("src/extra.rs").exists());
        assert!(!dir.join("build.rs").exists());
        for name in ["TASK.md", "recordings.jsonl", "agent.log"] {
            assert_eq!(fs::read_to_string(dir.join(name)).unwrap(), "later evidence\n");
        }
        let _ = fs::remove_dir_all(dir);
    }

    fn enabled_project(name: &str, text: &str) -> (PathBuf, String) {
        let root = std::env::temp_dir().join(format!("fa-disable-{name}-{}", std::process::id()));
        let _ = fs::remove_dir_all(&root);
        fs::create_dir_all(&root).unwrap();
        fs::write(root.join("app.py"), text).unwrap();
        let port = Port {
            id: "demo".into(),
            file: "app.py".into(),
            module: "app".into(),
            func: "f".into(),
            ext: "fa_demo".into(),
            crate_dir: ".fast/ports/demo".into(),
            recorded: 1,
            calls_seen: 1,
            verify: None,
            enabled: true,
            lang: Lang::Python,
            owner: None,
            line: 1,
            sources: vec![],
        };
        let id = port.id.clone();
        let mut ctx = Ctx::open(&root).unwrap();
        ctx.state.ports.insert(port.id.clone(), port);
        ctx.save().unwrap();
        (root, id)
    }

    #[test]
    fn disable_rejects_damaged_blocks_without_changing_state() {
        let (begin, end) = backend::markers("#", "demo");
        for (name, text) in [("missing-end", format!("{begin}\nf = rust\n")), ("missing-begin", format!("{end}\n"))] {
            let (root, id) = enabled_project(name, &text);
            let err = disable(&root, Some(&id)).unwrap_err();
            assert!(err.to_string().contains("incomplete"), "{err}");
            assert_eq!(fs::read_to_string(root.join("app.py")).unwrap(), text);
            assert!(Ctx::open(&root).unwrap().state.ports[&id].enabled);
            let _ = fs::remove_dir_all(root);
        }
    }

    #[test]
    fn disable_removes_a_complete_block() {
        let (begin, end) = backend::markers("#", "demo");
        let original = "def f():\n    pass\n";
        let text = format!("{original}\n{begin}\nf = rust\n{end}\n");
        let (root, id) = enabled_project("complete", &text);
        disable(&root, Some(&id)).unwrap();
        assert_eq!(fs::read_to_string(root.join("app.py")).unwrap(), original);
        assert!(!Ctx::open(&root).unwrap().state.ports[&id].enabled);
        let _ = fs::remove_dir_all(root);
    }

    #[test]
    fn clean_removes_target_cache() {
        let root = std::env::temp_dir().join(format!("fa-clean-cache-{}", std::process::id()));
        let _ = fs::remove_dir_all(&root);
        let fast_dir = root.join(".fast");
        let target_dir = fast_dir.join("target/release");
        fs::create_dir_all(&target_dir).unwrap();
        fs::write(target_dir.join("artifact"), "bytes").unwrap();
        fs::write(fast_dir.join("state.json"), "{}").unwrap();

        assert!(target_dir.exists());
        clean(&root, false).unwrap();
        assert!(!target_dir.exists());
        assert!(fast_dir.exists());

        // Calling clean again when target is already gone is a harmless no-op
        clean(&root, false).unwrap();
        assert!(!target_dir.exists());

        let _ = fs::remove_dir_all(root);
    }

    #[test]
    fn clean_all_disables_ports_and_removes_fast_dir() {
        let (begin, end) = backend::markers("#", "demo");
        let original = "def f():\n    pass\n";
        let text = format!("{original}\n{begin}\nf = rust\n{end}\n");
        let (root, _id) = enabled_project("clean-all", &text);
        let target_dir = root.join(".fast/target");
        fs::create_dir_all(&target_dir).unwrap();
        fs::write(target_dir.join("dummy.o"), "dummy").unwrap();

        clean(&root, true).unwrap();
        assert_eq!(fs::read_to_string(root.join("app.py")).unwrap(), original);
        assert!(!root.join(".fast").exists());

        // Calling clean on a non-existent .fast folder is a harmless no-op
        clean(&root, true).unwrap();
        assert!(!root.join(".fast").exists());

        let _ = fs::remove_dir_all(root);
    }

    #[test]
    fn clean_all_aborts_if_disable_fails_preserving_fast_dir() {
        let (begin, _end) = backend::markers("#", "demo");
        let text = format!("{begin}\nf = rust\n");
        let (root, id) = enabled_project("damaged-clean", &text);

        let res = clean(&root, true);
        assert!(res.is_err(), "clean --all must abort when disable fails");
        assert!(root.join(".fast").exists(), ".fast must not be deleted if disable fails");
        assert!(Ctx::open(&root).unwrap().state.ports[&id].enabled);

        let _ = fs::remove_dir_all(root);
    }
}