fix(workflow): validate handoff and approve targets at save
This commit is contained in:
1 parent
b38b3f6961
commit
24df173a31
3 files changed
+350
-1
No files matched your search
@@ -3,9 +3,13 @@ package workflow
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
)
|
||||
|
||||
// Control step types: they decide where the task goes next.
|
||||
@@ -166,3 +170,133 @@ func truthy(value any) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// validateControlTargets checks every handoff and approve step's target. A
|
||||
// literal target must name a workflow the task can follow; a target templated
|
||||
// from an agent result must resolve to one of the values the referenced enum
|
||||
// allows. A templated target with no enum has no fixed set to check.
|
||||
func validateControlTargets(parsed map[string]YAMLDefinition) error {
|
||||
for id, d := range parsed {
|
||||
err := walkSteps(d.Steps, fmt.Sprintf("workflow %q", id), func(where string, step StepRecord) error {
|
||||
target, ok, err := controlTarget(step)
|
||||
if err != nil || !ok {
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", where, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if !referencePattern.MatchString(target) {
|
||||
return checkControlTarget(where, d.WorkflowInterface, parsed, target)
|
||||
}
|
||||
for _, name := range targetEnum(d, target) {
|
||||
if err := checkControlTarget(where, d.WorkflowInterface, parsed, name); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// controlTarget is the workflow a handoff or approve step routes to, and
|
||||
// whether the step is one of those.
|
||||
func controlTarget(step StepRecord) (string, bool, error) {
|
||||
var target string
|
||||
switch step.Type {
|
||||
case HandoffStepName:
|
||||
var s handoffSettings
|
||||
if err := decodeSettings(HandoffStepName, step.Settings, &s); err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
target = strings.TrimSpace(s.Workflow)
|
||||
case ApproveStepName:
|
||||
var s approveSettings
|
||||
if err := decodeSettings(ApproveStepName, step.Settings, &s); err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
target = strings.TrimSpace(s.Then)
|
||||
default:
|
||||
return "", false, nil
|
||||
}
|
||||
return target, true, nil
|
||||
}
|
||||
|
||||
// checkControlTarget refuses a target workflow the task cannot follow: one
|
||||
// that is not defined, one that needs a repository the task does not have, or
|
||||
// one whose declared inputs the task's own inputs cannot satisfy.
|
||||
func checkControlTarget(where string, source task.WorkflowInterface, parsed map[string]YAMLDefinition, target string) error {
|
||||
wf, ok := parsed[target]
|
||||
if !ok {
|
||||
return fmt.Errorf("%s targets %q, which is not defined", where, target)
|
||||
}
|
||||
if wf.RepositoryMode() == task.RepositoryRequired && source.RepositoryMode() != task.RepositoryRequired {
|
||||
return fmt.Errorf("%s targets %q, which needs a repository the task does not have", where, target)
|
||||
}
|
||||
for _, name := range slices.Sorted(maps.Keys(source.Inputs)) {
|
||||
if _, declared := wf.Inputs[name]; !declared {
|
||||
return fmt.Errorf("%s targets %q, which does not declare input %q the task carries", where, target, name)
|
||||
}
|
||||
}
|
||||
for _, name := range slices.Sorted(maps.Keys(wf.Inputs)) {
|
||||
if wf.Inputs[name].Required && !source.Inputs[name].Required {
|
||||
return fmt.Errorf("%s targets %q, which requires input %q the task may not carry", where, target, name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// targetEnum returns the values a templated target can resolve to: the enum of
|
||||
// the agent result field a "{{ steps.<id>.result.<field> }}" reference reads. A
|
||||
// target that is not exactly such a reference, or reads a field without an
|
||||
// enum, has no fixed set to check and returns none.
|
||||
func targetEnum(d YAMLDefinition, target string) []string {
|
||||
match := referencePattern.FindStringSubmatch(target)
|
||||
if match == nil || strings.TrimSpace(target) != match[0] {
|
||||
return nil
|
||||
}
|
||||
parts := strings.Split(match[1], ".")
|
||||
if len(parts) != 4 || parts[0] != "steps" || parts[2] != "result" {
|
||||
return nil
|
||||
}
|
||||
step, ok := findStep(d.Steps, parts[1])
|
||||
if !ok || step.Type != AgentRunStepName {
|
||||
return nil
|
||||
}
|
||||
var s agentRunSettings
|
||||
if err := step.Settings.Decode(&s); err != nil {
|
||||
return nil
|
||||
}
|
||||
properties, _ := s.Result["properties"].(map[string]any)
|
||||
field, _ := properties[parts[3]].(map[string]any)
|
||||
values, _ := field["enum"].([]any)
|
||||
names := make([]string, 0, len(values))
|
||||
for _, value := range values {
|
||||
name, ok := value.(string)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
names = append(names, name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// findStep resolves a step by id anywhere in a definition, including a step
|
||||
// inside a parallel branch, whose result stays readable after the parallel
|
||||
// step.
|
||||
func findStep(steps []StepRecord, id string) (StepRecord, bool) {
|
||||
for _, step := range steps {
|
||||
if step.ID == id {
|
||||
return step, true
|
||||
}
|
||||
for _, branch := range step.Parallel {
|
||||
if found, ok := findStep(branch, id); ok {
|
||||
return found, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return StepRecord{}, false
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
package workflow
|
||||
|
||||
import (
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
)
|
||||
|
||||
func TestValidateControlTargets(t *testing.T) {
|
||||
registry := StepRegistry{
|
||||
AgentRunStepName: newAgentRunStage,
|
||||
FinishStepName: newFinishStage,
|
||||
HandoffStepName: newHandoffStage,
|
||||
ApproveStepName: newApproveStage,
|
||||
WorkflowCallStepName: newWorkflowCallStage,
|
||||
}
|
||||
defined := `id: defined
|
||||
repository: none
|
||||
steps:
|
||||
- type: workflow.finish
|
||||
`
|
||||
tests := []struct {
|
||||
name string
|
||||
defs map[string]string
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "a literal handoff to a defined workflow is accepted",
|
||||
defs: map[string]string{
|
||||
"source": `id: source
|
||||
repository: none
|
||||
steps:
|
||||
- type: workflow.handoff
|
||||
settings: {workflow: defined}
|
||||
`,
|
||||
"defined": defined,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "an approve target to a defined workflow is accepted",
|
||||
defs: map[string]string{
|
||||
"source": `id: source
|
||||
repository: none
|
||||
steps:
|
||||
- type: human.approve
|
||||
settings: {plan: p, then: defined}
|
||||
`,
|
||||
"defined": defined,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "an unknown literal handoff is refused naming the step",
|
||||
defs: map[string]string{
|
||||
"source": `id: source
|
||||
repository: none
|
||||
steps:
|
||||
- type: workflow.finish
|
||||
- type: workflow.handoff
|
||||
settings: {workflow: missing}
|
||||
`,
|
||||
},
|
||||
wantErr: `workflow "source" step 2 targets "missing", which is not defined`,
|
||||
},
|
||||
{
|
||||
name: "an unknown approve target is refused naming the step",
|
||||
defs: map[string]string{
|
||||
"source": `id: source
|
||||
repository: none
|
||||
steps:
|
||||
- type: human.approve
|
||||
settings: {plan: p, then: missing}
|
||||
`,
|
||||
},
|
||||
wantErr: `workflow "source" step 1 targets "missing", which is not defined`,
|
||||
},
|
||||
{
|
||||
name: "a target needing a repository the task does not have is refused",
|
||||
defs: map[string]string{
|
||||
"source": `id: source
|
||||
repository: none
|
||||
steps:
|
||||
- type: workflow.handoff
|
||||
settings: {workflow: defined}
|
||||
`,
|
||||
"defined": `id: defined
|
||||
steps:
|
||||
- type: workflow.finish
|
||||
`,
|
||||
},
|
||||
wantErr: `which needs a repository the task does not have`,
|
||||
},
|
||||
{
|
||||
name: "a target requiring an input the task may not carry is refused",
|
||||
defs: map[string]string{
|
||||
"source": `id: source
|
||||
repository: none
|
||||
steps:
|
||||
- type: workflow.handoff
|
||||
settings: {workflow: defined}
|
||||
`,
|
||||
"defined": `id: defined
|
||||
repository: none
|
||||
inputs:
|
||||
pr_number: {type: number, required: true}
|
||||
steps:
|
||||
- type: workflow.finish
|
||||
`,
|
||||
},
|
||||
wantErr: `which requires input "pr_number" the task may not carry`,
|
||||
},
|
||||
{
|
||||
name: "a target that drops an input the task carries is refused",
|
||||
defs: map[string]string{
|
||||
"source": `id: source
|
||||
repository: none
|
||||
inputs:
|
||||
plan: {type: string, required: true}
|
||||
steps:
|
||||
- type: workflow.handoff
|
||||
settings: {workflow: defined}
|
||||
`,
|
||||
"defined": defined,
|
||||
},
|
||||
wantErr: `which does not declare input "plan" the task carries`,
|
||||
},
|
||||
{
|
||||
name: "an enum target whose values are all defined is accepted",
|
||||
defs: map[string]string{
|
||||
"source": `id: source
|
||||
repository: none
|
||||
steps:
|
||||
- id: classify
|
||||
type: agent.run
|
||||
settings:
|
||||
mission: m
|
||||
read_only: true
|
||||
result:
|
||||
type: object
|
||||
properties:
|
||||
workflow: {type: string, enum: [alpha, beta]}
|
||||
- type: workflow.handoff
|
||||
settings: {workflow: "{{ steps.classify.result.workflow }}"}
|
||||
`,
|
||||
"alpha": strings.Replace(defined, "id: defined", "id: alpha", 1),
|
||||
"beta": strings.Replace(defined, "id: defined", "id: beta", 1),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "an enum target with an undefined value is refused",
|
||||
defs: map[string]string{
|
||||
"source": `id: source
|
||||
repository: none
|
||||
steps:
|
||||
- id: classify
|
||||
type: agent.run
|
||||
settings:
|
||||
mission: m
|
||||
read_only: true
|
||||
result:
|
||||
type: object
|
||||
properties:
|
||||
workflow: {type: string, enum: [alpha, missing]}
|
||||
- type: workflow.handoff
|
||||
settings: {workflow: "{{ steps.classify.result.workflow }}"}
|
||||
`,
|
||||
"alpha": strings.Replace(defined, "id: defined", "id: alpha", 1),
|
||||
},
|
||||
wantErr: `targets "missing", which is not defined`,
|
||||
},
|
||||
{
|
||||
name: "a templated target without an enum is not checked",
|
||||
defs: map[string]string{
|
||||
"source": `id: source
|
||||
repository: none
|
||||
steps:
|
||||
- id: classify
|
||||
type: agent.run
|
||||
settings:
|
||||
mission: m
|
||||
read_only: true
|
||||
result:
|
||||
type: object
|
||||
properties:
|
||||
workflow: {type: string}
|
||||
- type: workflow.handoff
|
||||
settings: {workflow: "{{ steps.classify.result.workflow }}"}
|
||||
`,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
collection := task.WorkflowDefinitionCollection{}
|
||||
for _, id := range slices.Sorted(maps.Keys(tt.defs)) {
|
||||
collection.Definitions = append(collection.Definitions, task.WorkflowDefinitionEntry{ID: id, YAML: tt.defs[id]})
|
||||
}
|
||||
err := ValidateDefinitionCollection(collection, registry)
|
||||
if tt.wantErr == "" {
|
||||
if err != nil {
|
||||
t.Fatalf("err = %v, want nil", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
||||
t.Fatalf("err = %v, want %q", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -169,7 +169,10 @@ func ValidateDefinitionCollection(collection WorkflowDefinitionCollection, regis
|
||||
}
|
||||
parsed[entry.ID] = definition
|
||||
}
|
||||
return validateWorkflowCalls(parsed)
|
||||
if err := validateWorkflowCalls(parsed); err != nil {
|
||||
return err
|
||||
}
|
||||
return validateControlTargets(parsed)
|
||||
}
|
||||
|
||||
// DecodeDefinitionCollection strictly decodes the control-plane projection.
|
||||
|
||||
Reference in new issue
Block a user