glass-browser 0.3.9

Local browser intelligence runtime with semantic memory, verified workflows, multi-surface understanding, CLI, TUI, MCP, and Rust APIs
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
//! Page wait conditions and lifecycle detection.
//!
//! Waits for conditions such as URL changes, element visibility, text
//! appearance, navigation completion, or configurable timeouts.

use super::*;
use std::future::Future;
use std::pin::Pin;

type VerificationCheckFuture<'a> =
    Pin<Box<dyn Future<Output = BrowserResult<(bool, String)>> + 'a>>;

pub(crate) fn is_ignored_network_resource_type(resource_type: Option<&str>) -> bool {
    matches!(
        resource_type,
        Some("WebSocket" | "EventSource" | "Media" | "Ping")
    )
}

impl BrowserSession {
    pub(crate) async fn evaluate_predicate_once(
        &self,
        predicate: &VerificationPredicate,
    ) -> BrowserResult<(bool, String)> {
        predicate.validate(0)?;
        self.check_verification_predicate(predicate).await
    }

    /// Evaluate a bounded, composable postcondition until it becomes true.
    pub async fn verify(
        &self,
        predicate: VerificationPredicate,
        deadline: Duration,
    ) -> BrowserResult<VerificationOutcome> {
        validate_wait_deadline(deadline)?;
        predicate.validate(0)?;
        let started = tokio::time::Instant::now();
        let expires = started + deadline;
        loop {
            let (matched, observed) = self.check_verification_predicate(&predicate).await?;
            let state = bounded_diagnostic_text(&observed);
            if matched {
                return Ok(VerificationOutcome {
                    status: "satisfied",
                    predicate,
                    elapsed_ms: started.elapsed().as_millis() as u64,
                    state,
                });
            }
            if tokio::time::Instant::now() >= expires {
                return Err(ActionVerificationError {
                    kind: ActionFailureKind::VerificationFailed,
                    action: ActionKind::Click,
                    phase: ActionFailurePhase::Verification,
                    recovery_strategy: RecoveryStrategy::Report,
                    execution_id: Some(self.next_execution_id()),
                    target: None,
                    revision: self.page_revision.load(Ordering::Relaxed),
                    reason: format!("verification predicate not satisfied: {state}"),
                }
                .into());
            }
            tokio::time::sleep(WAIT_POLL_INTERVAL).await;
        }
    }

