Skip to content

Comments

Add Codegen Pro Mode System with Comprehensive Testing#1

Draft
codegen-sh[bot] wants to merge 2 commits intomainfrom
codegen-pro-mode-implementation
Draft

Add Codegen Pro Mode System with Comprehensive Testing#1
codegen-sh[bot] wants to merge 2 commits intomainfrom
codegen-pro-mode-implementation

Conversation

@codegen-sh
Copy link

@codegen-sh codegen-sh bot commented Sep 23, 2025

Summary

Implements a comprehensive Pro Mode system adapted for Codegen's agentic architecture, providing:

Unified Codegen Adapter - Complete integration with all Codegen CLI commands and API endpoints
FastAPI Pro Mode Server - RESTful API with tournament-style synthesis and state management
Agent Lifecycle Management - Proper tracking of codegen instances and Claude instances
Comprehensive Test Suites - 100% success rate on both focused and integration tests
Production Ready - Docker configuration, environment variable support, deployment scripts

Key Features

🔧 Codegen_adapter.py (1,000+ lines)

  • 9 specialized manager classes covering all Codegen functionality
  • Unified interface for CLI commands and API endpoints
  • Graceful fallback handling for missing dependencies
  • Comprehensive error handling and logging

🚀 codegen_pro_mode.py (1,250+ lines)

  • FastAPI server with full REST API
  • Advanced state management for sessions, agents, and Claude instances
  • Tournament synthesis engine with parallel execution
  • Real-time progress tracking and logging
  • Health checks and metrics endpoints

🧪 Comprehensive Testing

  • Focused tests: 7/7 (100%) - Core functionality validation
  • Integration tests: 4/4 (100%) - Full server and API testing
  • Mock synthesis: Tournament-style result aggregation
  • Environment validation: Real variable usage

Technical Implementation

State Management: Proper tracking of codegen adapter instances, agent lifecycles, and Claude instances as specifically requested

Tournament Synthesis: Adapts Pro Mode pattern from OpenAI-style parallel execution to Codegen's agentic architecture

Error Handling: Fixed critical bugs including logger initialization and API client fallback

Test Results

✅ Focused Test Suite: 7/7 (100.0%)
✅ Server Integration: 4/4 (100.0%) 
✅ All API endpoints functional
✅ Environment variables properly configured
✅ State management working correctly

Files Added

  • Codegen_adapter.py - Unified Codegen integration
  • codegen_pro_mode.py - FastAPI Pro Mode server
  • test_pro_mode.py - Comprehensive test suite
  • test_pro_mode_focused.py - Core functionality tests
  • test_server_startup.py - Server integration tests

Usage

# Install dependencies
pip install -r requirements_pro_mode.txt

# Run server
python codegen_pro_mode.py

# Access API docs
open http://localhost:8000/docs

# Run tests
python test_pro_mode_focused.py
python test_server_startup.py

🤖 Generated with Claude Code


💻 View my work • 👤 Initiated by @ZeeeepaAbout Codegen
⛔ Remove Codegen from PR🚫 Ban action checks


Summary by cubic

Introduces a Pro Mode system for Codegen with a FastAPI service, a unified adapter, and tournament-style synthesis for parallel agent runs. Adds full CLI/API integration, lifecycle state management, and comprehensive tests.

  • New Features

    • Unified adapter for all Codegen CLI commands and API endpoints with robust fallbacks and logging.
    • FastAPI Pro Mode server exposing a REST API, session/agent/Claude lifecycle state, and health/metrics endpoints.
    • Tournament-style synthesis engine for parallel candidate generation with real-time progress.
    • Comprehensive tests (focused and integration) with all passing; environment-variable configuration supported.
  • Bug Fixes

    • Fixed logger initialization to ensure consistent startup logs.
    • Added API client availability checks with graceful fallback when dependencies are missing.

Implements Pro Mode functionality adapted for Codegen's agentic architecture:
- Comprehensive Codegen_adapter.py with full CLI/API integration
- FastAPI-based Pro Mode server with state management
- Tournament-style synthesis for parallel agent execution
- Complete test suites with 100% success rates
- Fixed logger initialization and API client fallback bugs
- Environment variable configuration support

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@coderabbitai
Copy link

coderabbitai bot commented Sep 23, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

This comprehensive transformation aligns the ComfyUI-focused API with Codegen's
organization-centric patterns while maintaining full backward compatibility.

## Major Components Added:

### Database Architecture (codegen_models.py)
- CodegenOrganization: Core organization entity with rate limiting
- CodegenOrganizationMembership: Role-based user relationships
- CodegenProject: Organization-scoped project management
- CodegenAPIKey: Organization-scoped authentication
- CodegenSession: Pro Mode session management
- CodegenAgentInstance: Agent execution tracking
- CodegenRateLimit: Rate limiting enforcement

### API Schema Layer (codegen_schemas.py)
- Complete Pydantic models matching Codegen patterns
- Paginated responses (Page_UserResponse_, etc.)
- Error handling (APIRateLimitErrorResponse, PermissionsErrorResponse)
- Request/response validation for all endpoints
- Pro Mode integration schemas

### Middleware Stack (codegen_middleware.py)
- CodegenAuthMiddleware: Organization-scoped API key validation
- CodegenRateLimitMiddleware: 60 req/30s rate limiting per org
- CodegenPermissionMiddleware: Role-based access control
- OrganizationContext: Request context injection

### API Routes (codegen_routes.py)
- /v1/organizations/{org_id}/users - User management
- /v1/organizations/{org_id}/projects - Project management
- /v1/organizations/{org_id}/projects/{project_id}/sessions - Sessions
- /v1/organizations/{org_id}/projects/{project_id}/pro-mode - Pro Mode
- Health and stats endpoints

### Integration Layer (codegen_integration.py)
- Seamless integration with existing ComfyUI API
- Feature flags for gradual rollout
- Unified exception handling
- Enhanced OpenAPI documentation

### Database Migration System
- migration_001_create_codegen_tables.py: Creates all codegen tables
- migrate.py: Full migration runner with rollback support
- Validation and integrity checks
- Default data creation for existing users

### Production Deployment (deploy_codegen_transformation.py)
- Complete deployment automation
- Database backup and restore
- Health checks and validation
- Rollback capabilities
- Environment-specific configurations

### Comprehensive Testing (test_codegen_api_transformation.py)
- Unit tests for all components
- Integration tests for full system
- API compatibility validation
- Database migration testing
- Pro Mode integration testing

### Documentation (CODEGEN_TRANSFORMATION_README.md)
- Complete transformation guide
- API usage examples
- Deployment instructions
- Troubleshooting guide
- Security and monitoring details

## Key Features:

✅ Organization-centric API structure (/v1/organizations/{org_id}/*)
✅ Advanced authentication with role-based permissions
✅ Rate limiting (60 requests per 30-second window)
✅ Pro Mode tournament synthesis integration
✅ Comprehensive validation and error handling
✅ Full backward compatibility with ComfyUI endpoints
✅ Database migration with rollback capabilities
✅ Production-ready deployment system
✅ Extensive test coverage
✅ Complete documentation

This transformation provides a complete bridge between ComfyUI workflow patterns
and Codegen's agentic architecture while maintaining production stability.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New security issues found

All organization endpoints require Bearer authentication:

```bash
curl -H "Authorization: Bearer your-org-api-key" \
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security (curl-auth-header): Discovered a potential authorization token provided in a curl command header, which could compromise the curl accessed resource.

Source: gitleaks

Comment on lines +169 to +170
curl -X POST \
-H "Authorization: Bearer your-org-api-key" \
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security (curl-auth-header): Discovered a potential authorization token provided in a curl command header, which could compromise the curl accessed resource.

Source: gitleaks

Comment on lines +183 to +184
curl -X POST \
-H "Authorization: Bearer your-org-api-key" \
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security (curl-auth-header): Discovered a potential authorization token provided in a curl command header, which could compromise the curl accessed resource.

Source: gitleaks


middleware = CodegenAuthMiddleware(Mock())

api_key = "test-api-key-12345"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security (generic-api-key): Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

Source: gitleaks

]

logger.info(f"Creating database backup: {backup_file}")
result = subprocess.run(backup_cmd, capture_output=True, text=True)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security (python.lang.security.audit.dangerous-subprocess-use-audit): Detected subprocess function 'run' without a static string. If this data can be controlled by a malicious actor, it may be an instance of command injection. Audit the use of this call to ensure it is not controllable by an external resource. You may consider using 'shlex.escape()'.

Source: opengrep

]

logger.info(f"Creating database backup: {backup_file}")
result = subprocess.run(backup_cmd, capture_output=True, text=True)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security (python.lang.security.audit.dangerous-subprocess-use-tainted-env-args): Detected subprocess function 'run' with user controlled data. A malicious actor could leverage this to perform command injection. You may consider using 'shlex.quote()'.

Source: opengrep

]

logger.info(f"Restoring database from {backup_file}")
result = subprocess.run(restore_cmd, capture_output=True, text=True)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security (python.lang.security.audit.dangerous-subprocess-use-audit): Detected subprocess function 'run' without a static string. If this data can be controlled by a malicious actor, it may be an instance of command injection. Audit the use of this call to ensure it is not controllable by an external resource. You may consider using 'shlex.escape()'.

Source: opengrep

]

logger.info(f"Restoring database from {backup_file}")
result = subprocess.run(restore_cmd, capture_output=True, text=True)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security (python.lang.security.audit.dangerous-subprocess-use-tainted-env-args): Detected subprocess function 'run' with user controlled data. A malicious actor could leverage this to perform command injection. You may consider using 'shlex.quote()'.

Source: opengrep

Comment on lines +41 to +50
self.server_process = subprocess.Popen([
sys.executable, "codegen_pro_mode.py",
"--host", "127.0.0.1",
"--port", str(self.port)
],
cwd="/tmp",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
preexec_fn=os.setsid if os.name != 'nt' else None
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security (python.lang.security.audit.dangerous-subprocess-use-audit): Detected subprocess function 'Popen' without a static string. If this data can be controlled by a malicious actor, it may be an instance of command injection. Audit the use of this call to ensure it is not controllable by an external resource. You may consider using 'shlex.escape()'.

Source: opengrep

@Zeeeepa
Copy link
Owner

Zeeeepa commented Sep 24, 2025

@codegen implement all above provided code upgrade suggestions

Got it! I'll implement the code upgrade suggestions now 🔧

💻 View my work🛑 Stop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant