Skip to content
Merged
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
54 changes: 52 additions & 2 deletions src/explain/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use alloc::vec::Vec;
/// redacted.
pub fn complete_rule_report() -> ExplanationReport {
let role = ConditionExplanation {
completed_scope: None,
call_uses: None,
site_id: 0,
owner: owner("data.test.allow", 0),
outcome: Outcome::Failure,
Expand All @@ -28,6 +30,8 @@ pub fn complete_rule_report() -> ExplanationReport {
binding: None,
};
let token = ConditionExplanation {
completed_scope: None,
call_uses: None,
site_id: 1,
owner: owner("data.test.allow", 0),
outcome: Outcome::Success,
Expand All @@ -37,6 +41,7 @@ pub fn complete_rule_report() -> ExplanationReport {
binding: None,
};
ExplanationReport {
query_conditions: None,
query: query("data.test.allow"),
query_result: Value::from(true),
rules: vec![RuleExplanation {
Expand All @@ -51,6 +56,9 @@ pub fn complete_rule_report() -> ExplanationReport {
definition(1, 1, Outcome::Success, 7, vec![token]),
],
results: None,
attempts: None,
scopes: None,
calls: None,
}],
checks: vec![
ConditionSite {
Expand All @@ -76,12 +84,16 @@ pub fn complete_rule_report() -> ExplanationReport {
pub fn partial_set_report() -> ExplanationReport {
let names: BTreeSet<Value> = [Value::from("bob")].into_iter().collect();
let in_users = ConditionExplanation {
completed_scope: None,
call_uses: None,
site_id: 0,
owner: owner("data.test.deny", 0),
outcome: Outcome::Success,
left: None,
right: None,
witness: Some(LoopWitness {
key_binding: None,
value_binding: None,
iterations: 2,
successes: 1,
sample_key: Some(known(Value::from(1_u64), None)),
Expand All @@ -90,6 +102,7 @@ pub fn partial_set_report() -> ExplanationReport {
binding: Some(String::from("name")),
};
ExplanationReport {
query_conditions: None,
query: query("data.test.deny"),
query_result: Value::from(names.clone()),
rules: vec![RuleExplanation {
Expand All @@ -101,6 +114,9 @@ pub fn partial_set_report() -> ExplanationReport {
emissions: vec![emission(None, Value::from("bob"), vec![in_users.clone()])],
definitions: vec![definition(0, 0, Outcome::Success, 3, vec![in_users])],
results: None,
attempts: None,
scopes: None,
calls: None,
}],
checks: vec![ConditionSite {
site_id: 0,
Expand All @@ -120,12 +136,16 @@ pub fn partial_object_report() -> ExplanationReport {
.collect();
let labels = Value::from(labels);
let in_tags = ConditionExplanation {
completed_scope: None,
call_uses: None,
site_id: 0,
owner: owner("data.test.labels", 0),
outcome: Outcome::Success,
left: None,
right: None,
witness: Some(LoopWitness {
key_binding: None,
value_binding: None,
iterations: 1,
successes: 1,
sample_key: Some(known(Value::from("env"), None)),
Expand All @@ -134,6 +154,7 @@ pub fn partial_object_report() -> ExplanationReport {
binding: None,
};
ExplanationReport {
query_conditions: None,
query: query("data.test.labels"),
query_result: labels.clone(),
rules: vec![RuleExplanation {
Expand All @@ -149,6 +170,9 @@ pub fn partial_object_report() -> ExplanationReport {
)],
definitions: vec![definition(0, 0, Outcome::Success, 3, vec![in_tags])],
results: None,
attempts: None,
scopes: None,
calls: None,
}],
checks: vec![ConditionSite {
site_id: 0,
Expand Down Expand Up @@ -185,9 +209,15 @@ pub fn attributed_report() -> ExplanationReport {
count: Some(Count::Exact(count)),
};
let proof = Derivation {
eligibility: None,
call_uses: None,
definition: 0,
body: 0,
attempt: None,
conditionality: None,
conditions: vec![ConditionExplanation {
completed_scope: None,
call_uses: None,
site_id: 2,
owner: owner("data.test.deny", 0),
outcome: Outcome::Success,
Expand All @@ -196,7 +226,7 @@ pub fn attributed_report() -> ExplanationReport {
witness: None,
binding: None,
}],
bindings: vec![
bindings: Some(vec![
IterationBinding {
site_id: 0,
collection: 0,
Expand All @@ -209,11 +239,12 @@ pub fn attributed_report() -> ExplanationReport {
key: ReportedValue::Known(Value::from(0_u64)),
value: ReportedValue::Known(Value::from("public")),
},
],
]),
};
let result = ResultExplanation {
key: None,
value: ReportedValue::Known(Value::from("public")),
conditionality: None,
derivations_total: Count::Exact(2),
distinct_elements: vec![
distinct(0, "input.resources[_]", 0, 2),
Expand All @@ -230,6 +261,7 @@ pub fn attributed_report() -> ExplanationReport {
operator: None,
};
ExplanationReport {
query_conditions: None,
query: query("data.test.deny"),
query_result: Value::from(public.clone()),
rules: vec![RuleExplanation {
Expand All @@ -241,6 +273,9 @@ pub fn attributed_report() -> ExplanationReport {
emissions: Vec::new(),
definitions: Vec::new(),
results: Some(vec![result]),
attempts: None,
scopes: None,
calls: None,
}],
checks: vec![
iteration(0, "some r in input.resources"),
Expand All @@ -265,13 +300,15 @@ pub fn conflict_report() -> ExplanationReport {
let mut target = query("data.test.allow");
target.with_overrides = true;
ExplanationReport {
query_conditions: None,
query: target,
query_result: Value::Undefined,
rules: vec![RuleExplanation {
name: String::from("data.test.allow"),
call_instance: 0,
kind: RuleKind::Complete,
outcome: RuleOutcome::Conflict {
key: None,
values: vec![
ReportedValue::Known(Value::from(true)),
ReportedValue::Known(Value::from(false)),
Expand All @@ -281,6 +318,9 @@ pub fn conflict_report() -> ExplanationReport {
emissions: Vec::new(),
definitions: Vec::new(),
results: None,
attempts: None,
scopes: None,
calls: None,
}],
checks: Vec::new(),
}
Expand Down Expand Up @@ -546,23 +586,31 @@ pub fn partial_eval_unsupported_result() -> PartialEvalResult {

pub fn known(value: Value, path: Option<&str>) -> Operand {
Operand {
parameter_origins: None,
call_uses: None,
value: ReportedValue::Known(value),
path: path.map(String::from),
concrete_path: ConcretePath::NotCaptured,
origins: None,
}
}

pub fn redacted(path: &str) -> Operand {
Operand {
parameter_origins: None,
call_uses: None,
value: ReportedValue::Redacted,
path: Some(String::from(path)),
concrete_path: ConcretePath::NotCaptured,
origins: None,
}
}

/// A query naming `rule`, whose top-level evaluation is call instance 0.
pub fn query(rule: &str) -> QueryTarget {
QueryTarget {
calls: None,
scopes: None,
entry_point: String::from(rule),
rule: Some(String::from(rule)),
call_instance: Some(0),
Expand All @@ -574,6 +622,8 @@ pub fn owner(rule: &str, call_instance: u64) -> ConditionOwner {
ConditionOwner {
rule: String::from(rule),
call_instance,
attempt: None,
scope: None,
}
}

Expand Down
58 changes: 49 additions & 9 deletions src/explain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
//! location, kind, operand references and owning rule of each check.
//! - [`ExplanationRecorder`] receives the RVM's evaluation events.
//!
//! The report types are `#[non_exhaustive]` so that fields and variants can
//! be added without breaking callers. They implement [`serde::Serialize`];
//! The report types are `#[non_exhaustive]` to permit new struct fields and
//! enum variants. This does not protect changes to existing field types or
//! fields of an exhaustive enum variant. They implement [`serde::Serialize`];
//! JSON is produced only by the language bindings. Consumers of an
//! [`ExplanationReport`] should accept unknown fields and enum values.
//! Consumers that enforce a [`PartialEvalResult`] must instead fail closed:
Expand Down Expand Up @@ -62,11 +63,49 @@
//! ([`DistinctElements`]). Concrete operand paths are independently opt-in;
//! [`ConcretePath`] distinguishes not captured, unrepresentable, and a
//! captured absence of a direct path.
//! [`AttemptExplanation`] and call-local [`ScopeExplanation`] distinguish
//! producing attempts from failed alternatives. [`CallUse`] preserves caller
//! syntactic context for cached results used under different negation scopes,
//! not logical influence through intervening operations. Query-owned uses and
//! scopes live in [`QueryTarget`]; conditions and proofs can identify their
//! contributing use edges without changing the callee's evaluation owner.
//! Query checks can retain binding names and loop witnesses. Standalone
//! comprehension sites use [`ConditionKind::Comprehension`], not `Binding`.
//! Use paths in rule proofs start at that proof's owning invocation, never
//! with an external query/caller prefix; query-owned checks start at the query.
//! [`Operand::call_uses`] distinguishes which calls contributed to each
//! operand even when sibling calls return equal values.
//! Caller-owned [`CallUse::arguments`] preserve incoming argument provenance;
//! callee binding/body operands identify parameter projections without importing
//! caller paths into callee proofs. Witness key/value binding names preserve
//! both roles of a two-variable iteration without parsing source text.
//! Per-result and per-proof [`Conditionality`] is unknown when not captured;
//! bounded proofs cannot establish that omitted evidence was concrete.
//! It describes producing contributions, not a guarantee that evaluation
//! succeeds: assumption-dependent conflicts or errors remain authoritative.
//! Computed operands can name [`CausalOrigins`] without claiming that their
//! input sources held the computed value.
//!
//! A report for a successful evaluation is complete: it explains every rule
//! within the selected scope. A report accompanying a failed evaluation
//! covers rules only up to the error.
//! Signalling a report that was cut short will come with bounded tracing.
//!
//! # WIP contract migration
//!
//! This contract update requires Rust callers to wrap captured
//! [`Derivation::bindings`] in `Some`, using `None` when capture was unavailable.
//! [`RuleOutcome::Conflict`] constructions now supply `key: None` when no
//! object key was captured; exact variant patterns must account for `key`.
//! These are source-breaking changes despite the outer `non_exhaustive`.
//! Direct constructors also initialize optional query witness/binding and
//! operand-use metadata, using `None` when uncaptured.
//! They also initialize optional witness roles, call arguments, parameter
//! origins and proof eligibility checks. `QueryTarget` retains `Eq`.
//! JSON schema version 2 is retained: omitted bindings remain unknown, including
//! older reports that omitted either empty or uncaptured bindings. Only an
//! explicit empty array certifies no result-producing iteration bindings;
//! nested-scope loop evidence can still contribute through conditions.

mod error;
mod provenance;
Expand All @@ -87,12 +126,13 @@ pub use recorder::{
NotCapturedEvent, UncapturedConstruct,
};
pub use report::{
ConcretePath, ConditionExplanation, ConditionKind, ConditionOwner, ConditionSite, Count,
DefinitionExplanation, Derivation, DistinctElements, EmissionExplanation, ExplanationReport,
IterationBinding, LoopWitness, Operand, Operator, Outcome, PartialEvalResult, PathRoot,
PathSegment, QueryTarget, ReportedValue, ResidualAlternativeStatus, ResidualArgument,
ResidualCall, ResidualCondition, ResidualKind, ResidualQuery, ResidualStatus,
ResidualUnresolved, ResidualUnresolvedReason, ResultExplanation, RuleExplanation, RuleKind,
RuleOutcome, SourceLocation,
AttemptExplanation, CallUse, CausalOrigins, ConcretePath, ConditionExplanation, ConditionKind,
ConditionOwner, ConditionSite, Conditionality, Count, DefinitionExplanation, Derivation,
DistinctElements, EmissionExplanation, ExplanationReport, IterationBinding, LoopWitness,
Operand, Operator, Outcome, ParameterOrigin, PartialEvalResult, PathRoot, PathSegment,
QueryConditionExplanation, QueryScopeExplanation, QueryTarget, ReportedValue,
ResidualAlternativeStatus, ResidualArgument, ResidualCall, ResidualCondition, ResidualKind,
ResidualQuery, ResidualStatus, ResidualUnresolved, ResidualUnresolvedReason, ResultExplanation,
RuleExplanation, RuleKind, RuleOutcome, ScopeExplanation, ScopeKind, SourceLocation,
};
pub use settings::{ConditionMode, ExplanationScope, ExplanationSettings, ValueMode};
Loading
Loading