tuitbot-core 0.1.47

Core library for Tuitbot autonomous X growth assistant
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
//! Content loop for posting original educational tweets.
//!
//! Generates and posts original educational tweets on a configurable
//! schedule, keeping the user's X account active with thought-leadership
//! content. Rotates through configured topics to avoid repetition.
//!
//! # Module layout
//!
//! | File            | Responsibility                                        |
//! |-----------------|-------------------------------------------------------|
//! | `mod.rs`        | Struct definition, constructors, shared test mocks   |
//! | `generator.rs`  | Tweet generation, topic selection, text utilities     |
//! | `scheduler.rs`  | Run loop, iteration logic, slot/interval scheduling   |
//! | `publisher.rs`  | Scheduled-content posting (single tweets & threads)   |

mod generator;
mod publisher;
mod scheduler;
#[cfg(test)]
mod tests_guardrails; // Task 3.5: safety guardrails + publisher tests

use super::loop_helpers::{
    ContentSafety, ContentStorage, ThreadPoster, TopicScorer, TweetGenerator,
};
use std::sync::Arc;

/// Fraction of the time to exploit top-performing topics (vs. explore random ones).
pub(super) const EXPLOIT_RATIO: f64 = 0.8;

/// Content loop that generates and posts original educational tweets.
pub struct ContentLoop {
    pub(super) generator: Arc<dyn TweetGenerator>,
    pub(super) safety: Arc<dyn ContentSafety>,
    pub(super) storage: Arc<dyn ContentStorage>,
    pub(super) topic_scorer: Option<Arc<dyn TopicScorer>>,
    pub(super) thread_poster: Option<Arc<dyn ThreadPoster>>,
    pub(super) topics: Vec<String>,
    pub(super) post_window_secs: u64,
    pub(super) dry_run: bool,
}

/// Result of a content generation attempt.
#[derive(Debug)]
pub enum ContentResult {
    /// Tweet was posted (or would be in dry-run).
    Posted { topic: String, content: String },
    /// Skipped because not enough time has elapsed since last tweet.
    TooSoon { elapsed_secs: u64, window_secs: u64 },
    /// Skipped due to daily tweet rate limit.
    RateLimited,
    /// No topics configured.
    NoTopics,
    /// Generation failed.
    Failed { error: String },
}

impl ContentLoop {
    /// Create a new content loop.
    pub fn new(
        generator: Arc<dyn TweetGenerator>,
        safety: Arc<dyn ContentSafety>,
        storage: Arc<dyn ContentStorage>,
        topics: Vec<String>,
        post_window_secs: u64,
        dry_run: bool,
    ) -> Self {
        Self {
            generator,
            safety,
            storage,
            topic_scorer: None,
            thread_poster: None,
            topics,
            post_window_secs,
            dry_run,
        }
    }

    /// Set a topic scorer for epsilon-greedy topic selection.
    ///
    /// When set, 80% of the time the loop picks from top-performing topics
    /// (exploit), and 20% of the time it picks a random topic (explore).
    pub fn with_topic_scorer(mut self, scorer: Arc<dyn TopicScorer>) -> Self {
        self.topic_scorer = Some(scorer);
        self
    }

    /// Set a thread poster for posting scheduled threads as reply chains.
    pub fn with_thread_poster(mut self, poster: Arc<dyn ThreadPoster>) -> Self {
        self.thread_poster = Some(poster);
        self
    }
}

// ---------------------------------------------------------------------------
// Shared test mocks (accessible to all child test modules).
// ---------------------------------------------------------------------------

#[cfg(test)]
pub(super) mod test_mocks {
    use crate::automation::loop_helpers::{
        ContentSafety, ContentStorage, TopicScorer, TweetGenerator,
    };
    use crate::automation::ContentLoopError;
    use std::sync::Mutex;

    // --- generators ---

    pub struct MockGenerator {
        pub response: String,
    }

    #[async_trait::async_trait]
    impl TweetGenerator for MockGenerator {
        async fn generate_tweet(&self, _topic: &str) -> Result<String, ContentLoopError> {
            Ok(self.response.clone())
        }
    }

    pub struct OverlongGenerator {
        pub first_response: String,
        pub retry_response: String,
        pub call_count: Mutex<usize>,
    }

    #[async_trait::async_trait]
    impl TweetGenerator for OverlongGenerator {
        async fn generate_tweet(&self, _topic: &str) -> Result<String, ContentLoopError> {
            let mut count = self.call_count.lock().expect("lock");
            *count += 1;
            if *count == 1 {
                Ok(self.first_response.clone())
            } else {
                Ok(self.retry_response.clone())
            }
        }
    }

    pub struct FailingGenerator;

    #[async_trait::async_trait]
    impl TweetGenerator for FailingGenerator {
        async fn generate_tweet(&self, _topic: &str) -> Result<String, ContentLoopError> {
            Err(ContentLoopError::LlmFailure(
                "model unavailable".to_string(),
            ))
        }
    }

    // --- safety ---

