straymark-cli 3.12.3

CLI for StrayMark — the cognitive discipline your AI-assisted projects need
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
use anyhow::Result;
use colored::Colorize;
use std::path::{Path, PathBuf};

use crate::charter::{self, CharterStatus};
use crate::config::StrayMarkConfig;
use crate::manifest::DistManifest;
use crate::utils::{self, pad_right_visual, visual_width};

/// Expected directories inside .straymark/
const EXPECTED_DIRS: &[&str] = &[
    "00-governance",
    "01-requirements",
    "02-design/decisions",
    "03-implementation",
    "04-testing",
    "05-operations/incidents",
    "05-operations/runbooks",
    "06-evolution/technical-debt",
    "07-ai-audit/agent-logs",
    "07-ai-audit/decisions",
    "07-ai-audit/ethical-reviews",
    "08-security",
    "09-ai-models",
    "templates",
];

/// Expected files (relative to project root)
const EXPECTED_FILES: &[(&str, &str)] = &[
    (".straymark/config.yml", "config.yml"),
    (".straymark/dist-manifest.yml", "dist-manifest.yml"),
    ("STRAYMARK.md", "STRAYMARK.md"),
];

/// Document type prefixes for counting
const DOC_TYPES: &[(&str, &str)] = &[
    ("ADR", "Architecture Decisions"),
    ("AIDEC", "AI Decisions"),
    ("AILOG", "AI Action Logs"),
    ("ETH", "Ethical Reviews"),
    ("INC", "Incident Post-mortems"),
    ("REQ", "Requirements"),
    ("TDE", "Technical Debt"),
    ("TES", "Test Plans"),
    ("SEC", "Security"),
    ("MCARD", "Model Cards"),
    ("SBOM", "Software Bill of Materials"),
    ("DPIA", "Data Protection Impact"),
];

