use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{Duration, Instant};
pub const MIN_CLAUDE_VERSION: (u32, u32, u32) = (2, 1, 287);
pub const MARKETPLACE: &str = "lean-ctx";
pub const PLUGIN_ID: &str = "lean-ctx@lean-ctx";
pub const REGISTER_TS: &str = include_str!("../../templates/claude_mod/register.ts");
pub const HOOKS_JSON: &str = include_str!("../../templates/claude_mod/hooks.json");
pub const COCKPIT_TSX: &str = include_str!("../../templates/claude_mod/cockpit.tsx");
pub const TYPES_DTS: &str = include_str!("../../templates/claude_mod/cockpit.d.ts");
pub const PLUGIN_JSON: &str = include_str!("../../templates/claude_mod/plugin.json");
const CLAUDE_TIMEOUT: Duration = Duration::from_mins(1);
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ModStatus {
ClaudeMissing,
ClaudeTooOld(String),
NotInstalled,
Current(String),
Stale(String),
}
impl ModStatus {
#[must_use]
pub fn describe(&self) -> String {
let want = mod_version();
match self {
Self::ClaudeMissing => "Claude Code not found on PATH".to_string(),
Self::ClaudeTooOld(v) => format!(
"Claude Code {v} predates mods (needs {}.{}.{}+)",
MIN_CLAUDE_VERSION.0, MIN_CLAUDE_VERSION.1, MIN_CLAUDE_VERSION.2
),
Self::NotInstalled => "not installed".to_string(),
Self::Current(v) => format!("installed ({v})"),
Self::Stale(v) => {
format!("installed ({v}), this engine ships {want} — refresh pending")
}
}
}
}
#[must_use]
pub fn mod_version() -> String {
let mut hasher = blake3::Hasher::new();
for part in [REGISTER_TS, COCKPIT_TSX, TYPES_DTS, HOOKS_JSON, PLUGIN_JSON] {
hasher.update(part.as_bytes());
hasher.update(&[0]);
}
let hash = hasher.finalize().to_hex();
format!("{}+{}", env!("CARGO_PKG_VERSION"), &hash[..8])
}
pub fn marketplace_dir() -> Result<PathBuf, String> {
crate::core::data_dir::lean_ctx_data_dir().map(|d| d.join("claude-mod"))
}
pub const INSTALLED_NOTICE: &str = "Installed the LeanCTX plugin for Claude Code: the cockpit (what Claude saw, \
tokens kept out, the two checks, receipts), wake on background jobs and a focused lean-ctx tool \
surface. Active in new Claude Code sessions (or /reload-plugins). Remove anytime with \
`lean-ctx claude-mod uninstall` — it then stays removed.";
fn declined_marker() -> Result<PathBuf, String> {
crate::core::data_dir::lean_ctx_data_dir().map(|d| d.join("claude-mod.declined"))
}
#[must_use]
pub fn auto_install_declined() -> bool {
declined_marker().is_ok_and(|p| p.exists())
}
pub fn plugin_manifest() -> String {
let mut manifest: serde_json::Value =
serde_json::from_str(PLUGIN_JSON).expect("templates/claude_mod/plugin.json is valid JSON");
manifest["version"] = serde_json::json!(mod_version());
serde_json::to_string_pretty(&manifest).expect("manifest serializes") + "\n"
}
fn marketplace_manifest() -> String {
let manifest = serde_json::json!({
"name": MARKETPLACE,
"description": "lean-ctx's Claude Code mod, installed by the lean-ctx binary that serves it.",
"owner": { "name": "Thinkery / Yves Gugger" },
"plugins": [{
"name": "lean-ctx",
"source": "./plugins/lean-ctx",
"description": "The LeanCTX cockpit, wake on background job completion, a focused lean-ctx tool surface, and per-session request usage.",
}]
});
serde_json::to_string_pretty(&manifest).expect("manifest serializes") + "\n"
}
pub fn materialize(root: &Path) -> Result<bool, String> {
let plugin = root.join("plugins").join("lean-ctx");
let files: [(PathBuf, String); 6] = [
(
root.join(".claude-plugin/marketplace.json"),
marketplace_manifest(),
),
(plugin.join(".claude-plugin/plugin.json"), plugin_manifest()),
(plugin.join("hooks/hooks.json"), HOOKS_JSON.to_string()),
(plugin.join("hooks/register.ts"), REGISTER_TS.to_string()),
(plugin.join("hooks/cockpit.tsx"), COCKPIT_TSX.to_string()),
(plugin.join("types/index.d.ts"), TYPES_DTS.to_string()),
];
let mut changed = false;
for (path, content) in files {
if std::fs::read_to_string(&path).is_ok_and(|on_disk| on_disk == content) {
continue;
}
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)
.map_err(|e| format!("create {}: {e}", parent.display()))?;
}
std::fs::write(&path, content).map_err(|e| format!("write {}: {e}", path.display()))?;
changed = true;
}
Ok(changed)
}
#[must_use]
pub fn parse_claude_version(output: &str) -> Option<(u32, u32, u32)> {
let token = output.split_whitespace().next()?;
let mut parts = token.split('.').map(|p| p.parse::<u32>().ok());
Some((parts.next()??, parts.next()??, parts.next()??))
}
#[must_use]
pub fn installed_version(list_json: &str) -> Option<String> {
let entries: Vec<serde_json::Value> = serde_json::from_str(list_json).ok()?;
entries
.iter()
.find(|e| e.get("id").and_then(serde_json::Value::as_str) == Some(PLUGIN_ID))
.and_then(|e| e.get("version").and_then(serde_json::Value::as_str))
.map(str::to_string)
}
pub fn status() -> ModStatus {
let Ok(version_out) = run_claude(&["--version"]) else {
return ModStatus::ClaudeMissing;
};
let version = version_out
.split_whitespace()
.next()
.unwrap_or("")
.to_string();
match parse_claude_version(&version_out) {
Some(v) if v >= MIN_CLAUDE_VERSION => {}
_ => return ModStatus::ClaudeTooOld(version),
}
match run_claude(&["plugin", "list", "--json"])
.ok()
.and_then(|out| installed_version(&out))
{
None => ModStatus::NotInstalled,
Some(v) if v == mod_version() => ModStatus::Current(v),
Some(v) => ModStatus::Stale(v),
}
}
pub fn install() -> Result<String, String> {
let before = status();
match &before {
ModStatus::ClaudeMissing | ModStatus::ClaudeTooOld(_) => {
return Err(before.describe());
}
ModStatus::Current(_) | ModStatus::NotInstalled | ModStatus::Stale(_) => {}
}
let root = marketplace_dir()?;
materialize(&root)?;
let root_str = root.to_string_lossy().to_string();
run_claude(&["plugin", "marketplace", "add", &root_str])?;
if let Ok(marker) = declined_marker() {
let _ = std::fs::remove_file(marker);
}
match before {
ModStatus::NotInstalled => {
run_claude(&["plugin", "install", PLUGIN_ID, "--scope", "user"])?;
Ok(format!(
"installed {PLUGIN_ID} {} — active in new Claude Code sessions (or /reload-plugins)",
mod_version()
))
}
ModStatus::Stale(old) => {
run_claude(&["plugin", "marketplace", "update", MARKETPLACE])?;
run_claude(&["plugin", "update", PLUGIN_ID, "--scope", "user"])?;
Ok(format!(
"updated {PLUGIN_ID} {old} → {} — applies in new sessions",
mod_version()
))
}
_ => Ok(format!("{PLUGIN_ID} is current ({})", mod_version())),
}
}
#[must_use]
pub fn cached_versions(claude_dir: &Path) -> Vec<String> {
let mut versions: Vec<String> = std::fs::read_dir(
claude_dir
.join("plugins/cache")
.join(MARKETPLACE)
.join("lean-ctx"),
)
.map(|entries| {
entries
.filter_map(Result::ok)
.filter(|e| e.path().is_dir())
.filter_map(|e| e.file_name().to_str().map(str::to_string))
.collect()
})
.unwrap_or_default();
versions.sort();
versions
}
#[must_use]
pub fn cache_dir_name(version: &str) -> String {
version.replace('+', "-")
}
pub fn uninstall() -> Result<String, String> {
if matches!(status(), ModStatus::ClaudeMissing) {
return Err(ModStatus::ClaudeMissing.describe());
}
let uninstall = run_claude(&["plugin", "uninstall", PLUGIN_ID, "--scope", "user"]);
let remove = run_claude(&["plugin", "marketplace", "remove", MARKETPLACE]);
let still_installed =
run_claude(&["plugin", "list", "--json"]).map(|out| installed_version(&out).is_some())?;
let marketplace_listed = run_claude(&["plugin", "marketplace", "list", "--json"])
.map(|out| marketplace_listed(&out))?;
if still_installed || marketplace_listed {
let cause = uninstall.err().or(remove.err()).unwrap_or_default();
return Err(format!(
"{PLUGIN_ID} is still registered with Claude Code{}{cause} — local files kept",
if cause.is_empty() { "" } else { ": " }
));
}
if let Ok(root) = marketplace_dir()
&& root.exists()
{
std::fs::remove_dir_all(&root).map_err(|e| format!("remove {}: {e}", root.display()))?;
}
if let Ok(marker) = declined_marker() {
if let Some(parent) = marker.parent() {
let _ = std::fs::create_dir_all(parent);
}
std::fs::write(&marker, "removed with `lean-ctx claude-mod uninstall`; `lean-ctx claude-mod install` lifts this\n")
.map_err(|e| format!("write {}: {e}", marker.display()))?;
}
Ok(format!(
"removed {PLUGIN_ID} — setup and update will not reinstall it (lean-ctx claude-mod install brings it back)"
))
}
#[must_use]
pub fn marketplace_listed(list_json: &str) -> bool {
serde_json::from_str::<Vec<serde_json::Value>>(list_json).is_ok_and(|entries| {
entries
.iter()
.any(|e| e.get("name").and_then(serde_json::Value::as_str) == Some(MARKETPLACE))
})
}
fn run_claude(args: &[&str]) -> Result<String, String> {
use std::io::Read as _;
let label = || format!("claude {}", args.join(" "));
let binary = crate::core::editor_registry::validate_claude_binary()?;
let mut child = Command::new(&binary)
.args(args)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.map_err(|e| format!("{}: {e}", label()))?;
let drain = |pipe: Option<Box<dyn std::io::Read + Send>>| {
std::thread::spawn(move || {
let mut buf = Vec::new();
if let Some(mut p) = pipe {
let _ = p.read_to_end(&mut buf);
}
String::from_utf8_lossy(&buf).into_owned()
})
};
let stdout = drain(child.stdout.take().map(|p| Box::new(p) as Box<_>));
let stderr = drain(child.stderr.take().map(|p| Box::new(p) as Box<_>));
let start = Instant::now();
let status = loop {
match child.try_wait() {
Ok(Some(status)) => break Ok(status),
Ok(None) if start.elapsed() > CLAUDE_TIMEOUT => {
break Err(format!("{} timed out", label()));
}
Ok(None) => std::thread::sleep(Duration::from_millis(50)),
Err(e) => break Err(format!("{}: {e}", label())),
}
};
let status = match status {
Ok(status) => status,
Err(e) => {
let _ = child.kill();
let _ = child.wait();
return Err(e);
}
};
let stdout = stdout.join().unwrap_or_default();
let stderr = stderr.join().unwrap_or_default();
if status.success() {
Ok(stdout)
} else {
let detail = stderr
.trim()
.lines()
.chain(stdout.trim().lines())
.last()
.unwrap_or("");
Err(format!("{} failed: {detail}", label()))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cache_dir_matches_the_installed_version() {
let dir = tempfile::tempdir().unwrap();
let cache = dir.path().join("plugins/cache/lean-ctx/lean-ctx");
let current = cache_dir_name(&mod_version());
for v in ["3.10.5", current.as_str()] {
std::fs::create_dir_all(cache.join(v)).unwrap();
}
assert!(cached_versions(dir.path()).contains(¤t));
assert!(!current.contains('+'), "{current}");
}
#[test]
fn version_gate_parses_claude_output() {
assert_eq!(
parse_claude_version("2.1.287 (Claude Code)"),
Some((2, 1, 287))
);
assert!(parse_claude_version("2.1.286 (Claude Code)").unwrap() < MIN_CLAUDE_VERSION);
assert!(parse_claude_version("2.2.0 (Claude Code)").unwrap() >= MIN_CLAUDE_VERSION);
assert_eq!(parse_claude_version("garbage"), None);
}
#[test]
fn installed_version_reads_only_our_plugin() {
let list = r#"[{"id":"other@x","version":"9.9.9"},
{"id":"lean-ctx@lean-ctx","version":"3.10.5","scope":"user"}]"#;
assert_eq!(installed_version(list).as_deref(), Some("3.10.5"));
assert_eq!(installed_version("[]"), None);
assert_eq!(installed_version("not json"), None);
let markets = r#"[{"name":"lean-ctx","source":"directory","path":"/d"}]"#;
assert!(marketplace_listed(markets));
assert!(!marketplace_listed(r#"[{"name":"other"}]"#));
assert!(!marketplace_listed("garbage"));
}
#[test]
fn removal_marker_lives_outside_the_marketplace_root() {
let marker = declined_marker().unwrap();
let root = marketplace_dir().unwrap();
assert!(
!marker.starts_with(&root),
"{} inside {}",
marker.display(),
root.display()
);
assert_eq!(marker.parent(), root.parent());
}
#[test]
fn materialize_stamps_engine_version_and_is_idempotent() {
let dir = tempfile::tempdir().unwrap();
assert!(materialize(dir.path()).unwrap());
let manifest: serde_json::Value = serde_json::from_str(
&std::fs::read_to_string(
dir.path()
.join("plugins/lean-ctx/.claude-plugin/plugin.json"),
)
.unwrap(),
)
.unwrap();
let version = manifest["version"].as_str().unwrap();
let (engine, hash) = version.split_once('+').expect("engine+hash version");
assert_eq!(engine, env!("CARGO_PKG_VERSION"));
assert!(
hash.len() == 8 && hash.chars().all(|c| c.is_ascii_hexdigit()),
"{version}"
);
assert_eq!(manifest["name"], "lean-ctx");
assert!(
std::fs::read_to_string(dir.path().join("plugins/lean-ctx/hooks/register.ts"))
.unwrap()
.contains("export const register")
);
let plugin = dir.path().join("plugins/lean-ctx");
assert_eq!(
std::fs::read_to_string(plugin.join("hooks/cockpit.tsx")).unwrap(),
COCKPIT_TSX
);
assert_eq!(manifest["types"], "./types/index.d.ts");
assert_eq!(
std::fs::read_to_string(plugin.join("types/index.d.ts")).unwrap(),
TYPES_DTS
);
assert!(
!materialize(dir.path()).unwrap(),
"second run must not rewrite"
);
}
}