chore(workflow): remove the needs block (#1888)
ci/woodpecker/pr/quality Pipeline was successful
Quality / check (pull_request) Successful in 3m8s

This commit is contained in:
sam committed 2026-11-13 02:11:22 +11:00
1 parent fbc9ed21e7
commit 606da1aee5
5 files changed
+106 -53

No files matched your search

+36 -1
View File
@@ -86,10 +86,45 @@ type StepFactory func(yaml.Node) (Stage, error)
// StepRegistry is the closed set of safe workflow operations available to YAML.
type StepRegistry map[string]StepFactory
// dropNeeds removes a needs: block a definition written before the field went
// away may still carry. The key is deleted from the document's root mapping,
// so the caller's strict decode sees no trace of it while every other key,
// including those inside steps, keeps its original wording.
func dropNeeds(src string) ([]byte, error) {
decoder := yaml.NewDecoder(strings.NewReader(src))
var document yaml.Node
if err := decoder.Decode(&document); err != nil {
return nil, fmt.Errorf("parse workflow YAML: %w", err)
}
if document.Kind == yaml.DocumentNode && len(document.Content) > 0 {
if root := document.Content[0]; root.Kind == yaml.MappingNode {
for i := 0; i+1 < len(root.Content); i += 2 {
if root.Content[i].Value == "needs" {
root.Content = slices.Delete(root.Content, i, i+2)
break
}
}
}
}
var encoded bytes.Buffer
encoder := yaml.NewEncoder(&encoded)
if err := encoder.Encode(&document); err != nil {
return nil, err
}
if err := encoder.Close(); err != nil {
return nil, err
}
return encoded.Bytes(), nil
}
// ParseDefinition strictly parses and validates one YAML workflow.
func ParseDefinition(src string, registry StepRegistry) (YAMLDefinition, error) {
document, err := dropNeeds(src)
if err != nil {
return YAMLDefinition{}, err
}
var definition YAMLDefinition
decoder := yaml.NewDecoder(strings.NewReader(src))
decoder := yaml.NewDecoder(bytes.NewReader(document))
decoder.KnownFields(true)
if err := decoder.Decode(&definition); err != nil {
return YAMLDefinition{}, fmt.Errorf("parse workflow YAML: %w", err)
@@ -0,0 +1,70 @@
package workflow
import (
"encoding/json"
"testing"
)
// Definitions written before the needs block went away still carry a needs
// entry in their YAML. The load path drops it, so a definition that parsed
// when it was saved still loads now, while strictness for everything else is
// unchanged.
func TestParseDefinitionDropsStoredNeeds(t *testing.T) {
t.Parallel()
var shipped string
for _, entry := range ShippedDefinitions().Definitions {
if entry.ID == "triage" {
shipped = entry.YAML
}
}
if shipped == "" {
t.Fatal("the triage definition is not shipped")
}
tests := []struct {
name string
yaml string
wantErr bool
}{
{
name: "a stored definition carrying needs still loads",
yaml: shipped + "needs:\n captures: true\n gate_retries: 3\n",
},
{
// The strip must drop exactly the needs key, not sed the parser
// into a lenient one: anything else unknown is still refused.
name: "a stored definition with another unknown key is still refused",
yaml: shipped + "bogus: 1\n",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
// Ship the whole collection: a call target only validates
// against the workflows beside it.
entries := []WorkflowDefinitionEntry{}
for _, entry := range ShippedDefinitions().Definitions {
if entry.ID == "triage" {
entry.YAML = tt.yaml
}
entries = append(entries, entry)
}
value, err := json.Marshal(WorkflowDefinitionCollection{Definitions: entries})
if err != nil {
t.Fatal(err)
}
collection, err := DecodeDefinitionCollection(value, triageRegistry())
if (err != nil) != tt.wantErr {
t.Fatalf("DecodeDefinitionCollection() err = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErr {
return
}
if len(collection.Definitions) != len(entries) {
t.Fatalf("kept %d definitions, want %d", len(collection.Definitions), len(entries))
}
})
}
}
@@ -63,29 +63,6 @@ type WorkflowInterface struct {
// Instructions are the workflow's layer of the instructions every agent
// stage of a run follows.
Instructions string `yaml:"instructions,omitempty" json:"instructions,omitempty"`
// DeclaredNeeds is what the definition's needs: block declares. Read
// Needs, not this, when deciding a harness requirement: a declared output
// forces the captures capability and Needs resolves it.
DeclaredNeeds WorkflowNeeds `yaml:"needs" json:"needs"`
}
// Needs returns the declared needs plus captures when outputs are declared.
func (w WorkflowInterface) Needs() WorkflowNeeds {
needs := w.DeclaredNeeds
if len(w.Outputs) > 0 {
needs.Captures = true
}
return needs
}
// WorkflowNeeds is WorkflowInterface's declared harness requirements.
type WorkflowNeeds struct {
// Captures declares that a stage returns structured output through
// capture tools.
Captures bool `yaml:"captures,omitempty" json:"captures,omitempty"`
// GateRetries is the largest gate-retry budget any stage declares. Zero
// means no stage gates its result, so no resume capability is needed.
GateRetries int `yaml:"gate_retries,omitempty" json:"gate_retries,omitempty"`
}
var (
@@ -127,9 +104,6 @@ func (w WorkflowInterface) Validate() error {
return fmt.Errorf("workflow output %q type %q must be one of %s", name, spec.Type, strings.Join(inputTypes, ", "))
}
}
if w.DeclaredNeeds.GateRetries < 0 {
return fmt.Errorf("workflow needs.gate_retries must not be negative")
}
return nil
}