Skip to content

FEAT: Declared limits, per-call timeouts and pre-run validation for SandboxClient (part 2) - #4102

Draft
Richard Lundeen (richlundeen) wants to merge 1 commit into
microsoft:mainfrom
richlundeen:richlundeen-sandbox-stack-2-sandbox
Draft

Richard Lundeen (richlundeen) wants to merge 1 commit into
microsoft:mainfrom
richlundeen:richlundeen-sandbox-stack-2-sandbox

Conversation

@richlundeen

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

Copy link
Copy Markdown
Contributor

Description

Part 2 of 5 of sandboxed code execution. SandboxToolProvider (#4104) needs to know a sandbox's limits before it runs anything, so it can return a correctable error to the model instead of an infrastructure failure. This part extends SandboxClient with what that needs and adapts Docker. It adds no new base class and no stateful sandboxes.

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 (this PR)
  3. FEAT: SandboxToolProvider, so a model's tool calls run in a sandbox (part 3) #4104 SandboxToolProvider, with run_<runtime> tools
  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 after review: sessions, renewal, idle close, quarantine and reset only served stateful backends that the stack didn't include. They will come back together with the first real persistent adapter, so the state model is designed against a real backend. #4103 (SandboxRegistry) and #4105 (SandboxCheckScorer) depend on persistent state and are parked until then. The previous implementation is kept on the richlundeen-sandbox-stateful-wip branch.

  • SandboxCapabilities (experimental, pyrit._code_execution) declares a client's runtimes (RuntimeProfile) and per-call limits: input and argument size, output size, and default and maximum timeout. validate_request checks a request against them and raises SandboxInputError (a ValueError) before anything runs, so adapters share one check.
  • SandboxClient gains get_capabilities(), and execute_async gains an optional timeout_seconds. Each invocation still runs in a fresh environment.
  • DockerSandboxClient implements the extended contract. Its configured timeout is both the default and the limit.
  • ProgramExecutionSource records a request the sandbox rejects as unavailable evidence, so the verdict is undetermined rather than an error.
  • Docs: the Code Execution section of doc/code/framework.md.

Behavior change: DockerSandboxClient accepts at most 16 KiB of arguments per run, since they go on a command line. A ProgramExecutionScorer case with longer arguments now scores undetermined instead of running; pass long data through standard input.

Related: #3218 (program execution scorer).

🤖 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

The image-only Docker cache can skip resource-support checks after a backend switch, allowing untrusted code to run without required resource limits.

4 open issues
What changed in this PR

Adds the experimental sandbox abstraction for PyRIT’s code-execution stack, building on #4101 and preparing for shared sandbox registration, tools, and scoring.

Changes:

  • Introduces shared session management, validation, retries, cancellation handling, and quarantine.
  • Migrates Docker to the sandbox interface, including a 16 KiB argument limit, and preserves execution evidence for scoring.
  • Adds fake and local test adapters, an adapter contract checker, and framework documentation.
File Description
tests/​unit/​prompt_target/​target/​test_tool.py Checks malformed-argument error details from the prerequisite changes.
tests/​unit/​prompt_target/​target/​test_openai_response_target_function_chaining.py Covers prerequisite tool-loop limits and infrastructure failures.
tests/​unit/​common/​test_lazy_package_imports.py Checks sandbox exports and package independence.
tests/​unit/​_code_execution/​test_sandbox.py Covers sandbox lifecycle, concurrency, validation, and evidence.
tests/​unit/​_code_execution/​test_sandbox_contract.py Checks contract validation against conforming and broken adapters.
tests/​unit/​_code_execution/​test_docker_client.py Covers Docker capabilities, limits, and error mapping.
tests/​unit/​_code_execution/​local_python_sandbox.py Adds a test-only local Python adapter.
tests/​integration/​score/​test_program_execution_scorer_integration.py Adapts cancellation coverage to capability-based timeouts.
pyrit/​score/​observation/​program_execution_source.py Handles rejected requests and retains sandbox execution identifiers.
pyrit/​prompt_target/​openai/​openai_response_target.py Includes prerequisite bounded tool-loop behavior.
pyrit/​prompt_target/​common/​tool_provider.py Defines the prerequisite tool infrastructure error.
pyrit/​prompt_target/​__init__.py Exports the tool infrastructure error.
pyrit/​models/​target/​tool_execution_metadata.py Adds prerequisite tool-loop stop metadata.
pyrit/​models/​code_execution.py Adds sandbox execution identifiers to run evidence.
pyrit/​models/​__init__.py Exports tool-loop stop models.
pyrit/​_code_execution/​sandbox.py Implements the sandbox contract and shared lifecycle rules.
pyrit/​_code_execution/​sandbox_contract.py Adds live adapter conformance checks.
pyrit/​_code_execution/​fake_sandbox.py Adds an in-memory adapter with fault injection.
pyrit/​_code_execution/​docker_client.py Migrates Docker to a call-scoped sandbox adapter.
pyrit/​_code_execution/​client.py Defines sandbox errors and documents persistent state.
pyrit/​_code_execution/​__init__.py Lazily exports the sandbox API and testing helpers.
doc/​code/​framework.md Documents sandbox responsibilities and tool-loop boundaries.

Review effort: Balanced

Comment thread pyrit/_code_execution/docker_client.py Resolved
Comment thread pyrit/_code_execution/sandbox.py Resolved
Comment thread pyrit/_code_execution/sandbox.py Resolved
@richlundeen Richard Lundeen (richlundeen) changed the title FEAT: Sandbox interface for code execution, with Docker as the example adapter FEAT: Sandbox interface for code execution, with Docker as the example adapter (part 2) Nov 9, 2026
@behnam-o Behnam (behnam-o) self-assigned this 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.

Comment thread pyrit/_code_execution/sandbox.py Resolved
Comment thread pyrit/_code_execution/sandbox.py Resolved
Comment thread pyrit/_code_execution/sandbox.py Resolved
Comment thread pyrit/_code_execution/client.py Resolved

@behnam-o Behnam (behnam-o) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Adding the summary that didn't make it into my previous review:

Stateful sandboxes for agentic evaluations are worth having, and the failure classification and cancellation handling here are careful and well tested. Thanks for removing the Docker validation cache in 7553ec4. My main concern is scope.

Most of Sandbox serves a kind of backend this PR doesn't include. Sessions, renewal, idle close, quarantine and reset/revisions only apply to environment sandboxes. Neither this PR nor the rest of the stack (#4103–#4105) includes one: Docker is still a call sandbox, while SandboxCheckScorer and the file tools require environment. So that code is exercised only by FakeSandbox and a test-only adapter. Some fields (tool_name, description, file_runtime_profile_name) exist only for later PRs, and no included adapter sets execution_id.

Suggested split:

  1. Here: extend SandboxClient with what #4104 needs now (declared limits, per-call timeout_seconds, pre-run validation via SandboxInputError) and adapt Docker.
  2. Add session handling together with the first real persistent adapter, so the state model is designed against a real backend (see the SandboxStateKind comment). A wrapper may fit better than a base class Docker has to inherit.
  3. Move the tool-facing fields to #4104, and keep FakeSandbox and the contract kit with the tests until outside adapters need them.

If the session code stays in this PR, my inline comments on the previous review cover what I'd fix first.

@behnam-o Behnam (behnam-o) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for addressing my previous comments. I've left two findings on the new session-state handling inline. My scope concern still stands: most of Sandbox serves stateful backends this PR doesn't include. I've restated it as an inline thread so it can be tracked and resolved.

Comment thread pyrit/_code_execution/sandbox.py Resolved
Comment thread pyrit/_code_execution/sandbox.py Resolved
Comment thread pyrit/_code_execution/sandbox.py Resolved
…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>
@richlundeen
Richard Lundeen (richlundeen) force-pushed the richlundeen-sandbox-stack-2-sandbox branch from 771bdc0 to 793511d Compare November 10, 2026 22:50
@richlundeen Richard Lundeen (richlundeen) changed the title FEAT: Sandbox interface for code execution, with Docker as the example adapter (part 2) FEAT: Declared limits, per-call timeouts and pre-run validation for SandboxClient (part 2) Nov 10, 2026
@richlundeen

Copy link
Copy Markdown
Contributor Author

Thanks Behnam (@behnam-o), agreed on scope. I've split it the way you suggested and force-pushed.

#4102 now (about 330 lines, down from about 4,150):

  • SandboxClient gains get_capabilities(), which declares runtimes and per-call limits, plus an optional per-call timeout_seconds on execute_async.
  • A request that doesn't fit raises SandboxInputError before anything runs. The check is SandboxCapabilities.validate_request, so adapters share it.
  • Docker implements this directly. There's no base class to inherit.
  • ProgramExecutionSource records a rejected request as unavailable evidence.
  • FakeSandbox, the contract kit, ProcessResult and execution_ids are gone. FEAT: SandboxToolProvider, so a model's tool calls run in a sandbox (part 3) #4104's tests use a small call-only fake that lives in the test file.

#4104 is rebased onto this and offers run_<runtime> tools only. The tool-facing fields (tool_name, runtime and sandbox description) moved there. The file tools and registry lookup are out, because they need persistent state.

#4103 and #4105 are parked. The registry exists to share one session and one lock, and SandboxCheckScorer inspects persisted state, so both wait for the first real persistent adapter. Session handling will be redesigned alongside it, probably as a wrapper rather than a base class, as you suggested. That PR will also need to answer your SandboxStateKind points: session-scoped state, a way to report state loss, and separating state-loss quarantine from cleanup quarantine. The old implementation is on richlundeen-sandbox-stateful-wip for reference.

On fresh environments: with no stateful sandboxes left, SandboxClient is back to "each invocation runs in a fresh environment", and the framework.md wording matches. When a stateful adapter lands, the observation will record the state it ran against.

The inline threads on the session code no longer apply to this PR. I'll carry them over to the persistent-adapter PR.

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.

3 participants