Skip to content

feat: 添加失败时保存本轮记录的配置项#7663

Closed
Blueteemo wants to merge 3 commits intoAstrBotDevs:masterfrom
Blueteemo:fix/issue-7620-save-failed-agent-history-clean
Closed

feat: 添加失败时保存本轮记录的配置项#7663
Blueteemo wants to merge 3 commits intoAstrBotDevs:masterfrom
Blueteemo:fix/issue-7620-save-failed-agent-history-clean

Conversation

@Blueteemo
Copy link
Copy Markdown
Contributor

@Blueteemo Blueteemo commented Apr 19, 2026

问题描述

目前 Agent 运行失败时(例如 EmptyModelOutputError),本轮记录通常不会写入正式会话历史。这在排查问题和长流程任务失败后希望从断点继续的场景下带来不便。

修复方案

添加 save_failed_agent_history 配置项(默认 false),启用后即使 Agent 本轮运行失败,也会将本轮记录保存到会话历史中,包括用户输入、工具调用记录和失败提示。

修改内容

  1. internal.py:读取配置并传递给 _save_to_history 方法
  2. _save_to_history 方法:添加 save_failed_history 参数控制空响应时是否保存
  3. default.py:添加配置项定义和 UI 元数据

Related Issue

Fixes #7620

Summary by Sourcery

Add configurable support for saving the current agent run to conversation history even when the run fails.

New Features:

  • Introduce a save_failed_agent_history configuration flag to control whether failed agent runs are persisted to conversation history.

Enhancements:

  • Wire the save_failed_agent_history flag through the agent internal pipeline so that empty or failed model responses can still be saved when enabled.
  • Extend chat provider template metadata to expose the new flag in the UI with appropriate description and conditions.

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels Apr 19, 2026
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:

  • In _save_to_history, when save_failed_history is true and the response is otherwise empty, consider adding an explicit debug/info log (e.g., including run status or error reason) to make it clear that a failed/empty run is being intentionally persisted.
  • The UI config for provider_settings.save_failed_agent_history is currently conditioned on agent_runner_type == 'local'; if other runner types also support this behavior, you may want to relax or extend this condition to avoid the option silently not appearing for them.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `_save_to_history`, when `save_failed_history` is true and the response is otherwise empty, consider adding an explicit debug/info log (e.g., including run status or error reason) to make it clear that a failed/empty run is being intentionally persisted.
- The UI config for `provider_settings.save_failed_agent_history` is currently conditioned on `agent_runner_type == 'local'`; if other runner types also support this behavior, you may want to relax or extend this condition to avoid the option silently not appearing for them.

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.

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 introduces a new configuration option, save_failed_agent_history, which allows the system to save session history even when an agent fails or returns an empty response. The changes include updates to the default configuration, schema definitions, and the internal agent processing logic. However, the current implementation has two significant issues: the new parameter is not passed to _save_to_history in non-Live mode, and existing early-return logic within _save_to_history prevents the function from reaching the new logic when the LLM response is null or the message role is not 'assistant'.

not llm_response.completion_text
and not req.tool_calls_result
and not user_aborted
and not save_failed_history
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

是否会导致下一轮的 messages 格式有误?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

感谢您的review!您担心的问题确实存在。

当 save_failed_history=True 且 response 为空时,我们之前只是创建了一个空的 LLMResponse 但没有添加到 messages 中保存。

现已修改:在保存失败历史时,会在 messages 中添加一条带有失败标记的 assistant message [Agent run failed. History saved for debugging.],确保下一轮 messages 格式正确。

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Apr 19, 2026
@Soulter Soulter force-pushed the master branch 2 times, most recently from faf411f to 0068960 Compare April 19, 2026 09:50
@Blueteemo Blueteemo closed this Apr 21, 2026
@Blueteemo Blueteemo deleted the fix/issue-7620-save-failed-agent-history-clean branch April 22, 2026 01:06
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:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] 建议增加“失败时保存本轮记录”的配置项

2 participants