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:
2026-11-30 13:46:09 +00:00
parent b6d06219cd
commit 1772100af2
2 changed files with 120 additions and 11 deletions
+52 -11
View File
@@ -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) {