Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 50 additions & 9 deletions src/detect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,12 +583,31 @@ fn agent_name_from_path_token(token: &str) -> Option<String> {
}

fn agent_name_from_known_package_path(path: &str) -> Option<String> {
let components: Vec<String> = path
let raw_components: Vec<&str> = path
.split(['/', '\\'])
.filter(|component| !component.is_empty())
.map(normalized_agent_lookup_name)
.collect();
let bundled_pi_cli = [
"node_modules",
"@earendil-works",
"pi-coding-agent",
"dist",
"bundle",
"cli.js",
];
if raw_components.len() >= bundled_pi_cli.len()
&& raw_components[raw_components.len() - bundled_pi_cli.len()..]
.iter()
.zip(bundled_pi_cli)
.all(|(actual, expected)| actual.eq_ignore_ascii_case(expected))
{
return Some(agent_label(Agent::Pi).to_string());
}

let components: Vec<String> = raw_components
.into_iter()
.map(normalized_agent_lookup_name)
.collect();
for window in components.windows(5) {
if window
== [
Expand Down Expand Up @@ -1125,40 +1144,62 @@ mod tests {
}

#[test]
fn identify_agent_in_job_detects_node_wrapped_mastracode_package_cli() {
fn identify_agent_in_job_detects_node_wrapped_pi_bundled_cli() {
let job = crate::platform::ForegroundJob {
process_group_id: 123,
processes: vec![foreground_process(
123,
"node.exe",
&[
"node.exe",
"C:\\Users\\herdr\\AppData\\Roaming\\npm\\node_modules\\mastracode\\dist\\cli.js",
r"C:\Users\herdr\AppData\Local\pi-node\current\node.exe",
r"C:\Users\herdr\AppData\Local\pi-node\current/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js",
],
)],
};

assert_eq!(
identify_agent_in_job(&job),
Some((Agent::Mastracode, "mastracode".to_string()))
Some((Agent::Pi, "pi".to_string()))
);
}

#[test]
fn identify_agent_in_job_ignores_non_cli_pi_package_script() {
fn identify_agent_in_job_detects_node_wrapped_mastracode_package_cli() {
let job = crate::platform::ForegroundJob {
process_group_id: 123,
processes: vec![foreground_process(
123,
"node.exe",
&[
"node.exe",
"C:\\Users\\herdr\\AppData\\Roaming\\npm\\node_modules\\@earendil-works\\pi-coding-agent\\scripts\\build.js",
"C:\\Users\\herdr\\AppData\\Roaming\\npm\\node_modules\\mastracode\\dist\\cli.js",
],
)],
};

assert_eq!(identify_agent_in_job(&job), None);
assert_eq!(
identify_agent_in_job(&job),
Some((Agent::Mastracode, "mastracode".to_string()))
);
}

#[test]
fn identify_agent_in_job_ignores_non_cli_pi_package_scripts() {
for script in [
r"C:\Users\herdr\AppData\Roaming\npm\node_modules\@earendil-works\pi-coding-agent\scripts\build.js",
r"C:\Users\herdr\AppData\Local\pi-node\current\node_modules\@earendil-works\pi-coding-agent\dist\bundle\update.js",
r"C:\workspace\dist\bundle\cli.js",
r"C:\workspace\node_modules\other-package\dist\bundle\cli.js",
r"C:\workspace\node_modules\@earendil-works\pi-coding-agent\dist\bundle\cli.exe",
r"C:\workspace\node_modules\@earendil-works\pi-coding-agent\dist\bundle\cli.js\other.js",
] {
let job = crate::platform::ForegroundJob {
process_group_id: 123,
processes: vec![foreground_process(123, "node.exe", &["node.exe", script])],
};

assert_eq!(identify_agent_in_job(&job), None, "script: {script}");
}
}

#[test]
Expand Down
Loading