Back to paths

Running coding agents safely in sandboxes

Treat agent-generated code as untrusted. Choose where a coding agent and its code run, from Antigravity's terminal sandbox to Cloud Workstations and GKE Agent Sandbox. Set permission presets and rules that ask before risky commands, and ship agent patches only through tests and human review.

advanced~25 min#agentic-ai#coding-agents#security#sandboxing#gke
Chapter 3Coding agentsLesson 4 of 4Show

Prerequisites: Customising coding agents: rules, hooks, skills, subagents and plugins


Agent-generated code is untrusted code

A coding agent writes code and runs commands. Both can be wrong: the model can misread the task, or follow an it read. Whatever runs, runs with the permissions of the process and the credentials around it. On a developer laptop that can mean SSH keys, cloud credentials with production access, and every file in the home folder.

Three rules follow:

  1. Treat agent-generated code as untrusted until it is reviewed and tested.
  2. Isolate where it runs, and keep production credentials out of reach.
  3. Keep a person reviewing every patch that ships.

The options below, summarised as a ladder in the simulation further down, differ in what they protect, and from what. The hooks from Customising coding agents and tool lists limit what an agent tries; isolation limits the damage when something gets through.

Antigravity's terminal sandbox and permission presets

Antigravity runs the agent's shell commands in a , using each operating system's built-in process isolation (kernel namespaces on Linux, Seatbelt on macOS, AppContainer on recent Windows 11). It runs on the developer's own machine, not in a Google-managed environment, built on the operating system's own isolation with no virtual machines or Docker images to manage. Inside it, writes are limited to the workspace, temporary folders, build caches and paths you allow with write_file, ~/.ssh and .env files are blocked, and "sandboxed commands run without network access by default". Domains you allow for read_url (Antigravity's web-fetch permission) form the sandbox's outbound allowlist.

How much the agent may do without asking is set by a :

PresetSandboxTerminal commands
Request ReviewOffAlways ask
DefaultOnAllowed in the sandbox; ask outside it
AutoOnAllowed in the sandbox; outside it, Antigravity decides automatically instead of asking
TurboOffAllowed, unrestricted, with full file system access

Default is selected unless the sandbox is unavailable, in which case Antigravity uses Request Review. Auto is not available for business or enterprise accounts, and Turbo should never be used on a machine that holds production credentials.

On top of a preset, rules say what is allowed, what asks and what is denied, written as an action and a target, such as command(git push) or read_url(registry.npmjs.org). When rules conflict, deny beats ask, and ask beats allow. A team can combine them like this:

RuleExampleEffect
Allowcommand(npm test)Tests run without asking
Askcommand(git push)Every push waits for approval, whatever else is allowed
Denycommand(rm -rf)Commands starting rm -rf never run, even if a broader rule allows them

Command rules match by literal prefix, so rm -fr is not caught: deny rules narrow risk, but they are not a complete blocklist.

Two details catch teams out:

  • The CLI depends on its mode. In the Antigravity CLI (v1.5.7 and later), agentMode defaults to "auto" where auto mode is available, and auto mode turns the sandbox on. In any other mode, including default mode, and on business and enterprise accounts, where auto mode isn't available, the sandbox is off until you set "enableTerminalSandbox": true in its settings or run agy --sandbox for a session. There, set "toolPermission": "proceed-in-sandbox" to let sandboxed commands run without asking.
  • Business and enterprise accounts use the earlier settings, whose presets have different meanings: even their Default, like Full machine and Turbo mode, leaves the sandbox off. There, switch on Enable Sandbox Mode explicitly. Antigravity's enterprise guidance is to keep Default or Request Review enabled. Administrators can also lock settings: in Gemini Enterprise, Settings > AI developer tools enforces policies such as sandbox mode and terminal auto-execution mode (Require review or Proceed in sandbox), and these override each developer's local configuration.

The sandbox confines commands, not credentials. A tool the terminal sandbox does not cover, such as an , still acts with whatever access it was given.

Cloud Workstations: a managed machine per developer

moves the developer's environment, and the coding agent with it, off the laptop. Each workstation runs on its own VM inside your VPC. To keep the environment off the internet, two settings work together, and neither replaces the other:

  • A private cluster. Choose a private gateway when you create the workstation cluster, so its endpoint has only an internal address. Developers reach it through a Private Service Connect endpoint in your VPC, with a private DNS record for the cluster, and from the company network over Cloud VPN or Cloud Interconnect.
  • No public IPs on the VMs, set in the workstation configuration. Without it, the VMs get public addresses even in a private cluster.

Google's security guidance adds a VPC Service Controls perimeter (a boundary that stops data in Google APIs such as Cloud Storage or BigQuery being reached from, or copied to, projects outside it), which requires a private cluster, and keeping each workstation single-user.

For coding agents, Google documents an agent-optimised setup: a workstation can suspend when idle and keep its memory, processes and terminal sessions, and sample hooks for Claude Code and Antigravity CLI keep it awake while the agent is working.

The agent acts with the workstation's credentials: the service account attached to its VM (the project's default workstation service account if you attach none), plus any gcloud credentials a user saves there. Attach a dedicated service account with only the roles development needs, and keep production roles off it.

