@vite-hub/shell 0.0.3 → 0.1.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/LICENSE +201 -0
- package/README.md +85 -35
- package/dist/analyze-BXwz20IH.js +247 -0
- package/dist/index.d.ts +4 -5
- package/dist/index.js +2 -2
- package/dist/{just-bash-D84udCFy.d.ts → just-bash-DAyZQWLK.d.ts} +2 -2
- package/dist/providers/just-bash.d.ts +1 -1
- package/dist/providers/just-bash.js +132 -59
- package/dist/session-CIy2VLp9.js +245 -0
- package/dist/{types-CNRdehmi.d.ts → types-2uHfTVHi.d.ts} +23 -3
- package/dist/workspace.d.ts +14 -11
- package/dist/workspace.js +237 -209
- package/package.json +29 -14
- package/dist/analyze-Bu5Ueqph.js +0 -126
- package/dist/parse-D9VT4P6N.js +0 -41
- package/dist/providers/cloudflare.d.ts +0 -28
- package/dist/providers/cloudflare.js +0 -44
- package/dist/session-0OrrlmE9.js +0 -178
|
@@ -1,61 +1,84 @@
|
|
|
1
|
-
import { t as analyzeShellCommand } from "../analyze-
|
|
2
|
-
import { t as parseShellCommand } from "../parse-D9VT4P6N.js";
|
|
1
|
+
import { i as shellErrorDiagnostics, n as parseShellCommand, t as analyzeShellCommand } from "../analyze-BXwz20IH.js";
|
|
3
2
|
//#region src/providers/just-bash.ts
|
|
4
3
|
function createJustBashProvider(options) {
|
|
4
|
+
const commands = options.commands?.slice();
|
|
5
|
+
const networkGrants = options.networkGrants;
|
|
6
|
+
const boundary = {
|
|
7
|
+
cwd: true,
|
|
8
|
+
env: true,
|
|
9
|
+
filesystem: {
|
|
10
|
+
mountPoint: "/workspace",
|
|
11
|
+
writable: options.fs.writeFs
|
|
12
|
+
},
|
|
13
|
+
network: Boolean(networkGrants) && (!commands || commands.includes("curl")),
|
|
14
|
+
processes: {
|
|
15
|
+
background: false,
|
|
16
|
+
interactive: false
|
|
17
|
+
},
|
|
18
|
+
streaming: false,
|
|
19
|
+
timeout: {
|
|
20
|
+
enforcedBy: "provider",
|
|
21
|
+
supported: true
|
|
22
|
+
},
|
|
23
|
+
execution: {
|
|
24
|
+
isolation: "in_process",
|
|
25
|
+
model: "virtual"
|
|
26
|
+
},
|
|
27
|
+
resources: { output: {
|
|
28
|
+
enforcedBy: "unsupported",
|
|
29
|
+
unit: "characters"
|
|
30
|
+
} }
|
|
31
|
+
};
|
|
5
32
|
return {
|
|
6
33
|
analyze: analyzeShellCommand,
|
|
7
|
-
boundary
|
|
8
|
-
cwd: true,
|
|
9
|
-
env: true,
|
|
10
|
-
filesystem: {
|
|
11
|
-
mountPoint: "/workspace",
|
|
12
|
-
writable: options.fs.writeFs
|
|
13
|
-
},
|
|
14
|
-
network: Boolean(options.networkGrants),
|
|
15
|
-
processes: {
|
|
16
|
-
background: false,
|
|
17
|
-
interactive: false
|
|
18
|
-
},
|
|
19
|
-
streaming: false,
|
|
20
|
-
timeout: {
|
|
21
|
-
enforcedBy: "provider",
|
|
22
|
-
supported: true
|
|
23
|
-
}
|
|
24
|
-
},
|
|
34
|
+
boundary,
|
|
25
35
|
async exec(command, execOptions = {}) {
|
|
26
|
-
const
|
|
36
|
+
const cwd = execOptions.cwd ?? options.cwd ?? "/workspace";
|
|
37
|
+
const result = await withProviderTimeout(command, {
|
|
38
|
+
...execOptions,
|
|
39
|
+
cwd
|
|
40
|
+
}, async (runOptions) => {
|
|
27
41
|
const curlResult = await runControlledCurlCommand(command, {
|
|
28
|
-
|
|
29
|
-
|
|
42
|
+
commands,
|
|
43
|
+
cwd,
|
|
44
|
+
networkGrants,
|
|
45
|
+
maxOutputLength: runOptions.maxOutputLength,
|
|
46
|
+
signal: runOptions.signal
|
|
30
47
|
});
|
|
31
48
|
if (curlResult) return curlResult;
|
|
32
49
|
const { Bash } = await import("just-bash/browser");
|
|
33
|
-
|
|
34
|
-
commands
|
|
35
|
-
cwd
|
|
50
|
+
return await new Bash({
|
|
51
|
+
commands,
|
|
52
|
+
cwd,
|
|
36
53
|
fs: options.fs
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
return await bash.exec(command, {
|
|
40
|
-
cwd: execOptions.cwd,
|
|
54
|
+
}).exec(command, {
|
|
55
|
+
cwd,
|
|
41
56
|
env: execOptions.env,
|
|
42
|
-
signal
|
|
43
|
-
}).then((result) =>
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
57
|
+
signal: runOptions.signal
|
|
58
|
+
}).then((result) => {
|
|
59
|
+
const stdout = boundOutput(result.stdout, runOptions.maxOutputLength);
|
|
60
|
+
const stderr = boundOutput(result.stderr, runOptions.maxOutputLength);
|
|
61
|
+
return {
|
|
62
|
+
command,
|
|
63
|
+
cwd,
|
|
64
|
+
event: "command_finished",
|
|
65
|
+
exitCode: result.exitCode,
|
|
66
|
+
maxOutputLength: runOptions.maxOutputLength,
|
|
67
|
+
outputTruncated: stdout.truncated || stderr.truncated,
|
|
68
|
+
stderr: stderr.content,
|
|
69
|
+
stdout: stdout.content
|
|
70
|
+
};
|
|
71
|
+
});
|
|
51
72
|
});
|
|
52
73
|
execOptions.onStdout?.(result.stdout);
|
|
53
74
|
execOptions.onStderr?.(result.stderr);
|
|
54
75
|
return {
|
|
55
76
|
command: result.command ?? command,
|
|
56
|
-
cwd: result.cwd ??
|
|
77
|
+
cwd: result.cwd ?? cwd,
|
|
57
78
|
event: result.timedOut ? "command_timed_out" : result.event,
|
|
58
79
|
exitCode: result.exitCode,
|
|
80
|
+
maxOutputLength: result.maxOutputLength,
|
|
81
|
+
outputTruncated: result.outputTruncated,
|
|
59
82
|
stderr: result.stderr,
|
|
60
83
|
stdout: result.stdout,
|
|
61
84
|
timedOut: result.timedOut
|
|
@@ -65,24 +88,37 @@ function createJustBashProvider(options) {
|
|
|
65
88
|
}
|
|
66
89
|
async function runControlledCurlCommand(command, options) {
|
|
67
90
|
if (!mentionsCurlCommand(command)) return void 0;
|
|
91
|
+
options.signal?.throwIfAborted();
|
|
92
|
+
if (options.commands && !options.commands.includes("curl")) return policyDeniedCurl(command, options.cwd, "curl is not in the permitted commands for this shell.");
|
|
68
93
|
const [parseError, parsed] = parseControlledCurlCommand(command);
|
|
69
94
|
if (parseError) return policyDeniedCurl(command, options.cwd, parseError.message);
|
|
70
95
|
if (!options.networkGrants) return policyDeniedCurl(command, options.cwd, "No API-backed Source request descriptors are visible in this workspace.");
|
|
71
96
|
try {
|
|
72
|
-
const
|
|
97
|
+
const request = {
|
|
73
98
|
body: parsed.body,
|
|
74
99
|
method: parsed.method,
|
|
100
|
+
signal: options.signal,
|
|
75
101
|
url: parsed.url
|
|
102
|
+
};
|
|
103
|
+
if (options.signal) Object.defineProperty(request, "signal", {
|
|
104
|
+
configurable: true,
|
|
105
|
+
enumerable: false,
|
|
106
|
+
value: options.signal
|
|
76
107
|
});
|
|
108
|
+
const result = await options.networkGrants.executeSourceRequest(request);
|
|
109
|
+
const bounded = boundOutput(typeof result.content === "string" ? result.content : new TextDecoder().decode(result.content), options.maxOutputLength);
|
|
77
110
|
return {
|
|
78
111
|
command,
|
|
79
112
|
cwd: options.cwd,
|
|
80
113
|
event: "command_finished",
|
|
81
114
|
exitCode: 0,
|
|
82
115
|
stderr: "",
|
|
83
|
-
|
|
116
|
+
maxOutputLength: options.maxOutputLength,
|
|
117
|
+
outputTruncated: bounded.truncated,
|
|
118
|
+
stdout: bounded.content
|
|
84
119
|
};
|
|
85
120
|
} catch (error) {
|
|
121
|
+
if (options.signal?.aborted) throw options.signal.reason;
|
|
86
122
|
const message = error instanceof Error ? error.message : String(error);
|
|
87
123
|
const httpFailure = message.includes("HTTP request failed");
|
|
88
124
|
return {
|
|
@@ -96,7 +132,7 @@ async function runControlledCurlCommand(command, options) {
|
|
|
96
132
|
}
|
|
97
133
|
}
|
|
98
134
|
function parseFailure(message) {
|
|
99
|
-
return [
|
|
135
|
+
return [shellErrorDiagnostics.SHELL_R0004({ message }), void 0];
|
|
100
136
|
}
|
|
101
137
|
function parseControlledCurlCommand(command) {
|
|
102
138
|
if (hasUnsupportedCurlShellSyntax(command)) return parseFailure("Controlled curl must be a single command without pipes, redirects, chaining, command substitution, or heredocs.");
|
|
@@ -104,7 +140,7 @@ function parseControlledCurlCommand(command) {
|
|
|
104
140
|
try {
|
|
105
141
|
words = parseShellCommand(command);
|
|
106
142
|
} catch (error) {
|
|
107
|
-
return [error instanceof Error ? error :
|
|
143
|
+
return [error instanceof Error ? error : shellErrorDiagnostics.SHELL_R0005({ message: String(error) }), void 0];
|
|
108
144
|
}
|
|
109
145
|
if (words[0] !== "curl") return parseFailure("Controlled curl command must start with curl.");
|
|
110
146
|
let body;
|
|
@@ -154,7 +190,7 @@ function parseControlledCurlCommand(command) {
|
|
|
154
190
|
}
|
|
155
191
|
if (word === "-d" || word === "--data" || word === "--data-raw" || word === "--data-binary") {
|
|
156
192
|
const value = words[++index];
|
|
157
|
-
if (
|
|
193
|
+
if (value === void 0) return parseFailure(`${word} requires a body.`);
|
|
158
194
|
body = parseCurlDataBody(value);
|
|
159
195
|
method ??= "POST";
|
|
160
196
|
continue;
|
|
@@ -174,7 +210,7 @@ function parseControlledCurlCommand(command) {
|
|
|
174
210
|
url = setCurlUrl(url, word);
|
|
175
211
|
}
|
|
176
212
|
} catch (error) {
|
|
177
|
-
return [error instanceof Error ? error :
|
|
213
|
+
return [error instanceof Error ? error : shellErrorDiagnostics.SHELL_R0006({ message: String(error) }), void 0];
|
|
178
214
|
}
|
|
179
215
|
if (!url) return parseFailure("Controlled curl requires a URL.");
|
|
180
216
|
try {
|
|
@@ -241,7 +277,7 @@ function parseJsonCurlBody(value) {
|
|
|
241
277
|
}
|
|
242
278
|
}
|
|
243
279
|
function setCurlUrl(current, next) {
|
|
244
|
-
if (current) throw
|
|
280
|
+
if (current) throw shellErrorDiagnostics.SHELL_R0007({ message: "Controlled curl accepts exactly one URL." });
|
|
245
281
|
return next;
|
|
246
282
|
}
|
|
247
283
|
function policyDeniedCurl(command, cwd, message) {
|
|
@@ -255,23 +291,60 @@ function policyDeniedCurl(command, cwd, message) {
|
|
|
255
291
|
};
|
|
256
292
|
}
|
|
257
293
|
async function withProviderTimeout(command, options, run) {
|
|
258
|
-
|
|
294
|
+
options.signal?.throwIfAborted();
|
|
295
|
+
const controller = new AbortController();
|
|
296
|
+
const abortListener = options.signal ? () => controller.abort(options.signal?.reason) : void 0;
|
|
297
|
+
if (abortListener) options.signal?.addEventListener("abort", abortListener, { once: true });
|
|
298
|
+
const runOptions = options.signal || typeof options.timeout === "number" ? {
|
|
299
|
+
...options,
|
|
300
|
+
signal: controller.signal
|
|
301
|
+
} : options;
|
|
259
302
|
let timeout;
|
|
303
|
+
let onAbort;
|
|
304
|
+
const aborted = options.signal ? new Promise((_resolve, reject) => {
|
|
305
|
+
const handleAbort = () => reject(options.signal?.reason);
|
|
306
|
+
onAbort = handleAbort;
|
|
307
|
+
options.signal?.addEventListener("abort", handleAbort, { once: true });
|
|
308
|
+
if (options.signal?.aborted) handleAbort();
|
|
309
|
+
}) : void 0;
|
|
260
310
|
try {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
311
|
+
const running = run(runOptions);
|
|
312
|
+
if (typeof options.timeout !== "number" && !aborted) return await running;
|
|
313
|
+
const races = [running];
|
|
314
|
+
if (aborted) races.push(aborted);
|
|
315
|
+
if (typeof options.timeout === "number") races.push(new Promise((resolve) => {
|
|
316
|
+
timeout = setTimeout(() => {
|
|
317
|
+
resolve({
|
|
318
|
+
command,
|
|
319
|
+
cwd: options.cwd,
|
|
320
|
+
event: "command_timed_out",
|
|
321
|
+
exitCode: null,
|
|
322
|
+
maxOutputLength: options.maxOutputLength,
|
|
323
|
+
outputTruncated: false,
|
|
324
|
+
stderr: `[vitehub] Workspace shell command timed out after ${options.timeout}ms.`,
|
|
325
|
+
stdout: "",
|
|
326
|
+
timedOut: true
|
|
327
|
+
});
|
|
328
|
+
controller.abort(/* @__PURE__ */ new Error("Shell command timed out."));
|
|
329
|
+
}, options.timeout);
|
|
330
|
+
}));
|
|
331
|
+
return await Promise.race(races);
|
|
272
332
|
} finally {
|
|
273
333
|
if (timeout) clearTimeout(timeout);
|
|
334
|
+
if (onAbort) options.signal?.removeEventListener("abort", onAbort);
|
|
335
|
+
if (abortListener) options.signal?.removeEventListener("abort", abortListener);
|
|
274
336
|
}
|
|
275
337
|
}
|
|
338
|
+
function boundOutput(content, maxLength) {
|
|
339
|
+
const limit = maxLength === void 0 ? void 0 : maxLength === Number.POSITIVE_INFINITY ? void 0 : Number.isFinite(maxLength) ? Math.max(0, Math.floor(maxLength)) : 0;
|
|
340
|
+
if (limit === void 0 || content.length <= limit) return {
|
|
341
|
+
content,
|
|
342
|
+
truncated: false
|
|
343
|
+
};
|
|
344
|
+
return {
|
|
345
|
+
content: `${content.slice(0, limit)}\n[output truncated to ${limit} characters]\n`,
|
|
346
|
+
truncated: true
|
|
347
|
+
};
|
|
348
|
+
}
|
|
276
349
|
//#endregion
|
|
277
350
|
export { createJustBashProvider };
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { i as shellErrorDiagnostics, t as analyzeShellCommand } from "./analyze-BXwz20IH.js";
|
|
2
|
+
//#region src/runtime/session.ts
|
|
3
|
+
function applyOutputLimit(result, maxLength) {
|
|
4
|
+
const limit = normalizeOutputLength(maxLength);
|
|
5
|
+
if (limit === void 0) return result;
|
|
6
|
+
const next = {
|
|
7
|
+
command: result.command,
|
|
8
|
+
cwd: result.cwd,
|
|
9
|
+
durationMs: result.durationMs,
|
|
10
|
+
event: result.event,
|
|
11
|
+
exitCode: result.exitCode,
|
|
12
|
+
maxOutputLength: limit,
|
|
13
|
+
outputTruncated: result.outputTruncated,
|
|
14
|
+
stderr: result.stderr,
|
|
15
|
+
stdout: result.stdout,
|
|
16
|
+
timedOut: result.timedOut,
|
|
17
|
+
workspaceGuardrail: result.workspaceGuardrail
|
|
18
|
+
};
|
|
19
|
+
let truncated = false;
|
|
20
|
+
if (next.stdout.length > limit) {
|
|
21
|
+
next.stdout = `${next.stdout.slice(0, limit)}\n[output truncated to ${limit} characters]\n`;
|
|
22
|
+
truncated = true;
|
|
23
|
+
}
|
|
24
|
+
if (next.stderr.length > limit) {
|
|
25
|
+
next.stderr = `${next.stderr.slice(0, limit)}\n[output truncated to ${limit} characters]\n`;
|
|
26
|
+
truncated = true;
|
|
27
|
+
}
|
|
28
|
+
return truncated ? {
|
|
29
|
+
...next,
|
|
30
|
+
outputTruncated: true
|
|
31
|
+
} : next;
|
|
32
|
+
}
|
|
33
|
+
function normalizeOutputLength(value) {
|
|
34
|
+
if (value === void 0) return void 0;
|
|
35
|
+
if (value === Number.POSITIVE_INFINITY) return void 0;
|
|
36
|
+
return Number.isFinite(value) ? Math.max(0, Math.floor(value)) : 0;
|
|
37
|
+
}
|
|
38
|
+
function limitOutputOptions(options, policy) {
|
|
39
|
+
const requestedOutputLength = normalizeOutputLength(options.maxOutputLength);
|
|
40
|
+
const policyOutputLength = normalizeOutputLength(policy.maxOutputLength);
|
|
41
|
+
const maxOutputLength = requestedOutputLength === void 0 ? policyOutputLength : policyOutputLength === void 0 ? requestedOutputLength : Math.min(requestedOutputLength, policyOutputLength);
|
|
42
|
+
let stdoutRemaining = maxOutputLength;
|
|
43
|
+
let stderrRemaining = maxOutputLength;
|
|
44
|
+
const limitedCallback = (callback, remaining, consume) => callback && ((data) => {
|
|
45
|
+
const limit = remaining();
|
|
46
|
+
if (limit === void 0) return callback(data);
|
|
47
|
+
if (limit <= 0) return;
|
|
48
|
+
const chunk = data.slice(0, limit);
|
|
49
|
+
consume(chunk.length);
|
|
50
|
+
if (chunk) callback(chunk);
|
|
51
|
+
});
|
|
52
|
+
return {
|
|
53
|
+
...options,
|
|
54
|
+
maxOutputLength,
|
|
55
|
+
onStdout: limitedCallback(options.onStdout, () => stdoutRemaining, (value) => {
|
|
56
|
+
stdoutRemaining = Math.max(0, (stdoutRemaining ?? 0) - value);
|
|
57
|
+
}),
|
|
58
|
+
onStderr: limitedCallback(options.onStderr, () => stderrRemaining, (value) => {
|
|
59
|
+
stderrRemaining = Math.max(0, (stderrRemaining ?? 0) - value);
|
|
60
|
+
})
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function createPolicyObservation(command, cwd, message) {
|
|
64
|
+
return {
|
|
65
|
+
command,
|
|
66
|
+
cwd,
|
|
67
|
+
event: "policy_denied",
|
|
68
|
+
exitCode: 126,
|
|
69
|
+
stderr: message.endsWith("\n") ? message : `${message}\n`,
|
|
70
|
+
stdout: ""
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const shellSessionDisposedMessage = "[vitehub] Shell session is disposed.";
|
|
74
|
+
var RuntimeShellSession = class {
|
|
75
|
+
provider;
|
|
76
|
+
env;
|
|
77
|
+
boundary;
|
|
78
|
+
policy;
|
|
79
|
+
#disposed = false;
|
|
80
|
+
#disposeTask;
|
|
81
|
+
#processes = /* @__PURE__ */ new Set();
|
|
82
|
+
#starts = /* @__PURE__ */ new Set();
|
|
83
|
+
#shellCalls = 0;
|
|
84
|
+
constructor(provider, policy, env) {
|
|
85
|
+
this.provider = provider;
|
|
86
|
+
this.env = env;
|
|
87
|
+
this.boundary = provider.boundary;
|
|
88
|
+
this.policy = policy;
|
|
89
|
+
}
|
|
90
|
+
async analyze(command, options) {
|
|
91
|
+
return await (this.provider.analyze ?? analyzeShellCommand).call(this.provider, command, options);
|
|
92
|
+
}
|
|
93
|
+
async exec(command, options = {}) {
|
|
94
|
+
if (this.#disposed) return createPolicyObservation(command, options.cwd, shellSessionDisposedMessage);
|
|
95
|
+
if (!options.budgetCharged) options.budget?.recordToolCall(new TextEncoder().encode(command).byteLength);
|
|
96
|
+
if (typeof this.policy.maxShellCalls === "number" && this.#shellCalls >= this.policy.maxShellCalls) return createPolicyObservation(command, options.cwd, `[vitehub] Shell session command budget exhausted after ${this.policy.maxShellCalls} calls. The Shell Session policy limits command calls for this run.`);
|
|
97
|
+
this.#shellCalls += 1;
|
|
98
|
+
const started = Date.now();
|
|
99
|
+
options = limitOutputOptions(options, this.policy);
|
|
100
|
+
const maxOutputLength = options.maxOutputLength;
|
|
101
|
+
const signal = options.budget && options.signal && options.signal !== options.budget.signal ? AbortSignal.any([options.signal, options.budget.signal]) : options.budget?.signal ?? options.signal;
|
|
102
|
+
const result = await this.provider.exec(command, {
|
|
103
|
+
...options,
|
|
104
|
+
env: {
|
|
105
|
+
...this.env,
|
|
106
|
+
...options.env
|
|
107
|
+
},
|
|
108
|
+
maxOutputLength,
|
|
109
|
+
signal,
|
|
110
|
+
timeout: options.timeout ?? this.policy.timeout
|
|
111
|
+
});
|
|
112
|
+
options.budget?.recordOutputBytes(new TextEncoder().encode(result.stdout).byteLength + new TextEncoder().encode(result.stderr).byteLength);
|
|
113
|
+
const timedOut = result.timedOut;
|
|
114
|
+
return applyOutputLimit({
|
|
115
|
+
command: result.command ?? command,
|
|
116
|
+
cwd: result.cwd ?? options.cwd,
|
|
117
|
+
durationMs: result.durationMs ?? Date.now() - started,
|
|
118
|
+
event: timedOut ? "command_timed_out" : result.event,
|
|
119
|
+
exitCode: result.exitCode,
|
|
120
|
+
maxOutputLength: result.maxOutputLength,
|
|
121
|
+
outputTruncated: result.outputTruncated,
|
|
122
|
+
stderr: result.stderr,
|
|
123
|
+
stdout: result.stdout,
|
|
124
|
+
timedOut,
|
|
125
|
+
workspaceGuardrail: result.workspaceGuardrail
|
|
126
|
+
}, maxOutputLength);
|
|
127
|
+
}
|
|
128
|
+
async startProcess(command, options = {}) {
|
|
129
|
+
if (this.#disposed) throw shellErrorDiagnostics.SHELL_R0008({ message: shellSessionDisposedMessage });
|
|
130
|
+
if (!this.provider.startProcess || !this.boundary.processes.background) throw shellErrorDiagnostics.SHELL_R0009({ message: "[vitehub] Shell provider does not support long-running processes." });
|
|
131
|
+
if (!options.budgetCharged) options.budget?.recordToolCall(new TextEncoder().encode(command).byteLength);
|
|
132
|
+
const maxProcesses = this.policy.maxProcesses;
|
|
133
|
+
if (maxProcesses !== void 0 && this.#processes.size + this.#starts.size >= maxProcesses) throw shellErrorDiagnostics.SHELL_R0010({ message: `[vitehub] Shell session process budget exhausted after ${this.policy.maxProcesses} processes.` });
|
|
134
|
+
options = limitOutputOptions(options, this.policy);
|
|
135
|
+
const signal = options.budget && options.signal && options.signal !== options.budget.signal ? AbortSignal.any([options.signal, options.budget.signal]) : options.budget?.signal ?? options.signal;
|
|
136
|
+
let finishStart;
|
|
137
|
+
const start = new Promise((resolve) => {
|
|
138
|
+
finishStart = resolve;
|
|
139
|
+
});
|
|
140
|
+
this.#starts.add(start);
|
|
141
|
+
start.finally(() => this.#starts.delete(start));
|
|
142
|
+
let process;
|
|
143
|
+
try {
|
|
144
|
+
process = await this.provider.startProcess(command, {
|
|
145
|
+
...options,
|
|
146
|
+
signal,
|
|
147
|
+
env: {
|
|
148
|
+
...this.env,
|
|
149
|
+
...options.env
|
|
150
|
+
},
|
|
151
|
+
timeout: options.timeout ?? this.policy.timeout
|
|
152
|
+
});
|
|
153
|
+
} catch (error) {
|
|
154
|
+
finishStart();
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
let stopTask;
|
|
158
|
+
let trackedProcess;
|
|
159
|
+
const stop = () => {
|
|
160
|
+
if (stopTask) return stopTask;
|
|
161
|
+
stopTask = Promise.resolve().then(() => process.stop()).then((result) => {
|
|
162
|
+
this.#processes.delete(trackedProcess);
|
|
163
|
+
return applyOutputLimit(result, options.maxOutputLength);
|
|
164
|
+
}, (error) => {
|
|
165
|
+
stopTask = void 0;
|
|
166
|
+
throw error;
|
|
167
|
+
});
|
|
168
|
+
return stopTask;
|
|
169
|
+
};
|
|
170
|
+
trackedProcess = {
|
|
171
|
+
get id() {
|
|
172
|
+
return process.id;
|
|
173
|
+
},
|
|
174
|
+
get command() {
|
|
175
|
+
return process.command;
|
|
176
|
+
},
|
|
177
|
+
get cwd() {
|
|
178
|
+
return process.cwd;
|
|
179
|
+
},
|
|
180
|
+
stop
|
|
181
|
+
};
|
|
182
|
+
this.#processes.add(trackedProcess);
|
|
183
|
+
finishStart();
|
|
184
|
+
if (this.#disposed) {
|
|
185
|
+
try {
|
|
186
|
+
await stop();
|
|
187
|
+
} catch (error) {
|
|
188
|
+
throw new AggregateError([shellErrorDiagnostics.SHELL_R0011({ message: shellSessionDisposedMessage }), error], "[vitehub] Shell session was disposed while starting a background process, and stopping it failed.");
|
|
189
|
+
}
|
|
190
|
+
throw shellErrorDiagnostics.SHELL_R0012({ message: shellSessionDisposedMessage });
|
|
191
|
+
}
|
|
192
|
+
return trackedProcess;
|
|
193
|
+
}
|
|
194
|
+
async listProcesses() {
|
|
195
|
+
return [...this.#processes];
|
|
196
|
+
}
|
|
197
|
+
dispose() {
|
|
198
|
+
this.#disposed = true;
|
|
199
|
+
if (this.#disposeTask) return this.#disposeTask;
|
|
200
|
+
this.#disposeTask = (async () => {
|
|
201
|
+
await Promise.all(this.#starts);
|
|
202
|
+
const failures = [];
|
|
203
|
+
for (const process of [...this.#processes].reverse()) try {
|
|
204
|
+
await process.stop();
|
|
205
|
+
} catch (error) {
|
|
206
|
+
failures.push(error);
|
|
207
|
+
}
|
|
208
|
+
if (failures.length === 1) throw failures[0];
|
|
209
|
+
if (failures.length > 1) throw new AggregateError(failures, "[vitehub] Shell session failed to stop multiple background processes.");
|
|
210
|
+
return {
|
|
211
|
+
event: "session_disposed",
|
|
212
|
+
exitCode: null,
|
|
213
|
+
stderr: "",
|
|
214
|
+
stdout: ""
|
|
215
|
+
};
|
|
216
|
+
})().catch((error) => {
|
|
217
|
+
this.#disposeTask = void 0;
|
|
218
|
+
throw error;
|
|
219
|
+
});
|
|
220
|
+
return this.#disposeTask;
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
function createShellRuntime(options) {
|
|
224
|
+
const { provider } = options;
|
|
225
|
+
const policy = { ...options.policy ?? {} };
|
|
226
|
+
return {
|
|
227
|
+
boundary: provider.boundary,
|
|
228
|
+
createSession(sessionOptions = {}) {
|
|
229
|
+
return new RuntimeShellSession(provider, {
|
|
230
|
+
...policy,
|
|
231
|
+
...sessionOptions.policy
|
|
232
|
+
}, sessionOptions.env);
|
|
233
|
+
},
|
|
234
|
+
async exec(command, execOptions = {}) {
|
|
235
|
+
const session = new RuntimeShellSession(provider, policy);
|
|
236
|
+
try {
|
|
237
|
+
return await session.exec(command, execOptions);
|
|
238
|
+
} finally {
|
|
239
|
+
await session.dispose();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
//#endregion
|
|
245
|
+
export { createShellRuntime as t };
|
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
import { ExecutionBudget } from "@vite-hub/runtime";
|
|
1
2
|
//#region src/runtime/types.d.ts
|
|
2
3
|
type ShellObservationEvent = "command_finished" | "command_timed_out" | "policy_denied" | "session_disposed";
|
|
3
4
|
interface ShellRuntimeExecOptions {
|
|
5
|
+
budget?: ExecutionBudget;
|
|
6
|
+
/** Set when the caller already charged this command against the budget. */
|
|
7
|
+
budgetCharged?: boolean;
|
|
4
8
|
cwd?: string;
|
|
5
9
|
env?: Record<string, string>;
|
|
10
|
+
/** Abort the command and any provider-owned work, such as a Source request. */
|
|
11
|
+
signal?: AbortSignal;
|
|
12
|
+
/** A provider may enforce this before buffering output. The runtime always enforces its policy limit. */
|
|
13
|
+
maxOutputLength?: number;
|
|
6
14
|
onStderr?: (data: string) => void;
|
|
7
15
|
onStdout?: (data: string) => void;
|
|
8
16
|
stdin?: string;
|
|
@@ -62,11 +70,23 @@ interface ShellBoundary {
|
|
|
62
70
|
enforcedBy: "provider" | "runtime" | "unsupported";
|
|
63
71
|
supported: boolean;
|
|
64
72
|
};
|
|
73
|
+
/** Provider execution details that callers can inspect without knowing the host implementation. */
|
|
74
|
+
execution?: {
|
|
75
|
+
isolation: "in_process" | "process" | "provider_managed" | "unknown";
|
|
76
|
+
model: "virtual" | "host" | "unknown";
|
|
77
|
+
};
|
|
78
|
+
/** Resource controls that this provider can enforce before buffering work. */
|
|
79
|
+
resources?: {
|
|
80
|
+
output: {
|
|
81
|
+
enforcedBy: "provider" | "runtime" | "unsupported";
|
|
82
|
+
unit: "characters";
|
|
83
|
+
};
|
|
84
|
+
};
|
|
65
85
|
}
|
|
66
86
|
interface ShellProcess {
|
|
67
|
-
id: string;
|
|
68
|
-
command: string;
|
|
69
|
-
cwd?: string;
|
|
87
|
+
readonly id: string;
|
|
88
|
+
readonly command: string;
|
|
89
|
+
readonly cwd?: string;
|
|
70
90
|
stop(): Promise<ShellObservation>;
|
|
71
91
|
}
|
|
72
92
|
interface ShellExecutionProvider {
|
package/dist/workspace.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { o as ShellExecutionProvider, s as ShellObservation } from "./types-
|
|
2
|
-
import { r as ShellNetworkGrantExecutor } from "./just-bash-
|
|
1
|
+
import { o as ShellExecutionProvider, s as ShellObservation } from "./types-2uHfTVHi.js";
|
|
2
|
+
import { r as ShellNetworkGrantExecutor } from "./just-bash-DAyZQWLK.js";
|
|
3
|
+
import { ExecutionBudget } from "@vite-hub/runtime";
|
|
3
4
|
import { IFileSystem } from "just-bash";
|
|
4
|
-
|
|
5
5
|
//#region src/workspace/types.d.ts
|
|
6
6
|
type ShellContent = string | Uint8Array;
|
|
7
7
|
interface ShellReadFileOptions {
|
|
@@ -47,7 +47,7 @@ interface SearchableShellWorkspace extends ReadonlyShellWorkspace {
|
|
|
47
47
|
search(query: ShellSearchQuery): Promise<ShellSearchHit[]>;
|
|
48
48
|
}
|
|
49
49
|
interface WritableShellWorkspace extends ReadonlyShellWorkspace {
|
|
50
|
-
writeFile(path: string, content: ShellContent): Promise<
|
|
50
|
+
writeFile(path: string, content: ShellContent): Promise<unknown>;
|
|
51
51
|
mkdir(path: string, options?: ShellMkdirOptions): Promise<void>;
|
|
52
52
|
rm(path: string, options?: ShellRmOptions): Promise<void>;
|
|
53
53
|
}
|
|
@@ -56,12 +56,14 @@ interface WritableShellWorkspace extends ReadonlyShellWorkspace {
|
|
|
56
56
|
interface WorkspaceShellFileSystem extends IFileSystem {
|
|
57
57
|
readonly writeFs: boolean;
|
|
58
58
|
}
|
|
59
|
-
declare const workspaceMountPoint = "/workspace";
|
|
60
|
-
declare function createReadonlyWorkspaceFs(workspace: ReadonlyShellWorkspace): WorkspaceShellFileSystem;
|
|
61
|
-
declare function createWritableWorkspaceFs(workspace: WritableShellWorkspace): WorkspaceShellFileSystem;
|
|
59
|
+
export declare const workspaceMountPoint = "/workspace";
|
|
60
|
+
export declare function createReadonlyWorkspaceFs(workspace: ReadonlyShellWorkspace): WorkspaceShellFileSystem;
|
|
61
|
+
export declare function createWritableWorkspaceFs(workspace: WritableShellWorkspace): WorkspaceShellFileSystem;
|
|
62
62
|
//#endregion
|
|
63
63
|
//#region src/workspace/inspection.d.ts
|
|
64
64
|
interface WorkspaceInspectionCommandOptions {
|
|
65
|
+
budget?: ExecutionBudget;
|
|
66
|
+
budgetCharged?: boolean;
|
|
65
67
|
broadSearchPaths?: string[];
|
|
66
68
|
commands?: string[];
|
|
67
69
|
cwd?: string;
|
|
@@ -69,12 +71,13 @@ interface WorkspaceInspectionCommandOptions {
|
|
|
69
71
|
maxOutputLength?: number;
|
|
70
72
|
networkGrants?: ShellNetworkGrantExecutor;
|
|
71
73
|
provider?: ShellExecutionProvider;
|
|
74
|
+
signal?: AbortSignal;
|
|
72
75
|
timeout?: number;
|
|
73
76
|
}
|
|
74
|
-
declare function runWorkspaceInspectionCommand(_input: SearchableShellWorkspace, command: string, options: WorkspaceInspectionCommandOptions): Promise<ShellObservation>;
|
|
77
|
+
export declare function runWorkspaceInspectionCommand(_input: SearchableShellWorkspace, command: string, options: WorkspaceInspectionCommandOptions): Promise<ShellObservation>;
|
|
75
78
|
//#endregion
|
|
76
79
|
//#region src/workspace/path.d.ts
|
|
77
|
-
declare function cleanWorkspaceShellPath(path?: string): string;
|
|
78
|
-
declare function cleanWorkspaceMutationPath(path: string): string;
|
|
80
|
+
export declare function cleanWorkspaceShellPath(path?: string): string;
|
|
81
|
+
export declare function cleanWorkspaceMutationPath(path: string): string;
|
|
79
82
|
//#endregion
|
|
80
|
-
export {
|
|
83
|
+
export type { ReadonlyShellWorkspace, SearchableShellWorkspace, ShellContent, ShellEntry, ShellListOptions, ShellMkdirOptions, ShellReadFileOptions, ShellRmOptions, ShellSearchHit, ShellSearchQuery, ShellStat, WorkspaceShellFileSystem, WritableShellWorkspace };
|