@leiyi-works/dsh-auto-restart 0.3.5 → 0.3.7
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 +78 -8
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -175,6 +175,58 @@ function interruptedPath() {
|
|
|
175
175
|
|
|
176
176
|
/** The plugin context, kept for the one-time boot resume and the jobs lookup. */
|
|
177
177
|
let activeCtx = null
|
|
178
|
+
/** In-memory restart state, kept for the on-demand tool tracker. */
|
|
179
|
+
let activeState = null
|
|
180
|
+
/** Disposer of the tools/execute listener; non-null only while a restart is pending. */
|
|
181
|
+
let toolsTrackingDispose = null
|
|
182
|
+
|
|
183
|
+
/** Records which tool each Session is executing right now. */
|
|
184
|
+
function trackToolExecution(exec, next) {
|
|
185
|
+
const agentId = exec?.agent?.id
|
|
186
|
+
const name = typeof exec?.name === 'string' ? exec.name : ''
|
|
187
|
+
const state = activeState
|
|
188
|
+
if (agentId === undefined || name === '' || state === null || !(state.activeTools instanceof Map)) return next()
|
|
189
|
+
const key = String(agentId)
|
|
190
|
+
const token = { name, at: Date.now() }
|
|
191
|
+
state.activeTools.set(key, token)
|
|
192
|
+
const settle = () => {
|
|
193
|
+
if (state.activeTools instanceof Map && state.activeTools.get(key) === token) state.activeTools.delete(key)
|
|
194
|
+
}
|
|
195
|
+
let pending
|
|
196
|
+
try {
|
|
197
|
+
pending = next()
|
|
198
|
+
} catch (error) {
|
|
199
|
+
settle()
|
|
200
|
+
throw error
|
|
201
|
+
}
|
|
202
|
+
return Promise.resolve(pending).then(
|
|
203
|
+
(result) => { settle(); return result },
|
|
204
|
+
(error) => { settle(); throw error },
|
|
205
|
+
)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Attach the tool tracker only while a restart is pending. Between restarts the
|
|
210
|
+
* plugin has no tools/execute listener at all, so idle memory and CPU are zero.
|
|
211
|
+
*/
|
|
212
|
+
function startToolsTracking() {
|
|
213
|
+
if (toolsTrackingDispose !== null || activeCtx === null) return
|
|
214
|
+
try {
|
|
215
|
+
toolsTrackingDispose = activeCtx.on('tools/execute', trackToolExecution)
|
|
216
|
+
} catch {
|
|
217
|
+
toolsTrackingDispose = null
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function stopToolsTracking() {
|
|
222
|
+
if (toolsTrackingDispose === null) return
|
|
223
|
+
const dispose = toolsTrackingDispose
|
|
224
|
+
toolsTrackingDispose = null
|
|
225
|
+
try { dispose() } catch {}
|
|
226
|
+
try {
|
|
227
|
+
if (activeState !== null && activeState.activeTools instanceof Map) activeState.activeTools.clear()
|
|
228
|
+
} catch {}
|
|
229
|
+
}
|
|
178
230
|
|
|
179
231
|
/** Background jobs still running for one Session (empty when the service is absent). */
|
|
180
232
|
function runningJobsFor(sessionId) {
|
|
@@ -943,6 +995,7 @@ function createState(profileDir) {
|
|
|
943
995
|
lastActivityAt: 0,
|
|
944
996
|
pendingInputIds: new Set(),
|
|
945
997
|
sessionLastEventAt: new Map(),
|
|
998
|
+
activeTools: new Map(),
|
|
946
999
|
lastRestartAt: persisted.lastRestartAt,
|
|
947
1000
|
restarts: persisted.restarts.filter((t) => now - t < 3600000),
|
|
948
1001
|
lastManagerAt: 0,
|
|
@@ -1000,6 +1053,11 @@ function tick(baseConfig) {
|
|
|
1000
1053
|
const now = Date.now()
|
|
1001
1054
|
state.restarts = state.restarts.filter((t) => now - t < 3600000)
|
|
1002
1055
|
|
|
1056
|
+
// The tool tracker only exists while a restart is actually being considered:
|
|
1057
|
+
// attach it when a change armed the restart, drop it once nothing is pending.
|
|
1058
|
+
if (state.pendingSince > 0 && state.dispatchedAt === 0) startToolsTracking()
|
|
1059
|
+
else stopToolsTracking()
|
|
1060
|
+
|
|
1003
1061
|
// A restart is already on its way; the process will disappear shortly.
|
|
1004
1062
|
if (state.dispatchedAt > 0 && now - state.dispatchedAt < 60000) return
|
|
1005
1063
|
|
|
@@ -1070,29 +1128,32 @@ function tick(baseConfig) {
|
|
|
1070
1128
|
}
|
|
1071
1129
|
|
|
1072
1130
|
// A Session blocked on a running background job (job_*, e.g. an npm polling
|
|
1073
|
-
// loop) is NOT counted as busy
|
|
1074
|
-
// its job dies with the Host, so
|
|
1075
|
-
// resumeInterrupted sends one "继续" after the
|
|
1076
|
-
//
|
|
1077
|
-
// still
|
|
1131
|
+
// loop) is NOT counted as busy while it sits in a job_* tool call (that is
|
|
1132
|
+
// exactly "waiting for the background job"): its job dies with the Host, so
|
|
1133
|
+
// the restart interrupts it and resumeInterrupted sends one "继续" after the
|
|
1134
|
+
// new process is up. Sessions running anything else (answering, bash, edits)
|
|
1135
|
+
// still count as busy and keep the restart waiting. The silence heuristic is
|
|
1136
|
+
// only a fallback for a wait that started before this plugin loaded.
|
|
1078
1137
|
const jobBlocked = []
|
|
1079
1138
|
const hardBusy = []
|
|
1080
1139
|
for (const id of state.busy) {
|
|
1081
1140
|
const sessionId = String(id)
|
|
1082
1141
|
const hasJobs = runningJobsFor(sessionId).length > 0
|
|
1142
|
+
const active = state.activeTools instanceof Map ? state.activeTools.get(sessionId) : undefined
|
|
1143
|
+
const inJobTool = active !== undefined && /^job_/i.test(String(active.name))
|
|
1083
1144
|
const lastAt = state.sessionLastEventAt instanceof Map ? (state.sessionLastEventAt.get(sessionId) ?? 0) : 0
|
|
1084
1145
|
const quietMs = lastAt > 0 ? now - lastAt : Number.POSITIVE_INFINITY
|
|
1085
|
-
if (hasJobs && quietMs >= cfg.jobWaitQuietMs) jobBlocked.push(sessionId)
|
|
1146
|
+
if (hasJobs && (inJobTool || quietMs >= cfg.jobWaitQuietMs)) jobBlocked.push(sessionId)
|
|
1086
1147
|
else hardBusy.push(sessionId)
|
|
1087
1148
|
}
|
|
1088
1149
|
if (hardBusy.length > 0) {
|
|
1089
1150
|
if (state.waitingSince === 0) {
|
|
1090
1151
|
state.waitingSince = now
|
|
1091
|
-
logLine('info', `有 ${hardBusy.length} 个会话正在回答/干活,等它们空闲后再重启${jobBlocked.length > 0 ? `;另外 ${jobBlocked.length}
|
|
1152
|
+
logLine('info', `有 ${hardBusy.length} 个会话正在回答/干活,等它们空闲后再重启${jobBlocked.length > 0 ? `;另外 ${jobBlocked.length} 个会话在等后台任务,重启会打断它们并在重启后发“继续”` : ''}`)
|
|
1092
1153
|
}
|
|
1093
1154
|
return
|
|
1094
1155
|
}
|
|
1095
|
-
logLine('info', `其余 ${jobBlocked.length}
|
|
1156
|
+
logLine('info', `其余 ${jobBlocked.length} 个会话都卡在后台任务工具上(job_*);本次重启会打断它们,重启后自动发“继续”`)
|
|
1096
1157
|
}
|
|
1097
1158
|
state.waitingSince = 0
|
|
1098
1159
|
|
|
@@ -1263,6 +1324,7 @@ export function apply(ctx, config) {
|
|
|
1263
1324
|
if (globalThis[STATE_KEY] === undefined) globalThis[STATE_KEY] = createState(profileDir)
|
|
1264
1325
|
else globalThis[STATE_KEY].profileDir = profileDir
|
|
1265
1326
|
const state = globalThis[STATE_KEY]
|
|
1327
|
+
activeState = state
|
|
1266
1328
|
|
|
1267
1329
|
// Report the outcome of the restart this process may have just survived.
|
|
1268
1330
|
// The supervisor writes `stopped` first and `relaunched` only after the new
|
|
@@ -1338,6 +1400,11 @@ export function apply(ctx, config) {
|
|
|
1338
1400
|
if (state.sessionLastEventAt instanceof Map) state.sessionLastEventAt.set(String(id), Date.now())
|
|
1339
1401
|
}), 'auto-restart: session events')
|
|
1340
1402
|
|
|
1403
|
+
// The exact signal for "this Session is waiting on a background job" is the
|
|
1404
|
+
// tool it is executing right now. That tracker is attached on demand by tick
|
|
1405
|
+
// only while a restart is pending (see startToolsTracking), so the plugin
|
|
1406
|
+
// carries no tools/execute listener while it is idle.
|
|
1407
|
+
|
|
1341
1408
|
// User-authored messages: the turn may not have started yet, but the text was
|
|
1342
1409
|
// just sent, so a restart here would swallow it and restore the draft.
|
|
1343
1410
|
safeOn(ctx, 'api-session/activity', () => { state.lastActivityAt = Date.now() }, 'auto-restart: session activity')
|
|
@@ -1348,6 +1415,9 @@ export function apply(ctx, config) {
|
|
|
1348
1415
|
safeOn(ctx, event, () => { state.lastManagerAt = Date.now() }, `auto-restart: ${event}`)
|
|
1349
1416
|
}
|
|
1350
1417
|
|
|
1418
|
+
// Drop the on-demand tool tracker when the plugin unloads.
|
|
1419
|
+
ctx.effect(() => () => stopToolsTracking())
|
|
1420
|
+
|
|
1351
1421
|
// One timer for the whole plugin; disposal clears it.
|
|
1352
1422
|
const interval = Math.max(500, Math.min(5000, cfg.pollIntervalMs))
|
|
1353
1423
|
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.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"title": "自动重启",
|
|
6
6
|
"description": "装/更插件后自动重启 DSH 并回到原会话:检测 profile 插件文件变化,一直等所有会话空闲且最近活动安静,由常驻后台看门狗(登录自启、不碰 WMI)或 WMI 守护先杀主进程再拉起(可最大化窗口),带冷却、试运行、日志和右侧原生侧边栏面板。",
|