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).
This commit is contained in:
sam committed 2026-09-15 10:14:50 +10:00
1 parent e537447333
commit 72d9125bba
5 files changed
+59 -2

No files matched your search

@@ -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
@@ -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) {
+7
View File
@@ -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
+3
View File
@@ -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},
}
}
+8 -2
View File
@@ -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