Claude history: show the newest 64 MiB of an oversized transcript
A session file over 64 MiB made the whole chat history unavailable. The history now reads only the file's newest 64 MiB (from a whole line) and opens with a notice that earlier turns are left out and how to see them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
@@ -16,9 +17,10 @@ import (
|
||||
)
|
||||
|
||||
// maxTranscriptBytes bounds one history read. A Claude session file grows with
|
||||
// every turn; past this bound the caller is told the truth instead of being
|
||||
// served a silently truncated conversation.
|
||||
const maxTranscriptBytes = 64 << 20
|
||||
// every turn; past this bound only its newest maxTranscriptBytes are read, and
|
||||
// the history says plainly that earlier turns are not shown instead of
|
||||
// refusing the whole chat.
|
||||
var maxTranscriptBytes int64 = 64 << 20
|
||||
|
||||
type transcriptEntry struct {
|
||||
Type string `json:"type"`
|
||||
@@ -54,21 +56,40 @@ type transcriptEntry struct {
|
||||
}
|
||||
|
||||
func readTranscript(path string) ([]transcriptEntry, error) {
|
||||
entries, _, err := readTranscriptTail(path)
|
||||
return entries, err
|
||||
}
|
||||
|
||||
// readTranscriptTail reads the session file's entries, or only those in its
|
||||
// newest maxTranscriptBytes when it is larger. skipped is how many leading
|
||||
// bytes were not read (0 for the whole file); a read that starts mid-file
|
||||
// begins at the first whole line.
|
||||
func readTranscriptTail(path string) (entries []transcriptEntry, skipped int64, err error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
defer file.Close()
|
||||
info, err := file.Stat()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
reader := bufio.NewReaderSize(file, 64<<10)
|
||||
if info.Size() > maxTranscriptBytes {
|
||||
return nil, fmt.Errorf("the Claude session transcript is larger than the %d MiB history limit", maxTranscriptBytes>>20)
|
||||
skipped = info.Size() - maxTranscriptBytes
|
||||
if _, err := file.Seek(skipped, io.SeekStart); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
reader.Reset(file)
|
||||
// The cut lands inside a line: drop that partial line.
|
||||
partial, err := reader.ReadBytes('\n')
|
||||
if err != nil && err != io.EOF {
|
||||
return nil, 0, err
|
||||
}
|
||||
skipped += int64(len(partial))
|
||||
}
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner := bufio.NewScanner(reader)
|
||||
scanner.Buffer(make([]byte, 0, 64<<10), maxLineBytes)
|
||||
var entries []transcriptEntry
|
||||
for scanner.Scan() {
|
||||
line := scanner.Bytes()
|
||||
if len(line) == 0 {
|
||||
@@ -80,9 +101,19 @@ func readTranscript(path string) ([]transcriptEntry, error) {
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
return entries, nil
|
||||
return entries, skipped, nil
|
||||
}
|
||||
|
||||
// earlierHistoryNotice is the first message of a history read from the tail of
|
||||
// an oversized session file: it says how much was left out and where it is.
|
||||
func earlierHistoryNotice(sessionID string, skipped int64, created int64) protocol.ChatMessage {
|
||||
id := sessionID + "#earlier-history"
|
||||
text := fmt.Sprintf("Earlier history is not shown: this session's transcript is larger than %d MiB, so only its newest %d MiB are loaded (%d MiB earlier are left out). The full conversation is in Claude Code's session file; `claude --resume %s` opens it in a terminal.",
|
||||
maxTranscriptBytes>>20, maxTranscriptBytes>>20, (skipped+(1<<20)-1)>>20, sessionID)
|
||||
part := protocol.ChatPart{Type: protocol.ChatPartText, ID: protocol.ChatDeltaPartID(id, protocol.ChatPartText, 0), Text: text}
|
||||
return protocol.ChatMessage{ID: id, SessionID: sessionID, Role: protocol.ChatRoleAssistant, Kind: protocol.ChatMessageKindNotice, CreatedAt: created, Parts: []protocol.ChatPart{part}}
|
||||
}
|
||||
|
||||
// transcriptSession describes a session from its retained file, so the manager
|
||||
@@ -135,7 +166,7 @@ func (s *session) transcriptMessages(limit int, cursor string) (protocol.ChatHis
|
||||
if path == "" {
|
||||
return protocol.ChatHistoryPage{Messages: []protocol.ChatMessage{}}, nil
|
||||
}
|
||||
entries, err := readTranscript(path)
|
||||
entries, skipped, err := readTranscriptTail(path)
|
||||
if err != nil {
|
||||
return protocol.ChatHistoryPage{}, err
|
||||
}
|
||||
@@ -164,6 +195,16 @@ func (s *session) transcriptMessages(limit int, cursor string) (protocol.ChatHis
|
||||
return ids[index]
|
||||
}
|
||||
messages := []protocol.ChatMessage{}
|
||||
if skipped > 0 {
|
||||
created := int64(0)
|
||||
for _, entry := range entries {
|
||||
if stamp, ok := transcriptTime(entry.Timestamp); ok {
|
||||
created = stamp
|
||||
break
|
||||
}
|
||||
}
|
||||
messages = append(messages, earlierHistoryNotice(s.id, skipped, created))
|
||||
}
|
||||
messageIndex := map[string]int{}
|
||||
toolIndex := map[string][2]int{} // tool id -> message/part index
|
||||
addUser := func(entry transcriptEntry, text string, files []protocol.ChatPart) {
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package claude
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// An oversized session file used to refuse the whole history. Now only its
|
||||
// newest maxTranscriptBytes are read, starting at a whole line, and the page
|
||||
// opens with a notice that earlier turns are left out.
|
||||
func TestHistoryOfAnOversizedTranscriptShowsItsNewestTurns(t *testing.T) {
|
||||
const id = "ses_oversized"
|
||||
var entries []map[string]any
|
||||
for i := 0; i < 40; i++ {
|
||||
entries = append(entries, map[string]any{
|
||||
"type": "assistant",
|
||||
"uuid": fmt.Sprintf("a-%d", i),
|
||||
"timestamp": fmt.Sprintf("2026-11-30T10:%02d:00Z", i),
|
||||
"message": map[string]any{
|
||||
"id": fmt.Sprintf("msg_%d", i), "role": "assistant",
|
||||
"content": textBlocks(fmt.Sprintf("answer %d %s", i, strings.Repeat("x", 200))),
|
||||
},
|
||||
})
|
||||
}
|
||||
writeTranscript(t, id, entries)
|
||||
defer func(limit int64) { maxTranscriptBytes = limit }(maxTranscriptBytes)
|
||||
maxTranscriptBytes = 2 << 10
|
||||
|
||||
chat := New(context.Background())
|
||||
s := chat.sessionFor(id, t.TempDir())
|
||||
page, err := s.transcriptMessages(200, "")
|
||||
if err != nil {
|
||||
t.Fatalf("oversized transcript refused: %v", err)
|
||||
}
|
||||
if len(page.Messages) < 3 {
|
||||
t.Fatalf("got %d messages", len(page.Messages))
|
||||
}
|
||||
first := page.Messages[0]
|
||||
if first.Kind != protocol.ChatMessageKindNotice || !strings.Contains(first.Parts[0].Text, "Earlier history is not shown") {
|
||||
t.Fatalf("first message = %+v", first)
|
||||
}
|
||||
last := page.Messages[len(page.Messages)-1]
|
||||
if !strings.HasPrefix(last.Parts[0].Text, "answer 39 ") {
|
||||
t.Fatalf("newest turn missing: %q", last.Parts[0].Text[:20])
|
||||
}
|
||||
for _, message := range page.Messages[1:] {
|
||||
if strings.HasPrefix(message.Parts[0].Text, "answer 0 ") {
|
||||
t.Fatal("the oldest turn was read past the bound")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A file within the bound is read whole, with no notice.
|
||||
func TestHistoryOfASmallTranscriptHasNoNotice(t *testing.T) {
|
||||
const id = "ses_small"
|
||||
writeTranscript(t, id, []map[string]any{
|
||||
{"type": "assistant", "uuid": "a-1", "message": map[string]any{"id": "msg_1", "role": "assistant", "content": textBlocks("hello")}},
|
||||
})
|
||||
chat := New(context.Background())
|
||||
page, err := chat.sessionFor(id, t.TempDir()).transcriptMessages(50, "")
|
||||
if err != nil || len(page.Messages) != 1 || page.Messages[0].Kind != "" {
|
||||
t.Fatalf("page = %+v err=%v", page.Messages, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user