browser-control-cli 0.1.1

A tiny, fast Rust CLI that drives a real browser over the Chrome DevTools Protocol, for coding agents.
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
use anyhow::{bail, Context, Result};
use futures_util::StreamExt;
use serde_json::{json, Value};
use std::{
    collections::{HashSet, VecDeque},
    fs,
    path::Path,
    process::{Command, Stdio},
    time::{Duration, Instant},
};
use tokio_tungstenite::tungstenite::Message;

#[cfg(unix)]
use tokio::io::{AsyncReadExt, AsyncWriteExt};
#[cfg(unix)]
use tokio::net::{UnixListener, UnixStream};

use crate::cdp::{cdp_timeout, Cdp};
use crate::lifecycle::pid_alive;
use crate::workspace::{debug, now_ms, read_current_tab};

const EVENT_RING_CAP: usize = 200;
const DETAIL_RING_CAP: usize = 100;

pub fn daemon_pid_path() -> &'static Path {
    Path::new(".browser-control/daemon.pid")
}
pub fn daemon_state_path() -> &'static Path {
    Path::new(".browser-control/daemon-state.json")
}
pub fn daemon_sock_path() -> &'static Path {
    Path::new(".browser-control/daemon.sock")
}

#[derive(Default)]
struct DaemonRings {
    events: VecDeque<Value>,
    network: VecDeque<Value>,
    console: VecDeque<Value>,
}

impl DaemonRings {
    fn clear(&mut self) {
        self.events.clear();
        self.network.clear();
        self.console.clear();
    }
}

fn push_ring(ring: &mut VecDeque<Value>, value: Value, cap: usize) {
    if ring.len() >= cap {
        ring.pop_front();
    }
    ring.push_back(value);
}

fn ring_value(ring: &VecDeque<Value>) -> Value {
    Value::Array(ring.iter().cloned().collect())
}

pub async fn daemon_loop() -> Result<()> {
    fs::create_dir_all(".browser-control")?;
    fs::write(daemon_pid_path(), std::process::id().to_string())?;
    let listener = daemon_listener()?;
    let mut c = Cdp::connect().await?;
    c.attach_page(None).await?;
    let mut inflight = HashSet::<String>::new();
    let mut last_activity = now_ms();
    let mut pending_dialog: Option<Value> = None;
    let mut rings = DaemonRings::default();
    loop {
        if let Some(target) = read_current_tab() {
            if c.target.as_deref() != Some(target.as_str())
                && c.attach_page(Some(target)).await.is_ok()
            {
                inflight.clear();
                rings.clear();
                last_activity = now_ms();
            }
        }
        if let Ok(Ok((mut stream, _))) =
            tokio::time::timeout(Duration::from_millis(20), listener.accept()).await
        {
            if let Err(e) = handle_daemon_stream(
                &mut c,
                &inflight,
                last_activity,
                &mut pending_dialog,
                &rings,
                &mut stream,
            )
            .await
            {
                debug(&format!("daemon command failed: {e}"));
            }
        }
        write_daemon_state(
            &c,
            &inflight,
            last_activity,
            pending_dialog.as_ref(),
            &rings,
        )?;
        match tokio::time::timeout(Duration::from_millis(80), c.ws.next()).await {
            Err(_) => continue,
            Ok(None) => bail!("cdp websocket closed"),
            Ok(Some(Err(e))) => return Err(e.into()),
            Ok(Some(Ok(Message::Text(txt)))) => {
                let v: Value = serde_json::from_str(&txt)?;
                if v.get("id").is_some() {
                    continue;
                }
                if c.session.as_deref().is_some() && v["sessionId"].as_str() != c.session.as_deref()
                {
                    continue;
                }
                let Some(method) = v["method"].as_str() else {
                    continue;
                };
                let method_name = method.to_string();
                let params = v["params"].clone();
                let entry = json!({
                    "ts": now_ms(),
                    "method": method_name,
                    "sessionId": v["sessionId"].as_str(),
                    "params": params,
                });
                push_ring(&mut rings.events, entry.clone(), EVENT_RING_CAP);
                if entry["method"]
                    .as_str()
                    .is_some_and(|m| m.starts_with("Network."))
                {
                    push_ring(&mut rings.network, entry.clone(), DETAIL_RING_CAP);
                }
                if entry["method"].as_str().is_some_and(|m| {
                    m == "Runtime.consoleAPICalled"
                        || m == "Runtime.exceptionThrown"
                        || m.starts_with("Log.")
                }) {
                    push_ring(&mut rings.console, entry, DETAIL_RING_CAP);
                }
                let params = v["params"].clone();
                let rid = params["requestId"].as_str().map(str::to_string);
                match method {
                    "Page.javascriptDialogOpening" => {
                        pending_dialog = Some(params);
                        last_activity = now_ms();
                    }
                    "Page.javascriptDialogClosed" => {
                        pending_dialog = None;
                        last_activity = now_ms();
                    }
                    "Network.requestWillBeSent" => {
                        if let Some(r) = rid {
                            inflight.insert(r);
                        }
                        last_activity = now_ms();
                    }
                    "Network.loadingFinished" | "Network.loadingFailed" => {
                        if let Some(r) = rid {
                            inflight.remove(&r);
                        }
                        last_activity = now_ms();
                    }
                    m if m.starts_with("Network.") || m.starts_with("Page.") => {
                        last_activity = now_ms();
                    }
                    _ => {}
                }
            }
            Ok(Some(Ok(_))) => {}
        }
    }
}

#[cfg(unix)]
fn daemon_listener() -> Result<UnixListener> {
    let path = daemon_sock_path();
    let _ = fs::remove_file(path);
    let listener = UnixListener::bind(path)?;
    {
        use std::os::unix::fs::PermissionsExt;
        let _ = fs::set_permissions(path, fs::Permissions::from_mode(0o600));
    }
    Ok(listener)
}

#[cfg(not(unix))]
fn daemon_listener() -> Result<()> {
    bail!("daemon IPC currently requires Unix sockets")
}

#[cfg(unix)]
async fn handle_daemon_stream(
    c: &mut Cdp,
    inflight: &HashSet<String>,
    last_activity: u64,
    pending_dialog: &mut Option<Value>,
    rings: &DaemonRings,
    stream: &mut UnixStream,
) -> Result<()> {
    let mut raw = Vec::new();
    stream.read_to_end(&mut raw).await?;
    let v: Value = serde_json::from_slice(&raw)?;
    let response = match v["cmd"].as_str() {
        Some("ping") => json!({"ok":true,"result":{"pong":true,"pid":std::process::id()}}),
        Some("state") => {
            json!({"ok":true,"result":daemon_state_value(c, inflight, last_activity, pending_dialog.as_ref(), rings)})
        }
        Some("events") => json!({"ok":true,"result":ring_value(&rings.events)}),
        Some("network") => json!({"ok":true,"result":ring_value(&rings.network)}),
        Some("console") => json!({"ok":true,"result":ring_value(&rings.console)}),
        Some("cdp") => {
            let Some(method) = v["method"].as_str() else {
                return write_daemon_error(stream, "missing CDP method").await;
            };
            let params = v.get("params").cloned().unwrap_or_else(|| json!({}));
            let attach = v["attach"].as_bool().unwrap_or(true);
            match c.call(method, params, attach).await {
                Ok(r) => json!({"ok":true,"result":r}),
                Err(e) => json!({"ok":false,"error":e.to_string()}),
            }
        }
        Some("dialog") => {
            let accept = v["accept"].as_bool().unwrap_or(true);
            match c
                .send(
                    "Page.handleJavaScriptDialog",
                    json!({"accept":accept}),
                    c.session.clone(),
                )
                .await
            {
                Ok(r) => {
                    *pending_dialog = None;
                    json!({"ok":true,"result":r})
                }
                Err(e) => {
                    *pending_dialog = None;
                    json!({"ok":false,"error":e.to_string()})
                }
            }
        }
        Some(cmd) => json!({"ok":false,"error":format!("unknown daemon command {cmd}")}),
        None => json!({"ok":false,"error":"missing daemon command"}),
    };
    stream
        .write_all(serde_json::to_string(&response)?.as_bytes())
        .await?;
    stream.shutdown().await?;
    Ok(())
}

