#1061·Yuxi

错误: 代理上下文中缺少跨 query_kb 调用的 chunk 去重,重复调用导致 ToolMessage 继续膨胀

作者: 774098632创建于 2026年9月22日更新于 2026年9月22日
标签feat

1. Problem Description When the Agent performs multiple near-identical query_kb queries for the same user question, the same knowledge fragment may be retrieved multiple times. Currently, each search result is appended to the thread state as an independent ToolMessage, without evidence aggregation or aggregation at the Run/Conversation level based on (kb_id, chunk_id). The problem is that the Agent's context layer does not recognize that the evidence has already appeared, resulting in the same body being sent repeatedly to subsequent model calls. In production, read-only observations: - Query A returns 10 results, including a fragment from document A; - Near-identical query B returns the same fragment from document A; - The title and body are completely identical after two expansions, with only the relevance score differing (50.2%/50.6%); - There are multiple duplicate documents between the two groups of near-identical queries; - A single round of Q&A results in a total of 39 tool messages, with a total of approximately 56.4K tokens; - The thread has made 11 model calls in total, with a total of approximately 547K tokens, and the most recent model call has approximately 126K tokens; - The end-to-end time is approximately 2 minutes and 20 seconds. Related call chains: - backend/package/yuxi/agents/toolkits/kbs/tools.py::query_kb: Each call to retrieve is independent, without maintaining a set of seen chunks; - backend/package/yuxi/knowledge/manager.py::KnowledgeBaseManager.retrieve: Performs a single search and constructs the output directly; - backend/package/yuxi/knowledge/base.py::build_search_output: Serializes all results for this time, without merging with historical ToolMessages; - The Filesystem/Summary middleware only unloads or compresses large results, without performing cross-call evidence deduplication.