Skip to content

armgit / ax

Public Imported from GitHub
0

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

Go
3.3 KB
Raw Blame
// 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.

// Command ax-task-runner is the entrypoint of every AX task container. It
// loads the Task and Workspace specs and hands them to the runner package,
// which does everything else.
//
// AX delivers the Task as YAML in AX_TASK_YAML and the bound Workspace, if
// any, as YAML in AX_WORKSPACE_YAML. For local runs the specs can be read from
// files instead with --task-file and --workspace-file.
package main

import (
	"context"
	"flag"
	"fmt"
	"log/slog"
	"os"
	"os/signal"
	"syscall"

	"github.com/google/ax/pkg/apis/v1alpha1"
	"github.com/google/ax/runner"
	"google.golang.org/protobuf/proto"
	"gopkg.in/yaml.v3"
)

func main() {
	var (
		cfg      runner.Config
		taskFile string
		wsFile   string
	)
	flag.IntVar(&cfg.Port, "port", runner.DefaultPort, "Port for the probe and guest server")
	flag.IntVar(&cfg.MetadataPort, "metadata-port", runner.DefaultMetadataPort, "Loopback-only port for the metadata endpoints")
	flag.StringVar(&taskFile, "task-file", "", "Read the Task YAML from this file instead of AX_TASK_YAML")
	flag.StringVar(&wsFile, "workspace-file", "", "Read the Workspace YAML from this file instead of AX_WORKSPACE_YAML")
	flag.Parse()

	logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
	slog.SetDefault(logger)

	var task v1alpha1.Task
	if err := loadSpec(taskFile, "AX_TASK_YAML", &task); err != nil {
		fatal(err)
	} else if task.GetMetadata() != nil {
		cfg.Task = &task
	}

	var ws v1alpha1.Workspace
	if err := loadSpec(wsFile, "AX_WORKSPACE_YAML", &ws); err != nil {
		fatal(err)
	} else if ws.GetMetadata() != nil {
		cfg.Workspace = &ws
	}

	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
	defer stop()

	if err := runner.Run(ctx, cfg); err != nil {
		fatal(err)
	}
}

// loadSpec decodes YAML into out from file when set, otherwise from the named
// environment variable. It is not an error for neither to be present, or for
// the YAML to be blank. Unknown fields are ignored, so a runner image keeps
// working with a newer control plane.
func loadSpec(file, envVar string, out proto.Message) error {
	var raw []byte
	switch {
	case file != "":
		data, err := os.ReadFile(file)
		if err != nil {
			return fmt.Errorf("reading %s: %w", file, err)
		}
		raw = data
	case os.Getenv(envVar) != "":
		raw = []byte(os.Getenv(envVar))
	default:
		return nil
	}
	var node yaml.Node
	if err := yaml.Unmarshal(raw, &node); err != nil {
		return fmt.Errorf("parsing %T: %w", out, err)
	}
	if len(node.Content) == 0 || node.Content[0].ShortTag() == "!!null" {
		return nil
	}
	if err := v1alpha1.UnmarshalYAMLLenient(node.Content[0], out); err != nil {
		return fmt.Errorf("parsing %T: %w", out, err)
	}
	return nil
}

func fatal(err error) {
	slog.Error("task runner failed", "error", err)
	os.Exit(1)
}