-
Notifications
You must be signed in to change notification settings - Fork 141
Add runtime extra instruction files to pvmap generator prompt #1972
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
rohitkumarbhagat
wants to merge
1
commit into
datacommonsorg:master
Choose a base branch
from
rohitkumarbhagat:a12n-extra-instruction-files
base: master
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
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 |
|---|---|---|
|
|
@@ -22,7 +22,7 @@ | |
| import subprocess | ||
| import sys | ||
| from datetime import datetime | ||
| from dataclasses import dataclass | ||
| from dataclasses import dataclass, field | ||
| from pathlib import Path | ||
| from typing import List, Optional | ||
|
|
||
|
|
@@ -44,6 +44,11 @@ def _define_flags(): | |
| flags.DEFINE_list('input_metadata', [], | ||
| 'List of input metadata file paths (optional)') | ||
|
|
||
| flags.DEFINE_list( | ||
| 'extra_instruction_files', [], | ||
| 'List of extra instruction file paths to make available to Gemini ' | ||
| '(optional)') | ||
|
|
||
| flags.DEFINE_boolean( | ||
| 'sdmx_dataset', False, | ||
| 'Whether the dataset is in SDMX format (default: False)') | ||
|
|
@@ -86,6 +91,11 @@ def _define_flags(): | |
| flags.DEFINE_string( | ||
| 'working_dir', None, | ||
| 'Working directory for the generator (default: current directory)') | ||
|
|
||
| flags.DEFINE_integer( | ||
| 'extra_instruction_max_bytes', 65536, | ||
| 'Maximum allowed size in bytes for each extra instruction file ' | ||
| '(default: 65536)') | ||
| except flags.DuplicateFlagError: | ||
| pass | ||
|
|
||
|
|
@@ -110,6 +120,8 @@ class Config: | |
| output_path: str = 'output/output' | ||
| gemini_cli: Optional[str] = None | ||
| working_dir: Optional[str] = None | ||
| extra_instruction_files: List[str] = field(default_factory=list) | ||
| extra_instruction_max_bytes: int = 65536 | ||
|
|
||
|
|
||
| @dataclass | ||
|
|
@@ -137,6 +149,8 @@ def __init__(self, config: Config): | |
| # Copy config to avoid modifying the original | ||
| self._config = copy.deepcopy(config) | ||
|
|
||
| self._validate_extra_instruction_max_bytes() | ||
|
|
||
| # Convert input_data paths to absolute | ||
| if self._config.data_config.input_data: | ||
| self._config.data_config.input_data = [ | ||
|
|
@@ -151,6 +165,13 @@ def __init__(self, config: Config): | |
| for path in self._config.data_config.input_metadata | ||
| ] | ||
|
|
||
| # Resolve and validate runtime extra instruction files. | ||
| if self._config.extra_instruction_files: | ||
| self._config.extra_instruction_files = [ | ||
| self._validate_extra_instruction_file(path) | ||
| for path in self._config.extra_instruction_files | ||
| ] | ||
|
|
||
| # Parse output_path into absolute path, handling relative paths and ~ expansion | ||
| output_path_raw = self._config.output_path | ||
| if not output_path_raw or not output_path_raw.strip(): | ||
|
|
@@ -186,10 +207,7 @@ def __init__(self, config: Config): | |
|
|
||
| def _validate_and_convert_path(self, path: str) -> Path: | ||
| """Convert path to absolute and validate it's within working directory.""" | ||
| p = Path(path).expanduser() | ||
| if not p.is_absolute(): | ||
| p = self._working_dir / p | ||
| real_path = p.resolve() | ||
| real_path = self._resolve_path(path) | ||
| working_dir = self._working_dir.resolve() | ||
| try: | ||
| real_path.relative_to(working_dir) | ||
|
|
@@ -198,6 +216,43 @@ def _validate_and_convert_path(self, path: str) -> Path: | |
| f"Path '{path}' is outside working directory '{working_dir}'") | ||
| return real_path | ||
|
|
||
| def _resolve_path(self, path: str) -> Path: | ||
| """Resolve a path against the working directory when needed.""" | ||
| p = Path(path).expanduser() | ||
| if not p.is_absolute(): | ||
| p = self._working_dir / p | ||
| return p.resolve() | ||
|
|
||
| def _validate_extra_instruction_max_bytes(self) -> None: | ||
| """Validate the configured size limit for extra instruction files.""" | ||
| if self._config.extra_instruction_max_bytes < 0: | ||
| raise ValueError("extra_instruction_max_bytes must be non-negative") | ||
|
|
||
| def _validate_extra_instruction_file(self, path: str) -> Path: | ||
| """Resolve and validate an extra instruction file.""" | ||
| resolved_path = self._resolve_path(path) | ||
| if not resolved_path.exists(): | ||
| raise ValueError( | ||
| f"Extra instruction file does not exist: {resolved_path}") | ||
| if not resolved_path.is_file(): | ||
| raise ValueError( | ||
| f"Extra instruction path is not a file: {resolved_path}") | ||
|
|
||
| file_size = resolved_path.stat().st_size | ||
| max_bytes = self._config.extra_instruction_max_bytes | ||
| if file_size > max_bytes: | ||
| raise ValueError( | ||
|
Contributor
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. can we truncate file with a warning rather than terminate program or in future summarize the file to fit in size threshold? |
||
| f"Extra instruction file is larger than {max_bytes} bytes: " | ||
| f"{resolved_path}") | ||
|
|
||
| try: | ||
| resolved_path.read_text(encoding='utf-8') | ||
| except UnicodeDecodeError: | ||
| raise ValueError(f"Extra instruction file is not valid UTF-8 text: " | ||
| f"{resolved_path}") | ||
|
|
||
| return resolved_path | ||
|
|
||
| def _initialize_datacommons_dir(self) -> Path: | ||
| """Initialize and return the .datacommons directory path.""" | ||
| dc_dir = self._working_dir / '.datacommons' | ||
|
|
@@ -419,7 +474,10 @@ def _generate_prompt(self) -> Path: | |
| 'output_basename': | ||
| self._output_basename, # Base name for pvmap/metadata files | ||
| 'run_dir_abs': | ||
| str(self._run_dir) | ||
| str(self._run_dir), | ||
| 'extra_instruction_files_abs': [ | ||
| str(path) for path in self._config.extra_instruction_files | ||
| ] if self._config.extra_instruction_files else [], | ||
| } | ||
|
|
||
| # Render template with these variables | ||
|
|
@@ -440,16 +498,19 @@ def prepare_config() -> Config: | |
| input_metadata=_FLAGS.input_metadata or [], | ||
| is_sdmx_dataset=_FLAGS.sdmx_dataset) | ||
|
|
||
| return Config(data_config=data_config, | ||
| dry_run=_FLAGS.dry_run, | ||
| maps_api_key=_FLAGS.maps_api_key, | ||
| dc_api_key=_FLAGS.dc_api_key, | ||
| max_iterations=_FLAGS.max_iterations, | ||
| skip_confirmation=_FLAGS.skip_confirmation, | ||
| enable_sandboxing=_FLAGS.enable_sandboxing, | ||
| output_path=_FLAGS.output_path, | ||
| gemini_cli=_FLAGS.gemini_cli, | ||
| working_dir=_FLAGS.working_dir) | ||
| return Config( | ||
| data_config=data_config, | ||
| dry_run=_FLAGS.dry_run, | ||
| maps_api_key=_FLAGS.maps_api_key, | ||
| dc_api_key=_FLAGS.dc_api_key, | ||
| max_iterations=_FLAGS.max_iterations, | ||
| skip_confirmation=_FLAGS.skip_confirmation, | ||
| enable_sandboxing=_FLAGS.enable_sandboxing, | ||
| output_path=_FLAGS.output_path, | ||
| gemini_cli=_FLAGS.gemini_cli, | ||
| working_dir=_FLAGS.working_dir, | ||
| extra_instruction_files=_FLAGS.extra_instruction_files or [], | ||
| extra_instruction_max_bytes=_FLAGS.extra_instruction_max_bytes) | ||
|
|
||
|
|
||
| def main(_): | ||
|
|
||
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.
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.
The
_validate_extra_instruction_filemethod uses_resolve_pathwhich allows files outside the working directory. However, ifenable_sandboxingis set toTrue(which is the default on macOS), the Gemini agent running in the sandbox will likely be restricted to the working directory and will fail to read these extra instruction files at runtime.To ensure the agent can actually access these files, consider enforcing the same working directory boundary as
input_dataandinput_metadataby using_validate_and_convert_pathinstead of_resolve_path.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.
pls check if Gemini suggestion for sandbox mode should be supported