From 9c006b8ed133ff3016ae2d66de578d5517cbdf2b Mon Sep 17 00:00:00 2001 From: OpenClaw Date: Sat, 7 Mar 2026 13:15:00 +0800 Subject: [PATCH] Retain pending queue states across snapshots --- .../data/RemoteOpenClawRepository.kt | 49 ++++++++++++++----- docs/MVP_PROGRESS.md | 1 + 2 files changed, 39 insertions(+), 11 deletions(-) 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 c9de732..b5e9142 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt @@ -328,12 +328,15 @@ class RemoteOpenClawRepository( } override suspend fun retryAttachment(sessionId: String, messageId: String) { + retryAttachmentInternal(sessionId, messageId, fromQueueDrain = false) + } + + private suspend fun retryAttachmentInternal(sessionId: String, messageId: String, fromQueueDrain: Boolean) { val payload = attachmentRetryStore[messageId] ?: return val current = messageStore[sessionId].orEmpty() - val isQueueDrain = messageId in pendingAttachmentQueue val uploading = current.map { msg -> if (msg.id == messageId) msg.copy( - status = if (isQueueDrain) "queue-draining" else "uploading", + status = if (fromQueueDrain) "queue-draining" else "uploading", retryable = false, createdAt = "现在", ) else msg @@ -585,7 +588,7 @@ class RemoteOpenClawRepository( val payload = pendingAttachmentQueue[messageId] ?: continue drained = true runCatching { - retryAttachment(payload.sessionId, messageId) + retryAttachmentInternal(payload.sessionId, messageId, fromQueueDrain = true) }.onFailure { return drained } @@ -842,8 +845,11 @@ class RemoteOpenClawRepository( autoRetryMessageIds.remove(domain.id) domain.copy(retryable = true) } - domain.kind == MessageKind.Attachment && domain.status == "uploading" -> { - domain.copy(status = "upload-failed", retryable = true) + domain.kind == MessageKind.Attachment && (domain.status == "uploading" || domain.status == "queue-draining" || domain.status == "upload-failed") -> { + domain.copy( + status = "upload-failed", + retryable = attachmentRetryStore.containsKey(domain.id) || pendingAttachmentQueue.containsKey(domain.id), + ) } else -> domain } @@ -853,13 +859,13 @@ class RemoteOpenClawRepository( val localMessages = messageStore[sessionId].orEmpty().filter { it.id.startsWith("local-") } return localMessages.filter { local -> when { - local.kind == MessageKind.Text && local.role == MessageRole.User && local.status == "send-failed" -> { + local.isRetainableLocalTextMessage() -> { textRetryStore.putIfAbsent(local.id, TextRetryPayload(sessionId, local.text)) remoteMessages.none { remote -> remote.role == MessageRole.User && remote.text == local.text } } - local.kind == MessageKind.Attachment && local.status == "upload-failed" -> { + local.isRetainableLocalAttachmentMessage() -> { remoteMessages.none { remote -> remote.kind == MessageKind.Attachment && remote.text == local.text } @@ -873,7 +879,9 @@ class RemoteOpenClawRepository( remoteMessages: List, localPending: List, ): List { - val pendingUsers = localPending.filter { it.role == MessageRole.User && it.status == "queued" } + val pendingUsers = localPending.filter { it.isRetainableLocalTextMessage() } + val activePendingUsers = pendingUsers.filter { it.status != "send-failed" } + val pendingAttachments = localPending.filter { it.isRetainableLocalAttachmentMessage() } val pendingThinking = localPending.filter { it.id.startsWith("local-thinking-") } val unmatchedPendingUsers = pendingUsers.filter { pending -> @@ -881,25 +889,31 @@ class RemoteOpenClawRepository( remote.role == MessageRole.User && remote.text == pending.text } } + val unmatchedPendingAttachments = pendingAttachments.filter { pending -> + remoteMessages.none { remote -> + remote.kind == MessageKind.Attachment && remote.text == pending.text + } + } - val hasPendingUserReachedRemote = pendingUsers.any { pending -> + val hasPendingUserReachedRemote = activePendingUsers.any { pending -> remoteMessages.any { remote -> remote.role == MessageRole.User && remote.text == pending.text } } val shouldKeepThinking = pendingThinking.isNotEmpty() && ( - unmatchedPendingUsers.isNotEmpty() || + unmatchedPendingUsers.any { it.status != "send-failed" } || (hasPendingUserReachedRemote && remoteMessages.lastOrNull()?.role != MessageRole.Assistant) ) return buildList { addAll(remoteMessages) addAll(unmatchedPendingUsers) + addAll(unmatchedPendingAttachments) if (shouldKeepThinking) { addAll(pendingThinking) } }.onEach { msg -> - if (msg.role == MessageRole.User && msg.status != "send-failed") { + if (!msg.id.startsWith("local-") && msg.role == MessageRole.User && msg.status != "send-failed") { textRetryStore.remove(msg.id) } } @@ -912,6 +926,19 @@ class RemoteOpenClawRepository( } } + private fun ChatMessage.isRetainableLocalTextMessage(): Boolean { + return id.startsWith("local-") && + role == MessageRole.User && + kind == MessageKind.Text && + status in setOf("queued", "queue-draining", "retrying-auto", "send-failed") + } + + private fun ChatMessage.isRetainableLocalAttachmentMessage(): Boolean { + return id.startsWith("local-") && + kind == MessageKind.Attachment && + status in setOf("queue-draining", "uploading", "upload-failed") + } + private fun rebuildSessionSortKeysFromSnapshot(items: List) { val total = items.size.toLong() items.forEachIndexed { index, session -> diff --git a/docs/MVP_PROGRESS.md b/docs/MVP_PROGRESS.md index e14b3c0..fb51fcf 100644 --- a/docs/MVP_PROGRESS.md +++ b/docs/MVP_PROGRESS.md @@ -201,6 +201,7 @@ Android/iOS App - [x] pending queue 已开始同时覆盖文本与附件:附件待上传项也会独立持久化,并在重新进入会话或手动同步时继续尝试上传 - [x] 附件上传状态已开始对齐队列语义:队列自动处理中的附件会显示“队列上传中”,与手动上传中的“上传中”分开 - [x] 已修复 `RemoteOpenClawRepository` 的 JVM 重载撞签名:原先两个 `rebuildSessionSortKeys(List<...>)` 因泛型擦除冲突,现已拆成 `rebuildSessionSortKeysFromSnapshot(...)` / `rebuildSessionSortKeysFromSessions(...)` +- [x] 已补本地 pending 保留逻辑:refresh / SSE snapshot 不再只保留 `queued` 文本;`queue-draining` / `retrying-auto` / 本地附件 pending 也会保留,避免队列处理中消息被快照冲掉 - [ ] 还没有完整的离线发送队列(当前已覆盖文本 + 附件,但还没有更完整的网络感知、后台 drain 与统一淘汰策略) - [ ] 还没有真正基于网络恢复信号的自动补发