agentos-client 0.4.4

High-level Rust client SDK for the Agent OS native sidecar (1:1 port of the TypeScript AgentOs client)
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
//! Registry software resolution (= TS `packages/core/src/software-registry.ts`).
//!
//! Each agentOS release publishes its software catalog as digest-addressed
//! `.aospkg` files next to `<registry>/<version>/software/manifest.json`. A
//! registry source resolves to that release's URL and exact digest, then
//! installs through the ordinary URL source, so the sidecar still verifies the
//! bytes. The manifest is fetched by this host process, not by the guest, so VM
//! network permissions do not apply to it.

use std::collections::VecDeque;
use std::sync::Arc;
use std::time::{Duration, Instant};

use once_cell::sync::Lazy;
use reqwest::Url;
use serde::Deserialize;

use crate::error::ClientError;
use crate::software::PackageSource;

/// Root that holds `<version>/software/manifest.json` for each release.
pub const DEFAULT_SOFTWARE_REGISTRY_URL: &str = "https://releases.rivet.dev/agent-os/";
/// Environment variable that overrides the default registry root.
pub const SOFTWARE_REGISTRY_URL_ENV: &str = "AGENTOS_SOFTWARE_REGISTRY_URL";

const MANIFEST_FETCH_TIMEOUT: Duration = Duration::from_secs(30);
const MANIFEST_MAX_BYTES: usize = 8 * 1024 * 1024;
const MANIFEST_CACHE_LIMIT: usize = 32;
// `latest` moves with every release; exact versions are immutable.
const LATEST_MANIFEST_TTL: Duration = Duration::from_secs(5 * 60);

/// Registry location and default release for name-based software.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SoftwareRegistryOptions {
    /// Root URL that holds `<version>/software/manifest.json`. Defaults to
    /// `AGENTOS_SOFTWARE_REGISTRY_URL`, then `https://releases.rivet.dev/agent-os/`.
    pub url: Option<String>,
    /// Release to resolve sources that name no version. Defaults to this
    /// crate's version, or `latest` for unreleased workspace and preview builds.
    pub version: Option<String>,
}

/// A software source for [`crate::AgentOs::install_software`] and
/// [`crate::AgentOsConfig::software_sources`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SoftwareSource {
    /// A URL or trusted local path.
    Package(PackageSource),
    /// A package in an agentOS release manifest, resolved on this host to a
    /// digest-pinned URL source.
    Registry {
        name: String,
        version: Option<String>,
    },
}

impl From<PackageSource> for SoftwareSource {
    fn from(source: PackageSource) -> Self {
        SoftwareSource::Package(source)
    }
}

/// A registry package resolved to its exact artifact.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedRegistrySoftware {
    pub name: String,
    /// Release whose manifest listed the package, such as `0.4.2` or `latest`.
    pub version: String,
    /// Absolute `.aospkg` URL.
    pub url: String,
    /// Exact `sha256:<64 lowercase hex>` content digest.
    pub digest: String,
    pub size_bytes: u64,
    pub manifest_url: String,
}

