DSPex Variables Bridge Implementation: Reacclimation Prompt
Context Refresher
You are implementing Phase 1 of the DSPex Variables Bridge - a revolutionary system that enables cross-module parameter optimization in language model programming. This represents a fundamental shift from DSPy’s module-centric approach to a variable-centric paradigm.
Required Reading to Get Up to Speed
1. Core Architecture Understanding
Read these files in order:
lib/dspex.ex
- Main module entry point- Focus on: Module structure and public API design patterns
- Key sections: Module documentation and main function signatures
lib/dspex/lm.ex
- Language model configuration- Focus on: How LM configuration works,
configure/2
function - Key pattern: Settings management and Python bridge usage
- Focus on: How LM configuration works,
lib/dspex/modules/predict.ex
- Core DSPy module wrapper- Focus on: How DSPy modules are wrapped,
create/2
andcall/2
patterns - Key insight: Stored module pattern with IDs
- Focus on: How DSPy modules are wrapped,
snakepit/lib/snakepit/python.ex
- Python bridge core- Focus on:
call/3
function signature, session management - Key sections: How Python code is executed and results returned
- Focus on:
snakepit/priv/python/enhanced_command_handler.py
- Python command handler- Focus on:
handle_command
method, object storage mechanism - Key sections: Lines 50-150 (core command handling)
- Critical: Understand the
stored_objects
dictionary pattern
- Focus on:
2. Variables System Design
Essential documentation:
docs/specs/dspex_variables_implementation_strategy.md
- Read: Entire document, especially “Phase 1: DSPy Adapter Layer” section
- Key concepts: Variable Registry, DSPy Variable Adapter, Integration Layer
docs/specs/dspex_cognitive_orchestration/02_variable_coordination_system.md
- Focus on: Variable structure (lines 50-80), Core operations (lines 85-120)
- Key insight: Variables as coordination points, not just parameters
docs/GENERALIZED_VARIABLES_DSPY_FEASIBILITY_20250719.md
- Focus on: “Minimum Native Components” section (lines 82-227)
- Critical: Understand why native evaluation is essential
3. Integration Patterns
Study these examples:
examples/dspy/01_basic_usage.exs
- Focus on: How DSPy modules are currently used
- Pattern to extend: Module creation and invocation
lib/dspex/settings.ex
- Focus on: Configuration management pattern
- Key insight: How settings propagate to Python
Current State Summary
What Exists:
- Basic DSPy module wrappers (Predict, ChainOfThought, etc.)
- Python bridge with object storage
- Settings and LM configuration
- Module ID generation and storage pattern
What’s Missing (You Will Build):
- Variable Registry (GenServer-based)
- Variable types and constraints
- Python-side variable injection
- Cross-module variable sharing
- Variable observation pattern
Phase 1 Implementation Goals
1. Variable Registry (Elixir Side)
Build a GenServer that:
- Manages all variables in the system
- Tracks observers and dependencies
- Stores optimization history
- Provides atomic updates with notifications
2. Python Variable Adapter
Create Python code that:
- Injects variable values into DSPy modules
- Tracks variable usage during execution
- Reports impact back to Elixir
- Maintains DSPy compatibility
3. Integration Layer
Connect the two sides:
- Variable-aware module creation
- Execution with variable application
- Feedback extraction and propagation
Key Technical Challenges
- State Synchronization: Variables must stay synchronized between Elixir and Python across multiple calls
- Module Wrapping: Must wrap DSPy modules without breaking their functionality
- Performance: Variable updates should have minimal overhead (<10ms)
- Compatibility: Existing DSPy code must continue working unchanged
Implementation Approach
Start Here:
Create Variable Registry GenServer
- File:
lib/dspex/variables/registry.ex
- Use ETS for fast lookups
- Implement observer pattern
- Add telemetry events
- File:
Create Variable Type System
- File:
lib/dspex/variables/types.ex
- Start with: Float, Integer, Choice, Module
- Each type needs: validate/1, cast/1, constraints/0
- File:
Extend Python Command Handler
- File:
snakepit/priv/python/dspex_variables.py
(new) - Import and extend EnhancedCommandHandler
- Add variable injection mechanism
- Create VariableAwareModule wrapper
- File:
Build Integration Bridge
- File:
lib/dspex/variables/dspy_bridge.ex
- Functions: create_variable_aware_module/3, execute_with_variables/3
- Handle variable value propagation
- Extract and process feedback
- File:
Code Patterns to Follow:
# Module creation pattern (from existing code)
{:ok, module_id} = Snakepit.Python.call(:python, code, store_as: id)
# Settings pattern (for variable values)
DSPex.Settings.put(:variables, variable_values)
# Observer pattern (new)
Registry.observe(variable_id, self())
# Object storage pattern (from existing)
self.stored_objects[store_as] = result
# Method call pattern (to extend)
obj = self._resolve_stored_ref(obj_ref)
method = getattr(obj, method_name)
result = method(*args, **kwargs)
Success Criteria for Phase 1
- Variable Registration: Can create and manage variables with constraints
- Value Injection: Variables affect DSPy module behavior
- Change Observation: Observers get notified of variable updates
- Backward Compatibility: All existing examples still work
- Performance: <10ms overhead per variable operation
Next Steps After Reading
- Review the existing codebase with focus on the patterns mentioned
- Create the directory structure for new modules
- Start with Variable Registry implementation
- Test each component in isolation before integration
- Create examples demonstrating variable usage
Remember: The goal is not to modify DSPy but to wrap and extend it with variable awareness while maintaining full compatibility.