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
+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 +