Skip to content

fix(core): prevent duplicate citation prompt in agent hooks#8416

Open
ZhaiXB wants to merge 1 commit into
AstrBotDevs:masterfrom
ZhaiXB:fix/duplicate-citation-prompt
Open

fix(core): prevent duplicate citation prompt in agent hooks#8416
ZhaiXB wants to merge 1 commit into
AstrBotDevs:masterfrom
ZhaiXB:fix/duplicate-citation-prompt

Conversation

@ZhaiXB
Copy link
Copy Markdown
Contributor

@ZhaiXB ZhaiXB commented May 29, 2026

修复了在工具(如网页搜索)执行结束时,搜索引用提示词被重复追加到系统提示词(System Prompt)结尾的问题。#8414

问题根源:在 MainAgentHooks.on_tool_end 中,代码通过 += 直接修改了常驻内存的 run_context.messages[0].content 对象,且缺乏去重(幂等性)判断。这导致在单次对话中,每当一个搜索工具调用结束,这段引用提示词就会被无条件追加一次。如果模型单轮触发了多次工具调用(例如复杂的天气或资讯查询),System Prompt 结尾就会出现严重的文本堆叠和重复。

Modifications / 改动点

  • 修改文件:astrbot/core/agent/hooks.py (或相关 Agent Hooks 核心文件)

  • MainAgentHooks.on_tool_end 的提示词注入逻辑中,将原本直接 += 的操作改为先进行字符串包含判断(if citation_prompt not in first_part.content:)。

  • 确保了无论单次交互中调用多少次搜索工具,该段引用提示词在系统上下文中永远只保留一份。

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

image 1780057977909_d

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Bug Fixes:

  • Prevent duplicate web search citation prompts from being repeatedly appended to the system message after multiple tool invocations in a single interaction.

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels May 29, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies astrbot/core/astr_agent_hooks.py to ensure that the citation prompt is only appended to the system prompt if it is not already present. The reviewer suggests adding a newline separator (\n\n) before appending the citation prompt to ensure proper formatting and prevent it from merging directly with the existing prompt text.

Comment on lines +103 to +104
if citation_prompt not in first_part.content:
first_part.content += citation_prompt
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

直接将 citation_prompt 拼接在 first_part.content 后面可能会导致提示词与原系统提示词内容紧贴在一起(例如 ...assistant.Always cite...),缺乏适当的分隔(如换行符),这会影响 Prompt 的结构和 LLM 的理解效果。

建议在拼接前使用 .rstrip() 清理末尾空白,并统一添加 \n\n 作为分隔符,以确保 Prompt 格式整洁。

Suggested change
if citation_prompt not in first_part.content:
first_part.content += citation_prompt
if citation_prompt not in first_part.content:
first_part.content = first_part.content.rstrip() + "\n\n" + citation_prompt

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Consider extracting the citation_prompt string into a shared constant (e.g., module-level) so it isn’t reallocated and can be reused consistently across hooks or modules that might need the same text.
  • The idempotency check if citation_prompt not in first_part.content could be made more precise (e.g., endswith or checking for a sentinel marker) to avoid skipping injection in cases where similar text appears earlier in the system prompt for other reasons.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider extracting the `citation_prompt` string into a shared constant (e.g., module-level) so it isn’t reallocated and can be reused consistently across hooks or modules that might need the same text.
- The idempotency check `if citation_prompt not in first_part.content` could be made more precise (e.g., `endswith` or checking for a sentinel marker) to avoid skipping injection in cases where similar text appears earlier in the system prompt for other reasons.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant