SSH Workspace
Run your agent’s commands and file edits on another machine over SSH, using your own ssh client and its configuration.
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
pip install "pydantic-ai-harness[coder,anthropic]"
uv add "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.
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_dirnor a FileSystem root confines shell commands. To confine them, wrap the capability inBubblewrappedWorkspace. - 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 relativeworking_diris resolved from there. - Commands get the remote login environment plus
env=. Your local environment isn’t sent, apart from locale variables yourSendEnvconfiguration forwards. - Every operation opens an SSH connection. Turn on connection sharing in your SSH configuration (
ControlMaster autowith aControlPersisttime) to make them fast.
For a single run, pass workspace=SSHWorkspaceBackend('dev@build-box') to agent.run instead of adding the capability.
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
WorkspaceRefnaming the host and directory, such asWorkspaceRef(provider='ssh', id='dev@build-box:/srv/app'). It is set as soon as the capability is created. SSHWorkspaceonly accepts a ref to its own host and directory, and raisesValueErrorfor any other. A ref in the message history can’t point it at another machine.
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]orIdentityFilein your SSH configuration, or load a key that has one intossh-agentwithssh-add. - Set the user in the destination (
dev@build-box), withUserin your SSH configuration, or withssh_args=['-l', 'dev']. - Your SSH configuration (
~/.ssh/config) also supplies ports and jump hosts.
- 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 scriptsshsends, which this machine passes to a localshas arguments. While a command runs, other users on this machine can see them withps. So can users on the host whensshdoesn’t forward its input:-nor-finssh_args, orStdinNullin your SSH configuration. On a shared machine, keep secrets out ofenv=. 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_authoritysets a level for single paths on the host, and~isn’t expanded in them. Default:'operator'.
- The machine running the agent must be Linux or macOS, like
LocalWorkspace. On Windows, constructingSSHWorkspaceraisesNotImplementedError. - The host needs a POSIX
shand the usual file utilities. - A command can be as long as the host allows one program argument to be, 128 KiB on Linux. If
sshdoesn’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 inserver > 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, whichsshalso uses for its own errors.
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.
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.
The host, as you’d pass it to ssh: 'user@host', a Host alias, or 'ssh://user@host:port'.
Type: str
Where commands start and relative paths resolve on the host; defaults to the login directory.
Type: str | None Default: None
Whether to wrap the workspace in a ReadOnlyWorkspace.
Type: bool Default: False
Environment variables for every command, on top of the remote login environment.
Type: Mapping[str, str] | None Default: field(default=None, repr=False)
Extra ssh arguments, such as ['-i', key_path]; prefer your SSH configuration where you can.
Type: Sequence[str] Default: ()
Fixed, so a later SSHWorkspace replaces an earlier one whole; pass distinct ids to keep both.
Type: str | None Default: 'ssh_workspace'
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'
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
def backend(ref: WorkspaceRef) -> SSHWorkspaceBackend
Attach to this capability’s host and working directory by ref, without connecting.
SSHWorkspaceBackend
ValueError— Ifrefis for another host or working directory; this capability never redirects commands to a host it wasn’t configured with.
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.
WorkspaceBackend | None
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.
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'.
Where commands start and relative paths resolve, on the remote host; a relative path starts in the login directory, which is the default.
Environment variables for every command, on top of the remote login environment; the
per-call env goes on top.
Extra ssh arguments, such as ['-i', key_path], placed before the destination.
WorkspaceRef(provider='ssh', id='<destination>[:<working_dir>]'), available from construction.
Type: WorkspaceRef