Runtimes now take a task launch: image, mounts, env, the daemon's NATS and State Store with the task's credentials, its model providers and the hosts its tools may reach. Docker addresses them through the egress relay as before. An OpenShell sandbox reaches NATS and the State Store over plain TCP, the model providers with their credential bound, and the task's remote MCP hosts and the allowed package registries; services addressed by IP or loopback are named by the configured services host, since sandbox policy admits only DNS names.
159 lines
5.6 KiB
Go
159 lines
5.6 KiB
Go
package controlplanestore
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"strings"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
|
)
|
|
|
|
// containerRuntimePolicies is the container-runtime-policies document, with
|
|
// snake_case JSON keys.
|
|
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"`
|
|
Runtime string `json:"runtime"`
|
|
OpenShell config.OpenShellConfig `json:"openshell"`
|
|
}
|
|
|
|
// agentProfile is one named execution environment as the document writes it.
|
|
type agentProfile struct {
|
|
Image string `json:"image,omitempty"`
|
|
Tools []string `json:"tools,omitempty"`
|
|
}
|
|
|
|
// legacyContainerKeys maps Go-cased keys from older stored documents to
|
|
// their current names.
|
|
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", "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 has no current field; drop it.
|
|
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,
|
|
Runtime: p.Runtime, OpenShell: p.OpenShell,
|
|
}
|
|
}
|
|
|
|
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,
|
|
Runtime: containers.Runtime, OpenShell: containers.OpenShell,
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
if err := validateRuntime(settings); err != nil {
|
|
return err
|
|
}
|
|
// Profiles is json:"-" on config.ContainerConfig: it is
|
|
// AgentProfileKind's document now, validated there.
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// validateRuntime checks the task runtime and, for OpenShell, its gateway
|
|
// settings.
|
|
func validateRuntime(settings config.ContainerConfig) error {
|
|
switch settings.Runtime {
|
|
case "", config.RuntimeDocker:
|
|
case config.RuntimeOpenShell:
|
|
if strings.TrimSpace(settings.OpenShell.Address) == "" {
|
|
return fmt.Errorf("the openshell runtime needs a gateway address")
|
|
}
|
|
default:
|
|
return fmt.Errorf("runtime must be docker or openshell")
|
|
}
|
|
if (settings.OpenShell.Token.Engine == "") != (settings.OpenShell.Token.Key == "") {
|
|
return fmt.Errorf("the openshell token reference needs both an engine and a key")
|
|
}
|
|
for _, host := range settings.OpenShell.EgressHosts {
|
|
if _, _, err := config.EgressHost(host); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if host := settings.OpenShell.ServicesHost; host != "" && net.ParseIP(host) != nil {
|
|
return fmt.Errorf("services_host must be a DNS name, not an IP address")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// normalizeContainerPolicies re-encodes the stored document with snake_case
|
|
// keys.
|
|
func normalizeContainerPolicies(input []byte) ([]byte, error) {
|
|
var policies containerRuntimePolicies
|
|
if err := json.Unmarshal(input, &policies); err != nil {
|
|
return nil, err
|
|
}
|
|
return json.Marshal(policies)
|
|
}
|