mrapids 0.1.31

Your OpenAPI, but executable
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
//! Plan sketch: read-only composition of operations into a visual execution plan.
//!
//! This module generates non-executable sketches showing how operations
//! would compose into a workflow. No side effects, no tokens, no execution.

use crate::cli::PlanFormat;
use crate::collections::{find_collection, parse_collection};
use crate::core::parser::{parse_spec, ParameterLocation, UnifiedOperation, UnifiedSpec};
use anyhow::{Context, Result};
use colored::*;
use std::path::{Path, PathBuf};

/// A single step in a plan sketch
#[derive(Debug, Clone, serde::Serialize)]
pub struct PlanStep {
    pub step: usize,
    pub operation_id: String,
    pub method: String,
    pub path: String,
    pub summary: Option<String>,
    pub risk: RiskLabel,
    pub auth: AuthLabel,
    pub path_params: Vec<String>,
    pub requires_body: bool,
    pub depends_on: Vec<String>,
    pub critical: bool,
    pub readiness: StepReadiness,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct RiskLabel {
    pub level: String,
    pub display: String,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct AuthLabel {
    pub required: bool,
    pub scheme: String,
}

/// Execution readiness analysis for a step
#[derive(Debug, Clone, serde::Serialize)]
pub struct StepReadiness {
    pub status: String, // "READY", "NEEDS_INPUT", "UNRESOLVED"
    pub gaps: Vec<StepGap>,
}

/// A single gap preventing execution readiness
#[derive(Debug, Clone, serde::Serialize)]
pub struct StepGap {
    pub kind: String,
    pub detail: String,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct PlanSketchOutput {
    pub sketch_id: String,
    pub steps: Vec<PlanStep>,
    pub total_steps: usize,
    pub plan_risk: String,
    pub risk_summary: RiskSummary,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub readiness_summary: Option<ReadinessSummary>,
    pub note: String,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct RiskSummary {
    pub read: usize,
    pub write: usize,
    pub destructive: usize,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct ReadinessSummary {
    pub ready: usize,
    pub needs_input: usize,
    pub unresolved: usize,
}

/// Build a plan sketch from explicit operation IDs
pub fn sketch_from_operations(
    operation_ids: &[String],
    spec_path: Option<&PathBuf>,
) -> Result<PlanSketchOutput> {
    let spec = load_spec(spec_path.map(|p| p.as_path()))?;
    let mut steps = Vec::new();

    for (i, op_id) in operation_ids.iter().enumerate() {
        let operation = find_operation(&spec, op_id)?;
        steps.push(build_step(i + 1, operation, &spec, &[], false));
    }

    Ok(build_output(steps))
}

/// Build a plan sketch from a collection YAML file
pub fn sketch_from_collection(
    name: &str,
    dir: &Path,
    spec_path: Option<&PathBuf>,
) -> Result<PlanSketchOutput> {
    let collection_path = find_collection(dir, name)?;
    let collection = parse_collection(&collection_path)?;
    let spec = load_spec(spec_path.map(|p| p.as_path()))?;
    let mut steps = Vec::new();

    for (i, req) in collection.requests.iter().enumerate() {
        match find_operation(&spec, &req.operation) {
            Ok(operation) => {
                let deps = req.depends_on.as_ref().cloned().unwrap_or_default();
                steps.push(build_step(i + 1, operation, &spec, &deps, req.critical));
            }
            Err(_) => {
                steps.push(unresolved_step(i + 1, req));
            }
        }
    }

    Ok(build_output(steps))
}

/// Display the plan sketch in the requested format
pub fn display_sketch(sketch: &PlanSketchOutput, format: &PlanFormat, show_gaps: bool) {
    match format {
        PlanFormat::Text => display_text(sketch, show_gaps),
        PlanFormat::Json => display_json(sketch, show_gaps),
    }
}

// --- Internal helpers ---

fn load_spec(spec_path: Option<&Path>) -> Result<UnifiedSpec> {
    let path = match spec_path {
        Some(p) => p.to_path_buf(),
        None => auto_find_spec()?,
    };
    let content = std::fs::read_to_string(&path)
        .with_context(|| format!("Failed to read spec: {}", path.display()))?;
    parse_spec(&content).with_context(|| "Failed to parse OpenAPI spec")
}

fn auto_find_spec() -> Result<PathBuf> {
    let current_dir = std::env::current_dir()?;
    let candidates = [
        "openapi.yaml",
        "openapi.yml",
        "openapi.json",
        "swagger.yaml",
        "swagger.yml",
        "swagger.json",
        "api.yaml",
        "api.yml",
        "api.json",
        "spec.yaml",
        "spec.yml",
        "spec.json",
    ];

    let search_dirs = [
        current_dir.join(".mrapids"),
        current_dir.join("specs"),
        current_dir,
    ];

    search_dirs
        .iter()
        .filter(|d| d.exists())
        .flat_map(|d| candidates.iter().map(move |c| d.join(c)))
        .find(|p| p.exists())
        .ok_or_else(|| anyhow::anyhow!("No OpenAPI spec found. Use --spec to specify the path."))
}

fn find_operation<'a>(spec: &'a UnifiedSpec, operation_id: &str) -> Result<&'a UnifiedOperation> {
    let search = operation_id.to_lowercase();

    // Exact match (case-insensitive)
    if let Some(op) = spec
        .operations
        .iter()
        .find(|op| op.operation_id.to_lowercase() == search)
    {
        return Ok(op);
    }

    // Partial match
    let matches: Vec<&UnifiedOperation> = spec
        .operations
        .iter()
        .filter(|op| op.operation_id.to_lowercase().contains(&search))
        .collect();

    match matches.len() {
        0 => Err(anyhow::anyhow!(
            "Operation '{}' not found in spec",
            operation_id
        )),
        1 => Ok(matches[0]),
        _ => Err(anyhow::anyhow!(
            "Ambiguous: '{}' matches multiple operations: {}",
            operation_id,
            matches
                .iter()
                .map(|op| op.operation_id.as_str())
                .collect::<Vec<_>>()
                .join(", ")
        )),
    }
}

fn risk_from_method(method: &str) -> RiskLabel {
    match method.to_uppercase().as_str() {
        "GET" | "HEAD" | "OPTIONS" => RiskLabel {
            level: "low".to_string(),
            display: "low".to_string(),
        },
        "DELETE" => RiskLabel {
            level: "high".to_string(),
            display: "HIGH".to_string(),
        },
        _ => RiskLabel {
            level: "medium".to_string(),
            display: "medium".to_string(),
        },
    }
}

fn resolve_auth_scheme(scheme_type: &str, http_scheme: Option<&str>) -> &'static str {
    match http_scheme {
        Some("bearer") => "bearer",
        Some("basic") => "basic",
        _ => match scheme_type {
            "apiKey" => "api-key",
            "oauth2" => "oauth2",
            "openIdConnect" => "oidc",
            _ => "unknown",
        },
    }
}

fn auth_from_operation(operation: &UnifiedOperation, spec: &UnifiedSpec) -> AuthLabel {
    let security = match operation.security.as_ref() {
        Some(s) => s,
        None => {
            return AuthLabel {
                required: false,
                scheme: "none".to_string(),
            }
        }
    };

    let req = match security.first() {
        Some(r) => r,
        None => {
            return AuthLabel {
                required: false,
                scheme: "none".to_string(),
            }
        }
    };

    let scheme_str = match spec.security_schemes.get(&req.scheme_name) {
        Some(scheme) => resolve_auth_scheme(&scheme.scheme_type, scheme.scheme.as_deref()),
        None => &req.scheme_name,
    };

    AuthLabel {
        required: true,
        scheme: scheme_str.to_string(),
    }
}

/// Analyze what's missing for a step to be execution-ready
fn analyze_gaps(operation: &UnifiedOperation, auth: &AuthLabel) -> StepReadiness {
    let mut gaps = Vec::new();

    // Required path parameters (always needed — can't construct URL without them)
    for p in &operation.parameters {
        if p.required && p.location == ParameterLocation::Path {
            gaps.push(StepGap {
                kind: "required_param".to_string(),
                detail: format!("path param '{}' required", p.name),
            });
        }
    }

    // Required query parameters
    for p in &operation.parameters {
        if p.required && p.location == ParameterLocation::Query {
            gaps.push(StepGap {
                kind: "required_param".to_string(),
                detail: format!("query param '{}' required", p.name),
            });
        }
    }

    // Required request body
    if let Some(ref body) = operation.request_body {
        if body.required {
            gaps.push(StepGap {
                kind: "required_body".to_string(),
                detail: "request body required".to_string(),
            });
        }
    }

    // Auth requirement
    if auth.required {
        gaps.push(StepGap {
            kind: "auth_required".to_string(),
            detail: format!("{} authentication required", auth.scheme),
        });
    }

    let status = if gaps.is_empty() {
        "READY".to_string()
    } else {
        "NEEDS_INPUT".to_string()
    };

    StepReadiness { status, gaps }
}

fn build_step(
    index: usize,
    operation: &UnifiedOperation,
    spec: &UnifiedSpec,
    depends_on: &[String],
    critical: bool,
) -> PlanStep {
    let path_params: Vec<String> = operation
        .parameters
        .iter()
        .filter(|p| p.location == ParameterLocation::Path)
        .map(|p| p.name.clone())
        .collect();

    let requires_body = operation.request_body.is_some();
    let auth = auth_from_operation(operation, spec);
    let readiness = analyze_gaps(operation, &auth);

    PlanStep {
        step: index,
        operation_id: operation.operation_id.clone(),
        method: operation.method.to_uppercase(),
        path: operation.path.clone(),
        summary: operation.summary.clone(),
        risk: risk_from_method(&operation.method),
        auth,
        path_params,
        requires_body,
        depends_on: depends_on.to_vec(),
        critical,
        readiness,
    }
}

fn unresolved_step(index: usize, req: &crate::collections::models::CollectionRequest) -> PlanStep {
    PlanStep {
        step: index,
        operation_id: req.operation.clone(),
        method: "???".to_string(),
        path: "(not found in spec)".to_string(),
        summary: None,
        risk: RiskLabel {
            level: "unknown".to_string(),
            display: "UNKNOWN".to_string(),
        },
        auth: AuthLabel {
            required: false,
            scheme: "unknown".to_string(),
        },
        path_params: vec![],
        requires_body: false,
        depends_on: req.depends_on.as_ref().cloned().unwrap_or_default(),
        critical: req.critical,
        readiness: StepReadiness {
            status: "UNRESOLVED".to_string(),
            gaps: vec![StepGap {
                kind: "unresolved".to_string(),
                detail: "operation not found in spec".to_string(),
            }],
        },
    }
}

fn generate_sketch_id() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    let ts = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis();
    format!("sketch_{:x}", ts & 0xFFFF)
}

fn derive_plan_risk(summary: &RiskSummary) -> String {
    if summary.destructive > 0 {
        "HIGH (contains destructive operations)".to_string()
    } else if summary.write > 0 {
        "MEDIUM (contains write operations)".to_string()
    } else {
        "LOW (read-only)".to_string()
    }
}

fn build_output(steps: Vec<PlanStep>) -> PlanSketchOutput {
    let total = steps.len();
    let mut read = 0;
    let mut write = 0;
    let mut destructive = 0;

    for s in &steps {
        match s.risk.level.as_str() {
            "low" => read += 1,
            "medium" => write += 1,
            "high" => destructive += 1,
            _ => {}
        }
    }

    let risk_summary = RiskSummary {
        read,
        write,
        destructive,
    };
    let plan_risk = derive_plan_risk(&risk_summary);

    // Compute readiness summary
    let mut ready = 0;
    let mut needs_input = 0;
    let mut unresolved = 0;
    for s in &steps {
        match s.readiness.status.as_str() {
            "READY" => ready += 1,
            "NEEDS_INPUT" => needs_input += 1,
            "UNRESOLVED" => unresolved += 1,
            _ => {}
        }
    }
    let readiness_summary = Some(ReadinessSummary {
        ready,
        needs_input,
        unresolved,
    });

    PlanSketchOutput {
        sketch_id: generate_sketch_id(),
        steps,
        total_steps: total,
        plan_risk,
        risk_summary,
        readiness_summary,
        note: "Sketch only — not executable. Use claim → preview → run for each step.".to_string(),
    }
}

fn display_text(sketch: &PlanSketchOutput, show_gaps: bool) {
    println!(
        "\n{}  {} steps (not executable — sketch only)  [{}]",
        "Plan:".bold(),
        sketch.total_steps,
        sketch.sketch_id.dimmed(),
    );

    // Derived plan risk
    let risk_colored = if sketch.risk_summary.destructive > 0 {
        sketch.plan_risk.red().bold()
    } else if sketch.risk_summary.write > 0 {
        sketch.plan_risk.yellow()
    } else {
        sketch.plan_risk.green()
    };
    println!("  Plan risk: {}", risk_colored);

    // Readiness summary (when show_gaps enabled)
    if show_gaps {
        if let Some(ref rs) = sketch.readiness_summary {
            display_readiness_summary(rs);
        }
    }

    println!();

    for step in &sketch.steps {
        let method_colored = match step.method.as_str() {
            "GET" => step.method.green(),
            "POST" => step.method.yellow(),
            "PUT" => step.method.blue(),
            "PATCH" => step.method.cyan(),
            "DELETE" => step.method.red(),
            _ => step.method.white(),
        };

        let risk_colored = match step.risk.level.as_str() {
            "low" => step.risk.display.green(),
            "medium" => step.risk.display.yellow(),
            "high" => format!("{} risk", step.risk.display).red().bold(),
            _ => step.risk.display.dimmed(),
        };

        let auth_str = if step.auth.required {
            format!("auth: {}", step.auth.scheme).dimmed().to_string()
        } else {
            "no auth".dimmed().to_string()
        };

        // Step line
        print!(
            "  {:>2}. {:<30} [{}]  {}  {}",
            step.step,
            step.operation_id.bright_cyan(),
            method_colored,
            risk_colored,
            auth_str,
        );

        // Flags
        if step.critical {
            print!("  {}", "CRITICAL".red().bold());
        }
        if step.requires_body {
            print!("  {}", "+body".dimmed());
        }

        // Readiness badge (when show_gaps enabled)
        if show_gaps {
            let badge = match step.readiness.status.as_str() {
                "READY" => "READY".green().bold(),
                "NEEDS_INPUT" => "NEEDS_INPUT".yellow().bold(),
                "UNRESOLVED" => "UNRESOLVED".red().bold(),
                _ => step.readiness.status.dimmed(),
            };
            print!("  {}", badge);
        }

        println!();

        // Path
        println!("      {} {}", step.method.dimmed(), step.path.dimmed());

        // Summary if available
        if let Some(ref summary) = step.summary {
            println!("      {}", summary.dimmed());
        }

        // Path params
        if !step.path_params.is_empty() {
            println!("      params: {}", step.path_params.join(", ").dimmed());
        }

        // Dependencies
        if !step.depends_on.is_empty() {
            println!(
                "      {} {}",
                "depends on:".dimmed(),
                step.depends_on.join(", ").yellow()
            );
        }

        // Gaps detail (when show_gaps enabled and there are gaps)
        if show_gaps {
            display_step_gaps(&step.readiness.gaps);
        }

        println!();
    }

    // Risk summary
    let summary = &sketch.risk_summary;
    println!("{}", "  ─────────────────────────────────────────".dimmed());
    print!("  Risk: ");
    if summary.read > 0 {
        print!("{} read  ", format!("{}", summary.read).green());
    }
    if summary.write > 0 {
        print!("{} write  ", format!("{}", summary.write).yellow());
    }
    if summary.destructive > 0 {
        print!(
            "{}",
            format!("{} destructive", summary.destructive).red().bold()
        );
    }
    println!();

    // Footer
    println!();
    println!(
        "  {}",
        "Order as provided. Dependencies not auto-resolved.".dimmed()
    );
    println!(
        "  {}",
        "Execution today: claim → preview → run (per step)".dimmed()
    );
    println!("  {}", "Future: grouped execution (experimental)".dimmed());
    println!();
}

fn display_readiness_summary(rs: &ReadinessSummary) {
    print!("  Readiness: ");
    if rs.ready > 0 {
        print!("{} ready  ", format!("{}", rs.ready).green());
    }
    if rs.needs_input > 0 {
        print!("{} needs input  ", format!("{}", rs.needs_input).yellow());
    }
    if rs.unresolved > 0 {
        print!("{} unresolved", format!("{}", rs.unresolved).red());
    }
    println!();
}

fn display_step_gaps(gaps: &[StepGap]) {
    for gap in gaps {
        println!("      {} {}", "gap:".yellow(), gap.detail);
    }
}

fn strip_readiness(value: &mut serde_json::Value) {
    let obj = match value.as_object_mut() {
        Some(o) => o,
        None => return,
    };
    obj.remove("readiness_summary");
    let steps = match obj.get_mut("steps").and_then(|s| s.as_array_mut()) {
        Some(a) => a,
        None => return,
    };
    for step in steps {
        if let Some(step_obj) = step.as_object_mut() {
            step_obj.remove("readiness");
        }
    }
}

fn display_json(sketch: &PlanSketchOutput, show_gaps: bool) {
    if show_gaps {
        match serde_json::to_string_pretty(sketch) {
            Ok(json) => println!("{}", json),
            Err(e) => eprintln!("Failed to serialize plan: {}", e),
        }
    } else {
        let mut value = match serde_json::to_value(sketch) {
            Ok(v) => v,
            Err(e) => {
                eprintln!("Failed to serialize plan: {}", e);
                return;
            }
        };
        strip_readiness(&mut value);
        match serde_json::to_string_pretty(&value) {
            Ok(json) => println!("{}", json),
            Err(e) => eprintln!("Failed to serialize plan: {}", e),
        }
    }
}