fix(chat): reject stale dispatch after session switch

This commit is contained in:
M57
2026-09-16 10:48:08 +03:00
parent 22b948f671
commit f94d1784d4
2 changed files with 4 additions and 8 deletions
@@ -1807,6 +1807,8 @@ class ChatViewModel(
if (text.isNotBlank()) "\n\n$text" else "" if (text.isNotBlank()) "\n\n$text" else ""
} }
if (dispatchGeneration != sessionGeneration) return@launch
// While a turn is actively streaming and this is a plain text prompt // While a turn is actively streaming and this is a plain text prompt
// (no attachments — session.redirect carries text only), steer the // (no attachments — session.redirect carries text only), steer the
// in-flight turn via session.redirect instead of queueing a fresh // in-flight turn via session.redirect instead of queueing a fresh
@@ -2110,7 +2110,7 @@ class ChatViewModelTest {
viewModel.sendMessage("Send in the old session") viewModel.sendMessage("Send in the old session")
advanceUntilIdle() advanceUntilIdle()
verify { verify(exactly = 0) {
HermesWsClient.sendMessage( HermesWsClient.sendMessage(
oldSessionId, oldSessionId,
match { it.contains("Send in the old session") }, match { it.contains("Send in the old session") },
@@ -2118,13 +2118,7 @@ class ChatViewModelTest {
any(), any(),
) )
} }
mockEventsFlow.emit( assertFalse(viewModel.streamingState.value.turnUsageBaselineCaptured)
WsEvent.RpcError(
promptRequestId,
JsonRpcError(code = 4001, message = "old session rejected prompt"),
),
)
advanceUntilIdle()
val state = viewModel.uiState.value val state = viewModel.uiState.value
assertEquals("session-456", state.currentSessionId) assertEquals("session-456", state.currentSessionId)