Enhance workflow execution with detailed run logging and error handling, including workflow run status tracking and node tracing

This commit is contained in:
mlogclub
2026-06-23 22:57:18 +08:00
parent 6c02d6a79c
commit a670084374
8 changed files with 335 additions and 34 deletions
+20
View File
@@ -27,6 +27,10 @@ func TestSummaryPrimaryToolCodePrefersToolSearchTarget(t *testing.T) {
}
func TestToRunLogFinalAction(t *testing.T) {
if got := toRunLogFinalAction(&applicationruntime.Summary{WorkflowVersionID: 66, ReplyText: "ok"}); got != "workflow_reply" {
t.Fatalf("expected workflow_reply final action, got %q", got)
}
if got := toRunLogFinalAction(&applicationruntime.Summary{PlannedSkillID: 44, ReplyText: "ok"}); got != "skill" {
t.Fatalf("expected skill final action, got %q", got)
}
@@ -50,6 +54,22 @@ func TestToRunLogFinalAction(t *testing.T) {
}
}
func TestBuildRunLogPlanUsesWorkflowSummary(t *testing.T) {
plannedAction, plannedToolCode, planReason := buildRunLogPlan(&applicationruntime.Summary{
WorkflowVersionID: 66,
Status: "completed",
})
if plannedAction != "workflow" {
t.Fatalf("expected workflow planned action, got %q", plannedAction)
}
if plannedToolCode != "workflow/66" {
t.Fatalf("expected workflow planned tool code, got %q", plannedToolCode)
}
if planReason == "" {
t.Fatalf("expected plan reason")
}
}
func TestExtractInterruptMessageAndCheckpointError(t *testing.T) {
if got := extractInterruptMessage(`{"message":"请补充订单号"}`); got != "请补充订单号" {
t.Fatalf("unexpected interrupt message: %q", got)
@@ -3,6 +3,7 @@ package runtime
import (
"encoding/json"
"log/slog"
"strconv"
"strings"
"time"
@@ -90,6 +91,9 @@ func buildRunLogPlan(summary *applicationruntime.Summary) (plannedAction, planne
if summary == nil {
return "", "", ""
}
if isWorkflowSummary(summary) {
return "workflow", workflowPlannedToolCode(summary), "workflow executed"
}
if summaryPlannedSkillID(summary) > 0 {
reason := strings.TrimSpace(summary.PlanReason)
if reason == "" {
@@ -138,6 +142,9 @@ func toRunLogFinalAction(summary *applicationruntime.Summary) string {
if summary == nil {
return ""
}
if isWorkflowSummary(summary) {
return workflowFinalAction(summary)
}
if summaryPlannedSkillID(summary) > 0 && strings.TrimSpace(summary.ReplyText) != "" {
return "skill"
}
@@ -160,6 +167,35 @@ func toRunLogFinalAction(summary *applicationruntime.Summary) string {
}
}
func isWorkflowSummary(summary *applicationruntime.Summary) bool {
return summary != nil && summary.WorkflowVersionID > 0
}
func workflowPlannedToolCode(summary *applicationruntime.Summary) string {
if summary == nil || summary.WorkflowVersionID <= 0 {
return ""
}
return "workflow/" + strconv.FormatInt(summary.WorkflowVersionID, 10)
}
func workflowFinalAction(summary *applicationruntime.Summary) string {
if summary == nil {
return ""
}
switch strings.TrimSpace(summary.Status) {
case "interrupted":
return "workflow_interrupted"
case "error":
return "workflow_error"
case "expired":
return "workflow_expired"
}
if strings.TrimSpace(summary.ReplyText) != "" {
return "workflow_reply"
}
return "workflow_completed"
}
func buildRunLogReplyText(summary *applicationruntime.Summary) string {
if summary == nil {
return ""
@@ -67,6 +67,7 @@ func (e *runtimeReplyExecutor) ResumePendingInterrupt(ctx context.Context, input
}
summary, err := Service.Resume(ctx, applicationruntime.ResumeRequest{
Conversation: input.Conversation,
UserMessage: input.Message,
AIAgent: input.AIAgent,
AIConfig: *aiConfig,
CheckPointID: strings.TrimSpace(input.PendingInterrupt.CheckPointID),
+74 -7
View File
@@ -7,6 +7,7 @@ import (
"reflect"
"strconv"
"strings"
"time"
"agent-desk/internal/ai"
"agent-desk/internal/ai/runtime/graphs"
@@ -34,6 +35,7 @@ type Result struct {
Status string
ReplyText string
NodePath []string
NodeTraces []NodeTrace
PromptTokens int
CompletionTokens int
RetrieverCount int
@@ -44,6 +46,16 @@ type Result struct {
Interrupts []InterruptSummary
}
type NodeTrace struct {
NodeID string
NodeType string
Status string
InputPreview string
OutputPreview string
ErrorMessage string
DurationMS int
}
type InterruptSummary struct {
Type string
ID string
@@ -127,23 +139,44 @@ func (e *Executor) executeFrom(ctx context.Context, state *runState, currentID s
for step := 0; step < maxWorkflowSteps; step++ {
node, ok := state.nodesByID[currentID]
if !ok {
return nil, fmt.Errorf("workflow node does not exist: %s", currentID)
err := fmt.Errorf("workflow node does not exist: %s", currentID)
state.result.Status = "error"
return &state.result, err
}
state.result.NodePath = append(state.result.NodePath, node.ID)
if err := e.executeNode(ctx, state, node); err != nil {
return nil, err
trace := NodeTrace{
NodeID: node.ID,
NodeType: node.Type,
Status: "running",
InputPreview: workflowPreviewJSON(state.nodeInputPreview(node)),
}
startedAt := time.Now()
if err := e.executeNode(ctx, state, node); err != nil {
trace.Status = "failed"
trace.ErrorMessage = err.Error()
trace.DurationMS = int(time.Since(startedAt).Milliseconds())
state.result.NodeTraces = append(state.result.NodeTraces, trace)
state.result.Status = "error"
return &state.result, err
}
trace.OutputPreview = workflowPreviewJSON(state.vars[node.ID])
trace.DurationMS = int(time.Since(startedAt).Milliseconds())
if state.result.Interrupted {
trace.Status = "interrupted"
state.result.NodeTraces = append(state.result.NodeTraces, trace)
state.result.Status = "interrupted"
return &state.result, nil
}
trace.Status = "completed"
state.result.NodeTraces = append(state.result.NodeTraces, trace)
if node.Type == workflowregistry.NodeTypeEnd {
state.result.Status = "completed"
return &state.result, nil
}
nextID, ok, err := state.nextNodeID(node.ID)
if err != nil {
return nil, err
state.result.Status = "error"
return &state.result, err
}
if !ok {
state.result.Status = "completed"
@@ -151,7 +184,9 @@ func (e *Executor) executeFrom(ctx context.Context, state *runState, currentID s
}
currentID = nextID
}
return nil, fmt.Errorf("workflow exceeded max steps")
err := fmt.Errorf("workflow exceeded max steps")
state.result.Status = "error"
return &state.result, err
}
func newRunState(input Input) *runState {
@@ -161,8 +196,9 @@ func newRunState(input Input) *runState {
outgoing: make(map[string][]dsl.Edge),
vars: make(map[string]map[string]any),
result: Result{
Status: "started",
NodePath: make([]string, 0),
Status: "started",
NodePath: make([]string, 0),
NodeTraces: make([]NodeTrace, 0),
},
}
for _, node := range input.Definition.Nodes {
@@ -553,6 +589,37 @@ func (s *runState) resolveInput(node dsl.Node, inputName string) any {
return s.resolveSelector(&selector)
}
func (s *runState) nodeInputPreview(node dsl.Node) map[string]any {
inputs := make(map[string]any, len(node.Inputs))
for name, selector := range node.Inputs {
inputs[name] = s.resolveSelector(&selector)
}
ret := map[string]any{
"inputs": inputs,
}
if len(node.Config) > 0 {
var cfg any
if err := json.Unmarshal(node.Config, &cfg); err == nil {
ret["config"] = cfg
} else {
ret["config"] = string(node.Config)
}
}
return ret
}
func workflowPreviewJSON(value any) string {
raw, err := json.Marshal(value)
if err != nil {
return ""
}
const maxPreviewBytes = 2000
if len(raw) <= maxPreviewBytes {
return string(raw)
}
return string(raw[:maxPreviewBytes])
}
func (s *runState) resolveSelector(selector *dsl.VariableSelector) any {
if selector == nil {
return nil