When an agent keeps hitting permission errors, resist granting Owner or Editor so it "never fails". These basic roles turn every mistake or injected instruction into a project-wide change. Give the agent its own identity, add the one role a failing task shows it needs, and never lend it a developer's credentials. Agent Identity and least-privilege access covers agent identities and why basic roles are a trap.

GKE Agent Sandbox: isolating generated code at scale

When an agent product runs code that agents generate for many users, gives each run its own isolated environment. It was announced as generally available in May 2026 and builds on the open-source Agent Sandbox project. This section assumes basic GKE: a Pod is the smallest unit GKE schedules, and a network policy controls which traffic may reach or leave it.

  • Isolation. Each sandbox is a Pod with its own kernel boundary: GKE Sandbox with gVisor (a user-space kernel that intercepts the Pod's system calls) or with a microVM (a lightweight VM with its own guest kernel). Code that escapes its process must break a second boundary before it reaches the node or other tenants.
  • Resources. A SandboxTemplate defines the Pod, a SandboxWarmPool keeps sandboxes ready "in less than one second", and a SandboxClaim takes one from the pool. A Python SDK creates the claims for you.
  • Network. Pods created from a template get a default-deny network policy: no ingress except through the sandbox router (the component that forwards requests to a claimed sandbox), and no egress to private ranges, cluster DNS or the metadata server (the endpoint that hands out the node's credentials to anything that asks). Public internet egress is allowed by default, so restrict it if the code should not call out. A Sandbox you create directly, without a template, gets no network policy.
  • Operations. You install the Agent Sandbox controller on your cluster and upgrade it yourself; it is not a managed add-on. It runs on Autopilot clusters (Google manages the nodes) and Standard clusters (you manage them).

Keep cloud credentials out of the sandbox too: Google's quickstart turns off the automatic Kubernetes service-account token (the credential Kubernetes mounts into every Pod by default), and with the template's network policy blocking the metadata server, generated code cannot borrow the node's identity.

The ladder below puts the options side by side, from a laptop with no sandbox up to GKE Agent Sandbox. Before you open the cards, predict the lowest rung that keeps company credentials off developers' laptops, then click each rung to see what it blocks, what it leaves open and who it's for.

Simulation: sandbox-ladder-explorer

Choose isolation by scenario

ScenarioIsolationKeep out
One developer using a coding agent on a laptopTerminal sandbox with the Default preset, or Request Review where the sandbox is unavailableProduction credentials on the laptop
An enterprise team of developers using coding agentsCloud Workstations in a private cluster with no public VM IPs, a perimeter and a minimal service accountCompany credentials on laptops; production roles on the workstation
An agent product running generated code for many usersGKE Agent Sandbox, a sandbox per run from a warm pool, with egress restrictedCloud credentials and the metadata server inside sandboxes

Patch a vulnerability with review gates

Isolation decides where agent code runs. Review decides what ships. Say a scanner flags a vulnerable dependency, and a coding agent on a workstation takes the task:

  1. Work on a branch, from a reproduction. Where the flaw can be shown, the agent first writes a test that reproduces it and fails. Then it upgrades the dependency and fixes what the upgrade breaks, on its own branch, with permissions that cannot push to main, until that test passes.
  2. Test in isolation. It runs the test suite inside the Antigravity CLI's terminal sandbox, turned on with enableTerminalSandbox (check that it starts: it needs unprivileged user namespaces, which some containers block), with only the package registry allowed. A malicious build script then cannot reach ~/.ssh or .env files or call anywhere else. Save no gcloud credentials on the workstation, and its minimal service account limits the rest.
  3. Open a pull request. CI, not the agent, runs the full tests and security scans; the agent cannot mark its own work as passing.
  4. A person reviews and merges. The reviewer checks the diff, especially anything beyond the dependency bump, such as changed tests, new network calls or edited CI files.

The same gates apply to every maintenance job: the agent proposes, tests and scans check, and a person decides. Protect the main branch on the server, so no agent credential can skip the gates. What changes from job to job is the check that proves the work is done:

JobThe checkWhat it proves
Patch a bug or vulnerabilityWhere possible, a reproduction test first: it fails before the fix and passes afterThe fix addresses the actual flaw, not a guess at it
RefactorThe tests of its public behaviour pass before and after, unchangedBehaviour stayed the same; edited tests are a reason to look closer
Optimise, such as a JVM heap size or a Cloud Run concurrency limitMeasure, change, then measure again under the same loadThe change helped; without a baseline, "faster" is a guess

What check proves each agent maintenance job: a patch, a refactor, an optimisation?

Google also offers a security agent built for the patch job: CodeMender, on Gemini Enterprise Agent Platform, in Preview for a limited set of customers (for testing and evaluation, not production use). It finds vulnerabilities, verifies them by trying a proof-of-concept exploit, and generates and validates patches for the verified ones. By default it asks you to confirm each file change and command, and both a flag (-y) and a configuration setting turn that off. Keep the confirmation on, and send its patches through the same branch, CI and review gates as any other agent's: a dedicated security agent is still an agent whose output is untrusted until reviewed.

Recap

  • Agent-generated code is untrusted until reviewed and tested; isolate where it runs and keep production credentials out.
  • Antigravity's terminal sandbox confines commands on the developer's own machine, with no network by default; presets decide what runs without asking, and rules resolve deny before ask before allow, so an ask rule guards risky commands such as a push. In the CLI, auto mode turns the sandbox on; in other modes it stays off until you turn it on. Gemini Enterprise administrators can enforce sandbox and auto-execution settings.
  • Cloud Workstations gives each developer a managed VM in your VPC; its service account is what the agent can do, so keep it minimal and never reach for Owner or Editor.
  • GKE Agent Sandbox gives each run of generated code a kernel-isolated Pod from a warm pool; for Pods from a template it blocks internal traffic and the metadata server, but allows public egress unless you restrict it.
  • Ship agent patches through a branch, CI tests and scans, and human review, with the main branch protected. Prove a patch with a reproduction test, a refactor with unchanged tests passing before and after, and an optimisation by measuring before and after.

Sources

Show 14 sourcesHide sources
SourcePublisherWhat it supports
"CodeMender" and "Fix and patch vulnerabilities" (opens in a new tab)docs.cloud.google.comGoogle CloudPreview, limited set of customers; an AI code security agent that can find, verify and generate patches; produces, validates and applies patches for verified vulnerabilities; prompts to confirm each file modification by default; -y auto-approves
"Sandbox" and "Permissions" (opens in a new tab)and antigravity.google/docs (opens in a new tab)antigravity.googleAntigravity documentationterminal sandbox enabled by default in the app; built on native operating system primitives, with no virtual machines or Docker images; Linux namespaces, macOS Seatbelt, Windows AppContainer; "Sandboxed commands run without network access by default"; read_url domains as the outbound allowlist; ~/.ssh and .env blocked; presets Request Review, Default, Auto, Turbo; Default selected by default, Request Review if the sandbox is unavailable; Auto not available for business or enterprise accounts; rules as action(target), Deny > Ask > Allow; legacy business and enterprise presets Default, Full machine and Turbo mode do not turn the sandbox on
settings and flags (opens in a new tab)antigravity.googleAntigravity CLI documentationenableTerminalSandbox default false; agy --sandbox; toolPermission request-review by default, proceed-in-sandbox
Enterprise (opens in a new tab)antigravity.googleAntigravity documentationkeep Default or Request Review enabled
Terminal sandbox (opens in a new tab)Antigravity CLI tabantigravity.googleAntigravity documentationretrieved 2026-12-02: agentMode defaults to "auto" where available in v1.5.7+, otherwise default mode; auto mode enables the terminal sandbox automatically; enableTerminalSandbox defaults to false; auto mode isn't available for business or enterprise accounts
Configure AI developer tools settings (opens in a new tab)docs.cloud.google.comGoogle Cloud, Gemini Enterpriseretrieved 2026-12-02: administrators enforce mandatory security policies so users can't override safety rules; sandbox mode; terminal auto-execution mode with Require review or Proceed in sandbox; specified settings override the user's local configuration
About Agent Sandbox on GKE (opens in a new tab)and its quickstartdocs.cloud.google.comGoogle CloudgVisor or microVMs; Sandbox, SandboxTemplate, SandboxWarmPool, SandboxClaim; warm sandboxes in less than one second; user-installed controller; default-deny network policy for Pods from a SandboxTemplate, with public internet egress allowed and the metadata server blocked; automountServiceAccountToken false; Agent Substrate recommended for most agent workloads
GKE Agent Sandbox general availability, May 2026Google Cloud Blog"GKE Agent Sandbox is now generally available"; Agent Substrate as a new open-source project
agent-sandbox README (opens in a new tab)github.comKubernetes SIGsthe open-source project and its resources
Agent-optimized development (opens in a new tab)docs.cloud.google.comGoogle Cloudsuspend on idle keeping memory and processes; keep-alive hooks for Claude Code and Antigravity CLI
Configure VPC Service Controls and private clusters (opens in a new tab)docs.cloud.google.comGoogle Clouda private cluster's control plane has only an internal IP; clients connect through a Private Service Connect endpoint and private DNS, and off-network machines through Cloud VPN or Cloud Interconnect; turn off public IP addresses in the workstation configuration separately; public clusters cannot be created in a VPC service perimeter
Cloud Workstations security best practices, service accounts and authentication pages (opens in a new tab)docs.cloud.google.comGoogle Clouddedicated VM per workstation; disable public IPs; VPC Service Controls; attach a service account, otherwise the default workstation service account; saved gcloud credentials persist; keep workstations single-user
AI security and safety (opens in a new tab)for MCPdocs.cloud.google.comGoogle Cloudcreate an agent identity; principle of least privilege; deny policy on read-write tool access to production resources
Use IAM securely (opens in a new tab)docs.cloud.google.comGoogle Cloudavoid basic roles in production; grant Owner only when nearly universal access is required

Suggested videos

Show 2 videosHide videos

Review

Flashcards1 / 5

What does Antigravity's terminal sandbox protect, and what does it not?

Check yourself

Quick exam1 / 9 · 0 correct

A product lets thousands of users ask an agent to write and run Python on their data, with each run needing a fresh environment in about a second. Where should the generated code run?

Go deeper

Your notes on this lesson