    fn check_verification_predicate<'a>(
        &'a self,
        predicate: &'a VerificationPredicate,
    ) -> VerificationCheckFuture<'a> {
        Box::pin(async move {
            match predicate {
                VerificationPredicate::UrlEquals { value } => {
                    let url = self.page_info().await?.url;
                    Ok((url == *value, format!("url={url}")))
                }
                VerificationPredicate::TitleContains { value } => {
                    let title = self.page_info().await?.title;
                    Ok((title.contains(value), format!("title={title}")))
                }
                VerificationPredicate::Visible { visible } => {
                    let (matched, state, _) = self
                        .check_wait_condition(&WaitCondition::TargetVisible(visible.clone()), None)
                        .await?;
                    Ok((matched, state))
                }
                VerificationPredicate::TextContains { value } => {
                    let (matched, state, _) = self
                        .check_wait_condition(&WaitCondition::Text(value.clone()), None)
                        .await?;
                    Ok((matched, state))
                }
                VerificationPredicate::PopupOpened { value } => {
                    let topology = self.topology.lock().await;
                    let opened = topology.targets.len() > 1;
                    Ok((opened == *value, format!("popupOpened={opened}")))
                }
                VerificationPredicate::DialogOpen { value } => {
                    let topology = self.topology.lock().await;
                    let open = topology.pending_dialog.is_some();
                    Ok((open == *value, format!("dialogOpen={open}")))
                }
                VerificationPredicate::DownloadStarted { value } => {
                    let started = self.download_sequence.load(Ordering::Relaxed) > 0;
                    Ok((started == *value, format!("downloadStarted={started}")))
                }
                VerificationPredicate::RevisionEquals { value } => {
                    let revision = self.page_revision.load(Ordering::Relaxed);
                    Ok((revision == *value, format!("revision={revision}")))
                }
                VerificationPredicate::All { all } => {
                    let mut states = Vec::with_capacity(all.len());
                    let mut matched = true;
                    for child in all {
                        let (child_matched, state) =
                            self.check_verification_predicate(child).await?;
                        matched &= child_matched;
                        states.push(state);
                    }
                    Ok((matched, format!("all=[{}]", states.join(","))))
                }
                VerificationPredicate::Any { any } => {
                    let mut states = Vec::with_capacity(any.len());
                    let mut matched = false;
                    for child in any {
                        let (child_matched, state) =
                            self.check_verification_predicate(child).await?;
                        matched |= child_matched;
                        states.push(state);
                    }
                    Ok((matched, format!("any=[{}]", states.join(","))))
                }
                VerificationPredicate::Not { not } => {
                    let (matched, state) = self.check_verification_predicate(not).await?;
                    Ok((!matched, format!("not({state})")))
                }
            }
        })
    }

    /// Wait for a condition to be satisfied on the page.
    ///
    /// Supported conditions include lifecycle events (`"complete"`, `"interactive"`),
    /// URL matching, target visibility/enabled/stability, text presence,
    /// JavaScript expressions, and network quiet. Returns a [`WaitOutcome`]
    /// on success or a [`WaitTimeout`] error if the deadline expires.
    pub async fn wait(
        &self,
        condition: WaitCondition,
        deadline: Duration,
    ) -> BrowserResult<WaitOutcome> {
        validate_wait_deadline(deadline)?;
        condition.validate()?;
        if let WaitCondition::NetworkQuiet(quiet) = condition {
            return tokio::time::timeout(deadline, self.wait_for_network_quiet(quiet, deadline))
                .await
                .map_err(|_| wait_timeout("network_quiet", deadline, "network_check_pending"))?;
        }
        let mut events = self.cdp.subscribe_events();
        self.wait_loop(condition, deadline, deadline, &mut events, false)
            .await
    }

    pub(crate) async fn wait_loop(
        &self,
        condition: WaitCondition,
        deadline: Duration,
        reported_deadline: Duration,
        events: &mut tokio::sync::broadcast::Receiver<crate::browser::cdp::CdpEvent>,
        require_load_event: bool,
    ) -> BrowserResult<WaitOutcome> {
        let started = tokio::time::Instant::now();
        let expires = started + deadline;
        let mut previous_geometry = None;
        let description = condition.description();
        let mut load_event_seen = !require_load_event;
        let mut last_state = "not_checked".to_string();
        loop {
            let now = tokio::time::Instant::now();
            if now >= expires {
                return Err(WaitTimeout {
                    condition: description.clone(),
                    deadline_ms: reported_deadline.as_millis() as u64,
                    last_state: bounded_wait_state(&last_state),
                    observed_page: self.page_info().await.ok(),
                    reason: "deadline_exceeded",
                }
                .into());
            }
            // Refresh the frame route before every probe. This is cheap for
            // stable pages and prevents a post-SPA-navigation stale-frame
            // error from poisoning the remainder of the wait.
            let remaining = expires.saturating_duration_since(now);
            tokio::time::timeout(remaining, self.ensured_route_identity())
                .await
                .map_err(|_| wait_timeout(&description, reported_deadline, &last_state))??;
            let remaining = expires.saturating_duration_since(tokio::time::Instant::now());
            let (matched, state, geometry) = tokio::time::timeout(
                remaining,
                self.check_wait_condition(&condition, previous_geometry.as_deref()),
            )
            .await
            .map_err(|_| wait_timeout(&description, reported_deadline, &last_state))??;
            last_state = bounded_wait_state(&state);
            previous_geometry = geometry;
            if matched && load_event_seen {
                let (target_id, frame_id) = self.ensured_route_identity().await?;
                return Ok(WaitOutcome {
                    condition: description,
                    elapsed_ms: started.elapsed().as_millis() as u64,
                    last_state,
                    target_id,
                    frame_id,
                });
            }
            let now = tokio::time::Instant::now();
            let remaining = expires - now;
            tokio::select! {
                _ = tokio::time::sleep(WAIT_POLL_INTERVAL.min(remaining)) => {}
                event = events.recv() => match event {
                    Ok(event) => { load_event_seen |= event.method == "Page.loadEventFired"; }
                    Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {}
                    Err(_) => return Err("CDP event stream closed during wait".into()),
                }
            }
        }
    }

    async fn check_wait_condition(
        &self,
        condition: &WaitCondition,
        previous_geometry: Option<&str>,
    ) -> BrowserResult<(bool, String, Option<String>)> {
        match condition {
            WaitCondition::Lifecycle(expected) => {
                let page = self.page_info().await?;
                Ok((page.ready_state == *expected, page.ready_state, None))
            }
            WaitCondition::UrlExact(expected) => {
                let page = self.page_info().await?;
                Ok((page.url == *expected, page.url, None))
            }
            WaitCondition::UrlPrefix(prefix) => {
                let page = self.page_info().await?;
                Ok((page.url.starts_with(prefix), page.url, None))
            }
            WaitCondition::Text(expected) => {
                let expression = visible_text_contains_expression(expected)?;
                let value = self.evaluate_value(&expression).await?;
                let matched = value.as_bool().unwrap_or(false);
                Ok((matched, format!("present={matched}"), None))
            }
            WaitCondition::SemanticRegion(region_id) => {
                let context = self.observe_fresh().await?;
                let observation = SemanticObservation::from_page_context(
                    &context,
                    SemanticObservationLevel::Interactive,
                )?;
                let region = observation
                    .regions
                    .iter()
                    .find(|region| region.id == *region_id);
                let matched = region.is_some_and(|region| !region.targets.is_empty());
                Ok((matched, format!("region={region_id};ready={matched}"), None))
            }
            WaitCondition::JavaScript(expression) => {
                let value = self.evaluate_value(expression).await?;
                let matched = value
                    .as_bool()
                    .ok_or("wait JavaScript predicate must return a boolean")?;
                Ok((matched, matched.to_string(), None))
            }
            WaitCondition::TargetAttached(target)
            | WaitCondition::TargetVisible(target)
            | WaitCondition::TargetHidden(target)
            | WaitCondition::TargetEnabled(target)
            | WaitCondition::TargetStable(target) => {
                self.check_target_wait(condition, target, previous_geometry)
                    .await
            }
            WaitCondition::NetworkQuiet(_) => unreachable!("handled by wait"),
        }
    }

    async fn check_target_wait(
        &self,
        condition: &WaitCondition,
        target: &str,
        previous_geometry: Option<&str>,
    ) -> BrowserResult<(bool, String, Option<String>)> {
        let element = match self.resolve_element(target).await {
            Ok(element) => element,
            Err(error)
                if error
                    .downcast_ref::<TargetError>()
                    .is_some_and(|error| error.kind == TargetErrorKind::NotFound) =>
            {
                let matched = matches!(condition, WaitCondition::TargetHidden(_));
                return Ok((matched, "detached".to_string(), None));
            }
            Err(error) => return Err(error),
        };
        if matches!(condition, WaitCondition::TargetAttached(_)) {
            return Ok((true, "attached".to_string(), None));
        }
        let object_id = self
            .cdp
            .resolve_node_object(element.node_id, element.backend_dom_node_id)
            .await?;
        let remote = RemoteObjectGuard::new(self.cdp.clone(), object_id);
        let raw = self
            .cdp
            .call_on_object(&remote.object_id, WAIT_TARGET_STATE_FUNCTION)
            .await;
        let value = runtime_value(&raw?)?;
        let visible = value["visible"].as_bool().unwrap_or(false);
        let enabled = value["enabled"].as_bool().unwrap_or(false);
        let geometry = value["geometry"].as_str().map(str::to_string);
        let matched = match condition {
            WaitCondition::TargetVisible(_) => visible,
            WaitCondition::TargetHidden(_) => !visible,
            WaitCondition::TargetEnabled(_) => visible && enabled,
            WaitCondition::TargetStable(_) => {
                visible
                    && geometry
                        .as_deref()
                        .is_some_and(|geometry| previous_geometry == Some(geometry))
            }
            _ => unreachable!(),
        };
        Ok((matched, value.to_string(), geometry))
    }

    async fn wait_for_network_quiet(
        &self,
        quiet: Duration,
        deadline: Duration,
    ) -> BrowserResult<WaitOutcome> {
        if quiet.is_zero() {
            return Err("network quiet duration must be positive".into());
        }
        let mut events = self.cdp.subscribe_events_with_params();
        let mut guard =
            NetworkDomainGuard::acquire(self.cdp.clone(), Arc::clone(&self.network_wait_leases))
                .await?;
        let started = tokio::time::Instant::now();
        let expires = started + deadline;
        let mut empty_since = started;
        let mut in_flight = HashSet::new();
        let mut overflowed = false;
        loop {
            self.ensured_route_identity().await?;
            let now = tokio::time::Instant::now();
            if in_flight.is_empty() && !overflowed && now.duration_since(empty_since) >= quiet {
                guard.disable().await?;
                let (target_id, frame_id) = self.route_identity().await?;
                return Ok(WaitOutcome {
                    condition: "network_quiet".to_string(),
                    elapsed_ms: started.elapsed().as_millis() as u64,
                    last_state: "in_flight=0".to_string(),
                    target_id,
                    frame_id,
                });
            }
            if now >= expires {
                return Err(WaitTimeout {
                    condition: "network_quiet".to_string(),
                    deadline_ms: deadline.as_millis() as u64,
                    last_state: if overflowed {
                        "in_flight=overflow".to_string()
                    } else {
                        format!("in_flight={}", in_flight.len())
                    },
                    observed_page: self.page_info().await.ok(),
                    reason: "deadline_exceeded",
                }
                .into());
            }
            tokio::select! {
                _ = tokio::time::sleep((expires - now).min(WAIT_POLL_INTERVAL)) => {}
                event = events.recv() => match event {
                    Ok(event) => {
                        let request_id = event.params["requestId"].as_str();
                        match event.method.as_str() {
                            "Network.requestWillBeSent" => {
                                if is_ignored_network_resource_type(event.params["type"].as_str()) {
                                    continue;
                                }
                                if let Some(id) = request_id {
                                    if in_flight.len() < NETWORK_IN_FLIGHT_LIMIT {
                                        in_flight.insert(id.to_string());
                                    } else {
                                        overflowed = true;
                                    }
                                }
                            }
                            "Network.loadingFinished" | "Network.loadingFailed" => {
                                if let Some(id) = request_id {
                                    in_flight.remove(id);
                                }
                                if in_flight.is_empty() && !overflowed {
                                    empty_since = tokio::time::Instant::now();
                                }
                            }
                            _ => {}
                        }
                    }
                    Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {
                        return Err("network wait event stream lagged".into());
                    }
                    Err(tokio::sync::broadcast::error::RecvError::Closed) => {
                        return Err("network wait event stream closed".into());
                    }
                }
            }
        }
    }
}