glass-browser 0.3.13

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
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
//! Persistent local browser sessions for CLI and terminal clients.
//!
//! A session owner process keeps the [`BrowserSession`] and Chrome child alive
//! between CLI invocations. Clients attach through the verified loopback CDP
//! port; the owner is the only process allowed to close the owned browser.

use super::policy::BrowserPolicy;
use super::session::{BrowserResult, BrowserSession, SessionOptions};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};

const SESSION_SCHEMA_VERSION: u32 = 1;
const MAX_SESSION_NAME_BYTES: usize = 64;
const MAX_STATUS_BYTES: usize = 32 * 1024;
const START_TIMEOUT: Duration = Duration::from_secs(20);
const START_POLL: Duration = Duration::from_millis(25);

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PersistentSessionRecord {
    pub schema_version: u32,
    pub name: String,
    pub state: String,
    pub pid: u32,
    pub browser_pid: u32,
    pub port: u16,
    pub profile: String,
    pub headed: bool,
    pub socket: PathBuf,
    pub status_path: PathBuf,
    pub started_at: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

#[derive(Debug, Clone)]
pub struct PersistentSessionConfig {
    pub name: String,
    pub port: u16,
    pub profile: String,
    pub headed: bool,
    pub chrome_path: Option<PathBuf>,
    pub policy_args: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct PersistentSessionPaths {
    pub socket: PathBuf,
    pub status: PathBuf,
}
#[derive(Debug, Clone)]
pub struct PersistentSessionServeConfig {
    pub name: String,
    pub socket: PathBuf,
    pub status_path: PathBuf,
    pub port: u16,
    pub profile: String,
    pub headed: bool,
    pub chrome_path: Option<PathBuf>,
    pub policy: BrowserPolicy,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct SessionStatusView {
    name: String,
    state: String,
    port: Option<u16>,
    profile: Option<String>,
    pid: Option<u32>,
    browser_pid: Option<u32>,
    headed: Option<bool>,
    started_at: Option<String>,
    socket: Option<PathBuf>,
    error: Option<String>,
}

#[derive(Debug, Deserialize)]
struct SessionRequest {
    op: String,
}

pub fn validate_name(name: &str) -> BrowserResult<()> {
    if name.is_empty() || name.len() > MAX_SESSION_NAME_BYTES {
        return Err(format!("session name must be 1..{MAX_SESSION_NAME_BYTES} bytes").into());
    }
    if name == "."
        || name == ".."
        || !name
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
    {
        return Err("session name may contain only ASCII letters, digits, '-', '_', or '.'".into());
    }
    Ok(())
}

pub fn session_root() -> PathBuf {
    std::env::var_os("GLASS_CONFIG_HOME")
        .map(PathBuf::from)
        .unwrap_or_else(|| dirs::data_local_dir().unwrap_or_else(|| PathBuf::from(".")))
        .join("glass")
        .join("sessions")
}

pub fn paths(name: &str) -> BrowserResult<PersistentSessionPaths> {
    validate_name(name)?;
    let root = session_root();
    Ok(PersistentSessionPaths {
        socket: root.join(format!("{name}.sock")),
        status: root.join(format!("{name}.json")),
    })
}

pub fn read_record(name: &str) -> BrowserResult<Option<PersistentSessionRecord>> {
    let paths = paths(name)?;
    let bytes = match std::fs::read(&paths.status) {
        Ok(bytes) => bytes,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(error) => return Err(error.into()),
    };
    if bytes.len() > MAX_STATUS_BYTES {
        return Err(format!("session status exceeds {MAX_STATUS_BYTES} bytes").into());
    }
    let record: PersistentSessionRecord = serde_json::from_slice(&bytes)?;
    if record.schema_version != SESSION_SCHEMA_VERSION {
        return Err("unsupported persistent session status schema".into());
    }
    if record.name != name || record.socket != paths.socket || record.status_path != paths.status {
        return Err("persistent session status identity does not match its name".into());
    }
    Ok(Some(record))
}

pub fn status(name: &str) -> BrowserResult<serde_json::Value> {
    let record = read_record(name)?;
    let view = match record {
        Some(record) => {
            let server_alive = process_is_alive(record.pid);
            let browser_alive = process_is_alive(record.browser_pid);
            let state = if record.state == "running" && server_alive && browser_alive {
                "running"
            } else if record.state == "failed" {
                "failed"
            } else {
                "stale"
            };
            SessionStatusView {
                name: record.name,
                state: state.into(),
                port: Some(record.port),
                profile: Some(record.profile),
                pid: Some(record.pid),
                browser_pid: Some(record.browser_pid),
                headed: Some(record.headed),
                started_at: Some(record.started_at),
                socket: Some(record.socket),
                error: record.error,
            }
        }
        None => SessionStatusView {
            name: name.into(),
            state: "stopped".into(),
            port: None,
            profile: None,
            pid: None,
            browser_pid: None,
            headed: None,
            started_at: None,
            socket: None,
            error: None,
        },
    };
    Ok(serde_json::to_value(view)?)
}

pub async fn start(config: PersistentSessionConfig) -> BrowserResult<PersistentSessionRecord> {
    validate_name(&config.name)?;
    if config.port == 0 {
        return Err("persistent session port must be non-zero".into());
    }
    let paths = paths(&config.name)?;
    if let Some(existing) = read_record(&config.name)? {
        if process_is_alive(existing.pid) {
            return Err(format!(
                "persistent session `{}` is already running as pid {} on port {}",
                existing.name, existing.pid, existing.port
            )
            .into());
        }
        remove_stale_artifacts(&existing)?;
    }
    std::fs::create_dir_all(session_root())?;
    remove_socket_if_safe(&paths.socket)?;
    let executable = std::env::current_exe()?;
    let mut command = std::process::Command::new(executable);
    command
        .arg("--policy")
        .arg(
            config
                .policy_args
                .first()
                .cloned()
                .unwrap_or_else(|| "development".into()),
        )
        .arg("--profile")
        .arg(&config.profile)
        .arg("--port")
        .arg(config.port.to_string());
    if config.headed {
        command.arg("--headed");
    }
    if let Some(chrome_path) = &config.chrome_path {
        command.arg("--chrome-path").arg(chrome_path);
    }
    for argument in config.policy_args.iter().skip(1) {
        command.arg(argument);
    }
    command
        .arg("session")
        .arg("serve")
        .arg(&config.name)
        .arg("--socket")
        .arg(&paths.socket)
        .arg("--status")
        .arg(&paths.status)
        .stdin(std::process::Stdio::null())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()?;

    let deadline = Instant::now() + START_TIMEOUT;
    loop {
        if let Some(record) = read_record(&config.name)? {
            if record.state == "failed" {
                return Err(record
                    .error
                    .unwrap_or_else(|| "persistent browser session failed to start".into())
                    .into());
            }
            if record.state == "running" && process_is_alive(record.pid) {
                return Ok(record);
            }
        }
        if Instant::now() >= deadline {
            return Err(format!(
                "persistent session `{}` did not become ready within {} seconds",
                config.name,
                START_TIMEOUT.as_secs()
            )
            .into());
        }
        tokio::time::sleep(START_POLL).await;
    }
}

pub async fn stop(name: &str) -> BrowserResult<serde_json::Value> {
    let Some(record) = read_record(name)? else {
        return status(name);
    };
    if !process_is_alive(record.pid) {
        remove_stale_artifacts(&record)?;
        return status(name);
    }
    let response = send_request(&record.socket, "stop").await?;
    let deadline = Instant::now() + START_TIMEOUT;
    while Instant::now() < deadline {
        if read_record(name)?.is_none() || !process_is_alive(record.pid) {
            return Ok(response);
        }
        tokio::time::sleep(START_POLL).await;
    }
    Err(
        format!("persistent session `{name}` did not stop; inspect `glass session status {name}`")
            .into(),
    )
}

pub fn open_message(name: &str) -> BrowserResult<String> {
    let value = status(name)?;
    if value.get("state").and_then(serde_json::Value::as_str) != Some("running") {
        return Err(format!(
            "persistent session `{name}` is not running; start it with `glass session start {name}`"
        )
        .into());
    }
    let port = value
        .get("port")
        .and_then(serde_json::Value::as_u64)
        .ok_or("persistent session has no verified port")?;
    Ok(format!(
        "Session `{name}` is ready on loopback port {port}.\n\nAttach one command:\n  glass --session {name} observe --level interactive\n\nLaunch the browser terminal:\n  glass browser --session {name}"
    ))
}

pub async fn serve(config: PersistentSessionServeConfig) -> BrowserResult<()> {
    validate_name(&config.name)?;
    if config.port == 0 {
        return Err("persistent session port must be non-zero".into());
    }
    #[cfg(not(unix))]
    {
        let _ = config;
        return Err("persistent browser sessions require a Unix local socket".into());
    }
    #[cfg(unix)]
    {
        serve_unix(config).await
    }
}

#[cfg(unix)]
async fn serve_unix(config: PersistentSessionServeConfig) -> BrowserResult<()> {
    use std::os::unix::fs::PermissionsExt;
    use tokio::net::UnixListener;

    let PersistentSessionServeConfig {
        name,
        socket,
        status_path,
        port,
        profile,
        headed,
        chrome_path,
        policy,
    } = config;

    remove_socket_if_safe(&socket)?;
    if let Some(parent) = socket.parent() {
        std::fs::create_dir_all(parent)?;
    }
    if let Some(parent) = status_path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let listener = UnixListener::bind(&socket)?;
    std::fs::set_permissions(&socket, std::fs::Permissions::from_mode(0o600))?;

    let options = SessionOptions {
        port,
        profile: profile.clone(),
        chrome_path,
        headed,
        policy: Some(policy),
        ..SessionOptions::default()
    };

    let session = match BrowserSession::start(&options).await {
        Ok(session) => session,
        Err(error) => {
            let failed = PersistentSessionRecord {
                schema_version: SESSION_SCHEMA_VERSION,
                name,
                state: "failed".into(),
                pid: std::process::id(),
                browser_pid: 0,
                port,
                profile: profile.clone(),
                headed,
                socket: socket.to_path_buf(),
                status_path: status_path.to_path_buf(),
                started_at: chrono::Utc::now().to_rfc3339(),
                error: Some(error.to_string()),
            };
            write_record(&status_path, &failed)?;
            return Err(error);
        }
    };
    let record = PersistentSessionRecord {
        schema_version: SESSION_SCHEMA_VERSION,
        name,
        state: "running".into(),
        pid: std::process::id(),
        browser_pid: session.owned_chrome_pid().unwrap_or_default(),
        port,
        profile,
        headed,
        socket: socket.to_path_buf(),
        status_path: status_path.to_path_buf(),
        started_at: chrono::Utc::now().to_rfc3339(),
        error: None,
    };
    write_record(&status_path, &record)?;

    let mut session = Some(session);
    let mut shutdown = false;
    let mut lines = String::new();
    while !shutdown {
        tokio::select! {
            accepted = listener.accept() => {
                let (stream, _) = accepted?;
                let (read, mut write) = stream.into_split();
                let mut reader = BufReader::new(read);
                lines.clear();
                reader.read_line(&mut lines).await?;
                let request = serde_json::from_str::<SessionRequest>(lines.trim())
                    .map_err(|error| format!("invalid session request: {error}"))?;
                let response = match request.op.as_str() {
                    "status" => serde_json::to_value(&record)?,
                    "stop" => {
                        shutdown = true;
                        json!({"ok": true, "state": "stopping"})
                    }
                    _ => json!({"ok": false, "error": "unknown session operation"}),
                };
                write.write_all(serde_json::to_string(&response)?.as_bytes()).await?;
                write.write_all(b"\n").await?;
            }
            _ = tokio::signal::ctrl_c() => {
                shutdown = true;
            }
        }
    }
    if let Some(session) = session.take() {
        let _ = session.close().await;
    }
    let _ = std::fs::remove_file(status_path);
    remove_socket_if_safe(&socket)?;
    Ok(())
}

async fn send_request(socket: &Path, op: &str) -> BrowserResult<serde_json::Value> {
    #[cfg(not(unix))]
    {
        let _ = (socket, op);
        return Err("persistent browser sessions require a Unix local socket".into());
    }
    #[cfg(unix)]
    {
        use tokio::net::UnixStream;
        let stream = UnixStream::connect(socket).await?;
        let (read, mut write) = stream.into_split();
        write
            .write_all(serde_json::to_string(&json!({"op": op}))?.as_bytes())
            .await?;
        write.write_all(b"\n").await?;
        let mut reader = BufReader::new(read);
        let mut line = String::new();
        reader.read_line(&mut line).await?;
        let value: serde_json::Value = serde_json::from_str(line.trim())?;
        if value.get("ok") == Some(&serde_json::Value::Bool(false)) {
            return Err(value
                .get("error")
                .and_then(serde_json::Value::as_str)
                .unwrap_or("persistent session request failed")
                .into());
        }
        Ok(value)
    }
}

fn write_record(path: &Path, record: &PersistentSessionRecord) -> BrowserResult<()> {
    let bytes = serde_json::to_vec_pretty(record)?;
    if bytes.len() > MAX_STATUS_BYTES {
        return Err("persistent session status exceeds its size bound".into());
    }
    let temporary = path.with_extension(format!("json.tmp-{}", std::process::id()));
    std::fs::write(&temporary, bytes)?;
    std::fs::rename(temporary, path)?;
    Ok(())
}

fn remove_stale_artifacts(record: &PersistentSessionRecord) -> BrowserResult<()> {
    let _ = std::fs::remove_file(&record.status_path);
    remove_socket_if_safe(&record.socket)
}

fn remove_socket_if_safe(path: &Path) -> BrowserResult<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::FileTypeExt;
        match std::fs::symlink_metadata(path) {
            Ok(metadata) if metadata.file_type().is_socket() => {
                std::fs::remove_file(path)?;
            }
            Ok(_) => {
                return Err(format!(
                    "refusing to replace non-socket session path {}",
                    path.display()
                )
                .into());
            }
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
            Err(error) => return Err(error.into()),
        }
    }
    #[cfg(not(unix))]
    {
        let _ = path;
    }
    Ok(())
}

#[cfg(unix)]
fn process_is_alive(pid: u32) -> bool {
    pid != 0 && unsafe { libc::kill(pid as libc::pid_t, 0) == 0 }
}

#[cfg(not(unix))]
fn process_is_alive(_pid: u32) -> bool {
    false
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn session_names_are_path_safe() {
        assert!(validate_name("default").is_ok());
        assert!(validate_name("team-1.dev").is_ok());
        assert!(validate_name("../escape").is_err());
        assert!(validate_name("a/b").is_err());
    }

    #[test]
    fn stopped_status_is_explicit_and_browser_free() {
        let value = status("missing-test-session").unwrap();
        assert_eq!(value["state"], "stopped");
        assert_eq!(value["name"], "missing-test-session");
        assert!(value.get("port").is_none_or(serde_json::Value::is_null));
    }
}