@radnine/claude-session-daemon 0.6.4 → 0.6.6

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radnine/claude-session-daemon",
3
- "version": "0.6.4",
3
+ "version": "0.6.6",
4
4
  "description": "Node.js daemon for managing Claude CLI sessions",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "repository": {
@@ -286,6 +286,23 @@ class ActionCableClient extends BaseConnection {
286
286
  _connect() {
287
287
  if (this._shutdownRequested) return;
288
288
 
289
+ // Close any prior socket before opening a new one. Some reconnect paths
290
+ // (e.g. subscription rejection) schedule a reconnect while the existing
291
+ // socket is still OPEN — without this, the old socket leaks with its
292
+ // listeners still attached.
293
+ if (this._ws) {
294
+ const prior = this._ws;
295
+ this._ws = null;
296
+ try {
297
+ prior.removeAllListeners();
298
+ if (prior.readyState === WebSocket.OPEN || prior.readyState === WebSocket.CONNECTING) {
299
+ prior.close();
300
+ }
301
+ } catch (_) {
302
+ // close() may throw if the socket is already closed — safe to ignore
303
+ }
304
+ }
305
+
289
306
  const wsUrl = this._buildWebSocketUrl();
290
307
  this._logger.info(`Connecting to ${wsUrl}`);
291
308
 
@@ -613,13 +630,15 @@ class ActionCableClient extends BaseConnection {
613
630
  const sessionId = message.session_id;
614
631
  const userId = message.user_id;
615
632
  const claudeSessionId = message.claude_session_id || undefined;
633
+ const resetGit = !!message.reset_git_state;
616
634
 
617
- this._logger.info('Received session_activate', { sessionId, userId, claudeSessionId });
635
+ this._logger.info('Received session_activate', { sessionId, userId, claudeSessionId, resetGit });
618
636
 
619
637
  try {
620
638
  await this._daemon.activateSession(sessionId, {
621
639
  workingDir: this._config.workingDir,
622
640
  claudeSessionId,
641
+ resetGit,
623
642
  });
624
643
 
625
644
  this.sendDaemonMessage({
package/src/daemon.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  const { EventEmitter } = require('node:events');
4
4
  const ClaudeProcess = require('./claude-process');
5
+ const { resetGitState } = require('./git-reset');
5
6
 
6
7
  /**
7
8
  * Process pool manager for Claude CLI sessions.
@@ -155,6 +156,15 @@ class Daemon extends EventEmitter {
155
156
  const shouldResume = !!claudeSessionId;
156
157
  this._cancelledSessionIds.delete(sessionId);
157
158
 
159
+ // Reset git state to clean main when requested (scheduled sessions).
160
+ // Awaited so spawn waits for a clean tree, but async under the hood so the
161
+ // event loop keeps servicing ActionCable pings during the git commands.
162
+ if (options.resetGit) {
163
+ const cwd = options.workingDir || this._config.workingDir;
164
+ this._logger.info('Resetting git state before activation', { sessionId, cwd });
165
+ await resetGitState(cwd, this._logger);
166
+ }
167
+
158
168
  // Create new process
159
169
  const proc = this._createProcess(sessionId, {
160
170
  workingDir: options.workingDir || this._config.workingDir,
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ const { execFile } = require('node:child_process');
4
+ const { promisify } = require('node:util');
5
+
6
+ const execFileAsync = promisify(execFile);
7
+
8
+ const EXEC_TIMEOUT = 15000; // 15 seconds per git command
9
+
10
+ /**
11
+ * Reset git state to a clean main branch.
12
+ *
13
+ * Runs: stash → checkout main → pull origin main.
14
+ * Each step logs its result. Failures are logged but never thrown —
15
+ * session activation must not be blocked by git problems.
16
+ *
17
+ * Uses async execFile (not execFileSync) so the Node.js event loop stays
18
+ * responsive to ActionCable pings while git commands are running. A blocked
19
+ * event loop causes the ping watchdog to fire and force a WebSocket reconnect.
20
+ *
21
+ * @param {string} workingDir - The repo directory to reset
22
+ * @param {object} logger - Logger with .info() and .warn() methods
23
+ * @returns {Promise<{ stash: boolean, checkout: boolean, pull: boolean }>} step results
24
+ */
25
+ async function resetGitState(workingDir, logger) {
26
+ const results = { stash: false, checkout: false, pull: false };
27
+
28
+ // Step 1: stash uncommitted changes
29
+ try {
30
+ const { stdout } = await execFileAsync('git', ['stash', '--include-untracked'], {
31
+ cwd: workingDir,
32
+ timeout: EXEC_TIMEOUT,
33
+ encoding: 'utf8',
34
+ });
35
+ results.stash = true;
36
+ logger.info('git stash completed', { output: stdout.trim() });
37
+ } catch (err) {
38
+ logger.warn('git stash failed (continuing)', { error: err.message });
39
+ }
40
+
41
+ // Step 2: checkout main
42
+ try {
43
+ const { stdout } = await execFileAsync('git', ['checkout', 'main'], {
44
+ cwd: workingDir,
45
+ timeout: EXEC_TIMEOUT,
46
+ encoding: 'utf8',
47
+ });
48
+ results.checkout = true;
49
+ logger.info('git checkout main completed', { output: stdout.trim() });
50
+ } catch (err) {
51
+ logger.warn('git checkout main failed (continuing)', { error: err.message });
52
+ }
53
+
54
+ // Step 3: pull latest
55
+ try {
56
+ const { stdout } = await execFileAsync('git', ['pull', 'origin', 'main'], {
57
+ cwd: workingDir,
58
+ timeout: EXEC_TIMEOUT,
59
+ encoding: 'utf8',
60
+ });
61
+ results.pull = true;
62
+ logger.info('git pull completed', { output: stdout.trim() });
63
+ } catch (err) {
64
+ logger.warn('git pull failed (continuing)', { error: err.message });
65
+ }
66
+
67
+ return results;
68
+ }
69
+
70
+ module.exports = { resetGitState };