fix(runtime): sweep node wrapper processes hosting managed agent shims (#1296)

Signed-off-by: Will Pfleger <pfleger.will@gmail.com>
Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1fgdl5qqnh3k3f2xkqrvt7cujalhm623x4s7fdjdj5yrtp5fzjl9qrjpucw <4a1bfa0013bc6d14a8d600d8bf6392efefbd2a26ac3c96c9b2a106b0d12297ca@sprout-oss.stage.blox.sqprod.co>
This commit is contained in:
Will Pfleger
2026-06-25 17:06:59 -04:00
committed by GitHub
parent 8717ddf2eb
commit f072032aaf
8 changed files with 104 additions and 28 deletions

View File

@@ -40,6 +40,12 @@ pub(crate) const KNOWN_AGENT_BINARIES: &[&str] = &[
"buzz_dev_mcp",
];
/// Script interpreters that may host managed agent wrappers (e.g. npm shims).
/// A process whose name matches here is NOT immediately claimed — it must also
/// carry `BUZZ_MANAGED_AGENT` in its environment (checked by the caller via
/// `process_has_buzz_marker()`). This avoids sweeping unrelated node processes.
pub(crate) const KNOWN_SCRIPT_INTERPRETERS: &[&str] = &["node"];
/// Check if a process name matches any of our known agent binaries.
/// Uses exact match or prefix-with-separator to avoid false positives
/// (e.g. `"goose"` must not match `"mongoose"`).
@@ -54,6 +60,15 @@ fn name_matches_known_binary(name: &str) -> bool {
})
}
/// Check if a process name is a known script interpreter that may be hosting
/// a managed agent wrapper (e.g. `node` running an npm shim for `codex-acp`).
/// Callers must additionally verify `BUZZ_MANAGED_AGENT` ownership.
fn name_matches_interpreter(name: &str) -> bool {
KNOWN_SCRIPT_INTERPRETERS
.iter()
.any(|&interp| name == interp)
}
#[cfg(unix)]
pub(crate) fn process_is_running(pid: u32) -> bool {
// Use libc::kill with signal 0 instead of forking a subprocess.
@@ -89,7 +104,9 @@ pub(crate) fn process_belongs_to_us(pid: u32) -> bool {
return false;
}
let name = String::from_utf8_lossy(&buf[..len as usize]);
name_matches_known_binary(&name)
// Fall through for script interpreters (e.g. `node` hosting an npm shim):
// the caller's `process_has_buzz_marker()` check decides true ownership.
name_matches_known_binary(&name) || name_matches_interpreter(&name)
}
#[cfg(all(unix, not(target_os = "macos")))]
@@ -101,13 +118,18 @@ pub(crate) fn process_belongs_to_us(pid: u32) -> bool {
if name_matches_known_binary(name.trim()) {
return true;
}
// Interpreter check: `node` is 4 bytes, never truncated.
if name_matches_interpreter(name.trim()) {
return true;
}
}
// Fallback: read /proc/<pid>/exe which is a symlink to the full binary path.
// This is not subject to the 15-byte truncation limit.
if let Ok(exe_path) = std::fs::read_link(format!("/proc/{pid}/exe")) {
if let Some(basename) = exe_path.file_name().and_then(|n| n.to_str()) {
return name_matches_known_binary(basename);
// Fall through for script interpreters — caller checks the marker.
return name_matches_known_binary(basename) || name_matches_interpreter(basename);
}
}
@@ -1190,6 +1212,7 @@ pub(crate) fn reap_dead_instance_agents(_our_instance_id: &str, _skip_pids: &[u3
pub fn kill_stale_tracked_processes(
records: &mut [ManagedAgentRecord],
runtimes: &HashMap<String, ManagedAgentProcess>,
instance_id: &str,
) -> bool {
use crate::managed_agents::BackendKind;
@@ -1202,7 +1225,7 @@ pub fn kill_stale_tracked_processes(
continue;
};
if !runtimes.contains_key(&record.pubkey) {
if process_belongs_to_us(pid) {
if process_belongs_to_us(pid) && process_has_buzz_marker(pid, instance_id) {
let _ = terminate_process(pid);
}
record.runtime_pid = None;
@@ -1217,6 +1240,7 @@ pub fn kill_stale_tracked_processes(
pub fn sync_managed_agent_processes(
records: &mut [ManagedAgentRecord],
runtimes: &mut HashMap<String, ManagedAgentProcess>,
instance_id: &str,
) -> bool {
let mut changed = false;
let mut exited = Vec::new();
@@ -1270,7 +1294,10 @@ pub fn sync_managed_agent_processes(
continue;
};
if process_is_running(pid) && process_belongs_to_us(pid) {
if process_is_running(pid)
&& process_belongs_to_us(pid)
&& process_has_buzz_marker(pid, instance_id)
{
continue;
}
@@ -1880,7 +1907,10 @@ pub fn start_managed_agent_process(
}
if let Some(pid) = record.runtime_pid {
if process_is_running(pid) && process_belongs_to_us(pid) {
if process_is_running(pid)
&& process_belongs_to_us(pid)
&& process_has_buzz_marker(pid, &current_instance_id(app))
{
record.updated_at = now_iso();
record.last_error = None;
return Ok(());