Skip to content

FEAT: SandboxToolProvider, so a model's tool calls run in a sandbox (part 3) - #4104

Draft
Richard Lundeen (richlundeen) wants to merge 2 commits into
microsoft:mainfrom
richlundeen:richlundeen-sandbox-stack-4-tool-provider
Draft

Richard Lundeen (richlundeen) wants to merge 2 commits into
microsoft:mainfrom
richlundeen:richlundeen-sandbox-stack-4-tool-provider

Conversation

@richlundeen

@richlundeen Richard Lundeen (richlundeen) commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #4102. Review only the commit after 793511d.

Description

Part 3 of 5 of sandboxed code execution. This part gives a model tools that run its code in a sandbox. They work with any target whose tool loop is bounded, starting with OpenAIResponseTarget.

The stack, merged in order:

  1. FEAT: Bound OpenAIResponseTarget's tool loop, and end the turn when a tool's infrastructure fails (part 1) #4101 Bounded tool loop for OpenAIResponseTarget (merged)
  2. FEAT: Declared limits, per-call timeouts and pre-run validation for SandboxClient (part 2) #4102 Declared limits, per-call timeouts and pre-run validation for SandboxClient
  3. FEAT: SandboxToolProvider, so a model's tool calls run in a sandbox (part 3) #4104 SandboxToolProvider, with run_<runtime> tools (this PR)
  4. FEAT: SandboxRegistry, so targets and scorers share a sandbox by name (part 4) #4103 SandboxRegistry (parked until a persistent adapter)
  5. FEAT: SandboxCheckScorer, which scores a turn by what a check program finds in the sandbox (part 5) #4105 SandboxCheckScorer (parked until a persistent adapter)

Rescoped with #4102: this part offers run_<runtime> tools on any SandboxClient. File tools (write_file, read_file, list_dir) and lookup by registered name need a sandbox that keeps state, so they move to the change that adds the first persistent adapter.

  • SandboxToolProvider(sandbox=...) (pyrit.prompt_target) turns a sandbox client into one run_<runtime> tool per runtime (run_python, …). RuntimeProfile gains tool_name and description, and SandboxCapabilities gains a description, so tools can name and describe runtimes to the model.
  • Results are JSON: the exit code, each stream's output (cut at max_output_chars, and marked when cut), whether the program timed out or hit a resource limit, and how long it took. A program that fails is an ordinary result the model can read and fix.
  • Arguments are checked against the sandbox's declared capabilities before anything runs, so a call rejected there goes back to the model with the reason and is never recorded as invoked.
  • ToolInputError: a sandbox's own input rejection reaches the model with the same no-run guarantee, and OpenAIResponseTarget lets the model correct the call without ending the turn.
  • When the sandbox itself fails, the tool raises ToolInfrastructureError, so the target ends the turn instead of asking the model to fix it.
  • Docs: a new page, doc/code/targets/12_sandboxed_tool_calls.md, and the Targets section of doc/code/framework.md.

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Adapter-raised SandboxInputError is returned as a normal result, causing rejected sandbox and file tool calls to be recorded as invoked and potentially scored as executed.

1 open issue
What changed in this PR

Adds SandboxToolProvider, connecting PyRIT’s sandbox execution layer to model tool calls. This fourth part of the stack builds on the sandbox interface, registry, and bounded tool loop.

Changes:

  • Exposes runtime execution tools and persistent file operations with validated arguments and bounded results.
  • Integrates sandbox failures with tool-loop termination and invocation metadata.
  • Adds usage documentation and coverage for tools, adapters, lifecycle behavior, and target integration.
