Files
pipelines/internal/claude/translate.go
T
alan c810437aa5 Claude Code chats survive server restarts in a detachable host
A pipelines-claude-host process owns each claude -p CLI and its pipes,
spools its stream-json output with offsets and serves a private Unix
socket. The server detaches on shutdown and reattaches on start,
replaying the spool to rebuild turns, approvals, questions, background
tasks and the live model. Explicit close, idle release and CLI exit stop
the host; a crashed host falls back to --resume with a notice.
2026-11-04 16:09:56 +00:00

923 lines
28 KiB
Go

package claude
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// ---- CLI line envelope -----------------------------------------------------
type lineEnvelope struct {
Type string `json:"type"`
Subtype string `json:"subtype"`
UUID string `json:"uuid"`
SessionID string `json:"session_id"`
ParentToolUseID string `json:"parent_tool_use_id"`
Message json.RawMessage `json:"message"`
Event json.RawMessage `json:"event"`
RequestID string `json:"request_id"`
Request json.RawMessage `json:"request"`
Response json.RawMessage `json:"response"`
Status string `json:"status"`
CompactMetadata json.RawMessage `json:"compactMetadata"`
Result string `json:"result"`
IsError bool `json:"is_error"`
TotalCostUSD float64 `json:"total_cost_usd"`
Usage json.RawMessage `json:"usage"`
// Origin and IsMeta mark a user line the harness injected (an agent report,
// a task notification, a reminder); see classifyInjected.
Origin json.RawMessage `json:"origin"`
IsMeta bool `json:"isMeta"`
// RateLimitInfo is the payload of a rate_limit_event line (see quota.go).
RateLimitInfo json.RawMessage `json:"rate_limit_info"`
}
type contentBlock struct {
Type string `json:"type"`
Text string `json:"text"`
Thinking string `json:"thinking"`
ID string `json:"id"`
Name string `json:"name"`
Input json.RawMessage `json:"input"`
ToolUseID string `json:"tool_use_id"`
Content json.RawMessage `json:"content"`
IsError bool `json:"is_error"`
Source *struct {
Type string `json:"type"`
MediaType string `json:"media_type"`
Data string `json:"data"`
} `json:"source"`
}
type messageBody struct {
ID string `json:"id"`
Role string `json:"role"`
Model string `json:"model"`
Content json.RawMessage `json:"content"`
}
type toolRequest struct {
Subtype string `json:"subtype"`
ToolName string `json:"tool_name"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
Input json.RawMessage `json:"input"`
PermissionSuggestions json.RawMessage `json:"permission_suggestions"`
ToolUseID string `json:"tool_use_id"`
DecisionReason string `json:"decision_reason"`
}
type streamEvent struct {
Type string `json:"type"`
Index int `json:"index"`
Message *messageBody `json:"message"`
ContentBlock *contentBlock `json:"content_block"`
Delta *struct {
Type string `json:"type"`
Text string `json:"text"`
Thinking string `json:"thinking"`
PartialJSON string `json:"partial_json"`
} `json:"delta"`
}
// ---- Dispatch --------------------------------------------------------------
func (s *session) handleLine(p *process, line []byte) {
var envelope lineEnvelope
if err := json.Unmarshal(line, &envelope); err != nil {
return
}
// Subagent traffic is a sidechain: its text and tools are not the operator's
// main turn, and history excludes the same entries (isSidechain).
if envelope.ParentToolUseID != "" {
return
}
switch envelope.Type {
case "control_request":
s.handleControlRequest(p, envelope)
case "control_response":
s.handleControlResponse(p, envelope)
case "stream_event":
s.adoptFollowUpTurn()
s.handleStreamEvent(envelope)
case "assistant":
s.adoptFollowUpTurn()
s.handleAssistant(envelope)
case "user":
s.handleUser(envelope)
case "result":
s.handleResult(envelope)
case "rate_limit_event":
s.handleRateLimit(envelope.RateLimitInfo)
case "system":
if envelope.Subtype == "task_notification" {
s.finishNotifiedTask(line)
return
}
s.handleSystem(envelope)
}
}
func (s *session) handleControlResponse(p *process, envelope lineEnvelope) {
var reply struct {
Subtype string `json:"subtype"`
RequestID string `json:"request_id"`
Response json.RawMessage `json:"response"`
Error string `json:"error"`
}
if json.Unmarshal(envelope.Response, &reply) != nil || reply.RequestID == "" {
return
}
if p == nil {
return
}
p.mu.Lock()
ch := p.pending[reply.RequestID]
delete(p.pending, reply.RequestID)
p.mu.Unlock()
if ch == nil {
return
}
if reply.Subtype == "error" {
select {
case ch <- controlReply{err: reply.Error}:
default:
}
return
}
select {
case ch <- controlReply{response: reply.Response}:
default:
}
}
func (s *session) handleControlRequest(p *process, envelope lineEnvelope) {
var request toolRequest
if json.Unmarshal(envelope.Request, &request) != nil {
return
}
switch request.Subtype {
case "can_use_tool":
s.receivePermission(p, envelope.RequestID, request)
case "hook_callback", "mcp_message":
s.respondUnsupported(p, envelope.RequestID)
default:
s.respondUnsupported(p, envelope.RequestID)
}
}
func (s *session) respondUnsupported(p *process, requestID string) {
if p == nil || requestID == "" {
return
}
_ = p.write(map[string]any{
"type": "control_response",
"response": map[string]any{
"subtype": "error",
"request_id": requestID,
"error": "This Pipelines server does not support that Claude Code interaction",
},
})
}
// ---- Streaming text --------------------------------------------------------
func (s *session) handleStreamEvent(envelope lineEnvelope) {
var event streamEvent
if json.Unmarshal(envelope.Event, &event) != nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
switch event.Type {
case "message_start":
s.streamID = ""
if event.Message != nil {
s.streamID = event.Message.ID
}
if s.streamID == "" {
s.streamID = envelope.UUID
}
s.resetStreamLocked()
case "content_block_start":
if event.ContentBlock == nil {
return
}
kind := streamKind(event.ContentBlock.Type)
if kind == "" {
return
}
ordinal := s.streamOrdinals[kind]
s.streamOrdinals[kind] = ordinal + 1
s.streamBlocks[event.Index] = blockState{kind: kind, ordinal: ordinal}
case "content_block_delta":
if event.Delta == nil {
return
}
block, ok := s.streamBlocks[event.Index]
kind := block.kind
if !ok {
kind = streamKind(deltaKind(event.Delta.Type))
if kind == "" {
return
}
block = blockState{kind: kind, ordinal: s.streamOrdinals[kind]}
s.streamOrdinals[kind]++
s.streamBlocks[event.Index] = block
}
var delta string
switch event.Delta.Type {
case "text_delta":
if block.kind != protocol.ChatPartText {
return
}
delta = event.Delta.Text
case "thinking_delta":
if block.kind != protocol.ChatPartReasoning {
return
}
delta = event.Delta.Thinking
default:
return
}
if delta == "" {
return
}
s.streamText[event.Index] += delta
messageID := s.streamID
if messageID == "" {
return
}
partID := protocol.ChatDeltaPartID(messageID, block.kind, block.ordinal)
s.broadcastLocked(protocol.ChatEventFrame{
SessionID: s.id,
NativeTurnID: s.turnID,
MessageID: messageID,
Kind: block.kind,
Delta: delta,
Part: &protocol.ChatPart{ID: partID, Type: block.kind},
})
case "message_stop":
// Keep the assembled blocks: the CLI sends the completed assistant
// message after message_stop, and that message must not repeat text
// already delivered as deltas. message_start resets them for the next one.
}
}
func streamKind(blockType string) string {
switch blockType {
case "text":
return protocol.ChatPartText
case "thinking", "reasoning":
return protocol.ChatPartReasoning
default:
return ""
}
}
func deltaKind(deltaType string) string {
switch deltaType {
case "text_delta":
return "text"
case "thinking_delta":
return "thinking"
default:
return ""
}
}
// resetStreamLocked forgets the assembled stream blocks of the previous message.
func (s *session) resetStreamLocked() {
s.streamBlocks = map[int]blockState{}
s.streamText = map[int]string{}
s.streamOrdinals = map[string]int{}
s.streamClaimed = map[int]bool{}
}
// claimStreamedLocked finds the streamed block a completed text or thinking block
// repeats. The CLI sends one assistant event per content block, so a block's
// index within the event (usually 0) is not its stream index: a text block that
// follows a thinking block streams at index 1 but arrives alone in its event.
// The match is the first unclaimed streamed block of the same kind whose text
// is a prefix of the completed text, preferring the event-local index. An
// unclaimed streamed block of that kind that disagrees is returned too, so the
// caller suppresses the repeat instead of emitting the whole answer again.
func (s *session) claimStreamedLocked(kind, full string, local int) (blockState, string, bool) {
indices := make([]int, 0, len(s.streamBlocks)+1)
indices = append(indices, local)
others := make([]int, 0, len(s.streamBlocks))
for index := range s.streamBlocks {
if index != local {
others = append(others, index)
}
}
sort.Ints(others)
indices = append(indices, others...)
disagree := -1
for _, index := range indices {
block := s.streamBlocks[index]
streamed := s.streamText[index]
if block.kind != kind || s.streamClaimed[index] || streamed == "" {
continue
}
if strings.HasPrefix(full, streamed) {
s.streamClaimed[index] = true
return block, streamed, true
}
if disagree < 0 {
disagree = index
}
}
if disagree >= 0 {
s.streamClaimed[disagree] = true
return s.streamBlocks[disagree], s.streamText[disagree], true
}
return blockState{}, "", false
}
// broadcastLocked emits while the session lock is already held: the caller is a
// stream-delta path that owns state it just updated.
func (s *session) broadcastLocked(frame protocol.ChatEventFrame) {
if s.silent {
return
}
targets := make([]chan protocol.ChatEventFrame, 0, len(s.subs))
for ch := range s.subs {
targets = append(targets, ch)
}
for _, ch := range targets {
select {
case ch <- frame:
default:
}
}
}
// ---- Completed messages ----------------------------------------------------
func (s *session) handleAssistant(envelope lineEnvelope) {
var body messageBody
if json.Unmarshal(envelope.Message, &body) != nil {
return
}
messageID := strings.TrimSpace(body.ID)
if messageID == "" {
messageID = envelope.UUID
}
if messageID == "" {
return
}
blocks := decodeBlocks(body.Content)
s.mu.Lock()
if messageID != s.streamID {
// A message that was not streamed (or a new one): nothing streamed can
// be aligned with it. Later events of the same message keep this state.
s.resetStreamLocked()
s.streamID = messageID
}
if body.Model != "" {
s.modelActual = body.Model
}
// Align the completed message with what was streamed, then emit only the
// remainder: the CLI sends deltas first and the full block afterwards.
var frames []protocol.ChatEventFrame
for index, block := range blocks {
switch block.Type {
case "text", "thinking":
kind := protocol.ChatPartText
full := block.Text
if block.Type == "thinking" {
kind = protocol.ChatPartReasoning
full = block.Thinking
}
if strings.TrimSpace(full) == "" {
continue
}
state, streamed, matched := s.claimStreamedLocked(kind, full, index)
if !matched {
state = blockState{kind: kind, ordinal: s.streamOrdinals[kind]}
s.streamOrdinals[kind]++
}
delta := full
if strings.HasPrefix(full, streamed) {
delta = full[len(streamed):]
} else if streamed != "" {
// The stream and the final block disagree; never duplicate the
// whole answer. The authoritative transcript carries the full text.
delta = ""
}
if delta == "" {
continue
}
frames = append(frames, protocol.ChatEventFrame{
SessionID: s.id,
NativeTurnID: s.turnID,
MessageID: messageID,
Kind: kind,
Delta: delta,
Part: &protocol.ChatPart{ID: protocol.ChatDeltaPartID(messageID, kind, state.ordinal), Type: kind},
})
case "tool_use":
if block.ID == "" {
continue
}
s.toolMessages[block.ID] = messageID
input := truncateJSON(block.Input, 64<<10)
frames = append(frames, protocol.ChatEventFrame{
SessionID: s.id,
NativeTurnID: s.turnID,
MessageID: messageID,
Kind: protocol.ChatEventTool,
Part: &protocol.ChatPart{
Type: protocol.ChatPartTool,
ID: block.ID,
Name: block.Name,
State: &protocol.ChatToolState{
Status: protocol.ChatToolRunning,
Input: input,
},
},
})
var shell *protocol.ChatShell
switch block.Name {
case "Bash":
shell = s.recordShellLocked(block.ID, bashCommand(input))
if shell != nil && backgroundRequested(input) {
s.shells[block.ID].background = true
}
case "Agent", "Task":
// A subagent usually runs in the background; its immediate
// result says so, and a foreground one simply finishes.
shell = s.recordShellLocked(block.ID, agentLabel(input))
if shell != nil {
s.shells[block.ID].background = true
}
}
if shell != nil {
frames = append(frames, protocol.ChatEventFrame{SessionID: s.id, Kind: protocol.ChatEventShell, Shell: shell})
}
}
}
framesCopy := frames
s.mu.Unlock()
for _, frame := range framesCopy {
s.emit(frame)
}
}
func (s *session) handleUser(envelope lineEnvelope) {
var body messageBody
if json.Unmarshal(envelope.Message, &body) != nil {
return
}
blocks := decodeBlocks(body.Content)
origin := decodeOrigin(envelope.Origin)
for _, block := range blocks {
if block.Type != "text" {
continue
}
if strings.Contains(block.Text, "<task-notification>") {
s.finishBackgroundTasks(block.Text)
}
// The operator's own prompts are published when they are sent, so the
// stream echo is never drawn again. Harness input that the operator did
// not send is drawn as a report card or a notice, never as a prompt.
s.emitInjected(classifyInjected(block.Text, origin, envelope.IsMeta))
}
for _, block := range blocks {
if block.Type != "tool_result" || block.ToolUseID == "" {
continue
}
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
message := "the tool returned an error"
if exit != nil && *exit != 0 {
message = fmt.Sprintf("Exit code %d", *exit)
}
if strings.TrimSpace(text) != "" {
message = boundedText(strings.TrimSpace(text), 2<<10)
}
toolErr = &protocol.ChatError{Type: "tool", Message: message}
}
content := []protocol.ChatToolContent{}
if text != "" {
content = append(content, protocol.ChatToolContent{Type: "text", Text: boundedText(text, 128<<10)})
}
content = append(content, images...)
s.mu.Lock()
messageID := s.toolMessages[block.ToolUseID]
shell := s.shells[block.ToolUseID]
s.mu.Unlock()
part := protocol.ChatPart{
Type: protocol.ChatPartTool,
ID: block.ToolUseID,
State: &protocol.ChatToolState{
Status: status,
Content: content,
Error: toolErr,
},
}
s.emit(protocol.ChatEventFrame{
SessionID: s.id,
NativeTurnID: s.turnID,
MessageID: messageID,
Kind: protocol.ChatEventTool,
Part: &part,
})
if shell != nil {
var frame *protocol.ChatShell
s.mu.Lock()
if snapshot, ok := s.shells[block.ToolUseID]; ok && snapshot.background && launchedInBackground(text) {
// Started, not finished: it stays running until Claude Code
// reports the task's completion.
snapshot.output = text
snapshot.size = int64(len(text))
} else if ok {
snapshot.shell.Status = protocol.ChatShellExited
snapshot.shell.Exit = exit
if text != "" && len(text) >= len(snapshot.output) {
snapshot.output = text
snapshot.size = int64(len(text))
if len(snapshot.output) > commandOutputCap {
snapshot.output = snapshot.output[:commandOutputCap]
}
}
frame = &protocol.ChatShell{ID: snapshot.shell.ID, Status: snapshot.shell.Status, Command: snapshot.shell.Command, Exit: snapshot.shell.Exit}
}
s.mu.Unlock()
if frame != nil {
s.emit(protocol.ChatEventFrame{SessionID: s.id, Kind: protocol.ChatEventShell, Shell: frame})
}
}
}
}
func (s *session) handleResult(envelope lineEnvelope) {
s.mu.Lock()
turn := s.turnID
running := s.running
proc := s.proc
if s.silent {
proc = nil // replayed: the live usage query is made once caught up
}
s.running = false
s.turnID = ""
// Messages written mid-turn that the CLI did not fold into this turn run
// next as their own turn; its output adopts a new turn id.
s.awaitFollowUp = s.steered > 0
s.steered = 0
s.approvals = map[string]*pendingApproval{}
s.forms = map[string]*pendingForm{}
s.resetStreamLocked()
s.mu.Unlock()
if envelope.Subtype != "success" && envelope.Subtype != "error_during_execution" && envelope.Subtype != "" {
message := strings.TrimSpace(envelope.Result)
if message == "" {
message = "Claude Code ended the turn: " + envelope.Subtype
}
s.emit(protocol.ChatEventFrame{
SessionID: s.id,
NativeTurnID: turn,
MessageID: "claude-result-" + turn,
Kind: protocol.ChatEventTool,
Part: &protocol.ChatPart{
Type: protocol.ChatPartTool,
ID: "claude-result-" + turn,
Name: "Claude Code",
State: &protocol.ChatToolState{Status: protocol.ChatToolError, Error: &protocol.ChatError{Type: envelope.Subtype, Message: boundedText(message, 2<<10)}},
},
})
}
if running {
s.emit(protocol.ChatEventFrame{SessionID: s.id, NativeTurnID: turn, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, TurnID: turn}})
}
if proc != nil {
go s.refreshUsage(proc)
}
}
func (s *session) handleSystem(envelope lineEnvelope) {
switch envelope.Subtype {
case "status":
if envelope.Status == "compacting" {
s.emit(protocol.ChatEventFrame{SessionID: s.id, NativeTurnID: s.currentTurn(), Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{Status: "running"}})
}
case "compact_boundary":
s.emit(protocol.ChatEventFrame{SessionID: s.id, NativeTurnID: s.currentTurn(), Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{Status: "completed"}})
case "init":
// The CLI re-emits init at each turn; keep only the authoritative identity.
s.adoptFollowUpTurn()
if envelope.SessionID != "" && envelope.SessionID != s.id {
s.mu.Lock()
s.lastErr = "Claude Code opened unexpected session " + envelope.SessionID
s.mu.Unlock()
}
}
}
func (s *session) currentTurn() string {
s.mu.Lock()
defer s.mu.Unlock()
return s.turnID
}
// ---- Tool helpers ----------------------------------------------------------
func decodeBlocks(raw json.RawMessage) []contentBlock {
if len(raw) == 0 {
return nil
}
var blocks []contentBlock
if json.Unmarshal(raw, &blocks) == nil {
return blocks
}
var text string
if json.Unmarshal(raw, &text) == nil && text != "" {
return []contentBlock{{Type: "text", Text: text}}
}
return nil
}
func toolResultContent(raw json.RawMessage) (string, []protocol.ChatToolContent) {
var text string
if json.Unmarshal(raw, &text) == nil {
return text, nil
}
blocks := decodeBlocks(raw)
var builder strings.Builder
images := []protocol.ChatToolContent{}
for _, block := range blocks {
switch block.Type {
case "text":
if builder.Len() > 0 {
builder.WriteString("\n")
}
builder.WriteString(block.Text)
case "image":
if block.Source != nil && block.Source.Data != "" && len(block.Source.Data) <= 512<<10 {
images = append(images, protocol.ChatToolContent{
Type: "file",
Mime: block.Source.MediaType,
Name: "image",
URI: "data:" + block.Source.MediaType + ";base64," + block.Source.Data,
})
}
}
}
return builder.String(), images
}
var exitLine = regexp.MustCompile(`(?m)^Exit code (\d+)\s*$`)
// exitCode reads the CLI's own "Exit code N" prefix. A successful command with
// no prefix is 0; an error without a numeric code stays unknown.
func exitCode(text string) *int {
if match := exitLine.FindStringSubmatch(text); len(match) == 2 {
if value, err := strconv.Atoi(match[1]); err == nil {
return &value
}
}
if strings.TrimSpace(text) != "" && !strings.HasPrefix(strings.TrimSpace(text), "Exit code") {
zero := 0
return &zero
}
return nil
}
func bashCommand(input json.RawMessage) string {
var parsed struct {
Command string `json:"command"`
}
if json.Unmarshal(input, &parsed) == nil {
return parsed.Command
}
return ""
}
func truncateJSON(raw json.RawMessage, limit int) json.RawMessage {
if len(raw) == 0 {
return json.RawMessage(`{}`)
}
if len(raw) > limit {
wrapped, _ := json.Marshal(map[string]any{"truncated": true, "preview": string(raw[:limit])})
return wrapped
}
return raw
}
func boundedText(text string, limit int) string {
if len(text) <= limit {
return text
}
return text[:limit] + "\n… (truncated)"
}
// ---- Shells ----------------------------------------------------------------
const commandOutputCap = 128 << 10
type shellSnapshot struct {
shell protocol.ChatShell
output string
size int64
order uint64
// background marks a command or subagent that may outlive its tool call:
// its immediate result only says it started.
background bool
}
// backgroundRequested reports a Bash call asked to run in the background.
func backgroundRequested(input json.RawMessage) bool {
var parsed struct {
Background bool `json:"run_in_background"`
}
return json.Unmarshal(input, &parsed) == nil && parsed.Background
}
// agentLabel names a subagent the way the operator recognises it.
func agentLabel(input json.RawMessage) string {
var parsed struct {
Description string `json:"description"`
Type string `json:"subagent_type"`
}
_ = json.Unmarshal(input, &parsed)
label := strings.TrimSpace(parsed.Description)
if label == "" {
label = strings.TrimSpace(parsed.Type)
}
if label == "" {
return "Agent"
}
return "Agent: " + label
}
// launchedInBackground recognises Claude Code's immediate result for a task
// that keeps running after its tool call returns.
func launchedInBackground(text string) bool {
return strings.Contains(text, "running in background with ID") ||
strings.HasPrefix(strings.TrimSpace(text), "Async agent launched")
}
var (
taskNotificationPattern = regexp.MustCompile(`(?s)<task-notification>(.*?)</task-notification>`)
taskToolUsePattern = regexp.MustCompile(`<tool-use-id>\s*([^<\s]+)\s*</tool-use-id>`)
taskSummaryPattern = regexp.MustCompile(`(?s)<summary>(.*?)</summary>`)
taskExitPattern = regexp.MustCompile(`exit code (-?\d+)`)
)
// finishNotifiedTask handles the SDK's structured completion message for a
// background task, the counterpart of the <task-notification> text.
func (s *session) finishNotifiedTask(line []byte) {
var notice struct {
ToolUseID string `json:"tool_use_id"`
Status string `json:"status"`
Summary string `json:"summary"`
}
if json.Unmarshal(line, &notice) != nil || notice.ToolUseID == "" {
return
}
s.emitInjected(taskNoticeLine(notice.ToolUseID, notice.Status, strings.Join(strings.Fields(notice.Summary), " ")))
s.finishBackgroundTasks(fmt.Sprintf(
"<task-notification><tool-use-id>%s</tool-use-id><status>%s</status><summary>%s</summary></task-notification>",
notice.ToolUseID, notice.Status, notice.Summary,
))
}
// finishBackgroundTasks marks the tasks a Claude Code task notification
// reports as finished, and publishes each one's final state.
func (s *session) finishBackgroundTasks(text string) {
var frames []*protocol.ChatShell
s.mu.Lock()
for _, match := range taskNotificationPattern.FindAllStringSubmatch(text, -1) {
toolID := taskToolUsePattern.FindStringSubmatch(match[1])
if toolID == nil {
continue
}
snapshot, ok := s.shells[toolID[1]]
if !ok || snapshot.shell.Status != protocol.ChatShellRunning {
continue
}
snapshot.shell.Status = protocol.ChatShellExited
if summary := taskSummaryPattern.FindStringSubmatch(match[1]); summary != nil {
line := strings.TrimSpace(summary[1])
if code := taskExitPattern.FindStringSubmatch(line); code != nil {
if exit, err := strconv.Atoi(code[1]); err == nil {
snapshot.shell.Exit = &exit
}
}
if line != "" {
snapshot.output = strings.TrimRight(snapshot.output, "\n") + "\n" + line
snapshot.size = int64(len(snapshot.output))
}
}
frames = append(frames, &protocol.ChatShell{ID: snapshot.shell.ID, Status: snapshot.shell.Status, Command: snapshot.shell.Command, Exit: snapshot.shell.Exit})
}
s.mu.Unlock()
for _, frame := range frames {
s.emit(protocol.ChatEventFrame{SessionID: s.id, Kind: protocol.ChatEventShell, Shell: frame})
}
}
func shellID(toolID string) string {
sum := sha256.Sum256([]byte("claude\x00" + toolID))
return "sh_claude_" + hex.EncodeToString(sum[:16])
}
// recordShellLocked retains a running Bash command. Called with s.mu held; the
// caller emits the returned frame after the tool part, in that order.
func (s *session) recordShellLocked(toolID, command string) *protocol.ChatShell {
if toolID == "" {
return nil
}
id := shellID(toolID)
s.shellOrder = append(s.shellOrder, id)
shell := protocol.ChatShell{ID: id, Status: protocol.ChatShellRunning, Command: command}
s.shells[toolID] = &shellSnapshot{shell: shell, order: uint64(len(s.shellOrder))}
return &shell
}
// ---- Usage -----------------------------------------------------------------
type usageModel struct {
InputTokens int64 `json:"inputTokens"`
OutputTokens int64 `json:"outputTokens"`
CacheReadInputTokens int64 `json:"cacheReadInputTokens"`
CacheCreationInputTokens int64 `json:"cacheCreationInputTokens"`
CostUSD float64 `json:"costUSD"`
ContextWindow int64 `json:"contextWindow"`
}
type sessionUsage struct {
Session struct {
TotalCostUSD float64 `json:"total_cost_usd"`
ModelUsage map[string]usageModel `json:"model_usage"`
} `json:"session"`
}
type contextUsage struct {
TotalTokens int64 `json:"totalTokens"`
MaxTokens int64 `json:"maxTokens"`
}
// refreshUsage asks the CLI for its own cumulative session totals. It runs off
// the reader goroutine because a control request waits for a response that only
// the reader can deliver.
func (s *session) refreshUsage(p *process) {
ctx, cancel := contextWithTimeout(controlRequestTimeout)
defer cancel()
raw, err := p.request(ctx, "get_usage", map[string]any{"skip_behaviors": true})
if err != nil {
return
}
var totals sessionUsage
if json.Unmarshal(raw, &totals) != nil {
return
}
var input, output, cacheRead, cacheCreate int64
for _, model := range totals.Session.ModelUsage {
input += model.InputTokens
output += model.OutputTokens
cacheRead += model.CacheReadInputTokens
cacheCreate += model.CacheCreationInputTokens
}
total := input + output + cacheRead + cacheCreate
cost := totals.Session.TotalCostUSD
usage := &protocol.ChatUsage{
Scope: "session",
InputTokens: int(input),
OutputTokens: int(output),
TotalTokens: &total,
CostUSD: &cost,
}
ctx2, cancel2 := contextWithTimeout(controlRequestTimeout)
defer cancel2()
if rawContext, err := p.request(ctx2, "get_context_usage", map[string]any{}); err == nil {
var context contextUsage
if json.Unmarshal(rawContext, &context) == nil {
if context.MaxTokens > 0 {
usage.ContextWindow = &context.MaxTokens
usage.ContextTokens = &context.TotalTokens
}
}
}
s.emit(protocol.ChatEventFrame{SessionID: s.id, Kind: protocol.ChatEventUsage, Usage: usage})
}
func contextWithTimeout(timeout time.Duration) (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), timeout)
}