use thiserror::Error;
use super::TransportType;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransportContractTier {
FirstPartyRuntime,
ObservationalHarness,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransportStartupMode {
ReadyOnCreate,
BackgroundWarmup,
ExplicitStart,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransportSemanticContract {
pub role_addressed_routing: bool,
pub authenticated_peers: bool,
pub per_peer_fifo_delivery: bool,
pub fail_closed_unknown_role: bool,
pub no_message_synthesis: bool,
pub explicit_readiness_errors: bool,
pub deterministic_for_regression: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransportOperationalContract {
pub transport_type: TransportType,
pub startup_mode: TransportStartupMode,
pub environment_resolved: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransportContractProfile {
pub transport_name: &'static str,
pub tier: TransportContractTier,
pub semantics: TransportSemanticContract,
pub operational: TransportOperationalContract,
pub notes: Vec<&'static str>,
}
#[derive(Debug, Error, PartialEq, Eq)]
pub enum TransportContractViolation {
#[error("first-party transports must route by role")]
MissingRoleRouting,
#[error("first-party transports must preserve per-peer FIFO delivery")]
MissingPerPeerFifo,
#[error("network transports must document whether peers are authenticated")]
MissingPeerAuthenticationDisclosure,
#[error("first-party transports must fail closed for unknown roles")]
MissingFailClosedUnknownRole,
#[error("first-party transports must not synthesize messages")]
MessageSynthesisAllowed,
#[error("background or explicit-start transports must expose readiness failures")]
MissingReadinessErrors,
#[error("observational harness transports must be deterministic for regression use")]
NonDeterministicHarness,
}
pub trait DocumentedTransportContract {
fn contract_profile() -> TransportContractProfile
where
Self: Sized;
}
pub fn validate_transport_contract_profile(
profile: &TransportContractProfile,
) -> Result<(), TransportContractViolation> {
match profile.tier {
TransportContractTier::FirstPartyRuntime => {
if !profile.semantics.role_addressed_routing {
return Err(TransportContractViolation::MissingRoleRouting);
}
if !profile.semantics.per_peer_fifo_delivery {
return Err(TransportContractViolation::MissingPerPeerFifo);
}
if !profile.semantics.fail_closed_unknown_role {
return Err(TransportContractViolation::MissingFailClosedUnknownRole);
}
if !profile.semantics.no_message_synthesis {
return Err(TransportContractViolation::MessageSynthesisAllowed);
}
if matches!(profile.operational.transport_type, TransportType::Tcp)
&& !profile
.notes
.iter()
.any(|note| note.contains("authenticated") || note.contains("trusted-network"))
{
return Err(TransportContractViolation::MissingPeerAuthenticationDisclosure);
}
}
TransportContractTier::ObservationalHarness => {
if !profile.semantics.deterministic_for_regression {
return Err(TransportContractViolation::NonDeterministicHarness);
}
}
}
if matches!(
profile.operational.startup_mode,
TransportStartupMode::BackgroundWarmup | TransportStartupMode::ExplicitStart
) && !profile.semantics.explicit_readiness_errors
{
return Err(TransportContractViolation::MissingReadinessErrors);
}
Ok(())
}
pub fn validated_transport_contract_profile<T>(
) -> Result<TransportContractProfile, TransportContractViolation>
where
T: DocumentedTransportContract,
{
let profile = T::contract_profile();
validate_transport_contract_profile(&profile)?;
Ok(profile)
}