File Description
tests/​unit/​setup/​test_reinitialization.py Checks sandbox closure during reinitialization.
tests/​unit/​prompt_target/​target/​test_tool.py Checks validation error details.
tests/​unit/​prompt_target/​target/​test_sandbox_tool_provider.py Covers sandbox tools and target integration.
tests/​unit/​prompt_target/​target/​test_openai_response_target_function_chaining.py Covers loop bounds and infrastructure failures.
tests/​unit/​common/​test_lazy_package_imports.py Extends sandbox import-boundary checks.
tests/​unit/​_code_execution/​test_sandbox.py Covers sandbox lifecycle and execution rules.
tests/​unit/​_code_execution/​test_sandbox_registry.py Covers registration, resolution, and closure.
tests/​unit/​_code_execution/​test_sandbox_contract.py Validates adapter contract checks.
tests/​unit/​_code_execution/​test_docker_client.py Covers Docker capabilities and model tool calls.
tests/​unit/​_code_execution/​local_python_sandbox.py Provides a local subprocess test adapter.
tests/​integration/​score/​test_program_execution_scorer_integration.py Updates cancellation-test timeout configuration.
pyrit/​setup/​initialization.py Resets the sandbox registry.
pyrit/​setup/​configuration_loader.py Closes sandboxes before reinitialization.
pyrit/​score/​observation/​program_execution_source.py Handles rejected input and preserves execution identifiers.
pyrit/​prompt_target/​openai/​openai_response_target.py Bounds tool rounds and handles infrastructure failures.
pyrit/​prompt_target/​common/​tool_provider.py Defines infrastructure errors with invocation status.
pyrit/​prompt_target/​common/​sandbox_tool_provider.py Implements sandbox runtime and file tools.
pyrit/​prompt_target/​__init__.py Exports the provider and infrastructure error.
pyrit/​models/​target/​tool_execution_metadata.py Adds tool-loop stop metadata.
pyrit/​models/​code_execution.py Adds sandbox execution identifiers.
pyrit/​models/​__init__.py Exports tool-loop metadata types.
pyrit/​_code_execution/​sandbox.py Defines shared execution and lifecycle behavior.
pyrit/​_code_execution/​sandbox_registry.py Registers and resolves shared sandboxes.
pyrit/​_code_execution/​sandbox_contract.py Provides adapter contract checks.
pyrit/​_code_execution/​fake_sandbox.py Provides a configurable test sandbox.
pyrit/​_code_execution/​docker_client.py Adapts Docker execution to the sandbox interface.
pyrit/​_code_execution/​client.py Defines sandbox errors and updates the client contract.
pyrit/​_code_execution/​__init__.py Exports sandbox APIs lazily.
doc/​myst.yml Adds the sandboxed tool-calls page to navigation.
doc/​code/​targets/​12_sandboxed_tool_calls.md Documents tools, configuration, and adapter development.
doc/​code/​framework.md Clarifies target and sandbox responsibilities.

Review effort: Balanced

@richlundeen Richard Lundeen (richlundeen) changed the title FEAT: SandboxToolProvider, so a model's tool calls run in a sandbox FEAT: SandboxToolProvider, so a model's tool calls run in a sandbox (part 4) Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review completed. No blocking concerns remain. This review records source coverage; it does not authorize merge.

Richard Lundeen (richlundeen) added a commit to richlundeen/PyRIT that referenced this pull request Nov 10, 2026
Integrate microsoft#4104 at f10c809 while retaining the final-layer tool-to-check-scorer integration test.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Richard Lundeen (richlundeen) added a commit to richlundeen/PyRIT that referenced this pull request Nov 10, 2026
Integrate microsoft#4104 at58d45c3ab7de35f55b7e3b11772ed69ddbc18531, including selected main6053ef39563941c7e06d26a94de3f9ea41bff86b. Preserve the lower SESSION/fresh-source contracts and upper check-scorer docs/test; its reproduced stateful acquisition compatibility repair follows separately.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…on for SandboxClient

SandboxClient gains get_capabilities(), which declares a client's runtimes and
per-call limits (input and argument size, output size, default and maximum
timeout), and execute_async gains an optional timeout_seconds. A request that
does not fit raises SandboxInputError (a ValueError) before anything runs;
SandboxCapabilities.validate_request performs the check so adapters share it.

DockerSandboxClient implements the extended contract. Its configured timeout is
both the default and the limit, and it accepts at most 16 KiB of arguments per
run, since they go on a command line. ProgramExecutionSource records a request
the sandbox rejects as unavailable evidence, so the verdict is undetermined.

Each invocation still runs in a fresh environment; stateful sandboxes are left
for the change that adds the first persistent adapter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SandboxToolProvider turns any SandboxClient into run_<runtime> tools, such as
run_python. Arguments are checked against the sandbox's declared capabilities
before anything runs, so a rejected call goes back to the model and is never
recorded as invoked. A sandbox's own input rejection raises the new
ToolInputError with the same no-run guarantee, and OpenAIResponseTarget returns
its reason to the model without ending the turn. A sandbox failure raises
ToolInfrastructureError, so the target ends the turn instead of asking the model
to fix infrastructure.

RuntimeProfile gains tool_name and description, and SandboxCapabilities gains a
description, so the tools can name and describe runtimes to the model.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@richlundeen
Richard Lundeen (richlundeen) force-pushed the richlundeen-sandbox-stack-4-tool-provider branch from 58d45c3 to 6704b83 Compare November 10, 2026 22:50
@richlundeen Richard Lundeen (richlundeen) changed the title FEAT: SandboxToolProvider, so a model's tool calls run in a sandbox (part 4) FEAT: SandboxToolProvider, so a model's tool calls run in a sandbox (part 3) Nov 10, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants