@backendkit-labs/agent-coding 0.25.0 → 0.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__tests__/sandbox.test.d.ts +2 -0
- package/dist/__tests__/sandbox.test.d.ts.map +1 -0
- package/dist/__tests__/sandbox.test.js +109 -0
- package/dist/__tests__/sandbox.test.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/dist/sandbox.d.ts +24 -0
- package/dist/sandbox.d.ts.map +1 -0
- package/dist/sandbox.js +31 -0
- package/dist/sandbox.js.map +1 -0
- package/dist/tools/__tests__/file-tools.sandbox.test.d.ts +2 -0
- package/dist/tools/__tests__/file-tools.sandbox.test.d.ts.map +1 -0
- package/dist/tools/__tests__/file-tools.sandbox.test.js +97 -0
- package/dist/tools/__tests__/file-tools.sandbox.test.js.map +1 -0
- package/dist/tools/__tests__/run-command.sandbox.test.d.ts +2 -0
- package/dist/tools/__tests__/run-command.sandbox.test.d.ts.map +1 -0
- package/dist/tools/__tests__/run-command.sandbox.test.js +149 -0
- package/dist/tools/__tests__/run-command.sandbox.test.js.map +1 -0
- package/dist/tools/edit-file.d.ts.map +1 -1
- package/dist/tools/edit-file.js +4 -0
- package/dist/tools/edit-file.js.map +1 -1
- package/dist/tools/legacy-sandbox.d.ts +19 -0
- package/dist/tools/legacy-sandbox.d.ts.map +1 -0
- package/dist/tools/legacy-sandbox.js +29 -0
- package/dist/tools/legacy-sandbox.js.map +1 -0
- package/dist/tools/list-directory.d.ts.map +1 -1
- package/dist/tools/list-directory.js +4 -0
- package/dist/tools/list-directory.js.map +1 -1
- package/dist/tools/read-file.d.ts.map +1 -1
- package/dist/tools/read-file.js +4 -0
- package/dist/tools/read-file.js.map +1 -1
- package/dist/tools/run-command.d.ts.map +1 -1
- package/dist/tools/run-command.js +44 -179
- package/dist/tools/run-command.js.map +1 -1
- package/dist/tools/sandbox-path.d.ts +8 -0
- package/dist/tools/sandbox-path.d.ts.map +1 -0
- package/dist/tools/sandbox-path.js +13 -0
- package/dist/tools/sandbox-path.js.map +1 -0
- package/dist/tools/search-files.d.ts.map +1 -1
- package/dist/tools/search-files.js +4 -0
- package/dist/tools/search-files.js.map +1 -1
- package/dist/tools/write-file.d.ts.map +1 -1
- package/dist/tools/write-file.js +4 -0
- package/dist/tools/write-file.js.map +1 -1
- package/package.json +3 -2
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const fs_1 = require("fs");
|
|
4
|
+
const os_1 = require("os");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const agent_core_1 = require("@backendkit-labs/agent-core");
|
|
7
|
+
const agent_sandbox_1 = require("@backendkit-labs/agent-sandbox");
|
|
8
|
+
const run_command_1 = require("../run-command");
|
|
9
|
+
const baseCtx = {
|
|
10
|
+
agentId: 'coder', sessionId: 's1', workingDir: process.cwd(),
|
|
11
|
+
memory: { get: () => undefined, set: () => undefined, getAll: () => ({}) },
|
|
12
|
+
askAgent: async () => '',
|
|
13
|
+
};
|
|
14
|
+
/** A runner that answers with a fixed outcome and remembers what it was asked. */
|
|
15
|
+
function runnerReturning(outcome, backend = 'none') {
|
|
16
|
+
const requests = [];
|
|
17
|
+
const runner = {
|
|
18
|
+
async run(caller, req) {
|
|
19
|
+
requests.push({ caller, req });
|
|
20
|
+
if ('code' in outcome)
|
|
21
|
+
return outcome;
|
|
22
|
+
return { ok: true, mode: 'workspace-write', network: 'none', backend, result: { exitCode: 0, output: '', timedOut: false, aborted: false, ...outcome } };
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
return { runner, requests };
|
|
26
|
+
}
|
|
27
|
+
const run = (runner, input, ctx = {}) => run_command_1.runCommandTool.execute({ timeout_ms: 30_000, ...input }, { ...baseCtx, sandbox: runner, ...ctx });
|
|
28
|
+
describe('run_command through ctx.sandbox', () => {
|
|
29
|
+
it('hands the sandbox the caller, the command and the directories, and uses its answer', async () => {
|
|
30
|
+
const { runner, requests } = runnerReturning({ output: 'hello\n' });
|
|
31
|
+
const controller = new AbortController();
|
|
32
|
+
const out = await run(runner, { command: 'echo hello', timeout_ms: 5000 }, { signal: controller.signal });
|
|
33
|
+
expect(out).toBe('hello');
|
|
34
|
+
expect(requests).toHaveLength(1);
|
|
35
|
+
const { caller, req } = requests[0];
|
|
36
|
+
expect(caller).toEqual({ agentId: 'coder', sessionId: 's1' });
|
|
37
|
+
expect(req).toMatchObject({ tool: 'run_command', command: 'echo hello', cwd: process.cwd(), workspace: process.cwd(), timeoutMs: 5000 });
|
|
38
|
+
expect(req.signal).toBe(controller.signal);
|
|
39
|
+
});
|
|
40
|
+
it('keeps the portable command for containers and the Windows-adjusted one for the host shell', async () => {
|
|
41
|
+
const { runner, requests } = runnerReturning({ output: 'x' });
|
|
42
|
+
await run(runner, { command: 'pwd' });
|
|
43
|
+
expect(requests[0].req.command).toBe('pwd');
|
|
44
|
+
expect(requests[0].req.hostCommand).toBe(process.platform === 'win32' ? 'echo %cd%' : 'pwd');
|
|
45
|
+
});
|
|
46
|
+
it('resolves a cwd inside the workspace and refuses one outside before asking the sandbox', async () => {
|
|
47
|
+
const { runner, requests } = runnerReturning({ output: 'x' });
|
|
48
|
+
await run(runner, { command: 'echo a', cwd: 'src' });
|
|
49
|
+
expect(requests[0].req.cwd).toBe((0, path_1.join)(process.cwd(), 'src'));
|
|
50
|
+
const out = await run(runner, { command: 'echo a', cwd: '../../..' });
|
|
51
|
+
expect(out).toMatch(/^Error: Access denied/);
|
|
52
|
+
expect(requests).toHaveLength(1);
|
|
53
|
+
});
|
|
54
|
+
it('maps every kind of result to the same strings as before', async () => {
|
|
55
|
+
expect(await run(runnerReturning({ exitCode: 0, output: '' }).runner, { command: 'echo ok' })).toBe('(no output)');
|
|
56
|
+
expect(await run(runnerReturning({ exitCode: 3, output: '' }).runner, { command: 'echo ok' })).toBe('Error: Command exited with status 3');
|
|
57
|
+
expect(await run(runnerReturning({ exitCode: 3, output: 'partial' }).runner, { command: 'echo x' })).toBe('partial');
|
|
58
|
+
expect(await run(runnerReturning({ timedOut: true, exitCode: null }).runner, { command: 'echo x', timeout_ms: 1500 })).toBe('Error: Command timed out after 1500ms');
|
|
59
|
+
expect(await run(runnerReturning({ aborted: true, exitCode: null }).runner, { command: 'echo x' })).toBe('Error: Command aborted');
|
|
60
|
+
expect(await run(runnerReturning({ error: 'spawn failed', exitCode: null }).runner, { command: 'echo x' })).toBe('Error: spawn failed');
|
|
61
|
+
});
|
|
62
|
+
it('a non-zero exit from a confining backend is reported as a sandboxed failure with its output', async () => {
|
|
63
|
+
const { runner } = runnerReturning({ exitCode: 2, output: 'boom' }, 'docker');
|
|
64
|
+
expect(await run(runner, { command: 'echo x' })).toBe('Error: sandboxed command exited with status 2\nboom');
|
|
65
|
+
});
|
|
66
|
+
it('truncates long output at 20000 characters and says how long it was', async () => {
|
|
67
|
+
const { runner } = runnerReturning({ output: 'x'.repeat(25_000) });
|
|
68
|
+
const out = await run(runner, { command: 'echo x' });
|
|
69
|
+
expect(out.length).toBeLessThan(25_000);
|
|
70
|
+
expect(out).toContain('[truncated — 25000 total chars]');
|
|
71
|
+
});
|
|
72
|
+
it.each([
|
|
73
|
+
['SANDBOX_UNAVAILABLE', 'cannot enforce workspace-write on this host; nothing was run'],
|
|
74
|
+
['SANDBOX_DENIED', 'working directory is outside the allowed roots'],
|
|
75
|
+
])('a %s answer comes back as an error naming the code and the reason', async (code, reason) => {
|
|
76
|
+
const { runner } = runnerReturning({ ok: false, code, reason, mode: 'workspace-write', network: 'none' });
|
|
77
|
+
expect(await run(runner, { command: 'echo hi' })).toBe(`Error: ${code}: ${reason}`);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
describe('run_command in the background through ctx.sandbox', () => {
|
|
81
|
+
const settle = async (jobs, id) => {
|
|
82
|
+
for (let i = 0; i < 200 && jobs.get(id, 'coder')?.status === 'running'; i++)
|
|
83
|
+
await new Promise(r => setTimeout(r, 10));
|
|
84
|
+
return jobs.get(id, 'coder');
|
|
85
|
+
};
|
|
86
|
+
it('streams output into the job and ends done on exit 0', async () => {
|
|
87
|
+
const jobs = (0, agent_core_1.createJobRegistry)();
|
|
88
|
+
const runner = {
|
|
89
|
+
async run(_c, req) {
|
|
90
|
+
req.onOutput?.('line 1\n');
|
|
91
|
+
req.onOutput?.('line 2\n');
|
|
92
|
+
expect(req.timeoutMs).toBeUndefined();
|
|
93
|
+
return { ok: true, mode: 'workspace-write', network: 'none', backend: 'fake', result: { exitCode: 0, output: '', timedOut: false, aborted: false } };
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
const started = await run(runner, { command: 'echo long', background: true }, { jobs });
|
|
97
|
+
expect(started).toContain('bash-1');
|
|
98
|
+
expect((await settle(jobs, 'bash-1')).status).toBe('done');
|
|
99
|
+
expect((await jobs.output('bash-1', 'coder')).text).toBe('line 1\nline 2\n');
|
|
100
|
+
});
|
|
101
|
+
it('ends failed with the exit code, or with the sandbox refusal', async () => {
|
|
102
|
+
const jobs = (0, agent_core_1.createJobRegistry)();
|
|
103
|
+
await run(runnerReturning({ exitCode: 4 }).runner, { command: 'echo x', background: true }, { jobs });
|
|
104
|
+
const failing = await settle(jobs, 'bash-1');
|
|
105
|
+
expect(failing).toMatchObject({ status: 'failed', summary: 'exit 4' });
|
|
106
|
+
const refused = runnerReturning({ ok: false, code: 'SANDBOX_UNAVAILABLE', reason: 'docker is down', mode: 'workspace-write', network: 'none' });
|
|
107
|
+
await run(refused.runner, { command: 'echo x', background: true }, { jobs });
|
|
108
|
+
expect(await settle(jobs, 'bash-2')).toMatchObject({ status: 'failed', summary: 'SANDBOX_UNAVAILABLE: docker is down' });
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
describe('run_command with a real sandbox service', () => {
|
|
112
|
+
let dir;
|
|
113
|
+
beforeEach(() => { dir = (0, fs_1.mkdtempSync)((0, path_1.join)((0, os_1.tmpdir)(), 'bk-rc-sbx-')); });
|
|
114
|
+
afterEach(() => { (0, fs_1.rmSync)(dir, { recursive: true, force: true }); });
|
|
115
|
+
it('FAILS CLOSED: with docker down it reports SANDBOX_UNAVAILABLE and the command never runs on the host', async () => {
|
|
116
|
+
const marker = (0, path_1.join)(dir, 'ran.txt');
|
|
117
|
+
const sandbox = (0, agent_sandbox_1.createSandboxService)({
|
|
118
|
+
policy: (0, agent_sandbox_1.createSandboxPolicy)({ preset: 'ask' }),
|
|
119
|
+
// The host backend is present, as in the default setup: it must still not be used for workspace-write.
|
|
120
|
+
backends: [new agent_sandbox_1.DockerBackend({ probe: async () => false }), new agent_sandbox_1.NoneBackend()],
|
|
121
|
+
});
|
|
122
|
+
const out = await run(sandbox, { command: `node -e "require('fs').writeFileSync(process.argv[1],'x')" "${marker.replace(/\\/g, '/')}"` }, { workingDir: dir });
|
|
123
|
+
expect(out).toMatch(/^Error: SANDBOX_UNAVAILABLE: cannot enforce workspace-write/);
|
|
124
|
+
await new Promise(r => setTimeout(r, 500));
|
|
125
|
+
expect((0, fs_1.existsSync)(marker)).toBe(false);
|
|
126
|
+
}, 20_000);
|
|
127
|
+
it('full access on the host backend runs the command, with credential-looking variables removed from its environment', async () => {
|
|
128
|
+
process.env.BK_TEST_RC_SECRET_TOKEN = 'must-not-leak';
|
|
129
|
+
try {
|
|
130
|
+
const sandbox = (0, agent_sandbox_1.createSandboxService)({ policy: (0, agent_sandbox_1.createSandboxPolicy)({ preset: 'full' }), backends: [new agent_sandbox_1.NoneBackend()] });
|
|
131
|
+
const out = await run(sandbox, { command: `node -e "console.log(process.env.BK_TEST_RC_SECRET_TOKEN || 'absent')"` }, { workingDir: dir });
|
|
132
|
+
expect(out).toBe('absent');
|
|
133
|
+
}
|
|
134
|
+
finally {
|
|
135
|
+
delete process.env.BK_TEST_RC_SECRET_TOKEN;
|
|
136
|
+
}
|
|
137
|
+
}, 20_000);
|
|
138
|
+
it('without a configured sandbox the legacy path still passes the whole environment (nothing changes for existing users)', async () => {
|
|
139
|
+
process.env.BK_TEST_RC_LEGACY_TOKEN = 'still-visible';
|
|
140
|
+
try {
|
|
141
|
+
const out = await run_command_1.runCommandTool.execute({ command: `node -e "console.log(process.env.BK_TEST_RC_LEGACY_TOKEN || 'absent')"`, timeout_ms: 20_000 }, { ...baseCtx, workingDir: dir });
|
|
142
|
+
expect(out).toBe('still-visible');
|
|
143
|
+
}
|
|
144
|
+
finally {
|
|
145
|
+
delete process.env.BK_TEST_RC_LEGACY_TOKEN;
|
|
146
|
+
}
|
|
147
|
+
}, 30_000);
|
|
148
|
+
});
|
|
149
|
+
//# sourceMappingURL=run-command.sandbox.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-command.sandbox.test.js","sourceRoot":"","sources":["../../../src/tools/__tests__/run-command.sandbox.test.ts"],"names":[],"mappings":";;AAAA,2BAAqD;AACrD,2BAA4B;AAC5B,+BAA4B;AAC5B,4DAAkI;AAClI,kEAAuH;AACvH,gDAAgD;AAEhD,MAAM,OAAO,GAAG;IACZ,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,CAAC,GAAG,EAAE;IAC5D,MAAM,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE;IAC1E,QAAQ,EAAE,KAAK,IAAI,EAAE,CAAC,EAAE;CAC3B,CAAC;AAEF,kFAAkF;AAClF,SAAS,eAAe,CAAC,OAA0G,EAAE,OAAO,GAAG,MAAM;IACjJ,MAAM,QAAQ,GAAuF,EAAE,CAAC;IACxG,MAAM,MAAM,GAAkB;QAC1B,KAAK,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG;YACjB,QAAQ,CAAC,IAAI,CAA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I,2BAAW,EAAE,CAAC,EAAE,CAAC,CAAC;YACzH,MAAM,GAAG,GAAG,MAAM,GAAG,CAAC,OAAO,EAAE,EAAE,OAAO,EAAE,wEAAwE,EAAE,EAAE,EAAE,UAAU,EAAE,GAAG,EAAE,CAAC,CAAC;YAC3I,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC/B,CAAC;gBAAS,CAAC;YAAC,OAAO,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC;QAAC,CAAC;IAC7D,CAAC,EAAE,MAAM,CAAC,CAAC;IAEX,EAAE,CAAC,sHAAsH,EAAE,KAAK,IAAI,EAAE;QAClI,OAAO,CAAC,GAAG,CAAC,uBAAuB,GAAG,eAAe,CAAC;QACtD,IAAI,CAAC;YACD,MAAM,GAAG,GAAG,MAAM,4BAAc,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,wEAAwE,EAAE,UAAU,EAAE,MAAM,EAAE,EAAE,EAAE,GAAG,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,CAAC,CAAC;YACrL,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;QACtC,CAAC;gBAAS,CAAC;YAAC,OAAO,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC;QAAC,CAAC;IAC7D,CAAC,EAAE,MAAM,CAAC,CAAC;AACf,CAAC,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"edit-file.d.ts","sourceRoot":"","sources":["../../src/tools/edit-file.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"edit-file.d.ts","sourceRoot":"","sources":["../../src/tools/edit-file.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,YAAY,sDAwCvB,CAAC"}
|
package/dist/tools/edit-file.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.editFileTool = void 0;
|
|
|
4
4
|
const fs_1 = require("fs");
|
|
5
5
|
const agent_core_1 = require("@backendkit-labs/agent-core");
|
|
6
6
|
const path_sandbox_1 = require("./path-sandbox");
|
|
7
|
+
const sandbox_path_1 = require("./sandbox-path");
|
|
7
8
|
const secret_scanner_1 = require("./secret-scanner");
|
|
8
9
|
exports.editFileTool = (0, agent_core_1.defineTool)({
|
|
9
10
|
name: 'edit_file',
|
|
@@ -20,6 +21,9 @@ exports.editFileTool = (0, agent_core_1.defineTool)({
|
|
|
20
21
|
const fwWarn = (0, path_sandbox_1.checkFrameworkProtection)(abs, ctx.workingDir);
|
|
21
22
|
if (fwWarn)
|
|
22
23
|
return `Error: ${fwWarn}`;
|
|
24
|
+
const denied = (0, sandbox_path_1.sandboxPathError)(ctx, 'edit_file', abs, 'write');
|
|
25
|
+
if (denied)
|
|
26
|
+
return denied;
|
|
23
27
|
const content = (0, fs_1.readFileSync)(abs, 'utf-8');
|
|
24
28
|
if (!content.includes(old_string)) {
|
|
25
29
|
return `Error: old_string not found in "${abs}"`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"edit-file.js","sourceRoot":"","sources":["../../src/tools/edit-file.ts"],"names":[],"mappings":";;;AAAA,2BAAkC;AAClC,4DAA6E;AAC7E,iDAA2E;AAC3E,qDAAiD;AAEpC,QAAA,YAAY,GAAG,IAAA,uBAAU,EAAC;IACnC,IAAI,EAAE,WAAW;IACjB,WAAW,EAAE,6EAA6E;IAC1F,KAAK,EAAE,cAAC,CAAC,MAAM,CAAC;QACZ,SAAS,EAAI,cAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,kBAAkB,CAAC;QACpD,UAAU,EAAG,cAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,oDAAoD,CAAC;QACtF,UAAU,EAAG,cAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,kBAAkB,CAAC;QACpD,WAAW,EAAE,cAAC,CAAC,OAAO,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,0CAA0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_command` runs through when the engine has no sandbox configured: the old
|
|
4
|
+
* `BK_RUN_COMMAND_SANDBOX` switch, translated to the equivalent policy. Deprecated: configure `sandbox` on the
|
|
5
|
+
* engine instead.
|
|
6
|
+
*
|
|
7
|
+
* - unset or `none`: the host, no confinement, the whole environment (exactly as before).
|
|
8
|
+
* - `docker`: the container backend with the workspace mounted read-write and the network closed. The
|
|
9
|
+
* availability probe is off on purpose: the legacy behaviour is to try `docker run` and report the failure,
|
|
10
|
+
* and it can never fall back to the host because the host backend is not in this policy.
|
|
11
|
+
* `BK_SANDBOX_NETWORK` other than `none` now means "open" (bridge). The command runs as the host user on POSIX.
|
|
12
|
+
*/
|
|
13
|
+
export type LegacySandbox = {
|
|
14
|
+
runner: SandboxRunner;
|
|
15
|
+
} | {
|
|
16
|
+
error: string;
|
|
17
|
+
};
|
|
18
|
+
export declare function legacySandbox(env?: NodeJS.ProcessEnv): LegacySandbox;
|
|
19
|
+
//# sourceMappingURL=legacy-sandbox.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"legacy-sandbox.d.ts","sourceRoot":"","sources":["../../src/tools/legacy-sandbox.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAGjE;;;;;;;;;;GAUG;AACH,MAAM,MAAM,aAAa,GAAG;IAAE,MAAM,EAAE,aAAa,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAI1E,wBAAgB,aAAa,CAAC,GAAG,GAAE,MAAM,CAAC,UAAwB,GAAG,aAAa,CAuBjF"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.legacySandbox = legacySandbox;
|
|
4
|
+
const agent_sandbox_1 = require("@backendkit-labs/agent-sandbox");
|
|
5
|
+
const cache = new Map();
|
|
6
|
+
function legacySandbox(env = process.env) {
|
|
7
|
+
const mode = (env.BK_RUN_COMMAND_SANDBOX ?? 'none').toLowerCase();
|
|
8
|
+
if (mode !== 'none' && mode !== 'docker') {
|
|
9
|
+
return { error: `Error: invalid BK_RUN_COMMAND_SANDBOX "${mode}" (expected "none" or "docker")` };
|
|
10
|
+
}
|
|
11
|
+
const network = (env.BK_SANDBOX_NETWORK ?? 'none') === 'none' ? 'none' : 'allow';
|
|
12
|
+
const key = mode === 'none' ? 'none' : `docker|${network}|${env.BK_SANDBOX_IMAGE ?? ''}|${env.BK_SANDBOX_MEMORY ?? ''}`;
|
|
13
|
+
let runner = cache.get(key);
|
|
14
|
+
if (!runner) {
|
|
15
|
+
runner = mode === 'none'
|
|
16
|
+
? (0, agent_sandbox_1.createSandboxService)({ policy: (0, agent_sandbox_1.createSandboxPolicy)({ preset: 'full' }), backends: [new agent_sandbox_1.NoneBackend()], scrubEnvironment: false })
|
|
17
|
+
: (0, agent_sandbox_1.createSandboxService)({
|
|
18
|
+
policy: (0, agent_sandbox_1.createSandboxPolicy)({ preset: 'ask', approval: 'none', network }),
|
|
19
|
+
backends: [new agent_sandbox_1.DockerBackend({
|
|
20
|
+
...(env.BK_SANDBOX_IMAGE ? { image: env.BK_SANDBOX_IMAGE } : {}),
|
|
21
|
+
...(env.BK_SANDBOX_MEMORY ? { memory: env.BK_SANDBOX_MEMORY } : {}),
|
|
22
|
+
probe: async () => true,
|
|
23
|
+
})],
|
|
24
|
+
});
|
|
25
|
+
cache.set(key, runner);
|
|
26
|
+
}
|
|
27
|
+
return { runner };
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=legacy-sandbox.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"legacy-sandbox.js","sourceRoot":"","sources":["../../src/tools/legacy-sandbox.ts"],"names":[],"mappings":";;AAkBA,sCAuBC;AAxCD,kEAAuH;AAevH,MAAM,KAAK,GAAG,IAAI,GAAG,EAAyB,CAAC;AAE/C,SAAgB,aAAa,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C,CAAC,CAAC,EAAE,CAAC;wBAChE,GAAG,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,iBAAiB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;wBACnE,KAAK,EAAE,KAAK,IAAI,EAAE,CAAC,IAAI;qBAC1B,CAAC,CAAC;aACN,CAAC,CAAC;QACP,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IAC3B,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,CAAC;AACtB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"list-directory.d.ts","sourceRoot":"","sources":["../../src/tools/list-directory.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"list-directory.d.ts","sourceRoot":"","sources":["../../src/tools/list-directory.ts"],"names":[],"mappings":"AAsBA,eAAO,MAAM,iBAAiB,sDAqB5B,CAAC"}
|
|
@@ -5,6 +5,7 @@ const fs_1 = require("fs");
|
|
|
5
5
|
const path_1 = require("path");
|
|
6
6
|
const agent_core_1 = require("@backendkit-labs/agent-core");
|
|
7
7
|
const path_sandbox_1 = require("./path-sandbox");
|
|
8
|
+
const sandbox_path_1 = require("./sandbox-path");
|
|
8
9
|
function listRecursive(dir, depth, maxDepth) {
|
|
9
10
|
if (depth > maxDepth)
|
|
10
11
|
return [];
|
|
@@ -34,6 +35,9 @@ exports.listDirectoryTool = (0, agent_core_1.defineTool)({
|
|
|
34
35
|
execute: async ({ path: inputPath, max_depth }, ctx) => {
|
|
35
36
|
try {
|
|
36
37
|
const abs = (0, path_sandbox_1.resolveAndCheck)(ctx.workingDir, inputPath, ctx.additionalRoots ?? []);
|
|
38
|
+
const denied = (0, sandbox_path_1.sandboxPathError)(ctx, 'list_directory', abs, 'read');
|
|
39
|
+
if (denied)
|
|
40
|
+
return denied;
|
|
37
41
|
const stat = (0, fs_1.statSync)(abs);
|
|
38
42
|
if (!stat.isDirectory())
|
|
39
43
|
return `${abs} (file, not a directory)`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"list-directory.js","sourceRoot":"","sources":["../../src/tools/list-directory.ts"],"names":[],"mappings":";;;AAAA,2BAA2C;AAC3C,+BAA4B;AAC5B,4DAA4D;AAC5D,iDAAiD;
|
|
1
|
+
{"version":3,"file":"list-directory.js","sourceRoot":"","sources":["../../src/tools/list-directory.ts"],"names":[],"mappings":";;;AAAA,2BAA2C;AAC3C,+BAA4B;AAC5B,4DAA4D;AAC5D,iDAAiD;AACjD,iDAAkD;AAElD,SAAS,aAAa,CAAC,GAAW,EAAE,KAAa,EAAE,QAAgB;IAC/D,IAAI,KAAK,GAAG,QAAQ;QAAE,OAAO,EAAE,CAAC;IAChC,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,KAAK,MAAM,KAAK,IAAI,IAAA,gBAAW,EAAC,GAAG,EAAE,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC;QAC5D,IAAI,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,KAAK,CAAC,IAAI,KAAK,cAAc;YAAE,SAAS;QAC1E,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAClC,IAAI,KAAK,CAAC,WAAW,EAAE,EAAE,CAAC;YACtB,KAAK,CAAC,IAAI,CAAC,GAAG,MAAM,GAAG,KAAK,CAAC,IAAI,GAAG,CAAC,CAAC;YACtC,KAAK,CAAC,IAAI,CAAC,GAAG,aAAa,CAAC,IAAA,WAAI,EAAC,GAAG,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE,KAAK,GAAG,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC;QAC7E,CAAC;aAAM,CAAC;YACJ,KAAK,CAAC,IAAI,CAAC,GAAG,MAAM,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;QACzC,CAAC;IACL,CAAC;IACD,OA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CAAC;IACL,CAAC;CACJ,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read-file.d.ts","sourceRoot":"","sources":["../../src/tools/read-file.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"read-file.d.ts","sourceRoot":"","sources":["../../src/tools/read-file.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,YAAY,sDAqBvB,CAAC"}
|
package/dist/tools/read-file.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.readFileTool = void 0;
|
|
|
4
4
|
const fs_1 = require("fs");
|
|
5
5
|
const agent_core_1 = require("@backendkit-labs/agent-core");
|
|
6
6
|
const path_sandbox_1 = require("./path-sandbox");
|
|
7
|
+
const sandbox_path_1 = require("./sandbox-path");
|
|
7
8
|
exports.readFileTool = (0, agent_core_1.defineTool)({
|
|
8
9
|
name: 'read_file',
|
|
9
10
|
concurrencySafe: true,
|
|
@@ -18,6 +19,9 @@ exports.readFileTool = (0, agent_core_1.defineTool)({
|
|
|
18
19
|
execute: async ({ file_path }, ctx) => {
|
|
19
20
|
try {
|
|
20
21
|
const abs = (0, path_sandbox_1.resolveAndCheck)(ctx.workingDir, file_path, ctx.additionalRoots ?? []);
|
|
22
|
+
const denied = (0, sandbox_path_1.sandboxPathError)(ctx, 'read_file', abs, 'read');
|
|
23
|
+
if (denied)
|
|
24
|
+
return denied;
|
|
21
25
|
return (0, fs_1.readFileSync)(abs, 'utf-8');
|
|
22
26
|
}
|
|
23
27
|
catch (err) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read-file.js","sourceRoot":"","sources":["../../src/tools/read-file.ts"],"names":[],"mappings":";;;AAAA,2BAAkC;AAClC,4DAA4D;AAC5D,iDAAiD;
|
|
1
|
+
{"version":3,"file":"read-file.js","sourceRoot":"","sources":["../../src/tools/read-file.ts"],"names":[],"mappings":";;;AAAA,2BAAkC;AAClC,4DAA4D;AAC5D,iDAAiD;AACjD,iDAAkD;AAErC,QAAA,YAAY,GAAG,IAAA,uBAAU,EAAC;IACnC,IAAI,EAAE,WAAW;IACjB,eAAe,EAAE,IAAI;IACrB,WAAW,EAAE;QACT,8BAA8B;QAC9B,4GAA4G;QAC5G,oGAAoG;KACvG,CAAC,IAAI,CAAC,GAAG,CAAC;IACX,KAAK,EAAE,cAAC,CAAC,MAAM,CAAC;QACZ,SAAS,EAAE,cAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,yDAAyD,CAAC;KAC5F,CAAC;IACF,OAAO,EAAE,KAAK,EAAE,EAAE,SAAS,EAAE,EAAE,GAAG,EAAE,EAAE;QAClC,IAAI,CAAC;YACD,MAAM,GAAG,GAAG,IAAA,8BAAe,EAAC,GAAG,CAAC,UAAU,EAAE,SAAS,EAAE,GAAG,CAAC,eAAe,IAAI,EAAE,CAAC,CAAC;YAClF,MAAM,MAAM,GAAG,IAAA,+BAAgB,EAAC,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;YAC/D,IAAI,MAAM;gBAAE,OAAO,MAAM,CAAC;YAC1B,OAAO,IAAA,iBAAY,EAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACtC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACX,OAAO,UAAW,GAAa,CAAC,OAAO,EAAE,CAAC;QAC9C,CAAC;IACL,CAAC;CACJ,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run-command.d.ts","sourceRoot":"","sources":["../../src/tools/run-command.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"run-command.d.ts","sourceRoot":"","sources":["../../src/tools/run-command.ts"],"names":[],"mappings":"AAwFA,eAAO,MAAM,cAAc,sDAsHzB,CAAC"}
|
|
@@ -1,43 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
3
|
exports.runCommandTool = void 0;
|
|
37
|
-
const child_process_1 = require("child_process");
|
|
38
|
-
const path = __importStar(require("path"));
|
|
39
4
|
const agent_core_1 = require("@backendkit-labs/agent-core");
|
|
40
5
|
const path_sandbox_1 = require("./path-sandbox");
|
|
6
|
+
const legacy_sandbox_1 = require("./legacy-sandbox");
|
|
41
7
|
const MAX_OUTPUT = 20_000;
|
|
42
8
|
/**
|
|
43
9
|
* Allowed base binaries. ALL binaries in a chained command (pipes, &&, ;)
|
|
@@ -115,51 +81,6 @@ const WIN_BIN_MAP = {
|
|
|
115
81
|
cat: 'type',
|
|
116
82
|
touch: 'type nul >>',
|
|
117
83
|
};
|
|
118
|
-
/**
|
|
119
|
-
* Docker sandbox: no network (unless BK_SANDBOX_NETWORK overrides), all caps
|
|
120
|
-
* dropped, only the working directory mounted. The command runs via `sh -c`
|
|
121
|
-
* inside the image (default node:22), so the image must ship the needed tools.
|
|
122
|
-
*/
|
|
123
|
-
function buildDockerArgs(name, baseDir, workDir, command) {
|
|
124
|
-
const rel = path.relative(baseDir, workDir).split(path.sep).join('/');
|
|
125
|
-
return [
|
|
126
|
-
'run', '--rm', '--name', name,
|
|
127
|
-
'--network', process.env.BK_SANDBOX_NETWORK ?? 'none',
|
|
128
|
-
'--cap-drop', 'ALL',
|
|
129
|
-
'--security-opt', 'no-new-privileges',
|
|
130
|
-
'--memory', process.env.BK_SANDBOX_MEMORY ?? '2g',
|
|
131
|
-
'--pids-limit', '512',
|
|
132
|
-
'-v', `${baseDir}:/workspace`,
|
|
133
|
-
'-w', rel ? `/workspace/${rel}` : '/workspace',
|
|
134
|
-
process.env.BK_SANDBOX_IMAGE ?? 'node:22',
|
|
135
|
-
'sh', '-c', command,
|
|
136
|
-
];
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Stops the command and everything it started. A plain proc.kill() leaves the children of a shell
|
|
140
|
-
* running (and on Windows does not reach them at all).
|
|
141
|
-
*/
|
|
142
|
-
function killProcessTree(proc, containerName) {
|
|
143
|
-
if (containerName) {
|
|
144
|
-
(0, child_process_1.spawn)('docker', ['kill', containerName], { stdio: 'ignore' }).on('error', () => undefined);
|
|
145
|
-
}
|
|
146
|
-
else if (process.platform === 'win32' && proc.pid) {
|
|
147
|
-
// taskkill must see the tree while the shell is alive (once cmd.exe dies Windows forgets its
|
|
148
|
-
// children), so the shell is not killed here: /F ends it together with the rest.
|
|
149
|
-
const tk = (0, child_process_1.spawn)('taskkill', ['/pid', String(proc.pid), '/T', '/F'], { stdio: 'ignore' });
|
|
150
|
-
tk.on('error', () => proc.kill());
|
|
151
|
-
tk.on('close', (code) => { if (code !== 0)
|
|
152
|
-
proc.kill(); });
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
else if (proc.pid) {
|
|
156
|
-
try {
|
|
157
|
-
process.kill(-proc.pid, 'SIGKILL');
|
|
158
|
-
}
|
|
159
|
-
catch { /* not a process group leader */ }
|
|
160
|
-
}
|
|
161
|
-
proc.kill();
|
|
162
|
-
}
|
|
163
84
|
exports.runCommandTool = (0, agent_core_1.defineTool)({
|
|
164
85
|
name: 'run_command',
|
|
165
86
|
description: 'Execute a shell command in the working directory. ' +
|
|
@@ -224,113 +145,57 @@ exports.runCommandTool = (0, agent_core_1.defineTool)({
|
|
|
224
145
|
`Use the appropriate MCP tool (e.g. pg_create, pg_run_sql) to accomplish this task.`);
|
|
225
146
|
}
|
|
226
147
|
}
|
|
227
|
-
//
|
|
228
|
-
//
|
|
229
|
-
|
|
230
|
-
if (
|
|
231
|
-
|
|
148
|
+
// Everything below runs through a sandbox: the engine's, or the legacy BK_RUN_COMMAND_SANDBOX switch
|
|
149
|
+
// translated to its equivalent policy. Fail closed in both: if confinement cannot be enforced, nothing runs.
|
|
150
|
+
let runner = ctx.sandbox;
|
|
151
|
+
if (!runner) {
|
|
152
|
+
const legacy = (0, legacy_sandbox_1.legacySandbox)();
|
|
153
|
+
if ('error' in legacy)
|
|
154
|
+
return legacy.error;
|
|
155
|
+
runner = legacy.runner;
|
|
232
156
|
}
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
157
|
+
const caller = { agentId: ctx.agentId, sessionId: ctx.sessionId };
|
|
158
|
+
// `command` is what a container runs; `hostCommand` has the Windows remap for the host shell.
|
|
159
|
+
const request = { tool: 'run_command', command: realCommand, hostCommand: effectiveCommand, cwd: workDir, workspace: baseDir };
|
|
236
160
|
if (background && ctx.jobs) {
|
|
237
161
|
const info = ctx.jobs.start({
|
|
238
162
|
kind: 'bash',
|
|
239
163
|
owner: ctx.agentId,
|
|
240
|
-
run: (signal, append) =>
|
|
241
|
-
const
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
signal.addEventListener('abort', kill, { once: true });
|
|
251
|
-
proc.on('error', reject);
|
|
252
|
-
proc.on('close', (code) => {
|
|
253
|
-
signal.removeEventListener('abort', kill);
|
|
254
|
-
if (code === 0)
|
|
255
|
-
resolve('exit 0');
|
|
256
|
-
else
|
|
257
|
-
reject(new Error(`exit ${code}`));
|
|
258
|
-
});
|
|
259
|
-
}),
|
|
164
|
+
run: async (signal, append) => {
|
|
165
|
+
const out = await runner.run(caller, { ...request, signal, onOutput: append });
|
|
166
|
+
if (!out.ok)
|
|
167
|
+
throw new Error(`${out.code}: ${out.reason}`);
|
|
168
|
+
if (out.result.error)
|
|
169
|
+
throw new Error(out.result.error);
|
|
170
|
+
if (out.result.exitCode === 0)
|
|
171
|
+
return 'exit 0';
|
|
172
|
+
throw new Error(`exit ${out.result.exitCode}`);
|
|
173
|
+
},
|
|
260
174
|
});
|
|
261
175
|
return `Started background job ${info.id}. Read its output with job_output (id: "${info.id}"); stop it with job_kill.`;
|
|
262
176
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
proc.stdout.on('data', collect);
|
|
286
|
-
proc.stderr.on('data', collect);
|
|
287
|
-
const killTree = () => killProcessTree(proc, containerName);
|
|
288
|
-
const signal = ctx.signal;
|
|
289
|
-
let aborted = false;
|
|
290
|
-
const onAbort = () => {
|
|
291
|
-
if (timedOut || aborted)
|
|
292
|
-
return;
|
|
293
|
-
aborted = true;
|
|
294
|
-
clearTimeout(timer);
|
|
295
|
-
killTree();
|
|
296
|
-
resolve('Error: Command aborted');
|
|
297
|
-
};
|
|
298
|
-
const timer = setTimeout(() => {
|
|
299
|
-
timedOut = true;
|
|
300
|
-
signal?.removeEventListener('abort', onAbort);
|
|
301
|
-
killTree();
|
|
302
|
-
resolve(`Error: Command timed out after ${timeout_ms}ms`);
|
|
303
|
-
}, timeout_ms);
|
|
304
|
-
if (signal?.aborted)
|
|
305
|
-
onAbort();
|
|
306
|
-
else
|
|
307
|
-
signal?.addEventListener('abort', onAbort, { once: true });
|
|
308
|
-
proc.on('error', (err) => {
|
|
309
|
-
clearTimeout(timer);
|
|
310
|
-
signal?.removeEventListener('abort', onAbort);
|
|
311
|
-
resolve(`Error: ${err.message}`);
|
|
312
|
-
});
|
|
313
|
-
proc.on('close', (code) => {
|
|
314
|
-
signal?.removeEventListener('abort', onAbort);
|
|
315
|
-
if (timedOut || aborted)
|
|
316
|
-
return;
|
|
317
|
-
clearTimeout(timer);
|
|
318
|
-
const raw = chunks.join('').trim();
|
|
319
|
-
const truncated = raw.length > MAX_OUTPUT
|
|
320
|
-
? raw.slice(0, MAX_OUTPUT) + `\n[truncated — ${raw.length} total chars]`
|
|
321
|
-
: raw;
|
|
322
|
-
if (containerName && code !== 0) {
|
|
323
|
-
resolve(`Error: sandboxed command exited with status ${code}\n${truncated}`.trim());
|
|
324
|
-
}
|
|
325
|
-
else if (code !== 0 && !truncated) {
|
|
326
|
-
resolve(`Error: Command exited with status ${code}`);
|
|
327
|
-
}
|
|
328
|
-
else {
|
|
329
|
-
resolve(truncated || '(no output)');
|
|
330
|
-
}
|
|
331
|
-
});
|
|
332
|
-
});
|
|
333
|
-
return output;
|
|
177
|
+
const out = await runner.run(caller, { ...request, timeoutMs: timeout_ms, signal: ctx.signal });
|
|
178
|
+
if (!out.ok)
|
|
179
|
+
return `Error: ${out.code}: ${out.reason}`;
|
|
180
|
+
const r = out.result;
|
|
181
|
+
if (r.error)
|
|
182
|
+
return `Error: ${r.error}`;
|
|
183
|
+
if (r.aborted)
|
|
184
|
+
return 'Error: Command aborted';
|
|
185
|
+
if (r.timedOut)
|
|
186
|
+
return `Error: Command timed out after ${timeout_ms}ms`;
|
|
187
|
+
const raw = r.output.trim();
|
|
188
|
+
const truncated = raw.length > MAX_OUTPUT
|
|
189
|
+
? raw.slice(0, MAX_OUTPUT) + `
|
|
190
|
+
[truncated — ${raw.length} total chars]`
|
|
191
|
+
: raw;
|
|
192
|
+
if (out.backend !== 'none' && r.exitCode !== 0) {
|
|
193
|
+
return `Error: sandboxed command exited with status ${r.exitCode}
|
|
194
|
+
${truncated}`.trim();
|
|
195
|
+
}
|
|
196
|
+
if (r.exitCode !== 0 && !truncated)
|
|
197
|
+
return `Error: Command exited with status ${r.exitCode}`;
|
|
198
|
+
return truncated || '(no output)';
|
|
334
199
|
},
|
|
335
200
|
});
|
|
336
201
|
//# sourceMappingURL=run-command.js.map
|