-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy pathapproval.rs
More file actions
348 lines (306 loc) · 10.7 KB
/
approval.rs
File metadata and controls
348 lines (306 loc) · 10.7 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//! Live approval service and broadcaster for the gateway.
use std::{borrow::Cow, sync::Arc};
use {
async_trait::async_trait,
serde_json::Value,
tracing::{info, warn},
};
use {
moltis_channels::ChannelReplyTarget,
moltis_sessions::metadata::SessionEntry,
moltis_tools::{
approval::{ApprovalDecision, ApprovalManager},
exec::ApprovalBroadcaster,
},
};
use crate::{
broadcast::{BroadcastOpts, broadcast},
services::{ExecApprovalService, ServiceResult},
state::GatewayState,
};
/// Live approval service backed by an `ApprovalManager`.
pub struct LiveExecApprovalService {
manager: Arc<ApprovalManager>,
}
impl LiveExecApprovalService {
pub fn new(manager: Arc<ApprovalManager>) -> Self {
Self { manager }
}
}
#[async_trait]
impl ExecApprovalService for LiveExecApprovalService {
async fn get(&self) -> ServiceResult {
Ok(serde_json::json!({
"mode": self.manager.mode,
"securityLevel": self.manager.security_level,
}))
}
async fn set(&self, _params: Value) -> ServiceResult {
// Config mutation not yet implemented.
Ok(serde_json::json!({}))
}
async fn node_get(&self, _params: Value) -> ServiceResult {
Ok(serde_json::json!({ "mode": self.manager.mode }))
}
async fn node_set(&self, _params: Value) -> ServiceResult {
Ok(serde_json::json!({}))
}
async fn request(&self, _params: Value) -> ServiceResult {
let requests = if let Some(session_key) = _params.get("sessionKey").and_then(|v| v.as_str())
{
self.manager.pending_requests_for_session(session_key).await
} else {
self.manager.pending_requests().await
};
let pending = requests
.iter()
.map(|request| request.id.clone())
.collect::<Vec<_>>();
Ok(serde_json::json!({
"pending": pending,
"requests": requests,
}))
}
async fn resolve(&self, params: Value) -> ServiceResult {
let id = params
.get("requestId")
.and_then(|v| v.as_str())
.ok_or_else(|| "missing 'requestId'".to_string())?;
let decision_str = params
.get("decision")
.and_then(|v| v.as_str())
.ok_or_else(|| "missing 'decision'".to_string())?;
let decision = match decision_str {
"approved" => ApprovalDecision::Approved,
"denied" => ApprovalDecision::Denied,
_ => return Err(format!("invalid decision: {decision_str}").into()),
};
let command = params.get("command").and_then(|v| v.as_str());
info!(id, ?decision, "resolving approval request");
self.manager.resolve(id, decision, command).await;
Ok(serde_json::json!({ "ok": true }))
}
}
/// Broadcasts approval requests to connected WebSocket clients.
pub struct GatewayApprovalBroadcaster {
state: Arc<GatewayState>,
}
impl GatewayApprovalBroadcaster {
pub fn new(state: Arc<GatewayState>) -> Self {
Self { state }
}
async fn notify_origin_channel(
&self,
session_key: Option<&str>,
command: &str,
) -> moltis_tools::Result<()> {
let Some(session_key) = session_key else {
return Ok(());
};
let Some(session_metadata) = self.state.services.session_metadata.as_ref() else {
return Ok(());
};
let Some(outbound) = self.state.services.channel_outbound_arc() else {
return Ok(());
};
let target = session_metadata
.get(session_key)
.await
.and_then(|entry| channel_reply_target_for_entry(&entry));
let Some(target) = target else {
return Ok(());
};
let preview = truncate_command_preview(command, MAX_COMMAND_PREVIEW_LEN);
let message = format!(
"Approval needed for `{preview}`.\nUse /approvals to see the numbered list, then /approve N or /deny N. The web UI still works too.",
);
outbound
.send_text(&target.account_id, &target.outbound_to(), &message, None)
.await
.map_err(|error| moltis_tools::Error::external("send approval notification", error))
}
}
#[async_trait]
impl ApprovalBroadcaster for GatewayApprovalBroadcaster {
async fn broadcast_request(
&self,
request_id: &str,
command: &str,
session_key: Option<&str>,
) -> moltis_tools::Result<()> {
broadcast(
&self.state,
"exec.approval.requested",
serde_json::json!({
"requestId": request_id,
"command": command,
"sessionKey": session_key,
}),
BroadcastOpts::default(),
)
.await;
if let Err(error) = self.notify_origin_channel(session_key, command).await {
warn!(%error, session_key, request_id, "failed to notify originating channel about approval");
}
Ok(())
}
}
/// Truncate a command string for safe display in channel notifications.
/// Prevents leaking full command text (which may contain secrets) to group chats.
pub(crate) const MAX_COMMAND_PREVIEW_LEN: usize = 80;
pub(crate) fn truncate_command_preview(command: &str, max_len: usize) -> Cow<'_, str> {
if command.len() <= max_len {
Cow::Borrowed(command)
} else {
Cow::Owned(format!(
"{}…",
&command[..command.floor_char_boundary(max_len)]
))
}
}
fn channel_reply_target_for_entry(entry: &SessionEntry) -> Option<ChannelReplyTarget> {
entry
.channel_binding
.as_deref()
.and_then(|binding| serde_json::from_str(binding).ok())
}
#[allow(clippy::unwrap_used, clippy::expect_used)]
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_live_service_resolve() {
let mgr = Arc::new(ApprovalManager::default());
let svc = LiveExecApprovalService::new(Arc::clone(&mgr));
// Create a pending request.
let (id, mut rx) = mgr.create_request("rm -rf /", Some("session:test")).await;
// Resolve via the service.
let result = svc
.resolve(serde_json::json!({
"requestId": id,
"decision": "denied",
}))
.await;
assert!(result.is_ok());
// The receiver should get Denied.
let decision = rx.try_recv().unwrap();
assert_eq!(decision, ApprovalDecision::Denied);
}
#[tokio::test]
async fn test_live_service_get() {
let mgr = Arc::new(ApprovalManager::default());
let svc = LiveExecApprovalService::new(mgr);
let result = svc.get().await.unwrap();
// Default mode is on-miss.
assert_eq!(result["mode"], "on-miss");
}
#[tokio::test]
async fn test_live_service_request_filters_by_session() {
let mgr = Arc::new(ApprovalManager::default());
let svc = LiveExecApprovalService::new(Arc::clone(&mgr));
let _ = mgr.create_request("echo one", Some("session:a")).await;
let _ = mgr.create_request("echo two", Some("session:b")).await;
let result = svc
.request(serde_json::json!({ "sessionKey": "session:a" }))
.await
.unwrap();
let requests = result["requests"]
.as_array()
.expect("requests should be an array");
assert_eq!(requests.len(), 1);
assert_eq!(requests[0]["session_key"], "session:a");
assert_eq!(requests[0]["command"], "echo one");
}
#[test]
fn test_channel_reply_target_for_entry_parses_channel_binding() {
let entry = SessionEntry {
id: "1".into(),
key: "telegram:bot-main:-100123".into(),
label: None,
model: None,
created_at: 0,
updated_at: 0,
message_count: 0,
last_seen_message_count: 0,
project_id: None,
archived: false,
worktree_branch: None,
sandbox_enabled: None,
sandbox_image: None,
sandbox_backend: None,
channel_binding: Some(
serde_json::json!({
"channel_type": "telegram",
"account_id": "bot-main",
"chat_id": "-100123",
"thread_id": "42"
})
.to_string(),
),
parent_session_key: None,
fork_point: None,
mcp_disabled: None,
preview: None,
agent_id: None,
mode_id: None,
node_id: None,
version: 0,
};
let target = channel_reply_target_for_entry(&entry).expect("expected channel target");
assert_eq!(target.account_id, "bot-main");
assert_eq!(target.outbound_to(), "-100123:42");
}
#[test]
fn test_channel_reply_target_for_entry_rejects_invalid_binding() {
let entry = SessionEntry {
id: "1".into(),
key: "session:abc".into(),
label: None,
model: None,
created_at: 0,
updated_at: 0,
message_count: 0,
last_seen_message_count: 0,
project_id: None,
archived: false,
worktree_branch: None,
sandbox_enabled: None,
sandbox_image: None,
sandbox_backend: None,
channel_binding: Some("{not-json".into()),
parent_session_key: None,
fork_point: None,
mcp_disabled: None,
preview: None,
agent_id: None,
mode_id: None,
node_id: None,
version: 0,
};
assert!(channel_reply_target_for_entry(&entry).is_none());
}
#[test]
fn truncate_command_preview_short_command_unchanged() {
let short = "git status";
assert_eq!(truncate_command_preview(short, 80).as_ref(), short);
}
#[test]
fn truncate_command_preview_long_command_truncated() {
let long = "a]".repeat(50); // 100 chars
let preview = truncate_command_preview(&long, 80);
assert!(preview.ends_with('…'));
// 80 chars of content + 3 bytes for '…'
assert_eq!(preview.len(), 83);
}
#[test]
fn truncate_command_preview_exact_length_unchanged() {
let exact = "a".repeat(80);
assert_eq!(truncate_command_preview(&exact, 80).as_ref(), exact);
}
#[test]
fn truncate_command_preview_respects_utf8_boundaries() {
let long = "é".repeat(60);
let preview = truncate_command_preview(&long, 81);
assert_eq!(preview.as_ref(), format!("{}…", "é".repeat(40)));
}
}