diff --git a/pkgs/id/crates/id-store/src/lib.rs b/pkgs/id/crates/id-store/src/lib.rs index 5e64c0436..91d5131a5 100644 --- a/pkgs/id/crates/id-store/src/lib.rs +++ b/pkgs/id/crates/id-store/src/lib.rs @@ -42,6 +42,7 @@ pub mod note_checkpoints; pub mod note_blocks; pub mod note_links; pub mod notes; +pub mod numeric_registry; pub mod policy; pub mod range; pub mod search; diff --git a/pkgs/id/crates/id-store/src/numeric_registry.rs b/pkgs/id/crates/id-store/src/numeric_registry.rs new file mode 100644 index 000000000..f6c000fd1 --- /dev/null +++ b/pkgs/id/crates/id-store/src/numeric_registry.rs @@ -0,0 +1,168 @@ +//! Numeric registry write validation (slice A9122-numeric-registry, issue +//! #10277; design A-9122.1, S4.64-S4.73). +//! +//! A numeric registry is a derived namespace over the array-address core. Its +//! write boundary admits only the numeric kinds named by its policy and uses +//! the numeric comparator's operational budget. The domain remains unbounded: +//! a large value is accepted whenever its encoded parts fit the configured +//! budget. +//! +//! This module deliberately does not own namespace generations or +//! authorization. Callers first admit a key here, then perform the namespace +//! mutation through [`crate::namespace_registry::RegistryState`] with its +//! existing [`crate::namespace_registry::RegistryAuthorizer`] seam. + +use std::fmt; + +use id_core::numeric_order; +use id_core::numeric_value::{NumericError, NumericLimits, NumericValue}; + +/// The version of this numeric write-policy contract. +pub const POLICY_VERSION: u64 = 1; + +/// The three stable [`NumericValue`] kind tags. +pub const ALL_KINDS: u8 = (1 << id_core::numeric_value::KIND_INTEGER) + | (1 << id_core::numeric_value::KIND_DECIMAL) + | (1 << id_core::numeric_value::KIND_BINARY_FLOAT); + +/// Namespace-specific numeric write policy. +/// +/// `allowed_kinds` is a bit mask over the stable numeric kind tags. The +/// `limits` field is operational only; it is never a semantic magnitude or +/// precision maximum. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct NumericNamespacePolicy { + /// Version of the policy contract. + pub version: u64, + /// Allowed [`NumericValue`] kinds, represented as `1 << kind_tag`. + pub allowed_kinds: u8, + /// Parser/comparator resource budget. + pub limits: NumericLimits, +} + +impl NumericNamespacePolicy { + /// A V3 ordered numeric policy admitting all three kinds. + #[must_use] + pub const fn ordered() -> Self { + Self { + version: POLICY_VERSION, + allowed_kinds: ALL_KINDS, + limits: NumericLimits { + max_magnitude_bytes: id_core::numeric_value::DEFAULT_MAX_MAGNITUDE_BYTES, + }, + } + } + + /// Creates a policy with an explicit kind mask and operational budget. + #[must_use] + pub const fn with_limits(allowed_kinds: u8, limits: NumericLimits) -> Self { + Self { + version: POLICY_VERSION, + allowed_kinds, + limits, + } + } + + fn allows(&self, value: &NumericValue) -> bool { + self.allowed_kinds & (1 << value.kind_tag()) != 0 + } +} + +/// A validated numeric key ready for an array-core write. +/// +/// The inner value is private so callers cannot construct a key without +/// passing through [`NumericRegistry::admit`]. Namespace generation and +/// authorization still belong to `RegistryState`. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct NumericKey(NumericValue); + +impl NumericKey { + /// The admitted numeric value. + #[must_use] + pub const fn value(&self) -> &NumericValue { + &self.0 + } +} + +/// Why a numeric namespace write was refused. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum NumericRegistryError { + /// The value's kind is not enabled by the namespace policy. + KindNotAllowed { + /// The stable kind tag that was presented. + kind: u8, + }, + /// The value exceeded an operational parser/comparator budget. + Numeric(NumericError), +} + +impl NumericRegistryError { + /// Stable error-envelope class. + #[must_use] + pub const fn code(&self) -> &'static str { + match self { + Self::KindNotAllowed { .. } => "kind_not_allowed", + Self::Numeric(error) => error.code(), + } + } +} + +impl fmt::Display for NumericRegistryError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::KindNotAllowed { kind } => write!(f, "numeric kind {kind} is not allowed"), + Self::Numeric(error) => write!(f, "numeric key rejected: {error}"), + } + } +} + +impl std::error::Error for NumericRegistryError {} + +impl From for NumericRegistryError { + fn from(error: NumericError) -> Self { + Self::Numeric(error) + } +} + +/// The numeric write boundary for one namespace policy. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct NumericRegistry { + policy: NumericNamespacePolicy, +} + +impl NumericRegistry { + /// Creates a validator for a namespace policy. + #[must_use] + pub const fn new(policy: NumericNamespacePolicy) -> Self { + Self { policy } + } + + /// The policy this validator applies. + #[must_use] + pub const fn policy(&self) -> NumericNamespacePolicy { + self.policy + } + + /// Admits a key for a namespace write. + /// + /// Kind validation happens before comparator work. The self-comparison + /// then exercises the exact comparator's budget checks without imposing a + /// semantic maximum on the numeric value. The caller must pass the + /// returned key to its array write and use `RegistryState` for the + /// authorized, versioned namespace mutation. + /// + /// # Errors + /// + /// [`NumericRegistryError::KindNotAllowed`] when the namespace policy + /// rejects the stable kind, or [`NumericRegistryError::Numeric`] when an + /// operational budget is exceeded. + pub fn admit(&self, value: NumericValue) -> Result { + if !self.policy.allows(&value) { + return Err(NumericRegistryError::KindNotAllowed { + kind: value.kind_tag(), + }); + } + numeric_order::compare_with(&value, &value, self.policy.limits)?; + Ok(NumericKey(value)) + } +} diff --git a/pkgs/id/crates/id-store/tests/numeric_registry.rs b/pkgs/id/crates/id-store/tests/numeric_registry.rs new file mode 100644 index 000000000..2879ad88e --- /dev/null +++ b/pkgs/id/crates/id-store/tests/numeric_registry.rs @@ -0,0 +1,94 @@ +//! Integration tests for the A9122 numeric registry write boundary. +//! +//! Oracle: a small independent policy procedure checks the allowed kind mask, +//! while the existing numeric comparator is the operational-budget oracle. +//! Registry generation and authorization remain exercised through the +//! `RegistryState` seam rather than being reimplemented in this test. + +#![allow(clippy::expect_used, clippy::unwrap_used)] + +use id_core::namespace_definition::{ + CoreKind, NamespaceDefinition, NamespaceId, RegistryId, Token, +}; +use id_core::numeric_value::{BigInt, NumericLimits, NumericValue}; +use id_store::namespace_registry::{AdminScopeAuthorizer, MutationContext, RegistryState}; +use id_store::numeric_registry::{NumericNamespacePolicy, NumericRegistry, NumericRegistryError}; + +fn definition(name: &str) -> NamespaceDefinition { + NamespaceDefinition { + namespace: NamespaceId::new(name).unwrap(), + core: CoreKind::Array, + handler: Token::new("handler", "numeric").unwrap(), + encoding_profile: Token::new("encoding", "numeric-v1").unwrap(), + read_policy: Token::new("read", "anyone").unwrap(), + write_policy: Token::new("write", "owner").unwrap(), + shorthand_policy: Token::new("shorthand", "none").unwrap(), + schema_version: 1, + } +} + +fn admin() -> MutationContext { + MutationContext::new("operator", "tenant-a", "registry-admin").unwrap() +} + +#[test] +fn rejects_a_kind_not_allowed_by_the_namespace_policy_at_admission() { + let policy = NumericNamespacePolicy::with_limits( + 1 << id_core::numeric_value::KIND_INTEGER, + NumericLimits::default(), + ); + let registry = NumericRegistry::new(policy); + let error = registry + .admit(NumericValue::decimal(&BigInt::from_i64(3), &BigInt::zero())) + .unwrap_err(); + assert_eq!(error, NumericRegistryError::KindNotAllowed { kind: 1 }); + assert_eq!(error.code(), "kind_not_allowed"); +} + +#[test] +fn configured_budget_is_resource_limit_not_numeric_semantics() { + let registry = NumericRegistry::new(NumericNamespacePolicy::with_limits( + id_store::numeric_registry::ALL_KINDS, + NumericLimits { + max_magnitude_bytes: 8, + }, + )); + let value = NumericValue::integer(BigInt::from_be_magnitude(false, &[0xff; 9])); + let error = registry.admit(value).unwrap_err(); + assert_eq!(error.code(), "resource_limit"); + assert!(matches!(error, NumericRegistryError::Numeric(_))); +} + +#[test] +fn large_valid_value_is_admitted_within_operational_budget() { + let registry = NumericRegistry::new(NumericNamespacePolicy::with_limits( + id_store::numeric_registry::ALL_KINDS, + NumericLimits { + max_magnitude_bytes: 8 * 1024, + }, + )); + let digits = "9".repeat(5_000); + let value = NumericValue::decimal( + &BigInt::parse(&digits).unwrap(), + &BigInt::parse("-400").unwrap(), + ); + let key = registry.admit(value.clone()).unwrap(); + assert_eq!(key.value(), &value); +} + +#[test] +fn admitted_key_does_not_bypass_versioned_registry_authorization() { + let validator = NumericRegistry::new(NumericNamespacePolicy::ordered()); + let key = validator + .admit(NumericValue::integer(BigInt::from_i64(7))) + .unwrap(); + assert_eq!(key.value().kind_tag(), id_core::numeric_value::KIND_INTEGER); + + let mut state = RegistryState::new(RegistryId::server("numeric").unwrap()); + let authorizer = AdminScopeAuthorizer::new("registry-admin"); + assert_eq!( + state.create(&authorizer, &admin(), 0, definition("numbers")), + Ok(1) + ); + assert_eq!(state.generation(), 1); +}