Version: 0.14.x → 0.15.0
Status: Current Breaking Change
This guide covers breaking changes in version 0.15.0 that require OpenSearch index updates before deployment.
Overview
Section titled “Overview”Version 0.15.0 introduces dramatically improved message-level analytics and message content search capabilities. This is a breaking change that requires running an OpenSearch index update script before deployment.
Breaking Change:
- Session index must be updated with new fields before code deployment
- If not done, OpenSearch will auto-index fields incorrectly, requiring expensive reindexing
What's New:
- Message-level analytics (user/assistant counts, timing metrics)
- Message content search in Session Insights
- Dedicated message index for fast full-text search
- Pre-computed statistics for 10-100x faster analytics
Estimated Time: 30-45 minutes (varies with data volume)
Prerequisites
Section titled “Prerequisites”Before starting the migration, ensure you have:
Environment Configuration:
services/pika/.env.localwith:stage- Your deployment stagePIKA_DOMAIN_ENDPOINT- OpenSearch endpointAWS_REGION- AWS regionPIKA_SERVICE_PROJ_NAME_KEBAB_CASE- Your project name
AWS Credentials:
- Ensure AWS CLI is configured with credentials that have access to:
- OpenSearch domain
- DynamoDB tables (chat-session, chat-message)
- Ensure AWS CLI is configured with credentials that have access to:
Backup Your Data:
- Consider taking snapshots of your DynamoDB tables and OpenSearch indices before proceeding
Migration Steps
Section titled “Migration Steps”Step 1: Update Session Index Mapping (CRITICAL - Do First)
Section titled “Step 1: Update Session Index Mapping (CRITICAL - Do First)”Before deploying any code, update the OpenSearch session index:
cd services/pikapnpm dlx tsx tools/os/update-session-mapping-for-messages.tsWhat it does:
- Adds
messages_summaryfield (nested array for message metadata) - Adds
messages_analysisfield (object for pre-computed timing statistics) - Operation is idempotent (safe to run multiple times)
Expected output:
Starting session index mapping update for message replication...Checking if session index exists...✓ Session index existsUpdating mapping for session index...✓ Successfully updated mapping for sessionVerifying mapping...✓ Confirmed: messages_summary field is now in the mapping✓ Confirmed: messages_analysis field is now in the mapping
✅ Mapping update complete!Verify success:
# Check that new fields exist in mappingcurl -X GET "https://your-os-domain/session/_mapping" | jq '.session.mappings.properties | keys'# Should include messages_summary and messages_analysisStep 2: Sync Pika Framework
Section titled “Step 2: Sync Pika Framework”Run pika sync to get the latest code:
pika syncThis will:
- Update type definitions (ChatMessage, SessionAnalytics)
- Add message replication Lambda logic
- Update frontend analytics dashboard
- Add migration tools
Step 3: Deploy Backend Stack
Section titled “Step 3: Deploy Backend Stack”Deploy the updated backend infrastructure:
cd services/pikapnpm run cdk:deployWhat gets deployed:
- New message index created automatically (via CloudFormation)
- Lambda function updated with message replication logic
- Session index mapping already updated (from Step 1)
- API updated with new analytics endpoints
Verify deployment:
# Check message index was createdcurl -X GET "https://your-os-domain/message"# Should return 200 with index detailsStep 4: Backfill invocationMode and userType to Messages
Section titled “Step 4: Backfill invocationMode and userType to Messages”Messages need invocationMode and userType fields for filtering:
cd services/pikapnpm dlx tsx tools/backfill-message-metadata/index.tsWhat it does:
- Scans all chat messages
- Fetches corresponding session for each message
- Copies
invocationModeanduserTypefrom session to message - Defaults to 'chat-app' and 'internal-user' if session has no values
Time estimate: ~5-10 minutes for 100k messages
Expected output:
Starting backfill of invocationMode and userType to messages...Scanning messages table...Processed: 100 messages, Updated: 95Processed: 200 messages, Updated: 195...✅ Backfill complete!Total messages processed: 10,234Messages updated: 9,876Messages skipped (already had fields): 358Step 5: Backfill Messages to OpenSearch
Section titled “Step 5: Backfill Messages to OpenSearch”Populate the message index and session analytics fields with historical data:
cd services/pika# Test with a small date range firstpnpm dlx tsx tools/backfill-messages-to-opensearch/index.ts --dry-run --start-date 2024-12-01 --end-date 2024-12-31
# Run full backfill (can be done in phases by date range)pnpm dlx tsx tools/backfill-messages-to-opensearch/index.ts --start-date 2024-01-01 --end-date 2024-12-31What it does:
- Indexes all messages to message index (with llmInstructions extraction)
- Builds messages_summary arrays for sessions
- Calculates messages_analysis statistics (timing, gaps, counts)
- Updates session documents in OpenSearch
Time estimate: ~2-5 minutes per 1,000 messages
Expected output:
Starting backfill of messages to OpenSearch...Date range: 2024-01-01 to 2024-12-31Processing sessions... Processed 50 sessions, 250 messages (3 skipped) Processed 100 sessions, 520 messages (5 skipped) ...✅ Backfill complete!Sessions processed: 2,456Messages indexed: 12,340Sessions updated: 2,451Sessions skipped (filtered by invocationMode): 5Errors: 0Time elapsed: 4m 32sStep 6: Deploy Frontend
Section titled “Step 6: Deploy Frontend”Deploy the updated frontend:
cd apps/pika-chatpnpm run deployStep 7: Verify in Production
Section titled “Step 7: Verify in Production”- Check CloudWatch logs for successful replications:
aws logs tail /aws/lambda/message-changed-lambda --follow# Look for SUCCESS_REPLICATION entries- Query message index directly to verify messages indexed:
curl -X GET "https://your-os-domain/message/_count"# Should show count of indexed messagesView analytics dashboard - verify new metrics display:
- Navigate to Session Analytics in admin site
- Check for new KPIs (user messages, assistant messages, timing)
- Verify new charts render (messages time series, timing analytics)
Test Session Insights search with message content:
- Navigate to Session Insights in admin site
- Search for a term that only appears in message content
- Verify sessions containing that term are returned
Post-Migration
Section titled “Post-Migration”Monitoring
Section titled “Monitoring”Set up CloudWatch alarms for:
- Lambda errors (> 10 in 5 minutes)
- OpenSearch cluster health (not green)
- Write latency (> 1 second)
Monitor these metrics for the first 24 hours:
- Lambda duration and error rate
- OpenSearch CPU and memory usage
- Storage growth rate
Rollback (If Needed)
Section titled “Rollback (If Needed)”If issues arise:
Lambda replication causing problems:
- Disable Lambda trigger (remove DynamoDB stream mapping)
- Messages will queue in stream (24 hour retention)
- Fix issue, re-enable trigger
Backend API issues:
- Revert to previous API version
- Frontend gracefully handles missing fields (uses optional chaining)
Frontend issues:
- Revert frontend deployment
- Backend changes are backwards compatible
OpenSearch mapping issues:
- Mappings are additive (new fields added)
- Old queries still work
- If needed, can re-create index (requires full reindex)
Troubleshooting
Section titled “Troubleshooting”Issue: Message index not created
Section titled “Issue: Message index not created”Check:
curl -X GET "https://your-os-domain/_cat/indices?v"# Should show 'message' indexSolution: Redeploy backend stack - CloudFormation custom resource should create it automatically.
Issue: Messages not replicating
Section titled “Issue: Messages not replicating”Check CloudWatch logs:
aws logs tail /aws/lambda/message-changed-lambda --filter-pattern "FAILED_REPLICATION"Common causes:
- OpenSearch cluster at capacity (scale up)
- Network issues (check VPC configuration)
- Malformed message data (check specific message)
Issue: Backfill taking too long
Section titled “Issue: Backfill taking too long”Solution: Process in smaller date ranges:
cd services/pikapnpm dlx tsx tools/backfill-messages-to-opensearch/index.ts --start-date 2024-01-01 --end-date 2024-03-31pnpm dlx tsx tools/backfill-messages-to-opensearch/index.ts --start-date 2024-04-01 --end-date 2024-06-30Issue: Analytics showing zero messages
Section titled “Issue: Analytics showing zero messages”Verify:
- Backfill completed successfully
- Message index has documents:
curl -X GET "https://your-os-domain/message/_count" - Session documents have messages_summary:
curl -X GET "https://your-os-domain/session/_search?size=1"
Need Help?
Section titled “Need Help?”- Questions? Check Troubleshooting docs
- Found a bug? Report it on GitHub
- Rollback issues? See Rollback section above
Changes Summary
Section titled “Changes Summary”What's New
Section titled “What's New”- Message-level analytics (user/assistant counts, timing metrics)
- Message content search in Session Insights
- Dedicated message index for fast full-text search
- Pre-computed statistics for 10-100x faster analytics
What Changed
Section titled “What Changed”- Session index now includes message metadata arrays
- ChatMessage type includes invocationMode, userType, and llmInstructions fields
- Lambda replicates to three locations (message index + 2 session fields)
What's Removed
Section titled “What's Removed”- Nothing removed, all changes are additive