Skip to content

fix: 适配 Agent 优雅关闭语义,避免终态事件在转发给 Agent 前被截断 - #332

Draft
MarchSnow-1 wants to merge 1 commit into
MistEO:mainfrom
MarchSnow-1:fix/agent-cleanup-delay
Draft

fix: 适配 Agent 优雅关闭语义,避免终态事件在转发给 Agent 前被截断#332
MarchSnow-1 wants to merge 1 commit into
MistEO:mainfrom
MarchSnow-1:fix/agent-cleanup-delay

Conversation

@MarchSnow-1

@MarchSnow-1 MarchSnow-1 commented Aug 27, 2026

Copy link
Copy Markdown

背景

#331 在任务结束瞬间同步断开 Agent IPC 会话,导致 Tasker.Task.Failed / Tasker.Task.Succeeded 终态事件在转发给 agent 前被截断,依赖终态事件的下游功能(失败通知、失败反馈提示等)失效

改动

本 PR 对 agent 生命周期管理做三点调整,为 Agent 优雅关闭语义做适配:

1、任务结束后的清理移至后台线程(handle_task_callback)

disconnect 会同步等待 agent 收尾(可达数秒),而该回调运行在框架事件分发线程上,阻塞它会卡住后续所有事件

2、强杀看门狗前置(stop_agent_impl)

先启动 15s 看门狗再 disconnect
因为 agent 收尾存在卡死的情况
卡死时看门狗杀进程、socket 断开,阻塞中的 disconnect 立即报错返回,调用线程不会被永久钉死
正常情况下 agent 收尾完会自行退出,看门狗不触发

3、启动前同步清理残留(start_tasks_impl,仅 reset_state = true)

防进程累积,追加批次(reset_state = false)复用现有 agent,不受影响


上述功能依赖于 MaaFramework 侧计划的优雅关闭改动(AgentServer 关闭回调、disconnect 先排空事件转发再断开),届时 agent 可在收尾(如等待失败通知发出)完成后自行退出

在 MaaFramework 侧合入前,本 PR 行为与现状等效(agent 断开后 5s→15s 被回收,对新启动的 agent 无干扰)
可独立先行合并

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

你好——我已经审阅了你的更改,看起来很棒!


Sourcery 对开源项目免费——如果你喜欢我们的评审,欢迎分享 ✨
帮助我提供更有用的反馈!请在每条评论上点击 👍 或 👎,我会利用这些反馈来改进评审。
Original comment in English

Hey - I've reviewed your changes and they look great!


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.

@MarchSnow-1
MarchSnow-1 marked this pull request as draft August 27, 2026 16:53
@MarchSnow-1
MarchSnow-1 force-pushed the fix/agent-cleanup-delay branch from 9a3f98c to a1d7a2e Compare August 27, 2026 17:02
@MarchSnow-1
MarchSnow-1 marked this pull request as ready for review August 27, 2026 17:08

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

你好——我发现了 1 个问题

面向 AI Agent 的提示
请处理本次代码审查中的评论:

## 具体评论

