refactor: pre-cleanup.

This commit is contained in:
sam committed 2026-08-06 01:40:15 +10:00
1 parent 0ceab9d897
commit e10cbbc92c
53 files changed
+3596 -648

No files matched your search

@@ -8,6 +8,7 @@ import (
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/secret"
"github.com/samcharles93/archie-core/internal/tools"
)
// Defaults applied to absent input. Named so a reader can find the value
@@ -25,6 +26,19 @@ const (
defaultContainerUptime = 60 * time.Minute
defaultVolumeTTL = 72 * time.Hour
defaultPullPolicy = "missing"
// 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
)
// applyDefaults fills in every absent value. It never reports an error and
@@ -43,6 +57,43 @@ func (l *Loader) applyDefaults(cfg *config.Config) {
applyDispatchDefaults(cfg)
applyIdentityDefaults(cfg)
applyContainerDefaults(cfg)
applyToolPolicyDefaults(cfg)
}
// 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
}
if cfg.Tools.Policy.TurnBudgetChars == 0 {
cfg.Tools.Policy.TurnBudgetChars = tools.DefaultTurnBudgetChars
}
// 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
@@ -151,6 +151,143 @@ func TestLoadAgentDefaultsAndOverrides(t *testing.T) {
}
}
// TestLoadToolPolicyDefaultsAndOverrides covers the result-size and turn-budget
// limits end to end from TOML. Before these fields carried toml tags they could
// not be set from the daemon's config at all, so a test that only exercised
// defaulting would have passed against a setting no operator could reach.
//
// A negative value means "disabled" rather than zero: defaulting cannot tell an
// explicit 0 from an absent key, so zero has to stay available as "unset".
func TestLoadToolPolicyDefaultsAndOverrides(t *testing.T) {
tests := []struct {
name string
policy string
wantMaxResult int
wantTurnBudget int
wantSpillDir string
}{
{
// SpillDir has no default on purpose: a path outside
// chat.workspace is one the read tool refuses, so the model would
// be handed a reference it cannot open. Absent means truncate
// inline.
name: "defaults",
wantMaxResult: 50_000,
wantTurnBudget: 200_000,
wantSpillDir: "",
},
{
name: "explicit values",
policy: "[tools.tool_policy]\nmax_result_chars = 1234\nturn_budget_chars = 5678\nspill_dir = \"/var/spool/archie\"\n",
wantMaxResult: 1234,
wantTurnBudget: 5678,
wantSpillDir: "/var/spool/archie",
},
{
name: "negative disables both caps",
policy: "[tools.tool_policy]\nmax_result_chars = -1\nturn_budget_chars = -1\n",
wantMaxResult: -1,
wantTurnBudget: -1,
wantSpillDir: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
contents := "bot_user = \"widget\"\n" + tt.policy +
"\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n"
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
t.Fatal(err)
}
cfg, err := loadFile(path)
if err != nil {
t.Fatal(err)
}
policy := cfg.Tools.Policy
if policy.MaxResultChars != tt.wantMaxResult {
t.Errorf("MaxResultChars = %d, want %d", policy.MaxResultChars, tt.wantMaxResult)
}
if policy.TurnBudgetChars != tt.wantTurnBudget {
t.Errorf("TurnBudgetChars = %d, want %d", policy.TurnBudgetChars, tt.wantTurnBudget)
}
if policy.SpillDir != tt.wantSpillDir {
t.Errorf("SpillDir = %q, want %q", policy.SpillDir, tt.wantSpillDir)
}
})
}
}
// TestLoadWebFetchDefaultsAndOverrides covers the fetch tool's settings.
//
// The enabled case matters most: it is a pointer precisely so an operator can
// turn the tool off, which a plain bool could not express because defaulting
// cannot tell `enabled = false` from an absent key.
func TestLoadWebFetchDefaultsAndOverrides(t *testing.T) {
tests := []struct {
name string
webFetch string
wantEnabled bool
wantTimeout time.Duration
wantMaxBytes int64
wantPrivate bool
}{
{
name: "defaults",
wantEnabled: true,
wantTimeout: 30 * time.Second,
wantMaxBytes: 2_000_000,
},
{
name: "explicitly disabled",
webFetch: "[tools.web_fetch]\nenabled = false\n",
wantEnabled: false,
wantTimeout: 30 * time.Second,
wantMaxBytes: 2_000_000,
},
{
name: "overrides",
webFetch: "[tools.web_fetch]\ntimeout = \"5s\"\nmax_bytes = 4096\nallow_private_networks = true\n",
wantEnabled: true,
wantTimeout: 5 * time.Second,
wantMaxBytes: 4096,
wantPrivate: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
contents := "bot_user = \"widget\"\n" + tt.webFetch +
"\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n"
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
t.Fatal(err)
}
cfg, err := loadFile(path)
if err != nil {
t.Fatal(err)
}
wf := cfg.Tools.WebFetch
if got := wf.IsEnabled(); got != tt.wantEnabled {
t.Errorf("IsEnabled() = %v, want %v", got, tt.wantEnabled)
}
if got := wf.Timeout.Std(); got != tt.wantTimeout {
t.Errorf("Timeout = %s, want %s", got, tt.wantTimeout)
}
if wf.MaxBytes != tt.wantMaxBytes {
t.Errorf("MaxBytes = %d, want %d", wf.MaxBytes, tt.wantMaxBytes)
}
if wf.AllowPrivateNetworks != tt.wantPrivate {
t.Errorf("AllowPrivateNetworks = %v, want %v", wf.AllowPrivateNetworks, tt.wantPrivate)
}
})
}
}
func TestLoadContainerVolumeTTL(t *testing.T) {
tests := []struct {
name string