Skip to content

armgit / ax

Public Imported from GitHub
0

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

Go

Update `ax` to use cobra.Command.Context (#147)

Hyang-Ah Hana Kimb3c9316d7425
diff --git a/cmd/ax/exec.go b/cmd/ax/exec.go
index f69ef67..77df032 100644
--- a/cmd/ax/exec.go
+++ b/cmd/ax/exec.go
@@ -66,7 +66,7 @@ var (
)
func runExec(cmd *cobra.Command, args []string) error {
- ctx := context.Background()
+ ctx := cmd.Context()
// Generate UUID if no ID provided
if execID == "" {
diff --git a/cmd/ax/fork.go b/cmd/ax/fork.go
index 1671b82..306daa6 100644
--- a/cmd/ax/fork.go
+++ b/cmd/ax/fork.go
@@ -48,7 +48,7 @@ func init() {
}
func runFork(cmd *cobra.Command, args []string) error {
- ctx := context.Background()
+ ctx := cmd.Context()
// Generate UUID if no destination ID provided
if forkDestID == "" {
diff --git a/cmd/ax/register.go b/cmd/ax/register.go
index 3e95398..a1c6635 100644
--- a/cmd/ax/register.go
+++ b/cmd/ax/register.go
@@ -15,7 +15,6 @@
package main
import (
- "context"
"fmt"
"github.com/google/ax/proto"
@@ -50,7 +49,7 @@ func init() {
}
func runRegister(cmd *cobra.Command, args []string) error {
- ctx := context.Background()
+ ctx := cmd.Context()
conn, err := connect(registerServerAddr)
if err != nil {
return err
diff --git a/cmd/ax/serve.go b/cmd/ax/serve.go
index 330cc96..3e165a1 100644
--- a/cmd/ax/serve.go
+++ b/cmd/ax/serve.go
@@ -50,7 +50,7 @@ func init() {
}
func runServe(cmd *cobra.Command, args []string) error {
- ctx := context.Background()
+ ctx := cmd.Context()
// Load configuration from YAML file
cfg, err := config.LoadFromFile(serveConfigFile)
diff --git a/cmd/ax/trace.go b/cmd/ax/trace.go
index a67be55..9c93c9a 100644
--- a/cmd/ax/trace.go
+++ b/cmd/ax/trace.go
@@ -89,7 +89,7 @@ func init() {
func runTrace(cmd *cobra.Command, args []string) error {
// Load trace data
- data, err := loadTraceData(traceID)
+ data, err := loadTraceData(cmd.Context(), traceID)
if err != nil {
return fmt.Errorf("error loading trace data: %w", err)
}
@@ -107,11 +107,11 @@ func runTrace(cmd *cobra.Command, args []string) error {
return serveTraceUI(listener, data, indexHTML)
}
-func loadTraceData(rootExecID string) (*TraceData, error) {
+func loadTraceData(ctx context.Context, rootExecID string) (*TraceData, error) {
// The trace command uses the config provided by --config flag
configPath := traceConfigFile
- events, err := fetchEventsFromDB(configPath, rootExecID)
+ events, err := fetchEventsFromDB(ctx, configPath, rootExecID)
if err != nil {
return nil, err
}
@@ -124,7 +124,7 @@ func loadTraceData(rootExecID string) (*TraceData, error) {
return data, nil
}
-func fetchEventsFromDB(configPath string, rootExecID string) ([]*proto.ExecutionEvent, error) {
+func fetchEventsFromDB(ctx context.Context, configPath string, rootExecID string) ([]*proto.ExecutionEvent, error) {
cfg, err := config.LoadFromFile(configPath)
if err != nil {
return nil, fmt.Errorf("error loading config: %w", err)
@@ -136,7 +136,6 @@ func fetchEventsFromDB(configPath string, rootExecID string) ([]*proto.Execution
}
defer evLog.Close()
- ctx := context.Background()
events, err := evLog.EventsByPrefix(ctx, rootExecID)
if err != nil {
return nil, fmt.Errorf("failed to query trace events: %w", err)
diff --git a/internal/agent/k8s_sandbox.go b/internal/agent/k8s_sandbox.go
index 45e2a3b..4799f7b 100644
--- a/internal/agent/k8s_sandbox.go
+++ b/internal/agent/k8s_sandbox.go
@@ -83,7 +83,7 @@ func (a *KubernetesSandboxAgent) HealthCheck(ctx context.Context) error {
}
func (a *KubernetesSandboxAgent) Connect(ctx context.Context, execID string, start *proto.AgentStart, e Executor, o OutputHandler) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
// Use the id deterministically so interactive executions reuse the same sandbox pod,
diff --git a/internal/sandboxclient/client.go b/internal/sandboxclient/client.go
index 705101c..c16febb 100644
--- a/internal/sandboxclient/client.go
+++ b/internal/sandboxclient/client.go
@@ -140,7 +140,7 @@ func (c *Client) WaitForSandboxReady(ctx context.Context, claimName string) erro
// PortForward establishes a local connection to the remote sandbox pod over the given port.
// It returns a cleanup function the caller MUST execute when finished, closing the port-forward pipeline.
func (c *Client) PortForward(ctx context.Context, claimName, selector string, localPort, targetPort int) (cancelFunc func(), err error) {
- pfCtx, pfCancel := context.WithCancel(context.Background())
+ pfCtx, pfCancel := context.WithCancel(ctx)
// Wait for Sandbox to be ready first
waitCmd := exec.CommandContext(pfCtx, "kubectl", "wait", "--for=condition=Ready", "--namespace", c.namespace, "sandbox.agents.x-k8s.io/"+claimName, "--timeout=120s")
@@ -179,7 +179,7 @@ func (c *Client) PortForward(ctx context.Context, claimName, selector string, lo
// PortForwardRouter establishes a local connection to the Sandbox Router service.
// It returns a cleanup function the caller MUST execute when finished.
func (c *Client) PortForwardRouter(ctx context.Context, localPort, targetPort int) (cancelFunc func(), err error) {
- pfCtx, pfCancel := context.WithCancel(context.Background())
+ pfCtx, pfCancel := context.WithCancel(ctx)
// Wait for Router service to be ready
waitCmd := exec.CommandContext(pfCtx, "kubectl", "wait", "--for=condition=Ready", "--namespace", c.namespace, "pod", "-l", "app=sandbox-router", "--timeout=120s")