tacticremote 1.6.5 → 1.7.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/lib/authChallengeManager.js +1 -134
- package/lib/claudeSessionManager.js +1 -0
- package/lib/claudeTransport.js +1 -0
- package/lib/errorCodes.js +1 -127
- package/lib/execSafe.js +1 -218
- package/lib/fileService.js +1 -177
- package/lib/hookInstaller.js +21 -7
- package/lib/outputParser.js +1 -106
- package/lib/pushClient.js +1 -0
- package/lib/rateLimiter.js +1 -111
- package/lib/serverIdentity.js +1 -0
- package/lib/streamJsonParser.js +1 -0
- package/lib/tmuxManager.js +1 -671
- package/lib/uploadService.js +1 -366
- package/lib/validators.js +1 -350
- package/package.json +2 -1
- package/routes/httpHandler.js +3 -1
- package/routes/wsHandler.js +418 -364
- package/server.js +67 -152
- package/shared/server-core/authChallengeManager.js +108 -0
- package/shared/server-core/claudeSessionManager.js +222 -0
- package/shared/server-core/claudeTransport.js +176 -0
- package/shared/server-core/errorCodes.js +91 -0
- package/shared/server-core/execSafe.js +185 -0
- package/shared/server-core/fileService.js +155 -0
- package/shared/server-core/outputParser.js +174 -0
- package/shared/server-core/protocol-manifest.json +59 -0
- package/shared/server-core/protocol-manifest.schema.json +90 -0
- package/shared/server-core/protocolManifest.js +42 -0
- package/shared/server-core/pushClient.js +198 -0
- package/shared/server-core/rateLimiter.js +98 -0
- package/shared/server-core/serverConnectionLifecycle.js +118 -0
- package/shared/server-core/serverHandshake.js +72 -0
- package/shared/server-core/serverIdentity.js +102 -0
- package/shared/server-core/streamJsonParser.js +321 -0
- package/shared/server-core/tmuxManager.js +596 -0
- package/shared/server-core/unixProcessLifecycle.js +96 -0
- package/shared/server-core/unixServerHooks.js +148 -0
- package/shared/server-core/unixWsRuntime.js +412 -0
- package/shared/server-core/uploadService.js +321 -0
- package/shared/server-core/validators.js +274 -0
- package/shared/server-core/windowsServerHooks.js +174 -0
- package/shared/server-core/windowsWsRuntime.js +413 -0
package/routes/wsHandler.js
CHANGED
|
@@ -11,6 +11,9 @@ const { parseClaudeOutput } = require('../lib/outputParser');
|
|
|
11
11
|
const { tmuxCapture, tmuxGetPanePath, execSafe } = require('../lib/execSafe');
|
|
12
12
|
const { validatePath, validateSessionName } = require('../lib/validators');
|
|
13
13
|
const authChallengeManager = require('../lib/authChallengeManager');
|
|
14
|
+
const { ClaudeSessionManager } = require('../lib/claudeSessionManager');
|
|
15
|
+
const pushClient = require('../lib/pushClient');
|
|
16
|
+
const { createUnixWsRuntime } = require('../shared/server-core/unixWsRuntime');
|
|
14
17
|
|
|
15
18
|
/**
|
|
16
19
|
* Create WebSocket message handler
|
|
@@ -18,370 +21,53 @@ const authChallengeManager = require('../lib/authChallengeManager');
|
|
|
18
21
|
* @returns {Object} Handler methods
|
|
19
22
|
*/
|
|
20
23
|
function createWsHandler(server) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Send a response to a request (new protocol)
|
|
67
|
-
* @param {WebSocket} ws - WebSocket connection
|
|
68
|
-
* @param {string} requestId - Original request ID
|
|
69
|
-
* @param {Object} payload - Response payload
|
|
70
|
-
*/
|
|
71
|
-
function sendResponse(ws, requestId, payload) {
|
|
72
|
-
sendToClient(ws, {
|
|
73
|
-
type: 'res',
|
|
74
|
-
id: requestId,
|
|
75
|
-
ok: true,
|
|
76
|
-
payload
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Send an error response to a request (new protocol)
|
|
82
|
-
* @param {WebSocket} ws - WebSocket connection
|
|
83
|
-
* @param {string} requestId - Original request ID
|
|
84
|
-
* @param {Object} error - Error object from createError
|
|
85
|
-
*/
|
|
86
|
-
function sendErrorResponse(ws, requestId, error) {
|
|
87
|
-
sendToClient(ws, {
|
|
88
|
-
type: 'res',
|
|
89
|
-
id: requestId,
|
|
90
|
-
ok: false,
|
|
91
|
-
error
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Send an event to client (new protocol)
|
|
97
|
-
* @param {WebSocket} ws - WebSocket connection
|
|
98
|
-
* @param {string} eventName - Event name
|
|
99
|
-
* @param {Object} payload - Event payload
|
|
100
|
-
*/
|
|
101
|
-
function sendEvent(ws, eventName, payload) {
|
|
102
|
-
sendToClient(ws, {
|
|
103
|
-
type: 'event',
|
|
104
|
-
event: eventName,
|
|
105
|
-
payload
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Broadcast event to all clients
|
|
111
|
-
* @param {string} eventName - Event name
|
|
112
|
-
* @param {Object} payload - Event payload
|
|
113
|
-
*/
|
|
114
|
-
function broadcastEvent(eventName, payload) {
|
|
115
|
-
server.clients.forEach((ws) => {
|
|
116
|
-
sendEvent(ws, eventName, payload);
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Wrap an operation that accesses a path which may be sensitive.
|
|
122
|
-
* If the path is in a sensitive directory and no grant exists,
|
|
123
|
-
* sends an auth_challenge event to the client and awaits authorization.
|
|
124
|
-
*
|
|
125
|
-
* @param {WebSocket} ws
|
|
126
|
-
* @param {string} userPath - The user-provided path
|
|
127
|
-
* @param {string} method - The operation method name (e.g. 'list_directory')
|
|
128
|
-
* @param {Object} validateOptions - Options for validatePath
|
|
129
|
-
* @param {Function} operation - async () => result to execute if authorized
|
|
130
|
-
* @returns {Object} The operation result, or an error object
|
|
131
|
-
*/
|
|
132
|
-
async function withSensitivePathAuth(ws, userPath, method, validateOptions, operation) {
|
|
133
|
-
const validation = validatePath(userPath, validateOptions);
|
|
134
|
-
console.log(`[DEBUG withSensitivePathAuth] method=${method}, userPath=${userPath}, valid=${validation.valid}, requiresAuth=${validation.requiresAuth}, error=${validation.error}`);
|
|
135
|
-
|
|
136
|
-
if (!validation.valid && validation.requiresAuth) {
|
|
137
|
-
const sensitiveDir = validation.sensitiveDir;
|
|
138
|
-
|
|
139
|
-
// Already granted?
|
|
140
|
-
if (authChallengeManager.isGranted(ws, sensitiveDir)) {
|
|
141
|
-
return await operation();
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// Send challenge to client and wait for response
|
|
145
|
-
const { challengeId, expiresAt, promise } = authChallengeManager.createChallenge(
|
|
146
|
-
ws, userPath, sensitiveDir, method
|
|
147
|
-
);
|
|
148
|
-
|
|
149
|
-
sendEvent(ws, 'auth_challenge', {
|
|
150
|
-
challengeId,
|
|
151
|
-
path: userPath,
|
|
152
|
-
sensitiveDir,
|
|
153
|
-
method,
|
|
154
|
-
expiresAt
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
const granted = await promise;
|
|
158
|
-
if (granted) {
|
|
159
|
-
return await operation();
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return {
|
|
163
|
-
success: false,
|
|
164
|
-
error: {
|
|
165
|
-
type: 'error',
|
|
166
|
-
code: 'AUTH_DENIED',
|
|
167
|
-
error: `Access to ${sensitiveDir} was denied`,
|
|
168
|
-
retryable: false
|
|
169
|
-
}
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// Not a sensitive dir issue — let the operation handle normal validation
|
|
174
|
-
return await operation();
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
async function collectGitInfo(cwd) {
|
|
178
|
-
try {
|
|
179
|
-
const [branch, gitStatus, diffStat, branchList] = await Promise.all([
|
|
180
|
-
execSafe('git', ['-C', cwd, 'branch', '--show-current'], { timeout: 5000 }).catch(() => ''),
|
|
181
|
-
execSafe('git', ['-C', cwd, 'status', '--short'], { timeout: 5000 }).catch(() => ''),
|
|
182
|
-
execSafe('git', ['-C', cwd, 'diff', '--stat', 'HEAD'], { timeout: 10000 }).catch(() => ''),
|
|
183
|
-
execSafe('git', ['-C', cwd, 'branch', '--format=%(refname:short)'], { timeout: 5000 }).catch(() => '')
|
|
184
|
-
]);
|
|
185
|
-
return {
|
|
186
|
-
cwd,
|
|
187
|
-
branch: branch.trim(),
|
|
188
|
-
branches: branchList.trim().split('\n').filter(Boolean),
|
|
189
|
-
git_status: gitStatus.trim(),
|
|
190
|
-
diff_stat: diffStat.trim(),
|
|
191
|
-
is_git_repo: true
|
|
192
|
-
};
|
|
193
|
-
} catch {
|
|
194
|
-
return { cwd, is_git_repo: false };
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Broadcast message to all clients
|
|
200
|
-
*/
|
|
201
|
-
function broadcast(message) {
|
|
202
|
-
server.clients.forEach((ws) => {
|
|
203
|
-
sendToClient(ws, message);
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Start monitoring output for a session
|
|
209
|
-
* Supports multiple clients subscribing to the same session
|
|
210
|
-
*/
|
|
211
|
-
function startOutputMonitor(ws, sessionName) {
|
|
212
|
-
// Track in reverse map
|
|
213
|
-
if (!clientMonitors.has(ws)) clientMonitors.set(ws, new Set());
|
|
214
|
-
clientMonitors.get(ws).add(sessionName);
|
|
215
|
-
|
|
216
|
-
// If this session already has an active monitor, just add this client as subscriber
|
|
217
|
-
if (outputMonitors.has(sessionName)) {
|
|
218
|
-
const monitor = outputMonitors.get(sessionName);
|
|
219
|
-
monitor.subscribers.set(ws, true);
|
|
220
|
-
monitor.checkCount = 0; // Reset timeout on re-subscribe
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
let lastOutput = '';
|
|
225
|
-
let stableCount = 0;
|
|
226
|
-
const maxChecks = 120; // 2 minutes max
|
|
227
|
-
|
|
228
|
-
const subscribers = new Map();
|
|
229
|
-
subscribers.set(ws, true);
|
|
230
|
-
|
|
231
|
-
const monitorState = { subscribers, intervalId: null, checkCount: 0 };
|
|
232
|
-
|
|
233
|
-
const intervalId = setInterval(async () => {
|
|
234
|
-
monitorState.checkCount++;
|
|
235
|
-
|
|
236
|
-
// Remove disconnected subscribers
|
|
237
|
-
for (const [subWs] of subscribers) {
|
|
238
|
-
if (subWs.readyState !== 1) {
|
|
239
|
-
subscribers.delete(subWs);
|
|
240
|
-
const sessions = clientMonitors.get(subWs);
|
|
241
|
-
if (sessions) sessions.delete(sessionName);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// Stop if no subscribers left
|
|
246
|
-
if (subscribers.size === 0 || monitorState.checkCount > maxChecks) {
|
|
247
|
-
clearInterval(intervalId);
|
|
248
|
-
outputMonitors.delete(sessionName);
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
try {
|
|
253
|
-
const output = await tmuxCapture(sessionName, 2000);
|
|
254
|
-
|
|
255
|
-
if (output !== lastOutput) {
|
|
256
|
-
lastOutput = output;
|
|
257
|
-
stableCount = 0;
|
|
258
|
-
|
|
259
|
-
const parsed = parseClaudeOutput(output);
|
|
260
|
-
for (const [subWs] of subscribers) {
|
|
261
|
-
sendOutputToClient(subWs, {
|
|
262
|
-
type: 'claude_output',
|
|
263
|
-
content: parsed.display,
|
|
264
|
-
session: sessionName
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
} else {
|
|
268
|
-
stableCount++;
|
|
269
|
-
|
|
270
|
-
if (stableCount >= 5) {
|
|
271
|
-
const isWaiting = tmux.isWaitingForInput(output);
|
|
272
|
-
|
|
273
|
-
if (isWaiting) {
|
|
274
|
-
const parsed = parseClaudeOutput(output);
|
|
275
|
-
for (const [subWs] of subscribers) {
|
|
276
|
-
sendToClient(subWs, {
|
|
277
|
-
type: 'status',
|
|
278
|
-
status: 'waiting'
|
|
279
|
-
});
|
|
280
|
-
sendToClient(subWs, {
|
|
281
|
-
type: 'claude_output',
|
|
282
|
-
content: parsed.display,
|
|
283
|
-
userQuestion: parsed.userQuestion,
|
|
284
|
-
claudeResponse: parsed.claudeResponse,
|
|
285
|
-
session: sessionName,
|
|
286
|
-
final: true
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
clearInterval(intervalId);
|
|
292
|
-
outputMonitors.delete(sessionName);
|
|
293
|
-
for (const [subWs] of subscribers) {
|
|
294
|
-
const sessions = clientMonitors.get(subWs);
|
|
295
|
-
if (sessions) sessions.delete(sessionName);
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
} catch (error) {
|
|
300
|
-
console.error('Monitor error:', error.message);
|
|
301
|
-
}
|
|
302
|
-
}, 1000);
|
|
303
|
-
|
|
304
|
-
monitorState.intervalId = intervalId;
|
|
305
|
-
outputMonitors.set(sessionName, monitorState);
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Stop monitor for a session
|
|
310
|
-
*/
|
|
311
|
-
function stopMonitor(sessionName) {
|
|
312
|
-
if (outputMonitors.has(sessionName)) {
|
|
313
|
-
const entry = outputMonitors.get(sessionName);
|
|
314
|
-
clearInterval(entry.intervalId);
|
|
315
|
-
for (const [subWs] of entry.subscribers) {
|
|
316
|
-
const sessions = clientMonitors.get(subWs);
|
|
317
|
-
if (sessions) sessions.delete(sessionName);
|
|
318
|
-
}
|
|
319
|
-
outputMonitors.delete(sessionName);
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
/**
|
|
324
|
-
* Remove a specific client from a session monitor (without stopping the whole monitor)
|
|
325
|
-
*/
|
|
326
|
-
function removeClientFromMonitor(ws, sessionName) {
|
|
327
|
-
if (outputMonitors.has(sessionName)) {
|
|
328
|
-
const entry = outputMonitors.get(sessionName);
|
|
329
|
-
entry.subscribers.delete(ws);
|
|
330
|
-
const sessions = clientMonitors.get(ws);
|
|
331
|
-
if (sessions) sessions.delete(sessionName);
|
|
332
|
-
// If no subscribers left, stop the monitor entirely
|
|
333
|
-
if (entry.subscribers.size === 0) {
|
|
334
|
-
clearInterval(entry.intervalId);
|
|
335
|
-
outputMonitors.delete(sessionName);
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
* Clean up all monitors and auth state for a disconnected client
|
|
342
|
-
*/
|
|
343
|
-
function cleanupClient(ws) {
|
|
344
|
-
const sessions = clientMonitors.get(ws);
|
|
345
|
-
if (sessions) {
|
|
346
|
-
for (const sessionName of sessions) {
|
|
347
|
-
if (outputMonitors.has(sessionName)) {
|
|
348
|
-
const entry = outputMonitors.get(sessionName);
|
|
349
|
-
entry.subscribers.delete(ws);
|
|
350
|
-
if (entry.subscribers.size === 0) {
|
|
351
|
-
clearInterval(entry.intervalId);
|
|
352
|
-
outputMonitors.delete(sessionName);
|
|
353
|
-
}
|
|
24
|
+
let claudeSessionManager;
|
|
25
|
+
|
|
26
|
+
const runtime = createUnixWsRuntime({
|
|
27
|
+
server,
|
|
28
|
+
tmux,
|
|
29
|
+
fileService,
|
|
30
|
+
authChallengeManager,
|
|
31
|
+
parseClaudeOutput,
|
|
32
|
+
tmuxCapture,
|
|
33
|
+
execSafe,
|
|
34
|
+
validatePath,
|
|
35
|
+
getStructuredSessionManager: () => claudeSessionManager
|
|
36
|
+
});
|
|
37
|
+
const {
|
|
38
|
+
outputMonitors,
|
|
39
|
+
clientMonitors,
|
|
40
|
+
getClientPath,
|
|
41
|
+
updateClientState,
|
|
42
|
+
setClientPath,
|
|
43
|
+
normalizeClientSession,
|
|
44
|
+
sendToClient,
|
|
45
|
+
sendResponse,
|
|
46
|
+
sendErrorResponse,
|
|
47
|
+
sendEvent,
|
|
48
|
+
broadcastEvent,
|
|
49
|
+
broadcast,
|
|
50
|
+
withSensitivePathAuth,
|
|
51
|
+
collectGitInfo,
|
|
52
|
+
collectWorktrees,
|
|
53
|
+
startOutputMonitor,
|
|
54
|
+
stopMonitor,
|
|
55
|
+
removeClientFromMonitor,
|
|
56
|
+
cleanupClient,
|
|
57
|
+
captureAndBroadcast
|
|
58
|
+
} = runtime;
|
|
59
|
+
|
|
60
|
+
claudeSessionManager = new ClaudeSessionManager({
|
|
61
|
+
broadcastToSubscribers: (subscribers, eventName, payload) => {
|
|
62
|
+
for (const ws of subscribers) {
|
|
63
|
+
if (ws.readyState === 1) {
|
|
64
|
+
sendEvent(ws, eventName, payload);
|
|
354
65
|
}
|
|
355
66
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
}
|
|
67
|
+
},
|
|
68
|
+
allowedBasePath: server.allowedBasePath
|
|
69
|
+
});
|
|
360
70
|
|
|
361
|
-
/**
|
|
362
|
-
* Capture and broadcast output
|
|
363
|
-
*/
|
|
364
|
-
async function captureAndBroadcast(sessionName, reason = 'update', originWs = null) {
|
|
365
|
-
const result = await tmux.captureOutput(sessionName, 2000);
|
|
366
|
-
if (result.success) {
|
|
367
|
-
const message = {
|
|
368
|
-
type: 'claude_output',
|
|
369
|
-
content: result.content,
|
|
370
|
-
userQuestion: result.userQuestion,
|
|
371
|
-
claudeResponse: result.claudeResponse,
|
|
372
|
-
session: sessionName,
|
|
373
|
-
reason: reason
|
|
374
|
-
};
|
|
375
|
-
const monitor = outputMonitors.get(sessionName);
|
|
376
|
-
if (monitor) {
|
|
377
|
-
for (const [subWs] of monitor.subscribers) {
|
|
378
|
-
sendOutputToClient(subWs, message);
|
|
379
|
-
}
|
|
380
|
-
} else if (originWs) {
|
|
381
|
-
sendOutputToClient(originWs, message);
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
71
|
|
|
386
72
|
/**
|
|
387
73
|
* Handle incoming WebSocket message
|
|
@@ -419,7 +105,18 @@ function createWsHandler(server) {
|
|
|
419
105
|
}
|
|
420
106
|
|
|
421
107
|
case 'send_to_claude': {
|
|
422
|
-
|
|
108
|
+
// Safety guard: reject terminal steer when a structured session has a pending approval
|
|
109
|
+
if (claudeSessionManager.hasAnyPendingApproval()) {
|
|
110
|
+
console.warn('[send_to_claude] Blocked: a structured session has a pending tool approval — refusing terminal send to prevent accidental y/n');
|
|
111
|
+
sendErrorResponse(ws, id, {
|
|
112
|
+
type: 'error',
|
|
113
|
+
code: 'STEER_BLOCKED_BY_APPROVAL',
|
|
114
|
+
error: 'Cannot send to terminal while a tool approval is pending in a structured session',
|
|
115
|
+
retryable: false
|
|
116
|
+
});
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
const result = await tmux.sendToClaude(params.content, params.session, params.path || getClientPath(ws), params.cols, params.rows, params.command);
|
|
423
120
|
if (result.success) {
|
|
424
121
|
sendResponse(ws, id, { status: 'sent' });
|
|
425
122
|
startOutputMonitor(ws, params.session || tmux.DEFAULT_SESSION);
|
|
@@ -484,6 +181,7 @@ function createWsHandler(server) {
|
|
|
484
181
|
|
|
485
182
|
const result = await tmux.attachSession(params.session);
|
|
486
183
|
if (result.success) {
|
|
184
|
+
updateClientState(ws, { currentSession: normalizeClientSession(params.session) });
|
|
487
185
|
if (result.path) setClientPath(ws, result.path);
|
|
488
186
|
sendResponse(ws, id, {
|
|
489
187
|
path: getClientPath(ws),
|
|
@@ -499,6 +197,16 @@ function createWsHandler(server) {
|
|
|
499
197
|
break;
|
|
500
198
|
}
|
|
501
199
|
|
|
200
|
+
case 'client_state': {
|
|
201
|
+
updateClientState(ws, {
|
|
202
|
+
foreground: params.foreground === true,
|
|
203
|
+
currentSession: normalizeClientSession(params.currentSession),
|
|
204
|
+
supportsClientState: true
|
|
205
|
+
});
|
|
206
|
+
sendResponse(ws, id, { status: 'ok' });
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
|
|
502
210
|
case 'create_session': {
|
|
503
211
|
console.log(`[DEBUG create_session] params.name=${params.name}, params.path=${params.path}, clientPath=${getClientPath(ws)}, allowedBasePath=${server.allowedBasePath}`);
|
|
504
212
|
const result = await withSensitivePathAuth(ws, params.path || getClientPath(ws), 'create_session',
|
|
@@ -743,6 +451,334 @@ function createWsHandler(server) {
|
|
|
743
451
|
break;
|
|
744
452
|
}
|
|
745
453
|
|
|
454
|
+
case 'list_worktrees': {
|
|
455
|
+
// Discover git worktrees rooted at the given path (or the
|
|
456
|
+
// client's current path). Returns [] if not a git repo.
|
|
457
|
+
let cwd = (params && typeof params.path === 'string' && params.path) || getClientPath(ws);
|
|
458
|
+
const pathOptions = {
|
|
459
|
+
allowedBasePath: server.allowedBasePath,
|
|
460
|
+
currentPath: getClientPath(ws)
|
|
461
|
+
};
|
|
462
|
+
const result = await withSensitivePathAuth(ws, cwd, 'list_worktrees', pathOptions, async () => {
|
|
463
|
+
const validation = validatePath(cwd, pathOptions);
|
|
464
|
+
if (!validation.valid) {
|
|
465
|
+
return {
|
|
466
|
+
success: false,
|
|
467
|
+
error: createError(ErrorCodes.PATH_ACCESS_DENIED, validation.error)
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
const worktrees = await collectWorktrees(validation.path);
|
|
471
|
+
return { success: true, payload: { worktrees } };
|
|
472
|
+
});
|
|
473
|
+
if (result.success) {
|
|
474
|
+
sendResponse(ws, id, result.payload);
|
|
475
|
+
} else {
|
|
476
|
+
sendErrorResponse(ws, id, result.error);
|
|
477
|
+
}
|
|
478
|
+
break;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// ── Structured JSON mode methods ──
|
|
482
|
+
|
|
483
|
+
case 'start_claude_structured': {
|
|
484
|
+
const cwd = params.path || getClientPath(ws);
|
|
485
|
+
const result = await withSensitivePathAuth(ws, cwd, 'start_claude_structured',
|
|
486
|
+
{ allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
487
|
+
async () => {
|
|
488
|
+
const validation = validatePath(cwd, {
|
|
489
|
+
allowedBasePath: server.allowedBasePath,
|
|
490
|
+
currentPath: getClientPath(ws)
|
|
491
|
+
});
|
|
492
|
+
if (!validation.valid) {
|
|
493
|
+
return { success: false, error: createError(ErrorCodes.PATH_ACCESS_DENIED, validation.error) };
|
|
494
|
+
}
|
|
495
|
+
return { success: true, path: validation.path };
|
|
496
|
+
}
|
|
497
|
+
);
|
|
498
|
+
if (!result.success) {
|
|
499
|
+
sendErrorResponse(ws, id, result.error);
|
|
500
|
+
break;
|
|
501
|
+
}
|
|
502
|
+
const resolvedPath = result.path;
|
|
503
|
+
const additionalArgs = [];
|
|
504
|
+
if (params.plan_mode === true || params.plan_mode === 'true') {
|
|
505
|
+
additionalArgs.push('--allowedTools', 'Read,Glob,Grep,LS');
|
|
506
|
+
}
|
|
507
|
+
try {
|
|
508
|
+
const result = claudeSessionManager.createSession(resolvedPath, ws, additionalArgs);
|
|
509
|
+
ws._structuredSessionId = result.sessionId;
|
|
510
|
+
setClientPath(ws, resolvedPath);
|
|
511
|
+
sendResponse(ws, id, {
|
|
512
|
+
sessionId: result.sessionId,
|
|
513
|
+
cwd: result.cwd,
|
|
514
|
+
mode: 'structured'
|
|
515
|
+
});
|
|
516
|
+
console.log(`🔧 Structured session started: ${result.sessionId} at ${resolvedPath}`);
|
|
517
|
+
} catch (err) {
|
|
518
|
+
sendErrorResponse(ws, id, {
|
|
519
|
+
type: 'error',
|
|
520
|
+
code: 'INTERNAL_ERROR',
|
|
521
|
+
error: `Failed to start structured session: ${err.message}`,
|
|
522
|
+
retryable: true
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
break;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
case 'send_structured': {
|
|
529
|
+
const sessionId = params.session_id;
|
|
530
|
+
if (!sessionId) {
|
|
531
|
+
sendErrorResponse(ws, id, {
|
|
532
|
+
type: 'error', code: 'INVALID_PARAMS',
|
|
533
|
+
error: 'session_id is required', retryable: false
|
|
534
|
+
});
|
|
535
|
+
break;
|
|
536
|
+
}
|
|
537
|
+
// Refuse to start a new user turn only when THIS structured
|
|
538
|
+
// session is waiting for approval. Other sessions must remain
|
|
539
|
+
// usable; otherwise one pending approval blocks the whole app.
|
|
540
|
+
if (claudeSessionManager.hasPendingApproval(sessionId)) {
|
|
541
|
+
console.warn('[send_structured] Blocked: a structured session has a pending tool approval');
|
|
542
|
+
sendErrorResponse(ws, id, {
|
|
543
|
+
type: 'error',
|
|
544
|
+
code: 'SEND_BLOCKED_BY_APPROVAL',
|
|
545
|
+
error: 'Cannot send a new message while a tool approval is pending',
|
|
546
|
+
retryable: true
|
|
547
|
+
});
|
|
548
|
+
break;
|
|
549
|
+
}
|
|
550
|
+
const sent = claudeSessionManager.sendMessage(sessionId, params.content);
|
|
551
|
+
if (sent) {
|
|
552
|
+
sendResponse(ws, id, { status: 'sent' });
|
|
553
|
+
} else {
|
|
554
|
+
sendErrorResponse(ws, id, {
|
|
555
|
+
type: 'error', code: 'SESSION_NOT_FOUND',
|
|
556
|
+
error: `Structured session ${sessionId} not found or process dead`,
|
|
557
|
+
retryable: false
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
break;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
case 'stop_structured': {
|
|
564
|
+
const sessionId = params.session_id;
|
|
565
|
+
if (!sessionId) {
|
|
566
|
+
sendErrorResponse(ws, id, {
|
|
567
|
+
type: 'error', code: 'INVALID_PARAMS',
|
|
568
|
+
error: 'session_id is required', retryable: false
|
|
569
|
+
});
|
|
570
|
+
break;
|
|
571
|
+
}
|
|
572
|
+
const sessionExists = claudeSessionManager.getSession(sessionId);
|
|
573
|
+
if (!sessionExists) {
|
|
574
|
+
sendErrorResponse(ws, id, {
|
|
575
|
+
type: 'error', code: 'SESSION_NOT_FOUND',
|
|
576
|
+
error: `Structured session ${sessionId} not found`,
|
|
577
|
+
retryable: false
|
|
578
|
+
});
|
|
579
|
+
break;
|
|
580
|
+
}
|
|
581
|
+
claudeSessionManager.stopSession(sessionId);
|
|
582
|
+
sendResponse(ws, id, { status: 'stopped' });
|
|
583
|
+
console.log(`🔧 Structured session stopped: ${sessionId}`);
|
|
584
|
+
break;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
case 'list_structured_sessions': {
|
|
588
|
+
const sessions = claudeSessionManager.listSessions();
|
|
589
|
+
sendResponse(ws, id, { sessions });
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
case 'attach_structured_session': {
|
|
594
|
+
const sessionId = params.session_id;
|
|
595
|
+
if (!sessionId) {
|
|
596
|
+
sendErrorResponse(ws, id, {
|
|
597
|
+
type: 'error', code: 'INVALID_PARAMS',
|
|
598
|
+
error: 'session_id is required', retryable: false
|
|
599
|
+
});
|
|
600
|
+
break;
|
|
601
|
+
}
|
|
602
|
+
const session = claudeSessionManager.getSession(sessionId);
|
|
603
|
+
if (!session) {
|
|
604
|
+
sendErrorResponse(ws, id, {
|
|
605
|
+
type: 'error', code: 'SESSION_NOT_FOUND',
|
|
606
|
+
error: `Structured session ${sessionId} not found`,
|
|
607
|
+
retryable: false
|
|
608
|
+
});
|
|
609
|
+
break;
|
|
610
|
+
}
|
|
611
|
+
claudeSessionManager.addSubscriber(sessionId, ws);
|
|
612
|
+
ws._structuredSessionId = sessionId;
|
|
613
|
+
sendResponse(ws, id, {
|
|
614
|
+
sessionId: session.sessionId,
|
|
615
|
+
cwd: session.cwd,
|
|
616
|
+
isRunning: session.isRunning,
|
|
617
|
+
mode: 'structured'
|
|
618
|
+
});
|
|
619
|
+
break;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
case 'respond_approval': {
|
|
623
|
+
const { session_id, request_id, behavior, updated_input, message } = params;
|
|
624
|
+
if (!session_id || !request_id || !behavior) {
|
|
625
|
+
sendErrorResponse(ws, id, {
|
|
626
|
+
type: 'error', code: 'INVALID_PARAMS',
|
|
627
|
+
error: 'session_id, request_id, and behavior are required', retryable: false
|
|
628
|
+
});
|
|
629
|
+
break;
|
|
630
|
+
}
|
|
631
|
+
if (behavior !== 'allow' && behavior !== 'deny') {
|
|
632
|
+
sendErrorResponse(ws, id, {
|
|
633
|
+
type: 'error', code: 'INVALID_PARAMS',
|
|
634
|
+
error: 'behavior must be "allow" or "deny"', retryable: false
|
|
635
|
+
});
|
|
636
|
+
break;
|
|
637
|
+
}
|
|
638
|
+
const sent = claudeSessionManager.respondApproval(
|
|
639
|
+
session_id, request_id, behavior, updated_input, message
|
|
640
|
+
);
|
|
641
|
+
if (sent) {
|
|
642
|
+
sendResponse(ws, id, { status: 'sent' });
|
|
643
|
+
} else {
|
|
644
|
+
sendErrorResponse(ws, id, {
|
|
645
|
+
type: 'error', code: 'SESSION_NOT_FOUND',
|
|
646
|
+
error: `Structured session ${session_id} not found or process dead`,
|
|
647
|
+
retryable: false
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
break;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
case 'steer_structured': {
|
|
654
|
+
const { session_id, content } = params;
|
|
655
|
+
if (!session_id || !content || typeof content !== 'string' || content.trim() === '') {
|
|
656
|
+
sendErrorResponse(ws, id, {
|
|
657
|
+
type: 'error', code: 'INVALID_PARAMS',
|
|
658
|
+
error: 'session_id and content (non-empty string) are required',
|
|
659
|
+
retryable: false
|
|
660
|
+
});
|
|
661
|
+
break;
|
|
662
|
+
}
|
|
663
|
+
const steerSession = claudeSessionManager.getSession(session_id);
|
|
664
|
+
if (!steerSession) {
|
|
665
|
+
sendErrorResponse(ws, id, {
|
|
666
|
+
type: 'error', code: 'SESSION_NOT_FOUND',
|
|
667
|
+
error: `Structured session ${session_id} not found`,
|
|
668
|
+
retryable: false
|
|
669
|
+
});
|
|
670
|
+
break;
|
|
671
|
+
}
|
|
672
|
+
if (claudeSessionManager.hasPendingApproval(session_id)) {
|
|
673
|
+
sendErrorResponse(ws, id, {
|
|
674
|
+
type: 'error', code: 'STEER_BLOCKED_BY_APPROVAL',
|
|
675
|
+
error: 'Cannot steer while a tool approval is pending',
|
|
676
|
+
retryable: false
|
|
677
|
+
});
|
|
678
|
+
break;
|
|
679
|
+
}
|
|
680
|
+
const steered = claudeSessionManager.steerMessage(session_id, content);
|
|
681
|
+
if (steered) {
|
|
682
|
+
sendResponse(ws, id, { status: 'steered' });
|
|
683
|
+
} else {
|
|
684
|
+
sendErrorResponse(ws, id, {
|
|
685
|
+
type: 'error', code: 'TRANSPORT_UNAVAILABLE',
|
|
686
|
+
error: 'Transport stdin is closed or unavailable',
|
|
687
|
+
retryable: true
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
break;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// ── Push notification methods ──
|
|
694
|
+
|
|
695
|
+
case 'register_push': {
|
|
696
|
+
const { deviceToken, alertsEnabled, apnsEnvironment } = params;
|
|
697
|
+
const normalizedAlertsEnabled = !(alertsEnabled === false || alertsEnabled === 'false');
|
|
698
|
+
const normalizedApnsEnvironment = typeof apnsEnvironment === 'string' && apnsEnvironment.trim() !== ''
|
|
699
|
+
? apnsEnvironment.trim()
|
|
700
|
+
: 'production';
|
|
701
|
+
if (!deviceToken || typeof deviceToken !== 'string' || deviceToken.trim() === '') {
|
|
702
|
+
sendErrorResponse(ws, id, {
|
|
703
|
+
type: 'error', code: 'INVALID_PARAMS',
|
|
704
|
+
error: 'deviceToken is required',
|
|
705
|
+
retryable: false
|
|
706
|
+
});
|
|
707
|
+
break;
|
|
708
|
+
}
|
|
709
|
+
if (!pushClient.isConfigured()) {
|
|
710
|
+
sendErrorResponse(ws, id, {
|
|
711
|
+
type: 'error', code: 'PUSH_NOT_CONFIGURED',
|
|
712
|
+
error: 'Push notifications are not configured on this server',
|
|
713
|
+
retryable: false
|
|
714
|
+
});
|
|
715
|
+
break;
|
|
716
|
+
}
|
|
717
|
+
const identity = server.serverIdentity;
|
|
718
|
+
if (!identity || !identity.serverId || !identity.serverSecret) {
|
|
719
|
+
sendErrorResponse(ws, id, {
|
|
720
|
+
type: 'error', code: 'INTERNAL_ERROR',
|
|
721
|
+
error: 'Server identity not available',
|
|
722
|
+
retryable: true
|
|
723
|
+
});
|
|
724
|
+
break;
|
|
725
|
+
}
|
|
726
|
+
try {
|
|
727
|
+
const result = await pushClient.register(
|
|
728
|
+
identity.serverId,
|
|
729
|
+
identity.serverSecret,
|
|
730
|
+
deviceToken.trim(),
|
|
731
|
+
normalizedAlertsEnabled,
|
|
732
|
+
normalizedApnsEnvironment
|
|
733
|
+
);
|
|
734
|
+
if (result.ok) {
|
|
735
|
+
sendResponse(ws, id, { status: 'registered', serverId: identity.serverId });
|
|
736
|
+
} else {
|
|
737
|
+
sendErrorResponse(ws, id, {
|
|
738
|
+
type: 'error', code: 'PUSH_REGISTER_FAILED',
|
|
739
|
+
error: result.error || 'Failed to register with push service',
|
|
740
|
+
retryable: true
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
} catch (err) {
|
|
744
|
+
console.error(`[push] register_push error: ${err.message}`);
|
|
745
|
+
sendErrorResponse(ws, id, {
|
|
746
|
+
type: 'error', code: 'INTERNAL_ERROR',
|
|
747
|
+
error: 'Failed to register with push service',
|
|
748
|
+
retryable: true
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
break;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
case 'unregister_push': {
|
|
755
|
+
const { deviceToken: unregToken } = params;
|
|
756
|
+
if (!unregToken || typeof unregToken !== 'string' || unregToken.trim() === '') {
|
|
757
|
+
sendErrorResponse(ws, id, {
|
|
758
|
+
type: 'error', code: 'INVALID_PARAMS',
|
|
759
|
+
error: 'deviceToken is required',
|
|
760
|
+
retryable: false
|
|
761
|
+
});
|
|
762
|
+
break;
|
|
763
|
+
}
|
|
764
|
+
if (!pushClient.isConfigured()) {
|
|
765
|
+
sendResponse(ws, id, { status: 'ok' });
|
|
766
|
+
break;
|
|
767
|
+
}
|
|
768
|
+
const unregIdentity = server.serverIdentity;
|
|
769
|
+
if (unregIdentity && unregIdentity.serverId && unregIdentity.serverSecret) {
|
|
770
|
+
pushClient.unregister(
|
|
771
|
+
unregIdentity.serverId,
|
|
772
|
+
unregIdentity.serverSecret,
|
|
773
|
+
unregToken.trim()
|
|
774
|
+
).catch((err) => {
|
|
775
|
+
console.error(`[push] unregister error: ${err.message}`);
|
|
776
|
+
});
|
|
777
|
+
}
|
|
778
|
+
sendResponse(ws, id, { status: 'ok' });
|
|
779
|
+
break;
|
|
780
|
+
}
|
|
781
|
+
|
|
746
782
|
default:
|
|
747
783
|
sendErrorResponse(ws, id, {
|
|
748
784
|
type: 'error',
|
|
@@ -769,7 +805,13 @@ function createWsHandler(server) {
|
|
|
769
805
|
try {
|
|
770
806
|
switch (message.type) {
|
|
771
807
|
case 'send_to_claude': {
|
|
772
|
-
|
|
808
|
+
// Safety guard: reject terminal steer when a structured session has a pending approval
|
|
809
|
+
if (claudeSessionManager.hasAnyPendingApproval()) {
|
|
810
|
+
console.warn('[send_to_claude] Blocked: a structured session has a pending tool approval — refusing terminal send to prevent accidental y/n');
|
|
811
|
+
sendToClient(ws, { type: 'error', error: 'Cannot send to terminal while a tool approval is pending in a structured session' });
|
|
812
|
+
break;
|
|
813
|
+
}
|
|
814
|
+
const result = await tmux.sendToClaude(message.content, message.session, getClientPath(ws), message.cols, message.rows, message.command);
|
|
773
815
|
if (result.success) {
|
|
774
816
|
sendToClient(ws, { type: 'status', status: 'sent' });
|
|
775
817
|
startOutputMonitor(ws, message.session || tmux.DEFAULT_SESSION);
|
|
@@ -839,6 +881,7 @@ function createWsHandler(server) {
|
|
|
839
881
|
|
|
840
882
|
const result = await tmux.attachSession(message.session);
|
|
841
883
|
if (result.success) {
|
|
884
|
+
updateClientState(ws, { currentSession: normalizeClientSession(message.session) });
|
|
842
885
|
if (result.path) setClientPath(ws, result.path);
|
|
843
886
|
sendToClient(ws, { type: 'path_changed', path: getClientPath(ws) });
|
|
844
887
|
console.log(`📎 Attached to session: ${message.session} at ${getClientPath(ws)}`);
|
|
@@ -865,6 +908,16 @@ function createWsHandler(server) {
|
|
|
865
908
|
break;
|
|
866
909
|
}
|
|
867
910
|
|
|
911
|
+
case 'client_state': {
|
|
912
|
+
updateClientState(ws, {
|
|
913
|
+
foreground: message.foreground === true,
|
|
914
|
+
currentSession: normalizeClientSession(message.currentSession),
|
|
915
|
+
supportsClientState: true
|
|
916
|
+
});
|
|
917
|
+
sendToClient(ws, { type: 'status', status: 'ok' });
|
|
918
|
+
break;
|
|
919
|
+
}
|
|
920
|
+
|
|
868
921
|
case 'create_session': {
|
|
869
922
|
const result = await withSensitivePathAuth(ws, message.path || getClientPath(ws), 'create_session',
|
|
870
923
|
{ allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
@@ -1033,7 +1086,8 @@ function createWsHandler(server) {
|
|
|
1033
1086
|
captureAndBroadcast,
|
|
1034
1087
|
getClientPath,
|
|
1035
1088
|
setClientPath,
|
|
1036
|
-
outputMonitors
|
|
1089
|
+
outputMonitors,
|
|
1090
|
+
claudeSessionManager
|
|
1037
1091
|
};
|
|
1038
1092
|
}
|
|
1039
1093
|
|