Skip to content

fix(tasks): Agent 内部 PostStop 时按停止任务回调收尾,回收本轮 Agent - #436

Open
zmdyy0318 wants to merge 2 commits into
MaaXYZ:mainfrom
zmdyy0318:fix/agent-poststop-completion
Open

zmdyy0318 wants to merge 2 commits into
MaaXYZ:mainfrom
zmdyy0318:fix/agent-poststop-completion

Conversation

@zmdyy0318

@zmdyy0318 zmdyy0318 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

close MaaEnd/MaaEnd#6167

问题

Agent 内部调用 Tasker.PostStop() 时(如 MaaEnd 分辨率检查不通过、pipeline 中的 PostStop 动作),本轮 Agent 不会被回收。下次启动任务时新 Agent 注册同名自定义识别失败,报:

任务启动失败: Agent start failed: MaaFramework internal error: status 0

maafw.log 中对应 custom name already registered。

相关:MaaEnd/MaaEnd#6167

原因

Tasker::post_stop 会 task_runner_->clear() 清空排队任务,被清掉的任务不会发送任何 Succeeded/Failed 回调,只会补发一个 entry 为 MaaTaskerPostStop 的停止任务。

handle_task_callback 只在 queue_result() 有结果或 stop_in_progress(MXU 自己发起停止)时开始收尾。Agent 内部停止时两者都不满足,而 MaaTaskerPostStop 的 task_id 不在 pending_task_ids 中会被直接过滤,所以收尾永远不触发,agent_clients / agent_children 残留到下一轮。

修改

收到 MaaTaskerPostStop 的终态回调时,若本轮队列仍有任务没有结果,按失败调用 begin_completion(false),走现有 schedule_run_completion(等待空闲 → 回收 Agent → 发送 tasks-completed)。

  • MXU 自己发起的停止:已由原路径收尾,begin() 返回 false,不会重复执行。
  • 最后一个任务结束时才 PostStop:队列已全部有结果,由原路径收尾,不覆盖其成功状态。
  • 停止任务回调执行时 tasker 仍处于 running,新一轮无法提交,迟到回调不会影响下一轮。

验证

Win32 控制器、游戏窗口非 16:9、勾选 2 个任务,触发 MaaEnd 分辨率检查的 PostStop 后再次开始:

  • 修改前:无 stop_agent 日志,第二次启动报 status 0。
  • 修改后:停止后立即 [stop_agent] Stopping 2 agent client(s) and 2 child process(es)...,子进程退出,前端收到 tasks-completed;第二次启动 Agent 正常连接。

cargo check 通过。

AI 使用信息:

  • Agent:Claude Code
  • 模型:Claude Opus 5.5
  • 思考强度:中

🤖 Generated with Claude Code

Sourcery 总结

完成并清理由内部 Tasker PostStop 回调触发的 Agent 运行,以便后续任务运行能够顺利启动。

Bug 修复:

  • 当内部 PostStop 回调中断排队中的任务时,确保 Agent 运行完成,并回收其资源。
  • 将被中断的待处理任务视为失败,同时在所有任务已有结果时保留正常的完成行为。
Original summary in English

Sourcery 摘要

错误修复:

  • 确保由内部 Tasker PostStop 回调中断的 Agent 运行能够完成收尾,记录失败的待处理任务,并回收 Agent 资源,以便后续运行能够成功启动。
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Ensure Agent runs interrupted by an internal Tasker PostStop callback are finalized, failed pending tasks are recorded, and Agent resources are reclaimed so subsequent runs can start successfully.

Tasker::post_stop 会清空排队任务且不为其发送回调,只补发一个 MaaTaskerPostStop 停止任务。
Agent 内部调用 PostStop 时,被清掉的任务永远等不到终态,收尾不会触发,本轮 Agent 残留,
下次启动时新 Agent 注册同名自定义识别失败,报 "MaaFramework internal error: status 0"。

收到 MaaTaskerPostStop 的终态回调且队列仍有任务无结果时,按失败走现有收尾流程。
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T12:54:09.281067Z c09e240 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c09e240a6a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src-tauri/src/commands/utils.rs Resolved

@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.

你好 - 我发现了 2 个问题

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

## Individual Comments

### Comment 1
<location path="src-tauri/src/commands/utils.rs" line_range="189" />
<code_context>
+        {
+            return;
+        }
+        instance.begin_completion(false)
+    };
+
</code_context>
<issue_to_address>
**Tasks run after stop request**

When an append has acquired its start permit and is still registering Agents when the internal PostStop callback arrives, `handle_post_stop_callback` sets neither `stop_in_progress` nor `submission_cancelled`, so `start_tasks_impl` posts the appended tasks after PostStop; work then runs while the run is being finalized as failed.

Make PostStop cancel or block admitted submissions and prevent them from posting tasks after the stop request.
</issue_to_address>

### Comment 2
<location path="src-tauri/src/commands/utils.rs" line_range="189" />
<code_context>
+        {
+            return;
+        }
+        instance.begin_completion(false)
+    };
+
</code_context>
<issue_to_address>
**Tasks remain active after completion**

When an Agent-internal PostStop clears queued tasks that have selected-task status mappings, `begin_completion(false)` updates only the overall run status, and the cleared tasks receive no terminal callbacks to update their mapped statuses. Those statuses remain `pending` or `running` after `tasks-completed`, so the frontend shows unfinished tasks.

Update the mapped statuses of tasks cleared by PostStop to terminal statuses before completing the run.
</issue_to_address>

Sourcery 对开源项目免费 - 如果你喜欢我们的评审,请考虑分享 ✨
Original comment in English

Hey - I've found 2 issues

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

## Individual Comments

### Comment 1
<location path="src-tauri/src/commands/utils.rs" line_range="189" />
<code_context>
+        {
+            return;
+        }
+        instance.begin_completion(false)
+    };
+
</code_context>
<issue_to_address>
**Tasks run after stop request**

When an append has acquired its start permit and is still registering Agents when the internal PostStop callback arrives, `handle_post_stop_callback` sets neither `stop_in_progress` nor `submission_cancelled`, so `start_tasks_impl` posts the appended tasks after PostStop; work then runs while the run is being finalized as failed.

Make PostStop cancel or block admitted submissions and prevent them from posting tasks after the stop request.
</issue_to_address>

### Comment 2
<location path="src-tauri/src/commands/utils.rs" line_range="189" />
<code_context>
+        {
+            return;
+        }
+        instance.begin_completion(false)
+    };
+
</code_context>
<issue_to_address>
**Tasks remain active after completion**

When an Agent-internal PostStop clears queued tasks that have selected-task status mappings, `begin_completion(false)` updates only the overall run status, and the cleared tasks receive no terminal callbacks to update their mapped statuses. Those statuses remain `pending` or `running` after `tasks-completed`, so the frontend shows unfinished tasks.

Update the mapped statuses of tasks cleared by PostStop to terminal statuses before completing the run.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src-tauri/src/commands/utils.rs Resolved
Comment thread src-tauri/src/commands/utils.rs Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

任务启动失败

1 participant