use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::sync::{Arc, Mutex};
use adk_core::{
AgentId, ApprovalAction, ApprovalCall, ApprovalRequest, ApprovalRequestQuery, ApprovalRisk,
ApprovalRoute, ApprovalRouter, ApprovalScope, ApprovalState, ApprovalStore, Event, Part,
PolicyCall, PolicyDecision, ToolConfirmationRequest, ToolPolicy, ToolPolicyRequest,
};
use adk_kernel::PartitionId;
use adk_kernel::drivers::EffectClass;
use adk_session::GetRequest;
use async_trait::async_trait;
use super::registry::VerbRegistry;
use super::{Callers, MAX_WAITING, verbs_principal, words};
use crate::hints::{CardHint, CardHints};
use crate::model::{CardDetail, Reversibility};
use crate::source::Source;
pub const VERB_POLICY_REF: &str = "adk-surface/verbs";
#[derive(Debug, Clone)]
pub struct VerbPolicy {
registry: VerbRegistry,
area: Option<PartitionId>,
}
impl VerbPolicy {
pub(crate) fn new(registry: VerbRegistry, area: Option<PartitionId>) -> Self {
Self { registry, area }
}
async fn waiting(&self, agent: &AgentId, user: &str) -> usize {
let registry = self.registry.inner();
let Some(store) = ®istry.approvals else {
return 0;
};
let query = ApprovalRequestQuery::default()
.with_states([ApprovalState::Pending])
.with_app_name(®istry.app)
.with_user_id(user);
let origin = self.registry.origin();
let mut count = 0;
let found = every_request(store.as_ref(), query, |request| {
if request.origin.as_ref().is_some_and(|by| by.source == origin)
&& request.acting.as_ref().is_some_and(|acting| acting.agent.id == *agent)
{
count += 1;
}
count >= MAX_WAITING
})
.await;
if let Err(error) = found {
tracing::warn!(error = %error, "the waiting screen actions could not be counted");
}
count
}
async fn answered(&self, request: &ToolPolicyRequest, call: &PolicyCall<'_>) -> bool {
let Some(store) = &self.registry.inner().approvals else {
return false;
};
let Some(acting) = &request.acting else {
return false;
};
let scope = ApprovalScope::new(&request.app_name, &request.user_id, &request.session_id);
let mut held = ToolConfirmationRequest::new(&request.tool_name, request.args.clone())
.with_acting_context(acting.clone());
if let Some(origin) = call.origin {
held = held.with_origin(origin.clone());
}
if store.has_decision(&scope, &held.fingerprint()).await.unwrap_or(false) {
return true;
}
let query = ApprovalRequestQuery::default()
.with_session(&scope)
.with_states([ApprovalState::Approved]);
let mut edited = false;
let _ = every_request(store.as_ref(), query, |approved| {
edited = approved.tool_name == request.tool_name
&& approved.acting.as_ref().is_some_and(|by| by.agent.id == acting.agent.id)
&& approved
.decisions
.last()
.and_then(|decision| decision.edited_args.as_ref())
.is_some_and(|args| *args == request.args);
edited
})
.await;
edited
}
}
pub(crate) async fn every_request(
store: &dyn ApprovalStore,
mut query: ApprovalRequestQuery,
mut seen: impl FnMut(&ApprovalRequest) -> bool,
) -> adk_core::Result<()> {
loop {
let page = store.list_requests(&query).await?;
let full = page.len() >= query.limit;
for request in &page {
if seen(request) {
return Ok(());
}
}
match page.last() {
Some(last) if full => query = query.with_after(Some(last.cursor())),
_ => return Ok(()),
}
}
}
#[async_trait]
impl ToolPolicy for VerbPolicy {
async fn evaluate(&self, _request: &ToolPolicyRequest) -> PolicyDecision {
PolicyDecision::Allow
}
async fn evaluate_call(
&self,
request: &ToolPolicyRequest,
call: &PolicyCall<'_>,
) -> PolicyDecision {
let origin = self.registry.origin();
if call.origin.is_none_or(|by| by.source != origin) {
return PolicyDecision::Allow;
}
let person = verbs_principal(self.registry.app_name()).id;
let agent = request.acting.as_ref().map(|acting| acting.agent.id.clone());
if agent.as_ref() == Some(&person) {
return PolicyDecision::Allow;
}
let Some(entry) = self.registry.entry_for_tool(&request.tool_name) else {
return PolicyDecision::deny(format!(
"there is no screen action `{}` on this home",
request.tool_name
));
};
if entry.spec.reads() {
return PolicyDecision::Allow;
}
if entry.spec.callers == Callers::PersonOnly {
return PolicyDecision::deny(format!(
"only a person takes `{}`, on a screen",
entry.spec.name
));
}
if let Some(agent) = &agent
&& self.waiting(agent, &request.user_id).await >= MAX_WAITING
&& !self.answered(request, call).await
{
let name = self.registry.display_name(&request.agent_name);
return PolicyDecision::deny(words::already_waiting(&name, MAX_WAITING));
}
let scope = self.registry.scope(&request.user_id, self.area.clone());
match entry.handler.describe(&scope, &request.args).await {
Ok(text) => PolicyDecision::require_approval(text),
Err(error) => PolicyDecision::deny(error.to_string()),
}
}
fn policy_ref(&self) -> Option<String> {
Some(VERB_POLICY_REF.to_string())
}
}
#[derive(Clone)]
pub struct VerbRouter {
origin: String,
effects: BTreeMap<String, EffectClass>,
fallback: Arc<dyn ApprovalRouter>,
}
impl std::fmt::Debug for VerbRouter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("VerbRouter")
.field("origin", &self.origin)
.field("tools", &self.effects.keys().collect::<Vec<_>>())
.finish_non_exhaustive()
}
}
impl VerbRouter {
pub(crate) fn new(registry: VerbRegistry, fallback: Arc<dyn ApprovalRouter>) -> Self {
let effects = registry
.entries()
.iter()
.map(|entry| (entry.spec.tool_name(), entry.spec.effect))
.collect();
Self { origin: registry.origin(), effects, fallback }
}
pub fn catalogue(app: &str, fallback: impl ApprovalRouter + 'static) -> Self {
let effects = super::snooze::catalogue()
.into_iter()
.map(|entry| (entry.spec.tool_name(), entry.spec.effect))
.collect();
Self { origin: super::verbs_origin(app), effects, fallback: Arc::new(fallback) }
}
}
fn risk_of(effect: EffectClass) -> ApprovalRisk {
match effect {
EffectClass::Read | EffectClass::WriteLocal => ApprovalRisk::Low,
EffectClass::PublishPublic | EffectClass::Financial | EffectClass::Delete => {
ApprovalRisk::High
}
_ => ApprovalRisk::Medium,
}
}
fn policy_words(reason: &str, tool: &str) -> String {
let prefix = format!("Tool '{tool}' requires confirmation: ");
reason.strip_prefix(&prefix).unwrap_or(reason).to_string()
}
impl ApprovalRouter for VerbRouter {
fn route(&self, call: &ApprovalCall) -> Option<ApprovalRoute> {
let route = self.fallback.route(call)?;
if call.request.origin.as_ref().is_none_or(|by| by.source != self.origin) {
return Some(route);
}
let Some(effect) = self.effects.get(&call.request.tool_name) else {
return Some(route);
};
Some(
route
.with_summary(policy_words(&call.reason, &call.request.tool_name))
.with_risk(risk_of(*effect))
.with_allowed_actions([
ApprovalAction::Approve,
ApprovalAction::Edit,
ApprovalAction::Reject,
]),
)
}
}
#[derive(Clone)]
pub struct VerbHints {
registry: VerbRegistry,
source: Option<Arc<dyn Source>>,
reads: Arc<Mutex<HashMap<String, Reads>>>,
}
impl std::fmt::Debug for VerbHints {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("VerbHints")
.field("registry", &self.registry)
.field("source", &self.source.is_some())
.finish_non_exhaustive()
}
}
#[derive(Debug, Clone)]
struct Reads {
agent: String,
pages: usize,
hosts: BTreeSet<String>,
}
const KEPT_TURNS: usize = 512;
const MAX_HOST: usize = 253;
impl VerbHints {
pub(crate) fn new(registry: VerbRegistry) -> Self {
Self { registry, source: None, reads: Arc::new(Mutex::new(HashMap::new())) }
}
#[must_use]
pub fn reading(mut self, source: Arc<dyn Source>) -> Self {
self.source = Some(source);
self
}
async fn events(&self, request: &ApprovalRequest) -> Option<Vec<Event>> {
let scope = &request.scope;
if let Some(sessions) = &self.registry.inner().sessions {
let session = sessions
.get(GetRequest {
app_name: scope.app_name.clone(),
user_id: scope.user_id.clone(),
session_id: scope.session_id.clone(),
num_recent_events: None,
after: None,
})
.await
.ok()?;
return Some(session.events().all());
}
let source = self.source.as_ref()?;
source.transcript(&scope.user_id, &scope.session_id).await.ok().flatten()
}
async fn reads(&self, request: &ApprovalRequest) -> Option<Reads> {
if let Some(reads) = self.reads.lock().unwrap_or_else(|e| e.into_inner()).get(&request.id) {
return Some(reads.clone());
}
let events = self.events(request).await?;
let (at, asking) = events.iter().enumerate().rev().find(|(_, event)| {
event.content().is_some_and(|content| {
content.parts.iter().any(|part| {
matches!(part, Part::FunctionCall { name, args, .. }
if *name == request.tool_name && *args == request.args_redacted)
})
})
})?;
let mut reads = Reads {
agent: Some(asking.author.clone())
.filter(|author| !author.is_empty() && author != "user")
.or_else(|| {
request.acting.as_ref().and_then(|acting| {
acting.agent.id.as_str().rsplit('/').next().map(str::to_string)
})
})
.unwrap_or_else(|| "The agent".to_string()),
pages: 0,
hosts: BTreeSet::new(),
};
let turn = events[..at].iter().filter(|event| event.invocation_id == asking.invocation_id);
for part in turn.filter_map(Event::content).flat_map(|content| content.parts.iter()) {
if let Part::FunctionResponse { function_response, .. } = part
&& function_response.name == "web_fetch"
&& let Some(host) = fetched(&function_response.response)
{
reads.pages += 1;
reads.hosts.extend(host);
}
}
let mut kept = self.reads.lock().unwrap_or_else(|e| e.into_inner());
if kept.len() >= KEPT_TURNS {
kept.clear();
}
kept.insert(request.id.clone(), reads.clone());
Some(reads)
}
async fn first_step(&self, request: &ApprovalRequest) -> Option<String> {
let reads = self.reads(request).await?;
if reads.pages == 0 {
return None;
}
let added = match &self.source {
Some(source) => source.agent(&request.scope.user_id, &reads.agent).await.ok().flatten(),
None => None,
};
let agent = match added {
Some(detail) => detail.agent.display_name,
None => self.registry.display_name(&reads.agent),
};
let what = match reads.pages {
1 => "1 web page".to_string(),
pages => format!("{pages} web pages"),
};
let hosts: Vec<String> = reads.hosts.into_iter().collect();
Some(match hosts.is_empty() {
true => format!("{agent} read {what}"),
false => format!("{agent} read {what} ({})", hosts.join(", ")),
})
}
}
fn fetched(response: &serde_json::Value) -> Option<Option<String>> {
if response.get("error").is_some() || response.get("status").is_none() {
return None;
}
let url = response.get("final_url").or_else(|| response.get("url"))?.as_str()?;
let host = reqwest::Url::parse(url)
.ok()
.filter(|url| matches!(url.scheme(), "http" | "https"))
.and_then(|url| url.host_str().map(str::to_ascii_lowercase))
.filter(|host| !host.is_empty() && host.len() <= MAX_HOST);
Some(host)
}
fn written(request: &ApprovalRequest) -> bool {
let summary = request.summary.trim();
!(summary.is_empty()
|| summary.starts_with(&format!("Tool '{}'", request.tool_name))
|| summary == format!("Approve a call to '{}'", request.tool_name))
}
#[async_trait]
impl CardHints for VerbHints {
async fn hint(&self, request: &ApprovalRequest) -> Option<CardHint> {
let origin = request.origin.as_ref()?;
if origin.source != self.registry.origin() {
return None;
}
let entry = self.registry.entry_for_tool(&request.tool_name)?;
let mut hint = CardHint::new()
.with_chat_heading(entry.spec.description.clone())
.without_always_allow()
.with_detail(CardDetail::new(words::AGENTS_ALWAYS_ASK));
if written(request) {
hint = hint.with_heading(request.summary.clone());
}
if entry.spec.reversible {
hint = hint.with_reversibility(Reversibility::CanBeChangedBack);
}
if let Some(step) = self.first_step(request).await {
hint = hint.with_first_step(step);
}
Some(hint)
}
async fn names(&self, request: &ApprovalRequest) -> Option<String> {
let origin = request.origin.as_ref()?;
if origin.source != self.registry.origin() {
return None;
}
let entry = self.registry.entry_for_tool(&request.tool_name)?;
let id = request.args_redacted.get("item").and_then(serde_json::Value::as_str)?;
match self.registry.scope(&request.scope.user_id, None).item(id).await {
Ok(item) => Some(item.title),
Err(_) if entry.spec.name == "snooze" => {
words::title_of_move(&request.summary).map(str::to_string)
}
Err(_) => None,
}
}
}