errand-bot 0.2.0

Run a coding agent from a chat channel, in a sandbox it cannot escape.
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
855
856
857
//! Thread creation, output, and the one case where a thread is closed.
//!
//! The only place that turns session events into calls against the chat
//! service, which is what keeps the session layer free of chat types and
//! testable without a connection.
//!
//! No message gets a link preview: a coding agent quotes URLs constantly, and
//! a preview card for each one buries the conversation. The production
//! transport is the only thing that builds a message, and it always sets the
//! suppress-embeds flag, so a new call site cannot reintroduce embeds by
//! forgetting it.

use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};

use serenity::builder::{
    CreateAllowedMentions, CreateAttachment, CreateMessage, EditMessage, EditThread,
};
use serenity::model::channel::Channel;
use serenity::model::channel::ChannelType;
use serenity::model::channel::MessageFlags;
use serenity::model::channel::ReactionType;
use serenity::model::id::{ChannelId, MessageId};
use tokio::sync::watch;

use crate::chat::chars::reaction;
use crate::chat::diff::FileDiff;
use crate::chat::diff::render_diff;
use crate::chat::outbox::{DEFAULT_MAX_BUFFERED, Outbox, TaskError};
use crate::chat::render::{MESSAGE_LIMIT, delegation_line, split_message};
use crate::log::{LogValue, Logger, fields};
use crate::session::event::Delegated;
use crate::session::event::ToolResult;
use crate::session::event::{EndReason, ReactionOutcome, SessionEvent};
use crate::session::views::SessionView;
use crate::session::views::ViewError;

/// The service expires a typing indicator after about ten seconds.
pub const TYPING_REFRESH_MS: u64 = 8_000;

/// Everything one send or edit leaves behind, for later edits and reactions.
#[derive(Debug, Clone, PartialEq)]
pub struct MessageHandle {
    /// The service's own id for the message, which an edit needs.
    pub id: String,
}

/// Why a call to the chat service failed.
pub type ServiceError = Box<dyn std::error::Error + Send + Sync>;

/// A call to the chat service, as a boxed future.
pub type ServiceCall<T> = Pin<Box<dyn Future<Output = Result<T, ServiceError>> + Send>>;

/// What a thread does against the chat service.
///
/// One production implementation talks to serenity's REST client; a test
/// hands the thread a recording double, which is the seam the original's
/// fake channel sat in.
pub trait ThreadTransport: Send + Sync + 'static {
    /// Sends a message, optionally carrying a named file. Returns its id.
    fn send(
        &self,
        content: String,
        attachment: Option<(String, Vec<u8>)>,
    ) -> ServiceCall<MessageHandle>;

    /// Edits a message's text.
    fn edit(&self, message_id: String, content: String) -> ServiceCall<()>;

    /// Deletes a message.
    fn delete(&self, message_id: String) -> ServiceCall<()>;

    /// Reports whether a message still exists.
    fn fetch(&self, message_id: String) -> ServiceCall<Option<MessageHandle>>;

    /// Adds a reaction named by its glyph.
    fn react(&self, message_id: String, glyph: String) -> ServiceCall<()>;

    /// Removes one reaction, the daemon's own, from a message.
    fn remove_reaction(&self, message_id: String, glyph: String) -> ServiceCall<()>;

    /// Archives or reopens the thread.
    fn set_archived(&self, archived: bool) -> ServiceCall<()>;

    /// Shows the typing indicator once; holding it is the caller's job.
    fn send_typing(&self) -> ServiceCall<()>;
}

/// Who a message the daemon posts may notify.
///
/// The daemon addresses a person on purpose, to say a turn they asked for has
/// settled, so user mentions are parsed. Nothing it posts ever means to reach
/// everyone or a role, and most of what it posts is the agent's words or a
/// person's own: without this, text that merely happens to contain the
/// everyone or a role syntax would notify them.
fn addressed_to_people() -> CreateAllowedMentions {
    CreateAllowedMentions::new()
        .all_users(true)
        .all_roles(false)
        .everyone(false)
}

/// The message options for the production transport.
///
/// The one way a message is built, so neither the suppress-embeds flag nor
/// the mention rule can be lost at a new call site.
pub fn plain(content: &str) -> CreateMessage {
    CreateMessage::new()
        .content(content.to_owned())
        .flags(MessageFlags::SUPPRESS_EMBEDS)
        .allowed_mentions(addressed_to_people())
}

/// Parses one message id and one reaction glyph, or fails trying.
fn prepare_reaction(
    message_id: &str,
    glyph: String,
) -> Result<(u64, ReactionType), std::num::ParseIntError> {
    let id = message_id.parse::<u64>()?;
    let glyph = ReactionType::try_from(glyph.as_str()).unwrap_or(ReactionType::Unicode(glyph));
    Ok((id, glyph))
}

/// A call that failed before it could be made.
fn parse_failure(error: std::num::ParseIntError) -> ServiceCall<()> {
    Box::pin(async move { Err(Box::new(error) as ServiceError) })
}

/// The production transport, over serenity's REST client.
#[derive(Clone)]
pub struct SerenityThread {
    thread_id: ChannelId,
    http: Arc<serenity::http::Http>,
}

impl SerenityThread {
    /// A transport for one thread channel.
    pub fn new(thread_id: ChannelId, http: Arc<serenity::http::Http>) -> Self {
        Self { thread_id, http }
    }
}

impl ThreadTransport for SerenityThread {
    fn send(
        &self,
        content: String,
        attachment: Option<(String, Vec<u8>)>,
    ) -> ServiceCall<MessageHandle> {
        let thread_id = self.thread_id;
        let http = Arc::clone(&self.http);
        Box::pin(async move {
            let mut message = plain(&content);
            if let Some((name, bytes)) = attachment {
                message = message.files(vec![CreateAttachment::bytes(bytes, name)]);
            }
            let sent = thread_id.send_message(&http, message).await?;
            Ok(MessageHandle {
                id: sent.id.get().to_string(),
            })
        })
    }

    fn edit(&self, message_id: String, content: String) -> ServiceCall<()> {
        let thread_id = self.thread_id;
        let http = Arc::clone(&self.http);
        let id = match message_id.parse::<u64>() {
            Ok(id) => id,
            Err(error) => return parse_failure(error),
        };
        Box::pin(async move {
            let edited = EditMessage::new()
                .content(content)
                .flags(MessageFlags::SUPPRESS_EMBEDS)
                .allowed_mentions(addressed_to_people());
            thread_id
                .edit_message(&http, MessageId::new(id), edited)
                .await?;
            Ok(())
        })
    }

    fn delete(&self, message_id: String) -> ServiceCall<()> {
        let thread_id = self.thread_id;
        let http = Arc::clone(&self.http);
        let id = match message_id.parse::<u64>() {
            Ok(id) => id,
            Err(error) => return parse_failure(error),
        };
        Box::pin(async move {
            thread_id.delete_message(&http, MessageId::new(id)).await?;
            Ok(())
        })
    }

    fn fetch(&self, message_id: String) -> ServiceCall<Option<MessageHandle>> {
        let thread_id = self.thread_id;
        let http = Arc::clone(&self.http);
        let Ok(id) = message_id.parse::<u64>() else {
            return Box::pin(async { Ok(None) });
        };
        Box::pin(async move {
            match thread_id.message(&http, MessageId::new(id)).await {
                Ok(found) => Ok(Some(MessageHandle {
                    id: found.id.get().to_string(),
                })),
                Err(_) => Ok(None),
            }
        })
    }

    fn react(&self, message_id: String, glyph: String) -> ServiceCall<()> {
        let thread_id = self.thread_id;
        let http = Arc::clone(&self.http);
        let (id, glyph) = match prepare_reaction(&message_id, glyph) {
            Ok(pair) => pair,
            Err(error) => return parse_failure(error),
        };
        Box::pin(async move {
            thread_id
                .create_reaction(&http, MessageId::new(id), glyph)
                .await?;
            Ok(())
        })
    }

    fn remove_reaction(&self, message_id: String, glyph: String) -> ServiceCall<()> {
        let thread_id = self.thread_id;
        let http = Arc::clone(&self.http);
        let (id, glyph) = match prepare_reaction(&message_id, glyph) {
            Ok(pair) => pair,
            Err(error) => return parse_failure(error),
        };
        Box::pin(async move {
            thread_id
                .delete_reaction(&http, MessageId::new(id), None, glyph)
                .await?;
            Ok(())
        })
    }

    fn set_archived(&self, archived: bool) -> ServiceCall<()> {
        let thread_id = self.thread_id;
        let http = Arc::clone(&self.http);
        Box::pin(async move {
            thread_id
                .edit_thread(&http, EditThread::new().archived(archived))
                .await?;
            Ok(())
        })
    }

    fn send_typing(&self) -> ServiceCall<()> {
        let thread_id = self.thread_id;
        let http = Arc::clone(&self.http);
        Box::pin(async move {
            // Not `start_typing`: that returns a guard which stops the
            // indicator when it is dropped, so a caller that wants one
            // moment of typing and repeats it gets none at all. This posts
            // the one moment, which the service expires on its own.
            http.broadcast_typing(thread_id).await?;
            Ok(())
        })
    }
}

/// One gap announcement, as a boxed future.
pub type Announce =
    dyn Fn(usize) -> Pin<Box<dyn Future<Output = Result<(), TaskError>> + Send>> + Send + Sync;

/// The thread's mutable side: what it is showing and holding.
struct ThreadState {
    waiting_message_id: Option<String>,
    reactions: HashMap<String, String>,
    activity_message_id: Option<String>,
    activity_text: String,
    typing: Option<tokio::task::JoinHandle<()>>,
}

/// A thread a session posts to.
///
/// Reactions are replaced rather than accumulated: a scrolled-back thread
/// should read as final state, not as a history of transitions.
pub struct ChatThread<T: ThreadTransport> {
    transport: Arc<T>,
    log: Logger,
    forward_tool_output: bool,
    outbox: Outbox,
    state: Arc<Mutex<ThreadState>>,
}

impl<T: ThreadTransport> ChatThread<T> {
    /// A thread over `transport`, announcing dropped work to itself.
    pub fn new(transport: Arc<T>, log: Logger, forward_tool_output: bool) -> Self {
        let announcer: Arc<Announce> = {
            let transport = transport.clone();
            Arc::new(move |count| {
                let transport = transport.clone();
                Box::pin(async move {
                    transport
                        .send(
                            format!("[{count} earlier message(s) dropped while disconnected]"),
                            None,
                        )
                        .await
                        .map(|_| ())
                })
            })
        };
        Self {
            transport,
            outbox: Outbox::new(log.clone(), announcer, DEFAULT_MAX_BUFFERED),
            log,
            forward_tool_output,
            state: Arc::new(Mutex::new(ThreadState {
                waiting_message_id: None,
                reactions: HashMap::new(),
                activity_message_id: None,
                activity_text: String::new(),
                typing: None,
            })),
        }
    }

