Skip to content

armgit / ax

Public Imported from GitHub
0

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

Go

Prevent agent resumption if the agent ID has changed (#144)

Jaana Dogan0f4fe2e04c31
diff --git a/internal/controller/executor/executor.go b/internal/controller/executor/executor.go
index 260cd70..27f994b 100644
--- a/internal/controller/executor/executor.go
+++ b/internal/controller/executor/executor.go
@@ -17,6 +17,7 @@ package executor
import (
"context"
"errors"
+ "fmt"
"github.com/google/ax/internal/agent"
"github.com/google/ax/proto"
@@ -53,11 +54,15 @@ func (tm *defaultExecutor) Exec(ctx context.Context, execID string, start *proto
return errors.New("no agent found")
}
- allInputs, state, err := history(ctx, tm.eventLog, execID)
+ allInputs, state, earlierAgentID, err := history(ctx, tm.eventLog, execID)
if err != nil {
return err
}
+ if earlierAgentID != "" && earlierAgentID != start.AgentId {
+ return fmt.Errorf("resumption not allowed: agent ID changed from %s to %s", earlierAgentID, start.AgentId)
+ }
+
if state == proto.State_STATE_COMPLETED {
return nil
}
@@ -70,7 +75,7 @@ func (tm *defaultExecutor) exec(
start *proto.AgentStart,
el EventLog,
a agent.Agent,
- allInputs []*proto.Content,
+ history []*proto.Content,
o agent.OutputHandler) error {
child := &defaultExecutor{
id: execID,
@@ -87,15 +92,15 @@ func (tm *defaultExecutor) exec(
return nil
}
- allInputs = append(allInputs, start.Contents...)
- if len(allInputs) == 0 {
+ history = append(history, start.Contents...)
+ if len(history) == 0 {
return errors.New("no inputs")
}
if err := logPending(ctx, el, execID, start); err != nil {
return err
}
- start.Contents = allInputs
+ start.Contents = history
if err := a.Connect(ctx, execID, start, child, outputBuffer); err != nil {
_ = logFailed(ctx, el, execID, start) // Attempt to log failure, but prioritize returning the original error.
return err
@@ -118,19 +123,23 @@ func (tm *defaultExecutor) exec(
return nil
}
-func history(ctx context.Context, el EventLog, execID string) ([]*proto.Content, proto.State, error) {
+func history(ctx context.Context, el EventLog, execID string) ([]*proto.Content, proto.State, string, error) {
events, err := el.Events(ctx, execID)
if err != nil {
- return nil, proto.State_STATE_UNSPECIFIED, err
+ return nil, proto.State_STATE_UNSPECIFIED, "", err
}
var history []*proto.Content
var state proto.State
+ var agentID string
for _, event := range events {
if event.ExecId != execID {
continue
}
+ if event.AgentId != "" {
+ agentID = event.AgentId
+ }
// Reset after the status change ensure
// that we have a clean state even if we are
// presented an event log with dirty entries
@@ -143,11 +152,7 @@ func history(ctx context.Context, el EventLog, execID string) ([]*proto.Content,
state = event.State
}
- if state == proto.State_STATE_COMPLETED || state == proto.State_STATE_FAILED {
- return history, state, nil
- }
-
- return history, state, nil
+ return history, state, agentID, nil
}
func logPending(ctx context.Context, el EventLog, execID string, start *proto.AgentStart) error {
diff --git a/internal/controller/executor/executor_test.go b/internal/controller/executor/executor_test.go
index 5a74a7d..9ca31ad 100644
--- a/internal/controller/executor/executor_test.go
+++ b/internal/controller/executor/executor_test.go
@@ -18,6 +18,7 @@ import (
"context"
"fmt"
"log"
+ "strings"
"sync"
"sync/atomic"
"testing"
@@ -335,3 +336,40 @@ func TestResume(t *testing.T) {
t.Fatal(err)
}
}
+
+func TestResumeAgentIDMismatch(t *testing.T) {
+ ctx := context.Background()
+ eventLog := memoryEventLog()
+
+ registry := map[string]agent.Agent{
+ "root": AgentFunc(func(inputs []*proto.Content, tm agent.Executor, o agent.OutputHandler) {
+ // Do nothing to leave it in PENDING state
+ }),
+ "other": AgentFunc(func(inputs []*proto.Content, tm agent.Executor, o agent.OutputHandler) {
+ }),
+ }
+
+ tm := DefaultExecutor(eventLog, registry)
+
+ // First run: starts as "root"
+ if err := tm.Exec(ctx, "task1", &proto.AgentStart{
+ AgentId: "root",
+ Contents: []*proto.Content{text("user", "hello!")},
+ }, nil); err != nil {
+ t.Fatal(err)
+ }
+
+ // Second run: attempts to resume as "other" for same execID "task1"
+ err := tm.Exec(ctx, "task1", &proto.AgentStart{
+ AgentId: "other",
+ Contents: []*proto.Content{text("user", "hello again!")},
+ }, nil)
+
+ if err == nil {
+ t.Fatal("expected error due to agent ID mismatch, got nil")
+ }
+
+ if !strings.Contains(err.Error(), "resumption not allowed") {
+ t.Fatalf("expected 'resumption not allowed' error, got: %v", err)
+ }
+}