Files

112 lines
3.3 KiB
Go

package claude
import (
"bytes"
"encoding/json"
"git.a-wan.net/alan/pipelines/internal/claudehost"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// reconcileHostTasks runs only after replay reaches the status snapshot's End.
// It restores native identities even when their starting records were trimmed
// from the bounded spool. Old hosts omit Tasks, which means unknown, not idle.
func (s *session) reconcileHostTasks(tasks *[]claudehost.Task) {
if tasks == nil {
return
}
s.mu.Lock()
present := make(map[string]bool, len(*tasks))
var frames []protocol.ChatShell
for _, task := range *tasks {
id := task.ToolUseID
if id == "" && task.TaskID != "" {
id = "task:" + task.TaskID
}
if id == "" {
continue
}
present[id] = true
command := boundedText(task.Command, 64<<10)
if command == "" && (task.Name == "Agent" || task.Name == "Task") && task.Description != "" {
command = "Agent: " + boundedText(task.Description, 2048)
}
s.recordShellLocked(id, command)
snapshot := s.shells[id]
snapshot.background = true
snapshot.taskID = task.TaskID
snapshot.shell.Status = protocol.ChatShellRunning
snapshot.shell.Exit = nil
if task.Description != "" {
snapshot.shell.Description = boundedText(task.Description, 2048)
}
if task.TaskType != "" {
snapshot.shell.TaskType = boundedText(task.TaskType, 128)
} else if snapshot.shell.TaskType == "" {
switch task.Name {
case "Bash":
snapshot.shell.TaskType = "shell"
case "Agent", "Task":
snapshot.shell.TaskType = "agent"
}
}
if snapshot.shell.StartedAt == 0 {
if stamp, ok := transcriptTime(task.Timestamp); ok && stamp > 0 {
snapshot.shell.StartedAt = stamp
}
}
frames = append(frames, snapshot.shell)
}
for id, snapshot := range s.shells {
if !snapshot.background || snapshot.shell.Status != protocol.ChatShellRunning || present[id] {
continue
}
// The host says this background identity is no longer running, but
// supplies neither an exit code nor a completion time. Keep both absent.
snapshot.shell.Status = protocol.ChatShellExited
frames = append(frames, snapshot.shell)
}
s.mu.Unlock()
for i := range frames {
s.emit(protocol.ChatEventFrame{SessionID: s.id, Kind: protocol.ChatEventShell, Shell: &frames[i]})
}
}
// resolveHostedTaskLine correlates a completion that carries only the native
// task id with the tool identity restored from the authoritative snapshot.
// This is translation inside the server; no altered event is sent to the CLI.
func (s *session) resolveHostedTaskLine(line []byte) []byte {
if !bytes.Contains(line, []byte(`"task_notification"`)) {
return line
}
var event struct {
Subtype string `json:"subtype"`
TaskID string `json:"task_id"`
ToolUseID string `json:"tool_use_id"`
}
if json.Unmarshal(line, &event) != nil || event.Subtype != "task_notification" || event.TaskID == "" || event.ToolUseID != "" {
return line
}
s.mu.Lock()
id := ""
for toolID, snapshot := range s.shells {
if snapshot.taskID == event.TaskID {
id = toolID
break
}
}
s.mu.Unlock()
if id == "" {
return line
}
var fields map[string]json.RawMessage
if json.Unmarshal(line, &fields) != nil {
return line
}
fields["tool_use_id"], _ = json.Marshal(id)
normalized, err := json.Marshal(fields)
if err != nil {
return line
}
return normalized
}