bare-agent 0.5.0 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/package.json +1 -1
- package/src/loop.js +80 -0
- package/src/mcp-bridge.js +12 -12
- package/src/tools.js +2 -1
- package/tools/shell.js +286 -0
package/README.md
CHANGED
|
@@ -60,7 +60,7 @@ Every piece works alone — take what you need, ignore the rest.
|
|
|
60
60
|
|
|
61
61
|
| Component | What it does |
|
|
62
62
|
|---|---|
|
|
63
|
-
| **Loop** | Think → act → observe → repeat. Calls any LLM, executes your tools, loops until done. Throws on error by default. Returns estimated USD cost per run |
|
|
63
|
+
| **Loop** | Think → act → observe → repeat. Calls any LLM, executes your tools, loops until done. Throws on error by default. Returns estimated USD cost per run. Loop-level `policy` + `audit` gate every tool call (native, MCP, browsing, mobile, user-defined) through one hook, JSONL audit to disk |
|
|
64
64
|
| **Planner** | Break a goal into a step DAG via LLM. Built-in caching (`cacheTTL`) |
|
|
65
65
|
| **runPlan** | Execute steps in parallel waves. Dependency-aware, failure propagation, per-step retry |
|
|
66
66
|
| **Retry** | Exponential/linear backoff with jitter. Respects `err.retryable` |
|
|
@@ -74,7 +74,8 @@ Every piece works alone — take what you need, ignore the rest.
|
|
|
74
74
|
| **Errors** | Typed hierarchy — `ProviderError`, `ToolError`, `TimeoutError`, `MaxRoundsError`, `CircuitOpenError` |
|
|
75
75
|
| **Browsing** | Web navigation, clicking, typing, reading via `barebrowse` (17 tools). Two modes: library tools (inline snapshots, pass to Loop) or CLI session (disk-based snapshots, token-efficient for multi-step flows). Optional `assess` tool (privacy scan) when `wearehere` is installed |
|
|
76
76
|
| **Mobile** | Android + iOS device control via `baremobile`. Same two modes: library tools (`createMobileTools` — action tools auto-return snapshots) or CLI session (`baremobile` CLI — disk-based snapshots) |
|
|
77
|
-
| **
|
|
77
|
+
| **Shell** | Cross-platform `shell_read`, `shell_grep`, `shell_run` (argv, no shell), `shell_exec` (raw shell). Pure Node — no `grep`/`rg`/`findstr` dependency. Injection-proof `shell_run` for policy-gated use |
|
|
78
|
+
| **MCP Bridge** | Auto-discover MCP servers from IDE configs (Claude Code, Cursor, etc.), expose as bareagent tools. Static allow/deny via `.mcp-bridge.json`, `systemContext` for LLM awareness. Runtime policy lives in `Loop({ policy })` — one hook for MCP + native tools alike. Zero deps |
|
|
78
79
|
|
|
79
80
|
**Providers:** OpenAI-compatible (OpenAI, OpenRouter, Groq, vLLM, LM Studio), Anthropic, Ollama, CLIPipe (any CLI tool via stdin/stdout with real-time streaming), Fallback, or bring your own (one method: `generate`). All return the same shape — swap freely.
|
|
80
81
|
|
package/package.json
CHANGED
package/src/loop.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const fs = require('node:fs');
|
|
3
4
|
const { ToolError, MaxRoundsError } = require('./errors');
|
|
4
5
|
|
|
5
6
|
// Average pricing per 1K tokens (USD). Adjust these to match your provider's rates.
|
|
@@ -39,6 +40,8 @@ class Loop {
|
|
|
39
40
|
* @param {object} [options.retry] - Retry instance for backoff on failures.
|
|
40
41
|
* @param {object} [options.stream] - Stream instance for event emission.
|
|
41
42
|
* @param {object} [options.store] - Store instance for validate() health check.
|
|
43
|
+
* @param {Function} [options.policy] - Async (toolName, args) => true|false|string. Deny returns the string (or a generic message) to the LLM as tool result.
|
|
44
|
+
* @param {string} [options.audit] - File path for JSONL audit log. Each tool call appends one line: {ts, tool, args, decision, result|reason|error, durationMs}.
|
|
42
45
|
* @throws {Error} `[Loop] requires a provider` — when options.provider is missing.
|
|
43
46
|
*/
|
|
44
47
|
constructor(options = {}) {
|
|
@@ -54,8 +57,38 @@ class Loop {
|
|
|
54
57
|
this.onError = options.onError || null;
|
|
55
58
|
this.throwOnError = options.throwOnError !== undefined ? options.throwOnError : true;
|
|
56
59
|
this.store = options.store || null;
|
|
60
|
+
if (options.policy != null && typeof options.policy !== 'function') {
|
|
61
|
+
throw new Error('[Loop] options.policy must be a function (toolName, args) => true | false | string');
|
|
62
|
+
}
|
|
63
|
+
this.policy = options.policy || null;
|
|
64
|
+
this.audit = options.audit || null;
|
|
57
65
|
this._stopped = false;
|
|
58
66
|
this._history = []; // for chat() stateful mode
|
|
67
|
+
this._auditInFlight = new Set();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Append one JSONL record. Returns nothing (fire-and-forget for callers)
|
|
71
|
+
// but tracks the in-flight promise so `flush()` and the end of `run()` can await it.
|
|
72
|
+
_writeAudit(record) {
|
|
73
|
+
if (!this.audit) return;
|
|
74
|
+
let line;
|
|
75
|
+
try {
|
|
76
|
+
line = JSON.stringify(record) + '\n';
|
|
77
|
+
} catch (err) {
|
|
78
|
+
console.warn(`[Loop] audit serialize failed: ${err.message}`);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const p = fs.promises.appendFile(this.audit, line)
|
|
82
|
+
.catch(err => console.warn(`[Loop] audit write failed: ${err.message}`))
|
|
83
|
+
.finally(() => this._auditInFlight.delete(p));
|
|
84
|
+
this._auditInFlight.add(p);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Await any in-flight audit writes. Safe to call multiple times; resolves immediately
|
|
88
|
+
// when no writes are pending. Called automatically at the end of each `run()`.
|
|
89
|
+
async flush() {
|
|
90
|
+
if (this._auditInFlight.size === 0) return;
|
|
91
|
+
await Promise.all([...this._auditInFlight]);
|
|
59
92
|
}
|
|
60
93
|
|
|
61
94
|
/**
|
|
@@ -107,6 +140,7 @@ class Loop {
|
|
|
107
140
|
} catch (err) {
|
|
108
141
|
this.stream?.emit({ type: 'loop:error', data: { error: err.message, round } });
|
|
109
142
|
this.onError?.(err);
|
|
143
|
+
await this.flush();
|
|
110
144
|
if (this.throwOnError) throw err;
|
|
111
145
|
return { text: '', toolCalls: [], usage: lastUsage, cost: totalCost, error: err.message };
|
|
112
146
|
}
|
|
@@ -120,6 +154,7 @@ class Loop {
|
|
|
120
154
|
if (!result.toolCalls || result.toolCalls.length === 0) {
|
|
121
155
|
this.stream?.emit({ type: 'loop:text', data: { text: result.text } });
|
|
122
156
|
this.onText?.(result.text);
|
|
157
|
+
await this.flush();
|
|
123
158
|
this.stream?.emit({ type: 'loop:done', data: { text: result.text, usage: lastUsage, cost: totalCost } });
|
|
124
159
|
return { text: result.text, toolCalls: [], usage: lastUsage, cost: totalCost, error: null };
|
|
125
160
|
}
|
|
@@ -163,23 +198,68 @@ class Loop {
|
|
|
163
198
|
this.stream?.emit({ type: 'loop:tool_call', data: { tool: tc.name, args: tc.arguments } });
|
|
164
199
|
this.onToolCall?.(tc.name, tc.arguments);
|
|
165
200
|
|
|
201
|
+
// Policy check — runs before execute. Fail-safe: only verdict === true allows;
|
|
202
|
+
// anything else (false, string, undefined, object, throw) denies. A string verdict
|
|
203
|
+
// is used verbatim as the deny reason.
|
|
204
|
+
if (this.policy) {
|
|
205
|
+
let verdict;
|
|
206
|
+
try {
|
|
207
|
+
verdict = await this.policy(tc.name, tc.arguments);
|
|
208
|
+
} catch (err) {
|
|
209
|
+
verdict = `[Loop] policy error: ${err.message}`;
|
|
210
|
+
}
|
|
211
|
+
if (verdict !== true) {
|
|
212
|
+
const reason = typeof verdict === 'string'
|
|
213
|
+
? verdict
|
|
214
|
+
: `[Loop] Tool "${tc.name}" denied by policy`;
|
|
215
|
+
msgs.push({ role: 'tool', tool_call_id: tc.id, content: reason });
|
|
216
|
+
this.stream?.emit({ type: 'loop:tool_result', data: { tool: tc.name, denied: true, reason } });
|
|
217
|
+
this._writeAudit({
|
|
218
|
+
ts: new Date().toISOString(),
|
|
219
|
+
tool: tc.name,
|
|
220
|
+
args: tc.arguments,
|
|
221
|
+
decision: 'deny',
|
|
222
|
+
reason,
|
|
223
|
+
});
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const startedAt = Date.now();
|
|
166
229
|
try {
|
|
167
230
|
const execute = () => tool.execute(tc.arguments);
|
|
168
231
|
const toolResult = this.retry ? await this.retry.call(execute) : await execute();
|
|
169
232
|
const content = typeof toolResult === 'string' ? toolResult : JSON.stringify(toolResult);
|
|
170
233
|
msgs.push({ role: 'tool', tool_call_id: tc.id, content });
|
|
171
234
|
this.stream?.emit({ type: 'loop:tool_result', data: { tool: tc.name, result: content } });
|
|
235
|
+
this._writeAudit({
|
|
236
|
+
ts: new Date().toISOString(),
|
|
237
|
+
tool: tc.name,
|
|
238
|
+
args: tc.arguments,
|
|
239
|
+
decision: 'allow',
|
|
240
|
+
result: content,
|
|
241
|
+
durationMs: Date.now() - startedAt,
|
|
242
|
+
});
|
|
172
243
|
} catch (err) {
|
|
173
244
|
const toolErr = err instanceof ToolError ? err : new ToolError(err.message, { context: { tool: tc.name } });
|
|
174
245
|
const errMsg = `[Loop] Tool error: ${toolErr.message}`;
|
|
175
246
|
msgs.push({ role: 'tool', tool_call_id: tc.id, content: errMsg });
|
|
176
247
|
this.stream?.emit({ type: 'loop:tool_result', data: { tool: tc.name, error: errMsg } });
|
|
248
|
+
this._writeAudit({
|
|
249
|
+
ts: new Date().toISOString(),
|
|
250
|
+
tool: tc.name,
|
|
251
|
+
args: tc.arguments,
|
|
252
|
+
decision: 'allow',
|
|
253
|
+
error: toolErr.message,
|
|
254
|
+
durationMs: Date.now() - startedAt,
|
|
255
|
+
});
|
|
177
256
|
}
|
|
178
257
|
}
|
|
179
258
|
}
|
|
180
259
|
|
|
181
260
|
// maxRounds exceeded
|
|
182
261
|
const warning = `[Loop] ended after ${this.maxRounds} rounds without final response`;
|
|
262
|
+
await this.flush();
|
|
183
263
|
this.stream?.emit({ type: 'loop:done', data: { text: '', warning, cost: totalCost } });
|
|
184
264
|
if (this.throwOnError) throw new MaxRoundsError(warning);
|
|
185
265
|
return { text: '', toolCalls: [], usage: lastUsage, cost: totalCost, error: warning };
|
package/src/mcp-bridge.js
CHANGED
|
@@ -194,21 +194,15 @@ function unwrapContent(content) {
|
|
|
194
194
|
|
|
195
195
|
// --- Tool wrapping ---
|
|
196
196
|
|
|
197
|
-
|
|
197
|
+
// Runtime arg-dependent policy has moved to Loop-level (new Loop({ policy })).
|
|
198
|
+
// mcp-bridge retains only the static .mcp-bridge.json allow/deny filter below —
|
|
199
|
+
// that decides which tools are exposed to the Loop in the first place.
|
|
200
|
+
function wrapTools(serverName, mcpTools, rpc) {
|
|
198
201
|
return mcpTools.map(t => ({
|
|
199
202
|
name: `${serverName}_${t.name}`,
|
|
200
203
|
description: t.description || '',
|
|
201
204
|
parameters: t.inputSchema || { type: 'object', properties: {} },
|
|
202
205
|
execute: async (args) => {
|
|
203
|
-
if (policy) {
|
|
204
|
-
const verdict = await policy(serverName, t.name, args);
|
|
205
|
-
if (verdict === false || typeof verdict === 'string') {
|
|
206
|
-
const reason = typeof verdict === 'string'
|
|
207
|
-
? verdict
|
|
208
|
-
: `[GOVERNANCE] Tool "${serverName}_${t.name}" is not permitted by policy. Do not retry this tool.`;
|
|
209
|
-
throw new ToolError(reason, { context: { server: serverName, tool: t.name } });
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
206
|
const result = await rpc('tools/call', { name: t.name, arguments: args });
|
|
213
207
|
if (result.isError) {
|
|
214
208
|
throw new ToolError(unwrapContent(result.content) || 'MCP tool error', {
|
|
@@ -327,11 +321,17 @@ function buildSystemContext(servers, tools, denied) {
|
|
|
327
321
|
* @param {string[]} [opts.configPaths] - IDE config paths for discovery.
|
|
328
322
|
* @param {string[]} [opts.servers] - Limit to these server names.
|
|
329
323
|
* @param {number} [opts.timeout=15000] - Per-server init timeout in ms.
|
|
330
|
-
* @param {Function} [opts.policy] - Async function(serverName, toolName, args) for runtime arg-dependent checks.
|
|
331
324
|
* @param {boolean} [opts.refresh=false] - Force re-discovery regardless of TTL.
|
|
332
325
|
* @returns {Promise<{tools: Array, servers: string[], systemContext: string, denied: Array, close: Function}>}
|
|
333
326
|
*/
|
|
334
327
|
async function createMCPBridge(opts = {}) {
|
|
328
|
+
if ('policy' in opts) {
|
|
329
|
+
throw new Error(
|
|
330
|
+
'[MCP Bridge] The `policy` option was removed in v0.6.0. Runtime arg-dependent policy is now Loop-level: ' +
|
|
331
|
+
'pass `policy` to `new Loop({ policy })` instead — it gates MCP tools identically to native tools. ' +
|
|
332
|
+
'The static allow/deny filter in .mcp-bridge.json is unchanged.'
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
335
|
const bridgePath = opts.bridgePath || DEFAULT_BRIDGE_PATH();
|
|
336
336
|
const timeout = opts.timeout || 15000;
|
|
337
337
|
|
|
@@ -411,7 +411,7 @@ async function createMCPBridge(opts = {}) {
|
|
|
411
411
|
|
|
412
412
|
// Only wrap tools that are allowed in config
|
|
413
413
|
const allowed = mcpTools.filter(t => allowedToolNames.includes(t.name));
|
|
414
|
-
const wrapped = wrapTools(name, allowed, client.rpc
|
|
414
|
+
const wrapped = wrapTools(name, allowed, client.rpc);
|
|
415
415
|
|
|
416
416
|
tools.push(...wrapped);
|
|
417
417
|
children.push(client.child);
|
package/src/tools.js
CHANGED
|
@@ -2,5 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
const { createBrowsingTools } = require('../tools/browse');
|
|
4
4
|
const { createMobileTools } = require('../tools/mobile');
|
|
5
|
+
const { createShellTools } = require('../tools/shell');
|
|
5
6
|
|
|
6
|
-
module.exports = { createBrowsingTools, createMobileTools };
|
|
7
|
+
module.exports = { createBrowsingTools, createMobileTools, createShellTools };
|
package/tools/shell.js
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Pure-Node shell tools — cross-platform (linux, macOS, Windows), no external binaries.
|
|
5
|
+
*
|
|
6
|
+
* Three primitives:
|
|
7
|
+
* shell_read — read a file or list a directory
|
|
8
|
+
* shell_grep — regex search across files (JS regex, no grep/rg/findstr)
|
|
9
|
+
* shell_exec — run a shell command with timeout + max buffer
|
|
10
|
+
*
|
|
11
|
+
* All three run through Loop's policy hook when wired via `new Loop({ policy })`.
|
|
12
|
+
* Library ships zero baked-in allowlist — gating is the agent author's responsibility.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const fs = require('node:fs/promises');
|
|
16
|
+
const path = require('node:path');
|
|
17
|
+
const { exec, execFile } = require('node:child_process');
|
|
18
|
+
|
|
19
|
+
const DEFAULT_READ_MAX_BYTES = 256 * 1024; // 256 KB
|
|
20
|
+
const DEFAULT_GREP_MAX_MATCHES = 200;
|
|
21
|
+
const DEFAULT_EXEC_TIMEOUT_MS = 30_000;
|
|
22
|
+
const DEFAULT_EXEC_MAX_BUFFER = 1024 * 1024; // 1 MB
|
|
23
|
+
|
|
24
|
+
function expandHome(p) {
|
|
25
|
+
if (!p) return p;
|
|
26
|
+
if (p.startsWith('~/') || p === '~') {
|
|
27
|
+
const home = process.env.HOME || process.env.USERPROFILE || '';
|
|
28
|
+
return path.join(home, p.slice(1));
|
|
29
|
+
}
|
|
30
|
+
return p;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function readEntry(rawPath, maxBytes) {
|
|
34
|
+
const resolved = path.resolve(expandHome(rawPath));
|
|
35
|
+
const stat = await fs.stat(resolved);
|
|
36
|
+
if (stat.isDirectory()) {
|
|
37
|
+
const entries = await fs.readdir(resolved, { withFileTypes: true });
|
|
38
|
+
const lines = entries.map(e => {
|
|
39
|
+
const kind = e.isDirectory() ? 'dir' : e.isSymbolicLink() ? 'link' : 'file';
|
|
40
|
+
return `${kind}\t${e.name}`;
|
|
41
|
+
});
|
|
42
|
+
return `dir ${resolved}\n${lines.join('\n')}`;
|
|
43
|
+
}
|
|
44
|
+
const cap = maxBytes || DEFAULT_READ_MAX_BYTES;
|
|
45
|
+
if (stat.size > cap) {
|
|
46
|
+
const fh = await fs.open(resolved, 'r');
|
|
47
|
+
try {
|
|
48
|
+
const buf = Buffer.alloc(cap);
|
|
49
|
+
await fh.read(buf, 0, cap, 0);
|
|
50
|
+
return buf.toString('utf8') + `\n\n[truncated: ${stat.size - cap} more bytes not shown]`;
|
|
51
|
+
} finally {
|
|
52
|
+
await fh.close();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return fs.readFile(resolved, 'utf8');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Probe the first 1KB for NUL bytes to skip binary files in grep walks.
|
|
59
|
+
async function isProbablyText(filePath) {
|
|
60
|
+
try {
|
|
61
|
+
const fh = await fs.open(filePath, 'r');
|
|
62
|
+
try {
|
|
63
|
+
const buf = Buffer.alloc(1024);
|
|
64
|
+
const { bytesRead } = await fh.read(buf, 0, 1024, 0);
|
|
65
|
+
for (let i = 0; i < bytesRead; i++) {
|
|
66
|
+
if (buf[i] === 0) return false;
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
} finally {
|
|
70
|
+
await fh.close();
|
|
71
|
+
}
|
|
72
|
+
} catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function* walk(dir, recursive) {
|
|
78
|
+
let entries;
|
|
79
|
+
try {
|
|
80
|
+
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
81
|
+
} catch {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
for (const entry of entries) {
|
|
85
|
+
const full = path.join(dir, entry.name);
|
|
86
|
+
if (entry.isDirectory()) {
|
|
87
|
+
if (recursive) yield* walk(full, true);
|
|
88
|
+
} else if (entry.isFile()) {
|
|
89
|
+
yield full;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function grepPath({ pattern, path: rawPath, recursive = true, maxMatches, flags = 'i' }) {
|
|
95
|
+
const resolved = path.resolve(expandHome(rawPath));
|
|
96
|
+
const cap = maxMatches || DEFAULT_GREP_MAX_MATCHES;
|
|
97
|
+
let re;
|
|
98
|
+
try {
|
|
99
|
+
re = new RegExp(pattern, flags);
|
|
100
|
+
} catch (err) {
|
|
101
|
+
throw new Error(`shell_grep: invalid regex — ${err.message}`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const hits = [];
|
|
105
|
+
const stat = await fs.stat(resolved).catch(() => null);
|
|
106
|
+
if (!stat) throw new Error(`shell_grep: path not found — ${rawPath}`);
|
|
107
|
+
|
|
108
|
+
const files = [];
|
|
109
|
+
if (stat.isFile()) {
|
|
110
|
+
files.push(resolved);
|
|
111
|
+
} else if (stat.isDirectory()) {
|
|
112
|
+
for await (const f of walk(resolved, recursive)) files.push(f);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
for (const file of files) {
|
|
116
|
+
if (hits.length >= cap) break;
|
|
117
|
+
if (!(await isProbablyText(file))) continue;
|
|
118
|
+
let content;
|
|
119
|
+
try {
|
|
120
|
+
content = await fs.readFile(file, 'utf8');
|
|
121
|
+
} catch {
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const lines = content.split(/\r?\n/);
|
|
125
|
+
for (let i = 0; i < lines.length; i++) {
|
|
126
|
+
if (hits.length >= cap) break;
|
|
127
|
+
if (re.test(lines[i])) {
|
|
128
|
+
hits.push({ file, line: i + 1, text: lines[i].slice(0, 500) });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const truncated = hits.length >= cap;
|
|
134
|
+
return { hits, truncated, fileCount: files.length };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function runArgv({ argv, cwd, timeout, maxBuffer, env }) {
|
|
138
|
+
if (!Array.isArray(argv) || argv.length === 0 || typeof argv[0] !== 'string') {
|
|
139
|
+
return Promise.reject(new Error('shell_run: argv must be a non-empty array of strings, starting with the command'));
|
|
140
|
+
}
|
|
141
|
+
const [file, ...args] = argv;
|
|
142
|
+
return new Promise((resolve) => {
|
|
143
|
+
execFile(
|
|
144
|
+
file,
|
|
145
|
+
args,
|
|
146
|
+
{
|
|
147
|
+
cwd: cwd ? expandHome(cwd) : undefined,
|
|
148
|
+
timeout: timeout || DEFAULT_EXEC_TIMEOUT_MS,
|
|
149
|
+
maxBuffer: maxBuffer || DEFAULT_EXEC_MAX_BUFFER,
|
|
150
|
+
env: env ? { ...process.env, ...env } : process.env,
|
|
151
|
+
windowsHide: true,
|
|
152
|
+
shell: false,
|
|
153
|
+
},
|
|
154
|
+
(err, stdout, stderr) => {
|
|
155
|
+
if (err) {
|
|
156
|
+
if (err.killed) {
|
|
157
|
+
resolve({ stdout: stdout || '', stderr: stderr || '', code: null, timedOut: true });
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (err.code === 'ENOENT') {
|
|
161
|
+
resolve({ stdout: '', stderr: `shell_run: command not found: ${file}`, code: null, timedOut: false });
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
resolve({
|
|
165
|
+
stdout: stdout || '',
|
|
166
|
+
stderr: stderr || '',
|
|
167
|
+
code: typeof err.code === 'number' ? err.code : null,
|
|
168
|
+
timedOut: false,
|
|
169
|
+
});
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
resolve({ stdout: stdout || '', stderr: stderr || '', code: 0, timedOut: false });
|
|
173
|
+
}
|
|
174
|
+
);
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function execCommand({ command, cwd, timeout, maxBuffer, env }) {
|
|
179
|
+
return new Promise((resolve) => {
|
|
180
|
+
exec(
|
|
181
|
+
command,
|
|
182
|
+
{
|
|
183
|
+
cwd: cwd ? expandHome(cwd) : undefined,
|
|
184
|
+
timeout: timeout || DEFAULT_EXEC_TIMEOUT_MS,
|
|
185
|
+
maxBuffer: maxBuffer || DEFAULT_EXEC_MAX_BUFFER,
|
|
186
|
+
env: env ? { ...process.env, ...env } : process.env,
|
|
187
|
+
windowsHide: true,
|
|
188
|
+
},
|
|
189
|
+
(err, stdout, stderr) => {
|
|
190
|
+
if (err) {
|
|
191
|
+
if (err.killed) {
|
|
192
|
+
resolve({ stdout: stdout || '', stderr: stderr || '', code: null, timedOut: true });
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
resolve({
|
|
196
|
+
stdout: stdout || '',
|
|
197
|
+
stderr: stderr || '',
|
|
198
|
+
code: typeof err.code === 'number' ? err.code : null,
|
|
199
|
+
timedOut: false,
|
|
200
|
+
});
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
resolve({ stdout: stdout || '', stderr: stderr || '', code: 0, timedOut: false });
|
|
204
|
+
}
|
|
205
|
+
);
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Create the three shell tools. No options — configuration is per-call via tool args,
|
|
211
|
+
* gating is the caller's responsibility via `new Loop({ policy })`.
|
|
212
|
+
*
|
|
213
|
+
* @returns {{tools: Array}}
|
|
214
|
+
*/
|
|
215
|
+
function createShellTools() {
|
|
216
|
+
const tools = [
|
|
217
|
+
{
|
|
218
|
+
name: 'shell_read',
|
|
219
|
+
description: 'Read a file or list a directory. Returns file contents (truncated at 256KB) or a tab-separated directory listing.',
|
|
220
|
+
parameters: {
|
|
221
|
+
type: 'object',
|
|
222
|
+
properties: {
|
|
223
|
+
path: { type: 'string', description: 'File or directory path. ~ expands to home.' },
|
|
224
|
+
maxBytes: { type: 'integer', description: 'Optional cap for file reads (default 262144).' },
|
|
225
|
+
},
|
|
226
|
+
required: ['path'],
|
|
227
|
+
},
|
|
228
|
+
execute: async ({ path: p, maxBytes }) => readEntry(p, maxBytes),
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
name: 'shell_grep',
|
|
232
|
+
description: 'Search for a JavaScript regex pattern across files. Skips binary files. Returns matching lines with file paths and line numbers.',
|
|
233
|
+
parameters: {
|
|
234
|
+
type: 'object',
|
|
235
|
+
properties: {
|
|
236
|
+
pattern: { type: 'string', description: 'JavaScript regex (without surrounding slashes).' },
|
|
237
|
+
path: { type: 'string', description: 'File or directory to search. ~ expands to home.' },
|
|
238
|
+
recursive: { type: 'boolean', description: 'Recurse into subdirectories (default true).' },
|
|
239
|
+
maxMatches: { type: 'integer', description: 'Stop after this many hits (default 200).' },
|
|
240
|
+
flags: { type: 'string', description: 'Regex flags, e.g. "i" or "gim" (default "i").' },
|
|
241
|
+
},
|
|
242
|
+
required: ['pattern', 'path'],
|
|
243
|
+
},
|
|
244
|
+
execute: async (args) => grepPath(args),
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
name: 'shell_run',
|
|
248
|
+
description: 'Run a command with an argv array (no shell, no interpolation) and return {stdout, stderr, code, timedOut}. Use this when a policy allowlist needs to match on argv[0] — no shell metacharacter injection is possible. Default timeout 30s, max output 1MB.',
|
|
249
|
+
parameters: {
|
|
250
|
+
type: 'object',
|
|
251
|
+
properties: {
|
|
252
|
+
argv: {
|
|
253
|
+
type: 'array',
|
|
254
|
+
items: { type: 'string' },
|
|
255
|
+
description: 'Non-empty array of strings: argv[0] is the command, argv[1..] are its arguments. Spawned via child_process.execFile (shell: false).',
|
|
256
|
+
},
|
|
257
|
+
cwd: { type: 'string', description: 'Working directory. ~ expands to home.' },
|
|
258
|
+
timeout: { type: 'integer', description: 'Kill after this many ms (default 30000).' },
|
|
259
|
+
maxBuffer: { type: 'integer', description: 'Max stdout/stderr bytes (default 1048576).' },
|
|
260
|
+
env: { type: 'object', description: 'Additional env vars merged over process.env.' },
|
|
261
|
+
},
|
|
262
|
+
required: ['argv'],
|
|
263
|
+
},
|
|
264
|
+
execute: async (args) => runArgv(args),
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
name: 'shell_exec',
|
|
268
|
+
description: 'Run a raw shell command string via /bin/sh -c (or cmd.exe) and return {stdout, stderr, code, timedOut}. SECURITY: shell metacharacters (;, &&, |, `, $(), etc.) are interpreted — a naive base-command allowlist like `command.split(/\\s+/)[0]` is bypassable via "ls;rm -rf". Prefer shell_run for policy-gated use cases. Default timeout 30s, max output 1MB.',
|
|
269
|
+
parameters: {
|
|
270
|
+
type: 'object',
|
|
271
|
+
properties: {
|
|
272
|
+
command: { type: 'string', description: 'Raw shell command string. Goes through the system shell.' },
|
|
273
|
+
cwd: { type: 'string', description: 'Working directory. ~ expands to home.' },
|
|
274
|
+
timeout: { type: 'integer', description: 'Kill after this many ms (default 30000).' },
|
|
275
|
+
maxBuffer: { type: 'integer', description: 'Max stdout/stderr bytes (default 1048576).' },
|
|
276
|
+
env: { type: 'object', description: 'Additional env vars merged over process.env.' },
|
|
277
|
+
},
|
|
278
|
+
required: ['command'],
|
|
279
|
+
},
|
|
280
|
+
execute: async (args) => execCommand(args),
|
|
281
|
+
},
|
|
282
|
+
];
|
|
283
|
+
return { tools };
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
module.exports = { createShellTools };
|