@@ -639,17 +639,18 @@ fn copy_dir_all(src: &Path, dest: &Path) -> Result<()> {
639639
640640/// Find the best-matching sketch name from `candidates` for a given `query`.
641641///
642- /// Uses the Jaro-Winkler distance (via `strsim`) to score each candidate.
642+ /// Uses kernel-owned Jaro-Winkler similarity to score each candidate.
643643/// Returns the candidate(s) with the highest score.
644644///
645645/// When `query` is an exact substring of a candidate that candidate is
646646/// returned immediately (mirrors the Python `partial_name in sketch_str`
647647/// fast-path in `sketch.py::find_sketch_by_partial_name`).
648648///
649649/// Returns an empty `Vec` when `candidates` is empty.
650- pub fn best_sketch_match ( query : & str , candidates : & [ & str ] ) -> Vec < String > {
650+ /// Returns an error if fuzzy scoring exceeds the kernel's resource limits.
651+ pub fn best_sketch_match ( query : & str , candidates : & [ & str ] ) -> Result < Vec < String > > {
651652 if candidates. is_empty ( ) {
652- return Vec :: new ( ) ;
653+ return Ok ( Vec :: new ( ) ) ;
653654 }
654655
655656 let q_lower = query. to_lowercase ( ) ;
@@ -661,25 +662,29 @@ pub fn best_sketch_match(query: &str, candidates: &[&str]) -> Vec<String> {
661662 . filter ( |c| c. to_lowercase ( ) . contains ( & q_lower) )
662663 . collect ( ) ;
663664 if !substring_hits. is_empty ( ) {
664- return substring_hits. iter ( ) . map ( |s| s. to_string ( ) ) . collect ( ) ;
665+ return Ok ( substring_hits. iter ( ) . map ( |s| s. to_string ( ) ) . collect ( ) ) ;
665666 }
666667
667668 // Fuzzy: Jaro-Winkler distance.
668669 let mut scored: Vec < ( f64 , & str ) > = candidates
669670 . iter ( )
670- . map ( |c| ( strsim:: jaro_winkler ( & q_lower, & c. to_lowercase ( ) ) , * c) )
671- . collect ( ) ;
671+ . map ( |c| {
672+ let score = kernal_api:: text:: name_similarity ( & q_lower, & c. to_lowercase ( ) )
673+ . context ( "score sketch-name suggestion" ) ?;
674+ Ok ( ( score, * c) )
675+ } )
676+ . collect :: < Result < _ > > ( ) ?;
672677
673678 // Sort descending by score.
674679 scored. sort_by ( |a, b| b. 0 . partial_cmp ( & a. 0 ) . unwrap_or ( std:: cmp:: Ordering :: Equal ) ) ;
675680
676681 let best_score = scored[ 0 ] . 0 ;
677682 // Return all candidates tied at the best score (within floating-point epsilon).
678- scored
683+ Ok ( scored
679684 . iter ( )
680685 . take_while ( |( s, _) | ( s - best_score) . abs ( ) < 1e-9 )
681686 . map ( |( _, name) | name. to_string ( ) )
682- . collect ( )
687+ . collect ( ) )
683688}
684689
685690/// Find a sketch directory by partial name match using the Python-side rules.
@@ -1063,10 +1068,31 @@ mod tests {
10631068 // best_sketch_match (fuzzy matching)
10641069 // ------------------------------------------------------------------
10651070
1071+ #[ test]
1072+ fn sketch_matching_preserves_substring_precedence_and_stable_ties ( ) {
1073+ assert_eq ! (
1074+ best_sketch_match( "Blink" , & [ "Blin" , "BlinkFast" , "Blink" ] ) . unwrap( ) ,
1075+ [ "BlinkFast" , "Blink" ]
1076+ ) ;
1077+ assert_eq ! (
1078+ best_sketch_match( "BLNK" , & [ "Blink" , "BLINK" , "Fire" ] ) . unwrap( ) ,
1079+ [ "Blink" , "BLINK" ]
1080+ ) ;
1081+ }
1082+
1083+ #[ test]
1084+ fn sketch_matching_reports_resource_errors_without_partial_ranking ( ) {
1085+ let error = best_sketch_match ( & "x" . repeat ( 4097 ) , & [ "Blink" ] ) . unwrap_err ( ) ;
1086+ assert ! ( format!( "{error:#}" ) . contains( "byte limit" ) ) ;
1087+ let candidate = "y" . repeat ( 1024 ) ;
1088+ let error = best_sketch_match ( & "x" . repeat ( 1025 ) , & [ "Blink" , & candidate] ) . unwrap_err ( ) ;
1089+ assert ! ( format!( "{error:#}" ) . contains( "work limit" ) ) ;
1090+ }
1091+
10661092 #[ test]
10671093 fn test_best_sketch_match_exact_substring ( ) {
10681094 let candidates = [ "Blink" , "BlinkFast" , "Fire2012" ] ;
1069- let matches = best_sketch_match ( "Blink" , & candidates) ;
1095+ let matches = best_sketch_match ( "Blink" , & candidates) . unwrap ( ) ;
10701096 assert ! (
10711097 matches. contains( & "Blink" . to_owned( ) ) ,
10721098 "Blink should match: {matches:?}"
@@ -1084,22 +1110,22 @@ mod tests {
10841110 #[ test]
10851111 fn test_best_sketch_match_fuzzy_fallback ( ) {
10861112 let candidates = [ "Blink" , "Fire2012" , "Noise" ] ;
1087- let matches = best_sketch_match ( "blnk" , & candidates) ;
1113+ let matches = best_sketch_match ( "blnk" , & candidates) . unwrap ( ) ;
10881114 // Jaro-Winkler should rank "Blink" highest.
10891115 assert ! ( !matches. is_empty( ) , "fuzzy match should return results" ) ;
10901116 assert_eq ! ( matches[ 0 ] , "Blink" , "best fuzzy match should be Blink" ) ;
10911117 }
10921118
10931119 #[ test]
10941120 fn test_best_sketch_match_empty_candidates ( ) {
1095- let matches = best_sketch_match ( "anything" , & [ ] ) ;
1121+ let matches = best_sketch_match ( "anything" , & [ ] ) . unwrap ( ) ;
10961122 assert ! ( matches. is_empty( ) ) ;
10971123 }
10981124
10991125 #[ test]
11001126 fn test_best_sketch_match_case_insensitive ( ) {
11011127 let candidates = [ "Blink" , "Fire2012" ] ;
1102- let matches = best_sketch_match ( "BLINK" , & candidates) ;
1128+ let matches = best_sketch_match ( "BLINK" , & candidates) . unwrap ( ) ;
11031129 assert ! (
11041130 matches. contains( & "Blink" . to_owned( ) ) ,
11051131 "case-insensitive match: {matches:?}"
0 commit comments