ui-automata 0.1.0

Declarative Workflow Engine for UI Automation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
use schemars::JsonSchema;
use serde::Deserialize;

use std::collections::HashMap;

use crate::{
    AutomataError, Browser, Desktop, Element, SelectorPath, ShadowDom, action::sub_output,
    output::Output,
};

// ── Text / title match helpers ────────────────────────────────────────────────

/// Matches element text. Exactly one field should be set.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct TextMatch {
    pub exact: Option<String>,
    pub contains: Option<String>,
    pub starts_with: Option<String>,
    /// Fancy-regex pattern (supports backreferences, lookahead, etc.).
    pub regex: Option<String>,
    #[serde(default)]
    pub non_empty: bool,
}

impl TextMatch {
    pub fn exact(s: impl Into<String>) -> Self {
        Self {
            exact: Some(s.into()),
            contains: None,
            starts_with: None,
            regex: None,
            non_empty: false,
        }
    }
    pub fn contains(s: impl Into<String>) -> Self {
        Self {
            exact: None,
            contains: Some(s.into()),
            starts_with: None,
            regex: None,
            non_empty: false,
        }
    }
    pub fn non_empty() -> Self {
        Self {
            exact: None,
            contains: None,
            starts_with: None,
            regex: None,
            non_empty: true,
        }
    }

    pub fn test(&self, s: &str) -> bool {
        if let Some(v) = &self.exact {
            return s == v;
        }
        if let Some(v) = &self.contains {
            return s.contains(v.as_str());
        }
        if let Some(v) = &self.starts_with {
            return s.starts_with(v.as_str());
        }
        if let Some(v) = &self.regex {
            return fancy_regex::Regex::new(v)
                .ok()
                .and_then(|re| re.is_match(s).ok())
                .unwrap_or(false);
        }
        if self.non_empty {
            return !s.is_empty();
        }
        false
    }
}

/// Matches a window title. Exactly one field should be set.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct TitleMatch {
    pub exact: Option<String>,
    pub contains: Option<String>,
    pub starts_with: Option<String>,
}

impl TitleMatch {
    pub fn exact(s: impl Into<String>) -> Self {
        Self {
            exact: Some(s.into()),
            contains: None,
            starts_with: None,
        }
    }
    pub fn contains(s: impl Into<String>) -> Self {
        Self {
            exact: None,
            contains: Some(s.into()),
            starts_with: None,
        }
    }
    pub fn starts_with(s: impl Into<String>) -> Self {
        Self {
            exact: None,
            contains: None,
            starts_with: Some(s.into()),
        }
    }

    pub fn test(&self, s: &str) -> bool {
        if let Some(v) = &self.exact {
            return s == v;
        }
        if let Some(v) = &self.contains {
            return s.contains(v.as_str());
        }
        if let Some(v) = &self.starts_with {
            return s.starts_with(v.as_str());
        }
        false
    }
}

/// Key written to locals by every `Exec` action — holds the integer exit code as a string.
/// Read by the [`Condition::ExecSucceeded`] condition.
pub const EXEC_EXIT_CODE_KEY: &str = "__exec_exit_code__";

// ── WindowState ───────────────────────────────────────────────────────────────

/// Observable state of a window anchor.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum WindowState {
    /// The window is currently the OS foreground window (has keyboard focus).
    Active,
    /// The window is visible on screen — not minimized or hidden.
    Visible,
}

// ── Condition ─────────────────────────────────────────────────────────────────

/// Custom `Deserialize` via `TryFrom<serde_yaml::Value>` to work around the
/// serde limitation that `#[serde(tag)]` + `#[serde(flatten)]` don't compose
/// in serde_yaml. We hand-roll the mapping from a YAML map to enum variants.
#[derive(Debug, Clone, Deserialize)]
#[serde(try_from = "serde_yaml::Value")]
pub enum Condition {
    ElementFound {
        scope: String,
        selector: SelectorPath,
    },
    ElementEnabled {
        scope: String,
        selector: SelectorPath,
    },
    ElementVisible {
        scope: String,
        selector: SelectorPath,
    },
    ElementHasText {
        scope: String,
        selector: SelectorPath,
        pattern: TextMatch,
    },
    ElementHasChildren {
        scope: String,
        selector: SelectorPath,
    },

