The Web UI and messaging channels now read and write Archie's runtime settings through one versioned, audited control-plane API hosted on the State Store's existing gRPC server: catalog, query, command, watch. Each feature owns its resource, validation and commands; storage only writes versions and audit records. Workflows move with it. Definitions become portable YAML compiled through a closed, typed step registry and stored as a control-plane resource, so the yaegi interpretation paths (internal/gate, wfeval, wfextract, gateeval, gateextract, skillbuild) are deleted rather than carried. The UI process reads definitions as data: the JSON projection lives in internal/domain/workflow/task so a dashboard decodes the stored collection without linking the workflow engine, which cmd/archie-ui's deletion gate forbids. Shipped definitions ride the catalog descriptor rather than a bespoke REST route, keeping the API to its four operations. archie-messaging gains -state-store-target/-state-store-token and reads channel settings from the database, failing closed when the State Store is unreachable: the database is authoritative for those settings.
203 lines
6.6 KiB
Go
203 lines
6.6 KiB
Go
package controlplane
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"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/store"
|
|
)
|
|
|
|
func TestCatalogCarriesShippedWorkflowDefinitionsForRestore(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
resources := store.OpenTest(t)
|
|
defer resources.Close()
|
|
catalog, err := NewServer(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 := NewServer(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)
|
|
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 := NewServer(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 := NewServer(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 := NewServer(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 := NewServer(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)
|
|
}
|
|
}
|