rust-doctor 0.6.0

Local-first health audit for Cargo workspaces: curated Clippy lints and native detectors, scored out of 100
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
use std::env;
use std::ffi::OsStr;
use std::fmt;
use std::fs;
use std::io::{self, Write};
use std::path::{Component, Path, PathBuf};
use std::process::{Command, Stdio};
use std::thread;
use std::time::{Duration, Instant};

use rust_doctor::InspectReport;
use rust_doctor::presentation::{DiagnosticGroup, ReportPresentation, canonical_rule_help};
use rust_doctor::terminal_text::sanitize;
use rust_doctor::{BlockingLevel, CategoryOverride, RuleOverride, ScopeMode};

pub const MAX_HANDOFF_BYTES: usize = 12 * 1024;
const MAX_GROUPS: usize = 3;
const MAX_LOCATIONS: usize = 24;
const MAX_ISSUE_SITES: usize = 8;
const CLIPBOARD_TIMEOUT: Duration = Duration::from_secs(2);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AgentTarget {
    ClaudeCode,
    Codex,
    Cursor,
}

impl AgentTarget {
    pub const fn label(self) -> &'static str {
        match self {
            Self::ClaudeCode => "Claude Code",
            Self::Codex => "Codex",
            Self::Cursor => "Cursor",
        }
    }

    const fn executable(self) -> &'static str {
        match self {
            Self::ClaudeCode => "claude",
            Self::Codex => "codex",
            Self::Cursor => "cursor-agent",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AvailableAgent {
    target: AgentTarget,
    executable: PathBuf,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HandoffPayload {
    body: String,
}

impl HandoffPayload {
    pub fn as_str(&self) -> &str {
        &self.body
    }
}

impl AvailableAgent {
    pub const fn label(&self) -> &'static str {
        self.target.label()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HandoffError {
    UnsafePayload,
    PayloadTooLarge,
    AgentUnavailable(AgentTarget),
    AgentSpawn(AgentTarget, io::ErrorKind),
    AgentExited(AgentTarget),
    ClipboardUnavailable,
    ClipboardFailed,
}

impl fmt::Display for HandoffError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnsafePayload => formatter.write_str("Handoff prompt contains unsafe content."),
            Self::PayloadTooLarge => {
                formatter.write_str("Handoff prompt exceeds the 12 KiB limit.")
            }
            Self::AgentUnavailable(target) => {
                write!(
                    formatter,
                    "{} is no longer available on PATH.",
                    target.label()
                )
            }
            Self::AgentSpawn(target, kind) => write!(
                formatter,
                "{} handoff could not start ({}).",
                target.label(),
                bounded_io_kind(*kind)
            ),
            Self::AgentExited(target) => write!(
                formatter,
                "{} exited before handoff completed.",
                target.label()
            ),
            Self::ClipboardUnavailable => {
                formatter.write_str("Clipboard unavailable; choose another destination.")
            }
            Self::ClipboardFailed => {
                formatter.write_str("Clipboard command failed; choose another destination.")
            }
        }
    }
}

impl std::error::Error for HandoffError {}

pub fn available_agents() -> Vec<AvailableAgent> {
    available_agents_in(env::var_os("PATH").as_deref())
}

fn available_agents_in(path: Option<&OsStr>) -> Vec<AvailableAgent> {
    [
        AgentTarget::ClaudeCode,
        AgentTarget::Codex,
        AgentTarget::Cursor,
    ]
    .into_iter()
    .filter_map(|target| {
        resolve_executable(target.executable(), path)
            .map(|executable| AvailableAgent { target, executable })
    })
    .collect()
}