pub fn run(path: &str) -> Result<()> {
    let resolved = match utils::resolve_project_root(path) {
        Some(r) => r,
        None => {
            let target = PathBuf::from(path)
                .canonicalize()
                .unwrap_or_else(|_| PathBuf::from(path));
            utils::info(&format!(
                "StrayMark is not installed in {}",
                target.display()
            ));
            utils::info("Run 'straymark init' to initialize StrayMark in this directory.");
            return Ok(());
        }
    };

    if resolved.is_fallback {
        utils::info(&format!(
            "Using StrayMark installation at repo root: {}",
            resolved.path.display()
        ));
    }

    let target = resolved.path;
    let straymark_dir = target.join(".straymark");

    let version = load_version(&target);
    let language = load_language(&target);
    let cli_version = env!("CARGO_PKG_VERSION");

    // ── Header ──
    println!();
    println!("  {}", "StrayMark Status".bold().cyan());
    println!();

    // ── Project Info ──
    println!("  {}", "Project".bold());
    let project_rows: Vec<(&str, String)> = vec![
        ("Path", target.display().to_string()),
        ("Framework", format!("fw-{}", version)),
        ("CLI", format!("cli-{}", cli_version)),
        ("Language", language.clone()),
    ];
    let label_w = project_rows
        .iter()
        .map(|(l, _)| visual_width(l))
        .max()
        .unwrap_or(5);
    let value_w = project_rows
        .iter()
        .map(|(_, v)| visual_width(v))
        .max()
        .unwrap_or(10);
    print_border("", label_w, "", value_w, "");
    for (label, value) in &project_rows {
        println!(
            "{}{}",
            pad_right_visual(label, label_w).dimmed(),
            pad_right_visual(value, value_w),
        );
    }
    print_border("", label_w, "", value_w, "");

    // ── Structure ──
    println!();
    println!("  {}", "Structure".bold());

    // Collect all structure items with their status
    let mut struct_items: Vec<(String, bool)> = Vec::new();
    for dir in EXPECTED_DIRS {
        let dir_path = straymark_dir.join(dir);
        struct_items.push((format!("{dir}/"), dir_path.exists()));
    }
    for &(rel_path, label) in EXPECTED_FILES {
        let file_path = target.join(rel_path);
        struct_items.push((label.to_string(), file_path.exists()));
    }

    let total_items = struct_items.len();
    let total_ok = struct_items.iter().filter(|(_, ok)| *ok).count();
    let total_missing = total_items - total_ok;

    if total_missing == 0 {
        println!(
            "  {} All {} items present",
            "".green().bold(),
            total_items
        );
    } else {
        println!(
            "  {} {}/{} items present ({} missing)",
            "!".yellow().bold(),
            total_ok,
            total_items,
            total_missing
        );
    }

    // Calculate column widths dynamically, measured in visual columns.
    let name_w = struct_items
        .iter()
        .map(|(name, _)| visual_width(name))
        .max()
        .unwrap_or(10)
        .max(visual_width("Directory / File"));
    let status_w = 6; // "✓ OK " or "✗ -- "

    println!();
    println!(
        "  {} {} {}",
        pad_right_visual("Directory / File", name_w).dimmed(),
        "".dimmed(),
        pad_right_visual("Status", status_w).dimmed(),
    );
    println!(
        "  {}",
        format!("{}─┼─{}", "".repeat(name_w), "".repeat(status_w)).dimmed()
    );

    for (name, exists) in &struct_items {
        let status_text = if *exists { "✓ OK" } else { "✗ --" };
        let name_cell = pad_right_visual(name, name_w);
        let status_cell = pad_right_visual(status_text, status_w);
        if *exists {
            println!("  {}{}", name_cell, status_cell.green());
        } else {
            println!("  {}{}", name_cell.yellow(), status_cell.yellow());
        }
    }

    // ── Documentation ──
    let counts = count_documents(&straymark_dir);
    let total: usize = counts.iter().map(|(_, _, c)| c).sum();

    println!();
    println!("  {}", "Documentation".bold());

    let type_w = DOC_TYPES
        .iter()
        .map(|(p, l)| visual_width(&format!("{p:<6}{l}")))
        .max()
        .unwrap_or(20)
        .max(visual_width("Type"));
    let count_w = 5;

    println!();
    println!(
        "  {} {} {}",
        pad_right_visual("Type", type_w).dimmed(),
        "".dimmed(),
        pad_right_visual("Count", count_w).dimmed(),
    );
    println!(
        "  {}",
        format!("{}─┼─{}", "".repeat(type_w), "".repeat(count_w)).dimmed()
    );

    for (prefix, label, count) in &counts {
        let display = format!("{prefix:<6}{label}");
        let count_str = format!("{count:>count_w$}");
        let padded = pad_right_visual(&display, type_w);
        if *count > 0 {
            println!("  {}{}", padded, count_str.green().bold());
        } else {
            println!("  {}{}", padded.dimmed(), count_str.dimmed());
        }
    }

    let total_str = format!("{total:>count_w$}");
    println!(
        "  {}{}",
        pad_right_visual("TOTAL", type_w).bold(),
        total_str.cyan().bold(),
    );
    println!();

    // ── Charters ──
    // Charters are an optional pattern (bounded units of work). Surfaced as a
    // dedicated block so adopters of `straymark charter new` see them in the
    // canonical health view; projects without Charters see a one-line hint.
    let charter_counts = count_charters(&target);
    print_charters_block(&charter_counts);

    // ── Hints ──
    if total_missing > 0 {
        println!(
            "  {} Run {} to restore missing directories and files",
            "".blue().bold(),
            "straymark repair".cyan().bold()
        );
    }
    if total > 0 {
        println!(
            "  {} Run {} to browse documentation interactively",
            "".blue().bold(),
            "straymark explore".cyan().bold()
        );
    }
    if total_missing > 0 || total > 0 {
        println!();
    }

    Ok(())
}

fn print_border(prefix: &str, w1: usize, mid: &str, w2: usize, suffix: &str) {
    println!(
        "{}",
        format!(
            "{}{}{}{}{}",
            prefix,
            "".repeat(w1 + 2),
            mid,
            "".repeat(w2 + 2),
            suffix
        )
        .dimmed()
    );
}

