agenttest

package
v0.3.49 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package agenttest provides shared helpers for AgentKit tests.

Tests should still assemble plugins through pluginkit/build; this package only removes duplicated wiring for scripted LLM runs, session assertions, and tool calls. See docs/guides/testing.zh.md for the full pyramid and CI tiers.

Index

Constants

View Source
const DefaultResearcherDef = `` /* 130-byte string literal not displayed */

Variables

This section is empty.

Functions

func AskAllToolsPolicy added in v0.1.10

func AskAllToolsPolicy(reason string) agentkit.Policy

AskAllToolsPolicy returns a policy that requires human approval for every tool call.

func AssertDeriveMessagesReplayPairing added in v0.3.33

func AssertDeriveMessagesReplayPairing(t *testing.T, sess agentkit.Session, ctx context.Context)

AssertDeriveMessagesReplayPairing checks derive replay invariants for LLM providers (answered tool calls, no leading orphan tool messages).

func AssertDeriveMessagesToolCallsAnswered

func AssertDeriveMessagesToolCallsAnswered(t *testing.T, sess agentkit.Session, ctx context.Context)

AssertDeriveMessagesToolCallsAnswered checks provider replay invariants.

func AssertDeriveMessagesToolCallsAnsweredMessages added in v0.3.33

func AssertDeriveMessagesToolCallsAnsweredMessages(t *testing.T, messages []agentkit.ModelMessage)

func AssertEventAtLeast

func AssertEventAtLeast(t *testing.T, events []agentkit.SessionEvent, typ agentkit.EventType, min int)

AssertEventAtLeast fails when an event type appears fewer than min times.

func AssertModelMessagesReplayPairing added in v0.3.33

func AssertModelMessagesReplayPairing(t *testing.T, messages []agentkit.ModelMessage)

AssertModelMessagesReplayPairing applies replay invariants to an already-derived slice.

func AssertNoToolResultWithContent

func AssertNoToolResultWithContent(t *testing.T, events []agentkit.SessionEvent, callID agentkit.ToolCallID, substr string)

AssertNoToolResultWithContent fails if any tool result for callID contains substr.

func AssertSubagentParentSession

func AssertSubagentParentSession(t *testing.T, events []agentkit.SessionEvent)

AssertSubagentParentSession verifies post-delegate parent session invariants.

func AssertToolResultContains

func AssertToolResultContains(t *testing.T, events []agentkit.SessionEvent, callID agentkit.ToolCallID, substr string)

AssertToolResultContains finds a tool result by call id and checks content.

func Build

func Build[T any](t *testing.T, graph map[string]any, rootID string) T

Build constructs a plugin instance from a pluginkit graph fragment.

func CallTool

func CallTool(t *testing.T, ctx context.Context, tool agentkit.Tool, input string) string

CallTool executes a tool with JSON input and returns raw JSON output.

func ContentText

func ContentText(msg agentkit.ModelMessage) string

ContentText joins text parts from a model message.

func CountEvents

func CountEvents(events []agentkit.SessionEvent, typ agentkit.EventType) int

CountEvents counts session events of a given type.

func DefaultAssembler

func DefaultAssembler(t *testing.T) agentkit.PromptAssembler

DefaultAssembler builds the standard prompt assembler used in smoke tests.

func DefaultSubagentSmokeSteps

func DefaultSubagentSmokeSteps() []llm.ScriptedStep

DefaultSubagentSmokeSteps matches presets/subagent-smoke.yaml scripted order.

func DenyAllToolsPolicy

func DenyAllToolsPolicy(reason string) agentkit.Policy

DenyAllToolsPolicy returns a policy that blocks all tool execution.

func EmptyToolsRuntime

func EmptyToolsRuntime(t *testing.T) agentkit.ToolRuntime

EmptyToolsRuntime returns a tool runtime with no tools and allow-all approval.

func FindChildSessionID

func FindChildSessionID(t *testing.T, events []agentkit.SessionEvent) agentkit.SessionID

FindChildSessionID reads the child session id from subagent/start.

func LoopRequest added in v0.2.1

func LoopRequest(conversation agentkit.SessionID, event agentkit.MessageEvent) agentkit.LoopRequest

LoopRequest builds a LoopRequest with conversation on the event envelope.

func MustScripted

