astrid 0.8.0

Command-line interface for Astrid secure agent runtime
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
//! Self-update command — download and install newer versions of the Astrid CLI.
//!
//! Checks GitHub releases for the `unicity-astrid/astrid` repo, compares
//! against the running binary's version, downloads the appropriate platform
//! binary, and swaps it in place.
//!
//! Also provides PATH setup helpers for `astrid init`.

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

use anyhow::{Context, bail};

use crate::theme::Theme;

/// GitHub org/repo for the core Astrid release.
const GITHUB_ORG: &str = "unicity-astrid";
const GITHUB_REPO: &str = "astrid";

/// Current binary version (set at compile time).
const CURRENT_VERSION: &str = env!("CARGO_PKG_VERSION");

/// TTL for cached update checks (24 hours).
const CHECK_TTL_SECS: u64 = 86_400;

/// The `~/.astrid/bin` directory where self-managed binaries live.
fn astrid_bin_dir() -> anyhow::Result<PathBuf> {
    let home = astrid_core::dirs::AstridHome::resolve()?;
    Ok(home.root().join("bin"))
}

/// Map the current platform to the GitHub release asset target triple.
fn platform_target() -> anyhow::Result<&'static str> {
    match (std::env::consts::OS, std::env::consts::ARCH) {
        ("macos", "aarch64") => Ok("aarch64-apple-darwin"),
        ("macos", "x86_64") => Ok("x86_64-apple-darwin"),
        ("linux", "x86_64") => Ok("x86_64-unknown-linux-gnu"),
        ("linux", "aarch64") => Ok("aarch64-unknown-linux-gnu"),
        (os, arch) => bail!("Unsupported platform: {os}/{arch}"),
    }
}

/// Cached update check result.
#[derive(serde::Serialize, serde::Deserialize)]
struct UpdateCache {
    checked_at: u64,
    latest_version: String,
}

fn cache_path() -> anyhow::Result<PathBuf> {
    let home = astrid_core::dirs::AstridHome::resolve()?;
    Ok(home.var_dir().join("update-check.json"))
}

fn now_epoch() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| d.as_secs())
}

/// Check for a newer version (cached, background-safe).
///
/// Returns `Some(version)` if an update is available, `None` if up-to-date
/// or check failed/cached.
pub(crate) async fn check_for_update_cached() -> Option<String> {
    let path = cache_path().ok()?;

    // Check cache first
    if let Ok(data) = std::fs::read_to_string(&path)
        && let Ok(cache) = serde_json::from_str::<UpdateCache>(&data)
        && now_epoch().saturating_sub(cache.checked_at) < CHECK_TTL_SECS
    {
        let current = semver::Version::parse(CURRENT_VERSION).ok()?;
        let latest = semver::Version::parse(&cache.latest_version).ok()?;
        return if latest > current {
            Some(cache.latest_version)
        } else {
            None
        };
    }

    // Cache miss or stale — do a live check
    let client = match reqwest::Client::builder()
        .user_agent("astrid-cli")
        .timeout(std::time::Duration::from_secs(5))
        .build()
    {
        Ok(c) => c,
        Err(e) => {
            tracing::debug!("Update check: failed to build HTTP client: {e}");
            return None;
        },
    };

    let url = format!("https://api.github.com/repos/{GITHUB_ORG}/{GITHUB_REPO}/releases/latest");
    let response = match client.get(&url).send().await {
        Ok(r) => r,
        Err(e) => {
            tracing::debug!("Update check: GitHub API request failed: {e}");
            return None;
        },
    };
    if !response.status().is_success() {
        tracing::debug!("Update check: GitHub API returned {}", response.status());
        return None;
    }

    let json: serde_json::Value = match response.json().await {
        Ok(j) => j,
        Err(e) => {
            tracing::debug!("Update check: failed to parse response: {e}");
            return None;
        },
    };
    let tag = json.get("tag_name")?.as_str()?;
    let version_str = tag.strip_prefix('v').unwrap_or(tag);

    // Update cache
    let cache = UpdateCache {
        checked_at: now_epoch(),
        latest_version: version_str.to_owned(),
    };
    if let Ok(json) = serde_json::to_string(&cache) {
        let _ = std::fs::write(&path, json);
    }

    let current = semver::Version::parse(CURRENT_VERSION).ok()?;
    let latest = semver::Version::parse(version_str).ok()?;
    if latest > current {
        Some(version_str.to_string())
    } else {
        None
    }
}

/// Print an update banner if a newer version is available.
pub(crate) async fn print_update_banner() {
    if let Some(latest) = check_for_update_cached().await {
        eprintln!(
            "{}",
            Theme::warning(&format!(
                "Update available: v{CURRENT_VERSION} → v{latest}. Run `astrid self-update` to upgrade (includes distro + capsule sync)."
            ))
        );
    }
}

