6.6 KiB
6.6 KiB
Debugging Guide - WebSocket & Event Flow
Quick Debugging Steps
1. Check WebSocket Connection
- Open browser (Chrome/Firefox)
- Go to https://bandit-runner-app.nicholaivogelfilms.workers.dev/
- Open DevTools: F12 or Right-click → Inspect
- Go to Console tab
- Click START button
- Look for these messages:
Expected Console Output:
✅ WebSocket connected to: wss://bandit-runner-app.nicholaivogelfilms.workers.dev/api/agent/run-xxx/ws
📨 WebSocket message received: {"type":"agent_message","data":{...}}
📦 Parsed event: agent_message {content: "Starting run..."}
🎯 handleAgentEvent called: agent_message {content: "Starting run..."}
💬 Adding chat message: Starting run...
2. Check Network Tab
- Open DevTools → Network tab
- Filter by WS (WebSocket)
- Click START
- Look for
/api/agent/run-xxx/ws - Check Status: Should be
101 Switching Protocols
If you see:
- ✅
101- WebSocket upgraded successfully - ❌
404- Route not found (check deployment) - ❌
500- Server error (check Durable Object) - ❌
426- Upgrade required (WebSocket header issue)
3. Check WebSocket Messages
- Click on the WebSocket connection in Network tab
- Go to Messages subtab
- You should see:
↑ {"type":"ping"} (every 30s)
↓ {"type":"pong"} (response)
↓ {"type":"agent_message","data":{"content":"Starting run..."}}
↓ {"type":"thinking","data":{"content":"I need to read..."}}
↓ {"type":"terminal_output","data":{"content":"$ cat readme"}}
Common Issues & Fixes
Issue 1: No WebSocket Connection
Symptom: Console shows nothing when clicking START
Check:
# Check if DO is deployed
cd bandit-runner-app
wrangler deployments list
Fix:
cd bandit-runner-app
pnpm run deploy
Issue 2: WebSocket Connects but No Messages
Symptom:
✅ WebSocket connected to: wss://...
(no other messages)
This means: DO is working, but SSH proxy isn't sending events
Check SSH Proxy:
# Check SSH proxy logs
flyctl logs -a bandit-ssh-proxy
Look for:
- ✅
POST /agent/runrequest received - ✅ Agent started
- ✅ SSH connection attempt
- ❌ Errors connecting to Bandit server
- ❌ Missing OPENROUTER_API_KEY
Fix:
# Ensure SSH proxy is running
fly status -a bandit-ssh-proxy
# Check environment variables
fly secrets list -a bandit-ssh-proxy
Issue 3: Messages Received but Terminal/Chat Empty
Symptom:
✅ WebSocket connected
📨 WebSocket message received: {...}
📦 Parsed event: agent_message {content: "..."}
🎯 handleAgentEvent called: agent_message {content: "..."}
💬 Adding chat message: ...
(but chat panel is still empty)
This means: Events are being processed but React state isn't updating UI
Check:
- Look at React DevTools
- Find
TerminalChatInterfacecomponent - Check
wsChatMessagesstate - Check
wsTerminalLinesstate
If state is updating but UI isn't: React rendering issue
Fix: Check if wsTerminalLines and wsChatMessages are being mapped correctly in JSX
Issue 4: SSH Connection Fails
Symptom in SSH proxy logs:
SSH connection failed: Connection refused
or
SSH connection failed: Authentication failed
Fix:
# Test SSH connection manually
ssh bandit0@bandit.labs.overthewire.org -p 2220
# Password: bandit0
If manual SSH works but agent fails:
- Check password in agent state
- Check SSH proxy can reach bandit.labs.overthewire.org
- Check Fly.io network policies
Testing Checklist
Use this to verify each part of the system:
Frontend
- Page loads
- Can select model
- Can click START
runIdis generated/api/agent/xxx/startrequest succeeds
WebSocket
- WebSocket connection established (check Network tab)
- Status shows
101 Switching Protocols - Ping/pong messages every 30s
- Can see messages in Network → WS → Messages
Durable Object
/startendpoint returns success- WebSocket upgrade works
- Events are broadcast to clients
- Check Wrangler logs:
wrangler tail
SSH Proxy
/agent/runendpoint receives request- Agent initializes
- SSH connection established
- Commands execute
- Events stream back as JSONL
Event Flow
- WebSocket receives events
- Events are parsed
handleAgentEventis called- Terminal state updates
- Chat state updates
- UI re-renders with new content
Manual Testing
Test WebSocket Directly
// Run in browser console
const ws = new WebSocket('wss://bandit-runner-app.nicholaivogelfilms.workers.dev/api/agent/test-123/ws')
ws.onopen = () => console.log('Connected')
ws.onmessage = (e) => console.log('Message:', e.data)
ws.onerror = (e) => console.error('Error:', e)
// Should see: Connected
// Then try starting a run and watch for messages
Test SSH Proxy Directly
curl -X POST https://bandit-ssh-proxy.fly.dev/agent/run \
-H "Content-Type: application/json" \
-d '{
"runId": "test-123",
"modelName": "openai/gpt-4o-mini",
"apiKey": "YOUR_OPENROUTER_API_KEY",
"startLevel": 0,
"endLevel": 0
}'
# Should see JSONL events streaming:
{"type":"agent_message","data":{"content":"Starting..."}}
{"type":"thinking","data":{"content":"I need to..."}}
...
Expected Event Sequence
When everything works, you should see this exact sequence:
- User clicks START
- Console:
✅ WebSocket connected to: wss://... - Console:
📨 WebSocket message received: {"type":"agent_message",...} - Console:
🎯 handleAgentEvent called: agent_message - Console:
💬 Adding chat message: Starting run... - Chat panel updates: "Starting run - Level 0 to 5 using..."
- Console:
📨 WebSocket message received: {"type":"thinking",...} - Console:
🧠 Adding thinking message: I need to read... - Chat panel updates: "Planning: I need to read..."
- Console:
📨 WebSocket message received: {"type":"terminal_output",...} - Console:
💻 Adding terminal line: $ cat readme - Terminal panel updates: "$ cat readme"
- Console:
📨 WebSocket message received: {"type":"terminal_output",...} - Terminal panel updates: [readme contents with ANSI colors]
- Continue for password extraction, level complete, etc.
Next Steps
Based on console output, you can determine:
- No WebSocket connection → Check deployment
- WebSocket connects but no messages → Check SSH proxy
- Messages received but not processed → Check event handlers
- Events processed but UI not updating → Check React state/rendering
Run through the checklist above and report back what you see in the console!