@radnine/claude-session-daemon 0.6.4 → 0.6.5

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.5",
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,13 @@ 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
+ if (options.resetGit) {
161
+ const cwd = options.workingDir || this._config.workingDir;
162
+ this._logger.info('Resetting git state before activation', { sessionId, cwd });
163
+ resetGitState(cwd, this._logger);
164
+ }
165
+
158
166
  // Create new process
159
167
  const proc = this._createProcess(sessionId, {
160
168
  workingDir: options.workingDir || this._config.workingDir,
@@ -0,0 +1,63 @@
1
+ 'use strict';
2
+
3
+ const { execFileSync } = require('node:child_process');
4
+
5
+ const EXEC_TIMEOUT = 15000; // 15 seconds per git command
6
+
7
+ /**
8
+ * Reset git state to a clean main branch.
9
+ *
10
+ * Runs: stash → checkout main → pull origin main.
11
+ * Each step logs its result. Failures are logged but never thrown —
12
+ * session activation must not be blocked by git problems.
13
+ *
14
+ * @param {string} workingDir - The repo directory to reset
15
+ * @param {object} logger - Logger with .info() and .warn() methods
16
+ * @returns {{ stash: boolean, checkout: boolean, pull: boolean }} step results
17
+ */
18
+ function resetGitState(workingDir, logger) {
19
+ const results = { stash: false, checkout: false, pull: false };
20
+
21
+ // Step 1: stash uncommitted changes
22
+ try {
23
+ const out = execFileSync('git', ['stash', '--include-untracked'], {
24
+ cwd: workingDir,
25
+ timeout: EXEC_TIMEOUT,
26
+ encoding: 'utf8',
27
+ });
28
+ results.stash = true;
29
+ logger.info('git stash completed', { output: out.trim() });
30
+ } catch (err) {
31
+ logger.warn('git stash failed (continuing)', { error: err.message });
32
+ }
33
+
34
+ // Step 2: checkout main
35
+ try {
36
+ const out = execFileSync('git', ['checkout', 'main'], {
37
+ cwd: workingDir,
38
+ timeout: EXEC_TIMEOUT,
39
+ encoding: 'utf8',
40
+ });
41
+ results.checkout = true;
42
+ logger.info('git checkout main completed', { output: out.trim() });
43
+ } catch (err) {
44
+ logger.warn('git checkout main failed (continuing)', { error: err.message });
45
+ }
46
+
47
+ // Step 3: pull latest
48
+ try {
49
+ const out = execFileSync('git', ['pull', 'origin', 'main'], {
50
+ cwd: workingDir,
51
+ timeout: EXEC_TIMEOUT,
52
+ encoding: 'utf8',
53
+ });
54
+ results.pull = true;
55
+ logger.info('git pull completed', { output: out.trim() });
56
+ } catch (err) {
57
+ logger.warn('git pull failed (continuing)', { error: err.message });
58
+ }
59
+
60
+ return results;
61
+ }
62
+
63
+ module.exports = { resetGitState };