diff --git a/internal/domain/workflow/definition.go b/internal/domain/workflow/definition.go index db87860cc..60655b6ca 100644 --- a/internal/domain/workflow/definition.go +++ b/internal/domain/workflow/definition.go @@ -110,9 +110,43 @@ func ParseDefinition(src string, registry StepRegistry) (YAMLDefinition, error) return YAMLDefinition{}, fmt.Errorf("workflow %q step %d: %w", definition.ID, i+1, err) } } + if err := checkOutputCaptureCollisions(definition.ID, definition.Outputs, definition.Steps); err != nil { + return YAMLDefinition{}, err + } return definition, nil } +// checkOutputCaptureCollisions refuses a definition that declares an output +// named result beside an agent.run step capturing its structured result +// through the capture tool of the same name. appendOutputTools refuses that +// clash when the step runs, so a definition carrying it must not save. +func checkOutputCaptureCollisions(workflowID string, outputs map[string]task.OutputSpec, steps []StepRecord) error { + if _, declared := outputs[resultToolName]; !declared { + return nil + } + for _, step := range steps { + if step.Type == AgentRunStepName { + var s agentRunSettings + if err := step.Settings.Decode(&s); err == nil && s.Result != nil { + return fmt.Errorf("workflow %q: output %q collides with agent.run step %q's result capture tool %q; rename the output or remove settings.result", workflowID, resultToolName, step.StageName(), resultToolName) + } + } + for _, branch := range step.Parallel { + if err := checkOutputCaptureCollisions(workflowID, outputs, branch); err != nil { + return err + } + } + if step.Switch != nil { + for _, branch := range step.Switch.Cases { + if err := checkOutputCaptureCollisions(workflowID, outputs, branch); err != nil { + return err + } + } + } + } + return nil +} + // PackageOf returns the package a pinned definition was projected from, or // empty for a workflow the operator wrote. func PackageOf(src string) string { diff --git a/internal/domain/workflow/output_collision_test.go b/internal/domain/workflow/output_collision_test.go new file mode 100644 index 000000000..8cf609353 --- /dev/null +++ b/internal/domain/workflow/output_collision_test.go @@ -0,0 +1,64 @@ +package workflow + +import "testing" + +func TestParseDefinitionResultOutputCollision(t *testing.T) { + registry := StepRegistry{AgentRunStepName: newAgentRunStage} + tests := []struct { + name string + yaml string + wantErr bool + }{ + {"top-level agent.run", `id: w +repository: none +outputs: {result: {type: string}} +steps: + - id: classify + type: agent.run + settings: + mission: m + result: {type: object, properties: {verdict: {type: string}}} +`, true}, + {"switch case", `id: w +repository: none +inputs: {kind: {type: string}} +outputs: {result: {type: string}} +steps: + - switch: + on: inputs.kind + cases: + review: + - id: classify + type: agent.run + settings: + mission: m + result: {type: object, properties: {verdict: {type: string}}} +`, true}, + {"no step result schema", `id: w +repository: none +outputs: {result: {type: string}} +steps: + - id: classify + type: agent.run + settings: {mission: m} +`, false}, + {"output under another name", `id: w +repository: none +outputs: {verdict: {type: string}} +steps: + - id: classify + type: agent.run + settings: + mission: m + result: {type: object, properties: {verdict: {type: string}}} +`, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, err := ParseDefinition(tt.yaml, registry) + if (err != nil) != tt.wantErr { + t.Fatalf("ParseDefinition err = %v, wantErr %t", err, tt.wantErr) + } + }) + } +}