From a211b20b778e4f8e8bcd7b741a408365f46a0583 Mon Sep 17 00:00:00 2001 From: "Steven Zimmerman, CPA" <15812269+EffortlessSteven@users.noreply.github.com> Date: Sun, 30 Aug 2026 20:08:58 -0400 Subject: [PATCH 1/4] fix(analysis): normalize repo-seam grip association stems across platforms The repo-seam grip path derived both sides of the same-test-file association with the host-native Path::file_stem: the owner stem in OwnerContext::resolve and the tests_by_file_stem index key in CompactGripContext::new. A path carrying the other platform's separator makes file_stem treat an entire relative path as one file name, so the lookup missed and the SameTestFile relation silently vanished on exactly one host (#3469 residue identified by the 2026-08-29 investigation). Apply the #3545 normalized_file_stem pattern to both sides, mirror it in the cfg(test) same_test_file helper, and pin the contract with a mixed-separator regression test that exercises all four owner/test separator combinations end to end through evidence_for_seam. Classify-side association was already normalized in #3545; this closes the remaining known host-native stem family on the grip path. Zero golden drift (goldens check + dogfood). --- .../test_grip_evidence/related_tests.rs | 30 +++++++++---- .../related_tests/context.rs | 8 ++-- .../src/analysis/test_grip_evidence/tests.rs | 44 +++++++++++++++++++ 3 files changed, 68 insertions(+), 14 deletions(-) diff --git a/crates/ripr/src/analysis/test_grip_evidence/related_tests.rs b/crates/ripr/src/analysis/test_grip_evidence/related_tests.rs index 74ecfea61d9..1371940f786 100644 --- a/crates/ripr/src/analysis/test_grip_evidence/related_tests.rs +++ b/crates/ripr/src/analysis/test_grip_evidence/related_tests.rs @@ -60,11 +60,7 @@ impl OwnerContext { let name = owner_fn.map(|f| f.name.as_str()).unwrap_or("").to_string(); let name_lower = name.to_ascii_lowercase(); let owner_file = owner_fn.map(|f| f.file.as_path()); - let file_stem = owner_file - .and_then(|p| p.file_stem()) - .and_then(|s| s.to_str()) - .unwrap_or("") - .to_string(); + let file_stem = owner_file.map(normalized_file_stem).unwrap_or_default(); let module_path = owner_file.and_then(|file| module_path_for_index(context.index, file)); let prefix = owner_fn.and_then(|f| package_prefix(&f.file)); let fixture_names = owner_file @@ -569,10 +565,10 @@ pub(super) fn assertion_targets_seam(test: &TestSummary, tokens: &[String]) -> b #[cfg(test)] pub(super) fn same_test_file(test_file: &Path, owner_stem: &str) -> bool { - let stem = match test_file.file_stem().and_then(|s| s.to_str()) { - Some(s) => s, - None => return false, - }; + let stem = normalized_file_stem(test_file); + if stem.is_empty() { + return false; + } if stem == owner_stem { return true; } @@ -772,6 +768,22 @@ pub(super) fn normalize_path(path: &Path) -> String { .to_string() } +/// Extract a source-file stem after normalizing separators from either +/// host. Grip inputs can carry paths produced on a different platform +/// than the host resolving the association (#3469); a foreign separator +/// makes [`std::path::Path::file_stem`] treat an entire relative path as +/// one file name, so same-test-file lookups miss on exactly one host. +/// Mirrors classify's `normalized_file_stem`. +pub(super) fn normalized_file_stem(path: &Path) -> String { + normalize_path(path) + .rsplit('/') + .next() + .and_then(|file| Path::new(file).file_stem()) + .and_then(|stem| stem.to_str()) + .unwrap_or_default() + .to_string() +} + pub(super) fn package_prefix(path: &Path) -> Option { let normalized = normalize_path(path); if let Some(rest) = normalized.strip_prefix("crates/") diff --git a/crates/ripr/src/analysis/test_grip_evidence/related_tests/context.rs b/crates/ripr/src/analysis/test_grip_evidence/related_tests/context.rs index 120be1f98f5..2887704ec02 100644 --- a/crates/ripr/src/analysis/test_grip_evidence/related_tests/context.rs +++ b/crates/ripr/src/analysis/test_grip_evidence/related_tests/context.rs @@ -159,11 +159,9 @@ impl<'a> CompactGripContext<'a> { .or_default() .push(test_index); } - if let Some(stem) = test.file.file_stem().and_then(|stem| stem.to_str()) { - tests_by_file_stem - .entry(stem.to_string()) - .or_default() - .push(test_index); + let stem = normalized_file_stem(&test.file); + if !stem.is_empty() { + tests_by_file_stem.entry(stem).or_default().push(test_index); } for token in import_affinity_tokens(&code_lines) { tests_by_import_token diff --git a/crates/ripr/src/analysis/test_grip_evidence/tests.rs b/crates/ripr/src/analysis/test_grip_evidence/tests.rs index 297cc4791b5..a9c65312f21 100644 --- a/crates/ripr/src/analysis/test_grip_evidence/tests.rs +++ b/crates/ripr/src/analysis/test_grip_evidence/tests.rs @@ -10230,6 +10230,50 @@ fn given_same_module_test_without_direct_call_when_related_tests_are_ranked_then Ok(()) } +#[test] +fn given_foreign_separator_paths_when_grip_associates_then_same_test_file_still_matches() +-> Result<(), String> { + // #3469: the repo-seam grip path derived both association stems with + // the host-native `Path::file_stem`, so a path carrying the other + // platform's separator (facts materialized on Windows, analyzed on + // Linux, or vice versa) never matched and the SameTestFile relation + // silently vanished. Every separator combination must associate; the + // test body stays free of owner/discriminator identifiers so + // SameTestFile is the only reason that can fire. + let prod_src = "pub fn apply_discount(amount: i32, threshold: i32) -> i32 \ + { if amount >= threshold { amount - 10 } else { amount } }\n"; + let test_src = "#[test] fn bounds_smoke() { assert_eq!(1, 1); }\n"; + let combinations = [ + ("src/pricing.rs", "tests/pricing_test.rs"), + ("src\\pricing.rs", "tests\\pricing_test.rs"), + ("src/pricing.rs", "tests\\pricing_test.rs"), + ("src\\pricing.rs", "tests/pricing_test.rs"), + ]; + for (owner_path, test_path) in combinations { + let files: Vec<(PathBuf, &str)> = vec![ + (PathBuf::from(owner_path), prod_src), + (PathBuf::from(test_path), test_src), + ]; + let index = index_from_files(&files)?; + let seams = inventory_seams_from_index(&[PathBuf::from(owner_path)], &index); + let predicate = seams + .iter() + .find(|s| s.kind() == SeamKind::PredicateBoundary) + .ok_or_else(|| "predicate seam present".to_string())?; + let evidence = evidence_for_seam(predicate, &index); + assert!( + evidence + .related_tests + .iter() + .any(|grip| grip.relation_reason == RelationReason::SameTestFile), + "same-test-file association must hold for owner `{owner_path}` / test \ + `{test_path}`; got {:?}", + evidence.related_tests + ); + } + Ok(()) +} + // -- helper coverage --------------------------------------------- // // Targeted unit tests for the small private helpers introduced by From de37046bdc88adb91b4253cce76a9e66b425f27d Mon Sep 17 00:00:00 2001 From: "Steven Zimmerman, CPA" <15812269+EffortlessSteven@users.noreply.github.com> Date: Sun, 30 Aug 2026 20:56:27 -0400 Subject: [PATCH 2/4] test(grip): carry association diagnostics in the foreign-separator failure message --- .../src/analysis/test_grip_evidence/tests.rs | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/crates/ripr/src/analysis/test_grip_evidence/tests.rs b/crates/ripr/src/analysis/test_grip_evidence/tests.rs index a9c65312f21..a6b259ec07d 100644 --- a/crates/ripr/src/analysis/test_grip_evidence/tests.rs +++ b/crates/ripr/src/analysis/test_grip_evidence/tests.rs @@ -10261,19 +10261,50 @@ fn given_foreign_separator_paths_when_grip_associates_then_same_test_file_still_ .find(|s| s.kind() == SeamKind::PredicateBoundary) .ok_or_else(|| "predicate seam present".to_string())?; let evidence = evidence_for_seam(predicate, &index); + let context = CompactGripContext::new(&index); + let owner_fn = crate::analysis::rust_index::find_owner_function( + &index, + predicate.file(), + predicate.display_line(), + ); + let ctx_tests: Vec = context + .tests + .iter() + .map(|t| t.test.file.to_string_lossy().to_string()) + .collect(); assert!( evidence .related_tests .iter() .any(|grip| grip.relation_reason == RelationReason::SameTestFile), "same-test-file association must hold for owner `{owner_path}` / test \ - `{test_path}`; got {:?}", - evidence.related_tests + `{test_path}`; got {:?} | diag: index_files={:?} index_tests={} \ + ctx_tests={ctx_tests:?} stem_keys={:?} seam_file={:?} seam_line={} \ + owner={owner_fn:?} owner_stem_norm={:?} owner_stem_native={:?} \ + test_stem_norm={:?} test_stem_native={:?}", + evidence.related_tests, + index.files.keys().collect::>(), + index.tests.len(), + context.tests_by_file_stem.keys().collect::>(), + predicate.file(), + predicate.display_line(), + normalized_file_stem_to_string(Path::new(owner_path)), + Path::new(owner_path) + .file_stem() + .map(|s| s.to_string_lossy().to_string()), + normalized_file_stem_to_string(Path::new(test_path)), + Path::new(test_path) + .file_stem() + .map(|s| s.to_string_lossy().to_string()), ); } Ok(()) } +fn normalized_file_stem_to_string(path: &Path) -> String { + super::related_tests::normalized_file_stem(path) +} + // -- helper coverage --------------------------------------------- // // Targeted unit tests for the small private helpers introduced by From de28bd9cd7762f29551c5bb364e338b0c6c44920 Mon Sep 17 00:00:00 2001 From: Steven Zimmerman Date: Sun, 30 Aug 2026 21:19:13 -0400 Subject: [PATCH 3/4] fix(analysis): resolve repo-seam grip associations across platform path separators A foreign-separator path broke the grip association in two stacked places (#3469): the owner lookup compared the seam's /-normalized file identity against raw host-separator index keys (component-wise Path equality only absorbs this on backslash-native hosts), and both association stems were derived with the host-native Path::file_stem. is_test_file had the same component-vs-string divergence, and lossy non-UTF-8 stems could collapse distinct names into one stem. - find_owner_function: normalized-key fallback on the miss path only - normalized_file_stem: separator-normalized, fails closed on non-UTF-8 paths (mirrors #3545 cross_host_stem) - is_test_file: judge the normalized path form - regression: all four separator combinations associate end-to-end; unix pin for the non-UTF-8 fail-closed behavior Verified by WSL reproduction of the CI failure and a green Linux lib suite; native-separator behavior is unchanged (zero golden drift). --- crates/ripr/src/analysis/rust_index.rs | 62 ++++++++++++++++--- .../test_grip_evidence/related_tests.rs | 15 +++-- .../src/analysis/test_grip_evidence/tests.rs | 32 +++++++--- 3 files changed, 90 insertions(+), 19 deletions(-) diff --git a/crates/ripr/src/analysis/rust_index.rs b/crates/ripr/src/analysis/rust_index.rs index e5b1209009b..cb6a7c7d737 100644 --- a/crates/ripr/src/analysis/rust_index.rs +++ b/crates/ripr/src/analysis/rust_index.rs @@ -178,13 +178,37 @@ pub fn find_owner_function<'a>( file: &Path, line: usize, ) -> Option<&'a FunctionSummary> { - index.files.get(file).and_then(|summary| { + let owner_in = |summary: &'a FileFacts| { summary .functions .iter() .filter(|f| f.start_line <= line && line <= f.end_line) .max_by_key(|f| f.start_line) - }) + }; + if let Some(summary) = index.files.get(file) { + return owner_in(summary); + } + // Repo seams normalize their file identity to `/` separators + // (#3469 family) while index keys keep the producing host's + // separators. On the opposite-separator host the component-wise + // lookup above misses (`src\pricing.rs` is a single component on + // Linux), so fall back to matching normalized key forms. The scan + // only runs after the direct lookup missed, so native-separator + // inputs keep the exact-lookup cost. + let target = normalize_display(file); + index + .files + .iter() + .find(|(key, _)| normalize_display(key) == target) + .and_then(|(_, summary)| owner_in(summary)) +} + +/// `/`-separated display form of a path, independent of the host's +/// native separator. Shared by the normalized-key fallback in +/// [`find_owner_function`] and [`is_test_file`] so both judge the same +/// normalized form. +fn normalize_display(path: &Path) -> String { + path.to_string_lossy().replace('\\', "/") } pub fn changed_nodes_for_lines( @@ -208,17 +232,41 @@ pub fn changed_nodes_for_lines( } pub(crate) fn is_test_file(path: &Path) -> bool { - path.starts_with("tests") - || path - .to_string_lossy() - .replace('\\', "/") - .contains("/tests/") + let normalized = normalize_display(path); + normalized == "tests" || normalized.starts_with("tests/") || normalized.contains("/tests/") } #[cfg(test)] mod tests { use super::*; + #[test] + fn find_owner_function_resolves_normalized_seam_paths() { + let facts = summarize_file( + PathBuf::from("src\\pricing.rs"), + "fn apply_discount(amount: i32, threshold: i32) -> i32 { \ + if amount >= threshold { amount - 10 } else { amount } }\n" + .to_string(), + ); + let mut index = RustIndex::default(); + index.files.insert(PathBuf::from("src\\pricing.rs"), facts); + // Repo seams carry `/`-normalized file identity; on Linux this is + // not component-equal to the `\`-separator index key, so the + // direct lookup misses and the normalized fallback must resolve. + let owner = find_owner_function(&index, Path::new("src/pricing.rs"), 1); + assert_eq!(owner.map(|f| f.name.as_str()), Some("apply_discount")); + } + + #[test] + fn is_test_file_judges_foreign_separator_forms_like_native_ones() { + assert!(is_test_file(Path::new("tests\\pricing_test.rs"))); + assert!(is_test_file(Path::new("tests/pricing_test.rs"))); + assert!(is_test_file(Path::new("crates\\demo\\tests\\main.rs"))); + assert!(!is_test_file(Path::new("src\\pricing.rs"))); + // A `tests`-prefixed sibling directory name stays production. + assert!(!is_test_file(Path::new("testing\\pricing.rs"))); + } + #[test] fn finds_tests_and_assertions() { let file = summarize_file( diff --git a/crates/ripr/src/analysis/test_grip_evidence/related_tests.rs b/crates/ripr/src/analysis/test_grip_evidence/related_tests.rs index 1371940f786..b90397b10a7 100644 --- a/crates/ripr/src/analysis/test_grip_evidence/related_tests.rs +++ b/crates/ripr/src/analysis/test_grip_evidence/related_tests.rs @@ -773,12 +773,17 @@ pub(super) fn normalize_path(path: &Path) -> String { /// than the host resolving the association (#3469); a foreign separator /// makes [`std::path::Path::file_stem`] treat an entire relative path as /// one file name, so same-test-file lookups miss on exactly one host. -/// Mirrors classify's `normalized_file_stem`. +/// Non-UTF-8 paths fail closed: lossy replacement characters could +/// collapse distinct file names into one stem and fabricate a +/// same-test-file relation (the #3545 `cross_host_stem` guard). pub(super) fn normalized_file_stem(path: &Path) -> String { - normalize_path(path) - .rsplit('/') - .next() - .and_then(|file| Path::new(file).file_stem()) + let Some(text) = path.to_str() else { + return String::new(); + }; + let unified = text.replace('\\', "/"); + let file = unified.rsplit('/').next().unwrap_or_default(); + Path::new(file) + .file_stem() .and_then(|stem| stem.to_str()) .unwrap_or_default() .to_string() diff --git a/crates/ripr/src/analysis/test_grip_evidence/tests.rs b/crates/ripr/src/analysis/test_grip_evidence/tests.rs index a6b259ec07d..10f4e42a230 100644 --- a/crates/ripr/src/analysis/test_grip_evidence/tests.rs +++ b/crates/ripr/src/analysis/test_grip_evidence/tests.rs @@ -10233,13 +10233,16 @@ fn given_same_module_test_without_direct_call_when_related_tests_are_ranked_then #[test] fn given_foreign_separator_paths_when_grip_associates_then_same_test_file_still_matches() -> Result<(), String> { - // #3469: the repo-seam grip path derived both association stems with - // the host-native `Path::file_stem`, so a path carrying the other - // platform's separator (facts materialized on Windows, analyzed on - // Linux, or vice versa) never matched and the SameTestFile relation - // silently vanished. Every separator combination must associate; the - // test body stays free of owner/discriminator identifiers so - // SameTestFile is the only reason that can fire. + // #3469: a path carrying the other platform's separator (facts + // materialized on Windows, analyzed on Linux, or vice versa) broke + // the SameTestFile relation in two stacked places: the owner lookup + // compared the seam's `/`-normalized file identity against raw + // host-separator index keys (`Path` component equality only + // absorbs this on `\`-native hosts), and both association stems + // were derived with the host-native `Path::file_stem`. Every + // separator combination must associate; the test body stays free + // of owner/discriminator identifiers so SameTestFile is the only + // reason that can fire. let prod_src = "pub fn apply_discount(amount: i32, threshold: i32) -> i32 \ { if amount >= threshold { amount - 10 } else { amount } }\n"; let test_src = "#[test] fn bounds_smoke() { assert_eq!(1, 1); }\n"; @@ -10305,6 +10308,21 @@ fn normalized_file_stem_to_string(path: &Path) -> String { super::related_tests::normalized_file_stem(path) } +#[test] +#[cfg(unix)] +fn given_non_utf8_stems_when_grip_associates_then_lossy_collision_fails_closed() { + use std::ffi::OsStr; + use std::os::unix::ffi::OsStrExt; + // Two distinct non-UTF-8 names can share one lossy rendering; + // deriving stems through lossy conversion would fabricate a + // same-test-file relation between unrelated entities, so stem + // extraction must fail closed instead. + let first = Path::new(OsStr::from_bytes(b"tests/pricing_\xff.rs")); + let second = Path::new(OsStr::from_bytes(b"tests/pricing_\xfe.rs")); + assert_eq!(normalized_file_stem(first), ""); + assert_eq!(normalized_file_stem(second), ""); +} + // -- helper coverage --------------------------------------------- // // Targeted unit tests for the small private helpers introduced by From 54d3ed65e56bb9177a3443b732d2970cf33f109f Mon Sep 17 00:00:00 2001 From: Steven Zimmerman Date: Sun, 30 Aug 2026 21:52:30 -0400 Subject: [PATCH 4/4] fix(analysis): fail closed on non-UTF-8 paths in the owner-lookup fallback The normalized-key fallback compared lossy renderings, so two distinct non-UTF-8 index keys sharing one replacement-character form could attribute a seam to the wrong file. Both sides of the fallback comparison now require valid UTF-8; non-UTF-8 queries return None and non-UTF-8 keys are skipped. is_test_file keeps lossy normalization: replacement characters can never become separators there. Pinned by a unix unit test asserting a lossy-colliding query resolves to None. (devin + coderabbit review findings on de28bd9cd) --- crates/ripr/src/analysis/rust_index.rs | 46 ++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/crates/ripr/src/analysis/rust_index.rs b/crates/ripr/src/analysis/rust_index.rs index cb6a7c7d737..216d79cd8db 100644 --- a/crates/ripr/src/analysis/rust_index.rs +++ b/crates/ripr/src/analysis/rust_index.rs @@ -194,19 +194,27 @@ pub fn find_owner_function<'a>( // lookup above misses (`src\pricing.rs` is a single component on // Linux), so fall back to matching normalized key forms. The scan // only runs after the direct lookup missed, so native-separator - // inputs keep the exact-lookup cost. - let target = normalize_display(file); + // inputs keep the exact-lookup cost. Non-UTF-8 paths fail closed: + // lossy replacement characters can collapse distinct names into + // one form and attribute the wrong owner, so neither side of the + // comparison may pass through lossy conversion. + let target = file.to_str()?.replace('\\', "/"); index .files .iter() - .find(|(key, _)| normalize_display(key) == target) - .and_then(|(_, summary)| owner_in(summary)) + .find_map(|(key, summary)| { + let key_text = key.to_str()?; + (key_text.replace('\\', "/") == target).then_some(summary) + }) + .and_then(owner_in) } /// `/`-separated display form of a path, independent of the host's -/// native separator. Shared by the normalized-key fallback in -/// [`find_owner_function`] and [`is_test_file`] so both judge the same -/// normalized form. +/// native separator. Used by [`is_test_file`] to judge the normalized +/// form; lossy text is safe here because replacement characters can +/// never become separators. The [`find_owner_function`] fallback does +/// NOT use this helper — identity attribution must not pass through +/// lossy conversion. fn normalize_display(path: &Path) -> String { path.to_string_lossy().replace('\\', "/") } @@ -267,6 +275,30 @@ mod tests { assert!(!is_test_file(Path::new("testing\\pricing.rs"))); } + #[test] + #[cfg(unix)] + fn find_owner_function_fails_closed_on_non_utf8_fallback() { + use std::ffi::OsStr; + use std::os::unix::ffi::OsStrExt; + let facts = summarize_file( + PathBuf::from(OsStr::from_bytes(b"src/pricing_\xff.rs")), + "fn apply_discount(amount: i32) -> i32 { amount }\n".to_string(), + ); + let mut index = RustIndex::default(); + index.files.insert( + PathBuf::from(OsStr::from_bytes(b"src/pricing_\xff.rs")), + facts, + ); + // A query whose lossy rendering collides with the indexed key + // must not attribute the owner through lossy text: the fallback + // requires valid UTF-8 on both sides. + let colliding = Path::new(OsStr::from_bytes(b"src\\pricing_\xfe.rs")); + assert_eq!( + find_owner_function(&index, colliding, 1).map(|f| f.name.as_str()), + None + ); + } + #[test] fn finds_tests_and_assertions() { let file = summarize_file(