use std::io::IsTerminal;
use std::path::{Path, PathBuf};
use crate::core::context_package::{
LocalRegistry, addon_manifest, addon_wiring, addons, content::DocumentBlob,
};
pub(crate) fn cmd_addon(args: &[String]) {
let sub = args
.iter()
.find(|a| !a.starts_with('-') && a.as_str() != "addon" && a.as_str() != "addons")
.map(String::as_str);
match sub {
Some("list" | "ls") | None => cmd_list(),
Some("info") => cmd_info(args),
Some("add" | "install") => cmd_add(args),
Some("remove" | "rm" | "uninstall") => cmd_remove(args),
Some("release") => cmd_release(args),
Some("help" | "--help" | "-h") => print_usage(),
Some(other) => {
eprintln!("lean-ctx addon: unknown subcommand '{other}'");
print_usage();
std::process::exit(1);
}
}
}
fn print_usage() {
println!(
"lean-ctx addon — extensions: WASM modules and declared MCP servers
list Installed addons, the modules they load, what they wire
info <name> The author's manifest and module digests
add <pkg | ns/name> Verify, show, ask, then install (file or registry ref)
remove <name> Remove an addon, its modules and its gateway entry
release <dir> Build a signed .ctxpkg from a directory
An addon directory holds `lean-ctx-addon.toml` and, for a WASM addon, one or
more `.wasm` modules. `release` embeds the modules in the package and signs it,
so publishing needs no artifact host, no checksum files and no CI.
WASM modules run in a sandbox: no ambient environment, a fresh store per call,
and the host enforces the output budget after decoding. Only what a module
returns can affect lean-ctx.
A manifest may instead declare an MCP server under `[mcp]`. That server is an
ordinary process with your privileges — not sandboxed — so `add` prints the
exact command before asking, and lean-ctx never installs the binary for you.
Docs: docs/guides/addons.md · contracts: wasm-abi-v1, addon-manifest-v1"
);
}
fn store_root() -> Option<PathBuf> {
addons::store_root().ok()
}
struct Installed {
name: String,
version: String,
dir: PathBuf,
modules: Vec<PathBuf>,
wiring: Option<String>,
active: bool,
}
fn installed_addons() -> Vec<Installed> {
let Some(root) = store_root() else {
return Vec::new();
};
let addons_root = addons::addons_root(&root);
let Ok(names) = std::fs::read_dir(&addons_root) else {
return Vec::new();
};
let mut out = Vec::new();
for name_entry in names.flatten().filter(|e| e.path().is_dir()) {
let display_name = name_entry.file_name().to_string_lossy().replace("__", "/");
let Ok(versions) = std::fs::read_dir(name_entry.path()) else {
continue;
};
let active_dir = std::fs::read_dir(name_entry.path()).ok().and_then(|v| {
v.flatten()
.filter(|e| e.path().is_dir())
.max_by_key(|e| {
e.metadata()
.and_then(|m| m.modified())
.unwrap_or(std::time::UNIX_EPOCH)
})
.map(|e| e.path())
});
for version_entry in versions.flatten().filter(|e| e.path().is_dir()) {
let dir = version_entry.path();
let mut modules: Vec<PathBuf> = std::fs::read_dir(&dir)
.into_iter()
.flatten()
.flatten()
.map(|e| e.path())
.filter(|p| p.extension().and_then(|e| e.to_str()) == Some("wasm"))
.collect();
modules.sort();
let wiring = std::fs::read_to_string(dir.join("lean-ctx-addon.toml"))
.ok()
.and_then(|t| addon_manifest::parse(&t).ok())
.and_then(|m| m.mcp.map(|w| w.describe()));
let active = active_dir.as_ref().is_some_and(|a| *a == dir);
out.push(Installed {
name: display_name.clone(),
version: version_entry.file_name().to_string_lossy().into_owned(),
dir,
modules,
wiring,
active,
});
}
}
out.sort_by(|a, b| (&a.name, &a.version).cmp(&(&b.name, &b.version)));
out
}
fn cmd_list() {
let installed = installed_addons();
if installed.is_empty() {
println!("No addons installed.");
println!();
println!("Install one: lean-ctx addon add <file.ctxpkg>");
println!("Build one: lean-ctx addon release <dir>");
return;
}
println!("Installed addons ({}):", installed.len());
println!();
let superseded = installed.iter().any(|a| !a.active);
for a in &installed {
let names: Vec<String> = a
.modules
.iter()
.filter_map(|m| m.file_stem().map(|s| s.to_string_lossy().into_owned()))
.collect();
println!(
" {}@{}{}",
a.name,
a.version,
if a.active { "" } else { " (superseded)" }
);
if !names.is_empty() {
if a.active {
println!(" compressors: {}", names.join(", "));
} else {
println!(" modules on disk, not loaded: {}", names.join(", "));
}
}
if let Some(w) = a.wiring.as_deref() {
println!(" MCP server: {w}");
}
if names.is_empty() && a.wiring.is_none() {
println!(" (nothing is loaded from this addon)");
}
}
println!();
println!("Modules register as compressors and are visible in `lean-ctx doctor`.");
if superseded {
println!("Superseded versions stay on disk; `addon remove <name>` clears them all.");
}
if !addon_wiring::gateway_enabled() {
println!("The MCP gateway is OFF — declared servers are recorded but not spawned.");
}
}
fn cmd_info(args: &[String]) {
let Some(name) = positional(args, "info") else {
eprintln!("Usage: lean-ctx addon info <name>");
std::process::exit(2);
};
let matches: Vec<Installed> = installed_addons()
.into_iter()
.filter(|a| a.name == name || a.name.trim_start_matches('@') == name)
.collect();
if matches.is_empty() {
eprintln!("No installed addon named `{name}`.");
eprintln!("See what is installed with: lean-ctx addon list");
std::process::exit(1);
}
for a in matches {
println!("{}@{}", a.name, a.version);
println!(" stored at {}", a.dir.display());
for module in &a.modules {
let bytes = std::fs::read(module).unwrap_or_default();
println!(
" module {} ({} bytes, sha256 {})",
module.file_name().unwrap_or_default().to_string_lossy(),
bytes.len(),
&sha256_hex(&bytes)[..16],
);
}
let manifest_path = a.dir.join("lean-ctx-addon.toml");
match std::fs::read_to_string(&manifest_path) {
Ok(text) => {
println!();
println!(" --- lean-ctx-addon.toml (as published) ---");
for line in text.lines() {
println!(" {line}");
}
}
Err(_) => println!(" (no authoring manifest stored)"),
}
}
}
struct Staged(std::path::PathBuf);
impl Drop for Staged {
fn drop(&mut self) {
std::fs::remove_file(&self.0).ok();
}
}
fn stage_remote(reference: &str, registry_flag: Option<&str>) -> Result<Staged, String> {
use crate::core::context_package::remote;
let parsed = remote::parse_remote_ref(reference)
.ok_or_else(|| format!("'{reference}' is not a file, nor a valid ns/name[@version]"))?;
let base = remote::registry_base(registry_flag);
let token = remote::publish_token(None);
let (ns, name) = (&parsed.namespace, &parsed.name);
println!("Resolving @{ns}/{name} via {base} …");
let versions = remote::fetch_versions(&base, ns, name, token.as_deref())?;
let info = remote::select_version(&versions, parsed.version.as_deref())?;
if info.yanked {
eprintln!(
"WARNING: @{ns}/{name}@{} is YANKED — continuing only because the version \
was pinned explicitly",
info.version
);
}
let bytes = remote::download_verified(&base, ns, name, info, token.as_deref())?;
println!(
"Downloaded @{ns}/{name}@{} ({} bytes, sha256 verified against the index)",
info.version,
bytes.len()
);
let tmp = std::env::temp_dir().join(format!("ctxpkg-addon-{}.ctxpkg", std::process::id()));
std::fs::write(&tmp, &bytes).map_err(|e| format!("stage artifact: {e}"))?;
Ok(Staged(tmp))
}
fn looks_like_a_path(arg: &str) -> bool {
arg.ends_with(".ctxpkg")
|| arg.starts_with('.')
|| arg.starts_with('/')
|| arg.starts_with('~')
|| arg.starts_with("file:")
}
fn cmd_add(args: &[String]) {
let Some(file) = positional(args, "add").or_else(|| positional(args, "install")) else {
eprintln!("Usage: lean-ctx addon add <file.ctxpkg | ns/name[@version]>");
std::process::exit(2);
};
let assume_yes = args.iter().any(|a| a == "--yes" || a == "-y");
let registry_flag = flag_value(args, "--registry");
let local = Path::new(&file);
let _staged;
let path: &Path = if local.is_file() {
local
} else if looks_like_a_path(&file) {
eprintln!("ERROR: no such file: {file}");
eprintln!(" To install from a registry instead, drop the path and pass ns/name.");
std::process::exit(1);
} else {
match stage_remote(&file, registry_flag.as_deref()) {
Ok(s) => {
_staged = s;
&_staged.0
}
Err(e) => {
eprintln!("ERROR: {e}");
std::process::exit(1);
}
}
};
let registry = match LocalRegistry::open() {
Ok(r) => r,
Err(e) => {
eprintln!("ERROR: {e}");
std::process::exit(1);
}
};
match preview(path) {
Ok(p) => {
println!("{}@{}", p.name, p.version);
println!(" {}", p.description);
println!(
" signature {}",
p.signed
.as_deref()
.map_or("UNSIGNED — anyone could have produced this file", |k| k)
);
for (module, digest, bytes) in &p.modules {
println!(
" module {module} ({}, sha256 {})",
bytes.map_or_else(
|| "size unavailable — payload does not decode".to_string(),
|n| format!("{n} bytes")
),
&digest[..16]
);
}
if let Some(w) = &p.wiring {
println!(" MCP server {w}");
if let Some(pinned) = p.pinned {
println!(
" pin {}",
if pinned {
"sha256 pinned — the gateway refuses to spawn a changed binary"
} else {
"none — whatever `command` resolves to at spawn time"
}
);
}
}
println!();
if !p.modules.is_empty() {
println!("Modules run inside lean-ctx as WASM: sandboxed, no ambient");
println!("environment, output budget enforced by the host.");
}
if p.wiring.is_some() {
if p.spawns_locally {
println!(
"The MCP server above runs as a NORMAL PROCESS with your privileges — it is"
);
println!(
"not sandboxed. lean-ctx will not install it: it records how to run it, and"
);
println!("only spawns it while `gateway.enabled = true`.");
} else {
println!(
"The endpoint above is REMOTE. Nothing runs on your machine, but lean-ctx"
);
println!(
"will send it requests — including file contents your agent asks about —"
);
println!(
"and treat its replies as untrusted input. Only while `gateway.enabled = true`."
);
}
}
println!();
}
Err(e) => {
eprintln!("ERROR: {e}");
std::process::exit(1);
}
}
if !crate::cli::prompt::confirm("Install this addon?", assume_yes) {
println!("Aborted. Nothing was installed.");
if !std::io::stdin().is_terminal() {
std::process::exit(1);
}
return;
}
match registry.install_addon_from_file(path) {
Ok(manifest) => {
println!("Installed {}@{}", manifest.name, manifest.version);
match wire_after_install(path) {
Ok(addon_wiring::Wired::NothingToWire) => {}
Ok(addon_wiring::Wired::Replaced(name)) => {
println!("Updated the gateway entry for `{name}`.");
println!("Your credentials and per-server on/off setting were kept.");
report_gateway_state();
}
Ok(addon_wiring::Wired::Added(name)) => {
println!("Wired `{name}` into the MCP gateway.");
report_gateway_state();
}
Err(e) => {
eprintln!("WARNING: installed, but could not wire the MCP server: {e}");
eprintln!(" Re-run `lean-ctx addon add` after fixing it.");
}
}
println!("Modules load on the next lean-ctx start.");
}
Err(e) => {
eprintln!("ERROR: install failed: {e}");
std::process::exit(1);
}
}
}
fn report_gateway_state() {
if !addon_wiring::gateway_enabled() {
println!();
println!("The gateway is currently OFF, so nothing spawns yet.");
println!("Turn it on with: lean-ctx config set gateway.enabled true");
}
}
fn wire_after_install(path: &Path) -> Result<addon_wiring::Wired, String> {
let json = std::fs::read_to_string(path).map_err(|e| e.to_string())?;
let value: serde_json::Value = serde_json::from_str(&json).map_err(|e| e.to_string())?;
let toml_text = value
.get("content")
.and_then(|c| c.get("addon"))
.and_then(|a| a.get("manifest_toml"))
.and_then(|v| v.as_str())
.ok_or("package has no addon manifest")?;
let manifest = addon_manifest::parse(toml_text)?;
addon_wiring::register(&manifest)
}
struct Preview {
name: String,
version: String,
description: String,
signed: Option<String>,
modules: Vec<(String, String, Option<usize>)>,
wiring: Option<String>,
pinned: Option<bool>,
spawns_locally: bool,
}
fn preview(path: &Path) -> Result<Preview, String> {
let json =
std::fs::read_to_string(path).map_err(|e| format!("read {}: {e}", path.display()))?;
let value: serde_json::Value =
serde_json::from_str(&json).map_err(|e| format!("parse package: {e}"))?;
let manifest = value.get("manifest").ok_or("package has no manifest")?;
let str_at = |k: &str| -> String {
manifest
.get(k)
.and_then(|v| v.as_str())
.unwrap_or_default()
.to_string()
};
let modules = value
.get("content")
.and_then(|c| c.get("addon"))
.and_then(|a| a.get("modules"))
.and_then(|m| m.as_array())
.map(|arr| {
arr.iter()
.map(|m| {
let decoded = serde_json::from_value::<DocumentBlob>(m.clone())
.ok()
.and_then(|b| b.decode_verified().ok())
.map(|plain| plain.len());
(
m.get("path")
.and_then(|v| v.as_str())
.unwrap_or("?")
.to_string(),
m.get("sha256")
.and_then(|v| v.as_str())
.unwrap_or(&"?".repeat(64))
.to_string(),
decoded,
)
})
.collect()
})
.unwrap_or_default();
let declared = value
.get("content")
.and_then(|c| c.get("addon"))
.and_then(|a| a.get("manifest_toml"))
.and_then(|v| v.as_str())
.and_then(|toml| addon_manifest::parse(toml).ok())
.and_then(|m| m.mcp);
let pinned = declared
.as_ref()
.filter(|w| w.transport == crate::core::mcp_catalog::config::TransportKind::Stdio)
.map(|w| !w.sha256.is_empty());
let spawns_locally = declared
.as_ref()
.is_some_and(|w| w.transport == crate::core::mcp_catalog::config::TransportKind::Stdio);
let wiring = declared.map(|w| w.describe());
Ok(Preview {
name: str_at("name"),
version: str_at("version"),
description: str_at("description"),
signed: manifest
.get("signature")
.and_then(|s| s.get("public_key"))
.and_then(|v| v.as_str())
.map(|k| format!("signed by {}…", &k[..k.len().min(16)])),
modules,
wiring,
pinned,
spawns_locally,
})
}
fn cmd_remove(args: &[String]) {
let Some(name) = positional(args, "remove")
.or_else(|| positional(args, "rm"))
.or_else(|| positional(args, "uninstall"))
else {
eprintln!("Usage: lean-ctx addon remove <name>");
std::process::exit(2);
};
let matches: Vec<Installed> = installed_addons()
.into_iter()
.filter(|a| a.name == name || a.name.trim_start_matches('@') == name)
.collect();
if matches.is_empty() {
eprintln!("No installed addon named `{name}`.");
std::process::exit(1);
}
let wired_names: Vec<String> = matches
.iter()
.map(|a| {
std::fs::read_to_string(a.dir.join("lean-ctx-addon.toml"))
.ok()
.and_then(|t| addon_manifest::parse(&t).ok())
.map_or_else(|| a.name.clone(), |m| m.addon.name)
})
.collect();
let mut removed = 0;
for a in &matches {
match std::fs::remove_dir_all(&a.dir) {
Ok(()) => {
println!("Removed {}@{}", a.name, a.version);
removed += 1;
}
Err(e) => eprintln!("ERROR: remove {}: {e}", a.dir.display()),
}
}
if let Ok(registry) = LocalRegistry::open() {
let _ = registry.remove(&matches[0].name, None);
}
for wired_name in &wired_names {
match addon_wiring::unregister(wired_name) {
Ok(true) => println!("Unwired `{wired_name}` from the MCP gateway."),
Ok(false) => {}
Err(e) => eprintln!("WARNING: could not update the gateway config: {e}"),
}
}
if removed > 0 {
println!("Its modules stop loading on the next lean-ctx start.");
}
}
fn cmd_release(args: &[String]) {
let dir = positional(args, "release").unwrap_or_else(|| ".".to_string());
let dir = Path::new(&dir);
let output = flag_value(args, "--output").or_else(|| flag_value(args, "-o"));
match build_release(dir, output.as_deref()) {
Ok(out) => {
println!("Wrote {}", out.display());
println!();
println!("Install locally: lean-ctx addon add {}", out.display());
println!("Publish: lean-ctx pack publish {}", out.display());
}
Err(e) => {
eprintln!("ERROR: {e}");
std::process::exit(1);
}
}
}
fn build_release(dir: &Path, output: Option<&str>) -> Result<PathBuf, String> {
let manifest_path = dir.join("lean-ctx-addon.toml");
let manifest_toml = std::fs::read_to_string(&manifest_path)
.map_err(|e| format!("read {}: {e}", manifest_path.display()))?;
let parsed: toml::Value =
toml::from_str(&manifest_toml).map_err(|e| format!("{}: {e}", manifest_path.display()))?;
let table = parsed
.get("addon")
.ok_or_else(|| format!("{}: missing [addon] table", manifest_path.display()))?;
let name = table
.get("name")
.and_then(toml::Value::as_str)
.ok_or_else(|| format!("{}: [addon] name is required", manifest_path.display()))?;
let version = table
.get("version")
.and_then(toml::Value::as_str)
.ok_or_else(|| format!("{}: [addon] version is required", manifest_path.display()))?;
let description = table
.get("description")
.and_then(toml::Value::as_str)
.unwrap_or("");
let mut wasm_files: Vec<PathBuf> = std::fs::read_dir(dir)
.map_err(|e| format!("read {}: {e}", dir.display()))?
.flatten()
.map(|e| e.path())
.filter(|p| p.extension().and_then(|e| e.to_str()) == Some("wasm"))
.collect();
wasm_files.sort();
if wasm_files.is_empty() {
let declares_server = addon_manifest::parse(&manifest_toml)
.map(|m| m.mcp.is_some())
.unwrap_or(false);
if !declares_server {
return Err(format!(
"{} declares neither a .wasm module nor an [mcp] server — \
installing this addon would have no effect",
dir.display()
));
}
}
let mut modules = Vec::new();
for path in &wasm_files {
let bytes = std::fs::read(path).map_err(|e| format!("read {}: {e}", path.display()))?;
let file_name = path
.file_name()
.and_then(|n| n.to_str())
.ok_or_else(|| format!("{}: unusable file name", path.display()))?;
if !bytes.starts_with(b"\0asm") {
return Err(format!(
"{file_name} is not a WebAssembly module (bad magic) — was it built with \
`--target wasm32-unknown-unknown`?"
));
}
modules.push(DocumentBlob::from_plaintext(file_name, &bytes)?);
}
let out_path = output.map_or_else(
|| {
let safe = name.replace('/', "__").replace('@', "");
dir.join(format!(
"{safe}-{version}.{}",
crate::core::contracts::PACKAGE_EXTENSION
))
},
PathBuf::from,
);
crate::core::context_package::addons_build::write_addon_package(
&out_path,
name,
version,
description,
&manifest_toml,
modules,
)?;
println!(
"Packed {} module(s) from {}",
wasm_files.len(),
dir.display()
);
Ok(out_path)
}
fn positional(args: &[String], after: &str) -> Option<String> {
args.iter()
.skip_while(|a| a.as_str() != after)
.skip(1)
.find(|a| !a.starts_with('-'))
.cloned()
}
fn flag_value(args: &[String], flag: &str) -> Option<String> {
if let Some(v) = args
.iter()
.find_map(|a| a.strip_prefix(&format!("{flag}=")))
{
return Some(v.to_string());
}
args.iter()
.position(|a| a == flag)
.and_then(|i| args.get(i + 1))
.cloned()
}
fn sha256_hex(data: &[u8]) -> String {
use sha2::{Digest, Sha256};
let mut h = Sha256::new();
h.update(data);
crate::core::agent_identity::hex_encode(&h.finalize())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::mcp_catalog::config::TransportKind;
#[test]
fn positional_skips_flags_and_the_verb() {
let args: Vec<String> = ["addon", "add", "--yes", "pack.ctxpkg"]
.iter()
.map(ToString::to_string)
.collect();
assert_eq!(positional(&args, "add"), Some("pack.ctxpkg".into()));
}
#[test]
fn flag_value_accepts_both_spellings() {
let split: Vec<String> = ["release", ".", "--output", "out.ctxpkg"]
.iter()
.map(ToString::to_string)
.collect();
assert_eq!(flag_value(&split, "--output"), Some("out.ctxpkg".into()));
let joined: Vec<String> = ["release", ".", "--output=out.ctxpkg"]
.iter()
.map(ToString::to_string)
.collect();
assert_eq!(flag_value(&joined, "--output"), Some("out.ctxpkg".into()));
}
#[test]
fn release_refuses_a_directory_without_a_module() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(
dir.path().join("lean-ctx-addon.toml"),
"[addon]\nname = \"@ns/demo\"\nversion = \"1.0.0\"\n",
)
.unwrap();
let err = build_release(dir.path(), None).expect_err("must refuse");
assert!(
err.contains("neither a .wasm module nor an [mcp] server"),
"{err}"
);
}
#[test]
fn release_accepts_an_mcp_only_addon() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(
dir.path().join("lean-ctx-addon.toml"),
"[addon]\nname = \"@ns/demo\"\nversion = \"1.0.0\"\n\
[mcp]\ncommand = \"demo-server\"\nargs = [\"serve\"]\n",
)
.unwrap();
let out = dir.path().join("demo.ctxpkg");
build_release(dir.path(), Some(out.to_str().unwrap())).expect("must build");
assert!(out.is_file(), "a package was written");
}
#[test]
fn release_refuses_a_file_that_is_not_webassembly() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(
dir.path().join("lean-ctx-addon.toml"),
"[addon]\nname = \"@ns/demo\"\nversion = \"1.0.0\"\n",
)
.unwrap();
std::fs::write(dir.path().join("bogus.wasm"), b"not wasm at all").unwrap();
let err = build_release(dir.path(), None).expect_err("must refuse");
assert!(err.contains("not a WebAssembly module"), "{err}");
assert!(
err.contains("wasm32-unknown-unknown"),
"must hint the cause: {err}"
);
}
#[test]
fn release_requires_name_and_version() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("lean-ctx-addon.toml"), "[addon]\n").unwrap();
let err = build_release(dir.path(), None).expect_err("must refuse");
assert!(err.contains("name is required"), "{err}");
}
#[test]
fn a_local_file_is_preferred_over_a_remote_reference() {
use crate::core::context_package::remote;
let dir = tempfile::tempdir().unwrap();
let ns_dir = dir.path().join("acme");
std::fs::create_dir(&ns_dir).unwrap();
let file = ns_dir.join("widget");
std::fs::write(&file, b"{}").unwrap();
assert!(
remote::parse_remote_ref("acme/widget").is_some(),
"precondition: this parses as a registry reference"
);
assert!(file.is_file());
}
#[test]
fn the_pin_line_applies_to_stdio_only() {
let stdio = addon_manifest::parse("[addon]\nname = \"x\"\n[mcp]\ncommand = \"c\"\n")
.unwrap()
.mcp
.unwrap();
assert_eq!(stdio.transport, TransportKind::Stdio);
let http = addon_manifest::parse(
"[addon]\nname = \"x\"\n[mcp]\ntransport = \"http\"\nurl = \"https://e.test/mcp\"\n",
)
.unwrap()
.mcp
.unwrap();
assert_eq!(http.transport, TransportKind::Http);
assert_eq!(
http.describe(),
"https://e.test/mcp",
"an http addon is described by its endpoint, not a command line"
);
}
#[test]
fn path_shaped_arguments_are_never_treated_as_registry_refs() {
for arg in [
"./missing.ctxpkg",
"../build/x.ctxpkg",
"/abs/path/x.ctxpkg",
"~/downloads/x.ctxpkg",
"demo__squeeze-1.0.0.ctxpkg",
] {
assert!(looks_like_a_path(arg), "should be a path: {arg}");
}
for arg in ["acme/widget", "acme/widget@1.2.0", "widget"] {
assert!(!looks_like_a_path(arg), "should be a registry ref: {arg}");
}
}
#[test]
fn staged_artifacts_are_deleted_on_drop() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("staged.ctxpkg");
std::fs::write(&path, b"{}").unwrap();
{
let _guard = Staged(path.clone());
assert!(path.is_file(), "available while the guard lives");
}
assert!(!path.exists(), "removed when the guard drops");
}
#[test]
fn the_wired_name_is_read_before_the_directory_is_deleted() {
let dir = tempfile::tempdir().unwrap();
let manifest = dir.path().join("lean-ctx-addon.toml");
std::fs::write(
&manifest,
"[addon]\nname = \"@ns/demo\"\n[mcp]\ncommand = \"c\"\n",
)
.unwrap();
let wired = std::fs::read_to_string(&manifest)
.ok()
.and_then(|t| addon_manifest::parse(&t).ok())
.map(|m| m.addon.name);
assert_eq!(wired.as_deref(), Some("@ns/demo"));
std::fs::remove_dir_all(dir.path()).unwrap();
assert!(std::fs::read_to_string(&manifest).is_err());
}
}