Skip to content

armgit / ax

Public Imported from GitHub
0

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

Go

Introduce a gRPC interceptor to suspend the server actor (#279)

Jaana Dogan1cf68d66dbfd
diff --git a/Makefile b/Makefile
index 60e3f96..628f6d3 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-.PHONY: all build proto test clean install install-ate
+.PHONY: all build proto test clean install install-ate images
# Default container registry for docker
export KO_DOCKER_REPO ?= gcr.io/ax-container-images
@@ -86,3 +86,6 @@ ax-shell-image:
# Used to debug ax servers within a cluster.
@echo "Building ax shell container image with ko using busybox..."
KO_DOCKER_REPO=$(KO_DOCKER_REPO)/ax-shell KO_DEFAULTBASEIMAGE=busybox:1.36 GOFLAGS="-tags=ate" ko build --base-import-paths ./cmd/ax
+
+# Build all container images
+images: ax-image ate-agent-image k8s-sandbox-router-image axepp-image ax-shell-image
diff --git a/internal/server/server.go b/internal/server/server.go
index 9087295..2a77464 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -20,6 +20,7 @@ package server
import (
"context"
"fmt"
+ "log"
"net"
"google.golang.org/grpc"
@@ -50,21 +51,30 @@ func New(c *controller.Controller) *Server {
// Exec executes a new agentic task with streaming responses.
func (s *Server) Exec(req *proto.ExecRequest, stream grpc.ServerStreamingServer[proto.ExecResponse]) error {
- // Create output handler to stream outputs back to client
+ log.Printf("Executing %q...", req.ConversationId)
+ ctx := stream.Context()
+
outputHandler := controller.ExecHandler(func(resp *proto.ExecResponse) error {
return stream.Send(resp)
})
- return s.controller.Exec(stream.Context(), req, outputHandler)
+ err := s.controller.Exec(ctx, req, outputHandler)
+ go suspendActor(req.ConversationId) // TODO(jbd): Move to an interceptor.
+ return err
}
func (s *Server) ForkConversation(ctx context.Context, req *proto.ForkRequest) (*proto.ForkResponse, error) {
if req.SrcConversationId == "" {
return nil, status.Errorf(codes.InvalidArgument, "src_conversation_id is required")
}
+ if req.DestConversationId == "" {
+ return nil, status.Errorf(codes.InvalidArgument, "dest_conversation_id is required")
+ }
+
destID, err := s.controller.Fork(ctx, req.SrcConversationId, req.SrcSeq, req.DestConversationId)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to fork conversation: %v", err)
}
+ go suspendActor(req.DestConversationId) // TODO(jbd): Move to an interceptor.
return &proto.ForkResponse{ConversationId: destID}, nil
}
@@ -75,6 +85,7 @@ func (s *Server) DeleteConversation(ctx context.Context, req *proto.DeleteReques
if err := s.controller.Delete(ctx, req.ConversationId); err != nil {
return nil, status.Errorf(codes.Internal, "failed to delete conversation: %v", err)
}
+ go suspendActor(req.ConversationId) // TODO(jbd): Move to an interceptor.
return &proto.DeleteResponse{}, nil
}
@@ -97,7 +108,6 @@ func (s *Server) Serve(address string, opts ...grpc.ServerOption) error {
if err := s.grpcServer.Serve(lis); err != nil {
return fmt.Errorf("failed to serve: %w", err)
}
-
return nil
}
diff --git a/internal/server/server_ate.go b/internal/server/server_ate.go
new file mode 100644
index 0000000..f31878b
--- /dev/null
+++ b/internal/server/server_ate.go
@@ -0,0 +1,102 @@
+//go:build ate
+
+// 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 server
+
+import (
+ "context"
+ "crypto/tls"
+ "crypto/x509"
+ "log"
+ "os"
+ "time"
+
+ "github.com/google/ax/internal/experimental/k8s/ate"
+ "google.golang.org/grpc"
+ "google.golang.org/grpc/credentials"
+)
+
+func createATEClient() any {
+ if os.Getenv("AX_SUBSTRATE") != "1" {
+ return nil
+ }
+
+ ns := os.Getenv("AX_SUBSTRATE_NAMESPACE")
+ if ns == "" {
+ ns = "ax"
+ }
+ template := os.Getenv("AX_SUBSTRATE_TEMPLATE")
+ if template == "" {
+ template = "ax-template"
+ }
+ endpoint := os.Getenv("AX_SUBSTRATE_ENDPOINT")
+ credBundle := os.Getenv("AX_SUBSTRATE_CRED_BUNDLE")
+
+ clientTLSConfig := &tls.Config{
+ InsecureSkipVerify: true,
+ }
+
+ if credBundle != "" {
+ bundleBytes, err := os.ReadFile(credBundle)
+ if err != nil {
+ log.Printf("Warning: Failed to read bundle %s: %v", credBundle, err)
+ } else {
+ certPool := x509.NewCertPool()
+ if ok := certPool.AppendCertsFromPEM(bundleBytes); ok {
+ clientTLSConfig.RootCAs = certPool
+ log.Printf("Successfully loaded CA bundle from %s", credBundle)
+ }
+ }
+ }
+
+ log.Printf("Creating SubstrATE client (Namespace: %s, Template: %s, Endpoint: %s)", ns, template, endpoint)
+ client, err := ate.NewClient(ns, template, endpoint, grpc.WithTransportCredentials(credentials.NewTLS(clientTLSConfig)))
+ if err != nil {
+ log.Printf("Warning: Failed to create SubstrATE client: %v", err)
+ return nil
+ }
+ return client
+}
+
+func (s *Server) substrateInterceptors() []grpc.ServerOption {
+ client := createATEClient()
+ if client == nil {
+ return nil
+ }
+ c := client.(*ate.Client)
+ return []grpc.ServerOption{
+ grpc.ChainUnaryInterceptor(ate.NewUnaryServerInterceptor(c)),
+ grpc.ChainStreamInterceptor(ate.NewStreamServerInterceptor(c)),
+ }
+}
+
+func suspendActor(actorID string) {
+ client := createATEClient()
+ c := client.(*ate.Client)
+ defer c.Close()
+
+ log.Printf("Automatically suspending actor %s in 50 milliseconds...", actorID)
+ time.Sleep(50 * time.Millisecond)
+
+ suspendCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+
+ if _, err := c.SuspendActor(suspendCtx, actorID); err != nil {
+ log.Printf("Failed to automatically suspend actor %s: %v", actorID, err)
+ } else {
+ log.Printf("Successfully suspended actor %s", actorID)
+ }
+}
diff --git a/internal/server/server_no_ate.go b/internal/server/server_no_ate.go
new file mode 100644
index 0000000..63af0cd
--- /dev/null
+++ b/internal/server/server_no_ate.go
@@ -0,0 +1,27 @@
+//go:build !ate
+
+// 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 server
+
+import "google.golang.org/grpc"
+
+func (s *Server) substrateInterceptors() []grpc.ServerOption {
+ return nil
+}
+
+func suspendActor(actorID string) {
+ // No-op
+}
diff --git a/manifests/ax-deployment.yaml b/manifests/ax-deployment.yaml
index 9fa9534..c894d8f 100644
--- a/manifests/ax-deployment.yaml
+++ b/manifests/ax-deployment.yaml
@@ -33,6 +33,8 @@ spec:
env:
- name: GEMINI_API_KEY
value: "YOUR_API_KEY" # TODO: Use envFrom once it's available.
+ - name: AX_SUBSTRATE
+ value: "1"
---
apiVersion: ate.gke.io/v1alpha1
kind: ActorTemplate