Skip to content

SSH Workspace

Run your agent’s commands and file edits on another machine over SSH, using your own ssh client and its configuration.

Source

While Pydantic AI Harness is on 0.x releases, the API may change between minor releases; when it does, deprecation warnings and release-note migration guidance tell you (or your agent) exactly how to upgrade. See the version policy.

flowchart TB
    accTitle: SSH Workspace: your agent's commands run on another machine over SSH
    accDescr: On your machine, the model calls Coder, Shell or FileSystem tools such as run_command. As their ctx.workspace, SSHWorkspace uses your ssh client with key login only to open a new SSH connection to host dev@build-box for each operation, run as the login user. A WorkspaceRef in the message history for the same host and directory reattaches, and any other ref raises ValueError.
    subgraph machine["Your machine"]
        model(["model"])
        tools["Coder, Shell or<br/>FileSystem tools,<br/>such as run_command"]
        ref["WorkspaceRef<br/>in the message history"]
        ssh["SSHWorkspace()<br/>your ssh client,<br/>key login only"]:::capability
    end
    host["host dev@build-box<br/>runs as the login user"]:::external
    other["ref for any other<br/>host or directory"]:::blocked
    model <-->|"tool call, result"| tools
    tools -->|"ctx.workspace"| ssh
    ref -.->|"same host and<br/>directory: reattaches"| ssh
    ssh <-->|"a new SSH connection<br/>for each operation"| host
    ssh -.-x|"raises ValueError"| other
    classDef capability stroke:#E520E9,stroke-width:2px
    classDef external stroke:#0E9C86,stroke-width:2px
    classDef blocked stroke:#E5533D,stroke-width:2px,stroke-dasharray:4 3

Install

Terminal
pip install "pydantic-ai-harness[coder,anthropic]"

The capability runs the ssh client already on your machine, so it needs no extra of its own. The examples also use Coder and an Anthropic model, hence the coder and anthropic extras; swap them for your own tools and model provider.

Quick start

from pydantic_ai import Agent
from pydantic_ai_harness import Coder, SSHWorkspace

agent = Agent(
    'anthropic:claude-opus-5-5',
    capabilities=[SSHWorkspace('dev@build-box', working_dir='/srv/app', env={'UV_OFFLINE': '1'}), Coder()],
)
result = agent.run_sync('Run the test suite and fix the first failure.')

Coder’s shell and file tools now run on build-box. For a narrower agent, use Shell, FileSystem or your own tools on ctx.workspace.

  • Commands run as the user you log in as, with that user’s full authority on the host. Neither working_dir nor a FileSystem root confines shell commands. To confine them, wrap the capability in BubblewrappedWorkspace.
  • Logging in needs a key without a prompt. See Logging in.
  • Commands start in working_dir, which must already exist. It defaults to the login directory, and a relative working_dir is resolved from there.
  • Commands get the remote login environment plus env=. Your local environment isn’t sent, apart from locale variables your SendEnv configuration forwards.
  • Every operation opens an SSH connection. Turn on connection sharing in your SSH configuration (ControlMaster auto with a ControlPersist time) to make them fast.

For a single run, pass workspace=SSHWorkspaceBackend('dev@build-box') to agent.run instead of adding the capability.

Continue in the same workspace

The host’s files stay where they are between runs, so any run with the same SSHWorkspace works in the same directory. Workspaces covers how runs find their workspace.

  • The run’s ref is a WorkspaceRef naming the host and directory, such as WorkspaceRef(provider='ssh', id='dev@build-box:/srv/app'). It is set as soon as the capability is created.
  • SSHWorkspace only accepts a ref to its own host and directory, and raises ValueError for any other. A ref in the message history can’t point it at another machine.

Logging in

Passwords aren’t supported. ssh runs with BatchMode=yes, so a host that asks for a password or a key passphrase fails right away instead of hanging the run. ssh_args= can’t turn prompts back on.

  • Use a key without a passphrase, such as ssh_args=['-i', key_path] or IdentityFile in your SSH configuration, or load a key that has one into ssh-agent with ssh-add.
  • Set the user in the destination (dev@build-box), with User in your SSH configuration, or with ssh_args=['-l', 'dev'].
  • Your SSH configuration (~/.ssh/config) also supplies ports and jump hosts.

Security

  • Commands can do anything the remote user can. Give the agent a login with only the access it needs. Choose the variables commands need with env=, and don’t pass your local environment through.
  • env= values show up in process listings. They travel inside the script ssh sends, which this machine passes to a local sh as arguments. While a command runs, other users on this machine can see them with ps. So can users on the host when ssh doesn’t forward its input: -n or -f in ssh_args, or StdinNull in your SSH configuration. On a shared machine, keep secrets out of env=. Put them in a file only the remote user can read, or in the remote user’s login environment.
  • Files the agent reads into the prompt are trusted as yours by default. For a checkout you didn’t write, pass authority='none', or 'user' for the user’s own files. Capabilities that put a file into the prompt then give it no more authority than that. path_authority sets a level for single paths on the host, and ~ isn’t expanded in them. Default: 'operator'.

