feat: Implement Phase 1 - Plugin & Hook System
Core plugin architecture for LLM-driven optimization: New Features: - Hook system with 6 lifecycle points (pre_mesh, post_mesh, pre_solve, post_solve, post_extraction, custom_objectives) - HookManager for centralized registration and execution - Code validation with AST-based safety checks - Feature registry (JSON) for LLM capability discovery - Example plugin: log_trial_start - 23 comprehensive tests (all passing) Integration: - OptimizationRunner now loads plugins automatically - Hooks execute at 5 points in optimization loop - Custom objectives can override total_objective via hooks Safety: - Module whitelist (numpy, scipy, pandas, optuna, pyNastran) - Dangerous operation blocking (eval, exec, os.system, subprocess) - Optional file operation permission flag Files Added: - optimization_engine/plugins/__init__.py - optimization_engine/plugins/hooks.py - optimization_engine/plugins/hook_manager.py - optimization_engine/plugins/validators.py - optimization_engine/feature_registry.json - optimization_engine/plugins/pre_solve/log_trial_start.py - tests/test_plugin_system.py (23 tests) Files Modified: - optimization_engine/runner.py (added hook integration) Ready for Phase 2: LLM interface layer 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1
optimization_engine/plugins/pre_solve/__init__.py
Normal file
1
optimization_engine/plugins/pre_solve/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# pre_solve hooks
|
||||
56
optimization_engine/plugins/pre_solve/log_trial_start.py
Normal file
56
optimization_engine/plugins/pre_solve/log_trial_start.py
Normal file
@@ -0,0 +1,56 @@
|
||||
"""
|
||||
Example Plugin: Log Trial Start
|
||||
|
||||
Simple pre-solve hook that logs trial information.
|
||||
|
||||
This demonstrates the plugin API and serves as a template for custom hooks.
|
||||
"""
|
||||
|
||||
from typing import Dict, Any, Optional
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def trial_start_logger(context: Dict[str, Any]) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Log trial information before solver execution.
|
||||
|
||||
Args:
|
||||
context: Hook context containing:
|
||||
- trial_number: Current trial number
|
||||
- design_variables: Dict of variable values
|
||||
- sim_file: Path to simulation file
|
||||
|
||||
Returns:
|
||||
Dict with logging status
|
||||
"""
|
||||
trial_num = context.get('trial_number', '?')
|
||||
design_vars = context.get('design_variables', {})
|
||||
|
||||
logger.info("=" * 60)
|
||||
logger.info(f"TRIAL {trial_num} STARTING")
|
||||
logger.info("=" * 60)
|
||||
|
||||
for var_name, var_value in design_vars.items():
|
||||
logger.info(f" {var_name}: {var_value:.4f}")
|
||||
|
||||
return {'logged': True, 'trial': trial_num}
|
||||
|
||||
|
||||
def register_hooks(hook_manager):
|
||||
"""
|
||||
Register this plugin's hooks with the manager.
|
||||
|
||||
This function is called automatically when the plugin is loaded.
|
||||
|
||||
Args:
|
||||
hook_manager: The HookManager instance
|
||||
"""
|
||||
hook_manager.register_hook(
|
||||
hook_point='pre_solve',
|
||||
function=trial_start_logger,
|
||||
description='Log trial number and design variables before solve',
|
||||
name='log_trial_start',
|
||||
priority=10 # Run early
|
||||
)
|
||||
Reference in New Issue
Block a user