use std::collections::HashMap;
use std::path::Path;
use serde_json::{Map, Value};
use crate::core::error::DispatchError;
use crate::server::registry::build_registry;
use crate::server::tool_trait::ToolContext;
#[derive(Debug)]
pub(crate) enum CallError {
Usage(String),
UnknownTool(String),
BadJson(String),
UnsafeRoot(String),
Dispatch(DispatchError),
}
impl std::fmt::Display for CallError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CallError::Usage(m) => write!(
f,
"error: {m}\nusage: lean-ctx call <tool> --project-root <path> --json '<json>' [--json-file <path>]"
),
CallError::UnknownTool(t) => write!(f, "error: unknown tool '{t}'"),
CallError::BadJson(m) => write!(f, "error: invalid --json: {m}"),
CallError::UnsafeRoot(p) => {
write!(f, "error: refusing broad/unsafe --project-root '{p}'")
}
CallError::Dispatch(e) => write!(f, "error: {e}"),
}
}
}
impl CallError {
#[allow(clippy::unused_self)]
pub(crate) fn exit_code(&self) -> i32 {
2
}
}
struct CallArgs {
tool: String,
project_root: String,
json: String,
}
fn parse_args(args: &[String]) -> Result<CallArgs, CallError> {
let mut tool: Option<String> = None;
let mut project_root: Option<String> = None;
let mut json: Option<String> = None;
let mut json_file: Option<String> = None;
let mut i = 0;
while i < args.len() {
match args[i].as_str() {
"--project-root" => {
i += 1;
project_root = Some(
args.get(i)
.ok_or_else(|| CallError::Usage("--project-root needs a value".into()))?
.clone(),
);
}
"--json" => {
i += 1;
json = Some(
args.get(i)
.ok_or_else(|| CallError::Usage("--json needs a value".into()))?
.clone(),
);
}
"--json-file" => {
i += 1;
json_file = Some(
args.get(i)
.ok_or_else(|| CallError::Usage("--json-file needs a value".into()))?
.clone(),
);
}
other if other.starts_with("--") => {
return Err(CallError::Usage(format!("unknown flag '{other}'")));
}
_ => {
if tool.is_none() {
tool = Some(args[i].clone());
} else {
return Err(CallError::Usage(format!(
"unexpected argument '{}'",
args[i]
)));
}
}
}
i += 1;
}
let tool = tool.ok_or_else(|| CallError::Usage("missing <tool>".into()))?;
let project_root =
project_root.ok_or_else(|| CallError::Usage("missing --project-root".into()))?;
let json = match (json, json_file) {
(Some(_), Some(_)) => {
return Err(CallError::Usage(
"use either --json or --json-file, not both".into(),
));
}
(Some(j), None) => j,
(None, Some(path)) => std::fs::read_to_string(&path)
.map_err(|e| CallError::Usage(format!("cannot read --json-file '{path}': {e}")))?,
(None, None) => "{}".to_string(),
};
Ok(CallArgs {
tool,
project_root,
json,
})
}
fn oneshot_session(project_root: &str) -> crate::core::session::SessionState {
crate::core::session::SessionState::load_recent_for_project_root(project_root, 1)
.into_iter()
.next()
.unwrap_or_default()
}
fn oneshot_ctx(project_root: String, resolved_paths: HashMap<String, String>) -> ToolContext {
let session = oneshot_session(&project_root);
ToolContext {
project_root,
resolved_paths,
cache: Some(std::sync::Arc::new(tokio::sync::RwLock::new(
crate::core::cache::SessionCache::default(),
))),
bm25_cache: Some(std::sync::Arc::new(std::sync::Mutex::new(None))),
session: Some(std::sync::Arc::new(tokio::sync::RwLock::new(session))),
tool_calls: Some(std::sync::Arc::new(tokio::sync::RwLock::new(Vec::new()))),
ledger: Some(std::sync::Arc::new(tokio::sync::RwLock::new(
crate::core::context_ledger::ContextLedger::load(),
))),
..Default::default()
}
}
pub(crate) fn run_call(args: &[String]) -> Result<String, CallError> {
let parsed = parse_args(args)?;
if crate::core::pathutil::is_broad_or_unsafe_root(Path::new(&parsed.project_root)) {
return Err(CallError::UnsafeRoot(parsed.project_root));
}
let value: Value =
serde_json::from_str(&parsed.json).map_err(|e| CallError::BadJson(e.to_string()))?;
let args_map: Map<String, Value> = match value {
Value::Object(m) => m,
_ => return Err(CallError::BadJson("expected a JSON object".into())),
};
let mut resolved_paths = HashMap::new();
if let Some(p) = args_map.get("path").and_then(Value::as_str) {
match crate::core::path_resolve::resolve_tool_path(Some(&parsed.project_root), None, p) {
Ok(abs) => {
let resolved = if abs.is_empty() || abs == "." {
parsed.project_root.clone()
} else {
abs
};
resolved_paths.insert("path".to_string(), resolved);
}
Err(e) => {
return Err(CallError::Dispatch(DispatchError::PathResolution {
message: e,
}));
}
}
}
let ctx = oneshot_ctx(parsed.project_root.clone(), resolved_paths);
let registry = build_registry();
let tool = registry
.get(&parsed.tool)
.ok_or_else(|| CallError::UnknownTool(parsed.tool.clone()))?;
let output = tool.handle(&args_map, &ctx).map_err(|e| {
CallError::Dispatch(DispatchError::Tool {
message: e.to_string(),
})
})?;
Ok(output.text)
}
pub(crate) fn cmd_call(args: &[String]) {
match run_call(args) {
Ok(text) => println!("{text}"),
Err(e) => {
eprintln!("{e}");
std::process::exit(e.exit_code());
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unknown_tool_is_cli_error() {
let args = vec![
"definitely_not_a_tool".to_string(),
"--project-root".to_string(),
std::env::temp_dir().to_string_lossy().to_string(),
"--json".to_string(),
"{}".to_string(),
];
let err = run_call(&args).expect_err("expected unknown-tool error");
assert!(matches!(err, CallError::UnknownTool(_)), "got {err:?}");
}
#[test]
fn invalid_json_is_cli_error() {
let args = vec![
"ctx_tree".to_string(),
"--project-root".to_string(),
std::env::temp_dir().to_string_lossy().to_string(),
"--json".to_string(),
"{not json".to_string(),
];
let err = run_call(&args).expect_err("expected bad-json error");
assert!(matches!(err, CallError::BadJson(_)), "got {err:?}");
}
#[test]
fn unsafe_root_is_rejected() {
let args = vec![
"ctx_tree".to_string(),
"--project-root".to_string(),
"/".to_string(),
"--json".to_string(),
"{}".to_string(),
];
let err = run_call(&args).expect_err("expected unsafe-root error");
assert!(matches!(err, CallError::UnsafeRoot(_)), "got {err:?}");
}
#[test]
fn missing_project_root_is_usage_error() {
let args = vec![
"ctx_tree".to_string(),
"--json".to_string(),
"{}".to_string(),
];
let err = run_call(&args).expect_err("expected usage error");
assert!(matches!(err, CallError::Usage(_)), "got {err:?}");
}
#[test]
fn happy_path_dispatches_to_real_tool() {
let dir = std::env::temp_dir().join(format!("leanctx-call-test-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let marker = dir.join("MARKER_FILE.txt");
std::fs::write(&marker, b"x").unwrap();
let args = vec![
"ctx_tree".to_string(),
"--project-root".to_string(),
dir.to_string_lossy().to_string(),
"--json".to_string(),
r#"{"path": "."}"#.to_string(),
];
let out = run_call(&args).expect("dispatch should succeed");
assert!(
out.contains("MARKER_FILE.txt"),
"tree output missing marker:\n{out}"
);
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn oneshot_ctx_supplies_session_tool_calls_and_ledger() {
let _env_lock = crate::core::data_dir::test_env_lock();
let data = tempfile::tempdir().expect("data dir");
crate::test_env::set_var("LEAN_CTX_DATA_DIR", data.path());
let project = tempfile::tempdir().expect("project dir");
let ctx = oneshot_ctx(project.path().to_string_lossy().to_string(), HashMap::new());
assert!(ctx.session.is_some(), "session handle missing");
assert!(ctx.tool_calls.is_some(), "tool_calls handle missing");
assert!(ctx.ledger.is_some(), "ledger handle missing");
}
#[test]
fn oneshot_session_without_an_index_returns_a_default() {
let _env_lock = crate::core::data_dir::test_env_lock();
let data = tempfile::tempdir().expect("data dir");
crate::test_env::set_var("LEAN_CTX_DATA_DIR", data.path());
let project = tempfile::tempdir().expect("project dir");
let session = oneshot_session(&project.path().to_string_lossy());
assert!(
session.task.is_none(),
"expected a default session for a root with no index"
);
}
#[test]
fn ctx_read_via_call_no_longer_aborts_on_a_missing_session() {
let _env_lock = crate::core::data_dir::test_env_lock();
let data = tempfile::tempdir().expect("data dir");
crate::test_env::set_var("LEAN_CTX_DATA_DIR", data.path());
let project = tempfile::tempdir().expect("project dir");
let file = project.path().join("sample.rs");
std::fs::write(&file, b"pub fn sample() -> u32 { 7 }\n").expect("write sample");
let args = vec![
"ctx_read".to_string(),
"--project-root".to_string(),
project.path().to_string_lossy().to_string(),
"--json".to_string(),
format!(
r#"{{"path": {}, "mode": "signatures"}}"#,
serde_json::to_string(file.to_string_lossy().as_ref()).expect("encode path")
),
];
let out = run_call(&args).expect("ctx_read should dispatch, not abort");
assert!(
out.contains("sample"),
"ctx_read output missing the file's symbol:\n{out}"
);
}
fn session_file_names(dir: &std::path::Path) -> Vec<String> {
let mut names: Vec<String> = std::fs::read_dir(dir)
.expect("sessions dir")
.flatten()
.map(|entry| entry.file_name().to_string_lossy().to_string())
.collect();
names.sort();
names
}
#[test]
fn an_indexed_session_loads_and_a_one_shot_call_never_writes_it_back() {
let _env_lock = crate::core::data_dir::test_env_lock();
let data = tempfile::tempdir().expect("data dir");
crate::test_env::set_var("LEAN_CTX_DATA_DIR", data.path());
let project = tempfile::tempdir().expect("project dir");
let root = project.path().to_string_lossy().to_string();
let mut seeded = crate::core::session::SessionState {
project_root: Some(root.clone()),
task: Some(crate::core::session::TaskInfo {
description: "seeded task description".to_string(),
intent: None,
progress_pct: None,
}),
..Default::default()
};
let seeded_id = seeded.id.clone();
seeded
.prepare_save()
.expect("prepare_save")
.write_to_disk()
.expect("write_to_disk");
let loaded = oneshot_session(&root);
assert_eq!(
loaded.task.as_ref().map(|task| task.description.as_str()),
Some("seeded task description"),
"oneshot_session did not resolve the seeded session through the index"
);
let sessions = data.path().join("sessions");
let seeded_path = sessions.join(format!("{seeded_id}.json"));
let before = std::fs::read(&seeded_path).expect("seeded session file");
let files_before = session_file_names(&sessions);
let args = vec![
"ctx_session".to_string(),
"--project-root".to_string(),
root,
"--json".to_string(),
r#"{"action": "task", "value": "a different task"}"#.to_string(),
];
let out = run_call(&args).expect("ctx_session action=task should run to completion");
assert!(
out.contains("Task set"),
"ctx_session did not report success:\n{out}"
);
assert_eq!(
before,
std::fs::read(&seeded_path).expect("seeded session file"),
"a one-shot call rewrote the seeded session file"
);
assert_eq!(
files_before,
session_file_names(&sessions),
"a one-shot call created or removed a session file"
);
}
#[test]
fn a_one_shot_call_never_truncates_the_global_ledger() {
let _env_lock = crate::core::data_dir::test_env_lock();
let state = tempfile::tempdir().expect("state dir");
crate::test_env::set_var("LEAN_CTX_STATE_DIR", state.path());
let data = tempfile::tempdir().expect("data dir");
crate::test_env::set_var("LEAN_CTX_DATA_DIR", data.path());
let project = tempfile::tempdir().expect("project dir");
let marker_file = project.path().join("LEDGER_SEED_MARKER.rs");
std::fs::write(&marker_file, b"pub fn seeded() -> u32 { 1 }\n").expect("write marker");
let mut seeded = crate::core::context_ledger::ContextLedger::new();
seeded.record(&marker_file.to_string_lossy(), "full", 4_000, 1_000);
seeded.save();
let ledger_file = state.path().join("context_ledger.json");
let before = std::fs::read_to_string(&ledger_file).expect("seeded ledger file");
assert!(
before.contains("LEDGER_SEED_MARKER"),
"precondition: the seed never reached {}",
ledger_file.display()
);
let args = vec![
"ctx_control".to_string(),
"--project-root".to_string(),
project.path().to_string_lossy().to_string(),
"--json".to_string(),
r#"{"action": "list"}"#.to_string(),
];
run_call(&args).expect("ctx_control should dispatch");
let after = crate::core::context_ledger::ContextLedger::load();
assert!(
after
.entries
.iter()
.any(|entry| entry.path.contains("LEDGER_SEED_MARKER")),
"a one-shot call truncated the global ledger — {} entries survived; file now:\n{}",
after.entries.len(),
std::fs::read_to_string(&ledger_file).unwrap_or_default()
);
}
}