organism-planning 1.9.3

Planning layer for Organism — huddle, debate loop, plan annotations
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//! Generic knowledge base writer for DD results.
//!
//! Writes convergent DD output to an Obsidian-compatible vault structure.
//! Apps parameterize the writer via `KbConfig` to control directory names,
//! hub categories, and root page definitions.

use std::collections::HashSet;
use std::fmt::Write;
use std::path::Path;

use chrono::Utc;
use converge_pack::{ContextFact, ContextKey};

use crate::dd::{DdHooks, HookPatterns, consolidate_dd_hypotheses, extract_hooks_from_facts};

fn fact_text(fact: &ContextFact) -> &str {
    fact.text().unwrap_or_default()
}

// ── Configuration ────────────────────────────────────────────────

/// Controls the vault directory structure and page layout.
#[derive(Debug, Clone)]
pub struct KbConfig {
    /// Top-level directory for subjects (e.g. "Companies", "Targets", "Deals").
    pub entity_dir: String,
    /// Hub categories to generate (Business Areas, Regions, etc.).
    pub hub_categories: Vec<HubCategory>,
    /// Root MOC pages to auto-generate.
    pub root_pages: Vec<RootPageDef>,
    /// Hook extraction patterns. Uses defaults if None.
    pub hook_patterns: Option<HookPatterns>,
}

#[derive(Debug, Clone)]
pub struct HubCategory {
    pub dir_name: String,
    pub display_name: String,
}

#[derive(Debug, Clone)]
pub struct RootPageDef {
    pub filename: String,
    pub title: String,
    pub scan_dirs: Vec<String>,
    pub children_are_dirs: bool,
}

// ── Vault Writer ─────────────────────────────────────────────────

