runner_test.go

Overview

This file contains unit tests for key functionalities of the runner package, which is responsible for managing the execution of AI agents within user sessions. The tests primarily focus on verifying the correct behavior of agent selection, transfer permission logic, and the handling of input blobs as artifacts during agent execution.

These tests ensure that:

This file plays a vital role in maintaining the integrity of the Agent Execution Runner and its associated features, including Agent Selection Logic and Input Blob Artifact Saving.


Detailed Explanation of Tests and Functions

TestRunner_findAgentToRun

Purpose:
Verifies that the runner's findAgentToRun method selects the correct agent to process a user message based on the session event history and agent transfer permissions.

Test Setup:

Test Cases:

Case Name

Session Events

Expected Agent Selected

last event from agent allowing transfer

Last agent event from agent allowing transfer, then user event

Allows transfer agent

last event from agent not allowing transfer

Last agent event from agent disallowing transfer, then user event

Root agent

no events from agents, call root

Only user events, no agent events

Root agent

Key Assertions:


Test_findAgent

Purpose:
Tests the recursive findAgent function that searches for an agent by name within an agent tree.

Test Setup:

Test Cases:

Case Name

Root Agent

Target Agent Name

Expected Result

ok

Multi-node tree

Name of allowsTransferAgent

AllowsTransferAgent

finds in one node tree

Single agent

Single agent's own name

Single agent

doesn't fail if missing

Multi-node tree

"random" (non-existent)

nil

doesn't fail on empty tree

nil

"random"

nil

Key Assertions:


Test_isTransferrableAcrossAgentTree

Purpose:
Validates the runner's logic to check if an agent and its parents allow transfer of control within the agent tree.

Test Setup:
Tests different agent configurations:

Test Cases:

Case Name

Agent Configuration

Expected Result (Transfer Allowed)

disallow for agent with flag

DisallowTransferToParent = true

false

disallow for non-LLM agent

Non-LLM agent type

false

allow for default LLM agent

LLM agent without restrictions

true

Key Assertions:


TestRunner_SaveInputBlobsAsArtifacts

Purpose:
Tests the runner's ability to detect inline blobs in user messages, save them as artifacts, and replace inline data with a placeholder text in the session event.

Test Setup:

Test Workflow:

Key Assertions:


Helper Functions

agentTree(t *testing.T) agentTreeStruct

Creates a test agent tree with a root agent and two sub-agents:

Returns references to these agents for use in tests.

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

Utility function that panics on error and returns the agent otherwise. Used to simplify agent creation in tests.

createSession

Creates a session with the given events appended. Uses the in-memory session service and appends the specified events to the session for test setup.

Parameters:

Returns the created session.Session.


Important Implementation Details


Interaction With Other System Components


Visual Diagram: Structure of runner_test.go

flowchart TD
RunnerTest[TestRunner_findAgentToRun]
FindAgentTest[Test_findAgent]
TransferTest[Test_isTransferrableAcrossAgentTree]
SaveBlobTest[TestRunner_SaveInputBlobsAsArtifacts]
agentTreeFunc[agentTree Helper]
mustFunc[must Helper]
createSessionFunc[createSession Helper]
RunnerTest --> agentTreeFunc
FindAgentTest --> agentTreeFunc
TransferTest --> mustFunc
SaveBlobTest --> mustFunc
SaveBlobTest --> createSessionFunc
RunnerTest --> FindAgentTest
RunnerTest --> TransferTest
RunnerTest --> SaveBlobTest

This diagram shows the main test functions and their relationships to helper utilities within the file.


Usage Examples from Tests

Example: Selecting Agent Based on Last Transferable Agent Event

r := &Runner{
    rootAgent: rootAgent,
}
agent, err := r.findAgentToRun(session)
if err != nil {
    // handle error
}
fmt.Println("Agent selected:", agent.Name())

Example: Saving Input Blob as Artifact During Run

cfg := agent.RunConfig{
    SaveInputBlobsAsArtifacts: true,
}
for _, err := range runner.Run(ctx, userID, sessionID, userMessage, cfg) {
    if err != nil {
        // handle error
    }
}

This documentation outlines the test coverage and verification strategies for critical components of the runner package, focusing on agent selection and input blob artifact saving logic. It references the Agent Execution Runner, Agent Selection Logic, and Input Blob Artifact Saving topics for deeper understanding of the underlying mechanisms.