Heuristic:3641397194 wq Gpt6 Astra Observation Truncation
| Knowledge Sources | |
|---|---|
| Domains | AI_Agents, Optimization |
| Last Updated | 2026-10-26 14:00 GMT |
Overview
Truncate agent observation history to 12000 characters per tool result to prevent context window overflow in the ReAct loop.
Description
In the agent tool loop (`runAgent`), each tool execution result is stringified via `JSON.stringify(output)` and pushed into the observation `history` array. Before pushing, the stringified result is sliced to 12000 characters, and a `truncated: true` flag is set if the original exceeded that length. This prevents a single large tool output (e.g., a full HTTP crawl or binary disassembly) from consuming the entire context window of the AI model, which would crowd out the goal, constraints, and prior reasoning steps.
Usage
Use this heuristic when running multi-round AI agent loops that accumulate tool outputs. It is especially important when tools may return large responses (web crawls, binary analysis, file reads). Without truncation, a single verbose tool result can exhaust the model's context window, causing the agent to lose track of the goal or fail to include prior observations.
The Insight (Rule of Thumb)
- Action: Set `history.push({ ...result: JSON.stringify(output).slice(0, 12000), truncated: JSON.stringify(output).length > 12000 })`
- Value: 12000 characters per observation entry
- Trade-off: Large tool outputs lose detail beyond 12000 chars; the agent may need to re-query for full content if truncation loses critical information
- Compatibility: Applies to all tools uniformly; no per-tool override
Reasoning
The ReAct loop sends the full observation history as context to the AI model each round: `context: JSON.stringify({ target, idaUrl, tools, observations: history })`. If each observation can be arbitrarily large, a 16-round loop with 1MB tool outputs would produce a 16MB context string — far exceeding any model's context window. The 12000-char cap bounds the worst-case history to ~192KB (16 × 12000), leaving room for the goal, constraints, and tool catalog. The `truncated` flag signals to downstream logic that the observation is partial, though the model itself is not explicitly told about truncation.
Code Evidence
Observation truncation from `agent.js:45`:
history.push({ tool: next.tool, arguments: args, result: JSON.stringify(output).slice(0, 12000), truncated: JSON.stringify(output).length > 12000 });
Context construction from `agent.js:35`:
const result = await cancellable(adapter.submit({ modelId: input.modelId, goal: String(input.goal || `分析 ${input.target} 并输出实际结果`), context: JSON.stringify({ target: input.target, idaUrl: input.idaUrl || null, tools: engine.catalog().tools, observations: history }), constraints, outputFormat: 'json', stream: false }), ctx.signal);