← Commit history

Disk check: no PowerShell query to a macOS or Linux desktop

Drew Owens ·b0777e20c4 · ·parent 4bef5cb
The legacy free-space fallback runs only when adom-bridge targets reports the box as Windows, or reports no OS. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 file changed +57−10
crates/aiflow-bridge/src/lib.rs+57−10
@@ -82,14 +82,37 @@ pub fn disk_free_mb(reply: &Value) -> Option<u64> {     read(reply).or_else(|| read(&reply_fields(reply))).map(|b| b / 1_048_576) } +/// Whether the legacy PowerShell free-space query may go to a box of this OS (as `adom-bridge targets`+/// reports it): Windows, or a box whose OS is not reported (the old Windows Adom Bridge the fallback is+/// for). Never macOS or Linux: they have no PowerShell, and the query is a human-gated shell call on+/// the human's own machine for a figure the run only notes (a Mac's Adom Bridge without+/// desktop_disk_free was sent it on every step).+pub fn powershell_free_ok(os: Option<&str>) -> bool {+    match os.map(|o| o.trim().to_ascii_lowercase()) {+        None => true,+        Some(o) if o.is_empty() => true,+        Some(o) => o.starts_with("win"),+    }+}++/// The OS `adom-bridge targets` reports for a target (by its name or hostname), lower case.+pub fn target_os_of(targets: &Value, target: &str) -> Option<String> {+    let t = target.trim().to_ascii_lowercase();+    targets["targets"].as_array()?.iter()+        .find(|x| [&x["name"], &x["hostname"]].iter().any(|v| v.as_str().is_some_and(|s| s.trim().to_ascii_lowercase() == t)))+        .and_then(|x| x["os"].as_str()).map(|o| o.trim().to_ascii_lowercase()).filter(|o| !o.is_empty())+}+ /// The recording drive's free space through the native verb. LEGACY FALLBACK: only when the box's /// Adom Bridge is older than 2.1.127 and answers `unknown_verb` does this ask the drive through a-/// human-gated `shell_execute` PowerShell query (a refusal or no answer gives None, and the disk-/// check falls back to the run drive). Remove the fallback once no supported box runs ab < 2.1.127.-fn remote_free_mb_with(mut call: impl FnMut(&str, &Value) -> Value) -> Option<u64> {+/// human-gated `shell_execute` PowerShell query, and only on a Windows box (`os` is asked only then;+/// a refusal or no answer gives None, and the disk check falls back to the run drive). Remove the+/// fallback once no supported box runs ab < 2.1.127.+fn remote_free_mb_with(mut call: impl FnMut(&str, &Value) -> Value, os: impl FnOnce() -> Option<String>) -> Option<u64> {     let r = call("desktop_disk_free", &json!({}));     if let Some(mb) = disk_free_mb(&r) { return Some(mb); }     if r["errorCode"].as_str() != Some("unknown_verb") { return None; }+    if !powershell_free_ok(os().as_deref()) { return None; }     let r = call("shell_execute", &json!({"command": "powershell -NoProfile -Command \"(Get-PSDrive -Name ($env:TEMP.Substring(0,1))).Free\"", "timeoutSeconds": 20, "reason": "aiflow checks the free space on the recording drive before it starts a step's clip (legacy Adom Bridge without desktop_disk_free)"}));     let out = r.get("stdout").or_else(|| r.get("output")).and_then(|v| v.as_str()).unwrap_or("");     out.trim().lines().last().and_then(|l| l.trim().parse::<u64>().ok()).map(|b| b / 1_048_576)@@ -424,6 +447,12 @@ impl Bridge {         serde_json::from_str(&text).unwrap_or_else(|_| json!({"status": "parse_error", "raw": text.chars().take(400).collect::<String>()}))     } +    /// The target's OS as `adom-bridge targets` reports it (lower case), or None when it is not listed.+    pub fn target_os(&self) -> Option<String> {+        let out = Command::new("adom-bridge").args(["targets", "--ai-thread", &self.ai_thread]).output().ok()?;+        target_os_of(&serde_json::from_slice(&out.stdout).ok()?, &self.target)+    }+     /// One adom-bridge verb, as is.     pub fn raw_call(&self, verb: &str, args: &Value) -> Value {         let args = socket_args(verb, args);@@ -742,7 +771,7 @@ impl Bridge {     /// Free megabytes on the test box's recording drive: Adom Bridge's native, read-only,     /// ungated `desktop_disk_free` (ab 2.1.127+), whose default path is the recorder's output folder.     pub fn remote_free_mb(&self) -> Option<u64> {-        remote_free_mb_with(|verb, args| self.raw_call(verb, args))+        remote_free_mb_with(|verb, args| self.raw_call(verb, args), || self.target_os())     }      /// Filled copper per layer. A refill of many zones can outlast the bridge's IPC wait@@ -968,22 +997,40 @@ mod native_preflight_tests {         assert_eq!(super::disk_free_mb(&reply), Some(5584));         assert_eq!(super::disk_free_mb(&json!({"output":"{\"freeBytes\":2097152}"})), Some(2));         let mut verbs = Vec::new();-        let mb = super::remote_free_mb_with(|v, _| { verbs.push(v.to_string()); reply.clone() });+        let mb = super::remote_free_mb_with(|v, _| { verbs.push(v.to_string()); reply.clone() }, || panic!("the OS is asked only for the legacy fallback"));         assert_eq!((mb, verbs), (Some(5584), vec!["desktop_disk_free".to_string()]));     }      #[test]     fn shell_fallback_only_on_a_bridge_without_the_verb() {-        let mut verbs = Vec::new();-        let mb = super::remote_free_mb_with(|v, _| { verbs.push(v.to_string()); if v == "desktop_disk_free" { json!({"errorCode":"unknown_verb"}) } else { json!({"stdout":"3145728\r\n"}) } });-        assert_eq!((mb, verbs.len()), (Some(3), 2));-        assert_eq!(verbs[1], "shell_execute");+        for os in [None, Some("windows"), Some("Windows 11")] {+            let mut verbs = Vec::new();+            let mb = super::remote_free_mb_with(|v, _| { verbs.push(v.to_string()); if v == "desktop_disk_free" { json!({"errorCode":"unknown_verb"}) } else { json!({"stdout":"3145728\r\n"}) } }, || os.map(str::to_string));+            assert_eq!((mb, verbs.len()), (Some(3), 2), "{os:?}");+            assert_eq!(verbs[1], "shell_execute");+        }         // any other refusal of the native verb is not a reason to shell out         let mut verbs = Vec::new();-        let mb = super::remote_free_mb_with(|v, _| { verbs.push(v.to_string()); json!({"errorCode":"target_not_found"}) });+        let mb = super::remote_free_mb_with(|v, _| { verbs.push(v.to_string()); json!({"errorCode":"target_not_found"}) }, || None);         assert_eq!((mb, verbs.len()), (None, 1));     } +    #[test]+    fn no_powershell_to_a_mac_or_linux_box() {+        // a macOS Adom Bridge has no desktop_disk_free: the PowerShell query cannot run there, and it is a+        // human-gated shell call on the human's own machine+        for os in ["macos", "darwin", "Mac OS", "linux"] {+            let mut verbs = Vec::new();+            let mb = super::remote_free_mb_with(|v, _| { verbs.push(v.to_string()); json!({"errorCode":"unknown_verb"}) }, || Some(os.to_string()));+            assert_eq!((mb, verbs.clone()), (None, vec!["desktop_disk_free".to_string()]), "{os}");+        }+        let targets = json!({"targets": [{"name": "Shop PC", "hostname": "SHOP-PC", "os": "windows"}, {"name": "Laptop", "hostname": "Some-MacBook-Air", "os": "macos"}]});+        assert_eq!(super::target_os_of(&targets, "laptop").as_deref(), Some("macos"));+        assert_eq!(super::target_os_of(&targets, "SHOP-PC").as_deref(), Some("windows"));+        assert_eq!(super::target_os_of(&targets, "elsewhere"), None);+        assert_eq!(super::target_os_of(&json!({"targets": [{"name": "x"}]}), "x"), None);+    }+     #[test]     fn part_splitter_is_legacy_only() {         assert!(super::legacy_pull_needs_split(Some("2.1.174")));