orra 0.0.2

Context-aware agent session management for any application
Documentation
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use async_trait::async_trait;
use tokio::sync::{mpsc, oneshot, RwLock};

use crate::hook::Hook;
use crate::message::ToolCall;
use crate::namespace::Namespace;
use crate::store::Session;

#[cfg(feature = "discord")]
use crate::tools::discord::DiscordConfig;

/// A pending approval request sent from the hook to the UI handler.
pub struct ApprovalRequest {
    pub call_id: String,
    pub tool_name: String,
    pub arguments: serde_json::Value,
    /// The hook blocks on the receiver side of this channel until the
    /// UI handler sends `true` (approved) or `false` (denied).
    pub response_tx: oneshot::Sender<bool>,
}

/// A hook that gates tool execution on user approval.
///
/// In normal mode, every tool call blocks in `before_tool_call` until the
/// user approves or denies it via the UI. In "chaos mode" (per-session),
/// all tool calls are auto-approved.
///
/// When a Discord config is provided via `with_discord()`, approval requests
/// from Discord sessions (namespace root == "discord") are sent as Discord
/// messages and the hook polls for a yes/no reply from the same user.
pub struct ApprovalHook {
    /// Channel to send approval requests to the UI handler.
    request_tx: mpsc::Sender<ApprovalRequest>,
    /// Per-session chaos mode flag, read from session metadata.
    chaos_mode: RwLock<bool>,
    /// Optional Discord config for sending approval prompts to Discord channels.
    #[cfg(feature = "discord")]
    discord: Option<DiscordConfig>,
}

impl ApprovalHook {
    pub fn new(request_tx: mpsc::Sender<ApprovalRequest>) -> Self {
        Self {
            request_tx,
            chaos_mode: RwLock::new(false),
            #[cfg(feature = "discord")]
            discord: None,
        }
    }

    /// Enable Discord-native approvals. When a tool call originates from a
    /// Discord session, the approval prompt is sent as a Discord message
    /// instead of going through the WebSocket UI channel.
    #[cfg(feature = "discord")]
    pub fn with_discord(mut self, discord: DiscordConfig) -> Self {
        self.discord = Some(discord);
        self
    }
}

/// Send the approval request through the UI channel (WebSocket path).
async fn ui_approval(
    request_tx: &mpsc::Sender<ApprovalRequest>,
    call: &ToolCall,
) -> Result<(), String> {
    let (response_tx, response_rx) = oneshot::channel();

    let request = ApprovalRequest {
        call_id: call.id.clone(),
        tool_name: call.name.clone(),
        arguments: call.arguments.clone(),
        response_tx,
    };

    // Send the approval request to the UI handler
    if request_tx.send(request).await.is_err() {
        // Channel closed — no active connection, auto-approve
        return Ok(());
    }

    // Block until the user responds
    match response_rx.await {
        Ok(true) => Ok(()),
        Ok(false) => Err(format!(
            "Tool call '{}' was denied by the user.",
            call.name
        )),
        Err(_) => {
            // Sender dropped — connection lost, auto-approve to avoid
            // permanently blocking
            Ok(())
        }
    }
}

/// Send an approval prompt to a Discord channel and poll for a yes/no reply.
#[cfg(feature = "discord")]
async fn discord_approval(
    discord: &DiscordConfig,
    namespace: &Namespace,
    call: &ToolCall,
) -> Result<(), String> {
    let segments = namespace.segments();
    // Namespace format: discord:<guild>:<channel>:<user>
    if segments.len() < 4 {
        // Can't determine channel/user, auto-approve
        return Ok(());
    }
    let channel_id = &segments[2];
    let author_id = &segments[3];

    // Format the approval message
    let args_str = serde_json::to_string_pretty(&call.arguments)
        .unwrap_or_else(|_| call.arguments.to_string());
    let prompt_content = format!(
        "**Tool approval required**\n`{}` wants to run with:\n```json\n{}\n```\nReply **yes** or **no**.",
        call.name, args_str
    );

    // Truncate if over 2000 chars (Discord message limit)
    let prompt_content = if prompt_content.len() > 2000 {
        format!(
            "**Tool approval required**\n`{}` wants to run. Arguments too large to display.\nReply **yes** or **no**.",
            call.name
        )
    } else {
        prompt_content
    };

    // Send the prompt message
    let send_resp = discord
        .request(
            reqwest::Method::POST,
            &format!("channels/{}/messages", channel_id),
        )
        .json(&serde_json::json!({ "content": prompt_content }))
        .send()
        .await
        .map_err(|e| format!("Failed to send approval prompt: {}", e))?;

    if !send_resp.status().is_success() {
        // Can't send to Discord, auto-approve
        return Ok(());
    }

    // Parse the sent message to get its ID for polling "after"
    let sent_msg: serde_json::Value = send_resp
        .json()
        .await
        .map_err(|e| format!("Failed to parse sent message: {}", e))?;
    let prompt_msg_id = sent_msg["id"]
        .as_str()
        .ok_or_else(|| "Missing message ID in response".to_string())?
        .to_string();

    // Poll for a reply from the same user
    let timeout = std::time::Duration::from_secs(300); // 5 minutes
    let poll_interval = std::time::Duration::from_secs(2);
    let start = std::time::Instant::now();

    loop {
        if start.elapsed() > timeout {
            return Err(format!(
                "Tool call '{}' timed out waiting for Discord approval.",
                call.name
            ));
        }

        tokio::time::sleep(poll_interval).await;

        // Get messages after our prompt
        let msgs_resp = discord
            .request(
                reqwest::Method::GET,
                &format!("channels/{}/messages", channel_id),
            )
            .query(&[("after", &prompt_msg_id), ("limit", &"10".to_string())])
            .send()
            .await;

        let msgs_resp = match msgs_resp {
            Ok(r) => r,
            Err(_) => continue, // Retry on network error
        };

        if !msgs_resp.status().is_success() {
            continue;
        }

        let messages: Vec<serde_json::Value> = match msgs_resp.json().await {
            Ok(m) => m,
            Err(_) => continue,
        };

        // Look for a message from the same author that says yes/no
        for msg in &messages {
            let msg_author_id = msg["author"]["id"].as_str().unwrap_or("");
            if msg_author_id != author_id {
                continue;
            }

            let content = msg["content"]
                .as_str()
                .unwrap_or("")
                .trim()
                .to_lowercase();

            if content == "yes" || content == "y" || content == "approve" {
                return Ok(());
            }
            if content == "no" || content == "n" || content == "deny" {
                return Err(format!(
                    "Tool call '{}' was denied by the user.",
                    call.name
                ));
            }
        }
    }
}

