From 9e04b9db20e4cd1b3802e99658e1bdec36b54dfe Mon Sep 17 00:00:00 2001 From: Ashlee Radka Date: Mon, 23 Feb 2026 18:51:41 -0500 Subject: [PATCH] fix: replace AX force-casts with safe casts Co-Authored-By: Claude --- .../vellum-assistant/Ambient/AmbientAXCapture.swift | 6 +++--- .../ComputerUse/AccessibilityTree.swift | 13 ++++++------- .../Features/Voice/DictationContextCapture.swift | 4 ++-- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/clients/macos/vellum-assistant/Ambient/AmbientAXCapture.swift b/clients/macos/vellum-assistant/Ambient/AmbientAXCapture.swift index 210ae27a3fe..431b734501f 100644 --- a/clients/macos/vellum-assistant/Ambient/AmbientAXCapture.swift +++ b/clients/macos/vellum-assistant/Ambient/AmbientAXCapture.swift @@ -71,15 +71,15 @@ enum AmbientAXCapture { log.debug("No focused window for \(appName, privacy: .public)") return nil } - let windowElement = windowRef as! AXUIElement + guard let windowElement = windowRef as? AXUIElement else { return nil } let windowTitle = getStringAttribute(windowElement, kAXTitleAttribute as CFString) ?? "Untitled" // Get focused element var focusedRef: CFTypeRef? var focusedElement: (role: String, label: String?)? if AXUIElementCopyAttributeValue(appElement, kAXFocusedUIElementAttribute as CFString, &focusedRef) == .success, - let focused = focusedRef { - let fe = focused as! AXUIElement + let focused = focusedRef, + let fe = focused as? AXUIElement { let role = getStringAttribute(fe, kAXRoleAttribute as CFString) ?? "unknown" let label = getStringAttribute(fe, kAXTitleAttribute as CFString) ?? getStringAttribute(fe, kAXDescriptionAttribute as CFString) diff --git a/clients/macos/vellum-assistant/ComputerUse/AccessibilityTree.swift b/clients/macos/vellum-assistant/ComputerUse/AccessibilityTree.swift index 722d2031e6b..92cc13b09d4 100644 --- a/clients/macos/vellum-assistant/ComputerUse/AccessibilityTree.swift +++ b/clients/macos/vellum-assistant/ComputerUse/AccessibilityTree.swift @@ -140,7 +140,7 @@ final class AccessibilityTreeEnumerator: AccessibilityTreeProviding { } return nil } - let windowElement = windowRef as! AXUIElement + guard let windowElement = windowRef as? AXUIElement else { return nil } let windowTitle = getStringAttribute(windowElement, kAXTitleAttribute as CFString) ?? "Untitled" @@ -207,7 +207,7 @@ final class AccessibilityTreeEnumerator: AccessibilityTreeProviding { log.debug("enumerateAppByPID: no focused window for \(appName, privacy: .public) (pid \(pid)): AXError \(result.rawValue)") return nil } - let windowElement = windowRef as! AXUIElement + guard let windowElement = windowRef as? AXUIElement else { return nil } let windowTitle = getStringAttribute(windowElement, kAXTitleAttribute as CFString) ?? "Untitled" let appName = NSRunningApplication(processIdentifier: pid)?.localizedName ?? "Unknown" @@ -424,12 +424,11 @@ final class AccessibilityTreeEnumerator: AccessibilityTreeProviding { var point = CGPoint.zero var size = CGSize.zero - // Extract values from AXValue refs (force cast is safe here since we validated success above) - if let posRef = positionValue { - AXValueGetValue(posRef as! AXValue, .cgPoint, &point) + if let posRef = positionValue, let posVal = posRef as? AXValue { + AXValueGetValue(posVal, .cgPoint, &point) } - if let sizeRef = sizeValue { - AXValueGetValue(sizeRef as! AXValue, .cgSize, &size) + if let sizeRef = sizeValue, let sizeVal = sizeRef as? AXValue { + AXValueGetValue(sizeVal, .cgSize, &size) } return CGRect(origin: point, size: size) diff --git a/clients/macos/vellum-assistant/Features/Voice/DictationContextCapture.swift b/clients/macos/vellum-assistant/Features/Voice/DictationContextCapture.swift index d878a82ba54..0843c84ba77 100644 --- a/clients/macos/vellum-assistant/Features/Voice/DictationContextCapture.swift +++ b/clients/macos/vellum-assistant/Features/Voice/DictationContextCapture.swift @@ -88,7 +88,7 @@ struct DictationContextCapture { log.debug("Could not get focused window — AX permission may be missing") return "" } - let window = windowRef as! AXUIElement + guard let window = windowRef as? AXUIElement else { return "" } return axStringAttribute(window, kAXTitleAttribute as CFString) ?? "" } @@ -100,7 +100,7 @@ struct DictationContextCapture { log.debug("Could not get focused UI element") return (nil, false) } - let focused = focusedRef as! AXUIElement + guard let focused = focusedRef as? AXUIElement else { return (nil, false) } // Selected text let selectedText = axStringAttribute(focused, kAXSelectedTextAttribute as CFString)