Skip to content

fix: apply max_agent_step config to cron and background task agents - #9801

Open
Shaofei215 wants to merge 3 commits into
AstrBotDevs:masterfrom
Shaofei215:fix/cron-bg-task-max-agent-step
Open

fix: apply max_agent_step config to cron and background task agents#9801
Shaofei215 wants to merge 3 commits into
AstrBotDevs:masterfrom
Shaofei215:fix/cron-bg-task-max-agent-step

Conversation

@Shaofei215

@Shaofei215 Shaofei215 commented Aug 24, 2026

Copy link
Copy Markdown

Fixes #9800
修复了 #9800

Modifications / 改动点

  • Read provider_settings.max_agent_step in the cron (future task) wake-up path (astrbot/core/cron/manager.py) and the background task result wake-up path (astrbot/core/astr_agent_tool_exec.py), and pass it to runner.step_until_done() instead of the hardcoded 30.
    在 Cron 未来任务唤醒路径(astrbot/core/cron/manager.py)与后台任务结果唤醒路径(astrbot/core/astr_agent_tool_exec.py)中读取 provider_settings.max_agent_step,传入 runner.step_until_done(),替换写死的 30

  • Parse the value with the existing coerce_int_config utility: bool / non-numeric string / None falls back to the default 30 with a warning. This is consistent with the main pipeline's [Bug] 更新4.0前如果更改过唤醒词,发出的任何请求不会有回应 #2622 bool workaround and the existing usage in the deerflow runner / request retry logic. Behavior is unchanged for users who have not customized this setting.
    使用既有的 coerce_int_config 工具解析取值:布尔 / 非数字字符串 / None 回退到默认值 30 并输出警告。与主管线的 [Bug] 更新4.0前如果更改过唤醒词,发出的任何请求不会有回应 #2622 布尔 workaround 及 deerflow runner / 请求重试逻辑的既有用法保持一致。未自定义该项配置的用户行为不变。

  • Add 8 parametrized unit tests (4 cases × 2 paths) covering: configured value propagation, missing-key fallback, boolean pollution fallback, and numeric string coercion.
    新增 8 个参数化单元测试(4 种用例 × 2 条路径),覆盖:配置值传播、缺省回退、布尔污染回退、数字字符串转换。

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

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

Lint and unit tests (all executed locally on the PR branch) / 代码检查与单元测试(均在 PR 分支本地执行):

$ ruff check .
All checks passed!

$ ruff format --check astrbot/core/cron/manager.py astrbot/core/astr_agent_tool_exec.py tests/unit/test_cron_manager.py tests/unit/test_astr_agent_tool_exec.py
4 files already formatted

$ pytest tests/unit/test_cron_manager.py tests/unit/test_astr_agent_tool_exec.py -q
57 passed in 6.66s

Regression guard verification: with the fix reverted (git checkout master -- astrbot/core/cron/manager.py astrbot/core/astr_agent_tool_exec.py), the new value-propagation test cases fail as expected (4 failed, 4 passed), proving the tests actually protect against the regression.
回归防护验证:将修复还原后(git checkout master -- astrbot/core/cron/manager.py astrbot/core/astr_agent_tool_exec.py),新增的取值传播测试用例如预期失败(4 failed, 4 passed),证明测试确实能防护该回归。


Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
    (Bug fix only, no new features — the fix targets the behavior reported in [Bug] 未来任务被写死只能调用30次工具 #9800. / 仅为缺陷修复,无新增功能,修复对象即 [Bug] 未来任务被写死只能调用30次工具 #9800 所报告的行为。)
  • 👀 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

Honor max_agent_step configuration for cron and background-task agent execution while retaining safe fallback behavior.

Bug Fixes:

  • Apply the configured provider_settings.max_agent_step limit to cron-triggered and background-task agent wake-ups instead of always using the default limit.

Enhancements:

  • Normalize invalid or missing step-limit configuration through the existing integer configuration handling, preserving the default and minimum safeguards.

Tests:

  • Add parameterized unit coverage for configured values, defaults, boolean and numeric-string inputs, and minimum-value clamping across both wake-up paths.

The cron (scheduled task) and background task wake-up paths hardcoded
step_until_done(30) since the "extract main agent" refactor (0c5308a),
ignoring provider_settings.max_agent_step.

Read the value via coerce_int_config so invalid values fall back to the
default with a warning instead of crashing or silently degrading.

Fixes AstrBotDevs#9800
Add parametrized tests asserting step_until_done receives the configured
max_agent_step on both the cron and background task agent paths, with
fallback coverage for missing, boolean-polluted, and numeric-string
config values.
@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 Aug 24, 2026

@sourcery-ai sourcery-ai Bot left a comment

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.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="astrbot/core/astr_agent_tool_exec.py" line_range="556" />
<code_context>
         cron_event.role = event.role
         cfg = ctx.get_config(umo=event.unified_msg_origin) or {}
         provider_settings = cfg.get("provider_settings") or {}
+        agent_max_step = coerce_int_config(
+            provider_settings.get("max_agent_step", 30),
+            default=30,
+            field_name="provider_settings.max_agent_step",
+        )
         config = MainAgentBuildConfig(
</code_context>
<issue_to_address>
**issue (bug_risk):** Non-positive `max_agent_step` values are accepted because these calls omit `min_value`. The local runner skips all normal steps and immediately forces a final response for zero or negative values, while the DeerFlow runner raises `ValueError`, so an invalid configured value breaks or truncates both wake-up paths instead of falling back to a usable default.

**Triggers:** When `provider_settings.max_agent_step` is configured as zero or a negative integer.

**Suggested fix:** Pass `min_value=1` to `coerce_int_config` so non-positive values fall back to the default or minimum instead of reaching `step_until_done`.

```suggestion
            min_value=1,
            field_name="provider_settings.max_agent_step",
```
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 1 finding to address first, and if the configured limit is wrong, cron or background agents may perform extra model/tool steps, increasing compute and potentially triggering additional tool side effects before the run stops. Reverting restores the fixed 30-step behavior, but any messages or other actions already taken during an overlong run would not be undone.

Blocking findings: astrbot/core/astr_agent_tool_exec.py:556


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.

Comment thread astrbot/core/astr_agent_tool_exec.py
…ke-ups

Non-positive provider_settings.max_agent_step values previously reached
step_until_done as-is: the local runner skipped all steps and forced a
final response, while the DeerFlow runner raised ValueError. Pass
min_value=1 to coerce_int_config so zero/negative values are clamped to
a usable minimum, and cover the zero case in both unit test suites.
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:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 未来任务被写死只能调用30次工具

1 participant