    pub struct MockSafety {
        pub can_tweet: bool,
        pub can_thread: bool,
    }

    #[async_trait::async_trait]
    impl ContentSafety for MockSafety {
        async fn can_post_tweet(&self) -> bool {
            self.can_tweet
        }
        async fn can_post_thread(&self) -> bool {
            self.can_thread
        }
    }

    // --- storage ---

    pub struct MockStorage {
        pub last_tweet: Mutex<Option<chrono::DateTime<chrono::Utc>>>,
        pub posted_tweets: Mutex<Vec<(String, String)>>,
        pub actions: Mutex<Vec<(String, String, String)>>,
    }

    impl MockStorage {
        pub fn new(last_tweet: Option<chrono::DateTime<chrono::Utc>>) -> Self {
            Self {
                last_tweet: Mutex::new(last_tweet),
                posted_tweets: Mutex::new(Vec::new()),
                actions: Mutex::new(Vec::new()),
            }
        }

        pub fn posted_count(&self) -> usize {
            self.posted_tweets.lock().expect("lock").len()
        }

        pub fn action_count(&self) -> usize {
            self.actions.lock().expect("lock").len()
        }
    }

    #[async_trait::async_trait]
    impl ContentStorage for MockStorage {
        async fn last_tweet_time(
            &self,
        ) -> Result<Option<chrono::DateTime<chrono::Utc>>, ContentLoopError> {
            Ok(*self.last_tweet.lock().expect("lock"))
        }

        async fn last_thread_time(
            &self,
        ) -> Result<Option<chrono::DateTime<chrono::Utc>>, ContentLoopError> {
            Ok(None)
        }

        async fn todays_tweet_times(
            &self,
        ) -> Result<Vec<chrono::DateTime<chrono::Utc>>, ContentLoopError> {
            Ok(Vec::new())
        }

        async fn post_tweet(&self, topic: &str, content: &str) -> Result<(), ContentLoopError> {
            self.posted_tweets
                .lock()
                .expect("lock")
                .push((topic.to_string(), content.to_string()));
            Ok(())
        }

        async fn create_thread(
            &self,
            _topic: &str,
            _tweet_count: usize,
        ) -> Result<String, ContentLoopError> {
            Ok("thread-1".to_string())
        }

        async fn update_thread_status(
            &self,
            _thread_id: &str,
            _status: &str,
            _tweet_count: usize,
            _root_tweet_id: Option<&str>,
        ) -> Result<(), ContentLoopError> {
            Ok(())
        }

        async fn store_thread_tweet(
            &self,
            _thread_id: &str,
            _position: usize,
            _tweet_id: &str,
            _content: &str,
        ) -> Result<(), ContentLoopError> {
            Ok(())
        }

        async fn log_action(
            &self,
            action_type: &str,
            status: &str,
            message: &str,
        ) -> Result<(), ContentLoopError> {
            self.actions.lock().expect("lock").push((
                action_type.to_string(),
                status.to_string(),
                message.to_string(),
            ));
            Ok(())
        }
    }

    // --- topic scorer ---

    pub struct MockTopicScorer {
        pub top_topics: Vec<String>,
    }

    #[async_trait::async_trait]
    impl TopicScorer for MockTopicScorer {
        async fn get_top_topics(&self, _limit: u32) -> Result<Vec<String>, ContentLoopError> {
            Ok(self.top_topics.clone())
        }
    }

    pub struct FailingTopicScorer;

    #[async_trait::async_trait]
    impl TopicScorer for FailingTopicScorer {
        async fn get_top_topics(&self, _limit: u32) -> Result<Vec<String>, ContentLoopError> {
            Err(ContentLoopError::StorageError("db error".to_string()))
        }
    }

    // --- RNG helper ---

    /// RNG wrapper that overrides only the first `next_u64()` call,
    /// then delegates everything to a real ThreadRng. This lets us
    /// control the initial `gen::<f64>()` roll without breaking
    /// subsequent `choose()` / `gen_range()` calls.
    pub struct FirstCallRng {
        pub first_u64: Option<u64>,
        pub inner: rand::rngs::ThreadRng,
    }

    impl FirstCallRng {
        /// Create an RNG whose first `gen::<f64>()` returns ~0.0 (exploit).
        pub fn low_roll() -> Self {
            Self {
                first_u64: Some(0),
                inner: rand::rng(),
            }
        }

        /// Create an RNG whose first `gen::<f64>()` returns ~1.0 (explore).
        pub fn high_roll() -> Self {
            Self {
                first_u64: Some(u64::MAX),
                inner: rand::rng(),
            }
        }
    }

    impl rand::RngCore for FirstCallRng {
        fn next_u32(&mut self) -> u32 {
            self.inner.next_u32()
        }
        fn next_u64(&mut self) -> u64 {
            if let Some(val) = self.first_u64.take() {
                val
            } else {
                self.inner.next_u64()
            }
        }
        fn fill_bytes(&mut self, dest: &mut [u8]) {
            self.inner.fill_bytes(dest);
        }
    }

