Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

report 128 + signal for a ! command killed by a signal (#3764)

kt26b32c9c5d0b
diff --git a/.changes/user-bash-signal-exit-code.md b/.changes/user-bash-signal-exit-code.md
new file mode 100644
index 0000000000..de2d142b89
--- /dev/null
+++ b/.changes/user-bash-signal-exit-code.md
@@ -0,0 +1 @@
+- A `!` command or a model `bash` tool call whose shell dies from a signal (for example a crash, an OOM kill or `kill`) now reports 128 + the signal number as its exit code, so the model is told it failed, like the TypeScript version.
diff --git a/crates/pa-core/src/platform/mod.rs b/crates/pa-core/src/platform/mod.rs
index df19b698b7..2f261d34b7 100644
--- a/crates/pa-core/src/platform/mod.rs
+++ b/crates/pa-core/src/platform/mod.rs
@@ -15,8 +15,8 @@ pub use perms::{
file_mode, is_executable, is_readable_writable, restrict_dir, restrict_file, set_private_mode,
};
pub use process::{
- kill_pid, kill_process_group_or_pid, pid_exists, set_new_process_group, set_no_window,
- termination_signal, Signal,
+ kill_pid, kill_process_group_or_pid, normalized_exit_code, pid_exists, set_new_process_group,
+ set_no_window, termination_signal, Signal,
};
// The rename-onto-destination primitive (bounded win32 destination-busy
// retry) lives in pa-telemetry — the bottom crate every persist owner
diff --git a/crates/pa-core/src/platform/process.rs b/crates/pa-core/src/platform/process.rs
index c6caeaef55..9bb4b6e54e 100644
--- a/crates/pa-core/src/platform/process.rs
+++ b/crates/pa-core/src/platform/process.rs
@@ -578,6 +578,16 @@ pub fn termination_signal(_status: &std::process::ExitStatus) -> Option<i32> {
None
}
+/// A child's exit code as a shell's `$?` reports it: the status code, or
+/// 128 + the signal number when a signal killed it (TS `normalizedExitCode`).
+/// Without the mapping a signal death carries no code and reads as success.
+#[must_use]
+pub fn normalized_exit_code(status: &std::process::ExitStatus) -> Option<i32> {
+ status
+ .code()
+ .or_else(|| termination_signal(status).map(|signal| 128 + signal))
+}
+
/// The kernel32 termination surface for [`kill_pid`], hand-declared (repo
/// policy: pinned constants and externs, no windows-sys dependency).
#[cfg(windows)]
diff --git a/crates/pa-core/src/tools/bash.rs b/crates/pa-core/src/tools/bash.rs
index f77616a63e..5847729fa5 100644
--- a/crates/pa-core/src/tools/bash.rs
+++ b/crates/pa-core/src/tools/bash.rs
@@ -48,12 +48,12 @@ pub struct ExecOptions<'a> {
}
/// Pluggable operations for the bash tool. Implementations stream merged stdout/stderr through
-/// `on_data`, kill the process tree on cancellation, resolve with the child exit code (`None` when
-/// killed by a signal), and reject with `"aborted"` / `"timeout:<seconds>"`. Object-safe on purpose
-/// (`&dyn` injection).
+/// `on_data`, kill the process tree on cancellation, resolve with the child exit code (128 + the
+/// signal number when killed by a signal, like TS `waitForChildProcess`), and reject with
+/// `"aborted"` / `"timeout:<seconds>"`. Object-safe on purpose (`&dyn` injection).
pub trait BashOperations: Send + Sync {
- /// Execute a command and stream output through `on_data`; resolves with the exit code (`None`
- /// when killed by a signal), or an error: `"aborted"` or `"timeout:<seconds>"`.
+ /// Execute a command and stream output through `on_data`; resolves with the exit code (128 +
+ /// the signal number when killed by a signal), or an error: `"aborted"` or `"timeout:<seconds>"`.
fn exec<'a>(
&'a self,
command: &'a str,
diff --git a/crates/pa-core/src/tools/bash_local.rs b/crates/pa-core/src/tools/bash_local.rs
index 4e45a0efad..fc00311161 100644
--- a/crates/pa-core/src/tools/bash_local.rs
+++ b/crates/pa-core/src/tools/bash_local.rs
@@ -1,6 +1,6 @@
//! Local shell execution backend for the bash tool: spawns the shell in its own process group,
//! streams stdout+stderr through one ordered channel, kills the whole group on timeout or abort,
-//! and resolves with the exit code (`None` when killed by a signal).
+//! and resolves with the exit code (128 + the signal number when killed by a signal).
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
@@ -167,7 +167,38 @@ impl BashOperations for LocalBashOperations {
if timed_out.load(Ordering::SeqCst) {
anyhow::bail!("timeout:{}", timeout.unwrap_or_default());
}
- Ok(status.code())
+ Ok(crate::platform::process::normalized_exit_code(&status))
})
}
}
+
+#[cfg(all(test, unix))]
+mod tests {
+ use crate::tools::bash::{execute_bash, BashToolOptions};
+
+ /// A command killed by a signal fails with 128 + the signal number, like TS
+ /// `waitForChildProcess`; with no exit code the model would read it as a success.
+ #[tokio::test]
+ async fn a_signal_death_fails_with_128_plus_the_signal() {
+ let cwd = tempfile::tempdir().expect("tempdir");
+ let cwd = cwd.path().to_str().expect("utf-8 tempdir");
+ for (command, code) in [("kill -KILL $$", 137), ("kill -SEGV $$", 139)] {
+ let err = execute_bash(
+ cwd,
+ &BashToolOptions::default(),
+ command,
+ None,
+ None,
+ None,
+ None,
+ )
+ .await
+ .expect_err(command);
+ assert_eq!(
+ err.to_string(),
+ format!("(no output)\n\nCommand exited with code {code}"),
+ "{command}"
+ );
+ }
+ }
+}
diff --git a/crates/pa-daemon/src/user_bash.rs b/crates/pa-daemon/src/user_bash.rs
index 8cb48ea304..ce61b32f28 100644
--- a/crates/pa-daemon/src/user_bash.rs
+++ b/crates/pa-daemon/src/user_bash.rs
@@ -653,7 +653,7 @@ async fn run_bash(run: RunBash<'_>) -> BashEnd {
});
let mut pumps_done = false;
// The run settles on the shell's exit, not on its pipes closing
- // (abort_bash may kill it meanwhile; a signal death reports no exit code).
+ // (abort_bash may kill it meanwhile; a cancelled run reports no exit code).
let status = loop {
tokio::select! {
// If both are ready, kill while the unreaped child still pins its PID.
@@ -672,7 +672,12 @@ async fn run_bash(run: RunBash<'_>) -> BashEnd {
let _ = tokio::time::timeout(EXIT_STDIO_GRACE, &mut pumps).await;
}
drop(pumps);
- let exit_code = status.ok().and_then(|status| status.code()).map(i64::from);
+ // A signal death reports 128 + the signal number, like a shell's `$?` (TS
+ // `normalizedExitCode`); a cancelled run still reports none below.
+ let exit_code = status
+ .ok()
+ .and_then(|status| pa_core::platform::process::normalized_exit_code(&status))
+ .map(i64::from);
let cancelled = run.cancellation.is_cancelled();
// The pumps are dropped, so this is the last reference; a poisoned
// lock (a reader panicked) keeps whatever it collected.
@@ -1030,6 +1035,38 @@ mod tests {
);
}
+ /// A command whose shell dies by a signal reports 128 + the signal number, like a shell's
+ /// `$?` (TS `normalizedExitCode`): with no exit code the model would read it as a success.
+ #[cfg(unix)]
+ #[tokio::test]
+ async fn a_signal_death_reports_128_plus_the_signal() {
+ let cwd = tempfile::tempdir().expect("tempdir");
+ let worker = created_worker(cwd.path()).await;
+ for (command, code) in [
+ ("kill -TERM $$", 143),
+ ("kill -KILL $$", 137),
+ ("kill -SEGV $$", 139),
+ ] {
+ let response = worker
+ .dispatch(
+ "execute_bash_and_wait",
+ &json!({ "activeSessionId": "bash-session", "command": command }),
+ )
+ .await;
+ assert!(response.success, "failed: {response:?}");
+ assert_eq!(
+ response.data,
+ Some(json!({
+ "output": "",
+ "exitCode": code,
+ "cancelled": false,
+ "truncated": false,
+ })),
+ "{command}"
+ );
+ }
+ }
+
#[test]
fn an_abort_stays_with_every_admitted_run_when_another_run_starts() {
let user_bash = UserBash::new();