/// Write convergent DD results to an Obsidian vault.
///
/// Returns the list of written file paths (relative to `vault_root`).
#[allow(clippy::too_many_lines)]
pub fn write_dd_to_vault(
    vault_root: &Path,
    subject: &str,
    result: &converge_kernel::ConvergeResult,
    config: &KbConfig,
) -> anyhow::Result<Vec<String>> {
    let slug = slugify(subject);
    let base = vault_root.join(&config.entity_dir).join(&slug);
    let sources_dir = base.join("Sources");
    let analysis_dir = base.join("Analysis");

    std::fs::create_dir_all(&sources_dir)?;
    std::fs::create_dir_all(&analysis_dir)?;

    let now = Utc::now().format("%Y-%m-%d %H:%M UTC").to_string();
    let corr = format!("convergent-dd:{slug}");
    let mut written_files = Vec::new();

    let signals = result.context.get(ContextKey::Signals);
    let hypotheses = result.context.get(ContextKey::Hypotheses);
    let evaluations = result.context.get(ContextKey::Evaluations);
    let proposals = result.context.get(ContextKey::Proposals);

    let consolidated = consolidate_dd_hypotheses(hypotheses);

    // ── Source pages ─────────────────────────────────────────────────

    let mut seen_urls = HashSet::new();
    let mut deduped_signals = Vec::new();
    for signal in signals {
        if let Ok(v) = serde_json::from_str::<serde_json::Value>(fact_text(signal)) {
            let url = v["url"].as_str().unwrap_or("").to_string();
            if seen_urls.insert(url) {
                deduped_signals.push(signal);
            }
        }
    }

    for (i, signal) in deduped_signals.iter().enumerate() {
        let v: serde_json::Value = serde_json::from_str(fact_text(signal)).unwrap_or_default();
        let title_raw = v["title"].as_str().unwrap_or("Untitled");
        let url = v["url"].as_str().unwrap_or("");
        let content = v["content"].as_str().unwrap_or("");
        let provider = v["provider"].as_str().unwrap_or("unknown");
        let query = v["query"].as_str().unwrap_or("");
        let title = sanitize_filename(title_raw);
        let filename = format!("Source {:03}{}.md", i + 1, title);

        let page = format!(
            "---\ntags: [source, {provider}, {entity_tag}]\n\
             provenance: fact\ncorrelation_id: {corr}\n\
             provider: {provider}\nurl: {url}\n\
             query: \"{query}\"\ngenerated_at: {now}\n---\n\
             # {title_raw}\n\n\
             > Source: [{provider}]({url})\n> Query: `{query}`\n\n{content}\n",
            entity_tag = slug,
            query = query.replace('"', "'"),
        );

        std::fs::write(sources_dir.join(&filename), &page)?;
        written_files.push(format!(
            "{entity_dir}/{slug}/Sources/{filename}",
            entity_dir = config.entity_dir
        ));
    }

    // ── Facts page ───────────────────────────────────────────────────

    let mut facts_body = String::new();
    for (i, fact) in consolidated.iter().enumerate() {
        let conf_label = if fact.confidence >= 0.9 {
            "HIGH"
        } else if fact.confidence >= 0.7 {
            "MEDIUM"
        } else {
            "LOW"
        };

        let support = if fact.support_count > 1 {
            format!(" (corroborated by {} sources)", fact.support_count)
        } else {
            String::new()
        };

        let _ = write!(
            facts_body,
            "### Fact {num}\n\n\
             **Category:** `{cat}`\n\
             **Confidence:** {conf_label} ({conf:.0}%){support}\n\
             **Claim:** {claim}\n\n---\n\n",
            num = i + 1,
            cat = fact.category,
            conf = fact.confidence * 100.0,
            claim = fact.claim,
        );
    }

    let facts_page = format!(
        "---\ntags: [facts, {entity_tag}]\nprovenance: inferred\n\
         correlation_id: {corr}\ngenerated_at: {now}\n---\n\
         # {subject} — Tagged Facts\n\n\
         > Extracted via convergent DD ({cycles} cycles, converged: {converged}).\n\
         > {n_raw} raw facts consolidated to {n_consolidated} unique claims.\n\
         > Provenance: **inferred** by LLM from source data.\n\n\
         {facts_body}",
        entity_tag = slug,
        cycles = result.cycles,
        converged = result.converged,
        n_raw = hypotheses.len(),
        n_consolidated = consolidated.len(),
    );
    std::fs::write(base.join("Facts.md"), &facts_page)?;
    written_files.push(format!("{}/{slug}/Facts.md", config.entity_dir));

    // ── Analysis pages ───────────────────────────────────────────────

    let synthesis: Option<serde_json::Value> = proposals
        .iter()
        .find_map(|p| serde_json::from_str(fact_text(p)).ok());

    let analysis_sections = [
        ("Market", "market_analysis", "market"),
        ("Competition", "competitive_landscape", "competition"),
        ("Technology", "technology_assessment", "technology"),
    ];

    for (name, json_key, category) in &analysis_sections {
        let content = synthesis
            .as_ref()
            .and_then(|s| s[json_key].as_str())
            .map_or_else(
                || {
                    let matching: Vec<String> = consolidated
                        .iter()
                        .filter(|f| {
                            f.category == *category
                                || (*category == "competition" && f.category == "competitors")
                        })
                        .map(|f| format!("- {}", f.claim))
                        .collect();
                    if matching.is_empty() {
                        format!("*No {name} data collected during this run.*")
                    } else {
                        format!(
                            "*Synthesis did not complete. Facts in this category:*\n\n{}",
                            matching.join("\n")
                        )
                    }
                },
                String::from,
            );

        let cat_tag = category;
        let entity_tag = &slug;
        let page = format!(
            "---\ntags: [analysis, {cat_tag}, {entity_tag}]\n\
             provenance: inferred\ncorrelation_id: {corr}\n\
             generated_at: {now}\n---\n\
             # {subject}{name} Analysis\n\n\
             > Synthesised from convergent DD facts.\n\
             > Provenance: **inferred**. Verify claims against [[Facts]] source links.\n\n\
             {content}\n",
        );
        std::fs::write(analysis_dir.join(format!("{name}.md")), &page)?;
        written_files.push(format!("{}/{slug}/Analysis/{name}.md", config.entity_dir));
    }

    // ── Risks page ───────────────────────────────────────────────────

    let risks_body = synthesis
        .as_ref()
        .and_then(|s| s["risk_factors"].as_array())
        .map_or_else(
            || "*No risk factors synthesised.*".to_string(),
            |arr| {
                arr.iter()
                    .filter_map(|v| v.as_str().map(|s| format!("- {s}")))
                    .collect::<Vec<_>>()
                    .join("\n")
            },
        );

    let entity_tag = &slug;
    let risks_page = format!(
        "---\ntags: [risks, {entity_tag}]\nprovenance: inferred\n\
         correlation_id: {corr}\ngenerated_at: {now}\n---\n\
         # {subject} — Risk Factors\n\n\
         > Synthesised from convergent DD.\n\
         > Provenance: **inferred**. Verify against source data.\n\n\
         {risks_body}\n",
    );
    std::fs::write(base.join("Risks.md"), &risks_page)?;
    written_files.push(format!("{}/{slug}/Risks.md", config.entity_dir));

    // ── Opportunities page ───────────────────────────────────────────

    let opps_body = synthesis
        .as_ref()
        .and_then(|s| s["growth_opportunities"].as_array())
        .map_or_else(
            || "*No growth opportunities synthesised.*".to_string(),
            |arr| {
                arr.iter()
                    .filter_map(|v| v.as_str().map(|s| format!("- {s}")))
                    .collect::<Vec<_>>()
                    .join("\n")
            },
        );

    let opps_page = format!(
        "---\ntags: [opportunities, {entity_tag}]\nprovenance: inferred\n\
         correlation_id: {corr}\ngenerated_at: {now}\n---\n\
         # {subject} — Growth Opportunities\n\n\
         > Synthesised from convergent DD.\n\
         > Provenance: **inferred**. Verify against source data.\n\n\
         {opps_body}\n",
    );
    std::fs::write(base.join("Opportunities.md"), &opps_page)?;
    written_files.push(format!("{}/{slug}/Opportunities.md", config.entity_dir));

    // ── Contradictions page ──────────────────────────────────────────

    if !evaluations.is_empty() {
        let contra_body: String = evaluations
            .iter()
            .enumerate()
            .filter_map(|(i, eval)| {
                let v: serde_json::Value = serde_json::from_str(fact_text(eval)).ok()?;
                if v["type"].as_str() != Some("contradiction") {
                    return None;
                }
                let cat = v["category"].as_str().unwrap_or("unknown");
                let desc = v["description"].as_str().unwrap_or_else(|| fact_text(eval));
                let needs_review = v["needs_human_review"].as_bool().unwrap_or(false);
                let review_tag = if needs_review {
                    " **NEEDS REVIEW**"
                } else {
                    ""
                };
                Some(format!(
                    "### Contradiction {num}\n\n\
                     **Category:** {cat}\n\
                     **Description:** {desc}{review_tag}\n\n---\n",
                    num = i + 1,
                ))
            })
            .collect();

        if !contra_body.is_empty() {
            let entity_tag = &slug;
            let contra_page = format!(
                "---\ntags: [contradictions, {entity_tag}]\n\
                 correlation_id: {corr}\ngenerated_at: {now}\n---\n\
                 # {subject} — Contradictions\n\n\
                 > Sources that disagree on the same claim. Resolve before decisions.\n\n\
                 {contra_body}",
            );
            std::fs::write(base.join("Contradictions.md"), &contra_page)?;
            written_files.push(format!("{}/{slug}/Contradictions.md", config.entity_dir));
        }
    }

    // ── Hub pages ────────────────────────────────────────────────────

    let patterns = config.hook_patterns.clone().unwrap_or_default();
    let hooks = extract_hooks_from_facts(subject, &consolidated, &patterns);

    let hub_mappings: Vec<(&str, &[String])> = vec![
        ("Investors", &hooks.investors),
        ("Business Areas", &hooks.business_areas),
        ("Regions", &hooks.regions),
        ("Competitors", &hooks.competitors),
    ];

    for (hub_dir, items) in &hub_mappings {
        for item in *items {
            let files = write_or_append_hub(vault_root, hub_dir, item, subject, &corr, &now)?;
            written_files.extend(files);
        }
    }

    let hook_section = format_hook_links(&hooks);

    // ── Subject overview ─────────────────────────────────────────────

    let summary = synthesis
        .as_ref()
        .and_then(|s| s["summary"].as_str())
        .unwrap_or("*Synthesis did not complete — see Facts for raw findings.*");

    let recommendation = synthesis
        .as_ref()
        .and_then(|s| s["recommendation"].as_str())
        .unwrap_or("*Pending synthesis completion.*");

    let graph_section = if hook_section.is_empty() {
        String::new()
    } else {
        format!("## Graph Connections\n\n{hook_section}\n")
    };

    let overview = format!(
        "---\ntags: [{entity_dir_lower}, {entity_tag}]\n\
         correlation_id: {corr}\ngenerated_at: {now}\n---\n\
         # {subject}\n\n\
         > Convergent due diligence overview. Correlation ID: `{corr}`\n\n\
         ## Summary\n\n{summary}\n\n\
         ## Recommendation\n\n{recommendation}\n\n\
         ## Navigation\n\n\
         ### Analysis\n\
         - [[Analysis/Market|Market Analysis]]\n\
         - [[Analysis/Competition|Competitive Landscape]]\n\
         - [[Analysis/Technology|Technology Assessment]]\n\n\
         ### Evidence\n\
         - [[Facts|Tagged Facts]] — {n_facts} facts\n\
         - [[Risks|Risk Factors]]\n\
         - [[Opportunities|Growth Opportunities]]\n\
         - [[Contradictions]] — {n_contradictions} flagged\n\n\
         ### Raw Sources\n\
         {n_sources} source documents in [[Sources/]]\n\n\
         {graph_section}\
         ## Metadata\n\n\
         | Field | Value |\n|-------|-------|\n\
         | Correlation ID | `{corr}` |\n\
         | Cycles | {cycles} |\n\
         | Converged | {converged} |\n\
         | Strategies | {n_strategies} |\n\
         | Generated | {now} |\n",
        entity_dir_lower = config.entity_dir.to_lowercase(),
        entity_tag = slug,
        n_facts = consolidated.len(),
        n_contradictions = evaluations.len(),
        n_sources = deduped_signals.len(),
        n_strategies = result.context.get(ContextKey::Strategies).len(),
        cycles = result.cycles,
        converged = result.converged,
    );
    std::fs::write(base.join(format!("{subject}.md")), &overview)?;
    written_files.push(format!("{}/{slug}/{subject}.md", config.entity_dir));

    Ok(written_files)
}

// ── Hub Pages ────────────────────────────────────────────────────

/// Create a hub page if it doesn't exist, or append this subject to an existing one.
pub fn write_or_append_hub(
    vault_root: &Path,
    hub_dir: &str,
    entity: &str,
    subject: &str,
    corr: &str,
    now: &str,
) -> anyhow::Result<Vec<String>> {
    let dir = vault_root.join(hub_dir);
    std::fs::create_dir_all(&dir)?;

    let filename = format!("{}.md", sanitize_filename(entity));
    let filepath = dir.join(&filename);
    let rel_path = format!("{hub_dir}/{filename}");

    let tag = slugify(hub_dir);
    let company_link = format!("[[{subject}]]");

    if filepath.exists() {
        let existing = std::fs::read_to_string(&filepath)?;
        if !existing.contains(&company_link) {
            let updated = format!("{existing}\n- {company_link} _(corr: `{corr}`, {now})_");
            std::fs::write(&filepath, updated)?;
        }
        Ok(vec![])
    } else {
        let page = format!(
            "---\ntags: [hub, {tag}]\nentity: \"{entity}\"\n---\n\
             # {entity}\n\n\
             > Hub page — auto-created by due diligence. Links grow as more subjects are analyzed.\n\n\
             ## Subjects\n\n\
             - {company_link} _(corr: `{corr}`, {now})_\n",
        );
        std::fs::write(&filepath, page)?;
        Ok(vec![rel_path])
    }
}

// ── Root Pages ───────────────────────────────────────────────────

/// Rebuild all root MOC pages by scanning the vault filesystem.
pub fn update_root_pages(vault_root: &Path, config: &KbConfig) -> anyhow::Result<Vec<String>> {
    let mut written = Vec::new();

    for root_def in &config.root_pages {
        let mut sections: Vec<(String, Vec<(String, String)>)> = Vec::new();

        for scan_dir in &root_def.scan_dirs {
            let dir = vault_root.join(scan_dir);
            if !dir.exists() {
                continue;
            }

            let mut children: Vec<(String, String)> = Vec::new();

            if root_def.children_are_dirs {
                for entry in std::fs::read_dir(&dir)? {
                    let entry = entry?;
                    if !entry.file_type()?.is_dir() {
                        continue;
                    }
                    let dir_name = entry.file_name().to_string_lossy().to_string();
                    let display =
                        find_overview_page(&entry.path()).unwrap_or_else(|| titlecase(&dir_name));
                    let parent = scan_dir.as_str();
                    let link = format!("{parent}/{dir_name}/{display}");
                    children.push((link, display));
                }
            } else {
                for entry in std::fs::read_dir(&dir)? {
                    let entry = entry?;
                    let path = entry.path();
                    if path.extension().is_none_or(|e| e != "md") {
                        continue;
                    }
                    let stem = path
                        .file_stem()
                        .unwrap_or_default()
                        .to_string_lossy()
                        .to_string();
                    let link = format!("{scan_dir}/{stem}");
                    children.push((link, stem));
                }
            }

            children.sort_by_key(|c| c.1.to_lowercase());

            let section_title = if root_def.scan_dirs.len() > 1 {
                scan_dir.clone()
            } else {
                String::new()
            };
            sections.push((section_title, children));
        }

        let total_children: usize = sections.iter().map(|(_, c)| c.len()).sum();
        if total_children == 0 {
            continue;
        }

        let mut body = String::new();
        for (section_title, children) in &sections {
            if !section_title.is_empty() {
                let _ = write!(body, "### {section_title}\n\n");
            }
            for (link, display) in children {
                let _ = writeln!(body, "- [[{link}|{display}]]");
            }
            body.push('\n');
        }

        let page = format!(
            "---\ntags: [moc, root]\n---\n# {title}\n\n\
             > Root index — auto-updated by due diligence runs.\n\n\
             {body}",
            title = root_def.title,
        );

        std::fs::write(vault_root.join(&root_def.filename), &page)?;
        written.push(root_def.filename.clone());
    }

    Ok(written)
}

// ── Helpers ──────────────────────────────────────────────────────

fn find_overview_page(subject_dir: &Path) -> Option<String> {
    let skip = [
        "Facts",
        "Risks",
        "Opportunities",
        "Contradictions",
        "Loose Ends",
        "_raw_report",
    ];
    for entry in std::fs::read_dir(subject_dir).ok()? {
        let entry = entry.ok()?;
        let path = entry.path();
        if path.extension().is_none_or(|e| e != "md") || path.is_dir() {
            continue;
        }
        let stem = path.file_stem()?.to_string_lossy().to_string();
        if skip.iter().any(|s| *s == stem) {
            continue;
        }
        return Some(stem);
    }
    None
}

fn format_hook_links(hooks: &DdHooks) -> String {
    let mut sections = Vec::new();

    let pairs: &[(&str, &str, &[String])] = &[
        ("Investors", "Investors", &hooks.investors),
        ("Business Areas", "Business Areas", &hooks.business_areas),
        ("Regions", "Regions", &hooks.regions),
        ("Competitors", "Competitors", &hooks.competitors),
    ];

    for (label, dir, items) in pairs {
        if !items.is_empty() {
            let links: Vec<String> = items
                .iter()
                .map(|item| {
                    let filename = sanitize_filename(item);
                    format!("[[{dir}/{filename}|{item}]]")
                })
                .collect();
            sections.push(format!("**{label}:** {}", links.join(" · ")));
        }
    }

    sections.join("\n\n")
}

fn titlecase(s: &str) -> String {
    let mut result = String::new();
    let mut capitalize_next = true;
    for c in s.chars() {
        if c == '-' || c == '_' {
            result.push(' ');
            capitalize_next = true;
        } else if capitalize_next {
            result.extend(c.to_uppercase());
            capitalize_next = false;
        } else {
            result.push(c);
        }
    }
    result
}

pub fn slugify(s: &str) -> String {
    s.chars()
        .map(|c| if c.is_alphanumeric() { c } else { '-' })
        .collect::<String>()
        .to_lowercase()
}

pub fn sanitize_filename(s: &str) -> String {
    let cleaned: String = s
        .chars()
        .map(|c| match c {
            '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '-',
            _ => c,
        })
        .collect();
    if cleaned.len() > 80 {
        cleaned[..80].to_string()
    } else {
        cleaned
    }
}