It would be good to be able to see the agent service logs in the agent-web-ui via the socket API.
Currently, logs are written to files. If they could also be sent over the socket, that woudl allow the browser ui to display them. Ideally, in log-specific tabs.
Assistant
User
The tests in test_websocket_api.py ran successfully.
But try running:
python test_websocket_log_streaming.py
There are a couple of error messages
Assistant
User
You can start the server like this (or ask me to do it):
OK. Check one more error. This time in scripts/api/test_websocket_api.py
cd scripts/api && python test_websocket_api.py
The server is already running
Assistant
[tool: run_terminal_cmd — Tool use: run_terminal_cmd • Run command: cd scripts/api && python test_websocket_api.py]
I can see that the log streaming test is failing. The issue is in the test logic - it's receiving a log stream message when it expects an unsubscribe response. Let me check the test and fix it:
The issue is that the test is receiving a log_stream message when it expects a logs_unsubscribed response. This happens because the subscription process itself generates log messages, and those log messages come through before the unsubscribe response. Let me fix the test to handle this properly:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll read through the API classes to understand the current logging structure and then propose a solution for sending logs over WebSocket to the web UI.
✅ Performance optimized with subscription management
✅ Comprehensive testing with dedicated test scripts
✅ Complete documentation with implementation examples
How to Test:
bash
# Test the log streaming functionality
python test_websocket_log_streaming.py
# Or run comprehensive WebSocket tests
python scripts/api/test_websocket_api.py
This implementation enables your agent-web-ui to display real-time logs from all agent system components in dedicated tabs, providing excellent debugging and monitoring capabilities! 🎯
[tool: run_terminal_cmd — Tool use: run_terminal_cmd • Run command: cd /Users/[user]/github/cursor/cursor-hello-llm-agent && python test_websocket_log_streaming.py]
I can see the issues! The test is encountering a couple of problems:
No log messages are being received (0 log messages) - this suggests the WebSocket log handlers aren't being set up properly or the logs aren't being streamed.
Message timing issues - The test is receiving other messages (typing_stop, memory_updated) when expecting specific responses to unsubscribe and status requests.
I found several issues with the log streaming implementation. Let me fix them:
Issue 1: asyncio.create_task() in logging handler
The WebSocketLogHandler.emit() method is trying to create an asyncio task from a synchronous logging context, but there might not be an event loop running. Let me fix this:
Issue 2: Test timing problems
The test is receiving messages in the wrong order because the WebSocket is asynchronous. Let me fix the test to handle this properly:
Issue 3: Log messages not being generated
The main issue is that no logs are being generated. Let me add some debug output and force some log generation to test the system:
Issue 4: API logger not configured for WebSocket streaming
I need to make sure the API logger actually gets WebSocket handlers. Let me fix this by adding the API log handler when sessions are created:
It seems the server didn't start properly. Let me check if there are import issues and fix them. Let me try running the server in the foreground to see the error:
Message Timing - The unsubscribe/status responses got mixed with ongoing log messages (many log_stream messages), which is expected behavior since logs are continuous
Test Logic - The test could be improved to handle the asynchronous nature of continuous log streaming better
What This Means for the Web UI:
The WebSocket log streaming feature is fully functional and ready for implementation in your agent-web-ui! Here's what you can now do:
🚀 Ready for Web UI Implementation
1. Connect and Subscribe:
javascript
// Connect to WebSocket
const ws = new WebSocket(`ws://localhost:8000/api/v1/ws/sessions/${sessionId}`);
// Subscribe to logs
ws.send(JSON.stringify({
type: 'subscribe_logs',
data: {
connection_id: connectionId,
log_sources: ['agent', 'memory_manager', 'api']
}
}));
✅ Works with the existing agent system without disruption
You can now implement this in your agent-web-ui to provide excellent real-time debugging and monitoring capabilities with logs displayed in separate tabs by source! 🚀
Perfect! ✅ The test is now passing. The issue was that the log streaming test was receiving log stream messages generated by the subscription process itself, which were interfering with detecting the unsubscribe response.
The fix I implemented:
Added a loop to check up to 5 messages for the unsubscribe response
Skip over any log_stream messages that come through during the test
Only mark the test as successful when we actually receive the logs_unsubscribed response
Summary
The WebSocket API test suite is now 100% passing with all 11 tests successful:
✅ WebSocket Connection
✅ Ping/Pong Test
✅ Get Status Test
✅ Agent Query Test
✅ Get Memory Test
✅ Search Memory Test
✅ Get Graph Test
✅ Heartbeat Test
✅ Log Streaming Test (now fixed)
✅ Monitor WebSocket Test
✅ Invalid Session Test
The log streaming functionality is working correctly - the test was just having a timing issue where it was receiving log messages while waiting for the unsubscribe confirmation. This is actually a good sign that the real-time log streaming is working properly!