mif-rh 0.6.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
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
//! Topic README metadata rollup (rht Category B, Story #282).
//!
//! Ports the jq-dependent data computation from
//! `scripts/build-topic-readme.sh`: the topic's registered title/status, a
//! findings rollup (counts, verdicts, sources, dimensions, tags,
//! earliest-created date, per-dimension counts, a draft "Key Findings"
//! excerpt), a dimension bullet list, and a purpose line. Everything else
//! in that script (file scanning, title/genre/version extraction from
//! rendered deliverables, markdown table assembly, the structural check
//! gate, the atomic write) stays pure bash — it never used jq.

use std::collections::BTreeMap;
use std::path::Path;

use serde_json::Value;

use crate::error::MifRhError;
use crate::harness_project::read_json;

/// Everything `build-topic-readme.sh` previously computed via `jq`, ready
/// to be emitted as shell variable assignments.
#[derive(Debug, Clone)]
pub struct TopicMetadata {
    /// The topic's registered title (falls back to the topic id).
    pub title: String,
    /// The topic's registered status (falls back to `"active"`).
    pub status: String,
    /// Total finding count.
    pub count: usize,
    /// Unique non-null citation URLs across every finding.
    pub sources: usize,
    /// The earliest non-null `created` value across findings, or empty.
    pub created: String,
    /// Count of findings with verdict `survived`.
    pub survived: usize,
    /// Count of findings with verdict `weakened`.
    pub weakened: usize,
    /// Count of findings with verdict `inconclusive`.
    pub inconclusive: usize,
    /// Count of findings with verdict `falsified`.
    pub falsified: usize,
    /// Pre-rendered `- **dim** — desc` bullet list (or `"—"` if empty).
    pub dim_bullets: String,
    /// Pre-rendered backtick-quoted tag list (or `"—"` if empty).
    pub tags: String,
    /// The goal statement, or a generic fallback naming the title.
    pub purpose: String,
    /// Pre-rendered `- <summary>` draft bullets (up to 8), survived-first.
    pub key_draft: String,
    /// Pre-rendered `| dim | count |` markdown table rows.
    pub by_dim_table: String,
}

fn escape_shell_single_quoted(value: &str) -> String {
    format!("'{}'", value.replace('\'', "'\\''"))
}

impl TopicMetadata {
    /// Renders every field as a `NAME='value'` shell assignment, one per
    /// line, suitable for `source <(...)` from bash.
    #[must_use]
    pub fn to_shell_script(&self) -> String {
        let mut lines = Vec::new();
        lines.push(format!("TITLE={}", escape_shell_single_quoted(&self.title)));
        lines.push(format!(
            "STATUS={}",
            escape_shell_single_quoted(&self.status)
        ));
        lines.push(format!("COUNT={}", self.count));
        lines.push(format!("SOURCES={}", self.sources));
        lines.push(format!(
            "CREATED={}",
            escape_shell_single_quoted(&self.created)
        ));
        lines.push(format!("SURV={}", self.survived));
        lines.push(format!("WEAK={}", self.weakened));
        lines.push(format!("INC={}", self.inconclusive));
        lines.push(format!("FALS={}", self.falsified));
        lines.push(format!(
            "DIM_BULLETS={}",
            escape_shell_single_quoted(&self.dim_bullets)
        ));
        lines.push(format!("TAGS={}", escape_shell_single_quoted(&self.tags)));
        lines.push(format!(
            "PURPOSE={}",
            escape_shell_single_quoted(&self.purpose)
        ));
        lines.push(format!(
            "KEY_DRAFT={}",
            escape_shell_single_quoted(&self.key_draft)
        ));
        lines.push(format!(
            "BY_DIM_TABLE={}",
            escape_shell_single_quoted(&self.by_dim_table)
        ));
        lines.join("\n")
    }
}

fn unique_sorted_strings(values: impl Iterator<Item = Option<String>>) -> Vec<String> {
    let mut set: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
    for value in values.flatten() {
        set.insert(value);
    }
    set.into_iter().collect()
}

struct Roll {
    count: usize,
    sources: usize,
    dimensions: Vec<String>,
    tags: Vec<String>,
    created: Option<String>,
    verdicts: BTreeMap<String, usize>,
    by_dim: Vec<(String, usize)>,
    key: Vec<String>,
}

fn compute_roll(findings: &[Value]) -> Roll {
    if findings.is_empty() {
        return Roll {
            count: 0,
            sources: 0,
            dimensions: Vec::new(),
            tags: Vec::new(),
            created: None,
            verdicts: BTreeMap::new(),
            by_dim: Vec::new(),
            key: Vec::new(),
        };
    }

    let sources = unique_sorted_strings(
        findings
            .iter()
            .flat_map(|f| {
                f.get("citations")
                    .and_then(Value::as_array)
                    .into_iter()
                    .flatten()
            })
            .map(|c| c.get("url").and_then(Value::as_str).map(str::to_string)),
    )
    .len();

    let dimensions = unique_sorted_strings(findings.iter().map(|f| {
        f.pointer("/extensions/harness/dimension")
            .and_then(Value::as_str)
            .map(str::to_string)
    }));

    let tags = unique_sorted_strings(
        findings
            .iter()
            .flat_map(|f| {
                f.get("tags")
                    .and_then(Value::as_array)
                    .into_iter()
                    .flatten()
            })
            .map(|t| t.as_str().map(str::to_string)),
    );

    let mut created_values: Vec<String> = findings
        .iter()
        .filter_map(|f| f.get("created").and_then(Value::as_str).map(str::to_string))
        .collect();
    created_values.sort();
    let created = created_values.into_iter().next();

    let verdicts = count_verdicts(findings);
    let by_dim = count_by_dimension(findings);
    let key = draft_key_findings(findings);

    Roll {
        count: findings.len(),
        sources,
        dimensions,
        tags,
        created,
        verdicts,
        by_dim,
        key,
    }
}

fn verdict_of(finding: &Value) -> &str {
    finding
        .pointer("/extensions/harness/verification/verdict")
        .and_then(Value::as_str)
        .unwrap_or("")
}

fn count_verdicts(findings: &[Value]) -> BTreeMap<String, usize> {
    let mut verdicts: BTreeMap<String, usize> = BTreeMap::new();
    for finding in findings {
        let verdict = match verdict_of(finding) {
            "" => "none",
            v => v,
        };
        *verdicts.entry(verdict.to_string()).or_insert(0) += 1;
    }
    verdicts
}

fn count_by_dimension(findings: &[Value]) -> Vec<(String, usize)> {
    let mut counts: BTreeMap<String, usize> = BTreeMap::new();
    for finding in findings {
        let dim = finding
            .pointer("/extensions/harness/dimension")
            .and_then(Value::as_str)
            .unwrap_or("unspecified")
            .to_string();
        *counts.entry(dim).or_insert(0) += 1;
    }
    let mut by_dim: Vec<(String, usize)> = counts.into_iter().collect();
    by_dim.sort_by_key(|(_, count)| std::cmp::Reverse(*count));
    by_dim
}

/// The up-to-8 survived/weakened finding summaries seeding the "Key
/// Findings" draft, survived-first (stable — ties preserve the findings'
/// own sorted-filename order).
fn draft_key_findings(findings: &[Value]) -> Vec<String> {
    let mut candidates: Vec<&Value> = findings
        .iter()
        .filter(|f| matches!(verdict_of(f), "survived" | "weakened"))
        .collect();
    candidates.sort_by_key(|f| verdict_of(f) != "survived");
    candidates
        .into_iter()
        .filter_map(|f| {
            f.get("summary")
                .and_then(Value::as_str)
                .or_else(|| f.get("title").and_then(Value::as_str))
        })
        .map(str::to_string)
        .take(8)
        .collect()
}

fn render_dim_bullets(roll_dimensions: &[String], goal: &Value, config: &Value) -> String {
    let goal_dimensions = goal
        .get("dimensions")
        .and_then(Value::as_array)
        .into_iter()
        .flatten()
        .filter_map(|v| v.as_str().map(str::to_string));
    let dims = unique_sorted_strings(
        goal_dimensions
            .chain(roll_dimensions.iter().cloned())
            .map(Some),
    );
    if dims.is_empty() {
        return "".to_string();
    }
    let config_dims: &[Value] = config
        .get("dimensions")
        .and_then(Value::as_array)
        .map_or(&[], Vec::as_slice);
    dims.iter()
        .map(|dim| {
            let description = config_dims
                .iter()
                .find(|entry| {
                    entry.get("id").and_then(Value::as_str) == Some(dim.as_str())
                        || entry.get("name").and_then(Value::as_str) == Some(dim.as_str())
                })
                .and_then(|entry| entry.get("description"))
                .and_then(Value::as_str);
            match description {
                Some(desc) if !desc.is_empty() => format!("- **{dim}** — {desc}"),
                _ => format!("- **{dim}**"),
            }
        })
        .collect::<Vec<_>>()
        .join("\n")
}

