revka 2026.6.20

Revka — memory-native AI agent runtime powered by Kumiho
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, OnceLock, RwLock};

/// Process-global approval registry shared between the gateway (which registers
/// pending approvals) and the channel listeners (Discord/Slack/Telegram) which
/// handle keyword replies.
///
/// Both components run in the same process when started via `revka daemon`.
/// This singleton is created on first access and lives for the process lifetime.
static GLOBAL_REGISTRY: OnceLock<Arc<ApprovalRegistry>> = OnceLock::new();

/// Return the process-global `ApprovalRegistry`, creating it on first call.
pub fn global() -> Arc<ApprovalRegistry> {
    Arc::clone(GLOBAL_REGISTRY.get_or_init(|| Arc::new(ApprovalRegistry::new())))
}

/// Per-channel scoping for a pending approval — populated AFTER the approval
/// prompt is sent so we can restrict keyword matching to the thread / reply
/// that belongs to this specific approval. Without this the bot matches any
/// message in the configured notification channel, which conflates parallel
/// approvals and triggers on unrelated chatter.
///
/// The fields are interpreted per platform:
/// - `channel_id`: the platform channel/conversation the prompt was sent to
///   (Discord channel, Slack channel, Telegram chat).
/// - `thread_id`: the platform thread anchor (Discord thread, Slack `thread_ts`).
/// - `prompt_message_id`: the prompt's message id (Discord prompt message for
///   reply matching, Telegram prompt message id stored as a string).
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ApprovalScope {
    pub channel_id: Option<String>,
    pub thread_id: Option<String>,
    pub prompt_message_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PendingApproval {
    pub run_id: String,
    pub step_id: String,
    pub workflow_name: String,
    pub approve_keywords: Vec<String>,
    pub reject_keywords: Vec<String>,
    pub cwd: String,
    pub created_at: DateTime<Utc>,

    /// Per-channel reply scoping, keyed by channel slug (e.g. "discord",
    /// "slack", "telegram"). Populated by `attach` once the channel adapter
    /// has sent the prompt and reported the platform identifiers back.
    pub scopes: HashMap<String, ApprovalScope>,
}

impl PendingApproval {
    pub fn new(
        run_id: String,
        step_id: String,
        workflow_name: String,
        approve_keywords: Vec<String>,
        reject_keywords: Vec<String>,
        cwd: String,
    ) -> Self {
        Self {
            run_id,
            step_id,
            workflow_name,
            approve_keywords,
            reject_keywords,
            cwd,
            created_at: Utc::now(),
            scopes: HashMap::new(),
        }
    }
}

/// Thread-safe registry of pending workflow approvals.
///
/// When a workflow hits a human_approval step, the operator pushes an event
/// to the gateway. The gateway registers the pending approval here.
/// When a user responds (via Discord/Slack/Telegram reply or dashboard REST),
/// the gateway looks up the approval, atomically claims it, and calls
/// resume_workflow.
pub struct ApprovalRegistry {
    pending: RwLock<HashMap<String, PendingApproval>>, // keyed by run_id
}

impl ApprovalRegistry {
    pub fn new() -> Self {
        Self {
            pending: RwLock::new(HashMap::new()),
        }
    }

    /// Register a new pending approval. Channel thread/reply IDs are attached
    /// later via [`ApprovalRegistry::attach`] once the channel adapter has sent
    /// the prompt and received the message/thread identifiers back.
    pub fn register(&self, approval: PendingApproval) {
        let mut map = self.pending.write().unwrap();
        map.insert(approval.run_id.clone(), approval);
    }

    /// Attach per-channel reply scoping to an existing pending approval, keyed
    /// by channel slug (e.g. "discord", "slack", "telegram"). Called after the
    /// channel adapter has sent the prompt and reported the platform message /
    /// thread identifiers back.
    pub fn attach(&self, run_id: &str, channel: &str, scope: ApprovalScope) {
        let mut map = self.pending.write().unwrap();
        if let Some(a) = map.get_mut(run_id) {
            a.scopes.insert(channel.to_string(), scope);
        }
    }

    /// Atomically claim a pending approval. Returns Some if the approval
    /// existed and hadn't been claimed yet. Returns None if already claimed
    /// or not found. This prevents race conditions between channel adapters
    /// and the dashboard.
    pub fn try_claim(&self, run_id: &str) -> Option<PendingApproval> {
        let mut map = self.pending.write().unwrap();
        map.remove(run_id)
    }

    /// Match a Discord message to a pending approval. Only matches when the
    /// message is either in the thread that was created for the approval OR
    /// is a reply to the original prompt message. This prevents unrelated
    /// chatter in the notification channel from triggering approvals and
    /// cleanly disambiguates parallel approvals.
    ///
    /// Pass `None` for `thread_id` if the incoming message is in the root
    /// channel (not a thread). Pass `None` for `reply_to_message_id` if the
    /// incoming message is not a reply.
    pub fn match_discord_keyword(
        &self,
        channel_id: &str,
        thread_id: Option<&str>,
        reply_to_message_id: Option<&str>,
        message: &str,
    ) -> Option<(String, bool, String)> {
        let map = self.pending.read().unwrap();
        let msg_lower = message.trim().to_lowercase();

        for (run_id, approval) in map.iter() {
            let Some(scope) = approval.scopes.get("discord") else {
                continue;
            };
            let in_expected_thread = match (&scope.thread_id, thread_id) {
                (Some(want), Some(got)) => want == got,
                _ => false,
            };
            let is_reply_to_prompt = match (&scope.prompt_message_id, reply_to_message_id) {
                (Some(want), Some(got)) => want == got,
                _ => false,
            };
            // Back-compat: if we never captured thread/message IDs (e.g. send
            // failed or thread creation was denied), fall back to matching by
            // channel only. This preserves existing behavior for unscoped
            // deployments but is strictly worse disambiguation.
            let fallback_channel_only = scope.thread_id.is_none()
                && scope.prompt_message_id.is_none()
                && scope
                    .channel_id
                    .as_ref()
                    .map(|id| id == channel_id)
                    .unwrap_or(false);

            if !(in_expected_thread || is_reply_to_prompt || fallback_channel_only) {
                continue;
            }

            if let Some(res) = match_keywords(
                &msg_lower,
                message,
                &approval.approve_keywords,
                &approval.reject_keywords,
            ) {
                return Some((run_id.clone(), res.0, res.1));
            }
        }

        None
    }

    /// Match a Slack message to a pending approval. Requires the incoming
    /// message to carry `thread_ts` equal to the approval's captured ts.
    pub fn match_slack_keyword(
        &self,
        channel_id: &str,
        thread_ts: Option<&str>,
        message: &str,
    ) -> Option<(String, bool, String)> {
        let map = self.pending.read().unwrap();
        let msg_lower = message.trim().to_lowercase();

        for (run_id, approval) in map.iter() {
            let Some(scope) = approval.scopes.get("slack") else {
                continue;
            };
            let channel_match = scope
                .channel_id
                .as_ref()
                .map(|id| id == channel_id)
                .unwrap_or(false);
            if !channel_match {
                continue;
            }
            let thread_match = match (&scope.thread_id, thread_ts) {
                (Some(want), Some(got)) => want == got,
                _ => false,
            };
            if !thread_match {
                continue;
            }
            if let Some(res) = match_keywords(
                &msg_lower,
                message,
                &approval.approve_keywords,
                &approval.reject_keywords,
            ) {
                return Some((run_id.clone(), res.0, res.1));
            }
        }

        None
    }

    /// Match a Telegram message to a pending approval. Requires the incoming
    /// message to be a reply to the approval's prompt message.
    pub fn match_telegram_keyword(
        &self,
        chat_id: &str,
        reply_to_message_id: Option<i64>,
        message: &str,
    ) -> Option<(String, bool, String)> {
        let map = self.pending.read().unwrap();
        let msg_lower = message.trim().to_lowercase();

        for (run_id, approval) in map.iter() {
            let Some(scope) = approval.scopes.get("telegram") else {
                continue;
            };
            let chat_match = scope
                .channel_id
                .as_ref()
                .map(|id| id == chat_id)
                .unwrap_or(false);
            if !chat_match {
                continue;
            }
            let want_prompt = scope
                .prompt_message_id
                .as_ref()
                .and_then(|s| s.parse::<i64>().ok());
            let reply_match = match (want_prompt, reply_to_message_id) {
                (Some(want), Some(got)) => want == got,
                _ => false,
            };
            if !reply_match {
                continue;
            }
            if let Some(res) = match_keywords(
                &msg_lower,
                message,
                &approval.approve_keywords,
                &approval.reject_keywords,
            ) {
                return Some((run_id.clone(), res.0, res.1));
            }
        }

        None
    }

    /// Match a bare approve/reject keyword to a pending approval on ANY channel,
    /// scoped to `(channel, channel_id)`. Channel-agnostic and reply-free: the
    /// per-platform matchers above handle precise reply/thread matching (which
    /// disambiguates parallel approvals), while this fallback lets a plain
    /// "approve" resume a run on any channel — but ONLY when exactly one approval
    /// is pending for that chat/channel, so multiple concurrent approvals still
    /// require an explicit reply/thread to disambiguate.
    ///
    /// `channel` is the channel slug (e.g. "telegram", "discord", "mattermost");
    /// `channel_id` is the incoming message's chat/channel, compared against the
    /// scope's `channel_id` captured when the prompt was sent.
    pub fn match_any_channel_keyword(
        &self,
        channel: &str,
        channel_id: &str,
        message: &str,
    ) -> Option<(String, bool, String)> {
        let map = self.pending.read().unwrap();
        if map.is_empty() {
            return None;
        }
        let msg_lower = message.trim().to_lowercase();

        // Collect REAL keyword matches scoped to (channel, channel_id). Two
        // pendings in the same chat are only ambiguous when BOTH match the same
        // keyword — disjoint keyword lists disambiguate themselves. So filter by
        // keyword first, then bail only if more than one pending actually
        // matched the incoming text.
        let mut hit: Option<(String, bool, String)> = None;
        for (run_id, approval) in map.iter() {
            let scoped = approval
                .scopes
                .get(channel)
                .and_then(|s| s.channel_id.as_deref())
                == Some(channel_id);
            if !scoped {
                continue;
            }
            if let Some((is_approve, feedback)) = match_keywords(
                &msg_lower,
                message,
                &approval.approve_keywords,
                &approval.reject_keywords,
            ) {
                if hit.is_some() {
                    // More than one pending matched the keyword → ambiguous;
                    // require an explicit reply/thread to disambiguate.
                    return None;
                }
                hit = Some((run_id.clone(), is_approve, feedback));
            }
        }
        hit
    }

    /// Remove a pending approval (cleanup after resolution).
    pub fn remove(&self, run_id: &str) {
        let mut map = self.pending.write().unwrap();
        map.remove(run_id);
    }

    /// List all pending approvals (for debugging/status).
    pub fn list_pending(&self) -> Vec<PendingApproval> {
        let map = self.pending.read().unwrap();
        map.values().cloned().collect()
    }
}

/// Check a normalized message against approve/reject keyword lists. Returns
/// `(is_approve, feedback)` on match.
fn match_keywords(
    msg_lower: &str,
    original: &str,
    approve_keywords: &[String],
    reject_keywords: &[String],
) -> Option<(bool, String)> {
    for kw in approve_keywords {
        if msg_lower == kw
            || msg_lower
                .strip_prefix(kw.as_str())
                .is_some_and(|rest| rest.starts_with(' '))
        {
            return Some((true, String::new()));
        }
    }
    for kw in reject_keywords {
        if msg_lower == kw {
            return Some((false, String::new()));
        }
        if let Some(rest_lower) = msg_lower.strip_prefix(kw.as_str()) {
            if rest_lower.starts_with(' ') {
                // Preserve the user's original casing when the keyword end lands
                // on a char boundary in the original (always true for ASCII
                // keywords); otherwise fall back to the lowercased remainder so
                // a case-folding byte-length change can never slice inside a
                // multibyte char and panic (which, under the registry read
                // lock, would poison it and brick all approvals).
                let trimmed = original.trim();
                let feedback = if trimmed.is_char_boundary(kw.len()) {
                    trimmed[kw.len()..].trim().to_string()
                } else {
                    rest_lower.trim().to_string()
                };
                return Some((false, feedback));
            }
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;

    fn approval(run_id: &str) -> PendingApproval {
        PendingApproval::new(
            run_id.into(),
            "approve".into(),
            "wf".into(),
            vec!["approve".into(), "yes".into()],
            vec!["reject".into()],
            "/tmp".into(),
        )
    }

    fn approval_with_keywords(
        run_id: &str,
        approve: Vec<String>,
        reject: Vec<String>,
    ) -> PendingApproval {
        PendingApproval::new(
            run_id.into(),
            "approve".into(),
            "wf".into(),
            approve,
            reject,
            "/tmp".into(),
        )
    }

    fn scope(chat: &str) -> ApprovalScope {
        ApprovalScope {
            channel_id: Some(chat.into()),
            thread_id: None,
            prompt_message_id: None,
        }
    }

    #[test]
    fn bare_keyword_resumes_single_pending_on_any_channel() {
        for ch in ["telegram", "discord", "slack", "mattermost", "signal"] {
            let reg = ApprovalRegistry::new();
            reg.register(approval("r1"));
            reg.attach("r1", ch, scope("100"));
            let m = reg.match_any_channel_keyword(ch, "100", "approve");
            assert_eq!(
                m.map(|(id, ok, _)| (id, ok)),
                Some(("r1".into(), true)),
                "channel {ch}"
            );
        }
    }

    #[test]
    fn bare_reject_carries_feedback() {
        let reg = ApprovalRegistry::new();
        reg.register(approval("r1"));
        reg.attach("r1", "discord", scope("c1"));
        let m = reg.match_any_channel_keyword("discord", "c1", "reject not safe");
        assert_eq!(m, Some(("r1".into(), false, "not safe".into())));
    }

    #[test]
    fn reject_feedback_survives_case_folding_byte_change() {
        // Kelvin sign 'K' (U+212A, 3 bytes) lowercases to ASCII 'k' (1 byte).
        // Slicing the original by the lowercased keyword's byte length would land
        // inside the multibyte char and panic (poisoning the registry lock). The
        // matcher must instead fall back to the lowercased remainder.
        let reg = ApprovalRegistry::new();
        reg.register(approval_with_keywords(
            "r1",
            vec!["approve".into()],
            vec!["k".into()],
        ));
        reg.attach("r1", "telegram", scope("100"));
        let m = reg.match_any_channel_keyword("telegram", "100", "\u{212A} bad");
        assert_eq!(m, Some(("r1".into(), false, "bad".into())));
    }

    #[test]
    fn ambiguous_when_multiple_pending_in_same_chat() {
        let reg = ApprovalRegistry::new();
        reg.register(approval("r1"));
        reg.attach("r1", "slack", scope("c1"));
        reg.register(approval("r2"));
        reg.attach("r2", "slack", scope("c1"));
        // Two pending in the same chat → ambiguous → no match (an explicit
        // reply/thread via the per-platform matcher disambiguates instead).
        assert!(
            reg.match_any_channel_keyword("slack", "c1", "approve")
                .is_none()
        );
    }

    #[test]
    fn no_match_for_other_chat_or_channel() {
        let reg = ApprovalRegistry::new();
        reg.register(approval("r1"));
        reg.attach("r1", "telegram", scope("100"));
        assert!(
            reg.match_any_channel_keyword("telegram", "999", "approve")
                .is_none()
        );
        assert!(
            reg.match_any_channel_keyword("discord", "100", "approve")
                .is_none()
        );
    }

    #[test]
    fn no_match_for_non_keyword_message() {
        let reg = ApprovalRegistry::new();
        reg.register(approval("r1"));
        reg.attach("r1", "telegram", scope("100"));
        assert!(
            reg.match_any_channel_keyword("telegram", "100", "hello there")
                .is_none()
        );
    }

    #[test]
    fn two_pending_disjoint_keywords_disambiguate() {
        let reg = ApprovalRegistry::new();
        // Two pendings in the same chat, but with DISJOINT keyword lists. A
        // keyword unique to one of them is unambiguous and resumes that run.
        reg.register(approval_with_keywords(
            "r1",
            vec!["alpha".into()],
            vec!["nope".into()],
        ));
        reg.attach("r1", "slack", scope("c1"));
        reg.register(approval_with_keywords(
            "r2",
            vec!["bravo".into()],
            vec!["deny".into()],
        ));
        reg.attach("r2", "slack", scope("c1"));

        let m = reg.match_any_channel_keyword("slack", "c1", "alpha");
        assert_eq!(m.map(|(id, ok, _)| (id, ok)), Some(("r1".into(), true)));

        let m = reg.match_any_channel_keyword("slack", "c1", "bravo");
        assert_eq!(m.map(|(id, ok, _)| (id, ok)), Some(("r2".into(), true)));
    }

    #[test]
    fn is_empty_registry_returns_none() {
        let reg = ApprovalRegistry::new();
        assert!(
            reg.match_any_channel_keyword("telegram", "100", "approve")
                .is_none()
        );
    }

    #[test]
    fn keyword_with_trailing_text_strip_prefix() {
        let reg = ApprovalRegistry::new();
        reg.register(approval("r1"));
        reg.attach("r1", "discord", scope("c1"));
        let m = reg.match_any_channel_keyword("discord", "c1", "yes please");
        assert_eq!(m.map(|(id, ok, _)| (id, ok)), Some(("r1".into(), true)));
    }
}