sim-incremental-core 0.1.2

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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
impl FixpointEngine {
    /// Solves to a stable fixpoint and mints its content-bound completion proof.
    pub fn solve_proven<N, E, L, C, S, P>(
        graph: &DataflowGraph<N, E, L, C>,
        transfer: &AdmittedTransfer<P>,
        bottom: S,
        seeds: impl IntoIterator<Item = (N, S)>,
        budgets: QueryBudgets,
    ) -> CompletionProofResult<N, E, L, C, S>
    where
        N: Clone + Hash + Ord,
        E: Clone + Hash + Ord,
        L: Clone + Hash + Ord,
        C: Clone + Hash + Ord,
        S: JoinSemilattice + Hash,
        P: TransferPolicy<S>,
    {
        let seeds = seeds.into_iter().collect::<BTreeMap<_, _>>();
        let solution = Self::solve(
            graph,
            transfer,
            bottom.clone(),
            seeds
                .iter()
                .map(|(node, state)| (node.clone(), state.clone())),
            budgets,
        )?;
        Ok(mint_completion_proof(
            graph, transfer, &bottom, &seeds, budgets, solution,
        ))
    }

    /// Presents a proof only when every semantic input still matches.
    pub fn present<'a, N, E, L, C, S, P>(
        proof: &'a DataflowCompletionProof<N, E, C, S>,
        graph: &DataflowGraph<N, E, L, C>,
        transfer: &AdmittedTransfer<P>,
        bottom: &S,
        seeds: impl IntoIterator<Item = (N, S)>,
        budgets: QueryBudgets,
    ) -> Result<&'a DataflowSolution<N, E, C, S>, CompletionProofMismatch>
    where
        N: Clone + Hash + Ord,
        E: Clone + Hash + Ord,
        L: Hash + Ord,
        C: Hash + Ord,
        S: Hash,
        P: TransferPolicy<S>,
    {
        let seeds = seeds.into_iter().collect::<BTreeMap<_, _>>();
        let identities = proof_inputs(graph, transfer, bottom, &seeds, budgets);
        if proof.graph != identities.graph || proof.boundaries != identities.boundaries {
            return Err(CompletionProofMismatch::Graph);
        }
        if proof.lattice != identities.lattice {
            return Err(CompletionProofMismatch::Lattice);
        }
        if proof.policy != identities.policy {
            return Err(CompletionProofMismatch::Policy);
        }
        if proof.limits != identities.limits {
            return Err(CompletionProofMismatch::Limits);
        }
        if proof.dependencies != identities.dependencies {
            return Err(CompletionProofMismatch::Dependencies);
        }
        Ok(&proof.solution)
    }

    /// Recomputes exactly the observed successor cone of changed entry facts.
    ///
    /// Structural, lattice, policy, or limit changes are intentionally refused:
    /// callers must perform a clean solve for those semantic edits.  Dependency
    /// edits reuse unaffected states and their observations, then remint a proof
    /// from the complete final state rather than blessing the old witness.
    pub fn solve_incremental<N, E, L, C, S, P>(
        previous: &DataflowCompletionProof<N, E, C, S>,
        graph: &DataflowGraph<N, E, L, C>,
        transfer: &AdmittedTransfer<P>,
        bottom: S,
        seeds: impl IntoIterator<Item = (N, S)>,
        budgets: QueryBudgets,
    ) -> CompletionProofResult<N, E, L, C, S>
    where
        N: Clone + Hash + Ord,
        E: Clone + Hash + Ord,
        L: Clone + Hash + Ord,
        C: Clone + Hash + Ord,
        S: JoinSemilattice + Hash,
        P: TransferPolicy<S>,
    {
        let seeds = seeds.into_iter().collect::<BTreeMap<_, _>>();
        for node in seeds.keys() {
            if graph.node(node).is_none() {
                return Err(DataflowError::UnknownSeed(node.clone()));
            }
        }
        let inputs = proof_inputs(graph, transfer, &bottom, &seeds, budgets);
        if previous.graph != inputs.graph
            || previous.lattice != inputs.lattice
            || previous.policy != inputs.policy
            || previous.boundaries != inputs.boundaries
            || previous.limits != inputs.limits
        {
            return Self::solve_proven(graph, transfer, bottom, seeds, budgets);
        }
        if previous.dependencies == inputs.dependencies {
            return Ok(previous.clone());
        }

        let old_seeds = previous_seed_fingerprints(previous, graph, &bottom);
        let changed = graph
            .nodes()
            .filter_map(|node| {
                let current = seeds
                    .get(node.id())
                    .unwrap_or(&bottom)
                    .incremental_fingerprint();
                (old_seeds.get(node.id()).copied() != Some(current)).then(|| node.id().clone())
            })
            .collect::<BTreeSet<_>>();
        let mut affected = changed.clone();
        let mut frontier = changed;
        while let Some(node) = frontier.pop_first() {
            for edge_id in graph.successors(&node).expect("graph node has an index") {
                let edge = graph.edge(edge_id).expect("graph edge has an index");
                let (_, target) = edge.predecessor_and_successor();
                if affected.insert(target.clone()) {
                    frontier.insert(target.clone());
                }
            }
        }

        let mut states = previous.solution.states.clone();
        for node in &affected {
            states.insert(node.clone(), bottom.clone());
        }
        for node in &affected {
            let mut state = seeds.get(node).cloned().unwrap_or_else(|| bottom.clone());
            for edge_id in graph.predecessors(node).expect("graph node has an index") {
                let edge = graph.edge(edge_id).expect("graph edge has an index");
                let (source, _) = edge.predecessor_and_successor();
                if !affected.contains(source) {
                    state = state.join(&transfer.transfer(&states[source]));
                }
            }
            states.insert(node.clone(), state);
        }

        let mut pending = affected.clone();
        let mut events = previous
            .solution
            .events
            .iter()
            .filter(|event| match event {
                DataflowEvent::Visit(node) => !affected.contains(node),
                DataflowEvent::Propagate { edge, .. } => {
                    let edge = graph.edge(edge).expect("proof edge belongs to graph");
                    let (source, _) = edge.predecessor_and_successor();
                    !affected.contains(source)
                }
            })
            .cloned()
            .collect::<Vec<_>>();
        let mut usage = DataflowUsage {
            ..DataflowUsage::default()
        };
        charge(
            &mut usage.depth,
            states.len(),
            budgets.max_depth,
            BudgetKind::Depth,
            ChargeLocation {
                node: None,
                edge: None,
                location: graph
                    .nodes()
                    .next()
                    .expect("graphs are non-empty")
                    .location()
                    .clone(),
                target_location: None,
            },
        )?;
        while let Some(node) = pending.pop_first() {
            let location = graph
                .node(&node)
                .expect("affected node exists")
                .location()
                .clone();
            charge_work(&mut usage, budgets, Some(node.clone()), location.clone())?;
            events.push(DataflowEvent::Visit(node.clone()));
            charge_work(&mut usage, budgets, Some(node.clone()), location.clone())?;
            let output = transfer.transfer(&states[&node]);
            if !states[&node].less_equal(&output) {
                return Err(DataflowError::NodeFailure {
                    failure: DataflowFailure::Transfer,
                    node,
                    location,
                });
            }
            for edge_id in graph.successors(&node).expect("affected node has an index") {
                let edge = graph.edge(edge_id).expect("graph edge exists");
                let (_, target) = edge.predecessor_and_successor();
                let target_location = graph
                    .node(target)
                    .expect("edge target exists")
                    .location()
                    .clone();
                charge(
                    &mut usage.observations,
                    1,
                    budgets.max_observations,
                    BudgetKind::Observations,
                    ChargeLocation {
                        node: None,
                        edge: Some(edge_id.clone()),
                        location: location.clone(),
                        target_location: Some(target_location.clone()),
                    },
                )?;
                events.push(DataflowEvent::Propagate {
                    edge: edge_id.clone(),
                    class: edge.class().clone(),
                });
                charge_work_edge(
                    &mut usage,
                    budgets,
                    edge_id.clone(),
                    location.clone(),
                    target_location,
                )?;
                let joined = states[target].join(&output);
                if joined != states[target] {
                    states.insert(target.clone(), joined);
                    pending.insert(target.clone());
                }
            }
        }
        charge(
            &mut usage.output,
            states.values().map(super::StateSize::state_size).sum(),
            budgets.max_output,
            BudgetKind::Output,
            ChargeLocation {
                node: None,
                edge: None,
                location: graph
                    .nodes()
                    .next()
                    .expect("graphs are non-empty")
                    .location()
                    .clone(),
                target_location: None,
            },
        )?;
        let solution = DataflowSolution {
            states,
            events,
            usage,
            causes: BTreeMap::new(),
        };
        Ok(mint_completion_proof(
            graph, transfer, &bottom, &seeds, budgets, solution,
        ))
    }

    /// Solves from explicit entry/exit facts; nodes not reached from a seed stay at bottom.
    pub fn solve<N, E, L, C, S, P>(
        graph: &DataflowGraph<N, E, L, C>,
        transfer: &AdmittedTransfer<P>,
        bottom: S,
        seeds: impl IntoIterator<Item = (N, S)>,
        budgets: QueryBudgets,
    ) -> DataflowResult<N, E, L, C, S>
    where
        N: Clone + Hash + Ord,
        E: Clone + Hash + Ord,
        L: Clone + Hash + Ord,
        C: Clone + Hash + Ord,
        S: JoinSemilattice,
        P: TransferPolicy<S>,
    {
        let mut usage = DataflowUsage::default();
        let retained = graph.nodes().count();
        charge(
            &mut usage.depth,
            retained,
            budgets.max_depth,
            BudgetKind::Depth,
            ChargeLocation {
                node: None,
                edge: None,
                location: graph
                    .nodes()
                    .next()
                    .expect("graphs are non-empty")
                    .location()
                    .clone(),
                target_location: None,
            },
        )?;
        let initial_output = bottom.state_size().saturating_mul(retained);
        charge(
            &mut usage.output,
            initial_output,
            budgets.max_output,
            BudgetKind::Output,
            ChargeLocation {
                node: None,
                edge: None,
                location: graph
                    .nodes()
                    .next()
                    .expect("graphs are non-empty")
                    .location()
                    .clone(),
                target_location: None,
            },
        )?;
        let mut states = graph
            .nodes()
            .map(|node| (node.id().clone(), bottom.clone()))
            .collect::<BTreeMap<_, _>>();
        let mut pending = BTreeSet::new();
        let mut causes = BTreeMap::<N, CausalRecord<N, E>>::new();
        for (node_id, seed) in seeds {
            let Some(node) = graph.node(&node_id) else {
                return Err(DataflowError::UnknownSeed(node_id));
            };
            let joined = states[&node_id].join(&seed);
            charge_work(
                &mut usage,
                budgets,
                Some(node_id.clone()),
                node.location().clone(),
            )?;
            if !states[&node_id].less_equal(&joined) || !seed.less_equal(&joined) {
                return Err(DataflowError::NodeFailure {
                    failure: DataflowFailure::Join,
                    node: node_id,
                    location: node.location().clone(),
                });
            }
            if joined != states[&node_id] {
                replace_state(
                    &mut states,
                    node_id.clone(),
                    joined,
                    &mut usage,
                    budgets,
                    node.location().clone(),
                )?;
                pending.insert(node_id.clone());
                record_cause(
                    &mut causes,
                    node_id.clone(),
                    CausalPredecessor {
                        node: node_id,
                        edge: None,
                    },
                    0,
                );
            }
        }

        let mut events = Vec::new();
        while let Some(node_id) = pending.pop_first() {
            let node = graph
                .node(&node_id)
                .expect("worklist node belongs to graph");
            charge_work(
                &mut usage,
                budgets,
                Some(node_id.clone()),
                node.location().clone(),
            )?;
            events.push(DataflowEvent::Visit(node_id.clone()));
            charge_work(
                &mut usage,
                budgets,
                Some(node_id.clone()),
                node.location().clone(),
            )?;
            let output = transfer.transfer(&states[&node_id]);
            if !states[&node_id].less_equal(&output) {
                return Err(DataflowError::NodeFailure {
                    failure: DataflowFailure::Transfer,
                    node: node_id,
                    location: node.location().clone(),
                });
            }
            for edge_id in graph.successors(&node_id).expect("node index is complete") {
                let edge = graph.edge(edge_id).expect("edge index is complete");
                let (_, target_id) = edge.predecessor_and_successor();
                let target = graph.node(target_id).expect("edge target exists");
                charge(
                    &mut usage.observations,
                    1,
                    budgets.max_observations,
                    BudgetKind::Observations,
                    ChargeLocation {
                        node: None,
                        edge: Some(edge_id.clone()),
                        location: node.location().clone(),
                        target_location: Some(target.location().clone()),
                    },
                )?;
                events.push(DataflowEvent::Propagate {
                    edge: edge_id.clone(),
                    class: edge.class().clone(),
                });
                charge_work_edge(
                    &mut usage,
                    budgets,
                    edge_id.clone(),
                    node.location().clone(),
                    target.location().clone(),
                )?;
                let joined = states[target_id].join(&output);
                if !states[target_id].less_equal(&joined) || !output.less_equal(&joined) {
                    return Err(DataflowError::EdgeFailure {
                        failure: DataflowFailure::Join,
                        edge: edge_id.clone(),
                        location: node.location().clone(),
                        target_location: target.location().clone(),
                    });
                }
                if joined != states[target_id] {
                    replace_state(
                        &mut states,
                        target_id.clone(),
                        joined,
                        &mut usage,
                        budgets,
                        target.location().clone(),
                    )?;
                    pending.insert(target_id.clone());
                    record_cause(
                        &mut causes,
                        target_id.clone(),
                        CausalPredecessor {
                            node: node_id.clone(),
                            edge: Some(edge_id.clone()),
                        },
                        0,
                    );
                }
            }
        }
        Ok(DataflowSolution {
            states,
            events,
            usage,
            causes,
        })
    }

    /// Starts a solve and executes at most `max_visits` complete worklist nodes.
    ///
    /// Unlike resource refusal inside [`Self::solve`], this cooperative boundary
    /// snapshots only between nodes, so resumption never repeats callbacks or
    /// presents a shortened event stream as complete.
    pub fn start_resumable<N, E, L, C, S, P>(
        graph: &DataflowGraph<N, E, L, C>,
        transfer: &AdmittedTransfer<P>,
        bottom: S,
        seeds: impl IntoIterator<Item = (N, S)>,
        max_visits: usize,
        explanation_limit: usize,
    ) -> DataflowProgressResult<N, E, L, C, S>
    where
        N: Clone + Hash + Ord,
        E: Clone + Hash + Ord,
        L: Clone + Hash + Ord,
        C: Clone + Hash + Ord,
        S: JoinSemilattice + Hash,
        P: TransferPolicy<S>,
    {
        let seeds = seeds.into_iter().collect::<BTreeMap<_, _>>();
        let dependencies = (&bottom, &seeds).incremental_fingerprint();
        let mut states = graph
            .nodes()
            .map(|node| (node.id().clone(), bottom.clone()))
            .collect::<BTreeMap<_, _>>();
        let mut pending = BTreeSet::new();
        let mut causes = BTreeMap::<N, CausalRecord<N, E>>::new();
        for (node_id, seed) in seeds {
            if graph.node(&node_id).is_none() {
                return Err(DataflowError::UnknownSeed(node_id));
            }
            let joined = states[&node_id].join(&seed);
            if joined != states[&node_id] {
                states.insert(node_id.clone(), joined);
                pending.insert(node_id.clone());
                record_cause(
                    &mut causes,
                    node_id.clone(),
                    CausalPredecessor {
                        node: node_id,
                        edge: None,
                    },
                    explanation_limit,
                );
            }
        }
        let continuation = make_continuation(
            ContinuationIdentity {
                graph: graph.fingerprint(),
                policy: transfer.fingerprint(),
                dependencies,
            },
            states,
            pending,
            Vec::new(),
            causes,
            DataflowUsage::default(),
            explanation_limit,
        );
        Self::resume_bound(graph, transfer, continuation, max_visits)
    }

    /// Resumes a content-bound snapshot, refusing every changed input identity.
    pub fn resume<N, E, L, C, S, P>(
        graph: &DataflowGraph<N, E, L, C>,
        transfer: &AdmittedTransfer<P>,
        bottom: &S,
        seeds: impl IntoIterator<Item = (N, S)>,
        continuation: DataflowContinuation<N, E, C, S>,
        max_visits: usize,
    ) -> DataflowProgressResult<N, E, L, C, S>
    where
        N: Clone + Hash + Ord,
        E: Clone + Hash + Ord,
        L: Clone + Hash + Ord,
        C: Clone + Hash + Ord,
        S: JoinSemilattice + Hash,
        P: TransferPolicy<S>,
    {
        check_fingerprint(
            ContinuationFingerprint::Graph,
            continuation.graph,
            graph.fingerprint(),
        )?;
        check_fingerprint(
            ContinuationFingerprint::Policy,
            continuation.policy,
            transfer.fingerprint(),
        )?;
        let seeds = seeds.into_iter().collect::<BTreeMap<_, _>>();
        check_fingerprint(
            ContinuationFingerprint::Dependencies,
            continuation.dependencies,
            (bottom, &seeds).incremental_fingerprint(),
        )?;
        Self::resume_bound(graph, transfer, continuation, max_visits)
    }

    fn resume_bound<N, E, L, C, S, P>(
        graph: &DataflowGraph<N, E, L, C>,
        transfer: &AdmittedTransfer<P>,
        continuation: DataflowContinuation<N, E, C, S>,
        max_visits: usize,
    ) -> DataflowProgressResult<N, E, L, C, S>
    where
        N: Clone + Hash + Ord,
        E: Clone + Hash + Ord,
        L: Clone + Hash + Ord,
        C: Clone + Hash + Ord,
        S: JoinSemilattice + Hash,
        P: TransferPolicy<S>,
    {
        let DataflowContinuation {
            graph: graph_fingerprint,
            policy: policy_fingerprint,
            dependencies,
            mut states,
            mut pending,
            mut events,
            mut causes,
            mut usage,
            cause_limit,
            ..
        } = continuation;
        let mut visits = 0;
        while visits < max_visits {
            let Some(node_id) = pending.pop_first() else {
                return Ok(DataflowProgress::Complete(DataflowSolution {
                    states,
                    events,
                    usage,
                    causes,
                }));
            };
            visits += 1;
            usage.work = usage.work.saturating_add(2);
            events.push(DataflowEvent::Visit(node_id.clone()));
            let output = transfer.transfer(&states[&node_id]);
            let node = graph
                .node(&node_id)
                .expect("snapshot node belongs to graph");
            if !states[&node_id].less_equal(&output) {
                return Err(DataflowError::NodeFailure {
                    failure: DataflowFailure::Transfer,
                    node: node_id,
                    location: node.location().clone(),
                });
            }
            for edge_id in graph.successors(&node_id).expect("node index is complete") {
                let edge = graph.edge(edge_id).expect("edge index is complete");
                let (_, target_id) = edge.predecessor_and_successor();
                usage.observations = usage.observations.saturating_add(1);
                usage.work = usage.work.saturating_add(1);
                events.push(DataflowEvent::Propagate {
                    edge: edge_id.clone(),
                    class: edge.class().clone(),
                });
                let joined = states[target_id].join(&output);
                if joined != states[target_id] {
                    usage.output = usage.output.saturating_add(joined.state_size());
                    states.insert(target_id.clone(), joined);
                    pending.insert(target_id.clone());
                    record_cause(
                        &mut causes,
                        target_id.clone(),
                        CausalPredecessor {
                            node: node_id.clone(),
                            edge: Some(edge_id.clone()),
                        },
                        cause_limit,
                    );
                }
            }
        }
        if pending.is_empty() {
            Ok(DataflowProgress::Complete(DataflowSolution {
                states,
                events,
                usage,
                causes,
            }))
        } else {
            Ok(DataflowProgress::Suspended(make_continuation(
                ContinuationIdentity {
                    graph: graph_fingerprint,
                    policy: policy_fingerprint,
                    dependencies,
                },
                states,
                pending,
                events,
                causes,
                usage,
                cause_limit,
            )))
        }
    }
}