fix(controlplane): give the channel document its own shape
The channel-settings resource embedded internal/config types directly, so the document the Web UI edits was a JSON projection of TOML-tagged structs: encoding/json fell back to Go field names, and rate_limit.window was a bare time.Duration. An operator saw "Window" over a field expecting a nanosecond count -- 60000000000 for the window = "1m" the file example writes -- and "ListenAddr" over the email addresses, while every sibling resource in the same registry is snake_case. Define the shape here instead: emailSettings and rateLimitSettings with explicit tags, and channelDuration, which writes the string the file accepts and reads that, a nanosecond count, and the Go-cased keys an existing store holds. Reading a legacy document is deliberate, so it stays strict about everything else. decodeRenamingLegacyKeys renames the four known legacy keys and keeps DisallowUnknownFields, because swallowing an unknown key decodes as "unset", and an unset rate limit is off -- the failure this shape exists to end. A misspelling is refused. normalizeChannels, through the Definition.Normalize hook schedules already uses, rewrites a legacy document into the current shape on write, so a stored document converges the next time it is saved. Reads tolerate it until then, so no deployment has to re-save before the document can be read. Verified: the controlplane, messaging and configuration suites, archied (the loadRuntimeConfig consumer), go vet, go build ./..., golangci-lint run ./..., and docs:check. The full task check is the merge train's, on this tip. The same audit found container-runtime-policies embedding config.ContainerConfig (seven fields, no json tags); tracked separately rather than folded in here.
This commit is contained in:
4 files changed
+327
-10
No files matched your search
@@ -1,21 +1,31 @@
|
||||
package controlplane
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
)
|
||||
|
||||
// channelSettings is the Channel settings resource document. Its shape IS the
|
||||
// contract the Web UI edits, so every nested object here is defined by this
|
||||
// file with explicit snake_case json tags and never handed an internal config
|
||||
// struct. Encoding/json falls back to Go field names when a tag is absent,
|
||||
// which is how "Window", "MaxRequests", "ListenAddr" and "RelayAddr" reached
|
||||
// operators in the first place -- a shape belonging to TOML, not to this
|
||||
// document.
|
||||
type channelSettings struct {
|
||||
Operator string `json:"operator"`
|
||||
ShowToolCalls bool `json:"show_tool_calls"`
|
||||
MaxSteps int `json:"max_steps"`
|
||||
Models []string `json:"models"`
|
||||
Email config.EmailConfig `json:"email"`
|
||||
Email emailSettings `json:"email"`
|
||||
WebhookAddr string `json:"webhook_addr"`
|
||||
Webhook webhookChannelSettings `json:"webhook"`
|
||||
Telegram telegramSettings `json:"telegram"`
|
||||
RateLimit config.RateLimitConfig `json:"rate_limit"`
|
||||
RateLimit rateLimitSettings `json:"rate_limit"`
|
||||
UnrestrictedFilesystem bool `json:"unrestricted_filesystem"`
|
||||
Workspace string `json:"workspace"`
|
||||
}
|
||||
@@ -35,12 +45,117 @@ type webhookChannelSettings struct {
|
||||
DeliverTo string `json:"deliver_to"`
|
||||
}
|
||||
|
||||
// emailSettings is the email object. It reads the Go-cased keys an earlier
|
||||
// revision stored as well as its own, because json tags alone would not: the
|
||||
// decoder's case-insensitive fallback compares whole keys, so "ListenAddr"
|
||||
// never matches "listen_addr".
|
||||
type emailSettings struct {
|
||||
ListenAddr string `json:"listen_addr"`
|
||||
RelayAddr string `json:"relay_addr"`
|
||||
}
|
||||
|
||||
func (e *emailSettings) UnmarshalJSON(data []byte) error {
|
||||
type document emailSettings
|
||||
var decoded document
|
||||
err := decodeRenamingLegacyKeys(data, map[string]string{
|
||||
"ListenAddr": "listen_addr",
|
||||
"RelayAddr": "relay_addr",
|
||||
}, &decoded)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*e = emailSettings(decoded)
|
||||
return nil
|
||||
}
|
||||
|
||||
// rateLimitSettings is the inbound rate-limit object, and it reads the Go-cased
|
||||
// keys for the reason emailSettings does.
|
||||
type rateLimitSettings struct {
|
||||
Window channelDuration `json:"window"`
|
||||
MaxRequests int `json:"max_requests"`
|
||||
}
|
||||
|
||||
func (r *rateLimitSettings) UnmarshalJSON(data []byte) error {
|
||||
type document rateLimitSettings
|
||||
var decoded document
|
||||
err := decodeRenamingLegacyKeys(data, map[string]string{
|
||||
"Window": "window",
|
||||
"MaxRequests": "max_requests",
|
||||
}, &decoded)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*r = rateLimitSettings(decoded)
|
||||
return nil
|
||||
}
|
||||
|
||||
// decodeRenamingLegacyKeys decodes an object into v after renaming each key in
|
||||
// legacy to the spelling v documents, and stays strict about everything else.
|
||||
// The strictness is the point: dropping unknown keys would let a typo
|
||||
// ("windows") read as "unset" and silently disable the setting, which is the
|
||||
// class of failure this shape exists to end.
|
||||
func decodeRenamingLegacyKeys(data []byte, legacy map[string]string, v any) error {
|
||||
var raw map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &raw); err != nil {
|
||||
return err
|
||||
}
|
||||
canonical := make(map[string]json.RawMessage, len(raw))
|
||||
for key, value := range raw {
|
||||
if renamed, ok := legacy[key]; ok {
|
||||
key = renamed
|
||||
}
|
||||
if _, duplicate := canonical[key]; duplicate {
|
||||
return fmt.Errorf("duplicate key %q", key)
|
||||
}
|
||||
canonical[key] = value
|
||||
}
|
||||
renamed, err := json.Marshal(canonical)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(renamed))
|
||||
decoder.DisallowUnknownFields()
|
||||
return decoder.Decode(v)
|
||||
}
|
||||
|
||||
// channelDuration is a duration as this document writes it: the human string
|
||||
// the file accepts ("1m0s", the same spelling config.Duration emits), never a
|
||||
// nanosecond count. It still reads the count, because every document written
|
||||
// before this shape existed carries one, and reading both forms is what lets
|
||||
// those documents keep working without being re-saved.
|
||||
type channelDuration time.Duration
|
||||
|
||||
func (d channelDuration) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(time.Duration(d).String())
|
||||
}
|
||||
|
||||
func (d *channelDuration) UnmarshalJSON(data []byte) error {
|
||||
var text string
|
||||
if err := json.Unmarshal(data, &text); err == nil {
|
||||
value, err := time.ParseDuration(text)
|
||||
if err != nil {
|
||||
return fmt.Errorf("duration must be a Go duration string such as %q: %w", "1m0s", err)
|
||||
}
|
||||
*d = channelDuration(value)
|
||||
return nil
|
||||
}
|
||||
var nanos int64
|
||||
if err := json.Unmarshal(data, &nanos); err != nil {
|
||||
return fmt.Errorf("duration must be a Go duration string such as %q or a nanosecond count: %w", "1m0s", err)
|
||||
}
|
||||
*d = channelDuration(nanos)
|
||||
return nil
|
||||
}
|
||||
|
||||
func seedChannels(cfg config.Config) any {
|
||||
chat := cfg.Chat
|
||||
return channelSettings{
|
||||
Operator: chat.Operator, Workspace: chat.Workspace, UnrestrictedFilesystem: chat.UnrestrictedFilesystem, ShowToolCalls: chat.ShowToolCalls, MaxSteps: chat.MaxSteps, Models: chat.Models, Email: chat.Email, WebhookAddr: chat.WebhookAddr,
|
||||
Telegram: telegramSettings{AllowedUserIDs: chat.Telegram.AllowedUserIDs, Token: chat.Telegram.Token, TokenEnv: chat.Telegram.TokenEnv, CredentialConfigured: chat.Telegram.TokenEnv != "" || chat.Telegram.Token != (config.SecretRef{})},
|
||||
Webhook: webhookChannelSettings{Path: chat.Webhook.Path, Secret: chat.Webhook.Secret, CredentialConfigured: chat.Webhook.Secret != (config.SecretRef{}), Template: chat.Webhook.Template, DeliverTo: chat.Webhook.DeliverTo}, RateLimit: chat.RateLimit,
|
||||
Operator: chat.Operator, Workspace: chat.Workspace, UnrestrictedFilesystem: chat.UnrestrictedFilesystem, ShowToolCalls: chat.ShowToolCalls, MaxSteps: chat.MaxSteps, Models: chat.Models,
|
||||
Email: emailSettings{ListenAddr: chat.Email.ListenAddr, RelayAddr: chat.Email.RelayAddr},
|
||||
WebhookAddr: chat.WebhookAddr,
|
||||
Telegram: telegramSettings{AllowedUserIDs: chat.Telegram.AllowedUserIDs, Token: chat.Telegram.Token, TokenEnv: chat.Telegram.TokenEnv, CredentialConfigured: chat.Telegram.TokenEnv != "" || chat.Telegram.Token != (config.SecretRef{})},
|
||||
Webhook: webhookChannelSettings{Path: chat.Webhook.Path, Secret: chat.Webhook.Secret, CredentialConfigured: chat.Webhook.Secret != (config.SecretRef{}), Template: chat.Webhook.Template, DeliverTo: chat.Webhook.DeliverTo},
|
||||
RateLimit: rateLimitSettings{Window: channelDuration(chat.RateLimit.Window), MaxRequests: chat.RateLimit.MaxRequests},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,3 +170,17 @@ func validateChannels(input []byte) error {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// normalizeChannels canonicalizes the stored document: decoding into the shape
|
||||
// above and re-encoding writes the snake_case keys and the string duration,
|
||||
// dropping the Go-cased keys an earlier revision stored. A document that still
|
||||
// carries them converges the next time it is written; reads tolerate them
|
||||
// meanwhile (emailSettings, rateLimitSettings), so nothing has to be re-saved
|
||||
// before it can be read again.
|
||||
func normalizeChannels(input []byte) ([]byte, error) {
|
||||
var settings channelSettings
|
||||
if err := json.Unmarshal(input, &settings); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.Marshal(settings)
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
package controlplane
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
)
|
||||
|
||||
// channelSettingsDefinition returns the registered definition, so a test that
|
||||
// pins the document's shape also pins its wiring: Seed, Validate and Normalize
|
||||
// are the three functions the registry actually runs.
|
||||
func channelSettingsDefinition(t *testing.T) Definition {
|
||||
t.Helper()
|
||||
for _, definition := range operationalDefinitions() {
|
||||
if definition.Kind == ChannelSettingsKind {
|
||||
return definition
|
||||
}
|
||||
}
|
||||
t.Fatal("channel-settings is not registered")
|
||||
return Definition{}
|
||||
}
|
||||
|
||||
// TestChannelSettingsSeedIsTheDocumentShape pins what an operator sees in the
|
||||
// Web UI: snake_case keys and a duration they can read. The Go-cased keys and
|
||||
// the nanosecond count this replaced are asserted absent, because the defect was
|
||||
// invisible to a test that only compared decoded values -- encoding/json
|
||||
// round-trips the wrong spelling perfectly happily.
|
||||
func TestChannelSettingsSeedIsTheDocumentShape(t *testing.T) {
|
||||
cfg := config.Config{Chat: config.ChatConfig{
|
||||
Email: config.EmailConfig{ListenAddr: "127.0.0.1:2525", RelayAddr: "smtp.example.com:587"},
|
||||
RateLimit: config.RateLimitConfig{Window: time.Minute, MaxRequests: 20},
|
||||
}}
|
||||
encoded, err := json.Marshal(seedChannels(cfg))
|
||||
if err != nil {
|
||||
t.Fatalf("marshal seed: %v", err)
|
||||
}
|
||||
var document map[string]any
|
||||
if err := json.Unmarshal(encoded, &document); err != nil {
|
||||
t.Fatalf("unmarshal seed: %v", err)
|
||||
}
|
||||
rateLimit, ok := document["rate_limit"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("rate_limit = %#v, want an object", document["rate_limit"])
|
||||
}
|
||||
if got := rateLimit["window"]; got != "1m0s" {
|
||||
t.Errorf("rate_limit.window = %#v, want %q: the document writes the string the file accepts", got, "1m0s")
|
||||
}
|
||||
if got := rateLimit["max_requests"]; got != float64(20) {
|
||||
t.Errorf("rate_limit.max_requests = %#v, want 20", got)
|
||||
}
|
||||
email, ok := document["email"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("email = %#v, want an object", document["email"])
|
||||
}
|
||||
if email["listen_addr"] != "127.0.0.1:2525" || email["relay_addr"] != "smtp.example.com:587" {
|
||||
t.Errorf("email = %#v, want the snake_case addresses", email)
|
||||
}
|
||||
for _, legacy := range []string{"Window", "MaxRequests", "ListenAddr", "RelayAddr"} {
|
||||
if _, present := email[legacy]; present {
|
||||
t.Errorf("email carries the Go-cased key %q", legacy)
|
||||
}
|
||||
if _, present := rateLimit[legacy]; present {
|
||||
t.Errorf("rate_limit carries the Go-cased key %q", legacy)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// legacyChannelDocument is a document as an earlier revision stored it: the Go
|
||||
// field names encoding/json fell back to, and a nanosecond count where the
|
||||
// document now writes a string. Every store written before the reshape holds
|
||||
// this, so it has to keep working.
|
||||
const legacyChannelDocument = `{"rate_limit":{"Window":60000000000,"MaxRequests":20},"email":{"ListenAddr":"127.0.0.1:2525","RelayAddr":"smtp.example.com:587"}}`
|
||||
|
||||
// TestChannelSettingsReadsADocumentWrittenBeforeTheReshape covers the two halves
|
||||
// the change needs: a legacy document still validates and still reads, and
|
||||
// writing it back (Definition.Decode is what every write path runs) rewrites it
|
||||
// into the current shape.
|
||||
func TestChannelSettingsReadsADocumentWrittenBeforeTheReshape(t *testing.T) {
|
||||
definition := channelSettingsDefinition(t)
|
||||
|
||||
if err := definition.Validate([]byte(legacyChannelDocument)); err != nil {
|
||||
t.Fatalf("Validate(legacy) = %v, want it accepted: it is what existing stores hold", err)
|
||||
}
|
||||
|
||||
decoded, err := definition.Decode([]byte(legacyChannelDocument))
|
||||
if err != nil {
|
||||
t.Fatalf("Decode(legacy) = %v", err)
|
||||
}
|
||||
if strings.Contains(string(decoded), `"Window"`) || strings.Contains(string(decoded), `60000000000`) {
|
||||
t.Fatalf("decoded document = %s, want the canonical keys and duration", decoded)
|
||||
}
|
||||
var document struct {
|
||||
RateLimit struct {
|
||||
Window string `json:"window"`
|
||||
MaxRequests int `json:"max_requests"`
|
||||
} `json:"rate_limit"`
|
||||
Email struct {
|
||||
ListenAddr string `json:"listen_addr"`
|
||||
RelayAddr string `json:"relay_addr"`
|
||||
} `json:"email"`
|
||||
}
|
||||
if err := json.Unmarshal(decoded, &document); err != nil {
|
||||
t.Fatalf("unmarshal decoded document: %v", err)
|
||||
}
|
||||
if document.RateLimit.Window != "1m0s" || document.RateLimit.MaxRequests != 20 {
|
||||
t.Errorf("rate_limit = %+v, want the legacy values preserved in the current shape", document.RateLimit)
|
||||
}
|
||||
if document.Email.ListenAddr != "127.0.0.1:2525" || document.Email.RelayAddr != "smtp.example.com:587" {
|
||||
t.Errorf("email = %+v, want the legacy values preserved", document.Email)
|
||||
}
|
||||
}
|
||||
|
||||
// TestChannelSettingsLayersEveryStoredFormOfTheDuration walks the forms a stored
|
||||
// document may hold: the legacy Go-cased keys with a nanosecond count, the
|
||||
// current keys with the string the file writes, and the current keys with a
|
||||
// count. All three have to reach config.ChatConfig, or a deployment's rate limit
|
||||
// silently stops applying.
|
||||
func TestChannelSettingsLayersEveryStoredFormOfTheDuration(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
rate map[string]any
|
||||
expected time.Duration
|
||||
}{
|
||||
{
|
||||
name: "legacy keys, nanosecond count",
|
||||
rate: map[string]any{"Window": int64(60000000000), "MaxRequests": 20},
|
||||
expected: time.Minute,
|
||||
},
|
||||
{
|
||||
name: "current keys, duration string",
|
||||
rate: map[string]any{"window": "1m", "max_requests": 20},
|
||||
expected: time.Minute,
|
||||
},
|
||||
{
|
||||
name: "current keys, nanosecond count",
|
||||
rate: map[string]any{"window": int64(60000000000), "max_requests": 20},
|
||||
expected: time.Minute,
|
||||
},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
client := NewRPCClient(&runtimeConfigClient{values: map[string]any{
|
||||
ChannelSettingsKind: map[string]any{
|
||||
"rate_limit": tt.rate,
|
||||
"email": map[string]any{"ListenAddr": "127.0.0.1:2525", "RelayAddr": "smtp.example.com:587"},
|
||||
},
|
||||
}})
|
||||
got, version, err := client.RuntimeChatConfig(t.Context(), config.ChatConfig{})
|
||||
if err != nil {
|
||||
t.Fatalf("RuntimeChatConfig: %v", err)
|
||||
}
|
||||
if version != 2 {
|
||||
t.Errorf("version = %d, want the version the store answered with", version)
|
||||
}
|
||||
if got.RateLimit.Window != tt.expected || got.RateLimit.MaxRequests != 20 || !got.RateLimit.Enabled() {
|
||||
t.Errorf("RateLimit = %+v, want window %v and 20 requests", got.RateLimit, tt.expected)
|
||||
}
|
||||
if got.Email.ListenAddr != "127.0.0.1:2525" || got.Email.RelayAddr != "smtp.example.com:587" {
|
||||
t.Errorf("Email = %+v, want the stored addresses", got.Email)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestChannelSettingsRefusesWhatItCannotApply keeps the tolerance above from
|
||||
// becoming indifference. Reading a legacy spelling is deliberate; quietly
|
||||
// swallowing a misspelling is the failure this whole shape exists to prevent,
|
||||
// because an unknown key decodes as an unset one and an unset rate limit is
|
||||
// off.
|
||||
func TestChannelSettingsRefusesWhatItCannotApply(t *testing.T) {
|
||||
definition := channelSettingsDefinition(t)
|
||||
for _, tt := range []struct{ name, document string }{
|
||||
{"a misspelt key is not an absent setting", `{"rate_limit":{"windows":"1m","max_requests":20}}`},
|
||||
{"an unknown top-level key", `{"rate_limmt":{}}`},
|
||||
{"an unparseable duration", `{"rate_limit":{"window":"soon","max_requests":20}}`},
|
||||
{"a fractional duration", `{"rate_limit":{"window":1.5,"max_requests":20}}`},
|
||||
{"a negative budget", `{"rate_limit":{"window":"1m","max_requests":-1}}`},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := definition.Validate([]byte(tt.document)); err == nil {
|
||||
t.Fatalf("Validate(%s) = nil, want a refusal: boot fails closed on a document it cannot apply", tt.document)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ type pluginSettings struct {
|
||||
func operationalDefinitions() []Definition {
|
||||
return []Definition{
|
||||
{Kind: RepositoryPoliciesKind, Title: "Repository policies", ApplyMode: "restart-required", Schema: arraySchema, Seed: func(cfg config.Config) any { return cfg.Repos }, Validate: validateRepositories},
|
||||
{Kind: ChannelSettingsKind, Title: "Channel settings", ApplyMode: "restart-required", Schema: objectSchema, Seed: seedChannels, Validate: validateChannels},
|
||||
{Kind: ChannelSettingsKind, Title: "Channel settings", ApplyMode: "restart-required", Schema: objectSchema, Seed: seedChannels, Validate: validateChannels, Normalize: normalizeChannels},
|
||||
{Kind: SchedulingPolicyKind, Title: "Scheduling policy", ApplyMode: "restart-required", Schema: objectSchema, Seed: func(cfg config.Config) any {
|
||||
return schedulingPolicy{PollInterval: cfg.PollInterval.Std().String(), MaxRetries: cfg.MaxRetries, Label: &cfg.Label, Dispatch: cfg.Dispatch}
|
||||
}, Validate: validateScheduling},
|
||||
|
||||
@@ -209,10 +209,11 @@ func runtimeChatConfigFrom(ctx context.Context, reader resourceReader, base conf
|
||||
check, install := base.Telegram.UpdateCheckCommand, base.Telegram.UpdateInstallCommand
|
||||
out = config.ChatConfig{
|
||||
Operator: settings.Operator, ShowToolCalls: settings.ShowToolCalls, MaxSteps: settings.MaxSteps,
|
||||
Models: settings.Models, Email: settings.Email, WebhookAddr: settings.WebhookAddr,
|
||||
Webhook: config.WebhookRoute{Path: settings.Webhook.Path, Secret: settings.Webhook.Secret, Template: settings.Webhook.Template, DeliverTo: settings.Webhook.DeliverTo},
|
||||
Telegram: config.TelegramConfig{AllowedUserIDs: settings.Telegram.AllowedUserIDs, Token: settings.Telegram.Token, TokenEnv: settings.Telegram.TokenEnv, UpdateCheckCommand: check, UpdateInstallCommand: install},
|
||||
RateLimit: settings.RateLimit, UnrestrictedFilesystem: settings.UnrestrictedFilesystem, Workspace: settings.Workspace,
|
||||
Models: settings.Models, Email: config.EmailConfig{ListenAddr: settings.Email.ListenAddr, RelayAddr: settings.Email.RelayAddr}, WebhookAddr: settings.WebhookAddr,
|
||||
Webhook: config.WebhookRoute{Path: settings.Webhook.Path, Secret: settings.Webhook.Secret, Template: settings.Webhook.Template, DeliverTo: settings.Webhook.DeliverTo},
|
||||
Telegram: config.TelegramConfig{AllowedUserIDs: settings.Telegram.AllowedUserIDs, Token: settings.Telegram.Token, TokenEnv: settings.Telegram.TokenEnv, UpdateCheckCommand: check, UpdateInstallCommand: install},
|
||||
RateLimit: config.RateLimitConfig{Window: time.Duration(settings.RateLimit.Window), MaxRequests: settings.RateLimit.MaxRequests},
|
||||
UnrestrictedFilesystem: settings.UnrestrictedFilesystem, Workspace: settings.Workspace,
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user