req-cli 0.5.0-rc.6

Managed requirements CLI for LLM agents and humans
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
// Implements REQ-0015 (interactive terminal menu for humans) and the TUI
// half of REQ-0083 (cross-surface parity): the menu exposes every
// agent-relevant CLI operation so a human at the terminal can achieve
// the same things an agent reaches for via MCP or the flag CLI.
use anyhow::Result;
use dialoguer::{theme::ColorfulTheme, Input, Select};
use std::path::PathBuf;

use crate::cli::{
    AddArgs, ConformArgs, CoverageArgs, DeleteArgs, DiffArgs, DoctorArgs, ExportArgs, ExportFormat,
    ListArgs, NextArgs, ShowArgs, StaleArgs, StatusArgs, UpdateArgs, VersionArgs,
};
use crate::commands;
use crate::storage::load_resolved;

/// The full TUI menu. Each entry maps to a top-level CLI command so the
/// surfaces stay one-to-one. See REQ-0083 for the parity contract.
pub const MENU: &[&str] = &[
    // REQ-0104: session brief is the natural first action.
    "Brief (where are we now?)",
    "Browse / view (list + show)",
    "Status",
    "Next requirement to work on",
    "Add",
    "Update",
    "Link",
    "Delete (mark obsolete)",
    "Split a compound requirement",
    // REQ-0190: rules/well-formedness check (renamed from validate).
    "Conform (check spec against the rules)",
    // REQ-0101: lint menu entry.
    "Lint (quality audit)",
    "Coverage report",
    "Stale report",
    "Review (PR-style spec report)",
    "Doctor (setup audit)",
    "Diff between git refs",
    "Audit (git signature trail)",
    "Export to markdown (stdout)",
    // REQ-0134: functional-safety review surface for humans — browse
    // hazards / SF / SR and trace a safety case. (Label avoids commas so
    // the parity guard's comma-split keeps it as one entry.)
    "Safety: hazards / SF / SR (sreq) / trace",
    "Version",
    "Quit",
];

pub fn run(file: &Option<PathBuf>) -> Result<()> {
    let theme = ColorfulTheme::default();
    loop {
        let (_, project) = load_resolved(file)?;
        let header = format!(
            "req — {} ({} requirements)",
            project.name,
            project.requirements.len()
        );
        println!("\n{}", header);
        println!("{}", "=".repeat(header.len()));

        let idx = Select::with_theme(&theme)
            .with_prompt("Action")
            .items(MENU)
            .default(0)
            .interact()?;

        let action = match dispatch(idx, file, &project, &theme) {
            Ok(()) => continue,
            Err(e) if e.to_string() == "__quit__" => return Ok(()),
            Err(e) => Err(e),
        };
        action?;
    }
}

fn dispatch(
    idx: usize,
    file: &Option<PathBuf>,
    project: &crate::model::Project,
    theme: &ColorfulTheme,
) -> Result<()> {
    match MENU[idx] {
        // REQ-0104: brief from the TUI.
        "Brief (where are we now?)" => commands::brief::run(
            crate::cli::BriefArgs {
                full: false,
                json: false,
            },
            file,
        ),
        "Browse / view (list + show)" => browse(file, project),
        "Status" => commands::status::run(
            StatusArgs {
                tag: vec![],
                json: false,
            },
            file,
        ),
        "Next requirement to work on" => commands::next::run(default_next(), file),
        "Add" => commands::add::run(default_add(), file),
        "Update" => {
            if let Some(id) = pick_id(theme, project)? {
                commands::update::run(default_update(id), file)?;
            }
            Ok(())
        }
        "Link" => link_flow(file, project, theme),
        "Delete (mark obsolete)" => {
            if let Some(id) = pick_id(theme, project)? {
                commands::delete::run(
                    DeleteArgs {
                        id,
                        hard: false,
                        reason: None,
                        json: false,
                    },
                    file,
                )?;
            }
            Ok(())
        }
        "Split a compound requirement" => split_flow(file, theme),
        "Conform (check spec against the rules)" => {
            commands::conform_cmd::run(ConformArgs { json: false }, file)
        }
        // REQ-0101: lint TUI dispatch.
        "Lint (quality audit)" => commands::lint::run(
            crate::cli::LintArgs {
                path: PathBuf::from("."),
                json: false,
            },
            file,
        ),
        "Coverage report" => commands::coverage::run(default_coverage(), file),
        "Stale report" => commands::stale::run(default_stale(), file),
        "Review (PR-style spec report)" => commands::review::run(
            crate::cli::ReviewArgs {
                base: "origin/main".into(),
                path: PathBuf::from("."),
                ext: vec![],
                ignore: vec![],
                staged: false,
                marker_near_hunks: 0,
                gate: false,
                no_defects: false,
                summary: false,
                new: false,
                all: false,
                json: false,
            },
            file,
        ),
        "Doctor (setup audit)" => commands::doctor::run(DoctorArgs { json: false }),
        "Diff between git refs" => diff_flow(file, theme),
        "Audit (git signature trail)" => audit_flow(file),
        "Export to markdown (stdout)" => commands::export::run(
            ExportArgs {
                format: ExportFormat::Markdown,
                output: "-".into(),
            },
            file,
        ),
        "Safety: hazards / SF / SR (sreq) / trace" => safety_flow(file, theme),
        "Version" => commands::version::run(VersionArgs { json: false }),
        "Quit" => Err(anyhow::anyhow!("__quit__")),
        _ => Ok(()),
    }
}

