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
+7 -34
View File
@@ -465,41 +465,14 @@ func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage {
return messages
}
// reconcileHistory annotates an authoritative page with operation identities
// from the retained live user turns. Pairing is by the backend's message order,
// never by text, timestamps, or a connection/request id. It is deliberately
// conservative: a partial page cannot establish a safe one-to-one mapping.
// 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.
func (h *chatHub) reconcileHistory(sessionID string, messages []protocol.ChatMessage) []protocol.ChatMessage {
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turns[sessionID]
if turn == nil {
return messages
}
var live []*chatMessage
for _, id := range turn.order {
message := turn.messages[id]
if message != nil && message.role == protocol.ChatRoleUser && message.operationID != "" {
live = append(live, message)
}
}
var users []int
for i := range messages {
if messages[i].Role == protocol.ChatRoleUser {
users = append(users, i)
}
}
if len(users) == 0 || len(users) != len(live) {
return messages
}
out := append([]protocol.ChatMessage(nil), messages...)
for i, index := range users {
out[index].OperationID = live[i].operationID
if out[index].Client == "" {
out[index].Client = live[i].client
}
}
return out
return messages
}
// shellSnapshot returns the session's still-running shells, in the order they
+15
View File
@@ -948,6 +948,21 @@ func TestChatHubKeepsMessagesSeparate(t *testing.T) {
}
}
// OpenCode history exposes native message ids but no prompt operation id. The
// broker must therefore leave partial, reordered, older and overlapping pages
// untouched rather than inventing a positional identity mapping.
func TestChatHubHistoryDoesNotInventOperationIdentity(t *testing.T) {
hub := newChatHub()
page := []protocol.ChatMessage{
{ID: "history-y", Role: protocol.ChatRoleUser, OperationID: "provider-value"},
{ID: "history-x", Role: protocol.ChatRoleUser},
}
got := hub.reconcileHistory("ses_1", page)
if len(got) != len(page) || got[0].ID != "history-y" || got[0].OperationID != "provider-value" || got[1].OperationID != "" {
t.Fatalf("history changed without a canonical provider mapping: %+v", got)
}
}
// TestChatHubChargesToolStateBytes pins the retained-state cap: a tool part's
// input/content bytes count against it, and an upsert that would exceed the cap
// is dropped with the overflow diagnostic rather than growing memory.