Skip to main content

EventVerifier

Struct EventVerifier 

Source
pub struct EventVerifier { /* private fields */ }
Expand description

Standalone protocol ordering and attribution checks, without an event queue. Disabling verification does not expose a permissive no-op version of this API. Tracks what is open, and who opened it, so misordered events can be named precisely.

Implementations§

Source§

impl Verifier

Source

pub fn new() -> Self

Creates the ordering state for one stream.

Source

pub fn seed(&mut self, history: &[Message])

Learns the conversation the run continues — the request’s messages — before its first event. As history, like the RUN_STARTED echo, which is optional: a resumed run answering a call the interrupted run opened is answering a call the consumer already holds, echo or not.

A hosted run does this itself. A verifier used on its own needs it when the run answers a tool call an earlier run made, or continues a message a subagent owns: without the history that result is one for a call that never started.

// The request's history: the previous run asked for a search.
let history = [Message::Assistant(AssistantMessage {
    id: "msg-1".into(),
    tool_calls: Some(vec![ToolCall::new("call-1", "search", "{}")]),
    ..Default::default()
})];
let answer = Event::tool_call_result("msg-2", "call-1", "3 results");

let mut verifier = EventVerifier::new();
verifier.observe(&Event::run_started("thread-1", "run-2"))?;
assert!(verifier.observe(&answer).is_err());

let mut verifier = EventVerifier::new();
verifier.seed(&history);
verifier.observe(&Event::run_started("thread-1", "run-2"))?;
verifier.observe(&answer)?;
Source

pub fn replay<'a>( &mut self, events: impl IntoIterator<Item = &'a Event>, ) -> Result<(), VerificationError>

Observes the events a run has already sent, in order, so that the checks carry on from where the run is.

This is how a verifier is made for a run that continues somewhere else: in another process, or after the one that was driving it stopped. Replay what was sent, then observe what is sent next. The continuation sends no RUN_STARTED: the one that was sent is among the events replayed, and a second inside the same run is rejected.

seed the history first when the run has one. What is replayed may also begin with the runs that came before on the same stream: the checks then stand in the last run it starts.

// What the store holds of the run so far.
let sent = [
    Event::run_started("thread-1", "run-1"),
    Event::text_message_start("msg-1", TextMessageRole::Assistant),
    Event::text_message_content("msg-1", "Hello"),
];

let mut verifier = EventVerifier::new();
verifier.replay(&sent)?;

// The run is where it was left: started, with `msg-1` open.
let again = verifier.observe(&Event::run_started("thread-1", "run-1"));
assert_eq!(again.unwrap_err().rule, Rule::DuplicateRunStarted);
let early = verifier.observe(&Event::run_finished_success("thread-1", "run-1"));
assert_eq!(early.unwrap_err().rule, Rule::OpenAtFinish);

verifier.observe(&Event::text_message_content("msg-1", " world"))?;
verifier.observe(&Event::text_message_end("msg-1"))?;
verifier.observe(&Event::run_finished_success("thread-1", "run-1"))?;
§Cost

One observe per event, and nothing is copied or serialized. What the verifier keeps grows with the ids a run introduces, not with the number of its events.

§Errors

The first event that breaks a rule, as observe reports it. The events before it stay observed.

Source

pub fn observe(&mut self, event: &Event) -> Result<(), VerificationError>

Checks event against the state machine and folds it in.

Trait Implementations§

Source§

impl Clone for Verifier

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Verifier

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Verifier

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more