Skip to main content

๐Ÿ”ง Streaming Debug Guide

๐Ÿšจ Emergency Fixes Appliedโ€‹

Issue 1: React Hooks Order Error โœ… FIXEDโ€‹

Problem: "React has detected a change in the order of Hooks called by SessionContainer" Cause: WebSocket subscription hook was re-subscribing on every state change Fix:

  • Removed unstable dependencies from useSubscribeChatCompletion
  • Made message handler truly stable with only sessionId and queryClient dependencies
  • Removed problematic telemetry hooks that caused order changes

Issue 2: WebSocket JSON Parse Errors โœ… FIXEDโ€‹

Problem: SyntaxError: Unexpected token 'T', "This funct"... is not valid JSON Cause: SST dev environment sending non-JSON error messages to WebSocket Fix:

  • Added robust pre-parsing checks for JSON format
  • Graceful handling of SST infrastructure messages
  • Better error logging for actual parsing issues

Issue 3: Streaming Freeze After First Chunk ๐Ÿ”ง INVESTIGATINGโ€‹

Symptoms: Response freezes at "The world of cocktail", full response appears on page reload Suspected Causes:

  1. Server throttling too aggressive
  2. Client-side rendering bottlenecks
  3. WebSocket message loss during subscription changes

๐Ÿ” Diagnostic Tools Addedโ€‹

Client-Side Telemetryโ€‹

Console logs now show:

๐Ÿ”Œ [STREAMING] WebSocket subscription created with ID: abc123
๐Ÿš€ [STREAMING] First chunk received for subscription abc123
โšก [STREAMING] Chunk 10: 45ms since last (avg: 52ms)
๐Ÿ [STREAMING] Quest completed for subscription abc123:
- Total messages: 157
- Total time: 8,234ms
- Average interval: 52ms

Server-Side Telemetryโ€‹

Console logs now show:

โฑ๏ธ [STREAMING] Server throttling enabled: 5ms interval
๐Ÿ“ค [STREAMING] Chunk sent: +67 chars (total: 342), interval: 8ms
๐Ÿ“ค [STREAMING] Final message sent, content length: 1,847

๐Ÿงช Testing Environment Variablesโ€‹

Disable Server Throttling (Testing Only)โ€‹

export DISABLE_SERVER_THROTTLING=true

This removes ALL server-side throttling and sends every 5+ character change immediately to test if server throttling is causing client issues.

Development Optimizationsโ€‹

Already active in development:

  • Reduced throttling: 5ms (vs 25ms production)
  • Verbose logging enabled
  • History count reduced to 5 for faster processing

๐Ÿ•ต๏ธ Current Investigation Planโ€‹

Step 1: Test Server Throttlingโ€‹

  1. Set DISABLE_SERVER_THROTTLING=true
  2. Restart dev server
  3. Test streaming - does it still freeze?

Step 2: Monitor WebSocket Messagesโ€‹

  1. Open browser DevTools โ†’ Network โ†’ WS (WebSocket)
  2. Start streaming request
  3. Watch for:
    • Message frequency
    • Connection drops
    • Large gaps in timing

Step 3: Check React Renderingโ€‹

  1. Use React DevTools Profiler
  2. Monitor component re-renders during streaming
  3. Look for expensive render cycles

Step 4: Database Query Analysisโ€‹

  1. Check server logs for quest status queries
  2. Verify our database optimizations are working
  3. Look for any blocking operations

๐ŸŽฏ Success Metricsโ€‹

Before Fixes:

  • โŒ React hooks error breaking subscriptions
  • โŒ WebSocket JSON parse errors flooding console
  • โŒ Streaming freezes after first chunk
  • โŒ Full response only visible after page reload

After Fixes:

  • โœ… Clean WebSocket subscription lifecycle
  • โœ… Robust message parsing with SST compatibility
  • ๐Ÿ”ง Testing: Streaming should continue smoothly
  • ๐Ÿ”ง Testing: Real-time updates without reload needed

๐Ÿ“‹ Next Stepsโ€‹

  1. Test the fixes - Start a new streaming request
  2. Monitor console - Look for our new telemetry logs
  3. Check for errors - Should see no more hooks/JSON errors
  4. Verify streaming - Response should update in real-time

If streaming still freezes, we have comprehensive telemetry to identify exactly where the problem occurs!