/// Fetch the latest release metadata from GitHub.
async fn fetch_latest_release(
    client: &reqwest::Client,
) -> anyhow::Result<(String, serde_json::Value)> {
    let url = format!("https://api.github.com/repos/{GITHUB_ORG}/{GITHUB_REPO}/releases/latest");
    let response = client
        .get(&url)
        .send()
        .await
        .context("failed to reach GitHub API")?;
    if !response.status().is_success() {
        bail!("GitHub API returned {}", response.status());
    }
    let json: serde_json::Value = response
        .json()
        .await
        .context("failed to parse API response")?;
    let tag = json
        .get("tag_name")
        .and_then(|v| v.as_str())
        .ok_or_else(|| anyhow::anyhow!("release has no tag_name"))?;
    let version = tag.strip_prefix('v').unwrap_or(tag).to_string();
    Ok((version, json))
}

/// Download and extract the release archive to a temp directory.
/// Returns the path to the extracted directory.
async fn download_and_extract(
    client: &reqwest::Client,
    release: &serde_json::Value,
    version: &str,
    target: &str,
) -> anyhow::Result<tempfile::TempDir> {
    let asset_name = format!("astrid-{version}-{target}.tar.gz");
    let assets = release
        .get("assets")
        .and_then(|a| a.as_array())
        .ok_or_else(|| anyhow::anyhow!("release has no assets"))?;

    let download_url = assets
        .iter()
        .find(|a| a.get("name").and_then(|n| n.as_str()) == Some(&asset_name))
        .and_then(|a| a.get("browser_download_url").and_then(|u| u.as_str()))
        .ok_or_else(|| {
            anyhow::anyhow!("no release asset '{asset_name}' — pre-built binaries may not be available for this platform")
        })?;

    let tmp_dir = tempfile::tempdir()?;
    let archive_path = tmp_dir.path().join(&asset_name);
    // Stream with 100 MB limit.
    let mut response = client.get(download_url).send().await?;
    let mut bytes = Vec::new();
    while let Some(chunk) = response.chunk().await? {
        bytes.extend_from_slice(&chunk);
        anyhow::ensure!(
            bytes.len() <= 100 * 1024 * 1024,
            "release archive exceeds 100 MB limit",
        );
    }
    std::fs::write(&archive_path, &bytes)?;

    let tar_gz = std::fs::File::open(&archive_path)?;
    let decoder = flate2::read::GzDecoder::new(tar_gz);
    let mut archive = tar::Archive::new(decoder);
    archive.unpack(tmp_dir.path())?;

    Ok(tmp_dir)
}

/// Install binaries from an extracted release directory to `~/.astrid/bin/`.
fn install_binaries(from: &Path, install_dir: &Path) -> anyhow::Result<()> {
    std::fs::create_dir_all(install_dir)?;

    for name in ["astrid", "astrid-daemon"] {
        let src = from.join(name);
        if src.exists() {
            let dest = install_dir.join(name);
            std::fs::copy(&src, &dest).with_context(|| format!("failed to install {name}"))?;
            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt;
                std::fs::set_permissions(&dest, std::fs::Permissions::from_mode(0o755))?;
            }
        }
    }
    Ok(())
}

/// Run the self-update command — download and install the latest version,
/// then sync distro and capsule updates.
pub(crate) async fn run_self_update() -> anyhow::Result<()> {
    let target = platform_target()?;

    println!(
        "{}",
        Theme::info(&format!(
            "Checking for updates (current: v{CURRENT_VERSION}, platform: {target})..."
        ))
    );

    let client = reqwest::Client::builder()
        .user_agent("astrid-cli")
        .timeout(std::time::Duration::from_secs(30))
        .build()?;

    let (version_str, release) = fetch_latest_release(&client).await?;

    let current = semver::Version::parse(CURRENT_VERSION)?;
    let latest = semver::Version::parse(&version_str)?;

    if latest <= current {
        println!(
            "{}",
            Theme::success(&format!("Already up to date (v{CURRENT_VERSION})"))
        );
    } else {
        println!(
            "{}",
            Theme::info(&format!("Downloading v{version_str} for {target}..."))
        );

        let tmp_dir = download_and_extract(&client, &release, &version_str, target).await?;
        let extract_dir = tmp_dir
            .path()
            .join(format!("astrid-{version_str}-{target}"));

        let install_dir = astrid_bin_dir()?;
        install_binaries(&extract_dir, &install_dir)?;

        println!(
            "{}",
            Theme::success(&format!(
                "Updated to v{version_str} — installed to {}",
                install_dir.display()
            ))
        );

        if !is_in_path(&install_dir) {
            println!(
                "{}",
                Theme::warning(&format!(
                    "Note: {} is not in your PATH. Run `astrid init` to set it up.",
                    install_dir.display()
                ))
            );
        }
    }

    // Update cache
    let cache = UpdateCache {
        checked_at: now_epoch(),
        latest_version: version_str.clone(),
    };
    if let Ok(path) = cache_path()
        && let Ok(json) = serde_json::to_string(&cache)
    {
        let _ = std::fs::write(path, json);
    }

    // Sync distro + capsules after binary update.
    sync_distro_and_capsules().await?;

    Ok(())
}