#[cfg(unix)]
async fn write_daemon_error(stream: &mut UnixStream, error: &str) -> Result<()> {
    stream
        .write_all(json!({"ok":false,"error":error}).to_string().as_bytes())
        .await?;
    stream.shutdown().await?;
    Ok(())
}

#[cfg(not(unix))]
async fn handle_daemon_stream(
    _c: &mut Cdp,
    _inflight: &HashSet<String>,
    _last_activity: u64,
    _pending_dialog: &mut Option<Value>,
    _rings: &DaemonRings,
    _stream: &mut (),
) -> Result<()> {
    bail!("daemon IPC currently requires Unix sockets")
}

#[cfg(unix)]
pub async fn daemon_command(mut cmd: Value) -> Result<Value> {
    ensure_daemon()?;
    cmd["id"] = json!(format!("{}-{}", std::process::id(), now_ms()));
    let mut stream = tokio::time::timeout(cdp_timeout(), UnixStream::connect(daemon_sock_path()))
        .await
        .context("daemon connect timeout")??;
    stream
        .write_all(serde_json::to_string(&cmd)?.as_bytes())
        .await?;
    stream.shutdown().await?;
    let mut raw = Vec::new();
    tokio::time::timeout(cdp_timeout(), stream.read_to_end(&mut raw))
        .await
        .context("daemon response timeout")??;
    let v: Value = serde_json::from_slice(&raw)?;
    if v["ok"].as_bool().unwrap_or(false) {
        return Ok(v["result"].clone());
    }
    bail!("{}", v["error"].as_str().unwrap_or("daemon command failed"))
}

#[cfg(not(unix))]
pub async fn daemon_command(_cmd: Value) -> Result<Value> {
    bail!("daemon IPC currently requires Unix sockets")
}

pub fn ensure_daemon() -> Result<()> {
    fs::create_dir_all(".browser-control")?;
    if daemon_ready() {
        return Ok(());
    }
    // Reap any stale daemon process before respawning.
    kill_recorded_daemon();
    let _ = fs::remove_file(daemon_pid_path());
    let _ = fs::remove_file(daemon_state_path());
    let _ = fs::remove_file(daemon_sock_path());
    let exe = std::env::current_exe()?;
    Command::new(exe)
        .arg("daemon")
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()?;
    let deadline = Instant::now() + Duration::from_secs(3);
    while Instant::now() < deadline {
        if daemon_ready() {
            return Ok(());
        }
        std::thread::sleep(Duration::from_millis(100));
    }
    bail!("browser-control daemon did not start")
}

pub fn stop_daemon() {
    kill_recorded_daemon();
    let _ = fs::remove_file(daemon_pid_path());
    let _ = fs::remove_file(daemon_state_path());
    let _ = fs::remove_file(daemon_sock_path());
}

fn kill_recorded_daemon() {
    if let Ok(raw) = fs::read_to_string(daemon_pid_path()) {
        let pid = raw.trim().to_string();
        if !pid.is_empty() && pid_alive(&pid) {
            #[cfg(unix)]
            {
                let _ = Command::new("kill").args(["-TERM", &pid]).status();
            }
            #[cfg(windows)]
            {
                let _ = Command::new("taskkill").args(["/PID", &pid, "/F"]).status();
            }
            for _ in 0..20 {
                if !pid_alive(&pid) {
                    break;
                }
                std::thread::sleep(Duration::from_millis(50));
            }
        }
    }
}

fn daemon_pid_alive() -> bool {
    fs::read_to_string(daemon_pid_path())
        .ok()
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .map(|p| pid_alive(&p))
        .unwrap_or(false)
}

