# Production Monitoring Checklist for Screenshare Bot

**Purpose**: Structured monitoring procedures for the screenshare bot during and after deployment.

**Audience**: Operations team, on-call engineers, deployment personnel

---

## Table of Contents

1. [Initial Deployment Monitoring (First 4 Hours)](#initial-deployment-monitoring-first-4-hours)
2. [Phase 2 & 3 Monitoring](#phase-2--3-monitoring)
3. [48-Hour Stability Period](#48-hour-stability-period)
4. [Ongoing Production Monitoring](#ongoing-production-monitoring)
5. [Alert Response Procedures](#alert-response-procedures)

---

## Initial Deployment Monitoring (First 4 Hours)

### Monitoring Frequency

**Every 30 minutes** for the first 4 hours after deployment

### Monitoring Checklist (30-Minute Intervals)

```
Phase 1 - Single Bot Monitoring

□ Railway Dashboard Check
  □ Service status: RUNNING
  □ Memory usage recorded: ______ MB
  □ CPU usage recorded: ______ %
  □ No restart events
  □ Build status: SUCCESS

□ Bot Status Check
  □ Active bot count: ______ (Expected: 1)
  □ Bot conversation ID: ________________
  □ Bot spawned successfully (check logs)
  □ Bot joined Daily.co room (check logs)

□ Performance Metrics
  □ Memory per bot: ______ MB (Target: <350 MB)
  □ CPU per bot: ______ % (Target: <30%)
  □ FPS: ______ (Target: 9.5-10.5)
  □ Frame capture time: ______ ms (Target: <50ms)
  □ Command latency: ______ ms (Target: <500ms)

□ Log Review
  □ No ERROR logs in last 30 min
  □ No crash logs (exit code != 0)
  □ No OOM (Out of Memory) indicators
  □ Performance logs present and healthy

□ Functionality Check
  □ Bot streaming frames (check "Frames:" in logs)
  □ Slide navigation working (if tested)
  □ Bot responds to kill command

□ Issues/Notes
  Notes: ________________________________________________
  ________________________________________________________
```

### Commands for 30-Minute Checks

```bash
# Set up environment
export API_URL="https://slide-system-production-5813.up.railway.app"
export CHECK_TIME=$(date +"%Y-%m-%d %H:%M:%S")

echo "=== Monitoring Check at $CHECK_TIME ===" | tee -a monitoring_log.txt

# 1. Railway Status
railway status --service slide-system | tee -a monitoring_log.txt

# 2. Bot Count
curl -s "$API_URL/api/tavus/bot/status" | jq | tee -a monitoring_log.txt

# 3. Performance Metrics
railway logs --service slide-system | grep "Performance -" | tail -3 | tee -a monitoring_log.txt

# 4. Recent Errors
ERROR_COUNT=$(railway logs --service slide-system --since 30m | grep -i "error" | wc -l)
echo "Errors in last 30min: $ERROR_COUNT" | tee -a monitoring_log.txt

# 5. Bot Crashes
CRASH_COUNT=$(railway logs --service slide-system --since 30m | grep "Bot process exited" | grep -cv "code: 0")
echo "Crashes in last 30min: $CRASH_COUNT" | tee -a monitoring_log.txt

echo "" | tee -a monitoring_log.txt
```

### Decision Points

**After 1 Hour**:
- ✅ If all checks pass: Continue monitoring
- ⚠️ If warnings detected: Investigate but continue
- ❌ If critical issues: Consider rollback

**After 2 Hours**:
- ✅ All checks pass: Increase confidence
- ⚠️ Persistent warnings: Investigate deeper
- ❌ Critical issues persist: Rollback

**After 4 Hours**:
- ✅ All checks pass: Phase 1 SUCCESS - Proceed to Phase 2 (if gradual rollout)
- ❌ Any failures: Rollback and investigate

---

## Phase 2 & 3 Monitoring

### Phase 2 (3 Bots) - Every 30 Minutes for 4 Hours

```
Phase 2 - Three Bot Monitoring

□ Bot Status
  □ Active bot count: ______ (Expected: 3)
  □ All bots spawned successfully
  □ All bots joined Daily.co rooms
  □ All bots streaming

□ Total Resource Usage
  □ Total memory: ______ MB (Target: <1050 MB, Max: 1500 MB)
  □ Total CPU: ______ % (Target: <90%, Max: 120%)
  □ Railway memory limit not exceeded

□ Per-Bot Performance
  □ Bot 1 - Memory: ______ MB, CPU: ______ %, FPS: ______
  □ Bot 2 - Memory: ______ MB, CPU: ______ %, FPS: ______
  □ Bot 3 - Memory: ______ MB, CPU: ______ %, FPS: ______

□ Stability Checks
  □ No memory leaks detected (stable over time)
  □ No CPU spikes or degradation
  □ FPS stable across all bots
  □ No resource contention between bots

□ Error Monitoring
  □ Error count last 30min: ______ (Target: <5)
  □ Crash count last 30min: ______ (Target: 0)
  □ No spawn failures

□ Issues/Notes
  Notes: ________________________________________________
```

### Phase 3 (5 Bots) - Variable Frequency

**First 4 Hours**: Every 30 minutes
**Hours 4-12**: Every 1 hour
**Hours 12-24**: Every 2 hours
**Hours 24-48**: Every 4 hours

```
Phase 3 - Five Bot Monitoring

□ Bot Status
  □ Active bot count: ______ (Expected: 5)
  □ All bots healthy

□ Total Resource Usage
  □ Total memory: ______ MB (Target: <1750 MB, Max: 2500 MB)
  □ Total CPU: ______ % (Target: <150%, Max: 200%)

□ Per-Bot Average Performance
  □ Avg memory per bot: ______ MB (Target: <350 MB)
  □ Avg CPU per bot: ______ % (Target: <30%)
  □ Avg FPS across bots: ______ (Target: 9.9-10.1)

□ Long-Term Stability
  □ Memory increase rate: ______ MB/hour (Target: <10 MB/hour)
  □ Crash rate: ______ crashes/hour (Target: 0)
  □ Error rate: ______ errors/hour (Target: <1)
  □ Spawn success rate: ______ % (Target: >99%)

□ Performance Trends
  □ Memory trend: [ ] Stable  [ ] Increasing  [ ] Decreasing
  □ CPU trend: [ ] Stable  [ ] Increasing  [ ] Decreasing
  □ FPS trend: [ ] Stable  [ ] Degrading

□ Issues/Notes
  Notes: ________________________________________________
```

### Commands for Phase 2 & 3 Monitoring

```bash
# Phase 2/3 Monitoring Script
export CHECK_TIME=$(date +"%Y-%m-%d %H:%M:%S")

echo "=== Phase 2/3 Check at $CHECK_TIME ===" | tee -a monitoring_log.txt

# 1. Active bot count
ACTIVE=$(curl -s "$API_URL/api/tavus/bot/status" | jq -r '.activeCount')
echo "Active Bots: $ACTIVE" | tee -a monitoring_log.txt

# 2. Total resources
TOTAL_MEM=$(railway logs --service slide-system | grep "Memory:" | tail -5 | \
  awk '{sum+=$6} END {print sum}')
TOTAL_CPU=$(railway logs --service slide-system | grep "CPU:" | tail -5 | \
  awk '{sum+=$8} END {print sum}')

echo "Total Memory: ${TOTAL_MEM} MB" | tee -a monitoring_log.txt
echo "Total CPU: ${TOTAL_CPU}%" | tee -a monitoring_log.txt

# 3. Performance per bot
railway logs --service slide-system | grep "Performance -" | tail -5 | tee -a monitoring_log.txt

# 4. Error & crash counts
ERROR_COUNT=$(railway logs --service slide-system --since 30m | grep -i "error" | wc -l)
CRASH_COUNT=$(railway logs --service slide-system --since 30m | grep "Bot process exited" | grep -cv "code: 0")

echo "Errors: $ERROR_COUNT, Crashes: $CRASH_COUNT" | tee -a monitoring_log.txt

echo "" | tee -a monitoring_log.txt
```

---

## 48-Hour Stability Period

### Monitoring Schedule

**Hours 0-4**: Every 30 minutes
**Hours 4-12**: Every 1 hour
**Hours 12-24**: Every 2 hours
**Hours 24-48**: Every 4 hours

### Extended Monitoring Checklist

```
48-Hour Stability Monitoring

Date: ________________  Time: ________________

□ System Health
  □ Service uptime: ______ hours
  □ Service status: [ ] RUNNING  [ ] Other: __________
  □ Railway resource usage within limits
  □ No service restarts in last check period

□ Bot Health Summary
  □ Total bots spawned since deployment: ______
  □ Total bots terminated normally: ______
  □ Total crashes: ______ (Target: 0)
  □ Current active bots: ______

□ Stability Indicators
  □ Memory leak check: [ ] Pass (stable)  [ ] Fail (increasing)
  □ CPU stability: [ ] Pass (stable)  [ ] Fail (degrading)
  □ FPS stability: [ ] Pass (9.5-10.5)  [ ] Fail (<9.5)
  □ Error rate: ______ errors/hour (Target: <1)

□ Performance Trends (vs. Baseline)
  □ Memory: [ ] Below baseline  [ ] At baseline  [ ] Above baseline
  □ CPU: [ ] Below baseline  [ ] At baseline  [ ] Above baseline
  □ FPS: [ ] At target  [ ] Below target

□ Alerts Triggered
  □ Critical alerts in last period: ______ (Target: 0)
  □ Warning alerts in last period: ______ (Target: <3)
  □ All alerts investigated: [ ] Yes  [ ] No

□ Action Items
  □ Issues identified: ______________________________
  □ Actions taken: __________________________________
  □ Follow-up required: [ ] Yes  [ ] No

□ Sign-Off
  Checked by: ________________  Date/Time: ________________
```

### Memory Leak Detection

Run this check every 4 hours during 48-hour period:

```bash
#!/bin/bash
# memory_leak_check.sh

echo "=== Memory Leak Check - $(date) ===" | tee -a memory_trend.log

# Get memory readings over last 4 hours
MEMORY_START=$(railway logs --service slide-system --since 4h | \
  grep "Memory:" | head -5 | awk '{sum+=$6; c++} END {print sum/c}')

MEMORY_END=$(railway logs --service slide-system | \
  grep "Memory:" | tail -5 | awk '{sum+=$6; c++} END {print sum/c}')

MEMORY_DIFF=$(echo "$MEMORY_END - $MEMORY_START" | bc)
LEAK_RATE=$(echo "scale=2; $MEMORY_DIFF / 4" | bc)

echo "Memory Start (4h ago): ${MEMORY_START} MB" | tee -a memory_trend.log
echo "Memory End (now): ${MEMORY_END} MB" | tee -a memory_trend.log
echo "Memory Change: ${MEMORY_DIFF} MB" | tee -a memory_trend.log
echo "Leak Rate: ${LEAK_RATE} MB/hour" | tee -a memory_trend.log

# Decision
if (( $(echo "$LEAK_RATE > 10" | bc -l) )); then
  echo "⚠️ WARNING: Potential memory leak detected (>${LEAK_RATE} MB/hour)" | tee -a memory_trend.log
  echo "ACTION REQUIRED: Investigate memory usage" | tee -a memory_trend.log
else
  echo "✅ Memory stable (leak rate: ${LEAK_RATE} MB/hour)" | tee -a memory_trend.log
fi

echo "" | tee -a memory_trend.log
```

---

## Ongoing Production Monitoring

### Daily Checks (5 minutes)

**Run once per day after deployment stable**

```
Daily Production Health Check

Date: ________________

□ Service Status
  □ Railway status: [ ] RUNNING  [ ] Other
  □ Uptime: ______ days
  □ Recent deployments: ______
  □ Service restarts (last 24h): ______ (Target: 0)

□ Bot Activity (Last 24 Hours)
  □ Total bots spawned: ______
  □ Total bots terminated: ______
  □ Crashes: ______ (Target: 0)
  □ Peak concurrent bots: ______

□ Resource Usage (24h Average)
  □ Avg memory per bot: ______ MB (Target: <350 MB)
  □ Avg CPU per bot: ______ % (Target: <30%)
  □ Avg FPS: ______ (Target: 9.9-10.1)

□ Error Summary
  □ Total errors (24h): ______ (Target: <24)
  □ Critical errors: ______ (Target: 0)
  □ Top error type: ______________________________

□ Alerts
  □ Critical alerts: ______ (Target: 0)
  □ Warning alerts: ______ (Target: <5)
  □ All alerts resolved: [ ] Yes  [ ] No

□ Action Items
  □ Issues to investigate: __________________________
  □ Planned maintenance: ____________________________
```

**Daily Check Commands**:
```bash
#!/bin/bash
# daily_health_check.sh

echo "=== Daily Health Check - $(date) ===" | tee -a daily_health.log

# Service status
railway status --service slide-system | tee -a daily_health.log

# Bot activity
SPAWNED=$(railway logs --service slide-system --since 24h | grep -c "Bot spawned")
TERMINATED=$(railway logs --service slide-system --since 24h | grep "Bot process exited.*code: 0" | wc -l)
CRASHED=$(railway logs --service slide-system --since 24h | grep "Bot process exited" | grep -cv "code: 0")

echo "Bots Spawned: $SPAWNED" | tee -a daily_health.log
echo "Bots Terminated: $TERMINATED" | tee -a daily_health.log
echo "Bots Crashed: $CRASHED" | tee -a daily_health.log

# Averages
AVG_MEM=$(railway logs --service slide-system --since 24h | grep "Memory:" | \
  awk '{sum+=$6; c++} END {printf "%.1f", sum/c}')
AVG_CPU=$(railway logs --service slide-system --since 24h | grep "CPU:" | \
  awk '{sum+=$8; c++} END {printf "%.1f", sum/c}')
AVG_FPS=$(railway logs --service slide-system --since 24h | grep "Actual FPS:" | \
  awk '{sum+=$6; c++} END {printf "%.2f", sum/c}')

echo "Avg Memory: ${AVG_MEM} MB" | tee -a daily_health.log
echo "Avg CPU: ${AVG_CPU}%" | tee -a daily_health.log
echo "Avg FPS: ${AVG_FPS}" | tee -a daily_health.log

# Errors
ERROR_COUNT=$(railway logs --service slide-system --since 24h | grep -ci "error")
echo "Total Errors (24h): $ERROR_COUNT" | tee -a daily_health.log

echo "" | tee -a daily_health.log
```

### Weekly Review (30 minutes)

**Run once per week**

```
Weekly Production Review

Week of: ________________

□ Performance Trends
  □ Memory usage trend: [ ] Stable  [ ] Increasing  [ ] Decreasing
  □ CPU usage trend: [ ] Stable  [ ] Increasing  [ ] Decreasing
  □ FPS trend: [ ] Stable  [ ] Degrading
  □ Error rate trend: [ ] Stable  [ ] Increasing  [ ] Decreasing

□ Reliability Metrics
  □ Total uptime: ______ % (Target: >99%)
  □ Spawn success rate: ______ % (Target: >99%)
  □ Crash rate: ______ crashes/day (Target: <0.5)
  □ Service restarts: ______ (Target: 0)

□ Capacity Planning
  □ Peak concurrent bots this week: ______
  □ Average concurrent bots: ______
  □ Approaching capacity? [ ] Yes  [ ] No
  □ Resource limit recommendations: __________________

□ Incidents & Issues
  □ Incidents this week: ______
  □ Mean time to resolution: ______ minutes
  □ Root causes identified: [ ] Yes  [ ] No
  □ Preventive actions taken: _________________________

□ Documentation Updates
  □ Runbook updates needed: [ ] Yes  [ ] No
  □ New issues to document: ___________________________
  □ Alert threshold adjustments: ______________________

□ Action Items for Next Week
  1. _______________________________________________
  2. _______________________________________________
  3. _______________________________________________
```

### Monthly Review (1 hour)

**Run once per month**

```
Monthly Production Review

Month: ________________

□ Overall Health
  □ Monthly uptime: ______ % (Target: >99.5%)
  □ Total bots spawned: ______
  □ Total crashes: ______ (Target: <10/month)

□ Performance Baselines
  □ Update memory baseline: ______ MB
  □ Update CPU baseline: ______ %
  □ Update FPS baseline: ______

□ Capacity & Scaling
  □ Current MAX_CONCURRENT_BOTS: ______
  □ Recommendation: [ ] Keep  [ ] Increase to ______  [ ] Decrease to ______
  □ Railway resource allocation: [ ] Sufficient  [ ] Needs increase

□ Monitoring Improvements
  □ New metrics to track: ____________________________
  □ Alert tuning needed: _____________________________
  □ Dashboard enhancements: __________________________

□ Team & Process
  □ Team training needs: _____________________________
  □ Runbook improvements: ____________________________
  □ Deployment process improvements: _________________

□ Next Month Goals
  1. _______________________________________________
  2. _______________________________________________
  3. _______________________________________________
```

---

## Alert Response Procedures

### Critical Alerts

**Alert: Service Down**

Response:
1. Check Railway dashboard immediately
2. Check logs for crash reason
3. Attempt restart: `railway restart --service slide-system`
4. If restart fails, rollback deployment
5. Page team lead if not resolved in 10 minutes

**Alert: Bot Spawn Failure Rate High (>10%)**

Response:
1. Check environment variables
2. Check Railway logs for spawn errors
3. Verify Daily API key valid
4. Check Supabase connectivity
5. If >50% failure rate, consider rollback

**Alert: OOM Kill**

Response:
1. Check memory usage in Railway dashboard
2. Kill all bots: `curl -X POST $API_URL/api/tavus/bot/kill-all`
3. Reduce MAX_CONCURRENT_BOTS temporarily
4. Investigate memory leak
5. Consider increasing Railway memory limit

### Warning Alerts

**Alert: High Memory Usage (>450 MB per bot)**

Response:
1. Monitor for 15 minutes
2. Check if memory increasing or stable
3. If increasing, investigate for memory leak
4. If stable, monitor closely
5. Consider bot restart if sustained

**Alert: Low Frame Rate (<9.0 FPS)**

Response:
1. Check CPU usage
2. Check frame capture time
3. Check network latency
4. Reduce bot count if necessary
5. Investigate performance bottleneck

---

## Monitoring Tools & Dashboards

### Railway Dashboard

**URL**: https://railway.app/project/YOUR_PROJECT_ID

**Monitor**:
- Service status
- Memory graph
- CPU graph
- Deployment history
- Recent restarts

### Custom Monitoring Script

Create automated monitoring dashboard:

```bash
#!/bin/bash
# monitoring_dashboard.sh

while true; do
  clear
  echo "╔══════════════════════════════════════════════════════════╗"
  echo "║     Screenshare Bot Production Monitoring Dashboard     ║"
  echo "╚══════════════════════════════════════════════════════════╝"
  echo ""
  echo "Last Updated: $(date)"
  echo ""

  # Service status
  echo "━━━ SERVICE STATUS ━━━"
  railway status --service slide-system | grep "Status\|Health\|CPU\|Memory"
  echo ""

  # Bot status
  echo "━━━ BOT STATUS ━━━"
  BOT_STATUS=$(curl -s "$API_URL/api/tavus/bot/status")
  echo "Active Bots: $(echo $BOT_STATUS | jq -r '.activeCount')"
  echo ""

  # Recent performance
  echo "━━━ RECENT PERFORMANCE ━━━"
  railway logs --service slide-system | grep "Performance -" | tail -3
  echo ""

  # Recent errors
  echo "━━━ ERROR COUNT (last 5 min) ━━━"
  ERROR_COUNT=$(railway logs --service slide-system --since 5m | grep -ci "error")
  echo "Errors: $ERROR_COUNT"
  echo ""

  # Recent crashes
  echo "━━━ CRASH COUNT (last 5 min) ━━━"
  CRASH_COUNT=$(railway logs --service slide-system --since 5m | grep "Bot process exited" | grep -cv "code: 0")
  echo "Crashes: $CRASH_COUNT"
  echo ""

  echo "Press Ctrl+C to exit"
  sleep 30
done
```

Run: `./monitoring_dashboard.sh`

---

## Quick Reference

### Essential Monitoring Commands

```bash
# Service status
railway status --service slide-system

# Active bots
curl -s "$API_URL/api/tavus/bot/status" | jq

# Recent performance
railway logs --service slide-system | grep "Performance -" | tail -10

# Recent errors
railway logs --service slide-system --since 30m | grep -i "error"

# Recent crashes
railway logs --service slide-system --since 30m | grep "Bot process exited" | grep -v "code: 0"

# Memory usage
railway logs --service slide-system | grep "Memory:" | tail -10

# CPU usage
railway logs --service slide-system | grep "CPU:" | tail -10

# Frame rate
railway logs --service slide-system | grep "Actual FPS:" | tail -10
```

### Monitoring Frequencies

| Period | Frequency | Duration |
|--------|-----------|----------|
| Initial deployment | Every 30 min | 4 hours |
| Phase 2 (3 bots) | Every 30 min | 4 hours |
| Phase 3 (5 bots, first 4h) | Every 30 min | 4 hours |
| Phase 3 (hours 4-12) | Every 1 hour | 8 hours |
| Phase 3 (hours 12-24) | Every 2 hours | 12 hours |
| Phase 3 (hours 24-48) | Every 4 hours | 24 hours |
| Daily checks | Once per day | Ongoing |
| Weekly review | Once per week | Ongoing |
| Monthly review | Once per month | Ongoing |

---

**Document Version**: 1.0
**Last Updated**: 2025-10-12
**Owner**: Stream C - Production Validation & Rollout
**Related Documents**: GRADUAL-ROLLOUT-PLAN.md, MONITORING.md, DEPLOYMENT-SIGN-OFF.md
