feat(electron): add idle mode and task-driven sleep prevention - #1561
Open
ConradLu2740 wants to merge 2 commits into
Open
feat(electron): add idle mode and task-driven sleep prevention#1561ConradLu2740 wants to merge 2 commits into
ConradLu2740 wants to merge 2 commits into
Conversation
added 2 commits
August 12, 2026 10:58
新增「挂机模式(息屏 + 防休眠)」:开启后立即熄灭显示器
(macOS pmset displaysleepnow / Windows SendMessage SC_MONITORPOWER /
Linux xset dpms force off),并通过 powerSaveBlocker
('prevent-app-suspension') 阻止系统休眠但允许息屏,适合挂机跑任务。
设置页新增开关,应用启动时同步、退出时自动释放。
Made-with: Proma
新增「任务防休眠」(默认开启):只要 Proma 有任务在跑(Agent run /
自动化任务 / 飞书桥接任务),通过 powerSaveBlocker
('prevent-app-suspension') 阻止系统休眠 —— 用户锁屏、息屏甚至合上
笔记本盖都不会中断任务;任务全部结束后自动恢复系统默认电源策略。
- task-sleep-guard.ts:token 集合跟踪活跃任务,纯逻辑可单测;
设置关闭时立即释放,重新开启时若仍有任务在跑则恢复
- agent-service.ts:runAgent / runAgentHeadless 挂钩 onRunStarted
begin / onComplete / onError / catch end,覆盖所有 run 入口
- 设置页新增开关(默认开启),与手动「挂机模式」相互独立
- macOS 合盖实测:任务运行期间合盖无睡眠事件,任务不中断
Made-with: Proma
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR 描述(中文版,覆盖两个功能:挂机模式 + 任务防休眠)
这个 PR 做了什么
面向「长任务无人值守」场景,新增两个睡眠管理能力(设置 → 通用):
powerSaveBlocker('prevent-app-suspension')阻止系统休眠 —— 用户锁屏、息屏、甚至合上笔记本盖都不会中断任务;任务全部结束后自动恢复系统默认电源策略。两者相互独立(各自持有独立的 powerSaveBlocker id),可叠加使用。
为什么做
feishu-sleep-blocker)只服务于飞书会话镜像,且没有任何「任务运行期间防休眠」的能力。实现方式
任务防休眠(默认开启)
apps/electron/src/main/lib/task-sleep-guard.ts—— 纯逻辑、不依赖 Electron,便于单测:powerSaveBlocker('prevent-app-suspension'),最后 1 个任务结束时释放;并发任务只持有一个 blocker。settings.taskSleepGuard?.enabled !== false;设置关闭时立即释放(任务继续跑但不再阻止休眠),重新开启时若仍有任务在跑则自动恢复。apps/electron/src/main/lib/task-sleep-guard-electron.ts—— Electron 接线层,导出taskSleepGuard单例与syncTaskSleepGuard/stopTaskSleepGuard。apps/electron/src/main/lib/agent-service.ts—— 挂钩所有 run 入口:runAgent(界面任务)与runAgentHeadless(飞书 Bridge、自动化任务等外部调用)都在onRunStarted时begin(token),在onComplete/onError/ 未处理异常时end(token);token 用闭包引用,重复 end 安全。types/settings.ts(TaskSleepGuardSettings)、settings-service.ts(默认{ enabled: true }+ sanitize)、ipc.ts(UPDATE/UPDATE_SYNC同步)、index.ts(启动同步、退出释放)、GeneralSettings.tsx(开关,带 pending 串行化防连点乱序)。挂机模式(手动,默认关闭)
apps/electron/src/main/lib/idle-mode.ts—— 平台无关核心:buildDisplayOffCommand(platform):macOSpmset displaysleepnow/ Windows 经-EncodedCommand的 PowerShellSendMessage(SC_MONITORPOWER)/ Linuxxset dpms force off;不支持的平台返回null,自动降级为「仅防休眠」。IdleModeManager:幂等启停 + generation 计数器,确保「关闭后仍在途的息屏命令」不产生误导反馈。apps/electron/src/main/lib/idle-mode-electron.ts—— Electron 接线层。IdleModeSettings,默认{ enabled: false })。平台支持
pmset displaysleepnowpowerSaveBlocker('prevent-app-suspension'),合盖实测有效SendMessage(SC_MONITORPOWER)经-EncodedCommand(规避 cmd.exe 引号剥离)xset dpms force off(尽力而为,Wayland 可能静默失败)息屏命令失败仅记录日志,绝不影响防休眠生效。唤醒屏幕是系统默认行为(鼠标/键盘)。
测试
bun test相关用例:task-sleep-guard.test.ts—— 11 个用例:并发任务、部分结束保持、重复 end 安全、设置关闭/重开恢复、退出清理、默认开启判断。idle-mode.test.ts—— 14 个用例:启停 / 幂等 / blocker 失效重建 / 息屏失败兜底 / 竞态 / 平台命令字符串(含 Windows 脚本 Base64 解码校验)。bun run typecheck—— 无错误。powerSaveBlocker生效/释放正常;合盖实测:合盖 10-15 秒期间系统零睡眠事件(pmset -g log佐证),开盖正常,任务不中断。pmset displaysleepnow真实熄灭显示器,鼠标/键盘唤醒正常;息屏后防休眠断言保持、停止后正确释放。备注 / 后续
xset;Wayland 用户可能需要wlr-randr等,暂不纳入本次范围。Made with Proma · GitHub