Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkgs/id/crates/id-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ use std::collections::BTreeSet;

use serde::{Deserialize, Serialize};

pub mod resolve;

pub use resolve::{LayerOverride, ResolutionInput, ResolveError, ResolvedLayers, resolve};

/// The only `ConfigDocument` schema version this crate accepts. The schema
/// file is `pkgs/id/config/id-config-v1.schema.json`; a document naming any
/// other version is rejected ([`ConfigError::UnsupportedSchemaVersion`])
Expand Down
288 changes: 288 additions & 0 deletions pkgs/id/crates/id-config/src/resolve.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
//! Layered mode and scoped behavior resolution (slice 9111-02, #10193).
//!
//! The resolver keeps configuration planes distinct while applying their
//! documented precedence: deployment defaults, the selected mode bundle,
//! tenant, peer, link, storage, and finally operation-scoped behavior. Lower
//! scopes may replace behavior in either direction, but authority and
//! resource ceilings remain hard boundaries.

use std::collections::BTreeMap;

use thiserror::Error;

use crate::{Behavior, CeilingOverride, Ceilings, Mode, ResolvedBehavior, SyncScope};

/// A behavior and optional resource narrowing supplied by one named scope.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LayerOverride {
/// Stable scope name used in provenance and diagnostics.
pub name: String,
/// Behavioral values supplied by this scope.
pub behavior: Behavior,
/// Optional narrowing of the current resource ceilings.
pub ceilings: CeilingOverride,
}

/// The ordered inputs to [`resolve`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolutionInput {
/// Deployment-wide behavioral defaults.
pub deployment: Behavior,
/// Named mode bundle applied after deployment defaults.
pub mode: Mode,
/// Tenant-scoped behavior, if selected.
pub tenant: Option<LayerOverride>,
/// Peer-scoped behavior, if selected.
pub peer: Option<LayerOverride>,
/// Link-scoped behavior, if selected.
pub link: Option<LayerOverride>,
/// Storage-scoped behavior, if selected.
pub storage: Option<LayerOverride>,
/// Operation-scoped behavior, if permitted.
pub operation: Option<LayerOverride>,
/// Hard resource ceiling from the validated configuration document.
pub resource_ceiling: Ceilings,
/// Sync scopes authorized by the owning tenant/authority plane.
pub authority_sync_scopes: Vec<SyncScope>,
}

/// The deterministic result of layered resolution.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedLayers {
/// Fully resolved behavior.
pub behavior: ResolvedBehavior,
/// Effective resource ceilings after scope narrowings.
pub ceilings: Ceilings,
/// Source of each resolved behavior field.
pub provenance: BTreeMap<String, String>,
}

/// Why layered resolution failed.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum ResolveError {
/// A scope name was empty.
#[error("scope name must not be empty")]
EmptyScope,
/// A scope attempted to widen a currently effective ceiling.
#[error("scope {scope} widens ceiling {field} to {attempted}, above current ceiling {ceiling}")]
CeilingWidened {
/// Scope that attempted the widening.
scope: String,
/// Ceiling field.
field: &'static str,
/// Requested value.
attempted: u64,
/// Current effective ceiling.
ceiling: u64,
},
/// A behavior value exceeds the effective resource ceiling.
#[error("scope {scope} sets {field} to {value}, above ceiling {ceiling}")]
ResourceExceeded {
/// Scope supplying the value.
scope: String,
/// Behavior field.
field: &'static str,
/// Requested value.
value: u64,
/// Effective ceiling.
ceiling: u64,
},
/// The final behavior is outside the owning authority plane.
#[error("scope {scope} sets {field}={value:?} outside the authority ceiling")]
AuthorityExceeded {
/// Last scope that supplied the value.
scope: String,
/// Behavior field.
field: &'static str,
/// Disallowed scope.
value: SyncScope,
},
/// No authority scopes were supplied.
#[error("authority must allow at least one sync scope")]
EmptyAuthority,
}

/// Resolve layered behavior in the fixed precedence order.
///
/// The mode bundle is applied after deployment defaults and before the named
/// scopes. Every scope is applied in the fixed plane order, never by caller
/// insertion order. Authority is checked on the final result because a mode
/// default may be replaced by a later authorized scope; resource ceilings are
/// checked at every layer so an over-ceiling value cannot be hidden by a later
/// narrowing override.
pub fn resolve(input: &ResolutionInput) -> Result<ResolvedLayers, ResolveError> {
if input.authority_sync_scopes.is_empty() {
return Err(ResolveError::EmptyAuthority);
}

let mut state = State {
behavior: Mode::SingleNode.defaults(),
ceilings: input.resource_ceiling,
provenance: BTreeMap::new(),
};
apply_behavior(&mut state, &input.deployment, "deployment")?;
apply_mode(&mut state, input.mode)?;
// Mode bundles are inherited defaults, not authority grants. Constrain
// their default to the narrowest authorized scope; later explicit scope
// overrides are still checked and rejected if unauthorized.
if !input
.authority_sync_scopes
.contains(&state.behavior.sync_scope)
{
state.behavior.sync_scope = if input.authority_sync_scopes.contains(&SyncScope::Metadata) {
SyncScope::Metadata
} else {
input.authority_sync_scopes[0]
};
state
.provenance
.insert("sync_scope".to_owned(), "authority:default".to_owned());
}
for layer in [
input.tenant.as_ref(),
input.peer.as_ref(),
input.link.as_ref(),
input.storage.as_ref(),
input.operation.as_ref(),
]
.into_iter()
.flatten()
{
apply_layer(&mut state, layer)?;
}

if !input
.authority_sync_scopes
.contains(&state.behavior.sync_scope)
Comment on lines +154 to +156

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Constrain inherited mode defaults to the authority ceiling

With deployment sync_scope=metadata, Mode::HostedTenant, authority allowing only Metadata, and no scoped behavior overrides, resolution returns AuthorityExceeded because the mode supplies Full. The “Configuration data model and precedence” section of doc/2026-11-13T03-40-00Z_design_id-atlas-summit/crosscut/versioned-design.md explicitly specifies that this combination resolves to metadata. Apply the authority restriction to inherited defaults while retaining rejection of unauthorized explicit overrides; otherwise metadata-only tenants cannot use this mode without redundantly supplying a behavior override.

Useful? React with 👍 / 👎.

{
let scope = state
.provenance
.get("sync_scope")
.cloned()
.unwrap_or_else(|| "unknown".to_owned());
return Err(ResolveError::AuthorityExceeded {
scope,
field: "sync_scope",
value: state.behavior.sync_scope,
});
}

Ok(ResolvedLayers {
behavior: state.behavior,
ceilings: state.ceilings,
provenance: state.provenance,
})
}

struct State {
behavior: ResolvedBehavior,
ceilings: Ceilings,
provenance: BTreeMap<String, String>,
}

fn apply_mode(state: &mut State, mode: Mode) -> Result<(), ResolveError> {
let source = format!("mode:{}", mode.as_str());
let defaults = mode.defaults();
state.behavior = defaults;
for field in ["max_hops", "sync_scope", "tier"] {
state.provenance.insert(field.to_owned(), source.clone());
}
ensure_resource(&state.behavior, &state.ceilings, source)
}

fn apply_layer(state: &mut State, layer: &LayerOverride) -> Result<(), ResolveError> {
if layer.name.trim().is_empty() {
return Err(ResolveError::EmptyScope);
}
narrow_ceilings(&mut state.ceilings, &layer.ceilings, &layer.name)?;
apply_behavior(state, &layer.behavior, &format!("override:{}", layer.name))
}

fn apply_behavior(
state: &mut State,
behavior: &Behavior,
source: &str,
) -> Result<(), ResolveError> {
if let Some(max_hops) = behavior.max_hops {
state.behavior.max_hops = max_hops;
state
.provenance
.insert("max_hops".to_owned(), source.to_owned());
}
if let Some(sync_scope) = behavior.sync_scope {
state.behavior.sync_scope = sync_scope;
state
.provenance
.insert("sync_scope".to_owned(), source.to_owned());
}
if let Some(tier) = behavior.tier {
state.behavior.tier = tier;
state
.provenance
.insert("tier".to_owned(), source.to_owned());
}
ensure_resource(&state.behavior, &state.ceilings, source.to_owned())
}

fn ensure_resource(
behavior: &ResolvedBehavior,
ceilings: &Ceilings,
scope: String,
) -> Result<(), ResolveError> {
if behavior.max_hops > ceilings.max_hops {
return Err(ResolveError::ResourceExceeded {
scope,
field: "max_hops",
value: u64::from(behavior.max_hops),
ceiling: u64::from(ceilings.max_hops),
});
}
Ok(())
}

fn narrow_ceilings(
current: &mut Ceilings,
requested: &CeilingOverride,
scope: &str,
) -> Result<(), ResolveError> {
check_narrowing(
scope,
"max_hops",
requested.max_hops.map(u64::from),
u64::from(current.max_hops),
)?;
check_narrowing(scope, "max_bytes", requested.max_bytes, current.max_bytes)?;
check_narrowing(
scope,
"max_time_ms",
requested.max_time_ms,
current.max_time_ms,
)?;
check_narrowing(
scope,
"max_fan_out",
requested.max_fan_out.map(u64::from),
u64::from(current.max_fan_out),
)?;
*current = current.narrowed_by(requested);
Ok(())
}

fn check_narrowing(
scope: &str,
field: &'static str,
requested: Option<u64>,
ceiling: u64,
) -> Result<(), ResolveError> {
if let Some(attempted) = requested {
if attempted > ceiling {
Comment on lines +277 to +278

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject zero-valued scoped resource ceilings

With a valid deployment ceiling, a layer specifying ceilings.max_bytes = Some(0) passes this check and resolves successfully with a zero-byte ceiling; the same applies to max_time_ms and max_fan_out. This violates the existing Ceilings contract that zero ceilings are rejected and the minimum: 1 constraints on scoped ceilings in pkgs/id/config/id-config-v1.schema.json. Since these layer overrides are supplied directly to the new resolver, validating the original configuration document does not validate them. Reject zero-valued narrowings before returning an effective configuration that forbids operations.

Useful? React with 👍 / 👎.

return Err(ResolveError::CeilingWidened {
scope: scope.to_owned(),
field,
attempted,
ceiling,
});
}
}
Ok(())
}
Loading