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
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Ownership-aware self-update support for Cargo-installed Glass executables.

use crate::browser::session::BrowserResult;
use serde::Serialize;
use serde_json::Value;
use std::env;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

const FULL_PACKAGE: &str = "glass-dev";
const CORE_PACKAGE: &str = "glass-browser";
const CRATES_IO_SOURCE: &str = "registry+https://github.com/rust-lang/crates.io-index";
const CRATES_IO_SPARSE_SOURCE: &str = "sparse+https://index.crates.io/";

#[derive(Debug)]
pub(crate) struct UpdateOptions {
    pub dry_run: bool,
    pub version: Option<String>,
    pub force: bool,
    pub registry: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct InstalledPackage {
    name: String,
    version: String,
    bins: Vec<String>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct UpdatePlan {
    executable: String,
    package: String,
    current_version: String,
    source: String,
    install_root: PathBuf,
    command: Vec<String>,
    waits_for_completion: bool,
}

pub(crate) fn run(options: UpdateOptions) -> BrowserResult<()> {
    validate_options(&options)?;
    let executable = env::current_exe()
        .map_err(|error| format!("could not resolve the running Glass executable: {error}"))?;
    let (binary, root) = installed_binary_and_root(&executable)?;
    let cargo = env::var_os("CARGO").unwrap_or_else(|| OsString::from("cargo"));
    let packages = cargo_install_list(&cargo, &root)?;
    let owner = resolve_owner(&packages, &binary)?;
    let source = match installed_source(&root, owner) {
        Ok(source) => source,
        Err(_) if options.registry.is_some() => "unknown (explicit registry selected)".to_owned(),
        Err(error) => return Err(error),
    };
    validate_source(&source, options.registry.as_deref())?;
    reject_owner_conflict(&packages, owner)?;

    let arguments = install_arguments(owner, &root, &options);
    let plan = UpdatePlan {
        executable: binary,
        package: owner.name.clone(),
        current_version: owner.version.clone(),
        source,
        install_root: root,
        command: display_command(&cargo, &arguments),
        waits_for_completion: !cfg!(windows),
    };

    if options.dry_run {
        println!("{}", serde_json::to_string_pretty(&plan)?);
        return Ok(());
    }

    eprintln!(
        "updating {} v{} in {}",
        plan.package,
        plan.current_version,
        plan.install_root.display()
    );
    let mut command = Command::new(&cargo);
    command
        .args(&arguments)
        .stdin(Stdio::inherit())
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit());

    #[cfg(windows)]
    {
        let child = command.spawn().map_err(|error| {
            format!(
                "could not start Cargo updater `{}`: {error}",
                plan.command.join(" ")
            )
        })?;
        eprintln!(
            "Cargo updater started as process {}; it will finish after Glass exits",
            child.id()
        );
        Ok(())
    }

    #[cfg(not(windows))]
    {
        let status = command.status().map_err(|error| {
            format!(
                "could not run Cargo updater `{}`: {error}",
                plan.command.join(" ")
            )
        })?;
        if !status.success() {
            return Err(format!("Cargo updater failed with {status}").into());
        }
        println!("updated {} successfully", plan.package);
        Ok(())
    }
}

fn validate_options(options: &UpdateOptions) -> BrowserResult<()> {
    for (label, value) in [
        ("version", options.version.as_deref()),
        ("registry", options.registry.as_deref()),
    ] {
        if value.is_some_and(|value| value.trim().is_empty()) {
            return Err(format!("--{label} cannot be empty").into());
        }
    }
    Ok(())
}

fn installed_binary_and_root(executable: &Path) -> BrowserResult<(String, PathBuf)> {
    let binary = executable
        .file_stem()
        .and_then(OsStr::to_str)
        .ok_or("the running executable name is not valid UTF-8")?;
    if !matches!(binary, "glass" | "glass-browser") {
        return Err(format!(
            "cannot update unmanaged executable `{}`; install `glass-dev` or `glass-browser` with Cargo",
            executable.display()
        )
        .into());
    }
    let bin_dir = executable
        .parent()
        .ok_or("the running executable has no parent directory")?;
    if bin_dir.file_name() != Some(OsStr::new("bin")) {
        return Err(format!(
            "cannot update source or unmanaged build `{}`; use `cargo install --path ...` from its source or install a published Glass package",
            executable.display()
        )
        .into());
    }
    let root = bin_dir
        .parent()
        .ok_or("the Cargo bin directory has no install root")?;
    Ok((binary.to_owned(), root.to_path_buf()))
}

fn cargo_install_list(cargo: &OsStr, root: &Path) -> BrowserResult<Vec<InstalledPackage>> {
    let output = Command::new(cargo)
        .args([
            OsStr::new("install"),
            OsStr::new("--list"),
            OsStr::new("--root"),
        ])
        .arg(root)
        .output()
        .map_err(|error| format!("could not query Cargo-installed packages: {error}"))?;
    if !output.status.success() {
        return Err(format!(
            "`cargo install --list --root {}` failed: {}",
            root.display(),
            String::from_utf8_lossy(&output.stderr).trim()
        )
        .into());
    }
    parse_install_list(&String::from_utf8(output.stdout)?)
}

fn parse_install_list(input: &str) -> BrowserResult<Vec<InstalledPackage>> {
    let mut packages: Vec<InstalledPackage> = Vec::new();
    for line in input.lines() {
        if !line.starts_with(char::is_whitespace) && line.ends_with(':') {
            let header = line.trim_end_matches(':');
            let mut fields = header.split_whitespace();
            let name = fields
                .next()
                .ok_or_else(|| format!("unrecognized Cargo install record `{line}`"))?;
            let version = fields
                .next()
                .and_then(|value| value.strip_prefix('v'))
                .filter(|value| !value.is_empty())
                .ok_or_else(|| format!("unrecognized Cargo install record `{line}`"))?;
            packages.push(InstalledPackage {
                name: name.to_owned(),
                version: version.to_owned(),
                bins: Vec::new(),
            });
        } else if let Some(package) = packages.last_mut() {
            let bin = line.trim();
            if !bin.is_empty() {
                package.bins.push(bin.to_owned());
            }
        }
    }
    Ok(packages)
}

fn resolve_owner<'a>(
    packages: &'a [InstalledPackage],
    binary: &str,
) -> BrowserResult<&'a InstalledPackage> {
    let owners: Vec<_> = packages
        .iter()
        .filter(|package| {
            matches!(package.name.as_str(), FULL_PACKAGE | CORE_PACKAGE)
                && package.bins.iter().any(|candidate| candidate == binary)
        })
        .collect();
    match owners.as_slice() {
        [owner] if binary != "glass" || owner.name == FULL_PACKAGE => Ok(owner),
        [] => Err(format!(
            "Cargo does not record an owner for `{binary}` in this install root; refusing to guess an update channel"
        )
        .into()),
        _ => Err(format!(
            "multiple or invalid Cargo package owners were found for `{binary}`; repair the install with `cargo uninstall glass-dev` and `cargo uninstall glass-browser`"
        )
        .into()),
    }
}

