From dcd4b2adbd1423e82cb993606d24207a13618e39 Mon Sep 17 00:00:00 2001 From: OpenClaw Date: Sat, 7 Mar 2026 14:04:12 +0800 Subject: [PATCH] Add local client tokens for pending text matching --- .../openclawapp/data/MessageCacheSnapshot.kt | 3 +++ .../cn/xiaobai/openclawapp/data/Models.kt | 1 + .../openclawapp/data/PendingSendQueue.kt | 1 + .../data/RemoteOpenClawRepository.kt | 19 ++++++++++++++++++- docs/MVP_PROGRESS.md | 1 + 5 files changed, 24 insertions(+), 1 deletion(-) 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 71c1c33..75416ff 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 clientToken: String? = null, val fingerprint: String? = null, val retryable: Boolean = false, ) @@ -51,6 +52,7 @@ fun ChatMessage.toCacheItem(): MessageCacheItem = MessageCacheItem( toolCallId = toolCallId, contentType = contentType, sizeBytes = sizeBytes, + clientToken = clientToken, fingerprint = attachmentFingerprint, retryable = retryable, ) @@ -76,6 +78,7 @@ fun MessageCacheItem.toDomain(): ChatMessage = ChatMessage( toolCallId = toolCallId, contentType = contentType, sizeBytes = sizeBytes, + clientToken = clientToken, 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 d9044bc..f839691 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 clientToken: String? = 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 93a4e88..d0d79d3 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/PendingSendQueue.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/PendingSendQueue.kt @@ -22,6 +22,7 @@ data class PendingSendItem( val kind: PendingSendKind = PendingSendKind.Text, val text: String = "", val autoRetryEligible: Boolean = true, + val clientToken: String? = null, val fileName: String = "", val contentType: String? = null, val base64Data: 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 0f914dd..5595b59 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt @@ -48,6 +48,7 @@ private data class PendingTextSend( val sessionId: String, val text: String, val createdAt: String, + val clientToken: String, val autoRetryEligible: Boolean = true, ) @@ -122,6 +123,14 @@ class RemoteOpenClawRepository( return listOf(fileName, contentType.orEmpty(), bytes.size.toString(), hash.toULong().toString(16)).joinToString("|") } + private fun textClientToken(sessionId: String, text: String, ordinal: Int): String { + var hash = 1469598103934665603L + text.take(128).forEach { ch -> + hash = (hash xor ch.code.toLong()) * 1099511628211L + } + return listOf(sessionId, ordinal.toString(), text.length.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() @@ -165,6 +174,7 @@ class RemoteOpenClawRepository( sessionId = item.sessionId, text = item.text, createdAt = item.createdAt, + clientToken = item.clientToken ?: textClientToken(item.sessionId, item.text, item.messageId.hashCode()), autoRetryEligible = item.autoRetryEligible, ) textRetryStore[item.messageId] = TextRetryPayload(item.sessionId, item.text) @@ -251,11 +261,13 @@ class RemoteOpenClawRepository( val withoutThinking = currentMessages.filterNot { it.id.startsWith("local-thinking-") } val localUserId = "local-user-$sessionId-${withoutThinking.size + 1}" textRetryStore[localUserId] = TextRetryPayload(sessionId, text) + val clientToken = textClientToken(sessionId, text, withoutThinking.size + 1) pendingTextQueue[localUserId] = PendingTextSend( messageId = localUserId, sessionId = sessionId, text = text, createdAt = "现在", + clientToken = clientToken, autoRetryEligible = true, ) val next = withoutThinking + listOf( @@ -265,6 +277,7 @@ class RemoteOpenClawRepository( text = text, createdAt = "现在", status = textStatusForLocalPhase(LocalMessagePhase.ManualProcessing), + clientToken = clientToken, ), ChatMessage( id = "local-thinking-$sessionId-${withoutThinking.size + 2}", @@ -470,6 +483,7 @@ class RemoteOpenClawRepository( kind = PendingSendKind.Text, text = item.text, autoRetryEligible = item.autoRetryEligible, + clientToken = item.clientToken, ) } ) @@ -685,6 +699,7 @@ class RemoteOpenClawRepository( private fun matchesPendingLocalText(local: ChatMessage, remote: ChatMessage): Boolean { return remote.role == MessageRole.User && remote.kind == MessageKind.Text && + (local.clientToken == null || remote.clientToken == null || remote.clientToken == local.clientToken) && remote.text == local.text } @@ -887,13 +902,15 @@ class RemoteOpenClawRepository( sessionId = sessionId, text = text, createdAt = "刚刚", + clientToken = message.clientToken ?: textClientToken(sessionId, text, failedId.hashCode()), autoRetryEligible = autoRetryEligible, ) message.copy( id = failedId, status = textStatusForLocalPhase(LocalMessagePhase.Failed), - retryable = true, createdAt = "刚刚", + retryable = true, + clientToken = message.clientToken ?: textClientToken(sessionId, text, failedId.hashCode()), ) } else -> message diff --git a/docs/MVP_PROGRESS.md b/docs/MVP_PROGRESS.md index 094760b..7a4be63 100644 --- a/docs/MVP_PROGRESS.md +++ b/docs/MVP_PROGRESS.md @@ -212,6 +212,7 @@ Android/iOS App - [x] 已补 queue recovery 轻量可观测性:pending queue 状态里会暴露“正在恢复中 / 刚因节流跳过”,聊天页和反馈文案能区分“真在跑”和“刚跑过所以先跳过” - [x] 已开始收紧本地 pending 与远端消息的匹配条件:文本 pending 不再只看裸文本;附件 pending 除文件名外还会参考 `contentType/sizeBytes`,降低同文案/同文件名重发时的误合并风险 - [x] 已给附件 pending 增加本地匹配指纹:基于 `fileName/contentType/sizeBytes/bytes 前缀 hash` 生成轻量 fingerprint,并随 pending queue / message cache 持久化,进一步降低同名附件重传时的误匹配概率 +- [x] 已给文本 pending 增加本地 client token:本地发送/失败回落/缓存恢复/队列持久化都会带上 token,文本 pending 与远端消息归并时优先参考 token,降低“同文案连续发送”时的误匹配概率 - [ ] 还没有更完整的网络恢复策略(目前先基于会话 SSE 恢复触发,不含更底层网络监听或全局后台队列调度) ### 4.2 附件后端主链路