feat(ui): inspect workflow step runs from canvas

This commit is contained in:
sam committed 2026-10-05 12:53:34 +11:00
1 parent 895a04047d
commit 1510c41254
10 files changed
+351 -89

No files matched your search

@@ -0,0 +1,32 @@
package workflow
import (
"log/slog"
"testing"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/events"
)
func TestStepActivityAttribution(t *testing.T) {
for _, branch := range []string{"", "docs"} {
t.Run(branch, func(t *testing.T) {
bus := events.NewBus()
sub := bus.Subscribe(1)
defer sub.Close()
tc := &TaskContext{Task: &task.Task{ID: 42, Attempt: 2}, StepID: 7, Bus: bus, Log: slog.Default()}
if branch != "" {
tc = tc.branch(branch)
}
data := map[string]any{"tool": "read"}
tc.Emit(events.KindToolCall, "agent.run", "read a file", data)
ev := <-sub.C
if ev.Data["step_id"] != int64(7) || ev.Data["branch"] != branch {
t.Fatalf("activity lost its step or branch: %#v", ev.Data)
}
if len(data) != 1 {
t.Fatalf("caller payload was mutated: %#v", data)
}
})
}
}
+1
View File
@@ -277,6 +277,7 @@ func runBranch(ctx context.Context, tc *TaskContext, stages []Stage) error {
// spent without the branches racing on one task.
func (tc *TaskContext) branch(name string) *TaskContext {
clone := *tc
clone.workflowBranch = name
taskCopy := *tc.Task
taskCopy.TokensUsed, taskCopy.Iterations = 0, 0
clone.Task = &taskCopy
+19 -3
View File
@@ -5,6 +5,7 @@ import (
"context"
"fmt"
"log/slog"
"maps"
"regexp"
"strings"
"time"
@@ -114,7 +115,8 @@ type TaskContext struct {
// body is running: every agent call the stage makes records itself as
// this step's child. Zero outside a stage body, which is how the stage
// unit tests run their bodies.
StepID int64
StepID int64
workflowBranch string
// BuildSummary is the builder agent's finish summary -- the PR body.
BuildSummary string
// BuildNoChanges is set when the builder returned StatusPassed but
@@ -169,10 +171,21 @@ func (tc *TaskContext) Emit(kind, stage, detail string, data map[string]any) {
Attempt: tc.Task.Attempt,
Stage: stage,
Detail: detail,
Data: data,
Data: tc.activityData(kind, data),
})
}
// Activity belongs to the recorded stage, even when an agent uses its own name.
func (tc *TaskContext) activityData(kind string, data map[string]any) map[string]any {
if tc.StepID == 0 || (kind != events.KindToolCall && kind != events.KindAgentFinish) {
return data
}
out := make(map[string]any, len(data)+2)
maps.Copy(out, data)
out["step_id"], out["branch"] = tc.StepID, tc.workflowBranch
return out
}
// EmitDurable persists evaluation-critical telemetry before publishing it to
// the lossy live bus. The assigned ID tells the daemon sink to broadcast
// without inserting a duplicate row.
@@ -184,7 +197,7 @@ func (tc *TaskContext) EmitDurable(ctx context.Context, kind, stage, detail stri
event := events.Event{
At: time.Now().UTC(), Kind: kind, TaskID: tc.Task.ID, Repo: tc.Task.Owner + "/" + tc.Task.Repo,
Issue: tc.Task.IssueNumber, Workflow: tc.Task.Workflow, Attempt: tc.Task.Attempt,
Stage: stage, Detail: detail, Data: data,
Stage: stage, Detail: detail, Data: tc.activityData(kind, data),
}
id, err := tc.Store.InsertEvent(ctx, event)
if err != nil {
@@ -497,6 +510,9 @@ var goTestOKLine = regexp.MustCompile(`^ok\s+\S+`)
// RunAgentChild records one agent call as a child step of the current stage.
// A failed write parks the execution. Outside a recorded run it just runs.
func (tc *TaskContext) RunAgentChild(ctx context.Context, name string, run func() (agentrun.Result, error)) (agentrun.Result, error) {
if tc.workflowBranch != "" {
name = tc.workflowBranch + "/" + name
}
stepID, _, err := tc.startChildStep(ctx, task.StepKindAgent, name)
if err != nil {
return agentrun.Result{}, err
+31 -5
View File
@@ -55,12 +55,25 @@ type taskAttemptView struct {
// 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"`
}
type taskAgentView 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"`
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
@@ -231,7 +244,7 @@ func stageViewsFromSteps(steps []task.StepExecution, inFlight bool) []taskStageV
if s.Kind != task.StepKindStage {
continue
}
view := taskStageView{Name: s.Name, Seq: len(stages), Status: mapStepStatus(s.Status, inFlight)}
view := taskStageView{ID: s.ID, Name: s.Name, Seq: len(stages), Status: mapStepStatus(s.Status, inFlight)}
if s.Status == taskstate.StepFailed {
view.Error = s.Detail
}
@@ -240,9 +253,22 @@ func stageViewsFromSteps(steps []task.StepExecution, inFlight bool) []taskStageV
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.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