agent_test.go

Overview

The agent_test.go file contains comprehensive unit tests and supporting mock implementations designed to validate the behavior of the Loop Agent implemented in the loopagent package. Its primary purpose is to verify the correct execution semantics of the Loop Agent when orchestrating multiple sub-agents over repeated iterations, including edge cases like infinite loops, early termination via escalation, and usage of function-call tools.

This test file exercises the looping logic by simulating various scenarios with different configurations of sub-agents, iteration limits, and function call behaviors, ensuring that the Loop Agent properly yields expected session events and respects termination signals. It also provides mock implementations of custom agents and LLM agents with function call tools to facilitate controlled testing.

The tests interact with other core components such as the session package (for event management), the runner package (to execute agents within sessions), and the agent interfaces, thus validating integration points within the broader Agent Workflow Management and Loop Agent contexts.


Detailed Explanations

Test Functions

TestNewLoopAgent

loopAgent, err := loopagent.New(loopagent.Config{
	MaxIterations: 1,
	AgentConfig: agent.Config{
		Name:      "test_agent",
		SubAgents: []agent.Agent{newCustomAgent(t, 0)},
	},
})
// Run the agent and collect events...

Helper Functions and Types

newCustomAgent(t *testing.T, id int) agent.Agent


customAgent struct and Run method

func (a *customAgent) Run(ctx agent.InvocationContext) iter.Seq2[*session.Event, error] {
	return func(yield func(*session.Event, error) bool) {
		a.callCounter++
		yield(&session.Event{
			LLMResponse: model.LLMResponse{
				Content: genai.NewContentFromText(fmt.Sprintf("hello %v", a.id), genai.RoleModel),
			},
		}, nil)
	}
}

Function Tools for Escalation Testing

These are used to create function tools that the LLM agent can call to trigger escalation events during tests.


newLmmAgentWithFunctionCall(t *testing.T, id int, skipSummarization bool) agent.Agent


FakeLLM struct and GenerateContent method


Important Implementation Details


Interaction with Other System Components


Visual Diagram: Structure and Workflow in agent_test.go

flowchart TD
TestNewLoopAgent[TestNewLoopAgent]
newCustomAgent[newCustomAgent]
customAgent_Run[customAgent.Run]
newLmmAgentWithFunctionCall[newLmmAgentWithFunctionCall]
FakeLLM_GenerateContent[FakeLLM.GenerateContent]
exampleFunctionThatEscalates[exampleFunctionThatEscalates]
exampleFunctionThatEscalatesAndSkips[exampleFunctionThatEscalatesAndSkips]
TestNewLoopAgent --> newCustomAgent
newCustomAgent --> customAgent_Run
TestNewLoopAgent --> newLmmAgentWithFunctionCall
newLmmAgentWithFunctionCall --> FakeLLM_GenerateContent
newLmmAgentWithFunctionCall --> exampleFunctionThatEscalates
newLmmAgentWithFunctionCall --> exampleFunctionThatEscalatesAndSkips

References to Relevant Topics


This documentation provides a detailed technical overview of the testing code validating the looping workflow agent capabilities, the mock agents used for testing, and their interplay with core system components.