arabica-cli 0.13.1

Arabica: a terminal coding agent with a fullscreen TUI, ACP support for Zed, and one-shot runs
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
//! Signed release updates for standalone CLI installations.

use std::fs::{self, File, OpenOptions};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use base64::Engine;
use clap::{Args, ValueEnum};
use ed25519_dalek::{Signature, VerifyingKey};
use semver::Version;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
const REPOSITORY: &str = "https://github.com/haowei2000/Arabica";
const RELEASE_API: &str = "https://api.github.com/repos/haowei2000/Arabica/releases/latest";
const MAX_ARCHIVE: usize = 128 * 1024 * 1024;
const MAX_BINARY: u64 = 256 * 1024 * 1024;
const CHECK_INTERVAL: u64 = 24 * 60 * 60;
const PUBLIC_KEY: &str = include_str!("update-public-key.txt");

#[derive(Args, Debug)]
pub struct UpdateArgs {
    /// Report the latest stable version without installing it.
    #[arg(long, conflicts_with_all = ["auto", "background"])]
    pub check: bool,
    /// Enable or disable daily background updates for interactive CLI sessions.
    #[arg(long, value_enum, conflicts_with = "background")]
    pub auto: Option<AutomaticUpdates>,
    /// Output a machine-readable result.
    #[arg(long, conflicts_with = "background")]
    pub json: bool,
    #[arg(long, hide = true)]
    pub background: bool,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum AutomaticUpdates {
    On,
    Off,
}

#[derive(Deserialize, Serialize)]
#[serde(default)]
struct UpdateState {
    enabled: bool,
    last_checked_at: u64,
    installed_version: Option<String>,
    last_error: Option<String>,
}

impl Default for UpdateState {
    fn default() -> Self {
        Self {
            enabled: true,
            last_checked_at: 0,
            installed_version: None,
            last_error: None,
        }
    }
}

#[derive(Deserialize)]
struct Release {
    tag_name: String,
    draft: bool,
    prerelease: bool,
}

#[derive(Deserialize)]
struct UpdateManifest {
    version: String,
    archive: String,
    sha256: String,
    size: usize,
}

#[derive(Serialize)]
struct UpdateResult {
    current_version: &'static str,
    latest_version: String,
    update_available: bool,
    updated: bool,
    automatic_updates: bool,
}

fn now() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

fn read_state(home: &Path) -> Result<UpdateState> {
    match fs::read(home.join("updates.json")) {
        Ok(bytes) => Ok(serde_json::from_slice(&bytes)?),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(UpdateState::default()),
        Err(error) => Err(error.into()),
    }
}

fn write_state(home: &Path, state: &UpdateState) -> Result<()> {
    let mut temporary = tempfile::NamedTempFile::new_in(home)?;
    serde_json::to_writer_pretty(temporary.as_file_mut(), state)?;
    temporary.as_file_mut().sync_all()?;
    temporary.persist(home.join("updates.json"))?;
    Ok(())
}

fn lock(home: &Path) -> Result<File> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::DirBuilderExt;
        fs::DirBuilder::new()
            .recursive(true)
            .mode(0o700)
            .create(home)?;
    }
    #[cfg(not(unix))]
    fs::create_dir_all(home)?;
    let mut options = OpenOptions::new();
    options.read(true).write(true).create(true).truncate(false);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(0o600);
    }
    let file = options.open(home.join("updates.lock"))?;
    fs2::FileExt::try_lock_exclusive(&file).map_err(|_| "another update operation is running")?;
    Ok(file)
}

