glass-browser 0.2.6

Local, revision-safe Chrome automation runtime for agents, with semantic observation, verified workflows, MCP, CLI, TUI, 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
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Bounded browser execution for versioned reliability scenarios.
//!
//! This runner deliberately keeps the fixture surface narrow: controls and
//! faults are mapped through typed allowlists, workflow sources stay inside an
//! authorized root, and replay events never contain workflow paths, values, or
//! browser payloads.

use crate::browser::session::{
    BrowserResult, BrowserSession, WorkflowCheckpoint, WorkflowDefinition, WorkflowRunResult,
    WorkflowRunStatus, compile_workflow_json, compile_workflow_yaml,
};
use crate::reliability::{
    ReliabilityExecutionOperation, ReliabilityFaultKind, ReliabilityFixtureManifest,
    ReliabilityForbiddenOutcome, ReliabilityPlatform, ReliabilityReplayBundle,
    ReliabilityReplayEvent, ReliabilityRunClassification, ReliabilityRunMetadata,
    ReliabilityScenario, ReliabilityScenarioObservation,
};
use serde_json::Value;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::time::Instant;

/// Inputs and filesystem boundary for one scenario run.
#[derive(Debug, Clone)]
pub struct ReliabilityRunOptions {
    pub workflow_root: PathBuf,
    pub inputs: BTreeMap<String, Value>,
}

/// Observation plus the redacted replay generated by one browser run.
#[derive(Debug, Clone)]
pub struct ReliabilityRunEvidence {
    pub observation: ReliabilityScenarioObservation,
    pub replay: ReliabilityReplayBundle,
}

