Agent Executor (AX) is Google's open agentic orchestration runtime
Go
[AX Trace] Optimize trace building to O(N) complexity (#243)
anj-s3666fa7eca10
diff --git a/cmd/ax/trace.go b/cmd/ax/trace.goindex d067e2d..a0cd4e3 100644--- a/cmd/ax/trace.go+++ b/cmd/ax/trace.go@@ -22,7 +22,6 @@ import ("net/http"osConfig "os/exec""runtime"- "sort""time""github.com/google/ax/internal/config"@@ -183,35 +182,23 @@ func buildExecTraces(rootExecID string, execIDs []string, events []*proto.Execut}var execs []ExecTrace- for execID, evs := range execsMap {- agentID := ""- for _, ev := range evs {- if ev.AgentID != "" {- agentID = ev.AgentID- break+ for _, execID := range execIDs {+ if evs, ok := execsMap[execID]; ok {+ agentID := ""+ for _, ev := range evs {+ if ev.AgentID != "" {+ agentID = ev.AgentID+ break+ }}+ execs = append(execs, ExecTrace{+ ExecID: execID,+ AgentID: agentID,+ Events: evs,+ })}- execs = append(execs, ExecTrace{- ExecID: execID,- AgentID: agentID,- Events: evs,- })}- // Sort execs by the order they appeared in execIDs (chronological)- getIndex := func(execID string) int {- for idx, id := range execIDs {- if id == execID {- return idx- }- }- return len(execIDs)- }-- sort.Slice(execs, func(i, j int) bool {- return getIndex(execs[i].ExecID) < getIndex(execs[j].ExecID)- })-return execs}diff --git a/cmd/ax/trace_test.go b/cmd/ax/trace_test.gonew file mode 100644index 0000000..e9f3fc2--- /dev/null+++ b/cmd/ax/trace_test.go@@ -0,0 +1,50 @@+// 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.++package main++import (+ "testing"++ "github.com/google/ax/proto"+)++func TestBuildExecTraces_PreservesOrderOfExecIDs(t *testing.T) {+ rootExecID := "root-exec"+ execIDs := []string{"root-exec", "child-exec-1", "child-exec-2"}++ // Events arrive in arbitrary order+ events := []*proto.ExecutionEvent{+ {ExecId: "child-exec-2"},+ {ExecId: "root-exec"},+ {ExecId: "child-exec-1"},+ }++ execs := buildExecTraces(rootExecID, execIDs, events)++ if len(execs) != 3 {+ t.Fatalf("expected 3 execution traces, got %d", len(execs))+ }++ // Verify order matches execIDs+ if execs[0].ExecID != "root-exec" {+ t.Errorf("expected execs[0] to be 'root-exec', got %s", execs[0].ExecID)+ }+ if execs[1].ExecID != "child-exec-1" {+ t.Errorf("expected execs[1] to be 'child-exec-1', got %s", execs[1].ExecID)+ }+ if execs[2].ExecID != "child-exec-2" {+ t.Errorf("expected execs[2] to be 'child-exec-2', got %s", execs[2].ExecID)+ }+}