tacticremote 1.7.0 → 1.8.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/LICENSE +28 -0
- package/README.md +33 -41
- package/bin/tacticremote.js +41 -101
- package/package.json +30 -42
- package/bin/tactic-remote-server.js +0 -636
- package/lib/authChallengeManager.js +0 -1
- package/lib/bonjourPublisher.js +0 -68
- package/lib/claudeSessionManager.js +0 -1
- package/lib/claudeTransport.js +0 -1
- package/lib/errorCodes.js +0 -1
- package/lib/execSafe.js +0 -1
- package/lib/fileService.js +0 -1
- package/lib/hookInstaller.js +0 -253
- package/lib/outputParser.js +0 -1
- package/lib/pushClient.js +0 -1
- package/lib/rateLimiter.js +0 -1
- package/lib/serverIdentity.js +0 -1
- package/lib/streamJsonParser.js +0 -1
- package/lib/tmuxManager.js +0 -1
- package/lib/uploadService.js +0 -1
- package/lib/validators.js +0 -1
- package/middleware/authMiddleware.js +0 -152
- package/routes/httpHandler.js +0 -111
- package/routes/wsHandler.js +0 -1094
- package/server.js +0 -236
- package/shared/server-core/authChallengeManager.js +0 -108
- package/shared/server-core/claudeSessionManager.js +0 -222
- package/shared/server-core/claudeTransport.js +0 -176
- package/shared/server-core/errorCodes.js +0 -91
- package/shared/server-core/execSafe.js +0 -185
- package/shared/server-core/fileService.js +0 -155
- package/shared/server-core/outputParser.js +0 -174
- package/shared/server-core/protocol-manifest.json +0 -59
- package/shared/server-core/protocol-manifest.schema.json +0 -90
- package/shared/server-core/protocolManifest.js +0 -42
- package/shared/server-core/pushClient.js +0 -198
- package/shared/server-core/rateLimiter.js +0 -98
- package/shared/server-core/serverConnectionLifecycle.js +0 -118
- package/shared/server-core/serverHandshake.js +0 -72
- package/shared/server-core/serverIdentity.js +0 -102
- package/shared/server-core/streamJsonParser.js +0 -321
- package/shared/server-core/tmuxManager.js +0 -596
- package/shared/server-core/unixProcessLifecycle.js +0 -96
- package/shared/server-core/unixServerHooks.js +0 -148
- package/shared/server-core/unixWsRuntime.js +0 -412
- package/shared/server-core/uploadService.js +0 -321
- package/shared/server-core/validators.js +0 -274
- package/shared/server-core/windowsServerHooks.js +0 -174
- package/shared/server-core/windowsWsRuntime.js +0 -413
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
// Warn-once-per-process flags to avoid log spam when hooks are stale.
|
|
2
|
-
// Cleared only when the server process restarts.
|
|
3
|
-
const warnedOnce = new Set();
|
|
4
|
-
function warnOnce(key, message) {
|
|
5
|
-
if (warnedOnce.has(key)) return;
|
|
6
|
-
warnedOnce.add(key);
|
|
7
|
-
console.warn(message);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function getForegroundMatchingClients(server, sessionName) {
|
|
11
|
-
if (!sessionName) return [];
|
|
12
|
-
const matches = [];
|
|
13
|
-
for (const [clientId] of server.externalClients) {
|
|
14
|
-
const state = server.clientState.get(clientId);
|
|
15
|
-
if (!state || state.foreground !== true) continue;
|
|
16
|
-
if (state.currentSession === sessionName) {
|
|
17
|
-
matches.push(clientId);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return matches;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function getLegacyExternalClients(server) {
|
|
24
|
-
const legacyClients = [];
|
|
25
|
-
for (const [clientId] of server.externalClients) {
|
|
26
|
-
const state = server.clientState.get(clientId);
|
|
27
|
-
if (!state || state.supportsClientState !== true) {
|
|
28
|
-
legacyClients.push(clientId);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return legacyClients;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function handleWindowsHookEvent(server, hookData, { extractToolDetail }) {
|
|
35
|
-
const projectName = hookData.project || 'unknown project';
|
|
36
|
-
|
|
37
|
-
if (hookData.tactic_session) {
|
|
38
|
-
hookData.tmux_session = hookData.tactic_session;
|
|
39
|
-
} else if (hookData.tmux_session) {
|
|
40
|
-
warnOnce('legacy-tmux-session', `[notify][hook] legacy tmux_session field — hooks need reinstall`);
|
|
41
|
-
} else {
|
|
42
|
-
warnOnce('no-session-identity', `[notify][hook] no session identity in hook payload — hooks may be stale or claude launched outside TacticRemote`);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
console.log(`[notify][hook] event=${hookData.event} project=${projectName} session=${hookData.tmux_session || '-'} externalClients=${server.externalClients.size} pushEnabled=${server.pushEnabled}`);
|
|
46
|
-
server.lastHookEvent = hookData;
|
|
47
|
-
|
|
48
|
-
server.wsHandler.broadcast({
|
|
49
|
-
type: 'hook_event',
|
|
50
|
-
event: hookData.event,
|
|
51
|
-
project: hookData.project,
|
|
52
|
-
session: hookData.tmux_session,
|
|
53
|
-
timestamp: hookData.timestamp
|
|
54
|
-
});
|
|
55
|
-
server.wsHandler.broadcastEvent('hook_event', {
|
|
56
|
-
event: hookData.event,
|
|
57
|
-
project: hookData.project,
|
|
58
|
-
session: hookData.tmux_session,
|
|
59
|
-
timestamp: hookData.timestamp
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
if (hookData.event === 'Stop') {
|
|
63
|
-
console.log(`✅ Claude task completed in ${hookData.project}`);
|
|
64
|
-
if (hookData.tmux_session) {
|
|
65
|
-
server.wsHandler.captureAndBroadcast(hookData.tmux_session, 'completed');
|
|
66
|
-
}
|
|
67
|
-
console.log(`[notify][ws] event=completed project=${projectName} source=hook_stop`);
|
|
68
|
-
server.wsHandler.broadcast({
|
|
69
|
-
type: 'notification',
|
|
70
|
-
title: 'Claude Task Completed',
|
|
71
|
-
body: `Project: ${hookData.project}`,
|
|
72
|
-
project: hookData.project,
|
|
73
|
-
session: hookData.tmux_session,
|
|
74
|
-
event: 'completed',
|
|
75
|
-
timestamp: hookData.timestamp
|
|
76
|
-
});
|
|
77
|
-
server.wsHandler.broadcastEvent('notification', {
|
|
78
|
-
title: 'Claude Task Completed',
|
|
79
|
-
body: `Project: ${hookData.project}`,
|
|
80
|
-
project: hookData.project,
|
|
81
|
-
session: hookData.tmux_session,
|
|
82
|
-
event: 'completed',
|
|
83
|
-
timestamp: hookData.timestamp
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const hookEventName = hookData.hook_event_name || hookData.event;
|
|
88
|
-
if (hookEventName === 'PreToolUse' || hookEventName === 'PostToolUse') {
|
|
89
|
-
const toolEvent = {
|
|
90
|
-
event: hookEventName,
|
|
91
|
-
tool_name: hookData.tool_name,
|
|
92
|
-
tool_use_id: hookData.tool_use_id,
|
|
93
|
-
tool_detail: extractToolDetail(hookData.tool_name, hookData.tool_input),
|
|
94
|
-
project: hookData.project,
|
|
95
|
-
session: hookData.tmux_session,
|
|
96
|
-
timestamp: hookData.timestamp || Date.now()
|
|
97
|
-
};
|
|
98
|
-
server.wsHandler.broadcastEvent('tool_use', toolEvent);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (hookData.event === 'Notification' && hookData.data?.message) {
|
|
102
|
-
console.log(`[notify][ws] event=notification project=${projectName} body="${hookData.data.message}"`);
|
|
103
|
-
server.wsHandler.broadcast({
|
|
104
|
-
type: 'notification',
|
|
105
|
-
title: 'Claude',
|
|
106
|
-
body: hookData.data.message,
|
|
107
|
-
project: hookData.project,
|
|
108
|
-
session: hookData.tmux_session,
|
|
109
|
-
event: 'notification',
|
|
110
|
-
timestamp: hookData.timestamp
|
|
111
|
-
});
|
|
112
|
-
server.wsHandler.broadcastEvent('notification', {
|
|
113
|
-
title: 'Claude',
|
|
114
|
-
body: hookData.data.message,
|
|
115
|
-
project: hookData.project,
|
|
116
|
-
session: hookData.tmux_session,
|
|
117
|
-
event: 'notification',
|
|
118
|
-
timestamp: hookData.timestamp
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const pushEvents = ['Stop', 'Notification', 'Error'];
|
|
123
|
-
const legacyExternalClients = getLegacyExternalClients(server);
|
|
124
|
-
const matchingForegroundClients = legacyExternalClients.length === 0
|
|
125
|
-
? getForegroundMatchingClients(server, hookData.tmux_session)
|
|
126
|
-
: [];
|
|
127
|
-
if (pushEvents.includes(hookData.event) && !server.pushEnabled) {
|
|
128
|
-
console.log(`[notify][push] skipped event=${hookData.event} project=${projectName} reason=push_disabled`);
|
|
129
|
-
} else if (pushEvents.includes(hookData.event) && legacyExternalClients.length !== 0) {
|
|
130
|
-
console.log(`[notify][push] skipped event=${hookData.event} project=${projectName} reason=legacy_client_without_client_state clients=${legacyExternalClients.join(',')}`);
|
|
131
|
-
} else if (pushEvents.includes(hookData.event) && matchingForegroundClients.length !== 0) {
|
|
132
|
-
console.log(`[notify][push] skipped event=${hookData.event} project=${projectName} session=${hookData.tmux_session || '-'} reason=foreground_client_viewing_session clients=${matchingForegroundClients.join(',')}`);
|
|
133
|
-
} else if (pushEvents.includes(hookData.event)) {
|
|
134
|
-
const { serverId, serverSecret } = server.serverIdentity || {};
|
|
135
|
-
if (serverId && serverSecret) {
|
|
136
|
-
const projectLabel = hookData.project || 'unknown project';
|
|
137
|
-
let title = `${projectLabel} — Claude`;
|
|
138
|
-
let body = `Task ${hookData.event.toLowerCase()} in ${projectLabel}`;
|
|
139
|
-
if (hookData.event === 'Stop') {
|
|
140
|
-
title = `${projectLabel} — Task Completed`;
|
|
141
|
-
body = 'Claude finished';
|
|
142
|
-
} else if (hookData.event === 'Notification' && hookData.data?.message) {
|
|
143
|
-
title = `${projectLabel} — Claude`;
|
|
144
|
-
body = hookData.data.message;
|
|
145
|
-
} else if (hookData.event === 'Error') {
|
|
146
|
-
title = `${projectLabel} — Error`;
|
|
147
|
-
body = 'An error occurred';
|
|
148
|
-
}
|
|
149
|
-
console.log(`[notify][push] dispatch event=${hookData.event} project=${projectName} title="${title}" body="${body}"`);
|
|
150
|
-
const pushClient = require('./pushClient');
|
|
151
|
-
pushClient.notifyCompletion(serverId, serverSecret, title, body, hookData.project || '', hookData.event, hookData.tmux_session || '')
|
|
152
|
-
.then((result) => {
|
|
153
|
-
const sent = result?.data?.sent ?? result?.sent ?? 'n/a';
|
|
154
|
-
if (result && result.ok) {
|
|
155
|
-
console.log(`[notify][push] dispatched event=${hookData.event} project=${projectName} sent=${sent} status=${result.status || '-'}`);
|
|
156
|
-
} else {
|
|
157
|
-
console.warn(`[notify][push] rejected event=${hookData.event} project=${projectName} sent=${sent} error=${result?.data?.error || result?.error || 'unknown'} status=${result?.status || '-'}`);
|
|
158
|
-
}
|
|
159
|
-
})
|
|
160
|
-
.catch((err) => {
|
|
161
|
-
console.error(`[push] notifyCompletion error: ${err.message}`);
|
|
162
|
-
});
|
|
163
|
-
} else {
|
|
164
|
-
console.warn(`[notify][push] skipped event=${hookData.event} project=${projectName} reason=missing_server_identity`);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
module.exports = {
|
|
170
|
-
getForegroundMatchingClients,
|
|
171
|
-
getLegacyExternalClients,
|
|
172
|
-
handleWindowsHookEvent,
|
|
173
|
-
_resetWarnedForTests: () => warnedOnce.clear()
|
|
174
|
-
};
|
|
@@ -1,413 +0,0 @@
|
|
|
1
|
-
const { stabilitySignature } = require('./outputParser');
|
|
2
|
-
|
|
3
|
-
function createWindowsWsRuntime({
|
|
4
|
-
server,
|
|
5
|
-
ptyMgr,
|
|
6
|
-
authChallengeManager,
|
|
7
|
-
parseClaudeOutput,
|
|
8
|
-
validatePath,
|
|
9
|
-
getStructuredSessionManager
|
|
10
|
-
}) {
|
|
11
|
-
const outputMonitors = new Map();
|
|
12
|
-
const clientMonitors = new Map();
|
|
13
|
-
|
|
14
|
-
function sendToClient(ws, message) {
|
|
15
|
-
if (ws.readyState === 1) {
|
|
16
|
-
ws.send(JSON.stringify(message));
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function sendOutputToClient(ws, message) {
|
|
21
|
-
if (ws.readyState === 1 && ws.bufferedAmount < 256 * 1024) {
|
|
22
|
-
ws.send(JSON.stringify(message));
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function sendResponse(ws, requestId, payload) {
|
|
27
|
-
sendToClient(ws, { type: 'res', id: requestId, ok: true, payload });
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function sendErrorResponse(ws, requestId, error) {
|
|
31
|
-
sendToClient(ws, { type: 'res', id: requestId, ok: false, error });
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function sendEvent(ws, eventName, payload) {
|
|
35
|
-
sendToClient(ws, { type: 'event', event: eventName, payload });
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function broadcastEvent(eventName, payload) {
|
|
39
|
-
server.clients.forEach((ws) => sendEvent(ws, eventName, payload));
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function broadcast(message) {
|
|
43
|
-
server.clients.forEach((ws) => sendToClient(ws, message));
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async function withSensitivePathAuth(ws, userPath, method, validateOptions, operation) {
|
|
47
|
-
const validation = validatePath(userPath, validateOptions);
|
|
48
|
-
|
|
49
|
-
if (!validation.valid && validation.requiresAuth) {
|
|
50
|
-
const sensitiveDir = validation.sensitiveDir;
|
|
51
|
-
|
|
52
|
-
if (authChallengeManager.isGranted(ws, sensitiveDir)) {
|
|
53
|
-
return await operation();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const { challengeId, expiresAt, promise } = authChallengeManager.createChallenge(
|
|
57
|
-
ws, userPath, sensitiveDir, method
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
sendEvent(ws, 'auth_challenge', {
|
|
61
|
-
challengeId, path: userPath, sensitiveDir, method, expiresAt
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
const granted = await promise;
|
|
65
|
-
if (granted) {
|
|
66
|
-
return await operation();
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return {
|
|
70
|
-
success: false,
|
|
71
|
-
error: {
|
|
72
|
-
type: 'error', code: 'AUTH_DENIED',
|
|
73
|
-
error: `Access to ${sensitiveDir} was denied`,
|
|
74
|
-
retryable: false
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return await operation();
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function getClientPath(ws) {
|
|
83
|
-
const state = server.clientState.get(ws._clientId);
|
|
84
|
-
return state ? state.currentPath : server.currentPath;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function updateClientState(ws, patch) {
|
|
88
|
-
const state = server.clientState.get(ws._clientId);
|
|
89
|
-
if (!state) return null;
|
|
90
|
-
Object.assign(state, patch);
|
|
91
|
-
return state;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function setClientPath(ws, path) {
|
|
95
|
-
updateClientState(ws, { currentPath: path });
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function normalizeClientSession(session) {
|
|
99
|
-
return typeof session === 'string' && session.trim() !== ''
|
|
100
|
-
? session.trim()
|
|
101
|
-
: null;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function decorateSessions(sessions = []) {
|
|
105
|
-
return sessions.map((session) => ({
|
|
106
|
-
...session,
|
|
107
|
-
managed: session.managed === true || server.managedSessions?.has(session.name) === true,
|
|
108
|
-
running: session.running === true
|
|
109
|
-
}));
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function rememberManagedSession(sessionName) {
|
|
113
|
-
if (!sessionName) return;
|
|
114
|
-
if (!server.managedSessions) {
|
|
115
|
-
server.managedSessions = new Set();
|
|
116
|
-
}
|
|
117
|
-
server.managedSessions.add(sessionName);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function forgetManagedSession(sessionName) {
|
|
121
|
-
if (!sessionName || !server.managedSessions) return;
|
|
122
|
-
server.managedSessions.delete(sessionName);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
async function collectGitInfo(cwd) {
|
|
126
|
-
const { spawnSync } = require('child_process');
|
|
127
|
-
|
|
128
|
-
function runGit(args, timeout) {
|
|
129
|
-
const result = spawnSync('git', ['-C', cwd, ...args], {
|
|
130
|
-
encoding: 'utf8',
|
|
131
|
-
timeout
|
|
132
|
-
});
|
|
133
|
-
if (result.status !== 0) {
|
|
134
|
-
throw new Error((result.stderr || result.stdout || 'git command failed').trim());
|
|
135
|
-
}
|
|
136
|
-
return result.stdout.trim();
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
try {
|
|
140
|
-
const branch = runGit(['branch', '--show-current'], 5000);
|
|
141
|
-
const gitStatus = runGit(['status', '--short'], 5000);
|
|
142
|
-
const diffStat = runGit(['diff', '--stat', 'HEAD'], 10000);
|
|
143
|
-
const branchList = runGit(['branch', '--format=%(refname:short)'], 5000);
|
|
144
|
-
let worktreePath = '';
|
|
145
|
-
try { worktreePath = runGit(['rev-parse', '--show-toplevel'], 5000); } catch { /* ignore */ }
|
|
146
|
-
|
|
147
|
-
return {
|
|
148
|
-
cwd,
|
|
149
|
-
branch,
|
|
150
|
-
branches: branchList.split('\n').filter(Boolean),
|
|
151
|
-
git_status: gitStatus,
|
|
152
|
-
diff_stat: diffStat,
|
|
153
|
-
is_git_repo: true,
|
|
154
|
-
worktree_path: worktreePath || undefined,
|
|
155
|
-
is_worktree: Boolean(worktreePath) && worktreePath !== cwd
|
|
156
|
-
};
|
|
157
|
-
} catch {
|
|
158
|
-
return { cwd, is_git_repo: false };
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
async function collectWorktrees(cwd) {
|
|
163
|
-
const { spawnSync } = require('child_process');
|
|
164
|
-
try {
|
|
165
|
-
const result = spawnSync('git', ['-C', cwd, 'worktree', 'list', '--porcelain'], {
|
|
166
|
-
encoding: 'utf8',
|
|
167
|
-
timeout: 5000
|
|
168
|
-
});
|
|
169
|
-
if (result.status !== 0) return [];
|
|
170
|
-
const worktrees = [];
|
|
171
|
-
let current = null;
|
|
172
|
-
for (const line of result.stdout.split('\n')) {
|
|
173
|
-
if (line.startsWith('worktree ')) {
|
|
174
|
-
if (current) worktrees.push(current);
|
|
175
|
-
current = {
|
|
176
|
-
path: line.slice('worktree '.length).trim(),
|
|
177
|
-
branch: null,
|
|
178
|
-
is_main: worktrees.length === 0,
|
|
179
|
-
is_detached: false,
|
|
180
|
-
is_locked: false
|
|
181
|
-
};
|
|
182
|
-
} else if (!current) {
|
|
183
|
-
continue;
|
|
184
|
-
} else if (line.startsWith('branch refs/heads/')) {
|
|
185
|
-
current.branch = line.slice('branch refs/heads/'.length).trim();
|
|
186
|
-
} else if (line.trim() === 'detached') {
|
|
187
|
-
current.is_detached = true;
|
|
188
|
-
} else if (line.trim() === 'locked' || line.startsWith('locked ')) {
|
|
189
|
-
current.is_locked = true;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
if (current) worktrees.push(current);
|
|
193
|
-
return worktrees;
|
|
194
|
-
} catch {
|
|
195
|
-
return [];
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function startOutputMonitor(ws, sessionName) {
|
|
200
|
-
if (!clientMonitors.has(ws)) clientMonitors.set(ws, new Set());
|
|
201
|
-
clientMonitors.get(ws).add(sessionName);
|
|
202
|
-
|
|
203
|
-
if (outputMonitors.has(sessionName)) {
|
|
204
|
-
const existing = outputMonitors.get(sessionName);
|
|
205
|
-
existing.subscribers.set(ws, true);
|
|
206
|
-
existing.checkCount = 0;
|
|
207
|
-
existing.stableCount = 0;
|
|
208
|
-
existing.lastOutput = '';
|
|
209
|
-
existing.lastSignature = '';
|
|
210
|
-
return;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
const maxChecks = 120;
|
|
214
|
-
const subscribers = new Map();
|
|
215
|
-
subscribers.set(ws, true);
|
|
216
|
-
|
|
217
|
-
const monitorState = {
|
|
218
|
-
subscribers,
|
|
219
|
-
intervalId: null,
|
|
220
|
-
checkCount: 0,
|
|
221
|
-
stableCount: 0,
|
|
222
|
-
lastOutput: '',
|
|
223
|
-
lastSignature: ''
|
|
224
|
-
};
|
|
225
|
-
|
|
226
|
-
function emitFinalAndTeardown(reason) {
|
|
227
|
-
try {
|
|
228
|
-
if (reason === 'stable') {
|
|
229
|
-
const parsed = parseClaudeOutput(monitorState.lastOutput || '');
|
|
230
|
-
const isWaiting = ptyMgr.isWaitingForInput(monitorState.lastOutput || '');
|
|
231
|
-
for (const [subWs] of subscribers) {
|
|
232
|
-
if (subWs.readyState !== 1) continue;
|
|
233
|
-
if (isWaiting) {
|
|
234
|
-
sendToClient(subWs, { type: 'status', status: 'waiting' });
|
|
235
|
-
}
|
|
236
|
-
sendToClient(subWs, {
|
|
237
|
-
type: 'claude_output',
|
|
238
|
-
content: parsed.display,
|
|
239
|
-
userQuestion: parsed.userQuestion,
|
|
240
|
-
claudeResponse: parsed.claudeResponse,
|
|
241
|
-
session: sessionName,
|
|
242
|
-
final: true,
|
|
243
|
-
finalReason: reason,
|
|
244
|
-
waiting: isWaiting
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
} finally {
|
|
249
|
-
if (monitorState.intervalId) clearInterval(monitorState.intervalId);
|
|
250
|
-
outputMonitors.delete(sessionName);
|
|
251
|
-
for (const [subWs] of subscribers) {
|
|
252
|
-
const sessions = clientMonitors.get(subWs);
|
|
253
|
-
if (sessions) sessions.delete(sessionName);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
const monitor = setInterval(async () => {
|
|
259
|
-
monitorState.checkCount++;
|
|
260
|
-
|
|
261
|
-
for (const [subWs] of subscribers) {
|
|
262
|
-
if (subWs.readyState !== 1) {
|
|
263
|
-
subscribers.delete(subWs);
|
|
264
|
-
const sessions = clientMonitors.get(subWs);
|
|
265
|
-
if (sessions) sessions.delete(sessionName);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
if (subscribers.size === 0) {
|
|
270
|
-
emitFinalAndTeardown('no_subscribers');
|
|
271
|
-
return;
|
|
272
|
-
}
|
|
273
|
-
if (monitorState.checkCount > maxChecks) {
|
|
274
|
-
emitFinalAndTeardown('max_checks');
|
|
275
|
-
return;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
try {
|
|
279
|
-
const output = ptyMgr.getRawBuffer(sessionName, 2000);
|
|
280
|
-
|
|
281
|
-
// Push display on any raw change so the client sees live
|
|
282
|
-
// output (logo animation, spinner tick, token streaming).
|
|
283
|
-
if (output !== monitorState.lastOutput) {
|
|
284
|
-
monitorState.lastOutput = output;
|
|
285
|
-
const parsed = parseClaudeOutput(output);
|
|
286
|
-
for (const [subWs] of subscribers) {
|
|
287
|
-
sendOutputToClient(subWs, {
|
|
288
|
-
type: 'claude_output',
|
|
289
|
-
content: parsed.display,
|
|
290
|
-
session: sessionName
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
// Stability is measured on a normalized signature of the
|
|
296
|
-
// input-box region only — see shared/server-core/unixWsRuntime.js
|
|
297
|
-
// for the rationale.
|
|
298
|
-
const signature = stabilitySignature(output);
|
|
299
|
-
if (signature !== monitorState.lastSignature) {
|
|
300
|
-
monitorState.lastSignature = signature;
|
|
301
|
-
monitorState.stableCount = 0;
|
|
302
|
-
monitorState.checkCount = 0;
|
|
303
|
-
} else {
|
|
304
|
-
monitorState.stableCount++;
|
|
305
|
-
if (monitorState.stableCount >= 5) {
|
|
306
|
-
emitFinalAndTeardown('stable');
|
|
307
|
-
return;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
} catch (error) {
|
|
311
|
-
console.error('Monitor error:', error.message);
|
|
312
|
-
}
|
|
313
|
-
}, 1000);
|
|
314
|
-
|
|
315
|
-
monitorState.intervalId = monitor;
|
|
316
|
-
outputMonitors.set(sessionName, monitorState);
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
function stopMonitor(sessionName) {
|
|
320
|
-
if (outputMonitors.has(sessionName)) {
|
|
321
|
-
const entry = outputMonitors.get(sessionName);
|
|
322
|
-
clearInterval(entry.intervalId);
|
|
323
|
-
for (const [subWs] of entry.subscribers) {
|
|
324
|
-
const sessions = clientMonitors.get(subWs);
|
|
325
|
-
if (sessions) sessions.delete(sessionName);
|
|
326
|
-
}
|
|
327
|
-
outputMonitors.delete(sessionName);
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
function removeClientFromMonitor(ws, sessionName) {
|
|
332
|
-
if (outputMonitors.has(sessionName)) {
|
|
333
|
-
const entry = outputMonitors.get(sessionName);
|
|
334
|
-
entry.subscribers.delete(ws);
|
|
335
|
-
const sessions = clientMonitors.get(ws);
|
|
336
|
-
if (sessions) sessions.delete(sessionName);
|
|
337
|
-
if (entry.subscribers.size === 0) {
|
|
338
|
-
clearInterval(entry.intervalId);
|
|
339
|
-
outputMonitors.delete(sessionName);
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
function cleanupClient(ws) {
|
|
345
|
-
const sessions = clientMonitors.get(ws);
|
|
346
|
-
if (sessions) {
|
|
347
|
-
for (const sessionName of sessions) {
|
|
348
|
-
if (outputMonitors.has(sessionName)) {
|
|
349
|
-
const entry = outputMonitors.get(sessionName);
|
|
350
|
-
entry.subscribers.delete(ws);
|
|
351
|
-
if (entry.subscribers.size === 0) {
|
|
352
|
-
clearInterval(entry.intervalId);
|
|
353
|
-
outputMonitors.delete(sessionName);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
clientMonitors.delete(ws);
|
|
358
|
-
}
|
|
359
|
-
authChallengeManager.cleanupClient(ws);
|
|
360
|
-
getStructuredSessionManager?.()?.removeSubscriberFromAll(ws);
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
async function captureAndBroadcast(sessionName, reason = 'update', originWs = null) {
|
|
364
|
-
const result = await ptyMgr.captureOutput(sessionName, 2000);
|
|
365
|
-
if (result.success) {
|
|
366
|
-
const message = {
|
|
367
|
-
type: 'claude_output',
|
|
368
|
-
content: result.content,
|
|
369
|
-
userQuestion: result.userQuestion,
|
|
370
|
-
claudeResponse: result.claudeResponse,
|
|
371
|
-
session: sessionName,
|
|
372
|
-
reason
|
|
373
|
-
};
|
|
374
|
-
const monitor = outputMonitors.get(sessionName);
|
|
375
|
-
if (monitor) {
|
|
376
|
-
for (const [subWs] of monitor.subscribers) {
|
|
377
|
-
sendOutputToClient(subWs, message);
|
|
378
|
-
}
|
|
379
|
-
} else if (originWs) {
|
|
380
|
-
sendOutputToClient(originWs, message);
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
return {
|
|
386
|
-
outputMonitors,
|
|
387
|
-
clientMonitors,
|
|
388
|
-
sendToClient,
|
|
389
|
-
sendOutputToClient,
|
|
390
|
-
sendResponse,
|
|
391
|
-
sendErrorResponse,
|
|
392
|
-
sendEvent,
|
|
393
|
-
broadcastEvent,
|
|
394
|
-
broadcast,
|
|
395
|
-
withSensitivePathAuth,
|
|
396
|
-
getClientPath,
|
|
397
|
-
updateClientState,
|
|
398
|
-
setClientPath,
|
|
399
|
-
normalizeClientSession,
|
|
400
|
-
decorateSessions,
|
|
401
|
-
rememberManagedSession,
|
|
402
|
-
forgetManagedSession,
|
|
403
|
-
collectGitInfo,
|
|
404
|
-
collectWorktrees,
|
|
405
|
-
startOutputMonitor,
|
|
406
|
-
stopMonitor,
|
|
407
|
-
removeClientFromMonitor,
|
|
408
|
-
cleanupClient,
|
|
409
|
-
captureAndBroadcast
|
|
410
|
-
};
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
module.exports = { createWindowsWsRuntime };
|