browser-control 0.2.1

CLI that manages browsers and exposes them over CDP/BiDi for agent-driven development. Includes an optional MCP server.
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
//! Resolve the `BROWSER_CONTROL` environment variable / `--browser` argument
//! into a concrete browser endpoint.
//!
//! Parsing priority (most-to-least specific):
//! 1. URL with scheme `ws://`, `wss://`, `http://`, `https://` → [`BrowserSelector::Url`].
//! 2. Absolute path that exists on disk → [`BrowserSelector::ExecutablePath`].
//! 3. String that matches a [`Kind`] (case-insensitive) → [`BrowserSelector::Kind`].
//! 4. Anything else → [`BrowserSelector::Name`].

use std::path::{Path, PathBuf};

use anyhow::{anyhow, bail, Context, Result};
use futures_util::future::BoxFuture;
use serde::Serialize;

use crate::detect::{Engine, Installed, Kind};
use crate::registry::Registry;

/// Parsed form of a `BROWSER_CONTROL` value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BrowserSelector {
    /// Direct CDP/BiDi WebSocket or HTTP URL.
    Url(url::Url),
    /// Friendly instance name like `firefox-pikachu`.
    Name(String),
    /// Browser kind like `chrome` or `firefox`.
    Kind(Kind),
    /// Absolute path to a browser executable (must exist).
    ExecutablePath(PathBuf),
}

/// Where the resolved browser came from.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
#[serde(tag = "source", rename_all = "lowercase")]
pub enum Source {
    /// An external CDP/BiDi endpoint passed by URL. No registry write.
    External,
    /// A registered browser, identified by friendly name.
    Registered { name: String },
}

/// Result of resolving a [`BrowserSelector`].
#[derive(Debug, Clone, Serialize)]
pub struct ResolvedBrowser {
    pub endpoint: String,
    pub engine: Engine,
    pub source: Source,
}

/// I/O surface for [`resolve_with`]. Implementations provide HTTP discovery
/// (`/json/version`) and the list of installed browsers.
pub trait Resolver: Send + Sync {
    fn fetch_version<'a>(&'a self, base: &'a str) -> BoxFuture<'a, Result<String>>;
    fn list_installed(&self) -> Vec<Installed>;
}

/// Production resolver: real HTTP + on-disk browser detection.
pub struct DefaultResolver;

impl Resolver for DefaultResolver {
    fn fetch_version<'a>(&'a self, base: &'a str) -> BoxFuture<'a, Result<String>> {
        Box::pin(async move {
            let client = reqwest::Client::builder()
                .timeout(std::time::Duration::from_secs(2))
                .build()
                .context("building reqwest client")?;
            let url = format!("{}/json/version", base.trim_end_matches('/'));
            let v: serde_json::Value = client
                .get(&url)
                .send()
                .await
                .with_context(|| format!("GET {url}"))?
                .error_for_status()
                .with_context(|| format!("GET {url}"))?
                .json()
                .await
                .with_context(|| format!("decode JSON from {url}"))?;
            let ws = v
                .get("webSocketDebuggerUrl")
                .and_then(|x| x.as_str())
                .ok_or_else(|| anyhow!("response from {url} missing webSocketDebuggerUrl"))?
                .to_string();
            Ok(ws)
        })
    }

    fn list_installed(&self) -> Vec<Installed> {
        crate::detect::list_installed()
    }
}

/// Parse a raw `BROWSER_CONTROL` value into a [`BrowserSelector`].
pub fn parse(value: &str) -> Result<BrowserSelector> {
    let v = value.trim();
    if v.is_empty() {
        bail!("BROWSER_CONTROL value is empty");
    }

    let lower = v.to_ascii_lowercase();
    if lower.starts_with("ws://")
        || lower.starts_with("wss://")
        || lower.starts_with("http://")
        || lower.starts_with("https://")
    {
        let u = url::Url::parse(v).with_context(|| format!("parsing URL {v}"))?;
        return Ok(BrowserSelector::Url(u));
    }

    let path = Path::new(v);
    if path.is_absolute() && path.exists() {
        return Ok(BrowserSelector::ExecutablePath(path.to_path_buf()));
    }

    if let Some(k) = Kind::parse(v) {
        return Ok(BrowserSelector::Kind(k));
    }

    Ok(BrowserSelector::Name(v.to_string()))
}

