okbot-testkit 0.10.1

Test doubles and fixtures for okbot crates and third-party adapters.
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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! An in-memory [`ConversationStore`].

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use jiff::{SignedDuration, Timestamp};
use okbot_core::{
    AgentRunRecord, AutomationRun, ChannelId, Clock, ConversationKey, ConversationStore, Fire,
    FireState, NewMessage, PAIRING_MAX_FAILURES, PeerTaskRecord, PeerTaskState, RoleGrant,
    SealedEntry, StoreError, StoredMessage, ToolCallRecord, Usage, UserId, async_trait,
};

#[derive(Default)]
struct State {
    seen: HashMap<(ChannelId, String), Timestamp>,
    messages: Vec<(u64, NewMessage)>,
    reset_after: HashMap<ConversationKey, u64>,
    sessions: HashMap<ConversationKey, (String, String)>,
    runs: Vec<AgentRunRecord>,
    next: u64,
    owners: Vec<(ChannelId, UserId)>,
    /// code → (expires, failures)
    codes: HashMap<String, (Timestamp, u32)>,
    fires: HashMap<(String, Timestamp), Fire>,
    direct: HashMap<(ChannelId, UserId), ConversationKey>,
    tool_calls: Vec<ToolCallRecord>,
    grants: HashMap<(String, String), RoleGrant>,
    /// (kind, server, subject) → (sealed, updated)
    sealed: HashMap<(String, String, String), (Vec<u8>, Timestamp)>,
    /// (automation, item) → seen
    items: HashMap<(String, String), Timestamp>,
    automation_runs: Vec<AutomationRun>,
    /// (inbound, id) → task
    peer_tasks: HashMap<(bool, String), PeerTaskRecord>,
}

/// A [`ConversationStore`] kept in memory, for runtime tests.
pub struct MemoryStore {
    state: Mutex<State>,
    clock: Arc<dyn Clock>,
}

impl std::fmt::Debug for MemoryStore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MemoryStore").finish_non_exhaustive()
    }
}

impl MemoryStore {
    /// An empty store reading time from `clock`.
    pub fn new(clock: Arc<dyn Clock>) -> Self {
        Self {
            state: Mutex::new(State::default()),
            clock,
        }
    }

    fn lock(&self) -> std::sync::MutexGuard<'_, State> {
        self.state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    /// Recorded agent runs, oldest first.
    pub fn runs(&self) -> Vec<AgentRunRecord> {
        self.lock().runs.clone()
    }

    /// Recorded tool calls, oldest first.
    pub fn recorded_tool_calls(&self) -> Vec<ToolCallRecord> {
        self.lock().tool_calls.clone()
    }
}

fn signed(d: Duration) -> SignedDuration {
    SignedDuration::try_from(d).unwrap_or(SignedDuration::MAX)
}

#[async_trait]
impl ConversationStore for MemoryStore {
    async fn mark_seen(
        &self,
        channel: &ChannelId,
        event_id: &str,
        ttl: Duration,
    ) -> Result<bool, StoreError> {
        let now = self.clock.now();
        let mut state = self.lock();
        let key = (channel.clone(), event_id.to_string());
        if state.seen.get(&key).is_some_and(|expires| *expires > now) {
            return Ok(false);
        }
        state
            .seen
            .insert(key, now.saturating_add(signed(ttl)).unwrap_or(now));
        Ok(true)
    }

    async fn append(&self, message: NewMessage) -> Result<(), StoreError> {
        let mut state = self.lock();
        state.next += 1;
        let id = state.next;
        state.messages.push((id, message));
        Ok(())
    }

    async fn recent(
        &self,
        conversation: &ConversationKey,
        limit: usize,
    ) -> Result<Vec<StoredMessage>, StoreError> {
        let state = self.lock();
        let after = state.reset_after.get(conversation).copied().unwrap_or(0);
        let mut out: Vec<StoredMessage> = state
            .messages
            .iter()
            .rev()
            .filter(|(id, m)| *id > after && m.conversation == *conversation)
            .take(limit)
            .map(|(_, m)| StoredMessage {
                role: m.role,
                author_name: m.author_name.clone(),
                text: m.text.clone(),
                at: m.at,
            })
            .collect();
        out.reverse();
        Ok(out)
    }

    async fn reset(&self, conversation: &ConversationKey) -> Result<(), StoreError> {
        let mut state = self.lock();
        let last = state.next;
        state.reset_after.insert(conversation.clone(), last);
        state.sessions.remove(conversation);
        Ok(())
    }

    async fn session(
        &self,
        conversation: &ConversationKey,
    ) -> Result<Option<(String, String)>, StoreError> {
        Ok(self.lock().sessions.get(conversation).cloned())
    }

    async fn set_session(
        &self,
        conversation: &ConversationKey,
        backend: &str,
        session: &str,
    ) -> Result<(), StoreError> {
        self.lock().sessions.insert(
            conversation.clone(),
            (backend.to_string(), session.to_string()),
        );
        Ok(())
    }

    async fn owners(&self) -> Result<Vec<(ChannelId, UserId)>, StoreError> {
        let mut owners = self.lock().owners.clone();
        owners.sort();
        Ok(owners)
    }

    async fn remove_owner(&self, channel: &ChannelId, user: &UserId) -> Result<bool, StoreError> {
        let mut state = self.lock();
        let before = state.owners.len();
        state.owners.retain(|(c, u)| c != channel || u != user);
        Ok(state.owners.len() != before)
    }

    async fn create_pairing_code(&self, code: &str, ttl: Duration) -> Result<(), StoreError> {
        let expires = self.clock.now() + signed(ttl);
        self.lock().codes.insert(code.to_string(), (expires, 0));
        Ok(())
    }

    async fn redeem_pairing_code(
        &self,
        code: &str,
        channel: &ChannelId,
        user: &UserId,
    ) -> Result<bool, StoreError> {
        let now = self.clock.now();
        let mut state = self.lock();
        state.codes.retain(|_, (expires, _)| *expires > now);
        if state.codes.remove(code).is_some() {
            let account = (channel.clone(), user.clone());
            if !state.owners.contains(&account) {
                state.owners.push(account);
            }
            return Ok(true);
        }
        for (_, failures) in state.codes.values_mut() {
            *failures += 1;
        }
        state
            .codes
            .retain(|_, (_, failures)| *failures < PAIRING_MAX_FAILURES);
        Ok(false)
    }

    async fn record_run(&self, run: AgentRunRecord) -> Result<(), StoreError> {
        self.lock().runs.push(run);
        Ok(())
    }

    async fn claim_fire(&self, task_id: &str, fire_at: Timestamp) -> Result<Fire, StoreError> {
        let now = self.clock.now();
        Ok(self
            .lock()
            .fires
            .entry((task_id.to_string(), fire_at))
            .or_insert_with(|| Fire {
                task_id: task_id.to_string(),
                fire_at,
                state: FireState::Sending,
                attempts: 0,
                updated_at: now,
            })
            .clone())
    }

    async fn set_fire(
        &self,
        task_id: &str,
        fire_at: Timestamp,
        state: FireState,
        attempts: u32,
    ) -> Result<(), StoreError> {
        let fire = Fire {
            task_id: task_id.to_string(),
            fire_at,
            state,
            attempts,
            updated_at: self.clock.now(),
        };
        self.lock()
            .fires
            .insert((task_id.to_string(), fire_at), fire);
        Ok(())
    }

    async fn recent_fires(&self, limit: usize) -> Result<Vec<Fire>, StoreError> {
        let mut fires: Vec<Fire> = self.lock().fires.values().cloned().collect();
        fires.sort_by_key(|f| std::cmp::Reverse((f.updated_at, f.fire_at)));
        fires.truncate(limit);
        Ok(fires)
    }

    async fn set_direct_chat(
        &self,
        channel: &ChannelId,
        user: &UserId,
        conversation: &ConversationKey,
    ) -> Result<(), StoreError> {
        self.lock()
            .direct
            .insert((channel.clone(), user.clone()), conversation.clone());
        Ok(())
    }

    async fn direct_chat(
        &self,
        channel: &ChannelId,
        user: &UserId,
    ) -> Result<Option<ConversationKey>, StoreError> {
        Ok(self
            .lock()
            .direct
            .get(&(channel.clone(), user.clone()))
            .cloned())
    }

