MiddlewareTestKit

org.llm4s.agent.testkit.MiddlewareTestKit

What a test needs to call an AgentMiddleware's hooks directly, with no agent, no runtime and no model (#2795): the contexts the agent loop would build, over a thread state the test chooses, and the order a stack resolves to.

val state   = MiddlewareTestKit.state(Set(Articles), StateUpdate.update(Articles, "returns policy"))
val context = state.map(MiddlewareTestKit.modelContext(_))
context.flatMap(new KnowledgeBase(search).promptSections(_))

new RedactEmails().admit("write to ada@example.com", MiddlewareTestKit.admissionContext())

The contexts are outside any runtime: events a hook emits go nowhere, no timeout is enforced, and effect records are kept in memory. Model usage is counted, and a token budget (RunBudgets.maxTokens on config) is enforced, as in a run: read what a hook's accounted calls recorded with usage. To test what a turn stores, what a model is sent or how a run recovers, run an agent against a scripted client instead.

Attributes

Source
MiddlewareTestKit.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def admissionContext(config: RunConfig, threadId: ThreadId, agent: String): AdmissionContext

The context admit is given (#2854), for a turn on threadId with config's tenant and principal, before anything of the turn is stored. It has no thread state and no run: a stage reads who the turn is for and counts its own model calls, which usage reads back.

The context admit is given (#2854), for a turn on threadId with config's tenant and principal, before anything of the turn is stored. It has no thread state and no run: a stage reads who the turn is for and counts its own model calls, which usage reads back.

Value parameters

agent

the id of the agent whose stage it is, for the usage events an accounted call announces

Attributes

Source
MiddlewareTestKit.scala
def modelContext(state: ThreadState, config: RunConfig, threadId: ThreadId, agent: String): ModelContext

The context beforeAgent, promptSections, wrapModelCall and afterAgent are given: state as the committed thread state, and a run on threadId with config - its run id, tenant, principal, budgets and metadata.

The context beforeAgent, promptSections, wrapModelCall and afterAgent are given: state as the committed thread state, and a run on threadId with config - its run id, tenant, principal, budgets and metadata.

Value parameters

agent

the id of the agent whose hook it is, for the usage events an accounted call announces

Attributes

Source
MiddlewareTestKit.scala

The ids of middleware in the order a stack runs them, outermost first - runsBefore and runsAfter applied, ties broken by the order given - or the ValidationError an agent's build() would return for them: an invalid or duplicate id, a required constraint naming a middleware that is absent, a cycle, a tool contributed twice, or a refused placement. It checks the middleware given and nothing else: an agent adds its own (a harness's, the builder's guardrails, delegation), and checks a placement across a handoff family.

The ids of middleware in the order a stack runs them, outermost first - runsBefore and runsAfter applied, ties broken by the order given - or the ValidationError an agent's build() would return for them: an invalid or duplicate id, a required constraint naming a middleware that is absent, a cycle, a tool contributed twice, or a refused placement. It checks the middleware given and nothing else: an agent adds its own (a harness's, the builder's guardrails, delegation), and checks a placement across a handoff family.

Attributes

Source
MiddlewareTestKit.scala
def state(keys: Set[StateKey[_, _]], update: StateUpdate): Result[ThreadState]

A thread state that registers keys - a middleware's writes, and any other key its hooks read - beside the loop's own (Messages.key, LoopKeys.usage, ...), with update applied. Left when update touches a key that is not registered, or does not apply.

A thread state that registers keys - a middleware's writes, and any other key its hooks read - beside the loop's own (Messages.key, LoopKeys.usage, ...), with update applied. Left when update touches a key that is not registered, or does not apply.

Attributes

Source
MiddlewareTestKit.scala
def toolContext(call: ToolCall, state: ThreadState, config: RunConfig, threadId: ThreadId, approved: Boolean): ToolContext

The context wrapToolCall - and a tool's execute - is given for call: state, a run on threadId with config, the call's id, and the idempotency key the loop would derive for it, the same for the same call on the same thread. approved is what a wrapper sees when the chain runs again after an approval.

The context wrapToolCall - and a tool's execute - is given for call: state, a run on threadId with config, the call's id, and the idempotency key the loop would derive for it, the same for the same call on the same thread. approved is what a wrapper sees when the chain runs again after an approval.

Attributes

Source
MiddlewareTestKit.scala

state with update applied, as the runtime commits it: for giving a later hook what an earlier one returned (BoundaryResult.update, ModelResponse.update). Left when update touches a key state does not register.

state with update applied, as the runtime commits it: for giving a later hook what an earlier one returned (BoundaryResult.update, ModelResponse.update). Left when update touches a key state does not register.

Attributes

Source
MiddlewareTestKit.scala

The model usage the hooks given context have recorded so far, through accounted clients and recordUsage.

The model usage the hooks given context have recorded so far, through accounted clients and recordUsage.

Attributes

Source
MiddlewareTestKit.scala

The model usage the stages given context have counted so far, through accounted clients and recordUsage.

The model usage the stages given context have counted so far, through accounted clients and recordUsage.

Attributes

Source
MiddlewareTestKit.scala

Concrete fields

The thread the contexts are on unless the test names another.

The thread the contexts are on unless the test names another.

Attributes

Source
MiddlewareTestKit.scala

A thread state with only the loop's own keys, each at its initial value.

A thread state with only the loop's own keys, each at its initial value.

Attributes

Source
MiddlewareTestKit.scala