verifyos-cli 0.13.1

AI agent-friendly Rust CLI for scanning iOS app bundles for App Store rejection risks before submission.
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
use clap::Parser;
use comfy_table::{Cell, Color, Table};
use miette::{IntoDiagnostic, Result};
use std::collections::HashSet;
use std::path::{Path, PathBuf};

use verifyos_cli::agent_assets::{build_repair_plan, AgentAssetLayout, RepairPolicy, RepairTarget};
use verifyos_cli::agent_io::{
    empty_agent_pack, infer_existing_command_hints, load_agent_pack, write_agent_pack,
    write_fix_prompt_file, write_next_steps_script, write_pr_brief_file, write_pr_comment_file,
};
use verifyos_cli::agents::{write_agents_file, CommandHints};
use verifyos_cli::config::FileConfig;
use verifyos_cli::doctor::{
    detect_freshness_source_path, run_doctor, DoctorPlanContext, DoctorReport, DoctorStatus,
};
use verifyos_cli::report::AgentPackFormat;

use crate::commands::support::{
    parse_optional_cli_profile, parse_optional_output_format, profile_key,
};
use crate::{run_scan_for_agent_pack, OutputFormat};
use verifyos_cli::profiles::ScanProfile;

#[derive(Debug, Clone)]
struct DoctorRepairOptions {
    profile: ScanProfile,
    policy: RepairPolicy,
}

#[derive(Debug, Parser)]
pub struct DoctorArgs {
    /// Root directory that contains AGENTS.md and generated init assets
    #[arg(long)]
    pub output_dir: Option<PathBuf>,

    /// Explicit AGENTS.md path to check
    #[arg(long)]
    pub agents: Option<PathBuf>,

    /// Explicit config path to validate
    #[arg(long)]
    pub config: Option<PathBuf>,

    /// Output format for doctor results
    #[arg(long, value_enum)]
    pub format: Option<OutputFormat>,

    /// Repair a broken or missing agent setup under the chosen output root
    #[arg(long)]
    pub fix: bool,

    /// Scan an app first and repair agent assets with current findings
    #[arg(long)]
    pub from_scan: Option<PathBuf>,

    /// Baseline JSON report used with --from-scan to keep only new or regressed risks
    #[arg(long)]
    pub baseline: Option<PathBuf>,

    /// Compare asset freshness against this specific report file instead of auto-detecting report.json/report.sarif
    #[arg(long)]
    pub freshness_against: Option<PathBuf>,

    /// Scan profile to use with --from-scan
    #[arg(long, value_enum)]
    pub profile: Option<ScanProfile>,

    /// Generate pr-brief.md for PR review and agent handoff
    #[arg(long)]
    pub open_pr_brief: bool,

    /// Generate pr-comment.md for sticky PR comments or manual GitHub updates
    #[arg(long)]
    pub open_pr_comment: bool,

    /// Only repair selected outputs (repeat or comma-separate)
    #[arg(long, value_enum, value_delimiter = ',', num_args = 1..)]
    pub repair: Vec<RepairTarget>,

    /// Show which assets would be rebuilt for the current fix/repair settings
    #[arg(long)]
    pub plan: bool,

    /// Optional Markdown file path for the current repair plan preview
    #[arg(long)]
    pub plan_out: Option<PathBuf>,
}

pub fn run(doctor: DoctorArgs, file_config: &FileConfig) -> Result<()> {
    let doctor_defaults = file_config.doctor.clone().unwrap_or_default();
    let doctor_format = doctor
        .format
        .or(parse_optional_output_format(
            doctor_defaults.format.as_deref(),
        )?)
        .unwrap_or(OutputFormat::Table);
    let doctor_profile = doctor
        .profile
        .or(parse_optional_cli_profile(
            doctor_defaults.profile.as_deref(),
        )?)
        .unwrap_or(ScanProfile::Full);
    let output_dir = doctor
        .output_dir
        .clone()
        .or(doctor_defaults.output_dir.clone())
        .unwrap_or_else(|| PathBuf::from("."));
    let mut layout = AgentAssetLayout::from_output_dir(&output_dir);
    let agents_path = doctor
        .agents
        .clone()
        .or(doctor_defaults.agents.clone())
        .unwrap_or_else(|| layout.agents_path.clone());
    layout = layout.with_agents_path(&agents_path);
    let repair_targets = if doctor.repair.is_empty() {
        parse_repair_targets(doctor_defaults.repair.as_deref())?
    } else {
        doctor.repair.iter().copied().collect()
    };
    let should_fix =
        doctor.fix || doctor_defaults.fix.unwrap_or(false) || !repair_targets.is_empty();
    let open_pr_brief = doctor.open_pr_brief || doctor_defaults.open_pr_brief.unwrap_or(false);
    let open_pr_comment =
        doctor.open_pr_comment || doctor_defaults.open_pr_comment.unwrap_or(false);
    let plan_out = doctor.plan_out.clone().or(doctor_defaults.plan_out.clone());
    let should_plan = doctor.plan || plan_out.is_some();
    let freshness_against = doctor
        .freshness_against
        .clone()
        .or(doctor_defaults.freshness_against.clone());

    if should_fix {
        let repair_options = DoctorRepairOptions {
            profile: doctor_profile,
            policy: RepairPolicy::new(repair_targets.clone(), open_pr_brief, open_pr_comment),
        };
        repair_doctor_setup(
            &layout,
            doctor.from_scan.as_deref(),
            doctor.baseline.as_deref(),
            &repair_options,
        )?;
    }

    let mut report = run_doctor(
        doctor.config.as_deref(),
        &layout.agents_path,
        freshness_against.as_deref(),
    );
    if should_plan {
        let policy = RepairPolicy::new(repair_targets, open_pr_brief, open_pr_comment);
        report.repair_plan = build_repair_plan(&layout, &policy);
        report.plan_context = Some(build_plan_context(
            &layout,
            doctor.from_scan.as_deref(),
            doctor.baseline.as_deref(),
            freshness_against.as_deref(),
            &policy,
        ));
        if let Some(path) = plan_out.as_deref() {
            let mut hints = infer_existing_command_hints(&layout);
            hints.repair_plan_path = Some(path.display().to_string());
            write_plan_markdown(path, &report, Some(&hints))?;
        }
    }
    render_doctor_report(&report, doctor_format)?;
    if report.has_failures() {
        std::process::exit(1);
    }
    Ok(())
}

fn build_plan_context(
    layout: &AgentAssetLayout,
    from_scan: Option<&Path>,
    baseline_path: Option<&Path>,
    freshness_against: Option<&Path>,
    policy: &RepairPolicy,
) -> DoctorPlanContext {
    let repair_targets = if policy.repairs_all() {
        vec!["all".to_string()]
    } else {
        let mut targets: Vec<String> = policy
            .repair_targets()
            .iter()
            .copied()
            .map(|target| target.key().to_string())
            .collect();
        targets.sort();
        targets
    };

    DoctorPlanContext {
        source: if from_scan.is_some() {
            "fresh-scan".to_string()
        } else {
            "existing-assets".to_string()
        },
        scan_artifact: from_scan.map(|path| path.display().to_string()),
        baseline_path: baseline_path.map(|path| path.display().to_string()),
        freshness_source: detect_freshness_source_path(&layout.output_dir, freshness_against)
            .map(|path| path.display().to_string()),
        repair_targets,
    }
}

fn parse_repair_targets(raw: Option<&[String]>) -> Result<HashSet<RepairTarget>> {
    let mut targets = HashSet::new();
    for value in raw.unwrap_or_default() {
        targets.insert(parse_repair_target(value)?);
    }
    Ok(targets)
}

fn parse_repair_target(value: &str) -> Result<RepairTarget> {
    match value.to_ascii_lowercase().as_str() {
        "agents" => Ok(RepairTarget::Agents),
        "agent-bundle" => Ok(RepairTarget::AgentBundle),
        "fix-prompt" => Ok(RepairTarget::FixPrompt),
        "pr-brief" => Ok(RepairTarget::PrBrief),
        "pr-comment" => Ok(RepairTarget::PrComment),
        _ => Err(miette::miette!(
            "Unknown repair target `{value}`. Expected one of: agents, agent-bundle, fix-prompt, pr-brief, pr-comment"
        )),
    }
}

