Four defects from the re-run contract review, all in the recovery path. A store missing a control-plane kind was refused while a store with no resources table at all was accepted -- two stances inside validateStore for states the daemon cannot tell apart. The State Store seeds every kind from its own config before it serves (RunStateStore -> ImportConfig), so a kind the file does not hold yet is one archied reads the seed for: an upgrade that adds a kind, and a validate before the State Store has started again, exited 1 with "read provider-settings: resource not found" for a store the daemon boots on fine. StoredRuntimeConfig now reads a missing kind as the seed the State Store would write for it (Definition.seededValue, shared with ImportConfig so the two cannot drift), and the missing-table branch is the same stance taken to its extreme. defaultConfigPath() derived the daemon's config with os.UserConfigDir, which disagrees with archied's configHome on darwin and rejects a relative XDG_CONFIG_HOME outright -- leaving validate with an empty path and no verdict, on a host where archied boots from relative-dir/archie/config.toml. It is now the daemon's own rule, exported as archied.DefaultConfigPath and used by the daemon's own -config default too, with a test pinning the equality. -h exited 2, the code documented as "the command line was wrong", while the serve path in the same binary exits 0 and main.go advertises -h for these subcommands. ErrHelp now returns 0. validate resolved the boot config through loadConfig -> setupLogging, which created cfg.Log.File's directory, opened the log, and wrote a line stamped component="daemon" -- a diagnostic editing the deployment it inspects, and rotating a log the daemon is writing. boot.stderrLog keeps setupLogging on the stderr logger for the recovery path: no feed, no task-log registry, no durable copy. Tests: TestRecoveryValidateRunsTheGateBootRuns now asserts the unseeded and the missing-kind stores are accepted (both exited 1 before), while the bogus-policy case still refuses; TestRecoveryHelpExitsZero, TestRecoveryValidateLeavesTheDaemonLogAlone and TestRecoveryDefaultConfigFollowsTheDaemonsConfigHome are new. Each was run red first, and the seed fallback and stderrLog were mutation-checked.
222 lines
9.5 KiB
Go
222 lines
9.5 KiB
Go
package controlplane
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
|
"github.com/samcharles93/archie-core/internal/store"
|
|
)
|
|
|
|
// resourceReader is the one read the layering performs: a resource kind's value
|
|
// and the version it came from. 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.
|
|
type resourceReader interface {
|
|
query(ctx context.Context, kind string, decode func([]byte) error) (int64, error)
|
|
}
|
|
|
|
// 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.
|
|
func (c *Client) RuntimeChatConfig(ctx context.Context, base config.ChatConfig) (config.ChatConfig, int64, error) {
|
|
return runtimeChatConfigFrom(ctx, c, base)
|
|
}
|
|
|
|
// 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, error) {
|
|
resource, err := r.resources.Resource(ctx, kind)
|
|
if errors.Is(err, store.ErrResourceNotFound) {
|
|
seed, ok := r.seeds[kind]
|
|
if !ok {
|
|
return 0, fmt.Errorf("read %s: %w", kind, store.ErrResourceNotFound)
|
|
}
|
|
if err := decode(seed); err != nil {
|
|
return 0, fmt.Errorf("decode %s seed: %w", kind, err)
|
|
}
|
|
// No version: nothing has been stored for this kind, so nothing has
|
|
// been applied either.
|
|
return 0, nil
|
|
}
|
|
if err != nil {
|
|
return 0, fmt.Errorf("read %s: %w", kind, err)
|
|
}
|
|
if err := decode(resource.Value); err != nil {
|
|
return 0, fmt.Errorf("decode %s: %w", kind, err)
|
|
}
|
|
return resource.Version, nil
|
|
}
|
|
|
|
func runtimeConfigFrom(ctx context.Context, reader 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 {
|
|
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
|
|
}
|
|
if err := layerResourceJSON(ctx, reader, versions, ModelRoleAssignmentsKind, &out.Models); 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 := 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
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
return runtimeToolConfigFrom(ctx, reader, versions, out)
|
|
}
|
|
|
|
func runtimeToolConfigFrom(ctx context.Context, reader 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))
|
|
for _, server := range out.Tools.MCPServers {
|
|
headers[server.Name] = server.Headers
|
|
}
|
|
servers := make([]config.MCPServer, 0, len(settings.MCPServers))
|
|
for _, server := range settings.MCPServers {
|
|
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})
|
|
}
|
|
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 := layerResourceJSON(ctx, reader, versions, ContainerRuntimePoliciesKind, &out.Containers); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
return out, versions, nil
|
|
}
|
|
|
|
func runtimeChatConfigFrom(ctx context.Context, reader resourceReader, base config.ChatConfig) (config.ChatConfig, int64, error) {
|
|
out := base
|
|
version, err := reader.query(ctx, ChannelSettingsKind, func(value []byte) error {
|
|
var settings channelSettings
|
|
if err := json.Unmarshal(value, &settings); err != nil {
|
|
return err
|
|
}
|
|
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,
|
|
}
|
|
return nil
|
|
})
|
|
return out, version, err
|
|
}
|
|
|
|
// layerResource decodes a resource and records the version it came from, so the
|
|
// layering ends up holding the version of every kind it applied.
|
|
func layerResource(ctx context.Context, reader resourceReader, versions map[string]int64, kind string, decode func([]byte) error) error {
|
|
version, err := reader.query(ctx, kind, decode)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
versions[kind] = version
|
|
return nil
|
|
}
|
|
|
|
func layerResourceJSON(ctx context.Context, reader 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.
|
|
func (c *Client) query(ctx context.Context, kind string, decode func([]byte) error) (int64, error) {
|
|
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: kind})
|
|
if err != nil {
|
|
return 0, clientError(err)
|
|
}
|
|
if response.Resource == nil {
|
|
return 0, fmt.Errorf("%s resource missing", kind)
|
|
}
|
|
if err := decode(response.Resource.ValueJson); err != nil {
|
|
return 0, fmt.Errorf("decode %s: %w", kind, err)
|
|
}
|
|
return response.Resource.Version, nil
|
|
}
|