From f62e23ffcc5fbd6341fc6a467fbd829284b7bf72 Mon Sep 17 00:00:00 2001 From: OpenClaw Date: Sat, 7 Mar 2026 13:45:18 +0800 Subject: [PATCH] Guard session queue recovery reentry --- .../openclawapp/app/OpenClawAppState.kt | 8 ++-- .../data/RemoteOpenClawRepository.kt | 42 +++++++++++++++++-- docs/MVP_PROGRESS.md | 1 + 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt index 957d522..928042f 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt @@ -135,14 +135,12 @@ class OpenClawAppState( isSyncing = true runCatching { remote.refreshMessages(sessionId) - val autoRetried = remote.retryAutoRecoverableMessages(sessionId) - val drainedText = remote.drainPendingTextQueue(sessionId) - val drainedAttachments = remote.drainPendingAttachmentQueue(sessionId) + val recovery = remote.recoverPendingQueueForSession(sessionId, allowAutoRetry = true) persistRecentMessageCache(remote, preferredSessionId = sessionId) persistPendingSendQueue(remote) - if (autoRetried) { + if (recovery.autoRetried) { feedbackMessage = "已自动重试恢复的待发消息" - } else if (drainedText || drainedAttachments) { + } else if (recovery.drained) { feedbackMessage = "已尝试继续处理当前会话的待发队列" } }.onFailure { diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt index 35c4633..7b8e672 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt @@ -16,6 +16,9 @@ import cn.xiaobai.openclawapp.network.SessionStreamEvent import cn.xiaobai.openclawapp.network.SseSessionStream import cn.xiaobai.openclawapp.network.createDefaultHttpClient import kotlin.io.encoding.Base64 +import kotlin.time.Duration.Companion.seconds +import kotlin.time.TimeMark +import kotlin.time.TimeSource import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -57,6 +60,12 @@ private data class PendingAttachmentSend( val createdAt: String, ) +internal data class PendingQueueRecoveryResult( + val autoRetried: Boolean = false, + val drained: Boolean = false, + val skipped: Boolean = false, +) + class RemoteOpenClawRepository( private val config: ServerConfig, ) : OpenClawRepository { @@ -93,6 +102,8 @@ class RemoteOpenClawRepository( private var streamStatusStore by mutableStateOf>(emptyMap()) private val sessionSortKeyStore = mutableMapOf() private val streamRecoveryQueueDrainPending = mutableSetOf() + private val queueRecoveryInFlightSessionIds = mutableSetOf() + private val queueRecoveryMarks = mutableMapOf() private val attachmentRetryStore = mutableMapOf() private val attachmentOpenStore = mutableMapOf() private val textRetryStore = mutableMapOf() @@ -609,6 +620,33 @@ class RemoteOpenClawRepository( } } + internal suspend fun recoverPendingQueueForSession( + sessionId: String, + allowAutoRetry: Boolean = true, + ): PendingQueueRecoveryResult { + if (sessionId in queueRecoveryInFlightSessionIds) { + return PendingQueueRecoveryResult(skipped = true) + } + val lastAttempt = queueRecoveryMarks[sessionId] + if (lastAttempt != null && lastAttempt.elapsedNow() < 3.seconds) { + return PendingQueueRecoveryResult(skipped = true) + } + + queueRecoveryInFlightSessionIds.add(sessionId) + queueRecoveryMarks[sessionId] = TimeSource.Monotonic.markNow() + try { + val autoRetried = if (allowAutoRetry) retryAutoRecoverableMessages(sessionId) else false + val drainedText = drainPendingTextQueue(sessionId) + val drainedAttachments = drainPendingAttachmentQueue(sessionId) + return PendingQueueRecoveryResult( + autoRetried = autoRetried, + drained = drainedText || drainedAttachments, + ) + } finally { + queueRecoveryInFlightSessionIds.remove(sessionId) + } + } + private fun clearPendingTextMessage(sessionId: String, text: String) { val targetId = pendingTextQueue.values .filter { it.sessionId == sessionId && it.text == text } @@ -630,9 +668,7 @@ class RemoteOpenClawRepository( streamStatusStore = streamStatusStore + (sessionId to "live") syncSessionStreamStatus(sessionId) if (streamRecoveryQueueDrainPending.remove(sessionId)) { - runCatching { retryAutoRecoverableMessages(sessionId) } - runCatching { drainPendingTextQueue(sessionId) } - runCatching { drainPendingAttachmentQueue(sessionId) } + runCatching { recoverPendingQueueForSession(sessionId, allowAutoRetry = true) } } sseStream.connect(sessionId).collect { event -> when (event) { diff --git a/docs/MVP_PROGRESS.md b/docs/MVP_PROGRESS.md index a19e813..34bb992 100644 --- a/docs/MVP_PROGRESS.md +++ b/docs/MVP_PROGRESS.md @@ -208,6 +208,7 @@ Android/iOS App - [x] 又收了一轮边角判断:文本入队去重、auto-retry 候选过滤、本地 retainable/failure 判定开始复用派生状态辅助函数,继续减少裸 `status == ...` 分支 - [ ] 还没有完整的离线发送队列(当前已覆盖文本 + 附件,但还没有更完整的网络感知、后台 drain 与统一淘汰策略) - [x] 已补第一版“网络恢复后继续处理待发队列”:当会话 SSE 从 recoverable failed / reconnecting 回到 live 时,会顺手尝试 auto-retry + drain 当前会话的文本/附件 pending +- [x] 已给 session 级自动恢复补简易防重入/短节流:`openSession()` 和 SSE recovery 现在共用 `recoverPendingQueueForSession(...)`,避免短时间内对同一会话重复触发 queue recovery - [ ] 还没有更完整的网络恢复策略(目前先基于会话 SSE 恢复触发,不含更底层网络监听或全局后台队列调度) ### 4.2 附件后端主链路