Add local client tokens for pending text matching

This commit is contained in:
OpenClaw committed 2026-03-07 14:04:12 +08:00
1 parent 485afdab02
commit dcd4b2adbd
5 files changed
+24 -1

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 clientToken: String? = null,
val fingerprint: String? = null, val fingerprint: String? = null,
val retryable: Boolean = false, val retryable: Boolean = false,
) )
@@ -51,6 +52,7 @@ fun ChatMessage.toCacheItem(): MessageCacheItem = MessageCacheItem(
toolCallId = toolCallId, toolCallId = toolCallId,
contentType = contentType, contentType = contentType,
sizeBytes = sizeBytes, sizeBytes = sizeBytes,
clientToken = clientToken,
fingerprint = attachmentFingerprint, fingerprint = attachmentFingerprint,
retryable = retryable, retryable = retryable,
) )
@@ -76,6 +78,7 @@ fun MessageCacheItem.toDomain(): ChatMessage = ChatMessage(
toolCallId = toolCallId, toolCallId = toolCallId,
contentType = contentType, contentType = contentType,
sizeBytes = sizeBytes, sizeBytes = sizeBytes,
clientToken = clientToken,
attachmentFingerprint = fingerprint, 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 clientToken: String? = null,
val attachmentFingerprint: String? = null, val attachmentFingerprint: String? = null,
val retryable: Boolean = false, val retryable: Boolean = false,
) )
@@ -22,6 +22,7 @@ data class PendingSendItem(
val kind: PendingSendKind = PendingSendKind.Text, val kind: PendingSendKind = PendingSendKind.Text,
val text: String = "", val text: String = "",
val autoRetryEligible: Boolean = true, val autoRetryEligible: Boolean = true,
val clientToken: String? = null,
val fileName: String = "", val fileName: String = "",
val contentType: String? = null, val contentType: String? = null,
val base64Data: String? = null, val base64Data: String? = null,
@@ -48,6 +48,7 @@ private data class PendingTextSend(
val sessionId: String, val sessionId: String,
val text: String, val text: String,
val createdAt: String, val createdAt: String,
val clientToken: String,
val autoRetryEligible: Boolean = true, val autoRetryEligible: Boolean = true,
) )
@@ -122,6 +123,14 @@ class RemoteOpenClawRepository(
return listOf(fileName, contentType.orEmpty(), bytes.size.toString(), hash.toULong().toString(16)).joinToString("|") 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 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()
@@ -165,6 +174,7 @@ class RemoteOpenClawRepository(
sessionId = item.sessionId, sessionId = item.sessionId,
text = item.text, text = item.text,
createdAt = item.createdAt, createdAt = item.createdAt,
clientToken = item.clientToken ?: textClientToken(item.sessionId, item.text, item.messageId.hashCode()),
autoRetryEligible = item.autoRetryEligible, autoRetryEligible = item.autoRetryEligible,
) )
textRetryStore[item.messageId] = TextRetryPayload(item.sessionId, item.text) textRetryStore[item.messageId] = TextRetryPayload(item.sessionId, item.text)
@@ -251,11 +261,13 @@ class RemoteOpenClawRepository(
val withoutThinking = currentMessages.filterNot { it.id.startsWith("local-thinking-") } val withoutThinking = currentMessages.filterNot { it.id.startsWith("local-thinking-") }
val localUserId = "local-user-$sessionId-${withoutThinking.size + 1}" val localUserId = "local-user-$sessionId-${withoutThinking.size + 1}"
textRetryStore[localUserId] = TextRetryPayload(sessionId, text) textRetryStore[localUserId] = TextRetryPayload(sessionId, text)
val clientToken = textClientToken(sessionId, text, withoutThinking.size + 1)
pendingTextQueue[localUserId] = PendingTextSend( pendingTextQueue[localUserId] = PendingTextSend(
messageId = localUserId, messageId = localUserId,
sessionId = sessionId, sessionId = sessionId,
text = text, text = text,
createdAt = "现在", createdAt = "现在",
clientToken = clientToken,
autoRetryEligible = true, autoRetryEligible = true,
) )
val next = withoutThinking + listOf( val next = withoutThinking + listOf(
@@ -265,6 +277,7 @@ class RemoteOpenClawRepository(
text = text, text = text,
createdAt = "现在", createdAt = "现在",
status = textStatusForLocalPhase(LocalMessagePhase.ManualProcessing), status = textStatusForLocalPhase(LocalMessagePhase.ManualProcessing),
clientToken = clientToken,
), ),
ChatMessage( ChatMessage(
id = "local-thinking-$sessionId-${withoutThinking.size + 2}", id = "local-thinking-$sessionId-${withoutThinking.size + 2}",
@@ -470,6 +483,7 @@ class RemoteOpenClawRepository(
kind = PendingSendKind.Text, kind = PendingSendKind.Text,
text = item.text, text = item.text,
autoRetryEligible = item.autoRetryEligible, autoRetryEligible = item.autoRetryEligible,
clientToken = item.clientToken,
) )
} }
) )
@@ -685,6 +699,7 @@ class RemoteOpenClawRepository(
private fun matchesPendingLocalText(local: ChatMessage, remote: ChatMessage): Boolean { private fun matchesPendingLocalText(local: ChatMessage, remote: ChatMessage): Boolean {
return remote.role == MessageRole.User && return remote.role == MessageRole.User &&
remote.kind == MessageKind.Text && remote.kind == MessageKind.Text &&
(local.clientToken == null || remote.clientToken == null || remote.clientToken == local.clientToken) &&
remote.text == local.text remote.text == local.text
} }
@@ -887,13 +902,15 @@ class RemoteOpenClawRepository(
sessionId = sessionId, sessionId = sessionId,
text = text, text = text,
createdAt = "刚刚", createdAt = "刚刚",
clientToken = message.clientToken ?: textClientToken(sessionId, text, failedId.hashCode()),
autoRetryEligible = autoRetryEligible, autoRetryEligible = autoRetryEligible,
) )
message.copy( message.copy(
id = failedId, id = failedId,
status = textStatusForLocalPhase(LocalMessagePhase.Failed), status = textStatusForLocalPhase(LocalMessagePhase.Failed),
retryable = true,
createdAt = "刚刚", createdAt = "刚刚",
retryable = true,
clientToken = message.clientToken ?: textClientToken(sessionId, text, failedId.hashCode()),
) )
} }
else -> message else -> message
+1
View File
@@ -212,6 +212,7 @@ Android/iOS App
- [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 持久化,进一步降低同名附件重传时的误匹配概率 - [x] 已给附件 pending 增加本地匹配指纹:基于 `fileName/contentType/sizeBytes/bytes 前缀 hash` 生成轻量 fingerprint,并随 pending queue / message cache 持久化,进一步降低同名附件重传时的误匹配概率
- [x] 已给文本 pending 增加本地 client token:本地发送/失败回落/缓存恢复/队列持久化都会带上 token,文本 pending 与远端消息归并时优先参考 token,降低“同文案连续发送”时的误匹配概率
- [ ] 还没有更完整的网络恢复策略(目前先基于会话 SSE 恢复触发,不含更底层网络监听或全局后台队列调度) - [ ] 还没有更完整的网络恢复策略(目前先基于会话 SSE 恢复触发,不含更底层网络监听或全局后台队列调度)
### 4.2 附件后端主链路 ### 4.2 附件后端主链路