Limits

  • The machine running the agent must be Linux or macOS, like LocalWorkspace. On Windows, constructing SSHWorkspace raises NotImplementedError.
  • The host needs a POSIX sh and the usual file utilities.
  • A command can be as long as the host allows one program argument to be, 128 KiB on Linux. If ssh doesn’t forward its input (see Security), a long command full of quotes can be too long to start.
  • A command that leaves a background process holding its output open, such as server &, doesn’t return until that process exits. Redirect its output, as in server > server.log 2>&1 &.
  • On a timeout, a second connection stops the command’s processes on the host. A process that detached into its own session, like a Shell background job, keeps running.
  • A host that can’t be reached, a missing working directory, or a connection lost mid-command raises WorkspaceUnavailableError. A command’s exit code is its own, even 255, which ssh also uses for its own errors.

Telemetry

SSHWorkspace emits no spans of its own. Core’s instrumentation records pydantic_ai.workspace.provider (ssh) and pydantic_ai.workspace.id (the host and directory) on the agent run span, and each command or file operation runs inside its tool call’s span.

API reference

SSHWorkspace

Bases: AbstractCapability[AgentDepsT]

Gives runs a workspace on a remote host over SSH, using your ssh client and its configuration.

Commands run as the remote user, with that user’s full authority on the host. Wrap it in BubblewrappedWorkspace to confine them to the working directory there.

from pydantic_ai import Agent
from pydantic_ai_harness import SSHWorkspace

agent = Agent('anthropic:claude-opus-5-5', capabilities=[SSHWorkspace('dev@build-box', working_dir='/srv/app')])

It declines a ref for any other host or directory, so a ref in message history can’t point it elsewhere.

Attributes

destination

The host, as you’d pass it to ssh: 'user@host', a Host alias, or 'ssh://user@host:port'.

Type: str

working_dir

Where commands start and relative paths resolve on the host; defaults to the login directory.

Type: str | None Default: None

read_only

Whether to wrap the workspace in a ReadOnlyWorkspace.

Type: bool Default: False

env

Environment variables for every command, on top of the remote login environment.

Type: Mapping[str, str] | None Default: field(default=None, repr=False)

ssh_args

Extra ssh arguments, such as ['-i', key_path]; prefer your SSH configuration where you can.

Type: Sequence[str] Default: ()

id

Fixed, so a later SSHWorkspace replaces an earlier one whole; pass distinct ids to keep both.

Type: str | None Default: 'ssh_workspace'

authority

The authority of the remote host’s files: 'none' for files nobody on the application’s side wrote, such as a cloned third-party repository, and 'user' for the user’s own files. Anything below 'operator' wraps the workspace in an AuthorityWorkspace.

Type: Authority Default: 'operator'

path_authority

Levels for some directories and files, overriding authority for the paths under them; the longest matching path wins. Paths are remote host paths: relative ones resolve against the working directory, and ~ isn’t expanded. See Workspace.authority_of.

Type: Mapping[str, Authority] | None Default: None

Methods

backend
def backend(ref: WorkspaceRef) -> SSHWorkspaceBackend

Attach to this capability’s host and working directory by ref, without connecting.

Returns

SSHWorkspaceBackend

Raises
  • ValueError — If ref is for another host or working directory; this capability never redirects commands to a host it wasn’t configured with.
get_workspace
def get_workspace(
    ctx: RunContext[AgentDepsT],
    *,
    ref: WorkspaceRef | None,
) -> WorkspaceBackend | None

The backend for this run, in an AuthorityWorkspace when authority or path_authority asks for one.

Returns

WorkspaceBackend | None

SSHWorkspaceBackend

Bases: WorkspaceBackend, SupportsCommands

Run commands on a remote host with the system’s OpenSSH ssh client.

Authentication, host keys, ports and jump hosts come from your SSH configuration (~/.ssh/config) and agent; ssh never prompts, so a missing key fails instead of waiting for a password. File operations run as shell commands on the host (see Writing a backend), which needs a POSIX sh there. The remote directory is the environment: the first operation raises WorkspaceUnavailableError if it is missing or the host can’t be reached. On a timeout or cancellation, a second connection stops the command’s process group on the host. A background process that keeps the command’s output open holds the call until it exits, as sshd waits for the output to close.

Constructor Parameters

destination : str

The host, as you’d pass it to ssh: 'user@host', a Host alias from your SSH configuration, or 'ssh://user@host:port'.

working_dir : str | None Default: None

Where commands start and relative paths resolve, on the remote host; a relative path starts in the login directory, which is the default.

env : Mapping[str, str] | None Default: None

Environment variables for every command, on top of the remote login environment; the per-call env goes on top.

ssh_args : Sequence[str] Default: ()

Extra ssh arguments, such as ['-i', key_path], placed before the destination.

Attributes

ref

WorkspaceRef(provider='ssh', id='<destination>[:<working_dir>]'), available from construction.

Type: WorkspaceRef