Skip to content

Open source CapCut alternative.

TypeScript

feat(app): capture and cache live AX element handles

David Le2bc85e94b4c2
diff --git a/apps/Donkey/Sources/DonkeyRuntime/AccessibilityObserver.swift b/apps/Donkey/Sources/DonkeyRuntime/AccessibilityObserver.swift
index 7228a1f5..14e1d723 100644
--- a/apps/Donkey/Sources/DonkeyRuntime/AccessibilityObserver.swift
+++ b/apps/Donkey/Sources/DonkeyRuntime/AccessibilityObserver.swift
@@ -23,12 +23,18 @@ public enum AccessibilityObserver {
return nil
}
let limits = MacAccessibilitySnapshotLimits(maxDepth: 8, maxChildrenPerNode: 120, maxTotalNodes: 1_200)
+ // Retain the live AXUIElement for each node so a later action resolves against the exact control
+ // we observed (identity-stable across list reordering) instead of re-walking by node index.
+ var handles: [String: AXUIElement] = [:]
guard let tree = try? ApplicationServicesMacAccessibilitySnapshotCapturer().captureTree(
target: target,
- limits: limits
+ limits: limits,
+ onNode: { _ in },
+ onLiveElement: { nodeID, element in handles[nodeID] = element }
) else {
return nil
}
+ MacAccessibilityElementHandleCache.shared.replace(processID: target.processID, handles: handles)
let snapshot = MacAccessibilitySnapshot(
target: target,
limits: limits,
diff --git a/apps/Donkey/Sources/DonkeyRuntime/MacAccessibilityElementHandleCache.swift b/apps/Donkey/Sources/DonkeyRuntime/MacAccessibilityElementHandleCache.swift
new file mode 100644
index 00000000..a51f3f83
--- /dev/null
+++ b/apps/Donkey/Sources/DonkeyRuntime/MacAccessibilityElementHandleCache.swift
@@ -0,0 +1,43 @@
+@preconcurrency import ApplicationServices
+import Foundation
+
+/// MainActor-confined cache of the live `AXUIElement` handles captured during an observe, keyed by
+/// process. A handle keeps pointing at the same logical control even after siblings are inserted,
+/// removed, or reordered — and errors cleanly once the control is destroyed — so resolving an action
+/// against the held handle is far more robust than re-walking the tree by positional node index.
+///
+/// The cache is replaced wholesale for a process on each observe; that replacement *is* the
+/// invalidation. Handles live only until the next observe of the same process drops them (bounded by
+/// the snapshot's node limit), so there is nothing to expire or evict by hand.
+///
+/// `AXUIElement` is a `CFType` and not `Sendable`; all access is confined to the main actor. Callers
+/// off the main thread (the action backend runs inside a timeout on a background queue) reach the
+/// cache through a `DispatchQueue.main.sync` hop, the same pattern the backend already uses to read
+/// the frontmost application.
+@MainActor
+final class MacAccessibilityElementHandleCache {
+ static let shared = MacAccessibilityElementHandleCache()
+
+ private var handlesByProcess: [pid_t: [String: AXUIElement]] = [:]
+
+ /// Replaces every handle held for `processID` with the freshly captured set, releasing the prior
+ /// snapshot's handles.
+ func replace(processID: pid_t, handles: [String: AXUIElement]) {
+ if handles.isEmpty {
+ handlesByProcess.removeValue(forKey: processID)
+ } else {
+ handlesByProcess[processID] = handles
+ }
+ }
+
+ /// The live handle captured for `nodeID` in the latest observe of `processID`, or nil if there is
+ /// no current snapshot for that process or it didn't contain the node.
+ func handle(processID: pid_t, nodeID: String) -> AXUIElement? {
+ handlesByProcess[processID]?[nodeID]
+ }
+
+ /// Drops all handles for a process (e.g. when it exits). Tests also use this to reset state.
+ func clear(processID: pid_t) {
+ handlesByProcess.removeValue(forKey: processID)
+ }
+}
diff --git a/apps/Donkey/Sources/DonkeyRuntime/MacAccessibilitySnapshotCaptureService.swift b/apps/Donkey/Sources/DonkeyRuntime/MacAccessibilitySnapshotCaptureService.swift
index 2c41b7c8..12d93b93 100644
--- a/apps/Donkey/Sources/DonkeyRuntime/MacAccessibilitySnapshotCaptureService.swift
+++ b/apps/Donkey/Sources/DonkeyRuntime/MacAccessibilitySnapshotCaptureService.swift
@@ -370,6 +370,19 @@ final class ApplicationServicesMacAccessibilitySnapshotCapturer: MacAccessibilit
target: MacWindowTargetCandidate,
limits: MacAccessibilitySnapshotLimits,
onNode: (MacAccessibilitySnapshotNode) -> Void
+ ) throws -> MacAccessibilitySnapshotTree {
+ try captureTree(target: target, limits: limits, onNode: onNode, onLiveElement: { _, _ in })
+ }
+
+ /// Like `captureTree(target:limits:onNode:)`, but also hands each visited node's live `AXUIElement`
+ /// (paired with its nodeID) to `onLiveElement` so a caller can retain handles for later, identity-
+ /// stable action resolution. Not part of the capture protocol — only the concrete capturer and the
+ /// observe path use it, so the overlay/inspection callers and their test fakes are untouched.
+ func captureTree(
+ target: MacWindowTargetCandidate,
+ limits: MacAccessibilitySnapshotLimits,
+ onNode: (MacAccessibilitySnapshotNode) -> Void,
+ onLiveElement: (String, AXUIElement) -> Void
) throws -> MacAccessibilitySnapshotTree {
let application = AXUIElementCreateApplication(target.processID)
let deadlineUptime = ProcessInfo.processInfo.systemUptime
@@ -388,7 +401,8 @@ final class ApplicationServicesMacAccessibilitySnapshotCapturer: MacAccessibilit
deadlineUptime: deadlineUptime,
remainingNodeCount: &remainingNodeCount,
isTreeTruncated: &isTreeTruncated,
- onNode: onNode
+ onNode: onNode,
+ onLiveElement: onLiveElement
) ?? RawMacAccessibilitySnapshotNode(
role: "AXUnknown",
title: target.title,
@@ -455,7 +469,8 @@ final class ApplicationServicesMacAccessibilitySnapshotCapturer: MacAccessibilit
deadlineUptime: TimeInterval,
remainingNodeCount: inout Int,
isTreeTruncated: inout Bool,
- onNode: (MacAccessibilitySnapshotNode) -> Void
+ onNode: (MacAccessibilitySnapshotNode) -> Void,
+ onLiveElement: (String, AXUIElement) -> Void
) -> RawMacAccessibilitySnapshotNode? {
guard !hasTimedOut(deadlineUptime) else {
isTreeTruncated = true
@@ -492,6 +507,7 @@ final class ApplicationServicesMacAccessibilitySnapshotCapturer: MacAccessibilit
}
)
)
+ onLiveElement(path, element)
let children = childNodes(
for: element,
@@ -501,7 +517,8 @@ final class ApplicationServicesMacAccessibilitySnapshotCapturer: MacAccessibilit
deadlineUptime: deadlineUptime,
remainingNodeCount: &remainingNodeCount,
isTreeTruncated: &isTreeTruncated,
- onNode: onNode
+ onNode: onNode,
+ onLiveElement: onLiveElement
)
return RawMacAccessibilitySnapshotNode(
@@ -525,7 +542,8 @@ final class ApplicationServicesMacAccessibilitySnapshotCapturer: MacAccessibilit
deadlineUptime: TimeInterval,
remainingNodeCount: inout Int,
isTreeTruncated: inout Bool,
- onNode: (MacAccessibilitySnapshotNode) -> Void
+ onNode: (MacAccessibilitySnapshotNode) -> Void,
+ onLiveElement: (String, AXUIElement) -> Void
) -> [RawMacAccessibilitySnapshotNode] {
guard !hasTimedOut(deadlineUptime) else {
isTreeTruncated = true
@@ -563,7 +581,8 @@ final class ApplicationServicesMacAccessibilitySnapshotCapturer: MacAccessibilit
deadlineUptime: deadlineUptime,
remainingNodeCount: &remainingNodeCount,
isTreeTruncated: &isTreeTruncated,
- onNode: onNode
+ onNode: onNode,
+ onLiveElement: onLiveElement
)
}
}