From 485afdab02ddfce134ce85dd69550da8eea117fc Mon Sep 17 00:00:00 2001 From: OpenClaw Date: Sat, 7 Mar 2026 13:58:09 +0800 Subject: [PATCH] Add attachment fingerprints for pending matching --- .../openclawapp/data/MessageCacheSnapshot.kt | 3 +++ .../cn/xiaobai/openclawapp/data/Models.kt | 1 + .../openclawapp/data/PendingSendQueue.kt | 1 + .../data/RemoteOpenClawRepository.kt | 18 ++++++++++++++++++ docs/MVP_PROGRESS.md | 1 + 5 files changed, 24 insertions(+) diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/MessageCacheSnapshot.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/MessageCacheSnapshot.kt index dc7f913..71c1c33 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/MessageCacheSnapshot.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/MessageCacheSnapshot.kt @@ -26,6 +26,7 @@ data class MessageCacheItem( val toolCallId: String? = null, val contentType: String? = null, val sizeBytes: Int? = null, + val fingerprint: String? = null, val retryable: Boolean = false, ) @@ -50,6 +51,7 @@ fun ChatMessage.toCacheItem(): MessageCacheItem = MessageCacheItem( toolCallId = toolCallId, contentType = contentType, sizeBytes = sizeBytes, + fingerprint = attachmentFingerprint, retryable = retryable, ) @@ -74,5 +76,6 @@ fun MessageCacheItem.toDomain(): ChatMessage = ChatMessage( toolCallId = toolCallId, contentType = contentType, sizeBytes = sizeBytes, + attachmentFingerprint = fingerprint, retryable = retryable, ) diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/Models.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/Models.kt index db4e082..d9044bc 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/Models.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/Models.kt @@ -27,6 +27,7 @@ data class ChatMessage( val toolCallId: String? = null, val contentType: String? = null, val sizeBytes: Int? = null, + val attachmentFingerprint: String? = null, val retryable: Boolean = false, ) diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/PendingSendQueue.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/PendingSendQueue.kt index ed7270f..93a4e88 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/PendingSendQueue.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/PendingSendQueue.kt @@ -26,4 +26,5 @@ data class PendingSendItem( val contentType: String? = null, val base64Data: String? = null, val sizeBytes: Int? = null, + val fingerprint: String? = null, ) 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 0b3f0ee..0f914dd 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt @@ -58,6 +58,7 @@ private data class PendingAttachmentSend( val bytes: ByteArray, val contentType: String?, val createdAt: String, + val fingerprint: String, ) internal data class PendingQueueRecoveryResult( @@ -113,6 +114,14 @@ class RemoteOpenClawRepository( private val autoRetryMessageIds = mutableSetOf() private var drainingSessionIds by mutableStateOf>(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 messagesFor(sessionId: String): List = messageStore[sessionId].orEmpty() @@ -173,6 +182,7 @@ class RemoteOpenClawRepository( bytes = bytes, contentType = item.contentType, createdAt = item.createdAt, + fingerprint = item.fingerprint ?: attachmentFingerprint(item.fileName, bytes, item.contentType), ) attachmentRetryStore[item.messageId] = AttachmentRetryPayload( sessionId = item.sessionId, @@ -293,6 +303,7 @@ class RemoteOpenClawRepository( bytes = bytes, contentType = contentType, createdAt = "现在", + fingerprint = attachmentFingerprint(fileName, bytes, contentType), ) val pending = ChatMessage( id = localId, @@ -303,6 +314,7 @@ class RemoteOpenClawRepository( kind = MessageKind.Attachment, contentType = contentType, sizeBytes = bytes.size, + attachmentFingerprint = attachmentFingerprint(fileName, bytes, contentType), ) messageStore = messageStore + (sessionId to (currentMessages + pending)) updateSessionPreview(sessionId, pending) @@ -322,6 +334,7 @@ class RemoteOpenClawRepository( kind = MessageKind.Attachment, contentType = remote.contentType, sizeBytes = remote.sizeBytes, + attachmentFingerprint = attachmentFingerprint(fileName, bytes, remote.contentType), ) attachmentRetryStore.remove(localId) attachmentOpenStore.remove(localId) @@ -340,6 +353,7 @@ class RemoteOpenClawRepository( kind = MessageKind.Attachment, contentType = contentType, sizeBytes = bytes.size, + attachmentFingerprint = attachmentFingerprint(fileName, bytes, contentType), retryable = true, ) val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == localId) failed else msg } @@ -382,6 +396,7 @@ class RemoteOpenClawRepository( kind = MessageKind.Attachment, contentType = remote.contentType, sizeBytes = remote.sizeBytes, + attachmentFingerprint = payload.fingerprint, ) attachmentRetryStore.remove(messageId) attachmentOpenStore.remove(messageId) @@ -400,6 +415,7 @@ class RemoteOpenClawRepository( kind = MessageKind.Attachment, contentType = payload.contentType, sizeBytes = payload.bytes.size, + attachmentFingerprint = payload.fingerprint, retryable = true, ) val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == messageId) failed else msg } @@ -468,6 +484,7 @@ class RemoteOpenClawRepository( contentType = item.contentType, base64Data = Base64.encode(item.bytes), sizeBytes = item.bytes.size, + fingerprint = item.fingerprint, ) } ) @@ -674,6 +691,7 @@ class RemoteOpenClawRepository( private fun matchesPendingLocalAttachment(local: ChatMessage, remote: ChatMessage): Boolean { return remote.kind == MessageKind.Attachment && remote.text == local.text && + (local.attachmentFingerprint == null || remote.attachmentFingerprint == null || remote.attachmentFingerprint == local.attachmentFingerprint) && (local.contentType == null || remote.contentType == local.contentType) && (local.sizeBytes == null || remote.sizeBytes == local.sizeBytes) } diff --git a/docs/MVP_PROGRESS.md b/docs/MVP_PROGRESS.md index 037fa19..094760b 100644 --- a/docs/MVP_PROGRESS.md +++ b/docs/MVP_PROGRESS.md @@ -211,6 +211,7 @@ Android/iOS App - [x] 已给 session 级自动恢复补简易防重入/短节流:`openSession()` 和 SSE recovery 现在共用 `recoverPendingQueueForSession(...)`,避免短时间内对同一会话重复触发 queue recovery - [x] 已补 queue recovery 轻量可观测性:pending queue 状态里会暴露“正在恢复中 / 刚因节流跳过”,聊天页和反馈文案能区分“真在跑”和“刚跑过所以先跳过” - [x] 已开始收紧本地 pending 与远端消息的匹配条件:文本 pending 不再只看裸文本;附件 pending 除文件名外还会参考 `contentType/sizeBytes`,降低同文案/同文件名重发时的误合并风险 +- [x] 已给附件 pending 增加本地匹配指纹:基于 `fileName/contentType/sizeBytes/bytes 前缀 hash` 生成轻量 fingerprint,并随 pending queue / message cache 持久化,进一步降低同名附件重传时的误匹配概率 - [ ] 还没有更完整的网络恢复策略(目前先基于会话 SSE 恢复触发,不含更底层网络监听或全局后台队列调度) ### 4.2 附件后端主链路