### 评论 1
<location path="src-tauri/src/commands/maa_agent.rs" line_range="530-531" />
<code_context>
 ) -> Result<Vec<i64>, String> {
     info!("start_tasks_impl called");

+    // 先清理上一轮残留的 agent(延迟清理可能尚未执行),避免旧反注册误删新 custom
+    let _ = stop_agent_impl(maa_state, &instance_id);
+
     info!("instance_id: {}", instance_id);
</code_context>
<issue_to_address>
**问题(更广泛的影响):** 每次调用 `start_tasks_impl`,包括带有 `reset_state = false` 的分段追加调用,都会在提交新任务之前同步断开并移除当前处于活动状态的 Agent 会话。因此,即使明确使用 `reset_state = false` 将任务追加到同一次运行中,后续任务批次仍会丢失其正在运行的 Agent IPC 连接。

**触发条件:** 当一次任务运行被拆分为多个批次,并且后续批次以 `reset_state = false` 启动时。

**建议修复:** 仅在开始新的一次运行时(`reset_state`)执行启动清理,或者以其他方式保留追加批次所需的现有 Agent。

```suggestion
    if reset_state {
        // 先清理上一轮残留的 agent(延迟清理可能尚未执行),避免旧反注册误删新 custom
        let _ = stop_agent_impl(maa_state, &instance_id);
    }
```
</issue_to_address>

Sourcery 对开源项目免费提供服务——如果您喜欢我们的审查结果,欢迎考虑分享 ✨
请帮助我变得更有用!请对每条评论点击 👍 或 👎,我会利用您的反馈改进审查结果。
Original comment in English

Hey - I've found 1 issue

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

## Individual Comments

### Comment 1
<location path="src-tauri/src/commands/maa_agent.rs" line_range="530-531" />
<code_context>
 ) -> Result<Vec<i64>, String> {
     info!("start_tasks_impl called");

+    // 先清理上一轮残留的 agent(延迟清理可能尚未执行),避免旧反注册误删新 custom
+    let _ = stop_agent_impl(maa_state, &instance_id);
+
     info!("instance_id: {}", instance_id);
</code_context>
<issue_to_address>
**issue (broader_impact):** Every call to `start_tasks_impl`, including segmented append calls with `reset_state = false`, synchronously disconnects and removes the currently active Agent sessions before submitting the new tasks. Subsequent task batches therefore lose their running Agent IPC connections even though `reset_state = false` is explicitly used to append tasks to the same run.

**Triggers:** When a task run is split into multiple batches and a later batch is started with `reset_state = false`.

**Suggested fix:** Only perform startup cleanup when beginning a new run (`reset_state`), or otherwise preserve the existing agents for append batches.

```suggestion
    if reset_state {
        // 先清理上一轮残留的 agent(延迟清理可能尚未执行),避免旧反注册误删新 custom
        let _ = stop_agent_impl(maa_state, &instance_id);
    }
```
</issue_to_address>

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 src-tauri/src/commands/maa_agent.rs Outdated
@MarchSnow-1
MarchSnow-1 marked this pull request as draft August 27, 2026 17:10
@MarchSnow-1
MarchSnow-1 marked this pull request as ready for review August 27, 2026 17:14

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

你好——我已经审阅了你的更改,看起来非常棒!


Sourcery 对开源项目免费——如果你喜欢我们的评审,欢迎分享 ✨
帮助我变得更有用!请点击每条评论旁的 👍 或 👎,我会利用这些反馈来改进评审。
Original comment in English

Hey - I've reviewed your changes and they look great!


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.

@ocsin1

ocsin1 commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

不太好这样,hook的话可以在接受关闭信号时自己决定什么时候关闭吧

@MarchSnow-1

Copy link
Copy Markdown
Author

不太好这样,hook的话可以在接受关闭信号时自己决定什么时候关闭吧

现在maafw那边好像没有对应实现🤔我看一下

@MarchSnow-1
MarchSnow-1 marked this pull request as draft August 28, 2026 03:36
@zmdyy0318

Copy link
Copy Markdown
Contributor

延迟 2s 执行这个方案不太建议,最终会演变成在某些电脑需要延迟到5秒、10秒很奇怪。
下一轮启动前同步清理残留,这个可以

@MarchSnow-1

Copy link
Copy Markdown
Author

延迟 2s 执行这个方案不太建议,最终会演变成在某些电脑需要延迟到5秒、10秒很奇怪。 下一轮启动前同步清理残留,这个可以

2s方案已经废弃了,改成任务结束就 disconnect(顺序保证移到 maafw 侧做)+ 强杀看门狗前置(防 disconnect 被 agent 卡死钉住)+ 下一轮启动前同步清理残留

然后我去 maafw 那边开个pr,实现 AgentServer 侧加关闭回调,agent 收到 ShutDownRequest 后自行阻塞等在途任务完成(比如失败通知依赖 task failed 信号)再返回,然后 Join 返回、进程自行退出

client 的 disconnect 同步等 ShutDownResponse,拿到确认就是收尾完成

@MarchSnow-1
MarchSnow-1 force-pushed the fix/agent-cleanup-delay branch from fca8bb4 to 845c4c2 Compare August 28, 2026 05:10
@MarchSnow-1 MarchSnow-1 changed the title fix: 延迟清理 agent 会话,避免终态事件转发被阻断 fix: 适配 Agent 优雅关闭语义,避免终态事件在转发给 Agent 前被截断 Aug 28, 2026
@zmdyy0318

zmdyy0318 commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

这个需求我来吧,fw那边审核比较严,等几天

@MarchSnow-1

MarchSnow-1 commented Aug 28, 2026

Copy link
Copy Markdown
Author

这个需求我来吧,fw那边审核比较严,等几天

我差不多写完了已经 还没提交

先看看能不能过,不能过的话就再说

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.

3 participants