rust-anything 2.1.0

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, Java and Kotlin
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
//! What a recording was made from, and the differences from it a port is allowed to keep.
//!
//! `record` saves a fingerprint of the original's unit (`recorded-from.json`): when the original
//! changes later, `diff` and `gate` say the recording is stale instead of passing against old
//! behavior. `known-diffs.jsonl` lists recorded cases where the port is meant to differ, each
//! with the Rust result it gives and why; any other difference still fails, and so does an entry
//! that no longer happens, so the list cannot outlive the differences it explains.

use std::path::Path;

use anyhow::{Context, Result};
use serde_json::{Value, json};

use crate::bridge::Manifest;
use crate::harness::DiffOutcome;

pub const RECORDED_FROM: &str = "recorded-from.json";
pub const KNOWN_DIFFS: &str = "known-diffs.jsonl";

/// FNV-1a over the unit's files (relative path and contents, in path order): stable across Rust
/// versions and platforms, unlike `DefaultHasher`.
pub fn fingerprint(m: &Manifest) -> Option<(String, usize)> {
    let unit = m.root.join(&m.unit);
    let mut files: Vec<std::path::PathBuf> = if unit.is_file() {
        vec![unit.clone()]
    } else if unit.is_dir() {
        crate::walk::filtered(&unit, |e| {
            !matches!(
                e.file_name().to_str(),
                Some("node_modules" | "target" | ".git" | "__pycache__")
            )
        })
        .build()
        .flatten()
        .filter(|e| e.file_type().is_some_and(|t| t.is_file()))
        .map(|e| e.into_path())
        .collect()
    } else {
        return None;
    };
    files.sort();
    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
    let mut eat = |bytes: &[u8]| {
        for b in bytes {
            hash ^= u64::from(*b);
            hash = hash.wrapping_mul(0x0100_0000_01b3);
        }
    };
    for file in &files {
        let relative = file.strip_prefix(&m.root).unwrap_or(file);
        eat(relative.to_string_lossy().replace('\\', "/").as_bytes());
        eat(&[0]);
        eat(&std::fs::read(file).ok()?);
        eat(&[0]);
    }
    Some((format!("{hash:016x}"), files.len()))
}

/// Saves the fingerprint of the original next to `cases.jsonl`, with how much of it the recorded
/// cases ran when that was measured.
pub fn save_fingerprint(crate_dir: &Path, m: &Manifest, coverage: Option<&Value>) -> Result<()> {
    let path = crate_dir.join(RECORDED_FROM);
    let Some((hash, files)) = fingerprint(m) else {
        return Ok(());
    };
    let mut saved = json!({ "unit": m.unit, "files": files, "fingerprint": hash });
    if let Some(c) = coverage {
        saved["coverage"] = c.clone();
    }
    std::fs::write(&path, serde_json::to_string_pretty(&saved)? + "\n")
        .with_context(|| format!("writing {}", path.display()))
}

/// The coverage `record` saved: `{"lines", "reached", "files": [{"file", "lines", "reached",
/// "missed"}]}`.
pub fn saved_coverage(crate_dir: &Path) -> Option<Value> {
    let saved: Value =
        serde_json::from_str(&std::fs::read_to_string(crate_dir.join(RECORDED_FROM)).ok()?).ok()?;
    saved.get("coverage").filter(|c| c.is_object()).cloned()
}

/// One line for a person: how much of the original the recorded cases ran, and where they
/// did not reach.
pub fn coverage_line(c: &Value) -> Option<String> {
    let lines = c["lines"].as_u64()?;
    let reached = c["reached"].as_u64()?;
    if lines == 0 {
        return None;
    }
    let mut files: Vec<&Value> = c["files"]
        .as_array()
        .into_iter()
        .flatten()
        .filter(|f| f["missed"].as_str().is_some_and(|m| !m.is_empty()))
        .collect();
    files.sort_by_key(|f| {
        std::cmp::Reverse(f["lines"].as_u64().unwrap_or(0) - f["reached"].as_u64().unwrap_or(0))
    });
    let mut missed: Vec<String> = files
        .iter()
        .take(4)
        .map(|f| {
            format!(
                "{}: {}",
                f["file"].as_str().unwrap_or("?"),
                f["missed"].as_str().unwrap_or_default()
            )
        })
        .collect();
    if files.len() > 4 {
        missed.push(format!(
            "{} more files, listed in {RECORDED_FROM}",
            files.len() - 4
        ));
    }
    let mut line = format!(
        "the recorded cases ran {reached} of the original's {lines} lines ({:.0}%)",
        reached as f64 * 100.0 / lines as f64
    );
    if !missed.is_empty() {
        line.push_str(&format!(
            "; never ran: {} (add inputs that reach them, e.g. `record --from-tests`)",
            missed.join("; ")
        ));
    }
    Some(line)
}

