From 5b7f1869bda176cae3cc3ba83700276722e9857f Mon Sep 17 00:00:00 2001 From: jjallaire Date: Wed, 11 Sep 2024 15:58:07 -0400 Subject: [PATCH] `user` parameter for `bash()` and `python()` tools (#369) --- CHANGELOG.md | 1 + src/inspect_ai/tool/_tools/_execute.py | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddaf6e0d9..300249d48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - [score()](https://inspect.ai-safety-institute.org.uk/solvers.html#sec-scoring-in-solvers) function for accessing scoring logic from within solvers. - `system_message()` now supports custom parameters and interpolation of `metadata` values from `Sample`. - `generate()` solver now accepts arbitrary generation config params. +- `bash()` and `python()` tools now have a `user` parameter for choosing an alternate user to run code as. - Input event for recording screen input in sample transcripts. - Record to sample function for CSV and JSON dataset readers can now return multiple samples. - Added `debug_errors` option to `eval()` to raise task errors (rather than logging them) so they can be debugged. diff --git a/src/inspect_ai/tool/_tools/_execute.py b/src/inspect_ai/tool/_tools/_execute.py index 0af6eff3c..fb5d8950e 100644 --- a/src/inspect_ai/tool/_tools/_execute.py +++ b/src/inspect_ai/tool/_tools/_execute.py @@ -4,13 +4,14 @@ @tool -def bash(timeout: int | None = None) -> Tool: +def bash(timeout: int | None = None, user: str | None = None) -> Tool: """Bash shell command execution tool. Execute bash shell commands using a sandbox environment (e.g. "docker"). Args: timeout (int | None): Timeout (in seconds) for command. + user (str | None): User to execute commands as. Returns: String with command output (stdout) or command error (stderr). @@ -26,7 +27,9 @@ async def execute(cmd: str) -> str: Returns: The output of the command. """ - result = await sandbox().exec(cmd=["bash", "-c", cmd], timeout=timeout) + result = await sandbox().exec( + cmd=["bash", "-c", cmd], timeout=timeout, user=user + ) if result.success: return result.stdout else: @@ -38,13 +41,14 @@ async def execute(cmd: str) -> str: @tool -def python(timeout: int | None = None) -> Tool: +def python(timeout: int | None = None, user: str | None = None) -> Tool: """Python code execution tool. Execute Python code using a sandbox environment (e.g. "docker"). Args: timeout (int | None): Timeout (in seconds) for command. + user (str | None): User to execute commands as. Returns: String with command output (stdout) or command error (stderr). @@ -60,7 +64,9 @@ async def execute(code: str) -> str: Returns: The output of the command. """ - result = await sandbox().exec(cmd=["python3"], input=code, timeout=timeout) + result = await sandbox().exec( + cmd=["python3"], input=code, timeout=timeout, user=user + ) if result.success: return result.stdout else: