Local Sandbox Tools#
Before involving a model, look at where tools actually execute. This page opens LocalBackend directly, dispatches file, command, and code payloads by hand, and shows how they operate around the same local workspace.
Coverage#
Topic |
Result |
|---|---|
Backend registry |
How |
Sandbox handle |
A local sandbox maps to one working directory. |
Backend payload |
File, command, and code execution are represented by |
Structured result |
Each dispatch returns a structured object. |
Lifecycle |
|
Step 1: Open the Local Backend#
from rath.backend import get
backend = get("local")
sandbox = backend.open()
print(backend.name)
print(backend.capabilities())
print(sandbox.handle)
Key lines:
Line |
Explanation |
|---|---|
|
Gets the local Backend instance from the Backend registry. |
|
Creates a |
|
The workspace path managed by the local Backend. |
Observed behavior:
backend.nameislocal.sandbox.handleis a local path.If no
working_diris passed, the local Backend creates a temporary directory.
Step 2: Write and Read a File#
from rath.backend import BackendToolFilesRead, BackendToolFilesWrite
write_result = sandbox.dispatch(
BackendToolFilesWrite(path="hello.txt", data="hello OpenRath")
)
content = sandbox.dispatch(
BackendToolFilesRead(path="hello.txt", encoding="utf-8")
)
print(write_result)
print(content)
Key lines:
Line |
Explanation |
|---|---|
|
Describes a file write without executing it directly. |
|
Sends the payload to the current sandbox for execution. |
|
Reads the file just written in the same workspace. |
Observed behavior:
The write result includes the number of bytes written.
The read result includes the file content.
The relative path
hello.txtis resolved fromsandbox.handle.
Step 3: Run a Shell Command#
from rath.backend import BackendToolCommandRun
result = sandbox.dispatch(
BackendToolCommandRun(cmd="pwd && cat hello.txt")
)
print(result.exit_code)
print(result.stdout.decode())
print(result.stderr.decode())
Key lines:
Line |
Explanation |
|---|---|
|
Describes a shell command execution. |
|
Lets the caller decide whether the command succeeded. |
|
The current implementation stores output as bytes, so callers need to decode it. |
Observed behavior:
pwdprints the directory for the local sandbox workspace.cat hello.txtcan read the content written in the previous step.When a command fails, check
exit_codeandstderrfirst.
Step 4: Run Python Code#
from rath.backend import BackendToolCodeRun
result = sandbox.dispatch(
BackendToolCodeRun(code="print(21 * 2)")
)
print(result.stdout.decode())
print(result.stderr.decode())
print(result.error)
The current local Backend writes the code to a temporary Python file, then runs it with the current Python interpreter. This is useful for checking tool paths and script behavior. Use a stricter isolated Backend for untrusted code.
Step 5: Close the Sandbox#
backend.close(sandbox)
print(sandbox.closed)
Key points:
Behavior |
Notes |
|---|---|
|
Closes the sandbox handle. |
Local workspace cleanup |
The local Backend cleans up directories it manages. |
Bound directory risk |
When binding a real directory, make sure it can be recreated to reduce the risk of deleting important content. |
Troubleshooting#
Symptom |
Check |
|---|---|
|
Confirm OpenRath is installed and |
File cannot be read |
Confirm the write and read happen on the same sandbox. |
Command has no output |
Print |
Dispatch fails after close |
Re-run |
Exercises#
Change
hello.txttonotes/hello.txtand observe whether the directory is created automatically.Rewrite the shell command so it lists all files under the workspace.
Change the Python code so it reads
hello.txtand prints its length.
Summary#
BackendTool*payloads describe Backend-side operations.BackendSandbox.dispatch(...)executes a payload and returns a structured result.File, command, and code payloads operate around the same sandbox workspace.
Built-in tools in the Session loop eventually run through this Backend dispatch layer.