Skip to content

fix(update): 任务结束后自动安装并重启时不再重复执行自动任务 - #433

Open
zmdyy0318 wants to merge 1 commit into
MaaXYZ:mainfrom
zmdyy0318:fix/autostart-skip-rerun-after-update
Open

zmdyy0318 wants to merge 1 commit into
MaaXYZ:mainfrom
zmdyy0318:fix/autostart-skip-rerun-after-update

Conversation

@zmdyy0318

@zmdyy0318 zmdyy0318 commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

close MaaEnd/MaaEnd#7160
修改前:通过命令行启动,直接运行任务->更新完成等待任务结束后重启->重启后重复运行已完成任务
修改后:通过命令行启动,直接运行任务->更新完成等待任务结束后重启->重启完成

问题

MaaEnd/MaaEnd#7160:用户用计划任务以 --autostart -i="日常" 启动,启动时检查到新版本并在后台下载。任务跑完、实例空闲后自动安装更新并重启,重启沿用原命令行参数,新进程再次进入自动执行分支,把同一批任务完整重跑了一遍。

原因:重启后在 consumeUpdateCompleteInfo() 的分支里,autostart 模式只跳过了更新完成弹窗,autoStartTasksPending 仍为 true,之后照常分发。代码无法区分以下两种更新重启:

场景 重启后是否应执行
A:启动时发现已下载的待安装更新 → 先安装 → 重启 应执行(本次尚未执行)
B:本次已执行完自动任务 → 空闲时自动安装 → 重启 不应执行(否则重复)

autoRunOnLaunch(手动启动时也自动执行)存在同样问题。

修改

  • store 新增 autoTasksRanInSession(仅内存,不持久化),记录本进程内自动任务是否已成功启动过。
  • App.tsx 将三处 autostart 分发合并为 dispatchAutoStartTasks(),onSettled(true) 时置位;启动失败不置位,重启后仍会补跑。
  • UpdateCompleteInfo 新增可选字段 autoTasksAlreadyRan,zip 自动安装重启前写入。
  • 重启后读到 autoTasksAlreadyRan 时跳过本次自动执行,并打日志 更新重启:更新前已执行过自动任务,跳过本次自动执行。
  • 场景 A、exe/dmg 安装器路径行为不变;旧版本写入的更新完成信息没有该字段,行为与之前一致。

测试

  • tsc --noEmit 通过,pnpm test 119 通过 / 1 跳过 / 0 失败。
  • 更新重启流程尚未实机验证。

AI 使用信息:

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

🤖 Generated with Claude Code

Sourcery 摘要

避免更新安装导致应用重启后,再次运行已完成的自动任务。

错误修复:

  • 如果自动任务在安装前已完成,则在更新触发重启后阻止其再次运行。

增强功能:

  • 跟踪当前会话中自动任务是否已成功启动,并集中处理自动启动模式下的自动任务调度。
  • 对尚未运行任务的更新重启,保留自动任务执行;同时维持基于安装程序的更新现有行为。
Original summary in English

Summary by Sourcery

Avoid rerunning completed automatic tasks after an update installation restarts the application.

Bug Fixes:

  • Prevent automatic tasks from running a second time after an update-triggered restart when they already completed before installation.

Enhancements:

  • Track whether automatic tasks were successfully started during the current session and centralize automatic-task dispatching for autostart modes.
  • Preserve automatic task execution for update restarts where tasks had not yet run, while retaining existing behavior for installer-based updates.

autostart / autoRunOnLaunch 的任务跑完后才触发自动安装,重启沿用原命令行参数,
新进程会再次分发同一批自动任务。记录本进程是否已成功启动过自动任务并写入更新完成信息,
重启后据此跳过自动执行;启动时先装挂起更新的场景不受影响。

Refs MaaEnd/MaaEnd#7160
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 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-07T14:51:30.139797Z 1bfc188 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.

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

嘿——我发现了 3 个问题

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

## Individual Comments

### Comment 1
<location path="src/components/InstallConfirmModal.tsx" line_range="119" />
<code_context>
           releaseNote: updateInfo.releaseNote,
           channel: updateInfo.channel,
           timestamp: Date.now(),
+          autoTasksAlreadyRan: useAppStore.getState().autoTasksRanInSession,
         });
       }
</code_context>
<issue_to_address>
**Automatic tasks run again after storage failure**

When writing the update-completion record to localStorage fails, `saveUpdateCompleteInfo` catches the write error, so `handleRestart` continues without saving `autoTasksAlreadyRan`; the relaunched app dispatches the same automatic tasks again.

Make `handleRestart` restart only after the completion record, including `autoTasksAlreadyRan`, has been saved successfully.

