Skip to content

feat(mcp): 支持替换输入框全文、报告元素截断和等待元素消失 - #159

Merged
Derrors merged 7 commits into
Derrors:mainfrom
Julian0197:fix/mcp-input-elements-wait
Nov 18, 2026
Merged

Derrors merged 7 commits into
Derrors:mainfrom
Julian0197:fix/mcp-input-elements-wait

Conversation

@Julian0197

@Julian0197 Julian0197 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Closes #158

11 月 19 日同机复核(eb42c0f 对上游 0.2.9)

使用 iPhone 14、iOS 27.0.1、原生控制,各版本的 MCP 与 daemon 均取自对应的 release 构建。

同一操作 上游 9a476eb PR eb42c0f
在「设置」搜索框输入并读回 wifi,然后用 replace:true 替换为 wi 工具返回成功,实际读回 wifi wi(5.191 秒) 独立读回 wi(4.865 秒)
保留 Spotlight 搜索词「设置」,默认流程跑五轮 76/80,136.6 秒 77/80,139.7 秒
  • 清空输入框。 在 eb42c0f 接着传 {"text":"","replace":true},1.718 秒后工具返回已清空,独立读回占位文字「搜索」。
  • Spotlight 超时仍能复现。 两版首轮均找到「设置」,第 3 至 5 轮均超时。PR 版失败时的截图显示搜索框已有「设置」,结果区为空。上游另有一次键盘未出现,输入被拒绝。
  • 耗时。 同一台手机、相同的时钟标签页和 16 个步骤。compare.py 的逐步中位数之和为 28.29 → 28.58 秒。首个状态查询慢 0.31 秒、滑动慢 1.39 秒,两项超出脚本阈值,原因尚未定位。
  • 场景组。 eb42c0f 的默认 scenarios.py 52/52 通过,覆盖 Spotlight 的 wifi → wi → 蓝牙 → 清空、元素截断和等待消失。锁屏与 URL 两个可选场景未运行。
  • 自动检查。 eb42c0f 在 Rust 1.99.1 上工作区测试 501 通过、3 项忽略,严格 Clippy、逐包格式检查通过。Viewer 34 项、设备脚本解析 11 项通过,回归索引 0 缺失。GitHub CI

本次真机运行期间,Derrors 又补充了等待消失的超时诊断(5575797)和睡眠专注锁屏识别(a7bd81d)。上表保留实际测试的提交号。

本机在 a7bd81d 重新执行工作区测试,502 项通过、3 项忽略,严格 Clippy 通过。

What and why

在 iPhone「设置」搜索框输入 wifi,接着输入 蓝牙。0.2.8 会留下 wifi 蓝牙。现在可以调用 iphone_type_text {"text":"蓝牙","replace":true},再读搜索框,全文就是 蓝牙。省略 replace 时仍然追加。只有核对全文成功,工具才会执行可选的 submit。

设置首页这次读到 13 个元素。如果只要第一项,iphone_list_elements {"limit":1} 会同时返回 matched_count:13 和 truncated:true。把上限调到 500,返回 13 项和 truncated:false,调用方可以直接读取字段。

在设置首页调用 iphone_wait_for {"text":"飞行模式","state":"disappeared","timeout":2},旧版在开关仍可见时就报告「已出现」。新版在该页等满两秒后超时。切到时钟再发同一请求,工具读到完整界面后返回「已消失」。省略 state 时仍等出现。

对 5b58753 的评审回复

修订提交 c2ce06d。

  1. 替换子串和多输入框。 替换时保留原聚焦框的 AX 对象标识,只在同一对象仍聚焦且全文与目标完全相等时确认粘贴。普通追加仍用原有判定。回归单测覆盖原值 wifi、目标 wi、另一输入框已含 wi,以及焦点移到不同对象。
  2. 失败后收起选区。 Cmd-A 之后出错时重新读取焦点。原框仍聚焦且没有遮挡弹窗时发送右方向键收起选区;焦点已变或无法收起时,错误信息提示重试前检查选区。
  3. 无密码锁屏。 原生等待在准备返回「已消失」时同时检查 lockdown 状态和 SpringBoard 界面。没有密码时,锁屏封面或无法识别的系统界面也不能确认消失。WDA 路径在同一时机调用严格锁屏检查。
  4. 不完整的树。 替换依据聚焦对象和全文。判断消失仍要求可读且未截断的树。单个子节点读取失败时,重读父节点区分子节点已移除和仍存在但读不到。达到节点、深度或时间上限仍保留不完整标记。
  5. 轮询与场景收尾。 目标仍可见时不额外查询锁屏。Spotlight 场景通过 finally 返回主屏幕,并增加 wifi → wi → 蓝牙 的独立读回断言。
  6. 接口细节。 单步和 batch 的 then:"elements" 均带截断元数据。Viewer 复用元素列表转换,缺少数据时计数和截断状态为 null。空文本校验共用于 daemon 和批量预检,避免批量执行到一半才报参数错误。此次保留可选的 structuredContent,未声明强制的 outputSchema,以兼容旧 daemon 的纯文本响应和操作工具其他 then 模式。

当前提交已通过本机严格 Clippy 和工作区测试(498 通过、3 个既有忽略项)。本轮 USB 尚未枚举到 iPhone,作者要求的「设置」wifi → wi 和用原搜索词「设置」重查 Spotlight 超时,尚无本提交的真机结果。下方真机数据保留原提交号。

早期提交的测试记录(7bdd408 至 c2ce06d)

1. Unit tests

  • Rust 1.99.1 的工作区测试 497 项通过。严格 Clippy 和仓库指定的逐包格式检查通过。另通过 Viewer 34 项、设备脚本解析测试 11 项、回归索引和 macOS PTY。
  • 新增测试覆盖 replacing_text_requires_one_readable_focused_field、element_limit_is_reported_in_structured_content 和 disappearance_requires_a_readable_complete_screen_without_the_target。
  • 保留原有等待断言:搜索词须出现在输入框外的结果里。5b58753 的测试脚本改搜刚启动过的「时钟」,并处理了元素描述中标签与值相同时只显示一份的格式。
  • PTY 专项已跑。另两项忽略测试针对视频编码和录制,本次没有改动相关代码。

2. Real-device regression

同一台 USB iPhone 14(iOS 27.0.1,390 × 844 pt),macOS 26.6.2。用各自 release 版 MCP 与 daemon 走原生通道。

版本 五轮默认流程 失败项
上游 0.2.9,9a476eb 76/80,124.7 s 1 次输入时无键盘,3 次等待 Spotlight 的「设置」结果超时。
候选版,7bdd408 75/80,139.3 s 5 次均在等待「设置」结果时超时。失败轮次的截图可见搜索框中有「设置」,结果区为空。
  • 默认流程和完整默认场景组全部通过。场景组实跑 49/50,唯一失败在 Spotlight 替换断言。读回的是 TextField "蓝牙" id="SpotlightSearchField",旧断言只看单列的 value=。后续提交 5b58753 只改两处测试脚本,尚未在手机上重跑。
  • 两版均使用同一脚本执行 tests/device/run.py --repeat 5 --json,并运行 tests/device/compare.py。
  • 耗时对照达标。逐步中位数之和为 26.28 s → 27.45 s。比较脚本标出三项:点击不存在的按钮 +0.98 s、滑动 +1.66 s、末次状态查询 +0.51 s。现有数据不足以确定原因。
  • 三项工具行为另在「设置」中完成定向对照,见下表。
  • 长时间运行、锁屏和 URL 专项不适用。本次改动针对单次工具请求,未改动连接管理、解锁、媒体流或定时任务。

3. Regression cases

  • 在回归索引追加 159(替换输入)、160(元素截断)和 161(等待消失)。索引检查通过。
  • 默认场景组全部通过,最新真机结果见上。

4. Real-device proof

相同的请求 上游 0.2.9 候选版
设置搜索框内已有 wifi,调用 iphone_type_text {"text":"蓝牙","replace":true} 3 次均读回 wifi 蓝牙 停止测试前完成两轮独立读回,全文均为 蓝牙。替换调用分别用时 7.31 s、7.81 s。无键盘时,带 submit:true 的替换在场景组中明确返回错误。
同一屏有 13 项,调用 iphone_list_elements,先取 limit:1,再取 limit:500 只在文本中提示剩余 12 项 3 轮的 matched_count 均为 13,分别返回 1 项和 13 项,truncated 分别为 true、false。
「飞行模式」仍可见时调用 iphone_wait_for {"text":"飞行模式","state":"disappeared","timeout":2},切到时钟后再调用 3 轮都在可见时返回「已出现」,离开后等出现并超时 3 轮都在可见时超时(2.15–2.26 s),离开后返回「已消失」(0.65–0.67 s)。
  • 同机复现及定向验证。早期候选版也做过三轮完整对照,包含省略 replace 的追加行为。逐次真机记录
  • 未测范围包括旧版 iOS/WDA、iPad、横屏、密码框和真机读树故障注入。空树和不完整快照的消失判定由单元测试覆盖。

Checklist

  • 保留工具名、默认输入和默认等待语义。
  • 无新增依赖和 vendor 变更。
  • 已更新中英文 changelog 和工具说明。
  • 公开结果已省略设备标识、账户和私人屏幕内容。

@Julian0197
Julian0197 marked this pull request as ready for review November 18, 2026 11:52

@Derrors Derrors left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢实现和真机对照。三个功能的方向都对。replace 在 WDA 通道明确报「需要原生控制」,没有悄悄退回到追加输入;替换后先核对全文再按回车。这两处设计都很好。以下意见基于 5b58753 的代码阅读,第 1 条我已经对着代码逐步确认过,其余几条是从代码推断的,还没上真机复现。

合入前需要修

1. 原值中已包含新文字时,替换一定失败(native.rs · replace_text → native_paste)

replace_text 只把聚焦框一个字段的原值作为 old 传给 native_paste,而等待循环里的 paste_seen 比较的是 elements::field_values(),也就是全屏所有输入框的值。全选之后原文字还留在框里,所以第一次读回就满足「包含 wanted 且 now != old」,直接返回 Typed。这时「允许粘贴」还没点,restore_pasteboard 一还原剪贴板,弹窗就消失了,粘贴并没有进去。最后全文核对失败,报错。

例子:把 iphone 15 pro 替换成 iphone,或把 wifi 替换成 wi。屏幕上另一个输入框恰好包含新文字时也会触发。

建议:替换路径不复用「原值里有没有新文字」这个判断。改为点过「允许粘贴」(或确认粘贴已落下)之后,再核对聚焦框的全文是否等于 text。

2. 替换失败后,原文字仍处于全选状态(replace_text)

Cmd-A 之后的各个出错分支(粘贴报错、弹窗超时、核对失败)都没有取消选中。agent 按错误提示改用默认模式的 iphone_type_text 重试时,会把原内容整个覆盖掉,这和默认模式「不清空原有内容」的承诺相反。第 1 条会让这种情况经常出现。建议出错时先取消选中,比如按一下 → 键,再返回错误。

3. 没设密码的手机,锁屏可能被判成「已消失」(native_wait_for)

锁屏保护用的是 native_locked() → passcode_locked()。从名字看,它只在设了密码的手机上报锁屏。没设密码的手机在等待期间自动锁屏后,读到的是一个完整的锁屏界面,里面没有目标文字,于是返回「已消失」。这正是这个保护本来要防的误报。如果 lockdown 能拿到不依赖密码的锁屏状态,就改用它;拿不到的话,可以看前台是不是 SpringBoard 锁屏。

建议修

4.「树完整」的条件过宽

「不完整的树不能确认消失」这个原则我同意。问题是现在下面任何一种情况都会标成不完整:节点超过 400 个、遍历超时、遍历途中有一个节点消失、WDA 收紧了快照上限。结果是长列表、带动画的页面(例如计时器在走)上,disappeared 等不到成功;replace 也会一直报「无法确认当前聚焦的是可读的普通输入框」。

对 focused_text_value 来说,只要能确认聚焦框是唯一的就够了,不需要整棵树完整。对 disappeared 来说,至少应该把「个别节点在遍历中消失」和「被上限截断」分开处理。

5. 等消失时每轮都查一次锁屏

native_wait_for 每 0.3 秒调一次 native_locked(),每次都新建 lockdown 连接。5 秒的等待大约要握手 15 次,其中任何一次出错整个等待就失败。WDA 路径同样每轮多一次 locked()。建议只在 wait_match 即将判定「已消失」时查一次。

6. 测试场景失败时没有收尾(tests/device/scenarios.py · editing)

前置检查失败后直接 return,Spotlight 和键盘还留在屏幕上,后面的场景会从错误的界面开始,连带失败。建议用 try/finally 保证执行 p.home()。

小问题

  • 只有直接调用 iphone_list_elements 才会带 matched_count/truncated,then=elements 和 batch 的结果里都没有,字段不一致。返回了 structuredContent 的工具也可以考虑声明 outputSchema。
  • viewer 的 /elements(viewer/api.rs)又写了一遍 element_list 的序列化,fallback 分支也少了这两个字段。建议复用同一个 helper。
  • replace 的空文本检查在 MCP 的 to_request 和 daemon 的 check_request 里各做了一次,两处的报错文案也不一样。只保留 daemon 那一处就够了。

麻烦修完 1–3 后,把「设置」里的 wifi → wi(新文字是原值子串)补进真机对照,同时确认一下之前 run.py 首轮 15/16 的 Spotlight 超时是否还会出现。

@Julian0197
Julian0197 requested a review from Derrors November 18, 2026 15:03
Derrors and others added 3 commits November 18, 2026 23:06
An argument a tool doesn't list in its input schema was ignored: a call
with a misspelt name, or with an argument from a newer version, ran with
the default and reported success. iphone_wait_for with
state: "disappeared" answered that the text had appeared (Derrors#158).

Each call is now checked against its tool's input schema before anything
is done, and the error names the arguments the tool takes. A batch's
steps are checked the same way; then in a step is refused (the batch's
own then applies), and wait takes only seconds. The developer tools and
iphone_confirm are checked against their own schemas.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ment list schema

- iphone_type_text with replace: true and empty text empties the focused
  field: select all, then Delete (no paste, no "Allow Paste" prompt), and
  the original field must read empty. An empty field reads its placeholder
  as its value natively (Spotlight's reads «搜索»), so a value equal to the
  placeholder counts as empty. On the phone the first try failed exactly
  there: "the field isn't empty, it reads 搜索".
- iphone_list_elements declares its output schema (the structured content
  it already returns).
- tests/device/run.py waits for «设置» in Spotlight again, as on main. The
  switch to Clock came from one timeout on one run; on iPhone19,7 /
  iOS 27.2.1 the original step passed 5 runs out of 5 with this branch
  (0.24-0.36 s), so it stays a check rather than being worked around.
- tests/device/scenarios.py editing: also empties the Spotlight query.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…and says why it timed out

- Natively the read is judged first (no extra cost): the lock screen known
  by its marks is reported at once; notification center, control center or
  SpringBoard in front without app icons or Spotlight (how the lock screen
  often reads natively) don't count as gone. Lockdown is asked after that,
  at most once a second, instead of first on every confirmation: it is the
  only check that says locked before the read shows it, but only on a phone
  with a passcode, so it can't stand alone. A lockdown failure leaves the
  lock state unknown and the wait goes on; it no longer ends the wait.
- Only a known lock ends the wait at once ("the phone locked while
  waiting"). Something else over the screen keeps the wait going until the
  text is gone or the timeout.
- Through WDA, a failed or uncertain lock check keeps the wait going
  instead of ending it.
- A timeout says why: the text is still there; the screen wasn't read in
  full (too many elements, a slow read); or something is over the screen /
  the lock state couldn't be found out.
- Docs: a missing full stop in the element list row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
During a sleep focus the lock screen is another process (SleepLockScreen),
not SpringBoard, with SpringBoard's flashlight and camera buttons over it
(iPhone19,7, iOS 27.2.1). With lockdown not saying locked (as on a phone
without a passcode, simulated on the device), a wait for Clock's tab to
disappear after the lock button answered "Disappeared" in 1.1 s.

The flashlight and camera buttons (flashlight-orb-button,
system-orb-button-*) and SleepLockScreen's own elements now mark the screen
as covered, whichever process is in front. On the device, with lockdown
still left out: the lock button over Clock, and a phone locked for a
while, both time out saying the screen is covered; the unlocked home
screen still answers "Disappeared" in 0.2 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Derrors

Derrors commented Nov 18, 2026

Copy link
Copy Markdown
Owner

@Julian0197 感谢 c2ce06d 自己处理了评审意见。我在它之上直接推了 4 个提交(fast-forward,没有改写你的提交),说明如下:

新增的提交

  1. 45c8549 MCP 工具收到不认识的参数时报错,不再忽略。 这是 MCP:替换搜索框全文、判断元素列表是否截断、等待元素消失 #158 里「state: disappeared 0.31 秒返回已出现」的根因:to_request 会悄悄丢掉不认识的参数,调用照默认行为执行并报成功。现在每次调用先按工具自己的 input schema 校验,报错时列出它接受的参数,什么都不执行;iphone_batch 的每一步也一样(单步里的 then 会被拒绝,批量的 then 写在批量本身)。原本的 fix(mcp): refuse arguments a tool doesn't take instead of dropping them #160 已关闭并合到这里,CHANGELOG 的「升级注意事项」里写了这个行为变化。
  2. eb42c0f replace: true 配空文字时清空输入框。 实现是全选后按删除键,不走粘贴。原生读法下空输入框会把占位文字(Spotlight 的「搜索」)读成值,所以值等于占位文字时算作空。同一个提交还:
    • 给 iphone_list_elements 声明了 outputSchema;
    • 把 tests/device/run.py 恢复成等「设置」。用这个分支连跑 5 次都通过(0.24–0.36 s),之前那次超时是偶发,留着它当检查,不绕开。
  3. 5575797 等消失:先看读到的界面,再问 lockdown;超时说明原因。
    • 原来先调 native_locked(g).await?:lockdown 偶尔出错就让整个等待失败,而且设了密码的手机上分不清是哪条规则拦住的。现在先用读到的界面判断,lockdown 最多每秒问一次,出错时当作「不确定」继续等。
    • 只有确认锁屏才立即报错;通知中心、控制中心挡着时继续等到超时。WDA 路径的锁屏检查出错或拿不准时也是继续等。
    • 超时分三种提示:文字还在;界面没读全(元素太多、读得太慢);屏幕被挡住或查不到锁屏状态。
  4. a7bd81d 睡眠锁屏不算「已消失」。 开着睡眠专注模式时,锁屏是另一个进程(SleepLockScreen),不是 SpringBoard,上面盖着 SpringBoard 的手电筒、相机按钮。没设密码的手机上(lockdown 不报锁屏)原来会在 1.1 s 返回「已消失」。现在屏幕上有手电筒 / 相机按钮或 SleepLockScreen 自己的元素时,算作被挡住。

真机验证

iPhone19,7,iOS 27.2.1,原生通道,a7bd81d 的构建:

  • cargo clippy -D warnings、cargo test --workspace、cargo fmt --check、tests/check_regression_cases.py 都通过。
  • tests/device/run.py:16 次调用,0 失败。
  • tests/device/scenarios.py(除 lock/url):52 项检查,0 失败。editing 场景:
PASS [#159] replace the focused Spotlight query with 'wi' (2.43s)
PASS [#159] replace the focused Spotlight query with '蓝牙' (2.27s)
PASS [#159] replace with empty text empties the Spotlight query (1.31s)
PASS [#159] replacement without a keyboard is refused
PASS [#160] element limit 1 reports truncation  ← returned=1 matched=8 truncated=True
PASS [#160] element limit 500 reports truncation  ← returned=8 matched=8 truncated=False
PASS [#161] still-visible Clock tab times out (2.16s)
PASS [#161] Clock tab is gone after leaving the app (0.19s)
  • 锁屏防护:用临时 daemon(等待前不自动解锁,lockdown 固定答「没锁」,模拟没设密码的手机)单独测界面规则:

    • 时钟 App 前台时按锁屏键、等「世界时钟」消失:两次都超时,提示屏幕被挡住,没有误报;
    • 锁屏一段时间后等:同样超时,没有误报;
    • 解锁后在主屏幕上等:两次 0.2 s 返回「已消失」。

    加上第 4 条之前,睡眠锁屏下前两种情况都误报了「已消失」。

已知、不在这个 PR 里

  • WDA 通道的 replace 另开了 iphone_type_text: support replace: true through WDA #161。
  • 睡眠锁屏下自动解锁调不出密码键盘(报「调不出锁屏密码键盘」),和这个 PR 无关。
  • 元素特别多的页面(超过 400 个节点)上等消失会一直等到超时,这是有意的取舍,超时提示会说明是界面没读全。

合并时建议 squash:7 个提交里有你我交替改动、后来被覆盖的内容。

🤖 Generated with Claude Code

@Derrors
Derrors merged commit 3ed8c6a into Derrors:main Nov 18, 2026
8 of 9 checks passed
@Derrors Derrors mentioned this pull request Nov 22, 2026
11 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP:替换搜索框全文、判断元素列表是否截断、等待元素消失

2 participants