diff --git a/WORKFLOWS.md b/WORKFLOWS.md index 7ceddd6a5..97caf1faf 100644 --- a/WORKFLOWS.md +++ b/WORKFLOWS.md @@ -74,9 +74,9 @@ Any string setting may contain `{{ path }}`, replaced when the step runs: | `task.issue`, `task.pr` | Issue and pull request numbers. | | `task.plan` | The plan a `human.approve` step handed over (an approved PRD). | | `task.review` | The review comments a remediation round addresses. | -| `inputs.` | A declared input. | +| `inputs.` | A declared input; an undeclared one is refused at save. | | `steps..summary` | What an earlier step reported, e.g. an agent's finish summary or a gate's output. | -| `steps..result.` | A field of an earlier agent's structured result. | +| `steps..result.` | A field of an earlier agent's structured result, or an output of an earlier `workflow.call` that waits. A field the agent's `result` schema or the callee's `outputs` does not declare is refused at save. | Only earlier steps may be referenced; a reference to a later or unknown step is refused when the definition is saved. A value that does not exist at run time diff --git a/internal/domain/workflow/workflow_call.go b/internal/domain/workflow/workflow_call.go index e7e8958ea..e10cb6cb7 100644 --- a/internal/domain/workflow/workflow_call.go +++ b/internal/domain/workflow/workflow_call.go @@ -154,8 +154,9 @@ func runWorkflowCall(ctx context.Context, s workflowCallSettings, tc *TaskContex } return err } - if err := tc.finishChildStep(ctx, callStep, taskstate.StepSucceeded, - fmt.Sprintf("%q (task %d) finished", s.Workflow, callee.ID), 0); err != nil { + // Later steps read the callee's outputs as this step's result. + tc.stepResult = StepResult{Summary: fmt.Sprintf("%q (task %d) finished", s.Workflow, callee.ID), Result: calleeOutputs} + if err := tc.finishChildStep(ctx, callStep, taskstate.StepSucceeded, tc.stepResult.Summary, 0); err != nil { return fmt.Errorf("%s: could not be closed: %w", WorkflowCallStepName, err) } return nil @@ -266,6 +267,7 @@ func callSucceeded(status string) bool { func validateWorkflowCalls(parsed map[string]YAMLDefinition) error { calls := make(map[string][]string, len(parsed)) for id, d := range parsed { + results := map[string]callResult{} err := walkSteps(d.Steps, fmt.Sprintf("workflow %q", id), func(where string, step StepRecord) error { if step.Type != WorkflowCallStepName { return nil @@ -285,15 +287,73 @@ func validateWorkflowCalls(parsed map[string]YAMLDefinition) error { return err } calls[id] = append(calls[id], s.Workflow) + if step.ID != "" { + results[step.ID] = callResult{wait: s.Wait, outputs: callee.Outputs} + } return nil }) if err != nil { return err } + if err := checkCallResultReferences(d, fmt.Sprintf("workflow %q", id), results); err != nil { + return err + } } return refuseCallCycles(calls) } +// callResult is what a workflow.call step leaves as its result: the callee's +// declared outputs, and only when the call waits for them. +type callResult struct { + wait bool + outputs map[string]task.OutputSpec +} + +// checkCallResultReferences refuses a reference to a call's result field the +// callee does not declare as an output, or to the result of a call that does +// not wait and so has none, in settings and when conditions alike. +func checkCallResultReferences(d YAMLDefinition, where string, results map[string]callResult) error { + if len(results) == 0 { + return nil + } + check := func(at, path string) error { + parts := strings.Split(path, ".") + if len(parts) < 4 || parts[0] != "steps" || parts[2] != "result" { + return nil + } + call, ok := results[parts[1]] + switch { + case !ok: + return nil + case !call.wait: + return fmt.Errorf("%s: reference {{ %s }}: call %q does not wait, so it has no result", at, path, parts[1]) + } + if _, declared := call.outputs[parts[3]]; !declared { + return fmt.Errorf("%s: reference {{ %s }}: the workflow call %q declares no output %q", at, path, parts[1], parts[3]) + } + return nil + } + return walkSteps(d.Steps, where, func(at string, step StepRecord) error { + var problem error + settings := step.Settings + walkStrings(&settings, func(value string) string { + for _, match := range referencePattern.FindAllStringSubmatch(value, -1) { + if err := check(at, match[1]); err != nil && problem == nil { + problem = err + } + } + return value + }) + if problem != nil || step.When == "" { + return problem + } + if c, err := parseCondition(step.When); err == nil { + return check(at+" when", c.path) + } + return nil + }) +} + // walkSteps visits each step a definition carries in order, descending into // parallel branches. A call inside a branch is a call like any other, so // validation must see it and name it by its branch. where locates the step in diff --git a/internal/domain/workflow/workflow_call_test.go b/internal/domain/workflow/workflow_call_test.go index 1f1981c7c..29b7d3524 100644 --- a/internal/domain/workflow/workflow_call_test.go +++ b/internal/domain/workflow/workflow_call_test.go @@ -69,6 +69,62 @@ steps: }, wantErr: "calls itself through", }, + { + name: "a later step reads a waited call's declared output", + defs: map[string]string{ + "caller": `id: caller +repository: none +steps: + - {id: check, type: workflow.call, settings: {workflow: callee, wait: true}} + - type: agent.run + settings: {mission: "{{ steps.check.result.verdict }}"} +`, + "callee": `id: callee +repository: none +outputs: {verdict: {type: string}} +steps: + - type: workflow.finish +`, + }, + }, + { + name: "a call result field the callee does not declare is refused", + defs: map[string]string{ + "caller": `id: caller +repository: none +steps: + - {id: check, type: workflow.call, settings: {workflow: callee, wait: true}} + - type: agent.run + settings: {mission: "{{ steps.check.result.verdit }}"} +`, + "callee": `id: callee +repository: none +outputs: {verdict: {type: string}} +steps: + - type: workflow.finish +`, + }, + wantErr: `the workflow call "check" declares no output "verdit"`, + }, + { + name: "a call that does not wait has no result", + defs: map[string]string{ + "caller": `id: caller +repository: none +steps: + - {id: check, type: workflow.call, settings: {workflow: callee, wait: false}} + - type: agent.run + settings: {mission: "{{ steps.check.result.verdict }}"} +`, + "callee": `id: callee +repository: none +outputs: {verdict: {type: string}} +steps: + - type: workflow.finish +`, + }, + wantErr: `call "check" does not wait, so it has no result`, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {