@vite-hub/shell 0.0.0 → 0.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/README.md +54 -1
- package/dist/analyze-BhMXNtZg.js +126 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +3 -0
- package/dist/providers/cloudflare.d.ts +28 -0
- package/dist/providers/cloudflare.js +83 -0
- package/dist/providers/just-bash.d.ts +15 -0
- package/dist/providers/just-bash.js +71 -0
- package/dist/session-Qzhl3bqv.js +129 -0
- package/dist/types-BNtJGjIv.d.ts +100 -0
- package/dist/workspace.d.ts +77 -0
- package/dist/workspace.js +733 -0
- package/package.json +47 -5
package/README.md
CHANGED
|
@@ -1,3 +1,56 @@
|
|
|
1
1
|
# @vite-hub/shell
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p>
|
|
4
|
+
<a href="https://vitehub.dev"><img alt="ViteHub" src="https://img.shields.io/badge/ViteHub-vitehub.dev-646cff?style=flat-square"></a>
|
|
5
|
+
<img alt="TypeScript" src="https://img.shields.io/badge/TypeScript-ready-3178c6?style=flat-square">
|
|
6
|
+
<img alt="Shell" src="https://img.shields.io/badge/Shell-controlled%20runtime-18181b?style=flat-square">
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
`@vite-hub/shell` gives agents and workspace tools structured command execution with explicit boundaries.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
pnpm add @vite-hub/shell
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Minimal API
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
// server/utils/shell.ts
|
|
21
|
+
import { analyzeShellCommand, createShellRuntime } from "@vite-hub/shell"
|
|
22
|
+
import type { ShellExecutionProvider } from "@vite-hub/shell"
|
|
23
|
+
|
|
24
|
+
const provider: ShellExecutionProvider = {
|
|
25
|
+
boundary: {
|
|
26
|
+
cwd: true,
|
|
27
|
+
env: true,
|
|
28
|
+
filesystem: { writable: false },
|
|
29
|
+
network: false,
|
|
30
|
+
processes: { background: false, interactive: false },
|
|
31
|
+
streaming: false,
|
|
32
|
+
timeout: { enforcedBy: "runtime", supported: true },
|
|
33
|
+
},
|
|
34
|
+
async exec(command) {
|
|
35
|
+
return {
|
|
36
|
+
command,
|
|
37
|
+
event: "command_finished",
|
|
38
|
+
exitCode: 0,
|
|
39
|
+
stderr: "",
|
|
40
|
+
stdout: "ok\n",
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const runtime = createShellRuntime({ provider, policy: { maxShellCalls: 5 } })
|
|
46
|
+
const analysis = await analyzeShellCommand("echo ok")
|
|
47
|
+
const result = analysis.ok ? await runtime.exec("echo ok") : undefined
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Used by
|
|
51
|
+
|
|
52
|
+
`workspaceShell()` in [`@vite-hub/agent`](../agent/README.md) uses this package to expose scoped shell work to an agent. Workspace adapters live in `@vite-hub/shell/workspace`.
|
|
53
|
+
|
|
54
|
+
Built on [just-bash](https://www.npmjs.com/package/just-bash) for the built-in shell provider and [sh-syntax](https://www.npmjs.com/package/sh-syntax) for command analysis.
|
|
55
|
+
|
|
56
|
+
Learn more at [vitehub.dev](https://vitehub.dev).
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
//#region src/command/analyze.ts
|
|
2
|
+
const defaultMaxInputBytes = 64 * 1024;
|
|
3
|
+
const defaultTimeoutMs = 100;
|
|
4
|
+
async function analyzeShellCommand(command, options = {}) {
|
|
5
|
+
const maxInputBytes = options.maxInputBytes ?? defaultMaxInputBytes;
|
|
6
|
+
if (new TextEncoder().encode(command).byteLength > maxInputBytes) return {
|
|
7
|
+
error: `Shell command exceeds ${maxInputBytes} bytes.`,
|
|
8
|
+
ok: false,
|
|
9
|
+
parser: "sh-syntax"
|
|
10
|
+
};
|
|
11
|
+
try {
|
|
12
|
+
await withTimeout(parseWithShSyntax(command), options.timeoutMs ?? defaultTimeoutMs);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
return {
|
|
15
|
+
commands: detectCommandNames(command),
|
|
16
|
+
error: error instanceof Error ? error.message : String(error),
|
|
17
|
+
ok: false,
|
|
18
|
+
parser: "sh-syntax"
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
commands: detectCommandNames(command),
|
|
23
|
+
hasCommandSubstitution: /(?:\$\(|`)/.test(command),
|
|
24
|
+
hasHeredocs: /<<-?/.test(command),
|
|
25
|
+
hasPipelines: hasUnquoted(command, "|"),
|
|
26
|
+
hasRedirects: /(?:^|[^<])(?:>>?|<)/.test(command),
|
|
27
|
+
ok: true,
|
|
28
|
+
parser: "sh-syntax"
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
async function parseWithShSyntax(command) {
|
|
32
|
+
const { parse, LangVariant } = await import("sh-syntax");
|
|
33
|
+
await parse(command, { variant: LangVariant.LangBash });
|
|
34
|
+
}
|
|
35
|
+
async function withTimeout(promise, timeoutMs) {
|
|
36
|
+
if (timeoutMs <= 0) throw new Error(`Shell analysis timed out after ${timeoutMs}ms.`);
|
|
37
|
+
let timeout;
|
|
38
|
+
try {
|
|
39
|
+
return await Promise.race([promise, new Promise((_, reject) => {
|
|
40
|
+
timeout = setTimeout(() => reject(/* @__PURE__ */ new Error(`Shell analysis timed out after ${timeoutMs}ms.`)), timeoutMs);
|
|
41
|
+
})]);
|
|
42
|
+
} finally {
|
|
43
|
+
if (timeout) clearTimeout(timeout);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function detectCommandNames(command) {
|
|
47
|
+
const names = [];
|
|
48
|
+
for (const segment of splitShellCommandSegments(command)) {
|
|
49
|
+
const name = firstCommandWord(segment);
|
|
50
|
+
if (name && !names.includes(name)) names.push(name);
|
|
51
|
+
}
|
|
52
|
+
return names;
|
|
53
|
+
}
|
|
54
|
+
function splitShellCommandSegments(command) {
|
|
55
|
+
const segments = [];
|
|
56
|
+
let current = "";
|
|
57
|
+
let quote;
|
|
58
|
+
let escaped = false;
|
|
59
|
+
for (let index = 0; index < command.length; index++) {
|
|
60
|
+
const char = command[index];
|
|
61
|
+
const next = command[index + 1];
|
|
62
|
+
if (escaped) {
|
|
63
|
+
current += char;
|
|
64
|
+
escaped = false;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (char === "\\") {
|
|
68
|
+
current += char;
|
|
69
|
+
escaped = true;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (quote) {
|
|
73
|
+
if (char === quote) quote = void 0;
|
|
74
|
+
current += char;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (char === "'" || char === "\"") {
|
|
78
|
+
quote = char;
|
|
79
|
+
current += char;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (char === "|" || char === ";" || char === "\n" || char === "&" && next === "&" || char === "|" && next === "|") {
|
|
83
|
+
segments.push(current);
|
|
84
|
+
current = "";
|
|
85
|
+
if ((char === "&" || char === "|") && next === char) index += 1;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
current += char;
|
|
89
|
+
}
|
|
90
|
+
segments.push(current);
|
|
91
|
+
return segments;
|
|
92
|
+
}
|
|
93
|
+
function firstCommandWord(segment) {
|
|
94
|
+
const words = segment.trim().match(/[^\s]+/g) || [];
|
|
95
|
+
for (const word of words) {
|
|
96
|
+
if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(word)) continue;
|
|
97
|
+
if (/^\d*(?:>>?|<<?-?|<&|>&)/.test(word)) continue;
|
|
98
|
+
return word.replace(/^command$/, "") || void 0;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function hasUnquoted(command, target) {
|
|
102
|
+
let quote;
|
|
103
|
+
let escaped = false;
|
|
104
|
+
for (const char of command) {
|
|
105
|
+
if (escaped) {
|
|
106
|
+
escaped = false;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (char === "\\") {
|
|
110
|
+
escaped = true;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (quote) {
|
|
114
|
+
if (char === quote) quote = void 0;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (char === "'" || char === "\"") {
|
|
118
|
+
quote = char;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (char === target) return true;
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
//#endregion
|
|
126
|
+
export { analyzeShellCommand as t };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { a as ShellBoundary, c as ShellObservationEvent, d as ShellRuntimeExecOptions, f as ShellSession, i as ShellAnalyzeResult, l as ShellProcess, n as CreateShellSessionOptions, o as ShellExecutionProvider, p as ShellSessionPolicy, r as ShellAnalyzeOptions, s as ShellObservation, t as CreateShellRuntimeOptions, u as ShellRuntime } from "./types-BNtJGjIv.js";
|
|
2
|
+
|
|
3
|
+
//#region src/command/analyze.d.ts
|
|
4
|
+
declare function analyzeShellCommand(command: string, options?: ShellAnalyzeOptions): Promise<ShellAnalyzeResult>;
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region src/runtime/session.d.ts
|
|
7
|
+
declare function createShellRuntime(options: CreateShellRuntimeOptions): ShellRuntime;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { CreateShellRuntimeOptions, CreateShellSessionOptions, ShellAnalyzeOptions, ShellAnalyzeResult, ShellBoundary, ShellExecutionProvider, ShellObservation, ShellObservationEvent, ShellProcess, ShellRuntime, ShellRuntimeExecOptions, ShellSession, ShellSessionPolicy, analyzeShellCommand, createShellRuntime };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { o as ShellExecutionProvider } from "../types-BNtJGjIv.js";
|
|
2
|
+
|
|
3
|
+
//#region src/providers/cloudflare.d.ts
|
|
4
|
+
interface CloudflareShellClient {
|
|
5
|
+
exec: (command: string, args?: string[], options?: {
|
|
6
|
+
cwd?: string;
|
|
7
|
+
env?: Record<string, string>;
|
|
8
|
+
onStderr?: (data: string) => void;
|
|
9
|
+
onStdout?: (data: string) => void;
|
|
10
|
+
stdin?: string;
|
|
11
|
+
timeout?: number;
|
|
12
|
+
}) => Promise<{
|
|
13
|
+
code?: number | null;
|
|
14
|
+
exitCode?: number | null;
|
|
15
|
+
stderr: string;
|
|
16
|
+
stdout: string;
|
|
17
|
+
}>;
|
|
18
|
+
supports: {
|
|
19
|
+
execCwd: boolean;
|
|
20
|
+
execEnv: boolean;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
interface CloudflareShellProviderOptions {
|
|
24
|
+
sandbox: CloudflareShellClient;
|
|
25
|
+
}
|
|
26
|
+
declare function createCloudflareShellProvider(options: CloudflareShellProviderOptions): ShellExecutionProvider;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { CloudflareShellClient, CloudflareShellProviderOptions, createCloudflareShellProvider };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { t as analyzeShellCommand } from "../analyze-BhMXNtZg.js";
|
|
2
|
+
//#region src/command/parse.ts
|
|
3
|
+
function parseShellCommand(command) {
|
|
4
|
+
const words = [];
|
|
5
|
+
let current = "";
|
|
6
|
+
let quote;
|
|
7
|
+
let escaped = false;
|
|
8
|
+
for (const char of command) {
|
|
9
|
+
if (escaped) {
|
|
10
|
+
current += char;
|
|
11
|
+
escaped = false;
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
if (char === "\\") {
|
|
15
|
+
escaped = true;
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
if (quote) {
|
|
19
|
+
if (char === quote) quote = void 0;
|
|
20
|
+
else current += char;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (char === "'" || char === "\"") {
|
|
24
|
+
quote = char;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
if (/\s/.test(char)) {
|
|
28
|
+
if (current) {
|
|
29
|
+
words.push(current);
|
|
30
|
+
current = "";
|
|
31
|
+
}
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
current += char;
|
|
35
|
+
}
|
|
36
|
+
if (escaped) current += "\\";
|
|
37
|
+
if (quote) throw new Error("unterminated quote");
|
|
38
|
+
if (current) words.push(current);
|
|
39
|
+
return words;
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/providers/cloudflare.ts
|
|
43
|
+
function createCloudflareShellProvider(options) {
|
|
44
|
+
return {
|
|
45
|
+
analyze: analyzeShellCommand,
|
|
46
|
+
boundary: {
|
|
47
|
+
cwd: options.sandbox.supports.execCwd,
|
|
48
|
+
env: options.sandbox.supports.execEnv,
|
|
49
|
+
filesystem: { writable: true },
|
|
50
|
+
network: "unknown",
|
|
51
|
+
processes: {
|
|
52
|
+
background: false,
|
|
53
|
+
interactive: false
|
|
54
|
+
},
|
|
55
|
+
streaming: true,
|
|
56
|
+
timeout: {
|
|
57
|
+
enforcedBy: "provider",
|
|
58
|
+
supported: true
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
async exec(command, execOptions = {}) {
|
|
62
|
+
const [cmd = "", ...args] = parseShellCommand(command);
|
|
63
|
+
const result = await options.sandbox.exec(cmd, args, {
|
|
64
|
+
cwd: execOptions.cwd,
|
|
65
|
+
env: execOptions.env,
|
|
66
|
+
onStderr: execOptions.onStderr,
|
|
67
|
+
onStdout: execOptions.onStdout,
|
|
68
|
+
stdin: execOptions.stdin,
|
|
69
|
+
timeout: execOptions.timeout
|
|
70
|
+
});
|
|
71
|
+
return {
|
|
72
|
+
command,
|
|
73
|
+
cwd: execOptions.cwd,
|
|
74
|
+
event: "command_finished",
|
|
75
|
+
exitCode: result.exitCode ?? result.code ?? null,
|
|
76
|
+
stderr: result.stderr,
|
|
77
|
+
stdout: result.stdout
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
export { createCloudflareShellProvider };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { o as ShellExecutionProvider } from "../types-BNtJGjIv.js";
|
|
2
|
+
import { IFileSystem } from "just-bash";
|
|
3
|
+
|
|
4
|
+
//#region src/providers/just-bash.d.ts
|
|
5
|
+
interface JustBashFileSystem extends IFileSystem {
|
|
6
|
+
readonly writeFs: boolean;
|
|
7
|
+
}
|
|
8
|
+
interface JustBashProviderOptions {
|
|
9
|
+
commands?: string[];
|
|
10
|
+
cwd?: string;
|
|
11
|
+
fs: JustBashFileSystem;
|
|
12
|
+
}
|
|
13
|
+
declare function createJustBashProvider(options: JustBashProviderOptions): ShellExecutionProvider;
|
|
14
|
+
//#endregion
|
|
15
|
+
export { JustBashFileSystem, JustBashProviderOptions, createJustBashProvider };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { t as analyzeShellCommand } from "../analyze-BhMXNtZg.js";
|
|
2
|
+
//#region src/providers/just-bash.ts
|
|
3
|
+
function createJustBashProvider(options) {
|
|
4
|
+
return {
|
|
5
|
+
analyze: analyzeShellCommand,
|
|
6
|
+
boundary: {
|
|
7
|
+
cwd: true,
|
|
8
|
+
env: true,
|
|
9
|
+
filesystem: {
|
|
10
|
+
mountPoint: "/workspace",
|
|
11
|
+
writable: options.fs.writeFs
|
|
12
|
+
},
|
|
13
|
+
network: false,
|
|
14
|
+
processes: {
|
|
15
|
+
background: false,
|
|
16
|
+
interactive: false
|
|
17
|
+
},
|
|
18
|
+
streaming: false,
|
|
19
|
+
timeout: {
|
|
20
|
+
enforcedBy: "provider",
|
|
21
|
+
supported: true
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
async exec(command, execOptions = {}) {
|
|
25
|
+
const result = await withProviderTimeout(command, execOptions, async () => {
|
|
26
|
+
const { Bash } = await import("just-bash/browser");
|
|
27
|
+
const bash = new Bash({
|
|
28
|
+
commands: options.commands,
|
|
29
|
+
cwd: options.cwd,
|
|
30
|
+
fs: options.fs
|
|
31
|
+
});
|
|
32
|
+
const signal = typeof execOptions.timeout === "number" ? AbortSignal.timeout(execOptions.timeout) : void 0;
|
|
33
|
+
return await bash.exec(command, {
|
|
34
|
+
cwd: execOptions.cwd,
|
|
35
|
+
env: execOptions.env,
|
|
36
|
+
signal
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
const timedOut = "timedOut" in result && result.timedOut === true;
|
|
40
|
+
execOptions.onStdout?.(result.stdout);
|
|
41
|
+
execOptions.onStderr?.(result.stderr);
|
|
42
|
+
return {
|
|
43
|
+
command,
|
|
44
|
+
cwd: execOptions.cwd,
|
|
45
|
+
event: timedOut ? "command_timed_out" : "command_finished",
|
|
46
|
+
exitCode: result.exitCode,
|
|
47
|
+
stderr: result.stderr,
|
|
48
|
+
stdout: result.stdout,
|
|
49
|
+
timedOut
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
async function withProviderTimeout(command, options, run) {
|
|
55
|
+
if (typeof options.timeout !== "number") return await run();
|
|
56
|
+
let timeout;
|
|
57
|
+
try {
|
|
58
|
+
return await Promise.race([run(), new Promise((resolve) => {
|
|
59
|
+
timeout = setTimeout(() => resolve({
|
|
60
|
+
exitCode: null,
|
|
61
|
+
stderr: `[vitehub] Workspace shell command timed out after ${options.timeout}ms.`,
|
|
62
|
+
stdout: "",
|
|
63
|
+
timedOut: true
|
|
64
|
+
}), options.timeout);
|
|
65
|
+
})]);
|
|
66
|
+
} finally {
|
|
67
|
+
if (timeout) clearTimeout(timeout);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
//#endregion
|
|
71
|
+
export { createJustBashProvider };
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { t as analyzeShellCommand } from "./analyze-BhMXNtZg.js";
|
|
2
|
+
//#region src/runtime/session.ts
|
|
3
|
+
function applyOutputLimit(result, maxLength) {
|
|
4
|
+
if (!maxLength) return result;
|
|
5
|
+
const next = {
|
|
6
|
+
...result,
|
|
7
|
+
maxOutputLength: maxLength
|
|
8
|
+
};
|
|
9
|
+
let truncated = false;
|
|
10
|
+
if (next.stdout.length > maxLength) {
|
|
11
|
+
next.stdout = `${next.stdout.slice(0, maxLength)}\n[output truncated to ${maxLength} characters]\n`;
|
|
12
|
+
truncated = true;
|
|
13
|
+
}
|
|
14
|
+
if (next.stderr.length > maxLength) {
|
|
15
|
+
next.stderr = `${next.stderr.slice(0, maxLength)}\n[output truncated to ${maxLength} characters]\n`;
|
|
16
|
+
truncated = true;
|
|
17
|
+
}
|
|
18
|
+
return truncated ? {
|
|
19
|
+
...next,
|
|
20
|
+
outputTruncated: true
|
|
21
|
+
} : next;
|
|
22
|
+
}
|
|
23
|
+
function createPolicyObservation(command, cwd, message) {
|
|
24
|
+
return {
|
|
25
|
+
command,
|
|
26
|
+
cwd,
|
|
27
|
+
event: "policy_denied",
|
|
28
|
+
exitCode: 126,
|
|
29
|
+
stderr: message.endsWith("\n") ? message : `${message}\n`,
|
|
30
|
+
stdout: ""
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
var RuntimeShellSession = class {
|
|
34
|
+
boundary;
|
|
35
|
+
policy;
|
|
36
|
+
#disposed = false;
|
|
37
|
+
#processes = /* @__PURE__ */ new Map();
|
|
38
|
+
#shellCalls = 0;
|
|
39
|
+
constructor(provider, policy, env) {
|
|
40
|
+
this.provider = provider;
|
|
41
|
+
this.env = env;
|
|
42
|
+
this.boundary = provider.boundary;
|
|
43
|
+
this.policy = policy;
|
|
44
|
+
}
|
|
45
|
+
async analyze(command, options) {
|
|
46
|
+
return await (this.provider.analyze ?? analyzeShellCommand)(command, options);
|
|
47
|
+
}
|
|
48
|
+
async exec(command, options = {}) {
|
|
49
|
+
if (this.#disposed) return createPolicyObservation(command, options.cwd, "[vitehub] Shell session is disposed.");
|
|
50
|
+
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. Answer from the evidence already collected instead of running more shell commands.`);
|
|
51
|
+
this.#shellCalls += 1;
|
|
52
|
+
const started = Date.now();
|
|
53
|
+
const result = await this.provider.exec(command, {
|
|
54
|
+
...options,
|
|
55
|
+
env: {
|
|
56
|
+
...this.env,
|
|
57
|
+
...options.env
|
|
58
|
+
},
|
|
59
|
+
timeout: options.timeout ?? this.policy.timeout
|
|
60
|
+
});
|
|
61
|
+
return applyOutputLimit({
|
|
62
|
+
...result,
|
|
63
|
+
command: result.command ?? command,
|
|
64
|
+
cwd: result.cwd ?? options.cwd,
|
|
65
|
+
durationMs: result.durationMs ?? Date.now() - started,
|
|
66
|
+
event: result.timedOut ? "command_timed_out" : result.event
|
|
67
|
+
}, this.policy.maxOutputLength);
|
|
68
|
+
}
|
|
69
|
+
async startProcess(command, options = {}) {
|
|
70
|
+
if (this.#disposed) throw new Error("[vitehub] Shell session is disposed.");
|
|
71
|
+
if (!this.provider.startProcess || !this.boundary.processes.background) throw new Error("[vitehub] Shell provider does not support long-running processes.");
|
|
72
|
+
if (typeof this.policy.maxProcesses === "number" && this.#processes.size >= this.policy.maxProcesses) throw new Error(`[vitehub] Shell session process budget exhausted after ${this.policy.maxProcesses} processes.`);
|
|
73
|
+
const process = await this.provider.startProcess(command, {
|
|
74
|
+
...options,
|
|
75
|
+
env: {
|
|
76
|
+
...this.env,
|
|
77
|
+
...options.env
|
|
78
|
+
},
|
|
79
|
+
timeout: options.timeout ?? this.policy.timeout
|
|
80
|
+
});
|
|
81
|
+
const trackedProcess = {
|
|
82
|
+
...process,
|
|
83
|
+
stop: async () => {
|
|
84
|
+
try {
|
|
85
|
+
return await process.stop();
|
|
86
|
+
} finally {
|
|
87
|
+
this.#processes.delete(process.id);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
this.#processes.set(process.id, trackedProcess);
|
|
92
|
+
return trackedProcess;
|
|
93
|
+
}
|
|
94
|
+
async listProcesses() {
|
|
95
|
+
return [...this.#processes.values()];
|
|
96
|
+
}
|
|
97
|
+
async dispose() {
|
|
98
|
+
this.#disposed = true;
|
|
99
|
+
return {
|
|
100
|
+
event: "session_disposed",
|
|
101
|
+
exitCode: null,
|
|
102
|
+
stderr: "",
|
|
103
|
+
stdout: ""
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
function createShellRuntime(options) {
|
|
108
|
+
const { provider } = options;
|
|
109
|
+
const policy = options.policy ?? {};
|
|
110
|
+
return {
|
|
111
|
+
boundary: provider.boundary,
|
|
112
|
+
createSession(sessionOptions = {}) {
|
|
113
|
+
return new RuntimeShellSession(provider, {
|
|
114
|
+
...policy,
|
|
115
|
+
...sessionOptions.policy
|
|
116
|
+
}, sessionOptions.env);
|
|
117
|
+
},
|
|
118
|
+
async exec(command, execOptions = {}) {
|
|
119
|
+
const session = new RuntimeShellSession(provider, policy);
|
|
120
|
+
try {
|
|
121
|
+
return await session.exec(command, execOptions);
|
|
122
|
+
} finally {
|
|
123
|
+
await session.dispose();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
129
|
+
export { createShellRuntime as t };
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
//#region src/runtime/types.d.ts
|
|
2
|
+
type ShellObservationEvent = "command_finished" | "command_timed_out" | "policy_denied" | "session_disposed";
|
|
3
|
+
interface ShellRuntimeExecOptions {
|
|
4
|
+
cwd?: string;
|
|
5
|
+
env?: Record<string, string>;
|
|
6
|
+
onStderr?: (data: string) => void;
|
|
7
|
+
onStdout?: (data: string) => void;
|
|
8
|
+
stdin?: string;
|
|
9
|
+
timeout?: number;
|
|
10
|
+
}
|
|
11
|
+
interface ShellObservation {
|
|
12
|
+
command?: string;
|
|
13
|
+
cwd?: string;
|
|
14
|
+
durationMs?: number;
|
|
15
|
+
event: ShellObservationEvent;
|
|
16
|
+
exitCode: number | null;
|
|
17
|
+
maxOutputLength?: number;
|
|
18
|
+
outputTruncated?: boolean;
|
|
19
|
+
stderr: string;
|
|
20
|
+
stdout: string;
|
|
21
|
+
timedOut?: boolean;
|
|
22
|
+
workspaceGuardrail?: {
|
|
23
|
+
kind: "broad_search" | "missing_path" | "no_match" | "timeout";
|
|
24
|
+
path?: string;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
interface ShellAnalyzeOptions {
|
|
28
|
+
maxInputBytes?: number;
|
|
29
|
+
timeoutMs?: number;
|
|
30
|
+
}
|
|
31
|
+
interface ShellAnalyzeResult {
|
|
32
|
+
commands?: string[];
|
|
33
|
+
error?: string;
|
|
34
|
+
hasCommandSubstitution?: boolean;
|
|
35
|
+
hasHeredocs?: boolean;
|
|
36
|
+
hasPipelines?: boolean;
|
|
37
|
+
hasRedirects?: boolean;
|
|
38
|
+
ok: boolean;
|
|
39
|
+
parser: "sh-syntax";
|
|
40
|
+
}
|
|
41
|
+
interface ShellSessionPolicy {
|
|
42
|
+
maxOutputLength?: number;
|
|
43
|
+
maxShellCalls?: number;
|
|
44
|
+
maxProcesses?: number;
|
|
45
|
+
timeout?: number;
|
|
46
|
+
}
|
|
47
|
+
interface ShellBoundary {
|
|
48
|
+
cwd: boolean;
|
|
49
|
+
env: boolean;
|
|
50
|
+
filesystem: {
|
|
51
|
+
mountPoint?: string;
|
|
52
|
+
writable: boolean;
|
|
53
|
+
};
|
|
54
|
+
network: boolean | "unknown";
|
|
55
|
+
processes: {
|
|
56
|
+
background: boolean;
|
|
57
|
+
interactive: boolean;
|
|
58
|
+
};
|
|
59
|
+
streaming: boolean;
|
|
60
|
+
timeout: {
|
|
61
|
+
enforcedBy: "provider" | "runtime" | "unsupported";
|
|
62
|
+
supported: boolean;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
interface ShellProcess {
|
|
66
|
+
id: string;
|
|
67
|
+
command: string;
|
|
68
|
+
cwd?: string;
|
|
69
|
+
stop(): Promise<ShellObservation>;
|
|
70
|
+
}
|
|
71
|
+
interface ShellExecutionProvider {
|
|
72
|
+
analyze?: (command: string, options?: ShellAnalyzeOptions) => Promise<ShellAnalyzeResult>;
|
|
73
|
+
boundary: ShellBoundary;
|
|
74
|
+
exec(command: string, options?: ShellRuntimeExecOptions): Promise<ShellObservation>;
|
|
75
|
+
startProcess?: (command: string, options?: ShellRuntimeExecOptions) => Promise<ShellProcess>;
|
|
76
|
+
}
|
|
77
|
+
interface ShellSession {
|
|
78
|
+
analyze(command: string, options?: ShellAnalyzeOptions): Promise<ShellAnalyzeResult>;
|
|
79
|
+
boundary: ShellBoundary;
|
|
80
|
+
dispose(): Promise<ShellObservation>;
|
|
81
|
+
exec(command: string, options?: ShellRuntimeExecOptions): Promise<ShellObservation>;
|
|
82
|
+
listProcesses(): Promise<ShellProcess[]>;
|
|
83
|
+
policy: ShellSessionPolicy;
|
|
84
|
+
startProcess(command: string, options?: ShellRuntimeExecOptions): Promise<ShellProcess>;
|
|
85
|
+
}
|
|
86
|
+
interface ShellRuntime {
|
|
87
|
+
boundary: ShellBoundary;
|
|
88
|
+
createSession(options?: CreateShellSessionOptions): ShellSession;
|
|
89
|
+
exec(command: string, options?: ShellRuntimeExecOptions): Promise<ShellObservation>;
|
|
90
|
+
}
|
|
91
|
+
interface CreateShellRuntimeOptions {
|
|
92
|
+
provider: ShellExecutionProvider;
|
|
93
|
+
policy?: ShellSessionPolicy;
|
|
94
|
+
}
|
|
95
|
+
interface CreateShellSessionOptions {
|
|
96
|
+
env?: Record<string, string>;
|
|
97
|
+
policy?: ShellSessionPolicy;
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
export { ShellBoundary as a, ShellObservationEvent as c, ShellRuntimeExecOptions as d, ShellSession as f, ShellAnalyzeResult as i, ShellProcess as l, CreateShellSessionOptions as n, ShellExecutionProvider as o, ShellSessionPolicy as p, ShellAnalyzeOptions as r, ShellObservation as s, CreateShellRuntimeOptions as t, ShellRuntime as u };
|