Guard session queue recovery reentry

This commit is contained in:
OpenClaw committed 2026-03-07 13:45:18 +08:00
1 parent c5e85f1857
commit f62e23ffcc
3 files changed
+43 -8

No files matched your search

@@ -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 {
@@ -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<Map<String, String>>(emptyMap())
private val sessionSortKeyStore = mutableMapOf<String, Long>()
private val streamRecoveryQueueDrainPending = mutableSetOf<String>()
private val queueRecoveryInFlightSessionIds = mutableSetOf<String>()
private val queueRecoveryMarks = mutableMapOf<String, TimeMark>()
private val attachmentRetryStore = mutableMapOf<String, AttachmentRetryPayload>()
private val attachmentOpenStore = mutableMapOf<String, AttachmentOpenPayload>()
private val textRetryStore = mutableMapOf<String, TextRetryPayload>()
@@ -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) {
+1
View File
@@ -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 附件后端主链路