Files
archie/internal/app/controlplane/container_settings.go
T
sam adf44d827f feat(controlplane): agent profiles as their own live control-plane resource
docs/prds/external-agent-harness.md ("Selection"): a Kit profile is an org
resource that applies without a restart. Profiles were bundled inside
container-runtime-policies (ApplyMode: restart-required), which is correct
for Image/MaxConcurrency/Network but not for Profiles, which nothing
startup-builds: daemon.configFor reads config.Holder fresh per dispatch
(pinTaskProfile -> Containers.Profile).

Split Profiles into its own resource, AgentProfileKind ("agent-profiles"),
ApplyMode: live, seeded from [containers.profiles] and validated with the
same rules the file document uses (config.ValidateAgentProfiles, factored
out of ContainerConfig.ValidateProfiles so the two cannot disagree).
config.ContainerConfig.Profiles is now json:"-" so container-runtime-policies
never carries it (one authoritative resource per field, not two), and
runtime_config.go layers the new resource in by wholesale map replacement
(matching ModelRoleAssignmentsKind's precedent) rather than merging into the
file's map. Added Profiles to reload.go's Containers reloadable-subfield set
to match this.

Deliberately NOT built here: true per-org multi-tenant storage for this
resource (docs/prds/orgs-and-access.md places agent profiles under org
ownership, granted to workspaces). No existing control-plane resource kind
in this registry does real per-org storage yet either -- schedules,
workflow settings, etc. are all single instance-wide documents -- so
agent-profiles follows that same established, tested pattern rather than
inventing org-scoped storage unilaterally for this one resource. Org/
workspace grants over agent-profiles are a follow-on, not a redo: nothing
about this resource's shape needs to change to add that scoping later.
2026-09-27 05:25:38 +10:00

149 lines
6.2 KiB
Go

package controlplane
import (
"encoding/json"
"fmt"
"strings"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
)
// containerRuntimePolicies is the container-runtime-policies resource document.
// Its shape IS the contract the Web UI edits, so it is defined here with
// explicit snake_case json tags and never handed the internal config.ContainerConfig
// directly: that struct's tags belong to the TOML file format, and when it was
// the document, encoding/json fell back to its Go field names -- "Image",
// "MaxConcurrency", "LegacyEnabled" -- the only Go-cased resource left in the
// registry (archie-core-1171). The two durations are config.Duration, which
// already writes the string the file accepts; the leak was the keys only.
//
// Profiles is deliberately not a field here: config.ContainerConfig.Profiles
// is json:"-" (its own resource, AgentProfileKind, applies live where the
// rest of this one is restart-required -- docs/prds/external-agent-harness.md
// "Selection"), so this document never carries it either.
type containerRuntimePolicies struct {
Image string `json:"image"`
MaxConcurrency int `json:"max_concurrency"`
MaxUptime config.Duration `json:"max_uptime"`
VolumeTTL config.Duration `json:"volume_ttl"`
PullPolicy string `json:"pull_policy"`
Network string `json:"network"`
}
// agentProfile is one named execution environment as the document writes it.
type agentProfile struct {
Image string `json:"image,omitempty"`
Kit []string `json:"kit,omitempty"`
Adapter string `json:"adapter,omitempty"`
Tools []string `json:"tools,omitempty"`
}
// legacyContainerKeys are the Go field names encoding/json fell back to while
// the document was the internal config struct. The decoder's case-insensitive
// fallback compares whole keys, so "MaxConcurrency" never matches
// "max_concurrency"; every store written before the reshape holds these keys,
// so reading them is deliberate and everything else stays strict.
var legacyContainerKeys = map[string]string{
"Image": "image", "MaxConcurrency": "max_concurrency", "MaxUptime": "max_uptime",
"VolumeTTL": "volume_ttl", "PullPolicy": "pull_policy", "Network": "network",
}
// legacyProfileKeys are the same fallback at the profile nesting level.
var legacyProfileKeys = map[string]string{
"Image": "image", "Kit": "kit", "Adapter": "adapter", "Tools": "tools",
}
func (p *agentProfile) UnmarshalJSON(data []byte) error {
type document agentProfile
var decoded document
err := controlplanerpc.DecodeRenamingLegacyKeys(data, legacyProfileKeys, &decoded)
if err != nil {
return err
}
*p = agentProfile(decoded)
return nil
}
func (p *containerRuntimePolicies) UnmarshalJSON(data []byte) error {
type document containerRuntimePolicies
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
// "LegacyEnabled" decoded the removed containers.enabled switch during the
// window in which this resource existed, and stores written then hold it.
// It has no counterpart in the current shape -- it never had a runtime
// consumer -- so it is dropped rather than renamed: the strict decode would
// otherwise refuse a document every deployment of that window stores.
delete(raw, "LegacyEnabled")
// "profiles"/"Profiles" carried config.ContainerConfig.Profiles before it
// moved to its own resource, AgentProfileKind; a document stored under
// either name before that still decodes, with the key simply dropped.
delete(raw, "profiles")
delete(raw, "Profiles")
encoded, err := json.Marshal(raw)
if err != nil {
return err
}
var decoded document
if err := controlplanerpc.DecodeRenamingLegacyKeys(encoded, legacyContainerKeys, &decoded); err != nil {
return err
}
*p = containerRuntimePolicies(decoded)
return nil
}
// settings maps the document onto the internal config struct the daemon runs.
// Profiles is never set here: config.ContainerConfig.Profiles is json:"-",
// and AgentProfileKind's own layering step in runtime_config.go is what sets
// it, after this one runs.
func (p containerRuntimePolicies) settings() config.ContainerConfig {
return config.ContainerConfig{
Image: p.Image, MaxConcurrency: p.MaxConcurrency, MaxUptime: p.MaxUptime,
VolumeTTL: p.VolumeTTL, PullPolicy: p.PullPolicy, Network: p.Network,
}
}
func seedContainerPolicies(cfg config.Config) any {
containers := cfg.Containers
return containerRuntimePolicies{
Image: containers.Image, MaxConcurrency: containers.MaxConcurrency, MaxUptime: containers.MaxUptime,
VolumeTTL: containers.VolumeTTL, PullPolicy: containers.PullPolicy, Network: containers.Network,
}
}
func validateContainers(input []byte) error {
return validateAs(input, func(policies containerRuntimePolicies) error {
settings := policies.settings()
if settings.MaxConcurrency < 0 || settings.MaxUptime < 0 || settings.VolumeTTL < 0 {
return fmt.Errorf("container limits must not be negative")
}
if settings.PullPolicy != "" && settings.PullPolicy != "missing" && settings.PullPolicy != "always" {
return fmt.Errorf("pull_policy must be missing or always")
}
// The image is required for autonomous workflow workers; the same rule
// holds for the stored policies that replace the file's [containers].
if strings.TrimSpace(settings.Image) == "" {
return fmt.Errorf("container image is required for autonomous workflow workers")
}
// Profiles is json:"-" on config.ContainerConfig: it is
// AgentProfileKind's document now, validated there.
return nil
})
}
// normalizeContainerPolicies canonicalizes the stored document: decoding into
// the shape above and re-encoding writes the snake_case keys, 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
// (containerRuntimePolicies), so nothing has to be re-saved before it can be
// read again.
func normalizeContainerPolicies(input []byte) ([]byte, error) {
var policies containerRuntimePolicies
if err := json.Unmarshal(input, &policies); err != nil {
return nil, err
}
return json.Marshal(policies)
}