chat: reconcile history by canonical message identity

This commit is contained in:
2026-10-19 00:08:30 +01:00
parent 3187b0c06e
commit 7da3d54013
13 changed files with 189 additions and 15 deletions
+29 -7
View File
@@ -465,14 +465,36 @@ func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage {
return messages
}
// reconcileHistory intentionally does not infer identity. OpenCode history
// exposes only its native message id and does not echo the prompt operation id,
// so there is no reliable mapping from a retained cross-client user event to a
// history record. In particular, position, text, timestamps and page boundaries
// are not identity keys; annotating with any of them corrupts partial, reordered,
// older or overlapping pages. The live event already carries the operation id.
// reconcileHistory applies only the explicit native-id mapping established by a
// caller-supplied prompt. It never infers identity from text, timestamps,
// position, ordering or page boundaries.
func (h *chatHub) reconcileHistory(sessionID string, messages []protocol.ChatMessage) []protocol.ChatMessage {
return messages
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turns[sessionID]
if turn == nil {
return messages
}
byNativeID := make(map[string]*chatMessage)
for _, message := range turn.messages {
if message.role == protocol.ChatRoleUser && message.id != "" && message.operationID != "" {
byNativeID[message.id] = message
}
}
if len(byNativeID) == 0 {
return messages
}
out := make([]protocol.ChatMessage, len(messages))
copy(out, messages)
for i := range out {
if live := byNativeID[out[i].ID]; live != nil {
out[i].OperationID = live.operationID
if out[i].Client == "" {
out[i].Client = live.client
}
}
}
return out
}
// shellSnapshot returns the session's still-running shells, in the order they