chat: preserve cross-client reconciliation work

This commit is contained in:
2026-10-18 21:45:56 +01:00
parent 5946f1abc8
commit 36d1c8ea7a
11 changed files with 213 additions and 35 deletions
+48 -6
View File
@@ -55,11 +55,12 @@ type chatPart struct {
// are carried for a user turn the server echoed (a prompt another client sent),
// so a resumed transcript still says who sent it.
type chatMessage struct {
id string
role string
client string
order []string
parts map[string]*chatPart
id string
role string
client string
operationID string
order []string
parts map[string]*chatPart
}
type chatTurn struct {
@@ -168,12 +169,15 @@ func (h *chatHub) applyLocked(frame protocol.ChatEventFrame) {
role = protocol.ChatRoleUser
client = frame.Client
}
message = &chatMessage{id: frame.MessageID, role: role, client: client, parts: map[string]*chatPart{}}
message = &chatMessage{id: frame.MessageID, role: role, client: client, operationID: frame.OperationID, parts: map[string]*chatPart{}}
turn.order = append(turn.order, frame.MessageID)
turn.messages[frame.MessageID] = message
} else if message.client == "" && frame.Kind == protocol.ChatEventUser {
message.client = frame.Client
}
if message.operationID == "" && frame.Kind == protocol.ChatEventUser {
message.operationID = frame.OperationID
}
switch frame.Kind {
case protocol.ChatEventUser:
if frame.Part == nil || frame.Part.ID == "" || frame.Part.Text == "" || turn.overflow {
@@ -448,6 +452,7 @@ func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage {
out.Role = message.role
}
out.Client = message.client
out.OperationID = message.operationID
for _, partID := range message.order {
if part, ok := message.parts[partID]; ok {
piece := part.part
@@ -460,6 +465,43 @@ 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.
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
}
// shellSnapshot returns the session's still-running shells, in the order they
// started, so a client attaching to the session's stream can be re-synced the
// background processes it cannot have seen. It is the shell half of the resume