@leiyi-works/dsh-auto-restart 0.1.2 → 0.1.4
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.
- package/README.md +3 -1
- package/client.js +9 -0
- package/index.js +24 -39
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
## 它怎么重启
|
|
20
20
|
|
|
21
21
|
1. 等所有会话空闲(`api-session/status` + `agent/status`),默认最多等 2 分钟;
|
|
22
|
-
2.
|
|
22
|
+
2. 最后一次会话活动(发消息、回合开始/结束)之后再安静 10 秒,避免刚发出、回合还没起来的消息被重启吞掉、文字回到输入框;
|
|
23
|
+
3. 等插件管理器安静 3 秒(不打断进行中的安装/更新);
|
|
23
24
|
3. 由 WMI 起一个**脱离进程树、隐藏控制台**的 PowerShell 守护进程;
|
|
24
25
|
4. 守护进程等 `restartDelayMs`(默认 2 秒)→ **先杀 Electron 主进程**(否则 Host 先退出会让主进程弹「DeepSeek Harness 无法使用」并响一声)→ 清掉残留应用进程 → `Start-Process` 拉起应用 → 等主窗口出现 → 按 `maximizeOnRestart` 把窗口最大化 → 写回执。
|
|
25
26
|
|
|
@@ -59,6 +60,7 @@
|
|
|
59
60
|
| `maxPerHour` | `0` | 每小时上限;`0` = 不限制 |
|
|
60
61
|
| `waitForIdle` | `true` | 等会话空闲再重启 |
|
|
61
62
|
| `maxIdleWaitMs` | `120000` | 等空闲上限,超时放弃本次 |
|
|
63
|
+
| `idleSettleMs` | `10000` | 最后一次会话活动后安静多久再重启;防止刚发出、回合还没起来的消息被吞掉 |
|
|
62
64
|
| `forceWhenBusy` | `false` | 超时后是否强重启 |
|
|
63
65
|
| `restartDelayMs` | `2000` | 守护进程收到请求后等多久动手 |
|
|
64
66
|
| `maximizeOnRestart` | `true` | 重启后最大化主窗口 |
|
package/client.js
CHANGED
|
@@ -357,6 +357,15 @@ window.__ModuleLoader__.load({
|
|
|
357
357
|
suffix: '秒',
|
|
358
358
|
onChange: (value) => change({ maxIdleWaitMs: Math.max(0, Math.round(value)) * 1000 }),
|
|
359
359
|
}),
|
|
360
|
+
h(NumberRow, {
|
|
361
|
+
label: '空闲后安静',
|
|
362
|
+
hint: '最后一次会话活动之后安静这么久再重启,防止刚发出、回合还没起来的消息被吞掉。',
|
|
363
|
+
value: Math.round((config.idleSettleMs ?? 10000) / 1000),
|
|
364
|
+
min: 0,
|
|
365
|
+
max: 300,
|
|
366
|
+
suffix: '秒',
|
|
367
|
+
onChange: (value) => change({ idleSettleMs: Math.max(0, Math.round(value)) * 1000 }),
|
|
368
|
+
}),
|
|
360
369
|
h('p', { style: styles.note }, '改动即时保存、即时生效,不需要重启 DSH。'),
|
|
361
370
|
)
|
|
362
371
|
|
package/index.js
CHANGED
|
@@ -75,12 +75,13 @@ const DEFAULTS = Object.freeze({
|
|
|
75
75
|
waitForIdle: true,
|
|
76
76
|
/** 等空闲的最长时间(毫秒),超时按下面 forceWhenBusy 处理。 */
|
|
77
77
|
maxIdleWaitMs: 120000,
|
|
78
|
+
/**
|
|
79
|
+
* 最后一次会话活动(发消息、回合开始/结束)之后,再安静这么久才重启。
|
|
80
|
+
* 防止「刚按发送、回合还没起来」的瞬间被重启吞掉,导致输入框回填已发出的文字。
|
|
81
|
+
*/
|
|
82
|
+
idleSettleMs: 10000,
|
|
78
83
|
/** 等空闲超时后是否仍然强重启;默认 false(放弃本次,等下次变化)。 */
|
|
79
84
|
forceWhenBusy: false,
|
|
80
|
-
/** 这些标记文件存在时(相对 DSH_HOME),说明有后台任务在跑,先等它结束再重启。 */
|
|
81
|
-
busyMarkers: ['.cloud-sync/sync-busy'],
|
|
82
|
-
/** 等后台任务结束的最长时间(毫秒),超时按 forceWhenBusy 处理。 */
|
|
83
|
-
busyWaitMs: 600000,
|
|
84
85
|
/** 守护进程收到请求后等多久动手(毫秒),留时间给当前回合收尾。 */
|
|
85
86
|
restartDelayMs: 2000,
|
|
86
87
|
/** 重启后把主窗口最大化(开关在左侧「自动重启」面板里)。 */
|
|
@@ -113,16 +114,6 @@ function dshHome() {
|
|
|
113
114
|
return typeof env === 'string' && env.trim() !== '' ? env.trim() : join(homedir(), '.dsh')
|
|
114
115
|
}
|
|
115
116
|
|
|
116
|
-
function activeBusyMarkers(cfg) {
|
|
117
|
-
const found = []
|
|
118
|
-
for (const rel of cfg.busyMarkers) {
|
|
119
|
-
try {
|
|
120
|
-
if (existsSync(join(dshHome(), rel))) found.push(rel)
|
|
121
|
-
} catch { /* 忽略 */ }
|
|
122
|
-
}
|
|
123
|
-
return found
|
|
124
|
-
}
|
|
125
|
-
|
|
126
117
|
function dataDir() {
|
|
127
118
|
return join(dshHome(), 'dsh-auto-restart')
|
|
128
119
|
}
|
|
@@ -234,13 +225,6 @@ function sanitizeConfig(raw) {
|
|
|
234
225
|
cfg.dryRun = bool(raw.dryRun, cfg.dryRun)
|
|
235
226
|
cfg.waitForIdle = bool(raw.waitForIdle, cfg.waitForIdle)
|
|
236
227
|
cfg.forceWhenBusy = bool(raw.forceWhenBusy, cfg.forceWhenBusy)
|
|
237
|
-
cfg.busyWaitMs = num(raw.busyWaitMs, cfg.busyWaitMs, 0, 3600000)
|
|
238
|
-
if (Array.isArray(raw.busyMarkers)) {
|
|
239
|
-
cfg.busyMarkers = raw.busyMarkers
|
|
240
|
-
.filter((item) => typeof item === 'string' && item.trim() !== '')
|
|
241
|
-
.slice(0, 8)
|
|
242
|
-
.map((item) => item.trim())
|
|
243
|
-
}
|
|
244
228
|
cfg.maximizeOnRestart = bool(raw.maximizeOnRestart, cfg.maximizeOnRestart)
|
|
245
229
|
cfg.pollIntervalMs = num(raw.pollIntervalMs, cfg.pollIntervalMs, 500, 60000)
|
|
246
230
|
cfg.stableMs = num(raw.stableMs, cfg.stableMs, 0, 600000)
|
|
@@ -248,6 +232,7 @@ function sanitizeConfig(raw) {
|
|
|
248
232
|
cfg.cooldownMs = num(raw.cooldownMs, cfg.cooldownMs, 0, 3600000)
|
|
249
233
|
cfg.maxPerHour = num(raw.maxPerHour, cfg.maxPerHour, 0, 100)
|
|
250
234
|
cfg.maxIdleWaitMs = num(raw.maxIdleWaitMs, cfg.maxIdleWaitMs, 0, 3600000)
|
|
235
|
+
cfg.idleSettleMs = num(raw.idleSettleMs, cfg.idleSettleMs, 0, 600000)
|
|
251
236
|
cfg.restartDelayMs = num(raw.restartDelayMs, cfg.restartDelayMs, 0, 600000)
|
|
252
237
|
cfg.logMaxBytes = num(raw.logMaxBytes, cfg.logMaxBytes, 4096, 64 * 1024 * 1024)
|
|
253
238
|
for (const key of ['exePath', 'supervisorScript', 'powershellPath', 'profileDir']) {
|
|
@@ -549,7 +534,7 @@ function createState(profileDir) {
|
|
|
549
534
|
lastChangeAt: 0,
|
|
550
535
|
pendingSince: 0,
|
|
551
536
|
waitingSince: 0,
|
|
552
|
-
|
|
537
|
+
lastActivityAt: 0,
|
|
553
538
|
lastRestartAt: persisted.lastRestartAt,
|
|
554
539
|
restarts: persisted.restarts.filter((t) => now - t < 3600000),
|
|
555
540
|
lastManagerAt: 0,
|
|
@@ -626,23 +611,6 @@ function tick(baseConfig) {
|
|
|
626
611
|
return
|
|
627
612
|
}
|
|
628
613
|
|
|
629
|
-
const markers = activeBusyMarkers(cfg)
|
|
630
|
-
if (markers.length > 0) {
|
|
631
|
-
if (state.markerWaitingSince === 0) {
|
|
632
|
-
state.markerWaitingSince = now
|
|
633
|
-
logLine('info', `检测到进行中的云同步(${markers.join('、')}),等它结束后再重启`)
|
|
634
|
-
return
|
|
635
|
-
}
|
|
636
|
-
if (now - state.markerWaitingSince <= cfg.busyWaitMs) return
|
|
637
|
-
if (!cfg.forceWhenBusy) {
|
|
638
|
-
abortPending(state, `云同步进行了 ${Math.round(cfg.busyWaitMs / 1000)}s 仍未结束,本次跳过重启`)
|
|
639
|
-
return
|
|
640
|
-
}
|
|
641
|
-
logLine('warn', `云同步超过 ${Math.round(cfg.busyWaitMs / 1000)}s,forceWhenBusy=true,按配置强重启`)
|
|
642
|
-
} else if (state.markerWaitingSince !== 0) {
|
|
643
|
-
state.markerWaitingSince = 0
|
|
644
|
-
}
|
|
645
|
-
|
|
646
614
|
if (cfg.waitForIdle && state.busy.size > 0) {
|
|
647
615
|
if (state.waitingSince === 0) {
|
|
648
616
|
state.waitingSince = now
|
|
@@ -657,6 +625,16 @@ function tick(baseConfig) {
|
|
|
657
625
|
logLine('warn', `会话忙了 ${Math.round(cfg.maxIdleWaitMs / 1000)}s,forceWhenBusy=true,按配置强重启`)
|
|
658
626
|
}
|
|
659
627
|
|
|
628
|
+
// 最后一次会话活动之后必须安静够久:刚发出、回合还没起来的消息不能被重启吞掉。
|
|
629
|
+
const sinceActivity = state.lastActivityAt > 0 ? now - state.lastActivityAt : Number.POSITIVE_INFINITY
|
|
630
|
+
if (cfg.idleSettleMs > 0 && sinceActivity < cfg.idleSettleMs) {
|
|
631
|
+
if (now - state.lastSkipLog > 15000) {
|
|
632
|
+
state.lastSkipLog = now
|
|
633
|
+
logLine('info', `刚有会话活动(${Math.round(sinceActivity / 1000)}s 前),等安静 ${Math.round(cfg.idleSettleMs / 1000)}s 后再重启`)
|
|
634
|
+
}
|
|
635
|
+
return
|
|
636
|
+
}
|
|
637
|
+
|
|
660
638
|
dispatchRestart(state, cfg, '插件文件变化(装/更/改插件)')
|
|
661
639
|
}
|
|
662
640
|
|
|
@@ -838,6 +816,7 @@ export function apply(ctx, config) {
|
|
|
838
816
|
const id = String(sessionId)
|
|
839
817
|
if (running === false) state.busy.delete(id)
|
|
840
818
|
else state.busy.add(id)
|
|
819
|
+
state.lastActivityAt = Date.now()
|
|
841
820
|
}), 'auto-restart: session status')
|
|
842
821
|
|
|
843
822
|
ctx.effect(() => ctx.on('agent/status', (payload) => {
|
|
@@ -846,8 +825,14 @@ export function apply(ctx, config) {
|
|
|
846
825
|
const status = payload?.status
|
|
847
826
|
if (status === 'idle') state.busy.delete(String(id))
|
|
848
827
|
else if (ACTIVE_STATUSES.has(status)) state.busy.add(String(id))
|
|
828
|
+
state.lastActivityAt = Date.now()
|
|
849
829
|
}), 'auto-restart: agent status')
|
|
850
830
|
|
|
831
|
+
// User-authored messages: the turn may not have started yet, but the text was
|
|
832
|
+
// just sent, so a restart here would swallow it and restore the draft.
|
|
833
|
+
safeOn(ctx, 'api-session/activity', () => { state.lastActivityAt = Date.now() }, 'auto-restart: session activity')
|
|
834
|
+
safeOn(ctx, 'agent/inbox/inserted', () => { state.lastActivityAt = Date.now() }, 'auto-restart: inbox inserted')
|
|
835
|
+
|
|
851
836
|
// Plugin-manager activity: do not restart in the middle of an operation.
|
|
852
837
|
for (const event of ['plugin-manager/operation', 'plugin-manager/install-state', 'plugin-manager/update-state', 'plugin-manager/changed']) {
|
|
853
838
|
safeOn(ctx, event, () => { state.lastManagerAt = Date.now() }, `auto-restart: ${event}`)
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leiyi-works/dsh-auto-restart",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"title": "自动重启",
|
|
6
|
-
"description": "装/更插件后自动重启 DSH 并回到原会话:检测 profile
|
|
6
|
+
"description": "装/更插件后自动重启 DSH 并回到原会话:检测 profile 插件文件变化,等所有会话空闲且最近活动安静后,由脱离进程树的守护者先杀主进程再拉起(可最大化窗口),带冷却、试运行、日志和左侧「自动重启」面板。",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": "./index.js",
|