fn installation_error(executable: &Path) -> Option<&'static str> {
    let text = executable.to_string_lossy().replace('\\', "/");
    let lower = text.to_ascii_lowercase();
    if text.contains(".app/Contents/") {
        Some("the desktop app manages its bundled CLI; use Check for Updates in the app")
    } else if text.contains("/Cellar/")
        || text.contains("/Caskroom/")
        || text.contains("/nix/store/")
        || lower.contains("/scoop/apps/")
        || lower.contains("/microsoft/winget/")
        || lower.contains("/chocolatey/")
    {
        Some("this installation is managed by a package manager; update it with that manager")
    } else if text.contains("/target/debug/") || text.contains("/target/release/") {
        Some("this is a development build; rebuild it from source")
    } else {
        let cargo_bin = std::env::var_os("CARGO_HOME")
            .map(PathBuf::from)
            .or_else(|| {
                std::env::var_os("HOME")
                    .or_else(|| std::env::var_os("USERPROFILE"))
                    .map(|home| PathBuf::from(home).join(".cargo"))
            })
            .map(|home| home.join("bin"));
        let cargo_receipt = executable
            .parent()
            .and_then(Path::parent)
            .is_some_and(|root| {
                root.join(".crates.toml").is_file() || root.join(".crates2.json").is_file()
            });
        if cargo_bin.as_deref() == executable.parent() || cargo_receipt {
            Some("this is a Cargo installation; run `cargo install arabica-cli --locked --force`")
        } else if matches!(
            executable.parent().and_then(Path::to_str),
            Some("/usr/bin" | "/bin")
        ) {
            Some("this is a system installation; update it with your package manager")
        } else {
            None
        }
    }
}

fn lock_installation(executable: &Path) -> Result<File> {
    let parent = executable
        .parent()
        .ok_or("executable has no installation directory")?;
    let mut options = OpenOptions::new();
    options.read(true).write(true).create(true).truncate(false);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(0o600);
    }
    let file = options.open(parent.join(".arabica-update.lock"))?;
    fs2::FileExt::try_lock_exclusive(&file)
        .map_err(|_| "another process is updating this installation")?;
    Ok(file)
}

/// Launch a separate, quiet updater so downloads never block or redraw a TUI.
/// Only the interactive entry point calls this; ACP and scripts do not.
pub fn schedule_background_update() {
    if cfg!(debug_assertions)
        || std::env::var_os("ARABICA_NO_UPDATE").is_some()
        || std::env::var_os("CI").is_some()
    {
        return;
    }
    let Ok(home) = arabica_adapters::default_arabica_home() else {
        return;
    };
    let Ok(state) = read_state(&home) else { return };
    if !state.enabled || now().saturating_sub(state.last_checked_at) < CHECK_INTERVAL {
        return;
    }
    let Ok(executable) = std::env::current_exe().and_then(fs::canonicalize) else {
        return;
    };
    if installation_error(&executable).is_some() {
        return;
    }
    let mut command = Command::new(executable);
    command
        .args(["update", "--background"])
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null());
    #[cfg(windows)]
    {
        use std::os::windows::process::CommandExt;
        command.creation_flags(0x08000000); // CREATE_NO_WINDOW
    }
    if let Ok(mut child) = command.spawn() {
        // Reap the helper when the interactive process remains alive.
        let _ = std::thread::Builder::new()
            .name("arabica-update".into())
            .spawn(move || {
                let _ = child.wait();
            });
    }
}

pub async fn run(args: UpdateArgs) -> i32 {
    let result = run_inner(&args).await;
    match result {
        Ok(()) => 0,
        Err(error) => {
            if !args.background {
                eprintln!("error: {error}");
            }
            1
        }
    }
}

