Files
archie/internal/domain/workflow/task/interface.go
T
sam c431834e2b feat(eda): workflow inputs, optional repository and agent profiles
Workflow definitions declare inputs, repository (none, optional or
required; default required) and profile. A binding assigns each input
from a mapped parameter or a constant and may take its repository from a
parameter, which must name a configured repo; both are checked when the
binding is saved and again at dispatch. Inputs travel as structured task
data (tasks.inputs, the task brief, and a JSON block in the agent's
mission), never as body text.

A task with no repository runs in a per-task scratch workspace with no
clone and no publication grant. The agent.run step runs one mission
without a repository and finishes the task in the new completed status.

Agent profiles live under [containers.profiles.<name>]: an image and an
allowlist over the tools archie adds (MCP, scripts, plugins).
[containers].image is the default profile; an unconfigured profile parks
the task. The workflow is now pinned before the container starts so the
profile can choose its image.

Also stop two archied tests writing embedded NATS state into the package
directory, refuse an empty state_dir for embedded NATS, and drop the NATS
store files an earlier lane commit tracked.
2026-09-24 20:29:33 +10:00

162 lines
5.0 KiB
Go

package task
import (
"encoding/json"
"fmt"
"regexp"
"slices"
"strings"
"gopkg.in/yaml.v3"
)
// RepositoryMode says whether a workflow works on a repository.
type RepositoryMode string
const (
// RepositoryRequired gives the agent a worktree of the task's repository.
// It is the default, so a workflow that does not declare one keeps working
// on a repository.
RepositoryRequired RepositoryMode = "required"
// RepositoryOptional runs with a worktree when the binding names a
// repository, and in a scratch workspace when it does not.
RepositoryOptional RepositoryMode = "optional"
// RepositoryNone runs in a scratch workspace with no clone.
RepositoryNone RepositoryMode = "none"
)
// InputSpec declares one workflow input. Type is one of the mapping field
// types, so a mapped parameter and the input it feeds are checked alike.
type InputSpec struct {
Type string `yaml:"type" json:"type"`
Required bool `yaml:"required,omitempty" json:"required,omitempty"`
}
// WorkflowInterface is what a workflow declares to whatever starts it: the
// inputs it takes, whether it works on a repository, and the agent profile it
// runs under. It lives beside the definition entry so a process that does not
// link the engine can check a binding against it.
type WorkflowInterface struct {
Inputs map[string]InputSpec `yaml:"inputs,omitempty" json:"inputs,omitempty"`
Repository RepositoryMode `yaml:"repository,omitempty" json:"repository,omitempty"`
Profile string `yaml:"profile,omitempty" json:"profile,omitempty"`
}
var (
inputName = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`)
inputTypes = []string{"string", "number", "bool", "object", "array", "any"}
)
// ParseWorkflowInterface reads the interface a workflow YAML declares,
// ignoring its steps.
func ParseWorkflowInterface(src string) (WorkflowInterface, error) {
var w WorkflowInterface
if err := yaml.Unmarshal([]byte(src), &w); err != nil {
return WorkflowInterface{}, fmt.Errorf("parse workflow YAML: %w", err)
}
return w, w.Validate()
}
// Validate rejects an unknown repository mode, a malformed input name and an
// unknown input type.
func (w WorkflowInterface) Validate() error {
switch w.Repository {
case "", RepositoryRequired, RepositoryOptional, RepositoryNone:
default:
return fmt.Errorf("workflow repository %q must be none, optional or required", w.Repository)
}
for name, spec := range w.Inputs {
if !inputName.MatchString(name) {
return fmt.Errorf("workflow input %q must be a letter or underscore followed by letters, digits or underscores", name)
}
if !slices.Contains(inputTypes, spec.Type) {
return fmt.Errorf("workflow input %q type %q must be one of %s", name, spec.Type, strings.Join(inputTypes, ", "))
}
}
return nil
}
// RepositoryMode returns the declared mode, defaulting to required.
func (w WorkflowInterface) RepositoryMode() RepositoryMode {
if w.Repository == "" {
return RepositoryRequired
}
return w.Repository
}
// CheckInputs rejects values that do not satisfy the declared inputs: a
// required input that is missing or null, a value for an undeclared input, or
// a value whose type is not the declared one.
func (w WorkflowInterface) CheckInputs(values map[string]any) error {
for name, spec := range w.Inputs {
v, ok := values[name]
if spec.Required && (!ok || v == nil) {
return fmt.Errorf("input %q is required", name)
}
}
for name, v := range values {
spec, ok := w.Inputs[name]
if !ok {
return fmt.Errorf("input %q is not declared by the workflow", name)
}
if got := ValueType(v); v != nil && !TypeAccepts(spec.Type, got) {
return fmt.Errorf("input %q is %s, want %s", name, got, spec.Type)
}
}
return nil
}
// TypeAccepts reports whether a value or parameter of type got may feed an
// input declared as want.
func TypeAccepts(want, got string) bool {
return want == "any" || got == "any" || want == got
}
// ValueType names the JSON type of a decoded value, in the mapping field type
// vocabulary.
func ValueType(v any) string {
switch v.(type) {
case nil:
return "null"
case string:
return "string"
case bool:
return "bool"
case json.Number, float64, float32, int, int32, int64:
return "number"
case map[string]any:
return "object"
case []any:
return "array"
default:
return fmt.Sprintf("%T", v)
}
}
// EncodeInputs is the stored and wire form of a task's inputs: a JSON
// object, or empty for none.
func EncodeInputs(inputs map[string]any) (string, error) {
if len(inputs) == 0 {
return "", nil
}
data, err := json.Marshal(inputs)
if err != nil {
return "", fmt.Errorf("encode task inputs: %w", err)
}
return string(data), nil
}
// DecodeInputs reverses EncodeInputs, keeping numbers exact.
func DecodeInputs(s string) (map[string]any, error) {
if s == "" {
return nil, nil
}
decoder := json.NewDecoder(strings.NewReader(s))
decoder.UseNumber()
var inputs map[string]any
if err := decoder.Decode(&inputs); err != nil {
return nil, fmt.Errorf("decode task inputs: %w", err)
}
return inputs, nil
}