pub fn build_prompt(
    report: &InspectReport,
    presentation: &ReportPresentation,
    rescan_command: &RescanCommand,
) -> Result<HandoffPayload, HandoffError> {
    let audit = match &report.audit.score {
        Some(score) => format!(
            "Audit: {}/100 {} ({}).",
            score.value,
            score.label,
            if score.authoritative {
                "authoritative"
            } else {
                "partial"
            }
        ),
        None => "Audit: score unavailable.".to_owned(),
    };

    let mut prompt = PromptBuilder::new();
    prompt
        .push_line(
            "Fix the highest-priority Rust Doctor findings. Preserve unrelated changes and rescan after the fixes.",
        )
        .push_line(&audit);

    let mut locations = 0usize;
    for (index, group) in presentation.groups.iter().take(MAX_GROUPS).enumerate() {
        push_group(&mut prompt, index + 1, group, &mut locations);
    }
    for advisory in presentation.migration_advisories.iter().take(MAX_GROUPS) {
        let Some(rule_id) = prompt.require(RuleId::new(&advisory.rule_id)) else {
            break;
        };
        let line = format!(
            "Migration advisory: {} has {} occurrences across {} files.",
            rule_id.as_str(),
            advisory.occurrences,
            advisory.files
        );
        prompt.push_line(&line);
    }
    prompt.push_line(&format!("Validate with: {}", rescan_command.as_str()));
    prompt.finish()
}

fn push_group(
    prompt: &mut PromptBuilder,
    index: usize,
    group: &DiagnosticGroup,
    locations: &mut usize,
) {
    let Some(rule_id) = prompt.require(RuleId::new(&group.rule_id)) else {
        return;
    };
    let heading = format!(
        "{index}. {} ({}, {} occurrences)",
        rule_id.as_str(),
        group.severity,
        group.occurrences
    );
    prompt.push_line(&heading);
    // The catalogued help, never the toolchain's. This prompt is handed to an
    // agent with tool access, so it carries only text this crate wrote: a
    // diagnostic's own message and help are workspace-controlled, and
    // `hostile_fields_are_excluded_or_refused_before_delivery` is what keeps
    // them out. This is why the handoff does not go through
    // `DiagnosticGroup::resolved_help`, which prefers exactly that text.
    if let Some(help) = canonical_rule_help(rule_id.as_str()) {
        prompt.push_line(&format!("   Help: {help}"));
    }
    for diagnostic in &group.diagnostics {
        if *locations >= MAX_LOCATIONS {
            break;
        }
        let Some(location) = diagnostic.location() else {
            continue;
        };
        let Some(path) = prompt.require(RelativeLocation::new(&location.path)) else {
            return;
        };
        let site = format!(
            "   - {}:{}:{}",
            path.as_str(),
            location.span.line_start.max(1),
            location.span.column_start.max(1)
        );
        prompt.push_line(&site);
        *locations += 1;
    }
}

/// One catalogued rule, ready to be handed to an agent on its own.
///
/// The interactive report copies this rather than the whole handoff prompt: a
/// reader who selected one finding wants that finding fixed, not a plan for the
/// backlog. It is built here, next to [`build_prompt`], so both payloads leave
/// through the same gate and inherit the same bound.
pub struct IssuePrompt<'a> {
    pub project_name: &'a str,
    pub rule_id: &'a str,
    pub title: &'a str,
    pub category: &'a str,
    pub is_error: bool,
    pub site_count: usize,
    pub message: &'a str,
    pub help: Option<&'a str>,
    pub rule_url: &'a str,
    pub sites: &'a [String],
}

