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) {
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<ChatMessage>,
localPending: 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 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<OverviewSessionSnapshot>) {
val total = items.size.toLong()
items.forEachIndexed { index, session ->