@robota-sdk/agent-tools 3.0.0-beta.8 → 3.0.0-beta.81
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 +661 -21
- package/README.md +140 -31
- package/dist/browser/browser.d.ts +67 -0
- package/dist/browser/browser.d.ts.map +1 -0
- package/dist/browser/browser.js +2 -0
- package/dist/browser/browser.js.map +1 -0
- package/dist/node/index.cjs +3348 -1463
- package/dist/node/index.d.cts +984 -307
- package/dist/node/index.d.cts.map +1 -0
- package/dist/node/index.d.ts +984 -307
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.js +3286 -1419
- package/dist/node/index.js.map +1 -0
- package/package.json +52 -19
package/dist/node/index.cjs
CHANGED
|
@@ -1,1494 +1,3379 @@
|
|
|
1
|
-
"
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
2
3
|
var __create = Object.create;
|
|
3
4
|
var __defProp = Object.defineProperty;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
9
|
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
//#endregion
|
|
24
|
+
let node_fs_promises = require("node:fs/promises");
|
|
25
|
+
let node_path = require("node:path");
|
|
26
|
+
let node_child_process = require("node:child_process");
|
|
27
|
+
let node_crypto = require("node:crypto");
|
|
28
|
+
let node_fs = require("node:fs");
|
|
29
|
+
let node_os = require("node:os");
|
|
30
|
+
let _robota_sdk_agent_core = require("@robota-sdk/agent-core");
|
|
31
|
+
let zod = require("zod");
|
|
32
|
+
let _robota_sdk_agent_process = require("@robota-sdk/agent-process");
|
|
33
|
+
let _robota_sdk_agent_core_node = require("@robota-sdk/agent-core/node");
|
|
34
|
+
let fast_glob = require("fast-glob");
|
|
35
|
+
fast_glob = __toESM(fast_glob, 1);
|
|
36
|
+
let p_limit = require("p-limit");
|
|
37
|
+
p_limit = __toESM(p_limit, 1);
|
|
38
|
+
let node_events = require("node:events");
|
|
39
|
+
let node_worker_threads = require("node:worker_threads");
|
|
40
|
+
//#region src/sandbox/e2b-sandbox-client.ts
|
|
41
|
+
var E2BSandboxClient = class {
|
|
42
|
+
sandbox;
|
|
43
|
+
connectSandbox;
|
|
44
|
+
createSandboxFromSnapshot;
|
|
45
|
+
constructor(options) {
|
|
46
|
+
this.sandbox = options.sandbox;
|
|
47
|
+
this.connectSandbox = options.connectSandbox;
|
|
48
|
+
this.createSandboxFromSnapshot = options.createSandboxFromSnapshot;
|
|
49
|
+
}
|
|
50
|
+
async run(command, options) {
|
|
51
|
+
const result = await this.sandbox.commands.run(command, {
|
|
52
|
+
background: false,
|
|
53
|
+
timeoutMs: options?.timeoutMs,
|
|
54
|
+
cwd: options?.workingDirectory
|
|
55
|
+
});
|
|
56
|
+
return {
|
|
57
|
+
stdout: result.stdout ?? "",
|
|
58
|
+
stderr: result.stderr ?? "",
|
|
59
|
+
exitCode: result.exitCode ?? result.exit_code ?? 0
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
async readFile(path) {
|
|
63
|
+
const content = await this.sandbox.files.read(path);
|
|
64
|
+
return typeof content === "string" ? content : Buffer.from(content).toString("utf8");
|
|
65
|
+
}
|
|
66
|
+
async writeFile(path, content) {
|
|
67
|
+
await this.sandbox.files.write(path, content);
|
|
68
|
+
}
|
|
69
|
+
async snapshot() {
|
|
70
|
+
if (this.sandbox.createSnapshot) {
|
|
71
|
+
const snapshot = await this.sandbox.createSnapshot();
|
|
72
|
+
const snapshotId = snapshot.snapshotId ?? snapshot.id;
|
|
73
|
+
if (!snapshotId) throw new Error("E2B createSnapshot() did not return a snapshot id.");
|
|
74
|
+
return snapshotId;
|
|
75
|
+
}
|
|
76
|
+
const sandboxId = this.sandbox.sandboxId;
|
|
77
|
+
if (!sandboxId) throw new Error("E2B sandboxId is required to create a resumable sandbox snapshot.");
|
|
78
|
+
if (!this.sandbox.pause) throw new Error("E2B sandbox adapter does not expose pause().");
|
|
79
|
+
await this.sandbox.pause();
|
|
80
|
+
return sandboxId;
|
|
81
|
+
}
|
|
82
|
+
async restore(snapshotId) {
|
|
83
|
+
if (this.createSandboxFromSnapshot) {
|
|
84
|
+
this.sandbox = await this.createSandboxFromSnapshot(snapshotId);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (this.connectSandbox) {
|
|
88
|
+
this.sandbox = await this.connectSandbox(snapshotId);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (this.sandbox.sandboxId === snapshotId && this.sandbox.connect) {
|
|
92
|
+
this.sandbox = await this.sandbox.connect();
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
throw new Error("E2B sandbox restore requires connectSandbox(snapshotId) or sandbox.connect().");
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/sandbox/in-memory-sandbox-client.ts
|
|
100
|
+
var InMemorySandboxClient = class {
|
|
101
|
+
files = /* @__PURE__ */ new Map();
|
|
102
|
+
snapshots = /* @__PURE__ */ new Map();
|
|
103
|
+
runHandler;
|
|
104
|
+
snapshotSequence = 0;
|
|
105
|
+
constructor(options = {}) {
|
|
106
|
+
for (const [path, content] of Object.entries(options.files ?? {})) this.files.set(path, content);
|
|
107
|
+
this.runHandler = options.runHandler;
|
|
108
|
+
}
|
|
109
|
+
async run(command, options) {
|
|
110
|
+
if (this.runHandler) return this.runHandler(command, options, this.files);
|
|
111
|
+
return {
|
|
112
|
+
stdout: "",
|
|
113
|
+
stderr: "",
|
|
114
|
+
exitCode: 0
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
async readFile(path) {
|
|
118
|
+
const content = this.files.get(path);
|
|
119
|
+
if (content === void 0) throw new Error(`Sandbox file not found: ${path}`);
|
|
120
|
+
return content;
|
|
121
|
+
}
|
|
122
|
+
async writeFile(path, content) {
|
|
123
|
+
this.files.set(path, content);
|
|
124
|
+
}
|
|
125
|
+
async snapshot() {
|
|
126
|
+
const snapshotId = `snapshot-${++this.snapshotSequence}`;
|
|
127
|
+
this.snapshots.set(snapshotId, new Map(this.files));
|
|
128
|
+
return snapshotId;
|
|
129
|
+
}
|
|
130
|
+
async restore(snapshotId) {
|
|
131
|
+
const snapshot = this.snapshots.get(snapshotId);
|
|
132
|
+
if (!snapshot) throw new Error(`Sandbox snapshot not found: ${snapshotId}`);
|
|
133
|
+
this.files.clear();
|
|
134
|
+
for (const [path, content] of snapshot.entries()) this.files.set(path, content);
|
|
135
|
+
}
|
|
136
|
+
getFile(path) {
|
|
137
|
+
return this.files.get(path);
|
|
138
|
+
}
|
|
19
139
|
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
140
|
+
//#endregion
|
|
141
|
+
//#region src/sandbox/containment.ts
|
|
142
|
+
function describeExecutionContainment(client) {
|
|
143
|
+
if (client === void 0) return "host";
|
|
144
|
+
return `sandbox-${client.filesystem ?? "separate"}`;
|
|
145
|
+
}
|
|
146
|
+
/** Whether file tools must read and write through the sandbox rather than the host filesystem. */
|
|
147
|
+
function routesFilesThroughSandbox(client) {
|
|
148
|
+
return describeExecutionContainment(client) === "sandbox-separate";
|
|
149
|
+
}
|
|
150
|
+
//#endregion
|
|
151
|
+
//#region src/sandbox/manifest-enforceability.ts
|
|
152
|
+
/**
|
|
153
|
+
* Refuse a manifest whose security-bearing fields the built-in applicator cannot enforce.
|
|
154
|
+
*
|
|
155
|
+
* Emptiness is what is checked, not presence: `environment: {}` and `permissions: {}` request
|
|
156
|
+
* nothing, so refusing them would fail a caller that asked for no controls at all. `permissions`
|
|
157
|
+
* counts as empty when neither list has an entry — `{ read: [] }` is a declared-but-empty policy,
|
|
158
|
+
* not a policy.
|
|
159
|
+
*/
|
|
160
|
+
function refuseUnenforceableManifestControls(manifest) {
|
|
161
|
+
const unenforceable = [];
|
|
162
|
+
if (manifest.environment && Object.keys(manifest.environment).length > 0) unenforceable.push("environment");
|
|
163
|
+
const permissions = manifest.permissions;
|
|
164
|
+
const requestsSomething = (value) => Array.isArray(value) ? value.length > 0 : value !== void 0;
|
|
165
|
+
if (permissions && Object.values(permissions).some(requestsSomething)) unenforceable.push("permissions");
|
|
166
|
+
if (unenforceable.length === 0) return;
|
|
167
|
+
throw new Error(`workspace manifest requests ${unenforceable.join(" and ")}, which this sandbox client cannot enforce. The built-in applicator applies entries only. Supply a sandbox client that implements applyManifest and honours these fields, or remove them from the manifest — they were previously accepted and silently ignored, which reported a sandbox policy that was never applied (issue #2027).`);
|
|
168
|
+
}
|
|
169
|
+
//#endregion
|
|
170
|
+
//#region src/sandbox/workspace-manifest.ts
|
|
171
|
+
const DEFAULT_TARGET_ROOT = "/workspace";
|
|
172
|
+
const WINDOWS_ABSOLUTE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;
|
|
173
|
+
const SHELL_QUOTE_PATTERN = /'/g;
|
|
174
|
+
async function applyWorkspaceManifest(sandboxClient, manifest, options = {}) {
|
|
175
|
+
if (sandboxClient.applyManifest) return sandboxClient.applyManifest(manifest, options);
|
|
176
|
+
refuseUnenforceableManifestControls(manifest);
|
|
177
|
+
const targetRoot = normalizeSandboxRoot(options.targetRoot ?? DEFAULT_TARGET_ROOT);
|
|
178
|
+
const appliedEntries = [];
|
|
179
|
+
for (const [rawPath, entry] of Object.entries(manifest.entries)) {
|
|
180
|
+
const path = validateWorkspaceManifestPath(rawPath);
|
|
181
|
+
const targetPath = joinSandboxPath(targetRoot, path);
|
|
182
|
+
appliedEntries.push(await applyManifestEntry(sandboxClient, path, targetPath, targetRoot, entry, options));
|
|
183
|
+
}
|
|
184
|
+
return { entries: appliedEntries };
|
|
185
|
+
}
|
|
186
|
+
function validateWorkspaceManifestPath(path) {
|
|
187
|
+
if (path.length === 0) throw new Error("workspace manifest path must not be empty");
|
|
188
|
+
if (path.includes("\0")) throw new Error("workspace manifest path must not contain NUL bytes");
|
|
189
|
+
if (path.startsWith("/") || path.startsWith("\\") || WINDOWS_ABSOLUTE_PATH_PATTERN.test(path)) throw new Error("workspace manifest path must be workspace-relative");
|
|
190
|
+
const parts = path.replace(/\\/g, "/").split("/").filter(Boolean);
|
|
191
|
+
if (parts.length === 0) throw new Error("workspace manifest path must not resolve to the workspace root");
|
|
192
|
+
if (parts.some((part) => part === "..")) throw new Error("workspace manifest path cannot contain traversal segments");
|
|
193
|
+
const normalizedParts = parts.filter((part) => part !== ".");
|
|
194
|
+
if (normalizedParts.length === 0) throw new Error("workspace manifest path must not resolve to the workspace root");
|
|
195
|
+
return normalizedParts.join("/");
|
|
196
|
+
}
|
|
197
|
+
async function applyManifestEntry(sandboxClient, path, targetPath, targetRoot, entry, options) {
|
|
198
|
+
switch (entry.type) {
|
|
199
|
+
case "file":
|
|
200
|
+
await writeSandboxFile(sandboxClient, targetPath, targetRoot, entry.content);
|
|
201
|
+
return createAppliedEntry(path, entry.type);
|
|
202
|
+
case "dir":
|
|
203
|
+
await createSandboxDirectory(sandboxClient, targetPath);
|
|
204
|
+
return createAppliedEntry(path, entry.type);
|
|
205
|
+
case "localFile":
|
|
206
|
+
await copyLocalFile(sandboxClient, entry.src, targetPath, targetRoot, options);
|
|
207
|
+
return createAppliedEntry(path, entry.type);
|
|
208
|
+
case "localDir":
|
|
209
|
+
await copyLocalDirectory(sandboxClient, entry.src, targetPath, options);
|
|
210
|
+
return createAppliedEntry(path, entry.type);
|
|
211
|
+
case "gitRepo":
|
|
212
|
+
await cloneGitRepository(sandboxClient, entry, targetPath);
|
|
213
|
+
return createAppliedEntry(path, entry.type);
|
|
214
|
+
case "s3Mount":
|
|
215
|
+
case "gcsMount":
|
|
216
|
+
case "r2Mount":
|
|
217
|
+
case "azureBlobMount": return {
|
|
218
|
+
path,
|
|
219
|
+
type: entry.type,
|
|
220
|
+
status: "unsupported",
|
|
221
|
+
message: `${entry.type} requires a provider-specific sandbox adapter.`
|
|
222
|
+
};
|
|
223
|
+
default: return assertUnreachable(entry);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
function createAppliedEntry(path, type) {
|
|
227
|
+
return {
|
|
228
|
+
path,
|
|
229
|
+
type,
|
|
230
|
+
status: "applied"
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
async function copyLocalFile(sandboxClient, source, targetPath, targetRoot, options) {
|
|
234
|
+
await writeSandboxFile(sandboxClient, targetPath, targetRoot, await (0, node_fs_promises.readFile)(resolveHostSourcePath(source, options.hostRoot), "utf8"));
|
|
235
|
+
}
|
|
236
|
+
async function copyLocalDirectory(sandboxClient, source, targetPath, options) {
|
|
237
|
+
await copyLocalDirectoryRecursive(sandboxClient, resolveHostSourcePath(source, options.hostRoot), targetPath);
|
|
238
|
+
}
|
|
239
|
+
async function copyLocalDirectoryRecursive(sandboxClient, sourcePath, targetPath) {
|
|
240
|
+
await createSandboxDirectory(sandboxClient, targetPath);
|
|
241
|
+
const entries = await (0, node_fs_promises.readdir)(sourcePath, { withFileTypes: true });
|
|
242
|
+
for (const entry of entries) {
|
|
243
|
+
const childSourcePath = (0, node_path.join)(sourcePath, entry.name);
|
|
244
|
+
const childTargetPath = joinSandboxPath(targetPath, entry.name);
|
|
245
|
+
if (entry.isDirectory()) {
|
|
246
|
+
await copyLocalDirectoryRecursive(sandboxClient, childSourcePath, childTargetPath);
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
if (entry.isFile()) {
|
|
250
|
+
const content = await (0, node_fs_promises.readFile)(childSourcePath, "utf8");
|
|
251
|
+
await sandboxClient.writeFile(childTargetPath, content);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
async function cloneGitRepository(sandboxClient, entry, targetPath) {
|
|
256
|
+
await runSandboxCommand(sandboxClient, `git clone${entry.shallow === false ? "" : " --depth 1"}${entry.ref ? ` --branch ${quoteShellArg(entry.ref)}` : ""} ${quoteShellArg(entry.url)} ${quoteShellArg(targetPath)}`);
|
|
257
|
+
}
|
|
258
|
+
async function writeSandboxFile(sandboxClient, targetPath, targetRoot, content) {
|
|
259
|
+
const parentPath = node_path.posix.dirname(targetPath);
|
|
260
|
+
if (parentPath !== targetRoot) await createSandboxDirectory(sandboxClient, parentPath);
|
|
261
|
+
await sandboxClient.writeFile(targetPath, content);
|
|
262
|
+
}
|
|
263
|
+
async function createSandboxDirectory(sandboxClient, targetPath) {
|
|
264
|
+
await runSandboxCommand(sandboxClient, `mkdir -p ${quoteShellArg(targetPath)}`);
|
|
265
|
+
}
|
|
266
|
+
async function runSandboxCommand(sandboxClient, command) {
|
|
267
|
+
const result = await sandboxClient.run(command);
|
|
268
|
+
if (result.exitCode !== 0) throw new Error(`workspace manifest command failed: ${command}\n${result.stderr ?? result.stdout}`);
|
|
269
|
+
}
|
|
270
|
+
function resolveHostSourcePath(source, hostRoot) {
|
|
271
|
+
return (0, node_path.isAbsolute)(source) ? (0, node_path.resolve)(source) : (0, node_path.resolve)(hostRoot ?? process.cwd(), source);
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Remove every trailing `/`, by index scan.
|
|
275
|
+
*
|
|
276
|
+
* Not `replace(/\/+$/, '')`: that run has no start anchor, so the engine retries it from every offset inside the
|
|
277
|
+
* run and each retry re-scans to the end — 3.0 s on a 100 K run (`js/polynomial-redos`, SEC-003). The backslash
|
|
278
|
+
* conversion in {@link normalizeSandboxRoot} manufactures such a run from a Windows-style path.
|
|
279
|
+
*/
|
|
280
|
+
function trimTrailingSlashes(value) {
|
|
281
|
+
let end = value.length;
|
|
282
|
+
while (end > 0 && value[end - 1] === "/") end -= 1;
|
|
283
|
+
return value.slice(0, end);
|
|
284
|
+
}
|
|
285
|
+
function normalizeSandboxRoot(root) {
|
|
286
|
+
const normalized = trimTrailingSlashes(root.replace(/\\/g, "/"));
|
|
287
|
+
if (!normalized.startsWith("/")) throw new Error("workspace manifest targetRoot must be an absolute sandbox path");
|
|
288
|
+
return normalized.length === 0 ? "/" : normalized;
|
|
289
|
+
}
|
|
290
|
+
function joinSandboxPath(root, path) {
|
|
291
|
+
const normalizedRoot = normalizeSandboxRoot(root);
|
|
292
|
+
if (normalizedRoot === "/") return `/${path}`;
|
|
293
|
+
return `${normalizedRoot}/${path}`;
|
|
294
|
+
}
|
|
295
|
+
function quoteShellArg(value) {
|
|
296
|
+
return `'${value.replace(SHELL_QUOTE_PATTERN, "'\\''")}'`;
|
|
297
|
+
}
|
|
298
|
+
function assertUnreachable(value) {
|
|
299
|
+
throw new Error(`unsupported workspace manifest entry: ${JSON.stringify(value)}`);
|
|
300
|
+
}
|
|
301
|
+
//#endregion
|
|
302
|
+
//#region src/sandbox/os-sandbox-policy.ts
|
|
303
|
+
/**
|
|
304
|
+
* What an OS-level sandbox lets a command touch, written once per backend (issue #3082).
|
|
305
|
+
*
|
|
306
|
+
* The same policy becomes bubblewrap arguments on Linux and a Seatbelt profile on macOS:
|
|
307
|
+
* - the whole filesystem is readable except the `denyRead` paths;
|
|
308
|
+
* - writes are allowed only inside the workspace, the temporary directories and `allowWrite`;
|
|
309
|
+
* - inside the workspace, the files that configure git, the agent, MCP servers and shells stay
|
|
310
|
+
* read-only, so a confined command cannot change what the next session trusts;
|
|
311
|
+
* - the network is either reachable or not. There is no per-domain allowlist: that needs a proxy
|
|
312
|
+
* process the OS cannot enforce, and a boundary here is only worth what the OS enforces.
|
|
313
|
+
*/
|
|
314
|
+
/** An isolated worktree's files are ordinary workspace files. */
|
|
315
|
+
const WRITABLE_INSIDE_PROTECTED = [".robota/worktrees", ".claude/worktrees"];
|
|
316
|
+
function join$2(root, relative) {
|
|
317
|
+
let end = root.length;
|
|
318
|
+
while (end > 0 && root[end - 1] === "/") end -= 1;
|
|
319
|
+
return `${root.slice(0, end)}/${relative}`;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Workspace entries a confined command must not write, relative to the root. `.git` is read-only
|
|
323
|
+
* as a whole: the files that make git run something (config, hooks, `commondir`, per-worktree
|
|
324
|
+
* config) are too many and too easy to add to for a list inside it to stay complete, so git
|
|
325
|
+
* commands that write run unconfined, through the ordinary permission path.
|
|
326
|
+
*/
|
|
327
|
+
function protectedWorkspaceEntries() {
|
|
328
|
+
return [..._robota_sdk_agent_core.PROTECTED_DIRECTORY_NAMES, ..._robota_sdk_agent_core.PROTECTED_FILE_NAMES];
|
|
329
|
+
}
|
|
330
|
+
/** The `bwrap` argument vector that runs `command args` under the policy. */
|
|
331
|
+
function bubblewrapArguments(input) {
|
|
332
|
+
const { policy } = input;
|
|
333
|
+
const args = [
|
|
334
|
+
"--ro-bind",
|
|
335
|
+
"/",
|
|
336
|
+
"/",
|
|
337
|
+
"--dev",
|
|
338
|
+
"/dev",
|
|
339
|
+
"--proc",
|
|
340
|
+
"/proc"
|
|
341
|
+
];
|
|
342
|
+
for (const path of [
|
|
343
|
+
policy.root,
|
|
344
|
+
...policy.tempDirectories,
|
|
345
|
+
...policy.allowWrite
|
|
346
|
+
]) args.push("--bind-try", path, path);
|
|
347
|
+
for (const entry of protectedWorkspaceEntries()) {
|
|
348
|
+
const path = join$2(policy.root, entry);
|
|
349
|
+
if (input.exists(path)) args.push("--ro-bind", path, path);
|
|
350
|
+
}
|
|
351
|
+
for (const entry of WRITABLE_INSIDE_PROTECTED) {
|
|
352
|
+
const path = join$2(policy.root, entry);
|
|
353
|
+
if (!input.exists(path)) continue;
|
|
354
|
+
args.push("--bind", path, path);
|
|
355
|
+
for (const name of input.listDirectory(path)) {
|
|
356
|
+
const gitFile = join$2(path, `${name}/.git`);
|
|
357
|
+
if (input.exists(gitFile)) args.push("--ro-bind", gitFile, gitFile);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
for (const hidden of policy.denyRead) {
|
|
361
|
+
if (!input.exists(hidden.path)) continue;
|
|
362
|
+
if (hidden.directory) args.push("--tmpfs", hidden.path);
|
|
363
|
+
else args.push("--ro-bind", "/dev/null", hidden.path);
|
|
364
|
+
}
|
|
365
|
+
if (!policy.network) {
|
|
366
|
+
if (input.seccompDescriptor === void 0) throw new Error("A sandbox without network needs the Unix-socket seccomp filter.");
|
|
367
|
+
args.push("--unshare-net", "--seccomp", String(input.seccompDescriptor));
|
|
368
|
+
}
|
|
369
|
+
args.push("--unshare-pid", "--die-with-parent", "--new-session", "--chdir", input.cwd);
|
|
370
|
+
args.push("--", input.command);
|
|
371
|
+
return [...args, ...input.args];
|
|
372
|
+
}
|
|
373
|
+
function regexEscape(path) {
|
|
374
|
+
return path.replace(/[\\^$.*+?()[\]{}|"]/g, (char) => `\\${char}`);
|
|
375
|
+
}
|
|
376
|
+
function quote(path) {
|
|
377
|
+
return `"${path.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* The Seatbelt profile for `sandbox-exec -p`. Later rules win, so the order below is the policy:
|
|
381
|
+
* deny writes, allow the writable places, deny the protected entries again, reopen worktrees.
|
|
382
|
+
*/
|
|
383
|
+
function seatbeltProfile(policy) {
|
|
384
|
+
const writable = [
|
|
385
|
+
policy.root,
|
|
386
|
+
...policy.tempDirectories,
|
|
387
|
+
...policy.allowWrite
|
|
388
|
+
].map((path) => `(subpath ${quote(path)})`).join(" ");
|
|
389
|
+
const protectedEntries = protectedWorkspaceEntries().map((entry) => {
|
|
390
|
+
const path = join$2(policy.root, entry);
|
|
391
|
+
return _robota_sdk_agent_core.PROTECTED_FILE_NAMES.includes(entry) ? `(literal ${quote(path)})` : `(subpath ${quote(path)})`;
|
|
392
|
+
});
|
|
393
|
+
const worktrees = WRITABLE_INSIDE_PROTECTED.map((entry) => `(subpath ${quote(join$2(policy.root, entry))})`);
|
|
394
|
+
const pinned = [`(literal ${quote(join$2(policy.root, ".git"))})`, ...WRITABLE_INSIDE_PROTECTED.map((entry) => `(regex #"^${regexEscape(join$2(policy.root, entry))}/[^/]+/\\.git$")`)];
|
|
395
|
+
const lines = [
|
|
396
|
+
"(version 1)",
|
|
397
|
+
"(allow default)",
|
|
398
|
+
"(deny file-write*)",
|
|
399
|
+
`(allow file-write* ${writable} (literal "/dev/null") (regex #"^/dev/tty") (regex #"^/dev/fd/"))`,
|
|
400
|
+
`(deny file-write* ${protectedEntries.join(" ")})`,
|
|
401
|
+
`(allow file-write* ${worktrees.join(" ")})`,
|
|
402
|
+
`(deny file-write* ${pinned.join(" ")})`
|
|
403
|
+
];
|
|
404
|
+
if (policy.denyRead.length > 0) {
|
|
405
|
+
const hidden = policy.denyRead.map((entry) => entry.directory ? `(subpath ${quote(entry.path)})` : `(literal ${quote(entry.path)})`);
|
|
406
|
+
lines.push(`(deny file-read* ${hidden.join(" ")})`);
|
|
407
|
+
}
|
|
408
|
+
if (!policy.network) lines.push("(deny network*)");
|
|
409
|
+
return lines.join("\n");
|
|
410
|
+
}
|
|
411
|
+
//#endregion
|
|
412
|
+
//#region src/sandbox/os-sandbox-seccomp.ts
|
|
413
|
+
/**
|
|
414
|
+
* The seccomp filter bubblewrap loads when a confined command has no network (issue #3082).
|
|
415
|
+
*
|
|
416
|
+
* `--unshare-net` removes every network interface, but a Unix socket is a file: a daemon listening
|
|
417
|
+
* on one outside the sandbox (a container engine, the session bus, an ssh agent) is still reachable
|
|
418
|
+
* through the read-only filesystem, and is a way to run anything on the host. The filter refuses
|
|
419
|
+
* creating an `AF_UNIX` socket, and refuses `io_uring_setup`, which could create one without the
|
|
420
|
+
* `socket` system call. A system call from another ABI (x32, 32-bit compat) is refused whole, since
|
|
421
|
+
* its numbers differ and the checks below would not see it. macOS's Seatbelt `(deny network*)`
|
|
422
|
+
* already covers Unix sockets.
|
|
423
|
+
*/
|
|
424
|
+
const BPF_LD_W_ABS = 32;
|
|
425
|
+
const BPF_JMP_JEQ_K = 21;
|
|
426
|
+
const BPF_JMP_JSET_K = 69;
|
|
427
|
+
const BPF_RET_K = 6;
|
|
428
|
+
const SECCOMP_RET_ALLOW = 2147418112;
|
|
429
|
+
const SECCOMP_RET_ERRNO = 327680;
|
|
430
|
+
const EPERM = 1;
|
|
431
|
+
const EAFNOSUPPORT = 97;
|
|
432
|
+
const AF_UNIX = 1;
|
|
433
|
+
const X32_SYSCALL_BIT = 1073741824;
|
|
434
|
+
/** `struct seccomp_data` offsets. */
|
|
435
|
+
const OFFSET_NR = 0;
|
|
436
|
+
const OFFSET_ARCH = 4;
|
|
437
|
+
const OFFSET_ARG0_LOW = 16;
|
|
438
|
+
const ARCHITECTURES = {
|
|
439
|
+
x64: {
|
|
440
|
+
audit: 3221225534,
|
|
441
|
+
socket: 41,
|
|
442
|
+
ioUringSetup: 425
|
|
443
|
+
},
|
|
444
|
+
arm64: {
|
|
445
|
+
audit: 3221225655,
|
|
446
|
+
socket: 198,
|
|
447
|
+
ioUringSetup: 425
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
function instruction(code, jt, jf, k) {
|
|
451
|
+
return [
|
|
452
|
+
code,
|
|
453
|
+
jt,
|
|
454
|
+
jf,
|
|
455
|
+
k
|
|
456
|
+
];
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* The filter as bytes `bwrap --seccomp` reads, or `undefined` for a processor architecture it has
|
|
460
|
+
* no system call numbers for — the caller then refuses to confine rather than confine with a gap.
|
|
461
|
+
*/
|
|
462
|
+
function unixSocketSeccompFilter(arch = process.arch) {
|
|
463
|
+
const target = ARCHITECTURES[arch];
|
|
464
|
+
if (target === void 0) return void 0;
|
|
465
|
+
const errno = (code) => SECCOMP_RET_ERRNO | code;
|
|
466
|
+
const program = [
|
|
467
|
+
instruction(BPF_LD_W_ABS, 0, 0, OFFSET_ARCH),
|
|
468
|
+
instruction(BPF_JMP_JEQ_K, 1, 0, target.audit),
|
|
469
|
+
instruction(BPF_RET_K, 0, 0, errno(EPERM)),
|
|
470
|
+
instruction(BPF_LD_W_ABS, 0, 0, OFFSET_NR),
|
|
471
|
+
instruction(BPF_JMP_JSET_K, 0, 1, X32_SYSCALL_BIT),
|
|
472
|
+
instruction(BPF_RET_K, 0, 0, errno(EPERM)),
|
|
473
|
+
instruction(BPF_JMP_JEQ_K, 0, 1, target.ioUringSetup),
|
|
474
|
+
instruction(BPF_RET_K, 0, 0, errno(EPERM)),
|
|
475
|
+
instruction(BPF_JMP_JEQ_K, 0, 3, target.socket),
|
|
476
|
+
instruction(BPF_LD_W_ABS, 0, 0, OFFSET_ARG0_LOW),
|
|
477
|
+
instruction(BPF_JMP_JEQ_K, 0, 1, AF_UNIX),
|
|
478
|
+
instruction(BPF_RET_K, 0, 0, errno(EAFNOSUPPORT)),
|
|
479
|
+
instruction(BPF_RET_K, 0, 0, SECCOMP_RET_ALLOW)
|
|
480
|
+
];
|
|
481
|
+
const bytes = new Uint8Array(program.length * 8);
|
|
482
|
+
const view = new DataView(bytes.buffer);
|
|
483
|
+
program.forEach(([code, jt, jf, k], index) => {
|
|
484
|
+
view.setUint16(index * 8, code, true);
|
|
485
|
+
view.setUint8(index * 8 + 2, jt);
|
|
486
|
+
view.setUint8(index * 8 + 3, jf);
|
|
487
|
+
view.setUint32(index * 8 + 4, k >>> 0, true);
|
|
488
|
+
});
|
|
489
|
+
return bytes;
|
|
490
|
+
}
|
|
491
|
+
//#endregion
|
|
492
|
+
//#region src/sandbox/os-sandbox-client.ts
|
|
493
|
+
/**
|
|
494
|
+
* OS-level confinement of shell commands over the host filesystem (issue #3082): bubblewrap on
|
|
495
|
+
* Linux and WSL2, Seatbelt (`sandbox-exec`) on macOS. Other platforms have no backend; the client
|
|
496
|
+
* reports that instead of pretending.
|
|
497
|
+
*
|
|
498
|
+
* It is a `shared` sandbox client: file tools stay on the host under the path guard, and the shell
|
|
499
|
+
* tool starts the wrapped invocation itself. Settings are live — `/sandbox` changes them for the
|
|
500
|
+
* next command without rebuilding the session.
|
|
501
|
+
*/
|
|
502
|
+
const DEFAULT_OS_SANDBOX_SETTINGS = Object.freeze({
|
|
503
|
+
enabled: false,
|
|
504
|
+
autoAllowBashIfSandboxed: true,
|
|
505
|
+
excludedCommands: [],
|
|
506
|
+
allowWrite: [],
|
|
507
|
+
denyRead: [],
|
|
508
|
+
network: false
|
|
48
509
|
});
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
510
|
+
const SEATBELT_EXECUTABLE = "/usr/bin/sandbox-exec";
|
|
511
|
+
function defaultProbe(command, args) {
|
|
512
|
+
const result = (0, node_child_process.spawnSync)(command, [...args], {
|
|
513
|
+
timeout: 5e3,
|
|
514
|
+
encoding: "utf8"
|
|
515
|
+
});
|
|
516
|
+
if (result.error !== void 0) return {
|
|
517
|
+
ok: false,
|
|
518
|
+
detail: result.error.message
|
|
519
|
+
};
|
|
520
|
+
const detail = (result.stderr ?? "").trim().split("\n")[0];
|
|
521
|
+
return result.status === 0 ? { ok: true } : {
|
|
522
|
+
ok: false,
|
|
523
|
+
...detail ? { detail } : {}
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
/** Find the platform's backend and check it can actually start a sandbox here. */
|
|
527
|
+
function detectOsSandbox(options = {}) {
|
|
528
|
+
const platform = options.platform ?? process.platform;
|
|
529
|
+
const probe = options.probe ?? defaultProbe;
|
|
530
|
+
if (platform === "linux") {
|
|
531
|
+
if (unixSocketSeccompFilter(options.arch ?? process.arch) === void 0) return {
|
|
532
|
+
backend: "bubblewrap",
|
|
533
|
+
missing: [`a seccomp filter for ${options.arch ?? process.arch} (x64 and arm64 are supported)`]
|
|
534
|
+
};
|
|
535
|
+
const check = probe("bwrap", [
|
|
536
|
+
"--ro-bind",
|
|
537
|
+
"/",
|
|
538
|
+
"/",
|
|
539
|
+
"--dev",
|
|
540
|
+
"/dev",
|
|
541
|
+
"--unshare-pid",
|
|
542
|
+
"true"
|
|
543
|
+
]);
|
|
544
|
+
if (check.ok) return {
|
|
545
|
+
backend: "bubblewrap",
|
|
546
|
+
executable: "bwrap",
|
|
547
|
+
missing: []
|
|
548
|
+
};
|
|
549
|
+
return {
|
|
550
|
+
backend: "bubblewrap",
|
|
551
|
+
missing: [check.detail?.includes("ENOENT") ? "bubblewrap (install the `bubblewrap` package)" : `bubblewrap cannot create a sandbox here${check.detail ? `: ${check.detail}` : ""}`]
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
if (platform === "darwin") {
|
|
555
|
+
const check = probe(SEATBELT_EXECUTABLE, [
|
|
556
|
+
"-p",
|
|
557
|
+
"(version 1)(allow default)",
|
|
558
|
+
"/usr/bin/true"
|
|
559
|
+
]);
|
|
560
|
+
if (check.ok) return {
|
|
561
|
+
backend: "seatbelt",
|
|
562
|
+
executable: SEATBELT_EXECUTABLE,
|
|
563
|
+
missing: []
|
|
564
|
+
};
|
|
565
|
+
return {
|
|
566
|
+
backend: "seatbelt",
|
|
567
|
+
missing: [`sandbox-exec cannot run${check.detail ? `: ${check.detail}` : ""}`]
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
return {
|
|
571
|
+
missing: [],
|
|
572
|
+
unsupportedPlatform: platform
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
function realPathOrSelf(path) {
|
|
576
|
+
try {
|
|
577
|
+
return (0, node_fs.realpathSync)(path);
|
|
578
|
+
} catch {
|
|
579
|
+
return path;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
function isDirectory(path) {
|
|
583
|
+
try {
|
|
584
|
+
return (0, node_fs.statSync)(path).isDirectory();
|
|
585
|
+
} catch {
|
|
586
|
+
return false;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
/** Give the owner read, write and search permission throughout an entry, never following links. */
|
|
590
|
+
function grantOwnerAccess(path) {
|
|
591
|
+
const stat = (0, node_fs.lstatSync)(path);
|
|
592
|
+
if (stat.isSymbolicLink()) return;
|
|
593
|
+
(0, node_fs.chmodSync)(path, stat.mode | 448);
|
|
594
|
+
if (!stat.isDirectory()) return;
|
|
595
|
+
for (const name of (0, node_fs.readdirSync)(path)) grantOwnerAccess(`${path}/${name}`);
|
|
596
|
+
}
|
|
597
|
+
function isSymbolicLink(path) {
|
|
598
|
+
try {
|
|
599
|
+
return (0, node_fs.lstatSync)(path).isSymbolicLink();
|
|
600
|
+
} catch {
|
|
601
|
+
return false;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
/** The program a shell line starts first — what `excludedCommands` names. */
|
|
605
|
+
function firstProgram(shellCommand) {
|
|
606
|
+
return shellCommand.trim().split(/\s+/)[0];
|
|
607
|
+
}
|
|
608
|
+
var OsSandboxClient = class {
|
|
609
|
+
filesystem = "shared";
|
|
610
|
+
root;
|
|
611
|
+
availability;
|
|
612
|
+
homeDirectory;
|
|
613
|
+
current;
|
|
614
|
+
inFlight = 0;
|
|
615
|
+
baseline = [];
|
|
616
|
+
/** Entries a clean-up could not restore, with the state they must return to. */
|
|
617
|
+
unresolved = /* @__PURE__ */ new Map();
|
|
618
|
+
constructor(options) {
|
|
619
|
+
this.root = realPathOrSelf(options.root);
|
|
620
|
+
this.availability = options.availability;
|
|
621
|
+
this.homeDirectory = options.homeDirectory ?? (0, node_os.homedir)();
|
|
622
|
+
this.current = {
|
|
623
|
+
...DEFAULT_OS_SANDBOX_SETTINGS,
|
|
624
|
+
...options.settings
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
status() {
|
|
628
|
+
return {
|
|
629
|
+
settings: this.current,
|
|
630
|
+
availability: this.availability,
|
|
631
|
+
active: this.current.enabled && this.availability.executable !== void 0
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
/** Change the settings for the next command. */
|
|
635
|
+
configure(settings) {
|
|
636
|
+
this.current = {
|
|
637
|
+
...this.current,
|
|
638
|
+
...settings
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
/** Whether `shellCommand` would run confined. */
|
|
642
|
+
confines(shellCommand) {
|
|
643
|
+
if (!this.status().active) return false;
|
|
644
|
+
if ((0, _robota_sdk_agent_core.splitCommandSegments)(shellCommand).length !== 1) return true;
|
|
645
|
+
const program = firstProgram(shellCommand);
|
|
646
|
+
return program === void 0 || !this.current.excludedCommands.includes(program);
|
|
647
|
+
}
|
|
648
|
+
autoApproves(shellCommand) {
|
|
649
|
+
if (!this.current.autoAllowBashIfSandboxed || !this.confines(shellCommand)) return false;
|
|
650
|
+
if (this.unresolved.size > 0) return false;
|
|
651
|
+
return !this.protectedEntryStates().some((state) => state.kind === "symlink" && !this.resolvesOutsideWritableWorkspace(state.path));
|
|
652
|
+
}
|
|
653
|
+
wrapCommand(invocation, shellCommand) {
|
|
654
|
+
if (!this.confines(shellCommand)) return invocation;
|
|
655
|
+
const policy = this.policy();
|
|
656
|
+
const executable = this.availability.executable;
|
|
657
|
+
if (this.availability.backend === "seatbelt") return {
|
|
658
|
+
command: executable,
|
|
659
|
+
args: [
|
|
660
|
+
"-p",
|
|
661
|
+
seatbeltProfile(policy),
|
|
662
|
+
invocation.command,
|
|
663
|
+
...invocation.args
|
|
664
|
+
],
|
|
665
|
+
cwd: invocation.cwd
|
|
666
|
+
};
|
|
667
|
+
const filter = policy.network ? void 0 : unixSocketSeccompFilter();
|
|
668
|
+
if (!this.protectedEntryStates().some((state) => state.path.endsWith("/.robota") && state.kind === "symlink")) (0, node_fs.mkdirSync)(`${this.root}/.robota`, { recursive: true });
|
|
669
|
+
const args = bubblewrapArguments({
|
|
670
|
+
policy,
|
|
671
|
+
exists: (path) => (0, node_fs.existsSync)(path) && !isSymbolicLink(path),
|
|
672
|
+
listDirectory: (path) => (0, node_fs.readdirSync)(path),
|
|
673
|
+
cwd: invocation.cwd,
|
|
674
|
+
command: invocation.command,
|
|
675
|
+
args: invocation.args,
|
|
676
|
+
...filter !== void 0 ? { seccompDescriptor: 3 } : {}
|
|
677
|
+
});
|
|
678
|
+
if (this.inFlight === 0) this.baseline = this.protectedEntryStates().map((state) => this.unresolved.get(state.path) ?? state);
|
|
679
|
+
this.inFlight += 1;
|
|
680
|
+
let finished = false;
|
|
681
|
+
return {
|
|
682
|
+
command: executable,
|
|
683
|
+
args,
|
|
684
|
+
cwd: invocation.cwd,
|
|
685
|
+
...filter !== void 0 ? { inputDescriptors: [filter] } : {},
|
|
686
|
+
afterExit: () => {
|
|
687
|
+
if (finished) return void 0;
|
|
688
|
+
finished = true;
|
|
689
|
+
this.inFlight -= 1;
|
|
690
|
+
return this.restoreProtectedEntries(this.baseline);
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* How each protected entry stands before a command: bubblewrap can mount an existing entry
|
|
696
|
+
* read-only, but not one that does not exist yet, and a symlink it mounts through to its target
|
|
697
|
+
* while the link itself stays replaceable. Read with `lstat`, so a dangling link is not "missing".
|
|
698
|
+
*/
|
|
699
|
+
protectedEntryStates() {
|
|
700
|
+
return protectedWorkspaceEntries().map((entry) => {
|
|
701
|
+
const path = `${this.root}/${entry}`;
|
|
702
|
+
try {
|
|
703
|
+
return (0, node_fs.lstatSync)(path).isSymbolicLink() ? {
|
|
704
|
+
path,
|
|
705
|
+
kind: "symlink",
|
|
706
|
+
target: (0, node_fs.readlinkSync)(path)
|
|
707
|
+
} : {
|
|
708
|
+
path,
|
|
709
|
+
kind: "present"
|
|
710
|
+
};
|
|
711
|
+
} catch {
|
|
712
|
+
return {
|
|
713
|
+
path,
|
|
714
|
+
kind: "missing"
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Undo what the command did to protected entries it could reach: one it created where none
|
|
721
|
+
* existed is moved into `.robota/sandbox-quarantine`, and a symlink it replaced is restored. Moved,
|
|
722
|
+
* not deleted, so nothing the host wrote meanwhile is lost.
|
|
723
|
+
*/
|
|
724
|
+
/** Whether a path's real location is under the read-only mounts: not the workspace, temp or `allowWrite`. */
|
|
725
|
+
resolvesOutsideWritableWorkspace(path) {
|
|
726
|
+
let real;
|
|
727
|
+
try {
|
|
728
|
+
real = (0, node_fs.realpathSync)(path);
|
|
729
|
+
} catch {
|
|
730
|
+
return false;
|
|
731
|
+
}
|
|
732
|
+
const policy = this.policy();
|
|
733
|
+
return ![
|
|
734
|
+
policy.root,
|
|
735
|
+
...policy.tempDirectories,
|
|
736
|
+
...policy.allowWrite
|
|
737
|
+
].some((area) => real === area || real.startsWith(`${area}/`));
|
|
738
|
+
}
|
|
739
|
+
/**
|
|
740
|
+
* Never throws: this runs as the command's process closes, and an exception there would take the
|
|
741
|
+
* host down and leave the entry in place. The quarantine is outside the workspace, under the
|
|
742
|
+
* user's `~/.robota`, where the command cannot reach it; what cannot be moved there is removed.
|
|
743
|
+
*/
|
|
744
|
+
restoreProtectedEntries(before) {
|
|
745
|
+
const quarantine = `${this.quarantineRoot(before)}/${Date.now()}-${(0, node_crypto.randomUUID)()}`;
|
|
746
|
+
const notes = [];
|
|
747
|
+
for (const state of before) {
|
|
748
|
+
if (state.kind === "present") continue;
|
|
749
|
+
try {
|
|
750
|
+
const now = this.protectedEntryStates().find((entry) => entry.path === state.path);
|
|
751
|
+
if (state.kind === "missing" && now.kind === "missing") {
|
|
752
|
+
this.unresolved.delete(state.path);
|
|
753
|
+
continue;
|
|
754
|
+
}
|
|
755
|
+
if (state.kind === "symlink" && now.kind === "symlink" && now.target === state.target) {
|
|
756
|
+
this.unresolved.delete(state.path);
|
|
757
|
+
continue;
|
|
758
|
+
}
|
|
759
|
+
if (now.kind !== "missing") notes.push(this.setAside(state.path, quarantine));
|
|
760
|
+
if (state.kind === "symlink") (0, node_fs.symlinkSync)(state.target, state.path);
|
|
761
|
+
this.unresolved.delete(state.path);
|
|
762
|
+
} catch (error) {
|
|
763
|
+
this.unresolved.set(state.path, state);
|
|
764
|
+
notes.push(`could not restore ${state.path} (${error instanceof Error ? error.message : String(error)}); commands will ask until it is removed`);
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
if (notes.length === 0) return void 0;
|
|
768
|
+
return `[sandbox] A confined command may not create or replace git, agent, MCP or shell configuration: ${notes.join("; ")}.`;
|
|
769
|
+
}
|
|
770
|
+
/**
|
|
771
|
+
* Where set-aside entries go: the workspace's own `.robota`, when it was a real directory before
|
|
772
|
+
* the command — then it was mounted read-only, so the command could not reach it, and a rename
|
|
773
|
+
* within one filesystem needs no permission inside the entry. Otherwise the user's `~/.robota`.
|
|
774
|
+
* Decided from the baseline: what is there now may be the command's own replacement.
|
|
775
|
+
*/
|
|
776
|
+
quarantineRoot(before) {
|
|
777
|
+
const robota = `${this.root}/.robota`;
|
|
778
|
+
return before.some((state) => state.path === robota && state.kind === "present") ? `${robota}/sandbox-quarantine` : `${this.homeDirectory}/.robota/sandbox-quarantine`;
|
|
779
|
+
}
|
|
780
|
+
setAside(path, quarantine) {
|
|
781
|
+
const destination = `${quarantine}/${(0, node_path.basename)(path)}`;
|
|
782
|
+
(0, node_fs.mkdirSync)(quarantine, { recursive: true });
|
|
783
|
+
try {
|
|
784
|
+
(0, node_fs.renameSync)(path, destination);
|
|
785
|
+
} catch (error) {
|
|
786
|
+
if (this.inFlight > 0) throw error;
|
|
787
|
+
grantOwnerAccess(path);
|
|
788
|
+
if (error.code === "EXDEV") {
|
|
789
|
+
(0, node_fs.cpSync)(path, destination, {
|
|
790
|
+
recursive: true,
|
|
791
|
+
verbatimSymlinks: true
|
|
792
|
+
});
|
|
793
|
+
(0, node_fs.rmSync)(path, {
|
|
794
|
+
recursive: true,
|
|
795
|
+
force: true
|
|
796
|
+
});
|
|
797
|
+
} else (0, node_fs.renameSync)(path, destination);
|
|
798
|
+
}
|
|
799
|
+
return `moved ${path} to ${destination}`;
|
|
800
|
+
}
|
|
801
|
+
/** The policy for the current settings, with every path made absolute and real. */
|
|
802
|
+
policy() {
|
|
803
|
+
const absolute = (path) => {
|
|
804
|
+
const expanded = path === "~" || path.startsWith("~/") ? `${this.homeDirectory}${path.slice(1)}` : path;
|
|
805
|
+
return realPathOrSelf((0, node_path.isAbsolute)(expanded) ? expanded : (0, node_path.resolve)(this.root, expanded));
|
|
806
|
+
};
|
|
807
|
+
const temp = [...new Set([(0, node_os.tmpdir)(), "/tmp"].filter(node_fs.existsSync).map(realPathOrSelf))];
|
|
808
|
+
return {
|
|
809
|
+
root: this.root,
|
|
810
|
+
tempDirectories: temp,
|
|
811
|
+
allowWrite: this.current.allowWrite.map(absolute),
|
|
812
|
+
denyRead: this.current.denyRead.map((path) => {
|
|
813
|
+
const resolved = absolute(path);
|
|
814
|
+
return {
|
|
815
|
+
path: resolved,
|
|
816
|
+
directory: isDirectory(resolved)
|
|
817
|
+
};
|
|
818
|
+
}),
|
|
819
|
+
network: this.current.network
|
|
820
|
+
};
|
|
821
|
+
}
|
|
822
|
+
run(command, options = {}) {
|
|
823
|
+
const shell = (0, _robota_sdk_agent_core.resolvePlatformShell)();
|
|
824
|
+
const cwd = options.workingDirectory ?? this.root;
|
|
825
|
+
const invocation = this.wrapCommand({
|
|
826
|
+
command: shell.command,
|
|
827
|
+
args: shell.commandArgs(command),
|
|
828
|
+
cwd
|
|
829
|
+
}, command);
|
|
830
|
+
return new Promise((resolveRun, reject) => {
|
|
831
|
+
const extra = invocation.inputDescriptors ?? [];
|
|
832
|
+
let child;
|
|
833
|
+
try {
|
|
834
|
+
child = (0, node_child_process.spawn)(invocation.command, [...invocation.args], {
|
|
835
|
+
cwd: invocation.cwd,
|
|
836
|
+
stdio: [
|
|
837
|
+
"ignore",
|
|
838
|
+
"pipe",
|
|
839
|
+
"pipe",
|
|
840
|
+
...extra.map(() => "pipe")
|
|
841
|
+
],
|
|
842
|
+
...options.timeoutMs !== void 0 ? { timeout: options.timeoutMs } : {}
|
|
843
|
+
});
|
|
844
|
+
} catch (error) {
|
|
845
|
+
invocation.afterExit?.();
|
|
846
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
extra.forEach((data, index) => {
|
|
850
|
+
const stream = child.stdio[index + 3];
|
|
851
|
+
stream?.on("error", () => void 0);
|
|
852
|
+
stream?.end(Buffer.from(data));
|
|
853
|
+
});
|
|
854
|
+
let stdout = "";
|
|
855
|
+
let stderr = "";
|
|
856
|
+
child.stdout?.on("data", (chunk) => stdout += chunk.toString());
|
|
857
|
+
child.stderr?.on("data", (chunk) => stderr += chunk.toString());
|
|
858
|
+
child.on("error", (error) => {
|
|
859
|
+
invocation.afterExit?.();
|
|
860
|
+
reject(error);
|
|
861
|
+
});
|
|
862
|
+
child.on("close", (code) => {
|
|
863
|
+
const note = invocation.afterExit?.();
|
|
864
|
+
resolveRun({
|
|
865
|
+
stdout: note === void 0 ? stdout : `${stdout}\n${note}`,
|
|
866
|
+
...stderr ? { stderr } : {},
|
|
867
|
+
exitCode: code ?? 1
|
|
868
|
+
});
|
|
869
|
+
});
|
|
870
|
+
});
|
|
871
|
+
}
|
|
872
|
+
readFile(path) {
|
|
873
|
+
return Promise.resolve((0, node_fs.readFileSync)(path, "utf8"));
|
|
874
|
+
}
|
|
875
|
+
writeFile(path, content) {
|
|
876
|
+
(0, node_fs.writeFileSync)(path, content, "utf8");
|
|
877
|
+
return Promise.resolve();
|
|
878
|
+
}
|
|
191
879
|
};
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
function
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
var FunctionTool = class {
|
|
299
|
-
schema;
|
|
300
|
-
fn;
|
|
301
|
-
eventService;
|
|
302
|
-
constructor(schema, fn) {
|
|
303
|
-
this.schema = schema;
|
|
304
|
-
this.fn = fn;
|
|
305
|
-
this.validateConstructorInputs();
|
|
306
|
-
}
|
|
307
|
-
/**
|
|
308
|
-
* Get tool name
|
|
309
|
-
*/
|
|
310
|
-
getName() {
|
|
311
|
-
return this.schema.name;
|
|
312
|
-
}
|
|
313
|
-
/**
|
|
314
|
-
* Set EventService for post-construction injection.
|
|
315
|
-
* Accepts EventService as-is without transformation.
|
|
316
|
-
* Caller is responsible for providing properly configured EventService.
|
|
317
|
-
*/
|
|
318
|
-
setEventService(eventService) {
|
|
319
|
-
this.eventService = eventService;
|
|
320
|
-
}
|
|
321
|
-
/**
|
|
322
|
-
* Execute the function tool
|
|
323
|
-
*/
|
|
324
|
-
async execute(parameters, context) {
|
|
325
|
-
const toolName = this.schema.name;
|
|
326
|
-
if (!this.validate(parameters)) {
|
|
327
|
-
const errors = this.getValidationErrors(parameters);
|
|
328
|
-
throw new import_agent_core3.ValidationError(`Invalid parameters for tool "${toolName}": ${errors.join(", ")}`);
|
|
329
|
-
}
|
|
330
|
-
const startTime = Date.now();
|
|
331
|
-
let result;
|
|
332
|
-
try {
|
|
333
|
-
result = await this.fn(parameters, context);
|
|
334
|
-
} catch (error) {
|
|
335
|
-
if (error instanceof import_agent_core3.ToolExecutionError || error instanceof import_agent_core3.ValidationError) {
|
|
336
|
-
throw error;
|
|
337
|
-
}
|
|
338
|
-
throw new import_agent_core3.ToolExecutionError(
|
|
339
|
-
`Function tool execution failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
340
|
-
toolName,
|
|
341
|
-
error instanceof Error ? error : new Error(String(error)),
|
|
342
|
-
{
|
|
343
|
-
parameterCount: Object.keys(parameters || {}).length,
|
|
344
|
-
hasContext: !!context
|
|
345
|
-
}
|
|
346
|
-
);
|
|
347
|
-
}
|
|
348
|
-
const executionTime = Date.now() - startTime;
|
|
349
|
-
return {
|
|
350
|
-
success: true,
|
|
351
|
-
data: result,
|
|
352
|
-
metadata: {
|
|
353
|
-
executionTime,
|
|
354
|
-
toolName,
|
|
355
|
-
parameters
|
|
356
|
-
}
|
|
357
|
-
};
|
|
358
|
-
}
|
|
359
|
-
/**
|
|
360
|
-
* Validate parameters (simple boolean result)
|
|
361
|
-
*/
|
|
362
|
-
validate(parameters) {
|
|
363
|
-
return this.getValidationErrors(parameters).length === 0;
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* Validate tool parameters with detailed result
|
|
367
|
-
*/
|
|
368
|
-
validateParameters(parameters) {
|
|
369
|
-
const errors = this.getValidationErrors(parameters);
|
|
370
|
-
return {
|
|
371
|
-
isValid: errors.length === 0,
|
|
372
|
-
errors
|
|
373
|
-
};
|
|
374
|
-
}
|
|
375
|
-
/**
|
|
376
|
-
* Get tool description
|
|
377
|
-
*/
|
|
378
|
-
getDescription() {
|
|
379
|
-
return this.schema.description;
|
|
380
|
-
}
|
|
381
|
-
/**
|
|
382
|
-
* Get detailed validation errors
|
|
383
|
-
*/
|
|
384
|
-
getValidationErrors(parameters) {
|
|
385
|
-
const errors = [];
|
|
386
|
-
const required = this.schema.parameters.required || [];
|
|
387
|
-
const properties = this.schema.parameters.properties || {};
|
|
388
|
-
for (const field of required) {
|
|
389
|
-
if (!(field in parameters)) {
|
|
390
|
-
errors.push(`Missing required parameter: ${field}`);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
for (const [key, value] of Object.entries(parameters)) {
|
|
394
|
-
const paramSchema = properties[key];
|
|
395
|
-
if (!paramSchema) {
|
|
396
|
-
errors.push(`Unknown parameter: ${key}`);
|
|
397
|
-
continue;
|
|
398
|
-
}
|
|
399
|
-
const typeError = this.validateParameterType(key, value, paramSchema);
|
|
400
|
-
if (typeError) {
|
|
401
|
-
errors.push(typeError);
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
return errors;
|
|
405
|
-
}
|
|
406
|
-
/**
|
|
407
|
-
* Validate individual parameter type
|
|
408
|
-
*/
|
|
409
|
-
validateParameterType(key, value, schema) {
|
|
410
|
-
const expectedType = schema["type"];
|
|
411
|
-
switch (expectedType) {
|
|
412
|
-
case "string":
|
|
413
|
-
if (typeof value !== "string") {
|
|
414
|
-
return `Parameter "${key}" must be a string, got ${typeof value}`;
|
|
415
|
-
}
|
|
416
|
-
break;
|
|
417
|
-
case "number":
|
|
418
|
-
if (typeof value !== "number" || isNaN(value)) {
|
|
419
|
-
return `Parameter "${key}" must be a number, got ${typeof value}`;
|
|
420
|
-
}
|
|
421
|
-
break;
|
|
422
|
-
case "boolean":
|
|
423
|
-
if (typeof value !== "boolean") {
|
|
424
|
-
return `Parameter "${key}" must be a boolean, got ${typeof value}`;
|
|
425
|
-
}
|
|
426
|
-
break;
|
|
427
|
-
case "array":
|
|
428
|
-
if (!Array.isArray(value)) {
|
|
429
|
-
return `Parameter "${key}" must be an array, got ${typeof value}`;
|
|
430
|
-
}
|
|
431
|
-
if (schema.items) {
|
|
432
|
-
for (let i = 0; i < value.length; i++) {
|
|
433
|
-
const itemError = this.validateParameterType(`${key}[${i}]`, value[i], schema.items);
|
|
434
|
-
if (itemError) {
|
|
435
|
-
return itemError;
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
break;
|
|
440
|
-
case "object":
|
|
441
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
442
|
-
return `Parameter "${key}" must be an object, got ${typeof value}`;
|
|
443
|
-
}
|
|
444
|
-
break;
|
|
445
|
-
}
|
|
446
|
-
if (schema.enum && schema.enum.length > 0) {
|
|
447
|
-
const enumValues = schema.enum;
|
|
448
|
-
let isValidEnum = false;
|
|
449
|
-
for (const enumValue of enumValues) {
|
|
450
|
-
if (value === enumValue) {
|
|
451
|
-
isValidEnum = true;
|
|
452
|
-
break;
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
if (!isValidEnum) {
|
|
456
|
-
return `Parameter "${key}" must be one of: ${enumValues.join(", ")}, got ${value}`;
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
return void 0;
|
|
460
|
-
}
|
|
461
|
-
/**
|
|
462
|
-
* Validate constructor inputs
|
|
463
|
-
*/
|
|
464
|
-
validateConstructorInputs() {
|
|
465
|
-
if (!this.schema) {
|
|
466
|
-
throw new import_agent_core3.ValidationError("Tool schema is required");
|
|
467
|
-
}
|
|
468
|
-
if (!this.fn || typeof this.fn !== "function") {
|
|
469
|
-
throw new import_agent_core3.ValidationError("Tool function is required and must be a function");
|
|
470
|
-
}
|
|
471
|
-
if (!this.schema.name) {
|
|
472
|
-
throw new import_agent_core3.ValidationError("Tool schema must have a name");
|
|
473
|
-
}
|
|
474
|
-
}
|
|
880
|
+
//#endregion
|
|
881
|
+
//#region src/retrieval/repo-map-index.ts
|
|
882
|
+
/** Persisted-schema version — bump when `IRepoMapIndex`'s serialized shape changes incompatibly. */
|
|
883
|
+
const REPO_MAP_INDEX_VERSION = 1;
|
|
884
|
+
/** Parse one corpus file into an index entry. */
|
|
885
|
+
function parseEntry(parser, file) {
|
|
886
|
+
const parsed = parser.parse(file.path, file.content);
|
|
887
|
+
return {
|
|
888
|
+
path: file.path,
|
|
889
|
+
definitions: parsed.definitions,
|
|
890
|
+
references: parsed.references
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
/** Parse the whole corpus once into a serializable repo-map index. */
|
|
894
|
+
function buildRepoMapIndex(options) {
|
|
895
|
+
return {
|
|
896
|
+
version: 1,
|
|
897
|
+
entries: options.corpus.map((file) => parseEntry(options.parser, file))
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* Apply corpus changes to a built index INCREMENTALLY (SELFHOST-003 P3): re-parse only the `upserted`
|
|
902
|
+
* files and drop `removed` paths, reusing every unchanged entry. Returns a new index (the input is not
|
|
903
|
+
* mutated). A file present in both `removed` and `upserted` is upserted (re-parse wins); a path repeated
|
|
904
|
+
* within `upserted` is de-duplicated last-wins, so the result always has one entry per path — matching a
|
|
905
|
+
* full rebuild (entry order does not affect ranking). Unchanged entries are REUSED BY REFERENCE; index
|
|
906
|
+
* entries are treated as immutable, so callers must not mutate an entry in place.
|
|
907
|
+
*/
|
|
908
|
+
function updateRepoMapIndex(index, changes, parser) {
|
|
909
|
+
const upsertedByPath = new Map((changes.upserted ?? []).map((file) => [file.path, file]));
|
|
910
|
+
const touched = /* @__PURE__ */ new Set([...changes.removed ?? [], ...upsertedByPath.keys()]);
|
|
911
|
+
const kept = index.entries.filter((entry) => !touched.has(entry.path));
|
|
912
|
+
const upserted = [...upsertedByPath.values()].map((file) => parseEntry(parser, file));
|
|
913
|
+
return {
|
|
914
|
+
version: index.version,
|
|
915
|
+
entries: [...kept, ...upserted]
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
/** Serialize a built index to a neutral JSON string for persistence by the surface. */
|
|
919
|
+
function serializeRepoMapIndex(index) {
|
|
920
|
+
return JSON.stringify(index);
|
|
921
|
+
}
|
|
922
|
+
/**
|
|
923
|
+
* Restore a built index from its serialized form. Throws on malformed JSON or an unsupported
|
|
924
|
+
* `version` — a stale/incompatible persisted index must be rebuilt, never silently mis-ranked.
|
|
925
|
+
*/
|
|
926
|
+
function deserializeRepoMapIndex(serialized) {
|
|
927
|
+
const parsed = JSON.parse(serialized);
|
|
928
|
+
if (parsed.version !== 1) throw new Error(`Unsupported repo-map index version ${String(parsed.version)} (expected 1); rebuild the index.`);
|
|
929
|
+
if (!Array.isArray(parsed.entries)) throw new Error("Malformed repo-map index: missing `entries`.");
|
|
930
|
+
for (const entry of parsed.entries) if (typeof entry?.path !== "string" || !Array.isArray(entry?.definitions) || !Array.isArray(entry?.references)) throw new Error("Malformed repo-map index: a corrupt entry — rebuild the index.");
|
|
931
|
+
return {
|
|
932
|
+
version: parsed.version,
|
|
933
|
+
entries: parsed.entries
|
|
934
|
+
};
|
|
935
|
+
}
|
|
936
|
+
//#endregion
|
|
937
|
+
//#region src/retrieval/repo-map-adapter.ts
|
|
938
|
+
/**
|
|
939
|
+
* SELFHOST-003: neutral repo-map ranking adapter — mirrors `InMemorySandboxClient`.
|
|
940
|
+
*
|
|
941
|
+
* Ranks a corpus of source files by graph centrality relative to the active files / mentioned
|
|
942
|
+
* identifiers, within a token budget. It is a NEUTRAL mechanism: it works on ANY repo given a corpus
|
|
943
|
+
* and an injected source parser — it carries no repo paths and no domain content. The heavy parser is
|
|
944
|
+
* injected as the duck-typed `IRetrievalSourceParser` (like `E2BSandboxClient` duck-types the E2B SDK),
|
|
945
|
+
* and the corpus is supplied from the surface.
|
|
946
|
+
*
|
|
947
|
+
* P2 (index build + persistence): the corpus is parsed ONCE into an `IRepoMapIndex` at construction
|
|
948
|
+
* (or supplied prebuilt/persisted via `{ index }`), so `retrieve()` ranks without re-parsing.
|
|
949
|
+
*
|
|
950
|
+
* Ranking model (aider repo-map style): a definition's score is the weighted number of references to it
|
|
951
|
+
* across the corpus, references FROM an active file weighted higher (personalization), plus a boost for
|
|
952
|
+
* a directly-mentioned identifier. Entries are emitted most-relevant-first, truncated to the budget.
|
|
953
|
+
*/
|
|
954
|
+
/** References from an active file weigh more (personalization toward the current focus). */
|
|
955
|
+
const ACTIVE_FILE_WEIGHT = 3;
|
|
956
|
+
/** A directly-mentioned identifier is a strong relevance signal. */
|
|
957
|
+
const MENTION_BOOST = 5;
|
|
958
|
+
/**
|
|
959
|
+
* Estimate the token cost of one repo-map entry (neutral chars/4 heuristic). Uses the same rendering
|
|
960
|
+
* shape the tool prints (`file:line kind name`) so the budgeted estimate matches the emitted output.
|
|
961
|
+
*/
|
|
962
|
+
function estimateTokens(symbol) {
|
|
963
|
+
const line = `${symbol.file}:${symbol.line} ${symbol.kind} ${symbol.name}`;
|
|
964
|
+
return Math.max(1, Math.ceil(line.length / 4));
|
|
965
|
+
}
|
|
966
|
+
const symbolKey = (s) => `${s.file}::${s.name}::${s.line}`;
|
|
967
|
+
var RepoMapRetrievalAdapter = class {
|
|
968
|
+
index;
|
|
969
|
+
constructor(options) {
|
|
970
|
+
if (options.index) this.index = options.index;
|
|
971
|
+
else if (options.parser && options.corpus) this.index = buildRepoMapIndex({
|
|
972
|
+
parser: options.parser,
|
|
973
|
+
corpus: options.corpus
|
|
974
|
+
});
|
|
975
|
+
else throw new Error("RepoMapRetrievalAdapter requires either { index } or { parser, corpus }.");
|
|
976
|
+
}
|
|
977
|
+
async retrieve(request) {
|
|
978
|
+
return selectWithinBudget(rankSymbols(this.index.entries.map((entry) => ({
|
|
979
|
+
file: entry.path,
|
|
980
|
+
parsed: {
|
|
981
|
+
definitions: entry.definitions,
|
|
982
|
+
references: entry.references
|
|
983
|
+
}
|
|
984
|
+
})), request), request.tokenBudget);
|
|
985
|
+
}
|
|
475
986
|
};
|
|
987
|
+
/** Index every definition in the corpus by its name (a name may be defined in several files). */
|
|
988
|
+
function indexDefinitions(parsed) {
|
|
989
|
+
const defsByName = /* @__PURE__ */ new Map();
|
|
990
|
+
for (const { parsed: file } of parsed) for (const def of file.definitions) {
|
|
991
|
+
const list = defsByName.get(def.name) ?? [];
|
|
992
|
+
list.push(def);
|
|
993
|
+
defsByName.set(def.name, list);
|
|
994
|
+
}
|
|
995
|
+
return defsByName;
|
|
996
|
+
}
|
|
997
|
+
/** Score each definition by weighted reference count + personalization + mention boost. */
|
|
998
|
+
function rankSymbols(parsed, request) {
|
|
999
|
+
const activeFiles = new Set(request.activeFiles ?? []);
|
|
1000
|
+
const mentioned = new Set(request.mentionedIdentifiers ?? []);
|
|
1001
|
+
const defsByName = indexDefinitions(parsed);
|
|
1002
|
+
const scoreByKey = /* @__PURE__ */ new Map();
|
|
1003
|
+
const bump = (s, delta) => {
|
|
1004
|
+
scoreByKey.set(symbolKey(s), (scoreByKey.get(symbolKey(s)) ?? 0) + delta);
|
|
1005
|
+
};
|
|
1006
|
+
for (const { file, parsed: source } of parsed) {
|
|
1007
|
+
const weight = activeFiles.has(file) ? ACTIVE_FILE_WEIGHT : 1;
|
|
1008
|
+
for (const ref of source.references) for (const def of defsByName.get(ref) ?? []) if (def.file !== file) bump(def, weight);
|
|
1009
|
+
}
|
|
1010
|
+
for (const name of mentioned) for (const def of defsByName.get(name) ?? []) bump(def, MENTION_BOOST);
|
|
1011
|
+
const ranked = [];
|
|
1012
|
+
for (const defs of defsByName.values()) for (const def of defs) ranked.push({
|
|
1013
|
+
...def,
|
|
1014
|
+
score: scoreByKey.get(symbolKey(def)) ?? 0,
|
|
1015
|
+
tokens: estimateTokens(def)
|
|
1016
|
+
});
|
|
1017
|
+
ranked.sort((a, b) => b.score - a.score || a.file.localeCompare(b.file) || a.line - b.line || a.name.localeCompare(b.name));
|
|
1018
|
+
return ranked;
|
|
1019
|
+
}
|
|
1020
|
+
/** Take the most-relevant-first prefix whose cumulative tokens fit the budget. */
|
|
1021
|
+
function selectWithinBudget(ranked, tokenBudget) {
|
|
1022
|
+
const symbols = [];
|
|
1023
|
+
let totalTokens = 0;
|
|
1024
|
+
for (const entry of ranked) {
|
|
1025
|
+
if (totalTokens + entry.tokens > tokenBudget) break;
|
|
1026
|
+
symbols.push(entry);
|
|
1027
|
+
totalTokens += entry.tokens;
|
|
1028
|
+
}
|
|
1029
|
+
return {
|
|
1030
|
+
symbols,
|
|
1031
|
+
totalTokens
|
|
1032
|
+
};
|
|
1033
|
+
}
|
|
1034
|
+
//#endregion
|
|
1035
|
+
//#region src/implementations/function-tool.ts
|
|
1036
|
+
/**
|
|
1037
|
+
* Helper function to create a function tool from a simple function
|
|
1038
|
+
*/
|
|
476
1039
|
function createFunctionTool(name, description, parameters, fn) {
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
function
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
}
|
|
605
|
-
/**
|
|
606
|
-
* Execute the actual API call
|
|
607
|
-
* @private
|
|
608
|
-
*/
|
|
609
|
-
async executeAPICall(parameters, _context) {
|
|
610
|
-
const operation = this.findOperation();
|
|
611
|
-
if (!operation) {
|
|
612
|
-
throw new Error(`Operation ${this.operationId} not found in OpenAPI spec`);
|
|
613
|
-
}
|
|
614
|
-
const requestConfig = this.buildRequestConfig(operation, parameters);
|
|
615
|
-
throw new Error("Not implemented: actual API execution is not yet available");
|
|
616
|
-
}
|
|
617
|
-
/**
|
|
618
|
-
* Find the operation in the OpenAPI specification
|
|
619
|
-
*/
|
|
620
|
-
findOperation() {
|
|
621
|
-
for (const [path, pathItem] of Object.entries(this.apiSpec.paths || {})) {
|
|
622
|
-
if (!pathItem) continue;
|
|
623
|
-
for (const method of [
|
|
624
|
-
"get",
|
|
625
|
-
"post",
|
|
626
|
-
"put",
|
|
627
|
-
"delete",
|
|
628
|
-
"patch",
|
|
629
|
-
"head",
|
|
630
|
-
"options"
|
|
631
|
-
]) {
|
|
632
|
-
const operation = pathItem[method];
|
|
633
|
-
if (operation?.operationId === this.operationId) {
|
|
634
|
-
return { method, path, operation };
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
return void 0;
|
|
639
|
-
}
|
|
640
|
-
/**
|
|
641
|
-
* Build HTTP request configuration from OpenAPI operation and parameters
|
|
642
|
-
*/
|
|
643
|
-
buildRequestConfig(opInfo, parameters) {
|
|
644
|
-
const { method, path, operation } = opInfo;
|
|
645
|
-
let url = this.baseURL + path;
|
|
646
|
-
const headers = {};
|
|
647
|
-
let body;
|
|
648
|
-
const params = operation.parameters || [];
|
|
649
|
-
for (const param of params) {
|
|
650
|
-
const value = parameters[param.name];
|
|
651
|
-
if (value === void 0 && param.required) {
|
|
652
|
-
throw new Error(`Required parameter ${param.name} is missing`);
|
|
653
|
-
}
|
|
654
|
-
if (value !== void 0) {
|
|
655
|
-
switch (param.in) {
|
|
656
|
-
case "path":
|
|
657
|
-
url = url.replace(`{${param.name}}`, encodeURIComponent(String(value)));
|
|
658
|
-
break;
|
|
659
|
-
case "query": {
|
|
660
|
-
const separator = url.includes("?") ? "&" : "?";
|
|
661
|
-
url += `${separator}${param.name}=${encodeURIComponent(String(value))}`;
|
|
662
|
-
break;
|
|
663
|
-
}
|
|
664
|
-
case "header":
|
|
665
|
-
headers[param.name] = String(value);
|
|
666
|
-
break;
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
if (["post", "put", "patch"].includes(method) && operation.requestBody) {
|
|
671
|
-
const requestBody = operation.requestBody;
|
|
672
|
-
const jsonContent = requestBody.content?.["application/json"];
|
|
673
|
-
if (jsonContent) {
|
|
674
|
-
headers["Content-Type"] = "application/json";
|
|
675
|
-
const bodyParams = {};
|
|
676
|
-
for (const [key, value] of Object.entries(parameters)) {
|
|
677
|
-
const isParamUsed = params.some((p) => p.name === key);
|
|
678
|
-
if (!isParamUsed) {
|
|
679
|
-
bodyParams[key] = value;
|
|
680
|
-
}
|
|
681
|
-
}
|
|
682
|
-
body = JSON.stringify(bodyParams);
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
if (this.config.auth) {
|
|
686
|
-
switch (this.config.auth.type) {
|
|
687
|
-
case "bearer":
|
|
688
|
-
headers["Authorization"] = `Bearer ${this.config.auth.token}`;
|
|
689
|
-
break;
|
|
690
|
-
case "apiKey": {
|
|
691
|
-
const headerName = this.config.auth.header || "X-API-Key";
|
|
692
|
-
headers[headerName] = this.config.auth.apiKey || "";
|
|
693
|
-
break;
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
const result = {
|
|
698
|
-
method,
|
|
699
|
-
url,
|
|
700
|
-
headers
|
|
701
|
-
};
|
|
702
|
-
if (body !== void 0) {
|
|
703
|
-
result.body = body;
|
|
704
|
-
}
|
|
705
|
-
return result;
|
|
706
|
-
}
|
|
707
|
-
/**
|
|
708
|
-
* Create tool schema from OpenAPI operation specification
|
|
709
|
-
*/
|
|
710
|
-
createSchemaFromOpenAPI() {
|
|
711
|
-
const operation = this.findOperation();
|
|
712
|
-
if (!operation) {
|
|
713
|
-
throw new Error(
|
|
714
|
-
`[STRICT-POLICY][EMITTER-CONTRACT] OpenAPI operation not found: ${this.operationId}. Emitter contract must provide a valid operationId present in the OpenAPI document.`
|
|
715
|
-
);
|
|
716
|
-
}
|
|
717
|
-
const { operation: opSpec } = operation;
|
|
718
|
-
const properties = {};
|
|
719
|
-
const required = [];
|
|
720
|
-
const params = opSpec.parameters || [];
|
|
721
|
-
for (const param of params) {
|
|
722
|
-
properties[param.name] = this.convertOpenAPIParamToSchema(param);
|
|
723
|
-
if (param.required) {
|
|
724
|
-
required.push(param.name);
|
|
725
|
-
}
|
|
726
|
-
}
|
|
727
|
-
if (opSpec.requestBody) {
|
|
728
|
-
const requestBody = opSpec.requestBody;
|
|
729
|
-
const jsonContent = requestBody.content?.["application/json"];
|
|
730
|
-
if (jsonContent?.schema) {
|
|
731
|
-
const bodySchema = this.convertOpenAPISchemaToParameterSchema(jsonContent.schema);
|
|
732
|
-
if (bodySchema.type === "object" && bodySchema.properties) {
|
|
733
|
-
Object.assign(properties, bodySchema.properties);
|
|
734
|
-
const schemaWithRequired = bodySchema;
|
|
735
|
-
if (schemaWithRequired.required) {
|
|
736
|
-
required.push(...schemaWithRequired.required);
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
const schemaParams = {
|
|
742
|
-
type: "object",
|
|
743
|
-
properties
|
|
744
|
-
};
|
|
745
|
-
if (required.length > 0) {
|
|
746
|
-
schemaParams.required = required;
|
|
747
|
-
}
|
|
748
|
-
return {
|
|
749
|
-
name: this.operationId,
|
|
750
|
-
description: opSpec.summary || opSpec.description || `OpenAPI operation: ${this.operationId}`,
|
|
751
|
-
parameters: schemaParams
|
|
752
|
-
};
|
|
753
|
-
}
|
|
754
|
-
/**
|
|
755
|
-
* Convert OpenAPI parameter to tool parameter schema
|
|
756
|
-
*/
|
|
757
|
-
convertOpenAPIParamToSchema(param) {
|
|
758
|
-
const schema = param.schema;
|
|
759
|
-
return this.convertOpenAPISchemaToParameterSchema(schema);
|
|
760
|
-
}
|
|
761
|
-
/**
|
|
762
|
-
* Convert OpenAPI schema to parameter schema
|
|
763
|
-
*/
|
|
764
|
-
convertOpenAPISchemaToParameterSchema(schema) {
|
|
765
|
-
if ("$ref" in schema) {
|
|
766
|
-
return { type: "object" };
|
|
767
|
-
}
|
|
768
|
-
const result = {
|
|
769
|
-
type: this.mapOpenAPIType(schema.type)
|
|
770
|
-
};
|
|
771
|
-
if (schema.description) {
|
|
772
|
-
result.description = schema.description;
|
|
773
|
-
}
|
|
774
|
-
if (schema.enum) {
|
|
775
|
-
result.enum = schema.enum;
|
|
776
|
-
}
|
|
777
|
-
if (schema.minimum !== void 0) {
|
|
778
|
-
result.minimum = schema.minimum;
|
|
779
|
-
}
|
|
780
|
-
if (schema.maximum !== void 0) {
|
|
781
|
-
result.maximum = schema.maximum;
|
|
782
|
-
}
|
|
783
|
-
if (schema.pattern) {
|
|
784
|
-
result.pattern = schema.pattern;
|
|
785
|
-
}
|
|
786
|
-
if (schema.format) {
|
|
787
|
-
result.format = schema.format;
|
|
788
|
-
}
|
|
789
|
-
if (schema.default !== void 0) {
|
|
790
|
-
result.default = schema.default;
|
|
791
|
-
}
|
|
792
|
-
if (schema.type === "array" && schema.items) {
|
|
793
|
-
result.items = this.convertOpenAPISchemaToParameterSchema(schema.items);
|
|
794
|
-
}
|
|
795
|
-
if (schema.type === "object" && schema.properties) {
|
|
796
|
-
result.properties = {};
|
|
797
|
-
for (const [propName, propSchema] of Object.entries(schema.properties)) {
|
|
798
|
-
result.properties[propName] = this.convertOpenAPISchemaToParameterSchema(propSchema);
|
|
799
|
-
}
|
|
800
|
-
if (schema.required && schema.required.length > 0) {
|
|
801
|
-
result.required = schema.required;
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
return result;
|
|
805
|
-
}
|
|
806
|
-
/**
|
|
807
|
-
* Map OpenAPI type to JSON schema type
|
|
808
|
-
*/
|
|
809
|
-
mapOpenAPIType(type) {
|
|
810
|
-
switch (type) {
|
|
811
|
-
case "string":
|
|
812
|
-
return "string";
|
|
813
|
-
case "number":
|
|
814
|
-
return "number";
|
|
815
|
-
case "integer":
|
|
816
|
-
return "integer";
|
|
817
|
-
case "boolean":
|
|
818
|
-
return "boolean";
|
|
819
|
-
case "array":
|
|
820
|
-
return "array";
|
|
821
|
-
case "object":
|
|
822
|
-
return "object";
|
|
823
|
-
default:
|
|
824
|
-
return "string";
|
|
825
|
-
}
|
|
826
|
-
}
|
|
1040
|
+
return new _robota_sdk_agent_core.FunctionTool({
|
|
1041
|
+
name,
|
|
1042
|
+
description,
|
|
1043
|
+
parameters
|
|
1044
|
+
}, fn);
|
|
1045
|
+
}
|
|
1046
|
+
/**
|
|
1047
|
+
* Helper function to create a function tool from Zod schema
|
|
1048
|
+
*/
|
|
1049
|
+
function createZodFunctionTool(name, description, zodSchema, fn, residency = {}) {
|
|
1050
|
+
const schema = {
|
|
1051
|
+
name,
|
|
1052
|
+
description,
|
|
1053
|
+
parameters: (0, _robota_sdk_agent_core.zodToJsonSchema)(zodSchema),
|
|
1054
|
+
...residency.deferLoading !== void 0 && { deferLoading: residency.deferLoading }
|
|
1055
|
+
};
|
|
1056
|
+
const wrappedFn = async (parameters, context) => {
|
|
1057
|
+
const parseResult = zodSchema.safeParse(parameters);
|
|
1058
|
+
if (!parseResult.success) throw new _robota_sdk_agent_core.ValidationError(`Zod validation failed: ${parseResult.error}`);
|
|
1059
|
+
const result = await fn(parseResult.data, context);
|
|
1060
|
+
return typeof result === "string" ? result : JSON.stringify(result);
|
|
1061
|
+
};
|
|
1062
|
+
return new _robota_sdk_agent_core.FunctionTool(schema, wrappedFn);
|
|
1063
|
+
}
|
|
1064
|
+
//#endregion
|
|
1065
|
+
//#region src/tool-permission-profiles.ts
|
|
1066
|
+
/**
|
|
1067
|
+
* What the permission system is told about the tools THIS package defines. CORE-030.
|
|
1068
|
+
*
|
|
1069
|
+
* The classification used to live in `@robota-sdk/agent-core`'s `permission-mode.ts`, as a matrix
|
|
1070
|
+
* keyed on a closed union of product tool names — a vendor-neutral foundation holding a product's
|
|
1071
|
+
* tool inventory, two layers below the code that defines it, with nothing coupling the two lists.
|
|
1072
|
+
* They drifted: `CodebaseRetrieval` is defined here and the matrix had never heard of it, so a
|
|
1073
|
+
* read-only retrieval prompted on every call and was refused outright in plan mode.
|
|
1074
|
+
*
|
|
1075
|
+
* A tool's own package declares what it does. The foundation decides what each MODE does about that
|
|
1076
|
+
* kind of action, and neither restates the other's half.
|
|
1077
|
+
*
|
|
1078
|
+
* `packages/agent-tools/src/__tests__/tool-permission-profiles.test.ts` asserts that every tool this
|
|
1079
|
+
* package produces appears here, so adding a tool without classifying it fails rather than silently
|
|
1080
|
+
* inheriting the prompt-on-every-call fallback.
|
|
1081
|
+
*/
|
|
1082
|
+
/**
|
|
1083
|
+
* Every tool this package defines, and what the permission system needs to know about it.
|
|
1084
|
+
*
|
|
1085
|
+
* `argument.key` is which argument a pattern like `Read(/src/**)` is matched against, and
|
|
1086
|
+
* `argument.kind` how (CORE-049: a URL is parsed, a path is segment-wise, a command is a glob). A tool without
|
|
1087
|
+
* one cannot be narrowed by an argument pattern at all — the gate treats such a pattern as
|
|
1088
|
+
* unevaluable and prompts rather than proceeding, which is why the ones that CAN be narrowed say so.
|
|
1089
|
+
*/
|
|
1090
|
+
const AGENT_TOOL_PERMISSION_PROFILES = {
|
|
1091
|
+
Read: {
|
|
1092
|
+
argument: {
|
|
1093
|
+
key: "filePath",
|
|
1094
|
+
kind: "path"
|
|
1095
|
+
},
|
|
1096
|
+
riskClass: "inspect"
|
|
1097
|
+
},
|
|
1098
|
+
Glob: {
|
|
1099
|
+
argument: {
|
|
1100
|
+
key: "pattern",
|
|
1101
|
+
kind: "text"
|
|
1102
|
+
},
|
|
1103
|
+
riskClass: "inspect"
|
|
1104
|
+
},
|
|
1105
|
+
Grep: {
|
|
1106
|
+
argument: {
|
|
1107
|
+
key: "pattern",
|
|
1108
|
+
kind: "text"
|
|
1109
|
+
},
|
|
1110
|
+
riskClass: "inspect"
|
|
1111
|
+
},
|
|
1112
|
+
WebFetch: {
|
|
1113
|
+
argument: {
|
|
1114
|
+
key: "url",
|
|
1115
|
+
kind: "url"
|
|
1116
|
+
},
|
|
1117
|
+
riskClass: "inspect"
|
|
1118
|
+
},
|
|
1119
|
+
WebSearch: {
|
|
1120
|
+
argument: {
|
|
1121
|
+
key: "query",
|
|
1122
|
+
kind: "text"
|
|
1123
|
+
},
|
|
1124
|
+
riskClass: "inspect"
|
|
1125
|
+
},
|
|
1126
|
+
CodebaseRetrieval: { riskClass: "inspect" },
|
|
1127
|
+
AskUserQuestion: { riskClass: "inspect" },
|
|
1128
|
+
ToolSearch: {
|
|
1129
|
+
argument: {
|
|
1130
|
+
key: "query",
|
|
1131
|
+
kind: "text"
|
|
1132
|
+
},
|
|
1133
|
+
riskClass: "inspect"
|
|
1134
|
+
},
|
|
1135
|
+
ComputerView: { riskClass: "inspect" },
|
|
1136
|
+
Write: {
|
|
1137
|
+
argument: {
|
|
1138
|
+
key: "filePath",
|
|
1139
|
+
kind: "path"
|
|
1140
|
+
},
|
|
1141
|
+
riskClass: "modify"
|
|
1142
|
+
},
|
|
1143
|
+
Edit: {
|
|
1144
|
+
argument: {
|
|
1145
|
+
key: "filePath",
|
|
1146
|
+
kind: "path"
|
|
1147
|
+
},
|
|
1148
|
+
riskClass: "modify"
|
|
1149
|
+
},
|
|
1150
|
+
Shell: {
|
|
1151
|
+
argument: {
|
|
1152
|
+
key: "command",
|
|
1153
|
+
kind: "command"
|
|
1154
|
+
},
|
|
1155
|
+
riskClass: "execute",
|
|
1156
|
+
aliases: ["Bash"]
|
|
1157
|
+
},
|
|
1158
|
+
Bash: {
|
|
1159
|
+
argument: {
|
|
1160
|
+
key: "command",
|
|
1161
|
+
kind: "command"
|
|
1162
|
+
},
|
|
1163
|
+
riskClass: "execute",
|
|
1164
|
+
aliases: ["Shell"]
|
|
1165
|
+
},
|
|
1166
|
+
Computer: { riskClass: "execute" }
|
|
827
1167
|
};
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
1168
|
+
/**
|
|
1169
|
+
* Tell the permission system about every tool this package defines. Idempotent.
|
|
1170
|
+
*
|
|
1171
|
+
* Not exported: the one caller is the line below. A registration a consumer could choose to skip is
|
|
1172
|
+
* a registration that might not happen, which is the state this change exists to leave behind.
|
|
1173
|
+
*/
|
|
1174
|
+
function registerAgentToolPermissionProfiles() {
|
|
1175
|
+
for (const [toolName, profile] of Object.entries(AGENT_TOOL_PERMISSION_PROFILES)) (0, _robota_sdk_agent_core.registerToolPermissionProfile)(toolName, profile);
|
|
1176
|
+
}
|
|
1177
|
+
registerAgentToolPermissionProfiles();
|
|
1178
|
+
//#endregion
|
|
1179
|
+
//#region src/retrieval/retrieval-tool.ts
|
|
1180
|
+
/**
|
|
1181
|
+
* SELFHOST-003: the `CodebaseRetrieval` tool — mirrors the `create*Tool(options)` pattern.
|
|
1182
|
+
*
|
|
1183
|
+
* Composes over the injected `IRetrievalAdapter` (via `IRetrievalToolOptions`). It carries NO corpus and
|
|
1184
|
+
* NO domain content itself — the adapter (built from a surface-supplied parser + corpus) does the
|
|
1185
|
+
* ranking. With no adapter the tool reports unavailability (it is added to the default set only when an
|
|
1186
|
+
* adapter is present — see `createDefaultTools`).
|
|
1187
|
+
*/
|
|
1188
|
+
/** Default token budget when the caller does not specify one. */
|
|
1189
|
+
const DEFAULT_TOKEN_BUDGET = 1e3;
|
|
1190
|
+
const RetrievalSchema = zod.z.object({
|
|
1191
|
+
activeFiles: zod.z.array(zod.z.string()).optional().describe("Repo-relative files currently in focus; the map is ranked toward what they reference."),
|
|
1192
|
+
mentionedIdentifiers: zod.z.array(zod.z.string()).optional().describe("Symbol names to bias the map toward (e.g. identifiers named in the task)."),
|
|
1193
|
+
tokenBudget: zod.z.number().int().positive().optional().describe(`Maximum tokens for the returned map (default ${DEFAULT_TOKEN_BUDGET}).`)
|
|
840
1194
|
});
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
)
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
var import_zod2 = require("zod");
|
|
912
|
-
var DEFAULT_LIMIT = 2e3;
|
|
913
|
-
var ReadSchema = import_zod2.z.object({
|
|
914
|
-
filePath: import_zod2.z.string().describe("The absolute path to the file to read"),
|
|
915
|
-
offset: import_zod2.z.number().optional().describe(
|
|
916
|
-
"The line number to start reading from (1-based). Only provide if the file is too large to read at once"
|
|
917
|
-
),
|
|
918
|
-
limit: import_zod2.z.number().optional().describe(
|
|
919
|
-
`The number of lines to read (default: ${DEFAULT_LIMIT}). Only provide if the file is too large to read at once`
|
|
920
|
-
)
|
|
1195
|
+
/** Render the ranked symbols as a compact, deterministic repo map. */
|
|
1196
|
+
function formatRepoMap(symbols) {
|
|
1197
|
+
return symbols.map((symbol) => `${symbol.file}:${symbol.line} ${symbol.kind} ${symbol.name}`).join("\n");
|
|
1198
|
+
}
|
|
1199
|
+
async function retrievalTool(args, options = {}) {
|
|
1200
|
+
if (!options.adapter) return "Codebase retrieval is not available in this session.";
|
|
1201
|
+
const result = await options.adapter.retrieve({
|
|
1202
|
+
...args.activeFiles ? { activeFiles: args.activeFiles } : {},
|
|
1203
|
+
...args.mentionedIdentifiers ? { mentionedIdentifiers: args.mentionedIdentifiers } : {},
|
|
1204
|
+
tokenBudget: args.tokenBudget ?? DEFAULT_TOKEN_BUDGET
|
|
1205
|
+
});
|
|
1206
|
+
if (result.symbols.length === 0) return "No relevant symbols found within the token budget.";
|
|
1207
|
+
return `Most relevant symbols (~${result.totalTokens} tokens):\n${formatRepoMap(result.symbols)}`;
|
|
1208
|
+
}
|
|
1209
|
+
function createRetrievalTool(options = {}) {
|
|
1210
|
+
return createZodFunctionTool("CodebaseRetrieval", "Retrieve the most relevant slice of the codebase (a ranked repo map of symbols) for the current task, within a token budget. Provide the files you are focused on and/or identifiers named in the task; returns the highest-centrality definitions first.", RetrievalSchema, async (params) => retrievalTool(params, options));
|
|
1211
|
+
}
|
|
1212
|
+
//#endregion
|
|
1213
|
+
//#region src/computer-use/computer-tool.ts
|
|
1214
|
+
/**
|
|
1215
|
+
* SELFHOST-010: the `ComputerView` (perceive) + `Computer` (act) tools — mirror the `create*Tool(options)`
|
|
1216
|
+
* pattern, split along the permission boundary.
|
|
1217
|
+
*
|
|
1218
|
+
* `createComputerTool({ driver })` registers BOTH tool names over one injected `IComputerDriver`. The split
|
|
1219
|
+
* is purely the permission-bearing boundary (the repo's own `Read`(auto)-vs-`Shell`(approve) precedent):
|
|
1220
|
+
* - `ComputerView` calls `driver.screenshot()` — a perceive with no action argument (gated `auto` like `Read`).
|
|
1221
|
+
* - `Computer` takes a single typed mutating `action`, executes it via `driver.act()`, and returns the
|
|
1222
|
+
* resulting screenshot so the model re-perceives (gated `approve`/`deny` like `Shell`).
|
|
1223
|
+
*
|
|
1224
|
+
* The typed action union stays WHOLE in the driver contract (`./types.ts`); this file only maps the
|
|
1225
|
+
* tool-boundary argument onto it. With no driver the tools report unavailability — they are added to the
|
|
1226
|
+
* default set ONLY when a driver is present (adapter-gated; there is NO host fallback — see
|
|
1227
|
+
* `createDefaultTools`).
|
|
1228
|
+
*/
|
|
1229
|
+
const UNAVAILABLE_MESSAGE = "Computer use is not available in this session (no driver injected).";
|
|
1230
|
+
const MouseButtonSchema = zod.z.enum([
|
|
1231
|
+
"left",
|
|
1232
|
+
"right",
|
|
1233
|
+
"middle"
|
|
1234
|
+
]);
|
|
1235
|
+
const PointSchema = zod.z.object({
|
|
1236
|
+
x: zod.z.number(),
|
|
1237
|
+
y: zod.z.number()
|
|
1238
|
+
});
|
|
1239
|
+
/**
|
|
1240
|
+
* The `Computer` action argument. A flat object (not a discriminated union) so it converts to JSON schema
|
|
1241
|
+
* — `type` selects the action and the remaining fields are validated per type in {@link buildAction}. The
|
|
1242
|
+
* strongly-typed discriminated union lives in the driver contract (`TComputerAction`).
|
|
1243
|
+
*/
|
|
1244
|
+
const ActionSchema = zod.z.object({
|
|
1245
|
+
type: zod.z.enum([
|
|
1246
|
+
"click",
|
|
1247
|
+
"double_click",
|
|
1248
|
+
"type",
|
|
1249
|
+
"keypress",
|
|
1250
|
+
"scroll",
|
|
1251
|
+
"drag",
|
|
1252
|
+
"wait",
|
|
1253
|
+
"takeover"
|
|
1254
|
+
]).describe("Which action to perform."),
|
|
1255
|
+
x: zod.z.number().optional().describe("X coordinate (click/double_click/scroll)."),
|
|
1256
|
+
y: zod.z.number().optional().describe("Y coordinate (click/double_click/scroll)."),
|
|
1257
|
+
button: MouseButtonSchema.optional().describe("Mouse button (click/double_click/drag)."),
|
|
1258
|
+
text: zod.z.string().optional().describe("Text to type (type)."),
|
|
1259
|
+
keys: zod.z.array(zod.z.string()).optional().describe("Keys to press as a chord (keypress)."),
|
|
1260
|
+
deltaX: zod.z.number().optional().describe("Horizontal wheel delta (scroll)."),
|
|
1261
|
+
deltaY: zod.z.number().optional().describe("Vertical wheel delta (scroll)."),
|
|
1262
|
+
path: zod.z.array(PointSchema).optional().describe("Points to drag through (drag)."),
|
|
1263
|
+
ms: zod.z.number().optional().describe("Milliseconds to wait (wait)."),
|
|
1264
|
+
reason: zod.z.string().optional().describe("Human-readable reason surfaced to the user (takeover).")
|
|
921
1265
|
});
|
|
1266
|
+
const ComputerSchema = zod.z.object({ action: ActionSchema.describe("The single mutating action to perform.") });
|
|
1267
|
+
const ComputerViewSchema = zod.z.object({});
|
|
1268
|
+
/** Raised when the tool-boundary action argument is missing fields the action type requires. */
|
|
1269
|
+
var InvalidComputerActionError = class extends Error {};
|
|
1270
|
+
function requireNumber(value, field, type) {
|
|
1271
|
+
if (typeof value !== "number") throw new InvalidComputerActionError(`Action '${type}' requires numeric '${field}'.`);
|
|
1272
|
+
return value;
|
|
1273
|
+
}
|
|
1274
|
+
/** Map the flat tool-boundary argument onto the strongly-typed driver action union. */
|
|
1275
|
+
function buildAction(args) {
|
|
1276
|
+
const button = args.button;
|
|
1277
|
+
switch (args.type) {
|
|
1278
|
+
case "click": return {
|
|
1279
|
+
type: "click",
|
|
1280
|
+
x: requireNumber(args.x, "x", "click"),
|
|
1281
|
+
y: requireNumber(args.y, "y", "click"),
|
|
1282
|
+
...button ? { button } : {}
|
|
1283
|
+
};
|
|
1284
|
+
case "double_click": return {
|
|
1285
|
+
type: "double_click",
|
|
1286
|
+
x: requireNumber(args.x, "x", "double_click"),
|
|
1287
|
+
y: requireNumber(args.y, "y", "double_click"),
|
|
1288
|
+
...button ? { button } : {}
|
|
1289
|
+
};
|
|
1290
|
+
case "type":
|
|
1291
|
+
if (typeof args.text !== "string") throw new InvalidComputerActionError("Action 'type' requires 'text'.");
|
|
1292
|
+
return {
|
|
1293
|
+
type: "type",
|
|
1294
|
+
text: args.text
|
|
1295
|
+
};
|
|
1296
|
+
case "keypress":
|
|
1297
|
+
if (!args.keys || args.keys.length === 0) throw new InvalidComputerActionError("Action 'keypress' requires non-empty 'keys'.");
|
|
1298
|
+
return {
|
|
1299
|
+
type: "keypress",
|
|
1300
|
+
keys: args.keys
|
|
1301
|
+
};
|
|
1302
|
+
case "scroll": return {
|
|
1303
|
+
type: "scroll",
|
|
1304
|
+
x: requireNumber(args.x, "x", "scroll"),
|
|
1305
|
+
y: requireNumber(args.y, "y", "scroll"),
|
|
1306
|
+
deltaX: requireNumber(args.deltaX, "deltaX", "scroll"),
|
|
1307
|
+
deltaY: requireNumber(args.deltaY, "deltaY", "scroll")
|
|
1308
|
+
};
|
|
1309
|
+
case "drag":
|
|
1310
|
+
if (!args.path || args.path.length < 2) throw new InvalidComputerActionError("Action 'drag' requires a 'path' of at least two points.");
|
|
1311
|
+
return {
|
|
1312
|
+
type: "drag",
|
|
1313
|
+
path: args.path,
|
|
1314
|
+
...button ? { button } : {}
|
|
1315
|
+
};
|
|
1316
|
+
case "wait": return {
|
|
1317
|
+
type: "wait",
|
|
1318
|
+
...typeof args.ms === "number" ? { ms: args.ms } : {}
|
|
1319
|
+
};
|
|
1320
|
+
case "takeover": return {
|
|
1321
|
+
type: "takeover",
|
|
1322
|
+
...args.reason ? { reason: args.reason } : {}
|
|
1323
|
+
};
|
|
1324
|
+
default: {
|
|
1325
|
+
const exhaustive = args.type;
|
|
1326
|
+
throw new InvalidComputerActionError(`Unknown action type: ${String(exhaustive)}`);
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
/** `ComputerView` — perceive the current surface (returns a screenshot). Gated `auto` like `Read`. */
|
|
1331
|
+
async function perceive(options) {
|
|
1332
|
+
if (!options.driver) return JSON.stringify({
|
|
1333
|
+
success: false,
|
|
1334
|
+
error: UNAVAILABLE_MESSAGE
|
|
1335
|
+
});
|
|
1336
|
+
const screenshot = await options.driver.screenshot();
|
|
1337
|
+
return JSON.stringify(screenshot ? {
|
|
1338
|
+
success: true,
|
|
1339
|
+
screenshot
|
|
1340
|
+
} : {
|
|
1341
|
+
success: true,
|
|
1342
|
+
takeover: true
|
|
1343
|
+
});
|
|
1344
|
+
}
|
|
1345
|
+
/** `Computer` — execute one typed mutating action and return the resulting screenshot. Gated like `Shell`. */
|
|
1346
|
+
async function act(args, options) {
|
|
1347
|
+
if (!options.driver) return JSON.stringify({
|
|
1348
|
+
success: false,
|
|
1349
|
+
error: UNAVAILABLE_MESSAGE
|
|
1350
|
+
});
|
|
1351
|
+
let action;
|
|
1352
|
+
try {
|
|
1353
|
+
action = buildAction(args.action);
|
|
1354
|
+
} catch (err) {
|
|
1355
|
+
return JSON.stringify({
|
|
1356
|
+
success: false,
|
|
1357
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1358
|
+
});
|
|
1359
|
+
}
|
|
1360
|
+
const outcome = await options.driver.act(action);
|
|
1361
|
+
const result = {
|
|
1362
|
+
success: true,
|
|
1363
|
+
...outcome.screenshot ? { screenshot: outcome.screenshot } : {},
|
|
1364
|
+
...outcome.takeover ? { takeover: true } : {}
|
|
1365
|
+
};
|
|
1366
|
+
return JSON.stringify(result);
|
|
1367
|
+
}
|
|
1368
|
+
/** Build the `ComputerView` perceive tool over the injected driver. */
|
|
1369
|
+
function createComputerViewTool(options = {}) {
|
|
1370
|
+
return createZodFunctionTool("ComputerView", "Perceive the computer/browser surface: capture and return a screenshot of the current screen so you can reason about what to do next. Read-only — it never changes anything.", ComputerViewSchema, async () => perceive(options));
|
|
1371
|
+
}
|
|
1372
|
+
/** Build the `Computer` act tool over the injected driver. */
|
|
1373
|
+
function createComputerActTool(options = {}) {
|
|
1374
|
+
return createZodFunctionTool("Computer", "Perform one mutating action on the computer/browser surface (click, double_click, type, keypress, scroll, drag, wait, or takeover) and return the resulting screenshot. Use `takeover` to hand control to the human for sensitive input (credentials/payment); perception is paused during a takeover.", ComputerSchema, async (params) => act(params, options));
|
|
1375
|
+
}
|
|
1376
|
+
/**
|
|
1377
|
+
* Create BOTH computer-use tools — `ComputerView` (perceive) and `Computer` (act) — over one injected
|
|
1378
|
+
* driver. Mirrors `create*Tool(options)`; returns the pair so the assembly layer can spread them into the
|
|
1379
|
+
* default set adapter-gated (see `createDefaultTools`).
|
|
1380
|
+
*/
|
|
1381
|
+
function createComputerTool(options = {}) {
|
|
1382
|
+
return [createComputerViewTool(options), createComputerActTool(options)];
|
|
1383
|
+
}
|
|
1384
|
+
//#endregion
|
|
1385
|
+
//#region src/computer-use/page-computer-driver.ts
|
|
1386
|
+
const DEFAULT_MEDIA_TYPE = "image/png";
|
|
1387
|
+
const DEFAULT_WAIT_MS = 500;
|
|
1388
|
+
/** Encode raw screenshot bytes to base64 (accepts the page's `Uint8Array` or an already-encoded string). */
|
|
1389
|
+
function encodeScreenshot(bytes) {
|
|
1390
|
+
if (typeof bytes === "string") return bytes;
|
|
1391
|
+
return Buffer.from(bytes).toString("base64");
|
|
1392
|
+
}
|
|
1393
|
+
var PageComputerDriver = class {
|
|
1394
|
+
page;
|
|
1395
|
+
mediaType;
|
|
1396
|
+
defaultWaitMs;
|
|
1397
|
+
suspended = false;
|
|
1398
|
+
constructor(options) {
|
|
1399
|
+
this.page = options.page;
|
|
1400
|
+
this.mediaType = options.mediaType ?? DEFAULT_MEDIA_TYPE;
|
|
1401
|
+
this.defaultWaitMs = options.defaultWaitMs ?? DEFAULT_WAIT_MS;
|
|
1402
|
+
}
|
|
1403
|
+
async capture() {
|
|
1404
|
+
const type = this.mediaType === "image/jpeg" ? "jpeg" : "png";
|
|
1405
|
+
return {
|
|
1406
|
+
data: encodeScreenshot(await this.page.screenshot({ type })),
|
|
1407
|
+
mediaType: this.mediaType
|
|
1408
|
+
};
|
|
1409
|
+
}
|
|
1410
|
+
async wait(ms) {
|
|
1411
|
+
if (this.page.waitForTimeout) {
|
|
1412
|
+
await this.page.waitForTimeout(ms);
|
|
1413
|
+
return;
|
|
1414
|
+
}
|
|
1415
|
+
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
1416
|
+
}
|
|
1417
|
+
async screenshot() {
|
|
1418
|
+
if (this.suspended) return;
|
|
1419
|
+
return this.capture();
|
|
1420
|
+
}
|
|
1421
|
+
async act(action) {
|
|
1422
|
+
if (action.type === "takeover") {
|
|
1423
|
+
await this.beginTakeover(action.reason);
|
|
1424
|
+
return { takeover: true };
|
|
1425
|
+
}
|
|
1426
|
+
if (this.suspended) return { takeover: true };
|
|
1427
|
+
const { mouse, keyboard } = this.page;
|
|
1428
|
+
switch (action.type) {
|
|
1429
|
+
case "click":
|
|
1430
|
+
await mouse.click(action.x, action.y, action.button ? { button: action.button } : void 0);
|
|
1431
|
+
break;
|
|
1432
|
+
case "double_click":
|
|
1433
|
+
await mouse.click(action.x, action.y, {
|
|
1434
|
+
clickCount: 2,
|
|
1435
|
+
...action.button ? { button: action.button } : {}
|
|
1436
|
+
});
|
|
1437
|
+
break;
|
|
1438
|
+
case "type":
|
|
1439
|
+
await keyboard.type(action.text);
|
|
1440
|
+
break;
|
|
1441
|
+
case "keypress":
|
|
1442
|
+
await keyboard.press(action.keys.join("+"));
|
|
1443
|
+
break;
|
|
1444
|
+
case "scroll":
|
|
1445
|
+
await mouse.move(action.x, action.y);
|
|
1446
|
+
await mouse.wheel(action.deltaX, action.deltaY);
|
|
1447
|
+
break;
|
|
1448
|
+
case "drag":
|
|
1449
|
+
await this.performDrag(action);
|
|
1450
|
+
break;
|
|
1451
|
+
case "wait":
|
|
1452
|
+
await this.wait(action.ms ?? this.defaultWaitMs);
|
|
1453
|
+
break;
|
|
1454
|
+
}
|
|
1455
|
+
return { screenshot: await this.capture() };
|
|
1456
|
+
}
|
|
1457
|
+
/** Move the pointer along a multi-point path with the button held (mouse down → moves → up). */
|
|
1458
|
+
async performDrag(action) {
|
|
1459
|
+
if (action.path.length < 2) throw new Error("computer drag requires a path of at least 2 points (start + end)");
|
|
1460
|
+
const { mouse } = this.page;
|
|
1461
|
+
const [first, ...rest] = action.path;
|
|
1462
|
+
const button = action.button ? { button: action.button } : void 0;
|
|
1463
|
+
await mouse.move(first.x, first.y);
|
|
1464
|
+
await mouse.down(button);
|
|
1465
|
+
for (const point of rest) await mouse.move(point.x, point.y);
|
|
1466
|
+
await mouse.up(button);
|
|
1467
|
+
}
|
|
1468
|
+
async beginTakeover(_reason) {
|
|
1469
|
+
this.suspended = true;
|
|
1470
|
+
}
|
|
1471
|
+
async endTakeover() {
|
|
1472
|
+
this.suspended = false;
|
|
1473
|
+
}
|
|
1474
|
+
};
|
|
1475
|
+
//#endregion
|
|
1476
|
+
//#region src/builtins/shell-tool-description.ts
|
|
1477
|
+
/**
|
|
1478
|
+
* Dedicated-tool routing hints, keyed by the sibling tool's registered name. A hint is only
|
|
1479
|
+
* emitted when that sibling is actually part of the registered tool set (NEUT-002) — the
|
|
1480
|
+
* description must not route the model to tools that do not exist in a given assembly.
|
|
1481
|
+
*/
|
|
1482
|
+
const SIBLING_ROUTING_HINTS = [
|
|
1483
|
+
{
|
|
1484
|
+
toolName: "Glob",
|
|
1485
|
+
hint: " - File search: Use Glob (NOT find or ls)"
|
|
1486
|
+
},
|
|
1487
|
+
{
|
|
1488
|
+
toolName: "Grep",
|
|
1489
|
+
hint: " - Content search: Use Grep (NOT grep or rg)"
|
|
1490
|
+
},
|
|
1491
|
+
{
|
|
1492
|
+
toolName: "Read",
|
|
1493
|
+
hint: " - Read files: Use Read (NOT cat/head/tail)"
|
|
1494
|
+
},
|
|
1495
|
+
{
|
|
1496
|
+
toolName: "Edit",
|
|
1497
|
+
hint: " - Edit files: Use Edit (NOT sed/awk)"
|
|
1498
|
+
}
|
|
1499
|
+
];
|
|
1500
|
+
/**
|
|
1501
|
+
* Build the OS-aware tool description so the model writes syntax the host shell can run.
|
|
1502
|
+
* When `availableTools` is provided, sibling routing hints are restricted to tools in that set;
|
|
1503
|
+
* when omitted, the full default hint set is included (default assembly registers all siblings).
|
|
1504
|
+
*/
|
|
1505
|
+
function buildShellToolDescription(shell, availableTools) {
|
|
1506
|
+
const hints = availableTools ? SIBLING_ROUTING_HINTS.filter((entry) => availableTools.includes(entry.toolName)) : SIBLING_ROUTING_HINTS;
|
|
1507
|
+
const routingBlock = hints.length > 0 ? [`IMPORTANT: Avoid using this tool to run \`find\`, \`grep\`, \`cat\`, \`head\`, \`tail\`, \`sed\`, \`awk\`, or \`echo\` commands. Instead, use the appropriate dedicated tool:`, ...hints.map((entry) => entry.hint)] : [];
|
|
1508
|
+
return [
|
|
1509
|
+
`Executes a command in the host shell and returns its output.`,
|
|
1510
|
+
``,
|
|
1511
|
+
`Active shell: ${shell.label}. ${shell.syntaxHint}`,
|
|
1512
|
+
``,
|
|
1513
|
+
`Each command runs in a fresh shell in workingDirectory (default: the configured working directory); no shell state carries over between calls.`,
|
|
1514
|
+
``,
|
|
1515
|
+
...routingBlock
|
|
1516
|
+
].join("\n");
|
|
1517
|
+
}
|
|
1518
|
+
//#endregion
|
|
1519
|
+
//#region src/builtins/shell-tool.ts
|
|
1520
|
+
/**
|
|
1521
|
+
* ShellTool — execute a host shell command via child_process.spawn (TERM-008).
|
|
1522
|
+
*
|
|
1523
|
+
* Cross-platform: the shell is resolved per OS through `resolvePlatformShell()` (POSIX `sh`/`bash`,
|
|
1524
|
+
* Windows PowerShell). The tool name is `Shell` and its description is built dynamically from the
|
|
1525
|
+
* resolved shell so the model is told the active shell/OS and writes the right syntax.
|
|
1526
|
+
*
|
|
1527
|
+
* Returns an IToolInvocationResult JSON string. A non-zero exit is returned as success:true with
|
|
1528
|
+
* exitCode set (the command ran, it just exited non-zero — the LLM decides what to do with that).
|
|
1529
|
+
*
|
|
1530
|
+
* ## SEC-007 — why `workingDirectory` is NOT path-contained (a deliberate decision, not an omission)
|
|
1531
|
+
*
|
|
1532
|
+
* `Read`/`Write`/`Edit` are contained by `checkPathWithinCwd`, and SEC-007 extended that to `Glob`
|
|
1533
|
+
* and `Grep`. This tool is deliberately excluded, and the reason is what the tool IS: it runs an
|
|
1534
|
+
* arbitrary command in a shell. A guard on `cwd` is undone by the first `cd ..` — or by an absolute
|
|
1535
|
+
* path in the command itself — so it would constrain nothing an attacker-controlled command cannot
|
|
1536
|
+
* trivially step around, while LOOKING like a boundary in the code and in review.
|
|
1537
|
+
*
|
|
1538
|
+
* That appearance is the actual hazard. SEC-006's R9 lesson was "'the guard is still there' is not a
|
|
1539
|
+
* verdict": a check that reads as containment but is not one is worse than no check, because the next
|
|
1540
|
+
* reviewer stops asking. The real boundary for this tool is the permission layer (every invocation is
|
|
1541
|
+
* permission-gated at call time) and the sandbox seam below — which is why SEC-006 already recorded
|
|
1542
|
+
* `js/indirect-command-line-injection` at the spawn site as a false positive on those same grounds.
|
|
1543
|
+
*
|
|
1544
|
+
* What the containment root DOES do here: it supplies the DEFAULT working directory. Binding a tool
|
|
1545
|
+
* to a session root and then silently running its commands in `process.cwd()` was a real defect — an
|
|
1546
|
+
* assembly that scoped its file tools to a workspace still ran `Shell` wherever the host process
|
|
1547
|
+
* happened to be started.
|
|
1548
|
+
*/
|
|
1549
|
+
/** POSIX children are spawned detached so a process-group kill reaps grandchildren (CORE-023). */
|
|
1550
|
+
const SPAWN_DETACHED = process.platform !== "win32";
|
|
1551
|
+
const DEFAULT_TIMEOUT_MS$2 = 12e4;
|
|
1552
|
+
/** ARCH-056: most bytes retained per stream while the child runs (head); the rest is dropped. */
|
|
1553
|
+
const MAX_CAPTURED_OUTPUT_BYTES = 2e6;
|
|
1554
|
+
const ShellSchema = zod.z.object({
|
|
1555
|
+
command: zod.z.string().describe("The shell command to execute"),
|
|
1556
|
+
timeout: zod.z.number().optional().describe("Optional timeout in milliseconds (max 600000). Default is 120000 (2 minutes)"),
|
|
1557
|
+
workingDirectory: zod.z.string().optional().describe("Working directory for the command. Defaults to the current working directory")
|
|
1558
|
+
});
|
|
1559
|
+
/** Run a shell command through the sandbox client, surfacing failures as a structured result. */
|
|
1560
|
+
async function runInSandbox(command, timeout, workingDirectory, options) {
|
|
1561
|
+
try {
|
|
1562
|
+
const sandboxResult = await options.sandboxClient.run(command, {
|
|
1563
|
+
timeoutMs: timeout,
|
|
1564
|
+
workingDirectory
|
|
1565
|
+
});
|
|
1566
|
+
const result = {
|
|
1567
|
+
success: true,
|
|
1568
|
+
output: sandboxResult.stderr ? `${sandboxResult.stdout}\nstderr:\n${sandboxResult.stderr}` : sandboxResult.stdout,
|
|
1569
|
+
exitCode: sandboxResult.exitCode
|
|
1570
|
+
};
|
|
1571
|
+
return JSON.stringify(result);
|
|
1572
|
+
} catch (err) {
|
|
1573
|
+
const result = {
|
|
1574
|
+
success: false,
|
|
1575
|
+
output: "",
|
|
1576
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1577
|
+
};
|
|
1578
|
+
return JSON.stringify(result);
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
/**
|
|
1582
|
+
* Run a shell command and return stdout + stderr.
|
|
1583
|
+
* Resolves with the IToolInvocationResult JSON string.
|
|
1584
|
+
*/
|
|
1585
|
+
async function runShell(args, options, shell, signal, traceEnv) {
|
|
1586
|
+
const { command, timeout: rawTimeout = DEFAULT_TIMEOUT_MS$2, workingDirectory } = args;
|
|
1587
|
+
const timeout = Math.min(rawTimeout, 6e5);
|
|
1588
|
+
const effectiveCwd = workingDirectory ?? options.cwd;
|
|
1589
|
+
if (effectiveCwd === void 0) return JSON.stringify({
|
|
1590
|
+
success: false,
|
|
1591
|
+
output: "",
|
|
1592
|
+
error: "Shell tool has no working directory: it was constructed without a `cwd` (ARCH-010). This is an assembly bug — the tool would otherwise run in whatever directory the host process was started in."
|
|
1593
|
+
});
|
|
1594
|
+
if (options.sandboxClient && options.sandboxClient.wrapCommand === void 0) return runInSandbox(command, timeout, workingDirectory ?? options.cwd, options);
|
|
1595
|
+
const hostInvocation = {
|
|
1596
|
+
command: shell.command,
|
|
1597
|
+
args: shell.commandArgs(command),
|
|
1598
|
+
cwd: effectiveCwd
|
|
1599
|
+
};
|
|
1600
|
+
const invocation = options.sandboxClient?.wrapCommand?.(hostInvocation, command) ?? hostInvocation;
|
|
1601
|
+
let released = false;
|
|
1602
|
+
const release = () => {
|
|
1603
|
+
if (released) return void 0;
|
|
1604
|
+
released = true;
|
|
1605
|
+
try {
|
|
1606
|
+
return invocation.afterExit?.();
|
|
1607
|
+
} catch (error) {
|
|
1608
|
+
return `[sandbox] clean-up failed: ${error instanceof Error ? error.message : String(error)}`;
|
|
1609
|
+
}
|
|
1610
|
+
};
|
|
1611
|
+
if (signal?.aborted) {
|
|
1612
|
+
release();
|
|
1613
|
+
return JSON.stringify({
|
|
1614
|
+
success: false,
|
|
1615
|
+
output: "",
|
|
1616
|
+
error: "Aborted before start"
|
|
1617
|
+
});
|
|
1618
|
+
}
|
|
1619
|
+
return new Promise((resolve) => {
|
|
1620
|
+
const stdoutOutput = (0, _robota_sdk_agent_core.createBoundedOutput)({ maxBytes: MAX_CAPTURED_OUTPUT_BYTES });
|
|
1621
|
+
const stderrOutput = (0, _robota_sdk_agent_core.createBoundedOutput)({ maxBytes: MAX_CAPTURED_OUTPUT_BYTES });
|
|
1622
|
+
let timedOut = false;
|
|
1623
|
+
let settled = false;
|
|
1624
|
+
let child;
|
|
1625
|
+
try {
|
|
1626
|
+
child = (0, node_child_process.spawn)(invocation.command, [...invocation.args], {
|
|
1627
|
+
cwd: invocation.cwd,
|
|
1628
|
+
env: traceEnv === void 0 ? process.env : (0, _robota_sdk_agent_core.subprocessTraceEnvironment)(process.env, traceEnv),
|
|
1629
|
+
stdio: [
|
|
1630
|
+
"pipe",
|
|
1631
|
+
"pipe",
|
|
1632
|
+
"pipe",
|
|
1633
|
+
...(invocation.inputDescriptors ?? []).map(() => "pipe")
|
|
1634
|
+
],
|
|
1635
|
+
detached: SPAWN_DETACHED
|
|
1636
|
+
});
|
|
1637
|
+
} catch (error) {
|
|
1638
|
+
const note = release();
|
|
1639
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1640
|
+
resolve(JSON.stringify({
|
|
1641
|
+
success: false,
|
|
1642
|
+
output: note ?? "",
|
|
1643
|
+
error: message
|
|
1644
|
+
}));
|
|
1645
|
+
return;
|
|
1646
|
+
}
|
|
1647
|
+
(invocation.inputDescriptors ?? []).forEach((data, index) => {
|
|
1648
|
+
const stream = child.stdio[index + 3];
|
|
1649
|
+
stream?.on("error", () => void 0);
|
|
1650
|
+
stream?.end(Buffer.from(data));
|
|
1651
|
+
});
|
|
1652
|
+
child.stdin?.end();
|
|
1653
|
+
child.stdout?.on("data", (chunk) => {
|
|
1654
|
+
stdoutOutput.append(chunk);
|
|
1655
|
+
});
|
|
1656
|
+
child.stderr?.on("data", (chunk) => {
|
|
1657
|
+
stderrOutput.append(chunk);
|
|
1658
|
+
});
|
|
1659
|
+
const timer = setTimeout(() => {
|
|
1660
|
+
timedOut = true;
|
|
1661
|
+
(0, _robota_sdk_agent_process.killProcessTree)(child, { processGroup: SPAWN_DETACHED });
|
|
1662
|
+
settle({
|
|
1663
|
+
success: false,
|
|
1664
|
+
output: stdoutOutput.toString(),
|
|
1665
|
+
error: `Command timed out after ${timeout}ms`
|
|
1666
|
+
});
|
|
1667
|
+
}, timeout);
|
|
1668
|
+
function settle(result) {
|
|
1669
|
+
if (settled) return;
|
|
1670
|
+
settled = true;
|
|
1671
|
+
clearTimeout(timer);
|
|
1672
|
+
signal?.removeEventListener("abort", onAbort);
|
|
1673
|
+
resolve(JSON.stringify(result));
|
|
1674
|
+
}
|
|
1675
|
+
function onAbort() {
|
|
1676
|
+
(0, _robota_sdk_agent_process.killProcessTree)(child, { processGroup: SPAWN_DETACHED });
|
|
1677
|
+
settle({
|
|
1678
|
+
success: false,
|
|
1679
|
+
output: stdoutOutput.toString(),
|
|
1680
|
+
error: "Aborted"
|
|
1681
|
+
});
|
|
1682
|
+
}
|
|
1683
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
1684
|
+
child.on("error", (err) => {
|
|
1685
|
+
if (child.pid === void 0) release();
|
|
1686
|
+
settle({
|
|
1687
|
+
success: false,
|
|
1688
|
+
output: "",
|
|
1689
|
+
error: err.message
|
|
1690
|
+
});
|
|
1691
|
+
});
|
|
1692
|
+
child.on("close", (code) => {
|
|
1693
|
+
const note = release();
|
|
1694
|
+
if (timedOut) {
|
|
1695
|
+
settle({
|
|
1696
|
+
success: false,
|
|
1697
|
+
output: stdoutOutput.toString(),
|
|
1698
|
+
error: `Command timed out after ${timeout}ms`,
|
|
1699
|
+
exitCode: code ?? void 0
|
|
1700
|
+
});
|
|
1701
|
+
return;
|
|
1702
|
+
}
|
|
1703
|
+
const stdout = stdoutOutput.toString();
|
|
1704
|
+
const stderr = stderrOutput.toString();
|
|
1705
|
+
const exitCode = code ?? 0;
|
|
1706
|
+
const combined = stderr ? `${stdout}\nstderr:\n${stderr}` : stdout;
|
|
1707
|
+
settle({
|
|
1708
|
+
success: true,
|
|
1709
|
+
output: note === void 0 ? combined : `${combined}\n${note}`,
|
|
1710
|
+
exitCode
|
|
1711
|
+
});
|
|
1712
|
+
});
|
|
1713
|
+
});
|
|
1714
|
+
}
|
|
1715
|
+
/**
|
|
1716
|
+
* Build a host-shell command tool under a given registered name. Both `Shell` and the
|
|
1717
|
+
* model-familiar `Bash` are registered as aliases of this one OS-aware implementation
|
|
1718
|
+
* (TERM-008): the shell is resolved per OS and the description names the active shell so the
|
|
1719
|
+
* model writes the right syntax regardless of which alias it calls.
|
|
1720
|
+
*/
|
|
1721
|
+
function createHostShellTool(name, options) {
|
|
1722
|
+
const shell = (0, _robota_sdk_agent_core.resolvePlatformShell)({ executable: options.shellExecutable });
|
|
1723
|
+
return createZodFunctionTool(name, options.description ?? buildShellToolDescription(shell, options.availableTools), ShellSchema, async (params, context) => {
|
|
1724
|
+
return runShell(params, options, shell, context?.signal, context?.shellTraceEnv);
|
|
1725
|
+
});
|
|
1726
|
+
}
|
|
1727
|
+
/**
|
|
1728
|
+
* Create a `Shell` tool instance — register with the Robota agent tools registry.
|
|
1729
|
+
* The description is resolved at creation time for the host's active shell.
|
|
1730
|
+
*/
|
|
1731
|
+
function createShellTool(options) {
|
|
1732
|
+
return createHostShellTool("Shell", options);
|
|
1733
|
+
}
|
|
1734
|
+
/**
|
|
1735
|
+
* Create a `Bash` tool instance — the model-familiar alias of the same OS-aware shell tool.
|
|
1736
|
+
*/
|
|
1737
|
+
function createBashTool(options) {
|
|
1738
|
+
return createHostShellTool("Bash", options);
|
|
1739
|
+
}
|
|
1740
|
+
//#endregion
|
|
1741
|
+
//#region src/builtins/path-guard.ts
|
|
1742
|
+
/**
|
|
1743
|
+
* Returns a JSON-serialized IToolInvocationResult error when filePath is outside cwd, or when NO
|
|
1744
|
+
* containment root is configured. Returns undefined only when the path is inside a configured root.
|
|
1745
|
+
*
|
|
1746
|
+
* This sentence used to end "or cwd is not set" — the fail-open default ARCH-010 removed. It sat
|
|
1747
|
+
* directly above the two functions that implement the distinction, which is the worst place for a
|
|
1748
|
+
* comment to say the opposite of the code.
|
|
1749
|
+
*
|
|
1750
|
+
* SEC-006: containment is decided on the CANONICAL (symlink-resolved) paths, via the shared
|
|
1751
|
+
* `isPathInside` SSOT in agent-core. A purely lexical `resolve()` + `startsWith` comparison let
|
|
1752
|
+
* `<cwd>/link/secret` through when `link -> /etc`, because `resolve` does not consult the filesystem
|
|
1753
|
+
* and so cannot see a symlink — while the subsequent `readFile`/`writeFile` followed the link out of
|
|
1754
|
+
* the sandbox. For `Write`/`Edit` that meant creating files anywhere the process could reach, and
|
|
1755
|
+
* since symlinks are ordinary committed git content, pointing the agent at an untrusted clone was
|
|
1756
|
+
* enough to arm it.
|
|
1757
|
+
*
|
|
1758
|
+
* The same defect existed in the CLI's monitor asset server; both now share one implementation,
|
|
1759
|
+
* because two containment checks that can disagree are their own defect.
|
|
1760
|
+
*/
|
|
1761
|
+
/**
|
|
1762
|
+
* Whether a host path is inside the tool's containment root — the single predicate every builtin
|
|
1763
|
+
* asks, whatever it does with the answer.
|
|
1764
|
+
*
|
|
1765
|
+
* `checkPathWithinCwd` turns a `false` into the tool-result error a tool RETURNS; the enumerating
|
|
1766
|
+
* tools (`Glob`, `Grep`) instead SKIP the entry mid-walk and must not fabricate an error per file.
|
|
1767
|
+
* Both ask this one question, which asks agent-core's `isPathInside` SSOT — so there is no second
|
|
1768
|
+
* containment rule that could disagree with the first (SEC-006's stated defect, SEC-007 keeping it
|
|
1769
|
+
* true as the guard's reach widens).
|
|
1770
|
+
*
|
|
1771
|
+
* `cwd === undefined` means no containment root is configured, and the answer is NO — ARCH-010.
|
|
1772
|
+
*
|
|
1773
|
+
* This used to return `true` there: with no root, everything was inside it. A guard whose default is
|
|
1774
|
+
* "allow" is not a guard, it is a guard that has to be remembered, and the architecture audit found
|
|
1775
|
+
* three independent layers that had forgotten. `pack-coding` had already written the consequence into
|
|
1776
|
+
* its own source — "file tools constructed with no options carry a DISARMED working-directory guard:
|
|
1777
|
+
* their `Read` will happily return `/etc/hostname`" — and the child-process subagent worker called
|
|
1778
|
+
* `createDefaultTools()` with no argument, so a subagent got exactly that. Measured, not inferred:
|
|
1779
|
+
* before this change a rootless `Read` of `/etc/hostname` returned the file.
|
|
1780
|
+
*
|
|
1781
|
+
* Refusing instead means a construction site that forgets the root fails loudly on its first file
|
|
1782
|
+
* access rather than silently running unconfined. The root is also required by the tool factories now,
|
|
1783
|
+
* so reaching this branch at all is an assembly bug — which is why the error says so specifically
|
|
1784
|
+
* rather than reporting an ordinary out-of-root path.
|
|
1785
|
+
*/
|
|
1786
|
+
function isWithinCwd(filePath, cwd) {
|
|
1787
|
+
if (cwd === void 0) return false;
|
|
1788
|
+
return (0, _robota_sdk_agent_core_node.isPathInside)(cwd, filePath);
|
|
1789
|
+
}
|
|
1790
|
+
/**
|
|
1791
|
+
* Where a RELATIVE host path the model supplied is anchored: the containment root, never
|
|
1792
|
+
* `process.cwd()` (issue #2429). `Read`/`Write`/`Edit` declare `filePath` absolute, but nothing
|
|
1793
|
+
* makes the model comply, and `isPathInside` canonicalises a relative candidate against the PROCESS
|
|
1794
|
+
* directory — so a relative path was confined to one root and judged against another. Same rule as
|
|
1795
|
+
* `resolveSearchRoot` for the enumerating tools. With no root there is nothing to anchor to; the path
|
|
1796
|
+
* is returned as written and `checkPathWithinCwd` refuses it (ARCH-010).
|
|
1797
|
+
*/
|
|
1798
|
+
function resolveHostPath(filePath, cwd) {
|
|
1799
|
+
if (cwd === void 0) return filePath;
|
|
1800
|
+
return (0, node_path.resolve)(cwd, filePath);
|
|
1801
|
+
}
|
|
1802
|
+
function checkPathWithinCwd(filePath, cwd) {
|
|
1803
|
+
if (cwd === void 0) {
|
|
1804
|
+
const result = {
|
|
1805
|
+
success: false,
|
|
1806
|
+
output: "",
|
|
1807
|
+
error: `Access denied: "${filePath}" cannot be checked because no containment root is configured for this tool. This is an assembly bug, not a path problem — the tool was constructed without a \`cwd\`, so it has no boundary to enforce (ARCH-010).`
|
|
1808
|
+
};
|
|
1809
|
+
return JSON.stringify(result);
|
|
1810
|
+
}
|
|
1811
|
+
if (!isWithinCwd(filePath, cwd)) {
|
|
1812
|
+
const result = {
|
|
1813
|
+
success: false,
|
|
1814
|
+
output: "",
|
|
1815
|
+
error: `Access denied: "${filePath}" is outside the working directory`
|
|
1816
|
+
};
|
|
1817
|
+
return JSON.stringify(result);
|
|
1818
|
+
}
|
|
1819
|
+
}
|
|
1820
|
+
/**
|
|
1821
|
+
* Resolve an LLM-supplied search root for an ENUMERATING tool, and refuse one that escapes (SEC-007).
|
|
1822
|
+
*
|
|
1823
|
+
* A relative `requested` anchors to the CONTAINMENT ROOT, not to `process.cwd()`: anchoring them to
|
|
1824
|
+
* two different directories is how a "contained" search silently starts somewhere else. `error`
|
|
1825
|
+
* carries the tool-result JSON to return, or is `undefined` when the root is allowed.
|
|
1826
|
+
*
|
|
1827
|
+
* With no root there is nothing to anchor to, so this refuses rather than reaching for the process
|
|
1828
|
+
* directory (ARCH-010). The previous `cwd ?? process.cwd()` was that reach: harmless once the guard
|
|
1829
|
+
* below refuses anyway, but it read as a supported fallback, which is the pattern being removed.
|
|
1830
|
+
*/
|
|
1831
|
+
function resolveSearchRoot(requested, cwd) {
|
|
1832
|
+
if (cwd === void 0) return {
|
|
1833
|
+
root: "",
|
|
1834
|
+
error: checkPathWithinCwd(requested ?? "", void 0)
|
|
1835
|
+
};
|
|
1836
|
+
const root = requested ? (0, node_path.resolve)(cwd, requested) : cwd;
|
|
1837
|
+
return {
|
|
1838
|
+
root,
|
|
1839
|
+
error: checkPathWithinCwd(root, cwd)
|
|
1840
|
+
};
|
|
1841
|
+
}
|
|
1842
|
+
//#endregion
|
|
1843
|
+
//#region src/builtins/read-tool.ts
|
|
1844
|
+
/**
|
|
1845
|
+
* ReadTool — read a file and return its contents with line numbers (cat -n style).
|
|
1846
|
+
*
|
|
1847
|
+
* Supports offset/limit for partial reads. Detects binary files and refuses to
|
|
1848
|
+
* return their raw bytes. Default limit is 2000 lines.
|
|
1849
|
+
*/
|
|
1850
|
+
const DEFAULT_READ_DESCRIPTION = "Reads a file from the local filesystem.\n\nBy default, reads up to 2000 lines from the beginning of the file. You can optionally specify offset and limit for partial reads.\n\nResults are returned using cat -n format, with line numbers starting at 1.\n\nThe filePath parameter must be an absolute path, not a relative path.";
|
|
1851
|
+
const DEFAULT_LIMIT$1 = 2e3;
|
|
1852
|
+
const MAX_READ_BYTES = 4 * 1024 * 1024;
|
|
1853
|
+
const READ_CHUNK_BYTES$2 = 64 * 1024;
|
|
1854
|
+
/** A budget refusal is a hard failure so a workflow cannot treat it as file content. */
|
|
1855
|
+
var ReadByteLimitError = class extends _robota_sdk_agent_core.ToolExecutionError {
|
|
1856
|
+
boundary;
|
|
1857
|
+
constructor(boundary) {
|
|
1858
|
+
super(`Read ${boundary} exceeds its UTF-8 byte limit`, "Read");
|
|
1859
|
+
this.boundary = boundary;
|
|
1860
|
+
}
|
|
1861
|
+
};
|
|
1862
|
+
/** Abort is a hard failure; the workflow must not accept a partial read. */
|
|
1863
|
+
var ReadCancelledError = class extends _robota_sdk_agent_core.ToolExecutionError {
|
|
1864
|
+
constructor() {
|
|
1865
|
+
super("Read cancelled", "Read");
|
|
1866
|
+
}
|
|
1867
|
+
};
|
|
1868
|
+
const ReadSchema = zod.z.object({
|
|
1869
|
+
filePath: zod.z.string().describe("The absolute path to the file to read"),
|
|
1870
|
+
offset: zod.z.number().optional().describe("The line number to start reading from (1-based). Only provide if the file is too large to read at once"),
|
|
1871
|
+
limit: zod.z.number().optional().describe(`The number of lines to read (default: ${DEFAULT_LIMIT$1}). Only provide if the file is too large to read at once`)
|
|
1872
|
+
});
|
|
1873
|
+
/**
|
|
1874
|
+
* Heuristic binary detection: scan the first 8 KB for null bytes.
|
|
1875
|
+
*/
|
|
922
1876
|
function isBinary(buffer) {
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
}
|
|
927
|
-
return false;
|
|
1877
|
+
const checkLength = Math.min(buffer.length, 8192);
|
|
1878
|
+
for (let i = 0; i < checkLength; i++) if (buffer[i] === 0) return true;
|
|
1879
|
+
return false;
|
|
928
1880
|
}
|
|
1881
|
+
/**
|
|
1882
|
+
* Format lines with 1-based line numbers in cat -n style.
|
|
1883
|
+
* Pads line number to the width of the highest line number.
|
|
1884
|
+
*/
|
|
929
1885
|
function formatWithLineNumbers(lines, startLine) {
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
}
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
}
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
)
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1886
|
+
const lastLineNum = startLine + lines.length - 1;
|
|
1887
|
+
const width = String(lastLineNum).length;
|
|
1888
|
+
return lines.map((line, idx) => {
|
|
1889
|
+
return `${String(startLine + idx).padStart(width, " ")}\t${line}`;
|
|
1890
|
+
}).join("\n");
|
|
1891
|
+
}
|
|
1892
|
+
function formatReadResult(filePath, content, startLine, limit) {
|
|
1893
|
+
const selectedLines = [];
|
|
1894
|
+
let selectedMinimumBytes = 0;
|
|
1895
|
+
let totalLines = 0;
|
|
1896
|
+
let lineStart = 0;
|
|
1897
|
+
const selectedStart = Math.trunc(startLine - 1);
|
|
1898
|
+
const selectedEnd = Math.trunc(startLine - 1 + limit);
|
|
1899
|
+
while (lineStart < content.length) {
|
|
1900
|
+
const newline = content.indexOf("\n", lineStart);
|
|
1901
|
+
const lineEnd = newline === -1 ? content.length : newline;
|
|
1902
|
+
totalLines++;
|
|
1903
|
+
if (totalLines > selectedStart && totalLines <= selectedEnd) {
|
|
1904
|
+
const line = content.slice(lineStart, lineEnd);
|
|
1905
|
+
selectedMinimumBytes += Buffer.byteLength(line, "utf8") + String(startLine + selectedLines.length).length + 1;
|
|
1906
|
+
if (selectedMinimumBytes > MAX_READ_BYTES) throw new ReadByteLimitError("output");
|
|
1907
|
+
selectedLines.push(line);
|
|
1908
|
+
}
|
|
1909
|
+
if (newline === -1) break;
|
|
1910
|
+
lineStart = newline + 1;
|
|
1911
|
+
}
|
|
1912
|
+
const returnedLines = selectedLines.length;
|
|
1913
|
+
const header = returnedLines < totalLines ? `[File: ${filePath} (lines ${startLine}-${startLine + returnedLines - 1} of ${totalLines})]\n` : `[File: ${filePath} (${totalLines} lines)]\n`;
|
|
1914
|
+
const width = String(startLine + returnedLines - 1).length;
|
|
1915
|
+
let outputBytes = Buffer.byteLength(header, "utf8") + Math.max(0, returnedLines - 1);
|
|
1916
|
+
for (const line of selectedLines) outputBytes += width + 1 + Buffer.byteLength(line, "utf8");
|
|
1917
|
+
if (outputBytes > MAX_READ_BYTES) throw new ReadByteLimitError("output");
|
|
1918
|
+
const result = {
|
|
1919
|
+
success: true,
|
|
1920
|
+
output: header + formatWithLineNumbers(selectedLines, startLine)
|
|
1921
|
+
};
|
|
1922
|
+
return JSON.stringify(result);
|
|
1923
|
+
}
|
|
1924
|
+
async function readFileTool(args, options) {
|
|
1925
|
+
if (options.signal?.aborted) throw new ReadCancelledError();
|
|
1926
|
+
const { offset, limit = DEFAULT_LIMIT$1 } = args;
|
|
1927
|
+
const filePath = options.sandboxClient ? args.filePath : resolveHostPath(args.filePath, options.cwd);
|
|
1928
|
+
const startLine = offset !== void 0 && offset > 0 ? offset : 1;
|
|
1929
|
+
if (options.sandboxClient) try {
|
|
1930
|
+
const content = await options.sandboxClient.readFile(filePath);
|
|
1931
|
+
if (options.signal?.aborted) throw new ReadCancelledError();
|
|
1932
|
+
if (Buffer.byteLength(content, "utf8") > MAX_READ_BYTES) throw new ReadByteLimitError("input");
|
|
1933
|
+
return formatReadResult(filePath, content, startLine, limit);
|
|
1934
|
+
} catch (err) {
|
|
1935
|
+
if (err instanceof ReadByteLimitError || err instanceof ReadCancelledError) throw err;
|
|
1936
|
+
const result = {
|
|
1937
|
+
success: false,
|
|
1938
|
+
output: "",
|
|
1939
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1940
|
+
};
|
|
1941
|
+
return JSON.stringify(result);
|
|
1942
|
+
}
|
|
1943
|
+
const pathError = checkPathWithinCwd(filePath, options.cwd);
|
|
1944
|
+
if (pathError !== void 0) return pathError;
|
|
1945
|
+
let fileStats;
|
|
1946
|
+
try {
|
|
1947
|
+
fileStats = await (0, node_fs_promises.stat)(filePath);
|
|
1948
|
+
} catch (err) {
|
|
1949
|
+
const result = {
|
|
1950
|
+
success: false,
|
|
1951
|
+
output: "",
|
|
1952
|
+
error: `File not found: ${filePath}`
|
|
1953
|
+
};
|
|
1954
|
+
return JSON.stringify(result);
|
|
1955
|
+
}
|
|
1956
|
+
if (!fileStats.isFile()) {
|
|
1957
|
+
const result = {
|
|
1958
|
+
success: false,
|
|
1959
|
+
output: "",
|
|
1960
|
+
error: `Path is not a file: ${filePath}`
|
|
1961
|
+
};
|
|
1962
|
+
return JSON.stringify(result);
|
|
1963
|
+
}
|
|
1964
|
+
let buffer = Buffer.alloc(0);
|
|
1965
|
+
let binaryFile = false;
|
|
1966
|
+
try {
|
|
1967
|
+
const handle = await (0, node_fs_promises.open)(filePath, "r");
|
|
1968
|
+
try {
|
|
1969
|
+
const chunks = [];
|
|
1970
|
+
const chunk = Buffer.allocUnsafe(READ_CHUNK_BYTES$2);
|
|
1971
|
+
let bytes = 0;
|
|
1972
|
+
let binaryCheckedBytes = 0;
|
|
1973
|
+
while (bytes <= MAX_READ_BYTES) {
|
|
1974
|
+
if (options.signal?.aborted) throw new ReadCancelledError();
|
|
1975
|
+
const { bytesRead } = await handle.read(chunk, 0, Math.min(chunk.length, 4194305 - bytes), null);
|
|
1976
|
+
if (bytesRead === 0) break;
|
|
1977
|
+
const binaryCheckLength = Math.min(bytesRead, 8192 - binaryCheckedBytes);
|
|
1978
|
+
if (binaryCheckLength > 0 && isBinary(chunk.subarray(0, binaryCheckLength))) {
|
|
1979
|
+
binaryFile = true;
|
|
1980
|
+
break;
|
|
1981
|
+
}
|
|
1982
|
+
binaryCheckedBytes += binaryCheckLength;
|
|
1983
|
+
bytes += bytesRead;
|
|
1984
|
+
if (bytes > MAX_READ_BYTES) throw new ReadByteLimitError("input");
|
|
1985
|
+
chunks.push(Buffer.from(chunk.subarray(0, bytesRead)));
|
|
1986
|
+
}
|
|
1987
|
+
if (!binaryFile) buffer = Buffer.concat(chunks, bytes);
|
|
1988
|
+
} finally {
|
|
1989
|
+
await handle.close();
|
|
1990
|
+
}
|
|
1991
|
+
} catch (err) {
|
|
1992
|
+
if (err instanceof ReadByteLimitError || err instanceof ReadCancelledError) throw err;
|
|
1993
|
+
const result = {
|
|
1994
|
+
success: false,
|
|
1995
|
+
output: "",
|
|
1996
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1997
|
+
};
|
|
1998
|
+
return JSON.stringify(result);
|
|
1999
|
+
}
|
|
2000
|
+
if (options.signal?.aborted) throw new ReadCancelledError();
|
|
2001
|
+
if (binaryFile) {
|
|
2002
|
+
const result = {
|
|
2003
|
+
success: false,
|
|
2004
|
+
output: "",
|
|
2005
|
+
error: `Binary file not supported: ${filePath}`
|
|
2006
|
+
};
|
|
2007
|
+
return JSON.stringify(result);
|
|
2008
|
+
}
|
|
2009
|
+
return formatReadResult(filePath, buffer.toString("utf8"), startLine, limit);
|
|
2010
|
+
}
|
|
2011
|
+
/**
|
|
2012
|
+
* Create a ReadTool instance — register with Robota agent tools registry.
|
|
2013
|
+
*/
|
|
2014
|
+
function createReadTool(options) {
|
|
2015
|
+
return createZodFunctionTool("Read", options.description ?? DEFAULT_READ_DESCRIPTION, ReadSchema, async (params) => {
|
|
2016
|
+
return readFileTool(params, options);
|
|
2017
|
+
});
|
|
2018
|
+
}
|
|
2019
|
+
//#endregion
|
|
2020
|
+
//#region src/builtins/atomic-file-write.ts
|
|
2021
|
+
const TEMP_RANDOM_BYTES = 6;
|
|
2022
|
+
const PRESERVED_MODE_BITS = 4095;
|
|
2023
|
+
const MISSING_FILE_ERROR_CODE = "ENOENT";
|
|
2024
|
+
/**
|
|
2025
|
+
* NEUT-009: this marker used to carry the consumer's product name, so a neutral tool library wrote
|
|
2026
|
+
* that name onto every temporary file it created — inherited by any other product built on it. The
|
|
2027
|
+
* marker now says what the file IS, which is all it was ever for.
|
|
2028
|
+
*
|
|
2029
|
+
* The product name is not quoted here either: the ratchet counts prose, deliberately, because a
|
|
2030
|
+
* library whose comments teach the product's layout is coupled to it just as firmly.
|
|
2031
|
+
*/
|
|
2032
|
+
const TEMP_MARKER = ".atomic-tmp-";
|
|
2033
|
+
function createTempFilePath(filePath) {
|
|
2034
|
+
const dir = (0, node_path.dirname)(filePath);
|
|
2035
|
+
const name = (0, node_path.basename)(filePath);
|
|
2036
|
+
const suffix = (0, node_crypto.randomBytes)(TEMP_RANDOM_BYTES).toString("hex");
|
|
2037
|
+
return (0, node_path.join)(dir, `.${name}${TEMP_MARKER}${process.pid}-${Date.now()}-${suffix}`);
|
|
2038
|
+
}
|
|
2039
|
+
async function readExistingMode(filePath) {
|
|
2040
|
+
try {
|
|
2041
|
+
return (await (0, node_fs_promises.stat)(filePath)).mode & PRESERVED_MODE_BITS;
|
|
2042
|
+
} catch (error) {
|
|
2043
|
+
if (error instanceof Error && hasErrorCode(error, MISSING_FILE_ERROR_CODE)) return void 0;
|
|
2044
|
+
throw error;
|
|
2045
|
+
}
|
|
2046
|
+
}
|
|
2047
|
+
function hasErrorCode(error, code) {
|
|
2048
|
+
return "code" in error && error.code === code;
|
|
2049
|
+
}
|
|
2050
|
+
async function atomicWriteUtf8File(filePath, content) {
|
|
2051
|
+
await (0, node_fs_promises.mkdir)((0, node_path.dirname)(filePath), { recursive: true });
|
|
2052
|
+
const existingMode = await readExistingMode(filePath);
|
|
2053
|
+
const tempFilePath = createTempFilePath(filePath);
|
|
2054
|
+
try {
|
|
2055
|
+
await (0, node_fs_promises.writeFile)(tempFilePath, content, "utf8");
|
|
2056
|
+
if (existingMode !== void 0) await (0, node_fs_promises.chmod)(tempFilePath, existingMode);
|
|
2057
|
+
await (0, node_fs_promises.rename)(tempFilePath, filePath);
|
|
2058
|
+
} catch (error) {
|
|
2059
|
+
await (0, node_fs_promises.rm)(tempFilePath, { force: true }).catch(() => void 0);
|
|
2060
|
+
throw error;
|
|
2061
|
+
}
|
|
2062
|
+
}
|
|
2063
|
+
//#endregion
|
|
2064
|
+
//#region src/builtins/write-tool.ts
|
|
2065
|
+
/**
|
|
2066
|
+
* WriteTool — write content to a file, auto-creating parent directories.
|
|
2067
|
+
*/
|
|
2068
|
+
const DEFAULT_WRITE_DESCRIPTION = "Writes a file to the local filesystem. This will overwrite an existing file if one exists.\n\nPrefer the Edit tool for modifying existing files — it only sends the changed text. Use this tool to create new files or for complete rewrites.\n\nParent directories are created automatically when missing.";
|
|
2069
|
+
const WriteSchema = zod.z.object({
|
|
2070
|
+
filePath: zod.z.string().describe("The absolute path to the file to write"),
|
|
2071
|
+
content: zod.z.string().describe("The content to write to the file")
|
|
1052
2072
|
});
|
|
1053
|
-
async function
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
const result = {
|
|
1100
|
-
success: true,
|
|
1101
|
-
output: `Replaced ${count} occurrence(s) in ${filePath}`
|
|
1102
|
-
};
|
|
1103
|
-
return JSON.stringify(result);
|
|
1104
|
-
}
|
|
1105
|
-
var editTool = createZodFunctionTool(
|
|
1106
|
-
"Edit",
|
|
1107
|
-
"Performs exact string replacements in files.\n\nYou must use the Read tool at least once before editing. When editing text from Read output, preserve the exact indentation.\n\nThe edit will FAIL if old_string is not unique in the file. Either provide more surrounding context to make it unique, or use replace_all to change every instance.\n\nALWAYS prefer editing existing files over creating new ones.",
|
|
1108
|
-
EditSchema,
|
|
1109
|
-
async (params) => {
|
|
1110
|
-
return editFileTool(params);
|
|
1111
|
-
}
|
|
1112
|
-
);
|
|
1113
|
-
|
|
1114
|
-
// src/builtins/glob-tool.ts
|
|
1115
|
-
var import_promises4 = require("fs/promises");
|
|
1116
|
-
var import_node_path2 = require("path");
|
|
1117
|
-
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
1118
|
-
var import_zod5 = require("zod");
|
|
1119
|
-
var DEFAULT_MAX_RESULTS = 1e3;
|
|
1120
|
-
var GlobSchema = import_zod5.z.object({
|
|
1121
|
-
pattern: import_zod5.z.string().describe('The glob pattern to match files against (e.g. "**/*.ts", "src/**/*.tsx")'),
|
|
1122
|
-
path: import_zod5.z.string().optional().describe(
|
|
1123
|
-
"The directory to search in. Defaults to the current working directory. Must be a valid directory path if provided"
|
|
1124
|
-
),
|
|
1125
|
-
limit: import_zod5.z.number().optional().describe(
|
|
1126
|
-
"Maximum number of results to return (default: 1000). Use a smaller limit to save context space"
|
|
1127
|
-
)
|
|
2073
|
+
async function writeFileTool(args, options) {
|
|
2074
|
+
const { content } = args;
|
|
2075
|
+
const filePath = options.sandboxClient ? args.filePath : resolveHostPath(args.filePath, options.cwd);
|
|
2076
|
+
if (!options.sandboxClient) {
|
|
2077
|
+
const pathError = checkPathWithinCwd(filePath, options.cwd);
|
|
2078
|
+
if (pathError !== void 0) return pathError;
|
|
2079
|
+
}
|
|
2080
|
+
try {
|
|
2081
|
+
if (options.sandboxClient) await options.sandboxClient.writeFile(filePath, content);
|
|
2082
|
+
else await atomicWriteUtf8File(filePath, content);
|
|
2083
|
+
const result = {
|
|
2084
|
+
success: true,
|
|
2085
|
+
output: `Written ${Buffer.byteLength(content, "utf8")} bytes to ${filePath}`
|
|
2086
|
+
};
|
|
2087
|
+
return JSON.stringify(result);
|
|
2088
|
+
} catch (err) {
|
|
2089
|
+
const result = {
|
|
2090
|
+
success: false,
|
|
2091
|
+
output: "",
|
|
2092
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2093
|
+
};
|
|
2094
|
+
return JSON.stringify(result);
|
|
2095
|
+
}
|
|
2096
|
+
}
|
|
2097
|
+
/**
|
|
2098
|
+
* Create a WriteTool instance — register with Robota agent tools registry.
|
|
2099
|
+
*/
|
|
2100
|
+
function createWriteTool(options) {
|
|
2101
|
+
return createZodFunctionTool("Write", options.description ?? DEFAULT_WRITE_DESCRIPTION, WriteSchema, async (params) => {
|
|
2102
|
+
return writeFileTool(params, options);
|
|
2103
|
+
});
|
|
2104
|
+
}
|
|
2105
|
+
//#endregion
|
|
2106
|
+
//#region src/builtins/edit-tool.ts
|
|
2107
|
+
/**
|
|
2108
|
+
* EditTool — perform string-replace edits on a file.
|
|
2109
|
+
*
|
|
2110
|
+
* By default, requires the oldString to appear exactly once in the file
|
|
2111
|
+
* (ensuring surgical edits). Pass replaceAll:true to replace all occurrences.
|
|
2112
|
+
*/
|
|
2113
|
+
const DEFAULT_EDIT_DESCRIPTION = "Performs exact string replacements in files.\n\noldString must exactly match the file's current content, including whitespace and indentation — reading the file first (e.g. with a file-read tool) is the reliable way to copy exact text.\n\nThe edit will FAIL if oldString is not unique in the file. Either provide more surrounding context to make it unique, or set replaceAll to change every instance.";
|
|
2114
|
+
const EditSchema = zod.z.object({
|
|
2115
|
+
filePath: zod.z.string().describe("The absolute path to the file to modify"),
|
|
2116
|
+
oldString: zod.z.string().describe("The text to replace (must be an exact match of existing content)"),
|
|
2117
|
+
newString: zod.z.string().describe("The text to replace it with (must be different from oldString)"),
|
|
2118
|
+
replaceAll: zod.z.boolean().optional().describe("Replace all occurrences of oldString (default: false). Useful for renaming variables")
|
|
1128
2119
|
});
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
2120
|
+
const MAX_EDIT_FILE_BYTES = 4 * 1024 * 1024;
|
|
2121
|
+
const READ_CHUNK_BYTES$1 = 64 * 1024;
|
|
2122
|
+
/** Marks a refusal that must not surface a partial or crashed read to the caller. */
|
|
2123
|
+
var EditByteLimitError = class extends Error {
|
|
2124
|
+
boundary;
|
|
2125
|
+
constructor(boundary) {
|
|
2126
|
+
super(`Edit ${boundary} exceeds its ${MAX_EDIT_FILE_BYTES}-byte limit`);
|
|
2127
|
+
this.boundary = boundary;
|
|
2128
|
+
}
|
|
2129
|
+
};
|
|
2130
|
+
/**
|
|
2131
|
+
* Read a file as UTF-8 while rejecting as soon as more than `maxBytes` bytes have arrived —
|
|
2132
|
+
* before the whole content is materialized. Reading actual bytes off the stream (rather than
|
|
2133
|
+
* trusting stat() size) also catches a file that grows after being stat'd, or has no stable
|
|
2134
|
+
* size at all (a named pipe).
|
|
2135
|
+
*/
|
|
2136
|
+
async function readBoundedUtf8File(filePath, maxBytes) {
|
|
2137
|
+
const stream = (0, node_fs.createReadStream)(filePath, { highWaterMark: READ_CHUNK_BYTES$1 });
|
|
2138
|
+
const chunks = [];
|
|
2139
|
+
let bytes = 0;
|
|
2140
|
+
try {
|
|
2141
|
+
for await (const chunk of stream) {
|
|
2142
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
2143
|
+
bytes += buffer.length;
|
|
2144
|
+
if (bytes > maxBytes) throw new EditByteLimitError("input");
|
|
2145
|
+
chunks.push(buffer);
|
|
2146
|
+
}
|
|
2147
|
+
} finally {
|
|
2148
|
+
stream.destroy();
|
|
2149
|
+
}
|
|
2150
|
+
return Buffer.concat(chunks, bytes).toString("utf8");
|
|
2151
|
+
}
|
|
2152
|
+
async function editFileTool(args, options) {
|
|
2153
|
+
const { oldString, newString, replaceAll = false } = args;
|
|
2154
|
+
const filePath = options.sandboxClient ? args.filePath : resolveHostPath(args.filePath, options.cwd);
|
|
2155
|
+
if (!options.sandboxClient) {
|
|
2156
|
+
const pathError = checkPathWithinCwd(filePath, options.cwd);
|
|
2157
|
+
if (pathError !== void 0) return pathError;
|
|
2158
|
+
}
|
|
2159
|
+
let content;
|
|
2160
|
+
try {
|
|
2161
|
+
if (options.sandboxClient) {
|
|
2162
|
+
content = await options.sandboxClient.readFile(filePath);
|
|
2163
|
+
if (Buffer.byteLength(content, "utf8") > MAX_EDIT_FILE_BYTES) throw new EditByteLimitError("input");
|
|
2164
|
+
} else content = await readBoundedUtf8File(filePath, MAX_EDIT_FILE_BYTES);
|
|
2165
|
+
} catch (err) {
|
|
2166
|
+
if (err instanceof EditByteLimitError) {
|
|
2167
|
+
const result = {
|
|
2168
|
+
success: false,
|
|
2169
|
+
output: "",
|
|
2170
|
+
error: `${err.message}: ${filePath}`
|
|
2171
|
+
};
|
|
2172
|
+
return JSON.stringify(result);
|
|
2173
|
+
}
|
|
2174
|
+
const result = {
|
|
2175
|
+
success: false,
|
|
2176
|
+
output: "",
|
|
2177
|
+
error: `File not found: ${filePath}`
|
|
2178
|
+
};
|
|
2179
|
+
return JSON.stringify(result);
|
|
2180
|
+
}
|
|
2181
|
+
if (!content.includes(oldString)) {
|
|
2182
|
+
const result = {
|
|
2183
|
+
success: false,
|
|
2184
|
+
output: "",
|
|
2185
|
+
error: `oldString not found in file: ${filePath}`
|
|
2186
|
+
};
|
|
2187
|
+
return JSON.stringify(result);
|
|
2188
|
+
}
|
|
2189
|
+
let parts = [];
|
|
2190
|
+
if (replaceAll) parts = content.split(oldString);
|
|
2191
|
+
else if (content.indexOf(oldString) !== content.lastIndexOf(oldString)) {
|
|
2192
|
+
const result = {
|
|
2193
|
+
success: false,
|
|
2194
|
+
output: "",
|
|
2195
|
+
error: `oldString is not unique in file (found ${content.split(oldString).length - 1} occurrences). Provide more context to make it unique, or use replaceAll:true.`
|
|
2196
|
+
};
|
|
2197
|
+
return JSON.stringify(result);
|
|
2198
|
+
}
|
|
2199
|
+
const count = replaceAll ? parts.length - 1 : 1;
|
|
2200
|
+
const oldBytes = Buffer.byteLength(oldString, "utf8");
|
|
2201
|
+
const newBytes = Buffer.byteLength(newString, "utf8");
|
|
2202
|
+
if (Buffer.byteLength(content, "utf8") - count * oldBytes + count * newBytes > MAX_EDIT_FILE_BYTES) {
|
|
2203
|
+
const result = {
|
|
2204
|
+
success: false,
|
|
2205
|
+
output: "",
|
|
2206
|
+
error: `Edit output exceeds its ${MAX_EDIT_FILE_BYTES}-byte limit: ${filePath}`
|
|
2207
|
+
};
|
|
2208
|
+
return JSON.stringify(result);
|
|
2209
|
+
}
|
|
2210
|
+
const updated = replaceAll ? parts.join(newString) : content.slice(0, content.indexOf(oldString)) + newString + content.slice(content.indexOf(oldString) + oldString.length);
|
|
2211
|
+
try {
|
|
2212
|
+
if (options.sandboxClient) await options.sandboxClient.writeFile(filePath, updated);
|
|
2213
|
+
else await atomicWriteUtf8File(filePath, updated);
|
|
2214
|
+
} catch (err) {
|
|
2215
|
+
const result = {
|
|
2216
|
+
success: false,
|
|
2217
|
+
output: "",
|
|
2218
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2219
|
+
};
|
|
2220
|
+
return JSON.stringify(result);
|
|
2221
|
+
}
|
|
2222
|
+
const matchIdx = content.indexOf(oldString);
|
|
2223
|
+
const startLine = matchIdx >= 0 ? content.substring(0, matchIdx).split("\n").length : 1;
|
|
2224
|
+
const result = {
|
|
2225
|
+
success: true,
|
|
2226
|
+
output: `Replaced ${count} occurrence(s) in ${filePath}`,
|
|
2227
|
+
startLine
|
|
2228
|
+
};
|
|
2229
|
+
return JSON.stringify(result);
|
|
2230
|
+
}
|
|
2231
|
+
/**
|
|
2232
|
+
* Create an EditTool instance — register with Robota agent tools registry.
|
|
2233
|
+
*/
|
|
2234
|
+
function createEditTool(options) {
|
|
2235
|
+
return createZodFunctionTool("Edit", options.description ?? DEFAULT_EDIT_DESCRIPTION, EditSchema, async (params) => {
|
|
2236
|
+
return editFileTool(params, options);
|
|
2237
|
+
});
|
|
2238
|
+
}
|
|
2239
|
+
//#endregion
|
|
2240
|
+
//#region src/builtins/glob-tool.ts
|
|
2241
|
+
/**
|
|
2242
|
+
* GlobTool — fast file pattern search using fast-glob.
|
|
2243
|
+
*
|
|
2244
|
+
* Excludes node_modules and .git by default.
|
|
2245
|
+
* Results are sorted by modification time (most recently modified first) among the candidates
|
|
2246
|
+
* enumerated before any candidate ceiling was hit (see DEFAULT_MAX_GLOB_CANDIDATES) — ordering is
|
|
2247
|
+
* not guaranteed across the full match set when the search tree is larger than that ceiling.
|
|
2248
|
+
*
|
|
2249
|
+
* SEC-007: when a containment root is configured the enumeration is confined to it. Listing the
|
|
2250
|
+
* filesystem is a disclosure in its own right — a sandbox that stops the model reading a file but
|
|
2251
|
+
* lets it map everything around that file is not a sandbox.
|
|
2252
|
+
*/
|
|
2253
|
+
const DEFAULT_MAX_RESULTS = 1e3;
|
|
2254
|
+
/**
|
|
2255
|
+
* Ceiling on how many raw glob matches are pulled off `fast-glob`'s match STREAM before enumeration
|
|
2256
|
+
* stops, independent of `limit`/`DEFAULT_MAX_RESULTS`.
|
|
2257
|
+
*
|
|
2258
|
+
* `fg(pattern)` (the promise form) materializes every match into memory and only then stats and
|
|
2259
|
+
* slices to `limit` — a pattern like `**\/*` under a huge tree allocates and stats the whole match
|
|
2260
|
+
* set no matter how small `limit` is. Streaming lets the walk stop as soon as this many CANDIDATES
|
|
2261
|
+
* have been seen, so memory and stat fan-out scale with this ceiling, not with the tree.
|
|
2262
|
+
*/
|
|
2263
|
+
const DEFAULT_MAX_GLOB_CANDIDATES = 5e4;
|
|
2264
|
+
const GlobSchema = zod.z.object({
|
|
2265
|
+
pattern: zod.z.string().describe("The glob pattern to match files against (e.g. \"**/*.ts\", \"src/**/*.tsx\")"),
|
|
2266
|
+
path: zod.z.string().optional().describe("The directory to search in. Defaults to the current working directory. Must be a valid directory path if provided"),
|
|
2267
|
+
limit: zod.z.number().optional().describe("Maximum number of results to return (default: 1000). Use a smaller limit to save context space")
|
|
1202
2268
|
});
|
|
2269
|
+
/** Cap on concurrent `stat` calls during the mtime sort, so a large match set cannot storm the FS. */
|
|
2270
|
+
const STAT_CONCURRENCY_LIMIT = 100;
|
|
2271
|
+
/**
|
|
2272
|
+
* Drop every match whose CANONICAL path escapes the containment root, then stat the survivors for the
|
|
2273
|
+
* mtime sort, newest first.
|
|
2274
|
+
*
|
|
2275
|
+
* Containment is decided per RESULT as well as per root (SEC-007): a `..` in the pattern, or an
|
|
2276
|
+
* absolute pattern, produces a match the search root never vouched for. Decided canonically through
|
|
2277
|
+
* the shared guard — a symlink named `escape` is a plain segment, so no amount of segment validation
|
|
2278
|
+
* would catch it.
|
|
2279
|
+
*/
|
|
2280
|
+
async function containedMatchesByMtime(matches, cwd, containmentRoot) {
|
|
2281
|
+
const limit = (0, p_limit.default)(STAT_CONCURRENCY_LIMIT);
|
|
2282
|
+
return (await Promise.all(matches.map((p) => limit(async () => {
|
|
2283
|
+
const absPath = (0, node_path.resolve)(cwd, p);
|
|
2284
|
+
if (!isWithinCwd(absPath, containmentRoot)) return void 0;
|
|
2285
|
+
try {
|
|
2286
|
+
return {
|
|
2287
|
+
path: p,
|
|
2288
|
+
mtime: (await (0, node_fs_promises.stat)(absPath)).mtimeMs
|
|
2289
|
+
};
|
|
2290
|
+
} catch {
|
|
2291
|
+
return {
|
|
2292
|
+
path: p,
|
|
2293
|
+
mtime: 0
|
|
2294
|
+
};
|
|
2295
|
+
}
|
|
2296
|
+
})))).filter((entry) => entry !== void 0).sort((a, b) => b.mtime - a.mtime);
|
|
2297
|
+
}
|
|
2298
|
+
/**
|
|
2299
|
+
* Pull matches off `fast-glob`'s streaming API one at a time, stopping at `maxCandidates` instead of
|
|
2300
|
+
* materializing the whole match set (see {@link DEFAULT_MAX_GLOB_CANDIDATES}). Exported for tests that
|
|
2301
|
+
* need a smaller ceiling than the real default.
|
|
2302
|
+
*/
|
|
2303
|
+
async function collectGlobMatches(pattern, options, maxCandidates) {
|
|
2304
|
+
const matches = [];
|
|
2305
|
+
let truncated = false;
|
|
2306
|
+
const stream = fast_glob.default.stream(pattern, options);
|
|
2307
|
+
for await (const entry of stream) {
|
|
2308
|
+
if (matches.length >= maxCandidates) {
|
|
2309
|
+
truncated = true;
|
|
2310
|
+
break;
|
|
2311
|
+
}
|
|
2312
|
+
matches.push(entry);
|
|
2313
|
+
}
|
|
2314
|
+
return {
|
|
2315
|
+
matches,
|
|
2316
|
+
truncated
|
|
2317
|
+
};
|
|
2318
|
+
}
|
|
2319
|
+
/**
|
|
2320
|
+
* Exported (rather than module-private) so tests can drive it with a `maxCandidates` far smaller than
|
|
2321
|
+
* {@link DEFAULT_MAX_GLOB_CANDIDATES} — the real default is too large to exercise cheaply — without
|
|
2322
|
+
* adding any test-only knob to the public `createGlobTool` factory or its schema.
|
|
2323
|
+
*/
|
|
2324
|
+
async function globFileTool(args, options, maxCandidates = DEFAULT_MAX_GLOB_CANDIDATES) {
|
|
2325
|
+
const { pattern, path: basePath } = args;
|
|
2326
|
+
const containmentRoot = options.cwd;
|
|
2327
|
+
const { root: cwd, error: rootError } = resolveSearchRoot(basePath, containmentRoot);
|
|
2328
|
+
if (rootError) return rootError;
|
|
2329
|
+
let candidates;
|
|
2330
|
+
try {
|
|
2331
|
+
candidates = await collectGlobMatches(pattern, {
|
|
2332
|
+
cwd,
|
|
2333
|
+
ignore: ["**/node_modules/**", "**/.git/**"],
|
|
2334
|
+
dot: true,
|
|
2335
|
+
absolute: false,
|
|
2336
|
+
followSymbolicLinks: false
|
|
2337
|
+
}, maxCandidates);
|
|
2338
|
+
} catch (err) {
|
|
2339
|
+
const result = {
|
|
2340
|
+
success: false,
|
|
2341
|
+
output: "",
|
|
2342
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2343
|
+
};
|
|
2344
|
+
return JSON.stringify(result);
|
|
2345
|
+
}
|
|
2346
|
+
const { matches, truncated: candidatesTruncated } = candidates;
|
|
2347
|
+
const withMtime = await containedMatchesByMtime(matches, cwd, containmentRoot);
|
|
2348
|
+
const maxResults = args.limit ?? DEFAULT_MAX_RESULTS;
|
|
2349
|
+
const totalMatches = withMtime.length;
|
|
2350
|
+
const truncated = totalMatches > maxResults;
|
|
2351
|
+
const sorted = (truncated ? withMtime.slice(0, maxResults) : withMtime).map((f) => f.path);
|
|
2352
|
+
let output = sorted.length > 0 ? sorted.join("\n") : "(no matches)";
|
|
2353
|
+
if (truncated) output += `\n\n[Showing ${maxResults} of ${totalMatches} matches. Use limit parameter to see more.]`;
|
|
2354
|
+
if (candidatesTruncated) output += `\n\n[Candidate search stopped early; the search tree has more matches than this tool scans in one call. Results are ordered among the scanned candidates only — narrow the pattern or path to see the rest.]`;
|
|
2355
|
+
return JSON.stringify({
|
|
2356
|
+
success: true,
|
|
2357
|
+
output
|
|
2358
|
+
});
|
|
2359
|
+
}
|
|
2360
|
+
const DEFAULT_GLOB_DESCRIPTION = "Fast file pattern matching tool that works with any codebase size.\n\nSupports glob patterns like '**/*.js' or 'src/**/*.ts'. Returns matching file paths sorted by modification time.\n\nUse this tool when you need to find files by name patterns.\n\nDefault limit is 1000 results. Use the limit parameter if you need fewer results to save context space.";
|
|
2361
|
+
/**
|
|
2362
|
+
* Create a GlobTool instance — register with Robota agent tools registry.
|
|
2363
|
+
*/
|
|
2364
|
+
function createGlobTool(options) {
|
|
2365
|
+
return createZodFunctionTool("Glob", options.description ?? DEFAULT_GLOB_DESCRIPTION, GlobSchema, async (params) => {
|
|
2366
|
+
return globFileTool(params, options);
|
|
2367
|
+
});
|
|
2368
|
+
}
|
|
2369
|
+
//#endregion
|
|
2370
|
+
//#region src/builtins/grep-search.ts
|
|
2371
|
+
/**
|
|
2372
|
+
* The `Grep` tool's search internals — file enumeration and per-file matching.
|
|
2373
|
+
*
|
|
2374
|
+
* Split out of `grep-tool.ts` (SEC-007) when adding containment pushed that file past the
|
|
2375
|
+
* anti-monolith limit. The split is by responsibility, not by line count: this module is HOW the
|
|
2376
|
+
* search is performed, while `grep-tool.ts` is the tool SURFACE — schema, model-facing description,
|
|
2377
|
+
* factory, and the result envelope. Neither half needs to know the other's concerns.
|
|
2378
|
+
*/
|
|
2379
|
+
/** Convert a simple glob to a RegExp for file name filtering. */
|
|
1203
2380
|
function globToRegex(glob) {
|
|
1204
|
-
|
|
1205
|
-
|
|
2381
|
+
const escaped = glob.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, ".+").replace(/\*/g, "[^/]*");
|
|
2382
|
+
return new RegExp(`^${escaped}$`);
|
|
1206
2383
|
}
|
|
2384
|
+
/** Check if a file name matches an optional glob filter. */
|
|
1207
2385
|
function matchesGlob(filename, glob) {
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
}
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
2386
|
+
if (glob === void 0) return true;
|
|
2387
|
+
return globToRegex(glob).test(filename);
|
|
2388
|
+
}
|
|
2389
|
+
/**
|
|
2390
|
+
* Ceiling on how many directory entries `collectFiles` will `stat` before it stops walking.
|
|
2391
|
+
*
|
|
2392
|
+
* Without a cap, enumeration and stat fan-out scale with the whole tree under the search root,
|
|
2393
|
+
* not with any result limit — a directory with millions of files makes every `Grep` call walk and
|
|
2394
|
+
* stat millions of entries before `headLimit` ever gets a chance to truncate the OUTPUT. This bounds
|
|
2395
|
+
* the WALK itself.
|
|
2396
|
+
*/
|
|
2397
|
+
const DEFAULT_MAX_COLLECTED_FILES = 5e4;
|
|
2398
|
+
/**
|
|
2399
|
+
* Gather files under a directory recursively, excluding node_modules/.git, stopping once `maxFiles`
|
|
2400
|
+
* entries have been visited.
|
|
2401
|
+
*
|
|
2402
|
+
* `containmentRoot` (SEC-007) drops any entry whose CANONICAL path escapes the root, before it is
|
|
2403
|
+
* descended into or read. `stat` follows symlinks, so without this a link inside the root pointing
|
|
2404
|
+
* out of it made the whole target tree readable — including, for a symlinked FILE, its contents.
|
|
2405
|
+
*/
|
|
2406
|
+
async function collectFiles(dirPath, glob, containmentRoot, maxFiles = DEFAULT_MAX_COLLECTED_FILES) {
|
|
2407
|
+
const results = [];
|
|
2408
|
+
let visited = 0;
|
|
2409
|
+
let truncated = false;
|
|
2410
|
+
async function walk(current) {
|
|
2411
|
+
if (truncated) return;
|
|
2412
|
+
let entryNames;
|
|
2413
|
+
try {
|
|
2414
|
+
entryNames = await (0, node_fs_promises.readdir)(current);
|
|
2415
|
+
} catch {
|
|
2416
|
+
return;
|
|
2417
|
+
}
|
|
2418
|
+
for (const name of entryNames) {
|
|
2419
|
+
if (truncated) return;
|
|
2420
|
+
if (name === "node_modules" || name === ".git") continue;
|
|
2421
|
+
const fullPath = (0, node_path.join)(current, name);
|
|
2422
|
+
if (!isWithinCwd(fullPath, containmentRoot)) continue;
|
|
2423
|
+
if (visited >= maxFiles) {
|
|
2424
|
+
truncated = true;
|
|
2425
|
+
return;
|
|
2426
|
+
}
|
|
2427
|
+
visited++;
|
|
2428
|
+
let fileStat;
|
|
2429
|
+
try {
|
|
2430
|
+
fileStat = await (0, node_fs_promises.stat)(fullPath);
|
|
2431
|
+
} catch {
|
|
2432
|
+
continue;
|
|
2433
|
+
}
|
|
2434
|
+
if (fileStat.isDirectory()) await walk(fullPath);
|
|
2435
|
+
else if (fileStat.isFile()) {
|
|
2436
|
+
if (matchesGlob(name, glob)) results.push(fullPath);
|
|
2437
|
+
}
|
|
2438
|
+
}
|
|
2439
|
+
}
|
|
2440
|
+
await walk(dirPath);
|
|
2441
|
+
return {
|
|
2442
|
+
files: results,
|
|
2443
|
+
truncated
|
|
2444
|
+
};
|
|
2445
|
+
}
|
|
2446
|
+
/** Search a single file for lines matching the regex. */
|
|
2447
|
+
function searchFile(content, filePath, regex, contextLines, outputMode, maxOutputBytes) {
|
|
2448
|
+
const lines = content.split("\n");
|
|
2449
|
+
const matchingIndices = [];
|
|
2450
|
+
for (let i = 0; i < lines.length; i++) if (regex.test(lines[i])) matchingIndices.push(i);
|
|
2451
|
+
if (matchingIndices.length === 0) return [];
|
|
2452
|
+
if (outputMode === "files_with_matches") return [filePath];
|
|
2453
|
+
if (outputMode === "count") return [`${filePath}:${matchingIndices.length}`];
|
|
2454
|
+
const includedIndices = /* @__PURE__ */ new Set();
|
|
2455
|
+
for (const idx of matchingIndices) for (let c = Math.max(0, idx - contextLines); c <= Math.min(lines.length - 1, idx + contextLines); c++) includedIndices.add(c);
|
|
2456
|
+
const outputLines = [];
|
|
2457
|
+
let outputBytes = 0;
|
|
2458
|
+
const sortedIndices = Array.from(includedIndices).sort((a, b) => a - b);
|
|
2459
|
+
let prevIdx;
|
|
2460
|
+
let matchingCursor = 0;
|
|
2461
|
+
for (const idx of sortedIndices) {
|
|
2462
|
+
if (prevIdx !== void 0 && idx > prevIdx + 1) outputLines.push("--");
|
|
2463
|
+
const lineNum = idx + 1;
|
|
2464
|
+
while (matchingIndices[matchingCursor] < idx) matchingCursor++;
|
|
2465
|
+
const row = `${filePath}:${lineNum}${matchingIndices[matchingCursor] === idx ? ":" : "-"}${lines[idx]}`;
|
|
2466
|
+
outputBytes += Buffer.byteLength(row, "utf8") + 1;
|
|
2467
|
+
if (maxOutputBytes !== void 0 && outputBytes > maxOutputBytes) throw new Error("byte limit");
|
|
2468
|
+
outputLines.push(row);
|
|
2469
|
+
prevIdx = idx;
|
|
2470
|
+
}
|
|
2471
|
+
return outputLines;
|
|
2472
|
+
}
|
|
2473
|
+
//#endregion
|
|
2474
|
+
//#region src/builtins/isolated-grep-search.ts
|
|
2475
|
+
const BOOTSTRAP = `
|
|
2476
|
+
const { parentPort } = require('node:worker_threads');
|
|
2477
|
+
const searchFile = ${searchFile.toString()};
|
|
2478
|
+
let outputBytes = 0;
|
|
2479
|
+
parentPort.on('message', (request) => {
|
|
1283
2480
|
try {
|
|
1284
|
-
regex = new RegExp(pattern);
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
2481
|
+
const regex = new RegExp(request.pattern);
|
|
2482
|
+
const matches = searchFile(request.content, request.filePath, regex, request.contextLines, request.outputMode, 4 * 1024 * 1024);
|
|
2483
|
+
let bytes = 0;
|
|
2484
|
+
for (const match of matches) { bytes += Buffer.byteLength(match, 'utf8') + 1; if (outputBytes + bytes > 4 * 1024 * 1024) throw new Error('byte limit'); }
|
|
2485
|
+
outputBytes += bytes;
|
|
2486
|
+
parentPort.postMessage({ id: request.id, matches });
|
|
2487
|
+
} catch (error) { parentPort.postMessage({ id: request.id, error: error?.message === 'byte limit' ? 'Grep search exceeded its byte limit' : 'Invalid grep regex execution' }); }
|
|
2488
|
+
});
|
|
2489
|
+
`;
|
|
2490
|
+
var GrepProcessWorker = class extends node_events.EventEmitter {
|
|
2491
|
+
child = (0, node_child_process.spawn)(process.execPath, ["-e", `
|
|
2492
|
+
const searchFile = ${searchFile.toString()};
|
|
2493
|
+
const readline = require('node:readline');
|
|
2494
|
+
let outputBytes = 0;
|
|
2495
|
+
readline.createInterface({ input: process.stdin }).on('line', line => {
|
|
2496
|
+
const request = JSON.parse(line);
|
|
1294
2497
|
try {
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
return JSON.stringify(result2);
|
|
1303
|
-
}
|
|
1304
|
-
let files;
|
|
1305
|
-
if (targetStat.isFile()) {
|
|
1306
|
-
files = [targetPath];
|
|
1307
|
-
} else {
|
|
1308
|
-
files = await collectFiles(targetPath, glob);
|
|
1309
|
-
}
|
|
1310
|
-
const allOutputLines = [];
|
|
1311
|
-
for (const filePath of files) {
|
|
1312
|
-
let content;
|
|
1313
|
-
try {
|
|
1314
|
-
const buffer = await (0, import_promises5.readFile)(filePath);
|
|
1315
|
-
const checkLen = Math.min(buffer.length, 8192);
|
|
1316
|
-
let hasBinary = false;
|
|
1317
|
-
for (let i = 0; i < checkLen; i++) {
|
|
1318
|
-
if (buffer[i] === 0) {
|
|
1319
|
-
hasBinary = true;
|
|
1320
|
-
break;
|
|
1321
|
-
}
|
|
1322
|
-
}
|
|
1323
|
-
if (hasBinary) continue;
|
|
1324
|
-
content = buffer.toString("utf8");
|
|
1325
|
-
} catch {
|
|
1326
|
-
continue;
|
|
1327
|
-
}
|
|
1328
|
-
const fileMatches = searchFile(content, filePath, regex, contextLines, outputMode);
|
|
1329
|
-
allOutputLines.push(...fileMatches);
|
|
1330
|
-
}
|
|
1331
|
-
const result = {
|
|
1332
|
-
success: true,
|
|
1333
|
-
output: allOutputLines.length > 0 ? allOutputLines.join("\n") : "(no matches)"
|
|
1334
|
-
};
|
|
1335
|
-
return JSON.stringify(result);
|
|
1336
|
-
}
|
|
1337
|
-
var grepTool = createZodFunctionTool(
|
|
1338
|
-
"Grep",
|
|
1339
|
-
"A powerful search tool built on regex matching.\n\nSupports full regex syntax (e.g., 'log.*Error', 'function\\\\s+\\\\w+'). Filter files with glob parameter (e.g., '*.js', '**/*.tsx').\n\nOutput modes: 'content' shows matching lines with context, 'files_with_matches' shows only file paths (default), 'count' shows match counts.\n\nUse this tool for ALL search tasks. NEVER invoke grep or rg as a Bash command.\n\nUse head_limit to control result size and save context space.",
|
|
1340
|
-
GrepSchema,
|
|
1341
|
-
async (params) => {
|
|
1342
|
-
return grepFileTool(params);
|
|
1343
|
-
}
|
|
1344
|
-
);
|
|
1345
|
-
|
|
1346
|
-
// src/builtins/web-fetch-tool.ts
|
|
1347
|
-
var import_zod7 = require("zod");
|
|
1348
|
-
var DEFAULT_TIMEOUT_MS2 = 3e4;
|
|
1349
|
-
var MAX_RESPONSE_BYTES = 5e6;
|
|
1350
|
-
var WebFetchSchema = import_zod7.z.object({
|
|
1351
|
-
url: import_zod7.z.string().describe("The URL to fetch"),
|
|
1352
|
-
headers: import_zod7.z.record(import_zod7.z.string()).optional().describe("Optional HTTP headers as key-value pairs")
|
|
2498
|
+
const regex = new RegExp(request.pattern);
|
|
2499
|
+
const matches = searchFile(request.content, request.filePath, regex, request.contextLines, request.outputMode, 4 * 1024 * 1024);
|
|
2500
|
+
let bytes = 0;
|
|
2501
|
+
for (const match of matches) { bytes += Buffer.byteLength(match, 'utf8') + 1; if (outputBytes + bytes > 4 * 1024 * 1024) throw new Error('byte limit'); }
|
|
2502
|
+
outputBytes += bytes;
|
|
2503
|
+
process.stdout.write(JSON.stringify({ id: request.id, matches }) + String.fromCharCode(10));
|
|
2504
|
+
} catch (error) { process.stdout.write(JSON.stringify({ id: request.id, error: error?.message === 'byte limit' ? 'Grep search exceeded its byte limit' : 'Invalid grep regex execution' }) + String.fromCharCode(10)); }
|
|
1353
2505
|
});
|
|
2506
|
+
`], {
|
|
2507
|
+
env: {
|
|
2508
|
+
BUN_BE_BUN: "1",
|
|
2509
|
+
...process.env.SystemRoot ? { SystemRoot: process.env.SystemRoot } : {}
|
|
2510
|
+
},
|
|
2511
|
+
cwd: (0, node_os.tmpdir)(),
|
|
2512
|
+
stdio: "pipe"
|
|
2513
|
+
});
|
|
2514
|
+
closed;
|
|
2515
|
+
constructor() {
|
|
2516
|
+
super();
|
|
2517
|
+
let pending = "";
|
|
2518
|
+
this.child.stdout.setEncoding("utf8");
|
|
2519
|
+
this.child.stdout.on("data", (chunk) => {
|
|
2520
|
+
pending += chunk;
|
|
2521
|
+
if (Buffer.byteLength(pending, "utf8") > 25166848) {
|
|
2522
|
+
this.emit("error");
|
|
2523
|
+
return;
|
|
2524
|
+
}
|
|
2525
|
+
let newline;
|
|
2526
|
+
while ((newline = pending.indexOf("\n")) >= 0) {
|
|
2527
|
+
const line = pending.slice(0, newline);
|
|
2528
|
+
pending = pending.slice(newline + 1);
|
|
2529
|
+
try {
|
|
2530
|
+
this.emit("message", JSON.parse(line));
|
|
2531
|
+
} catch {
|
|
2532
|
+
this.emit("error");
|
|
2533
|
+
}
|
|
2534
|
+
}
|
|
2535
|
+
});
|
|
2536
|
+
this.child.stderr.resume();
|
|
2537
|
+
this.child.on("error", () => this.emit("error"));
|
|
2538
|
+
this.child.stdin.on("error", () => this.emit("error"));
|
|
2539
|
+
this.closed = new Promise((resolve) => {
|
|
2540
|
+
this.child.once("close", () => {
|
|
2541
|
+
this.emit("exit");
|
|
2542
|
+
resolve();
|
|
2543
|
+
});
|
|
2544
|
+
});
|
|
2545
|
+
}
|
|
2546
|
+
postMessage(request) {
|
|
2547
|
+
this.child.stdin.write(JSON.stringify(request) + "\n");
|
|
2548
|
+
}
|
|
2549
|
+
async terminate() {
|
|
2550
|
+
if (this.child.exitCode === null && this.child.signalCode === null) this.child.kill("SIGKILL");
|
|
2551
|
+
await this.closed;
|
|
2552
|
+
}
|
|
2553
|
+
};
|
|
2554
|
+
/** One worker per grep invocation; a deadline or abort terminates it before the failure is exposed. */
|
|
2555
|
+
var IsolatedGrepSearch = class {
|
|
2556
|
+
pattern;
|
|
2557
|
+
signal;
|
|
2558
|
+
worker = process.versions.bun ? new GrepProcessWorker() : new node_worker_threads.Worker(BOOTSTRAP, {
|
|
2559
|
+
eval: true,
|
|
2560
|
+
execArgv: [],
|
|
2561
|
+
resourceLimits: {
|
|
2562
|
+
maxOldGenerationSizeMb: 128,
|
|
2563
|
+
maxYoungGenerationSizeMb: 32
|
|
2564
|
+
}
|
|
2565
|
+
});
|
|
2566
|
+
pending = /* @__PURE__ */ new Map();
|
|
2567
|
+
nextId = 0;
|
|
2568
|
+
stopped = false;
|
|
2569
|
+
termination;
|
|
2570
|
+
timer;
|
|
2571
|
+
abort = () => {
|
|
2572
|
+
this.stop(/* @__PURE__ */ new Error("Grep search cancelled"));
|
|
2573
|
+
};
|
|
2574
|
+
constructor(pattern, signal) {
|
|
2575
|
+
this.pattern = pattern;
|
|
2576
|
+
this.signal = signal;
|
|
2577
|
+
this.worker.on("message", (message) => {
|
|
2578
|
+
const pending = this.pending.get(message.id);
|
|
2579
|
+
if (!pending || this.stopped) return;
|
|
2580
|
+
this.pending.delete(message.id);
|
|
2581
|
+
if (Array.isArray(message.matches) && message.matches.every((m) => typeof m === "string")) pending.resolve(message.matches);
|
|
2582
|
+
else pending.reject(new Error(message.error ?? "Invalid grep worker response"));
|
|
2583
|
+
});
|
|
2584
|
+
this.worker.on("error", () => {
|
|
2585
|
+
this.stop(/* @__PURE__ */ new Error("Grep search worker failed"));
|
|
2586
|
+
});
|
|
2587
|
+
this.worker.once("exit", () => {
|
|
2588
|
+
this.stop(/* @__PURE__ */ new Error("Grep search worker exited"));
|
|
2589
|
+
});
|
|
2590
|
+
this.timer = setTimeout(() => {
|
|
2591
|
+
this.stop(/* @__PURE__ */ new Error("Grep search timed out"));
|
|
2592
|
+
}, 2e3);
|
|
2593
|
+
signal?.addEventListener("abort", this.abort, { once: true });
|
|
2594
|
+
if (signal?.aborted) this.abort();
|
|
2595
|
+
}
|
|
2596
|
+
search(content, filePath, contextLines, outputMode) {
|
|
2597
|
+
if (this.stopped) return Promise.reject(/* @__PURE__ */ new Error(this.signal?.aborted ? "Grep search cancelled" : "Grep search timed out"));
|
|
2598
|
+
const id = this.nextId++;
|
|
2599
|
+
return new Promise((resolve, reject) => {
|
|
2600
|
+
this.pending.set(id, {
|
|
2601
|
+
resolve,
|
|
2602
|
+
reject
|
|
2603
|
+
});
|
|
2604
|
+
try {
|
|
2605
|
+
this.worker.postMessage({
|
|
2606
|
+
id,
|
|
2607
|
+
content,
|
|
2608
|
+
filePath,
|
|
2609
|
+
pattern: this.pattern,
|
|
2610
|
+
contextLines,
|
|
2611
|
+
outputMode
|
|
2612
|
+
});
|
|
2613
|
+
} catch {
|
|
2614
|
+
this.stop(/* @__PURE__ */ new Error("Grep search worker failed"));
|
|
2615
|
+
}
|
|
2616
|
+
});
|
|
2617
|
+
}
|
|
2618
|
+
async stop(error) {
|
|
2619
|
+
if (this.termination) return this.termination;
|
|
2620
|
+
this.stopped = true;
|
|
2621
|
+
clearTimeout(this.timer);
|
|
2622
|
+
this.signal?.removeEventListener("abort", this.abort);
|
|
2623
|
+
this.termination = (async () => {
|
|
2624
|
+
try {
|
|
2625
|
+
await this.worker.terminate();
|
|
2626
|
+
} catch {}
|
|
2627
|
+
for (const pending of this.pending.values()) pending.reject(error ?? /* @__PURE__ */ new Error("Grep search stopped"));
|
|
2628
|
+
this.pending.clear();
|
|
2629
|
+
})();
|
|
2630
|
+
return this.termination;
|
|
2631
|
+
}
|
|
2632
|
+
};
|
|
2633
|
+
//#endregion
|
|
2634
|
+
//#region src/builtins/grep-tool.ts
|
|
2635
|
+
/**
|
|
2636
|
+
* GrepTool — recursive regex content search.
|
|
2637
|
+
*
|
|
2638
|
+
* Supports three output modes:
|
|
2639
|
+
* - files_with_matches (default): return only file paths that contain a match
|
|
2640
|
+
* - content: return matching lines with optional context lines
|
|
2641
|
+
* - count: return per-file match counts as "path:count" rows
|
|
2642
|
+
*
|
|
2643
|
+
* headLimit caps the number of result lines; excess is truncated with a marker.
|
|
2644
|
+
*
|
|
2645
|
+
* SEC-007: when a containment root is configured the search is confined to it. Grep is the most
|
|
2646
|
+
* disclosing of the file tools — `content` mode returns the matching LINES — so it must be contained
|
|
2647
|
+
* at least as strictly as `Read`, which it could otherwise stand in for.
|
|
2648
|
+
*/
|
|
2649
|
+
const GrepSchema = zod.z.object({
|
|
2650
|
+
pattern: zod.z.string().describe("The regular expression pattern to search for in file contents"),
|
|
2651
|
+
path: zod.z.string().optional().describe("File or directory to search in. Defaults to the current working directory"),
|
|
2652
|
+
glob: zod.z.string().optional().describe("Glob pattern to filter files (e.g. \"*.ts\", \"*.{ts,tsx}\"). Only files matching this pattern will be searched"),
|
|
2653
|
+
contextLines: zod.z.number().optional().describe("Number of context lines to show before and after each match. Only applies when outputMode is \"content\". Default: 0"),
|
|
2654
|
+
outputMode: zod.z.enum([
|
|
2655
|
+
"files_with_matches",
|
|
2656
|
+
"content",
|
|
2657
|
+
"count"
|
|
2658
|
+
]).optional().describe("Output mode: \"files_with_matches\" shows only file paths (default), \"content\" shows matching lines with context, \"count\" shows per-file match counts"),
|
|
2659
|
+
headLimit: zod.z.number().int().positive().optional().describe("Maximum number of result lines (file paths, content lines, or count rows) to return. Excess results are truncated with a marker line")
|
|
2660
|
+
});
|
|
2661
|
+
/** The matcher consumes one file at a time; keep only a few reads outstanding. */
|
|
2662
|
+
const READ_CONCURRENCY_LIMIT = 8;
|
|
2663
|
+
const MAX_GREP_FILE_BYTES = 4 * 1024 * 1024;
|
|
2664
|
+
const READ_CHUNK_BYTES = 64 * 1024;
|
|
2665
|
+
/** A grep isolation failure is a hard tool failure, distinct from ordinary no-match/invalid-input results. */
|
|
2666
|
+
var GrepIsolationError = class extends _robota_sdk_agent_core.ToolExecutionError {
|
|
2667
|
+
reason;
|
|
2668
|
+
constructor(reason) {
|
|
2669
|
+
super(`Grep search ${reason === "timeout" ? "timed out" : reason === "cancelled" ? "cancelled" : reason === "limit" ? "exceeded its byte limit" : "worker failed"}`, "Grep");
|
|
2670
|
+
this.reason = reason;
|
|
2671
|
+
}
|
|
2672
|
+
};
|
|
2673
|
+
async function grepFileTool(args, options) {
|
|
2674
|
+
const { pattern, path: searchPath, glob, contextLines = 0, outputMode = "files_with_matches", headLimit } = args;
|
|
2675
|
+
const containmentRoot = options.cwd;
|
|
2676
|
+
const { root: targetPath, error: rootError } = resolveSearchRoot(searchPath, containmentRoot);
|
|
2677
|
+
if (rootError) return rootError;
|
|
2678
|
+
try {
|
|
2679
|
+
new RegExp(pattern);
|
|
2680
|
+
} catch (err) {
|
|
2681
|
+
const result = {
|
|
2682
|
+
success: false,
|
|
2683
|
+
output: "",
|
|
2684
|
+
error: `Invalid regex pattern: ${pattern}`
|
|
2685
|
+
};
|
|
2686
|
+
return JSON.stringify(result);
|
|
2687
|
+
}
|
|
2688
|
+
let targetStat;
|
|
2689
|
+
try {
|
|
2690
|
+
targetStat = await (0, node_fs_promises.stat)(targetPath);
|
|
2691
|
+
} catch {
|
|
2692
|
+
const result = {
|
|
2693
|
+
success: false,
|
|
2694
|
+
output: "",
|
|
2695
|
+
error: `Path not found: ${targetPath}`
|
|
2696
|
+
};
|
|
2697
|
+
return JSON.stringify(result);
|
|
2698
|
+
}
|
|
2699
|
+
let files;
|
|
2700
|
+
let filesTruncated = false;
|
|
2701
|
+
if (targetStat.isFile()) files = [targetPath];
|
|
2702
|
+
else {
|
|
2703
|
+
const collected = await collectFiles(targetPath, glob, containmentRoot);
|
|
2704
|
+
files = collected.files;
|
|
2705
|
+
filesTruncated = collected.truncated;
|
|
2706
|
+
}
|
|
2707
|
+
const search = new IsolatedGrepSearch(pattern, options.signal);
|
|
2708
|
+
const readAbort = new AbortController();
|
|
2709
|
+
const abortReads = () => readAbort.abort();
|
|
2710
|
+
options.signal?.addEventListener("abort", abortReads, { once: true });
|
|
2711
|
+
if (options.signal?.aborted) abortReads();
|
|
2712
|
+
let perFileMatches;
|
|
2713
|
+
try {
|
|
2714
|
+
if (readAbort.signal.aborted) throw new GrepIsolationError("cancelled");
|
|
2715
|
+
const orderedMatches = new Array(files.length);
|
|
2716
|
+
let nextFile = 0;
|
|
2717
|
+
let failure;
|
|
2718
|
+
const readAndSearch = async (filePath) => {
|
|
2719
|
+
let content;
|
|
2720
|
+
try {
|
|
2721
|
+
if ((await (0, node_fs_promises.stat)(filePath)).size > MAX_GREP_FILE_BYTES) throw new GrepIsolationError("limit");
|
|
2722
|
+
const stream = (0, node_fs.createReadStream)(filePath, {
|
|
2723
|
+
highWaterMark: READ_CHUNK_BYTES,
|
|
2724
|
+
signal: readAbort.signal
|
|
2725
|
+
});
|
|
2726
|
+
const chunks = [];
|
|
2727
|
+
let bytes = 0;
|
|
2728
|
+
try {
|
|
2729
|
+
for await (const chunk of stream) {
|
|
2730
|
+
if (readAbort.signal.aborted) throw new GrepIsolationError("cancelled");
|
|
2731
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
2732
|
+
bytes += buffer.length;
|
|
2733
|
+
if (bytes > MAX_GREP_FILE_BYTES) throw new GrepIsolationError("limit");
|
|
2734
|
+
chunks.push(buffer);
|
|
2735
|
+
}
|
|
2736
|
+
} finally {
|
|
2737
|
+
stream.destroy();
|
|
2738
|
+
}
|
|
2739
|
+
const buffer = Buffer.concat(chunks, bytes);
|
|
2740
|
+
const checkLen = Math.min(buffer.length, 8192);
|
|
2741
|
+
let hasBinary = false;
|
|
2742
|
+
for (let i = 0; i < checkLen; i++) if (buffer[i] === 0) {
|
|
2743
|
+
hasBinary = true;
|
|
2744
|
+
break;
|
|
2745
|
+
}
|
|
2746
|
+
if (hasBinary) return [];
|
|
2747
|
+
content = buffer.toString("utf8");
|
|
2748
|
+
} catch (error) {
|
|
2749
|
+
if (error instanceof GrepIsolationError) throw error;
|
|
2750
|
+
if (readAbort.signal.aborted) throw new GrepIsolationError("cancelled");
|
|
2751
|
+
return [];
|
|
2752
|
+
}
|
|
2753
|
+
return search.search(content, filePath, contextLines, outputMode);
|
|
2754
|
+
};
|
|
2755
|
+
const worker = async () => {
|
|
2756
|
+
while (failure === void 0 && nextFile < files.length) {
|
|
2757
|
+
const index = nextFile++;
|
|
2758
|
+
try {
|
|
2759
|
+
orderedMatches[index] = await readAndSearch(files[index]);
|
|
2760
|
+
} catch (error) {
|
|
2761
|
+
if (failure === void 0) {
|
|
2762
|
+
failure = error;
|
|
2763
|
+
readAbort.abort();
|
|
2764
|
+
search.stop(error instanceof Error ? error : /* @__PURE__ */ new Error("Grep search failed"));
|
|
2765
|
+
}
|
|
2766
|
+
}
|
|
2767
|
+
}
|
|
2768
|
+
};
|
|
2769
|
+
await Promise.all(Array.from({ length: Math.min(READ_CONCURRENCY_LIMIT, files.length) }, worker));
|
|
2770
|
+
if (failure !== void 0) throw failure;
|
|
2771
|
+
perFileMatches = orderedMatches;
|
|
2772
|
+
} catch (error) {
|
|
2773
|
+
const message = error instanceof Error ? error.message : "";
|
|
2774
|
+
throw error instanceof GrepIsolationError ? error : new GrepIsolationError(message.includes("timed out") ? "timeout" : message.includes("cancelled") ? "cancelled" : message.includes("byte limit") ? "limit" : "failed");
|
|
2775
|
+
} finally {
|
|
2776
|
+
options.signal?.removeEventListener("abort", abortReads);
|
|
2777
|
+
await search.stop();
|
|
2778
|
+
}
|
|
2779
|
+
let outputBytes = 0;
|
|
2780
|
+
for (const matches of perFileMatches) for (const match of matches) {
|
|
2781
|
+
outputBytes += Buffer.byteLength(match, "utf8") + 1;
|
|
2782
|
+
if (outputBytes > 4 * 1024 * 1024) throw new GrepIsolationError("limit");
|
|
2783
|
+
}
|
|
2784
|
+
let outputLines = perFileMatches.flat();
|
|
2785
|
+
if (headLimit !== void 0 && outputLines.length > headLimit) {
|
|
2786
|
+
const truncatedCount = outputLines.length - headLimit;
|
|
2787
|
+
outputLines = [...outputLines.slice(0, headLimit), `(+${truncatedCount} more results truncated by headLimit)`];
|
|
2788
|
+
}
|
|
2789
|
+
if (filesTruncated) outputLines = [...outputLines, `[File enumeration stopped early; the search tree has more files than this tool scans in one call. Results may be incomplete — narrow the path or glob.]`];
|
|
2790
|
+
const result = {
|
|
2791
|
+
success: true,
|
|
2792
|
+
output: outputLines.length > 0 ? outputLines.join("\n") : "(no matches)"
|
|
2793
|
+
};
|
|
2794
|
+
return JSON.stringify(result);
|
|
2795
|
+
}
|
|
2796
|
+
/** The registered name of the shell tool this package's default assembly ships (NEUT-002). */
|
|
2797
|
+
const DEFAULT_SHELL_TOOL_NAME = "Shell";
|
|
2798
|
+
/** Build the default description, referencing the actually-registered shell tool by name. */
|
|
2799
|
+
function buildGrepDescription(shellToolName) {
|
|
2800
|
+
return `A powerful search tool built on regex matching.\n\nSupports full regex syntax (e.g., 'log.*Error', 'function\\\\s+\\\\w+'). Filter files with glob parameter (e.g., '*.js', '**/*.tsx').\n\nOutput modes: 'content' shows matching lines with context, 'files_with_matches' shows only file paths (default), 'count' shows per-file match counts.\n\nPrefer this tool over running grep or rg through the ${shellToolName} tool — it returns structured results directly.\n\nUse headLimit to control result size and save context space.`;
|
|
2801
|
+
}
|
|
2802
|
+
/**
|
|
2803
|
+
* Create a GrepTool instance — register with Robota agent tools registry.
|
|
2804
|
+
*/
|
|
2805
|
+
function createGrepTool(options) {
|
|
2806
|
+
return createZodFunctionTool("Grep", options.description ?? buildGrepDescription(options.shellToolName ?? DEFAULT_SHELL_TOOL_NAME), GrepSchema, async (params) => {
|
|
2807
|
+
return grepFileTool(params, options);
|
|
2808
|
+
});
|
|
2809
|
+
}
|
|
2810
|
+
//#endregion
|
|
2811
|
+
//#region src/builtins/web-fetch-tool.ts
|
|
2812
|
+
/**
|
|
2813
|
+
* WebFetchTool — fetch a URL and return its content as text.
|
|
2814
|
+
*
|
|
2815
|
+
* HTML is stripped to plain text for readability. Fetches through the shared egress boundary
|
|
2816
|
+
* (`fetchWithEgressPolicy`, #2026): loopback / private / link-local / metadata destinations are
|
|
2817
|
+
* refused, redirects are re-validated, and the response is capped while streaming.
|
|
2818
|
+
*/
|
|
2819
|
+
const DEFAULT_TIMEOUT_MS$1 = 3e4;
|
|
2820
|
+
const MAX_RESPONSE_BYTES = 5e6;
|
|
2821
|
+
const WebFetchSchema = zod.z.object({
|
|
2822
|
+
url: zod.z.string().describe("The URL to fetch"),
|
|
2823
|
+
headers: zod.z.record(zod.z.string()).optional().describe("Optional HTTP headers as key-value pairs")
|
|
2824
|
+
});
|
|
2825
|
+
/**
|
|
2826
|
+
* Remove every `<tag>…</tag>` element — the linear equivalent of `replace(/<tag[\s\S]*?<\/tag>/gi, '')`.
|
|
2827
|
+
*
|
|
2828
|
+
* The regex form is quadratic: every `<tag` with no closing tag after it rescans to end of input, and the scan
|
|
2829
|
+
* then restarts at the next one. `htmlToText`'s input is a **response body from an arbitrary URL**, capped only
|
|
2830
|
+
* at {@link MAX_RESPONSE_BYTES} (5 MB) — 5 MB of `<script` would have taken minutes. Because the closing tag is
|
|
2831
|
+
* searched forward, its absence at one opener means no later opener can have one either, so the scan stops.
|
|
2832
|
+
*
|
|
2833
|
+
* Case folding is `[A-Z]`-only, not `toLowerCase()`: `toLowerCase()` can change a string's LENGTH (U+0130
|
|
2834
|
+
* lowercases to two code units), which would desynchronise the indices from the original text.
|
|
2835
|
+
*/
|
|
2836
|
+
function stripElement(html, tag) {
|
|
2837
|
+
const openTag = `<${tag}`;
|
|
2838
|
+
const closeTag = `</${tag}>`;
|
|
2839
|
+
const haystack = html.replace(/[A-Z]/g, (c) => c.toLowerCase());
|
|
2840
|
+
const parts = [];
|
|
2841
|
+
let cursor = 0;
|
|
2842
|
+
for (;;) {
|
|
2843
|
+
const open = haystack.indexOf(openTag, cursor);
|
|
2844
|
+
if (open < 0) break;
|
|
2845
|
+
const close = haystack.indexOf(closeTag, open + openTag.length);
|
|
2846
|
+
if (close < 0) break;
|
|
2847
|
+
parts.push(html.slice(cursor, open));
|
|
2848
|
+
cursor = close + closeTag.length;
|
|
2849
|
+
}
|
|
2850
|
+
parts.push(html.slice(cursor));
|
|
2851
|
+
return parts.join("");
|
|
2852
|
+
}
|
|
2853
|
+
/**
|
|
2854
|
+
* Replace every `<…>` tag with a space — the linear equivalent of `replace(/<[^>]+>/g, ' ')`.
|
|
2855
|
+
*
|
|
2856
|
+
* Same defect, same input: `[^>]+` cannot cross a `>`, so a `<` with no `>` after it consumed the rest of the
|
|
2857
|
+
* document and then backtracked over it, once per `<`. A page of 200 K `<` characters took 12.6 s; the 5 MB the
|
|
2858
|
+
* fetch allows would have taken hours. `close === open + 1` reproduces the regex's `+` (a tag body must be at
|
|
2859
|
+
* least one character), so a literal `<>` is left in the text exactly as before.
|
|
2860
|
+
*/
|
|
2861
|
+
function stripTags(html) {
|
|
2862
|
+
const parts = [];
|
|
2863
|
+
let cursor = 0;
|
|
2864
|
+
for (;;) {
|
|
2865
|
+
const open = html.indexOf("<", cursor);
|
|
2866
|
+
if (open < 0) break;
|
|
2867
|
+
const close = html.indexOf(">", open + 1);
|
|
2868
|
+
if (close < 0) break;
|
|
2869
|
+
if (close === open + 1) {
|
|
2870
|
+
parts.push(html.slice(cursor, open + 1));
|
|
2871
|
+
cursor = open + 1;
|
|
2872
|
+
continue;
|
|
2873
|
+
}
|
|
2874
|
+
parts.push(html.slice(cursor, open), " ");
|
|
2875
|
+
cursor = close + 1;
|
|
2876
|
+
}
|
|
2877
|
+
parts.push(html.slice(cursor));
|
|
2878
|
+
return parts.join("");
|
|
2879
|
+
}
|
|
2880
|
+
/**
|
|
2881
|
+
* The character entities {@link htmlToText} decodes, and the single alternation that matches them.
|
|
2882
|
+
*
|
|
2883
|
+
* SEC-004 (`js/double-escaping`): decoding these by CHAINED `.replace()` calls with `&` first
|
|
2884
|
+
* decodes twice. `&lt;` — how a page encodes the literal text `<` so a browser DISPLAYS it —
|
|
2885
|
+
* became `<` after the `&` pass and then `<` after the `<` pass, so a page reading
|
|
2886
|
+
* `&lt;script&gt;` came back out of a tag-stripping converter as `<script>`. One pass over
|
|
2887
|
+
* one alternation decodes each entity exactly once and never rescans its own output, so the decoder
|
|
2888
|
+
* is the inverse of the encoder for every input rather than only for singly-encoded ones.
|
|
2889
|
+
*/
|
|
2890
|
+
const HTML_ENTITIES = {
|
|
2891
|
+
"&": "&",
|
|
2892
|
+
"<": "<",
|
|
2893
|
+
">": ">",
|
|
2894
|
+
""": "\"",
|
|
2895
|
+
"'": "'",
|
|
2896
|
+
" ": " "
|
|
2897
|
+
};
|
|
2898
|
+
const HTML_ENTITY_PATTERN = /&(?:amp|lt|gt|quot|nbsp|#39);/g;
|
|
2899
|
+
/** Strip HTML tags and decode common entities to produce readable text. */
|
|
1354
2900
|
function htmlToText(html) {
|
|
1355
|
-
|
|
2901
|
+
return stripTags(stripElement(stripElement(html, "script"), "style")).replace(HTML_ENTITY_PATTERN, (entity) => HTML_ENTITIES[entity]).replace(/\s+/g, " ").trim();
|
|
1356
2902
|
}
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
2903
|
+
function classifyFetchError(err) {
|
|
2904
|
+
if (!(err instanceof Error)) return String(err);
|
|
2905
|
+
if (err.name === "AbortError") return `Request timed out after ${DEFAULT_TIMEOUT_MS$1 / 1e3}s. The server did not respond in time.`;
|
|
2906
|
+
const code = err.code;
|
|
2907
|
+
if (code === "ENOTFOUND" || code === "EAI_AGAIN") return `Network error: DNS resolution failed for this host. The URL may be incorrect or the host does not exist. Do not retry with the same URL.`;
|
|
2908
|
+
if (code === "ECONNREFUSED") return `Network error: Connection refused. The server is not accepting connections at this address. Do not retry with the same URL.`;
|
|
2909
|
+
if (code === "ECONNRESET") return `Network error: Connection was reset by the server. The server may be temporarily unavailable.`;
|
|
2910
|
+
if (code === "ETIMEDOUT") return `Network error: Connection timed out. The server is not reachable within the expected time.`;
|
|
2911
|
+
if (code === "CERT_HAS_EXPIRED" || code === "UNABLE_TO_VERIFY_LEAF_SIGNATURE") return `Network error: SSL certificate error (${code}). The server's certificate is invalid. Do not retry with the same URL.`;
|
|
2912
|
+
return `Network error: ${err.message} Check that the URL is correct and the server is reachable.`;
|
|
2913
|
+
}
|
|
2914
|
+
async function runWebFetch(args, egress, signal) {
|
|
2915
|
+
const { url, headers } = args;
|
|
2916
|
+
try {
|
|
2917
|
+
new URL(url);
|
|
2918
|
+
} catch {
|
|
2919
|
+
const result = {
|
|
2920
|
+
success: false,
|
|
2921
|
+
output: "",
|
|
2922
|
+
error: `Invalid URL: "${url}". Fix the URL format before retrying.`
|
|
2923
|
+
};
|
|
2924
|
+
return JSON.stringify(result);
|
|
2925
|
+
}
|
|
2926
|
+
try {
|
|
2927
|
+
const response = await (0, _robota_sdk_agent_core_node.fetchWithEgressPolicy)(url, {
|
|
2928
|
+
headers: {
|
|
2929
|
+
"User-Agent": "Robota-CLI/3.0",
|
|
2930
|
+
...headers ?? {}
|
|
2931
|
+
},
|
|
2932
|
+
signal,
|
|
2933
|
+
timeoutMs: DEFAULT_TIMEOUT_MS$1,
|
|
2934
|
+
maxResponseBytes: MAX_RESPONSE_BYTES
|
|
2935
|
+
}, egress.policy, egress.deps);
|
|
2936
|
+
if (!response.ok) {
|
|
2937
|
+
const { rejection } = response;
|
|
2938
|
+
const result = {
|
|
2939
|
+
success: false,
|
|
2940
|
+
output: "",
|
|
2941
|
+
error: rejection.reason === "response_too_large" ? `Response too large (max ${MAX_RESPONSE_BYTES} bytes). Consider fetching a more specific URL or a paginated endpoint.` : `Blocked by egress policy: ${rejection.message} Do not retry with the same URL.`
|
|
2942
|
+
};
|
|
2943
|
+
return JSON.stringify(result);
|
|
2944
|
+
}
|
|
2945
|
+
if (response.status < 200 || response.status >= 300) {
|
|
2946
|
+
const retryHint = response.status >= 500 ? " The server is temporarily unavailable — retrying may help." : " Do not retry with the same URL.";
|
|
2947
|
+
const result = {
|
|
2948
|
+
success: false,
|
|
2949
|
+
output: "",
|
|
2950
|
+
error: `HTTP ${response.status} ${response.statusText}.${retryHint}`
|
|
2951
|
+
};
|
|
2952
|
+
return JSON.stringify(result);
|
|
2953
|
+
}
|
|
2954
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
2955
|
+
let text = new TextDecoder().decode(response.body);
|
|
2956
|
+
if (contentType.includes("html")) text = htmlToText(text);
|
|
2957
|
+
return JSON.stringify({
|
|
2958
|
+
success: true,
|
|
2959
|
+
output: text
|
|
2960
|
+
});
|
|
2961
|
+
} catch (err) {
|
|
2962
|
+
const result = {
|
|
2963
|
+
success: false,
|
|
2964
|
+
output: "",
|
|
2965
|
+
error: classifyFetchError(err)
|
|
2966
|
+
};
|
|
2967
|
+
return JSON.stringify(result);
|
|
2968
|
+
}
|
|
2969
|
+
}
|
|
2970
|
+
const DEFAULT_WEB_FETCH_DESCRIPTION = "Fetch a URL and return its content as text. HTML pages are converted to plain text.";
|
|
2971
|
+
/**
|
|
2972
|
+
* Create a WebFetchTool instance — register with Robota agent tools registry.
|
|
2973
|
+
*/
|
|
2974
|
+
function createWebFetchTool(options = {}) {
|
|
2975
|
+
const egress = options.egress ?? {};
|
|
2976
|
+
return createZodFunctionTool("WebFetch", options.description ?? DEFAULT_WEB_FETCH_DESCRIPTION, WebFetchSchema, async (params, context) => runWebFetch(params, egress, context?.signal));
|
|
2977
|
+
}
|
|
2978
|
+
/**
|
|
2979
|
+
* WebFetchTool instance — register with Robota agent tools registry.
|
|
2980
|
+
*/
|
|
2981
|
+
const webFetchTool = createWebFetchTool();
|
|
2982
|
+
//#endregion
|
|
2983
|
+
//#region src/builtins/brave-search-provider.ts
|
|
2984
|
+
const BRAVE_SEARCH_ENDPOINT = "https://api.search.brave.com/res/v1/web/search";
|
|
2985
|
+
/** Brave caps `count` at 20 per request. */
|
|
2986
|
+
const BRAVE_MAX_COUNT = 20;
|
|
2987
|
+
/**
|
|
2988
|
+
* Create the Brave Search provider. Throws from `search()` when `BRAVE_API_KEY` is not set or
|
|
2989
|
+
* the API responds with an error — the tool layer surfaces the message as a structured result.
|
|
2990
|
+
*/
|
|
2991
|
+
function createBraveSearchProvider() {
|
|
2992
|
+
return { async search({ query, limit }, signal) {
|
|
2993
|
+
const apiKey = process.env["BRAVE_API_KEY"];
|
|
2994
|
+
if (!apiKey) throw new Error("Web search requires BRAVE_API_KEY environment variable for the default Brave Search provider, or inject a custom search provider at the composition root.");
|
|
2995
|
+
const params = new URLSearchParams({
|
|
2996
|
+
q: query,
|
|
2997
|
+
count: String(Math.min(limit, BRAVE_MAX_COUNT))
|
|
2998
|
+
});
|
|
2999
|
+
const response = await fetch(`${BRAVE_SEARCH_ENDPOINT}?${params}`, {
|
|
3000
|
+
headers: {
|
|
3001
|
+
Accept: "application/json",
|
|
3002
|
+
"Accept-Encoding": "gzip",
|
|
3003
|
+
"X-Subscription-Token": apiKey
|
|
3004
|
+
},
|
|
3005
|
+
...signal ? { signal } : {}
|
|
3006
|
+
});
|
|
3007
|
+
if (!response.ok) throw new Error(`Brave Search API error: HTTP ${response.status} ${response.statusText}`);
|
|
3008
|
+
return ((await response.json()).web?.results ?? []).map((r) => ({
|
|
3009
|
+
title: r.title,
|
|
3010
|
+
url: r.url,
|
|
3011
|
+
snippet: r.description
|
|
3012
|
+
}));
|
|
3013
|
+
} };
|
|
3014
|
+
}
|
|
3015
|
+
//#endregion
|
|
3016
|
+
//#region src/builtins/web-search-tool.ts
|
|
3017
|
+
/**
|
|
3018
|
+
* WebSearchTool — search the web and return results.
|
|
3019
|
+
*
|
|
3020
|
+
* Vendor-free tool layer (NEUT-008): composes over the duck-typed `IWebSearchProvider` port.
|
|
3021
|
+
* The default provider is the vendor-specific default adapter wired at creation time; a custom
|
|
3022
|
+
* provider is injected via `createWebSearchTool({ provider })`. Provider failures (missing
|
|
3023
|
+
* configuration, HTTP/network errors) are thrown by the provider and surfaced here as
|
|
3024
|
+
* structured error results.
|
|
3025
|
+
*/
|
|
3026
|
+
const DEFAULT_LIMIT = 10;
|
|
3027
|
+
const DEFAULT_TIMEOUT_MS = 15e3;
|
|
3028
|
+
const DEFAULT_WEB_SEARCH_DESCRIPTION = "Search the web and return results with title, URL, and snippet.";
|
|
3029
|
+
const WebSearchSchema = zod.z.object({
|
|
3030
|
+
query: zod.z.string().describe("The search query"),
|
|
3031
|
+
limit: zod.z.number().optional().describe(`Maximum number of results to return (default: ${DEFAULT_LIMIT})`)
|
|
1421
3032
|
});
|
|
1422
|
-
async function runWebSearch(args) {
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
globTool,
|
|
1488
|
-
grepTool,
|
|
1489
|
-
readTool,
|
|
1490
|
-
webFetchTool,
|
|
1491
|
-
webSearchTool,
|
|
1492
|
-
writeTool,
|
|
1493
|
-
zodToJsonSchema
|
|
3033
|
+
async function runWebSearch(args, provider, signal) {
|
|
3034
|
+
const { query, limit = DEFAULT_LIMIT } = args;
|
|
3035
|
+
try {
|
|
3036
|
+
const controller = new AbortController();
|
|
3037
|
+
const timeout = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT_MS);
|
|
3038
|
+
const searchSignal = signal ? AbortSignal.any([controller.signal, signal]) : controller.signal;
|
|
3039
|
+
try {
|
|
3040
|
+
const results = await provider.search({
|
|
3041
|
+
query,
|
|
3042
|
+
limit
|
|
3043
|
+
}, searchSignal);
|
|
3044
|
+
const result = {
|
|
3045
|
+
success: true,
|
|
3046
|
+
output: JSON.stringify(results, null, 2)
|
|
3047
|
+
};
|
|
3048
|
+
return JSON.stringify(result);
|
|
3049
|
+
} finally {
|
|
3050
|
+
clearTimeout(timeout);
|
|
3051
|
+
}
|
|
3052
|
+
} catch (err) {
|
|
3053
|
+
const result = {
|
|
3054
|
+
success: false,
|
|
3055
|
+
output: "",
|
|
3056
|
+
error: err instanceof Error ? err.message : String(err)
|
|
3057
|
+
};
|
|
3058
|
+
return JSON.stringify(result);
|
|
3059
|
+
}
|
|
3060
|
+
}
|
|
3061
|
+
/**
|
|
3062
|
+
* Create a WebSearchTool instance — register with Robota agent tools registry.
|
|
3063
|
+
*/
|
|
3064
|
+
function createWebSearchTool(options = {}) {
|
|
3065
|
+
const provider = options.provider ?? createBraveSearchProvider();
|
|
3066
|
+
return createZodFunctionTool("WebSearch", options.description ?? DEFAULT_WEB_SEARCH_DESCRIPTION, WebSearchSchema, async (params, context) => runWebSearch(params, provider, context?.signal));
|
|
3067
|
+
}
|
|
3068
|
+
/**
|
|
3069
|
+
* WebSearchTool instance — register with Robota agent tools registry.
|
|
3070
|
+
*/
|
|
3071
|
+
const webSearchTool = createWebSearchTool();
|
|
3072
|
+
//#endregion
|
|
3073
|
+
//#region src/builtins/ask-user-question-tool.ts
|
|
3074
|
+
/**
|
|
3075
|
+
* AskUserQuestionTool — let the model ask the user structured questions mid-turn (CMD-005).
|
|
3076
|
+
*
|
|
3077
|
+
* Built on the CMD-004 ask seam: each question maps onto the `IActionRequest` SSOT and is issued
|
|
3078
|
+
* through the injected `IToolExecutionContext.ask` port; the attached environment renders it (Ink
|
|
3079
|
+
* dialog, web modal, programmatic pre-answer) and the answers return as the tool result.
|
|
3080
|
+
*
|
|
3081
|
+
* Contract points (spec CMD-005):
|
|
3082
|
+
* - 1–4 questions per call, asked sequentially (the channel's ask queue renders one at a time).
|
|
3083
|
+
* - Cancellation is data, not an exception: a dismissed question yields `{ cancelled: true }` and the
|
|
3084
|
+
* remaining unasked questions of the same call are marked cancelled too (no per-item re-prompt).
|
|
3085
|
+
* - No `context.ask` (headless/automation): returns `{ unavailable: true, reason }` — never a silent
|
|
3086
|
+
* guess, never a thrown error, so the model can continue autonomously.
|
|
3087
|
+
*/
|
|
3088
|
+
const MAX_QUESTIONS = 4;
|
|
3089
|
+
const QuestionSchema = zod.z.object({
|
|
3090
|
+
question: zod.z.string().min(1).describe("The complete question to ask the user."),
|
|
3091
|
+
header: zod.z.string().optional().describe("Very short topic label for the question (e.g. \"Auth method\")."),
|
|
3092
|
+
options: zod.z.array(zod.z.union([zod.z.string().min(1).describe("Display text of this choice."), zod.z.object({
|
|
3093
|
+
label: zod.z.string().min(1).describe("Display text of this choice."),
|
|
3094
|
+
description: zod.z.string().optional().describe("What choosing this option means.")
|
|
3095
|
+
})])).optional().describe("Predefined choices (strings or {label, description}). Omit for pure free text."),
|
|
3096
|
+
multiSelect: zod.z.boolean().optional().describe("Allow selecting multiple options (default: single select)."),
|
|
3097
|
+
allowFreeText: zod.z.boolean().optional().describe("Allow a typed custom answer besides the options (default: true).")
|
|
1494
3098
|
});
|
|
3099
|
+
const AskUserQuestionSchema = zod.z.object({ questions: zod.z.array(QuestionSchema).min(1).max(MAX_QUESTIONS).describe(`Questions to ask the user (1-${MAX_QUESTIONS}), rendered one at a time.`) });
|
|
3100
|
+
const ASK_USER_QUESTION_DESCRIPTION = [
|
|
3101
|
+
"Ask the user one or more structured questions and wait for their answers.",
|
|
3102
|
+
"",
|
|
3103
|
+
"Use this when you are blocked on a decision only the user can make — ambiguous requirements,",
|
|
3104
|
+
"mutually exclusive approaches, or choices with real trade-offs. Do not use it for decisions with",
|
|
3105
|
+
"a conventional default or facts you can verify yourself.",
|
|
3106
|
+
"",
|
|
3107
|
+
`Provide 1-${MAX_QUESTIONS} questions. Each question offers predefined options and/or free text:`,
|
|
3108
|
+
" - options + default: user picks one option (or types a custom answer unless allowFreeText: false)",
|
|
3109
|
+
" - multiSelect: true: user may pick several options",
|
|
3110
|
+
" - no options: pure free-text entry",
|
|
3111
|
+
"",
|
|
3112
|
+
"The result is a JSON array with one entry per question: the selected option labels in `values`",
|
|
3113
|
+
"and/or the typed answer in `text`, or `cancelled: true` if the user dismissed the question.",
|
|
3114
|
+
"If no interactive user is attached (headless run), the result is `{ unavailable: true }` —",
|
|
3115
|
+
"continue autonomously with your best judgment and say what you assumed."
|
|
3116
|
+
].join("\n");
|
|
3117
|
+
function toActionRequest(question) {
|
|
3118
|
+
const options = (question.options ?? []).map((option) => typeof option === "string" ? { label: option } : option);
|
|
3119
|
+
const multi = question.multiSelect === true && options.length > 1;
|
|
3120
|
+
return {
|
|
3121
|
+
id: `ask_${(0, node_crypto.randomUUID)()}`,
|
|
3122
|
+
title: question.question,
|
|
3123
|
+
...question.header !== void 0 ? { description: question.header } : {},
|
|
3124
|
+
...options.length > 0 ? { options: options.map((o) => ({
|
|
3125
|
+
value: o.label,
|
|
3126
|
+
label: o.label,
|
|
3127
|
+
...o.description !== void 0 ? { description: o.description } : {}
|
|
3128
|
+
})) } : {},
|
|
3129
|
+
minSelect: options.length > 0 ? 1 : 0,
|
|
3130
|
+
maxSelect: multi ? options.length : 1,
|
|
3131
|
+
allowFreeText: question.allowFreeText !== false || options.length === 0
|
|
3132
|
+
};
|
|
3133
|
+
}
|
|
3134
|
+
async function askQuestions(args, ask) {
|
|
3135
|
+
const answers = [];
|
|
3136
|
+
let dismissed = false;
|
|
3137
|
+
for (const question of args.questions) {
|
|
3138
|
+
if (dismissed) {
|
|
3139
|
+
answers.push({
|
|
3140
|
+
question: question.question,
|
|
3141
|
+
cancelled: true
|
|
3142
|
+
});
|
|
3143
|
+
continue;
|
|
3144
|
+
}
|
|
3145
|
+
const response = await ask(toActionRequest(question));
|
|
3146
|
+
if (response.type === "cancelled") {
|
|
3147
|
+
dismissed = true;
|
|
3148
|
+
answers.push({
|
|
3149
|
+
question: question.question,
|
|
3150
|
+
cancelled: true
|
|
3151
|
+
});
|
|
3152
|
+
continue;
|
|
3153
|
+
}
|
|
3154
|
+
answers.push({
|
|
3155
|
+
question: question.question,
|
|
3156
|
+
values: [...response.values],
|
|
3157
|
+
...response.text !== void 0 ? { text: response.text } : {}
|
|
3158
|
+
});
|
|
3159
|
+
}
|
|
3160
|
+
return { answers };
|
|
3161
|
+
}
|
|
3162
|
+
/**
|
|
3163
|
+
* Create an `AskUserQuestion` tool instance — register with the Robota agent tools registry.
|
|
3164
|
+
*/
|
|
3165
|
+
function createAskUserQuestionTool(options = {}) {
|
|
3166
|
+
return createZodFunctionTool("AskUserQuestion", options.description ?? ASK_USER_QUESTION_DESCRIPTION, AskUserQuestionSchema, async (params, context) => {
|
|
3167
|
+
const args = params;
|
|
3168
|
+
const ask = context?.ask;
|
|
3169
|
+
const output = ask ? await askQuestions(args, ask) : {
|
|
3170
|
+
unavailable: true,
|
|
3171
|
+
reason: "no interactive user attached"
|
|
3172
|
+
};
|
|
3173
|
+
const result = {
|
|
3174
|
+
success: true,
|
|
3175
|
+
output: JSON.stringify(output)
|
|
3176
|
+
};
|
|
3177
|
+
return JSON.stringify(result);
|
|
3178
|
+
});
|
|
3179
|
+
}
|
|
3180
|
+
/** `AskUserQuestion` tool instance — register with the Robota agent tools registry. */
|
|
3181
|
+
const askUserQuestionTool = createAskUserQuestionTool();
|
|
3182
|
+
//#endregion
|
|
3183
|
+
//#region src/builtins/tool-search-matching.ts
|
|
3184
|
+
/** Both vendors default a tool search to five results; so does this one. */
|
|
3185
|
+
const DEFAULT_TOOL_SEARCH_LIMIT = 5;
|
|
3186
|
+
/**
|
|
3187
|
+
* Where a query matched, lowest first — the primary sort key.
|
|
3188
|
+
*
|
|
3189
|
+
* A tool whose NAME the query names is a better answer than one that merely mentions it in a
|
|
3190
|
+
* parameter description, and saying so is what makes "the top five" meaningful once a catalog is
|
|
3191
|
+
* large enough for the limit to bite.
|
|
3192
|
+
*/
|
|
3193
|
+
const RANK_EXACT_NAME = 0;
|
|
3194
|
+
const RANK_NAME = 1;
|
|
3195
|
+
const RANK_DESCRIPTION = 2;
|
|
3196
|
+
const RANK_PARAMETER = 3;
|
|
3197
|
+
/** Not a match at all — filtered out rather than ranked last. */
|
|
3198
|
+
const RANK_NONE = Number.POSITIVE_INFINITY;
|
|
3199
|
+
/** Every parameter name and description in a schema, including nested nodes. */
|
|
3200
|
+
function collectParameterText(node, into) {
|
|
3201
|
+
if (node.description !== void 0) into.push(node.description);
|
|
3202
|
+
for (const [name, child] of Object.entries(node.properties ?? {})) {
|
|
3203
|
+
into.push(name);
|
|
3204
|
+
collectParameterText(child, into);
|
|
3205
|
+
}
|
|
3206
|
+
if (node.items !== void 0) collectParameterText(node.items, into);
|
|
3207
|
+
for (const branch of node.anyOf ?? []) collectParameterText(branch, into);
|
|
3208
|
+
}
|
|
3209
|
+
function rankMatch(schema, query) {
|
|
3210
|
+
const name = schema.name.toLowerCase();
|
|
3211
|
+
if (name === query) return RANK_EXACT_NAME;
|
|
3212
|
+
if (name.includes(query)) return RANK_NAME;
|
|
3213
|
+
if (schema.description.toLowerCase().includes(query)) return RANK_DESCRIPTION;
|
|
3214
|
+
const parameterText = [];
|
|
3215
|
+
collectParameterText(schema.parameters, parameterText);
|
|
3216
|
+
if (parameterText.some((text) => text.toLowerCase().includes(query))) return RANK_PARAMETER;
|
|
3217
|
+
return RANK_NONE;
|
|
3218
|
+
}
|
|
3219
|
+
/**
|
|
3220
|
+
* The tools a query selects, best match first and capped at `limit`.
|
|
3221
|
+
*
|
|
3222
|
+
* Ordering is total and deterministic: rank first, then name, so two tools that matched the same way
|
|
3223
|
+
* never trade places between calls. An empty query string matches nothing rather than everything —
|
|
3224
|
+
* "search for nothing" is a question with an empty answer, not a request for the whole catalog.
|
|
3225
|
+
*/
|
|
3226
|
+
function matchDeferredTools(schemas, query, limit) {
|
|
3227
|
+
const needle = query.trim().toLowerCase();
|
|
3228
|
+
if (needle.length === 0) return [];
|
|
3229
|
+
return schemas.map((schema) => ({
|
|
3230
|
+
schema,
|
|
3231
|
+
rank: rankMatch(schema, needle)
|
|
3232
|
+
})).filter((entry) => entry.rank !== RANK_NONE).sort((a, b) => a.rank - b.rank || (a.schema.name < b.schema.name ? -1 : 1)).slice(0, limit).map((entry) => entry.schema);
|
|
3233
|
+
}
|
|
3234
|
+
//#endregion
|
|
3235
|
+
//#region src/builtins/tool-search-tool.ts
|
|
3236
|
+
/**
|
|
3237
|
+
* ToolSearch — the model-facing half of client-side tool deferral (CLI-1990 § Solution 4).
|
|
3238
|
+
*
|
|
3239
|
+
* A tool that declares `deferLoading` is withheld from the request entirely while the tool-search
|
|
3240
|
+
* policy is engaged, so the model never sees its schema. This tool is how the model gets it back:
|
|
3241
|
+
* it searches the withheld catalog by query, or loads an exact list by name, and the runtime marks
|
|
3242
|
+
* the matches loaded — so the NEXT round's `tools` array carries their full definitions and they
|
|
3243
|
+
* stay callable for the rest of the session.
|
|
3244
|
+
*
|
|
3245
|
+
* Deliberately an ordinary function tool rather than a vendor block. Anthropic and OpenAI each ship
|
|
3246
|
+
* a server-side tool search, but neither reduces the request payload (the API needs every definition
|
|
3247
|
+
* to run the search), Gemini has no equivalent at all, and both vendors document a client-executed
|
|
3248
|
+
* search as the portable form. One shape therefore runs everywhere and saves both wire bytes and
|
|
3249
|
+
* context tokens.
|
|
3250
|
+
*
|
|
3251
|
+
* Two results are NOT errors, and the distinction is the contract:
|
|
3252
|
+
* - a query that matches nothing returns `{ loaded: [], unavailableSources: [] }` — a normal empty
|
|
3253
|
+
* answer, mirroring the vendor's own empty `tool_references` array;
|
|
3254
|
+
* - an unknown entry in `names` throws, naming the entry, and loads nothing — asking for a tool that
|
|
3255
|
+
* does not exist is a mistake to correct, not an empty search.
|
|
3256
|
+
*
|
|
3257
|
+
* `unavailableSources` is present and empty from day one. MCP-003 (the connection and capability
|
|
3258
|
+
* supervisor) fills it with servers that failed or need auth, so a model told "nothing matched" can
|
|
3259
|
+
* tell that apart from "the server holding it is down" — without a contract change here.
|
|
3260
|
+
*/
|
|
3261
|
+
/**
|
|
3262
|
+
* The registered name — agent-core's own constant, re-exported under this package's name so the
|
|
3263
|
+
* execution layer's unknown-tool remedy and this tool can never name two different things.
|
|
3264
|
+
*/
|
|
3265
|
+
const TOOL_SEARCH_NAME = _robota_sdk_agent_core.TOOL_SEARCH_TOOL_NAME;
|
|
3266
|
+
const ToolSearchSchema = zod.z.object({
|
|
3267
|
+
query: zod.z.string().optional().describe("Text matched case-insensitively against each withheld tool's name, description, and its parameters' names and descriptions. An exact tool name is a valid query."),
|
|
3268
|
+
names: zod.z.array(zod.z.string().min(1)).optional().describe("Exact tool names to load, skipping the search. An unknown name is an error naming it."),
|
|
3269
|
+
limit: zod.z.number().int().positive().optional().describe(`Maximum tools to load from a query match (default 5).`)
|
|
3270
|
+
});
|
|
3271
|
+
const TOOL_SEARCH_DESCRIPTION = [
|
|
3272
|
+
"Load tools whose definitions are withheld from your tool list, so you can call them.",
|
|
3273
|
+
"",
|
|
3274
|
+
"Some tools are deferred: they exist and are callable, but their schemas are not sent to you until",
|
|
3275
|
+
"you load them here. If a capability you need is not in your tool list, search for it before",
|
|
3276
|
+
"concluding it is unavailable — and if a tool call fails as \"deferred and not yet loaded\", load it",
|
|
3277
|
+
"with this tool and call it again.",
|
|
3278
|
+
"",
|
|
3279
|
+
" - query: what you are trying to do (e.g. \"read a spreadsheet\", \"postgres\"). Matched against tool",
|
|
3280
|
+
" names, descriptions, and parameter names and descriptions. An exact tool name works too.",
|
|
3281
|
+
` - names: load exactly these tools, skipping the search. Unknown names are an error.`,
|
|
3282
|
+
` - limit: how many matches to load (default 5).`,
|
|
3283
|
+
"",
|
|
3284
|
+
"The result lists what is now loaded; those tools appear in your tool list from your next turn and",
|
|
3285
|
+
"stay available. A query that matches nothing returns an empty list — that is a normal answer, not",
|
|
3286
|
+
"a failure. `unavailableSources` names any tool source that could not be consulted."
|
|
3287
|
+
].join("\n");
|
|
3288
|
+
/**
|
|
3289
|
+
* The catalog the runtime injects, or a thrown wiring error.
|
|
3290
|
+
*
|
|
3291
|
+
* Its absence is not a runtime condition to degrade around: `ToolExecutionService` attaches this
|
|
3292
|
+
* port to every tool call it issues, so a missing one means this tool was invoked outside the
|
|
3293
|
+
* execution loop. Guessing an empty catalog there would report "nothing matched" for a search that
|
|
3294
|
+
* was never actually run.
|
|
3295
|
+
*/
|
|
3296
|
+
function requireCatalog(context) {
|
|
3297
|
+
const catalog = context?.deferredTools;
|
|
3298
|
+
if (!catalog) throw new Error(`${TOOL_SEARCH_NAME} requires the deferred-tool catalog, which the execution runtime injects; it was not present, so this tool was called outside the agent execution loop.`);
|
|
3299
|
+
return catalog;
|
|
3300
|
+
}
|
|
3301
|
+
/** Which schemas this call loads: the exact `names`, else the query's ranked matches. */
|
|
3302
|
+
function selectTools(args, catalog) {
|
|
3303
|
+
if (args.names !== void 0) return catalog.loadDeferredTools(args.names);
|
|
3304
|
+
if (args.query === void 0) throw new Error(`${TOOL_SEARCH_NAME} needs either "query" to search for tools or "names" to load exact ones.`);
|
|
3305
|
+
const matches = matchDeferredTools(catalog.listDeferredTools(), args.query, args.limit ?? 5);
|
|
3306
|
+
return catalog.loadDeferredTools(matches.map((schema) => schema.name));
|
|
3307
|
+
}
|
|
3308
|
+
/**
|
|
3309
|
+
* Create a `ToolSearch` tool instance — register it RESIDENT with the agent's tool registry.
|
|
3310
|
+
*
|
|
3311
|
+
* It must never itself be deferred: a search tool the model cannot see is a catalog with no way in,
|
|
3312
|
+
* which is the state the vendor's own "at least one tool must stay resident" invariant forbids.
|
|
3313
|
+
*/
|
|
3314
|
+
function createToolSearchTool(options = {}) {
|
|
3315
|
+
return createZodFunctionTool(TOOL_SEARCH_NAME, options.description ?? TOOL_SEARCH_DESCRIPTION, ToolSearchSchema, async (params, context) => {
|
|
3316
|
+
const output = {
|
|
3317
|
+
loaded: selectTools(params, requireCatalog(context)).map(({ name, description }) => ({
|
|
3318
|
+
name,
|
|
3319
|
+
description
|
|
3320
|
+
})),
|
|
3321
|
+
unavailableSources: []
|
|
3322
|
+
};
|
|
3323
|
+
const result = {
|
|
3324
|
+
success: true,
|
|
3325
|
+
output: JSON.stringify(output)
|
|
3326
|
+
};
|
|
3327
|
+
return JSON.stringify(result);
|
|
3328
|
+
});
|
|
3329
|
+
}
|
|
3330
|
+
/** `ToolSearch` tool instance — register with the Robota agent tools registry. */
|
|
3331
|
+
const toolSearchTool = createToolSearchTool();
|
|
3332
|
+
//#endregion
|
|
3333
|
+
exports.DEFAULT_OS_SANDBOX_SETTINGS = DEFAULT_OS_SANDBOX_SETTINGS;
|
|
3334
|
+
exports.DEFAULT_TOOL_SEARCH_LIMIT = DEFAULT_TOOL_SEARCH_LIMIT;
|
|
3335
|
+
exports.E2BSandboxClient = E2BSandboxClient;
|
|
3336
|
+
exports.GrepIsolationError = GrepIsolationError;
|
|
3337
|
+
exports.InMemorySandboxClient = InMemorySandboxClient;
|
|
3338
|
+
exports.OsSandboxClient = OsSandboxClient;
|
|
3339
|
+
exports.PageComputerDriver = PageComputerDriver;
|
|
3340
|
+
exports.REPO_MAP_INDEX_VERSION = REPO_MAP_INDEX_VERSION;
|
|
3341
|
+
exports.ReadByteLimitError = ReadByteLimitError;
|
|
3342
|
+
exports.ReadCancelledError = ReadCancelledError;
|
|
3343
|
+
exports.RepoMapRetrievalAdapter = RepoMapRetrievalAdapter;
|
|
3344
|
+
exports.TOOL_SEARCH_NAME = TOOL_SEARCH_NAME;
|
|
3345
|
+
exports.applyWorkspaceManifest = applyWorkspaceManifest;
|
|
3346
|
+
exports.askUserQuestionTool = askUserQuestionTool;
|
|
3347
|
+
exports.bubblewrapArguments = bubblewrapArguments;
|
|
3348
|
+
exports.buildRepoMapIndex = buildRepoMapIndex;
|
|
3349
|
+
exports.createAskUserQuestionTool = createAskUserQuestionTool;
|
|
3350
|
+
exports.createBashTool = createBashTool;
|
|
3351
|
+
exports.createBraveSearchProvider = createBraveSearchProvider;
|
|
3352
|
+
exports.createComputerActTool = createComputerActTool;
|
|
3353
|
+
exports.createComputerTool = createComputerTool;
|
|
3354
|
+
exports.createComputerViewTool = createComputerViewTool;
|
|
3355
|
+
exports.createEditTool = createEditTool;
|
|
3356
|
+
exports.createFunctionTool = createFunctionTool;
|
|
3357
|
+
exports.createGlobTool = createGlobTool;
|
|
3358
|
+
exports.createGrepTool = createGrepTool;
|
|
3359
|
+
exports.createReadTool = createReadTool;
|
|
3360
|
+
exports.createRetrievalTool = createRetrievalTool;
|
|
3361
|
+
exports.createShellTool = createShellTool;
|
|
3362
|
+
exports.createToolSearchTool = createToolSearchTool;
|
|
3363
|
+
exports.createWebFetchTool = createWebFetchTool;
|
|
3364
|
+
exports.createWebSearchTool = createWebSearchTool;
|
|
3365
|
+
exports.createWriteTool = createWriteTool;
|
|
3366
|
+
exports.createZodFunctionTool = createZodFunctionTool;
|
|
3367
|
+
exports.describeExecutionContainment = describeExecutionContainment;
|
|
3368
|
+
exports.deserializeRepoMapIndex = deserializeRepoMapIndex;
|
|
3369
|
+
exports.detectOsSandbox = detectOsSandbox;
|
|
3370
|
+
exports.matchDeferredTools = matchDeferredTools;
|
|
3371
|
+
exports.protectedWorkspaceEntries = protectedWorkspaceEntries;
|
|
3372
|
+
exports.routesFilesThroughSandbox = routesFilesThroughSandbox;
|
|
3373
|
+
exports.seatbeltProfile = seatbeltProfile;
|
|
3374
|
+
exports.serializeRepoMapIndex = serializeRepoMapIndex;
|
|
3375
|
+
exports.toolSearchTool = toolSearchTool;
|
|
3376
|
+
exports.updateRepoMapIndex = updateRepoMapIndex;
|
|
3377
|
+
exports.validateWorkspaceManifestPath = validateWorkspaceManifestPath;
|
|
3378
|
+
exports.webFetchTool = webFetchTool;
|
|
3379
|
+
exports.webSearchTool = webSearchTool;
|