WIP preserve large chat history recovery

This commit is contained in:
2026-10-18 15:03:18 +01:00
parent f91145001a
commit 998da0e7af
21 changed files with 1088 additions and 75 deletions
+11 -22
View File
@@ -47,6 +47,7 @@ type fakeChat struct {
messageCalls int
messageSessionID string
messageLimit int
messageCursor string
messages []protocol.ChatMessage
permissionCalls int
permissionSessionID string
@@ -123,13 +124,14 @@ func (f *fakeChat) Interrupt(context.Context, string) error {
return f.interruptErr
}
func (f *fakeChat) Messages(_ context.Context, sessionID string, limit int) ([]protocol.ChatMessage, error) {
func (f *fakeChat) Messages(_ context.Context, sessionID string, limit int, cursor string) (protocol.ChatHistoryPage, error) {
f.mu.Lock()
defer f.mu.Unlock()
f.messageCalls++
f.messageSessionID = sessionID
f.messageLimit = limit
return f.messages, nil
f.messageCursor = cursor
return protocol.ChatHistoryPage{Messages: f.messages}, nil
}
// messageQuery reports the recorded chat_messages call count, session id and
@@ -420,6 +422,7 @@ func TestChatPromptWithoutBroker(t *testing.T) {
func TestChatMessagesResumesRetainedState(t *testing.T) {
broker := scriptedChat()
broker.messages = seededChatMessages()
ctx, _, conn := dialChat(t, broker)
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); err != nil {
t.Fatal(err)
@@ -436,27 +439,13 @@ func TestChatMessagesResumesRetainedState(t *testing.T) {
if err := wsjson.Read(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.ChatMessages || response.ID != "r2" || len(response.ChatMessages) != 2 {
if response.Type != protocol.ChatMessages || response.ID != "r2" || response.ChatHistory == nil {
t.Fatalf("resume response = %+v", response)
}
// The prompt is retained as a user turn and the assistant reply beside it, so
// a reconnecting client re-syncs a conversation that still says who sent
// what. The user turn carries the sending client's label (the generic
// fallback when the prompt did not name one).
user := response.ChatMessages[0]
if user.Role != protocol.ChatRoleUser || len(user.Parts) != 1 || user.Parts[0].Type != protocol.ChatPartText || user.Parts[0].Text != "hi" || user.Client != chatClientLabelFallback {
t.Fatalf("resumed user turn = %+v", user)
}
message := response.ChatMessages[1]
if message.ID != "msg_1" || message.Role != protocol.ChatRoleAssistant || len(message.Parts) != 2 {
t.Fatalf("resumed message = %+v", message)
}
if message.Parts[0].Type != protocol.ChatPartText || message.Parts[0].ID != protocol.ChatDeltaPartID("msg_1", protocol.ChatPartText, 0) || message.Parts[0].Text != "hello world" {
t.Fatalf("resumed text part = %+v", message.Parts[0])
}
if message.Parts[1].Type != protocol.ChatPartTool || message.Parts[1].State == nil || message.Parts[1].State.Status != protocol.ChatToolCompleted {
t.Fatalf("resumed tool part = %+v", message.Parts[1])
if calls, _, _ := broker.messageQuery(); calls != 1 {
t.Fatalf("authoritative history was not read: %d calls", calls)
}
assertSeededChatMessages(t, response.ChatMessages)
}
// seededChatMessages is the stored user + assistant + tool session the
@@ -547,12 +536,12 @@ func TestChatMessagesThroughRealAdapter(t *testing.T) {
messageReads.Add(1)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":[
{"id":"msg_user","time":{"created":10},"type":"user","text":"list the files"},
{"id":"msg_assistant","time":{"created":20,"completed":30},"type":"assistant","agent":"build","model":{"id":"deepseek-v4.1-flash","providerID":"opencode-go"},"content":[
{"type":"text","text":"here you go"},
{"type":"reasoning","text":"thinking"},
{"type":"tool","id":"call_1","name":"shell","executed":true,"state":{"status":"completed","input":{"command":"ls"},"content":[{"type":"text","text":"ok"}]},"time":{"created":1,"ran":2,"completed":3}}
]}
]},
{"id":"msg_user","time":{"created":10},"type":"user","text":"list the files"}
],"cursor":{"next":null}}`))
}))
defer fixture.Close()