@leiyi-works/dsh-auto-restart 0.3.6 → 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 +65 -25
- 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) {
|
|
@@ -1001,6 +1053,11 @@ function tick(baseConfig) {
|
|
|
1001
1053
|
const now = Date.now()
|
|
1002
1054
|
state.restarts = state.restarts.filter((t) => now - t < 3600000)
|
|
1003
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
|
+
|
|
1004
1061
|
// A restart is already on its way; the process will disappear shortly.
|
|
1005
1062
|
if (state.dispatchedAt > 0 && now - state.dispatchedAt < 60000) return
|
|
1006
1063
|
|
|
@@ -1267,6 +1324,7 @@ export function apply(ctx, config) {
|
|
|
1267
1324
|
if (globalThis[STATE_KEY] === undefined) globalThis[STATE_KEY] = createState(profileDir)
|
|
1268
1325
|
else globalThis[STATE_KEY].profileDir = profileDir
|
|
1269
1326
|
const state = globalThis[STATE_KEY]
|
|
1327
|
+
activeState = state
|
|
1270
1328
|
|
|
1271
1329
|
// Report the outcome of the restart this process may have just survived.
|
|
1272
1330
|
// The supervisor writes `stopped` first and `relaunched` only after the new
|
|
@@ -1342,31 +1400,10 @@ export function apply(ctx, config) {
|
|
|
1342
1400
|
if (state.sessionLastEventAt instanceof Map) state.sessionLastEventAt.set(String(id), Date.now())
|
|
1343
1401
|
}), 'auto-restart: session events')
|
|
1344
1402
|
|
|
1345
|
-
// The exact signal for "this Session is waiting on a background job"
|
|
1346
|
-
// tool it is executing right now.
|
|
1347
|
-
//
|
|
1348
|
-
|
|
1349
|
-
const agentId = exec?.agent?.id
|
|
1350
|
-
const name = typeof exec?.name === 'string' ? exec.name : ''
|
|
1351
|
-
if (agentId === undefined || name === '' || !(state.activeTools instanceof Map)) return next()
|
|
1352
|
-
const key = String(agentId)
|
|
1353
|
-
const token = { name, at: Date.now() }
|
|
1354
|
-
state.activeTools.set(key, token)
|
|
1355
|
-
const settle = () => {
|
|
1356
|
-
if (state.activeTools instanceof Map && state.activeTools.get(key) === token) state.activeTools.delete(key)
|
|
1357
|
-
}
|
|
1358
|
-
let pending
|
|
1359
|
-
try {
|
|
1360
|
-
pending = next()
|
|
1361
|
-
} catch (error) {
|
|
1362
|
-
settle()
|
|
1363
|
-
throw error
|
|
1364
|
-
}
|
|
1365
|
-
return Promise.resolve(pending).then(
|
|
1366
|
-
(result) => { settle(); return result },
|
|
1367
|
-
(error) => { settle(); throw error },
|
|
1368
|
-
)
|
|
1369
|
-
}), 'auto-restart: active tools')
|
|
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.
|
|
1370
1407
|
|
|
1371
1408
|
// User-authored messages: the turn may not have started yet, but the text was
|
|
1372
1409
|
// just sent, so a restart here would swallow it and restore the draft.
|
|
@@ -1378,6 +1415,9 @@ export function apply(ctx, config) {
|
|
|
1378
1415
|
safeOn(ctx, event, () => { state.lastManagerAt = Date.now() }, `auto-restart: ${event}`)
|
|
1379
1416
|
}
|
|
1380
1417
|
|
|
1418
|
+
// Drop the on-demand tool tracker when the plugin unloads.
|
|
1419
|
+
ctx.effect(() => () => stopToolsTracking())
|
|
1420
|
+
|
|
1381
1421
|
// One timer for the whole plugin; disposal clears it.
|
|
1382
1422
|
const interval = Math.max(500, Math.min(5000, cfg.pollIntervalMs))
|
|
1383
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 守护先杀主进程再拉起(可最大化窗口),带冷却、试运行、日志和右侧原生侧边栏面板。",
|