vela-protocol 0.940.5

Core library for the Vela scientific knowledge protocol: replayable frontier state, signed canonical events, and proof packets.
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
//! The dark-matter boundary query (memo §3, §7.3): the productive edges of a
//! frontier — the work that is one step from done, the results that rest on
//! thin ground, the places the record disagrees, and the open findings with
//! no scaffolding yet.
//!
//! "Dark matter" is the memo's name for the latent work a frontier implies but
//! has not yet surfaced as a task: an open finding whose every premise is
//! already established (so closing it is one verifier-run away), an
//! established result resting on a single thread (fragile), a live
//! contradiction, an isolated open question nobody has started. This module is
//! a pure projection over the typed [`FrontierGraph`] and the findings' review
//! state — it classifies, it never adjudicates, and every item points back at
//! a real finding the caller can open a submission against.

use serde::Serialize;
use std::collections::{BTreeMap, BTreeSet};

use crate::frontier_graph::{EdgeKind, FindingState, FrontierGraph};
use crate::pathfind::SUPPORT_KINDS;
use crate::project::Project;

/// One boundary finding: the finding itself, why it sits on the boundary, and
/// the related findings that explain the classification (its established
/// premises, its thin support, or its contradiction partner).
#[derive(Debug, Clone, Serialize)]
pub struct BoundaryItem {
    pub finding: String,
    pub label: String,
    pub reason: String,
    pub related: Vec<String>,
}

/// A finding whose support funnels entirely through one load-bearing
/// dependency that is not itself established (GPT §11): a single point of
/// failure. If that dependency falls, the finding's whole support falls with
/// it. The kintsugi crack — where the frontier is one correction from a cascade.
#[derive(Debug, Clone, Serialize)]
pub struct BrittleItem {
    pub finding: String,
    pub label: String,
    /// The load-bearing dependency every support path runs through.
    pub dominator: String,
    pub dominator_label: String,
    /// The dominator's own state (why it is a risk).
    pub dominator_state: String,
    /// How many support nodes vanish if the dominator is removed.
    pub support_size: usize,
}

/// The boundary of a frontier, partitioned into the dark-matter categories.
/// Each list is sorted by finding id for stable output.
#[derive(Debug, Clone, Default, Serialize)]
pub struct Boundary {
    /// Open findings whose every in-frontier premise is already established —
    /// closing them is one step away (the highest-value queue).
    pub one_premise_away: Vec<BoundaryItem>,
    /// Established findings resting on thin ground (low confidence, or a
    /// single supporting thread).
    pub fragile: Vec<BoundaryItem>,
    /// Findings whose entire support funnels through one un-established
    /// load-bearing dependency (a single point of failure).
    pub brittle: Vec<BrittleItem>,
    /// Findings in live disagreement — a contradiction partner or a contested
    /// review verdict. Never auto-resolved.
    pub contested: Vec<BoundaryItem>,
    /// Open findings with no scaffolding: no premises, no support, nobody has
    /// started building toward or away from them.
    pub stale_open: Vec<BoundaryItem>,
}

impl Boundary {
    /// Derive the boundary from a project. Pure and deterministic.
    #[must_use]
    pub fn derive(project: &Project) -> Self {
        Self::derive_with_demoted(project, &BTreeSet::new())
    }

    /// Derive the boundary treating every finding id in `demoted` as if it were
    /// `Open` — the control arm of the lemma-inheritance ablation (the memo's
    /// "Compounding B"). Removing an accepted lemma from the established pool can
    /// only shrink `one_premise_away` (a target that rested on it is no longer
    /// one step from done), so `one_premise_away(treatment) - one_premise_away(
    /// control)` measures the unlock structure inherited verified lemmas provide.
    /// Pure and deterministic.
    #[must_use]
    pub fn derive_with_demoted(project: &Project, demoted: &BTreeSet<String>) -> Self {
        Self::derive_with_overrides(project, demoted, &BTreeSet::new())
    }

    /// Derive the boundary treating every finding id in `established` as if it
    /// were `Established` — the inverse of [`Self::derive_with_demoted`], the
    /// what-if arm of the unlock measure. Promoting a target's last missing
    /// premise into the established pool can only grow `one_premise_away` (the
    /// target becomes one step from done), so `one_premise_away(promoted) -
    /// one_premise_away(actual)` measures the unlock yield a candidate
    /// acceptance would produce before anyone signs it. Pure and deterministic.
    #[must_use]
    pub fn derive_with_promoted(project: &Project, established: &BTreeSet<String>) -> Self {
        Self::derive_with_overrides(project, &BTreeSet::new(), established)
    }

    /// The shared body behind the demoted/promoted arms. `demoted` wins over
    /// `promoted` when an id appears in both (the control arm strips state; a
    /// what-if promotion never resurrects a deliberately demoted lemma).
    #[must_use]
    fn derive_with_overrides(
        project: &Project,
        demoted: &BTreeSet<String>,
        promoted: &BTreeSet<String>,
    ) -> Self {
        let graph = FrontierGraph::from_project(project);
        // Effective state: a demoted finding is treated as Open, a promoted
        // finding as Established, regardless of its actual review state.
        let eff = |id: &str, actual: FindingState| -> FindingState {
            if demoted.contains(id) {
                FindingState::Open
            } else if promoted.contains(id) {
                FindingState::Established
            } else {
                actual
            }
        };

        // Per-node incidence: outgoing premises (depends/derived/discharges),
        // outgoing/incoming support, for the scaffolding and one-premise tests.
        let mut out_premise: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
        let mut out_support: BTreeMap<&str, usize> = BTreeMap::new();
        let mut in_support: BTreeMap<&str, usize> = BTreeMap::new();
        for e in graph.all_edges() {
            match e.kind {
                EdgeKind::DependsOn | EdgeKind::DerivedFrom | EdgeKind::Discharges => {
                    out_premise.entry(&e.source).or_default().push(&e.target);
                }
                EdgeKind::Supports | EdgeKind::Improves => {
                    *out_support.entry(&e.source).or_default() += 1;
                    *in_support.entry(&e.target).or_default() += 1;
                }
                _ => {}
            }
        }

        let mut boundary = Boundary::default();

        for node in graph.nodes() {
            let id = node.id.as_str();
            match eff(id, node.state) {
                FindingState::Open => {
                    let premises = out_premise.get(id).cloned().unwrap_or_default();
                    let has_support_scaffolding = out_support.contains_key(id)
                        || in_support.contains_key(id)
                        || !premises.is_empty();
                    if premises.is_empty() {
                        if !has_support_scaffolding {
                            boundary.stale_open.push(BoundaryItem {
                                finding: node.id.clone(),
                                label: node.label.clone(),
                                reason: "open with no premises, support, or dependents".into(),
                                related: vec![],
                            });
                        }
                        continue;
                    }
                    // One-premise-away: every in-frontier premise is
                    // established. An out-of-frontier or unestablished premise
                    // disqualifies it (we can only certify what we can see).
                    let all_established = premises.iter().all(|t| {
                        graph
                            .node(t)
                            .is_some_and(|n| eff(&n.id, n.state) == FindingState::Established)
                    });
                    if all_established {
                        boundary.one_premise_away.push(BoundaryItem {
                            finding: node.id.clone(),
                            label: node.label.clone(),
                            reason: format!("open; all {} premise(s) established", premises.len()),
                            related: premises.iter().map(|s| (*s).to_string()).collect(),
                        });
                    }
                }
                FindingState::Fragile => {
                    let support = out_support.get(id).copied().unwrap_or(0);
                    boundary.fragile.push(BoundaryItem {
                        finding: node.id.clone(),
                        label: node.label.clone(),
                        reason: if support <= 1 {
                            format!(
                                "established but thin (confidence {:.2}, {support} supporting link)",
                                node.confidence
                            )
                        } else {
                            format!("established but thin (confidence {:.2})", node.confidence)
                        },
                        related: vec![],
                    });
                }
                FindingState::Contested => {
                    boundary.contested.push(BoundaryItem {
                        finding: node.id.clone(),
                        label: node.label.clone(),
                        reason: "contested review verdict".into(),
                        related: vec![],
                    });
                }
                FindingState::Established | FindingState::Refuted => {}
            }
        }

        // Brittle: a finding whose support funnels entirely through one
        // load-bearing dependency that is not itself established. Computed over
        // the support-bearing edge kinds; only single-points-of-failure with a
        // risky dominator surface (an established dominator is sound footing).
        for node in graph.nodes() {
            let doms = graph.support_dominators(&node.id, &SUPPORT_KINDS);
            let Some(spof) = doms.iter().find(|d| d.single_point_of_failure) else {
                continue;
            };
            let dom_state = spof.state.unwrap_or(FindingState::Open);
            if dom_state == FindingState::Established {
                continue;
            }
            boundary.brittle.push(BrittleItem {
                finding: node.id.clone(),
                label: node.label.clone(),
                dominator: spof.node.clone(),
                dominator_label: spof.label.clone(),
                dominator_state: dom_state.as_str().to_string(),
                support_size: spof.weight,
            });
        }
        boundary.brittle.sort_by(|a, b| {
            b.support_size
                .cmp(&a.support_size)
                .then(a.finding.cmp(&b.finding))
        });

        // Contradiction pairs add any node not already flagged contested, with
        // its partner as the related finding. Each endpoint is listed once.
        let already: BTreeSet<String> = boundary
            .contested
            .iter()
            .map(|i| i.finding.clone())
            .collect();
        let mut added: BTreeSet<String> = already.clone();
        for (a, b) in graph.contradiction_pairs() {
            for (node_id, partner) in [(&a, &b), (&b, &a)] {
                if added.insert(node_id.clone()) {
                    let label = graph.label_of(node_id).unwrap_or("").to_string();
                    boundary.contested.push(BoundaryItem {
                        finding: node_id.clone(),
                        label,
                        reason: "in a recorded contradiction".into(),
                        related: vec![partner.clone()],
                    });
                }
            }
        }
        boundary.contested.sort_by(|x, y| x.finding.cmp(&y.finding));

        boundary
    }

