Skip to main content

Harness sandboxes

A harness runtime runs inside a sandbox. A sandbox provider creates a network sandbox session, which gives the runtime file I/O, process execution, exposed ports and a network policy. This page covers the sandbox types in pkg/harness, bootstrap and templates, and the two built-in providers.

The agent API is in Harness agent.

SandboxConfig​

AgentSettings.SandboxConfig configures the working directory and lifecycle hooks. Its type is harness.AgentSandboxConfig, an alias of harness.SandboxConfig.

FieldTypeDescription
WorkDirstringWorkDir is an optional fixed working directory for all sessions, relative to the sandbox default working directory.
BootstrapHashstringBootstrapHash is the caller-controlled identity for OnBootstrap.
OnBootstrapfunc(ctx context.Context, bc SandboxBootstrapContext) errorOnBootstrap runs during sandbox template creation after the adapter's own bootstrap, before snapshot-capable providers publish a snapshot (a83a367). Must be provided together with BootstrapHash.
OnSessionfunc(ctx context.Context, sc SandboxSessionContext) errorOnSession runs after each sandbox session is acquired and the session work directory exists, before the adapter starts.

OnBootstrap runs once per sandbox template, after the adapter's own bootstrap and before a snapshot-capable provider takes a snapshot. BootstrapHash identifies that work, so changing it invalidates the snapshot. Set the two together. OnSession runs for every session, after the session work directory exists and before the adapter starts.

FieldTypeDescription
Sessionproviderutils.SandboxSession
WorkDirstring
FieldTypeDescription
Sessionproviderutils.SandboxSession
SessionWorkDirstring

harness.ValidateSandboxBootstrapSettings(cfg SandboxConfig) error checks that OnBootstrap and BootstrapHash are set together.

SandboxProvider​

type SandboxProvider interface {
SpecificationVersion() string // "harness-sandbox-v1"
ProviderID() string
CreateSession(ctx context.Context, opts CreateSandboxSessionOptions) (NetworkSandboxSession, error)
}

A provider returns *harness.SandboxAuthenticationError when credentials are missing or invalid. A provider that can reattach by ID also implements harness.SandboxSessionResumer (ResumeSession(ctx, sessionID) (NetworkSandboxSession, error)). harness.SandboxSpecificationVersion is the specification version.

FieldTypeDescription
SessionIDstringSessionID names the underlying resource deterministically so a future ResumeSession can find it. Empty for prewarm paths.
IdentitystringIdentity is the stable identity for snapshot-based reuse.
OnFirstCreateOnFirstCreateFuncOnFirstCreate is called exactly once per identity, on fresh creation.

harness.OnFirstCreateFunc runs once per identity on fresh sandbox creation. It lets a snapshot-capable provider run the template's prepare step before it snapshots.

NetworkSandboxSession​

harness.NetworkSandboxSession extends providerutils.SandboxSession (file I/O, exec and spawn) with the infrastructure surface.

MethodDescription
ID() stringStable ID of the underlying sandbox resource.
DefaultWorkingDirectory() stringAbsolute path that relative commands resolve against.
Ports() []intPorts the sandbox exposes.
GetPortEndpoint(ctx, PortEndpointOptions) (PortEndpoint, error)Connection details for an exposed port.
GetPortURL(ctx, PortEndpointOptions) (string, error)Deprecated. Use GetPortEndpoint.
Stop(ctx) errorStops the sandbox. Idempotent.
Destroy(ctx) errorStops the sandbox, then cleans up. Works on running and stopped sandboxes.
Restricted() providerutils.SandboxSessionA reduced view with file I/O and process APIs only.

harness.AsNetworkSandboxSession(s) returns the network view of a session when it has one. harness.GetRestrictedSandboxSession(s) returns the restricted view, or the session itself.

Optional interfaces extend a session. A provider implements the ones it supports.

InterfaceMethod
harness.NetworkPolicySetterSetNetworkPolicy(ctx, NetworkPolicy) error
harness.RequestTransformationSetterSetRequestTransformations(ctx, []RequestTransformation) error. Replaces the full set.
harness.RequestTransformationAdderAdds rules without replacing existing ones.
harness.PortsSetterSetPorts(ctx, []int) error. Replaces the set of exposed ports.

Ports​

