Agent Executor (AX) is Google's open agentic orchestration runtime
Go
Resume agent loop from the agent that waits for confirmation (#68)
Jaana Doganfa02bbca8d95
diff --git a/internal/controller/loop.go b/internal/controller/loop.goindex 1a2b1a4..535167a 100644--- a/internal/controller/loop.go+++ b/internal/controller/loop.go@@ -83,20 +83,22 @@ func (e *LoopExecutor) runLoop(ctx context.Context, session *Session, incoming *}// Ensure the last confirmation question was answered.- exitLoop, err := e.handleConfirmation(ctx, session, incoming, handler)+ exit, nextAgentID, err := e.handleConfirmation(ctx, session, incoming, handler)if err != nil {return err}- if exitLoop {+ if exit {return nil}+ if nextAgentID == "" {+ nextAgentID = plannerAgentID+ }// Write the new inputs to the event log.if err := session.WriteContent(ctx, "", incoming.CheckpointId, incoming.Contents); err != nil {return fmt.Errorf("failed to write input content: %w", err)}- var nextAgentID = plannerAgentID // Start from planner.for steps < e.maxSteps {// Check context cancellationselect {@@ -187,40 +189,40 @@ func (e *LoopExecutor) runAgent(ctx context.Context, session *Session, agentID s// handleConfirmation checks if the session is waiting for a confirmation response,// and if so, verifies that the incoming request provides a matching response.-// It returns a boolean indicating whether the loop should stop early, and an error.-func (e *LoopExecutor) handleConfirmation(ctx context.Context, session *Session, incoming *proto.ProcessRequest, handler agent.OutputHandler) (exitLoop bool, err error) {+// It returns a boolean indicating whether the loop should stop early, the agent to resume from, and an error.+func (e *LoopExecutor) handleConfirmation(ctx context.Context, session *Session, incoming *proto.ProcessRequest, handler agent.OutputHandler) (exitLoop bool, resumedAgentID string, err error) {history := session.History()if len(history) == 0 {- return false, nil+ return false, "", nil}lastMsg := history[len(history)-1]confReq := lastMsg.GetConfirmation()if confReq == nil {// not a confirmation question or answer- return false, nil+ return false, "", nil}if lastMsg.Role != "assistant" || confReq.Id == "" || confReq.Question == "" {// not a confirmation question- return false, nil+ return false, "", nil}// Require the first incoming content to be the confirmation responseif len(incoming.Contents) == 0 {- return true, handler(&proto.ProcessResponse{+ return true, "", handler(&proto.ProcessResponse{Contents: []*proto.Content{lastMsg},})}confResp := incoming.Contents[0].GetConfirmation()if confResp == nil || (confResp.GetApproval() == nil && confResp.GetDecline() == nil) {- return true, handler(&proto.ProcessResponse{+ return true, "", handler(&proto.ProcessResponse{Contents: []*proto.Content{lastMsg},})}if confResp.Id != confReq.Id {- return true, handler(&proto.ProcessResponse{+ return true, "", handler(&proto.ProcessResponse{Contents: []*proto.Content{lastMsg},})}@@ -238,13 +240,9 @@ func (e *LoopExecutor) handleConfirmation(ctx context.Context, session *Session,}}if err := session.WriteContent(ctx, plannerAgentID, "", out); err != nil {- return true, fmt.Errorf("failed to write content: %w", err)+ return true, "", fmt.Errorf("failed to write content: %w", err)}- return true, handler(&proto.ProcessResponse{Contents: out})+ return true, "", handler(&proto.ProcessResponse{Contents: out})}- // TODO(jbd): We can consider directly supporting- // handing off to the agent that asked the question.- // Planner should be able to handoff to the correct agent- // if it's good enough.- return false, nil+ return false, session.ConfirmationWaitingAgent(confResp.Id), nil}diff --git a/internal/controller/session.go b/internal/controller/session.goindex 07019ea..0d84fb7 100644--- a/internal/controller/session.go+++ b/internal/controller/session.go@@ -30,12 +30,17 @@ import (type Session struct {id string- mu sync.RWMutex- eventLog eventlog.EventLog- state proto.State- messageHistory []*proto.Content- waitingAgents map[string][]*proto.Content // by agent ID- checkpointIDs map[string]struct{} // checkpoint UUIDs+ mu sync.RWMutex+ eventLog eventlog.EventLog+ state proto.State+ steps []*Step+ waitingAgents map[string][]*proto.Content // by agent ID+ checkpointIDs map[string]struct{} // checkpoint UUIDs+}++type Step struct {+ AgentID string+ Contents []*proto.Content}// SessionManager manages multiple sessions.@@ -73,12 +78,12 @@ func (sm *SessionManager) NewSession(sessionID string) (*Session, error) {}session := &Session{- id: sessionID,- state: proto.State_STATE_UNSPECIFIED,- messageHistory: []*proto.Content{},- waitingAgents: make(map[string][]*proto.Content),- checkpointIDs: make(map[string]struct{}),- eventLog: el,+ id: sessionID,+ state: proto.State_STATE_UNSPECIFIED,+ steps: []*Step{},+ waitingAgents: make(map[string][]*proto.Content),+ checkpointIDs: make(map[string]struct{}),+ eventLog: el,}sm.sessions[sessionID] = session@@ -107,12 +112,12 @@ func (sm *SessionManager) LoadSessionFromCheckpoint(ctx context.Context, session// 2. Construct session state in memory - No Locksession := &Session{- id: sessionID,- state: state,- eventLog: el,- waitingAgents: make(map[string][]*proto.Content),- messageHistory: []*proto.Content{},- checkpointIDs: make(map[string]struct{}),+ id: sessionID,+ state: state,+ eventLog: el,+ waitingAgents: make(map[string][]*proto.Content),+ steps: []*Step{},+ checkpointIDs: make(map[string]struct{}),}err = session.reconstructState(events)@@ -199,12 +204,12 @@ func (sm *SessionManager) ForkSession(ctx context.Context, sourceSessionID, sour// 4. Construct base sessionsession := &Session{- id: newSessionID,- state: proto.State_STATE_UNSPECIFIED,- messageHistory: []*proto.Content{},- waitingAgents: make(map[string][]*proto.Content),- checkpointIDs: make(map[string]struct{}),- eventLog: newEL,+ id: newSessionID,+ state: proto.State_STATE_UNSPECIFIED,+ steps: []*Step{},+ waitingAgents: make(map[string][]*proto.Content),+ checkpointIDs: make(map[string]struct{}),+ eventLog: newEL,}// Reconstruct session from new events@@ -239,16 +244,14 @@ func (s *Session) reconstructState(events []*proto.Event) error {s.waitingAgents[event.Sender] = append(s.waitingAgents[event.Sender], event.Contents...)} else {- // If buffer already exists, append it to the history first.- // Then merge the new contents to the message history.- // Once we don't wait for new contents from an agent,- // we don't care about the origin of the contents anymore.- if len(s.waitingAgents[event.Sender]) > 0 {- s.messageHistory = append(- s.messageHistory, s.waitingAgents[event.Sender]...)+ // Merge buffered and new contents into a single step.+ contents := append(s.waitingAgents[event.Sender], event.Contents...)+ if len(contents) > 0 {+ s.steps = append(s.steps, &Step{+ AgentID: event.Sender,+ Contents: contents,+ })}- s.messageHistory = append(- s.messageHistory, event.Contents...)// Cleanup the waiting buffer, it's now a part of the overall history.delete(s.waitingAgents, event.Sender)@@ -258,7 +261,12 @@ func (s *Session) reconstructState(events []*proto.Event) error {case *proto.Event_HandoffEvent:// TODO: Visit this section when resuming the waiting agents.case *proto.Event_ContentEvent:- s.messageHistory = append(s.messageHistory, x.ContentEvent.Contents...)+ if len(x.ContentEvent.Contents) > 0 {+ s.steps = append(s.steps, &Step{+ AgentID: e.SenderId,+ Contents: x.ContentEvent.Contents,+ })+ }default:return fmt.Errorf("unknown event kind: %v", e.Kind)}@@ -360,7 +368,12 @@ func (s *Session) WriteContent(ctx context.Context, sender string, checkpointIDreturn err}- s.messageHistory = append(s.messageHistory, contents...)+ if len(contents) > 0 {+ s.steps = append(s.steps, &Step{+ AgentID: sender,+ Contents: contents,+ })+ }if checkpointID != "" {s.checkpointIDs[checkpointID] = struct{}{}}@@ -422,5 +435,25 @@ func (s *Session) History() []*proto.Content {s.mu.RLock()defer s.mu.RUnlock()- return s.messageHistory+ var history []*proto.Content+ for _, step := range s.steps {+ history = append(history, step.Contents...)+ }+ return history+}++func (s *Session) ConfirmationWaitingAgent(confirmationID string) string {+ s.mu.RLock()+ defer s.mu.RUnlock()++ for i := len(s.steps) - 1; i >= 0; i-- {+ step := s.steps[i]+ for _, content := range step.Contents {+ conf := content.GetConfirmation()+ if conf != nil && conf.Id == confirmationID && conf.Question != "" {+ return step.AgentID+ }+ }+ }+ return ""}