Scenarios Feature - Engineering Documentation
⚠️ IMPORTANT NOTICE (as of 9/13/2025): This feature currently operates in PRODUCTION ONLY. There is no development or test environment available. All testing and development work must be done carefully in the production environment. Please coordinate with the team before making any infrastructure changes.
Overview
The Scenarios feature is an AI-powered financial analysis tool that allows users to explore “what-if” scenarios in financial markets. Users can ask questions about market conditions, portfolio impacts, and investment strategies, receiving detailed analysis with charts and insights. Key Capabilities:- AI-powered financial scenario analysis
- Real-time chart generation
- Code execution tracking
- Asynchronous processing with status updates
- Infinite scroll pagination
- Analytics tracking
System Architecture Overview
Architecture
Frontend (Next.js)
- Location:
app/scenarios/ - Main Component:
page.tsx- React component with real-time updates - Layout: Uses
LayoutWithSidebarfor consistent navigation
Backend (AWS)
- Location:
aws/ - Infrastructure: AWS CDK (Cloud Development Kit)
- Stack:
AgentStackdeployed via CDK - API: REST API hosted on AWS Lambda + API Gateway
- Queue: SQS for asynchronous consultation processing
- Storage: DynamoDB for data, S3 for charts
- Secrets: AWS Secrets Manager for API keys
Technical Stack
Frontend Dependencies
Backend Infrastructure
AWS Infrastructure Details
SQS Queue Architecture
DynamoDB Tables
S3 Chart Storage
Lambda Functions
Processing Flow
1. Consultation Creation Flow
2. Status Polling Flow
Core Functionality
1. Scenario Creation
2. Real-time Status Polling
3. Infinite Scroll Loading
API Endpoints
Base URL
Endpoints
1. Create Consultation
2. Get Consultations (Paginated)
3. Get Consultation Status
Data Models
Consultation Object
Chart Metadata
Code Execution
Key Features
1. Status Management
- Pending: Consultation created, waiting to start
- Queued: In processing queue
- Processing: AI actively analyzing (1-10 minutes)
- Completed: Analysis finished with results
- Failed: Error occurred, retry available
2. Chart Display
- S3-hosted chart images with presigned URLs
- Modal expansion for detailed viewing
- Lazy loading with loading states
- Error handling for failed image loads
3. Response Rendering
- Markdown support for formatted AI responses
- Tabbed interface (Response/Charts/Code)
- Collapsible sections for better UX
4. Analytics Tracking
5. Chart Generation & Display
6. Code Execution Timeline
Deployment
AWS Infrastructure
SQS Queue Monitoring
Environment Variables
Development Workflow
1. Local Development
2. Testing Scenarios
3. Monitoring
- CloudWatch logs for Lambda functions
- PostHog analytics for user behavior
- Real-time status polling for processing updates
Performance Considerations
1. Frontend Optimizations
- Intersection Observer for infinite scroll
- Debounced polling intervals
- Optimistic UI updates
- Image lazy loading
2. Backend Optimizations
- SQS Queue Processing: Asynchronous consultation processing with dead letter queue
- Lambda Scaling: Auto-scaling based on queue depth
- DynamoDB: Optimized for user-scoped queries with TTL for status cleanup
- S3 Chart Storage: Presigned URLs with 30-day lifecycle policy
- Memory Allocation: 3GB for AI processor, 1GB for API functions
- Timeout Management: 15-minute timeout for long-running AI analysis
3. Error Handling
- Retry mechanism for failed scenarios
- Graceful degradation for missing charts
- Timeout handling for long-running processes
Security
Authentication
- User ID passed via
X-User-Idheader - Integration with existing auth system
- User-scoped data access
Data Protection
- S3 presigned URLs for secure chart access
- User data isolation
- No sensitive financial data in logs
Future Enhancements
Planned Features
- Real-time Collaboration: Share scenarios between users
- Scenario Templates: Pre-built scenario types
- Export Functionality: PDF/Excel export of results
- Advanced Filtering: Filter by status, date, topic
- Scenario Comparison: Side-by-side analysis
Technical Improvements
- WebSocket Integration: Replace polling with real-time updates
- Caching Layer: Redis for frequently accessed scenarios
- Batch Processing: Handle multiple scenarios simultaneously
- Advanced Analytics: More detailed user behavior tracking
Troubleshooting
Common Issues
1. Polling Not Working
2. Charts Not Loading
- Verify S3 presigned URL validity (expires after 1 hour)
- Check CORS configuration in S3 bucket
- Ensure proper error handling for failed image loads
- Verify chart generation completed successfully in processor logs
3. SQS Processing Issues
4. Lambda Function Issues
5. DynamoDB Query Issues
6. Infinite Scroll Issues
- Verify Intersection Observer setup
- Check
hasMoreandlastKeystate management - Ensure proper cleanup on unmount
Debug Commands
Contact & Support
For technical questions or issues:- Frontend Issues: Check React DevTools and browser console
- Backend Issues: Review CloudWatch logs
- Infrastructure: Verify CDK stack deployment
- Analytics: Check PostHog dashboard for event tracking