sim-incremental-core 0.5.1

Dependency-light incremental query graph with memo cutoff and bounded snapshots.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use std::collections::{BTreeMap, BTreeSet};

use sim_kernel::{ContentId, Datum, Symbol};

use super::{
    ConclusionId, Explanation, FactId, FederatedClosure,
    assay_repair::{ProjectionRepairItem, ProjectionRepairSet},
};

const REQUIRED_DELTAS: [&str; 6] = ["D1", "D2", "D3", "D4", "D5", "D6"];

/// Semantic class of one controlled stage-one delta.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ControlledDeltaClass {
    /// An identical semantic input batch.
    NoSemanticChange,
    /// One owner's private implementation changes.
    PrivateImplementation,
    /// A declared public API changes.
    PublicApi,
    /// Three public heads and their control-plane pins change together.
    PublicHeadsAndPins,
    /// Proof implementation and checker implementation identities change.
    ProofAndCheckerImplementation,
    /// Display or ordering changes without changing semantics.
    PresentationOnly,
}

/// One controlled input delta. It predicts work but never executes proof.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ControlledDelta {
    /// Stable assay row (`D1` through `D6`).
    pub id: String,
    /// Semantic class under test.
    pub class: ControlledDeltaClass,
    /// Exact changed semantic facts; empty for semantic no-ops.
    pub changed_facts: BTreeSet<FactId>,
    /// Maximum journal revision movement produced by normalization.
    pub revision_delta: u64,
    /// Whether the prediction emitted carried execution state.
    pub carried_state: bool,
}

impl ControlledDelta {
    /// Constructs a checked controlled delta.
    pub fn new(
        id: impl Into<String>,
        class: ControlledDeltaClass,
        changed_facts: impl IntoIterator<Item = FactId>,
        revision_delta: u64,
        carried_state: bool,
    ) -> Result<Self, AssayError> {
        let id = id.into();
        if id.trim().is_empty() {
            return Err(AssayError::InvalidDeltaId);
        }
        let changed_facts = changed_facts.into_iter().collect::<BTreeSet<_>>();
        if matches!(
            class,
            ControlledDeltaClass::NoSemanticChange | ControlledDeltaClass::PresentationOnly
        ) && !changed_facts.is_empty()
        {
            return Err(AssayError::SemanticNoOpCarriesChangedFacts(id));
        }
        Ok(Self {
            id,
            class,
            changed_facts,
            revision_delta,
            carried_state,
        })
    }
}

/// One independently authored expected affected set.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExpectedClosure {
    /// Controlled delta identity.
    pub delta: String,
    /// Declared-conclusion denominator used for every percentage.
    pub denominator: usize,
    /// Maximum affected conclusions admitted by the assay.
    pub max_affected: usize,
    /// Maximum semantic journal revision movement.
    pub max_revision_delta: u64,
    /// Whether carried execution state is allowed.
    pub carried_state: bool,
    /// Exact independently expected affected conclusions.
    pub affected: BTreeSet<ConclusionId>,
}

/// Frozen independently authored stage-one oracle.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExpectedClosureSet {
    source: ContentId,
    id: ContentId,
    rows: BTreeMap<String, ExpectedClosure>,
}

impl ExpectedClosureSet {
    /// Freezes six unique D1-D6 rows under the exact authored-source identity.
    pub fn freeze(
        source: ContentId,
        rows: impl IntoIterator<Item = ExpectedClosure>,
    ) -> Result<Self, AssayError> {
        let mut canonical = BTreeMap::new();
        for row in rows {
            if canonical.insert(row.delta.clone(), row).is_some() {
                return Err(AssayError::DuplicateDelta);
            }
        }
        let actual = canonical.keys().map(String::as_str).collect::<Vec<_>>();
        if actual != REQUIRED_DELTAS {
            return Err(AssayError::IncompleteDeltaSet(actual.join(",")));
        }
        let denominator = canonical["D1"].denominator;
        for row in canonical.values() {
            if row.denominator == 0
                || row.denominator != denominator
                || row.max_affected > row.denominator
                || row.affected.len() > row.max_affected
            {
                return Err(AssayError::InvalidExpectedClosure {
                    delta: row.delta.clone(),
                    reason: ExpectedClosureViolation::InconsistentDenominatorOrCeiling,
                });
            }
        }
        let d1 = &canonical["D1"];
        if !d1.affected.is_empty()
            || d1.max_affected != 0
            || d1.max_revision_delta != 0
            || d1.carried_state
        {
            return Err(AssayError::InvalidExpectedClosure {
                delta: "D1".into(),
                reason: ExpectedClosureViolation::NoChangeContract,
            });
        }
        let d2 = &canonical["D2"];
        if unaffected_basis_points(d2.denominator, d2.affected.len()) < 9_500 {
            return Err(AssayError::InvalidExpectedClosure {
                delta: "D2".into(),
                reason: ExpectedClosureViolation::UnaffectedFloor,
            });
        }
        let d4 = &canonical["D4"];
        if unaffected_basis_points(d4.denominator, d4.affected.len()) < 7_000 {
            return Err(AssayError::InvalidExpectedClosure {
                delta: "D4".into(),
                reason: ExpectedClosureViolation::UnaffectedFloor,
            });
        }
        let d6 = &canonical["D6"];
        if !d6.affected.is_empty()
            || d6.max_affected != 0
            || d6.max_revision_delta > 1
            || d6.carried_state
        {
            return Err(AssayError::InvalidExpectedClosure {
                delta: "D6".into(),
                reason: ExpectedClosureViolation::PresentationOnlyContract,
            });
        }
        let datum = Datum::Node {
            tag: Symbol::qualified("projection", "expected-closure-set-v1"),
            fields: vec![
                (Symbol::new("source"), content_id_datum(&source)),
                (
                    Symbol::new("rows"),
                    Datum::Vector(canonical.values().map(expected_datum).collect()),
                ),
            ],
        };
        let id = datum
            .content_id()
            .map_err(|error| AssayError::Canonical(error.to_string()))?;
        Ok(Self {
            source,
            id,
            rows: canonical,
        })
    }