#[async_trait]
impl Hook for ApprovalHook {
    async fn after_session_load(&self, _namespace: &Namespace, session: &Session) {
        let chaos = session
            .metadata
            .get("chaos_mode")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);
        *self.chaos_mode.write().await = chaos;
    }

    async fn before_tool_call(&self, namespace: &Namespace, call: &mut ToolCall) -> Result<(), String> {
        // If chaos mode is enabled, auto-approve everything
        if *self.chaos_mode.read().await {
            return Ok(());
        }

        // Route based on namespace origin
        #[cfg(feature = "discord")]
        if namespace.root() == "discord" {
            if let Some(ref discord) = self.discord {
                return discord_approval(discord, namespace, call).await;
            }
            // No Discord config available, auto-approve
            return Ok(());
        }

        // Web UI approval flow (existing behavior)
        ui_approval(&self.request_tx, call).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::message::ToolCall;
    use crate::namespace::Namespace;
    use crate::store::Session;

    #[tokio::test]
    async fn chaos_mode_auto_approves() {
        let (tx, _rx) = mpsc::channel(1);
        let hook = ApprovalHook::new(tx);

        // Enable chaos mode via session metadata
        let ns = Namespace::new("test");
        let mut session = Session::new(ns.clone());
        session
            .metadata
            .insert("chaos_mode".into(), serde_json::json!(true));
        hook.after_session_load(&ns, &session).await;

        let mut call = ToolCall {
            id: "c1".into(),
            name: "exec".into(),
            arguments: serde_json::json!({"command": "ls"}),
        };

        // Should auto-approve without blocking
        let result = hook.before_tool_call(&ns, &mut call).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn approval_approved() {
        let (tx, mut rx) = mpsc::channel(1);
        let hook = ApprovalHook::new(tx);
        let ns = Namespace::new("test");

        let mut call = ToolCall {
            id: "c1".into(),
            name: "exec".into(),
            arguments: serde_json::json!({"command": "pwd"}),
        };

        // Spawn a task to approve the request
        let handle = tokio::spawn(async move {
            let req = rx.recv().await.unwrap();
            assert_eq!(req.tool_name, "exec");
            assert_eq!(req.call_id, "c1");
            req.response_tx.send(true).unwrap();
        });

        let result = hook.before_tool_call(&ns, &mut call).await;
        assert!(result.is_ok());
        handle.await.unwrap();
    }

    #[tokio::test]
    async fn approval_denied() {
        let (tx, mut rx) = mpsc::channel(1);
        let hook = ApprovalHook::new(tx);
        let ns = Namespace::new("test");

        let mut call = ToolCall {
            id: "c1".into(),
            name: "exec".into(),
            arguments: serde_json::json!({"command": "rm -rf /"}),
        };

        // Spawn a task to deny the request
        let handle = tokio::spawn(async move {
            let req = rx.recv().await.unwrap();
            req.response_tx.send(false).unwrap();
        });

        let result = hook.before_tool_call(&ns, &mut call).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("denied"));
        handle.await.unwrap();
    }

    #[tokio::test]
    async fn closed_channel_auto_approves() {
        let (tx, rx) = mpsc::channel(1);
        let hook = ApprovalHook::new(tx);
        let ns = Namespace::new("test");

        // Drop the receiver to simulate no active connection
        drop(rx);

        let mut call = ToolCall {
            id: "c1".into(),
            name: "exec".into(),
            arguments: serde_json::json!({"command": "ls"}),
        };

        // Should auto-approve when no listener is available
        let result = hook.before_tool_call(&ns, &mut call).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn chaos_mode_reads_from_session() {
        let (tx, _rx) = mpsc::channel(1);
        let hook = ApprovalHook::new(tx);
        let ns = Namespace::new("test");

        // Default: chaos mode off
        let session = Session::new(ns.clone());
        hook.after_session_load(&ns, &session).await;
        assert!(!*hook.chaos_mode.read().await);

        // Set chaos mode on
        let mut session2 = Session::new(ns.clone());
        session2
            .metadata
            .insert("chaos_mode".into(), serde_json::json!(true));
        hook.after_session_load(&ns, &session2).await;
        assert!(*hook.chaos_mode.read().await);

        // Clear chaos mode
        let session3 = Session::new(ns.clone());
        hook.after_session_load(&ns, &session3).await;
        assert!(!*hook.chaos_mode.read().await);
    }

    #[cfg(feature = "discord")]
    #[tokio::test]
    async fn discord_namespace_without_config_auto_approves() {
        let (tx, _rx) = mpsc::channel(1);
        let hook = ApprovalHook::new(tx);

        // Discord namespace but no DiscordConfig set — should auto-approve
        let ns = Namespace::new("discord")
            .child("guild123")
            .child("chan456")
            .child("user789");

        let mut call = ToolCall {
            id: "c1".into(),
            name: "exec".into(),
            arguments: serde_json::json!({"command": "ls"}),
        };

        let result = hook.before_tool_call(&ns, &mut call).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn non_discord_namespace_uses_ui_channel() {
        let (tx, mut rx) = mpsc::channel(1);
        let hook = ApprovalHook::new(tx);
        let ns = Namespace::new("web").child("session-123");

        let mut call = ToolCall {
            id: "c1".into(),
            name: "exec".into(),
            arguments: serde_json::json!({"command": "pwd"}),
        };

        // Spawn a task to approve via the UI channel
        let handle = tokio::spawn(async move {
            let req = rx.recv().await.unwrap();
            assert_eq!(req.tool_name, "exec");
            req.response_tx.send(true).unwrap();
        });

        let result = hook.before_tool_call(&ns, &mut call).await;
        assert!(result.is_ok());
        handle.await.unwrap();
    }
}