diff --git a/RUST-TOPS.md b/RUST-TOPS.md new file mode 100644 index 0000000..16c439a --- /dev/null +++ b/RUST-TOPS.md @@ -0,0 +1,90 @@ +# Rust-TOPS + +Rust-TOPS is a testing and quality protocol for Rust crates. Each crate +states its class, its test layers and their thresholds in a +`rust-tops.yaml` beside its `Cargo.toml`, and `cargo-tops` checks that file +and runs the enabled layers in a fixed order. Protocol and tooling: +. + +## Versions + +- **Protocol:** rust-tops 1.1.0, spec + `https://github.com/Hedronite/rust-tops/blob/v0.2.0/RUST_TOPS.md`, as each + crate's `protocol` block states it. +- **Gate:** run with cargo-tops 0.2.0, cargo-llvm-cov 0.9.1, cargo-crap + 0.5.0 and cargo-mutants 27.1.0. cargo-tops 0.2.0 enforces no coverage or + CRAP threshold, so the grade comes from the measured cargo-llvm-cov, + cargo-crap and cargo-mutants figures, not from the gate's exit. + +## Crates and thresholds + +Coverage is line, region and per-file line, in percent. CRAP is the +production and agent threshold. Mutation is the kill rate on the diff, in +percent, and the most unexplained survivors. + +| Crate | Class | Line | Region | Per-file | CRAP | Mutation | +|---|---|---|---|---|---|---| +| `phantom-fractal` | binary-cli | 80 | 75 | 90 | 6 / 6 | 85, 0 | +| `phantom-fractal-proto` | library-core | 95 | 90 | 90 | 6 / 6 | 95, 0 | +| `phantom-fractal-lang` | library-core | 95 | 90 | 90 | 6 / 6 | 95, 0 | +| `phantom-fractal-lang-rust` | library-core | 95 | 90 | 90 | 6 / 6 | 95, 0 | +| `phantom-fractal-extract` | library-core | 95 | 90 | 90 | 6 / 6 | 95, 0 | +| `phantom-fractal-analyze` | library-core | 95 | 90 | 90 | 6 / 6 | 95, 0 | +| `phantom-fractal-store` | library-core | 95 | 90 | 90 | 6 / 6 | 95, 0 | +| `phantom-fractal-git` | library-core | 95 | 90 | 90 | 6 / 6 | 95, 0 | + +Each crate's yaml governs. Coverage, CRAP and mutation are on in every +crate. + +## Running the gate + +From a crate's directory: + +```sh +cargo-tops check # validate rust-tops.yaml against the protocol schema +cargo-tops gate # fmt, clippy, deny, nextest, coverage, CRAP, mutation, advisories +``` + +Call the `cargo-tops` binary directly, not `cargo tops`. Cargo passes +`tops` as the first argument to a subcommand binary, and cargo-tops 0.2.0 +does not strip it, so `cargo tops gate` fails with +`unrecognized subcommand 'tops'`. + +An exit of 0 is not a pass. The coverage layer writes +`target/coverage/lcov.info` and the CRAP layer writes `target/crap.json`; +neither applies a threshold. The figures are read from the cargo-llvm-cov +summary, `target/crap.json` and the cargo-mutants report, and compared +with the crate's yaml. Mutation runs on the diff against `origin/main`. + +## The CRAP rule + +CRAP is CC² × (1 − coverage)³ + CC, so it is never below a function's +cyclomatic complexity. + +1. Every production function added or changed since `61236dc` has CRAP at + or below its crate's threshold. +2. No existing function's CRAP rises above its value at `61236dc`. +3. The per-function list at `61236dc` (187 production functions over 6), + kept with the project's quality measurements, is a burn-down list and + is not graded. It moves to `target/crap-baseline.json` once cargo-crap + supports a baseline file. +4. A new or changed exhaustive enum dispatch match (one arm per variant, + no guards, each arm one call or expression) that cannot reach the + threshold without a split made only for CRAP takes an `exemptions` + entry in its crate's yaml, with layer `crap`, a reason and an expiry. + +No function is split only to lower its CRAP. + +## Exemptions in force + +None. + +## Known deviations + +- **Fuzz and property runs.** A std-only fuzz harness and map property + run, driven by a seeded splitmix64 generator, stand in for cargo-fuzz + and proptest. They add no package and need no nightly toolchain. +- **No CI quality job.** Coverage, CRAP and mutation run on the build box, + not in CI. +- **CLI crate CRAP.** `phantom-fractal` holds CRAP 6, tighter than the + binary-cli class default of 8. diff --git a/crates/phantom-fractal-analyze/rust-tops.yaml b/crates/phantom-fractal-analyze/rust-tops.yaml index f0349ae..07dfe51 100644 --- a/crates/phantom-fractal-analyze/rust-tops.yaml +++ b/crates/phantom-fractal-analyze/rust-tops.yaml @@ -1,7 +1,6 @@ # Rust-TOPS 1.1.0 instance for phantom-fractal-analyze (library-core). -# Scaffold slice: laws, coverage, CRAP, and mutation stay off until the -# crate has code they can measure. - +# Layers: lint, unit and security-advisory stay on; coverage, CRAP and +# mutation turn on with the thresholds the R1 ruling keeps. protocol: id: rust-tops version: "1.1.0" @@ -31,9 +30,9 @@ layers: - { id: acceptance, enabled: false, tier: 1, runner: "assert_cmd journeys", scope: tests } - { id: snapshot, enabled: false, tier: 2, runner: insta, scope: tests } - { id: compile-fail, enabled: false, tier: 2, runner: trybuild, scope: tests } - - { id: coverage, enabled: false, tier: 0, runner: cargo-llvm-cov, scope: lib } - - { id: crap, enabled: false, tier: 0, runner: cargo-crap, scope: lib } - - { id: mutation, enabled: false, tier: 1, runner: cargo-mutants, scope: diff } + - { id: coverage, enabled: true, tier: 0, runner: cargo-llvm-cov, scope: lib } + - { id: crap, enabled: true, tier: 0, runner: cargo-crap, scope: lib } + - { id: mutation, enabled: true, tier: 1, runner: cargo-mutants, scope: diff } - { id: property, enabled: false, tier: 1, runner: proptest, scope: lib } - { id: fuzz, enabled: false, tier: 3, runner: cargo-fuzz, scope: lib } - { id: concurrency, enabled: false, tier: 3, runner: loom, scope: lib } diff --git a/crates/phantom-fractal-analyze/tests/fixture.rs b/crates/phantom-fractal-analyze/tests/fixture.rs index 2d8b7e1..fe71783 100644 --- a/crates/phantom-fractal-analyze/tests/fixture.rs +++ b/crates/phantom-fractal-analyze/tests/fixture.rs @@ -4,6 +4,7 @@ use std::fs; use std::path::{Path, PathBuf}; use std::process; +use std::sync::atomic::{AtomicU32, Ordering}; use std::time::{SystemTime, UNIX_EPOCH}; use phantom_fractal_analyze::{Hit, Impact, ImpactOptions, impact}; @@ -19,12 +20,16 @@ struct TempDir(PathBuf); impl TempDir { fn new() -> Self { + static NEXT: AtomicU32 = AtomicU32::new(0); let nanos = SystemTime::now() .duration_since(UNIX_EPOCH) .map(|dur| dur.as_nanos()) .unwrap_or(0); - let path = - std::env::temp_dir().join(format!("phantom-fractal-analyze-{}-{nanos}", process::id())); + let path = std::env::temp_dir().join(format!( + "phantom-fractal-analyze-{}-{nanos}-{}", + process::id(), + NEXT.fetch_add(1, Ordering::Relaxed) + )); fs::create_dir_all(&path).expect("temp dir"); Self(path) } diff --git a/crates/phantom-fractal-analyze/tests/map.rs b/crates/phantom-fractal-analyze/tests/map.rs index 5263579..c684b1c 100644 --- a/crates/phantom-fractal-analyze/tests/map.rs +++ b/crates/phantom-fractal-analyze/tests/map.rs @@ -8,6 +8,7 @@ use std::fs; use std::io::ErrorKind; use std::path::{Path, PathBuf}; use std::process; +use std::sync::atomic::{AtomicU32, Ordering}; use std::time::{SystemTime, UNIX_EPOCH}; use phantom_fractal_analyze::{ @@ -26,18 +27,22 @@ const DETAILS: [(Detail, &str); 3] = [ (Detail::Full, "full"), ]; -/// A fresh directory under the system temp dir, removed on drop. +/// A fresh directory under the system temp dir, removed on drop. Its name +/// carries the process id, the time and a per-process count, so tests +/// that share one process, as libtest runs them, never share a path. struct TempDir(PathBuf); impl TempDir { fn new(tag: &str) -> Self { + static NEXT: AtomicU32 = AtomicU32::new(0); let nanos = SystemTime::now() .duration_since(UNIX_EPOCH) .map(|dur| dur.as_nanos()) .unwrap_or(0); let path = std::env::temp_dir().join(format!( - "phantom-fractal-analyze-map-{tag}-{}-{nanos}", - process::id() + "phantom-fractal-analyze-map-{tag}-{}-{nanos}-{}", + process::id(), + NEXT.fetch_add(1, Ordering::Relaxed) )); fs::create_dir_all(&path).expect("temp dir"); Self(path) diff --git a/crates/phantom-fractal-analyze/tests/map_git.rs b/crates/phantom-fractal-analyze/tests/map_git.rs index 37ab536..a1b6c4f 100644 --- a/crates/phantom-fractal-analyze/tests/map_git.rs +++ b/crates/phantom-fractal-analyze/tests/map_git.rs @@ -26,6 +26,7 @@ use std::fs; use std::io::Write as _; use std::path::{Path, PathBuf}; use std::process::{self, Command, Stdio}; +use std::sync::atomic::{AtomicU32, Ordering}; use std::time::{SystemTime, UNIX_EPOCH}; use phantom_fractal_analyze::{Detail, MapRequest, estimate_tokens, packing_budget, render}; @@ -52,13 +53,15 @@ struct TempDir(PathBuf); impl TempDir { fn new(tag: &str) -> Self { + static NEXT: AtomicU32 = AtomicU32::new(0); let nanos = SystemTime::now() .duration_since(UNIX_EPOCH) .map(|dur| dur.as_nanos()) .unwrap_or(0); let path = std::env::temp_dir().join(format!( - "phantom-fractal-analyze-map-git-{tag}-{}-{nanos}", - process::id() + "phantom-fractal-analyze-map-git-{tag}-{}-{nanos}-{}", + process::id(), + NEXT.fetch_add(1, Ordering::Relaxed) )); fs::create_dir_all(path.join("repo")).expect("temp dir"); fs::create_dir_all(path.join("home")).expect("temp home"); @@ -81,6 +84,11 @@ fn git(dir: &Path, home: &Path, args: &[&str], stdin: Option<&[u8]>) { .env("HOME", home) .env("GIT_CONFIG_NOSYSTEM", "1") .env("GIT_CONFIG_GLOBAL", "/dev/null") + .env("XDG_CONFIG_HOME", home) + .env("GIT_AUTHOR_NAME", "Test") + .env("GIT_AUTHOR_EMAIL", "test@example.invalid") + .env("GIT_COMMITTER_NAME", "Test") + .env("GIT_COMMITTER_EMAIL", "test@example.invalid") .env("LC_ALL", "C") .args([ "-c", diff --git a/crates/phantom-fractal-analyze/tests/rank_store.rs b/crates/phantom-fractal-analyze/tests/rank_store.rs index 433071a..3ac9d7d 100644 --- a/crates/phantom-fractal-analyze/tests/rank_store.rs +++ b/crates/phantom-fractal-analyze/tests/rank_store.rs @@ -6,6 +6,7 @@ use std::fs; use std::io::ErrorKind; use std::path::{Path, PathBuf}; use std::process; +use std::sync::atomic::{AtomicU32, Ordering}; use std::time::{SystemTime, UNIX_EPOCH}; use phantom_fractal_analyze::{EdgeKind, RankSource, personalised, rank}; @@ -21,13 +22,15 @@ struct TempDir(PathBuf); impl TempDir { fn new(tag: &str) -> Self { + static NEXT: AtomicU32 = AtomicU32::new(0); let nanos = SystemTime::now() .duration_since(UNIX_EPOCH) .map(|dur| dur.as_nanos()) .unwrap_or(0); let path = std::env::temp_dir().join(format!( - "phantom-fractal-analyze-{tag}-{}-{nanos}", - process::id() + "phantom-fractal-analyze-{tag}-{}-{nanos}-{}", + process::id(), + NEXT.fetch_add(1, Ordering::Relaxed) )); fs::create_dir_all(&path).expect("temp dir"); Self(path) diff --git a/crates/phantom-fractal-extract/rust-tops.yaml b/crates/phantom-fractal-extract/rust-tops.yaml index a807d83..8d69df6 100644 --- a/crates/phantom-fractal-extract/rust-tops.yaml +++ b/crates/phantom-fractal-extract/rust-tops.yaml @@ -1,7 +1,6 @@ # Rust-TOPS 1.1.0 instance for phantom-fractal-extract (library-core). -# Scaffold slice: laws, coverage, CRAP, and mutation stay off until the -# crate has code they can measure. - +# Layers: lint, unit and security-advisory stay on; coverage, CRAP and +# mutation turn on with the thresholds the R1 ruling keeps. protocol: id: rust-tops version: "1.1.0" @@ -31,9 +30,9 @@ layers: - { id: acceptance, enabled: false, tier: 1, runner: "assert_cmd journeys", scope: tests } - { id: snapshot, enabled: false, tier: 2, runner: insta, scope: tests } - { id: compile-fail, enabled: false, tier: 2, runner: trybuild, scope: tests } - - { id: coverage, enabled: false, tier: 0, runner: cargo-llvm-cov, scope: lib } - - { id: crap, enabled: false, tier: 0, runner: cargo-crap, scope: lib } - - { id: mutation, enabled: false, tier: 1, runner: cargo-mutants, scope: diff } + - { id: coverage, enabled: true, tier: 0, runner: cargo-llvm-cov, scope: lib } + - { id: crap, enabled: true, tier: 0, runner: cargo-crap, scope: lib } + - { id: mutation, enabled: true, tier: 1, runner: cargo-mutants, scope: diff } - { id: property, enabled: false, tier: 1, runner: proptest, scope: lib } - { id: fuzz, enabled: false, tier: 3, runner: cargo-fuzz, scope: lib } - { id: concurrency, enabled: false, tier: 3, runner: loom, scope: lib } diff --git a/crates/phantom-fractal-extract/tests/diff.rs b/crates/phantom-fractal-extract/tests/diff.rs index 487b881..180fa0a 100644 --- a/crates/phantom-fractal-extract/tests/diff.rs +++ b/crates/phantom-fractal-extract/tests/diff.rs @@ -93,6 +93,11 @@ impl Repo { .env("HOME", self.home.path()) .env("GIT_CONFIG_NOSYSTEM", "1") .env("GIT_CONFIG_GLOBAL", "/dev/null") + .env("XDG_CONFIG_HOME", self.home.path()) + .env("GIT_AUTHOR_NAME", "Test") + .env("GIT_AUTHOR_EMAIL", "test@example.invalid") + .env("GIT_COMMITTER_NAME", "Test") + .env("GIT_COMMITTER_EMAIL", "test@example.invalid") .env("LC_ALL", "C") .args([ "-c", @@ -505,3 +510,33 @@ fn common_snapshot(roots: &[&Path]) -> Vec<(PathBuf, u64, std::time::SystemTime, out.sort(); out } + +#[test] +fn a_target_beside_a_nested_manifest_is_pruned_and_one_elsewhere_is_not() { + // SPEC-w9 G2a: a base file under `target` counts only when that + // `target` is neither at the root nor beside a `Cargo.toml`. + let repo = Repo::new(); + repo.commit( + "main", + T0 + 60, + &[ + ("crates/tool/Cargo.toml", MANIFEST), + ("crates/tool/target/built.rs", "pub fn built() {}\n"), + ("src/target/kept.rs", "pub fn kept() {}\n"), + ], + ); + repo.git(&["reset", "-q", "--hard", "main"], None); + fs::remove_file(repo.root().join("crates/tool/target/built.rs")).expect("delete built"); + fs::remove_file(repo.root().join("src/target/kept.rs")).expect("delete kept"); + let found = repo.diff(&Base::Head); + let keys: Vec<(&str, FileChange)> = found + .changed + .iter() + .map(|symbol| (symbol.key.as_str(), symbol.change)) + .collect(); + assert_eq!( + keys, + [("src/target/kept.rs#kept#function#0", FileChange::Removed)] + ); + assert!(found.files_only.is_empty(), "{:?}", found.files_only); +} diff --git a/crates/phantom-fractal-extract/tests/errors.rs b/crates/phantom-fractal-extract/tests/errors.rs new file mode 100644 index 0000000..eca7f60 --- /dev/null +++ b/crates/phantom-fractal-extract/tests/errors.rs @@ -0,0 +1,137 @@ +//! How `index`, `probe` and `diff` fail, and how their errors read. +//! +//! An unreadable source file fails an index as I/O. A repository whose +//! HEAD cannot be read fails a probe as git. A missing root fails a diff +//! as I/O, and an unknown base fails it with the git error's own text. + +mod common; + +use std::error::Error as _; +use std::fs; +use std::os::unix::fs::PermissionsExt as _; +use std::path::Path; +use std::process::{Command, Stdio}; + +use common::{TempDir, write}; +use phantom_fractal_extract::{DiffError, IndexError, diff, index, probe}; +use phantom_fractal_git::{Base, ChangesError}; +use phantom_fractal_lang::ExtractError; +use phantom_fractal_lang_rust::RustLanguage; +use phantom_fractal_store::{Store, StoreError}; + +const MANIFEST: &[u8] = b"[package]\nname = \"app\"\nversion = \"0.1.0\"\nedition = \"2024\"\n"; + +fn store(dir: &TempDir) -> Store { + Store::open(&dir.path().join("graph.db")).expect("open store") +} + +fn rust() -> RustLanguage { + RustLanguage::new().expect("the query compiles") +} + +/// Sets the mode of `path`. +fn chmod(path: &Path, mode: u32) { + fs::set_permissions(path, fs::Permissions::from_mode(mode)).expect("chmod"); +} + +#[test] +fn an_unreadable_source_file_fails_the_index_as_io() { + let root = TempDir::new("errors-unreadable"); + write(root.path(), "Cargo.toml", MANIFEST); + write(root.path(), "src/lib.rs", b"pub fn f() {}\n"); + let locked = root.path().join("src/lib.rs"); + chmod(&locked, 0o000); + if fs::read(&locked).is_ok() { + // A superuser reads it anyway; there is nothing to fail. + chmod(&locked, 0o644); + return; + } + let db = TempDir::new("errors-unreadable-db"); + let got = index(root.path(), &mut store(&db), &rust()); + chmod(&locked, 0o644); + let Err(err @ IndexError::Io(_)) = got else { + panic!("{got:?}"); + }; + assert!(err.to_string().starts_with("walk failed: "), "{err}"); + assert!(err.source().is_some()); +} + +/// Runs `git` in `dir` with no system or global config. +fn git(dir: &Path, home: &Path, args: &[&str]) { + let status = Command::new("git") + .current_dir(dir) + .env_clear() + .env("PATH", std::env::var_os("PATH").unwrap_or_default()) + .env("HOME", home) + .env("GIT_CONFIG_NOSYSTEM", "1") + .env("GIT_CONFIG_GLOBAL", "/dev/null") + .env("XDG_CONFIG_HOME", home) + .env("GIT_AUTHOR_NAME", "Test") + .env("GIT_AUTHOR_EMAIL", "test@example.invalid") + .env("GIT_COMMITTER_NAME", "Test") + .env("GIT_COMMITTER_EMAIL", "test@example.invalid") + .args(args) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .expect("run git"); + assert!(status.success(), "git {args:?}"); +} + +#[test] +fn a_repository_whose_head_cannot_be_read_fails_the_probe_as_git() { + let root = TempDir::new("errors-head"); + let home = TempDir::new("errors-head-home"); + write(root.path(), "Cargo.toml", MANIFEST); + write(root.path(), "src/lib.rs", b"pub fn f() {}\n"); + git(root.path(), home.path(), &["init", "-q", "-b", "main"]); + // HEAD names a branch whose ref holds no object id. + write(root.path(), ".git/refs/heads/main", b"not an object id\n"); + let db = TempDir::new("errors-head-db"); + let got = probe(root.path(), &store(&db)); + let Err(err @ IndexError::Git(_)) = got else { + panic!("{got:?}"); + }; + let IndexError::Git(inner) = &err else { + unreachable!(); + }; + assert_eq!(err.to_string(), inner.to_string()); + assert!(err.source().is_some()); +} + +#[test] +fn a_store_error_reads_as_store_failed() { + let err = IndexError::from(StoreError::MissingProducer); + assert!(matches!( + err, + IndexError::Store(StoreError::MissingProducer) + )); + assert_eq!( + err.to_string(), + format!("store failed: {}", StoreError::MissingProducer) + ); + assert!(err.source().is_some()); +} + +#[test] +fn a_diff_of_a_missing_root_fails_as_io() { + let dir = TempDir::new("errors-diff-missing"); + let got = diff(&dir.path().join("gone"), &Base::Head, &rust()); + let Err(err @ DiffError::Io(_)) = got else { + panic!("{got:?}"); + }; + assert!(err.to_string().starts_with("walk failed: "), "{err}"); +} + +#[test] +fn an_unknown_base_reads_as_the_git_error() { + let err = DiffError::from(ChangesError::UnknownBase); + assert_eq!(err.to_string(), ChangesError::UnknownBase.to_string()); +} + +#[test] +fn an_extractor_refusal_reads_as_extract() { + let err = DiffError::from(ExtractError::Parse); + assert!(matches!(err, DiffError::Extract(ExtractError::Parse))); + assert_eq!(err.to_string(), format!("extract: {}", ExtractError::Parse)); +} diff --git a/crates/phantom-fractal-extract/tests/fresh.rs b/crates/phantom-fractal-extract/tests/fresh.rs index 780a2b7..a24330d 100644 --- a/crates/phantom-fractal-extract/tests/fresh.rs +++ b/crates/phantom-fractal-extract/tests/fresh.rs @@ -436,6 +436,11 @@ fn git(root: &Path, home: &Path, args: &[&str], stdin: Option<&[u8]>) { .env("HOME", home) .env("GIT_CONFIG_NOSYSTEM", "1") .env("GIT_CONFIG_GLOBAL", "/dev/null") + .env("XDG_CONFIG_HOME", home) + .env("GIT_AUTHOR_NAME", "Test") + .env("GIT_AUTHOR_EMAIL", "test@example.invalid") + .env("GIT_COMMITTER_NAME", "Test") + .env("GIT_COMMITTER_EMAIL", "test@example.invalid") .env("LC_ALL", "C") .args([ "-c", diff --git a/crates/phantom-fractal-extract/tests/scip/foreign.rs b/crates/phantom-fractal-extract/tests/scip/foreign.rs index 8f4a638..449d85a 100644 --- a/crates/phantom-fractal-extract/tests/scip/foreign.rs +++ b/crates/phantom-fractal-extract/tests/scip/foreign.rs @@ -54,16 +54,45 @@ impl Members { /// True when the document sits inside some member's directory. A /// member at the workspace root reads as an empty directory and - /// claims everything. + /// claims everything under it. pub fn in_scope(&self, document: &str) -> bool { - self.dirs.iter().any(|dir| { - dir.is_empty() || document == dir.as_str() || document.starts_with(&format!("{dir}/")) - }) + self.dirs.iter().any(|dir| under(dir, document)) + } + + /// True when the document is in scope under the W13 R7 rule: the + /// longest directory boundary that covers it must be a member's. + /// A document under a nested package directory that is not a + /// workspace member (its own `Cargo.toml`, such as a vendored + /// crate) stays out of scope even when a member prefix — say the + /// workspace-root package's empty one — covers it. + pub fn in_scope_with_packages(&self, document: &str, packages: &BTreeSet) -> bool { + let member = self + .dirs + .iter() + .filter(|dir| under(dir, document)) + .map(|dir| dir.len()) + .max(); + let Some(member) = member else { + return false; + }; + let nested = packages + .iter() + .filter(|dir| !self.dirs.contains(*dir)) + .filter(|dir| under(dir, document)) + .map(|dir| dir.len()) + .max(); + !matches!(nested, Some(nested) if nested > member) } } /// Every string value that starts right after `key`, honoring the two /// escapes JSON writes for paths (`\\` and `\"`). +/// True when `document` is the directory `dir` itself or a path under +/// it; an empty directory covers everything. +fn under(dir: &str, document: &str) -> bool { + dir.is_empty() || document == dir || document.starts_with(&format!("{dir}/")) +} + fn string_values(bytes: &[u8], key: &[u8]) -> Vec { let mut out = Vec::new(); let mut rest = bytes; diff --git a/crates/phantom-fractal-extract/tests/scip/score.rs b/crates/phantom-fractal-extract/tests/scip/score.rs index d35a3fc..547f3f7 100644 --- a/crates/phantom-fractal-extract/tests/scip/score.rs +++ b/crates/phantom-fractal-extract/tests/scip/score.rs @@ -109,7 +109,16 @@ pub fn score_with( store: &phantom_fractal_store::Store, members: Option<&Members>, ) -> Result { - let in_scope = |document: &str| members.is_none_or(|list| list.in_scope(document)); + let package_dirs = match members { + Some(_) => store + .package_dirs() + .map_err(|err| format!("store: {err}"))?, + None => BTreeSet::new(), + }; + let in_scope = |document: &str| match members { + Some(list) => list.in_scope_with_packages(document, &package_dirs), + None => true, + }; let all = store .symbol_spans() .map_err(|err| format!("store: {err}"))?; @@ -236,11 +245,14 @@ pub fn score_with( let edges: Vec<&EdgeRow> = if members.is_some() { let mut kept = Vec::new(); for edge in &edges_all { - let in_scope = matches!( - (by_id.get(&edge.src_id), by_id.get(&edge.dst_id)), - (Some(a), Some(b)) if in_scope(a.path.as_str()) && in_scope(b.path.as_str()) - ); - if in_scope { + // The edge belongs to the document its call sits in: a + // caller outside every member leaves with its document, + // while a call into a foreign document stays and reads + // uncovered (W13 D4, R7). + let keep = by_id + .get(&edge.src_id) + .is_some_and(|span| in_scope(span.path.as_str())); + if keep { kept.push(edge); } else { out_of_scope_edges += 1; diff --git a/crates/phantom-fractal-extract/tests/scip/w13_cases.rs b/crates/phantom-fractal-extract/tests/scip/w13_cases.rs new file mode 100644 index 0000000..8268777 --- /dev/null +++ b/crates/phantom-fractal-extract/tests/scip/w13_cases.rs @@ -0,0 +1,291 @@ +//! The hand-built bucket cases: a store written by hand and scored +//! through the report. Moved whole from the scorer's test file; no +//! logic, name or output changed. +use super::*; + +pub fn each_d4_bucket_classifies_its_hand_built_refs() { + use scip::score::w12_bucket; + + let reference = + |kind: &str, text: &str, receiver: Option<&str>, hint: Option<&str>| ScoreCallRef { + kind: kind.to_owned(), + text: text.to_owned(), + receiver: receiver.map(|text| text.to_owned()), + receiver_type: hint.map(|hint| hint.to_owned()), + scope: String::new(), + placed: false, + }; + let one_ref = |kind: &str, receiver: Option<&str>, hint: Option<&str>| { + vec![reference(kind, "f", receiver, hint)] + }; + + assert_eq!( + w12_bucket(&one_ref("macro_arg", None, None), "f", false), + "macro_arg path shape" + ); + assert_eq!( + w12_bucket(&one_ref("macro_arg", Some("x"), None), "f", false), + "macro_arg method shape" + ); + assert_eq!( + w12_bucket(&one_ref("macro", None, None), "f", false), + "macro invocation" + ); + // A macro_arg ref outranks the invocation that carries it. + let both = vec![ + reference("macro_arg", "f", Some("x"), None), + reference("macro", "f", None, None), + ]; + assert_eq!(w12_bucket(&both, "f", false), "macro_arg method shape"); + let both = vec![ + reference("macro_arg", "f", None, None), + reference("macro", "f", None, None), + ]; + assert_eq!(w12_bucket(&both, "f", false), "macro_arg path shape"); + + assert_eq!( + w12_bucket(&one_ref("call", None, None), "f", false), + "unresolved path call" + ); + assert_eq!( + w12_bucket(&one_ref("method_call", Some("x"), None), "f", true), + "trait-method callee" + ); + for (hint, bucket) in [ + ("type:T", "identifier with hint type"), + ("ctor:T::f", "identifier with hint ctor"), + ("ctor?:T::f", "identifier with hint ctor?"), + ("ret:f", "identifier with hint ret"), + ("ret?:f", "identifier with hint ret?"), + ] { + assert_eq!( + w12_bucket(&one_ref("method_call", Some("x"), Some(hint)), "f", false), + bucket, + "{hint}" + ); + } + assert_eq!( + w12_bucket( + &one_ref("method_call", Some("x"), Some("bogus:z")), + "f", + false + ), + "other" + ); + assert_eq!( + w12_bucket(&one_ref("method_call", Some("self"), None), "f", false), + "identifier with no hint" + ); + assert_eq!( + w12_bucket(&one_ref("method_call", Some("self.f"), None), "f", false), + "self.field" + ); + for receiver in ["make()", "a.b", "self.f.g()"] { + assert_eq!( + w12_bucket(&one_ref("method_call", Some(receiver), None), "f", false), + "call or chained receiver", + "{receiver}" + ); + } + for receiver in ["v[0]", ""] { + assert_eq!( + w12_bucket(&one_ref("method_call", Some(receiver), None), "f", false), + "other", + "{receiver}" + ); + } + let empty: Vec = Vec::new(); + assert_eq!(w12_bucket(&empty, "f", false), "other"); + + // The precedence with several same-named refs in one caller: the + // hint kind, then the unhinted identifier, self.field, the call or + // chained receiver, and the catch-all. + let mixed = vec![ + reference("method_call", "f", Some("make()"), None), + reference("method_call", "f", Some("self.f"), None), + reference("method_call", "f", Some("n2"), None), + reference("method_call", "f", Some("x"), Some("type:T")), + ]; + assert_eq!(w12_bucket(&mixed, "f", false), "identifier with hint type"); + let mixed = vec![ + reference("method_call", "f", Some("make()"), None), + reference("method_call", "f", Some("self.f"), None), + reference("method_call", "f", Some("n2"), None), + ]; + assert_eq!(w12_bucket(&mixed, "f", false), "identifier with no hint"); + let mixed = vec![ + reference("method_call", "f", Some("make()"), None), + reference("method_call", "f", Some("self.f"), None), + ]; + assert_eq!(w12_bucket(&mixed, "f", false), "self.field"); +} + +pub fn each_w13_bucket_classifies_its_hand_built_refs() { + use scip::foreign::{Binding, MissContext}; + use scip::score::MissInput; + use scip::score::miss_bucket; + + let reference = |kind: &str, text: &str| ScoreCallRef { + kind: kind.to_owned(), + text: text.to_owned(), + receiver: None, + receiver_type: None, + scope: String::new(), + placed: false, + }; + let empty = || MissContext::default(); + + // The W12 ladder reads through an empty context unchanged. + assert_eq!( + miss_bucket( + &empty(), + MissInput { + refs: &[reference("call", "f")], + callee_name: "f", + is_trait: false, + caller_file: 1, + callee_file: 1, + callee_id: 2, + }, + ), + ("unresolved path call", "unresolved path call") + ); + + // shared module file: the caller or the callee sits in a file more + // than one crate target claims, whatever the family. + let shared = MissContext { + shared: [7].into(), + ..MissContext::default() + }; + assert_eq!( + miss_bucket( + &shared, + MissInput { + refs: &[reference("call", "f")], + callee_name: "f", + is_trait: false, + caller_file: 7, + callee_file: 1, + callee_id: 2, + }, + ), + ("shared module file", "unresolved path call") + ); + assert_eq!( + miss_bucket( + &shared, + MissInput { + refs: &[reference("method_call", "f")], + callee_name: "f", + is_trait: false, + caller_file: 1, + callee_file: 7, + callee_id: 2, + }, + ), + ("shared module file", "other") + ); + + // glob import in scope: the callee is defined in a module row the + // caller's scope's glob use reaches. + let glob = MissContext { + glob: [((1, String::new()), [5].into())].into(), + symbol_module: [(9, 5)].into(), + ..MissContext::default() + }; + assert_eq!( + miss_bucket( + &glob, + MissInput { + refs: &[reference("call", "f")], + callee_name: "f", + is_trait: false, + caller_file: 1, + callee_file: 1, + callee_id: 9, + }, + ), + ("glob import in scope", "unresolved path call") + ); + // A callee defined elsewhere stays with the W12 bucket. + assert_eq!( + miss_bucket( + &glob, + MissInput { + refs: &[reference("call", "f")], + callee_name: "f", + is_trait: false, + caller_file: 1, + callee_file: 1, + callee_id: 10, + }, + ), + ("unresolved path call", "unresolved path call") + ); + + // re-exported path: the first segment binds an item whose home + // module differs from the module the binding's path names. + let reexport = MissContext { + uses: [( + (1, String::new()), + vec![Binding { + bound: "g".to_owned(), + prefix: "crate".to_owned(), + home: "crate::inner".to_owned(), + }], + )] + .into(), + ..MissContext::default() + }; + assert_eq!( + miss_bucket( + &reexport, + MissInput { + refs: &[reference("call", "g::f")], + callee_name: "f", + is_trait: false, + caller_file: 1, + callee_file: 1, + callee_id: 2, + }, + ), + ("re-exported path", "unresolved path call") + ); + assert_eq!( + miss_bucket( + &reexport, + MissInput { + refs: &[reference("call", "crate::f")], + callee_name: "f", + is_trait: false, + caller_file: 1, + callee_file: 1, + callee_id: 2, + }, + ), + ("unresolved path call", "unresolved path call") + ); + + // The shared-file mark outranks the glob reach, and a miss still + // lands in exactly one bucket. + let both = MissContext { + shared: [1].into(), + glob: [((1, String::new()), [5].into())].into(), + symbol_module: [(9, 5)].into(), + ..MissContext::default() + }; + assert_eq!( + miss_bucket( + &both, + MissInput { + refs: &[reference("call", "f")], + callee_name: "f", + is_trait: false, + caller_file: 1, + callee_file: 1, + callee_id: 9, + }, + ), + ("shared module file", "unresolved path call") + ); +} diff --git a/crates/phantom-fractal-extract/tests/scip_score.rs b/crates/phantom-fractal-extract/tests/scip_score.rs index 9e5025c..1a19792 100644 --- a/crates/phantom-fractal-extract/tests/scip_score.rs +++ b/crates/phantom-fractal-extract/tests/scip_score.rs @@ -9,6 +9,8 @@ mod scip; #[path = "scip/score_support.rs"] mod score_support; +#[path = "scip/w13_cases.rs"] +mod w13_cases; use std::path::PathBuf; @@ -135,120 +137,7 @@ fn each_band_carries_its_covered_figure() { /// several same-named refs. #[test] fn each_d4_bucket_classifies_its_hand_built_refs() { - use scip::score::w12_bucket; - - let reference = - |kind: &str, text: &str, receiver: Option<&str>, hint: Option<&str>| ScoreCallRef { - kind: kind.to_owned(), - text: text.to_owned(), - receiver: receiver.map(|text| text.to_owned()), - receiver_type: hint.map(|hint| hint.to_owned()), - scope: String::new(), - placed: false, - }; - let one_ref = |kind: &str, receiver: Option<&str>, hint: Option<&str>| { - vec![reference(kind, "f", receiver, hint)] - }; - - assert_eq!( - w12_bucket(&one_ref("macro_arg", None, None), "f", false), - "macro_arg path shape" - ); - assert_eq!( - w12_bucket(&one_ref("macro_arg", Some("x"), None), "f", false), - "macro_arg method shape" - ); - assert_eq!( - w12_bucket(&one_ref("macro", None, None), "f", false), - "macro invocation" - ); - // A macro_arg ref outranks the invocation that carries it. - let both = vec![ - reference("macro_arg", "f", Some("x"), None), - reference("macro", "f", None, None), - ]; - assert_eq!(w12_bucket(&both, "f", false), "macro_arg method shape"); - let both = vec![ - reference("macro_arg", "f", None, None), - reference("macro", "f", None, None), - ]; - assert_eq!(w12_bucket(&both, "f", false), "macro_arg path shape"); - - assert_eq!( - w12_bucket(&one_ref("call", None, None), "f", false), - "unresolved path call" - ); - assert_eq!( - w12_bucket(&one_ref("method_call", Some("x"), None), "f", true), - "trait-method callee" - ); - for (hint, bucket) in [ - ("type:T", "identifier with hint type"), - ("ctor:T::f", "identifier with hint ctor"), - ("ctor?:T::f", "identifier with hint ctor?"), - ("ret:f", "identifier with hint ret"), - ("ret?:f", "identifier with hint ret?"), - ] { - assert_eq!( - w12_bucket(&one_ref("method_call", Some("x"), Some(hint)), "f", false), - bucket, - "{hint}" - ); - } - assert_eq!( - w12_bucket( - &one_ref("method_call", Some("x"), Some("bogus:z")), - "f", - false - ), - "other" - ); - assert_eq!( - w12_bucket(&one_ref("method_call", Some("self"), None), "f", false), - "identifier with no hint" - ); - assert_eq!( - w12_bucket(&one_ref("method_call", Some("self.f"), None), "f", false), - "self.field" - ); - for receiver in ["make()", "a.b", "self.f.g()"] { - assert_eq!( - w12_bucket(&one_ref("method_call", Some(receiver), None), "f", false), - "call or chained receiver", - "{receiver}" - ); - } - for receiver in ["v[0]", ""] { - assert_eq!( - w12_bucket(&one_ref("method_call", Some(receiver), None), "f", false), - "other", - "{receiver}" - ); - } - let empty: Vec = Vec::new(); - assert_eq!(w12_bucket(&empty, "f", false), "other"); - - // The precedence with several same-named refs in one caller: the - // hint kind, then the unhinted identifier, self.field, the call or - // chained receiver, and the catch-all. - let mixed = vec![ - reference("method_call", "f", Some("make()"), None), - reference("method_call", "f", Some("self.f"), None), - reference("method_call", "f", Some("n2"), None), - reference("method_call", "f", Some("x"), Some("type:T")), - ]; - assert_eq!(w12_bucket(&mixed, "f", false), "identifier with hint type"); - let mixed = vec![ - reference("method_call", "f", Some("make()"), None), - reference("method_call", "f", Some("self.f"), None), - reference("method_call", "f", Some("n2"), None), - ]; - assert_eq!(w12_bucket(&mixed, "f", false), "identifier with no hint"); - let mixed = vec![ - reference("method_call", "f", Some("make()"), None), - reference("method_call", "f", Some("self.f"), None), - ]; - assert_eq!(w12_bucket(&mixed, "f", false), "self.field"); + w13_cases::each_d4_bucket_classifies_its_hand_built_refs(); } /// One extracted case per D4 bucket the scan can produce at this @@ -546,173 +435,7 @@ fn the_origin_line_counts_macro_arg_edges() { /// a multi-target file yet. #[test] fn each_w13_bucket_classifies_its_hand_built_refs() { - use scip::foreign::{Binding, MissContext}; - use scip::score::MissInput; - use scip::score::miss_bucket; - - let reference = |kind: &str, text: &str| ScoreCallRef { - kind: kind.to_owned(), - text: text.to_owned(), - receiver: None, - receiver_type: None, - scope: String::new(), - placed: false, - }; - let empty = || MissContext::default(); - - // The W12 ladder reads through an empty context unchanged. - assert_eq!( - miss_bucket( - &empty(), - MissInput { - refs: &[reference("call", "f")], - callee_name: "f", - is_trait: false, - caller_file: 1, - callee_file: 1, - callee_id: 2, - }, - ), - ("unresolved path call", "unresolved path call") - ); - - // shared module file: the caller or the callee sits in a file more - // than one crate target claims, whatever the family. - let shared = MissContext { - shared: [7].into(), - ..MissContext::default() - }; - assert_eq!( - miss_bucket( - &shared, - MissInput { - refs: &[reference("call", "f")], - callee_name: "f", - is_trait: false, - caller_file: 7, - callee_file: 1, - callee_id: 2, - }, - ), - ("shared module file", "unresolved path call") - ); - assert_eq!( - miss_bucket( - &shared, - MissInput { - refs: &[reference("method_call", "f")], - callee_name: "f", - is_trait: false, - caller_file: 1, - callee_file: 7, - callee_id: 2, - }, - ), - ("shared module file", "other") - ); - - // glob import in scope: the callee is defined in a module row the - // caller's scope's glob use reaches. - let glob = MissContext { - glob: [((1, String::new()), [5].into())].into(), - symbol_module: [(9, 5)].into(), - ..MissContext::default() - }; - assert_eq!( - miss_bucket( - &glob, - MissInput { - refs: &[reference("call", "f")], - callee_name: "f", - is_trait: false, - caller_file: 1, - callee_file: 1, - callee_id: 9, - }, - ), - ("glob import in scope", "unresolved path call") - ); - // A callee defined elsewhere stays with the W12 bucket. - assert_eq!( - miss_bucket( - &glob, - MissInput { - refs: &[reference("call", "f")], - callee_name: "f", - is_trait: false, - caller_file: 1, - callee_file: 1, - callee_id: 10, - }, - ), - ("unresolved path call", "unresolved path call") - ); - - // re-exported path: the first segment binds an item whose home - // module differs from the module the binding's path names. - let reexport = MissContext { - uses: [( - (1, String::new()), - vec![Binding { - bound: "g".to_owned(), - prefix: "crate".to_owned(), - home: "crate::inner".to_owned(), - }], - )] - .into(), - ..MissContext::default() - }; - assert_eq!( - miss_bucket( - &reexport, - MissInput { - refs: &[reference("call", "g::f")], - callee_name: "f", - is_trait: false, - caller_file: 1, - callee_file: 1, - callee_id: 2, - }, - ), - ("re-exported path", "unresolved path call") - ); - assert_eq!( - miss_bucket( - &reexport, - MissInput { - refs: &[reference("call", "crate::f")], - callee_name: "f", - is_trait: false, - caller_file: 1, - callee_file: 1, - callee_id: 2, - }, - ), - ("unresolved path call", "unresolved path call") - ); - - // The shared-file mark outranks the glob reach, and a miss still - // lands in exactly one bucket. - let both = MissContext { - shared: [1].into(), - glob: [((1, String::new()), [5].into())].into(), - symbol_module: [(9, 5)].into(), - ..MissContext::default() - }; - assert_eq!( - miss_bucket( - &both, - MissInput { - refs: &[reference("call", "f")], - callee_name: "f", - is_trait: false, - caller_file: 1, - callee_file: 1, - callee_id: 9, - }, - ), - ("shared module file", "unresolved path call") - ); + w13_cases::each_w13_bucket_classifies_its_hand_built_refs(); } /// The scope rule (W13 D4): a document outside every member directory @@ -971,3 +694,99 @@ fn the_w13_buckets_hold_their_extracted_cases() { "{report}" ); } + +/// The R7 scope rule on a hand-built case: a workspace-root package +/// member covers the tree, but a directory that carries its own +/// `Cargo.toml` and is not a member is its own boundary, so documents +/// under it leave both sets and only the counters move — recall and +/// the graded precision are the member documents' own. +#[test] +fn a_nested_package_that_is_not_a_member_stays_out_of_scope() { + let temp = Temp::new("nested-scope"); + let repo = temp.root.join("repo"); + std::fs::create_dir_all(repo.join("src")).expect("create src"); + std::fs::create_dir_all(repo.join("vendor/odd/src")).expect("create vendor"); + std::fs::write(repo.join("Cargo.toml"), "[package]\nname = \"root\"\n").expect("manifest"); + std::fs::write( + repo.join("vendor/odd/Cargo.toml"), + "[package]\nname = \"odd\"\n", + ) + .expect("vendor manifest"); + std::fs::write( + repo.join("src/lib.rs"), + "pub fn one() {}\npub fn caller() {\n one();\n}\n", + ) + .expect("lib"); + std::fs::write( + repo.join("vendor/odd/src/lib.rs"), + "pub fn far() {}\npub fn caller() {\n far();\n}\n", + ) + .expect("vendor lib"); + // Q1: a fixture document under `fixtures/` is a nested non-member + // package too — its own manifest, not a workspace member. + std::fs::create_dir_all(repo.join("fixtures/odd/src")).expect("create fixture dir"); + std::fs::write( + repo.join("fixtures/odd/Cargo.toml"), + "[package]\nname = \"fix-odd\"\n", + ) + .expect("fixture manifest"); + std::fs::write( + repo.join("fixtures/odd/src/lib.rs"), + "pub fn fixture() {}\n", + ) + .expect("fixture lib"); + let mut store = Store::open(&temp.db()).expect("open store"); + index(&repo, &mut store, &RustLanguage::new().expect("grammar")).expect("index"); + + let document = |path: &str, occurrences: Vec| scip::wire::Document { + relative_path: path.to_owned(), + occurrences, + }; + let export = scip::wire::ScipIndex { + documents: vec![ + document( + "src/lib.rs", + vec![ + occurrence("root", 0, 7, 10, "one", true), + occurrence("root", 2, 4, 7, "one", false), + ], + ), + document( + "vendor/odd/src/lib.rs", + vec![ + occurrence("odd", 0, 7, 10, "far", true), + occurrence("odd", 2, 4, 7, "far", false), + ], + ), + ], + }; + // Only the root package is a member; vendor/odd carries its own + // manifest and is not one. + let members = Members::from_metadata( + b"{\"workspace_root\":\"/w\",\"packages\":[{\"manifest_path\":\"/w/Cargo.toml\"}]}", + ) + .expect("members"); + assert!(members.in_scope("vendor/odd/src/lib.rs")); + assert!(!members.in_scope_with_packages( + "vendor/odd/src/lib.rs", + &store.package_dirs().expect("package dirs") + )); + // Q1: the fixture document reads out of scope even though the root + // member's prefix covers it, because fixtures/odd is its own + // package and not a member. + let package_dirs = store.package_dirs().expect("package dirs"); + assert!(package_dirs.contains("fixtures/odd")); + assert!(members.in_scope("fixtures/odd/src/lib.rs")); + assert!(!members.in_scope_with_packages("fixtures/odd/src/lib.rs", &package_dirs)); + let report = scip::score::score_with(&export, &store, Some(&members)).expect("score"); + assert!(report.contains("\"out_of_scope_documents\":1"), "{report}"); + assert!(report.contains("\"out_of_scope_truth\":1"), "{report}"); + assert!(report.contains("\"out_of_scope_edges\":1"), "{report}"); + // Only the member pair remains in truth, and the graded figures are + // the member edges' own: recall and precision did not move. + assert!(report.contains("\"recall\":{\"truth\":1,"), "{report}"); + assert!( + report.contains("\"graded\":{\"0.5\":{\"edges\":1,\"corroborated\":1,\"precision\":1.0000"), + "{report}" + ); +} diff --git a/crates/phantom-fractal-extract/tests/walk.rs b/crates/phantom-fractal-extract/tests/walk.rs index 9563693..86cb8a4 100644 --- a/crates/phantom-fractal-extract/tests/walk.rs +++ b/crates/phantom-fractal-extract/tests/walk.rs @@ -162,3 +162,30 @@ fn secret_denylist_matches_names_not_paths() { assert!(!is_secret(name), "{name}"); } } + +#[test] +fn an_unreadable_directory_is_recorded_unreadable() { + use std::os::unix::fs::PermissionsExt as _; + let root = common::TempDir::new("walk-unreadable"); + common::write(root.path(), "lib.rs", b"pub fn f() {}\n"); + common::write(root.path(), "locked/inner.rs", b"pub fn g() {}\n"); + let locked = root.path().join("locked"); + let mode = |bits| std::fs::Permissions::from_mode(bits); + std::fs::set_permissions(&locked, mode(0o000)).expect("lock"); + let readable = std::fs::read_dir(&locked).is_ok(); + let got = walk(root.path()); + std::fs::set_permissions(&locked, mode(0o755)).expect("unlock"); + if readable { + // A superuser lists it anyway; there is nothing to record. + return; + } + let got = got.expect("walk"); + let kept: Vec<&str> = got.files.iter().map(|file| file.path.as_str()).collect(); + assert_eq!(kept, ["lib.rs"]); + let skipped: Vec<(&str, SkipReason)> = got + .skipped + .iter() + .map(|skip| (skip.path.as_str(), skip.reason)) + .collect(); + assert_eq!(skipped, [("locked", SkipReason::Unreadable)]); +} diff --git a/crates/phantom-fractal-git/rust-tops.yaml b/crates/phantom-fractal-git/rust-tops.yaml new file mode 100644 index 0000000..14ba211 --- /dev/null +++ b/crates/phantom-fractal-git/rust-tops.yaml @@ -0,0 +1,142 @@ +# Rust-TOPS 1.1.0 instance for phantom-fractal-git (library-core). +# First instance (W13 G1): the store scaffold shape with the three +# quality layers on, thresholds unchanged. +protocol: + id: rust-tops + version: "1.1.0" + spec_uri: "https://github.com/Hedronite/rust-tops/blob/v0.2.0/RUST_TOPS.md" + adopted: "2026-10-24" + +crate: + name: phantom-fractal-git + class: library-core + edition: "2024" + msrv: "1.95.0" + unsafe_policy: forbid + hot_paths: [] + +goals: + correctness: behavior matches specified invariants under all applicable layers + density: fewest necessary production lines that preserve clarity and performance + performance: no unexplained regression on gated hot paths; prefer algorithmic then codegen then micro + notes: > + Schema 1. producer phantom-fractal. gix =0.89.0 with features sha1 only. + +layers: + - { id: static-lint, enabled: true, tier: 0, runner: "cargo clippy/fmt/deny", scope: workspace } + - { id: unit, enabled: true, tier: 0, runner: "cargo nextest", scope: lib } + - { id: doc, enabled: false, tier: 0, runner: "cargo test --doc", scope: lib } + - { id: integration, enabled: true, tier: 1, runner: "cargo nextest", scope: tests } + - { id: acceptance, enabled: false, tier: 1, runner: "assert_cmd journeys", scope: tests } + - { id: snapshot, enabled: false, tier: 2, runner: insta, scope: tests } + - { id: compile-fail, enabled: false, tier: 2, runner: trybuild, scope: tests } + - { id: coverage, enabled: true, tier: 0, runner: cargo-llvm-cov, scope: lib } + - { id: crap, enabled: true, tier: 0, runner: cargo-crap, scope: lib } + - { id: mutation, enabled: true, tier: 1, runner: cargo-mutants, scope: diff } + - { id: property, enabled: false, tier: 1, runner: proptest, scope: lib } + - { id: fuzz, enabled: false, tier: 3, runner: cargo-fuzz, scope: lib } + - { id: concurrency, enabled: false, tier: 3, runner: loom, scope: lib } + - { id: miri, enabled: false, tier: 3, runner: "cargo +nightly miri", scope: unsafe } + - { id: sanitizer, enabled: false, tier: 3, runner: "asan/tsan nightly", scope: unsafe } + - { id: formal-kani, enabled: false, tier: 4, runner: cargo-kani, scope: hot-paths } + - { id: bench-criterion, enabled: false, tier: 5, runner: criterion, scope: hot-paths } + - { id: bench-callgrind, enabled: false, tier: 4, runner: gungraun, scope: hot-paths } + - { id: security-advisory, enabled: true, tier: 0, runner: cargo-deny, scope: workspace } + - { id: density-audit, enabled: true, tier: 5, runner: review+clippy, scope: workspace } + - { id: laws, enabled: false, tier: 0, runner: bend, scope: workspace } + +gates: + lint: + clippy_deny: + - warnings + - clippy::correctness + - clippy::suspicious + - clippy::perf + - clippy::complexity + - clippy::dbg_macro + fmt_check: true + cognitive_complexity_max: 12 + too_many_lines_max: 40 + too_many_arguments_max: 6 + unit: + must_pass: true + allow_ignored: false + nextest_profile: ci + coverage: + tool: cargo-llvm-cov + line_min_pct: 95 + region_min_pct: 90 + branch_min_pct: null + per_file_line_min_pct: 90 + exclude_globs: + - tests/** + - benches/** + - examples/** + - src/generated/** + crap: + tool: cargo-crap + formula: "CC^2 * (1 - cov)^3 + CC" + threshold_production: 6 + threshold_agent: 6 + threshold_human: 4 + classic_crappy: 30 + fail_on: any-over-threshold + scar_guard: true + mutation: + tool: cargo-mutants + scope: diff + kill_rate_min_pct: 95 + unexplained_survivors_max: 0 + timeout_multiplier: 2 + property: + framework: proptest + cases_ci: 256 + cases_nightly: 2048 + required_on_classes: + - parser-codec + - library-core + - unsafe-kernel + fuzz: + engine: libfuzzer + pr_smoke_seconds: 30 + nightly_seconds: 7200 + formal: + kani_required_on_unsafe_kernels: true + unwind_bound_default: 8 + performance: + ci_tool: gungraun + local_tool: criterion + instruction_regression_max_pct: 2 + estimated_cycles_regression_max_pct: 5 + +density: + production_sloc_policy: minimum necessary lines that preserve named invariants, performance, and reviewability + max_fn_sloc: 40 + max_fn_cc: 8 + allow_long_table_functions: true + rules: + - one function, one named job + - enum+match over boolean modes + - no clone on hot_paths without a why-comment + - no trait or builder with a single impl + - unwrap only for programmer-invariant bugs + - tests may be table-driven; production may not be copy-pasted + +performance: + release_profile: + opt-level: 3 + lto: thin + codegen-units: 1 + panic: abort + debug: 0 + hot_path_requirements: [] + +agent: + guidelines_file: AGENTS.md + may_run_tools: true + must_not_write_tests_from_impl: true + must_not_split_for_crap_alone: true + required_before_pr: + - fmt + - clippy + - nextest diff --git a/crates/phantom-fractal-git/tests/head.rs b/crates/phantom-fractal-git/tests/head.rs index 273a3d6..02761f8 100644 --- a/crates/phantom-fractal-git/tests/head.rs +++ b/crates/phantom-fractal-git/tests/head.rs @@ -96,3 +96,30 @@ fn head_writes_nothing() { assert_eq!(snapshot(repo.dir()), before); assert_eq!(first, second); } + +/// A repository that opens but cannot be read answers the GitError the +/// store's `last_oid` comparison sees, with its own display text; a +/// directory that is not a repository at all answers None. +#[test] +fn a_broken_head_is_an_error_and_a_plain_dir_is_none() { + let repo = Repo::new("head-broken", &script()); + fs::write(repo.root.join(".git/HEAD"), "not a ref or an oid\n").expect("poison HEAD"); + let broken = head(&repo.root); + match broken { + Err(error) => { + let text = error.to_string(); + assert!( + text.starts_with("git history could not be read: "), + "{text}" + ); + } + Ok(found) => panic!("a poisoned HEAD read as {found:?}"), + } + // The plain directory stays the None case, and `mine` with it. + let plain = TempDir::new("head-plain-again"); + assert_eq!(head(&plain.0), Ok(None)); + assert_eq!( + mine(&plain.0, &Window::default(), &indexed(&["a.rs"])), + Ok(None) + ); +} diff --git a/crates/phantom-fractal-git/tests/sandbox.rs b/crates/phantom-fractal-git/tests/sandbox.rs index f1bdbb9..4c9c2cc 100644 --- a/crates/phantom-fractal-git/tests/sandbox.rs +++ b/crates/phantom-fractal-git/tests/sandbox.rs @@ -27,6 +27,19 @@ use phantom_fractal_git::{Window, mine}; use support::{DAY, HEAD, Op, Repo, Script, indexed}; const SANDBOX: &str = "/usr/bin/sandbox-exec"; + +/// The LLVM profile file the spawned child uses. The parent's value +/// passes through when a coverage run set it — its patterns name a +/// file per process, so the child's raw profile still lands where the +/// coverage run reads it — and a plain test run points the child at +/// the test's own temp dir. Either way an instrumented child never +/// writes the default profraw into the source tree (W13 G1). +fn child_profile_file(repo: &Repo) -> String { + match std::env::var_os("LLVM_PROFILE_FILE") { + Some(value) => value.to_string_lossy().into_owned(), + None => repo.dir().join("child-%p.profraw").display().to_string(), + } +} const CHILD: &str = "PHANTOM_GIT_SANDBOX_ROOT"; const TEST: &str = "mine_under_an_exec_and_fork_kill_sandbox_equals_mine_outside"; const PATHS: [&str; 3] = ["a.rs", "b.rs", "c.rs"]; @@ -126,6 +139,7 @@ fn mine_under_an_exec_and_fork_kill_sandbox_equals_mine_outside() { .args(["--exact", TEST, "--nocapture", "--test-threads=1"]) .env_clear() .env(CHILD, &repo.root) + .env("LLVM_PROFILE_FILE", child_profile_file(&repo)) .env("PATH", format!("{}:/usr/bin:/bin", bin.display())) .env("HOME", &repo.home) .env("GIT_DIR", "/nonexistent") @@ -162,6 +176,7 @@ fn mine_under_an_exec_and_fork_kill_sandbox_equals_mine_outside() { .arg(&profile) .args(["/bin/sh", "-c", "exit 0"]) .env_clear() + .env("LLVM_PROFILE_FILE", child_profile_file(&repo)) .status() .expect("run control"); assert_eq!(control.signal(), Some(9), "control not killed: {control:?}"); diff --git a/crates/phantom-fractal-git/tests/support/mod.rs b/crates/phantom-fractal-git/tests/support/mod.rs index 8222594..3dc3a0f 100644 --- a/crates/phantom-fractal-git/tests/support/mod.rs +++ b/crates/phantom-fractal-git/tests/support/mod.rs @@ -4,8 +4,9 @@ //! CLI, run only here: `git init`, then one `git fast-import` stream with //! fixed committer times and identity, then `git reset --hard` to check //! the tree out. Every run clears the environment and sets -//! `GIT_CONFIG_NOSYSTEM=1`, `GIT_CONFIG_GLOBAL=/dev/null`, a temp `HOME` -//! and `core.hooksPath=/dev/null`, so no hook or outside config runs. +//! `GIT_CONFIG_NOSYSTEM=1`, `GIT_CONFIG_GLOBAL=/dev/null`, a temp `HOME`, +//! an explicit author and committer identity, and `core.hooksPath=/dev/null`, +//! so no hook or outside config runs (W13 R9). #![allow(dead_code)] use std::collections::BTreeSet; @@ -14,7 +15,8 @@ use std::fs; use std::io::Write as _; use std::path::{Path, PathBuf}; use std::process::{self, Command, Stdio}; -use std::time::{SystemTime, UNIX_EPOCH}; +use std::sync::atomic::{AtomicU32, Ordering}; +use std::time::SystemTime; /// HEAD's committer time in most tests, unix seconds (2025-06-15). pub const HEAD: i64 = 1_750_000_000; @@ -25,12 +27,10 @@ pub struct TempDir(pub PathBuf); impl TempDir { pub fn new(tag: &str) -> Self { - let nanos = SystemTime::now() - .duration_since(UNIX_EPOCH) - .map(|dur| dur.as_nanos()) - .unwrap_or(0); + static COUNTER: AtomicU32 = AtomicU32::new(0); + let count = COUNTER.fetch_add(1, Ordering::Relaxed); let path = std::env::temp_dir().join(format!( - "phantom-fractal-git-{tag}-{}-{nanos}", + "phantom-fractal-git-{tag}-{}-{count}", process::id() )); fs::create_dir_all(&path).expect("temp dir"); @@ -159,6 +159,13 @@ impl Repo { .env("HOME", &self.home) .env("GIT_CONFIG_NOSYSTEM", "1") .env("GIT_CONFIG_GLOBAL", "/dev/null") + // R9: an explicit identity on every spawned git, so no + // machine-wide config can supply one and a commit git makes + // itself is deterministic. + .env("GIT_AUTHOR_NAME", "Phantom Tests") + .env("GIT_AUTHOR_EMAIL", "tests@phantom.invalid") + .env("GIT_COMMITTER_NAME", "Phantom Tests") + .env("GIT_COMMITTER_EMAIL", "tests@phantom.invalid") .env("LC_ALL", "C") .args([ "-c", diff --git a/crates/phantom-fractal-lang-rust/rust-tops.yaml b/crates/phantom-fractal-lang-rust/rust-tops.yaml index 707ef24..6392a82 100644 --- a/crates/phantom-fractal-lang-rust/rust-tops.yaml +++ b/crates/phantom-fractal-lang-rust/rust-tops.yaml @@ -1,7 +1,6 @@ # Rust-TOPS 1.1.0 instance for phantom-fractal-lang-rust (library-core). -# Scaffold slice: laws, coverage, CRAP, and mutation stay off until the -# crate has code they can measure. - +# Layers: lint, unit and security-advisory stay on; coverage, CRAP and +# mutation turn on with the thresholds the R1 ruling keeps. protocol: id: rust-tops version: "1.1.0" @@ -31,9 +30,9 @@ layers: - { id: acceptance, enabled: false, tier: 1, runner: "assert_cmd journeys", scope: tests } - { id: snapshot, enabled: false, tier: 2, runner: insta, scope: tests } - { id: compile-fail, enabled: false, tier: 2, runner: trybuild, scope: tests } - - { id: coverage, enabled: false, tier: 0, runner: cargo-llvm-cov, scope: lib } - - { id: crap, enabled: false, tier: 0, runner: cargo-crap, scope: lib } - - { id: mutation, enabled: false, tier: 1, runner: cargo-mutants, scope: diff } + - { id: coverage, enabled: true, tier: 0, runner: cargo-llvm-cov, scope: lib } + - { id: crap, enabled: true, tier: 0, runner: cargo-crap, scope: lib } + - { id: mutation, enabled: true, tier: 1, runner: cargo-mutants, scope: diff } - { id: property, enabled: false, tier: 1, runner: proptest, scope: lib } - { id: fuzz, enabled: false, tier: 3, runner: cargo-fuzz, scope: lib } - { id: concurrency, enabled: false, tier: 3, runner: loom, scope: lib } diff --git a/crates/phantom-fractal-lang-rust/src/resolve/lookup.rs b/crates/phantom-fractal-lang-rust/src/resolve/lookup.rs index 0fbf3b0..3af9617 100644 --- a/crates/phantom-fractal-lang-rust/src/resolve/lookup.rs +++ b/crates/phantom-fractal-lang-rust/src/resolve/lookup.rs @@ -8,6 +8,7 @@ //! caller's target may call (SPEC-w4 G2b). `use` paths and `impl` trait //! paths resolve by the same walk (SPEC-w7 G0a). +use std::cell::RefCell; use std::collections::HashMap; use phantom_fractal_lang::{ @@ -104,12 +105,16 @@ impl Tier { /// What a name binds in one module: the entities, whether they are /// declared there, and whether a `use` reached them through another crate. +#[derive(Clone)] struct Found { entities: Vec, local: bool, crossed: bool, } +/// What [`Index::lookup`] keeps: `(module, name, depth)` to its answer. +type Imported = HashMap<(Id, String, u32), Result>; + pub(crate) struct Index<'a> { modules: HashMap, symbols: HashMap, @@ -134,6 +139,11 @@ pub(crate) struct Index<'a> { /// Target to the crate names it may call, each to that lib's root /// module, or none when two libs share the name. crates: HashMap>>, + /// What the `use` bindings of a module give a name at a depth, kept + /// once computed. A lookup is a pure function of the index and those + /// three, so a kept answer is the answer, and a name whose `use` names + /// itself costs one walk per depth instead of one per path. + imported: RefCell, } impl<'a> Index<'a> { @@ -150,6 +160,7 @@ impl<'a> Index<'a> { by_type: HashMap::new(), by_name: HashMap::new(), crates: HashMap::new(), + imported: RefCell::default(), }; for module in &input.modules { index @@ -648,6 +659,29 @@ impl<'a> Index<'a> { /// What `name` binds in `module`. fn lookup(&self, module: Id, name: &str, depth: u32) -> Result { + let local = self.declared(module, name); + if !local.is_empty() { + return Ok(Found { + entities: local, + local: true, + crossed: false, + }); + } + if depth >= MAX_DEPTH { + return Err(Miss::Unresolved); + } + let key = (module, name.to_owned(), depth); + if let Some(kept) = self.imported.borrow().get(&key) { + return kept.clone(); + } + let found = self.through_uses(module, name, depth); + self.imported.borrow_mut().insert(key, found.clone()); + found + } + + /// The child modules and items named `name` declared in `module`. A + /// module item that has its own module row counts once, as the row. + fn declared(&self, module: Id, name: &str) -> Vec { let mut local: Vec = self .children .get(&(module, name)) @@ -664,16 +698,14 @@ impl<'a> Index<'a> { .filter(|id| !(has_module_row && self.kind(**id) == Some(SymbolKind::Module))) .map(|id| Entity::Symbol(*id)), ); - if !local.is_empty() { - return Ok(Found { - entities: local, - local: true, - crossed: false, - }); - } - if depth >= MAX_DEPTH { - return Err(Miss::Unresolved); - } + local + } + + /// What the non-glob `use` bindings of `module` that bind `name` name, + /// each walked one level deeper. With none found, the worst miss among + /// them; with no such binding, unresolved when a glob could supply the + /// name, else external. + fn through_uses(&self, module: Id, name: &str, depth: u32) -> Result { let bindings = self.uses.get(&module).map_or(&[][..], Vec::as_slice); let mut found = Vec::new(); let mut crossed = false; @@ -690,24 +722,23 @@ impl<'a> Index<'a> { Err(err) => miss = Some(miss.map_or(err, |prior| worse(prior, err))), } } - if !found.is_empty() { - found.sort_by_key(|entity| match entity { - Entity::Module(id) | Entity::Symbol(id) => *id, - }); - found.dedup(); - return Ok(Found { - entities: found, - local: false, - crossed, - }); - } - if let Some(miss) = miss { - return Err(miss); - } - if bindings.iter().any(|binding| binding.glob) { - return Err(Miss::Unresolved); - } - Err(Miss::External) + if found.is_empty() { + let glob = bindings.iter().any(|binding| binding.glob); + return Err(miss.unwrap_or(if glob { + Miss::Unresolved + } else { + Miss::External + })); + } + found.sort_by_key(|entity| match entity { + Entity::Module(id) | Entity::Symbol(id) => *id, + }); + found.dedup(); + Ok(Found { + entities: found, + local: false, + crossed, + }) } /// What a `use` path names, in any namespace. diff --git a/crates/phantom-fractal-lang-rust/tests/fuzz.rs b/crates/phantom-fractal-lang-rust/tests/fuzz.rs index c957802..a7d591c 100644 --- a/crates/phantom-fractal-lang-rust/tests/fuzz.rs +++ b/crates/phantom-fractal-lang-rust/tests/fuzz.rs @@ -14,7 +14,7 @@ //! the store builds from that extract, and resolves it. Each input must //! return without a panic within 250 ms of wall time. //! -//! The `smoke_*` tests run cases 0 to 1999 in CI, in 32 shards, and +//! The `smoke_*` tests run cases 0 to 1999 in CI, in 200 shards of 10, and //! assert no panic; they print their times without asserting them (ruling //! R3). The ignored `full` test asserts both the panic and the time bound. It //! runs `PHANTOM_FUZZ_CASES` cases from `PHANTOM_FUZZ_FIRST` (default 0) @@ -43,7 +43,7 @@ use splitmix::{Rng, env_number}; /// The CI smoke count. const SMOKE: u64 = 2_000; /// The CI smoke runs as this many tests, so each fits the CI time limit. -const SHARDS: u64 = 32; +const SHARDS: u64 = 200; /// The most bytes a random-bytes or token-soup input has. const MAX_BYTES: usize = 64 * 1024; /// The wall-time bound of one input. @@ -633,38 +633,206 @@ macro_rules! smoke_shards { } smoke_shards!( - smoke_00 = 0, - smoke_01 = 1, - smoke_02 = 2, - smoke_03 = 3, - smoke_04 = 4, - smoke_05 = 5, - smoke_06 = 6, - smoke_07 = 7, - smoke_08 = 8, - smoke_09 = 9, - smoke_10 = 10, - smoke_11 = 11, - smoke_12 = 12, - smoke_13 = 13, - smoke_14 = 14, - smoke_15 = 15, - smoke_16 = 16, - smoke_17 = 17, - smoke_18 = 18, - smoke_19 = 19, - smoke_20 = 20, - smoke_21 = 21, - smoke_22 = 22, - smoke_23 = 23, - smoke_24 = 24, - smoke_25 = 25, - smoke_26 = 26, - smoke_27 = 27, - smoke_28 = 28, - smoke_29 = 29, - smoke_30 = 30, - smoke_31 = 31, + smoke_000 = 0, + smoke_001 = 1, + smoke_002 = 2, + smoke_003 = 3, + smoke_004 = 4, + smoke_005 = 5, + smoke_006 = 6, + smoke_007 = 7, + smoke_008 = 8, + smoke_009 = 9, + smoke_010 = 10, + smoke_011 = 11, + smoke_012 = 12, + smoke_013 = 13, + smoke_014 = 14, + smoke_015 = 15, + smoke_016 = 16, + smoke_017 = 17, + smoke_018 = 18, + smoke_019 = 19, + smoke_020 = 20, + smoke_021 = 21, + smoke_022 = 22, + smoke_023 = 23, + smoke_024 = 24, + smoke_025 = 25, + smoke_026 = 26, + smoke_027 = 27, + smoke_028 = 28, + smoke_029 = 29, + smoke_030 = 30, + smoke_031 = 31, + smoke_032 = 32, + smoke_033 = 33, + smoke_034 = 34, + smoke_035 = 35, + smoke_036 = 36, + smoke_037 = 37, + smoke_038 = 38, + smoke_039 = 39, + smoke_040 = 40, + smoke_041 = 41, + smoke_042 = 42, + smoke_043 = 43, + smoke_044 = 44, + smoke_045 = 45, + smoke_046 = 46, + smoke_047 = 47, + smoke_048 = 48, + smoke_049 = 49, + smoke_050 = 50, + smoke_051 = 51, + smoke_052 = 52, + smoke_053 = 53, + smoke_054 = 54, + smoke_055 = 55, + smoke_056 = 56, + smoke_057 = 57, + smoke_058 = 58, + smoke_059 = 59, + smoke_060 = 60, + smoke_061 = 61, + smoke_062 = 62, + smoke_063 = 63, + smoke_064 = 64, + smoke_065 = 65, + smoke_066 = 66, + smoke_067 = 67, + smoke_068 = 68, + smoke_069 = 69, + smoke_070 = 70, + smoke_071 = 71, + smoke_072 = 72, + smoke_073 = 73, + smoke_074 = 74, + smoke_075 = 75, + smoke_076 = 76, + smoke_077 = 77, + smoke_078 = 78, + smoke_079 = 79, + smoke_080 = 80, + smoke_081 = 81, + smoke_082 = 82, + smoke_083 = 83, + smoke_084 = 84, + smoke_085 = 85, + smoke_086 = 86, + smoke_087 = 87, + smoke_088 = 88, + smoke_089 = 89, + smoke_090 = 90, + smoke_091 = 91, + smoke_092 = 92, + smoke_093 = 93, + smoke_094 = 94, + smoke_095 = 95, + smoke_096 = 96, + smoke_097 = 97, + smoke_098 = 98, + smoke_099 = 99, + smoke_100 = 100, + smoke_101 = 101, + smoke_102 = 102, + smoke_103 = 103, + smoke_104 = 104, + smoke_105 = 105, + smoke_106 = 106, + smoke_107 = 107, + smoke_108 = 108, + smoke_109 = 109, + smoke_110 = 110, + smoke_111 = 111, + smoke_112 = 112, + smoke_113 = 113, + smoke_114 = 114, + smoke_115 = 115, + smoke_116 = 116, + smoke_117 = 117, + smoke_118 = 118, + smoke_119 = 119, + smoke_120 = 120, + smoke_121 = 121, + smoke_122 = 122, + smoke_123 = 123, + smoke_124 = 124, + smoke_125 = 125, + smoke_126 = 126, + smoke_127 = 127, + smoke_128 = 128, + smoke_129 = 129, + smoke_130 = 130, + smoke_131 = 131, + smoke_132 = 132, + smoke_133 = 133, + smoke_134 = 134, + smoke_135 = 135, + smoke_136 = 136, + smoke_137 = 137, + smoke_138 = 138, + smoke_139 = 139, + smoke_140 = 140, + smoke_141 = 141, + smoke_142 = 142, + smoke_143 = 143, + smoke_144 = 144, + smoke_145 = 145, + smoke_146 = 146, + smoke_147 = 147, + smoke_148 = 148, + smoke_149 = 149, + smoke_150 = 150, + smoke_151 = 151, + smoke_152 = 152, + smoke_153 = 153, + smoke_154 = 154, + smoke_155 = 155, + smoke_156 = 156, + smoke_157 = 157, + smoke_158 = 158, + smoke_159 = 159, + smoke_160 = 160, + smoke_161 = 161, + smoke_162 = 162, + smoke_163 = 163, + smoke_164 = 164, + smoke_165 = 165, + smoke_166 = 166, + smoke_167 = 167, + smoke_168 = 168, + smoke_169 = 169, + smoke_170 = 170, + smoke_171 = 171, + smoke_172 = 172, + smoke_173 = 173, + smoke_174 = 174, + smoke_175 = 175, + smoke_176 = 176, + smoke_177 = 177, + smoke_178 = 178, + smoke_179 = 179, + smoke_180 = 180, + smoke_181 = 181, + smoke_182 = 182, + smoke_183 = 183, + smoke_184 = 184, + smoke_185 = 185, + smoke_186 = 186, + smoke_187 = 187, + smoke_188 = 188, + smoke_189 = 189, + smoke_190 = 190, + smoke_191 = 191, + smoke_192 = 192, + smoke_193 = 193, + smoke_194 = 194, + smoke_195 = 195, + smoke_196 = 196, + smoke_197 = 197, + smoke_198 = 198, + smoke_199 = 199, ); #[test] diff --git a/crates/phantom-fractal-lang-rust/tests/resolve.rs b/crates/phantom-fractal-lang-rust/tests/resolve.rs index e063482..97f2027 100644 --- a/crates/phantom-fractal-lang-rust/tests/resolve.rs +++ b/crates/phantom-fractal-lang-rust/tests/resolve.rs @@ -428,3 +428,28 @@ fn resolve_is_pure() { i.call(caller, "helper"); assert_eq!(i.resolve(), i.resolve()); } + +#[test] +fn a_name_two_failing_uses_bind_is_unresolved_when_either_names_the_workspace() { + // Unresolved beats external: a binding that names the workspace but + // cannot be placed is not an external call. + for (first, second, unresolved, external) in [ + ("std::fmt", "crate::gone", 1, 0), + ("crate::gone", "std::fmt", 1, 0), + ("crate::gone", "crate::missing", 1, 0), + ("std::fmt", "core::fmt", 0, 1), + ] { + let mut i = Input::new(); + i.uses("src/lib.rs", "", first, Some("a"), false); + i.uses("src/lib.rs", "", second, Some("a"), false); + let caller = i.function("caller"); + i.call(caller, "a::f"); + let (edges, counts) = i.resolve(); + assert!(edges.is_empty(), "{first} {second}: {edges:?}"); + assert_eq!( + (counts.unresolved, counts.external), + (unresolved, external), + "{first} {second}" + ); + } +} diff --git a/crates/phantom-fractal-lang-rust/tests/use_cycles.rs b/crates/phantom-fractal-lang-rust/tests/use_cycles.rs new file mode 100644 index 0000000..24535e6 --- /dev/null +++ b/crates/phantom-fractal-lang-rust/tests/use_cycles.rs @@ -0,0 +1,138 @@ +//! `use` bindings that name themselves or each other (SPEC-w13 G1b). +//! +//! A lookup follows `use` chains up to 8 deep, and gives up there as +//! unresolved. Before a lookup kept its answers, k bindings of one name +//! that each name that name cost about k^8 walks, and a fuzzed file took +//! minutes. Each shape below resolves exactly as before. The `_in_time` +//! cases reach counts that took minutes before; a regression shows as the +//! test runner's time limit. + +mod support; + +use phantom_fractal_lang::{Language, Reason, SymbolKind}; +use phantom_fractal_lang_rust::{RustLanguage, links}; +use support::{Input, edge}; + +/// `k` copies of `use gpui;`, 75 leaves `use gpui::A;`, and a call +/// `gpui::g()` from `caller`. +fn self_named(k: usize) -> Input { + let mut i = Input::new(); + for _ in 0..k { + i.uses("src/lib.rs", "", "gpui", None, false); + } + for leaf in 0..75 { + i.uses("src/lib.rs", "", &format!("gpui::A{leaf}"), None, false); + } + let caller = i.function("caller"); + i.call(caller, "gpui::g"); + i +} + +/// The one call is unresolved and no `use` gives a link. +fn assert_unresolved(i: &Input, case: &str) { + let (edges, counts) = i.resolve(); + assert!(edges.is_empty(), "{case}: {edges:?}"); + assert_eq!((counts.unresolved, counts.external), (1, 0), "{case}"); + assert!(links(&i.input).is_empty(), "{case}"); +} + +#[test] +fn a_use_that_names_itself_leaves_its_paths_unresolved() { + for k in 1..=3 { + assert_unresolved(&self_named(k), &format!("k={k}")); + } +} + +#[test] +fn many_uses_that_name_themselves_resolve_in_time() { + // 12^8 walks without kept answers. + assert_unresolved(&self_named(12), "k=12"); +} + +#[test] +fn a_self_leaf_is_the_same_binding_as_a_plain_use() { + let extract = RustLanguage::new() + .expect("the query compiles") + .extract("use gpui::{self};\nuse gpui;\n") + .expect("the source parses"); + let paths: Vec<(&str, Option<&str>)> = extract + .imports + .iter() + .map(|import| (import.path.as_str(), import.alias.as_deref())) + .collect(); + assert_eq!(paths, [("gpui", None), ("gpui", None)]); +} + +/// `use b as a; use a as b;`, `k` times over, and a call `a::f()`. +fn aliased_pair(k: usize) -> Input { + let mut i = Input::new(); + for _ in 0..k { + i.uses("src/lib.rs", "", "b", Some("a"), false); + i.uses("src/lib.rs", "", "a", Some("b"), false); + } + let caller = i.function("caller"); + i.call(caller, "a::f"); + i +} + +#[test] +fn two_aliases_that_name_each_other_leave_the_path_unresolved() { + assert_unresolved(&aliased_pair(1), "k=1"); + assert_unresolved(&aliased_pair(2), "k=2"); +} + +#[test] +fn many_aliases_that_name_each_other_resolve_in_time() { + assert_unresolved(&aliased_pair(8), "k=8"); +} + +/// Modules `m` and `n` that each re-export `f` from the other, `k` times +/// over, and a call `m::f()` from the crate root. +fn module_cycle(k: usize) -> Input { + let mut i = Input::new(); + i.module(1, "m", "src/lib.rs", "m"); + i.module(1, "n", "src/lib.rs", "n"); + for _ in 0..k { + i.uses("src/lib.rs", "m", "super::n::f", None, false); + i.uses("src/lib.rs", "n", "super::m::f", None, false); + } + let caller = i.function("caller"); + i.call(caller, "m::f"); + i +} + +#[test] +fn modules_that_re_export_from_each_other_leave_the_path_unresolved() { + assert_unresolved(&module_cycle(1), "k=1"); + assert_unresolved(&module_cycle(2), "k=2"); +} + +#[test] +fn many_re_exports_between_two_modules_resolve_in_time() { + assert_unresolved(&module_cycle(8), "k=8"); +} + +/// `k` copies of `use x;` beside `use crate::inner as x;`, where `inner` +/// declares `f`, and a call `x::f()`. +fn cycle_beside_a_real_binding(k: usize) -> (Input, [i64; 3]) { + let mut i = Input::new(); + i.module(1, "inner", "src/lib.rs", "inner"); + let f = i.symbol(SymbolKind::Function, "f", "src/lib.rs", "inner"); + for _ in 0..k { + i.uses("src/lib.rs", "", "x", None, false); + } + i.uses("src/lib.rs", "", "crate::inner", Some("x"), false); + let caller = i.function("caller"); + let call = i.call(caller, "x::f"); + (i, [call, caller, f]) +} + +#[test] +fn a_cycle_beside_a_real_binding_keeps_the_real_edge() { + for k in [1, 2, 12] { + let (i, [call, caller, f]) = cycle_beside_a_real_binding(k); + let (edges, counts) = i.resolve(); + assert_eq!(edges, [edge(call, caller, f, Reason::UseAlias)], "k={k}"); + assert_eq!(counts.edges, 1, "k={k}"); + } +} diff --git a/crates/phantom-fractal-lang/rust-tops.yaml b/crates/phantom-fractal-lang/rust-tops.yaml index 06eb2ea..1c90e17 100644 --- a/crates/phantom-fractal-lang/rust-tops.yaml +++ b/crates/phantom-fractal-lang/rust-tops.yaml @@ -1,8 +1,7 @@ # Rust-TOPS 1.1.0 instance for phantom-fractal-lang (library-core). -# Scaffold slice: laws, coverage, CRAP, and mutation stay off until the -# crate has code they can measure. - +# Layers: lint, unit and security-advisory stay on; coverage, CRAP and +# mutation turn on with the thresholds the R1 ruling keeps. protocol: id: rust-tops version: "1.1.0" @@ -32,9 +31,9 @@ layers: - { id: acceptance, enabled: false, tier: 1, runner: "assert_cmd journeys", scope: tests } - { id: snapshot, enabled: false, tier: 2, runner: insta, scope: tests } - { id: compile-fail, enabled: false, tier: 2, runner: trybuild, scope: tests } - - { id: coverage, enabled: false, tier: 0, runner: cargo-llvm-cov, scope: lib } - - { id: crap, enabled: false, tier: 0, runner: cargo-crap, scope: lib } - - { id: mutation, enabled: false, tier: 1, runner: cargo-mutants, scope: diff } + - { id: coverage, enabled: true, tier: 0, runner: cargo-llvm-cov, scope: lib } + - { id: crap, enabled: true, tier: 0, runner: cargo-crap, scope: lib } + - { id: mutation, enabled: true, tier: 1, runner: cargo-mutants, scope: diff } - { id: property, enabled: false, tier: 1, runner: proptest, scope: lib } - { id: fuzz, enabled: false, tier: 3, runner: cargo-fuzz, scope: lib } - { id: concurrency, enabled: false, tier: 3, runner: loom, scope: lib } diff --git a/crates/phantom-fractal-lang/tests/resolve_names.rs b/crates/phantom-fractal-lang/tests/resolve_names.rs new file mode 100644 index 0000000..90ade63 --- /dev/null +++ b/crates/phantom-fractal-lang/tests/resolve_names.rs @@ -0,0 +1,64 @@ +//! The resolve types' store texts and conversions. +//! +//! The store keeps these texts in `edges.kind`, `edges.reason`, +//! `edges.src_kind` and `edges.dst_kind`, so each one is fixed here as the +//! documentation names it. + +use phantom_fractal_lang::{ItemKind, LinkKind, LinkReason, Node, Reason, SymbolKind}; + +#[test] +fn every_item_kind_is_the_symbol_kind_of_the_same_name() { + for (item, symbol) in [ + (ItemKind::Function, SymbolKind::Function), + (ItemKind::Struct, SymbolKind::Struct), + (ItemKind::Enum, SymbolKind::Enum), + (ItemKind::Trait, SymbolKind::Trait), + (ItemKind::Module, SymbolKind::Module), + ] { + assert_eq!(SymbolKind::from(item), symbol); + } +} + +#[test] +fn each_call_reason_has_its_store_text() { + for (reason, text) in [ + (Reason::PathExact, "path-exact"), + (Reason::Local, "local"), + (Reason::UseAlias, "use-alias"), + (Reason::Assoc, "assoc"), + (Reason::SelfMethod, "self-method"), + (Reason::SelfAssoc, "self-assoc"), + (Reason::CratePath, "crate-path"), + (Reason::TypedMethod, "typed-method"), + (Reason::UniqueMethod, "unique-method"), + ] { + assert_eq!(reason.as_str(), text); + } +} + +#[test] +fn a_link_reason_shares_the_text_of_the_call_reason_of_its_tier() { + for (link, call) in [ + (LinkReason::PathExact, Reason::PathExact), + (LinkReason::Local, Reason::Local), + (LinkReason::UseAlias, Reason::UseAlias), + (LinkReason::CratePath, Reason::CratePath), + ] { + assert_eq!(link.as_str(), call.as_str()); + } + assert_eq!(LinkReason::ImplTrait.as_str(), "impl-trait"); +} + +#[test] +fn each_link_kind_has_its_store_text() { + assert_eq!(LinkKind::Import.as_str(), "import"); + assert_eq!(LinkKind::Impl.as_str(), "impl"); +} + +#[test] +fn a_node_gives_its_row_id_and_its_row_kind() { + assert_eq!(Node::Module(7).id(), 7); + assert_eq!(Node::Symbol(9).id(), 9); + assert_eq!(Node::Module(7).kind(), "module"); + assert_eq!(Node::Symbol(9).kind(), "symbol"); +} diff --git a/crates/phantom-fractal-proto/rust-tops.yaml b/crates/phantom-fractal-proto/rust-tops.yaml index e192089..0a22fdc 100644 --- a/crates/phantom-fractal-proto/rust-tops.yaml +++ b/crates/phantom-fractal-proto/rust-tops.yaml @@ -31,9 +31,9 @@ layers: - { id: acceptance, enabled: false, tier: 1, runner: "assert_cmd journeys", scope: tests } - { id: snapshot, enabled: false, tier: 2, runner: insta, scope: tests } - { id: compile-fail, enabled: false, tier: 2, runner: trybuild, scope: tests } - - { id: coverage, enabled: false, tier: 0, runner: cargo-llvm-cov, scope: lib } - - { id: crap, enabled: false, tier: 0, runner: cargo-crap, scope: lib } - - { id: mutation, enabled: false, tier: 1, runner: cargo-mutants, scope: diff } + - { id: coverage, enabled: true, tier: 0, runner: cargo-llvm-cov, scope: lib } + - { id: crap, enabled: true, tier: 0, runner: cargo-crap, scope: lib } + - { id: mutation, enabled: true, tier: 1, runner: cargo-mutants, scope: diff } - { id: property, enabled: false, tier: 1, runner: proptest, scope: lib } - { id: fuzz, enabled: false, tier: 3, runner: cargo-fuzz, scope: lib } - { id: concurrency, enabled: false, tier: 3, runner: loom, scope: lib } diff --git a/crates/phantom-fractal-store/rust-tops.yaml b/crates/phantom-fractal-store/rust-tops.yaml index a48f754..04e4981 100644 --- a/crates/phantom-fractal-store/rust-tops.yaml +++ b/crates/phantom-fractal-store/rust-tops.yaml @@ -1,7 +1,6 @@ # Rust-TOPS 1.1.0 instance for phantom-fractal-store (library-core). -# Scaffold slice: laws, coverage, CRAP, and mutation stay off until the -# crate has code they can measure. - +# Layers: lint, unit and security-advisory stay on; coverage, CRAP and +# mutation turn on with the thresholds the R1 ruling keeps. protocol: id: rust-tops version: "1.1.0" @@ -31,9 +30,9 @@ layers: - { id: acceptance, enabled: false, tier: 1, runner: "assert_cmd journeys", scope: tests } - { id: snapshot, enabled: false, tier: 2, runner: insta, scope: tests } - { id: compile-fail, enabled: false, tier: 2, runner: trybuild, scope: tests } - - { id: coverage, enabled: false, tier: 0, runner: cargo-llvm-cov, scope: lib } - - { id: crap, enabled: false, tier: 0, runner: cargo-crap, scope: lib } - - { id: mutation, enabled: false, tier: 1, runner: cargo-mutants, scope: diff } + - { id: coverage, enabled: true, tier: 0, runner: cargo-llvm-cov, scope: lib } + - { id: crap, enabled: true, tier: 0, runner: cargo-crap, scope: lib } + - { id: mutation, enabled: true, tier: 1, runner: cargo-mutants, scope: diff } - { id: property, enabled: false, tier: 1, runner: proptest, scope: lib } - { id: fuzz, enabled: false, tier: 3, runner: cargo-fuzz, scope: lib } - { id: concurrency, enabled: false, tier: 3, runner: loom, scope: lib } diff --git a/crates/phantom-fractal-store/src/cochange.rs b/crates/phantom-fractal-store/src/cochange.rs index ba6e322..4edf467 100644 --- a/crates/phantom-fractal-store/src/cochange.rs +++ b/crates/phantom-fractal-store/src/cochange.rs @@ -104,7 +104,10 @@ pub struct CochangeGroup { /// once per `write_cochange` so each pair insert binds numbers only. fn file_ids(conn: &Connection) -> Result, StoreError> { let mut statement = conn.prepare("SELECT path, id FROM files")?; - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut map: HashMap = HashMap::new(); while let Some(row) = rows.next()? { map.insert(row.get::<_, String>(0)?, row.get::<_, i64>(1)?); @@ -257,7 +260,10 @@ impl crate::Store { ORDER BY c.lift DESC, c.together DESC, s.path ASC LIMIT ?3", )?; - let mut rows = statement.query(rusqlite::params![file_id, min_lift, i64::from(limit)])?; + let queried = statement.query(rusqlite::params![file_id, min_lift, i64::from(limit)]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out: Vec = Vec::new(); while let Some(row) = rows.next()? { out.push(CochangePartner { @@ -331,7 +337,10 @@ fn read_scan_files(conn: &Connection) -> Result, StoreError> { LEFT JOIN files s ON s.path = h.path ORDER BY h.path", )?; - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out: Vec = Vec::new(); while let Some(row) = rows.next()? { let _unused_file_id: Option = row.get(1)?; @@ -357,7 +366,10 @@ fn read_scan_pairs(conn: &Connection) -> Result, StoreError> { JOIN files fb ON fb.id = c.b ORDER BY c.a, c.b", )?; - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out: Vec = Vec::new(); while let Some(row) = rows.next()? { out.push(PairRow { diff --git a/crates/phantom-fractal-store/src/detail.rs b/crates/phantom-fractal-store/src/detail.rs index 54a53f2..27785d1 100644 --- a/crates/phantom-fractal-store/src/detail.rs +++ b/crates/phantom-fractal-store/src/detail.rs @@ -31,7 +31,10 @@ impl crate::Store { JOIN files f ON f.id = s.file_id WHERE s.id = ?1", )?; - let mut rows = statement.query([id])?; + let queried = statement.query([id]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; match rows.next()? { None => Ok(None), Some(row) => Ok(Some(SymbolDetail { @@ -81,7 +84,10 @@ impl crate::Store { AND s.kind IN ('struct', 'enum') ORDER BY f.path, m.start_line, m.id", )?; - let mut rows = statement.query([id])?; + let queried = statement.query([id]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out = Vec::new(); while let Some(row) = rows.next()? { out.push(SymbolDetail { diff --git a/crates/phantom-fractal-store/src/dump.rs b/crates/phantom-fractal-store/src/dump.rs index 5787c2d..1199271 100644 --- a/crates/phantom-fractal-store/src/dump.rs +++ b/crates/phantom-fractal-store/src/dump.rs @@ -167,7 +167,10 @@ const DANGLING: &str = "\\N"; fn query_values(conn: &Connection, sql: &str) -> Result>, StoreError> { let mut statement = conn.prepare(sql)?; let count = statement.column_count(); - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out = Vec::new(); while let Some(row) = rows.next()? { let mut cells = Vec::with_capacity(count); diff --git a/crates/phantom-fractal-store/src/graph.rs b/crates/phantom-fractal-store/src/graph.rs index 1c561a4..51ca2e0 100644 --- a/crates/phantom-fractal-store/src/graph.rs +++ b/crates/phantom-fractal-store/src/graph.rs @@ -212,7 +212,10 @@ impl crate::Store { } let file_ids: HashMap = { let mut statement = tx.prepare("SELECT path, id FROM files")?; - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut map = HashMap::new(); while let Some(row) = rows.next()? { map.insert(row.get::<_, String>(0)?, row.get::<_, i64>(1)?); @@ -287,7 +290,10 @@ impl crate::Store { FROM targets t JOIN packages p ON p.id = t.package_id ORDER BY t.id", )?; - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; while let Some(row) = rows.next()? { targets.push(Target { id: row.get(0)?, @@ -305,7 +311,10 @@ impl crate::Store { FROM modules m JOIN files f ON f.id = m.file_id ORDER BY m.id", )?; - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; while let Some(row) = rows.next()? { modules.push(Module { id: row.get(0)?, @@ -325,7 +334,10 @@ impl crate::Store { FROM symbols s JOIN files f ON f.id = s.file_id ORDER BY s.id", )?; - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; while let Some(row) = rows.next()? { symbols.push(Symbol { id: row.get(0)?, @@ -350,7 +362,10 @@ impl crate::Store { FROM refs r JOIN files f ON f.id = r.file_id WHERE r.kind = 'use' ORDER BY r.id", )?; - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; while let Some(row) = rows.next()? { uses.push(UseBinding { id: row.get(6)?, @@ -372,7 +387,10 @@ impl crate::Store { FROM refs r JOIN files f ON f.id = r.file_id WHERE r.kind IN ('call', 'method_call', 'macro', 'macro_arg') ORDER BY r.id", )?; - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; while let Some(row) = rows.next()? { refs.push(CallRef { id: row.get(0)?, @@ -469,7 +487,10 @@ impl crate::Store { WHERE e.kind = ?1 ORDER BY e.src_kind, e.src_id, e.dst_kind, e.dst_id, e.id", )?; - let mut rows = statement.query([kind])?; + let queried = statement.query([kind]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out = Vec::new(); while let Some(row) = rows.next()? { out.push(EdgeRow { @@ -664,7 +685,10 @@ impl crate::Store { ) ORDER BY s.qual_name, f.path, s.id", )?; - let mut rows = statement.query([query, qualified])?; + let queried = statement.query([query, qualified]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out = Vec::new(); while let Some(row) = rows.next()? { out.push(SymbolMatch { @@ -715,7 +739,10 @@ impl crate::Store { statement: &mut rusqlite::Statement<'_>, id: [i64; 1], ) -> Result, StoreError> { - let mut rows = statement.query(id)?; + let queried = statement.query(id); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out = Vec::new(); while let Some(row) = rows.next()? { let reason = reason_from_text(&row.get::<_, String>(4)?)?; diff --git a/crates/phantom-fractal-store/src/impact.rs b/crates/phantom-fractal-store/src/impact.rs index b722fd5..381d324 100644 --- a/crates/phantom-fractal-store/src/impact.rs +++ b/crates/phantom-fractal-store/src/impact.rs @@ -39,7 +39,10 @@ impl crate::Store { LEFT JOIN packages p ON p.id = t.package_id WHERE s.id = ?1", )?; - let mut rows = statement.query([id])?; + let queried = statement.query([id]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; match rows.next()? { None => Ok(None), Some(row) => Ok(Some(SymbolFacts { @@ -128,7 +131,10 @@ impl crate::Store { JOIN files f ON f.id = s.file_id ORDER BY s.id", )?; - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out = Vec::new(); while let Some(row) = rows.next()? { out.push(SymbolSpan { @@ -169,7 +175,10 @@ impl crate::Store { AND r.kind IN ('call', 'method_call', 'macro', 'macro_arg') ORDER BY r.id", )?; - let mut rows = statement.query([id])?; + let queried = statement.query([id]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out = Vec::new(); while let Some(row) = rows.next()? { out.push(SymbolCallRef { diff --git a/crates/phantom-fractal-store/src/lib.rs b/crates/phantom-fractal-store/src/lib.rs index 66e57bd..84b4b69 100644 --- a/crates/phantom-fractal-store/src/lib.rs +++ b/crates/phantom-fractal-store/src/lib.rs @@ -778,3 +778,49 @@ pub(crate) mod open_trace { #[cfg(test)] mod tests; + +/// The test-only fault seam (W13 G1, after the writer lock's race +/// window): a statement's own result passes through here, and an armed +/// seam answers the error a failing statement would, so the caller's +/// error arm runs on a real failure path. Absent from every non-test +/// build: the callers' code is unchanged there. +#[cfg(test)] +pub(crate) mod fault { + use std::cell::Cell; + + thread_local!(static LEFT: Cell> = const { Cell::new(None) }); + + /// Arms the seam: the `nth` wrapped statement of the next call on + /// this thread answers an error, and the rest of that call runs or + /// returns as it would. + pub(crate) fn arm(nth: u32) { + LEFT.with(|left| left.set(Some(nth))); + } + + /// True while a wrapped statement is still waiting to fail. + pub(crate) fn armed() -> bool { + LEFT.with(|left| left.get().is_some()) + } + + /// Counts one wrapped statement; the armed one answers the store + /// error a real failing statement would. + pub(crate) fn tick>( + value: Result, + ) -> Result { + let mut fail = false; + LEFT.with(|left| { + if let Some(count) = left.get() { + if count <= 1 { + left.set(None); + fail = true; + } else { + left.set(Some(count - 1)); + } + } + }); + if fail { + return Err(crate::StoreError::Sqlite(rusqlite::Error::InvalidQuery)); + } + value.map_err(Into::into) + } +} diff --git a/crates/phantom-fractal-store/src/lock.rs b/crates/phantom-fractal-store/src/lock.rs index af9f5ce..843517e 100644 --- a/crates/phantom-fractal-store/src/lock.rs +++ b/crates/phantom-fractal-store/src/lock.rs @@ -282,6 +282,11 @@ fn open_lock_file(path: &Path) -> Result { let Some(pre) = verified_lock_meta(path)? else { return Err(identity_refusal()); }; + // The second race window: between the re-verify and the + // reopen, test-only, so a test can show the identity match + // and the `ELOOP` refusal that no plain sequence reaches. + #[cfg(test)] + run_lock_race(path); let mut reopen = std::fs::OpenOptions::new(); reopen.read(true).write(true); #[cfg(any( diff --git a/crates/phantom-fractal-store/src/rank.rs b/crates/phantom-fractal-store/src/rank.rs index d67c84b..b1001bd 100644 --- a/crates/phantom-fractal-store/src/rank.rs +++ b/crates/phantom-fractal-store/src/rank.rs @@ -101,7 +101,10 @@ impl crate::Store { WHERE s.kind <> 'module' ORDER BY s.id", )?; - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out = Vec::new(); while let Some(row) = rows.next()? { let target_kind: Option = row.get(6)?; @@ -136,7 +139,10 @@ impl crate::Store { WHERE e.kind IN ('call', 'impl') ORDER BY e.id", )?; - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out = Vec::new(); while let Some(row) = rows.next()? { out.push(RankEdge { @@ -176,7 +182,10 @@ impl crate::Store { AND e.dst_kind = 'symbol' ORDER BY 1, 2", )?; - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out = Vec::new(); while let Some(row) = rows.next()? { out.push(RankImportEdge { @@ -233,7 +242,10 @@ impl crate::Store { FROM rank r ORDER BY r.node_kind, r.node_id", )?; - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out = Vec::new(); while let Some(row) = rows.next()? { out.push((row.get(0)?, row.get(1)?, row.get(2)?)); @@ -247,7 +259,10 @@ impl crate::Store { let mut statement = self .conn .prepare("SELECT id, path FROM files ORDER BY path")?; - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out = Vec::new(); while let Some(row) = rows.next()? { out.push(MapFile { @@ -272,7 +287,10 @@ impl crate::Store { WHERE s.is_test = 0 AND s.kind <> 'module' ORDER BY s.id", )?; - let mut rows = statement.query([])?; + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out = Vec::new(); while let Some(row) = rows.next()? { out.push(MapRow { diff --git a/crates/phantom-fractal-store/src/scoring.rs b/crates/phantom-fractal-store/src/scoring.rs index 23a8e41..0cacd80 100644 --- a/crates/phantom-fractal-store/src/scoring.rs +++ b/crates/phantom-fractal-store/src/scoring.rs @@ -5,6 +5,11 @@ //! `macro_arg` refs. These live beside `impact`'s `symbol_call_refs`, //! which the scorer no longer uses: the buckets need the hint text, //! and that helper sits in another lane's file this week. +//! +//! Each read is a reader (prepare, query, one pass through +//! [`collect_rows`]) and a mapper that owns the row body; a column the +//! struct does not carry, or the placement mark, is read by the reader +//! and handed to its mapper. use std::collections::{BTreeMap, BTreeSet}; @@ -64,19 +69,13 @@ impl crate::Store { AND r.kind IN ('call', 'method_call', 'macro', 'macro_arg') ORDER BY r.id", )?; - let mut rows = statement.query([id])?; - let mut out = Vec::new(); - while let Some(row) = rows.next()? { - out.push(ScoreCallRef { - kind: row.get(0)?, - text: row.get(1)?, - receiver: row.get(2)?, - receiver_type: row.get(3)?, - scope: row.get(4)?, - placed: row.get::<_, i64>(5)? != 0, - }); - } - Ok(out) + let queried = statement.query([id]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; + collect_rows(&mut rows, |row| { + score_call_ref_row(row, row.get::<_, i64>(5)? != 0) + }) } /// Every `modules` row, by id, for the glob reach and the shared @@ -85,22 +84,14 @@ impl crate::Store { let mut statement = self.conn.prepare( "SELECT id, target_id, parent_id, path, file_id, scope FROM modules ORDER BY id", )?; - let mut rows = statement.query([])?; - let mut out = BTreeMap::new(); - while let Some(row) = rows.next()? { - let file_id: Option = row.get(4)?; - out.insert( - row.get(0)?, - ModuleRow { - target_id: row.get(1)?, - parent_id: row.get(2)?, - path: row.get(3)?, - file_id, - scope: row.get(5)?, - }, - ); - } - Ok(out) + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; + let keyed = collect_rows(&mut rows, |row| { + Ok((row.get::<_, i64>(0)?, module_row(row)?)) + })?; + Ok(keyed.into_iter().collect()) } /// Every file that carries a module row, with the number of crate @@ -111,12 +102,12 @@ impl crate::Store { "SELECT file_id, COUNT(DISTINCT target_id) FROM modules WHERE file_id IS NOT NULL GROUP BY file_id", )?; - let mut rows = statement.query([])?; - let mut out = BTreeMap::new(); - while let Some(row) = rows.next()? { - out.insert(row.get(0)?, row.get::<_, i64>(1)? as usize); - } - Ok(out) + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; + let counted = collect_rows(&mut rows, file_target_count)?; + Ok(counted.into_iter().collect()) } /// Every `use` ref row, for the glob and re-export buckets. @@ -125,19 +116,11 @@ impl crate::Store { "SELECT id, file_id, scope, text, alias, glob FROM refs WHERE kind = 'use' ORDER BY id", )?; - let mut rows = statement.query([])?; - let mut out = Vec::new(); - while let Some(row) = rows.next()? { - out.push(UseRow { - id: row.get(0)?, - file_id: row.get(1)?, - scope: row.get(2)?, - text: row.get(3)?, - alias: row.get(4)?, - glob: row.get::<_, i64>(5)? != 0, - }); - } - Ok(out) + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; + collect_rows(&mut rows, |row| use_row(row, row.get::<_, i64>(0)?)) } /// The target of every `import` edge that carries its `use` ref: @@ -147,10 +130,23 @@ impl crate::Store { "SELECT ref_id, dst_kind, dst_id FROM edges WHERE kind = 'import' AND ref_id IS NOT NULL", )?; - let mut rows = statement.query([])?; - let mut out = BTreeMap::new(); - while let Some(row) = rows.next()? { - out.insert(row.get(0)?, (row.get(1)?, row.get(2)?)); + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; + let keyed = collect_rows(&mut rows, import_target)?; + Ok(keyed.into_iter().collect()) + } + + /// The directory of every package Phantom's own discovery found, as + /// its manifest path's parent (relative, as the store keeps it). The + /// scope rule reads a nested package that is not a workspace member + /// as its own boundary (W13 R7). + pub fn package_dirs(&self) -> Result, StoreError> { + let mut statement = self.conn.prepare("SELECT manifest_path FROM packages")?; + let mut out = BTreeSet::new(); + for path in statement.query_map([], |row| row.get::<_, String>(0))? { + out.insert(manifest_dir(&path?)); } Ok(out) } @@ -160,12 +156,12 @@ impl crate::Store { let mut statement = self .conn .prepare("SELECT id, module_id FROM symbols WHERE module_id IS NOT NULL")?; - let mut rows = statement.query([])?; - let mut out = BTreeMap::new(); - while let Some(row) = rows.next()? { - out.insert(row.get(0)?, row.get(1)?); - } - Ok(out) + let queried = statement.query([]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; + let keyed = collect_rows(&mut rows, symbol_module)?; + Ok(keyed.into_iter().collect()) } /// The ids of every ref of one kind. The scorer's origin line asks @@ -173,7 +169,10 @@ impl crate::Store { /// call that sat inside a macro argument. pub fn ref_ids_of_kind(&self, kind: &str) -> Result, StoreError> { let mut statement = self.conn.prepare("SELECT id FROM refs WHERE kind = ?1")?; - let mut rows = statement.query([kind])?; + let queried = statement.query([kind]); + #[cfg(test)] + let queried = crate::fault::tick(queried); + let mut rows = queried?; let mut out = BTreeSet::new(); while let Some(row) = rows.next()? { out.insert(row.get(0)?); @@ -181,3 +180,79 @@ impl crate::Store { Ok(out) } } + +/// Every row of one statement through its mapper, in statement order. +fn collect_rows( + rows: &mut rusqlite::Rows<'_>, + map: impl Fn(&rusqlite::Row<'_>) -> Result, +) -> Result, StoreError> { + let mut out = Vec::new(); + while let Some(row) = rows.next()? { + out.push(map(row)?); + } + Ok(out) +} + +/// One `score_call_refs` row: kind, text, receiver, receiver-type hint +/// and scope, with the placement mark the reader read from the last +/// column. +fn score_call_ref_row(row: &rusqlite::Row<'_>, placed: bool) -> Result { + Ok(ScoreCallRef { + kind: row.get(0)?, + text: row.get(1)?, + receiver: row.get(2)?, + receiver_type: row.get(3)?, + scope: row.get(4)?, + placed, + }) +} + +/// One `modules` row, with the row key read by the reader: target, +/// parent, path, file and scope. +fn module_row(row: &rusqlite::Row<'_>) -> Result { + Ok(ModuleRow { + target_id: row.get(1)?, + parent_id: row.get(2)?, + path: row.get(3)?, + file_id: row.get(4)?, + scope: row.get(5)?, + }) +} + +/// One `use` ref row, with the row key read by the reader. +fn use_row(row: &rusqlite::Row<'_>, id: i64) -> Result { + Ok(UseRow { + id, + file_id: row.get(1)?, + scope: row.get(2)?, + text: row.get(3)?, + alias: row.get(4)?, + glob: row.get::<_, i64>(5)? != 0, + }) +} + +/// One `import` edge row: its ref id and the destination kind and row +/// id. +fn import_target(row: &rusqlite::Row<'_>) -> Result<(i64, (String, i64)), StoreError> { + Ok((row.get(0)?, (row.get(1)?, row.get(2)?))) +} + +/// One `modules` grouping row: the file and the number of crate targets +/// claiming it. +fn file_target_count(row: &rusqlite::Row<'_>) -> Result<(i64, usize), StoreError> { + Ok((row.get(0)?, row.get::<_, i64>(1)? as usize)) +} + +/// One `symbols` row: its id and its module row. +fn symbol_module(row: &rusqlite::Row<'_>) -> Result<(i64, i64), StoreError> { + Ok((row.get(0)?, row.get(1)?)) +} + +/// The directory of one package manifest path: everything before its +/// last separator, and the borrowed empty string for a path with none. +fn manifest_dir(path: &str) -> String { + match path.rsplit_once('/') { + Some((head, _)) => head.to_owned(), + None => String::new(), + } +} diff --git a/crates/phantom-fractal-store/src/tests/defects.rs b/crates/phantom-fractal-store/src/tests/defects.rs new file mode 100644 index 0000000..4cb340f --- /dev/null +++ b/crates/phantom-fractal-store/src/tests/defects.rs @@ -0,0 +1,298 @@ +//! The arms a malformed store reaches (W13 G1): a path that is data +//! rather than framing, and references whose target row left. The +//! canonical dump must escape the first and name the second, and it +//! must do both deterministically. +use super::{TempDb, assert_outside_workspace}; +use crate::Store; +use phantom_fractal_lang_rust::RustLanguage; +const ALPHA: &str = "pub fn alpha() -> u8 {\n 1\n}\n"; +/// A path carrying every character the dump escapes, plus two control +/// bytes a path may legally hold on a Unix filesystem. +const ODD: &str = "src/odd\tname\nline\rcarry\\slash\u{1}ctl\u{7f}del.rs"; +#[test] +fn canonical_dump_escapes_its_cells_and_names_what_it_cannot_resolve() { + let db = TempDb::new("defects-dump"); + assert_outside_workspace(db.path()); + let mut store = Store::open(db.path()).expect("open"); + let language = RustLanguage::new().expect("grammar"); + store.put_source(ODD, ALPHA, 0, &language).expect("put odd"); + store + .put_source("src/a.rs", ALPHA, 0, &language) + .expect("put a"); + // Rows whose references left: a target over a package the table + // does not hold, a module over a target it does not hold, and rank + // rows over a symbol it does not hold, over a package id, and under + // a node kind the dump does not resolve. + store + .conn + .execute_batch( + "PRAGMA foreign_keys = OFF; + INSERT INTO targets(package_id, kind, name, root_file) + VALUES (999999, 'lib', 'ghost', 'src/a.rs'); + INSERT INTO modules(target_id, path, file_id, scope) + VALUES (999999, 'crate', NULL, ''); + INSERT INTO rank(node_kind, node_id, score, epoch) VALUES ('symbol', 999999, 1.0, 1); + INSERT INTO rank(node_kind, node_id, score, epoch) VALUES ('package', 1, 2.0, 1); + INSERT INTO rank(node_kind, node_id, score, epoch) VALUES ('bogus', 1, 3.0, 1); + INSERT INTO edges(kind, src_kind, src_id, dst_kind, dst_id, confidence, reason, ref_id) + VALUES ('call', 'symbol', 1, 'symbol', 999999, 0.5, 'local', NULL);", + ) + .expect("poison"); + let dump = store.canonical_dump().expect("dump"); + // The escapes: tab, newline, carriage return, backslash, and the two + // control characters, so one stored path stays one cell. + assert!( + dump.contains("src/odd\\tname\\nline\\rcarry\\\\slash\\x01ctl\\x7fdel.rs"), + "{dump}" + ); + // A reference with no row to name reads its dangling marker. + assert!(dump.contains("\\N"), "{dump}"); + // The framing holds: thirteen tables, one header each. + assert_eq!(dump.matches("\n# table ").count() + 1, 13, "{dump}"); + // And the abnormal rows do not make the dump unstable. + assert_eq!(dump, store.canonical_dump().expect("dump again")); +} +/// Every canon table one store needs, minus the two the lock and the +/// stamp keep. +const TABLES: &[&str] = &[ + "packages", + "targets", + "modules", + "symbols", + "refs", + "edges", + "files", + "file_history", + "git_scan", + "cochange", + "rank", +]; +/// One store with rows in every table, then `table` dropped. +fn store_missing(label: &str, table: &str) -> (TempDb, Store) { + let db = TempDb::new(label); + assert_outside_workspace(db.path()); + let mut store = Store::open(db.path()).expect("open"); + let language = RustLanguage::new().expect("grammar"); + store + .put_source("src/a.rs", ALPHA, 0, &language) + .expect("put a"); + store + .put_source( + "src/b.rs", + "use crate::alpha;\npub fn beta() {\n alpha();\n}\n", + 0, + &language, + ) + .expect("put b"); + let package = crate::Package { + name: "sample".to_owned(), + version: None, + manifest_path: "/w/Cargo.toml".to_owned(), + }; + let target = phantom_fractal_lang::Target { + id: 1, + package: "sample".to_owned(), + name: "sample".to_owned(), + kind: phantom_fractal_lang::TargetKind::Lib, + root: "src/a.rs".to_owned(), + }; + let module = phantom_fractal_lang::Module { + id: 1, + target: 1, + parent: None, + path: "crate".to_owned(), + file: "src/a.rs".to_owned(), + scope: String::new(), + }; + store + .write_crate_graph(&[package], &[target], &[module], &[]) + .expect("graph"); + store.replace_edges(&[]).expect("edges"); + store.write_rank(1, &[], &[]).expect("rank"); + store + .conn + .execute_batch( + "DROP TRIGGER IF EXISTS symbols_ai; + DROP TRIGGER IF EXISTS symbols_ad; + DROP TRIGGER IF EXISTS symbols_au; + PRAGMA foreign_keys = OFF;", + ) + .expect("prep"); + store + .conn + .execute_batch(&format!("DROP TABLE IF EXISTS \"{table}\";")) + .expect("drop"); + (db, store) +} +#[test] +fn every_read_answers_when_a_table_is_missing() { + for table in TABLES { + let (_db, store) = store_missing(&format!("missing-{table}"), table); + // Every read this crate exposes answers — a value or a store + // error — instead of panicking on the missing table. + let _ = store.file_paths(); + let _ = store.file_stats(); + let _ = store.read_items("src/a.rs"); + let _ = store.stored_blake3("src/a.rs"); + let _ = store.ref_texts("src/a.rs"); + let _ = store.match_count("alpha"); + let _ = store.producer(); + let _ = store.schema_version(); + let _ = store.user_version(); + let _ = store.row_count("files"); + let _ = store.read_only_still_valid(); + let _ = store.trusted_schema_is_off(); + let _ = store.synchronous_is_normal(); + let _ = store.defensive_is_on(); + let _ = store.symbol_spans(); + let _ = store.symbol_detail(1); + let _ = store.symbol_facts(1); + let _ = store.members(1); + let _ = store.symbol_by_stable_key("k"); + let _ = store.symbol_call_refs(1); + let _ = store.unresolved_named("alpha"); + let _ = store.macro_args_named("alpha"); + let _ = store.file_has_module(1); + let _ = store.score_call_refs(1); + let _ = store.module_rows(); + let _ = store.use_rows(); + let _ = store.import_targets(); + let _ = store.package_dirs(); + let _ = store.symbol_modules(); + let _ = store.ref_ids_of_kind("call"); + let _ = store.file_target_counts(); + let _ = store.rank_input(); + let _ = store.rank_call_edges(); + let _ = store.rank_import_edges(); + let _ = store.stored_rank(); + let _ = store.files(); + let _ = store.map_file_paths(); + let _ = store.map_rows(); + let _ = store.graph_epoch(); + let _ = store.rank_epoch(); + let _ = store.workspace_fingerprint(); + let _ = store.rs_skipped(); + let _ = store.extractor_version(); + let _ = store.edges_of_kind("call"); + let _ = store.resolve_input(); + let _ = store.symbols_matching("alpha"); + let _ = store.callers(1); + let _ = store.callees(1); + let _ = store.unresolved_calls(1); + let _ = store.cochange_scan(); + let _ = store.cochange_head(); + let _ = store.cochange_of("src/a.rs", 0.0, 5); + let _ = store.cochange_partners_for_files(&["src/a.rs".to_owned()], 0.0, 5); + let _ = store.canonical_dump(); + } +} +#[test] +fn every_writer_answers_when_a_table_is_missing() { + for table in TABLES { + let (_db, mut store) = store_missing(&format!("write-{table}"), table); + let language = RustLanguage::new().expect("grammar"); + // Every writer answers a value or a store error, never a panic. + let _ = store.put_source("src/a.rs", ALPHA, 1, &language); + let _ = store.remove_file("src/b.rs"); + let _ = store.write_crate_graph(&[], &[], &[], &[]); + let _ = store.replace_edges(&[]); + let _ = store.write_links(&[]); + let _ = store.write_method_owners(&[(1, 1)]); + let _ = store.reset_symbol_test_flags("src/a.rs", &[true, true]); + let _ = store.write_rank(2, &[], &[]); + let _ = store.write_cochange(&crate::CochangeScan::default()); + let _ = store.set_workspace_fingerprint(&[0u8; 32], 0); + let _ = store.set_extractor_version(11); + } +} + +/// A SQLite file that is not one of ours: a table, no stamp. +fn foreign_database(path: &std::path::Path) { + let conn = rusqlite::Connection::open(path).expect("raw sqlite"); + conn.execute_batch("CREATE TABLE not_ours(x TEXT);") + .expect("table"); +} +#[test] +fn open_and_index_paths_refuse_what_they_cannot_read() { + let db = TempDb::new("defects-open"); + assert_outside_workspace(db.path()); + let not_ours = db.path().with_extension("foreign.db"); + foreign_database(¬_ours); + // A database with no stamp is not ours, and the read-only open says + // so rather than writing one. + assert!(matches!( + Store::open_read_only(¬_ours), + Err(crate::StoreError::ForeignDatabase) + )); + // A `meta` table of another shape answers through the same read-only + // path, as a store error rather than a panic. + let malformed = db.path().with_extension("malformed.db"); + let conn = rusqlite::Connection::open(&malformed).expect("raw sqlite"); + conn.execute_batch("CREATE TABLE meta(x TEXT);") + .expect("table"); + drop(conn); + assert!(Store::open_read_only(&malformed).is_err()); + // A read-only open of a database that does not exist answers, and + // creates nothing. + let absent = db.path().with_extension("absent.db"); + assert!(Store::open_read_only(&absent).is_err()); + assert!(!absent.exists()); + // A data directory whose name needs escaping still opens read-only: + // the URI percent-encodes it instead of failing the open. + let spaced = db.path().with_extension("spaced"); + std::fs::create_dir_all(&spaced).expect("dir"); + let inside = spaced.join("a graph.db"); + drop(Store::open(&inside).expect("write open")); + let store = Store::open_read_only(&inside).expect("read-only open"); + assert_eq!(store.row_count("files").expect("count"), 0); + drop(store); + // The index path names a repo without a file name, and one that is + // not on disk, without an absolute path in either. + let root = crate::index_database_path(std::path::Path::new("/")); + assert!(root.to_string_lossy().ends_with("graph.db")); + assert!(root.to_string_lossy().contains("/repo-")); + let missing = crate::index_database_path(std::path::Path::new("/no/such/repo")); + assert!(missing.to_string_lossy().contains("/repo-")); +} +#[test] +fn a_fingerprint_or_a_target_this_slice_never_writes_is_refused() { + let db = TempDb::new("defects-graph"); + assert_outside_workspace(db.path()); + let mut store = Store::open(db.path()).expect("open"); + // A stored fingerprint with a character outside the alphabet the + // writer emits reads as no fingerprint, never as a panic. + store + .conn + .execute( + "INSERT INTO meta(key, value) VALUES ('workspace_fingerprint', ?1) + ON CONFLICT(key) DO UPDATE SET value = excluded.value", + ["z".repeat(64)], + ) + .expect("poison fingerprint"); + assert_eq!(store.workspace_fingerprint().expect("fingerprint"), None); + // A module naming a target the graph does not carry is refused. + let package = crate::Package { + name: "sample".to_owned(), + version: None, + manifest_path: "/w/Cargo.toml".to_owned(), + }; + let target = phantom_fractal_lang::Target { + id: 1, + package: "sample".to_owned(), + name: "sample".to_owned(), + kind: phantom_fractal_lang::TargetKind::Lib, + root: "src/a.rs".to_owned(), + }; + let stray = phantom_fractal_lang::Module { + id: 2, + target: 99, + parent: None, + path: "crate::stray".to_owned(), + file: "src/a.rs".to_owned(), + scope: String::new(), + }; + assert!(matches!( + store.write_crate_graph(&[package], &[target], &[stray], &[]), + Err(crate::StoreError::UnknownTarget(99)) + )); +} diff --git a/crates/phantom-fractal-store/src/tests/dirs.rs b/crates/phantom-fractal-store/src/tests/dirs.rs index 1bcbc30..7b91740 100644 --- a/crates/phantom-fractal-store/src/tests/dirs.rs +++ b/crates/phantom-fractal-store/src/tests/dirs.rs @@ -13,6 +13,7 @@ use crate::{Store, StoreError}; #[test] #[cfg(unix)] fn fresh_data_directories_are_created_0700() { + let _serial = super::serial(); let data = TempData::new("dirs"); let db = data.db(); let store = Store::open(&db).expect("open"); @@ -33,6 +34,7 @@ fn fresh_data_directories_are_created_0700() { #[test] #[cfg(unix)] fn open_tightens_repos_directories_and_leaves_the_data_root() { + let _serial = super::serial(); let data = TempData::new("tighten"); mkdir_explicit(&data.root, 0o751); let repos = data.root.join("repos"); @@ -56,6 +58,7 @@ fn open_tightens_repos_directories_and_leaves_the_data_root() { #[test] #[cfg(unix)] fn open_leaves_a_wider_non_repos_directory_alone() { + let _serial = super::serial(); let db = TempDb::new("plain"); assert_outside_workspace(db.path()); // The temp dir already exists with a wider umask-carried mode; opening @@ -72,6 +75,7 @@ fn open_leaves_a_wider_non_repos_directory_alone() { #[test] #[cfg(unix)] fn database_and_wal_files_are_0600_after_index() { + let _serial = super::serial(); let data = TempData::new("wal"); let db = data.db(); let mut store = Store::open(&db).expect("open"); @@ -90,6 +94,7 @@ fn database_and_wal_files_are_0600_after_index() { #[test] #[cfg(unix)] fn a_symlinked_repos_refuses_open_without_chmod() { + let _serial = super::serial(); let data = TempData::new("symlink-repos"); mkdir_explicit(&data.root, 0o755); let target = data.root.join("target"); @@ -107,6 +112,7 @@ fn a_symlinked_repos_refuses_open_without_chmod() { #[test] #[cfg(unix)] fn a_symlinked_repo_directory_refuses_open_without_chmod() { + let _serial = super::serial(); let data = TempData::new("symlink-repo"); mkdir_explicit(&data.root, 0o755); mkdir_explicit(&data.root.join("repos"), 0o755); @@ -125,6 +131,7 @@ fn a_symlinked_repo_directory_refuses_open_without_chmod() { #[test] #[cfg(unix)] fn non_directory_repos_refuses_open() { + let _serial = super::serial(); let data = TempData::new("file-repos"); mkdir_explicit(&data.root, 0o755); fs::write(data.root.join("repos"), b"not a directory").expect("write"); @@ -135,6 +142,7 @@ fn non_directory_repos_refuses_open() { #[test] #[cfg(unix)] fn owner_comparison_rejects_a_mismatched_uid() { + let _serial = super::serial(); use std::os::unix::fs::MetadataExt; let probe = unique_path("owner-compare"); std::fs::OpenOptions::new() @@ -152,6 +160,7 @@ fn owner_comparison_rejects_a_mismatched_uid() { #[test] #[cfg(unix)] fn the_real_owner_path_uses_the_effective_uid() { + let _serial = super::serial(); let data = TempData::new("owner-real"); mkdir_explicit(&data.root, 0o755); mkdir_explicit(&data.root.join("repos"), 0o755); @@ -180,6 +189,7 @@ fn unique_path(label: &str) -> PathBuf { #[test] #[cfg(unix)] fn open_fails_when_the_repo_dir_swaps_to_a_symlink() { + let _serial = super::serial(); let data = TempData::new("swap-link"); mkdir_explicit(&data.root, 0o755); mkdir_explicit(&data.root.join("repos"), 0o755); @@ -201,6 +211,7 @@ fn open_fails_when_the_repo_dir_swaps_to_a_symlink() { #[test] #[cfg(unix)] fn open_refuses_a_swapped_in_directory() { + let _serial = super::serial(); let data = TempData::new("swap-dir"); mkdir_explicit(&data.root, 0o755); mkdir_explicit(&data.root.join("repos"), 0o755); @@ -220,6 +231,7 @@ fn open_refuses_a_swapped_in_directory() { #[test] #[cfg(unix)] fn tighten_refuses_without_the_uid_and_narrows_with_it() { + let _serial = super::serial(); use std::os::unix::fs::MetadataExt; // Same dev/ino directory with a group bit: the chmod lands through // the handle only after the uid check passes (the probe's uid). @@ -257,6 +269,7 @@ fn tighten_refuses_without_the_uid_and_narrows_with_it() { ), ))] fn a_symlink_swap_fails_at_the_open() { + let _serial = super::serial(); let data = TempData::new("w5-swap-link"); mkdir_explicit(&data.root, 0o755); mkdir_explicit(&data.root.join("repos"), 0o755); @@ -298,6 +311,7 @@ fn a_symlink_swap_fails_at_the_open() { ), ))] fn a_regular_file_swap_fails_at_the_open() { + let _serial = super::serial(); let data = TempData::new("w5-swap-file"); mkdir_explicit(&data.root, 0o755); mkdir_explicit(&data.root.join("repos"), 0o755); diff --git a/crates/phantom-fractal-store/src/tests/faults.rs b/crates/phantom-fractal-store/src/tests/faults.rs new file mode 100644 index 0000000..a5096ab --- /dev/null +++ b/crates/phantom-fractal-store/src/tests/faults.rs @@ -0,0 +1,118 @@ +//! The statement error edges (W13 G1): each row statement of a read and +//! of the crate-graph writer fails in turn through the test-only seam, +//! and the call answers a store error the way a real database fault +//! makes it. The seam is `cfg(test)` only, so no non-test build carries +//! it and no behaviour moves. +use super::{TempDb, assert_outside_workspace}; +use crate::{Store, fault}; +use phantom_fractal_lang::{Module, Target, TargetKind}; +use phantom_fractal_lang_rust::RustLanguage; +const ALPHA: &str = "pub fn alpha() -> u8 {\n 1\n}\n"; +const USES: &str = "use crate::alpha;\npub fn beta() {\n alpha();\n}\n"; +/// One store with two files, a package, a target, a module and an edge, +/// so every read below has rows to walk before its statement fails. +fn populated(label: &str) -> (TempDb, Store) { + let db = TempDb::new(label); + assert_outside_workspace(db.path()); + let mut store = Store::open(db.path()).expect("open"); + let language = RustLanguage::new().expect("grammar"); + store + .put_source("src/a.rs", ALPHA, 0, &language) + .expect("put a"); + store + .put_source("src/b.rs", USES, 0, &language) + .expect("put b"); + store + .write_crate_graph( + &[crate::Package { + name: "sample".to_owned(), + version: None, + manifest_path: "/w/Cargo.toml".to_owned(), + }], + &[Target { + id: 1, + package: "sample".to_owned(), + name: "sample".to_owned(), + kind: TargetKind::Lib, + root: "src/a.rs".to_owned(), + }], + &[Module { + id: 1, + target: 1, + parent: None, + path: "crate".to_owned(), + file: "src/a.rs".to_owned(), + scope: String::new(), + }], + &[], + ) + .expect("graph"); + store.replace_edges(&[]).expect("edges"); + (db, store) +} +/// Arms each of the call's `statements` row statements in turn: every +/// position must be reached, and the call must answer a store error at +/// each one instead of running on. +fn surfaces(statements: u32, mut call: impl FnMut() -> Result<(), crate::StoreError>) { + for nth in 1..=statements { + fault::arm(nth); + let answered = call(); + assert!( + !fault::armed(), + "statement {nth} of the call was never reached" + ); + assert!(answered.is_err(), "statement {nth} surfaces a store error"); + } +} +#[test] +fn every_read_surfaces_a_failing_statement() { + let (_db, store) = populated("fault-reads"); + let s = &store; + // The resolver's five row statements: targets, modules, symbols, + // uses and call refs fail one at a time. + surfaces(5, || s.resolve_input().map(|_| ())); + surfaces(1, || s.symbol_spans().map(|_| ())); + surfaces(1, || s.symbol_detail(1).map(|_| ())); + surfaces(1, || s.members(1).map(|_| ())); + surfaces(1, || s.symbol_facts(1).map(|_| ())); + surfaces(1, || s.symbol_call_refs(1).map(|_| ())); + surfaces(1, || s.score_call_refs(1).map(|_| ())); + surfaces(1, || s.module_rows().map(|_| ())); + surfaces(1, || s.file_target_counts().map(|_| ())); + surfaces(1, || s.use_rows().map(|_| ())); + surfaces(1, || s.import_targets().map(|_| ())); + surfaces(1, || s.symbol_modules().map(|_| ())); + surfaces(1, || s.ref_ids_of_kind("call").map(|_| ())); + surfaces(1, || s.rank_input().map(|_| ())); + surfaces(1, || s.rank_call_edges().map(|_| ())); + surfaces(1, || s.rank_import_edges().map(|_| ())); + surfaces(1, || s.stored_rank().map(|_| ())); + surfaces(1, || s.files().map(|_| ())); + surfaces(1, || s.map_rows().map(|_| ())); + surfaces(1, || s.edges_of_kind("call").map(|_| ())); + surfaces(1, || s.symbols_matching("alpha").map(|_| ())); + surfaces(1, || s.callers(1).map(|_| ())); + surfaces(1, || s.callees(1).map(|_| ())); + surfaces(1, || s.cochange_of("src/a.rs", 0.0, 5).map(|_| ())); + // The partner read walks the same statement twice, once per path. + surfaces(2, || { + s.cochange_partners_for_files(&["src/a.rs".to_owned(), "src/b.rs".to_owned()], 0.0, 5) + .map(|_| ()) + }); + surfaces(1, || s.canonical_dump().map(|_| ())); +} +#[test] +fn every_writer_surfaces_a_failing_statement() { + let (_db, mut store) = populated("fault-writes"); + // The crate-graph writer's package row read fails like any other + // statement: the write answers an error and the tables it already + // wrote in this transaction roll back with it. + surfaces(1, || { + store.write_crate_graph(&[], &[], &[], &[]).map(|_| ()) + }); + // The failed write rolled back: the module the store already had is + // still there, and nothing half-written stayed. + assert_eq!(store.module_rows().expect("the graph still reads").len(), 1); + // The store still serves every read after the injected faults. + assert!(store.symbol_spans().expect("spans").len() >= 2); +} diff --git a/crates/phantom-fractal-store/src/tests/lift.rs b/crates/phantom-fractal-store/src/tests/lift.rs new file mode 100644 index 0000000..66a5b6b --- /dev/null +++ b/crates/phantom-fractal-store/src/tests/lift.rs @@ -0,0 +1,750 @@ +//! The W13 G1 coverage lift (SPEC-w13 G1, D8): the error surfaces, the +//! stamp and migration paths, the read-only guard, and the scorer +//! reads the bars ask for. Written from the SPEC and the behavior, +//! never from the lines. + +use std::fs; + +use phantom_fractal_lang_rust::RustLanguage; +use std::error::Error as _; + +use rusqlite::Connection; + +use super::{TempDb, assert_outside_workspace}; +use crate::{SCHEMA_VERSION, Store, StoreError, WriterLock}; + +/// One raw stamped database, as the legacy fixtures build them. +fn stamped(label: &str, extra_sql: &str) -> TempDb { + let db = TempDb::new(label); + assert_outside_workspace(db.path()); + let conn = Connection::open(db.path()).expect("sqlite"); + conn.execute_batch( + "CREATE TABLE meta(key TEXT PRIMARY KEY, value TEXT NOT NULL) STRICT; + INSERT INTO meta(key, value) VALUES ('producer', 'phantom-fractal');", + ) + .expect("meta"); + conn.execute_batch(extra_sql).expect("stamp"); + drop(conn); + db +} + +#[test] +fn store_error_displays_its_own_text_for_every_variant() { + let cases: Vec<(StoreError, &str)> = vec![ + ( + StoreError::Sqlite(rusqlite::Error::InvalidColumnName("x".to_owned())), + "sqlite: Invalid column name: x", + ), + (StoreError::Io(std::io::Error::other("boom")), "io: boom"), + (StoreError::MissingProducer, "database has no producer row"), + ( + StoreError::ForeignProducer, + "producer is not phantom-fractal", + ), + ( + StoreError::SchemaNewer { found: 9 }, + "schema_version 9 is newer than this binary", + ), + ( + StoreError::SchemaMismatch { + schema_version: 2, + user_version: 1, + }, + "user_version 1 does not match schema_version 2", + ), + (StoreError::UnreadableStamp, "schema stamp is not readable"), + ( + StoreError::ForeignDatabase, + "database is not a phantom-fractal index", + ), + (StoreError::UnknownTable, "table name is not allowed"), + ( + StoreError::Extract(phantom_fractal_lang::ExtractError::Parse), + "extract: parser returned no tree", + ), + ( + StoreError::UnknownKind("weird".to_owned()), + "unknown kind weird", + ), + ( + StoreError::UnknownVisibility("secret".to_owned()), + "unknown visibility secret", + ), + ( + StoreError::UnknownReason("because".to_owned()), + "unknown edge reason because", + ), + ( + StoreError::UnknownPackage("pkg".to_owned()), + "the graph has no package named pkg", + ), + ( + StoreError::UnknownTarget(7), + "the graph has no target with id 7", + ), + ( + StoreError::DataDirectory("repos".to_owned(), "is a symlink".to_owned()), + "repos: is a symlink", + ), + ( + StoreError::NotOwnedByUs("repos".to_owned(), "another uid".to_owned()), + "repos: another uid", + ), + ( + StoreError::LockHeld, + "another writer holds the database lock", + ), + ( + StoreError::ReadOnlyUnavailable, + "read-only open cannot proceed without a sidecar write", + ), + ( + StoreError::OwnFlagCount { + stored: 2, + flags: 3, + }, + "3 own test flags do not match the file's 2 symbols", + ), + ]; + for (error, text) in cases { + assert_eq!(error.to_string(), text, "{text}"); + } + // Only the wrapped causes carry a source. + assert!( + StoreError::Sqlite(rusqlite::Error::InvalidColumnName("x".to_owned())) + .source() + .is_some() + ); + assert!( + StoreError::Io(std::io::Error::other("boom")) + .source() + .is_some() + ); + assert!( + StoreError::Extract(phantom_fractal_lang::ExtractError::Parse) + .source() + .is_some() + ); + assert!(StoreError::LockHeld.source().is_none()); + assert!(StoreError::UnknownTarget(7).source().is_none()); +} + +#[test] +fn an_unreadable_stamp_is_refused_in_every_shape() { + for (sql, why) in [ + ("", "no version row"), + ( + "INSERT INTO meta(key, value) VALUES ('schema_version', 'abc'); + PRAGMA user_version = 2;", + "a version that is not a number", + ), + ( + "INSERT INTO meta(key, value) VALUES ('schema_version', '0'); + PRAGMA user_version = 0;", + "a version the binary does not carry", + ), + ] { + let db = stamped("unreadable", sql); + let opened = Store::open(db.path()).err().expect("refused"); + assert!(matches!(opened, StoreError::UnreadableStamp), "{why}"); + } +} + +#[test] +fn a_user_version_that_disagrees_with_the_stamp_is_refused() { + let db = stamped( + "mismatch", + "INSERT INTO meta(key, value) VALUES ('schema_version', '3'); + PRAGMA user_version = 2;", + ); + let opened = Store::open(db.path()).err().expect("refused"); + assert!(matches!( + opened, + StoreError::SchemaMismatch { + schema_version: 3, + user_version: 2, + } + )); +} + +#[test] +fn a_version_one_database_migrates_through_two_to_three() { + assert_eq!(SCHEMA_VERSION, 3); + let db = TempDb::new("v1"); + assert_outside_workspace(db.path()); + drop(Store::open(db.path()).expect("create")); + { + let conn = Connection::open(db.path()).expect("sqlite"); + conn.execute_batch( + "ALTER TABLE symbols DROP COLUMN scope; + ALTER TABLE symbols DROP COLUMN owner; + ALTER TABLE symbols DROP COLUMN owner_path; + ALTER TABLE symbols DROP COLUMN trait_name; + ALTER TABLE modules DROP COLUMN scope; + ALTER TABLE refs DROP COLUMN scope; + ALTER TABLE refs DROP COLUMN receiver; + ALTER TABLE refs DROP COLUMN shadowed; + ALTER TABLE refs DROP COLUMN alias; + ALTER TABLE refs DROP COLUMN glob; + UPDATE meta SET value = '1' WHERE key = 'schema_version'; + PRAGMA user_version = 1;", + ) + .expect("downgrade to version 1"); + } + let before = file_bytes_before(db.path()); + let store = Store::open(db.path()).expect("open migrates"); + assert_eq!(store.schema_version().expect("version"), 3); + assert_eq!(store.user_version().expect("user_version"), 3); + drop(store); + // The migration wrote the database, unlike the refusal cases. + assert_ne!(file_bytes_before(db.path()), before); +} + +#[test] +fn a_read_only_open_refuses_a_stamp_it_would_migrate() { + let db = stamped( + "v2ro", + "INSERT INTO meta(key, value) VALUES ('schema_version', '2'); + PRAGMA user_version = 2;", + ); + let opened = Store::open_read_only(db.path()).err().expect("refused"); + assert!(matches!( + opened, + StoreError::SchemaMismatch { + schema_version: 2, + user_version: 2, + } + )); +} + +#[test] +fn the_read_only_guard_answers_the_writer_and_the_sidecars() { + let db = TempDb::new("guard"); + assert_outside_workspace(db.path()); + Store::open(db.path()).expect("fresh store"); + let store = Store::open_read_only(db.path()).expect("read-only open"); + assert!(store.read_only_still_valid().expect("valid")); + + // A writer taking the lock makes the reads possibly racy. + let lock = WriterLock::acquire(db.path()).expect("lock"); + assert!(!store.read_only_still_valid().expect("held")); + drop(lock); + assert!(store.read_only_still_valid().expect("free again")); + + // A sidecar beside the database does the same. + fs::write(format!("{}-wal", db.path().display()), b"").expect("wal"); + assert!(!store.read_only_still_valid().expect("sidecar")); + drop(store); +} + +#[test] +fn the_scorer_reads_count_the_macro_argument_and_unresolved_calls() { + let db = TempDb::new("refs"); + assert_outside_workspace(db.path()); + let mut store = Store::open(db.path()).expect("open"); + let source = "pub fn one() {}\n\ + pub fn plain_caller() {\n one();\n}\n\ + #[test]\nfn macro_caller() {\n assert_eq!(one(), 0);\n}\n"; + store + .put_source("refs.rs", source, 1, &RustLanguage::new().expect("grammar")) + .expect("put"); + // The plain call carries no edge (put_source writes refs, never + // resolve), and the macro argument is its own ref kind. + assert_eq!(store.unresolved_named("one").expect("unresolved"), 1); + assert_eq!(store.macro_args_named("one").expect("macro args"), 1); +} + +fn file_bytes_before(path: &std::path::Path) -> Vec { + fs::read(path).expect("read database") +} + +#[test] +fn the_scorer_reads_answer_their_store_facts() { + use crate::Package; + use phantom_fractal_lang::{Id, Link, LinkKind, LinkReason, Module, Node, Target, TargetKind}; + + let db = TempDb::new("scoring"); + assert_outside_workspace(db.path()); + let mut store = Store::open(db.path()).expect("open"); + let language = RustLanguage::new().expect("grammar"); + store + .put_source( + "src/lib.rs", + "mod helper;\npub use helper::f;\nuse helper::*;\npub fn caller() {\n f();\n}\n", + 1, + &language, + ) + .expect("put lib"); + store + .put_source("src/helper.rs", "pub fn f() {}\n", 1, &language) + .expect("put helper"); + + // One package, two targets (the lib and a test target that also + // claims src/lib.rs), and the modules the declarations walk gives. + let package = Package { + name: "fixture".to_owned(), + version: Some("0.0.0".to_owned()), + manifest_path: "/w/Cargo.toml".to_owned(), + }; + let lib_target = Target { + id: 1, + package: "fixture".to_owned(), + name: "fixture".to_owned(), + kind: TargetKind::Lib, + root: "src/lib.rs".to_owned(), + }; + let test_target = Target { + id: 2, + package: "fixture".to_owned(), + name: "lib".to_owned(), + kind: TargetKind::Test, + root: "src/lib.rs".to_owned(), + }; + let root_mod = Module { + id: 10, + target: 1, + parent: None, + path: "crate".to_owned(), + file: "src/lib.rs".to_owned(), + scope: String::new(), + }; + let helper_mod = Module { + id: 11, + target: 1, + parent: Some(10), + path: "crate::helper".to_owned(), + file: "src/helper.rs".to_owned(), + scope: String::new(), + }; + let shared_mod = Module { + id: 12, + target: 2, + parent: None, + path: "crate".to_owned(), + file: "src/lib.rs".to_owned(), + scope: String::new(), + }; + store + .write_crate_graph( + &[package], + &[lib_target, test_target], + &[root_mod, helper_mod, shared_mod], + &[], + ) + .expect("crate graph"); + + // The import edge the root package's re-export write gives: the + // module to the symbol, through the `use` ref. + let f_id: Id = store + .symbol_spans() + .expect("spans") + .iter() + .find(|span| span.name == "f" && span.path == "src/helper.rs") + .expect("f span") + .id; + let use_rows = store.use_rows().expect("uses"); + let reexport_ref = use_rows + .iter() + .find(|row| !row.glob && row.text == "helper::f") + .expect("the re-export use ref") + .id; + let root_row = store + .module_rows() + .expect("modules") + .iter() + .find(|(id, row)| { + row.file_id.is_some() && row.path == "crate" && { + let _ = id; + true + } + }) + .map(|(id, _)| *id) + .expect("root module row"); + store + .write_links(&[Link { + kind: LinkKind::Import, + source: Node::Module(root_row), + target: Node::Symbol(f_id), + confidence: 0.9, + reason: LinkReason::UseAlias, + ref_id: Some(reexport_ref), + }]) + .expect("links"); + + // module_rows: every row with its target, parent, path and file. + let rows = store.module_rows().expect("module rows"); + assert_eq!(rows.len(), 3); + let lib_file = rows + .iter() + .find(|(_, row)| row.path == "crate" && row.target_id == 1) + .and_then(|(_, row)| row.file_id) + .expect("lib file id"); + assert_eq!( + rows.get(&root_row).and_then(|row| row.file_id), + Some(lib_file) + ); + // file_target_counts: src/lib.rs is claimed by two targets. + let counts = store.file_target_counts().expect("counts"); + assert_eq!(counts.get(&lib_file), Some(&2)); + // use_rows: the two root uses and the caller's file. + assert_eq!(use_rows.len(), 2); + assert!(use_rows.iter().any(|row| row.glob)); + // import_targets: the re-export's ref id answers its symbol target. + let imports = store.import_targets().expect("imports"); + assert_eq!( + imports.get(&reexport_ref), + Some(&("symbol".to_owned(), f_id)) + ); + // symbol_modules: f's row is the helper module. + let helper_row = *rows + .iter() + .find(|(_, row)| row.path == "crate::helper") + .map(|(id, _)| id) + .expect("helper row"); + let symbol_modules = store.symbol_modules().expect("symbol modules"); + assert_eq!(symbol_modules.get(&f_id), Some(&helper_row)); + // score_call_refs: the caller carries its call ref with its scope. + let caller_id: Id = store + .symbol_spans() + .expect("spans") + .iter() + .find(|span| span.name == "caller") + .expect("caller span") + .id; + let refs = store.score_call_refs(caller_id).expect("refs"); + assert_eq!(refs.len(), 1); + assert_eq!(refs[0].kind, "call"); + assert_eq!(refs[0].text, "f"); + assert_eq!(refs[0].scope, ""); + + // The impact reads the scorer leans on: the file's module claim, + // and the caller's refs with their edge marks. + assert!(store.file_has_module(lib_file).expect("claimed")); + let helper_file = rows + .get(&helper_row) + .and_then(|row| row.file_id) + .expect("helper file"); + let impact_refs = store.symbol_call_refs(caller_id).expect("impact refs"); + assert_eq!(impact_refs.len(), 1); + assert_eq!(impact_refs[0].kind, "call"); + assert!(!impact_refs[0].placed); + let _ = helper_file; + let _ = lib_file; +} + +/// Every stored-text decoder refuses a value this slice does not write, +/// and the crate-graph writer refuses a target or module that names a +/// row it was not given. +#[test] +fn stored_text_decoders_refuse_values_this_slice_never_writes() { + use phantom_fractal_lang::{SymbolKind, Target, TargetKind}; + + assert!(matches!( + crate::graph::symbol_kind_from_text("weird"), + Err(StoreError::UnknownKind(value)) if value == "weird" + )); + assert!(matches!( + crate::graph::target_kind_from_text("weird"), + Err(StoreError::UnknownKind(value)) if value == "weird" + )); + assert!(matches!( + crate::graph::reason_from_text("because"), + Err(StoreError::UnknownReason(value)) if value == "because" + )); + assert!(matches!( + crate::rows::ref_kind_from_text("weird"), + Err(StoreError::UnknownKind(value)) if value == "weird" + )); + assert!(matches!( + crate::rows::visibility_from_text("secret"), + Err(StoreError::UnknownVisibility(value)) if value == "secret" + )); + // The known texts read back, so the refusals are the odd ones out. + assert_eq!( + crate::graph::symbol_kind_from_text("function").expect("known kind"), + SymbolKind::Function + ); + assert_eq!( + crate::graph::target_kind_from_text("lib").expect("known target"), + TargetKind::Lib + ); + + let db = TempDb::new("poison-graph"); + assert_outside_workspace(db.path()); + let mut store = Store::open(db.path()).expect("open"); + // A target that names no package. + let stray_target = Target { + id: 1, + package: "missing".to_owned(), + name: "missing".to_owned(), + kind: TargetKind::Lib, + root: "src/lib.rs".to_owned(), + }; + assert!(matches!( + store.write_crate_graph(&[], &[stray_target], &[], &[]), + Err(StoreError::UnknownPackage(name)) if name == "missing" + )); + // A module that names no target. + let package = crate::Package { + name: "fixture".to_owned(), + version: Some("0.0.0".to_owned()), + manifest_path: "/w/Cargo.toml".to_owned(), + }; + let module = phantom_fractal_lang::Module { + id: 5, + target: 99, + parent: None, + path: "crate".to_owned(), + file: "src/lib.rs".to_owned(), + scope: String::new(), + }; + assert!(matches!( + store.write_crate_graph(&[package], &[], &[module], &[]), + Err(StoreError::UnknownTarget(99)) + )); +} + +/// Poisoned rows answer the decoder errors through the readers: a +/// symbol of an unknown kind, a ref of an unknown kind, and an edge of +/// an unknown reason. +#[test] +fn poisoned_rows_answer_the_decoder_errors_through_the_readers() { + let db = TempDb::new("poison-rows"); + assert_outside_workspace(db.path()); + let mut store = Store::open(db.path()).expect("open"); + let language = RustLanguage::new().expect("grammar"); + store + .put_source("src/lib.rs", "pub fn one() {}\n", 1, &language) + .expect("put"); + // A symbol row whose kind text is not one this slice writes. + store + .conn + .execute("UPDATE symbols SET kind = 'weird' WHERE name = 'one'", []) + .expect("poison symbol"); + assert!(matches!( + store.read_items("src/lib.rs"), + Err(StoreError::UnknownKind(value)) if value == "weird" + )); + // A ref row with an unknown kind answers the resolve-input read. + store + .conn + .execute( + "INSERT INTO refs(file_id, kind, text, last_segment, line, col, scope) + SELECT id, 'weird', 'x', 'x', 1, 1, '' FROM files LIMIT 1", + [], + ) + .expect("poison ref"); + assert!(matches!( + store.resolve_input(), + Err(StoreError::UnknownKind(value)) if value == "weird" + )); + // An edge row with an unknown reason answers the edge read. + store + .conn + .execute("DELETE FROM refs WHERE kind = 'weird'", []) + .expect("clean ref"); + store + .conn + .execute( + "UPDATE symbols SET kind = 'function' WHERE name = 'one'", + [], + ) + .expect("repair symbol"); + let spans = store.symbol_spans().expect("spans"); + let one = spans + .iter() + .find(|span| span.name == "one") + .expect("one") + .id; + let call_ref: i64 = store + .conn + .query_row( + "INSERT INTO refs(file_id, from_symbol, kind, text, last_segment, line, col, scope) + SELECT file_id, id, 'call', 'one', 'one', 1, 1, '' FROM symbols + WHERE name = 'one' RETURNING id", + [], + |row| row.get(0), + ) + .expect("call ref"); + store + .conn + .execute( + "INSERT INTO edges(kind, src_kind, src_id, dst_kind, dst_id, confidence, reason, ref_id) + VALUES ('call', 'symbol', ?1, 'symbol', ?1, 0.9, 'because', ?2)", + rusqlite::params![one, call_ref], + ) + .expect("poison edge"); + assert!(matches!( + store.callees(one), + Err(StoreError::UnknownReason(value)) if value == "because" + )); +} + +/// Every reader answers a real `Sqlite` error when its tables are gone: +/// the arms behind the `?` on each statement, exercised through the +/// public reads. +#[test] +fn readers_answer_the_sqlite_error_when_their_tables_are_gone() { + let db = TempDb::new("tables-gone"); + assert_outside_workspace(db.path()); + let mut store = Store::open(db.path()).expect("open"); + let language = RustLanguage::new().expect("grammar"); + store + .put_source( + "src/lib.rs", + "pub fn one() {}\npub fn two() {}\n", + 1, + &language, + ) + .expect("put"); + store + .conn + .execute_batch( + "DROP TRIGGER IF EXISTS symbols_ai; + DROP TRIGGER IF EXISTS symbols_ad; + DROP TRIGGER IF EXISTS symbols_au; + PRAGMA foreign_keys = OFF;", + ) + .expect("drop triggers"); + let tables: Vec = store + .conn + .prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name <> 'meta'") + .expect("list") + .query_map([], |row| row.get(0)) + .expect("rows") + .collect::>() + .expect("tables"); + for table in &tables { + store + .conn + .execute_batch(&format!("DROP TABLE IF EXISTS \"{table}\";")) + .expect("drop"); + } + + // Every read that touches a dropped table answers a store error, + // never a panic. + assert!(store.symbol_spans().is_err()); + assert!(store.read_items("src/lib.rs").is_err()); + assert!(store.file_paths().is_err()); + assert!(store.file_stats().is_err()); + assert!(store.edges_of_kind("call").is_err()); + assert!(store.resolve_input().is_err()); + assert!(store.symbol_modules().is_err()); + assert!(store.module_rows().is_err()); + assert!(store.file_target_counts().is_err()); + assert!(store.use_rows().is_err()); + assert!(store.import_targets().is_err()); + assert!(store.package_dirs().is_err()); + assert!(store.cochange_scan().is_err()); + assert!(store.rank_input().is_err()); + assert!(store.canonical_dump().is_err()); + assert!(store.symbol_by_stable_key("x").is_err()); + assert!(store.match_count("one").is_err()); + assert!(store.row_count("files").is_err()); + assert!(store.symbol_facts(1).is_err()); + assert!(store.symbol_call_refs(1).is_err()); + assert!(store.macro_args_named("one").is_err()); + assert!(store.unresolved_named("one").is_err()); + assert!(store.file_has_module(1).is_err()); + assert!(store.ref_texts("src/lib.rs").is_err()); + assert!(store.symbol_detail(1).is_err()); + assert!(store.members(1).is_err()); + assert!(store.map_rows().is_err()); + assert!(store.rank_call_edges().is_err()); + assert!(store.rank_import_edges().is_err()); + // The writers answer their errors through the same arms, with one + // row each so their insert paths run against the broken schema. + let package = crate::Package { + name: "gone".to_owned(), + version: Some("0.0.0".to_owned()), + manifest_path: "/w/Cargo.toml".to_owned(), + }; + let target = phantom_fractal_lang::Target { + id: 1, + package: "gone".to_owned(), + name: "gone".to_owned(), + kind: phantom_fractal_lang::TargetKind::Lib, + root: "src/lib.rs".to_owned(), + }; + let module = phantom_fractal_lang::Module { + id: 2, + target: 1, + parent: None, + path: "crate".to_owned(), + file: "src/lib.rs".to_owned(), + scope: String::new(), + }; + assert!( + store + .write_crate_graph(&[package], &[target], &[module], &[]) + .is_err() + ); + assert!( + store + .replace_edges(&[phantom_fractal_lang::Edge { + ref_id: 1, + source: 1, + target: 1, + confidence: 0.9, + reason: phantom_fractal_lang::Reason::Local, + }]) + .is_err() + ); + assert!( + store + .write_links(&[phantom_fractal_lang::Link { + kind: phantom_fractal_lang::LinkKind::Import, + source: phantom_fractal_lang::Node::Module(1), + target: phantom_fractal_lang::Node::Symbol(1), + confidence: 0.9, + reason: phantom_fractal_lang::LinkReason::Local, + ref_id: Some(1), + }]) + .is_err() + ); + assert!(store.remove_file("src/lib.rs").is_err()); + assert!( + store + .put_source("src/lib.rs", "pub fn one() {}\n", 2, &language) + .is_err() + ); + // The reads and writers the co-change, rank, map and neighbour + // families answer through the same arms. + // A partner read over a path whose file row is gone answers an + // empty list, never an error. + assert!( + store + .cochange_of("src/lib.rs", 0.0, 5) + .expect("no file row") + .is_empty() + ); + assert!( + store + .cochange_partners_for_files(&["src/lib.rs".to_owned()], 0.0, 5) + .expect("no file row") + .iter() + .all(|group| group.partners.is_empty()) + ); + assert!( + store + .write_cochange(&crate::CochangeScan::default()) + .is_err() + ); + assert!(store.stored_rank().is_err()); + assert!(store.files().is_err()); + assert!(store.map_file_paths().is_err()); + assert!(store.write_rank(1, &[], &[]).is_err()); + assert!(store.symbols_matching("one").is_err()); + assert!(store.callers(1).is_err()); + assert!(store.callees(1).is_err()); + assert!(store.unresolved_calls(1).is_err()); + assert!(store.write_method_owners(&[(1, 1)]).is_err()); + assert!( + store + .reset_symbol_test_flags("src/lib.rs", &[true]) + .is_err() + ); + assert!(store.score_call_refs(1).is_err()); + assert!(store.ref_ids_of_kind("call").is_err()); + assert!(store.stored_blake3("src/lib.rs").is_err()); +} diff --git a/crates/phantom-fractal-store/src/tests/lock.rs b/crates/phantom-fractal-store/src/tests/lock.rs index ea6bc95..ff1fbc0 100644 --- a/crates/phantom-fractal-store/src/tests/lock.rs +++ b/crates/phantom-fractal-store/src/tests/lock.rs @@ -47,6 +47,7 @@ fn wait_for_held(child: &mut std::process::Child, marker: &std::path::Path) { #[test] fn acquire_creates_an_empty_0600_file_and_releases_on_drop() { + let _serial = super::serial(); let db = TempDb::new("lockfile"); assert_outside_workspace(db.path()); let lock_path = crate::lock::lock_path(db.path()); @@ -72,6 +73,7 @@ fn acquire_creates_an_empty_0600_file_and_releases_on_drop() { #[test] fn two_threads_contend_and_the_loser_gets_lock_held() { + let _serial = super::serial(); let db = TempDb::new("lock2thread"); assert_outside_workspace(db.path()); // The chain is born outside the timed window on a loaded runner: @@ -108,6 +110,7 @@ fn two_threads_contend_and_the_loser_gets_lock_held() { #[test] fn two_processes_contend_and_the_loser_gets_lock_held() { + let _serial = super::serial(); let db = TempDb::new("lock2proc"); assert_outside_workspace(db.path()); let marker = std::env::temp_dir().join(format!( @@ -147,6 +150,7 @@ fn two_processes_contend_and_the_loser_gets_lock_held() { #[test] fn killed_holder_frees_the_lock() { + let _serial = super::serial(); let db = TempDb::new("lockkill"); assert_outside_workspace(db.path()); let marker = std::env::temp_dir().join(format!( @@ -173,6 +177,7 @@ fn killed_holder_frees_the_lock() { #[test] fn lock_state_never_creates_the_file() { + let _serial = super::serial(); let db = TempDb::new("lockstate"); assert_outside_workspace(db.path()); let lock_path = crate::lock::lock_path(db.path()); @@ -188,6 +193,7 @@ fn lock_state_never_creates_the_file() { /// the environment answers at once. #[test] fn spawned_child_holds_the_lock() { + let _serial = super::serial(); let Ok(db) = std::env::var("PHA_STORE_TEST_LOCK_DB") else { return; }; @@ -204,6 +210,7 @@ fn spawned_child_holds_the_lock() { #[test] fn acquire_creates_the_0700_chain_and_holds() { + let _serial = super::serial(); let data = TempData::new("lockchain"); // No chain exists yet: only the lock file is here, and the chain // Store::open would have created is born around it — at `0700`, @@ -232,6 +239,7 @@ fn acquire_creates_the_0700_chain_and_holds() { #[test] fn acquire_refuses_a_broken_chain_like_the_store() { + let _serial = super::serial(); use std::fs; let data = TempData::new("lockshape"); fs::create_dir_all(data.root.join("repos")).expect("repos"); @@ -298,6 +306,7 @@ fn file_state(path: &std::path::Path) -> ((i64, i64), u32, Vec) { #[test] fn acquire_refuses_a_symlink_and_spares_its_target() { + let _serial = super::serial(); let (data, lock) = bare_repo("roloclink"); let target = data.repo_dir().join("scout"); std::fs::write(&target, b"the target bytes").expect("target"); @@ -324,6 +333,7 @@ fn acquire_refuses_a_symlink_and_spares_its_target() { #[test] fn acquire_and_lock_state_refuse_a_dangling_symlink() { + let _serial = super::serial(); let (data, lock) = bare_repo("roloclink-dg"); std::os::unix::fs::symlink("nowhere", &lock).expect("dangling"); @@ -348,6 +358,7 @@ fn acquire_and_lock_state_refuse_a_dangling_symlink() { #[test] fn acquire_refuses_a_directory_at_the_lock_path() { + let _serial = super::serial(); let (data, lock) = bare_repo("rolocdir"); std::fs::create_dir(&lock).expect("dir at the lock path"); let refused = WriterLock::acquire(&data.db()).expect_err("refused"); @@ -360,6 +371,7 @@ fn acquire_refuses_a_directory_at_the_lock_path() { #[test] fn lock_state_refuses_a_directory_and_a_link_to_one() { + let _serial = super::serial(); // The FIFO case takes the same `is_file()` false a directory or a // link to one takes: both are non-regular entries at the lock // path, and QA plants a real FIFO against the same refusal. @@ -391,6 +403,7 @@ fn lock_state_refuses_a_directory_and_a_link_to_one() { /// roots; without the tool the case skips, never fakes. #[test] fn a_real_fifo_at_the_lock_path_is_refused_without_a_hang() { + let _serial = super::serial(); let (data, lock) = bare_repo("rolocfifo"); let planted = Command::new("mkfifo") .arg(&lock) @@ -453,6 +466,7 @@ fn a_real_fifo_at_the_lock_path_is_refused_without_a_hang() { /// QA uses to plant its own between-checks shapes. #[test] fn a_fifo_racing_the_open_is_refused_without_a_hang() { + let _serial = super::serial(); let (data, lock) = bare_repo("rolockrace"); let db = data.db(); let lock_for_hook = lock.clone(); @@ -498,6 +512,7 @@ fn a_fifo_racing_the_open_is_refused_without_a_hang() { /// refusal, never an absolute path and never a follow. #[test] fn a_symlink_racing_the_open_is_refused_as_a_lock_file() { + let _serial = super::serial(); use crate::StoreError; let (data, lock) = bare_repo("rolocelink"); @@ -538,3 +553,262 @@ fn a_symlink_racing_the_open_is_refused_as_a_lock_file() { let _ = std::fs::remove_file(&lock); let _ = std::fs::remove_file(data.repo_dir().join("target.rs")); } + +/// A bare database name has no parent directory, and the lock path is +/// then the bare name itself. +#[test] +fn a_bare_database_name_keeps_a_bare_lock_path() { + let _serial = super::serial(); + assert_eq!( + crate::lock::lock_path(std::path::Path::new("graph.db")), + std::path::PathBuf::from("graph.db.lock") + ); +} + +/// A wide lock file is narrowed to `0600` on the way in, and a link +/// swapped in after the pre-open check is re-verified and refused by +/// the reopen path's own identity match. +#[test] +fn a_wide_lock_file_is_narrowed_and_a_swapped_in_file_is_refused() { + let _serial = super::serial(); + use std::os::unix::fs::PermissionsExt; + + let (data, lock) = bare_repo("rolocwide"); + let db = data.db(); + let held = WriterLock::acquire(&db).expect("first lock"); + drop(held); + fs::set_permissions(&lock, fs::Permissions::from_mode(0o666)).expect("widen"); + drop(WriterLock::acquire(&db).expect("second lock")); + let mode = fs::metadata(&lock).expect("metadata").permissions().mode() & 0o777; + assert_eq!(mode, 0o600, "the lock file was not narrowed"); + + // The hook swaps in a different regular file after the shape + // check: the exclusive create sees it, and the reopen's identity + // match refuses it. + let raced = std::sync::atomic::AtomicBool::new(false); + let raced_once = std::sync::Arc::new(raced); + let raced_hook = raced_once.clone(); + let hook_target = lock.clone(); + let hook: Box = Box::new(move |_| { + if !raced_hook.swap(true, std::sync::atomic::Ordering::SeqCst) { + let _ = fs::remove_file(&hook_target); + let _ = fs::write(&hook_target, b"swapped"); + fs::set_permissions(&hook_target, fs::Permissions::from_mode(0o600)).ok(); + } + }); + crate::lock::race_lock_window(Some(hook)); + let taken = std::thread::scope(|scope| { + let (tx, rx) = std::sync::mpsc::channel(); + scope.spawn(move || { + let _ = tx.send(WriterLock::acquire(&db).is_ok()); + }); + rx.recv_timeout(Duration::from_secs(2)) + }); + crate::lock::race_lock_window(None); + // A regular file swapped in after the check is re-verified and + // adopted through the reopen path, and its bytes are never + // touched — the lock file carries no content. + assert!(taken.expect("the swap race hung past two seconds")); + assert_eq!(fs::read(&lock).expect("contents"), b"swapped"); + let _ = fs::remove_file(&lock); +} + +/// A repository directory the process cannot stat answers the store's +/// own error rather than a raw `io` one. +#[test] +fn an_unreadable_repository_directory_refuses_the_lock() { + let _serial = super::serial(); + use std::os::unix::fs::PermissionsExt; + + let (data, _lock) = bare_repo("rolocnoread"); + let db = data.db(); + let repo = data.repo_dir(); + fs::set_permissions(&repo, fs::Permissions::from_mode(0o000)).expect("close"); + // The process must be unable to read the directory for the refusal + // to exist at all: uid 0 reads a mode-000 directory, so the probe + // decides, and the test says why it stood down instead of failing. + if fs::read_dir(&repo).is_ok() { + fs::set_permissions(&repo, fs::Permissions::from_mode(0o700)).expect("reopen"); + eprintln!( + "skipping: this process still reads a mode-000 directory (uid 0), \ + so the unreadable case cannot be staged" + ); + return; + } + let opened = WriterLock::acquire(&db); + fs::set_permissions(&repo, fs::Permissions::from_mode(0o700)).expect("reopen"); + assert!( + matches!( + opened, + Err(StoreError::Io(_) | StoreError::DataDirectory(..) | StoreError::NotOwnedByUs(..)) + ), + "{opened:?}" + ); +} + +/// The second race window: a regular file is planted after the pre-open +/// check (the exclusive create then answers `AlreadyExists`), and the +/// reopen's window swaps a different regular file in — the identity +/// match refuses it. +#[test] +fn a_second_swap_between_the_reverify_and_the_reopen_is_refused() { + let _serial = super::serial(); + use std::os::unix::fs::PermissionsExt; + + let (data, lock) = bare_repo("rolocswap2"); + let db = data.db(); + let shots = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0)); + let shots_hook = shots.clone(); + let hook_target = lock.clone(); + let hook: Box = Box::new(move |_| { + let shot = shots_hook.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + match shot { + 0 => { + fs::write(&hook_target, b"first").expect("plant"); + fs::set_permissions(&hook_target, fs::Permissions::from_mode(0o600)).expect("mode"); + } + 1 => { + // A file written first and renamed over the lock is a + // different inode for certain: the two coexisted, so the + // number cannot be reused. Removing first would let the + // filesystem hand the freed number straight back. + let replacement = hook_target.with_extension("second"); + fs::write(&replacement, b"second").expect("swap"); + fs::set_permissions(&replacement, fs::Permissions::from_mode(0o600)).expect("mode"); + fs::rename(&replacement, &hook_target).expect("over"); + } + _ => {} + } + }); + crate::lock::race_lock_window(Some(hook)); + let refused = std::thread::scope(|scope| { + let (tx, rx) = std::sync::mpsc::channel(); + scope.spawn(move || { + let _ = tx.send(WriterLock::acquire(&db).err()); + }); + rx.recv_timeout(Duration::from_secs(2)) + }); + crate::lock::race_lock_window(None); + let refused = refused + .expect("the second swap hung past two seconds") + .expect("refused"); + assert_lock_refusal(&refused, "changed between the check and the open"); + let _ = fs::remove_file(&lock); +} + +/// The reopen's window with a SYMLINK planted: the flagged targets fail +/// the open itself with `ELOOP`, mapped to the store's lock-file +/// refusal — never a raw `Io` error, never a follow. +#[test] +fn a_symlink_in_the_reopen_window_refuses_as_a_lock_file() { + let _serial = super::serial(); + use std::os::unix::fs::PermissionsExt; + + let (data, lock) = bare_repo("roloclink2"); + let db = data.db(); + let shots = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0)); + let shots_hook = shots.clone(); + let hook_target = lock.clone(); + let repo = data.repo_dir(); + let hook: Box = Box::new(move |_| { + let shot = shots_hook.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + match shot { + 0 => { + fs::write(&hook_target, b"first").expect("plant"); + fs::set_permissions(&hook_target, fs::Permissions::from_mode(0o600)).expect("mode"); + } + 1 => { + let target = repo.join("linktarget.rs"); + fs::write(&target, b"target").expect("target"); + fs::remove_file(&hook_target).expect("remove"); + std::os::unix::fs::symlink(&target, &hook_target).expect("link"); + } + _ => {} + } + }); + crate::lock::race_lock_window(Some(hook)); + let refused = std::thread::scope(|scope| { + let (tx, rx) = std::sync::mpsc::channel(); + scope.spawn(move || { + let _ = tx.send(WriterLock::acquire(&db).err()); + }); + rx.recv_timeout(Duration::from_secs(2)) + }); + crate::lock::race_lock_window(None); + let refused = refused + .expect("the reopen link hung past two seconds") + .expect("refused"); + assert_lock_refusal(&refused, "symlink or not a regular file"); + let _ = fs::remove_file(&lock); + let _ = fs::remove_file(data.repo_dir().join("linktarget.rs")); +} + +/// A path with no file name and no parent still answers a lock path: +/// the default database name, bare. +#[test] +fn a_root_like_path_answers_the_default_lock_name() { + let _serial = super::serial(); + assert_eq!( + crate::lock::lock_path(std::path::Path::new("/")), + std::path::PathBuf::from("graph.db.lock") + ); +} + +/// A lock file the process cannot open answers the store's `io` +/// error on the probe path rather than a panic. +#[test] +fn an_unreadable_lock_file_answers_the_io_error() { + let _serial = super::serial(); + use std::os::unix::fs::PermissionsExt; + + let (data, lock) = bare_repo("rolocnoreadfile"); + let db = data.db(); + drop(WriterLock::acquire(&db).expect("first lock")); + fs::set_permissions(&lock, fs::Permissions::from_mode(0o000)).expect("close"); + let probed = crate::lock_state(&db); + fs::set_permissions(&lock, fs::Permissions::from_mode(0o600)).expect("reopen"); + assert!( + matches!(probed, Err(StoreError::Io(_)) | Ok(crate::LockState::Free)), + "{probed:?}" + ); +} + +/// The reopen's window with the file GONE: the reopen answers the +/// store's own error through the open-error mapping, never a success +/// on a missing lock file. +#[test] +fn a_vanished_file_in_the_reopen_window_answers_the_open_error() { + let _serial = super::serial(); + use std::os::unix::fs::PermissionsExt; + + let (data, lock) = bare_repo("rolocvanish"); + let db = data.db(); + let shots = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0)); + let shots_hook = shots.clone(); + let hook_target = lock.clone(); + let hook: Box = Box::new(move |_| { + let shot = shots_hook.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + match shot { + 0 => { + fs::write(&hook_target, b"first").expect("plant"); + fs::set_permissions(&hook_target, fs::Permissions::from_mode(0o600)).expect("mode"); + } + 1 => { + fs::remove_file(&hook_target).expect("remove"); + } + _ => {} + } + }); + crate::lock::race_lock_window(Some(hook)); + let refused = std::thread::scope(|scope| { + let (tx, rx) = std::sync::mpsc::channel(); + scope.spawn(move || { + let _ = tx.send(WriterLock::acquire(&db).err()); + }); + rx.recv_timeout(Duration::from_secs(2)) + }); + crate::lock::race_lock_window(None); + let outcome = refused.expect("the vanish race hung past two seconds"); + assert!(outcome.is_some(), "{outcome:?}"); + let _ = fs::remove_file(&lock); +} diff --git a/crates/phantom-fractal-store/src/tests/mod.rs b/crates/phantom-fractal-store/src/tests/mod.rs index e7cc412..75cffbc 100644 --- a/crates/phantom-fractal-store/src/tests/mod.rs +++ b/crates/phantom-fractal-store/src/tests/mod.rs @@ -7,33 +7,49 @@ use std::fs; use std::path::{Path, PathBuf}; use std::process; -use std::time::{SystemTime, UNIX_EPOCH}; +use std::sync::atomic::{AtomicU32, Ordering}; +use std::time::UNIX_EPOCH; mod cochange; +mod defects; mod detail; mod dirs; mod dump; +mod faults; mod graph; mod hints; mod legacy; +mod lift; mod lock; mod meta; mod rank; mod readonly; +mod reads; mod writes; +/// One process-wide guard for the tests that take or observe a global +/// test hook (the lock race window, the directory swap window): plain +/// libtest runs tests in threads of one process, so two hook-taking +/// tests would otherwise share a hook. `cargo nextest` isolates +/// processes and does not need it; it costs nothing there. +static SERIAL: std::sync::Mutex<()> = std::sync::Mutex::new(()); + +fn serial() -> std::sync::MutexGuard<'static, ()> { + SERIAL + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) +} + struct TempDb { path: PathBuf, } impl TempDb { fn new(label: &str) -> Self { - let nanos = SystemTime::now() - .duration_since(UNIX_EPOCH) - .map(|dur| dur.as_nanos()) - .unwrap_or(0); + static COUNTER: AtomicU32 = AtomicU32::new(0); + let count = COUNTER.fetch_add(1, Ordering::Relaxed); let path = std::env::temp_dir().join(format!( - "phantom-fractal-store-{label}-{}-{nanos}.db", + "phantom-fractal-store-{label}-{}-{count}.db", process::id() )); Self { path } @@ -59,12 +75,10 @@ struct TempData { impl TempData { fn new(label: &str) -> Self { - let nanos = SystemTime::now() - .duration_since(UNIX_EPOCH) - .map(|dur| dur.as_nanos()) - .unwrap_or(0); + static COUNTER: AtomicU32 = AtomicU32::new(0); + let count = COUNTER.fetch_add(1, Ordering::Relaxed); let root = std::env::temp_dir().join(format!( - "phantom-fractal-data-{label}-{}-{nanos}", + "phantom-fractal-data-{label}-{}-{count}", process::id() )); Self { root } diff --git a/crates/phantom-fractal-store/src/tests/readonly.rs b/crates/phantom-fractal-store/src/tests/readonly.rs index 3aed5e4..4344687 100644 --- a/crates/phantom-fractal-store/src/tests/readonly.rs +++ b/crates/phantom-fractal-store/src/tests/readonly.rs @@ -111,6 +111,7 @@ fn assert_serves(store: &Store) { #[test] fn open_read_only_serves_the_getters_and_moves_nothing() { + let _serial = super::serial(); let data = written_store("rooclosed"); let before = snapshot(data.data()); let store = Store::open_read_only(&data.db()).expect("read-only open"); @@ -147,6 +148,7 @@ fn open_read_only_serves_the_getters_and_moves_nothing() { #[test] fn open_read_only_gates_on_sidecars_and_a_held_lock() { + let _serial = super::serial(); // A writer connection alive keeps `-wal` and `-shm` on disk; the // guarded read refuses them before any SQLite work. let data = written_store("rowal"); @@ -187,6 +189,7 @@ fn open_read_only_gates_on_sidecars_and_a_held_lock() { #[test] fn read_only_still_valid_mirrors_the_gate() { + let _serial = super::serial(); let data = written_store("rostale"); let store = Store::open_read_only(&data.db()).expect("read-only open"); assert!(store.read_only_still_valid().expect("valid")); @@ -199,6 +202,7 @@ fn read_only_still_valid_mirrors_the_gate() { #[test] fn open_read_only_needs_no_writable_index_dir_and_never_chmods() { + let _serial = super::serial(); let data = written_store("ro0755"); { // The guarded read never creates a sidecar, so an index dir @@ -233,6 +237,7 @@ fn open_read_only_needs_no_writable_index_dir_and_never_chmods() { #[test] fn open_read_only_refuses_a_schema_newer_database_without_a_touch() { + let _serial = super::serial(); let data = written_store("ronewer"); { // Stamp a newer version through a direct write connection: diff --git a/crates/phantom-fractal-store/src/tests/reads.rs b/crates/phantom-fractal-store/src/tests/reads.rs new file mode 100644 index 0000000..774094e --- /dev/null +++ b/crates/phantom-fractal-store/src/tests/reads.rs @@ -0,0 +1,691 @@ +//! Every row-mapping read answers the rows an index pass wrote (W13 G1): +//! the resolver's input, the rank and map rows, the per-symbol detail +//! and impact reads, the scorer's reads and the edge neighbours. One +//! fixture writes a crate graph over four files with two packages, a +//! lib, a bin and a test target, a method owner, a call edge, two +//! import links, an impl link, a rank and a co-change scan; each test +//! reads one family back. +use super::{TempDb, assert_outside_workspace}; +use crate::{CochangeScan, FileRow, Package, PairRow, Store}; +use phantom_fractal_lang::{ + Edge, Id, Link, LinkKind, LinkReason, Module, Node, Reason, RefKind, SymbolKind, Target, + TargetKind, +}; +use phantom_fractal_lang_rust::RustLanguage; +const LIB: &str = "\ +pub mod helper; +pub use helper::*; +use helper::three as triplet; +pub fn one() { + three(); +} +pub fn two() { + let _ = triplet(); + assert_eq!(one(), ()); +} +pub struct Holder { + field: u8, +} +impl Holder { + pub fn method(&self) { + two(); + } +} +pub trait Shape {} +impl Shape for Holder {} +"; +/// The whole fixture: the store it wrote, the ids its rows carry, and +/// the temp database that owns them. +struct Fixture { + _db: TempDb, + store: Store, + one: Id, + two: Id, + three: Id, + holder: Id, + shape: Id, + method: Id, + lib_file: i64, +} +/// One co-change scan over two of the four files, so a pair and a +/// rename read back. +fn scan() -> CochangeScan { + CochangeScan { + last_oid: Some("1".repeat(40)), + oldest_oid: Some("1".repeat(40)), + commits_seen: 3, + skipped_bulk: 1, + window_json: "{}".to_owned(), + files: vec![ + FileRow { + path: "src/lib.rs".to_owned(), + commits: 2.0, + last_time: 30, + renamed_from: Some("src/old.rs".to_owned()), + }, + FileRow { + path: "src/helper.rs".to_owned(), + commits: 1.0, + last_time: 20, + renamed_from: None, + }, + ], + pairs: vec![PairRow { + a: "src/lib.rs".to_owned(), + b: "src/helper.rs".to_owned(), + together: 1.0, + n_a: 2.0, + n_b: 1.0, + lift: 1.5, + conf_ab: 0.5, + conf_ba: 0.25, + last_oid: "1".repeat(40), + last_time: 30, + }], + } +} +fn populated(label: &str) -> Fixture { + let db = TempDb::new(label); + assert_outside_workspace(db.path()); + let mut store = Store::open(db.path()).expect("open"); + let language = RustLanguage::new().expect("grammar"); + store + .put_source("src/lib.rs", LIB, 1, &language) + .expect("put lib"); + store + .put_source("src/helper.rs", "pub fn three() {}\n", 1, &language) + .expect("put helper"); + store + .put_source("src/main.rs", "fn main() {\n one();\n}\n", 1, &language) + .expect("put bin"); + store + .put_source("tests/common/mod.rs", "pub fn shared() {}\n", 1, &language) + .expect("put shared"); + let packages = [ + Package { + name: "sample".to_owned(), + version: Some("0.0.0".to_owned()), + manifest_path: "/w/Cargo.toml".to_owned(), + }, + Package { + name: "nested".to_owned(), + version: None, + manifest_path: "/w/vendor/nested/Cargo.toml".to_owned(), + }, + ]; + let targets = [ + Target { + id: 1, + package: "sample".to_owned(), + name: "sample".to_owned(), + kind: TargetKind::Lib, + root: "src/lib.rs".to_owned(), + }, + Target { + id: 2, + package: "sample".to_owned(), + name: "sample".to_owned(), + kind: TargetKind::Bin, + root: "src/main.rs".to_owned(), + }, + Target { + id: 3, + package: "sample".to_owned(), + name: "shared".to_owned(), + kind: TargetKind::Test, + root: "tests/common/mod.rs".to_owned(), + }, + Target { + id: 4, + package: "nested".to_owned(), + name: "nested".to_owned(), + kind: TargetKind::Lib, + root: "vendor/nested/src/lib.rs".to_owned(), + }, + ]; + let modules = [ + Module { + id: 10, + target: 1, + parent: None, + path: "crate".to_owned(), + file: "src/lib.rs".to_owned(), + scope: String::new(), + }, + Module { + id: 11, + target: 1, + parent: Some(10), + path: "crate::helper".to_owned(), + file: "src/helper.rs".to_owned(), + scope: String::new(), + }, + Module { + id: 12, + target: 2, + parent: None, + path: "crate".to_owned(), + file: "src/main.rs".to_owned(), + scope: String::new(), + }, + Module { + id: 13, + target: 3, + parent: None, + path: "crate".to_owned(), + file: "tests/common/mod.rs".to_owned(), + scope: String::new(), + }, + ]; + // The test target's module is the only test mark: its file's + // symbols are tests, every other file's are not. + store + .write_crate_graph(&packages, &targets, &modules, &[13]) + .expect("graph"); + let input = store.resolve_input().expect("input"); + let id_of = |name: &str, file: &str| -> Id { + input + .symbols + .iter() + .find(|symbol| symbol.name == name && symbol.file == file) + .unwrap_or_else(|| panic!("no symbol {name} in {file}")) + .id + }; + let one = id_of("one", "src/lib.rs"); + let two = id_of("two", "src/lib.rs"); + let three = id_of("three", "src/helper.rs"); + let holder = id_of("Holder", "src/lib.rs"); + let shape = id_of("Shape", "src/lib.rs"); + let method = id_of("method", "src/lib.rs"); + let call = input + .refs + .iter() + .find(|call| call.kind == RefKind::Call && call.text == "three") + .expect("the call ref") + .id; + store + .replace_edges(&[Edge { + ref_id: call, + source: one, + target: three, + confidence: 0.9, + reason: Reason::PathExact, + }]) + .expect("edges"); + store + .write_method_owners(&[(method, holder)]) + .expect("owners"); + let uses = store.use_rows().expect("uses"); + let alias = uses + .iter() + .find(|row| row.alias.as_deref() == Some("triplet")) + .expect("the alias use") + .id; + let glob = uses.iter().find(|row| row.glob).expect("the glob use").id; + let module_rows = store.module_rows().expect("modules"); + let lib_root = module_rows + .iter() + .find(|(_, row)| row.target_id == 1 && row.path == "crate") + .map(|(id, _)| *id) + .expect("the lib root module"); + let helper_module = module_rows + .iter() + .find(|(_, row)| row.path == "crate::helper") + .map(|(id, _)| *id) + .expect("the helper module"); + store + .write_links(&[ + Link { + kind: LinkKind::Import, + source: Node::Module(lib_root), + target: Node::Symbol(three), + confidence: 0.9, + reason: LinkReason::UseAlias, + ref_id: Some(alias), + }, + Link { + kind: LinkKind::Import, + source: Node::Module(lib_root), + target: Node::Module(helper_module), + confidence: 0.8, + reason: LinkReason::UseAlias, + ref_id: Some(glob), + }, + Link { + kind: LinkKind::Impl, + source: Node::Symbol(holder), + target: Node::Symbol(shape), + confidence: 0.7, + reason: LinkReason::ImplTrait, + ref_id: None, + }, + ]) + .expect("links"); + let lib_file = store + .files() + .expect("files") + .into_iter() + .find(|file| file.path == "src/lib.rs") + .expect("the lib file") + .id; + store + .write_rank(2, &[(one, 1.0), (three, 0.5)], &[(lib_file, 0.25)]) + .expect("rank"); + store.write_cochange(&scan()).expect("cochange"); + Fixture { + _db: db, + store, + one, + two, + three, + holder, + shape, + method, + lib_file, + } +} +#[test] +fn resolve_input_carries_every_stored_row() { + let fixture = populated("readarms"); + let input = fixture.store.resolve_input().expect("input"); + // Targets: every row with its package and kind text. + assert_eq!(input.targets.len(), 4); + let bin = input + .targets + .iter() + .find(|target| target.kind == TargetKind::Bin) + .expect("the bin target"); + assert_eq!(bin.root, "src/main.rs"); + assert_eq!(bin.package, "sample"); + assert_eq!( + input + .targets + .iter() + .find(|target| target.kind == TargetKind::Test) + .expect("the test target") + .name, + "shared" + ); + assert_eq!( + input + .targets + .iter() + .find(|target| target.package == "nested") + .expect("the nested target") + .kind, + TargetKind::Lib + ); + // Modules: the parent link and the file each one claims. + let lib_target = input + .targets + .iter() + .find(|target| target.kind == TargetKind::Lib && target.package == "sample") + .expect("the lib target"); + let root = input + .modules + .iter() + .find(|module| module.target == lib_target.id && module.path == "crate") + .expect("the lib root module"); + let helper = input + .modules + .iter() + .find(|module| module.path == "crate::helper") + .expect("the helper module"); + assert_eq!(helper.parent, Some(root.id)); + assert_eq!(helper.file, "src/helper.rs"); + // Symbols: kinds decode back, and the owner and module are stored. + let method = input + .symbols + .iter() + .find(|symbol| symbol.id == fixture.method) + .expect("the method symbol"); + assert_eq!(method.kind, SymbolKind::Method); + assert_eq!(method.name, "method"); + assert_eq!(method.module, Some(helper.parent.expect("lib root"))); + assert_eq!( + input + .symbols + .iter() + .find(|symbol| symbol.id == fixture.holder) + .expect("the holder") + .kind, + SymbolKind::Struct + ); + assert_eq!( + input + .symbols + .iter() + .find(|symbol| symbol.id == fixture.shape) + .expect("the trait") + .kind, + SymbolKind::Trait + ); + // Uses: the glob's path stops before `*`, and the alias rides. + assert!( + input + .uses + .iter() + .any(|binding| binding.glob && binding.path == "helper") + ); + assert!( + input + .uses + .iter() + .any(|binding| binding.alias.as_deref() == Some("triplet")) + ); + // Call refs: the path call the edge resolved, and the macro argument. + let call = input + .refs + .iter() + .find(|call| call.kind == RefKind::Call && call.text == "three") + .expect("the call ref"); + assert_eq!(call.from, Some(fixture.one)); + assert_eq!(call.file, "src/lib.rs"); + assert!(input.refs.iter().any(|call| call.kind == RefKind::Macro)); +} +#[test] +fn rank_and_map_reads_answer_a_ranked_store() { + let fixture = populated("readrank"); + let rank_input = fixture.store.rank_input().expect("rank input"); + // `fn main` in a bin's root file is the one main row. + let mains: Vec<_> = rank_input + .iter() + .filter(|row| row.is_main) + .map(|row| row.path.as_str()) + .collect(); + assert_eq!(mains, vec!["src/main.rs"]); + let main = rank_input + .iter() + .find(|row| row.is_main) + .expect("the main row"); + assert_eq!(main.target_kind.as_deref(), Some("bin")); + let one = rank_input + .iter() + .find(|row| row.id == fixture.one) + .expect("the `one` row"); + assert_eq!(one.target_kind.as_deref(), Some("lib")); + assert_eq!(one.kind, "function"); + assert_eq!(one.visibility, "public"); + assert!(!one.is_test); + // The test target's file carries the test mark. + assert!( + rank_input + .iter() + .any(|row| row.path == "tests/common/mod.rs" && row.is_test) + ); + // Call and impl edges ship their kind and confidence. + let calls = fixture.store.rank_call_edges().expect("call edges"); + let call = calls + .iter() + .find(|edge| edge.kind == "call") + .expect("the call edge"); + assert_eq!((call.src_id, call.dst_id), (fixture.one, fixture.three)); + assert!((call.confidence - 0.9).abs() < f64::EPSILON); + assert!(calls.iter().any(|edge| edge.kind == "impl")); + // Import edges land on files: one symbol end, one module end. + let imports = fixture.store.rank_import_edges().expect("import edges"); + assert_eq!(imports.len(), 2); + assert!( + imports + .iter() + .all(|edge| edge.src_file_id == fixture.lib_file) + ); + assert!( + imports + .iter() + .filter(|edge| edge.dst_file_id != fixture.lib_file) + .count() + == 2 + ); + // The stored rank table keeps both node kinds. + let stored = fixture.store.stored_rank().expect("stored rank"); + assert_eq!(stored.len(), 3); + assert!( + stored + .iter() + .any(|(kind, id, _)| kind == "file" && *id == fixture.lib_file) + ); + // Files and map rows: the map drops modules and tests. + let files = fixture.store.files().expect("files"); + assert_eq!(files.len(), 4); + assert_eq!( + fixture.store.map_file_paths().expect("paths"), + vec![ + "src/helper.rs".to_owned(), + "src/lib.rs".to_owned(), + "src/main.rs".to_owned(), + "tests/common/mod.rs".to_owned() + ] + ); + let rows = fixture.store.map_rows().expect("map rows"); + let method = rows + .iter() + .find(|row| row.id == fixture.method) + .expect("the method row"); + assert!(method.owner.is_some()); + assert!(method.start_line.is_some()); + assert!(rows.iter().all(|row| row.kind != "module")); + assert!(rows.iter().all(|row| row.file_id != files[3].id)); + // The epochs the readers keep: the graph's moved once, the rank's + // carries the write. + assert_eq!(fixture.store.graph_epoch().expect("graph epoch"), 1); + assert_eq!(fixture.store.rank_epoch().expect("rank epoch"), 2); +} +#[test] +fn detail_reads_answer_their_rows() { + let fixture = populated("readdetail"); + let detail = fixture + .store + .symbol_detail(fixture.holder) + .expect("detail") + .expect("the holder row"); + assert_eq!(detail.name, "Holder"); + assert_eq!(detail.kind, "struct"); + assert_eq!(detail.path, "src/lib.rs"); + assert!(detail.start_line.is_some()); + assert!(!detail.is_test); + assert!(fixture.store.symbol_detail(9_999).expect("miss").is_none()); + // Members: the method the owner write attributed to the struct. + let members = fixture.store.members(fixture.holder).expect("members"); + assert_eq!(members.len(), 1); + assert_eq!(members[0].name, "method"); + assert_eq!(members[0].id, fixture.method); + // A function and a trait carry no members. + assert!(fixture.store.members(fixture.one).expect("fn").is_empty()); + assert!( + fixture + .store + .members(fixture.shape) + .expect("trait") + .is_empty() + ); + // The stable key names the row back. + let key = detail.stable_key.clone(); + assert_eq!( + fixture.store.symbol_by_stable_key(&key).expect("by key"), + Some(fixture.holder) + ); + assert!( + fixture + .store + .symbol_by_stable_key("no-such-key") + .expect("miss") + .is_none() + ); +} +#[test] +fn impact_reads_answer_their_rows() { + let fixture = populated("readimpact"); + let facts = fixture + .store + .symbol_facts(fixture.one) + .expect("facts") + .expect("the `one` row"); + assert_eq!(facts.name, "one"); + assert_eq!(facts.kind, "function"); + assert_eq!(facts.file, "src/lib.rs"); + assert_eq!(facts.package.as_deref(), Some("sample")); + assert_eq!(facts.target_kind.as_deref(), Some("lib")); + assert_eq!(facts.trait_name, None); + assert!(!facts.is_test); + assert!(fixture.store.symbol_facts(9_999).expect("miss").is_none()); + // A trait method keeps its trait text; the test file's symbols are + // marked. + assert!( + fixture + .store + .symbol_spans() + .expect("spans") + .iter() + .any(|span| span.path == "tests/common/mod.rs" && span.is_test) + ); + let span = fixture + .store + .symbol_spans() + .expect("spans") + .into_iter() + .find(|span| span.id == fixture.one) + .expect("the `one` span"); + assert_eq!(span.file_id, fixture.lib_file); + assert!(span.end_line >= span.start_line); + // The placed call and the unplaced ones. + let refs = fixture.store.symbol_call_refs(fixture.one).expect("refs"); + assert_eq!(refs.len(), 1); + assert_eq!(refs[0].kind, "call"); + assert_eq!(refs[0].text, "three"); + assert!(refs[0].placed); + assert_eq!(fixture.store.unresolved_named("three").expect("named"), 0); + assert_eq!(fixture.store.unresolved_named("triplet").expect("named"), 1); + // The unplaced call and the macro invocation both count. + assert_eq!( + fixture.store.unresolved_calls(fixture.two).expect("calls"), + 2 + ); + assert_eq!( + fixture.store.unresolved_calls(fixture.one).expect("calls"), + 0 + ); +} +#[test] +fn scoring_reads_answer_their_rows() { + let fixture = populated("readscoring"); + // Every package's own directory, nested vendor package included. + let dirs = fixture.store.package_dirs().expect("package dirs"); + assert!(dirs.contains("/w")); + assert!(dirs.contains("/w/vendor/nested")); + // The scored module rows: one per file module, with its target. + let modules = fixture.store.module_rows().expect("modules"); + assert_eq!(modules.len(), 4); + assert!( + modules + .values() + .any(|row| row.path == "crate::helper" && row.file_id.is_some()) + ); + // The scored uses: the glob and the alias. + let uses = fixture.store.use_rows().expect("uses"); + assert!(uses.iter().any(|row| row.glob && row.text == "helper")); + assert!( + uses.iter() + .any(|row| row.alias.as_deref() == Some("triplet")) + ); + // The scored import targets, and the module of every symbol. + let imports = fixture.store.import_targets().expect("imports"); + assert_eq!(imports.len(), 2); + let modules_of = fixture.store.symbol_modules().expect("symbol modules"); + assert!(modules_of.contains_key(&fixture.three)); + // Ref ids by kind: the macro arguments exist, an unused kind is + // empty. + assert!( + !fixture + .store + .ref_ids_of_kind("call") + .expect("calls") + .is_empty() + ); + assert!( + fixture + .store + .ref_ids_of_kind("no-such-kind") + .expect("none") + .is_empty() + ); + // The scorer's call refs keep the receiver and the placement. + let calls = fixture + .store + .score_call_refs(fixture.two) + .expect("call refs"); + assert!( + calls + .iter() + .any(|call| call.kind == "method_call" || call.kind == "call") + ); + assert!(calls.iter().all(|call| !call.placed)); + // The same read marks a call an edge placed. + let placed = fixture + .store + .score_call_refs(fixture.one) + .expect("call refs"); + assert_eq!(placed.len(), 1); + assert_eq!(placed[0].text, "three"); + assert!(placed[0].placed); +} +#[test] +fn edge_reads_answer_their_rows() { + let fixture = populated("readedges"); + let calls = fixture.store.edges_of_kind("call").expect("calls"); + assert_eq!(calls.len(), 1); + assert_eq!(calls[0].src_kind, "symbol"); + assert_eq!(calls[0].reason, "path-exact"); + assert!(calls[0].ref_id.is_some()); + let impls = fixture.store.edges_of_kind("impl").expect("impls"); + assert_eq!(impls.len(), 1); + assert_eq!(impls[0].reason, "impl-trait"); + assert_eq!(impls[0].ref_id, None); + let imports = fixture.store.edges_of_kind("import").expect("imports"); + assert_eq!(imports.len(), 2); + assert!(imports.iter().all(|edge| edge.src_kind == "module")); + // The neighbours: upward to the caller, downward to the callee, both + // with the file, line, confidence and reason. + let callers = fixture.store.callers(fixture.three).expect("callers"); + assert_eq!(callers.len(), 1); + assert_eq!(callers[0].symbol, fixture.one); + assert_eq!(callers[0].file, "src/lib.rs"); + assert_eq!(callers[0].reason, "path-exact"); + let callees = fixture.store.callees(fixture.one).expect("callees"); + assert_eq!(callees.len(), 1); + assert_eq!(callees[0].symbol, fixture.three); + assert_eq!(callees[0].file, "src/helper.rs"); + // A symbol with no edges answers empty vectors. + assert!(fixture.store.callers(fixture.two).expect("none").is_empty()); + assert!( + fixture + .store + .callees(fixture.three) + .expect("none") + .is_empty() + ); + // Lookups by name and by module path both name the row. + assert!( + fixture + .store + .symbols_matching("three") + .expect("by name") + .iter() + .any(|row| row.id == fixture.three) + ); + assert!( + fixture + .store + .symbols_matching("crate::helper::three") + .expect("by path") + .iter() + .any(|row| row.id == fixture.three) + ); + assert!( + fixture + .store + .symbols_matching("no-such-symbol") + .expect("miss") + .is_empty() + ); +} diff --git a/crates/phantom-fractal/rust-tops.yaml b/crates/phantom-fractal/rust-tops.yaml index bf6c3eb..da90f01 100644 --- a/crates/phantom-fractal/rust-tops.yaml +++ b/crates/phantom-fractal/rust-tops.yaml @@ -32,9 +32,9 @@ layers: - { id: acceptance, enabled: true, tier: 1, runner: "std::process journeys", scope: tests } - { id: snapshot, enabled: false, tier: 2, runner: insta, scope: tests } - { id: compile-fail, enabled: false, tier: 2, runner: trybuild, scope: tests } - - { id: coverage, enabled: false, tier: 0, runner: cargo-llvm-cov, scope: lib } - - { id: crap, enabled: false, tier: 0, runner: cargo-crap, scope: lib } - - { id: mutation, enabled: false, tier: 1, runner: cargo-mutants, scope: diff } + - { id: coverage, enabled: true, tier: 0, runner: cargo-llvm-cov, scope: lib } + - { id: crap, enabled: true, tier: 0, runner: cargo-crap, scope: lib } + - { id: mutation, enabled: true, tier: 1, runner: cargo-mutants, scope: diff } - { id: property, enabled: false, tier: 1, runner: proptest, scope: lib } - { id: fuzz, enabled: false, tier: 3, runner: cargo-fuzz, scope: lib } - { id: concurrency, enabled: false, tier: 3, runner: loom, scope: lib } diff --git a/crates/phantom-fractal/src/mcp.rs b/crates/phantom-fractal/src/mcp.rs index e0331bf..c4460e7 100644 --- a/crates/phantom-fractal/src/mcp.rs +++ b/crates/phantom-fractal/src/mcp.rs @@ -18,7 +18,7 @@ mod roots; mod rpc; mod tools; -use std::io::{self, Write}; +use std::io::{self, BufRead, Write}; use std::path::PathBuf; use std::process::ExitCode; @@ -27,31 +27,119 @@ use std::process::ExitCode; /// empty. A root that is not a directory stops the server before it reads /// a line. pub(crate) fn serve(allow_index: bool, roots: &[PathBuf]) -> ExitCode { - let roots = match roots::Roots::new(roots) { - Ok(roots) => roots, - Err(message) => return fatal(&message), - }; - let mut input = io::stdin().lock(); - let mut output = io::stdout().lock(); - let mut server = rpc::Server::new(allow_index, roots); + match roots::Roots::new(roots) { + Ok(roots) => answer( + &mut rpc::Server::new(allow_index, roots), + &mut io::stdin().lock(), + &mut io::stdout().lock(), + ), + Err(message) => fatal(&message), + } +} + +/// Answers each line of `input` on `output` until `input` ends, or until +/// a read or a write fails. +fn answer(server: &mut rpc::Server, input: &mut impl BufRead, output: &mut impl Write) -> ExitCode { loop { - let line = match frame::read_line(&mut input, frame::MAX_LINE) { + let line = match frame::read_line(input, frame::MAX_LINE) { Ok(frame::Line::Eof) => return ExitCode::SUCCESS, Ok(line) => line, Err(err) => return fatal(&format!("cannot read stdin: {err}")), }; - let Some(reply) = server.handle(line) else { - continue; - }; - if let Err(err) = writeln!(output, "{reply}").and_then(|()| output.flush()) { + if let Err(err) = reply(server, line, output) { return fatal(&format!("cannot write stdout: {err}")); } } } +/// Writes the reply to `line`, if it has one, and flushes it. +fn reply(server: &mut rpc::Server, line: frame::Line, output: &mut impl Write) -> io::Result<()> { + let Some(reply) = server.handle(line) else { + return Ok(()); + }; + writeln!(output, "{reply}")?; + output.flush() +} + /// The one stderr line of a server that cannot go on, and exit 1. fn fatal(message: &str) -> ExitCode { // If stderr is gone too, there is nowhere left to report it. let _ = writeln!(io::stderr().lock(), "phantom-fractal mcp: {message}"); ExitCode::from(1) } + +#[cfg(test)] +mod tests { + use std::io::{self, Cursor, Read, Write}; + + use super::*; + + fn server() -> rpc::Server { + rpc::Server::new( + false, + roots::Roots::new(&[]).expect("the working directory"), + ) + } + + /// Input that cannot be read, and output that cannot be written. + struct Broken; + + impl Read for Broken { + fn read(&mut self, _: &mut [u8]) -> io::Result { + Err(io::Error::from(io::ErrorKind::BrokenPipe)) + } + } + + impl Write for Broken { + fn write(&mut self, _: &[u8]) -> io::Result { + Err(io::Error::from(io::ErrorKind::BrokenPipe)) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } + } + + /// `work`'s value, or a panic when it has not returned within 10 s: + /// every input ends in a line, the end of input, or an error. + fn returns(work: impl FnOnce() -> T + Send + 'static) -> T { + let (send, got) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let _ = send.send(work()); + }); + got.recv_timeout(std::time::Duration::from_secs(10)) + .unwrap_or_else(|err| panic!("the server did not return: {err:?}")) + } + + const PING: &[u8] = b"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"ping\"}\n"; + + #[test] + fn the_end_of_input_ends_the_server_with_exit_0() { + let (code, output) = returns(|| { + let mut output = Vec::new(); + let code = answer(&mut server(), &mut Cursor::new(PING), &mut output); + (code, output) + }); + assert_eq!(code, ExitCode::SUCCESS); + let reply = String::from_utf8(output).expect("utf-8"); + assert_eq!(reply.lines().count(), 1, "{reply}"); + assert!( + reply.ends_with('\n') && reply.contains("\"result\":{}"), + "{reply}" + ); + } + + #[test] + fn a_failed_read_or_write_ends_the_server_with_exit_1() { + let code = returns(|| { + answer( + &mut server(), + &mut io::BufReader::new(Broken), + &mut Vec::new(), + ) + }); + assert_eq!(code, ExitCode::from(1)); + let code = returns(|| answer(&mut server(), &mut Cursor::new(PING), &mut Broken)); + assert_eq!(code, ExitCode::from(1)); + } +} diff --git a/crates/phantom-fractal/src/mcp/frame.rs b/crates/phantom-fractal/src/mcp/frame.rs index 1591009..d51f261 100644 --- a/crates/phantom-fractal/src/mcp/frame.rs +++ b/crates/phantom-fractal/src/mcp/frame.rs @@ -20,61 +20,117 @@ pub(crate) enum Line { /// Reads one line of at most `cap` bytes. A longer line is consumed to its /// end and never held whole in memory. pub(crate) fn read_line(input: &mut impl BufRead, cap: usize) -> io::Result { - let mut line = Vec::new(); - let mut too_long = false; - let mut started = false; + let mut reading = Reading { + cap, + line: Vec::new(), + too_long: false, + started: false, + }; loop { - let buf = match input.fill_buf() { - Ok(buf) => buf, - Err(err) if err.kind() == io::ErrorKind::Interrupted => continue, - Err(err) => return Err(err), - }; + let filled = input.fill_buf(); + if interrupted(&filled) { + continue; + } + let (used, line) = reading.take(filled?); + input.consume(used); + if let Some(line) = line { + return Ok(line); + } + } +} + +/// True when a read was interrupted and is to be tried again. +fn interrupted(filled: &io::Result<&[u8]>) -> bool { + filled + .as_ref() + .is_err_and(|err| err.kind() == io::ErrorKind::Interrupted) +} + +/// One line read so far. +struct Reading { + cap: usize, + /// The bytes kept: none once the line is over the cap. + line: Vec, + too_long: bool, + /// Set once any byte of the line was read. + started: bool, +} + +impl Reading { + /// Takes in one buffer of input, empty at its end: how many of its + /// bytes the line used, and the line once it is whole. + fn take(&mut self, buf: &[u8]) -> (usize, Option) { if buf.is_empty() { - return Ok(match (started, too_long) { - (false, _) => Line::Eof, - (true, true) => Line::TooLong, - (true, false) => Line::Text(line), - }); + return (0, Some(self.at_end())); } - started = true; + self.started = true; let newline = buf.iter().position(|byte| *byte == b'\n'); - let chunk = &buf[..newline.unwrap_or(buf.len())]; - if !too_long { - if line.len() + chunk.len() > cap { - too_long = true; - line = Vec::new(); - } else { - line.extend_from_slice(chunk); - } + self.keep(&buf[..newline.unwrap_or(buf.len())]); + match newline { + Some(at) => (at + 1, Some(self.line())), + None => (buf.len(), None), } - let used = newline.map_or(buf.len(), |at| at + 1); - input.consume(used); - if newline.is_some() { - return Ok(if too_long { - Line::TooLong - } else { - Line::Text(line) - }); + } + + /// Keeps `chunk` while the line stays within the cap, and drops the + /// line once it goes over. + fn keep(&mut self, chunk: &[u8]) { + if self.too_long { + return; + } + if self.line.len() + chunk.len() > self.cap { + self.too_long = true; + self.line = Vec::new(); + } else { + self.line.extend_from_slice(chunk); + } + } + + /// The line when the input ends: none before its first byte. + fn at_end(&mut self) -> Line { + if self.started { self.line() } else { Line::Eof } + } + + /// The line read, or [`Line::TooLong`]. + fn line(&mut self) -> Line { + if self.too_long { + Line::TooLong + } else { + Line::Text(std::mem::take(&mut self.line)) } } } #[cfg(test)] mod tests { - use std::io::{BufReader, Cursor}; + use std::io::{BufReader, Cursor, Read}; use super::*; + /// `work`'s value, or a panic when it has not returned within 10 s: + /// every input ends in a line, the end of input, or an error. + fn returns(work: impl FnOnce() -> T + Send + 'static) -> T { + let (send, got) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let _ = send.send(work()); + }); + got.recv_timeout(std::time::Duration::from_secs(10)) + .unwrap_or_else(|err| panic!("the read did not return: {err:?}")) + } + fn lines(input: &[u8], cap: usize, capacity: usize) -> Vec { - let mut reader = BufReader::with_capacity(capacity, Cursor::new(input.to_vec())); - let mut out = Vec::new(); - loop { - let line = read_line(&mut reader, cap).expect("read"); - if line == Line::Eof { - return out; + let input = input.to_vec(); + returns(move || { + let mut reader = BufReader::with_capacity(capacity, Cursor::new(input)); + let mut out = Vec::new(); + loop { + let line = read_line(&mut reader, cap).expect("read"); + if line == Line::Eof { + return out; + } + out.push(line); } - out.push(line); - } + }) } fn text(bytes: &[u8]) -> Line { @@ -99,4 +155,37 @@ mod tests { ); assert_eq!(lines(b"abcdefgh", 4, 3), [Line::TooLong]); } + + /// A reader that fails once with `kind`, then reads `rest`. + struct FailOnce { + kind: Option, + rest: Cursor>, + } + + impl Read for FailOnce { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + match self.kind.take() { + Some(kind) => Err(io::Error::from(kind)), + None => self.rest.read(buf), + } + } + } + + fn fail_once(kind: io::ErrorKind) -> BufReader { + BufReader::new(FailOnce { + kind: Some(kind), + rest: Cursor::new(b"ab\n".to_vec()), + }) + } + + #[test] + fn an_interrupted_read_is_tried_again_and_any_other_error_is_returned() { + let read = + |kind| returns(move || read_line(&mut fail_once(kind), 8).map_err(|err| err.kind())); + assert_eq!(read(io::ErrorKind::Interrupted), Ok(text(b"ab"))); + assert_eq!( + read(io::ErrorKind::BrokenPipe), + Err(io::ErrorKind::BrokenPipe) + ); + } } diff --git a/crates/phantom-fractal/src/mcp/rpc.rs b/crates/phantom-fractal/src/mcp/rpc.rs index 8741a61..57aaa4f 100644 --- a/crates/phantom-fractal/src/mcp/rpc.rs +++ b/crates/phantom-fractal/src/mcp/rpc.rs @@ -47,21 +47,23 @@ impl Server { /// The reply line for one input line, or none for a notification, a /// client response, or a blank line. pub(crate) fn handle(&mut self, line: Line) -> Option { - let bytes = match line { - Line::Eof => return None, - Line::TooLong => { - return Some(error( - &Value::Null, - INVALID_REQUEST, - "message is over 1 MiB", - )); - } - Line::Text(bytes) => bytes, - }; + match line { + Line::Eof => None, + Line::TooLong => Some(error( + &Value::Null, + INVALID_REQUEST, + "message is over 1 MiB", + )), + Line::Text(bytes) => self.text(&bytes), + } + } + + /// The reply to one whole line of text: none for a blank one. + fn text(&mut self, bytes: &[u8]) -> Option { if bytes.iter().all(u8::is_ascii_whitespace) { return None; } - match serde_json::from_slice::(&bytes) { + match serde_json::from_slice::(bytes) { Ok(message) => self.message(message), Err(_) => Some(error(&Value::Null, PARSE_ERROR, "parse error")), } @@ -69,17 +71,12 @@ impl Server { fn message(&mut self, message: Value) -> Option { let Value::Object(message) = message else { - let text = if message.is_array() { - "batches are not supported" - } else { - "a message must be a JSON object" - }; - return Some(error(&Value::Null, INVALID_REQUEST, text)); + return Some(not_an_object(&message)); }; let id = message.get("id"); let method = message.get("method"); // A client's response: this server sends no requests to answer. - if method.is_none() && (message.contains_key("result") || message.contains_key("error")) { + if is_response(&message) { return None; } let reply_id = id.filter(|id| valid_id(id)).unwrap_or(&Value::Null); @@ -89,6 +86,18 @@ impl Server { let Some(method) = method.and_then(Value::as_str) else { return Some(error(reply_id, INVALID_REQUEST, "method must be a string")); }; + self.reply_to(id, method, message.get("params")) + } + + /// The reply to a well-formed message by its id: none for a + /// notification, a refusal for an id that is not a string or a number, + /// and the request's answer otherwise. + fn reply_to( + &mut self, + id: Option<&Value>, + method: &str, + params: Option<&Value>, + ) -> Option { match id { // A notification gets no reply. `notifications/initialized` // changes nothing here, and the rest are ignored. @@ -98,20 +107,23 @@ impl Server { INVALID_REQUEST, "id must be a string or a number", )), - Some(id) => Some(self.request(id, method, message.get("params"))), + Some(id) => Some(self.request(id, method, params)), } } fn request(&mut self, id: &Value, method: &str, params: Option<&Value>) -> String { - if !self.initialized && method != "initialize" && method != "ping" { + if !self.initialized && !before_initialize(method) { return error(id, NOT_INITIALIZED, "server not initialized"); } let empty = Map::new(); - let params = match params { - None => &empty, - Some(Value::Object(params)) => params, - Some(_) => return error(id, INVALID_PARAMS, "params must be an object"), + let Some(params) = object(params, &empty) else { + return error(id, INVALID_PARAMS, "params must be an object"); }; + self.dispatch(id, method, params) + } + + /// The answer to method `method`. + fn dispatch(&mut self, id: &Value, method: &str, params: &Map) -> String { match method { "initialize" => { self.initialized = true; @@ -129,16 +141,57 @@ impl Server { return error(id, INVALID_PARAMS, "tool name must be a string"); }; let empty = Map::new(); - let arguments = match params.get("arguments") { - None => &empty, - Some(Value::Object(arguments)) => arguments, - Some(_) => return error(id, INVALID_PARAMS, "arguments must be an object"), + let Some(arguments) = object(params.get("arguments"), &empty) else { + return error(id, INVALID_PARAMS, "arguments must be an object"); }; - match tools::call(name, arguments, self.allow_index, &self.roots) { - Some(Ok(outcome)) => tool_result(id, &outcome), - Some(Err(())) => error(id, INTERNAL_ERROR, "the result could not be encoded"), - None => error(id, INVALID_PARAMS, "unknown tool"), - } + tool_reply( + id, + tools::call(name, arguments, self.allow_index, &self.roots), + ) + } +} + +/// The reply to a message that is not a JSON object. +fn not_an_object(message: &Value) -> String { + let text = if message.is_array() { + "batches are not supported" + } else { + "a message must be a JSON object" + }; + error(&Value::Null, INVALID_REQUEST, text) +} + +/// True for a client's response: no method, and a result or an error. +fn is_response(message: &Map) -> bool { + !message.contains_key("method") + && (message.contains_key("result") || message.contains_key("error")) +} + +/// True for the methods a server answers before `initialize`. +fn before_initialize(method: &str) -> bool { + method == "initialize" || method == "ping" +} + +/// `value` as an object: `empty` when it is absent, none when it is +/// anything but an object. +fn object<'a>( + value: Option<&'a Value>, + empty: &'a Map, +) -> Option<&'a Map> { + match value { + None => Some(empty), + Some(Value::Object(map)) => Some(map), + Some(_) => None, + } +} + +/// The reply to a tool call: its result, the refusal of an unknown tool, +/// or the internal error of a result that could not be encoded. +fn tool_reply(id: &Value, outcome: Option>) -> String { + match outcome { + Some(Ok(outcome)) => tool_result(id, &outcome), + Some(Err(())) => error(id, INTERNAL_ERROR, "the result could not be encoded"), + None => error(id, INVALID_PARAMS, "unknown tool"), } } diff --git a/crates/phantom-fractal/src/mcp/tools.rs b/crates/phantom-fractal/src/mcp/tools.rs index 3b19ee3..72b6af1 100644 --- a/crates/phantom-fractal/src/mcp/tools.rs +++ b/crates/phantom-fractal/src/mcp/tools.rs @@ -9,6 +9,7 @@ //! argument. Every string argument has a [`max_length`], in its schema and //! in the check, so an over-long one never reaches the store. +use std::cell::RefCell; use std::path::PathBuf; use phantom_fractal_analyze::{ImpactOptions, PATH_DEFAULT_DEPTH, PATH_MAX_DEPTH, PATH_MIN_DEPTH}; @@ -474,9 +475,10 @@ pub(crate) fn call( fn find_request(arguments: &Map, roots: &Roots) -> Result { let args = Args::new(arguments, &["query", "repo", "max_tokens"])?; - let query = args.required_string("query")?; - let repo = args.string("repo")?; - let max_tokens = args.max_tokens()?; + let query = args.read(args.required_string("query")); + let repo = args.read(args.string("repo")); + let max_tokens = args.read(args.max_tokens()); + args.checked()?; Ok(FindRequest { query, repo: roots.indexed(repo.as_deref())?, @@ -486,14 +488,11 @@ fn find_request(arguments: &Map, roots: &Roots) -> Result, roots: &Roots) -> Result { let args = Args::new(arguments, &["symbol", "id", "repo", "source", "max_tokens"])?; - let target = match (args.string("symbol")?, args.string("id")?) { - (Some(symbol), None) => Target::Name(symbol), - (None, Some(key)) => Target::Id(key), - _ => return Err(usage("give exactly one of symbol and id")), - }; - let repo = args.string("repo")?; - let source = args.boolean("source")?.unwrap_or(false); - let max_tokens = args.max_tokens()?; + let target = args.target()?; + let repo = args.read(args.string("repo")); + let source = args.read(args.boolean("source")).unwrap_or(false); + let max_tokens = args.read(args.max_tokens()); + args.checked()?; Ok(ShowRequest { target, repo: roots.indexed(repo.as_deref())?, @@ -504,9 +503,10 @@ fn show_request(arguments: &Map, roots: &Roots) -> Result, roots: &Roots) -> Result { let args = Args::new(arguments, &["symbol", "repo", "max_tokens"])?; - let symbol = args.required_string("symbol")?; - let repo = args.string("repo")?; - let max_tokens = args.max_tokens()?; + let symbol = args.read(args.required_string("symbol")); + let repo = args.read(args.string("repo")); + let max_tokens = args.read(args.max_tokens()); + args.checked()?; Ok(CallsRequest { symbol, repo: roots.indexed(repo.as_deref())?, @@ -527,16 +527,12 @@ fn impact_request(arguments: &Map, roots: &Roots) -> Result, roots: &Roots) -> Result, roots: &Roots) -> Result { let args = Args::new(arguments, &["kind", "focus", "limit", "repo", "max_tokens"])?; - let kind = match args.string("kind")? { - None => RankKind::Symbols, - Some(text) => { - RankKind::parse(&text).ok_or_else(|| usage("kind must be symbols or files"))? - } - }; - let focus = args.focus()?; + let kind = args + .read(args.choice("kind", RankKind::parse, "kind must be symbols or files")) + .unwrap_or(RankKind::Symbols); + let focus = args.read(args.focus()); let limit = args - .integer("limit", LIMIT_MIN, LIMIT_MAX)? + .read(args.integer("limit", LIMIT_MIN, LIMIT_MAX)) .unwrap_or(DEFAULT_LIMIT); - let repo = args.string("repo")?; - let max_tokens = args.max_tokens()?; + let repo = args.read(args.string("repo")); + let max_tokens = args.read(args.max_tokens()); + args.checked()?; Ok(RankRequest { kind, focus, @@ -571,17 +565,20 @@ fn rank_request(arguments: &Map, roots: &Roots) -> Result, roots: &Roots) -> Result { let args = Args::new(arguments, &["budget", "focus", "detail", "repo"])?; - let detail = match args.string("detail")? { - None => Default::default(), - Some(text) => { - ops::map::detail(&text).ok_or_else(|| usage("detail must be files, symbols or full"))? - } - }; - let budget = args - .integer("budget", BUDGET_MIN, BUDGET_MAX)? - .ok_or_else(|| usage("budget is required"))?; - let focus = args.focus()?; - let repo = args.string("repo")?; + let detail = args + .read(args.choice( + "detail", + ops::map::detail, + "detail must be files, symbols or full", + )) + .unwrap_or_default(); + let budget = args.read(required( + "budget", + args.integer("budget", BUDGET_MIN, BUDGET_MAX), + )); + let focus = args.read(args.focus()); + let repo = args.read(args.string("repo")); + args.checked()?; Ok(MapRequest { budget, focus, @@ -598,13 +595,14 @@ fn cochange_request( arguments, &["file", "min_lift", "limit", "repo", "max_tokens"], )?; - let file = args.required_string("file")?; - let min_lift = args.min_lift()?.unwrap_or(DEFAULT_MIN_LIFT); + let file = args.read(args.required_string("file")); + let min_lift = args.read(args.min_lift()).unwrap_or(DEFAULT_MIN_LIFT); let limit = args - .integer("limit", cochange::LIMIT_MIN, cochange::LIMIT_MAX)? + .read(args.integer("limit", cochange::LIMIT_MIN, cochange::LIMIT_MAX)) .unwrap_or(cochange::DEFAULT_LIMIT); - let repo = args.string("repo")?; - let max_tokens = args.max_tokens()?; + let repo = args.read(args.string("repo")); + let max_tokens = args.read(args.max_tokens()); + args.checked()?; Ok(CochangeRequest { file, min_lift, @@ -627,16 +625,12 @@ fn diff_request(arguments: &Map, roots: &Roots) -> Result, roots: &Roots) -> Result, roots: &Roots) -> Result, roots: &Roots) -> Result { let args = Args::new(arguments, &["path", "cochange_days", "cochange_commits"])?; - let path = args.required_string("path")?; - let days = args.integer("cochange_days", 1, u64::from(Window::MAX_DAYS))?; - let commits = args.integer("cochange_commits", 1, u64::from(Window::MAX_COMMITS))?; + let path = args.read(args.required_string("path")); + let days = args.read(args.integer("cochange_days", 1, u64::from(Window::MAX_DAYS))); + let commits = args.read(args.integer("cochange_commits", 1, u64::from(Window::MAX_COMMITS))); + args.checked()?; // The ranges above fit in u32. let window = Window { days: days.map_or(Window::DEFAULT_DAYS, |days| days as u32), @@ -725,6 +721,11 @@ fn usage(message: &str) -> Failure { Failure::new(ErrorKind::Usage, message) } +/// The value argument `name` holds, or the usage failure for a missing one. +fn required(name: &str, value: Result, Failure>) -> Result { + value?.ok_or_else(|| usage(&format!("{name} is required"))) +} + /// The most Unicode scalar values string argument `name`, or each string /// of array argument `name`, may hold: 256 for a base, 4096 for a path or /// file, and 1024 for anything else. @@ -749,14 +750,23 @@ fn bounded(name: &str, text: &str) -> Result { /// A tool's arguments, checked against the names it takes. Messages name /// only the tool's own argument names, never a value or key the client /// sent. +/// +/// [`Args::read`] takes each reader's result and keeps the first failure +/// met, in the order the reads are made, so a tool reads its arguments as +/// one list and [`Args::checked`] then reports the first bad one, as if +/// each read had returned at once. struct Args<'a> { map: &'a Map, + first: RefCell>, } impl<'a> Args<'a> { fn new(map: &'a Map, allowed: &[&str]) -> Result { if map.keys().all(|key| allowed.contains(&key.as_str())) { - Ok(Self { map }) + Ok(Self { + map, + first: RefCell::new(None), + }) } else { Err(usage(&format!( "unknown argument; this tool takes {}", @@ -773,9 +783,55 @@ impl<'a> Args<'a> { } } + /// The value `value` holds, or its default when it holds a failure, + /// which is kept when it is the first. + fn read(&self, value: Result) -> T { + value.unwrap_or_else(|failure| { + self.first.borrow_mut().get_or_insert(failure); + T::default() + }) + } + + /// The first failure [`Args::read`] kept, if any. + fn checked(&self) -> Result<(), Failure> { + self.first.take().map_or(Ok(()), Err) + } + fn required_string(&self, name: &str) -> Result { + required(name, self.string(name)) + } + + /// String `name`, read by `parse` as one of a fixed set, or the usage + /// failure `message` for any other string. + fn choice( + &self, + name: &str, + parse: impl Fn(&str) -> Option, + message: &str, + ) -> Result, Failure> { self.string(name)? - .ok_or_else(|| usage(&format!("{name} is required"))) + .map(|text| parse(&text).ok_or_else(|| usage(message))) + .transpose() + } + + /// `show_symbol`'s target: exactly one of `symbol` and `id`. + fn target(&self) -> Result { + match (self.string("symbol")?, self.string("id")?) { + (Some(symbol), None) => Ok(Target::Name(symbol)), + (None, Some(key)) => Ok(Target::Id(key)), + _ => Err(usage("give exactly one of symbol and id")), + } + } + + /// `depth`, `min_confidence` and `include_tests`, read in that order, + /// with the command line's defaults. + fn impact_options(&self) -> Result { + let defaults = ImpactOptions::default(); + Ok(ImpactOptions { + max_depth: self.depth()?.unwrap_or(defaults.max_depth), + min_confidence: self.min_confidence()?.unwrap_or(defaults.min_confidence), + include_tests: self.boolean("include_tests")?.unwrap_or(true), + }) } fn boolean(&self, name: &str) -> Result, Failure> { diff --git a/crates/phantom-fractal/src/ops/lookup.rs b/crates/phantom-fractal/src/ops/lookup.rs index 4a6a118..a3cfabd 100644 --- a/crates/phantom-fractal/src/ops/lookup.rs +++ b/crates/phantom-fractal/src/ops/lookup.rs @@ -14,7 +14,7 @@ use phantom_fractal_proto::ErrorKind; use phantom_fractal_store::{Store, SymbolMatch}; use serde::Serialize; -use super::{Failure, open_index}; +use super::{Failure, budget, open_index}; /// Most candidates an `Ambiguous` failure lists. const CANDIDATE_CAP: usize = 10; @@ -96,6 +96,15 @@ pub(crate) fn current_index(repo: &Path) -> Result<(Store, Names), Failure> { Ok((store, names)) } +/// [`current_index`], once `max_tokens` is checked first. +pub(crate) fn checked_index( + max_tokens: Option, + repo: &Path, +) -> Result<(Store, Names), Failure> { + budget::check(max_tokens)?; + current_index(repo) +} + /// The store for the tree at `repo`. It is `NoIndex` unless the index was /// written by this extractor version, as well as in every case /// [`open_index`] refuses. diff --git a/crates/phantom-fractal/src/ops/map.rs b/crates/phantom-fractal/src/ops/map.rs index 9bbf46b..bea9360 100644 --- a/crates/phantom-fractal/src/ops/map.rs +++ b/crates/phantom-fractal/src/ops/map.rs @@ -90,3 +90,29 @@ pub(crate) fn map(request: &MapRequest) -> Result, Failure> { }; Ok(Reply::with_meta(result, meta)) } + +#[cfg(test)] +mod tests { + use super::*; + + /// The budget is checked by `map` itself, before any index is read, so + /// the command line and the MCP tool share one bound. + #[test] + fn a_budget_outside_1_to_100000_is_usage() { + for budget in [0, BUDGET_MAX + 1] { + let request = MapRequest { + budget, + focus: Vec::new(), + detail: Detail::default(), + repo: PathBuf::from("no-such-tree"), + }; + let (line, code) = super::super::envelope(map(&request)); + assert_eq!(code, 1, "budget {budget}"); + assert_eq!( + line.expect("envelope"), + r#"{"ok":false,"data":null,"error":{"kind":"usage","message":"budget must be 1 to 100000","exit":1},"schema":1}"#, + "budget {budget}" + ); + } + } +} diff --git a/crates/phantom-fractal/src/ops/show.rs b/crates/phantom-fractal/src/ops/show.rs index 6bfb9e7..2de86cb 100644 --- a/crates/phantom-fractal/src/ops/show.rs +++ b/crates/phantom-fractal/src/ops/show.rs @@ -8,13 +8,13 @@ use std::path::PathBuf; use phantom_fractal_proto::ErrorKind; -use phantom_fractal_store::SymbolDetail; +use phantom_fractal_store::{Store, SymbolDetail}; use serde::Serialize; -use super::budget::{self, Shed, Tokens, drop_last}; +use super::budget::{Shed, Tokens, drop_last}; use super::calls::{CallOut, Direction, edges}; use super::cochange::{DEFAULT_MIN_LIFT, PartnerOut, partners}; -use super::lookup::{Names, current_index, lookup_keyed}; +use super::lookup::{Names, checked_index, lookup_keyed}; use super::source::{SourceOut, read_source}; use super::{Failure, Reply, answer, canonical_repo}; @@ -137,61 +137,114 @@ struct Calls { /// The detail of the one symbol `request.target` names in the index of /// `request.repo`, fitted to `request.max_tokens`. pub(crate) fn show(request: &ShowRequest) -> Result, Failure> { - budget::check(request.max_tokens)?; - let (store, names) = current_index(&request.repo)?; - let id = match &request.target { - Target::Name(query) => lookup_keyed(&store, &names, query)?.id, + let (store, names) = checked_index(request.max_tokens, &request.repo)?; + let id = target_id(&store, &names, &request.target)?; + let detail = indexed_detail(&store, id)?; + let around = around(&store, &names, id, &detail)?; + let source = source(request, &store, &detail)?; + let result = ShowResult { + symbol: detail_out(&names, detail), + members: around.members, + callers: around.callers, + callees: around.callees, + macro_args: around.macro_args, + cochange: around.cochange, + source, + tokens: None, + }; + answer(result, &store, &request.repo, request.max_tokens) +} + +/// What the index holds around one symbol, read in this order. +struct Around { + members: Members, + callers: Calls, + callees: Calls, + macro_args: i64, + cochange: Option>, +} + +/// The id of the symbol `target` names: by name as `callers` looks it up, +/// or by exact stable key. +fn target_id(store: &Store, names: &Names, target: &Target) -> Result { + match target { + Target::Name(query) => Ok(lookup_keyed(store, names, query)?.id), Target::Id(key) => store .symbol_by_stable_key(key)? - .ok_or_else(|| Failure::new(ErrorKind::NotFound, "no symbol has that stable key"))?, - }; - let detail = store + .ok_or_else(|| Failure::new(ErrorKind::NotFound, "no symbol has that stable key")), + } +} + +/// The stored detail of symbol `id`, which a lookup just found. +fn indexed_detail(store: &Store, id: i64) -> Result { + store .symbol_detail(id)? - .ok_or_else(|| Failure::new(ErrorKind::NotFound, "the symbol left the index"))?; + .ok_or_else(|| Failure::new(ErrorKind::NotFound, "the symbol left the index")) +} + +/// The members, callers, callees, macro-argument count and file partners +/// of symbol `id`, whose detail is `detail`. +fn around(store: &Store, names: &Names, id: i64, detail: &SymbolDetail) -> Result { let mut members = store.members(id)?; - let members_truncated = members.len() > MEMBER_CAP; + let truncated = members.len() > MEMBER_CAP; members.truncate(MEMBER_CAP); - let callers = calls( - edges(&store, &names, Direction::Callers, id, &detail.path)?, - None, - ); - let callees = calls( - edges(&store, &names, Direction::Callees, id, &detail.path)?, - Some(store.unresolved_calls(id)?), - ); - let macro_args = store.macro_args_named(&detail.name)?; - let cochange = match store.cochange_head()? { - Some(_) => Some(partners( - &store, - &detail.path, - DEFAULT_MIN_LIFT, - SHOW_COCHANGE_CAP, - )?), - None => None, - }; - let source = if request.source { - let root = canonical_repo(&request.repo)?; - Some(read_source(&store, &root, &detail)?) - } else { - None - }; - let result = ShowResult { - symbol: detail_out(&names, detail), + let (callers, callees) = call_sides(store, names, id, &detail.path)?; + Ok(Around { members: Members { hits: members .into_iter() - .map(|member| detail_out(&names, member)) + .map(|member| detail_out(names, member)) .collect(), - truncated: members_truncated, + truncated, }, callers, callees, - macro_args, - cochange, - source, - tokens: None, - }; - answer(result, &store, &request.repo, request.max_tokens) + macro_args: store.macro_args_named(&detail.name)?, + cochange: file_partners(store, &detail.path)?, + }) +} + +/// The capped callers and callees of symbol `id` in file `path`, the +/// callees with their count of unresolved calls. +fn call_sides( + store: &Store, + names: &Names, + id: i64, + path: &str, +) -> Result<(Calls, Calls), Failure> { + let callers = calls(edges(store, names, Direction::Callers, id, path)?, None); + let callees = calls( + edges(store, names, Direction::Callees, id, path)?, + Some(store.unresolved_calls(id)?), + ); + Ok((callers, callees)) +} + +/// The co-change partners of file `path`, as `cochange` lists them with +/// its default `min_lift`, when the index holds a co-change scan. +fn file_partners(store: &Store, path: &str) -> Result>, Failure> { + if store.cochange_head()?.is_none() { + return Ok(None); + } + Ok(Some(partners( + store, + path, + DEFAULT_MIN_LIFT, + SHOW_COCHANGE_CAP, + )?)) +} + +/// The symbol's source lines, when `request` asks for them. +fn source( + request: &ShowRequest, + store: &Store, + detail: &SymbolDetail, +) -> Result, Failure> { + if !request.source { + return Ok(None); + } + let root = canonical_repo(&request.repo)?; + Ok(Some(read_source(store, &root, detail)?)) } fn calls(mut hits: Vec, unresolved_calls: Option) -> Calls { diff --git a/crates/phantom-fractal/src/ops/status.rs b/crates/phantom-fractal/src/ops/status.rs index bb0300e..e3e6852 100644 --- a/crates/phantom-fractal/src/ops/status.rs +++ b/crates/phantom-fractal/src/ops/status.rs @@ -4,7 +4,7 @@ use std::path::{Path, PathBuf}; -use phantom_fractal_extract::probe; +use phantom_fractal_extract::{Freshness, probe}; use phantom_fractal_proto::{ErrorKind, SCHEMA_VERSION}; use phantom_fractal_store::{LockState, Store, index_database_path, lock_state}; use serde::Serialize; @@ -75,17 +75,36 @@ pub(crate) fn status(request: &StatusRequest) -> Result, Fai let store = open_index(&request.repo)?; let root = canonical_repo(&request.repo)?; let database = index_database_path(&root); - // The probe's error may hold a path, so it is not passed on. - let freshness = probe(&root, &store).map_err(|_| { + let freshness = probe_tree(&root, &store)?; + let rank = store.rank_epoch()?; + let result = result(&store, &database, &freshness, rank)?; + Ok(Reply::with_meta(result, fresh::of(&freshness, rank))) +} + +/// The tree at `root` compared with its index. The probe's error may hold +/// a path, so it is not passed on. +fn probe_tree(root: &Path, store: &Store) -> Result { + probe(root, store).map_err(|_| { Failure::new( ErrorKind::Usage, "the tree could not be read to compare it with the index", ) - })?; - let rank = store.rank_epoch()?; - let key = key(&database); - let result = StatusResult { - name: name(&store, &key)?, + }) +} + +/// The reply: the index's key and name, the tree's state against it, and +/// what it stores, read in that order. +fn result( + store: &Store, + database: &Path, + freshness: &Freshness, + rank: u64, +) -> Result { + let key = key(database); + let name = name(store, &key)?; + let stored = stored(store, database, rank)?; + Ok(StatusResult { + name, key, head: freshness.head.clone(), index_head: freshness.index_head.clone(), @@ -97,27 +116,60 @@ pub(crate) fn status(request: &StatusRequest) -> Result, Fai }, fingerprint_changed: freshness.fingerprint_changed, extractor_changed: freshness.extractor_changed, - counts: Counts { - files: store.row_count("files")?, - symbols: store.row_count("symbols")?, - edges: store.row_count("edges")?, - }, - versions: Versions { - schema: store.schema_version()?, - extractor: store.extractor_version()?, - envelope: SCHEMA_VERSION, - }, + counts: stored.counts, + versions: stored.versions, + epochs: stored.epochs, + cochange: stored.cochange, + lock: stored.lock, + }) +} + +/// What the index stores, and whether a writer holds it. +struct Stored { + counts: Counts, + versions: Versions, + epochs: Epochs, + cochange: Option, + lock: &'static str, +} + +/// The stored counts, versions, epochs and co-change scan of the index at +/// `database`, read in that order, then its lock. +fn stored(store: &Store, database: &Path, rank: u64) -> Result { + Ok(Stored { + counts: counts(store)?, + versions: versions(store)?, epochs: Epochs { graph: store.graph_epoch()?, rank, }, - cochange: stored_cochange(&store)?, - lock: match lock_state(&database)? { - LockState::Held => "held", - LockState::Free | LockState::Absent => "free", - }, - }; - Ok(Reply::with_meta(result, fresh::of(&freshness, rank))) + cochange: stored_cochange(store)?, + lock: lock_text(database)?, + }) +} + +fn counts(store: &Store) -> Result { + Ok(Counts { + files: store.row_count("files")?, + symbols: store.row_count("symbols")?, + edges: store.row_count("edges")?, + }) +} + +fn versions(store: &Store) -> Result { + Ok(Versions { + schema: store.schema_version()?, + extractor: store.extractor_version()?, + envelope: SCHEMA_VERSION, + }) +} + +/// `held` while a writer holds the index at `database`, else `free`. +fn lock_text(database: &Path) -> Result<&'static str, Failure> { + Ok(match lock_state(database)? { + LockState::Held => "held", + LockState::Free | LockState::Absent => "free", + }) } /// The registry key: the name of the database's directory. diff --git a/crates/phantom-fractal/tests/commands.rs b/crates/phantom-fractal/tests/commands.rs index 02aa51b..f19c3a7 100644 --- a/crates/phantom-fractal/tests/commands.rs +++ b/crates/phantom-fractal/tests/commands.rs @@ -564,3 +564,28 @@ fn find_without_an_index_is_no_index_and_creates_nothing() { let created = fs::read_dir(fixture.data.path()).expect("data dir").count(); assert_eq!(created, 0, "find created files in the data directory"); } + +/// A path that names a file, not a directory, is usage, and the data +/// directory stays empty. +#[test] +fn index_refuses_a_path_that_is_not_a_directory() { + for bin in BINS { + let fixture = Fixture::new(); + let file = fixture.repo.path().join("Cargo.toml"); + let out = fixture.run(bin, &["index", file.to_str().expect("utf-8 temp path")]); + let (code, env) = envelope(&out); + assert_eq!(code, Some(1), "{bin}: {env}"); + assert_eq!(env["error"]["kind"], "usage", "{bin}: {env}"); + assert_eq!( + env["error"]["message"], "index path is not a directory", + "{bin}: {env}" + ); + assert!( + fs::read_dir(fixture.data.path()) + .expect("data dir") + .next() + .is_none(), + "{bin} wrote the data directory" + ); + } +} diff --git a/crates/phantom-fractal/tests/doctor.rs b/crates/phantom-fractal/tests/doctor.rs index 4c1f57a..5b1ef1f 100644 --- a/crates/phantom-fractal/tests/doctor.rs +++ b/crates/phantom-fractal/tests/doctor.rs @@ -387,3 +387,67 @@ fn a_gone_tree_and_a_missing_lock_file_are_reported_without_a_write() { ); assert!(!lock.exists(), "doctor made a lock file"); } + +#[cfg(unix)] +#[test] +fn a_data_directory_others_can_write_and_an_open_repos_directory_are_flagged() { + use std::os::unix::fs::PermissionsExt; + let world = World::new(); + let data_dir = world.data.path(); + let mode = |path: &Path| fs::metadata(path).expect("meta").permissions().mode() & 0o777; + let original = mode(data_dir); + fs::set_permissions(data_dir, fs::Permissions::from_mode(0o777)).expect("chmod data"); + let data = doctor(data_dir, world.dir.path()); + assert_eq!(data["ok"], false); + let check_data = check(&data, "data_dir"); + assert_eq!(check_data["ok"], false); + assert_eq!( + check_data["detail"], + "the data directory is 0777, writable by others" + ); + assert_eq!(mode(data_dir), 0o777, "doctor narrowed the data directory"); + fs::set_permissions(data_dir, fs::Permissions::from_mode(original)).expect("chmod back"); + + let repos = data_dir.join("repos"); + fs::set_permissions(&repos, fs::Permissions::from_mode(0o755)).expect("chmod repos"); + let data = doctor(data_dir, world.dir.path()); + let check_data = check(&data, "data_dir"); + assert_eq!(check_data["ok"], false); + assert_eq!( + check_data["detail"], + "repos/ is 0755, not a private directory" + ); + assert_eq!(mode(&repos), 0o755, "doctor narrowed repos/"); +} + +#[test] +fn a_database_that_is_not_sqlite_or_has_an_older_schema_is_reported() { + let world = World::new(); + // An older binary's stamp, in `user_version` at byte 60 of the header. + let older = world.database("repo-"); + let mut bytes = fs::read(&older).expect("read"); + bytes[60..64].copy_from_slice(&2_u32.to_be_bytes()); + fs::write(&older, &bytes).expect("write"); + let data = doctor(world.data.path(), world.dir.path()); + let databases = check(&data, "databases"); + assert_eq!(databases["ok"], true, "{databases}"); + let detail = databases["detail"].as_str().expect("detail").to_owned(); + assert!( + detail.contains(&format!( + "{}: schema 2, which index migrates to 3", + world.key("repo-") + )), + "{detail}" + ); + + fs::write(world.database("graph-"), b"not a database").expect("write"); + let data = doctor(world.data.path(), world.dir.path()); + assert_eq!(data["ok"], false); + let databases = check(&data, "databases"); + assert_eq!(databases["ok"], false); + let detail = databases["detail"].as_str().expect("detail").to_owned(); + assert!( + detail.contains(&format!("{}: not an SQLite database", world.key("graph-"))), + "{detail}" + ); +} diff --git a/crates/phantom-fractal/tests/fresh.rs b/crates/phantom-fractal/tests/fresh.rs index 5d75627..730bd72 100644 --- a/crates/phantom-fractal/tests/fresh.rs +++ b/crates/phantom-fractal/tests/fresh.rs @@ -637,3 +637,17 @@ fn status_flags_an_index_from_another_extractor_version() { } } } + +/// With no stored root, or a root with no directory name, `status` names +/// the tree by its key without the hash. +#[test] +fn status_names_a_tree_with_no_stored_root_by_its_key() { + let world = World::fixture(); + world.index(); + world.store().set_root("").expect("clear the root"); + let env = world.ok(&["status"]); + let key = env["data"]["key"].as_str().expect("key"); + let (slug, hash) = key.rsplit_once('-').expect("a hashed key"); + assert_eq!(hash.len(), 8, "{key}"); + assert_eq!(env["data"]["name"], slug, "{env}"); +} diff --git a/crates/phantom-fractal/tests/mcp.rs b/crates/phantom-fractal/tests/mcp.rs index 748ba82..182b492 100644 --- a/crates/phantom-fractal/tests/mcp.rs +++ b/crates/phantom-fractal/tests/mcp.rs @@ -956,3 +956,29 @@ fn stdout_carries_only_json_rpc_lines() { assert!(line.starts_with('{') && line.ends_with('}'), "{line}"); } } + +/// A client's response, a message with a result or an error and no method, +/// gets no reply. A message with no method and neither is refused. +#[test] +fn a_client_response_gets_no_reply_and_a_message_without_a_method_is_refused() { + let mut input = transcript(&[initialize(1, "2025-11-25"), initialized()]); + input.extend(transcript(&[ + json!({ "jsonrpc": "2.0", "id": 2, "result": {} }), + json!({ "jsonrpc": "2.0", "id": 3, "error": { "code": 1, "message": "no" } }), + json!({ "jsonrpc": "2.0", "id": 4 }), + request(5, "ping", json!({})), + ])); + let replies = session(&[], &input); + let got: Vec<(Value, Value)> = replies[1..] + .iter() + .map(|reply| { + let outcome = reply + .get("error") + .map_or_else(|| reply["result"].clone(), |error| error["code"].clone()); + (reply["id"].clone(), outcome) + }) + .collect(); + let want: Vec<(Value, Value)> = vec![(json!(4), json!(-32600)), (json!(5), json!({}))]; + assert_eq!(got, want); + assert_eq!(replies[1]["error"]["message"], "method must be a string"); +} diff --git a/crates/phantom-fractal/tests/rank_map.rs b/crates/phantom-fractal/tests/rank_map.rs index 6a70da2..8712e27 100644 --- a/crates/phantom-fractal/tests/rank_map.rs +++ b/crates/phantom-fractal/tests/rank_map.rs @@ -689,3 +689,50 @@ fn equal_scores_go_by_path_then_line() { ); } } + +/// A damaged index never makes `map` panic or print a partial reply. With +/// any one page of `graph.db` zeroed, it answers in full or fails with one +/// error envelope, and a page `map` reads past the store's own checks +/// fails as the store's error. +#[test] +fn map_over_a_damaged_index_answers_or_fails_with_one_envelope() { + let world = World::new("graph"); + let database = world.database(); + assert!( + !database.with_file_name("graph.db-wal").exists() + || fs::metadata(database.with_file_name("graph.db-wal")) + .expect("wal") + .len() + == 0, + "the index was not checkpointed" + ); + let original = fs::read(&database).expect("read graph.db"); + let page = usize::from(u16::from_be_bytes([original[16], original[17]])); + assert!( + page >= 512 && original.len().is_multiple_of(page), + "page size {page}" + ); + let mut store_failures = 0; + for at in 1..original.len() / page { + let mut damaged = original.clone(); + damaged[at * page..(at + 1) * page].fill(0); + fs::write(&database, &damaged).expect("damage graph.db"); + let run = world.run(BINS[1], &["map", "--budget", "2000"]); + assert!(run.code.is_some(), "map was killed with page {at} zeroed"); + let envelope = run.envelope(); + assert_eq!( + envelope["ok"], + run.code == Some(0), + "page {at}: {}", + run.stdout + ); + if envelope["error"]["message"] + .as_str() + .is_some_and(|message| message.starts_with("store: ")) + { + store_failures += 1; + } + } + fs::write(&database, &original).expect("restore graph.db"); + assert!(store_failures > 0, "no zeroed page reached a store failure"); +} diff --git a/crates/phantom-fractal/tests/show.rs b/crates/phantom-fractal/tests/show.rs index 57000a2..bd55633 100644 --- a/crates/phantom-fractal/tests/show.rs +++ b/crates/phantom-fractal/tests/show.rs @@ -751,3 +751,23 @@ fn source_is_ok_stale_too_large_or_unreadable() { fn symlink(target: &Path, link: &Path) { std::os::unix::fs::symlink(target, link).expect("symlink"); } + +/// Exactly the cap of 100 members is not truncated; one more is. +#[test] +fn a_type_with_exactly_100_members_is_not_truncated() { + for (count, truncated) in [(100, false), (101, true)] { + let mut lib = String::from("pub struct Wide;\n\nimpl Wide {\n"); + for n in 0..count { + writeln!(lib, " pub fn m{n:03}(&self) {{}}").expect("write"); + } + lib.push_str("}\n"); + let fixture = Fixture::generated(&[("src/lib.rs", lib)]); + let wide = fixture.show(BINS[0], &["Wide"]).1; + assert_eq!( + wide["members"]["hits"].as_array().map(Vec::len), + Some(100), + "{count} members" + ); + assert_eq!(wide["members"]["truncated"], truncated, "{count} members"); + } +} diff --git a/crates/phantom-fractal/tests/support/mod.rs b/crates/phantom-fractal/tests/support/mod.rs index b4b5c28..a76da21 100644 --- a/crates/phantom-fractal/tests/support/mod.rs +++ b/crates/phantom-fractal/tests/support/mod.rs @@ -7,7 +7,8 @@ //! fixed committer times and identity, then `git reset --hard` to check //! the tree out. Every run clears the environment and sets //! `GIT_CONFIG_NOSYSTEM=1`, `GIT_CONFIG_GLOBAL=/dev/null`, a temp `HOME` -//! and `core.hooksPath=/dev/null`, so no hook or outside config runs. +//! and `XDG_CONFIG_HOME`, a fixed identity and `core.hooksPath=/dev/null`, +//! so no hook, outside config or host-name lookup runs. //! Phantom itself never runs `git`. #![allow(dead_code)] @@ -19,6 +20,15 @@ use std::process::{self, Command, Stdio}; use std::sync::atomic::{AtomicU32, Ordering}; use std::time::SystemTime; +/// The author and committer every spawned `git` is given, so none derives +/// one from the host name, which costs a DNS lookup. +pub const IDENTITY: [(&str, &str); 4] = [ + ("GIT_AUTHOR_NAME", "Test"), + ("GIT_AUTHOR_EMAIL", "test@example.invalid"), + ("GIT_COMMITTER_NAME", "Test"), + ("GIT_COMMITTER_EMAIL", "test@example.invalid"), +]; + /// HEAD's committer time, unix seconds (2025-06-15). pub const HEAD: i64 = 1_750_000_000; pub const DAY: i64 = 86_400; @@ -119,8 +129,10 @@ impl Repo { .env_clear() .env("PATH", std::env::var_os("PATH").unwrap_or_default()) .env("HOME", &self.home) + .env("XDG_CONFIG_HOME", &self.home) .env("GIT_CONFIG_NOSYSTEM", "1") .env("GIT_CONFIG_GLOBAL", "/dev/null") + .envs(IDENTITY) .env("LC_ALL", "C") .args([ "-c", diff --git a/crates/phantom-fractal/tests/version.rs b/crates/phantom-fractal/tests/version.rs index 35ad829..2f6f52f 100644 --- a/crates/phantom-fractal/tests/version.rs +++ b/crates/phantom-fractal/tests/version.rs @@ -85,9 +85,23 @@ fn built_sha() -> Option { return Some(sha); } } + // An isolated git: no outside config, and a fixed identity, so it never + // derives one from the host name, which costs a DNS lookup. + let home = std::env::temp_dir().join(format!("phantom-fractal-version-{}", std::process::id())); let out = Command::new("git") .args(["rev-parse", "HEAD"]) .current_dir(env!("CARGO_MANIFEST_DIR")) + .env_clear() + .env("PATH", std::env::var_os("PATH").unwrap_or_default()) + .env("HOME", &home) + .env("XDG_CONFIG_HOME", &home) + .env("GIT_CONFIG_NOSYSTEM", "1") + .env("GIT_CONFIG_GLOBAL", "/dev/null") + .env("GIT_AUTHOR_NAME", "Test") + .env("GIT_AUTHOR_EMAIL", "test@example.invalid") + .env("GIT_COMMITTER_NAME", "Test") + .env("GIT_COMMITTER_EMAIL", "test@example.invalid") + .env("LC_ALL", "C") .output() .ok()?; let sha = String::from_utf8(out.stdout).ok()?.trim().to_owned(); diff --git a/crates/phantom-fractal/tests/watch.rs b/crates/phantom-fractal/tests/watch.rs index 80e1360..a15064c 100644 --- a/crates/phantom-fractal/tests/watch.rs +++ b/crates/phantom-fractal/tests/watch.rs @@ -741,3 +741,35 @@ fn a_git_branch_switch() { assert_eq!(counts(&batch), (1, 1, true)); assert_eq!(batch["meta"]["head"], repo.rev("main")); } + +/// A watch whose stdout is closed stops at its first batch with exit 1, +/// as any failed write stops it. +#[test] +fn a_closed_stdout_stops_the_watch_with_exit_one() { + let root = package(); + let data = TempDir::new("watch-data"); + let mut child = Command::new(FRACT) + .args(["watch", "--repo"]) + .arg(root.path()) + .args(["--interval-ms", INTERVAL_MS, "--debounce-ms", DEBOUNCE_MS]) + .env("PHANTOM_FRACTAL_DATA", data.path()) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .spawn() + .expect("spawn watch"); + drop(child.stdout.take()); + let deadline = Instant::now() + BATCH_WAIT; + let code = loop { + if let Some(status) = child.try_wait().expect("try wait") { + break status.code(); + } + if Instant::now() >= deadline { + let _ = child.kill(); + let _ = child.wait(); + panic!("the watch kept running with its stdout closed"); + } + thread::sleep(Duration::from_millis(20)); + }; + assert_eq!(code, Some(1)); +}