Repository navigation
FEAT: Declared limits, per-call timeouts and pre-run validation for SandboxClient (part 2) - #4102
Conversation
There was a problem hiding this comment.
🟡 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
Richard Lundeen (richlundeen)
left a comment
There was a problem hiding this comment.
Automated review completed. No blocking concerns remain. This review records source coverage; it does not authorize merge.
Behnam (behnam-o)
left a comment
There was a problem hiding this comment.
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:
- Here: extend
SandboxClientwith what #4104 needs now (declared limits, per-calltimeout_seconds, pre-run validation viaSandboxInputError) and adapt Docker. - Add session handling together with the first real persistent adapter, so the state model is designed against a real backend (see the
SandboxStateKindcomment). A wrapper may fit better than a base class Docker has to inherit. - Move the tool-facing fields to #4104, and keep
FakeSandboxand 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 (behnam-o)
left a comment
There was a problem hiding this comment.
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.
…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>
771bdc0 to
793511d
Compare
|
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):
#4104 is rebased onto this and offers #4103 and #4105 are parked. The registry exists to share one session and one lock, and On fresh environments: with no stateful sandboxes left, The inline threads on the session code no longer apply to this PR. I'll carry them over to the persistent-adapter PR. |


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 extendsSandboxClientwith what that needs and adapts Docker. It adds no new base class and no stateful sandboxes.The stack, merged in order:
OpenAIResponseTarget(merged)SandboxClient(this PR)SandboxToolProvider, withrun_<runtime>toolsSandboxRegistry(parked until a persistent adapter)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 therichlundeen-sandbox-stateful-wipbranch.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_requestchecks a request against them and raisesSandboxInputError(aValueError) before anything runs, so adapters share one check.SandboxClientgainsget_capabilities(), andexecute_asyncgains an optionaltimeout_seconds. Each invocation still runs in a fresh environment.DockerSandboxClientimplements the extended contract. Its configured timeout is both the default and the limit.ProgramExecutionSourcerecords a request the sandbox rejects as unavailable evidence, so the verdict is undetermined rather than an error.doc/code/framework.md.Behavior change:
DockerSandboxClientaccepts at most 16 KiB of arguments per run, since they go on a command line. AProgramExecutionScorercase with longer arguments now scores undetermined instead of running; pass long data through standard input.Related: #3218 (program execution scorer).
🤖 Generated with Claude Code