Skip to content

Commit 3a09e9d

Browse files
committed
fix(cross-ledger): use encode_iri_strict for IRI-to-Sid translation
`encode_iri` falls back to the EMPTY namespace (code 0) for IRIs whose prefix is not registered, so it always returns Some(...). Every cross-ledger materializer wrote code as if encode_iri returned None on miss — `if let Some` guards, `let Some(...) else { return Ok(empty) }`, `filter_map`, and the `(Some, Some, Some) = ... else { continue; }` schema-bundle pattern — all of which were unreachable, causing unregistered well-known IRIs to silently translate to EMPTY-Sids that match nothing in M's flakes. The observable effect is a silent governance downgrade rather than the loud failure or correct skip the surrounding code intended. Switch every cross_ledger snapshot.encode_iri(...) site to encode_iri_strict so the Option semantics the existing code is written against actually hold: - types.rs ConstraintsArtifactWire::translate_to_sids — drops unresolvable property IRIs as documented. - types.rs SchemaArtifactWire::translate_to_schema_bundle_flakes — drops whitelist triples whose s/p/o IRI is unknown to D, instead of admitting EMPTY-namespace Sids into the schema bundle. - policy_materializer — strict encode for f:AccessPolicy structural baseline scan; missing class IRI now correctly skips the scan. - shapes_materializer — strict encode for SHACL predicates and rdf:first/rdf:rest; absent predicates correctly skipped. - rules_materializer — strict encode for f:rule; the documented "no f:rule on M → empty rule set" early return is now reachable. - schema_materializer — strict encode for schema predicate / class IRIs and rdf:type; only registered terms drive PSOT/OPST scans. Hoist the duplicate `encode_system_iri` helper out of policy_materializer.rs and constraints_materializer.rs into cross_ledger/mod.rs as `pub(crate) fn encode_system_iri`, also switched to encode_iri_strict so a missing system IRI on a model ledger surfaces as TranslationFailed instead of silently degrading to an EMPTY-Sid that matches nothing. Update the SchemaArtifactWire docstring and WireObject::Ref doc to describe the actual (strict) encoding semantics.
1 parent 147a83b commit 3a09e9d

7 files changed

Lines changed: 53 additions & 64 deletions

File tree

‎fluree-db-api/src/cross_ledger/constraints_materializer.rs‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
//! values.
1313
1414
use super::types::{ConstraintsArtifactWire, WireOrigin};
15-
use super::CrossLedgerError;
15+
use super::{encode_system_iri, CrossLedgerError};
1616
use crate::Fluree;
17-
use fluree_db_core::{FlakeValue, IndexType, LedgerSnapshot, RangeMatch, RangeTest, Sid};
17+
use fluree_db_core::{FlakeValue, IndexType, RangeMatch, RangeTest};
1818
use fluree_vocab::config_iris;
1919

2020
/// Materialize the constraints graph at `graph_iri` in model
@@ -115,22 +115,3 @@ pub(super) async fn materialize_constraints(
115115
property_iris,
116116
})
117117
}
118-
119-
fn encode_system_iri(
120-
snapshot: &LedgerSnapshot,
121-
iri: &str,
122-
canonical_model_ledger_id: &str,
123-
graph_iri: &str,
124-
) -> Result<Sid, CrossLedgerError> {
125-
snapshot
126-
.encode_iri(iri)
127-
.ok_or_else(|| CrossLedgerError::TranslationFailed {
128-
ledger_id: canonical_model_ledger_id.to_string(),
129-
graph_iri: graph_iri.to_string(),
130-
detail: format!(
131-
"system IRI '{iri}' is not in the model ledger's namespace \
132-
map; this usually indicates the model ledger is corrupted \
133-
or did not initialize default namespaces"
134-
),
135-
})
136-
}

‎fluree-db-api/src/cross_ledger/mod.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,30 @@ pub(crate) fn resolve_selector_g_id(
5555
}
5656
Ok(snapshot.graph_registry.graph_id_for_iri(graph_iri))
5757
}
58+
59+
/// Encode a fixed system IRI (e.g. `rdf:type`, `f:allow`) against a
60+
/// model-ledger snapshot, returning a structured error if the IRI's
61+
/// namespace is not registered.
62+
///
63+
/// Uses `encode_iri_strict` so a missing well-known prefix surfaces as
64+
/// [`CrossLedgerError::TranslationFailed`] rather than silently
65+
/// falling back to an EMPTY-namespace Sid that would match nothing in
66+
/// M's flakes (silent governance downgrade).
67+
pub(crate) fn encode_system_iri(
68+
snapshot: &fluree_db_core::LedgerSnapshot,
69+
iri: &str,
70+
canonical_model_ledger_id: &str,
71+
graph_iri: &str,
72+
) -> Result<fluree_db_core::Sid, CrossLedgerError> {
73+
snapshot
74+
.encode_iri_strict(iri)
75+
.ok_or_else(|| CrossLedgerError::TranslationFailed {
76+
ledger_id: canonical_model_ledger_id.to_string(),
77+
graph_iri: graph_iri.to_string(),
78+
detail: format!(
79+
"system IRI '{iri}' is not in the model ledger's namespace map; \
80+
this usually indicates the model ledger is corrupted or did \
81+
not initialize default namespaces"
82+
),
83+
})
84+
}

‎fluree-db-api/src/cross_ledger/policy_materializer.rs‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
//! `TranslationFailed` (no silent drop; dropping a target would
4747
//! produce a structurally weaker policy than authored).
4848
49-
use super::CrossLedgerError;
49+
use super::{encode_system_iri, CrossLedgerError};
5050
use crate::Fluree;
5151
use fluree_db_core::{FlakeValue, IndexType, LedgerSnapshot, RangeMatch, RangeTest, Sid};
5252
use fluree_vocab::policy_iris;
@@ -159,7 +159,7 @@ pub(super) async fn materialize_policy_rules(
159159
// canonical class is the only structural baseline; custom-typed
160160
// policies still need an explicit effect predicate to be picked
161161
// up cross-ledger. That's a documented Phase 1a limitation.
162-
if let Some(access_policy_sid) = m_db.snapshot.encode_iri(policy_iris::ACCESS_POLICY) {
162+
if let Some(access_policy_sid) = m_db.snapshot.encode_iri_strict(policy_iris::ACCESS_POLICY) {
163163
let flakes = m_view
164164
.range(
165165
IndexType::Post,
@@ -242,25 +242,6 @@ fn rdf_type_iri() -> &'static str {
242242
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
243243
}
244244

245-
fn encode_system_iri(
246-
snapshot: &LedgerSnapshot,
247-
iri: &str,
248-
canonical_model_ledger_id: &str,
249-
graph_iri: &str,
250-
) -> Result<Sid, CrossLedgerError> {
251-
snapshot
252-
.encode_iri(iri)
253-
.ok_or_else(|| CrossLedgerError::TranslationFailed {
254-
ledger_id: canonical_model_ledger_id.to_string(),
255-
graph_iri: graph_iri.to_string(),
256-
detail: format!(
257-
"system IRI '{iri}' is not in the model ledger's namespace map; \
258-
this usually indicates the model ledger is corrupted or did \
259-
not initialize default namespaces"
260-
),
261-
})
262-
}
263-
264245
/// Read every `rdf:type` value for `subject` in the policy graph and
265246
/// decode each to its IRI form.
266247
async fn read_rdf_types(

‎fluree-db-api/src/cross_ledger/rules_materializer.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub(super) async fn materialize_rules(
5656
// No `f:rule` predicate in M's namespace map → M never authored
5757
// rules. Treat the same as an empty rule set so the caller's
5858
// downstream merge is a no-op.
59-
let Some(rule_pred_sid) = m_db.snapshot.encode_iri(RULE) else {
59+
let Some(rule_pred_sid) = m_db.snapshot.encode_iri_strict(RULE) else {
6060
return Ok(RulesArtifactWire {
6161
origin: WireOrigin {
6262
model_ledger_id: canonical_model_ledger_id.to_string(),

‎fluree-db-api/src/cross_ledger/schema_materializer.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ pub(super) async fn materialize_schema(
9292

9393
let schema_predicates: Vec<Sid> = schema_predicate_iris
9494
.iter()
95-
.filter_map(|iri| m_db.snapshot.encode_iri(iri))
95+
.filter_map(|iri| m_db.snapshot.encode_iri_strict(iri))
9696
.collect();
9797
let schema_classes: Vec<Sid> = schema_class_iris
9898
.iter()
99-
.filter_map(|iri| m_db.snapshot.encode_iri(iri))
99+
.filter_map(|iri| m_db.snapshot.encode_iri_strict(iri))
100100
.collect();
101-
let rdf_type_sid = m_db.snapshot.encode_iri(rdf::TYPE);
101+
let rdf_type_sid = m_db.snapshot.encode_iri_strict(rdf::TYPE);
102102

103103
// 4. Build M's view at the requested graph + t.
104104
let m_view =

‎fluree-db-api/src/cross_ledger/shapes_materializer.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ pub(super) async fn materialize_shapes(
9898

9999
let mut shacl_predicate_sids: Vec<Sid> = Vec::new();
100100
for name in shacl_predicate_names {
101-
if let Some(sid) = m_db.snapshot.encode_iri(&format!("{SHACL}{name}")) {
101+
if let Some(sid) = m_db.snapshot.encode_iri_strict(&format!("{SHACL}{name}")) {
102102
shacl_predicate_sids.push(sid);
103103
}
104104
}
105-
let rdf_first_sid = m_db.snapshot.encode_iri(&format!("{RDF}first"));
106-
let rdf_rest_sid = m_db.snapshot.encode_iri(&format!("{RDF}rest"));
105+
let rdf_first_sid = m_db.snapshot.encode_iri_strict(&format!("{RDF}first"));
106+
let rdf_rest_sid = m_db.snapshot.encode_iri_strict(&format!("{RDF}rest"));
107107

108108
let opts = RangeOptions::default().with_to_t(m_db.t);
109109
let mut triples: Vec<WireTriple> = Vec::new();

‎fluree-db-api/src/cross_ledger/types.rs‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl ConstraintsArtifactWire {
151151
) -> Vec<fluree_db_core::Sid> {
152152
self.property_iris
153153
.iter()
154-
.filter_map(|iri| snapshot.encode_iri(iri))
154+
.filter_map(|iri| snapshot.encode_iri_strict(iri))
155155
.collect()
156156
}
157157
}
@@ -162,10 +162,10 @@ impl ConstraintsArtifactWire {
162162
/// owl:equivalentClass, owl:imports, rdf:type for owl:Class/etc.,
163163
/// plus the rest of the schema-bundle whitelist) in IRI form. The
164164
/// translator on D encodes each IRI against D's snapshot and builds
165-
/// a `SchemaBundleFlakes` from the result. IRIs that fail to encode
166-
/// drop their triples — same semantics as the same-ledger
167-
/// `build_schema_bundle_flakes` flow, where `encode_iri` returning
168-
/// `None` for an unseen IRI contributes nothing.
165+
/// a `SchemaBundleFlakes` from the result. Triples whose subject,
166+
/// predicate, or object IRI has no registered namespace on D are
167+
/// dropped — D has no instance data under those IRIs, so the missing
168+
/// axiom can't fire on data D doesn't have.
169169
///
170170
/// Phase 1b-a constraint: the materializer projects only Ref-valued
171171
/// objects (every predicate / class in the schema whitelist is
@@ -207,8 +207,8 @@ pub struct WireTriple {
207207
/// as if the constraint weren't authored.
208208
#[derive(Debug, Clone)]
209209
pub enum WireObject {
210-
/// IRI reference; encoded via `snapshot.encode_iri` at the data
211-
/// ledger to produce a `FlakeValue::Ref(Sid)`.
210+
/// IRI reference; encoded via `snapshot.encode_iri_strict` at the
211+
/// data ledger to produce a `FlakeValue::Ref(Sid)`.
212212
Ref(String),
213213
/// Literal value in its canonical lexical form, paired with the
214214
/// datatype IRI and optional language tag. The datatype IRI is
@@ -234,11 +234,11 @@ impl SchemaArtifactWire {
234234
/// Translate to a Sid-form `SchemaBundleFlakes` against the
235235
/// data ledger's snapshot.
236236
///
237-
/// Each triple's IRIs are encoded via `snapshot.encode_iri`.
238-
/// Failed encodings drop the triple — D has no data of those
239-
/// IRIs so the missing axiom can't fire on instance data D
240-
/// doesn't have, matching same-ledger semantics. The
241-
/// downstream whitelist check in
237+
/// Each triple's IRIs are encoded via `snapshot.encode_iri_strict`.
238+
/// Triples whose subject, predicate, or object IRI has no
239+
/// registered namespace on D are dropped — D has no instance
240+
/// data under those IRIs so the missing axiom can't fire on data
241+
/// D doesn't have. The downstream whitelist check in
242242
/// `SchemaBundleFlakes::from_collected_schema_triples` is the
243243
/// canonical filter; if a future cross-ledger producer emits
244244
/// non-whitelist triples they'll drop there.
@@ -262,9 +262,9 @@ impl SchemaArtifactWire {
262262
continue;
263263
};
264264
let (Some(s_sid), Some(p_sid), Some(o_sid)) = (
265-
snapshot.encode_iri(&t.s),
266-
snapshot.encode_iri(&t.p),
267-
snapshot.encode_iri(o_iri),
265+
snapshot.encode_iri_strict(&t.s),
266+
snapshot.encode_iri_strict(&t.p),
267+
snapshot.encode_iri_strict(o_iri),
268268
) else {
269269
continue;
270270
};

0 commit comments

Comments
 (0)