fix(native-ui): handle incomplete accessibility child collections (#944)

* fix(native-ui): align verifier with scenario contract

* perf(native-ui): reuse accessibility child counts

* fix(native-ui): handle incomplete child collections
This commit is contained in:
morluto
2026-10-07 19:52:16 +08:00
committed by GitHub
parent 348c65c7ec
commit ac3bae3cb9
6 changed files with 71 additions and 12 deletions
+11
View File
@@ -0,0 +1,11 @@
struct ChildBatch<Element> {
let values: [Element]
let complete: Bool
}
func captureChildValues<Element>(requestedCount: Int, copy: () -> [Element]?) -> ChildBatch<Element> {
guard requestedCount > 0 else { return ChildBatch(values: [], complete: true) }
let returned = copy() ?? []
let values = Array(returned.prefix(requestedCount))
return ChildBatch(values: values, complete: values.count == requestedCount)
}
+11 -7
View File
@@ -26,11 +26,12 @@ func childCount(_ element: AXUIElement) -> Int {
var count = 0
return AXUIElementGetAttributeValueCount(element, kAXChildrenAttribute as CFString, &count) == .success ? count : 0
}
func children(_ element: AXUIElement, limit: Int) -> [AXUIElement] {
var value: CFArray?
let count = min(limit, childCount(element))
if count == 0 { return [] }
return AXUIElementCopyAttributeValues(element, kAXChildrenAttribute as CFString, 0, count, &value) == .success ? value as? [AXUIElement] ?? [] : []
func children(_ element: AXUIElement, count: Int) -> ChildBatch<AXUIElement> {
captureChildValues(requestedCount: count) {
var value: CFArray?
guard AXUIElementCopyAttributeValues(element, kAXChildrenAttribute as CFString, 0, count, &value) == .success else { return nil }
return value as? [AXUIElement]
}
}
func text(_ element: AXUIElement, _ key: String) -> Any { (attribute(element, key) as? String) ?? NSNull() as Any }
func windowBounds(_ element: AXUIElement) -> CGRect? {
@@ -115,9 +116,12 @@ func observe(_ request: Request) async throws -> [String: Any] {
if path.count >= 32 { if totalChildren > 0 { truncated = true }; continue }
let available = max(0, request.max_nodes - nodes.count - pending.count)
let count = min(available, totalChildren)
let items = children(element, limit: count)
let batch = children(element, count: count)
if !batch.complete { truncated = true }
if totalChildren > count { truncated = true }
for index in (0..<count).reversed() { pending.append((items[index], path + [index])) }
for (index, item) in batch.values.enumerated().reversed() {
pending.append((item, path + [index]))
}
}
}
var screenshot: Any = NSNull()
+1
View File
@@ -42,6 +42,7 @@
"dist",
"install.sh",
"bridge/hopper_bridge.py",
"bridge/native/NativeUIChildren.swift",
"bridge/native/ReaNativeUI.swift",
"bridge/ghidra/ReaGhidraBridge.java",
"bridge/ghidra/ReaGhidraExtensions.java",
+17 -3
View File
@@ -15,6 +15,23 @@ const root = await mkdtemp(join(tmpdir(), "rea-ui-fixture-"));
let child;
let fixturePid;
try {
const childRetrievalTest = join(root, "child-retrieval-test");
await promisify(execFile)("/usr/bin/xcrun", [
"swiftc",
fileURLToPath(
new URL("../bridge/native/NativeUIChildren.swift", import.meta.url),
),
fileURLToPath(
new URL(
"../tests/conformance/native/native-ui-children/main.swift",
import.meta.url,
),
),
"-o",
childRetrievalTest,
]);
await promisify(execFile)(childRetrievalTest, []);
const contents = join(root, "Fixture.app", "Contents");
await mkdir(join(contents, "MacOS"), { recursive: true });
await writeFile(
@@ -82,7 +99,6 @@ try {
"capture_native_ui_scenario",
{
...scope,
restore: "leave-as-is",
screenshot: false,
steps: [
{ kind: "click", path: button.path },
@@ -111,8 +127,6 @@ try {
String(scope.pid),
"--window-id",
String(scope.window_id),
"--restore",
"leave-as-is",
"--steps",
JSON.stringify([
{ kind: "click", path: button.path },
+13 -2
View File
@@ -1,4 +1,4 @@
import { mkdtemp, rm } from "node:fs/promises";
import { mkdtemp, rm, symlink } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
@@ -14,6 +14,13 @@ export const createNativeUiHelperRuntime = () => {
if (executable === undefined) {
root = await mkdtemp(join(tmpdir(), "rea-native-ui-"));
const output = join(root, "observer");
const main = join(root, "main.swift");
await symlink(
fileURLToPath(
new URL("../../bridge/native/ReaNativeUI.swift", import.meta.url),
),
main,
);
await execFileOutput(
"/usr/bin/xcrun",
[
@@ -21,8 +28,12 @@ export const createNativeUiHelperRuntime = () => {
"-module-cache-path",
join(root, "modules"),
fileURLToPath(
new URL("../../bridge/native/ReaNativeUI.swift", import.meta.url),
new URL(
"../../bridge/native/NativeUIChildren.swift",
import.meta.url,
),
),
main,
"-o",
output,
],
@@ -0,0 +1,18 @@
let failed: ChildBatch<Int> = captureChildValues(requestedCount: 2) { nil }
precondition(failed.values.isEmpty, "AX retrieval failure must return no values")
precondition(!failed.complete, "AX retrieval failure must mark traversal incomplete")
let short = captureChildValues(requestedCount: 3) { [10, 20] }
precondition(short.values == [10, 20], "Short retrieval must preserve returned children")
precondition(!short.complete, "Short retrieval must mark traversal incomplete")
var visited: [Int] = []
for (index, value) in short.values.enumerated() {
visited.append(index * 10 + value)
}
precondition(visited == [10, 30], "Traversal must use actual returned indices")
let complete = captureChildValues(requestedCount: 2) { [10, 20, 30] }
precondition(complete.values == [10, 20], "Retrieval must respect the requested node budget")
precondition(complete.complete, "A full bounded result must remain complete")
print("Native UI child retrieval seam passed")