agent_test.go

Overview

The agent_test.go file contains unit tests and supporting helper functions to validate the behavior of the Parallel Agent, a workflow agent that executes multiple sub-agents concurrently. This test suite verifies the correct orchestration of sub-agents, proper event streaming, error propagation, and cancellation handling within the parallel execution context.

This file exercises the Parallel Agent functionality as part of the broader Agent Workflow Management framework, specifically focusing on concurrent execution patterns implemented in the parallelagent package. It uses sub-agents created with the loopagent package, which repeatedly emits events, to simulate realistic agent workloads. The tests ensure that the parallel agent correctly merges events from multiple sub-agents and handles error and cancellation scenarios gracefully.

The tests run agents inside an in-memory session service and utilize the agent execution runner (Agent Execution Runner) to simulate the actual runtime environment of agents.


Detailed Explanations

TestNewParallelAgent

func TestNewParallelAgent(t *testing.T)
t.Run("subagents complete run", func(t *testing.T) {
    // setup parallel agent with 3 sub-agents and max 2 iterations
    // run agent and collect events
    // compare received events to expected events
})

newParallelAgent

func newParallelAgent(t *testing.T, maxIterations uint, numSubAgents int, agentErr error) agent.Agent
parallelAgent := newParallelAgent(t, 2, 3, nil)
// parallelAgent runs 3 sub-agents emitting 2 events each

must

func must[T agent.Agent](a T, err error) T

customRun

func customRun(id int, agentErr error) func(agent.InvocationContext) iter.Seq2[*session.Event, error]
runFn := customRun(1, nil) // yields one event with "hello 1"
runFnWithError := customRun(-1, fmt.Errorf("error")) // yields error immediately

Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram: Structure and Workflow of the Test Setup and Parallel Agent Execution

flowchart TD
TestNewParallelAgent[TestNewParallelAgent]
newParallelAgent[newParallelAgent]
parallelAgent[ParallelAgent Instance]
SubAgentLoop1[LoopAgent Sub-Agent 1]
SubAgentLoop2[LoopAgent Sub-Agent 2]
...
SubAgentLoopN[LoopAgent Sub-Agent N]
customRunFn[customRun Function]
runner[Agent Runner]
sessionService[InMemory Session Service]
events[Collected Events]
ctx[Context & Cancellation]
TestNewParallelAgent -->|calls| newParallelAgent
newParallelAgent --> parallelAgent
parallelAgent --> SubAgentLoop1
parallelAgent --> SubAgentLoop2
parallelAgent --> SubAgentLoopN
SubAgentLoop1 --> customRunFn
SubAgentLoop2 --> customRunFn
SubAgentLoopN --> customRunFn
TestNewParallelAgent --> runner
runner --> parallelAgent
runner --> sessionService
runner --> ctx
parallelAgent --> events
runner --> events
TestNewParallelAgent --> events

References to Related Topics


This documentation provides a comprehensive technical overview of the agent_test.go file, covering its role in verifying the parallel agent's execution correctness, the structure of helper functions used, and its integration within the agent workflow and execution framework.