Add `KubernetesSandbox` for kubernetes-sigs/agent-sandbox pods

Merged
AD
20d ago
+3913-81

Closes #13257

Why we make these changes

Adds KubernetesSandbox to the harness: an agent's workspace runs in a pod claimed from a kubernetes-sigs/agent-sandbox SandboxWarmPool on the user's own cluster. Commands run over sandboxd's streaming Start, through the SDK's sandbox.commands.start() (k8s-agent-sandbox 1.0.11).

New public surface

  • KubernetesSandbox (capability) and KubernetesSandboxBackend, exported from pydantic_ai_harness and pydantic_ai_harness.kubernetes_sandbox.
  • Extra pydantic-ai-harness[kubernetes] = k8s-agent-sandbox[async,grpc]>=1.0.11,<2. 1.0.11 has a cooldown exception in pyproject.toml until 2026-11-19.
  • Docs page harness/kubernetes-sandbox.md.

User-visible behavior

agent = Agent(model, capabilities=[KubernetesSandbox(warm_pool='python-pool'), Coder()])
  • A run claims a sandbox on first use; its ref, WorkspaceRef(provider='kubernetes', id='<namespace>/<claim>'), reattaches later runs to the same pod. A claim or Sandbox the API server reports deleted raises WorkspaceUnavailableError; other failures propagate for retry. Deletion is read from the SDK helper's typed 404 result (k8s_helper.get_sandbox_claim / get_sandbox returning None), not from status()'s strings, and a lookup that fails or times out is logged.
  • connection='port-forward' (default), 'in-cluster-pod-ip' or 'in-cluster-service', or the SDK's SandboxdPodTunnelConnectionConfig / SandboxdInClusterConnectionConfig for other ports, timeouts or a sandboxd auth_token. Agent specs take the mode strings. The mode strings are our own ConnectionMode type and are passed to the SDK as they are: an upstream rename fails type checking on that call, and the SDK itself still accepts the names it renamed (as in v1.0.6).
  • sandbox_timeout (default 24 h) makes the claim delete itself; destroy(ref) deletes it sooner. Runs never delete it.

Verification

  • tests/harness/kubernetes_sandbox: the full WorkspaceBackendSuite plus unit tests, against an in-process sandboxd and the SDK's real commands.start(); 100% coverage.

  • CI harness-kubernetes-integration (every PR that touches the backend): the same suite against real sandboxd built from v1.0.7, the oldest release the docs support. No cluster.

  • Live tier tests/harness/kubernetes_sandbox/test_kubernetes_live.py, run nightly as kubernetes in sandbox-live.yml's provider matrix, like every other sandbox provider: a kind cluster with the newest agent-sandbox release's controller and sandboxd from the staging registry's latest-main, covering the conformance suite plus expiry, destroy, cancelled claims, recreated pods, other ports, a token-protected sandboxd, Coder's tools and every docs example. The nightly also upgrades k8s-agent-sandbox to the newest release past the 7-day cooldown and logs its version, so a breaking SDK release shows up there first. up.sh logs the digest and release it resolved. Run it locally with make integration-kubernetes-cluster.

  • tests/conftest.py exempts the SDK's one-time credential loading (async_k8s_helper._ensure_initialized, where kubernetes_asyncio reads the kubeconfig synchronously) from the blocking-call detector, through a new BLOCKBUSTER_LIBRARY_SETUP list documented in tests/AGENTS.md. The harness code around it stays checked.

What changes for existing users

A new capability behind a new extra. The SDK's grpc extra needs protobuf 7, so the lock moves to it (with xai-sdk 1.21.0 and modal 1.6.3), and pydantic-ai-slim[xai] now requires xai-sdk>=1.21.0 (was >=1.16.0): 1.21.0 is the first release that allows protobuf 7, so the lowest-versions job keeps testing the declared floor. This raises the minimum for xAI users who never install the kubernetes extra; the maintainer accepted that on 2026-11-15, settling the question #13257 raised.

SmolSandbox's launcher script moves into _workspace_provider.py as EXEC_SCRIPT, unchanged, so both backends share it. SmolSandbox users see no change.

