nusy-kanban 0.15.2

Arrow-native kanban engine — SHACL shapes, graph-native PRs, dual boards, NATS multi-agent collaboration
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
//! Shape-driven template generation for all 12 nusy-kanban item types.
//!
//! Reads SHACL shapes from `ontology/shapes/{dev,research}/` and generates
//! rich body templates with section headers, field guidance, and turtle block
//! skeletons for research types.
//!
//! Override hierarchy: project (`.yurtle-kanban/shapes/`) → built-in.

use crate::item_type::ItemType;
use std::path::{Path, PathBuf};

/// A parsed section from a SHACL shape's `kb:requiredSection`.
#[derive(Debug, Clone)]
pub struct ShapeSection {
    pub name: String,
    pub order: u32,
    pub heading_level: u32,
    pub template_hint: String,
    pub required: bool,
}

/// A parsed turtle block template from `kb:TurtleBlockShape`.
#[derive(Debug, Clone)]
pub struct TurtleBlock {
    pub template_hint: String,
}

/// A parsed SHACL shape for one item type.
#[derive(Debug, Clone)]
pub struct Shape {
    pub item_type: String,
    pub description: String,
    pub sections: Vec<ShapeSection>,
    pub turtle_block: Option<TurtleBlock>,
}

/// Summary of an item type for `nk templates` listing.
#[derive(Debug, Clone)]
pub struct TypeSummary {
    pub item_type: ItemType,
    pub description: String,
}

/// Loads SHACL shapes with override hierarchy.
pub struct ShapeLoader {
    project_shapes_dir: Option<PathBuf>,
    builtin_shapes_dir: PathBuf,
}

impl ShapeLoader {
    /// Create a loader with optional project-level overrides.
    pub fn new(root: &Path) -> Self {
        let project_dir = root.join(".yurtle-kanban/shapes");
        Self {
            project_shapes_dir: if project_dir.exists() {
                Some(project_dir)
            } else {
                None
            },
            builtin_shapes_dir: root.join("crates/nusy-kanban/ontology/shapes"),
        }
    }

    /// Create a loader pointing directly at a shapes directory (for testing).
    pub fn from_dir(shapes_dir: &Path) -> Self {
        Self {
            project_shapes_dir: None,
            builtin_shapes_dir: shapes_dir.to_path_buf(),
        }
    }

    /// Load the shape for an item type.
    pub fn load_shape(&self, item_type: &ItemType) -> Option<Shape> {
        let board_dir = if item_type.is_research() {
            "research"
        } else {
            "dev"
        };
        let filename = format!("{}.ttl", item_type.as_str());

        // Project override first
        if let Some(ref project_dir) = self.project_shapes_dir {
            let path = project_dir.join(board_dir).join(&filename);
            if let Ok(content) = std::fs::read_to_string(&path) {
                return Some(parse_shape(&content, item_type.as_str()));
            }
        }

        // Built-in
        let path = self.builtin_shapes_dir.join(board_dir).join(&filename);
        if let Ok(content) = std::fs::read_to_string(path) {
            return Some(parse_shape(&content, item_type.as_str()));
        }

        None
    }
}

/// Template generator driven by SHACL shapes.
pub struct TemplateGenerator {
    loader: ShapeLoader,
}

impl TemplateGenerator {
    pub fn new(loader: ShapeLoader) -> Self {
        Self { loader }
    }

    /// Generate a full body template for the given item type and title.
    pub fn generate(&self, item_type: &ItemType, title: &str) -> String {
        let shape = match self.loader.load_shape(item_type) {
            Some(s) => s,
            None => return format!("# {title}\n"),
        };

        let mut lines = Vec::new();
        lines.push(format!("# {title}"));
        lines.push(String::new());

        // Emit sections ordered by sh:order
        let mut sections = shape.sections.clone();
        sections.sort_by_key(|s| s.order);

        for section in &sections {
            let heading = "#".repeat(section.heading_level as usize);
            lines.push(format!("{heading} {}", section.name));
            lines.push(String::new());

            if !section.template_hint.is_empty() {
                // Unescape \n in template hints
                let hint = section.template_hint.replace("\\n", "\n");
                for hint_line in hint.lines() {
                    lines.push(hint_line.to_string());
                }
            } else if section.required {
                lines.push("<!-- TODO -->".to_string());
            } else {
                lines.push("<!-- Optional -->".to_string());
            }
            lines.push(String::new());
        }

        // Append turtle block for research types
        if let Some(tb) = &shape.turtle_block {
            let hint = tb.template_hint.replace("\\n", "\n");
            lines.push(hint);
            lines.push(String::new());
        }

        lines.join("\n")
    }

    /// List all item types with their descriptions.
    pub fn list_all(&self) -> Vec<TypeSummary> {
        let all_types = [
            ItemType::Expedition,
            ItemType::Voyage,
            ItemType::Chore,
            ItemType::Hazard,
            ItemType::Signal,
            ItemType::Feature,
            ItemType::Paper,
            ItemType::Hypothesis,
            ItemType::Experiment,
            ItemType::Measure,
            ItemType::Idea,
            ItemType::Literature,
        ];

        all_types
            .iter()
            .map(|it| {
                let desc = self
                    .loader
                    .load_shape(it)
                    .map(|s| s.description)
                    .unwrap_or_default();
                TypeSummary {
                    item_type: *it,
                    description: desc,
                }
            })
            .collect()
    }
}

// ─── Shape Parser ──────────────────────────────────────────────────────────

/// Parse a TTL shape file into a Shape struct.
///
/// This is a minimal parser — not a full Turtle parser. It extracts:
/// - `sh:description` on the NodeShape
/// - `kb:requiredSection` blocks with their properties
/// - `kb:TurtleBlockShape` block
fn parse_shape(content: &str, type_name: &str) -> Shape {
    let description = extract_node_description(content);
    let sections = extract_sections(content);
    let turtle_block = extract_turtle_block(content);

    Shape {
        item_type: type_name.to_string(),
        description,
        sections,
        turtle_block,
    }
}

/// Extract `sh:description "..."` from the NodeShape declaration.
fn extract_node_description(content: &str) -> String {
    // Look for sh:description on the NodeShape (first occurrence)
    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with("sh:description") {
            return extract_quoted_string(trimmed);
        }
    }
    String::new()
}

/// Extract all `kb:requiredSection` blocks.
fn extract_sections(content: &str) -> Vec<ShapeSection> {
    let mut sections = Vec::new();
    let lines: Vec<&str> = content.lines().collect();
    let mut i = 0;

    while i < lines.len() {
        if lines[i].trim().starts_with("kb:requiredSection") {
            let mut name = String::new();
            let mut order = 0u32;
            let mut heading_level = 2u32;
            let mut template_hint = String::new();
            let mut required = false;

            // Parse until closing ] ;
            let mut j = i;
            while j < lines.len() {
                let t = lines[j].trim();

                if t.contains("kb:sectionName") {
                    name = extract_quoted_string(t);
                }
                if t.contains("sh:order") {
                    order = extract_number(t);
                }
                if t.contains("kb:headingLevel") {
                    heading_level = extract_number(t);
                }
                if t.contains("kb:templateHint") {
                    template_hint = extract_quoted_string(t);
                }
                if t.contains("kb:required") && !t.contains("requiredSection") {
                    required = t.contains("true");
                }
                if t.ends_with("] ;") || t.ends_with("] .") {
                    break;
                }
                j += 1;
            }

            if !name.is_empty() {
                sections.push(ShapeSection {
                    name,
                    order,
                    heading_level,
                    template_hint,
                    required,
                });
            }
            i = j + 1;
        } else {
            i += 1;
        }
    }

    sections
}

/// Extract `kb:TurtleBlockShape` block.
fn extract_turtle_block(content: &str) -> Option<TurtleBlock> {
    let lines: Vec<&str> = content.lines().collect();
    for (i, line) in lines.iter().enumerate() {
        if line.trim().starts_with("kb:TurtleBlockShape") {
            let mut hint = String::new();
            let mut j = i;
            while j < lines.len() {
                let t = lines[j].trim();
                if t.contains("kb:templateHint") {
                    hint = extract_quoted_string(t);
                }
                if t.ends_with("] ;") || t.ends_with("] .") {
                    break;
                }
                j += 1;
            }
            if !hint.is_empty() {
                return Some(TurtleBlock {
                    template_hint: hint,
                });
            }
        }
    }
    None
}

/// Extract a quoted string value from a TTL property line.
fn extract_quoted_string(line: &str) -> String {
    if let Some(start) = line.find('"') {
        let rest = &line[start + 1..];
        if let Some(end) = rest.rfind('"') {
            return rest[..end].to_string();
        }
    }
    String::new()
}

/// Extract an integer value from a TTL property line.
fn extract_number(line: &str) -> u32 {
    line.split_whitespace()
        .filter_map(|w| {
            w.trim_end_matches(';')
                .trim_end_matches('.')
                .parse::<u32>()
                .ok()
        })
        .next()
        .unwrap_or(0)
}

