From cc902789b28bd2846c944c6e41d57a0d3b4117a2 Mon Sep 17 00:00:00 2001 From: cosine <1726697408@qq.com> Date: Tue, 17 Nov 2026 14:36:35 +0800 Subject: [PATCH] feat(daemon): say when the same action leaves the screen unchanged again An agent stuck on a screen tends to repeat the same tap. Each time the answer is the same "the screen is the same as before the action, don't repeat it", so nothing tells it that this is already the third try. The daemon now remembers the last action as sent (its request JSON) and how many times in a row it left the screen unchanged. When the same request leaves it unchanged again, from the second time on the note adds "this exact action has now left the screen unchanged N times in a row: stop repeating it and try something else, or tell the user where it is stuck". Another action, other parameters or a changed screen start the count again; an action that couldn't be compared (nothing was looked at before it) neither counts nor resets. Regression case #150. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 + CHANGELOG.zh-CN.md | 2 + crates/ictl-daemon/src/core.rs | 4 ++ crates/ictl-daemon/src/tools/mod.rs | 31 +++++++++++++- crates/ictl-daemon/src/tools/observe.rs | 56 ++++++++++++++++++++++++- docs/dev/regression-cases.md | 1 + docs/tools.md | 2 + 7 files changed, 96 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d433e42..5139aab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ What changed in each release, one line per change. Versions follow [Semantic Ver ### Improvements +- When the same action leaves the screen unchanged again and again, the result says how many times in a row and to try something else. + - An action that had to unlock the phone first says so, and how long it took (#135). ### Fixes diff --git a/CHANGELOG.zh-CN.md b/CHANGELOG.zh-CN.md index fa578f6..27ee2a1 100644 --- a/CHANGELOG.zh-CN.md +++ b/CHANGELOG.zh-CN.md @@ -8,6 +8,8 @@ ### 改进 +- 同一个操作原样重做、界面还是没变时,结果里说明已经连续几次,并提示换个做法。 + - 操作前先自动解锁了的话,结果里会说明,并写明用了多久(#135)。 ### 修复 diff --git a/crates/ictl-daemon/src/core.rs b/crates/ictl-daemon/src/core.rs index 6bc2b5b..d386aa2 100644 --- a/crates/ictl-daemon/src/core.rs +++ b/crates/ictl-daemon/src/core.rs @@ -436,6 +436,9 @@ pub(crate) struct PerDevice { /// 最近一个操作开始前的界面:操作做完、界面稳定时和它比,告诉调用方界面变没变(`Daemon::settle`)。 /// 按名字点击会在点之前读树,用那棵树 pub before_action: Option, + /// 最近一个操作原样是什么(请求的 JSON),和它连续几次做完界面都没变:同一个操作原样重做、界面还是不变, + /// 结果里说是第几次了([`crate::tools::Repeats`]) + pub repeats: crate::tools::Repeats, /// 最近一个操作开始的时间:原生控制等界面稳定时,从这之后的推送算起 pub acted_at: Option, /// How long the request being handled spent unlocking the phone first ([`Daemon::ensure_unlocked`]); taken by @@ -1168,6 +1171,7 @@ impl Daemon { if acted { // 这个操作之前读过的界面就是「操作前」;操作期间读的(按名字点击先读树)也是操作前,操作完都不再算 g.dev.before_action = g.dev.seen.take_fresh(); + g.dev.repeats.start(&req); // 原生控制等界面稳定时,从这之后手机推来的树算起 g.dev.acted_at = Some(Instant::now()); } diff --git a/crates/ictl-daemon/src/tools/mod.rs b/crates/ictl-daemon/src/tools/mod.rs index a243b6c..7c9a04f 100644 --- a/crates/ictl-daemon/src/tools/mod.rs +++ b/crates/ictl-daemon/src/tools/mod.rs @@ -24,8 +24,8 @@ use crate::notify::Event; use crate::protocol::{ALERT_BLOCKING, Response, TAP_MISSED, TouchPhase}; use crate::protocol::{ElementData, StatusData}; pub(crate) use native::AX_WAIT; -pub(crate) use observe::Seen; use observe::*; +pub(crate) use observe::{Repeats, Seen}; /// 锁屏后至少等这么久再自动解锁:锁屏过渡没走完就唤醒,WDA 第一下上滑卡约 16 秒后报 `point.x != INFINITY` pub(crate) const LOCK_SETTLE: Duration = Duration::from_secs(3); @@ -484,6 +484,35 @@ mod tests { assert!(super::observe::change_text(Change::Unknown).is_empty()); } + /// 同一个操作原样重做、界面还是没变,从第二次起说是第几次;换了操作、参数不同、界面变了都重新数, + /// 没法比(操作前没看过界面)的那次不数也不清零 + #[test] + fn the_same_action_leaving_the_screen_unchanged_is_counted() { + use super::observe::{Change, Repeats, repeat_text}; + use crate::protocol::Request; + let tap = |x| Request::Tap { x, y: 640.0 }; + let mut r = Repeats::default(); + let mut run = |req: &Request, c| { + r.start(req); + r.settled(c) + }; + assert_eq!(run(&tap(232.0), Change::Same), 1); + assert_eq!(run(&tap(232.0), Change::Same), 2); + assert_eq!(run(&tap(232.0), Change::Unknown), 0); + assert_eq!(run(&tap(232.0), Change::Same), 3); + // another point is another action + assert_eq!(run(&tap(100.0), Change::Same), 1); + assert_eq!(run(&tap(100.0), Change::Changed), 0); + assert_eq!(run(&tap(100.0), Change::Same), 1); + // something else in between starts the count again + assert_eq!(run(&Request::PressButton { button: "home".into() }, Change::Unknown), 0); + assert_eq!(run(&tap(100.0), Change::Same), 1); + // a settle without an action counts nothing + assert_eq!(r.settled(Change::Same), 0); + assert!(repeat_text(0).is_empty() && repeat_text(1).is_empty()); + assert!(repeat_text(3).contains('3')); + } + /// 太久以前看过的界面不拿来比;拿走以后就没了 #[test] fn seen_screens_are_taken_once() { diff --git a/crates/ictl-daemon/src/tools/observe.rs b/crates/ictl-daemon/src/tools/observe.rs index 3eac074..6956a77 100644 --- a/crates/ictl-daemon/src/tools/observe.rs +++ b/crates/ictl-daemon/src/tools/observe.rs @@ -152,7 +152,9 @@ impl Daemon { g.dev.seen.saw_thumb(t.clone()); thumb = Some(t); } - change_text(compare(&before, tree, thumb.as_deref())) + let change = compare(&before, tree, thumb.as_deref()); + let n = g.dev.repeats.settled(change); + change_text(change) + &repeat_text(n) } pub(super) async fn settle_visually( @@ -407,6 +409,58 @@ pub(super) fn change_text(c: Change) -> String { } } +/// 同一个操作原样连续做了几次、界面都没变。操作开始时 [`Self::start`],等界面稳定比较完 [`Self::settled`] +#[derive(Debug, Default)] +pub(crate) struct Repeats { + /// 正在做的操作 + acting: Option, + /// 上一个做完界面没变的操作,和它连续没变的次数 + same: Option<(String, u32)>, +} + +impl Repeats { + /// 一个操作开始:换了操作(参数不同也算)就重新数 + pub(crate) fn start(&mut self, req: &crate::protocol::Request) { + let sig = serde_json::to_string(req).unwrap_or_default(); + if self.same.as_ref().is_some_and(|(s, _)| *s != sig) { + self.same = None; + } + self.acting = Some(sig); + } + + /// 操作做完、和操作前比过:界面没变时返回这是连续第几次(1 起),变了或没法比时为 0 + pub(super) fn settled(&mut self, change: Change) -> u32 { + let Some(sig) = self.acting.take() else { return 0 }; + match change { + Change::Same => { + let n = match &self.same { + Some((s, n)) if *s == sig => n + 1, + _ => 1, + }; + self.same = Some((sig, n)); + n + } + Change::Changed => { + self.same = None; + 0 + } + // 没法比(操作前没看过界面):不数这一次,也不清零 + Change::Unknown => 0, + } + } +} + +/// 同一个操作原样做了 `n` 次界面都没变(第二次起):明说在原地打转,要换做法 +pub(super) fn repeat_text(n: u32) -> String { + if n < 2 { + return String::new(); + } + ictl_l10n::trf!( + ";这已经是连续第 {n} 次原样做这个操作、界面都没变:别再重复了,换个做法(截图看清目标在哪、换个位置或改用按名字点、先关掉挡住的东西),还不行就告诉用户卡在了哪一步", + "; this exact action has now left the screen unchanged {n} times in a row: stop repeating it and try something else (look at a screenshot to find the target, tap elsewhere or by text, close whatever is in the way), or tell the user where it is stuck" + ) +} + /// An element as data: the text it shows (its label, else its name, else its value) and its box pub(super) fn element_data(e: &elements::Element) -> ElementData { let text = [&e.label, &e.name, &e.value].into_iter().find(|s| !s.is_empty()).cloned().unwrap_or_default(); diff --git a/docs/dev/regression-cases.md b/docs/dev/regression-cases.md index 5f0a9f3..e928ca7 100644 --- a/docs/dev/regression-cases.md +++ b/docs/dev/regression-cases.md @@ -149,3 +149,4 @@ | 143 | 原生:Spotlight 打开时选中上次的搜索词,再粘贴同样的词会把它替换成自己,输入框的值不变,`iphone_type_text` 报「粘贴了…但输入框里没有这段文字」(带 `submit` 时还不按回车),其实已经输进去了 | 「允许粘贴」点过之后,输入框原来就有这段文字、1.5 秒不变,算输入成功并提示截图确认;没点过「允许粘贴」时仍报错 | 单测 `a_paste_that_replaced_the_same_text_is_told_apart_from_one_that_missed` + 真机(Spotlight 留着「设置」时再输入「设置」,`run.py` 连跑两遍);真机脚本 `scenarios.py typing` | | 144 | 设备闲置时自动锁屏,下一个操作先自动解锁(iPad 经 WDA 约 15 秒,iPhone 原生约 9 秒),结果和事件日志都没说,看着像「切换设备后第一个操作慢」 | 结果第一行(截图在说明里、出错时在报错里)写「手机锁着,先自动解锁了,用时 X 秒」,事件日志记「已自动解锁」 | 单测 `an_unlock_first_is_said_on_the_answers_first_line` + 真机(锁屏后按名字点击、截图,两台设备);真机脚本 `scenarios.py lock` | | 145 | WDA 的短探测已正常应答或返回远端错误,却不清零超时计数,三个独立慢请求仍会触发重启;连接失败反而清零。日志还把长请求和短探测均未应答写成「连续 3 次」、宣称尚未检查的 `/status` 正常,请求超时本身缺端点日志 | 只有正常响应或远端错误清零,连接失败不清零;连续无应答和长请求后的失败探测分别记录原因及实际次数;超时诊断记录方法、短路径和耗时,不记录查询参数或正文 | 单测 `answered_probe_resets_stall_count_after_each_slow_request`、`connection_error_does_not_reset_unanswered_count`、`long_request_and_failed_probe_keep_the_actual_count_and_reason`、`three_short_unanswered_requests_report_a_consecutive_stall`、`answered_session_delete_does_not_mask_a_failed_probe`、`failed_request_log_keeps_endpoint_and_time_without_query`、`successful_request_log_does_not_include_response_body`;真机:同一 iPad、同一 WDA,把只读业务请求的客户端时限缩短到 20ms,短探测保留 5s,对照修复前后各 3 轮 | +| 150 | Agent 在原地打转:同一个点击原样做了好几次、界面一直没变,每次的结果都是同一句「界面和操作前一样…不要原样再做一遍」,看不出已经是第几次了 | 守护进程记下最近一个操作的原样请求和它连续几次做完界面没变:同一个请求再次没变时,从第二次起在这句后面说「这已经是连续第 N 次…换个做法,还不行就告诉用户卡在了哪一步」;换了操作、参数不同或界面变了重新数,没法比(操作前没看过界面)的那次不数也不清零 | 单测 `the_same_action_leaving_the_screen_unchanged_is_counted` + 真机(iPhone 12 mini,iOS 26.4.1:主屏幕空白处原样点 4 次,第 2~4 次带次数;换个点、再点回来、点开「设置」都不带)| diff --git a/docs/tools.md b/docs/tools.md index c7ae63c..7dc851a 100644 --- a/docs/tools.md +++ b/docs/tools.md @@ -88,6 +88,8 @@ The tools under *Acting* and `iphone_batch` accept the following options. `iphon "Settled" means two consecutive element trees match and no loading spinner is shown. On screens with few elements (games, maps) it compares screenshots instead. The result says how long it waited, whether something is probably still loading, and whether a dialog is in the way. +If the screen was looked at before the action (a screenshot, the element list, or the previous action's `then`), the result also says whether the screen changed. When exactly the same action (same tool and parameters) leaves the screen unchanged again, from the second time on it says how many times in a row this has happened and to try something else instead, so an agent going round in circles is told so. A different action, different parameters or a changed screen start the count again. + Without `then`, the result still warns when a system alert covers the screen, because taps and swipes would land on the alert. ## Batches: `iphone_batch`