#![cfg(test)]
use std::error::Error;
use std::future::{Future, pending};
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll, Waker};
use std::time::Duration;
use crate::cancel::Token;
use super::event::ToolEmitter;
use super::hook::{
self, AfterToolCommitScope, CallbackResources, Emitter, HookResult, StepSnapshot,
ToolCallDecision, ToolCallScope, ToolHooks, ToolResultDecision, ToolResultScope,
};
use super::model::{Info, RequestPlan};
use super::session::Session;
use super::tool::{
self, CallDisposition, Descriptor, ExecutionError, Executor, ExecutorError, ExposeAll,
Invocation, MappingError, StepToolSnapshot,
};
use super::tool_batch::ErrorOrigin;
use super::tool_batch_prepare::{ToolBatchPreparePlan, prepare_tool_calls};
use super::tool_batch_route::{RoutedToolBatch, ToolRouteBinding, route_and_execute};
use crate::audit;
use crate::opaque::Payload;
use crate::state::run_state::{self};
use crate::transcript::item::{Item, ModelVisibility, System};
use crate::transcript::store::{Snapshot, Version};
use crate::value::lifecycle::Coord;
use crate::value::tool_call::{self, Id, ToolCall, ToolResult};
fn assert_pending_once<F: Future + ?Sized>(future: Pin<&mut F>) {
let mut context = Context::from_waker(Waker::noop());
assert!(matches!(future.poll(&mut context), Poll::Pending));
}
struct NoopHookEmitter;
impl Emitter for NoopHookEmitter {
fn emit_audit(&self, _event: audit::Event) {}
fn emit_error(&self, _error: Arc<dyn Error + Send + Sync>) {}
}
struct PrepareHook;
impl ToolHooks for PrepareHook {
fn before_tool_call(&self, scope: &ToolCallScope<'_>) -> HookResult<ToolCallDecision> {
Ok(match scope.call().id().as_str() {
"settled" => {
ToolCallDecision::ShortCircuit(ToolResult::success_text("settled".to_owned()))
}
"rewrite" => ToolCallDecision::Modify(call("spoofed", "routed")),
_ => ToolCallDecision::Continue,
})
}
fn before_tool_result(&self, _scope: &ToolResultScope<'_>) -> HookResult<ToolResultDecision> {
Ok(ToolResultDecision::Continue)
}
fn after_tool_commit(&self, _scope: &AfterToolCommitScope<'_>) -> Vec<Item> {
Vec::new()
}
}
fn call(id: &str, name: &str) -> ToolCall {
ToolCall::new(
Id::new(id.to_owned()),
tool_call::Name::new(name.to_owned()),
Payload::copy_from_slice(b"{}"),
)
}
fn transcript() -> Snapshot {
Snapshot::new(
Version::ZERO,
vec![Item::System(System::new(
"step".to_owned(),
"top".to_owned(),
ModelVisibility::Visible,
))],
)
}
fn local(name: &str) -> tool::Name {
tool::Name::local(name.to_owned()).unwrap()
}
fn descriptor(name: &str) -> Descriptor {
Descriptor::new(
local(name),
"test tool".to_owned(),
Payload::copy_from_slice(b"{}"),
)
}
fn tool_snapshot(registrations: Vec<tool::Registration>) -> Arc<StepToolSnapshot> {
let mut builder = tool::RegistryBuilder::new();
for registration in registrations {
builder.register(registration);
}
Arc::new(
builder
.build()
.unwrap()
.resolve_step_without_providers(transcript(), &Token::new(), &ExposeAll)
.unwrap()
.into_snapshot(),
)
}
fn prepare(calls: Vec<ToolCall>) -> ToolBatchPreparePlan {
let registry = {
let mut builder = hook::RegistryBuilder::new();
builder.register(hook::Registration::new("prepare".into()).tool(Arc::new(PrepareHook)));
builder.build_runtime().unwrap()
};
let hook_tools = tool::RegistryBuilder::new()
.build()
.unwrap()
.resolve_step_without_providers(transcript(), &Token::new(), &ExposeAll)
.unwrap()
.into_snapshot();
let snapshot = StepSnapshot::freeze(
Info::new("model".into(), 1024).with_tools(true),
RequestPlan::new(),
hook_tools,
);
let session = Session::new("session".into(), None);
let reader = session.state();
let coord = Coord::at_run("session".into(), "run".into()).unwrap();
let state = run_state::Store::new();
let emitter = NoopHookEmitter;
let resources = [CallbackResources::new(
&coord, &state, &reader, None, &emitter,
)];
prepare_tool_calls(calls, ®istry, &resources, snapshot, &Token::new()).unwrap()
}
type ExecutionObservation = (String, String, Option<String>);
struct RecordingExecutor {
calls: Arc<Mutex<Vec<ExecutionObservation>>>,
executions: Arc<AtomicUsize>,
}
impl Executor for RecordingExecutor {
type Output = String;
async fn execute(
&self,
invocation: &Invocation,
call: &ToolCall,
_cancellation: Token,
) -> Result<Self::Output, ExecutorError> {
self.executions.fetch_add(1, Ordering::SeqCst);
self.calls.lock().unwrap().push((
call.id().as_str().to_owned(),
call.name().as_str().to_owned(),
invocation.session_id().map(str::to_owned),
));
assert_eq!(invocation.call_id().as_str(), call.id().as_str());
assert_eq!(invocation.transcript().items().len(), 1);
Ok(call.name().as_str().to_owned())
}
}
fn recording_registration(name: &str, executor: &Arc<RecordingExecutor>) -> tool::Registration {
tool::Registration::client(
descriptor(name),
Arc::clone(executor),
Arc::new(|output: String| Ok(ToolResult::success_text(output))),
)
}
fn text(result: &ToolResult) -> String {
result.output().unwrap().user_view().unwrap().to_owned()
}
trait AmbiguousIfClone<Marker> {
fn assert_not_clone() {}
}
impl<T: ?Sized> AmbiguousIfClone<()> for T {}
struct CloneMarker;
impl<T: Clone> AmbiguousIfClone<CloneMarker> for T {}
#[test]
fn routed_batch_is_a_non_clone_linear_typestate() {
<RoutedToolBatch as AmbiguousIfClone<_>>::assert_not_clone();
}
#[tokio::test(flavor = "current_thread")]
async fn mixed_settled_and_executed_calls_keep_rewrite_correlation_and_source_order() {
let executions = Arc::new(AtomicUsize::new(0));
let observed = Arc::new(Mutex::new(Vec::new()));
let executor = Arc::new(RecordingExecutor {
calls: Arc::clone(&observed),
executions: Arc::clone(&executions),
});
let snapshot = tool_snapshot(vec![
recording_registration("first", &executor),
recording_registration("routed", &executor),
recording_registration("last", &executor),
]);
let plan = prepare(vec![
call("first-id", "first"),
call("settled", "never-bound"),
call("rewrite", "original"),
call("last-id", "last"),
]);
let bound = Arc::new(Mutex::new(Vec::new()));
let binder_log = Arc::clone(&bound);
let batch = route_and_execute(
plan,
snapshot,
Some("session-42".to_owned()),
&mut move |effective| {
binder_log
.lock()
.unwrap()
.push(effective.name().as_str().to_owned());
ToolRouteBinding::new(ToolEmitter::closed())
},
&Token::new(),
)
.await
.unwrap();
assert_eq!(*bound.lock().unwrap(), ["first", "routed", "last"]);
assert_eq!(executions.load(Ordering::SeqCst), 3);
let records = batch.records();
assert_eq!(records.len(), 4);
assert_eq!(
records
.iter()
.map(|record| record.original_call().id().as_str())
.collect::<Vec<_>>(),
["first-id", "settled", "rewrite", "last-id"]
);
assert_eq!(text(records[0].outcome().result()), "first");
assert_eq!(text(records[1].outcome().result()), "settled");
assert_eq!(
records[1].outcome().disposition(),
CallDisposition::ShortCircuited
);
assert_eq!(records[2].original_call().name().as_str(), "original");
assert_eq!(records[2].effective_call().name().as_str(), "routed");
assert_eq!(
records[2].original_call().id().as_str(),
records[2].effective_call().id().as_str()
);
assert_eq!(
records[2].outcome().disposition(),
CallDisposition::Executed
);
assert_eq!(text(records[2].outcome().result()), "routed");
assert_eq!(
*observed.lock().unwrap(),
[
(
"first-id".to_owned(),
"first".to_owned(),
Some("session-42".to_owned())
),
(
"rewrite".to_owned(),
"routed".to_owned(),
Some("session-42".to_owned())
),
(
"last-id".to_owned(),
"last".to_owned(),
Some("session-42".to_owned())
),
]
);
}
struct ImmediateExecutor;
impl Executor for ImmediateExecutor {
type Output = String;
async fn execute(
&self,
_invocation: &Invocation,
_call: &ToolCall,
_cancellation: Token,
) -> Result<Self::Output, ExecutorError> {
Ok("unmappable".to_owned())
}
}
struct PendingExecutor {
started: Arc<AtomicUsize>,
dropped: Arc<AtomicUsize>,
}
struct PendingGuard(Arc<AtomicUsize>);
impl Drop for PendingGuard {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::SeqCst);
}
}
impl Executor for PendingExecutor {
type Output = String;
async fn execute(
&self,
_invocation: &Invocation,
_call: &ToolCall,
_cancellation: Token,
) -> Result<Self::Output, ExecutorError> {
self.started.fetch_add(1, Ordering::SeqCst);
let _guard = PendingGuard(Arc::clone(&self.dropped));
pending().await
}
}
fn fatal_registration(name: &str) -> tool::Registration {
tool::Registration::client(
descriptor(name),
Arc::new(ImmediateExecutor),
Arc::new(|_output: String| Err(MappingError::Protocol("fatal mapper".to_owned()))),
)
}
fn pending_registration(
name: &str,
started: &Arc<AtomicUsize>,
dropped: &Arc<AtomicUsize>,
) -> tool::Registration {
tool::Registration::client(
descriptor(name),
Arc::new(PendingExecutor {
started: Arc::clone(started),
dropped: Arc::clone(dropped),
}),
Arc::new(|output: String| Ok(ToolResult::success_text(output))),
)
}
#[tokio::test(flavor = "current_thread")]
async fn execute_fatal_joins_started_siblings_and_returns_no_routed_typestate() {
let started = Arc::new(AtomicUsize::new(0));
let dropped = Arc::new(AtomicUsize::new(0));
let snapshot = tool_snapshot(vec![
fatal_registration("fatal"),
pending_registration("pending", &started, &dropped),
]);
let result = route_and_execute(
prepare(vec![
call("fatal-id", "fatal"),
call("pending-id", "pending"),
]),
snapshot,
None,
&mut |_effective| ToolRouteBinding::new(ToolEmitter::closed()),
&Token::new(),
)
.await;
let error = result
.err()
.expect("expected the fatal execution failure without records");
let failure = error.execution();
assert_eq!(failure.origin(), ErrorOrigin::Slot(0));
assert_eq!(
failure.execution(),
&ExecutionError::Mapping(MappingError::Protocol("fatal mapper".to_owned()))
);
assert_eq!(started.load(Ordering::SeqCst), 1);
assert_eq!(dropped.load(Ordering::SeqCst), 1);
}
#[tokio::test(flavor = "current_thread")]
async fn pre_cancel_binds_and_executes_nothing() {
let executions = Arc::new(AtomicUsize::new(0));
let executor = Arc::new(RecordingExecutor {
calls: Arc::new(Mutex::new(Vec::new())),
executions: Arc::clone(&executions),
});
let snapshot = tool_snapshot(vec![recording_registration("first", &executor)]);
let cancellation = Token::new();
cancellation.cancel();
let bind_count = Arc::new(AtomicUsize::new(0));
let binder_count = Arc::clone(&bind_count);
let result = route_and_execute(
prepare(vec![call("first-id", "first")]),
snapshot,
None,
&mut move |_effective| {
binder_count.fetch_add(1, Ordering::SeqCst);
ToolRouteBinding::new(ToolEmitter::closed())
},
&cancellation,
)
.await;
match result {
Err(error) => {
let failure = error.execution();
assert_eq!(failure.origin(), ErrorOrigin::Batch);
assert_eq!(failure.execution(), &ExecutionError::Cancelled);
}
_ => panic!("expected pre-routing cancellation without records"),
}
assert_eq!(bind_count.load(Ordering::SeqCst), 0);
assert_eq!(executions.load(Ordering::SeqCst), 0);
}
#[tokio::test(flavor = "current_thread")]
async fn midflight_cancel_returns_only_after_owned_execution_is_dropped() {
let started = Arc::new(AtomicUsize::new(0));
let dropped = Arc::new(AtomicUsize::new(0));
let snapshot = tool_snapshot(vec![pending_registration("pending", &started, &dropped)]);
let cancellation = Token::new();
let mut binder = |_effective: &ToolCall| ToolRouteBinding::new(ToolEmitter::closed());
let mut route = Box::pin(route_and_execute(
prepare(vec![call("pending-id", "pending")]),
snapshot,
None,
&mut binder,
&cancellation,
));
assert_pending_once(route.as_mut());
assert_eq!(started.load(Ordering::SeqCst), 1);
cancellation.cancel();
let result = tokio::time::timeout(Duration::from_secs(1), route)
.await
.unwrap();
assert!(matches!(
result,
Err(ref error)
if error.execution().execution() == &ExecutionError::Cancelled
));
assert_eq!(dropped.load(Ordering::SeqCst), 1);
}
#[tokio::test(flavor = "current_thread")]
async fn dropping_route_future_cancel_drops_all_owned_work_without_detaching() {
let started = Arc::new(AtomicUsize::new(0));
let dropped = Arc::new(AtomicUsize::new(0));
let snapshot = tool_snapshot(vec![pending_registration("pending", &started, &dropped)]);
let cancellation = Token::new();
let mut binder = |_effective: &ToolCall| ToolRouteBinding::new(ToolEmitter::closed());
let mut route = Box::pin(route_and_execute(
prepare(vec![call("pending-id", "pending")]),
snapshot,
None,
&mut binder,
&cancellation,
));
assert_pending_once(route.as_mut());
assert_eq!(started.load(Ordering::SeqCst), 1);
drop(route);
assert_eq!(dropped.load(Ordering::SeqCst), 1);
assert!(!cancellation.is_cancelled());
}