@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
package/dist/workspace.js
CHANGED
|
@@ -1,21 +1,42 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as shellErrorDiagnostics, n as parseShellCommand, r as splitShellCommandSegments } from "./analyze-BXwz20IH.js";
|
|
2
|
+
import { t as createShellRuntime } from "./session-CIy2VLp9.js";
|
|
2
3
|
import { createJustBashProvider } from "./providers/just-bash.js";
|
|
4
|
+
import { Buffer } from "node:buffer";
|
|
3
5
|
import { posix } from "node:path";
|
|
6
|
+
import { hasRuntimeType } from "@vite-hub/runtime/internal/runtime-type";
|
|
4
7
|
//#region src/workspace/filesystem.ts
|
|
5
8
|
const workspaceMountPoint = "/workspace";
|
|
6
9
|
function createEscapeError(path) {
|
|
7
|
-
return
|
|
10
|
+
return shellErrorDiagnostics.SHELL_R0013({ message: `[vitehub] Workspace path escapes the workspace root: "${path}".` });
|
|
8
11
|
}
|
|
9
12
|
function createReadonlyError() {
|
|
10
|
-
return
|
|
13
|
+
return shellErrorDiagnostics.SHELL_R0014({ message: "[vitehub] Workspace filesystem is read-only." });
|
|
11
14
|
}
|
|
12
|
-
function toShellContent(content) {
|
|
13
|
-
|
|
15
|
+
function toShellContent(content, encoding) {
|
|
16
|
+
if (!hasRuntimeType(content, "string")) return content;
|
|
17
|
+
if (encoding === "base64") {
|
|
18
|
+
const decoded = atob(content);
|
|
19
|
+
const bytes = new Uint8Array(decoded.length);
|
|
20
|
+
for (let index = 0; index < decoded.length; index++) bytes[index] = decoded.charCodeAt(index);
|
|
21
|
+
return bytes;
|
|
22
|
+
}
|
|
23
|
+
if (encoding === "hex") {
|
|
24
|
+
const bytes = new Uint8Array(content.length / 2);
|
|
25
|
+
for (let index = 0; index < content.length; index += 2) bytes[index / 2] = Number.parseInt(content.slice(index, index + 2), 16);
|
|
26
|
+
return bytes;
|
|
27
|
+
}
|
|
28
|
+
if (encoding === "binary" || encoding === "latin1") {
|
|
29
|
+
if (content.length <= 65536) return Uint8Array.from(content, (char) => char.charCodeAt(0));
|
|
30
|
+
const bytes = new Uint8Array(content.length);
|
|
31
|
+
for (let index = 0; index < content.length; index++) bytes[index] = content.charCodeAt(index);
|
|
32
|
+
return bytes;
|
|
33
|
+
}
|
|
34
|
+
return new TextEncoder().encode(content);
|
|
14
35
|
}
|
|
15
36
|
function decodeContent(content, encoding) {
|
|
16
37
|
if (encoding === "base64") return Buffer.from(content).toString("base64");
|
|
17
38
|
if (encoding === "hex") return Buffer.from(content).toString("hex");
|
|
18
|
-
if (encoding === "
|
|
39
|
+
if (encoding === "latin1" || encoding === "binary") return Buffer.from(content).toString(encoding);
|
|
19
40
|
return new TextDecoder().decode(content);
|
|
20
41
|
}
|
|
21
42
|
function normalizeInputPath(path) {
|
|
@@ -58,6 +79,7 @@ var WorkspaceFileSystem = class {
|
|
|
58
79
|
writeFs;
|
|
59
80
|
#paths = [workspaceMountPoint];
|
|
60
81
|
#refreshPromise;
|
|
82
|
+
#refreshAgain = false;
|
|
61
83
|
constructor(workspace, writeFs) {
|
|
62
84
|
this.workspace = workspace;
|
|
63
85
|
this.writeFs = writeFs;
|
|
@@ -69,15 +91,20 @@ var WorkspaceFileSystem = class {
|
|
|
69
91
|
const relativePath = this.#toRelativePath(path);
|
|
70
92
|
return toShellContent(await this.workspace.readFile(relativePath, { encoding: "binary" }));
|
|
71
93
|
}
|
|
72
|
-
async writeFile(path, content,
|
|
73
|
-
|
|
94
|
+
async writeFile(path, content, options) {
|
|
95
|
+
const workspace = this.#requireWritable();
|
|
96
|
+
const encoding = hasRuntimeType(options, "string") ? options : options?.encoding;
|
|
97
|
+
await workspace.writeFile(this.#toRelativePath(path), encoding ? toShellContent(content, encoding) : content);
|
|
74
98
|
await this.#refreshPaths();
|
|
75
99
|
}
|
|
76
|
-
async appendFile(path, content,
|
|
100
|
+
async appendFile(path, content, options) {
|
|
77
101
|
const workspace = this.#requireWritable();
|
|
78
102
|
const relativePath = this.#toRelativePath(path);
|
|
79
|
-
const current = toShellContent(await workspace.readFile(relativePath, { encoding: "binary" }).catch(() =>
|
|
80
|
-
|
|
103
|
+
const current = toShellContent(await workspace.readFile(relativePath, { encoding: "binary" }).catch(async (error) => {
|
|
104
|
+
if (await workspace.exists(relativePath)) throw error;
|
|
105
|
+
return /* @__PURE__ */ new Uint8Array();
|
|
106
|
+
}));
|
|
107
|
+
const next = toShellContent(content, hasRuntimeType(options, "string") ? options : options?.encoding);
|
|
81
108
|
const merged = new Uint8Array(current.byteLength + next.byteLength);
|
|
82
109
|
merged.set(current, 0);
|
|
83
110
|
merged.set(next, current.byteLength);
|
|
@@ -87,21 +114,28 @@ var WorkspaceFileSystem = class {
|
|
|
87
114
|
async exists(path) {
|
|
88
115
|
try {
|
|
89
116
|
const relativePath = this.#toRelativePath(path);
|
|
117
|
+
if (!relativePath) return true;
|
|
90
118
|
return await this.workspace.exists(relativePath);
|
|
91
119
|
} catch {
|
|
92
120
|
return false;
|
|
93
121
|
}
|
|
94
122
|
}
|
|
95
123
|
async stat(path) {
|
|
96
|
-
const
|
|
97
|
-
if (
|
|
124
|
+
const relativePath = this.#toRelativePath(path);
|
|
125
|
+
if (!relativePath) return statFromEntry({
|
|
98
126
|
path: "",
|
|
99
127
|
type: "directory"
|
|
100
128
|
});
|
|
101
|
-
return statFromEntry(await this.workspace.stat(
|
|
129
|
+
return statFromEntry(await this.workspace.stat(relativePath));
|
|
102
130
|
}
|
|
103
131
|
async mkdir(path, options) {
|
|
104
|
-
|
|
132
|
+
const workspace = this.#requireWritable();
|
|
133
|
+
const relativePath = this.#toRelativePath(path);
|
|
134
|
+
if (!relativePath) {
|
|
135
|
+
if (options?.recursive) return;
|
|
136
|
+
throw shellErrorDiagnostics.SHELL_R0023({ message: `[vitehub] Workspace directory already exists: "${path}".` });
|
|
137
|
+
}
|
|
138
|
+
await workspace.mkdir(relativePath, { recursive: options?.recursive });
|
|
105
139
|
await this.#refreshPaths();
|
|
106
140
|
}
|
|
107
141
|
async readdir(path) {
|
|
@@ -133,8 +167,14 @@ var WorkspaceFileSystem = class {
|
|
|
133
167
|
}
|
|
134
168
|
async mv(src, dest) {
|
|
135
169
|
const workspace = this.#requireWritable();
|
|
136
|
-
const from = this.#toRelativePath(src);
|
|
137
|
-
|
|
170
|
+
const from = this.#toRelativePath(src).replace(/\/$/, "");
|
|
171
|
+
const to = this.#toRelativePath(dest).replace(/\/$/, "");
|
|
172
|
+
if (from === to) {
|
|
173
|
+
if (from) await workspace.stat(from);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (!from || to.startsWith(`${from}/`)) throw shellErrorDiagnostics.SHELL_R0022({ message: `[vitehub] Cannot move Workspace path "${src}" into itself, "${dest}".` });
|
|
177
|
+
await copyWorkspacePath(workspace, from, to);
|
|
138
178
|
await workspace.rm(from, {
|
|
139
179
|
recursive: true,
|
|
140
180
|
force: true
|
|
@@ -155,16 +195,16 @@ var WorkspaceFileSystem = class {
|
|
|
155
195
|
return [...this.#paths];
|
|
156
196
|
}
|
|
157
197
|
async chmod(_path, _mode) {
|
|
158
|
-
throw
|
|
198
|
+
throw shellErrorDiagnostics.SHELL_R0015({ message: "chmod is not supported by the workspace filesystem." });
|
|
159
199
|
}
|
|
160
200
|
async symlink(_target, _linkPath) {
|
|
161
|
-
throw
|
|
201
|
+
throw shellErrorDiagnostics.SHELL_R0016({ message: "symlink is not supported by the workspace filesystem." });
|
|
162
202
|
}
|
|
163
203
|
async link(_existingPath, _newPath) {
|
|
164
|
-
throw
|
|
204
|
+
throw shellErrorDiagnostics.SHELL_R0017({ message: "link is not supported by the workspace filesystem." });
|
|
165
205
|
}
|
|
166
206
|
async readlink(_path) {
|
|
167
|
-
throw
|
|
207
|
+
throw shellErrorDiagnostics.SHELL_R0018({ message: "readlink is not supported by the workspace filesystem." });
|
|
168
208
|
}
|
|
169
209
|
async lstat(path) {
|
|
170
210
|
return await this.stat(path);
|
|
@@ -173,7 +213,7 @@ var WorkspaceFileSystem = class {
|
|
|
173
213
|
return this.#resolveFromRoot(path);
|
|
174
214
|
}
|
|
175
215
|
async utimes(_path, _atime, _mtime) {
|
|
176
|
-
throw
|
|
216
|
+
throw shellErrorDiagnostics.SHELL_R0019({ message: "utimes is not supported by the workspace filesystem." });
|
|
177
217
|
}
|
|
178
218
|
#requireWritable() {
|
|
179
219
|
if (!this.writeFs) throw createReadonlyError();
|
|
@@ -192,18 +232,22 @@ var WorkspaceFileSystem = class {
|
|
|
192
232
|
#toRelativePath(path) {
|
|
193
233
|
const absolutePath = this.#resolveFromRoot(path);
|
|
194
234
|
if (absolutePath === "/workspace") return "";
|
|
195
|
-
return absolutePath.slice(`${workspaceMountPoint}/`.length);
|
|
235
|
+
return absolutePath.slice(`${workspaceMountPoint}/`.length).replace(/\/$/, "");
|
|
196
236
|
}
|
|
197
237
|
async #refreshPaths() {
|
|
198
|
-
this.#
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
238
|
+
this.#refreshAgain = true;
|
|
239
|
+
this.#refreshPromise ||= Promise.resolve().then(async () => {
|
|
240
|
+
try {
|
|
241
|
+
do {
|
|
242
|
+
this.#refreshAgain = false;
|
|
243
|
+
const entries = await this.workspace.list("", { recursive: true });
|
|
244
|
+
this.#paths = [workspaceMountPoint, ...entries.map((entry) => entry.path ? `${workspaceMountPoint}/${entry.path}` : workspaceMountPoint).sort((left, right) => left.localeCompare(right))];
|
|
245
|
+
} while (this.#refreshAgain);
|
|
246
|
+
} finally {
|
|
247
|
+
this.#refreshPromise = void 0;
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
await this.#refreshPromise;
|
|
207
251
|
}
|
|
208
252
|
};
|
|
209
253
|
function createReadonlyWorkspaceFs(workspace) {
|
|
@@ -216,7 +260,7 @@ function createWritableWorkspaceFs(workspace) {
|
|
|
216
260
|
//#region src/workspace/command-analysis.ts
|
|
217
261
|
function analyzeWorkspaceInspectionCommand(command) {
|
|
218
262
|
return splitShellCommandSegments(command).map((segment) => {
|
|
219
|
-
const words =
|
|
263
|
+
const words = parseShellCommand(segment.command, "inspection");
|
|
220
264
|
return {
|
|
221
265
|
...segment,
|
|
222
266
|
paths: shellPathArguments(words),
|
|
@@ -384,75 +428,6 @@ function pathArgumentsUntilShellBoundary(args) {
|
|
|
384
428
|
}
|
|
385
429
|
return paths;
|
|
386
430
|
}
|
|
387
|
-
function splitShellCommandSegments(command) {
|
|
388
|
-
const segments = [];
|
|
389
|
-
let current = "";
|
|
390
|
-
let quote;
|
|
391
|
-
let escaped = false;
|
|
392
|
-
let followsPipe = false;
|
|
393
|
-
for (let index = 0; index < command.length; index++) {
|
|
394
|
-
const char = command[index];
|
|
395
|
-
if (escaped) {
|
|
396
|
-
current += char;
|
|
397
|
-
escaped = false;
|
|
398
|
-
continue;
|
|
399
|
-
}
|
|
400
|
-
if (char === "\\") {
|
|
401
|
-
current += char;
|
|
402
|
-
escaped = true;
|
|
403
|
-
continue;
|
|
404
|
-
}
|
|
405
|
-
if (quote) {
|
|
406
|
-
if (char === quote) quote = void 0;
|
|
407
|
-
current += char;
|
|
408
|
-
continue;
|
|
409
|
-
}
|
|
410
|
-
if (char === "'" || char === "\"") {
|
|
411
|
-
quote = char;
|
|
412
|
-
current += char;
|
|
413
|
-
continue;
|
|
414
|
-
}
|
|
415
|
-
const next = command[index + 1];
|
|
416
|
-
if (char === "&" && next === "&") {
|
|
417
|
-
segments.push({
|
|
418
|
-
command: current,
|
|
419
|
-
followsPipe,
|
|
420
|
-
separatorAfter: "&&"
|
|
421
|
-
});
|
|
422
|
-
current = "";
|
|
423
|
-
followsPipe = false;
|
|
424
|
-
index += 1;
|
|
425
|
-
continue;
|
|
426
|
-
}
|
|
427
|
-
if (char === "|" && next === "|") {
|
|
428
|
-
segments.push({
|
|
429
|
-
command: current,
|
|
430
|
-
followsPipe,
|
|
431
|
-
separatorAfter: "||"
|
|
432
|
-
});
|
|
433
|
-
current = "";
|
|
434
|
-
followsPipe = false;
|
|
435
|
-
index += 1;
|
|
436
|
-
continue;
|
|
437
|
-
}
|
|
438
|
-
if (char === "|" || char === ";" || char === "\n") {
|
|
439
|
-
segments.push({
|
|
440
|
-
command: current,
|
|
441
|
-
followsPipe,
|
|
442
|
-
separatorAfter: char
|
|
443
|
-
});
|
|
444
|
-
current = "";
|
|
445
|
-
followsPipe = char === "|";
|
|
446
|
-
continue;
|
|
447
|
-
}
|
|
448
|
-
current += char;
|
|
449
|
-
}
|
|
450
|
-
segments.push({
|
|
451
|
-
command: current,
|
|
452
|
-
followsPipe
|
|
453
|
-
});
|
|
454
|
-
return segments;
|
|
455
|
-
}
|
|
456
431
|
function hasRecursiveGrepFlag(words) {
|
|
457
432
|
const args = words.slice(1);
|
|
458
433
|
for (let index = 0; index < args.length; index++) {
|
|
@@ -472,76 +447,58 @@ function hasRecursiveGrepFlag(words) {
|
|
|
472
447
|
}
|
|
473
448
|
return false;
|
|
474
449
|
}
|
|
475
|
-
function parseShellWords(command) {
|
|
476
|
-
const words = [];
|
|
477
|
-
let current = "";
|
|
478
|
-
let quote;
|
|
479
|
-
let escaped = false;
|
|
480
|
-
for (const char of command) {
|
|
481
|
-
if (escaped) {
|
|
482
|
-
current += char;
|
|
483
|
-
escaped = false;
|
|
484
|
-
continue;
|
|
485
|
-
}
|
|
486
|
-
if (char === "\\") {
|
|
487
|
-
escaped = true;
|
|
488
|
-
continue;
|
|
489
|
-
}
|
|
490
|
-
if (quote) {
|
|
491
|
-
if (char === quote) quote = void 0;
|
|
492
|
-
else current += char;
|
|
493
|
-
continue;
|
|
494
|
-
}
|
|
495
|
-
if (char === "'" || char === "\"") {
|
|
496
|
-
quote = char;
|
|
497
|
-
continue;
|
|
498
|
-
}
|
|
499
|
-
if (/\s/.test(char)) {
|
|
500
|
-
if (current) {
|
|
501
|
-
words.push(current);
|
|
502
|
-
current = "";
|
|
503
|
-
}
|
|
504
|
-
continue;
|
|
505
|
-
}
|
|
506
|
-
current += char;
|
|
507
|
-
}
|
|
508
|
-
if (current) words.push(current);
|
|
509
|
-
return words;
|
|
510
|
-
}
|
|
511
450
|
//#endregion
|
|
512
451
|
//#region src/workspace/path.ts
|
|
513
452
|
function cleanWorkspaceShellPath(path = ".") {
|
|
514
|
-
const
|
|
515
|
-
if (
|
|
516
|
-
return normalizeSafeShellPath(
|
|
453
|
+
const input = path || ".";
|
|
454
|
+
if (input === "." || input === "./" || input === "/" || input === "/workspace") return "";
|
|
455
|
+
return normalizeSafeShellPath(input.replace(/^\/workspace(\/|$)/, ""));
|
|
517
456
|
}
|
|
518
457
|
function cleanWorkspaceMutationPath(path) {
|
|
519
458
|
const normalized = cleanWorkspaceShellPath(path);
|
|
520
|
-
if (!normalized) throw
|
|
459
|
+
if (!normalized) throw shellErrorDiagnostics.SHELL_R0020({ message: "[vitehub] Workspace root is not a valid mutation target." });
|
|
521
460
|
return normalized;
|
|
522
461
|
}
|
|
523
462
|
function normalizeSafeShellPath(path) {
|
|
524
463
|
const normalized = posix.normalize(path.replace(/\\/g, "/")).replace(/^\//, "");
|
|
525
464
|
if (normalized === ".") return "";
|
|
526
|
-
if (normalized === ".." || normalized.startsWith("../")) throw
|
|
465
|
+
if (normalized === ".." || normalized.startsWith("../")) throw shellErrorDiagnostics.SHELL_R0021({ message: `[vitehub] Workspace path escapes the workspace root: "${path}".` });
|
|
527
466
|
return normalized;
|
|
528
467
|
}
|
|
529
468
|
//#endregion
|
|
530
469
|
//#region src/workspace/inspection.ts
|
|
531
470
|
async function runWorkspaceInspectionCommand(_input, command, options) {
|
|
532
|
-
|
|
533
|
-
const
|
|
471
|
+
throwIfAborted(options.signal);
|
|
472
|
+
const maxOutputLength = options.maxOutputLength === void 0 ? 3e4 : normalizeOutputLength(options.maxOutputLength);
|
|
473
|
+
const timeout = options.timeout ?? 3e4;
|
|
534
474
|
const provider = options.networkGrants && usesWorkspaceNetworkGrant(command) ? void 0 : options.provider;
|
|
475
|
+
const chargeRejected = () => {
|
|
476
|
+
if (!options.budgetCharged) options.budget?.recordToolCall(new TextEncoder().encode(command).byteLength);
|
|
477
|
+
};
|
|
535
478
|
const unsupportedSyntax = preflightUnsupportedWorkspaceSyntax(command, options.commands, Boolean(provider));
|
|
536
|
-
if (unsupportedSyntax)
|
|
537
|
-
|
|
538
|
-
|
|
479
|
+
if (unsupportedSyntax) {
|
|
480
|
+
chargeRejected();
|
|
481
|
+
return limitInspectionObservation(unsupportedSyntax, maxOutputLength);
|
|
482
|
+
}
|
|
483
|
+
const preflight = await withInspectionTimeout(timeout, options.signal, async (signal) => {
|
|
484
|
+
const broad = await preflightWorkspaceInspectionCommand(command, options.fs, options.broadSearchPaths, options.cwd, signal);
|
|
485
|
+
if (broad) return broad;
|
|
486
|
+
return await preflightMissingWorkspacePath(command, options.fs, options.cwd, signal);
|
|
487
|
+
}, command, options.cwd);
|
|
488
|
+
if (preflight) {
|
|
489
|
+
chargeRejected();
|
|
490
|
+
return limitInspectionObservation(preflight, maxOutputLength);
|
|
491
|
+
}
|
|
539
492
|
const pipedBroadSearch = preflightPipedSearchWithoutPath(command, options.broadSearchPaths);
|
|
540
|
-
if (pipedBroadSearch)
|
|
541
|
-
|
|
542
|
-
|
|
493
|
+
if (pipedBroadSearch) {
|
|
494
|
+
chargeRejected();
|
|
495
|
+
return limitInspectionObservation(pipedBroadSearch, maxOutputLength);
|
|
496
|
+
}
|
|
543
497
|
const unsupported = preflightUnsupportedWorkspaceCommand(command, options.commands, Boolean(provider));
|
|
544
|
-
if (unsupported)
|
|
498
|
+
if (unsupported) {
|
|
499
|
+
chargeRejected();
|
|
500
|
+
return limitInspectionObservation(unsupported, maxOutputLength);
|
|
501
|
+
}
|
|
545
502
|
const runtime = createShellRuntime({
|
|
546
503
|
policy: {
|
|
547
504
|
maxOutputLength,
|
|
@@ -556,16 +513,87 @@ async function runWorkspaceInspectionCommand(_input, command, options) {
|
|
|
556
513
|
});
|
|
557
514
|
const cwd = options.cwd || "/workspace";
|
|
558
515
|
const result = await runtime.exec(command, {
|
|
516
|
+
budget: options.budget,
|
|
517
|
+
budgetCharged: options.budgetCharged,
|
|
559
518
|
cwd,
|
|
560
519
|
timeout,
|
|
520
|
+
signal: options.signal,
|
|
561
521
|
workspacePaths: workspaceSessionPaths(command, cwd)
|
|
562
522
|
});
|
|
563
523
|
const noMatchFeedback = searchNoMatchFeedback(command, result, options.broadSearchPaths, options.cwd);
|
|
564
|
-
|
|
524
|
+
if (!noMatchFeedback) return result;
|
|
525
|
+
const bounded = limitInspectionFeedback(noMatchFeedback, maxOutputLength);
|
|
526
|
+
return {
|
|
565
527
|
...result,
|
|
566
|
-
stdout:
|
|
528
|
+
stdout: bounded.stdout,
|
|
529
|
+
outputTruncated: result.outputTruncated || bounded.outputTruncated,
|
|
567
530
|
workspaceGuardrail: { kind: "no_match" }
|
|
568
|
-
}
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
function limitInspectionFeedback(feedback, maxLength) {
|
|
534
|
+
if (maxLength === void 0 || feedback.length <= maxLength) return {
|
|
535
|
+
stdout: feedback,
|
|
536
|
+
outputTruncated: false
|
|
537
|
+
};
|
|
538
|
+
return {
|
|
539
|
+
stdout: `${feedback.slice(0, maxLength)}\n[output truncated to ${maxLength} characters]\n`,
|
|
540
|
+
outputTruncated: true
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
function limitInspectionObservation(observation, maxLength) {
|
|
544
|
+
const bounded = limitInspectionFeedback(observation.stdout, maxLength);
|
|
545
|
+
const boundedError = limitInspectionFeedback(observation.stderr, maxLength);
|
|
546
|
+
if (!bounded.outputTruncated && !boundedError.outputTruncated) return observation;
|
|
547
|
+
return {
|
|
548
|
+
...observation,
|
|
549
|
+
stdout: bounded.stdout,
|
|
550
|
+
stderr: boundedError.stdout,
|
|
551
|
+
outputTruncated: true
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
function normalizeOutputLength(value) {
|
|
555
|
+
if (value === void 0) return void 0;
|
|
556
|
+
if (value === Number.POSITIVE_INFINITY) return void 0;
|
|
557
|
+
return Number.isFinite(value) ? Math.max(0, Math.floor(value)) : 0;
|
|
558
|
+
}
|
|
559
|
+
async function withInspectionTimeout(timeout, callerSignal, run, command, cwd) {
|
|
560
|
+
const controller = new AbortController();
|
|
561
|
+
const signal = callerSignal ? AbortSignal.any([callerSignal, controller.signal]) : controller.signal;
|
|
562
|
+
if (!Number.isFinite(timeout) || timeout < 0) return await run(signal);
|
|
563
|
+
let timer;
|
|
564
|
+
let removeCallerAbortListener;
|
|
565
|
+
try {
|
|
566
|
+
const callerCancellation = callerSignal ? new Promise((_, reject) => {
|
|
567
|
+
const rejectOnAbort = () => reject(callerSignal.reason);
|
|
568
|
+
if (callerSignal.aborted) rejectOnAbort();
|
|
569
|
+
else {
|
|
570
|
+
callerSignal.addEventListener("abort", rejectOnAbort, { once: true });
|
|
571
|
+
removeCallerAbortListener = () => callerSignal.removeEventListener("abort", rejectOnAbort);
|
|
572
|
+
}
|
|
573
|
+
}) : void 0;
|
|
574
|
+
return await Promise.race([
|
|
575
|
+
run(signal),
|
|
576
|
+
...callerCancellation ? [callerCancellation] : [],
|
|
577
|
+
new Promise((resolve) => {
|
|
578
|
+
timer = setTimeout(() => {
|
|
579
|
+
resolve({
|
|
580
|
+
command,
|
|
581
|
+
cwd,
|
|
582
|
+
event: "command_timed_out",
|
|
583
|
+
exitCode: null,
|
|
584
|
+
stderr: `[vitehub] Workspace shell command timed out after ${timeout}ms.`,
|
|
585
|
+
stdout: "",
|
|
586
|
+
timedOut: true,
|
|
587
|
+
workspaceGuardrail: { kind: "timeout" }
|
|
588
|
+
});
|
|
589
|
+
controller.abort(/* @__PURE__ */ new Error("Workspace inspection preflight timed out."));
|
|
590
|
+
}, timeout);
|
|
591
|
+
})
|
|
592
|
+
]);
|
|
593
|
+
} finally {
|
|
594
|
+
if (timer) clearTimeout(timer);
|
|
595
|
+
removeCallerAbortListener?.();
|
|
596
|
+
}
|
|
569
597
|
}
|
|
570
598
|
function workspaceSessionPaths(command, cwd = workspaceMountPoint) {
|
|
571
599
|
try {
|
|
@@ -606,75 +634,72 @@ function usesWorkspaceNetworkGrant(command) {
|
|
|
606
634
|
return false;
|
|
607
635
|
}
|
|
608
636
|
}
|
|
609
|
-
async function preflightWorkspaceInspectionCommand(command, fs, broadSearchPaths = [], cwd = workspaceMountPoint) {
|
|
637
|
+
async function preflightWorkspaceInspectionCommand(command, fs, broadSearchPaths = [], cwd = workspaceMountPoint, signal) {
|
|
610
638
|
try {
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
for (const segment of analyzeWorkspaceInspectionCommand(command)) {
|
|
615
|
-
if (skipAndChain) {
|
|
616
|
-
skipAndChain = segment.separatorAfter === "&&";
|
|
617
|
-
continue;
|
|
618
|
-
}
|
|
619
|
-
if (skipOrChain) {
|
|
620
|
-
skipOrChain = segment.separatorAfter === "||";
|
|
621
|
-
continue;
|
|
622
|
-
}
|
|
623
|
-
const words = segment.words;
|
|
624
|
-
if (words[0] === "cd") {
|
|
625
|
-
const path = words[1] || "/workspace";
|
|
626
|
-
if (!isWorkspacePathCandidate(path)) continue;
|
|
627
|
-
const resolvedPath = resolveWorkspaceShellPath(currentCwd, path);
|
|
628
|
-
const directory = await workspacePathIsDirectory(fs, resolvedPath);
|
|
629
|
-
if (segment.separatorAfter === "&&" && !directory) skipAndChain = true;
|
|
630
|
-
if (segment.separatorAfter === "||" && directory) skipOrChain = true;
|
|
631
|
-
if (directory) currentCwd = resolvedPath ? posix.join(workspaceMountPoint, resolvedPath) : workspaceMountPoint;
|
|
632
|
-
continue;
|
|
633
|
-
}
|
|
634
|
-
if (isBroadWorkspaceSearch(segment, broadSearchPaths, currentCwd)) return broadWorkspaceSearchFeedback(broadSearchPaths);
|
|
639
|
+
for await (const { segment, cwd: segmentCwd } of walkWorkspaceInspectionCommand(command, fs, cwd, signal)) {
|
|
640
|
+
throwIfAborted(signal);
|
|
641
|
+
if (isBroadWorkspaceSearch(segment, broadSearchPaths, segmentCwd)) return broadWorkspaceSearchFeedback(broadSearchPaths);
|
|
635
642
|
}
|
|
636
643
|
} catch {
|
|
644
|
+
throwIfAborted(signal);
|
|
637
645
|
return;
|
|
638
646
|
}
|
|
639
647
|
}
|
|
640
|
-
async function preflightMissingWorkspacePath(command, fs, cwd = workspaceMountPoint) {
|
|
648
|
+
async function preflightMissingWorkspacePath(command, fs, cwd = workspaceMountPoint, signal) {
|
|
641
649
|
try {
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
let skipOrChain = false;
|
|
645
|
-
for (const segment of analyzeWorkspaceInspectionCommand(command)) {
|
|
646
|
-
if (skipAndChain) {
|
|
647
|
-
skipAndChain = segment.separatorAfter === "&&";
|
|
648
|
-
continue;
|
|
649
|
-
}
|
|
650
|
-
if (skipOrChain) {
|
|
651
|
-
skipOrChain = segment.separatorAfter === "||";
|
|
652
|
-
continue;
|
|
653
|
-
}
|
|
654
|
-
const words = segment.words;
|
|
655
|
-
if (words[0] === "cd") {
|
|
656
|
-
const path = words[1] || "/workspace";
|
|
657
|
-
if (!isWorkspacePathCandidate(path)) continue;
|
|
658
|
-
const resolvedPath = resolveWorkspaceShellPath(currentCwd, path);
|
|
659
|
-
const directory = await workspacePathIsDirectory(fs, resolvedPath);
|
|
660
|
-
if (segment.separatorAfter === "&&" && !directory) skipAndChain = true;
|
|
661
|
-
if (segment.separatorAfter === "||" && directory) skipOrChain = true;
|
|
662
|
-
if (directory) currentCwd = resolvedPath ? posix.join(workspaceMountPoint, resolvedPath) : workspaceMountPoint;
|
|
663
|
-
continue;
|
|
664
|
-
}
|
|
650
|
+
for await (const { segment, cwd: segmentCwd } of walkWorkspaceInspectionCommand(command, fs, cwd, signal)) {
|
|
651
|
+
throwIfAborted(signal);
|
|
665
652
|
for (const path of segment.paths) {
|
|
666
653
|
if (!isConcreteWorkspacePath(path)) continue;
|
|
667
|
-
const resolvedPath = resolveWorkspaceShellPath(
|
|
668
|
-
|
|
654
|
+
const resolvedPath = resolveWorkspaceShellPath(segmentCwd, path);
|
|
655
|
+
const exists = await fs.exists(resolvedPath);
|
|
656
|
+
throwIfAborted(signal);
|
|
657
|
+
if (exists) continue;
|
|
669
658
|
if (segment.separatorAfter === "||") continue;
|
|
670
659
|
return missingWorkspacePathFeedback(command, resolvedPath);
|
|
671
660
|
}
|
|
672
661
|
if (segment.separatorAfter === "&&" || segment.separatorAfter === "||") return void 0;
|
|
673
662
|
}
|
|
674
663
|
} catch {
|
|
664
|
+
throwIfAborted(signal);
|
|
675
665
|
return;
|
|
676
666
|
}
|
|
677
667
|
}
|
|
668
|
+
async function* walkWorkspaceInspectionCommand(command, fs, cwd, signal) {
|
|
669
|
+
let currentCwd = cwd;
|
|
670
|
+
let skipAndChain = false;
|
|
671
|
+
let skipOrChain = false;
|
|
672
|
+
for (const segment of analyzeWorkspaceInspectionCommand(command)) {
|
|
673
|
+
throwIfAborted(signal);
|
|
674
|
+
if (skipAndChain) {
|
|
675
|
+
skipAndChain = segment.separatorAfter === "&&";
|
|
676
|
+
continue;
|
|
677
|
+
}
|
|
678
|
+
if (skipOrChain) {
|
|
679
|
+
skipOrChain = segment.separatorAfter === "||";
|
|
680
|
+
continue;
|
|
681
|
+
}
|
|
682
|
+
const words = segment.words;
|
|
683
|
+
if (words[0] === "cd") {
|
|
684
|
+
const path = words[1] || "/workspace";
|
|
685
|
+
if (!isWorkspacePathCandidate(path)) continue;
|
|
686
|
+
const resolvedPath = resolveWorkspaceShellPath(currentCwd, path);
|
|
687
|
+
const directory = await workspacePathIsDirectory(fs, resolvedPath);
|
|
688
|
+
throwIfAborted(signal);
|
|
689
|
+
if (segment.separatorAfter === "&&" && !directory) skipAndChain = true;
|
|
690
|
+
if (segment.separatorAfter === "||" && directory) skipOrChain = true;
|
|
691
|
+
if (directory) currentCwd = resolvedPath ? posix.join(workspaceMountPoint, resolvedPath) : workspaceMountPoint;
|
|
692
|
+
continue;
|
|
693
|
+
}
|
|
694
|
+
yield {
|
|
695
|
+
segment,
|
|
696
|
+
cwd: currentCwd
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
function throwIfAborted(signal) {
|
|
701
|
+
signal?.throwIfAborted();
|
|
702
|
+
}
|
|
678
703
|
async function workspacePathIsDirectory(fs, path) {
|
|
679
704
|
if (path === "") return true;
|
|
680
705
|
try {
|
|
@@ -748,7 +773,10 @@ function searchNoMatchFeedback(command, result, broadSearchPaths = [], cwd = wor
|
|
|
748
773
|
if (segment.separatorAfter === "&&") return "";
|
|
749
774
|
continue;
|
|
750
775
|
}
|
|
751
|
-
if (!segment.paths.some((path) =>
|
|
776
|
+
if (!segment.paths.some((path) => {
|
|
777
|
+
const resolved = cleanWorkspaceShellPath(resolvedWorkspaceCwd(currentCwd, path));
|
|
778
|
+
return isBroadWorkspacePath(path, broadSearchPaths, currentCwd) || broadSearchPaths.some((root) => resolved.startsWith(`${cleanWorkspaceShellPath(root)}/`));
|
|
779
|
+
})) {
|
|
752
780
|
if (segment.separatorAfter === "&&") return "";
|
|
753
781
|
continue;
|
|
754
782
|
}
|
|
@@ -764,7 +792,7 @@ function preflightUnsupportedWorkspaceCommand(command, commands, strictProvider
|
|
|
764
792
|
if (syntax) return syntax;
|
|
765
793
|
if (!strictProvider && usesCompoundShellSyntax(command)) return void 0;
|
|
766
794
|
if (!commands) return void 0;
|
|
767
|
-
const allowed = new Set([
|
|
795
|
+
const allowed = /* @__PURE__ */ new Set([
|
|
768
796
|
...commands,
|
|
769
797
|
"cd",
|
|
770
798
|
"false",
|
|
@@ -850,7 +878,7 @@ function isCompoundShellSyntaxWord(word) {
|
|
|
850
878
|
function isAssignmentWord(word) {
|
|
851
879
|
return /^[A-Za-z_][A-Za-z0-9_]*=.*/.test(word);
|
|
852
880
|
}
|
|
853
|
-
const shellSyntaxWords = new Set([
|
|
881
|
+
const shellSyntaxWords = /* @__PURE__ */ new Set([
|
|
854
882
|
"case",
|
|
855
883
|
"do",
|
|
856
884
|
"done",
|