func MustScripted(t *testing.T, steps ...llm.ScriptedStep) agentkit.LLMProvider

MustScripted builds an llm/scripted provider or fails the test.

func NewScriptedAgent

func NewScriptedAgent(t *testing.T, cfg ScriptedAgentConfig) (agentkit.Agent, agentkit.SessionStore)

NewScriptedAgent builds an agent on a temp store unless Store is set.

func RepoRoot

func RepoRoot(t *testing.T) string

RepoRoot finds the module root containing go.mod.

func RunTurn

func RunTurn(t *testing.T, ctx context.Context, ag agentkit.Agent, text string)

RunTurn is a thin helper around agent.RunTurn with a user text message.

func SeedCrashedToolCall

func SeedCrashedToolCall(t *testing.T, store agentkit.SessionStore, sessionID agentkit.SessionID, agentID agentkit.AgentID, call agentkit.ToolCall, userText string) agentkit.Session

SeedCrashedToolCall writes an incomplete turn ending on a pending tool call.

func SessionEvents

SessionEvents reads all events from a session id via the store.

func SubagentStart

SubagentStart decodes a subagent/start event payload.

func TempFileStore

func TempFileStore(t *testing.T) (agentkit.SessionStore, string)

TempFileStore creates a session/store rooted at t.TempDir().

func TestWorkspace added in v0.1.12

func TestWorkspace(t *testing.T, root ...string) workspace.Service

TestWorkspace returns a temp-dir workspace for agent tests.

func ToolResult

func ToolResult(t *testing.T, ev agentkit.SessionEvent) agentkit.ToolResult

ToolResult decodes a tool/result event payload.

func ToolsRuntime

func ToolsRuntime(t *testing.T, deps tools.RuntimeDeps) agentkit.ToolRuntime

ToolsRuntime builds a tool runtime with optional policies and approval.

func TurnContext

func TurnContext(sessionID agentkit.SessionID, agentID agentkit.AgentID) context.Context

TurnContext returns a context seeded for agent.RunTurn.

Types

type AllowAll

type AllowAll struct{}

AllowAll approves every tool execution.

type DenyAll

type DenyAll struct{}

DenyAll rejects every tool execution.

type DirWorkspace

type DirWorkspace map[string]string

DirWorkspace maps scoped rel paths (e.g. local:agents) to directories.

func (DirWorkspace) Resolve

func (w DirWorkspace) Resolve(_ context.Context, rel string) (string, error)

type GateSandbox added in v0.3.48

type GateSandbox struct {
	ReadErr  error
	WriteErr error
}

GateSandbox returns fixed CheckRead/CheckWrite errors (nil allows).

func (GateSandbox) CheckRead added in v0.3.48

func (g GateSandbox) CheckRead(context.Context, string) error

func (GateSandbox) CheckWrite added in v0.3.48

func (g GateSandbox) CheckWrite(context.Context, string) error

func (GateSandbox) Enabled added in v0.3.48

func (g GateSandbox) Enabled() bool

func (GateSandbox) WrapArgv added in v0.3.48

func (g GateSandbox) WrapArgv(_ context.Context, _ string, inner []string) ([]string, error)

type RecordingFS added in v0.3.48

type RecordingFS struct {
	ReadCalls   int
	StatCalls   int
	ListCalls   int
	WriteCalls  int
	AppendCalls int
	GrepCalls   int
	FindCalls   int
}

RecordingFS implements cap/filesystem.Service and counts inner method calls.

func (*RecordingFS) Append added in v0.3.48

func (r *RecordingFS) Append(context.Context, string, []byte) error

func (*RecordingFS) Find added in v0.3.48

func (*RecordingFS) Grep added in v0.3.48

func (*RecordingFS) List added in v0.3.48

func (*RecordingFS) Read added in v0.3.48

func (r *RecordingFS) Read(context.Context, string) ([]byte, error)

func (*RecordingFS) Stat added in v0.3.48

func (*RecordingFS) Write added in v0.3.48

type RecordingSandbox added in v0.3.48

type RecordingSandbox struct {
	WrapErr     error
	WrappedArgv []string
	Calls       []SandboxWrapCall

	ReadErr  error
	WriteErr error
	// contains filtered or unexported fields
}

RecordingSandbox implements capsandbox.Service and records WrapArgv calls.