/// Re-fetch the distro manifest and sync capsules.
///
/// Compares the remote Distro.toml against the local Distro.lock. If the
/// distro version changed, re-runs init to install new/updated capsules.
/// Then runs `capsule update` for any capsules with newer GitHub releases.
async fn sync_distro_and_capsules() -> anyhow::Result<()> {
    println!();
    println!("{}", Theme::info("Checking distro and capsule updates..."));

    let home = astrid_core::dirs::AstridHome::resolve()?;
    let principal = astrid_core::PrincipalId::default();
    let lock_path = home
        .principal_home(&principal)
        .config_dir()
        .join("distro.lock");

    // Load existing lock to get the distro ID.
    let lock = super::distro::lock::load_lock(&lock_path)?;
    let distro_id = lock.as_ref().map_or("astralis", |l| l.distro.id.as_str());

    // Re-run init which handles: fetch manifest, diff lock, install new capsules.
    // init is idempotent — if lock is fresh it returns immediately.
    if let Err(e) = super::init::run_init(distro_id).await {
        println!("{}", Theme::warning(&format!("Distro sync: {e}")));
    }

    // Update individual capsules (checks GitHub releases for newer versions).
    if let Err(e) = super::capsule::install::update_capsule(None, false).await {
        println!("{}", Theme::warning(&format!("Capsule update: {e}")));
    }

    Ok(())
}

// ── PATH setup helpers ──────────────────────────────────────────────────

/// Check if a directory is already in the current PATH.
fn is_in_path(dir: &Path) -> bool {
    std::env::var_os("PATH").is_some_and(|p| std::env::split_paths(&p).any(|entry| entry == dir))
}

/// Detect the user's shell RC file.
fn detect_shell_rc() -> Option<PathBuf> {
    let home = directories::BaseDirs::new()?.home_dir().to_path_buf();
    let shell = std::env::var("SHELL").unwrap_or_default();

    if shell.ends_with("zsh") {
        Some(home.join(".zshrc"))
    } else if shell.ends_with("bash") {
        // Prefer .bashrc on Linux, .bash_profile on macOS
        let bashrc = home.join(".bashrc");
        let profile = home.join(".bash_profile");
        if cfg!(target_os = "macos") && profile.exists() {
            Some(profile)
        } else if bashrc.exists() {
            Some(bashrc)
        } else {
            Some(home.join(".bashrc"))
        }
    } else if shell.ends_with("fish") {
        Some(home.join(".config/fish/config.fish"))
    } else {
        // Fallback: try zshrc (macOS default), then bashrc
        let zshrc = home.join(".zshrc");
        if zshrc.exists() {
            Some(zshrc)
        } else {
            Some(home.join(".bashrc"))
        }
    }
}

/// Ensure `~/.astrid/bin` is in PATH. Prompts user if interactive.
///
/// Called by `astrid init` after capsule installation.
pub(crate) fn ensure_path_setup() -> anyhow::Result<()> {
    let bin_dir = astrid_bin_dir()?;
    std::fs::create_dir_all(&bin_dir)?;

    if is_in_path(&bin_dir) {
        return Ok(());
    }

    let bin_str = bin_dir.to_string_lossy();
    let Some(rc_file) = detect_shell_rc() else {
        println!(
            "{}",
            Theme::warning(&format!("Add {bin_str} to your PATH manually."))
        );
        return Ok(());
    };

    let export_line = if rc_file.to_string_lossy().contains("fish") {
        format!("fish_add_path {bin_str}")
    } else {
        format!("export PATH=\"{bin_str}:$PATH\"")
    };

    // Check if already in the RC file
    if let Ok(contents) = std::fs::read_to_string(&rc_file)
        && contents.contains(&*bin_str)
    {
        return Ok(()); // Already configured, just not sourced yet
    }

    // Prompt if interactive
    if std::io::stdin().is_terminal() {
        eprint!(
            "\n{bin_str} is not in your PATH. Add it to {}? [Y/n] ",
            rc_file.display()
        );
        std::io::Write::flush(&mut std::io::stderr())?;
        let mut input = String::new();
        std::io::stdin().read_line(&mut input)?;
        let input = input.trim();
        if !input.is_empty() && !input.eq_ignore_ascii_case("y") {
            println!(
                "{}",
                Theme::dimmed(&format!("Skipped. Add manually: {export_line}"))
            );
            return Ok(());
        }
    }

    // Append to RC file
    let mut file = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&rc_file)?;
    std::io::Write::write_all(
        &mut file,
        format!("\n# Astrid OS\n{export_line}\n").as_bytes(),
    )?;

    println!(
        "{}",
        Theme::success(&format!("Added to {}", rc_file.display()))
    );
    println!(
        "  Run: {} (or restart your terminal)",
        Theme::dimmed(&format!("source {}", rc_file.display()))
    );

    Ok(())
}