Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions rustcode/engine/src/harness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod config;
pub(crate) mod denial;
pub(crate) mod detect;
pub(crate) mod handoff;
pub(crate) mod npx;
pub(crate) mod pi_mcp;
pub(crate) mod pressure;
pub(crate) mod process;
Expand Down
55 changes: 55 additions & 0 deletions rustcode/engine/src/harness/npx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! Starting an npm-published adapter (Claude Code's, Pi's) through
//! `npx --prefer-offline`.

/// npm's flag that starts a cached copy without asking the registry.
const PREFER_OFFLINE: &str = "--prefer-offline";

/// The command to start again with when `error` says npm found no matching
/// version, or `None`. With `--prefer-offline` npm trusts its cached package
/// listing: one fetched before a dependency of the pinned adapter was
/// published fails every start with `ETARGET`, and Retry hits the same cache.
/// Without the flag npm asks the registry, which also refreshes the listing.
pub(crate) fn online_retry(command: &[String], error: &str) -> Option<Vec<String>> {
if !(error.contains("ETARGET") || error.contains("notarget")) {
return None;
}
if !command.iter().any(|arg| arg == PREFER_OFFLINE) {
return None;
}
Some(
command
.iter()
.filter(|arg| *arg != PREFER_OFFLINE)
.cloned()
.collect(),
)
}

#[cfg(test)]
mod tests {
use super::*;

fn words(line: &str) -> Vec<String> {
line.split(' ').map(str::to_owned).collect()
}

#[test]
fn a_stale_cache_retries_without_prefer_offline() {
let error = "Claude Code: the harness process exited\nnpm ERR! code ETARGET\n\
npm ERR! notarget No matching version found for @agentclientprotocol/sdk@2.9.1.";
assert_eq!(
online_retry(&words("npx --prefer-offline -y adapter@1.0.0"), error),
Some(words("npx -y adapter@1.0.0"))
);
}

#[test]
fn other_failures_and_online_commands_do_not_retry() {
let command = words("npx --prefer-offline -y adapter@1.0.0");
assert_eq!(online_retry(&command, "Claude Code: not signed in"), None);
assert_eq!(
online_retry(&words("npx -y adapter@1.0.0"), "npm ERR! code ETARGET"),
None
);
}
}
27 changes: 27 additions & 0 deletions rustcode/engine/src/harness/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,38 @@ fn launch_env(
}

impl Adapter {
/// Start `harness`'s child and connect to it. An npm adapter that found no
/// matching version in a stale cache starts once more from the registry
/// (`super::npx`).
pub(crate) async fn start(
harness: &ResolvedHarness,
cwd: &Path,
options: StartOptions,
out: OutputSender,
) -> Result<Self, String> {
let started = Self::start_once(harness, cwd, options.clone(), out.clone()).await;
let Err(error) = &started else {
return started;
};
let Some(command) = super::npx::online_retry(&harness.command, error) else {
return started;
};
crate::dbg_log!(
"{}: npm found no matching version in its cache; starting again from the registry",
harness.name
);
let online = ResolvedHarness {
command,
..harness.clone()
};
Self::start_once(&online, cwd, options, out).await
}

async fn start_once(
harness: &ResolvedHarness,
cwd: &Path,
options: StartOptions,
out: OutputSender,
) -> Result<Self, String> {
let (mut env, remove) = launch_env(harness, &options);
// A delegate building a Rust project in a worktree: a shared build
Expand Down
Loading