opencrabs 0.5.4

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Recommended: the 40MB prebuilt binary for macOS, Linux and Windows: https://github.com/adolfousier/opencrabs/releases
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
//! Channel Message Repository
//!
//! Database operations for passively captured channel messages.

use crate::db::Pool;
use crate::db::database::interact_err;
use crate::db::models::ChannelMessage;
use anyhow::{Context, Result};
use rusqlite::{OptionalExtension, params};

/// Summary of a known chat
pub struct ChatSummary {
    pub channel: String,
    pub channel_chat_id: String,
    pub channel_chat_name: Option<String>,
    pub message_count: i64,
    pub last_message_at: i64,
}

/// Repository for channel message operations
#[derive(Clone)]
pub struct ChannelMessageRepository {
    pool: Pool,
}

impl ChannelMessageRepository {
    pub fn new(pool: Pool) -> Self {
        Self { pool }
    }

    /// Insert a single channel message
    pub async fn insert(&self, msg: &ChannelMessage) -> Result<()> {
        let m = msg.clone();
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                conn.execute(
                    "INSERT OR IGNORE INTO channel_messages
                        (id, channel, channel_chat_id, channel_chat_name,
                         sender_id, sender_name, content, message_type,
                         platform_message_id, created_at, thread_id, topic_name)
                     VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)",
                    params![
                        m.id.to_string(),
                        m.channel,
                        m.channel_chat_id,
                        m.channel_chat_name,
                        m.sender_id,
                        m.sender_name,
                        m.content,
                        m.message_type,
                        m.platform_message_id,
                        m.created_at.timestamp(),
                        m.thread_id,
                        m.topic_name,
                    ],
                )
            })
            .await
            .map_err(interact_err)?
            .context("Failed to insert channel message")?;

        Ok(())
    }

    /// Rewrite a stored message's content, matched by platform message id.
    ///
    /// Reconciles streamed/edited messages from peer bots: a bot that
    /// streams its reply via Telegram message edits arrives as one initial
    /// frame plus a run of `edited_message` updates. When an edit lands
    /// after we already captured the message, this rewrites the row so
    /// group history reflects the FINAL text rather than the first frame.
    /// Returns the number of rows updated (0 if the message wasn't stored).
    pub async fn update_content(
        &self,
        channel: &str,
        chat_id: &str,
        platform_message_id: &str,
        content: &str,
    ) -> Result<usize> {
        let ch = channel.to_string();
        let cid = chat_id.to_string();
        let pmid = platform_message_id.to_string();
        let body = content.to_string();
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                conn.execute(
                    "UPDATE channel_messages SET content = ?4 \
                     WHERE channel = ?1 AND channel_chat_id = ?2 \
                       AND platform_message_id = ?3",
                    params![ch, cid, pmid, body],
                )
            })
            .await
            .map_err(interact_err)?
            .context("Failed to update channel message content")
    }

    /// Forum-topic dead-key eviction (#116): Telegram answered
    /// `400 Bad Request: message thread not found` for `(channel, chat,
    /// thread)` — the remembered topic no longer exists. Clear the
    /// thread_id on every row of that chat carrying it, so the
    /// auto-lookup (`latest_thread_id_for_chat`) and the topic listing
    /// stop serving the dead address. Chat-scoped by design: a deleted
    /// topic in one chat must never touch another chat's rows.
    pub async fn clear_thread_for_chat(
        &self,
        channel: &str,
        chat_id: &str,
        thread_id: &str,
    ) -> Result<u64> {
        let ch = channel.to_string();
        let cid = chat_id.to_string();
        let tid = thread_id.to_string();
        let updated = self
            .pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                conn.execute(
                    "UPDATE channel_messages SET thread_id = NULL \
                     WHERE channel = ?1 AND channel_chat_id = ?2 AND thread_id = ?3",
                    params![ch, cid, tid],
                )
            })
            .await
            .map_err(interact_err)?
            .context("Failed to clear dead forum topic thread_id")?;
        Ok(updated as u64)
    }

    /// #1655: General-topic history fetch. General rows persist with
    /// `thread_id = NULL` (General messages carry no `message_thread_id`),
    /// so "this topic only" for General means `thread_id IS NULL` — a state
    /// `recent()`'s `Option<&str>` cannot express, because `None` there means
    /// "no thread filter" and returns EVERY topic's rows. Without this, a
    /// General prompt in a forum pulled cross-topic history and the agent
    /// continued other topics' work in General.
    pub async fn recent_general(
        &self,
        channel: &str,
        chat_id: &str,
        limit: i64,
        message_type: Option<&str>,
    ) -> Result<Vec<ChannelMessage>> {
        let ch = channel.to_string();
        let cid = chat_id.to_string();
        let mtype = message_type.map(|s| s.to_string());
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| match mtype {
                Some(mtype) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE channel = ?1 AND channel_chat_id = ?2 \
                             AND thread_id IS NULL AND message_type = ?3 \
                             ORDER BY created_at DESC LIMIT ?4",
                    )?;
                    let rows =
                        stmt.query_map(params![ch, cid, mtype, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
                None => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE channel = ?1 AND channel_chat_id = ?2 \
                             AND thread_id IS NULL \
                             ORDER BY created_at DESC LIMIT ?3",
                    )?;
                    let rows = stmt.query_map(params![ch, cid, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
            })
            .await
            .map_err(interact_err)?
            .context("Failed to fetch recent general-topic messages")
    }

    /// Get recent messages for a specific chat, optionally filtered by thread_id.
    /// When `thread_id` is Some, only messages belonging to that forum topic are returned.
    pub async fn recent(
        &self,
        channel: Option<&str>,
        chat_id: &str,
        limit: i64,
        thread_id: Option<&str>,
        message_type: Option<&str>,
    ) -> Result<Vec<ChannelMessage>> {
        let ch = channel.map(|s| s.to_string());
        let cid = chat_id.to_string();
        let tid = thread_id.map(|s| s.to_string());
        let mtype = message_type.map(|s| s.to_string());
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| match (ch, tid, mtype) {
                (Some(ch), Some(tid), Some(mtype)) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE channel = ?1 AND channel_chat_id = ?2 AND thread_id = ?3 AND message_type = ?4 \
                             ORDER BY created_at DESC LIMIT ?5",
                    )?;
                    let rows =
                        stmt.query_map(params![ch, cid, tid, mtype, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
                (Some(ch), Some(tid), None) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE channel = ?1 AND channel_chat_id = ?2 AND thread_id = ?3 \
                             ORDER BY created_at DESC LIMIT ?4",
                    )?;
                    let rows =
                        stmt.query_map(params![ch, cid, tid, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
                (Some(ch), None, Some(mtype)) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE channel = ?1 AND channel_chat_id = ?2 AND message_type = ?3 \
                             ORDER BY created_at DESC LIMIT ?4",
                    )?;
                    let rows = stmt.query_map(params![ch, cid, mtype, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
                (Some(ch), None, None) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE channel = ?1 AND channel_chat_id = ?2 \
                             ORDER BY created_at DESC LIMIT ?3",
                    )?;
                    let rows = stmt.query_map(params![ch, cid, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
                (None, Some(tid), Some(mtype)) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE channel_chat_id = ?1 AND thread_id = ?2 AND message_type = ?3 \
                             ORDER BY created_at DESC LIMIT ?4",
                    )?;
                    let rows =
                        stmt.query_map(params![cid, tid, mtype, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
                (None, Some(tid), None) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE channel_chat_id = ?1 AND thread_id = ?2 \
                             ORDER BY created_at DESC LIMIT ?3",
                    )?;
                    let rows =
                        stmt.query_map(params![cid, tid, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
                (None, None, Some(mtype)) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE channel_chat_id = ?1 AND message_type = ?2 \
                             ORDER BY created_at DESC LIMIT ?3",
                    )?;
                    let rows = stmt.query_map(params![cid, mtype, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
                (None, None, None) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE channel_chat_id = ?1 \
                             ORDER BY created_at DESC LIMIT ?2",
                    )?;
                    let rows = stmt.query_map(params![cid, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
            })
            .await
            .map_err(interact_err)?
            .context("Failed to fetch recent channel messages")
    }

    /// Look up the stored content of a single message by its platform message id
    /// (the Telegram `message_id`). Used to recover the EXACT message a user
    /// replied to — Telegram delivers rich bot messages with empty text, so the
    /// reply handler can't read the quoted content from the update and must look
    /// it up by id instead of guessing "the most recent bot message". Returns
    /// the most recent match (ids are unique per chat, but a defensive ORDER BY
    /// keeps this deterministic). `None` when nothing was stored under that id.
    /// The chat's earliest stored row that carries a platform message id —
    /// the anchor for a bounded PDO history request (#1525). Returns
    /// (platform_message_id, sender_id, created_at).
    pub async fn oldest_for_chat(
        &self,
        channel: &str,
        chat_id: &str,
    ) -> Result<Option<(String, String, chrono::DateTime<chrono::Utc>)>> {
        let ch = channel.to_string();
        let cid = chat_id.to_string();
        let found: Option<(String, String, i64)> = self
            .pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                conn.query_row(
                    "SELECT platform_message_id, sender_id, created_at FROM channel_messages \
                     WHERE channel = ?1 AND channel_chat_id = ?2 \
                       AND platform_message_id IS NOT NULL \
                     ORDER BY created_at ASC LIMIT 1",
                    params![ch, cid],
                    |row| {
                        Ok((
                            row.get::<_, String>(0)?,
                            row.get::<_, String>(1)?,
                            row.get::<_, i64>(2)?,
                        ))
                    },
                )
                .optional()
            })
            .await
            .map_err(interact_err)?
            .context("Failed to look up channel message anchor")?;
        Ok(found.map(|(pid, sid, ts)| {
            (
                pid,
                sid,
                chrono::DateTime::from_timestamp(ts, 0).unwrap_or_default(),
            )
        }))
    }

    /// Read-only bounded history search for one chat (#1525): newest-first
    /// substring rows, within a caller-supplied epoch-seconds window.
    pub async fn search_history(
        &self,
        channel: &str,
        chat_id: &str,
        query: &str,
        since_ts: i64,
        limit: i64,
    ) -> Result<Vec<(String, String, i64)>> {
        let ch = channel.to_string();
        let cid = chat_id.to_string();
        // Escape LIKE wildcards and the escape character itself: a search
        // for "100%" must match a literal percent, not a pattern.
        let escaped = query
            .replace('\\', "\\\\")
            .replace('%', "\\%")
            .replace('_', "\\_");
        let pattern = format!("%{escaped}%");
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                let mut stmt = conn.prepare_cached(
                    "SELECT content, sender_name, created_at FROM channel_messages \
                     WHERE channel = ?1 AND channel_chat_id = ?2 AND created_at >= ?3 \
                       AND content LIKE ?4 ESCAPE '\\' \
                     ORDER BY created_at DESC LIMIT ?5",
                )?;
                let rows = stmt
                    .query_map(params![ch, cid, since_ts, pattern, limit], |row| {
                        Ok((
                            row.get::<_, String>(0)?,
                            row.get::<_, String>(1)?,
                            row.get::<_, i64>(2)?,
                        ))
                    })?
                    .collect::<rusqlite::Result<Vec<_>>>()?;
                Ok::<_, rusqlite::Error>(rows)
            })
            .await
            .map_err(interact_err)?
            .context("Failed to search channel message history")
    }

    /// #1529: per-newsletter poll cursor — (last_server_id, last_ts). Absent
    /// row means "never polled": the poller baselines silently rather than
    /// replaying history at whoever flipped the config on.
    pub async fn newsletter_cursor(&self, wa_jid: &str) -> Result<Option<(i64, i64)>> {
        let j = wa_jid.to_string();
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                conn.query_row(
                    "SELECT last_server_id, last_ts FROM whatsapp_newsletter_cursors \
                     WHERE wa_jid = ?1",
                    params![j],
                    |row| Ok((row.get(0)?, row.get(1)?)),
                )
                .optional()
            })
            .await
            .map_err(interact_err)?
            .context("Failed to read newsletter cursor")
    }

    /// Advance (or baseline) the poll cursor. Upsert: first sight creates the
    /// row, later ticks overwrite it.
    pub async fn set_newsletter_cursor(
        &self,
        wa_jid: &str,
        last_server_id: i64,
        last_ts: i64,
    ) -> Result<()> {
        let j = wa_jid.to_string();
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                conn.execute(
                    "INSERT INTO whatsapp_newsletter_cursors \
                     (wa_jid, last_server_id, last_ts, updated_at) \
                     VALUES (?1, ?2, ?3, strftime('%s','now')) \
                     ON CONFLICT(wa_jid) DO UPDATE SET \
                       last_server_id = excluded.last_server_id, \
                       last_ts = excluded.last_ts, \
                       updated_at = excluded.updated_at",
                    params![j, last_server_id, last_ts],
                )
            })
            .await
            .map_err(interact_err)?
            .context("Failed to write newsletter cursor")
            .map(|_| ())
    }

    pub async fn content_by_platform_message_id(
        &self,
        channel: &str,
        chat_id: &str,
        platform_message_id: &str,
    ) -> Result<Option<String>> {
        let ch = channel.to_string();
        let cid = chat_id.to_string();
        let pmid = platform_message_id.to_string();
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                conn.query_row(
                    "SELECT content FROM channel_messages \
                     WHERE channel = ?1 AND channel_chat_id = ?2 AND platform_message_id = ?3 \
                     ORDER BY created_at DESC LIMIT 1",
                    params![ch, cid, pmid],
                    |row| row.get::<_, String>(0),
                )
                .optional()
            })
            .await
            .map_err(interact_err)?
            .context("Failed to look up channel message by platform id")
    }

    /// The bot message's content AND the forum thread it was posted in.
    ///
    /// A reaction update carries no thread id, and sessions are keyed by
    /// `(chat_id, topic_id)`, so a reaction in a forum topic could not be
    /// matched to its session from the update alone. The row we already read
    /// for the content knows the thread, which makes it recoverable rather
    /// than guessed at.
    pub async fn bot_message_with_thread(
        &self,
        channel: &str,
        chat_id: &str,
        platform_message_id: &str,
    ) -> Result<Option<(String, Option<String>)>> {
        let ch = channel.to_string();
        let cid = chat_id.to_string();
        let pmid = platform_message_id.to_string();
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                conn.query_row(
                    "SELECT content, thread_id FROM channel_messages \
                     WHERE channel = ?1 AND channel_chat_id = ?2 \
                     AND platform_message_id = ?3 AND sender_id = 'bot:opencrabs' \
                     ORDER BY created_at DESC LIMIT 1",
                    params![ch, cid, pmid],
                    |row| Ok((row.get::<_, String>(0)?, row.get::<_, Option<String>>(1)?)),
                )
                .optional()
            })
            .await
            .map_err(interact_err)?
            .context("Failed to look up bot message by platform id")
    }

    /// Like [`content_by_platform_message_id`] but only returns content when the
    /// message was sent by the bot (sender_id = 'bot:opencrabs'). Used by the
    /// inbound reaction handler to skip reactions on user-to-user messages.
    pub async fn bot_content_by_platform_message_id(
        &self,
        channel: &str,
        chat_id: &str,
        platform_message_id: &str,
    ) -> Result<Option<String>> {
        let ch = channel.to_string();
        let cid = chat_id.to_string();
        let pmid = platform_message_id.to_string();
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                conn.query_row(
                    "SELECT content FROM channel_messages \
                     WHERE channel = ?1 AND channel_chat_id = ?2 \
                     AND platform_message_id = ?3 AND sender_id = 'bot:opencrabs' \
                     ORDER BY created_at DESC LIMIT 1",
                    params![ch, cid, pmid],
                    |row| row.get::<_, String>(0),
                )
                .optional()
            })
            .await
            .map_err(interact_err)?
            .context("Failed to look up bot message by platform id")
    }

    /// Most recent non-null forum topic name for a thread, used to label the
    /// session after the topic ("Devops") instead of its numeric thread id
    /// ("topic:2"). Telegram only carries the name on regular topic messages
    /// (via the topic-creation reply target); a user replying to a specific
    /// message inside the topic omits it, so we read the last one we persisted
    /// to keep the label stable.
    pub async fn latest_topic_name(
        &self,
        channel: &str,
        chat_id: &str,
        thread_id: &str,
    ) -> Result<Option<String>> {
        let ch = channel.to_string();
        let cid = chat_id.to_string();
        let tid = thread_id.to_string();
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                // Rename rows (`topic_edited`, #143) outrank creation-name
                // rows: regular messages re-teach the ORIGINAL creation name
                // via their reply target forever, so "newest non-null name"
                // alone re-stales after every rename. A rename is the
                // freshest user intent — if one exists for the thread, its
                // newest instance wins outright; only nameless-of-edit
                // threads fall back to the newest created/learned name.
                conn.query_row(
                    "SELECT topic_name FROM channel_messages \
                         WHERE channel = ?1 AND channel_chat_id = ?2 AND thread_id = ?3 \
                         AND topic_name IS NOT NULL AND topic_name != '' \
                         ORDER BY (message_type = 'topic_edited') DESC, created_at DESC \
                         LIMIT 1",
                    params![ch, cid, tid],
                    |row| row.get::<_, String>(0),
                )
                .optional()
            })
            .await
            .map_err(interact_err)?
            .context("Failed to fetch latest topic name")
    }

    /// Recent user-sent request texts across ALL chats, newest first (#504).
    /// Excludes the bot's own messages (sender_id = 'bot:opencrabs') and
    /// non-text kinds. Feeds the RSI command-pattern detector, which groups
    /// repeated asks into slash-command promotion candidates.
    pub async fn recent_user_requests(&self, limit: i64) -> Result<Vec<String>> {
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                let mut stmt = conn.prepare_cached(
                    "SELECT content FROM channel_messages \
                     WHERE sender_id <> 'bot:opencrabs' AND message_type = 'text' \
                     AND content <> '' \
                     ORDER BY created_at DESC LIMIT ?1",
                )?;
                let rows = stmt.query_map(params![limit], |r| r.get::<_, String>(0))?;
                rows.collect::<std::result::Result<Vec<_>, _>>()
            })
            .await
            .map_err(interact_err)?
            .context("Failed to fetch recent user requests")
    }

    /// Search messages by content (LIKE-based) with optional thread_id filter
    pub async fn search(
        &self,
        channel: Option<&str>,
        chat_id: Option<&str>,
        query: &str,
        limit: i64,
        thread_id: Option<&str>,
    ) -> Result<Vec<ChannelMessage>> {
        let ch = channel.map(|s| s.to_string());
        let cid = chat_id.map(|s| s.to_string());
        let tid = thread_id.map(|s| s.to_string());
        let pattern = format!("%{query}%");

        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| match (ch, cid, tid) {
                (Some(ch), Some(cid), Some(tid)) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE channel = ?1 AND channel_chat_id = ?2 AND content LIKE ?3 AND thread_id = ?4 \
                             ORDER BY created_at DESC LIMIT ?5",
                    )?;
                    let rows =
                        stmt.query_map(params![ch, cid, pattern, tid, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
                (Some(ch), Some(cid), None) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE channel = ?1 AND channel_chat_id = ?2 AND content LIKE ?3 \
                             ORDER BY created_at DESC LIMIT ?4",
                    )?;
                    let rows =
                        stmt.query_map(params![ch, cid, pattern, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
                (Some(ch), None, Some(tid)) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE channel = ?1 AND content LIKE ?2 AND thread_id = ?3 \
                             ORDER BY created_at DESC LIMIT ?4",
                    )?;
                    let rows =
                        stmt.query_map(params![ch, pattern, tid, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
                (Some(ch), None, None) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE channel = ?1 AND content LIKE ?2 \
                             ORDER BY created_at DESC LIMIT ?3",
                    )?;
                    let rows =
                        stmt.query_map(params![ch, pattern, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
                (None, Some(cid), Some(tid)) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE channel_chat_id = ?1 AND content LIKE ?2 AND thread_id = ?3 \
                             ORDER BY created_at DESC LIMIT ?4",
                    )?;
                    let rows =
                        stmt.query_map(params![cid, pattern, tid, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
                (None, Some(cid), None) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE channel_chat_id = ?1 AND content LIKE ?2 \
                             ORDER BY created_at DESC LIMIT ?3",
                    )?;
                    let rows =
                        stmt.query_map(params![cid, pattern, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
                (None, None, Some(tid)) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE content LIKE ?1 AND thread_id = ?2 \
                             ORDER BY created_at DESC LIMIT ?3",
                    )?;
                    let rows =
                        stmt.query_map(params![pattern, tid, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
                (None, None, None) => {
                    let mut stmt = conn.prepare_cached(
                        "SELECT * FROM channel_messages \
                             WHERE content LIKE ?1 \
                             ORDER BY created_at DESC LIMIT ?2",
                    )?;
                    let rows = stmt.query_map(params![pattern, limit], ChannelMessage::from_row)?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
            })
            .await
            .map_err(interact_err)?
            .context("Failed to search channel messages")
    }

    /// List distinct chats with message count and last message time
    pub async fn list_chats(&self, channel: Option<&str>) -> Result<Vec<ChatSummary>> {
        let ch = channel.map(|s| s.to_string());
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                if let Some(ch) = ch {
                    let mut stmt = conn.prepare_cached(
                        "SELECT channel, channel_chat_id, \
                                MAX(channel_chat_name) as channel_chat_name, \
                                COUNT(*) as message_count, \
                                MAX(created_at) as last_message_at \
                         FROM channel_messages \
                         WHERE channel = ?1 \
                         GROUP BY channel, channel_chat_id \
                         ORDER BY last_message_at DESC",
                    )?;
                    let rows = stmt.query_map(params![ch], |row| {
                        Ok(ChatSummary {
                            channel: row.get(0)?,
                            channel_chat_id: row.get(1)?,
                            channel_chat_name: row.get(2)?,
                            message_count: row.get(3)?,
                            last_message_at: row.get(4)?,
                        })
                    })?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                } else {
                    let mut stmt = conn.prepare_cached(
                        "SELECT channel, channel_chat_id, \
                                MAX(channel_chat_name) as channel_chat_name, \
                                COUNT(*) as message_count, \
                                MAX(created_at) as last_message_at \
                         FROM channel_messages \
                         GROUP BY channel, channel_chat_id \
                         ORDER BY last_message_at DESC",
                    )?;
                    let rows = stmt.query_map([], |row| {
                        Ok(ChatSummary {
                            channel: row.get(0)?,
                            channel_chat_id: row.get(1)?,
                            channel_chat_name: row.get(2)?,
                            message_count: row.get(3)?,
                            last_message_at: row.get(4)?,
                        })
                    })?;
                    rows.collect::<std::result::Result<Vec<_>, _>>()
                }
            })
            .await
            .map_err(interact_err)?
            .context("Failed to list channel chats")
    }

    /// Return all distinct forum topics seen in a chat — the
    /// (thread_id, topic_name) pairs captured from incoming messages.
    ///
    /// The bot learns topic names from three sources:
    ///
    /// - `forum_topic_created` service messages it witnesses
    /// - the `reply_to_message().forum_topic_created()` chain that
    ///   Telegram includes on every regular topic message
    /// - `forum_topic_edited` renames, persisted as `topic_edited`
    ///   rows by the handler (#143)
    ///
    /// The name reported per topic is the one from the NEWEST row that
    /// carries a non-null name — not an alphabetical `MAX()`, which
    /// could serve a stale or arbitrarily-ordered name after a rename.
    /// Telegram's Bot API exposes no `listForumTopics` endpoint
    /// — there is no way to enumerate all topics in a supergroup,
    /// only learn them passively as messages arrive.
    ///
    /// Used by `telegram_send`'s `list_topics` action so the agent
    /// can map user-typed names like "#announcements" back to the
    /// numeric `thread_id` it needs to pass to `message_in_thread`.
    pub async fn topics_for_chat(&self, channel: &str, chat_id: &str) -> Result<Vec<TopicSummary>> {
        let ch = channel.to_string();
        let cid = chat_id.to_string();
        self.pool
            .get()
            .await
            .context("Failed to get connection")?
            .interact(move |conn| {
                let mut stmt = conn.prepare_cached(
                    // Every topic row: thread stats from the aggregate,
                    // name from a LEFT JOIN pick so threads whose rows
                    // carry NO name at all still surface (with name NULL)
                    // — a thread_id is sendable even when never named (#143).
                    "SELECT t.thread_id, \
                            np.topic_name, \
                            t.message_count, \
                            t.last_message_at \
                     FROM (\
                         SELECT thread_id, COUNT(*) as message_count, \
                                MAX(created_at) as last_message_at \
                         FROM channel_messages \
                         WHERE channel = ?1 AND channel_chat_id = ?2 AND thread_id IS NOT NULL \
                         GROUP BY thread_id\
                     ) t \
                     LEFT JOIN (\
                         SELECT m.thread_id, m.topic_name \
                         FROM channel_messages m \
                         WHERE m.channel = ?1 AND m.channel_chat_id = ?2 \
                           AND m.topic_name IS NOT NULL AND m.topic_name != '' \
                           AND m.id = (\
                               SELECT m2.id FROM channel_messages m2 \
                               WHERE m2.channel = ?1 AND m2.channel_chat_id = ?2 \
                                 AND m2.thread_id = m.thread_id \
                                 AND m2.topic_name IS NOT NULL AND m2.topic_name != ''\
                                 ORDER BY (m2.message_type = 'topic_edited') DESC, \
                                          m2.created_at DESC, m2.id DESC \
                                 LIMIT 1\
                           ) \
                     ) np ON np.thread_id = t.thread_id \
                     ORDER BY t.last_message_at DESC",
                )?;
                let rows = stmt.query_map(params![ch, cid], |row| {
                    Ok(TopicSummary {
                        thread_id: row.get::<_, Option<String>>(0)?.unwrap_or_default(),
                        topic_name: row.get(1)?,
                        message_count: row.get(2)?,
                        last_message_at: row.get(3)?,
                    })
                })?;
                rows.collect::<std::result::Result<Vec<_>, _>>()
            })
            .await
            .map_err(interact_err)?
            .context("Failed to list topics for chat")
    }
}

/// A forum topic the bot has seen activity in. Surfaced by
/// `ChannelMessageRepository::topics_for_chat` so the agent can
/// translate user-typed topic names to numeric thread_ids.
#[derive(Debug, Clone)]
pub struct TopicSummary {
    pub thread_id: String,
    /// `None` for topics where we only ever saw messages but never
    /// the topic-creation service message and never a reply chain
    /// carrying it. The thread_id is still usable for sending.
    pub topic_name: Option<String>,
    pub message_count: i64,
    /// Unix epoch seconds — matches the column type
    /// `created_at INTEGER` used by every other query in this file.
    pub last_message_at: i64,
}