glean/testing/simulation
Deterministic simulation of multi-step agent workflows.
A simulation is a script of what the model says at each step. There are two ways to run it:
run_agentputs the script in front of a real agent: the steps are the model’s replies, and the real agent loop runs the agent’s own tools, step limit and output validation. Use it to test an agent.runwalks the script alone. No agent and no tool runs: the tool results are the scripted ones. Use it to try out assertions on a conversation shape.
Neither makes a network call.
Types
Optional assertion function. Use no_assert for no assertion.
pub type AssertFn {
NoAssert
Assert(fn(SimulationState) -> Result(Nil, AssertionError))
}
Constructors
-
NoAssert -
Assert(fn(SimulationState) -> Result(Nil, AssertionError))
Error from a failed assertion.
pub type AssertionError {
AssertionError(step: Int, expected: String, got: String)
}
Constructors
-
AssertionError(step: Int, expected: String, got: String)
Describes a tool call and its expected result.
pub type ScriptedToolExecution {
ScriptedToolExecution(
call: provider.ScriptedToolCall,
result: ToolExecutionResult,
)
}
Constructors
-
ScriptedToolExecution( call: provider.ScriptedToolCall, result: ToolExecutionResult, )
A complete simulation scenario (opaque, built with builder pattern).
pub opaque type Simulation
Result of running a simulation.
pub type SimulationResult {
SimulationPassed(name: String, final_state: SimulationState)
SimulationFailed(
name: String,
step: Int,
error: AssertionError,
)
SimulationExceededMaxSteps(
name: String,
max_steps: Int,
state: SimulationState,
)
}
Constructors
-
SimulationPassed(name: String, final_state: SimulationState) -
SimulationFailed(name: String, step: Int, error: AssertionError) -
SimulationExceededMaxSteps( name: String, max_steps: Int, state: SimulationState, )
State accumulated during simulation.
pub type SimulationState {
SimulationState(
messages: List(message.Message),
step_count: Int,
total_usage: usage.Usage,
tool_calls_made: List(message.ToolResult),
errors_encountered: List(error.GleanError),
)
}
Constructors
-
SimulationState( messages: List(message.Message), step_count: Int, total_usage: usage.Usage, tool_calls_made: List(message.ToolResult), errors_encountered: List(error.GleanError), )
A step in a simulated agent workflow.
pub type SimulationStep {
ExpectText(text: String, then_assert: AssertFn)
ExpectToolCalls(
executions: List(ScriptedToolExecution),
then_assert: AssertFn,
)
ExpectError(error: error.GleanError, then_assert: AssertFn)
}
Constructors
-
ExpectText(text: String, then_assert: AssertFn)Provider returns text (agent loop should terminate)
-
ExpectToolCalls( executions: List(ScriptedToolExecution), then_assert: AssertFn, )Provider returns tool calls, tools execute, results fed back
-
ExpectError(error: error.GleanError, then_assert: AssertFn)Provider returns an error
What happens when a tool is executed.
pub type ToolExecutionResult {
ToolSuccess(content: String)
ToolFailure(error: String)
}
Constructors
-
ToolSuccess(content: String) -
ToolFailure(error: String)
Values
pub fn assert_finally(
sim: Simulation,
assertion: fn(SimulationState) -> Result(Nil, AssertionError),
) -> Simulation
Add a final assertion that runs after all steps complete.
pub fn assert_passed(result: SimulationResult) -> SimulationState
Assert that a simulation passed. Panics with descriptive message if not.
pub fn run(sim: Simulation) -> SimulationResult
Walk the script on its own. A pure function: no agent and no tool
runs, and every tool result is the scripted one, so this tests the
assertions and not an agent. To test an agent, use run_agent.
pub fn run_agent(
sim: Simulation,
agent: agent.Agent(deps),
deps: deps,
) -> SimulationResult
Run the script against a real agent.
The steps are the model’s replies, in order. Everything else is real:
the agent loop, the agent’s own tools called with deps, its settings,
its step limit (not with_max_steps) and its output validation. The
simulation’s system prompt, when set, is added after the agent’s own.
The simulation fails when:
- a tool returns something other than its scripted result
(
ToolSuccessis the output the tool must return,ToolFailurethe error the model must be shown), - the agent stops before the script ends, or asks the model for more replies than the script has,
- the run fails with an error the script doesn’t end with, or
- an assertion fails.
Each step’s assertion sees the conversation as it stood after that step, tool results included.
pub fn then_call_tools(
sim: Simulation,
executions: List(ScriptedToolExecution),
) -> Simulation
Add a step where the provider calls tools.
pub fn then_call_tools_and_assert(
sim: Simulation,
executions: List(ScriptedToolExecution),
assertion: fn(SimulationState) -> Result(Nil, AssertionError),
) -> Simulation
Add a tool call step with assertion.
pub fn then_provider_error(
sim: Simulation,
error: error.GleanError,
) -> Simulation
Add a step where the provider returns an error.
pub fn then_respond_text(
sim: Simulation,
text: String,
) -> Simulation
Add a step where the provider returns text.
pub fn then_respond_text_and_assert(
sim: Simulation,
text: String,
assertion: fn(SimulationState) -> Result(Nil, AssertionError),
) -> Simulation
Add a text step with assertion.
pub fn with_max_steps(sim: Simulation, max: Int) -> Simulation
Set max steps before the simulation is considered stuck.
pub fn with_system_prompt(
sim: Simulation,
prompt: String,
) -> Simulation
Set the system prompt.