pub mod constitutional {
pub use crate::kernel_boundary::{
AdapterTrace,
ContentKind,
ContextFact,
ContractResult,
DataClassification,
DecisionStep,
ExecutionEnv,
KernelContext,
KernelIntent,
KernelPolicy,
KernelProposal,
LocalReplayTrace,
ProposalKind,
ProposedContent,
RecallTrace,
RemoteReplayTrace,
ReplayTrace,
Replayability,
ReplayabilityDowngradeReason,
RiskTier,
RoutingPolicy,
SamplerParams,
};
}
#[derive(Debug, Clone)]
pub struct AuthorityGrant {
grantor: AuthorityGrantor,
granted_at: crate::types::Timestamp,
scope: Option<AuthorityScope>,
}
#[allow(dead_code)]
impl AuthorityGrant {
pub(crate) fn system() -> Self {
Self {
grantor: AuthorityGrantor::System,
granted_at: crate::types::Timestamp::now(),
scope: None,
}
}
pub(crate) fn human(approver_id: impl Into<String>) -> Self {
Self {
grantor: AuthorityGrantor::Human {
approver_id: approver_id.into(),
},
granted_at: crate::types::Timestamp::now(),
scope: None,
}
}
pub(crate) fn policy(policy_id: impl Into<String>) -> Self {
Self {
grantor: AuthorityGrantor::Policy {
policy_id: policy_id.into(),
},
granted_at: crate::types::Timestamp::now(),
scope: None,
}
}
pub fn grantor(&self) -> &AuthorityGrantor {
&self.grantor
}
pub fn granted_at(&self) -> &crate::types::Timestamp {
&self.granted_at
}
pub fn scope(&self) -> Option<&AuthorityScope> {
self.scope.as_ref()
}
pub(crate) fn with_scope(mut self, scope: AuthorityScope) -> Self {
self.scope = Some(scope);
self
}
}
#[derive(Debug, Clone)]
pub enum AuthorityGrantor {
System,
Human {
approver_id: String,
},
Policy {
policy_id: String,
},
}
#[derive(Debug, Clone)]
pub struct AuthorityScope {
pub proposal_kinds: Option<Vec<crate::kernel_boundary::ProposalKind>>,
pub gate_ids: Option<Vec<crate::types::GateId>>,
pub expires_at: Option<crate::types::Timestamp>,
}
impl AuthorityScope {
pub fn new() -> Self {
Self {
proposal_kinds: None,
gate_ids: None,
expires_at: None,
}
}
pub fn with_proposal_kinds(mut self, kinds: Vec<crate::kernel_boundary::ProposalKind>) -> Self {
self.proposal_kinds = Some(kinds);
self
}
pub fn with_gate_ids(mut self, ids: Vec<crate::types::GateId>) -> Self {
self.gate_ids = Some(ids);
self
}
pub fn with_expiration(mut self, expires_at: crate::types::Timestamp) -> Self {
self.expires_at = Some(expires_at);
self
}
}
impl Default for AuthorityScope {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn system_authority_grant() {
let grant = AuthorityGrant::system();
assert!(matches!(grant.grantor(), AuthorityGrantor::System));
assert!(grant.scope().is_none());
}
#[test]
fn human_authority_grant() {
let grant = AuthorityGrant::human("user@example.com");
match grant.grantor() {
AuthorityGrantor::Human { approver_id } => {
assert_eq!(approver_id, "user@example.com");
}
_ => panic!("Expected Human grantor"),
}
}
#[test]
fn policy_authority_grant_with_scope() {
let scope = AuthorityScope::new()
.with_proposal_kinds(vec![crate::kernel_boundary::ProposalKind::Claims]);
let grant = AuthorityGrant::policy("auto-promote-claims").with_scope(scope);
assert!(matches!(grant.grantor(), AuthorityGrantor::Policy { .. }));
assert!(grant.scope().is_some());
assert!(grant.scope().unwrap().proposal_kinds.is_some());
}
#[test]
fn authority_scope_builder() {
let scope = AuthorityScope::new()
.with_proposal_kinds(vec![
crate::kernel_boundary::ProposalKind::Claims,
crate::kernel_boundary::ProposalKind::Plan,
])
.with_gate_ids(vec![crate::types::GateId::new("gate-1")])
.with_expiration(crate::types::Timestamp::new("2025-01-01T00:00:00Z"));
assert_eq!(scope.proposal_kinds.as_ref().unwrap().len(), 2);
assert_eq!(scope.gate_ids.as_ref().unwrap().len(), 1);
assert!(scope.expires_at.is_some());
}
#[test]
fn authority_scope_default() {
let scope = AuthorityScope::default();
assert!(scope.proposal_kinds.is_none());
assert!(scope.gate_ids.is_none());
assert!(scope.expires_at.is_none());
}
}