Add `KubernetesSandbox` for kubernetes-sigs/agent-sandbox pods
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) andKubernetesSandboxBackend, exported frompydantic_ai_harnessandpydantic_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 inpyproject.tomluntil 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 raisesWorkspaceUnavailableError; other failures propagate for retry. Deletion is read from the SDK helper's typed 404 result (k8s_helper.get_sandbox_claim/get_sandboxreturningNone), not fromstatus()'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'sSandboxdPodTunnelConnectionConfig/SandboxdInClusterConnectionConfigfor other ports, timeouts or a sandboxdauth_token. Agent specs take the mode strings. The mode strings are our ownConnectionModetype 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 fullWorkspaceBackendSuiteplus unit tests, against an in-process sandboxd and the SDK's realcommands.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 askubernetesinsandbox-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'slatest-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 upgradesk8s-agent-sandboxto the newest release past the 7-day cooldown and logs its version, so a breaking SDK release shows up there first.up.shlogs the digest and release it resolved. Run it locally withmake integration-kubernetes-cluster. -
tests/conftest.pyexempts the SDK's one-time credential loading (async_k8s_helper._ensure_initialized, wherekubernetes_asyncioreads the kubeconfig synchronously) from the blocking-call detector, through a newBLOCKBUSTER_LIBRARY_SETUPlist documented intests/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.
| Workaround | Where | Upstream |
|---|---|---|
The exec script puts stdin at EOF; working_dir is entered with cd | _backend.py run | Not tracked yet (Start leaves stdin open and re-roots a cwd outside its root) |
| File operations go through the shell | _backend.py | Not 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 _exec | agent-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_sandbox | agent-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 _attach | agent-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 staginglatest-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-sandbox1.0.11: itsget_sandboxlooks 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_helperis a public attribute that upstream's own integrations call; exceptions come fromk8s_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_asynciochanges 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 (nowWorkspaceUnavailableError), an unlogged claim the API server did not delete in time, aworking_dirwhose name ends in a newline, and an argument script that grew with each argument. The sixth, the launcher assigningpandqover a command'senv, was already fixed onmain. 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, asDaytonaSandboxdoes, instead of one environment variable each plus anevalloop. 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-1exit handling for sandboxd before v1.0.7 were removed too.
Limits
- asyncio only (the SDK and
grpc.aio); Trio raisesUserError. - Needs sandboxd from agent-sandbox v1.0.7 or later (signal deaths exit 128+n) and
/bin/shin 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