Files
archie/internal/infrastructure/workflowsteps/workflowsteps_test.go
T
sam 9fb65d0519 fix(workflow): pin the roots, drop the dead vocabulary, guard the nil manager
Third review pass on the step-vocabulary shape, one fix per finding.

P1, roots unpinned. The agreement test built its own manager and injected it, so
it pinned the resolution sites and not the roots that decide the provider set; a
root regressed to workflow.NewManager() (no shipped stages) left go test green.
archied now resolves through one unit, stepVocabulary (step_vocabulary.go), and
agentworker's productionWorkerDependencies is exercised directly:
internal/app/agentworker/step_vocabulary_test.go and
internal/app/archied/step_vocabulary_test.go assert each root's manager resolves
the shared provider set's shipped stages and accepts a provider registered
through it. RunStateStore and openStateStoreAdapter own a SQLite file and a gRPC
dial, so their calls are pinned by source position, the way
composition_order_test.go pins the memory-engine ordering.

P2-B, dead vocabulary. archie-messaging registered a step vocabulary it never
resolved (its only control-plane call is RuntimeChatConfig) and could fail
startup on it before the State Store target guard. The workflow-definitions
surface is now its own type, controlplane.WorkflowDefinitionsClient, so the
vocabulary is a constructor dependency of the one surface that resolves step
types and of nothing else. The messaging root no longer imports workflowsteps.

P2-C, nil manager. A nil *workflow.Manager dereferenced on every resolution path.
stepRegistry and CompilePinnedWorkflow now refuse with an error naming the wiring
mistake; TestNilStepVocabularyFailsClosedAtEveryResolutionSite pins the refusal.

Claim-vs-code corrections: agreement is stated as within one build (vocabulary.go,
stepRegistry, openStateStoreControlPlane, openStateStoreAdapter); the Yaegi claim
now says no bridge to StepFactory has been built and cites
internal/secret/secretextract; the provider set names its three roots;
Manager.Register's ordering is documented as a constraint with its consequence,
and the shape rule no longer claims to reserve shipped stage names; the
shipped-stage test compares against the builtin factory's own stage.
2026-09-21 20:00:37 +10:00

116 lines
4.7 KiB
Go

package workflowsteps
import (
"slices"
"strings"
"testing"
"gopkg.in/yaml.v3"
"github.com/samcharles93/archie-core/internal/domain/workflow"
)
// TestShippedVocabularyCoversEveryShippedStage is the guard that the bundled
// provider set really carries the shipped stages: a process that registers it
// can compile every shipped workflow, and one that does not has no vocabulary
// at all.
func TestShippedVocabularyCoversEveryShippedStage(t *testing.T) {
t.Parallel()
manager, err := NewManager()
if err != nil {
t.Fatalf("NewManager: %v", err)
}
want := make([]string, 0, len(workflow.BuiltinStepRegistry()))
for name := range workflow.BuiltinStepRegistry() {
want = append(want, name)
}
slices.Sort(want)
if got := manager.StepTypes(); !slices.Equal(got, want) {
t.Fatalf("registered vocabulary = %v, want the shipped stages %v", got, want)
}
// The vocabulary arrives by registration, not by implication: a manager
// that was handed no provider set resolves nothing, which is what makes
// every resolution site's use of the injected manager observable.
if got := workflow.NewManager().StepTypes(); len(got) != 0 {
t.Fatalf("an unregistered manager resolves %v, want nothing", got)
}
}
// TestShippedStagesKeepTheirBehaviourThroughTheManager pins that the provider
// hands the builtin factories through unchanged. The shipped stages are typed,
// non-interpreted steps: each compiles to the stage the builtin registry's own
// factory builds for that step type -- the same name, and Run present or absent
// the same way -- and refuses settings. The one way this enumeration could keep
// every name and lose that guard is to rebuild the factories instead of
// forwarding them, and definition_test.go's assertion cannot see it -- that test
// drives BuiltinStepRegistry, which production no longer calls.
func TestShippedStagesKeepTheirBehaviourThroughTheManager(t *testing.T) {
t.Parallel()
manager, err := NewManager()
if err != nil {
t.Fatalf("NewManager: %v", err)
}
registry := manager.Registry()
for _, entry := range workflow.ShippedDefinitions().Definitions {
definition, err := workflow.ParseDefinition(entry.YAML, registry)
if err != nil {
t.Fatalf("parse shipped workflow %q through the registered vocabulary: %v", entry.ID, err)
}
compiled, err := workflow.ParseAndCompile(entry.YAML, registry)
if err != nil {
t.Fatalf("compile shipped workflow %q through the registered vocabulary: %v", entry.ID, err)
}
if len(compiled.Stages) != len(definition.Steps) {
t.Fatalf("%s compiled %d stages, want %d", entry.ID, len(compiled.Stages), len(definition.Steps))
}
for i, step := range definition.Steps {
// The expectation is the stage the builtin registry's own factory
// builds for this step type, not a name derived from the step type:
// that is what "the registered factory is the builtin one" means
// for a Stage, which is a name plus an uncomparable func.
shipped, err := workflow.BuiltinStepRegistry()[step.Type](yaml.Node{})
if err != nil {
t.Fatalf("build shipped stage %s: %v", step.Type, err)
}
if compiled.Stages[i].Name != shipped.Name {
t.Errorf("%s step %d (%s) compiled to stage %q, want the shipped stage %q: the registered factory is not the builtin one", entry.ID, i, step.Type, compiled.Stages[i].Name, shipped.Name)
}
if (compiled.Stages[i].Run != nil) != (shipped.Run != nil) {
t.Errorf("%s step %d (%s) compiled to a stage whose Run is present=%t, want the shipped stage's present=%t", entry.ID, i, step.Type, compiled.Stages[i].Run != nil, shipped.Run != nil)
}
}
}
if _, err := workflow.ParseAndCompile("id: probe\nsteps:\n - type: bootstrap.apply\n settings:\n command: rm -rf /\n", registry); err == nil || !strings.Contains(err.Error(), "accepts no settings") {
t.Fatalf("ParseAndCompile() error = %v, want the shipped stage's settings refusal", err)
}
}
// TestNewManagerIsRepeatableWithinOneProcess is the regression guard for
// registering the provider set into shared state: every construction is
// independent, so building the vocabulary twice in one process — as `-count=2`
// does — must succeed and produce the same vocabulary.
func TestNewManagerIsRepeatableWithinOneProcess(t *testing.T) {
t.Parallel()
first, err := NewManager()
if err != nil {
t.Fatalf("first NewManager: %v", err)
}
second, err := NewManager()
if err != nil {
t.Fatalf("second NewManager: %v", err)
}
if !slices.Equal(first.StepTypes(), second.StepTypes()) {
t.Fatalf("two managers resolved different vocabularies: %v and %v", first.StepTypes(), second.StepTypes())
}
if !slices.Equal(first.Providers(), second.Providers()) {
t.Fatalf("two managers registered different providers: %v and %v", first.Providers(), second.Providers())
}
}