use std::collections::BTreeSet;
use std::io::IsTerminal;
use std::process::Command;
use std::time::Duration;
use anyhow::{bail, Context, Result};
use chrono::{DateTime, Utc};
use inquire::Select;
use serde::Deserialize;
pub const SKILLS_SOURCE: &str = "coralogix/cx-cli/skills";
const INSTALLED_SOURCE: &str = "coralogix/cx-cli";
pub const SKILLS_PACKAGE: &str = "skills@1.8.1";
const CX_VERSION: &str = env!("CARGO_PKG_VERSION");
fn pinned_source(version: &str) -> String {
if version.contains('-') {
SKILLS_SOURCE.to_string()
} else {
format!("{SKILLS_SOURCE}#v{version}")
}
}
fn manual_install_cmd() -> String {
format!("npx {SKILLS_PACKAGE} add {}", pinned_source(CX_VERSION))
}
const SKILLS_REVIEW_URL: &str = "https://skills.sh/coralogix/cx-cli";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SkillsScope {
Global,
Local,
}
pub struct InstallOptions {
pub scope: Option<SkillsScope>,
pub agents: Vec<String>,
pub skip_if_installed: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InstallOutcome {
Installed,
AlreadyInstalled,
}
#[derive(Debug, PartialEq, Eq)]
enum Preflight {
Fail(String),
Proceed {
scope: Option<SkillsScope>,
},
}
fn preflight(
npx_available: bool,
scope: Option<SkillsScope>,
stdin_is_terminal: bool,
) -> Preflight {
if !npx_available {
return Preflight::Fail(
"Node.js/npx is required to install the cx agent skills.\n\
Install Node.js (https://nodejs.org) and rerun, or skip the skills install."
.to_string(),
);
}
if scope.is_none() && !stdin_is_terminal {
return Preflight::Fail(
"no skills install scope - pass --global or --local \
(--global-skills/--local-skills on `cx init`)"
.into(),
);
}
Preflight::Proceed { scope }
}
pub fn run_install(opts: InstallOptions) -> Result<InstallOutcome> {
let scope = match preflight(npx_available(), opts.scope, std::io::stdin().is_terminal()) {
Preflight::Fail(message) => bail!(message),
Preflight::Proceed { scope } => match scope {
Some(scope) => scope,
None => ask_scope()?,
},
};
let installed = installed_cx_skills(scope);
if !installed.is_empty() {
if opts.skip_if_installed {
return Ok(InstallOutcome::AlreadyInstalled);
}
println!(
"cx agent skills already installed ({}) - reinstalling to update.",
installed_summary(&installed)
);
}
println!("Installing cx agent skills…");
let output = run_npx_install(scope, &opts.agents, Run::Foreground)?;
if !output.status.success() {
for captured in [&output.stdout, &output.stderr] {
let text = String::from_utf8_lossy(captured);
let trimmed = text.trim();
if !trimmed.is_empty() {
eprintln!("{trimmed}");
}
}
bail!(
"skills installer exited with {}.\n\
You can retry manually with: {}",
output.status,
manual_install_cmd()
);
}
print_install_summary(scope);
if scope == SkillsScope::Global {
crate::version_cache::record_global_skills_install(&opts.agents);
}
Ok(InstallOutcome::Installed)
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Run {
Foreground,
Background,
}
const BACKGROUND_TIMEOUT: Duration = Duration::from_secs(5 * 60);
fn run_npx_install(
scope: SkillsScope,
agents: &[String],
run: Run,
) -> Result<std::process::Output> {
let install = |source: &str| {
let mut cmd = Command::new(npx_program());
cmd.args(build_install_args(source, scope, agents));
match run {
Run::Foreground => cmd.output().context("failed to run npx"),
Run::Background => output_in_background(cmd, BACKGROUND_TIMEOUT),
}
};
let pinned = pinned_source(CX_VERSION);
let output = install(&pinned)?;
if output.status.success() || pinned == SKILLS_SOURCE || !is_missing_ref(&output) {
return Ok(output);
}
install(SKILLS_SOURCE)
}
fn output_in_background(mut cmd: Command, timeout: Duration) -> Result<std::process::Output> {
use std::io::Read;
use std::process::Stdio;
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
cmd.process_group(0);
}
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
cmd.creation_flags(CREATE_NO_WINDOW);
}
let mut child = cmd
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.context("failed to run npx")?;
let drain = |pipe: Option<Box<dyn Read + Send>>| {
std::thread::spawn(move || {
let mut buf = Vec::new();
if let Some(mut pipe) = pipe {
let _ = pipe.read_to_end(&mut buf);
}
buf
})
};
let stdout = drain(child.stdout.take().map(|p| Box::new(p) as _));
let stderr = drain(child.stderr.take().map(|p| Box::new(p) as _));
let deadline = std::time::Instant::now() + timeout;
let status = loop {
if let Some(status) = child.try_wait()? {
break status;
}
if std::time::Instant::now() >= deadline {
stop_process_tree(&mut child);
bail!(
"skills installer did not finish within {}s",
timeout.as_secs()
);
}
std::thread::sleep(Duration::from_millis(200));
};
Ok(std::process::Output {
status,
stdout: stdout.join().unwrap_or_default(),
stderr: stderr.join().unwrap_or_default(),
})
}
fn stop_process_tree(child: &mut std::process::Child) {
let pid = child.id().to_string();
#[cfg(unix)]
let _ = Command::new("kill")
.args(["-KILL", "--", &format!("-{pid}")])
.status();
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
let _ = Command::new("taskkill")
.args(["/T", "/F", "/PID", &pid])
.creation_flags(CREATE_NO_WINDOW)
.status();
}
let _ = child.kill();
let _ = child.wait();
}
fn is_missing_ref(output: &std::process::Output) -> bool {
[&output.stdout, &output.stderr].iter().any(|captured| {
let text = String::from_utf8_lossy(captured).to_lowercase();
(text.contains("remote branch") && text.contains("not found in upstream origin"))
|| text.contains("couldn't find remote ref")
})
}
pub fn run_auto_refresh() -> Result<()> {
let Some(_lock) = RefreshLock::acquire() else {
return Ok(());
};
let agents = crate::version_cache::VersionCheckCache::load().skills_agents;
let output = run_npx_install(SkillsScope::Global, &agents, Run::Background)?;
if output.status.success() {
crate::version_cache::record_global_skills_install(&agents);
}
Ok(())
}
struct RefreshLock(std::path::PathBuf);
impl RefreshLock {
const STALE_AFTER: std::time::Duration = std::time::Duration::from_secs(15 * 60);
fn acquire() -> Option<Self> {
let dir = crate::config::config_dir().ok()?;
std::fs::create_dir_all(&dir).ok()?;
let path = dir.join("skills-refresh.lock");
let stale = std::fs::metadata(&path)
.and_then(|m| m.modified())
.ok()
.and_then(|modified| modified.elapsed().ok())
.is_some_and(|age| age > Self::STALE_AFTER);
if stale {
let _ = std::fs::remove_file(&path);
}
std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&path)
.ok()?;
Some(Self(path))
}
}
impl Drop for RefreshLock {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.0);
}
}
pub fn npx_on_path() -> bool {
std::env::var_os("PATH").is_some_and(|path| npx_in(&path))
}
fn npx_in(path: &std::ffi::OsStr) -> bool {
std::env::split_paths(path).any(|dir| dir.join(npx_program()).is_file())
}
fn print_install_summary(scope: SkillsScope) {
let scope_label = match scope {
SkillsScope::Global => "global",
SkillsScope::Local => "local",
};
let count = installed_cx_skills(scope).len();
if count > 0 {
println!(
"✓ Installed {count} cx agent skills ({scope_label}). \
Browse them at: {SKILLS_REVIEW_URL}"
);
} else {
println!(
"✓ cx agent skills installed ({scope_label}). \
Browse them at: {SKILLS_REVIEW_URL}"
);
}
}
pub fn run_advanced_install() -> Result<()> {
let source = pinned_source(CX_VERSION);
println!("Running the skills installer interactively: npx {SKILLS_PACKAGE} add {source}");
let status = Command::new(npx_program())
.args([SKILLS_PACKAGE, "add", &source])
.status()
.context(
"failed to run npx - Node.js is required for the skills install \
(https://nodejs.org)",
)?;
if !status.success() {
bail!("skills installer exited with {status}");
}
Ok(())
}
fn npx_program() -> &'static str {
if cfg!(windows) {
"npx.cmd"
} else {
"npx"
}
}
fn npx_available() -> bool {
Command::new(npx_program())
.arg("--version")
.output()
.map(|out| out.status.success())
.unwrap_or(false)
}
#[derive(Deserialize)]
struct LsSkill {
name: String,
#[serde(default)]
source: Option<String>,
}
fn installed_cx_skills(scope: SkillsScope) -> Vec<String> {
let mut cmd = Command::new(npx_program());
cmd.args(["-y", SKILLS_PACKAGE, "ls"]);
if scope == SkillsScope::Global {
cmd.arg("-g");
}
cmd.arg("--json");
let Ok(output) = cmd.output() else {
return Vec::new();
};
if !output.status.success() {
return Vec::new();
}
let Ok(skills) = serde_json::from_slice::<Vec<LsSkill>>(&output.stdout) else {
return Vec::new();
};
skills
.into_iter()
.filter(|skill| skill.source.as_deref() == Some(INSTALLED_SOURCE))
.map(|skill| skill.name)
.collect::<BTreeSet<_>>()
.into_iter()
.collect()
}
#[derive(Debug, PartialEq, Eq)]
pub struct LockedCxSkills {
pub skills: Vec<LockedCxSkill>,
}
#[derive(Debug, PartialEq, Eq)]
pub struct LockedCxSkill {
pub git_ref: Option<String>,
pub updated: Option<DateTime<Utc>>,
}
pub fn global_lock_cx_skills() -> Option<LockedCxSkills> {
let xdg_state_home = std::env::var_os("XDG_STATE_HOME").map(std::path::PathBuf::from);
let path = global_lock_path(xdg_state_home, dirs::home_dir())?;
let contents = std::fs::read_to_string(path).ok()?;
lock_cx_skills(&contents)
}
fn global_lock_path(
xdg_state_home: Option<std::path::PathBuf>,
home: Option<std::path::PathBuf>,
) -> Option<std::path::PathBuf> {
match xdg_state_home {
Some(xdg) if !xdg.as_os_str().is_empty() => {
Some(xdg.join("skills").join(".skill-lock.json"))
}
_ => home.map(|h| h.join(".agents").join(".skill-lock.json")),
}
}
fn lock_cx_skills(contents: &str) -> Option<LockedCxSkills> {
#[derive(Deserialize)]
struct Lock {
#[serde(default)]
skills: std::collections::BTreeMap<String, LockEntry>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct LockEntry {
source: Option<String>,
#[serde(rename = "ref")]
git_ref: Option<String>,
installed_at: Option<String>,
updated_at: Option<String>,
}
let lock = serde_json::from_str::<Lock>(contents).ok()?;
let skills: Vec<LockedCxSkill> = lock
.skills
.into_values()
.filter(|entry| entry.source.as_deref() == Some(INSTALLED_SOURCE))
.map(|entry| LockedCxSkill {
updated: entry
.updated_at
.as_deref()
.or(entry.installed_at.as_deref())
.and_then(|t| DateTime::parse_from_rfc3339(t).ok())
.map(|t| t.with_timezone(&Utc)),
git_ref: entry.git_ref,
})
.collect();
(!skills.is_empty()).then_some(LockedCxSkills { skills })
}
fn build_install_args(source: &str, scope: SkillsScope, agents: &[String]) -> Vec<String> {
let mut args: Vec<String> = vec![
"-y".into(),
SKILLS_PACKAGE.into(),
"add".into(),
source.into(),
"--skill".into(),
"*".into(),
"-y".into(),
];
if scope == SkillsScope::Global {
args.push("-g".into());
}
if !agents.is_empty() {
args.push("-a".into());
args.extend(agents.iter().cloned());
}
args
}
fn installed_summary(installed: &[String]) -> String {
const SHOWN: usize = 3;
let names = installed
.iter()
.take(SHOWN)
.map(String::as_str)
.collect::<Vec<_>>()
.join(", ");
let suffix = if installed.len() > SHOWN { ", ..." } else { "" };
format!("{} skills: {names}{suffix}", installed.len())
}
fn ask_scope() -> Result<SkillsScope> {
const GLOBAL: &str = "Global (~/) - available in every project";
const LOCAL: &str = "Local (./) - this project only";
let chosen = Select::new(
"Where should the agent skills be installed?",
vec![GLOBAL, LOCAL],
)
.with_help_message(
"Skills teach coding agents (Claude Code, Cursor, Codex, ...) how to use cx.",
)
.prompt()?;
Ok(if chosen == GLOBAL {
SkillsScope::Global
} else {
SkillsScope::Local
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn install_args_local_scope() {
let args = build_install_args(SKILLS_SOURCE, SkillsScope::Local, &[]);
assert_eq!(
args,
[
"-y",
SKILLS_PACKAGE,
"add",
SKILLS_SOURCE,
"--skill",
"*",
"-y"
]
);
}
#[test]
fn install_args_global_scope() {
let args = build_install_args(SKILLS_SOURCE, SkillsScope::Global, &[]);
assert_eq!(
args,
[
"-y",
SKILLS_PACKAGE,
"add",
SKILLS_SOURCE,
"--skill",
"*",
"-y",
"-g"
]
);
}
#[test]
fn install_args_pass_agents_through() {
let args = build_install_args(
SKILLS_SOURCE,
SkillsScope::Global,
&["claude-code".to_string(), "cursor".to_string()],
);
assert_eq!(
args,
[
"-y",
SKILLS_PACKAGE,
"add",
SKILLS_SOURCE,
"--skill",
"*",
"-y",
"-g",
"-a",
"claude-code",
"cursor"
]
);
}
#[test]
fn global_lock_path_prefers_xdg_state_home() {
use std::path::PathBuf;
let home = Some(PathBuf::from("/home/u"));
assert_eq!(
global_lock_path(Some(PathBuf::from("/state")), home.clone()),
Some(PathBuf::from("/state/skills/.skill-lock.json"))
);
assert_eq!(
global_lock_path(Some(PathBuf::new()), home.clone()),
Some(PathBuf::from("/home/u/.agents/.skill-lock.json"))
);
assert_eq!(
global_lock_path(None, home),
Some(PathBuf::from("/home/u/.agents/.skill-lock.json"))
);
}
#[cfg(unix)]
#[test]
fn background_run_stops_a_hung_installer_and_its_children() {
let pid_file = std::env::temp_dir().join(format!("cx-hung-{}", std::process::id()));
let mut cmd = Command::new("sh");
cmd.args([
"-c",
&format!("sleep 30 & echo $! > {}; wait", pid_file.display()),
]);
let started = std::time::Instant::now();
let err = output_in_background(cmd, Duration::from_millis(500)).unwrap_err();
assert!(err.to_string().contains("did not finish"), "{err}");
assert!(started.elapsed() < Duration::from_secs(10));
let grandchild = std::fs::read_to_string(&pid_file).unwrap();
let _ = std::fs::remove_file(&pid_file);
let alive = Command::new("kill")
.args(["-0", grandchild.trim()])
.stderr(std::process::Stdio::null())
.status()
.unwrap()
.success();
assert!(!alive, "the installer's child must be stopped too");
}
#[cfg(unix)]
#[test]
fn background_run_returns_the_installer_output() {
let mut cmd = Command::new("sh");
cmd.args(["-c", "echo out; echo err >&2; exit 3"]);
let output = output_in_background(cmd, Duration::from_secs(10)).unwrap();
assert_eq!(output.status.code(), Some(3));
assert_eq!(output.stdout, b"out\n");
assert_eq!(output.stderr, b"err\n");
}
#[cfg(unix)]
fn output_with(code: i32, stdout: &str, stderr: &str) -> std::process::Output {
use std::os::unix::process::ExitStatusExt;
std::process::Output {
status: std::process::ExitStatus::from_raw(code << 8),
stdout: stdout.as_bytes().to_vec(),
stderr: stderr.as_bytes().to_vec(),
}
}
#[cfg(unix)]
#[test]
fn missing_release_tag_is_detected() {
let stdout = "■ Failed to clone repository\n\
│ [SKILLS_GIT_CLONE_FAILED] Failed to clone https://github.com/coralogix/cx-cli.git: \
Cloning into '/tmp/skills-x'...\n\
│ fatal: Remote branch v0.0.999 not found in upstream origin\n";
assert!(is_missing_ref(&output_with(1, stdout, "")));
assert!(is_missing_ref(&output_with(
1,
"",
"fatal: couldn't find remote ref refs/tags/v0.0.999"
)));
}
#[cfg(unix)]
#[test]
fn other_install_failures_are_not_a_missing_tag() {
for text in [
"[SKILLS_GIT_CLONE_TIMEOUT] Clone timed out after 60s",
"[SKILLS_GIT_AUTH_FAILED] Authentication failed for https://github.com/coralogix/cx-cli.git",
"fatal: unable to access 'https://github.com/coralogix/cx-cli.git/': Could not resolve host: github.com",
] {
assert!(!is_missing_ref(&output_with(1, text, "")), "{text}");
assert!(!is_missing_ref(&output_with(1, "", text)), "{text}");
}
}
fn time(t: &str) -> Option<DateTime<Utc>> {
Some(t.parse().unwrap())
}
#[test]
fn lock_lists_each_cx_skill_with_ref_and_time() {
let lock = r#"{"version":3,"skills":{
"other":{"source":"someone/else","updatedAt":"2020-01-01T00:00:00.000Z"},
"cx-alerts":{"source":"coralogix/cx-cli","ref":"v0.1.31",
"installedAt":"2026-09-01T10:00:00.000Z","updatedAt":"2026-10-05T08:00:00.000Z"},
"cx-cli":{"source":"coralogix/cx-cli","installedAt":"2026-10-01T09:30:00.000Z"},
"cx-slos":{"source":"coralogix/cx-cli","updatedAt":"not a time"}
}}"#;
assert_eq!(
lock_cx_skills(lock),
Some(LockedCxSkills {
skills: vec![
LockedCxSkill {
git_ref: Some("v0.1.31".into()),
updated: time("2026-10-05T08:00:00Z"),
},
LockedCxSkill {
git_ref: None,
updated: time("2026-10-01T09:30:00Z"),
},
LockedCxSkill {
git_ref: None,
updated: None,
},
]
})
);
}
#[test]
fn lock_without_cx_skills_is_not_detected() {
assert_eq!(
lock_cx_skills(r#"{"version":3,"skills":{"other":{"source":"someone/else"}}}"#),
None
);
assert_eq!(lock_cx_skills(r#"{"version":3}"#), None);
assert_eq!(lock_cx_skills("not json"), None);
}
#[test]
fn release_versions_pin_to_their_tag() {
assert_eq!(pinned_source("0.1.31"), "coralogix/cx-cli/skills#v0.1.31");
assert_eq!(pinned_source("0.1.32-dev"), SKILLS_SOURCE);
assert_eq!(
build_install_args(&pinned_source("0.1.31"), SkillsScope::Global, &[])[3],
"coralogix/cx-cli/skills#v0.1.31"
);
}
#[test]
fn npx_on_path_scans_path_dirs() {
let dir = std::env::temp_dir().join(format!("cx-npx-scan-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = std::env::join_paths([&dir]).unwrap();
assert!(!npx_in(&path));
std::fs::write(dir.join(npx_program()), "").unwrap();
assert!(npx_in(&path));
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn summary_truncates_long_skill_lists() {
let skills: Vec<String> = ["cx-a", "cx-b", "cx-c", "cx-d"]
.iter()
.map(|s| s.to_string())
.collect();
assert_eq!(
installed_summary(&skills),
"4 skills: cx-a, cx-b, cx-c, ..."
);
assert_eq!(installed_summary(&skills[..2]), "2 skills: cx-a, cx-b");
}
#[test]
fn ls_json_matches_only_our_source_ignoring_extra_fields() {
let json = r#"[
{"name": "cx-alerts", "path": "/p", "scope": "global",
"agents": ["Claude Code"], "source": "coralogix/cx-cli",
"sourceUrl": null, "sourceType": "github"},
{"name": "cx-other", "source": "someone/else"},
{"name": "cx-copied", "source": null}
]"#;
let skills: Vec<LsSkill> = serde_json::from_str(json).unwrap();
let ours: Vec<&str> = skills
.iter()
.filter(|s| s.source.as_deref() == Some(INSTALLED_SOURCE))
.map(|s| s.name.as_str())
.collect();
assert_eq!(ours, ["cx-alerts"]);
}
#[test]
fn preflight_no_npx_fails_actionably() {
let Preflight::Fail(message) = preflight(false, None, true) else {
panic!("expected Fail");
};
assert!(message.contains("Node.js"));
}
#[test]
fn preflight_no_scope_no_tty_fails_naming_the_flags() {
let Preflight::Fail(message) = preflight(true, None, false) else {
panic!("expected Fail");
};
assert!(message.contains("--global") && message.contains("--local"));
}
#[test]
fn preflight_no_scope_with_tty_asks() {
assert_eq!(
preflight(true, None, true),
Preflight::Proceed { scope: None }
);
}
#[test]
fn preflight_scope_flag_proceeds_without_asking() {
assert_eq!(
preflight(true, Some(SkillsScope::Local), false),
Preflight::Proceed {
scope: Some(SkillsScope::Local)
}
);
}
}