    /// Returns the exact authored-source identity.
    #[must_use]
    pub fn source(&self) -> &ContentId {
        &self.source
    }

    /// Returns the canonical frozen-oracle identity.
    #[must_use]
    pub fn id(&self) -> &ContentId {
        &self.id
    }
}

/// Prediction-only work counters. There is deliberately no proof result field.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PredictedWork {
    /// Number of planner decisions emitted.
    pub decisions: usize,
    /// Number of semantic changed facts consumed.
    pub changed_facts: usize,
    /// Number of exact explanation paths emitted.
    pub explanations: usize,
    /// Number of semantic journal records predicted.
    pub journal_records: usize,
    /// Number of work items a later planner would start.
    pub planned_starts: usize,
}

/// Complete stage-one prediction for one controlled delta.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PredictedClosureReport {
    /// Delta identity.
    pub delta: String,
    /// Exact predicted affected set.
    pub affected: BTreeSet<ConclusionId>,
    /// Exact predicted unaffected set.
    pub unaffected: BTreeSet<ConclusionId>,
    /// Explicit declared-conclusion denominator.
    pub denominator: usize,
    /// Integer basis points of conclusions predicted unaffected.
    pub unaffected_basis_points: u16,
    /// Predicted semantic journal revision movement.
    pub revision_delta: u64,
    /// Whether the prediction emitted carried execution state.
    pub carried_state: bool,
    /// Exact causal explanation paths.
    pub explanations: Vec<Explanation>,
    /// Prediction-only counters.
    pub work: PredictedWork,
}

/// Closed stage-one contracts that can require projection repair.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum AssayContract {
    /// A controlled delta used the wrong semantic class.
    DeltaClass,
    /// A changed fact was absent from the sealed fact universe.
    UnknownChangedFact,
    /// The sealed graph disagreed with the declared denominator.
    Denominator,
    /// The prediction exceeded its affected-conclusion ceiling.
    AffectedCeiling,
    /// The prediction exceeded its journal revision ceiling.
    RevisionDelta,
    /// Carried-state emission disagreed with the oracle.
    CarriedState,
    /// D1 did not leave every conclusion unaffected.
    D1UnaffectedFloor,
    /// D2 left less than 95 percent of conclusions unaffected.
    D2UnaffectedFloor,
    /// D4 left less than 70 percent of conclusions unaffected.
    D4UnaffectedFloor,
    /// D6 emitted carried state.
    D6CarriedState,
    /// A semantic no-op predicted journal or execution work.
    SemanticNoOpWork,
}

/// One assay row either matches its frozen oracle or enters bounded repair.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AssayOutcome {
    /// Exact expected closure and every row contract passed.
    Passed(PredictedClosureReport),
    /// The report is retained with the canonical bounded repair set.
    Repair {
        /// Failed prediction.
        report: PredictedClosureReport,
        /// Exact locations to repair.
        repairs: ProjectionRepairSet,
    },
}

/// Qualification token proving all six stage-one deltas passed together.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StageOneQualification {
    /// Frozen oracle identity.
    pub oracle: ContentId,
    /// Six passing reports in D1-D6 order.
    pub reports: Vec<PredictedClosureReport>,
}

/// Pure prediction-only assay over a sealed federated closure.
pub struct PredictedClosureAssay<'a> {
    closure: &'a FederatedClosure,
}

impl<'a> PredictedClosureAssay<'a> {
    /// Binds the sealed projection closure under test.
    #[must_use]
    pub const fn new(closure: &'a FederatedClosure) -> Self {
        Self { closure }
    }

    /// Predicts one row and compares it with the independently frozen oracle.
    pub fn predict(
        &self,
        delta: &ControlledDelta,
        expected: &ExpectedClosureSet,
    ) -> Result<AssayOutcome, AssayError> {
        let oracle = expected
            .rows
            .get(&delta.id)
            .ok_or_else(|| AssayError::UnknownDelta(delta.id.clone()))?;
        let all = self.closure.conclusions().cloned().collect::<BTreeSet<_>>();
        let affected = self
            .closure
            .affected(delta.changed_facts.iter().cloned())
            .into_iter()
            .collect::<BTreeSet<_>>();
        let unaffected = all.difference(&affected).cloned().collect::<BTreeSet<_>>();
        let explanations = affected
            .iter()
            .flat_map(|conclusion| {
                delta
                    .changed_facts
                    .iter()
                    .filter_map(move |fact| self.closure.explain(conclusion, fact).ok())
            })
            .collect::<Vec<_>>();
        let semantic = !delta.changed_facts.is_empty();
        let work = PredictedWork {
            decisions: affected.len(),
            changed_facts: delta.changed_facts.len(),
            explanations: explanations.len(),
            journal_records: usize::from(semantic) * delta.changed_facts.len(),
            planned_starts: affected.len(),
        };
        let basis_points = unaffected_basis_points(all.len(), affected.len());
        let report = PredictedClosureReport {
            delta: delta.id.clone(),
            affected: affected.clone(),
            unaffected,
            denominator: all.len(),
            unaffected_basis_points: basis_points,
            revision_delta: delta.revision_delta,
            carried_state: delta.carried_state,
            explanations,
            work,
        };
        let mut repairs = BTreeSet::new();
        if expected_delta_class(&delta.id) != Some(delta.class) {
            repairs.insert(ProjectionRepairItem::Contract(AssayContract::DeltaClass));
        }
        if delta
            .changed_facts
            .iter()
            .any(|fact| !self.closure.contains_fact(fact))
        {
            repairs.insert(ProjectionRepairItem::Contract(
                AssayContract::UnknownChangedFact,
            ));
        }
        if oracle.denominator != all.len() {
            repairs.insert(ProjectionRepairItem::Contract(AssayContract::Denominator));
        }
        for conclusion in affected.difference(&oracle.affected) {
            for fact in &delta.changed_facts {
                if self.closure.depends_on(conclusion, fact) {
                    repairs.insert(ProjectionRepairItem::GraphEdge {
                        conclusion: conclusion.clone(),
                        fact: fact.clone(),
                    });
                }
            }
        }
        for conclusion in oracle.affected.difference(&affected) {
            repairs.insert(ProjectionRepairItem::Declaration(conclusion.clone()));
        }
        if affected.len() > oracle.max_affected {
            repairs.insert(ProjectionRepairItem::Contract(
                AssayContract::AffectedCeiling,
            ));
        }
        if delta.revision_delta > oracle.max_revision_delta {
            repairs.insert(ProjectionRepairItem::Contract(AssayContract::RevisionDelta));
        }
        if delta.carried_state != oracle.carried_state {
            repairs.insert(ProjectionRepairItem::Contract(AssayContract::CarriedState));
        }
        match delta.id.as_str() {
            "D1" if report.unaffected_basis_points != 10_000 => {
                repairs.insert(ProjectionRepairItem::Contract(
                    AssayContract::D1UnaffectedFloor,
                ));
            }
            "D2" if report.unaffected_basis_points < 9_500 => {
                repairs.insert(ProjectionRepairItem::Contract(
                    AssayContract::D2UnaffectedFloor,
                ));
            }
            "D4" if report.unaffected_basis_points < 7_000 => {
                repairs.insert(ProjectionRepairItem::Contract(
                    AssayContract::D4UnaffectedFloor,
                ));
            }
            "D6" if report.carried_state => {
                repairs.insert(ProjectionRepairItem::Contract(
                    AssayContract::D6CarriedState,
                ));
            }
            _ => {}
        }
        if !semantic && (report.work.planned_starts != 0 || report.work.journal_records != 0) {
            repairs.insert(ProjectionRepairItem::Contract(
                AssayContract::SemanticNoOpWork,
            ));
        }
        if repairs.is_empty() {
            Ok(AssayOutcome::Passed(report))
        } else {
            Ok(AssayOutcome::Repair {
                report,
                repairs: ProjectionRepairSet::new(&delta.id, repairs)?,
            })
        }
    }

