pub const NOT_AUTHORIZED_PREFIX: &str = "not authorized:";
pub fn not_authorized(msg: &str) -> String {
format!("{NOT_AUTHORIZED_PREFIX} {msg}")
}
pub fn is_not_authorized(text: &str) -> bool {
let t = text.trim_start();
let t = t.strip_prefix("Error:").map(str::trim_start).unwrap_or(t);
t.len() >= NOT_AUTHORIZED_PREFIX.len()
&& t[..NOT_AUTHORIZED_PREFIX.len()].eq_ignore_ascii_case(NOT_AUTHORIZED_PREFIX)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn refusals_are_recognised_with_or_without_the_mcp_wrapper() {
assert!(is_not_authorized(¬_authorized(
"target 'ghost' for parallel_jobs"
)));
assert!(is_not_authorized("Error: not authorized: fleet_run denied"));
assert!(is_not_authorized(" NOT AUTHORIZED: x"));
assert!(
!is_not_authorized("the file says: not authorized: nope"),
"prefix, not substring"
);
assert!(!is_not_authorized("permission denied"));
}
}