Files
archie/internal/app/controlplane/controlplane_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

272 lines
9.2 KiB
Go

package controlplane
import (
"context"
"encoding/json"
"testing"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/samcharles93/archie-core/internal/config"
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/agent"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps"
"github.com/samcharles93/archie-core/internal/store"
)
// testSteps builds the production workflow step vocabulary for tests that are
// not about the vocabulary itself; step_vocabulary_agreement_test.go is the one
// that registers a provider and drives every production resolution site.
func testSteps(t *testing.T) *workflow.Manager {
t.Helper()
steps, err := workflowsteps.NewManager()
if err != nil {
t.Fatalf("build workflow step vocabulary: %v", err)
}
return steps
}
// testServer builds the State Store's control plane against the production
// vocabulary, so a test that is not about the vocabulary still exercises the
// constructor production uses.
func testServer(t *testing.T, resources ResourceStore) *Server {
t.Helper()
server, err := NewServer(resources, testSteps(t))
if err != nil {
t.Fatalf("build control plane server: %v", err)
}
return server
}
func TestCatalogCarriesShippedWorkflowDefinitionsForRestore(t *testing.T) {
t.Parallel()
resources := store.OpenTest(t)
defer resources.Close()
catalog, err := testServer(t, resources).Catalog(t.Context(), &pb.CatalogRequest{})
if err != nil {
t.Fatal(err)
}
var defaults string
for _, resource := range catalog.Resources {
if resource.Kind == WorkflowDefinitionsKind {
defaults = resource.DefaultsJson
}
}
if defaults == "" {
t.Fatal("workflow-definitions descriptor carries no defaults")
}
collection, err := workflow.DecodeDefinitionCollection([]byte(defaults), workflow.BuiltinStepRegistry())
if err != nil {
t.Fatal(err)
}
if _, ok := collection.DefinitionByID("remediate"); !ok {
t.Fatalf("shipped defaults = %#v", collection.Definitions)
}
}
func TestImportConfigSeedsWorkflowDefinitionsWithoutOverwritingOverride(t *testing.T) {
t.Parallel()
resources := store.OpenTest(t)
defer resources.Close()
server := testServer(t, resources)
if _, err := server.ImportConfig(t.Context(), config.Config{}); err != nil {
t.Fatal(err)
}
resource, err := resources.Resource(t.Context(), WorkflowDefinitionsKind)
if err != nil {
t.Fatal(err)
}
definitions, err := workflow.DecodeDefinitionCollection(resource.Value, workflow.BuiltinStepRegistry())
if err != nil {
t.Fatal(err)
}
if _, ok := definitions.DefinitionByID("remediate"); !ok {
t.Fatal("seeded definitions omit remediate")
}
custom := workflow.WorkflowDefinitionCollection{Definitions: []workflow.WorkflowDefinitionEntry{{ID: "custom", YAML: "id: custom\nsteps:\n - type: bootstrap.apply\n"}}}
value, err := encodeWorkflowDefinitions(custom, testSteps(t).Registry())
if err != nil {
t.Fatal(err)
}
if _, err := resources.PutResource(t.Context(), store.ResourceWrite{Kind: WorkflowDefinitionsKind, Value: value, ExpectedVersion: resource.Version, Actor: "test", Source: "test", RequestID: "override"}); err != nil {
t.Fatal(err)
}
if _, err := server.ImportConfig(t.Context(), config.Config{}); err != nil {
t.Fatal(err)
}
got, err := resources.Resource(t.Context(), WorkflowDefinitionsKind)
if err != nil {
t.Fatal(err)
}
if got.Version != 2 {
t.Fatalf("version after reimport = %d, want 2", got.Version)
}
}
func TestImportConfigSeedsPersonasWithoutOverwritingEdits(t *testing.T) {
t.Parallel()
resources := store.OpenTest(t)
defer resources.Close()
server := testServer(t, resources)
if _, err := server.ImportConfig(t.Context(), config.Config{}); err != nil {
t.Fatal(err)
}
resource, err := resources.Resource(t.Context(), PersonasKind)
if err != nil {
t.Fatal(err)
}
collection, err := decodePersonas(resource.Value)
if err != nil || len(collection.Personas) != len(agent.ShippedPersonas().Personas) {
t.Fatalf("seeded personas = %+v, %v", collection, err)
}
collection.Personas[0].Prompt = "edited"
value, err := json.Marshal(collection)
if err != nil {
t.Fatal(err)
}
if _, err := resources.PutResource(t.Context(), store.ResourceWrite{Kind: PersonasKind, Value: value, ExpectedVersion: resource.Version, Actor: "test", Source: "test", RequestID: "persona-edit"}); err != nil {
t.Fatal(err)
}
if _, err := server.ImportConfig(t.Context(), config.Config{}); err != nil {
t.Fatal(err)
}
got, err := resources.Resource(t.Context(), PersonasKind)
if err != nil {
t.Fatal(err)
}
updated, err := decodePersonas(got.Value)
if err != nil || updated.Personas[0].Prompt != "edited" || got.Version != 2 {
t.Fatalf("personas after reimport = %+v (version %d), %v", updated, got.Version, err)
}
}
func TestImportWorkflowExecutionSettingsDoesNotOverwriteExistingValue(t *testing.T) {
t.Parallel()
resources := store.OpenTest(t)
defer resources.Close()
server := testServer(t, resources)
first := workflow.ExecutionSettings{MaxModelToolSteps: 10, MaxRuntime: time.Minute, MaxConsecutiveGateFailures: 2}
if _, err := server.ImportWorkflowExecutionSettings(t.Context(), first); err != nil {
t.Fatal(err)
}
if _, err := server.ImportWorkflowExecutionSettings(t.Context(), workflow.ExecutionSettings{MaxModelToolSteps: 99, MaxRuntime: time.Hour, MaxConsecutiveGateFailures: 9}); err != nil {
t.Fatal(err)
}
resource, err := resources.Resource(t.Context(), WorkflowExecutionSettingsKind)
if err != nil {
t.Fatal(err)
}
got, err := decodeSettings(resource.Value)
if err != nil {
t.Fatal(err)
}
if got != first || resource.Version != 1 {
t.Fatalf("imported settings = (%+v, version %d), want (%+v, version 1)", got, resource.Version, first)
}
}
func TestRegistryRoutesAndValidatesDefinitions(t *testing.T) {
t.Parallel()
resources := store.OpenTest(t)
defer resources.Close()
server := testServer(t, resources)
if _, err := server.ImportConfig(t.Context(), config.Config{Containers: config.ContainerConfig{PullPolicy: "missing"}}); err != nil {
t.Fatal(err)
}
catalog, err := server.Catalog(t.Context(), &pb.CatalogRequest{})
if err != nil {
t.Fatal(err)
}
found := false
for _, descriptor := range catalog.Resources {
if descriptor.Kind == ContainerRuntimePoliciesKind {
found = descriptor.ApplyMode == "restart-required"
}
}
if !found {
t.Fatal("container runtime policies missing restart-required descriptor")
}
_, err = server.Command(t.Context(), &pb.CommandRequest{Kind: ContainerRuntimePoliciesKind, Command: "replace", ValueJson: []byte(`{"pull_policy":"sometimes"}`), Actor: "test", Source: "test", RequestId: "bad", ExpectedVersion: 1})
if err == nil {
t.Fatal("invalid pull policy accepted")
}
}
func TestProviderSeedKeepsReferencesAndNeverResolvedSecrets(t *testing.T) {
t.Parallel()
resources := store.OpenTest(t)
defer resources.Close()
server := testServer(t, resources)
cfg := config.Config{Providers: map[string]config.Provider{"openai": {Class: "openai", APIKeyEnv: "OPENAI_API_KEY", APIKey: config.SecretRef{Engine: "env", Key: "OPENAI_API_KEY"}}}}
if _, err := server.ImportConfig(context.Background(), cfg); err != nil {
t.Fatal(err)
}
resource, err := resources.Resource(t.Context(), ProviderSettingsKind)
if err != nil {
t.Fatal(err)
}
var value map[string]any
if err := json.Unmarshal(resource.Value, &value); err != nil {
t.Fatal(err)
}
encoded := string(resource.Value)
if encoded == "" || !json.Valid(resource.Value) {
t.Fatalf("provider resource = %q", encoded)
}
if _, ok := value["openai"]; !ok {
t.Fatalf("provider resource = %s", encoded)
}
}
func TestHistoryCarriesEveryRevisionWithItsAudit(t *testing.T) {
t.Parallel()
resources := store.OpenTest(t)
defer resources.Close()
server := testServer(t, resources)
if _, err := server.ImportConfig(t.Context(), config.Config{}); err != nil {
t.Fatal(err)
}
seeded, err := server.Query(t.Context(), &pb.QueryRequest{Kind: WorkflowExecutionSettingsKind})
if err != nil {
t.Fatal(err)
}
if _, err := server.Command(t.Context(), &pb.CommandRequest{
Kind: WorkflowExecutionSettingsKind, Command: "replace",
ValueJson: []byte(`{"max_model_tool_steps":40,"max_runtime_seconds":600,"max_consecutive_gate_failures":2}`),
ExpectedVersion: seeded.Resource.Version, Actor: "operator", Source: "archie-ui", RequestId: "edit-1",
}); err != nil {
t.Fatal(err)
}
history, err := server.History(t.Context(), &pb.HistoryRequest{Kind: WorkflowExecutionSettingsKind})
if err != nil {
t.Fatal(err)
}
if len(history.Revisions) != 2 {
t.Fatalf("revisions = %d, want the seed and the edit", len(history.Revisions))
}
newest := history.Revisions[0]
if newest.Actor != "operator" || newest.Source != "archie-ui" || newest.RequestId != "edit-1" {
t.Fatalf("newest revision audit = %+v, want the dashboard edit", newest)
}
// The value rides along so a restore is an ordinary replace of it.
if len(history.Revisions[1].ValueJson) == 0 {
t.Fatal("older revision carries no value; nothing to restore from")
}
if _, err := server.History(t.Context(), &pb.HistoryRequest{Kind: "not-a-resource"}); status.Code(err) != codes.NotFound {
t.Fatalf("history for an unknown kind = %v, want NotFound", err)
}
}