provenance-cli 0.2.3

Command-line interface for Provenance requirements traceability
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
//! Intermediate page model for the generated wiki.
//!
//! Every page is plain data with semantic sections; nothing here knows about
//! HTML. Cross-references between pages are typed [`PageLink`]s carrying the
//! target [`PageId`]. Missing data is surfaced as [`GapNotice`]s and orphaned
//! records as an [`OrphanReport`] rather than being smoothed over.

use crate::wiki::links::{EvidenceRef, InlineRef};
use provenance_core::{
    MessageRole, NodeType, RequirementStatus, ResolutionInputType, ResolutionStatus, RuleSeverity,
    RuleStatus, SourceType, ThreadStatus,
};
pub use provenance_store::cache::GapKind;
use serde::Serialize;

mod discovery;
pub use discovery::{
    DecisionIndexPage, DomainGroup, DomainIndexPage, DomainState, HomepageDomain,
    OpenQuestionNotice, SearchEntry, SearchIndexPage, UnfinishedPage, HOMEPAGE_DOMAIN_ROW_CAP,
};

/// A rendered page kind, including singleton discovery pages.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum PageKind {
    ScopeIndex,
    DomainIndex,
    SearchIndex,
    DecisionIndex,
    Unfinished,
    Requirement,
    Resolution,
    Rule,
    Source,
}

/// A persisted record kind. Singleton pages deliberately have no variant here.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RecordKind {
    Requirement,
    Resolution,
    Rule,
    Source,
}

impl From<RecordKind> for PageKind {
    fn from(kind: RecordKind) -> Self {
        match kind {
            RecordKind::Requirement => Self::Requirement,
            RecordKind::Resolution => Self::Resolution,
            RecordKind::Rule => Self::Rule,
            RecordKind::Source => Self::Source,
        }
    }
}

/// Identifies one persisted record page by kind and stable id.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct PageId {
    pub kind: RecordKind,
    pub record_id: String,
}

impl PageId {
    pub fn new(kind: RecordKind, record_id: impl Into<String>) -> Self {
        Self {
            kind,
            record_id: record_id.into(),
        }
    }

    /// The canonical route for this page, mirroring the docs server's
    /// trailing-slash convention.
    pub fn route(&self) -> String {
        crate::wiki::routes::WikiRoute::Record(self).path()
    }
}

/// A typed cross-reference to another wiki page.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct PageLink {
    pub target: PageId,
    pub title: String,
}

/// An honest hole in the graph, rendered as a first-class notice.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct GapNotice {
    pub kind: GapKind,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subject: Option<PageLink>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub related: Option<PageLink>,
    pub detail: String,
}

/// A source no requirement cites, carrying the gap's own words.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct OrphanRecord {
    pub link: PageLink,
    pub reason: String,
}

/// Records that exist but are attached to nothing, listed on the unfinished page.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct OrphanReport {
    pub sources: Vec<OrphanRecord>,
}

/// Record totals for the scope, shown on the index page.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
pub struct CorpusCounts {
    pub sources: usize,
    pub requirements: usize,
    pub resolutions: usize,
    pub rules: usize,
}

/// The scope index: search, domain discovery, record totals, and an
/// unfinished-work summary.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ScopeIndexPage {
    pub scope: String,
    pub title: String,
    pub counts: CorpusCounts,
    pub search_coverage: String,
    pub search_example: Option<String>,
    pub domains: Vec<HomepageDomain>,
    pub authored_domain_count: usize,
    pub unfinished_count: usize,
}

/// One step in a requirement's parent chain, root first; the final entry is
/// the page's own requirement.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct LineageEntry {
    pub link: PageLink,
    pub is_current: bool,
}

/// A resolution input rendered as a scholarly citation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct InputCitation {
    pub input_type: ResolutionInputType,
    pub summary: String,
    pub reference: EvidenceRef,
}

/// A requirement's source reference rendered as a numbered footnote
/// (numbering is the renderer's job).
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SourceCitation {
    pub link: PageLink,
    pub source_type: SourceType,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub clause: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reference: Option<EvidenceRef>,
}

/// A resolving decision embedded on a requirement page. The decision's
/// body is copied from its resolution record (the single place it lives);
/// the link points at the full resolution page.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct DecisionSection {
    pub link: PageLink,
    pub status: ResolutionStatus,
    pub position: String,
    pub rationale: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enforcement: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub confidence: Option<f64>,
    pub inputs: Vec<InputCitation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub made_by: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub approved_by: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub approved_at: Option<i64>,
}

/// A produced rule summarized on a territory card.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RuleCard {
    pub link: PageLink,
    pub statement: String,
    pub status: RuleStatus,
    pub severity: RuleSeverity,
    pub evidence: Vec<EvidenceRef>,
}

/// One message in an evidence thread (a field note).
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct FieldNote {
    pub message_id: String,
    pub role: MessageRole,
    pub created_at: i64,
    pub body: String,
    /// Linkable spans inside `body` (code refs and test-case names).
    pub refs: Vec<InlineRef>,
}

/// An evidence thread attached to a record, with its parent named so the
/// renderer can attribute borrowed threads (a requirement page also shows
/// its resolving decisions' threads).
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct EvidenceThread {
    pub thread_id: String,
    pub parent_type: NodeType,
    pub parent_id: String,
    pub status: ThreadStatus,
    pub messages: Vec<FieldNote>,
}

/// A requirement detail page.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct RequirementPage {
    pub id: PageId,
    pub title: String,
    pub status: RequirementStatus,
    pub statement: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fog: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub domain_id: Option<String>,
    pub domain_has_anchor: bool,
    /// The parent requirement; `None` means the back link targets the index.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub back_link: Option<PageLink>,
    pub lineage: Vec<LineageEntry>,
    pub decisions: Vec<DecisionSection>,
    pub produced_rules: Vec<RuleCard>,
    pub children: Vec<PageLink>,
    pub siblings: Vec<PageLink>,
    /// The requirements this one supersedes, and the ones it depends on.
    pub supersedes: Vec<PageLink>,
    pub depends_on: Vec<PageLink>,
    /// The requirement whose `supersedes` names this one.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub superseded_by: Option<PageLink>,
    pub sources: Vec<SourceCitation>,
    pub gaps: Vec<GapNotice>,
    pub threads: Vec<EvidenceThread>,
}

/// A resolution detail page.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ResolutionPage {
    pub id: PageId,
    pub title: String,
    pub status: ResolutionStatus,
    pub position: String,
    pub rationale: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enforcement: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub confidence: Option<f64>,
    pub inputs: Vec<InputCitation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub made_by: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub approved_by: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub approved_at: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub review_on: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub superseded_by: Option<PageLink>,
    pub resolves: Vec<PageLink>,
    pub spawned: Vec<PageLink>,
    pub produced_rules: Vec<RuleCard>,
    pub gaps: Vec<GapNotice>,
    pub threads: Vec<EvidenceThread>,
}

/// A production item claimed as a Rule's primary implementation by a scanner
/// relationship or canonical typed binding.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ImplementationBinding {
    pub symbol: Option<String>,
    pub location: EvidenceRef,
}

/// The code scan the binding sections were read from. `None` on a page built
/// without a scan report: such a page knows nothing about bindings and must
/// not report an absent one.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CodeScan {
    /// The commit the scan ran against, or `None` for an uncommitted tree.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub commit: Option<String>,
}

/// A scanned site claiming how a rule is verified.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct VerificationSite {
    pub method: String,
    pub symbol: Option<String>,
    pub location: EvidenceRef,
    pub outside_implementation_module: bool,
}

/// A rule detail page with its backward traceability chain.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RulePage {
    pub id: PageId,
    pub title: String,
    pub statement: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    pub status: RuleStatus,
    pub severity: RuleSeverity,
    /// The scan behind scanner-discovered implementations and verifications.
    /// Canonical typed bindings remain available when this is `None`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code_scan: Option<CodeScan>,
    pub implementations: Vec<ImplementationBinding>,
    pub verifications: Vec<VerificationSite>,
    /// The resolutions and requirements the rule names as producing it.
    pub produced_by: Vec<PageLink>,
    /// Upstream requirements reached through the producing records.
    pub requirements: Vec<PageLink>,
    /// Sources referencing those upstream requirements.
    pub sources: Vec<PageLink>,
    pub gaps: Vec<GapNotice>,
    pub threads: Vec<EvidenceThread>,
}

/// A source detail page.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SourcePage {
    pub id: PageId,
    pub title: String,
    pub source_type: SourceType,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reference: Option<EvidenceRef>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub commit_pin: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub effective_date: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub review_date: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub superseded_by: Option<PageLink>,
    pub referenced_requirements: Vec<PageLink>,
    pub gaps: Vec<GapNotice>,
    pub threads: Vec<EvidenceThread>,
}

/// Every page assembled for one scope.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct WikiCorpus {
    pub scope: String,
    pub index: ScopeIndexPage,
    pub domains: DomainIndexPage,
    pub search: SearchIndexPage,
    pub decisions: DecisionIndexPage,
    pub unfinished: UnfinishedPage,
    pub requirements: Vec<RequirementPage>,
    pub resolutions: Vec<ResolutionPage>,
    pub rules: Vec<RulePage>,
    pub sources: Vec<SourcePage>,
}

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

    #[test]
    fn singleton_pages_are_not_record_identities() {
        let singletons = [
            PageKind::ScopeIndex,
            PageKind::DomainIndex,
            PageKind::SearchIndex,
            PageKind::DecisionIndex,
            PageKind::Unfinished,
        ];
        assert_eq!(singletons.len(), 5);
    }

    #[test]
    fn page_id_routes_records_under_their_kind() {
        assert_eq!(
            PageId::new(RecordKind::Requirement, "req_split").route(),
            "/requirements/req_split/"
        );
        assert_eq!(
            PageId::new(RecordKind::Resolution, "res_split").route(),
            "/resolutions/res_split/"
        );
        assert_eq!(
            PageId::new(RecordKind::Rule, "rule_sah_inv_001").route(),
            "/rules/rule_sah_inv_001/"
        );
        assert_eq!(
            PageId::new(RecordKind::Source, "source_schads").route(),
            "/sources/source_schads/"
        );
    }
}