fix(cron): trigger llm request hooks before resetting main agent#8702
fix(cron): trigger llm request hooks before resetting main agent#8702Foolllll-J wants to merge 1 commit into
Conversation
Align cron-triggered main-agent wake flow with the normal LLM request path. - trigger OnWaitingLLMRequestEvent before building the main agent - build the cron main agent with apply_reset=False - trigger OnLLMRequestEvent before awaiting reset_coro - preserve hook-based interception by closing reset_coro when stopped - add a regression test for cron hook timing
There was a problem hiding this comment.
Code Review
This pull request updates the cron manager's main agent wake flow to trigger OnWaitingLLMRequestEvent and OnLLMRequestEvent hooks, mirroring the standard request-hook timing, and adds corresponding unit tests. The review feedback suggests defensively checking if reset_coro has a close method before calling it to prevent potential AttributeError exceptions if the coroutine type is refactored in the future.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if reset_coro: | ||
| reset_coro.close() | ||
| return |
There was a problem hiding this comment.
To ensure defensive programming and prevent potential AttributeErrors, it is safer to check if reset_coro has a close method before calling it. If reset_coro is ever refactored to be a Task, Future, or another custom awaitable that does not implement close(), calling close() directly would raise an error.
| if reset_coro: | |
| reset_coro.close() | |
| return | |
| if reset_coro and hasattr(reset_coro, "close"): | |
| reset_coro.close() | |
| return |
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The new
reset_corohandling in_woke_main_agentwill skip awaiting/closing ifrunner.step_until_doneraises; consider wrapping the body in atry/finallyto ensurereset_corois always awaited/closed even on errors to match the intended lifecycle semantics. - The hook +
apply_reset=Falseflow in_woke_main_agentnow closely mirrors the normal LLM request path; consider extracting this into a shared helper so that cron and non-cron flows reuse the same hook ordering and reset logic to prevent future divergence.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `reset_coro` handling in `_woke_main_agent` will skip awaiting/closing if `runner.step_until_done` raises; consider wrapping the body in a `try/finally` to ensure `reset_coro` is always awaited/closed even on errors to match the intended lifecycle semantics.
- The hook + `apply_reset=False` flow in `_woke_main_agent` now closely mirrors the normal LLM request path; consider extracting this into a shared helper so that cron and non-cron flows reuse the same hook ordering and reset logic to prevent future divergence.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Align cron-triggered main-agent wake flow with the normal LLM request path.
Closes #8701
Fixes the inconsistency where cron-triggered main-agent requests did not follow the same hook semantics as normal LLM requests. Previously, plugins relying on
OnWaitingLLMRequestEventorOnLLMRequestEventcould work in regular conversations but not in cron-triggered proactive tasks.This change limits its scope to the
cronpath only. It does not alter the hook behavior of background-task result callbacks or other internal wake flows.OnWaitingLLMRequestEventbefore building the main agentapply_reset=FalseOnLLMRequestEventbefore awaitingreset_cororeset_corowhen stoppedModifications / 改动点
Updated
astrbot/core/cron/manager.pyOnWaitingLLMRequestEventbefore the cron main-agent build flowapply_reset=FalseOnLLMRequestEventbefore awaitingreset_cororeset_corowhen the hook stops the eventUpdated
tests/unit/test_cron_manager.pyThis is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
Verification steps executed locally:
Results:
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.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Align cron-triggered main-agent wake flow with standard LLM request hook behavior for proactive tasks.
Bug Fixes:
Tests: