When the managed OpenCode process exits but a server survives on the old
port (Windows: killProcessOnPort is a no-op, so the orphaned process tree
keeps the port), restartOpenCode() times out waiting for the port and
spawns a fresh server on a NEW port. HTTP/proxy traffic follows the new
port, but the global message-stream hub's upstream SSE reader stays pinned
to the old server's /global/event stream — that connection never closes —
so new events never reach the UI and the chat stops updating until the
app is restarted (#2638).
Lifecycle now fires an optional onOpenCodeRestarted hook after a
successful managed restart; index.js wires it to the new
messageStreamRuntime.rebindUpstream(), which restarts the shared hub
(its reader re-dials buildOpenCodeUrl → the current port) and closes
directory-scoped sockets so their per-connection readers rebuild against
the new port. External servers are untouched (their port cannot change).
Fixes#2638
Recover stalled event streams without dropping the session
Wait briefly for reconnection before showing connection lost errors
Persist Electron server logs for easier disconnect debugging
When the computer sleeps and wakes, the SSE/WS event stream drops
silently. Messages appeared sent (optimistic insert) but never reached
the OpenCode server, and the user had no indication the system was
disconnected.
Three fixes:
1. Connection state tracking: add onDisconnect callback to the event
pipeline. Stream failures set isConnected=false in useConfigStore;
successful reconnect sets isConnected=true.
2. Send guard: optimisticSend, respondToPermission, and
respondToQuestion now check isConnected before making API calls,
throwing a clear error that surfaces as a toast to the user.
The /compact command also checks connection with error feedback.
3. Faster server recovery: add triggerHealthCheck() to the server
lifecycle and wire it into the WS event stream runtime. When the
upstream OpenCode connection fails, the server immediately checks
health and restarts if needed, instead of waiting up to 15s for
the periodic health check.