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
- func AskAllToolsPolicy(reason string) agentkit.Policy
- func AssertDeriveMessagesReplayPairing(t *testing.T, sess agentkit.Session, ctx context.Context)
- func AssertDeriveMessagesToolCallsAnswered(t *testing.T, sess agentkit.Session, ctx context.Context)
- func AssertDeriveMessagesToolCallsAnsweredMessages(t *testing.T, messages []agentkit.ModelMessage)
- func AssertEventAtLeast(t *testing.T, events []agentkit.SessionEvent, typ agentkit.EventType, min int)
- func AssertModelMessagesReplayPairing(t *testing.T, messages []agentkit.ModelMessage)
- func AssertNoToolResultWithContent(t *testing.T, events []agentkit.SessionEvent, callID agentkit.ToolCallID, ...)
- func AssertSubagentParentSession(t *testing.T, events []agentkit.SessionEvent)
- func AssertToolResultContains(t *testing.T, events []agentkit.SessionEvent, callID agentkit.ToolCallID, ...)
- func Build[T any](t *testing.T, graph map[string]any, rootID string) T
- func CallTool(t *testing.T, ctx context.Context, tool agentkit.Tool, input string) string
- func ContentText(msg agentkit.ModelMessage) string
- func CountEvents(events []agentkit.SessionEvent, typ agentkit.EventType) int
- func DefaultAssembler(t *testing.T) agentkit.PromptAssembler
- func DefaultSubagentSmokeSteps() []llm.ScriptedStep
- func DenyAllToolsPolicy(reason string) agentkit.Policy
- func EmptyToolsRuntime(t *testing.T) agentkit.ToolRuntime
- func FindChildSessionID(t *testing.T, events []agentkit.SessionEvent) agentkit.SessionID
- func LoopRequest(conversation agentkit.SessionID, event agentkit.MessageEvent) agentkit.LoopRequest
- func MustScripted(t *testing.T, steps ...llm.ScriptedStep) agentkit.LLMProvider
- func NewScriptedAgent(t *testing.T, cfg ScriptedAgentConfig) (agentkit.Agent, agentkit.SessionStore)
- func RepoRoot(t *testing.T) string
- func RunTurn(t *testing.T, ctx context.Context, ag agentkit.Agent, text string)
- func SeedCrashedToolCall(t *testing.T, store agentkit.SessionStore, sessionID agentkit.SessionID, ...) agentkit.Session
- func SessionEvents(t *testing.T, ctx context.Context, store agentkit.SessionStore, ...) []agentkit.SessionEvent
- func SubagentStart(t *testing.T, ev agentkit.SessionEvent) sessevents.SubagentStartData
- func TempFileStore(t *testing.T) (agentkit.SessionStore, string)
- func TestWorkspace(t *testing.T, root ...string) workspace.Service
- func ToolResult(t *testing.T, ev agentkit.SessionEvent) agentkit.ToolResult
- func ToolsRuntime(t *testing.T, deps tools.RuntimeDeps) agentkit.ToolRuntime
- func TurnContext(sessionID agentkit.SessionID, agentID agentkit.AgentID) context.Context
- type AllowAll
- type DenyAll
- type DirWorkspace
- type GateSandbox
- type RecordingFS
- func (r *RecordingFS) Append(context.Context, string, []byte) error
- func (r *RecordingFS) Find(context.Context, capfs.FindRequest) (capfs.FindResult, error)
- func (r *RecordingFS) Grep(context.Context, capfs.GrepRequest) (capfs.GrepResult, error)
- func (r *RecordingFS) List(context.Context, string) ([]capfs.DirEntry, error)
- func (r *RecordingFS) Read(context.Context, string) ([]byte, error)
- func (r *RecordingFS) Stat(context.Context, string) (capfs.Info, error)
- func (r *RecordingFS) Write(context.Context, string, []byte, ...capfs.WriteOption) error
- type RecordingSandbox
- func (r *RecordingSandbox) CheckRead(context.Context, string) error
- func (r *RecordingSandbox) CheckWrite(context.Context, string) error
- func (r *RecordingSandbox) Enabled() bool
- func (r *RecordingSandbox) LastWrapCall() (SandboxWrapCall, bool)
- func (r *RecordingSandbox) WrapArgv(_ context.Context, cwd string, inner []string) ([]string, error)
- type SandboxWrapCall
- type ScopedWorkspace
- type ScriptedAgentConfig
- type StubEnvPairResolver
- type SubagentDelegateConfig
- type SubagentDelegateEnv
Constants ¶
const DefaultResearcherDef = `` /* 130-byte string literal not displayed */
Variables ¶
This section is empty.
Functions ¶
func AskAllToolsPolicy ¶ added in v0.1.10
AskAllToolsPolicy returns a policy that requires human approval for every tool call.
func AssertDeriveMessagesReplayPairing ¶ added in v0.3.33
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 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 ¶
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 ¶
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 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 ¶
func SessionEvents(t *testing.T, ctx context.Context, store agentkit.SessionStore, id agentkit.SessionID) []agentkit.SessionEvent
SessionEvents reads all events from a session id via the store.
func SubagentStart ¶
func SubagentStart(t *testing.T, ev agentkit.SessionEvent) sessevents.SubagentStartData
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
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.
Types ¶
type AllowAll ¶
type AllowAll struct{}
AllowAll approves every tool execution.
func (AllowAll) Ask ¶
func (AllowAll) Ask(context.Context, agentkit.ApprovalRequest) (agentkit.ApprovalDecision, error)
type DenyAll ¶
type DenyAll struct{}
DenyAll rejects every tool execution.
func (DenyAll) Ask ¶
func (DenyAll) Ask(context.Context, agentkit.ApprovalRequest) (agentkit.ApprovalDecision, error)
type DirWorkspace ¶
DirWorkspace maps scoped rel paths (e.g. local:agents) to directories.
type GateSandbox ¶ added in v0.3.48
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
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) Find ¶ added in v0.3.48
func (r *RecordingFS) Find(context.Context, capfs.FindRequest) (capfs.FindResult, error)
func (*RecordingFS) Grep ¶ added in v0.3.48
func (r *RecordingFS) Grep(context.Context, capfs.GrepRequest) (capfs.GrepResult, error)
func (*RecordingFS) Write ¶ added in v0.3.48
func (r *RecordingFS) Write(context.Context, string, []byte, ...capfs.WriteOption) error
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.
type SandboxWrapCall ¶ added in v0.3.48
SandboxWrapCall records one WrapArgv invocation.
type ScopedWorkspace ¶ added in v0.3.48
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.
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
StubEnvPairResolver implements credentials.Store and EnvPairResolver for shell / shell-bwrap env injection tests.
func (StubEnvPairResolver) EnvPairs ¶ added in v0.3.48
func (s StubEnvPairResolver) EnvPairs(_ context.Context, scope string, _ credentials.EnvPairsOptions) ([]string, error)
func (StubEnvPairResolver) Resolve ¶ added in v0.3.48
func (s StubEnvPairResolver) Resolve(context.Context, string, string) (credentials.Secret, error)
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.