/// REQ-0134: read-oriented functional-safety browser. Authoring (add /
/// assess / verify) stays on the CLI/MCP surface where the multi-field
/// risk parameters belong; the TUI exists so a human reviewer can walk
/// the hazard log and trace a safety case end to end.
fn safety_flow(file: &Option<PathBuf>, theme: &ColorfulTheme) -> Result<()> {
    let opts = [
        "Hazards (list)",
        "Safety functions (list)",
        "Safety requirements (sreq, list)",
        "Trace a safety case",
        "Back",
    ];
    let idx = Select::with_theme(theme)
        .with_prompt("Safety")
        .items(&opts)
        .default(0)
        .interact()?;
    if idx == 3 {
        let id: String = dialoguer::Input::with_theme(theme)
            .with_prompt("Trace which id? (HAZ-/SF-/SR-NNNN)")
            .interact_text()?;
        if id.trim().is_empty() {
            return Ok(());
        }
        return commands::safety::run_trace(crate::cli::TraceArgs { id, json: false }, file);
    }
    // The three list views are non-interactive — factored out so they can
    // be smoke-tested without driving the terminal menu (REQ-0134).
    safety_review(idx, file)
}

/// REQ-0134: the read-only safety review actions reachable from the TUI
/// menu, separated from the interactive Select so they are unit-testable.
fn safety_review(action: usize, file: &Option<PathBuf>) -> Result<()> {
    use crate::cli::{HazardCmd, HazardListArgs, SfCmd, SfListArgs, SreqCmd, SreqListArgs};
    match action {
        0 => commands::safety::run_hazard(
            HazardCmd::List(HazardListArgs {
                sil: None,
                status: None,
                unmitigated: false,
                json: false,
            }),
            file,
        ),
        1 => commands::safety::run_sf(
            SfCmd::List(SfListArgs {
                sil: None,
                status: None,
                unrealized: false,
                json: false,
            }),
            file,
        ),
        2 => commands::safety::run_sreq(
            SreqCmd::List(SreqListArgs {
                sil: None,
                status: None,
                unverified: false,
                json: false,
            }),
            file,
        ),
        _ => Ok(()),
    }
}

fn split_flow(file: &Option<PathBuf>, _theme: &ColorfulTheme) -> Result<()> {
    use dialoguer::Input;
    let id: String = Input::with_theme(_theme)
        .with_prompt("Requirement to split (e.g. REQ-0042)")
        .interact_text()?;
    let reason: String = Input::with_theme(_theme)
        .with_prompt("Reason (recorded on history)")
        .allow_empty(true)
        .interact_text()?;
    // Delegate the interactive part-statement prompting to split::run
    // by passing an empty `into` (it prompts when empty).
    commands::split::run(
        crate::cli::SplitArgs {
            id,
            into: vec![],
            reason: if reason.trim().is_empty() {
                None
            } else {
                Some(reason)
            },
            keep_original: false,
            json: false,
        },
        file,
    )
}

fn link_flow(
    file: &Option<PathBuf>,
    project: &crate::model::Project,
    theme: &ColorfulTheme,
) -> Result<()> {
    let from = match pick_id(theme, project)? {
        Some(id) => id,
        None => return Ok(()),
    };
    let to = match pick_id(theme, project)? {
        Some(id) => id,
        None => return Ok(()),
    };
    let kinds = ["parent", "depends-on", "refines", "conflicts", "verifies"];
    let idx = Select::with_theme(theme)
        .with_prompt("Link kind")
        .items(&kinds)
        .default(0)
        .interact()?;
    let kind = match kinds[idx] {
        "parent" => crate::cli::LinkKindArg::Parent,
        "depends-on" => crate::cli::LinkKindArg::DependsOn,
        "refines" => crate::cli::LinkKindArg::Refines,
        "conflicts" => crate::cli::LinkKindArg::Conflicts,
        _ => crate::cli::LinkKindArg::Verifies,
    };
    commands::link::run(
        crate::cli::LinkArgs {
            from,
            to,
            kind,
            remove: false,
            json: false,
        },
        file,
    )
}

fn diff_flow(file: &Option<PathBuf>, theme: &ColorfulTheme) -> Result<()> {
    let spec: String = Input::with_theme(theme)
        .with_prompt("Git diff spec (e.g. origin/main..HEAD)")
        .default("HEAD~1..HEAD".to_string())
        .interact_text()?;
    commands::diff::run(DiffArgs { spec, json: false }, file)
}

fn audit_flow(file: &Option<PathBuf>) -> Result<()> {
    use crate::cli::AuditArgs;
    commands::audit::run(
        AuditArgs {
            limit: 20,
            gate: false,
            require_good_signature: false,
            required_signers: Vec::new(),
            json: false,
        },
        file,
    )
}

fn browse(file: &Option<PathBuf>, project: &crate::model::Project) -> Result<()> {
    if project.requirements.is_empty() {
        println!("(no requirements yet)");
        return Ok(());
    }
    let theme = ColorfulTheme::default();
    let ids: Vec<String> = project.requirements.keys().cloned().collect();
    let labels: Vec<String> = ids
        .iter()
        .map(|id| {
            let r = &project.requirements[id];
            format!("{:<10} [{}] {}", id, r.status.as_str(), r.title)
        })
        .collect();
    let pick = Select::with_theme(&theme)
        .with_prompt("Pick a requirement")
        .items(&labels)
        .default(0)
        .interact_opt()?;
    if let Some(i) = pick {
        commands::show::run(
            ShowArgs {
                id: ids[i].clone(),
                json: false,
            },
            file,
        )?;
    }
    Ok(())
}

fn pick_id(theme: &ColorfulTheme, project: &crate::model::Project) -> Result<Option<String>> {
    if project.requirements.is_empty() {
        println!("(no requirements yet)");
        return Ok(None);
    }
    let ids: Vec<String> = project.requirements.keys().cloned().collect();
    let labels: Vec<String> = ids
        .iter()
        .map(|id| format!("{}{}", id, project.requirements[id].title))
        .collect();
    let pick = Select::with_theme(theme)
        .items(&labels)
        .default(0)
        .interact_opt()?;
    Ok(pick.map(|i| ids[i].clone()))
}

fn default_add() -> AddArgs {
    AddArgs {
        title: None,
        statement: None,
        rationale: None,
        acceptance: vec![],
        kind: None,
        priority: None,
        tag: vec![],
        parent: None,
        interactive: true,
        json: false,
        from_json: None,
    }
}

fn default_update(id: String) -> UpdateArgs {
    UpdateArgs {
        id,
        title: None,
        statement: None,
        rationale: None,
        acceptance: None,
        add_acceptance: vec![],
        remove_acceptance: vec![],
        kind: None,
        priority: None,
        status: None,
        add_tag: vec![],
        remove_tag: vec![],
        reason: None,
        force: false,
        json: false,
    }
}

// Unused for now; kept for completeness so list-style filtering compiles if reused.
#[allow(dead_code)]
fn default_list() -> ListArgs {
    ListArgs {
        status: None,
        include_obsolete: false,
        kind: None,
        priority: None,
        tag: vec![],
        query: None,
        offset: None,
        limit: None,
        json: false,
    }
}

fn default_next() -> NextArgs {
    NextArgs {
        status: None,
        kind: None,
        priority: None,
        tag: vec![],
        json: false,
    }
}

fn default_coverage() -> CoverageArgs {
    CoverageArgs {
        path: PathBuf::from("."),
        extensions: vec![],
        unlinked_files: false,
        by_file: false,
        by_req: false,
        remap: vec![],
        apply: false,
        strict: false,
        allow_orphans: vec![],
        json: false,
    }
}

fn default_stale() -> StaleArgs {
    StaleArgs {
        path: PathBuf::from("."),
        only_stale: false,
        json: false,
    }
}

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

    // REQ-0134: the TUI safety-review dispatch routes the three list
    // actions to the safety commands without panicking. Drives
    // safety_review directly (the interactive Select is bypassed) against
    // a real on-disk project, which is the part worth covering — the menu
    // wiring, not dialoguer's terminal I/O.
    #[test]
    fn safety_review_dispatch_lists_without_panicking() {
        let dir = tempfile::Builder::new()
            .prefix("req-tui-")
            .tempdir()
            .unwrap();
        let path = dir.path().join("project.req");
        let mut project = crate::model::Project::new("p".into());
        // A minimal safety chain so the list views have something to show.
        let now = chrono::Utc::now();
        let hid = project.allocate_haz_id();
        project.hazards.insert(
            hid.clone(),
            crate::model::Hazard {
                id: hid,
                title: "H".into(),
                description: String::new(),
                operating_context: String::new(),
                harm: "hurt".into(),
                consequence: Some(crate::model::Consequence::Cc),
                frequency: Some(crate::model::Frequency::Fb),
                avoidance: Some(crate::model::Avoidance::Pb),
                probability: Some(crate::model::Probability::W3),
                status: crate::model::HazardStatus::Assessed,
                tags: vec![],
                links: vec![],
                created: now,
                updated: now,
                history: vec![],
                extra: Default::default(),
            },
        );
        crate::storage::save(&path, &project).unwrap();
        let file = Some(path);
        // actions 0,1,2 are the non-interactive list views; 99 is the
        // "Back"/no-op arm.
        for action in [0usize, 1, 2, 99] {
            assert!(
                safety_review(action, &file).is_ok(),
                "safety_review({}) should not error",
                action
            );
        }
    }
}