Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

refactor(upgrade): streamline release asset name resolution and add end-to-end tests

Ruben Licio Reis6669690c5ac7
diff --git a/crates/ai-memory-cli/src/commands/upgrade.rs b/crates/ai-memory-cli/src/commands/upgrade.rs
index e0e3c62b..05ceac55 100644
--- a/crates/ai-memory-cli/src/commands/upgrade.rs
+++ b/crates/ai-memory-cli/src/commands/upgrade.rs
@@ -301,7 +301,16 @@ fn release_base_url(config: &Config) -> String {
}
fn release_asset_name() -> Option<&'static str> {
- match (std::env::consts::OS, std::env::consts::ARCH) {
+ release_asset_name_for(std::env::consts::OS, std::env::consts::ARCH)
+}
+
+/// Map an OS/arch pair to the GitHub release tarball name.
+///
+/// Pure so every CI host can assert the full Unix matrix (and Windows →
+/// `None`) without cross-compiling. Callers use [`release_asset_name`] for
+/// the running host.
+fn release_asset_name_for(os: &str, arch: &str) -> Option<&'static str> {
+ match (os, arch) {
("linux", "x86_64") => Some("ai-memory-linux-x86_64.tar.gz"),
("linux", "aarch64") => Some("ai-memory-linux-aarch64.tar.gz"),
("macos", "aarch64") => Some("ai-memory-macos-aarch64.tar.gz"),
@@ -806,17 +815,43 @@ mod tests {
use tar::Header;
#[test]
- fn asset_name_matches_release_matrix_for_this_host() {
- // Compile-time host must map or explicitly fail — never invent a name.
- let name = release_asset_name();
- match (std::env::consts::OS, std::env::consts::ARCH) {
- ("linux", "x86_64") => assert_eq!(name, Some("ai-memory-linux-x86_64.tar.gz")),
- ("linux", "aarch64") => assert_eq!(name, Some("ai-memory-linux-aarch64.tar.gz")),
- ("macos", "aarch64") => assert_eq!(name, Some("ai-memory-macos-aarch64.tar.gz")),
- ("macos", "x86_64") => assert_eq!(name, Some("ai-memory-macos-x86_64.tar.gz")),
- ("windows", _) => assert_eq!(name, None),
- _ => {}
+ fn release_asset_name_for_covers_unix_matrix_and_refusals() {
+ // Table-driven so Linux CI still guards macos-* and windows → None.
+ let cases: &[(&str, &str, Option<&str>)] = &[
+ (
+ "linux",
+ "x86_64",
+ Some("ai-memory-linux-x86_64.tar.gz"),
+ ),
+ (
+ "linux",
+ "aarch64",
+ Some("ai-memory-linux-aarch64.tar.gz"),
+ ),
+ (
+ "macos",
+ "aarch64",
+ Some("ai-memory-macos-aarch64.tar.gz"),
+ ),
+ ("macos", "x86_64", Some("ai-memory-macos-x86_64.tar.gz")),
+ ("windows", "x86_64", None),
+ ("windows", "aarch64", None),
+ ("linux", "arm", None),
+ ("freebsd", "x86_64", None),
+ ("unknown", "unknown", None),
+ ];
+ for &(os, arch, expected) in cases {
+ assert_eq!(
+ release_asset_name_for(os, arch),
+ expected,
+ "os={os} arch={arch}"
+ );
}
+ // Host still maps through the same function (never invent a name).
+ assert_eq!(
+ release_asset_name(),
+ release_asset_name_for(std::env::consts::OS, std::env::consts::ARCH)
+ );
}
#[test]
diff --git a/crates/ai-memory-cli/tests/suite/main.rs b/crates/ai-memory-cli/tests/suite/main.rs
index f98ddeb2..0e72cf45 100644
--- a/crates/ai-memory-cli/tests/suite/main.rs
+++ b/crates/ai-memory-cli/tests/suite/main.rs
@@ -26,3 +26,4 @@ mod routing_instructions;
mod routing_skills;
mod serve_shutdown;
mod shutdown_signals;
+mod upgrade_e2e;
diff --git a/crates/ai-memory-cli/tests/suite/upgrade_e2e.rs b/crates/ai-memory-cli/tests/suite/upgrade_e2e.rs
new file mode 100644
index 00000000..9586a26a
--- /dev/null
+++ b/crates/ai-memory-cli/tests/suite/upgrade_e2e.rs
@@ -0,0 +1,214 @@
+//! Hermetic end-to-end coverage for native `ai-memory upgrade`.
+//!
+//! Unit tests in `commands/upgrade.rs` cover checksum / extract / atomic
+//! replace in isolation. This drives the shipped binary through the same
+//! path an operator hits: download from a fixture Releases base → verify →
+//! replace the on-disk exe → refresh a sibling `hooks/` tree. No real
+//! GitHub, no Docker.
+//!
+//! Unix-only: Windows self-replace is refused by design.
+
+#![cfg(unix)]
+
+/// Spawns a loopback release fixture and a real CLI subprocess: seconds,
+/// not milliseconds — slow tier (`cargo tf` / CI), not the everyday loop.
+mod slow {
+ use std::fs;
+ use std::io::Write as _;
+ use std::os::unix::fs::PermissionsExt as _;
+ use std::path::{Path, PathBuf};
+ use std::sync::Arc;
+
+ use axum::Router;
+ use axum::body::Body;
+ use axum::extract::{Path as AxumPath, State};
+ use axum::http::{StatusCode, header};
+ use axum::response::{IntoResponse, Response};
+ use axum::routing::get;
+ use flate2::Compression;
+ use flate2::write::GzEncoder;
+ use sha2::{Digest as _, Sha256};
+ use tar::Header;
+
+ use crate::e2e_support::hermetic;
+
+ const BIN: &str = env!("CARGO_BIN_EXE_ai-memory");
+ const TAG: &str = "v9.9.9";
+ /// Distinct bytes so we can prove the on-disk binary was swapped.
+ const NEW_BINARY: &[u8] = b"#!/bin/sh\necho upgraded-native-e2e\n";
+ const NEW_HOOK: &[u8] = b"#!/bin/sh\necho refreshed-hook\n";
+ const OLD_HOOK: &[u8] = b"old-hook-body\n";
+
+ #[derive(Clone)]
+ struct ReleaseFixture {
+ asset: String,
+ archive: Arc<Vec<u8>>,
+ checksum_body: String,
+ }
+
+ fn host_asset_name() -> &'static str {
+ match (std::env::consts::OS, std::env::consts::ARCH) {
+ ("linux", "x86_64") => "ai-memory-linux-x86_64.tar.gz",
+ ("linux", "aarch64") => "ai-memory-linux-aarch64.tar.gz",
+ ("macos", "aarch64") => "ai-memory-macos-aarch64.tar.gz",
+ ("macos", "x86_64") => "ai-memory-macos-x86_64.tar.gz",
+ other => panic!("native upgrade e2e unsupported on {other:?}"),
+ }
+ }
+
+ fn build_release_tarball() -> Vec<u8> {
+ let mut tar_bytes = Vec::new();
+ {
+ let mut builder = tar::Builder::new(&mut tar_bytes);
+ append_regular(&mut builder, "ai-memory", NEW_BINARY, 0o755);
+ append_regular(
+ &mut builder,
+ "hooks/claude-code/new.sh",
+ NEW_HOOK,
+ 0o755,
+ );
+ builder.finish().expect("finish tar");
+ }
+ let mut gz_bytes = Vec::new();
+ {
+ let mut enc = GzEncoder::new(&mut gz_bytes, Compression::fast());
+ enc.write_all(&tar_bytes).expect("gzip write");
+ enc.finish().expect("gzip finish");
+ }
+ gz_bytes
+ }
+
+ fn append_regular(
+ builder: &mut tar::Builder<&mut Vec<u8>>,
+ path: &str,
+ body: &[u8],
+ mode: u32,
+ ) {
+ let mut header = Header::new_gnu();
+ header.set_entry_type(tar::EntryType::Regular);
+ header.set_path(path).expect("set path");
+ header.set_size(body.len() as u64);
+ header.set_mode(mode);
+ header.set_cksum();
+ builder.append(&header, body).expect("append tar entry");
+ }
+
+ async fn latest_tag() -> &'static str {
+ TAG
+ }
+
+ async fn download(
+ AxumPath((tag, name)): AxumPath<(String, String)>,
+ State(fx): State<ReleaseFixture>,
+ ) -> Response {
+ if tag != TAG {
+ return (StatusCode::NOT_FOUND, "unknown tag").into_response();
+ }
+ let checksum_name = format!("{}.sha256", fx.asset);
+ if name == checksum_name {
+ return (
+ StatusCode::OK,
+ [(header::CONTENT_TYPE, "text/plain")],
+ fx.checksum_body.clone(),
+ )
+ .into_response();
+ }
+ if name == fx.asset {
+ return Response::builder()
+ .status(StatusCode::OK)
+ .header(header::CONTENT_TYPE, "application/gzip")
+ .body(Body::from(fx.archive.as_ref().clone()))
+ .expect("build archive response");
+ }
+ (StatusCode::NOT_FOUND, "unknown asset").into_response()
+ }
+
+ fn install_writable_prefix(prefix: &Path) -> PathBuf {
+ let bin_dir = prefix.join("bin");
+ fs::create_dir_all(&bin_dir).expect("bin dir");
+ let exe = bin_dir.join("ai-memory");
+ fs::copy(BIN, &exe).expect("copy built binary into writable prefix");
+ fs::set_permissions(&exe, fs::Permissions::from_mode(0o755)).expect("chmod +x");
+
+ // Sibling hooks tree: upgrade refreshes it when the archive ships hooks/.
+ let hooks = bin_dir.join("hooks/claude-code");
+ fs::create_dir_all(&hooks).expect("sibling hooks");
+ fs::write(hooks.join("old.sh"), OLD_HOOK).expect("plant old hook");
+ exe
+ }
+
+ #[tokio::test]
+ async fn native_upgrade_replaces_binary_and_sibling_hooks_from_fixture() {
+ let asset = host_asset_name().to_string();
+ let archive = Arc::new(build_release_tarball());
+ let hash = format!("{:x}", Sha256::digest(archive.as_ref()));
+ let checksum_body = format!("{hash} {asset}\n");
+ let fixture = ReleaseFixture {
+ asset: asset.clone(),
+ archive: Arc::clone(&archive),
+ checksum_body,
+ };
+
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
+ .await
+ .expect("bind release fixture");
+ let addr = listener.local_addr().expect("fixture addr");
+ let app = Router::new()
+ .route("/releases/latest/tag", get(latest_tag))
+ .route("/releases/download/{tag}/{name}", get(download))
+ .with_state(fixture);
+ let server = tokio::spawn(async move {
+ axum::serve(listener, app).await.expect("fixture serve");
+ });
+
+ let prefix = tempfile::tempdir().expect("install prefix");
+ let data_dir = tempfile::tempdir().expect("data dir");
+ let home = tempfile::tempdir().expect("home");
+ let exe = install_writable_prefix(prefix.path());
+ let before = fs::read(&exe).expect("read pre-upgrade binary");
+ assert_ne!(
+ before, NEW_BINARY,
+ "pre-upgrade binary must differ from fixture payload"
+ );
+
+ let base = format!("http://{addr}/releases");
+ // Async child I/O — a blocking `.output()` would starve the in-process
+ // fixture on the current-thread tokio test runtime.
+ let mut cmd = hermetic(exe.to_str().expect("utf-8 exe path"));
+ cmd.args(["upgrade", "--version", TAG, "--force"])
+ .env("AI_MEMORY_RELEASE_BASE_URL", &base)
+ .env("AI_MEMORY_DATA_DIR", data_dir.path())
+ .env("AI_MEMORY_HOME", home.path());
+ let output = tokio::process::Command::from(cmd)
+ .output()
+ .await
+ .expect("spawn upgrade");
+
+ assert!(
+ output.status.success(),
+ "upgrade failed: stdout={}\nstderr={}",
+ String::from_utf8_lossy(&output.stdout),
+ String::from_utf8_lossy(&output.stderr)
+ );
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ assert!(
+ stdout.contains(&format!("upgraded to {TAG}")),
+ "missing success line: {stdout}"
+ );
+
+ let after = fs::read(&exe).expect("read post-upgrade binary");
+ assert_eq!(after, NEW_BINARY, "on-disk binary was not replaced");
+
+ let hooks_root = prefix.path().join("bin/hooks");
+ assert_eq!(
+ fs::read(hooks_root.join("claude-code/new.sh")).expect("new hook"),
+ NEW_HOOK
+ );
+ assert!(
+ !hooks_root.join("claude-code/old.sh").exists(),
+ "sibling hooks tree should be fully replaced"
+ );
+
+ server.abort();
+ }
+}