@radnine/claude-session-daemon 0.6.3 → 0.6.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/package.json +1 -1
- package/src/claude-process.js +1 -0
- package/src/config.js +9 -0
- package/src/connections/action-cable-client.js +11 -9
- package/src/daemon.js +70 -20
package/package.json
CHANGED
package/src/claude-process.js
CHANGED
package/src/config.js
CHANGED
|
@@ -63,6 +63,11 @@ function loadConfig() {
|
|
|
63
63
|
|
|
64
64
|
// Path to the Claude CLI binary (override for testing)
|
|
65
65
|
claudeCommand: env.CLAUDE_COMMAND || 'claude',
|
|
66
|
+
|
|
67
|
+
// Stale session timeout in seconds — kill alive processes with no activity beyond this threshold.
|
|
68
|
+
// Safety net for when WebSocket-based deactivation messages are lost.
|
|
69
|
+
// Default: 30 minutes (1800s). Set to 0 to disable.
|
|
70
|
+
staleSessionTimeout: intEnv(env.CLAUDE_STALE_SESSION_TIMEOUT, 1800),
|
|
66
71
|
};
|
|
67
72
|
|
|
68
73
|
// Validate mode
|
|
@@ -79,6 +84,10 @@ function loadConfig() {
|
|
|
79
84
|
throw new Error(`CLAUDE_DAEMON_PORT must be 1-65535, got ${config.port}`);
|
|
80
85
|
}
|
|
81
86
|
|
|
87
|
+
if (config.staleSessionTimeout < 0) {
|
|
88
|
+
throw new Error(`CLAUDE_STALE_SESSION_TIMEOUT must be >= 0, got ${config.staleSessionTimeout}`);
|
|
89
|
+
}
|
|
90
|
+
|
|
82
91
|
return Object.freeze(config);
|
|
83
92
|
}
|
|
84
93
|
|
|
@@ -580,6 +580,12 @@ class ActionCableClient extends BaseConnection {
|
|
|
580
580
|
this._logger.info(`Updated server config - heartbeat: ${hbMs / 1000}s`);
|
|
581
581
|
}
|
|
582
582
|
|
|
583
|
+
// Send an immediate heartbeat so Rails can reconcile zombie sessions right away
|
|
584
|
+
// instead of waiting up to one heartbeat interval (30s) after reconnect.
|
|
585
|
+
this._sendHeartbeat().catch((err) => {
|
|
586
|
+
this._logger.error('Failed to send post-registration heartbeat', { error: err.message });
|
|
587
|
+
});
|
|
588
|
+
|
|
583
589
|
this._startHeartbeat();
|
|
584
590
|
this.emit('registered');
|
|
585
591
|
}
|
|
@@ -972,11 +978,9 @@ class ActionCableClient extends BaseConnection {
|
|
|
972
978
|
return;
|
|
973
979
|
}
|
|
974
980
|
|
|
975
|
-
|
|
976
|
-
this._sendHeartbeat();
|
|
977
|
-
} catch (err) {
|
|
981
|
+
this._sendHeartbeat().catch((err) => {
|
|
978
982
|
this._logger.error('Heartbeat error', { error: err.message });
|
|
979
|
-
}
|
|
983
|
+
});
|
|
980
984
|
}, this._heartbeatInterval);
|
|
981
985
|
|
|
982
986
|
// Allow the Node.js process to exit even if the timer is active
|
|
@@ -989,7 +993,7 @@ class ActionCableClient extends BaseConnection {
|
|
|
989
993
|
* Start the ping watchdog timer.
|
|
990
994
|
*
|
|
991
995
|
* Monitors _lastPingAt and forces a reconnect if no server ping has been
|
|
992
|
-
* received within
|
|
996
|
+
* received within 3× the heartbeat interval. This catches "half-open"
|
|
993
997
|
* connections where the WebSocket appears open but the server side is gone
|
|
994
998
|
* (e.g., after a machine sleep/wake cycle or network change).
|
|
995
999
|
*/
|
|
@@ -1030,11 +1034,9 @@ class ActionCableClient extends BaseConnection {
|
|
|
1030
1034
|
*/
|
|
1031
1035
|
_handleHealthCheckPing(msg) {
|
|
1032
1036
|
this._logger.info(`Health check ping received (reason: ${msg.reason || 'unknown'}), sending immediate heartbeat`);
|
|
1033
|
-
|
|
1034
|
-
this._sendHeartbeat();
|
|
1035
|
-
} catch (err) {
|
|
1037
|
+
this._sendHeartbeat().catch((err) => {
|
|
1036
1038
|
this._logger.error('Failed to respond to health check ping', { error: err.message });
|
|
1037
|
-
}
|
|
1039
|
+
});
|
|
1038
1040
|
}
|
|
1039
1041
|
|
|
1040
1042
|
/**
|
package/src/daemon.js
CHANGED
|
@@ -36,6 +36,7 @@ class Daemon extends EventEmitter {
|
|
|
36
36
|
this._healthCheckTimer = null;
|
|
37
37
|
this._createProcess = options._createProcess || this._defaultCreateProcess.bind(this);
|
|
38
38
|
this._shutdownHandler = null;
|
|
39
|
+
this._cleanupRunning = false;
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
// -------------------------------------------------------------------------
|
|
@@ -73,6 +74,7 @@ class Daemon extends EventEmitter {
|
|
|
73
74
|
maxSessions: this._config.maxSessions,
|
|
74
75
|
cleanupInterval: this._config.cleanupInterval,
|
|
75
76
|
heartbeatInterval: this._config.heartbeatInterval,
|
|
77
|
+
staleSessionTimeout: this._config.staleSessionTimeout,
|
|
76
78
|
});
|
|
77
79
|
|
|
78
80
|
this.emit('started');
|
|
@@ -276,7 +278,10 @@ class Daemon extends EventEmitter {
|
|
|
276
278
|
* @returns {number}
|
|
277
279
|
*/
|
|
278
280
|
get activeCount() {
|
|
279
|
-
|
|
281
|
+
// Remove only dead processes synchronously (stale reaping is async, handled by timer)
|
|
282
|
+
for (const [sessionId, proc] of this._processes) {
|
|
283
|
+
if (!proc.isAlive()) this._processes.delete(sessionId);
|
|
284
|
+
}
|
|
280
285
|
return this._processes.size;
|
|
281
286
|
}
|
|
282
287
|
|
|
@@ -323,42 +328,87 @@ class Daemon extends EventEmitter {
|
|
|
323
328
|
// -------------------------------------------------------------------------
|
|
324
329
|
|
|
325
330
|
/**
|
|
326
|
-
* Clean up dead processes from the pool.
|
|
331
|
+
* Clean up dead processes and reap stale alive processes from the pool.
|
|
332
|
+
*
|
|
333
|
+
* Dead processes are removed immediately.
|
|
334
|
+
* Stale processes (alive but no activity beyond staleSessionTimeout) are
|
|
335
|
+
* killed then removed — this is the safety net for when WebSocket-based
|
|
336
|
+
* deactivation messages are lost during connection drops.
|
|
327
337
|
*/
|
|
328
|
-
_cleanupDeadProcesses() {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
338
|
+
async _cleanupDeadProcesses() {
|
|
339
|
+
if (this._cleanupRunning) return;
|
|
340
|
+
this._cleanupRunning = true;
|
|
341
|
+
|
|
342
|
+
try {
|
|
343
|
+
const deadSessions = [];
|
|
344
|
+
const staleSessions = [];
|
|
345
|
+
const staleTimeoutMs = (this._config.staleSessionTimeout || 0) * 1000;
|
|
346
|
+
const now = Date.now();
|
|
347
|
+
|
|
348
|
+
for (const [sessionId, proc] of this._processes) {
|
|
349
|
+
if (!proc.isAlive()) {
|
|
350
|
+
deadSessions.push(sessionId);
|
|
351
|
+
} else if (staleTimeoutMs > 0 && proc.lastActivityAt) {
|
|
352
|
+
const idleMs = now - proc.lastActivityAt;
|
|
353
|
+
if (idleMs > staleTimeoutMs) {
|
|
354
|
+
staleSessions.push({ sessionId, idleMs, pid: proc.pid });
|
|
355
|
+
}
|
|
356
|
+
}
|
|
333
357
|
}
|
|
334
|
-
}
|
|
335
358
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
359
|
+
// Remove dead processes
|
|
360
|
+
for (const sessionId of deadSessions) {
|
|
361
|
+
this._processes.delete(sessionId);
|
|
362
|
+
this._cancelledSessionIds.delete(sessionId);
|
|
363
|
+
this._logger.info('Cleaned up dead session', { sessionId });
|
|
364
|
+
}
|
|
340
365
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
366
|
+
// Kill and remove stale processes
|
|
367
|
+
for (const { sessionId, idleMs, pid } of staleSessions) {
|
|
368
|
+
this._logger.warn('Killing stale session — no activity', {
|
|
369
|
+
sessionId,
|
|
370
|
+
pid,
|
|
371
|
+
idleSeconds: Math.round(idleMs / 1000),
|
|
372
|
+
threshold: this._config.staleSessionTimeout,
|
|
373
|
+
});
|
|
374
|
+
const proc = this._processes.get(sessionId);
|
|
375
|
+
if (proc) {
|
|
376
|
+
try {
|
|
377
|
+
await proc.kill();
|
|
378
|
+
} catch (err) {
|
|
379
|
+
this._logger.error('Failed to kill stale session', { sessionId, error: err.message });
|
|
380
|
+
}
|
|
381
|
+
this._processes.delete(sessionId);
|
|
382
|
+
this._cancelledSessionIds.delete(sessionId);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const removed = deadSessions.length + staleSessions.length;
|
|
387
|
+
if (removed > 0) {
|
|
388
|
+
this._logger.info('Cleanup complete', {
|
|
389
|
+
deadRemoved: deadSessions.length,
|
|
390
|
+
staleKilled: staleSessions.length,
|
|
391
|
+
remaining: this._processes.size,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
} finally {
|
|
395
|
+
this._cleanupRunning = false;
|
|
346
396
|
}
|
|
347
397
|
}
|
|
348
398
|
|
|
349
399
|
/**
|
|
350
400
|
* Run periodic health check and log status.
|
|
351
401
|
*/
|
|
352
|
-
_runHealthCheck() {
|
|
402
|
+
async _runHealthCheck() {
|
|
353
403
|
const health = this.healthCheck();
|
|
354
404
|
const alive = health.filter((h) => h.alive).length;
|
|
355
405
|
const dead = health.filter((h) => !h.alive).length;
|
|
356
406
|
|
|
357
407
|
this._logger.debug('Health check', { alive, dead, total: health.length });
|
|
358
408
|
|
|
359
|
-
// Clean up any dead processes found during health check
|
|
409
|
+
// Clean up any dead or stale processes found during health check
|
|
360
410
|
if (dead > 0) {
|
|
361
|
-
this._cleanupDeadProcesses();
|
|
411
|
+
await this._cleanupDeadProcesses();
|
|
362
412
|
}
|
|
363
413
|
}
|
|
364
414
|
|