async fn run_inner(args: &UpdateArgs) -> Result<()> {
    let home = arabica_adapters::default_arabica_home()?;
    let _lock = lock(&home)?;
    let mut state = read_state(&home)?;
    if let Some(automatic) = args.auto {
        state.enabled = matches!(automatic, AutomaticUpdates::On);
        write_state(&home, &state)?;
        if args.json {
            println!(
                "{}",
                serde_json::json!({"automatic_updates": state.enabled})
            );
        } else {
            println!(
                "Automatic CLI updates: {}",
                if state.enabled { "on" } else { "off" }
            );
        }
        return Ok(());
    }
    if args.background
        && (!state.enabled || now().saturating_sub(state.last_checked_at) < CHECK_INTERVAL)
    {
        return Ok(());
    }
    let _installation_lock = if !args.check {
        let executable = fs::canonicalize(std::env::current_exe()?)?;
        if let Some(message) = installation_error(&executable) {
            return Err(message.into());
        }
        Some(lock_installation(&executable)?)
    } else {
        None
    };
    let current = Version::parse(env!("CARGO_PKG_VERSION"))?;
    let client = reqwest::Client::builder()
        .user_agent(concat!("Arabica/", env!("CARGO_PKG_VERSION")))
        .connect_timeout(Duration::from_secs(5))
        .timeout(Duration::from_secs(120))
        .build()?;
    state.last_checked_at = now();
    state.last_error = None;
    write_state(&home, &state)?;
    let outcome = perform_update(&client, &current, args.check, state.enabled).await;
    match outcome {
        Ok(result) => {
            if result.updated {
                state.installed_version = Some(result.latest_version.clone());
            }
            write_state(&home, &state)?;
            if !args.background {
                if args.json {
                    println!("{}", serde_json::to_string(&result)?);
                } else if result.updated {
                    println!(
                        "Updated Arabica to {}. New sessions will use it.",
                        result.latest_version
                    );
                } else if result.update_available {
                    println!(
                        "Arabica {} is available (installed: {}). Run `arabica update`.",
                        result.latest_version, result.current_version
                    );
                } else {
                    println!("Arabica {} is up to date.", result.current_version);
                }
            }
            Ok(())
        }
        Err(error) => {
            state.last_error = Some(error.to_string().chars().take(512).collect());
            let _ = write_state(&home, &state);
            Err(error)
        }
    }
}

async fn download(client: &reqwest::Client, url: &str, maximum: usize) -> Result<Vec<u8>> {
    let mut response = client.get(url).send().await?.error_for_status()?;
    if response
        .content_length()
        .is_some_and(|length| length > maximum as u64)
    {
        return Err("update download exceeds its size limit".into());
    }
    let mut bytes = Vec::new();
    while let Some(chunk) = response.chunk().await? {
        if chunk.len() > maximum.saturating_sub(bytes.len()) {
            return Err("update download exceeds its size limit".into());
        }
        bytes.extend_from_slice(&chunk);
    }
    Ok(bytes)
}

fn platform() -> Result<(&'static str, &'static str)> {
    match (std::env::consts::OS, std::env::consts::ARCH) {
        ("macos", "aarch64") => Ok((
            "structure-macos-aarch64.tar.gz",
            "structure-aarch64-apple-darwin/arabica",
        )),
        ("macos", "x86_64") => Ok((
            "structure-macos-x86_64.tar.gz",
            "structure-x86_64-apple-darwin/arabica",
        )),
        ("linux", "aarch64") => Ok((
            "structure-linux-aarch64.tar.gz",
            "structure-aarch64-unknown-linux-gnu/arabica",
        )),
        ("linux", "x86_64") => Ok((
            "structure-linux-x86_64.tar.gz",
            "structure-x86_64-unknown-linux-gnu/arabica",
        )),
        ("windows", "x86_64") => Ok((
            "structure-windows-x86_64.zip",
            "structure-x86_64-pc-windows-msvc/arabica.exe",
        )),
        _ => Err("no official update archive exists for this platform".into()),
    }
}

async fn perform_update(
    client: &reqwest::Client,
    current: &Version,
    check: bool,
    automatic: bool,
) -> Result<UpdateResult> {
    let release: Release =
        serde_json::from_slice(&download(client, RELEASE_API, 2 * 1024 * 1024).await?)?;
    let version = Version::parse(
        release
            .tag_name
            .strip_prefix('v')
            .ok_or("invalid release tag")?,
    )?;
    if release.draft || release.prerelease || !version.pre.is_empty() {
        return Err("latest release is not a stable release".into());
    }
    let mut result = UpdateResult {
        current_version: env!("CARGO_PKG_VERSION"),
        latest_version: version.to_string(),
        update_available: version > *current,
        updated: false,
        automatic_updates: automatic,
    };
    if check || !result.update_available {
        return Ok(result);
    }
    let (archive_name, binary_path) = platform()?;
    let root = format!("{REPOSITORY}/releases/download/v{version}/{archive_name}");
    let manifest = download(client, &format!("{root}.update.json"), 4096).await?;
    let signature = download(client, &format!("{root}.sig"), 4096).await?;
    let public_key = base64::engine::general_purpose::STANDARD.decode(PUBLIC_KEY.trim())?;
    let key = VerifyingKey::from_bytes(
        &public_key
            .try_into()
            .map_err(|_| "invalid update public key")?,
    )?;
    let signature = base64::engine::general_purpose::STANDARD
        .decode(std::str::from_utf8(&signature)?.trim())?;
    key.verify_strict(&manifest, &Signature::from_slice(&signature)?)
        .map_err(|_| "update signature verification failed")?;
    let manifest: UpdateManifest = serde_json::from_slice(&manifest)?;
    if manifest.version != version.to_string()
        || manifest.archive != archive_name
        || manifest.size == 0
        || manifest.size > MAX_ARCHIVE
        || manifest.sha256.len() != 64
        || !manifest.sha256.bytes().all(|byte| byte.is_ascii_hexdigit())
    {
        return Err("signed update manifest does not match the requested release".into());
    }
    let archive = download(client, &root, manifest.size).await?;
    if archive.len() != manifest.size
        || hex::encode(Sha256::digest(&archive)) != manifest.sha256.to_ascii_lowercase()
    {
        return Err("update checksum verification failed".into());
    }
    let temporary = tempfile::tempdir()?;
    let binary = temporary.path().join(if cfg!(windows) {
        "arabica.exe"
    } else {
        "arabica"
    });
    extract_binary(
        &archive,
        archive_name.ends_with(".zip"),
        binary_path,
        &binary,
    )?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        fs::set_permissions(&binary, fs::Permissions::from_mode(0o755))?;
    }
    self_replace::self_replace(&binary)?;
    result.updated = true;
    Ok(result)
}

fn extract_binary(archive: &[u8], zip: bool, expected_path: &str, output: &Path) -> Result<()> {
    let mut destination = File::create(output)?;
    if zip {
        let mut archive = zip::ZipArchive::new(std::io::Cursor::new(archive))?;
        let mut entry = archive.by_name(expected_path)?;
        if !entry.is_file()
            || entry
                .unix_mode()
                .is_some_and(|mode| mode & 0o170000 != 0 && mode & 0o170000 != 0o100000)
            || entry.size() > MAX_BINARY
        {
            return Err("update archive contains an invalid binary".into());
        }
        std::io::copy(&mut entry.by_ref().take(MAX_BINARY + 1), &mut destination)?;
    } else {
        let decoder = flate2::read::GzDecoder::new(archive).take(MAX_BINARY * 2);
        let mut archive = tar::Archive::new(decoder);
        let mut found = false;
        for entry in archive.entries()?.take(1000) {
            let mut entry = entry?;
            if entry.path()?.as_ref() != Path::new(expected_path) {
                continue;
            }
            if !entry.header().entry_type().is_file() || entry.size() > MAX_BINARY {
                return Err("update archive contains an invalid binary".into());
            }
            std::io::copy(&mut entry.by_ref().take(MAX_BINARY + 1), &mut destination)?;
            found = true;
            break;
        }
        if !found {
            return Err("update archive does not contain the expected binary".into());
        }
    }
    if destination.metadata()?.len() == 0 || destination.metadata()?.len() > MAX_BINARY {
        return Err("update binary has an invalid size".into());
    }
    destination.flush()?;
    destination.sync_all()?;
    Ok(())
}