@radnine/claude-session-daemon 0.6.7 → 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.7",
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]
@@ -762,27 +762,46 @@ class ActionCableClient extends BaseConnection {
762
762
  }
763
763
 
764
764
  /**
765
- * Handle command_cancel — cancel the active command for a session, preserving
766
- * 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.
767
772
  * @param {object} message
768
773
  */
769
774
  async _handleCommandCancel(message) {
770
775
  const sessionId = message.session_id;
771
776
  const commandId = message.command_id;
772
- this._logger.info('Cancelling command', { sessionId, commandId });
777
+ this._logger.info('Interrupting command (kill + immediate respawn)', { sessionId, commandId });
773
778
 
774
779
  try {
775
- // Mark session as cancelled before killing, so the session:exit event
776
- // doesn't produce a spurious 'complete' message.
780
+ // Suppress the exit→complete event for this session
777
781
  this._cancelledSessions.add(sessionId);
778
782
 
783
+ // Kill the process — cancelSession saves claudeSessionId for resume
779
784
  const result = await this._daemon.cancelSession(sessionId);
780
785
 
786
+ // Clear active command tracking
787
+ this._activeCommands.delete(sessionId);
788
+
781
789
  if (result.success) {
782
- // Clear active command tracking
783
- 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
+ }
784
804
 
785
- // Notify Rails that the command was cancelled
786
805
  this.sendDaemonMessage({
787
806
  type: 'command_cancelled',
788
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}