fn reject_owner_conflict(
    packages: &[InstalledPackage],
    owner: &InstalledPackage,
) -> BrowserResult<()> {
    let other = if owner.name == FULL_PACKAGE {
        CORE_PACKAGE
    } else {
        FULL_PACKAGE
    };
    if packages.iter().any(|package| package.name == other) {
        return Err(format!(
            "both `{}` and `{other}` are recorded in this Cargo root; choose one owner and uninstall the other before updating",
            owner.name
        )
        .into());
    }
    Ok(())
}

fn installed_source(root: &Path, owner: &InstalledPackage) -> BrowserResult<String> {
    let path = root.join(".crates2.json");
    let bytes = fs::read(&path).map_err(|error| {
        format!(
            "cannot verify the installed package source from {}: {error}; pass --registry NAME only if changing to that registry is intentional",
            path.display()
        )
    })?;
    let value: Value = serde_json::from_slice(&bytes).map_err(|error| {
        format!(
            "cannot read Cargo install provenance from {}: {error}; pass --registry NAME only if changing to that registry is intentional",
            path.display()
        )
    })?;
    let installs = value
        .get("installs")
        .and_then(Value::as_object)
        .ok_or_else(|| {
            format!(
                "Cargo install provenance in {} has an unsupported shape",
                path.display()
            )
        })?;
    let prefix = format!("{} {} (", owner.name, owner.version);
    let matches: Vec<_> = installs
        .keys()
        .filter_map(|key| key.strip_prefix(&prefix)?.strip_suffix(')'))
        .collect();
    match matches.as_slice() {
        [source] => Ok((*source).to_owned()),
        [] => Err(format!(
            "Cargo provenance has no source for {} v{}; pass --registry NAME only if changing registries is intentional",
            owner.name, owner.version
        )
        .into()),
        _ => Err(format!(
            "Cargo provenance has multiple sources for {} v{}; refusing to choose one",
            owner.name, owner.version
        )
        .into()),
    }
}

fn validate_source(source: &str, registry: Option<&str>) -> BrowserResult<()> {
    if source == CRATES_IO_SOURCE || source == CRATES_IO_SPARSE_SOURCE || registry.is_some() {
        return Ok(());
    }
    Err(format!(
        "installed source `{source}` is not the crates.io registry; pass --registry NAME to intentionally select a configured Cargo registry, or update from the original source manually"
    )
    .into())
}

fn install_arguments(
    owner: &InstalledPackage,
    root: &Path,
    options: &UpdateOptions,
) -> Vec<OsString> {
    let mut arguments = vec![
        OsString::from("install"),
        OsString::from(&owner.name),
        OsString::from("--locked"),
        OsString::from("--root"),
        root.as_os_str().to_owned(),
    ];
    if let Some(version) = &options.version {
        arguments.push(OsString::from("--version"));
        arguments.push(OsString::from(version));
    }
    if options.force {
        arguments.push(OsString::from("--force"));
    }
    if let Some(registry) = &options.registry {
        arguments.push(OsString::from("--registry"));
        arguments.push(OsString::from(registry));
    }
    arguments
}

fn display_command(cargo: &OsStr, arguments: &[OsString]) -> Vec<String> {
    std::iter::once(cargo)
        .chain(arguments.iter().map(OsString::as_os_str))
        .map(|value| value.to_string_lossy().into_owned())
        .collect()
}

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

    fn package(name: &str, version: &str, bins: &[&str]) -> InstalledPackage {
        InstalledPackage {
            name: name.to_owned(),
            version: version.to_owned(),
            bins: bins.iter().map(|value| (*value).to_owned()).collect(),
        }
    }

    #[test]
    fn parses_cargo_install_ownership() {
        let packages = parse_install_list(
            "glass-dev v0.3.3 (/checkout/crates/glass-dev):\n    glass\n    glass-browser\nserde-cli v1.0.0:\n    serde\n",
        )
        .unwrap();
        assert_eq!(packages.len(), 2);
        assert_eq!(
            packages[0],
            package("glass-dev", "0.3.3", &["glass", "glass-browser"])
        );
    }

    #[test]
    fn resolves_each_supported_package_owner() {
        let full = vec![package(FULL_PACKAGE, "0.3.3", &["glass", "glass-browser"])];
        assert_eq!(resolve_owner(&full, "glass").unwrap().name, FULL_PACKAGE);
        assert_eq!(
            resolve_owner(&full, "glass-browser").unwrap().name,
            FULL_PACKAGE
        );

        let core = vec![package(CORE_PACKAGE, "0.3.3", &["glass-browser"])];
        assert_eq!(
            resolve_owner(&core, "glass-browser").unwrap().name,
            CORE_PACKAGE
        );
        assert!(resolve_owner(&core, "glass").is_err());
    }

    #[test]
    fn rejects_conflicting_package_records() {
        let packages = vec![
            package(FULL_PACKAGE, "0.3.3", &["glass", "glass-browser"]),
            package(CORE_PACKAGE, "0.3.2", &["glass-browser"]),
        ];
        assert!(resolve_owner(&packages, "glass-browser").is_err());
        assert!(reject_owner_conflict(&packages, &packages[0]).is_err());
    }

    #[test]
    fn builds_a_root_preserving_locked_install() {
        let owner = package(FULL_PACKAGE, "0.3.3", &["glass"]);
        let arguments = install_arguments(
            &owner,
            Path::new("/opt/glass"),
            &UpdateOptions {
                dry_run: false,
                version: Some("0.3.4".to_owned()),
                force: true,
                registry: Some("crates-io".to_owned()),
            },
        );
        let rendered: Vec<_> = arguments
            .iter()
            .map(|value| value.to_string_lossy().into_owned())
            .collect();
        assert_eq!(
            rendered,
            [
                "install",
                FULL_PACKAGE,
                "--locked",
                "--root",
                "/opt/glass",
                "--version",
                "0.3.4",
                "--force",
                "--registry",
                "crates-io"
            ]
        );
    }

    #[test]
    fn requires_explicit_registry_for_source_change() {
        assert!(validate_source("git+https://example.test/glass", None).is_err());
        assert!(validate_source("git+https://example.test/glass", Some("internal")).is_ok());
        assert!(validate_source(CRATES_IO_SOURCE, None).is_ok());
    }
}