Agent Executor (AX) is Google's open agentic orchestration runtime
Go
fork: validate src_seq exists, and update README for fork requirement on dest_conversation_id (#292)
JoyceLiu6d60b40ac5e3
diff --git a/README.md b/README.mdindex fa2a51e..50068fd 100644--- a/README.md+++ b/README.md@@ -260,28 +260,28 @@ into a new event log.```bashax fork \--src-conversation <id> \+ --dest-conversation <id> \[--src-seq <number>] \- [--dest-conversation <id>] \[--server <address>]```Options:- `--src-conversation`: Source conversation ID to fork from (required)-- `--src-seq`: Sequence number to fork from (optional, defaults to latest)-- `--dest-conversation`: Destination conversation ID (optional, generates UUID if not provided)+- `--dest-conversation`: Destination conversation ID for the new event log (required)+- `--src-seq`: Sequence number to fork from (optional, defaults to latest). Must be a sequence that exists in the source conversation.- `--server`: gRPC controller server address (default: "localhost:8494")**Example:**```bash-# Fork from the latest state-ax fork --src-conversation 38460323-9a78-41cb-8991-022b0ff2c19c+# Fork from the latest state into a new conversation+ax fork --src-conversation 38460323-9a78-41cb-8991-022b0ff2c19c \+ --dest-conversation e5e26e38-53a2-4f22-b1cb-ae867357df83# Fork from a specific checkpoint-ax fork --src-conversation 38460323-9a78-41cb-8991-022b0ff2c19c --src-seq 12--# Fork from a specific checkpoint to a new event log with a specific new ID-ax fork --src-conversation 38460323-9a78-41cb-8991-022b0ff2c19c --src-seq 12 --dest-conversation e5e26e38-53a2-4f22-b1cb-ae867357df83+ax fork --src-conversation 38460323-9a78-41cb-8991-022b0ff2c19c \+ --dest-conversation e5e26e38-53a2-4f22-b1cb-ae867357df83 \+ --src-seq 12```### Tracediff --git a/cmd/ax/fork.go b/cmd/ax/fork.goindex faa2027..3db2368 100644--- a/cmd/ax/fork.go+++ b/cmd/ax/fork.go@@ -34,19 +34,19 @@ var (var forkCmd = &cobra.Command{Use: "fork",Short: "Fork an event log from a specific checkpoint",- Long: `Fork an existing agentic event log from a specific checkpoint.-If --dest-conversation is not provided, a new UUID will be generated.`,- RunE: runFork,+ Long: `Fork an existing agentic event log into a new conversation, optionally from a specific checkpoint.`,+ RunE: runFork,}func init() {forkCmd.Flags().StringVar(&forkSourceConversation, "src-conversation", "", "Source conversation ID to fork from (required)")forkCmd.Flags().Int32Var(&forkSourceSeq, "src-seq", 0, "Sequence number to fork from (optional, defaults to latest)")- forkCmd.Flags().StringVar(&forkDestConversation, "dest-conversation", "", "Destination conversation ID (optional, generates UUID if not provided)")+ forkCmd.Flags().StringVar(&forkDestConversation, "dest-conversation", "", "Destination conversation ID (required)")forkCmd.Flags().StringVar(&forkServerAddr, "server", "", "gRPC controller server address (if specified, connects to remote server; otherwise runs with a local built-in AX server)")forkCmd.Flags().StringVar(&forkConfigFile, "config", "ax.yaml", "Path to YAML configuration file (only used with a local built-in AX server)")forkCmd.MarkFlagRequired("src-conversation")+ forkCmd.MarkFlagRequired("dest-conversation")}func runFork(cmd *cobra.Command, args []string) error {diff --git a/internal/controller/controller.go b/internal/controller/controller.goindex 7def527..2219ba3 100644--- a/internal/controller/controller.go+++ b/internal/controller/controller.go@@ -302,11 +302,31 @@ func (d *Controller) Fork(ctx context.Context, srcConversationID string, srcSeqreturn "", fmt.Errorf("source conversation %s not found or has no events", srcConversationID)}- for _, ev := range events {- if srcSeq > 0 && ev.Seq > srcSeq {- break // Optimization: events are ordered by seq, so we can stop iterating+ // When the caller specifies srcSeq, require that it actually exists in+ // the source event log. Without this check a typo or stale checkpoint+ // silently degrades to "fork all events", which is misleading. Walk+ // the events once: stop as soon as we pass the requested seq, and+ // truncate the slice on an exact match so the copy loop below doesn't+ // need to re-check the bound.+ if srcSeq > 0 {+ found := false+ for i, ev := range events {+ if ev.Seq == srcSeq {+ events = events[:i+1]+ found = true+ break+ }+ if ev.Seq > srcSeq {+ break+ }}- // Clone the event to update the conversation ID+ if !found {+ return "", fmt.Errorf("src_seq %d not found in conversation %s", srcSeq, srcConversationID)+ }+ }++ for _, ev := range events {+ // Clone the event to update the conversation ID.newEvent := &proto.ConversationEvent{ConversationId: destConversationID,Seq: ev.Seq,diff --git a/internal/controller/fork_test.go b/internal/controller/fork_test.goindex 9b1b503..8547973 100644--- a/internal/controller/fork_test.go+++ b/internal/controller/fork_test.go@@ -104,3 +104,58 @@ func TestController_Fork(t *testing.T) {t.Errorf("expected seq 1, got %d", events2[0].Seq)}}++func TestController_Fork_SrcSeqNotFound(t *testing.T) {+ ctx := context.Background()+ srcCID := "src-conv"++ log := &executortest.MemoryEventLog{}+ log.AllEvents = []*proto.ConversationEvent{+ {+ ConversationId: srcCID,+ Seq: 1,+ Messages: []*proto.Message{+ {Role: "user", Content: &proto.Content{Type: &proto.Content_Text{Text: &proto.TextContent{Text: "msg 1"}}}},+ },+ State: proto.State_STATE_COMPLETED,+ },+ {+ ConversationId: srcCID,+ Seq: 2,+ Messages: []*proto.Message{+ {Role: "assistant", Content: &proto.Content{Type: &proto.Content_Text{Text: &proto.TextContent{Text: "msg 2"}}}},+ },+ State: proto.State_STATE_COMPLETED,+ },+ }++ c, err := New(ctx, Config{+ EventLogBuilder: func() (executor.EventLog, error) {+ return log, nil+ },+ PlannerBuilder: func(ctx context.Context, r *Registry) (agent.Agent, error) {+ return &dummyAgent{}, nil+ },+ })+ if err != nil {+ t.Fatal(err)+ }+ defer c.Close()++ // Non-existent src_seq must error and write nothing.+ if _, err := c.Fork(ctx, srcCID, 99, "dest-conv"); err == nil {+ t.Fatal("expected error when src_seq does not exist, got nil")+ }+ events, err := log.Events(ctx, "dest-conv")+ if err != nil {+ t.Fatal(err)+ }+ if len(events) != 0 {+ t.Fatalf("expected 0 events on failed fork, got %d", len(events))+ }++ // src_seq exactly at the max is valid and must succeed.+ if _, err := c.Fork(ctx, srcCID, 2, "dest-conv-boundary"); err != nil {+ t.Fatalf("unexpected error forking at boundary src_seq: %v", err)+ }+}diff --git a/internal/server/server.go b/internal/server/server.goindex 2a77464..a512e9a 100644--- a/internal/server/server.go+++ b/internal/server/server.go@@ -66,6 +66,11 @@ func (s *Server) ForkConversation(ctx context.Context, req *proto.ForkRequest) (if req.SrcConversationId == "" {return nil, status.Errorf(codes.InvalidArgument, "src_conversation_id is required")}+ // dest_conversation_id must be supplied by the caller: the substrate+ // router uses it to bring up the actor for the new conversation+ // before the request reaches this handler, so an empty value here+ // would mean no actor was provisioned.+ // TODO: consider relaxing this requirement.if req.DestConversationId == "" {return nil, status.Errorf(codes.InvalidArgument, "dest_conversation_id is required")}@@ -74,7 +79,7 @@ func (s *Server) ForkConversation(ctx context.Context, req *proto.ForkRequest) (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.+ go suspendActor(destID) // TODO(jbd): Move to an interceptor.return &proto.ForkResponse{ConversationId: destID}, nil}diff --git a/internal/server/server_test.go b/internal/server/server_test.goindex 74a7e3a..3e28252 100644--- a/internal/server/server_test.go+++ b/internal/server/server_test.go@@ -85,3 +85,46 @@ func TestServer_Fork(t *testing.T) {t.Fatalf("expected 1 event, got %d", len(events))}}++// TestServer_Fork_RequiresDestID verifies that ForkConversation rejects+// a request without DestConversationId. The substrate router relies on+// the dest ID to bring up the conversation's actor before the handler+// runs, so we can't accept empty IDs at this layer.+func TestServer_Fork_RequiresDestID(t *testing.T) {+ ctx := context.Background()+ srcCID := "src-conv"++ log := &executortest.MemoryEventLog{}+ log.AllEvents = []*proto.ConversationEvent{+ {+ ConversationId: srcCID,+ Seq: 1,+ Messages: []*proto.Message{+ {Role: "user", Content: &proto.Content{Type: &proto.Content_Text{Text: &proto.TextContent{Text: "msg 1"}}}},+ },+ State: proto.State_STATE_COMPLETED,+ },+ }++ c, err := controller.New(ctx, controller.Config{+ EventLogBuilder: func() (executor.EventLog, error) {+ return log, nil+ },+ PlannerBuilder: func(ctx context.Context, r *controller.Registry) (agent.Agent, error) {+ return &dummyAgent{}, nil+ },+ })+ if err != nil {+ t.Fatal(err)+ }+ defer c.Close()++ s := New(c)++ if _, err := s.ForkConversation(ctx, &proto.ForkRequest{+ SrcConversationId: srcCID,+ // DestConversationId intentionally left empty.+ }); err == nil {+ t.Fatal("expected InvalidArgument when DestConversationId is empty, got nil")+ }+}