feat(runtime): configurable per-tool-result truncation cap

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.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
This commit is contained in:
sausheongandClaude Opus 4.7 committed 2026-05-21 07:55:16 +08:00
1 parent 65021e1fe2
commit cbb40e239d
5 files changed
+95 -5

No files matched your search

+4 -4
View File
@@ -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)