Skip to content

feat(adapters): snapshots, fork and pause/resume on GKE for agent-sandbox - #131

Merged
reachjalil merged 4 commits into
pre-releasefrom
feat/agent-sandbox-gke-pod-snapshots
Nov 20, 2026
Merged

reachjalil merged 4 commits into
pre-releasefrom
feat/agent-sandbox-gke-pod-snapshots

Conversation

@reachjalil

@reachjalil reachjalil commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

What

Fast sandboxes that can be snapshotted, on GCP without running VMs ourselves: the experimental agent-sandbox adapter now drives GKE Pod Snapshots (podsnapshot.gke.io/v1) for Sandboxes under gVisor. A pool turns it on with config.podSnapshotPolicy (and runtimeClassName: gvisor), and then advertises snapshots.memory, snapshots.disk, fork, lifecycle.pause-resume and lifecycle.pause-checkpoint.

Operation How
Snapshot A PodSnapshotManualTrigger for the pod, waited on until the PodSnapshot is Ready. Captures memory, processes and the root filesystem; the sandbox keeps running
Fork A Sandbox with the source's pod spec and snapshot group, with podsnapshot.gke.io/ps-name naming the snapshot
Pause / resume Checkpoint, then operatingMode: Suspended (the pod is deleted, no slot held); resume restores it. Only the newest pause checkpoint is kept, and delete removes them
Delete snapshot The PodSnapshot (GKE removes the files) and its trigger

With the existing warm pools (adopting a pod in 0.5–1 s on kind), this gives the fast-start path. Warm pools and snapshots don't combine (see the docs for why).

Two GKE behaviours handled fail-closed

  1. Cross-tenant restore. Without grouping, GKE restores any new pod whose spec matches from the newest snapshot, so two tenants with the same image and size could share memory. Every sandbox's pod gets differently.dev/snapshot-group with a value of its own (a fork gets its source's), and the pool is unhealthy unless the policy selects on and groups by that label. It must also be manual, keep whole-pod and not set maxSnapshotCountPerGroup.
  2. Silent fresh starts. GKE starts a pod fresh when it cannot restore it (another machine series, a different spec, a snapshot that isn't ready). The adapter writes a token to /tmp/.differently-snapshot before each checkpoint and reads it back after a restore. A fork without it fails (sandbox_failed) and is deleted. A resume without it suspends the sandbox again (it stays paused) and fails, and the scheduler retries later.

A paused sandbox reports paused, not stopped, so the reconciler doesn't mark it as stopped unexpectedly.

Docs: docs/providers/agent-sandbox.md gets a GKE section (cluster, bucket, storage config, policy, RBAC, pool config). gke-agent-sandbox.md no longer says there is no adapter. The catalog, feature parity, site data and changelog are updated.

After an independent review (second commit)

A review of the first version found no cross-org path (the server only forks snapshots from the caller's own org), but did find these, now fixed and tested:

  • A fork shared its source's snapshot group, so a source whose pod GKE recreated (a node drain, or stop/start) could come back from its fork's newer snapshot, crossing projects within an org. Once its restore is verified, a fork now moves to a group of its own and stops naming the snapshot. agent-sandbox's controller patches template label changes onto the running pod and does not recreate it (checked in its source); the adapter waits until the pod carries the new group.
  • Rollback after resume: the template kept naming the pause checkpoint. Resume now clears it.
  • Stop/start would restore the newest checkpoint over a newer volume, so it is not offered with snapshots.
  • A failed pause (the suspend times out) is undone, so the record and the Sandbox agree (running). The timeout returns sandbox_failed.
  • Endless resume retries: after 3 resumes GKE didn't restore, the sandbox reports error, which fails it.
  • A marker race: each checkpoint now has its own marker file.
  • Leaks: pause checkpoints of Sandboxes deleted by the TTL backstop are collected on List.
  • Fork size vs quota: a fork asked for at a size other than its source's is refused.

The fake now restores as GKE documents it (the named snapshot, or else the newest in the pod's group). New tests cover each case. Deliberately breaking the regrouping or the template clearing makes them fail.

After a second review of those fixes (third commit)

  • A pause whose context is cancelled (the reconciler had already recorded it) is no longer undone. A sandbox reports paused only once its pod is gone.
  • Only a definite miss (the pod answers without the marker) counts toward the 3-resume limit. At the limit, the pause checkpoint is kept (relabelled unrestored) when the sandbox is deleted, since it's the only copy of the state.
  • A fork whose create was interrupted by a restart is finished by List (verified and regrouped), or deleted so it is created again. It is never listed while still in its source's group.
  • Orphaned pause checkpoints are collected only after 15 minutes.
  • The Role gains patch on triggers.

Deliberately disabling the cancelled-pause guard makes its test fail.

What ran

  • make verify passed locally on the first two commits. On the third, every package passed except internal/loadsim's TestSplitFollowsShares, which timed out once under machine load. It passes alone (-race -count=3), and this branch doesn't touch internal/loadsim. CI runs it again.
  • npm run build in site/ passed, with all links valid.
  • New fake-server tests (podsnapshot_test.go): the fake API also plays GKE's snapshot controller, restoring only when GKE would. It covers the policy checks, snapshot, fork, both shapes of status.snapshotCreated, a failed checkpoint, two pause/resume rounds, a fork or resume GKE didn't restore, and clean-up. Disabling the marker check makes the two restore tests fail. The fake's PATCH now applies real JSON merge patches; the existing tests still pass.

What did not run

Nothing ran on GKE. TestLivePodSnapshots (build tags live,experimental_adapters, AGENTSANDBOX_SNAPSHOT_POLICY) is written to check a running process and a file through a fork and a pause/resume, that a new sandbox does not restore anything, and to log each step's time. It needs a GKE cluster with --enable-pod-snapshots. Other gaps:

  • The shape of status.snapshotCreated: Google's docs disagree (string or {name}), so both are read.
  • Failure reason names, which Google doesn't document: matched on Fail/Error, otherwise a checkpoint waits until snapshotTimeout.
  • Whether GKE's managed add-on serves agent-sandbox's v1beta1 API.

🤖 Generated with Claude Code

@reachjalil
reachjalil force-pushed the feat/agent-sandbox-gke-pod-snapshots branch from 03acbf4 to 155dab7 Compare November 20, 2026 02:07
reachjalil and others added 3 commits November 19, 2026 18:15
…dbox

The agent-sandbox adapter (experimental) gains GKE Pod Snapshots
(podsnapshot.gke.io/v1) for Sandboxes that run under gVisor, turned on with
config.podSnapshotPolicy:

- Snapshot: a PodSnapshotManualTrigger for the sandbox's pod, waited on
  until the PodSnapshot is Ready. It captures memory, processes and the
  root filesystem; the sandbox keeps running.
- Fork: a Sandbox with the source's pod spec and snapshot group, naming
  the snapshot in podsnapshot.gke.io/ps-name.
- Pause/resume (lifecycle.pause-checkpoint): checkpoint, then suspend, so
  the pod is deleted and the sandbox holds no slot; resume restores it.
  Only the newest pause checkpoint is kept, and delete removes them.

GKE restores any new pod whose spec matches the newest snapshot unless
snapshots are grouped, which could start one tenant's sandbox from
another's memory. Every sandbox's pod therefore gets a snapshot-group
label of its own, and the pool is unhealthy unless the PodSnapshotPolicy
selects on and groups by it, is manual, keeps whole pods and does not
evict per group. GKE also starts a pod fresh, silently, when it cannot
restore it, so the adapter writes a token before each checkpoint and
reads it back after a restore: a fork without it fails and is deleted,
and a resume without it suspends the sandbox again.

Tested with a fake API server that plays GKE's snapshot controller; the
fake now applies real JSON merge patches. TestLivePodSnapshots is written
but has not run on GKE.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Jalil <reachjalil@gmail.com>
…il closed

Addresses an independent review of the GKE Pod Snapshots support:

- A fork stayed in its source's snapshot group, so a source whose pod GKE
  recreated (a node drain, or stop/start) could come back from its fork's
  newer snapshot, crossing projects within an org. Once its restore is
  verified, a fork now moves to a group of its own and stops naming the
  snapshot; agent-sandbox's controller patches the running pod's labels
  rather than recreating it, and the adapter waits until it has.
- After a resume the template kept naming the pause checkpoint, so a
  recreated pod rolled back to it. Resume now clears it.
- Stop/start would restore the newest checkpoint over a newer volume; it is
  no longer offered with snapshots.
- A pause whose suspend did not finish left the Sandbox suspended while the
  scheduler recorded it running. It is now set running again, and the
  timeout is sandbox_failed.
- A resume GKE never restores retried for ever. After three, the sandbox
  reports an error, which fails it.
- One marker file was shared by concurrent checkpoints. Each checkpoint now
  has its own.
- Pause checkpoints of Sandboxes deleted behind Differently's back (the TTL
  backstop) are collected on List; triggers carry the installation label.
- A fork ran at its source's size whatever was asked for, so capacity and
  quotas under-counted it. A fork at another size is now refused.

The fake now restores as GKE documents it (the named snapshot, or else the
newest in the pod's group), and new tests cover each case above.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Jalil <reachjalil@gmail.com>
…view

- A pause whose context is cancelled (the reconciler recorded the pause
  and the scheduler stopped waiting) is no longer undone; only a suspend
  that times out is. A sandbox reports paused only once its pod is gone,
  pending until then. A failed undo is reported.
- Only a pod that answers without the marker counts as not restored; a
  check that could not run suspends the sandbox again without counting.
  A sandbox that reaches the limit keeps its pause checkpoint, relabelled
  unrestored, when it is deleted: it is the only copy of its state.
- A fork whose Create was interrupted is finished by List (checked and
  moved to its own group) or deleted so it is created again; it is never
  listed while still in its source's group, where the reconciler would
  adopt it.
- Orphaned pause checkpoints are collected only when older than 15
  minutes, so a sandbox created during a long List keeps its own.

The Role in the docs gains patch on podsnapshotmanualtriggers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Jalil <reachjalil@gmail.com>
@reachjalil
reachjalil force-pushed the feat/agent-sandbox-gke-pod-snapshots branch from 54e37d6 to f6c0ed9 Compare November 20, 2026 02:15
The GKE Agent Sandbox entry named the agentsandbox adapter, which the
agent-sandbox entry already does; the console's catalog test requires
each adapter name once. The GKE page links to the agent-sandbox adapter
instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Jalil <reachjalil@gmail.com>

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI passed at 5302aff.

@reachjalil
reachjalil merged commit 2b08279 into pre-release Nov 20, 2026
22 checks passed
@reachjalil
reachjalil deleted the feat/agent-sandbox-gke-pod-snapshots branch November 20, 2026 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant