Restore hosted background tasks and preserve live hosts on reconnect failure
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package claude
|
||||
|
||||
import (
|
||||
"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]})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user