func (*RecordingSandbox) CheckRead added in v0.3.48

func (r *RecordingSandbox) CheckRead(context.Context, string) error

func (*RecordingSandbox) CheckWrite added in v0.3.48

func (r *RecordingSandbox) CheckWrite(context.Context, string) error

func (*RecordingSandbox) Enabled added in v0.3.48

func (r *RecordingSandbox) Enabled() bool

func (*RecordingSandbox) LastWrapCall added in v0.3.48

func (r *RecordingSandbox) LastWrapCall() (SandboxWrapCall, bool)

LastWrapCall returns the most recent WrapArgv call, if any.

func (*RecordingSandbox) WrapArgv added in v0.3.48

func (r *RecordingSandbox) WrapArgv(_ context.Context, cwd string, inner []string) ([]string, error)

type SandboxWrapCall added in v0.3.48

type SandboxWrapCall struct {
	Cwd   string
	Inner []string
}

SandboxWrapCall records one WrapArgv invocation.

type ScopedWorkspace added in v0.3.48

type ScopedWorkspace struct {
	Local  string
	Global string
}

ScopedWorkspace resolves local: and global: refs the way sandbox/bwrap tests expect (tenant root under Local, shared state under Global).

func SetupScopedTenantDirs added in v0.3.48

func SetupScopedTenantDirs(t *testing.T) (ws ScopedWorkspace, local, global string)

SetupScopedTenantDirs creates .../tenants/tenant-a and .../global under a realpath-normalized temp dir and writes secrets.enc.json in global.

func (ScopedWorkspace) Resolve added in v0.3.48

func (w ScopedWorkspace) Resolve(_ context.Context, rel string) (string, error)

type ScriptedAgentConfig

type ScriptedAgentConfig struct {
	AgentID agentkit.AgentID
	Steps   []llm.ScriptedStep
	Tools   agentkit.ToolRuntime
	Store   agentkit.SessionStore
	// Workspace overrides the default; use with Store from TempFileStore so spill files land beside sessions.
	Workspace workspace.Service
}

ScriptedAgentConfig wires a minimal agent for smoke scenarios.

type StubEnvPairResolver added in v0.3.48

type StubEnvPairResolver struct {
	Pairs map[string][]string
	Err   error
}

StubEnvPairResolver implements credentials.Store and EnvPairResolver for shell / shell-bwrap env injection tests.

func (StubEnvPairResolver) EnvPairs added in v0.3.48

func (StubEnvPairResolver) Resolve added in v0.3.48

type SubagentDelegateConfig

type SubagentDelegateConfig struct {
	LogicalID     agentkit.SessionID
	ResearcherDef string
	Steps         []llm.ScriptedStep
	ParentAgentID agentkit.AgentID
	// ChildToolPacks are mounted on the child runtime (alongside finish) for allowlist smokes.
	ChildToolPacks []agentkit.ToolPack
	// ChildTools are extra tools on the child runtime (e.g. slow/blocking tools for timeout smokes).
	ChildTools []agentkit.Tool
	// SpawnerTimeoutSeconds bounds one in-process child delegation (wall clock).
	SpawnerTimeoutSeconds int
	// NewFinishTool builds the child agent's finish tool from the session store,
	// e.g. an adapter of finish.NewFinish. Required: agenttest stays plugin-agnostic.
	NewFinishTool func(store agentkit.SessionStore) (agentkit.Tool, error)
	// NewDelegateTool builds the parent agent's delegate tool bound to the spawner,
	// e.g. an adapter of the tool/subagent plugin constructor. Required.
	NewDelegateTool func(spawner capsubagent.Spawner) (agentkit.Tool, error)
}

SubagentDelegateConfig controls subagent smoke wiring.

type SubagentDelegateEnv

type SubagentDelegateEnv struct {
	Store     agentkit.SessionStore
	Agent     agentkit.Agent
	ParentID  agentkit.SessionID
	LogicalID agentkit.SessionID
	AgentsDir string
}

SubagentDelegateEnv wires a parent agent with a delegate tool and in-process subagent.

func NewSubagentDelegateEnv

func NewSubagentDelegateEnv(t *testing.T, cfg SubagentDelegateConfig) SubagentDelegateEnv

NewSubagentDelegateEnv builds a scripted parent→child delegate stack on a temp store.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL