Class: SandboxedToolForge
Defined in: packages/agentos/src/emergent/SandboxedToolForge.ts:138
Runs agent-generated code in an isolated sandbox with strict resource limits.
Attempts to use isolated-vm for true V8 isolate sandboxing. Falls back to
Node.js vm module with timeout if isolated-vm is not installed.
Resource limits:
- Memory: configurable, default 128 MB
- Execution time: configurable, default 5000 ms
- Blocked APIs: eval, Function, process, require, import, child_process, fs.write*
Allowlisted APIs (each requires explicit opt-in):
fetch: HTTP requests (domain-restricted)fs.readFile: Read-only file access (path-restricted, max 1 MB)crypto: Hashing and HMAC only
Example
const forge = new SandboxedToolForge({ timeoutMs: 3000 });
const result = await forge.execute({
code: 'function execute(input) { return input.a + input.b; }',
input: { a: 2, b: 3 },
allowlist: [],
memoryMB: 128,
timeoutMs: 3000,
});
console.log(result.output); // 5
Constructors
Constructor
new SandboxedToolForge(
config?):SandboxedToolForge
Defined in: packages/agentos/src/emergent/SandboxedToolForge.ts:157
Create a new SandboxedToolForge instance.
Parameters
config?
Optional configuration overrides. All fields have sensible defaults (128 MB memory, 5000 ms timeout, no domain restrictions).
Returns
SandboxedToolForge
Methods
execute()
execute(
request):Promise<SandboxExecutionResult>
Defined in: packages/agentos/src/emergent/SandboxedToolForge.ts:277
Execute agent-generated code in the sandbox.
The code must define a function named execute that accepts a single
argument and returns the output:
function execute(input) { return input.a + input.b; }
Execution flow:
- Run validateCode — reject immediately if violations are found.
- Wrap the agent's code into a self-contained expression that calls
execute. - Run in a Node.js
vmsandbox with a restricted global context. - Parse the output, measure execution time, and return the result.
Parameters
request
The execution request containing code, input, allowlist, and resource limits.
Returns
Promise<SandboxExecutionResult>
A SandboxExecutionResult with the output (on success) or error description (on failure), plus execution time telemetry.
Example
const result = await forge.execute({
code: 'function execute(input) { return { sum: input.a + input.b }; }',
input: { a: 10, b: 20 },
allowlist: [],
memoryMB: 128,
timeoutMs: 5000,
});
// result.success === true
// result.output === { sum: 30 }
validateCode()
validateCode(
code,allowlist):object
Defined in: packages/agentos/src/emergent/SandboxedToolForge.ts:200
Static analysis of code — reject dangerous patterns before execution.
Scans the source string for banned API usage patterns using regex matching. If an API is not present in the allowlist, references to it are also flagged.
Checked patterns (always banned):
eval(),new Function(),require(),import,process.*child_process,fs.write*,fs.unlink,fs.rm,fs.rmdir
Conditionally banned (when not in allowlist):
fetch(— when'fetch'is not in the allowlistfs.*— when'fs.readFile'is not in the allowlistcrypto.*— when'crypto'is not in the allowlist
Parameters
code
string
The raw source code string to validate.
allowlist
The set of APIs the code is permitted to use.
Returns
object
An object with valid: true if no violations were found, or
valid: false with a violations array describing each flagged pattern.
valid
valid:
boolean
violations
violations:
string[]
Example
const forge = new SandboxedToolForge();
const result = forge.validateCode('eval("exploit")', []);
// result.valid === false
// result.violations === ['eval() is forbidden']