    /// True once closed, after which nothing more will ever be sent.
    ///
    /// Closing closes the outbox, which is correct for a session that is over
    /// and fatal for one that is starting: a reused closed port accepts posts
    /// and silently drops every one of them.
    #[allow(
        dead_code,
        reason = "read by this module's tests, which assert on state the daemon never asks for"
    )]
    pub fn is_closed(&self) -> bool {
        self.outbox.is_closed()
    }

    /// Waits for queued work to run. Used by tests, which have no connection.
    #[allow(
        dead_code,
        reason = "read by this module's tests, which assert on state the daemon never asks for"
    )]
    pub async fn flush(&self) {
        self.outbox.flush().await;
    }

    /// Shows a typing indicator while the agent is working.
    ///
    /// The service expires the indicator after about ten seconds, so holding
    /// it means repeating it. This is the only signal during a long tool loop
    /// that the session is alive rather than stuck.
    pub fn set_busy(&self, busy: bool) {
        let mut state = self.state.lock().expect("the thread state lock");
        if busy {
            if state.typing.is_some() {
                return;
            }
            let transport = Arc::clone(&self.transport);
            let log = self.log.clone();
            tokio::spawn(async move {
                if let Err(error) = transport.send_typing().await {
                    log.warn(
                        "the typing indicator could not be shown",
                        &fields([("detail", LogValue::from(error.to_string()))]),
                    );
                }
            });
            state.typing = Some(tokio::spawn(typing_loop(self.transport.clone())));
            return;
        }

        if let Some(running) = state.typing.take() {
            running.abort();
        }
    }

    /// Posts the message, split into pieces the service will take.
    pub fn post(&self, text: &str) {
        // The agent speaking ends the current run of tool calls, so the next
        // one starts a fresh block rather than being appended below prose.
        self.reset_activity();
        for chunk in split_message(text, MESSAGE_LIMIT) {
            let transport = Arc::clone(&self.transport);
            let chunk = chunk.clone();
            self.outbox.enqueue(Box::new(move || {
                let transport = Arc::clone(&transport);
                let chunk = chunk.clone();
                Box::pin(async move { transport.send(chunk, None).await.map(|_| ()) })
                    as Pin<Box<dyn Future<Output = Result<(), TaskError>> + Send>>
            }));
        }
    }

    /// Renders a change as a fenced diff, which is all a thread can show.
    pub fn post_diff(&self, path: &str, added: u64, removed: u64, body: &str) {
        #[expect(
            clippy::cast_possible_truncation,
            reason = "diff line counts sit far below any pointer-width limit"
        )]
        let diff = FileDiff {
            empty: false,
            added: added as usize,
            removed: removed as usize,
            body: body.to_owned(),
        };
        self.post(&render_diff(path, &diff));
    }

    /// Shows that a cheaper model was asked something.
    ///
    /// One line, not the answer: the answer goes to the agent, and what a
    /// reader needs is that part of this turn was not the session's own
    /// model.
    pub fn note_delegation(&self, delegated: &Delegated) {
        self.post(&delegation_line(delegated));
    }

    /// Posts what a tool produced, when the thread is configured to forward
    /// it.
    ///
    /// A thread cannot attach output to the call above it, so forwarding is
    /// the only way to show it there, and it stays off by default because it
    /// is a lot of text in a conversation.
    pub fn note_tool_result(&self, result: &ToolResult) {
        if !self.forward_tool_output {
            return;
        }
        let body = result.output.trim();
        if body.is_empty() {
            return;
        }
        self.post(&format!("```\n{body}\n```"));
    }

    /// Adds a line of tool activity, extending the current block when there
    /// is one.
    ///
    /// A run of tool calls is one thing the agent is doing, not ten. Editing
    /// one message keeps it as one block, and keeps a busy turn from pushing
    /// the agent's own words off the screen.
    pub fn append_activity(&self, line: &str) {
        let line = line.to_owned();
        let transport = Arc::clone(&self.transport);
        let state = Arc::clone(&self.state);
        self.outbox.enqueue(Box::new(move || {
            let transport = Arc::clone(&transport);
            let state = Arc::clone(&state);
            let line = line.clone();
            Box::pin(async move {
                let extended = {
                    let state = state.lock().expect("the thread state lock");
                    if state.activity_text.is_empty() {
                        line.clone()
                    } else {
                        format!("{}\n{line}", state.activity_text)
                    }
                };

                let fresh = {
                    let mut state = state.lock().expect("the thread state lock");
                    let fresh = state.activity_message_id.is_none()
                        || extended.chars().count() > MESSAGE_LIMIT;
                    if !fresh {
                        state.activity_text.clear();
                        state.activity_text.push_str(&extended);
                    }
                    fresh
                };

                if !fresh {
                    let id = state
                        .lock()
                        .expect("the thread state lock")
                        .activity_message_id
                        .clone()
                        .unwrap_or_default();
                    transport.edit(id, extended).await?;
                    return Ok(());
                }

                // One entry can exceed a whole message on its own. Nothing is
                // dropped: it is split, and the block continues from the
                // final piece.
                let pieces = split_message(&line, MESSAGE_LIMIT);
                let mut last: Option<MessageHandle> = None;
                for piece in &pieces {
                    let sent = transport.send(piece.clone(), None).await?;
                    last = Some(sent);
                }
                let mut state = state.lock().expect("the thread state lock");
                state.activity_message_id = last.map(|sent| sent.id);
                state.activity_text = pieces.last().cloned().unwrap_or_default();
                Ok(())
            })
        }));
    }

    /// Buffers while the gateway is down and drains when it comes back.
    pub fn follow(&self, connection: watch::Receiver<bool>) {
        self.outbox.follow(connection);
    }

    /// Creates, updates, or takes away the one message reporting queue
    /// position.
    pub async fn set_waiting(&self, text: Option<&str>) {
        match text {
            None => {
                let id = self
                    .state
                    .lock()
                    .expect("the thread state lock")
                    .waiting_message_id
                    .take();
                if let Some(id) = id
                    && let Err(error) = self.transport.delete(id).await
                {
                    // A missing waiting message is cosmetic; the session
                    // continues.
                    self.warn_waiting(&error);
                }
            }
            Some(text) => {
                let shown = format!("[{text}]");
                let existing = self
                    .state
                    .lock()
                    .expect("the thread state lock")
                    .waiting_message_id
                    .clone();
                match existing {
                    None => match self.transport.send(shown, None).await {
                        Ok(sent) => {
                            self.state
                                .lock()
                                .expect("the thread state lock")
                                .waiting_message_id = Some(sent.id);
                        }
                        Err(error) => self.warn_waiting(&error),
                    },
                    Some(id) => {
                        if let Err(error) = self.transport.edit(id, shown).await {
                            self.warn_waiting(&error);
                            self.state
                                .lock()
                                .expect("the thread state lock")
                                .waiting_message_id = None;
                        }
                    }
                }
            }
        }
    }

    fn warn_waiting(&self, error: &dyn std::fmt::Display) {
        self.log.warn(
            "could not update the waiting message",
            &fields([("detail", LogValue::from(error.to_string()))]),
        );
    }

    /// Sets the outcome reaction on a message, replacing any earlier one.
    pub async fn set_reaction(&self, message_id: &str, outcome: ReactionOutcome) {
        let glyph = reaction(outcome.into());
        let previous = {
            let state = self.state.lock().expect("the thread state lock");
            state.reactions.get(message_id).cloned()
        };
        if previous.as_deref() == Some(glyph.as_str()) {
            return;
        }

        let found = self
            .transport
            .fetch(message_id.to_owned())
            .await
            .ok()
            .flatten();
        let Some(_) = found else { return };

        if let Some(previous) = &previous
            && let Err(error) = self
                .transport
                .remove_reaction(message_id.to_owned(), previous.clone())
                .await
        {
            // A reaction is an acknowledgement, not the work. Losing one
            // must not disturb the session it was acknowledging.
            self.warn_reaction(&error);
            return;
        }
        match self
            .transport
            .react(message_id.to_owned(), glyph.clone())
            .await
        {
            Ok(()) => {
                self.state
                    .lock()
                    .expect("the thread state lock")
                    .reactions
                    .insert(message_id.to_owned(), glyph);
            }
            Err(error) => self.warn_reaction(&error),
        }
    }

    fn warn_reaction(&self, error: &dyn std::fmt::Display) {
        self.log.warn(
            "could not set a reaction",
            &fields([("detail", LogValue::from(error.to_string()))]),
        );
    }

    /// Uploads a file with its caption.
    ///
    /// The bytes are taken by value and cloned into the queued task, so the
    /// caller keeps what it passed.
    pub fn upload(&self, name: &str, bytes: Vec<u8>, caption: &str) {
        self.reset_activity();
        let transport = Arc::clone(&self.transport);
        let name = name.to_owned();
        let caption = caption.to_owned();
        self.outbox.enqueue(Box::new(move || {
            let transport = Arc::clone(&transport);
            let name = name.clone();
            let bytes = bytes.clone();
            let caption = caption.clone();
            Box::pin(async move {
                transport
                    .send(caption, Some((name, bytes)))
                    .await
                    .map(|_| ())
            })
        }));
    }

    /// Finishes with the thread, archiving it only when somebody said to.
    ///
    /// A thread archived because its session idled out drops off the sidebar,
    /// and the people who were in it have to go hunting for it. Every reason
    /// but a deliberate stop can also be resumed, so the thread is left where
    /// it is and the last notice in it says what happened.
    pub async fn close(&self, reason: EndReason) {
        self.set_busy(false);
        self.state
            .lock()
            .expect("the thread state lock")
            .activity_message_id = None;
        self.outbox.flush().await;
        self.outbox.close();

        if reason != EndReason::Stopped {
            return;
        }
        if let Err(error) = self.transport.set_archived(true).await {
            self.log.warn(
                "could not archive the thread",
                &fields([("detail", LogValue::from(error.to_string()))]),
            );
        }
    }

    fn reset_activity(&self) {
        let state = Arc::clone(&self.state);
        self.outbox.enqueue(Box::new(move || {
            let state = Arc::clone(&state);
            Box::pin(async move {
                let mut state = state.lock().expect("the thread state lock");
                state.activity_message_id = None;
                state.activity_text = String::new();
                Ok(())
            })
        }));
    }
}

impl<T: ThreadTransport> SessionView for ChatThread<T> {
    fn observe<'a>(
        &'a self,
        event: &'a SessionEvent,
    ) -> Pin<Box<dyn Future<Output = Result<(), ViewError>> + Send + 'a>> {
        Box::pin(async move {
            match event {
                // A thread has one voice, so a notice is posted like
                // anything else; so is a reply, whose command already sits
                // above it in the thread.
                SessionEvent::Post { text }
                | SessionEvent::Notice { text, .. }
                | SessionEvent::Reply { text, .. } => self.post(text),
                SessionEvent::Diff {
                    path,
                    added,
                    removed,
                    body,
                    ..
                } => self.post_diff(path, *added, *removed, body),
                SessionEvent::Activity { line, .. } => self.append_activity(line),
                SessionEvent::ToolResult { result } => self.note_tool_result(result),
                SessionEvent::Delegation { delegated } => self.note_delegation(delegated),
                SessionEvent::Waiting { text } => {
                    self.set_waiting(text.as_deref()).await;
                }
                SessionEvent::Reaction {
                    message_id,
                    outcome,
                } => self.set_reaction(message_id, *outcome).await,
                SessionEvent::Upload { name, bytes, caption } => {
                    self.upload(name, bytes.clone(), caption);
                }
                SessionEvent::Busy { busy } => self.set_busy(*busy),
                SessionEvent::Close { reason } => self.close(*reason).await,
                // The prompt is already in the thread, posted by the person
                // who wrote it; an aside likewise; reasoning is long and a
                // thread is a conversation; usage is a dashboard thing; and
                // turn boundaries are plain from the messages themselves.
                SessionEvent::Prompt { .. }
                | SessionEvent::Aside { .. }
                | SessionEvent::Thinking { .. }
                | SessionEvent::Usage { .. }
                | SessionEvent::BeginTurn { .. }
                // An attachment is recorded for surfaces that can show one;
                // the upload event above already carried the file.
                | SessionEvent::Attachment { .. } => {}
            }
            Ok(())
        })
    }
}

