@leiyi-works/dsh-auto-restart 0.3.2 → 0.3.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 CHANGED
@@ -18,7 +18,7 @@
18
18
 
19
19
  ## 它怎么重启
20
20
 
21
- 1. 等所有会话空闲(`api-session/status` + `agent/status`):最多等 `maxIdleWaitMs`(默认 **10 分钟**),超时就按计划重启并记录被中断的会话;设 `0` 表示一直等;
21
+ 1. 等所有会话空闲(`api-session/status` + `agent/status`):正在回答/干活的会一直等;**只卡在后台任务(`job_*`)上的会话不算忙**——这些 job 随 Host 被杀、不会自动恢复,所以重启会直接打断它们并在重启后发「继续」;停在选择题/审批上的会话永远不打断,答完且任务跑完才重新计 10 秒;
22
22
  2. 最后一次会话活动(发消息、回合开始/结束)之后再安静 10 秒,避免刚发出、回合还没起来的消息被重启吞掉、文字回到输入框;
23
23
  3. 等插件管理器安静 3 秒(不打断进行中的安装/更新);
24
24
  4. **优先交给常驻后台看门狗**:看门狗只在登录 Windows 时由「启动文件夹」里的引线自动启动,插件把请求写进 `~/.dsh/dsh-auto-restart/watchdog-request.json`,由它先杀 Electron 主进程、清残留、拉起应用并最大化——**不碰 WMI,也不会触发安全软件的 WMI 提示**;
package/client.js CHANGED
@@ -308,19 +308,10 @@ window.__ModuleLoader__.load({
308
308
  }),
