Retain pending queue states across snapshots

This commit is contained in:
OpenClaw committed 2026-03-07 13:15:00 +08:00
1 parent aa314ec084
commit 9c006b8ed1
2 files changed
+39 -11

No files matched your search

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