Files
archie/internal/app/controlplane/runtime_config_test.go
T
sam de98260765 fix(controlplane): make the scheduling policy carry the label its trigger pairs with
The scheduling-policy resource carried the dispatch trigger but not the
trigger-matching label, so validateScheduling could judge only half of
the pairing the file layer's validateDispatch enforces on the effective
document (a label-requiring trigger with no label matches every open
issue -- the GH#445 guard). The write path therefore stored trigger
label/either with no label whenever the file's own label was unset, and
boot then refused the layered document: a value the store blessed,
archied unstartable, the operator left with the offline rollback as the
only remedy (archie-core-7pyj).

The resource now carries the label: the seed copies the file document's
label, RuntimeConfig layers it with the trigger (a policy stored before
the field existed decodes nil and leaves the file's label in force, the
same absence-means-inherited shape a kind with no stored value has),
and validateScheduling refuses a label-requiring trigger with no label,
the pairing judged on the data the store actually holds. Once the store
carries a label it owns it, so a file that later loses the label can no
longer break a stored policy that depends on one. The parity test
gains both directions, and a legacy policy without the field is pinned
as an absence, not an erasure.
2026-09-22 00:32:30 +10:00

148 lines
7.2 KiB
Go

package controlplane
import (
"context"
"encoding/json"
"testing"
"google.golang.org/grpc"
"github.com/samcharles93/archie-core/internal/config"
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
)
type runtimeConfigClient struct{ values map[string]any }
func (*runtimeConfigClient) Catalog(context.Context, *pb.CatalogRequest, ...grpc.CallOption) (*pb.CatalogResponse, error) {
return &pb.CatalogResponse{}, nil
}
func (c *runtimeConfigClient) Query(_ context.Context, request *pb.QueryRequest, _ ...grpc.CallOption) (*pb.QueryResponse, error) {
value, err := json.Marshal(c.values[request.Kind])
return &pb.QueryResponse{Resource: &pb.Resource{Kind: request.Kind, Version: 2, ValueJson: value}}, err
}
func (*runtimeConfigClient) History(context.Context, *pb.HistoryRequest, ...grpc.CallOption) (*pb.HistoryResponse, error) {
panic("unexpected History")
}
func (*runtimeConfigClient) Command(context.Context, *pb.CommandRequest, ...grpc.CallOption) (*pb.CommandResponse, error) {
panic("unexpected Command")
}
func (*runtimeConfigClient) Watch(context.Context, *pb.WatchRequest, ...grpc.CallOption) (grpc.ServerStreamingClient[pb.WatchResponse], error) {
panic("unexpected Watch")
}
func TestRuntimeConfigUsesDatabaseResourcesAndPreservesBootstrapOnlySecrets(t *testing.T) {
base := config.Config{
Providers: map[string]config.Provider{"old": {Class: "openai"}},
Models: map[string]string{"builder": "old/model"},
Repos: []config.Repo{{Owner: "old", Name: "repo"}},
Chat: config.ChatConfig{Telegram: config.TelegramConfig{
UpdateCheckCommand: []string{"check"}, UpdateInstallCommand: []string{"install"},
}},
Tools: config.ToolsConfig{MCPServers: []config.MCPServer{{Name: "docs", Headers: map[string]string{"Authorization": "secret"}}}},
}
client := NewRPCClient(&runtimeConfigClient{values: map[string]any{
ProviderSettingsKind: map[string]any{"main": map[string]any{"class": "openai", "base_url": "https://api.example.com", "api_key_ref": map[string]any{"engine": "env", "key": "API_KEY"}, "has_credential": true}},
ModelRoleAssignmentsKind: map[string]string{"builder": "main/model"},
RepositoryPoliciesKind: []config.Repo{{Owner: "acme", Name: "widget"}},
ChannelSettingsKind: map[string]any{"operator": "Sam", "show_tool_calls": true, "max_steps": 20, "models": []string{"main/model"}, "email": map[string]any{}, "webhook_addr": "", "webhook": map[string]any{}, "telegram": map[string]any{"allowed_user_ids": []int64{42}, "credential_configured": false}, "rate_limit": map[string]any{}, "unrestricted_filesystem": false, "workspace": "/workspace"},
SchedulingPolicyKind: map[string]any{"poll_interval": "2m", "max_retries": 7, "dispatch": map[string]any{"trigger": "assignee"}},
ToolSettingsKind: map[string]any{"mcp_servers": []map[string]any{{"name": "docs", "transport": "http", "url": "https://mcp.example.com", "headers_configured": true}}, "policy": map[string]any{}, "web_fetch": map[string]any{}, "minimax": map[string]any{"enabled": false, "credential_configured": false}},
PluginSettingsKind: map[string]any{"plugin_dir": "/plugins", "module_dir": "/modules", "secret_engine_dir": "/secrets", "skills_dir": "/skills"},
ContainerRuntimePoliciesKind: config.ContainerConfig{LegacyEnabled: true, Image: "archie:next", PullPolicy: "missing"},
}})
got, versions, err := client.RuntimeConfig(t.Context(), base)
if err != nil {
t.Fatalf("RuntimeConfig: %v", err)
}
// Every kind it read is reported as applied, so the version a process
// publishes is the one it actually layered in (archie-core-pskb).
for _, kind := range []string{
ProviderSettingsKind, ModelRoleAssignmentsKind, RepositoryPoliciesKind, ChannelSettingsKind,
SchedulingPolicyKind, ToolSettingsKind, PluginSettingsKind, ContainerRuntimePoliciesKind,
} {
if versions[kind] != 2 {
t.Errorf("versions[%s] = %d, want the 2 the store answered with", kind, versions[kind])
}
}
if got.Models["builder"] != "main/model" || got.Repos[0].FullName() != "acme/widget" || got.MaxRetries != 7 || got.PluginDir != "/plugins" || got.Containers.Image != "archie:next" {
t.Fatalf("database settings not applied: %+v", got)
}
if got.Chat.Operator != "Sam" || got.Chat.Telegram.AllowedUserIDs[0] != 42 {
t.Fatalf("channel settings not applied: %+v", got.Chat)
}
if got.Chat.Telegram.UpdateCheckCommand[0] != "check" || got.Tools.MCPServers[0].Headers["Authorization"] != "secret" {
t.Fatalf("bootstrap-only secret-backed settings were dropped: chat=%+v tools=%+v", got.Chat, got.Tools)
}
}
// TestRuntimeConfigLayersTheStoredSchedulingLabel: the label pairs with the
// dispatch trigger and layers with it. A policy that carries one replaces the
// file document's label, so the store owns the half of the pairing it judged
// at write time (archie-core-7pyj).
func TestRuntimeConfigLayersTheStoredSchedulingLabel(t *testing.T) {
base := config.Config{
Label: "file-label",
PollInterval: config.Duration(60 * 1e9),
Dispatch: config.Dispatch{Trigger: "assignee"},
}
client := NewRPCClient(&runtimeConfigClient{values: map[string]any{
SchedulingPolicyKind: map[string]any{"poll_interval": "2m", "max_retries": 7, "label": "stored-label", "dispatch": map[string]any{"trigger": "assignee"}},
}})
got, _, err := client.RuntimeConfig(t.Context(), base)
if err != nil {
t.Fatalf("RuntimeConfig: %v", err)
}
if got.Label != "stored-label" {
t.Fatalf("label after layering = %q, want the stored policy's label", got.Label)
}
}
// TestRuntimeConfigLeavesTheFileLabelInForceWhenThePolicyCarriesNone: a
// policy stored before the label field existed (or one that omits it) is not
// an instruction to clear the file document's label -- the same
// absence-means-inherited shape a kind with no stored value has.
func TestRuntimeConfigLeavesTheFileLabelInForceWhenThePolicyCarriesNone(t *testing.T) {
base := config.Config{
Label: "file-label",
PollInterval: config.Duration(60 * 1e9),
Dispatch: config.Dispatch{Trigger: "assignee"},
}
client := NewRPCClient(&runtimeConfigClient{values: map[string]any{
SchedulingPolicyKind: map[string]any{"poll_interval": "2m", "max_retries": 7, "dispatch": map[string]any{"trigger": "assignee"}},
}})
got, _, err := client.RuntimeConfig(t.Context(), base)
if err != nil {
t.Fatalf("RuntimeConfig: %v", err)
}
if got.Label != "file-label" {
t.Fatalf("label after layering = %q, want the file document's label left in force", got.Label)
}
}
// TestSchedulingPolicySeedCarriesTheLabel: the seed a fresh store writes must
// carry the file document's label, so the pairing a stored trigger requires
// is judgeable at write time from the moment the resource exists.
func TestSchedulingPolicySeedCarriesTheLabel(t *testing.T) {
server := testServer(t, nil)
seed, err := server.definitions[SchedulingPolicyKind].seededValue(config.Config{
Label: "archie:labelled",
PollInterval: config.Duration(60 * 1e9),
Dispatch: config.Dispatch{Trigger: "label"},
})
if err != nil {
t.Fatalf("seededValue: %v", err)
}
var document map[string]any
if err := json.Unmarshal(seed, &document); err != nil {
t.Fatalf("decode seed: %v", err)
}
if label, _ := document["label"].(string); label != "archie:labelled" {
t.Fatalf("seed label = %q, want the file document's label", label)
}
}