    async fn usage_since(&self, person: &str, since: Timestamp) -> Result<Usage, StoreError> {
        let state = self.lock();
        let mut usage = Usage::default();
        for run in state
            .runs
            .iter()
            .filter(|r| r.person.as_deref() == Some(person) && r.started_at >= since)
        {
            usage.turns += 1;
            usage.tokens += run.usage.map_or(0, |u| u.input + u.output);
        }
        Ok(usage)
    }

    async fn record_tool_call(&self, call: ToolCallRecord) -> Result<(), StoreError> {
        self.lock().tool_calls.push(call);
        Ok(())
    }

    async fn tool_calls(
        &self,
        since: Timestamp,
        limit: usize,
    ) -> Result<Vec<ToolCallRecord>, StoreError> {
        Ok(self
            .lock()
            .tool_calls
            .iter()
            .rev()
            .filter(|c| c.at >= since)
            .take(limit)
            .cloned()
            .collect())
    }

    async fn grant_role(&self, grant: RoleGrant) -> Result<(), StoreError> {
        self.lock()
            .grants
            .insert((grant.role.clone(), grant.account.clone()), grant);
        Ok(())
    }

    async fn revoke_role(&self, role: &str, account: &str) -> Result<bool, StoreError> {
        let now = self.clock.now();
        Ok(self
            .lock()
            .grants
            .remove(&(role.to_string(), account.to_string()))
            .is_some_and(|g| g.expires_at > now))
    }

    async fn role_grants(&self, now: Timestamp) -> Result<Vec<RoleGrant>, StoreError> {
        let mut grants: Vec<RoleGrant> = self
            .lock()
            .grants
            .values()
            .filter(|g| g.expires_at > now)
            .cloned()
            .collect();
        grants.sort_by(|a, b| (&a.role, &a.account).cmp(&(&b.role, &b.account)));
        Ok(grants)
    }

    async fn purge_before(&self, before: Timestamp) -> Result<u64, StoreError> {
        let now = self.clock.now();
        let mut state = self.lock();
        let count = |s: &State| {
            s.messages.len()
                + s.runs.len()
                + s.seen.len()
                + s.fires.len()
                + s.tool_calls.len()
                + s.grants.len()
                + s.items.len()
                + s.automation_runs.len()
                + s.peer_tasks.len()
        };
        let start = count(&state);
        state.messages.retain(|(_, m)| m.at >= before);
        state.runs.retain(|r| r.started_at >= before);
        state.tool_calls.retain(|c| c.at >= before);
        state.grants.retain(|_, g| g.expires_at > now);
        state.items.retain(|_, seen| *seen >= before);
        state.automation_runs.retain(|r| r.at >= before);
        state.peer_tasks.retain(|_, t| t.updated_at >= before);
        state.seen.retain(|_, expires| *expires > now);
        state
            .fires
            .retain(|_, f| f.updated_at >= before || f.state == FireState::Sending);
        let end = count(&state);
        Ok((start - end) as u64)
    }

    async fn sealed(
        &self,
        kind: &str,
        server: &str,
        subject: &str,
    ) -> Result<Option<Vec<u8>>, StoreError> {
        let key = (kind.to_string(), server.to_string(), subject.to_string());
        Ok(self.lock().sealed.get(&key).map(|(v, _)| v.clone()))
    }

    async fn set_sealed(
        &self,
        kind: &str,
        server: &str,
        subject: &str,
        sealed: Vec<u8>,
    ) -> Result<(), StoreError> {
        let key = (kind.to_string(), server.to_string(), subject.to_string());
        let now = self.clock.now();
        self.lock().sealed.insert(key, (sealed, now));
        Ok(())
    }

    async fn delete_sealed(
        &self,
        kind: &str,
        server: &str,
        subject: &str,
    ) -> Result<bool, StoreError> {
        let key = (kind.to_string(), server.to_string(), subject.to_string());
        Ok(self.lock().sealed.remove(&key).is_some())
    }

    async fn sealed_entries(&self, kind: &str) -> Result<Vec<SealedEntry>, StoreError> {
        let mut out: Vec<SealedEntry> = self
            .lock()
            .sealed
            .iter()
            .filter(|((k, _, _), _)| k == kind)
            .map(|((k, server, subject), (_, at))| SealedEntry {
                kind: k.clone(),
                server: server.clone(),
                subject: subject.clone(),
                updated_at: *at,
            })
            .collect();
        out.sort_by(|a, b| (&a.server, &a.subject).cmp(&(&b.server, &b.subject)));
        Ok(out)
    }

    async fn claim_items(
        &self,
        automation: &str,
        items: Vec<String>,
    ) -> Result<Vec<String>, StoreError> {
        let now = self.clock.now();
        let mut state = self.lock();
        let mut new = Vec::new();
        for item in items {
            let fresh = state
                .items
                .insert((automation.to_string(), item.clone()), now)
                .is_none();
            if fresh {
                new.push(item);
            }
        }
        Ok(new)
    }

    async fn release_items(&self, automation: &str, items: Vec<String>) -> Result<(), StoreError> {
        let mut state = self.lock();
        for item in items {
            state.items.remove(&(automation.to_string(), item));
        }
        Ok(())
    }

    async fn record_automation_run(&self, run: AutomationRun) -> Result<(), StoreError> {
        self.lock().automation_runs.push(run);
        Ok(())
    }

    async fn automation_runs(
        &self,
        automation: Option<&str>,
        since: Timestamp,
        limit: usize,
    ) -> Result<Vec<AutomationRun>, StoreError> {
        Ok(self
            .lock()
            .automation_runs
            .iter()
            .rev()
            .filter(|r| r.at >= since && automation.is_none_or(|a| r.automation == a))
            .take(limit)
            .cloned()
            .collect())
    }

    async fn put_peer_task(&self, task: PeerTaskRecord) -> Result<(), StoreError> {
        let mut state = self.lock();
        let key = (task.inbound, task.id.clone());
        let duplicate = state.peer_tasks.values().any(|t| {
            t.inbound == task.inbound
                && t.team == task.team
                && t.peer == task.peer
                && t.message_id == task.message_id
                && t.id != task.id
        });
        if duplicate {
            return Err(StoreError::Backend("duplicate peer message".into()));
        }
        match state.peer_tasks.get_mut(&key) {
            Some(old) => {
                old.state = task.state;
                old.conversation = task.conversation;
                old.text = task.text;
                old.tokens = task.tokens;
                old.updated_at = task.updated_at;
            }
            None => {
                state.peer_tasks.insert(key, task);
            }
        }
        Ok(())
    }

    async fn peer_task(
        &self,
        inbound: bool,
        id: &str,
    ) -> Result<Option<PeerTaskRecord>, StoreError> {
        Ok(self
            .lock()
            .peer_tasks
            .get(&(inbound, id.to_string()))
            .cloned())
    }

    async fn peer_task_by_message(
        &self,
        inbound: bool,
        team: &str,
        peer: &str,
        message_id: &str,
    ) -> Result<Option<PeerTaskRecord>, StoreError> {
        Ok(self
            .lock()
            .peer_tasks
            .values()
            .find(|t| {
                t.inbound == inbound
                    && t.team == team
                    && t.peer == peer
                    && t.message_id == message_id
            })
            .cloned())
    }

    async fn peer_tasks(&self, limit: usize) -> Result<Vec<PeerTaskRecord>, StoreError> {
        let mut tasks: Vec<PeerTaskRecord> = self.lock().peer_tasks.values().cloned().collect();
        tasks.sort_by_key(|t| std::cmp::Reverse((t.updated_at, t.created_at)));
        tasks.truncate(limit);
        Ok(tasks)
    }

    async fn interrupt_peer_tasks(&self, now: Timestamp) -> Result<u64, StoreError> {
        let mut changed = 0;
        for task in self.lock().peer_tasks.values_mut() {
            if task.inbound && !task.state.is_final() {
                task.state = PeerTaskState::Failed;
                task.text = Some("interrupted: the bot stopped".into());
                task.updated_at = now;
                changed += 1;
            }
        }
        Ok(changed)
    }
}

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

    #[tokio::test]
    async fn conformance() {
        let clock = Arc::new(ManualClock::default());
        let store = MemoryStore::new(clock.clone());
        crate::conformance::conversation_store(&store, &clock).await;
    }
}