-
Notifications
You must be signed in to change notification settings - Fork 388
Fix coding_env API compatibility and safety reward false positives #635
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
abhinavgautam01
wants to merge
8
commits into
huggingface:main
Choose a base branch
from
abhinavgautam01:fix/coding-env-api-signature
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.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
27f2f50
Fix coding_env API signature compatibility
abhinavgautam01 fe57c1c
Fix coding_env safety reward false positives with AST detection
abhinavgautam01 fc49a72
Address Greptile findings for coding_env safety and step signature
abhinavgautam01 4e9c397
Address coding env API review issues
abhinavgautam01 3e91ae1
Tighten coding env API signatures
abhinavgautam01 2d0bc8f
Clarify coding env reset compatibility
abhinavgautam01 37abfb1
Harden coding safety AST parsing
abhinavgautam01 493a496
Harden coding env quality transform
abhinavgautam01 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
Some comments aren't visible on the classic Files Changed page.
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
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,119 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| """Tests for coding_env safety transform false-positive handling.""" | ||
|
|
||
| from coding_env.models import CodeObservation | ||
| from coding_env.server.transforms import CodeQualityTransform, CodeSafetyTransform | ||
|
|
||
|
|
||
| def _apply_safety_transform(code: str) -> CodeObservation: | ||
| transform = CodeSafetyTransform() | ||
| observation = CodeObservation( | ||
| stdout="", | ||
| stderr="", | ||
| exit_code=0, | ||
| metadata={"last_code": code}, | ||
| ) | ||
| transformed = transform(observation) | ||
| assert isinstance(transformed, CodeObservation) | ||
| return transformed | ||
|
|
||
|
|
||
| def test_blocks_real_dangerous_import(): | ||
| observation = _apply_safety_transform("import os\nprint('x')") | ||
| assert observation.reward == -1.0 | ||
| assert "safety_violation" in observation.metadata | ||
|
|
||
|
|
||
| def test_blocks_subprocess_import(): | ||
| observation = _apply_safety_transform("import subprocess") | ||
| assert observation.reward == -1.0 | ||
| assert observation.metadata["safety_violation"] == "import subprocess" | ||
|
|
||
|
|
||
| def test_blocks_from_subprocess_import(): | ||
| observation = _apply_safety_transform("from subprocess import run") | ||
| assert observation.reward == -1.0 | ||
| assert observation.metadata["safety_violation"] == "import subprocess" | ||
|
|
||
|
|
||
| def test_blocks_from_os_path_import(): | ||
| observation = _apply_safety_transform("from os.path import join") | ||
| assert observation.reward == -1.0 | ||
| assert observation.metadata["safety_violation"] == "import os" | ||
|
|
||
|
|
||
| def test_blocks_builtin_open_call(): | ||
| observation = _apply_safety_transform( | ||
| "with open('f.txt') as f:\n data = f.read()" | ||
| ) | ||
| assert observation.reward == -1.0 | ||
| assert "safety_violation" in observation.metadata | ||
|
|
||
|
|
||
| def test_blocks_builtin_eval_call(): | ||
| observation = _apply_safety_transform("result = eval('1 + 1')") | ||
| assert observation.reward == -1.0 | ||
| assert observation.metadata["safety_violation"] == "eval" | ||
|
|
||
|
|
||
| def test_blocks_builtin_exec_call(): | ||
| observation = _apply_safety_transform("exec('x = 1')") | ||
| assert observation.reward == -1.0 | ||
| assert observation.metadata["safety_violation"] == "exec" | ||
|
|
||
|
|
||
| def test_blocks_builtin_import_call(): | ||
| observation = _apply_safety_transform("__import__('os')") | ||
| assert observation.reward == -1.0 | ||
| assert observation.metadata["safety_violation"] == "__import__" | ||
|
|
||
|
|
||
| def test_does_not_flag_string_literal_with_dangerous_text(): | ||
| observation = _apply_safety_transform("print('import os')") | ||
| assert observation.reward == 0.0 | ||
| assert "safety_violation" not in observation.metadata | ||
|
|
||
|
|
||
| def test_does_not_flag_user_defined_myopen_function(): | ||
| observation = _apply_safety_transform( | ||
| "def myopen():\n return 1\nresult = myopen()" | ||
| ) | ||
| assert observation.reward == 0.0 | ||
| assert "safety_violation" not in observation.metadata | ||
|
|
||
|
|
||
| def test_does_not_flag_attribute_method_named_exec(): | ||
| observation = _apply_safety_transform( | ||
| "class DB:\n" | ||
| " def exec(self, sql):\n" | ||
| " return sql\n" | ||
| "db = DB()\n" | ||
| "result = db.exec('SELECT 1')" | ||
| ) | ||
| assert observation.reward == 0.0 | ||
| assert "safety_violation" not in observation.metadata | ||
|
|
||
|
|
||
| def test_quality_transform_handles_ast_recursion_error(monkeypatch): | ||
| def raise_recursion_error(_code: str): | ||
| raise RecursionError("pathologically nested code") | ||
|
|
||
| monkeypatch.setattr("coding_env.server.transforms.ast.parse", raise_recursion_error) | ||
|
|
||
| transform = CodeQualityTransform(concise_bonus=0.0, syntax_penalty=-0.2) | ||
| observation = CodeObservation( | ||
| stdout="", | ||
| stderr="", | ||
| exit_code=0, | ||
| metadata={"last_code": "x = 1"}, | ||
| ) | ||
|
|
||
| transformed = transform(observation) | ||
|
|
||
| assert isinstance(transformed, CodeObservation) | ||
| assert transformed.reward == -0.2 |
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
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.