Also at `src/App.tsx:1104`.
</issue_to_address>

### Comment 2
<location path="src/components/InstallConfirmModal.tsx" line_range="119" />
<code_context>
           releaseNote: updateInfo.releaseNote,
           channel: updateInfo.channel,
           timestamp: Date.now(),
+          autoTasksAlreadyRan: useAppStore.getState().autoTasksRanInSession,
         });
       }
</code_context>
<issue_to_address>
**Interrupted installs repeat tasks**

When the process crashes in the interval after installation completes and before `handleRestart` saves the completion info, or the completion-info write fails, `autoTasksAlreadyRan` is saved only by `handleRestart` after `installUpdate` has completed; a crash after the update replaces the files but before that save leaves no marker, so the relaunched app dispatches the same automatic tasks again.

Persist the run marker before the update can replace the running application, and do not proceed with an automatic restart if that durable write fails.
</issue_to_address>

### Comment 3
<location path="src/components/InstallConfirmModal.tsx" line_range="119" />
<code_context>
           releaseNote: updateInfo.releaseNote,
           channel: updateInfo.channel,
           timestamp: Date.now(),
+          autoTasksAlreadyRan: useAppStore.getState().autoTasksRanInSession,
         });
       }
</code_context>
<issue_to_address>
**Interrupted tasks are not resumed**

When a background download completes while an automatic task is running and the user installs the update before that task finishes, `autoTasksRanInSession` becomes true as soon as the automatic task start succeeds, so installing a downloaded update while that task is still running records it as already run. The restart then skips dispatch and abandons the unfinished task batch.

Only persist the skip marker after the automatic task batch has completed, or prevent update installation and restart while that batch is running.
</issue_to_address>

Sourcery 对开源项目免费——如果您喜欢我们的审查结果,请考虑分享 ✨
Original comment in English

Hey - I've found 3 issues

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

## Individual Comments

### Comment 1
<location path="src/components/InstallConfirmModal.tsx" line_range="119" />
<code_context>
           releaseNote: updateInfo.releaseNote,
           channel: updateInfo.channel,
           timestamp: Date.now(),
+          autoTasksAlreadyRan: useAppStore.getState().autoTasksRanInSession,
         });
       }
</code_context>
<issue_to_address>
**Automatic tasks run again after storage failure**

When writing the update-completion record to localStorage fails, `saveUpdateCompleteInfo` catches the write error, so `handleRestart` continues without saving `autoTasksAlreadyRan`; the relaunched app dispatches the same automatic tasks again.

Make `handleRestart` restart only after the completion record, including `autoTasksAlreadyRan`, has been saved successfully.

Also at `src/App.tsx:1104`.
</issue_to_address>

### Comment 2
<location path="src/components/InstallConfirmModal.tsx" line_range="119" />
<code_context>
           releaseNote: updateInfo.releaseNote,
           channel: updateInfo.channel,
           timestamp: Date.now(),
+          autoTasksAlreadyRan: useAppStore.getState().autoTasksRanInSession,
         });
       }
</code_context>
<issue_to_address>
**Interrupted installs repeat tasks**

When the process crashes in the interval after installation completes and before `handleRestart` saves the completion info, or the completion-info write fails, `autoTasksAlreadyRan` is saved only by `handleRestart` after `installUpdate` has completed; a crash after the update replaces the files but before that save leaves no marker, so the relaunched app dispatches the same automatic tasks again.

Persist the run marker before the update can replace the running application, and do not proceed with an automatic restart if that durable write fails.
</issue_to_address>

### Comment 3
<location path="src/components/InstallConfirmModal.tsx" line_range="119" />
<code_context>
           releaseNote: updateInfo.releaseNote,
           channel: updateInfo.channel,
           timestamp: Date.now(),
+          autoTasksAlreadyRan: useAppStore.getState().autoTasksRanInSession,
         });
       }
</code_context>
<issue_to_address>
**Interrupted tasks are not resumed**

When a background download completes while an automatic task is running and the user installs the update before that task finishes, `autoTasksRanInSession` becomes true as soon as the automatic task start succeeds, so installing a downloaded update while that task is still running records it as already run. The restart then skips dispatch and abandons the unfinished task batch.

Only persist the skip marker after the automatic task batch has completed, or prevent update installation and restart while that batch is running.
</issue_to_address>

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

Comment thread src/components/InstallConfirmModal.tsx Resolved
Comment thread src/components/InstallConfirmModal.tsx Resolved
Comment thread src/components/InstallConfirmModal.tsx Resolved

@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: 1bfc18887a

ℹ️ 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/App.tsx 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.

疑似更新后重复执行autostart

1 participant