package claudehost import ( "encoding/json" "regexp" "strings" "unicode/utf8" ) // The host owns the real CLI even with no Pipelines connection. Foreground // result messages do not end background tools; only their native completion // notifications do. Keep this lifecycle state independently of spool acks. func (h *host) trackTasksLocked(line []byte) { var event struct { Type string `json:"type"` Subtype string `json:"subtype"` ToolUseID string `json:"tool_use_id"` TaskID string `json:"task_id"` Status string `json:"status"` Description string `json:"description"` TaskType string `json:"task_type"` Timestamp string `json:"timestamp"` Message struct { Content json.RawMessage `json:"content"` } `json:"message"` } if json.Unmarshal(line, &event) != nil { return } if h.tasks == nil { h.tasks = map[string]string{} } if h.taskOrigins == nil { h.taskOrigins = map[string]Task{} } switch event.Subtype { case "task_started", "task_progress": id := event.ToolUseID if id == "" { for key, task := range h.tasks { if event.TaskID != "" && task == event.TaskID { id = key break } } if id == "" { id = "task:" + event.TaskID } } if id != "task:" { if event.ToolUseID != "" && event.TaskID != "" { fallback := "task:" + event.TaskID if prior, exists := h.taskOrigins[fallback]; exists { if _, recorded := h.taskOrigins[id]; !recorded { h.taskOrigins[id] = prior } delete(h.tasks, fallback) delete(h.taskOrigins, fallback) } } if _, exists := h.tasks[id]; !exists || event.TaskID != "" { h.tasks[id] = event.TaskID } origin := h.taskOrigins[id] if event.ToolUseID != "" { origin.ToolUseID = event.ToolUseID } if event.TaskID != "" { origin.TaskID = event.TaskID } if event.Description != "" { origin.Description = taskText(event.Description, 2048) } if event.TaskType != "" { origin.TaskType = taskText(event.TaskType, 128) } if origin.Timestamp == "" { origin.Timestamp = event.Timestamp } h.taskOrigins[id] = origin } case "task_notification": if event.Status == "completed" || event.Status == "failed" || event.Status == "stopped" { for key, task := range h.tasks { if key == event.ToolUseID || (event.TaskID != "" && (task == event.TaskID || key == "task:"+event.TaskID)) { delete(h.tasks, key) delete(h.taskOrigins, key) } } } } if event.Type == "result" { for key := range h.taskOrigins { if _, running := h.tasks[key]; !running { delete(h.taskOrigins, key) } } } var blocks []taskBlock if json.Unmarshal(event.Message.Content, &blocks) != nil { var text string if json.Unmarshal(event.Message.Content, &text) != nil { return } blocks = []taskBlock{{Type: "text", Text: text}} } finishText := func(text string) { for _, match := range completedTaskNotice.FindAllStringSubmatch(text, -1) { key := match[1] taskID := h.tasks[key] delete(h.tasks, key) delete(h.taskOrigins, key) if taskID != "" { delete(h.tasks, "task:"+taskID) delete(h.taskOrigins, "task:"+taskID) } } } for _, block := range blocks { switch block.Type { case "tool_use": if block.ID != "" && (block.Name == "Agent" || block.Name == "Task" || block.Name == "Bash") { if block.Input.Background { if _, exists := h.tasks[block.ID]; !exists { h.tasks[block.ID] = "" } } origin := h.taskOrigins[block.ID] origin.ToolUseID, origin.Name = block.ID, block.Name origin.Command = taskText(block.Input.Command, 64<<10) if block.Input.Description != "" { origin.Description = taskText(block.Input.Description, 2048) } if origin.TaskType == "" { if block.Name == "Bash" { origin.TaskType = "shell" } else { origin.TaskType = "agent" } } if origin.Timestamp == "" { origin.Timestamp = event.Timestamp } h.taskOrigins[block.ID] = origin } case "tool_result": var text string if json.Unmarshal(block.Content, &text) != nil { var parts []struct { Text string `json:"text"` } _ = json.Unmarshal(block.Content, &parts) for _, part := range parts { text += part.Text + "\n" } } if strings.Contains(text, "running in background with ID") || strings.HasPrefix(strings.TrimSpace(text), "Async agent launched") { if block.ToolUseID != "" { if _, exists := h.tasks[block.ToolUseID]; !exists { h.tasks[block.ToolUseID] = "" } } } else { delete(h.tasks, block.ToolUseID) delete(h.taskOrigins, block.ToolUseID) } finishText(text) case "text": finishText(block.Text) } } } var completedTaskNotice = regexp.MustCompile(`(?s).*?\s*([^<\s]+)\s*.*?`) // Native stream-json message content can be an array or a plain notification. type taskBlock struct { Type string `json:"type"` ID string `json:"id"` ToolUseID string `json:"tool_use_id"` Name string `json:"name"` Input struct { Background bool `json:"run_in_background"` Command string `json:"command"` Description string `json:"description"` } `json:"input"` Text string `json:"text"` Content json.RawMessage `json:"content"` } func taskText(s string, limit int) string { if len(s) <= limit { return s } for limit > 0 && !utf8.ValidString(s[:limit]) { limit-- } return s[:limit] }