#[derive(Debug, Clone, Deserialize)]
struct ManifestArtifact {
    name: String,
    digest: String,
    size: u64,
    path: String,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Manifest {
    schema_version: serde_json::Value,
    artifacts: Vec<serde_json::Value>,
}

struct CachedManifest {
    manifest_url: String,
    expires_at: Option<Instant>,
    artifacts: Arc<Vec<ManifestArtifact>>,
}

// Process-wide like the TS module cache, bounded by MANIFEST_CACHE_LIMIT
// distinct registry/version pairs. The guard is never held across an await.
static MANIFEST_CACHE: Lazy<parking_lot::Mutex<VecDeque<CachedManifest>>> =
    Lazy::new(|| parking_lot::Mutex::new(VecDeque::new()));

/// The release a registry source resolves against when it names no version.
///
/// A published client matches its own release so a given agentOS version gets
/// the software built with it. The committed workspace version `0.0.1` and
/// `0.0.0-*` preview builds never publish software, so they use `latest`.
pub fn default_registry_version(version: &str) -> String {
    if version == "0.0.1" || version == "0.0.0" || version.starts_with("0.0.0-") {
        return String::from("latest");
    }
    version.to_string()
}

fn registry_root(options: Option<&SoftwareRegistryOptions>) -> Result<Url, ClientError> {
    let raw = options
        .and_then(|options| options.url.clone())
        .or_else(|| std::env::var(SOFTWARE_REGISTRY_URL_ENV).ok())
        .unwrap_or_else(|| DEFAULT_SOFTWARE_REGISTRY_URL.to_string());
    let normalized = if raw.ends_with('/') {
        raw.clone()
    } else {
        format!("{raw}/")
    };
    let root = Url::parse(&normalized).map_err(|_| {
        ClientError::InvalidConfig(format!(
            "invalid agentOS software registry URL {raw:?}; pass an absolute http(s) URL in software_registry.url or {SOFTWARE_REGISTRY_URL_ENV}"
        ))
    })?;
    ensure_secure_registry_url(&root, "registry URL")?;
    Ok(root)
}

// The manifest supplies the digest every package is verified against, so it
// needs the same transport integrity as the packages: HTTPS, or plain HTTP only
// to a loopback mirror.
fn ensure_secure_registry_url(url: &Url, label: &str) -> Result<(), ClientError> {
    let loopback = url.host_str().is_some_and(|host| {
        host == "localhost"
            || host
                .trim_start_matches('[')
                .trim_end_matches(']')
                .parse::<std::net::IpAddr>()
                .is_ok_and(|ip| ip.is_loopback())
    });
    if url.scheme() == "https" || (url.scheme() == "http" && loopback) {
        return Ok(());
    }
    Err(ClientError::InvalidConfig(format!(
        "agentOS software {label} must use https, or plain http only on a loopback address, got {url}"
    )))
}

fn validate_name(name: &str) -> Result<(), ClientError> {
    let mut chars = name.chars();
    let valid = matches!(chars.next(), Some(c) if c.is_ascii_lowercase() || c.is_ascii_digit())
        && chars.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-');
    if !valid {
        return Err(ClientError::InvalidPackageSource(format!(
            "invalid agentOS registry package name {name:?}; names are lowercase letters, digits, and hyphens, such as \"git\""
        )));
    }
    Ok(())
}

fn validate_version(version: &str) -> Result<(), ClientError> {
    let valid = version == "latest" || {
        let (core, prerelease) = match version.split_once('-') {
            Some((core, prerelease)) => (core, Some(prerelease)),
            None => (version, None),
        };
        let parts: Vec<&str> = core.split('.').collect();
        parts.len() == 3
            && parts
                .iter()
                .all(|part| !part.is_empty() && part.chars().all(|c| c.is_ascii_digit()))
            && prerelease.is_none_or(|prerelease| {
                !prerelease.is_empty()
                    && prerelease
                        .chars()
                        .all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-')
            })
    };
    if !valid {
        return Err(ClientError::InvalidPackageSource(format!(
            "invalid agentOS registry version {version:?}; pass a release version such as \"0.4.2\", or \"latest\""
        )));
    }
    Ok(())
}

fn is_digest(value: &str) -> bool {
    value.strip_prefix("sha256:").is_some_and(|hex| {
        hex.len() == 64 && hex.chars().all(|c| matches!(c, '0'..='9' | 'a'..='f'))
    })
}

fn parse_manifest(bytes: &[u8], manifest_url: &str) -> Result<Vec<ManifestArtifact>, ClientError> {
    let manifest: Manifest = serde_json::from_slice(bytes).map_err(|error| {
        ClientError::PackageDownload(format!(
            "agentOS software manifest {manifest_url} is not a schemaVersion 1 manifest with an artifacts list: {error}"
        ))
    })?;
    if manifest.schema_version != serde_json::json!(1) {
        return Err(ClientError::PackageDownload(format!(
            "agentOS software manifest {manifest_url} is not a schemaVersion 1 manifest with an artifacts list"
        )));
    }
    manifest
        .artifacts
        .into_iter()
        .enumerate()
        .map(|(index, value)| {
            serde_json::from_value::<ManifestArtifact>(value)
                .ok()
                .filter(|artifact| is_digest(&artifact.digest))
                .ok_or_else(|| {
                    ClientError::PackageDownload(format!(
                        "agentOS software manifest {manifest_url} has a malformed artifact at index {index}"
                    ))
                })
        })
        .collect()
}

async fn fetch_manifest(
    manifest_url: &str,
    version: &str,
) -> Result<Vec<ManifestArtifact>, ClientError> {
    let unreachable = |error: &dyn std::fmt::Display| {
        ClientError::PackageDownload(format!(
            "failed to fetch agentOS software manifest {manifest_url}: {error}; check host network access or set software_registry.url / {SOFTWARE_REGISTRY_URL_ENV} to a reachable mirror"
        ))
    };
    let client = reqwest::Client::builder()
        .timeout(MANIFEST_FETCH_TIMEOUT)
        .build()
        .map_err(|error| unreachable(&error))?;
    let mut response = client
        .get(manifest_url)
        .send()
        .await
        .map_err(|error| unreachable(&error))?;
    ensure_secure_registry_url(response.url(), "manifest redirect")?;
    let status = response.status();
    if status == reqwest::StatusCode::NOT_FOUND {
        return Err(ClientError::PackageDownload(format!(
            "agentOS software registry has no release \"{version}\": GET {manifest_url} returned 404; pass a published agentOS version or \"latest\""
        )));
    }
    if !status.is_success() {
        return Err(ClientError::PackageDownload(
            format!(
                "failed to fetch agentOS software manifest {manifest_url}: HTTP {} {}",
                status.as_u16(),
                status.canonical_reason().unwrap_or_default()
            )
            .trim_end()
            .to_string(),
        ));
    }
    if response
        .content_length()
        .is_some_and(|length| length > MANIFEST_MAX_BYTES as u64)
    {
        return Err(ClientError::PackageDownload(format!(
            "agentOS software manifest {manifest_url} exceeds the {MANIFEST_MAX_BYTES}-byte limit"
        )));
    }
    let mut body = Vec::new();
    while let Some(chunk) = response
        .chunk()
        .await
        .map_err(|error| unreachable(&error))?
    {
        if body.len() + chunk.len() > MANIFEST_MAX_BYTES {
            return Err(ClientError::PackageDownload(format!(
                "agentOS software manifest {manifest_url} exceeds the {MANIFEST_MAX_BYTES}-byte limit"
            )));
        }
        body.extend_from_slice(&chunk);
    }
    parse_manifest(&body, manifest_url)
}

async fn load_manifest(
    manifest_url: &str,
    version: &str,
) -> Result<Arc<Vec<ManifestArtifact>>, ClientError> {
    {
        let mut cache = MANIFEST_CACHE.lock();
        let now = Instant::now();
        if let Some(index) = cache
            .iter()
            .position(|entry| entry.manifest_url == manifest_url)
        {
            if cache[index]
                .expires_at
                .is_none_or(|expires_at| expires_at > now)
            {
                return Ok(cache[index].artifacts.clone());
            }
            cache.remove(index);
        }
    }
    // Failed fetches are never cached, so the next call retries.
    let artifacts = Arc::new(fetch_manifest(manifest_url, version).await?);
    let mut cache = MANIFEST_CACHE.lock();
    cache.retain(|entry| entry.manifest_url != manifest_url);
    while cache.len() >= MANIFEST_CACHE_LIMIT {
        cache.pop_front();
    }
    cache.push_back(CachedManifest {
        manifest_url: manifest_url.to_string(),
        expires_at: if version == "latest" {
            Some(Instant::now() + LATEST_MANIFEST_TTL)
        } else {
            None
        },
        artifacts: artifacts.clone(),
    });
    Ok(artifacts)
}

fn edit_distance(left: &str, right: &str) -> usize {
    let right: Vec<char> = right.chars().collect();
    let mut previous: Vec<usize> = (0..=right.len()).collect();
    for (i, left_char) in left.chars().enumerate() {
        let mut current = vec![i + 1];
        for (j, right_char) in right.iter().enumerate() {
            current.push(
                (previous[j + 1] + 1)
                    .min(current[j] + 1)
                    .min(previous[j] + usize::from(left_char != *right_char)),
            );
        }
        previous = current;
    }
    previous[right.len()]
}

fn close_matches<'a>(name: &str, names: impl Iterator<Item = &'a str>) -> Vec<&'a str> {
    let mut scored: Vec<(usize, &str)> = names
        .map(|candidate| {
            let distance = if candidate.contains(name) || name.contains(candidate) {
                0
            } else {
                edit_distance(name, candidate)
            };
            (distance, candidate)
        })
        .filter(|(distance, _)| *distance <= 2)
        .collect();
    scored.sort();
    scored
        .into_iter()
        .take(5)
        .map(|(_, candidate)| candidate)
        .collect()
}

