Skip to content
Open
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
12 changes: 12 additions & 0 deletions src/strands_tools/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ def execute_with_pty(self, command: str, cwd: str, non_interactive_mode: bool) -

if pid == 0: # Child process
try:
# Set up environment for proper terminal emulation
# TERM is needed for pagers like 'less' to work correctly
if "TERM" not in os.environ or not os.environ["TERM"]:
os.environ["TERM"] = "xterm-256color"

# In non-interactive mode, disable pagers to prevent hangs
# Commands like 'git diff', 'git log', 'man' use pagers by default
if non_interactive_mode:
os.environ["GIT_PAGER"] = "cat"
os.environ["PAGER"] = "cat"
os.environ["MANPAGER"] = "cat"

os.chdir(cwd)
os.execvp("/bin/sh", ["/bin/sh", "-c", command])
except Exception as e:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,3 +996,23 @@ def test_shell_non_interactive_parameter(mock_get_user_input, mock_execute_comma

# Verify that get_user_input was not called because non_interactive=True
mock_get_user_input.assert_not_called()


def test_command_executor_sets_pager_env_in_non_interactive():
"""Test that pager environment variables are set correctly in non-interactive mode.

This test verifies that GIT_PAGER, PAGER, and MANPAGER are set to 'cat'
when running in non-interactive mode to prevent pager-related hangs.
"""
# The actual env setting happens in the forked child process
# Here we verify the code structure exists by checking the source
import inspect

source = inspect.getsource(shell.CommandExecutor.execute_with_pty)

# Verify our fix is in place
assert "GIT_PAGER" in source, "GIT_PAGER environment variable should be set"
assert "PAGER" in source, "PAGER environment variable should be set"
assert "MANPAGER" in source, "MANPAGER environment variable should be set"
assert "non_interactive_mode" in source, "non_interactive_mode check should be present"
assert "TERM" in source, "TERM environment variable should be set"