Add attachment fingerprints for pending matching

This commit is contained in:
OpenClaw committed 2026-03-07 13:58:09 +08:00
1 parent 39f16a8119
commit 485afdab02
5 files changed
+24

No files matched your search

@@ -26,6 +26,7 @@ data class MessageCacheItem(
val toolCallId: String? = null, val toolCallId: String? = null,
val contentType: String? = null, val contentType: String? = null,
val sizeBytes: Int? = null, val sizeBytes: Int? = null,
val fingerprint: String? = null,
val retryable: Boolean = false, val retryable: Boolean = false,
) )
@@ -50,6 +51,7 @@ fun ChatMessage.toCacheItem(): MessageCacheItem = MessageCacheItem(
toolCallId = toolCallId, toolCallId = toolCallId,
contentType = contentType, contentType = contentType,
sizeBytes = sizeBytes, sizeBytes = sizeBytes,
fingerprint = attachmentFingerprint,
retryable = retryable, retryable = retryable,
) )
@@ -74,5 +76,6 @@ fun MessageCacheItem.toDomain(): ChatMessage = ChatMessage(
toolCallId = toolCallId, toolCallId = toolCallId,
contentType = contentType, contentType = contentType,
sizeBytes = sizeBytes, sizeBytes = sizeBytes,
attachmentFingerprint = fingerprint,
retryable = retryable, retryable = retryable,
) )
@@ -27,6 +27,7 @@ data class ChatMessage(
val toolCallId: String? = null, val toolCallId: String? = null,
val contentType: String? = null, val contentType: String? = null,
val sizeBytes: Int? = null, val sizeBytes: Int? = null,
val attachmentFingerprint: String? = null,
val retryable: Boolean = false, val retryable: Boolean = false,
) )
@@ -26,4 +26,5 @@ data class PendingSendItem(
val contentType: String? = null, val contentType: String? = null,
val base64Data: String? = null, val base64Data: String? = null,
val sizeBytes: Int? = null, val sizeBytes: Int? = null,
val fingerprint: String? = null,
) )
@@ -58,6 +58,7 @@ private data class PendingAttachmentSend(
val bytes: ByteArray, val bytes: ByteArray,
val contentType: String?, val contentType: String?,
val createdAt: String, val createdAt: String,
val fingerprint: String,
) )
internal data class PendingQueueRecoveryResult( internal data class PendingQueueRecoveryResult(
@@ -113,6 +114,14 @@ class RemoteOpenClawRepository(
private val autoRetryMessageIds = mutableSetOf<String>() private val autoRetryMessageIds = mutableSetOf<String>()
private var drainingSessionIds by mutableStateOf<Set<String>>(emptySet()) private var drainingSessionIds by mutableStateOf<Set<String>>(emptySet())
private fun attachmentFingerprint(fileName: String, bytes: ByteArray, contentType: String?): String {
var hash = 1125899907L
bytes.take(256).forEach { byte ->
hash = (hash * 1315423911L) xor (byte.toLong() and 0xff)
}
return listOf(fileName, contentType.orEmpty(), bytes.size.toString(), hash.toULong().toString(16)).joinToString("|")
}
override fun findSession(sessionId: String): SessionSummary? = sessions.firstOrNull { it.id == sessionId } override fun findSession(sessionId: String): SessionSummary? = sessions.firstOrNull { it.id == sessionId }
override fun messagesFor(sessionId: String): List<ChatMessage> = messageStore[sessionId].orEmpty() override fun messagesFor(sessionId: String): List<ChatMessage> = messageStore[sessionId].orEmpty()
@@ -173,6 +182,7 @@ class RemoteOpenClawRepository(
bytes = bytes, bytes = bytes,
contentType = item.contentType, contentType = item.contentType,
createdAt = item.createdAt, createdAt = item.createdAt,
fingerprint = item.fingerprint ?: attachmentFingerprint(item.fileName, bytes, item.contentType),
) )
attachmentRetryStore[item.messageId] = AttachmentRetryPayload( attachmentRetryStore[item.messageId] = AttachmentRetryPayload(
sessionId = item.sessionId, sessionId = item.sessionId,
@@ -293,6 +303,7 @@ class RemoteOpenClawRepository(
bytes = bytes, bytes = bytes,
contentType = contentType, contentType = contentType,
createdAt = "现在", createdAt = "现在",
fingerprint = attachmentFingerprint(fileName, bytes, contentType),
) )
val pending = ChatMessage( val pending = ChatMessage(
id = localId, id = localId,
@@ -303,6 +314,7 @@ class RemoteOpenClawRepository(
kind = MessageKind.Attachment, kind = MessageKind.Attachment,
contentType = contentType, contentType = contentType,
sizeBytes = bytes.size, sizeBytes = bytes.size,
attachmentFingerprint = attachmentFingerprint(fileName, bytes, contentType),
) )
messageStore = messageStore + (sessionId to (currentMessages + pending)) messageStore = messageStore + (sessionId to (currentMessages + pending))
updateSessionPreview(sessionId, pending) updateSessionPreview(sessionId, pending)
@@ -322,6 +334,7 @@ class RemoteOpenClawRepository(
kind = MessageKind.Attachment, kind = MessageKind.Attachment,
contentType = remote.contentType, contentType = remote.contentType,
sizeBytes = remote.sizeBytes, sizeBytes = remote.sizeBytes,
attachmentFingerprint = attachmentFingerprint(fileName, bytes, remote.contentType),
) )
attachmentRetryStore.remove(localId) attachmentRetryStore.remove(localId)
attachmentOpenStore.remove(localId) attachmentOpenStore.remove(localId)
@@ -340,6 +353,7 @@ class RemoteOpenClawRepository(
kind = MessageKind.Attachment, kind = MessageKind.Attachment,
contentType = contentType, contentType = contentType,
sizeBytes = bytes.size, sizeBytes = bytes.size,
attachmentFingerprint = attachmentFingerprint(fileName, bytes, contentType),
retryable = true, retryable = true,
) )
val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == localId) failed else msg } val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == localId) failed else msg }
@@ -382,6 +396,7 @@ class RemoteOpenClawRepository(
kind = MessageKind.Attachment, kind = MessageKind.Attachment,
contentType = remote.contentType, contentType = remote.contentType,
sizeBytes = remote.sizeBytes, sizeBytes = remote.sizeBytes,
attachmentFingerprint = payload.fingerprint,
) )
attachmentRetryStore.remove(messageId) attachmentRetryStore.remove(messageId)
attachmentOpenStore.remove(messageId) attachmentOpenStore.remove(messageId)
@@ -400,6 +415,7 @@ class RemoteOpenClawRepository(
kind = MessageKind.Attachment, kind = MessageKind.Attachment,
contentType = payload.contentType, contentType = payload.contentType,
sizeBytes = payload.bytes.size, sizeBytes = payload.bytes.size,
attachmentFingerprint = payload.fingerprint,
retryable = true, retryable = true,
) )
val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == messageId) failed else msg } val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == messageId) failed else msg }
@@ -468,6 +484,7 @@ class RemoteOpenClawRepository(
contentType = item.contentType, contentType = item.contentType,
base64Data = Base64.encode(item.bytes), base64Data = Base64.encode(item.bytes),
sizeBytes = item.bytes.size, sizeBytes = item.bytes.size,
fingerprint = item.fingerprint,
) )
} }
) )
@@ -674,6 +691,7 @@ class RemoteOpenClawRepository(
private fun matchesPendingLocalAttachment(local: ChatMessage, remote: ChatMessage): Boolean { private fun matchesPendingLocalAttachment(local: ChatMessage, remote: ChatMessage): Boolean {
return remote.kind == MessageKind.Attachment && return remote.kind == MessageKind.Attachment &&
remote.text == local.text && remote.text == local.text &&
(local.attachmentFingerprint == null || remote.attachmentFingerprint == null || remote.attachmentFingerprint == local.attachmentFingerprint) &&
(local.contentType == null || remote.contentType == local.contentType) && (local.contentType == null || remote.contentType == local.contentType) &&
(local.sizeBytes == null || remote.sizeBytes == local.sizeBytes) (local.sizeBytes == null || remote.sizeBytes == local.sizeBytes)
} }
+1
View File
@@ -211,6 +211,7 @@ Android/iOS App
- [x] 已给 session 级自动恢复补简易防重入/短节流:`openSession()` 和 SSE recovery 现在共用 `recoverPendingQueueForSession(...)`,避免短时间内对同一会话重复触发 queue recovery - [x] 已给 session 级自动恢复补简易防重入/短节流:`openSession()` 和 SSE recovery 现在共用 `recoverPendingQueueForSession(...)`,避免短时间内对同一会话重复触发 queue recovery
- [x] 已补 queue recovery 轻量可观测性:pending queue 状态里会暴露“正在恢复中 / 刚因节流跳过”,聊天页和反馈文案能区分“真在跑”和“刚跑过所以先跳过” - [x] 已补 queue recovery 轻量可观测性:pending queue 状态里会暴露“正在恢复中 / 刚因节流跳过”,聊天页和反馈文案能区分“真在跑”和“刚跑过所以先跳过”
- [x] 已开始收紧本地 pending 与远端消息的匹配条件:文本 pending 不再只看裸文本;附件 pending 除文件名外还会参考 `contentType/sizeBytes`,降低同文案/同文件名重发时的误合并风险 - [x] 已开始收紧本地 pending 与远端消息的匹配条件:文本 pending 不再只看裸文本;附件 pending 除文件名外还会参考 `contentType/sizeBytes`,降低同文案/同文件名重发时的误合并风险
- [x] 已给附件 pending 增加本地匹配指纹:基于 `fileName/contentType/sizeBytes/bytes 前缀 hash` 生成轻量 fingerprint,并随 pending queue / message cache 持久化,进一步降低同名附件重传时的误匹配概率
- [ ] 还没有更完整的网络恢复策略(目前先基于会话 SSE 恢复触发,不含更底层网络监听或全局后台队列调度) - [ ] 还没有更完整的网络恢复策略(目前先基于会话 SSE 恢复触发,不含更底层网络监听或全局后台队列调度)
### 4.2 附件后端主链路 ### 4.2 附件后端主链路