👾 Open Computer Use – Open-Source Alternative to Codex Computer Use
Swift accessibilityai-agentai-agentsclaude-codecodexcodex-computer-usecomputer-usecomputer-use-agentcross-platformcuadesktop-automationgemini-cligui-automationmacosmcpmodel-context-protocolopen-computer-useopen-computer-use-mcpopenai-codexopencode
fix(linux): bound focus lookup and reuse its result (#102)
draplater5e1f189f86ee
diff --git a/apps/OpenComputerUseLinux/runtime.py b/apps/OpenComputerUseLinux/runtime.pyindex 0aee2cc..e4d2971 100644--- a/apps/OpenComputerUseLinux/runtime.py+++ b/apps/OpenComputerUseLinux/runtime.py@@ -385,50 +385,53 @@ def pixbuf_looks_black(pixbuf):return False-def focused_summary(app_pid, text_limit=DEFAULT_TEXT_LIMIT):- try:- root = desktop()- for app in iter_apps():- if node_pid(app) != app_pid:- continue- _, win = main_window(app)- focused = find_first(- win, lambda node: state_contains(node, Atspi.StateType.FOCUSED)- )- if focused is None:- return None- role = node_role(focused)- name = limit_text(node_name(focused), text_limit=text_limit)- return (role + " " + name).strip()- except Exception:+def focus_details(window, text_limit=DEFAULT_TEXT_LIMIT, max_nodes=MAX_ELEMENTS, max_depth=MAX_DEPTH):+ """Find focus once, within the same node/depth budget as the snapshot."""+ visited = 0+ truncated = False++ def visit(node, depth):+ nonlocal visited, truncated+ if visited >= max_nodes or depth > max_depth:+ truncated = True+ return None+ visited += 1+ if node is None:+ return None+ if state_contains(node, Atspi.StateType.FOCUSED):+ return node+ children = child_count(node)+ if depth >= max_depth:+ # Do not fetch children that cannot be visited. Otherwise a wide+ # node at the depth boundary can bypass the node budget entirely.+ truncated = truncated or children > 0+ return None+ for index in range(children):+ if visited >= max_nodes:+ truncated = True+ break+ found = visit(child_at(node, index), depth + 1)+ if found is not None:+ return foundreturn None--def selected_text(app_pid, text_limit=DEFAULT_TEXT_LIMIT):- try:- for app in iter_apps():- if node_pid(app) != app_pid:- continue- _, win = main_window(app)- focused = find_first(- win, lambda node: state_contains(node, Atspi.StateType.FOCUSED)- )- if focused is None or not supports_interface(focused, "Text"):- return None- text_iface = safe(focused.get_text_iface)- selections = safe(lambda: Atspi.Text.get_text_selections(text_iface), [])- if selections:- selection = selections[0]- end_offset = selection.end_offset- if text_limit is not None:- end_offset = min(end_offset, selection.start_offset + text_limit + 1)- value = Atspi.Text.get_text(- text_iface, selection.start_offset, end_offset- )- return limit_text(value, text_limit=text_limit)- except Exception:- return None- return None+ focused = visit(window, 0)+ if focused is None:+ return None, None, truncated+ summary = (node_role(focused) + " " + limit_text(node_name(focused), text_limit)).strip()+ selection = None+ if supports_interface(focused, "Text"):+ text_iface = safe(focused.get_text_iface)+ count = safe(lambda: Atspi.Text.get_n_selections(text_iface), 0)+ selected = safe(lambda: Atspi.Text.get_selection(text_iface, 0)) if count > 0 else None+ if selected is not None:+ end = selected.end_offset+ if text_limit is not None:+ end = min(end, selected.start_offset + text_limit + 1)+ selection = safe(lambda: Atspi.Text.get_text(text_iface, selected.start_offset, end))+ if selection is not None:+ selection = limit_text(selection, text_limit)+ return summary, selection, Falsedef build_snapshot(query, text_limit=DEFAULT_TEXT_LIMIT, max_tree_nodes=MAX_ELEMENTS, max_tree_depth=MAX_DEPTH):@@ -443,6 +446,9 @@ def build_snapshot(query, text_limit=DEFAULT_TEXT_LIMIT, max_tree_nodes=MAX_ELEMmax_tree_nodes=max_tree_nodes,max_tree_depth=max_tree_depth,)+ focus, selection, focus_truncated = focus_details(window, text_limit, max_tree_nodes, max_tree_depth)+ if focus_truncated:+ lines.append("[Focus search incomplete: node/depth budget reached.]")pid = node_pid(app)return {"app": {@@ -454,8 +460,8 @@ def build_snapshot(query, text_limit=DEFAULT_TEXT_LIMIT, max_tree_nodes=MAX_ELEM"windowBounds": bounds,"screenshotPngBase64": capture_window_png(bounds),"treeLines": lines,- "focusedSummary": focused_summary(pid, text_limit=text_limit),- "selectedText": selected_text(pid, text_limit=text_limit),+ "focusedSummary": focus,+ "selectedText": selection,"elements": records,}diff --git a/apps/OpenComputerUseLinux/runtime_test.py b/apps/OpenComputerUseLinux/runtime_test.pyindex fb7b922..0baa261 100644--- a/apps/OpenComputerUseLinux/runtime_test.py+++ b/apps/OpenComputerUseLinux/runtime_test.py@@ -172,6 +172,102 @@ class RuntimeKeyTests(unittest.TestCase):self.assertEqual(self.events, [])+class FocusDetailsTests(unittest.TestCase):+ def setUp(self):+ self.runtime = load_runtime()+ self.runtime.Atspi.StateType = types.SimpleNamespace(FOCUSED="focused")++ def node(self, children=(), focused=False):+ node = mock.Mock()+ node.get_child_count.return_value = len(children)+ node.get_child_at_index.side_effect = lambda i: children[i]+ node.get_state_set.return_value.contains.side_effect = lambda s: focused+ node.get_name.return_value = "Editor"+ node.get_role_name.return_value = "text"+ node.get_interfaces.return_value = ["Text"]+ return node++ def test_summary_and_selection_share_one_focus_lookup(self):+ focused = self.node(focused=True)+ root = self.node([focused])+ self.runtime.Atspi.Text = types.SimpleNamespace(+ get_n_selections=lambda _: 1,+ get_selection=lambda _iface, index: types.SimpleNamespace(start_offset=1, end_offset=8),+ get_text=lambda _iface, start, end: "hello"[start:end],+ )+ summary, selected, truncated = self.runtime.focus_details(root)+ self.assertEqual((summary, selected, truncated), ("text Editor", "ello", False))+ root.get_child_at_index.assert_called_once_with(0)+ focused.get_state_set.assert_called_once()++ def test_node_budget_reports_incomplete_instead_of_absent(self):+ children = [self.node() for _ in range(100)]+ root = self.node(children)+ self.assertEqual(self.runtime.focus_details(root, max_nodes=3), (None, None, True))+ self.assertEqual(root.get_child_at_index.call_count, 2)++ def test_depth_budget_and_complete_absence(self):+ root = self.node([self.node([self.node(focused=True)])])+ self.assertEqual(self.runtime.focus_details(root, max_depth=1), (None, None, True))+ self.assertEqual(self.runtime.focus_details(self.node()), (None, None, False))++ def test_depth_boundary_does_not_fetch_out_of_budget_children(self):+ boundary = self.node()+ boundary.get_child_count.return_value = 100+ boundary.get_child_at_index.side_effect = None+ boundary.get_child_at_index.return_value = None+ root = self.node([boundary])+ self.assertEqual(self.runtime.focus_details(root, max_nodes=3, max_depth=1), (None, None, True))+ boundary.get_child_at_index.assert_not_called()++ def test_depth_boundary_leaf_is_complete(self):+ root = self.node([self.node()])+ self.assertEqual(self.runtime.focus_details(root, max_depth=1), (None, None, False))++ def test_focus_after_a_pruned_branch_is_still_reported(self):+ pruned = self.node([self.node()])+ focused = self.node(focused=True)+ root = self.node([pruned, focused])+ self.assertEqual(self.runtime.focus_details(root, max_depth=1), ("text Editor", None, False))+ pruned.get_child_at_index.assert_not_called()++ def test_selection_uses_standard_atspi_api_and_character_offsets(self):+ focused = self.node(focused=True)+ self.runtime.Atspi.Text = types.SimpleNamespace(+ get_n_selections=lambda _: 1,+ get_selection=lambda _iface, index: types.SimpleNamespace(start_offset=1, end_offset=4),+ get_text=lambda _iface, start, end: "a中文🙂z"[start:end],+ )+ self.assertEqual(self.runtime.focus_details(focused), ("text Editor", "中文🙂", False))+ self.assertEqual(self.runtime.focus_details(focused, text_limit=2), ("text Ed...", "中文...", False))++ def test_no_selection_does_not_fetch_a_range(self):+ selection = mock.Mock()+ self.runtime.Atspi.Text = types.SimpleNamespace(get_n_selections=lambda _: 0, get_selection=selection)+ self.assertEqual(self.runtime.focus_details(self.node(focused=True)), ("text Editor", None, False))+ selection.assert_not_called()++ def test_selection_read_failure_keeps_the_focus_summary(self):+ self.runtime.Atspi.Text = types.SimpleNamespace(+ get_n_selections=lambda _: 1,+ get_selection=mock.Mock(side_effect=RuntimeError("element became unavailable")),+ )+ self.assertEqual(self.runtime.focus_details(self.node(focused=True)), ("text Editor", None, False))++ def test_snapshot_reports_focus_budget_exhaustion(self):+ with mock.patch.object(self.runtime, "resolve_app"), \+ mock.patch.object(self.runtime, "main_window", return_value=(0, self.node())), \+ mock.patch.object(self.runtime, "extents"), \+ mock.patch.object(self.runtime, "render_tree", return_value=([], [])), \+ mock.patch.object(self.runtime, "focus_details", return_value=(None, None, True)) as focus, \+ mock.patch.object(self.runtime, "node_pid", return_value=42), \+ mock.patch.object(self.runtime, "node_name", return_value="Editor"), \+ mock.patch.object(self.runtime, "capture_window_png"):+ state = self.runtime.build_snapshot("Editor", max_tree_nodes=5, max_tree_depth=2)+ self.assertIn("incomplete", state["treeLines"][0])+ self.assertEqual(focus.call_args.args[2:], (5, 2))++class RuntimeActionReadBackTests(unittest.TestCase):def setUp(self):self.runtime = load_runtime()diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.mdindex 5e0ba8a..83254de 100644--- a/docs/ARCHITECTURE.md+++ b/docs/ARCHITECTURE.md@@ -138,6 +138,7 @@- Linux 上最接近 macOS AX 的是 AT-SPI2/D-Bus accessibility,而不是一套统一的后台键鼠输入模型。第一版优先使用元素暴露的 AT-SPI action、EditableText 和 Value 接口;coordinate `click` / `drag` 与 `press_key` 使用 AT-SPI event synthesis fallback,在 Wayland 下只能按 best-effort 处理。`press_key` 的具名键以 keysym 配合 `KeySynthType.SYM` 发送,修饰键以 X modifier mask 配合 `LOCKMODIFIERS` / `UNLOCKMODIFIERS` 保持;`PRESS` / `RELEASE` / `PRESSRELEASE` 只接受硬件 keycode,不能传 keysym。- Linux runtime 需要运行在已登录桌面用户 session 里。缺少 `XDG_RUNTIME_DIR`、`DBUS_SESSION_BUS_ADDRESS` 或 display 环境时,Go runtime 会在启动 Python AT-SPI bridge 前尝试从 `/run/user/<uid>` 和常见桌面进程自动发现当前用户的 session bus、display / Wayland 值;纯 SSH tty 如果找不到已登录桌面 session,可以启动二进制,但不能直接 inspect 或操作 GUI session。- `get_app_state` 的 accessibility tree 在 GTK/GNOME app 上可能很深,Linux bridge 使用与 macOS / Windows 一致的 1200 节点、64 层默认 tree budget,并支持显式提高 `max_tree_nodes` / `max_tree_depth`。截图通过 GDK root window best-effort capture;GNOME Wayland 可能返回黑图,bridge 会检测全黑采样并省略 image block。+- 焦点摘要和选区在快照已经选定的窗口里共用一次查找,分别受 `max_tree_nodes` / `max_tree_depth` 约束;到深度边界后不再读取下一层子节点,查找未完成时追加明确提示。选中文字使用标准 AT-SPI `get_n_selections` / `get_selection` 接口读取。实现与隔离 GTK 验证见 [Linux 焦点查找](linux-bounded-focus.md)。- 这 9 个 tool 的协议面与 macOS / Windows 保持一致:`list_apps`、`get_app_state`、`click`、`perform_secondary_action`、`scroll`、`drag`、`type_text`、`press_key`、`set_value`。其中 element-targeted action 会优先复用上一轮 `get_app_state` 的 runtime path metadata,coordinate action 使用 screenshot/window-relative 坐标。- Linux `click_method=accessibility` 映射到 AT-SPI action,`global` 映射到 AT-SPI mouse synthesis 并要求全局指针环境变量;AT-SPI 没有等价的进程定向 mouse dispatch,因此 `app_post` 和 macOS-only 的 `sky_click` 会在 snapshot lookup 前明确返回 unsupported,`key_method=sky_key` 也一样。`auto` 仍保持 AT-SPI action 优先、mouse synthesis fallback 的现有行为。- Linux 动作回读已有独立 GTK fixture,可在私有 D-Bus / Xvfb 中通过 native MCP 复测;运行方式见 [Linux 动作回读](linux-action-read-back.md)。当前 Linux 侧仍是功能性第一版:没有 visual cursor overlay、没有 installer/desktop entry,也没有覆盖全部工具的 Linux fixture。后续 TODO 记录在 `docs/exec-plans/active/20260422-linux-computer-use-runtime.md`。diff --git a/docs/exec-plans/completed/20261017-pr102-bounded-focus.md b/docs/exec-plans/completed/20261017-pr102-bounded-focus.mdnew file mode 100644index 0000000..d8bb528--- /dev/null+++ b/docs/exec-plans/completed/20261017-pr102-bounded-focus.md@@ -0,0 +1,28 @@+# PR #102: bounded Linux focus lookup++## Objective++Merge latest main into #102, preserve #100 read-back behavior and all existing tests, review focus traversal bounds and real selection behavior, repair defects, validate on Linux and push follow-ups to the original PR. Do not merge.++## Constraints++Use the existing worktree and a temporary container on the already-running engine. Do not start UTM or operate personal desktop applications. Keep full tree/index and screenshot behavior, and distinguish incomplete focus search from complete absence. Keep requests synchronous with the existing outer bridge timeout; traversal budgets cannot interrupt a blocked AT-SPI call.++## Progress++- [x] Merge latest main; retain both action-read-back and focus tests and all release entries.+- [x] Add boundary regressions, inspect real GI text-selection APIs and repair any defects.+- [x] Run Linux Go/Python/Node tests, isolated real GTK focus/selection checks and existing action smoke.+- [x] Update docs/history, synchronize remotes, push and verify merge readiness.++## Validation++Check wide trees at the depth boundary, bounded node fetches, complete absence, focus found after a pruned branch, shared focus/selection lookup and error containment. Use native MCP with a disposable GTK fixture on a private Xvfb display and D-Bus session. Verify selection metadata and budget reporting while keeping existing action read-back tests passing. Record platform limits.++## Evidence and outcome++- Main `1b6fd8f` is integrated. The test conflict preserves #100 action read-back regressions alongside focus tests; release entries are retained.+- Four pre-fix regression failures established the depth-boundary fetch defect and unsupported selection API. All 24 Python tests now pass.+- Real GTK/native MCP focus smoke passes both budget limits, Unicode selection, text truncation and clearing selection back to a focus summary. Existing read-back smoke passes.+- Full Linux Go test binary and Node 24 36/36 passed in Debian 12 ARM64; Linux-targeted vet and both architecture builds passed.+- No desktop VM, personal app interaction, Wayland claim or whole-task speedup claim. Tracking budgets remain cooperative within the existing bridge timeout.diff --git a/docs/histories/2026-10/20261015-2035-linux-bounded-focus.md b/docs/histories/2026-10/20261015-2035-linux-bounded-focus.mdnew file mode 100644index 0000000..a266d7d--- /dev/null+++ b/docs/histories/2026-10/20261015-2035-linux-bounded-focus.md@@ -0,0 +1,33 @@+# Bound Linux focus lookup and reuse its result++Agent: Codex; runtime: Linux.++User request: submit scoped improvements for the remaining Linux performance paths.++Linux snapshots currently enumerate the app/window twice and recursively locate the focused element separately for its summary and selected text. This PR resolves focus once in the window already selected for the snapshot and applies the snapshot's node/depth budgets. If traversal ends before focus is found, the returned text explicitly reports an incomplete search rather than claiming focus is absent.++Full AX tree/index and screenshot behavior is unchanged. The focus summary and selection come from the same element. A very deep or large focus search may now return incomplete metadata; callers can explicitly raise the existing tree budgets. This is a cooperative traversal budget, not a way to interrupt a blocking AT-SPI call.++Validation: Python bridge tests (14 passed), including shared focus lookup, node/depth bounds, complete absence and explicit truncation reporting. No live desktop benchmark is claimed.++Changed files: `apps/OpenComputerUseLinux/runtime.py`, `apps/OpenComputerUseLinux/runtime_test.py`, `docs/linux-bounded-focus.md`, architecture and release notes.+++## 2026-10-17:基于当前 main 复查++用户要求在最新 main 上解决冲突、检查测试并补充提交。合入 `1b6fd8f`(含 #100),保留两组 Python 测试和全部发布记录。++### 补充修复++- 原实现到达深度上限后仍逐个读取下一层的子节点,但这些节点不计入 visited,宽树下可绕过节点预算。现在在读取下一层前停止,同时区分边界叶节点的完整查找与有未读子节点的不完整查找。+- 原选区路径调用当前 AT-SPI 不存在的 `get_text_selections`,导致选中文字被静默省略。改用 `get_n_selections` / `get_selection` / `get_text`;摘要与选区仍来自同一个焦点元素,读取失败保留摘要。+- 增加宽树深度边界、叶节点、剪枝后找到焦点、Unicode 偏移与文本限制、无选区和选区读取失败的回归。+- 扩展 #100 的隔离 GTK fixture,新增 `--focus-checks`;更新架构、使用文档和功能记录。++### 验证++- 新回归在补修前有 4 项失败;修复后 Python 全套 24 项通过,包含 #100 的动作回读测试。+- Debian 12 ARM64 临时容器中,完整 Linux Go 测试二进制与 Node 24 的 36 项测试通过;Go 测试由宿主 Go 1.26.1 交叉编译,实际在 Linux 执行。+- native MCP / 真实 GTK 验证通过:节点和深度预算提示、提高预算后的中文 / emoji 选区、text_limit 截断、真实按键清除选区后的焦点摘要。原有动作回读 smoke 也通过。+- Linux-targeted go vet、格式检查、ARM64 / AMD64 构建、shell / Python 语法、文档与 whitespace 检查通过。+- 使用已有容器引擎及私有 D-Bus / Xvfb,未启动桌面虚拟机;不操作个人应用。验证限于 X11 / GTK / AT-SPI,不声称 Wayland 或整任务性能收益。diff --git a/docs/linux-bounded-focus.md b/docs/linux-bounded-focus.mdnew file mode 100644index 0000000..b619626--- /dev/null+++ b/docs/linux-bounded-focus.md@@ -0,0 +1,30 @@+# Linux 焦点查找++快照复用已经选定的窗口,只查找一次焦点,再从同一个元素读取焦点摘要和选中文字,省去重复的应用 / 窗口枚举与树遍历。++焦点查找单独遵守 `get_app_state` 的 `max_tree_nodes` / `max_tree_depth` 上限,默认 1200 个节点、64 层。到达深度上限后不再读取下一层子节点。预算不足且未找到焦点时,返回:++```text+[Focus search incomplete: node/depth budget reached.]+```++这表示查找未完成。需要时可以提高预算后重试。完整 AX tree、索引和截图仍走原来的路径;遍历预算也无法中断某一次阻塞的 AT-SPI 调用,外层 bridge 仍有超时保护。++选区使用标准 AT-SPI `get_n_selections` / `get_selection` / `get_text` 接口,按字符偏移读取,支持中文和 emoji,并遵守 `text_limit`。没有选区或选区读取失败时仍可返回焦点摘要。++## 测试++回归测试:++```sh+python3 apps/OpenComputerUseLinux/runtime_test.py+```++在 Linux 上构建后,可复用 [GTK smoke 环境](linux-action-read-back.md) 验证真实焦点和选区:++```sh+(cd apps/OpenComputerUseLinux && go build -o /tmp/ocu-focus-test .)+bash scripts/run-linux-read-back-smoke.sh /tmp/ocu-focus-test --focus-checks+```++脚本在私有 D-Bus / Xvfb 中创建自己的窗口,验证节点 / 深度预算提示、提高预算后的选区、中文 / emoji、文本截断,以及清除选区后的焦点摘要。覆盖 X11 / GTK / AT-SPI,不代表 Wayland 或其他应用的性能测试。diff --git a/docs/releases/feature-release-notes.md b/docs/releases/feature-release-notes.mdindex cf201c5..7b34ca3 100644--- a/docs/releases/feature-release-notes.md+++ b/docs/releases/feature-release-notes.md@@ -11,6 +11,7 @@| 2026-10-15 | 1.1.1 修复发布 | Locked Use 安装更新更可靠,降低认证与后台 IPC 开销。 | 统一版本为 1.1.1,发布授权宿主、安装加载检查、旧组件卸载与连接生命周期修复;完整双屏遮罩、自动解锁、点击验证和重锁实测通过。 || 2026-10-15 | Locked Use 安装与性能修复 | 减少认证和 IPC 开销,旧系统组件也能正常移除并重装。 | 非 UI 插件使用 privileged host,安装前验证真实加载;修复系统缓存卸载与转交 socket 的生命周期,降低空闲唤醒,status 提示组件版本差异。 || 2026-10-15 | Linux 动作回读 | REPL 批量操作可省去每个动作后被丢弃的完整界面观察。 | Linux 开始支持现有的 `OPEN_COMPUTER_USE_ACTION_READ_BACK=0`,成功动作返回 `ok`,保留最后一次显式观察的索引缓存;默认原生 MCP 回读与显式 `get_app_state` 保持不变。 |+| 2026-10-15 | Linux 焦点与选区 | 大型界面减少重复查找,预算不足时明确提示,选中文字能正确返回。 | 复用快照已选窗口,一次查找生成焦点摘要与选区;按节点和深度预算停止读取,使用标准 AT-SPI 选区接口,并补隔离 GTK 验证。 || 2026-10-14 | 1.1.0 正式发布 | 用户可直接安装 Locked Use,在锁屏遮罩期间继续 GUI 操作,也可用定向查询和持久截图配置减少工具开销。 | 统一版本为 1.1.0;签名 universal macOS App 内嵌服务、Guardian、安装器和授权插件,普通 enable 请求权限,补齐冷启动握手隔离和发布制品检查;新增 app.query()、doctor --json 与 ocu config。 || 2026-10-10 | macOS Locked Use 验证版 | 可在已登录 Mac 锁屏时,通过全屏遮罩继续 GUI 自动化,并在结束或本地接管后安全重锁。 | 正式组件采用 OCU Guardian / OCULockService / OCULockInstaller / OCULockAuth 短名称,开发 App 保留 Dev 区分;新增签名 Broker / 授权插件、双进程显示器保护、一次性许可、租约排空 / 恢复及构建和实机验证脚本;管理员显式启用 validation profile,生产 backend 在完整验证前保持关闭。 || 2026-10-10 | macOS 定向 AX 查询 | 知道控件文案时,可以直接查询并操作,不必先获取截图和完整 AX tree。 | 新增 native `query` 和 JS `app.query()`,按文字、角色和窗口查找元素,返回完整性元数据;查询索引仅在同一会话内有效,操作前重新验证目标和位置。 |diff --git a/scripts/run-linux-read-back-smoke.sh b/scripts/run-linux-read-back-smoke.shindex e02f6e4..3d4c7c2 100755--- a/scripts/run-linux-read-back-smoke.sh+++ b/scripts/run-linux-read-back-smoke.sh@@ -2,13 +2,15 @@set -euo pipefailrepo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"-if [[ "$(uname -s)" != Linux || $# != 1 ]]; then- echo "Usage (Linux): bash scripts/run-linux-read-back-smoke.sh /path/to/open-computer-use" >&2+if [[ "$(uname -s)" != Linux || $# -lt 1 ]]; then+ echo "Usage (Linux): bash scripts/run-linux-read-back-smoke.sh /path/to/open-computer-use [--focus-checks]" >&2exit 2fi+native_path="$1"+shiftruntime_dir="$(mktemp -d)"trap 'rm -rf "${runtime_dir}"' EXITXDG_RUNTIME_DIR="${runtime_dir}" NO_AT_BRIDGE=0 GTK_A11Y=always \xvfb-run -a dbus-run-session -- python3 \- "${repo_root}/scripts/tests/linux-read-back-smoke.py" --native "$1"+ "${repo_root}/scripts/tests/linux-read-back-smoke.py" --native "${native_path}" "$@"diff --git a/scripts/tests/linux-read-back-smoke.py b/scripts/tests/linux-read-back-smoke.pyindex 71bf58f..9ae62f4 100644--- a/scripts/tests/linux-read-back-smoke.py+++ b/scripts/tests/linux-read-back-smoke.py@@ -13,7 +13,7 @@ import tempfileimport time-def fixture(name, state_file):+def fixture(name, state_file, focus_checks=False):import gigi.require_version("Gtk", "3.0")@@ -37,6 +37,8 @@ def fixture(name, state_file):def publish(*_args):state["text"] = entry.get_text()state["focused"] = entry.has_focus()+ bounds = entry.get_selection_bounds()+ state["selected"] = entry.get_text()[bounds[0]:bounds[1]] if bounds else ""temporary = state_file.with_suffix(".tmp")temporary.write_text(json.dumps(state))temporary.replace(state_file)@@ -55,10 +57,24 @@ def fixture(name, state_file):button.connect("clicked", increment)entry.connect("changed", publish)entry.connect("activate", activate)+ entry.connect("notify::selection-bound", publish)+ entry.connect("notify::cursor-position", publish)+ entry.connect("notify::has-focus", publish)window.connect("destroy", Gtk.main_quit)window.show_all()entry.grab_focus()window.get_window().focus(Gdk.CURRENT_TIME)+ if focus_checks:+ def initialize_selection():+ entry.grab_focus()+ window.get_window().focus(Gdk.CURRENT_TIME)+ if not entry.has_focus():+ return True+ entry.set_text("prefix-中文🙂-suffix")+ entry.select_region(7, 10)+ publish()+ return False+ GLib.timeout_add(20, initialize_selection)publish()Gtk.main()@@ -138,14 +154,45 @@ def element_index(result, name):return matches[0]-def smoke(native):+def check_focus(client, name, state_file):+ wait_for(lambda: json.loads(state_file.read_text())["focused"] and+ json.loads(state_file.read_text())["selected"] == "中文🙂", "fixture focus and selection")+ marker = "[Focus search incomplete: node/depth budget reached.]"+ for limits in ({"max_tree_depth": 1}, {"max_tree_nodes": 2}):+ result = client.call("get_app_state", name, **limits)+ assert not result.get("isError"), result+ assert marker in text(result), text(result)+ assert "Selected text:" not in text(result), text(result)+ assert "The focused UI element" not in text(result), text(result)+ observed = client.call("get_app_state", name, max_tree_depth=10)+ expect_snapshot(observed)+ actual = json.loads(state_file.read_text())+ assert actual["focused"] and actual["selected"] == "中文🙂", actual+ assert "Selected text: [中文🙂]" in text(observed), text(observed)+ assert marker not in text(observed), text(observed)+ limited = client.call("get_app_state", name, text_limit=2)+ assert not limited.get("isError"), limited+ assert "Selected text: [中文...]" in text(limited), text(limited)+ result = client.call("press_key", name, key="Right")+ assert not result.get("isError"), result+ wait_for(lambda: json.loads(state_file.read_text())["selected"] == "", "selection cleared by real key")+ observed = client.call("get_app_state", name)+ expect_snapshot(observed)+ assert "Selected text:" not in text(observed), text(observed)+ assert "The focused UI element is text Read-back input." in text(observed), text(observed)+ print("Focus smoke: node/depth limits, Unicode selection, text limit and focus summary passed.")+++def smoke(native, focus_checks=False):with tempfile.TemporaryDirectory(prefix="ocu-read-back-smoke-") as directory:directory = Path(directory)state_file = directory / "state.json"name = "OCUReadBackFixture" + str(os.getpid())with (directory / "stderr.log").open("w+") as stderr:- process = subprocess.Popen([sys.executable, __file__, "--fixture", name,- "--state-file", str(state_file)], stderr=stderr)+ arguments = [sys.executable, __file__, "--fixture", name, "--state-file", str(state_file)]+ if focus_checks:+ arguments.append("--focus-checks")+ process = subprocess.Popen(arguments, stderr=stderr)clients = []try:wait_for(state_file.exists, "GTK fixture startup")@@ -153,6 +200,9 @@ def smoke(native):clients.append(client)# AT-SPI registration follows GTK startup asynchronously.wait_for(lambda: not client.call("get_app_state", name).get("isError"), "AT-SPI registration")+ if focus_checks:+ check_focus(client, name, state_file)+ returnobserved = client.call("get_app_state", name)expect_snapshot(observed)input_index = element_index(observed, "Read-back input")@@ -221,11 +271,12 @@ if __name__ == "__main__":parser = argparse.ArgumentParser(description=__doc__)parser.add_argument("--native")parser.add_argument("--fixture")+ parser.add_argument("--focus-checks", action="store_true")parser.add_argument("--state-file", type=Path)args = parser.parse_args()if args.fixture:- fixture(args.fixture, args.state_file)+ fixture(args.fixture, args.state_file, args.focus_checks)else:if not args.native:parser.error("--native is required")- smoke(str(Path(args.native).resolve()))+ smoke(str(Path(args.native).resolve()), args.focus_checks)