    /// Total findings on the boundary across all categories.
    #[must_use]
    pub fn total(&self) -> usize {
        self.one_premise_away.len()
            + self.fragile.len()
            + self.brittle.len()
            + self.contested.len()
            + self.stale_open.len()
    }

    /// Stable JSON for the web boundary view and the Dark Matter Queue.
    #[must_use]
    pub fn to_json(&self) -> serde_json::Value {
        serde_json::json!({
            "schema": "vela.boundary.v0.1",
            "summary": {
                "one_premise_away": self.one_premise_away.len(),
                "fragile": self.fragile.len(),
                "brittle": self.brittle.len(),
                "contested": self.contested.len(),
                "stale_open": self.stale_open.len(),
                "total": self.total(),
            },
            "one_premise_away": self.one_premise_away,
            "fragile": self.fragile,
            "brittle": self.brittle,
            "contested": self.contested,
            "stale_open": self.stale_open,
            "boundary_is_derived": true,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bundle::ReviewState;
    use crate::project::assemble;
    use crate::project::reverse_dep_index_tests::{link_to, synth_finding};

    fn link_typed(target: &str, link_type: &str) -> crate::bundle::Link {
        let mut link = link_to(target);
        link.link_type = link_type.into();
        link
    }

    #[test]
    fn one_premise_away_needs_all_premises_established() {
        // established premise `a`; open `b` depends_on a → one premise away.
        let mut a = synth_finding(0, vec![]);
        a.flags.review_state = Some(ReviewState::Accepted);
        a.confidence.score = 0.9;
        let b = synth_finding(1, vec![link_typed(&a.id, "depends")]);
        let (a_id, b_id) = (a.id.clone(), b.id.clone());
        let mut project = assemble("bd", vec![], 0, 0, "test");
        project.findings = vec![a, b];

        let boundary = Boundary::derive(&project);
        assert_eq!(boundary.one_premise_away.len(), 1);
        assert_eq!(boundary.one_premise_away[0].finding, b_id);
        assert_eq!(boundary.one_premise_away[0].related, vec![a_id]);
    }

    #[test]
    fn demoting_an_inherited_lemma_shrinks_one_premise_away() {
        // The lemma-inheritance ablation: `a` established, open `b` depends_on a.
        // Treatment (a inherited) -> b is one-premise-away. Control (a demoted to
        // Open) -> b is no longer one-premise-away. Δ = 1 = the unlock structure.
        let mut a = synth_finding(0, vec![]);
        a.flags.review_state = Some(ReviewState::Accepted);
        a.confidence.score = 0.9;
        let b = synth_finding(1, vec![link_typed(&a.id, "depends")]);
        let a_id = a.id.clone();
        let mut project = assemble("ablate", vec![], 0, 0, "test");
        project.findings = vec![a, b];

        let treatment = Boundary::derive(&project).one_premise_away.len();
        let demoted: BTreeSet<String> = std::iter::once(a_id).collect();
        let control = Boundary::derive_with_demoted(&project, &demoted)
            .one_premise_away
            .len();
        assert_eq!(treatment, 1);
        assert_eq!(control, 0);
        assert!(
            treatment > control,
            "inheritance compounds: Δ = {}",
            treatment - control
        );
    }

    #[test]
    fn promoting_the_sole_missing_premise_moves_target_into_one_premise_away() {
        // The inverse arm: `a` established, `b` open, target `z` depends on
        // BOTH. Actual state -> z is not one-premise-away (b is open).
        // Promoting b (treat as Established) -> z becomes one-premise-away.
        // Δ = 1 = the unlock yield accepting b would produce.
        let mut a = synth_finding(0, vec![]);
        a.flags.review_state = Some(ReviewState::Accepted);
        a.confidence.score = 0.9;
        let b = synth_finding(1, vec![]);
        let z = synth_finding(
            2,
            vec![link_typed(&a.id, "depends"), link_typed(&b.id, "depends")],
        );
        let (b_id, z_id) = (b.id.clone(), z.id.clone());
        let mut project = assemble("promote", vec![], 0, 0, "test");
        project.findings = vec![a, b, z];

        let actual = Boundary::derive(&project);
        assert!(
            actual.one_premise_away.iter().all(|i| i.finding != z_id),
            "z is two premises away in the actual state"
        );
        let established: BTreeSet<String> = std::iter::once(b_id).collect();
        let promoted = Boundary::derive_with_promoted(&project, &established);
        assert_eq!(promoted.one_premise_away.len(), 1);
        assert_eq!(promoted.one_premise_away[0].finding, z_id);
        assert!(
            promoted.one_premise_away.len() > actual.one_premise_away.len(),
            "promotion unlocks: Δ = {}",
            promoted.one_premise_away.len() - actual.one_premise_away.len()
        );
    }

    #[test]
    fn open_premise_disqualifies_one_premise_away() {
        // premise `a` is itself open → `b` is not one-premise-away.
        let a = synth_finding(0, vec![]);
        let b = synth_finding(1, vec![link_typed(&a.id, "depends")]);
        let mut project = assemble("bd2", vec![], 0, 0, "test");
        project.findings = vec![a, b];
        let boundary = Boundary::derive(&project);
        assert!(boundary.one_premise_away.is_empty());
    }

    #[test]
    fn verified_but_unreviewed_premise_remains_outside_established_boundary() {
        let a = synth_finding(0, vec![]);
        let b = synth_finding(1, vec![link_typed(&a.id, "depends")]);
        let b_id = b.id.clone();
        let attachments = crate::test_support::verified_attachment_pair(&a);
        let mut project = assemble("bd-verified-open", vec![], 0, 0, "test");
        project.findings = vec![a, b];
        project.verifier_attachments = attachments;

        let boundary = Boundary::derive(&project);
        assert!(
            boundary
                .one_premise_away
                .iter()
                .all(|item| item.finding != b_id),
            "a verified method result is not an accepted premise"
        );
    }

    #[test]
    fn isolated_open_is_stale_open() {
        let a = synth_finding(0, vec![]);
        let a_id = a.id.clone();
        let mut project = assemble("bd3", vec![], 0, 0, "test");
        project.findings = vec![a];
        let boundary = Boundary::derive(&project);
        assert_eq!(boundary.stale_open.len(), 1);
        assert_eq!(boundary.stale_open[0].finding, a_id);
    }

    #[test]
    fn fragile_is_accepted_but_low_confidence() {
        let mut a = synth_finding(0, vec![]);
        a.flags.review_state = Some(ReviewState::Accepted);
        a.confidence.score = 0.4; // below FRAGILE_CONFIDENCE
        let mut project = assemble("bd4", vec![], 0, 0, "test");
        project.findings = vec![a];
        let boundary = Boundary::derive(&project);
        assert_eq!(boundary.fragile.len(), 1);
        assert!(boundary.stale_open.is_empty());
    }

    #[test]
    fn brittle_when_support_funnels_through_one_open_dependency() {
        // z depends_on a depends_on b, none established: z's whole support
        // funnels through a (a single point of failure that is itself open).
        let b = synth_finding(0, vec![]);
        let a = synth_finding(1, vec![link_typed(&b.id, "depends")]);
        let z = synth_finding(2, vec![link_typed(&a.id, "depends")]);
        let (a_id, z_id) = (a.id.clone(), z.id.clone());
        let mut project = assemble("brit", vec![], 0, 0, "test");
        project.findings = vec![b, a, z];

        let boundary = Boundary::derive(&project);
        let item = boundary
            .brittle
            .iter()
            .find(|i| i.finding == z_id)
            .expect("z is brittle");
        assert_eq!(item.dominator, a_id);
        assert_eq!(item.dominator_state, "open");
        assert_eq!(item.support_size, 2);
    }

    #[test]
    fn not_brittle_when_load_bearing_dependency_is_established() {
        let mut a = synth_finding(0, vec![]);
        a.flags.review_state = Some(ReviewState::Accepted);
        a.confidence.score = 0.9;
        let z = synth_finding(1, vec![link_typed(&a.id, "depends")]);
        let z_id = z.id.clone();
        let mut project = assemble("brit2", vec![], 0, 0, "test");
        project.findings = vec![a, z];
        let boundary = Boundary::derive(&project);
        // a is established → z is one-premise-away, not brittle.
        assert!(boundary.brittle.iter().all(|i| i.finding != z_id));
    }

    #[test]
    fn contradiction_pair_marks_both_contested() {
        let x = synth_finding(0, vec![]);
        let y = synth_finding(1, vec![link_typed(&x.id, "contradicts")]);
        let mut project = assemble("bd5", vec![], 0, 0, "test");
        project.findings = vec![x, y];
        let boundary = Boundary::derive(&project);
        assert_eq!(boundary.contested.len(), 2);
        // each endpoint names the other as related
        assert!(boundary.contested.iter().all(|i| i.related.len() == 1));
    }
}