Files
archie/internal/infrastructure/configuration/defaults.go
T
sam 9d8f364af1
Quality / check (push) Successful in 5m53s
deploy / gate (push) Canceled after 0s
deploy / archied-image (push) Canceled after 0s
deploy / agent-image (push) Canceled after 0s
deploy / dist-zip (push) Canceled after 0s
feat(releaseupdate): signed OCI distribution sources for releases and the catalogue (#1715)
2026-10-31 18:18:43 +11:00

338 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
defaultWebListen = "127.0.0.1:8484" // "off" disables
defaultMaxRetries = 3
defaultForgeType = "github"
defaultForgeHost = "https://github.com"
defaultDispatchTrigger = "assignee"
defaultAckReaction = "eyes"
// defaultContainerUptime outlasts the default whole-task limit (4h) so the
// task budget, not the container reaper, ends a run.
defaultContainerUptime = 5 * time.Hour
defaultVolumeTTL = 72 * time.Hour
defaultPullPolicy = "missing"
defaultContainerImage = "git.catlow.cloud/sam/archie-agent:latest"
// defaultMaxResultChars bounds one tool result.
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.
defaultCaptureRetention = 7 * 24 * time.Hour
defaultCaptureMaxEvents = 5000
defaultCaptureMaxBodyBytes = 256 * 1024
defaultCaptureRatePerSecond = 1.0
defaultCaptureRateBurst = 5
// defaultDenialRetention keeps denial records for 90 days, the same
// horizon the audit-retention seed gives the audit trail
// (docs/prds/orgs-and-access.md, "Audit retention").
defaultDenialRetention = 90 * 24 * time.Hour
// defaultHealthListen is the daemon's own liveness address.
defaultHealthListen = "127.0.0.1:8485"
// defaultHealthDependencyTimeout matches the extracted services'
// -dependency-timeout default, so every process bounds a probe into a
// dependency the same way out of the box.
defaultHealthDependencyTimeout = 5 * time.Second
)
// applyDefaults fills every absent value. It never validates.
func (l *Loader) applyDefaults(cfg *config.Config) {
applyServiceDefaults(cfg)
if cfg.Health.Listen == "" {
cfg.Health.Listen = defaultHealthListen
}
if cfg.Health.DependencyTimeout == 0 {
cfg.Health.DependencyTimeout = config.Duration(defaultHealthDependencyTimeout)
}
if cfg.Telemetry.SampleRatio == 0 {
cfg.Telemetry.SampleRatio = 1
}
l.applyGeneralDefaults(cfg)
applyForgeDefaults(cfg)
applyDispatchDefaults(cfg)
applyIdentityDefaults(cfg)
applyContainerDefaults(cfg)
applyToolPolicyDefaults(cfg)
applyCaptureDefaults(cfg)
applyAccessDefaults(cfg)
applyNATSDefaults(cfg)
applyMemoryDefaults(cfg)
applyCuratorDefaults(cfg)
}
// applyCuratorDefaults resolves an omitted enabled key to on, so the seeded
// resource and the dashboard show the state the runtime applies.
func applyCuratorDefaults(cfg *config.Config) {
for i := range cfg.Curators {
if cfg.Curators[i].Enabled == nil {
on := true
cfg.Curators[i].Enabled = &on
}
}
}
// applyServiceDefaults fills each registered service's absent addresses from
// its registration. An empty registered target stays empty for validation.
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 default capture settings.
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 not defaulted: spilled results must be inside the read tool's
// workspace to be readable.
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 default work directory.
func DefaultWorkDir() string {
return filepath.Join(xdgDataHome(), InstanceName(), "work")
}
// DefaultPostgresSocketDir is where the bundled PostgreSQL's Unix socket is
// mounted on the host.
func DefaultPostgresSocketDir() string {
return filepath.Join(xdgDataHome(), InstanceName(), "postgres")
}
// DefaultConfigDir returns the directory archie's configuration lives in:
// $XDG_CONFIG_HOME/<instance>, or ~/.config/<instance>. It is the base every
// config-home-derived path is built on. It is not the -config source
// directory, which is whichever file or directory the operator names.
func DefaultConfigDir() string {
return filepath.Join(xdgConfigHome(), InstanceName())
}
// DefaultConfigPath returns the config file archie binaries read when
// -config is not given.
func DefaultConfigPath() string {
return filepath.Join(DefaultConfigDir(), "config.toml")
}
// applyGeneralDefaults derives the paths and limits that have no owning
// section.
func (l *Loader) applyGeneralDefaults(cfg *config.Config) {
cfg.SkillsDir = expandHomePath(cfg.SkillsDir)
cfg.WorkDir = expandHomePath(cfg.WorkDir)
cfg.StateDir = expandHomePath(cfg.StateDir)
cfg.Chat.Workspace = expandHomePath(cfg.Chat.Workspace)
// DatabaseURL has no default.
if cfg.WorkDir == "" {
cfg.WorkDir = filepath.Join(l.dataHome(), cfg.Instance, "work")
}
if cfg.StateDir == "" {
cfg.StateDir = filepath.Join(l.dataHome(), cfg.Instance)
}
if cfg.PollInterval == 0 {
cfg.PollInterval = config.Duration(defaultPollInterval)
}
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.Intake == "" {
cfg.Forge.Intake = config.ForgeIntakePoll
}
}
// applyAccessDefaults fills the access surfaces' settings. See
// [config.AccessConfig].
func applyAccessDefaults(cfg *config.Config) {
if cfg.Access.DenialRetention == 0 {
cfg.Access.DenialRetention = config.Duration(defaultDenialRetention)
}
}
// 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 = ""
}
}
// applyIdentityDefaults fills omitted commit emails and copies a sole
// [[identities]] entry into the root single-identity fields.
func applyIdentityDefaults(cfg *config.Config) {
if len(cfg.Identities) > 0 {
for i := range cfg.Identities {
cfg.Identities[i].BotEmail = defaultBotEmail(cfg.Forge.Type, cfg.Identities[i].BotUser, cfg.Identities[i].BotEmail)
}
if len(cfg.Identities) == 1 && cfg.BotUser == "" {
cfg.BotUser = cfg.Identities[0].BotUser
if cfg.BotEmail == "" {
cfg.BotEmail = cfg.Identities[0].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
}
}