Repository navigation
fix(swe): mask verifications without a verdict and block agent egress in more SWE servers #6670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yfw
wants to merge
13
commits into
main
Choose a base branch
from
yifuw/swe-egress-block-no-verdict-masks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,070
−126
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
61c482d
fix(swe_gym): grade unfinished test runs as inconclusive and capture …
hemildesai 66b8a93
feat(swe): block agent sandbox egress in r2e_gym, swe_gym and swemer_…
HeyyyyyyG 9b78bc4
fix(r2e_gym,swe_gym): mask verifications that produce no verdict
HeyyyyyyG 52de6e7
fix(scale_swe): mask a missing verdict on the Agent /run cookie path
HeyyyyyyG 1560b3c
docs(scale_swe): say which cookie path stops are bounded
yfw 6c7e4ec
fix(swe_gym): report a timed-out test run as a completed 0
yfw 41a428c
fix(swe): read the reserved exit codes only before the test output st…
yfw 2a24f8a
fix(swe_gym,r2e_gym): name the verifier's error in a no-verdict mask
yfw a7ecce4
fix(scale_swe): take the captured sandbox out of a cookie session
yfw b184ac5
fix(swebench): treat a null provider_options as no options
yfw df91095
test(swe_gym,r2e_gym): pin the stream capture, the empty patch and a …
yfw 8645c67
fix(scale_swe): drop a cookie session's stop task once it ends
yfw d26642d
Merge main into yifuw/swe-egress-block-no-verdict-masks
yfw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
| from fastapi import HTTPException | ||
| from pydantic import ValidationError | ||
|
|
||
| import resources_servers.r2e_gym.app as app | ||
| import resources_servers.swebench.agent_network as agent_network | ||
| from nemo_gym.client import ServerClient | ||
| from nemo_gym.config import ModelServerRef | ||
|
|
||
|
|
||
| # The sandbox created for a session is the agent's; the evaluation sandbox is created for a verifier session. | ||
| AGENT_SANDBOX = {"session_id": "session"} | ||
| EVAL_SANDBOX = {"verifier_session_id": "session"} | ||
| INSTANCE = dict(instance_id="o__r-1", repo_name="o/r", image_name="img") | ||
| MODEL_SERVER = ModelServerRef(type="responses_api_models", name="policy_model") | ||
|
|
||
|
|
||
| class _Started(Exception): | ||
| """Raised by the recording sandbox once it has the spec, so a seed stops before its setup commands.""" | ||
|
|
||
|
|
||
| class _RecordingAsyncSandbox: | ||
| def __init__(self, provider_config) -> None: | ||
| self.spec = None | ||
|
|
||
| async def start(self, spec) -> None: | ||
| self.spec = spec | ||
| if _RecordingAsyncSandbox.raise_on_start: | ||
| raise _Started(spec) | ||
|
|
||
| async def exec(self, *args, **kwargs) -> None: | ||
| return None | ||
|
|
||
| async def stop(self) -> None: | ||
| return None | ||
|
|
||
|
|
||
| _RecordingAsyncSandbox.raise_on_start = False | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def make_server(monkeypatch): | ||
| def make(*, enforce=True, provider="opensandbox", model_url="http://10.1.2.3:8000", sandbox_config=None): | ||
| monkeypatch.setattr(app, "AsyncSandbox", _RecordingAsyncSandbox) | ||
| monkeypatch.setattr(app, "get_global_config_dict", lambda: {}) | ||
| monkeypatch.setattr(app, "resolve_provider_config", lambda *_: {provider: {}}) | ||
| monkeypatch.setattr(app, "resolve_provider_metadata", lambda *_: {}) | ||
| monkeypatch.setattr(agent_network, "resolve_provider_config", lambda *_: {provider: {}}) | ||
| monkeypatch.setattr(agent_network, "get_server_url", lambda name: model_url) | ||
| config = app.R2EGymResourcesServerConfig( | ||
| host="", | ||
| port=0, | ||
| name="r2e_gym", | ||
| entrypoint="", | ||
| sandbox_provider="sandbox", | ||
| sandbox_config=sandbox_config or {}, | ||
| enforce_agent_no_network=enforce, | ||
| sandbox_model_server=MODEL_SERVER if enforce else None, | ||
| ) | ||
| return app.R2EGymResourcesServer(config=config, server_client=MagicMock(spec=ServerClient)) | ||
|
|
||
| return make | ||
|
|
||
|
|
||
| async def _create(server, sandbox_kwargs): | ||
| return await server._create_sandbox(app.R2EGymInstanceRequest(**INSTANCE), **sandbox_kwargs) | ||
|
|
||
|
|
||
| def test_disabled_network_check_does_not_load_global_cli_config(make_server, monkeypatch) -> None: | ||
| server = make_server(enforce=False) | ||
|
|
||
| def unexpected_global_config(): | ||
| raise AssertionError("Disabled network enforcement must not parse process arguments") | ||
|
|
||
| monkeypatch.setattr(app, "get_global_config_dict", unexpected_global_config) | ||
| type(server)(config=server.config, server_client=server.server_client) | ||
|
|
||
|
|
||
| async def test_off_by_default(make_server) -> None: | ||
| sandbox = await _create(make_server(enforce=False), AGENT_SANDBOX) | ||
| assert "network_policy" not in sandbox.spec.provider_options | ||
|
|
||
|
|
||
| async def test_agent_sandbox_allows_only_the_model_host(make_server) -> None: | ||
| sandbox = await _create(make_server(), AGENT_SANDBOX) | ||
| assert sandbox.spec.provider_options["network_policy"] == { | ||
| "defaultAction": "deny", | ||
| "egress": [{"action": "allow", "target": "10.1.2.3"}], | ||
| } | ||
|
|
||
|
|
||
| async def test_seeding_blocks_the_agent_sandbox_egress(make_server, monkeypatch) -> None: | ||
| server = make_server() | ||
| monkeypatch.setattr(_RecordingAsyncSandbox, "raise_on_start", True) | ||
| with pytest.raises(_Started) as started: | ||
| await server._start_task_sandbox("session", app.R2EGymInstanceRequest(**INSTANCE)) | ||
| assert started.value.args[0].provider_options["network_policy"]["defaultAction"] == "deny" | ||
|
|
||
|
|
||
| async def test_eval_sandbox_keeps_its_network(make_server) -> None: | ||
| sandbox = await _create(make_server(), EVAL_SANDBOX) | ||
| assert "network_policy" not in sandbox.spec.provider_options | ||
|
|
||
|
|
||
| async def test_other_provider_options_are_kept(make_server) -> None: | ||
| sandbox_config = {"provider_options": {"platform": "linux/amd64"}} | ||
| sandbox = await _create(make_server(sandbox_config=sandbox_config), AGENT_SANDBOX) | ||
| assert sandbox.spec.provider_options["platform"] == "linux/amd64" | ||
| assert sandbox.spec.provider_options["network_policy"]["defaultAction"] == "deny" | ||
| assert "network_policy" not in sandbox_config["provider_options"], "the shared config must not be mutated" | ||
|
|
||
|
|
||
| async def test_unreachable_model_host_refuses_the_seed(make_server) -> None: | ||
| server = make_server(model_url="http://127.0.0.1:8000") | ||
| with pytest.raises(HTTPException) as refused: | ||
| await _create(server, AGENT_SANDBOX) | ||
| assert refused.value.status_code == 422 | ||
|
|
||
|
|
||
| def test_startup_refuses_a_provider_without_network_policies(make_server) -> None: | ||
| with pytest.raises(ValueError, match="OpenSandbox"): | ||
| make_server(provider="docker") | ||
|
|
||
|
|
||
| def test_config_refuses_the_flag_without_a_model_server_or_with_a_set_policy() -> None: | ||
| base = dict(host="", port=0, name="r2e_gym", entrypoint="", sandbox_provider="sandbox") | ||
| with pytest.raises(ValidationError, match="needs sandbox_model_server"): | ||
| app.R2EGymResourcesServerConfig(**base, sandbox_config={}, enforce_agent_no_network=True) | ||
| with pytest.raises(ValidationError, match="not both"): | ||
| app.R2EGymResourcesServerConfig( | ||
| **base, | ||
| sandbox_config={"provider_options": {"network_policy": {"defaultAction": "deny"}}}, | ||
| enforce_agent_no_network=True, | ||
| sandbox_model_server=MODEL_SERVER, | ||
| ) | ||
|
|
||
|
|
||
| def test_config_fields_default_to_open_egress() -> None: | ||
| fields = app.R2EGymResourcesServerConfig.model_fields | ||
| assert fields["enforce_agent_no_network"].default is False | ||
| assert fields["sandbox_model_server"].default is None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.