Environments
Mount sandboxed workspaces on Harness runs, enforce file and shell permission ceilings, and manage background processes across local and cloud targets.
DynamicEnvironmentCapability gives an Agent file, shell, and optional desktop tools backed by a Host-supplied EnvironmentSource. Harness opens each source lazily on first use, enforces mount permission ceilings, and detaches execution resources when the Run closes while leaving target creation and destruction to the Host.
Supply a source
Implement EnvironmentSource (returning an EnvironmentConnector from async def ensure_ready()) and pass it to run(environment=...):
import asyncio
from dataclasses import dataclass
from pathlib import Path
from tempfile import TemporaryDirectory
from a13n_harness import AgentSpec, HarnessBuilder
from a13n_harness.environment import DynamicEnvironmentCapability, DynamicEnvironmentConfiguration
from a13n_harness.environment.execution import EnvironmentConnector
from a13n_harness.environment.models import EnvironmentDescriptor, EnvironmentState
from a13n_harness.providers.environment.direct_local.provider import DIRECT_LOCAL
from pydantic_ai.models.test import TestModel
@dataclass(frozen=True)
class PreparedSource:
connector: EnvironmentConnector
@property
def provider_key(self) -> str:
return self.connector.provider_key
@property
def environment_id(self) -> str:
return self.connector.environment_id
@property
def descriptor(self) -> EnvironmentDescriptor:
return self.connector.descriptor
@property
def state(self) -> EnvironmentState | None:
return self.connector.state
async def ensure_ready(self) -> EnvironmentConnector:
return self.connector
async def main() -> None:
executable = HarnessBuilder().build(
AgentSpec(),
output_type=str,
model=TestModel(),
capabilities=(DynamicEnvironmentCapability(DynamicEnvironmentConfiguration()),),
)
with TemporaryDirectory() as directory:
root = Path(directory).resolve()
(root / "message.txt").write_text("Hello from the workspace.\n", encoding="utf-8")
connector = DIRECT_LOCAL.execution_connector(
{"root": {"path": str(root)}}, environment_id="env-workspace"
)
result = await executable.run("Read message.txt.", environment=PreparedSource(connector))
print(result.output_or_raise())
asyncio.run(main())Mounts, paths, and permission ceilings
Pass environment=source to mount /workspace, or pass a mapping to environments= with EnvironmentMount to combine multiple directories and restrict allowed actions:
from a13n_harness import EnvironmentMount
from a13n_harness.environment import FILE_READ_ACTIONS, EnvironmentPermissionSet
result = await executable.run(
"Build output from reference data",
environments={
"build": build_source,
"data": EnvironmentMount(
data_source,
permission_ceiling=EnvironmentPermissionSet(operations=FILE_READ_ACTIONS),
working_directory="/reference",
),
},
default_environment="build",
)- Mount routing: The default mount maps to
/workspace; named mounts map to/environment/{name}(unless overridden withmount_path). - Built-in permission sets:
FILE_READ_ACTIONS(view,ls,glob,grep),FILE_ACTIONS(addswrite,edit,multi_edit,mkdir,move,copy,delete),FILE_EXECUTION_ACTIONS(default—addsshell_execand process controls), andCOMPUTER_ACTIONS(desktop tools, opt-in). - OS boundary: Permission ceilings restrict model-callable Environment operations; OS sandboxing comes from the selected backend (Envd isolation, Docker, or cloud sandboxes).
Shell process controls and state ownership
- Foreground & background shell:
shell_execwaits up toToolExecutionConfig.foreground_wait_seconds(5s default) before returning either completed output or a live process handle (exec-*with a HostToolExecutionRuntime, or Run-localprocess-*), which the Agent follows viashell_info,shell_wait(using explicitnext_offset),shell_input, andshell_signal. - Host state lifecycle: Persist authoritative
EnvironmentStatein your Host store before returning a connector (see Lifecycle and state); closing a Run never destroys the underlying container or directory.