Add workflow runtime execution and condition validation
This commit is contained in:
@@ -56,6 +56,7 @@ func (v *definitionValidator) validate() {
|
||||
v.validateReachability()
|
||||
v.validateConfirmationGuards()
|
||||
v.validateVariableMappings()
|
||||
v.validateConditions()
|
||||
}
|
||||
|
||||
func (v *definitionValidator) validateNodes() {
|
||||
@@ -250,6 +251,80 @@ func (v *definitionValidator) validateInputSelector(nodeID string, input registr
|
||||
}
|
||||
}
|
||||
|
||||
func (v *definitionValidator) validateConditions() {
|
||||
conditionalSources := make(map[string]bool)
|
||||
defaultSources := make(map[string]bool)
|
||||
for index, edge := range v.def.Edges {
|
||||
field := fmt.Sprintf("edges[%d].condition", index)
|
||||
sourceID := strings.TrimSpace(edge.Source)
|
||||
if edge.Condition == nil {
|
||||
if sourceID != "" {
|
||||
defaultSources[sourceID] = true
|
||||
}
|
||||
continue
|
||||
}
|
||||
if sourceID != "" {
|
||||
conditionalSources[sourceID] = true
|
||||
}
|
||||
v.validateCondition(field, sourceID, edge.Condition)
|
||||
}
|
||||
for sourceID := range conditionalSources {
|
||||
if !defaultSources[sourceID] {
|
||||
v.addError("edges."+sourceID, "conditional branch must include a default edge")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *definitionValidator) validateCondition(field string, sourceNodeID string, condition *dsl.Condition) {
|
||||
if condition == nil {
|
||||
return
|
||||
}
|
||||
operator := strings.TrimSpace(condition.Operator)
|
||||
if operator == "" && strings.TrimSpace(condition.Expression) != "" {
|
||||
v.addError(field+".expression", "free-form condition expressions are not supported")
|
||||
return
|
||||
}
|
||||
if !isSupportedConditionOperator(operator) {
|
||||
v.addError(field+".operator", "unsupported condition operator: "+operator)
|
||||
return
|
||||
}
|
||||
if condition.Left == nil {
|
||||
v.addError(field+".left", "condition left variable is required")
|
||||
return
|
||||
}
|
||||
sourceSelectorNodeID := strings.TrimSpace(condition.Left.NodeID)
|
||||
sourceField := strings.TrimSpace(condition.Left.Field)
|
||||
if sourceSelectorNodeID == "" || sourceField == "" {
|
||||
v.addError(field+".left", "condition left variable is required")
|
||||
return
|
||||
}
|
||||
sourceNode, ok := v.nodesByID[sourceSelectorNodeID]
|
||||
if !ok {
|
||||
v.addError(field+".left", "condition source node does not exist: "+sourceSelectorNodeID)
|
||||
return
|
||||
}
|
||||
if sourceNodeID != "" && !v.hasPath(sourceSelectorNodeID, sourceNodeID, make(map[string]struct{})) && sourceSelectorNodeID != sourceNodeID {
|
||||
v.addError(field+".left", "condition source node is not available before branch: "+sourceSelectorNodeID)
|
||||
return
|
||||
}
|
||||
sourceSpec, ok := v.registry.Get(sourceNode.Type)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if _, ok := findOutputSpec(sourceSpec.OutputSchema, sourceField); !ok {
|
||||
v.addError(field+".left", "condition source field does not exist: "+sourceSelectorNodeID+"."+sourceField)
|
||||
}
|
||||
}
|
||||
|
||||
func isSupportedConditionOperator(operator string) bool {
|
||||
switch strings.TrimSpace(operator) {
|
||||
case "eq", "equals", "neq", "not_equals", "contains", "exists", "not_exists", "truthy", "is_true", "falsy", "is_false", "gt", "gte", "lt", "lte":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (v *definitionValidator) hasPath(sourceID string, targetID string, visiting map[string]struct{}) bool {
|
||||
if sourceID == targetID {
|
||||
return false
|
||||
|
||||
@@ -194,6 +194,34 @@ func TestValidateDefinitionAcceptsMappedKnowledgeFlow(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDefinitionRejectsUnknownConditionOperator(t *testing.T) {
|
||||
def := conditionDefinition()
|
||||
def.Edges[1].Condition.Operator = "regex"
|
||||
|
||||
result := validator.ValidateDefinition(def, registry.DefaultRegistry())
|
||||
|
||||
if result.Valid {
|
||||
t.Fatalf("expected unknown condition operator to be invalid")
|
||||
}
|
||||
if !hasValidationMessage(result, "unsupported condition operator") {
|
||||
t.Fatalf("expected condition operator error, got %#v", result.Errors)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDefinitionRejectsUnknownConditionVariable(t *testing.T) {
|
||||
def := conditionDefinition()
|
||||
def.Edges[1].Condition.Left.Field = "missing"
|
||||
|
||||
result := validator.ValidateDefinition(def, registry.DefaultRegistry())
|
||||
|
||||
if result.Valid {
|
||||
t.Fatalf("expected unknown condition variable to be invalid")
|
||||
}
|
||||
if !hasValidationMessage(result, "condition source field does not exist") {
|
||||
t.Fatalf("expected condition variable error, got %#v", result.Errors)
|
||||
}
|
||||
}
|
||||
|
||||
func minimalDefinition() dsl.Definition {
|
||||
return dsl.Definition{
|
||||
SchemaVersion: 1,
|
||||
@@ -220,6 +248,32 @@ func mappedReplyDefinition() dsl.Definition {
|
||||
return def
|
||||
}
|
||||
|
||||
func conditionDefinition() dsl.Definition {
|
||||
return dsl.Definition{
|
||||
SchemaVersion: 1,
|
||||
EntryNodeID: "start_1",
|
||||
Nodes: []dsl.Node{
|
||||
{ID: "start_1", Type: "start"},
|
||||
{ID: "condition_1", Type: "condition"},
|
||||
{ID: "end_1", Type: "end"},
|
||||
},
|
||||
Edges: []dsl.Edge{
|
||||
{ID: "e1", Source: "start_1", Target: "condition_1"},
|
||||
{
|
||||
ID: "e2",
|
||||
Source: "condition_1",
|
||||
Target: "end_1",
|
||||
Condition: &dsl.Condition{
|
||||
Left: &dsl.VariableSelector{NodeID: "start_1", Field: "userMessage"},
|
||||
Operator: "eq",
|
||||
Right: "hello",
|
||||
},
|
||||
},
|
||||
{ID: "e3", Source: "condition_1", Target: "end_1"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func hasValidationMessage(result validator.Result, want string) bool {
|
||||
for _, item := range result.Errors {
|
||||
if strings.Contains(item.Message, want) {
|
||||
|
||||
Reference in New Issue
Block a user