use std::collections::HashSet;
use std::future::Future;
use std::sync::Arc;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::error::{Result, RustyError};
use crate::llm::{ChatMessage, ChatModel, ChatResponse, ToolCall};
use crate::node::NodeOutput;
use crate::state::State;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InterceptPoint {
NodeRun,
ModelCall,
ToolCall,
}
impl std::fmt::Display for InterceptPoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
InterceptPoint::NodeRun => "node_run",
InterceptPoint::ModelCall => "model_call",
InterceptPoint::ToolCall => "tool_call",
};
f.write_str(s)
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Rejection {
pub middleware: String,
pub point: InterceptPoint,
pub reason: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub detail: Option<String>,
}
impl Rejection {
pub fn new(
middleware: impl Into<String>,
point: InterceptPoint,
reason: impl Into<String>,
) -> Self {
Self {
middleware: middleware.into(),
point,
reason: reason.into(),
detail: None,
}
}
pub fn with_detail(mut self, detail: impl Into<String>) -> Self {
self.detail = Some(detail.into());
self
}
}
impl std::fmt::Display for Rejection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"rejected by middleware `{}` at {}: {}",
self.middleware, self.point, self.reason
)
}
}
impl Rejection {
pub(crate) fn into_error(self) -> RustyError {
let message = self.to_string();
match self.point {
InterceptPoint::NodeRun => RustyError::Node(message),
InterceptPoint::ModelCall => RustyError::Llm(message),
InterceptPoint::ToolCall => RustyError::Tool(message),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Decision<R> {
Continue,
Reject(Rejection),
ShortCircuit(R),
}
#[derive(Debug, Clone)]
pub struct NodeCall {
thread_id: String,
node: String,
step: usize,
state: State,
}
impl NodeCall {
pub fn new(
thread_id: impl Into<String>,
node: impl Into<String>,
step: usize,
state: State,
) -> Self {
Self {
thread_id: thread_id.into(),
node: node.into(),
step,
state,
}
}
pub fn thread_id(&self) -> &str {
&self.thread_id
}
pub fn node(&self) -> &str {
&self.node
}
pub fn step(&self) -> usize {
self.step
}
pub fn state(&self) -> &State {
&self.state
}
pub fn state_mut(&mut self) -> &mut State {
&mut self.state
}
pub fn insert(&mut self, channel: impl Into<String>, value: Value) {
self.state.insert(channel, value);
}
}
#[derive(Debug, Clone)]
pub struct ModelCall {
thread_id: String,
node: String,
messages: Vec<ChatMessage>,
tools: Vec<Value>,
}
impl ModelCall {
pub fn new(
thread_id: impl Into<String>,
node: impl Into<String>,
messages: Vec<ChatMessage>,
tools: Vec<Value>,
) -> Self {
Self {
thread_id: thread_id.into(),
node: node.into(),
messages,
tools,
}
}
pub fn thread_id(&self) -> &str {
&self.thread_id
}
pub fn node(&self) -> &str {
&self.node
}
pub fn messages(&self) -> &[ChatMessage] {
&self.messages
}
pub fn messages_mut(&mut self) -> &mut Vec<ChatMessage> {
&mut self.messages
}
pub fn tools(&self) -> &[Value] {
&self.tools
}
pub fn tools_mut(&mut self) -> &mut Vec<Value> {
&mut self.tools
}
}
#[derive(Debug, Clone)]
pub struct ToolInvocation {
thread_id: String,
node: String,
call: ToolCall,
}
impl ToolInvocation {
pub fn new(thread_id: impl Into<String>, node: impl Into<String>, call: ToolCall) -> Self {
Self {
thread_id: thread_id.into(),
node: node.into(),
call,
}
}
pub fn thread_id(&self) -> &str {
&self.thread_id
}
pub fn node(&self) -> &str {
&self.node
}
pub fn call(&self) -> &ToolCall {
&self.call
}
pub fn call_mut(&mut self) -> &mut ToolCall {
&mut self.call
}
pub fn id(&self) -> &str {
&self.call.id
}
pub fn name(&self) -> &str {
&self.call.name
}
pub fn arguments(&self) -> &Value {
&self.call.arguments
}
pub fn set_arguments(&mut self, arguments: Value) {
self.call.arguments = arguments;
}
}
#[async_trait]
pub trait Middleware: Send + Sync {
fn name(&self) -> &str;
async fn before_node(&self, call: &mut NodeCall) -> Decision<NodeOutput> {
let _ = call;
Decision::Continue
}
async fn after_node(&self, call: &NodeCall, output: &mut NodeOutput) -> Decision<NodeOutput> {
let _ = (call, output);
Decision::Continue
}
async fn before_model(&self, call: &mut ModelCall) -> Decision<ChatResponse> {
let _ = call;
Decision::Continue
}
async fn after_model(
&self,
call: &ModelCall,
response: &mut ChatResponse,
) -> Decision<ChatResponse> {
let _ = (call, response);
Decision::Continue
}
async fn before_tool(&self, call: &mut ToolInvocation) -> Decision<Value> {
let _ = call;
Decision::Continue
}
async fn after_tool(&self, call: &ToolInvocation, result: &mut Value) -> Decision<Value> {
let _ = (call, result);
Decision::Continue
}
}
#[derive(Clone, Default)]
pub struct MiddlewareChain {
layers: Vec<Arc<dyn Middleware>>,
}
impl std::fmt::Debug for MiddlewareChain {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MiddlewareChain")
.field(
"layers",
&self.layers.iter().map(|l| l.name()).collect::<Vec<_>>(),
)
.finish()
}
}
impl MiddlewareChain {
pub fn new() -> Self {
Self::default()
}
pub fn layer<M: Middleware + 'static>(mut self, middleware: M) -> Self {
self.push(Arc::new(middleware));
self
}
pub fn push(&mut self, middleware: Arc<dyn Middleware>) -> &mut Self {
self.layers.push(middleware);
self
}
pub fn is_empty(&self) -> bool {
self.layers.is_empty()
}
pub fn len(&self) -> usize {
self.layers.len()
}
pub fn names(&self) -> impl Iterator<Item = &str> {
self.layers.iter().map(|l| l.name())
}
pub fn layers(&self) -> &[Arc<dyn Middleware>] {
&self.layers
}
pub async fn run_node<F, Fut>(&self, call: &mut NodeCall, op: F) -> Result<NodeOutput>
where
F: FnOnce(&NodeCall) -> Fut + Send,
Fut: Future<Output = Result<NodeOutput>> + Send,
{
let mut entered = 0;
let mut substitute = None;
for layer in &self.layers {
match layer.before_node(call).await {
Decision::Continue => entered += 1,
Decision::Reject(rejection) => return Err(rejection.into_error()),
Decision::ShortCircuit(output) => {
substitute = Some(output);
break;
}
}
}
let mut result = match substitute {
Some(output) => output,
None => {
entered = self.layers.len();
op(call).await?
}
};
for layer in self.layers[..entered].iter().rev() {
match layer.after_node(call, &mut result).await {
Decision::Continue => {}
Decision::Reject(rejection) => return Err(rejection.into_error()),
Decision::ShortCircuit(output) => {
result = output;
break;
}
}
}
Ok(result)
}
pub async fn run_model<F, Fut>(&self, call: &mut ModelCall, op: F) -> Result<ChatResponse>
where
F: FnOnce(&ModelCall) -> Fut + Send,
Fut: Future<Output = Result<ChatResponse>> + Send,
{
let mut entered = 0;
let mut substitute = None;
for layer in &self.layers {
match layer.before_model(call).await {
Decision::Continue => entered += 1,
Decision::Reject(rejection) => return Err(rejection.into_error()),
Decision::ShortCircuit(response) => {
substitute = Some(response);
break;
}
}
}
let mut result = match substitute {
Some(response) => response,
None => {
entered = self.layers.len();
op(call).await?
}
};
for layer in self.layers[..entered].iter().rev() {
match layer.after_model(call, &mut result).await {
Decision::Continue => {}
Decision::Reject(rejection) => return Err(rejection.into_error()),
Decision::ShortCircuit(response) => {
result = response;
break;
}
}
}
Ok(result)
}
pub async fn run_tool<F, Fut>(&self, call: &mut ToolInvocation, op: F) -> Result<Value>
where
F: FnOnce(&ToolInvocation) -> Fut + Send,
Fut: Future<Output = Result<Value>> + Send,
{
let mut entered = 0;
let mut substitute = None;
for layer in &self.layers {
match layer.before_tool(call).await {
Decision::Continue => entered += 1,
Decision::Reject(rejection) => return Err(rejection.into_error()),
Decision::ShortCircuit(value) => {
substitute = Some(value);
break;
}
}
}
let mut result = match substitute {
Some(value) => value,
None => {
entered = self.layers.len();
op(call).await?
}
};
for layer in self.layers[..entered].iter().rev() {
match layer.after_tool(call, &mut result).await {
Decision::Continue => {}
Decision::Reject(rejection) => return Err(rejection.into_error()),
Decision::ShortCircuit(value) => {
result = value;
break;
}
}
}
Ok(result)
}
}
#[derive(Clone)]
pub struct MiddlewareChatModel {
inner: Arc<dyn ChatModel>,
chain: MiddlewareChain,
thread_id: String,
node: String,
}
impl MiddlewareChatModel {
pub fn new(inner: Arc<dyn ChatModel>, chain: MiddlewareChain) -> Self {
Self {
inner,
chain,
thread_id: String::new(),
node: String::new(),
}
}
pub fn thread(mut self, thread_id: impl Into<String>) -> Self {
self.thread_id = thread_id.into();
self
}
pub fn node(mut self, node: impl Into<String>) -> Self {
self.node = node.into();
self
}
}
#[async_trait]
impl ChatModel for MiddlewareChatModel {
async fn chat(&self, messages: &[ChatMessage], tools: &[Value]) -> Result<ChatResponse> {
if self.chain.is_empty() {
return self.inner.chat(messages, tools).await;
}
let mut call = ModelCall::new(
self.thread_id.clone(),
self.node.clone(),
messages.to_vec(),
tools.to_vec(),
);
self.chain
.run_model(&mut call, |call| {
let inner = Arc::clone(&self.inner);
let messages = call.messages().to_vec();
let tools = call.tools().to_vec();
async move { inner.chat(&messages, &tools).await }
})
.await
}
fn effect(&self) -> crate::record::Effect {
self.inner.effect()
}
}
#[derive(Debug, Default)]
pub struct RequestLogger;
impl RequestLogger {
pub fn new() -> Self {
Self
}
}
#[async_trait]
impl Middleware for RequestLogger {
fn name(&self) -> &str {
"request_logger"
}
async fn before_node(&self, call: &mut NodeCall) -> Decision<NodeOutput> {
tracing::info!(
thread_id = %call.thread_id(),
node = %call.node(),
step = call.step(),
"middleware: node run started"
);
Decision::Continue
}
async fn after_node(&self, call: &NodeCall, output: &mut NodeOutput) -> Decision<NodeOutput> {
tracing::info!(
thread_id = %call.thread_id(),
node = %call.node(),
step = call.step(),
channels = output.updates.len(),
"middleware: node run completed"
);
Decision::Continue
}
async fn before_model(&self, call: &mut ModelCall) -> Decision<ChatResponse> {
tracing::info!(
thread_id = %call.thread_id(),
node = %call.node(),
messages = call.messages().len(),
tools = call.tools().len(),
"middleware: model call started"
);
Decision::Continue
}
async fn after_model(
&self,
call: &ModelCall,
response: &mut ChatResponse,
) -> Decision<ChatResponse> {
tracing::info!(
thread_id = %call.thread_id(),
node = %call.node(),
total_tokens = response.usage.map(|u| u.total_tokens),
"middleware: model call completed"
);
Decision::Continue
}
async fn before_tool(&self, call: &mut ToolInvocation) -> Decision<Value> {
tracing::info!(
thread_id = %call.thread_id(),
node = %call.node(),
tool = %call.name(),
call_id = %call.id(),
"middleware: tool call started"
);
Decision::Continue
}
async fn after_tool(&self, call: &ToolInvocation, result: &mut Value) -> Decision<Value> {
let _ = result;
tracing::info!(
thread_id = %call.thread_id(),
node = %call.node(),
tool = %call.name(),
call_id = %call.id(),
"middleware: tool call completed"
);
Decision::Continue
}
}
#[derive(Debug, Clone)]
pub struct ToolCallBlocklist {
blocked: HashSet<String>,
reason: String,
}
impl ToolCallBlocklist {
pub fn new<I, S>(tools: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self {
blocked: tools.into_iter().map(Into::into).collect(),
reason: "tool_blocked".to_owned(),
}
}
pub fn with_reason(mut self, reason: impl Into<String>) -> Self {
self.reason = reason.into();
self
}
pub fn contains(&self, tool: &str) -> bool {
self.blocked.contains(tool)
}
}
#[async_trait]
impl Middleware for ToolCallBlocklist {
fn name(&self) -> &str {
"tool_call_blocklist"
}
async fn before_tool(&self, call: &mut ToolInvocation) -> Decision<Value> {
if self.blocked.contains(call.name()) {
Decision::Reject(
Rejection::new(self.name(), InterceptPoint::ToolCall, self.reason.clone())
.with_detail(format!("tool `{}` is blocked by policy", call.name())),
)
} else {
Decision::Continue
}
}
}
pub fn instantiate_composition(
layers: &[crate::learn::MiddlewareLayerConfig],
) -> Result<MiddlewareChain> {
#[derive(Deserialize)]
struct BlocklistConfig {
blocked: Vec<String>,
#[serde(default)]
reason: Option<String>,
}
let mut chain = MiddlewareChain::new();
for entry in layers {
match entry.layer.as_str() {
"request_logger" => {
if entry.config.is_some() {
return Err(RustyError::Graph(
"middleware layer `request_logger` takes no config payload".to_owned(),
));
}
chain.push(Arc::new(RequestLogger::new()));
}
"tool_call_blocklist" => {
let config = entry.config.as_ref().ok_or_else(|| {
RustyError::Graph(
"middleware layer `tool_call_blocklist` requires a config payload: \
{\"blocked\": [\"tool.name\"], \"reason\"?}"
.to_owned(),
)
})?;
let parsed: BlocklistConfig =
serde_json::from_value(config.clone()).map_err(|e| {
RustyError::Graph(format!(
"middleware layer `tool_call_blocklist` config is malformed: {e}"
))
})?;
let mut layer = ToolCallBlocklist::new(parsed.blocked);
if let Some(reason) = parsed.reason {
layer = layer.with_reason(reason);
}
chain.push(Arc::new(layer));
}
other => {
return Err(RustyError::Graph(format!(
"unknown middleware layer `{other}`: the compiled-in vocabulary is \
[request_logger, tool_call_blocklist]"
)));
}
}
}
Ok(chain)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::learn::MiddlewareLayerConfig;
use serde_json::json;
use std::sync::Mutex;
#[derive(Clone, Default)]
struct Trace(Arc<Mutex<Vec<String>>>);
impl Trace {
fn record(&self, entry: impl Into<String>) {
self.0.lock().expect("trace lock").push(entry.into());
}
fn entries(&self) -> Vec<String> {
self.0.lock().expect("trace lock").clone()
}
}
struct Recorder {
id: &'static str,
trace: Trace,
}
#[async_trait]
impl Middleware for Recorder {
fn name(&self) -> &str {
self.id
}
async fn before_node(&self, call: &mut NodeCall) -> Decision<NodeOutput> {
self.trace
.record(format!("{}:before:{}", self.id, call.node()));
Decision::Continue
}
async fn after_node(
&self,
call: &NodeCall,
_output: &mut NodeOutput,
) -> Decision<NodeOutput> {
self.trace
.record(format!("{}:after:{}", self.id, call.node()));
Decision::Continue
}
}
#[tokio::test]
async fn empty_chain_passes_through() {
let chain = MiddlewareChain::new();
assert!(chain.is_empty());
let mut call = NodeCall::new("t-1", "node-a", 0, State::new());
let result = chain
.run_node(&mut call, |_call| async {
Ok(NodeOutput::update("x", json!(1)))
})
.await
.unwrap();
assert_eq!(result.updates.get("x"), Some(&json!(1)));
}
#[tokio::test]
async fn before_hooks_run_in_registration_order_after_hooks_in_reverse() {
let trace = Trace::default();
let chain = MiddlewareChain::new()
.layer(Recorder {
id: "L1",
trace: trace.clone(),
})
.layer(Recorder {
id: "L2",
trace: trace.clone(),
})
.layer(Recorder {
id: "L3",
trace: trace.clone(),
});
assert_eq!(chain.len(), 3);
assert_eq!(chain.names().collect::<Vec<_>>(), ["L1", "L2", "L3"]);
let op_trace = trace.clone();
let mut call = NodeCall::new("t-1", "node-a", 0, State::new());
let result = chain
.run_node(&mut call, |_call| {
let op_trace = op_trace.clone();
async move {
op_trace.record("op");
Ok(NodeOutput::update("x", json!(1)))
}
})
.await
.unwrap();
assert_eq!(result.updates.get("x"), Some(&json!(1)));
assert_eq!(
trace.entries(),
vec![
"L1:before:node-a",
"L2:before:node-a",
"L3:before:node-a",
"op",
"L3:after:node-a",
"L2:after:node-a",
"L1:after:node-a",
]
);
}
struct Mutate;
#[async_trait]
impl Middleware for Mutate {
fn name(&self) -> &str {
"mutate"
}
async fn before_node(&self, call: &mut NodeCall) -> Decision<NodeOutput> {
call.insert("injected", json!(true));
Decision::Continue
}
async fn after_node(
&self,
_call: &NodeCall,
output: &mut NodeOutput,
) -> Decision<NodeOutput> {
output.updates.insert("x".into(), json!("rewritten"));
Decision::Continue
}
}
#[tokio::test]
async fn mutations_propagate_inward_and_outward() {
let chain = MiddlewareChain::new().layer(Mutate);
let mut call = NodeCall::new("t-1", "node-a", 0, State::new());
let result = chain
.run_node(&mut call, |call| {
assert_eq!(call.state().get("injected"), Some(&json!(true)));
async move { Ok(NodeOutput::update("x", json!("original"))) }
})
.await
.unwrap();
assert_eq!(result.updates.get("x"), Some(&json!("rewritten")));
}
struct PolicyReject {
reason: &'static str,
}
#[async_trait]
impl Middleware for PolicyReject {
fn name(&self) -> &str {
"policy_reject"
}
async fn before_node(&self, _call: &mut NodeCall) -> Decision<NodeOutput> {
Decision::Reject(
Rejection::new(self.name(), InterceptPoint::NodeRun, self.reason)
.with_detail("denied by test policy"),
)
}
}
#[tokio::test]
async fn reject_skips_operation_and_all_after_hooks() {
let trace = Trace::default();
let chain = MiddlewareChain::new()
.layer(Recorder {
id: "L1",
trace: trace.clone(),
})
.layer(PolicyReject { reason: "policy" })
.layer(Recorder {
id: "L3",
trace: trace.clone(),
});
let op_ran = Arc::new(std::sync::atomic::AtomicBool::new(false));
let op_flag = op_ran.clone();
let mut call = NodeCall::new("t-1", "node-b", 2, State::new());
let err = chain
.run_node(&mut call, |_call| {
let op_flag = op_flag.clone();
async move {
op_flag.store(true, std::sync::atomic::Ordering::SeqCst);
Ok(NodeOutput::empty())
}
})
.await
.unwrap_err();
match err {
RustyError::Node(message) => {
assert!(
message.contains("rejected by middleware `policy_reject` at node_run: policy"),
"got: {message}"
);
}
other => panic!("expected RustyError::Node, got {other:?}"),
}
assert!(!op_ran.load(std::sync::atomic::Ordering::SeqCst));
assert_eq!(trace.entries(), vec!["L1:before:node-b"]);
}
struct Skip;
#[async_trait]
impl Middleware for Skip {
fn name(&self) -> &str {
"skip"
}
async fn before_node(&self, call: &mut NodeCall) -> Decision<NodeOutput> {
if call.node() == "victim" {
Decision::ShortCircuit(NodeOutput::update("sub", json!("substitute")))
} else {
Decision::Continue
}
}
}
#[tokio::test]
async fn short_circuit_returns_substitute_and_unwinds_entered_layers() {
let trace = Trace::default();
let chain = MiddlewareChain::new()
.layer(Recorder {
id: "L1",
trace: trace.clone(),
})
.layer(Skip)
.layer(Recorder {
id: "L3",
trace: trace.clone(),
});
let op_ran = Arc::new(std::sync::atomic::AtomicBool::new(false));
let op_flag = op_ran.clone();
let mut call = NodeCall::new("t-1", "victim", 1, State::new());
let result = chain
.run_node(&mut call, |_call| {
let op_flag = op_flag.clone();
async move {
op_flag.store(true, std::sync::atomic::Ordering::SeqCst);
Ok(NodeOutput::empty())
}
})
.await
.unwrap();
assert_eq!(result.updates.get("sub"), Some(&json!("substitute")));
assert!(!op_ran.load(std::sync::atomic::Ordering::SeqCst));
assert_eq!(trace.entries(), vec!["L1:before:victim", "L1:after:victim"]);
}
struct Replace;
#[async_trait]
impl Middleware for Replace {
fn name(&self) -> &str {
"replace"
}
async fn after_node(
&self,
_call: &NodeCall,
_output: &mut NodeOutput,
) -> Decision<NodeOutput> {
Decision::ShortCircuit(NodeOutput::update("x", json!("replaced")))
}
}
#[tokio::test]
async fn after_hook_short_circuit_replaces_result_and_skips_outer_layers() {
let trace = Trace::default();
let chain = MiddlewareChain::new()
.layer(Recorder {
id: "L1",
trace: trace.clone(),
})
.layer(Replace);
let mut call = NodeCall::new("t-1", "node-a", 0, State::new());
let result = chain
.run_node(&mut call, |_call| async {
Ok(NodeOutput::update("x", json!("original")))
})
.await
.unwrap();
assert_eq!(result.updates.get("x"), Some(&json!("replaced")));
assert_eq!(trace.entries(), vec!["L1:before:node-a"]);
}
struct CountingModel {
seen: Arc<Mutex<Option<usize>>>,
}
#[async_trait]
impl ChatModel for CountingModel {
async fn chat(&self, messages: &[ChatMessage], _tools: &[Value]) -> Result<ChatResponse> {
*self.seen.lock().unwrap() = Some(messages.len());
Ok(ChatResponse {
message: ChatMessage::assistant(format!("saw {} messages", messages.len())),
model: None,
usage: None,
})
}
}
struct ModelMutate;
#[async_trait]
impl Middleware for ModelMutate {
fn name(&self) -> &str {
"model_mutate"
}
async fn before_model(&self, call: &mut ModelCall) -> Decision<ChatResponse> {
call.messages_mut().insert(0, ChatMessage::system("rules"));
Decision::Continue
}
async fn after_model(
&self,
_call: &ModelCall,
response: &mut ChatResponse,
) -> Decision<ChatResponse> {
response.message.content = Some("intercepted".into());
Decision::Continue
}
}
#[tokio::test]
async fn model_chain_mutates_request_and_response() {
let seen = Arc::new(Mutex::new(None));
let model = MiddlewareChatModel::new(
Arc::new(CountingModel { seen: seen.clone() }),
MiddlewareChain::new().layer(ModelMutate),
)
.thread("t-1")
.node("agent");
let response = model.chat(&[ChatMessage::user("hi")], &[]).await.unwrap();
assert_eq!(*seen.lock().unwrap(), Some(2));
assert_eq!(response.message.content.as_deref(), Some("intercepted"));
}
struct ModelReject;
#[async_trait]
impl Middleware for ModelReject {
fn name(&self) -> &str {
"model_reject"
}
async fn before_model(&self, _call: &mut ModelCall) -> Decision<ChatResponse> {
Decision::Reject(Rejection::new(
self.name(),
InterceptPoint::ModelCall,
"model_blocked",
))
}
}
#[tokio::test]
async fn model_reject_skips_inner_model() {
let seen = Arc::new(Mutex::new(None));
let model = MiddlewareChatModel::new(
Arc::new(CountingModel { seen: seen.clone() }),
MiddlewareChain::new().layer(ModelReject),
);
let err = model.chat(&[], &[]).await.unwrap_err();
match err {
RustyError::Llm(message) => {
assert!(
message.contains(
"rejected by middleware `model_reject` at model_call: model_blocked"
),
"got: {message}"
);
}
other => panic!("expected RustyError::Llm, got {other:?}"),
}
assert_eq!(*seen.lock().unwrap(), None);
}
struct ModelCache;
#[async_trait]
impl Middleware for ModelCache {
fn name(&self) -> &str {
"model_cache"
}
async fn before_model(&self, _call: &mut ModelCall) -> Decision<ChatResponse> {
Decision::ShortCircuit(ChatResponse {
message: ChatMessage::assistant("cached"),
model: None,
usage: None,
})
}
}
#[tokio::test]
async fn model_short_circuit_returns_substitute_response() {
let seen = Arc::new(Mutex::new(None));
let model = MiddlewareChatModel::new(
Arc::new(CountingModel { seen: seen.clone() }),
MiddlewareChain::new().layer(ModelCache),
);
let response = model.chat(&[], &[]).await.unwrap();
assert_eq!(response.message.content.as_deref(), Some("cached"));
assert_eq!(*seen.lock().unwrap(), None);
}
#[tokio::test]
async fn model_wrapper_with_empty_chain_delegates() {
let seen = Arc::new(Mutex::new(None));
let model = MiddlewareChatModel::new(
Arc::new(CountingModel { seen: seen.clone() }),
MiddlewareChain::new(),
);
let response = model.chat(&[ChatMessage::user("hi")], &[]).await.unwrap();
assert_eq!(response.message.content.as_deref(), Some("saw 1 messages"));
assert_eq!(*seen.lock().unwrap(), Some(1));
}
struct EchoTool;
#[async_trait]
impl crate::tool::Tool for EchoTool {
fn name(&self) -> &str {
"echo"
}
fn description(&self) -> &str {
"Echoes its `text` argument."
}
fn parameters_schema(&self) -> Value {
json!({"type": "object", "properties": {"text": {"type": "string"}}})
}
async fn call(&self, args: Value) -> Result<Value> {
Ok(json!(args.get("text").cloned().unwrap_or(Value::Null)))
}
}
struct DangerTool;
#[async_trait]
impl crate::tool::Tool for DangerTool {
fn name(&self) -> &str {
"danger"
}
fn description(&self) -> &str {
"Must never run."
}
fn parameters_schema(&self) -> Value {
json!({"type": "object"})
}
async fn call(&self, _args: Value) -> Result<Value> {
panic!("blocklisted tool executed");
}
}
fn echo_registry() -> crate::tool::ToolRegistry {
let mut registry = crate::tool::ToolRegistry::new();
registry.register(EchoTool);
registry.register(DangerTool);
registry
}
#[tokio::test]
async fn blocklist_rejects_blocked_tool_and_passes_allowed() {
let executor = crate::tool::ToolExecutor::new(echo_registry())
.with_middleware(MiddlewareChain::new().layer(ToolCallBlocklist::new(["danger"])));
let calls = vec![
ToolCall::new("c1", "echo", json!({"text": "hello"})),
ToolCall::new("c2", "danger", json!({})),
];
let results = executor.execute_batch(&calls).await;
assert_eq!(results.len(), 2);
assert_eq!(results[0].content.as_deref(), Some("hello"));
let blocked = results[1].content.as_deref().unwrap();
assert!(blocked.starts_with("ERROR:"), "got: {blocked}");
assert!(blocked.contains("tool_call_blocklist"), "got: {blocked}");
assert!(blocked.contains("tool_blocked"), "got: {blocked}");
}
struct ArgsRewrite;
#[async_trait]
impl Middleware for ArgsRewrite {
fn name(&self) -> &str {
"args_rewrite"
}
async fn before_tool(&self, call: &mut ToolInvocation) -> Decision<Value> {
call.set_arguments(json!({"text": "mutated"}));
Decision::Continue
}
}
#[tokio::test]
async fn tool_argument_mutation_propagates_to_tool() {
let executor = crate::tool::ToolExecutor::new(echo_registry())
.with_middleware(MiddlewareChain::new().layer(ArgsRewrite));
let calls = vec![ToolCall::new("c1", "echo", json!({"text": "original"}))];
let results = executor.execute_batch(&calls).await;
assert_eq!(results[0].content.as_deref(), Some("mutated"));
}
struct ToolCache;
#[async_trait]
impl Middleware for ToolCache {
fn name(&self) -> &str {
"tool_cache"
}
async fn before_tool(&self, _call: &mut ToolInvocation) -> Decision<Value> {
Decision::ShortCircuit(json!("cached"))
}
}
#[tokio::test]
async fn tool_short_circuit_returns_substitute_result() {
let executor = crate::tool::ToolExecutor::new(echo_registry())
.with_middleware(MiddlewareChain::new().layer(ToolCache));
let calls = vec![ToolCall::new("c1", "echo", json!({"text": "ignored"}))];
let results = executor.execute_batch(&calls).await;
assert_eq!(results[0].content.as_deref(), Some("cached"));
}
struct ResultRewrite;
#[async_trait]
impl Middleware for ResultRewrite {
fn name(&self) -> &str {
"result_rewrite"
}
async fn after_tool(&self, _call: &ToolInvocation, result: &mut Value) -> Decision<Value> {
*result = json!("rewritten");
Decision::Continue
}
}
#[tokio::test]
async fn tool_result_mutation_propagates_to_caller() {
let executor = crate::tool::ToolExecutor::new(echo_registry())
.with_middleware(MiddlewareChain::new().layer(ResultRewrite));
let calls = vec![ToolCall::new("c1", "echo", json!({"text": "original"}))];
let results = executor.execute_batch(&calls).await;
assert_eq!(results[0].content.as_deref(), Some("rewritten"));
}
struct ToolRecorder {
id: &'static str,
trace: Trace,
}
#[async_trait]
impl Middleware for ToolRecorder {
fn name(&self) -> &str {
self.id
}
async fn before_tool(&self, call: &mut ToolInvocation) -> Decision<Value> {
self.trace
.record(format!("{}:before:{}", self.id, call.name()));
Decision::Continue
}
async fn after_tool(&self, call: &ToolInvocation, _result: &mut Value) -> Decision<Value> {
self.trace
.record(format!("{}:after:{}", self.id, call.name()));
Decision::Continue
}
}
#[tokio::test]
async fn tool_hooks_follow_onion_order() {
let trace = Trace::default();
let executor = crate::tool::ToolExecutor::new(echo_registry()).with_middleware(
MiddlewareChain::new()
.layer(ToolRecorder {
id: "L1",
trace: trace.clone(),
})
.layer(ToolRecorder {
id: "L2",
trace: trace.clone(),
}),
);
let calls = vec![ToolCall::new("c1", "echo", json!({"text": "hi"}))];
let results = executor.execute_batch(&calls).await;
assert_eq!(results[0].content.as_deref(), Some("hi"));
assert_eq!(
trace.entries(),
vec![
"L1:before:echo",
"L2:before:echo",
"L2:after:echo",
"L1:after:echo"
]
);
}
#[tokio::test]
async fn request_logger_passes_everything_through() {
let chain = MiddlewareChain::new().layer(RequestLogger::new());
let mut node_call = NodeCall::new("t-1", "node-a", 0, State::new());
let node_result = chain
.run_node(&mut node_call, |_call| async {
Ok(NodeOutput::update("x", json!(1)))
})
.await
.unwrap();
assert_eq!(node_result.updates.get("x"), Some(&json!(1)));
let mut model_call = ModelCall::new("t-1", "agent", vec![ChatMessage::user("hi")], vec![]);
let model_result = chain
.run_model(&mut model_call, |_call| async {
Ok(ChatResponse {
message: ChatMessage::assistant("hello"),
model: None,
usage: None,
})
})
.await
.unwrap();
assert_eq!(model_result.message.content.as_deref(), Some("hello"));
let mut tool_call =
ToolInvocation::new("t-1", "tools", ToolCall::new("c1", "echo", json!({})));
let tool_result = chain
.run_tool(&mut tool_call, |_call| async { Ok(json!("done")) })
.await
.unwrap();
assert_eq!(tool_result, json!("done"));
}
#[test]
fn rejection_serde_roundtrip_display_and_error_mapping() {
let rejection = Rejection::new(
"tool_call_blocklist",
InterceptPoint::ToolCall,
"tool_blocked",
)
.with_detail("tool `rm_rf` is blocked by policy");
let back: Rejection =
serde_json::from_str(&serde_json::to_string(&rejection).unwrap()).unwrap();
assert_eq!(rejection, back);
let msg = rejection.to_string();
assert!(msg.contains("tool_call_blocklist"), "got: {msg}");
assert!(msg.contains("tool_call"), "got: {msg}");
assert!(msg.contains("tool_blocked"), "got: {msg}");
assert_eq!(format!("{}", rejection.point), "tool_call");
for (point, expect) in [
(InterceptPoint::NodeRun, "node error"),
(InterceptPoint::ModelCall, "llm error"),
(InterceptPoint::ToolCall, "tool error"),
] {
let err = Rejection::new("m", point, "r").into_error();
assert!(
err.to_string().starts_with(expect),
"{point}: expected prefix `{expect}`, got: {err}"
);
assert!(err.to_string().contains("rejected by middleware `m`"));
}
}
#[test]
fn chain_debug_lists_layer_names() {
let chain = MiddlewareChain::new()
.layer(RequestLogger::new())
.layer(ToolCallBlocklist::new(["danger"]));
let debug = format!("{chain:?}");
assert!(debug.contains("request_logger"), "got: {debug}");
assert!(debug.contains("tool_call_blocklist"), "got: {debug}");
}
fn layer(name: &str, config: Option<Value>) -> MiddlewareLayerConfig {
MiddlewareLayerConfig {
layer: name.to_owned(),
config,
}
}
#[test]
fn instantiate_builds_the_journaled_order() {
let chain = instantiate_composition(&[
layer("request_logger", None),
layer(
"tool_call_blocklist",
Some(json!({"blocked": ["shell", "fs_write"], "reason": "policy_denied"})),
),
])
.unwrap();
assert_eq!(
chain.names().collect::<Vec<_>>(),
["request_logger", "tool_call_blocklist"]
);
let reversed = instantiate_composition(&[
layer("tool_call_blocklist", Some(json!({"blocked": ["shell"]}))),
layer("request_logger", None),
])
.unwrap();
assert_eq!(
reversed.names().collect::<Vec<_>>(),
["tool_call_blocklist", "request_logger"]
);
assert!(instantiate_composition(&[]).unwrap().is_empty());
}
#[tokio::test]
async fn instantiated_blocklist_enforces_its_config() {
let chain = instantiate_composition(&[layer(
"tool_call_blocklist",
Some(json!({"blocked": ["shell"]})),
)])
.unwrap();
let mut call = ToolInvocation::new(
"t-1",
"node-a",
ToolCall {
id: "c-1".into(),
name: "shell".into(),
arguments: json!({}),
},
);
let err = chain
.run_tool(&mut call, |_call| async { Ok(json!("ran")) })
.await
.unwrap_err();
assert!(
err.to_string().contains(
"rejected by middleware `tool_call_blocklist` at tool_call: tool_blocked"
),
"got: {err}"
);
}
#[test]
fn instantiate_refuses_what_the_vocabulary_cannot_honor() {
let err = instantiate_composition(&[layer("otel_tracer", None)]).unwrap_err();
assert!(err.to_string().contains("otel_tracer"), "got: {err}");
assert!(
err.to_string().contains("compiled-in vocabulary"),
"got: {err}"
);
let err =
instantiate_composition(&[layer("request_logger", Some(json!({"level": "debug"})))])
.unwrap_err();
assert!(err.to_string().contains("request_logger"), "got: {err}");
let err = instantiate_composition(&[layer("tool_call_blocklist", None)]).unwrap_err();
assert!(err.to_string().contains("requires a config"), "got: {err}");
let err = instantiate_composition(&[layer(
"tool_call_blocklist",
Some(json!({"blocked": "shell"})),
)])
.unwrap_err();
assert!(err.to_string().contains("malformed"), "got: {err}");
}
}