diff --git a/assets/locales/pending/geometry-kernel.json b/assets/locales/pending/geometry-kernel.json new file mode 100644 index 000000000..d9f814d7d --- /dev/null +++ b/assets/locales/pending/geometry-kernel.json @@ -0,0 +1,51 @@ +{ + "en": { + "cmd.diagnostics_kernel": "Geometry Kernel", + "prefs.geometry_kernel": "Geometry kernel", + "prefs.geometry_kernel_environment": "The SOLIDCRAFT_KERNEL environment variable is set; it wins over this setting.", + "prefs.geometry_kernel_note": "Routed uses solidcore only for operations that match SMLib on the kernel test rig; a solidcore failure is reported, never retried on SMLib.", + "prefs.geometry_kernel_routed": "Routed" + }, + "ar": { + "cmd.diagnostics_kernel": "نواة الهندسة", + "prefs.geometry_kernel": "نواة الهندسة", + "prefs.geometry_kernel_environment": "متغير البيئة SOLIDCRAFT_KERNEL مضبوط، وله الأولوية على هذا الإعداد.", + "prefs.geometry_kernel_note": "يستخدم الوضع الموجَّه solidcore فقط للعمليات التي تطابق SMLib على منصة اختبار النواة؛ ويُبلَّغ عن إخفاق solidcore ولا تُعاد المحاولة أبدًا على SMLib.", + "prefs.geometry_kernel_routed": "موجَّه" + }, + "de": { + "cmd.diagnostics_kernel": "Geometriekernel", + "prefs.geometry_kernel": "Geometriekernel", + "prefs.geometry_kernel_environment": "Die Umgebungsvariable SOLIDCRAFT_KERNEL ist gesetzt; sie hat Vorrang vor dieser Einstellung.", + "prefs.geometry_kernel_note": "Geroutet nutzt solidcore nur für Vorgänge, die auf dem Kernel-Prüfstand mit SMLib übereinstimmen; ein Fehler in solidcore wird gemeldet, nie mit SMLib wiederholt.", + "prefs.geometry_kernel_routed": "Geroutet" + }, + "fr": { + "cmd.diagnostics_kernel": "Noyau géométrique", + "prefs.geometry_kernel": "Noyau géométrique", + "prefs.geometry_kernel_environment": "La variable d’environnement SOLIDCRAFT_KERNEL est définie ; elle l’emporte sur ce réglage.", + "prefs.geometry_kernel_note": "Routé n’utilise solidcore que pour les opérations qui concordent avec SMLib sur le banc d’essai du noyau ; un échec de solidcore est signalé, jamais réessayé avec SMLib.", + "prefs.geometry_kernel_routed": "Routé" + }, + "he": { + "cmd.diagnostics_kernel": "ליבת גאומטריה", + "prefs.geometry_kernel": "ליבת גאומטריה", + "prefs.geometry_kernel_environment": "משתנה הסביבה SOLIDCRAFT_KERNEL מוגדר, והוא גובר על הגדרה זו.", + "prefs.geometry_kernel_note": "מנותב משתמש ב-solidcore רק לפעולות שתואמות את SMLib במתקן הבדיקה של הליבה; כשל של solidcore מדווח, ולעולם אינו מנוסה שוב ב-SMLib.", + "prefs.geometry_kernel_routed": "מנותב" + }, + "ja": { + "cmd.diagnostics_kernel": "ジオメトリ カーネル", + "prefs.geometry_kernel": "ジオメトリ カーネル", + "prefs.geometry_kernel_environment": "環境変数 SOLIDCRAFT_KERNEL が設定されているため、この設定より優先されます。", + "prefs.geometry_kernel_note": "ルーティングでは、カーネルのテスト環境で SMLib と一致した処理にのみ solidcore を使います。solidcore の失敗は報告され、SMLib で再試行されることはありません。", + "prefs.geometry_kernel_routed": "ルーティング" + }, + "zh": { + "cmd.diagnostics_kernel": "几何内核", + "prefs.geometry_kernel": "几何内核", + "prefs.geometry_kernel_environment": "已设置环境变量 SOLIDCRAFT_KERNEL,它优先于此设置。", + "prefs.geometry_kernel_note": "路由模式仅对在内核测试台上与 SMLib 结果一致的操作使用 solidcore;solidcore 失败时会报告,绝不会改用 SMLib 重试。", + "prefs.geometry_kernel_routed": "路由" + } +} diff --git a/crates/engine/src/cmd/diagnostics.rs b/crates/engine/src/cmd/diagnostics.rs index 6eff986a4..14e0e5446 100644 --- a/crates/engine/src/cmd/diagnostics.rs +++ b/crates/engine/src/cmd/diagnostics.rs @@ -53,8 +53,9 @@ pub static COMMANDS: &[CommandSpec] = &[ fn kernel(_: &mut Session, p: &Value) -> Result { use solidcraft_kernel::kernel_route as route; - if let Some(id) = str_(p, "choice") { - let c = route::Choice::from_id(id).ok_or_else(|| bad("diagnostics.kernel", "`choice` is routed, smlib or solidcore"))?; + // A choice of the wrong type is refused like an unknown one, not read as no choice. + if let Some(v) = p.get("choice").filter(|v| !v.is_null()) { + let c = v.as_str().and_then(route::Choice::from_id).ok_or_else(|| bad("diagnostics.kernel", "`choice` is routed, smlib or solidcore"))?; route::set_choice(c); } let routes: Vec = route::Op::ALL diff --git a/crates/engine/src/schema/diagnostics_tests.rs b/crates/engine/src/schema/diagnostics_tests.rs index 23cbef048..24c720700 100644 --- a/crates/engine/src/schema/diagnostics_tests.rs +++ b/crates/engine/src/schema/diagnostics_tests.rs @@ -60,3 +60,93 @@ fn diagnostics_profile_reports_commands_features_and_the_kernel() { assert_eq!(after["enabled"], json!(false), "{after}"); let _ = std::fs::remove_dir_all(&dir); } + +/// Tests that set the process-wide kernel choice run one at a time, and put `routed` back however +/// they end (a failed assertion included). +struct KernelChoice(#[allow(dead_code)] std::sync::MutexGuard<'static, ()>); + +static KERNEL_TESTS: std::sync::Mutex<()> = std::sync::Mutex::new(()); + +impl KernelChoice { + fn take() -> Self { + KernelChoice(KERNEL_TESTS.lock().unwrap_or_else(std::sync::PoisonError::into_inner)) + } +} + +impl Drop for KernelChoice { + fn drop(&mut self) { + solidcraft_kernel::kernel_route::set_choice(solidcraft_kernel::kernel_route::Choice::Routed); + } +} + +/// diagnostics.kernel lists every operation family with the table's kernel and the one it runs +/// on now, and its `choice` moves them (unless SOLIDCRAFT_KERNEL overrides it). +#[test] +fn diagnostics_kernel_reports_and_sets_the_choice() { + use solidcraft_kernel::kernel_route as route; + let _choice = KernelChoice::take(); + let mut s = Session::default(); + let r = s.execute("diagnostics.kernel", &json!({})).unwrap(); + let routes = r["routes"].as_array().unwrap(); + assert_eq!(routes.len(), route::Op::ALL.len(), "{r}"); + for (row, op) in routes.iter().zip(route::Op::ALL) { + assert_eq!(row["op"], json!(format!("{op:?}").to_lowercase()), "{r}"); + assert_eq!(row["route"], json!(op.route().id()), "{r}"); + assert!(matches!(row["backend"].as_str(), Some("smlib" | "solidcore")), "{r}"); + } + let environment = route::overridden(); + assert_eq!(r["environment"], json!(environment), "{r}"); + for choice in ["solidcore", "smlib", "routed"] { + let r = s.execute("diagnostics.kernel", &json!({ "choice": choice })).unwrap(); + // Back to the table straight away: other tests in this process build bodies meanwhile. + if choice != "routed" { + route::set_choice(route::Choice::Routed); + } + if environment { + continue; // SOLIDCRAFT_KERNEL wins: the choice and the backends stay its own + } + assert_eq!(r["choice"], json!(choice), "{r}"); + for (row, op) in r["routes"].as_array().unwrap().iter().zip(route::Op::ALL) { + // `.smb` files stay on SMLib whatever is chosen. + let want = match choice { + _ if op == route::Op::File => "smlib", + "routed" => op.route().id(), + other => other, + }; + assert_eq!(row["backend"], json!(want), "{choice}: {r}"); + } + } +} + +/// An unknown or mistyped choice is an error that changes nothing; no hostile value panics. +#[test] +fn diagnostics_kernel_refuses_unknown_choices() { + let _choice = KernelChoice::take(); + let mut s = Session::default(); + let before = s.execute("diagnostics.kernel", &json!({})).unwrap()["choice"].clone(); + let huge = "solidcore".repeat(100_000); + for bad in [ + json!("truck"), + json!("SMLIB"), + json!(""), + json!(huge), + json!("routed\0"), + json!(5), + json!(true), + json!(["smlib"]), + json!({"choice": "smlib"}), + ] { + match s.execute("diagnostics.kernel", &json!({ "choice": bad })) { + Err(crate::EngineError::Internal(..)) | Ok(_) => panic!("`{}` was not refused", bad.as_str().map_or(0, str::len)), + Err(_) => {} + } + } + for v in crate::tests::hostile_values() { + for p in [json!({ "choice": v.clone() }), v] { + if let Err(crate::EngineError::Internal(_, msg)) = s.execute("diagnostics.kernel", &p) { + panic!("diagnostics.kernel {p}: {msg}"); + } + } + } + assert_eq!(s.execute("diagnostics.kernel", &json!({})).unwrap()["choice"], before); +} diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs index 44a1ddec2..0ed978d48 100644 --- a/crates/ui-egui/src/prefs.rs +++ b/crates/ui-egui/src/prefs.rs @@ -33,6 +33,8 @@ use crate::ui_kit; use crate::viewport::NavMode; pub const UNITS: [&str; 5] = ["mm", "cm", "m", "in", "ft"]; +/// The Geometry kernel preference's choices, as `diagnostics.kernel` spells them. +pub const KERNELS: [&str; 3] = ["routed", "smlib", "solidcore"]; pub const OPENSCAD_NAV: &str = "openscad"; pub const TOUCHPAD_NAV: &str = "touchpad"; /// A mouse button that can drag the camera. @@ -621,6 +623,10 @@ preference_settings! { /// the tap maker's drills; off by default: the tap maker's drill (M8: 6.8 mm). Saved designs /// keep the drill they stored either way. metric_tap_drill_major_less_pitch: bool = false, PAGE_DESIGN => check_id("prefs.metric_tap_drill_major_less_pitch"); + /// Which geometry kernel answers (one of [`KERNELS`]): `routed` (each operation on the + /// kernel the routing table gives it), `smlib` or `solidcore` for all. Applied through + /// `diagnostics.kernel`; `SOLIDCRAFT_KERNEL`, when set, wins. + geometry_kernel: String = "routed".into(), PAGE_DESIGN; /// Reopen the saved designs that were open when the app was last closed (#5542). reopen_designs: bool = true, PAGE_GENERAL; /// Those designs' files, in tab order, as the last quit left them. @@ -840,6 +846,12 @@ pub struct PrefsWindow { /// The Profile operations preference the profiler last followed: applied when it changes, /// so `diagnostics.profile` (an agent) and SOLIDCRAFT_PROFILE can switch it in between. applied_profile: Option, + /// The Geometry kernel preference `diagnostics.kernel` last applied: applied when it changes + /// (and at startup), so an agent's `diagnostics.kernel` in between stands. + applied_kernel: Option, + /// The choice `SOLIDCRAFT_KERNEL` forces when it overrides the preference (the command's + /// `environment` and `choice`). + pub(crate) kernel_environment: Option, /// The search text of the interface font list. font_filter: String, /// Search text on the Plugins page. @@ -1117,6 +1129,19 @@ pub fn apply(app: &mut SolveApp, ctx: &egui::Context) { } app.prefs_window.applied_profile = Some(profile); } + // The geometry kernel, through its command (an unknown saved value is Routed). + let kernel = KERNELS.iter().copied().find(|k| *k == app.preferences.geometry_kernel).unwrap_or("routed"); + if app.prefs_window.applied_kernel.as_deref() != Some(kernel) { + match app.session.execute("diagnostics.kernel", &json!({ "choice": kernel })) { + Ok(v) => { + let environment = v.get("environment").and_then(serde_json::Value::as_bool).unwrap_or(false); + app.prefs_window.kernel_environment = + environment.then(|| v.get("choice").and_then(serde_json::Value::as_str).unwrap_or_default().to_string()); + } + Err(e) => log::warn!("geometry kernel preference `{kernel}`: {e}"), + } + app.prefs_window.applied_kernel = Some(kernel.into()); + } // The viewport's display quality (an unknown saved value draws at Normal). solidcraft_engine::doc::display_quality::set_current( solidcraft_engine::doc::display_quality::DisplayQuality::parse(&app.preferences.display_quality).unwrap_or_default(), @@ -2323,6 +2348,9 @@ pub fn set(app: &mut SolveApp, p: &serde_json::Value) -> Result