From 7da3d54013447b7fe69000ee5148ada2a3931aad Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Mon, 19 Oct 2026 00:08:30 +0100 Subject: [PATCH] chat: reconcile history by canonical message identity --- .../pipelines/data/PipelinesRepository.kt | 9 +- .../net/awan/pipelines/data/protocol/Chat.kt | 5 +- .../pipelines/ui/chat/MultiChatStateTest.kt | 8 ++ internal/opencode/chat.go | 10 +++ internal/opencode/chat_test.go | 5 +- internal/opencode/chat_v2_test.go | 19 +++++ internal/opencode/native.go | 4 + internal/protocol/chat.go | 4 + internal/server/chat.go | 4 + internal/server/chat_hub.go | 36 ++++++-- internal/server/chat_room.go | 11 ++- internal/server/chat_stream.go | 6 +- internal/server/chat_test.go | 83 +++++++++++++++++++ 13 files changed, 189 insertions(+), 15 deletions(-) diff --git a/android/app/src/main/java/net/awan/pipelines/data/PipelinesRepository.kt b/android/app/src/main/java/net/awan/pipelines/data/PipelinesRepository.kt index a8e163d2..b01f4ad8 100644 --- a/android/app/src/main/java/net/awan/pipelines/data/PipelinesRepository.kt +++ b/android/app/src/main/java/net/awan/pipelines/data/PipelinesRepository.kt @@ -353,7 +353,7 @@ class PipelinesRepository( if (sessionId.isBlank()) throw ProtocolException("Select a chat session first.") val trimmed = text.trim() if (trimmed.isEmpty()) throw ProtocolException("Enter a message before sending.") - return client.call(ChatProtocol.promptRequest(sessionId, trimmed, files, clientLabel, operationId)).chatQueued + return client.call(ChatProtocol.promptRequest(sessionId, trimmed, files, clientLabel, operationId, nativeMessageId(operationId))).chatQueued } suspend fun chatInterrupt(sessionId: String) { @@ -408,5 +408,12 @@ class PipelinesRepository( /** A durable key for one chat-create intent; retain it when retrying. */ fun newChatCreateOperationId(): String = "android-chat-" + UUID.randomUUID().toString() + + /** OpenCode's optional caller id is the canonical bridge to history. */ + fun nativeMessageId(operationId: String): String = + operationId.filter { character -> character.isLetterOrDigit() }.take(120) + .takeIf { it.isNotEmpty() } + ?.let { "msg_" + it } + .orEmpty() } } diff --git a/android/app/src/main/java/net/awan/pipelines/data/protocol/Chat.kt b/android/app/src/main/java/net/awan/pipelines/data/protocol/Chat.kt index 3c3ce2d8..954543ee 100644 --- a/android/app/src/main/java/net/awan/pipelines/data/protocol/Chat.kt +++ b/android/app/src/main/java/net/awan/pipelines/data/protocol/Chat.kt @@ -84,6 +84,7 @@ data class ChatPromptRequest( // a generic label when it is blank. val client: String = "", @SerialName("operation_id") val operationId: String = "", + @SerialName("native_message_id") val nativeMessageId: String = "", ) /** One stored turn: a user turn is a single text part, an assistant turn holds text/reasoning/tool parts. */ @@ -361,10 +362,10 @@ object ChatProtocol { * shape a text-only prompt has always sent). Built here (not inline in the * repository) so production and its unit test share the one wire shape. */ - fun promptRequest(sessionId: String, text: String, files: List = emptyList(), client: String = "", operationId: String = ""): Message = + fun promptRequest(sessionId: String, text: String, files: List = emptyList(), client: String = "", operationId: String = "", nativeMessageId: String = ""): Message = Message( type = Protocol.CHAT_PROMPT, - chatPrompt = ChatPromptRequest(sessionId = sessionId, text = text, files = files, client = client, operationId = operationId), + chatPrompt = ChatPromptRequest(sessionId = sessionId, text = text, files = files, client = client, operationId = operationId, nativeMessageId = nativeMessageId), ) /** diff --git a/android/app/src/test/java/net/awan/pipelines/ui/chat/MultiChatStateTest.kt b/android/app/src/test/java/net/awan/pipelines/ui/chat/MultiChatStateTest.kt index dc17a7d4..194dd4a5 100644 --- a/android/app/src/test/java/net/awan/pipelines/ui/chat/MultiChatStateTest.kt +++ b/android/app/src/test/java/net/awan/pipelines/ui/chat/MultiChatStateTest.kt @@ -1,6 +1,7 @@ package net.awan.pipelines.ui.chat import net.awan.pipelines.data.protocol.ChatEventFrame +import net.awan.pipelines.data.PipelinesRepository import net.awan.pipelines.data.protocol.ChatMessage import net.awan.pipelines.data.protocol.ChatPart import net.awan.pipelines.data.protocol.ChatPermissionFrame @@ -380,6 +381,13 @@ class MultiChatStateTest { assertEquals(attachmentUri, messages.first().parts.firstOrNull { it.type == ChatProtocol.PART_FILE }?.uri) } + @Test + fun emptyOperationDoesNotInventASharedNativeMessageId() { + assertEquals("", PipelinesRepository.nativeMessageId("")) + assertEquals("", PipelinesRepository.nativeMessageId(" ")) + assertEquals("msg_opx", PipelinesRepository.nativeMessageId("op-x")) + } + @Test fun historyOperationIdentityReplacesLocalIdWithoutDroppingAttachment() { val store = ChatStore() diff --git a/internal/opencode/chat.go b/internal/opencode/chat.go index 6342f772..0f38d5df 100644 --- a/internal/opencode/chat.go +++ b/internal/opencode/chat.go @@ -82,6 +82,13 @@ func (c *Chat) call(ctx context.Context, method, path string, body any) ([]byte, // (authenticated, bounded, directory-scoped). The endpoint returns the inbox // envelope immediately; the reply arrives on Stream. func (c *Chat) Prompt(ctx context.Context, id, text string) error { + return c.PromptWithMessageID(ctx, id, text, "") +} + +// PromptWithMessageID forwards the caller's canonical message id to OpenCode. +// OpenCode persists this id in history, which is the only safe bridge between +// the live user echo and an authoritative history page. +func (c *Chat) PromptWithMessageID(ctx context.Context, id, text, messageID string) error { if !sessionID.MatchString(id) { return fmt.Errorf("invalid OpenCode session id") } @@ -92,6 +99,9 @@ func (c *Chat) Prompt(ctx context.Context, id, text string) error { // the same "text" field, so one shape works on both backends without a // second request format. body := map[string]any{"text": text} + if messageID != "" { + body["id"] = messageID + } if _, err := c.call(ctx, "post", "/api/session/"+id+"/prompt", body); err != nil { return err } diff --git a/internal/opencode/chat_test.go b/internal/opencode/chat_test.go index b00ebcf5..c9acff88 100644 --- a/internal/opencode/chat_test.go +++ b/internal/opencode/chat_test.go @@ -782,7 +782,7 @@ func TestChatV1ConnectionDrivesThroughCall(t *testing.T) { chat := NewChat(&Connection{Backend: "v1", URL: srv.URL, Directory: "/repo", models: map[string]map[string]string{}, responses: map[string][]byte{}}) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - if err := chat.Prompt(ctx, "ses_1", "hello"); err != nil { + if err := chat.PromptWithMessageID(ctx, "ses_1", "hello", "msg_canonical"); err != nil { t.Fatalf("prompt: %v", err) } if !sawPrompt { @@ -792,6 +792,7 @@ func TestChatV1ConnectionDrivesThroughCall(t *testing.T) { // V2 `parts` shape and the v1 translation has to preserve it, or the native // serve process receives an empty prompt. var sent struct { + ID string `json:"id"` Parts []struct { Type string `json:"type"` Text string `json:"text"` @@ -800,7 +801,7 @@ func TestChatV1ConnectionDrivesThroughCall(t *testing.T) { if err := json.Unmarshal([]byte(promptBody), &sent); err != nil { t.Fatalf("prompt body: %v (%s)", err, promptBody) } - if len(sent.Parts) != 1 || sent.Parts[0].Type != protocol.ChatPartText || sent.Parts[0].Text != "hello" { + if sent.ID != "msg_canonical" || len(sent.Parts) != 1 || sent.Parts[0].Type != protocol.ChatPartText || sent.Parts[0].Text != "hello" { t.Fatalf("prompt text not carried through the v1 translation: %s", promptBody) } var got []protocol.ChatEventFrame diff --git a/internal/opencode/chat_v2_test.go b/internal/opencode/chat_v2_test.go index dfcb3973..805d3892 100644 --- a/internal/opencode/chat_v2_test.go +++ b/internal/opencode/chat_v2_test.go @@ -64,6 +64,25 @@ func TestChatV2PromptShapeAndAuth(t *testing.T) { } } +func TestChatV2PromptWithMessageID(t *testing.T) { + var body map[string]string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if err := json.NewDecoder(r.Body).Decode(&body); err != nil { + t.Fatal(err) + } + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"data":{"id":"msg_canonical","sessionID":"ses_1","type":"user"}}`)) + })) + defer srv.Close() + chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) + if err := chat.PromptWithMessageID(context.Background(), "ses_1", "same text", "msg_canonical"); err != nil { + t.Fatal(err) + } + if body["id"] != "msg_canonical" || body["text"] != "same text" { + t.Fatalf("prompt body = %+v", body) + } +} + // TestChatStreamRealV2Shapes replays the exact event frames the installed 2.0.20 // service emitted on /api/event during a live turn (captured, not documented): // a step start, token text delta, usage and a step end whose data carries no diff --git a/internal/opencode/native.go b/internal/opencode/native.go index d3646fc4..e5d7fa3a 100644 --- a/internal/opencode/native.go +++ b/internal/opencode/native.go @@ -195,6 +195,7 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([ var request struct { Prompt string `json:"prompt"` Text string `json:"text"` + ID string `json:"id"` Format json.RawMessage `json:"format"` Parts []struct { Text string `json:"text"` @@ -219,6 +220,9 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([ } } payload := map[string]any{"model": c.models[id], "parts": []map[string]string{{"type": "text", "text": text}}} + if request.ID != "" { + payload["id"] = request.ID + } if len(request.Format) > 0 { supported, err := c.supportsFormat(ctx, request.Format) if err != nil { diff --git a/internal/protocol/chat.go b/internal/protocol/chat.go index 52d4a0e8..c9f9f4ed 100644 --- a/internal/protocol/chat.go +++ b/internal/protocol/chat.go @@ -231,6 +231,10 @@ type ChatPromptRequest struct { Files []string `json:"files,omitempty"` Client string `json:"client,omitempty"` OperationID string `json:"operation_id,omitempty"` + // NativeMessageID is the caller-supplied OpenCode message identity. It is + // sent with the prompt so history can be reconciled without guessing from + // text, time, order or page boundaries. + NativeMessageID string `json:"native_message_id,omitempty"` } // ChatMessage is one stored turn returned by chat_messages. A user turn is a diff --git a/internal/server/chat.go b/internal/server/chat.go index 3d4ddf5c..c0da3f30 100644 --- a/internal/server/chat.go +++ b/internal/server/chat.go @@ -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) } diff --git a/internal/server/chat_hub.go b/internal/server/chat_hub.go index ac430ce7..f761decc 100644 --- a/internal/server/chat_hub.go +++ b/internal/server/chat_hub.go @@ -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 diff --git a/internal/server/chat_room.go b/internal/server/chat_room.go index e9ec97fb..0fbb54d6 100644 --- a/internal/server/chat_room.go +++ b/internal/server/chat_room.go @@ -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 } diff --git a/internal/server/chat_stream.go b/internal/server/chat_stream.go index e6433b0b..91370f56 100644 --- a/internal/server/chat_stream.go +++ b/internal/server/chat_stream.go @@ -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, }) diff --git a/internal/server/chat_test.go b/internal/server/chat_test.go index e9868dd8..312bc221 100644 --- a/internal/server/chat_test.go +++ b/internal/server/chat_test.go @@ -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.