Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Agent commands compute off the window thread, so the window stays live (opt-in) (#1989) (#3340)

SS-EN776541b0811d
diff --git a/assets/locales/en.json b/assets/locales/en.json
index 73731f653b..842513fc88 100644
--- a/assets/locales/en.json
+++ b/assets/locales/en.json
@@ -1209,6 +1209,8 @@
"ui.agent_chat_view_attached": "View attached",
"ui.agent_cursor": "Agent cursor",
"ui.agent_cursor_speed": "Agent cursor speed",
+ "ui.agent_off_thread": "Keep the window responsive while agents run commands",
+ "ui.agent_off_thread_note": "An agent's slow commands (from the AI panel or an MCP client) compute beside the window: you can keep turning and inspecting the view, and stop one from the chip in the view. Off: each runs on the window's thread and the window waits for it.",
"ui.ai_add": "Add",
"ui.ai_add_agent": "Add agent…",
"ui.ai_add_agent_title": "Add agent",
diff --git a/assets/locales/ja.json b/assets/locales/ja.json
index 792efa23d4..f6e8176468 100644
--- a/assets/locales/ja.json
+++ b/assets/locales/ja.json
@@ -1209,6 +1209,8 @@
"ui.agent_chat_view_attached": "ビューを添付済み",
"ui.agent_cursor": "エージェントカーソル",
"ui.agent_cursor_speed": "エージェントカーソル速度",
+ "ui.agent_off_thread": "エージェントのコマンド実行中もウィンドウを操作可能にする",
+ "ui.agent_off_thread_note": "エージェント(AI パネルまたは MCP クライアント)の時間のかかるコマンドをウィンドウとは別に計算します。実行中もビューの回転や確認ができ、ビュー内の表示から停止できます。オフ:各コマンドはウィンドウのスレッドで実行され、終わるまでウィンドウは待ちます。",
"ui.ai_add": "追加",
"ui.ai_add_agent": "エージェントを追加…",
"ui.ai_add_agent_title": "エージェントを追加",
diff --git a/assets/locales/zh.json b/assets/locales/zh.json
index 5276c629ca..8bc721debd 100644
--- a/assets/locales/zh.json
+++ b/assets/locales/zh.json
@@ -1209,6 +1209,8 @@
"ui.agent_chat_view_attached": "已附加视图",
"ui.agent_cursor": "代理光标",
"ui.agent_cursor_speed": "代理光标速度",
+ "ui.agent_off_thread": "代理运行命令时保持窗口响应",
+ "ui.agent_off_thread_note": "代理(来自 AI 面板或 MCP 客户端)的耗时命令在窗口之外计算:运行时仍可旋转和查看视图,并可在视图中的提示上停止它。关闭:每条命令在窗口线程上运行,窗口会等待其完成。",
"ui.ai_add": "添加",
"ui.ai_add_agent": "添加智能体…",
"ui.ai_add_agent_title": "添加智能体",
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index e4bcd2f851..29ec73ce28 100644
--- a/crates/doc/src/eval.rs
+++ b/crates/doc/src/eval.rs
@@ -777,6 +777,48 @@ impl EvalCache {
}
}
+/// The results an evaluation ahead on a copy of a model computed or took from the cache, kept for
+/// the run that follows on the model itself (#1989). The evaluation cache is bounded by bytes and
+/// charges every entry its whole model, so on a design with large bodies it holds only the last
+/// few states: by the time the run came, the cache had dropped most of what was computed ahead,
+/// and the run computed it all again, on the window thread.
+#[derive(Debug, Default)]
+pub struct KeptResults(std::sync::Mutex<std::collections::HashMap<u64, CachedEval>>);
+
+impl KeptResults {
+ /// Results kept.
+ pub fn len(&self) -> usize {
+ self.0.lock().map_or(0, |m| m.len())
+ }
+
+ pub fn is_empty(&self) -> bool {
+ self.len() == 0
+ }
+
+ fn get(&self, key: u64) -> Option<CachedEval> {
+ self.0.lock().ok().and_then(|m| m.get(&key).cloned())
+ }
+
+ fn put(&self, key: u64, e: &CachedEval) {
+ if let Ok(mut m) = self.0.lock()
+ && (m.len() < CACHE_ENTRIES || m.contains_key(&key))
+ {
+ m.insert(key, e.clone());
+ }
+ }
+}
+
+/// Where a model keeps its results besides the cache ([`Model::keep`]). A copy of the model
+/// starts without: it keeps nothing it computes for someone else's run.
+#[derive(Debug, Default)]
+struct KeptSlot(Option<Arc<KeptResults>>);
+
+impl Clone for KeptSlot {
+ fn clone(&self) -> Self {
+ KeptSlot(None)
+ }
+}
+
impl SolvedSketch {
/// Rough heap footprint (see [`ModelState::retained_bytes`]).
pub fn retained_bytes(&self) -> usize {
@@ -810,6 +852,14 @@ impl ModelState {
}
});
}
+ /// [`ModelState::warm_meshes`], and every body's display plan at the current display quality
+ /// (what a GPU viewport draws).
+ pub fn warm_display(&self) {
+ kernel::for_each_on_threads(&self.bodies, 16, |b| {
+ let _ = b.mesh();
+ let _ = b.display_plan();
+ });
+ }
pub fn sketch(&self, feature: u64) -> Option<&SolvedSketch> {
self.sketches.iter().find(|s| s.feature == feature).map(|s| &**s)
}
@@ -844,6 +894,9 @@ pub struct Model {
empty: Arc<ModelState>,
/// Features recomputed by the last `evaluate`.
pub last_recomputed: usize,
+ /// Features recomputed by every `evaluate` of this model (and of the model it was copied
+ /// from) so far.
+ pub recomputed: usize,
/// Features whose result the last `evaluate` took from the cache instead of recomputing: the
/// same feature on the same inputs was evaluated before (an edit set back, undo, redo, a
/// configuration row). Their geometry did change since the previous evaluation.
@@ -852,6 +905,8 @@ pub struct Model {
pub cache: Arc<std::sync::Mutex<EvalCache>>,
/// Evaluate everything from scratch (no reuse; tests compare the two).
pub no_cache: bool,
+ /// Results kept besides the cache ([`Model::keep`]).
+ kept: KeptSlot,
/// The imported texts' digests the last `evaluate` fingerprinted the features with.
step_digests: StepDigests,
}
@@ -1295,6 +1350,20 @@ impl Model {
})
}
+ /// While `kept` is set, every result this model evaluates or takes from the cache is also
+ /// kept there, and a result the cache no longer has is taken from there (#1989): an
+ /// evaluation ahead on a copy keeps what it computes, and the run that follows on the model
+ /// itself finds it all, whatever the cache dropped meanwhile. Copies of the model keep
+ /// nothing.
+ pub fn keep(&mut self, kept: Option<Arc<KeptResults>>) {
+ self.kept = KeptSlot(kept);
+ }
+
+ /// Where this model keeps its results besides the cache, if it does ([`Model::keep`]).
+ pub fn kept(&self) -> Option<Arc<KeptResults>> {
+ self.kept.0.clone()
+ }
+
/// Whether the results are `doc`'s evaluation: one per feature, each on the inputs `doc`
/// gives it, none stale, so [`Model::evaluate`] would keep every one of them. A document
/// changed without an evaluation since is not.
@@ -1368,9 +1437,13 @@ impl Model {
}
// The same feature on the same inputs: its earlier result.
let key = cache_key(f, fp, fingerprint(0, f, &vals, false, &self.step_digests) ^ placement_digest(doc, f), ctx, &state);
+ let kept = self.kept.0.as_ref().filter(|_| !self.no_cache);
if !self.no_cache
- && let Some(c) = self.cache.lock().ok().and_then(|mut m| m.get(key))
+ && let Some(c) = self.cache.lock().ok().and_then(|mut m| m.get(key)).or_else(|| kept.and_then(|k| k.get(key)))
{
+ if let Some(k) = kept {
+ k.put(key, &c);
+ }
restored += 1;
if c.error.is_none() {
state = c.state.clone();
@@ -1499,20 +1572,21 @@ impl Model {
}
Err(e) => Some(e.to_string().trim_start_matches("the operation failed: ").to_string()),
};
- if !self.no_cache
- && let Ok(mut m) = self.cache.lock()
- {
- m.put(
- key,
- CachedEval {
- error: error.clone(),
- warning: warning.clone(),
- boolean_diagnostics: boolean_diagnostics.clone(),
- ms,
- bytes: state.retained_bytes(),
- state: state.clone(),
- },
- );
+ if !self.no_cache {
+ let e = CachedEval {
+ error: error.clone(),
+ warning: warning.clone(),
+ boolean_diagnostics: boolean_diagnostics.clone(),
+ ms,
+ bytes: state.retained_bytes(),
+ state: state.clone(),
+ };
+ if let Some(k) = &self.kept.0 {
+ k.put(key, &e);
+ }
+ if let Ok(mut m) = self.cache.lock() {
+ m.put(key, e);
+ }
}
out.push(FeatureResult {
id: f.id,
@@ -1529,6 +1603,7 @@ impl Model {
}
self.results = out;
self.last_recomputed = recomputed;
+ self.recomputed += recomputed;
self.last_restored = restored;
}
}
diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs
index 1118764638..3cdab51d15 100644
--- a/crates/doc/src/lib.rs
+++ b/crates/doc/src/lib.rs
@@ -57,6 +57,7 @@ pub use assembly::{
rigid,
};
pub use document::*;
+pub use eval::KeptResults;
pub use eval::{
EdgeRef, FeatureResult, Model, ModelBody, ModelState, PlacedBodies, RevolveExtent, SavedModel, SolvedSketch, ThreadInfo, curve_pick_error,
edge_names_for, edge_refs, face_point_names, face_point_state, hole_direction_at, import_body_names, metric_thread_sizes, object_height,
diff --git a/crates/doc/src/tests.rs b/crates/doc/src/tests.rs
index 9dbaad6e9a..d2f5f47a62 100644
--- a/crates/doc/src/tests.rs
+++ b/crates/doc/src/tests.rs
@@ -103,6 +103,28 @@ fn set_back_reports_restored_features() {
assert_eq!((m.last_recomputed, m.last_restored), (0, 0));
}
+/// #1989: what an evaluation ahead on a copy of the model computed is kept for the run that
+/// follows on the model itself, which finds it there even once the evaluation cache has dropped
+/// it (the cache keeps only a few states of a design with large bodies). Copies keep nothing.
+#[test]
+fn results_kept_ahead_outlive_the_cache() {
+ let (mut doc, _) = plate_doc();
+ let mut m = Model::new();
+ m.evaluate(&doc);
+ doc.set_param("width", "50 mm", None, None).unwrap();
+ let kept = std::sync::Arc::new(KeptResults::default());
+ let mut ahead = m.clone();
+ ahead.keep(Some(kept.clone()));
+ assert!(ahead.clone().kept().is_none(), "a copy keeps nothing");
+ ahead.evaluate(&doc);
+ assert_eq!((ahead.last_recomputed, kept.len()), (2, 2));
+ *m.cache.lock().unwrap() = Default::default();
+ m.keep(Some(kept));
+ m.evaluate(&doc);
+ assert_eq!((m.last_recomputed, m.last_restored), (0, 2), "nothing computed again");
+ assert!(rel(measure(&m.state().bodies[0].body).unwrap().volume, 30000.0) < 1e-9);
+}
+
#[test]
fn box_fillet_cut_timeline() {
let (mut doc, _) = plate_doc();
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index 6c587973be..6cde8bd05a 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -170,9 +170,19 @@ pub struct Snapshot {
pub struct Ahead(Session);
impl Ahead {
- /// Run `commands` (their results stay in the shared evaluation cache).
+ /// What the evaluation computes, kept for the run that follows here whatever the evaluation
+ /// cache drops meanwhile: hand it to [`solidcraft_doc::Model::keep`] for that run.
+ pub fn kept(&self) -> Arc<solidcraft_doc::KeptResults> {
+ self.0.model.kept().unwrap_or_default()
+ }
+
+ /// Run `commands` (their results stay in the shared evaluation cache, and in [`Ahead::kept`]),
+ /// then make the display meshes and plans of the bodies they end with: the bodies the run
+ /// here finds share them, so the window draws, fits and picks without tessellating anything.
pub fn evaluate(self, commands: &[(String, Value)]) -> Result<()> {
- self.0.preview_in_place(commands).map(|_| ())
+ let (s, _) = self.0.preview_in_place(commands)?;
+ s.world_state().warm_display();
+ Ok(())
}
/// Run `commands` one after another, each reply or error in turn (what they evaluate stays
@@ -1030,6 +1040,7 @@ impl Session {
let mut copy = self.scratch();
copy.undo.clone_from(&self.undo);
copy.redo.clone_from(&self.redo);
+ copy.model.keep(Some(Arc::default()));
Ahead(copy)
}
diff --git a/crates/ui-egui/src/agent_chat/settings.rs b/crates/ui-egui/src/agent_chat/settings.rs
index c886c52d78..7d035be7c8 100644
--- a/crates/ui-egui/src/agent_chat/settings.rs
+++ b/crates/ui-egui/src/agent_chat/settings.rs
@@ -110,6 +110,10 @@ pub fn page(app: &mut SolveApp, ui: &mut egui::Ui) {
ui_kit::text(ui, tr("ui.agent_chat_desktop_only"), ui_kit::Text::Note("desktop"));
return;
}
+ // Any agent's control requests, the chat's or an outside MCP client's (#1989).
+ let r = ui_kit::checkbox(ui, tr("ui.agent_off_thread"), &mut app.preferences.agent_off_thread);
+ crate::scenario::publish_handle("prefs:agent_off_thread", r.rect.center());
+ ui_kit::text(ui, tr("ui.agent_off_thread_note"), ui_kit::Text::Note("off_thread"));
if !app.preferences.ai_features {
return;
}
diff --git a/crates/ui-egui/src/agent_cursor.rs b/crates/ui-egui/src/agent_cursor.rs
index 3e929fa2ca..949e847d83 100644
--- a/crates/ui-egui/src/agent_cursor.rs
+++ b/crates/ui-egui/src/agent_cursor.rs
@@ -140,6 +140,9 @@ pub struct Job {
started: f64,
/// Clicked at the current stop (waiting out the click).
clicked: bool,
+ /// The command computing ahead off the window thread while the pointer glides (#1989): at
+ /// its last stop the pointer waits for it, then the command runs and finds it in the cache.
+ ahead: Option<crate::control_job::Ahead>,
}
/// The pointer's state.
@@ -158,6 +161,19 @@ impl AgentCursor {
pub fn busy(&self) -> bool {
self.job.is_some()
}
+
+ /// Since when the pointer has waited at its last stop for its command to compute (ms), and
+ /// the command.
+ pub fn computing(&self) -> Option<(f64, &str)> {
+ self.job.as_ref().filter(|j| j.stops.is_empty()).and_then(|j| j.ahead.as_ref()).map(|a| (a.started, a.label.as_str()))
+ }
+
+ /// Stop the request the pointer waits on: answered as stopped, nothing of it applied.
+ pub(crate) fn stop(&mut self) -> Option<(Sender<ControlResponse>, String)> {
+ self.computing()?;
+ let job = self.job.take()?;
+ Some((job.reply, job.ahead.map(|a| a.label).unwrap_or_default()))
+ }
}
/// Most picks the pointer visits for one command, besides its toolbar button. More are
@@ -324,6 +340,8 @@ pub fn intercept(app: &mut SolveApp, method: &str, params: &Value, reply: &Sende
return false;
}
let from = app.cursor_start();
+ // Computed ahead (a command or a batch's steps) while the pointer glides (#1989).
+ let ahead = crate::control_job::Ahead::begin(app, method, params);
let (glide, click) = leg_times(s.speed, stops.len());
app.agent.job = Some(Job {
method: method.to_string(),
@@ -335,6 +353,7 @@ pub fn intercept(app: &mut SolveApp, method: &str, params: &Value, reply: &Sende
from,
started: app.now,
clicked: false,
+ ahead,
});
true
}
@@ -362,7 +381,9 @@ pub fn step(app: &mut SolveApp, ctx: &egui::Context) {
let (glide, click) = (job.glide, job.click);
ctx.request_repaint();
let Some(&to) = job.stops.front() else {
- finish(app, ctx);
+ if job.ahead.as_ref().is_none_or(crate::control_job::Ahead::finished) {
+ finish(app, ctx);
+ }
return;
};
let t = if glide > 0.0 { (now - job.started) / glide } else { 1.0 };
@@ -381,7 +402,7 @@ pub fn step(app: &mut SolveApp, ctx: &egui::Context) {
job.from = to;
job.started = now;
job.clicked = false;
- if job.stops.is_empty() {
+ if job.stops.is_empty() && job.ahead.as_ref().is_none_or(crate::control_job::Ahead::finished) {
finish(app, ctx);
}
}
@@ -399,10 +420,11 @@ fn finish(app: &mut SolveApp, ctx: &egui::Context) {
app.agent.saved_view = Some(app.cam);
}
let (req, _rx) = crate::control::ControlRequest::new(job.method, params);
- let out = match crate::control::handle(app, ctx, &req) {
+ // What was computed ahead while the pointer glided is found rather than computed again here.
+ let out = app.run_with_kept(job.ahead.map(|a| a.kept), |app| match crate::control::handle(app, ctx, &req) {
crate::control::Outcome::Done(v) => v,
crate::control::Outcome::Screenshot { .. } => json!({"ok": false, "error": "not a command"}),
- };
+ });
let ran = out.get("ok").and_then(Value::as_bool).unwrap_or(false);
if follow && ran {
if id == "sketch.create" {
diff --git a/crates/ui-egui/src/control_job.rs b/crates/ui-egui/src/control_job.rs
new file mode 100644
index 0000000000..0c2f1361bf
--- /dev/null
+++ b/crates/ui-egui/src/control_job.rs
@@ -0,0 +1,394 @@
+//! Control-channel (MCP) requests that compute off the window thread (#1989): an agent's slow
+//! command (a boolean, a fillet, a big batch, an export) used to run on the window thread, and
+//! the window froze until it was done: no redraw, no orbit, no way to stop it.
+//!
+//! The window thread still owns the design. What leaves it is the computing:
+//! - **Design changes** (`engine.execute` of a modelling command, `engine.batch`) are evaluated
+//! ahead on a copy of the session ([`solidcraft_engine::Session::ahead`]), as the history
+//! marker and undo are ([`crate::deferred`]). What that computes is kept
+//! ([`solidcraft_engine::doc::KeptResults`]); once it is in, the request runs here for real, as
+//! it always did (undo step, recovery autosave, crash journal, agent cursor, reply), and finds
+//! every feature it computed there, whatever the bounded evaluation cache dropped meanwhile.
+//! One design change computes at a time; later ones wait their turn, in order.
+//! - **Exports** (`file.export`, `file.save_mesh`) change nothing in the design: they run on a
+//! copy of it as it was when they were asked for, and their reply comes from there. Nothing
+//! waits behind them.
+//!
+//! Meanwhile the window goes on drawing the design as it was, `ui.*` requests and inspection are
+//! answered, and a chip says which agent command is computing, with a Stop button. A person's
+//! own edits meanwhile go first: the agent's command is then computed again for the design as it
+//! is and lands after them, never in between. If the design itself was replaced (another tab, a
+//! file opened) the agent's command is refused, saying so, and nothing is applied.
+//!
+//! Done within the app's wait ([`crate::dialogs::APPLY_AT_ONCE`]), a request just runs, as
+//! before. Headless use, tests and the web build ([`SolveApp::apply_off_thread`] unset) run
+//! everything at once, and so does the app unless the preference is on (Preferences > AI,
+//! `agent_off_thread`, off by default).
+
+use std::sync::Arc;
+use std::sync::mpsc::{Receiver, TryRecvError};
+
+use serde_json::{Value, json};
+
+use solidcraft_engine::doc::KeptResults;
+
+use crate::SolveApp;
+use crate::control::ControlRequest;
+
+/// Areas whose commands only compute the design (no files written, nothing started outside the
+/// app), so evaluating them ahead on a copy has no effect but a warm cache.
+const AHEAD_AREAS: [&str; 22] = [
+ "appearance",
+ "assembly",
+ "body",
+ "component",
+ "config",
+ "construct",
+ "contact",
+ "explode",
+ "expr",
+ "face",
+ "joint",
+ "material",
+ "mesh",
+ "model",
+ "occurrence",
+ "parameters",
+ "plastic",
+ "sheet",
+ "sketch",
+ "solid",
+ "surface",
+ "timeline",
+];
+
+/// Parameters that name a file or folder: a command given one runs here, as before.
+const NAMES_A_FILE: [&str; 8] = ["path", "paths", "file", "files", "dir", "folder", "url", "out"];
+
+/// Commands that change nothing in the design and write a file: they run on a copy.
+const ANSWERED_ON_A_COPY: [&str; 2] = ["file.export", "file.save_mesh"];
+
+/// Exports computing at once; more run here.
+const MAX_ANSWERING: usize = 4;
+
+/// How often a design change is computed again because the design changed under it before it
+/// runs here anyway.
+const MAX_TRIES: u8 = 3;
+
+/// The command's own id (as registered), if it has one.
+fn spec_id(command: &str) -> Option<&'static str> {
+ solidcraft_engine::find_command(command).map(|c| c.id)
+}
+
+fn names_a_file(params: &Value) -> bool {
+ params.as_object().is_some_and(|o| o.keys().any(|k| NAMES_A_FILE.contains(&k.as_str())))
+}
+
+/// Can `command` be evaluated ahead on a copy of the session with no effect outside it?
+pub(crate) fn computes_ahead(command: &str, params: &Value) -> bool {
+ let Some(id) = spec_id(command) else { return false };
+ if matches!(id, "edit.undo" | "edit.redo") {
+ return true;
+ }
+ let area = id.split('.').next().unwrap_or_default();
+ AHEAD_AREAS.contains(&area) && !["export", "import", "save", "library"].iter().any(|w| id.contains(w)) && !names_a_file(params)
+}
+
+/// The command an `engine.execute` request (its `params`) runs and its parameters.
+fn executed(params: &Value) -> Option<(&str, Value)> {
+ let command = params.get("command").or_else(|| params.get("id")).and_then(Value::as_str)?;
+ Some((command, params.get("params").cloned().filter(|v| !v.is_null()).unwrap_or(json!({}))))
+}
+
+/// Requests that change the design: they wait while one computes.
+pub(crate) fn changes_the_design(method: &str) -> bool {
+ matches!(method, "engine.execute" | "command" | "engine.script" | "engine.batch")
+}
+
+/// What a request computes off the window thread.
+enum Work {
+ /// Evaluated ahead: once in, the request runs here. `doc` and `revision` are the design it
+ /// was computed for.
+ Ahead { commands: Vec<(String, Value)>, doc: Arc<solidcraft_engine::doc::Document>, revision: u64, tries: u8, kept: Arc<KeptResults> },
+ /// Run on a copy; the reply comes from there.
+ Answer,
+}
+
+/// A control request computing off the window thread.
+pub struct ControlJob {
+ req: ControlRequest,
+ work: Work,
+ /// The worker's result: `Some` reply for [`Work::Answer`].
+ done: Receiver<Option<Value>>,
+ /// When it was asked for (ms).
+ pub(crate) started: f64,
+ /// The command, as the toolbar names it.
+ pub(crate) label: String,
+}
+
+impl ControlJob {
+ fn poll(&self) -> Option<Option<Value>> {
+ match self.done.try_recv() {
+ Ok(r) => Some(r),
+ Err(TryRecvError::Disconnected) => Some(None),
+ Err(TryRecvError::Empty) => None,
+ }
+ }
+}
+
+fn label(app: &SolveApp, command: &str) -> String {
+ let language = crate::i18n::Locale::for_language(&app.preferences.language);
+ language.label(solidcraft_engine::find_command(command).map_or(command, |c| c.label)).to_string()
+}
+
+/// Run `f` on a worker thread (with the request's folder scope), answering on the returned
+/// channel. `None` without threads (the web).
+fn spawn(
+ scope: Option<solidcraft_engine::io::vfs::PathScope>,
+ f: impl FnOnce() -> Option<Value> + Send + 'static,
+) -> Option<Receiver<Option<Value>>> {
+ let (tx, rx) = std::sync::mpsc::channel();
+ std::thread::Builder::new()
+ .name("agent".into())
+ .spawn(move || {
+ let r = solidcraft_engine::io::vfs::scoped(scope, f);
+ let _ = tx.send(r);
+ })
+ .ok()?;
+ Some(rx)
+}
+
+/// Evaluate `commands` ahead on a copy of the session, on a worker (with the folder scope
+/// `request` names). What it computes is kept for the run here.
+fn evaluate_ahead(app: &SolveApp, request: &Value, commands: Vec<(String, Value)>) -> Option<(Receiver<Option<Value>>, Arc<KeptResults>)> {
+ let scope = crate::control::request_scope(app, request).ok()?;
+ let ahead = app.session.ahead();
+ let kept = ahead.kept();
+ let rx = spawn(scope, move || {
+ let _ = ahead.evaluate(&commands);
+ None
+ })?;
+ Some((rx, kept))
+}
+
+/// Start computing a design change ahead, for the design as it is now.
+fn begin_ahead(app: &SolveApp, req: &ControlRequest, commands: Vec<(String, Value)>, tries: u8) -> Option<(Work, Receiver<Option<Value>>)> {
+ let (rx, kept) = evaluate_ahead(app, &req.params, commands.clone())?;
+ Some((Work::Ahead { commands, doc: app.session.doc.clone(), revision: app.session.revision, tries, kept }, rx))
+}
+
+impl SolveApp {
+ /// Run a control request here with what was computed ahead for it at hand ([`KeptResults`]):
+ /// its features are found there rather than computed again on the window thread.
+ pub(crate) fn run_with_kept<R>(&mut self, kept: Option<Arc<KeptResults>>, run: impl FnOnce(&mut SolveApp) -> R) -> R {
+ let keeps = kept.is_some();
+ if keeps {
+ self.session.model.keep(kept);
+ }
+ let r = run(self);
+ if keeps {
+ self.session.model.keep(None);
+ }
+ r
+ }
+}
+
+/// A command the agent cursor acts out, evaluating ahead while the pointer glides to it.
+pub struct Ahead {
+ done: Receiver<Option<Value>>,
+ /// What it computes, for the run once the pointer is there.
+ pub(crate) kept: Arc<KeptResults>,
+ /// When it started (ms).
+ pub(crate) started: f64,
+ /// The command, as the toolbar names it.
+ pub(crate) label: String,
+}
+
+impl Ahead {
+ /// Start evaluating the design changes of the `request` (`method`: an `engine.execute` or an
+ /// `engine.batch`) the pointer acts out; `None` when it just runs once the pointer is there
+ /// (not computed off the window thread, or nothing to compute).
+ pub(crate) fn begin(app: &SolveApp, method: &str, request: &Value) -> Option<Ahead> {
+ app.apply_off_thread?;
+ if !app.preferences.agent_off_thread || app.file_load.busy() || app.file_picker.is_some() {
+ return None;
+ }
+ let commands = ahead_commands(method, request);
+ let label = label(app, &commands.first()?.0);
+ let (done, kept) = evaluate_ahead(app, request, commands)?;
+ Some(Ahead { done, kept, started: crate::now_ms(), label })
+ }
+
+ /// The evaluation is in (or its worker ended).
+ pub(crate) fn finished(&self) -> bool {
+ !matches!(self.done.try_recv(), Err(TryRecvError::Empty))
+ }
+}
+
+/// The design changes a request (`method`, `params`) would make that can be computed ahead: an
+/// `engine.execute` of such a command, or the steps of a batch up to the first that can't.
+/// Empty: it just runs.
+fn ahead_commands(method: &str, p: &Value) -> Vec<(String, Value)> {
+ match method {
+ "engine.execute" | "command" => match executed(p) {
+ Some((command, params)) if computes_ahead(command, &params) => vec![(command.to_string(), params)],
+ _ => Vec::new(),
+ },
+ "engine.batch" => {
+ // Timings measure the run here: it must do the work.
+ if ["timings", "profile"].iter().any(|k| p.get(*k).and_then(Value::as_bool) == Some(true)) {
+ return Vec::new();
+ }
+ let Some(steps) = p.get("commands").and_then(Value::as_array) else { return Vec::new() };
+ steps
+ .iter()
+ .map_while(|step| {
+ let command = step.get("command").or_else(|| step.get("id")).and_then(Value::as_str)?;
+ let params = step.get("params").cloned().filter(|v| !v.is_null()).unwrap_or(json!({}));
+ computes_ahead(command, &params).then(|| (command.to_string(), params))
+ })
+ .collect()
+ }
+ _ => Vec::new(),
+ }
+}
+
+/// Wait the app's wait for a worker: `Some(result)` when it finished within it.
+fn within_wait(app: &SolveApp, rx: &Receiver<Option<Value>>) -> Option<Option<Value>> {
+ let wait = app.apply_off_thread?;
+ if wait.is_zero() {
+ return None;
+ }
+ match rx.recv_timeout(wait) {
+ Ok(r) => Some(r),
+ Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => Some(None),
+ Err(std::sync::mpsc::RecvTimeoutError::Timeout) => None,
+ }
+}
+
+impl SolveApp {
+ /// Since when an agent's design change has been computing off the window thread (ms), and
+ /// its command.
+ pub fn agent_computing(&self) -> Option<(f64, &str)> {
+ self.control_job.as_ref().map(|j| (j.started, j.label.as_str())).or_else(|| self.agent.computing())
+ }
+
+ /// Stop waiting for the agent's design change that is computing: it is answered as stopped
+ /// and nothing of it is applied (its worker finishes on its own, unseen). Requests waiting
+ /// behind it go on.
+ pub fn stop_agent_command(&mut self) {
+ let stopped = match self.control_job.take() {
+ Some(job) => Some((job.req.reply, job.label)),
+ None => self.agent.stop(),
+ };
+ if let Some((reply, label)) = stopped {
+ let _ = reply.send(json!({
+ "ok": false,
+ "stopped": true,
+ "error": format!("stopped: the person at the app stopped `{label}` while it computed; nothing was applied"),
+ }));
+ }
+ }
+
+ /// Take a control request off the window thread when it computes long (see the module
+ /// notes). Returns it when it should simply run here now.
+ pub(crate) fn offload_control(&mut self, ctx: &egui::Context, req: ControlRequest) -> Option<ControlRequest> {
+ if self.apply_off_thread.is_none() || !self.preferences.agent_off_thread || self.file_load.busy() || self.file_picker.is_some() {
+ return Some(req);
+ }
+ // An export: on a copy, answered from there.
+ if matches!(req.method.as_str(), "engine.execute" | "command")
+ && self.control_answering.len() < MAX_ANSWERING
+ && let Some((command, params)) = executed(&req.params)
+ && let Some(id) = spec_id(command).filter(|id| ANSWERED_ON_A_COPY.contains(id))
+ {
+ let Ok(scope) = crate::control::request_scope(self, &req.params) else { return Some(req) };
+ let mut copy = self.session.scratch();
+ let rx = spawn(scope, move || {
+ Some(match copy.execute(id, &params) {
+ Ok(v) => json!({"ok": true, "result": v}),
+ Err(e) => json!({"ok": false, "error": e.to_string()}),
+ })
+ });
+ let Some(rx) = rx else { return Some(req) };
+ let job = ControlJob { label: label(self, id), req, work: Work::Answer, done: rx, started: crate::now_ms() };
+ match within_wait(self, &job.done) {
+ Some(reply) => self.answered(job, reply),
+ None => self.control_answering.push(job),
+ }
+ return None;
+ }
+ let commands = ahead_commands(&req.method, &req.params);
+ let Some(first) = commands.first().map(|(c, _)| c.clone()) else { return Some(req) };
+ let Some((work, rx)) = begin_ahead(self, &req, commands, 0) else { return Some(req) };
+ let job = ControlJob { label: label(self, &first), req, work, done: rx, started: crate::now_ms() };
+ if within_wait(self, &job.done).is_some() {
+ // Computed within the wait: it runs now, as before, finding what was computed.
+ let Work::Ahead { kept, .. } = job.work else { return Some(job.req) };
+ self.run_with_kept(Some(kept), |app| app.run_control(ctx, job.req));
+ return None;
+ }
+ self.control_job = Some(job);
+ None
+ }
+
+ /// An export's reply, from its copy, with the echo the command leaves when it runs here.
+ fn answered(&mut self, job: ControlJob, reply: Option<Value>) {
+ let reply = reply.unwrap_or_else(|| json!({"ok": false, "error": format!("`{}` ended without a reply", job.label)}));
+ if reply["ok"] == true {
+ let language = crate::i18n::Locale::for_language(&self.preferences.language);
+ self.session.echo(language.format("ui.command_done", &[("command", job.label.as_str())]));
+ }
+ let _ = job.req.reply.send(reply);
+ }
+
+ /// Each frame: exports that are done reply; a design change whose evaluation is in runs.
+ pub(crate) fn poll_control_jobs(&mut self, ctx: &egui::Context) {
+ if !self.control_answering.is_empty() {
+ let mut i = 0;
+ while i < self.control_answering.len() {
+ match self.control_answering.get(i).and_then(ControlJob::poll) {
+ Some(reply) => {
+ let job = self.control_answering.remove(i);
+ self.answered(job, reply);
+ }
+ None => i += 1,
+ }
+ }
+ ctx.request_repaint_after(std::time::Duration::from_millis(100));
+ }
+ let Some(job) = &self.control_job else { return };
+ if job.poll().is_none() {
+ ctx.request_repaint_after(std::time::Duration::from_millis(100));
+ return;
+ }
+ let Some(job) = self.control_job.take() else { return };
+ // A file opening meanwhile: the request waits for it as any design change does.
+ if self.file_load.busy() || self.file_picker.is_some() {
+ self.control_held.push_front(job.req);
+ return;
+ }
+ let Work::Ahead { commands, doc, revision, tries, kept } = job.work else { return };
+ let s = &self.session;
+ let same = Arc::ptr_eq(&s.doc, &doc);
+ // The design it was computed for is still this one, edited since (an undo step away).
+ let edited = !same && s.undo.iter().chain(&s.redo).any(|snap| Arc::ptr_eq(&snap.doc, &doc));
+ if !same && !edited {
+ let _ = job.req.reply.send(json!({
+ "ok": false,
+ "error": format!("the design was replaced (another tab, or a file opened) while `{}` computed; nothing was applied", job.label),
+ }));
+ return;
+ }
+ if (!same || s.revision != revision) && tries < MAX_TRIES {
+ // A person's edit went first: computed again for the design as it is now.
+ if let Some((work, rx)) = begin_ahead(self, &job.req, commands, tries + 1) {
+ self.control_job = Some(ControlJob { work, done: rx, ..job });
+ return;
+ }
+ }
+ self.big_operation |= crate::now_ms() - job.started >= crate::recovery_ui::BIG_OPERATION_MS;
+ self.run_with_kept(Some(kept), |app| app.run_control(ctx, job.req));
+ ctx.request_repaint();
+ }
+}
diff --git a/crates/ui-egui/src/history_off_thread_tests.rs b/crates/ui-egui/src/history_off_thread_tests.rs
index c06c16576c..d4ec4d6ab6 100644
--- a/crates/ui-egui/src/history_off_thread_tests.rs
+++ b/crates/ui-egui/src/history_off_thread_tests.rs
@@ -1,6 +1,7 @@
//! Commands that recompute the history off the window thread (`deferred`): the marker moved,
//! undo, each computed beside the window and run once in; a marker moved again while one move
-//! computes replaces the move still waiting.
+//! computes replaces the move still waiting. And an agent's control requests (`control_job`,
+//! #1989): its design changes computed beside the window, in order, while the window answers.
use std::time::Duration;
@@ -99,6 +100,257 @@ fn a_design_changed_while_a_move_computes_is_computed_again() {
assert_eq!(marker(&h), Some(2));
}
+/// A control request for the app, and where its reply lands.
+fn ask(tx: &std::sync::mpsc::Sender<crate::ControlRequest>, method: &str, params: serde_json::Value) -> std::sync::mpsc::Receiver<serde_json::Value> {
+ let (req, reply) = crate::ControlRequest::new(method, params);
+ tx.send(req).unwrap();
+ reply
+}
+
+/// The block with its boss and hole, driven over the control channel.
+fn agent_harness() -> (Harness, std::sync::mpsc::Sender<crate::ControlRequest>) {
+ let mut h = block_boss_hole();
+ h.app.preferences.agent_off_thread = true;
+ let (tx, rx) = std::sync::mpsc::channel();
+ h.app.control_rx = Some(rx);
+ (h, tx)
+}
+
+/// Frames until `reply` lands (a few seconds at most).
+fn reply_to(h: &mut Harness, reply: &std::sync::mpsc::Receiver<serde_json::Value>) -> serde_json::Value {
+ for _ in 0..500 {
+ if let Ok(v) = reply.try_recv() {
+ return v;
+ }
+ h.frames(1);
+ std::thread::sleep(Duration::from_millis(10));
+ }
+ panic!("no reply");
+}
+
+fn second_hole() -> serde_json::Value {
+ json!({"command": "solid.cylinder", "params": {"base": [50, 10, -1], "axis": [0, 0, 1], "radius": 3, "height": 20, "operation": "cut"}})
+}
+
+/// #1989: an agent's design change computes off the window thread. The window keeps answering
+/// (`ui.*`, inspection) while it computes and shows which command it is; the agent's next
+/// design change waits its turn; each lands as its own undo step with its usual reply.
+#[test]
+fn an_agent_design_change_computes_off_the_window_thread() {
+ let (mut h, tx) = agent_harness();
+ let features = h.app.session.doc.features.len();
+ let undo = h.app.session.undo.len();
+ let hole = ask(&tx, "engine.execute", second_hole());
+ let view = ask(&tx, "ui.inspect", json!({}));
+ let inspect = ask(&tx, "document.inspect", json!({}));
+ let boss = ask(
+ &tx,
+ "engine.execute",
+ json!({"command": "solid.box", "params": {"corner": [40, 5, 10], "length": 5, "width": 5, "height": 5, "operation": "join"}}),
+ );
+ h.frames(1);
+ assert!(h.app.agent_computing().is_some_and(|(_, label)| !label.is_empty()), "the window says the agent's command computes");
+ assert!(view.try_recv().is_ok_and(|v| v["ok"] == true), "ui.* answered while it computes");
+ let seen = inspect.try_recv().expect("inspection answered while it computes");
+ assert_eq!(seen["result"]["timeline"].as_array().map(Vec::len), Some(features), "the design as it was: {seen}");
+ assert!(boss.try_recv().is_err(), "the next design change waits its turn");
+ assert_eq!(h.app.session.doc.features.len(), features, "nothing applied yet");
+ let r = reply_to(&mut h, &hole);
+ assert_eq!(r["ok"], true, "{r}");
+ let r = reply_to(&mut h, &boss);
+ assert_eq!(r["ok"], true, "{r}");
+ assert_eq!(h.app.session.doc.features.len(), features + 2);
+ assert_eq!(h.app.session.undo.len(), undo + 2, "one undo step each");
+ assert!(h.app.agent_computing().is_none());
+ // In the order they were asked for: undo takes the boss, the hole stays.
+ h.app.run("edit.undo", json!({})).unwrap();
+ let holes = std::f64::consts::PI * (4.0 * 15.0 + 9.0 * 10.0);
+ let v = h.app.run("inspect.measure", json!({})).unwrap()["total"]["volume_mm3"].as_f64().unwrap();
+ assert!((v - (12500.0 - holes)).abs() < 1.0, "{v}");
+}
+
+/// A person's edit while the agent's command computes goes first; the agent's command is
+/// computed again for the design as it is and lands after it, never in between.
+#[test]
+fn a_persons_edit_meanwhile_goes_first_and_the_agents_lands_after() {
+ let (mut h, tx) = agent_harness();
+ let features = h.app.session.doc.features.len();
+ let hole = ask(&tx, "engine.execute", second_hole());
+ h.frames(1);
+ assert!(h.app.agent_computing().is_some());
+ h.app.run("solid.box", json!({"corner": [40, 5, 10], "length": 5, "width": 5, "height": 5, "operation": "join"})).unwrap();
+ let r = reply_to(&mut h, &hole);
+ assert_eq!(r["ok"], true, "{r}");
+ assert_eq!(h.app.session.doc.features.len(), features + 2, "both kept");
+ // Undo takes the agent's hole first: it came last.
+ h.app.run("edit.undo", json!({})).unwrap();
+ assert_eq!(h.app.session.doc.features.len(), features + 1);
+ let boss = 12500.0 + 125.0 - std::f64::consts::PI * 4.0 * 15.0;
+ let v = h.app.run("inspect.measure", json!({})).unwrap()["total"]["volume_mm3"].as_f64().unwrap();
+ assert!((v - boss).abs() < 1.0, "the person's boss stays: {v}");
+}
+
+/// A design replaced while the agent's command computes (another tab, a new design) is not
+/// where it lands: it is refused, saying so, and nothing is applied.
+#[test]
+fn an_agent_command_is_refused_when_its_design_was_replaced() {
+ let (mut h, tx) = agent_harness();
+ let hole = ask(&tx, "engine.execute", second_hole());
+ h.frames(1);
+ assert!(h.app.agent_computing().is_some());
+ h.app.run("file.new", json!({})).unwrap();
+ let r = reply_to(&mut h, &hole);
+ assert_eq!(r["ok"], false, "{r}");
+ assert!(r["error"].as_str().is_some_and(|e| e.contains("replaced") && e.contains("nothing was applied")), "{r}");
+ assert!(h.app.session.doc.features.is_empty(), "nothing went into the new design");
+}
+
+/// Stop answers the agent at once, applies nothing, and lets the requests behind it go on.
+#[test]
+fn stopping_an_agent_command_applies_nothing_and_goes_on() {
+ let (mut h, tx) = agent_harness();
+ let features = h.app.session.doc.features.len();
+ let hole = ask(&tx, "engine.execute", second_hole());
+ let boss = ask(
+ &tx,
+ "engine.execute",
+ json!({"command": "solid.box", "params": {"corner": [40, 5, 10], "length": 5, "width": 5, "height": 5, "operation": "join"}}),
+ );
+ h.frames(1);
+ assert!(h.app.agent_computing().is_some());
+ h.app.stop_agent_command();
+ let r = hole.try_recv().expect("answered at once");
+ assert!(r["ok"] == false && r["stopped"] == true, "{r}");
+ let r = reply_to(&mut h, &boss);
+ assert_eq!(r["ok"], true, "{r}");
+ assert_eq!(h.app.session.doc.features.len(), features + 1, "only the boss");
+}
+
+/// A batch computes off the window thread too, with the reply a batch run here gives.
+#[test]
+fn an_agent_batch_computes_off_the_window_thread() {
+ let (mut h, tx) = agent_harness();
+ let features = h.app.session.doc.features.len();
+ let steps = json!({"commands": [second_hole(), {"command": "solid.box", "params": {"corner": [40, 5, 10], "length": 5, "width": 5, "height": 5, "operation": "join"}}]});
+ let batch = ask(&tx, "engine.batch", steps);
+ h.frames(1);
+ assert!(h.app.agent_computing().is_some(), "computing");
+ assert_eq!(h.app.session.doc.features.len(), features);
+ let r = reply_to(&mut h, &batch);
+ assert_eq!((&r["ok"], &r["result"]["completed"], &r["result"]["undone"]), (&json!(true), &json!(2), &json!(0)), "{r}");
+ assert_eq!(h.app.session.doc.features.len(), features + 2);
+}
+
+/// An export changes nothing in the design: it runs on a copy and answers from there, the file
+/// written; a design change asked for meanwhile does not wait for it.
+#[test]
+fn an_agent_export_runs_on_a_copy() {
+ let (mut h, tx) = agent_harness();
+ let dir = std::env::temp_dir().join(format!("sc-agent-export-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let path = dir.join("part.stl").to_string_lossy().to_string();
+ let export = ask(&tx, "engine.execute", json!({"command": "file.export", "params": {"path": path}}));
+ let r = reply_to(&mut h, &export);
+ assert_eq!(r["ok"], true, "{r}");
+ assert!(std::fs::metadata(&path).is_ok_and(|m| m.len() > 0), "written");
+ assert!(h.app.session.log.iter().any(|l| l.contains("Export")), "echoed as when it runs here: {:?}", h.app.session.log);
+ let _ = std::fs::remove_dir_all(&dir);
+}
+
+/// With the preference off (Preferences > AI, the default) an agent's design change runs on the
+/// window thread, as before: answered in the frame it arrives.
+#[test]
+fn with_the_preference_off_an_agent_command_runs_at_once() {
+ let (mut h, tx) = agent_harness();
+ h.app.preferences.agent_off_thread = false;
+ let features = h.app.session.doc.features.len();
+ let hole = ask(&tx, "engine.execute", second_hole());
+ h.frames(1);
+ assert!(h.app.agent_computing().is_none());
+ assert!(hole.try_recv().is_ok_and(|r| r["ok"] == true), "answered in the same frame");
+ assert_eq!(h.app.session.doc.features.len(), features + 1);
+}
+
+/// #1989: with the agent cursor shown, the pointer acts out the agent's design change while it
+/// computes off the window thread. Meanwhile the window answers `ui.*` and inspection (it used to
+/// answer nothing at all until the command had run: a client polling the window saw it frozen),
+/// and the next design change waits its turn.
+#[test]
+fn the_window_answers_while_the_agent_cursor_acts_out_a_design_change() {
+ let (mut h, tx) = agent_harness();
+ h.app.ui.agent_cursor.show = true;
+ h.frames(2);
+ let features = h.app.session.doc.features.len();
+ let hole = ask(&tx, "engine.execute", second_hole());
+ h.frames(1);
+ assert!(h.app.agent.busy(), "the pointer acts it out");
+ let view = ask(&tx, "ui.inspect", json!({}));
+ let inspect = ask(&tx, "document.inspect", json!({}));
+ let boss = ask(
+ &tx,
+ "engine.execute",
+ json!({"command": "solid.box", "params": {"corner": [40, 5, 10], "length": 5, "width": 5, "height": 5, "operation": "join"}}),
+ );
+ h.frames(1);
+ assert!(h.app.agent.busy());
+ assert!(view.try_recv().is_ok_and(|v| v["ok"] == true), "ui.* answered while the pointer acts out the command");
+ assert!(inspect.try_recv().is_ok_and(|v| v["ok"] == true), "inspection answered too");
+ assert!(boss.try_recv().is_err(), "the next design change waits its turn");
+ assert_eq!(h.app.session.doc.features.len(), features, "nothing applied yet");
+ let r = reply_to(&mut h, &hole);
+ assert_eq!(r["ok"], true, "{r}");
+ let r = reply_to(&mut h, &boss);
+ assert_eq!(r["ok"], true, "{r}");
+ assert_eq!(h.app.session.doc.features.len(), features + 2, "both, in order");
+}
+
+/// #1989: the run on the window thread reuses what was computed ahead, even when the evaluation
+/// cache dropped it meanwhile (a design with large bodies keeps only a few states there): it
+/// computes nothing again, so the window waits only for the bookkeeping. A parameter that drives
+/// the whole timeline, changed by the agent with the cursor off and with it shown.
+#[test]
+fn the_window_thread_run_reuses_what_was_computed_ahead() {
+ for cursor in [false, true] {
+ let mut h = Harness::new();
+ h.app.home.open = false;
+ h.app.run("parameters.add", json!({"name": "len", "expression": "60 mm"})).unwrap();
+ for (id, p) in [
+ ("solid.box", json!({"length": "len", "width": 20, "height": 10})),
+ ("solid.box", json!({"corner": ["len / 2", 5, 10], "length": 10, "width": 10, "height": 5, "operation": "join"})),
+ ("solid.cylinder", json!({"base": ["len / 2 + 5", 10, -1], "axis": [0, 0, 1], "radius": 2, "height": 20, "operation": "cut"})),
+ ] {
+ h.app.run(id, p).unwrap();
+ }
+ h.app.apply_off_thread = Some(Duration::ZERO);
+ h.app.preferences.agent_off_thread = true;
+ h.app.ui.agent_cursor.show = cursor;
+ let (tx, rx) = std::sync::mpsc::channel();
+ h.app.control_rx = Some(rx);
+ h.frames(2);
+ let recomputed = h.app.session.model.recomputed;
+ let change = ask(&tx, "engine.execute", json!({"command": "parameters.change", "params": {"name": "len", "expression": "70 mm"}}));
+ let mut reply = None;
+ for _ in 0..500 {
+ // The cache drops everything the evaluation ahead puts there.
+ for _ in 0..2 {
+ *h.app.session.model.cache.lock().unwrap() = Default::default();
+ std::thread::sleep(Duration::from_millis(5));
+ }
+ h.frames(1);
+ if let Ok(v) = change.try_recv() {
+ reply = Some(v);
+ break;
+ }
+ }
+ let r = reply.expect("answered");
+ assert_eq!(r["ok"], true, "{r}");
+ assert_eq!(h.app.session.model.recomputed, recomputed, "cursor {cursor}: nothing computed again on the window thread");
+ let v = h.app.run("inspect.measure", json!({})).unwrap()["total"]["volume_mm3"].as_f64().unwrap();
+ let want = 70.0 * 200.0 + 500.0 - std::f64::consts::PI * 4.0 * 15.0;
+ assert!((v - want).abs() < 1.0, "the design as changed: {v}");
+ }
+}
+
fn feature_ids(h: &Harness) -> Vec<u64> {
h.app.session.doc.features.iter().map(|f| f.id).collect()
}
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index dc5a9417f7..b8c657e56d 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -23,6 +23,7 @@ mod colour_vision_tests;
pub mod command_help;
pub mod context_menu;
pub mod control;
+pub mod control_job;
pub mod crash_ui;
pub mod deferred;
pub mod delete;
@@ -466,8 +467,13 @@ pub struct SolveApp {
/// The modifiers of the synthetic press in progress (a Ctrl-drag keeps Ctrl while it moves).
pub synthetic_mods: egui::Modifiers,
control_rx: Option<Receiver<ControlRequest>>,
- /// Control requests that change the design, held while a file loads.
+ /// Control requests that change the design, held while a file loads or an agent's design
+ /// change computes.
control_held: std::collections::VecDeque<ControlRequest>,
+ /// An agent's design change computing off the window thread ([`control_job`]).
+ control_job: Option<control_job::ControlJob>,
+ /// Agent exports computing on copies of the design.
+ control_answering: Vec<control_job::ControlJob>,
/// The agent cursor acting out control requests.
pub agent: agent_cursor::AgentCursor,
/// The agent chat panel (docs/design/in-app-agent-chat.md).
@@ -580,6 +586,8 @@ impl SolveApp {
control_rx: None,
agent_chat: Default::default(),
control_held: Default::default(),
+ control_job: None,
+ control_answering: Vec::new(),
agent: Default::default(),
pending_shots: Vec::new(),
queued_shots: Vec::new(),
@@ -1426,6 +1434,7 @@ impl SolveApp {
self.set_up_axis(self.ui.up_axis);
}
self.step_view_animation(ctx);
+ self.poll_control_jobs(ctx);
self.drain_control(ctx);
self.follow_view_request(true);
agent_cursor::step(self, ctx);
@@ -1673,15 +1682,15 @@ impl SolveApp {
return;
}
let rx = self.control_rx.take();
- // While the agent cursor acts out a request, later ones wait their turn.
for _ in 0..PER_FRAME {
- if self.agent.busy() {
- break;
- }
- // Held requests go first, in order, once the file is in.
+ // A file loading, an agent's design change computing, or the agent cursor acting one
+ // out: commands that change the design wait their turn (the window, inspection and
+ // screenshots still answer, #1989).
+ let waits = self.file_load.busy() || self.file_picker.is_some() || self.control_job.is_some() || self.agent.busy();
// A screenshot waiting for an opened design to be meshed holds what came after it.
let shot_waits = open_meshing::active(self) && self.control_held.front().is_some_and(|r| r.method == "ui.screenshot");
- let held = if self.file_load.busy() || self.file_picker.is_some() || shot_waits { None } else { self.control_held.pop_front() };
+ // Held requests go first, in order, once nothing makes them wait.
+ let held = if waits || shot_waits { None } else { self.control_held.pop_front() };
let Some(req) = held.or_else(|| rx.as_ref().and_then(|rx| rx.try_recv().ok())) else { break };
if shot_waits {
if self.control_held.len() < MAX_HELD {
@@ -1691,10 +1700,7 @@ impl SolveApp {
}
continue;
}
- // A file is loading: commands that change the design wait for it (the window,
- // inspection and screenshots still answer).
- if ((self.file_load.busy() || self.file_picker.is_some())
- && matches!(req.method.as_str(), "engine.execute" | "command" | "engine.script" | "engine.batch"))
+ if (waits && control_job::changes_the_design(&req.method))
|| (self.file_picker_busy() && matches!(req.method.as_str(), "ui.file" | "ui.home"))
// A screenshot shows the whole design: it waits until an opened one is meshed.
|| (open_meshing::active(self) && req.method == "ui.screenshot")
@@ -1717,27 +1723,38 @@ impl SolveApp {
ctx.request_repaint();
continue;
}
- match control::handle(self, ctx, &req) {
- control::Outcome::Done(v) => {
- let _ = reply.send(v);
- }
- // A window that is minimised or covered (another macOS Space) presents no frame:
- // say so at once rather than after the deadline (#1677).
- control::Outcome::Screenshot { .. } if window_hidden(ctx) => {
- let _ = reply.send(json!({"ok": false, "hidden": true, "error": "no frame was presented (window hidden?); use ui.render"}));
- }
- control::Outcome::Screenshot { path } => {
- self.shot_token += 1;
- let token = self.shot_token;
- self.queued_shots.push((token, now_ms() + 150.0, 0));
- self.pending_shots.push((token, path, reply, now_ms() + 8000.0));
- }
- }
- ctx.request_repaint();
+ // A slow design change or export: computed off the window thread (#1989).
+ let Some(req) = self.offload_control(ctx, req) else {
+ ctx.request_repaint();
+ continue;
+ };
+ self.run_control(ctx, req);
}
self.control_rx = rx;
}
+ /// Run a control request here, now, and reply (or queue the screenshot it asks for).
+ pub(crate) fn run_control(&mut self, ctx: &egui::Context, req: ControlRequest) {
+ let reply = req.reply.clone();
+ match control::handle(self, ctx, &req) {
+ control::Outcome::Done(v) => {
+ let _ = reply.send(v);
+ }
+ // A window that is minimised or covered (another macOS Space) presents no frame:
+ // say so at once rather than after the deadline (#1677).
+ control::Outcome::Screenshot { .. } if window_hidden(ctx) => {
+ let _ = reply.send(json!({"ok": false, "hidden": true, "error": "no frame was presented (window hidden?); use ui.render"}));
+ }
+ control::Outcome::Screenshot { path } => {
+ self.shot_token += 1;
+ let token = self.shot_token;
+ self.queued_shots.push((token, now_ms() + 150.0, 0));
+ self.pending_shots.push((token, path, reply, now_ms() + 8000.0));
+ }
+ }
+ ctx.request_repaint();
+ }
+
fn issue_screenshots(&mut self, ctx: &egui::Context) {
let now = now_ms();
self.queued_shots.retain_mut(|(token, at, frames)| {
diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs
index 66a890897e..64e4b4043b 100644
--- a/crates/ui-egui/src/prefs.rs
+++ b/crates/ui-egui/src/prefs.rs
@@ -463,6 +463,10 @@ preference_settings! {
ai_features: bool = true, PAGE_AI;
/// The Assistant button in the application bar (the shortcut and View menu entry stay).
ai_toolbar_button: bool = true, PAGE_AI;
+ /// An agent's slow commands over the control channel (MCP) compute off the window thread,
+ /// so the window stays live while they run (#1989, [`crate::control_job`]). Off: they run on
+ /// the window thread, and the window waits for each, as before (the default).
+ agent_off_thread: bool = false, PAGE_AI;
/// Agents the user added (the installed ones are found by themselves). API keys are never
/// here: they live in the system keychain.
agents: Vec<crate::agent_chat::agents::AgentConfig> = Vec::new();
diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs
index 6a2fe1413e..37a36216a8 100644
--- a/crates/ui-egui/src/viewport.rs
+++ b/crates/ui-egui/src/viewport.rs
@@ -2986,6 +2986,7 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) {
crate::select_other::show(app, ui.ctx());
axis_view(app, ui, rect);
nav_bar(app, ui, rect);
+ agent_chip(app, ui, rect);
transient_messages(app, ui, rect);
}
@@ -3009,6 +3010,38 @@ fn paint_box_selection(painter: &egui::Painter, bx: BoxSel, sketch: bool) {
}
}
+/// An agent's command computing off the window thread (#1989): which one, in the agent cursor's
+/// colour, and a button that stops waiting for it.
+fn agent_chip(app: &mut SolveApp, ui: &egui::Ui, rect: Rect) {
+ let t = Tokens::get();
+ let painter = ui.painter().clone();
+ if let Some((since, label)) = app.agent_computing().map(|(since, label)| (since, label.to_string())) {
+ let seconds = ((crate::now_ms() - since) / 1000.0).floor().max(0.0);
+ let computing = crate::i18n::format("dialog.computing", &[("seconds", &format!("{seconds:.0}"))]);
+ let above = if app.computing_since().is_some() { 106.0 } else { 76.0 };
+ let pos = pos2(rect.left() + 12.0, rect.bottom() - above);
+ let galley = painter.layout_no_wrap(format!("{label} · {computing}"), FontId::proportional(t.font_label), t.text);
+ let stop = painter.layout_no_wrap(crate::i18n::tr("ui.agent_chat_stop").to_string(), FontId::proportional(t.font_label), t.text);
+ let r = Rect::from_min_size(pos, vec2(galley.size().x + stop.size().x + 54.0, galley.size().y.max(stop.size().y) + 8.0));
+ painter.rect_filled(r, 4.0, t.overlay);
+ painter.rect_stroke(r, 4.0, Stroke::new(1.0, crate::icons::pointer::agent()), egui::StrokeKind::Inside);
+ egui::Spinner::new()
+ .color(crate::icons::pointer::agent())
+ .size(t.space_12)
+ .paint_at(ui, Rect::from_center_size(pos2(r.left() + 13.0, r.center().y), vec2(12.0, 12.0)));
+ painter.galley(pos + vec2(26.0, 4.0), galley.clone(), t.text);
+ let button = Rect::from_min_size(pos2(pos.x + galley.size().x + 34.0, r.top() + 2.0), vec2(stop.size().x + 12.0, r.height() - 4.0));
+ let clicked = ui.interact(button, egui::Id::new("sc_agent_stop"), Sense::click());
+ painter.rect_filled(button, 3.0, if clicked.hovered() { t.hover } else { t.overlay });
+ painter.galley(button.min + vec2(6.0, 2.0), stop, t.text);
+ crate::scenario::publish_handle("agent:stop", button.center());
+ if clicked.clicked() {
+ app.stop_agent_command();
+ }
+ ui.ctx().request_repaint_after(std::time::Duration::from_millis(100));
+ }
+}
+
/// A single visual pattern for ongoing work and the existing six-second status.
fn transient_messages(app: &mut SolveApp, ui: &egui::Ui, viewport: Rect) {
if app.status.is_none() && app.computing_since().is_none() {
diff --git a/docs/control-protocol.md b/docs/control-protocol.md
index 5e852bb055..690f986d37 100644
--- a/docs/control-protocol.md
+++ b/docs/control-protocol.md
@@ -99,6 +99,38 @@ MCP server sends its folders this way on every request ([mcp.md](mcp.md)). Witho
Commands never open dialogs when run this way. `solidcraft-cli commands` prints the same registry.
+### Slow commands and the window (#1989)
+
+With the preference **Preferences > AI > Keep the window responsive while agents run commands**
+on (`ui.prefs {"agent_off_thread": true}`; off by default), a design change in the desktop app
+that takes longer than about 150 ms (an `engine.execute` of a
+modelling command, an `engine.batch` of them) computes off the window thread, so the window keeps
+drawing and answering while it runs:
+- The command is evaluated ahead on a copy of the design, which also makes the display meshes of
+ the bodies it ends with. Then it runs for real on the window thread, reusing every feature the
+ evaluation computed (kept for it, whatever the bounded evaluation cache dropped meanwhile), so
+ the window waits only for the bookkeeping. Its reply, undo step, autosave and crash journal are
+ the same as before. With the agent cursor shown, the evaluation runs while the pointer glides
+ to the button, and the pointer waits there until it is done.
+- Meanwhile `ui.*` requests, `document.inspect` and `engine.commands` are answered at once
+ (inspection shows the design as it was), also while the agent cursor acts a command out. Later
+ design changes, from any connection, wait their turn and run in the order they arrived.
+- A person's edit made meanwhile goes first. The agent's command is then computed again for the
+ design as it is and lands after that edit, never in the middle of it. If the design was replaced
+ (another tab made active, a new file opened), the request fails with `… the design was
+ replaced … nothing was applied`.
+- A chip in the view names the command, with **Stop**. Stop answers
+ `{"ok": false, "stopped": true, "error": "stopped: …; nothing was applied"}` and the requests
+ waiting behind it go on. The computation finishes unseen in the background.
+- `file.export` and `file.save_mesh` change nothing in the design. They run on a copy of the
+ design as it was when they were asked for, and nothing waits behind them.
+- Batches with `timings` or `profile` still run on the window thread, so the times they report
+ measure the real work.
+
+Timeouts and `control.pending` work as described above. A slow command is still answered when it
+is done, from a later frame. With the preference off, commands run on the window thread, and the
+window waits for each one.
+
Product images use `render.preflight/start/status/cancel/export` through `engine.execute`.
Keep the returned session-scoped job ID for subsequent calls; status does not block or
render. See [render jobs](render-jobs.md) for capture, cancellation and export semantics.