@orchbee/cli-win32-x64 0.0.0-stage → 0.1.0-beta.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 +21 -0
- package/README.md +4 -2
- package/package.json +22 -5
- package/release/agent-observation-adapters/.agents/plugins/marketplace.json +12 -0
- package/release/agent-observation-adapters/.claude-plugin/marketplace.json +13 -0
- package/release/agent-observation-adapters/README.md +363 -0
- package/release/agent-observation-adapters/bundle/.claude-plugin/plugin.json +8 -0
- package/release/agent-observation-adapters/bundle/.codex-plugin/plugin.json +7 -0
- package/release/agent-observation-adapters/bundle/.grok-plugin/plugin.json +7 -0
- package/release/agent-observation-adapters/bundle/.plugin/plugin.json +7 -0
- package/release/agent-observation-adapters/bundle/cline/orchbee-agent-observation.js +187 -0
- package/release/agent-observation-adapters/bundle/gemini/gemini-extension.json +5 -0
- package/release/agent-observation-adapters/bundle/gemini/hooks/hooks.json +118 -0
- package/release/agent-observation-adapters/bundle/hermes/plugin.py +40 -0
- package/release/agent-observation-adapters/bundle/hooks/codex.json +142 -0
- package/release/agent-observation-adapters/bundle/hooks/copilot.json +114 -0
- package/release/agent-observation-adapters/bundle/hooks/grok.json +170 -0
- package/release/agent-observation-adapters/bundle/hooks/hooks.json +244 -0
- package/release/agent-observation-adapters/bundle/kilo/orchbee-agent-observation.mjs +3 -0
- package/release/agent-observation-adapters/bundle/omp/orchbee-agent-observation.cjs +4 -0
- package/release/agent-observation-adapters/bundle/omp/orchbee-agent-observation.mjs +117 -0
- package/release/agent-observation-adapters/bundle/opencode/orchbee-agent-observation.mjs +13 -0
- package/release/agent-observation-adapters/bundle/package.json +25 -0
- package/release/agent-observation-adapters/bundle/pi/orchbee-agent-observation.cjs +4 -0
- package/release/agent-observation-adapters/bundle/pi/orchbee-agent-observation.mjs +112 -0
- package/release/agent-observation-adapters/bundle/qwen-code/qwen-extension.json +232 -0
- package/release/agent-observation-adapters/bundle/scripts/forward-hook.mjs +33 -0
- package/release/agent-observation-adapters/bundle/scripts/hook-sanitizers.mjs +571 -0
- package/release/agent-observation-adapters/bundle/scripts/observation-transport.mjs +549 -0
- package/release/agent-observation-adapters/bundle/scripts/opencode-family-observation.mjs +350 -0
- package/release/agent-observation-adapters/capsule.json +4 -0
- package/release/cohort.json +1 -0
- package/release/conpty/Microsoft.ConPTY-LICENSE.txt +21 -0
- package/release/conpty/OpenConsole.exe +0 -0
- package/release/conpty/conpty.dll +0 -0
- package/release/licenses/agent-detection/LICENSE +201 -0
- package/release/licenses/agent-detection/NOTICE +20 -0
- package/release/orchbee-launcher.exe +0 -0
- package/release/orchbee-server.exe +0 -0
- package/release/orchbee-terminal-host.exe +0 -0
- package/release/orchbee-worker.exe +0 -0
- package/release/orchbee.exe +4 -0
- package/release/release.json +1 -0
- package/release/release.json.sig +1 -0
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { randomBytes } from 'node:crypto';
|
|
3
|
+
import { constants as fileSystemConstants } from 'node:fs';
|
|
4
|
+
import { lstat, open, readFile, readdir, unlink } from 'node:fs/promises';
|
|
5
|
+
import { createConnection } from 'node:net';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { promisify } from 'node:util';
|
|
8
|
+
|
|
9
|
+
const executeFile = promisify(execFile);
|
|
10
|
+
const MAX_CORRELATIONS = 32;
|
|
11
|
+
const MAX_SOCKET_CANDIDATES = 32;
|
|
12
|
+
const MAX_COMMAND_OUTPUT_BYTES = 64 * 1024;
|
|
13
|
+
const SOCKET_TIMEOUT_MILLISECONDS = 250;
|
|
14
|
+
const IDENTITY_PATTERN = /^[A-Za-z0-9._:-]{1,128}$/u;
|
|
15
|
+
const PRIVATE_SOCKET_NAME_PATTERN = /^orchbee-agent-v1-[A-Za-z0-9_-]{16,64}\.sock$/u;
|
|
16
|
+
const OBSERVATION_SOCKET_ENVIRONMENT_NAME = 'ORCHBEE_AGENT_OBSERVATION_SOCKET';
|
|
17
|
+
const RUN_TOKEN_ENVIRONMENT_NAME = 'ORCHBEE_RUN_TOKEN';
|
|
18
|
+
const RUN_TOKEN_PATTERN = /^(?!0{32}$)[0-9a-f]{32}$/u;
|
|
19
|
+
// The environment a managed terminal belongs to. A hook reports only to the environment that
|
|
20
|
+
// installed its Capsule, named by the owner record beside this module.
|
|
21
|
+
const ENVIRONMENT_NAME = 'ORCHBEE_ENVIRONMENT';
|
|
22
|
+
const RELEASE_ENVIRONMENT = 'orchbee';
|
|
23
|
+
const ENVIRONMENT_PATTERN = /^(?:orchbee|ob-(?:dev|test)-[0-9a-f]{16})$/u;
|
|
24
|
+
const OWNER_RECORD_URL = new URL('./environment.json', import.meta.url);
|
|
25
|
+
const MAX_OWNER_RECORD_BYTES = 256;
|
|
26
|
+
const WINDOWS_OBSERVATION_PIPE_PATTERN =
|
|
27
|
+
/^\\\\\.\\pipe\\orchbee-[a-f0-9]{32}-observation$(?![\s\S])/u;
|
|
28
|
+
const WINDOWS_CONSOLE_TARGET = String.raw`\\.\CONOUT$`;
|
|
29
|
+
// The one `agent-hook` generation this Capsule produces. The envelope, its observation, and the
|
|
30
|
+
// correlation hint all carry it, and only that generation's acknowledgement releases the hint.
|
|
31
|
+
const AGENT_HOOK_GENERATION = 1;
|
|
32
|
+
// A permanent channel name, not a generation: Host forwards the payload unread.
|
|
33
|
+
const PRIVATE_APC_PREFIX = 'orchbee.agent.v1;';
|
|
34
|
+
// Generation 1's acknowledgement, whose bytes never change.
|
|
35
|
+
const ACCEPTED_ENVELOPE_ACK = '{"accepted":true,"version":1}\n';
|
|
36
|
+
const MAX_ACK_BYTES = Buffer.byteLength(ACCEPTED_ENVELOPE_ACK);
|
|
37
|
+
const ADAPTER_IDS = new Set([
|
|
38
|
+
'claude-code',
|
|
39
|
+
'codex',
|
|
40
|
+
'grok',
|
|
41
|
+
'opencode',
|
|
42
|
+
'kilo',
|
|
43
|
+
'cursor',
|
|
44
|
+
'qodercli',
|
|
45
|
+
'antigravity-cli',
|
|
46
|
+
'mastracode',
|
|
47
|
+
'droid',
|
|
48
|
+
'kimi',
|
|
49
|
+
'letta',
|
|
50
|
+
'devin',
|
|
51
|
+
'hermes',
|
|
52
|
+
'pi',
|
|
53
|
+
'omp',
|
|
54
|
+
'gemini',
|
|
55
|
+
'github-copilot',
|
|
56
|
+
'cline',
|
|
57
|
+
'qwen-code',
|
|
58
|
+
]);
|
|
59
|
+
const ACTIVITIES = new Set([
|
|
60
|
+
'thinking',
|
|
61
|
+
'tool',
|
|
62
|
+
'editing',
|
|
63
|
+
'command',
|
|
64
|
+
'subagent',
|
|
65
|
+
'compacting',
|
|
66
|
+
'retrying',
|
|
67
|
+
'other',
|
|
68
|
+
]);
|
|
69
|
+
const EVENTS = new Set([
|
|
70
|
+
'session-identified',
|
|
71
|
+
'session-started',
|
|
72
|
+
'session-idle',
|
|
73
|
+
'turn-started',
|
|
74
|
+
'tool-started',
|
|
75
|
+
'tool-finished',
|
|
76
|
+
'tool-failed',
|
|
77
|
+
'question-requested',
|
|
78
|
+
'approval-requested',
|
|
79
|
+
'attention-resolved',
|
|
80
|
+
'compaction-started',
|
|
81
|
+
'compaction-finished',
|
|
82
|
+
'subagent-started',
|
|
83
|
+
'subagent-stopped',
|
|
84
|
+
'turn-stopped',
|
|
85
|
+
'turn-failed',
|
|
86
|
+
'turn-canceled',
|
|
87
|
+
'turn-interrupted',
|
|
88
|
+
'session-ended',
|
|
89
|
+
]);
|
|
90
|
+
|
|
91
|
+
export async function forwardSanitizedObservation(adapterId, value, options = {}) {
|
|
92
|
+
const observation = sanitizeNormalizedObservation(value);
|
|
93
|
+
if (!ADAPTER_IDS.has(adapterId)) return;
|
|
94
|
+
if (observation === null) return;
|
|
95
|
+
const environment = options.environment ?? process.env;
|
|
96
|
+
const owner = await (options.readOwner ?? readCapsuleOwner)();
|
|
97
|
+
if (!reportsTo(owner, environment)) return;
|
|
98
|
+
const allocateIdentity = options.allocateIdentity ?? allocateTransportIdentity;
|
|
99
|
+
const resolveTargets = options.resolveTargets ?? resolveObservationTargets;
|
|
100
|
+
const writeHint = options.writeHint ?? writeCorrelationHint;
|
|
101
|
+
const discoverSockets = options.discoverSockets ?? discoverObservationSockets;
|
|
102
|
+
const sendEnvelope = options.sendEnvelope ?? sendObservationEnvelope;
|
|
103
|
+
let sockets;
|
|
104
|
+
try {
|
|
105
|
+
sockets = await discoverSockets(environment);
|
|
106
|
+
} catch {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (sockets.length === 0) return;
|
|
110
|
+
let targets;
|
|
111
|
+
try {
|
|
112
|
+
targets = deduplicateTargets(await resolveTargets(environment)).slice(0, MAX_CORRELATIONS);
|
|
113
|
+
} catch {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (targets.length === 0) return;
|
|
117
|
+
|
|
118
|
+
const eventId = allocateIdentity();
|
|
119
|
+
const correlations = targets.map((target) => ({
|
|
120
|
+
correlationId: allocateIdentity(),
|
|
121
|
+
target,
|
|
122
|
+
}));
|
|
123
|
+
const runToken = runTokenFrom(environment);
|
|
124
|
+
const envelope = {
|
|
125
|
+
adapterId,
|
|
126
|
+
correlations: correlations.map(({ correlationId, target }) => ({
|
|
127
|
+
correlationId,
|
|
128
|
+
visibility: target.visibility,
|
|
129
|
+
})),
|
|
130
|
+
eventId,
|
|
131
|
+
observation,
|
|
132
|
+
...(runToken === null ? {} : { runToken }),
|
|
133
|
+
version: AGENT_HOOK_GENERATION,
|
|
134
|
+
};
|
|
135
|
+
const deliveries = await Promise.allSettled(
|
|
136
|
+
sockets.slice(0, MAX_SOCKET_CANDIDATES).map((socketPath) => sendEnvelope(socketPath, envelope)),
|
|
137
|
+
);
|
|
138
|
+
if (!deliveries.some((delivery) => delivery.status === 'fulfilled' && delivery.value === true)) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
await Promise.allSettled(
|
|
142
|
+
correlations.map(({ correlationId, target }) =>
|
|
143
|
+
writeHint(target.tty, {
|
|
144
|
+
correlationId,
|
|
145
|
+
eventId,
|
|
146
|
+
kind: 'correlation',
|
|
147
|
+
version: AGENT_HOOK_GENERATION,
|
|
148
|
+
}),
|
|
149
|
+
),
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export async function resolveObservationTargets(environment, options = {}) {
|
|
154
|
+
if ((options.platform ?? process.platform) === 'win32') {
|
|
155
|
+
return [{ tty: WINDOWS_CONSOLE_TARGET, visibility: 'unknown' }];
|
|
156
|
+
}
|
|
157
|
+
const execute = options.execute ?? executeBounded;
|
|
158
|
+
const targetGroups = [];
|
|
159
|
+
if (validEnvironmentValue(environment.TMUX_PANE)) {
|
|
160
|
+
targetGroups.push(resolveTmuxTargets(environment.TMUX_PANE, execute));
|
|
161
|
+
}
|
|
162
|
+
if (
|
|
163
|
+
validEnvironmentValue(environment.ZELLIJ_SESSION_NAME) &&
|
|
164
|
+
validEnvironmentValue(environment.ZELLIJ_PANE_ID)
|
|
165
|
+
) {
|
|
166
|
+
targetGroups.push(
|
|
167
|
+
resolveZellijTargets(environment.ZELLIJ_SESSION_NAME, environment.ZELLIJ_PANE_ID, execute),
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
const settled = await Promise.allSettled(targetGroups);
|
|
171
|
+
const muxTargets = settled.flatMap((result) =>
|
|
172
|
+
result.status === 'fulfilled' ? result.value : [],
|
|
173
|
+
);
|
|
174
|
+
if (muxTargets.length > 0) return deduplicateTargets(muxTargets);
|
|
175
|
+
const directTty = normalizeTty(environment.SSH_TTY) ?? '/dev/tty';
|
|
176
|
+
return [{ tty: directTty, visibility: 'unknown' }];
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function parseTmuxClientOutput(value, sessionName, paneId) {
|
|
180
|
+
const targets = [];
|
|
181
|
+
for (const line of value.split(/\r?\n/u)) {
|
|
182
|
+
const [tty, clientSession, activePane] = line.split('\t');
|
|
183
|
+
const normalizedTty = normalizeTty(tty);
|
|
184
|
+
if (normalizedTty === null || clientSession !== sessionName) continue;
|
|
185
|
+
targets.push({
|
|
186
|
+
tty: normalizedTty,
|
|
187
|
+
visibility: activePane === paneId ? 'unknown' : 'hidden',
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
return deduplicateTargets(targets);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function parseZellijClientOutput(value) {
|
|
194
|
+
const clients = [];
|
|
195
|
+
for (const line of value.split(/\r?\n/u)) {
|
|
196
|
+
const trimmed = line.trim();
|
|
197
|
+
if (trimmed === '' || trimmed.startsWith('CLIENT_ID ')) continue;
|
|
198
|
+
const [clientId, paneId] = trimmed.split(/\s+/u);
|
|
199
|
+
if (clientId === undefined || paneId === undefined) continue;
|
|
200
|
+
clients.push({ clientId, paneId });
|
|
201
|
+
}
|
|
202
|
+
return clients;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function parseZellijClientProcesses(value, userId, sessionName) {
|
|
206
|
+
const targets = [];
|
|
207
|
+
for (const line of value.split(/\r?\n/u)) {
|
|
208
|
+
const match = /^\s*(\d+)\s+(\d+)\s+(\S+)\s+(.+)$/u.exec(line);
|
|
209
|
+
if (match === null) continue;
|
|
210
|
+
const [, uidText, , ttyText, command] = match;
|
|
211
|
+
if (
|
|
212
|
+
Number(uidText) !== userId ||
|
|
213
|
+
command === undefined ||
|
|
214
|
+
!zellijClientCommandForSession(command, sessionName)
|
|
215
|
+
) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
const tty = normalizeTty(ttyText);
|
|
219
|
+
if (tty !== null) targets.push(tty);
|
|
220
|
+
}
|
|
221
|
+
return [...new Set(targets)];
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export async function discoverObservationSockets(environment, options = {}) {
|
|
225
|
+
const localSocket = environment[OBSERVATION_SOCKET_ENVIRONMENT_NAME];
|
|
226
|
+
if ((options.platform ?? process.platform) === 'win32') {
|
|
227
|
+
return typeof localSocket === 'string' && WINDOWS_OBSERVATION_PIPE_PATTERN.test(localSocket)
|
|
228
|
+
? [localSocket]
|
|
229
|
+
: [];
|
|
230
|
+
}
|
|
231
|
+
if (typeof localSocket === 'string') {
|
|
232
|
+
if (!path.isAbsolute(localSocket)) return [];
|
|
233
|
+
return (await isOwnedSocket(localSocket)) ? [localSocket] : [];
|
|
234
|
+
}
|
|
235
|
+
// Missing local routing is not proof of SSH and must not couple another terminal to Orchbee.
|
|
236
|
+
if (!isRemoteSshEnvironment(environment)) return [];
|
|
237
|
+
const candidates = [];
|
|
238
|
+
let entries = [];
|
|
239
|
+
try {
|
|
240
|
+
entries = await readdir('/tmp');
|
|
241
|
+
} catch {
|
|
242
|
+
// A missing shared temporary directory simply disables remote discovery.
|
|
243
|
+
}
|
|
244
|
+
for (const entry of entries) {
|
|
245
|
+
if (PRIVATE_SOCKET_NAME_PATTERN.test(entry)) candidates.push(path.join('/tmp', entry));
|
|
246
|
+
}
|
|
247
|
+
const admitted = [];
|
|
248
|
+
for (const candidate of [...new Set(candidates)].slice(0, MAX_SOCKET_CANDIDATES * 2)) {
|
|
249
|
+
if (await isOwnedSocket(candidate)) admitted.push(candidate);
|
|
250
|
+
}
|
|
251
|
+
return admitted.slice(0, MAX_SOCKET_CANDIDATES);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export async function writeCorrelationHint(tty, hint) {
|
|
255
|
+
if (process.platform === 'win32') {
|
|
256
|
+
if (tty !== WINDOWS_CONSOLE_TARGET) return;
|
|
257
|
+
const handle = await open(WINDOWS_CONSOLE_TARGET, 'r+');
|
|
258
|
+
try {
|
|
259
|
+
if (!(await handle.stat()).isCharacterDevice()) return;
|
|
260
|
+
const payload = Buffer.from(JSON.stringify(hint), 'utf8').toString('base64url');
|
|
261
|
+
await handle.writeFile(`\u001B_${PRIVATE_APC_PREFIX}${payload}\u001B\\`);
|
|
262
|
+
} finally {
|
|
263
|
+
await handle.close();
|
|
264
|
+
}
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
if (normalizeTty(tty) !== tty) return;
|
|
268
|
+
const payload = Buffer.from(JSON.stringify(hint), 'utf8').toString('base64url');
|
|
269
|
+
const handle = await open(
|
|
270
|
+
tty,
|
|
271
|
+
fileSystemConstants.O_WRONLY |
|
|
272
|
+
fileSystemConstants.O_NOCTTY |
|
|
273
|
+
fileSystemConstants.O_NOFOLLOW |
|
|
274
|
+
fileSystemConstants.O_NONBLOCK,
|
|
275
|
+
);
|
|
276
|
+
try {
|
|
277
|
+
const metadata = await handle.stat();
|
|
278
|
+
const userId = process.getuid?.();
|
|
279
|
+
if (
|
|
280
|
+
!metadata.isCharacterDevice() ||
|
|
281
|
+
(tty !== '/dev/tty' && userId !== undefined && metadata.uid !== userId)
|
|
282
|
+
) {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
await handle.write(`\u001B_${PRIVATE_APC_PREFIX}${payload}\u001B\\`);
|
|
286
|
+
} finally {
|
|
287
|
+
await handle.close();
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export async function sendObservationEnvelope(socketPath, envelope) {
|
|
292
|
+
return new Promise((resolve) => {
|
|
293
|
+
const socket = createConnection({ path: socketPath });
|
|
294
|
+
const chunks = [];
|
|
295
|
+
let byteLength = 0;
|
|
296
|
+
let settled = false;
|
|
297
|
+
const finish = (accepted) => {
|
|
298
|
+
if (settled) return;
|
|
299
|
+
settled = true;
|
|
300
|
+
socket.destroy();
|
|
301
|
+
resolve(accepted);
|
|
302
|
+
};
|
|
303
|
+
socket.setTimeout(SOCKET_TIMEOUT_MILLISECONDS, () => finish(false));
|
|
304
|
+
socket.once('connect', () => socket.end(JSON.stringify(envelope)));
|
|
305
|
+
socket.on('data', (chunk) => {
|
|
306
|
+
byteLength += chunk.byteLength;
|
|
307
|
+
if (byteLength > MAX_ACK_BYTES) {
|
|
308
|
+
finish(false);
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
chunks.push(chunk);
|
|
312
|
+
});
|
|
313
|
+
socket.once('end', () => {
|
|
314
|
+
const response = Buffer.concat(chunks, byteLength).toString('utf8');
|
|
315
|
+
finish(response === ACCEPTED_ENVELOPE_ACK);
|
|
316
|
+
});
|
|
317
|
+
socket.once('close', () => finish(false));
|
|
318
|
+
socket.once('error', async (error) => {
|
|
319
|
+
if (process.platform !== 'win32' && hasErrorCode(error, 'ECONNREFUSED')) {
|
|
320
|
+
await removeStaleForwardSocket(socketPath);
|
|
321
|
+
}
|
|
322
|
+
finish(false);
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
async function resolveTmuxTargets(paneId, execute) {
|
|
328
|
+
const sessionName = (
|
|
329
|
+
await execute('tmux', ['display-message', '-p', '-t', paneId, '#{session_name}'])
|
|
330
|
+
).trim();
|
|
331
|
+
if (sessionName === '') return [];
|
|
332
|
+
const clients = await execute('tmux', [
|
|
333
|
+
'list-clients',
|
|
334
|
+
'-F',
|
|
335
|
+
'#{client_tty}\t#{client_session}\t#{pane_id}',
|
|
336
|
+
]);
|
|
337
|
+
return parseTmuxClientOutput(clients, sessionName, paneId);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
async function resolveZellijTargets(sessionName, paneId, execute) {
|
|
341
|
+
const [clientOutput, processOutput] = await Promise.all([
|
|
342
|
+
execute('zellij', ['--session', sessionName, 'action', 'list-clients']),
|
|
343
|
+
execute('ps', ['-axo', 'uid=,pid=,tty=,command=']),
|
|
344
|
+
]);
|
|
345
|
+
const clients = parseZellijClientOutput(clientOutput);
|
|
346
|
+
const userId = process.getuid?.();
|
|
347
|
+
if (userId === undefined || clients.length === 0) return [];
|
|
348
|
+
const ttys = parseZellijClientProcesses(processOutput, userId, sessionName);
|
|
349
|
+
const visibleCount = clients.filter((client) => client.paneId === paneId).length;
|
|
350
|
+
const visibility = visibleCount === 0 ? 'hidden' : 'unknown';
|
|
351
|
+
return ttys.map((tty) => ({ tty, visibility }));
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
async function executeBounded(file, arguments_) {
|
|
355
|
+
const result = await executeFile(file, arguments_, {
|
|
356
|
+
encoding: 'utf8',
|
|
357
|
+
maxBuffer: MAX_COMMAND_OUTPUT_BYTES,
|
|
358
|
+
timeout: SOCKET_TIMEOUT_MILLISECONDS,
|
|
359
|
+
});
|
|
360
|
+
return result.stdout;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
async function removeStaleForwardSocket(socketPath) {
|
|
364
|
+
if (
|
|
365
|
+
path.dirname(socketPath) !== '/tmp' ||
|
|
366
|
+
!PRIVATE_SOCKET_NAME_PATTERN.test(path.basename(socketPath))
|
|
367
|
+
) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
try {
|
|
371
|
+
const metadata = await lstat(socketPath);
|
|
372
|
+
const userId = process.getuid?.();
|
|
373
|
+
if (metadata.isSocket() && (userId === undefined || metadata.uid === userId)) {
|
|
374
|
+
await unlink(socketPath);
|
|
375
|
+
}
|
|
376
|
+
} catch {
|
|
377
|
+
// Another hook or OpenSSH may already have removed the stale socket.
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
async function isOwnedSocket(socketPath) {
|
|
382
|
+
try {
|
|
383
|
+
const metadata = await lstat(socketPath);
|
|
384
|
+
const userId = process.getuid?.();
|
|
385
|
+
return metadata.isSocket() && (userId === undefined || metadata.uid === userId);
|
|
386
|
+
} catch {
|
|
387
|
+
// Candidate discovery is intentionally best effort.
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function sanitizeNormalizedObservation(value) {
|
|
393
|
+
if (!isRecord(value)) return null;
|
|
394
|
+
const sessionKey = identity(value.sessionKey);
|
|
395
|
+
const event = typeof value.event === 'string' && EVENTS.has(value.event) ? value.event : null;
|
|
396
|
+
const activity =
|
|
397
|
+
value.activity === null || ACTIVITIES.has(value.activity) ? value.activity : undefined;
|
|
398
|
+
const agentKey = nullableIdentity(value.agentKey);
|
|
399
|
+
const operationKey = nullableIdentity(value.operationKey);
|
|
400
|
+
const turnKey = nullableIdentity(value.turnKey);
|
|
401
|
+
if (
|
|
402
|
+
sessionKey === null ||
|
|
403
|
+
event === null ||
|
|
404
|
+
activity === undefined ||
|
|
405
|
+
agentKey === undefined ||
|
|
406
|
+
operationKey === undefined ||
|
|
407
|
+
turnKey === undefined ||
|
|
408
|
+
value.version !== 1
|
|
409
|
+
) {
|
|
410
|
+
return null;
|
|
411
|
+
}
|
|
412
|
+
if (
|
|
413
|
+
(event === 'question-requested' ||
|
|
414
|
+
event === 'approval-requested' ||
|
|
415
|
+
event === 'attention-resolved') &&
|
|
416
|
+
operationKey === null
|
|
417
|
+
) {
|
|
418
|
+
return null;
|
|
419
|
+
}
|
|
420
|
+
if ((event === 'subagent-started' || event === 'subagent-stopped') && agentKey === null) {
|
|
421
|
+
return null;
|
|
422
|
+
}
|
|
423
|
+
return {
|
|
424
|
+
activity,
|
|
425
|
+
agentKey,
|
|
426
|
+
event,
|
|
427
|
+
operationKey,
|
|
428
|
+
sessionKey,
|
|
429
|
+
turnKey,
|
|
430
|
+
version: AGENT_HOOK_GENERATION,
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// The Agent inherits the Server-minted run token from its managed launch command. A missing or
|
|
435
|
+
// malformed value is omitted so the observation still reaches the newest-run fallback.
|
|
436
|
+
/**
|
|
437
|
+
* The environment that installed this Capsule: its owner record's, or release when the Capsule has
|
|
438
|
+
* none, as every Capsule installed before each environment kept its own. `null` when a record
|
|
439
|
+
* exists but cannot be read, so the hook reports nowhere.
|
|
440
|
+
*/
|
|
441
|
+
export async function readCapsuleOwner(recordUrl = OWNER_RECORD_URL) {
|
|
442
|
+
try {
|
|
443
|
+
const metadata = await lstat(recordUrl);
|
|
444
|
+
if (!metadata.isFile() || metadata.size > MAX_OWNER_RECORD_BYTES) return null;
|
|
445
|
+
const record = JSON.parse(await readFile(recordUrl, 'utf8'));
|
|
446
|
+
if (
|
|
447
|
+
record === null ||
|
|
448
|
+
typeof record !== 'object' ||
|
|
449
|
+
Array.isArray(record) ||
|
|
450
|
+
Object.keys(record).length !== 2 ||
|
|
451
|
+
record.version !== 1 ||
|
|
452
|
+
typeof record.environment !== 'string' ||
|
|
453
|
+
!ENVIRONMENT_PATTERN.test(record.environment)
|
|
454
|
+
) {
|
|
455
|
+
return null;
|
|
456
|
+
}
|
|
457
|
+
return record.environment;
|
|
458
|
+
} catch (error) {
|
|
459
|
+
return error?.code === 'ENOENT' ? RELEASE_ENVIRONMENT : null;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Whether a hook running in `environment` reports to `owner`: the terminal names that environment,
|
|
465
|
+
* or names none and the owner is release. Any other hook stays silent and succeeds.
|
|
466
|
+
*/
|
|
467
|
+
export function reportsTo(owner, environment) {
|
|
468
|
+
if (owner === null) return false;
|
|
469
|
+
const tag = environment[ENVIRONMENT_NAME];
|
|
470
|
+
return tag === undefined ? owner === RELEASE_ENVIRONMENT : tag === owner;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
export function runTokenFrom(environment) {
|
|
474
|
+
const value = environment[RUN_TOKEN_ENVIRONMENT_NAME];
|
|
475
|
+
return typeof value === 'string' && RUN_TOKEN_PATTERN.test(value) ? value : null;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
function allocateTransportIdentity() {
|
|
479
|
+
return randomBytes(18).toString('base64url');
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function identity(value) {
|
|
483
|
+
return typeof value === 'string' && IDENTITY_PATTERN.test(value) ? value : null;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function nullableIdentity(value) {
|
|
487
|
+
if (value === null) return null;
|
|
488
|
+
const result = identity(value);
|
|
489
|
+
return result === null ? undefined : result;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function validEnvironmentValue(value) {
|
|
493
|
+
return typeof value === 'string' && value.length > 0 && value.length <= 256;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function isRemoteSshEnvironment(environment) {
|
|
497
|
+
return (
|
|
498
|
+
validEnvironmentValue(environment.SSH_CONNECTION) ||
|
|
499
|
+
validEnvironmentValue(environment.SSH_CLIENT) ||
|
|
500
|
+
validEnvironmentValue(environment.SSH_TTY)
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function normalizeTty(value) {
|
|
505
|
+
if (typeof value !== 'string' || value === '' || value.includes('\0')) return null;
|
|
506
|
+
const candidate = value.startsWith('/dev/') ? value : `/dev/${value}`;
|
|
507
|
+
if (!/^\/dev\/[A-Za-z0-9._/-]{1,240}$/u.test(candidate)) return null;
|
|
508
|
+
return path.posix.normalize(candidate) === candidate ? candidate : null;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
function deduplicateTargets(targets) {
|
|
512
|
+
const byTty = new Map();
|
|
513
|
+
for (const target of targets) {
|
|
514
|
+
const tty =
|
|
515
|
+
target.tty === WINDOWS_CONSOLE_TARGET ? WINDOWS_CONSOLE_TARGET : normalizeTty(target.tty);
|
|
516
|
+
if (tty === null) continue;
|
|
517
|
+
const existing = byTty.get(tty);
|
|
518
|
+
if (existing === undefined) byTty.set(tty, { tty, visibility: target.visibility });
|
|
519
|
+
else existing.visibility = combineVisibility(existing.visibility, target.visibility);
|
|
520
|
+
}
|
|
521
|
+
return [...byTty.values()];
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function combineVisibility(left, right) {
|
|
525
|
+
if (left === 'hidden' || right === 'hidden') return 'hidden';
|
|
526
|
+
return left === 'visible' && right === 'visible' ? 'visible' : 'unknown';
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function zellijClientCommandForSession(command, sessionName) {
|
|
530
|
+
const arguments_ = command.trim().split(/\s+/u);
|
|
531
|
+
const executable = arguments_[0];
|
|
532
|
+
if (executable === undefined || path.basename(executable) !== 'zellij') return false;
|
|
533
|
+
for (let index = 1; index < arguments_.length; index += 1) {
|
|
534
|
+
const argument = arguments_[index];
|
|
535
|
+
const next = arguments_[index + 1];
|
|
536
|
+
if ((argument === '--session' || argument === '-s') && next === sessionName) return true;
|
|
537
|
+
if (argument === `--session=${sessionName}`) return true;
|
|
538
|
+
if ((argument === 'attach' || argument === 'a') && next === sessionName) return true;
|
|
539
|
+
}
|
|
540
|
+
return false;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
function hasErrorCode(error, code) {
|
|
544
|
+
return typeof error === 'object' && error !== null && error.code === code;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function isRecord(value) {
|
|
548
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
549
|
+
}
|