/// Execute one validated scenario against an already navigated fixture page.
pub async fn run_reliability_scenario(
    session: &BrowserSession,
    scenario: &ReliabilityScenario,
    fixture: &ReliabilityFixtureManifest,
    options: &ReliabilityRunOptions,
) -> BrowserResult<ReliabilityRunEvidence> {
    let plan = scenario.execution_plan(fixture)?;
    let platform = current_platform()?;
    let browser_version = browser_version(session).await?;
    let started = Instant::now();
    let mut events = Vec::new();
    let mut action_count = 0u32;
    let mut workflow: Option<WorkflowDefinition> = None;
    let mut checkpoint: Option<WorkflowCheckpoint> = None;
    let mut last_result: Option<WorkflowRunResult> = None;
    let mut had_failure = false;
    let mut unsupported = false;

    for operation in &plan.operations {
        if started.elapsed().as_millis() as u64 >= scenario.budgets.max_duration_ms {
            had_failure = true;
            events.push(event(events.len() as u32, "budget", "exhausted"));
            break;
        }
        if action_count >= scenario.budgets.max_browser_actions {
            had_failure = true;
            events.push(event(events.len() as u32, "budget", "actions_exhausted"));
            break;
        }
        action_count = action_count.saturating_add(1);
        match operation {
            ReliabilityExecutionOperation::ApplyControl { control } => {
                let expression = format!(
                    "window.reliabilityLab.{}(); true",
                    control.javascript_method()
                );
                match session.evaluate(&expression).await {
                    Ok(_) => events.push(event(events.len() as u32, "applyControl", "applied")),
                    Err(_) => {
                        had_failure = true;
                        events.push(event(events.len() as u32, "applyControl", "failed"));
                        break;
                    }
                }
            }
            ReliabilityExecutionOperation::InjectFault { injection } => {
                if matches!(
                    injection.fault,
                    ReliabilityFaultKind::RendererDisconnect
                        | ReliabilityFaultKind::BrowserDisconnect
                ) {
                    let dispatched = match session.raw_cdp() {
                        Ok(cdp) => match injection.fault {
                            ReliabilityFaultKind::RendererDisconnect => {
                                cdp.send("Page.crash", None).await.is_ok()
                            }
                            ReliabilityFaultKind::BrowserDisconnect => {
                                cdp.send_browser("Browser.close", None).await.is_ok()
                            }
                            _ => unreachable!("transport fault branch is exhaustive"),
                        },
                        Err(_) => false,
                    };
                    events.push(event(
                        events.len() as u32,
                        "injectFault",
                        if dispatched {
                            "dispatched"
                        } else {
                            "dispatch_failed"
                        },
                    ));
                    unsupported = true;
                    break;
                }
                let fault = serde_json::to_string(injection.fault.fixture_name())?;
                let expression = format!("window.reliabilityLab.injectFault({fault}); true");
                match session.evaluate(&expression).await {
                    Ok(_) => events.push(event(events.len() as u32, "injectFault", "applied")),
                    Err(_) => {
                        had_failure = true;
                        events.push(event(events.len() as u32, "injectFault", "failed"));
                        break;
                    }
                }
            }
            ReliabilityExecutionOperation::RunWorkflow { source } => {
                let path = bounded_workflow_path(&options.workflow_root, source)?;
                let definition = load_workflow(&path)?;
                let result = match session.run_workflow(&definition, &options.inputs).await {
                    Ok(result) => result,
                    Err(_) => {
                        had_failure = true;
                        events.push(event(events.len() as u32, "runWorkflow", "failed"));
                        break;
                    }
                };
                action_count = action_count
                    .saturating_add(result.trace.events.len().min(u32::MAX as usize) as u32);
                had_failure |= result.status != WorkflowRunStatus::Completed;
                checkpoint = session
                    .export_workflow_checkpoint(&definition, &result)
                    .await
                    .ok();
                workflow = Some(definition);
                events.push(event(
                    events.len() as u32,
                    "runWorkflow",
                    workflow_status(result.status),
                ));
                last_result = Some(result);
            }
            ReliabilityExecutionOperation::ResumeFromCheckpoint { checkpoint: name } => {
                if name != "latest" {
                    unsupported = true;
                    events.push(event(
                        events.len() as u32,
                        "resume",
                        "unsupported_checkpoint",
                    ));
                    break;
                }
                let (Some(definition), Some(saved_checkpoint)) = (&workflow, &checkpoint) else {
                    unsupported = true;
                    events.push(event(events.len() as u32, "resume", "missing_checkpoint"));
                    break;
                };
                let result = match session
                    .resume_workflow(definition, &options.inputs, saved_checkpoint)
                    .await
                {
                    Ok(result) => result,
                    Err(_) => {
                        had_failure = true;
                        events.push(event(events.len() as u32, "resume", "failed"));
                        break;
                    }
                };
                had_failure |= result.status != WorkflowRunStatus::Completed;
                checkpoint = session
                    .export_workflow_checkpoint(definition, &result)
                    .await
                    .ok();
                events.push(event(
                    events.len() as u32,
                    "resume",
                    workflow_status(result.status),
                ));
                last_result = Some(result);
            }
        }
    }

    let snapshot = session
        .evaluate("window.reliabilityLab.snapshot()")
        .await
        .ok();
    let side_effect_count = expected_side_effects(snapshot.as_ref(), scenario);
    let terminal_state = snapshot
        .as_ref()
        .and_then(|value| value.get("state"))
        .and_then(Value::as_str)
        .map(str::to_string)
        .or_else(|| {
            last_result
                .as_ref()
                .map(|result| workflow_status(result.status).to_string())
        });
    let mut forbidden_outcomes = Vec::new();
    for (name, expected) in &scenario.expect.side_effect_count {
        let actual = side_effect_count.get(name).copied().unwrap_or_default();
        if actual > *expected {
            forbidden_outcomes.push(ReliabilityForbiddenOutcome::NonIdempotentMutationDuplicated);
        } else if *expected == 0 && actual > 0 {
            forbidden_outcomes.push(ReliabilityForbiddenOutcome::WrongTargetExecuted);
        }
    }
    forbidden_outcomes.sort_unstable();
    forbidden_outcomes.dedup();
    let actual_terminal = terminal_state.as_deref();
    let expected_terminal = scenario.expect.terminal_state.as_str();
    let classification = if unsupported {
        ReliabilityRunClassification::Unsupported
    } else if !forbidden_outcomes.is_empty() {
        ReliabilityRunClassification::Failed
    } else if !had_failure && actual_terminal == Some(expected_terminal) {
        ReliabilityRunClassification::Passed
    } else if had_failure
        && actual_terminal == Some(expected_terminal)
        && side_effect_count == scenario.expect.side_effect_count
    {
        ReliabilityRunClassification::SafeRefusal
    } else {
        ReliabilityRunClassification::Failed
    };
    let scenario_hash = scenario.content_hash()?;
    let fixture_hash = fixture.content_hash()?;
    let elapsed_ms = started.elapsed().as_millis() as u64;
    let metadata = ReliabilityRunMetadata {
        platform,
        browser: "chromium".into(),
        browser_version,
        duration_ms: elapsed_ms.max(1).min(scenario.budgets.max_duration_ms),
        browser_actions: action_count
            .max(1)
            .min(scenario.budgets.max_browser_actions),
    };
    let observation = ReliabilityScenarioObservation {
        scenario_id: scenario.id.clone(),
        scenario_hash: scenario_hash.clone(),
        metadata,
        classification,
        terminal_state,
        side_effect_count,
        forbidden_outcomes,
        oracle_evidence: snapshot.is_some(),
        artifacts_complete: snapshot.is_some() && !events.is_empty(),
    };
    let replay = ReliabilityReplayBundle {
        schema_version: crate::reliability::RELIABILITY_REPLAY_SCHEMA_VERSION,
        scenario_id: scenario.id.clone(),
        scenario_hash,
        fixture_id: fixture.id.clone(),
        fixture_hash,
        events,
        observation: observation.clone(),
    };
    replay.validate(scenario)?;
    Ok(ReliabilityRunEvidence {
        observation,
        replay,
    })
}

fn event(sequence: u32, operation: &str, result: &str) -> ReliabilityReplayEvent {
    ReliabilityReplayEvent {
        sequence,
        operation: operation.into(),
        result: result.into(),
    }
}

fn workflow_status(status: WorkflowRunStatus) -> &'static str {
    match status {
        WorkflowRunStatus::Completed => "completed",
        WorkflowRunStatus::Failed => "failed",
        WorkflowRunStatus::BudgetExhausted => "budget_exhausted",
        WorkflowRunStatus::ResumeRequired => "resume_required",
    }
}

fn expected_side_effects(
    snapshot: Option<&Value>,
    scenario: &ReliabilityScenario,
) -> BTreeMap<String, u64> {
    scenario
        .expect
        .side_effect_count
        .keys()
        .map(|name| {
            let property = format!("{name}Count");
            let count = snapshot
                .and_then(|value| value.get(&property))
                .and_then(Value::as_u64)
                .unwrap_or_default();
            (name.clone(), count)
        })
        .collect()
}

fn bounded_workflow_path(root: &Path, source: &str) -> BrowserResult<PathBuf> {
    let root = std::fs::canonicalize(root)?;
    let path = root.join(source);
    let canonical = std::fs::canonicalize(path)?;
    if !canonical.starts_with(&root) {
        return Err("workflow source escapes the authorized reliability root".into());
    }
    Ok(canonical)
}

fn load_workflow(path: &Path) -> BrowserResult<WorkflowDefinition> {
    let source = std::fs::read_to_string(path)?;
    let format = path.extension().and_then(|extension| extension.to_str());
    let document = match format {
        Some("yaml") | Some("yml") => compile_workflow_yaml(&source)?,
        _ => compile_workflow_json(&source)?,
    };
    Ok(document.definition)
}

async fn browser_version(session: &BrowserSession) -> BrowserResult<String> {
    let Ok(cdp) = session.raw_cdp() else {
        return Ok("unknown".into());
    };
    let value = cdp.send_browser("Browser.getVersion", None).await?;
    Ok(value
        .get("product")
        .and_then(Value::as_str)
        .unwrap_or("chromium")
        .to_string())
}

fn current_platform() -> BrowserResult<ReliabilityPlatform> {
    #[cfg(all(target_os = "linux", target_arch = "x86_64"))]
    {
        Ok(ReliabilityPlatform::LinuxX86_64)
    }
    #[cfg(all(target_os = "linux", target_arch = "aarch64"))]
    {
        Ok(ReliabilityPlatform::LinuxArm64)
    }
    #[cfg(all(target_os = "macos", target_arch = "x86_64"))]
    {
        Ok(ReliabilityPlatform::MacosX86_64)
    }
    #[cfg(all(target_os = "macos", target_arch = "aarch64"))]
    {
        Ok(ReliabilityPlatform::MacosArm64)
    }
    #[cfg(not(any(
        all(target_os = "linux", target_arch = "x86_64"),
        all(target_os = "linux", target_arch = "aarch64"),
        all(target_os = "macos", target_arch = "x86_64"),
        all(target_os = "macos", target_arch = "aarch64")
    )))]
    {
        Err("reliability runner supports Linux x86-64/arm64 and macOS x86-64/arm64 only".into())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::reliability::{ReliabilityScenarioExpectation, ReliabilityScenarioStep};

    #[test]
    fn workflow_sources_stay_inside_the_authorized_root() {
        let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures");

        assert!(bounded_workflow_path(&root, "workflow-minimal.json").is_ok());
        assert!(bounded_workflow_path(&root, "../Cargo.toml").is_err());
    }

    #[test]
    fn workflow_loader_validates_the_checked_in_contract() {
        let path =
            PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/workflow-minimal.json");
        let workflow = load_workflow(&path).expect("fixture workflow should compile");

        assert_eq!(workflow.name, "open-example");
        assert_eq!(workflow.steps.len(), 1);
    }

    #[test]
    fn expected_side_effects_are_limited_to_declared_oracles() {
        let scenario = ReliabilityScenario {
            schema_version: crate::reliability::RELIABILITY_SCENARIO_SCHEMA_VERSION,
            id: "runner-test".into(),
            category: "runner".into(),
            fixture: "fixture-v1".into(),
            platforms: vec![ReliabilityPlatform::LinuxX86_64],
            capabilities: Vec::new(),
            setup: crate::reliability::ReliabilityScenarioSetup {
                browser: "chromium".into(),
                policy: "development".into(),
            },
            steps: vec![ReliabilityScenarioStep {
                run_workflow: None,
                apply_control: None,
                inject: None,
                resume_from_checkpoint: None,
            }],
            expect: ReliabilityScenarioExpectation {
                terminal_state: "submitted".into(),
                side_effect_count: BTreeMap::from([(String::from("submit"), 1)]),
            },
            forbid: Vec::new(),
            budgets: crate::reliability::ReliabilityScenarioBudgets {
                max_duration_ms: 1_000,
                max_browser_actions: 4,
            },
        };
        let snapshot = serde_json::json!({
            "submitCount": 3,
            "ignoredCount": 99,
        });

        assert_eq!(
            expected_side_effects(Some(&snapshot), &scenario)["submit"],
            3
        );
        assert!(!expected_side_effects(Some(&snapshot), &scenario).contains_key("ignored"));
    }
}