Files
archie/internal/domain/workflow/task/interface_test.go
T
sam abb9f8a04e feat(harness): declare and enforce a workflow's harness needs at dispatch
docs/prds/external-agent-harness.md ("Contract"): workflow definition
validation must reject a stage whose needs the selected harness cannot
meet -- capture tools without an adapter, or a gate retry without a
resume verb. The step vocabulary had no static way to declare either.

Added task.WorkflowInterface.Needs{Captures, GateRetries}, parsed and
validated (GateRetries >= 0) the same way Inputs/Repository already are.
Only a Kit profile is checked against it: an image profile's built-in
agent loop always supports both, so Needs constrains only the harness path.

Two enforcement points, chosen for where each fact is actually known
without a network fetch at save time:
- Captures vs adapter is knowable from config alone (profile.Adapter, the
  agentexec.harnessAdapters table), so daemon.pinTaskProfile checks it
  right where it already resolves the profile (validateProfileMeetsNeeds),
  parking the task the same way an unconfigured profile already does.
- GateRetries vs the Kit's actual agent-sessions resume verb is only known
  once the Kit composition is fetched, which kitrun.Launcher.Launch
  already does -- so kit.ValidateNeeds runs there (new kitrun.Request.
  GateRetries field, threaded from the workflow's parsed Needs in
  daemon.runKitTask) rather than inventing an image-fetching workflow-save
  validator.

Deliberately NOT built: "a Kit profile named by no workflow is a
validation error" (external-agent-harness.md "Selection"). Read literally
("a Kit profile [that is] named by no workflow"), this is a
cross-resource check -- an agent-profiles entry unreferenced by anything
in workflow-definitions -- and no control-plane Definition validator can
see another resource today (Definition.Validate is a pure []byte->error
function by design, matching every existing resource kind). Building
that crossing is a real, separate piece of infrastructure with its own
ordering questions (does defining a Kit profile before its workflow now
fail?), not a two-line addition, so it is left for a follow-on rather
than bolted on here.

Also fixes a test bug caught by rerunning with a short -timeout per
instruction: TestRunKitTaskThreadsGateRetriesToTheLauncher's fake NATS
subscriber never responded, so runKitTask's call into runViaAgent blocked
forever waiting for a reply bounded by a zero (unconfigured) wall-clock
budget. Fixed by responding, matching the sibling test's pattern.
2026-09-27 05:25:39 +10:00

89 lines
3.2 KiB
Go

package task
import (
"encoding/json"
"strings"
"testing"
)
func TestParseWorkflowInterface(t *testing.T) {
tests := []struct {
name string
src string
wantErr string
wantMode RepositoryMode
}{
{name: "undeclared repository defaults to required", src: "id: a\nsteps: []\n", wantMode: RepositoryRequired},
{name: "none", src: "id: a\nrepository: none\n", wantMode: RepositoryNone},
{name: "unknown mode", src: "id: a\nrepository: sometimes\n", wantErr: "must be none, optional or required"},
{name: "unknown input type", src: "id: a\ninputs:\n ip: {type: ipv4}\n", wantErr: `type "ipv4"`},
{name: "malformed input name", src: "id: a\ninputs:\n src-ip: {type: string}\n", wantErr: `input "src-ip"`},
{name: "negative gate_retries", src: "id: a\nneeds: {gate_retries: -1}\n", wantErr: "gate_retries must not be negative"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w, err := ParseWorkflowInterface(tt.src)
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("err = %v, want containing %q", err, tt.wantErr)
}
return
}
if err != nil {
t.Fatal(err)
}
if got := w.RepositoryMode(); got != tt.wantMode {
t.Errorf("RepositoryMode() = %q, want %q", got, tt.wantMode)
}
})
}
}
// TestParseWorkflowInterfaceNeeds pins the declared-needs vocabulary
// (docs/prds/external-agent-harness.md, "Contract"): a stage's requirements
// of its harness are static, readable metadata, not derived at run time.
func TestParseWorkflowInterfaceNeeds(t *testing.T) {
w, err := ParseWorkflowInterface("id: a\nprofile: contain\nneeds: {captures: true, gate_retries: 3}\n")
if err != nil {
t.Fatal(err)
}
if !w.Needs.Captures || w.Needs.GateRetries != 3 {
t.Fatalf("Needs = %+v, want {Captures:true GateRetries:3}", w.Needs)
}
}
func TestCheckInputs(t *testing.T) {
w := WorkflowInterface{Inputs: map[string]InputSpec{
"src_ip": {Type: "string", Required: true},
"severity": {Type: "number"},
"extra": {Type: "any"},
}}
tests := []struct {
name string
values map[string]any
wantErr string
}{
{name: "required and typed", values: map[string]any{"src_ip": "10.0.0.1", "severity": json.Number("3")}},
{name: "optional null is absent", values: map[string]any{"src_ip": "10.0.0.1", "severity": nil}},
{name: "any takes an object", values: map[string]any{"src_ip": "x", "extra": map[string]any{"a": 1.0}}},
{name: "missing required", values: map[string]any{"severity": 1.0}, wantErr: `"src_ip" is required`},
{name: "null required", values: map[string]any{"src_ip": nil}, wantErr: `"src_ip" is required`},
{name: "undeclared", values: map[string]any{"src_ip": "x", "port": 22.0}, wantErr: `"port" is not declared`},
{name: "wrong type", values: map[string]any{"src_ip": "x", "severity": "high"}, wantErr: `"severity" is string, want number`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := w.CheckInputs(tt.values)
if tt.wantErr == "" {
if err != nil {
t.Fatalf("CheckInputs() = %v", err)
}
return
}
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("CheckInputs() = %v, want containing %q", err, tt.wantErr)
}
})
}
}