Skip to content

👾 Open Computer Use – Open-Source Alternative to Codex Computer Use

Align snapshot tree indentation

Leob80f587a16c5
diff --git a/docs/histories/2026-05/20260507-2004-align-tree-root-indentation.md b/docs/histories/2026-05/20260507-2004-align-tree-root-indentation.md
new file mode 100644
index 0000000..9cabffd
--- /dev/null
+++ b/docs/histories/2026-05/20260507-2004-align-tree-root-indentation.md
@@ -0,0 +1,24 @@
+# 对齐 AX tree 根节点缩进
+
+## 用户诉求
+
+继续对比 Lark / Electron app 上开源版和官方 `computer-use` 的 `get_app_state` 返回,逐步修正不一致的输出形状。
+
+## 主要改动
+
+- 将真实 AX tree renderer 的根节点缩进从一层 tab 改为顶格。
+- 合成文本节点沿用同一缩进规则,避免 summary children 比真实 children 多缩一层。
+
+## 设计动机
+
+官方 Lark 返回里根节点形态是 `0 standard window ...` 顶格,子节点才从一层 tab 开始。开源版之前输出为 `\t0 standard window ...`,整棵树比官方多缩进一层,影响对比和可读性。
+
+## 验证
+
+- Lark 本地回归确认前三个树行变为:根节点顶格、一级子节点一层缩进、二级子节点两层缩进。
+- `swift test --filter SnapshotRenderedTextStartsDirectlyWithAppHeader`
+- `./scripts/build-open-computer-use-app.sh debug`
+
+## 受影响文件
+
+- `packages/OpenComputerUseKit/Sources/OpenComputerUseKit/AccessibilitySnapshot.swift`
diff --git a/packages/OpenComputerUseKit/Sources/OpenComputerUseKit/AccessibilitySnapshot.swift b/packages/OpenComputerUseKit/Sources/OpenComputerUseKit/AccessibilitySnapshot.swift
index 1c9e67e..6db0958 100644
--- a/packages/OpenComputerUseKit/Sources/OpenComputerUseKit/AccessibilitySnapshot.swift
+++ b/packages/OpenComputerUseKit/Sources/OpenComputerUseKit/AccessibilitySnapshot.swift
@@ -602,7 +602,7 @@ private struct TreeRenderer {
let linePrefix = roleText.isEmpty ? "\(index)" : "\(index) \(roleText)"
let lineBody = "\(linePrefix)\(traitsSegment)\(titleSegment)\(rowSummarySegment)\(labelSegment)\(helpSegment)\(urlSegment)\(identifierSegment)\(valueSegment)"
- lines.append("\(String(repeating: "\t", count: depth + 1))\(lineBody)\(actionsSegment)")
+ lines.append("\(String(repeating: "\t", count: depth))\(lineBody)\(actionsSegment)")
let record = ElementRecord(
index: index,
@@ -653,7 +653,7 @@ private struct TreeRenderer {
let index = nextIndex
nextIndex += 1
- lines.append("\(String(repeating: "\t", count: depth + 1))\(index) text \(text)")
+ lines.append("\(String(repeating: "\t", count: depth))\(index) text \(text)")
records[index] = ElementRecord(
index: index,