Implement the following plan: Fix: WebSocket reconnect storm — 重构 useSessionStream 依赖关系…
- User
Implement the following plan:
Fix: WebSocket reconnect storm — 重构 useSessionStream 依赖关系
Context
Web UI 切换 session 时出现 WebSocket 快速重连风暴(每秒数十次 open/close)。根因是 PR #1359 (
416bc1b) 为resetState的useCallback依赖数组新增了slashCommands.length,导致resetState回调引用不稳定,触发useLayoutEffect循环执行。问题分析:当前完整依赖链
关键函数依赖关系(当前代码)
coderesetStepState deps: [] ✅ 稳定 └─ 调用者: resetState, processEvent setMessages deps: [] ✅ 稳定 setAwaitingFirstResponse deps: [] ✅ 稳定 sendInitialize deps: [] ✅ 稳定 completeStreamingMessages deps: [setMessages] ✅ 稳定 resetState deps: [resetStepState, setAwaitingFirstResponse, slashCommands.length] ❌ 不稳定 └─ 调用者: connect, clearMessages, useLayoutEffect applySessionStatus deps: [completeStreamingMessages, normalizeSessionStatus, onSessionStatus, setAwaitingFirstResponse, onFirstTurnComplete] ⚠️ 视 props 而定 handleMessage deps: [processEvent, onError, setAwaitingFirstResponse, applySessionStatus, completeStreamingMessages, sendInitialize] ⚠️ 视 processEvent 而定 sendPendingMessage deps: [setAwaitingFirstResponse] ✅ 稳定 connect deps: [sessionId, resetState, setMessages, getWebSocketUrl, handleMessage, onError, sendInitialize, sendPendingMessage, setAwaitingFirstResponse, completeStreamingMessages] ❌ 不稳定 (← resetState) disconnect deps: [completeStreamingMessages, setAwaitingFirstResponse, setMessages] ✅ 稳定 cancel deps: [status, disconnect, setAwaitingFirstResponse, setMessages] ⚠️ status 变化 reconnect deps: [disconnect, connect] ❌ 不稳定 (← connect) clearMessages deps: [setMessages, resetState] ❌ 不稳定 (← resetState) useLayoutEffect (auto-connect) deps: [sessionId, resetState, setMessages] ❌ 不稳定 (← resetState) Assistant
Let me find the specific code sections I need to modify.
Assistant
Now let me find where
setSlashCommandsis called inhandleMessage.Assistant
Now I have all the context. Let me make the changes.
Assistant
Step 1: Add
slashCommandsLenRefnear the other refs (after line 329).Assistant
Step 2: Add
resetStateRefnear the other refs (after connectRef/disconnectRef/reconnectRef).Assistant
Step 3: Fix
resetState— useslashCommandsLenRefinstead ofslashCommands.length, and remove from deps.Assistant
Step 4: Update
setSlashCommandscall inhandleMessageto sync the ref.Assistant
Step 5: Sync
resetStateRefalongside the other refs.Assistant
Step 6: Update
clearMessagesto useresetStateRef.Assistant
Step 7: Update
useLayoutEffectto useresetStateRefand only depend on[sessionId].Assistant
Now let's verify with biome and tsc.
Assistant
Biome wants
setMessagesin the deps. SincesetMessagesis fromuseStateand is stable, adding it back is safe and won't cause the issue.Assistant
Both biome and tsc pass clean.
Assistant
All changes applied successfully. Here's a summary:
Changes in
web/src/hooks/useSessionStream.ts:- Added
slashCommandsLenRef(line ~330) — tracksslashCommandslength via ref instead of state dependency - Added
resetStateRef(line ~311) — allowsuseLayoutEffectandclearMessagesto callresetStatewithout depending on its identity resetState— replacedslashCommands.length > 0withslashCommandsLenRef.current > 0, addedslashCommandsLenRef.current = 0on clear, removedslashCommands.lengthfrom depshandleMessage— addedslashCommandsLenRef.current = slash_commands.lengthwhen setting slash commands- Ref sync block — added
resetStateRef.current = resetState clearMessages— usesresetStateRef.current()instead ofresetState, deps reduced to
- Added
- User
深度review一下这个实现有什么其他可能的问题
Assistant
Let me re-read the full context around the changes and related code paths.
Assistant
深度审查一下完整的改动和周边代码。
Assistant