/// Repeats the typing indicator for as long as the task lives.
async fn typing_loop<T: ThreadTransport>(transport: Arc<T>) {
    let mut tick = tokio::time::interval(std::time::Duration::from_millis(TYPING_REFRESH_MS));
    tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
    // The interval's first tick is immediate, and the ping already went out
    // when the turn started.
    tick.tick().await;
    loop {
        tick.tick().await;
        // A missed refresh is cosmetic; the next tick is seconds away.
        if transport.send_typing().await.is_err() {
            return;
        }
    }
}

#[cfg(test)]
mod tests;

/// Creates threads on the served channel, one per session.
pub struct ChatThreadFactory {
    channel_id: ChannelId,
    http: Arc<serenity::http::Http>,
    log: Logger,
    /// Whether a thread shows what tools produced. Off unless configured.
    forward_tool_output: bool,
    /// Whether the gateway is up, which every port it builds follows.
    connection: watch::Receiver<bool>,
}

impl ChatThreadFactory {
    /// A factory for the served channel.
    pub fn new(
        channel_id: ChannelId,
        http: Arc<serenity::http::Http>,
        log: Logger,
        forward_tool_output: bool,
        connection: watch::Receiver<bool>,
    ) -> Self {
        Self {
            channel_id,
            http,
            log,
            forward_tool_output,
            connection,
        }
    }

