[Bug](bot): openviking_memory_commit 作为副作用工具时,其结果被喂回 LLM 反思上下文并污染最终回复
Bug Description
在 vikingbot 的 agent loop 中,openviking_memory_commit 是一个用于保存记忆的副作用工具,但当前它会被当成普通信息工具处理:工具执行结果会被追加到 LLM messages 中,并触发下一轮 Reflect on the results and decide next steps.。
如果 LLM 在同一条 assistant message 中同时返回:
- 非空
content,且该 content 已经是面向用户的答案; tool_calls,其中唯一工具为openviking_memory_commit;
当前 loop 会按“工具调用轮”处理该 message:先执行 openviking_memory_commit,把 commit 的状态结果追加到 messages,再追加固定 user 消息 Reflect on the results and decide next steps.,然后让 LLM 继续下一轮。
这会导致下一轮 LLM 容易基于 memory commit 的工具结果生成类似“已提交到 OpenViking memory,无需后续动作”的回复。最终返回给用户/评测脚本的是 commit 状态总结,而不是上一轮 assistant message 中已经生成好的真实答案。
这类问题在 repo-QA 评测中会造成严重误判:模型事实上已经给出了正确答案,但最终输出被 memory commit 状态覆盖,导致得分显著下降。
更根本的问题是:agent loop 没有区分“信息型工具”和“副作用工具”。openviking_search / openviking_multi_read 的结果需要喂给 LLM 继续生成答案,而 openviking_memory_commit 的执行结果通常只应被系统记录,不应成为下一轮 LLM 推理上下文。
Steps to Reproduce
可以使用下面的最小脚本复现该控制流问题(不调用真实 LLM、不调用真实 OpenViking server、不写入 memory):
import json
QUESTION = "repo-QA question"
USEFUL_ANSWER = "### Implementation Location\n这里已经是用户需要的答案..."
COMMIT_RESULT = '{"status": "success", "task_status": "running"}'
BAD_FINAL = (
"The query is fully resolved, and the information has been successfully "
"committed to OpenViking memory for future reference. No further actions are needed."
)
def fake_model(messages):
# 第 1 轮:模型同时生成用户答案和 openviking_memory_commit tool call
if len(messages) == 1:
return {
"content": USEFUL_ANSWER,
"tool_calls": [
{
"id": "call_memory_commit_1",
"name": "openviking_memory_commit",
"arguments": {
"messages": [
{"role": "user", "content": QUESTION},
{"role": "assistant", "content": USEFUL_ANSWER},
]
},
}
],
}
# 第 2 轮:模型看到 commit success + Reflect 后,返回 commit 状态总结
return {"content": BAD_FINAL, "tool_calls": []}
messages = [{"role": "user", "content": QUESTION}]
while True:
response = fake_model(messages)
if response["tool_calls"]:
messages.append(
{
"role": "assistant",
"content": response["content"],
"tool_calls": response["tool_calls"],
}
)
messages.append(
{
"role": "tool",
"tool_call_id": "call_memory_commit_1",
"name": "openviking_memory_commit",
"content": COMMIT_RESULT,
}
)
messages.append(
{"role": "user", "content": "Reflect on the results and decide next steps."}
)
continue
final_content = response["content"]
break
print("useful_answer:", USEFUL_ANSWER)
print("final_content:", final_content)
print("badcase_reproduced:", final_content == BAD_FINAL)
输出示例:
BADCASE_REPRODUCED: True
useful_answer: ### Implementation Location
这里已经是用户需要的答案...
final_content:
The query is fully resolved, and the information has been successfully committed to OpenViking memory for future reference. No further actions are needed.
badcase_reproduced: True
复现 trace 中的关键消息序列:
[
{
"role": "assistant",
"content": "### Implementation Location\nThe lower-level validation and encoding logic ...",
"tool_calls": [
{
"function": {
"name": "openviking_memory_commit"
}
}
]
},
{
"role": "tool",
"name": "openviking_memory_commit",
"content": "{ \"status\": \"success\", \"task_status\": \"running\", ... }"
},
{
"role": "user",
"content": "Reflect on the results and decide next steps."
},
{
"role": "assistant",
"content": "The query is fully resolved, and the information has been successfully committed to OpenViking memory for future reference. No further actions are needed."
}
]
"answer": "The query is fully resolved, and the information has been successfully committed to your OpenViking memory for future reference. If you need further details (like specific test cases for this \nscenario, or how to patch the behavior if needed), feel free to ask."
真实评测中也观察到同类 badcase:LLM 已经在带 openviking_memory_commit 的 assistant message 中生成了有效答案,但最终答案变成 memory commit 状态说明。
Expected Behavior
openviking_memory_commit 这类副作用工具的执行结果不应作为普通 tool result 喂回 LLM,并且不应单独触发下一轮 Reflect on the results and decide next steps.。
更合理的行为是按工具类型区分:
- 信息型工具,例如
openviking_search/openviking_multi_read:执行结果继续追加到 LLM messages,触发后续反思/回答; - 副作用工具,例如
openviking_memory_commit:可以执行并记录到 trace / tools_used / logs,但默认不把工具状态结果追加到 LLM messages; - 如果本轮只有
openviking_memory_commit,且 assistantcontent非空,可以返回该content,或按产品语义返回固定确认文案; - 如果本轮同时包含
openviking_memory_commit和其他信息型工具,应只把信息型工具结果喂给 LLM,openviking_memory_commit的结果不进入 LLM 上下文。
注意:不能简单假设 “assistant content + memory commit” 中的 content 一定是最终答案。它也可能只是中间状态说明、阶段性摘要,或者和其他工具调用混在一起。因此修复重点不应是判断 content 是否最终答案,而是避免 memory commit 的内部状态污染 LLM 上下文。
Actual Behavior
当前 _run_agent_loop 的行为是:
- 只要
response.has_tool_calls为 true,就将该 assistant message 视为中间工具调用轮; - 即使
response.content已经是完整答案,也不会设置为final_content; - 执行
openviking_memory_commit后,把 commit status 作为 tool result 追加到 messages; - 执行完工具后追加:
messages.append(
{"role": "user", "content": "Reflect on the results and decide next steps."}
)
- 下一轮 LLM 看到最近上下文是 memory commit 的 success JSON 和 “Reflect...” 指令,容易生成 commit 状态总结;
- 该状态总结由于没有 tool call,被作为最终
final_content返回。
相关代码路径:
bot/vikingbot/agent/loop.py_run_agent_loopresponse.has_tool_calls分支中执行工具后统一追加Reflect on the results and decide next steps.
Minimal Reproducible Example
# 简化后的控制流示例
messages = [{"role": "user", "content": "repo-QA question"}]
# 第 1 轮:LLM 同时返回 content 和 memory commit tool call。
# 这里 content 在真实 badcase 中是最终答案,但在其他场景中也可能只是中间说明。
response = {
"content": "### Implementation Location\n这里已经是用户需要的答案...",
"tool_calls": [
{
"name": "openviking_memory_commit",
"arguments": {
"messages": [
{"role": "user", "content": "repo-QA question"},
{"role": "assistant", "content": "这里已经是用户需要的答案..."}
]
},
}
],
}
# 当前 loop:因为有 tool_calls,不会把 response["content"] 作为 final_content
messages.append({
"role": "assistant",
"content": response["content"],
"tool_calls": response["tool_calls"],
})
# 执行 memory commit,并把副作用工具的状态结果喂回 LLM messages
messages.append({
"role": "tool",
"name": "openviking_memory_commit",
"content": '{"status": "success", "task_status": "running"}',
})
# loop 继续反思。问题在于:memory commit 的内部状态不应该驱动下一轮回答。
messages.append({"role": "user", "content": "Reflect on the results and decide next steps."})
# 第 2 轮:LLM 输出 commit 状态总结,并被作为最终答案
final_content = "The query is fully resolved and committed to OpenViking memory..."
## Error Logs
无 Python exception。该问题表现为逻辑层面的最终答案污染。
该问题可以用以下指标量化:
commit_tool_call_rate =
含 openviking_memory_commit 的 trace 数 / 总 trace 数
same_turn_answer_and_commit_rate =
assistant message 同时有非空 content 和 openviking_memory_commit tool_call 的 trace 数 / 总 trace 数
final_commit_pollution_rate =
最终答案类似 “committed to memory / no further actions needed” 的 trace 数 / 总 trace 数
Error Logs
OpenViking Version
0.4.8
Python Version
3.14.3
Operating System
macOS
Model Backend
Volcengine (Doubao)
Additional Context
触发条件不是代码中的硬阈值,而是 LLM tool selection 的条件概率行为:
openviking_memory_commit暴露在 tool schema 中;- system prompt 中有
Remember important facts: using openviking_memory_commit tool to commit; - LLM 在完成 QA 答案后认为该问答值得保存,于是在同一轮同时生成
content和openviking_memory_committool call。
这不是 openviking_memory_commit 执行失败,而是副作用工具被当成信息型工具参与了最终回答路径,导致工具状态覆盖用户答案。
建议的最小修复方向:
SIDE_EFFECT_TOOLS = {"openviking_memory_commit"}
llm_visible_tool_results = []
side_effect_tool_results = []
for tool_call in response.tool_calls:
result = execute_tool(tool_call)
if tool_call.name in SIDE_EFFECT_TOOLS:
# 仍然记录 tools_used / trace / logs,但不要把结果加入 LLM messages。
side_effect_tool_results.append((tool_call, result))
continue
# 信息型工具仍按原逻辑进入 LLM 上下文。
messages = self.context.add_tool_result(messages, tool_call, result)
llm_visible_tool_results.append((tool_call, result))
if llm_visible_tool_results:
messages.append({"role": "user", "content": "Reflect on the results and decide next steps."})
continue
# 本轮只有副作用工具,没有任何信息型工具结果需要 LLM 继续处理。
if isinstance(response.content, str) and response.content.strip():
final_content = response.content
final_reasoning_content = response.reasoning_content
break
对于QA-eval 场景,可以通过手动禁用 openviking_memory_commit 规避,但是通用 bot 场景仍建议在 agent loop 或工具暴露策略上修复。
已明确该诉求,但更倾向于模型能力导致的上下文理解错误,影响了评测结果。 commit tool结果不进入上下文可暂时解决该问题,但更倾向于优化tool result,降低result对上下文的污染。我们将进行优化