-
Notifications
You must be signed in to change notification settings - Fork 4
feat: package restructure with backward compatibility and error handling #2
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
allenday
wants to merge
6
commits into
aertoria:main
Choose a base branch
from
allenday:feat/package-restructure
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.
Conversation
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
- Restructure project into agentproxy package - Add OpenTelemetry support with traces and metrics - Add --claude-bin CLI option for custom Claude binary path - Fix .env loading path to project root - Add security fixes for command injection in BrowserVerifier - Add OTEL stack examples with Grafana dashboards - Add pyproject.toml for proper packaging Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
- Add build artifacts (*.egg-info, build/, dist/) to gitignore - Add vim undo files (*.un~) to gitignore - Add sandbox/ directory to gitignore - Include claude-code-otel.sh wrapper for testing upstream telemetry - Include implementation plan document Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
This PR implements the package restructuring (PR1) with critical error handling improvements added for production resilience. ## Package Structure & Backward Compatibility ### New Package Structure - Moved all modules to `agentproxy/` package directory - Created `pyproject.toml` with entry points for `pa` and `pa-server` - Added `agentproxy/__init__.py` with proper exports - Added `agentproxy/__main__.py` for module execution ### Backward Compatibility (CRITICAL) - **cli.py** - Shim for old `python cli.py` entry point - **server.py** - Shim for old `python server.py` entry point - All old entry points continue to work without modification - New entry points available after `pip install -e .`: - `pa` command - `pa-server` command - `python -m agentproxy` - `python -m agentproxy.server` ### Tests (29 passing) - **tests/test_baseline_compatibility.py** - 23 tests covering: - Old entry points (python cli.py, python server.py) - New entry points (pa, pa-server, python -m agentproxy) - Package imports and structure - Core components - **tests/unit/test_gemini_error_handling.py** - 4 unit tests - **tests/integration/test_integration_error_handling.py** - 2 integration tests ## Gemini Error Handling & Resilience (Critical Addition) ### Problem Solved Previous implementation had no retry logic or error recovery, causing: - Lost instruction context during transient API errors - Abrupt failures on rate limits or network issues - No graceful degradation ### Solution Implemented #### 1. Retry Logic with Exponential Backoff (gemini_client.py) - **GeminiAPIError** exception with retry metadata - Automatic retry on 5xx errors (max 3 attempts) - Exponential backoff: 1s, 2s, 4s - No retry on 4xx client errors (won't succeed) - Structured error messages for upstream handling #### 2. Error Recovery & Context Preservation (pa_agent.py) - Track consecutive errors (resets on success) - **First 2 errors**: Return NO_OP to preserve instruction context - **3rd consecutive error**: Trigger SAVE_SESSION for graceful exit - Parse error strings to extract type, status code, message - Increment error counter on both API and parse failures #### 3. Instruction Preservation (pa.py) - Track `_original_task` and `_last_valid_instruction` - NO_OP returns empty string (signals "no change") - Main loop keeps last valid instruction during errors - Prevents sending vague "Continue" during error states #### 4. Session Save Function (function_executor.py) - New SAVE_SESSION function for graceful exit - Saves session state with error context - Returns session ID for resumption - Metadata includes error type, status code, reason ### Error Flow ``` Gemini API Error ↓ Retry 3 times with exponential backoff ↓ If all retries fail → Return [GEMINI_ERROR:type:code:message] ↓ PA Agent detects error string ↓ Error #1-2: Return NO_OP (preserve instruction) ↓ Error aertoria#3: Trigger SAVE_SESSION (graceful exit) ``` ### Testing - Unit tests verify error detection, counter increment, session save - Integration tests verify instruction preservation across error cycles - All 29 tests passing ## Files Changed ### New Files - `cli.py` - Backward compatibility shim - `server.py` - Backward compatibility shim - `tests/test_baseline_compatibility.py` - Package structure tests - `tests/unit/test_gemini_error_handling.py` - Error handling unit tests - `tests/integration/test_integration_error_handling.py` - Integration tests - `tests/__init__.py`, `tests/unit/__init__.py`, `tests/integration/__init__.py` ### Modified Files - `agentproxy/gemini_client.py` - Retry logic, GeminiAPIError exception - `agentproxy/pa_agent.py` - Error detection, consecutive error tracking - `agentproxy/pa.py` - Instruction preservation during errors - `agentproxy/function_executor.py` - SAVE_SESSION function - `.gitignore` - Updated patterns ### Deleted Files - `test_state_detection.py` - Obsolete test file ## Verification All entry points tested and working: - ✅ `python cli.py --help` - ✅ `python server.py --help` - ✅ `pa --help` - ✅ `pa-server --help` - ✅ `python -m agentproxy --help` - ✅ `python -m agentproxy.server --help` - ✅ Package imports work correctly - ✅ All 29 tests passing ## Breaking Changes None - full backward compatibility maintained. Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
- Add CLAUDE_BIN env var support for custom Claude binary paths - Add automatic .env file loading in CLI and server - Update .env.example with CLAUDE_BIN documentation These changes enable using local Claude builds with stream-json support and simplify configuration management. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Fixed multiple critical bugs preventing PA from terminating correctly:
1. Path duplication in verification (./sandbox/./sandbox/file.py)
- Use os.path.relpath() since cwd is already set
2. Gemini retry logic not respecting retryable flag
- Check both is_client_error and retryable flag
3. Missing DONE state in ControllerState enum
- Added DONE state for proper termination
- PA checks for DONE state and breaks iteration loop
4. PA hallucinating requirements from task breakdown
- Clarified breakdown is GUIDE not REQUIREMENTS
- PA now validates against ORIGINAL TASK only
- Updated prompts to prevent adding hallucinated requirements
5. Verification results not in PA reasoning context
- PA now sees verification results when making decisions
- Includes [VERIFICATION RESULT] in context passed to run_iteration
6. AttributeError accessing event.source
- Use event.metadata.get('source') instead
These fixes enable PA to properly terminate after task completion
instead of continuing to iterate unnecessarily.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
f17f63e to
ea5502e
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR implements the package restructuring (PR1) with critical error
handling improvements added for production resilience.
Package Structure & Backward Compatibility
New Package Structure
agentproxy/package directorypyproject.tomlwith entry points forpaandpa-serveragentproxy/__init__.pywith proper exportsagentproxy/__main__.pyfor module executionBackward Compatibility (CRITICAL)
python cli.pyentry pointpython server.pyentry pointpip install -e .:pacommandpa-servercommandpython -m agentproxypython -m agentproxy.serverTests (29 passing)
Gemini Error Handling & Resilience (Critical Addition)
Problem Solved
Previous implementation had no retry logic or error recovery, causing:
Solution Implemented
1. Retry Logic with Exponential Backoff (gemini_client.py)
2. Error Recovery & Context Preservation (pa_agent.py)
3. Instruction Preservation (pa.py)
_original_taskand_last_valid_instruction4. Session Save Function (function_executor.py)
Error Flow
Testing
Files Changed
New Files
cli.py- Backward compatibility shimserver.py- Backward compatibility shimtests/test_baseline_compatibility.py- Package structure teststests/unit/test_gemini_error_handling.py- Error handling unit teststests/integration/test_integration_error_handling.py- Integration teststests/__init__.py,tests/unit/__init__.py,tests/integration/__init__.pyModified Files
agentproxy/gemini_client.py- Retry logic, GeminiAPIError exceptionagentproxy/pa_agent.py- Error detection, consecutive error trackingagentproxy/pa.py- Instruction preservation during errorsagentproxy/function_executor.py- SAVE_SESSION function.gitignore- Updated patternsDeleted Files
test_state_detection.py- Obsolete test fileVerification
All entry points tested and working:
python cli.py --helppython server.py --helppa --helppa-server --helppython -m agentproxy --helppython -m agentproxy.server --helpBreaking Changes
None - full backward compatibility maintained.