forked from deepnoodle-ai/workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution_state.go
More file actions
631 lines (544 loc) · 17.4 KB
/
execution_state.go
File metadata and controls
631 lines (544 loc) · 17.4 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
package workflow
import (
"errors"
"fmt"
"strings"
"sync"
"time"
)
// BranchState tracks the state of an execution branch. This struct is designed to
// be fully JSON serializable.
type BranchState struct {
ID string `json:"id"`
Status ExecutionStatus `json:"status"`
CurrentStep string `json:"current_step"`
StartTime time.Time `json:"start_time,omitzero"`
EndTime time.Time `json:"end_time,omitzero"`
ErrorMessage string `json:"error_message,omitempty"`
StepOutputs map[string]any `json:"step_outputs"`
Variables map[string]any `json:"variables"`
// Wait is populated when the branch is hard-suspended on a durable
// wait (signal-wait or durable sleep). nil otherwise.
Wait *WaitState `json:"wait,omitempty"`
// PauseRequested marks a branch as paused by an explicit pause
// trigger — either an external PauseBranch call or a declarative
// Pause step. A branch with PauseRequested=true will re-park at its
// next step boundary after construction; UnpauseBranch must clear
// the flag before the branch can advance.
PauseRequested bool `json:"pause_requested,omitempty"`
// PauseReason is an optional human-readable note describing why
// the branch was paused. Set by the PauseBranch caller or by a
// PauseConfig.Reason on a declarative Pause step.
PauseReason string `json:"pause_reason,omitempty"`
// ActivityHistory is the persisted cache for the currently
// executing activity. It survives wait-unwind replays so
// activities can cache expensive work across suspensions via
// [Context.History] + [History.RecordOrReplay]. Cleared
// when the step advances past the activity so there is no
// cross-step leakage.
ActivityHistory map[string]any `json:"activity_history,omitempty"`
// ActivityHistoryStep records which step's activity owns the
// current ActivityHistory map. executeActivity uses it to scope
// history access to a single step: if the branch has raced ahead to
// a new step before the orchestrator cleared the prior step's
// history, the mismatch discards the stale entries so they do not
// leak into the next activity.
ActivityHistoryStep string `json:"activity_history_step,omitempty"`
}
// JoinState tracks a branch waiting at a join step
type JoinState struct {
StepName string `json:"step_name"`
WaitingBranchID string `json:"waiting_branch_id"` // The single branch that's waiting
Config *JoinConfig `json:"config"`
CreatedAt time.Time `json:"created_at"`
}
// Copy returns a shallow copy of the branch state.
func (p *BranchState) Copy() *BranchState {
var wait *WaitState
if p.Wait != nil {
waitCopy := *p.Wait
wait = &waitCopy
}
return &BranchState{
ID: p.ID,
Status: p.Status,
CurrentStep: p.CurrentStep,
StartTime: p.StartTime,
EndTime: p.EndTime,
ErrorMessage: p.ErrorMessage,
StepOutputs: copyMap(p.StepOutputs),
Variables: copyMap(p.Variables),
Wait: wait,
PauseRequested: p.PauseRequested,
PauseReason: p.PauseReason,
ActivityHistory: copyMap(p.ActivityHistory),
ActivityHistoryStep: p.ActivityHistoryStep,
}
}
// executionState consolidates all execution state into a single structure. All
// data here is serializable for checkpointing.
type executionState struct {
executionID string
workflowName string
status ExecutionStatus
startTime time.Time
endTime time.Time
err string
inputs map[string]any
outputs map[string]any
pathCounter int
branchStates map[string]*BranchState
joinStates map[string]*JoinState // stepName -> JoinState
mutex sync.RWMutex
}
// newExecutionState creates a new unified execution state
func newExecutionState(executionID, workflowName string, inputs map[string]any) *executionState {
return &executionState{
executionID: executionID,
workflowName: workflowName,
status: ExecutionStatusPending,
inputs: copyMap(inputs),
outputs: map[string]any{},
branchStates: map[string]*BranchState{},
joinStates: map[string]*JoinState{},
}
}
// ID returns the execution ID
func (s *executionState) ID() string {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.executionID
}
// SetID sets the execution ID
func (s *executionState) SetID(id string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.executionID = id
}
// GetStatus returns the current execution status
func (s *executionState) GetStatus() ExecutionStatus {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.status
}
// SetStatus updates the execution status
func (s *executionState) SetStatus(status ExecutionStatus) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.status = status
if status != ExecutionStatusFailed {
s.err = ""
}
}
// SetError sets the execution error
func (s *executionState) SetError(err error) {
s.mutex.Lock()
defer s.mutex.Unlock()
if err != nil {
s.err = err.Error()
s.status = ExecutionStatusFailed
} else {
s.err = ""
}
}
// GetError returns the current execution error
func (s *executionState) GetError() error {
s.mutex.RLock()
defer s.mutex.RUnlock()
if s.err == "" {
return nil
}
return errors.New(s.err)
}
// SetTiming updates the execution timing
func (s *executionState) SetTiming(startTime, endTime time.Time) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.startTime = startTime
s.endTime = endTime
}
func (s *executionState) SetFinished(status ExecutionStatus, endTime time.Time, err error) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.status = status
s.endTime = endTime
if err != nil {
s.err = err.Error()
} else {
s.err = ""
}
}
// NextBranchID generates a new unique branch ID
func (s *executionState) NextBranchID(baseID string) string {
s.mutex.Lock()
defer s.mutex.Unlock()
s.pathCounter++
return baseID + "-" + fmt.Sprintf("%d", s.pathCounter)
}
// GenerateBranchID creates a branch ID, using branchName if provided, otherwise generating a sequential ID
func (s *executionState) GenerateBranchID(parentID, branchName string) (string, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
var branchID string
if branchName != "" {
// Use the provided branch name as the ID
branchID = branchName
// Check for duplicate branch names
if _, exists := s.branchStates[branchID]; exists {
return "", fmt.Errorf("duplicate branch name: %q", branchName)
}
} else {
// Default to generating sequential IDs
s.pathCounter++
branchID = parentID + "-" + fmt.Sprintf("%d", s.pathCounter)
}
return branchID, nil
}
// SetBranchState sets or updates a branch state
func (s *executionState) SetBranchState(branchID string, state *BranchState) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.branchStates[branchID] = state.Copy()
}
// GetBranchState retrieves a branch state
func (s *executionState) GetBranchStates() map[string]*BranchState {
s.mutex.RLock()
defer s.mutex.RUnlock()
return copyBranchStates(s.branchStates)
}
// UpdateBranchState applies an update function to a branch state
func (s *executionState) UpdateBranchState(branchID string, updateFn func(*BranchState)) {
s.mutex.Lock()
defer s.mutex.Unlock()
if state, exists := s.branchStates[branchID]; exists {
updateFn(state)
}
}
// GetInputs creates a shallow copy of the inputs
func (s *executionState) GetInputs() map[string]any {
s.mutex.RLock()
defer s.mutex.RUnlock()
return copyMap(s.inputs)
}
// SetOutput sets an output value
func (s *executionState) SetOutput(key string, value any) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.outputs[key] = value
}
// GetOutput retrieves an output value
func (s *executionState) GetOutputs() map[string]any {
s.mutex.RLock()
defer s.mutex.RUnlock()
return copyMap(s.outputs)
}
// GetStartTime returns the execution start time
func (s *executionState) GetStartTime() time.Time {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.startTime
}
// GetEndTime returns the execution end time
func (s *executionState) GetEndTime() time.Time {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.endTime
}
// GetFailedBranchIDs returns a list of branch IDs that have failed
func (s *executionState) GetFailedBranchIDs() []string {
s.mutex.RLock()
defer s.mutex.RUnlock()
var failedIDs []string
for branchID, branchState := range s.branchStates {
if branchState.Status == ExecutionStatusFailed {
failedIDs = append(failedIDs, branchID)
}
}
return failedIDs
}
// GetWaitingBranchIDs returns a list of branch IDs that are waiting at joins.
// This reflects Status == Waiting, which the engine uses exclusively for
// join-in-progress. Hard-suspended branches have Status == Suspended and are
// reported by GetSuspendedBranchIDs.
func (s *executionState) GetWaitingBranchIDs() []string {
s.mutex.RLock()
defer s.mutex.RUnlock()
var waitingIDs []string
for branchID, branchState := range s.branchStates {
if branchState.Status == ExecutionStatusWaiting {
waitingIDs = append(waitingIDs, branchID)
}
}
return waitingIDs
}
// GetSuspendedBranchIDs returns a list of branch IDs that are hard-suspended
// on a durable wait (signal-wait or sleep). These branches have exited their
// goroutine and only live in the checkpoint.
func (s *executionState) GetSuspendedBranchIDs() []string {
s.mutex.RLock()
defer s.mutex.RUnlock()
var suspendedIDs []string
for branchID, branchState := range s.branchStates {
if branchState.Status == ExecutionStatusSuspended {
suspendedIDs = append(suspendedIDs, branchID)
}
}
return suspendedIDs
}
// GetPausedBranchIDs returns a list of branch IDs that are currently paused.
// Paused branches have exited their goroutine and only live in the checkpoint;
// an external UnpauseBranch call is required before the branch can advance.
func (s *executionState) GetPausedBranchIDs() []string {
s.mutex.RLock()
defer s.mutex.RUnlock()
var pausedIDs []string
for branchID, branchState := range s.branchStates {
if branchState.Status == ExecutionStatusPaused {
pausedIDs = append(pausedIDs, branchID)
}
}
return pausedIDs
}
// AddBranchToJoin adds a branch to a join step
func (s *executionState) AddBranchToJoin(stepName, branchID string, config *JoinConfig, variables, stepOutputs map[string]any) {
s.mutex.Lock()
defer s.mutex.Unlock()
// Create join state if it doesn't exist
if s.joinStates[stepName] == nil {
s.joinStates[stepName] = &JoinState{
StepName: stepName,
WaitingBranchID: branchID,
Config: config,
CreatedAt: time.Now(),
}
} else {
// Update the existing join state with the new branch ID
s.joinStates[stepName].WaitingBranchID = branchID
}
}
// IsJoinReady checks if a join step is ready to proceed
func (s *executionState) IsJoinReady(stepName string) bool {
s.mutex.RLock()
defer s.mutex.RUnlock()
joinState := s.joinStates[stepName]
if joinState == nil {
return false
}
config := joinState.Config
// If specific branches are specified, check if all are completed (excluding the waiting branch)
if len(config.Branches) > 0 {
for _, requiredBranch := range config.Branches {
// Skip the branch that's currently waiting at the join
if requiredBranch == joinState.WaitingBranchID {
continue
}
branchState, exists := s.branchStates[requiredBranch]
if !exists || branchState.Status != ExecutionStatusCompleted {
return false
}
}
return true
}
// If count is specified, count completed branches (excluding the waiting branch)
if config.Count > 0 {
completedCount := 0
for branchID, branchState := range s.branchStates {
if branchID != joinState.WaitingBranchID && branchState.Status == ExecutionStatusCompleted {
completedCount++
}
}
return completedCount >= config.Count
}
// Default: wait for at least 2 branches to complete (minimum for a join)
completedCount := 0
for branchID, branchState := range s.branchStates {
if branchID != joinState.WaitingBranchID && branchState.Status == ExecutionStatusCompleted {
completedCount++
}
}
return completedCount >= 2
}
// GetJoinState returns a copy of the join state for a step
func (s *executionState) GetJoinState(stepName string) *JoinState {
s.mutex.RLock()
defer s.mutex.RUnlock()
if joinState := s.joinStates[stepName]; joinState != nil {
return &JoinState{
StepName: joinState.StepName,
WaitingBranchID: joinState.WaitingBranchID,
Config: joinState.Config,
CreatedAt: joinState.CreatedAt,
}
}
return nil
}
// RemoveJoinState removes a join state after it has been processed
func (s *executionState) RemoveJoinState(stepName string) {
s.mutex.Lock()
defer s.mutex.Unlock()
delete(s.joinStates, stepName)
}
// GetAllJoinStates returns all join states
func (s *executionState) GetAllJoinStates() map[string]*JoinState {
s.mutex.RLock()
defer s.mutex.RUnlock()
result := make(map[string]*JoinState)
for stepName, joinState := range s.joinStates {
result[stepName] = &JoinState{
StepName: joinState.StepName,
WaitingBranchID: joinState.WaitingBranchID,
Config: joinState.Config,
CreatedAt: joinState.CreatedAt,
}
}
return result
}
// ToCheckpoint converts the execution state to a checkpoint
func (s *executionState) ToCheckpoint() *Checkpoint {
s.mutex.RLock()
defer s.mutex.RUnlock()
return &Checkpoint{
SchemaVersion: CheckpointSchemaVersion,
ID: s.executionID + "-" + fmt.Sprintf("%d", time.Now().UnixNano()),
ExecutionID: s.executionID,
WorkflowName: s.workflowName,
Status: s.status,
Inputs: copyMap(s.inputs),
Outputs: copyMap(s.outputs),
Variables: map[string]any{}, // Variables are now per-branch, so global variables are empty
BranchStates: copyBranchStates(s.branchStates),
JoinStates: copyJoinStates(s.joinStates),
BranchCounter: s.pathCounter,
StartTime: s.startTime,
EndTime: s.endTime,
CheckpointAt: time.Now(),
Error: s.err,
}
}
// FromCheckpoint restores execution state from a checkpoint
func (s *executionState) FromCheckpoint(checkpoint *Checkpoint) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.executionID = checkpoint.ExecutionID
s.workflowName = checkpoint.WorkflowName
s.status = checkpoint.Status
s.inputs = copyMap(checkpoint.Inputs)
s.outputs = copyMap(checkpoint.Outputs)
s.branchStates = copyBranchStates(checkpoint.BranchStates)
// Handle backward compatibility for checkpoints without JoinStates
if checkpoint.JoinStates != nil {
s.joinStates = copyJoinStates(checkpoint.JoinStates)
} else {
s.joinStates = make(map[string]*JoinState)
}
s.pathCounter = checkpoint.BranchCounter
s.startTime = checkpoint.StartTime
s.endTime = checkpoint.EndTime
s.err = checkpoint.Error
}
// copyMap creates a deep copy of a map
func copyMap(m map[string]any) map[string]any {
copy := make(map[string]any, len(m))
for k, v := range m {
copy[k] = v
}
return copy
}
// copyBranchStates creates a deep copy of a branch states map
func copyBranchStates(m map[string]*BranchState) map[string]*BranchState {
copy := make(map[string]*BranchState, len(m))
for k, v := range m {
copy[k] = v.Copy()
}
return copy
}
// copyJoinStates creates a deep copy of a join states map
func copyJoinStates(m map[string]*JoinState) map[string]*JoinState {
copy := make(map[string]*JoinState, len(m))
for k, v := range m {
copy[k] = &JoinState{
StepName: v.StepName,
WaitingBranchID: v.WaitingBranchID,
Config: v.Config,
CreatedAt: v.CreatedAt,
}
}
return copy
}
// getNestedField retrieves a nested field from a map using dot notation
// e.g., "user.profile.name" -> map["user"]["profile"]["name"]
func getNestedField(data map[string]any, branch string) (any, bool) {
if branch == "" {
return nil, false
}
// Handle simple case with no dots
if !strings.Contains(branch, ".") {
value, exists := data[branch]
return value, exists
}
// Split branch by dots and traverse
parts := strings.Split(branch, ".")
current := data
for i, part := range parts {
if part == "" {
return nil, false // Empty part in branch
}
value, exists := current[part]
if !exists {
return nil, false
}
// If this is the last part, return the value
if i == len(parts)-1 {
return value, true
}
// Otherwise, expect the value to be a map and continue traversing
if nextMap, ok := value.(map[string]any); ok {
current = nextMap
} else {
return nil, false // Path leads to non-map value before the end
}
}
return nil, false
}
// setNestedField sets a nested field in a map using dot notation
// e.g., "user.profile.name" -> map["user"]["profile"]["name"] = value
// Creates intermediate maps as needed
func setNestedField(data map[string]any, branch string, value any) {
if branch == "" {
return
}
// Handle simple case with no dots
if !strings.Contains(branch, ".") {
data[branch] = value
return
}
// Split branch by dots and traverse, creating maps as needed
parts := strings.Split(branch, ".")
current := data
for i, part := range parts {
if part == "" {
return // Empty part in branch
}
// If this is the last part, set the value
if i == len(parts)-1 {
current[part] = value
return
}
// Otherwise, ensure the next level exists as a map
if existing, exists := current[part]; exists {
if nextMap, ok := existing.(map[string]any); ok {
current = nextMap
} else {
// Existing value is not a map, replace it with a map
newMap := make(map[string]any)
current[part] = newMap
current = newMap
}
} else {
// Create new map for this level
newMap := make(map[string]any)
current[part] = newMap
current = newMap
}
}
}