Upstream workarounds

Each one is commented in the code with the reason.

WorkaroundWhereUpstream
The exec script puts stdin at EOF; working_dir is entered with cd_backend.py runNot tracked yet (Start leaves stdin open and re-roots a cwd outside its root)
File operations go through the shell_backend.pyNot tracked yet (the REST file API re-roots absolute paths: /etc/hosts reads /workspace/etc/hosts)
Output capped at 10 MiB in the client_backend.py _execagent-sandbox#1918 is closed, but its fix (#2117, v1.0.10) limits only the unary Execute RPCs; commands.start() streams through Start
destroy deletes through k8s_helper.delete_sandbox_claim; delete_sandbox() swallows errors_backend.py destroy_sandboxagent-sandbox#1856
A failed attach is checked against the claim and its Sandbox; get_sandbox reports any failure, an unreachable API server too, as not found_backend.py _attachagent-sandbox#1785

Removed in this PR now that the SDK ships them (1.0.9): the backend's own Start client and hand-encoded protobuf (agent-sandbox#1603, #2054), and the missing bearer token (#2024).

PR decisions
  • The docs template uses registry.k8s.io/agent-sandbox/sandboxd:v1.0.11, the newest release (v1.0.10 was the first agent-sandbox published there); v1.0.7 stays the stated minimum. The nightly keeps staging latest-main.
  • The live kind-cluster tier runs in the nightly provider matrix, not on PRs; PRs run the cluster-free sandboxd job.
  • The public connection= API stays: our mode strings, passed to the SDK as they are, plus the SDK's two sandboxd config classes for advanced settings. No version is named in the docs.
  • The nightly upgrades the SDK within the cooldown rather than bypassing it, because its test step holds every provider's secrets.
  • SDK floor is k8s-agent-sandbox 1.0.11: its get_sandbox looks the claim up first and fails at once when it is missing (agent-sandbox#2229), so the backend no longer checks the claim before every attach. No other call the backend makes changed from 1.0.10. sandboxd v1.0.11 passes the real-daemon suite (38 passed, 2 Linux-only skips on macOS).
  • Only the SDK's typed public surface: k8s_helper is a public attribute that upstream's own integrations call; exceptions come from k8s_agent_sandbox.exceptions (the package __init__ has no __all__, so its names are not typed re-exports). The backend names its claims (pydantic-ai-<32 hex>), so it can delete a claim that never became Ready, and the capability accepts only refs with that name in its namespace.
  • Unit tests cover the backend's own behavior; the SDK's port-forward restart is left to the live tier.
  • Whole-function blockbuster exemption for SDK credential setup, instead of call-by-call entries that would break when kubernetes_asyncio changes which file calls it makes.
  • An adversarial review of the branch (2026-11-12) found seven issues. Five are fixed here with a regression test each: aclose() racing a command that had just taken the connection, attaching to a claim whose Sandbox was deleted (now WorkspaceUnavailableError), an unlogged claim the API server did not delete in time, a working_dir whose name ends in a newline, and an argument script that grew with each argument. The sixth, the launcher assigning p and q over a command's env, was already fixed on main. The seventh, a non-UTF-8 file name that cannot round-trip through the SDK's protobuf strings, needs a byte-safe transport and is left for a follow-up.
  • Simplified on 2026-11-16: arguments are quoted into the script with shlex.join, as DaytonaSandbox does, instead of one environment variable each plus an eval loop. Commands keep the usual 128 KiB limit on one command line, and core already splits file writes into chunks. The in-cluster mode map and the -1 exit handling for sandboxd before v1.0.7 were removed too.
Limits
  • asyncio only (the SDK and grpc.aio); Trio raises UserError.
  • Needs sandboxd from agent-sandbox v1.0.7 or later (signal deaths exit 128+n) and /bin/sh in the image.
  • A background job that keeps a command's output open delays its result by about 5 s (sandboxd's wait delay).
  • A recreated pod keeps only what its template stores on a persistent volume.
  • Outside a run (Temporal activities), every command opens and closes its own connection, so one file operation can start a few kubectl port-forwards.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.

šŸ¤– Generated with Claude Code