From 72d9125bbaf80e5fae3c9e92bfd176f7433260e6 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 15 Sep 2026 10:14:50 +1000 Subject: [PATCH] fix(agentworker): wire configured tool-result policy into worker's LoopRunner TaskConfig now carries the daemon's ToolPolicy (MaxResultChars/SpillDir) via ForTask/ToConfig, and runTask applies it to the constructed LoopRunner. Previously the worker always built the agent loop with a zero-value ToolLimits, so tool results in every task's agent stage were never capped or spilled despite the configured 50k default (agentexec-1, tools-core-11). --- internal/app/agentworker/task_execution.go | 18 +++++++++++++++ .../app/agentworker/task_execution_test.go | 23 +++++++++++++++++++ internal/app/archied/reload.go | 7 ++++++ internal/config/config.go | 3 +++ internal/config/config_test.go | 10 ++++++-- 5 files changed, 59 insertions(+), 2 deletions(-) diff --git a/internal/app/agentworker/task_execution.go b/internal/app/agentworker/task_execution.go index 373f21d65..6f98fa98d 100644 --- a/internal/app/agentworker/task_execution.go +++ b/internal/app/agentworker/task_execution.go @@ -11,6 +11,7 @@ import ( "strconv" "github.com/samcharles93/archie-core/internal/agentexec" + "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/workflow" "github.com/samcharles93/archie-core/internal/domain/workflow/skillbuild" "github.com/samcharles93/archie-core/internal/domain/workflow/wfeval" @@ -138,6 +139,22 @@ func newTaskRunner(providers map[string]agentexec.Provider, log *slog.Logger) ag return agentexec.NewLoopRunner(agentexec.NewRuntime(providers), log) } +// applyToolLimits wires the task's carried tool policy into a *LoopRunner's +// result cap/spill, so a worker-executed stage enforces the same limits the +// daemon's own chat path applies (config.Config.Tools.Policy, carried +// non-secret via TaskConfig.ToolPolicy). A runner that isn't a *LoopRunner +// (e.g. a test fake) is left untouched. +func applyToolLimits(agent agentexec.Runner, policy config.ToolPolicy) { + runner, ok := agent.(*agentexec.LoopRunner) + if !ok { + return + } + runner.Limits = agentexec.ToolLimits{ + MaxResultChars: policy.MaxResultChars, + SpillDir: policy.SpillDir, + } +} + // routeTask applies the request-carried routing bindings and selects the // workflow for a task. The daemon's package-level SetKindWorkflows state // never crosses the process boundary, so runTask installs the resolved @@ -191,6 +208,7 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen if agent == nil { return nil, fmt.Errorf("no agent runner configured for task %d", req.Task.ID) } + applyToolLimits(agent, req.Cfg.ToolPolicy) agent = persistentRunner{Runner: agent, enabled: req.Repo.PersistentStorage} // A workflow run in this process publishes to an in-process *events.Bus diff --git a/internal/app/agentworker/task_execution_test.go b/internal/app/agentworker/task_execution_test.go index b2c792c6b..7a5f6e42a 100644 --- a/internal/app/agentworker/task_execution_test.go +++ b/internal/app/agentworker/task_execution_test.go @@ -195,6 +195,29 @@ func TestHybridTreesPushSurfacesOwnershipReconciliationFailure(t *testing.T) { } } +// TestApplyToolLimitsWiresConfiguredPolicyIntoLoopRunner guards against +// regressing archie-core's tool-result cap and spill dir back to inert +// zero values on the worker path (agentexec-1 / tools-core-11): a +// *LoopRunner built for a task must carry the daemon's configured +// ToolPolicy, not the zero value NewLoopRunner leaves it at. +func TestApplyToolLimitsWiresConfiguredPolicyIntoLoopRunner(t *testing.T) { + runner := agentexec.NewLoopRunner(agentexec.NewRuntime(nil), slog.New(slog.DiscardHandler)) + policy := config.ToolPolicy{MaxResultChars: 12345, SpillDir: "/var/tmp/archie-spill"} + + applyToolLimits(runner, policy) + + want := agentexec.ToolLimits{MaxResultChars: 12345, SpillDir: "/var/tmp/archie-spill"} + if runner.Limits != want { + t.Fatalf("runner.Limits = %+v, want %+v", runner.Limits, want) + } +} + +// TestApplyToolLimitsIgnoresNonLoopRunner confirms a test-fake runner (which +// is not a *agentexec.LoopRunner) is left untouched rather than panicking. +func TestApplyToolLimitsIgnoresNonLoopRunner(t *testing.T) { + applyToolLimits(panicRunner{t}, config.ToolPolicy{MaxResultChars: 1}) +} + type panicRunner struct{ t *testing.T } func (r panicRunner) Run(context.Context, string, agentexec.Request, agentexec.ToolCallReporter) (agentexec.Result, error) { diff --git a/internal/app/archied/reload.go b/internal/app/archied/reload.go index 4ca56d598..377508dcd 100644 --- a/internal/app/archied/reload.go +++ b/internal/app/archied/reload.go @@ -173,6 +173,10 @@ var reloadableFields = map[string]bool{ // [agent] is a decode-only compatibility shim; changes have no runtime // consumer and therefore require neither reload nor restart. "LegacyAgent": true, + // Tools.Policy (MaxResultChars/SpillDir) is carried into TaskConfig by + // ForTask (config.go) and applied fresh per dispatch via + // agentworker.applyToolLimits. See reloadableSubFields["Tools"]. + "ToolPolicy": true, } // reloadableSubFields are sub-fields of structs that are otherwise @@ -188,6 +192,9 @@ var reloadableFields = map[string]bool{ var reloadableSubFields = map[string]map[string]bool{ "Containers": {"LegacyEnabled": true, "VolumeTTL": true}, "Forge": {"Host": true}, + // Policy is carried into TaskConfig by ForTask (config.go); MCPServers, + // WebFetch and Minimax are not and stay requires-restart. + "Tools": {"Policy": true}, } // changedNonReloadableFields compares old and new and returns the diff --git a/internal/config/config.go b/internal/config/config.go index d3638604e..6291fa2bf 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -590,6 +590,7 @@ type TaskConfig struct { DiffCapLines int `json:"diff_cap_lines"` Notify Notify `json:"notify"` Forge TaskForge `json:"forge"` + ToolPolicy ToolPolicy `json:"tool_policy"` } // TaskForge is the non-secret forge configuration needed by workflow stages. @@ -610,6 +611,7 @@ func (c Config) ForTask() TaskConfig { DiffCapLines: c.DiffCapLines, Notify: c.Notify, Forge: TaskForge{Host: c.Forge.Host}, + ToolPolicy: c.Tools.Policy, } } @@ -628,6 +630,7 @@ func (tc TaskConfig) ToConfig() Config { DiffCapLines: tc.DiffCapLines, Notify: tc.Notify, Forge: Forge{Host: tc.Forge.Host}, + Tools: ToolsConfig{Policy: tc.ToolPolicy}, } } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 9d59fc9f8..65fee5bcf 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -20,9 +20,13 @@ func TestTaskConfigToConfigRoundTrip(t *testing.T) { Dispatch: Dispatch{Trigger: "label", AckReaction: "eyes", Labels: map[string]string{"working": "bot:working"}}, Notify: Notify{Webhook: "https://notify.example.test/hook"}, Forge: Forge{Type: "github", Host: "https://forge.example.test", Token: SecretRef{Engine: "env", Key: "TOP_SECRET"}}, + Tools: ToolsConfig{Policy: ToolPolicy{MaxResultChars: 50_000, SpillDir: "/var/tmp/archie-spill"}}, } got := cfg.ForTask().ToConfig() + if got.Tools.Policy != cfg.Tools.Policy { + t.Fatalf("Tools.Policy = %#v, want %#v", got.Tools.Policy, cfg.Tools.Policy) + } if !reflect.DeepEqual(got.Models, cfg.Models) { t.Fatalf("Models = %#v, want %#v", got.Models, cfg.Models) } @@ -110,7 +114,8 @@ func TestConfigForTaskJSONRoundTrip(t *testing.T) { Providers: map[string]Provider{ "secret": {APIKeyEnv: "TOP_SECRET_PROVIDER_TOKEN"}, }, - NATS: NATSConfig{TokenEnv: "TOP_SECRET_NATS_TOKEN"}, + NATS: NATSConfig{TokenEnv: "TOP_SECRET_NATS_TOKEN"}, + Tools: ToolsConfig{Policy: ToolPolicy{MaxResultChars: 50_000, SpillDir: "/var/tmp/archie-spill"}}, } want := TaskConfig{ @@ -122,6 +127,7 @@ func TestConfigForTaskJSONRoundTrip(t *testing.T) { DiffCapLines: cfg.DiffCapLines, Notify: cfg.Notify, Forge: TaskForge{Host: cfg.Forge.Host}, + ToolPolicy: cfg.Tools.Policy, } got := cfg.ForTask() if !reflect.DeepEqual(got, want) { @@ -148,7 +154,7 @@ func TestConfigForTaskJSONRoundTrip(t *testing.T) { if err != nil { t.Fatal(err) } - assertJSONKeys(t, data, "bot_user", "bot_email", "models", "budgets", "dispatch", "diff_cap_lines", "notify", "forge") + assertJSONKeys(t, data, "bot_user", "bot_email", "models", "budgets", "dispatch", "diff_cap_lines", "notify", "forge", "tool_policy") var forgePayload map[string]json.RawMessage var payload map[string]json.RawMessage