diff --git a/internal/domain/workflow/definition.go b/internal/domain/workflow/definition.go index 7019f40a1..8801a7bfe 100644 --- a/internal/domain/workflow/definition.go +++ b/internal/domain/workflow/definition.go @@ -87,7 +87,7 @@ func ParseDefinition(src string, registry StepRegistry) (YAMLDefinition, error) return YAMLDefinition{}, fmt.Errorf("workflow %q: %w", definition.ID, err) } mode := definition.RepositoryMode() - earlier := map[string]bool{} + earlier := newRefScope(definition.Inputs) for i, step := range definition.Steps { if err := checkStep(step, mode, earlier, registry, false); err != nil { return YAMLDefinition{}, fmt.Errorf("workflow %q step %d: %w", definition.ID, i+1, err) diff --git a/internal/domain/workflow/step_flow.go b/internal/domain/workflow/step_flow.go index 30d485752..b5b8589b5 100644 --- a/internal/domain/workflow/step_flow.go +++ b/internal/domain/workflow/step_flow.go @@ -19,7 +19,7 @@ const maxRetryAttempts = 10 // checkStep validates one step against the vocabulary and the steps before // it, and adds the ids it declares to earlier. -func checkStep(step StepRecord, mode task.RepositoryMode, earlier map[string]bool, registry StepRegistry, inBranch bool) error { +func checkStep(step StepRecord, mode task.RepositoryMode, earlier *refScope, registry StepRegistry, inBranch bool) error { if err := checkStepID(step.ID, earlier); err != nil { return err } @@ -34,14 +34,14 @@ func checkStep(step StepRecord, mode task.RepositoryMode, earlier map[string]boo return err } if step.ID != "" { - earlier[step.ID] = true + earlier.steps[step.ID] = resultFields(step) } return nil } // checkStepControls validates the settings every step carries: when, // on_failure and retry. -func checkStepControls(step StepRecord, earlier map[string]bool) error { +func checkStepControls(step StepRecord, earlier *refScope) error { if step.OnFailure != "" && step.OnFailure != "park" && step.OnFailure != onFailureContinue { return fmt.Errorf("on_failure is park or continue, not %q", step.OnFailure) } @@ -66,7 +66,7 @@ func checkStepControls(step StepRecord, earlier map[string]bool) error { return nil } -func checkTypedStep(step StepRecord, mode task.RepositoryMode, earlier map[string]bool, registry StepRegistry, inBranch bool) error { +func checkTypedStep(step StepRecord, mode task.RepositoryMode, earlier *refScope, registry StepRegistry, inBranch bool) error { factory, ok := registry[step.Type] if !ok { return fmt.Errorf("unknown type %q", step.Type) @@ -89,7 +89,7 @@ func checkTypedStep(step StepRecord, mode task.RepositoryMode, earlier map[strin // checkParallel validates each branch on its own: a branch sees the steps // before the parallel step and its own earlier steps, never a sibling's. // Every branch's ids are visible after the parallel step. -func checkParallel(step StepRecord, mode task.RepositoryMode, earlier map[string]bool, registry StepRegistry, inBranch bool) error { +func checkParallel(step StepRecord, mode task.RepositoryMode, earlier *refScope, registry StepRegistry, inBranch bool) error { switch { case step.Type != "" || step.Settings.Kind != 0: return errors.New("a parallel step has branches, not a type or settings") @@ -98,7 +98,7 @@ func checkParallel(step StepRecord, mode task.RepositoryMode, earlier map[string case len(step.Parallel) < 2: return errors.New("parallel needs at least two branches") } - declared := map[string]bool{} + declared := map[string]map[string]bool{} for _, name := range slices.Sorted(maps.Keys(step.Parallel)) { if !stableid.Valid(name) { return fmt.Errorf("branch name %q is not a stable identifier", name) @@ -107,20 +107,20 @@ func checkParallel(step StepRecord, mode task.RepositoryMode, earlier map[string if len(branch) == 0 { return fmt.Errorf("branch %q has no steps", name) } - seen := maps.Clone(earlier) + seen := earlier.clone() for i, branchStep := range branch { - if declared[branchStep.ID] { + if _, twice := declared[branchStep.ID]; twice { return fmt.Errorf("branch %q step %d: step id %q is declared twice", name, i+1, branchStep.ID) } if err := checkStep(branchStep, mode, seen, registry, true); err != nil { return fmt.Errorf("branch %q step %d: %w", name, i+1, err) } if branchStep.ID != "" { - declared[branchStep.ID] = true + declared[branchStep.ID] = resultFields(branchStep) } } } - maps.Copy(earlier, declared) + maps.Copy(earlier.steps, declared) return nil } diff --git a/internal/domain/workflow/step_refs.go b/internal/domain/workflow/step_refs.go index b2b0bcea9..3b5faa311 100644 --- a/internal/domain/workflow/step_refs.go +++ b/internal/domain/workflow/step_refs.go @@ -10,6 +10,7 @@ import ( "gopkg.in/yaml.v3" "github.com/samcharles93/archie-core/internal/domain/stableid" + "github.com/samcharles93/archie-core/internal/domain/workflow/task" ) // StepResult is what a finished step leaves for later steps to reference as @@ -22,9 +23,57 @@ type StepResult struct { // referencePattern matches {{ path }} in a step's string settings. var referencePattern = regexp.MustCompile(`\{\{\s*([^{}\s]+)\s*\}\}`) +// refScope is what a step's references may read: the workflow's declared +// inputs and the steps declared before it, each with the result fields its +// schema declares. A step with no result schema has nil fields, and any field +// of its result is accepted. +type refScope struct { + inputs map[string]bool + steps map[string]map[string]bool +} + +func newRefScope(inputs map[string]task.InputSpec) *refScope { + scope := &refScope{inputs: map[string]bool{}, steps: map[string]map[string]bool{}} + for name := range inputs { + scope.inputs[name] = true + } + return scope +} + +func (s *refScope) has(id string) bool { + _, ok := s.steps[id] + return ok +} + +func (s *refScope) clone() *refScope { + return &refScope{inputs: s.inputs, steps: maps.Clone(s.steps)} +} + +// resultFields returns the fields step's result schema declares, or nil when +// it declares none. +func resultFields(step StepRecord) map[string]bool { + if step.Type != AgentRunStepName { + return nil + } + var s agentRunSettings + if err := step.Settings.Decode(&s); err != nil { + return nil + } + properties, ok := s.Result["properties"].(map[string]any) + if !ok { + return nil + } + fields := make(map[string]bool, len(properties)) + for name := range properties { + fields[name] = true + } + return fields +} + // checkReferences rejects a reference a run could not resolve: an unknown -// root, or a step id that is not declared by an earlier step. -func checkReferences(settings yaml.Node, earlier map[string]bool) error { +// root, an undeclared input, a step id that is not declared by an earlier +// step, or a result field that step's schema does not declare. +func checkReferences(settings yaml.Node, earlier *refScope) error { var problem error walkStrings(&settings, func(value string) string { for _, match := range referencePattern.FindAllStringSubmatch(value, -1) { @@ -37,7 +86,7 @@ func checkReferences(settings yaml.Node, earlier map[string]bool) error { return problem } -func checkReference(path string, earlier map[string]bool) error { +func checkReference(path string, earlier *refScope) error { parts := strings.Split(path, ".") switch parts[0] { case "task", "inputs": @@ -47,22 +96,34 @@ func checkReference(path string, earlier map[string]bool) error { if parts[0] == "task" && !taskFields[parts[1]] { return fmt.Errorf("reference {{ %s }}: task has no field %q", path, parts[1]) } + if parts[0] == "inputs" && !earlier.inputs[parts[1]] { + return fmt.Errorf("reference {{ %s }}: the workflow declares no input %q", path, parts[1]) + } return nil case "steps": - if len(parts) < 3 { - return fmt.Errorf("reference {{ %s }} must name a step and a field, e.g. steps.plan.summary", path) - } - if !earlier[parts[1]] { - return fmt.Errorf("reference {{ %s }}: no earlier step has id %q", path, parts[1]) - } - if parts[2] != "summary" && parts[2] != "result" { - return fmt.Errorf("reference {{ %s }}: a step exposes summary and result", path) - } - return nil + return checkStepReference(path, parts, earlier) } return fmt.Errorf("reference {{ %s }} must start with task, inputs or steps", path) } +// checkStepReference checks steps..summary and steps..result. +// against the steps declared before the referencing one. +func checkStepReference(path string, parts []string, earlier *refScope) error { + if len(parts) < 3 { + return fmt.Errorf("reference {{ %s }} must name a step and a field, e.g. steps.plan.summary", path) + } + if !earlier.has(parts[1]) { + return fmt.Errorf("reference {{ %s }}: no earlier step has id %q", path, parts[1]) + } + if parts[2] != "summary" && parts[2] != "result" { + return fmt.Errorf("reference {{ %s }}: a step exposes summary and result", path) + } + if fields := earlier.steps[parts[1]]; parts[2] == "result" && len(parts) > 3 && fields != nil && !fields[parts[3]] { + return fmt.Errorf("reference {{ %s }}: step %q's result schema has no field %q", path, parts[1], parts[3]) + } + return nil +} + var taskFields = map[string]bool{"review": true, "plan": true, "title": true, "body": true, "prompt": true, "repository": true, "kind": true, "issue": true, "pr": true} // renderSettings returns a copy of settings with every reference replaced by @@ -146,14 +207,14 @@ func deepCopyNode(node *yaml.Node) *yaml.Node { // checkStepID requires each declared step id to be a stable identifier and // unique within the workflow. -func checkStepID(id string, seen map[string]bool) error { +func checkStepID(id string, seen *refScope) error { if id == "" { return nil } if !stableid.Valid(id) { return fmt.Errorf("step id %q is not a stable identifier", id) } - if seen[id] { + if seen.has(id) { return fmt.Errorf("step id %q is declared twice", id) } return nil diff --git a/internal/domain/workflow/step_refs_test.go b/internal/domain/workflow/step_refs_test.go index e6c2efccd..f2935041e 100644 --- a/internal/domain/workflow/step_refs_test.go +++ b/internal/domain/workflow/step_refs_test.go @@ -108,6 +108,51 @@ steps: `, rendered: "build the plan / go / ", }, + { + name: "an undeclared input is refused", + yaml: `id: w +repository: none +steps: + - type: agent.run + settings: {mission: "{{ inputs.ticket }}"} +`, + wantErr: `reference {{ inputs.ticket }}: the workflow declares no input "ticket"`, + }, + { + name: "when may only read a declared input", + yaml: `id: w +repository: none +inputs: {ticket: {type: string}} +steps: + - {type: workflow.finish, when: "inputs.other"} +`, + wantErr: `when: reference {{ inputs.other }}: the workflow declares no input "other"`, + }, + { + name: "a result field outside the step's schema is refused", + yaml: `id: w +repository: none +steps: + - id: plan + type: agent.run + settings: {mission: m, result: {type: object, properties: {verdict: {type: string}}}} + - type: agent.run + settings: {mission: "{{ steps.plan.result.verdit }}"} +`, + wantErr: `step "plan"'s result schema has no field "verdit"`, + }, + { + name: "when may only read a declared result field", + yaml: `id: w +repository: none +steps: + - id: plan + type: agent.run + settings: {mission: m, result: {type: object, properties: {verdict: {type: string}}}} + - {type: workflow.finish, when: "steps.plan.result.fit"} +`, + wantErr: `when: reference {{ steps.plan.result.fit }}: step "plan"'s result schema has no field "fit"`, + }, { name: "retry attempts are bounded", yaml: `id: w