/// Resolve a registry package name to the exact `.aospkg` URL and digest listed
/// in a release manifest. `version` overrides `registry.version`. Manifests are
/// cached per registry and version for the life of the process; `latest` is
/// refreshed every five minutes.
pub async fn resolve_registry_software(
    name: &str,
    version: Option<&str>,
    registry: Option<&SoftwareRegistryOptions>,
) -> Result<ResolvedRegistrySoftware, ClientError> {
    validate_name(name)?;
    let version = version
        .map(str::to_string)
        .or_else(|| registry.and_then(|registry| registry.version.clone()))
        .unwrap_or_else(|| default_registry_version(env!("CARGO_PKG_VERSION")));
    validate_version(&version)?;
    let manifest_url = registry_root(registry)?
        .join(&format!("{version}/software/manifest.json"))
        .map_err(|error| ClientError::InvalidConfig(format!("invalid registry URL: {error}")))?;
    let manifest_url = manifest_url.to_string();
    let artifacts = load_manifest(&manifest_url, &version).await?;
    let Some(artifact) = artifacts.iter().find(|artifact| artifact.name == name) else {
        let suggestions = close_matches(
            name,
            artifacts.iter().map(|artifact| artifact.name.as_str()),
        );
        let hint = if suggestions.is_empty() {
            String::from("; browse https://rivet.dev/agentos/docs/registry for available packages")
        } else {
            format!(
                "; did you mean {}?",
                suggestions
                    .iter()
                    .map(|suggestion| format!("\"{suggestion}\""))
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        };
        return Err(ClientError::SoftwareNotFound(format!(
            "agentOS registry release \"{version}\" has no package \"{name}\" ({manifest_url}){hint}"
        )));
    };
    let url = Url::parse(&manifest_url)
        .and_then(|base| base.join(&artifact.path))
        .map_err(|error| {
            ClientError::PackageDownload(format!(
                "agentOS software manifest {manifest_url} lists {name} at an invalid path: {error}"
            ))
        })?;
    if url.scheme() != "https" && url.scheme() != "http" {
        return Err(ClientError::PackageDownload(format!(
            "agentOS software manifest {manifest_url} lists {name} at a non-http(s) URL: {url}"
        )));
    }
    Ok(ResolvedRegistrySoftware {
        name: name.to_string(),
        version,
        url: url.to_string(),
        digest: artifact.digest.clone(),
        size_bytes: artifact.size,
        manifest_url,
    })
}

/// Resolve a [`SoftwareSource`] to the package source the sidecar acquires.
pub async fn resolve_software_source(
    source: SoftwareSource,
    registry: Option<&SoftwareRegistryOptions>,
) -> Result<PackageSource, ClientError> {
    match source {
        SoftwareSource::Package(source) => Ok(source),
        SoftwareSource::Registry { name, version } => {
            let resolved = resolve_registry_software(&name, version.as_deref(), registry).await?;
            Ok(PackageSource::Url {
                url: resolved.url,
                expected_digest: Some(resolved.digest),
            })
        }
    }
}

/// Test hook: forget cached manifests.
#[doc(hidden)]
pub fn clear_software_registry_cache() {
    MANIFEST_CACHE.lock().clear();
}

/// Test hook: number of cached manifests.
#[doc(hidden)]
pub fn software_registry_cache_size() -> usize {
    MANIFEST_CACHE.lock().len()
}