    // --- fixtures ---

    pub fn make_topics() -> Vec<String> {
        vec![
            "Rust".to_string(),
            "CLI tools".to_string(),
            "Open source".to_string(),
            "Developer productivity".to_string(),
        ]
    }
}

#[cfg(test)]
mod tests_content_loop {
    use super::test_mocks::{make_topics, MockGenerator, MockSafety, MockStorage, MockTopicScorer};
    use super::{ContentLoop, ContentResult, EXPLOIT_RATIO};
    use std::sync::Arc;

    #[test]
    fn exploit_ratio_value() {
        assert!((EXPLOIT_RATIO - 0.8).abs() < f64::EPSILON);
    }

    #[test]
    fn content_loop_new_fields() {
        let content = ContentLoop::new(
            Arc::new(MockGenerator {
                response: "tweet".to_string(),
            }),
            Arc::new(MockSafety {
                can_tweet: true,
                can_thread: true,
            }),
            Arc::new(MockStorage::new(None)),
            make_topics(),
            14400,
            true,
        );

        assert!(content.dry_run);
        assert_eq!(content.post_window_secs, 14400);
        assert_eq!(content.topics.len(), 4);
        assert!(content.topic_scorer.is_none());
        assert!(content.thread_poster.is_none());
    }

    #[test]
    fn content_loop_with_topic_scorer() {
        let scorer = Arc::new(MockTopicScorer {
            top_topics: vec!["Rust".to_string()],
        });

        let content = ContentLoop::new(
            Arc::new(MockGenerator {
                response: "t".to_string(),
            }),
            Arc::new(MockSafety {
                can_tweet: true,
                can_thread: true,
            }),
            Arc::new(MockStorage::new(None)),
            make_topics(),
            14400,
            false,
        )
        .with_topic_scorer(scorer);

        assert!(content.topic_scorer.is_some());
    }

    #[test]
    fn content_result_debug() {
        let posted = ContentResult::Posted {
            topic: "Rust".to_string(),
            content: "hello".to_string(),
        };
        let debug = format!("{:?}", posted);
        assert!(debug.contains("Posted"));

        let too_soon = ContentResult::TooSoon {
            elapsed_secs: 10,
            window_secs: 3600,
        };
        let debug = format!("{:?}", too_soon);
        assert!(debug.contains("TooSoon"));

        let rate_limited = ContentResult::RateLimited;
        let debug = format!("{:?}", rate_limited);
        assert!(debug.contains("RateLimited"));

        let no_topics = ContentResult::NoTopics;
        let debug = format!("{:?}", no_topics);
        assert!(debug.contains("NoTopics"));

        let failed = ContentResult::Failed {
            error: "oops".to_string(),
        };
        let debug = format!("{:?}", failed);
        assert!(debug.contains("Failed"));
    }

    #[test]
    fn content_loop_empty_topics() {
        let content = ContentLoop::new(
            Arc::new(MockGenerator {
                response: "t".to_string(),
            }),
            Arc::new(MockSafety {
                can_tweet: true,
                can_thread: true,
            }),
            Arc::new(MockStorage::new(None)),
            vec![],
            14400,
            false,
        );
        assert!(content.topics.is_empty());
    }

    #[test]
    fn content_loop_with_thread_poster() {
        use crate::automation::loop_helpers::ThreadPoster;
        use crate::automation::ContentLoopError;

        struct MockThreadPoster;

        #[async_trait::async_trait]
        impl ThreadPoster for MockThreadPoster {
            async fn post_tweet(&self, _content: &str) -> Result<String, ContentLoopError> {
                Ok("tweet_id_1".to_string())
            }
            async fn reply_to_tweet(
                &self,
                _in_reply_to: &str,
                _content: &str,
            ) -> Result<String, ContentLoopError> {
                Ok("reply_id_1".to_string())
            }
        }

        let poster = Arc::new(MockThreadPoster);
        let content = ContentLoop::new(
            Arc::new(MockGenerator {
                response: "t".to_string(),
            }),
            Arc::new(MockSafety {
                can_tweet: true,
                can_thread: true,
            }),
            Arc::new(MockStorage::new(None)),
            make_topics(),
            14400,
            false,
        )
        .with_thread_poster(poster);

        assert!(content.thread_poster.is_some());
    }

    #[test]
    fn mock_storage_counts() {
        let storage = MockStorage::new(None);
        assert_eq!(storage.posted_count(), 0);
        assert_eq!(storage.action_count(), 0);
    }

    #[test]
    fn content_loop_dry_run_false() {
        let content = ContentLoop::new(
            Arc::new(MockGenerator {
                response: "t".to_string(),
            }),
            Arc::new(MockSafety {
                can_tweet: true,
                can_thread: true,
            }),
            Arc::new(MockStorage::new(None)),
            make_topics(),
            3600,
            false,
        );
        assert!(!content.dry_run);
        assert_eq!(content.post_window_secs, 3600);
    }
}