/// Line coverage of the unit's files from the V8 coverage Node writes to `dir`
/// (`NODE_V8_COVERAGE`). A line counts when it has code; it was reached when any of its code ran
/// in any of the record runs. Files Node transformed (not only stripped of types) are left out,
/// since V8's offsets then point into the transformed text.
pub fn node_coverage(dir: &Path, m: &Manifest) -> Option<Value> {
    let mut unit: Vec<std::path::PathBuf> = m
        .exports
        .iter()
        .map(|f| m.root.join(&f.file))
        .chain(m.classes.iter().map(|c| m.root.join(&c.file)))
        .collect();
    unit.sort();
    unit.dedup();
    let root = m.root.canonicalize().unwrap_or_else(|_| m.root.clone());
    let mut files: Vec<std::path::PathBuf> = crate::gate::with_local_imports(&m.root, unit)
        .into_iter()
        .filter_map(|p| p.canonicalize().ok())
        .collect();
    files.sort();
    files.dedup();
    let reports: Vec<Value> = std::fs::read_dir(dir)
        .ok()?
        .flatten()
        .filter_map(|e| serde_json::from_str(&std::fs::read_to_string(e.path()).ok()?).ok())
        .collect();
    if reports.is_empty() {
        return None;
    }
    let mut out = Vec::new();
    let (mut total, mut total_reached) = (0u64, 0u64);
    for path in files {
        let file = path
            .strip_prefix(&root)
            .unwrap_or(&path)
            .to_string_lossy()
            .replace('\\', "/");
        let Ok(text) = std::fs::read_to_string(&path) else {
            continue;
        };
        let real = &path;
        let units: usize = text.encode_utf16().count();
        let mut ran = vec![false; units];
        let mut seen = false;
        for report in &reports {
            let transformed = report["source-map-cache"]
                .as_object()
                .is_some_and(|c| c.keys().any(|url| same_file(url, real)));
            if transformed {
                continue;
            }
            for script in report["result"].as_array().into_iter().flatten() {
                if !script["url"].as_str().is_some_and(|u| same_file(u, real)) {
                    continue;
                }
                seen = true;
                let mut counts = vec![0u64; units];
                for f in script["functions"].as_array().into_iter().flatten() {
                    for r in f["ranges"].as_array().into_iter().flatten() {
                        let (Some(s), Some(e), Some(n)) = (
                            r["startOffset"].as_u64(),
                            r["endOffset"].as_u64(),
                            r["count"].as_u64(),
                        ) else {
                            continue;
                        };
                        let (s, e) = (s as usize, (e as usize).min(units));
                        if s < e {
                            counts[s..e].fill(n);
                        }
                    }
                }
                for (r, c) in ran.iter_mut().zip(&counts) {
                    *r |= *c > 0;
                }
            }
        }
        if !seen {
            continue;
        }
        let mut lines: Vec<(usize, bool, bool)> = Vec::new();
        let (mut line, mut code, mut hit) = (1usize, false, false);
        let mut at = 0usize;
        for ch in text.chars() {
            if ch == '\n' {
                lines.push((line, code, hit));
                (line, code, hit) = (line + 1, false, false);
            } else if !ch.is_whitespace() {
                code = true;
                hit |= ran.get(at).copied().unwrap_or(false);
            }
            at += ch.len_utf16();
        }
        lines.push((line, code, hit));
        let code_lines: Vec<(usize, bool)> = lines
            .into_iter()
            .filter(|(_, code, _)| *code)
            .map(|(n, _, hit)| (n, hit))
            .collect();
        let reached = code_lines.iter().filter(|(_, hit)| *hit).count() as u64;
        total += code_lines.len() as u64;
        total_reached += reached;
        out.push(json!({
            "file": file,
            "lines": code_lines.len(),
            "reached": reached,
            "missed": spans(code_lines.iter().filter(|(_, hit)| !hit).map(|(n, _)| *n)),
        }));
    }
    (!out.is_empty()).then(|| json!({ "lines": total, "reached": total_reached, "files": out }))
}

fn same_file(url: &str, real: &Path) -> bool {
    url.strip_prefix("file://")
        .map(|p| {
            percent_encoding::percent_decode_str(p)
                .decode_utf8_lossy()
                .into_owned()
        })
        .is_some_and(|p| Path::new(&p) == real)
}

