use crate::traits::{ToolCallContext, UserRole};
use aidaemon_core::ChannelVisibility;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct VerifiedPrivateOwner<'a> {
session_id: &'a str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PrivateOwnerDenial {
SubAgentSession,
InternalExecution,
NotOwner,
NotPrivateChannel,
}
impl<'a> VerifiedPrivateOwner<'a> {
pub fn session_id(&self) -> &'a str {
self.session_id
}
pub fn verify(facts: &'a ToolCallContext) -> Result<Self, PrivateOwnerDenial> {
Self::verify_parts(&facts.session_id, facts.user_role, facts.channel_visibility)
}
pub fn verify_parts(
session_id: &'a str,
user_role: Option<UserRole>,
channel_visibility: Option<ChannelVisibility>,
) -> Result<Self, PrivateOwnerDenial> {
if session_id.starts_with("specialist:") || session_id.starts_with("sub-") {
return Err(PrivateOwnerDenial::SubAgentSession);
}
if channel_visibility == Some(ChannelVisibility::Internal) {
return Err(PrivateOwnerDenial::InternalExecution);
}
if user_role != Some(UserRole::Owner) {
return Err(PrivateOwnerDenial::NotOwner);
}
if channel_visibility != Some(ChannelVisibility::Private) {
return Err(PrivateOwnerDenial::NotPrivateChannel);
}
Ok(Self { session_id })
}
}
impl PrivateOwnerDenial {
pub fn automations_refusal(&self) -> &'static str {
match self {
Self::NotOwner | Self::NotPrivateChannel => {
"Automations can only be seen or changed by the owner in a private chat."
}
Self::InternalExecution => {
"Automations cannot be seen or changed from an internal system turn (visibility is internal, not a private owner chat)."
}
Self::SubAgentSession => {
"Automations cannot be seen or changed from a specialist or sub-agent session."
}
}
}
pub fn mandates_refusal(&self, action: &str) -> String {
match self {
Self::InternalExecution => "Mandates cannot be seen or changed from an internal system turn (visibility is internal, not a private owner chat).".to_string(),
Self::SubAgentSession => "Mandates cannot be seen or changed from a specialist or sub-agent session.".to_string(),
Self::NotOwner | Self::NotPrivateChannel => {
let (subject, verb) = match action {
"draft" => ("Mandate drafts", "prepared for"),
"create" => ("Mandates", "created by"),
"list" => ("Mandates", "listed by"),
"get" => ("Mandates", "inspected by"),
"update" => ("Mandate authority", "changed by"),
"list_intentions" => ("Mandate intentions", "inspected by"),
_ => ("Mandates", "changed by"),
};
format!("{subject} can only be {verb} the owner in a verified private channel.")
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn verification_uses_typed_facts_in_diagnostic_order() {
for (session, role, visibility, expected) in [
(
"specialist:synthetic",
None,
Some(ChannelVisibility::Internal),
PrivateOwnerDenial::SubAgentSession,
),
(
"sub-synthetic",
Some(UserRole::Owner),
Some(ChannelVisibility::Private),
PrivateOwnerDenial::SubAgentSession,
),
(
"app:synthetic",
None,
Some(ChannelVisibility::Internal),
PrivateOwnerDenial::InternalExecution,
),
("app:synthetic", None, None, PrivateOwnerDenial::NotOwner),
(
"app:synthetic",
Some(UserRole::Owner),
None,
PrivateOwnerDenial::NotPrivateChannel,
),
] {
assert_eq!(
VerifiedPrivateOwner::verify_parts(session, role, visibility),
Err(expected)
);
}
for session in [
"",
"app:synthetic",
"telegram:synthetic",
"slack:synthetic",
"discord:synthetic",
] {
let facts = ToolCallContext {
session_id: session.to_string(),
user_role: Some(UserRole::Owner),
channel_visibility: Some(ChannelVisibility::Private),
..Default::default()
};
assert_eq!(
VerifiedPrivateOwner::verify(&facts).unwrap().session_id(),
session
);
}
}
}