@@ -18,6 +18,7 @@ import kotlinx.coroutines.CancellationException
18import kotlinx.coroutines.ExperimentalCoroutinesApi
18import kotlinx.coroutines.ExperimentalCoroutinesApi
19import kotlinx.coroutines.Job
19import kotlinx.coroutines.Job
20import kotlinx.coroutines.channels.BufferOverflow
20import kotlinx.coroutines.channels.BufferOverflow
21+import kotlinx.coroutines.delay
21import kotlinx.coroutines.flow.MutableSharedFlow
22import kotlinx.coroutines.flow.MutableSharedFlow
22import kotlinx.coroutines.flow.MutableStateFlow
23import kotlinx.coroutines.flow.MutableStateFlow
23import kotlinx.coroutines.flow.SharedFlow
24import kotlinx.coroutines.flow.SharedFlow
@@ -228,7 +229,9 @@ class SessionListViewModel(private val container: AppContainer) : ViewModel() {
229 * Reload the preview for a single session (used by the live SSE handler).
230 * Reload the preview for a single session (used by the live SSE handler).
230 * Cancels any prior in-flight load for the same session so a burst of
231 * Cancels any prior in-flight load for the same session so a burst of
231− * [SessionUpdated] events coalesces into a single request.
232+ * [SessionUpdated] events coalesces into a single request. A short debounce
233+ * delay before the fetch avoids launching one full-history download per event
234+ * during active streaming — the fetch runs only after events quiet down.
233 private fun loadPreview(sessionId: String) {
236 private fun loadPreview(sessionId: String) {
234 val conn = container.activeConnection.value ?: return
237 val conn = container.activeConnection.value ?: return
@@ -238,6 +241,7 @@ class SessionListViewModel(private val container: AppContainer) : ViewModel() {
238 livePreviewJobs[sessionId] = viewModelScope.launch {
241 livePreviewJobs[sessionId] = viewModelScope.launch {
239 val job = coroutineContext[kotlinx.coroutines.Job]
242 val job = coroutineContext[kotlinx.coroutines.Job]
244+ delay(NetworkConfig.previewDebounceMs)
241 val preview = fetchSessionPreview(api, sessionId) ?: return@launch
245 val preview = fetchSessionPreview(api, sessionId) ?: return@launch
242 _state.update { s -> s.copy(previews = s.previews + (sessionId to preview)) }
246 _state.update { s -> s.copy(previews = s.previews + (sessionId to preview)) }
243 } catch (e: CancellationException) {
247 } catch (e: CancellationException) {