@radnine/claude-session-daemon 0.2.1 → 0.2.3
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 +2 -2
- package/src/claude-process.js +1 -0
- package/src/cli.js +183 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@radnine/claude-session-daemon",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Node.js daemon for managing Claude CLI sessions",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"repository": {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
},
|
|
11
11
|
"main": "src/index.js",
|
|
12
12
|
"bin": {
|
|
13
|
-
"claude-session-daemon": "src/
|
|
13
|
+
"claude-session-daemon": "src/cli.js"
|
|
14
14
|
},
|
|
15
15
|
"files": [
|
|
16
16
|
"src/**",
|
package/src/claude-process.js
CHANGED
|
@@ -64,6 +64,7 @@ class ClaudeProcess extends EventEmitter {
|
|
|
64
64
|
'--output-format', 'stream-json',
|
|
65
65
|
'--session-id', this._claudeSessionId,
|
|
66
66
|
'--replay-user-messages',
|
|
67
|
+
'--dangerously-skip-permissions',
|
|
67
68
|
];
|
|
68
69
|
|
|
69
70
|
const env = { ...process.env, CLAUDE_SESSION_ID: this._sessionId };
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const { spawn } = require('node:child_process');
|
|
5
|
+
const fs = require('node:fs');
|
|
6
|
+
const path = require('node:path');
|
|
7
|
+
const net = require('node:net');
|
|
8
|
+
|
|
9
|
+
const PID_FILE = path.join(process.cwd(), '.claude-daemon.pid');
|
|
10
|
+
const DAEMON_SCRIPT = path.join(__dirname, 'index.js');
|
|
11
|
+
|
|
12
|
+
const command = process.argv[2] || 'start';
|
|
13
|
+
|
|
14
|
+
switch (command) {
|
|
15
|
+
case 'start':
|
|
16
|
+
start();
|
|
17
|
+
break;
|
|
18
|
+
case 'stop':
|
|
19
|
+
stop();
|
|
20
|
+
break;
|
|
21
|
+
case 'restart':
|
|
22
|
+
stop();
|
|
23
|
+
start();
|
|
24
|
+
break;
|
|
25
|
+
case 'status':
|
|
26
|
+
status();
|
|
27
|
+
break;
|
|
28
|
+
default:
|
|
29
|
+
console.error(`Unknown command: ${command}`);
|
|
30
|
+
console.error('Usage: claude-session-daemon [start|stop|restart|status]');
|
|
31
|
+
process.exit(1);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function readPid() {
|
|
35
|
+
try {
|
|
36
|
+
return parseInt(fs.readFileSync(PID_FILE, 'utf8').trim(), 10);
|
|
37
|
+
} catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function writePid(pid) {
|
|
43
|
+
fs.writeFileSync(PID_FILE, String(pid));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function removePid() {
|
|
47
|
+
try {
|
|
48
|
+
fs.unlinkSync(PID_FILE);
|
|
49
|
+
} catch {
|
|
50
|
+
// ignore
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function isProcessAlive(pid) {
|
|
55
|
+
try {
|
|
56
|
+
process.kill(pid, 0);
|
|
57
|
+
return true;
|
|
58
|
+
} catch {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function killProcess(pid) {
|
|
64
|
+
try {
|
|
65
|
+
process.kill(pid, 'SIGTERM');
|
|
66
|
+
// Wait briefly for graceful shutdown
|
|
67
|
+
const start = Date.now();
|
|
68
|
+
while (isProcessAlive(pid) && Date.now() - start < 5000) {
|
|
69
|
+
// busy-wait in small increments
|
|
70
|
+
const waitUntil = Date.now() + 100;
|
|
71
|
+
while (Date.now() < waitUntil) { /* spin */ }
|
|
72
|
+
}
|
|
73
|
+
if (isProcessAlive(pid)) {
|
|
74
|
+
process.kill(pid, 'SIGKILL');
|
|
75
|
+
}
|
|
76
|
+
} catch {
|
|
77
|
+
// process already dead
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function checkPort(port) {
|
|
82
|
+
return new Promise((resolve) => {
|
|
83
|
+
const s = net.createConnection({ port }, () => {
|
|
84
|
+
s.end();
|
|
85
|
+
resolve(true);
|
|
86
|
+
});
|
|
87
|
+
s.on('error', () => resolve(false));
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function killStaleProcessOnPort(port) {
|
|
92
|
+
const inUse = await checkPort(port);
|
|
93
|
+
if (!inUse) return false;
|
|
94
|
+
|
|
95
|
+
console.log(`Port ${port} is in use. Attempting to stop existing process...`);
|
|
96
|
+
|
|
97
|
+
// Try PID file first
|
|
98
|
+
const pid = readPid();
|
|
99
|
+
if (pid && isProcessAlive(pid)) {
|
|
100
|
+
killProcess(pid);
|
|
101
|
+
removePid();
|
|
102
|
+
console.log(`Stopped process ${pid}`);
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// No PID file — warn the user
|
|
107
|
+
console.error(`Port ${port} is occupied but no PID file found.`);
|
|
108
|
+
console.error(`Kill the process manually: lsof -ti :${port} | xargs kill`);
|
|
109
|
+
process.exit(1);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function start() {
|
|
113
|
+
const port = process.env.CLAUDE_DAEMON_PORT || 3001;
|
|
114
|
+
|
|
115
|
+
// Clean up stale PID
|
|
116
|
+
const existingPid = readPid();
|
|
117
|
+
if (existingPid && isProcessAlive(existingPid)) {
|
|
118
|
+
console.log(`Daemon already running (PID ${existingPid}). Use 'restart' to replace it.`);
|
|
119
|
+
process.exit(0);
|
|
120
|
+
}
|
|
121
|
+
removePid();
|
|
122
|
+
|
|
123
|
+
// Check if port is occupied
|
|
124
|
+
await killStaleProcessOnPort(port);
|
|
125
|
+
|
|
126
|
+
// Fork the daemon as a foreground process (inherits stdio for log visibility)
|
|
127
|
+
const child = spawn(process.execPath, [DAEMON_SCRIPT], {
|
|
128
|
+
stdio: 'inherit',
|
|
129
|
+
env: process.env,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
if (child.pid) {
|
|
133
|
+
writePid(child.pid);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
child.on('exit', (code) => {
|
|
137
|
+
removePid();
|
|
138
|
+
process.exit(code || 0);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// Forward signals to child
|
|
142
|
+
for (const sig of ['SIGTERM', 'SIGINT']) {
|
|
143
|
+
process.on(sig, () => {
|
|
144
|
+
child.kill(sig);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function stop() {
|
|
150
|
+
const pid = readPid();
|
|
151
|
+
if (!pid) {
|
|
152
|
+
console.log('No daemon PID file found. Nothing to stop.');
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (!isProcessAlive(pid)) {
|
|
157
|
+
console.log(`Daemon (PID ${pid}) is not running. Cleaning up PID file.`);
|
|
158
|
+
removePid();
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
console.log(`Stopping daemon (PID ${pid})...`);
|
|
163
|
+
killProcess(pid);
|
|
164
|
+
removePid();
|
|
165
|
+
console.log('Daemon stopped.');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function status() {
|
|
169
|
+
const pid = readPid();
|
|
170
|
+
if (!pid) {
|
|
171
|
+
console.log('Daemon is not running (no PID file).');
|
|
172
|
+
process.exit(1);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (isProcessAlive(pid)) {
|
|
176
|
+
console.log(`Daemon is running (PID ${pid}).`);
|
|
177
|
+
process.exit(0);
|
|
178
|
+
} else {
|
|
179
|
+
console.log(`Daemon is not running (stale PID ${pid}). Cleaning up.`);
|
|
180
|
+
removePid();
|
|
181
|
+
process.exit(1);
|
|
182
|
+
}
|
|
183
|
+
}
|