From 2d557bb7f5d3de7e0308e3f7303e958c9b024f7a Mon Sep 17 00:00:00 2001 From: sausheong Date: Thu, 21 May 2026 07:55:16 +0800 Subject: [PATCH] feat(runtime): configurable per-tool-result truncation cap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds LoopConfig.MaxToolResultLen with the same config > env > default precedence as the existing MaxToolConcurrency / MaxAgentDepth knobs: 1. Runtime.AgentLoop.MaxToolResultLen (>0) — config wins 2. HARNESS_MAX_TOOL_RESULT_LEN env (>0) — env fallback 3. defaultMaxToolResultLen (4000) — unchanged for back-compat The previous 4000-char cap was conservative for agents doing real engineering work (read_file, web_fetch, browser, bash output). Setting MaxToolResultLen to 16000-25000 lets a typical source file or test run land in-context without hitting spillover; spill remains the safety net for genuinely large outputs. No behaviour change for callers that don't set the field. --- runtime/cap.go | 27 ++++++++++++++++++++++++ runtime/cap_test.go | 50 +++++++++++++++++++++++++++++++++++++++++++++ runtime/context.go | 7 ++++++- runtime/runtime.go | 8 ++++---- runtime/types.go | 8 ++++++++ 5 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 runtime/cap.go create mode 100644 runtime/cap_test.go diff --git a/runtime/cap.go b/runtime/cap.go new file mode 100644 index 0000000..489cd6d --- /dev/null +++ b/runtime/cap.go @@ -0,0 +1,27 @@ +package runtime + +import ( + "os" + "strconv" +) + +// maxToolResultLen returns the per-result truncation cap (chars) for tool +// results in the in-context message history. Precedence: +// 1. Runtime.AgentLoop.MaxToolResultLen (>0) — config wins. +// 2. HARNESS_MAX_TOOL_RESULT_LEN env var (>0) — env fallback. +// 3. defaultMaxToolResultLen (4000). +// +// Tool results longer than this are truncated (or spilled to disk via +// spillConfig when supplied). 4000 is conservative; engineering-style +// agents that read source files and run tests typically want 16000-25000. +func (r *Runtime) maxToolResultLen() int { + if r.AgentLoop.MaxToolResultLen > 0 { + return r.AgentLoop.MaxToolResultLen + } + if v := os.Getenv("HARNESS_MAX_TOOL_RESULT_LEN"); v != "" { + if n, err := strconv.Atoi(v); err == nil && n > 0 { + return n + } + } + return defaultMaxToolResultLen +} diff --git a/runtime/cap_test.go b/runtime/cap_test.go new file mode 100644 index 0000000..42543f3 --- /dev/null +++ b/runtime/cap_test.go @@ -0,0 +1,50 @@ +package runtime + +import ( + "testing" +) + +// TestMaxToolResultLen_Precedence locks in the config > env > default +// precedence so a regression here doesn't silently swap one source for +// another. +func TestMaxToolResultLen_Precedence(t *testing.T) { + t.Run("default when nothing set", func(t *testing.T) { + t.Setenv("HARNESS_MAX_TOOL_RESULT_LEN", "") + r := &Runtime{} + if got := r.maxToolResultLen(); got != defaultMaxToolResultLen { + t.Fatalf("want %d, got %d", defaultMaxToolResultLen, got) + } + }) + + t.Run("env overrides default", func(t *testing.T) { + t.Setenv("HARNESS_MAX_TOOL_RESULT_LEN", "12345") + r := &Runtime{} + if got := r.maxToolResultLen(); got != 12345 { + t.Fatalf("want 12345, got %d", got) + } + }) + + t.Run("config overrides env", func(t *testing.T) { + t.Setenv("HARNESS_MAX_TOOL_RESULT_LEN", "12345") + r := &Runtime{AgentLoop: LoopConfig{MaxToolResultLen: 25000}} + if got := r.maxToolResultLen(); got != 25000 { + t.Fatalf("want 25000, got %d", got) + } + }) + + t.Run("zero env is ignored", func(t *testing.T) { + t.Setenv("HARNESS_MAX_TOOL_RESULT_LEN", "0") + r := &Runtime{} + if got := r.maxToolResultLen(); got != defaultMaxToolResultLen { + t.Fatalf("want %d, got %d", defaultMaxToolResultLen, got) + } + }) + + t.Run("garbage env is ignored", func(t *testing.T) { + t.Setenv("HARNESS_MAX_TOOL_RESULT_LEN", "not-a-number") + r := &Runtime{} + if got := r.maxToolResultLen(); got != defaultMaxToolResultLen { + t.Fatalf("want %d, got %d", defaultMaxToolResultLen, got) + } + }) +} diff --git a/runtime/context.go b/runtime/context.go index 403d83b..d620402 100644 --- a/runtime/context.go +++ b/runtime/context.go @@ -13,7 +13,12 @@ import ( "github.com/sausheong/harness/session" ) -const maxToolResultLen = 4000 // truncate tool results longer than this +// defaultMaxToolResultLen is the fallback cap used when LoopConfig.MaxToolResultLen +// is unset and the HARNESS_MAX_TOOL_RESULT_LEN env var is unset/invalid. Tool +// results longer than this get truncated (or spilled to disk + truncated-with- +// marker when spillConfig is supplied). 4000 chars (~1000 tokens) is conservative +// so behaviour is unchanged for callers that don't opt into a larger budget. +const defaultMaxToolResultLen = 4000 // detectImageMIME returns the actual MIME type based on magic bytes. // Falls back to the provided hint if the format is unrecognized. diff --git a/runtime/runtime.go b/runtime/runtime.go index b18c8ba..19d8197 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -364,7 +364,7 @@ func (r *Runtime) Run(ctx context.Context, userMsg string, images []llm.ImageCon msgs := assembleMessages(history) spillCfg := spillConfig{Workspace: r.Workspace, SessionKey: r.Session.Key} - pruneToolResults(msgs, maxToolResultLen, spillCfg) + pruneToolResults(msgs, r.maxToolResultLen(), spillCfg) toolDefs := r.Tools.ToolDefs() if r.Permission != nil { @@ -390,7 +390,7 @@ func (r *Runtime) Run(ctx context.Context, userMsg string, images []llm.ImageCon r.emit(AgentEvent{Type: EventCompactionDone, Compaction: &res}) history = r.Session.View() msgs = assembleMessages(history) - pruneToolResults(msgs, maxToolResultLen, spillCfg) + pruneToolResults(msgs, r.maxToolResultLen(), spillCfg) msgs = prependPostCompactRestore(msgs, r.snapshotTouchedFiles()) } } @@ -416,7 +416,7 @@ func (r *Runtime) Run(ctx context.Context, userMsg string, images []llm.ImageCon r.emit(AgentEvent{Type: EventCompactionDone, Compaction: &res}) history = r.Session.View() msgs = assembleMessages(history) - pruneToolResults(msgs, maxToolResultLen, spillCfg) + pruneToolResults(msgs, r.maxToolResultLen(), spillCfg) msgs = prependPostCompactRestore(msgs, r.snapshotTouchedFiles()) } else { r.emit(AgentEvent{Type: EventCompactionSkipped, Compaction: &res}) @@ -449,7 +449,7 @@ func (r *Runtime) Run(ctx context.Context, userMsg string, images []llm.ImageCon r.emit(AgentEvent{Type: EventCompactionDone, Compaction: &res}) history = r.Session.View() msgs = assembleMessages(history) - pruneToolResults(msgs, maxToolResultLen, spillCfg) + pruneToolResults(msgs, r.maxToolResultLen(), spillCfg) msgs = prependPostCompactRestore(msgs, r.snapshotTouchedFiles()) req.Messages = msgs stream, err = r.LLM.ChatStream(ctx, req) diff --git a/runtime/types.go b/runtime/types.go index 10c997f..9d350b4 100644 --- a/runtime/types.go +++ b/runtime/types.go @@ -71,6 +71,14 @@ type LoopConfig struct { // StreamingTools enables mid-stream concurrency-safe tool kickoff. // false ⇒ env fallback (HARNESS_STREAMING_TOOLS=1) then off. StreamingTools bool + // MaxToolResultLen caps the in-context length of any single tool + // result; longer results are truncated to their first MaxToolResultLen + // chars (cut at a newline boundary) with the remainder either spilled + // to disk via spillConfig or simply dropped. 0 ⇒ env fallback + // (HARNESS_MAX_TOOL_RESULT_LEN) then default 4000. Common pick for + // engineering-style agents (reading source files, running tests) is + // 16000-25000. + MaxToolResultLen int // Hooks are optional callbacks the runtime fires at well-known // points in the loop. Zero value (all nil fields) disables every // hook with no overhead.