fn spans(lines: impl Iterator<Item = usize>) -> String {
    let mut out: Vec<(usize, usize)> = Vec::new();
    for n in lines {
        match out.last_mut() {
            Some((_, end)) if *end + 1 == n => *end = n,
            _ => out.push((n, n)),
        }
    }
    out.iter()
        .map(|(a, b)| {
            if a == b {
                a.to_string()
            } else {
                format!("{a}-{b}")
            }
        })
        .collect::<Vec<_>>()
        .join(", ")
}

/// Why the recording no longer describes the original, if it does not: the unit's files changed
/// since `record`. `None` when they did not, or when there is nothing to compare (a recording
/// made before fingerprints, or the original not at hand).
pub fn stale(crate_dir: &Path, m: &Manifest) -> Option<String> {
    let saved: Value =
        serde_json::from_str(&std::fs::read_to_string(crate_dir.join(RECORDED_FROM)).ok()?).ok()?;
    let (now, _) = fingerprint(m)?;
    (saved["fingerprint"].as_str()? != now).then(|| {
        format!(
            "{} changed since it was recorded: re-run `rust-anything record {}` so the port is \
             checked against what the original does now",
            m.unit,
            crate_dir.display()
        )
    })
}

const HISTORY: &str = ".rust-anything/diff-history.jsonl";
/// Changes to `port.rs` in a row that gain no matching case before `diff` calls the port stalled.
pub const STALL_AFTER: usize = 3;

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct Trend {
    /// Cases matching after each change to `port.rs`, oldest first (the last ten).
    pub passed: Vec<usize>,
    pub total: usize,
    /// The last `STALL_AFTER` changes gained nothing while cases still fail.
    pub stalled: bool,
}

/// Records this diff when `port.rs` changed since the last one, and returns the trend. Rerunning
/// `diff` on an unchanged port is not another attempt, so it adds nothing.
pub fn track(crate_dir: &Path, passed: usize, total: usize) -> Option<Trend> {
    let port = std::fs::read(crate_dir.join("src/port.rs")).ok()?;
    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
    for b in &port {
        hash ^= u64::from(*b);
        hash = hash.wrapping_mul(0x0100_0000_01b3);
    }
    let hash = format!("{hash:016x}");
    let path = crate_dir.join(HISTORY);
    let mut runs: Vec<Value> = std::fs::read_to_string(&path)
        .unwrap_or_default()
        .lines()
        .filter_map(|l| serde_json::from_str(l).ok())
        .filter(|r: &Value| r["total"].as_u64() == Some(total as u64))
        .collect();
    let entry = json!({ "port": hash, "passed": passed, "total": total });
    match runs.last_mut() {
        Some(last) if last["port"] == entry["port"] => *last = entry,
        _ => runs.push(entry),
    }
    let keep = runs.len().saturating_sub(10);
    runs.drain(..keep);
    if let Some(dir) = path.parent() {
        let _ = std::fs::create_dir_all(dir);
    }
    let text: String = runs.iter().map(|r| format!("{r}\n")).collect();
    let _ = std::fs::write(&path, text);
    Some(trend(
        runs.iter()
            .filter_map(|r| r["passed"].as_u64().map(|p| p as usize))
            .collect(),
        total,
    ))
}

pub fn last_diff(crate_dir: &Path) -> Option<(usize, usize)> {
    let text = std::fs::read_to_string(crate_dir.join(HISTORY)).ok()?;
    let last: Value = serde_json::from_str(text.lines().last()?).ok()?;
    Some((
        usize::try_from(last["passed"].as_u64()?).ok()?,
        usize::try_from(last["total"].as_u64()?).ok()?,
    ))
}

fn trend(passed: Vec<usize>, total: usize) -> Trend {
    let n = passed.len();
    let stalled = n > STALL_AFTER
        && passed[n - 1] < total
        && passed[n - STALL_AFTER..]
            .iter()
            .all(|p| *p <= passed[..n - STALL_AFTER].iter().copied().max().unwrap_or(0));
    Trend {
        passed,
        total,
        stalled,
    }
}

/// The entries of `known-diffs.jsonl`: `{"fn", "args", "actual", "reason"}` per line, `actual`
/// being the port's result as `diff --json` prints it. An entry without a reason is refused.
pub fn known_diffs(crate_dir: &Path) -> Result<Vec<Value>> {
    let path = crate_dir.join(KNOWN_DIFFS);
    let Ok(text) = std::fs::read_to_string(&path) else {
        return Ok(Vec::new());
    };
    let mut entries = Vec::new();
    for (i, line) in text.lines().enumerate() {
        let line = line.trim();
        if line.is_empty() || line.starts_with("//") {
            continue;
        }
        let entry: Value = serde_json::from_str(line)
            .with_context(|| format!("{}:{}: not JSON", path.display(), i + 1))?;
        for key in ["fn", "args", "actual"] {
            anyhow::ensure!(
                entry.get(key).is_some(),
                "{}:{}: an entry needs `{key}`",
                path.display(),
                i + 1
            );
        }
        anyhow::ensure!(
            entry["reason"]
                .as_str()
                .is_some_and(|r| !r.trim().is_empty()),
            "{}:{}: an entry needs a `reason`: why the port is meant to differ here",
            path.display(),
            i + 1
        );
        entries.push(entry);
    }
    Ok(entries)
}

