Preserve partial cross-client identity fixes before canonical-ID continuation

This commit is contained in:
2026-10-18 23:28:54 +01:00
parent 980f52bd85
commit d630235657
4 changed files with 46 additions and 44 deletions
@@ -845,16 +845,20 @@ class ChatStore(
fun history(sessionId: String, messages: List<ChatMessage>, nextCursor: String = "", hasMore: Boolean = false) {
update(sessionId) {
val existing = it.messages.associateBy { message -> message.id }
val optimisticByOperation = it.messages
// Operation identity remains valid after a live user event replaces
// local-* with the server's synthetic id. Index every user turn,
// not only optimistic records, so a later history read can replace
// that synthetic id with the provider's authoritative id.
val existingUserByOperation = it.messages
.asSequence()
.filter { message -> message.id.startsWith(LOCAL_MESSAGE_PREFIX) && message.operationId.isNotBlank() }
.filter { message -> message.role == ChatProtocol.ROLE_USER && message.operationId.isNotBlank() }
.associateBy { message -> message.operationId }
val reconciledIds = messages.asSequence()
.mapNotNull { message -> optimisticByOperation[message.operationId]?.id }
.mapNotNull { message -> existingUserByOperation[message.operationId]?.id }
.toSet()
val authoritativeMessages = messages.map { message ->
val optimistic = optimisticByOperation[message.operationId]
if (optimistic == null || message.role != ChatProtocol.ROLE_USER) {
val local = existingUserByOperation[message.operationId]
if (local == null || message.role != ChatProtocol.ROLE_USER) {
message
} else {
// History is authoritative for identity and ordering, but it
@@ -862,9 +866,10 @@ class ChatStore(
// fields. Keep those fields on the same turn while replacing
// the temporary local id.
message.copy(
client = message.client.ifBlank { optimistic.client },
parts = message.parts + optimistic.parts.filter { localPart ->
localPart.type == ChatProtocol.PART_FILE && message.parts.none { it.type == ChatProtocol.PART_FILE && it.uri == localPart.uri }
client = message.client.ifBlank { local.client },
parts = message.parts + local.parts.filter { localPart ->
message.parts.none { it.type == localPart.type && it.id == localPart.id } &&
(localPart.type == ChatProtocol.PART_FILE || localPart.type == ChatProtocol.PART_TEXT || localPart.type == ChatProtocol.PART_REASONING)
},
)
}
@@ -344,15 +344,24 @@ class MultiChatStateTest {
store.open(session("a"))
val attachmentUri = "content://x"
store.appendLocalUserMessage("a", "X", listOf(ChatAttachment(uri = attachmentUri, name = "x.txt", mime = "text/plain")), "op-x")
// The sender sees the live event before the provider history catches up.
store.event(ChatEventFrame(
sessionId = "a", messageId = "native-x", kind = ChatProtocol.EVENT_USER,
operationId = "op-x", client = "Phone", part = ChatPart(type = ChatProtocol.PART_TEXT, text = "X"),
))
// History replaces the synthetic event id, but must keep the same turn
// (including its attachment) rather than appending a second X.
store.history("a", listOf(ChatMessage(
id = "native-history-x", sessionId = "a", role = ChatProtocol.ROLE_USER,
operationId = "op-x", client = "Phone",
parts = listOf(ChatPart(type = ChatProtocol.PART_TEXT, text = "X")),
)))
// A different client sends Y after the history read.
store.event(ChatEventFrame(
sessionId = "a", messageId = "native-y", kind = ChatProtocol.EVENT_USER,
operationId = "op-y", client = "Computer", part = ChatPart(type = ChatProtocol.PART_TEXT, text = "Y"),
))
// A replayed X is an idempotent delivery, not another turn.
// The delayed original X is an idempotent delivery, not another turn.
store.event(ChatEventFrame(
sessionId = "a", messageId = "native-x", kind = ChatProtocol.EVENT_USER,
operationId = "op-x", client = "Phone", part = ChatPart(type = ChatProtocol.PART_TEXT, text = "X"),
@@ -364,7 +373,7 @@ class MultiChatStateTest {
))
val messages = store.value.thread("a")!!.messages
assertEquals(listOf("native-x", "native-y", "native-x-2"), messages.map { it.id })
assertEquals(listOf("native-history-x", "native-y", "native-x-2"), messages.map { it.id })
assertEquals(listOf("X", "Y", "X"), messages.map { it.parts.first().text })
assertEquals(listOf("Phone", "Computer", "Phone"), messages.map { it.client })
assertEquals(listOf("op-x", "op-y", "op-x-2"), messages.map { it.operationId })