#!/usr/bin/env python3 """ Test script to demonstrate cross-process WebSocket emission issues. This simulates what happens when AI processing runs in separate asyncio context. """ import os import threading import time import asyncio from app import create_app from app.models.focus_group import emit_websocket_event def simulate_main_process(): """Simulate the main Flask/SocketIO process.""" print("=" * 50) print("SIMULATING MAIN FLASK/SOCKETIO PROCESS") print("=" * 50) # Create Flask app (this initializes WebSocket manager) app = create_app() main_pid = os.getpid() main_thread = threading.get_ident() print(f"๐Ÿ”Œ Main Process - PID: {main_pid}, Thread: {main_thread}") # Simulate WebSocket connection handling print("๐Ÿ”Œ WebSocket connections would be handled in this process/thread") return app def simulate_ai_processing(): """Simulate AI processing that emits WebSocket events.""" print("\n" + "=" * 50) print("SIMULATING AI PROCESSING (SEPARATE CONTEXT)") print("=" * 50) ai_pid = os.getpid() ai_thread = threading.get_ident() print(f"๐Ÿค– AI Process - PID: {ai_pid}, Thread: {ai_thread}") # Simulate emitting WebSocket events from AI processing test_focus_group_id = "test_fg_123" test_message_data = { "id": "msg_456", "content": "This is a test message from AI processing", "sender_name": "AI Assistant", "timestamp": time.time() } print(f"๐Ÿค– AI attempting to emit WebSocket event...") emit_websocket_event('message_update', test_focus_group_id, test_message_data) print(f"๐Ÿค– AI emit completed") async def simulate_async_ai_processing(): """Simulate asyncio-based AI processing (like AutonomousConversationController).""" print("\n" + "=" * 50) print("SIMULATING ASYNCIO AI PROCESSING") print("=" * 50) async_pid = os.getpid() async_thread = threading.get_ident() print(f"๐Ÿ”„ AsyncIO Process - PID: {async_pid}, Thread: {async_thread}") # Simulate async AI processing with WebSocket events test_focus_group_id = "test_fg_async_789" test_message_data = { "id": "msg_async_101", "content": "This is a test message from asyncio AI processing", "sender_name": "Async AI Assistant", "timestamp": time.time() } print(f"๐Ÿ”„ AsyncIO AI attempting to emit WebSocket event...") emit_websocket_event('message_update', test_focus_group_id, test_message_data) print(f"๐Ÿ”„ AsyncIO AI emit completed") # Simulate processing delay await asyncio.sleep(0.1) def main(): """Main test function.""" print("๐Ÿงช WEBSOCKET CROSS-PROCESS TEST") print("This test demonstrates potential cross-process WebSocket emission issues") print("identified by GPT-5 analysis.\n") # Step 1: Simulate main Flask/SocketIO process app = simulate_main_process() # Step 2: Simulate AI processing in same thread simulate_ai_processing() # Step 3: Simulate async AI processing print("\n๐Ÿ”„ Running asyncio AI simulation...") asyncio.run(simulate_async_ai_processing()) print("\n" + "=" * 50) print("TEST ANALYSIS") print("=" * 50) print("๐Ÿ‘€ Key observations to look for:") print("1. Are all PID/Thread IDs the same?") print("2. If different, this confirms cross-process emission issue") print("3. WebSocket events emitted from different process won't reach clients") print("4. This explains why AI mode messages don't appear in real-time") print("\nโœ… Cross-process test completed") if __name__ == "__main__": main()