mant-protocol 0.11.0

Transport-neutral query contracts and projections for ManT
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
//! Page-local original context, explicitly distinct from alias evidence.
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Original content supporting directly matched declarations. IDs are indices
/// in the containing document response's pool, never persistent identities.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(
    tag = "kind",
    rename_all = "kebab-case",
    rename_all_fields = "camelCase",
    deny_unknown_fields
)]
pub enum ExplanationSupport {
    /// A physical owner's original body reused by contained contexts.
    OwnedEntry {
        /// Single-entry original list excerpt, including nested content.
        block: mant_ir::Block,
    },
    /// Recovered adjacency supplies reading context, not proof that each
    /// sentence applies to every member or that members are interchangeable.
    DeclarationGroup {
        /// Exact containing list in the queried document snapshot.
        block_path: String,
        /// Half-open range in the original list, before response slicing.
        group: mant_ir::DeclarationGroup,
        /// Original members in source order; the last supplies the description.
        members: Vec<crate::OutlineTrail>,
        /// Complete original heads and final description. Local group indices
        /// are rebased; original item sources and identities remain unchanged.
        block: mant_ir::Block,
    },
    /// A nested declaration group already present in another returned source
    /// fragment. The descriptor preserves its own members and provider without
    /// copying their body again. References point directly to an owned fragment.
    ContainedDeclarationGroup {
        /// Original containing list in the document snapshot.
        block_path: String,
        /// Original member interval in the nested list.
        group: mant_ir::DeclarationGroup,
        /// Exact nested members, in source order.
        members: Vec<crate::OutlineTrail>,
        /// Index of a materialized `declaration-group` or `owned-entry`.
        support: usize,
        /// Typed path from that fragment's copied block to the nested list.
        path: Vec<super::ExplanationBlockStep>,
    },
}

impl ExplanationSupport {
    /// Validate the local/original ranges and exact member identities before
    /// accepting any reference. Context cannot silently point at another owner.
    #[must_use]
    pub fn items(&self) -> Option<&[mant_ir::DefinitionItem]> {
        let Self::DeclarationGroup {
            group,
            members,
            block,
            ..
        } = self
        else {
            return None;
        };
        let mant_ir::Block::DefinitionList {
            items,
            declaration_groups,
            ..
        } = block
        else {
            return None;
        };
        let width = group.end_item.checked_sub(group.start_item)?;
        let local = mant_ir::DeclarationGroup {
            start_item: 0,
            end_item: width,
        };
        if width > crate::MAX_EXPLANATION_RESULTS as usize
            || items.len() != width
            || members.len() != width
            || declaration_groups.as_slice() != [local]
            || items.iter().zip(members).any(|(item, member)| {
                mant_ir::EntryOwner::Definition(item)
                    .facts()
                    .is_none_or(|facts| facts.id.as_str() != member.node.id())
            })
        {
            return None;
        }
        local.resolve(items)
    }

    /// Resolve this group's original member interval, including a contained
    /// fragment. References never follow chains or cross document pools.
    #[must_use]
    pub fn items_in<'a>(&'a self, pool: &'a [Self]) -> Option<&'a [mant_ir::DefinitionItem]> {
        let (block, group) = self.fragment(pool)?;
        let mant_ir::Block::DefinitionList { items, .. } = block else {
            return None;
        };
        group.resolve(items)
    }

    /// Member trails belonging to this group, not its enclosing context.
    #[must_use]
    pub fn members(&self) -> &[crate::OutlineTrail] {
        match self {
            Self::OwnedEntry { .. } => &[],
            Self::DeclarationGroup { members, .. }
            | Self::ContainedDeclarationGroup { members, .. } => members,
        }
    }

    /// Materialized outer source block, if the complete descriptor is valid.
    /// Frontends render this block once and retain each group's own provenance.
    #[must_use]
    pub fn materialized<'a>(&'a self, pool: &'a [Self]) -> Option<&'a mant_ir::Block> {
        if let Self::OwnedEntry { block } = self {
            return block.entry_owner().map(|_| block);
        }
        self.fragment(pool)?;
        match self {
            Self::OwnedEntry { .. } => None,
            Self::DeclarationGroup { block, .. } => Some(block),
            Self::ContainedDeclarationGroup { support, .. } => match pool.get(*support)? {
                Self::DeclarationGroup { block, .. } | Self::OwnedEntry { block } => Some(block),
                Self::ContainedDeclarationGroup { .. } => None,
            },
        }
    }

    fn fragment<'a>(
        &'a self,
        pool: &'a [Self],
    ) -> Option<(&'a mant_ir::Block, mant_ir::DeclarationGroup)> {
        match self {
            Self::OwnedEntry { .. } => None,
            Self::DeclarationGroup { block, .. } => Some((
                block,
                mant_ir::DeclarationGroup {
                    start_item: 0,
                    end_item: self.items()?.len(),
                },
            )),
            Self::ContainedDeclarationGroup {
                group,
                members,
                support,
                path,
                ..
            } => {
                let parent = pool.get(*support)?;
                let block = match parent {
                    Self::DeclarationGroup { block, .. } => {
                        parent.items()?;
                        block
                    }
                    Self::OwnedEntry { block } => {
                        block.entry_owner()?;
                        block
                    }
                    Self::ContainedDeclarationGroup { .. } => return None,
                };
                if path.is_empty() {
                    return None;
                }
                let block = super::locations::block_at(block, path)?;
                let mant_ir::Block::DefinitionList {
                    items,
                    declaration_groups,
                    ..
                } = block
                else {
                    return None;
                };
                let items = group.resolve(items)?;
                if !declaration_groups.contains(group)
                    || items.len() != members.len()
                    || items.len() > crate::MAX_EXPLANATION_RESULTS as usize
                    || items.iter().zip(members).any(|(item, member)| {
                        item.entry
                            .as_ref()
                            .is_none_or(|facts| facts.id.as_str() != member.node.id())
                    })
                {
                    return None;
                }
                Some((block, *group))
            }
        }
    }
}

impl super::ExplanationContent {
    /// Resolve this source-qualified reference to its original physical owner.
    #[must_use]
    pub fn referenced_owner<'a>(
        &self,
        pool: &'a [ExplanationSupport],
    ) -> Option<mant_ir::EntryOwner<'a>> {
        if let Self::SharedEntry {
            support,
            path,
            item_index,
        } = self
        {
            let fragment = pool.get(*support)?;
            if matches!(
                fragment,
                ExplanationSupport::ContainedDeclarationGroup { .. }
            ) {
                return None;
            }
            let block = super::locations::block_at(fragment.materialized(pool)?, path)?;
            return match block {
                mant_ir::Block::DefinitionList { items, .. } => {
                    items.get(*item_index).map(mant_ir::EntryOwner::Definition)
                }
                mant_ir::Block::List { items, .. } => {
                    items.get(*item_index).map(mant_ir::EntryOwner::List)
                }
                _ => None,
            };
        }
        let Self::DeclarationMember {
            support,
            item_index,
        } = self
        else {
            return None;
        };
        Some(mant_ir::EntryOwner::Definition(
            pool.get(*support)?.items_in(pool)?.get(*item_index)?,
        ))
    }

    /// Resolve an owner-local position without copying the shared source body.
    #[must_use]
    pub fn resolve_range<'a>(
        &'a self,
        pool: &'a [ExplanationSupport],
        range: &super::ExplanationContentRange,
    ) -> Option<super::ExplanationTextRoot<'a>> {
        use super::{ExplanationBlockStep as Step, ExplanationContentRange as Range};
        match self {
            Self::SharedEntry {
                support,
                path,
                item_index,
            } => {
                self.referenced_owner(pool)?;
                remap_range(range, path, *item_index)?
                    .resolve(pool.get(*support)?.materialized(pool)?)
            }
            Self::Entry { block } | Self::Block { block } => range.resolve(block),
            Self::DeclarationMember {
                support,
                item_index,
            } => {
                self.referenced_owner(pool)?;
                let (block, group) = pool.get(*support)?.fragment(pool)?;
                let item_index = group.start_item.checked_add(*item_index)?;
                let mut mapped = range.clone();
                let path = match &mut mapped {
                    Range::DefinitionTerm {
                        path,
                        item_index: index,
                        ..
                    } if path.is_empty() => {
                        if *index != 0 {
                            return None;
                        }
                        *index = u32::try_from(item_index).ok()?;
                        return mapped.resolve(block);
                    }
                    Range::BlockText { path, .. } | Range::DefinitionTerm { path, .. } => path,
                };
                let Some(Step::DefinitionItem { index }) = path.first_mut() else {
                    return None;
                };
                if *index != 0 {
                    return None;
                }
                *index = u32::try_from(item_index).ok()?;
                mapped.resolve(block)
            }
        }
    }
}

impl super::ExplanationEvidence {
    /// Validated reference to returned original source, regardless of whether
    /// it also carries a declaration-group relationship.
    #[must_use]
    pub fn source_reference(&self, pool: &[ExplanationSupport]) -> Option<usize> {
        if self.covered_by_support(pool) {
            self.support
        } else {
            self.shared_entry(pool)
        }
    }
    /// Validated physical-entry reference, distinct from a group relationship.
    #[must_use]
    pub fn shared_entry(&self, pool: &[ExplanationSupport]) -> Option<usize> {
        let content @ super::ExplanationContent::SharedEntry { support, .. } =
            self.content.as_ref()?
        else {
            return None;
        };
        (self.class == super::EvidenceClass::DirectEntry
            && self.support.is_none()
            && !self.content_omitted
            && content
                .referenced_owner(pool)
                .is_some_and(|owner| self.matches_owner(owner)))
        .then_some(*support)
    }
    /// Whether a shared source fragment covers this exact owner and its forms.
    /// Invalid references must never hide separately returned metadata.
    #[must_use]
    pub fn covered_by_support(&self, pool: &[ExplanationSupport]) -> bool {
        let Some(content @ super::ExplanationContent::DeclarationMember { support, .. }) =
            &self.content
        else {
            return false;
        };
        self.support == Some(*support)
            && self.class == super::EvidenceClass::DirectEntry
            && !self.content_omitted
            && !self.support_omitted
            && content
                .referenced_owner(pool)
                .is_some_and(|owner| self.matches_owner(owner))
    }

    fn matches_owner(&self, owner: mant_ir::EntryOwner<'_>) -> bool {
        owner
            .facts()
            .is_some_and(|facts| facts.id.as_str() == self.outline.node.id())
            && owner.forms().is_some_and(|forms| {
                self.entry.as_ref().is_none_or(|entry| {
                    entry.forms.is_empty() || forms.iter().eq(entry.forms.iter().map(Vec::as_slice))
                })
            })
    }

    fn valid_references(&self, pool: &[ExplanationSupport]) -> bool {
        use super::{EvidenceBasis, ExplanationContent, ExplanationOccurrence};
        if matches!(self.content, Some(ExplanationContent::SharedEntry { .. }))
            && self.shared_entry(pool).is_none()
            || self.support_omitted && self.class != super::EvidenceClass::DirectEntry
            || self.content_omitted && self.content.is_some()
            || self.support.is_some() && !self.covered_by_support(pool)
            || self.support.is_some_and(|index| pool.get(index).is_none())
            || self.support.is_some() && self.support_omitted
            || matches!(
                self.content,
                Some(ExplanationContent::DeclarationMember { .. })
            ) && !self.covered_by_support(pool)
        {
            return false;
        }
        let valid_content = |range: &super::ExplanationContentRange| {
            self.content
                .as_ref()
                .is_some_and(|content| content.resolve_range(pool, range).is_some())
        };
        let valid_occurrence = |occurrence: &ExplanationOccurrence| {
            occurrence.forms.iter().all(|range| {
                self.entry
                    .as_ref()
                    .is_some_and(|entry| range.resolve(&entry.forms).is_some())
            }) && occurrence.content.iter().all(&valid_content)
        };
        self.bases.iter().all(|basis| match basis {
            EvidenceBasis::Name { matches } => matches
                .iter()
                .all(|m| m.occurrences.iter().all(&valid_occurrence)),
            EvidenceBasis::Form { matches } => matches
                .iter()
                .all(|m| m.occurrences.iter().all(&valid_occurrence)),
            _ => true,
        }) && self.entry.as_ref().is_none_or(|entry| {
            entry.name_bindings.iter().all(|binding| {
                (binding.name_index as usize) < entry.names.len()
                    && binding.occurrences.iter().all(&valid_occurrence)
            })
        }) && self
            .previews
            .iter()
            .all(|p| p.content_ranges.iter().all(&valid_content))
    }
}

fn valid_pool(pool: &[ExplanationSupport]) -> bool {
    pool.len() <= crate::MAX_EXPLANATION_RESULTS as usize
        && pool
            .iter()
            .all(|support| support.materialized(pool).is_some())
}

fn remap_range(
    range: &super::ExplanationContentRange,
    prefix: &[super::ExplanationBlockStep],
    owner: usize,
) -> Option<super::ExplanationContentRange> {
    use super::{ExplanationBlockStep as Step, ExplanationContentRange as Range};
    let mut mapped = range.clone();
    let owner = u32::try_from(owner).ok()?;
    let path = match &mut mapped {
        Range::DefinitionTerm {
            path, item_index, ..
        } if path.is_empty() => {
            if *item_index != 0 {
                return None;
            }
            *item_index = owner;
            path.extend_from_slice(prefix);
            return Some(mapped);
        }
        Range::BlockText { path, .. } | Range::DefinitionTerm { path, .. } => path,
    };
    let (Step::DefinitionItem { index } | Step::ListItem { index }) = path.first_mut()? else {
        return None;
    };
    if *index != 0 {
        return None;
    }
    *index = owner;
    path.splice(0..0, prefix.iter().copied());
    Some(mapped)
}

impl super::QueryExplanation {
    /// Validate page-local source references and returned position domains.
    /// Deserialization runs this check; in-memory producers can call it too.
    ///
    /// # Errors
    /// Returns an error for dangling, wrong-owner or out-of-bounds references.
    pub fn validate_references(&self) -> Result<(), &'static str> {
        (valid_pool(&self.supports)
            && self
                .evidence
                .iter()
                .all(|e| e.valid_references(&self.supports)))
        .then_some(())
        .ok_or("invalid explanation source reference or position")
    }
}

impl crate::ScopeExplanation {
    /// Validate references within their own document's pool, never another
    /// document with an equal-looking node ID.
    ///
    /// # Errors
    /// Returns an error for invalid document, support, owner or text positions.
    pub fn validate_references(&self) -> Result<(), &'static str> {
        (self.documents.iter().all(|d| valid_pool(&d.supports))
            && self.evidence.iter().all(|e| {
                self.documents
                    .get(e.document_index)
                    .is_some_and(|d| e.evidence.valid_references(&d.supports))
            }))
        .then_some(())
        .ok_or("invalid scoped explanation source reference or position")
    }
}