Craft Anything: understand how any software works, and craft your own.
Rust
Report a denied macOS Accessibility permission as an error (#887)
mavericzzba91c9fbf90b
diff --git a/crates/engine/src/tools/apple_runtime.rs b/crates/engine/src/tools/apple_runtime.rsindex d70613cb..6761eb8f 100644--- a/crates/engine/src/tools/apple_runtime.rs+++ b/crates/engine/src/tools/apple_runtime.rs@@ -416,6 +416,22 @@ mod macos {Ok(bin)}+ /// Turn the helper's own error object into a tool error. The helper exits 0 after reporting a+ /// refusal (such as a denied Accessibility permission), so an unchecked `nodes: []` would look+ /// like a successful read of an empty tree (#240).+ pub(super) fn helper_error(result: &Value) -> Option<ToolError> {+ let message = result.get("error")?.as_str()?;+ let reason = result.get("reason").and_then(Value::as_str).unwrap_or("helper_error");+ let error = if reason == "accessibility_not_granted" {+ ToolError::new(ErrorCode::NoPermission, "The app's screen was not read: this app does not have Accessibility permission.".to_string())+ .with_remediation("Allow the app running Craft Anything in System Settings → Privacy & Security → Accessibility, then run this again.")+ } else {+ ToolError::new(ErrorCode::Internal, format!("The accessibility helper refused the request: {message}"))+ .with_remediation("This is a bug in Craft Anything; report it with the details below.")+ };+ Some(error.with_details(json!({"reason": reason})))+ }+pub(super) fn run_helper(ctx: &mut Ctx<'_>, request: Value, duration_ms: u64) -> Result<Value, ToolError> {let root = private_root()?;let helper = build_helper(&root).inspect_err(|_| {@@ -431,7 +447,12 @@ mod macos {.run_checked();let _ = std::fs::remove_dir_all(&root);let out = out?;- serde_json::from_slice(&out.stdout).map_err(|e| ToolError::new(ErrorCode::GarbledOutput, format!("accessibility helper output was not JSON: {e}")))+ let result: Value = serde_json::from_slice(&out.stdout)+ .map_err(|e| ToolError::new(ErrorCode::GarbledOutput, format!("accessibility helper output was not JSON: {e}")))?;+ match helper_error(&result) {+ Some(error) => Err(error),+ None => Ok(result),+ }}pub fn runtime_ui_read(ctx: &mut Ctx<'_>, p: ObserveUiParams) -> Result<ToolOutput, ToolError> {@@ -705,6 +726,29 @@ mod tests {assert_eq!(err.code, craft_anything_evidence::ErrorCode::ConsentRequired);}+ /// #240: the helper answers a denied Accessibility permission with `{"error": …}` and exits 0,+ /// so the empty `nodes` list must not be reported as a successful read.+ #[cfg(target_os = "macos")]+ #[test]+ fn a_denied_accessibility_permission_is_an_error_not_an_empty_tree() {+ use craft_anything_evidence::ErrorCode;+ let denied = json!({+ "error": "accessibility permission not granted",+ "reason": "accessibility_not_granted",+ "nodes": []+ });+ let err = super::macos::helper_error(&denied).expect("a denial is an error");+ assert_eq!(err.code, ErrorCode::NoPermission);+ assert_eq!(err.details.as_ref().unwrap()["reason"], "accessibility_not_granted");+ assert!(err.message.contains("was not read"), "{}", err.message);+ assert!(err.remediation.as_deref().is_some_and(|r| r.contains("Accessibility")), "{err:?}");+ // An empty tree the helper really read stays a successful result.+ assert!(super::macos::helper_error(&json!({"nodes": [], "truncated": false})).is_none());+ // Any other refusal is still an error, not an empty tree.+ let err = super::macos::helper_error(&json!({"error": "unknown command"})).expect("a refusal is an error");+ assert_eq!(err.code, ErrorCode::Internal);+ }+/// #251: the callback had a literal backslash and `n` between its Python statements, which lldb/// rejected before it launched anything; and only breakpoint 1 got it.#[test]diff --git a/crates/engine/src/tools/apple_runtime/native_ui_helper.swift b/crates/engine/src/tools/apple_runtime/native_ui_helper.swiftindex 5475429e..fd14ed08 100644--- a/crates/engine/src/tools/apple_runtime/native_ui_helper.swift+++ b/crates/engine/src/tools/apple_runtime/native_ui_helper.swift@@ -94,7 +94,8 @@ guard let req = try? JSONDecoder().decode(Request.self, from: input), let pid =}if !AXIsProcessTrusted() {- emit(["error": "accessibility permission not granted", "nodes": []])+ // A typed reason so the caller can report a permission problem, not an empty tree.+ emit(["error": "accessibility permission not granted", "reason": "accessibility_not_granted", "nodes": []])exit(0)}diff --git a/docs/ROADMAP.md b/docs/ROADMAP.mdindex 9d0f4744..44f4411c 100644--- a/docs/ROADMAP.md+++ b/docs/ROADMAP.md@@ -348,7 +348,8 @@ Owner: open · Status: todoOwner: open · Status: todoGolden rule 3: the result is evidence. All four tickets are confirmed in the code, with no PR yet.Each fix needs a Mac to test on.-- [ ] macOS: a denied Accessibility permission is reported as success with an empty tree (#240)+- [x] macOS: a denied Accessibility permission is reported as success with an empty tree (#240)+ — claimed: mavericzz, branch bugfix/r5.6-accessibility-denied; PR #887- [ ] macOS: `runtime_ui_read` ignores `screenshot` (#248) and `accessibility` (#307): the Swifthelper declares both and reads neither- [ ] macOS: call observation writes a literal `\n` into its lldb callback, so the Python isdiff --git a/docs/STATUS.md b/docs/STATUS.mdindex f2931530..49fa0cad 100644--- a/docs/STATUS.md+++ b/docs/STATUS.md@@ -17,6 +17,7 @@ workspace clippy, layers, fmt and the secret scan of all history were clean. Chi| Claude desktop app without Claude Code | When the `claude` command isn't found but the Claude desktop app is (Windows: the Store package's `claude-desktop` app alias or the older installer's `AnthropicClaude` folder; macOS: `Claude.app`), the builder dialog under "No coding agent was found" and `ai detect` (on stderr) say the desktop app is installed but building needs Claude Code, installed apart (#259). Typed as `DetectHint` from `AiHub::detect_hints` | A regression finds the app in temp folders laid out as each Windows installer leaves it, and nothing on Linux; a UI test shows the hint in the dialog. On Windows 10 the real Store package's alias was found. Not checked: macOS, the older installer, and a computer with no `claude` installed || Missing program paths | A program started by its path (a saved agent program, for one) that has no file there is reported with that path, as possibly moved by an update, with reason `path_missing`, instead of "isn't installed or isn't on your PATH"; a bare name still speaks of PATH. A relative path is looked up where the program starts (on Unix, its working folder), so a script there whose interpreter is missing isn't called a path with no file. It stays a missing program (`EngineMissing`), so builder turns aren't retried (#721). `ai test` names the agent program it ran and the saved one it replaced (`program`, `saved_program_replaced`), and `doctor` says a saved path has no program there | `a_saved_program_that_is_gone_is_named_by_test_and_doctor` runs both with a removed path. A regression starts a program at a path with no file on Windows 10 and checks the message, remediation and reason, with a bare-name control; on Linux (Docker), a relative script with a missing interpreter started in its folder, through `Process` and `Launch`. Not checked: a real Codex desktop update, or the app's Settings screen with a stale path || Mach-O Objective-C pointers | Chained offsets mapped only into file-empty `__PAGEZERO` reach the image-base fallback; pointer candidates must be backed by file bytes | Synthetic thin and fat Mach-O regression, plain-pointer and authenticated-rebase controls, null and unmapped-pointer checks. Real binaries were not checked for this correction |+| Runtime UI evidence | A denied macOS Accessibility permission is an error (`no_permission`, naming System Settings → Privacy & Security → Accessibility), not a successful read of an empty tree (#240). Any other refusal from the helper is also an error, never `nodes: []` | Unit regression maps the helper's refusal object, keeps a genuinely empty tree a success, and covers the unknown refusal; live `runtime_ui_read --pid 1` on macOS with the permission denied returned the structured error. The granted-permission path was not re-run for this correction || Apple app graphs | Selecting a macOS bundle recognises its root-relative `Contents/Info.plist` and retains its service/extension type; nested XPC bundles appear in `services` and as `xpc-service` graph nodes, including their executables when hosted in frameworks | Synthetic inventory tests, including parent-directory and flat bundle layouts, selected services/extensions, framework-hosted service executables, and bare-program controls. Ordinary selected directories with `Contents/Info.plist` need an Apple bundle suffix; archives retain layout-based recognition. Framework libraries and dylibs stay excluded from executables. Real app bundles were not checked for this correction || Chinese UI | Simplified (`zh-hans`) and Traditional (`zh-hant`) Chinese in Settings → General → Language; System chooses a supported OS language, otherwise English. Both catalogs cover UI controls and tool titles; translated titles are searchable. Raw tool data, AI text, logs, CLI/MCP and the separate browser viewer keep their original text. See [localisation](localization.md). | Catalog integrity and locale-resolution tests; `cargo xtask i18n-coverage`; isolated UI language-switch test. Platform visual checks are recorded separately. || Engine | 300 engine tools (320 with the AI and setup layers') across native, managed, Java, Android, Apple, JS/Electron, web, firmware, packets, databases, game archives and assets; see [tools.md](tools.md) and [tool-status.md](tool-status.md). A saved result can be narrowed without running again (`core_evidence_browse` keeps the rows matching given field values, cut to chosen fields) | A functional audit that ran every tool on real inputs on Windows, plus 600+ unit and integration tests |