Skip to content

FEAT: SandboxRegistry, so targets and scorers share a sandbox by name (part 4) - #4103

Draft
Richard Lundeen (richlundeen) wants to merge 16 commits into
microsoft:mainfrom
richlundeen:richlundeen-sandbox-stack-3-registry
Draft

Richard Lundeen (richlundeen) wants to merge 16 commits into
microsoft:mainfrom
richlundeen:richlundeen-sandbox-stack-3-registry

Conversation

@richlundeen

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

Copy link
Copy Markdown
Contributor

Parked. #4102 was rescoped to sandboxes that keep nothing between calls (see the review there). This PR depends on persistent sandbox state, so it waits for the first real persistent adapter and will be rebuilt on top of it. Its current contents are kept on richlundeen-sandbox-stateful-wip.

Stacked on #4102 — review only the commits after c85ff9a.

Description

Part 4 of 5 of sandboxed code execution. A sandbox that keeps state should be represented by one Sandbox object, so that everything using it shares one session and sees the same state: a target's tools, the scorer that checks what they did, and operators. This part adds a registry that names sandboxes, and closes them when the configuration is re-applied.

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
  4. FEAT: SandboxRegistry, so targets and scorers share a sandbox by name (part 4) #4103 SandboxRegistry (this PR, 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)
  • SandboxRegistry (pyrit._code_execution): register each sandbox once, under a name, and refer to it by that name. It rejects a name that's taken and a sandbox registered twice, and close_all_async closes every registered sandbox.
  • resolve_sandbox accepts a Sandbox or a registered name. The tools and the check scorer later in the stack take either.
  • Re-applying the configuration closes the registered sandboxes before the registries reset, so no session is left open, and reset_setup_registries resets this registry too.
  • Docs: the Code Execution section of doc/code/framework.md.

🤖 Generated with Claude Code

… tool's infrastructure fails

- max_tool_rounds (default 10) bounds the previously unbounded tool loop. When
  it is reached, pending calls are answered as not run and the turn ends with
  an error response carrying a ToolLoopStop reason.
- A tool raises ToolInfrastructureError(invoked=...) when its infrastructure
  fails; the turn ends once every call in the round has a result, and the
  model is told only that the tool's infrastructure failed.
- ToolLoopStop and ToolLoopStopReason are exported from pyrit.models.
- Malformed-argument results include the validation message.

Part 1 of 5 of sandboxed code execution.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e adapter

- Sandbox (experimental, pyrit._code_execution): an adapter implements a few
  hooks, and the base class enforces the rules every sandbox follows: one
  shared session that callers take turns using, within one deadline; retries
  only when nothing ran; renewal before a session limit; idle close; work
  that finishes when its caller gives up, and work that never starts once it
  has; quarantine when a close, reset or cleanup can't be confirmed; and
  evidence (warnings, partial runs, execution ids). It implements
  SandboxClient, so ProgramExecutionScorer works with any adapter.
- DockerSandboxClient is the example adapter, a call sandbox. Invalid input,
  including arguments too long for a command line, raises SandboxInputError
  before anything runs.
- FakeSandbox for tests, and a contract-test kit, check_sandbox_contract_async,
  that adapter authors run against a live sandbox.
- ProgramExecutionSource reports a rejected request as unavailable and keeps
  the sandbox's warnings and execution ids.

Part 2 of 5 of sandboxed code execution.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- SandboxRegistry names sandboxes, so every caller shares one Sandbox object,
  and with it one session, per sandbox. It rejects a name that is taken and a
  sandbox registered twice; close_all_async closes every registered sandbox.
- resolve_sandbox accepts a Sandbox or a registered name.
- Re-applying the configuration closes the registered sandboxes before the
  registries reset, so no session is left open; reset_setup_registries also
  resets this registry.

Part 3 of 5 of sandboxed code execution.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

Registry replacement can discard cleanup records and quarantine after an unconfirmed sandbox close, allowing a fresh object to register while the old session remains open.

1 open issue
What changed in this PR

Adds named sandbox sharing to PyRIT’s experimental code-execution layer, allowing targets, scorers, and operators to use the same sandbox session. This is the registry stage of the sandboxed-execution stack.

Changes:

  • Adds SandboxRegistry and resolve_sandbox, with duplicate-name and duplicate-identity checks.
  • Integrates sandbox closure and registry reset into live configuration reapplication.
  • Adds registry tests and architecture documentation; the supplied diff also includes prerequisite sandbox and bounded-tool-loop changes.
File Description
tests/​unit/​setup/​test_reinitialization.py Tests sandbox closure during configuration reapplication.
tests/​unit/​prompt_target/​target/​test_tool.py Updates malformed-argument assertions.
tests/​unit/​prompt_target/​target/​test_openai_response_target_function_chaining.py Tests tool-loop limits and infrastructure failures.
tests/​unit/​common/​test_lazy_package_imports.py Checks sandbox exports and import independence.
tests/​unit/​_code_execution/​test_sandbox.py Tests sandbox lifecycle and execution guarantees.
tests/​unit/​_code_execution/​test_sandbox_registry.py Tests registration, lookup, closure, and reset.
tests/​unit/​_code_execution/​test_sandbox_contract.py Tests adapter contract checks.
tests/​unit/​_code_execution/​test_docker_client.py Updates Docker adapter and input-limit tests.
tests/​unit/​_code_execution/​local_python_sandbox.py Provides a local subprocess adapter for tests.
tests/​integration/​score/​test_program_execution_scorer_integration.py Updates cancellation-test timeout configuration.
pyrit/​setup/​initialization.py Includes sandboxes in setup registry resets.
pyrit/​setup/​configuration_loader.py Closes registered sandboxes before live replacement.
pyrit/​score/​observation/​program_execution_source.py Handles rejected input and retains execution identifiers.
pyrit/​prompt_target/​openai/​openai_response_target.py Bounds tool execution and handles infrastructure failures.
pyrit/​prompt_target/​common/​tool_provider.py Defines tool infrastructure errors.
pyrit/​prompt_target/​__init__.py Exports the tool infrastructure error.
pyrit/​models/​target/​tool_execution_metadata.py Defines tool-loop stop metadata.
pyrit/​models/​code_execution.py Adds sandbox execution identifiers to results.
pyrit/​models/​__init__.py Exports tool-loop stop models.
pyrit/​_code_execution/​sandbox.py Implements shared sandbox execution and lifecycle rules.
pyrit/​_code_execution/​sandbox_registry.py Adds named registration and sandbox resolution.
pyrit/​_code_execution/​sandbox_contract.py Provides adapter contract checks.
pyrit/​_code_execution/​fake_sandbox.py Provides an in-memory 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 and registry APIs lazily.
doc/​code/​framework.md Documents sandbox responsibilities and named sharing.

Review effort: Balanced

Comment thread pyrit/setup/configuration_loader.py Resolved
@richlundeen Richard Lundeen (richlundeen) changed the title FEAT: SandboxRegistry, so targets and scorers share a sandbox by name FEAT: SandboxRegistry, so targets and scorers share a sandbox by name (part 3) Nov 9, 2026
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…cement

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

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 9, 2026
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Integrate only 6053ef3, preserving prior Sandbox and tool-loop repairs and deterministic Docker timeout phase coverage.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Preserve session-owned state, separate routine release from operator recovery, bound each invocation acquisition, and keep program scoring fresh.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Keep automatic idle eviction disabled without suppressing an explicitly requested final close. Isolate exact session-budget selection from host interpreter latency while retaining runtime timeout checks.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…egistry layer

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@richlundeen Richard Lundeen (richlundeen) changed the title FEAT: SandboxRegistry, so targets and scorers share a sandbox by name (part 3) FEAT: SandboxRegistry, so targets and scorers share a sandbox by name (part 4) 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