|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Advisory diagnostics for structurally suspicious agent invocations. |
| 5 | +
|
| 6 | +use crate::agents::CodingAgent; |
| 7 | + |
| 8 | +pub(crate) const POSSIBLE_DUPLICATE_AGENT_EXECUTABLE: &str = "possible_duplicate_agent_executable"; |
| 9 | + |
| 10 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 11 | +pub(crate) enum InvocationForm { |
| 12 | + Run, |
| 13 | + Shortcut, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 17 | +pub(crate) struct DuplicateAgentExecutable { |
| 18 | + agent: CodingAgent, |
| 19 | + form: InvocationForm, |
| 20 | + command: Vec<String>, |
| 21 | +} |
| 22 | + |
| 23 | +impl DuplicateAgentExecutable { |
| 24 | + pub(crate) fn detect( |
| 25 | + agent: CodingAgent, |
| 26 | + command: &[String], |
| 27 | + form: InvocationForm, |
| 28 | + ) -> Option<Self> { |
| 29 | + let executable = command.first()?; |
| 30 | + (CodingAgent::infer(executable) == Some(agent)).then(|| Self { |
| 31 | + agent, |
| 32 | + form, |
| 33 | + command: command.to_vec(), |
| 34 | + }) |
| 35 | + } |
| 36 | + |
| 37 | + pub(crate) fn log(&self) { |
| 38 | + let agent = self.agent.as_arg(); |
| 39 | + log::warn!( |
| 40 | + target: "nemo_relay.cli", |
| 41 | + event = "agent_invocation_warning", |
| 42 | + diagnostic_code = POSSIBLE_DUPLICATE_AGENT_EXECUTABLE, |
| 43 | + agent = agent, |
| 44 | + duplicate_executable = agent, |
| 45 | + confidence = "high", |
| 46 | + action = "continued", |
| 47 | + command_modified = false, |
| 48 | + arguments_redacted = true; |
| 49 | + "Possible duplicate agent executable after `--`" |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + pub(crate) fn format_doctor(&self, show_full_command: bool) -> String { |
| 54 | + let visibility = if show_full_command { |
| 55 | + "full command; may contain sensitive data" |
| 56 | + } else { |
| 57 | + "arguments redacted" |
| 58 | + }; |
| 59 | + format!( |
| 60 | + "INVOCATION DIAGNOSTIC\n\ |
| 61 | + code = {POSSIBLE_DUPLICATE_AGENT_EXECUTABLE}\n\ |
| 62 | + confidence = high\n\ |
| 63 | + selected_agent = {}\n\ |
| 64 | + duplicate_executable = {}\n\ |
| 65 | + visibility = {visibility}\n\ |
| 66 | + observed = {}\n\ |
| 67 | + recommended = {}\n\ |
| 68 | + action = continue unchanged", |
| 69 | + self.agent.as_arg(), |
| 70 | + self.agent.as_arg(), |
| 71 | + self.observed_command(show_full_command), |
| 72 | + self.recommended_command(show_full_command), |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + pub(crate) fn format_warning(&self) -> String { |
| 77 | + format!( |
| 78 | + "WARNING: Possible duplicate agent executable after `--`.\n\ |
| 79 | + Diagnostic: {POSSIBLE_DUPLICATE_AGENT_EXECUTABLE}\n\ |
| 80 | + Duplicate executable: {}\n\ |
| 81 | + Observed: {}\n\ |
| 82 | + Recommended: {}\n\ |
| 83 | + Doctor (safe): {}\n\ |
| 84 | + Doctor (full): {}\n\ |
| 85 | + Relay will continue without modifying the command.", |
| 86 | + self.agent.as_arg(), |
| 87 | + self.observed_command(false), |
| 88 | + self.recommended_command(false), |
| 89 | + self.doctor_command(false), |
| 90 | + self.doctor_command(true), |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + fn observed_command(&self, show_full_command: bool) -> String { |
| 95 | + let mut command = self.relay_prefix(); |
| 96 | + command.push("--".into()); |
| 97 | + if show_full_command { |
| 98 | + command.extend(self.command.iter().cloned()); |
| 99 | + } else { |
| 100 | + command.push(self.agent.as_arg().into()); |
| 101 | + if self.command.len() > 1 { |
| 102 | + command.push("<arguments redacted>".into()); |
| 103 | + } |
| 104 | + } |
| 105 | + render_command(&command) |
| 106 | + } |
| 107 | + |
| 108 | + fn recommended_command(&self, show_full_command: bool) -> String { |
| 109 | + let mut command = self.relay_prefix(); |
| 110 | + command.push("--".into()); |
| 111 | + if show_full_command { |
| 112 | + command.extend(self.command.iter().skip(1).cloned()); |
| 113 | + } else if self.command.len() > 1 { |
| 114 | + command.push("<arguments redacted>".into()); |
| 115 | + } |
| 116 | + render_command(&command) |
| 117 | + } |
| 118 | + |
| 119 | + fn doctor_command(&self, show_full_command: bool) -> String { |
| 120 | + let mut command = vec![ |
| 121 | + "nemo-relay".into(), |
| 122 | + "doctor".into(), |
| 123 | + "invocation".into(), |
| 124 | + "--agent".into(), |
| 125 | + self.agent.as_arg().into(), |
| 126 | + ]; |
| 127 | + if self.form == InvocationForm::Shortcut { |
| 128 | + command.push("--shortcut".into()); |
| 129 | + } |
| 130 | + if show_full_command { |
| 131 | + command.push("--show-full-command".into()); |
| 132 | + } |
| 133 | + command.push("--".into()); |
| 134 | + command.push(self.agent.as_arg().into()); |
| 135 | + if show_full_command && self.command.len() > 1 { |
| 136 | + command.push("<original arguments>".into()); |
| 137 | + } |
| 138 | + render_command(&command) |
| 139 | + } |
| 140 | + |
| 141 | + fn relay_prefix(&self) -> Vec<String> { |
| 142 | + match self.form { |
| 143 | + InvocationForm::Run => vec![ |
| 144 | + "nemo-relay".into(), |
| 145 | + "run".into(), |
| 146 | + "--agent".into(), |
| 147 | + self.agent.as_arg().into(), |
| 148 | + ], |
| 149 | + InvocationForm::Shortcut => { |
| 150 | + vec!["nemo-relay".into(), self.agent.as_arg().into()] |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +fn render_command(command: &[String]) -> String { |
| 157 | + command |
| 158 | + .iter() |
| 159 | + .map(|argument| crate::process::shell_quote_arg_for_platform(argument, cfg!(windows))) |
| 160 | + .collect::<Vec<_>>() |
| 161 | + .join(" ") |
| 162 | +} |
| 163 | + |
| 164 | +#[cfg(test)] |
| 165 | +#[path = "../../tests/coverage/shared/invocation_diagnostic_tests.rs"] |
| 166 | +mod tests; |
0 commit comments