Skip to content

Conversation

@Li-shi-ling
Copy link
Contributor

@Li-shi-ling Li-shi-ling commented Feb 9, 2026

在调用 HandoffTool 工具时,出现了 "HandoffTool 对象没有 'agent' 属性" 的错误。经过排查发现,问题出现在 HandoffTool 的 init 方法中。在 self.agent = agent 赋值之后,调用 super().init() 会导致 self.agent 属性被意外覆盖或删除。
Fixes #5004

Modifications / 改动点

改变了astrbot.core.agent.handoff文件
将self.agent = agent移动到super().__init__之后

    def __init__(
        self,
        agent: Agent[TContext],
        parameters: dict | None = None,
        tool_description: str | None = None,
        **kwargs,
    ) -> None:

        # Avoid passing duplicate `description` to the FunctionTool dataclass.
        # Some call sites (e.g. SubAgentOrchestrator) pass `description` via kwargs
        # to override what the main agent sees, while we also compute a default
        # description here.
        # `tool_description` is the public description shown to the main LLM.
        # Keep a separate kwarg to avoid conflicting with FunctionTool's `description`.
        description = tool_description or self.default_description(agent.name)
        super().__init__(
            name=f"transfer_to_{agent.name}",
            parameters=parameters or self.default_parameters(),
            description=description,
            **kwargs,
        )

        # Optional provider override for this subagent. When set, the handoff
        # execution will use this chat provider id instead of the global/default.
        self.provider_id: str | None = None
        self.agent = agent
  • This is NOT a breaking change. / 这不是一个破坏性变更。

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

6a615f34b2e14d7ab249863327542988 image

Checklist / 检查清单

  • [ X ] 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。/ If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    没有新功能
  • [ X ] 👀 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    验证步骤在iss里面,运行截图在上方
  • [ X ] 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ 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.
    没有引入
  • [ X ] 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.
    没有引入

Summary by Sourcery

错误修复:

  • 确保 HandoffTool 在基类初始化之后仍然保留其 agent 属性,以避免出现“object has no attribute agent”(对象没有 agent 属性)的错误。
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Ensure HandoffTool retains its agent attribute after base class initialization to avoid 'object has no attribute agent' errors.
- 修复在初始化过程中丢失 `HandoffTool.agent` 引用的问题,该问题会导致运行时错误:`'HandoffTool' object has no attribute 'agent'`。
Original summary in English

Summary by Sourcery

错误修复:

  • 确保 HandoffTool 在基类初始化之后仍然保留其 agent 属性,以避免出现“object has no attribute agent”(对象没有 agent 属性)的错误。
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Ensure HandoffTool retains its agent attribute after base class initialization to avoid 'object has no attribute agent' errors.

@auto-assign auto-assign bot requested review from Soulter and advent259141 February 9, 2026 16:45
@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Feb 9, 2026
@dosubot
Copy link

dosubot bot commented Feb 9, 2026

Related Documentation

Checked 1 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

Copy link
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 - 我在这里给出了一些总体反馈:

  • 建议在 self.agent = agent 附近加一条简短注释,说明这行代码必须放在 super().__init__() 之后赋值,因为父类的初始化方法会覆盖属性,这样可以避免以后重构时无意中再次引入这个 bug。
给 AI Agent 的提示
请根据本次代码审查中的评论进行修改:

## 总体评论
- 建议在 `self.agent = agent` 附近加一条简短注释,说明这行代码必须放在 `super().__init__()` 之后赋值,因为父类的初始化方法会覆盖属性,这样可以避免以后重构时无意中再次引入这个 bug。

Sourcery 对开源项目是免费的——如果你觉得我们的审查有帮助,欢迎分享 ✨
帮我变得更有用!请对每条评论点 👍 或 👎,我会根据你的反馈来改进后续的代码审查。
Original comment in English

Hey - I've left some high level feedback:

  • Consider adding a short comment near self.agent = agent explaining that it must be assigned after super().__init__() because the parent initializer overwrites attributes, so future refactors don’t accidentally reintroduce the bug.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider adding a short comment near `self.agent = agent` explaining that it must be assigned after `super().__init__()` because the parent initializer overwrites attributes, so future refactors don’t accidentally reintroduce the bug.

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.

@dosubot dosubot bot added the area:core The bug / feature is about astrbot's core, backend label Feb 9, 2026
@Li-shi-ling
Copy link
Contributor Author

Li-shi-ling commented Feb 9, 2026

这个bug藏的好深!我前半段时间一直以为是其他部位有什么操作将agent给去掉了
然后一直在研究SubAgentOrchestrator的走向
直到发现Context里的SubAgentOrchestrator管理器
就开始用插件测试,然后就发现在__init__阶段就出问题了

Copy link
Member

@Dt8333 Dt8333 left a comment

Choose a reason for hiding this comment

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

LGTM
合并前可能需要重新跑一下action确认无问题。

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Feb 9, 2026
Dt8333

This comment was marked as duplicate.

@Soulter Soulter changed the title fix: 移动agent的位置到super().__init__之后 #5004 fix: 'HandoffTool' object has no attribute 'agent' Feb 10, 2026
@Soulter Soulter merged commit aab0953 into AstrBotDevs:master Feb 10, 2026
4 of 6 checks passed
@Li-shi-ling Li-shi-ling deleted the fix-ensure-agent-attribute-preservation-in-HandoffTool-initialization branch February 10, 2026 03:10
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 lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 在使用SubAgent 编排时会出现'HandoffTool' object has no attribute 'agent'的错误

3 participants