agent_runtime/runtime/cancellation_token.rs
1//! Shared cancellation signal and its operation-boundary semantics.
2
3use std::sync::{
4 Arc,
5 atomic::{AtomicBool, Ordering},
6};
7
8/// Shared cancellation signal for an agent run.
9///
10/// Cancellation is observed at operation boundaries. A model request or tool
11/// invocation that has already started is allowed to finish; its result is
12/// recorded before the runtime stops. No subsequent model or tool operation is
13/// started after cancellation is observed. This avoids abandoning a tool result
14/// when an external side effect may already have occurred.
15#[derive(Clone, Default)]
16pub struct CancellationToken {
17 cancelled: Arc<AtomicBool>,
18}
19
20impl CancellationToken {
21 pub fn new() -> Self {
22 Self::default()
23 }
24
25 /// Requests cancellation of the run.
26 pub fn cancel(&self) {
27 self.cancelled.store(true, Ordering::Relaxed);
28 }
29
30 /// Returns whether cancellation has been requested.
31 pub fn is_cancelled(&self) -> bool {
32 self.cancelled.load(Ordering::Relaxed)
33 }
34}