Sandbox Agent

repository·main·Indexed 23 days ago

https://github.com/rivet-dev/sandbox-agent

A tool for running coding agents such as Claude Code, Codex, Pi, and Amp inside isolated sandboxes, controllable via a universal HTTP/SSE API. It includes a TypeScript SDK for embedded and server modes, a Rust-based standalone daemon, and Gigacode, a CLI that bridges the OpenCode interface with specialized coding agent tools.

Tokens
172.4K
Snippets
342
Records
702
Agent score
80%

What's inside Sandbox Agent

  1. Use the Inspector for debugging and development

    main

    The Inspector provides several tools for interacting with agent sessions:

    • Session Management: View a list of active sessions and manage processes (create, stop, kill, delete, and view logs).
    • Event Inspection: View the event stream and inspect raw JSON payloads for requests and responses.
    • Interactive Controls: Approve, always-allow, or reject tool-use requests via interactive permission prompts.
    • Terminal Access: Use an embedded Ghostty-based terminal for interactive TTY processes.
    • Desktop Management: Use the Desktop panel to check dependencies, start/stop the desktop runtime, refresh status, and capture screenshots.
    • Testing: Perform prompt testing and one-shot command execution.
  2. Use @sandbox-agent/mock-acp-agent for testing

    main

    The @sandbox-agent/mock-acp-agent is a minimal ACP (Agent Communication Protocol) mock agent that uses newline-delimited JSON-RPC. It is designed for testing agent communication flows by simulating specific agent and client behaviors.

    Mocked Behaviors

    • Echoing: Every inbound message is echoed back as a mock/echo notification.
    • Request Handling: For standard requests (containing both method and id), the agent returns a payload where result.echoed contains the original message.
    • Client Interaction (mock/ask_client): When the agent receives a mock/ask_client method, it simulates an agent-initiated interaction by emitting a mock/request notification before providing its response.
    • Client Responses: If the client sends a message with an id but no method (representing a response to a previous request), the agent emits a mock/client_response notification.
  3. Data Model: WorkbenchTask Summary vs Detail

    main

    To optimize performance, WorkbenchTask is split into two distinct interfaces located in packages/shared/src/workbench.ts:

    WorkbenchTaskSummary

    Used for sidebar-level data. This is materialized in the organization actor's SQLite database to allow for fast, single-query initial fetches of the entire workspace.

    • id, repoId, title, status, repoName, updatedAtMs, branch, pullRequest, sessionsSummary.

    WorkbenchTaskDetail

    Used for full task views. This contains heavy data like file trees and diffs and is only fetched or broadcasted when a user is actively viewing a specific task.

    • Includes all summary fields plus: fileChanges, diffs, fileTree, minutesUsed, sandboxes, and activeSandboxId.
  4. Understand AgentCapabilities in ACP v1

    main

    The AgentCapabilities object (found in initialize.result.agentCapabilities) defines what an agent can do. Below are the mappings for common capabilities:

    CapabilityACP Implementation / Path
    mcpToolsinitialize.result.agentCapabilities.mcpCapabilities
    imagesinitialize.result.agentCapabilities.promptCapabilities.image
    permissionssession/request_permission (agent $\rightarrow$ client request)
    reasoningsession/update.params.update.sessionUpdate=agent_thought_chunk
    streamingDeltassession/update.params.update.sessionUpdate=agent_message_chunk
    toolCallssession/update.params.update.sessionUpdate=tool_call
    toolResultssession/update.params.update.sessionUpdate=tool_call_update
    textMessagesContentBlock.type=text
    statussession/update.params.update.tool_call.status
  5. VCS Integration Core Functionality

    main

    The VCS integration provides the following capabilities for managing repository state within a session:

    • Repo discovery: Automatically identifies the repository from the session directory with safe fallback mechanisms.
    • Status summary: Provides information on the current branch, dirty files, and whether the local branch is ahead or behind the remote.
    • Diff generation: Supports generating diffs for staged/unstaged changes, both per-file and for the full repository.
    • Revert/unrevert mechanics: Implements state management using temporary snapshots or stashes to allow users to undo or redo changes.
    • File status integration: Integrates with existing file status endpoints when available.
  6. What telemetry data is collected by sandbox-agent

    main

    sandbox-agent sends an anonymous telemetry payload on startup and every 5 minutes to assist with usage analysis and reliability improvements.

    Collected data includes:

    • Sandbox Agent version.
    • OS name, architecture, and OS family.
    • Detected sandbox provider (e.g., Docker, E2B, Vercel Sandboxes).

    To maintain anonymity, each sandbox is assigned a random anonymous ID stored on disk. The last successful send time is also stored locally to ensure heartbeats are rate-limited to at most one every 5 minutes.

  7. The lifecycle of SandboxAgent.start()

    main

    When you call SandboxAgent.start(), the following sequence occurs:

    1. Provision: The provider creates the sandbox (e.g., starts a container or creates a VM).
    2. Install: The Sandbox Agent binary is installed inside the sandbox.
    3. Boot: The server starts listening on an HTTP port.
    4. Health check: The SDK polls the /v1/health endpoint.
    5. Ready: Once the health check passes, the SDK returns a connected client.

    Note: For the local provider, provisioning is a no-op and the server runs as a local subprocess.

  8. Understand instruction priority for Claude and Codex

    main

    When configuring agents via sandbox-agent, be aware that Claude and Codex handle instruction priority differently. This affects how appendSystemPrompt and projectInstructions interact.

    • Claude: The system prompt additions (via --append-system-prompt) have the highest priority, followed by the base prompt, and finally the CLAUDE.md file.
    • Codex: The AGENTS.md project file has the highest priority, followed by developer_instructions, and finally the base prompt.

    Implication: In Claude, system prompt additions override project files. In Codex, project files override system prompt additions. It is recommended to use only one mechanism to avoid unexpected behavior.

  9. Understand File System path resolution

    main

    The Sandbox Agent filesystem API uses the following rules for path resolution:

    • Absolute paths: Used as-is.
    • Relative paths: Resolve from the server process working directory.
    • Security: Any requests attempting to escape the allowed root directories are rejected by the server.
  10. Design recommendations for PTY and Command transport

    main

    The sandbox-agent design distinguishes between interactive terminal sessions (PTY) and request-response execution (Commands) to optimize for latency and complexity:

    • PTY (Pseudo-Terminal): Use WebSockets for bidirectional, low-latency, and long-lived interactive I/O (e.g., for terminal emulators like xterm.js). PTYs are treated as unified streams where stdout and stderr are merged by the kernel.
    • Commands: Use REST for request-response operations (e.g., running ls -la). Commands are suitable for non-interactive execution where the client sends a command and receives a structured JSON response containing stdout, stderr, and an exit_code.
    • Streaming Output: For long-running commands that require streaming output but not interactive input, use SSE (Server-Sent Events). If interaction (like ctrl+c) is required, the user should switch to a PTY.