Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
style(rust): make cargo fmt --check and clippy -D warnings pass
kevin-gatimu0b38c009c118
diff --git a/laya-rust/src/language_detection.rs b/laya-rust/src/language_detection.rsindex bc7a4c47..b980a08e 100644--- a/laya-rust/src/language_detection.rs+++ b/laya-rust/src/language_detection.rs@@ -502,13 +502,15 @@ impl LanguageDetection {let mut current = String::new();let mut script = None;let is_word = |word: &str| {- word.chars().count() >= 2- && word.chars().next().is_some_and(|c| !c.is_uppercase())+ word.chars().count() >= 2 && word.chars().next().is_some_and(|c| !c.is_uppercase())};for ch in text.chars() {- if matches!(get_general_category(ch), GeneralCategory::NonspacingMark- | GeneralCategory::SpacingMark | GeneralCategory::EnclosingMark)- {+ if matches!(+ get_general_category(ch),+ GeneralCategory::NonspacingMark+ | GeneralCategory::SpacingMark+ | GeneralCategory::EnclosingMark+ ) {continue;}let next = if Self::is_alpha(ch) && !Self::is_latin_codepoint(ch as u32) {@@ -544,7 +546,8 @@ impl LanguageDetection {};let n_non_latin = (non_latin * text.chars().filter(|&c| Self::is_alpha(c)).count() as f64).round_ties_even();- if script == "latin" && Self::has_non_latin_words(&text)+ if script == "latin"+ && Self::has_non_latin_words(&text)&& (non_latin >= 0.2 || (non_latin >= 0.1 && n_non_latin >= 10.0)){// Preserve Python's first-seen tie order, rather than HashMap iteration order.diff --git a/laya-rust/src/laya_email.rs b/laya-rust/src/laya_email.rsindex d444a950..c543eb32 100644--- a/laya-rust/src/laya_email.rs+++ b/laya-rust/src/laya_email.rs@@ -150,9 +150,10 @@ impl LayaEmail {let mut pieces = Vec::new();let mut buf = String::new();for line in part.lines().map(str::trim).filter(|l| !l.is_empty()) {- let starts_sentence = line.chars().find(|&c| {- crate::language_detection::LanguageDetection::is_alpha(c)- }).is_some_and(char::is_uppercase);+ let starts_sentence = line+ .chars()+ .find(|&c| crate::language_detection::LanguageDetection::is_alpha(c))+ .is_some_and(char::is_uppercase);if !buf.is_empty() && starts_sentence {pieces.push(std::mem::take(&mut buf));}diff --git a/laya-rust/src/laya_engine.rs b/laya-rust/src/laya_engine.rsindex 4b83792f..23029165 100644--- a/laya-rust/src/laya_engine.rs+++ b/laya-rust/src/laya_engine.rs@@ -689,7 +689,9 @@ fn build_answer(question: &Question, p: &[f64], action: ActionInfo) -> Answer {let expected = Calibration::round4(Calibration::expected_score(p));Answer::Score(ScoreAnswer::new(expected,- score.levels().iter()+ score+ .levels()+ .iter().map(|level| Value::String(crate::python_json::PythonJson::criterion(level))).collect(),probabilities,@@ -830,12 +832,18 @@ mod tests {fn score_legends_render_structured_levels_as_strings() {let question = Question::score("severity",- [json!({"label": "info", "cvss": 0.0}), json!({"label": "high", "cvss": 7.5})],- ).unwrap();+ [+ json!({"label": "info", "cvss": 0.0}),+ json!({"label": "high", "cvss": 7.5}),+ ],+ )+ .unwrap();let Answer::Score(answer) = build_answer(&question,&[0.25, 0.75],- ActionInfo { act_probability: 1.0 },+ ActionInfo {+ act_probability: 1.0,+ },) else {panic!("score question must produce a score answer");};diff --git a/laya-rust/src/sequence_builder.rs b/laya-rust/src/sequence_builder.rsindex 4f8a9b9b..09e1f017 100644--- a/laya-rust/src/sequence_builder.rs+++ b/laya-rust/src/sequence_builder.rs@@ -139,8 +139,15 @@ impl SequenceBuilder {head_max_len: usize,truncate_left: bool,) -> Result<(Vec<u32>, Vec<usize>)> {- Self::build_with_stats(tokenizer, state, question, max_len, head_max_len, truncate_left)- .map(|(ids, markers, _)| (ids, markers))+ Self::build_with_stats(+ tokenizer,+ state,+ question,+ max_len,+ head_max_len,+ truncate_left,+ )+ .map(|(ids, markers, _)| (ids, markers))}/// The question's own part of the sequence: everything up to and including the SEP thatdiff --git a/laya-rust/tests/common/result_compare.rs b/laya-rust/tests/common/result_compare.rsindex 6869292e..5e935893 100644--- a/laya-rust/tests/common/result_compare.rs+++ b/laya-rust/tests/common/result_compare.rs@@ -95,41 +95,75 @@ pub fn assert_answers_match(where_case: &str, expected: &Value, result: &LayaRes}pub fn assert_state_usage_matches(where_case: &str, expected: &Value, result: &LayaResult) {- let usage = result.state_usage().expect("engine result must report state usage");+ let usage = result+ .state_usage()+ .expect("engine result must report state usage");let want = &expected["usage"];let options = want.get("options").and_then(Value::as_object);- assert_eq!(want.as_object().unwrap().len(), 6 + usize::from(options.is_some()),- "{where_case}: new Python usage fields need a Rust port");- assert_eq!(options.map_or(0, |o| o.len()), usage.options.len(),- "{where_case}: usage.options count");+ assert_eq!(+ want.as_object().unwrap().len(),+ 6 + usize::from(options.is_some()),+ "{where_case}: new Python usage fields need a Rust port"+ );+ assert_eq!(+ options.map_or(0, |o| o.len()),+ usage.options.len(),+ "{where_case}: usage.options count"+ );if let Some(options) = options {for (id, expected) in options {- assert_eq!(expected.as_object().unwrap().len(), 3,- "{where_case}/{id}: new option usage fields need a Rust port");- let got = usage.options.get(id).expect("collapsed option diagnostic missing");- assert_eq!(expected["total"].as_u64().unwrap() as usize, got.total,- "{where_case}/{id}: usage.options.total");- assert_eq!(expected["distinct"].as_u64().unwrap() as usize, got.distinct,- "{where_case}/{id}: usage.options.distinct");- assert_eq!(expected["tokens_per_option"].as_u64().map(|v| v as usize), got.tokens_per_option,- "{where_case}/{id}: usage.options.tokens_per_option");+ assert_eq!(+ expected.as_object().unwrap().len(),+ 3,+ "{where_case}/{id}: new option usage fields need a Rust port"+ );+ let got = usage+ .options+ .get(id)+ .expect("collapsed option diagnostic missing");+ assert_eq!(+ expected["total"].as_u64().unwrap() as usize,+ got.total,+ "{where_case}/{id}: usage.options.total"+ );+ assert_eq!(+ expected["distinct"].as_u64().unwrap() as usize,+ got.distinct,+ "{where_case}/{id}: usage.options.distinct"+ );+ assert_eq!(+ expected["tokens_per_option"].as_u64().map(|v| v as usize),+ got.tokens_per_option,+ "{where_case}/{id}: usage.options.tokens_per_option"+ );}}assert_eq!(- want["state_tokens"].as_u64().unwrap() as usize, usage.state_tokens,+ want["state_tokens"].as_u64().unwrap() as usize,+ usage.state_tokens,"{where_case}: usage.state_tokens");assert_eq!(- want["state_tokens_dropped"].as_u64().unwrap() as usize, usage.state_tokens_dropped,+ want["state_tokens_dropped"].as_u64().unwrap() as usize,+ usage.state_tokens_dropped,"{where_case}: usage.state_tokens_dropped");assert_eq!(- want["truncated"].as_bool().unwrap(), usage.truncated,+ want["truncated"].as_bool().unwrap(),+ usage.truncated,"{where_case}: usage.truncated");- let ids: Vec<&str> = want["truncated_questions"].as_array().unwrap()- .iter().map(|id| id.as_str().unwrap()).collect();- let got: Vec<&str> = usage.truncated_questions.iter().map(String::as_str).collect();+ let ids: Vec<&str> = want["truncated_questions"]+ .as_array()+ .unwrap()+ .iter()+ .map(|id| id.as_str().unwrap())+ .collect();+ let got: Vec<&str> = usage+ .truncated_questions+ .iter()+ .map(String::as_str)+ .collect();assert_eq!(ids, got, "{where_case}: usage.truncated_questions");}@@ -161,11 +195,7 @@ fn assert_score(want: &Value, got: &ScoreAnswer, where_q: &str) {assert_eq!(legend.len(), got.legend.len(), "{where_q}: legend length");for i in 0..got.legend.len() {let expected_level = &legend[&i.to_string()];- assert_eq!(- expected_level,- &got.legend[i],- "{where_q}: legend[{i}]"- );+ assert_eq!(expected_level, &got.legend[i], "{where_q}: legend[{i}]");}let probabilities = want["probabilities"].as_object().unwrap();for i in 0..got.probabilities.len() {diff --git a/laya-rust/tests/common/test_artifacts.rs b/laya-rust/tests/common/test_artifacts.rsindex c1a051b8..2c6d26a5 100644--- a/laya-rust/tests/common/test_artifacts.rs+++ b/laya-rust/tests/common/test_artifacts.rs@@ -67,7 +67,10 @@ pub fn locate_split(checkpoint: LayaCheckpoint) -> Option<PathBuf> {&& !root.trim().is_empty(){let candidate = PathBuf::from(root).join(subfolder);- return candidate.join("rl_agent_config.json").is_file().then_some(candidate);+ return candidate+ .join("rl_agent_config.json")+ .is_file()+ .then_some(candidate);}let mut dir = Some(Path::new(env!("CARGO_MANIFEST_DIR")).to_path_buf());while let Some(d) = dir {diff --git a/laya-rust/tests/laya_router_tests.rs b/laya-rust/tests/laya_router_tests.rsindex 337f19f4..1681cc01 100644--- a/laya-rust/tests/laya_router_tests.rs+++ b/laya-rust/tests/laya_router_tests.rs@@ -207,7 +207,11 @@ fn default_keeps_two_checkpoints_like_python() {router.load("multilingual").unwrap();router.load("english").unwrap();assert_eq!(router.loaded().len(), 2, "both checkpoints stay resident");- assert_eq!(log.lock().unwrap().len(), 2, "switching back does not rebuild english");+ assert_eq!(+ log.lock().unwrap().len(),+ 2,+ "switching back does not rebuild english"+ );}#[test]diff --git a/laya-rust/tests/predict_parity_tests.rs b/laya-rust/tests/predict_parity_tests.rsindex 36cac94a..a894c05d 100644--- a/laya-rust/tests/predict_parity_tests.rs+++ b/laya-rust/tests/predict_parity_tests.rs@@ -440,11 +440,7 @@ fn assert_score(want: &Value, got: &ScoreAnswer, where_q: &str) {assert_eq!(legend.len(), got.legend.len(), "{where_q}: legend length");for i in 0..got.legend.len() {let expected_level = &legend[&i.to_string()];- assert_eq!(- expected_level,- &got.legend[i],- "{where_q}: legend[{i}]"- );+ assert_eq!(expected_level, &got.legend[i], "{where_q}: legend[{i}]");}let probabilities = want["probabilities"].as_object().unwrap();diff --git a/laya-rust/tests/python_json_tests.rs b/laya-rust/tests/python_json_tests.rsindex 320107a7..83068d5f 100644--- a/laya-rust/tests/python_json_tests.rs+++ b/laya-rust/tests/python_json_tests.rs@@ -139,12 +139,18 @@ fn repr_matches_python_bit_pattern_sweep() {}let probe = data.load("repr_probe.json");if let Some(expected) = &reference {- assert_eq!(expected, &probe, "repr probes must agree across checkpoints");+ assert_eq!(+ expected, &probe,+ "repr probes must agree across checkpoints"+ );} else {reference = Some(probe.clone());}let entries = probe.as_array().expect("repr_probe.json must be an array");- assert!(entries.len() >= 2000, "the differential sweep must not shrink");+ assert!(+ entries.len() >= 2000,+ "the differential sweep must not shrink"+ );for entry in entries {let bits = u64::from_str_radix(entry["bits"].as_str().unwrap(), 16).unwrap();let value = f64::from_bits(bits);@@ -160,7 +166,10 @@ fn repr_matches_python_bit_pattern_sweep() {}}eprintln!("repr differential sweep: {comparisons} comparisons, {mismatches} mismatches");- assert!(comparisons >= 6000, "all three checkpoint probes must be checked");+ assert!(+ comparisons >= 6000,+ "all three checkpoint probes must be checked"+ );assert_eq!(0, mismatches, "float repr must match Python byte-for-byte");}diff --git a/laya-rust/tests/router_end_to_end_tests.rs b/laya-rust/tests/router_end_to_end_tests.rsindex 04265f4d..3fc4ba0c 100644--- a/laya-rust/tests/router_end_to_end_tests.rs+++ b/laya-rust/tests/router_end_to_end_tests.rs@@ -108,18 +108,30 @@ fn sample_inputs_match_recorded_model_case_inputs() {let sample_inputs = routing_golden::load("sample_inputs.json").expect("the routing sample fixture must be present");let support = &sample_inputs["support_email"];- let english_case = CheckpointGoldenData::for_checkpoint("english")- .load("case_sample_app_english.json");- let hindi_case = CheckpointGoldenData::for_checkpoint("multilingual")- .load("case_sample_app_hindi.json");+ let english_case =+ CheckpointGoldenData::for_checkpoint("english").load("case_sample_app_english.json");+ let hindi_case =+ CheckpointGoldenData::for_checkpoint("multilingual").load("case_sample_app_hindi.json");- assert_eq!(support["english_state"], english_case["state"], "English sample state");- assert_eq!(support["questions"], english_case["questions"], "English sample questions");- assert_eq!(support["questions"], hindi_case["questions"], "Hindi sample questions");+ assert_eq!(+ support["english_state"], english_case["state"],+ "English sample state"+ );+ assert_eq!(+ support["questions"], english_case["questions"],+ "English sample questions"+ );+ assert_eq!(+ support["questions"], hindi_case["questions"],+ "Hindi sample questions"+ );// The routing sample includes Hindi sender/subject headers; the quickstart// recording deliberately uses only its body. Compare that exact projection.- assert!(support["hindi_state"]["body"].is_string(), "Hindi sample body must exist");+ assert!(+ support["hindi_state"]["body"].is_string(),+ "Hindi sample body must exist"+ );assert_eq!(serde_json::json!({"body": support["hindi_state"]["body"]}),hindi_case["state"],diff --git a/laya-rust/tests/shape_sweep_tests.rs b/laya-rust/tests/shape_sweep_tests.rsindex 5b9fbf47..52e66671 100644--- a/laya-rust/tests/shape_sweep_tests.rs+++ b/laya-rust/tests/shape_sweep_tests.rs@@ -211,7 +211,7 @@ fn assert_act_probabilities(expected: &[f32], actual: &[f32], classes: usize, wh#[test]fn saturated_act_logit_drift_preserves_probabilities() {assert_act_probabilities(- &[4807.720, -3937.599],+ &[4807.72, -3937.599],&[4807.878, -3937.442],2,"saturated",