/// Format the `nk templates` listing output.
pub fn format_type_listing(summaries: &[TypeSummary]) -> String {
    let mut lines = Vec::new();
    lines.push("Available item types:".to_string());
    lines.push(String::new());

    for s in summaries {
        let desc = if s.description.is_empty() {
            "(no description)".to_string()
        } else {
            s.description.clone()
        };
        lines.push(format!("  {:12} {}", s.item_type.as_str(), desc));
    }

    lines.push(String::new());
    lines.push("Usage: nk templates <type>".to_string());

    lines.join("\n") + "\n"
}

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

    fn test_shapes_dir() -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ontology/shapes")
    }

    fn make_loader() -> ShapeLoader {
        ShapeLoader::from_dir(&test_shapes_dir())
    }

    #[test]
    fn test_expedition_template_has_sections() {
        let generator = TemplateGenerator::new(make_loader());
        let template = generator.generate(&ItemType::Expedition, "Test Expedition");

        assert!(template.contains("# Test Expedition"));
        assert!(template.contains("## V12/V13 Parity Check"));
        assert!(template.contains("## Context"));
        assert!(template.contains("## Phase 1:"));
        assert!(template.contains("## Tests"));
        assert!(template.contains("## Definition of Done"));
        assert!(template.contains("## Constraints"));
    }

    #[test]
    fn test_hypothesis_template_has_turtle_block() {
        let generator = TemplateGenerator::new(make_loader());
        let template = generator.generate(&ItemType::Hypothesis, "Test Hypothesis");

        assert!(template.contains("# Test Hypothesis"));
        assert!(template.contains("## Claim"));
        assert!(template.contains("## Rationale"));
        assert!(template.contains("@prefix hyp:"));
        assert!(template.contains("hyp:Hypothesis"));
    }

    #[test]
    fn test_list_all_has_12_types() {
        let generator = TemplateGenerator::new(make_loader());
        let summaries = generator.list_all();
        assert_eq!(summaries.len(), 12);
    }

    #[test]
    fn test_list_all_has_descriptions() {
        let generator = TemplateGenerator::new(make_loader());
        let summaries = generator.list_all();

        let expedition = summaries
            .iter()
            .find(|s| s.item_type == ItemType::Expedition)
            .expect("expedition in list");
        assert!(
            !expedition.description.is_empty(),
            "expedition should have a description"
        );
    }

    #[test]
    fn test_all_12_types_produce_non_trivial_templates() {
        let generator = TemplateGenerator::new(make_loader());
        let types = [
            ItemType::Expedition,
            ItemType::Voyage,
            ItemType::Chore,
            ItemType::Hazard,
            ItemType::Signal,
            ItemType::Feature,
            ItemType::Paper,
            ItemType::Hypothesis,
            ItemType::Experiment,
            ItemType::Measure,
            ItemType::Idea,
            ItemType::Literature,
        ];

        for it in &types {
            let template = generator.generate(it, "Test");
            assert!(
                template.contains("## "),
                "{} template should have section headers",
                it.as_str()
            );
            assert!(
                template.len() > 50,
                "{} template should be non-trivial (got {} bytes)",
                it.as_str(),
                template.len()
            );
        }
    }

    #[test]
    fn test_research_types_have_turtle_blocks() {
        let generator = TemplateGenerator::new(make_loader());
        let research_types = [ItemType::Hypothesis, ItemType::Experiment, ItemType::Paper];

        for it in &research_types {
            let template = generator.generate(it, "Test");
            assert!(
                template.contains("@prefix") || template.contains("```turtle"),
                "{} should include turtle block",
                it.as_str()
            );
        }
    }

    #[test]
    fn test_project_override_takes_precedence() {
        let dir = tempfile::tempdir().expect("tempdir");
        let shapes = dir.path().join("dev");
        std::fs::create_dir_all(&shapes).expect("create shapes dir");

        // Write a custom expedition shape
        std::fs::write(
            shapes.join("expedition.ttl"),
            r#"
kb:ExpeditionShape a sh:NodeShape ;
    sh:description "Custom project expedition" ;

    kb:requiredSection [
        kb:sectionName "Custom Section" ; sh:order 1 ;
        kb:headingLevel 2 ;
        kb:templateHint "This is a custom template" ;
        kb:required true ] .
"#,
        )
        .expect("write shape");

        let loader = ShapeLoader {
            project_shapes_dir: Some(dir.path().to_path_buf()),
            builtin_shapes_dir: test_shapes_dir(),
        };
        let generator = TemplateGenerator::new(loader);
        let template = generator.generate(&ItemType::Expedition, "Override Test");

        assert!(template.contains("## Custom Section"));
        assert!(template.contains("This is a custom template"));
    }

    #[test]
    fn test_shape_description_extraction() {
        let content = r#"
kb:TestShape a sh:NodeShape ;
    sh:description "A test shape for validation" ;
    sh:property [ sh:path kb:id ] .
"#;
        let shape = parse_shape(content, "test");
        assert_eq!(shape.description, "A test shape for validation");
    }

    #[test]
    fn test_format_type_listing() {
        let summaries = vec![
            TypeSummary {
                item_type: ItemType::Expedition,
                description: "Multi-phase feature work".to_string(),
            },
            TypeSummary {
                item_type: ItemType::Chore,
                description: "Routine maintenance".to_string(),
            },
        ];

        let output = format_type_listing(&summaries);
        assert!(output.contains("expedition"));
        assert!(output.contains("Multi-phase feature work"));
        assert!(output.contains("chore"));
        assert!(output.contains("nk templates <type>"));
    }

    #[test]
    fn test_unknown_type_falls_back() {
        let loader = ShapeLoader::from_dir(Path::new("/nonexistent"));
        let generator = TemplateGenerator::new(loader);
        let template = generator.generate(&ItemType::Expedition, "Fallback");
        assert_eq!(template, "# Fallback\n");
    }
}