-
Notifications
You must be signed in to change notification settings - Fork 26
feat: Improve isolation of agent evaluation CUJs by staging the work_dir to a new tmp directory tree #462
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
g-lynnzee
wants to merge
8
commits into
GoogleCloudPlatform:main
Choose a base branch
from
g-lynnzee:main
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
feat: Improve isolation of agent evaluation CUJs by staging the work_dir to a new tmp directory tree #462
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
228cdf6
Feat: Add ability to filter to specific json scenario/examples
g-lynnzee dc1a38d
Update filtering logig
g-lynnzee d2b0b23
feat: Improve isolation of CUJs by copying and using only the working…
g-lynnzee d585315
Revert "Update filtering logig"
g-lynnzee 5aaabff
Revert "Feat: Add ability to filter to specific json scenario/examples"
g-lynnzee bef1a67
Merge branch 'main' into main
g-lynnzee 5484575
Remove newlines
g-lynnzee 57edee5
style guide fixes
g-lynnzee 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,10 @@ | |
| import logging | ||
| import os | ||
| import shutil | ||
| import tempfile | ||
| import threading | ||
|
|
||
|
|
||
| from dataset.evalgeminicliinput import EvalGeminiCliRequest | ||
| from generators.models import get_generator | ||
| from generators.models.agent_cli import AgentCliGenerator | ||
|
|
@@ -119,12 +121,12 @@ def process_scenario( | |
| last_result = None | ||
|
|
||
| resolved_work_dir = scenario.get("resolved_work_dir") | ||
| if resolved_work_dir: | ||
| os.makedirs(resolved_work_dir, exist_ok=True) | ||
| execution_cwd, temp_sandbox_dir = self._setup_sandbox(resolved_work_dir) | ||
|
|
||
| # Copy declared env_files to fake_home | ||
| fake_home = getattr(self.generator, "fake_home", None) | ||
| if fake_home: | ||
| self._register_trusted_folders(fake_home, execution_cwd, resolved_work_dir) | ||
| session_dir = os.path.dirname(fake_home) | ||
| env_files = scenario.get("env_files", []) | ||
| for env_file in env_files: | ||
|
|
@@ -152,7 +154,7 @@ def process_scenario( | |
| env=env, | ||
| resume=(turn > 0), | ||
| session_id=session_id, | ||
| cwd=resolved_work_dir, | ||
| cwd=execution_cwd, | ||
| ) | ||
| try: | ||
| result = self.generator.safe_generate(cli_cmd) | ||
|
|
@@ -205,6 +207,8 @@ def process_scenario( | |
| else: | ||
| break | ||
|
|
||
| self._cleanup_sandbox(resolved_work_dir, temp_sandbox_dir) | ||
|
|
||
| if last_result: | ||
| self._finalize_scenario( | ||
| scenario, | ||
|
|
@@ -267,3 +271,41 @@ def _finalize_scenario( | |
| score_work.run() | ||
|
|
||
| eval_result.agent_results.append(eval_output_data) | ||
|
|
||
| def _setup_sandbox(self, resolved_work_dir: str | None) -> tuple[str | None, str | None]: | ||
| """Creates a temporary isolated directory and copies resolved_work_dir into it.""" | ||
| if not resolved_work_dir: | ||
| return None, None | ||
| os.makedirs(resolved_work_dir, exist_ok=True) | ||
| temp_sandbox_dir = tempfile.mkdtemp(prefix="evalbench-sandbox-") | ||
| shutil.copytree(resolved_work_dir, temp_sandbox_dir, dirs_exist_ok=True) | ||
| return temp_sandbox_dir, temp_sandbox_dir | ||
|
|
||
| def _register_trusted_folders(self, fake_home: str, execution_cwd: str | None, resolved_work_dir: str | None) -> None: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to only work for gemini, we should support others and restrict this one to gemini. |
||
| """Writes trusted folder configuration mappings to settings json to prevent CLI trust hangs.""" | ||
| if not execution_cwd: | ||
| return | ||
| trusted_folders_path = os.path.join(fake_home, ".gemini", "trustedFolders.json") | ||
| os.makedirs(os.path.dirname(trusted_folders_path), exist_ok=True) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. worth looking at mktemp ? |
||
|
|
||
| trusted_folders = {} | ||
| if os.path.exists(trusted_folders_path): | ||
| try: | ||
| with open(trusted_folders_path, "r") as f: | ||
| trusted_folders = json.load(f) | ||
| except json.JSONDecodeError: | ||
| pass | ||
|
|
||
| trusted_folders[execution_cwd] = "TRUST_FOLDER" | ||
| if resolved_work_dir: | ||
| trusted_folders[resolved_work_dir] = "TRUST_FOLDER" | ||
|
|
||
| with open(trusted_folders_path, "w") as f: | ||
| json.dump(trusted_folders, f, indent=2) | ||
| logging.info("Registered sandbox folder trust in fake_home settings.") | ||
|
|
||
| def _cleanup_sandbox(self, resolved_work_dir: str | None, temp_sandbox_dir: str | None) -> None: | ||
| """Copies sandboxed modifications back to the original directory and deletes the temp folder.""" | ||
| if resolved_work_dir and temp_sandbox_dir: | ||
| shutil.copytree(temp_sandbox_dir, resolved_work_dir, dirs_exist_ok=True) | ||
| shutil.rmtree(temp_sandbox_dir) | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cleanup call isn't reached on exception. The turn-loop body has its own
try/exceptforsafe_generate/generate, but any exception that bypasses those (e.g. an uncaught error inextract_tools/extract_skills,simulated_user.get_next_response,KeyboardInterrupt, or a bug somewhere else in the loop body) skips this line and leaves the tempdir on disk. On a long-running eval server (or a flaky scenario that fails repeatedly), this leaksevalbench-sandbox-*directories under/tmpindefinitely.Wrap the post-setup block in
try/finally:Worth a unit test alongside the change: patch
self.generator.safe_generateto raise, callprocess_scenario, then assert noevalbench-sandbox-*directories remain undertempfile.gettempdir(). That locks the contract in.