Skip to main content

agentmux_srv_core/backend/container/
cli_release.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The container route for the agent CLIs that are not on npm: grok and muse (a pinned release file). The same
5//! registry as the host's own copy (`backend::cli_download::SPECS`) gives the URL, the SHA-256 and the size, so one pin
6//! covers both. No vendor script is run, and nothing a caller sends decides what is fetched: the script gets its
7//! values from the registry as positional parameters, never spliced into the script text.
8//!
9//! The install lands in the per-agent volume (`/home/agent/.agentmux/cli`) beside the npm installs and ends with the
10//! same completion marker: `bin/<command>` for a release file.
11//! SPEC_ISOLATED_INSTALL_FOR_NON_NPM_CLIS_2026_11_05.md, PR 5d.
12
13
14use crate::backend::cli_download::{self, Archive, Asset, DownloadSpec};
15use crate::backend::container::cli::InstallLimits;
16
17/// How a CLI gets into the container.
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum Source {
20    /// `npm install` of the provider's package (the usual route).
21    Npm,
22    /// One pinned file from the vendor, checked against a SHA-256 in the registry.
23    Download,
24}
25
26/// A file the install script reads, written into the container's `/tmp` before it runs.
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct Upload {
29    pub name: String,
30    pub bytes: Vec<u8>,
31}
32
33impl Source {
34    /// The time limits of one install. A download is one file.
35    pub fn limits(self) -> InstallLimits {
36        match self {
37            Source::Npm | Source::Download => InstallLimits::default(),
38        }
39    }
40}
41
42/// The registry entry for a provider's non-npm route, if it has one that can run in a Linux container.
43pub fn source_for(provider_id: &str) -> Option<(Source, &'static str)> {
44    let spec = cli_download::download_spec(provider_id)?;
45    has_linux(spec.assets).then_some((Source::Download, spec.version))
46}
47
48fn has_linux(assets: &[Asset]) -> bool {
49    ["x86_64", "aarch64"].iter().all(|arch| assets.iter().any(|a| a.os == "linux" && a.arch == *arch))
50}
51
52fn kind_word(a: Archive) -> &'static str {
53    match a {
54        Archive::TarGz => "tar.gz",
55        Archive::Raw => "raw",
56        // No Linux asset is a zip; the script refuses the word, so one added later fails loudly.
57        Archive::Zip => "zip",
58    }
59}
60
61/// Six positional parameters per Linux asset: machine, URL, SHA-256, size, archive kind, executable path.
62fn asset_params(assets: &[Asset]) -> Vec<String> {
63    let mut out = Vec::new();
64    for a in assets.iter().filter(|a| a.os == "linux") {
65        out.extend([
66            a.arch.to_string(),
67            a.url.to_string(),
68            a.sha256.to_string(),
69            a.size.to_string(),
70            kind_word(a.archive).to_string(),
71            a.exe_in_archive.to_string(),
72        ]);
73    }
74    out
75}
76
77fn head(script: &str, dir: &str, marker: &str, command: &str, limits: InstallLimits) -> Vec<String> {
78    vec![
79        "sh".into(),
80        "-c".into(),
81        script.into(),
82        "agentmux-cli-install".into(),
83        dir.into(),
84        marker.into(),
85        command.into(),
86        limits.script.as_secs().max(1).to_string(),
87    ]
88}
89
90/// argv that installs a pinned release file. `None` when the provider has no entry in the download registry.
91pub fn download_argv(dir: &str, marker: &str, command: &str, provider_id: &str, limits: InstallLimits) -> Option<Vec<String>> {
92    let spec: &DownloadSpec = cli_download::download_spec(provider_id)?;
93    let mut argv = head(DOWNLOAD_SCRIPT.as_str(), dir, marker, command, limits);
94    argv.extend(asset_params(spec.assets));
95    Some(argv)
96}
97
98/// The steps of the script. `$1` install dir, `$2` marker, `$3` command, `$4` seconds one network step may
99/// run; the rest is the script's own.
100const COMMON: &str = r#"set -eu
101DIR="$1"; MARKER="$2"; BIN="$3"; LIMIT="$4"; shift 4
102case "$(uname -m)" in
103    x86_64|amd64) ARCH=x86_64 ;;
104    aarch64|arm64) ARCH=aarch64 ;;
105    *) echo "there is no build of $BIN for $(uname -m)" >&2; exit 3 ;;
106esac
107# pick MACHINE URL SHA SIZE KIND EXE ...: this machine's group of six, as URL SHA SIZE KIND EXE
108pick() {
109    URL=""
110    while [ "$#" -ge 6 ]; do
111        if [ "$1" = "$ARCH" ]; then URL="$2"; SHA="$3"; SIZE="$4"; KIND="$5"; EXE="$6"; fi
112        shift 6
113    done
114    [ -n "$URL" ] || { echo "there is no build of $BIN for $ARCH Linux" >&2; exit 3; }
115}
116# fetch URL SHA SIZE OUT: https only, no more than the pinned size, and exactly the pinned hash
117fetch() {
118    MAX=$(( $3 + 1048576 ))
119    timeout -k 15 "$LIMIT" curl -fsSL --proto '=https' --tlsv1.2 --connect-timeout 15 --max-filesize "$MAX" -o "$4" "$1"
120    [ "$(wc -c < "$4")" -le "$MAX" ] || { echo "the download is larger than the pinned file" >&2; exit 4; }
121    GOT=$(sha256sum "$4" | cut -d' ' -f1)
122    [ "$GOT" = "$2" ] || { echo "the download does not match the pinned hash (expected $2, got $GOT)" >&2; exit 4; }
123}
124# unpack KIND FILE EXE OUT: the one executable, made 0755. An archive is unpacked into a throwaway folder; tar
125# refuses a member that names a parent folder, and only a plain file is taken from it.
126unpack() {
127    case "$1" in
128        raw) cp "$2" "$4" ;;
129        tar.gz)
130            X="$STAGE/unpacked"; mkdir -p "$X"
131            tar -xzf "$2" -C "$X" --no-same-owner
132            [ -f "$X/$3" ] && [ ! -L "$X/$3" ] || { echo "the archive has no file $3" >&2; exit 4; }
133            cp "$X/$3" "$4" ;;
134        *) echo "unknown archive kind $1" >&2; exit 4 ;;
135    esac
136    chmod 0755 "$4"
137}
138STAGE="$DIR/.staging"
139mkdir -p "$DIR"
140"#;
141
142/// `$1...` six parameters per Linux asset of the CLI. The staging folder is inside the volume, so an attempt that
143/// dies half-way never leaves a half-written `bin/`; the marker is written last.
144const DOWNLOAD_BODY: &str = r#"pick "$@"
145rm -rf "$STAGE"
146mkdir -p "$STAGE/bin"
147trap 'rm -rf "$STAGE"' EXIT
148fetch "$URL" "$SHA" "$SIZE" "$STAGE/download"
149unpack "$KIND" "$STAGE/download" "$EXE" "$STAGE/bin/$BIN"
150rm -f "$DIR"/.installed-*
151rm -rf "$DIR/bin"
152mv "$STAGE/bin" "$DIR/bin"
153: > "$DIR/$MARKER""#;
154
155/// The script, assembled once.
156pub static DOWNLOAD_SCRIPT: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| format!("{COMMON}{DOWNLOAD_BODY}"));
157
158#[cfg(test)]
159#[path = "cli_release_tests.rs"]
160mod tests;