@burtson-labs/agent-core 1.6.103 → 1.6.104
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/dist/tools/core-tools.d.ts +60 -4
- package/dist/tools/core-tools.d.ts.map +1 -1
- package/dist/tools/core-tools.js +361 -64
- package/dist/tools/core-tools.js.map +1 -1
- package/dist/tools/loop/editClaims.d.ts +30 -0
- package/dist/tools/loop/editClaims.d.ts.map +1 -0
- package/dist/tools/loop/editClaims.js +183 -0
- package/dist/tools/loop/editClaims.js.map +1 -0
- package/dist/tools/loop/finalAnswerNudges.d.ts +9 -0
- package/dist/tools/loop/finalAnswerNudges.d.ts.map +1 -1
- package/dist/tools/loop/finalAnswerNudges.js +46 -3
- package/dist/tools/loop/finalAnswerNudges.js.map +1 -1
- package/dist/tools/loop/renameLeftovers.d.ts +34 -0
- package/dist/tools/loop/renameLeftovers.d.ts.map +1 -0
- package/dist/tools/loop/renameLeftovers.js +124 -0
- package/dist/tools/loop/renameLeftovers.js.map +1 -0
- package/dist/tools/tool-types.d.ts +15 -0
- package/dist/tools/tool-types.d.ts.map +1 -1
- package/dist/tools/tool-use-loop.d.ts +22 -0
- package/dist/tools/tool-use-loop.d.ts.map +1 -1
- package/dist/tools/tool-use-loop.js +90 -20
- package/dist/tools/tool-use-loop.js.map +1 -1
- package/package.json +1 -1
package/dist/tools/core-tools.js
CHANGED
|
@@ -6,14 +6,22 @@
|
|
|
6
6
|
* dependency on Node.js APIs, VS Code, or any specific host.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.createSkillTool = exports.watchCommandTool = exports.runCommandTool = exports.searchCodeTool = exports.findDirectoryTool = exports.lsTool = exports.listFilesTool = exports.applyPatchTool = exports.replaceRangeTool = exports.applyEditTool = exports.deleteFileTool = exports.writeFileTool = exports.readFileTool = void 0;
|
|
9
|
+
exports.createSkillTool = exports.watchCommandTool = exports.runCommandTool = exports.searchCodeTool = exports.findDirectoryTool = exports.lsTool = exports.listFilesTool = exports.applyPatchTool = exports.replaceRangeTool = exports.applyEditTool = exports.deleteFileTool = exports.writeFileTool = exports.readFileTool = exports.BUILTIN_COMMAND_GROUPS = exports.FORBIDDEN_COMMANDS = void 0;
|
|
10
10
|
exports.decodeOverEscaped = decodeOverEscaped;
|
|
11
11
|
exports.addFileLines = addFileLines;
|
|
12
12
|
exports.resolvePatchHunk = resolvePatchHunk;
|
|
13
13
|
exports.reindentToFile = reindentToFile;
|
|
14
|
+
exports.leadingCommentBlock = leadingCommentBlock;
|
|
15
|
+
exports.headerCommentRemovedNote = headerCommentRemovedNote;
|
|
14
16
|
exports.workspaceDisplayPath = workspaceDisplayPath;
|
|
15
17
|
exports.noMatchHint = noMatchHint;
|
|
18
|
+
exports.commandBaseName = commandBaseName;
|
|
19
|
+
exports.isBuiltinCommand = isBuiltinCommand;
|
|
20
|
+
exports.forbiddenCommandReason = forbiddenCommandReason;
|
|
21
|
+
exports.shellOperatorRefusal = shellOperatorRefusal;
|
|
16
22
|
exports.runCommandRefusal = runCommandRefusal;
|
|
23
|
+
exports.parseOutputFilters = parseOutputFilters;
|
|
24
|
+
exports.applyOutputFilters = applyOutputFilters;
|
|
17
25
|
exports.runCommandFailureHint = runCommandFailureHint;
|
|
18
26
|
exports.toSkillId = toSkillId;
|
|
19
27
|
exports.buildSkillMarkdown = buildSkillMarkdown;
|
|
@@ -358,6 +366,15 @@ const writeFileTool = {
|
|
|
358
366
|
}
|
|
359
367
|
}
|
|
360
368
|
}
|
|
369
|
+
// An overwrite that drops the existing header comment is reported
|
|
370
|
+
// below (headerCommentRemovedNote); a new file has nothing to lose.
|
|
371
|
+
let existing;
|
|
372
|
+
try {
|
|
373
|
+
existing = await ctx.readFile(absPath);
|
|
374
|
+
}
|
|
375
|
+
catch {
|
|
376
|
+
existing = undefined;
|
|
377
|
+
}
|
|
361
378
|
await ctx.writeFile(absPath, toWrite);
|
|
362
379
|
// The model authored every byte it just wrote, so a follow-up
|
|
363
380
|
// apply_edit/replace_range on this file is not a blind edit. Without
|
|
@@ -370,7 +387,8 @@ const writeFileTool = {
|
|
|
370
387
|
const baseMessage = `Wrote ${lineCount} lines to ${relPath}. File saved. Do not restate the file contents — the user can see the diff. Move on to the next pending task or reply with a brief summary if the work is complete.`;
|
|
371
388
|
const validationWarning = validatePostWrite(absPath, toWrite);
|
|
372
389
|
const postEditCheck = await (0, post_edit_checks_1.runPostEditTypeCheck)(absPath, ctx).catch(() => ({ newErrorCount: 0, warning: undefined }));
|
|
373
|
-
const
|
|
390
|
+
const headerNote = existing !== undefined ? headerCommentRemovedNote(existing, toWrite, relPath) : undefined;
|
|
391
|
+
const trailers = [escapeNote, headerNote, validationWarning, postEditCheck.warning].filter(Boolean).join('\n\n');
|
|
374
392
|
return {
|
|
375
393
|
output: trailers ? `${baseMessage}\n\n${trailers}` : baseMessage
|
|
376
394
|
};
|
|
@@ -876,7 +894,8 @@ const applyEditTool = {
|
|
|
876
894
|
// call to 38s+ and eventually tripped a 504.
|
|
877
895
|
const baseMessage = `Replaced ${matches} in ${relPath} (${deltaLabel}). File saved. Do not restate the file contents — the user can see the diff. Do not re-read this file just to make another edit — you already have the structure in context. Move on to the next pending task or reply with a brief summary if the work is complete.`;
|
|
878
896
|
const postEditCheck = await (0, post_edit_checks_1.runPostEditTypeCheck)(absPath, ctx).catch(() => ({ newErrorCount: 0, warning: undefined }));
|
|
879
|
-
const
|
|
897
|
+
const headerNote = headerCommentRemovedNote(before, after, relPath);
|
|
898
|
+
const trailers = [escapeNote, headerNote, validationWarning, postEditCheck.warning].filter(Boolean).join('\n\n');
|
|
880
899
|
return {
|
|
881
900
|
output: trailers ? `${baseMessage}\n\n${trailers}` : baseMessage
|
|
882
901
|
};
|
|
@@ -1060,7 +1079,8 @@ const replaceRangeTool = {
|
|
|
1060
1079
|
: undefined;
|
|
1061
1080
|
const validationWarning = validatePostWrite(absPath, after);
|
|
1062
1081
|
const postEditCheck = await (0, post_edit_checks_1.runPostEditTypeCheck)(absPath, ctx).catch(() => ({ newErrorCount: 0, warning: undefined }));
|
|
1063
|
-
const
|
|
1082
|
+
const headerNote = headerCommentRemovedNote(before, after, relPath);
|
|
1083
|
+
const trailers = [shiftNote, escapeNote, hashWarning, headerNote, validationWarning, postEditCheck.warning].filter(Boolean).join('\n\n');
|
|
1064
1084
|
return {
|
|
1065
1085
|
output: trailers ? `${baseMessage}\n\n${trailers}` : baseMessage
|
|
1066
1086
|
};
|
|
@@ -1617,6 +1637,91 @@ function maybeParseJsonArrayArgs(argsString) {
|
|
|
1617
1637
|
}
|
|
1618
1638
|
return null;
|
|
1619
1639
|
}
|
|
1640
|
+
/**
|
|
1641
|
+
* The file's leading comment block (license, module doc, shebang): the run
|
|
1642
|
+
* of blank and comment lines before the first line of code. Block comments
|
|
1643
|
+
* (`/* … *\/`, `<!-- … -->`, Python `"""` docstrings) count whole. Markdown,
|
|
1644
|
+
* plain text, JSON and the like have no comment syntax here, so they return
|
|
1645
|
+
* nothing (`#` is a heading in Markdown).
|
|
1646
|
+
*/
|
|
1647
|
+
function leadingCommentBlock(text, path = '') {
|
|
1648
|
+
if (/\.(md|markdown|mdx|txt|rst|adoc|json|jsonc|json5|csv|tsv|lock)$/i.test(path)) {
|
|
1649
|
+
return [];
|
|
1650
|
+
}
|
|
1651
|
+
const lines = text.replace(/\r\n/g, '\n').split('\n');
|
|
1652
|
+
const out = [];
|
|
1653
|
+
let open = null;
|
|
1654
|
+
for (const line of lines) {
|
|
1655
|
+
const t = line.trim();
|
|
1656
|
+
if (open) {
|
|
1657
|
+
out.push(line);
|
|
1658
|
+
if (open.test(t)) {
|
|
1659
|
+
open = null;
|
|
1660
|
+
}
|
|
1661
|
+
continue;
|
|
1662
|
+
}
|
|
1663
|
+
if (t === '') {
|
|
1664
|
+
out.push(line);
|
|
1665
|
+
continue;
|
|
1666
|
+
}
|
|
1667
|
+
if (/^(\/\/|#|--|;|%|\*)/.test(t)) {
|
|
1668
|
+
out.push(line);
|
|
1669
|
+
continue;
|
|
1670
|
+
}
|
|
1671
|
+
if (t.startsWith('/*')) {
|
|
1672
|
+
out.push(line);
|
|
1673
|
+
if (!t.includes('*/')) {
|
|
1674
|
+
open = /\*\//;
|
|
1675
|
+
}
|
|
1676
|
+
continue;
|
|
1677
|
+
}
|
|
1678
|
+
if (t.startsWith('<!--')) {
|
|
1679
|
+
out.push(line);
|
|
1680
|
+
if (!t.includes('-->')) {
|
|
1681
|
+
open = /-->/;
|
|
1682
|
+
}
|
|
1683
|
+
continue;
|
|
1684
|
+
}
|
|
1685
|
+
if (/^("""|''')/.test(t)) {
|
|
1686
|
+
out.push(line);
|
|
1687
|
+
const q = t.slice(0, 3);
|
|
1688
|
+
if (!(t.length > 3 && t.endsWith(q))) {
|
|
1689
|
+
open = new RegExp(q.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
|
|
1690
|
+
}
|
|
1691
|
+
continue;
|
|
1692
|
+
}
|
|
1693
|
+
break;
|
|
1694
|
+
}
|
|
1695
|
+
// Only the comment lines count; trailing blanks belong to the code below.
|
|
1696
|
+
while (out.length > 0 && out[out.length - 1].trim() === '') {
|
|
1697
|
+
out.pop();
|
|
1698
|
+
}
|
|
1699
|
+
return out.filter((l) => l.trim() !== '');
|
|
1700
|
+
}
|
|
1701
|
+
/**
|
|
1702
|
+
* A note when an edit threw away the file's header comment. bandit-core:31b
|
|
1703
|
+
* (2026-11-28 bench, ag-holdout-09 and go-locate-ninja-comment) replaced a
|
|
1704
|
+
* file's license/doc header while editing the code right under it; the task
|
|
1705
|
+
* never mentioned the header and nothing told the model. The edit is not
|
|
1706
|
+
* blocked — a task can legitimately change a header — but the model is told
|
|
1707
|
+
* exactly what disappeared so it can put it back in the same turn.
|
|
1708
|
+
*/
|
|
1709
|
+
function headerCommentRemovedNote(before, after, path) {
|
|
1710
|
+
const was = leadingCommentBlock(before, path);
|
|
1711
|
+
if (was.length < 2) {
|
|
1712
|
+
return undefined;
|
|
1713
|
+
}
|
|
1714
|
+
const now = leadingCommentBlock(after, path);
|
|
1715
|
+
// Lines still present anywhere at the top, whatever the edit did to spacing.
|
|
1716
|
+
const nowSet = new Set(now.map((l) => l.trim()));
|
|
1717
|
+
const kept = was.filter((l) => nowSet.has(l.trim())).length;
|
|
1718
|
+
const removed = was.length - kept;
|
|
1719
|
+
if (removed === 0 || removed * 2 < was.length) {
|
|
1720
|
+
return undefined;
|
|
1721
|
+
}
|
|
1722
|
+
const preview = was.slice(0, 2).map((l) => l.trim().slice(0, 80)).join(' / ');
|
|
1723
|
+
return `NOTE: this edit removed ${removed} of the ${was.length} lines of the file's leading comment block ("${preview}${was.length > 2 ? ' / …' : ''}"). If the task did not ask you to change that header, restore it now (re-read the top of the file and put the comment back above the code) before moving on.`;
|
|
1724
|
+
}
|
|
1620
1725
|
function introducedNewErrors(before, after) {
|
|
1621
1726
|
const afterText = after ?? '';
|
|
1622
1727
|
if (!afterText.trim()) {
|
|
@@ -2209,20 +2314,30 @@ const searchCodeTool = {
|
|
|
2209
2314
|
};
|
|
2210
2315
|
exports.searchCodeTool = searchCodeTool;
|
|
2211
2316
|
// ── run_command ────────────────────────────────────────────────────────────────
|
|
2212
|
-
/**
|
|
2213
|
-
* Grouped by ecosystem so additions are obvious. Curation rules:
|
|
2317
|
+
/** The programs run_command runs without the host vouching for them: the
|
|
2318
|
+
* BUILT-IN set. Grouped by ecosystem so additions are obvious. Curation rules:
|
|
2214
2319
|
* - Build/test/inspect tools: allow.
|
|
2215
|
-
* - Shell interpreters (bash/sh/zsh/pwsh):
|
|
2216
|
-
* blast radius for a single command-as-skill style execution
|
|
2320
|
+
* - Shell interpreters (bash/sh/zsh/pwsh): FORBIDDEN (see FORBIDDEN_COMMANDS)
|
|
2321
|
+
* — too broad a blast radius for a single command-as-skill style execution,
|
|
2322
|
+
* and no approval admits them: the user runs a shell line themselves with
|
|
2323
|
+
* the `!` composer prefix.
|
|
2217
2324
|
* - HTTP clients (curl/wget): allow. Skills that fetch remote data
|
|
2218
2325
|
* (status checks, webhooks, REST diagnostics) need them. The
|
|
2219
2326
|
* per-primary permission gate still prompts the user and the
|
|
2220
2327
|
* web_fetch tool remains the preferred path for content
|
|
2221
2328
|
* retrieval, but blocking curl outright was forcing skill
|
|
2222
2329
|
* authors to shell out via subprocess hacks anyway.
|
|
2223
|
-
* -
|
|
2224
|
-
*
|
|
2225
|
-
*
|
|
2330
|
+
* - Research computing (R, Julia, Slurm): allow. The 2026-11-28 research
|
|
2331
|
+
* benchmark had every model fail the R tasks, inside the no-network
|
|
2332
|
+
* sandbox, because `Rscript` was refused and the refusal told a headless
|
|
2333
|
+
* run to "type !Rscript yourself".
|
|
2334
|
+
* - Anything else (ssh, rsync, ffmpeg, a project's own binary …) is not
|
|
2335
|
+
* built in but is not forbidden either: the HOST admits it through
|
|
2336
|
+
* `ctx.commandAllowed` when the user approved the call on a permission
|
|
2337
|
+
* card, allowed the program for the session or wrote a
|
|
2338
|
+
* `run_command:<program>*` rule — or when the command runs in a container
|
|
2339
|
+
* the host treats as the boundary. Before 2026-11-29 this list was the
|
|
2340
|
+
* only check, so an approved `ssh` still failed here.
|
|
2226
2341
|
*/
|
|
2227
2342
|
const ALLOWED_COMMANDS = new Set([
|
|
2228
2343
|
// Node / JS ecosystem
|
|
@@ -2231,6 +2346,11 @@ const ALLOWED_COMMANDS = new Set([
|
|
|
2231
2346
|
'jest', 'vitest', 'mocha', 'playwright',
|
|
2232
2347
|
// Python
|
|
2233
2348
|
'python', 'python3', 'pip', 'pip3', 'poetry', 'uv', 'pytest', 'ruff', 'mypy', 'black',
|
|
2349
|
+
// Research computing: R and Julia interpreters, Slurm job control
|
|
2350
|
+
// (sbatch/srun submit, squeue/sacct/sinfo read, scancel/scontrol manage
|
|
2351
|
+
// the user's own jobs). Perl was already an INTERPRETER_CODE_FLAG entry.
|
|
2352
|
+
'R', 'Rscript', 'julia', 'perl',
|
|
2353
|
+
'sbatch', 'srun', 'squeue', 'sacct', 'sinfo', 'scancel', 'scontrol',
|
|
2234
2354
|
// Git + version control. `gh` is the GitHub CLI — used for PR / issue
|
|
2235
2355
|
// / release operations. Same blast-radius profile as git itself; the
|
|
2236
2356
|
// agent already does git_commit / git_push via dedicated tools, so
|
|
@@ -2328,14 +2448,96 @@ const ALLOWED_COMMANDS = new Set([
|
|
|
2328
2448
|
]);
|
|
2329
2449
|
const BLOCKED_PATTERNS = [/rm\s+-rf/, /rmdir/, /format/, /mkfs/, /dd\s+if=/];
|
|
2330
2450
|
/**
|
|
2331
|
-
*
|
|
2451
|
+
* Programs no approval admits through run_command. Shell interpreters (the
|
|
2452
|
+
* whole point of the no-shell runner), privilege escalation (the user types
|
|
2453
|
+
* `!sudo …` themselves), and `rm`/`unlink` (the dedicated `delete_file` tool
|
|
2454
|
+
* goes through its own gate with a diff of what disappears). A host must not
|
|
2455
|
+
* draw a permission card for these: the refusal is the answer.
|
|
2456
|
+
*/
|
|
2457
|
+
const SHELL_INTERPRETERS = new Set(['bash', 'sh', 'zsh', 'fish', 'dash', 'ksh', 'csh', 'tcsh', 'pwsh', 'powershell', 'cmd', 'cmd.exe', 'command', 'eval', 'exec']);
|
|
2458
|
+
const PRIVILEGE_COMMANDS = new Set(['sudo', 'doas', 'su', 'runas', 'pkexec']);
|
|
2459
|
+
exports.FORBIDDEN_COMMANDS = new Set([
|
|
2460
|
+
...SHELL_INTERPRETERS,
|
|
2461
|
+
...PRIVILEGE_COMMANDS,
|
|
2462
|
+
'rm', 'unlink'
|
|
2463
|
+
]);
|
|
2464
|
+
/** Bare program name (`/usr/bin/git` → `git`, `git.exe` → `git`). */
|
|
2465
|
+
function commandBaseName(cmd) {
|
|
2466
|
+
const base = cmd.trim().split(/[\\/]/).pop() ?? cmd;
|
|
2467
|
+
return base.replace(/\.exe$/i, '');
|
|
2468
|
+
}
|
|
2469
|
+
/** True for a program run_command runs on its own, with no host approval. */
|
|
2470
|
+
function isBuiltinCommand(cmd) {
|
|
2471
|
+
return ALLOWED_COMMANDS.has(commandBaseName(cmd));
|
|
2472
|
+
}
|
|
2473
|
+
/**
|
|
2474
|
+
* The refusal for a program no approval can admit, or null. Hosts call this
|
|
2475
|
+
* BEFORE their permission gate so a forbidden program never reaches a card
|
|
2476
|
+
* ("approve sudo?" is not a question Bandit asks).
|
|
2477
|
+
*/
|
|
2478
|
+
function forbiddenCommandReason(cmd) {
|
|
2479
|
+
const base = commandBaseName(cmd);
|
|
2480
|
+
const lower = base.toLowerCase();
|
|
2481
|
+
if (SHELL_INTERPRETERS.has(lower)) {
|
|
2482
|
+
return `"${base}" (a shell interpreter) is never run by run_command, approvals included. Do NOT wrap commands in \`${base} -c "…"\` — put the program in cmd and its arguments in args (e.g. cmd="diff", args="-rq dirA dirB"). Pipes and redirects do not exist here: filter output with the grep/head/tail params, or run the steps as separate calls. Only the user can run a shell line, by typing it with the \`!\` prefix.`;
|
|
2483
|
+
}
|
|
2484
|
+
if (PRIVILEGE_COMMANDS.has(lower)) {
|
|
2485
|
+
return `"${base}" (privilege escalation) is never run by run_command, approvals included. Run the command without ${base} if it works unprivileged; otherwise tell the user exactly what to run and why, and let them type it with the \`!\` prefix. Never suggest killing or restarting processes you do not own on a shared or remote host.`;
|
|
2486
|
+
}
|
|
2487
|
+
const dedicated = DEDICATED_TOOL_HINTS[lower];
|
|
2488
|
+
if (dedicated) {
|
|
2489
|
+
return `"${base}" is not run by run_command. Use the dedicated tool: ${dedicated}.`;
|
|
2490
|
+
}
|
|
2491
|
+
return null;
|
|
2492
|
+
}
|
|
2493
|
+
/** Tokens that only a shell interprets; run_command has none. A token is an
|
|
2494
|
+
* operator only when it is the whole argument (`grep -E 'a|b'` is fine). */
|
|
2495
|
+
const SHELL_OPERATOR_TOKENS = new Set(['|', '||', '&&', ';', '>', '>>', '<', '<<', '2>', '2>>', '2>&1', '&>', '&', '1>', '1>&2']);
|
|
2496
|
+
/**
|
|
2497
|
+
* The refusal for an argv that needs a shell (`… | tail -20`, `a && b`,
|
|
2498
|
+
* `> out.txt`), or null. Checked before anything runs — and by hosts before
|
|
2499
|
+
* their permission card, so a command that cannot run is not something the
|
|
2500
|
+
* user is asked to approve (overnight 2026-11-29: seven such cards).
|
|
2501
|
+
*/
|
|
2502
|
+
function shellOperatorRefusal(argv) {
|
|
2503
|
+
const ops = argv.filter((a) => SHELL_OPERATOR_TOKENS.has(a.trim()));
|
|
2504
|
+
if (ops.length === 0) {
|
|
2505
|
+
return null;
|
|
2506
|
+
}
|
|
2507
|
+
const unique = [...new Set(ops)];
|
|
2508
|
+
const pipes = unique.some((o) => o === '|' || o === '||');
|
|
2509
|
+
const redirects = unique.some((o) => /[<>]/.test(o));
|
|
2510
|
+
const chains = unique.some((o) => o === '&&' || o === ';' || o === '&');
|
|
2511
|
+
const fixes = [];
|
|
2512
|
+
if (pipes) {
|
|
2513
|
+
fixes.push('to filter output, use the run_command params instead of a pipe: grep="<regex>" keeps matching lines, head="N" / tail="N" keep the first/last N lines (applied in that order)');
|
|
2514
|
+
}
|
|
2515
|
+
if (redirects) {
|
|
2516
|
+
fixes.push('to save output to a file, run the command and then write_file with what you need (there is no `>`); input redirection is not available');
|
|
2517
|
+
}
|
|
2518
|
+
if (chains) {
|
|
2519
|
+
fixes.push('to run several steps, make one run_command call per program, in order (`cd` is the `cwd` param)');
|
|
2520
|
+
}
|
|
2521
|
+
return `run_command runs ONE program with NO shell, so ${unique.map((o) => `\`${o}\``).join(' ')} ${unique.length === 1 ? 'is' : 'are'} passed to the program as literal arguments and this cannot work. ${fixes.join('; ')}.`;
|
|
2522
|
+
}
|
|
2523
|
+
/**
|
|
2524
|
+
* The run_command program check and blocked patterns, as one check other
|
|
2332
2525
|
* command-running tools can apply before they call `ctx.runCommand`. Returns
|
|
2333
2526
|
* the refusal message, or null when the command may run. A workspace skill's
|
|
2334
|
-
* `command` used to bypass both — `sh`, `bash` and friends included.
|
|
2527
|
+
* `command` used to bypass both — `sh`, `bash` and friends included. With a
|
|
2528
|
+
* `ctx`, a program the host admits (`ctx.commandAllowed`) passes too.
|
|
2335
2529
|
*/
|
|
2336
|
-
function runCommandRefusal(cmd, args) {
|
|
2337
|
-
const baseCmd = cmd
|
|
2338
|
-
|
|
2530
|
+
function runCommandRefusal(cmd, args, ctx) {
|
|
2531
|
+
const baseCmd = commandBaseName(cmd);
|
|
2532
|
+
const forbidden = forbiddenCommandReason(baseCmd);
|
|
2533
|
+
if (forbidden) {
|
|
2534
|
+
return forbidden;
|
|
2535
|
+
}
|
|
2536
|
+
const operators = shellOperatorRefusal(args);
|
|
2537
|
+
if (operators) {
|
|
2538
|
+
return operators;
|
|
2539
|
+
}
|
|
2540
|
+
if (!ALLOWED_COMMANDS.has(baseCmd) && ctx?.commandAllowed?.(baseCmd, [baseCmd, ...args]) !== true) {
|
|
2339
2541
|
return rejectionMessage(baseCmd);
|
|
2340
2542
|
}
|
|
2341
2543
|
const fullCommand = [cmd, ...args].join(' ');
|
|
@@ -2346,6 +2548,24 @@ function runCommandRefusal(cmd, args) {
|
|
|
2346
2548
|
}
|
|
2347
2549
|
return null;
|
|
2348
2550
|
}
|
|
2551
|
+
/** The built-in programs, grouped for the tool description and `stealth_about`. */
|
|
2552
|
+
exports.BUILTIN_COMMAND_GROUPS = [
|
|
2553
|
+
{ group: 'JS/TS', programs: ['node', 'npm', 'pnpm', 'yarn', 'npx', 'bun', 'deno', 'tsc', 'eslint', 'prettier', 'vitest', 'jest'] },
|
|
2554
|
+
{ group: 'Python', programs: ['python3', 'pip', 'pytest', 'uv', 'poetry', 'ruff', 'mypy', 'black'] },
|
|
2555
|
+
{ group: 'research', programs: ['R', 'Rscript', 'julia', 'perl', 'sbatch', 'srun', 'squeue', 'sacct', 'sinfo', 'scancel', 'scontrol'] },
|
|
2556
|
+
{ group: 'VCS', programs: ['git', 'gh', 'glab'] },
|
|
2557
|
+
{ group: 'compiled', programs: ['cargo', 'rustc', 'go', 'dotnet', 'java', 'javac', 'mvn', 'gradle', 'kotlin', 'swift', 'xcodebuild', 'make', 'cmake', 'ninja', 'gcc', 'clang'] },
|
|
2558
|
+
{ group: 'other stacks', programs: ['ruby', 'bundle', 'rake', 'rspec', 'gem', 'php', 'composer', 'phpunit', 'pod'] },
|
|
2559
|
+
{ group: 'containers/infra', programs: ['docker', 'docker-compose', 'podman', 'kubectl', 'helm', 'terraform', 'ansible', 'pulumi', 'aws', 'gcloud', 'az'] },
|
|
2560
|
+
{ group: 'inspect (read-only)', programs: ['ls', 'cat', 'head', 'tail', 'wc', 'grep', 'rg', 'find', 'tree', 'file', 'stat', 'which', 'pwd', 'echo', 'date', 'env', 'df', 'du', 'ps', 'top', 'lsof', 'id', 'whoami', 'hostname', 'uname', 'jq', 'yq', 'diff', 'sort', 'uniq', 'cut', 'tr', 'sed', 'awk', 'xargs', 'base64'] },
|
|
2561
|
+
{ group: 'files', programs: ['mkdir', 'mv', 'cp', 'touch', 'ln', 'chmod', 'tar', 'zip', 'unzip', 'gzip', 'gunzip', 'pbcopy', 'pbpaste', 'open', 'osascript'] },
|
|
2562
|
+
{ group: 'network', programs: ['curl', 'wget', 'ping', 'dig', 'nslookup', 'traceroute', 'host', 'nc'] },
|
|
2563
|
+
{ group: 'processes/db', programs: ['kill', 'pkill', 'psql', 'mysql', 'sqlite3', 'redis-cli', 'mongosh'] },
|
|
2564
|
+
{ group: 'installers', programs: ['brew'] }
|
|
2565
|
+
];
|
|
2566
|
+
function builtinSummaryLine() {
|
|
2567
|
+
return exports.BUILTIN_COMMAND_GROUPS.map((g) => `${g.group}: ${g.programs.join(' ')}`).join('; ');
|
|
2568
|
+
}
|
|
2349
2569
|
/**
|
|
2350
2570
|
* Map of well-known CLIs the agent might be asked to install but that
|
|
2351
2571
|
* aren't on the allow-list themselves (terraform, kubectl when not yet
|
|
@@ -2414,26 +2634,20 @@ function rejectionMessage(baseCmd) {
|
|
|
2414
2634
|
// enough: what failed, the install/dedicated-tool path (if known),
|
|
2415
2635
|
// and the !-prefix escape hatch as a last resort. The model can ask
|
|
2416
2636
|
// for the full allow-list if it needs one — it never does in practice.
|
|
2417
|
-
|
|
2418
|
-
//
|
|
2419
|
-
//
|
|
2420
|
-
//
|
|
2421
|
-
//
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
}
|
|
2432
|
-
const hint = INSTALL_HINTS[lower];
|
|
2433
|
-
const installLine = hint
|
|
2434
|
-
? `Install it first: run_command("${hint}"), then retry.`
|
|
2435
|
-
: `If it ships via a package manager already on the allow-list (brew, npm, pip, cargo, gem, go), install it first.`;
|
|
2436
|
-
return `"${baseCmd}" is not in the run_command allow-list. ${installLine} Or tell the user to type \`!${baseCmd} <args>\` in the composer — the \`!\`-prefix runs directly in their shell and bypasses the gate.`;
|
|
2637
|
+
// Shell interpreters, sudo and rm never get here: forbiddenCommandReason
|
|
2638
|
+
// (checked first, by the host before its card and by the tool) explains
|
|
2639
|
+
// those. What is left is a program that is simply not built in and that the
|
|
2640
|
+
// host did not admit — in the CLI that means the user denied the card, a
|
|
2641
|
+
// deny rule matched, or the run is unattended. Say what admits it, and never
|
|
2642
|
+
// suggest retrying the same call: the loop repeated `ssh` twice after two
|
|
2643
|
+
// approvals on 2026-11-29 because the message blamed an "allow-list".
|
|
2644
|
+
const forbidden = forbiddenCommandReason(baseCmd);
|
|
2645
|
+
if (forbidden) {
|
|
2646
|
+
return forbidden;
|
|
2647
|
+
}
|
|
2648
|
+
const hint = INSTALL_HINTS[baseCmd.toLowerCase()];
|
|
2649
|
+
const installLine = hint ? ` If it is not installed: run_command("${hint}").` : '';
|
|
2650
|
+
return `"${baseCmd}" is not one of run_command's built-in programs and the host did not admit it for this call. The user admits a program by approving it on the permission card ("allow once", or "allow ${baseCmd} this session"), or with a rule \`run_command:${baseCmd}*\` in permissions.allow (\`bandit config add permissions.allow 'run_command:${baseCmd}*'\`, or /permissions). Do not retry the same call: if it was just denied, do the task another way or say what you need; if nobody is at the terminal, say so in your answer.${installLine} The user can also run it themselves with the \`!${baseCmd} <args>\` composer prefix.`;
|
|
2437
2651
|
}
|
|
2438
2652
|
/**
|
|
2439
2653
|
* Shell-aware argv tokenizer. Replaces the naive `split(/\s+/)` that
|
|
@@ -2521,6 +2735,61 @@ function shellTokenize(input) {
|
|
|
2521
2735
|
}
|
|
2522
2736
|
return out;
|
|
2523
2737
|
}
|
|
2738
|
+
/** Read grep/head/tail params; `{ error }` names a bad value with the fix. */
|
|
2739
|
+
function parseOutputFilters(params) {
|
|
2740
|
+
const out = {};
|
|
2741
|
+
const grep = params.grep?.trim();
|
|
2742
|
+
if (grep) {
|
|
2743
|
+
// `(?i)` is how most models spell case-insensitivity; JavaScript spells it
|
|
2744
|
+
// as a flag. Accept both.
|
|
2745
|
+
const insensitive = grep.startsWith('(?i)');
|
|
2746
|
+
const source = insensitive ? grep.slice(4) : grep;
|
|
2747
|
+
try {
|
|
2748
|
+
out.grep = new RegExp(source, insensitive ? 'i' : '');
|
|
2749
|
+
}
|
|
2750
|
+
catch (err) {
|
|
2751
|
+
return { error: `run_command: the grep param is not a valid regular expression (${err instanceof Error ? err.message : String(err)}). Use JavaScript regex syntax, e.g. grep="error|warn" or grep="(?i)failed".` };
|
|
2752
|
+
}
|
|
2753
|
+
}
|
|
2754
|
+
for (const key of ['head', 'tail']) {
|
|
2755
|
+
const raw = params[key]?.trim();
|
|
2756
|
+
if (!raw) {
|
|
2757
|
+
continue;
|
|
2758
|
+
}
|
|
2759
|
+
const n = Number(raw.replace(/^-/, ''));
|
|
2760
|
+
if (!Number.isInteger(n) || n < 1) {
|
|
2761
|
+
return { error: `run_command: the ${key} param must be a positive whole number of lines (got "${raw}"), e.g. ${key}="20".` };
|
|
2762
|
+
}
|
|
2763
|
+
out[key] = n;
|
|
2764
|
+
}
|
|
2765
|
+
return out;
|
|
2766
|
+
}
|
|
2767
|
+
/** grep, then head, then tail — the order a pipeline would usually apply them. */
|
|
2768
|
+
function applyOutputFilters(stdout, filters) {
|
|
2769
|
+
if (!filters.grep && filters.head === undefined && filters.tail === undefined) {
|
|
2770
|
+
return { text: stdout, note: '' };
|
|
2771
|
+
}
|
|
2772
|
+
const lines = stdout.replace(/\n$/, '').split('\n');
|
|
2773
|
+
const total = lines.length;
|
|
2774
|
+
let kept = lines;
|
|
2775
|
+
const steps = [];
|
|
2776
|
+
if (filters.grep) {
|
|
2777
|
+
kept = kept.filter((line) => filters.grep.test(line));
|
|
2778
|
+
steps.push(`grep ${filters.grep.source}`);
|
|
2779
|
+
}
|
|
2780
|
+
if (filters.head !== undefined) {
|
|
2781
|
+
kept = kept.slice(0, filters.head);
|
|
2782
|
+
steps.push(`head ${filters.head}`);
|
|
2783
|
+
}
|
|
2784
|
+
if (filters.tail !== undefined) {
|
|
2785
|
+
kept = kept.slice(-filters.tail);
|
|
2786
|
+
steps.push(`tail ${filters.tail}`);
|
|
2787
|
+
}
|
|
2788
|
+
return {
|
|
2789
|
+
text: kept.join('\n'),
|
|
2790
|
+
note: ` (filtered: ${steps.join(' | ')} → ${kept.length} of ${total} lines)`
|
|
2791
|
+
};
|
|
2792
|
+
}
|
|
2524
2793
|
/** Interpreters run_command may start, and their inline-code flag. */
|
|
2525
2794
|
const INTERPRETER_CODE_FLAG = {
|
|
2526
2795
|
python: '-c',
|
|
@@ -2532,11 +2801,21 @@ const INTERPRETER_CODE_FLAG = {
|
|
|
2532
2801
|
};
|
|
2533
2802
|
const runCommandTool = {
|
|
2534
2803
|
name: 'run_command',
|
|
2535
|
-
|
|
2804
|
+
// The description leads with the two facts models get wrong most (one
|
|
2805
|
+
// program, no shell; which programs run) because a wrong first call costs
|
|
2806
|
+
// a permission card or a refusal and the loop then fights the runner
|
|
2807
|
+
// (seven pipe/redirect/&& attempts in one 2026-11-29 session).
|
|
2808
|
+
description: 'Run ONE program (no shell) in the workspace and return its output. There is no shell: `|`, `&&`, `;`, `>`, `source`, `cd` and glob/`~` expansion inside arguments do NOT work — put the program in cmd and its arguments in args (a space-separated string, or a JSON array when an argument contains spaces), one program per call, `cwd` instead of cd. To filter output in-process use the grep/head/tail params (grep="ERROR" tail="20" means the last 20 matching lines) instead of a pipe. '
|
|
2809
|
+
+ 'Built-in programs run on their own: ' + builtinSummaryLine() + '. '
|
|
2810
|
+
+ 'Any other program (ssh, rsync, ffmpeg, a project binary …) runs only when the user admits it: they approve it on the permission card ("allow once" / "allow <program> this session") or have a `run_command:<program>*` rule in permissions.allow — so calling it is correct, and a refusal means the user declined, not that you should retry. Shell interpreters (bash -c, sh, zsh, pwsh), sudo/su and rm are never run, approvals included (rm → delete_file; the user types shell lines themselves with the `!` prefix). '
|
|
2811
|
+
+ 'Installing a CLI or package is normal work: run the install through its package manager (brew, npm, pip, pipx, cargo, gem, go); the permission gate asks the user. Python: prefer `python3 -m pip`; on an "externally-managed-environment" (PEP 668) error create a venv once (cmd="python3" args="-m venv .venv") and then call `.venv/bin/pip` / `.venv/bin/python` directly — never `source .venv/bin/activate`. Never kill or restart processes you do not own on a shared or remote host.',
|
|
2536
2812
|
parameters: [
|
|
2537
|
-
{ name: 'cmd', description: 'The
|
|
2538
|
-
{ name: 'args', description: '
|
|
2539
|
-
{ name: 'cwd', description: 'Working directory relative to workspace root (optional)' }
|
|
2813
|
+
{ name: 'cmd', description: 'The program to run (e.g. "npm", "tsc", "git"). Just the program — arguments go in args.', required: true },
|
|
2814
|
+
{ name: 'args', description: 'Arguments: a space-separated string (quotes group words: -m "two words") or a JSON array of strings (["-m", "two words"]).' },
|
|
2815
|
+
{ name: 'cwd', description: 'Working directory, relative to the workspace root or absolute (optional; this replaces `cd`).' },
|
|
2816
|
+
{ name: 'grep', description: 'Optional regex (JavaScript syntax, case-sensitive unless (?i)…): keep only stdout lines that match. In-process — use this instead of `| grep`.' },
|
|
2817
|
+
{ name: 'head', description: 'Optional: keep only the first N lines of stdout (after grep). Use instead of `| head -N`.' },
|
|
2818
|
+
{ name: 'tail', description: 'Optional: keep only the last N lines of stdout (after grep and head). Use instead of `| tail -N`.' }
|
|
2540
2819
|
],
|
|
2541
2820
|
async execute(params, ctx) {
|
|
2542
2821
|
const rawCmd = params.cmd?.trim();
|
|
@@ -2580,23 +2859,36 @@ const runCommandTool = {
|
|
|
2580
2859
|
const inlineArgs = rawCmd.slice(firstSpace + 1).trim();
|
|
2581
2860
|
argsString = argsString ? `${inlineArgs} ${argsString}` : inlineArgs;
|
|
2582
2861
|
}
|
|
2583
|
-
const baseCmd = cmd
|
|
2584
|
-
|
|
2862
|
+
const baseCmd = commandBaseName(cmd);
|
|
2863
|
+
// Forbidden first (shells, sudo, rm): no host hook and no approval
|
|
2864
|
+
// admits these, and the message tells the model the real shape.
|
|
2865
|
+
const forbidden = forbiddenCommandReason(baseCmd);
|
|
2866
|
+
if (forbidden) {
|
|
2867
|
+
return { output: forbidden, isError: true };
|
|
2868
|
+
}
|
|
2869
|
+
const args = preparsedArgs ?? (argsString ? shellTokenize(argsString) : []);
|
|
2870
|
+
if (inlineCode !== undefined && codeFlag) {
|
|
2871
|
+
args.unshift(codeFlag, inlineCode);
|
|
2872
|
+
inlineCode = undefined;
|
|
2873
|
+
}
|
|
2874
|
+
// A pipe, redirect or chain cannot work without a shell: refuse before
|
|
2875
|
+
// spawning, with the in-process alternative, instead of handing `|` to
|
|
2876
|
+
// the program and returning its confused error.
|
|
2877
|
+
const operators = shellOperatorRefusal(args);
|
|
2878
|
+
if (operators) {
|
|
2879
|
+
return { output: operators, isError: true };
|
|
2880
|
+
}
|
|
2881
|
+
// Built in, or admitted by the host for this call (an approval, a
|
|
2882
|
+
// `run_command:<program>*` rule, the command sandbox).
|
|
2883
|
+
if (!ALLOWED_COMMANDS.has(baseCmd) && ctx.commandAllowed?.(baseCmd, [baseCmd, ...args]) !== true) {
|
|
2585
2884
|
return { output: rejectionMessage(baseCmd), isError: true };
|
|
2586
2885
|
}
|
|
2587
|
-
const fullCommand =
|
|
2588
|
-
? `${cmd} ${preparsedArgs.join(' ')}`.trim()
|
|
2589
|
-
: `${cmd} ${argsString}`.trim();
|
|
2886
|
+
const fullCommand = `${cmd} ${args.join(' ')}`.trim();
|
|
2590
2887
|
for (const blocked of BLOCKED_PATTERNS) {
|
|
2591
2888
|
if (blocked.test(fullCommand)) {
|
|
2592
2889
|
return { output: `Error: command contains a blocked pattern (${blocked.source})`, isError: true };
|
|
2593
2890
|
}
|
|
2594
2891
|
}
|
|
2595
|
-
const args = preparsedArgs ?? (argsString ? shellTokenize(argsString) : []);
|
|
2596
|
-
if (inlineCode !== undefined && codeFlag) {
|
|
2597
|
-
args.unshift(codeFlag, inlineCode);
|
|
2598
|
-
inlineCode = undefined;
|
|
2599
|
-
}
|
|
2600
2892
|
// An interpreter with no script, no -c/-e and no module reads its program
|
|
2601
2893
|
// from stdin; commands here get no stdin, so it exits 0 having run
|
|
2602
2894
|
// nothing. That is not a check — refuse it with the fix.
|
|
@@ -2628,10 +2920,19 @@ const runCommandTool = {
|
|
|
2628
2920
|
const cwd = params.cwd
|
|
2629
2921
|
? (isAbsolutePath(params.cwd) ? params.cwd : `${ctx.workspaceRoot}/${params.cwd}`)
|
|
2630
2922
|
: ctx.workspaceRoot;
|
|
2923
|
+
// In-process stdout filters: the no-shell replacement for `| grep`,
|
|
2924
|
+
// `| head`, `| tail`. Validated before the command runs so a bad regex
|
|
2925
|
+
// does not cost a run.
|
|
2926
|
+
const filters = parseOutputFilters(params);
|
|
2927
|
+
if ('error' in filters) {
|
|
2928
|
+
return { output: filters.error, isError: true };
|
|
2929
|
+
}
|
|
2631
2930
|
try {
|
|
2632
|
-
const { stdout, stderr, exitCode } = await ctx.runCommand(cmd, args, cwd);
|
|
2931
|
+
const { stdout: rawStdout, stderr, exitCode } = await ctx.runCommand(cmd, args, cwd);
|
|
2932
|
+
const filtered = applyOutputFilters(rawStdout, filters);
|
|
2933
|
+
const stdout = filtered.text;
|
|
2633
2934
|
const combined = [
|
|
2634
|
-
stdout.trim() ? `stdout:\n${stdout.trim()}` : '',
|
|
2935
|
+
stdout.trim() ? `stdout${filtered.note}:\n${stdout.trim()}` : filtered.note ? `stdout${filtered.note}: (no lines left)` : '',
|
|
2635
2936
|
stderr.trim() ? `stderr:\n${stderr.trim()}` : '',
|
|
2636
2937
|
`exit code: ${exitCode}`
|
|
2637
2938
|
].filter(Boolean).join('\n\n');
|
|
@@ -2721,19 +3022,15 @@ const watchCommandTool = {
|
|
|
2721
3022
|
const inlineArgs = rawCmd.slice(firstSpace + 1).trim();
|
|
2722
3023
|
argsString = argsString ? `${inlineArgs} ${argsString}` : inlineArgs;
|
|
2723
3024
|
}
|
|
2724
|
-
const baseCmd = cmd
|
|
2725
|
-
if (!ALLOWED_COMMANDS.has(baseCmd)) {
|
|
2726
|
-
return { output: rejectionMessage(baseCmd), isError: true };
|
|
2727
|
-
}
|
|
2728
|
-
const fullCommand = preparsedArgs
|
|
2729
|
-
? `${cmd} ${preparsedArgs.join(' ')}`.trim()
|
|
2730
|
-
: `${cmd} ${argsString}`.trim();
|
|
2731
|
-
for (const blocked of BLOCKED_PATTERNS) {
|
|
2732
|
-
if (blocked.test(fullCommand)) {
|
|
2733
|
-
return { output: `Error: command contains a blocked pattern (${blocked.source})`, isError: true };
|
|
2734
|
-
}
|
|
2735
|
-
}
|
|
3025
|
+
const baseCmd = commandBaseName(cmd);
|
|
2736
3026
|
const args = preparsedArgs ?? (argsString ? shellTokenize(argsString) : []);
|
|
3027
|
+
// Same gate as run_command: forbidden programs, shell operators, then
|
|
3028
|
+
// built-in or host-admitted.
|
|
3029
|
+
const refusal = runCommandRefusal(cmd, args, ctx);
|
|
3030
|
+
if (refusal) {
|
|
3031
|
+
return { output: refusal, isError: true };
|
|
3032
|
+
}
|
|
3033
|
+
const fullCommand = `${cmd} ${args.join(' ')}`.trim();
|
|
2737
3034
|
const cwd = params.cwd
|
|
2738
3035
|
? (isAbsolutePath(params.cwd) ? params.cwd : `${ctx.workspaceRoot}/${params.cwd}`)
|
|
2739
3036
|
: ctx.workspaceRoot;
|