Skip to content

perf: 前置程序运行中可以使用停止任务#159

Merged
MistEO merged 4 commits intoMistEO:mainfrom
overflow65537:fix/pre-action-stop-control
Apr 7, 2026
Merged

perf: 前置程序运行中可以使用停止任务#159
MistEO merged 4 commits intoMistEO:mainfrom
overflow65537:fix/pre-action-stop-control

Conversation

@overflow65537
Copy link
Copy Markdown
Contributor

@overflow65537 overflow65537 commented Apr 7, 2026

close #157

Summary by Sourcery

通过在前端和 Tauri 后端之间传递可取消的停止信号,使任务在预动作程序运行期间也可以被停止。

New Features:

  • 使用现有的开始/停止控件和快捷键,在预动作执行期间支持停止任务。

Enhancements:

  • 在工具栏中跟踪由预动作控制的实例,将其视为“运行中”以反映 UI 状态,并禁用相冲突的启动操作。
  • 在预动作流程(设备搜索、连接延时)中加入协作式取消检查,当收到停止请求时能够干净地中止执行。
  • 调整开始/停止按钮的行为和状态处理,以区分“已取消的启动”和“启动失败”。
Original summary in English

Summary by Sourcery

Allow tasks to be stopped while a pre-action program is running by wiring a cancellable stop signal through the frontend and Tauri backend.

New Features:

  • Support stopping tasks during pre-action execution using the existing start/stop controls and hotkeys.

Enhancements:

  • Track pre-action-controlled instances in the toolbar to treat them as running for UI state, disabling conflicting starts.
  • Introduce cooperative cancellation checks into pre-action flows (device search, connect delay) to abort cleanly when a stop is requested.
  • Adjust start/stop button behavior and status handling to distinguish between cancelled starts and failed starts.

Summary by Sourcery

允许在预操作程序运行时停止任务,并在前端和后端之间传播取消状态。

New Features:

  • 通过跟踪由预操作控制的实例,并通过 Tauri 命令暴露停止标志,支持在预操作执行期间停止任务。

Bug Fixes:

  • 将预操作控制视为运行状态,防止在预操作进行中启动新的运行。
  • 当启动序列在预操作期间被显式取消时,避免显示启动失败错误以及失败状态。

Enhancements:

  • 优化开始/停止按钮的行为和快捷键处理逻辑,使其能够正确处理预操作运行状态,包括禁用状态和视觉反馈。
  • 将预操作等待逻辑集中管理,并加入停止检查,以确保长时间等待可以被干净地中断。
Original summary in English

Summary by Sourcery

Allow tasks to be stopped while a pre-action program is running and propagate cancellation through the frontend and backend.

New Features:

  • Support stopping tasks during pre-action execution by tracking a pre-action controlled instance and exposing a stop flag via Tauri commands.

Bug Fixes:

  • Prevent starting a new run while a pre-action is in progress by treating pre-action control as a running state.
  • Avoid showing start-failed errors and failed status when a start sequence is explicitly cancelled during pre-action.

Enhancements:

  • Refine start/stop button behavior and hotkey handling to account for pre-action runs, including disabled states and visual feedback.
  • Centralize pre-action wait logic with stop checks to ensure long waits can be interrupted cleanly.

Treat pre-action execution as a controllable running state so users can stop waiting for prerequisite programs, device polling, and startup delays before the main task queue begins.

Made-with: Cursor
@overflow65537 overflow65537 marked this pull request as ready for review April 7, 2026 06:53
Copy link
Copy Markdown
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 - 我在这里给出了一些高层次的反馈:

  • 当前的取消流程依赖于将 err.message 与硬编码字符串 MXU_PRE_ACTION_CANCELLED 进行比较;建议考虑使用专门的错误类或错误标记(例如使用 err.code),以避免在 TS/Rust 边界之间依赖脆弱的字符串比较。
  • 在代码中有多处零散地检查 preActionControlledInstanceIdRef.current === instanceId 及相关状态(例如 isStartStopRunning、快捷键处理函数、performStop);如果将这类逻辑封装到一个小的辅助函数中(例如 isInstanceInPreAction(instanceId)),可能会更容易理解和维护。
给 AI 代理的提示
Please address the comments from this code review:

## Overall Comments
- The cancellation flow relies on comparing `err.message` to the hard-coded string `MXU_PRE_ACTION_CANCELLED`; consider using a dedicated error class or tagging (e.g. `err.code`) to avoid fragile string comparisons across TS/Rust boundaries.
- There are several scattered checks of `preActionControlledInstanceIdRef.current === instanceId` and related state (e.g. `isStartStopRunning`, hotkey handlers, `performStop`); it may be easier to reason about and maintain if you encapsulate this logic in a small helper like `isInstanceInPreAction(instanceId)`.

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

Hey - I've left some high level feedback:

  • The cancellation flow relies on comparing err.message to the hard-coded string MXU_PRE_ACTION_CANCELLED; consider using a dedicated error class or tagging (e.g. err.code) to avoid fragile string comparisons across TS/Rust boundaries.
  • There are several scattered checks of preActionControlledInstanceIdRef.current === instanceId and related state (e.g. isStartStopRunning, hotkey handlers, performStop); it may be easier to reason about and maintain if you encapsulate this logic in a small helper like isInstanceInPreAction(instanceId).
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The cancellation flow relies on comparing `err.message` to the hard-coded string `MXU_PRE_ACTION_CANCELLED`; consider using a dedicated error class or tagging (e.g. `err.code`) to avoid fragile string comparisons across TS/Rust boundaries.
- There are several scattered checks of `preActionControlledInstanceIdRef.current === instanceId` and related state (e.g. `isStartStopRunning`, hotkey handlers, `performStop`); it may be easier to reason about and maintain if you encapsulate this logic in a small helper like `isInstanceInPreAction(instanceId)`.

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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

该 PR 旨在解决 #157:在“前置程序/预动作”执行与等待期间,也能使用现有的停止按钮/快捷键来中断启动流程,并让工具栏把“前置程序控制中”的实例视为运行中,以便正确呈现 UI 状态和阻止冲突启动。

Changes:

  • 前端引入“前置程序控制”状态与协作式取消检查(设备搜索、连接延时等待等)。
  • 前端通过新的 Tauri 命令设置/清理“前置程序停止请求”,并在 run_action 等待退出时可被取消。
  • 调整开始/停止按钮与快捷键停止逻辑:前置阶段也可停止,并区分“取消启动”与“启动失败”。

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/services/maaService.ts runAction 传递 instanceId,并新增 setPreActionStop 调用后端停止标记。
src/components/Toolbar.tsx 增加前置控制状态、停止检查与等待封装;更新按钮/快捷键逻辑以支持前置阶段停止。
src-tauri/src/lib.rs 注册新的 set_pre_action_stop Tauri command。
src-tauri/src/commands/types.rs MaaState 新增 pre_action_stop_requests,用于跨命令保存停止请求。
src-tauri/src/commands/system.rs 实现 set_pre_action_stop;run_action 等待退出改为可轮询取消。
Comments suppressed due to low confidence (1)

src/services/maaService.ts:707

  • runAction 新增了 instanceId 参数,但上方 JSDoc 仍标注为“Run pre-action”且缺少对 instanceId 的说明(尤其是它会影响后端取消逻辑)。建议补充 @param instanceId 的含义/来源,避免后续调用方漏传或误传。
  /**
   * Run pre-action
   * @param program 程序路径
   * @param args 附加参数
   * @param cwd 工作目录(可选)
   * @param waitForExit 是否等待进程退出(默认 true)
   * @param useCmd 是否通过 cmd /c 启动(仅 Windows,默认 false)
   * @returns 程序退出码(不等待时返回 0)
   */
  async runAction(
    instanceId: string,
    program: string,
    args: string,
    cwd?: string,
    waitForExit: boolean = true,
    useCmd: boolean = false,
  ): Promise<number> {

Eliminated redundant invocation of setPreActionStop to streamline the pre-action control logic in the Toolbar component.
Improved the error handling logic in the Toolbar component's pre-action stop functionality. Added a flag to manage stopping behavior during pre-action requests, ensuring that the stopping state is correctly set based on the success of the stop request. This change enhances the robustness of task management during pre-action execution.
Implemented a background thread to manage the waiting of child processes when a stop request is received during pre-action execution. This change prevents potential child process leaks while ensuring that the stop request is handled gracefully.
@MistEO MistEO merged commit f6904d8 into MistEO:main Apr 7, 2026
9 checks passed
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