chat: reconcile history by canonical message identity
This commit is contained in:
@@ -42,6 +42,10 @@ type chatBroker interface {
|
||||
Reply(ctx context.Context, request protocol.ChatReplyRequest) error
|
||||
}
|
||||
|
||||
type chatPromptWithMessageID interface {
|
||||
PromptWithMessageID(context.Context, string, string, string) error
|
||||
}
|
||||
|
||||
type chatShellOutputReader interface {
|
||||
ShellOutput(ctx context.Context, query protocol.ChatShellOutputQuery) (protocol.ChatShellOutputPage, error)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -102,6 +102,7 @@ func (s *chatSub) close() {
|
||||
type chatQueuedPrompt struct {
|
||||
text string
|
||||
operationID string
|
||||
nativeID string
|
||||
requestID string
|
||||
write func(protocol.Message) error
|
||||
acked bool
|
||||
@@ -659,8 +660,14 @@ func (h *chatHub) runTurn(room *chatRoom, p *chatQueuedPrompt, done chan struct{
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := broker.Prompt(room.ctx, room.sessionID, p.text); err != nil {
|
||||
h.failPrompt(room, p, err)
|
||||
var promptErr error
|
||||
if withID, ok := broker.(chatPromptWithMessageID); ok {
|
||||
promptErr = withID.PromptWithMessageID(room.ctx, room.sessionID, p.text, p.nativeID)
|
||||
} else {
|
||||
promptErr = broker.Prompt(room.ctx, room.sessionID, p.text)
|
||||
}
|
||||
if promptErr != nil {
|
||||
h.failPrompt(room, p, promptErr)
|
||||
h.endRoomTurn(room)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -55,15 +55,19 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
|
||||
// sender's own client appended it locally, so it is excluded below.
|
||||
userFrame := protocol.ChatEventFrame{
|
||||
SessionID: sessionID,
|
||||
MessageID: hub.nextUserMessageID(),
|
||||
MessageID: prompt.NativeMessageID,
|
||||
Kind: protocol.ChatEventUser,
|
||||
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: prompt.Text},
|
||||
Client: sub.label,
|
||||
OperationID: prompt.OperationID,
|
||||
}
|
||||
if userFrame.MessageID == "" {
|
||||
userFrame.MessageID = hub.nextUserMessageID()
|
||||
}
|
||||
queued, refused := hub.submitPrompt(sessionID, &chatQueuedPrompt{
|
||||
text: prompt.Text,
|
||||
operationID: prompt.OperationID,
|
||||
nativeID: userFrame.MessageID,
|
||||
requestID: request.ID,
|
||||
write: write,
|
||||
})
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user