use adk_core::{PolicyCall, PolicyDecision, ToolPolicy, ToolPolicyRequest};
use async_trait::async_trait;
use super::driver_of;
use super::supervisor::DriverSupervisor;
pub const DRIVER_POLICY_REF: &str = "adk-kernel/drivers";
#[derive(Debug, Clone)]
pub struct DriverPolicy {
drivers: DriverSupervisor,
}
impl DriverPolicy {
pub fn new(drivers: DriverSupervisor) -> Self {
Self { drivers }
}
pub fn decide(&self, id: &str, tool: &str, digest: &str) -> PolicyDecision {
let Some(record) = self.drivers.record(id) else {
return PolicyDecision::deny(format!(
"driver `{id}` is not mounted; a driver runs only with a manifest the user \
approved (adk-rust drivers mount <file>)"
));
};
let pins = &record.manifest.declaration_digests;
let (tool, stale) = match self.drivers.wrapped_tool(id, tool) {
Some(wrapped) => {
let stale = record.stale.contains_key(&wrapped)
|| (!pins.is_empty() && !pins.contains_key(&wrapped));
(wrapped, stale)
}
None => {
let pinned = pins.get(tool);
let stale = record.stale.contains_key(tool)
|| pinned.is_some_and(|pinned| pinned != digest)
|| (pinned.is_none() && !pins.is_empty());
(tool.to_string(), stale)
}
};
let tool = tool.as_str();
if stale {
return PolicyDecision::require_approval(format!(
"`{tool}` of driver `{id}` declares something other than what was approved, so \
its standing rules do not apply and every call needs approval until the driver \
is re-approved (adk-rust drivers reapprove {id})"
));
}
let class = record.manifest.class_of(tool);
if class.is_never_auto_allowed() {
return PolicyDecision::require_approval(format!(
"`{tool}` of driver `{id}` is `{class}`, which runs only when a person approves \
the call"
));
}
PolicyDecision::Allow
}
}
#[async_trait]
impl ToolPolicy for DriverPolicy {
async fn evaluate(&self, _request: &ToolPolicyRequest) -> PolicyDecision {
PolicyDecision::Allow
}
async fn evaluate_call(
&self,
request: &ToolPolicyRequest,
call: &PolicyCall<'_>,
) -> PolicyDecision {
match call.origin {
Some(origin) => match driver_of(&origin.source) {
Some(id) => self.decide(id, &request.tool_name, &origin.declaration_digest),
None => PolicyDecision::Allow,
},
None => PolicyDecision::Allow,
}
}
fn policy_ref(&self) -> Option<String> {
Some(DRIVER_POLICY_REF.to_string())
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use super::*;
use crate::drivers::{DriverManifest, DriverRecord, DriverStore};
use crate::stores::InMemoryDriverStore;
async fn supervisor(stale: bool) -> DriverSupervisor {
let manifest = DriverManifest::from_toml(
"id = \"calendar\"\ncommand = \"mcp-calendar\"\npartitions = [\"work\"]\n\
[tools]\nlist_events = \"read\"\npay = \"financial\"\n",
)
.unwrap();
let mut record = DriverRecord::new(manifest, "alice", chrono::Utc::now());
for tool in ["list_events", "pay"] {
record.manifest.declaration_digests.insert(tool.into(), format!("sha256:{tool}"));
}
if stale {
record.stale.insert("list_events".into(), "sha256:changed".into());
}
let store = Arc::new(InMemoryDriverStore::new());
store.put(record).await.unwrap();
let drivers = DriverSupervisor::builder(store).build();
drivers.load().await.unwrap();
drivers.register_crossing("calendar", "work__list_events", "list_events");
drivers.register_crossing("calendar", "work__pay", "pay");
drivers
}
#[tokio::test]
async fn a_crossing_is_judged_as_the_driver_tool_it_wraps() {
let fresh = DriverPolicy::new(supervisor(false).await);
assert_eq!(
fresh.decide("calendar", "work__list_events", "sha256:renamed"),
PolicyDecision::Allow
);
assert_eq!(
fresh.drivers.class_of("driver:calendar", "work__list_events"),
Some(crate::drivers::EffectClass::Read)
);
assert!(matches!(
fresh.decide("calendar", "work__pay", "sha256:renamed"),
PolicyDecision::RequireApproval { .. }
));
let stale = DriverPolicy::new(supervisor(true).await);
assert!(matches!(
stale.decide("calendar", "work__list_events", "sha256:renamed"),
PolicyDecision::RequireApproval { .. }
));
assert!(matches!(
fresh.decide("calendar", "home__list_events", "sha256:x"),
PolicyDecision::RequireApproval { .. }
));
}
}