fn render_tags(tags: &[String]) -> String {
    if tags.is_empty() {
        "".to_string()
    } else {
        tags.iter()
            .map(|t| format!("`{t}`"))
            .collect::<Vec<_>>()
            .join(" ")
    }
}

fn render_purpose(goal: &Value, title: &str) -> String {
    for key in ["goal_statement", "research_question", "goal", "question"] {
        if let Some(value) = goal.get(key).and_then(Value::as_str)
            && !value.is_empty()
        {
            return value.to_string();
        }
    }
    format!("Research session for {title}.")
}

/// Computes a topic's README metadata rollup.
///
/// # Errors
///
/// Returns [`MifRhError::TopicNotRegistered`] if `topic` has no entry in
/// `config_path`'s `topics[]`, or [`MifRhError::Io`]/[`MifRhError::Json`] if
/// a finding file under `findings_dir` cannot be read or parsed.
pub fn topic_metadata(
    topic: &str,
    config_path: &Path,
    findings_dir: &Path,
    goal_path: &Path,
) -> Result<TopicMetadata, MifRhError> {
    let config = read_json(config_path)?;
    let topic_entry = config
        .get("topics")
        .and_then(Value::as_array)
        .into_iter()
        .flatten()
        .find(|t| t.get("id").and_then(Value::as_str) == Some(topic))
        .cloned()
        .ok_or_else(|| MifRhError::TopicNotRegistered {
            topic: topic.to_string(),
            config_path: config_path.display().to_string(),
        })?;
    let title = topic_entry
        .get("title")
        .and_then(Value::as_str)
        .map_or_else(|| topic.to_string(), str::to_string);
    let status = topic_entry
        .get("status")
        .and_then(Value::as_str)
        .unwrap_or("active")
        .to_string();

    let mut paths: Vec<_> = std::fs::read_dir(findings_dir)
        .into_iter()
        .flatten()
        .flatten()
        .map(|entry| entry.path())
        .filter(|path| path.extension().is_some_and(|ext| ext == "json"))
        .collect();
    paths.sort();
    let findings: Vec<Value> = paths
        .iter()
        .map(|path| read_json(path))
        .collect::<Result<Vec<_>, _>>()?;

    let roll = compute_roll(&findings);
    let goal = read_json(goal_path).unwrap_or_else(|_| Value::Object(serde_json::Map::new()));

    let dim_bullets = render_dim_bullets(&roll.dimensions, &goal, &config);
    let tags = render_tags(&roll.tags);
    let purpose = render_purpose(&goal, &title);
    let key_draft = roll
        .key
        .iter()
        .map(|line| format!("- {line}"))
        .collect::<Vec<_>>()
        .join("\n");
    let by_dim_table = roll
        .by_dim
        .iter()
        .map(|(dim, count)| format!("| {dim} | {count} |"))
        .collect::<Vec<_>>()
        .join("\n");

    Ok(TopicMetadata {
        title,
        status,
        count: roll.count,
        sources: roll.sources,
        created: roll.created.unwrap_or_default(),
        survived: roll.verdicts.get("survived").copied().unwrap_or(0),
        weakened: roll.verdicts.get("weakened").copied().unwrap_or(0),
        inconclusive: roll.verdicts.get("inconclusive").copied().unwrap_or(0),
        falsified: roll.verdicts.get("falsified").copied().unwrap_or(0),
        dim_bullets,
        tags,
        purpose,
        key_draft,
        by_dim_table,
    })
}

#[cfg(test)]
mod tests {
    use super::topic_metadata;
    use std::fs;

    fn setup(
        dir: &std::path::Path,
    ) -> (std::path::PathBuf, std::path::PathBuf, std::path::PathBuf) {
        let config_path = dir.join("harness.config.json");
        fs::write(
            &config_path,
            r#"{"topics": [{"id": "t1", "title": "Topic One", "status": "active"}],
                "dimensions": [{"id": "landscape", "description": "Market landscape"}]}"#,
        )
        .unwrap();
        let findings_dir = dir.join("findings");
        fs::create_dir_all(&findings_dir).unwrap();
        let goal_path = dir.join("goal.json");
        (config_path, findings_dir, goal_path)
    }

    #[test]
    fn errors_when_the_topic_is_not_registered() {
        let dir = tempfile::tempdir().unwrap();
        let (config_path, findings_dir, goal_path) = setup(dir.path());

        let error = topic_metadata("nope", &config_path, &findings_dir, &goal_path).unwrap_err();
        assert!(matches!(
            error,
            super::MifRhError::TopicNotRegistered { .. }
        ));
    }

    #[test]
    fn computes_counts_and_falls_back_title_purpose_with_no_findings() {
        let dir = tempfile::tempdir().unwrap();
        let (config_path, findings_dir, goal_path) = setup(dir.path());

        let meta = topic_metadata("t1", &config_path, &findings_dir, &goal_path).unwrap();
        assert_eq!(meta.title, "Topic One");
        assert_eq!(meta.status, "active");
        assert_eq!(meta.count, 0);
        assert_eq!(meta.sources, 0);
        assert_eq!(meta.dim_bullets, "");
        assert_eq!(meta.tags, "");
        assert_eq!(meta.purpose, "Research session for Topic One.");
        assert_eq!(meta.key_draft, "");
    }

    #[test]
    fn rolls_up_verdicts_sources_and_key_findings() {
        let dir = tempfile::tempdir().unwrap();
        let (config_path, findings_dir, goal_path) = setup(dir.path());
        fs::write(
            findings_dir.join("f1.json"),
            r#"{"summary": "Finding one survives", "created": "2026-01-02",
                "citations": [{"url": "https://a"}, {"url": "https://a"}],
                "tags": ["alpha", "beta"],
                "extensions": {"harness": {"dimension": "landscape",
                    "verification": {"verdict": "survived"}}}}"#,
        )
        .unwrap();
        fs::write(
            findings_dir.join("f2.json"),
            r#"{"summary": "Finding two is falsified", "created": "2026-01-01",
                "extensions": {"harness": {"dimension": "market",
                    "verification": {"verdict": "falsified"}}}}"#,
        )
        .unwrap();

        let meta = topic_metadata("t1", &config_path, &findings_dir, &goal_path).unwrap();
        assert_eq!(meta.count, 2);
        assert_eq!(meta.sources, 1);
        assert_eq!(meta.survived, 1);
        assert_eq!(meta.falsified, 1);
        assert_eq!(meta.created, "2026-01-01");
        assert_eq!(meta.tags, "`alpha` `beta`");
        assert_eq!(
            meta.dim_bullets,
            "- **landscape** — Market landscape\n- **market**"
        );
        assert_eq!(meta.key_draft, "- Finding one survives");
    }

    #[test]
    fn purpose_prefers_the_goal_statement_over_the_fallback() {
        let dir = tempfile::tempdir().unwrap();
        let (config_path, findings_dir, goal_path) = setup(dir.path());
        fs::write(
            &goal_path,
            r#"{"goal_statement": "Understand the market."}"#,
        )
        .unwrap();

        let meta = topic_metadata("t1", &config_path, &findings_dir, &goal_path).unwrap();
        assert_eq!(meta.purpose, "Understand the market.");
    }

    #[test]
    fn shell_script_escapes_embedded_single_quotes() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(
            dir.path().join("harness.config.json"),
            r#"{"topics": [{"id": "t1", "title": "Bob's Topic"}]}"#,
        )
        .unwrap();
        let findings_dir = dir.path().join("findings");
        fs::create_dir_all(&findings_dir).unwrap();

        let meta = topic_metadata(
            "t1",
            &dir.path().join("harness.config.json"),
            &findings_dir,
            &dir.path().join("goal.json"),
        )
        .unwrap();
        let script = meta.to_shell_script();
        assert!(script.contains(r"TITLE='Bob'\''s Topic'"));
    }
}