fn repair_doctor_setup(
    layout: &AgentAssetLayout,
    from_scan: Option<&Path>,
    baseline_path: Option<&Path>,
    options: &DoctorRepairOptions,
) -> Result<()> {
    std::fs::create_dir_all(&layout.output_dir).into_diagnostic()?;
    std::fs::create_dir_all(&layout.agent_bundle_dir).into_diagnostic()?;

    let inferred_hints = infer_existing_command_hints(layout);
    let should_open_pr_brief = options.policy.open_pr_brief
        || inferred_hints.pr_brief_path.is_some()
        || options.policy.should_repair_pr_brief();
    let should_open_pr_comment = options.policy.open_pr_comment
        || inferred_hints.pr_comment_path.is_some()
        || options.policy.should_repair_pr_comment();

    let pack = if let Some(app_path) = from_scan {
        run_scan_for_agent_pack(app_path, options.profile, baseline_path)?
    } else {
        load_agent_pack(&layout.agent_pack_json_path).unwrap_or_else(empty_agent_pack)
    };

    let command_hints = CommandHints {
        output_dir: Some(layout.output_dir.display().to_string()),
        app_path: Some(
            from_scan
                .map(|path| path.display().to_string())
                .or(inferred_hints.app_path)
                .unwrap_or_else(|| "<path-to-.ipa-or-.app>".to_string()),
        ),
        baseline_path: baseline_path
            .map(|path| path.display().to_string())
            .or(inferred_hints.baseline_path),
        agent_pack_dir: Some(layout.agent_bundle_dir.display().to_string()),
        profile: Some(
            from_scan
                .map(|_| profile_key(options.profile))
                .or(inferred_hints.profile)
                .unwrap_or_else(|| profile_key(options.profile)),
        ),
        shell_script: true,
        fix_prompt_path: Some(layout.fix_prompt_path.display().to_string()),
        repair_plan_path: Some(layout.repair_plan_path.display().to_string()),
        pr_brief_path: should_open_pr_brief.then(|| layout.pr_brief_path.display().to_string()),
        pr_comment_path: should_open_pr_comment
            .then(|| layout.pr_comment_path.display().to_string()),
    };

    if options.policy.should_repair_agents() {
        write_agents_file(
            &layout.agents_path,
            Some(&pack),
            Some(&layout.agent_bundle_dir),
            Some(&command_hints),
        )?;
    }
    if options.policy.should_repair_bundle() {
        write_agent_pack(&layout.agent_bundle_dir, &pack, AgentPackFormat::Bundle)?;
        write_next_steps_script(&layout.next_steps_script_path, &command_hints)?;
    }
    if options.policy.should_repair_fix_prompt() {
        write_fix_prompt_file(&layout.fix_prompt_path, &pack, &command_hints)?;
    }
    if options.policy.should_repair_pr_brief() && should_open_pr_brief {
        write_pr_brief_file(&layout.pr_brief_path, &pack, &command_hints)?;
    }
    if options.policy.should_repair_pr_comment() && should_open_pr_comment {
        write_pr_comment_file(&layout.pr_comment_path, &pack, &command_hints)?;
    }
    let repair_plan = build_repair_plan(layout, &options.policy);
    let repair_plan_context =
        build_plan_context(layout, from_scan, baseline_path, None, &options.policy);
    write_plan_markdown(
        &layout.repair_plan_path,
        &DoctorReport {
            checks: Vec::new(),
            repair_plan,
            plan_context: Some(repair_plan_context),
        },
        Some(&command_hints),
    )?;

    Ok(())
}

fn render_doctor_report(report: &DoctorReport, format: OutputFormat) -> Result<()> {
    match format {
        OutputFormat::Table => {
            let mut table = Table::new();
            table.set_header(vec!["Check", "Status", "Detail"]);
            for item in &report.checks {
                let status = match item.status {
                    DoctorStatus::Pass => Cell::new("PASS").fg(Color::Green),
                    DoctorStatus::Warn => Cell::new("WARN").fg(Color::Yellow),
                    DoctorStatus::Fail => Cell::new("FAIL").fg(Color::Red),
                };
                table.add_row(vec![Cell::new(&item.name), status, Cell::new(&item.detail)]);
            }
            println!("{table}");
            if let Some(plan_context) = &report.plan_context {
                println!("\nPlan context:");
                println!("- source: {}", plan_context.source);
                if let Some(scan_artifact) = &plan_context.scan_artifact {
                    println!("- scan artifact: {scan_artifact}");
                }
                if let Some(baseline_path) = &plan_context.baseline_path {
                    println!("- baseline: {baseline_path}");
                }
                if let Some(freshness_source) = &plan_context.freshness_source {
                    println!("- freshness source: {freshness_source}");
                }
                println!(
                    "- repair targets: {}",
                    plan_context.repair_targets.join(", ")
                );
            }
            if !report.repair_plan.is_empty() {
                println!("\nRepair plan:");
                for item in &report.repair_plan {
                    println!("- {} -> {} ({})", item.target, item.path, item.reason);
                }
            }
        }
        OutputFormat::Json => {
            println!(
                "{}",
                serde_json::to_string_pretty(report).into_diagnostic()?
            );
        }
        OutputFormat::Sarif => {
            return Err(miette::miette!(
                "`doctor` supports only table or json output"
            ));
        }
    }
    Ok(())
}

