-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution.go
More file actions
220 lines (200 loc) · 6.92 KB
/
execution.go
File metadata and controls
220 lines (200 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package engine
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/LAA-Software-Engineering/agentic-control-plane/internal/models"
"github.com/LAA-Software-Engineering/agentic-control-plane/internal/policy"
"github.com/LAA-Software-Engineering/agentic-control-plane/internal/spec"
"github.com/LAA-Software-Engineering/agentic-control-plane/internal/state"
"github.com/LAA-Software-Engineering/agentic-control-plane/internal/tools"
"github.com/LAA-Software-Engineering/agentic-control-plane/internal/trace"
)
// Executor runs sequential workflow steps (design doc section 12.2 E, section 13).
type Executor struct {
Graph *spec.ProjectGraph
ProjectRoot string
Tools tools.ToolExecutor
Models *models.Registry
// ModelResolve, if set, is used instead of Models.ClientFor (tests inject mocks).
ModelResolve func(modelRef string) (models.ModelClient, string, error)
Store state.RuntimeStore
Trace *trace.Recorder
Now func() time.Time
}
// RunInput identifies the workflow run and parsed input map (already JSON-valid).
type RunInput struct {
RunID string
WorkflowName string
Env string
StartedAt time.Time
Input map[string]any
ApprovedActions []string
}
func (e *Executor) now() time.Time {
if e != nil && e.Now != nil {
return e.Now()
}
return time.Now().UTC()
}
func (e *Executor) modelClient(modelRef string) (models.ModelClient, string, error) {
if e.ModelResolve != nil {
return e.ModelResolve(modelRef)
}
if e.Models == nil {
return nil, "", fmt.Errorf("engine: Models registry is nil")
}
return e.Models.ClientFor(modelRef)
}
// Run executes a workflow sequentially: interpolate step inputs, policy checks, tool/agent calls,
// optional JSON Schema validation for agent output, persisted run_steps and trace events.
// The run row must already exist in [state.RuntimeStore] (e.g. via [state.RuntimeStore.StartRun]).
func (e *Executor) Run(ctx context.Context, in RunInput) error {
if e == nil || e.Store == nil {
return fmt.Errorf("engine: nil executor or store")
}
if e.Graph == nil {
return fmt.Errorf("engine: nil project graph")
}
wf, err := lookupWorkflow(e.Graph, in.WorkflowName)
if err != nil {
return err
}
if err := validateWorkflowInput(e.ProjectRoot, wf, in.Input); err != nil {
return e.failRun(ctx, in, err, 0)
}
polEng := policy.NewEngine(e.Graph)
wfPol := polEng.Evaluator(strings.TrimSpace(wf.Spec.Policy))
ictx := Context{Input: in.Input, Steps: make(map[string]StepResult)}
var totalCost float64
finishAt := e.now()
for _, step := range wf.Spec.Steps {
step := step
if strings.TrimSpace(step.ID) == "" {
return e.failRun(ctx, in, fmt.Errorf("engine: workflow step missing id"), totalCost)
}
uses := strings.TrimSpace(step.Uses)
agentName := strings.TrimSpace(step.Agent)
if (uses == "") == (agentName == "") {
return e.failRun(ctx, in, fmt.Errorf("engine: step %q must set exactly one of uses or agent", step.ID), totalCost)
}
withAny, err := InterpolateWalk(step.With, ictx)
if err != nil {
return e.failRun(ctx, in, fmt.Errorf("engine: step %q with: %w", step.ID, err), totalCost)
}
with, ok := withAny.(map[string]any)
if !ok {
with = map[string]any{}
}
elapsed := e.now().Sub(in.StartedAt)
pctx := policy.RunContext{
StartedAt: in.StartedAt,
Elapsed: elapsed,
AccumulatedCostUSD: totalCost,
ApprovedActions: in.ApprovedActions,
}
if err := wfPol.CheckRun(ctx, pctx); err != nil {
return e.failRunStep(ctx, in, step.ID, with, err, totalCost)
}
inJSON, _ := json.Marshal(with)
started := e.now()
if err := e.Store.UpsertRunStep(ctx, state.RunStep{
RunID: in.RunID,
StepID: step.ID,
Status: "running",
StartedAt: &started,
InputJSON: string(inJSON),
}); err != nil {
return e.failRun(ctx, in, fmt.Errorf("engine: upsert step %q: %w", step.ID, err), totalCost)
}
if e.Trace != nil {
_, _ = e.Trace.Append(ctx, in.RunID, step.ID, trace.EventStepStarted, map[string]any{"uses": uses, "agent": agentName})
}
var out map[string]any
var stepCost float64
if uses != "" {
var meta tools.ToolCallMeta
out, meta, err = e.runToolStep(ctx, wfPol, in.RunID, step, with, pctx)
stepCost = meta.CostUSD
} else {
ar, ok := e.Graph.Agents[agentName]
if !ok || ar == nil {
err = fmt.Errorf("engine: unknown agent %q", agentName)
} else {
var gmeta models.GenerateMeta
out, gmeta, err = e.runAgentStep(ctx, wfPol, in.RunID, step, with, pctx, ar)
stepCost = gmeta.CostUSD
}
}
finished := e.now()
totalCost += stepCost
if err != nil {
_ = e.Store.UpsertRunStep(ctx, state.RunStep{
RunID: in.RunID,
StepID: step.ID,
Status: "failed",
StartedAt: &started,
FinishedAt: &finished,
InputJSON: string(inJSON),
ErrorText: err.Error(),
CostUSD: stepCost,
})
if e.Trace != nil {
_, _ = e.Trace.Append(ctx, in.RunID, step.ID, trace.EventStepFailed, map[string]any{"error": err.Error()})
}
return e.failRun(ctx, in, fmt.Errorf("engine: step %q: %w", step.ID, err), totalCost)
}
outJSON, _ := json.Marshal(out)
if err := e.Store.UpsertRunStep(ctx, state.RunStep{
RunID: in.RunID,
StepID: step.ID,
Status: "succeeded",
StartedAt: &started,
FinishedAt: &finished,
InputJSON: string(inJSON),
OutputJSON: string(outJSON),
CostUSD: stepCost,
}); err != nil {
return e.failRun(ctx, in, fmt.Errorf("engine: upsert step %q: %w", step.ID, err), totalCost)
}
if e.Trace != nil {
_, _ = e.Trace.Append(ctx, in.RunID, step.ID, trace.EventStepFinished, map[string]any{"costUsd": stepCost})
}
meta := map[string]any{"costUsd": stepCost, "durationMs": finished.Sub(started).Milliseconds()}
ictx.Steps[step.ID] = StepResult{Output: out, Meta: meta}
}
finalOut, err := buildWorkflowOutput(wf, ictx)
if err != nil {
return e.failRun(ctx, in, err, totalCost)
}
outBytes, err := json.Marshal(finalOut)
if err != nil {
return e.failRun(ctx, in, err, totalCost)
}
finishAt = e.now()
return e.Store.FinishRun(ctx, in.RunID, "succeeded", finishAt, string(outBytes), "", totalCost)
}
func (e *Executor) failRun(ctx context.Context, in RunInput, runErr error, totalCost float64) error {
finishAt := e.now()
_ = e.Store.FinishRun(ctx, in.RunID, "failed", finishAt, "", runErr.Error(), totalCost)
return runErr
}
func (e *Executor) failRunStep(ctx context.Context, in RunInput, stepID string, with map[string]any, runErr error, totalCost float64) error {
inJSON, _ := json.Marshal(with)
now := e.now()
_ = e.Store.UpsertRunStep(ctx, state.RunStep{
RunID: in.RunID,
StepID: stepID,
Status: "failed",
StartedAt: &now,
FinishedAt: &now,
InputJSON: string(inJSON),
ErrorText: runErr.Error(),
})
if e.Trace != nil {
_, _ = e.Trace.Append(ctx, in.RunID, stepID, trace.EventStepFailed, map[string]any{"error": runErr.Error()})
}
return e.failRun(ctx, in, runErr, totalCost)
}