From 7e30ebb2610bfcd0bb9cf3561369605a9a91a825 Mon Sep 17 00:00:00 2001 From: broisnees Date: Tue, 17 Nov 2026 09:22:59 +0000 Subject: [PATCH] No console windows from the AI agent CLIs on Windows; reload the page if WebView2's renderer dies --- src-tauri/crates/agent-claude/src/lib.rs | 30 ++++++++++++++++++++-- src-tauri/crates/agent-cli/src/lib.rs | 3 ++- src-tauri/crates/agent-cli/src/models.rs | 4 +-- src-tauri/crates/agent-cli/src/opencode.rs | 2 +- src-tauri/src/lib.rs | 10 ++++++++ 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src-tauri/crates/agent-claude/src/lib.rs b/src-tauri/crates/agent-claude/src/lib.rs index d14fdfde..f0b4e81f 100644 --- a/src-tauri/crates/agent-claude/src/lib.rs +++ b/src-tauri/crates/agent-claude/src/lib.rs @@ -22,6 +22,32 @@ use tokio::process::{Child, ChildStdin, Command}; use tokio::sync::mpsc; /// Tokens of transcript we are willing to replay on a handoff. + +/// `CREATE_NO_WINDOW`. Stroke is a GUI process, so on Windows every console +/// program it starts (`claude --version`, the chat itself) got a console of +/// its own: a black cmd window popped up on top of the app each time, and the +/// focus jumping to it could take WebView2 down with it. Unix has nothing +/// like it, so off Windows these are no-ops. +#[cfg(windows)] +const CREATE_NO_WINDOW: u32 = 0x0800_0000; + +/// Start this console program with no console window (Windows). +pub fn quiet(cmd: &mut Command) -> &mut Command { + #[cfg(windows)] + cmd.creation_flags(CREATE_NO_WINDOW); + cmd +} + +/// The blocking-`Command` form of [`quiet`]. +pub fn quiet_std(cmd: &mut std::process::Command) -> &mut std::process::Command { + #[cfg(windows)] + { + use std::os::windows::process::CommandExt; + cmd.creation_flags(CREATE_NO_WINDOW); + } + cmd +} + const REPLAY_BUDGET: usize = 60_000; /// Bound on buffered events. Deep enough to absorb a fast burst, shallow enough @@ -191,7 +217,7 @@ impl AgentAdapter for ClaudeAdapter { } async fn probe(&self) -> Result { - let out = Command::new(&self.bin).arg("--version").output().await; + let out = quiet(Command::new(&self.bin).arg("--version")).output().await; match out { Ok(o) if o.status.success() => Ok(Availability::Ready { version: Some(String::from_utf8_lossy(&o.stdout).trim().to_string()), @@ -210,7 +236,7 @@ impl AgentAdapter for ClaudeAdapter { let mcp_config = self.write_mcp_config(&ctx).await?; let args = self.args(&ctx, mcp_config.as_ref()); - let mut child = Command::new(&self.bin) + let mut child = quiet(&mut Command::new(&self.bin)) .args(&args) .envs(ctx.env.iter().map(|(k, v)| (k, v))) .current_dir(&ctx.cwd) diff --git a/src-tauri/crates/agent-cli/src/lib.rs b/src-tauri/crates/agent-cli/src/lib.rs index 59d9a5df..4369997f 100644 --- a/src-tauri/crates/agent-cli/src/lib.rs +++ b/src-tauri/crates/agent-cli/src/lib.rs @@ -127,7 +127,7 @@ impl AgentAdapter for CliAdapter { let listing = self.refresh_models(); // `--version` is the cheapest liveness check that does not hit the // network or consume credits. - let version = Command::new(self.spec.bin) + let version = agent_claude::quiet(&mut Command::new(self.spec.bin)) .arg("--version") .stdin(Stdio::null()) .output() @@ -259,6 +259,7 @@ pub(crate) fn spawn_turn( resuming: bool, ) -> Result { let mut cmd = Command::new(spec.bin); + agent_claude::quiet(&mut cmd); cmd.args(&args) .envs(spec.env.iter().copied()) .envs(ctx.env.iter().map(|(k, v)| (k, v))) diff --git a/src-tauri/crates/agent-cli/src/models.rs b/src-tauri/crates/agent-cli/src/models.rs index ea928bba..577ad3b0 100644 --- a/src-tauri/crates/agent-cli/src/models.rs +++ b/src-tauri/crates/agent-cli/src/models.rs @@ -84,7 +84,7 @@ fn leak(listed: Vec) -> &'static [ModelSpec] { /// Run a CLI's list command and read its answer. Blocks, so it runs on a /// thread of its own. pub(crate) fn read(bin: &str, list: &ListModels) -> Option> { - let child = std::process::Command::new(bin) + let child = agent_claude::quiet_std(&mut std::process::Command::new(bin)) .args(list.args) .stdin(Stdio::null()) .stdout(Stdio::piped()) @@ -169,7 +169,7 @@ pub(crate) async fn codex_default(bin: &str) -> Option { .and_then(|text| top_level(text, "model"))?; let out = tokio::time::timeout( Duration::from_secs(5), - tokio::process::Command::new(bin) + agent_claude::quiet(&mut tokio::process::Command::new(bin)) .args(["debug", "models"]) .stdin(Stdio::null()) .stderr(Stdio::null()) diff --git a/src-tauri/crates/agent-cli/src/opencode.rs b/src-tauri/crates/agent-cli/src/opencode.rs index cbcdc74d..fb52046a 100644 --- a/src-tauri/crates/agent-cli/src/opencode.rs +++ b/src-tauri/crates/agent-cli/src/opencode.rs @@ -61,7 +61,7 @@ kill -TERM 0"#; /// opencode 2 moved the server's API under `/api` with new routes, which the /// session below does not speak; on 2 and later each turn is an `opencode run`. pub(crate) async fn serve_api_moved(bin: &str) -> bool { - let out = Command::new(bin) + let out = agent_claude::quiet(&mut Command::new(bin)) .arg("--version") .stdin(Stdio::null()) .output() diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 51b08425..ceb4c7df 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -189,6 +189,7 @@ fn guard_windows_webview(window: &tauri::WebviewWindow) { ICoreWebView2AcceleratorKeyPressedEventArgs2, COREWEBVIEW2_KEY_EVENT_KIND, COREWEBVIEW2_KEY_EVENT_KIND_KEY_DOWN, COREWEBVIEW2_KEY_EVENT_KIND_SYSTEM_KEY_DOWN, COREWEBVIEW2_PROCESS_FAILED_KIND, COREWEBVIEW2_PROCESS_FAILED_KIND_BROWSER_PROCESS_EXITED, + COREWEBVIEW2_PROCESS_FAILED_KIND_RENDER_PROCESS_EXITED, }; use webview2_com::{AcceleratorKeyPressedEventHandler, ProcessFailedEventHandler}; use windows_core::Interface; @@ -253,6 +254,15 @@ fn guard_windows_webview(window: &tauri::WebviewWindow) { tauri::async_runtime::spawn(async move { let _ = host.set_decorations(true); }); + } else if kind == COREWEBVIEW2_PROCESS_FAILED_KIND_RENDER_PROCESS_EXITED { + // Only the page's process went (the browser is still up): the + // window stayed white with nothing to click. Load the page + // again; it reconnects the way a launch does. + log::error!("webview2: render process exited, reloading the page"); + let host = host.clone(); + tauri::async_runtime::spawn(async move { + let _ = host.reload(); + }); } Ok(()) }));