    /// Any application window matches the given attribute filters.
    /// YAML: `type: WindowWithAttribute` + at least one of:
    ///   - flat title fields: `exact`, `contains`, `starts_with`
    ///   - `automation_id: <string>` (exact match on UIA AutomationId / AXIdentifier)
    ///   - `pid: <u32>` (exact process ID match)
    /// Optional `process: <name>` restricts to a specific process (case-insensitive, no .exe).
    WindowWithAttribute {
        title: Option<TitleMatch>,
        automation_id: Option<String>,
        pid: Option<u32>,
        process: Option<String>,
    },

    /// True when any application window belongs to a process whose name
    /// (without `.exe`) matches `process` (case-insensitive).
    /// YAML: `type: ProcessRunning` + `process: <name>`.
    ProcessRunning {
        process: String,
    },
    /// True when the window anchored to `anchor` is no longer open.
    /// If the anchor was mounted with a PID (`AnchorDef::session_pid`), the
    /// check is done at the process level (no window for that PID exists).
    /// Otherwise it falls back to attempting re-resolution of the anchor.
    WindowClosed {
        anchor: String,
    },
    /// True when the anchor's window is in the given state.
    WindowWithState {
        anchor: String,
        state: WindowState,
    },
    DialogPresent {
        scope: String,
    },
    DialogAbsent {
        scope: String,
    },

    ForegroundIsDialog {
        scope: String,
        title: Option<TitleMatch>,
    },

    /// True when the file at `path` exists on disk.
    /// `path` supports `{output.*}` substitution via `apply_output`.
    FileExists {
        path: String,
    },

    /// Always evaluates to true immediately. Use as `expect` on steps where
    /// success is guaranteed by the action itself (e.g. `Capture`, `NoOp`).
    Always,

    /// True when the most recent `Exec` action exited with code 0.
    /// Fails (step times out) if the process exited non-zero.
    ExecSucceeded,

    /// Evaluates a boolean expression against the current output, locals, and params.
    /// The expression **must** return a `Bool` (use a comparison operator).
    /// Example: `"count % 10 == 0"`, `"score >= param.threshold"`
    EvalCondition {
        expr: String,
    },

    /// True when the browser tab anchored to `scope` matches the given attribute filters.
    /// YAML: `type: TabWithAttribute` + at least one of:
    ///   - `title`: `TextMatch` against the tab's current title.
    ///   - `url`: `TextMatch` against the tab's current URL.
    /// `scope` must name a mounted `Tab` anchor.
    TabWithAttribute {
        scope: String,
        title: Option<TextMatch>,
        url: Option<TextMatch>,
    },

    /// True when the JS expression `expr` evaluates to a truthy value in the browser tab `scope`.
    /// YAML: `type: TabWithState` + `scope` (Tab anchor) + `expr` (JS expression string).
    /// Example: `expr: "document.readyState === 'complete'"`
    TabWithState {
        scope: String,
        expr: String,
    },

    AllOf {
        conditions: Vec<Condition>,
    },
    AnyOf {
        conditions: Vec<Condition>,
    },
    Not {
        condition: Box<Condition>,
    },
}

// ── Custom TryFrom for serde_yaml::Value ──────────────────────────────────────

impl TryFrom<serde_yaml::Value> for Condition {
    type Error = String;

    fn try_from(v: serde_yaml::Value) -> Result<Self, String> {
        let map = v.as_mapping().ok_or("Condition must be a YAML mapping")?;

        let type_str = map
            .get("type")
            .and_then(|v| v.as_str())
            .ok_or("Condition missing string field 'type'")?;

        let str_field = |key: &str| -> Option<String> {
            map.get(key).and_then(|v| v.as_str()).map(String::from)
        };
        let req_str = |key: &str| -> Result<String, String> {
            str_field(key).ok_or_else(|| format!("Condition '{type_str}' missing '{key}'"))
        };
        let req_selector = |key: &str| -> Result<SelectorPath, String> {
            let s = req_str(key)?;
            SelectorPath::parse(&s).map_err(|e| e.to_string())
        };

        match type_str {
            "ElementFound" => Ok(Condition::ElementFound {
                scope: req_str("scope")?,
                selector: req_selector("selector")?,
            }),
            "ElementEnabled" => Ok(Condition::ElementEnabled {
                scope: req_str("scope")?,
                selector: req_selector("selector")?,
            }),
            "ElementVisible" => Ok(Condition::ElementVisible {
                scope: req_str("scope")?,
                selector: req_selector("selector")?,
            }),
            "ElementHasText" => {
                let pattern_val = map
                    .get("pattern")
                    .ok_or("ElementHasText missing 'pattern'")?;
                let pattern: TextMatch = serde_yaml::from_value(pattern_val.clone())
                    .map_err(|e| format!("ElementHasText.pattern: {e}"))?;
                Ok(Condition::ElementHasText {
                    scope: req_str("scope")?,
                    selector: req_selector("selector")?,
                    pattern,
                })
            }
            "ElementHasChildren" => Ok(Condition::ElementHasChildren {
                scope: req_str("scope")?,
                selector: req_selector("selector")?,
            }),
            "WindowWithAttribute" => {
                let title: Option<TitleMatch> = map
                    .get("title")
                    .and_then(|v| serde_yaml::from_value(v.clone()).ok());
                let automation_id = str_field("automation_id");
                let pid = map.get("pid").and_then(|v| v.as_u64()).map(|v| v as u32);
                if title.is_none() && automation_id.is_none() && pid.is_none() {
                    return Err(
                        "WindowWithAttribute requires at least one of: title, automation_id, pid"
                            .into(),
                    );
                }
                Ok(Condition::WindowWithAttribute {
                    title,
                    automation_id,
                    pid,
                    process: str_field("process"),
                })
            }
            "ProcessRunning" => Ok(Condition::ProcessRunning {
                process: req_str("process")?,
            }),
            "WindowClosed" => Ok(Condition::WindowClosed {
                anchor: req_str("anchor")?,
            }),
            "WindowWithState" => {
                let anchor = req_str("anchor")?;
                let state_str = req_str("state")?;
                let state = match state_str.as_str() {
                    "active" => WindowState::Active,
                    "visible" => WindowState::Visible,
                    other => return Err(format!("unknown WindowState '{other}'")),
                };
                Ok(Condition::WindowWithState { anchor, state })
            }
            "DialogPresent" => Ok(Condition::DialogPresent {
                scope: req_str("scope")?,
            }),
            "DialogAbsent" => Ok(Condition::DialogAbsent {
                scope: req_str("scope")?,
            }),
            "ForegroundIsDialog" => {
                let title = if let Some(t) = map.get("title") {
                    Some(
                        serde_yaml::from_value(t.clone())
                            .map_err(|e| format!("ForegroundIsDialog.title: {e}"))?,
                    )
                } else {
                    None
                };
                Ok(Condition::ForegroundIsDialog {
                    scope: req_str("scope")?,
                    title,
                })
            }
            "FileExists" => Ok(Condition::FileExists {
                path: req_str("path")?,
            }),
            "AllOf" => {
                let conditions = parse_condition_list(map, "conditions", type_str)?;
                Ok(Condition::AllOf { conditions })
            }
            "AnyOf" => {
                let conditions = parse_condition_list(map, "conditions", type_str)?;
                Ok(Condition::AnyOf { conditions })
            }
            "Not" => {
                let inner_val = map
                    .get("condition")
                    .ok_or("Not missing 'condition'")?
                    .clone();
                let condition = Box::new(Condition::try_from(inner_val)?);
                Ok(Condition::Not { condition })
            }
            "TabWithAttribute" => {
                let title: Option<TextMatch> = map
                    .get("title")
                    .and_then(|v| serde_yaml::from_value(v.clone()).ok());
                let url: Option<TextMatch> = map
                    .get("url")
                    .and_then(|v| serde_yaml::from_value(v.clone()).ok());
                if title.is_none() && url.is_none() {
                    return Err("TabWithAttribute requires at least one of: title, url".into());
                }
                Ok(Condition::TabWithAttribute {
                    scope: req_str("scope")?,
                    title,
                    url,
                })
            }
            "TabWithState" => Ok(Condition::TabWithState {
                scope: req_str("scope")?,
                expr: req_str("expr")?,
            }),
            "Always" => Ok(Condition::Always),
            "ExecSucceeded" => Ok(Condition::ExecSucceeded),
            "EvalCondition" => {
                let expr = map
                    .get("expr")
                    .and_then(|v| v.as_str())
                    .ok_or("EvalCondition missing 'expr'")?
                    .to_string();
                Ok(Condition::EvalCondition { expr })
            }
            other => Err(format!("unknown Condition type '{other}'")),
        }
    }
}

