Skip to content

armgit / ax

Public Imported from GitHub
0

Agent Executor (AX) is Google's open agentic orchestration runtime

Go

Sandbox enhancements (#79)

JoyceLiu9c740d90e39f
diff --git a/.gitignore b/.gitignore
index b416f85..b7fd4f7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,3 +8,5 @@ gar-bin
google-cloud-cli-darwin-arm.tar.gz
google-cloud-sdk/
test-sandbox-config.yaml
+sandbox-router
+examples/sandbox_agent/cloudbuild.yaml
diff --git a/README.md b/README.md
index 85b20a7..023a388 100644
--- a/README.md
+++ b/README.md
@@ -306,7 +306,7 @@ See `examples/remote_agent/main.go` for a complete implementation.
See `examples/remote_agent/main.go` for a complete implementation.
-### Sandbox Agent (GKE Integration)
+### Sandbox Agent (GKE Sandbox Integration)
GAR supports dynamically provisioning secure, isolated agents on Google Kubernetes Engine (GKE) via the [Agent Sandbox](https://github.com/kubernetes-sigs/agent-sandbox) feature. When a component requires a Sandbox Agent, the GAR server requests a temporary remote agent container in the cluster, establishes a secure connection locally (using port-forwarding via a proxy service), and cleans up the sandbox claim automatically upon closing.
@@ -364,11 +364,11 @@ docker build -t gar-uppercase:latest -f examples/sandbox_agent/Dockerfile .
**2. Publish Image to Registry**
When deploying to a cluster, you can host the agent container image in **any standard container registry** accessible by your Kubernetes cluster (e.g., Docker Hub, Google Artifact Registry, GitHub Container Registry).
- For local testing (Minikube/Kind), load the image directly: `minikube image load gar-uppercase:latest`
-- For production, update the `image` field in `examples/sandbox_agent/sandbox-template.yaml` to your full registry path.
+- For production, update the `image` field in `examples/sandbox_agent/sandbox-template-and-pool.yaml` to your full registry path.
Once the image is available, register the SandboxTemplate:
```bash
-kubectl apply -f examples/sandbox_agent/sandbox-template.yaml
+kubectl apply -f examples/sandbox_agent/sandbox-template-and-pool.yaml
```
**3. Configure gar.yaml**
diff --git a/agent/sandbox.go b/agent/sandbox.go
index 9fd6ab9..f382f63 100644
--- a/agent/sandbox.go
+++ b/agent/sandbox.go
@@ -33,7 +33,6 @@ type SandboxAgent struct {
client *sandboxclient.Client
config SandboxAgentConfig
inCluster bool
- poolName string
claimMu sync.Mutex
activeClaims map[string]struct{}
@@ -45,7 +44,6 @@ type SandboxAgentConfig struct {
SandboxTemplateRef string
ContainerPort int
- WarmPoolSize int
UseRouter bool
RouterAddress string
}
@@ -75,20 +73,7 @@ func newSandboxAgentWithClient(ctx context.Context, client *sandboxclient.Client
activeClaims: make(map[string]struct{}),
}
- // Create a SandboxWarmPool if configured
- if config.WarmPoolSize > 0 {
- poolName := fmt.Sprintf("gar-pool-%s", config.ID)
- agent.poolName = poolName
-
- // TODO: This forced recreation can cause a race condition if multiple
- // GAR controller instances are running in the same namespace with the
- // same agent ID. Consider using a 'Get or Create' pattern with
- // version/generation checks for multi-instance deployments.
- _ = client.DeleteWarmPool(ctx, poolName)
- if err := client.CreateWarmPool(ctx, poolName, config.SandboxTemplateRef, config.WarmPoolSize); err != nil {
- return nil, fmt.Errorf("failed to create SandboxWarmPool: %w", err)
- }
- }
+
return agent, nil
}
@@ -229,8 +214,8 @@ func findAvailablePort() (int, error) {
return l.Addr().(*net.TCPAddr).Port, nil
}
-// Close gracefully deletes the SandboxWarmPool if one was created,
-// and shreds all active SandboxClaims generated by this agent during interactive sessions.
+// Close gracefully shreds all active SandboxClaims generated by this agent
+// during interactive sessions.
func (a *SandboxAgent) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
@@ -245,11 +230,7 @@ func (a *SandboxAgent) Close() error {
}
a.claimMu.Unlock()
- if a.poolName != "" {
- if err := a.client.DeleteWarmPool(ctx, a.poolName); err != nil {
- errs = append(errs, fmt.Sprintf("failed to delete warm pool %q: %v", a.poolName, err))
- }
- }
+
if len(errs) > 0 {
return fmt.Errorf("cleanup errors: %s", strings.Join(errs, "; "))
diff --git a/examples/sandbox_agent/main.go b/examples/sandbox_agent/main.go
index 2624f09..08ddb27 100644
--- a/examples/sandbox_agent/main.go
+++ b/examples/sandbox_agent/main.go
@@ -1,3 +1,17 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
package main
import (
@@ -36,20 +50,29 @@ func (s *server) Process(stream grpc.BidiStreamingServer[proto.ProcessRequest, p
}
var outputs []*proto.Content
- for _, content := range req.Contents {
- // Only process text messages
- if textContent := content.GetText(); textContent != nil {
+
+ if len(req.Contents) > 0 {
+ // Find the most recent message from the "user" in the session history
+ var targetText string
+ for i := len(req.Contents) - 1; i >= 0; i-- {
+ if text := req.Contents[i].GetText(); text != nil && req.Contents[i].Role == "user" {
+ targetText = text.Text
+ break
+ }
+ }
+
+ if targetText != "" {
// Convert to upper case dynamically!
- upper := strings.ToUpper(textContent.Text)
+ upper := strings.ToUpper(targetText)
- log.Printf("📥 Received request text: %q", textContent.Text)
+ log.Printf("📥 Processed resolved text: %q", targetText)
log.Printf("📤 Sending response: %q", upper)
outputs = append(outputs, &proto.Content{
Role: "agent",
Content: &proto.Content_Text{
Text: &proto.TextContent{
- Text: "hey im your sandbox agent",
+ Text: "Hey, I'm your sandbox agent.\n",
},
},
})
@@ -57,7 +80,7 @@ func (s *server) Process(stream grpc.BidiStreamingServer[proto.ProcessRequest, p
Role: "agent",
Content: &proto.Content_Text{
Text: &proto.TextContent{
- Text: fmt.Sprintf("HERE IS YOUR UPPERCASE TEXT: %s", upper),
+ Text: fmt.Sprintf("here is your upper case text: %s", upper),
},
},
})
diff --git a/examples/sandbox_agent/main_test.go b/examples/sandbox_agent/main_test.go
new file mode 100644
index 0000000..20b14eb
--- /dev/null
+++ b/examples/sandbox_agent/main_test.go
@@ -0,0 +1,93 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package main
+
+import (
+ "testing"
+ "strings"
+ "fmt"
+
+ "github.com/google/gar/proto"
+)
+
+func TestExtractLatestPayload(t *testing.T) {
+ // Let's create a simulated history payload
+ history := []*proto.Content{
+ {
+ Role: "user",
+ Content: &proto.Content_Text{
+ Text: &proto.TextContent{Text: "old user prompt"},
+ },
+ },
+ {
+ Role: "agent",
+ Content: &proto.Content_Text{
+ Text: &proto.TextContent{Text: "old agent response"},
+ },
+ },
+ {
+ Role: "user",
+ Content: &proto.Content_Text{
+ Text: &proto.TextContent{Text: "find this text plz plz plz"},
+ },
+ },
+ {
+ Role: "agent", // System might wrap it in something like this during delegation
+ Content: &proto.Content_FunctionCall{
+ FunctionCall: &proto.FunctionCallContent{
+ Name: "uppercase-agent",
+ },
+ },
+ },
+ }
+
+ // 1. Emulate the agent logic exactly!
+ var targetText string
+ for i := len(history) - 1; i >= 0; i-- {
+ // As per our fix, we just grab the last raw text we can find from a user
+ if text := history[i].GetText(); text != nil && history[i].Role == "user" {
+ targetText = text.Text
+ break
+ }
+ }
+
+ if targetText != "find this text plz plz plz" {
+ t.Errorf("Failed to identify correct user substring! Got: %q", targetText)
+ }
+
+ // 2. Format output
+ upper := strings.ToUpper(targetText)
+ var outputs []*proto.Content
+ outputs = append(outputs, &proto.Content{
+ Role: "agent",
+ Content: &proto.Content_Text{
+ Text: &proto.TextContent{
+ Text: "Hey, I'm your sandbox agent.\n",
+ },
+ },
+ })
+ outputs = append(outputs, &proto.Content{
+ Role: "agent",
+ Content: &proto.Content_Text{
+ Text: &proto.TextContent{
+ Text: fmt.Sprintf("here is your upper case text: %s", upper),
+ },
+ },
+ })
+
+ if len(outputs) != 2 || !strings.Contains(outputs[1].GetText().Text, strings.ToUpper("find this text plz plz plz")) {
+ t.Errorf("Outputs failed! Got: %v", outputs)
+ }
+}
diff --git a/examples/sandbox_agent/sandbox-template.yaml b/examples/sandbox_agent/sandbox-template-and-pool.yaml
similarity index 64%
rename from examples/sandbox_agent/sandbox-template.yaml
rename to examples/sandbox_agent/sandbox-template-and-pool.yaml
index a9a820f..d5baf16 100644
--- a/examples/sandbox_agent/sandbox-template.yaml
+++ b/examples/sandbox_agent/sandbox-template-and-pool.yaml
@@ -1,5 +1,11 @@
# This is the Kubernetes manifest defining the execution environment for the agent.
# It is applied strictly to the GKE Agent Sandbox controllers to define the Sandbox.
+#
+# AGENT SEGREGATION:
+# To support multiple different agents, you MUST create a separate SandboxTemplate
+# and SandboxWarmPool for each agent with different sandbox requirements.
+# The `sandboxTemplateRef` in the WarmPool and `gar.yaml` maps the GAR logic to the specific isolated environment.
+#
# For more details on GKE Agent Sandbox container requirements, see:
# https://docs.cloud.google.com/kubernetes-engine/docs/how-to/agent-sandbox
apiVersion: extensions.agents.x-k8s.io/v1alpha1
@@ -26,3 +32,13 @@ spec:
port: 8494
initialDelaySeconds: 1
periodSeconds: 2
+---
+apiVersion: extensions.agents.x-k8s.io/v1alpha1
+kind: SandboxWarmPool
+metadata:
+ name: uppercase-agent-warmpool
+ namespace: default
+spec:
+ replicas: 2
+ sandboxTemplateRef:
+ name: uppercase-agent-template
diff --git a/internal/config/config.go b/internal/config/config.go
index 0360df3..9e66900 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -93,10 +93,9 @@ type SandboxAgentConfig struct {
Description string `yaml:"description"` // Description of agent capabilities
SandboxTemplateRef string `yaml:"sandbox_template_ref"` // Name of the SandboxTemplate CR to use
// TODO: implement shutdown_time for automatic sandbox shutdown
- ContainerPort int `yaml:"container_port,omitempty"` // Optional container port, defaults to 8494
- WarmPoolSize int `yaml:"warm_pool_size,omitempty"` // Optional warm pool size for sandbox agents
- UseRouter bool `yaml:"use_router,omitempty"` // Override port-forwarding to use Sandbox Router
- Metadata map[string]string `yaml:"metadata,omitempty"` // Optional metadata
+ ContainerPort int `yaml:"container_port,omitempty"`// Optional container port, defaults to 8494
+ UseRouter bool `yaml:"use_router,omitempty"` // Override port-forwarding to use Sandbox Router
+ Metadata map[string]string `yaml:"metadata,omitempty"` // Optional metadata
}
type LocalAgentConfig struct {
diff --git a/internal/controller/registry.go b/internal/controller/registry.go
index c2539bd..b23ab36 100644
--- a/internal/controller/registry.go
+++ b/internal/controller/registry.go
@@ -152,7 +152,6 @@ func (r *Registry) RegisterSandbox(ctx context.Context, cfg config.SandboxAgentC
sandboxAgent, err := agent.NewSandboxAgent(ctx, agent.SandboxAgentConfig{
ID: cfg.ID,
SandboxTemplateRef: cfg.SandboxTemplateRef,
- WarmPoolSize: cfg.WarmPoolSize,
ContainerPort: cfg.ContainerPort,
UseRouter: cfg.UseRouter,
})
diff --git a/internal/sandboxclient/client.go b/internal/sandboxclient/client.go
index 5a7e943..2313982 100644
--- a/internal/sandboxclient/client.go
+++ b/internal/sandboxclient/client.go
@@ -43,11 +43,6 @@ var (
Version: "v1alpha1",
Resource: "sandboxclaims",
}
- SandboxWarmPoolGVR = schema.GroupVersionResource{
- Group: "extensions.agents.x-k8s.io",
- Version: "v1alpha1",
- Resource: "sandboxwarmpools",
- }
)
// Client represents a high-level client for managing Sandbox resources in Kubernetes.
@@ -80,33 +75,6 @@ func NewClient(namespace string) (*Client, error) {
}, nil
}
-// CreateWarmPool creates a pre-warmed pool of sandbox environments.
-func (c *Client) CreateWarmPool(ctx context.Context, poolName, templateRef string, size int) error {
- pool := &unstructured.Unstructured{
- Object: map[string]interface{}{
- "apiVersion": "extensions.agents.x-k8s.io/v1alpha1",
- "kind": "SandboxWarmPool",
- "metadata": map[string]interface{}{
- "name": poolName,
- "namespace": c.namespace,
- },
- "spec": map[string]interface{}{
- "replicas": int64(size),
- "sandboxTemplateRef": map[string]interface{}{
- "name": templateRef,
- },
- },
- },
- }
-
- _, err := c.dynClient.Resource(SandboxWarmPoolGVR).Namespace(c.namespace).Create(ctx, pool, metav1.CreateOptions{})
- return err
-}
-
-// DeleteWarmPool removes a warm pool.
-func (c *Client) DeleteWarmPool(ctx context.Context, poolName string) error {
- return c.dynClient.Resource(SandboxWarmPoolGVR).Namespace(c.namespace).Delete(ctx, poolName, metav1.DeleteOptions{})
-}
// CreateClaim provisions an ephemeral sandbox execution environment.
func (c *Client) CreateClaim(ctx context.Context, claimName, templateRef string) error {