From da0553ac987b529d8508a5e115e66e0752591486 Mon Sep 17 00:00:00 2001 From: LHagfoss Date: Sun, 22 Nov 2026 21:44:45 +0100 Subject: [PATCH] fix(engine): start npm adapters from the registry when the cache is stale --- rustcode/engine/src/harness/mod.rs | 1 + rustcode/engine/src/harness/npx.rs | 55 ++++++++++++++++++++++++++ rustcode/engine/src/harness/session.rs | 27 +++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 rustcode/engine/src/harness/npx.rs diff --git a/rustcode/engine/src/harness/mod.rs b/rustcode/engine/src/harness/mod.rs index 1395a1edc..cb30bb900 100644 --- a/rustcode/engine/src/harness/mod.rs +++ b/rustcode/engine/src/harness/mod.rs @@ -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; diff --git a/rustcode/engine/src/harness/npx.rs b/rustcode/engine/src/harness/npx.rs new file mode 100644 index 000000000..42f46200a --- /dev/null +++ b/rustcode/engine/src/harness/npx.rs @@ -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> { + 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 { + 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 + ); + } +} diff --git a/rustcode/engine/src/harness/session.rs b/rustcode/engine/src/harness/session.rs index e50c6ffb0..9835527b1 100644 --- a/rustcode/engine/src/harness/session.rs +++ b/rustcode/engine/src/harness/session.rs @@ -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 { + 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 { let (mut env, remove) = launch_env(harness, &options); // A delegate building a Rust project in a worktree: a shared build