fn parse_condition_list(
    map: &serde_yaml::Mapping,
    key: &str,
    type_str: &str,
) -> Result<Vec<Condition>, String> {
    let seq = map
        .get(key)
        .and_then(|v| v.as_sequence())
        .ok_or_else(|| format!("{type_str} missing sequence field '{key}'"))?;
    seq.iter().map(|v| Condition::try_from(v.clone())).collect()
}

// ── describe / scope_name / evaluate ─────────────────────────────────────────

impl Condition {
    /// Return a clone with all `{output.<key>}` tokens substituted in pattern strings.
    pub fn apply_output(&self, locals: &HashMap<String, String>, output: &Output) -> Self {
        let sub = |s: &str| sub_output(s, locals, output);
        let sub_tm = |tm: &TextMatch| TextMatch {
            exact: tm.exact.as_deref().map(|s| sub(s)),
            contains: tm.contains.as_deref().map(|s| sub(s)),
            starts_with: tm.starts_with.as_deref().map(|s| sub(s)),
            regex: tm.regex.clone(),
            non_empty: tm.non_empty,
        };
        match self {
            Condition::ElementHasText {
                scope,
                selector,
                pattern,
            } => Condition::ElementHasText {
                scope: scope.clone(),
                selector: selector.clone(),
                pattern: sub_tm(pattern),
            },
            Condition::AllOf { conditions } => Condition::AllOf {
                conditions: conditions
                    .iter()
                    .map(|c| c.apply_output(locals, output))
                    .collect(),
            },
            Condition::AnyOf { conditions } => Condition::AnyOf {
                conditions: conditions
                    .iter()
                    .map(|c| c.apply_output(locals, output))
                    .collect(),
            },
            Condition::FileExists { path } => Condition::FileExists { path: sub(path) },
            Condition::Not { condition } => Condition::Not {
                condition: Box::new(condition.apply_output(locals, output)),
            },
            Condition::TabWithAttribute { scope, title, url } => Condition::TabWithAttribute {
                scope: scope.clone(),
                title: title.as_ref().map(|t| sub_tm(t)),
                url: url.as_ref().map(|u| sub_tm(u)),
            },
            Condition::TabWithState { scope, expr } => Condition::TabWithState {
                scope: scope.clone(),
                expr: sub(expr),
            },
            _ => self.clone(),
        }
    }

    pub fn scope_name(&self) -> Option<&str> {
        match self {
            Condition::ElementFound { scope, .. }
            | Condition::ElementEnabled { scope, .. }
            | Condition::ElementVisible { scope, .. }
            | Condition::ElementHasText { scope, .. }
            | Condition::ElementHasChildren { scope, .. }
            | Condition::DialogPresent { scope }
            | Condition::DialogAbsent { scope }
            | Condition::ForegroundIsDialog { scope, .. } => Some(scope),
            _ => None,
        }
    }

    pub fn describe(&self) -> String {
        match self {
            Condition::ElementFound { scope, selector } => {
                format!("ElementFound({scope}:{selector})")
            }
            Condition::ElementEnabled { scope, selector } => {
                format!("ElementEnabled({scope}:{selector})")
            }
            Condition::ElementVisible { scope, selector } => {
                format!("ElementVisible({scope}:{selector})")
            }
            Condition::ElementHasText {
                scope, selector, ..
            } => {
                format!("ElementHasText({scope}:{selector})")
            }
            Condition::ElementHasChildren { scope, selector } => {
                format!("ElementHasChildren({scope}:{selector})")
            }
            Condition::WindowWithAttribute {
                title,
                automation_id,
                pid,
                process,
            } => {
                let mut parts = Vec::new();
                if let Some(t) = title {
                    parts.push(format!("{t:?}"));
                }
                if let Some(aid) = automation_id {
                    parts.push(format!("automation_id={aid}"));
                }
                if let Some(p) = pid {
                    parts.push(format!("pid={p}"));
                }
                if let Some(p) = process {
                    parts.push(format!("process={p}"));
                }
                format!("WindowWithAttribute({})", parts.join(", "))
            }
            Condition::ProcessRunning { process } => format!("ProcessRunning({process})"),
            Condition::WindowClosed { anchor } => format!("WindowClosed({anchor})"),
            Condition::WindowWithState { anchor, state } => {
                format!("WindowWithState({anchor}:{state:?})")
            }
            Condition::DialogPresent { scope } => format!("DialogPresent({scope})"),
            Condition::DialogAbsent { scope } => format!("DialogAbsent({scope})"),
            Condition::ForegroundIsDialog { scope, .. } => {
                format!("ForegroundIsDialog({scope})")
            }
            Condition::Always => "Always".to_string(),
            Condition::ExecSucceeded => "ExecSucceeded".to_string(),
            Condition::AllOf { conditions } => format!(
                "AllOf({})",
                conditions
                    .iter()
                    .map(|c| c.describe())
                    .collect::<Vec<_>>()
                    .join(", ")
            ),
            Condition::AnyOf { conditions } => format!(
                "AnyOf({})",
                conditions
                    .iter()
                    .map(|c| c.describe())
                    .collect::<Vec<_>>()
                    .join(", ")
            ),
            Condition::FileExists { path } => format!("FileExists({path})"),
            Condition::Not { condition } => format!("Not({})", condition.describe()),
            Condition::EvalCondition { expr } => format!("EvalCondition({expr:?})"),
            Condition::TabWithAttribute { scope, .. } => format!("TabWithAttribute({scope})"),
            Condition::TabWithState { scope, expr } => {
                format!("TabWithState({scope}: {expr:?})")
            }
        }
    }

