expect

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package expect checks tool calls against typed expectations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Require

func Require(t testing.TB, s Subject, expectations ...Expectation)

Require fails a test once with every failed diagnostic in declaration order. Requiring no expectations fails the test.

Types

type Check

type Check struct {
	Name       string
	Passed     bool
	Diagnostic string
}

Check is the result of one expectation. Passed checks have no diagnostic; failed checks always have a diagnostic.

func Evaluate

func Evaluate(s Subject, expectations ...Expectation) []Check

Evaluate returns one check per expectation in declaration order. Call expectations share a greedy, forward-only cursor. Check names are unique; repeated names receive #2, #3 and so on, avoiding existing suffixes.

type Expectation

type Expectation interface {
	// contains filtered or unexported methods
}

Expectation is a sealed check constructed by this package.

func AllAttemptsSuccessful

func AllAttemptsSuccessful(tool tools.Named) Expectation

AllAttemptsSuccessful checks every selected execution of tool, including earlier failed calls before a later successful call. No calls fails the check. It is not applicable to Calls.

func Await

func Await(kind planner.AwaitItemKind, predicate func(planner.AwaitItem) error) Expectation

Await checks any observed prompt of the requested planner await kind without consuming the tool-call cursor. Unverified prompts fail before decoding or invoking predicates. It is not applicable to Calls.

func Bounds

func Bounds(tool tools.Named, predicate func(*agent.Bounds) error) Expectation

Bounds checks recorded bounds on any execution of tool. It does not consume the call-matching cursor. It is not applicable to Calls.

func Call

func Call[P, R any](tool tools.TypedTool[P, R], args func(P) error, result func(R) error) Expectation

Call matches the first later call satisfying the typed predicates. Either predicate may be nil. Nil predicates skip decoding that value. A result predicate is not applicable to a Calls subject.

func Completed

func Completed() Expectation

Completed checks that every selected run has observed successful completion. Missing or unverified completion evidence fails. It is not applicable to Calls.

func Decoded

func Decoded[T any](tool tools.Named, codec tools.JSONCodec[T], args func(T) error) Expectation

Decoded matches the first later named call whose arguments decode with codec and satisfy args. A nil predicate still requires successful decoding.

func Exact

func Exact(expectations ...Expectation) Expectation

Exact requires its expectations to match the remaining calls one-to-one in order. It panics at construction if any nested expectation is Forbid.

func Failed

func Failed[P, R any](tool tools.TypedTool[P, R], args func(P) error, failure func(*planner.ToolError) error) Expectation

Failed matches the first later failed call satisfying optional argument and error predicates. It is not applicable to Calls.

func Forbid

func Forbid(names ...tools.Named) Expectation

Forbid rejects calls to any of the named tools anywhere in the subject.

func Func

func Func(name string, predicate func(Subject) error) Expectation

Func creates an expectation from a named predicate. A nil error passes.

func MaxModelCalls

func MaxModelCalls(n int) Expectation

MaxModelCalls limits adopted model calls across every selected run. Abandoned and incomplete calls do not count. It is not applicable to Calls.

func MaxTokens

func MaxTokens(n int) Expectation

MaxTokens limits input, output and cache tokens from all recorded model calls, including abandoned calls, across selected runs. It is not applicable to Calls.

func RawCall

func RawCall(tool tools.Named, args func(rawjson.Message) error, result func(rawjson.Message) error) Expectation

RawCall matches the first later named call satisfying raw JSON predicates. Arguments must be valid JSON even when args is nil. A non-nil result predicate requires a verified successful execution with a recorded JSON result; it is not applicable to Calls. A nil result predicate checks arguments only and does not assert execution success. Predicates receive independent copies of the bytes. Prefer Call when a generated typed descriptor is available.

func RawFailed

func RawFailed(tool tools.Named, args func(rawjson.Message) error, failure func(*planner.ToolError) error) Expectation

RawFailed matches the first later verified failed execution of a named tool. Arguments must be valid JSON even when args is nil. A nil failure predicate accepts any recorded tool error. RawFailed is not applicable to Calls. The argument predicate receives an independent copy of the JSON bytes. Prefer Failed when a generated typed descriptor is available.

type ExpectedTool

type ExpectedTool struct {
	Name tools.Ident     `json:"name"`
	Args json.RawMessage `json:"args,omitzero"`
}

ExpectedTool is a tool identifier with optional JSON-subset arguments. Omitted Args matches any arguments, while JSON null requires an actual null.

type ExpectedTools

type ExpectedTools struct {
	Tools   []ExpectedTool `json:"tools"`
	Ordered bool           `json:"ordered"`
	Exact   bool           `json:"exact"`
}

ExpectedTools describes data-driven tool correctness. Ordered uses greedy subsequence matching; otherwise matching is unordered and one-to-one. Exact additionally requires every actual call to match an expected call.

func (ExpectedTools) Evaluate

func (e ExpectedTools) Evaluate(s Subject) (Check, Score)

Evaluate returns the tool check and its tool_correctness score: matching calls divided by calls made. With no calls, the score is one if none were expected, otherwise zero. The score has no threshold. Objects match recursively as subsets, arrays match exactly, and numbers compare by exact decimal value. Malformed actual arguments are non-matches reported in a failed check's diagnostic, while valid calls still contribute to the score.

type Score

type Score struct {
	Name      string
	Value     float64
	Threshold *float64
}

Score is a numeric evaluation result with an optional passing threshold.

type Subject

type Subject interface {
	// contains filtered or unexported methods
}

Subject is a sealed input built by a subject constructor, such as Calls. Constructors normalize their inputs inside this package.

func Calls

func Calls(calls []model.ToolCall) Subject

Calls constructs a subject from the ordered calls chosen by a model. Calls provide arguments but no execution results, errors, or run-level evidence.

func Session

func Session(runs trajectory.Session) Subject

Session constructs a subject in caller-supplied run order, including children. Listing a child separately from its parent selects that child twice.

func TopLevel

func TopLevel(s Subject) Subject

TopLevel excludes descendants from calls, events, completion and budgets. Explicit session members remain roots even if they were another run's child.

func Trajectory

func Trajectory(run *trajectory.Trajectory) Subject

Trajectory constructs a subject from one recorded run. Descendant calls follow their parent call in the trajectory's canonical child-link order.

Jump to

Keyboard shortcuts

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