The layering rebuilds cfg.Tools from the control plane's projection wholesale, and ParallelToolCalls was a plain bool. A tool-settings resource seeded before the field existed therefore carried no key for it, the zero value replaced the operator's file-level parallel_tool_calls = true, and the setting was inert -- on exactly the stores that cannot be re-seeded, which is the case a new field has to reach. Make it a *bool: the shape schedulingPolicy.Label documents, and the shape the mcp headers in this same function already use. Absent now means "leave the file's value in force", an explicit stored value still outranks the file, and a server with no file entry has nothing to inherit and keeps the zero value.
315 lines
14 KiB
Go
315 lines
14 KiB
Go
package controlplane
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
|
)
|
|
|
|
// resourceReader is the one read the layering performs: a resource kind's value
|
|
// and the version it came from, or nothing at all when the store holds no value
|
|
// for that kind. The gRPC client and the in-process server both provide it, so
|
|
// "what the database covers in the running config" has exactly one
|
|
// implementation. The offline validate runs the same code as boot, which is the
|
|
// only way its verdict can be boot's verdict.
|
|
//
|
|
// found is false only for the reader that dials a live State Store: a kind with
|
|
// no stored value leaves the file document's value in effect instead of failing
|
|
// the process, which is the state a seed ImportConfig refused leaves behind
|
|
// (controlplane.Server.ImportConfig skips it) and the state of a database the
|
|
// migration has not reached yet. storeReader never reports absent -- it answers
|
|
// with the seed the State Store would write -- so the offline verdict stays
|
|
// boot's.
|
|
|
|
// RuntimeConfig applies restart-scoped database resources over bootstrap
|
|
// configuration. Values intentionally absent from a control-plane projection,
|
|
// such as secret-bearing MCP headers and channel update commands, remain
|
|
// bootstrap-owned.
|
|
//
|
|
// It also returns the version of each kind it read, so the process that
|
|
// layers them in can report which version it is running
|
|
// (docs/prds/control-plane-apply-status.md).
|
|
func (c *Client) RuntimeConfig(ctx context.Context, base config.Config) (config.Config, map[string]int64, error) {
|
|
return runtimeConfigFrom(ctx, c, base)
|
|
}
|
|
|
|
// RuntimeChatConfig layers the stored channel settings over the file document's
|
|
// chat section.
|
|
|
|
// StoredRuntimeConfig is RuntimeConfig's layering over the store this server
|
|
// owns, for a caller holding the database file rather than a connection to it.
|
|
// Boot layers the stored settings onto the file config and then runs
|
|
// configuration.Validate, so an offline check of that database has to make the
|
|
// same first move with the same implementation; called with the config the
|
|
// daemon would load, the result is the document boot decides on.
|
|
//
|
|
// A kind the store does not hold is read as the seed the State Store would
|
|
// write for it from base (see storeReader.query), because that store seeds
|
|
// every kind before it serves.
|
|
func (s *Server) StoredRuntimeConfig(ctx context.Context, base config.Config) (config.Config, map[string]int64, error) {
|
|
seeds, err := s.seededValues(base)
|
|
if err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
return runtimeConfigFrom(ctx, storeReader{resources: s.store, seeds: seeds}, base)
|
|
}
|
|
|
|
// storeReader reads the server's own store. It mirrors what the gRPC read
|
|
// answers -- a kind that is stored is the value and version it holds -- with
|
|
// one difference: a kind the store does not hold yet is the value the State
|
|
// Store seeds for it, since the daemon dials a store that has already run
|
|
// ImportConfig over the same config.
|
|
type storeReader struct {
|
|
resources ResourceStore
|
|
seeds map[string][]byte
|
|
}
|
|
|
|
func (r storeReader) Query(ctx context.Context, kind string, decode func([]byte) error) (int64, bool, error) {
|
|
resource, err := r.resources.Resource(ctx, storecontract.DefaultOrgID, kind)
|
|
if errors.Is(err, storecontract.ErrResourceNotFound) {
|
|
seed, ok := r.seeds[kind]
|
|
if !ok {
|
|
return 0, false, fmt.Errorf("read %s: %w", kind, storecontract.ErrResourceNotFound)
|
|
}
|
|
if err := decode(seed); err != nil {
|
|
return 0, false, fmt.Errorf("decode %s seed: %w", kind, err)
|
|
}
|
|
// No version: nothing has been stored for this kind, so nothing has
|
|
// been applied either. It is still a value to layer in -- the seed --
|
|
// which is why this reader never reports the kind absent.
|
|
return 0, true, nil
|
|
}
|
|
if err != nil {
|
|
return 0, false, fmt.Errorf("read %s: %w", kind, err)
|
|
}
|
|
if err := decode(resource.Value); err != nil {
|
|
return 0, false, fmt.Errorf("decode %s: %w", kind, err)
|
|
}
|
|
return resource.Version, true, nil
|
|
}
|
|
|
|
func runtimeConfigFrom(ctx context.Context, reader controlplanerpc.ResourceReader, base config.Config) (config.Config, map[string]int64, error) {
|
|
out := base.Clone()
|
|
versions := map[string]int64{}
|
|
if err := layerResource(ctx, reader, versions, ProviderSettingsKind, func(value []byte) error {
|
|
var providers map[string]providerDocument
|
|
if err := json.Unmarshal(value, &providers); err != nil {
|
|
return err
|
|
}
|
|
out.Providers = make(map[string]config.Provider, len(providers))
|
|
for name, provider := range providers {
|
|
if err := rejectBootDerivedProviderEnv(name, provider); err != nil {
|
|
return err
|
|
}
|
|
out.Providers[name] = config.Provider{Class: provider.Class, APIKeyEnv: provider.APIKeyEnv, APIKey: provider.APIKey, BaseURL: provider.BaseURL}
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
// The stored role assignments own cfg.Models once the store carries a
|
|
// value, the way the provider layer above owns cfg.Providers: a role
|
|
// removed from the store is gone from the layered document too. The
|
|
// runtime-resource watches re-layer over an already-layered base
|
|
// (archie-core-zfb0.1), so json.Unmarshal's map merge here would
|
|
// resurrect a role the store deleted every time any watched kind changed.
|
|
if err := layerResource(ctx, reader, versions, ModelRoleAssignmentsKind, func(value []byte) error {
|
|
var roles map[string]string
|
|
if err := json.Unmarshal(value, &roles); err != nil {
|
|
return err
|
|
}
|
|
out.Models = roles
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
if err := layerResourceJSON(ctx, reader, versions, RepositoryPoliciesKind, &out.Repos); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
chat, chatVersion, err := controlplanerpc.RuntimeChatConfigFrom(ctx, reader, out.Chat)
|
|
if err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
out.Chat, versions[ChannelSettingsKind] = chat, chatVersion
|
|
if err := layerResource(ctx, reader, versions, SchedulingPolicyKind, func(value []byte) error {
|
|
var policy schedulingPolicy
|
|
if err := json.Unmarshal(value, &policy); err != nil {
|
|
return err
|
|
}
|
|
interval, err := time.ParseDuration(policy.PollInterval)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
out.PollInterval, out.MaxRetries, out.Dispatch = config.Duration(interval), policy.MaxRetries, policy.Dispatch
|
|
// The label pairs with the trigger and layers with it: once the store
|
|
// carries one, it owns it, and the file can no longer drop the label a
|
|
// stored label-requiring trigger depends on. A policy stored before
|
|
// the field existed -- the nil the seed never produces -- leaves the
|
|
// file document's label in force, and boot's gate judges the pairing
|
|
// either way.
|
|
if policy.Label != nil {
|
|
out.Label = *policy.Label
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
// review-settings owns the pr-review dials once the store carries a value.
|
|
// The two fields are assigned individually, not `out.Review = ...`: a field
|
|
// added to config.Review later keeps the file's value until the stored
|
|
// document carries it (docs/prds/review-settings-resource.md).
|
|
if err := layerResource(ctx, reader, versions, ReviewSettingsKind, func(value []byte) error {
|
|
var settings reviewSettings
|
|
if err := json.Unmarshal(value, &settings); err != nil {
|
|
return err
|
|
}
|
|
out.Review.PrecisionGate = settings.PrecisionGate
|
|
out.Review.ApproveBeforePost = settings.ApproveBeforePost
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
return runtimeToolConfigFrom(ctx, reader, versions, out)
|
|
}
|
|
|
|
func runtimeToolConfigFrom(ctx context.Context, reader controlplanerpc.ResourceReader, versions map[string]int64, out config.Config) (config.Config, map[string]int64, error) {
|
|
if err := layerResource(ctx, reader, versions, ToolSettingsKind, func(value []byte) error {
|
|
var settings toolSettings
|
|
if err := json.Unmarshal(value, &settings); err != nil {
|
|
return err
|
|
}
|
|
headers := make(map[string]map[string]string, len(out.Tools.MCPServers))
|
|
fileParallelToolCalls := make(map[string]bool, len(out.Tools.MCPServers))
|
|
for _, server := range out.Tools.MCPServers {
|
|
headers[server.Name] = server.Headers
|
|
fileParallelToolCalls[server.Name] = server.ParallelToolCalls
|
|
}
|
|
servers := make([]config.MCPServer, 0, len(settings.MCPServers))
|
|
for _, server := range settings.MCPServers {
|
|
// This assignment replaces cfg.Tools wholesale, so every field the
|
|
// stored resource does not carry has to be taken back from the file
|
|
// or the operator's file value is dropped. Headers have always been
|
|
// file-owned; ParallelToolCalls joins them when the store predates
|
|
// the field, because a nil pointer means "no stored decision", not
|
|
// "false". A server with no file entry has nothing to inherit and
|
|
// gets the zero value.
|
|
parallelToolCalls := fileParallelToolCalls[server.Name]
|
|
if server.ParallelToolCalls != nil {
|
|
parallelToolCalls = *server.ParallelToolCalls
|
|
}
|
|
servers = append(servers, config.MCPServer{
|
|
Name: server.Name,
|
|
Transport: server.Transport,
|
|
Command: server.Command,
|
|
Args: server.Args,
|
|
WorkDir: server.WorkDir,
|
|
URL: server.URL,
|
|
Headers: headers[server.Name],
|
|
SSEEndpoint: server.SSEEndpoint,
|
|
MessageEndpoint: server.MessageEndpoint,
|
|
ParallelToolCalls: parallelToolCalls,
|
|
})
|
|
}
|
|
out.Tools = config.ToolsConfig{MCPServers: servers, Policy: settings.Policy, WebFetch: settings.WebFetch, Minimax: config.MinimaxConfig{Enabled: settings.Minimax.Enabled, APIKey: settings.Minimax.APIKey, BaseURL: settings.Minimax.BaseURL}}
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
if err := layerResource(ctx, reader, versions, PluginSettingsKind, func(value []byte) error {
|
|
var settings pluginSettings
|
|
if err := json.Unmarshal(value, &settings); err != nil {
|
|
return err
|
|
}
|
|
out.PluginDir, out.ModuleDir, out.SecretEngineDir, out.SkillsDir = settings.PluginDir, settings.ModuleDir, settings.SecretEngineDir, settings.SkillsDir
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
if err := layerResource(ctx, reader, versions, ContainerRuntimePoliciesKind, func(value []byte) error {
|
|
var policies containerRuntimePolicies
|
|
if err := json.Unmarshal(value, &policies); err != nil {
|
|
return err
|
|
}
|
|
// Profiles survives this assignment untouched: containerRuntimePolicies
|
|
// carries no Profiles field at all (it is AgentProfileKind's document
|
|
// now), and settings() never sets it, so whatever the AgentProfileKind
|
|
// layering step below assigns is what's left standing regardless of
|
|
// which of the two runs first.
|
|
//
|
|
// RegistryAuth is the same shape of file-owned field, with no resource of
|
|
// its own to restore it later the way CredentialBindingsKind restores
|
|
// Credentials: the document cannot carry a secret reference, so it has to
|
|
// survive this assignment or the daemon pulls anonymously again.
|
|
profiles := out.Containers.Profiles
|
|
registryAuth := out.Containers.RegistryAuth
|
|
out.Containers = policies.settings()
|
|
out.Containers.Profiles = profiles
|
|
out.Containers.RegistryAuth = registryAuth
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
// AgentProfileKind is a separate resource from ContainerRuntimePoliciesKind
|
|
// (config.ContainerConfig.Profiles is json:"-") so a Kit profile applies
|
|
// without a restart: a stored value replaces the file's outright rather
|
|
// than merging into it, the same reason ModelRoleAssignmentsKind above
|
|
// reassigns out.Models wholesale instead of decoding into the existing map.
|
|
if err := layerResource(ctx, reader, versions, AgentProfileKind, func(value []byte) error {
|
|
var profiles map[string]agentProfile
|
|
if err := json.Unmarshal(value, &profiles); err != nil {
|
|
return err
|
|
}
|
|
out.Containers.Profiles = agentProfilesSettings(profiles)
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
// CredentialBindingsKind is likewise its own resource: a binding applies
|
|
// without a restart, and a stored value replaces the file's outright.
|
|
if err := layerResource(ctx, reader, versions, CredentialBindingsKind, func(value []byte) error {
|
|
var bindings []credentialBinding
|
|
if err := json.Unmarshal(value, &bindings); err != nil {
|
|
return err
|
|
}
|
|
out.Containers.Credentials = credentialBindingsSettings(bindings)
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
return out, versions, nil
|
|
}
|
|
|
|
// layerResource decodes a resource and records the version it came from, so the
|
|
// layering ends up holding the version of every kind it applied.
|
|
//
|
|
// A kind with no stored value is not an error and records no version: the file
|
|
// document's value stays in effect. That is the state a seed the resource
|
|
// validator refused leaves behind (controlplane.Server.ImportConfig skips it),
|
|
// and failing here instead would stop the process with a database-named error
|
|
// that editing config.toml cannot clear.
|
|
func layerResource(ctx context.Context, reader controlplanerpc.ResourceReader, versions map[string]int64, kind string, decode func([]byte) error) error {
|
|
version, found, err := reader.Query(ctx, kind, decode)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if found {
|
|
versions[kind] = version
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func layerResourceJSON(ctx context.Context, reader controlplanerpc.ResourceReader, versions map[string]int64, kind string, target any) error {
|
|
return layerResource(ctx, reader, versions, kind, func(value []byte) error { return json.Unmarshal(value, target) })
|
|
}
|
|
|
|
// query returns the version of the resource it decoded, so callers that layer
|
|
// a resource in can report the version they applied. A kind the store holds no
|
|
// value for is reported as not found rather than as an error: the caller leaves
|
|
// the file document's value in effect (see resourceReader).
|