/// Heuristic engine detection for a WebSocket URL.
///
/// A path beginning with `/session` (WebDriver BiDi) → [`Engine::Bidi`];
/// anything else (e.g. `/devtools/browser/<id>`) → [`Engine::Cdp`].
fn engine_from_ws_url(u: &url::Url) -> Engine {
    let path = u.path();
    if path.starts_with("/session") || path.contains("/session/") {
        Engine::Bidi
    } else {
        Engine::Cdp
    }
}

/// Resolve a selector using the real environment.
pub async fn resolve(selector: BrowserSelector, registry: &Registry) -> Result<ResolvedBrowser> {
    resolve_with(selector, registry, &DefaultResolver).await
}

/// Resolve a selector using an injected [`Resolver`] for I/O.
pub async fn resolve_with<R: Resolver>(
    selector: BrowserSelector,
    registry: &Registry,
    r: &R,
) -> Result<ResolvedBrowser> {
    match selector {
        BrowserSelector::Url(u) => resolve_url(u, r).await,
        BrowserSelector::Name(name) => resolve_name(&name, registry),
        BrowserSelector::Kind(k) => resolve_kind(k, registry),
        BrowserSelector::ExecutablePath(p) => resolve_path(&p, registry, r),
    }
}

async fn resolve_url<R: Resolver>(u: url::Url, r: &R) -> Result<ResolvedBrowser> {
    match u.scheme() {
        "ws" | "wss" => Ok(ResolvedBrowser {
            engine: engine_from_ws_url(&u),
            endpoint: u.to_string(),
            source: Source::External,
        }),
        "http" | "https" => {
            let base = u.as_str().trim_end_matches('/').to_string();
            let ws = r.fetch_version(&base).await?;
            let ws_url = url::Url::parse(&ws)
                .with_context(|| format!("parsing webSocketDebuggerUrl {ws}"))?;
            Ok(ResolvedBrowser {
                engine: engine_from_ws_url(&ws_url),
                endpoint: ws,
                source: Source::External,
            })
        }
        other => bail!("unsupported URL scheme: {other}"),
    }
}

fn resolve_name(name: &str, registry: &Registry) -> Result<ResolvedBrowser> {
    let row = registry
        .get_by_name(name)
        .with_context(|| format!("looking up browser {name}"))?
        .ok_or_else(|| anyhow!("no registered browser named {name}"))?;
    Ok(ResolvedBrowser {
        endpoint: row.endpoint,
        engine: row.engine,
        source: Source::Registered { name: row.name },
    })
}

fn resolve_kind(kind: Kind, registry: &Registry) -> Result<ResolvedBrowser> {
    let row = registry
        .first_alive_by_kind(kind)
        .with_context(|| format!("looking up alive {kind} browser"))?
        .ok_or_else(|| anyhow!("no running {kind} browser found in registry"))?;
    Ok(ResolvedBrowser {
        endpoint: row.endpoint,
        engine: row.engine,
        source: Source::Registered { name: row.name },
    })
}

fn resolve_path<R: Resolver>(path: &Path, registry: &Registry, r: &R) -> Result<ResolvedBrowser> {
    let installed = r.list_installed();
    let kind = installed
        .iter()
        .find(|i| i.executable == path)
        .map(|i| i.kind)
        .ok_or_else(|| {
            anyhow!(
                "executable {} does not match any known installed browser",
                path.display()
            )
        })?;
    resolve_kind(kind, registry)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::registry::BrowserRow;
    use std::sync::Mutex;

    fn row(name: &str, kind: Kind, port: u16, started_at: &str) -> BrowserRow {
        BrowserRow {
            name: name.to_string(),
            kind,
            engine: kind.engine(),
            pid: std::process::id(),
            endpoint: format!("ws://127.0.0.1:{port}/devtools/browser/abcd"),
            port,
            profile_dir: PathBuf::from(format!("/tmp/profiles/{name}")),
            executable: PathBuf::from("/usr/bin/example"),
            headless: false,
            started_at: started_at.to_string(),
        }
    }

    /// Bind a TCP listener on a random local port and return (listener, port).
    /// Keep the listener alive for the duration of liveness checks.
    fn alive_listener() -> (std::net::TcpListener, u16) {
        let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let port = l.local_addr().unwrap().port();
        (l, port)
    }

    struct FakeResolver {
        fetch_version_result: Mutex<Option<Result<String>>>,
        installed: Vec<Installed>,
    }

    impl FakeResolver {
        fn ok(ws: &str) -> Self {
            Self {
                fetch_version_result: Mutex::new(Some(Ok(ws.to_string()))),
                installed: Vec::new(),
            }
        }
        fn empty() -> Self {
            Self {
                fetch_version_result: Mutex::new(None),
                installed: Vec::new(),
            }
        }
        fn with_installed(installed: Vec<Installed>) -> Self {
            Self {
                fetch_version_result: Mutex::new(None),
                installed,
            }
        }
    }

    impl Resolver for FakeResolver {
        fn fetch_version<'a>(&'a self, _base: &'a str) -> BoxFuture<'a, Result<String>> {
            let taken = self
                .fetch_version_result
                .lock()
                .unwrap()
                .take()
                .unwrap_or_else(|| Err(anyhow!("fetch_version not configured")));
            Box::pin(async move { taken })
        }
        fn list_installed(&self) -> Vec<Installed> {
            self.installed.clone()
        }
    }

    // --- parse() ----------------------------------------------------------

    #[test]
    fn parse_ws_url() {
        let s = "ws://x:9222/devtools/browser/abc";
        match parse(s).unwrap() {
            BrowserSelector::Url(u) => assert_eq!(u.as_str(), s),
            other => panic!("expected Url, got {other:?}"),
        }
    }

    #[test]
    fn parse_http_url() {
        match parse("http://x:9222").unwrap() {
            BrowserSelector::Url(u) => assert_eq!(u.scheme(), "http"),
            other => panic!("expected Url, got {other:?}"),
        }
    }

    #[test]
    fn parse_absolute_nonexistent_path_falls_through_to_name() {
        // Absolute path that does not exist on disk falls through to Name per spec.
        let s = "/non/existent/path/to/nothing-xyz-12345";
        match parse(s).unwrap() {
            BrowserSelector::Name(n) => assert_eq!(n, s),
            other => panic!("expected Name, got {other:?}"),
        }
    }

    #[test]
    fn parse_existing_absolute_path_is_executable_path() {
        let f = tempfile::NamedTempFile::new().unwrap();
        let p = f.path().to_path_buf();
        assert!(p.is_absolute());
        match parse(p.to_str().unwrap()).unwrap() {
            BrowserSelector::ExecutablePath(got) => assert_eq!(got, p),
            other => panic!("expected ExecutablePath, got {other:?}"),
        }
    }

    #[test]
    fn parse_kind_lowercase() {
        assert_eq!(
            parse("chrome").unwrap(),
            BrowserSelector::Kind(Kind::Chrome)
        );
    }

    #[test]
    fn parse_kind_case_insensitive() {
        assert_eq!(
            parse("FIREFOX").unwrap(),
            BrowserSelector::Kind(Kind::Firefox)
        );
    }

    #[test]
    fn parse_friendly_name() {
        assert_eq!(
            parse("firefox-pikachu").unwrap(),
            BrowserSelector::Name("firefox-pikachu".to_string())
        );
    }

    // --- resolve_with() ---------------------------------------------------

    #[tokio::test]
    async fn url_ws_returned_verbatim_cdp_engine() {
        let reg = Registry::open_in_memory().unwrap();
        let r = FakeResolver::empty();
        let sel = parse("ws://127.0.0.1:9222/devtools/browser/abc").unwrap();
        let got = resolve_with(sel, &reg, &r).await.unwrap();
        assert_eq!(got.endpoint, "ws://127.0.0.1:9222/devtools/browser/abc");
        assert_eq!(got.engine, Engine::Cdp);
        assert_eq!(got.source, Source::External);
    }

    #[tokio::test]
    async fn url_ws_session_path_is_bidi() {
        let reg = Registry::open_in_memory().unwrap();
        let r = FakeResolver::empty();
        let sel = parse("ws://127.0.0.1:9222/session/abc123").unwrap();
        let got = resolve_with(sel, &reg, &r).await.unwrap();
        assert_eq!(got.engine, Engine::Bidi);
    }

    #[tokio::test]
    async fn url_http_invokes_fetch_version() {
        let reg = Registry::open_in_memory().unwrap();
        let r = FakeResolver::ok("ws://127.0.0.1:9222/devtools/browser/discovered");
        let sel = parse("http://127.0.0.1:9222").unwrap();
        let got = resolve_with(sel, &reg, &r).await.unwrap();
        assert_eq!(
            got.endpoint,
            "ws://127.0.0.1:9222/devtools/browser/discovered"
        );
        assert_eq!(got.engine, Engine::Cdp);
        assert_eq!(got.source, Source::External);
    }

    #[tokio::test]
    async fn kind_with_one_running_resolves() {
        let reg = Registry::open_in_memory().unwrap();
        let (_listener, port) = alive_listener();
        let r = row("chrome-foxtrot", Kind::Chrome, port, "2024-06-01T00:00:00Z");
        reg.insert(&r).unwrap();
        let fake = FakeResolver::empty();
        let got = resolve_with(BrowserSelector::Kind(Kind::Chrome), &reg, &fake)
            .await
            .unwrap();
        assert_eq!(got.endpoint, r.endpoint);
        assert_eq!(got.engine, Engine::Cdp);
        assert_eq!(
            got.source,
            Source::Registered {
                name: "chrome-foxtrot".to_string()
            }
        );
    }

    #[tokio::test]
    async fn kind_with_none_running_errors() {
        let reg = Registry::open_in_memory().unwrap();
        let fake = FakeResolver::empty();
        let err = resolve_with(BrowserSelector::Kind(Kind::Chrome), &reg, &fake)
            .await
            .unwrap_err();
        assert!(format!("{err:#}").to_lowercase().contains("chrome"));
    }

    #[tokio::test]
    async fn name_lookup_hit() {
        let reg = Registry::open_in_memory().unwrap();
        let r = row(
            "firefox-pikachu",
            Kind::Firefox,
            9111,
            "2024-06-01T00:00:00Z",
        );
        reg.insert(&r).unwrap();
        let fake = FakeResolver::empty();
        let got = resolve_with(
            BrowserSelector::Name("firefox-pikachu".to_string()),
            &reg,
            &fake,
        )
        .await
        .unwrap();
        assert_eq!(got.endpoint, r.endpoint);
        assert_eq!(got.engine, Engine::Bidi);
        assert_eq!(
            got.source,
            Source::Registered {
                name: "firefox-pikachu".to_string()
            }
        );
    }

    #[tokio::test]
    async fn name_lookup_miss_errors() {
        let reg = Registry::open_in_memory().unwrap();
        let fake = FakeResolver::empty();
        let err = resolve_with(BrowserSelector::Name("nope".to_string()), &reg, &fake)
            .await
            .unwrap_err();
        assert!(format!("{err:#}").contains("nope"));
    }

    #[tokio::test]
    async fn executable_path_maps_to_kind_then_errors_when_no_running() {
        let reg = Registry::open_in_memory().unwrap();
        let exe = PathBuf::from("/opt/myorg/chrome");
        let installed = vec![Installed {
            kind: Kind::Chrome,
            executable: exe.clone(),
            version: "130.0.0.0".to_string(),
            engine: Engine::Cdp,
        }];
        let fake = FakeResolver::with_installed(installed);
        let err = resolve_with(BrowserSelector::ExecutablePath(exe), &reg, &fake)
            .await
            .unwrap_err();
        // Mapped to Chrome, but nothing running.
        assert!(format!("{err:#}").to_lowercase().contains("chrome"));
    }

    #[tokio::test]
    async fn executable_path_resolves_via_kind_when_running() {
        let reg = Registry::open_in_memory().unwrap();
        let (_listener, port) = alive_listener();
        let r = row("chrome-x", Kind::Chrome, port, "2024-06-01T00:00:00Z");
        reg.insert(&r).unwrap();

        let exe = PathBuf::from("/opt/myorg/chrome");
        let installed = vec![Installed {
            kind: Kind::Chrome,
            executable: exe.clone(),
            version: "130.0.0.0".to_string(),
            engine: Engine::Cdp,
        }];
        let fake = FakeResolver::with_installed(installed);
        let got = resolve_with(BrowserSelector::ExecutablePath(exe), &reg, &fake)
            .await
            .unwrap();
        assert_eq!(got.endpoint, r.endpoint);
    }

    #[tokio::test]
    async fn executable_path_unknown_errors() {
        let reg = Registry::open_in_memory().unwrap();
        let fake = FakeResolver::with_installed(Vec::new());
        let err = resolve_with(
            BrowserSelector::ExecutablePath(PathBuf::from("/totally/unknown")),
            &reg,
            &fake,
        )
        .await
        .unwrap_err();
        assert!(format!("{err:#}").contains("/totally/unknown"));
    }
}