/// The clipboard payload of one rule, the transposition of
/// `build-issue-prompt.ts`.
///
/// Unlike [`build_prompt`], this one carries the diagnostic's own message and
/// help, which is text the scanned workspace controls. That is the point: a
/// reader asked for one finding explained, and the catalogued help alone does
/// not explain it. It is safe here and not there because nothing executes this
/// payload; it lands on a clipboard for a person to read and paste. Every
/// field is sanitized on the way in, so a message carrying an escape sequence
/// cannot travel through the clipboard, and refusing rather than neutralizing
/// would give the reader an error they cannot act on.
///
/// The rule id is the one field that must be well formed, since it is what the
/// agent is told to fix and to re-verify.
pub fn build_issue_prompt(issue: &IssuePrompt<'_>) -> Result<HandoffPayload, HandoffError> {
    // Validated raw rather than sanitized first: sanitizing a rule id would
    // turn `clippy::a\nb` into the plausible-looking `clippy::ab` and send the
    // agent after a rule that does not exist. Refusing is the honest answer.
    let rule_id = RuleId::new(issue.rule_id)?.as_str().to_owned();
    let severity = if issue.is_error { "ERROR" } else { "WARN" };

    let mut prompt = PromptBuilder::new();
    prompt
        .push_line(&format!(
            "Fix exactly one Rust Doctor rule in {}:",
            sanitize(issue.project_name)
        ))
        .push_line("")
        .push_line(&format!(
            "{severity} {}: {} ({rule_id}, ×{})",
            sanitize(issue.category),
            sanitize(issue.title),
            issue.site_count
        ))
        .push_line(&sanitize(issue.message));
    if let Some(help) = issue.help {
        prompt
            .push_line("")
            .push_line(&format!("Suggested fix: {}", sanitize(help)));
    }

    prompt
        .push_line("")
        .push_line("Scope:")
        .push_line(&format!("- Fix only {rule_id}."))
        .push_line("- Fix the root cause; do not suppress, disable, or silence the rule.")
        .push_line("- Keep unrelated refactors out of this pass.")
        .push_line("")
        .push_line("Affected sites:");
    for site in issue.sites.iter().take(MAX_ISSUE_SITES) {
        prompt.push_line(&format!("- {}", sanitize(site)));
    }
    let remaining = issue.sites.len().saturating_sub(MAX_ISSUE_SITES);
    if remaining > 0 {
        prompt.push_line(&format!("- +{remaining} more sites"));
    }

    if !issue.rule_url.is_empty() {
        prompt
            .push_line("")
            .push_line(&format!("Learn more: {}", sanitize(issue.rule_url)));
    }

    prompt.push_line("").push_line(&format!(
        "Verify with `rust-doctor . --yes --verbose` and confirm {rule_id} is gone before moving on."
    ));
    prompt.finish()
}

/// The payload under construction, and the first reason it cannot be
/// delivered.
///
/// The refusal is held rather than returned per line. A prompt is a template:
/// twenty literal lines returning a `Result` each meant twenty `?` in a
/// function with no branching in it, which is what made `build_issue_prompt`
/// one of the crate's own complexity hotspots at cyclomatic 26 and cognitive
/// 6. Nothing is appended once a refusal is held, so the bound and the control
/// character check stop the payload exactly where they did before, and
/// [`finish`](Self::finish) is the one place either is reported.
struct PromptBuilder {
    body: String,
    refused: Option<HandoffError>,
}

impl PromptBuilder {
    fn new() -> Self {
        Self {
            body: String::new(),
            refused: None,
        }
    }

    fn push_line(&mut self, line: &str) -> &mut Self {
        if self.refused.is_some() {
            return self;
        }
        if line.chars().any(char::is_control) {
            self.refused = Some(HandoffError::UnsafePayload);
            return self;
        }
        if self.body.len().saturating_add(line.len()).saturating_add(1) > MAX_HANDOFF_BYTES {
            self.refused = Some(HandoffError::PayloadTooLarge);
            return self;
        }
        self.body.push_str(line);
        self.body.push('\n');
        self
    }

    /// A value that has to be well formed rather than merely safe, such as a
    /// rule id: the refusal is recorded the same way, so the caller stays a
    /// straight line.
    fn require<T>(&mut self, checked: Result<T, HandoffError>) -> Option<T> {
        match checked {
            Ok(value) => Some(value),
            Err(error) => {
                self.refused.get_or_insert(error);
                None
            }
        }
    }

    fn finish(self) -> Result<HandoffPayload, HandoffError> {
        if let Some(refused) = self.refused {
            return Err(refused);
        }
        if self.body.len() > MAX_HANDOFF_BYTES {
            return Err(HandoffError::PayloadTooLarge);
        }
        Ok(HandoffPayload { body: self.body })
    }
}

struct RuleId<'a>(&'a str);

impl<'a> RuleId<'a> {
    fn new(value: &'a str) -> Result<Self, HandoffError> {
        if value.is_empty()
            || !value.bytes().all(|byte| {
                byte.is_ascii_alphanumeric() || matches!(byte, b':' | b'_' | b'-' | b'.')
            })
        {
            return Err(HandoffError::UnsafePayload);
        }
        Ok(Self(value))
    }

    const fn as_str(&self) -> &'a str {
        self.0
    }
}

struct RelativeLocation<'a>(&'a str);

impl<'a> RelativeLocation<'a> {
    fn new(value: &'a str) -> Result<Self, HandoffError> {
        let path = Path::new(value);
        if value.is_empty()
            || value.chars().any(char::is_control)
            || value.contains('\\')
            || has_windows_drive_prefix(value)
            || path.is_absolute()
            || path
                .components()
                .any(|component| !matches!(component, Component::Normal(_)))
        {
            return Err(HandoffError::UnsafePayload);
        }
        Ok(Self(value))
    }

    const fn as_str(&self) -> &'a str {
        self.0
    }
}

fn has_windows_drive_prefix(value: &str) -> bool {
    let bytes = value.as_bytes();
    bytes.len() >= 3
        && bytes[0].is_ascii_alphabetic()
        && bytes[1] == b':'
        && matches!(bytes[2], b'/' | b'\\')
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RescanCommand(String);

impl RescanCommand {
    pub fn for_inspection(
        verbose: bool,
        blocking: Option<BlockingLevel>,
        rule_overrides: &[RuleOverride],
        category_overrides: &[CategoryOverride],
        scope: Option<(ScopeMode, &str)>,
    ) -> Result<Self, HandoffError> {
        let mut arguments = vec!["rust-doctor".to_owned(), ".".to_owned()];
        if verbose {
            arguments.push("--verbose".to_owned());
        }
        if let Some(blocking) = blocking {
            arguments.push("--blocking".to_owned());
            arguments.push(blocking.to_string());
        }
        for rule_override in rule_overrides {
            arguments.push("--rule".to_owned());
            arguments.push(rule_override.to_string());
        }
        for category_override in category_overrides {
            arguments.push("--category".to_owned());
            arguments.push(category_override.to_string());
        }
        if let Some((scope, base)) = scope {
            validate_git_base(base)?;
            arguments.push("--scope".to_owned());
            arguments.push(
                match scope {
                    ScopeMode::Files => "files",
                    ScopeMode::Baseline => "baseline",
                    ScopeMode::Full => return Err(HandoffError::UnsafePayload),
                }
                .to_owned(),
            );
            arguments.push("--base".to_owned());
            arguments.push(base.to_owned());
        }
        arguments.push("--yes".to_owned());

        let mut command = String::new();
        for argument in &arguments {
            if !command.is_empty() {
                command.push(' ');
            }
            command.push_str(&shell_argument(argument));
        }
        Ok(Self(command))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

fn validate_git_base(base: &str) -> Result<(), HandoffError> {
    if !(1..=256).contains(&base.len())
        || Path::new(base).is_absolute()
        || !base.bytes().all(|byte| {
            byte.is_ascii_alphanumeric()
                || matches!(
                    byte,
                    b'-' | b'_' | b'.' | b'/' | b'~' | b'^' | b'@' | b'{' | b'}'
                )
        })
    {
        return Err(HandoffError::UnsafePayload);
    }
    Ok(())
}

fn shell_argument(argument: &str) -> String {
    if argument
        .chars()
        .all(|character| character.is_ascii_alphanumeric() || "-._/".contains(character))
    {
        argument.to_owned()
    } else {
        format!("'{0}'", argument.replace('\'', "'\\''"))
    }
}

pub fn launch_agent(
    agent: &AvailableAgent,
    payload: &HandoffPayload,
    workspace_root: &Path,
) -> Result<(), HandoffError> {
    if !is_executable_file(&agent.executable) {
        return Err(HandoffError::AgentUnavailable(agent.target));
    }
    let status = Command::new(&agent.executable)
        .arg(payload.as_str())
        .current_dir(workspace_root)
        .stdin(Stdio::inherit())
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .status()
        .map_err(|error| HandoffError::AgentSpawn(agent.target, error.kind()))?;
    if status.success() {
        Ok(())
    } else {
        Err(HandoffError::AgentExited(agent.target))
    }
}

pub fn copy_to_clipboard(payload: &HandoffPayload) -> Result<(), HandoffError> {
    copy_text(payload.as_str())
}

/// Hands a URL to the desktop, reporting whether the launcher accepted it.
///
/// Both streams go to the void: a browser launcher that writes to stdout would
/// land in the middle of an interactive frame. Only compiled-in URLs travel
/// through here, which is what makes the `cmd /C start` form on Windows safe.
pub fn open_url(url: &str) -> bool {
    let (program, arguments): (&str, &[&str]) = if cfg!(target_os = "macos") {
        ("open", &[])
    } else if cfg!(target_os = "windows") {
        ("cmd", &["/C", "start", ""])
    } else {
        ("xdg-open", &[])
    };
    Command::new(program)
        .args(arguments)
        .arg(url)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .is_ok_and(|status| status.success())
}

/// Puts arbitrary already-sanitized text on the clipboard. The interactive
/// report copies one rule at a time rather than the whole handoff prompt; that
/// payload comes out of [`build_issue_prompt`].
pub fn copy_text(text: &str) -> Result<(), HandoffError> {
    let path = env::var_os("PATH");
    copy_to_clipboard_in(text, path.as_deref(), clipboard_candidates())
}

fn clipboard_candidates() -> &'static [(&'static str, &'static [&'static str])] {
    if cfg!(target_os = "macos") {
        &[("pbcopy", &[])]
    } else if cfg!(target_os = "windows") {
        &[("clip", &[])]
    } else {
        &[
            ("wl-copy", &[]),
            ("xclip", &["-selection", "clipboard"]),
            ("xsel", &["--clipboard", "--input"]),
        ]
    }
}

