@leiyi-works/dsh-auto-restart 0.3.7 → 0.3.9
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/index.js +23 -11
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -76,8 +76,8 @@ const DEFAULTS = Object.freeze({
|
|
|
76
76
|
waitForIdle: true,
|
|
77
77
|
/** 重启后是否自动给被后台任务打断的会话发一条“继续”。 */
|
|
78
78
|
continueInterrupted: true,
|
|
79
|
-
/**
|
|
80
|
-
jobWaitQuietMs:
|
|
79
|
+
/** 兜底:会话持有 job 又安静了多久也算“在等后台任务”;0 = 关闭兜底,只认当前工具是不是 job_*。 */
|
|
80
|
+
jobWaitQuietMs: 0,
|
|
81
81
|
/** 自动继续时发送的文本。 */
|
|
82
82
|
continueText: '继续',
|
|
83
83
|
/** 多久以内的中断记录还值得自动继续(毫秒)。 */
|
|
@@ -245,6 +245,13 @@ function runningJobsFor(sessionId) {
|
|
|
245
245
|
* with the Host and never recover on their own, so they get one "继续" after the
|
|
246
246
|
* restart. Sessions that are merely answering are left alone.
|
|
247
247
|
*/
|
|
248
|
+
/** One-line, bounded label for a job (bash jobs carry the whole command line). */
|
|
249
|
+
function shortenLabel(text, max = 48) {
|
|
250
|
+
const clean = String(text ?? '').replace(/\s+/g, ' ').trim()
|
|
251
|
+
if (clean === '') return ''
|
|
252
|
+
return clean.length <= max ? clean : `${clean.slice(0, Math.max(1, max - 1))}…`
|
|
253
|
+
}
|
|
254
|
+
|
|
248
255
|
function recordInterrupted(state, reason) {
|
|
249
256
|
const file = interruptedPath()
|
|
250
257
|
const entries = []
|
|
@@ -254,7 +261,7 @@ function recordInterrupted(state, reason) {
|
|
|
254
261
|
if (jobs.length === 0) continue
|
|
255
262
|
entries.push({
|
|
256
263
|
sessionId,
|
|
257
|
-
jobs: jobs.slice(0, 5).map((job) => ({ id: String(job.id), label:
|
|
264
|
+
jobs: jobs.slice(0, 5).map((job) => ({ id: String(job.id), label: shortenLabel(job.label || job.kind || job.id) })),
|
|
258
265
|
})
|
|
259
266
|
}
|
|
260
267
|
if (entries.length === 0) {
|
|
@@ -309,10 +316,12 @@ async function resumeInterrupted(ctx, cfg) {
|
|
|
309
316
|
const entry = entries[index]
|
|
310
317
|
const sessionId = String(entry.sessionId)
|
|
311
318
|
const short = sessionId.slice(0, 18)
|
|
312
|
-
const labels = Array.isArray(entry.jobs)
|
|
319
|
+
const labels = Array.isArray(entry.jobs)
|
|
320
|
+
? entry.jobs.map((job) => shortenLabel(job.label || job.id, 40)).filter((label) => label !== '').join('、')
|
|
321
|
+
: ''
|
|
313
322
|
const text = labels === ''
|
|
314
323
|
? String(cfg.continueText || '继续')
|
|
315
|
-
: `${String(cfg.continueText || '继续')}(刚才的后台任务因 DSH 重启被中断,不会自动恢复:${labels.slice(0,
|
|
324
|
+
: `${String(cfg.continueText || '继续')}(刚才的后台任务因 DSH 重启被中断,不会自动恢复:${labels.slice(0, 140)}。请继续原来的工作)`
|
|
316
325
|
try {
|
|
317
326
|
let sessionState = null
|
|
318
327
|
try { sessionState = await controller.state({ sessionId }) } catch {}
|
|
@@ -673,7 +682,7 @@ function sanitizeConfig(raw) {
|
|
|
673
682
|
cfg.enabled = bool(raw.enabled, cfg.enabled)
|
|
674
683
|
cfg.dryRun = bool(raw.dryRun, cfg.dryRun)
|
|
675
684
|
cfg.waitForIdle = bool(raw.waitForIdle, cfg.waitForIdle)
|
|
676
|
-
cfg.jobWaitQuietMs = num(raw.jobWaitQuietMs, cfg.jobWaitQuietMs,
|
|
685
|
+
cfg.jobWaitQuietMs = num(raw.jobWaitQuietMs, cfg.jobWaitQuietMs, 0, 3600000)
|
|
677
686
|
cfg.continueInterrupted = bool(raw.continueInterrupted, cfg.continueInterrupted)
|
|
678
687
|
cfg.continueWindowMs = num(raw.continueWindowMs, cfg.continueWindowMs, 60000, 86400000)
|
|
679
688
|
if (typeof raw.continueText === 'string' && raw.continueText.trim() !== '') cfg.continueText = raw.continueText.slice(0, 200)
|
|
@@ -1140,10 +1149,13 @@ function tick(baseConfig) {
|
|
|
1140
1149
|
const sessionId = String(id)
|
|
1141
1150
|
const hasJobs = runningJobsFor(sessionId).length > 0
|
|
1142
1151
|
const active = state.activeTools instanceof Map ? state.activeTools.get(sessionId) : undefined
|
|
1143
|
-
|
|
1152
|
+
// Only job_output blocks on a job; job_list/job_kill/job_read return at once
|
|
1153
|
+
// and must not make a busy Session look interruptible.
|
|
1154
|
+
const inJobTool = active !== undefined && /^job_output$/i.test(String(active.name))
|
|
1144
1155
|
const lastAt = state.sessionLastEventAt instanceof Map ? (state.sessionLastEventAt.get(sessionId) ?? 0) : 0
|
|
1145
1156
|
const quietMs = lastAt > 0 ? now - lastAt : Number.POSITIVE_INFINITY
|
|
1146
|
-
|
|
1157
|
+
const fallbackWait = cfg.jobWaitQuietMs > 0 && quietMs >= cfg.jobWaitQuietMs
|
|
1158
|
+
if (hasJobs && (inJobTool || fallbackWait)) jobBlocked.push(sessionId)
|
|
1147
1159
|
else hardBusy.push(sessionId)
|
|
1148
1160
|
}
|
|
1149
1161
|
if (hardBusy.length > 0) {
|
|
@@ -1415,9 +1427,9 @@ export function apply(ctx, config) {
|
|
|
1415
1427
|
safeOn(ctx, event, () => { state.lastManagerAt = Date.now() }, `auto-restart: ${event}`)
|
|
1416
1428
|
}
|
|
1417
1429
|
|
|
1418
|
-
// Drop the on-demand tool tracker when the plugin unloads.
|
|
1419
|
-
ctx.effect(() => () => stopToolsTracking())
|
|
1420
|
-
|
|
1430
|
+
// Drop the on-demand tool tracker when the plugin unloads.
|
|
1431
|
+
ctx.effect(() => () => stopToolsTracking())
|
|
1432
|
+
|
|
1421
1433
|
// One timer for the whole plugin; disposal clears it.
|
|
1422
1434
|
const interval = Math.max(500, Math.min(5000, cfg.pollIntervalMs))
|
|
1423
1435
|
ctx.effect(() => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leiyi-works/dsh-auto-restart",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"title": "自动重启",
|
|
6
6
|
"description": "装/更插件后自动重启 DSH 并回到原会话:检测 profile 插件文件变化,一直等所有会话空闲且最近活动安静,由常驻后台看门狗(登录自启、不碰 WMI)或 WMI 守护先杀主进程再拉起(可最大化窗口),带冷却、试运行、日志和右侧原生侧边栏面板。",
|