FieldTypeDescription
URLstring
Headersmap[string]string
FieldTypeDescription
Portint
ProtocolPortProtocol

harness.PortProtocol selects the URL scheme: harness.PortProtocolHTTP, harness.PortProtocolHTTPS or harness.PortProtocolWS.

Network policy​

FieldTypeDescription
Modestring
AllowedHosts[]string
AllowedCIDRs[]string
DeniedCIDRs[]string

Mode is harness.NetworkPolicyAllowAll, harness.NetworkPolicyDenyAll or harness.NetworkPolicyCustom. A custom policy needs at least one of AllowedHosts or AllowedCIDRs. DeniedCIDRs takes precedence over both.

Request transformations​

A request transformation rewrites an outbound HTTPS request outside the sandbox security boundary. Adapters use them for credential brokering: the sandbox holds a placeholder, and the transformation adds the real credential on the way out, so the credential never enters the sandbox.

FieldTypeDescription
MatchRequestTransformationMatch
TransformRequestTransformationTransform
FieldTypeDescription
Hoststring
Path*StringMatcher
Method[]string
QueryString[]KeyValueMatcher
Headers[]KeyValueMatcher
FieldTypeDescription
Headersmap[string]string

harness.StringMatcher matches a string exactly, by prefix or by regular expression. harness.KeyValueMatcher matches a header or a query parameter. harness.RequestTransformationSources holds the inputs adapters use to build credential transformations.

Bootstrap recipes​

An adapter can declare a recipe that installs its runtime in the sandbox. The recipe is a set of files and commands. It runs once per sandbox, and a marker file records completion, so repeated calls are cheap.

NameDescription
harness.BootstrapThe recipe: HarnessID, BootstrapDir, Files and Commands.
harness.BootstrapFile, harness.BootstrapCommandOne file written, and one command run from the bootstrap directory after all files are written.
harness.BootstrapProviderInterface an adapter implements to declare a recipe.
harness.GetBootstrap(ctx, h Harness) (*Bootstrap, error)Returns the adapter's recipe, or nil.
harness.BootstrapSchemaVersionThe version of the recipe shape.
harness.HashHarnessBootstrap(recipe Bootstrap) stringA deterministic 16-character hex identity of a recipe. Identical to the TypeScript hash for identical input.
harness.ApplyBootstrapRecipe(ctx, session, recipe, identity, stateDirectory) errorApplies a recipe idempotently.
harness.BootstrapMarkerPath(recipe, identity, stateDirectory) stringThe path of the marker written after a recipe is applied.
harness.StateDirectoryName, harness.StateDirectoryPath(sandboxHomeDir) stringThe fixed directory under the sandbox HOME that holds all harness state.
harness.SessionDataDirectoryPath(stateDirectory, sessionID) stringThe per-session state path under .agent-runs.

OnBootstrap markers​

FunctionDescription
harness.OnBootstrapMarkerPath(ctx, session, bootstrapHash) (string, error)The marker path for a caller's OnBootstrap hook, keyed by the SHA-256 of the hash.
harness.HasOnBootstrapMarker(ctx, session, bootstrapHash) (bool, error)Reports whether OnBootstrap already finished for this hash on this sandbox.
harness.WriteOnBootstrapMarker(ctx, session, bootstrapHash) errorRecords completion.

Templates​

A template packages the bootstrap work so a snapshot-capable provider can reuse it.

FunctionDescription
harness.CreateSandboxBootstrapPlan(recipe *Bootstrap, cfg SandboxConfig) (SandboxBootstrapPlan, error)Computes the recipe identity and the combined sandbox identity.
harness.CreateHarnessSandboxTemplate(ctx, CreateHarnessSandboxTemplateOptions) (*HarnessSandboxTemplate, error)Builds a template for one or more harnesses. Returns nil, nil when there is nothing to prepare.
harness.PrepareHarnessSandboxTemplate(ctx, PrepareHarnessSandboxTemplateOptions) errorPrepares a template without running an agent. Idempotent.
harness.PrepareSandboxForHarness(ctx, PrepareSandboxForHarnessOptions) (*PrepareSandboxForHarnessResult, error)Applies recipes to a sandbox you own and returns an identity you can use as a snapshot key.
harness.RunSandboxBootstrap(ctx, RunSandboxBootstrapOptions) errorApplies the adapter recipe, then runs OnBootstrap.
harness.PrewarmHarness(ctx, opts)Deprecated alias of PrepareHarnessSandboxTemplate.
FieldTypeDescription
Identitystring
Preparefunc(ctx context.Context, session providerutils.SandboxSession) error
FieldTypeDescription
Recipe*Bootstrap
RecipeIdentitystring
IdentitystringIdentity is the sandbox identity passed to SandboxProvider.CreateSession.
WorkDirstring
OnFirstCreateOnFirstCreateFunc
FieldTypeDescription
Harnesses[]Harness
SandboxConfig*SandboxConfigSandboxConfig is optional; OnSession is ignored.
FieldTypeDescription
HarnessHarness
SandboxProviderSandboxProvider
SandboxConfig*SandboxConfigSandboxConfig is optional; OnSession is ignored.
FieldTypeDescription
Sessionproviderutils.SandboxSession
Harnesses[]Harness
SandboxConfig*SandboxConfigSandboxConfig is optional; OnSession is ignored.
FieldTypeDescription
IdentitystringIdentity is empty when no recipe was applied and no bootstrapHash set.
RecipeIdentitiesmap[string]string
SkippedHarnessIDs[]string
FieldTypeDescription
Sessionproviderutils.SandboxSession
Recipe*Bootstrap
RecipeIdentitystring
WorkDirstring
OnBootstrapfunc(ctx context.Context, bc SandboxBootstrapContext) error
BootstrapHashstringBootstrapHash is the caller-controlled identity OnBootstrap is keyed by. Required for SkipOnBootstrapIfMarked and for the completion marker this writes after a successful OnBootstrap run.
SkipOnBootstrapIfMarkedboolSkipOnBootstrapIfMarked skips OnBootstrap entirely when a marker for BootstrapHash already exists on this sandbox (from a prior call, e.g. a snapshot-provider's OnFirstCreate having already run it). Mirrors TS runSandboxBootstrap's skipOnBootstrapIfMarked (31742b9a1b).
DefaultWorkingDirectorystringDefaultWorkingDirectory skips the pwd lookup when known.

Working directory helpers​

FunctionDescription
harness.NormalizeSandboxWorkDir(workDir string) (string, error)Validates and normalizes a relative work directory.
harness.ResolveSessionWorkDir(defaultWorkingDirectory, harnessID, sessionID, workDir string) stringComposes <default>/<harnessId>-<sessionId>, or <default>/<workDir> when workDir is set.
harness.EncodePathSegment(value string) stringEncodes a value as one safe path segment.
harness.EnsureSandboxDirectory(ctx, session, workDir) errorRuns mkdir -p for the directory.
harness.ResolveSandboxHomeDir(ctx, sandbox) (string, error)Resolves the sandbox HOME.
harness.ResolveSandboxDefaultWorkingDirectory(ctx, sandbox) (string, error)Returns the default working directory.
harness.StripWorkDir(...), harness.NewToolInputWorkDirStripper(...)Remove the session work directory prefix from path fields of stream parts before they reach the consumer.

Local provider​

Package github.com/digitallysavvy/go-ai/pkg/harness/sandbox/local runs each session in a directory on the host. It has no isolation. Use it for development and tests. local.ProviderID is "local".

func NewProvider(opts Options) *Provider
FieldTypeDescription
RootDirstringRootDir holds one directory per session. Defaults to <os.TempDir()>/ai-sdk-harness-local.
HomeDirstringHomeDir, when set, is used as HOME for every session (for example the real user home to reuse caches and native subscriptions). When empty, each session gets an isolated HOME under its session directory.
ShellstringShell runs commands. Defaults to /bin/sh.
Envmap[string]stringEnv is added to every command's environment (after the host environment and HOME, before per-command env).
Ports[]intPorts lists the ports reported by Ports(); all localhost ports are reachable regardless.
HoststringHost used in port endpoints. Defaults to 127.0.0.1.
KeepFilesboolKeepFiles keeps the session directory on Destroy.

Vercel provider​

Package github.com/digitallysavvy/go-ai/pkg/harness/sandbox/vercel runs sandboxes on Vercel Sandbox, with network policies and template snapshots. See Harness sandbox: Vercel for the full API.