fn copy_to_clipboard_in(
    payload: &str,
    path: Option<&OsStr>,
    candidates: &[(&str, &[&str])],
) -> Result<(), HandoffError> {
    let mut found = false;
    for (program, arguments) in candidates {
        let Some(executable) = resolve_executable(program, path) else {
            continue;
        };
        found = true;
        if run_clipboard(&executable, arguments, payload).is_ok() {
            return Ok(());
        }
    }
    if found {
        Err(HandoffError::ClipboardFailed)
    } else {
        Err(HandoffError::ClipboardUnavailable)
    }
}

fn run_clipboard(executable: &Path, arguments: &[&str], payload: &str) -> io::Result<()> {
    let mut child = Command::new(executable)
        .args(arguments)
        .stdin(Stdio::piped())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()?;
    let write_result = child
        .stdin
        .take()
        .ok_or_else(|| io::Error::other("clipboard stdin unavailable"))?
        .write_all(payload.as_bytes());
    if let Err(error) = write_result {
        let _ = child.kill();
        let _ = child.wait();
        return Err(error);
    }
    let deadline = Instant::now() + CLIPBOARD_TIMEOUT;
    loop {
        if let Some(status) = child.try_wait()? {
            return status
                .success()
                .then_some(())
                .ok_or_else(|| io::Error::other("clipboard command failed"));
        }
        if Instant::now() >= deadline {
            child.kill()?;
            let _ = child.wait();
            return Err(io::Error::new(
                io::ErrorKind::TimedOut,
                "clipboard command timed out",
            ));
        }
        thread::sleep(Duration::from_millis(10));
    }
}

fn resolve_executable(program: &str, path: Option<&OsStr>) -> Option<PathBuf> {
    let path = path?;
    for directory in env::split_paths(path) {
        for candidate in executable_candidates(&directory, program) {
            if is_executable_file(&candidate) {
                return Some(candidate);
            }
        }
    }
    None
}

fn executable_candidates(directory: &Path, program: &str) -> Vec<PathBuf> {
    if cfg!(windows) {
        ["exe", "cmd", "bat"]
            .into_iter()
            .map(|extension| directory.join(format!("{program}.{extension}")))
            .collect()
    } else {
        vec![directory.join(program)]
    }
}

fn is_executable_file(path: &Path) -> bool {
    let Ok(metadata) = fs::metadata(path) else {
        return false;
    };
    if !metadata.is_file() {
        return false;
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        metadata.permissions().mode() & 0o111 != 0
    }
    #[cfg(not(unix))]
    {
        true
    }
}

const fn bounded_io_kind(kind: io::ErrorKind) -> &'static str {
    match kind {
        io::ErrorKind::NotFound => "not found",
        io::ErrorKind::PermissionDenied => "permission denied",
        io::ErrorKind::Interrupted => "interrupted",
        _ => "process error",
    }
}

#[cfg(test)]
mod tests;