Skip to content

armgit / ax

Public Imported from GitHub
0

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

Go

fix(controller): fail task when ActorTemplate creation fails (#472)

Simran Kaur7b3347f5a8af
diff --git a/docs/manifests.md b/docs/manifests.md
index 603c2ef..1789762 100644
--- a/docs/manifests.md
+++ b/docs/manifests.md
@@ -34,6 +34,10 @@ spec:
debug: true # serve guest services inside the sandbox so `ax ssh` works; off by default
```
+### Image references
+
+Substrate requires `spec.image` to be pinned by digest, e.g. `ghcr.io/my-org/my-agent-image@sha256:...`. A tag-only reference such as `my-agent:v1` is rejected when the task's ActorTemplate is created, because a tag can move and would silently invalidate the actor's snapshots. If that happens the task reports `Ready=False` with reason `TemplateCreationFailed` and the Substrate error as the message. Resolve a tag to its digest with `docker buildx imagetools inspect <image:tag>` or `crane digest <image:tag>`.
+
### Binding a workspace
A task binds at most one `Workspace` through `spec.workspace`. The binding needs a `name`; without a `path` the workspace is mounted at `/workspace`. That path is the working directory of `spec.command`, and the task reports `WorkspaceReady` once the workspace is prepared. A task with no `workspace` gets an empty working directory at `/workspace`.
diff --git a/internal/controller/reconciler.go b/internal/controller/reconciler.go
index 136fe8e..5e31e1d 100644
--- a/internal/controller/reconciler.go
+++ b/internal/controller/reconciler.go
@@ -191,16 +191,21 @@ func (r *TaskReconciler) Reconcile(ctx context.Context, task *v1alpha1.Task, ws
// spec.resources rides along in AX_TASK_YAML, so a limits change is already
// part of the template digest and re-provisions the template.
tmpl, err := r.client.EnsureActorTemplateWithImage(ctx, templateAtespace, templateName, atespace, customTemplateName, task.Spec.Image, resources, extraEnv)
- if err != nil && resources != nil {
- // The default template does not carry the task's limits, so falling back
- // would silently run the task unconstrained.
- r.setNotReady(task, "TemplateCreationFailed", err.Error(), now)
+ if err != nil {
+ if substrate.IsTransientError(err) {
+ r.setNotReady(task, "SubstrateUnavailable", err.Error(), now)
+ task.Status.Phase = "Pending"
+ return task, fmt.Errorf("ensuring actor template: %w", err)
+ }
+ // Falling back to the default template would run a different image than
+ // the user asked for, without the limits it asked for, or fail later with
+ // a misleading "actor template not found". Surface the real error on the
+ // task and stop here.
+ r.setNotReady(task, "TemplateCreationFailed", substrateErrorMessage(err), now)
task.Status.Phase = "Failed"
return task, fmt.Errorf("ensuring actor template: %w", err)
}
- if err != nil {
- slog.Warn("could not create custom ActorTemplate, falling back to default template", "error", err)
- } else if tmpl != nil && tmpl.Metadata != nil {
+ if tmpl != nil && tmpl.Metadata != nil {
templateAtespace = tmpl.Metadata.Atespace
templateName = tmpl.Metadata.Name
slog.Info("using custom ActorTemplate for actor", "templateAtespace", templateAtespace, "templateName", templateName)
@@ -362,6 +367,23 @@ func (r *TaskReconciler) conditionTrue(task *v1alpha1.Task, condType string) boo
return false
}
+// substrateErrorMessage returns the message to show users for a failed Substrate call.
+// Substrate errors reach the reconciler wrapped (fmt.Errorf("...: %w", grpcErr)), and
+// status.Convert on a wrapped error yields the full err.Error() including the
+// "rpc error: code = ... desc = " boilerplate. Unwrapping to the underlying gRPC
+// status recovers just the description Substrate wrote, e.g.
+// `actor_template.containers[0].image: Invalid value: "...": must be pinned by digest`.
+// Non-gRPC errors are returned verbatim.
+func substrateErrorMessage(err error) string {
+ var gs interface{ GRPCStatus() *status.Status }
+ if errors.As(err, &gs) {
+ if s := gs.GRPCStatus(); s != nil && s.Message() != "" {
+ return s.Message()
+ }
+ }
+ return err.Error()
+}
+
func (r *TaskReconciler) setCondition(task *v1alpha1.Task, condType, status, reason, message string, t time.Time) {
ts := timestamppb.New(t)
for i, c := range task.Status.Conditions {
diff --git a/internal/controller/reconciler_test.go b/internal/controller/reconciler_test.go
index 78d47c0..37842be 100644
--- a/internal/controller/reconciler_test.go
+++ b/internal/controller/reconciler_test.go
@@ -393,6 +393,140 @@ func TestTaskReconciler_SuspendError(t *testing.T) {
assertCondition(t, reconciled, "Ready", "True", "TaskRunning")
}
+// TestTaskReconciler_TemplateCreationFailed covers google/ax#366: when Substrate
+// rejects the per-task ActorTemplate (for example because the image is referenced
+// by tag rather than digest), the task must report that error directly instead of
+// silently falling back to the default template.
+func TestTaskReconciler_TemplateCreationFailed(t *testing.T) {
+ ctx := t.Context()
+
+ lis, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ t.Fatalf("failed to listen: %v", err)
+ }
+ defer lis.Close()
+
+ const substrateMsg = `actor_template.containers[0].image: Invalid value: "localhost:5001/my-agent:v1": must be pinned by digest (changing the image invalidates snapshots)`
+ mockSrv := &mockControlServer{
+ createTemplateErr: status.Error(codes.InvalidArgument, substrateMsg),
+ }
+ grpcServer := grpc.NewServer()
+ ateapipb.RegisterControlServer(grpcServer, mockSrv)
+ go grpcServer.Serve(lis)
+ defer grpcServer.Stop()
+
+ client, err := substrate.NewClient(lis.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
+ if err != nil {
+ t.Fatalf("failed to create substrate client: %v", err)
+ }
+ defer client.Close()
+
+ reconciler := controller.NewTaskReconciler(client, "test-template", "ax-system")
+ reconciler.SecretResolver = noSecrets
+ reconciler.WorkspaceReadyTimeout = 200 * time.Millisecond
+
+ task := &v1alpha1.Task{
+ ApiVersion: v1alpha1.APIVersion,
+ Kind: v1alpha1.KindTask,
+ Metadata: &v1alpha1.ObjectMeta{
+ Name: "tagged-image",
+ Atespace: "default",
+ },
+ Spec: &v1alpha1.TaskSpec{
+ Image: "localhost:5001/my-agent:v1",
+ Command: []string{"python3", "/app/agent.py"},
+ },
+ Status: &v1alpha1.TaskStatus{Phase: "Running"},
+ }
+
+ reconciled, err := reconciler.Reconcile(ctx, task, nil)
+ if err == nil {
+ t.Fatal("expected Reconcile to fail when the ActorTemplate cannot be created")
+ }
+ if !strings.Contains(err.Error(), "must be pinned by digest") {
+ t.Errorf("expected error to carry the Substrate message, got %v", err)
+ }
+
+ if reconciled.Status.Phase != "Failed" {
+ t.Errorf("expected phase 'Failed', got %q", reconciled.Status.Phase)
+ }
+ assertCondition(t, reconciled, "Ready", "False", "TemplateCreationFailed")
+ for _, c := range reconciled.Status.Conditions {
+ if c.Type != "Ready" {
+ continue
+ }
+ // The user-facing message should be exactly what Substrate said, without the
+ // "rpc error: code = ... desc = " wrapper or our internal context prefix.
+ if c.Message != substrateMsg {
+ t.Errorf("expected Ready message to be the bare Substrate error\n got: %q\nwant: %q", c.Message, substrateMsg)
+ }
+ }
+
+ // The reconcile must stop before touching the actor: no fallback to the default template.
+ if len(mockSrv.createdActors) != 0 {
+ t.Errorf("expected no actor to be created, got %v", mockSrv.createdActors)
+ }
+ if len(mockSrv.resumedActors) != 0 {
+ t.Errorf("expected no actor to be resumed, got %v", mockSrv.resumedActors)
+ }
+}
+
+// TestTaskReconciler_TemplateCreationTransient checks that a transient Substrate
+// error during ActorTemplate creation (for example Unavailable after retries are
+// exhausted) leaves the task Pending for a later retry rather than permanently Failed.
+func TestTaskReconciler_TemplateCreationTransient(t *testing.T) {
+ ctx := t.Context()
+
+ lis, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ t.Fatalf("failed to listen: %v", err)
+ }
+ defer lis.Close()
+
+ // DeadlineExceeded is transient per substrate.IsTransientError but is not in the
+ // client's retry list, so the test does not wait out the Unavailable backoff.
+ mockSrv := &mockControlServer{
+ createTemplateErr: status.Error(codes.DeadlineExceeded, "substrate control plane timed out"),
+ }
+ grpcServer := grpc.NewServer()
+ ateapipb.RegisterControlServer(grpcServer, mockSrv)
+ go grpcServer.Serve(lis)
+ defer grpcServer.Stop()
+
+ client, err := substrate.NewClient(lis.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
+ if err != nil {
+ t.Fatalf("failed to create substrate client: %v", err)
+ }
+ defer client.Close()
+
+ reconciler := controller.NewTaskReconciler(client, "test-template", "ax-system")
+ reconciler.SecretResolver = noSecrets
+ reconciler.WorkspaceReadyTimeout = 200 * time.Millisecond
+
+ task := &v1alpha1.Task{
+ ApiVersion: v1alpha1.APIVersion,
+ Kind: v1alpha1.KindTask,
+ Metadata: &v1alpha1.ObjectMeta{
+ Name: "template-transient",
+ Atespace: "default",
+ },
+ Spec: &v1alpha1.TaskSpec{Image: "ghcr.io/my-org/my-agent@sha256:0000000000000000000000000000000000000000000000000000000000000000"},
+ Status: &v1alpha1.TaskStatus{Phase: "Running"},
+ }
+
+ reconciled, err := reconciler.Reconcile(ctx, task, nil)
+ if err == nil {
+ t.Fatal("expected Reconcile to return an error on transient template failure")
+ }
+ if reconciled.Status.Phase != "Pending" {
+ t.Errorf("expected phase 'Pending', got %q", reconciled.Status.Phase)
+ }
+ assertCondition(t, reconciled, "Ready", "False", "SubstrateUnavailable")
+ if len(mockSrv.createdActors) != 0 {
+ t.Errorf("expected no actor to be created, got %v", mockSrv.createdActors)
+ }
+}
+
func TestTaskReconciler_WorkspaceReady(t *testing.T) {
ctx := context.Background()
@@ -682,23 +816,24 @@ func TestTaskReconciler_ResourceLimitsFailurePaths(t *testing.T) {
}
})
- t.Run("rejected template without limits still falls back", func(t *testing.T) {
+ t.Run("rejected template without limits does not fall back either", func(t *testing.T) {
mockSrv := &mockControlServer{
createTemplateErr: status.Error(codes.InvalidArgument, "image must be pinned by digest"),
}
reconciler := setup(t, mockSrv)
reconciled, err := reconciler.Reconcile(ctx, newTask("no-limits", nil), nil)
- if err != nil {
- t.Fatalf("Reconcile without limits failed: %v", err)
+ if err == nil {
+ t.Fatal("Reconcile succeeded although the template was rejected")
}
- // A fresh task is provisioned on the fallback template and then
- // suspended until it is executed.
- if reconciled.Status.Phase != "Suspended" {
- t.Errorf("phase = %q, want Suspended", reconciled.Status.Phase)
+ // Falling back would run a different image than the task asked for, so the
+ // template error is terminal regardless of whether limits are set (#366).
+ if reconciled.Status.Phase != "Failed" {
+ t.Errorf("phase = %q, want Failed", reconciled.Status.Phase)
}
- if len(mockSrv.createdActors) != 1 {
- t.Errorf("fallback did not create the actor: %v", mockSrv.createdActors)
+ assertCondition(t, reconciled, "Ready", "False", "TemplateCreationFailed")
+ if len(mockSrv.createdActors) != 0 {
+ t.Errorf("task ran on a fallback template: actors=%v", mockSrv.createdActors)
}
})
}