Skip to content
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ What changed in each release, one line per change. Versions follow [Semantic Ver

## [Unreleased]

### Upgrade notes

- MCP tools now refuse an argument they don't take, and do nothing, instead of ignoring it. A misspelt argument, or one from a newer version, used to run the call with the default and report success: `iphone_wait_for` with `state: "disappeared"` answered that the text had appeared (#158). The error names the arguments the tool takes. This includes `then` on tools that don't list it, and on the steps of `iphone_batch` (the batch's own `then` applies).

### New

- With native control, `iphone_type_text` supports `replace: true` (empty text empties the field) and verifies the full field value before pressing Return; when it fails, the old text is left unselected.
- `iphone_list_elements` reports the match count and whether `limit` truncated the list in its structured result.
- `iphone_wait_for` can wait for text to disappear without mistaking an incomplete screen read for success.

## [0.2.9] - 2026-11-18

### Fixes
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

## [Unreleased]

### 升级注意事项

- MCP 工具收到不认识的参数时改为报错、什么都不做,不再忽略。以前参数名拼错、或者用了新版本才有的参数,调用会按默认行为执行并报成功:`iphone_wait_for` 带 `state: "disappeared"` 时直接回答文字已出现(#158)。报错里会列出这个工具接受的参数。没有声明 `then` 的工具传 `then`、`iphone_batch` 的单个步骤里传 `then` 也会报错(批量的 `then` 写在批量本身)。

### 新功能

- 原生控制的 `iphone_type_text` 支持 `replace: true`(文字为空时清空输入框),核对替换后的输入框全文后才按回车;替换失败时会取消全选,原文保留。
- `iphone_list_elements` 的结构化结果会说明匹配数量和是否被 `limit` 截断。
- `iphone_wait_for` 支持等待文字消失;读屏不完整时不会误报成功。

## [0.2.9] - 2026-11-18

### 修复
Expand Down
5 changes: 3 additions & 2 deletions apps/iphone-control/src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2159,12 +2159,13 @@ mod tests {
height: h,
})
.collect();
let els = element_list(Some(protocol::Data::Elements { elements }));
let els =
element_list(Some(protocol::Data::Elements { elements, matched_count: None, truncated: None })).elements;
assert_eq!(
els[0],
json!({"type": "Button", "label": "今天", "x": 33.0, "y": 861.0, "width": 56.0, "height": 38.0})
);
assert!(element_list(Some(protocol::Data::Alert { text: None })).is_empty());
assert!(element_list(Some(protocol::Data::Alert { text: None })).elements.is_empty());
let (frame, b) = alert_parts(&els);
assert_eq!(frame, json!({"x": 80.0, "y": 306.0, "width": 270.0, "height": 320.0}));
let labels: Vec<&str> = b.iter().filter_map(|v| v["label"].as_str()).collect();
Expand Down
19 changes: 13 additions & 6 deletions apps/iphone-control/src/viewer/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pub(super) async fn apps(ctx: &Ctx, system: bool) -> Result<Value, String> {
/// `keep_locked`: a read-only visitor, for whom a locked phone isn't unlocked
pub(super) async fn elements(ctx: &Ctx, filter: &str, keep_locked: bool) -> Result<Value, String> {
let req = Request::ListElements { filter: filter.to_owned(), limit: ELEMENT_LIMIT, max_age_ms: 0, keep_locked };
Ok(json!({"elements": element_list(data_of(&ctx.socket, req).await?)}))
Ok(serde_json::to_value(element_list(data_of(&ctx.socket, req).await?)).expect("element list is JSON"))
}

/// A system alert on the phone (a permission prompt and the like): `{ alert: null }` or
Expand All @@ -197,17 +197,24 @@ pub(super) async fn alert(ctx: &Ctx, keep_locked: bool) -> Result<Value, String>
let req = Request::ListElements { filter: String::new(), limit: ELEMENT_LIMIT, max_age_ms: 0, keep_locked };
// without its buttons the page still says there is an alert
let els = data_of(&ctx.socket, req).await.map(element_list).unwrap_or_default();
let (frame, buttons) = alert_parts(&els);
let (frame, buttons) = alert_parts(&els.elements);
Ok(json!({"alert": {"text": text, "frame": frame, "buttons": buttons}}))
}

/// The elements in a [`protocol::Data::Elements`] as the page's JSON: `{ type, label, x, y, width, height }`
pub(super) fn element_list(data: Option<protocol::Data>) -> Vec<Value> {
#[derive(Default, serde::Serialize)]
pub(super) struct ElementList {
pub(super) elements: Vec<Value>,
matched_count: Option<usize>,
truncated: Option<bool>,
}

pub(super) fn element_list(data: Option<protocol::Data>) -> ElementList {
match data {
Some(protocol::Data::Elements { elements }) => {
elements.iter().filter_map(|e| serde_json::to_value(e).ok()).collect()
Some(protocol::Data::Elements { elements, matched_count, truncated }) => {
ElementList { elements: elements.into_iter().map(|e| json!(e)).collect(), matched_count, truncated }
}
_ => Vec::new(),
_ => ElementList::default(),
}
}

Expand Down
48 changes: 38 additions & 10 deletions crates/ictl-ax/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,31 +397,59 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send + 'static> Session<S> {
let deadline = Instant::now() + budget;
let mut attrs: HashMap<ObjectRef, Obj> = HashMap::new();
let mut seen = HashSet::from([(root.pid, root.id)]);
let mut frontier: VecDeque<(ObjectRef, usize)> = VecDeque::from([(root, 0)]);
let mut frontier = VecDeque::from([(root, 0, None)]);
let mut missing = Vec::new();
let mut complete = true;
while !frontier.is_empty() {
if attrs.len() >= limit || Instant::now() >= deadline {
complete = false;
break;
}
let n = frontier.len().min(BATCH).min(limit - attrs.len());
let chunk: Vec<(ObjectRef, usize)> = frontier.drain(..n).collect();
let objects: Vec<ObjectRef> = chunk.iter().map(|(o, _)| *o).collect();
let chunk: Vec<_> = frontier.drain(..n).collect();
let objects: Vec<ObjectRef> = chunk.iter().map(|(o, _, _)| *o).collect();
let got = self.attributes_of(&objects).await?;
for (o, depth) in chunk {
let Some(a) = got.get(&o) else { continue };
for (o, depth, parent) in chunk {
let Some(a) = got.get(&o) else {
if let Some(parent) = parent {
missing.push((parent, o));
} else {
complete = false;
}
continue;
};
let (e, kids) = tree::element(Some(o), a, None, depth);
if (depth > 0 && e.traits & prune != 0) || depth >= 40 {
if depth > 0 && e.traits & prune != 0 {
continue;
}
if depth >= 40 {
complete &= kids.is_empty();
continue;
}
for k in kids {
if seen.insert((k.pid, k.id)) {
frontier.push_back((k, depth + 1));
frontier.push_back((k, depth + 1, Some(o)));
}
}
}
attrs.extend(got);
}
// An attribute error alone cannot distinguish a removed child from an unreadable live one.
// Re-read each affected parent once, only when the rest of the walk completed within budget.
if complete && !missing.is_empty() {
complete = false;
if Instant::now() < deadline {
let parents: HashSet<_> = missing.iter().map(|(parent, _)| *parent).collect();
let parents: Vec<_> = parents.into_iter().collect();
let fresh = self.attributes_of(&parents).await?;
complete = missing.iter().all(|(parent, child)| {
fresh.get(parent).is_some_and(|a| {
let (_, kids) = tree::element(Some(*parent), a, None, 0);
!kids.contains(child) && kids.iter().all(|k| seen.contains(&(k.pid, k.id)))
})
});
}
}
Ok((tree::assemble(root, &attrs), complete))
}

Expand All @@ -433,15 +461,15 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send + 'static> Session<S> {
limit: usize,
budget: Duration,
prune: u64,
) -> Result<Vec<Element>> {
let (mut els, _) = self.walk(root, limit + 1, budget, prune).await?;
) -> Result<(Vec<Element>, bool)> {
let (mut els, complete) = self.walk(root, limit + 1, budget, prune).await?;
if !els.is_empty() {
els.remove(0);
}
for e in &mut els {
e.parent = e.parent.and_then(|p| p.checked_sub(1));
}
Ok(els)
Ok((els, complete))
}

/// Tells the phone this client no longer needs accessibility (it then closes the connection)
Expand Down
20 changes: 14 additions & 6 deletions crates/ictl-daemon/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ impl Daemon {
.list_elements(g, &filter, limit, Duration::from_millis(max_age_ms), keep_locked)
.await
.map(|(text, elements)| {
data = Some(crate::protocol::Data::Elements { elements });
data = Some(elements);
text
}),
Request::Tap { x, y, count } => self.tap(g, x, y, count, view).await,
Expand All @@ -1238,6 +1238,7 @@ impl Daemon {
self.swipe(g, crate::tools::Stroke { from, to, seconds, hold }, view, watched).await
}
Request::TypeText { text, submit } => self.type_text(g, &text, submit).await,
Request::ReplaceText { text, submit } => Box::pin(self.replace_text(g, &text, submit)).await,
Request::PressButton { button } => self.press_button(g, &button, watched).await,
Request::Touch { phase, x, y } => {
return self
Expand Down Expand Up @@ -1268,7 +1269,12 @@ impl Daemon {
data = Some(crate::protocol::Data::Alert { text: alert });
text
}),
Request::WaitFor { text, timeout_ms } => self.wait_for(g, &text, Duration::from_millis(timeout_ms)).await,
Request::WaitFor { text, timeout_ms } => {
self.wait_for(g, &text, Duration::from_millis(timeout_ms), false).await
}
Request::WaitForDisappearance { text, timeout_ms } => {
self.wait_for(g, &text, Duration::from_millis(timeout_ms), true).await
}
Request::Doctor => Ok(self.doctor(g).await),
Request::UnlockReset => {
let _ = std::fs::remove_file(self.cfg.unlock_disabled_flag());
Expand Down Expand Up @@ -1357,6 +1363,7 @@ fn lane(req: &Request) -> Lane {
| Request::LongPress { .. }
| Request::Swipe { .. }
| Request::TypeText { .. }
| Request::ReplaceText { .. }
| Request::PressButton { .. }
| Request::Touch { .. }
| Request::ListApps { .. }
Expand All @@ -1366,6 +1373,7 @@ fn lane(req: &Request) -> Lane {
| Request::Settle { .. }
| Request::AlertCheck
| Request::WaitFor { .. }
| Request::WaitForDisappearance { .. }
| Request::Doctor
| Request::UnlockReset
| Request::ListDevices
Expand Down Expand Up @@ -1484,6 +1492,7 @@ fn admit(req: &Request, queued_at: Instant, ctx: &CallCtx, controlled: Option<&s
}

fn check_request(req: &Request) -> Result<(), String> {
req.check_required_text()?;
match req {
Request::WebEval { expression, timeout_ms, .. }
if expression.trim().is_empty() || *timeout_ms > MAX_WEB_EVAL_MS =>
Expand All @@ -1497,7 +1506,9 @@ fn check_request(req: &Request) -> Result<(), String> {
Request::Settle { max_ms } if *max_ms > MAX_WAIT_MS => {
return Err(ictl_l10n::trf!("max_ms 不能超过 {MAX_WAIT_MS}", "max_ms can't exceed {MAX_WAIT_MS}"));
}
Request::WaitFor { timeout_ms, .. } if *timeout_ms > MAX_WAIT_MS => {
Request::WaitFor { timeout_ms, .. } | Request::WaitForDisappearance { timeout_ms, .. }
if *timeout_ms > MAX_WAIT_MS =>
{
return Err(ictl_l10n::trf!(
"timeout 不能超过 {} 秒",
"timeout can't exceed {} seconds",
Expand All @@ -1507,9 +1518,6 @@ fn check_request(req: &Request) -> Result<(), String> {
Request::Touch { x, y, .. } if !x.is_finite() || !y.is_finite() => {
return Err(ictl_l10n::tr!("x、y 要是有效的数", "x and y must be valid numbers").into());
}
Request::WaitFor { text, .. } if text.is_empty() => {
return Err(ictl_l10n::tr!("text 不能为空", "text can't be empty").into());
}
Request::AppLogs { since_secs, limit, .. } if *since_secs > 3600 || *limit == 0 || *limit > 1000 => {
return Err(ictl_l10n::tr!(
"since 最多 3600 秒,limit 要在 1 到 1000 之间",
Expand Down
10 changes: 9 additions & 1 deletion crates/ictl-daemon/src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,13 @@ fn ended_message() -> &'static str {

/// Requests that show the agent the screen: after one, it may act again ([`TaskState::recheck`])
pub(crate) fn observes(req: &Request) -> bool {
matches!(req, Request::Screenshot { .. } | Request::ListElements { .. } | Request::WaitFor { .. })
matches!(
req,
Request::Screenshot { .. }
| Request::ListElements { .. }
| Request::WaitFor { .. }
| Request::WaitForDisappearance { .. }
)
}

/// The home screen, the lock screen and notifications: always in a task's scope (getting back from elsewhere)
Expand Down Expand Up @@ -219,6 +225,7 @@ pub fn scope_check(req: &Request) -> Option<Check> {
| Request::LongPress { .. }
| Request::Swipe { .. }
| Request::TypeText { .. }
| Request::ReplaceText { .. }
| Request::WebEval { .. }
| Request::Touch { phase: TouchPhase::Down | TouchPhase::Move, .. } => Some(Check::Foreground),
Request::InstallApp { path } => {
Expand All @@ -240,6 +247,7 @@ pub fn scope_check(req: &Request) -> Option<Check> {
| Request::Settle { .. }
| Request::AlertCheck
| Request::WaitFor { .. }
| Request::WaitForDisappearance { .. }
| Request::Daemon
| Request::Doctor
| Request::Resign { .. }
Expand Down
Loading
Loading