use serde_json::Value;
pub enum Ran {
Exit(u8),
NoClient(Vec<Value>),
Cancelled,
NoneSelected,
Done(Outcome),
}
pub struct Outcome {
pub write: bool,
pub level: String,
pub rows: Vec<Value>,
pub searched: Vec<Value>,
pub installation: Value,
pub storage: Value,
pub failed: bool,
}
pub const PARTS: [&str; 4] = ["mcp", "hooks", "cache", "skills"];
#[derive(Debug, PartialEq, Eq)]
pub enum State {
Connected,
Partial,
Failed,
}
#[derive(Debug, PartialEq, Eq)]
pub enum Why {
Refused,
Missing(String),
Failed(String),
NotApplied,
}
#[derive(Debug, PartialEq, Eq)]
pub struct Agent {
pub client: String,
pub label: String,
pub state: State,
pub problem: Option<(&'static str, Why)>,
}
fn applied(part: &Value, write: bool) -> bool {
part["success"] == true && (!write || part["applied"] == true)
}
pub fn agents(outcome: &Outcome) -> Vec<Agent> {
outcome
.rows
.iter()
.map(|row| {
let problem = PARTS
.into_iter()
.find(|k| !row[k].is_null() && !applied(&row[k], outcome.write))
.map(|k| (k, why(&row[k])));
let state = match &problem {
None => State::Connected,
Some(("mcp", _)) => State::Failed,
Some(_) => State::Partial,
};
Agent {
client: text(row, "client"),
label: text(row, "label"),
state,
problem,
}
})
.collect()
}
fn text(row: &Value, key: &str) -> String {
row[key].as_str().unwrap_or("-").to_owned()
}
fn why(part: &Value) -> Why {
if part["success"] != true {
return Why::Refused;
}
let Some(apply) = part["apply"].as_str() else {
return Why::NotApplied;
};
let program = shlex::split(apply)
.and_then(|argv| argv.into_iter().next())
.map(|first| {
std::path::Path::new(&first)
.file_name()
.map_or(first.clone(), |name| name.to_string_lossy().into_owned())
})
.unwrap_or_default();
if part["apply_error"].is_string() {
Why::Failed(program)
} else {
Why::Missing(program)
}
}
pub fn restart(outcome: &Outcome) -> bool {
outcome
.rows
.iter()
.any(|row| row["mcp"]["applied"] == true || row["hooks"]["applied"] == true)
}
pub fn codex_review(outcome: &Outcome) -> bool {
outcome.level == "user"
&& outcome.rows.iter().any(|row| {
row["client"] == "codex"
&& row["hooks"]["applied"] == true
&& row["hooks"]["trust_hint"].is_string()
})
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn outcome(rows: Vec<Value>) -> Outcome {
Outcome {
write: true,
level: "user".into(),
rows,
searched: vec![],
installation: Value::Null,
storage: Value::Null,
failed: false,
}
}
#[test]
fn each_client_has_one_outcome_and_its_first_problem() {
let ok = json!({"success": true, "applied": true});
let done = outcome(vec![
json!({"client": "cursor", "label": "Cursor", "mcp": ok, "hooks": ok}),
json!({"client": "codex", "label": "Codex",
"mcp": {"success": true, "applied": false, "apply": "/usr/local/bin/codex mcp add neosian", "apply_error": null},
"hooks": {"success": true, "applied": true, "trust_hint": "open /hooks"},
"skills": {"success": true, "applied": false}}),
json!({"client": "claude-code", "label": "Claude Code", "mcp": ok, "hooks": ok,
"cache": {"success": false, "error": "cannot write /x"}}),
json!({"client": "pi", "label": "Pi", "mcp": {"success": true, "applied": false,
"apply": "pi add", "apply_error": "exit 1"}, "hooks": ok}),
]);
let agents = agents(&done);
assert_eq!(agents[0].state, State::Connected);
assert_eq!(agents[0].problem, None);
assert_eq!(agents[1].state, State::Failed);
assert_eq!(
agents[1].problem,
Some(("mcp", Why::Missing("codex".into())))
);
assert_eq!(agents[2].state, State::Partial);
assert_eq!(agents[2].problem, Some(("cache", Why::Refused)));
assert_eq!(agents[3].problem, Some(("mcp", Why::Failed("pi".into()))));
assert!(restart(&done) && codex_review(&done));
assert!(!codex_review(&outcome(vec![json!({"client": "codex",
"hooks": {"success": true, "applied": false, "trust_hint": "x"}})])));
}
}