rvt 0.4.0

Apache-2.0 clean-room reader for Autodesk Revit files (.rvt, .rfa, .rte, .rft) without Revit: metadata, previews and the embedded schema of 2016-2026 files, and typed elements with measured geometry from Revit 2024 and 2025 projects, exported as IFC4, glTF and plan SVG.
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
//! Experimental typed-relation domains, SCC condensation, and quarantine.
//!
//! Governing decisions (§3 of `docs/research/unified-research-report.md`):
//! relationships are **typed edges with provenance**, not a universal parent
//! pointer. ES reference graphs, ES value trees, BIM topology, and ElemTable
//! ownership must stay in separate [`RelationDomain`]s so research ledgers do
//! not cross-contaminate scoring (especially ES vs #152).
//!
//! **Experimental / not production.** These helpers are architecture stubs for
//! Phase 1 leftovers. They do **not** claim ES remapping, Door/Window joins,
//! compound openings, or converter-grade IFC are solved. Nothing here is wired
//! into default IFC emission.

use crate::evidence::{EdgeKind, EvidenceTier, TypedEdge};
use crate::identity::ScopedElementRef;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet, HashMap};

/// Isolated relation domain. Edges must not silently migrate across domains.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RelationDomain {
    /// ES-held ElementId / reference candidates (Lane A).
    EsRefGraph,
    /// ES value-tree containment (separate from BIM topology).
    EsValueTree,
    /// BIM host/opening/connect — only after evidence gates (Lane C).
    BimTopology,
    /// ElemTable ownership candidates (#152) — scored separately from ES.
    ElemTableOwnership,
    /// Catch-all research-only domain.
    ResearchOther,
}

impl RelationDomain {
    /// Stable id for manifests / CLI output.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::EsRefGraph => "es_ref_graph",
            Self::EsValueTree => "es_value_tree",
            Self::BimTopology => "bim_topology",
            Self::ElemTableOwnership => "elem_table_ownership",
            Self::ResearchOther => "research_other",
        }
    }

    /// Domains that must never contribute to each other's promotion scores.
    pub fn isolation_peers(self) -> &'static [RelationDomain] {
        match self {
            Self::EsRefGraph | Self::EsValueTree => &[Self::ElemTableOwnership],
            Self::ElemTableOwnership => &[Self::EsRefGraph, Self::EsValueTree],
            Self::BimTopology | Self::ResearchOther => &[],
        }
    }

    /// Map a ledger [`EdgeKind`] into a domain (fail closed on `Other`).
    pub fn for_edge_kind(kind: &EdgeKind) -> Self {
        match kind {
            EdgeKind::EsElementIdRef => Self::EsRefGraph,
            EdgeKind::EsValueTree => Self::EsValueTree,
            EdgeKind::BimRelation => Self::BimTopology,
            EdgeKind::ElemTableOwnership => Self::ElemTableOwnership,
            EdgeKind::Other(_) => Self::ResearchOther,
        }
    }
}

/// Registry of known relation domains with honesty notes.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RelationDomainRegistry {
    pub domains: Vec<RelationDomainEntry>,
    pub experimental: bool,
    pub notes: Vec<String>,
}

/// One registered domain row.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RelationDomainEntry {
    pub domain: RelationDomain,
    pub id: String,
    /// Production wiring status — always experimental for Phase 1 leftovers.
    pub status: DomainStatus,
    pub isolation_peers: Vec<RelationDomain>,
    pub notes: Vec<String>,
}

/// Domain promotion status (intentionally coarse).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DomainStatus {
    /// Architecture stub only — not wired to product claims.
    Experimental,
    /// Research observations may be recorded; no verified claims.
    Research,
    /// Do not use for product / IFC defaults.
    Quarantined,
}

impl RelationDomainRegistry {
    /// Seed registry with honest Phase 1 leftovers status.
    pub fn phase1_stub() -> Self {
        let domains = [
            RelationDomain::EsRefGraph,
            RelationDomain::EsValueTree,
            RelationDomain::BimTopology,
            RelationDomain::ElemTableOwnership,
            RelationDomain::ResearchOther,
        ]
        .into_iter()
        .map(|domain| RelationDomainEntry {
            domain,
            id: domain.as_str().to_string(),
            status: DomainStatus::Experimental,
            isolation_peers: domain.isolation_peers().to_vec(),
            notes: match domain {
                RelationDomain::EsRefGraph => {
                    vec!["ES ElementId remapping not claimed (H-ES5 Phase 2 Revit-blocked)".into()]
                }
                RelationDomain::EsValueTree => {
                    vec!["Separate from BIM topology per governing decision §3.2".into()]
                }
                RelationDomain::BimTopology => {
                    vec!["Bound by RE-19 / RE-20 negatives; no invented Door/Window joins".into()]
                }
                RelationDomain::ElemTableOwnership => {
                    vec!["#152 scoring wall — ES refs must not contribute".into()]
                }
                RelationDomain::ResearchOther => {
                    vec!["Catch-all; never promote without an explicit domain move".into()]
                }
            },
        })
        .collect();
        Self {
            domains,
            experimental: true,
            notes: vec![
                "RelationDomainRegistry is experimental architecture — not production topology."
                    .into(),
                "Default IFC must not emit ES-derived edges (G-IFC).".into(),
            ],
        }
    }
}

/// Opaque node id inside one domain graph (stable for serialization).
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct RelationNodeId(pub String);

impl RelationNodeId {
    pub fn from_scoped(r: &ScopedElementRef) -> Self {
        let eid = r
            .element_id
            .map(|id| id.to_string())
            .unwrap_or_else(|| "unknown".into());
        Self(format!("{}#{eid}", r.document.document_key))
    }
}

/// Directed edge inside a single [`RelationDomain`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DomainEdge {
    pub from: RelationNodeId,
    pub to: RelationNodeId,
    pub kind: EdgeKind,
    pub tier: EvidenceTier,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub notes: Vec<String>,
}

impl DomainEdge {
    pub fn from_typed(edge: &TypedEdge) -> Self {
        Self {
            from: RelationNodeId::from_scoped(&edge.from),
            to: RelationNodeId::from_scoped(&edge.to),
            kind: edge.kind.clone(),
            tier: edge.tier,
            notes: edge.notes.clone(),
        }
    }
}

/// Reason an edge or component was quarantined.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum QuarantineReason {
    /// Edge domain does not match the graph domain.
    CrossDomain,
    /// Evidence tier too low for the requested operation.
    LowEvidence { tier: String, required: String },
    /// Cycle / mutual reachability marked for human review.
    StronglyConnectedComponent { component_id: usize },
    /// Explicit research non-claim.
    HonestyWall { detail: String },
}

/// Quarantined edge or SCC summary.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct QuarantineEntry {
    pub reason: QuarantineReason,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub edge: Option<DomainEdge>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub nodes: Vec<RelationNodeId>,
    pub notes: Vec<String>,
}

/// Directed graph confined to one [`RelationDomain`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RelationGraph {
    pub domain: RelationDomain,
    pub nodes: BTreeSet<RelationNodeId>,
    pub edges: Vec<DomainEdge>,
    pub quarantine: Vec<QuarantineEntry>,
}

impl RelationGraph {
    pub fn new(domain: RelationDomain) -> Self {
        Self {
            domain,
            nodes: BTreeSet::new(),
            edges: Vec::new(),
            quarantine: Vec::new(),
        }
    }

    /// Insert an edge when its [`EdgeKind`] maps to this domain; else quarantine.
    pub fn push_typed(&mut self, edge: &TypedEdge) {
        let domain = RelationDomain::for_edge_kind(&edge.kind);
        let domain_edge = DomainEdge::from_typed(edge);
        self.nodes.insert(domain_edge.from.clone());
        self.nodes.insert(domain_edge.to.clone());
        if domain != self.domain {
            self.quarantine.push(QuarantineEntry {
                reason: QuarantineReason::CrossDomain,
                edge: Some(domain_edge),
                nodes: Vec::new(),
                notes: vec![format!(
                    "refused: edge domain {} ≠ graph domain {}",
                    domain.as_str(),
                    self.domain.as_str()
                )],
            });
            return;
        }
        self.edges.push(domain_edge);
    }

    /// Adjacency list for algorithms (node → successors).
    fn adjacency(&self) -> BTreeMap<RelationNodeId, Vec<RelationNodeId>> {
        let mut adj: BTreeMap<RelationNodeId, Vec<RelationNodeId>> = BTreeMap::new();
        for n in &self.nodes {
            adj.entry(n.clone()).or_default();
        }
        for e in &self.edges {
            adj.entry(e.from.clone()).or_default().push(e.to.clone());
            adj.entry(e.to.clone()).or_default();
        }
        adj
    }

    /// Tarjan strongly connected components.
    ///
    /// Returns components as lists of node ids. Singleton non-loop nodes are
    /// included (standard Tarjan). Components with `len >= 2` (or a self-loop)
    /// are condensation candidates for quarantine review.
    pub fn strongly_connected_components(&self) -> Vec<Vec<RelationNodeId>> {
        tarjan_scc(&self.adjacency())
    }

    /// Condensation DAG: each SCC becomes a supernode; edges between SCCs retained.
    pub fn condense(&self) -> Condensation {
        let components = self.strongly_connected_components();
        let mut node_to_comp: HashMap<RelationNodeId, usize> = HashMap::new();
        for (ci, comp) in components.iter().enumerate() {
            for n in comp {
                node_to_comp.insert(n.clone(), ci);
            }
        }

        let mut dag_edges: BTreeSet<(usize, usize)> = BTreeSet::new();
        for e in &self.edges {
            let Some(&a) = node_to_comp.get(&e.from) else {
                continue;
            };
            let Some(&b) = node_to_comp.get(&e.to) else {
                continue;
            };
            if a != b {
                dag_edges.insert((a, b));
            }
        }

        let mut cyclic: Vec<usize> = Vec::new();
        let self_loop_nodes: BTreeSet<_> = self
            .edges
            .iter()
            .filter(|e| e.from == e.to)
            .map(|e| e.from.clone())
            .collect();
        for (ci, comp) in components.iter().enumerate() {
            let multi = comp.len() >= 2;
            let looped = comp.len() == 1 && self_loop_nodes.contains(&comp[0]);
            if multi || looped {
                cyclic.push(ci);
            }
        }

        Condensation {
            domain: self.domain,
            components,
            dag_edges: dag_edges.into_iter().collect(),
            cyclic_component_ids: cyclic,
        }
    }

    /// Quarantine cyclic SCCs for human / oracle review (does not delete edges).
    pub fn quarantine_cyclic_sccs(&mut self) {
        let condensation = self.condense();
        for &ci in &condensation.cyclic_component_ids {
            let nodes = condensation.components[ci].clone();
            self.quarantine.push(QuarantineEntry {
                reason: QuarantineReason::StronglyConnectedComponent { component_id: ci },
                edge: None,
                nodes,
                notes: vec![
                    "SCC quarantined — cycles are not auto-promoted to BIM/IFC relations".into(),
                ],
            });
        }
    }

    /// Quarantine edges below a minimum evidence tier.
    pub fn quarantine_below_tier(&mut self, required: EvidenceTier) {
        let mut kept = Vec::new();
        for edge in std::mem::take(&mut self.edges) {
            if edge.tier < required {
                self.quarantine.push(QuarantineEntry {
                    reason: QuarantineReason::LowEvidence {
                        tier: edge.tier.as_str().to_string(),
                        required: required.as_str().to_string(),
                    },
                    edge: Some(edge),
                    nodes: Vec::new(),
                    notes: vec!["Below evidence gate — left in quarantine ledger".into()],
                });
            } else {
                kept.push(edge);
            }
        }
        self.edges = kept;
    }
}

/// Condensation of a [`RelationGraph`] (DAG of SCCs).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Condensation {
    pub domain: RelationDomain,
    pub components: Vec<Vec<RelationNodeId>>,
    /// Directed edges between component indices (`from_comp`, `to_comp`).
    pub dag_edges: Vec<(usize, usize)>,
    /// Component indices that contain a cycle (size≥2 or self-loop).
    pub cyclic_component_ids: Vec<usize>,
}

/// Tarjan SCC over an adjacency map.
fn tarjan_scc(adj: &BTreeMap<RelationNodeId, Vec<RelationNodeId>>) -> Vec<Vec<RelationNodeId>> {
    struct Tarjan<'a> {
        adj: &'a BTreeMap<RelationNodeId, Vec<RelationNodeId>>,
        index: usize,
        stack: Vec<RelationNodeId>,
        on_stack: BTreeSet<RelationNodeId>,
        indices: HashMap<RelationNodeId, usize>,
        lowlink: HashMap<RelationNodeId, usize>,
        components: Vec<Vec<RelationNodeId>>,
    }

    impl Tarjan<'_> {
        fn strongconnect(&mut self, v: &RelationNodeId) {
            self.indices.insert(v.clone(), self.index);
            self.lowlink.insert(v.clone(), self.index);
            self.index += 1;
            self.stack.push(v.clone());
            self.on_stack.insert(v.clone());

            if let Some(succs) = self.adj.get(v).cloned() {
                for w in succs {
                    if !self.indices.contains_key(&w) {
                        self.strongconnect(&w);
                        let lw = *self.lowlink.get(&w).expect("lowlink w");
                        let lv = *self.lowlink.get(v).expect("lowlink v");
                        self.lowlink.insert(v.clone(), lv.min(lw));
                    } else if self.on_stack.contains(&w) {
                        let iw = *self.indices.get(&w).expect("index w");
                        let lv = *self.lowlink.get(v).expect("lowlink v");
                        self.lowlink.insert(v.clone(), lv.min(iw));
                    }
                }
            }

            if self.lowlink.get(v) == self.indices.get(v) {
                let mut comp = Vec::new();
                loop {
                    let w = self.stack.pop().expect("stack");
                    self.on_stack.remove(&w);
                    let done = w == *v;
                    comp.push(w);
                    if done {
                        break;
                    }
                }
                self.components.push(comp);
            }
        }
    }

    let mut state = Tarjan {
        adj,
        index: 0,
        stack: Vec::new(),
        on_stack: BTreeSet::new(),
        indices: HashMap::new(),
        lowlink: HashMap::new(),
        components: Vec::new(),
    };

    for v in adj.keys() {
        if !state.indices.contains_key(v) {
            state.strongconnect(v);
        }
    }
    state.components
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::identity::DocumentIdentity;

    fn edge(kind: EdgeKind, from: u32, to: u32, tier: EvidenceTier) -> TypedEdge {
        let doc = DocumentIdentity::from_key("doc");
        TypedEdge {
            kind,
            from: ScopedElementRef::from_element_id(doc.clone(), from),
            to: ScopedElementRef::from_element_id(doc, to),
            tier,
            span: None,
            notes: vec![],
        }
    }

    #[test]
    fn registry_marks_experimental_and_isolates_es_from_elemtable() {
        let reg = RelationDomainRegistry::phase1_stub();
        assert!(reg.experimental);
        let es = reg
            .domains
            .iter()
            .find(|d| d.domain == RelationDomain::EsRefGraph)
            .expect("es");
        assert!(
            es.isolation_peers
                .contains(&RelationDomain::ElemTableOwnership)
        );
        let json = serde_json::to_value(&reg).expect("ser");
        let back: RelationDomainRegistry = serde_json::from_value(json).expect("de");
        assert_eq!(back.domains.len(), 5);
    }

    #[test]
    fn cross_domain_edge_is_quarantined() {
        let mut g = RelationGraph::new(RelationDomain::EsRefGraph);
        g.push_typed(&edge(EdgeKind::ElemTableOwnership, 1, 2, EvidenceTier::E1));
        assert!(g.edges.is_empty());
        assert_eq!(g.quarantine.len(), 1);
        assert!(matches!(
            g.quarantine[0].reason,
            QuarantineReason::CrossDomain
        ));
    }

    #[test]
    fn tarjan_finds_cycle_and_condensation_dag() {
        // 1 → 2 → 3 → 1 (cycle) and 3 → 4 (tail)
        let mut g = RelationGraph::new(RelationDomain::ResearchOther);
        for (a, b) in [(1, 2), (2, 3), (3, 1), (3, 4)] {
            g.push_typed(&edge(EdgeKind::Other("t".into()), a, b, EvidenceTier::E0));
        }
        let comps = g.strongly_connected_components();
        let cyclic = comps.iter().find(|c| c.len() >= 2).expect("cycle scc");
        assert_eq!(cyclic.len(), 3);
        let cond = g.condense();
        assert!(!cond.cyclic_component_ids.is_empty());
        // Exactly one DAG edge: cycle-comp → {4}
        assert_eq!(cond.dag_edges.len(), 1);
    }

    #[test]
    fn quarantine_cyclic_and_low_tier() {
        let mut g = RelationGraph::new(RelationDomain::ResearchOther);
        g.push_typed(&edge(EdgeKind::Other("a".into()), 1, 2, EvidenceTier::E0));
        g.push_typed(&edge(EdgeKind::Other("a".into()), 2, 1, EvidenceTier::E0));
        g.push_typed(&edge(EdgeKind::Other("a".into()), 3, 4, EvidenceTier::E1));
        g.quarantine_cyclic_sccs();
        assert!(g.quarantine.iter().any(|q| matches!(
            q.reason,
            QuarantineReason::StronglyConnectedComponent { .. }
        )));
        g.quarantine_below_tier(EvidenceTier::E2);
        assert!(g.edges.is_empty());
        assert!(
            g.quarantine
                .iter()
                .any(|q| matches!(q.reason, QuarantineReason::LowEvidence { .. }))
        );
    }

    #[test]
    fn condensation_round_trips_json() {
        let mut g = RelationGraph::new(RelationDomain::EsRefGraph);
        g.push_typed(&edge(EdgeKind::EsElementIdRef, 10, 20, EvidenceTier::E0));
        let cond = g.condense();
        let json = serde_json::to_string(&cond).expect("ser");
        let back: Condensation = serde_json::from_str(&json).expect("de");
        assert_eq!(back.domain, RelationDomain::EsRefGraph);
        assert_eq!(back.components.len(), 2);
    }
}