#![cfg_attr(
not(all(feature = "client", feature = "server", feature = "verify")),
allow(dead_code)
)]
use std::collections::{HashMap, HashSet};
use crate::streams::{ChunkError, Owner, Streams};
use crate::{
Event, EventType, Message, MessageId, ReasoningEncryptedValueSubtype, StepName, SubagentRunId,
ToolCallId,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Policy {
pub(crate) results_answer_known_calls: bool,
}
impl Policy {
pub(crate) const PRODUCER: Self = Self {
results_answer_known_calls: true,
};
pub(crate) const CONSUMER: Self = Self {
results_answer_known_calls: false,
};
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Entity {
Message,
ToolCall,
ReasoningMessage,
ReasoningBlock,
Activity,
}
impl Entity {
pub(crate) const fn noun(self) -> &'static str {
match self {
Self::Message => "message",
Self::ToolCall => "tool call",
Self::ReasoningMessage => "reasoning message",
Self::ReasoningBlock => "reasoning block",
Self::Activity => "activity",
}
}
}
pub(crate) fn describe(owner: &Owner) -> String {
match owner {
None => "the parent agent".to_owned(),
Some(id) => format!("subagent {:?}", id.as_str()),
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Claim {
Continues,
Opens,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Open {
Entity { entity: Entity, id: String },
Step { name: String, owner: Owner },
Subagent { id: String },
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Broke {
NotStarted,
RunEnded,
RunStartedTwice,
AlreadyOpen { entity: Entity, id: String },
NotOpen { entity: Entity, id: String },
OwnerMismatch {
entity: Entity,
id: String,
owner: Owner,
named: Owner,
claim: Claim,
},
ParentMismatch {
id: String,
named: Owner,
parent: String,
owner: Owner,
},
UnknownToolCall { id: String },
ResultBeforeEnd { id: String },
StepRunning { name: String, owner: Owner },
StepNotRunning { name: String, owner: Owner },
SubagentActive { id: String },
SubagentReused { id: String },
UnknownParentSubagent { id: String, parent: String },
SubagentNotActive { id: String },
StillOpen(Open),
InvalidOutcome(String),
Chunk(ChunkError),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Violation {
pub(crate) event: EventType,
pub(crate) broke: Broke,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Phase {
Idle,
Assumed,
Active,
Finished,
Failed,
}
#[derive(Clone, Debug)]
pub(crate) struct Ordering {
policy: Policy,
phase: Phase,
streams: Streams,
text: OpenSet<MessageId, Owner>,
tool: OpenSet<ToolCallId, Owner>,
reasoning: OpenSet<MessageId, Owner>,
blocks: OpenSet<MessageId, Owner>,
steps: OpenSet<(Owner, StepName), ()>,
subagents: OpenSet<SubagentRunId, ()>,
closed_subagents: HashSet<SubagentRunId>,
message_owners: HashMap<MessageId, Owner>,
reasoning_owners: HashMap<MessageId, Owner>,
tool_call_owners: HashMap<ToolCallId, Owner>,
activity_owners: HashMap<MessageId, Owner>,
}
#[derive(Clone)]
struct OpenSet<K, V> {
by_key: HashMap<K, (u64, V)>,
next: u64,
}
impl<K: Eq + std::hash::Hash + std::fmt::Debug, V: std::fmt::Debug> std::fmt::Debug
for OpenSet<K, V>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_map().entries(self.in_order()).finish()
}
}
impl<K, V> Default for OpenSet<K, V> {
fn default() -> Self {
Self {
by_key: HashMap::new(),
next: 0,
}
}
}
impl<K: Eq + std::hash::Hash, V> OpenSet<K, V> {
fn contains(&self, key: &K) -> bool {
self.by_key.contains_key(key)
}
fn get(&self, key: &K) -> Option<&V> {
self.by_key.get(key).map(|(_, value)| value)
}
fn push(&mut self, key: K, value: V) {
self.by_key.insert(key, (self.next, value));
self.next += 1;
}
fn remove(&mut self, key: &K) -> Option<(u64, V)> {
self.by_key.remove(key)
}
fn restore(&mut self, key: K, place: u64, value: V) {
self.by_key.insert(key, (place, value));
}
fn clear(&mut self) {
self.by_key.clear();
}
fn in_order(&self) -> Vec<(&K, &V)> {
let mut open: Vec<_> = self.by_key.iter().collect();
open.sort_unstable_by_key(|(_, (place, _))| *place);
open.into_iter()
.map(|(key, (_, value))| (key, value))
.collect()
}
fn first(&self) -> Option<(&K, &V)> {
self.by_key
.iter()
.min_by_key(|(_, (place, _))| *place)
.map(|(key, (_, value))| (key, value))
}
}
enum Ended {
Text(u64, MessageId, Owner),
Tool(u64, ToolCallId, Owner),
Reasoning(u64, MessageId, Owner),
}
impl Ordering {
pub(crate) fn new(policy: Policy) -> Self {
Self {
policy,
phase: Phase::Idle,
streams: Streams::default(),
text: OpenSet::default(),
tool: OpenSet::default(),
reasoning: OpenSet::default(),
blocks: OpenSet::default(),
steps: OpenSet::default(),
subagents: OpenSet::default(),
closed_subagents: HashSet::new(),
message_owners: HashMap::new(),
reasoning_owners: HashMap::new(),
tool_call_owners: HashMap::new(),
activity_owners: HashMap::new(),
}
}
pub(crate) fn seed(&mut self, history: &[Message]) {
self.seed_owners(history, false);
self.streams.seed(history);
}
pub(crate) fn assume_opened(&mut self) {
if self.phase == Phase::Idle {
self.phase = Phase::Assumed;
}
}
pub(crate) fn end_run(&mut self) {
if matches!(self.phase, Phase::Assumed | Phase::Active) {
self.phase = Phase::Finished;
}
}
pub(crate) fn has_opened(&self) -> bool {
self.phase != Phase::Idle
}
pub(crate) fn has_ended(&self) -> bool {
matches!(self.phase, Phase::Finished | Phase::Failed)
}
pub(crate) fn observe(&mut self, event: &Event) -> Result<(), Violation> {
let at = event.event_type();
let reject = |broke| Violation { event: at, broke };
self.admit(at).map_err(reject)?;
let is_chunk = matches!(
event,
Event::TextMessageChunk(_) | Event::ToolCallChunk(_) | Event::ReasoningMessageChunk(_)
);
if !is_chunk && self.streams.is_idle() {
self.apply(event, None).map_err(reject)?;
self.streams.note(event);
return Ok(());
}
let before = self.streams.checkpoint();
let mut stood_for = Vec::new();
let expanded = if is_chunk {
self.streams.expand_quietly(event.clone(), &mut stood_for)
} else {
self.streams.close_for(event, &mut stood_for);
Ok(())
};
let mut ended = Vec::new();
let judged = expanded.map_err(Broke::Chunk).and_then(|()| {
for implied in &stood_for {
self.apply(implied, Some(&mut ended))?;
}
if is_chunk {
Ok(())
} else {
self.apply(event, None)
}
});
if judged.is_ok() {
for implied in &stood_for {
self.streams.note(implied);
}
if !is_chunk {
self.streams.note(event);
}
} else {
self.streams.rewind(before);
for entry in ended.into_iter().rev() {
match entry {
Ended::Text(place, id, owner) => self.text.restore(id, place, owner),
Ended::Tool(place, id, owner) => self.tool.restore(id, place, owner),
Ended::Reasoning(place, id, owner) => {
self.reasoning.restore(id, place, owner);
}
}
}
}
judged.map_err(reject)
}
fn admit(&self, at: EventType) -> Result<(), Broke> {
let opens = matches!(at, EventType::RunStarted | EventType::RunError);
match self.phase {
Phase::Idle if opens => Ok(()),
Phase::Idle => Err(Broke::NotStarted),
Phase::Assumed | Phase::Active => Ok(()),
Phase::Finished | Phase::Failed if at == EventType::RunStarted => Ok(()),
Phase::Finished if at == EventType::RunError => Ok(()),
Phase::Finished | Phase::Failed => Err(Broke::RunEnded),
}
}
fn begin_next_run(&mut self) {
self.streams.abandon();
self.text.clear();
self.tool.clear();
self.reasoning.clear();
self.blocks.clear();
self.steps.clear();
self.subagents.clear();
self.closed_subagents.clear();
}
fn apply(&mut self, event: &Event, ended: Option<&mut Vec<Ended>>) -> Result<(), Broke> {
match event {
Event::RunStarted(e) => {
if self.phase == Phase::Active {
return Err(Broke::RunStartedTwice);
}
if self.has_ended() {
self.begin_next_run();
}
self.phase = Phase::Active;
if let Some(input) = &e.input {
self.seed_owners(&input.messages, false);
}
}
Event::RunFinished(e) => {
if let Some(open) = self.first_open() {
return Err(Broke::StillOpen(open));
}
if let Some(error) = e
.outcome
.as_ref()
.and_then(crate::RunOutcome::interrupt_error)
{
return Err(Broke::InvalidOutcome(error));
}
self.phase = Phase::Finished;
}
Event::RunError(_) => self.phase = Phase::Failed,
Event::TextMessageStart(e) => {
let owner = self.claim(Entity::Message, &e.message_id, &e.subagent_run_id)?;
not_open(&self.text, Entity::Message, &e.message_id)?;
self.message_owners
.entry(e.message_id.clone())
.or_insert_with(|| owner.clone());
self.text.push(e.message_id.clone(), owner);
}
Event::TextMessageContent(e) => {
require(
&self.text,
Entity::Message,
&e.message_id,
&e.subagent_run_id,
)?;
}
Event::TextMessageEnd(e) => {
require(
&self.text,
Entity::Message,
&e.message_id,
&e.subagent_run_id,
)?;
if let Some((place, owner)) = self.text.remove(&e.message_id) {
if let Some(ended) = ended {
ended.push(Ended::Text(place, e.message_id.clone(), owner));
}
}
}
Event::ToolCallStart(e) => {
let owner = self.tool_call_owner(
&e.tool_call_id,
e.parent_message_id.as_ref(),
&e.subagent_run_id,
)?;
not_open(&self.tool, Entity::ToolCall, &e.tool_call_id)?;
self.tool_call_owners
.insert(e.tool_call_id.clone(), owner.clone());
self.tool.push(e.tool_call_id.clone(), owner);
}
Event::ToolCallArgs(e) => {
require(
&self.tool,
Entity::ToolCall,
&e.tool_call_id,
&e.subagent_run_id,
)?;
}
Event::ToolCallEnd(e) => {
require(
&self.tool,
Entity::ToolCall,
&e.tool_call_id,
&e.subagent_run_id,
)?;
if let Some((place, owner)) = self.tool.remove(&e.tool_call_id) {
if let Some(ended) = ended {
ended.push(Ended::Tool(place, e.tool_call_id.clone(), owner));
}
}
}
Event::ToolCallResult(e) => {
let id = &e.tool_call_id;
if self.policy.results_answer_known_calls && !self.tool_call_owners.contains_key(id)
{
return Err(Broke::UnknownToolCall {
id: id.as_str().to_owned(),
});
}
if self.tool.contains(id) {
return Err(Broke::ResultBeforeEnd {
id: id.as_str().to_owned(),
});
}
self.message_owners
.insert(e.message_id.clone(), e.subagent_run_id.clone());
}
Event::ReasoningStart(e) => {
let owner =
self.claim(Entity::ReasoningMessage, &e.message_id, &e.subagent_run_id)?;
not_open(&self.blocks, Entity::ReasoningBlock, &e.message_id)?;
self.reasoning_owners
.entry(e.message_id.clone())
.or_insert_with(|| owner.clone());
self.blocks.push(e.message_id.clone(), owner);
}
Event::ReasoningEnd(e) => {
require(
&self.blocks,
Entity::ReasoningBlock,
&e.message_id,
&e.subagent_run_id,
)?;
self.blocks.remove(&e.message_id);
}
Event::ReasoningMessageStart(e) => {
let owner =
self.claim(Entity::ReasoningMessage, &e.message_id, &e.subagent_run_id)?;
not_open(&self.reasoning, Entity::ReasoningMessage, &e.message_id)?;
self.reasoning_owners
.entry(e.message_id.clone())
.or_insert_with(|| owner.clone());
self.reasoning.push(e.message_id.clone(), owner);
}
Event::ReasoningMessageContent(e) => {
require(
&self.reasoning,
Entity::ReasoningMessage,
&e.message_id,
&e.subagent_run_id,
)?;
}
Event::ReasoningMessageEnd(e) => {
require(
&self.reasoning,
Entity::ReasoningMessage,
&e.message_id,
&e.subagent_run_id,
)?;
if let Some((place, owner)) = self.reasoning.remove(&e.message_id) {
if let Some(ended) = ended {
ended.push(Ended::Reasoning(place, e.message_id.clone(), owner));
}
}
}
Event::ReasoningEncryptedValue(e) => {
let (entity, owner) = match e.subtype {
ReasoningEncryptedValueSubtype::ToolCall => (
Entity::ToolCall,
self.tool_call_owners
.get(&ToolCallId::new(e.entity_id.clone())),
),
ReasoningEncryptedValueSubtype::Message => {
let id = MessageId::new(e.entity_id.clone());
(
Entity::Message,
self.message_owners
.get(&id)
.or_else(|| self.reasoning_owners.get(&id)),
)
}
};
if let Some(owner) = owner {
agrees(
entity,
&e.entity_id,
owner,
&e.subagent_run_id,
Claim::Continues,
)?;
}
}
Event::ActivitySnapshot(e) => {
if e.replace || !self.activity_owners.contains_key(&e.message_id) {
self.activity_owners
.insert(e.message_id.clone(), e.subagent_run_id.clone());
}
}
Event::ActivityDelta(e) => {
if let Some(owner) = self.activity_owners.get(&e.message_id) {
let named = &e.subagent_run_id;
agrees(
Entity::Activity,
&e.message_id,
owner,
named,
Claim::Continues,
)?;
}
}
Event::StepStarted(e) => {
let key = (e.subagent_run_id.clone(), e.step_name.clone());
if self.steps.contains(&key) {
return Err(Broke::StepRunning {
name: key.1.as_str().to_owned(),
owner: key.0,
});
}
self.steps.push(key, ());
}
Event::StepFinished(e) => {
let key = (e.subagent_run_id.clone(), e.step_name.clone());
if self.steps.remove(&key).is_none() {
return Err(Broke::StepNotRunning {
name: key.1.as_str().to_owned(),
owner: key.0,
});
}
}
Event::SubagentStarted(e) => {
let id = &e.subagent_run_id;
if self.subagents.contains(id) {
return Err(Broke::SubagentActive {
id: id.as_str().to_owned(),
});
}
if self.closed_subagents.contains(id) {
return Err(Broke::SubagentReused {
id: id.as_str().to_owned(),
});
}
if let Some(parent) = &e.parent_subagent_run_id {
if !self.subagents.contains(parent) && !self.closed_subagents.contains(parent) {
return Err(Broke::UnknownParentSubagent {
id: id.as_str().to_owned(),
parent: parent.as_str().to_owned(),
});
}
}
self.subagents.push(id.clone(), ());
}
Event::SubagentFinished(e) => self.close_subagent(&e.subagent_run_id)?,
Event::SubagentError(e) => self.close_subagent(&e.subagent_run_id)?,
Event::MessagesSnapshot(e) => self.seed_owners(&e.messages, true),
_ => {}
}
Ok(())
}
fn claim(&self, entity: Entity, id: &MessageId, named: &Owner) -> Result<Owner, Broke> {
let owners = match entity {
Entity::Message => &self.message_owners,
_ => &self.reasoning_owners,
};
match owners.get(id) {
Some(owner) => {
agrees(entity, id, owner, named, Claim::Opens)?;
Ok(owner.clone())
}
None => Ok(named.clone()),
}
}
fn tool_call_owner(
&self,
id: &ToolCallId,
parent_message_id: Option<&MessageId>,
named: &Owner,
) -> Result<Owner, Broke> {
let inherited = parent_message_id
.and_then(|parent| self.message_owners.get(parent).map(|owner| (parent, owner)));
if let Some((parent, owner)) = inherited {
if named.is_some() && owner != named {
return Err(Broke::ParentMismatch {
id: id.as_str().to_owned(),
named: named.clone(),
parent: parent.as_str().to_owned(),
owner: owner.clone(),
});
}
}
let asserted: Option<Owner> = if named.is_some() {
Some(named.clone())
} else {
inherited.map(|(_, owner)| owner.clone())
};
let known = self.tool_call_owners.get(id);
if let (Some(asserted), Some(known)) = (&asserted, known) {
if asserted != known {
return Err(Broke::OwnerMismatch {
entity: Entity::ToolCall,
id: id.as_str().to_owned(),
owner: known.clone(),
named: asserted.clone(),
claim: Claim::Opens,
});
}
}
Ok(asserted.or_else(|| known.cloned()).unwrap_or(None))
}
fn seed_owners(&mut self, messages: &[Message], authoritative: bool) {
for message in messages {
let owner = message.subagent_run_id().cloned();
let bucket = match message {
Message::Activity(_) => &mut self.activity_owners,
Message::Reasoning(_) => &mut self.reasoning_owners,
_ => &mut self.message_owners,
};
if authoritative || !bucket.contains_key(message.id()) {
bucket.insert(message.id().clone(), owner.clone());
}
if let Message::Assistant(assistant) = message {
for call in assistant.tool_calls.iter().flatten() {
if authoritative || !self.tool_call_owners.contains_key(&call.id) {
self.tool_call_owners.insert(call.id.clone(), owner.clone());
}
}
}
}
}
fn close_subagent(&mut self, id: &SubagentRunId) -> Result<(), Broke> {
match self.subagents.remove(id) {
Some(_) => {
self.closed_subagents.insert(id.clone());
Ok(())
}
None => Err(Broke::SubagentNotActive {
id: id.as_str().to_owned(),
}),
}
}
fn first_open(&self) -> Option<Open> {
let entity = |entity, id: &str| Open::Entity {
entity,
id: id.to_owned(),
};
if let Some((id, _)) = self.text.first() {
return Some(entity(Entity::Message, id.as_str()));
}
if let Some((id, _)) = self.tool.first() {
return Some(entity(Entity::ToolCall, id.as_str()));
}
if let Some((id, _)) = self.reasoning.first() {
return Some(entity(Entity::ReasoningMessage, id.as_str()));
}
if let Some((id, _)) = self.blocks.first() {
return Some(entity(Entity::ReasoningBlock, id.as_str()));
}
if let Some(((owner, name), ())) = self.steps.first() {
return Some(Open::Step {
name: name.as_str().to_owned(),
owner: owner.clone(),
});
}
self.subagents.first().map(|(id, ())| Open::Subagent {
id: id.as_str().to_owned(),
})
}
pub(crate) fn open(&self) -> OpenSets<'_> {
fn ids<K: Eq + std::hash::Hash + AsRef<str>, V>(set: &OpenSet<K, V>) -> Vec<&str> {
set.in_order()
.into_iter()
.map(|(id, _)| id.as_ref())
.collect()
}
OpenSets {
messages: ids(&self.text),
tool_calls: ids(&self.tool),
reasoning_messages: ids(&self.reasoning),
reasoning_blocks: ids(&self.blocks),
steps: self
.steps
.in_order()
.into_iter()
.map(|((owner, name), ())| {
(owner.as_ref().map(SubagentRunId::as_str), name.as_str())
})
.collect(),
subagents: self
.subagents
.in_order()
.into_iter()
.map(|(id, ())| id.as_str())
.collect(),
}
}
}
#[derive(Debug, Default, PartialEq, Eq)]
pub(crate) struct OpenSets<'a> {
pub(crate) messages: Vec<&'a str>,
pub(crate) tool_calls: Vec<&'a str>,
pub(crate) reasoning_messages: Vec<&'a str>,
pub(crate) reasoning_blocks: Vec<&'a str>,
pub(crate) steps: Vec<(Option<&'a str>, &'a str)>,
pub(crate) subagents: Vec<&'a str>,
}
fn agrees(
entity: Entity,
id: &str,
owner: &Owner,
named: &Owner,
claim: Claim,
) -> Result<(), Broke> {
if named.is_some() && owner != named {
return Err(Broke::OwnerMismatch {
entity,
id: id.to_owned(),
owner: owner.clone(),
named: named.clone(),
claim,
});
}
Ok(())
}
fn not_open<T: Eq + std::hash::Hash + AsRef<str>>(
open: &OpenSet<T, Owner>,
entity: Entity,
id: &T,
) -> Result<(), Broke> {
if open.contains(id) {
return Err(Broke::AlreadyOpen {
entity,
id: id.as_ref().to_owned(),
});
}
Ok(())
}
fn require<T: Eq + std::hash::Hash + AsRef<str>>(
open: &OpenSet<T, Owner>,
entity: Entity,
id: &T,
named: &Owner,
) -> Result<(), Broke> {
let Some(owner) = open.get(id) else {
return Err(Broke::NotOpen {
entity,
id: id.as_ref().to_owned(),
});
};
agrees(entity, id.as_ref(), owner, named, Claim::Continues)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{RunAgentInput, TextMessageRole};
fn started(policy: Policy) -> Ordering {
let mut rules = Ordering::new(policy);
rules.observe(&Event::run_started("t", "r")).unwrap();
rules
}
fn broke(rules: &mut Ordering, event: Event) -> Broke {
rules
.observe(&event)
.expect_err("the event breaks a rule")
.broke
}
fn text_chunk(id: Option<&str>, delta: &str) -> Event {
Event::text_message_chunk(id.map(MessageId::new), Some(delta.to_owned()))
}
fn start(id: &str) -> Event {
Event::text_message_start(id, TextMessageRole::Assistant)
}
#[test]
fn the_lifecycle_is_the_one_in_the_specification() {
let mut rules = Ordering::new(Policy::CONSUMER);
assert_eq!(broke(&mut rules, start("m1")), Broke::NotStarted);
assert!(!rules.has_opened(), "a rejected first event opens nothing");
rules.observe(&Event::run_error("unreachable")).unwrap();
assert!(rules.has_ended());
let mut rules = started(Policy::CONSUMER);
assert_eq!(
broke(&mut rules, Event::run_started("t", "r2")),
Broke::RunStartedTwice
);
rules.observe(&Event::run_finished("t", "r")).unwrap();
assert_eq!(broke(&mut rules, start("m1")), Broke::RunEnded);
assert_eq!(
broke(&mut rules, Event::run_finished("t", "r")),
Broke::RunEnded
);
rules.observe(&Event::run_error("while flushing")).unwrap();
assert_eq!(
broke(&mut rules, Event::run_error("again")),
Broke::RunEnded
);
assert_eq!(broke(&mut rules, start("m1")), Broke::RunEnded);
assert!(rules.has_ended());
rules.observe(&Event::run_started("t", "r2")).unwrap();
assert!(!rules.has_ended());
rules.observe(&start("m1")).unwrap();
rules.observe(&Event::text_message_end("m1")).unwrap();
rules.observe(&Event::run_finished("t", "r2")).unwrap();
rules.observe(&Event::run_started("t", "r3")).unwrap();
assert_eq!(
broke(&mut rules, Event::run_started("t", "r4")),
Broke::RunStartedTwice
);
}
#[test]
fn a_new_run_starts_with_nothing_open_and_keeps_the_owners() {
for policy in [Policy::PRODUCER, Policy::CONSUMER] {
let mut rules = started(policy);
for event in [
Event::subagent_started("s1", "researcher"),
Event::subagent_started("s2", "writer"),
Event::subagent_finished_success("s2"),
start("m1").with_subagent_run_id("s1"),
Event::tool_call_start("c1", "search"),
Event::reasoning_start("r1"),
Event::reasoning_message_start("r1"),
Event::step_started("plan"),
text_chunk(Some("m2"), "a"),
Event::run_error("the model hung up"),
Event::run_started("t", "r2"),
] {
rules.observe(&event).unwrap();
}
assert_eq!(rules.open(), OpenSets::default(), "{policy:?}");
assert!(matches!(
broke(&mut rules, Event::text_message_content("m1", "x")),
Broke::NotOpen { .. }
));
assert!(matches!(
broke(&mut rules, text_chunk(None, "b")),
Broke::Chunk(ChunkError::NothingOpen { .. })
));
assert!(matches!(
broke(&mut rules, Event::step_finished("plan")),
Broke::StepNotRunning { .. }
));
assert!(matches!(
broke(&mut rules, Event::subagent_finished_success("s1")),
Broke::SubagentNotActive { .. }
));
for event in [
Event::step_started("plan"),
Event::step_finished("plan"),
Event::subagent_started("s2", "writer"),
Event::subagent_finished_success("s2"),
] {
rules.observe(&event).unwrap();
}
assert!(matches!(
broke(&mut rules, start("m1").with_subagent_run_id("s2")),
Broke::OwnerMismatch { .. }
));
rules
.observe(&Event::tool_call_result("m3", "c1", "ok"))
.expect("a call the run before introduced is known");
rules.observe(&Event::run_finished("t", "r2")).unwrap();
}
}
#[test]
fn a_rejected_run_started_changes_nothing() {
let mut rules = started(Policy::PRODUCER);
rules.observe(&start("m1")).unwrap();
rules.observe(&text_chunk(Some("m2"), "a")).unwrap();
let before = format!("{rules:?}");
assert_eq!(
broke(&mut rules, Event::run_started("t", "r2")),
Broke::RunStartedTwice
);
assert_eq!(format!("{rules:?}"), before);
}
#[test]
fn both_ends_reach_the_same_verdict_except_where_the_policy_says() {
let result = || Event::tool_call_result("m1", "c-unseen", "ok");
assert_eq!(
broke(&mut started(Policy::PRODUCER), result()),
Broke::UnknownToolCall {
id: "c-unseen".into()
}
);
started(Policy::CONSUMER).observe(&result()).unwrap();
}
#[test]
fn history_makes_a_call_known() {
let history = [Message::Assistant(crate::AssistantMessage {
id: "a1".into(),
tool_calls: Some(vec![crate::ToolCall::new("c1", "search", "{}")]),
..Default::default()
})];
let result = Event::tool_call_result("m1", "c1", "ok");
let mut seeded = Ordering::new(Policy::PRODUCER);
seeded.seed(&history);
seeded.observe(&Event::run_started("t", "r")).unwrap();
seeded.observe(&result).unwrap();
let mut input = RunAgentInput::new("t", "r");
input.messages = history.to_vec();
let mut echo = crate::RunStartedEvent::new("t", "r");
echo.input = Some(Box::new(input));
let mut echoed = Ordering::new(Policy::PRODUCER);
echoed.observe(&Event::RunStarted(echo)).unwrap();
echoed.observe(&result).unwrap();
}
#[test]
fn a_chunk_is_judged_as_the_events_it_stands_for() {
let mut rules = started(Policy::PRODUCER);
rules.observe(&text_chunk(Some("m1"), "a")).unwrap();
assert_eq!(rules.open().messages, ["m1"]);
let unnamed = Event::tool_call_chunk(Some("c1".into()), None, Some("{}".into()));
let violation = rules.observe(&unnamed).unwrap_err();
assert_eq!(violation.event, EventType::ToolCallChunk);
assert!(matches!(
violation.broke,
Broke::Chunk(ChunkError::Unnamed { .. })
));
rules
.observe(&Event::custom("note", serde_json::json!(1)))
.unwrap();
assert!(rules.open().messages.is_empty());
assert!(matches!(
broke(&mut rules, text_chunk(None, "b")),
Broke::Chunk(ChunkError::NothingOpen { .. })
));
rules.observe(&Event::run_finished("t", "r")).unwrap();
}
#[test]
fn the_two_spellings_do_not_mix_within_one_item() {
let mut rules = started(Policy::CONSUMER);
rules.observe(&start("m1")).unwrap();
assert_eq!(
broke(&mut rules, text_chunk(Some("m1"), "a")),
Broke::AlreadyOpen {
entity: Entity::Message,
id: "m1".into()
}
);
let mut rules = started(Policy::CONSUMER);
rules.observe(&text_chunk(Some("m1"), "a")).unwrap();
assert_eq!(
broke(&mut rules, Event::text_message_content("m1", "b")),
Broke::NotOpen {
entity: Entity::Message,
id: "m1".into()
}
);
}
#[test]
fn a_rejected_event_leaves_the_state_as_it_was() {
let mut rules = started(Policy::PRODUCER);
rules.observe(&Event::step_started("plan")).unwrap();
rules.observe(&text_chunk(Some("m1"), "a")).unwrap();
rules
.observe(&text_chunk(Some("m2"), "b").with_subagent_run_id("s1"))
.unwrap();
let before = format!("{rules:?}");
assert!(matches!(
broke(&mut rules, Event::run_finished("t", "r")),
Broke::StillOpen(Open::Step { .. })
));
assert_eq!(format!("{rules:?}"), before);
assert!(matches!(
broke(&mut rules, Event::text_message_end("m1")),
Broke::NotOpen { .. }
));
assert_eq!(format!("{rules:?}"), before);
rules.observe(&text_chunk(None, " more")).unwrap();
rules.observe(&Event::step_finished("plan")).unwrap();
rules.observe(&Event::run_finished("t", "r")).unwrap();
}
#[test]
fn the_state_is_rebuilt_by_replaying_what_was_accepted() {
let events = [
Event::run_started("t", "r"),
Event::subagent_started("s1", "researcher"),
start("m1").with_subagent_run_id("s1"),
Event::tool_call_chunk(Some("c1".into()), Some("search".into()), None),
Event::step_started("plan"),
];
let mut rules = Ordering::new(Policy::PRODUCER);
for event in &events {
rules.observe(event).unwrap();
}
let mut replayed = Ordering::new(Policy::PRODUCER);
for event in &events {
replayed.observe(event).unwrap();
}
assert_eq!(rules.open(), replayed.open());
assert_eq!(rules.clone().open(), rules.open());
assert_eq!(
broke(&mut replayed, Event::run_finished("t", "r")),
Broke::StillOpen(Open::Entity {
entity: Entity::Message,
id: "m1".into()
})
);
}
#[test]
fn an_interrupt_outcome_names_an_interrupt() {
let mut rules = started(Policy::PRODUCER);
assert_eq!(
broke(
&mut rules,
Event::run_finished_interrupt("t", "r", Vec::new())
),
Broke::InvalidOutcome(
"RUN_FINISHED outcome `interrupt` requires at least one interrupt".into()
)
);
assert!(!rules.has_ended());
}
fn ownership() -> Vec<(&'static str, Vec<Event>, Option<usize>)> {
use crate::{
ActivitySnapshotEvent, AssistantMessage, JsonObject, PatchOperation, RunStartedEvent,
ToolCall, ToolCallStartEvent,
};
let tagged = |event: Event, id: &str| event.with_subagent_run_id(id);
let call_in = |message: &str, id: &str| {
let mut start = ToolCallStartEvent::new(id, "search");
start.parent_message_id = Some(message.into());
Event::ToolCallStart(start)
};
let activity = |id: &str, replace: bool| {
let mut event = ActivitySnapshotEvent::new(id, "progress", JsonObject::new());
event.replace = replace;
Event::ActivitySnapshot(event)
};
let delta =
|id: &str| Event::activity_delta(id, "progress", vec![PatchOperation::add("/step", 1)]);
let history = |owner: Option<&str>| {
Message::Assistant(AssistantMessage {
id: "h1".into(),
content: Some("earlier".into()),
tool_calls: Some(vec![ToolCall::new("hc1", "search", "{}")]),
subagent_run_id: owner.map(Into::into),
..Default::default()
})
};
let echo = |messages: Vec<Message>| {
let mut input = RunAgentInput::new("t", "r");
input.messages = messages;
let mut started = RunStartedEvent::new("t", "r");
started.input = Some(Box::new(input));
Event::RunStarted(started)
};
let value = |subtype, id: &str| Event::reasoning_encrypted_value(subtype, id, "x");
let run = || Event::run_started("t", "r");
let done = || Event::run_finished_success("t", "r");
vec![
(
"a tagged continuation must name the opener",
vec![
run(),
tagged(start("m1"), "s1"),
tagged(Event::text_message_content("m1", "x"), "s2"),
],
Some(2),
),
(
"the first writer keeps the id through an untagged re-open",
vec![
run(),
tagged(start("m1"), "s1"),
tagged(Event::text_message_end("m1"), "s1"),
start("m1"),
tagged(Event::text_message_content("m1", "x"), "s1"),
Event::text_message_end("m1"),
tagged(call_in("m1", "c1"), "s1"),
tagged(Event::tool_call_end("c1"), "s1"),
done(),
],
None,
),
(
"and a sibling still may not continue it",
vec![
run(),
tagged(start("m1"), "s1"),
tagged(Event::text_message_end("m1"), "s1"),
start("m1"),
tagged(Event::text_message_content("m1", "x"), "s2"),
],
Some(4),
),
(
"a tool call belongs to the message that carries it",
vec![
run(),
tagged(start("m1"), "s1"),
tagged(call_in("m1", "c1"), "s2"),
],
Some(2),
),
(
"an untagged call inherits, so a sibling cannot continue it",
vec![
run(),
tagged(start("m1"), "s1"),
call_in("m1", "c1"),
tagged(Event::tool_call_args("c1", "{}"), "s2"),
],
Some(3),
),
(
"the echo seeds ownership",
vec![echo(vec![history(Some("s1"))]), tagged(start("h1"), "s2")],
Some(1),
),
(
"the echo seeds tool calls too",
vec![
echo(vec![history(Some("s1"))]),
tagged(Event::tool_call_start("hc1", "search"), "s2"),
],
Some(1),
),
(
"a snapshot is authoritative over the echo",
vec![
echo(vec![history(Some("s1"))]),
Event::messages_snapshot(vec![history(None)]),
tagged(start("h1"), "s1"),
],
Some(2),
),
(
"a result mints its message under its own attribution",
vec![
run(),
Event::tool_call_start("c1", "search"),
Event::tool_call_end("c1"),
tagged(Event::tool_call_result("m2", "c1", "ok"), "s1"),
start("m2"),
tagged(Event::text_message_end("m2"), "s2"),
],
Some(5),
),
(
"a reasoning block and its message share an owner",
vec![
run(),
tagged(Event::reasoning_start("r1"), "s1"),
tagged(Event::reasoning_message_start("r1"), "s2"),
],
Some(2),
),
(
"a text message and a reasoning message may share an id",
vec![
run(),
tagged(start("x"), "s1"),
tagged(Event::text_message_end("x"), "s1"),
tagged(Event::reasoning_message_start("x"), "s2"),
tagged(Event::reasoning_message_end("x"), "s2"),
done(),
],
None,
),
(
"an encrypted value names the owner of its tool call",
vec![
run(),
tagged(Event::tool_call_start("c1", "search"), "s1"),
tagged(Event::tool_call_end("c1"), "s1"),
tagged(value(ReasoningEncryptedValueSubtype::ToolCall, "c1"), "s2"),
],
Some(3),
),
(
"an encrypted value names the owner of its reasoning message",
vec![
run(),
tagged(Event::reasoning_message_start("r1"), "s1"),
tagged(Event::reasoning_message_end("r1"), "s1"),
tagged(value(ReasoningEncryptedValueSubtype::Message, "r1"), "s2"),
],
Some(3),
),
(
"an activity delta names the owner of the activity",
vec![
run(),
tagged(activity("a1", true), "s1"),
delta("a1"),
activity("a1", false),
tagged(delta("a1"), "s2"),
],
Some(4),
),
(
"a replacing snapshot re-owns the activity",
vec![
run(),
tagged(activity("a1", true), "s1"),
tagged(activity("a1", true), "s2"),
tagged(delta("a1"), "s2"),
done(),
],
None,
),
(
"steps are keyed by owner",
vec![
run(),
Event::step_started("plan"),
tagged(Event::step_started("plan"), "s1"),
tagged(Event::step_finished("plan"), "s1"),
tagged(Event::step_finished("plan"), "s2"),
],
Some(4),
),
(
"concurrent subagents interleave under their own tags",
vec![
run(),
Event::subagent_started("s1", "researcher"),
Event::subagent_started("s2", "researcher"),
tagged(start("m1"), "s1"),
tagged(start("m2"), "s2"),
tagged(Event::text_message_content("m1", "a"), "s1"),
tagged(Event::text_message_content("m2", "b"), "s2"),
tagged(Event::text_message_end("m2"), "s2"),
Event::subagent_finished_success("s2"),
tagged(Event::text_message_end("m1"), "s1"),
Event::subagent_finished_success("s1"),
done(),
],
None,
),
(
"a run may not finish with a subagent active",
vec![run(), Event::subagent_started("s1", "researcher"), done()],
Some(2),
),
]
}
#[test]
fn every_clause_of_the_attribution_rule_holds_at_both_ends() {
for policy in [Policy::PRODUCER, Policy::CONSUMER] {
for (name, events, expected) in ownership() {
let mut rules = Ordering::new(policy);
let rejected = events
.iter()
.position(|event| rules.observe(event).is_err());
assert_eq!(rejected, expected, "{policy:?}, {name}: {events:?}");
}
}
}
}