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
+83
View File
@@ -963,6 +963,89 @@ func TestChatHubHistoryDoesNotInventOperationIdentity(t *testing.T) {
}
}
func TestChatHubHistoryMapsOnlyCanonicalNativeIdentity(t *testing.T) {
hub := newChatHub()
hub.apply(protocol.ChatEventFrame{
SessionID: "ses_1", MessageID: "msg_x", Kind: protocol.ChatEventUser,
Client: "Phone", OperationID: "op_x",
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: "same"},
})
hub.apply(protocol.ChatEventFrame{
SessionID: "ses_1", MessageID: "msg_y", Kind: protocol.ChatEventUser,
Client: "Laptop", OperationID: "op_y",
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: "same"},
})
got := hub.reconcileHistory("ses_1", []protocol.ChatMessage{
{ID: "msg_y", Role: protocol.ChatRoleUser},
{ID: "unrelated", Role: protocol.ChatRoleUser, Parts: []protocol.ChatPart{{Text: "same"}}},
{ID: "msg_x", Role: protocol.ChatRoleUser},
})
if got[0].OperationID != "op_y" || got[2].OperationID != "op_x" || got[1].OperationID != "" {
t.Fatalf("history identity mapping = %+v", got)
}
}
// TestChatMessagesReconcilesProviderPagesThroughTheHandler exercises the normal
// history path, rather than calling reconcileHistory alone. The provider fixture
// deliberately returns partial, reordered, older and overlapping pages (including
// equal timestamps); only the explicit native ids may restore operation identity.
func TestChatMessagesReconcilesProviderPagesThroughTheHandler(t *testing.T) {
hub := newChatHub()
for _, turn := range []struct {
id, operation, client string
}{
{"msg_x", "op_x", "Phone"},
{"msg_y", "op_y", "Computer"},
} {
hub.apply(protocol.ChatEventFrame{
SessionID: "ses_1", MessageID: turn.id, Kind: protocol.ChatEventUser,
OperationID: turn.operation, Client: turn.client,
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: turn.operation},
})
}
pages := []struct {
name string
messages []protocol.ChatMessage
}{
{"partial-reordered", []protocol.ChatMessage{
{ID: "msg_y", Role: protocol.ChatRoleUser, CreatedAt: 10},
{ID: "msg_x", Role: protocol.ChatRoleUser, CreatedAt: 10},
}},
{"older", []protocol.ChatMessage{
{ID: "msg_x", Role: protocol.ChatRoleUser, CreatedAt: 10},
}},
{"overlap-reconnect", []protocol.ChatMessage{
{ID: "msg_y", Role: protocol.ChatRoleUser, CreatedAt: 10},
{ID: "unrelated", Role: protocol.ChatRoleUser, CreatedAt: 10},
}},
}
for _, page := range pages {
broker := scriptedChat()
broker.messages = page.messages
var response protocol.Message
handleChatMessages(context.Background(), protocol.Message{
Type: protocol.ChatMessages, ID: page.name,
ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"},
}, broker, hub, func(message protocol.Message) error {
response = message
return nil
})
if response.Type != protocol.ChatMessages || response.ChatHistory == nil {
t.Fatalf("%s response = %+v", page.name, response)
}
for _, message := range response.ChatHistory.Messages {
want := map[string]string{"msg_x": "op_x", "msg_y": "op_y"}[message.ID]
if want != "" && message.OperationID != want {
t.Fatalf("%s message %q operation = %q, want %q", page.name, message.ID, message.OperationID, want)
}
if message.ID == "unrelated" && message.OperationID != "" {
t.Fatalf("%s invented identity for unrelated message: %+v", page.name, message)
}
}
}
}
// 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.