mif-rh 0.8.0

Compiled ontology resolution/review engine for research-harness-template corpora
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
//! Artifact-to-channel rendering (rht Category B, Story #293).
//!
//! Ports rht's `scripts/render-artifact.sh`: renders one typed Artifact
//! (`schemas/artifact.schema.json`) to an output channel. Three channels:
//! `report` (the canonical MIF Level-3 markdown report, write-then-validated
//! by `mif-project.sh`), `blog`, and `book` (published, MIF Level-1
//! channels — exempt from L3 conformance, no internal finding identity
//! leaks into their prose).

use serde_json::{Value, json};

use crate::error::MifRhError;
use crate::harness_markdown::{dedupe_sections, secblock, source_link_line};

/// The pre-resolved, caller-supplied inputs [`render_artifact`] needs
/// beyond the artifact JSON itself.
///
/// Path/version arithmetic the original script does against the live
/// filesystem (resolving `$OUT`'s repo-relative slug path, reading a
/// prior version to increment) stays a CLI-layer concern, not a
/// pure-rendering one.
pub struct RenderInputs<'a> {
    /// The parsed `artifact.json` document. Its `.namespace` field (or the
    /// `"harness/report"` default) is the report/blog/book namespace —
    /// derived internally, not a separate input.
    pub artifact: &'a Value,
    /// The output file's slug (its basename, minus `.md`).
    pub slug: &'a str,
    /// The output file's repo-root-relative path (for the `slug:`
    /// frontmatter field astro-rehype-relative-markdown-links needs).
    pub slugpath: &'a str,
    /// The RFC 3339 `created` timestamp.
    pub created: &'a str,
    /// This revision's version number (prior version + 1, or 1).
    pub version: u64,
    /// A falsification verdict (`extensions.harness.verification`) to fold
    /// into the `report` channel's frontmatter. Required for `report`;
    /// ignored for `blog`/`book`.
    pub verification: Option<&'a Value>,
}

/// Renders `inputs.artifact` to `channel` (`"report"`, `"blog"`, or
/// `"book"`), returning the complete markdown file contents (frontmatter +
/// body).
///
/// # Errors
///
/// Returns [`MifRhError::InvalidToggleValue`] if `channel` is not one of
/// the three recognized values.
pub fn render_artifact(inputs: &RenderInputs<'_>, channel: &str) -> Result<String, MifRhError> {
    match channel {
        "report" => render_report(inputs),
        "blog" => Ok(render_blog(inputs)),
        "book" => Ok(render_book(inputs)),
        other => Err(MifRhError::InvalidToggleValue {
            field: "channel".to_string(),
            value: other.to_string(),
            allowed: "report|blog|book".to_string(),
        }),
    }
}

fn namespace_of(artifact: &Value) -> &str {
    artifact
        .get("namespace")
        .and_then(Value::as_str)
        .unwrap_or("harness/report")
}

fn sections_of(artifact: &Value) -> Vec<Value> {
    let raw = artifact
        .get("sections")
        .and_then(Value::as_array)
        .cloned()
        .unwrap_or_default();
    dedupe_sections(&raw)
}

fn sources_of(artifact: &Value) -> Vec<Value> {
    artifact
        .get("sources")
        .and_then(Value::as_array)
        .cloned()
        .unwrap_or_default()
}

fn sources_list_block(artifact: &Value) -> Vec<String> {
    let mut lines = vec![String::new(), "## Sources".to_string(), String::new()];
    for source in &sources_of(artifact) {
        lines.push(source_link_line(source));
    }
    lines
}

/// Prefixes every line of `text` with `> ` so a multiline lede stays inside
/// one Markdown blockquote instead of only its first line.
fn blockquote_lines(text: &str) -> Vec<String> {
    text.trim()
        .lines()
        .map(|line| format!("> {line}"))
        .collect()
}

fn render_report(inputs: &RenderInputs<'_>) -> Result<String, MifRhError> {
    let artifact = inputs.artifact;
    let namespace = namespace_of(artifact);
    let genre = artifact
        .get("genre")
        .and_then(Value::as_str)
        .unwrap_or("general");
    let finding_count = artifact
        .get("finding_refs")
        .and_then(Value::as_array)
        .map_or(0, Vec::len);

    let mut body_lines = vec![format!(
        "This {genre} synthesis covers {finding_count} surviving finding(s) across the research."
    )];
    for section in &sections_of(artifact) {
        body_lines.extend(secblock(section, true, true));
    }
    body_lines.extend(sources_list_block(artifact));
    let body = body_lines.join("\n");

    let citations: Vec<Value> = sources_of(artifact)
        .iter()
        .map(|source| {
            let mut citation = json!({
                "@type": "Citation",
                "citationType": source.get("citationType"),
                "citationRole": source.get("citationRole"),
                "title": source.get("title"),
                "url": source.get("url"),
            });
            if let Some(note) = source.get("note") {
                citation["note"] = note.clone();
            }
            citation
        })
        .collect();

    let mut concept = json!({
        "@context": "https://mif-spec.dev/schema/context.jsonld",
        "@type": "Concept",
        "@id": format!("urn:mif:report:{namespace}:{}", inputs.slug),
        "slug": inputs.slugpath,
        "version": inputs.version,
        "conceptType": "semantic",
        "namespace": namespace,
        "title": artifact.get("title"),
        "genre": genre,
        "created": inputs.created,
        "provenance": {
            "@type": "Provenance",
            "sourceType": "system_generated",
            "confidence": 0.9,
            "trustLevel": "moderate_confidence",
        },
        "citations": citations,
        "extensions": { "harness": { "dimension": "synthesis" } },
    });
    if let Some(verification) = inputs.verification {
        concept["extensions"]["harness"]["verification"] = verification.clone();
    }
    if let Some(subtitle) = artifact.get("subtitle").and_then(Value::as_str) {
        concept["description"] = json!(subtitle);
    }

    // serde_json::Value's Serialize impl is format-agnostic, so serializing
    // it through serde_norway's Serializer produces the same YAML a
    // dedicated YAML type would, matching `yq -p=json -o=yaml`.
    let frontmatter_yaml = serde_norway::to_string(&concept)
        .map_err(|source| MifRhError::FrontmatterYamlSerialize { source })?;
    Ok(format!("---\n{frontmatter_yaml}---\n\n{body}\n"))
}

fn render_blog(inputs: &RenderInputs<'_>) -> String {
    let artifact = inputs.artifact;
    let namespace = namespace_of(artifact);
    let title = artifact
        .get("title")
        .and_then(Value::as_str)
        .unwrap_or_default();
    let subtitle = artifact.get("subtitle").and_then(Value::as_str);

    let mut lines = vec![
        "---".to_string(),
        "\"@context\": https://mif-spec.dev/schema/context.jsonld".to_string(),
        "\"@type\": Concept".to_string(),
        format!("\"@id\": urn:mif:blog:{namespace}:{}", inputs.slug),
        format!("slug: {}", inputs.slugpath),
        format!("version: {}", inputs.version),
        "conceptType: semantic".to_string(),
        format!("created: \"{}\"", inputs.created),
        format!("namespace: {namespace}"),
        "---".to_string(),
        String::new(),
        format!("# {title}"),
    ];
    if let Some(subtitle) = subtitle {
        lines.push(String::new());
        lines.extend(blockquote_lines(subtitle));
    }
    for section in &sections_of(artifact) {
        lines.extend(secblock(section, false, true));
    }
    lines.extend(sources_list_block(artifact));
    format!("{}\n", lines.join("\n"))
}

fn render_book(inputs: &RenderInputs<'_>) -> String {
    let artifact = inputs.artifact;
    let namespace = namespace_of(artifact);
    let title = artifact
        .get("title")
        .and_then(Value::as_str)
        .unwrap_or_default();
    let genre = artifact
        .get("genre")
        .and_then(Value::as_str)
        .unwrap_or("general");
    let audience = artifact
        .get("audience")
        .and_then(Value::as_str)
        .unwrap_or("general");
    let subtitle = artifact.get("subtitle").and_then(Value::as_str);

    let mut lines = vec![
        "---".to_string(),
        "\"@context\": https://mif-spec.dev/schema/context.jsonld".to_string(),
        "\"@type\": Concept".to_string(),
        format!("\"@id\": urn:mif:book:{namespace}:{}", inputs.slug),
        format!("slug: {}", inputs.slugpath),
        format!("version: {}", inputs.version),
        "conceptType: semantic".to_string(),
        format!("created: \"{}\"", inputs.created),
        format!("namespace: {namespace}"),
        "---".to_string(),
        String::new(),
        format!("# Chapter: {title}"),
    ];
    if let Some(subtitle) = subtitle {
        lines.push(String::new());
        lines.extend(blockquote_lines(subtitle));
    }
    lines.push(String::new());
    lines.push(format!("> Genre: {genre} · audience: {audience}"));
    for section in &sections_of(artifact) {
        lines.extend(secblock(section, false, false));
    }
    lines.push(String::new());
    lines.push("## Endnotes".to_string());
    lines.push(String::new());
    for (i, source) in sources_of(artifact).iter().enumerate() {
        let title = source
            .get("title")
            .and_then(Value::as_str)
            .unwrap_or_default();
        let title = title.trim_matches([' ', '\t']);
        let url = source
            .get("url")
            .and_then(Value::as_str)
            .unwrap_or_default();
        lines.push(format!("[{}] {title} — <{url}>", i + 1));
    }
    format!("{}\n", lines.join("\n"))
}

#[cfg(test)]
mod tests {
    use super::{RenderInputs, render_artifact};
    use serde_json::json;

    fn artifact() -> serde_json::Value {
        json!({
            "title": "Widget Synthesis",
            "genre": "landscape",
            "audience": "engineers",
            "namespace": "harness/widgets",
            "finding_refs": ["urn:mif:f1", "urn:mif:f2"],
            "sections": [
                {
                    "heading": "First finding",
                    "body": "Some prose about widgets.",
                    "dimension": "landscape",
                    "verdict": "survived",
                    "sources": [{"title": "Source A", "url": "https://a.example"}],
                },
            ],
            "sources": [{"title": "Source A", "url": "https://a.example"}],
        })
    }

    fn inputs(artifact: &serde_json::Value) -> RenderInputs<'_> {
        RenderInputs {
            artifact,
            slug: "widget-report",
            slugpath: "reports/widgets/widget-report",
            created: "2026-01-01T00:00:00Z",
            version: 1,
            verification: None,
        }
    }

    #[test]
    fn report_channel_produces_yaml_frontmatter_and_l3_concept_fields() {
        let art = artifact();
        let rendered = render_artifact(&inputs(&art), "report").unwrap();
        assert!(rendered.starts_with("---\n"));
        assert!(rendered.contains("'@id': urn:mif:report:harness/widgets:widget-report"));
        assert!(rendered.contains("conceptType: semantic"));
        assert!(rendered.contains("## First finding"));
        assert!(rendered.contains("## Sources"));
        // The report body carries no H1 — the title lives in frontmatter.
        assert!(!rendered.contains("\n# Widget Synthesis"));
        // No `subtitle` in the fixture — `description` is omitted, not null.
        assert!(!rendered.contains("description:"));
    }

    #[test]
    fn blog_channel_carries_a_body_h1_and_no_dimension_meta_line() {
        let art = artifact();
        let rendered = render_artifact(&inputs(&art), "blog").unwrap();
        assert!(rendered.contains("# Widget Synthesis"));
        assert!(!rendered.contains("_Dimension:"));
        assert!(rendered.contains("Evidence:"));
        // No `subtitle` in the fixture — no lede blockquote under the H1.
        assert!(!rendered.contains("# Widget Synthesis\n\n>"));
    }

    #[test]
    fn book_channel_has_chapter_heading_and_numbered_endnotes_not_inline_evidence() {
        let art = artifact();
        let rendered = render_artifact(&inputs(&art), "book").unwrap();
        assert!(rendered.contains("# Chapter: Widget Synthesis"));
        assert!(rendered.contains("> Genre: landscape · audience: engineers"));
        assert!(rendered.contains("## Endnotes"));
        assert!(rendered.contains("[1] Source A"));
        assert!(!rendered.contains("Evidence:"));
        // No `subtitle` in the fixture — the genre/audience line is the
        // first blockquote right after the H1, no separate lede above it.
        assert!(rendered.contains("# Chapter: Widget Synthesis\n\n> Genre:"));
    }

    #[test]
    fn report_channel_projects_subtitle_into_description_frontmatter_when_present() {
        let mut art = artifact();
        art["subtitle"] = json!("The BLUF sentence a reader needs most.");
        let rendered = render_artifact(&inputs(&art), "report").unwrap();
        assert!(rendered.contains("description: The BLUF sentence a reader needs most."));
    }

    #[test]
    fn blog_channel_carries_a_lede_blockquote_when_subtitle_present() {
        let mut art = artifact();
        art["subtitle"] = json!("The BLUF sentence a reader needs most.");
        let rendered = render_artifact(&inputs(&art), "blog").unwrap();
        assert!(
            rendered.contains("# Widget Synthesis\n\n> The BLUF sentence a reader needs most.")
        );
    }

    #[test]
    fn book_channel_carries_a_lede_blockquote_when_subtitle_present() {
        let mut art = artifact();
        art["subtitle"] = json!("The BLUF sentence a reader needs most.");
        let rendered = render_artifact(&inputs(&art), "book").unwrap();
        assert!(rendered.contains(
            "# Chapter: Widget Synthesis\n\n> The BLUF sentence a reader needs most.\n\n> Genre:"
        ));
    }

    #[test]
    fn blog_channel_prefixes_every_line_of_a_multiline_subtitle() {
        let mut art = artifact();
        art["subtitle"] = json!("First line.\nSecond line.");
        let rendered = render_artifact(&inputs(&art), "blog").unwrap();
        assert!(rendered.contains("# Widget Synthesis\n\n> First line.\n> Second line."));
    }

    #[test]
    fn book_channel_prefixes_every_line_of_a_multiline_subtitle() {
        let mut art = artifact();
        art["subtitle"] = json!("First line.\nSecond line.");
        let rendered = render_artifact(&inputs(&art), "book").unwrap();
        assert!(
            rendered.contains(
                "# Chapter: Widget Synthesis\n\n> First line.\n> Second line.\n\n> Genre:"
            )
        );
    }

    #[test]
    fn rejects_an_unknown_channel() {
        let art = artifact();
        let error = render_artifact(&inputs(&art), "podcast").unwrap_err();
        assert!(matches!(
            error,
            super::MifRhError::InvalidToggleValue { .. }
        ));
    }

    #[test]
    fn report_channel_folds_in_a_supplied_verification_verdict() {
        let art = artifact();
        let verification = json!({"verdict": "survived", "attempted_at": "2026-01-01"});
        let mut opts = inputs(&art);
        opts.verification = Some(&verification);
        let rendered = render_artifact(&opts, "report").unwrap();
        assert!(rendered.contains("verdict: survived"));
    }
}