Server: serve a Claude chat's history page from a per-file line index

Reading a history page parsed the whole session file (tens of MB) on every
chat_messages. A per-file index now records which lines build each shown
message; a page re-reads and replays only its own messages' lines, and the
index follows appends by reading only the new bytes. Results match the
whole-file read (compared page by page on synthetic transcripts); shapes the
index cannot replay exactly fall back to the whole-file read.

chat_subscribe may ask (history: true) for the newest page in its reply,
read once the subscription is attached, saving the client a round trip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 14:58:57 +00:00
parent 17baa9e24c
commit 686690f7b4
8 changed files with 1303 additions and 251 deletions
+1 -1
View File
@@ -184,7 +184,7 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
handleChatPrompt(ctx, request, broker, hub, registry, clientID, write)
return true
case protocol.ChatSubscribe:
handleChatSubscribe(request, broker, hub, clientID, write)
handleChatSubscribe(ctx, request, broker, hub, clientID, write)
return true
case protocol.ChatUnqueue:
handleChatUnqueue(ctx, request, hub, write)
+12 -3
View File
@@ -20,18 +20,27 @@ func handleChatMessages(ctx context.Context, request protocol.Message, broker ch
_ = write(response)
return
}
page, err := broker.Messages(ctx, query.SessionID, query.Limit, query.Cursor)
if err != nil {
if err := addChatHistory(ctx, &response, broker, hub, *query); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
_ = write(response)
}
// addChatHistory reads one history page into a reply: a chat_messages reply,
// or a chat_subscribe reply that asked for the newest page.
func addChatHistory(ctx context.Context, response *protocol.Message, broker chatBroker, hub *chatHub, query protocol.ChatQuery) error {
page, err := broker.Messages(ctx, query.SessionID, query.Limit, query.Cursor)
if err != nil {
return err
}
page.Messages = hub.reconcileHistory(query.SessionID, page.Messages)
response.ChatMessages = page.Messages // legacy peers keep working.
response.ChatHistory = &page
// The session's recorded per-turn changes ride with its history, so the
// transcript's change chips survive a reload and a server restart.
response.ChatTurnDiffList = hub.turnDiffList(ctx, query.SessionID)
_ = write(response)
return nil
}
func handleChatSimple(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) {
+16 -2
View File
@@ -2,6 +2,7 @@ package server
import (
"context"
"log"
"strings"
"time"
@@ -233,7 +234,7 @@ func handleChatModelSwitch(ctx context.Context, request protocol.Message, broker
// receives every frame any client's turn produces, its own or another device's.
// It replays the session's still-pending approval requests, if the room holds
// any, so a device that joins late still shows the cards.
func handleChatSubscribe(request protocol.Message, broker chatBroker, hub *chatHub, clientID string, write func(protocol.Message) error) {
func handleChatSubscribe(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, clientID string, write func(protocol.Message) error) {
// The wire body of chat_subscribe is the chat_subscribe field (the app's
// subscribeRequest and docs/cli.md both use it), not the chat_query body the
// other session-scoped commands carry.
@@ -265,7 +266,20 @@ func handleChatSubscribe(request protocol.Message, broker chatBroker, hub *chatH
// session, so a reloaded client draws them as Queued without any device
// storage.
queue := hub.queueFrame(query.SessionID)
_ = write(protocol.Message{Type: protocol.ChatSubscribe, ID: request.ID, ChatQueue: &queue, ChatShown: hub.shownFrame(query.SessionID)})
reply := protocol.Message{Type: protocol.ChatSubscribe, ID: request.ID, ChatQueue: &queue, ChatShown: hub.shownFrame(query.SessionID)}
if query.History {
// Read after attaching, as a chat_messages sent after this reply would
// be: a frame produced meanwhile waits in the subscription's queue.
// A failed read leaves the page out; the client then asks for it with
// chat_messages, which reports the error.
history := protocol.Message{}
if err := addChatHistory(ctx, &history, broker, hub, protocol.ChatQuery{SessionID: query.SessionID, Limit: query.Limit}); err == nil {
reply.ChatMessages, reply.ChatHistory, reply.ChatTurnDiffList = history.ChatMessages, history.ChatHistory, history.ChatTurnDiffList
} else {
log.Printf("chat session %s: history for the subscribe reply: %v", query.SessionID, err)
}
}
_ = write(reply)
if fresh {
go sub.run()
}
+31
View File
@@ -732,6 +732,37 @@ func TestChatMessagesReturnsBrokerHistory(t *testing.T) {
}
}
// A chat_subscribe that asks for history carries the newest page in its reply,
// read once attached, so opening a chat takes one round trip. Without the flag
// the reply carries no history and the broker is not read.
func TestChatSubscribeCarriesTheNewestPageWhenAsked(t *testing.T) {
broker := scriptedChat()
broker.block = true
broker.messages = seededChatMessages()
ctx, _, conn := dialChat(t, broker)
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatSubscribe, ID: "plain", ChatSubscribe: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil {
t.Fatal(err)
}
plain := readUntil(t, ctx, conn, protocol.ChatSubscribe)
if plain.ChatHistory != nil || len(plain.ChatMessages) != 0 {
t.Fatalf("a plain subscribe reply carried history: %+v", plain)
}
if calls, _, _ := broker.messageQuery(); calls != 0 {
t.Fatalf("a plain subscribe read history %d times", calls)
}
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatSubscribe, ID: "with", ChatSubscribe: &protocol.ChatQuery{SessionID: "ses_1", History: true, Limit: 9}}); err != nil {
t.Fatal(err)
}
reply := readUntil(t, ctx, conn, protocol.ChatSubscribe)
if reply.ID != "with" || reply.ChatHistory == nil || reply.ChatQueue == nil {
t.Fatalf("subscribe reply = %+v, want the queue and a history page", reply)
}
assertSeededChatMessages(t, reply.ChatHistory.Messages)
if calls, sessionID, limit := broker.messageQuery(); calls != 1 || sessionID != "ses_1" || limit != 9 {
t.Fatalf("broker query = %d/%q/%d, want 1/ses_1/9", calls, sessionID, limit)
}
}
// TestChatMessagesThroughRealAdapter loads a seeded session through the real
// opencode.Chat broker: the chat_messages handler reads the V2
// GET /api/session/{id}/message fixture once and returns the user + assistant +