Files
archie/internal/infrastructure/configuration/defaults.go
T
sam 6bdbd5e31e feat(config): key services by name through a registry
Services was a struct with one field per service, so each service's name was
stated three times: the field, the loader's defaulting branch, and every
consumer's field access. Adding one meant editing all three, and nothing
stopped them disagreeing. The drift was already there: stateStoreResolvedToken
and gatewayResolvedToken were identical apart from the env var each hard-coded.

Services becomes map[string]ServiceConnection, and RegisterService declares a
service once with the five fields it has. Everything that differs between
services is now an argument rather than a branch elsewhere: an empty registered
target means the operator must supply one, which is how services.state.target
stays required while the gateway's is defaulted, and the registered token env
var replaces the two copied resolvers with Services.ResolvedToken.

Defaulting iterates the registry, so a new service needs no loader edit.
Consumers read Services.Get(name).

Keeping a typo'd section reported took explicit work. [services.gatway] used to
land in toml.MetaData.Undecoded() because the struct had no such field; a map
decodes any section, so it would have loaded silently and left the daemon
dialing the default. unregisteredServiceKeys asks the registry which names are
real, and the test for it fails with an empty UnknownKeys when that check is
removed.

The TOML shape is unchanged, so no deployment profile, config.example.toml or
install.sh edit is needed.

Refs: archie-core-u23g
2026-09-18 23:32:36 +10:00

327 lines
11 KiB
Go