    /// Qualifies exactly D1-D6 together; any repair prevents the token.
    pub fn qualify(
        &self,
        deltas: &[ControlledDelta],
        expected: &ExpectedClosureSet,
    ) -> Result<StageOneQualification, AssayError> {
        let actual = deltas
            .iter()
            .map(|delta| delta.id.as_str())
            .collect::<Vec<_>>();
        if actual != REQUIRED_DELTAS {
            return Err(AssayError::IncompleteDeltaSet(actual.join(",")));
        }
        let mut reports = Vec::with_capacity(REQUIRED_DELTAS.len());
        for delta in deltas {
            match self.predict(delta, expected)? {
                AssayOutcome::Passed(report) => reports.push(report),
                AssayOutcome::Repair { repairs, .. } => {
                    return Err(AssayError::RepairRequired(repairs));
                }
            }
        }
        Ok(StageOneQualification {
            oracle: expected.id.clone(),
            reports,
        })
    }
}

/// Invalid oracle, delta, graph, or failed stage-one qualification.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AssayError {
    /// Delta id is empty.
    InvalidDeltaId,
    /// Two expected rows name one delta.
    DuplicateDelta,
    /// Exact D1-D6 row set or ordering is absent.
    IncompleteDeltaSet(String),
    /// Requested delta is absent from the frozen oracle.
    UnknownDelta(String),
    /// An expected row contradicts the stage-one measurement contract.
    InvalidExpectedClosure {
        /// Invalid controlled delta.
        delta: String,
        /// Stable reason code.
        reason: ExpectedClosureViolation,
    },
    /// A semantic no-op declared changed semantic facts.
    SemanticNoOpCarriesChangedFacts(String),
    /// Canonical evidence construction failed.
    Canonical(String),
    /// At least one exact prediction requires repair.
    RepairRequired(ProjectionRepairSet),
}

/// Structural reason an independently authored oracle cannot be frozen.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ExpectedClosureViolation {
    /// Denominators differ, are zero, or conflict with affected ceilings.
    InconsistentDenominatorOrCeiling,
    /// D1 does not describe a complete semantic no-op.
    NoChangeContract,
    /// D2 or D4 violates its required unaffected percentage.
    UnaffectedFloor,
    /// D6 predicts semantic work, excessive revision, or carried state.
    PresentationOnlyContract,
}

impl std::fmt::Display for AssayError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(formatter, "{self:?}")
    }
}

impl std::error::Error for AssayError {}

fn content_id_datum(id: &ContentId) -> Datum {
    Datum::Node {
        tag: Symbol::qualified("core", "content-id-v1"),
        fields: vec![
            (
                Symbol::new("algorithm"),
                Datum::Symbol(id.algorithm.clone()),
            ),
            (Symbol::new("digest"), Datum::Bytes(id.bytes.to_vec())),
        ],
    }
}

fn expected_delta_class(delta: &str) -> Option<ControlledDeltaClass> {
    match delta {
        "D1" => Some(ControlledDeltaClass::NoSemanticChange),
        "D2" => Some(ControlledDeltaClass::PrivateImplementation),
        "D3" => Some(ControlledDeltaClass::PublicApi),
        "D4" => Some(ControlledDeltaClass::PublicHeadsAndPins),
        "D5" => Some(ControlledDeltaClass::ProofAndCheckerImplementation),
        "D6" => Some(ControlledDeltaClass::PresentationOnly),
        _ => None,
    }
}

fn unaffected_basis_points(denominator: usize, affected: usize) -> u16 {
    if denominator == 0 || affected > denominator {
        return 0;
    }
    u16::try_from((denominator - affected).saturating_mul(10_000) / denominator).unwrap_or(10_000)
}

fn expected_datum(row: &ExpectedClosure) -> Datum {
    Datum::Node {
        tag: Symbol::qualified("projection", "expected-closure-v1"),
        fields: vec![
            (Symbol::new("delta"), Datum::String(row.delta.clone())),
            (
                Symbol::new("denominator"),
                Datum::String(row.denominator.to_string()),
            ),
            (
                Symbol::new("max-affected"),
                Datum::String(row.max_affected.to_string()),
            ),
            (
                Symbol::new("max-revision-delta"),
                Datum::String(row.max_revision_delta.to_string()),
            ),
            (Symbol::new("carried-state"), Datum::Bool(row.carried_state)),
            (
                Symbol::new("affected"),
                Datum::Vector(
                    row.affected
                        .iter()
                        .map(|id| Datum::String(id.as_str().to_owned()))
                        .collect(),
                ),
            ),
        ],
    }
}