use std::sync::Arc;
use async_trait::async_trait;
use crate::error::DomainError;
use crate::ports::agent::AgentPort;
use crate::ports::AgentDescriptor;
use crate::value_objects::{AgentId, AuthorizationEvidence};
#[async_trait]
pub trait AgentRegistryPort: Send + Sync {
async fn register(&self, agent: Arc<dyn AgentPort>) -> Result<(), DomainError>;
async fn register_authorized(
&self,
agent: Arc<dyn AgentPort>,
authorization: Option<AuthorizationEvidence>,
) -> Result<(), DomainError> {
reject_unsupported(authorization.as_ref())?;
self.register(agent).await
}
async fn register_described(
&self,
descriptor: AgentDescriptor,
agent: Arc<dyn AgentPort>,
) -> Result<(), DomainError> {
if descriptor.id != *agent.id() || descriptor.specialty != *agent.specialty() {
return Err(DomainError::InvariantViolated {
reason: "agent descriptor and materialized identity differ",
});
}
self.register(agent).await
}
async fn register_described_authorized(
&self,
descriptor: AgentDescriptor,
agent: Arc<dyn AgentPort>,
authorization: Option<AuthorizationEvidence>,
) -> Result<(), DomainError> {
if descriptor.id != *agent.id() || descriptor.specialty != *agent.specialty() {
return Err(DomainError::InvariantViolated {
reason: "agent descriptor and materialized identity differ",
});
}
reject_unsupported(authorization.as_ref())?;
self.register_described(descriptor, agent).await
}
async fn unregister(&self, id: &AgentId) -> Result<(), DomainError>;
async fn unregister_authorized(
&self,
id: &AgentId,
authorization: Option<AuthorizationEvidence>,
) -> Result<(), DomainError> {
reject_unsupported(authorization.as_ref())?;
self.unregister(id).await
}
}
fn reject_unsupported(authorization: Option<&AuthorizationEvidence>) -> Result<(), DomainError> {
if authorization.is_some() {
return Err(DomainError::InvariantViolated {
reason: "agent registry adapter cannot persist authorization evidence",
});
}
Ok(())
}