use serde::{Deserialize, Serialize};
use crate::ContextChannel;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct OutputContract {
pub tool_pattern: String,
pub expected_channels: Vec<ContextChannel>,
pub on_violation: ContractViolationAction,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum ContractViolationAction {
#[default]
Log,
Quarantine,
Block,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContractCheckResult {
Compliant,
NoContract,
Violation {
expected: Vec<ContextChannel>,
observed: ContextChannel,
action: ContractViolationAction,
},
}
pub fn check_output_contract(
tool_name: &str,
observed_channel: ContextChannel,
contracts: &[OutputContract],
) -> ContractCheckResult {
let contract = contracts
.iter()
.find(|c| tool_matches(&c.tool_pattern, tool_name));
let contract = match contract {
Some(c) => c,
None => return ContractCheckResult::NoContract,
};
if contract.expected_channels.contains(&observed_channel) {
ContractCheckResult::Compliant
} else {
ContractCheckResult::Violation {
expected: contract.expected_channels.clone(),
observed: observed_channel,
action: contract.on_violation.clone(),
}
}
}
fn tool_matches(pattern: &str, name: &str) -> bool {
if pattern == "*" {
return true;
}
if let Some(star) = pattern.find('*') {
let prefix = &pattern[..star];
let suffix = &pattern[star + 1..];
name.starts_with(prefix) && name.ends_with(suffix)
} else {
pattern == name
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compliant_response() {
let contracts = vec![OutputContract {
tool_pattern: "read_file".to_string(),
expected_channels: vec![ContextChannel::Data, ContextChannel::ResourceContent],
on_violation: ContractViolationAction::Block,
}];
let result = check_output_contract("read_file", ContextChannel::Data, &contracts);
assert_eq!(result, ContractCheckResult::Compliant);
}
#[test]
fn test_violation_detected() {
let contracts = vec![OutputContract {
tool_pattern: "read_file".to_string(),
expected_channels: vec![ContextChannel::Data],
on_violation: ContractViolationAction::Block,
}];
let result = check_output_contract("read_file", ContextChannel::CommandLike, &contracts);
match result {
ContractCheckResult::Violation {
observed, action, ..
} => {
assert_eq!(observed, ContextChannel::CommandLike);
assert_eq!(action, ContractViolationAction::Block);
}
_ => panic!("Expected violation"),
}
}
#[test]
fn test_no_contract_for_tool() {
let contracts = vec![OutputContract {
tool_pattern: "read_file".to_string(),
expected_channels: vec![ContextChannel::Data],
on_violation: ContractViolationAction::Log,
}];
let result = check_output_contract("write_file", ContextChannel::Data, &contracts);
assert_eq!(result, ContractCheckResult::NoContract);
}
#[test]
fn test_glob_pattern_matching() {
let contracts = vec![OutputContract {
tool_pattern: "read_*".to_string(),
expected_channels: vec![ContextChannel::Data],
on_violation: ContractViolationAction::Quarantine,
}];
let result = check_output_contract("read_database", ContextChannel::Url, &contracts);
match result {
ContractCheckResult::Violation { action, .. } => {
assert_eq!(action, ContractViolationAction::Quarantine);
}
_ => panic!("Expected violation"),
}
}
#[test]
fn test_wildcard_contract() {
let contracts = vec![OutputContract {
tool_pattern: "*".to_string(),
expected_channels: vec![ContextChannel::Data, ContextChannel::FreeText],
on_violation: ContractViolationAction::Log,
}];
let result = check_output_contract("anything", ContextChannel::CommandLike, &contracts);
assert!(matches!(result, ContractCheckResult::Violation { .. }));
}
}