fix: recover from sleep/wake disconnection with connection state tracking and immediate health check (#940)
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.
This commit is contained in:
@@ -714,6 +714,25 @@ export const createOpenCodeLifecycleRuntime = (deps) => {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Perform an immediate (one-shot) health check and restart OpenCode if it's
|
||||
* not healthy. Callers on the SSE / WS proxy path use this to trigger
|
||||
* recovery without waiting for the next periodic interval (up to 15 s).
|
||||
*/
|
||||
const triggerHealthCheck = async () => {
|
||||
if (!state.openCodeProcess || state.isShuttingDown || state.isRestartingOpenCode) return;
|
||||
|
||||
try {
|
||||
const healthy = await isOpenCodeProcessHealthy();
|
||||
if (!healthy) {
|
||||
console.log('[lifecycle] immediate health check: OpenCode not healthy, restarting...');
|
||||
await restartOpenCode();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[lifecycle] immediate health check error: ${error.message}`);
|
||||
}
|
||||
};
|
||||
|
||||
const startHealthMonitoring = (healthCheckIntervalMs) => {
|
||||
if (state.healthCheckInterval) {
|
||||
clearInterval(state.healthCheckInterval);
|
||||
@@ -743,6 +762,7 @@ export const createOpenCodeLifecycleRuntime = (deps) => {
|
||||
refreshOpenCodeAfterConfigChange,
|
||||
bootstrapOpenCodeAtStartup,
|
||||
startHealthMonitoring,
|
||||
triggerHealthCheck,
|
||||
waitForPortRelease,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user