Class DockerSandbox
java.lang.Object
io.agentscope.harness.agent.sandbox.AbstractBaseSandbox
io.agentscope.harness.agent.sandbox.impl.docker.DockerSandbox
- All Implemented Interfaces:
Sandbox,SandboxFileTransfer,AutoCloseable
Docker
Sandbox that runs commands in a container.
Uses the docker CLI via ProcessBuilder; no docker-java library
dependency is required. The Docker daemon must be accessible on the host.
Container Lifecycle
- On
start(): the container is created and started if it does not exist; if the container exists but is stopped it is restarted; if it is already running the existing container is reused. - On
AbstractBaseSandbox.stop(): the workspace snapshot is persisted (if configured). The container keeps running. - On
shutdown(): the container is stopped and removed if self-managed.
Workspace Operations
- Exec:
docker exec -w <root> <containerId> sh -c <command> - PersistWorkspace:
docker exec <containerId> tar -cf - -C <root> . - HydrateWorkspace:
docker exec -i <containerId> tar -xf - -C <root> - UploadFile / DownloadFile (
SandboxFileTransfer): a host temp file round-tripped throughdocker cp, so file bytes never pass through exec argv or the stdout capture cap. Scoped to the workspace subtree by lexical validation; uploads land with mode 0644, matching the archive-hydrate fallback. Symlinks inside the sandbox are not resolved — the isolation boundary is the container, not the subtree.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidDestroys the workspace root and any backend resources.protected ExecResultExecutes a shell command within the workspace.protected voiddoHydrateWorkspace(InputStream archive) Extracts a tar archive into the workspace.protected InputStreamCreates a tar archive of the current workspace contents.protected voidCreates the workspace root directory.byte[]downloadFile(String path) Reads a file from the sandbox.protected StringReturns the absolute path of the workspace root directory.voidshutdown()Stops and removes the Docker container if self-managed.voidstart()Ensures the backing Docker container is running before executing the standard 4-branch workspace start logic.booleansupportsFileTransfer(String path) Whether this sandbox can natively transfer the given path.voiduploadFile(String path, byte[] content) Writes a file inside the sandbox, creating parent directories if needed.Methods inherited from class io.agentscope.harness.agent.sandbox.AbstractBaseSandbox
close, exec, getDefaultExecTimeoutSeconds, getState, hydrateWorkspace, isRunning, persistWorkspace, probeWorkspaceRootForPreservedResume, stop
-
Constructor Details
-
DockerSandbox
-
-
Method Details
-
start
Ensures the backing Docker container is running before executing the standard 4-branch workspace start logic.- Specified by:
startin interfaceSandbox- Overrides:
startin classAbstractBaseSandbox- Throws:
Exception- if the container cannot be started
-
shutdown
Stops and removes the Docker container if self-managed. -
doExec
protected ExecResult doExec(io.agentscope.core.agent.RuntimeContext runtimeContext, String command, int timeoutSeconds) throws Exception Description copied from class:AbstractBaseSandboxExecutes a shell command within the workspace.- Specified by:
doExecin classAbstractBaseSandbox- Parameters:
runtimeContext- per-call context; may benullfor internal probescommand- shell command stringtimeoutSeconds- maximum execution time- Returns:
- execution result
- Throws:
Exception- if execution fails
-
doPersistWorkspace
Description copied from class:AbstractBaseSandboxCreates a tar archive of the current workspace contents.- Specified by:
doPersistWorkspacein classAbstractBaseSandbox- Returns:
- an
InputStreamover the tar stream; caller must close - Throws:
Exception- if archiving fails
-
doHydrateWorkspace
Description copied from class:AbstractBaseSandboxExtracts a tar archive into the workspace.- Specified by:
doHydrateWorkspacein classAbstractBaseSandbox- Parameters:
archive- the tar archive stream to extract- Throws:
Exception- if extraction fails
-
doSetupWorkspace
Description copied from class:AbstractBaseSandboxCreates the workspace root directory.- Specified by:
doSetupWorkspacein classAbstractBaseSandbox- Throws:
Exception- if directory creation fails
-
doDestroyWorkspace
Description copied from class:AbstractBaseSandboxDestroys the workspace root and any backend resources.- Specified by:
doDestroyWorkspacein classAbstractBaseSandbox- Throws:
Exception- if cleanup fails
-
getWorkspaceRoot
Description copied from class:AbstractBaseSandboxReturns the absolute path of the workspace root directory.- Specified by:
getWorkspaceRootin classAbstractBaseSandbox- Returns:
- workspace root path string
-
supportsFileTransfer
Description copied from interface:SandboxFileTransferWhether this sandbox can natively transfer the given path.- Specified by:
supportsFileTransferin interfaceSandboxFileTransfer- Parameters:
path- path to probe — absolute inside the sandbox; implementations may also accept forms they can resolve themselves (e.g. workspace-relative paths)- Returns:
- true if
SandboxFileTransfer.uploadFile(java.lang.String, byte[])/SandboxFileTransfer.downloadFile(java.lang.String)accept this path
-
uploadFile
Description copied from interface:SandboxFileTransferWrites a file inside the sandbox, creating parent directories if needed.- Specified by:
uploadFilein interfaceSandboxFileTransfer- Parameters:
path- destination path inside the sandbox — absolute, or a form the implementation resolvescontent- file bytes- Throws:
Exception- when the transfer fails; unlike a declined path, a failed transfer is not retried through the exec strategy
-
downloadFile
Description copied from interface:SandboxFileTransferReads a file from the sandbox.- Specified by:
downloadFilein interfaceSandboxFileTransfer- Parameters:
path- path inside the sandbox — absolute, or a form the implementation resolves- Returns:
- file bytes
- Throws:
Exception- when the transfer fails; unlike a declined path, a failed transfer is not retried through the exec strategy
-