309
309
  h(SwitchRow, {
310
310
  label: '等所有会话空闲再重启',
311
- hint: '有回答或任务在跑时一直等它们结束,避免截断。',
311
+ hint: '正在回答/干活的会话会等它们结束;只卡在后台任务(job_*,如 npm 轮询、长命令)上的会话不算忙,会被本次重启打断并在重启后收到“继续”。',
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
315
  h(SwitchRow, {
325
316
  label: '重启后自动继续',
326
317
  hint: '只给被后台任务(job_*,如 npm 轮询、长命令)打断的会话自动发“继续”;正在回答或等用户选择/授权的会话不动。',
package/index.js CHANGED
@@ -72,11 +72,9 @@ const DEFAULTS = Object.freeze({
72
72
  cooldownMs: 60000,
73
73
  /** 每小时最多重启几次;0 = 不限制。 */
74
74
  maxPerHour: 0,
75
- /** 有会话在跑时一直等它们空闲(没有上限,不会硬切)。 */
75
+ /** 有会话在跑时等它们空闲;等空闲的会话若只是卡在后台任务(job_*)上,会被本次重启打断并在重启后收到“继续”。 */
76
76
  waitForIdle: true,
77
- /** 等空闲最多等多久;超过就按计划重启(重启后会给被中断的会话发“继续”);0 = 一直等。 */
78
- maxIdleWaitMs: 600000,
79
- /** 重启后是否自动给被中断的会话发一条“继续”。 */
77
+ /** 重启后是否自动给被后台任务打断的会话发一条“继续”。 */
80
78
  continueInterrupted: true,
81
79
  /** 自动继续时发送的文本。 */
82
80
  continueText: '继续',
@@ -309,13 +307,45 @@ function ensureWatchdogFiles() {
309
307
  const target = join(dataDir(), file)
310
308
  try {
311
309
  if (!existsSync(source)) continue
312
- copyFileSync(source, target)
310
+ const bytes = readFileSync(source)
311
+ // Windows PowerShell 5.1 decodes a BOM-less .ps1 as ANSI, which turns the
312
+ // Chinese messages in the installer into mojibake and a parse error. Always
313
+ // write the runtime copy with a UTF-8 BOM, whatever the package shipped.
314
+ const needsBom = /\.ps1$/i.test(file)
315
+ && !(bytes.length >= 3 && bytes[0] === 0xEF && bytes[1] === 0xBB && bytes[2] === 0xBF)
316
+ if (needsBom) writeFileSync(target, Buffer.concat([Buffer.from([0xEF, 0xBB, 0xBF]), bytes]))
317
+ else writeFileSync(target, bytes)
313
318
  } catch (error) {
314
319
  logLine('warn', `同步看门狗文件失败 ${file}: ${String(error?.message ?? error)}`)
315
320
  }
316
321
  }
317
322
  }
318
323
 
324
+ /**
325
+ * Runs the boot resume once `sessionController` is available. Cordis may not have
326
+ * provided it yet when this plugin applies, so the optional service is looked up
327
+ * immediately and waited for through `inject` otherwise.
328
+ */
329
+ function resumeWithController(ctx, cfg) {
330
+ let started = false
331
+ const run = (scoped) => {
332
+ if (started) return
333
+ started = true
334
+ void resumeInterrupted(scoped, cfg)
335
+ }
336
+ try {
337
+ if (typeof ctx.get === 'function' && ctx.get('sessionController')) {
338
+ run(ctx)
339
+ return
340
+ }
341
+ } catch {}
342
+ try {
343
+ ctx.inject(['sessionController'], (scoped) => run(scoped))
344
+ } catch (error) {
345
+ logLine('warn', `等待 sessionController 失败:${String(error?.message ?? error)}`)
346
+ }
347
+ }
348
+
319
349
  /** SHA1 of the watchdog script shipped with this plugin version. */
320
350
  function watchdogScriptHash() {
321
351
  try {
@@ -559,7 +589,6 @@ function sanitizeConfig(raw) {
559
589
  cfg.enabled = bool(raw.enabled, cfg.enabled)
560
590
  cfg.dryRun = bool(raw.dryRun, cfg.dryRun)
561
591
  cfg.waitForIdle = bool(raw.waitForIdle, cfg.waitForIdle)
562
- cfg.maxIdleWaitMs = num(raw.maxIdleWaitMs, cfg.maxIdleWaitMs, 0, 86400000)
563
592
  cfg.continueInterrupted = bool(raw.continueInterrupted, cfg.continueInterrupted)
564
593
  cfg.continueWindowMs = num(raw.continueWindowMs, cfg.continueWindowMs, 60000, 86400000)
565
594
  if (typeof raw.continueText === 'string' && raw.continueText.trim() !== '') cfg.continueText = raw.continueText.slice(0, 200)
@@ -1005,16 +1034,26 @@ function tick(baseConfig) {
1005
1034
  }
1006
1035
  return
1007
1036
  }
1008
- const busyForMs = state.waitingSince > 0 ? now - state.waitingSince : 0
1009
- const overLimit = cfg.maxIdleWaitMs > 0 && busyForMs >= cfg.maxIdleWaitMs
1010
- if (!overLimit) {
1037
+
1038
+ // A Session blocked on a running background job (job_*, e.g. an npm polling
1039
+ // loop) is deliberately NOT counted as busy: its job dies with the Host and
1040
+ // cannot recover on its own, so the restart interrupts it and resumeInterrupted
1041
+ // sends one "继续" after the new process is up. Only Sessions doing real work
1042
+ // (streaming an answer, running the model) keep the restart waiting.
1043
+ const jobBlocked = []
1044
+ const hardBusy = []
1045
+ for (const id of state.busy) {
1046
+ if (runningJobsFor(String(id)).length > 0) jobBlocked.push(String(id))
1047
+ else hardBusy.push(String(id))
1048
+ }
1049
+ if (hardBusy.length > 0) {
1011
1050
  if (state.waitingSince === 0) {
1012
1051
  state.waitingSince = now
1013
- logLine('info', `有 ${state.busy.size} 个会话在跑,等它们空闲后再重启(最多等 ${cfg.maxIdleWaitMs > 0 ? Math.round(cfg.maxIdleWaitMs / 60000) + ' 分钟' : '一直等'})`)
1052
+ logLine('info', `有 ${hardBusy.length} 个会话正在回答/干活,等它们空闲后再重启(会一直等)${jobBlocked.length > 0 ? `;另外 ${jobBlocked.length} 个会话在等后台任务,重启会打断它们并在重启后发“继续”` : ''}`)
1014
1053
  }
1015
1054
  return
1016
1055
  }
1017
- logLine('warn', `等空闲已超过 ${Math.round(cfg.maxIdleWaitMs / 60000)} 分钟(${state.busy.size} 个会话还在跑),按计划重启;重启后会给它们发“继续”`)
1056
+ logLine('info', `其余 ${jobBlocked.length} 个会话都在等后台任务(job_*);本次重启会打断它们,重启后自动发“继续”`)
1018
1057
  }
1019
1058
  state.waitingSince = 0
1020
1059
 
@@ -1112,7 +1151,6 @@ function installRoutes(ctx, baseConfig) {
1112
1151
  logPath: watchdogLogPath(),
1113
1152
  },
1114
1153
  waitForIdle: cfg.waitForIdle,
1115
- maxIdleWaitMs: cfg.maxIdleWaitMs,
1116
1154
  continueInterrupted: cfg.continueInterrupted,
1117
1155
  continueText: cfg.continueText,
1118
1156
  busySessions: state ? [...state.busy] : [],
@@ -1232,7 +1270,7 @@ export function apply(ctx, config) {
1232
1270
  cleanForeignRequests()
1233
1271
  bootWatchdogCheck(state)
1234
1272
  // Sessions that a previous restart interrupted get one "继续" after boot.
1235
- void resumeInterrupted(ctx, cfg)
1273
+ resumeWithController(ctx, cfg)
1236
1274
 
1237
1275
  // Track busy sessions so a restart never cuts an answer.
1238
1276
  ctx.effect(() => ctx.on('api-session/status', (sessionId, running) => {
@@ -1,4 +1,4 @@
1
- param(
1
+ param(
2
2
  [switch]$Uninstall
3
3
  )
4
4
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leiyi-works/dsh-auto-restart",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "type": "module",
5
5
  "title": "自动重启",
6
6
  "description": "装/更插件后自动重启 DSH 并回到原会话:检测 profile 插件文件变化,一直等所有会话空闲且最近活动安静,由常驻后台看门狗(登录自启、不碰 WMI)或 WMI 守护先杀主进程再拉起(可最大化窗口),带冷却、试运行、日志和右侧原生侧边栏面板。",