package configuration
import (
"os"
"path/filepath"
"strings"
"time"
"github.com/samcharles93/archie-core/internal/config"
)
// Defaults applied to absent input. Named so a reader can find the value
// without reading the code that applies it.
const (
defaultPollInterval = 60 * time.Second
defaultDiffCapLines = 400
defaultWebListen = "127.0.0.1:8484" // "off" disables
defaultMaxRetries = 3
defaultForgeType = "github"
defaultForgeHost = "https://github.com"
defaultDispatchTrigger = "assignee"
defaultAckReaction = "eyes"
defaultContainerUptime = 60 * time.Minute
defaultVolumeTTL = 72 * time.Hour
defaultPullPolicy = "missing"
defaultContainerImage = "ghcr.io/samcharles93/archie-agent:latest"
// defaultMaxResultChars bounds one tool result. It matches the 50KB the
// lifted workspace tools already truncate themselves at
// (tools/builtin.DefaultMaxBytes), so those stay the binding limit and
// only tools with no truncation of their own -- skill bodies, MCP
// results -- see a change.
defaultMaxResultChars = 50_000
defaultWebFetchTimeout = 30 * time.Second
// defaultWebFetchMaxBytes bounds a fetched body before the result limit
// above trims it further. Generous enough for a documentation page,
// small enough that a large download cannot occupy the daemon.
defaultWebFetchMaxBytes = 2_000_000
// Capture defaults, per docs/prds/event-capture-storage.md's disk-bound
// mechanics: retention + row cap + per-request size cap + per-source
// rate limit, composed.
defaultCaptureRetention = 7 * 24 * time.Hour
defaultCaptureMaxEvents = 5000
defaultCaptureMaxBodyBytes = 256 * 1024
defaultCaptureRatePerSecond = 1.0
defaultCaptureRateBurst = 5
// defaultHealthListen is the daemon's own liveness address. The update
// watchdog's fallback in scripts/archie-update-watchdog must match it.
defaultHealthListen = "127.0.0.1:8485"
)
// applyDefaults fills in every absent value. It never reports an error and
// never rejects input.
//
// Defaulting and validation were previously interleaved: functions named
// applyXDefaults also returned validation errors, and functions named
// validateX quietly mutated the config. That made it impossible to answer
// "what did the operator actually set?" -- the only way to see the supplied
// input was to not call them. Splitting them means defaults can be applied,
// inspected, and reasoned about independently of whether the result is valid.
func (l *Loader) applyDefaults(cfg *config.Config) {
applyServiceDefaults(cfg)
if cfg.Health.Listen == "" {
cfg.Health.Listen = defaultHealthListen
}
l.applyGeneralDefaults(cfg)
applyForgeDefaults(cfg)
applyDispatchDefaults(cfg)
applyIdentityDefaults(cfg)
applyContainerDefaults(cfg)
applyToolPolicyDefaults(cfg)
applyCaptureDefaults(cfg)
applyNATSDefaults(cfg)
applyMemoryDefaults(cfg)
}
// applyServiceDefaults fills each registered service's absent addresses from
// its registration. It iterates the registry rather than naming services, so a
// new service needs no branch here -- only its RegisterService call.
//
// A registered service always ends with an entry, so later code can read
// cfg.Services.Get(name) without distinguishing "absent section" from
// "section with every field defaulted". An empty registered Target is a
// service whose target the operator must supply, and stays empty for
// validation to reject rather than being defaulted to something dialable.
func applyServiceDefaults(cfg *config.Config) {
if cfg.Services == nil {
cfg.Services = config.Services{}
}
for _, spec := range config.RegisteredServices() {
conn := cfg.Services[spec.Name]
if conn.Target == "" {
conn.Target = spec.Target
}
if conn.Listen == "" {
conn.Listen = spec.Listen
}
cfg.Services[spec.Name] = conn
}
}
// applyMemoryDefaults fills an absent engine choice with the builtin
// engine, so "nothing configured" and "explicitly builtin" are the same
// resolved state everywhere after this point -- validateMemory and every
// later reader see one value, not two that happen to mean the same thing.
func applyMemoryDefaults(cfg *config.Config) {
if cfg.Memory.Engine == "" {
cfg.Memory.Engine = memoryEngineBuiltin
}
}
// DefaultCapture returns the webhook capture endpoint's default settings
// (docs/prds/event-capture-storage.md). applyCaptureDefaults fills a decoded
// config with them; the UI process composes the same capture receiver and
// must produce the same effective values when it is driven by flags alone
// and no configuration file projected [capture] to it.
func DefaultCapture() config.CaptureConfig {
return config.CaptureConfig{
Retention: config.Duration(defaultCaptureRetention),
MaxEvents: defaultCaptureMaxEvents,
MaxBodyBytes: defaultCaptureMaxBodyBytes,
RatePerSecond: defaultCaptureRatePerSecond,
RateBurst: defaultCaptureRateBurst,
}
}
// applyCaptureDefaults fills the webhook capture endpoint's settings. See
// [config.CaptureConfig].
func applyCaptureDefaults(cfg *config.Config) {
c := DefaultCapture()
if cfg.Capture.Retention == 0 {
cfg.Capture.Retention = c.Retention
}
if cfg.Capture.MaxEvents == 0 {
cfg.Capture.MaxEvents = c.MaxEvents
}
if cfg.Capture.MaxBodyBytes == 0 {
cfg.Capture.MaxBodyBytes = c.MaxBodyBytes
}
if cfg.Capture.RatePerSecond == 0 {
cfg.Capture.RatePerSecond = c.RatePerSecond
}
if cfg.Capture.RateBurst == 0 {
cfg.Capture.RateBurst = c.RateBurst
}
}
// applyToolPolicyDefaults fills the tool result limits.
//
// Zero means "unset" and takes the default; a negative value is the operator
// saying "no limit" and is left alone. See [config.ToolPolicy].
func applyToolPolicyDefaults(cfg *config.Config) {
if cfg.Tools.Policy.MaxResultChars == 0 {
cfg.Tools.Policy.MaxResultChars = defaultMaxResultChars
}
// SpillDir is deliberately NOT defaulted.
//
// Spilling hands the model a file path to read back, and the read tool is
// confined to chat.workspace. A default under work_dir is outside that for
// any workspace narrower than the whole home directory, and always outside
// a task agent's worktree -- so the model would be given a path it cannot
// open and the result would be lost rather than displaced. Truncating
// inline at least shows the first part of the result and says so.
//
// An operator who wants spilling points this at a directory inside the
// workspace; startup warns when it is not (see cmd/archied).
applyWebFetchDefaults(cfg)
}
// applyWebFetchDefaults fills the fetch tool's settings. Enabled is left alone
// when the operator set it either way; see [config.WebFetchConfig].
func applyWebFetchDefaults(cfg *config.Config) {
if cfg.Tools.WebFetch.Timeout == 0 {
cfg.Tools.WebFetch.Timeout = config.Duration(defaultWebFetchTimeout)
}
if cfg.Tools.WebFetch.MaxBytes == 0 {
cfg.Tools.WebFetch.MaxBytes = defaultWebFetchMaxBytes
}
}
// DefaultWorkDir returns the work directory an unset work_dir would have been
// given. Callers that must fall back when a configured path turns out to be
// unusable use this so the fallback is the same location the operator would
// have got by saying nothing, rather than a second guess that only this
// caller knows about.
func DefaultWorkDir() string {
return filepath.Join(xdgDataHome(), "archie", "work")
}
// applyGeneralDefaults derives the paths and limits that have no owning
// section.
func (l *Loader) applyGeneralDefaults(cfg *config.Config) {
cfg.SecretEngineDir = expandHomePath(cfg.SecretEngineDir)
cfg.WorkDir = expandHomePath(cfg.WorkDir)
cfg.DBPath = expandHomePath(cfg.DBPath)
cfg.Chat.Workspace = expandHomePath(cfg.Chat.Workspace)
if cfg.WorkDir == "" {
cfg.WorkDir = filepath.Join(l.dataHome(), "archie", "work")
}
if cfg.DBPath == "" {
cfg.DBPath = filepath.Join(l.dataHome(), "archie", "archie.db")
}
cfg.Indexing = cfg.Indexing.WithDefaults(cfg.WorkDir)
if cfg.PollInterval == 0 {
cfg.PollInterval = config.Duration(defaultPollInterval)
}
if cfg.DiffCapLines == 0 {
cfg.DiffCapLines = defaultDiffCapLines
}
if cfg.Web.Listen == "" {
cfg.Web.Listen = defaultWebListen
}
if cfg.MaxRetries == 0 {
cfg.MaxRetries = defaultMaxRetries
}
}
func expandHomePath(path string) string {
if path != "~" && !strings.HasPrefix(path, "~/") {
return path
}
home, err := os.UserHomeDir()
if err != nil {
return path
}
if path == "~" {
return home
}
return filepath.Join(home, strings.TrimPrefix(path, "~/"))
}
// applyForgeDefaults fills the forge section, promoting the legacy
// token_env field to a secret reference when no explicit one was given.
func applyForgeDefaults(cfg *config.Config) {
if cfg.Forge.Type == "" {
cfg.Forge.Type = defaultForgeType
}
if cfg.Forge.Host == "" {
cfg.Forge.Host = defaultForgeHost
}
if cfg.Forge.Token.Engine == "" && cfg.Forge.Token.Key == "" && cfg.Forge.TokenEnv != "" {
cfg.Forge.Token = config.SecretRef{Engine: "env", Key: cfg.Forge.TokenEnv}
}
if cfg.Forge.Intake == "" {
cfg.Forge.Intake = config.ForgeIntakePoll
}
}
// applyNATSDefaults resolves the nats mode from URL when the operator left it
// unset. URL set implies external; URL empty implies embedded.
func applyNATSDefaults(cfg *config.Config) {
if cfg.NATS.Mode == "" {
if cfg.NATS.URL != "" {
cfg.NATS.Mode = config.NATSModeExternal
} else {
cfg.NATS.Mode = config.NATSModeEmbedded
}
}
}
// applyDispatchDefaults fills the dispatch section. "off" is the documented
// way to disable the acknowledgement reaction, and normalises to empty.
func applyDispatchDefaults(cfg *config.Config) {
if cfg.Dispatch.Trigger == "" {
cfg.Dispatch.Trigger = defaultDispatchTrigger
}
switch {
case cfg.Dispatch.AckReaction == "":
cfg.Dispatch.AckReaction = defaultAckReaction
case isOff(cfg.Dispatch.AckReaction):
cfg.Dispatch.AckReaction = ""
}
if cfg.Dispatch.Labels == nil {
cfg.Dispatch.Labels = map[string]string{}
}
for k, v := range config.DispatchLabelDefaults() {
if cfg.Dispatch.Labels[k] == "" {
cfg.Dispatch.Labels[k] = v
}
}
}
// applyIdentityDefaults derives forge-appropriate commit emails wherever the
// operator omitted one, for both deployment shapes.
func applyIdentityDefaults(cfg *config.Config) {
if len(cfg.Identities) > 0 {
for i := range cfg.Identities {
cfg.Identities[i].BotEmail = defaultBotEmail(cfg.Identities[i].Forge.Type, cfg.Identities[i].BotUser, cfg.Identities[i].BotEmail)
}
}
cfg.BotEmail = defaultBotEmail(cfg.Forge.Type, cfg.BotUser, cfg.BotEmail)
}
func defaultBotEmail(forgeType, user, email string) string {
if user == "" || email != "" {
return email
}
switch forgeType {
case forgeTypeGitHub:
return user + "@users.noreply.github.com"
case forgeTypeGitea:
return user + "@gitea.local"
}
return email
}
// applyContainerDefaults makes managed task workers usable without an explicit
// [containers] section. A negative VolumeTTL is left for validation to reject.
func applyContainerDefaults(cfg *config.Config) {
if cfg.Containers.Image == "" {
cfg.Containers.Image = defaultContainerImage
}
if cfg.Containers.MaxUptime == 0 {
cfg.Containers.MaxUptime = config.Duration(defaultContainerUptime)
}
if cfg.Containers.VolumeTTL == 0 {
cfg.Containers.VolumeTTL = config.Duration(defaultVolumeTTL)
}
if cfg.Containers.PullPolicy == "" {
cfg.Containers.PullPolicy = defaultPullPolicy
}
}