fn write_plan_markdown(
    path: &Path,
    report: &DoctorReport,
    hints: Option<&CommandHints>,
) -> Result<()> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).into_diagnostic()?;
    }
    std::fs::write(path, render_plan_markdown(report, hints)).into_diagnostic()?;
    Ok(())
}

fn render_plan_markdown(report: &DoctorReport, hints: Option<&CommandHints>) -> String {
    let mut out = String::new();
    out.push_str("# verifyOS Repair Plan\n\n");

    if let Some(context) = &report.plan_context {
        out.push_str("## Context\n\n");
        out.push_str(&format!("- Source: `{}`\n", context.source));
        if let Some(scan_artifact) = &context.scan_artifact {
            out.push_str(&format!("- Scan artifact: `{scan_artifact}`\n"));
        }
        if let Some(baseline_path) = &context.baseline_path {
            out.push_str(&format!("- Baseline: `{baseline_path}`\n"));
        }
        if let Some(freshness_source) = &context.freshness_source {
            out.push_str(&format!("- Freshness source: `{freshness_source}`\n"));
        }
        out.push_str(&format!(
            "- Repair targets: `{}`\n\n",
            context.repair_targets.join(", ")
        ));
    }

    if let Some(hints) = hints {
        let mut rows = Vec::new();
        if let Some(path) = hints.fix_prompt_path.as_deref() {
            rows.push(format!("- Fix prompt: `{path}`"));
        }
        if let Some(path) = hints.pr_brief_path.as_deref() {
            rows.push(format!("- PR brief: `{path}`"));
        }
        if let Some(path) = hints.pr_comment_path.as_deref() {
            rows.push(format!("- PR comment: `{path}`"));
        }
        if !rows.is_empty() {
            out.push_str("## Related Artifacts\n\n");
            for row in rows {
                out.push_str(&row);
                out.push('\n');
            }
            out.push('\n');
        }
    }

    out.push_str("## Planned Outputs\n\n");
    if report.repair_plan.is_empty() {
        out.push_str("- No repair outputs selected.\n");
    } else {
        for item in &report.repair_plan {
            out.push_str(&format!(
                "- **{}**\n  - Path: `{}`\n  - Reason: {}\n",
                item.target, item.path, item.reason
            ));
        }
    }

    out
}

#[cfg(test)]
mod tests {
    use super::render_plan_markdown;
    use verifyos_cli::agent_assets::RepairPlanItem;
    use verifyos_cli::agents::CommandHints;
    use verifyos_cli::doctor::{DoctorPlanContext, DoctorReport};

    #[test]
    fn render_plan_markdown_matches_snapshot() {
        let report = DoctorReport {
            checks: Vec::new(),
            repair_plan: vec![
                RepairPlanItem {
                    target: "agent-bundle".to_string(),
                    path: ".verifyos/.verifyos-agent".to_string(),
                    reason: "rebuild agent-pack files and next-steps.sh".to_string(),
                },
                RepairPlanItem {
                    target: "pr-comment".to_string(),
                    path: ".verifyos/pr-comment.md".to_string(),
                    reason: "refresh sticky PR comment draft".to_string(),
                },
            ],
            plan_context: Some(DoctorPlanContext {
                source: "fresh-scan".to_string(),
                scan_artifact: Some("examples/bad_app.ipa".to_string()),
                baseline_path: Some("baseline.json".to_string()),
                freshness_source: Some(".verifyos/report.json".to_string()),
                repair_targets: vec!["agent-bundle".to_string(), "pr-comment".to_string()],
            }),
        };

        let hints = CommandHints {
            fix_prompt_path: Some(".verifyos/fix-prompt.md".to_string()),
            pr_brief_path: Some(".verifyos/pr-brief.md".to_string()),
            pr_comment_path: Some(".verifyos/pr-comment.md".to_string()),
            ..CommandHints::default()
        };

        let markdown = render_plan_markdown(&report, Some(&hints));
        let expected = r#"# verifyOS Repair Plan

## Context

- Source: `fresh-scan`
- Scan artifact: `examples/bad_app.ipa`
- Baseline: `baseline.json`
- Freshness source: `.verifyos/report.json`
- Repair targets: `agent-bundle, pr-comment`

## Related Artifacts

- Fix prompt: `.verifyos/fix-prompt.md`
- PR brief: `.verifyos/pr-brief.md`
- PR comment: `.verifyos/pr-comment.md`

## Planned Outputs

- **agent-bundle**
  - Path: `.verifyos/.verifyos-agent`
  - Reason: rebuild agent-pack files and next-steps.sh
- **pr-comment**
  - Path: `.verifyos/pr-comment.md`
  - Reason: refresh sticky PR comment draft
"#;

        assert_eq!(markdown, expected);
    }
}