jg-agent-hq 4.0.2 → 5.0.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/CHANGELOG.md +13 -0
- package/README.md +4 -4
- package/api/README.md +20 -12
- package/dist/cli/cli.js +23 -2
- package/dist/contracts/agent-tools/types.d.ts +2 -0
- package/dist/contracts/browser/types.d.ts +11 -0
- package/dist/contracts/constants.d.ts +1 -0
- package/dist/contracts/constants.js +2 -0
- package/dist/contracts/evidence/types.d.ts +4 -0
- package/dist/contracts/evidence/types.js +2 -1
- package/dist/contracts/index.d.ts +5 -5
- package/dist/contracts/index.js +3 -3
- package/dist/contracts/sessions/index.d.ts +2 -2
- package/dist/contracts/sessions/index.js +1 -1
- package/dist/contracts/sessions/types.d.ts +5 -0
- package/dist/contracts/sessions/types.js +2 -0
- package/dist/contracts/types.d.ts +242 -0
- package/dist/contracts/types.js +37 -5
- package/dist/providers/claude/agents/agents.d.ts +7 -3
- package/dist/providers/claude/agents/agents.js +33 -19
- package/dist/providers/claude/agents/constants.d.ts +2 -1
- package/dist/providers/claude/agents/constants.js +3 -2
- package/dist/providers/claude/claude.js +80 -30
- package/dist/providers/claude/control/control.d.ts +44 -5
- package/dist/providers/claude/control/control.js +425 -228
- package/dist/providers/claude/control/stream.js +22 -6
- package/dist/providers/claude/control/types.d.ts +7 -0
- package/dist/providers/claude/history/history.model.js +44 -27
- package/dist/providers/claude/history/types.d.ts +88 -0
- package/dist/providers/claude/history/types.js +15 -0
- package/dist/providers/claude/history/utilities.d.ts +6 -4
- package/dist/providers/claude/history/utilities.js +146 -52
- package/dist/providers/codex/adapter.js +33 -4
- package/dist/providers/codex/codex.js +5 -4
- package/dist/providers/codex/events.js +3 -1
- package/dist/providers/codex/transformers.d.ts +4 -2
- package/dist/providers/codex/transformers.js +80 -21
- package/dist/providers/codex/types.d.ts +4 -3
- package/dist/providers/registry/registry.js +12 -9
- package/dist/providers/registry/types.d.ts +1 -1
- package/dist/providers/registry/validations.js +7 -0
- package/dist/providers/types.d.ts +7 -1
- package/dist/runtime/runtime.js +20 -4
- package/dist/sessions/evidence/utilities.js +3 -1
- package/dist/sessions/index.d.ts +2 -2
- package/dist/sessions/index.js +3 -3
- package/dist/sessions/sessions.d.ts +11 -1
- package/dist/sessions/sessions.js +89 -7
- package/dist/sessions/utilities.d.ts +15 -0
- package/dist/sessions/utilities.js +19 -0
- package/dist/store/schema.sql +1 -1
- package/dist/store/sessions/index.d.ts +2 -0
- package/dist/store/sessions/index.js +2 -0
- package/dist/store/sessions/sessions.model.d.ts +9 -0
- package/dist/store/sessions/sessions.model.js +55 -0
- package/dist/store/sessions/types.d.ts +67 -0
- package/dist/store/sessions/types.js +36 -0
- package/dist/store/store.js +4 -0
- package/dist/store/store.worker.js +3 -0
- package/dist/store/types.d.ts +40 -0
- package/dist/store/types.js +1 -0
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
2
|
import { Exception, isException } from 'jg-exception';
|
|
3
|
-
import { isEqual } from 'jg-utilities';
|
|
3
|
+
import { delay, isEqual } from 'jg-utilities';
|
|
4
4
|
import { z } from 'zod';
|
|
5
5
|
import { DISPATCH_OUTPUT_SCHEMA } from '../../../contracts/collaboration/index.js';
|
|
6
6
|
import { capabilityUnavailable } from '../../registry/index.js';
|
|
@@ -10,6 +10,8 @@ import { createClaudeRpc } from '../rpc.js';
|
|
|
10
10
|
import { ClaudeInitializeSchema } from '../types.js';
|
|
11
11
|
import { receiveClaudeMessage } from './stream.js';
|
|
12
12
|
import { toClaudeSkills } from './skills.js';
|
|
13
|
+
// a session idle this long since its last turn ended gives up its process until it is needed
|
|
14
|
+
const IDLE_STOP_SECONDS = 3_600;
|
|
13
15
|
/**
|
|
14
16
|
* Owns one persistent native CLI per explicitly controlled Claude session.
|
|
15
17
|
* @returns Controls whose processes outlive client connections and stop at runtime shutdown.
|
|
@@ -17,6 +19,8 @@ import { toClaudeSkills } from './skills.js';
|
|
|
17
19
|
export const createClaudeControl = (additionalDirectories = [], agentTools) => {
|
|
18
20
|
const sessions = new Map();
|
|
19
21
|
const extensionRequests = new Map();
|
|
22
|
+
// calls running against each session, which a session never parks during
|
|
23
|
+
const activeCalls = new Map();
|
|
20
24
|
let events;
|
|
21
25
|
let isClosing = false;
|
|
22
26
|
const unavailable = () => {
|
|
@@ -33,12 +37,130 @@ export const createClaudeControl = (additionalDirectories = [], agentTools) => {
|
|
|
33
37
|
},
|
|
34
38
|
});
|
|
35
39
|
};
|
|
36
|
-
const get = (id) => {
|
|
40
|
+
const get = (id, isParkedAllowed = false) => {
|
|
37
41
|
const session = sessions.get(id);
|
|
38
|
-
if (!session
|
|
42
|
+
if (!session ||
|
|
43
|
+
session.isClosing ||
|
|
44
|
+
!(session.connection?.isAvailable || (isParkedAllowed && session.parked)))
|
|
39
45
|
unavailable();
|
|
40
46
|
return session;
|
|
41
47
|
};
|
|
48
|
+
const countOpen = () => [...sessions.values()].filter((session) => !session.parked).length;
|
|
49
|
+
const hasExtensionRequest = (nativeId) => [...extensionRequests.values()].some((request) => request.nativeId === nativeId);
|
|
50
|
+
/** Stops the wait that parks an idle session, as a turn or a stop does. */
|
|
51
|
+
const cancelIdleStop = (session) => {
|
|
52
|
+
session.idleStop?.abort();
|
|
53
|
+
session.idleStop = undefined;
|
|
54
|
+
};
|
|
55
|
+
/** Parks the session once it stays idle for the whole period, restarting any earlier wait. */
|
|
56
|
+
const scheduleIdleStop = (session) => {
|
|
57
|
+
cancelIdleStop(session);
|
|
58
|
+
if (isClosing || session.isClosing)
|
|
59
|
+
return;
|
|
60
|
+
const idleStop = new AbortController();
|
|
61
|
+
session.idleStop = idleStop;
|
|
62
|
+
void delay(IDLE_STOP_SECONDS, { signal: idleStop.signal }).then(() => park(session), () => undefined);
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Reads an idle session's native settings once, then closes its process and releases its agent
|
|
66
|
+
* bridge. The session stays idle and records nothing; a call that arrives first keeps the process.
|
|
67
|
+
*/
|
|
68
|
+
const park = async (session) => {
|
|
69
|
+
const connection = session.connection;
|
|
70
|
+
if (isClosing ||
|
|
71
|
+
session.isClosing ||
|
|
72
|
+
session.input ||
|
|
73
|
+
session.transition ||
|
|
74
|
+
!connection?.isAvailable)
|
|
75
|
+
return;
|
|
76
|
+
if (activeCalls.has(session.id) || session.requests.size || hasExtensionRequest(session.id)) {
|
|
77
|
+
scheduleIdleStop(session);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
session.transition = (async () => {
|
|
81
|
+
let configuration;
|
|
82
|
+
try {
|
|
83
|
+
configuration = await connection.requestControl({ subtype: 'get_settings' });
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
scheduleIdleStop(session);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (isClosing || session.isClosing || activeCalls.has(session.id)) {
|
|
90
|
+
scheduleIdleStop(session);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
// set before closing, so the connection's close isn't reported as a lost process
|
|
94
|
+
session.parked = {
|
|
95
|
+
configuration,
|
|
96
|
+
agents: connection.initialization.agents,
|
|
97
|
+
permissionMode: connection.initialization.current_permission_mode,
|
|
98
|
+
};
|
|
99
|
+
try {
|
|
100
|
+
await connection.close();
|
|
101
|
+
await session.agentBridge?.release();
|
|
102
|
+
session.agentBridge = undefined;
|
|
103
|
+
session.connection = undefined;
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
session.parked = undefined;
|
|
107
|
+
events?.unavailable(session.id);
|
|
108
|
+
}
|
|
109
|
+
})().finally(() => {
|
|
110
|
+
session.transition = undefined;
|
|
111
|
+
});
|
|
112
|
+
await session.transition;
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* Reopens a parked session with `--resume`, its kept settings and a new agent bridge; a session
|
|
116
|
+
* that fails to reopen is unavailable, as one whose process was lost.
|
|
117
|
+
*/
|
|
118
|
+
const reopen = async (session, signal, toReopenedSettings) => {
|
|
119
|
+
if (isClosing || countOpen() >= 32)
|
|
120
|
+
conflict();
|
|
121
|
+
if (toReopenedSettings)
|
|
122
|
+
session.settings = toReopenedSettings(session.settings);
|
|
123
|
+
session.parked = undefined;
|
|
124
|
+
session.scope = randomUUID();
|
|
125
|
+
session.opening = start(session, session.hasInput, undefined, signal);
|
|
126
|
+
try {
|
|
127
|
+
await session.opening;
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
events?.unavailable(session.id);
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Runs one call on a session after any park or reopen in progress, never letting it park
|
|
136
|
+
* meanwhile. A parked session reopens first when the call needs its process, with the settings
|
|
137
|
+
* `toReopenedSettings` selects, and otherwise answers from what it kept.
|
|
138
|
+
*/
|
|
139
|
+
const withSession = async (nativeId, isProcessNeeded, call, signal, toReopenedSettings) => {
|
|
140
|
+
activeCalls.set(nativeId, (activeCalls.get(nativeId) ?? 0) + 1);
|
|
141
|
+
try {
|
|
142
|
+
let session = sessions.get(nativeId);
|
|
143
|
+
while (session?.transition) {
|
|
144
|
+
await session.transition.catch(() => undefined);
|
|
145
|
+
session = sessions.get(nativeId);
|
|
146
|
+
}
|
|
147
|
+
if (session?.parked && isProcessNeeded && !session.isClosing) {
|
|
148
|
+
const parked = session;
|
|
149
|
+
parked.transition = reopen(parked, signal, toReopenedSettings).finally(() => {
|
|
150
|
+
parked.transition = undefined;
|
|
151
|
+
});
|
|
152
|
+
await parked.transition;
|
|
153
|
+
}
|
|
154
|
+
return await call(get(nativeId, !isProcessNeeded));
|
|
155
|
+
}
|
|
156
|
+
finally {
|
|
157
|
+
const count = (activeCalls.get(nativeId) ?? 1) - 1;
|
|
158
|
+
if (count > 0)
|
|
159
|
+
activeCalls.set(nativeId, count);
|
|
160
|
+
else
|
|
161
|
+
activeCalls.delete(nativeId);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
42
164
|
/** Refreshes optional command/agent metadata after native configuration changes. */
|
|
43
165
|
const refreshCatalog = async (session) => {
|
|
44
166
|
session.skills = null;
|
|
@@ -82,12 +204,118 @@ export const createClaudeControl = (additionalDirectories = [], agentTools) => {
|
|
|
82
204
|
*/
|
|
83
205
|
const validateInput = (nativeId, settings) => {
|
|
84
206
|
if (settings.outputSchema &&
|
|
85
|
-
!isEqual(settings.outputSchema, get(nativeId).settings.outputSchema) &&
|
|
207
|
+
!isEqual(settings.outputSchema, get(nativeId, true).settings.outputSchema) &&
|
|
86
208
|
!isEqual(settings.outputSchema, DISPATCH_OUTPUT_SCHEMA))
|
|
87
209
|
capabilityUnavailable();
|
|
88
210
|
};
|
|
211
|
+
/**
|
|
212
|
+
* Starts a session's native process and permits control only after native idle initialization,
|
|
213
|
+
* beginning its idle period.
|
|
214
|
+
* @throws
|
|
215
|
+
* - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
|
|
216
|
+
* - JG_AGENT_HQ_COLLABORATION_CONFLICT: The HQ collaboration operation conflicts with its recorded state
|
|
217
|
+
* - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
|
|
218
|
+
* - JG_AGENT_HQ_CAPABILITY_UNAVAILABLE: The HQ capability is unavailable
|
|
219
|
+
* - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
|
|
220
|
+
*/
|
|
221
|
+
const start = async (session, isResume, forkSourceNativeId, signal) => {
|
|
222
|
+
const nativeId = session.id;
|
|
223
|
+
session.agentBridge = await agentTools?.mcp(nativeId);
|
|
224
|
+
try {
|
|
225
|
+
const connection = await createClaudeRpc((reason) => {
|
|
226
|
+
if (reason !== 'unavailable' || session.isClosing || session.parked)
|
|
227
|
+
return;
|
|
228
|
+
if (session.input) {
|
|
229
|
+
clearTimeout(session.input.timer);
|
|
230
|
+
session.input.reject(new Exception('The Claude integration failed', {
|
|
231
|
+
code: 'JG_AGENT_HQ_CLAUDE_FAILED',
|
|
232
|
+
data: {
|
|
233
|
+
kind: 'agent-unavailable',
|
|
234
|
+
},
|
|
235
|
+
}));
|
|
236
|
+
}
|
|
237
|
+
events?.unavailable(session.id);
|
|
238
|
+
}, {
|
|
239
|
+
nativeId,
|
|
240
|
+
workspaceRoot: session.workspaceRoot,
|
|
241
|
+
additionalDirectories,
|
|
242
|
+
isResume,
|
|
243
|
+
settings: session.settings,
|
|
244
|
+
...(session.agentBridge
|
|
245
|
+
? {
|
|
246
|
+
mcpServers: {
|
|
247
|
+
[session.agentBridge.serverName]: session.agentBridge.configuration,
|
|
248
|
+
},
|
|
249
|
+
}
|
|
250
|
+
: {}),
|
|
251
|
+
...(forkSourceNativeId ? { forkSourceNativeId } : {}),
|
|
252
|
+
receive: (raw) => {
|
|
253
|
+
const hadInput = session.input !== undefined;
|
|
254
|
+
receiveClaudeMessage(session, raw, events);
|
|
255
|
+
// the end of a turn starts the session's idle period
|
|
256
|
+
if (hadInput && !session.input)
|
|
257
|
+
scheduleIdleStop(session);
|
|
258
|
+
},
|
|
259
|
+
lateResponse(id, result, error) {
|
|
260
|
+
if (extensionRequests.get(id)?.nativeId === nativeId) {
|
|
261
|
+
if (error !== undefined) {
|
|
262
|
+
extensionRequests.delete(id);
|
|
263
|
+
events?.outcome({ kind: 'failed', requestId: id, failure: 'agent-unavailable' });
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
void (async () => {
|
|
267
|
+
try {
|
|
268
|
+
const resultState = await completeExtension(id, result);
|
|
269
|
+
events?.outcome({ kind: 'control-accepted', requestId: id, ...resultState });
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
events?.unavailable(nativeId);
|
|
273
|
+
await session.connection?.close();
|
|
274
|
+
}
|
|
275
|
+
})().catch(() => events?.unavailable(nativeId));
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (!/^[a-f0-9-]{36}:(name|interrupt)$/.test(id))
|
|
279
|
+
return;
|
|
280
|
+
if (error !== undefined)
|
|
281
|
+
events?.outcome({
|
|
282
|
+
kind: 'failed',
|
|
283
|
+
requestId: id,
|
|
284
|
+
failure: 'agent-unavailable',
|
|
285
|
+
});
|
|
286
|
+
else
|
|
287
|
+
events?.outcome({
|
|
288
|
+
kind: id.endsWith(':name') ? 'renamed' : 'interrupt-accepted',
|
|
289
|
+
requestId: id,
|
|
290
|
+
});
|
|
291
|
+
},
|
|
292
|
+
}, undefined, signal);
|
|
293
|
+
session.connection = connection;
|
|
294
|
+
if (connection.initialization.session_state !== 'idle') {
|
|
295
|
+
await connection.close();
|
|
296
|
+
unavailable();
|
|
297
|
+
}
|
|
298
|
+
// unsupported optional metadata disables the catalog, without disabling native controls
|
|
299
|
+
session.skills = null;
|
|
300
|
+
try {
|
|
301
|
+
const skills = toClaudeSkills({ skills: connection.initialization.commands });
|
|
302
|
+
if (Buffer.byteLength(JSON.stringify(skills.skills)) <= 1_048_576)
|
|
303
|
+
session.skills = { ...skills, observedAt: connection.initializedAt };
|
|
304
|
+
}
|
|
305
|
+
catch {
|
|
306
|
+
/* catalog availability is reported independently */
|
|
307
|
+
}
|
|
308
|
+
delete connection.initialization.commands;
|
|
309
|
+
scheduleIdleStop(session);
|
|
310
|
+
}
|
|
311
|
+
catch (error) {
|
|
312
|
+
await session.agentBridge?.release();
|
|
313
|
+
throw error;
|
|
314
|
+
}
|
|
315
|
+
};
|
|
89
316
|
/**
|
|
90
317
|
* Records ownership before startup and permits control only after native idle initialization.
|
|
318
|
+
* Parked sessions don't count toward the 32 open processes.
|
|
91
319
|
* @returns The process-bound native identity and workspace.
|
|
92
320
|
* @throws
|
|
93
321
|
* - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
|
|
@@ -100,7 +328,7 @@ export const createClaudeControl = (additionalDirectories = [], agentTools) => {
|
|
|
100
328
|
const open = async (nativeId, workspaceRoot, isResume, forkSourceNativeId, settings = {}, signal) => {
|
|
101
329
|
if (isClosing ||
|
|
102
330
|
sessions.has(nativeId) ||
|
|
103
|
-
|
|
331
|
+
countOpen() >= 32 ||
|
|
104
332
|
!z.uuid().safeParse(nativeId).success)
|
|
105
333
|
conflict();
|
|
106
334
|
const session = {
|
|
@@ -115,149 +343,98 @@ export const createClaudeControl = (additionalDirectories = [], agentTools) => {
|
|
|
115
343
|
requests: new Map(),
|
|
116
344
|
};
|
|
117
345
|
sessions.set(nativeId, session);
|
|
118
|
-
session.opening = (
|
|
119
|
-
session.agentBridge = await agentTools?.mcp(nativeId);
|
|
120
|
-
try {
|
|
121
|
-
const connection = await createClaudeRpc((reason) => {
|
|
122
|
-
if (reason !== 'unavailable' || session.isClosing)
|
|
123
|
-
return;
|
|
124
|
-
if (session.input) {
|
|
125
|
-
clearTimeout(session.input.timer);
|
|
126
|
-
session.input.reject(new Exception('The Claude integration failed', {
|
|
127
|
-
code: 'JG_AGENT_HQ_CLAUDE_FAILED',
|
|
128
|
-
data: {
|
|
129
|
-
kind: 'agent-unavailable',
|
|
130
|
-
},
|
|
131
|
-
}));
|
|
132
|
-
}
|
|
133
|
-
events?.unavailable(session.id);
|
|
134
|
-
}, {
|
|
135
|
-
nativeId,
|
|
136
|
-
workspaceRoot,
|
|
137
|
-
additionalDirectories,
|
|
138
|
-
isResume,
|
|
139
|
-
settings,
|
|
140
|
-
...(session.agentBridge
|
|
141
|
-
? {
|
|
142
|
-
mcpServers: {
|
|
143
|
-
[session.agentBridge.serverName]: session.agentBridge.configuration,
|
|
144
|
-
},
|
|
145
|
-
}
|
|
146
|
-
: {}),
|
|
147
|
-
...(forkSourceNativeId ? { forkSourceNativeId } : {}),
|
|
148
|
-
receive: (raw) => receiveClaudeMessage(session, raw, events),
|
|
149
|
-
lateResponse(id, result, error) {
|
|
150
|
-
if (extensionRequests.get(id)?.nativeId === nativeId) {
|
|
151
|
-
if (error !== undefined) {
|
|
152
|
-
extensionRequests.delete(id);
|
|
153
|
-
events?.outcome({ kind: 'failed', requestId: id, failure: 'agent-unavailable' });
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
void (async () => {
|
|
157
|
-
try {
|
|
158
|
-
const resultState = await completeExtension(id, result);
|
|
159
|
-
events?.outcome({ kind: 'control-accepted', requestId: id, ...resultState });
|
|
160
|
-
}
|
|
161
|
-
catch {
|
|
162
|
-
events?.unavailable(nativeId);
|
|
163
|
-
await session.connection?.close();
|
|
164
|
-
}
|
|
165
|
-
})().catch(() => events?.unavailable(nativeId));
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
if (!/^[a-f0-9-]{36}:(name|interrupt)$/.test(id))
|
|
169
|
-
return;
|
|
170
|
-
if (error !== undefined)
|
|
171
|
-
events?.outcome({
|
|
172
|
-
kind: 'failed',
|
|
173
|
-
requestId: id,
|
|
174
|
-
failure: 'agent-unavailable',
|
|
175
|
-
});
|
|
176
|
-
else
|
|
177
|
-
events?.outcome({
|
|
178
|
-
kind: id.endsWith(':name') ? 'renamed' : 'interrupt-accepted',
|
|
179
|
-
requestId: id,
|
|
180
|
-
});
|
|
181
|
-
},
|
|
182
|
-
}, undefined, signal);
|
|
183
|
-
session.connection = connection;
|
|
184
|
-
if (connection.initialization.session_state !== 'idle') {
|
|
185
|
-
await connection.close();
|
|
186
|
-
unavailable();
|
|
187
|
-
}
|
|
188
|
-
// unsupported optional metadata disables the catalog, without disabling native controls
|
|
189
|
-
session.skills = null;
|
|
190
|
-
try {
|
|
191
|
-
const skills = toClaudeSkills({ skills: connection.initialization.commands });
|
|
192
|
-
if (Buffer.byteLength(JSON.stringify(skills.skills)) <= 1_048_576)
|
|
193
|
-
session.skills = { ...skills, observedAt: connection.initializedAt };
|
|
194
|
-
}
|
|
195
|
-
catch {
|
|
196
|
-
/* catalog availability is reported independently */
|
|
197
|
-
}
|
|
198
|
-
delete connection.initialization.commands;
|
|
199
|
-
}
|
|
200
|
-
catch (error) {
|
|
201
|
-
await session.agentBridge?.release();
|
|
202
|
-
throw error;
|
|
203
|
-
}
|
|
204
|
-
})();
|
|
346
|
+
session.opening = start(session, isResume, forkSourceNativeId, signal);
|
|
205
347
|
await session.opening;
|
|
206
348
|
return { id: nativeId, cwd: workspaceRoot, status: { type: 'idle' } };
|
|
207
349
|
};
|
|
208
350
|
return {
|
|
209
351
|
/**
|
|
210
|
-
* Applies one supported native extension command to an idle owned session.
|
|
352
|
+
* Applies one supported native extension command to an idle owned session, reopening a parked one.
|
|
211
353
|
* @returns Native acknowledgement, preserving held plugin reloads explicitly.
|
|
212
354
|
* @throws
|
|
213
355
|
* - JG_AGENT_HQ_ACTION_CONFLICT: The HQ action conflicts with its recorded state
|
|
356
|
+
* - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
|
|
357
|
+
* - JG_AGENT_HQ_COLLABORATION_CONFLICT: The HQ collaboration operation conflicts with its recorded state
|
|
214
358
|
* - JG_AGENT_HQ_CAPABILITY_UNAVAILABLE: The HQ capability is unavailable
|
|
215
359
|
* - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
|
|
360
|
+
* - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
|
|
216
361
|
*/
|
|
217
362
|
async controlExtensions(requestId, nativeId, workspaceRoot, command) {
|
|
218
|
-
|
|
219
|
-
if (session.workspaceRoot !== workspaceRoot)
|
|
363
|
+
if (get(nativeId, true).workspaceRoot !== workspaceRoot)
|
|
220
364
|
conflict();
|
|
221
365
|
if (command.kind === 'mcp-login' || command.kind === 'mcp-reload')
|
|
222
366
|
capabilityUnavailable();
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
367
|
+
return withSession(nativeId, true, async (session) => {
|
|
368
|
+
if (session.input || session.requests.size || extensionRequests.size >= 32)
|
|
369
|
+
conflict();
|
|
370
|
+
extensionRequests.set(requestId, { nativeId, kind: command.kind });
|
|
371
|
+
const body = command.kind === 'skills-reload'
|
|
372
|
+
? { subtype: 'reload_skills' }
|
|
373
|
+
: command.kind === 'plugins-reload'
|
|
374
|
+
? { subtype: 'reload_plugins', hold_on_cache_impact: true }
|
|
375
|
+
: command.kind === 'mcp-reconnect'
|
|
376
|
+
? { subtype: 'mcp_reconnect', serverName: command.serverName }
|
|
377
|
+
: {
|
|
378
|
+
subtype: 'mcp_toggle',
|
|
379
|
+
serverName: command.serverName,
|
|
380
|
+
enabled: command.isEnabled,
|
|
381
|
+
};
|
|
382
|
+
try {
|
|
383
|
+
return await completeExtension(requestId, await session.connection.requestControl(body, requestId));
|
|
384
|
+
}
|
|
385
|
+
catch (error) {
|
|
386
|
+
if (isException(error) &&
|
|
387
|
+
z.object({ nativeRejection: z.literal(true) }).safeParse(error.data).success)
|
|
388
|
+
extensionRequests.delete(requestId);
|
|
389
|
+
throw error;
|
|
390
|
+
}
|
|
391
|
+
});
|
|
246
392
|
},
|
|
247
393
|
get isAvailable() {
|
|
248
394
|
return !isClosing;
|
|
249
395
|
},
|
|
250
|
-
/**
|
|
396
|
+
/**
|
|
397
|
+
* Reads native MCP status for this exact controlled session, reopening a parked one.
|
|
398
|
+
* @returns The native server page.
|
|
399
|
+
* @throws
|
|
400
|
+
* - JG_AGENT_HQ_CURSOR_INVALID: The HQ cursor does not match this request
|
|
401
|
+
* - JG_AGENT_HQ_ACTION_CONFLICT: The HQ action conflicts with its recorded state
|
|
402
|
+
* - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
|
|
403
|
+
* - JG_AGENT_HQ_COLLABORATION_CONFLICT: The HQ collaboration operation conflicts with its recorded state
|
|
404
|
+
* - JG_AGENT_HQ_CAPABILITY_UNAVAILABLE: The HQ capability is unavailable
|
|
405
|
+
* - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
|
|
406
|
+
* - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
|
|
407
|
+
*/
|
|
251
408
|
readMcpServers(nativeId, limit, cursor) {
|
|
252
|
-
return
|
|
409
|
+
return withSession(nativeId, true, (session) => createClaudeExtensions(session.connection).readMcpServers(limit, cursor));
|
|
253
410
|
},
|
|
254
|
-
/**
|
|
411
|
+
/**
|
|
412
|
+
* Reads native tool metadata for one configured session server, reopening a parked one.
|
|
413
|
+
* @returns The server's native tool metadata.
|
|
414
|
+
* @throws
|
|
415
|
+
* - JG_AGENT_HQ_ACTION_CONFLICT: The HQ action conflicts with its recorded state
|
|
416
|
+
* - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
|
|
417
|
+
* - JG_AGENT_HQ_COLLABORATION_CONFLICT: The HQ collaboration operation conflicts with its recorded state
|
|
418
|
+
* - JG_AGENT_HQ_CAPABILITY_UNAVAILABLE: The HQ capability is unavailable
|
|
419
|
+
* - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
|
|
420
|
+
* - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
|
|
421
|
+
*/
|
|
255
422
|
readMcpTools(nativeId, serverName) {
|
|
256
|
-
return
|
|
423
|
+
return withSession(nativeId, true, (session) => createClaudeExtensions(session.connection).readMcpTools(serverName));
|
|
257
424
|
},
|
|
258
|
-
/**
|
|
425
|
+
/**
|
|
426
|
+
* Reports the native resource catalog, reopening a parked session.
|
|
427
|
+
* @returns An explicit unsupported native resource catalog observation.
|
|
428
|
+
* @throws
|
|
429
|
+
* - JG_AGENT_HQ_ACTION_CONFLICT: The HQ action conflicts with its recorded state
|
|
430
|
+
* - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
|
|
431
|
+
* - JG_AGENT_HQ_COLLABORATION_CONFLICT: The HQ collaboration operation conflicts with its recorded state
|
|
432
|
+
* - JG_AGENT_HQ_CAPABILITY_UNAVAILABLE: The HQ capability is unavailable
|
|
433
|
+
* - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
|
|
434
|
+
* - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
|
|
435
|
+
*/
|
|
259
436
|
readMcpResources(nativeId) {
|
|
260
|
-
return
|
|
437
|
+
return withSession(nativeId, true, async (session) => createClaudeExtensions(session.connection).readMcpResources());
|
|
261
438
|
},
|
|
262
439
|
get version() {
|
|
263
440
|
return ([...sessions.values()].find((session) => session.connection)?.connection?.version ?? null);
|
|
@@ -267,7 +444,7 @@ export const createClaudeControl = (additionalDirectories = [], agentTools) => {
|
|
|
267
444
|
? 'unavailable'
|
|
268
445
|
: [...sessions.values()].some((session) => session.connection?.isAvailable && !session.isClosing)
|
|
269
446
|
? 'ready'
|
|
270
|
-
:
|
|
447
|
+
: countOpen()
|
|
271
448
|
? 'unavailable'
|
|
272
449
|
: 'idle';
|
|
273
450
|
},
|
|
@@ -338,10 +515,7 @@ export const createClaudeControl = (additionalDirectories = [], agentTools) => {
|
|
|
338
515
|
* - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
|
|
339
516
|
*/
|
|
340
517
|
async readSkills(nativeId) {
|
|
341
|
-
|
|
342
|
-
if (!skills)
|
|
343
|
-
unavailable();
|
|
344
|
-
return skills;
|
|
518
|
+
return withSession(nativeId, false, async (session) => session.skills ?? unavailable());
|
|
345
519
|
},
|
|
346
520
|
/**
|
|
347
521
|
* Reads settings from the exact owned native process rather than a global configuration file.
|
|
@@ -351,18 +525,28 @@ export const createClaudeControl = (additionalDirectories = [], agentTools) => {
|
|
|
351
525
|
* - JG_AGENT_HQ_CURSOR_INVALID: The HQ cursor does not match this request
|
|
352
526
|
*/
|
|
353
527
|
async readConfiguration(command, nativeId) {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
528
|
+
return withSession(nativeId, false, async (session) => {
|
|
529
|
+
const native = session.parked ?? {
|
|
530
|
+
configuration: await session.connection.requestControl({ subtype: 'get_settings' }),
|
|
531
|
+
agents: session.connection.initialization.agents,
|
|
532
|
+
permissionMode: session.connection.initialization.current_permission_mode,
|
|
533
|
+
};
|
|
534
|
+
return toClaudeConfiguration(native.configuration, native.agents, native.permissionMode, nativeId, session.settings, command);
|
|
535
|
+
});
|
|
357
536
|
},
|
|
358
537
|
/**
|
|
359
|
-
* Names only the native session selected by the admitted action.
|
|
538
|
+
* Names only the native session selected by the admitted action, reopening a parked one.
|
|
360
539
|
* @returns Native acknowledgement of the name change.
|
|
361
540
|
* @throws
|
|
541
|
+
* - JG_AGENT_HQ_ACTION_CONFLICT: The HQ action conflicts with its recorded state
|
|
542
|
+
* - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
|
|
543
|
+
* - JG_AGENT_HQ_COLLABORATION_CONFLICT: The HQ collaboration operation conflicts with its recorded state
|
|
544
|
+
* - JG_AGENT_HQ_CAPABILITY_UNAVAILABLE: The HQ capability is unavailable
|
|
362
545
|
* - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
|
|
546
|
+
* - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
|
|
363
547
|
*/
|
|
364
548
|
async nameThread(requestId, nativeId, name) {
|
|
365
|
-
await
|
|
549
|
+
await withSession(nativeId, true, (session) => session.connection.requestControl({ subtype: 'rename_session', title: name, source: 'host', session_id: nativeId }, requestId));
|
|
366
550
|
},
|
|
367
551
|
validateInput,
|
|
368
552
|
/**
|
|
@@ -377,98 +561,104 @@ export const createClaudeControl = (additionalDirectories = [], agentTools) => {
|
|
|
377
561
|
* - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
|
|
378
562
|
*/
|
|
379
563
|
async startTurn(requestId, nativeId, text, settings = {}, signal, files = []) {
|
|
380
|
-
let session = get(nativeId);
|
|
381
564
|
validateInput(nativeId, settings);
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
if (session.requests.size ||
|
|
390
|
-
[...extensionRequests.values()].some((request) => request.nativeId === nativeId))
|
|
565
|
+
const isEnvelope = settings.outputSchema !== undefined &&
|
|
566
|
+
isEqual(settings.outputSchema, DISPATCH_OUTPUT_SCHEMA);
|
|
567
|
+
return withSession(nativeId, true, async (opened) => {
|
|
568
|
+
let session = opened;
|
|
569
|
+
if (signal?.aborted)
|
|
570
|
+
unavailable();
|
|
571
|
+
if (session.input)
|
|
391
572
|
conflict();
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
573
|
+
if (isEnvelope && !isEqual(session.settings.outputSchema, DISPATCH_OUTPUT_SCHEMA)) {
|
|
574
|
+
if (session.requests.size ||
|
|
575
|
+
[...extensionRequests.values()].some((request) => request.nativeId === nativeId))
|
|
576
|
+
conflict();
|
|
577
|
+
cancelIdleStop(session);
|
|
578
|
+
const selected = { ...session.settings, outputSchema: DISPATCH_OUTPUT_SCHEMA };
|
|
579
|
+
const workspaceRoot = session.workspaceRoot;
|
|
580
|
+
const hasInput = session.hasInput;
|
|
581
|
+
session.isClosing = true;
|
|
582
|
+
await session.connection.close();
|
|
583
|
+
await session.agentBridge?.release();
|
|
584
|
+
sessions.delete(nativeId);
|
|
585
|
+
await open(nativeId, workspaceRoot, hasInput, undefined, selected, signal);
|
|
586
|
+
session = get(nativeId);
|
|
587
|
+
}
|
|
588
|
+
if (settings.model || settings.effort) {
|
|
589
|
+
await session.connection.requestControl({
|
|
590
|
+
subtype: 'apply_flag_settings',
|
|
591
|
+
settings: {
|
|
592
|
+
...(settings.model ? { model: settings.model } : {}),
|
|
593
|
+
...(settings.effort ? { effortLevel: settings.effort } : {}),
|
|
594
|
+
},
|
|
595
|
+
});
|
|
596
|
+
if (settings.model)
|
|
597
|
+
session.settings.model = settings.model;
|
|
598
|
+
if (settings.effort)
|
|
599
|
+
session.settings.effort = settings.effort;
|
|
600
|
+
}
|
|
601
|
+
if (signal?.aborted || session.isClosing || !session.connection?.isAvailable)
|
|
602
|
+
unavailable();
|
|
603
|
+
if (session.input)
|
|
604
|
+
conflict();
|
|
605
|
+
return new Promise((resolve, reject) => {
|
|
606
|
+
const id = randomUUID();
|
|
607
|
+
const timer = setTimeout(() => reject(new Exception('The Claude integration failed: input acknowledgement deadline exceeded', { code: 'JG_AGENT_HQ_CLAUDE_FAILED', data: { kind: 'agent-unavailable' } })), 30_000);
|
|
608
|
+
session.input = {
|
|
609
|
+
id,
|
|
610
|
+
requestId,
|
|
611
|
+
isAcknowledged: false,
|
|
612
|
+
interruptRequested: false,
|
|
613
|
+
hasInterruptionMarker: false,
|
|
614
|
+
finalText: null,
|
|
615
|
+
isOutputOversized: false,
|
|
616
|
+
isOrdinaryEnvelope: settings.outputSchema === undefined &&
|
|
617
|
+
isEqual(session.settings.outputSchema, DISPATCH_OUTPUT_SCHEMA),
|
|
618
|
+
resolve,
|
|
619
|
+
reject,
|
|
620
|
+
timer,
|
|
621
|
+
};
|
|
622
|
+
session.hasInput = true;
|
|
623
|
+
cancelIdleStop(session);
|
|
624
|
+
void session
|
|
625
|
+
.connection.write({
|
|
626
|
+
type: 'user',
|
|
627
|
+
uuid: id,
|
|
628
|
+
session_id: nativeId,
|
|
629
|
+
message: {
|
|
630
|
+
role: 'user',
|
|
631
|
+
content: files.length
|
|
632
|
+
? [
|
|
633
|
+
{ type: 'text', text },
|
|
634
|
+
...files.map((file) => file.mediaType === 'text/plain'
|
|
635
|
+
? {
|
|
636
|
+
type: 'document',
|
|
637
|
+
source: {
|
|
638
|
+
type: 'text',
|
|
639
|
+
media_type: 'text/plain',
|
|
640
|
+
data: new TextDecoder('utf-8', { fatal: true }).decode(file.content),
|
|
641
|
+
},
|
|
642
|
+
title: file.path,
|
|
643
|
+
}
|
|
644
|
+
: {
|
|
645
|
+
type: file.mediaType.startsWith('image/') ? 'image' : 'document',
|
|
646
|
+
source: {
|
|
647
|
+
type: 'base64',
|
|
648
|
+
media_type: file.mediaType,
|
|
649
|
+
data: Buffer.from(file.content).toString('base64'),
|
|
650
|
+
},
|
|
651
|
+
}),
|
|
652
|
+
]
|
|
653
|
+
: text,
|
|
654
|
+
},
|
|
655
|
+
parent_tool_use_id: null,
|
|
656
|
+
})
|
|
657
|
+
.catch(reject);
|
|
409
658
|
});
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
session.settings.effort = settings.effort;
|
|
414
|
-
}
|
|
415
|
-
if (signal?.aborted || session.isClosing || !session.connection?.isAvailable)
|
|
416
|
-
unavailable();
|
|
417
|
-
if (session.input)
|
|
418
|
-
conflict();
|
|
419
|
-
return new Promise((resolve, reject) => {
|
|
420
|
-
const id = randomUUID();
|
|
421
|
-
const timer = setTimeout(() => reject(new Exception('The Claude integration failed: input acknowledgement deadline exceeded', { code: 'JG_AGENT_HQ_CLAUDE_FAILED', data: { kind: 'agent-unavailable' } })), 30_000);
|
|
422
|
-
session.input = {
|
|
423
|
-
id,
|
|
424
|
-
requestId,
|
|
425
|
-
isAcknowledged: false,
|
|
426
|
-
interruptRequested: false,
|
|
427
|
-
hasInterruptionMarker: false,
|
|
428
|
-
finalText: null,
|
|
429
|
-
isOutputOversized: false,
|
|
430
|
-
isOrdinaryEnvelope: settings.outputSchema === undefined &&
|
|
431
|
-
isEqual(session.settings.outputSchema, DISPATCH_OUTPUT_SCHEMA),
|
|
432
|
-
resolve,
|
|
433
|
-
reject,
|
|
434
|
-
timer,
|
|
435
|
-
};
|
|
436
|
-
session.hasInput = true;
|
|
437
|
-
void session
|
|
438
|
-
.connection.write({
|
|
439
|
-
type: 'user',
|
|
440
|
-
uuid: id,
|
|
441
|
-
session_id: nativeId,
|
|
442
|
-
message: {
|
|
443
|
-
role: 'user',
|
|
444
|
-
content: files.length
|
|
445
|
-
? [
|
|
446
|
-
{ type: 'text', text },
|
|
447
|
-
...files.map((file) => file.mediaType === 'text/plain'
|
|
448
|
-
? {
|
|
449
|
-
type: 'document',
|
|
450
|
-
source: {
|
|
451
|
-
type: 'text',
|
|
452
|
-
media_type: 'text/plain',
|
|
453
|
-
data: new TextDecoder('utf-8', { fatal: true }).decode(file.content),
|
|
454
|
-
},
|
|
455
|
-
title: file.path,
|
|
456
|
-
}
|
|
457
|
-
: {
|
|
458
|
-
type: file.mediaType.startsWith('image/') ? 'image' : 'document',
|
|
459
|
-
source: {
|
|
460
|
-
type: 'base64',
|
|
461
|
-
media_type: file.mediaType,
|
|
462
|
-
data: Buffer.from(file.content).toString('base64'),
|
|
463
|
-
},
|
|
464
|
-
}),
|
|
465
|
-
]
|
|
466
|
-
: text,
|
|
467
|
-
},
|
|
468
|
-
parent_tool_use_id: null,
|
|
469
|
-
})
|
|
470
|
-
.catch(reject);
|
|
471
|
-
});
|
|
659
|
+
}, signal,
|
|
660
|
+
// a parked session reopens with the envelope a collaboration request needs
|
|
661
|
+
(kept) => (isEnvelope ? { ...kept, outputSchema: DISPATCH_OUTPUT_SCHEMA } : kept));
|
|
472
662
|
},
|
|
473
663
|
/**
|
|
474
664
|
* Requests interruption of the exact active input; completion arrives separately.
|
|
@@ -478,11 +668,13 @@ export const createClaudeControl = (additionalDirectories = [], agentTools) => {
|
|
|
478
668
|
* - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
|
|
479
669
|
*/
|
|
480
670
|
async interrupt(requestId, nativeId, turnId) {
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
671
|
+
// a parked session is idle, so its interrupt is refused as any idle session's is
|
|
672
|
+
await withSession(nativeId, false, async (session) => {
|
|
673
|
+
if (session.input?.id !== turnId)
|
|
674
|
+
conflict();
|
|
675
|
+
session.input.interruptRequested = true;
|
|
676
|
+
await session.connection.requestControl({ subtype: 'interrupt' }, requestId);
|
|
677
|
+
});
|
|
486
678
|
},
|
|
487
679
|
/**
|
|
488
680
|
* Closes the owned CLI and proves that its process group has stopped.
|
|
@@ -496,7 +688,10 @@ export const createClaudeControl = (additionalDirectories = [], agentTools) => {
|
|
|
496
688
|
const session = sessions.get(nativeId);
|
|
497
689
|
if (!session)
|
|
498
690
|
conflict();
|
|
691
|
+
cancelIdleStop(session);
|
|
499
692
|
session.isClosing = true;
|
|
693
|
+
// a parked session finishes without a process
|
|
694
|
+
await session.transition?.catch(() => undefined);
|
|
500
695
|
await session.opening.catch((cause) => {
|
|
501
696
|
if (isException(cause) &&
|
|
502
697
|
z.object({ cleanupUnconfirmed: z.literal(true) }).safeParse(cause.data).success)
|
|
@@ -530,6 +725,7 @@ export const createClaudeControl = (additionalDirectories = [], agentTools) => {
|
|
|
530
725
|
async close() {
|
|
531
726
|
isClosing = true;
|
|
532
727
|
const results = await Promise.allSettled([...sessions.values()].map(async (session) => {
|
|
728
|
+
cancelIdleStop(session);
|
|
533
729
|
session.isClosing = true;
|
|
534
730
|
if (session.input) {
|
|
535
731
|
clearTimeout(session.input.timer);
|
|
@@ -545,6 +741,7 @@ export const createClaudeControl = (additionalDirectories = [], agentTools) => {
|
|
|
545
741
|
z.object({ cleanupUnconfirmed: z.literal(true) }).safeParse(cause.data).success)
|
|
546
742
|
throw cause;
|
|
547
743
|
});
|
|
744
|
+
await session.transition?.catch(() => undefined);
|
|
548
745
|
await session.connection?.close();
|
|
549
746
|
await session.agentBridge?.release();
|
|
550
747
|
}));
|