pub fn daemon_ready() -> bool {
    daemon_fresh() && daemon_pid_alive() && daemon_sock_path().exists()
}

fn daemon_fresh() -> bool {
    let Ok(raw) = fs::read_to_string(daemon_state_path()) else {
        return false;
    };
    let Ok(v) = serde_json::from_str::<Value>(&raw) else {
        return false;
    };
    let Some(heartbeat) = v["heartbeat"].as_u64() else {
        return false;
    };
    now_ms().saturating_sub(heartbeat) < 2_000
}

pub async fn wait_network_idle_daemon(timeout: Duration, idle: Duration) -> Result<bool> {
    ensure_daemon()?;
    let started = Instant::now();
    let deadline = started + timeout;
    let min_observe = idle.max(Duration::from_millis(1000));
    let baseline = now_ms();
    while Instant::now() < deadline {
        let v = daemon_command(json!({"cmd":"state"})).await?;
        let heartbeat = v["heartbeat"].as_u64().unwrap_or(0);
        if now_ms().saturating_sub(heartbeat) > 2_000 {
            bail!("daemon heartbeat stale")
        }
        let inflight = v["inflight"].as_u64().unwrap_or(0);
        let last = v["lastActivity"].as_u64().unwrap_or(0);
        let observed = last >= baseline;
        if started.elapsed() >= min_observe
            && inflight == 0
            && observed
            && now_ms().saturating_sub(last) >= idle.as_millis() as u64
        {
            return Ok(true);
        }
        tokio::time::sleep(Duration::from_millis(100)).await;
    }
    Ok(false)
}

/// Pending dialog params if the daemon is live and a dialog is open.
/// Never auto-starts the daemon — this is a read-only probe.
pub async fn pending_dialog() -> Option<Value> {
    if !daemon_ready() {
        return None;
    }
    let state = daemon_command(json!({"cmd":"state"})).await.ok()?;
    if state["pendingDialog"].as_bool().unwrap_or(false) {
        Some(state["dialog"].clone())
    } else {
        None
    }
}

pub async fn dialog_action(action: &str) -> Result<()> {
    let accept = action != "dismiss";
    if daemon_command(json!({"cmd":"dialog","accept":accept}))
        .await
        .is_err()
    {
        let params = json!({"accept":accept});
        debug("dialog: resolving page websocket");
        if let Ok(ws) = crate::cdp::page_ws_url(read_current_tab()).await {
            debug("dialog: using page websocket");
            crate::cdp::send_page_command(&ws, "Page.handleJavaScriptDialog", params).await?;
        } else {
            debug("dialog: falling back to browser websocket");
            let mut c = Cdp::connect().await?;
            c.attach_page_existing(None).await?;
            c.send("Page.handleJavaScriptDialog", params, c.session.clone())
                .await?;
        }
    }
    println!("ok");
    Ok(())
}

fn write_daemon_state(
    c: &Cdp,
    inflight: &HashSet<String>,
    last_activity: u64,
    dialog: Option<&Value>,
    rings: &DaemonRings,
) -> Result<()> {
    fs::write(
        daemon_state_path(),
        serde_json::to_string(&daemon_state_value(
            c,
            inflight,
            last_activity,
            dialog,
            rings,
        ))?,
    )?;
    Ok(())
}

fn daemon_state_value(
    c: &Cdp,
    inflight: &HashSet<String>,
    last_activity: u64,
    dialog: Option<&Value>,
    rings: &DaemonRings,
) -> Value {
    json!({
        "pid": std::process::id(),
        "heartbeat": now_ms(),
        "targetId": c.target.as_ref(),
        "sessionId": c.session.as_ref(),
        "inflight": inflight.len(),
        "lastActivity": last_activity,
        "pendingDialog": dialog.is_some(),
        "dialog": dialog,
        "events": rings.events.len(),
        "networkEvents": rings.network.len(),
        "consoleEvents": rings.console.len(),
    })
}