package claude import ( "bufio" "context" "encoding/json" "errors" "fmt" "io" "os" "path/filepath" "strconv" "strings" "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) // maxTranscriptBytes bounds one history read. A Claude session file grows with // 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"` Subtype string `json:"subtype"` UUID string `json:"uuid"` IsSidechain bool `json:"isSidechain"` Timestamp string `json:"timestamp"` Cwd string `json:"cwd"` Message struct { ID string `json:"id"` Role string `json:"role"` Model string `json:"model"` Content json.RawMessage `json:"content"` } `json:"message"` ToolUseResult json.RawMessage `json:"toolUseResult"` Compact json.RawMessage `json:"compactMetadata"` // Attachment is set on "attachment" entries; a queued_command with // commandMode "prompt" is a user message the CLI read mid-turn. Prompt is // either a string or an array of content blocks. Attachment struct { Type string `json:"type"` Prompt json.RawMessage `json:"prompt"` CommandMode string `json:"commandMode"` IsMeta bool `json:"isMeta"` Origin json.RawMessage `json:"origin"` } `json:"attachment"` // IsMeta marks a user entry the CLI wrote itself (a skill body, an image // placeholder, a reminder). Origin says where injected input came from: a // peer or subagent report, or a task notification. An operator prompt has // neither. IsMeta bool `json:"isMeta"` Origin json.RawMessage `json:"origin"` } 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, 0, err } defer file.Close() info, err := file.Stat() if err != nil { return nil, 0, err } reader := bufio.NewReaderSize(file, 64<<10) if info.Size() > maxTranscriptBytes { 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(reader) scanner.Buffer(make([]byte, 0, 64<<10), maxLineBytes) for scanner.Scan() { line := scanner.Bytes() if len(line) == 0 { continue } var entry transcriptEntry if json.Unmarshal(line, &entry) == nil { entries = append(entries, entry) } } if err := scanner.Err(); err != nil { return nil, 0, err } 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 // can resolve a registry row after a restart. func transcriptSession(id string) (protocol.ChatSession, bool) { path := findTranscriptFile(id) if path == "" { return protocol.ChatSession{}, false } session := protocol.ChatSession{ID: id, Backend: Backend, ModelProvider: Backend, Tag: protocol.ChatSessionTagOperator} entries, err := readTranscript(path) if err != nil { return session, true } for _, entry := range entries { if entry.Cwd != "" && session.Directory == "" { session.Directory = entry.Cwd } if stamp, ok := transcriptTime(entry.Timestamp); ok { if session.CreatedAt == 0 || stamp < session.CreatedAt { session.CreatedAt = stamp } if stamp > session.UpdatedAt { session.UpdatedAt = stamp } } if entry.Type == "assistant" && entry.Message.Model != "" && !entry.IsSidechain { session.Model = entry.Message.Model } } return session, true } func transcriptTime(text string) (int64, bool) { if strings.TrimSpace(text) == "" { return 0, false } parsed, err := time.Parse(time.RFC3339Nano, text) if err != nil { return 0, false } return parsed.UnixMilli(), true } // transcriptMessages maps the CLI's own session file into chat messages. It // merges the JSONL's one-entry-per-content-block shape by message id, attaches // tool results to the call that produced them, and never invents a turn. func (s *session) transcriptMessages(limit int, cursor string) (protocol.ChatHistoryPage, error) { newest := strings.TrimSpace(cursor) == "" || cursor == "0" // Taken before the file is read: a block completing in between is then // whole in the file, never missing from both (see live.go). overlay := s.liveOverlaySnapshot() page, err := s.transcriptPage(limit, cursor) if err == nil && newest { overlay.apply(&page, s.id) } return page, err } func (s *session) transcriptPage(limit int, cursor string) (protocol.ChatHistoryPage, error) { path := findTranscriptFile(s.id) if path == "" { return protocol.ChatHistoryPage{Messages: []protocol.ChatMessage{}}, nil } entries, skipped, err := readTranscriptTail(path) if err != nil { return protocol.ChatHistoryPage{}, err } s.mu.Lock() prompts := append([]promptRecord(nil), s.prompts...) s.mu.Unlock() promptIDs := map[string][]string{} for _, prompt := range prompts { if prompt.id == "" || prompt.text == "" { continue } promptIDs[prompt.text] = append(promptIDs[prompt.text], prompt.id) } usedPrompts := map[string]int{} takePromptID := func(text string) string { text = strings.TrimSpace(text) ids := promptIDs[text] if len(ids) == 0 { return "" } index := usedPrompts[text] if index >= len(ids) { return "" } usedPrompts[text] = index + 1 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{} ordinals := blockOrdinals{} toolIndex := map[string][2]int{} // tool id -> message/part index addUser := func(entry transcriptEntry, text string, files []protocol.ChatPart) { id := takePromptID(text) if id == "" { id = entry.UUID } if id == "" { return } created, _ := transcriptTime(entry.Timestamp) parts := []protocol.ChatPart{} if text != "" { parts = append(parts, protocol.ChatPart{Type: protocol.ChatPartText, ID: id + "#text", Text: text}) } parts = append(parts, files...) messages = append(messages, protocol.ChatMessage{ID: id, SessionID: s.id, Role: protocol.ChatRoleUser, CreatedAt: created, Parts: parts}) } injectedSeen := map[string]bool{} addInjected := func(entry transcriptEntry, item injected) { if item.id == "" || injectedSeen[item.id] { return } injectedSeen[item.id] = true created, _ := transcriptTime(entry.Timestamp) messages = append(messages, item.message(s.id, created)) } for _, entry := range entries { if entry.IsSidechain { continue } switch entry.Type { case "assistant": messageID := strings.TrimSpace(entry.Message.ID) if messageID == "" { messageID = entry.UUID } if messageID == "" { continue } created, _ := transcriptTime(entry.Timestamp) index, exists := messageIndex[messageID] if !exists { messages = append(messages, protocol.ChatMessage{ID: messageID, SessionID: s.id, Role: protocol.ChatRoleAssistant, Model: entry.Message.Model, CreatedAt: created, Parts: []protocol.ChatPart{}}) index = len(messages) - 1 messageIndex[messageID] = index } if entry.Message.Model != "" { messages[index].Model = entry.Message.Model } blocks := decodeBlocks(entry.Message.Content) for _, block := range blocks { switch block.Type { case "text": // Every block of a kind takes an ordinal, empty or not, as // the live stream numbers them when they start: a part keeps // one id live and in history. ordinal := ordinals.next(messageID, protocol.ChatPartText) if strings.TrimSpace(block.Text) == "" { continue } part := protocol.ChatPart{Type: protocol.ChatPartText, ID: protocol.ChatDeltaPartID(messageID, protocol.ChatPartText, ordinal), Text: block.Text} messages[index].Parts = append(messages[index].Parts, part) case "thinking": // An empty (display-omitted) thinking block often precedes // the one with text; it is counted, not shown. ordinal := ordinals.next(messageID, protocol.ChatPartReasoning) if strings.TrimSpace(block.Thinking) == "" { continue } part := protocol.ChatPart{Type: protocol.ChatPartReasoning, ID: protocol.ChatDeltaPartID(messageID, protocol.ChatPartReasoning, ordinal), Text: block.Thinking} messages[index].Parts = append(messages[index].Parts, part) case "tool_use": if block.ID == "" { continue } part := protocol.ChatPart{ Type: protocol.ChatPartTool, ID: block.ID, Name: block.Name, State: &protocol.ChatToolState{ Status: protocol.ChatToolRunning, Input: truncateJSON(block.Input, 64<<10), }, } messages[index].Parts = append(messages[index].Parts, part) toolIndex[block.ID] = [2]int{index, len(messages[index].Parts) - 1} } } case "user": origin := decodeOrigin(entry.Origin) if len(entry.Message.Content) > 0 && entry.Message.Content[0] == '"' { // A string user entry is harness input (local command output, a // reminder, a task notification or an agent's report), never a // turn the operator sent. Reports and notices are shown as such. var raw string if json.Unmarshal(entry.Message.Content, &raw) == nil { if item := classifyInjected(raw, origin, entry.IsMeta); item.class == classAgentReport || item.class == classNotice { addInjected(entry, item) } } continue } blocks := decodeBlocks(entry.Message.Content) var text string files := []protocol.ChatPart{} hasResult := false for _, block := range blocks { switch block.Type { case "tool_result": hasResult = true applyTranscriptToolResult(&messages, toolIndex, block) case "text": if strings.TrimSpace(block.Text) == "" { continue } if text != "" { text += "\n" } text += block.Text case "image": if block.Source != nil && block.Source.Data != "" && len(block.Source.Data) <= 512<<10 { files = append(files, protocol.ChatPart{Type: protocol.ChatPartFile, Mime: block.Source.MediaType, Name: "image", URI: "data:" + block.Source.MediaType + ";base64," + block.Source.Data}) } } } if hasResult || (text == "" && len(files) == 0) { continue } if text != "" { switch item := classifyInjected(text, origin, entry.IsMeta); item.class { case classAgentReport, classNotice: addInjected(entry, item) continue case classHidden: continue default: text = item.text } } else if entry.IsMeta { continue } addUser(entry, text, files) case "attachment": // A message the operator sent while the turn was running is recorded // as a queued_command attachment where the CLI surfaced it to the // model, so it is the user's turn at that point of the transcript. // Other queued commands (task notifications, and agent reports the // CLI also queues as "prompt" with an origin) are harness input: // they are shown as notices and report cards, never as the operator's. if entry.Attachment.Type != "queued_command" { continue } mode := entry.Attachment.CommandMode if mode != "prompt" && mode != "task-notification" { continue } var text string for _, block := range decodeBlocks(entry.Attachment.Prompt) { if block.Type == "text" && strings.TrimSpace(block.Text) != "" { if text != "" { text += "\n" } text += block.Text } } if strings.TrimSpace(text) == "" { continue } origin := decodeOrigin(entry.Attachment.Origin) if mode == "task-notification" && origin.Kind == "" { origin.Kind = "task-notification" } switch item := classifyInjected(text, origin, entry.Attachment.IsMeta); item.class { case classAgentReport, classNotice: addInjected(entry, item) continue case classHidden: continue default: text = item.text } addUser(entry, text, []protocol.ChatPart{}) } } // Drop assistant messages that ended with no renderable part (for example a // signature-only thinking entry). filtered := messages[:0] for _, message := range messages { if len(message.Parts) > 0 { filtered = append(filtered, message) } } messages = filtered if limit <= 0 { limit = 50 } if limit > 100 { limit = 100 } offset := 0 if strings.TrimSpace(cursor) != "" { if parsed, err := strconv.Atoi(cursor); err == nil && parsed >= 0 { offset = parsed } } if offset > len(messages) { offset = len(messages) } end := len(messages) - offset start := end - limit if start < 0 { start = 0 } page := protocol.ChatHistoryPage{Messages: append([]protocol.ChatMessage{}, messages[start:end]...)} if start > 0 { page.HasMore = true page.NextCursor = strconv.Itoa(offset + len(page.Messages)) } return page, nil } // blockOrdinals counts each message's text and thinking blocks by kind. type blockOrdinals map[string]map[string]int func (o blockOrdinals) next(messageID, kind string) int { counts := o[messageID] if counts == nil { counts = map[string]int{} o[messageID] = counts } ordinal := counts[kind] counts[kind] = ordinal + 1 return ordinal } func applyTranscriptToolResult(messages *[]protocol.ChatMessage, toolIndex map[string][2]int, block contentBlock) { position, ok := toolIndex[block.ToolUseID] if !ok || position[0] >= len(*messages) { return } message := &(*messages)[position[0]] if position[1] >= len(message.Parts) { return } part := &message.Parts[position[1]] if part.State == nil { part.State = &protocol.ChatToolState{} } text, images := toolResultContent(block.Content) status := protocol.ChatToolCompleted var toolErr *protocol.ChatError exit := exitCode(text) if block.IsError || (exit != nil && *exit != 0) { status = protocol.ChatToolError detail := strings.TrimSpace(text) if detail == "" { detail = "the tool returned an error" } toolErr = &protocol.ChatError{Type: "tool", Message: boundedText(detail, 2<<10)} } part.State.Status = status part.State.Error = toolErr if text != "" { part.State.Content = append(part.State.Content, protocol.ChatToolContent{Type: "text", Text: boundedText(text, 128<<10)}) } part.State.Content = append(part.State.Content, images...) } // Messages serves the authoritative transcript history. func (c *Chat) Messages(ctx context.Context, id string, limit int, cursor string) (protocol.ChatHistoryPage, error) { s := c.session(id) if s == nil { meta, ok := transcriptSession(id) if !ok { return protocol.ChatHistoryPage{}, errors.New("unknown Claude session " + id) } s = c.sessionFor(id, meta.Directory) } return s.transcriptMessages(limit, cursor) } // sessionTranscriptPath is used by tests and diagnostics. func sessionTranscriptPath(id string) string { path := findTranscriptFile(id) if path == "" { return "" } return filepath.Clean(path) }