    fn adopt(&self, thread_id: ChannelId) -> ChatThread<SerenityThread> {
        let thread = ChatThread::new(
            Arc::new(SerenityThread::new(thread_id, Arc::clone(&self.http))),
            self.log.clone(),
            self.forward_tool_output,
        );
        thread.follow(self.connection.clone());
        thread
    }

    /// Starts a thread from the message that asked for the session.
    pub async fn create(
        &self,
        starter: MessageId,
        name: &str,
    ) -> Result<(String, ChatThread<SerenityThread>), String> {
        let thread = self
            .channel_id
            .create_thread_from_message(
                &self.http,
                starter,
                serenity::builder::CreateThread::new(name)
                    .auto_archive_duration(serenity::model::channel::AutoArchiveDuration::OneDay),
            )
            .await
            .map_err(|error| error.to_string())?;
        Ok((thread.id.get().to_string(), self.adopt(thread.id)))
    }

    /// Opens a thread with no message to hang it on, by posting one first.
    ///
    /// A thread hangs off a message, so one is posted first. That message is
    /// also what tells the channel that work has started somewhere else.
    pub async fn open(
        &self,
        name: &str,
        opener: &str,
    ) -> Result<(String, ChatThread<SerenityThread>), String> {
        let starter = self
            .channel_id
            .send_message(&self.http, plain(opener))
            .await
            .map_err(|error| error.to_string())?;
        self.create(starter.id, name).await
    }

    /// A thread for one that already exists, adopted by id.
    ///
    /// A resumed thread was created by a previous run of the daemon, so it is
    /// fetched rather than read from the cache, which is cold after a
    /// restart. A thread archived by the service's own inactivity is
    /// reopened, since somebody writing in it is asking for exactly that.
    pub async fn port_for(&self, thread_id: ChannelId) -> Option<ChatThread<SerenityThread>> {
        let channel = thread_id.to_channel(&self.http).await.ok()?;
        let Channel::Guild(thread) = channel else {
            return None;
        };
        if !matches!(
            thread.kind,
            ChannelType::PublicThread | ChannelType::PrivateThread | ChannelType::NewsThread
        ) {
            return None;
        }
        if thread
            .thread_metadata
            .as_ref()
            .is_some_and(|metadata| metadata.archived)
            && let Err(error) = thread_id
                .edit_thread(&self.http, EditThread::new().archived(false))
                .await
        {
            self.log.warn(
                "could not reopen a thread",
                &fields([("detail", LogValue::from(error.to_string()))]),
            );
        }
        Some(self.adopt(thread_id))
    }
}