@leiyi-works/dsh-auto-restart 0.2.5 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +121 -114
  2. package/client.js +15 -0
  3. package/index.js +112 -5
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -1,115 +1,122 @@
1
- # dsh-auto-restart · 自动重启
2
-
3
- 装完/更新完插件后,不用再自己关掉 DSH 再打开:它会发现插件文件变了,等所有会话空闲,然后把整个桌面应用重启一次,回到原来的会话,**窗口默认最大化**。
4
-
5
- 只支持 **Windows 桌面端**(Electron 外壳 + Host 子进程的形态)。`dsh web` / headless 不会重启(插件会写日志说明不支持)。
6
-
7
- 左侧菜单有一个「自动重启」入口:点一下先回到会话,再在**右侧原生侧边栏**打开面板(和「临时目录」等插件一致,不遮挡会话);状态、上次重启回执、开关与参数都在那里改,改完即时生效。右侧边栏不可用时才退回全页面板。
8
-
9
- ## 它怎么判断"插件变了"
10
-
11
- 盯着当前 profile 的这几处,每 2 秒取一次指纹:
12
-
13
- - `node_modules/` 顶层条目,以及 `@scope/` 下面的条目(时间、大小、增删)
14
- - `node_modules-link/`(如果存在)
15
- - `package.json` 里的 `dependencies` 与 `dsh.profile.bundles`(内容签名)
16
-
17
- 指纹变了并且**稳定 10 秒**没再变,才算一次安装/更新结束。这样 dshmarket、官方插件管理器、手动 `pnpm add`、改自己插件的文件都能覆盖到,而云同步恢复同样的内容(内容签名不变)不会误触发。
18
-
19
- ## 它怎么重启
20
-
21
- 1. 等所有会话空闲(`api-session/status` + `agent/status`),**一直等、没有上限**;
22
- 2. 最后一次会话活动(发消息、回合开始/结束)之后再安静 10 秒,避免刚发出、回合还没起来的消息被重启吞掉、文字回到输入框;
23
- 3. 等插件管理器安静 3 秒(不打断进行中的安装/更新);
24
- 4. **优先交给常驻后台看门狗**:看门狗只在登录 Windows 时由「启动文件夹」里的引线自动启动,插件把请求写进 `~/.dsh/dsh-auto-restart/watchdog-request.json`,由它先杀 Electron 主进程、清残留、拉起应用并最大化——**不碰 WMI,也不会触发安全软件的 WMI 提示**;
25
- 5. 看门狗没在跑时(未安装/被关掉)才退回 WMI 守护进程:写一个隐藏窗口的快捷方式,交给 explorer.exe 启动守护进程(脱离 DSH 的 job object),由它完成同样的「先杀主进程 → 清残留 → 拉起 → 最大化」。
26
-
27
- ### 后台看门狗(零弹窗模式)
28
-
29
- 首次启动时插件会检测:没装就自动弹一个**中文控制台窗口**,说明用途并等你按回车安装。安装做三件事:把脚本拷到 `~/.dsh/dsh-auto-restart/`、在启动文件夹写一个隐藏引线、立刻启动看门狗。之后每次登录 Windows 它自动起来,所有自动重启都不再经过 WMI。
30
-
31
- - 面板里只显示看门狗状态(运行中/未安装/已安装无心跳/脚本已更新待重装),**没有安装/卸载按钮**;
32
- - **每次 DSH 启动(含自动重启后)体检一次**:看门狗没在回答(进程没了/心跳过期)就自动打开安装窗口;不再做周期检查(避免工作中反复弹窗);这期间自动重启会退回 WMI 兜底;
33
- - **脚本更新检测**:心跳里带脚本哈希;插件升级后如果后台还跑着旧脚本,会再弹一次安装窗口把看门狗重启到新脚本(安装器会先停掉旧进程);
1
+ # dsh-auto-restart · 自动重启
2
+
3
+ 装完/更新完插件后,不用再自己关掉 DSH 再打开:它会发现插件文件变了,等所有会话空闲,然后把整个桌面应用重启一次,回到原来的会话,**窗口默认最大化**。
4
+
5
+ 只支持 **Windows 桌面端**(Electron 外壳 + Host 子进程的形态)。`dsh web` / headless 不会重启(插件会写日志说明不支持)。
6
+
7
+ 左侧菜单有一个「自动重启」入口:点一下先回到会话,再在**右侧原生侧边栏**打开面板(和「临时目录」等插件一致,不遮挡会话);状态、上次重启回执、开关与参数都在那里改,改完即时生效。右侧边栏不可用时才退回全页面板。
8
+
9
+ ## 它怎么判断"插件变了"
10
+
11
+ 盯着当前 profile 的这几处,每 2 秒取一次指纹:
12
+
13
+ - `node_modules/` 顶层条目,以及 `@scope/` 下面的条目(时间、大小、增删)
14
+ - `node_modules-link/`(如果存在)
15
+ - `package.json` 里的 `dependencies` 与 `dsh.profile.bundles`(内容签名)
16
+
17
+ 指纹变了并且**稳定 10 秒**没再变,才算一次安装/更新结束。这样 dshmarket、官方插件管理器、手动 `pnpm add`、改自己插件的文件都能覆盖到,而云同步恢复同样的内容(内容签名不变)不会误触发。
18
+
19
+ ## 它怎么重启
20
+
21
+ 1. 等所有会话空闲(`api-session/status` + `agent/status`):最多等 `maxIdleWaitMs`(默认 **10 分钟**),超时就按计划重启并记录被中断的会话;设 `0` 表示一直等;
22
+ 2. 最后一次会话活动(发消息、回合开始/结束)之后再安静 10 秒,避免刚发出、回合还没起来的消息被重启吞掉、文字回到输入框;
23
+ 3. 等插件管理器安静 3 秒(不打断进行中的安装/更新);
24
+ 4. **优先交给常驻后台看门狗**:看门狗只在登录 Windows 时由「启动文件夹」里的引线自动启动,插件把请求写进 `~/.dsh/dsh-auto-restart/watchdog-request.json`,由它先杀 Electron 主进程、清残留、拉起应用并最大化——**不碰 WMI,也不会触发安全软件的 WMI 提示**;
25
+ 5. 看门狗没在跑时(未安装/被关掉)才退回 WMI 守护进程:写一个隐藏窗口的快捷方式,交给 explorer.exe 启动守护进程(脱离 DSH 的 job object),由它完成同样的「先杀主进程 → 清残留 → 拉起 → 最大化」。
26
+
27
+ ### 后台看门狗(零弹窗模式)
28
+
29
+ 首次启动时插件会检测:没装就自动弹一个**中文控制台窗口**,说明用途并等你按回车安装。安装做三件事:把脚本拷到 `~/.dsh/dsh-auto-restart/`、在启动文件夹写一个隐藏引线、立刻启动看门狗。之后每次登录 Windows 它自动起来,所有自动重启都不再经过 WMI。
30
+
31
+ - 面板里只显示看门狗状态(运行中/未安装/已安装无心跳/脚本已更新待重装),**没有安装/卸载按钮**;
32
+ - **每次 DSH 启动(含自动重启后)体检一次**:看门狗没在回答(进程没了/心跳过期)就自动打开安装窗口;不再做周期检查(避免工作中反复弹窗);这期间自动重启会退回 WMI 兜底;
33
+ - **脚本更新检测**:心跳里带脚本哈希;插件升级后如果后台还跑着旧脚本,会再弹一次安装窗口把看门狗重启到新脚本(安装器会先停掉旧进程);
34
34
  - 急停(可选):在 `~/.dsh/dsh-auto-restart/` 里新建空文件 `disabled`,插件就不再动手(这和直接在插件列表里停用插件基本等价,留着只是应急用);删掉即恢复;
35
- - 卸载:在 `~/.dsh/dsh-auto-restart/` 里执行 `powershell -File install-watchdog.ps1 -Uninstall`;
36
- - 看门狗日志:`~/.dsh/dsh-auto-restart/watchdog.log`。
37
-
38
- ### 多机同步注意
39
-
40
- - 看门狗本体和安装脚本在 `~/.dsh/dsh-auto-restart/`,会跟着云端同步走;**启动文件夹的引线不在 `~/.dsh` 里,不会被同步**——所以每台机器第一次都会弹安装窗口,各自装一次即可(路径全部运行时由 `%USERPROFILE%` / `%APPDATA%` 计算,不同用户名不影响);
41
- - 其它机器同步过来的旧重启请求(`watchdog-request.json` / `restart-request.json`)会在启动时按 exe 路径校验后清理,不会被本机看门狗误执行;
42
- - `disabled`(急停文件)和 `config.json` 会同步:一处急停等于全机队急停、一处改配置全机队生效;如果不想要这个行为,把这两个文件加进云端同步的排除项。
43
-
44
- > 少数安全软件(如 360)对「PowerShell 操作 WMI」有行为规则:装了看门狗后重启不走 WMI,就不会再因此弹窗;安装看门狗那一次仍可能被提示一次,允许即可。
45
-
46
- 会话记录在磁盘上,重启后回到同一个会话。**没有每小时次数上限**,只有 60 秒冷却防止连环重启。
47
-
48
- ## 安装后第一次
49
-
50
- 第一次安装需要手动重启一次 DSH(托盘「退出」再打开),让 Host 半边的代码加载。之后就不用再管了。
51
-
52
- **首次默认 `dryRun: true`**:只写日志、不真重启。确认日志里看到「检测到插件文件变化」和「[dry-run] 本应重启」之后,在左侧「自动重启」面板里关掉试运行即可(也可以在 `config.json` 里把 `dryRun` 改成 `false`,改完即时生效,不用重启)。
53
-
54
- 配置文件:`%USERPROFILE%\.dsh\dsh-auto-restart\config.json`(也可以放 `~/...` 形式的可移植路径)。
55
-
56
- ## 左侧面板
57
-
58
- | 控件 | 说明 |
59
- |---|---|
60
- | 启用自动重启 | 总开关 |
61
- | 试运行(dryRun) | 只记日志不重启 |
62
- | 重启后最大化窗口 | 守护进程拉起应用后把主窗口最大化 |
63
- | 等所有会话空闲再重启 | 有回答在跑时一直等,没有上限 |
64
- | 文件稳定判定 | 默认 10 秒 |
65
- | 两次重启冷却 | 默认 60 秒 |
66
- | 空闲后安静 | 默认 10 秒 |
67
- | 立即重启 | 手动触发一次(有会话在跑时会被拒绝) |
68
-
69
- ## 配置
70
-
71
- | 键 | 默认 | 说明 |
72
- |---|---|---|
73
- | `enabled` | `true` | 总开关 |
74
- | `dryRun` | `true` | 只记日志不重启;验证后改 `false` |
75
- | `stableMs` | `10000` | 文件停止变化多久算安装完成 |
76
- | `pollIntervalMs` | `2000` | 轮询间隔 |
77
- | `managerQuietMs` | `3000` | 插件管理器最后一次活动后至少安静多久 |
78
- | `cooldownMs` | `60000` | 两次重启最小间隔(跨重启持久化) |
79
- | `maxPerHour` | `0` | 每小时上限;`0` = 不限制 |
80
- | `waitForIdle` | `true` | 等会话空闲再重启 |
81
- | `idleSettleMs` | `10000` | 最后一次会话活动后安静多久再重启;防止刚发出、回合还没起来的消息被吞掉 |
82
- | `restartDelayMs` | `2000` | 守护进程收到请求后等多久动手 |
83
- | `maximizeOnRestart` | `true` | 重启后最大化主窗口 |
84
- | `exePath` | `''` | 应用可执行文件,留空自动推导 |
85
- | `launchArgs` | `[]` | 拉起应用时的附加参数 |
86
- | `supervisorScript` | `''` | 自定义守护脚本路径 |
87
- | `powershellPath` | `''` | 自定义 powershell.exe |
88
- | `profileDir` | `''` | 自定义 profile 目录 |
89
- | `logMaxBytes` | `1048576` | 日志滚动阈值 |
90
-
91
- ## 急停
92
-
93
- 在 `%USERPROFILE%\.dsh\dsh-auto-restart\` 下新建一个空文件 `disabled`,它立刻什么都不做(每 2 秒检查一次)。删掉就恢复。
94
-
95
- ## 状态与日志
96
-
97
- - 日志:`%USERPROFILE%\.dsh\dsh-auto-restart\auto-restart.log`
98
- - 上次重启回执:`restart-result.json`;上次请求:`last-restart.json`
99
- - 状态:`GET http://127.0.0.1:<Host 端口>/auto-restart/status`(只接受本机回环)
100
- - 配置:`GET/POST /auto-restart/config`
101
-
102
- ## 卸载
103
-
104
- ```bash
105
- dsh plugin --profile desktop remove @leiyi-works/dsh-auto-restart
106
- ```
107
-
108
- `%USERPROFILE%\.dsh\dsh-auto-restart\` 里的日志和配置可以留也可以删。
109
-
110
- ## 边界
111
-
112
- - 只重启应用,不动其它进程。
113
- - 正在跑的任务会被重启打断——所以一直等它空闲,不会硬切;等多久都不会放弃,直到空闲。
114
- - 不碰 `app.asar`,DSH 升级不受影响。
115
- - 桌面端正式构建没有可用重启入口(官方菜单项只在 development 构建出现),所以只能用外部守护进程的方式;这也是为什么需要一次引导重启。
35
+ - 卸载:在 `~/.dsh/dsh-auto-restart/` 里执行 `powershell -File install-watchdog.ps1 -Uninstall`;
36
+ - 看门狗日志:`~/.dsh/dsh-auto-restart/watchdog.log`。
37
+
38
+ ### 多机同步注意
39
+
40
+ - 看门狗本体和安装脚本在 `~/.dsh/dsh-auto-restart/`,会跟着云端同步走;**启动文件夹的引线不在 `~/.dsh` 里,不会被同步**——所以每台机器第一次都会弹安装窗口,各自装一次即可(路径全部运行时由 `%USERPROFILE%` / `%APPDATA%` 计算,不同用户名不影响);
41
+ - 其它机器同步过来的旧重启请求(`watchdog-request.json` / `restart-request.json`)会在启动时按 exe 路径校验后清理,不会被本机看门狗误执行;
42
+ - `disabled`(急停文件)和 `config.json` 会同步:一处急停等于全机队急停、一处改配置全机队生效;如果不想要这个行为,把这两个文件加进云端同步的排除项。
43
+
44
+ > 少数安全软件(如 360)对「PowerShell 操作 WMI」有行为规则:装了看门狗后重启不走 WMI,就不会再因此弹窗;安装看门狗那一次仍可能被提示一次,允许即可。
45
+
46
+ 会话记录在磁盘上,重启后回到同一个会话。
47
+
48
+ ### 重启后自动继续
49
+
50
+ 被重启打断的会话(重启派发时正忙的那些)会在新进程启动后各收到一条 **「继续」**:用宿主服务 `sessionController.prompt()` 作为用户消息发回去。可通过 `continueInterrupted` 关闭、`continueText` 改文字;在等用户做选择/授权的会话(`pendingInput` 非空)会跳过,避免替你回答。适合"等 npm 发布窗口/等版本对齐"这类后台等待任务。**没有每小时次数上限**,只有 60 秒冷却防止连环重启。
51
+
52
+ ## 安装后第一次
53
+
54
+ 第一次安装需要手动重启一次 DSH(托盘「退出」再打开),让 Host 半边的代码加载。之后就不用再管了。
55
+
56
+ **首次默认 `dryRun: true`**:只写日志、不真重启。确认日志里看到「检测到插件文件变化」和「[dry-run] 本应重启」之后,在左侧「自动重启」面板里关掉试运行即可(也可以在 `config.json` 里把 `dryRun` 改成 `false`,改完即时生效,不用重启)。
57
+
58
+ 配置文件:`%USERPROFILE%\.dsh\dsh-auto-restart\config.json`(也可以放 `~/...` 形式的可移植路径)。
59
+
60
+ ## 左侧面板
61
+
62
+ | 控件 | 说明 |
63
+ |---|---|
64
+ | 启用自动重启 | 总开关 |
65
+ | 试运行(dryRun) | 只记日志不重启 |
66
+ | 重启后最大化窗口 | 守护进程拉起应用后把主窗口最大化 |
67
+ | 等所有会话空闲再重启 | 有回答在跑时一直等,没有上限 |
68
+ | 文件稳定判定 | 默认 10 秒 |
69
+ | 两次重启冷却 | 默认 60 秒 |
70
+ | 空闲后安静 | 默认 10 秒 |
71
+ | 立即重启 | 手动触发一次(有会话在跑时会被拒绝) |
72
+
73
+ ## 配置
74
+
75
+ | 键 | 默认 | 说明 |
76
+ |---|---|---|
77
+ | `enabled` | `true` | 总开关 |
78
+ | `dryRun` | `true` | 只记日志不重启;验证后改 `false` |
79
+ | `stableMs` | `10000` | 文件停止变化多久算安装完成 |
80
+ | `pollIntervalMs` | `2000` | 轮询间隔 |
81
+ | `managerQuietMs` | `3000` | 插件管理器最后一次活动后至少安静多久 |
82
+ | `cooldownMs` | `60000` | 两次重启最小间隔(跨重启持久化) |
83
+ | `maxPerHour` | `0` | 每小时上限;`0` = 不限制 |
84
+ | `waitForIdle` | `true` | 等会话空闲再重启 |
85
+ | `idleSettleMs` | `10000` | 最后一次会话活动后安静多久再重启;防止刚发出、回合还没起来的消息被吞掉 |
86
+ | `maxIdleWaitMs` | `600000` | 等空闲最多等多久,超过就按计划重启(0 = 一直等) |
87
+ | `continueInterrupted` | `true` | 重启后是否给被中断的会话自动发"继续" |
88
+ | `continueText` | `继续` | 自动继续发送的文本 |
89
+ | `restartDelayMs` | `2000` | 守护进程收到请求后等多久动手 |
90
+ | `maximizeOnRestart` | `true` | 重启后最大化主窗口 |
91
+ | `exePath` | `''` | 应用可执行文件,留空自动推导 |
92
+ | `launchArgs` | `[]` | 拉起应用时的附加参数 |
93
+ | `supervisorScript` | `''` | 自定义守护脚本路径 |
94
+ | `powershellPath` | `''` | 自定义 powershell.exe |
95
+ | `profileDir` | `''` | 自定义 profile 目录 |
96
+ | `logMaxBytes` | `1048576` | 日志滚动阈值 |
97
+
98
+ ## 急停
99
+
100
+ 在 `%USERPROFILE%\.dsh\dsh-auto-restart\` 下新建一个空文件 `disabled`,它立刻什么都不做(每 2 秒检查一次)。删掉就恢复。
101
+
102
+ ## 状态与日志
103
+
104
+ - 日志:`%USERPROFILE%\.dsh\dsh-auto-restart\auto-restart.log`
105
+ - 上次重启回执:`restart-result.json`;上次请求:`last-restart.json`
106
+ - 状态:`GET http://127.0.0.1:<Host 端口>/auto-restart/status`(只接受本机回环)
107
+ - 配置:`GET/POST /auto-restart/config`
108
+
109
+ ## 卸载
110
+
111
+ ```bash
112
+ dsh plugin --profile desktop remove @leiyi-works/dsh-auto-restart
113
+ ```
114
+
115
+ `%USERPROFILE%\.dsh\dsh-auto-restart\` 里的日志和配置可以留也可以删。
116
+
117
+ ## 边界
118
+
119
+ - 只重启应用,不动其它进程。
120
+ - 正在跑的任务会被重启打断——所以一直等它空闲,不会硬切;等多久都不会放弃,直到空闲。
121
+ - 不碰 `app.asar`,DSH 升级不受影响。
122
+ - 桌面端正式构建没有可用重启入口(官方菜单项只在 development 构建出现),所以只能用外部守护进程的方式;这也是为什么需要一次引导重启。
package/client.js CHANGED
@@ -312,6 +312,21 @@ window.__ModuleLoader__.load({
312
312
  checked: config.waitForIdle !== false,
313
313
  onChange: (value) => change({ waitForIdle: value }),
314
314
  }),
315
+ h(NumberRow, {
316
+ label: '等空闲上限',
317
+ hint: '忙超过这么久就按计划重启,不再无限等(重启后自动给被中断的会话发“继续”);0 表示一直等。',
318
+ value: Math.round((config.maxIdleWaitMs ?? 600000) / 60000),
319
+ min: 0,
320
+ max: 240,
321
+ suffix: '分钟',
322
+ onChange: (value) => change({ maxIdleWaitMs: Math.max(0, Math.round(value)) * 60000 }),
323
+ }),
324
+ h(SwitchRow, {
325
+ label: '重启后自动继续',
326
+ hint: '给被这次重启打断的会话自动发一条“继续”;在等用户做选择/授权的会话会跳过。',
327
+ checked: config.continueInterrupted !== false,
328
+ onChange: (value) => change({ continueInterrupted: value }),
329
+ }),
315
330
  h(NumberRow, {
316
331
  label: '空闲后安静',
317
332
  hint: '最后一次会话活动之后安静这么久再重启,防止刚发出、回合还没起来的消息被吞掉。',
package/index.js CHANGED
@@ -74,6 +74,14 @@ const DEFAULTS = Object.freeze({
74
74
  maxPerHour: 0,
75
75
  /** 有会话在跑时一直等它们空闲(没有上限,不会硬切)。 */
76
76
  waitForIdle: true,
77
+ /** 等空闲最多等多久;超过就按计划重启(重启后会给被中断的会话发“继续”);0 = 一直等。 */
78
+ maxIdleWaitMs: 600000,
79
+ /** 重启后是否自动给被中断的会话发一条“继续”。 */
80
+ continueInterrupted: true,
81
+ /** 自动继续时发送的文本。 */
82
+ continueText: '继续',
83
+ /** 多久以内的中断记录还值得自动继续(毫秒)。 */
84
+ continueWindowMs: 1800000,
77
85
  /**
78
86
  * 最后一次会话活动(发消息、回合开始/结束)之后,再安静这么久才重启。
79
87
  * 防止「刚按发送、回合还没起来」的瞬间被重启吞掉,导致输入框回填已发出的文字。
@@ -160,6 +168,90 @@ function watchdogLogPath() {
160
168
  return join(dataDir(), 'watchdog.log')
161
169
  }
162
170
 
171
+ /** Sessions that were busy when a restart was dispatched (for auto-continue). */
172
+ function interruptedPath() {
173
+ return join(dataDir(), 'interrupted.json')
174
+ }
175
+
176
+ function recordInterrupted(state, reason) {
177
+ const file = interruptedPath()
178
+ const ids = [...state.busy]
179
+ if (ids.length === 0) {
180
+ try { rmSync(file, { force: true }) } catch {}
181
+ return
182
+ }
183
+ try {
184
+ writeJsonAtomic(file, {
185
+ at: new Date().toISOString(),
186
+ reason,
187
+ ids,
188
+ exe: resolveExe(readConfig({})),
189
+ })
190
+ logLine('info', `已记录 ${ids.length} 个可能被中断的会话(重启后自动发“继续”):${ids.map((id) => String(id).slice(0, 18)).join(', ')}`)
191
+ } catch (error) {
192
+ logLine('warn', `记录被中断会话失败:${String(error?.message ?? error)}`)
193
+ }
194
+ }
195
+
196
+ /**
197
+ * After a restart, tell the Sessions that were interrupted to keep going.
198
+ * Waiting-for-input sessions are skipped: resuming those would answer a question
199
+ * the user never answered.
200
+ */
201
+ async function resumeInterrupted(ctx, cfg) {
202
+ const file = interruptedPath()
203
+ const record = readJsonFile(file)
204
+ if (record === null) return
205
+ try {
206
+ if (cfg.continueInterrupted === false || !Array.isArray(record.ids) || record.ids.length === 0) {
207
+ try { rmSync(file, { force: true }) } catch {}
208
+ return
209
+ }
210
+ const at = Date.parse(String(record.at))
211
+ const age = Date.now() - at
212
+ if (!Number.isFinite(at) || age < 0 || age > cfg.continueWindowMs) {
213
+ try { rmSync(file, { force: true }) } catch {}
214
+ logLine('info', '上次的中断记录太久远,忽略自动继续')
215
+ return
216
+ }
217
+ let controller = null
218
+ try { controller = ctx.get('sessionController') } catch { controller = null }
219
+ if (!controller || typeof controller.prompt !== 'function') {
220
+ logLine('warn', '拿不到 sessionController,无法自动给被中断的会话发“继续”')
221
+ return
222
+ }
223
+ for (let index = 0; index < record.ids.length; index += 1) {
224
+ const sessionId = String(record.ids[index])
225
+ const short = sessionId.slice(0, 18)
226
+ try {
227
+ let sessionState = null
228
+ try { sessionState = await controller.state({ sessionId }) } catch {}
229
+ if (sessionState && sessionState.running === true) {
230
+ logLine('info', `跳过自动继续 ${short}:会话已经在跑`)
231
+ continue
232
+ }
233
+ if (sessionState && Array.isArray(sessionState.pendingInput) && sessionState.pendingInput.length > 0) {
234
+ logLine('info', `跳过自动继续 ${short}:会话在等用户输入(${sessionState.pendingInput.join('/')})`)
235
+ continue
236
+ }
237
+ await controller.prompt({
238
+ requestId: `auto-restart-continue-${Date.now()}-${index}`,
239
+ sessionId,
240
+ mode: 'queue',
241
+ content: [{ type: 'text', text: String(cfg.continueText || '继续') }],
242
+ clientTimeZone: 'Asia/Shanghai',
243
+ })
244
+ logLine('info', `已给被中断的会话发“${cfg.continueText}”:${short}`)
245
+ } catch (error) {
246
+ logLine('warn', `给 ${short} 发“继续”失败:${String(error?.message ?? error)}`)
247
+ }
248
+ }
249
+ try { rmSync(file, { force: true }) } catch {}
250
+ } catch (error) {
251
+ logLine('warn', `自动继续失败:${String(error?.message ?? error)}`)
252
+ }
253
+ }
254
+
163
255
  function startupEntryPath() {
164
256
  const appData = process.env.APPDATA
165
257
  if (typeof appData !== 'string' || appData.trim() === '') return ''
@@ -217,7 +309,7 @@ function readWatchdogHeartbeat() {
217
309
  */
218
310
  function watchdogScriptStale(heartbeat) {
219
311
  const current = watchdogScriptHash()
220
- return heartbeat !== null && current !== '' && heartbeat.script !== '' && heartbeat.script !== current
312
+ return heartbeat !== null && current !== '' && heartbeat.script !== current
221
313
  }
222
314
 
223
315
  function watchdogAlive() {
@@ -429,6 +521,10 @@ function sanitizeConfig(raw) {
429
521
  cfg.enabled = bool(raw.enabled, cfg.enabled)
430
522
  cfg.dryRun = bool(raw.dryRun, cfg.dryRun)
431
523
  cfg.waitForIdle = bool(raw.waitForIdle, cfg.waitForIdle)
524
+ cfg.maxIdleWaitMs = num(raw.maxIdleWaitMs, cfg.maxIdleWaitMs, 0, 86400000)
525
+ cfg.continueInterrupted = bool(raw.continueInterrupted, cfg.continueInterrupted)
526
+ cfg.continueWindowMs = num(raw.continueWindowMs, cfg.continueWindowMs, 60000, 86400000)
527
+ if (typeof raw.continueText === 'string' && raw.continueText.trim() !== '') cfg.continueText = raw.continueText.slice(0, 200)
432
528
  cfg.maximizeOnRestart = bool(raw.maximizeOnRestart, cfg.maximizeOnRestart)
433
529
  cfg.pollIntervalMs = num(raw.pollIntervalMs, cfg.pollIntervalMs, 500, 60000)
434
530
  cfg.stableMs = num(raw.stableMs, cfg.stableMs, 0, 600000)
@@ -648,6 +744,7 @@ function dispatchRestart(state, cfg, reason, options = {}) {
648
744
  // Preferred path: a resident watchdog started outside DSH's process tree; it
649
745
  // performs the restart without any WMI call. Falls back to the WMI launcher
650
746
  // when the watchdog is not running.
747
+ recordInterrupted(state, reason)
651
748
  if (watchdogAlive()) return dispatchToWatchdog(state, cfg, reason)
652
749
 
653
750
  const delay = Math.max(0, Number(cfg.restartDelayMs) || 0)
@@ -823,11 +920,16 @@ function tick(baseConfig) {
823
920
  }
824
921
 
825
922
  if (cfg.waitForIdle && state.busy.size > 0) {
826
- if (state.waitingSince === 0) {
827
- state.waitingSince = now
828
- logLine('info', `有 ${state.busy.size} 个会话在跑,一直等它们空闲后再重启`)
923
+ const busyForMs = state.waitingSince > 0 ? now - state.waitingSince : 0
924
+ const overLimit = cfg.maxIdleWaitMs > 0 && busyForMs >= cfg.maxIdleWaitMs
925
+ if (!overLimit) {
926
+ if (state.waitingSince === 0) {
927
+ state.waitingSince = now
928
+ logLine('info', `有 ${state.busy.size} 个会话在跑,等它们空闲后再重启(最多等 ${cfg.maxIdleWaitMs > 0 ? Math.round(cfg.maxIdleWaitMs / 60000) + ' 分钟' : '一直等'})`)
929
+ }
930
+ return
829
931
  }
830
- return
932
+ logLine('warn', `等空闲已超过 ${Math.round(cfg.maxIdleWaitMs / 60000)} 分钟(${state.busy.size} 个会话还在跑),按计划重启;重启后会给它们发“继续”`)
831
933
  }
832
934
  state.waitingSince = 0
833
935
 
@@ -925,6 +1027,9 @@ function installRoutes(ctx, baseConfig) {
925
1027
  logPath: watchdogLogPath(),
926
1028
  },
927
1029
  waitForIdle: cfg.waitForIdle,
1030
+ maxIdleWaitMs: cfg.maxIdleWaitMs,
1031
+ continueInterrupted: cfg.continueInterrupted,
1032
+ continueText: cfg.continueText,
928
1033
  busySessions: state ? [...state.busy] : [],
929
1034
  pending: state ? state.pendingSince > 0 : false,
930
1035
  baseline: state?.baseline?.slice(0, 10) ?? null,
@@ -1040,6 +1145,8 @@ export function apply(ctx, config) {
1040
1145
  ensureWatchdogFiles()
1041
1146
  cleanForeignRequests()
1042
1147
  bootWatchdogCheck(state)
1148
+ // Sessions that a previous restart interrupted get one "继续" after boot.
1149
+ void resumeInterrupted(ctx, cfg)
1043
1150
 
1044
1151
  // Track busy sessions so a restart never cuts an answer.
1045
1152
  ctx.effect(() => ctx.on('api-session/status', (sessionId, running) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leiyi-works/dsh-auto-restart",
3
- "version": "0.2.5",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "title": "自动重启",
6
6
  "description": "装/更插件后自动重启 DSH 并回到原会话:检测 profile 插件文件变化,一直等所有会话空闲且最近活动安静,由常驻后台看门狗(登录自启、不碰 WMI)或 WMI 守护先杀主进程再拉起(可最大化窗口),带冷却、试运行、日志和右侧原生侧边栏面板。",