Guides
Agents & tools
Autonomous agent loops, tool policy gates, the in-process sandbox, and the trust boundaries that apply.
On this page7 sections
sofuu.agent defines and runs the agent loop: a named definition, a task, a tool set, a budget, and a trace. Runs can be cancelled, listed, and inspected, and every tool call passes through validation and policy before it executes.
Define an agent
sofuu.agent.define({
name: "researcher",
system: "You research accurately. Cite sources with URLs.",
tools: ["web", "memory", "system"],
memory: "agent",
approve: "sensitive",
budget: { maxSteps: 12, maxWallMs: 120_000 },
});
define(def) registers a definition and returns a handle. run also accepts a definition object directly.
| Field | Meaning |
|---|---|
name | Required registry name. |
system | The system prompt carried by the definition. |
tools | Tool names, domain packs, inline tool objects, or connected MCP clients. |
memory | "shared", "agent", or "off". Scopes over one physical brain file. |
provider, model | Optional per-agent backend selection. |
budget | maxSteps, maxDepth, maxTokens, and maxWallMs. |
approve | "none", "sensitive", or "all". |
agents | Names of sub-agents this definition may delegate to. |
rlm | "auto", "on", or "off" for long-context routing. |
identity | Optional { role, expertise } metadata. |
Definitions live in memory and can also be loaded from ~/.sofuu/agents/*.js with sofuu.agent.loadDir(). Those files are trusted scripts: they execute in global scope like any other user script.
Run, cancel, and inspect
const result = await sofuu.agent.run(
"researcher",
"Compare ARM NEON and AVX2",
);
console.log(result.answer);
console.log(result.stopped, result.usage);
run(handleOrNameOrDef, task, opts) returns a promise. Options include context, history, signal, onStep, onApproval, toolTimeoutMs, and per-run provider or model overrides.
| Result field | Meaning |
|---|---|
answer | Final text produced by the run. |
steps | Step records for the run. |
subRuns | Nested runs started by delegation. |
usage | Token usage accumulated across the run. |
trace | The structured event trace. |
stopped | Why the run ended, including cancellation. |
runId, name | Identity of the run and its definition. |
sofuu.agent.cancel(id) marks the run cancelled and fires its abort handlers; it returns false when the id is not active. sofuu.agent.list() returns registry summaries with tool counts, MCP server counts, sub-agents, and 24-hour run counts. sofuu.agent.renderTrace(result) renders the trace as text for logs or host display. sofuu.agent.runMany() and sofuu.agent.mapContext() are available for concurrent runs and context reduction.
Tools
| Source | How it is added |
|---|---|
| Inline tools | An object with name, description, parameters, and execute(args). |
| Domain toolpacks | A pack name such as "coding", "web", "memory", or "system". |
| MCP | A connected client in tools, or a server configured in ~/.sofuu/mcp.json. |
| Plugins | Tools registered by defineTool, subject to the plugin pipeline. |
| Host tools | Native tools registered through the C ABI. |
| Pack | Tools | Notes |
|---|---|---|
coding | read_file, write_file, edit_file, apply_patch, grep, glob, list_dir, bash, code_eval, todo_write | Filesystem tools use the project jail. SOFUU_NO_SHELL=1 removes bash. |
web | web_search, web_open | Keyless search and readable page fetching. |
memory | memory_search, memory_remember | Recall and persist against the local QTSQ brain. |
system | system_info | Runtime version, platform, CPU, GPU engine, RAM, and capabilities. |
Non-coding agents receive no coding tools by default. Agents opt in with a pack, individual tool names, or "all".
Policy and approvals
Validation runs before permission, policy, and execution. A bad argument returns E_SCHEMA and the tool never runs. The coding reader jail checks both the lexical path and the real path, and refuses a symlink inside the project that points outside it. bash is filtered by SOFUU_NO_SHELL, tools.deny, shell.enabled, and shell.allow; an allowlist judges every chain segment, not just the first command.
sofuu.tools.list() reports each tool's policy.allowed and requiresApproval flags, so a host can render the same inventory the model sees. Agent-level netPolicy matches the URL host only, and the infrastructure SSRF guards remain underneath it.
Approval decisions follow the definition and the active policy:
approve: "sensitive"prompts for tools outside the read-only auto-pass set;"all"prompts for every tool;"none"relies on policy.opts.onApproval(tool, args)or the host approval channel decides. The loop emitsapproval_request {id, tool, args, risky, mode}.- Policy is authoritative over the definition.
approval.mode: "always"prompts even forapprove: "none"and bypasses the session cache;"never"suppresses the gate; a deny always wins. Each decision emits apolicyevent with a rule id. - A missing answer, a callback error, a cancellation, or the tool timeout all deny. An
alwaysgrant applies only to the rest of that run, and bash grants are command-scoped.
Hosts that park prompts can settle them explicitly:
const pending = sofuu.agent.pendingApprovals(); // [{ id, tool }]
const settled = sofuu.agent.resolveApproval(pending[0].id, true, false);
resolveApproval(id, allow, always) returns {ok:true} or {ok:false, error}. The same two operations are exposed through the C funnel as agent.pendingApprovals and agent.resolveApproval.
Sandbox and enforcement limits
sofuu.sandbox.run(source, options) evaluates JavaScript or TypeScript in a separate QuickJS runtime with its own heap and intrinsics.
const result = await sofuu.sandbox.run(source, {
preset: "standard",
fuel: 20_000_000,
timeoutMs: 10_000,
maxMemoryMB: 64,
});
| Preset | Workload | Deadline | Fuel | Memory |
|---|---|---|---|---|
strict | Untrusted model-generated code | 2 s | 5M instructions | 32 MB |
standard | Developer scripting | 10 s | 20M instructions | 64 MB |
compute | Math, simulations, ETL | 300 s | unlimited | 512 MB |
worker | Long-running background work | none | unlimited | 256 MB |
The result carries success, result, errorType, code, fuelUsed, elapsedMs, and captured logs. A deadline violation carries E_TIMEOUT.
The sandbox is a separate runtime with no host globals, no I/O, no process, no module system, no subprocess, and no route to sofuu.* unless you bridge a value into globals. It is not a defence against native extensions (there are none), a multi-tenant scheduler, or a module loader. It also cannot interrupt a bridged host function that blocks; the deadline meters sandbox execution, not the host callback.
Tool policy applies at the tool-call boundary. Code inside the sandbox does not inherit the host tool inventory or its policy decisions unless you bridge a function that performs those calls. Plugins remain in-process and are not made safe by the sandbox. No layer here is a universal isolation boundary.
Execution status
Local inference is an experimental preview. Selected embedded Kaze paths have recorded real model-and-authorized-MCP journeys on local debug builds, including one using existing local weights. That evidence does not establish every provider, released artifact or application. Verify each example's backend and execution path before claiming an end-to-end result.