ifc_lite_geometry/placement/
validated_chain.rs

1// SPDX-License-Identifier: MPL-2.0
2//! Verified suffix reuse for consumers inspecting many products (#7779 E1).
3
4use super::chain_walk::{climb, Climb};
5use super::validate::check_frames;
6use super::{unsupported, ChainPolicy, Link, PlacementResult};
7use ifc_lite_core::{execution_owner::ExecutionIdentity, EntityDecoder};
8use rustc_hash::FxHashMap;
9use smallvec::SmallVec;
10
11/// A validation session tied to ONE immutable decoder source and ONE policy.
12/// A transform-cache entry alone never proves that a consumer's policy holds.
13/// Only whole successful chains enter this session's private verified suffixes;
14/// failures remain uncached and cycles still use the canonical visited walk.
15/// Drop the session before changing policy or moving to another decoder.
16pub struct ChainValidator<'decoder, 'source> {
17    decoder: &'decoder mut EntityDecoder<'source>,
18    policy: ChainPolicy,
19    identity: ExecutionIdentity,
20    verified: FxHashMap<u32, u32>,
21}
22
23impl<'decoder, 'source> ChainValidator<'decoder, 'source> {
24    pub fn new(decoder: &'decoder mut EntityDecoder<'source>, policy: ChainPolicy) -> Self {
25        let identity = decoder.execution_identity();
26        Self {
27            decoder,
28            policy,
29            identity,
30            verified: FxHashMap::default(),
31        }
32    }
33
34    /// Access the same decoder for consumer-specific checks and output work.
35    /// Replacing the decoder or its explicit configuration invalidates verified
36    /// suffixes at the next validation. Lazy memo population preserves them.
37    pub fn decoder(&mut self) -> &mut EntityDecoder<'source> {
38        self.decoder
39    }
40
41    /// Return `(root, newly_verified_links)`, links leaf first. An already
42    /// verified starting id returns its root and no new links. At each ancestor
43    /// a known suffix stops the walk, so a parent-before-child ladder costs one
44    /// new link per product rather than 1+2+...+N links. No chain depth cap.
45    pub fn validate(&mut self, id: u32) -> PlacementResult<(u32, SmallVec<[Link; 8]>)> {
46        let identity = self.decoder.execution_identity();
47        if identity != self.identity {
48            self.verified.clear();
49            self.identity = identity;
50        }
51        if let Some(root) = self.verified.get(&id) {
52            return Ok((*root, SmallVec::new()));
53        }
54        let start = self.decoder.decode_by_id(id)?;
55        let mut climbed = Climb::new();
56        climb(
57            &mut climbed,
58            &start,
59            self.decoder,
60            false,
61            false,
62            Some(&self.verified),
63        )?;
64        if let Some((at, found)) = climbed.stopped_at {
65            return Err(unsupported(self.decoder, at, found));
66        }
67        check_frames(&climbed.frames, self.decoder, &self.policy)?;
68        // A fresh start either contributes a frame or encounters a verified id.
69        let root = climbed.verified_root.unwrap_or_else(|| {
70            climbed
71                .frames
72                .last()
73                .expect("nonempty placement chain")
74                .entity
75                .id
76        });
77        let links: SmallVec<[Link; 8]> = climbed
78            .frames
79            .iter()
80            .map(|frame| Link {
81                id: frame.entity.id,
82                kind: frame.kind,
83            })
84            .collect();
85        for link in &links {
86            self.verified.insert(link.id, root);
87        }
88        Ok((root, links))
89    }
90}