// K2381-K2385: configuration.schema — Sürümlü servis yapılandırması // Invariant: Şema ve bilinmeyen zorunlu alan ayrılır // Positive: Console ve shell config // Counterexample: Döngü veya aşırı uzunluk // Acceptance: Eski güvenli config korunur pub const CONFIGURATION_SCHEMA_VERSION_ID: &str = "turkos.os.configuration.schema.v1"; pub const CONFIGURATION_SCHEMA_INVARIANT: &str = "Şema ve bilinmeyen zorunlu alan ayrılır"; pub const CONFIGURATION_SCHEMA_POSITIVE: &str = "Console ve shell config"; pub const CONFIGURATION_SCHEMA_COUNTEREXAMPLE: &str = "Döngü veya aşırı uzunluk"; pub const CONFIGURATION_SCHEMA_ACCEPTANCE: &str = "Eski güvenli config korunur"; pub const EXPECTED_VERSIONED_SERVICE_CONFIGS: u32 = 2; pub const EXPECTED_SEPARATED_SCHEMA_FIELDS: u32 = 4; pub const EXPECTED_PRESERVED_SAFE_FALLBACKS: u32 = 2; pub const EXPECTED_CORRUPTED_PREVIOUS_CONFIGS: u32 = 0; #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ConfigurationSchemaError { CycleOrExcessiveLengthDetected = 1, SchemaOrUnknownRequiredFieldConflated = 2, PreviousSafeConfigOverwritten = 3, VersionedServiceConfigsMismatch = 4, SeparatedSchemaFieldsMismatch = 5, PreservedSafeFallbacksMismatch = 6, ZeroSchemaEpoch = 7, NonMonotonicSchemaEpoch = 8, ZeroSchemaConfigDigest = 9, UnboundConfigurationSchemaDigest = 10, AlreadyCommitted = 11, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct ConfigurationSchemaRequest { pub cycle_or_excessive_length_detected: bool, pub schema_or_unknown_required_field_conflated: bool, pub previous_safe_config_preserved: bool, pub versioned_service_configs: u32, pub separated_schema_fields: u32, pub preserved_safe_fallbacks: u32, pub schema_epoch: u64, pub previous_schema_epoch: u64, pub schema_config_digest: u64, pub schema_binding_digest: u64, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct ConfigurationSchemaReceipt { pub versioned_service_configs: u32, pub separated_schema_fields: u32, pub preserved_safe_fallbacks: u32, pub corrupted_previous_configs: u32, pub previous_safe_config_preserved: bool, pub schema_epoch: u64, pub schema_config_digest: u64, pub receipt_digest: u64, } pub const fn compute_configuration_schema_binding_digest( versioned_service_configs: u32, separated_schema_fields: u32, preserved_safe_fallbacks: u32, corrupted_previous_configs: u32, schema_epoch: u64, schema_config_digest: u64, ) -> u64 { let mut h: u64 = 0xcbf2_9ce4_8422_2325; h = (h ^ 0x2385_4346_5343_484d).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (versioned_service_configs as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (separated_schema_fields as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (preserved_safe_fallbacks as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (corrupted_previous_configs as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ schema_epoch).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ schema_config_digest).wrapping_mul(0x0000_0100_0000_01b3); h } pub struct ConfigurationSchemaCoordinator { committed: bool, committed_total: u64, rejected_total: u64, schema_validation_refusals: u64, schema_accounting_refusals: u64, committed_receipt: Option, } impl ConfigurationSchemaCoordinator { pub const fn new() -> Self { Self { committed: false, committed_total: 0, rejected_total: 0, schema_validation_refusals: 0, schema_accounting_refusals: 0, committed_receipt: None, } } pub fn is_committed(&self) -> bool { self.committed } pub fn committed_total(&self) -> u64 { self.committed_total } pub fn rejected_total(&self) -> u64 { self.rejected_total } pub fn schema_validation_refusals(&self) -> u64 { self.schema_validation_refusals } pub fn schema_accounting_refusals(&self) -> u64 { self.schema_accounting_refusals } pub fn committed_receipt(&self) -> Option { self.committed_receipt } pub fn commit_versioned_service_config( &mut self, req: &ConfigurationSchemaRequest, ) -> Result { if self.committed { self.rejected_total += 1; return Err(ConfigurationSchemaError::AlreadyCommitted); } if req.cycle_or_excessive_length_detected { self.rejected_total += 1; self.schema_validation_refusals += 1; return Err(ConfigurationSchemaError::CycleOrExcessiveLengthDetected); } if req.schema_or_unknown_required_field_conflated { self.rejected_total += 1; self.schema_validation_refusals += 1; return Err(ConfigurationSchemaError::SchemaOrUnknownRequiredFieldConflated); } if !req.previous_safe_config_preserved { self.rejected_total += 1; self.schema_validation_refusals += 1; return Err(ConfigurationSchemaError::PreviousSafeConfigOverwritten); } if req.versioned_service_configs != EXPECTED_VERSIONED_SERVICE_CONFIGS { self.rejected_total += 1; self.schema_accounting_refusals += 1; return Err(ConfigurationSchemaError::VersionedServiceConfigsMismatch); } if req.separated_schema_fields != EXPECTED_SEPARATED_SCHEMA_FIELDS { self.rejected_total += 1; self.schema_accounting_refusals += 1; return Err(ConfigurationSchemaError::SeparatedSchemaFieldsMismatch); } if req.preserved_safe_fallbacks != EXPECTED_PRESERVED_SAFE_FALLBACKS { self.rejected_total += 1; self.schema_accounting_refusals += 1; return Err(ConfigurationSchemaError::PreservedSafeFallbacksMismatch); } if req.schema_epoch == 0 { self.rejected_total += 1; return Err(ConfigurationSchemaError::ZeroSchemaEpoch); } if req.schema_epoch <= req.previous_schema_epoch { self.rejected_total += 1; return Err(ConfigurationSchemaError::NonMonotonicSchemaEpoch); } if req.schema_config_digest == 0 { self.rejected_total += 1; return Err(ConfigurationSchemaError::ZeroSchemaConfigDigest); } let expected_binding = compute_configuration_schema_binding_digest( req.versioned_service_configs, req.separated_schema_fields, req.preserved_safe_fallbacks, EXPECTED_CORRUPTED_PREVIOUS_CONFIGS, req.schema_epoch, req.schema_config_digest, ); if req.schema_binding_digest != expected_binding { self.rejected_total += 1; self.schema_accounting_refusals += 1; return Err(ConfigurationSchemaError::UnboundConfigurationSchemaDigest); } self.committed = true; self.committed_total += 1; let receipt = ConfigurationSchemaReceipt { versioned_service_configs: req.versioned_service_configs, separated_schema_fields: req.separated_schema_fields, preserved_safe_fallbacks: req.preserved_safe_fallbacks, corrupted_previous_configs: EXPECTED_CORRUPTED_PREVIOUS_CONFIGS, previous_safe_config_preserved: true, schema_epoch: req.schema_epoch, schema_config_digest: req.schema_config_digest, receipt_digest: expected_binding ^ 0x2385_ACE0_5343_484d, }; self.committed_receipt = Some(receipt); Ok(receipt) } }