Skip to content

FEAT: SandboxCheckScorer, which scores a turn by what a check program finds in the sandbox (part 5) - #4105

Draft
Richard Lundeen (richlundeen) wants to merge 31 commits into
microsoft:mainfrom
richlundeen:richlundeen-sandbox-stack-5-check-scorer
Draft

Richard Lundeen (richlundeen) wants to merge 31 commits into
microsoft:mainfrom
richlundeen:richlundeen-sandbox-stack-5-check-scorer

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 #4104 — review only the commits after 98004dd.

Description

Part 5 of 5 of sandboxed code execution, which lets PyRIT test models that act by running code. A model's claim that it succeeded isn't evidence, so this part adds a scorer that checks the effect itself: after a turn, it runs a fixed check program in the sandbox the model's code ran in.

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 (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 (this PR, parked until a persistent adapter)

How the whole stack fits together, for one turn and its check:

flowchart TD
    model(["Model under test"])
    target["OpenAIResponseTarget<br/>tool loop, now bounded"]
    registry["SandboxRegistry<br/>one Sandbox per name"]
    tools["SandboxToolProvider<br/>run_python, write_file, …"]
    scorer["SandboxCheckScorer<br/>runs a fixed check program"]
    sandbox["Sandbox base class<br/>shared session, turns, retries, quarantine"]
    docker["DockerSandboxClient<br/>example adapter"]
    yours["Your adapter<br/>a few hooks"]
    infra[("Container, VM<br/>or hosted service")]

    model -->|1 tool call| target
    target -->|2 runs the tool| tools
    tools -->|3 validated program| sandbox
    sandbox --> docker
    sandbox --> yours
    docker --> infra
    yours --> infra
    target -.->|4 JSON result| model
    scorer -->|5 check, after the turn| sandbox
    registry -.- tools
    registry -.- scorer

    classDef new fill:#ddf4ff,stroke:#0969da,color:#1f2328
    classDef changed fill:#fff8c5,stroke:#9a6700,color:#1f2328
    classDef outside fill:#f6f8fa,stroke:#8c959f,color:#1f2328
    class tools,sandbox,scorer,registry new
    class target,docker changed
    class model,yours,infra outside
Loading

Blue is new in this stack, yellow is changed, and grey is outside PyRIT. Part 1 bounds the target's tool loop; part 2 adds the Sandbox base class and moves Docker onto it; part 3 adds the tools; part 4 adds the registry; and this part adds the check scorer. The tools and the check scorer refer to the same sandbox by name, so the check sees what the model's code left behind.

  • SandboxCheckScorer(sandbox=..., check_program=..., condition=...) (pyrit.score) scores the judged response true when every case of the check produces its expected result, and false when one doesn't. When the sandbox fails, or the check times out or hits a resource limit, the score is undetermined, so an infrastructure problem is never counted as the model failing.
  • The response's own text never runs. The check reads the sandbox as it is when it runs, so stored verdicts are never reused, and callers own isolation between attempts: one attempt at a time per sandbox, no automatic retries, and a reset between attempts. The docs say how.
  • The check's program and output are stored as an observation linked to the score, and a stored run can be judged again without running it.
  • It needs a sandbox whose state persists (an environment sandbox).
  • Docs: the "Check what the code did" section of doc/code/targets/12_sandboxed_tool_calls.md, the Scorers section of doc/code/framework.md, and a note in the program execution scorer notebook.

Demonstration, with scripted model replies and a local test sandbox: the model calls run_python with a program that writes flag.txt, and the check program is print('present' if os.path.exists('flag.txt') else 'absent'), expecting present.

Scored Score Rationale
Before the turn false Check program: The program did not produce the expected result under python: case 0 output did not match.
After the turn true Check program: All 1 case(s) produced the expected result under python.

Related: #3218 (program execution scorer).

🤖 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>
- SandboxToolProvider turns a sandbox into tools: a run_<runtime> tool per
  runtime (run_python, ...), plus write_file, read_file and list_dir where
  files persist. Results are JSON with the exit code and bounded output.
- Arguments are checked against the sandbox's limits before anything runs, so
  a rejected call is never recorded as invoked. A sandbox failure raises
  ToolInfrastructureError, which ends the turn and records whether anything
  may have run.
- Docs: doc/code/targets/12_sandboxed_tool_calls.md.

Part 4 of 5 of sandboxed code execution.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… finds in the sandbox

- SandboxCheckScorer runs a fixed check program, after a turn, in the sandbox
  the model's code ran in, and scores the judged response true when every case
  of the check produces its expected result and false when one doesn't. A
  sandbox failure, or a check that times out or hits a resource limit, leaves
  the score undetermined.
- The response's own text never runs, stored verdicts are never reused (the
  sandbox can change), and the check's program and output are stored as an
  observation linked to the score; a stored run can be judged again.
- Docs: the check section of the sandboxed tool calls page, and a note in the
  program execution scorer notebook.

Part 5 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

Wrapped SandboxCheckScorer instances can reuse stale verdicts after sandbox changes or resets because wrapper matching bypasses the override; the matching hook needs an override and regression coverage.

1 open issue
What changed in this PR

Adds SandboxCheckScorer, completing the sandboxed execution stack by scoring observable effects rather than a model’s claims. It runs a fixed check in the shared sandbox and preserves execution evidence for later replay.

Changes:

  • Adds fixed-program checking, undetermined outcomes for infrastructure failures, and stored-observation replay.
  • Includes supporting sandbox adapters, registry lifecycle management, tool providers, and bounded target tool loops from the stack.
  • Adds scorer and sandbox coverage, usage guidance, and architecture documentation.
File Description
tests/​unit/​setup/​test_reinitialization.py Checks sandbox cleanup during reinitialization.
tests/​unit/​score/​test_sandbox_check_scorer.py Covers check verdicts, evidence, replay, and validation.
tests/​unit/​prompt_target/​target/​test_tool.py Checks malformed-argument error details.
tests/​unit/​prompt_target/​target/​test_sandbox_tool_provider.py Covers sandbox tools and model-to-check integration.
tests/​unit/​prompt_target/​target/​test_openai_response_target_function_chaining.py Covers bounded loops and infrastructure failures.
tests/​unit/​common/​test_lazy_package_imports.py Extends sandbox import-boundary coverage.
tests/​unit/​_code_execution/​test_sandbox.py Covers sandbox lifecycle and concurrency contracts.
tests/​unit/​_code_execution/​test_sandbox_registry.py Tests registration, lookup, and cleanup.
tests/​unit/​_code_execution/​test_sandbox_contract.py Checks adapter-contract enforcement.
tests/​unit/​_code_execution/​test_docker_client.py Covers Docker adaptation, limits, and tool execution.
tests/​unit/​_code_execution/​local_python_sandbox.py Supplies a local test execution adapter.
tests/​integration/​score/​test_program_execution_scorer_integration.py Updates cancellation-test timeout configuration.
pyrit/​setup/​initialization.py Resets the sandbox registry with setup registries.
pyrit/​setup/​configuration_loader.py Closes sandboxes before live reinitialization.
pyrit/​score/​true_false/​sandbox_check_scorer.py Implements fixed-check scoring and replay.
pyrit/​score/​observation/​program_execution_source.py Preserves rejection details and execution identifiers.
pyrit/​score/​__init__.py Exports SandboxCheckScorer lazily.
pyrit/​prompt_target/​openai/​openai_response_target.py Bounds tool rounds and handles infrastructure failures.
pyrit/​prompt_target/​common/​tool_provider.py Defines tool infrastructure errors.
pyrit/​prompt_target/​common/​sandbox_tool_provider.py Exposes sandbox execution and file tools.
pyrit/​prompt_target/​__init__.py Exports sandbox tools and infrastructure errors.
pyrit/​models/​target/​tool_execution_metadata.py Defines tool-loop stop metadata.
pyrit/​models/​code_execution.py Adds sandbox execution identifiers to run evidence.
pyrit/​models/​__init__.py Exports tool-loop metadata types.
pyrit/​_code_execution/​sandbox_registry.py Shares sandbox instances by registered name.
pyrit/​_code_execution/​sandbox_contract.py Provides adapter contract checks.
pyrit/​_code_execution/​fake_sandbox.py Provides a controllable 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 stateful-client semantics.
pyrit/​_code_execution/​__init__.py Exports sandbox APIs lazily.
doc/​myst.yml Adds the sandboxed-tool guide to navigation.
doc/​code/​targets/​12_sandboxed_tool_calls.md Explains tools, checks, isolation, and adapters.
doc/​code/​scoring/​7_program_execution_scorer.py Adds a documentation note distinguishing sandbox checks.
doc/​code/​scoring/​7_program_execution_scorer.ipynb Adds the matching notebook explanation.
doc/​code/​framework.md Documents sandbox component responsibilities.

Review effort: Balanced

@richlundeen Richard Lundeen (richlundeen) changed the title FEAT: SandboxCheckScorer, which scores a turn by what a check program finds in the sandbox FEAT: SandboxCheckScorer, which scores a turn by what a check program finds in the sandbox (part 5) 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>
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>
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>
Integrate main at 15afd46. Preserve the existing microsoft#4115 deterministic Docker classification tests together with the inherited Sandbox adaptation.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Reject stored wrapper results through the participating child snapshot hook. Cover persisted and legacy candidates after mutation/reset, both fallback branches, nesting, fresh rescoring and the unused-fallback control while preserving explicit observation replay.

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.

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>
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>
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>
Preserve ProgramExecutionSource fresh-only validation and identity while sharing condition-independent evidence capture. Checks explicitly share SESSION or ENVIRONMENT state with tools without ending sessions or recovering quarantine. Cover named sharing, loss/uncertainty, bounded persisted evidence, replay and fresh-source exclusions.

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

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