fn load_version(project_root: &std::path::Path) -> String {
    let manifest_path = project_root.join(".straymark/dist-manifest.yml");
    match DistManifest::load(&manifest_path) {
        Ok(m) => m.version,
        Err(_) => {
            utils::warn("Could not read dist-manifest.yml");
            "unknown".to_string()
        }
    }
}

fn load_language(project_root: &std::path::Path) -> String {
    // Use the same resolver as `explore` / `new` so all three commands
    // agree on the effective language (config when present, else OS locale,
    // else "en").
    StrayMarkConfig::resolve_language(project_root)
}

fn count_documents(straymark_dir: &std::path::Path) -> Vec<(&'static str, &'static str, usize)> {
    let files = walk_files(straymark_dir);
    DOC_TYPES
        .iter()
        .map(|&(doc_type, label)| {
            let prefix = format!("{}-", doc_type);
            let count = files
                .iter()
                .filter(|p| {
                    utils::is_user_document(p)
                        && p.file_name()
                            .and_then(|n| n.to_str())
                            .map(|n| n.starts_with(&prefix))
                            .unwrap_or(false)
                })
                .count();
            (doc_type, label, count)
        })
        .collect()
}

fn walk_files(dir: &std::path::Path) -> Vec<PathBuf> {
    let mut files = Vec::new();
    if let Ok(entries) = std::fs::read_dir(dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                files.extend(walk_files(&path));
            } else {
                files.push(path);
            }
        }
    }
    files
}

/// Counts of Charters by lifecycle status. `total` includes parseable charters
/// only; `unparseable` reports files that look like charters by filename but
/// failed schema parse, so the user knows to fix them.
struct CharterCounts {
    declared: usize,
    in_progress: usize,
    closed: usize,
    unparseable: usize,
    total: usize,
}

fn count_charters(project_root: &Path) -> CharterCounts {
    let (charters, errors) = charter::discover_and_parse(project_root);
    let mut declared = 0;
    let mut in_progress = 0;
    let mut closed = 0;
    for c in &charters {
        match c.frontmatter.status {
            CharterStatus::Declared => declared += 1,
            CharterStatus::InProgress => in_progress += 1,
            CharterStatus::Closed => closed += 1,
        }
    }
    CharterCounts {
        declared,
        in_progress,
        closed,
        unparseable: errors.len(),
        total: charters.len(),
    }
}

fn print_charters_block(c: &CharterCounts) {
    println!("  {}", "Charters".bold());
    if c.total == 0 && c.unparseable == 0 {
        println!(
            "  {} {}",
            "·".dimmed(),
            "No Charters yet — run `straymark charter new` to declare one (see STRAYMARK.md §15).".dimmed(),
        );
        println!();
        return;
    }

    let rows: Vec<(&str, usize, colored::Color)> = vec![
        ("declared", c.declared, colored::Color::White),
        ("in-progress", c.in_progress, colored::Color::Yellow),
        ("closed", c.closed, colored::Color::Green),
    ];
    let label_w = rows
        .iter()
        .map(|(l, _, _)| visual_width(l))
        .max()
        .unwrap_or(11);
    let count_w = 5;

    println!();
    println!(
        "  {} {} {}",
        pad_right_visual("Status", label_w).dimmed(),
        "".dimmed(),
        pad_right_visual("Count", count_w).dimmed(),
    );
    println!(
        "  {}",
        format!("{}─┼─{}", "".repeat(label_w), "".repeat(count_w)).dimmed()
    );

    for (label, count, color) in &rows {
        let count_str = format!("{count:>count_w$}");
        let padded = pad_right_visual(label, label_w);
        if *count > 0 {
            println!("  {}{}", padded, count_str.color(*color).bold());
        } else {
            println!("  {}{}", padded.dimmed(), count_str.dimmed());
        }
    }

    let total_str = format!("{:>count_w$}", c.total);
    println!(
        "  {}{}",
        pad_right_visual("TOTAL", label_w).bold(),
        total_str.cyan().bold(),
    );

    if c.unparseable > 0 {
        println!(
            "  {} {} unparseable Charter file{} — run `straymark charter list` to see the warning detail.",
            "!".yellow().bold(),
            c.unparseable,
            if c.unparseable == 1 { "" } else { "s" },
        );
    }
    println!();
}