1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//! Public `EngineHandle` methods.
//!
//! The struct itself lives next door in `engine.rs` because two
//! construction sites (`Engine::new` and the test-only
//! `mock_engine_handle`) need access to its private mpsc channels.
//! The method surface — `send`, `cancel*`, `is_cancelled`,
//! `approve_tool_call` / `deny_tool_call` / `retry_tool_with_policy`,
//! `submit_user_input` / `cancel_user_input`, and `steer` — moves here
//! so the agent loop's mailbox API is reviewable on its own.
use anyhow::Result;
use tokio::sync::mpsc;
use super::approval::{ApprovalDecision, UserInputDecision};
use super::{
CancelReason, EngineHandle, LiveRuntimeAuthority, Op, RuntimePermissionAuthority,
UserInputResponse,
};
impl EngineHandle {
/// True when the caller must preflight a concrete provider client before
/// committing UI/runtime turn state. Test and embedding handles with an
/// injected model client return false because that client owns model I/O.
#[must_use]
pub(crate) fn client_preflight_required(&self) -> bool {
self.client_preflight_required
}
/// Send an operation to the engine
pub async fn send(&self, op: Op) -> Result<()> {
let authority = Self::change_mode_authority(&op);
let permit = self.tx_op.reserve().await?;
if let Some(authority) = authority {
self.publish_runtime_authority(authority);
}
permit.send(op);
Ok(())
}
/// Try to send an operation without blocking.
///
/// Returns `Err` if the channel is full or closed. Use this for
/// non-critical, refresh-type ops (e.g. `Op::ListSubAgents`) that can
/// safely be dropped and re-requested on the next drain cycle.
pub fn try_send(&self, op: Op) -> Result<()> {
let authority = Self::change_mode_authority(&op);
let result = self.tx_op.try_send(op);
// A full channel already guarantees that the engine will wake and
// drain an operation. Publish the typed authority anyway: the drain
// applies pending authority before handling that queued operation, so
// a posture edit never blocks behind refresh traffic. A closed
// channel has no engine left to observe the update.
if !matches!(&result, Err(mpsc::error::TrySendError::Closed(_)))
&& let Some(authority) = authority
{
self.publish_runtime_authority(authority);
}
result?;
Ok(())
}
fn change_mode_authority(op: &Op) -> Option<LiveRuntimeAuthority> {
let Op::ChangeMode {
mode,
allow_shell,
trust_mode,
auto_approve,
approval_mode,
configured_sandbox_mode,
} = op
else {
return None;
};
Some(LiveRuntimeAuthority::from_fields(
*mode,
*allow_shell,
*trust_mode,
*auto_approve,
*approval_mode,
configured_sandbox_mode.clone(),
))
}
fn publish_runtime_authority(&self, authority: LiveRuntimeAuthority) {
let mut state = self
.live_runtime_authority
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
state.revision = state.revision.wrapping_add(1).max(1);
state.authority = authority;
}
pub(crate) fn publish_turn_authority(
&self,
mode: crate::tui::app::AppMode,
allow_shell: bool,
trust_mode: bool,
auto_approve: bool,
approval_mode: crate::tui::approval::ApprovalMode,
configured_sandbox_mode: Option<String>,
) {
self.publish_runtime_authority(LiveRuntimeAuthority::from_fields(
mode,
allow_shell,
trust_mode,
auto_approve,
approval_mode,
configured_sandbox_mode,
));
}
/// Exact live permission authority for runtime approval and elevation
/// gates. This is the same typed state the active engine turn drains.
#[must_use]
pub(crate) fn runtime_permission_authority(&self) -> RuntimePermissionAuthority {
self.live_runtime_authority
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.authority
.permission_snapshot()
}
/// Reserve capacity for a runtime steer before it mutates durable state.
/// The owned permit lets the caller persist and dispatch synchronously,
/// without a cancellation point between those two operations.
pub(crate) async fn reserve_steer(&self) -> Result<mpsc::OwnedPermit<String>> {
Ok(self.tx_steer.clone().reserve_owned().await?)
}
/// Cancel the current request (user-initiated path — keeps the
/// public `cancel()` signature stable). Equivalent to
/// `cancel_with_reason(CancelReason::User)`.
pub fn cancel(&self) {
self.cancel_with_reason(CancelReason::User);
}
/// Cancel the current request and latch the reason so downstream
/// "request cancelled" error messages can name a cause.
pub fn cancel_with_reason(&self, reason: CancelReason) {
match self.cancel_reason.lock() {
Ok(mut slot) => *slot = Some(reason),
Err(poisoned) => *poisoned.into_inner() = Some(reason),
}
match self.cancel_token.lock() {
Ok(token) => token.cancel(),
Err(poisoned) => poisoned.into_inner().cancel(),
}
crate::retry_status::clear();
}
/// Check if a request is currently cancelled
#[must_use]
#[allow(dead_code)]
pub fn is_cancelled(&self) -> bool {
match self.cancel_token.lock() {
Ok(token) => token.is_cancelled(),
Err(poisoned) => poisoned.into_inner().is_cancelled(),
}
}
/// Pause or resume the current pausable command.
pub fn set_paused(&self, paused: bool) {
match self.shared_paused.lock() {
Ok(mut slot) => *slot = paused,
Err(poisoned) => *poisoned.into_inner() = paused,
}
}
/// Check whether the engine pause gate is set.
#[cfg(test)]
#[must_use]
pub fn is_paused(&self) -> bool {
match self.shared_paused.lock() {
Ok(slot) => *slot,
Err(poisoned) => *poisoned.into_inner(),
}
}
/// Approve a pending tool call
pub async fn approve_tool_call(&self, id: impl Into<String>) -> Result<()> {
self.tx_approval
.send(ApprovalDecision::Approved { id: id.into() })
.await?;
Ok(())
}
/// Deny a pending tool call
pub async fn deny_tool_call(&self, id: impl Into<String>) -> Result<()> {
self.tx_approval
.send(ApprovalDecision::Denied { id: id.into() })
.await?;
Ok(())
}
/// Retry a tool call with an elevated sandbox policy.
pub async fn retry_tool_with_policy(
&self,
id: impl Into<String>,
policy: crate::sandbox::SandboxPolicy,
) -> Result<()> {
self.tx_approval
.send(ApprovalDecision::RetryWithPolicy {
id: id.into(),
policy,
})
.await?;
Ok(())
}
/// Submit a response for request_user_input.
pub async fn submit_user_input(
&self,
id: impl Into<String>,
response: UserInputResponse,
) -> Result<()> {
self.tx_user_input
.send(UserInputDecision::Submitted {
id: id.into(),
response,
})
.await?;
Ok(())
}
/// Cancel a request_user_input prompt.
pub async fn cancel_user_input(&self, id: impl Into<String>) -> Result<()> {
self.tx_user_input
.send(UserInputDecision::Cancelled { id: id.into() })
.await?;
Ok(())
}
/// Steer an in-flight turn with additional user input.
pub async fn steer(&self, content: impl Into<String>) -> Result<()> {
self.tx_steer.send(content.into()).await?;
Ok(())
}
/// Request a snapshot of the current session state.
/// Returns the snapshot directly via a oneshot channel, avoiding
/// competition with the SSE event stream on the mpsc receiver.
pub async fn get_session_snapshot(&self) -> Result<crate::core::ops::SessionSnapshot> {
let (tx, rx) = tokio::sync::oneshot::channel();
let tx = std::sync::Arc::new(std::sync::Mutex::new(Some(tx)));
self.send(Op::GetSessionSnapshot { tx }).await?;
rx.await
.map_err(|_| anyhow::anyhow!("Engine dropped session snapshot oneshot"))
}
/// Request active provider request concurrency state.
pub async fn get_provider_runtime_status(
&self,
) -> Result<crate::core::ops::ProviderRuntimeStatus> {
let (tx, rx) = tokio::sync::oneshot::channel();
let tx = std::sync::Arc::new(std::sync::Mutex::new(Some(tx)));
self.send(Op::GetProviderRuntimeStatus { tx }).await?;
rx.await
.map_err(|_| anyhow::anyhow!("Engine dropped provider runtime status oneshot"))
}
/// Force the engine-owned MCP pool to reload and reconnect, returning a
/// snapshot from the exact live pool that supplies the next model turn.
pub async fn reload_mcp(
&self,
config_path: std::path::PathBuf,
) -> Result<crate::mcp::McpManagerSnapshot> {
let (tx, rx) = tokio::sync::oneshot::channel();
let tx = std::sync::Arc::new(std::sync::Mutex::new(Some(tx)));
self.send(Op::ReloadMcp { config_path, tx }).await?;
rx.await
.map_err(|_| anyhow::anyhow!("Engine dropped MCP reload oneshot"))?
.map_err(anyhow::Error::msg)
}
}