|
| 1 | +# Inline Python Steering |
| 2 | + |
| 3 | +Prevent unstable inline Python execution by steering agents toward script-based execution. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Inline Python Steering feature intercepts attempts to run inline Python code via shell commands (e.g., `python -c "..."`) and guides the agent to use temporary scripts instead. Inline Python code execution in terminals is often unstable, prone to quoting issues, and difficult to debug. By enforcing script usage, this feature improves reliability and maintainability of agent-generated code execution. |
| 8 | + |
| 9 | +When an agent attempts to execute inline Python, the proxy blocks the call and returns a steering message explaining the issue and suggesting the creation of a temporary script. |
| 10 | + |
| 11 | +## Key Features |
| 12 | + |
| 13 | +- **Automatic Detection**: Recognizes various inline Python patterns (`python -c`, `python3 -c`, etc.) |
| 14 | +- **Immediate Blocking**: Prevents the command from executing on the host |
| 15 | +- **Helpful Steering**: Explains why the command was blocked and what to do instead |
| 16 | +- **Robust Parsing**: Handles various python executables and flags |
| 17 | +- **Tool Awareness**: Monitors known shell execution tools |
| 18 | + |
| 19 | +## Configuration |
| 20 | + |
| 21 | +The feature is disabled by default and can be enabled via environment variable or YAML configuration. |
| 22 | + |
| 23 | +### Environment Variable |
| 24 | + |
| 25 | +```bash |
| 26 | +export INLINE_PYTHON_STEERING_ENABLED=true |
| 27 | +``` |
| 28 | + |
| 29 | +### YAML Configuration |
| 30 | + |
| 31 | +```yaml |
| 32 | +session: |
| 33 | + inline_python_steering_enabled: true # Default: false |
| 34 | + |
| 35 | + # Optional: Custom steering message |
| 36 | + inline_python_steering_message: | |
| 37 | + Inline Python execution is blocked for stability reasons. |
| 38 | + Please write your code to a temporary file (e.g., script.py) and execute that file instead. |
| 39 | +``` |
| 40 | +
|
| 41 | +## Usage Examples |
| 42 | +
|
| 43 | +### Enable with Default Message |
| 44 | +
|
| 45 | +Set the environment variable before running the proxy: |
| 46 | +
|
| 47 | +```bash |
| 48 | +export INLINE_PYTHON_STEERING_ENABLED=true |
| 49 | +.venv/Scripts/python.exe -m src.core.cli --default-backend openai |
| 50 | +``` |
| 51 | + |
| 52 | +### Enable with Custom Message |
| 53 | + |
| 54 | +Create `config/my_config.yaml`: |
| 55 | + |
| 56 | +```yaml |
| 57 | +session: |
| 58 | + inline_python_steering_enabled: true |
| 59 | + inline_python_steering_message: | |
| 60 | + [Security/Stability Notice] |
| 61 | + You are attempting to run inline Python code (python -c). |
| 62 | + This pattern is unreliable in this environment. |
| 63 | + |
| 64 | + Please: |
| 65 | + 1. Create a file named 'temp_script.py' with your code |
| 66 | + 2. Run 'python temp_script.py' |
| 67 | +``` |
| 68 | +
|
| 69 | +Then run: |
| 70 | +
|
| 71 | +```bash |
| 72 | +.venv/Scripts/python.exe -m src.core.cli --config config/my_config.yaml |
| 73 | +``` |
| 74 | + |
| 75 | +## Detection Logic |
| 76 | + |
| 77 | +The handler detects commands matching the following patterns: |
| 78 | + |
| 79 | +- `python -c "..."` |
| 80 | +- `python3 -c '...'` |
| 81 | +- `python.exe -c "..."` |
| 82 | +- `python -u -c "..."` (with flags) |
| 83 | + |
| 84 | +It specifically looks for the `-c` flag combined with a Python executable. Normal Python file execution (e.g., `python script.py` or `python -m pytest`) is **allowed**. |
| 85 | + |
| 86 | +## Recognized Shell Tools |
| 87 | + |
| 88 | +The handler monitors the following shell execution tools (from `ShellExecutionTools` constant): |
| 89 | + |
| 90 | +- `bash` |
| 91 | +- `Execute` |
| 92 | +- `ShellTool` |
| 93 | +- `exec_command` |
| 94 | +- `execute_command` |
| 95 | +- `run_shell_command` |
| 96 | +- `run_terminal_command` |
| 97 | +- `shell` |
| 98 | +- `local_shell` |
| 99 | +- `container.exec` |
| 100 | + |
| 101 | +## Rationale |
| 102 | + |
| 103 | +Why block inline Python? |
| 104 | + |
| 105 | +1. **Quoting Hell**: Passing complex Python code inside shell strings often leads to escaping issues, especially on Windows (cmd.exe vs PowerShell) and with nested quotes. |
| 106 | +2. **Terminal Stability**: Long or complex inline one-liners can behave unpredictably depending on the underlying shell. |
| 107 | +3. **Debuggability**: Code in a file is easier to review, debug, and log than a ephemeral one-liner. |
| 108 | +4. **Agent Behavior**: Encouraging agents to write scripts fosters better coding habits and more robust solutions. |
| 109 | + |
| 110 | +## Behavior Flow |
| 111 | + |
| 112 | +1. **Agent Action**: Agent calls a shell tool with `python -c "print('hello')"` |
| 113 | +2. **Interception**: The `InlinePythonSteeringHandler` detects the pattern. |
| 114 | +3. **Blocking**: The tool call is swallow (not executed). |
| 115 | +4. **Response**: The agent receives the steering message (default or custom). |
| 116 | +5. **Correction**: The agent should then create a file and execute it, which is allowed. |
| 117 | + |
| 118 | +## Troubleshooting |
| 119 | + |
| 120 | +**Feature not working:** |
| 121 | +- Verify `INLINE_PYTHON_STEERING_ENABLED` is set to `true`. |
| 122 | +- Ensure the tool being used is one of the recognized shell tools. |
| 123 | + |
| 124 | +**False Positives:** |
| 125 | +- The regex is designed to be specific to `-c`. If you find valid commands being blocked, please report them. |
| 126 | +- Normal `python filename.py` should never be blocked. |
| 127 | + |
| 128 | +## Implementation References |
| 129 | + |
| 130 | +- **Handler**: `src/core/services/tool_call_handlers/inline_python_steering_handler.py` |
| 131 | +- **Tests**: `tests/unit/core/services/tool_call_handlers/test_inline_python_steering_handler.py` |
| 132 | +- **Configuration**: `src/core/config/app_config.py` |
| 133 | +- **DI Registration**: `src/core/di/services.py` |
0 commit comments