sandbox

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sandbox defines the driver interface the core uses to run untrusted code (repo code, model-written code) away from its own credentials.

The core never executes a repo command itself: it asks a Driver for a Sandbox and talks to it through Exec and Copy. A sandbox sees placeholder tokens only; real credentials are injected by the egress proxy on allowlisted hosts (see internal/proxy).

Drivers stay thin, so a breaking change in an upstream (experimental) API stays inside one adapter. Every driver must pass test/conformance.

Index

Constants

View Source
const (
	DefaultExecTimeout = 10 * time.Minute
	DefaultMaxOutput   = 64 << 10
)

Defaults for ExecRequest.

View Source
const WorkspacePath = "/workspace"

WorkspacePath is the default mount point of the workspace.

Variables

View Source
var ErrNotFound = errors.New("sandbox: not found")

ErrNotFound is returned by Get and Destroy for an unknown sandbox ID.

View Source
var ErrUnavailable = errors.New("sandbox: driver unavailable")

ErrUnavailable is returned by a driver whose backend is not usable on this host (no sbx binary, no hardware virtualization, no cluster access).

Functions

func CapQuantity

func CapQuantity(want, limit string) (string, bool, error)

CapQuantity returns the smaller of want and limit, two quantities in Kubernetes syntax, and whether that is limit. When either is empty it returns want unchanged, without parsing it.

func Placeholder

func Placeholder(name string) string

Placeholder is the token a sandbox sees for the named credential.

Types

type CredentialRef

type CredentialRef struct {
	Name  string   `json:"name" yaml:"name"`
	Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
	// Scope documents what the credential is for ("contents:read"). It is
	// not enforced from here: a forge's clone credential is read-only by
	// construction (see HostCredential.ReadOnly), whatever a kit writes.
	Scope string `json:"scope,omitempty" yaml:"scope,omitempty"`
}

CredentialRef names a credential the deployment holds. The dashboard and the kit files only ever carry these names, never a secret.

type CredentialSource

type CredentialSource interface {
	Resolve(ctx context.Context, sessionID string, ref CredentialRef) ([]HostCredential, error)
}

CredentialSource resolves credential references into header values. The core implements it (it holds the forge tokens); drivers call it from Create and ApplyPolicy and hand the result to the proxy.

type CredentialSourceFunc

type CredentialSourceFunc func(ctx context.Context, sessionID string, ref CredentialRef) ([]HostCredential, error)

CredentialSourceFunc adapts a function to a CredentialSource.

func (CredentialSourceFunc) Resolve

func (f CredentialSourceFunc) Resolve(ctx context.Context, sessionID string, ref CredentialRef) ([]HostCredential, error)

Resolve calls f.

type Driver

type Driver interface {
	// Name is the driver's key in kojo.yaml: sbx, k8s, e2b, docker or fake.
	Name() string
	// Create starts a sandbox and waits until Exec works in it. A driver
	// that cannot give a sandbox its own Docker daemon refuses a spec with
	// NeedsDocker by returning ErrUnavailable (wrapped).
	Create(ctx context.Context, spec SandboxSpec) (Sandbox, error)
	// Get finds a sandbox created earlier, possibly by another process. It
	// returns ErrNotFound when the sandbox is gone.
	Get(ctx context.Context, id string) (Sandbox, error)
	// Destroy removes a sandbox and its workspace. Destroying an unknown ID
	// returns ErrNotFound.
	Destroy(ctx context.Context, id string) error
	// Health reports whether the backend is usable and how it isolates.
	Health(ctx context.Context) (Health, error)
}

Driver creates and finds sandboxes on one backend.

type ExecRequest

type ExecRequest struct {
	// Command is the argv. When empty, Script runs under `sh -lc`.
	Command []string
	Script  string
	// Dir is the working directory. Default: the workspace path.
	Dir   string
	Env   map[string]string
	Stdin io.Reader
	// Stdout and Stderr receive output as it is produced.
	Stdout io.Writer
	Stderr io.Writer
	// Timeout bounds the command. Zero means DefaultExecTimeout. A command
	// that times out is killed, its child processes included, and reported
	// with ExitCode 124 and TimedOut.
	Timeout time.Duration
	// MaxOutput caps the captured Stdout and Stderr, each. Zero means
	// DefaultMaxOutput. Output beyond it is dropped from the middle; the
	// cap is hard, the marker that replaces the dropped part included.
	MaxOutput int
}

