1use std::path::Path;
14
15use serde::{Deserialize, Serialize};
16use smix_adapter_maestro::Step;
17use smix_error::{ExpectationFailure, FailureCode, FailureInit};
18use smix_selector::Selector;
19
20pub use smix_ai_tier::AiTierConfig;
24
25#[derive(Clone, Debug, Serialize, Deserialize)]
27pub struct Proposal {
28 pub edits: Vec<ProposalEdit>,
30}
31
32#[derive(Clone, Debug, Serialize, Deserialize)]
37#[serde(tag = "op", rename_all = "camelCase")]
38pub enum ProposalEdit {
39 ReplaceSelector {
41 step_index: usize,
43 new_selector: Selector,
45 },
46 InsertStep {
49 before_index: usize,
51 step: Step,
53 },
54 ReorderStep {
56 from_index: usize,
58 to_index: usize,
60 },
61 ReplaceStep {
64 step_index: usize,
66 new_step: Step,
68 },
69}
70
71#[derive(Clone, Debug, PartialEq, Eq)]
73pub enum ProposalError {
74 IndexOutOfRange {
76 index: usize,
78 flow_len: usize,
80 },
81 InsertNotWait {
83 verb: &'static str,
85 },
86}
87
88impl Proposal {
89 pub fn validate(&self, flow_len: usize) -> Result<(), ProposalError> {
98 for edit in &self.edits {
99 match edit {
100 ProposalEdit::ReplaceSelector { step_index, .. }
101 | ProposalEdit::ReplaceStep { step_index, .. } => {
102 check_in_range(*step_index, flow_len)?;
103 }
104 ProposalEdit::ReorderStep {
105 from_index,
106 to_index,
107 } => {
108 check_in_range(*from_index, flow_len)?;
109 check_in_range(*to_index, flow_len)?;
110 }
111 ProposalEdit::InsertStep { before_index, step } => {
112 if *before_index > flow_len {
113 return Err(ProposalError::IndexOutOfRange {
114 index: *before_index,
115 flow_len,
116 });
117 }
118 match step {
119 Step::ExtendedWaitUntil { .. } | Step::WaitForAnimationToEnd { .. } => {}
120 other => {
121 return Err(ProposalError::InsertNotWait {
122 verb: step_verb(other),
123 });
124 }
125 }
126 }
127 }
128 }
129 Ok(())
130 }
131}
132
133fn check_in_range(index: usize, flow_len: usize) -> Result<(), ProposalError> {
134 if index < flow_len {
135 Ok(())
136 } else {
137 Err(ProposalError::IndexOutOfRange { index, flow_len })
138 }
139}
140
141pub fn parse_proposal_reply(reply: &str) -> Option<Proposal> {
144 smix_ai_tier::parse_json_object::<Proposal>(reply)
145}
146
147pub async fn propose_from_bundle(
159 flow_path: &Path,
160 bundle_dir: &Path,
161 cfg: &AiTierConfig,
162) -> Result<Proposal, ExpectationFailure> {
163 let prompt = format!(
164 "A smix test flow failed. Read the original flow at {flow} and the run \
165 artifacts in {bundle}: run-summary.json (per-step verdicts), \
166 failure.json (the structured failure with selector, suggestions, and \
167 visible elements), any *.fail.tree.json (the accessibility tree at the \
168 failing step), and any .png screenshots.\n\n\
169 Propose the edits that would make the flow pass. Reply with one JSON \
170 object and nothing else:\n\
171 {{\"edits\": [ <edit>, ... ]}}\n\n\
172 The single most reliable repair signal is the `suggestions` array in \
173 failure.json (the driver's \"Did you mean ...?\" list for a selector \
174 that matched nothing); prefer swapping the failing selector to a \
175 suggested one. `*.fail.tree.json` and .png may be absent — ignore them \
176 if missing rather than treating their absence as an error.\n\n\
177 Each <edit> is exactly one of:\n\
178 {{\"op\": \"replaceSelector\", \"step_index\": <n>, \"new_selector\": <selector>}}\n\
179 {{\"op\": \"insertStep\", \"before_index\": <n>, \"step\": <step>}} (step must be extendedWaitUntil or waitForAnimationToEnd)\n\
180 {{\"op\": \"reorderStep\", \"from_index\": <n>, \"to_index\": <n>}}\n\
181 {{\"op\": \"replaceStep\", \"step_index\": <n>, \"new_step\": <step>}}\n\n\
182 Every index (`step_index`, `before_index`, `from_index`, `to_index`) \
183 is 0-based into the flow's step list: the first step is 0. Do not use \
184 the 1-based `n` from run-summary.json's `steps[].n`.\n\n\
185 A <selector> is a maestro selector object, e.g. {{\"id\": \"submit-btn\"}} \
186 or {{\"text\": \"Login\"}}. A <step> is a maestro step object, e.g. \
187 {{\"tapOn\": {{\"selector\": {{\"id\": \"submit-btn\"}}}}}} or \
188 {{\"extendedWaitUntil\": {{\"selector\": {{\"id\": \"spinner\"}}, \"timeout_ms\": 5000, \"expect_visible\": false}}}}.",
189 flow = flow_path.display(),
190 bundle = bundle_dir.display(),
191 );
192
193 let reply = smix_ai_tier::ask(prompt, cfg).await?;
197
198 parse_proposal_reply(&reply).ok_or_else(|| {
199 driver_error(
200 format!(
201 "authoring-propose: no proposal in the reply — wanted a JSON object, got: {}",
202 reply.trim()
203 ),
204 Some(
205 "the model answered but not in the shape asked for; this is not \
206 an empty proposal"
207 .into(),
208 ),
209 )
210 })
211}
212
213#[derive(Clone, Debug, PartialEq, Eq)]
215pub enum ApplyError {
216 Invalid(ProposalError),
219 NoSelector {
224 step_index: usize,
226 verb: &'static str,
228 },
229}
230
231impl std::fmt::Display for ApplyError {
232 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
233 match self {
234 ApplyError::Invalid(e) => write!(f, "proposal failed validation: {e:?}"),
235 ApplyError::NoSelector { step_index, verb } => write!(
236 f,
237 "replaceSelector at step {step_index} targets `{verb}`, which has no selector"
238 ),
239 }
240 }
241}
242
243impl std::error::Error for ApplyError {}
244
245pub fn apply(proposal: &Proposal, steps: &[Step]) -> Result<Vec<Step>, ApplyError> {
260 proposal
261 .validate(steps.len())
262 .map_err(ApplyError::Invalid)?;
263 let mut out = steps.to_vec();
264 for edit in &proposal.edits {
265 match edit {
266 ProposalEdit::ReplaceSelector {
267 step_index,
268 new_selector,
269 } => {
270 set_step_selector(&mut out[*step_index], new_selector.clone()).map_err(|verb| {
271 ApplyError::NoSelector {
272 step_index: *step_index,
273 verb,
274 }
275 })?;
276 }
277 ProposalEdit::InsertStep { before_index, step } => {
278 out.insert(*before_index, step.clone());
279 }
280 ProposalEdit::ReorderStep {
281 from_index,
282 to_index,
283 } => {
284 let s = out.remove(*from_index);
285 out.insert(*to_index, s);
286 }
287 ProposalEdit::ReplaceStep {
288 step_index,
289 new_step,
290 } => {
291 out[*step_index] = new_step.clone();
292 }
293 }
294 }
295 Ok(out)
296}
297
298fn set_step_selector(step: &mut Step, new_selector: Selector) -> Result<(), &'static str> {
299 match step {
300 Step::TapOn { selector, .. }
301 | Step::ExtendedWaitUntil { selector, .. }
302 | Step::AssertVisible { selector }
303 | Step::AssertNotVisible { selector }
304 | Step::InputTextInto { selector, .. }
305 | Step::ScrollUntilVisible { selector, .. }
306 | Step::CopyTextFrom { selector }
307 | Step::DoubleTapOn { selector }
308 | Step::RepeatTap { selector, .. }
309 | Step::LongPressOn { selector, .. } => {
310 *selector = new_selector;
311 Ok(())
312 }
313 other => Err(step_verb(other)),
314 }
315}
316
317fn driver_error(message: String, hint: Option<String>) -> ExpectationFailure {
318 ExpectationFailure::new(FailureInit {
319 code: Some(FailureCode::DriverError),
320 message,
321 hint,
322 ..Default::default()
323 })
324}
325
326#[derive(Debug)]
330pub enum AmendError {
331 ReadFlow(std::io::Error),
333 ParseFlow(String),
335 Propose(Box<ExpectationFailure>),
339 Apply(ApplyError),
341 Emit(smix_adapter_maestro::EmitError),
343}
344
345pub async fn propose_and_amend(
354 flow_path: &Path,
355 bundle_dir: &Path,
356 cfg: &AiTierConfig,
357) -> Result<String, AmendError> {
358 let yaml = std::fs::read_to_string(flow_path).map_err(AmendError::ReadFlow)?;
359 let flow = smix_adapter_maestro::parse_flow_yaml(&yaml)
360 .map_err(|e| AmendError::ParseFlow(e.to_string()))?;
361
362 let proposal = propose_from_bundle(flow_path, bundle_dir, cfg)
363 .await
364 .map_err(|e| AmendError::Propose(Box::new(e)))?;
365
366 let amended = apply(&proposal, &flow.steps).map_err(AmendError::Apply)?;
367 smix_adapter_maestro::emit_flow_yaml(&amended, &flow.app_id).map_err(AmendError::Emit)
368}
369
370fn step_verb(step: &Step) -> &'static str {
371 match step {
372 Step::TapOn { .. } => "tapOn",
373 Step::TapAtPoint { .. } => "tapAtPoint",
374 Step::WebViewEval { .. } => "webViewEval",
375 Step::WaitForAnimationToEnd { .. } => "waitForAnimationToEnd",
376 Step::ExtendedWaitUntil { .. } => "extendedWaitUntil",
377 Step::AssertVisible { .. } => "assertVisible",
378 Step::InputText(_) => "inputText",
379 Step::InputTextInto { .. } => "inputTextInto",
380 Step::PressKey(_) => "pressKey",
381 Step::Back(_) => "back",
382 Step::RunFlow(_) => "runFlow",
383 Step::RunFlowConditional { .. } => "runFlowConditional",
384 Step::RunFlowInline { .. } => "runFlowInline",
385 Step::ScrollUntilVisible { .. } => "scrollUntilVisible",
386 Step::EraseText(_) => "eraseText",
387 Step::Swipe { .. } => "swipe",
388 Step::LaunchApp { .. } => "launchApp",
389 _ => "step",
390 }
391}