Files
archie/internal/webui/api_tasks_attempts.go
T

383 lines
13 KiB
Go

package webui
import (
"net/http"
"sort"
"strconv"
"strings"
"time"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// Stage/attempt statuses the attempt rail reports. They are one vocabulary
// because the rail renders one status per stage and one for the attempt it
// belongs to; "unknown" exists only for an attempt whose events carry no stage
// information at all, so a rail never claims a verdict it did not observe.
const (
attemptStatusOK = "ok"
attemptStatusFailed = "failed"
attemptStatusInterrupted = "interrupted"
attemptStatusRunning = "running"
attemptStatusSkipped = "skipped"
attemptStatusUnknown = "unknown"
)
// taskAttemptsView is GET /api/tasks/{id}/attempts: every attempt of one task
// with its stages inline, so the rail and the attempt selector cannot disagree
// and the page makes no per-attempt calls of its own.
type taskAttemptsView struct {
TaskID int64 `json:"task_id"`
// CurrentAttempt is the task's own attempt, echoed so the page can show
// which run the daemon is on even when that attempt has no events yet.
CurrentAttempt int `json:"current_attempt"`
Attempts []taskAttemptView `json:"attempts"`
// Inputs are what the task was started with; Outputs what its current
// attempt wrote; CalledBy the task whose workflow.call started it.
Inputs map[string]any `json:"inputs,omitempty"`
Outputs map[string]any `json:"outputs,omitempty"`
CalledBy int64 `json:"called_by,omitempty"`
// Recovery links a parked task to the page that fixes its blocker,
// present only when the viewer may act there.
Recovery *taskRecoveryView `json:"recovery,omitempty"`
}
// taskAttemptView is one attempt, bounded by its first and last event.
// Unknown fields are omitted.
type taskAttemptView struct {
Attempt int `json:"attempt"`
Status string `json:"status"`
StartedAt *time.Time `json:"started_at,omitempty"`
FinishedAt *time.Time `json:"finished_at,omitempty"`
DurationMS *int64 `json:"duration_ms,omitempty"`
EventCount int `json:"event_count"`
Error string `json:"error,omitempty"`
Stages []taskStageView `json:"stages"`
}
// taskStageView is one stage occurrence within an attempt. Error is always
// present (empty when the stage recorded none) so a consumer can tell "no error
// text" from a field it did not receive.
type taskStageView struct {
ID int64 `json:"id"`
Name string `json:"name"`
Seq int `json:"seq"`
Status string `json:"status"`
StartedAt *time.Time `json:"started_at,omitempty"`
DurationMS *int64 `json:"duration_ms,omitempty"`
Error string `json:"error"`
FinishedAt *time.Time `json:"finished_at,omitempty"`
Agents []taskAgentView `json:"agents,omitempty"`
// Calls are the callee tasks this stage's workflow.call steps started.
Calls []int64 `json:"calls,omitempty"`
}
type taskAgentView struct {
ID int64 `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
Detail string `json:"detail"`
TokensUsed int64 `json:"tokens_used"`
StartedAt time.Time `json:"started_at"`
FinishedAt *time.Time `json:"finished_at,omitempty"`
}
// handleTaskAttempts returns a task's attempts from its events, each with the
// stages recorded as step executions.
func (s *Server) handleTaskAttempts(w http.ResponseWriter, r *http.Request) {
t, ok := s.taskByPathID(w, r)
if !ok {
return
}
evs, err := s.Store.TaskEvents(r.Context(), t.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var steps []task.StepExecution
if s.Steps != nil {
steps, err = s.Steps.ListSteps(r.Context(), t.ID, 0)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
attempts := attemptsFromEvents(evs, steps, t.Status, t.Attempt)
for i := range attempts {
if attempts[i].Attempt == t.Attempt && t.Status == task.StatusParked {
attempts[i].Error = t.ParkReason
}
}
writeJSON(w, taskAttemptsView{
TaskID: t.ID,
CurrentAttempt: t.Attempt,
Attempts: attempts,
Inputs: t.Inputs,
Outputs: t.Outputs,
CalledBy: t.CallParentTaskID,
Recovery: s.taskRecovery(r, t),
})
}
// taskByPathID resolves a task route's {id} to the task it names, answering the
// request itself and reporting false when it cannot. An unknown task is 404
// plain text -- TaskByID says "no such row" the same way it says "here is the
// row", so the distinction is drawn here rather than leaked to the page.
func (s *Server) taskByPathID(w http.ResponseWriter, r *http.Request) (*task.Task, bool) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.Error(w, "bad id", http.StatusBadRequest)
return nil, false
}
t, err := s.Store.TaskByID(r.Context(), id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil, false
}
if t == nil {
http.Error(w, "task not found", http.StatusNotFound)
return nil, false
}
return t, true
}
// taskAttemptTarget resolves the task in {id} and the requested attempt,
// defaulting to the current one.
func (s *Server) taskAttemptTarget(w http.ResponseWriter, r *http.Request) (*task.Task, int, bool) {
t, ok := s.taskByPathID(w, r)
if !ok {
return nil, 0, false
}
attempt := t.Attempt
raw := strings.TrimSpace(r.URL.Query().Get("attempt"))
if raw == "" {
return t, attempt, true
}
parsed, err := strconv.Atoi(raw)
if err != nil {
http.Error(w, "bad attempt", http.StatusBadRequest)
return nil, 0, false
}
if parsed != 0 {
attempt = parsed
}
return t, attempt, true
}
// attemptsFromEvents groups a task's events by attempt and counts events with
// no attempt.
func attemptsFromEvents(evs []events.Event, steps []task.StepExecution, taskStatus string, currentAttempt int) []taskAttemptView {
grouped := make(map[int][]events.Event)
numbers := make([]int, 0, 4)
for _, e := range evs {
if e.Attempt == 0 {
continue
}
if _, seen := grouped[e.Attempt]; !seen {
numbers = append(numbers, e.Attempt)
}
grouped[e.Attempt] = append(grouped[e.Attempt], e)
}
// A claimed attempt exists even if preparation failed before its first step.
if currentAttempt > 0 {
if _, seen := grouped[currentAttempt]; !seen {
grouped[currentAttempt] = nil
numbers = append(numbers, currentAttempt)
}
}
for _, step := range steps {
if _, seen := grouped[step.Attempt]; !seen && step.Attempt > 0 {
grouped[step.Attempt] = nil
numbers = append(numbers, step.Attempt)
}
}
sort.Ints(numbers)
attempts := make([]taskAttemptView, 0, len(numbers))
for _, number := range numbers {
attempts = append(attempts, attemptRail(number, grouped[number], stepsForAttempt(steps, number), taskStatus, currentAttempt))
}
return attempts
}
// stepsForAttempt filters ListSteps' full-execution result to one attempt's
// steps, in the order the query already returns them (attempt, id).
func stepsForAttempt(steps []task.StepExecution, attempt int) []task.StepExecution {
var out []task.StepExecution
for _, s := range steps {
if s.Attempt == attempt {
out = append(out, s)
}
}
return out
}
// attemptRail derives one attempt's status and bounds from its events, and
// its stages from its recorded StepExecutions.
func attemptRail(number int, evs []events.Event, steps []task.StepExecution, taskStatus string, currentAttempt int) taskAttemptView {
// The task's own record is the only honest source for "is this still
// happening": a step still running on any other attempt ended without
// finishing -- parked, retried, crashed or restarted -- so it is reported
// as interrupted rather than left looking permanently in flight.
inFlight := number == currentAttempt && taskStatus == task.StatusRunning
first, last := attemptBounds(evs)
stages := stageViewsFromSteps(steps, inFlight)
attempt := taskAttemptView{
Attempt: number,
Status: attemptStatus(stages, inFlight),
EventCount: len(evs),
Stages: stages,
}
if attempt.Status == attemptStatusUnknown {
attempt.Status, attempt.Error = earlyAttemptOutcome(evs, number, taskStatus, currentAttempt)
}
if !first.IsZero() {
attempt.StartedAt = &first
}
// A finished_at is only claimed for an attempt that has an end: a running
// one has none yet, and an unknown one never had stage information to time.
if !last.IsZero() && attempt.Status != attemptStatusRunning && attempt.Status != attemptStatusUnknown {
attempt.FinishedAt = &last
if !first.IsZero() {
span := last.Sub(first).Milliseconds()
attempt.DurationMS = &span
}
}
return attempt
}
// earlyAttemptOutcome uses explicit park evidence when no stage ran.
func earlyAttemptOutcome(evs []events.Event, number int, taskStatus string, currentAttempt int) (string, string) {
status, detail := attemptStatusUnknown, ""
for _, event := range evs {
switch event.Kind {
case events.KindParked:
status, detail = task.StatusParked, event.Detail
case events.KindTaskRetried:
if reason, ok := event.Data["previous_reason"].(string); ok && reason != "" {
status, detail = task.StatusParked, reason
}
}
}
if number == currentAttempt {
switch taskStatus {
case task.StatusParked, task.StatusDead, task.StatusWaitingApproval:
status = taskStatus
}
}
return status, detail
}
// attemptBounds is the span of an attempt's own recorded events: the earliest
// and latest timestamp any of them carries. A zero time contributes nothing -- a
// record whose timestamp did not parse is not evidence of when it happened.
func attemptBounds(evs []events.Event) (first, last time.Time) {
for _, e := range evs {
if e.At.IsZero() {
continue
}
if first.IsZero() || e.At.Before(first) {
first = e.At
}
if last.IsZero() || e.At.After(last) {
last = e.At
}
}
return first, last
}
// stageViewsFromSteps returns an attempt's stage steps in order.
func stageViewsFromSteps(steps []task.StepExecution, inFlight bool) []taskStageView {
stages := []taskStageView{}
for _, s := range steps {
if s.Kind != task.StepKindStage {
continue
}
view := taskStageView{ID: s.ID, Name: s.Name, Seq: len(stages), Status: mapStepStatus(s.Status, inFlight)}
// A skipped step's detail is its when condition, not a cause.
if s.Status == taskstate.StepFailed || s.Status == taskstate.StepInterrupted {
view.Error = s.Detail
}
if !s.StartedAt.IsZero() {
started := s.StartedAt
view.StartedAt = &started
}
if !s.FinishedAt.IsZero() {
finished := s.FinishedAt
view.FinishedAt = &finished
duration := s.FinishedAt.Sub(s.StartedAt).Milliseconds()
view.DurationMS = &duration
}
for _, child := range steps {
if child.ParentID == s.ID && child.CalledExecutionID != 0 {
view.Calls = append(view.Calls, child.CalledExecutionID)
}
if child.ParentID != s.ID || child.Kind != task.StepKindAgent {
continue
}
agent := taskAgentView{ID: child.ID, Name: child.Name, Status: mapStepStatus(child.Status, inFlight), Detail: child.Detail, TokensUsed: child.TokensUsed, StartedAt: child.StartedAt}
if !child.FinishedAt.IsZero() {
finished := child.FinishedAt
agent.FinishedAt = &finished
}
view.Agents = append(view.Agents, agent)
}
stages = append(stages, view)
}
return stages
}
// mapStepStatus maps a step status; a step still running after its attempt
// ended reads interrupted.
func mapStepStatus(status taskstate.StepStatus, inFlight bool) string {
switch status {
case taskstate.StepSucceeded:
return attemptStatusOK
case taskstate.StepFailed:
return attemptStatusFailed
case taskstate.StepCancelled, taskstate.StepInterrupted:
return attemptStatusInterrupted
case taskstate.StepSkipped:
return attemptStatusSkipped
case taskstate.StepRunning:
if inFlight {
return attemptStatusRunning
}
return attemptStatusInterrupted
default:
return attemptStatusUnknown
}
}
// attemptStatus derives an attempt's status from its stages and whether it is
// running now.
func attemptStatus(stages []taskStageView, inFlight bool) string {
if inFlight {
return attemptStatusRunning
}
switch {
case anyStageStatus(stages, attemptStatusFailed):
return attemptStatusFailed
case anyStageStatus(stages, attemptStatusInterrupted):
return attemptStatusInterrupted
case len(stages) == 0:
// The attempt exists only because events attributed to it do; if none
// of them recorded a stage there is no verdict to report.
return attemptStatusUnknown
default:
return attemptStatusOK
}
}
func anyStageStatus(stages []taskStageView, status string) bool {
for _, s := range stages {
if s.Status == status {
return true
}
}
return false
}