    pub fn evaluate<D: Desktop>(
        &self,
        dom: &mut ShadowDom<D>,
        desktop: &D,
        locals: &std::collections::HashMap<String, String>,
        params: &std::collections::HashMap<String, String>,
        output: &crate::Output,
    ) -> Result<bool, AutomataError> {
        match self {
            Condition::ElementFound { scope, selector } => {
                Ok(find_in_scope(dom, desktop, scope, selector)?.is_some())
            }
            Condition::ElementEnabled { scope, selector } => {
                Ok(find_in_scope(dom, desktop, scope, selector)?
                    .and_then(|el| el.is_enabled().ok())
                    .unwrap_or(false))
            }
            Condition::ElementVisible { scope, selector } => {
                Ok(find_in_scope(dom, desktop, scope, selector)?
                    .and_then(|el| el.is_visible().ok())
                    .unwrap_or(false))
            }
            Condition::ElementHasText {
                scope,
                selector,
                pattern,
            } => Ok(find_in_scope(dom, desktop, scope, selector)?
                .and_then(|el| el.text().ok())
                .map(|t| pattern.test(&t))
                .unwrap_or(false)),
            Condition::ElementHasChildren { scope, selector } => {
                Ok(find_in_scope(dom, desktop, scope, selector)?
                    .and_then(|el| el.children().ok())
                    .map(|ch| !ch.is_empty())
                    .unwrap_or(false))
            }
            Condition::WindowWithAttribute {
                title,
                automation_id,
                pid,
                process,
            } => {
                let proc_filter = process.as_deref().map(|s| s.to_lowercase());
                Ok(desktop
                    .application_windows()
                    .unwrap_or_default()
                    .iter()
                    .filter(|w| {
                        proc_filter.as_deref().map_or(true, |pf| {
                            w.process_name()
                                .map(|n| n.to_lowercase() == pf)
                                .unwrap_or(false)
                        })
                    })
                    .any(|w| {
                        let title_ok = title
                            .as_ref()
                            .map_or(true, |t| w.name().map(|n| t.test(&n)).unwrap_or(false));
                        let aid_ok = automation_id
                            .as_ref()
                            .map_or(true, |aid| w.automation_id().as_deref() == Some(aid));
                        let pid_ok =
                            pid.map_or(true, |p| w.process_id().map_or(false, |wp| wp == p));
                        title_ok && aid_ok && pid_ok
                    }))
            }
            Condition::ProcessRunning { process } => {
                let target = process.to_lowercase();
                Ok(desktop
                    .application_windows()
                    .unwrap_or_default()
                    .iter()
                    .any(|w| {
                        w.process_name()
                            .map(|n| n.to_lowercase() == target)
                            .unwrap_or(false)
                    }))
            }
            Condition::WindowClosed { anchor } => {
                let windows = desktop.application_windows().unwrap_or_default();
                if let Some(hwnd) = dom.anchor_hwnd(anchor) {
                    // HWND-locked anchor: closed when that specific window is gone.
                    Ok(!windows.iter().any(|w| w.hwnd() == Some(hwnd)))
                } else if let Some(pid) = dom.anchor_pid(anchor) {
                    // PID-only anchor (e.g. single-instance process): closed when
                    // no window exists for that process.
                    Ok(!windows
                        .iter()
                        .any(|w| w.process_id().map_or(false, |p| p == pid)))
                } else {
                    // Unpinned anchor: closed when re-resolution fails.
                    Ok(dom.get(anchor, desktop).is_err())
                }
            }
            Condition::WindowWithState { anchor, state } => {
                let el = match dom.get(anchor, desktop).ok().cloned() {
                    Some(e) => e,
                    None => return Ok(false),
                };
                Ok(match state {
                    WindowState::Active => {
                        let fg = match desktop.foreground_window() {
                            Some(w) => w,
                            None => return Ok(false),
                        };
                        el.process_id().unwrap_or(0) != 0
                            && el.process_id().ok() == fg.process_id().ok()
                    }
                    WindowState::Visible => el.is_visible().unwrap_or(false),
                })
            }
            Condition::DialogPresent { scope } => has_dialog_child(dom, desktop, scope),
            Condition::DialogAbsent { scope } => Ok(!has_dialog_child(dom, desktop, scope)?),
            Condition::ForegroundIsDialog { scope: _, title } => {
                let fg = match desktop.foreground_window() {
                    Some(w) => w,
                    None => return Ok(false),
                };
                if fg.role() != "dialog" {
                    return Ok(false);
                }
                if let Some(tm) = title {
                    if !tm.test(&fg.name().unwrap_or_default()) {
                        return Ok(false);
                    }
                }
                Ok(true)
            }
            Condition::AllOf { conditions } => {
                for c in conditions {
                    if !c.evaluate(dom, desktop, locals, params, output)? {
                        return Ok(false);
                    }
                }
                Ok(true)
            }
            Condition::AnyOf { conditions } => {
                for c in conditions {
                    if c.evaluate(dom, desktop, locals, params, output)? {
                        return Ok(true);
                    }
                }
                Ok(false)
            }
            Condition::Always => Ok(true),
            Condition::ExecSucceeded => {
                Ok(locals.get(EXEC_EXIT_CODE_KEY).map(String::as_str) == Some("0"))
            }
            Condition::FileExists { path } => Ok(std::path::Path::new(path).exists()),
            Condition::Not { condition } => {
                Ok(!condition.evaluate(dom, desktop, locals, params, output)?)
            }
            Condition::EvalCondition { expr } => {
                crate::expression::eval_bool_expr(expr, locals, params, output)
                    .map_err(|e| AutomataError::Internal(format!("EvalCondition: {e}")))
            }
            Condition::TabWithAttribute { scope, title, url } => {
                let tab_id = match dom.tab_handle(scope) {
                    Some(h) => h.tab_id.clone(),
                    None => return Ok(false),
                };
                let info = desktop
                    .browser()
                    .tab_info(&tab_id)
                    .map_err(|e| AutomataError::Internal(format!("tab_info: {e}")))?;
                let title_ok = title.as_ref().map_or(true, |t| t.test(&info.title));
                let url_ok = url.as_ref().map_or(true, |u| u.test(&info.url));
                Ok(title_ok && url_ok)
            }
            Condition::TabWithState { scope, expr } => {
                let tab_id = match dom.tab_handle(scope) {
                    Some(h) => h.tab_id.clone(),
                    None => return Ok(false),
                };
                let result = desktop
                    .browser()
                    .eval(&tab_id, expr)
                    .map_err(|e| AutomataError::Internal(format!("TabWithState eval: {e}")))?;
                Ok(result.trim() == "true")
            }
        }
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

fn find_in_scope<D: Desktop>(
    dom: &mut ShadowDom<D>,
    desktop: &D,
    scope: &str,
    selector: &SelectorPath,
) -> Result<Option<D::Elem>, AutomataError> {
    dom.find_descendant(scope, selector, desktop)
}

fn has_dialog_child<D: Desktop>(
    dom: &mut ShadowDom<D>,
    desktop: &D,
    scope: &str,
) -> Result<bool, AutomataError> {
    let root = match dom.get(scope, desktop).ok().cloned() {
        Some(el) => el,
        None => return Ok(false),
    };
    Ok(root
        .children()
        .unwrap_or_default()
        .iter()
        .any(|c| c.role() == "dialog"))
}