ExecRequest is one command.

func (ExecRequest) Argv

func (r ExecRequest) Argv() []string

Argv returns the command to run: Command, or Script under sh.

type ExecResult

type ExecResult struct {
	ExitCode int
	Stdout   string
	Stderr   string
	TimedOut bool
	Duration time.Duration
}

ExecResult is what a command produced.

type Health

type Health struct {
	Isolation Isolation
	// Version of the backend (sbx CLI, Agent Sandbox controller, Docker).
	Version string
	// Docker reports whether sandboxes can run their own Docker daemon.
	Docker bool
	// EgressEnforced reports whether a sandbox has no route out except the
	// proxy. False means the allowlist is advisory (a dev setup), which kojo
	// doctor refuses outside dev mode.
	EgressEnforced bool
	// Detail is one human-readable line.
	Detail string
}

Health is what kojo doctor reports about a driver.

type HostCredential

type HostCredential struct {
	Name   string `json:"name"`
	Host   string `json:"host"`
	Header string `json:"header"` // usually Authorization
	Value  string `json:"value"`  // full header value, e.g. "Bearer ghs_..."
	// Placeholder is what the sandbox sees instead of the secret.
	Placeholder string    `json:"placeholder"`
	Expires     time.Time `json:"expires,omitempty"`
	// ReadOnly limits where the proxy sets the credential to requests that
	// cannot write: GET, HEAD and OPTIONS, and git's fetch (a POST to
	// .../git-upload-pack). Every forge's clone credential sets it, so a
	// token that could write (GitLab's and Forgejo's are the configured
	// token) still cannot be used from a sandbox to push or to call a
	// writing API. Docker Sandboxes' own proxy cannot enforce it.
	ReadOnly bool `json:"read_only,omitempty"`
}

HostCredential is a resolved credential for one host: the header the proxy sets on requests to it.

type Isolation

type Isolation string

Isolation is how strongly a driver separates a sandbox from its host.

const (
	// IsolationMicroVM is a dedicated kernel per sandbox (sbx, Kata).
	IsolationMicroVM Isolation = "microvm"
	// IsolationGVisor is a user-space kernel (runsc).
	IsolationGVisor Isolation = "gvisor"
	// IsolationContainer is a plain container sharing the host kernel. It is
	// for development only: kojo doctor refuses it outside dev mode.
	IsolationContainer Isolation = "container"
	// IsolationNone is the in-memory fake used by unit tests.
	IsolationNone Isolation = "none"
)

func (Isolation) Secure

func (i Isolation) Secure() bool

Secure reports whether the isolation level is acceptable outside dev mode.

type Policy

type Policy struct {
	// EgressAllow lists hostnames. "*.example.com" matches subdomains.
	// Everything else is denied.
	EgressAllow []string `json:"egress_allow,omitempty" yaml:"egress_allow,omitempty"`
	// Credentials are injected by the proxy, never mounted.
	Credentials []CredentialRef `json:"credentials,omitempty" yaml:"credentials,omitempty"`
}

Policy is the egress allowlist and the credentials a sandbox may use.

type Resources

type Resources struct {
	// CPU in cores ("4", "0.5") or millicores ("500m").
	CPU string `json:"cpu,omitempty" yaml:"cpu,omitempty"`
	// Memory in bytes with a binary or decimal suffix ("8Gi", "512Mi").
	Memory string `json:"memory,omitempty" yaml:"memory,omitempty"`
	// Disk is the size of the workspace ("40Gi").
	Disk string `json:"disk,omitempty" yaml:"disk,omitempty"`
}

Resources are requests, in Kubernetes quantity syntax ("4", "8Gi").

func (Resources) Capped

func (r Resources) Capped(limit Resources) (capped Resources, lowered []string, err error)

Capped bounds r by limit, quantity by quantity: a CPU, memory or disk that r asks for and that is more than limit's becomes limit's. A quantity that limit leaves empty, or that r does not ask for, stays as it is: the cap lowers what a kit asks for and never adds a request. lowered describes each quantity it lowered ("cpu 8 -> 1"), for the driver's log.

func (Resources) Describe

func (r Resources) Describe() string

Describe names the CPU and memory of r for an error message: "cpu 4, memory 8Gi", or "no cpu or memory request".

func (Resources) Validate

func (r Resources) Validate() error

Validate checks that every quantity r names parses and is more than zero.

type Sandbox

type Sandbox interface {
	ID() string
	// Exec runs a command and waits for it. Output is streamed to the
	// request's Stdout and Stderr writers when set, and always captured (up
	// to MaxOutput bytes each) in the result. A non-zero exit code is a
	// result, not an error; an error means the command could not be run.
	Exec(ctx context.Context, cmd ExecRequest) (ExecResult, error)
	// CopyIn extracts a tar stream into the directory dst, creating it.
	CopyIn(ctx context.Context, dst string, r io.Reader) error
	// CopyOut returns a tar stream of the file or directory src. Entry names
	// are relative to src's parent, as `tar -C parent base` writes them.
	CopyOut(ctx context.Context, src string) (io.ReadCloser, error)
	// ApplyPolicy replaces the sandbox's egress allowlist and credentials. It
	// is called again before each subagent, which also refreshes short-lived
	// tokens.
	ApplyPolicy(ctx context.Context, p Policy) error
}

Sandbox is one running sandbox.

type SandboxSpec

type SandboxSpec struct {
	// SessionID labels the sandbox so leaked ones can be traced and reaped.
	SessionID string
	// Skill is the skill whose kit produced this spec (the SandboxTemplate
	// name on Kubernetes).
	Skill       string
	Image       string
	Resources   Resources
	Workspace   WorkspaceSpec
	Policy      Policy
	NeedsDocker bool
	// TTL is how long the sandbox may live. Drivers reap it afterwards.
	TTL time.Duration
	// Env is extra environment for every Exec.
	Env map[string]string
}

SandboxSpec is what a skill's kit asks for.

type WorkspaceSpec

type WorkspaceSpec struct {
	// Path is where the workspace is mounted. Default /workspace.
	Path string
	// Size of the persistent volume. Default: Resources.Disk.
	Size string
}

WorkspaceSpec describes the persistent workspace.

Directories

Path Synopsis
Package docker is the sandbox driver that runs each sandbox as a plain Docker container.
Package docker is the sandbox driver that runs each sandbox as a plain Docker container.
e2b
Package e2b is the sandbox driver for E2B-compatible services: E2B Cloud, a self-hosted E2B, and CubeSandbox, which serves the same API.
Package e2b is the sandbox driver for E2B-compatible services: E2B Cloud, a self-hosted E2B, and CubeSandbox, which serves the same API.
e2btest
Package e2btest is an in-process E2B-compatible service for tests: the control API and envd behind one HTTP server, as far as the e2b driver uses them (see internal/sandbox/e2b/api.go).
Package e2btest is an in-process E2B-compatible service for tests: the control API and envd behind one HTTP server, as far as the e2b driver uses them (see internal/sandbox/e2b/api.go).
Package fake is the sandbox driver unit tests use.
Package fake is the sandbox driver unit tests use.
Package k8s is the sandbox driver for Kubernetes Agent Sandbox (kubernetes-sigs/agent-sandbox): one claim per session, isolated by the cluster's Kata or gVisor RuntimeClass.
Package k8s is the sandbox driver for Kubernetes Agent Sandbox (kubernetes-sigs/agent-sandbox): one claim per session, isolated by the cluster's Kata or gVisor RuntimeClass.
Package sbx is the sandbox driver for Docker Sandboxes: one microVM per session, each with its own kernel and a private Docker daemon.
Package sbx is the sandbox driver for Docker Sandboxes: one microVM per session, each with its own kernel and a private Docker daemon.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL