Class AbstractBaseSandbox

java.lang.Object
io.agentscope.harness.agent.sandbox.AbstractBaseSandbox
All Implemented Interfaces:
Sandbox, AutoCloseable
Direct Known Subclasses:
DockerSandbox

public abstract class AbstractBaseSandbox extends Object implements Sandbox
Abstract base implementation of Sandbox with the 4-branch workspace start logic.

4-Branch Start Logic

 Branch A: workspaceRootReady=true  & workspace dir exists   → apply ephemeral-only entries
 Branch B: workspaceRootReady=true  & workspace dir missing  → restore from snapshot + ephemeral entries
 Branch C: workspaceRootReady=false & snapshot is restorable → hydrate from snapshot + all entries
 Branch D: workspaceRootReady=false & no restorable snapshot → fresh init from full workspace spec
 

Subclasses implement the backend-specific operations:

  • Constructor Details

    • AbstractBaseSandbox

      protected AbstractBaseSandbox(SandboxState state)
  • Method Details

    • start

      public void start() throws Exception
      Executes the 4-branch workspace start logic.
      Specified by:
      start in interface Sandbox
      Throws:
      Exception - if the workspace fails to start
    • stop

      public void stop() throws Exception
      Persists the workspace snapshot and marks the workspace root as ready.
      Specified by:
      stop in interface Sandbox
      Throws:
      Exception - if snapshot persistence fails
    • close

      public void close() throws Exception

      Calls stop() then Sandbox.shutdown(). Stop failures are logged but do not prevent shutdown.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Sandbox
      Throws:
      Exception
    • isRunning

      public boolean isRunning()
      Specified by:
      isRunning in interface Sandbox
    • getState

      public SandboxState getState()
      Description copied from interface: Sandbox
      Returns the current serializable state of this sandbox.
      Specified by:
      getState in interface Sandbox
      Returns:
      state (may be modified by lifecycle methods)
    • exec

      public ExecResult exec(io.agentscope.core.agent.RuntimeContext runtimeContext, String command, Integer timeoutSeconds) throws Exception
      Delegates to doExec(RuntimeContext, String, int) with a fallback timeout.
      Specified by:
      exec in interface Sandbox
      Parameters:
      runtimeContext - per-call agent context (session, user, attributes); may be null
      command - shell command
      timeoutSeconds - max wait; null for implementation default
      Throws:
      Exception
    • persistWorkspace

      public InputStream persistWorkspace() throws Exception
      Specified by:
      persistWorkspace in interface Sandbox
      Throws:
      Exception
    • hydrateWorkspace

      public void hydrateWorkspace(InputStream archive) throws Exception
      Specified by:
      hydrateWorkspace in interface Sandbox
      Throws:
      Exception
    • probeWorkspaceRootForPreservedResume

      protected boolean probeWorkspaceRootForPreservedResume()
      Probes whether the workspace root directory still exists, using a backend exec.

      Uses test -d {workspaceRoot} with a 10-second timeout. Returns true if the command exits with code 0.

      Returns:
      true if the workspace root exists
    • getDefaultExecTimeoutSeconds

      protected int getDefaultExecTimeoutSeconds()
      Returns the default command execution timeout in seconds.
      Returns:
      default timeout (120 seconds)
    • doExec

      protected abstract ExecResult doExec(io.agentscope.core.agent.RuntimeContext runtimeContext, String command, int timeoutSeconds) throws Exception
      Executes a shell command within the workspace.
      Parameters:
      runtimeContext - per-call context; may be null for internal probes
      command - shell command string
      timeoutSeconds - maximum execution time
      Returns:
      execution result
      Throws:
      Exception - if execution fails
    • doPersistWorkspace

      protected abstract InputStream doPersistWorkspace() throws Exception
      Creates a tar archive of the current workspace contents.
      Returns:
      an InputStream over the tar stream; caller must close
      Throws:
      Exception - if archiving fails
    • doHydrateWorkspace

      protected abstract void doHydrateWorkspace(InputStream archive) throws Exception
      Extracts a tar archive into the workspace.
      Parameters:
      archive - the tar archive stream to extract
      Throws:
      Exception - if extraction fails
    • doSetupWorkspace

      protected abstract void doSetupWorkspace() throws Exception
      Creates the workspace root directory.
      Throws:
      Exception - if directory creation fails
    • doDestroyWorkspace

      protected abstract void doDestroyWorkspace() throws Exception
      Destroys the workspace root and any backend resources.
      Throws:
      Exception - if cleanup fails
    • getWorkspaceRoot

      protected abstract String getWorkspaceRoot()
      Returns the absolute path of the workspace root directory.
      Returns:
      workspace root path string