A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
[RSI] Make agent-spawned shells non-interactive-safe by default (#2219)
Seth Karten766ddce2ce03
diff --git a/packages/coding-agent/.changes/eng-6087-noninteractive-shell-defaults.md b/packages/coding-agent/.changes/eng-6087-noninteractive-shell-defaults.mdnew file mode 100644index 0000000000..33511f864d--- /dev/null+++ b/packages/coding-agent/.changes/eng-6087-noninteractive-shell-defaults.md@@ -0,0 +1 @@+- Fixed agent-spawned shells hanging on interactive prompts: git commit/rebase without -m, credential asks, and pagers now fail fast or no-op because GIT_EDITOR, EDITOR, VISUAL, PAGER, and related variables default to non-interactive values.diff --git a/packages/coding-agent/src/utils/shell.ts b/packages/coding-agent/src/utils/shell.tsindex 4e5fc19201..1b0d872860 100644--- a/packages/coding-agent/src/utils/shell.ts+++ b/packages/coding-agent/src/utils/shell.ts@@ -152,9 +152,30 @@ export function getShellEnv(): NodeJS.ProcessEnv {const hasBinDir = pathEntries.includes(binDir);const updatedPath = hasBinDir ? currentPath : [binDir, currentPath].filter(Boolean).join(delimiter);+ // Agent-spawned shells never have a usable stdin (stdio: ["ignore", "pipe", "pipe"]),+ // so any interactive prompt opened via /dev/tty is a guaranteed hang until killed:+ // `git commit` without -m launches $EDITOR, credential helpers block waiting for a+ // password, pagers read the terminal directly. Make those cases fail fast or no-op+ // instead of hanging.+ //+ // These deliberately override inherited terminal settings (an EDITOR=vim inherited+ // from the launching shell is exactly the hang we are preventing, and stdin is+ // ignored even for user `!` commands, so an interactive editor can never receive+ // keystrokes anyway). A user who wants a prompt in a specific command can override+ // inline (`GIT_EDITOR=vim git commit`), which takes precedence over exported vars.return {...process.env,[pathKey]: updatedPath,+ GIT_EDITOR: "true",+ GIT_SEQUENCE_EDITOR: "true",+ GIT_TERMINAL_PROMPTS: "0",+ GIT_ASKPASS: "true",+ SSH_ASKPASS_REQUIRE: "never",+ EDITOR: "true",+ VISUAL: "true",+ PAGER: "cat",+ GIT_PAGER: "cat",+ DEBIAN_FRONTEND: "noninteractive",};}diff --git a/packages/coding-agent/test/shell-env.test.ts b/packages/coding-agent/test/shell-env.test.tsnew file mode 100644index 0000000000..8de5533fee--- /dev/null+++ b/packages/coding-agent/test/shell-env.test.ts@@ -0,0 +1,78 @@+import { afterEach, beforeEach, describe, expect, it } from "vitest";+import { getShellEnv } from "../src/utils/shell.js";++const GUARD_VARS: Record<string, string> = {+ GIT_EDITOR: "true",+ GIT_SEQUENCE_EDITOR: "true",+ GIT_TERMINAL_PROMPTS: "0",+ GIT_ASKPASS: "true",+ SSH_ASKPASS_REQUIRE: "never",+ EDITOR: "true",+ VISUAL: "true",+ PAGER: "cat",+ GIT_PAGER: "cat",+ DEBIAN_FRONTEND: "noninteractive",+};++const KEPT = [+ "GIT_EDITOR",+ "GIT_SEQUENCE_EDITOR",+ "GIT_TERMINAL_PROMPTS",+ "GIT_ASKPASS",+ "SSH_ASKPASS_REQUIRE",+ "EDITOR",+ "VISUAL",+ "PAGER",+ "GIT_PAGER",+ "DEBIAN_FRONTEND",+];++describe("getShellEnv", () => {+ const saved: Record<string, string | undefined> = {};++ beforeEach(() => {+ for (const key of KEPT) {+ saved[key] = process.env[key];+ delete process.env[key];+ }+ });++ afterEach(() => {+ for (const key of KEPT) {+ const value = saved[key];+ if (value === undefined) {+ delete process.env[key];+ } else {+ process.env[key] = value;+ }+ }+ });++ it("sets non-interactive defaults for agent-spawned shells", () => {+ const env = getShellEnv();+ for (const [key, value] of Object.entries(GUARD_VARS)) {+ expect(env[key]).toBe(value);+ }+ });++ it("overrides inherited terminal settings instead of honoring them", () => {+ process.env.EDITOR = "vim";+ process.env.PAGER = "less";+ process.env.GIT_SEQUENCE_EDITOR = "vim";+ const env = getShellEnv();+ // stdin is never a TTY for agent shells, so an inherited EDITOR/PAGER is+ // exactly the hang this guard prevents; it must be replaced, not kept.+ expect(env.EDITOR).toBe("true");+ expect(env.PAGER).toBe("cat");+ // GIT_SEQUENCE_EDITOR outranks GIT_EDITOR for `git rebase -i`, so an+ // inherited value would still hang the interactive todo editor.+ expect(env.GIT_SEQUENCE_EDITOR).toBe("true");+ });++ it("keeps unrelated inherited variables intact", () => {+ process.env.PRIME_AGENT_SHELL_ENV_TEST = "sentinel";+ const env = getShellEnv();+ expect(env.PRIME_AGENT_SHELL_ENV_TEST).toBe("sentinel");+ delete process.env.PRIME_AGENT_SHELL_ENV_TEST;+ });+});diff --git a/prime-agent-runtime/src/rlm/bash.py b/prime-agent-runtime/src/rlm/bash.pyindex cf325725b8..34e7700bf6 100644--- a/prime-agent-runtime/src/rlm/bash.py+++ b/prime-agent-runtime/src/rlm/bash.py@@ -1017,7 +1017,33 @@ def _status_script(command: str, completion_a: str, completion_b: str) -> str:def _child_env() -> dict[str, str]:- return {**os.environ, "NO_COLOR": "1", "TERM": "dumb", "CLICOLOR": "0", "FORCE_COLOR": "0"}+ """Environment for kernel-spawned shell commands.++ Same non-interactive guard as the coding-agent shell tool+ (packages/coding-agent/src/utils/shell.ts): agent shell commands have no+ usable stdin, so interactive prompts (git commit without -m opening+ $EDITOR, credential asks, pagers) can only hang. Fail fast or no-op+ instead. Deliberately overrides inherited terminal settings; a+ per-command inline assignment (`GIT_EDITOR=vim git commit`) still wins+ because it replaces the exported value for that command.+ """+ return {+ **os.environ,+ "NO_COLOR": "1",+ "TERM": "dumb",+ "CLICOLOR": "0",+ "FORCE_COLOR": "0",+ "GIT_EDITOR": "true",+ "GIT_SEQUENCE_EDITOR": "true",+ "GIT_TERMINAL_PROMPTS": "0",+ "GIT_ASKPASS": "true",+ "SSH_ASKPASS_REQUIRE": "never",+ "EDITOR": "true",+ "VISUAL": "true",+ "PAGER": "cat",+ "GIT_PAGER": "cat",+ "DEBIAN_FRONTEND": "noninteractive",+ }def _signal_group(pid: int, sig: int) -> bool:diff --git a/prime-agent-runtime/test/test_bash.py b/prime-agent-runtime/test/test_bash.pyindex 0edd053f6e..f11c930b2e 100644--- a/prime-agent-runtime/test/test_bash.py+++ b/prime-agent-runtime/test/test_bash.py@@ -132,6 +132,40 @@ class BashTest(unittest.IsolatedAsyncioTestCase):self.assertIn("400000", result.output)self.assertIn("bytes dropped", result.output)+ def test_child_env_is_non_interactive(self):+ """Agent shells have no usable stdin: interactive prompts (git commit+ opening $EDITOR, credential asks, pagers) can only hang. _child_env()+ must neutralize them, overriding inherited terminal settings."""+ with mock.patch.dict(+ os.environ,+ {+ "EDITOR": "vim",+ "PAGER": "less",+ "GIT_SEQUENCE_EDITOR": "vim",+ "GIT_ASKPASS": "/usr/bin/git-credential-manager",+ "SSH_ASKPASS_REQUIRE": "force",+ },+ ):+ env = bash_module._child_env()+ self.assertEqual(env["GIT_EDITOR"], "true")+ self.assertEqual(env["GIT_SEQUENCE_EDITOR"], "true")+ self.assertEqual(env["EDITOR"], "true")+ self.assertEqual(env["VISUAL"], "true")+ self.assertEqual(env["GIT_TERMINAL_PROMPTS"], "0")+ self.assertEqual(env["GIT_ASKPASS"], "true")+ self.assertEqual(env["SSH_ASKPASS_REQUIRE"], "never")+ self.assertEqual(env["PAGER"], "cat")+ self.assertEqual(env["GIT_PAGER"], "cat")+ self.assertEqual(env["DEBIAN_FRONTEND"], "noninteractive")++ async def test_spawned_shell_receives_non_interactive_env(self):+ handle = bash(+ 'echo "$GIT_EDITOR|$GIT_SEQUENCE_EDITOR|$GIT_TERMINAL_PROMPTS|$GIT_ASKPASS|$SSH_ASKPASS_REQUIRE"'+ )+ result = await handle+ self.assertEqual(result.exit_code, 0)+ self.assertIn("true|true|0|true|never", result.output)+async def test_env_prefix_and_journal(self):with tempfile.TemporaryDirectory() as tmp:journal = os.path.join(tmp, "journal.jsonl")