← Commit history
0.2.116: capture open finds an off-screen editor on macOS (closes #253)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
8 files changed
+99−24
Cargo.lock+10−10@@ -4,7 +4,7 @@ version = 4 [[package]] name = "adom-aiflow"-version = "0.2.115"+version = "0.2.116" dependencies = [ "aiflow-analyze", "aiflow-board",@@ -25,7 +25,7 @@ dependencies = [ [[package]] name = "aiflow-analyze"-version = "0.2.115"+version = "0.2.116" dependencies = [ "aiflow-board", "serde",@@ -35,7 +35,7 @@ dependencies = [ [[package]] name = "aiflow-board"-version = "0.2.115"+version = "0.2.116" dependencies = [ "roxmltree", "serde",@@ -44,7 +44,7 @@ dependencies = [ [[package]] name = "aiflow-bridge"-version = "0.2.115"+version = "0.2.116" dependencies = [ "aiflow-board", "serde",@@ -53,7 +53,7 @@ dependencies = [ [[package]] name = "aiflow-copper"-version = "0.2.115"+version = "0.2.116" dependencies = [ "aiflow-board", "aiflow-grid",@@ -63,7 +63,7 @@ dependencies = [ [[package]] name = "aiflow-grid"-version = "0.2.115"+version = "0.2.116" dependencies = [ "aiflow-board", "serde",@@ -72,7 +72,7 @@ dependencies = [ [[package]] name = "aiflow-place"-version = "0.2.115"+version = "0.2.116" dependencies = [ "aiflow-board", "serde",@@ -81,7 +81,7 @@ dependencies = [ [[package]] name = "aiflow-pours"-version = "0.2.115"+version = "0.2.116" dependencies = [ "aiflow-board", "aiflow-copper",@@ -91,7 +91,7 @@ dependencies = [ [[package]] name = "aiflow-router"-version = "0.2.115"+version = "0.2.116" dependencies = [ "aiflow-board", "aiflow-grid",@@ -102,7 +102,7 @@ dependencies = [ [[package]] name = "aiflow-run"-version = "0.2.115"+version = "0.2.116" dependencies = [ "serde", "serde_json",
Cargo.toml+1−1@@ -14,7 +14,7 @@ members = [ ] [workspace.package]-version = "0.2.115"+version = "0.2.116" edition = "2021" license = "MIT" repository = "https://wiki.adom.inc/adom/adom-aiflow"
bin/adom-aiflow⋯ 1 unchanged line ⋯
crates/adom-aiflow/src/main.rs+1−1@@ -7150,7 +7150,7 @@ fn main() { } "open" => { let raise = screen_mode(&mut r).may_raise();- let (hwnd, shown) = br.open_board(&rb, reason.as_deref().unwrap_or(&format!("aiflow run {engine} on the test box")), raise).unwrap_or_else(|rep| err(&format!("could not open the board: {}", rep.to_string().chars().take(300).collect::<String>()), &[]));+ let (hwnd, shown) = br.open_board(&rb, reason.as_deref().unwrap_or(&format!("aiflow run {engine} on the test box")), raise).unwrap_or_else(|rep| err(&format!("could not open the board: {}", rep["error"].as_str().filter(|_| rep.get("reply").is_some()).map(str::to_string).unwrap_or_else(|| rep.to_string().chars().take(300).collect::<String>())), &[])); r.data["captures"].as_array_mut().unwrap().push(json!({"opened": now(), "hwnd": hwnd, "window": shown})); r.save().unwrap(); let (revision, reply) = br.revision_with_reply(&rb);
crates/aiflow-bridge/src/lib.rs+74−10@@ -535,12 +535,21 @@ impl Bridge { board_window_in(&self.raw_call("kicad_status", &json!({})), &self.raw_call("desktop_list_windows", &json!({})), &board_stem(remote_board)) } + /// The board's editor window and whether it is on screen: `board_window`, else (issue #253, Drew Owens) the+ /// desktop's off-screen windows too. On macOS the window list holds on-screen windows only and `kicad_status`+ /// names no editors, so an editor opened behind a full-screen app (on another Space) is found only there.+ pub fn board_window_any(&self, remote_board: &str, pid: Option<i64>) -> Option<(i64, bool)> {+ if let Some(h) = self.board_window(remote_board) { return Some((h, true)); }+ offscreen_board_window_in(&self.raw_call("desktop_list_windows", &json!({"includeOffscreen": true})), &board_stem(remote_board), pid)+ }+ /// After `kicad_open_board` answered without a window handle (a background open says so in- /// `_foregroundHint`), the board's window as it appears, polled for up to a minute.- fn wait_board_window(&self, remote_board: &str, reply: &Value) -> Option<i64> {+ /// `_foregroundHint`), the board's window as it appears, polled for up to a minute: (hwnd, on screen).+ fn wait_board_window(&self, remote_board: &str, reply: &Value) -> Option<(i64, bool)> { if !opened_without_handle(reply) { return None; }+ let pid = open_pid(reply); for _ in 0..20 {- if let Some(h) = self.board_window(remote_board) { return Some(h); }+ if let Some(w) = self.board_window_any(remote_board, pid) { return Some(w); } std::thread::sleep(std::time::Duration::from_secs(3)); } None@@ -548,24 +557,25 @@ impl Bridge { pub fn open_board(&self, remote_board: &str, reason: &str, raise: bool) -> Result<(i64, String), Value> { // issue #222: a board already open (an earlier try, a background open) is reused: never a second pcbnew- if let Some(hwnd) = self.board_window(remote_board) {+ if let Some((hwnd, onscreen)) = self.board_window_any(remote_board, None) { if raise { self.call("desktop_ui_window", &json!({"hwnd": hwnd, "action": "maximize", "reason": "aiflow capture: fill the monitor"})); self.call("desktop_bring_to_front", &json!({"hwnd": hwnd, "reason": "aiflow capture: keep the PCB editor on screen on the test box"})); } self.call("kicad_send_key", &json!({"hwnd": hwnd, "menu": "Zoom to Fit"}));- return Ok((hwnd, format!("already open in KiCad (window {hwnd}): reused, no second pcbnew started{}", if raise { ", maximised" } else { "" })));+ return Ok((hwnd, format!("already open in KiCad (window {hwnd}): reused, no second pcbnew started{}{}", if raise { ", maximised" } else { "" }, if onscreen { "" } else { OFFSCREEN_NOTE }))); } if !raise { let before = self.foreground(); let r = self.call("kicad_open_board", &json!({"filePath": remote_board, "foreground": false}));- let Some(hwnd) = r.get("hwnd").and_then(|h| h.as_i64()).or_else(|| self.wait_board_window(remote_board, &r)) else { return Err(r) };+ let Some((hwnd, onscreen)) = r.get("hwnd").and_then(|h| h.as_i64()).map(|h| (h, true)).or_else(|| self.wait_board_window(remote_board, &r)) else { return Err(not_found(remote_board, r)) };+ let off = if onscreen { "" } else { OFFSCREEN_NOTE }; // KiCad Bridge 1.1.7: the open says whether the person is working and whether KiCad came in front; // the bridge's own rule then (once, never a loop), no foreground guessing of ours if let Some(said) = self.follow_focus_rule(&r) { std::thread::sleep(std::time::Duration::from_secs(40)); self.call("kicad_send_key", &json!({"hwnd": hwnd, "menu": "Zoom to Fit"}));- return Ok((hwnd, format!("opened in the background, not maximised (screen hands-off), zoomed to fit; {said}")));+ return Ok((hwnd, format!("opened in the background, not maximised (screen hands-off), zoomed to fit; {said}{off}"))); } let mut done: Vec<String> = self.keep_behind(hwnd, before.as_ref()).into_iter().collect(); std::thread::sleep(std::time::Duration::from_secs(40));@@ -574,10 +584,10 @@ impl Bridge { self.call("kicad_send_key", &json!({"hwnd": hwnd, "menu": "Zoom to Fit"})); std::thread::sleep(std::time::Duration::from_secs(3)); let what = if done.is_empty() { "the human's window stayed in front".to_string() } else { done.join("; ") };- return Ok((hwnd, format!("opened in the background, not maximised (screen hands-off), zoomed to fit; {what}")));+ return Ok((hwnd, format!("opened in the background, not maximised (screen hands-off), zoomed to fit; {what}{off}"))); } let r = self.call("kicad_open_board", &json!({"filePath": remote_board, "foreground": true, "foregroundReason": reason}));- let Some(hwnd) = r.get("hwnd").and_then(|h| h.as_i64()).or_else(|| self.wait_board_window(remote_board, &r)) else { return Err(r) };+ let Some(hwnd) = r.get("hwnd").and_then(|h| h.as_i64()).or_else(|| self.wait_board_window(remote_board, &r).map(|w| w.0)) else { return Err(not_found(remote_board, r)) }; self.call("desktop_ui_window", &json!({"hwnd": hwnd, "action": "maximize", "reason": "aiflow capture: fill the monitor"})); std::thread::sleep(std::time::Duration::from_secs(40)); self.call("desktop_bring_to_front", &json!({"hwnd": hwnd, "reason": "aiflow capture: keep the PCB editor on screen on the test box"}));@@ -594,7 +604,7 @@ impl Bridge { let r = self.call("kicad_open_board", &json!({"filePath": remote_board, "isolated": true, "foreground": false, "reason": reason})); let mut iso = isolated::isolated_of(&r)?; if !raise { let _ = self.follow_focus_rule(&r); }- if iso.hwnd.is_none() { iso.hwnd = self.wait_board_window(remote_board, &json!({"success": true})); }+ if iso.hwnd.is_none() { iso.hwnd = self.wait_board_window(remote_board, &json!({"success": true})).map(|w| w.0); } if let (true, Some(h)) = (raise, iso.hwnd) { self.call("desktop_ui_window", &json!({"hwnd": h, "action": "maximize", "reason": "aiflow capture: fill the monitor"})); self.call("desktop_bring_to_front", &json!({"hwnd": h, "reason": "aiflow capture: keep the PCB editor on screen on the test box"}));@@ -1415,6 +1425,37 @@ pub fn opened_without_handle(reply: &Value) -> bool { || reply["error"].as_str().is_some_and(|e| e.contains("BACKGROUND"))) } +/// Said once when the editor was found off screen (issue #253): a window that is not on screen films nothing on+/// macOS (compare #123).+pub const OFFSCREEN_NOTE: &str = "; the editor is open off screen (behind a full-screen app, on another desktop or Space): this step's clip films nothing until the person leaves the full-screen app or brings it on screen";++/// The pid a `kicad_open_board` reply names for the editor it launched.+pub fn open_pid(reply: &Value) -> Option<i64> {+ reply["pid"].as_i64().or_else(|| reply["data"]["pid"].as_i64()).filter(|p| *p > 0)+}++/// Issue #253: what an open that found no window is judged by, instead of the bridge's reply alone (whose first+/// characters are its `_foregroundHint`). A refusal that is not a background open is passed on as it is.+pub fn not_found(remote_board: &str, reply: Value) -> Value {+ if !opened_without_handle(&reply) { return reply; }+ let pid = open_pid(&reply).map(|p| format!(" (pid {p})")).unwrap_or_default();+ json!({"error": format!("the bridge opened the board in the background{pid} but gave no window handle, and no window naming {} appeared within 60 s, on screen or off. Is KiCad showing a dialog? Look at the desktop, close the dialog, then run capture open again (it reuses the window)", board_stem(remote_board)), "reply": reply})+}++/// Issue #253: the board's editor among the desktop's windows listed with `includeOffscreen`: a window whose title+/// names the board and that is the PCB editor (its title, pcbnew, or the pid the open launched). (hwnd, on screen)+pub fn offscreen_board_window_in(windows: &Value, stem: &str, pid: Option<i64>) -> Option<(i64, bool)> {+ if stem.is_empty() { return None; }+ let out = windows.get("output").and_then(|o| o.as_str()).and_then(|t| serde_json::from_str::<Value>(t).ok()).unwrap_or_else(|| windows.clone());+ let list = out["data"]["windows"].as_array().or(out["windows"].as_array()).cloned().unwrap_or_default();+ list.iter().find(|w| {+ let t = w["title"].as_str().unwrap_or("");+ let app = [&w["image"], &w["process"], &w["appBundleId"]].iter().filter_map(|v| v.as_str()).collect::<Vec<_>>().join(" ").to_ascii_lowercase();+ let ours = pid.is_some_and(|p| w["ownerPid"].as_i64() == Some(p) || w["pid"].as_i64() == Some(p));+ crate::eda::names_board(t, stem) && (t.contains("PCB Editor") || app.contains("pcbnew") || ours)+ }).and_then(|w| w["hwnd"].as_i64().filter(|h| *h > 0).map(|h| (h, w["isOnscreen"] != false)))+}+ /// The PCB editor window naming `stem`: KiCad's editor list first (kind pcb), else a desktop window titled /// "... PCB Editor" or of pcbnew that names the board. pub fn board_window_in(status: &Value, windows: &Value, stem: &str) -> Option<i64> {@@ -1436,6 +1477,29 @@ pub fn board_window_in(status: &Value, windows: &Value, stem: &str) -> Option<i6 mod board_window_tests { use super::*; + /// Issue #253 (Drew Owens): macOS, the editor opened behind a full-screen app is on another Space: found among+ /// the off-screen windows (by its title, or the pid the open launched), and said to be off screen.+ #[test]+ fn an_editor_behind_a_full_screen_app_is_found_off_screen() {+ let open = json!({"success": true, "status": "ok", "openedInBackground": true, "windowAppeared": false, "pid": 29294, "_foregroundHint": "Window opened in the BACKGROUND - ..."});+ assert_eq!(open_pid(&open), Some(29294));+ let onscreen = json!({"status": "ok", "output": "{\"data\":{\"windows\":[{\"appBundleId\":\"com.example.FullScreenApp\",\"hwnd\":5530,\"isOnscreen\":true}]},\"includeOffscreen\":false}"});+ assert_eq!(board_window_in(&json!({"success": true}), &onscreen, "af114"), None, "the on-screen list has no editor");+ let all = json!({"status": "ok", "output": "{\"data\":{\"windows\":[{\"appBundleId\":\"com.example.FullScreenApp\",\"hwnd\":5530,\"isOnscreen\":true},{\"appBundleId\":\"org.kicad.pcbnew\",\"ownerPid\":29294,\"hwnd\":18626,\"isOnscreen\":false,\"title\":\"af114 \\u2014 PCB Editor\"}]}}"});+ assert_eq!(offscreen_board_window_in(&all, "af114", open_pid(&open)), Some((18626, false)));+ assert_eq!(offscreen_board_window_in(&all, "af114", None), Some((18626, false)), "its title names it too");+ assert_eq!(offscreen_board_window_in(&all, "af115", Some(29294)), None, "another board's editor is not this one");+ // a window naming the board that is not the PCB editor (a file browser) is not taken+ let other = json!({"data": {"windows": [{"hwnd": 7, "title": "af114 folder", "appBundleId": "com.apple.finder", "isOnscreen": false}]}});+ assert_eq!(offscreen_board_window_in(&other, "af114", Some(29294)), None);+ // the error says what was judged, not the bridge's hint+ let e = not_found("/tmp/aiflow-test/af114/af114.kicad_pcb", open.clone());+ assert!(e.to_string().starts_with("{\"error\":\"the bridge opened the board in the background (pid 29294) but gave no window handle, and no window naming af114"), "{e}");+ let refused = json!({"success": false, "error": "file not found"});+ assert_eq!(not_found("x.kicad_pcb", refused.clone()), refused);+ assert!(OFFSCREEN_NOTE.contains("off screen"));+ }+ /// Issue #222: a board opened in the background is found by its window; a retry reuses it. #[test] fn the_board_window_is_found_and_a_background_open_is_not_a_failure() {
docs/release-0.2.116.mdadded+11@@ -0,0 +1,11 @@+# adom-aiflow 0.2.116++## capture open on macOS: an editor behind a full-screen app (issue #253, Drew Owens)++In hands-off mode on a Mac, KiCad opens behind the person's full-screen app, on another Space. The bridge's open answered with a pid but no window handle. `kicad_status` lists no editors on macOS, and the desktop's window list holds on-screen windows only. So `capture open` polled for a minute, never found the editor and stopped with "could not open the board", even though the board was open and its IPC worked.++- When the editor is not found on screen, `capture open` also looks among the off-screen windows (`desktop_list_windows {includeOffscreen: true}`). It takes the window whose title names the board and that is the PCB editor (its title, pcbnew, or the pid the open launched). A retry finds and reuses that window, never a second pcbnew.+- When the editor is off screen, `capture open` says so once: "the editor is open off screen (behind a full-screen app, on another desktop or Space): this step's clip films nothing until the person leaves the full-screen app or brings it on screen".+- When no window turns up at all, the error says what was judged instead of quoting the bridge's hint: "the bridge opened the board in the background (pid N) but gave no window handle, and no window naming <board> appeared within 60 s, on screen or off".++Windows is unchanged: KiCad's editor list and the window list find the editor there as before.
package.json+1−1@@ -1,7 +1,7 @@ { "slug": "adom-aiflow", "type": "app",- "version": "0.2.115",+ "version": "0.2.116", "title": "AI Flow", "description": "Adom's AI Flow: a tool to help the AI follow all of the steps it takes to build a board. One Rust binary with a crate per step (placement helpers, a grid router with Kelvin taps, pours with keepouts, KiCad's DRC gate, live landing through the KiCad Bridge, copper measurement, current and thermal analysis) and a finish line that refuses an unfinished board. Every command answers with hints for the AI; every turn, its thinking time and every rework loop go into run.jsonl, so Claude, Codex and any other engine are compared on the same flow. KiCad, Fusion 360 and Altium Designer today (the same steps and gates through each tool's bridge); Adom's own web apps next.", "summary": "Adom's AI Flow: a tool to help the AI follow all of the steps it takes to build a board. The AI thinks its way from placement through routing, pours, current and thermal analysis to a delivered video; the binary does the fast, deterministic parts of every step, hands back hints, and keeps a ledger of every turn, every return to an earlier step, and the clock from the prompt to done.",
page.json+1−1@@ -1,7 +1,7 @@ { "slug": "adom-aiflow", "type": "app",- "version": "0.2.115",+ "version": "0.2.116", "title": "AI Flow", "description": "Adom's AI Flow: a tool to help the AI follow all of the steps it takes to build a board. One Rust binary with a crate per step (placement helpers, a grid router with Kelvin taps, pours with keepouts, KiCad's DRC gate, live landing through the KiCad Bridge, copper measurement, current and thermal analysis) and a finish line that refuses an unfinished board. Every command answers with hints for the AI; every turn, its thinking time and every rework loop go into run.jsonl, so Claude, Codex and any other engine are compared on the same flow. KiCad, Fusion 360 and Altium Designer today (the same steps and gates through each tool's bridge); Adom's own web apps next.", "summary": "Adom's AI Flow: a tool to help the AI follow all of the steps it takes to build a board. The AI thinks its way from placement through routing, pours, current and thermal analysis to a delivered video; the binary does the fast, deterministic parts of every step, hands back hints, and keeps a ledger of every turn, every return to an earlier step, and the clock from the prompt to done.",