/// Counts the mismatches a known-diffs entry explains as passed (in `accepted`), and records the
/// entries no mismatch matched (in `stale_known`). A mismatch is explained only when the port
/// gives exactly the entry's `actual`: a different wrong result still fails.
pub fn apply_known(mut outcome: DiffOutcome, entries: &[Value]) -> DiffOutcome {
    if entries.is_empty() {
        return outcome;
    }
    let mut used = vec![false; entries.len()];
    outcome.mismatches.retain(|m| {
        let hit = entries.iter().position(|e| {
            e["fn"] == m["fn"] && e["args"] == m["args"] && e["actual"] == m["actual"]
        });
        match hit {
            Some(i) => {
                used[i] = true;
                false
            }
            None => true,
        }
    });
    let accepted = used.iter().filter(|u| **u).count();
    outcome.accepted = accepted;
    outcome.failed -= accepted.min(outcome.failed);
    outcome.passed += accepted;
    outcome.stale_known = entries
        .iter()
        .zip(&used)
        .filter(|(_, u)| !**u)
        .map(|(e, _)| json!({ "fn": e["fn"], "args": e["args"], "reason": e["reason"] }))
        .collect();
    outcome
}

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

    fn outcome() -> DiffOutcome {
        DiffOutcome {
            total: 3,
            passed: 1,
            failed: 2,
            mismatches: vec![
                json!({"fn": "f", "args": [1], "expected": {"ok": 1}, "actual": {"ok": 2}}),
                json!({"fn": "f", "args": [2], "expected": {"ok": 1}, "actual": {"ok": 3}}),
            ],
            ..DiffOutcome::default()
        }
    }

    #[test]
    fn known_diffs_pass_only_with_the_listed_result() {
        let entries = [
            json!({"fn": "f", "args": [1], "actual": {"ok": 2}, "reason": "fixed a bug"}),
            json!({"fn": "f", "args": [2], "actual": {"ok": 4}, "reason": "other result"}),
            json!({"fn": "g", "args": [], "actual": {"ok": 0}, "reason": "gone"}),
        ];
        let out = apply_known(outcome(), &entries);
        assert_eq!((out.passed, out.failed, out.accepted), (2, 1, 1));
        assert_eq!(out.mismatches.len(), 1);
        assert_eq!(out.mismatches[0]["args"], json!([2]));
        assert_eq!(out.stale_known.len(), 2);
    }

    #[test]
    fn a_port_stalls_after_changes_that_gain_nothing() {
        assert!(!trend(vec![10, 12, 15], 20).stalled);
        assert!(!trend(vec![10, 12, 12, 12], 20).stalled);
        assert!(trend(vec![10, 12, 12, 11, 12], 20).stalled);
        assert!(!trend(vec![10, 12, 12, 11, 13], 20).stalled);
        assert!(!trend(vec![20, 20, 20, 20, 20], 20).stalled);
    }

    #[test]
    fn rerunning_diff_on_an_unchanged_port_is_not_an_attempt() {
        let dir = std::env::temp_dir().join(format!("ra-trend-{}", std::process::id()));
        std::fs::create_dir_all(dir.join("src")).unwrap();
        let mut last = None;
        for (port, passed) in [("a", 1), ("a", 2), ("b", 2), ("c", 2), ("d", 2), ("e", 2)] {
            std::fs::write(dir.join("src/port.rs"), port).unwrap();
            last = track(&dir, passed, 5);
        }
        std::fs::remove_dir_all(&dir).unwrap();
        let last = last.unwrap();
        assert_eq!(last.passed, vec![2, 2, 2, 2, 2]);
        assert!(last.stalled);
    }

    #[test]
    fn entries_need_a_reason() {
        let dir = std::env::temp_dir().join(format!("ra-known-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(
            dir.join(KNOWN_DIFFS),
            "// comment\n{\"fn\": \"f\", \"args\": [1], \"actual\": {\"ok\": 2}}\n",
        )
        .unwrap();
        let err = known_diffs(&dir).unwrap_err();
        std::fs::remove_dir_all(&dir).unwrap();
        assert!(format!("{err:#}").contains("reason"), "{err:#}");
    }
}