@radnine/claude-session-daemon 0.6.9 → 0.7.1

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.9",
3
+ "version": "0.7.1",
4
4
  "description": "Node.js daemon for managing Claude CLI sessions",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "repository": {
@@ -0,0 +1,56 @@
1
+ 'use strict';
2
+
3
+ const fs = require('node:fs').promises;
4
+ const os = require('node:os');
5
+ const path = require('node:path');
6
+
7
+ const CACHE_TTL_MS = 60_000;
8
+ let _cache = null; // { value, expiresAt }
9
+
10
+ /**
11
+ * Read the logged-in Claude account from ~/.claude.json.
12
+ *
13
+ * The Claude CLI persists the active OAuth account at `oauthAccount.emailAddress`.
14
+ * We surface email + account/org UUIDs so the UI can warn when the user is logged
15
+ * into a different account than they expect (different rate limits).
16
+ *
17
+ * Cached for 60s; returns null if the file is missing/malformed or the field is absent.
18
+ *
19
+ * @param {string} [filePath] - Override path for tests.
20
+ * @returns {Promise<{email: string, account_uuid: string|null, organization_uuid: string|null}|null>}
21
+ */
22
+ async function getClaudeAccount(filePath) {
23
+ const now = Date.now();
24
+ if (!filePath && _cache && _cache.expiresAt > now) {
25
+ return _cache.value;
26
+ }
27
+
28
+ const target = filePath || path.join(os.homedir(), '.claude.json');
29
+ let value = null;
30
+
31
+ try {
32
+ const raw = await fs.readFile(target, 'utf8');
33
+ const parsed = JSON.parse(raw);
34
+ const oauth = parsed && parsed.oauthAccount;
35
+ if (oauth && typeof oauth.emailAddress === 'string' && oauth.emailAddress.length > 0) {
36
+ value = {
37
+ email: oauth.emailAddress,
38
+ account_uuid: oauth.accountUuid || null,
39
+ organization_uuid: oauth.organizationUuid || null,
40
+ };
41
+ }
42
+ } catch (_) {
43
+ value = null;
44
+ }
45
+
46
+ if (!filePath) {
47
+ _cache = { value, expiresAt: now + CACHE_TTL_MS };
48
+ }
49
+ return value;
50
+ }
51
+
52
+ function _resetCacheForTests() {
53
+ _cache = null;
54
+ }
55
+
56
+ module.exports = { getClaudeAccount, _resetCacheForTests };
@@ -30,6 +30,7 @@ class ClaudeProcess extends EventEmitter {
30
30
  * @param {boolean} [options.skipPermissions] - Pass --dangerously-skip-permissions (only safe in Docker)
31
31
  * @param {Function} [options._spawnFn] - For testing: replacement for child_process.spawn
32
32
  * @param {object} [options.sessionEnv] - Extra env vars to inject into the spawned process
33
+ * @param {string} [options.systemPrompt] - Appended to Claude's default system prompt at spawn time via --append-system-prompt
33
34
  */
34
35
  constructor(sessionId, options = {}) {
35
36
  super();
@@ -44,6 +45,7 @@ class ClaudeProcess extends EventEmitter {
44
45
  this._permissionPort = options.permissionPort || null;
45
46
  this._spawnFn = options._spawnFn || defaultSpawn;
46
47
  this._sessionEnv = options.sessionEnv || {};
48
+ this._systemPrompt = options.systemPrompt || null;
47
49
 
48
50
  this._state = 'idle';
49
51
  this._child = null;
@@ -87,6 +89,10 @@ class ClaudeProcess extends EventEmitter {
87
89
 
88
90
  args.push('--replay-user-messages');
89
91
 
92
+ if (this._systemPrompt) {
93
+ args.push('--append-system-prompt', this._systemPrompt);
94
+ }
95
+
90
96
  if (this._skipPermissions) {
91
97
  args.push('--dangerously-skip-permissions');
92
98
  }
@@ -7,6 +7,7 @@ const fs = require('node:fs').promises;
7
7
  const WebSocket = require('ws');
8
8
  const { BaseConnection } = require('./base-connection');
9
9
  const { getGitContext } = require('../git-context');
10
+ const { getClaudeAccount } = require('../claude-account');
10
11
  const daemonPkg = require('../../package.json');
11
12
 
12
13
  /**
@@ -562,7 +563,10 @@ class ActionCableClient extends BaseConnection {
562
563
  async _sendDaemonRegistration() {
563
564
  this._logger.info('Sending daemon registration');
564
565
 
565
- const gitContext = await this._getGitContext();
566
+ const [gitContext, claudeAccount] = await Promise.all([
567
+ this._getGitContext(),
568
+ this._getClaudeAccount(),
569
+ ]);
566
570
 
567
571
  this.sendDaemonMessage({
568
572
  type: 'pilot_connect',
@@ -579,6 +583,7 @@ class ActionCableClient extends BaseConnection {
579
583
  working_directory: this._workingDir,
580
584
  },
581
585
  git_context: gitContext,
586
+ claude_account: claudeAccount,
582
587
  });
583
588
  }
584
589
 
@@ -631,14 +636,16 @@ class ActionCableClient extends BaseConnection {
631
636
  const userId = message.user_id;
632
637
  const claudeSessionId = message.claude_session_id || undefined;
633
638
  const resetGit = !!message.reset_git_state;
639
+ const systemPrompt = message.system_prompt || undefined;
634
640
 
635
- this._logger.info('Received session_activate', { sessionId, userId, claudeSessionId, resetGit });
641
+ this._logger.info('Received session_activate', { sessionId, userId, claudeSessionId, resetGit, hasSystemPrompt: !!systemPrompt });
636
642
 
637
643
  try {
638
644
  const proc = await this._daemon.activateSession(sessionId, {
639
645
  workingDir: this._config.workingDir,
640
646
  claudeSessionId,
641
647
  resetGit,
648
+ systemPrompt,
642
649
  });
643
650
 
644
651
  this.sendDaemonMessage({
@@ -1100,7 +1107,10 @@ class ActionCableClient extends BaseConnection {
1100
1107
  * Send a heartbeat message to the server.
1101
1108
  */
1102
1109
  async _sendHeartbeat() {
1103
- const gitContext = await this._getGitContext();
1110
+ const [gitContext, claudeAccount] = await Promise.all([
1111
+ this._getGitContext(),
1112
+ this._getClaudeAccount(),
1113
+ ]);
1104
1114
 
1105
1115
  this.sendDaemonMessage({
1106
1116
  type: 'heartbeat',
@@ -1110,6 +1120,7 @@ class ActionCableClient extends BaseConnection {
1110
1120
  session_summary: this._getSessionSummary(),
1111
1121
  resource_usage: this._getResourceUsage(),
1112
1122
  git_context: gitContext,
1123
+ claude_account: claudeAccount,
1113
1124
  timestamp: new Date().toISOString(),
1114
1125
  });
1115
1126
  }
@@ -1127,6 +1138,18 @@ class ActionCableClient extends BaseConnection {
1127
1138
  }
1128
1139
  }
1129
1140
 
1141
+ /**
1142
+ * Get the currently logged-in Claude account from ~/.claude.json (60s cache).
1143
+ */
1144
+ async _getClaudeAccount() {
1145
+ try {
1146
+ return await getClaudeAccount();
1147
+ } catch (err) {
1148
+ this._logger.warn('Failed to read Claude account', { error: err.message });
1149
+ return null;
1150
+ }
1151
+ }
1152
+
1130
1153
  /**
1131
1154
  * Get a summary of daemon sessions from the daemon health check.
1132
1155
  * @returns {object}
package/src/daemon.js CHANGED
@@ -125,11 +125,16 @@ class Daemon extends EventEmitter {
125
125
  * @param {object} [options]
126
126
  * @param {string} [options.workingDir]
127
127
  * @param {string} [options.claudeSessionId]
128
+ * @param {string} [options.systemPrompt] - Forwarded to ClaudeProcess; appended to default system prompt at spawn
128
129
  * @returns {Promise<ClaudeProcess>}
129
130
  */
130
131
  async activateSession(sessionId, options = {}) {
131
132
  sessionId = String(sessionId); // Normalize — Rails sends integers, Map keys must match
132
- // If process exists and alive, return it
133
+ // If process exists and alive, return it. Note: any updated options on this
134
+ // call (including systemPrompt) are intentionally ignored — the Claude CLI's
135
+ // system prompt is immutable post-spawn, so we'd be silently lying if we
136
+ // pretended otherwise. To apply a new systemPrompt, terminate the session
137
+ // first; the next activate will spawn fresh with the updated value.
133
138
  const existing = this._processes.get(sessionId);
134
139
  if (existing && existing.isAlive()) {
135
140
  this._logger.debug('Reusing existing session', { sessionId });
@@ -174,6 +179,7 @@ class Daemon extends EventEmitter {
174
179
  skipPermissions: this._config.skipPermissions,
175
180
  permissionPort: this._permissionPort || null,
176
181
  sessionEnv: options.sessionEnv || {},
182
+ systemPrompt: options.systemPrompt,
177
183
  logger: this._logger.child(`Session:${sessionId}`),
178
184
  });
179
185