World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
Preserve fractional values when expanding written magnitudes (#825)
Maya Kidc6029069146b
diff --git a/crates/utopia-extract/src/lib.rs b/crates/utopia-extract/src/lib.rsindex 09b0a6d6..41a4c963 100644--- a/crates/utopia-extract/src/lib.rs+++ b/crates/utopia-extract/src/lib.rs@@ -371,16 +371,16 @@ pub(crate) fn currency_unit(tok: &str) -> Option<&'static str> {)}-/// 量级词:英文全写,中文千/万/亿。**不认单字母**(`3M` 是一家公司)。-fn magnitude(tok: &str) -> Option<f64> {+/// 量级词对应的十进制指数:英文全写,中文千/万/亿。**不认单字母**(`3M` 是一家公司)。+fn magnitude(tok: &str) -> Option<u8> {Some(match tok {- "thousand" | "千" => 1e3,- "万" => 1e4,- "million" | "百万" => 1e6,- "千万" => 1e7,- "亿" => 1e8,- "billion" | "十亿" => 1e9,- "trillion" | "万亿" => 1e12,+ "thousand" | "千" => 3,+ "万" => 4,+ "million" | "百万" => 6,+ "千万" => 7,+ "亿" => 8,+ "billion" | "十亿" => 9,+ "trillion" | "万亿" => 12,_ => return None,})}@@ -435,7 +435,10 @@ fn scan_quantity(s: &str, strict: bool) -> Option<(f64, Option<String>)> {let (tok, next) = next_token(tail);if !ate_magnitude {if let Some(m) = magnitude(tok) {- n *= m;+ // Parse the written decimal with its scale in one conversion. Multiplying+ // an already rounded f64 needs an epsilon that can erase real fractions.+ // The suffix is at most three bytes (e12), so allocation stays O(num.len()).+ n = format!("{cleaned}e{m}").parse().ok()?;ate_magnitude = true;tail = next.trim_start();continue;@@ -456,10 +459,6 @@ fn scan_quantity(s: &str, strict: bool) -> Option<(f64, Option<String>)> {if !n.is_finite() {return None;}- // 9.2 × 1e8 在二进制浮点里是 919999999.9999999;乘过量级词的数本来就是整数,收回去- if ate_magnitude && (n - n.round()).abs() < 1e-6 * n.abs().max(1.0) {- n = n.round();- }let unit = if percent {Some("%".to_string())} else {@@ -758,6 +757,97 @@ mod tests {assert_eq!(opening_block(Some(" ")), "");}+ #[test]+ fn written_magnitudes_preserve_fractional_values() {+ for (input, expected) in [+ ("1.00000025 million", 1000000.25),+ ("100.000025万", 1000000.25),+ ("-1.00000025 million", -1000000.25),+ ("+100.000025万", 1000000.25),+ ("9.2亿", 920000000.0),+ ("0.00000000025 thousand", 0.00000025),+ ("0 million", 0.0),+ ("1,000.00025 thousand", 1000000.25),+ ] {+ assert_eq!(parse_quantity(input), Some((expected, None)), "{input}");+ assert_eq!(+ parse_leading_quantity(input),+ Some((expected, None)),+ "{input}"+ );+ assert_eq!(+ normalize_attr_value("number", &serde_json::json!(input)),+ Some(serde_json::json!(expected)),+ "{input}"+ );+ }+ assert_eq!(+ parse_quantity("$1.00000025 million"),+ Some((1000000.25, Some("$".into())))+ );+ assert_eq!(+ parse_leading_quantity("1.00000025 million people worldwide"),+ Some((1000000.25, Some("people".into())))+ );+ for rejected in [+ "3M",+ "5k",+ "1e3 million",+ "1.2.3 million",+ "--1 million",+ "1 million people",+ ] {+ assert_eq!(parse_quantity(rejected), None, "{rejected}");+ }+ assert_eq!(+ parse_quantity(&format!("{} trillion", "9".repeat(400))),+ None+ );+ assert_eq!(+ parse_quantity(&format!("{}1.00000025 million", "0".repeat(20_000))),+ Some((1000000.25, None))+ );+ }++ #[test]+ fn written_magnitudes_match_integer_decimal_oracles() {+ // The oracle shifts exact u128 integers, then parses an ordinary decimal.+ // No float multiplication or epsilon can erase a meaningful remainder.+ for (word, exponent) in [+ ("thousand", 3),+ ("万", 4),+ ("million", 6),+ ("千万", 7),+ ("亿", 8),+ ("billion", 9),+ ("trillion", 12),+ ] {+ for coefficient in [0u128, 1, 25, 100000025, 9200000000, 9007199254740991] {+ for places in 0..=15u32 {+ let divisor = 10u128.pow(places);+ let expanded = coefficient * 10u128.pow(exponent);+ let decimal = |n: u128| {+ if places == 0 {+ n.to_string()+ } else {+ format!(+ "{}.{:0width$}",+ n / divisor,+ n % divisor,+ width = places as usize+ )+ }+ };+ for sign in ["", "-"] {+ let input = format!("{sign}{} {word}", decimal(coefficient));+ let expected: f64 = format!("{sign}{}", decimal(expanded)).parse().unwrap();+ assert_eq!(parse_quantity(&input), Some((expected, None)), "{input}");+ }+ }+ }+ }+ }+#[test]fn a_quantity_is_the_whole_string_or_nothing() {// 整体就是一个量:符号、量级词、千分位都读得动diff --git a/crates/utopia-server/src/api/mcp_tests.rs b/crates/utopia-server/src/api/mcp_tests.rsindex 4b0c1a1f..5e2f92ad 100644--- a/crates/utopia-server/src/api/mcp_tests.rs+++ b/crates/utopia-server/src/api/mcp_tests.rs@@ -1784,6 +1784,100 @@ async fn computed_rule_descriptions_keep_the_expression_tree_and_identity() -> af.clean().await}+#[tokio::test]+async fn written_magnitudes_keep_fractions_through_authenticated_adoption() -> anyhow::Result<()> {+ use axum::body::{to_bytes, Body};+ use axum::http::{Request, StatusCode};+ use tower::ServiceExt;++ let Some(f) = Fixture::new().await? else {+ return Ok(());+ };+ async fn check(f: &Fixture) -> anyhow::Result<()> {+ let auth = utopia_store::tokens::authenticate(&f.state.pool, &f.token).await?;+ sqlx::query("UPDATE kb_members SET role='editor' WHERE kb_id=$1 AND user_id=$2")+ .bind(f.kb)+ .bind(auth.user_id)+ .execute(&f.state.pool)+ .await?;+ let jwt = crate::auth::issue_token(&f.state, auth.user_id)?;+ let app = crate::api::router(f.state.clone(), &Default::default());+ for (index, input) in ["1.00000025 million", "100.000025万"]+ .into_iter()+ .enumerate()+ {+ let form = format!("fractional_amount_{index}");+ let raw = json!({"value":input,"unit":"$"});+ // This endpoint adopts unbound typed value facts. Open statements are+ // intentionally not used: their alignment is a different write path.+ let (old, _) = utopia_store::graph::insert_value_fact(+ &f.state.pool,+ f.kb,+ f.subject,+ None,+ &raw,+ utopia_store::graph::Validity::default(),+ 0.9,+ )+ .await?;+ sqlx::query("INSERT INTO fact_evidence(fact_id,chunk_id,document_id,doc_version,quote,proposed_predicate)+ VALUES ($1,$2,$3,1,$4,$5)")+ .bind(old).bind(f.chunk).bind(f.document).bind(input).bind(&form).execute(&f.state.pool).await?;+ let waiting = utopia_store::graph::value_facts_for_forms(+ &f.state.pool,+ f.kb,+ std::slice::from_ref(&form),+ )+ .await?;+ anyhow::ensure!(+ waiting.len() == 1 && waiting[0].0 == old,+ "fixture is not supported by adoption"+ );+ let response = app.clone().oneshot(Request::builder().method("POST")+ .uri(format!("/api/v1/kbs/{}/ontology/adopt-predicate",f.kb))+ .header("authorization",format!("Bearer {jwt}"))+ .header("content-type","application/json")+ .body(Body::from(json!({"key":form,"label":form,"forms":[form],"kind":"attribute","datatype":"number"}).to_string()))?).await?;+ let status = response.status();+ let bytes = to_bytes(response.into_body(), 1024 * 1024).await?;+ anyhow::ensure!(+ status == StatusCode::OK,+ "adoption rejected: {}",+ String::from_utf8_lossy(&bytes)+ );+ let result: Value = serde_json::from_slice(&bytes)?;+ anyhow::ensure!(result["remapped"] == 1, "no fact adopted: {result}");+ let attribute = uuid(&result["id"]);+ let (new, stored, supersedes): (Uuid,Value,Option<Uuid>) = sqlx::query_as(+ "SELECT id,object_value,supersedes FROM facts WHERE kb_id=$1 AND predicate_id=$2 AND invalidated_at IS NULL")+ .bind(f.kb).bind(attribute).fetch_one(&f.state.pool).await?;+ anyhow::ensure!(+ stored["value"].as_f64() == Some(1000000.25),+ "adoption rounded away .25: {stored}"+ );+ anyhow::ensure!(+ stored["unit"] == "$" && supersedes == Some(old),+ "unit or history lost"+ );+ let original: Value = sqlx::query_scalar("SELECT object_value FROM facts WHERE id=$1")+ .bind(old)+ .fetch_one(&f.state.pool)+ .await?;+ anyhow::ensure!(original == raw, "historical value rewritten");+ let evidence: bool = sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM fact_evidence WHERE fact_id=$1 AND quote=$2 AND document_id=$3)")+ .bind(new).bind(input).bind(f.document).fetch_one(&f.state.pool).await?;+ anyhow::ensure!(evidence, "adopted fact lost source evidence");+ let audit: i64 = sqlx::query_scalar("SELECT count(*) FROM audit_events WHERE kb_id=$1 AND action='ontology.attribute_adopted' AND target_id=$2")+ .bind(f.kb).bind(attribute).fetch_one(&f.state.pool).await?;+ anyhow::ensure!(audit == 1, "missing adoption audit");+ }+ Ok(())+ }+ let result = check(&f).await;+ let cleanup = f.clean().await;+ result.and(cleanup)+}+#[tokio::test]async fn rule_reads_preserve_matches_and_empty_results() -> anyhow::Result<()> {let Some(f) = Fixture::new().await? else {