@radnine/claude-session-daemon 0.5.3 → 0.6.0
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/connections/action-cable-client.js +76 -31
- package/src/daemon.js +10 -0
package/package.json
CHANGED
|
@@ -4,14 +4,15 @@ const crypto = require('node:crypto');
|
|
|
4
4
|
const os = require('node:os');
|
|
5
5
|
const WebSocket = require('ws');
|
|
6
6
|
const { BaseConnection } = require('./base-connection');
|
|
7
|
+
const { getGitContext } = require('../git-context');
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* ActionCable WebSocket client that connects to a Rails server.
|
|
10
11
|
*
|
|
11
12
|
* Implements the ActionCable protocol:
|
|
12
|
-
* 1. Connect to ws://<host>/cable?
|
|
13
|
+
* 1. Connect to ws://<host>/cable?pilot_id=<id>
|
|
13
14
|
* 2. Wait for {"type":"welcome"}
|
|
14
|
-
* 3. Subscribe to
|
|
15
|
+
* 3. Subscribe to PilotChannel
|
|
15
16
|
* 4. Exchange messages via ActionCable envelope wrapping
|
|
16
17
|
*
|
|
17
18
|
* Reconnects automatically with exponential backoff on connection loss.
|
|
@@ -68,7 +69,7 @@ class ActionCableClient extends BaseConnection {
|
|
|
68
69
|
return this._connected;
|
|
69
70
|
}
|
|
70
71
|
|
|
71
|
-
/** Whether the
|
|
72
|
+
/** Whether the PilotChannel subscription is confirmed. */
|
|
72
73
|
get subscribed() {
|
|
73
74
|
return this._subscribed;
|
|
74
75
|
}
|
|
@@ -121,7 +122,7 @@ class ActionCableClient extends BaseConnection {
|
|
|
121
122
|
}
|
|
122
123
|
|
|
123
124
|
// ---------------------------------------------------------------------------
|
|
124
|
-
// Outbound message methods (Rails
|
|
125
|
+
// Outbound message methods (Rails PilotChannel format)
|
|
125
126
|
// ---------------------------------------------------------------------------
|
|
126
127
|
|
|
127
128
|
/**
|
|
@@ -187,7 +188,7 @@ class ActionCableClient extends BaseConnection {
|
|
|
187
188
|
type: 'error',
|
|
188
189
|
error_code: errorCode,
|
|
189
190
|
message: message,
|
|
190
|
-
context: {
|
|
191
|
+
context: { pilot_id: this._daemonId, session_id: sessionId },
|
|
191
192
|
severity: 'error',
|
|
192
193
|
timestamp: new Date().toISOString(),
|
|
193
194
|
});
|
|
@@ -201,12 +202,12 @@ class ActionCableClient extends BaseConnection {
|
|
|
201
202
|
* Wrap a daemon message in an ActionCable envelope and send it.
|
|
202
203
|
* The `identifier` and `data` fields are double-encoded (JSON string inside JSON).
|
|
203
204
|
*
|
|
204
|
-
* @param {object} messageData - The payload to send on the
|
|
205
|
+
* @param {object} messageData - The payload to send on the PilotChannel
|
|
205
206
|
*/
|
|
206
207
|
sendDaemonMessage(messageData) {
|
|
207
208
|
const identifier = JSON.stringify({
|
|
208
|
-
channel: '
|
|
209
|
-
|
|
209
|
+
channel: 'PilotChannel',
|
|
210
|
+
pilot_id: this._daemonId,
|
|
210
211
|
});
|
|
211
212
|
|
|
212
213
|
const envelope = {
|
|
@@ -224,13 +225,13 @@ class ActionCableClient extends BaseConnection {
|
|
|
224
225
|
|
|
225
226
|
/**
|
|
226
227
|
* Build the WebSocket URL from the Rails server URL.
|
|
227
|
-
* Converts http(s) to ws(s) and appends /cable?
|
|
228
|
+
* Converts http(s) to ws(s) and appends /cable?pilot_id=<id>.
|
|
228
229
|
* @returns {string}
|
|
229
230
|
*/
|
|
230
231
|
_buildWebSocketUrl() {
|
|
231
232
|
const url = new URL(this._railsUrl);
|
|
232
233
|
const protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
233
|
-
return `${protocol}//${url.host}/cable?
|
|
234
|
+
return `${protocol}//${url.host}/cable?pilot_id=${this._daemonId}`;
|
|
234
235
|
}
|
|
235
236
|
|
|
236
237
|
/**
|
|
@@ -245,7 +246,7 @@ class ActionCableClient extends BaseConnection {
|
|
|
245
246
|
const headers = {};
|
|
246
247
|
if (this._authToken) {
|
|
247
248
|
headers['Authorization'] = `Bearer ${this._authToken}`;
|
|
248
|
-
headers['X-
|
|
249
|
+
headers['X-Pilot-Auth'] = this._authToken;
|
|
249
250
|
}
|
|
250
251
|
|
|
251
252
|
this._ws = new WebSocket(wsUrl, { headers });
|
|
@@ -261,11 +262,13 @@ class ActionCableClient extends BaseConnection {
|
|
|
261
262
|
*/
|
|
262
263
|
_onOpen() {
|
|
263
264
|
this._connected = true;
|
|
264
|
-
const wasReconnecting = this._reconnectAttempts > 0;
|
|
265
|
-
this._reconnectAttempts = 0;
|
|
266
265
|
|
|
267
|
-
|
|
268
|
-
|
|
266
|
+
// NOTE: Do NOT reset _reconnectAttempts here. The backoff counter is only
|
|
267
|
+
// reset once the subscription is confirmed (_handleSubscriptionConfirmed).
|
|
268
|
+
// Resetting on open would cause a tight reconnect loop when the WebSocket
|
|
269
|
+
// connects but the subscription is then rejected (e.g. invalid auth token).
|
|
270
|
+
if (this._reconnectAttempts > 0) {
|
|
271
|
+
this._logger.info('WebSocket reconnected, waiting for subscription confirmation');
|
|
269
272
|
} else {
|
|
270
273
|
this._logger.info('WebSocket connection opened, waiting for welcome');
|
|
271
274
|
}
|
|
@@ -358,10 +361,10 @@ class ActionCableClient extends BaseConnection {
|
|
|
358
361
|
// ---------------------------------------------------------------------------
|
|
359
362
|
|
|
360
363
|
/**
|
|
361
|
-
* Welcome received — subscribe to
|
|
364
|
+
* Welcome received — subscribe to PilotChannel.
|
|
362
365
|
*/
|
|
363
366
|
_handleWelcome() {
|
|
364
|
-
this._logger.info('Received welcome, subscribing to
|
|
367
|
+
this._logger.info('Received welcome, subscribing to PilotChannel');
|
|
365
368
|
this._subscribeToDaemonChannel();
|
|
366
369
|
}
|
|
367
370
|
|
|
@@ -370,7 +373,8 @@ class ActionCableClient extends BaseConnection {
|
|
|
370
373
|
*/
|
|
371
374
|
_handleSubscriptionConfirmed(msg) {
|
|
372
375
|
this._subscribed = true;
|
|
373
|
-
this.
|
|
376
|
+
this._reconnectAttempts = 0; // Backoff resets only after full connect+subscribe success
|
|
377
|
+
this._logger.info('PilotChannel subscription confirmed');
|
|
374
378
|
this.emit('subscribed');
|
|
375
379
|
this._sendDaemonRegistration();
|
|
376
380
|
}
|
|
@@ -380,7 +384,9 @@ class ActionCableClient extends BaseConnection {
|
|
|
380
384
|
*/
|
|
381
385
|
_handleSubscriptionRejected(msg) {
|
|
382
386
|
this._subscribed = false;
|
|
383
|
-
|
|
387
|
+
const reason = (msg && msg.reason) || 'unknown';
|
|
388
|
+
this._logger.warn(`PilotChannel subscription rejected (reason: ${reason}, attempt: ${this._reconnectAttempts + 1}). Backoff will increase.`);
|
|
389
|
+
// Do NOT reset _reconnectAttempts here — let backoff escalate to avoid tight loop
|
|
384
390
|
this._scheduleReconnect();
|
|
385
391
|
}
|
|
386
392
|
|
|
@@ -418,6 +424,9 @@ class ActionCableClient extends BaseConnection {
|
|
|
418
424
|
case 'session_recover':
|
|
419
425
|
this._handleSessionRecovery(inner);
|
|
420
426
|
break;
|
|
427
|
+
case 'session_pause':
|
|
428
|
+
this._handleSessionPause(inner);
|
|
429
|
+
break;
|
|
421
430
|
case 'session_deactivate':
|
|
422
431
|
this._handleSessionDeactivation(inner);
|
|
423
432
|
break;
|
|
@@ -437,14 +446,17 @@ class ActionCableClient extends BaseConnection {
|
|
|
437
446
|
/**
|
|
438
447
|
* Send daemon registration message after subscription is confirmed.
|
|
439
448
|
*/
|
|
440
|
-
_sendDaemonRegistration() {
|
|
449
|
+
async _sendDaemonRegistration() {
|
|
441
450
|
this._logger.info('Sending daemon registration');
|
|
451
|
+
|
|
452
|
+
const gitContext = await this._getGitContext();
|
|
453
|
+
|
|
442
454
|
this.sendDaemonMessage({
|
|
443
|
-
type: '
|
|
455
|
+
type: 'pilot_connect',
|
|
444
456
|
version: '1.0',
|
|
445
457
|
auth: {
|
|
446
458
|
token: this._authToken,
|
|
447
|
-
|
|
459
|
+
pilot_id: this._daemonId,
|
|
448
460
|
},
|
|
449
461
|
capabilities: this._capabilities,
|
|
450
462
|
system_info: {
|
|
@@ -453,6 +465,7 @@ class ActionCableClient extends BaseConnection {
|
|
|
453
465
|
arch: process.arch,
|
|
454
466
|
working_directory: this._workingDir,
|
|
455
467
|
},
|
|
468
|
+
git_context: gitContext,
|
|
456
469
|
});
|
|
457
470
|
}
|
|
458
471
|
|
|
@@ -569,6 +582,22 @@ class ActionCableClient extends BaseConnection {
|
|
|
569
582
|
this._sendRecoveryStatus(sessionId, 'completed', 'recovery_finished');
|
|
570
583
|
}
|
|
571
584
|
|
|
585
|
+
/**
|
|
586
|
+
* Handle session_pause — cancel session gracefully, saving claudeSessionId for resume.
|
|
587
|
+
* @param {object} message
|
|
588
|
+
*/
|
|
589
|
+
async _handleSessionPause(message) {
|
|
590
|
+
const sessionId = message.session_id;
|
|
591
|
+
this._logger.info('Pausing session', { sessionId });
|
|
592
|
+
|
|
593
|
+
try {
|
|
594
|
+
await this._daemon.cancelSession(sessionId);
|
|
595
|
+
this._sendSessionStateUpdate(sessionId, { status: 'paused' });
|
|
596
|
+
} catch (err) {
|
|
597
|
+
this._logger.error('Failed to pause session', { sessionId, error: err.message });
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
572
601
|
/**
|
|
573
602
|
* Handle session_deactivate — terminate the session process.
|
|
574
603
|
* @param {object} message
|
|
@@ -634,7 +663,7 @@ class ActionCableClient extends BaseConnection {
|
|
|
634
663
|
type: 'error',
|
|
635
664
|
error_code: errorCode,
|
|
636
665
|
message: errorMessage,
|
|
637
|
-
context: {
|
|
666
|
+
context: { pilot_id: this._daemonId, ...context },
|
|
638
667
|
severity: 'error',
|
|
639
668
|
timestamp: new Date().toISOString(),
|
|
640
669
|
});
|
|
@@ -671,17 +700,33 @@ class ActionCableClient extends BaseConnection {
|
|
|
671
700
|
/**
|
|
672
701
|
* Send a heartbeat message to the server.
|
|
673
702
|
*/
|
|
674
|
-
_sendHeartbeat() {
|
|
703
|
+
async _sendHeartbeat() {
|
|
704
|
+
const gitContext = await this._getGitContext();
|
|
705
|
+
|
|
675
706
|
this.sendDaemonMessage({
|
|
676
707
|
type: 'heartbeat',
|
|
677
|
-
|
|
708
|
+
pilot_id: this._daemonId,
|
|
678
709
|
status: 'healthy',
|
|
679
710
|
session_summary: this._getSessionSummary(),
|
|
680
711
|
resource_usage: this._getResourceUsage(),
|
|
712
|
+
git_context: gitContext,
|
|
681
713
|
timestamp: new Date().toISOString(),
|
|
682
714
|
});
|
|
683
715
|
}
|
|
684
716
|
|
|
717
|
+
/**
|
|
718
|
+
* Get git context (branch, PR, remote) for the working directory.
|
|
719
|
+
* Returns cached results (30s TTL via git-context module).
|
|
720
|
+
*/
|
|
721
|
+
async _getGitContext() {
|
|
722
|
+
try {
|
|
723
|
+
return await getGitContext(this._workingDir);
|
|
724
|
+
} catch (err) {
|
|
725
|
+
this._logger.warn('Failed to get git context', { error: err.message });
|
|
726
|
+
return null;
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
685
730
|
/**
|
|
686
731
|
* Get a summary of daemon sessions from the daemon health check.
|
|
687
732
|
* @returns {object}
|
|
@@ -728,12 +773,12 @@ class ActionCableClient extends BaseConnection {
|
|
|
728
773
|
// ---------------------------------------------------------------------------
|
|
729
774
|
|
|
730
775
|
/**
|
|
731
|
-
* Send a subscribe command for
|
|
776
|
+
* Send a subscribe command for PilotChannel.
|
|
732
777
|
*/
|
|
733
778
|
_subscribeToDaemonChannel() {
|
|
734
779
|
const identifier = JSON.stringify({
|
|
735
|
-
channel: '
|
|
736
|
-
|
|
780
|
+
channel: 'PilotChannel',
|
|
781
|
+
pilot_id: this._daemonId,
|
|
737
782
|
});
|
|
738
783
|
|
|
739
784
|
this._send({
|
|
@@ -743,12 +788,12 @@ class ActionCableClient extends BaseConnection {
|
|
|
743
788
|
}
|
|
744
789
|
|
|
745
790
|
/**
|
|
746
|
-
* Send an unsubscribe command for
|
|
791
|
+
* Send an unsubscribe command for PilotChannel.
|
|
747
792
|
*/
|
|
748
793
|
_unsubscribe() {
|
|
749
794
|
const identifier = JSON.stringify({
|
|
750
|
-
channel: '
|
|
751
|
-
|
|
795
|
+
channel: 'PilotChannel',
|
|
796
|
+
pilot_id: this._daemonId,
|
|
752
797
|
});
|
|
753
798
|
|
|
754
799
|
this._send({
|
package/src/daemon.js
CHANGED
|
@@ -374,6 +374,16 @@ class Daemon extends EventEmitter {
|
|
|
374
374
|
*/
|
|
375
375
|
_forwardEvents(sessionId, proc) {
|
|
376
376
|
proc.on('output', (data) => {
|
|
377
|
+
// Suppress informational rate_limit_event messages (status: "allowed")
|
|
378
|
+
// These are not actual rate limits — only forward when truly rate-limited
|
|
379
|
+
if (data?.type === 'rate_limit_event') {
|
|
380
|
+
const status = data?.rate_limit_info?.status;
|
|
381
|
+
if (status === 'allowed') {
|
|
382
|
+
this._logger.debug('Suppressing informational rate_limit_event', { sessionId, status });
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
377
387
|
this.emit('session:output', { sessionId, data });
|
|
378
388
|
});
|
|
379
389
|
|