@radnine/claude-session-daemon 0.6.6 → 0.6.8

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.6",
3
+ "version": "0.6.8",
4
4
  "description": "Node.js daemon for managing Claude CLI sessions",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "repository": {
@@ -151,6 +151,28 @@ class ClaudeProcess extends EventEmitter {
151
151
  return this.sendMessage(command);
152
152
  }
153
153
 
154
+ /**
155
+ * Interrupt the current turn by sending SIGINT to the child process.
156
+ * Unlike kill(), this preserves the process — Claude stops its current
157
+ * response and returns to an idle state, ready for the next message.
158
+ * @returns {boolean} true if signal was sent
159
+ */
160
+ interrupt() {
161
+ if (this._state !== 'running' || !this._child) {
162
+ this._logger.warn('Cannot interrupt: process not running');
163
+ return false;
164
+ }
165
+
166
+ this._logger.info('Sending SIGINT to interrupt current turn', { pid: this._pid });
167
+ try {
168
+ this._child.kill('SIGINT');
169
+ return true;
170
+ } catch (err) {
171
+ this._logger.error('Failed to send SIGINT', { pid: this._pid, error: err.message });
172
+ return false;
173
+ }
174
+ }
175
+
154
176
  /**
155
177
  * Graceful shutdown: close stdin, SIGTERM, wait, SIGKILL if needed.
156
178
  * @param {number} [timeoutMs=5000]
@@ -635,7 +635,7 @@ class ActionCableClient extends BaseConnection {
635
635
  this._logger.info('Received session_activate', { sessionId, userId, claudeSessionId, resetGit });
636
636
 
637
637
  try {
638
- await this._daemon.activateSession(sessionId, {
638
+ const proc = await this._daemon.activateSession(sessionId, {
639
639
  workingDir: this._config.workingDir,
640
640
  claudeSessionId,
641
641
  resetGit,
@@ -644,6 +644,10 @@ class ActionCableClient extends BaseConnection {
644
644
  this.sendDaemonMessage({
645
645
  type: 'session_activated',
646
646
  session_id: sessionId,
647
+ // Report the actual Claude CLI session ID back to Rails so it can be
648
+ // persisted. Without this, Rails has nothing to send on reconnect and
649
+ // the daemon spawns a fresh CLI — losing all conversation context.
650
+ claude_session_id: proc.claudeSessionId,
647
651
  stream_config: {
648
652
  buffer_mode: 'line',
649
653
  max_message_size: 8192,
@@ -758,27 +762,46 @@ class ActionCableClient extends BaseConnection {
758
762
  }
759
763
 
760
764
  /**
761
- * Handle command_cancel — cancel the active command for a session, preserving
762
- * the Claude CLI session ID so the next command resumes the conversation.
765
+ * Handle command_cancel — stop the current turn and immediately respawn
766
+ * the CLI with --resume so the prompt cache stays warm.
767
+ *
768
+ * Claude CLI in stream-json mode exits on any signal (SIGINT/SIGTERM) rather
769
+ * than interrupting the turn, so we must kill and respawn. The key optimisation
770
+ * is doing this *immediately* — a prompt-cache TTL is 5 minutes, so a fast
771
+ * respawn means the resume replays at ~10% of the full input cost.
763
772
  * @param {object} message
764
773
  */
765
774
  async _handleCommandCancel(message) {
766
775
  const sessionId = message.session_id;
767
776
  const commandId = message.command_id;
768
- this._logger.info('Cancelling command', { sessionId, commandId });
777
+ this._logger.info('Interrupting command (kill + immediate respawn)', { sessionId, commandId });
769
778
 
770
779
  try {
771
- // Mark session as cancelled before killing, so the session:exit event
772
- // doesn't produce a spurious 'complete' message.
780
+ // Suppress the exit→complete event for this session
773
781
  this._cancelledSessions.add(sessionId);
774
782
 
783
+ // Kill the process — cancelSession saves claudeSessionId for resume
775
784
  const result = await this._daemon.cancelSession(sessionId);
776
785
 
786
+ // Clear active command tracking
787
+ this._activeCommands.delete(sessionId);
788
+
777
789
  if (result.success) {
778
- // Clear active command tracking
779
- this._activeCommands.delete(sessionId);
790
+ // Immediately respawn with --resume while the prompt cache is warm.
791
+ // activateSession picks up the saved claudeSessionId and passes --resume.
792
+ try {
793
+ await this._daemon.activateSession(sessionId, {
794
+ workingDir: this._config.workingDir,
795
+ });
796
+ this._logger.info('Session respawned after interrupt', { sessionId });
797
+ } catch (respawnErr) {
798
+ // Non-fatal — the next command will trigger lazy re-activation
799
+ this._logger.warn('Immediate respawn failed (will retry on next command)', {
800
+ sessionId,
801
+ error: respawnErr.message,
802
+ });
803
+ }
780
804
 
781
- // Notify Rails that the command was cancelled
782
805
  this.sendDaemonMessage({
783
806
  type: 'command_cancelled',
784
807
  session_id: sessionId,
package/src/daemon.js CHANGED
@@ -283,6 +283,41 @@ class Daemon extends EventEmitter {
283
283
  };
284
284
  }
285
285
 
286
+ /**
287
+ * Interrupt the active turn for a session without killing the process.
288
+ * Sends SIGINT — Claude stops its current response but stays alive.
289
+ * @param {string} sessionId
290
+ * @returns {{ success: boolean, message: string }}
291
+ */
292
+ interruptSession(sessionId) {
293
+ sessionId = String(sessionId);
294
+ const proc = this._processes.get(sessionId);
295
+
296
+ if (!proc) {
297
+ this._logger.warn('Interrupt requested for unknown session', { sessionId });
298
+ return {
299
+ success: false,
300
+ message: `Session not found: "${sessionId}"`,
301
+ };
302
+ }
303
+
304
+ if (!proc.isAlive()) {
305
+ this._logger.warn('Interrupt requested for dead process', { sessionId, pid: proc.pid });
306
+ return {
307
+ success: false,
308
+ message: `Session process already stopped: "${sessionId}"`,
309
+ };
310
+ }
311
+
312
+ const sent = proc.interrupt();
313
+ return {
314
+ success: sent,
315
+ message: sent
316
+ ? `Session interrupted: "${sessionId}"`
317
+ : `Failed to interrupt session: "${sessionId}"`,
318
+ };
319
+ }
320
+
286
321
  /**
287
322
  * Count of active processes.
288
323
  * @returns {number}